Skip to content

Commit

Permalink
fix #596: toString for transform input
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Dec 14, 2020
1 parent 9db53e7 commit 66170fd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
8 changes: 5 additions & 3 deletions lib/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ export const startService: typeof types.startService = options => {
new Promise<types.BuildResult>((resolve, reject) =>
service.buildOrServe(null, options, false, (err, res) =>
err ? reject(err) : resolve(res as types.BuildResult))),
transform: (input, options) =>
new Promise((resolve, reject) =>
transform: (input, options) => {
input += '';
return new Promise((resolve, reject) =>
service.transform(input, options || {}, false, {
readFile(_, callback) { callback(new Error('Internal error'), null); },
writeFile(_, callback) { callback(null); },
}, (err, res) => err ? reject(err) : resolve(res!))),
}, (err, res) => err ? reject(err) : resolve(res!)))
},
serve() {
throw new Error(`The "serve" API only works in node`)
},
Expand Down
4 changes: 2 additions & 2 deletions lib/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ export function createChannel(streamIn: StreamIn): StreamOut {
command: 'transform',
flags,
inputFS: inputPath !== null,
input: inputPath !== null ? inputPath : input + '',
input: inputPath !== null ? inputPath : input,
};
sendRequest<protocol.TransformRequest, protocol.TransformResponse>(request, (error, response) => {
if (error) return callback(new Error(error), null);
Expand Down Expand Up @@ -774,7 +774,7 @@ export function createChannel(streamIn: StreamIn): StreamOut {
});
}
};
if (typeof input === 'string' && input.length > 1024 * 1024) {
if (input.length > 1024 * 1024) {
let next = start;
start = () => fs.writeFile(input, next);
}
Expand Down
11 changes: 8 additions & 3 deletions lib/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export let serve: typeof types.serve = (serveOptions, buildOptions) => {
};

export let transform: typeof types.transform = (input, options) => {
input += '';
return startService().then(service => {
let promise = service.transform(input, options);
promise.then(service.stop, service.stop);
Expand All @@ -124,6 +125,8 @@ export let buildSync: typeof types.buildSync = (options: types.BuildOptions): an
};

export let transformSync: typeof types.transformSync = (input, options) => {
input += '';

// Try using a long-lived worker thread to avoid repeated start-up overhead
if (worker_threads) {
if (!workerThreadService) workerThreadService = startWorkerThreadService(worker_threads);
Expand Down Expand Up @@ -195,8 +198,9 @@ export let startService: typeof types.startService = options => {
service.buildOrServe(serveOptions, buildOptions, isTTY(), (err, res) =>
err ? reject(err) : resolve(res as types.ServeResult)))
},
transform: (input, options) =>
new Promise((resolve, reject) =>
transform: (input, options) => {
input += '';
return new Promise((resolve, reject) =>
service.transform(input, options || {}, isTTY(), {
readFile(tempFile, callback) {
try {
Expand All @@ -220,7 +224,8 @@ export let startService: typeof types.startService = options => {
callback(null);
}
},
}, (err, res) => err ? reject(err) : resolve(res!))),
}, (err, res) => err ? reject(err) : resolve(res!)));
},
stop() { child.kill(); },
});
};
Expand Down
27 changes: 27 additions & 0 deletions scripts/js-api-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,21 @@ async function futureSyntax(service, js, targetBelow, targetAbove) {
}

let transformTests = {
async transformWithNonString({ esbuild, service }) {
for (let toTest of [esbuild, service]) {
try {
// Do not "await" here. The error should be thrown outside of the promise.
toTest.transform({ toString() { throw new Error('toString() error') } })
throw new Error('Expected an error to be thrown');
} catch (e) {
assert.strictEqual(e.message, 'toString() error')
}

var { code } = await toTest.transform(Buffer.from(`1+2`))
assert.strictEqual(code, `1 + 2;\n`)
}
},

async version({ esbuild }) {
const version = fs.readFileSync(path.join(repoDir, 'version.txt'), 'utf8').trim()
assert.strictEqual(esbuild.version, version);
Expand Down Expand Up @@ -2156,6 +2171,18 @@ let syncTests = {
assert.strictEqual(map, '')
},

async transformSyncWithNonString({ esbuild }) {
try {
esbuild.transformSync({ toString() { throw new Error('toString() error') } })
throw new Error('Expected an error to be thrown');
} catch (e) {
assert.strictEqual(e.message, 'toString() error')
}

var { code } = await esbuild.transformSync(Buffer.from(`1+2`))
assert.strictEqual(code, `1 + 2;\n`)
},

async transformSync100x({ esbuild }) {
for (let i = 0; i < 100; i++) {
const { code } = esbuild.transformSync(`console.log(1+${i})`, {})
Expand Down

0 comments on commit 66170fd

Please sign in to comment.