@@ -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 = / ( ' | " | ` ) ( .+ ( \. j s | \. t s ) ) ( \1) / ig; // for the path string ("lorem/ipsum.js")
6673 const importInsRegex = / ( i m p o r t ( | ) ) ( { .+ } | .+ ) ( f r o m ( | ) ) / 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
0 commit comments