diff --git a/package.json b/package.json index a114ac68a2d..2c5f8f929a7 100644 --- a/package.json +++ b/package.json @@ -90,7 +90,6 @@ "setup-hooks": "node scripts/link-hooks.js" }, "browser": { - "buffer": false, "fs": false, "os": false, "path": false diff --git a/src/harness/harness.ts b/src/harness/harness.ts index 6c5b3ba4778..bf5cde2bc48 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -67,17 +67,16 @@ namespace Utils { export let currentExecutionEnvironment = getExecutionEnvironment(); - const Buffer: typeof global.Buffer = currentExecutionEnvironment !== ExecutionEnvironment.Browser - ? require("buffer").Buffer - : undefined; + // Thanks to browserify, Buffer is always available nowadays + const Buffer: typeof global.Buffer = require("buffer").Buffer; export function encodeString(s: string): string { - return Buffer ? (new Buffer(s)).toString("utf8") : s; + return Buffer.from(s).toString("utf8"); } export function byteLength(s: string, encoding?: string): number { // stub implementation if Buffer is not available (in-browser case) - return Buffer ? Buffer.byteLength(s, encoding) : s.length; + return Buffer.byteLength(s, encoding); } export function evalFile(fileContents: string, fileName: string, nodeContext?: any) { @@ -133,17 +132,18 @@ namespace Utils { return content; } - export function memoize(f: T): T { - const cache: { [idx: string]: any } = {}; + export function memoize(f: T, memoKey: (...anything: any[]) => string): T { + const cache = ts.createMap(); - return (function(this: any) { - const key = Array.prototype.join.call(arguments); - const cachedResult = cache[key]; - if (cachedResult) { - return cachedResult; + return (function(this: any, ...args: any[]) { + const key = memoKey(...args); + if (cache.has(key)) { + return cache.get(key); } else { - return cache[key] = f.apply(this, arguments); + const value = f.apply(this, args); + cache.set(key, value); + return value; } }); } @@ -686,7 +686,7 @@ namespace Harness { return dirPath; } - export let directoryName: typeof IO.directoryName = Utils.memoize(directoryNameImpl); + export let directoryName: typeof IO.directoryName = Utils.memoize(directoryNameImpl, path => path); export function resolvePath(path: string) { const response = Http.getFileFromServerSync(serverRoot + path + "?resolve=true"); @@ -703,21 +703,22 @@ namespace Harness { return response.status === 200; } - export let listFiles = Utils.memoize((path: string, spec?: RegExp): string[] => { + export const listFiles = Utils.memoize((path: string, spec?: RegExp, options?: { recursive?: boolean }): string[] => { const response = Http.getFileFromServerSync(serverRoot + path); if (response.status === 200) { - const results = response.responseText.split(","); + let results = response.responseText.split(","); if (spec) { - return results.filter(file => spec.test(file)); + results = results.filter(file => spec.test(file)); } - else { - return results; + if (options && !options.recursive) { + results = results.filter(file => (ts.getDirectoryPath(ts.normalizeSlashes(file)) === path)); } + return results; } else { return [""]; } - }); + }, (path: string, spec?: RegExp, options?: { recursive?: boolean }) => `${path}|${spec}|${options ? options.recursive : undefined}`); export function readFile(file: string): string | undefined { const response = Http.getFileFromServerSync(serverRoot + file); @@ -1989,7 +1990,7 @@ namespace Harness { IO.writeFile(actualFileName + ".delete", ""); } else { - IO.writeFile(actualFileName, actual); + IO.writeFile(actualFileName, encoded_actual); } throw new Error(`The baseline file ${relativeFileName} has changed.`); } diff --git a/tests/webTestServer.ts b/tests/webTestServer.ts index 20228f9a741..5a3b4cc5048 100644 --- a/tests/webTestServer.ts +++ b/tests/webTestServer.ts @@ -288,7 +288,7 @@ console.log(`Static file server running at\n => http://localhost:${port}/\nCTRL http.createServer((req: http.ServerRequest, res: http.ServerResponse) => { log(`${req.method} ${req.url}`); - const uri = url.parse(req.url).pathname; + const uri = decodeURIComponent(url.parse(req.url).pathname); const reqPath = path.join(process.cwd(), uri); const operation = getRequestOperation(req); handleRequestOperation(req, res, operation, reqPath);