Skip to content

Commit b90cb1a

Browse files
authored
Merge pull request #961 from Pauan/fix/polyfill
Fixing issue with polyfill
2 parents 2975260 + 79fd85b commit b90cb1a

File tree

7 files changed

+170
-222
lines changed

7 files changed

+170
-222
lines changed

sdk/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
"comlink": "^4.4.2",
5252
"core-js": "^3.40.0",
5353
"mime": "^4.0.6",
54-
"sync-request": "^6.1.0"
54+
"sync-request": "^6.1.0",
55+
"xmlhttprequest-ssl": "^3.1.0"
5556
},
5657
"devDependencies": {
5758
"@rollup/plugin-replace": "^6.0.2",

sdk/rollup.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export default networks.map((network) => {
2828
"node:crypto",
2929
"mime/lite",
3030
"sync-request",
31+
"xmlhttprequest-ssl",
3132

3233
// Used by the SDK
3334
"comlink",

sdk/rollup.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export default networks.map((network) => {
3535
"node:crypto",
3636
"mime/lite",
3737
"sync-request",
38+
"xmlhttprequest-ssl",
3839

3940
// Used by the SDK
4041
"comlink",

sdk/src/polyfill/fetch.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,35 @@ import $mime from "mime/lite";
44

55
const oldFetch = globalThis.fetch;
66

7+
8+
let supports: Promise<boolean> | null = null;
9+
10+
async function checkFetch() {
11+
try {
12+
await oldFetch(new URL("file:"));
13+
return true;
14+
15+
} catch (e) {
16+
return false;
17+
}
18+
}
19+
20+
async function supportsFetch(): Promise<boolean> {
21+
if (supports === null) {
22+
supports = checkFetch();
23+
}
24+
25+
return await supports;
26+
}
27+
28+
729
// We always polyfill fetch because Node's fetch doesn't support file URLs.
830
(globalThis.fetch as any) = async function (resource: URL | RequestInfo, options: RequestInit | undefined): Promise<Response> {
931
const request = new Request(resource, options);
1032

1133
const url = new URL(request.url);
1234

13-
if (url.protocol === "file:") {
35+
if (!(await supportsFetch()) && url.protocol === "file:") {
1436
const readStream = $fs.createReadStream(url);
1537

1638
const headers: HeadersInit = {};

sdk/src/polyfill/worker.ts

Lines changed: 72 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -10,90 +10,92 @@ if (globalThis.navigator == null) {
1010
} as Navigator;
1111
}
1212

13-
globalThis.Worker = class Worker extends EventTarget {
14-
private _worker: import("node:worker_threads").Worker;
13+
if (globalThis.Worker == null) {
14+
globalThis.Worker = class Worker extends EventTarget {
15+
private _worker: import("node:worker_threads").Worker;
1516

16-
constructor(url: string | URL, options?: WorkerOptions | undefined) {
17-
super();
17+
constructor(url: string | URL, options?: WorkerOptions | undefined) {
18+
super();
1819

19-
if (url instanceof URL) {
20-
if (url.protocol !== "file:") {
21-
throw new Error("Worker only supports file: URLs");
20+
if (url instanceof URL) {
21+
if (url.protocol !== "file:") {
22+
throw new Error("Worker only supports file: URLs");
23+
}
24+
25+
url = url.href;
26+
27+
} else {
28+
throw new Error("Filepaths are unreliable, use `new URL(\"...\", import.meta.url)` instead.");
2229
}
2330

24-
url = url.href;
31+
if (!options || options.type !== "module") {
32+
throw new Error("Workers must use \`type: \"module\"\`");
33+
}
2534

26-
} else {
27-
throw new Error("Filepaths are unreliable, use `new URL(\"...\", import.meta.url)` instead.");
35+
const code = `
36+
import("node:worker_threads")
37+
.then(({ workerData }) => {
38+
return import(workerData.polyfill)
39+
.then(() => import(workerData.url))
40+
})
41+
.catch((e) => {
42+
// TODO maybe it should send a message to the parent?
43+
console.error(e.stack);
44+
});
45+
`;
46+
47+
this._worker = new $worker.Worker(code, {
48+
eval: true,
49+
workerData: {
50+
url,
51+
polyfill: new URL("node-polyfill.js", import.meta.url).href,
52+
},
53+
});
54+
55+
this._worker.on("message", (data) => {
56+
this.dispatchEvent(new MessageEvent("message", { data }));
57+
});
58+
59+
this._worker.on("messageerror", (error) => {
60+
throw new Error("UNIMPLEMENTED");
61+
});
62+
63+
this._worker.on("error", (error) => {
64+
// TODO attach the error to the event somehow
65+
const event = new Event("error");
66+
this.dispatchEvent(event);
67+
});
2868
}
2969

30-
if (!options || options.type !== "module") {
31-
throw new Error("Workers must use \`type: \"module\"\`");
70+
set onmessage(f: () => void) {
71+
throw new Error("UNIMPLEMENTED");
3272
}
3373

34-
const code = `
35-
import("node:worker_threads")
36-
.then(({ workerData }) => {
37-
return import(workerData.polyfill)
38-
.then(() => import(workerData.url))
39-
})
40-
.catch((e) => {
41-
// TODO maybe it should send a message to the parent?
42-
console.error(e.stack);
43-
});
44-
`;
45-
46-
this._worker = new $worker.Worker(code, {
47-
eval: true,
48-
workerData: {
49-
url,
50-
polyfill: new URL("node-polyfill.js", import.meta.url).href,
51-
},
52-
});
53-
54-
this._worker.on("message", (data) => {
55-
this.dispatchEvent(new MessageEvent("message", { data }));
56-
});
57-
58-
this._worker.on("messageerror", (error) => {
74+
set onmessageerror(f: () => void) {
5975
throw new Error("UNIMPLEMENTED");
60-
});
61-
62-
this._worker.on("error", (error) => {
63-
// TODO attach the error to the event somehow
64-
const event = new Event("error");
65-
this.dispatchEvent(event);
66-
});
67-
}
68-
69-
set onmessage(f: () => void) {
70-
throw new Error("UNIMPLEMENTED");
71-
}
72-
73-
set onmessageerror(f: () => void) {
74-
throw new Error("UNIMPLEMENTED");
75-
}
76+
}
7677

77-
set onerror(f: () => void) {
78-
throw new Error("UNIMPLEMENTED");
79-
}
78+
set onerror(f: () => void) {
79+
throw new Error("UNIMPLEMENTED");
80+
}
8081

81-
postMessage(message: any, transfer: Array<Transferable>): void;
82-
postMessage(message: any, options?: StructuredSerializeOptions | undefined): void;
83-
postMessage(value: any, transfer: any) {
84-
this._worker.postMessage(value, transfer);
85-
}
82+
postMessage(message: any, transfer: Array<Transferable>): void;
83+
postMessage(message: any, options?: StructuredSerializeOptions | undefined): void;
84+
postMessage(value: any, transfer: any) {
85+
this._worker.postMessage(value, transfer);
86+
}
8687

87-
terminate() {
88-
this._worker.terminate();
89-
}
88+
terminate() {
89+
this._worker.terminate();
90+
}
9091

91-
// This is Node-specific, it allows the process to exit
92-
// even if the Worker is still running.
93-
unref() {
94-
this._worker.unref();
95-
}
96-
};
92+
// This is Node-specific, it allows the process to exit
93+
// even if the Worker is still running.
94+
unref() {
95+
this._worker.unref();
96+
}
97+
};
98+
}
9799

98100

99101
if (!$worker.isMainThread) {

0 commit comments

Comments
 (0)