Skip to content

Commit 9641deb

Browse files
authored
Merge pull request #9 from duart38/local-file-import-from-net-lib-fails
Local file import from net lib fails
2 parents eb528b6 + 1184fa1 commit 9641deb

File tree

5 files changed

+45
-100
lines changed

5 files changed

+45
-100
lines changed

Thread.bundle.js

Lines changed: 0 additions & 83 deletions
This file was deleted.

Thread.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export default class Thread<T = unknown, K = unknown> {
88
private imports: Array<string>;
99
private blob: Promise<Blob>;
1010
private blobURL = "";
11+
public debugMode: boolean;
12+
1113
/**
1214
* Tells if the worker has been stopped
1315
*/
@@ -23,8 +25,12 @@ export default class Thread<T = unknown, K = unknown> {
2325
) => T | Promise<T>,
2426
type?: "classic" | "module",
2527
imports?: Array<string>,
28+
opts: { debug?: boolean } = { debug: false },
2629
) {
30+
this.debugMode = opts.debug ?? false;
2731
this.imports = imports || [];
32+
33+
// these methods are asynchronous, because we're in the constructor, we must make sure they're at the end
2834
this.blob = this.populateFile(operation);
2935
this.worker = this.makeWorker(type);
3036
}
@@ -44,17 +50,18 @@ export default class Thread<T = unknown, K = unknown> {
4450
const imported = this.imports?.flatMap(async (val) =>
4551
(await this.copyDep(val)).join("\n")
4652
);
47-
return new Blob([`
48-
${(await Promise.all(imported)).join("\n")}
49-
50-
var global = {};
51-
var userCode = ${code.toString()}
52-
53-
onmessage = async function(e) {
54-
postMessage(await userCode(e, global));
55-
}
56-
57-
`]);
53+
const blobContent = `
54+
${(await Promise.all(imported)).join("\n")}
55+
56+
var global = {};
57+
var userCode = ${code.toString()}
58+
59+
onmessage = async function(e) {
60+
postMessage(await userCode(e, global));
61+
}
62+
`;
63+
this.debug(`Blob content:${blobContent}\n\n\n`);
64+
return new Blob([blobContent]);
5865
}
5966

6067
/**
@@ -65,6 +72,8 @@ export default class Thread<T = unknown, K = unknown> {
6572
const importPathRegex = /('|"|`)(.+(\.js|\.ts))(\1)/ig; // for the path string ("lorem/ipsum.js")
6673
const importInsRegex = /(import( |))({.+}|.+)(from( |))/ig; // for the instruction before the path (import {som} from)
6774
const matchedPath = importPathRegex.exec(str) || "";
75+
this.debug("attempting to import: ", str);
76+
6877
let file = false;
6978
let fqfn = "";
7079

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

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

8797
if (file) {
88-
const x = await import(fqfn); //Deno.realPathSync(fqfn)
98+
this.debug(
99+
"importing file: ",
100+
import.meta.resolve("file://" + Deno.realPathSync(fqfn)),
101+
);
102+
const x = await import("file://" + Deno.realPathSync(fqfn));
103+
this.debug(
104+
"file imported, inlining the following: ",
105+
Object.keys(x).join(","),
106+
);
89107
return Object.keys(x).map((v) => x[v].toString());
90108
} else {
91109
const filePath = matchedPath[0].replaceAll(/'|"/g, "");
110+
this.debug("importing from the net: ", filePath);
92111
if (filePath.endsWith(".ts")) {
93-
return [str]; // dont import the content if ts just paste import string
112+
this.debug("filePath ends with .ts, returning: ", str);
113+
return [str]; // do nothing if plain import string
94114
}
95115
const x = await import(filePath);
116+
this.debug(
117+
"imported from the net, inlining the following: ",
118+
Object.keys(x).join(","),
119+
);
96120
return Object.keys(x).map((v) => x[v].toString());
97121
}
98122
}
99123

124+
private debug(...msg: unknown[]) {
125+
if (this.debugMode) console.debug(`[${new Date()}]\t`, ...msg);
126+
}
127+
100128
/**
101129
* Sends data to the Thread
102130
* @param msg

egg.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"entry": "./Thread.ts",
55
"description": "Type-safe multi-threading made 'easier'",
66
"homepage": "https://github.com/duart38/Thread",
7-
"version": "4.1.0",
7+
"version": "4.2.0",
88
"releaseType": null,
99
"unstable": false,
1010
"unlisted": false,

examples/example_async_support.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import Thread from "../Thread.ts";
99
const thread = new Thread<number, number[]>(async (e) => {
1010
console.log("Worker: Message received from main script");
1111
const result = e.data[0] * e.data[1];
12-
await new Promise((resolve) => setTimeout(resolve, 5 * 1000))
12+
await new Promise((resolve) => setTimeout(resolve, 5 * 1000));
1313
if (isNaN(result)) {
1414
return 0;
1515
} else {
1616
console.log("Worker: Posting message back to main script");
17-
return (result);
17+
return result;
1818
}
1919
}, "module");
2020

examples/example_simple.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const thread = new Thread<number, number[]>((e) => {
77
return 0;
88
} else {
99
console.log("Worker: Posting message back to main script");
10-
return (result);
10+
return result;
1111
}
1212
});
1313

0 commit comments

Comments
 (0)