@utoo/web 0.0.1-alpha.13 → 0.0.1-alpha.15

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.
package/esm/index.d.ts CHANGED
@@ -1,10 +1,11 @@
1
- import { Dirent, MountOpt, ProjectEndpoint } from "./type";
1
+ import { Dirent, ProjectEndpoint, ProjectOptions } from "./type";
2
2
  export declare class Project implements ProjectEndpoint {
3
3
  #private;
4
+ private serviceWorkerOptions?;
4
5
  private remote;
5
- constructor(opt: MountOpt & {
6
- entryUrl?: string;
7
- });
6
+ constructor(options: ProjectOptions);
7
+ private connectWorker;
8
+ installServiceWorker(): Promise<void>;
8
9
  install(packageLock: string): Promise<void>;
9
10
  build(): Promise<void>;
10
11
  readFile(path: string, encoding?: "utf8"): Promise<any>;
@@ -16,6 +17,6 @@ export declare class Project implements ProjectEndpoint {
16
17
  mkdir(path: string, options?: {
17
18
  recursive?: boolean;
18
19
  }): Promise<void>;
19
- static fork(channel: MessageChannel): ProjectEndpoint;
20
+ static fork(channel: MessageChannel, eventSource: Client | DedicatedWorkerGlobalScope): ProjectEndpoint;
20
21
  }
21
22
  export * from "./type";
package/esm/index.js CHANGED
@@ -11,30 +11,55 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
11
11
  };
12
12
  var _Project_tunnel;
13
13
  import * as comlink from "comlink";
14
- import { Fork, HandShake } from "./message";
15
- import { Dirent } from "./type";
14
+ import { Fork, HandShake, ServiceWorkerHandShake } from "./message";
15
+ import { Dirent, } from "./type";
16
16
  let ProjectWorker;
17
17
  const ConnectedPorts = new Set();
18
18
  export class Project {
19
- constructor(opt) {
19
+ constructor(options) {
20
20
  var _a, _b;
21
21
  _Project_tunnel.set(this, void 0);
22
- const { cwd, entryUrl, wasmUrl, threadUrl } = opt;
22
+ const { cwd, workerUrl, wasmUrl, threadWorkerUrl, serviceWorker } = options;
23
+ this.serviceWorkerOptions = serviceWorker;
23
24
  const { port1, port2 } = new MessageChannel();
24
25
  (_a = this.remote) !== null && _a !== void 0 ? _a : (this.remote = comlink.wrap(port1));
25
26
  if (!ProjectWorker) {
26
- ProjectWorker = entryUrl
27
- ? new Worker(entryUrl)
27
+ ProjectWorker = workerUrl
28
+ ? new Worker(workerUrl)
28
29
  : new Worker(new URL("./worker", import.meta.url));
29
- self.addEventListener("message", (e) => {
30
- const port = e.ports[0];
31
- if (e.data === Fork && !ConnectedPorts.has(port)) {
32
- ProjectWorker.postMessage(HandShake, [port]);
33
- }
30
+ window.addEventListener("message", (e) => {
31
+ this.connectWorker(e);
34
32
  });
33
+ if (this.serviceWorkerOptions) {
34
+ navigator.serviceWorker.addEventListener("message", (e) => {
35
+ this.connectWorker(e);
36
+ });
37
+ }
35
38
  }
36
39
  ProjectWorker.postMessage(HandShake, [port2]);
37
- __classPrivateFieldSet(this, _Project_tunnel, (_b = __classPrivateFieldGet(this, _Project_tunnel, "f")) !== null && _b !== void 0 ? _b : this.remote.mount({ cwd, wasmUrl, threadUrl }), "f");
40
+ __classPrivateFieldSet(this, _Project_tunnel, (_b = __classPrivateFieldGet(this, _Project_tunnel, "f")) !== null && _b !== void 0 ? _b : this.remote.mount({
41
+ cwd,
42
+ wasmUrl,
43
+ threadWorkerUrl,
44
+ }), "f");
45
+ }
46
+ connectWorker(e) {
47
+ const port = e.ports[0];
48
+ if (e.data === Fork && !ConnectedPorts.has(port)) {
49
+ ProjectWorker.postMessage(HandShake, [port]);
50
+ }
51
+ }
52
+ async installServiceWorker() {
53
+ var _a;
54
+ if (this.serviceWorkerOptions) {
55
+ const { url, scope } = this.serviceWorkerOptions;
56
+ // Should add "Service-Worker-Allowed": "/" in page root response,
57
+ await navigator.serviceWorker.register(url, { scope: "/" });
58
+ (_a = navigator.serviceWorker.controller) === null || _a === void 0 ? void 0 : _a.postMessage({
59
+ [ServiceWorkerHandShake]: true,
60
+ scope,
61
+ });
62
+ }
38
63
  }
39
64
  async install(packageLock) {
40
65
  await __classPrivateFieldGet(this, _Project_tunnel, "f");
@@ -65,9 +90,8 @@ export class Project {
65
90
  await __classPrivateFieldGet(this, _Project_tunnel, "f");
66
91
  return await this.remote.mkdir(path, options);
67
92
  }
68
- static fork(channel) {
69
- self.postMessage(Fork, {
70
- targetOrigin: "*",
93
+ static fork(channel, eventSource) {
94
+ eventSource.postMessage(Fork, {
71
95
  transfer: [channel.port2],
72
96
  });
73
97
  return new ForkedProject(channel.port1);
package/esm/message.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export declare const HandShake = "__handshake__";
2
+ export declare const ServiceWorkerHandShake = "__service_worker_handshake__";
2
3
  export declare const Fork = "__fork__";
package/esm/message.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export const HandShake = "__handshake__";
2
+ export const ServiceWorkerHandShake = "__service_worker_handshake__";
2
3
  export const Fork = "__fork__";
@@ -0,0 +1,56 @@
1
+ import { Project } from ".";
2
+ import { ServiceWorkerHandShake } from "./message";
3
+ let _resolve;
4
+ let _promise = new Promise((resolve) => {
5
+ _resolve = resolve;
6
+ });
7
+ let _projectEndpoint;
8
+ let _serviceWorkerScope;
9
+ self.addEventListener("message", (event) => {
10
+ if (event.data && event.data[ServiceWorkerHandShake] === true) {
11
+ _serviceWorkerScope = event.data.scope;
12
+ _projectEndpoint = Project.fork(new MessageChannel(), event.source);
13
+ _resolve();
14
+ }
15
+ });
16
+ self.addEventListener("fetch", async (event) => {
17
+ const { url, referrer } = event.request;
18
+ if (new URL(url).pathname.startsWith(_serviceWorkerScope) ||
19
+ (referrer && new URL(referrer).pathname.startsWith(_serviceWorkerScope))) {
20
+ await _promise;
21
+ const projectPath = "." + new URL(url).pathname.replace(_serviceWorkerScope, "");
22
+ event.respondWith(readFileFromProject(projectPath));
23
+ }
24
+ else {
25
+ event.respondWith(fetch(event.request));
26
+ }
27
+ });
28
+ async function readFileFromProject(projectPath) {
29
+ try {
30
+ const content = await _projectEndpoint.readFile(projectPath);
31
+ let mimeType = "application/octet-stream";
32
+ if (projectPath.endsWith(".js")) {
33
+ mimeType = "application/javascript";
34
+ }
35
+ else if (projectPath.endsWith(".css")) {
36
+ mimeType = "text/css";
37
+ }
38
+ else if (projectPath.endsWith(".html")) {
39
+ mimeType = "text/html";
40
+ }
41
+ else if (projectPath.endsWith(".json")) {
42
+ mimeType = "application/json";
43
+ }
44
+ return new Response(content, {
45
+ headers: {
46
+ "Content-Type": mimeType,
47
+ ...(mimeType === "text/html"
48
+ ? { "Cross-Origin-Embedder-Policy": "require-corp" }
49
+ : {}),
50
+ },
51
+ });
52
+ }
53
+ catch (e) {
54
+ return new Response("Not Found", { status: 404 });
55
+ }
56
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ import initWasm from "./utoo";
2
+ // this is for wasm_thread to spawn new worker thread.
3
+ // see: https://github.com/utooland/wasm_thread/blob/94438ff771ee0a6a55d79e49a655707970acb615/src/wasm32/js/web_worker.js#L10
4
+ self.wasm_bindgen = initWasm;
package/esm/type.d.ts CHANGED
@@ -24,8 +24,14 @@ export interface ProjectEndpoint {
24
24
  }): Promise<void>;
25
25
  copyFile(src: string, dst: string): Promise<void>;
26
26
  }
27
- export interface MountOpt {
27
+ export interface ProjectOptions {
28
28
  cwd: string;
29
- threadUrl: string;
29
+ workerUrl?: string;
30
+ threadWorkerUrl: string;
30
31
  wasmUrl?: string;
32
+ serviceWorker?: ServiceWorkerOptions;
33
+ }
34
+ export interface ServiceWorkerOptions {
35
+ url: string;
36
+ scope: string;
31
37
  }
package/esm/utoo/index.js CHANGED
@@ -1150,27 +1150,27 @@ function __wbg_get_imports() {
1150
1150
  const ret = false;
1151
1151
  return ret;
1152
1152
  };
1153
- imports.wbg.__wbindgen_closure_wrapper163125 = function (arg0, arg1, arg2) {
1153
+ imports.wbg.__wbindgen_closure_wrapper163109 = function (arg0, arg1, arg2) {
1154
1154
  const ret = makeMutClosure(arg0, arg1, 9521, __wbg_adapter_49);
1155
1155
  return addHeapObject(ret);
1156
1156
  };
1157
- imports.wbg.__wbindgen_closure_wrapper163127 = function (arg0, arg1, arg2) {
1157
+ imports.wbg.__wbindgen_closure_wrapper163111 = function (arg0, arg1, arg2) {
1158
1158
  const ret = makeMutClosure(arg0, arg1, 1976, __wbg_adapter_49);
1159
1159
  return addHeapObject(ret);
1160
1160
  };
1161
- imports.wbg.__wbindgen_closure_wrapper168916 = function (arg0, arg1, arg2) {
1161
+ imports.wbg.__wbindgen_closure_wrapper168917 = function (arg0, arg1, arg2) {
1162
1162
  const ret = makeMutClosure(arg0, arg1, 9521, __wbg_adapter_49);
1163
1163
  return addHeapObject(ret);
1164
1164
  };
1165
- imports.wbg.__wbindgen_closure_wrapper168970 = function (arg0, arg1, arg2) {
1165
+ imports.wbg.__wbindgen_closure_wrapper168971 = function (arg0, arg1, arg2) {
1166
1166
  const ret = makeMutClosure(arg0, arg1, 9524, __wbg_adapter_56);
1167
1167
  return addHeapObject(ret);
1168
1168
  };
1169
- imports.wbg.__wbindgen_closure_wrapper168993 = function (arg0, arg1, arg2) {
1169
+ imports.wbg.__wbindgen_closure_wrapper168994 = function (arg0, arg1, arg2) {
1170
1170
  const ret = makeMutClosure(arg0, arg1, 9528, __wbg_adapter_46);
1171
1171
  return addHeapObject(ret);
1172
1172
  };
1173
- imports.wbg.__wbindgen_closure_wrapper30076 = function (arg0, arg1, arg2) {
1173
+ imports.wbg.__wbindgen_closure_wrapper30059 = function (arg0, arg1, arg2) {
1174
1174
  const ret = makeMutClosure(arg0, arg1, 1976, __wbg_adapter_46);
1175
1175
  return addHeapObject(ret);
1176
1176
  };
Binary file
package/esm/worker.js CHANGED
@@ -7,10 +7,10 @@ const projectEndpoint = {
7
7
  // This should be called only once
8
8
  async mount(opt) {
9
9
  var _a;
10
- const { cwd, wasmUrl, threadUrl } = opt;
10
+ const { cwd, wasmUrl, threadWorkerUrl } = opt;
11
11
  (_a = this.wasmInit) !== null && _a !== void 0 ? _a : (this.wasmInit = initWasm(wasmUrl));
12
12
  await this.wasmInit;
13
- this.projectInternal = new ProjectInternal(cwd, threadUrl);
13
+ this.projectInternal = new ProjectInternal(cwd, threadWorkerUrl);
14
14
  return;
15
15
  },
16
16
  async install(packageLock) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@utoo/web",
3
- "version": "0.0.1-alpha.13",
3
+ "version": "0.0.1-alpha.15",
4
4
  "module": "esm/index.js",
5
5
  "types": "esm/index.d.ts",
6
6
  "files": [
@@ -12,11 +12,11 @@
12
12
  "build-wasm:pm": "cargo build -p utoo-wasm --target wasm32-unknown-unknown --no-default-features -Z build-std=panic_abort,std",
13
13
  "bindgen-dev": "wasm-bindgen ../../target/wasm32-unknown-unknown/wasm-dev/utoo_wasm.wasm --out-dir src/utoo --out-name index --target web --debug --keep-debug --no-demangle",
14
14
  "bindgen-build": "wasm-bindgen ../../target/wasm32-unknown-unknown/release/utoo_wasm.wasm --out-dir src/utoo --out-name index --target web",
15
- "tsx": "rm -rf esm && tsc -p ./tsconfig.json",
16
- "dev": "npm run build-wasm -- --profile wasm-dev && npm run bindgen-dev && npm run tsx && cp src/utoo/index_bg.wasm esm/utoo",
17
- "dev:pm": "npm run build-wasm:pm -- --profile wasm-dev && npm run bindgen-dev && npm run tsx && cp src/utoo/index_bg.wasm esm/utoo",
15
+ "tsc": "rm -rf esm && tsc -p ./tsconfig.json",
16
+ "dev": "npm run build-wasm -- --profile wasm-dev && npm run bindgen-dev && npm run tsc && cp src/utoo/index_bg.wasm esm/utoo",
17
+ "dev:pm": "npm run build-wasm:pm -- --profile wasm-dev && npm run bindgen-dev && npm run tsc && cp src/utoo/index_bg.wasm esm/utoo",
18
18
  "wasm-opt": "wasm-opt src/utoo/index_bg.wasm -o esm/utoo/index_bg.wasm --enable-threads --enable-bulk-memory --enable-nontrapping-float-to-int -Oz",
19
- "build": "npm run build-wasm -- --release && npm run bindgen-build && npm run tsx && npm run wasm-opt",
19
+ "build": "npm run build-wasm -- --release && npm run bindgen-build && npm run tsc && npm run wasm-opt",
20
20
  "prepublishOnly": "npm run build"
21
21
  },
22
22
  "dependencies": {
@@ -30,5 +30,5 @@
30
30
  },
31
31
  "author": "xusd320",
32
32
  "license": "MIT",
33
- "repository": "git@github.com:umijs/mako.git"
33
+ "repository": "git@github.com:utooland/utoo.git"
34
34
  }
@@ -1,2 +0,0 @@
1
- import initWasm from "./utoo";
2
- self.wasm_bindgen = initWasm;
File without changes