@utoo/web 1.2.0-rc.1 → 1.2.0-rc.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/README.md +55 -0
  2. package/esm/{aa0ad15d2a587c43406f.wasm → a0a815481b4ff49f8961.wasm} +0 -0
  3. package/esm/index.d.ts +3 -2
  4. package/esm/index.js +3 -2
  5. package/esm/loaderWorker.js +1 -1
  6. package/esm/loaderWorker.js.LICENSE.txt +0 -21
  7. package/esm/{forkedProject.d.ts → project/ForkedProject.d.ts} +5 -3
  8. package/esm/{forkedProject.js → project/ForkedProject.js} +8 -0
  9. package/esm/{internalProject.d.ts → project/InternalProject.d.ts} +4 -3
  10. package/esm/project/InternalProject.js +132 -0
  11. package/esm/{project.d.ts → project/Project.d.ts} +3 -1
  12. package/esm/{project.js → project/Project.js} +19 -6
  13. package/esm/serviceWorker.js +4 -3
  14. package/esm/{type.d.ts → types.d.ts} +43 -0
  15. package/esm/types.js +56 -0
  16. package/esm/utils/sabcom.d.ts +61 -0
  17. package/esm/utils/sabcom.js +203 -0
  18. package/esm/utoo/index.d.ts +73 -38
  19. package/esm/utoo/index.js +165 -141
  20. package/esm/utoo/index_bg.wasm +0 -0
  21. package/esm/webpackLoaders/{worker/cjs.js → cjs.js} +65 -41
  22. package/esm/webpackLoaders/loaderWorkerPool.d.ts +2 -0
  23. package/esm/webpackLoaders/loaderWorkerPool.js +90 -0
  24. package/esm/webpackLoaders/polyfills/fsPolyfill.d.ts +78 -0
  25. package/esm/webpackLoaders/{worker/polyfills → polyfills}/fsPolyfill.js +154 -191
  26. package/esm/webpackLoaders/polyfills/fsPromisesPolyfill.d.ts +26 -0
  27. package/esm/webpackLoaders/polyfills/fsPromisesPolyfill.js +112 -0
  28. package/esm/webpackLoaders/{worker/polyfills → polyfills}/nodePolyFills.d.ts +0 -2
  29. package/esm/webpackLoaders/{worker/polyfills → polyfills}/nodePolyFills.js +4 -3
  30. package/esm/webpackLoaders/{worker/type.d.ts → types.d.ts} +1 -0
  31. package/esm/webpackLoaders/{worker/index.js → worker.js} +8 -3
  32. package/esm/worker.js +2 -2
  33. package/package.json +14 -13
  34. package/esm/internalProject.js +0 -110
  35. package/esm/loaderWorkerPool.d.ts +0 -3
  36. package/esm/loaderWorkerPool.js +0 -125
  37. package/esm/sabcom.d.ts +0 -31
  38. package/esm/sabcom.js +0 -71
  39. package/esm/type.js +0 -12
  40. package/esm/webpackLoaders/loaders/less-loader/index.d.ts +0 -3
  41. package/esm/webpackLoaders/loaders/less-loader/index.js +0 -103
  42. package/esm/webpackLoaders/loaders/less-loader/options.json +0 -67
  43. package/esm/webpackLoaders/loaders/less-loader/utils.d.ts +0 -14
  44. package/esm/webpackLoaders/loaders/less-loader/utils.js +0 -217
  45. package/esm/webpackLoaders/worker/polyfills/fastGlobPolyfill.d.ts +0 -2
  46. package/esm/webpackLoaders/worker/polyfills/fastGlobPolyfill.js +0 -48
  47. package/esm/webpackLoaders/worker/polyfills/fsPolyfill.d.ts +0 -124
  48. package/esm/webpackLoaders/worker/polyfills/fsPromisesPolyfill.d.ts +0 -9
  49. package/esm/webpackLoaders/worker/polyfills/fsPromisesPolyfill.js +0 -9
  50. /package/esm/{installServiceWorker.d.ts → utils/installServiceWorker.d.ts} +0 -0
  51. /package/esm/{installServiceWorker.js → utils/installServiceWorker.js} +0 -0
  52. /package/esm/{message.d.ts → utils/message.d.ts} +0 -0
  53. /package/esm/{message.js → utils/message.js} +0 -0
  54. /package/esm/webpackLoaders/{worker/cjs.d.ts → cjs.d.ts} +0 -0
  55. /package/esm/webpackLoaders/{worker/polyfills → polyfills}/workerThreadsPolyfill.d.ts +0 -0
  56. /package/esm/webpackLoaders/{worker/polyfills → polyfills}/workerThreadsPolyfill.js +0 -0
  57. /package/esm/webpackLoaders/{worker/type.js → types.js} +0 -0
  58. /package/esm/webpackLoaders/{worker/index.d.ts → worker.d.ts} +0 -0
@@ -10,8 +10,9 @@ const searchPathsCache = {};
10
10
  const statSync = (p) => {
11
11
  if (p.includes("node_modules") &&
12
12
  Object.prototype.hasOwnProperty.call(statCache, p)) {
13
- if (statCache[p] === false)
13
+ if (statCache[p] === false) {
14
14
  throw new Error("ENOENT");
15
+ }
15
16
  return statCache[p];
16
17
  }
17
18
  try {
@@ -31,7 +32,7 @@ const existsSync = (p) => {
31
32
  statSync(p);
32
33
  return true;
33
34
  }
34
- catch (_a) {
35
+ catch (e) {
35
36
  return false;
36
37
  }
37
38
  };
@@ -51,13 +52,16 @@ const executeModule = (moduleCode, moduleId, id, importMaps, entrypoint) => {
51
52
  if (moduleId === entrypoint) {
52
53
  moduleCode = "self.Buffer = require('buffer').Buffer;" + moduleCode;
53
54
  }
55
+ if (!moduleId.endsWith(".json")) {
56
+ moduleCode = `// [Debug] Resolved: ${moduleId}\n` + moduleCode;
57
+ }
54
58
  try {
55
59
  if (moduleId.endsWith(".json")) {
56
60
  finalExports = JSON.parse(moduleCode);
57
61
  module.exports = finalExports;
58
62
  }
59
63
  else {
60
- new Function("require", "exports", "module", "__filename", "__dirname", moduleCode)(moduleRequire, module.exports, module, moduleId, context);
64
+ new Function("require", "exports", "module", "__filename", "__dirname", "process", moduleCode)(moduleRequire, module.exports, module, moduleId, context, nodePolyFills.process);
61
65
  finalExports = module.exports;
62
66
  }
63
67
  }
@@ -86,7 +90,6 @@ const executeModule = (moduleCode, moduleId, id, importMaps, entrypoint) => {
86
90
  return finalExports;
87
91
  };
88
92
  const loadModule = (id, context, importMaps, entrypoint) => {
89
- var _a, _b, _c, _d, _e, _f;
90
93
  const cacheKey = `${context}:${id}`;
91
94
  if (resolutionCache[cacheKey]) {
92
95
  const cachedId = resolutionCache[cacheKey];
@@ -133,13 +136,6 @@ const loadModule = (id, context, importMaps, entrypoint) => {
133
136
  let searchPaths = searchPathsCache[context];
134
137
  if (!searchPaths) {
135
138
  searchPaths = [];
136
- // @ts-ignore
137
- const cwd = ((_b = (_a = self.process) === null || _a === void 0 ? void 0 : _a.cwd) === null || _b === void 0 ? void 0 : _b.call(_a)) || ((_c = self.workerData) === null || _c === void 0 ? void 0 : _c.cwd) || "/";
138
- const isInsideNodeModules = context.includes("/node_modules/") ||
139
- context.includes("\\node_modules\\");
140
- if (isInsideNodeModules) {
141
- searchPaths.push(path.join(cwd, "node_modules"));
142
- }
143
139
  let currentDir = context;
144
140
  while (true) {
145
141
  if (path.basename(currentDir) !== "node_modules") {
@@ -153,22 +149,29 @@ const loadModule = (id, context, importMaps, entrypoint) => {
153
149
  break;
154
150
  currentDir = parent;
155
151
  }
152
+ // Ensure cwd/node_modules is always included
153
+ const cwd = nodePolyFills.process.cwd();
154
+ const cwdNodeModules = path.join(cwd, "node_modules");
155
+ if (!searchPaths.includes(cwdNodeModules)) {
156
+ searchPaths.push(cwdNodeModules);
157
+ }
156
158
  searchPathsCache[context] = searchPaths;
157
159
  }
158
160
  for (const nodeModulesDir of searchPaths) {
159
161
  const nodeModulesPath = path.join(nodeModulesDir, id);
160
162
  // Check package.json first
161
163
  const pkgJsonPath = path.join(nodeModulesPath, "package.json");
162
- if (existsSync(pkgJsonPath)) {
164
+ let pkg;
165
+ try {
166
+ const content = fs.readFileSync(pkgJsonPath, "utf8");
167
+ pkg = JSON.parse(content);
168
+ pkgJsonCache[pkgJsonPath] = pkg;
169
+ }
170
+ catch (e) {
171
+ // ignore
172
+ }
173
+ if (pkg) {
163
174
  try {
164
- let pkg;
165
- if (pkgJsonCache[pkgJsonPath]) {
166
- pkg = pkgJsonCache[pkgJsonPath];
167
- }
168
- else {
169
- pkg = JSON.parse(fs.readFileSync(pkgJsonPath, "utf8"));
170
- pkgJsonCache[pkgJsonPath] = pkg;
171
- }
172
175
  const mainField = pkg.main;
173
176
  if (mainField) {
174
177
  const candidates = [
@@ -178,65 +181,77 @@ const loadModule = (id, context, importMaps, entrypoint) => {
178
181
  path.resolve(nodeModulesPath, mainField, "index.js"),
179
182
  ];
180
183
  for (const candidate of candidates) {
181
- if (existsSync(candidate) && !statSync(candidate).isDirectory()) {
182
- resolvedId = candidate;
184
+ try {
185
+ // Try reading directly to bypass potential statSync issues
183
186
  moduleCode = fs.readFileSync(candidate, "utf8");
187
+ resolvedId = candidate;
184
188
  moduleId = candidate;
185
189
  break;
186
190
  }
191
+ catch (e) {
192
+ // ignore
193
+ }
187
194
  }
188
195
  }
189
196
  }
190
- catch (_g) { }
197
+ catch (e) {
198
+ // ignore
199
+ }
191
200
  }
192
201
  if (!moduleCode) {
193
202
  const extensions = ["", ".js", ".json", "/index.js"];
194
203
  for (const ext of extensions) {
195
204
  const p = nodeModulesPath + ext;
196
- if (existsSync(p) && !statSync(p).isDirectory()) {
197
- resolvedId = p;
205
+ try {
206
+ // Try reading directly
198
207
  moduleCode = fs.readFileSync(p, "utf8");
208
+ resolvedId = p;
199
209
  moduleId = p;
200
210
  break;
201
211
  }
212
+ catch (e) {
213
+ // ignore
214
+ }
202
215
  }
203
216
  }
204
217
  if (moduleCode)
205
218
  break;
206
219
  }
207
220
  }
208
- // Fallback: Try resolving absolute path (handling CWD stripping)
221
+ // Fallback: Try resolving absolute path
209
222
  if (!moduleCode && id.startsWith("/")) {
210
- // @ts-ignore
211
- const cwd = ((_e = (_d = self.process) === null || _d === void 0 ? void 0 : _d.cwd) === null || _e === void 0 ? void 0 : _e.call(_d)) || ((_f = self.workerData) === null || _f === void 0 ? void 0 : _f.cwd) || "/";
212
- let relativeId = id;
213
- if (id.startsWith(cwd)) {
214
- relativeId = id.slice(cwd.length);
215
- if (relativeId.startsWith("/"))
216
- relativeId = relativeId.slice(1);
217
- }
218
223
  const extensions = ["", ".js", ".json", "/index.js"];
219
224
  for (const ext of extensions) {
220
- const p = relativeId + ext;
221
- if (existsSync(p) && !statSync(p).isDirectory()) {
222
- resolvedId = p; // Use relative path for FS ops
225
+ if (ext === "/index.js" && id.endsWith(".js"))
226
+ continue;
227
+ const p = id + ext;
228
+ try {
223
229
  moduleCode = fs.readFileSync(p, "utf8");
224
- moduleId = id; // Keep original absolute path as module ID
230
+ resolvedId = p;
231
+ moduleId = id;
225
232
  break;
226
233
  }
234
+ catch (e) {
235
+ // ignore
236
+ }
227
237
  }
228
238
  }
229
239
  if (!moduleCode) {
230
240
  // Try extensions
231
241
  const extensions = ["", ".js", ".json", "/index.js"];
232
242
  for (const ext of extensions) {
243
+ if (ext === "/index.js" && id.endsWith(".js"))
244
+ continue;
233
245
  const p = resolvedId + ext;
234
- if (existsSync(p) && !statSync(p).isDirectory()) {
235
- resolvedId = p;
246
+ try {
236
247
  moduleCode = fs.readFileSync(p, "utf8");
248
+ resolvedId = p;
237
249
  moduleId = p;
238
250
  break;
239
251
  }
252
+ catch (e) {
253
+ // ignore
254
+ }
240
255
  }
241
256
  }
242
257
  if (moduleCode) {
@@ -244,10 +259,19 @@ const loadModule = (id, context, importMaps, entrypoint) => {
244
259
  resolutionCache[cacheKey] = moduleId;
245
260
  return executeModule(moduleCode, moduleId, id, importMaps, entrypoint);
246
261
  }
247
- console.error(`Worker: Dependency ${id} (resolved: ${resolvedId}) not found.`);
262
+ console.error(`Worker: Dependency ${id} (resolved: ${resolvedId}) not found. Context: ${context}`);
248
263
  return {};
249
264
  };
250
265
  export async function cjs(entrypoint, importMaps) {
266
+ // Clear caches to avoid stale data across runs
267
+ for (const key in statCache)
268
+ delete statCache[key];
269
+ for (const key in pkgJsonCache)
270
+ delete pkgJsonCache[key];
271
+ for (const key in resolutionCache)
272
+ delete resolutionCache[key];
273
+ for (const key in searchPathsCache)
274
+ delete searchPathsCache[key];
251
275
  await Promise.all(Object.entries(importMaps).map(async ([k, v]) => {
252
276
  if (v.startsWith("https://")) {
253
277
  try {
@@ -0,0 +1,2 @@
1
+ import { Binding } from "../types";
2
+ export declare const runLoaderWorkerPool: (binding: Binding, projectCwd: string, loaderWorkerUrl: string, loadersImportMap?: Record<string, string>) => Promise<void>;
@@ -0,0 +1,90 @@
1
+ import * as sabcom from "../utils/sabcom";
2
+ import initWasm, { Project as ProjectInternal, registerWorkerScheduler, workerCreated, } from "../utoo";
3
+ let nextWorkerId = 0;
4
+ const loaderWorkers = {};
5
+ export const runLoaderWorkerPool = async (binding, projectCwd, loaderWorkerUrl, loadersImportMap) => {
6
+ registerWorkerScheduler(async (creation) => {
7
+ const { options: { filename, cwd }, } = creation;
8
+ nextWorkerId += 1;
9
+ const workerId = nextWorkerId;
10
+ const sab = new SharedArrayBuffer(1024 * 1024 * 10); // 10MB
11
+ const sabHost = new sabcom.SabComHost(sab);
12
+ const worker = new Worker(loaderWorkerUrl, { name: filename });
13
+ worker.onmessage = async (event) => {
14
+ if (event.data === "sab_request") {
15
+ await sabcom.handleSabRequest(sabHost, {
16
+ read: (path) => ProjectInternal.read(path),
17
+ readDir: (path) => ProjectInternal.readDir(path),
18
+ writeString: (path, content) => ProjectInternal.writeString(path, content),
19
+ createDirAll: (path) => ProjectInternal.createDirAll(path),
20
+ createDir: (path) => ProjectInternal.createDir(path),
21
+ metadata: (path) => ProjectInternal.metadata(path),
22
+ removeFile: (path) => ProjectInternal.removeFile(path),
23
+ removeDir: (path, recursive) => ProjectInternal.removeDir(path, recursive),
24
+ copyFile: (src, dst) => ProjectInternal.copyFile(src, dst),
25
+ });
26
+ }
27
+ };
28
+ let finalCwd = cwd;
29
+ let finalFilename = filename;
30
+ if (projectCwd) {
31
+ const sep = "/";
32
+ let pCwd = projectCwd.endsWith(sep)
33
+ ? projectCwd.slice(0, -1)
34
+ : projectCwd;
35
+ if (!pCwd.startsWith(sep)) {
36
+ pCwd = sep + pCwd;
37
+ }
38
+ if (cwd.startsWith(sep)) {
39
+ finalCwd = cwd;
40
+ }
41
+ else {
42
+ let cCwd = cwd;
43
+ if (cCwd === "." || cCwd === "./") {
44
+ cCwd = "";
45
+ }
46
+ finalCwd = cCwd ? `${pCwd}${sep}${cCwd}` : pCwd;
47
+ }
48
+ if (filename.startsWith(sep)) {
49
+ finalFilename = filename;
50
+ }
51
+ else {
52
+ let fName = filename;
53
+ if (fName.startsWith("./"))
54
+ fName = fName.slice(2);
55
+ finalFilename = `${pCwd}${sep}${fName}`;
56
+ }
57
+ }
58
+ worker.postMessage([
59
+ // @ts-ignore
60
+ initWasm.__wbindgen_wasm_module,
61
+ binding.memory,
62
+ {
63
+ workerData: {
64
+ cwd: finalCwd,
65
+ projectRoot: projectCwd,
66
+ workerId: workerId,
67
+ },
68
+ loaderAssets: {
69
+ importMaps: { ...loadersImportMap },
70
+ entrypoint: finalFilename,
71
+ },
72
+ sab,
73
+ },
74
+ ]);
75
+ const workers = loaderWorkers[filename] || (loaderWorkers[filename] = new Map());
76
+ workers.set(workerId, worker);
77
+ workerCreated(workerId);
78
+ }, (termination) => {
79
+ const { workerId, options } = termination;
80
+ const entrypoint = options.filename;
81
+ const workers = loaderWorkers[entrypoint];
82
+ if (workers) {
83
+ const worker = workers.get(workerId);
84
+ if (worker) {
85
+ worker.terminate();
86
+ workers.delete(workerId);
87
+ }
88
+ }
89
+ });
90
+ };
@@ -0,0 +1,78 @@
1
+ import { Buffer } from "buffer";
2
+ import { Stats } from "../../types";
3
+ import { promises } from "./fsPromisesPolyfill";
4
+ export declare function readFileSync(path: string, options: any): string | Buffer<any>;
5
+ export declare function readdirSync(path: string, options?: any): any;
6
+ export declare function writeFileSync(path: string, data: string | Uint8Array, options?: any): void;
7
+ export declare function mkdirSync(path: string, options?: any): void;
8
+ export declare function rmSync(path: string, options?: any): void;
9
+ export declare function rmdirSync(path: string, options?: any): void;
10
+ export declare function copyFileSync(src: string, dst: string): void;
11
+ export declare function statSync(p: string): Stats;
12
+ export declare function lstatSync(p: string): Stats;
13
+ export declare function realpathSync(p: string): string;
14
+ export declare function accessSync(path: string, mode?: number): void;
15
+ export declare function existsSync(path: string): boolean;
16
+ export declare function readFile(path: string, options: any, cb: Function): void;
17
+ export declare function readdir(path: string, options: any, cb: Function): void;
18
+ export declare function writeFile(path: string, data: string | Uint8Array, options: any, cb: Function): void;
19
+ export declare function mkdir(path: string, options: any, cb: Function): void;
20
+ export declare function rm(path: string, options: any, cb: Function): void;
21
+ export declare function rmdir(path: string, options: any, cb: Function): void;
22
+ export declare function copyFile(src: string, dst: string, cb: Function): void;
23
+ export declare function stat(p: string, cb: Function): void;
24
+ export declare function lstat(p: string, cb: Function): void;
25
+ export declare function realpath(p: string, cb: Function): void;
26
+ export declare function access(p: string, mode: number | Function, cb?: Function): void;
27
+ export declare const constants: {
28
+ F_OK: number;
29
+ R_OK: number;
30
+ W_OK: number;
31
+ X_OK: number;
32
+ };
33
+ declare const _default: {
34
+ readFile: typeof readFile;
35
+ readFileSync: typeof readFileSync;
36
+ readdir: typeof readdir;
37
+ readdirSync: typeof readdirSync;
38
+ writeFile: typeof writeFile;
39
+ writeFileSync: typeof writeFileSync;
40
+ mkdir: typeof mkdir;
41
+ mkdirSync: typeof mkdirSync;
42
+ rm: typeof rm;
43
+ rmSync: typeof rmSync;
44
+ rmdir: typeof rmdir;
45
+ rmdirSync: typeof rmdirSync;
46
+ copyFile: typeof copyFile;
47
+ copyFileSync: typeof copyFileSync;
48
+ stat: typeof stat;
49
+ statSync: typeof statSync;
50
+ lstat: typeof lstat;
51
+ lstatSync: typeof lstatSync;
52
+ realpath: typeof realpath;
53
+ realpathSync: typeof realpathSync;
54
+ access: typeof access;
55
+ accessSync: typeof accessSync;
56
+ existsSync: typeof existsSync;
57
+ promises: {
58
+ readFile: (p: string, options?: any) => Promise<any>;
59
+ writeFile: (p: string, data: string | Uint8Array, options?: any) => Promise<void>;
60
+ readdir: (p: string, options?: any) => Promise<any>;
61
+ mkdir: (p: string, options?: any) => Promise<void>;
62
+ rm: (p: string, options?: any) => Promise<void>;
63
+ rmdir: (p: string, options?: any) => Promise<any>;
64
+ copyFile: (src: string, dst: string) => Promise<any>;
65
+ stat: (p: string) => Promise<Stats>;
66
+ lstat: (p: string) => Promise<Stats>;
67
+ realpath: (p: string) => Promise<string>;
68
+ access: (p: string, mode?: number) => Promise<void>;
69
+ };
70
+ constants: {
71
+ F_OK: number;
72
+ R_OK: number;
73
+ W_OK: number;
74
+ X_OK: number;
75
+ };
76
+ };
77
+ export default _default;
78
+ export { promises };