MCPcopy Create free account
hub / github.com/tester-army/e2e / initScriptSource

Function initScriptSource

packages/web/src/init-scripts.ts:102–117  ·  view source on GitHub ↗

* The page source of one script. A function is called with its JSON argument * as Playwright calls it, beside the `__name` helper tsx's output needs; the * argument is parsed in the page, since an object literal would turn an own * `__proto__` key into the prototype. A * file gets a `sourceURL`,

(
  script: WebInitScript,
  arg: JsonValue | undefined,
  resolvePath: (file: string) => string,
)

Source from the content-addressed store, hash-verified

100 * file gets a `sourceURL`, so a stack trace in the page names it.
101 */
102async function initScriptSource(
103 script: WebInitScript,
104 arg: JsonValue | undefined,
105 resolvePath: (file: string) => string,
106): Promise<string> {
107 if (typeof script === 'string') return script;
108 if (typeof script === 'function') {
109 return `(() => {\n${KEEP_NAMES_HELPER}\n(${script.toString()}\n)(${arg === undefined ? '' : `JSON.parse(${JSON.stringify(JSON.stringify(arg))})`});\n})();`;
110 }
111 const file = resolvePath(script.path);
112 try {
113 return `${await readFile(file, 'utf8')}\n//# sourceURL=${file.replace(/[\r\n]/g, '')}`;
114 } catch (cause) {
115 throw new Error(`path cannot be read: ${file} (${(cause as NodeJS.ErrnoException).code ?? message(cause)})`, { cause });
116 }
117}

Callers 2

loadMethod · 0.85
testInitScriptSourceFunction · 0.85

Calls 3

messageFunction · 0.90
toStringMethod · 0.80
replaceMethod · 0.80

Tested by

no test coverage detected