@primate/go 0.4.0 → 0.5.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/lib/private/Default.js +10 -9
- package/lib/private/to-request.d.ts +1 -1
- package/lib/private/to-request.js +1 -1
- package/lib/private/to-response.d.ts +1 -1
- package/lib/private/to-response.js +2 -3
- package/lib/private/wrapper.d.ts +5 -1
- package/lib/private/wrapper.js +29 -15
- package/package.json +17 -17
package/lib/private/Default.js
CHANGED
|
@@ -5,12 +5,11 @@ import fail from "@primate/core/fail";
|
|
|
5
5
|
import log from "@primate/core/log";
|
|
6
6
|
import assert from "@rcompat/assert";
|
|
7
7
|
import user from "@rcompat/env/user";
|
|
8
|
-
import
|
|
9
|
-
|
|
10
|
-
const COMMAND = await which("go");
|
|
8
|
+
import io from "@rcompat/io";
|
|
9
|
+
const COMMAND = await io.which("go");
|
|
11
10
|
const ENV = {
|
|
12
11
|
GOARCH: "wasm",
|
|
13
|
-
GOCACHE: (await
|
|
12
|
+
GOCACHE: (await io.run(`${COMMAND} env GOCACHE`, {})).replaceAll("\n", ""),
|
|
14
13
|
GOOS: "js",
|
|
15
14
|
HOME: user.HOME,
|
|
16
15
|
};
|
|
@@ -30,7 +29,7 @@ function postlude(code, route_id) {
|
|
|
30
29
|
}
|
|
31
30
|
async function check_version() {
|
|
32
31
|
try {
|
|
33
|
-
const version = await
|
|
32
|
+
const version = await io.run(`go list -m -f '{{.Version}}' ${REPO}`);
|
|
34
33
|
const trimmed = version.trim();
|
|
35
34
|
const version_match = trimmed.match(/^v?(\d+)\.(\d+)\.(\d+)/);
|
|
36
35
|
if (!version_match)
|
|
@@ -51,18 +50,18 @@ export default class Default extends Runtime {
|
|
|
51
50
|
async build(app, next) {
|
|
52
51
|
await check_version();
|
|
53
52
|
app.bind(this.fileExtension, async (route, { context }) => {
|
|
54
|
-
assert(context === "routes", "go: only route files are supported");
|
|
53
|
+
assert.true(context === "routes", "go: only route files are supported");
|
|
55
54
|
const relative = route.debase(app.path.routes);
|
|
56
55
|
const route_id = relative.path.slice(1, -relative.extension.length);
|
|
57
56
|
// create a temporary .go file in the build directory for compilation
|
|
58
57
|
// this is necessary because `go build` requires an actual file on disk
|
|
59
58
|
const build_go_file = app.runpath("wasm", `${route_id}.go`);
|
|
60
|
-
await build_go_file.directory.create(
|
|
59
|
+
await build_go_file.directory.create();
|
|
61
60
|
await build_go_file.write(postlude(await route.text(), route_id));
|
|
62
61
|
const wasm = app.runpath("wasm", `${route_id}.wasm`);
|
|
63
62
|
try {
|
|
64
63
|
log.info("compiling {0} to WebAssembly", route);
|
|
65
|
-
await
|
|
64
|
+
await io.run(run(wasm, build_go_file), {
|
|
66
65
|
cwd: build_go_file.directory.path,
|
|
67
66
|
env: ENV,
|
|
68
67
|
});
|
|
@@ -74,7 +73,9 @@ export default class Default extends Runtime {
|
|
|
74
73
|
return `
|
|
75
74
|
import wrapper from "@primate/go/wrapper";
|
|
76
75
|
import bytes from "app:wasm/${route_id}.wasm" with { type: "bytes" };
|
|
77
|
-
|
|
76
|
+
import i18n from "app:config:i18n";
|
|
77
|
+
import session from "app:config:session";
|
|
78
|
+
await wrapper(bytes, ${JSON.stringify(route_id)}, { i18n, session });
|
|
78
79
|
`;
|
|
79
80
|
});
|
|
80
81
|
return next(app);
|
|
@@ -10,7 +10,7 @@ async function bridge_form(body) {
|
|
|
10
10
|
const type = v.type;
|
|
11
11
|
const size = v.size;
|
|
12
12
|
meta[k] = { name, size, type };
|
|
13
|
-
// precompute bytes so Go can call a
|
|
13
|
+
// precompute bytes so Go can call a sync getter
|
|
14
14
|
pending.push(v.arrayBuffer().then(buffer => {
|
|
15
15
|
files.push({
|
|
16
16
|
bytes: new Uint8Array(buffer),
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import view from "@primate/core/response/view";
|
|
1
|
+
import response from "@primate/core/response";
|
|
2
|
+
const { error, redirect, view } = response;
|
|
4
3
|
const handlers = { error, redirect, view };
|
|
5
4
|
const is_handler = (handler) => typeof handler === "string" && Object.keys(handlers).includes(handler);
|
|
6
5
|
export default (response) => {
|
package/lib/private/wrapper.d.ts
CHANGED
|
@@ -6,7 +6,11 @@ declare global {
|
|
|
6
6
|
};
|
|
7
7
|
};
|
|
8
8
|
var PRMT_SESSION: any;
|
|
9
|
+
var PRMT_I18N: any;
|
|
9
10
|
var __primate_go_initialized: Set<string> | undefined;
|
|
10
11
|
}
|
|
11
|
-
export default function wrapper(bytes: Uint8Array,
|
|
12
|
+
export default function wrapper(bytes: Uint8Array, route_id: string, context: {
|
|
13
|
+
i18n?: any;
|
|
14
|
+
session?: any;
|
|
15
|
+
}): Promise<void>;
|
|
12
16
|
//# sourceMappingURL=wrapper.d.ts.map
|
package/lib/private/wrapper.js
CHANGED
|
@@ -1,24 +1,38 @@
|
|
|
1
1
|
import env from "@primate/go/env";
|
|
2
2
|
import toRequest from "@primate/go/to-request";
|
|
3
3
|
import to_response from "@primate/go/to-response";
|
|
4
|
-
import session from "primate/config/session";
|
|
5
4
|
import route from "primate/route";
|
|
6
|
-
export default async function wrapper(bytes,
|
|
5
|
+
export default async function wrapper(bytes, route_id, context) {
|
|
7
6
|
if (!globalThis.__primate_go_initialized) {
|
|
8
7
|
globalThis.__primate_go_initialized = new Set();
|
|
9
8
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
9
|
+
if (context.session !== undefined) {
|
|
10
|
+
const session = context.session;
|
|
11
|
+
globalThis.PRMT_SESSION = {
|
|
12
|
+
get exists() { return session.exists; },
|
|
13
|
+
get id() { return session.id; },
|
|
14
|
+
get data() { return JSON.stringify(session.try()); },
|
|
15
|
+
create(data) { session.create(JSON.parse(data)); },
|
|
16
|
+
get() { return JSON.stringify(session.get()); },
|
|
17
|
+
try() { return JSON.stringify(session.try()); },
|
|
18
|
+
set(data) { session.set(JSON.parse(data)); },
|
|
19
|
+
destroy() { session.destroy(); },
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
if (context.i18n !== undefined) {
|
|
23
|
+
const i18n = context.i18n;
|
|
24
|
+
globalThis.PRMT_I18N = {
|
|
25
|
+
get locale() { return i18n.locale.get(); },
|
|
26
|
+
t(key, params) {
|
|
27
|
+
if (!params)
|
|
28
|
+
return i18n(key);
|
|
29
|
+
return i18n(key, JSON.parse(params));
|
|
30
|
+
},
|
|
31
|
+
set(locale) { i18n.locale.set(locale); },
|
|
32
|
+
};
|
|
33
|
+
}
|
|
20
34
|
env();
|
|
21
|
-
const safe_route_id =
|
|
35
|
+
const safe_route_id = route_id.replace(/\//g, "_");
|
|
22
36
|
const registry_name = `__primate_go_registry_${safe_route_id}`;
|
|
23
37
|
const call_go = `__primate_call_go_${safe_route_id}`;
|
|
24
38
|
delete globalThis[registry_name];
|
|
@@ -30,7 +44,7 @@ export default async function wrapper(bytes, routeId) {
|
|
|
30
44
|
const go = new globalThis.Go();
|
|
31
45
|
WebAssembly.instantiate(bytes, go.importObject).then(result => {
|
|
32
46
|
go.run(result.instance).catch(err => {
|
|
33
|
-
console.error("Go runtime error:",
|
|
47
|
+
console.error("Go runtime error:", route_id, err);
|
|
34
48
|
});
|
|
35
49
|
});
|
|
36
50
|
});
|
|
@@ -47,6 +61,6 @@ export default async function wrapper(bytes, routeId) {
|
|
|
47
61
|
return to_response(response);
|
|
48
62
|
});
|
|
49
63
|
}
|
|
50
|
-
globalThis.__primate_go_initialized.add(
|
|
64
|
+
globalThis.__primate_go_initialized.add(route_id);
|
|
51
65
|
}
|
|
52
66
|
//# sourceMappingURL=wrapper.js.map
|
package/package.json
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@primate/go",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Go backend for Primate",
|
|
5
5
|
"homepage": "https://primate.run/docs/backend/go",
|
|
6
6
|
"bugs": "https://github.com/primate-run/primate/issues",
|
|
7
|
+
"type": "module",
|
|
7
8
|
"license": "MIT",
|
|
8
|
-
"files": [
|
|
9
|
-
"/lib/**/*.js",
|
|
10
|
-
"/lib/**/*.d.ts",
|
|
11
|
-
"!/**/*.spec.*"
|
|
12
|
-
],
|
|
13
9
|
"repository": {
|
|
14
10
|
"type": "git",
|
|
15
11
|
"url": "https://github.com/primate-run/primate",
|
|
16
12
|
"directory": "packages/go"
|
|
17
13
|
},
|
|
14
|
+
"files": [
|
|
15
|
+
"/lib/**/*.js",
|
|
16
|
+
"/lib/**/*.d.ts",
|
|
17
|
+
"!/**/*.spec.*"
|
|
18
|
+
],
|
|
18
19
|
"dependencies": {
|
|
19
|
-
"@rcompat/assert": "^0.
|
|
20
|
-
"@rcompat/
|
|
21
|
-
"@rcompat/
|
|
22
|
-
"@rcompat/
|
|
23
|
-
"@
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"@
|
|
20
|
+
"@rcompat/assert": "^0.6.0",
|
|
21
|
+
"@rcompat/env": "^0.13.0",
|
|
22
|
+
"@rcompat/fs": "^0.25.2",
|
|
23
|
+
"@rcompat/io": "^0.3.0",
|
|
24
|
+
"@primate/core": "^0.5.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@rcompat/type": "^0.9.0"
|
|
27
28
|
},
|
|
28
29
|
"peerDependencies": {
|
|
29
|
-
"primate": "^0.
|
|
30
|
+
"primate": "^0.36.0"
|
|
30
31
|
},
|
|
31
|
-
"type": "module",
|
|
32
32
|
"imports": {
|
|
33
33
|
"#*": {
|
|
34
34
|
"apekit": "./src/private/*.ts",
|