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

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,54 @@ 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, serviceWorkerOptions } = options;
23
+ this.serviceWorkerOptions = serviceWorkerOptions;
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 { serviceWorkerUrl, proxiedResourcePath } = this.serviceWorkerOptions;
56
+ await navigator.serviceWorker.register(serviceWorkerUrl);
57
+ (_a = navigator.serviceWorker.controller) === null || _a === void 0 ? void 0 : _a.postMessage({
58
+ [ServiceWorkerHandShake]: true,
59
+ proxiedResourcePath,
60
+ });
61
+ }
38
62
  }
39
63
  async install(packageLock) {
40
64
  await __classPrivateFieldGet(this, _Project_tunnel, "f");
@@ -65,9 +89,8 @@ export class Project {
65
89
  await __classPrivateFieldGet(this, _Project_tunnel, "f");
66
90
  return await this.remote.mkdir(path, options);
67
91
  }
68
- static fork(channel) {
69
- self.postMessage(Fork, {
70
- targetOrigin: "*",
92
+ static fork(channel, eventSource) {
93
+ eventSource.postMessage(Fork, {
71
94
  transfer: [channel.port2],
72
95
  });
73
96
  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,57 @@
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 proxiedResourcePath;
9
+ self.addEventListener("message", (event) => {
10
+ if (event.data && event.data[ServiceWorkerHandShake] === true) {
11
+ proxiedResourcePath = event.data.proxiedResourcePath;
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(`/${proxiedResourcePath}`) ||
19
+ (referrer &&
20
+ new URL(referrer).pathname.startsWith(`/${proxiedResourcePath}`))) {
21
+ await _promise__;
22
+ const projectPath = "." + new URL(url).pathname.replace(`/${proxiedResourcePath}`, "");
23
+ event.respondWith(readFileFromProject(projectPath));
24
+ }
25
+ else {
26
+ event.respondWith(fetch(event.request));
27
+ }
28
+ });
29
+ async function readFileFromProject(projectPath) {
30
+ try {
31
+ const content = await projectEndpoint.readFile(projectPath);
32
+ let mimeType = "application/octet-stream";
33
+ if (projectPath.endsWith(".js")) {
34
+ mimeType = "application/javascript";
35
+ }
36
+ else if (projectPath.endsWith(".css")) {
37
+ mimeType = "text/css";
38
+ }
39
+ else if (projectPath.endsWith(".html")) {
40
+ mimeType = "text/html";
41
+ }
42
+ else if (projectPath.endsWith(".json")) {
43
+ mimeType = "application/json";
44
+ }
45
+ return new Response(content, {
46
+ headers: {
47
+ "Content-Type": mimeType,
48
+ ...(mimeType === "text/html"
49
+ ? { "Cross-Origin-Embedder-Policy": "require-corp" }
50
+ : {}),
51
+ },
52
+ });
53
+ }
54
+ catch (e) {
55
+ return new Response("Not Found", { status: 404 });
56
+ }
57
+ }
@@ -0,0 +1 @@
1
+ export {};
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
+ serviceWorkerOptions?: ServiceWorkerOptions;
33
+ }
34
+ export interface ServiceWorkerOptions {
35
+ serviceWorkerUrl: string;
36
+ proxiedResourcePath: string;
31
37
  }
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.14",
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": {
File without changes
File without changes