@primate/native 0.1.3 → 0.1.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@primate/native",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Primate native applications",
5
5
  "homepage": "https://primatejs.com/modules/native",
6
6
  "bugs": "https://github.com/primatejs/primate/issues",
@@ -16,14 +16,15 @@
16
16
  },
17
17
  "dependencies": {
18
18
  "@rcompat/cli": "^0.5.1",
19
- "@rcompat/fs": "^0.5.0",
19
+ "@rcompat/fs": "^0.5.1",
20
20
  "@rcompat/stdio": "^0.5.0",
21
21
  "@rcompat/webview": "^0.7.0",
22
- "@primate/core": "^0.1.5"
22
+ "@primate/core": "^0.1.7"
23
23
  },
24
24
  "type": "module",
25
25
  "exports": {
26
- "./worker": "./src/worker.js",
27
- ".": "./src/index.js"
26
+ ".": "./src/index.js",
27
+ "./*": "./src/public/*.js",
28
+ "./platform/*": "./src/platform/*.js"
28
29
  }
29
30
  }
package/src/desktop.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import collect from "@rcompat/fs/collect";
2
+ import join from "@rcompat/fs/join";
2
3
  import webpath from "@rcompat/fs/webpath";
3
4
 
4
5
  const html = /^.*.html$/u;
@@ -9,9 +10,9 @@ export default async app => {
9
10
  const client = app.runpath(location.client);
10
11
  const re = /app..*(?:js|css)$/u;
11
12
 
12
- const import_statics = (await client.collect()).map((path, i) => `
13
+ const static_imports = (await client.collect()).map((path, i) => `
13
14
  import static${i} from "${webpath(`./client${path.debase(client)}`)}" with { type: "file" };
14
- statics["${webpath(path.debase(client))}"] = await file(static${i});`)
15
+ static_imports["${webpath(path.debase(client))}"] = static${i};`)
15
16
  .join("\n");
16
17
 
17
18
  const $imports = (await Promise.all((await client.collect(re, { recursive: false }))
@@ -22,94 +23,62 @@ export default async app => {
22
23
  return {
23
24
  src,
24
25
  path,
25
- code: `await file(asset${i}).text()`,
26
+ code: `await load_text(asset${i})`,
26
27
  type,
27
28
  empty: (await file.text()).length === 0,
28
29
  };
29
30
  }))).filter(file => !file.empty);
30
- const d = app.runpath(location.pages);
31
+ const d = app.runpath(location.server, location.pages);
31
32
  const pages = await Promise.all((await collect(d, html, { recursive: true }))
32
33
  .map(async file => `${file}`.replace(`${d}/`, _ => "")));
33
34
  const app_js = $imports.find($import => $import.src.endsWith(".js"));
34
35
 
35
36
  const assets_scripts = `
36
- import Webview from "@rcompat/webview/worker/${app.build_target}";
37
- import join from "@rcompat/fs/join";
38
- import file from "@rcompat/fs/file";
39
- import stringify from "@rcompat/object/stringify";
40
- import crypto from "@rcompat/crypto";
41
- import { OK } from "@rcompat/http/status";
42
- import { resolve } from "@rcompat/http/mime";
37
+ import Webview from "@primate/native/platform/${app.build_target}";
38
+ import loader from "@primate/native/loader";
39
+ import load_text from "primate/load-text";
43
40
 
44
- const encoder = new TextEncoder();
45
- const hash = async (data, algorithm = "sha-384") => {
46
- const bytes = await crypto.subtle.digest(algorithm, encoder.encode(data));
47
- const prefix = algorithm.replace("-", _ => "");
48
- return \`\${prefix}-\${btoa(String.fromCharCode(...new Uint8Array(bytes)))}\`;
49
- };
50
-
51
- const statics = {};
52
- ${import_statics}
41
+ const static_imports = {};
42
+ ${static_imports}
53
43
 
54
44
  ${$imports.map(({ path }, i) =>
55
45
  `import asset${i} from "${path}" with { type: "file" };
56
- const file${i} = await file(asset${i}).text();`).join("\n ")}
46
+ const file${i} = await load_text(asset${i});`).join("\n ")}
57
47
  const assets = [${$imports.map(($import, i) => `{
58
48
  src: "${$import.src}",
59
49
  code: file${i},
60
50
  type: "${$import.type}",
61
51
  inline: false,
62
- integrity: await hash(file${i}),
63
52
  }`).join(",\n ")}];
64
53
 
65
54
  ${app_js === undefined ? "" :
66
- `const imports = {
67
- app: join("${http.static.root}", "${$imports.find($import =>
68
- $import.src.includes("app") && $import.src.endsWith(".js")).src}").webpath(),
55
+ `
56
+ const imports = {
57
+ app: "${join(http.static.root, $imports.find($import =>
58
+ $import.src.includes("app") && $import.src.endsWith(".js")).src).webpath()}"
69
59
  };
70
60
  // importmap
71
61
  assets.push({
72
62
  inline: true,
73
- code: stringify({ imports }),
63
+ code: { imports },
74
64
  type: "importmap",
75
- integrity: await hash(stringify({ imports })),
76
65
  });`}
77
66
 
67
+ const page_imports = {};
78
68
  ${pages.map((page, i) =>
79
- `import i_page${i} from "${webpath(`./${location.pages}/${page}`)}" with { type: "file" };
80
- const page${i} = await file(i_page${i}).text();`).join("\n ")}
69
+ `import page${i} from "${webpath(`./${location.server}/${location.pages}/${page}`)}" with { type: "file" };
70
+ page_imports["${page}"] = page${i};`).join("\n ")}
81
71
 
82
- const pages = {
83
- ${pages.map((page, i) => `"${page}": page${i},`).join("\n ")}
72
+ export default {
73
+ assets,
74
+ loader: await loader({
75
+ page_imports,
76
+ static_imports,
77
+ pages_app: "${app.get("pages.app")}",
78
+ Webview,
79
+ }),
80
+ target: "${app.build_target}",
84
81
  };
85
- const serve_asset = asset => new Response(asset.stream(), {
86
- status: OK,
87
- headers: {
88
- "Content-Type": resolve(asset.name),
89
- },
90
- });
91
-
92
- const loader = {
93
- page(name) {
94
- return pages[name] ?? pages["${app.get("pages.app")}"];
95
- },
96
- asset(pathname) {
97
- const root_asset = statics[pathname];
98
- if (root_asset !== undefined) {
99
- return serve_asset(root_asset);
100
- }
101
- const static_asset = statics[\`/static\${pathname}\`];
102
- if (static_asset !== undefined) {
103
- return serve_asset(static_asset);
104
- }
105
- },
106
- webview() {
107
- return Webview;
108
- }
109
- };
110
- const target = "${app.build_target}";
111
-
112
- export { assets, loader, target };
113
82
  `;
114
83
  await app.path.build.join("target.js").write(assets_scripts);
115
84
 
@@ -0,0 +1 @@
1
+ export { default } from "@rcompat/webview/worker/darwin-arm64";
@@ -0,0 +1 @@
1
+ export { default } from "@rcompat/webview/worker/darwin-x64";
@@ -0,0 +1 @@
1
+ export { default } from "@rcompat/webview/worker/linux-x64";
@@ -0,0 +1 @@
1
+ export { default } from "@rcompat/webview/worker/windows-x64";
@@ -0,0 +1,36 @@
1
+ import serve_asset from "primate/serve-asset";
2
+ import file from "@rcompat/fs/file";
3
+
4
+ const load = async resource_map =>
5
+ Object.fromEntries(await Promise.all(Object.entries(resource_map).map(
6
+ async ([key, url]) => [key, await file(url).text()])));
7
+
8
+ export default async ({
9
+ pages_app,
10
+ page_imports,
11
+ static_imports,
12
+ Webview,
13
+ }) => {
14
+ const statics = Object.fromEntries(Object.entries(static_imports).map(
15
+ ([key, url]) => [key, file(url)]));
16
+ const pages = await load(page_imports);
17
+
18
+ return {
19
+ page(name) {
20
+ return pages[name] ?? pages[pages_app];
21
+ },
22
+ asset(pathname) {
23
+ const root_asset = statics[pathname];
24
+ if (root_asset !== undefined) {
25
+ return serve_asset(root_asset);
26
+ }
27
+ const static_asset = statics[`/static${pathname}`];
28
+ if (static_asset !== undefined) {
29
+ return serve_asset(static_asset);
30
+ }
31
+ },
32
+ webview() {
33
+ return Webview;
34
+ },
35
+ };
36
+ };