forked from puppeteer/puppeteer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch: - introduces a transpiler which substitutes async/await logic with generators. - starts using the transpiler to generate a node6-compatible version of puppeteer - introduces a runtime-check to decide which version of code to use Fixes puppeteer#316.
- Loading branch information
1 parent
46115f9
commit 9212863
Showing
12 changed files
with
292 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
third_party/* | ||
utils/doclint/check_public_api/test/ | ||
node6/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ | |
*.pyc | ||
.vscode | ||
package-lock.json | ||
/node6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Copyright 2017 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
describe('Puppeteer Sanity', function() { | ||
it('should not be insane', function(done) { | ||
const puppeteer = require('..'); | ||
puppeteer.launch().then(browser => { | ||
browser.newPage().then(page => { | ||
page.goto('data:text/html,hello').then(() => { | ||
page.evaluate(() => document.body.textContent).then(content => { | ||
expect(content).toBe('hello'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/** | ||
* Copyright 2017 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
const esprima = require('esprima'); | ||
const ESTreeWalker = require('../ESTreeWalker'); | ||
|
||
// This is converted from Babel's "transform-async-to-generator" | ||
// https://babeljs.io/docs/plugins/transform-async-to-generator/ | ||
const asyncToGenerator = fn => { | ||
const gen = fn.call(this); | ||
return new Promise((resolve, reject) => { | ||
function step(key, arg) { | ||
let info, value; | ||
try { | ||
info = gen[key](arg); | ||
value = info.value; | ||
} catch (error) { | ||
reject(error); | ||
return; | ||
} | ||
if (info.done) { | ||
resolve(value); | ||
} else { | ||
return Promise.resolve(value).then( | ||
value => { | ||
step('next', value); | ||
}, | ||
err => { | ||
step('throw', err); | ||
}); | ||
} | ||
} | ||
return step('next'); | ||
}); | ||
}; | ||
|
||
/** | ||
* @param {string} text | ||
*/ | ||
function transformAsyncFunctions(text) { | ||
const edits = []; | ||
|
||
const ast = esprima.parseScript(text, {range: true}); | ||
const walker = new ESTreeWalker(node => { | ||
if (node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration' || node.type === 'ArrowFunctionExpression') | ||
onFunction(node); | ||
else if (node.type === 'AwaitExpression') | ||
onAwait(node); | ||
}); | ||
walker.walk(ast); | ||
|
||
edits.sort((a, b) => b.from - a.from); | ||
for (const {replacement, from, to} of edits) | ||
text = text.substring(0, from) + replacement + text.substring(to); | ||
|
||
return text; | ||
|
||
/** | ||
* @param {ESTree.Node} node | ||
*/ | ||
function onFunction(node) { | ||
if (!node.async) return; | ||
|
||
let range; | ||
if (node.parent.type === 'MethodDefinition') | ||
range = node.parent.range; | ||
else | ||
range = node.range; | ||
const index = text.substring(range[0], range[1]).indexOf('async') + range[0]; | ||
insertText(index, index + 'async'.length, '/* async */'); | ||
|
||
let before = `{return (${asyncToGenerator.toString()})(function*()`; | ||
let after = `);}`; | ||
if (node.body.type !== 'BlockStatement') { | ||
before += `{ return `; | ||
after = `; }` + after; | ||
} | ||
insertText(node.body.range[0], node.body.range[0], before); | ||
insertText(node.body.range[1], node.body.range[1], after); | ||
} | ||
|
||
/** | ||
* @param {ESTree.Node} node | ||
*/ | ||
function onAwait(node) { | ||
const index = text.substring(node.range[0], node.range[1]).indexOf('await') + node.range[0]; | ||
insertText(index, index + 'await'.length, '(yield'); | ||
insertText(node.range[1], node.range[1], ')'); | ||
} | ||
|
||
/** | ||
* @param {number} from | ||
* @param {number} to | ||
* @param {string} replacement | ||
*/ | ||
function insertText(from, to, replacement) { | ||
edits.push({from, to, replacement}); | ||
} | ||
} | ||
|
||
module.exports = transformAsyncFunctions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Copyright 2017 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const removeRecursive = require('rimraf').sync; | ||
const transformAsyncFunctions = require('./TransformAsyncFunctions'); | ||
|
||
const dirPath = path.join(__dirname, '..', '..', 'lib'); | ||
const outPath = path.join(__dirname, '..', '..', 'node6'); | ||
const fileNames = fs.readdirSync(dirPath); | ||
const filePaths = fileNames.filter(fileName => fileName.endsWith('.js')); | ||
|
||
if (fs.existsSync(outPath)) | ||
removeRecursive(outPath); | ||
fs.mkdirSync(outPath); | ||
|
||
filePaths.forEach(filePath => { | ||
const content = fs.readFileSync(path.join(dirPath, filePath), 'utf8'); | ||
const output = transformAsyncFunctions(content); | ||
fs.writeFileSync(path.resolve(outPath, filePath), output); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* Copyright 2017 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
const transformAsyncFunctions = require('../TransformAsyncFunctions'); | ||
|
||
describe('TransformAsyncFunctions', function() { | ||
it('should convert a function expression', function(done) { | ||
const input = `(async function(){ return 123 })()`; | ||
const output = eval(transformAsyncFunctions(input)); | ||
expect(output instanceof Promise).toBe(true); | ||
output.then(result => expect(result).toBe(123)).then(done); | ||
}); | ||
it('should convert an arrow function', function(done) { | ||
const input = `(async () => 123)()`; | ||
const output = eval(transformAsyncFunctions(input)); | ||
expect(output instanceof Promise).toBe(true); | ||
output.then(result => expect(result).toBe(123)).then(done); | ||
}); | ||
it('should convert an arrow function with curly braces', function(done) { | ||
const input = `(async () => { return 123 })()`; | ||
const output = eval(transformAsyncFunctions(input)); | ||
expect(output instanceof Promise).toBe(true); | ||
output.then(result => expect(result).toBe(123)).then(done); | ||
}); | ||
it('should convert a function declaration', function(done) { | ||
const input = `async function f(){ return 123; } f();`; | ||
const output = eval(transformAsyncFunctions(input)); | ||
expect(output instanceof Promise).toBe(true); | ||
output.then(result => expect(result).toBe(123)).then(done); | ||
}); | ||
it('should convert await', function(done) { | ||
const input = `async function f(){ return 23 + await Promise.resolve(100); } f();`; | ||
const output = eval(transformAsyncFunctions(input)); | ||
expect(output instanceof Promise).toBe(true); | ||
output.then(result => expect(result).toBe(123)).then(done); | ||
}); | ||
it('should convert method', function(done) { | ||
const input = `class X{async f() { return 123 }} (new X()).f();`; | ||
const output = eval(transformAsyncFunctions(input)); | ||
expect(output instanceof Promise).toBe(true); | ||
output.then(result => expect(result).toBe(123)).then(done); | ||
}); | ||
it('should pass arguments', function(done) { | ||
const input = `(async function(a, b){ return await a + await b })(Promise.resolve(100), 23)`; | ||
const output = eval(transformAsyncFunctions(input)); | ||
expect(output instanceof Promise).toBe(true); | ||
output.then(result => expect(result).toBe(123)).then(done); | ||
}); | ||
it('should still work across eval', function(done) { | ||
const input = `var str = (async function(){ return 123; }).toString(); eval('(' + str + ')')();`; | ||
const output = eval(transformAsyncFunctions(input)); | ||
expect(output instanceof Promise).toBe(true); | ||
output.then(result => expect(result).toBe(123)).then(done); | ||
}); | ||
}); |