Skip to content

Commit

Permalink
Fixing issue with polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
Pauan committed Jan 30, 2025
1 parent 2975260 commit dcb529c
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 212 deletions.
54 changes: 34 additions & 20 deletions sdk/src/polyfill/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,44 @@ import $mime from "mime/lite";

const oldFetch = globalThis.fetch;

// We always polyfill fetch because Node's fetch doesn't support file URLs.
(globalThis.fetch as any) = async function (resource: URL | RequestInfo, options: RequestInit | undefined): Promise<Response> {
const request = new Request(resource, options);

const url = new URL(request.url);
async function supportsFetch() {
try {
await oldFetch(new URL("file:"));
return true;

if (url.protocol === "file:") {
const readStream = $fs.createReadStream(url);
} catch (e) {
return false;
}
}

const headers: HeadersInit = {};

const type = $mime.getType(url.pathname);
if (!(await supportsFetch())) {
// We always polyfill fetch because Node's fetch doesn't support file URLs.
(globalThis.fetch as any) = async function (resource: URL | RequestInfo, options: RequestInit | undefined): Promise<Response> {
const request = new Request(resource, options);

if (type) {
headers["Content-Type"] = type;
}
const url = new URL(request.url);

return new Response(readStream as any, {
status: 200,
statusText: "OK",
headers,
});
if (url.protocol === "file:") {
const readStream = $fs.createReadStream(url);

} else {
return await oldFetch(request);
}
};
const headers: HeadersInit = {};

const type = $mime.getType(url.pathname);

if (type) {
headers["Content-Type"] = type;
}

return new Response(readStream as any, {
status: 200,
statusText: "OK",
headers,
});

} else {
return await oldFetch(request);
}
};
}
142 changes: 72 additions & 70 deletions sdk/src/polyfill/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,90 +10,92 @@ if (globalThis.navigator == null) {
} as Navigator;
}

globalThis.Worker = class Worker extends EventTarget {
private _worker: import("node:worker_threads").Worker;
if (globalThis.Worker == null) {
globalThis.Worker = class Worker extends EventTarget {
private _worker: import("node:worker_threads").Worker;

constructor(url: string | URL, options?: WorkerOptions | undefined) {
super();
constructor(url: string | URL, options?: WorkerOptions | undefined) {
super();

if (url instanceof URL) {
if (url.protocol !== "file:") {
throw new Error("Worker only supports file: URLs");
if (url instanceof URL) {
if (url.protocol !== "file:") {
throw new Error("Worker only supports file: URLs");
}

url = url.href;

} else {
throw new Error("Filepaths are unreliable, use `new URL(\"...\", import.meta.url)` instead.");
}

url = url.href;
if (!options || options.type !== "module") {
throw new Error("Workers must use \`type: \"module\"\`");
}

} else {
throw new Error("Filepaths are unreliable, use `new URL(\"...\", import.meta.url)` instead.");
const code = `
import("node:worker_threads")
.then(({ workerData }) => {
return import(workerData.polyfill)
.then(() => import(workerData.url))
})
.catch((e) => {
// TODO maybe it should send a message to the parent?
console.error(e.stack);
});
`;

this._worker = new $worker.Worker(code, {
eval: true,
workerData: {
url,
polyfill: new URL("node-polyfill.js", import.meta.url).href,
},
});

this._worker.on("message", (data) => {
this.dispatchEvent(new MessageEvent("message", { data }));
});

this._worker.on("messageerror", (error) => {
throw new Error("UNIMPLEMENTED");
});

this._worker.on("error", (error) => {
// TODO attach the error to the event somehow
const event = new Event("error");
this.dispatchEvent(event);
});
}

if (!options || options.type !== "module") {
throw new Error("Workers must use \`type: \"module\"\`");
set onmessage(f: () => void) {
throw new Error("UNIMPLEMENTED");
}

const code = `
import("node:worker_threads")
.then(({ workerData }) => {
return import(workerData.polyfill)
.then(() => import(workerData.url))
})
.catch((e) => {
// TODO maybe it should send a message to the parent?
console.error(e.stack);
});
`;

this._worker = new $worker.Worker(code, {
eval: true,
workerData: {
url,
polyfill: new URL("node-polyfill.js", import.meta.url).href,
},
});

this._worker.on("message", (data) => {
this.dispatchEvent(new MessageEvent("message", { data }));
});

this._worker.on("messageerror", (error) => {
set onmessageerror(f: () => void) {
throw new Error("UNIMPLEMENTED");
});

this._worker.on("error", (error) => {
// TODO attach the error to the event somehow
const event = new Event("error");
this.dispatchEvent(event);
});
}

set onmessage(f: () => void) {
throw new Error("UNIMPLEMENTED");
}

set onmessageerror(f: () => void) {
throw new Error("UNIMPLEMENTED");
}
}

set onerror(f: () => void) {
throw new Error("UNIMPLEMENTED");
}
set onerror(f: () => void) {
throw new Error("UNIMPLEMENTED");
}

postMessage(message: any, transfer: Array<Transferable>): void;
postMessage(message: any, options?: StructuredSerializeOptions | undefined): void;
postMessage(value: any, transfer: any) {
this._worker.postMessage(value, transfer);
}
postMessage(message: any, transfer: Array<Transferable>): void;
postMessage(message: any, options?: StructuredSerializeOptions | undefined): void;
postMessage(value: any, transfer: any) {
this._worker.postMessage(value, transfer);
}

terminate() {
this._worker.terminate();
}
terminate() {
this._worker.terminate();
}

// This is Node-specific, it allows the process to exit
// even if the Worker is still running.
unref() {
this._worker.unref();
}
};
// This is Node-specific, it allows the process to exit
// even if the Worker is still running.
unref() {
this._worker.unref();
}
};
}


if (!$worker.isMainThread) {
Expand Down
Loading

0 comments on commit dcb529c

Please sign in to comment.