Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local file import from net lib fails #9

Merged
merged 9 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 0 additions & 83 deletions Thread.bundle.js

This file was deleted.

54 changes: 41 additions & 13 deletions Thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export default class Thread<T = unknown, K = unknown> {
private imports: Array<string>;
private blob: Promise<Blob>;
private blobURL = "";
public debugMode: boolean;

/**
* Tells if the worker has been stopped
*/
Expand All @@ -23,8 +25,12 @@ export default class Thread<T = unknown, K = unknown> {
) => T | Promise<T>,
type?: "classic" | "module",
imports?: Array<string>,
opts: { debug?: boolean } = { debug: false },
) {
this.debugMode = opts.debug ?? false;
this.imports = imports || [];

// these methods are asynchronous, because we're in the constructor, we must make sure they're at the end
this.blob = this.populateFile(operation);
this.worker = this.makeWorker(type);
}
Expand All @@ -44,17 +50,18 @@ export default class Thread<T = unknown, K = unknown> {
const imported = this.imports?.flatMap(async (val) =>
(await this.copyDep(val)).join("\n")
);
return new Blob([`
${(await Promise.all(imported)).join("\n")}

var global = {};
var userCode = ${code.toString()}

onmessage = async function(e) {
postMessage(await userCode(e, global));
}

`]);
const blobContent = `
${(await Promise.all(imported)).join("\n")}

var global = {};
var userCode = ${code.toString()}

onmessage = async function(e) {
postMessage(await userCode(e, global));
}
`;
this.debug(`Blob content:${blobContent}\n\n\n`);
return new Blob([blobContent]);
}

/**
Expand All @@ -65,6 +72,8 @@ export default class Thread<T = unknown, K = unknown> {
const importPathRegex = /('|"|`)(.+(\.js|\.ts))(\1)/ig; // for the path string ("lorem/ipsum.js")
const importInsRegex = /(import( |))({.+}|.+)(from( |))/ig; // for the instruction before the path (import {som} from)
const matchedPath = importPathRegex.exec(str) || "";
this.debug("attempting to import: ", str);

let file = false;
let fqfn = "";

Expand All @@ -74,6 +83,7 @@ export default class Thread<T = unknown, K = unknown> {
) {
file = true;
fqfn = matchedPath[0].replaceAll(/('|"|`)/ig, "");
this.debug("file identified as local file");
}
const matchedIns = importInsRegex.exec(str) || ""; // matchedIns[0] > import {sss} from

Expand All @@ -85,18 +95,36 @@ export default class Thread<T = unknown, K = unknown> {
}

if (file) {
const x = await import(fqfn); //Deno.realPathSync(fqfn)
this.debug(
"importing file: ",
import.meta.resolve("file://" + Deno.realPathSync(fqfn)),
);
const x = await import("file://" + Deno.realPathSync(fqfn));
this.debug(
"file imported, inlining the following: ",
Object.keys(x).join(","),
);
return Object.keys(x).map((v) => x[v].toString());
} else {
const filePath = matchedPath[0].replaceAll(/'|"/g, "");
this.debug("importing from the net: ", filePath);
if (filePath.endsWith(".ts")) {
return [str]; // dont import the content if ts just paste import string
this.debug("filePath ends with .ts, returning: ", str);
return [str]; // do nothing if plain import string
}
const x = await import(filePath);
this.debug(
"imported from the net, inlining the following: ",
Object.keys(x).join(","),
);
return Object.keys(x).map((v) => x[v].toString());
}
}

private debug(...msg: unknown[]) {
if (this.debugMode) console.debug(`[${new Date()}]\t`, ...msg);
}

/**
* Sends data to the Thread
* @param msg
Expand Down
2 changes: 1 addition & 1 deletion egg.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"entry": "./Thread.ts",
"description": "Type-safe multi-threading made 'easier'",
"homepage": "https://github.com/duart38/Thread",
"version": "4.1.0",
"version": "4.2.0",
"releaseType": null,
"unstable": false,
"unlisted": false,
Expand Down
4 changes: 2 additions & 2 deletions examples/example_async_support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import Thread from "../Thread.ts";
const thread = new Thread<number, number[]>(async (e) => {
console.log("Worker: Message received from main script");
const result = e.data[0] * e.data[1];
await new Promise((resolve) => setTimeout(resolve, 5 * 1000))
await new Promise((resolve) => setTimeout(resolve, 5 * 1000));
if (isNaN(result)) {
return 0;
} else {
console.log("Worker: Posting message back to main script");
return (result);
return result;
}
}, "module");

Expand Down
2 changes: 1 addition & 1 deletion examples/example_simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const thread = new Thread<number, number[]>((e) => {
return 0;
} else {
console.log("Worker: Posting message back to main script");
return (result);
return result;
}
});

Expand Down
Loading