mirror of
https://github.com/microsoft/playwright.git
synced 2024-09-21 01:17:43 +03:00
fix: avoid using builtins in injected script bundles (#17145)
This commit is contained in:
parent
8d25f2ef59
commit
fbf9ca5316
@ -662,3 +662,21 @@ it('should throw when frame is detached', async ({ page, server }) => {
|
||||
expect(error).toBeTruthy();
|
||||
expect(error.message).toMatch(/frame.evaluate: (Frame was detached|Execution context was destroyed)/);
|
||||
});
|
||||
|
||||
it('should work with overridden Object.defineProperty', async ({ page, server }) => {
|
||||
server.setRoute('/test', (req, res) => {
|
||||
res.writeHead(200, {
|
||||
'content-type': 'text/html',
|
||||
});
|
||||
res.end(`<script>
|
||||
Object.create = null;
|
||||
Object.defineProperty = null;
|
||||
Object.getOwnPropertyDescriptor = null;
|
||||
Object.getOwnPropertyNames = null;
|
||||
Object.getPrototypeOf = null;
|
||||
Object.prototype.hasOwnProperty = null;
|
||||
</script>`);
|
||||
});
|
||||
await page.goto(server.PREFIX + '/test');
|
||||
expect(await page.evaluate('1+2')).toBe(3);
|
||||
});
|
||||
|
@ -29,6 +29,40 @@ const injectedScripts = [
|
||||
path.join(ROOT, 'packages', 'playwright-core', 'src', 'server', 'injected', 'recorder.ts'),
|
||||
];
|
||||
|
||||
const modulePrefix = `"use strict";
|
||||
let __export = (target, all) => {
|
||||
for (var name in all)
|
||||
target[name] = all[name];
|
||||
};
|
||||
let __commonJS = cb => function __require() {
|
||||
let fn;
|
||||
for (const name in cb) {
|
||||
fn = cb[name];
|
||||
break;
|
||||
}
|
||||
const exports = {};
|
||||
fn(exports);
|
||||
return exports;
|
||||
};
|
||||
let __toESM = mod => ({ ...mod, 'default': mod });
|
||||
let __toCommonJS = mod => ({ ...mod, __esModule: true });
|
||||
`;
|
||||
|
||||
async function replaceEsbuildHeader(content, outFileJs) {
|
||||
const sourcesStart = content.indexOf('// packages/playwright-core/src/server');
|
||||
if (sourcesStart === -1)
|
||||
throw new Error(`Did not find start of bundled code in ${outFileJs}`);
|
||||
|
||||
const preambule = content.substring(0, sourcesStart);
|
||||
// Replace standard esbuild definition with our own which do not depend on builtins.
|
||||
// See https://github.com/microsoft/playwright/issues/17029
|
||||
if (preambule.indexOf('__toESM') !== -1 || preambule.indexOf('__toCommonJS') !== -1) {
|
||||
content = modulePrefix + content.substring(sourcesStart);
|
||||
await fs.promises.writeFile(outFileJs, content);
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
(async () => {
|
||||
const generatedFolder = path.join(ROOT, 'packages', 'playwright-core', 'src', 'generated');
|
||||
await fs.promises.mkdir(generatedFolder, { recursive: true });
|
||||
@ -43,7 +77,9 @@ const injectedScripts = [
|
||||
target: 'ES2019'
|
||||
});
|
||||
const baseName = path.basename(injected);
|
||||
const content = await fs.promises.readFile(path.join(outdir, baseName.replace('.ts', '.js')), 'utf-8');
|
||||
const outFileJs = path.join(outdir, baseName.replace('.ts', '.js'));
|
||||
let content = await fs.promises.readFile(outFileJs, 'utf-8');
|
||||
content = await replaceEsbuildHeader(content, outFileJs);
|
||||
const newContent = `export const source = ${JSON.stringify(content)};`;
|
||||
await fs.promises.writeFile(path.join(generatedFolder, baseName.replace('.ts', 'Source.ts')), newContent);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user