@utoo/web 1.1.0 → 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 (56) hide show
  1. package/README.md +55 -0
  2. package/esm/a0a815481b4ff49f8961.wasm +0 -0
  3. package/esm/index.d.ts +3 -2
  4. package/esm/index.js +3 -2
  5. package/esm/loaderWorker.js +2 -0
  6. package/esm/loaderWorker.js.LICENSE.txt +23 -0
  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} +5 -3
  10. package/esm/project/InternalProject.js +132 -0
  11. package/esm/{project.d.ts → project/Project.d.ts} +4 -1
  12. package/esm/{project.js → project/Project.js} +21 -6
  13. package/esm/serviceWorker.js +4 -3
  14. package/esm/{type.d.ts → types.d.ts} +44 -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 +126 -66
  19. package/esm/utoo/index.js +351 -235
  20. package/esm/utoo/index_bg.wasm +0 -0
  21. package/esm/webpackLoaders/cjs.js +299 -0
  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/polyfills/fsPolyfill.js +279 -0
  26. package/esm/webpackLoaders/polyfills/fsPromisesPolyfill.d.ts +26 -0
  27. package/esm/webpackLoaders/polyfills/fsPromisesPolyfill.js +112 -0
  28. package/esm/webpackLoaders/polyfills/nodePolyFills.d.ts +92 -0
  29. package/esm/webpackLoaders/polyfills/nodePolyFills.js +230 -0
  30. package/esm/webpackLoaders/polyfills/workerThreadsPolyfill.d.ts +7 -0
  31. package/esm/webpackLoaders/polyfills/workerThreadsPolyfill.js +16 -0
  32. package/esm/webpackLoaders/{worker/type.d.ts → types.d.ts} +2 -1
  33. package/esm/webpackLoaders/worker.d.ts +2 -0
  34. package/esm/webpackLoaders/worker.js +46 -0
  35. package/esm/worker.js +2 -2
  36. package/package.json +19 -18
  37. package/esm/internalProject.js +0 -185
  38. package/esm/type.js +0 -12
  39. package/esm/webpackLoaders/loaders/less-loader/index.d.ts +0 -3
  40. package/esm/webpackLoaders/loaders/less-loader/index.js +0 -103
  41. package/esm/webpackLoaders/loaders/less-loader/options.json +0 -67
  42. package/esm/webpackLoaders/loaders/less-loader/utils.d.ts +0 -14
  43. package/esm/webpackLoaders/loaders/less-loader/utils.js +0 -217
  44. package/esm/webpackLoaders/worker/cjs.js +0 -80
  45. package/esm/webpackLoaders/worker/index.d.ts +0 -2
  46. package/esm/webpackLoaders/worker/index.js +0 -31
  47. package/esm/webpackLoaders/worker/nodePolyFills.d.ts +0 -14
  48. package/esm/webpackLoaders/worker/nodePolyFills.js +0 -24
  49. package/esm/webpackLoaders/workerContent.d.ts +0 -2
  50. package/esm/webpackLoaders/workerContent.js +0 -1
  51. /package/esm/{installServiceWorker.d.ts → utils/installServiceWorker.d.ts} +0 -0
  52. /package/esm/{installServiceWorker.js → utils/installServiceWorker.js} +0 -0
  53. /package/esm/{message.d.ts → utils/message.d.ts} +0 -0
  54. /package/esm/{message.js → utils/message.js} +0 -0
  55. /package/esm/webpackLoaders/{worker/cjs.d.ts → cjs.d.ts} +0 -0
  56. /package/esm/webpackLoaders/{worker/type.js → types.js} +0 -0
Binary file
@@ -0,0 +1,299 @@
1
+ import "systemjs/dist/system.js";
2
+ import nodePolyFills from "./polyfills/nodePolyFills";
3
+ const fs = nodePolyFills.fs;
4
+ const path = nodePolyFills.path;
5
+ const installedModules = {};
6
+ const statCache = {};
7
+ const pkgJsonCache = {};
8
+ const resolutionCache = {};
9
+ const searchPathsCache = {};
10
+ const statSync = (p) => {
11
+ if (p.includes("node_modules") &&
12
+ Object.prototype.hasOwnProperty.call(statCache, p)) {
13
+ if (statCache[p] === false) {
14
+ throw new Error("ENOENT");
15
+ }
16
+ return statCache[p];
17
+ }
18
+ try {
19
+ const res = fs.statSync(p);
20
+ if (p.includes("node_modules"))
21
+ statCache[p] = res;
22
+ return res;
23
+ }
24
+ catch (e) {
25
+ if (p.includes("node_modules"))
26
+ statCache[p] = false;
27
+ throw e;
28
+ }
29
+ };
30
+ const existsSync = (p) => {
31
+ try {
32
+ statSync(p);
33
+ return true;
34
+ }
35
+ catch (e) {
36
+ return false;
37
+ }
38
+ };
39
+ const executeModule = (moduleCode, moduleId, id, importMaps, entrypoint) => {
40
+ if (installedModules[moduleId]) {
41
+ return installedModules[moduleId].exports;
42
+ }
43
+ const context = path.dirname(moduleId);
44
+ let finalExports = {};
45
+ const moduleRequire = (childId) => loadModule(childId, context, importMaps, entrypoint);
46
+ moduleRequire.resolve = (request) => request;
47
+ const module = { exports: finalExports, require: moduleRequire };
48
+ if (moduleId.includes("node_modules")) {
49
+ installedModules[moduleId] = module;
50
+ }
51
+ // Hack for entrypoint
52
+ if (moduleId === entrypoint) {
53
+ moduleCode = "self.Buffer = require('buffer').Buffer;" + moduleCode;
54
+ }
55
+ if (!moduleId.endsWith(".json")) {
56
+ moduleCode = `// [Debug] Resolved: ${moduleId}\n` + moduleCode;
57
+ }
58
+ try {
59
+ if (moduleId.endsWith(".json")) {
60
+ finalExports = JSON.parse(moduleCode);
61
+ module.exports = finalExports;
62
+ }
63
+ else {
64
+ new Function("require", "exports", "module", "__filename", "__dirname", "process", moduleCode)(moduleRequire, module.exports, module, moduleId, context, nodePolyFills.process);
65
+ finalExports = module.exports;
66
+ }
67
+ }
68
+ catch (e) {
69
+ console.error(`Worker: Error executing module ${moduleId}:`, e);
70
+ throw new Error(`Failed to load dependency ${moduleId}: ${e.message}`);
71
+ }
72
+ const originalWarn = console.warn;
73
+ console.warn = (...args) => {
74
+ var _a;
75
+ const msg = ((_a = args[0]) === null || _a === void 0 ? void 0 : _a.toString()) || "";
76
+ if (msg.includes("(SystemJS Error#W3")) {
77
+ return;
78
+ }
79
+ originalWarn.apply(console, args);
80
+ };
81
+ try {
82
+ System.set(moduleId, { default: finalExports });
83
+ }
84
+ catch (e) {
85
+ // ignore
86
+ }
87
+ finally {
88
+ console.warn = originalWarn;
89
+ }
90
+ return finalExports;
91
+ };
92
+ const loadModule = (id, context, importMaps, entrypoint) => {
93
+ const cacheKey = `${context}:${id}`;
94
+ if (resolutionCache[cacheKey]) {
95
+ const cachedId = resolutionCache[cacheKey];
96
+ if (installedModules[cachedId]) {
97
+ return installedModules[cachedId].exports;
98
+ }
99
+ // Fast path: if we know the resolved path, try to load it directly
100
+ try {
101
+ const moduleCode = fs.readFileSync(cachedId, "utf8");
102
+ return executeModule(moduleCode, cachedId, id, importMaps, entrypoint);
103
+ }
104
+ catch (e) {
105
+ // If read fails, fall back to full resolution
106
+ }
107
+ }
108
+ // 1. Resolve
109
+ let resolvedId = id;
110
+ if (id.startsWith(".")) {
111
+ resolvedId = path.resolve(context, id);
112
+ }
113
+ // 2. Check Cache (SystemJS)
114
+ let dependency = System.get(resolvedId);
115
+ if (dependency)
116
+ return dependency.default;
117
+ if (id !== resolvedId) {
118
+ dependency = System.get(id);
119
+ if (dependency)
120
+ return dependency.default;
121
+ }
122
+ // 3. Check Node Polyfills
123
+ if (id in nodePolyFills) {
124
+ // @ts-ignore
125
+ return nodePolyFills[id];
126
+ }
127
+ if (resolvedId in nodePolyFills) {
128
+ // @ts-ignore
129
+ return nodePolyFills[resolvedId];
130
+ }
131
+ // 4. Check importMaps & FS
132
+ let moduleCode = importMaps[resolvedId] || importMaps[id];
133
+ let moduleId = importMaps[resolvedId] ? resolvedId : id;
134
+ // Fallback: Try resolving from node_modules
135
+ if (!moduleCode && !id.startsWith(".") && !id.startsWith("/")) {
136
+ let searchPaths = searchPathsCache[context];
137
+ if (!searchPaths) {
138
+ searchPaths = [];
139
+ let currentDir = context;
140
+ while (true) {
141
+ if (path.basename(currentDir) !== "node_modules") {
142
+ const nodeModulesPath = path.join(currentDir, "node_modules");
143
+ if (!searchPaths.includes(nodeModulesPath)) {
144
+ searchPaths.push(nodeModulesPath);
145
+ }
146
+ }
147
+ const parent = path.dirname(currentDir);
148
+ if (parent === currentDir)
149
+ break;
150
+ currentDir = parent;
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
+ }
158
+ searchPathsCache[context] = searchPaths;
159
+ }
160
+ for (const nodeModulesDir of searchPaths) {
161
+ const nodeModulesPath = path.join(nodeModulesDir, id);
162
+ // Check package.json first
163
+ const pkgJsonPath = path.join(nodeModulesPath, "package.json");
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) {
174
+ try {
175
+ const mainField = pkg.main;
176
+ if (mainField) {
177
+ const candidates = [
178
+ path.resolve(nodeModulesPath, mainField),
179
+ path.resolve(nodeModulesPath, mainField) + ".js",
180
+ path.resolve(nodeModulesPath, mainField) + ".json",
181
+ path.resolve(nodeModulesPath, mainField, "index.js"),
182
+ ];
183
+ for (const candidate of candidates) {
184
+ try {
185
+ // Try reading directly to bypass potential statSync issues
186
+ moduleCode = fs.readFileSync(candidate, "utf8");
187
+ resolvedId = candidate;
188
+ moduleId = candidate;
189
+ break;
190
+ }
191
+ catch (e) {
192
+ // ignore
193
+ }
194
+ }
195
+ }
196
+ }
197
+ catch (e) {
198
+ // ignore
199
+ }
200
+ }
201
+ if (!moduleCode) {
202
+ const extensions = ["", ".js", ".json", "/index.js"];
203
+ for (const ext of extensions) {
204
+ const p = nodeModulesPath + ext;
205
+ try {
206
+ // Try reading directly
207
+ moduleCode = fs.readFileSync(p, "utf8");
208
+ resolvedId = p;
209
+ moduleId = p;
210
+ break;
211
+ }
212
+ catch (e) {
213
+ // ignore
214
+ }
215
+ }
216
+ }
217
+ if (moduleCode)
218
+ break;
219
+ }
220
+ }
221
+ // Fallback: Try resolving absolute path
222
+ if (!moduleCode && id.startsWith("/")) {
223
+ const extensions = ["", ".js", ".json", "/index.js"];
224
+ for (const ext of extensions) {
225
+ if (ext === "/index.js" && id.endsWith(".js"))
226
+ continue;
227
+ const p = id + ext;
228
+ try {
229
+ moduleCode = fs.readFileSync(p, "utf8");
230
+ resolvedId = p;
231
+ moduleId = id;
232
+ break;
233
+ }
234
+ catch (e) {
235
+ // ignore
236
+ }
237
+ }
238
+ }
239
+ if (!moduleCode) {
240
+ // Try extensions
241
+ const extensions = ["", ".js", ".json", "/index.js"];
242
+ for (const ext of extensions) {
243
+ if (ext === "/index.js" && id.endsWith(".js"))
244
+ continue;
245
+ const p = resolvedId + ext;
246
+ try {
247
+ moduleCode = fs.readFileSync(p, "utf8");
248
+ resolvedId = p;
249
+ moduleId = p;
250
+ break;
251
+ }
252
+ catch (e) {
253
+ // ignore
254
+ }
255
+ }
256
+ }
257
+ if (moduleCode) {
258
+ const cacheKey = `${context}:${id}`;
259
+ resolutionCache[cacheKey] = moduleId;
260
+ return executeModule(moduleCode, moduleId, id, importMaps, entrypoint);
261
+ }
262
+ console.error(`Worker: Dependency ${id} (resolved: ${resolvedId}) not found. Context: ${context}`);
263
+ return {};
264
+ };
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];
275
+ await Promise.all(Object.entries(importMaps).map(async ([k, v]) => {
276
+ if (v.startsWith("https://")) {
277
+ try {
278
+ const response = await fetch(v);
279
+ if (response.ok) {
280
+ importMaps[k] = await response.text();
281
+ }
282
+ else {
283
+ console.error(`Failed to fetch loader '${k}' from ${v}: ${response.status} ${response.statusText}`);
284
+ delete importMaps[k];
285
+ }
286
+ }
287
+ catch (error) {
288
+ console.error(`Error fetching loader '${k}' from ${v}:`, error);
289
+ delete importMaps[k];
290
+ }
291
+ }
292
+ }));
293
+ // @ts-ignore
294
+ // a hack for loader-runner resolving
295
+ self.__systemjs_require__ = (id) => loadModule(id, path.dirname(entrypoint), importMaps, entrypoint);
296
+ // @ts-ignore
297
+ self.__systemjs_require__.resolve = (request) => request;
298
+ loadModule(entrypoint, path.dirname(entrypoint), importMaps, entrypoint);
299
+ }
@@ -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 };