@primate/native 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) Terrablue <terrablue@proton.me> and contributors.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@primate/native",
3
+ "version": "0.1.0",
4
+ "description": "Primate native applications",
5
+ "homepage": "https://primatejs.com/modules/native",
6
+ "bugs": "https://github.com/primatejs/primate/issues",
7
+ "license": "MIT",
8
+ "files": [
9
+ "src/**/*.js",
10
+ "!src/**/*.spec.js"
11
+ ],
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/primatejs/primate",
15
+ "directory": "packages/native"
16
+ },
17
+ "dependencies": {
18
+ "@rcompat/cli": "^0.5.1",
19
+ "@rcompat/fs": "^0.4.0",
20
+ "@rcompat/stdio": "^0.4.0",
21
+ "@rcompat/webview": "^0.7.0"
22
+ },
23
+ "type": "module",
24
+ "exports": {
25
+ "./worker": "./src/worker.js",
26
+ ".": "./src/index.js"
27
+ }
28
+ }
package/src/desktop.js ADDED
@@ -0,0 +1,90 @@
1
+ import collect from "@rcompat/fs/collect";
2
+ const html = /^.*.html$/u;
3
+
4
+ export default async app => {
5
+ const location = app.get("location");
6
+ const http = app.get("http");
7
+ const client = app.runpath(location.client);
8
+ const re = /app..*(?:js|css)$/u;
9
+ const $imports = (await Promise.all((await client.collect(re, { recursive: false }))
10
+ .map(async (file, i) => {
11
+ const type = file.extension === ".css" ? "style" : "js";
12
+ const src = `${http.static.root}${file.debase(client).name}`;
13
+ const path = `./${file.debase(`${app.path.build}/`)}`;
14
+ return {
15
+ src,
16
+ path,
17
+ code: `await file(asset${i}).text()`,
18
+ type,
19
+ empty: (await file.text()).length === 0,
20
+ };
21
+ }))).filter(file => !file.empty);
22
+ const d = app.runpath(location.pages);
23
+ const pages = await Promise.all((await collect(d, html, { recursive: true }))
24
+ .map(async file => `${file}`.replace(`${d}/`, _ => "")));
25
+ const app_js = $imports.find($import => $import.src.endsWith(".js"));
26
+
27
+ const assets_scripts = `
28
+ import Webview from "@rcompat/webview/worker/${app.build_target}";
29
+ import join from "@rcompat/fs/join";
30
+ import file from "@rcompat/fs/file";
31
+ import stringify from "@rcompat/object/stringify";
32
+ import crypto from "@rcompat/crypto";
33
+
34
+ const encoder = new TextEncoder();
35
+ const hash = async (data, algorithm = "sha-384") => {
36
+ const bytes = await crypto.subtle.digest(algorithm, encoder.encode(data));
37
+ const prefix = algorithm.replace("-", _ => "");
38
+ return \`\${prefix}-\${btoa(String.fromCharCode(...new Uint8Array(bytes)))}\`;
39
+ };
40
+
41
+ ${$imports.map(({ path }, i) =>
42
+ `import asset${i} from "${path}" with { type: "file" };
43
+ const file${i} = await file(asset${i}).text();`).join("\n ")}
44
+ const assets = [${$imports.map(($import, i) => `{
45
+ src: "${$import.src}",
46
+ code: file${i},
47
+ type: "${$import.type}",
48
+ inline: false,
49
+ integrity: await hash(file${i}),
50
+ }`).join(",\n ")}];
51
+
52
+ ${app_js === undefined ? "" :
53
+ `const imports = {
54
+ app: join("${http.static.root}", "${$imports.find($import =>
55
+ $import.src.includes("app") && $import.src.endsWith(".js")).src}").webpath(),
56
+ };
57
+ // importmap
58
+ assets.push({
59
+ inline: true,
60
+ code: stringify({ imports }),
61
+ type: "importmap",
62
+ integrity: await hash(stringify({ imports })),
63
+ });`}
64
+
65
+ ${pages.map((page, i) =>
66
+ `import i_page${i} from "./${location.pages}/${page}" with { type: "file" };
67
+ const page${i} = await file(i_page${i}).text();`).join("\n ")}
68
+
69
+ const pages = {
70
+ ${pages.map((page, i) => `"${page}": page${i},`).join("\n ")}
71
+ };
72
+
73
+ const loader = {
74
+ page(name) {
75
+ return pages[name] ?? pages["${app.get("pages.app")}"];
76
+ },
77
+ asset(pathname) {
78
+ return assets.find(asset => asset.src === pathname);
79
+ },
80
+ webview() {
81
+ return Webview;
82
+ }
83
+ };
84
+ const target = "${app.build_target}";
85
+
86
+ export { assets, loader, target };
87
+ `;
88
+ await app.path.build.join("target.js").write(assets_scripts);
89
+
90
+ };
package/src/index.js ADDED
@@ -0,0 +1,43 @@
1
+ import dim from "@rcompat/cli/color/dim";
2
+ import execute from "@rcompat/stdio/execute";
3
+ import targets from "./targets.js";
4
+ import log from "@primate/core/log";
5
+
6
+ const target_keys = Object.keys(targets);
7
+ const command = "bun build build/serve.js --conditions=runtime --compile --minify";
8
+
9
+ export default ({
10
+ start = "/",
11
+ } = {}) => {
12
+ return {
13
+ name: "primate:native",
14
+ init(app, next) {
15
+ target_keys.forEach(target => app.target(target, targets[target]));
16
+ return next(app);
17
+ },
18
+ build(app, next) {
19
+ if (target_keys.includes(app.build_target)) {
20
+ app.done(async () => {
21
+ const { flags, exe } = targets[app.build_target];
22
+ const executable_path = dim(`${app.path.build}/${exe}`);
23
+ await execute(`${command} ${flags} --outfile build/${exe}`);
24
+ log.system(`executable written to ${executable_path}`);
25
+ });
26
+ }
27
+ return next(app);
28
+ },
29
+ async serve(app, next) {
30
+ if (target_keys.includes(app.build_target)) {
31
+ const Webview = app.loader.webview();
32
+ const webview = new Webview();
33
+ const { host, port } = app.get("http");
34
+ webview.navigate(`http://${host}:${port}${start}`);
35
+ webview.run();
36
+ webview.closed(() => {
37
+ app.stop();
38
+ });
39
+ }
40
+ return next(app);
41
+ },
42
+ };
43
+ };
package/src/targets.js ADDED
@@ -0,0 +1,37 @@
1
+ import target from "./desktop.js";
2
+
3
+ const desktop = {
4
+ forward: `${process.platform}-${process.arch}`,
5
+ };
6
+
7
+ const linux_x64 = {
8
+ flags: "--target=bun-linux-x64",
9
+ exe: "app",
10
+ target,
11
+ };
12
+
13
+ const windows_x64 = {
14
+ flags: "--target=bun-windows-x64",
15
+ exe: "app.exe",
16
+ target,
17
+ };
18
+
19
+ const darwin_x64 = {
20
+ flags: "--target=bun-darwin-x64",
21
+ exe: "app",
22
+ target,
23
+ };
24
+
25
+ const darwin_arm64 = {
26
+ flags: "--target=bun-darwin-arm64",
27
+ exe: "app",
28
+ target,
29
+ };
30
+
31
+ export default {
32
+ desktop,
33
+ "linux-x64": linux_x64,
34
+ "windows-x64": windows_x64,
35
+ "darwin-x64": darwin_x64,
36
+ "darwin-arm64": darwin_arm64,
37
+ };