@native-sdk/cli 0.8.4 → 0.9.1
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/build/app.zig +1178 -74
- package/build.zig +715 -17
- package/build.zig.zon +1 -0
- package/native-sdk.d.ts +10 -0
- package/package.json +12 -11
- package/packages/core/compile-surface/core.ts +435 -3
- package/packages/core/package-lock.json +11 -11
- package/packages/core/package.json +2 -2
- package/packages/core/scripts/compiler_typecheck.mjs +116 -15
- package/packages/core/scripts/gen_service_surface.mjs +268 -0
- package/packages/core/scripts/merge_static_archives.mjs +78 -0
- package/packages/core/scripts/npm_static_spike.mjs +64 -0
- package/packages/core/scripts/run_external_core_compiler.mjs +122 -6
- package/packages/core/scripts/run_external_service_compiler.mjs +280 -0
- package/packages/core/scripts/stage_external_core.mjs +9 -1
- package/packages/core/scripts/stage_external_services.mjs +172 -0
- package/packages/core/scripts/surface_manifest_diff.mjs +253 -0
- package/packages/core/scripts/vendor_service_packages.mjs +189 -0
- package/packages/core/sdk/core.d.ts +224 -0
- package/packages/core/sdk/core.ts +694 -22
- package/packages/core/sdk/events.d.ts +89 -0
- package/packages/core/sdk/events.ts +128 -0
- package/packages/core/src/checker.ts +936 -15
- package/packages/core/src/cli.ts +156 -3
- package/packages/core/src/contract.ts +28 -5
- package/packages/core/src/devhost.ts +1876 -16
- package/packages/core/src/devhost_journal.mjs +183 -0
- package/packages/core/src/devhost_resolver.mjs +17 -1
- package/packages/core/src/devhost_service_worker.mjs +90 -0
- package/packages/core/src/diagnostics.ts +179 -22
- package/packages/core/src/frontend.ts +210 -34
- package/packages/core/src/infer.ts +24 -0
- package/packages/core/src/modules.ts +129 -80
- package/packages/core/src/service_contract.ts +597 -0
- package/packages/core/src/sqlite_cli.ts +93 -0
- package/packages/core/src/sqlite_codegen.ts +806 -0
- package/packages/core/src/sqlite_runtime_policy.ts +364 -0
- package/packages/core/src/typed_ast.ts +36 -8
- package/skill-data/automation/SKILL.md +1 -1
- package/skill-data/core/references/project-anatomy.md +1 -1
- package/skill-data/native-ui/SKILL.md +58 -5
- package/skill-data/ts-core/SKILL.md +123 -41
- package/skill-data/ts-services/SKILL.md +126 -0
- package/skill-data/ts-services/references/service-surface.md +604 -0
- package/skill-data/zig/SKILL.md +1 -1
- package/skills/native-sdk/SKILL.md +1 -1
- package/src/app_runner/no_migrations.zig +3 -0
- package/src/app_runner/no_services.zig +20 -0
- package/src/app_runner/root.zig +133 -4
- package/src/app_runner/ts_core_main.zig +254 -8
- package/src/app_runner/ts_core_mobile.zig +241 -0
- package/src/automation/protocol.zig +5 -1
- package/src/automation/snapshot.zig +31 -11
- package/src/embed/app_exports.zig +12 -1
- package/src/embed/c_api.zig +48 -2
- package/src/embed/conversions.zig +4 -5
- package/src/embed/host.zig +109 -1
- package/src/embed/root.zig +8 -0
- package/src/embed/tests.zig +241 -0
- package/src/embed/types.zig +51 -0
- package/src/embed/ui_host.zig +185 -5
- package/src/platform/android/NativeSdkActivity.java +131 -0
- package/src/platform/android/android_host.c +182 -1
- package/src/platform/android/native_sdk_app.h +17 -0
- package/src/platform/ios/native_sdk_app.h +17 -0
- package/src/platform/ios/uikit_host.m +115 -0
- package/src/platform/linux/cef_host.cpp +8 -2
- package/src/platform/linux/gtk_host.c +289 -22
- package/src/platform/linux/gtk_host.h +5 -1
- package/src/platform/linux/root.zig +109 -22
- package/src/platform/macos/appkit_host.h +24 -7
- package/src/platform/macos/appkit_host.m +686 -64
- package/src/platform/macos/cef_host.mm +400 -66
- package/src/platform/macos/root.zig +304 -47
- package/src/platform/null_platform.zig +435 -48
- package/src/platform/null_platform_tests.zig +113 -0
- package/src/platform/root.zig +19 -0
- package/src/platform/types.zig +281 -28
- package/src/platform/windows/root.zig +207 -27
- package/src/platform/windows/webview2_host.cpp +334 -51
- package/src/primitives/app_manifest/comptime_scene.zig +2 -0
- package/src/primitives/app_manifest/root.zig +4 -0
- package/src/primitives/app_manifest/tests.zig +3 -0
- package/src/primitives/app_manifest/types.zig +32 -0
- package/src/primitives/app_manifest/validation.zig +19 -1
- package/src/root.zig +53 -0
- package/src/runtime/api.zig +19 -0
- package/src/runtime/automation_commands.zig +35 -2
- package/src/runtime/automation_snapshot.zig +32 -20
- package/src/runtime/bridge_permission_tests.zig +58 -3
- package/src/runtime/bridge_responses.zig +4 -2
- package/src/runtime/builtin_bridge.zig +11 -1
- package/src/runtime/canvas_frame.zig +18 -11
- package/src/runtime/canvas_frame_animation_tests.zig +4 -0
- package/src/runtime/clock.zig +13 -1
- package/src/runtime/core.zig +145 -7
- package/src/runtime/credentials_store.zig +115 -0
- package/src/runtime/effects.zig +3701 -231
- package/src/runtime/effects_credentials_tests.zig +332 -0
- package/src/runtime/effects_file_tests.zig +587 -0
- package/src/runtime/effects_host_tests.zig +208 -1
- package/src/runtime/effects_notification_tests.zig +11 -0
- package/src/runtime/effects_relational_tests.zig +314 -0
- package/src/runtime/effects_store_tests.zig +211 -0
- package/src/runtime/file_access.zig +400 -0
- package/src/runtime/file_effect_crash_helper.zig +28 -0
- package/src/runtime/file_effect_crash_tests.zig +37 -0
- package/src/runtime/flow.zig +35 -10
- package/src/runtime/gpu_surface_events.zig +6 -1
- package/src/runtime/native_command_event_tests.zig +86 -2
- package/src/runtime/persist_store.zig +666 -0
- package/src/runtime/pty.zig +5 -2
- package/src/runtime/record_store.zig +788 -0
- package/src/runtime/relational_store.zig +854 -0
- package/src/runtime/root.zig +54 -0
- package/src/runtime/runtime_event_tests.zig +70 -0
- package/src/runtime/service_host.zig +711 -0
- package/src/runtime/service_pool.zig +1314 -0
- package/src/runtime/session_blobs.zig +4 -1
- package/src/runtime/session_journal.zig +158 -5
- package/src/runtime/session_record.zig +215 -0
- package/src/runtime/session_replay.zig +206 -11
- package/src/runtime/session_tests.zig +81 -0
- package/src/runtime/shell_layout.zig +1 -0
- package/src/runtime/shell_layout_tests.zig +120 -0
- package/src/runtime/sqlite_engine.zig +474 -0
- package/src/runtime/state.zig +16 -0
- package/src/runtime/system_services.zig +170 -38
- package/src/runtime/tests.zig +4 -0
- package/src/runtime/ts_core_host.zig +1067 -95
- package/src/runtime/ts_core_host_tests.zig +325 -11
- package/src/runtime/ts_ui_app.zig +953 -2
- package/src/runtime/ui_app.zig +434 -96
- package/src/runtime/ui_app_tests.zig +287 -11
- package/src/runtime/ui_app_window_tests.zig +61 -7
- package/src/runtime/validation.zig +42 -0
- package/src/runtime/view.zig +9 -0
- package/src/runtime/window_storage.zig +1 -0
- package/src/runtime/window_view_runtime.zig +2 -3
- package/src/runtime/window_views.zig +19 -0
- package/src/tooling/android.zig +21 -16
- package/src/tooling/db.zig +160 -0
- package/src/tooling/ios.zig +7 -1
- package/src/tooling/manifest.zig +214 -2
- package/src/tooling/package.zig +116 -0
- package/src/tooling/raw_manifest.zig +30 -0
- package/src/tooling/root.zig +1 -0
- package/src/tooling/templates.zig +189 -4
- package/src/tooling/ts_core.zig +366 -17
- package/src/tooling/verbs.zig +7 -0
- package/third_party/sqlite/README.md +13 -0
- package/third_party/sqlite/sqlite3.c +269649 -0
- package/third_party/sqlite/sqlite3.h +14349 -0
- package/tools/corewire/core_abi.zig +14 -2
- package/tools/corewire/emit.zig +98 -29
- package/tools/corewire/emit_facade.zig +442 -27
- package/tools/corewire/emit_profile.zig +6 -2
- package/tools/corewire/emit_service.zig +924 -0
- package/tools/corewire/main.zig +122 -0
- package/tools/corewire/service_contract.zig +250 -0
- package/tools/corewire/sidecar.zig +26 -15
|
@@ -9,8 +9,12 @@
|
|
|
9
9
|
// the same rule).
|
|
10
10
|
//
|
|
11
11
|
// node run_external_core_compiler.mjs --stage <dir> --name <symbol name>
|
|
12
|
-
// --manifest <packages/core/package.json> --
|
|
13
|
-
// --out-
|
|
12
|
+
// --manifest <packages/core/package.json> --frontend-sidecar <file>
|
|
13
|
+
// --out-archive <file>
|
|
14
|
+
// --out-sidecar <file>
|
|
15
|
+
// [--host-platform <arch-os-abi> --target-platform <arch-os-abi>
|
|
16
|
+
// --zig-exe <path>] [--android-ndk <dir>]
|
|
17
|
+
// (--compiler <cmd> | --compiler-js <main.js>)
|
|
14
18
|
|
|
15
19
|
import { spawnSync } from "node:child_process";
|
|
16
20
|
import fs from "node:fs";
|
|
@@ -23,12 +27,12 @@ function parseArgs(argv) {
|
|
|
23
27
|
const key = argv[i];
|
|
24
28
|
const value = argv[i + 1];
|
|
25
29
|
if (!key.startsWith("--") || value === undefined) {
|
|
26
|
-
console.error("usage: run_external_core_compiler.mjs --stage <dir> --name <n> --manifest <package.json> --out-archive <file> --out-sidecar <file> (--compiler <cmd> | --compiler-js <main.js>)");
|
|
30
|
+
console.error("usage: run_external_core_compiler.mjs --stage <dir> --name <n> --manifest <package.json> --frontend-sidecar <file> --out-archive <file> --out-sidecar <file> [--host-platform <arch-os-abi> --target-platform <arch-os-abi> --zig-exe <path>] (--compiler <cmd> | --compiler-js <main.js>)");
|
|
27
31
|
process.exit(2);
|
|
28
32
|
}
|
|
29
33
|
args[key.slice(2)] = value;
|
|
30
34
|
}
|
|
31
|
-
for (const required of ["stage", "name", "manifest", "out-archive", "out-sidecar"]) {
|
|
35
|
+
for (const required of ["stage", "name", "manifest", "frontend-sidecar", "out-archive", "out-sidecar"]) {
|
|
32
36
|
if (!(required in args)) {
|
|
33
37
|
console.error(`run_external_core_compiler.mjs: missing --${required}`);
|
|
34
38
|
process.exit(2);
|
|
@@ -38,13 +42,17 @@ function parseArgs(argv) {
|
|
|
38
42
|
console.error("run_external_core_compiler.mjs: supply --compiler <cmd> or --compiler-js <main.js>");
|
|
39
43
|
process.exit(2);
|
|
40
44
|
}
|
|
45
|
+
if (Boolean(args["host-platform"]) !== Boolean(args["target-platform"])) {
|
|
46
|
+
console.error("run_external_core_compiler.mjs: supply --host-platform and --target-platform together");
|
|
47
|
+
process.exit(2);
|
|
48
|
+
}
|
|
41
49
|
return args;
|
|
42
50
|
}
|
|
43
51
|
|
|
44
52
|
const args = parseArgs(process.argv);
|
|
45
53
|
// Every path argument resolves against the INVOCATION's cwd up front:
|
|
46
54
|
// the compile itself runs from a scratch directory.
|
|
47
|
-
for (const key of ["stage", "manifest", "out-archive", "out-sidecar", "compiler-js"]) {
|
|
55
|
+
for (const key of ["stage", "manifest", "frontend-sidecar", "out-archive", "out-sidecar", "compiler-js", "zig-exe", "android-ndk"]) {
|
|
48
56
|
if (key in args) args[key] = path.resolve(args[key]);
|
|
49
57
|
}
|
|
50
58
|
// --compiler is a COMMAND: a bare executable path (possibly containing
|
|
@@ -54,6 +62,86 @@ const argv0 = args.compiler
|
|
|
54
62
|
? (fs.existsSync(args.compiler) ? [args.compiler] : args.compiler.split(/\s+/))
|
|
55
63
|
: [process.execPath, args["compiler-js"]];
|
|
56
64
|
|
|
65
|
+
// App and fixture build graphs state the host and target explicitly. A
|
|
66
|
+
// differing target must compile the core archive through the same ScriptC
|
|
67
|
+
// zig-cc lane as the service archive that links beside it; otherwise a cross
|
|
68
|
+
// build mixes a host-format core archive with target-format Zig and service
|
|
69
|
+
// objects. Standalone host-only callers may omit both platform arguments.
|
|
70
|
+
// The pairing matrix is the pinned compiler's build matrix, shared with
|
|
71
|
+
// run_external_service_compiler.mjs: same-triple compiles run the native
|
|
72
|
+
// lane; Linux and Windows GNU targets cross-compile from any desktop host;
|
|
73
|
+
// macOS targets need a macOS build host.
|
|
74
|
+
const hostParts = args["host-platform"]?.split("-") ?? [];
|
|
75
|
+
const targetParts = args["target-platform"]?.split("-") ?? [];
|
|
76
|
+
const hostArch = hostParts[0] ?? "";
|
|
77
|
+
const hostOs = (hostParts[1] ?? "").split(".")[0];
|
|
78
|
+
const hostAbi = (hostParts[2] ?? "").split(".")[0];
|
|
79
|
+
const targetArch = targetParts[0] ?? "";
|
|
80
|
+
const targetOs = (targetParts[1] ?? "").split(".")[0];
|
|
81
|
+
const targetAbi = (targetParts[2] ?? "").split(".")[0];
|
|
82
|
+
// The two mobile families, from the build graph's Zig triple spelling:
|
|
83
|
+
// `aarch64-ios[-simulator]` and `aarch64-linux-android`. The pinned compiler
|
|
84
|
+
// admits exactly aarch64 for both — library archives, which is the only
|
|
85
|
+
// output this driver produces.
|
|
86
|
+
const iosTarget = targetOs === "ios";
|
|
87
|
+
const androidTarget = targetOs === "linux" && targetAbi === "android";
|
|
88
|
+
const mobileTarget = iosTarget || androidTarget;
|
|
89
|
+
// The canonical SCRIPTC_TARGET spelling for a mobile compile: the compiler
|
|
90
|
+
// names iOS targets with the vendor (`aarch64-apple-ios[-simulator]`) and
|
|
91
|
+
// Android with the Zig triple itself.
|
|
92
|
+
const scriptcTarget = iosTarget
|
|
93
|
+
? `aarch64-apple-ios${targetAbi === "simulator" ? "-simulator" : ""}`
|
|
94
|
+
: args["target-platform"];
|
|
95
|
+
// A Windows host's Zig triple defaults to GNU, but an explicit same-arch MSVC
|
|
96
|
+
// target still compiles through native clang and the installed Windows SDK.
|
|
97
|
+
const nativeWindows = hostOs === "windows" && targetOs === "windows" && hostArch === targetArch &&
|
|
98
|
+
(targetAbi === hostAbi || targetAbi === "msvc");
|
|
99
|
+
const cross = args["host-platform"] !== undefined &&
|
|
100
|
+
args["target-platform"] !== args["host-platform"] && !nativeWindows;
|
|
101
|
+
if (mobileTarget) {
|
|
102
|
+
const desktopHost = ["macos", "linux", "windows"].includes(hostOs);
|
|
103
|
+
const admitted = desktopHost && targetArch === "aarch64" && (!iosTarget || hostOs === "macos");
|
|
104
|
+
if (!admitted) {
|
|
105
|
+
console.error(
|
|
106
|
+
iosTarget && hostOs !== "macos"
|
|
107
|
+
? `TypeScript cores for an iOS target (${args["target-platform"]}) compile on a macOS build host only — the Apple SDK sysroot and Mach-O localization live there — but this build host is ${args["host-platform"]}. Build iOS apps on a Mac.`
|
|
108
|
+
: `TypeScript cores compile for the mobile targets the pinned compiler covers — aarch64 iOS/iOS-simulator (macOS host) and aarch64 Android — but this build pairs host ${args["host-platform"]} with target ${args["target-platform"]}.`,
|
|
109
|
+
);
|
|
110
|
+
process.exit(2);
|
|
111
|
+
}
|
|
112
|
+
} else if (cross) {
|
|
113
|
+
const desktopHost = ["macos", "linux", "windows"].includes(hostOs);
|
|
114
|
+
const admitted = desktopHost &&
|
|
115
|
+
(targetOs === "linux" ||
|
|
116
|
+
(targetOs === "windows" && targetAbi === "gnu") ||
|
|
117
|
+
(targetOs === "macos" && hostOs === "macos"));
|
|
118
|
+
if (!admitted) {
|
|
119
|
+
console.error(
|
|
120
|
+
targetOs === "windows" && targetAbi !== "gnu"
|
|
121
|
+
? `TypeScript cores for a cross-target Windows build (${args["target-platform"]}) require the GNU ABI: Zig supplies that target's CRT and system libraries, while an MSVC target needs a native Windows toolchain. Build ${targetArch}-windows-msvc on a matching Windows host, or cross-compile as "${targetArch}-windows-gnu".`
|
|
122
|
+
: targetOs === "macos"
|
|
123
|
+
? `TypeScript cores for a macOS target (${args["target-platform"]}) compile on a macOS build host only — Apple linking needs the host toolchain's SDK — but this build host is ${args["host-platform"]}. Build macOS apps on a Mac.`
|
|
124
|
+
: `TypeScript cores compile for desktop targets the pinned compiler covers — Linux and Windows GNU from a macOS/Linux/Windows build host, macOS from a macOS host — but this build pairs host ${args["host-platform"]} with target ${args["target-platform"]}.`,
|
|
125
|
+
);
|
|
126
|
+
process.exit(2);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
// Cross compiles ride the compiler's zig-cc lane: SCRIPTC_TARGET carries the
|
|
130
|
+
// compiler's own mobile spellings for iOS/Android, --zig-exe's directory
|
|
131
|
+
// fronts PATH, and an Android build threads the NDK location the same way
|
|
132
|
+
// (--android-ndk becomes ANDROID_NDK_ROOT for the sysroot discovery).
|
|
133
|
+
const compileEnv = cross
|
|
134
|
+
? {
|
|
135
|
+
...process.env,
|
|
136
|
+
SCRIPTC_CC: "zigcc",
|
|
137
|
+
SCRIPTC_TARGET: scriptcTarget,
|
|
138
|
+
...(args["zig-exe"]
|
|
139
|
+
? { PATH: `${path.dirname(args["zig-exe"])}${path.delimiter}${process.env.PATH ?? ""}` }
|
|
140
|
+
: {}),
|
|
141
|
+
...(args["android-ndk"] && androidTarget ? { ANDROID_NDK_ROOT: args["android-ndk"] } : {}),
|
|
142
|
+
}
|
|
143
|
+
: process.env;
|
|
144
|
+
|
|
57
145
|
// The profile's determinism-fence table is RELEASE-PINNED DATA (see
|
|
58
146
|
// tools/corewire/emit_profile.zig): its ids resolve against one
|
|
59
147
|
// toolchain release's surface manifest, so the supplied command must BE
|
|
@@ -84,6 +172,7 @@ try {
|
|
|
84
172
|
const build = spawnSync(argv0[0], [...argv0.slice(1), "build", "--lib", "--profile", "profile.json", "-o", args.name], {
|
|
85
173
|
cwd: work,
|
|
86
174
|
stdio: "inherit",
|
|
175
|
+
env: compileEnv,
|
|
87
176
|
});
|
|
88
177
|
if (build.status !== 0) process.exit(build.status ?? 1);
|
|
89
178
|
|
|
@@ -102,7 +191,34 @@ try {
|
|
|
102
191
|
fs.mkdirSync(path.dirname(args["out-archive"]), { recursive: true });
|
|
103
192
|
fs.mkdirSync(path.dirname(args["out-sidecar"]), { recursive: true });
|
|
104
193
|
fs.copyFileSync(path.join(work, produced), args["out-archive"]);
|
|
105
|
-
|
|
194
|
+
|
|
195
|
+
// The pinned compiler owns the archive contract (including its build id),
|
|
196
|
+
// while the SDK frontend owns facts the compiler release predates. Carry
|
|
197
|
+
// exactly that checked source metadata across the compile boundary.
|
|
198
|
+
const compiledContract = JSON.parse(fs.readFileSync(sidecar, "utf8"));
|
|
199
|
+
const frontendContract = JSON.parse(fs.readFileSync(args["frontend-sidecar"], "utf8"));
|
|
200
|
+
const modelUnbound = frontendContract.model_unbound;
|
|
201
|
+
const msgUnbound = frontendContract.msg?.unbound;
|
|
202
|
+
const stringList = (value) => Array.isArray(value) && value.every((entry) => typeof entry === "string");
|
|
203
|
+
if (
|
|
204
|
+
!/^[0-9a-f]{16}$/.test(frontendContract.model_fingerprint ?? "") ||
|
|
205
|
+
typeof frontendContract.has_migrate !== "boolean" ||
|
|
206
|
+
!stringList(modelUnbound) ||
|
|
207
|
+
!stringList(msgUnbound)
|
|
208
|
+
) {
|
|
209
|
+
console.error("the frontend contract is missing its checked persistence or unbound-view metadata — the SDK frontend and build driver are out of sync");
|
|
210
|
+
process.exit(1);
|
|
211
|
+
}
|
|
212
|
+
compiledContract.model_fingerprint = frontendContract.model_fingerprint;
|
|
213
|
+
compiledContract.has_migrate = frontendContract.has_migrate;
|
|
214
|
+
// Model and Msg are re-exported into the generated facade, so the pinned
|
|
215
|
+
// compiler cannot resolve that entry module's viewUnbound list against an
|
|
216
|
+
// entry-owned root declaration. These lists are lint-only source facts:
|
|
217
|
+
// restore the frontend's checked names without replacing any ABI fact the
|
|
218
|
+
// compiler co-emitted for the archive.
|
|
219
|
+
compiledContract.model_unbound = modelUnbound;
|
|
220
|
+
compiledContract.msg = { ...(compiledContract.msg ?? {}), unbound: msgUnbound };
|
|
221
|
+
fs.writeFileSync(args["out-sidecar"], `${JSON.stringify(compiledContract, null, 2)}\n`);
|
|
106
222
|
} finally {
|
|
107
223
|
fs.rmSync(work, { recursive: true, force: true });
|
|
108
224
|
}
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Exact-pinned scriptc compile for the service class. Two output lanes over
|
|
3
|
+
// one staged tree: --out-exe builds the plain-scriptc child executable
|
|
4
|
+
// (service_host_main.ts, framed stdio), --out-archive builds the library-mode
|
|
5
|
+
// archive the in-process carrier links (service_inproc_main.ts under the
|
|
6
|
+
// staged service_profile.json). One invocation may produce either or both.
|
|
7
|
+
// The compiler version is verified against the one packages/core pin and the
|
|
8
|
+
// echo in services.contract.json before any compiler work starts. Host/target
|
|
9
|
+
// pairings follow the pinned compiler's build matrix: same-triple compiles run
|
|
10
|
+
// natively; Linux and Windows GNU targets cross-compile from any desktop host
|
|
11
|
+
// over the compiler's zig-cc lane; macOS targets need a macOS build host.
|
|
12
|
+
// Mobile targets (aarch64 iOS, iOS simulator, and Android) are archive-only:
|
|
13
|
+
// --out-archive is admitted (iOS from a macOS host, Android from any desktop
|
|
14
|
+
// host with an NDK), --out-exe refuses with the in-process pointer.
|
|
15
|
+
|
|
16
|
+
import { spawnSync } from "node:child_process";
|
|
17
|
+
import fs from "node:fs";
|
|
18
|
+
import os from "node:os";
|
|
19
|
+
import path from "node:path";
|
|
20
|
+
|
|
21
|
+
function parseArgs(argv) {
|
|
22
|
+
const args = {};
|
|
23
|
+
for (let i = 2; i < argv.length; i += 2) {
|
|
24
|
+
const key = argv[i];
|
|
25
|
+
const value = argv[i + 1];
|
|
26
|
+
if (!key?.startsWith("--") || value === undefined) {
|
|
27
|
+
console.error("usage: run_external_service_compiler.mjs --stage <dir> --manifest <package.json> --contract <services.contract.json> (--out-exe <file> | --out-archive <file>) --host-platform <arch-os-abi> --target-platform <arch-os-abi> [--zig-exe <path>] [--android-ndk <dir>] (--compiler <cmd> | --compiler-js <main.js>)");
|
|
28
|
+
process.exit(2);
|
|
29
|
+
}
|
|
30
|
+
args[key.slice(2)] = value;
|
|
31
|
+
}
|
|
32
|
+
for (const required of ["stage", "manifest", "contract", "host-platform", "target-platform"]) {
|
|
33
|
+
if (!args[required]) {
|
|
34
|
+
console.error(`run_external_service_compiler.mjs: missing --${required}`);
|
|
35
|
+
process.exit(2);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (!args["out-exe"] && !args["out-archive"]) {
|
|
39
|
+
console.error("run_external_service_compiler.mjs: supply --out-exe, --out-archive, or both");
|
|
40
|
+
process.exit(2);
|
|
41
|
+
}
|
|
42
|
+
if (!args.compiler && !args["compiler-js"]) {
|
|
43
|
+
console.error("run_external_service_compiler.mjs: supply --compiler or --compiler-js");
|
|
44
|
+
process.exit(2);
|
|
45
|
+
}
|
|
46
|
+
return args;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const args = parseArgs(process.argv);
|
|
50
|
+
|
|
51
|
+
// The pairing matrix the pinned compiler covers. Same-triple compiles run
|
|
52
|
+
// the native lane (host clang, no cross env). Different triples run the
|
|
53
|
+
// compiler's zig-cc lane: Linux and Windows GNU targets build from any
|
|
54
|
+
// desktop host (the compiler cross-compiles its runtime and localizes ELF and
|
|
55
|
+
// COFF objects itself); macOS targets build on a macOS host only, where Apple
|
|
56
|
+
// linking rides the host toolchain's SDK.
|
|
57
|
+
const platformOs = (platform) => (platform.split("-")[1] ?? "").split(".")[0];
|
|
58
|
+
const platformArch = (platform) => platform.split("-")[0] ?? "";
|
|
59
|
+
const platformAbi = (platform) => (platform.split("-")[2] ?? "").split(".")[0];
|
|
60
|
+
const hostArch = platformArch(args["host-platform"]);
|
|
61
|
+
const hostOs = platformOs(args["host-platform"]);
|
|
62
|
+
const hostAbi = platformAbi(args["host-platform"]);
|
|
63
|
+
const targetOs = platformOs(args["target-platform"]);
|
|
64
|
+
const targetArch = platformArch(args["target-platform"]);
|
|
65
|
+
const targetAbi = platformAbi(args["target-platform"]);
|
|
66
|
+
// The two mobile families, from the build graph's Zig triple spelling:
|
|
67
|
+
// `aarch64-ios[-simulator]` and `aarch64-linux-android`. The pinned compiler
|
|
68
|
+
// admits exactly aarch64 for both, library archives only.
|
|
69
|
+
const iosTarget = targetOs === "ios";
|
|
70
|
+
const androidTarget = targetOs === "linux" && targetAbi === "android";
|
|
71
|
+
const mobileTarget = iosTarget || androidTarget;
|
|
72
|
+
// The canonical SCRIPTC_TARGET spelling for a mobile compile: the compiler
|
|
73
|
+
// names iOS targets with the vendor (`aarch64-apple-ios[-simulator]`) and
|
|
74
|
+
// Android with the Zig triple itself.
|
|
75
|
+
const scriptcTarget = iosTarget
|
|
76
|
+
? `aarch64-apple-ios${targetAbi === "simulator" ? "-simulator" : ""}`
|
|
77
|
+
: args["target-platform"];
|
|
78
|
+
// Zig names a native Windows host with the GNU ABI by default, while a user
|
|
79
|
+
// may explicitly target MSVC on that same machine. That remains native
|
|
80
|
+
// compiler work; an exact native GNU triple does too.
|
|
81
|
+
const nativeWindows = hostOs === "windows" && targetOs === "windows" && hostArch === targetArch &&
|
|
82
|
+
(targetAbi === hostAbi || targetAbi === "msvc");
|
|
83
|
+
const cross = args["target-platform"] !== args["host-platform"] && !nativeWindows;
|
|
84
|
+
if (mobileTarget) {
|
|
85
|
+
// Mobile services are archive-only: there is no child process to spawn on
|
|
86
|
+
// iOS or Android, so the plain-scriptc executable lane refuses here.
|
|
87
|
+
if (args["out-exe"]) {
|
|
88
|
+
console.error(
|
|
89
|
+
`TypeScript services for ${args["target-platform"]} build library archives only: mobile apps cannot spawn a sibling service process. ` +
|
|
90
|
+
"Use the in-process carrier (--out-archive) — the mobile default.",
|
|
91
|
+
);
|
|
92
|
+
process.exit(2);
|
|
93
|
+
}
|
|
94
|
+
const desktopHost = ["macos", "linux", "windows"].includes(hostOs);
|
|
95
|
+
const admitted = desktopHost && targetArch === "aarch64" && (!iosTarget || hostOs === "macos");
|
|
96
|
+
if (!admitted) {
|
|
97
|
+
console.error(
|
|
98
|
+
iosTarget && hostOs !== "macos"
|
|
99
|
+
? `TypeScript services for an iOS target (${args["target-platform"]}) compile on a macOS build host only — the Apple SDK sysroot and Mach-O localization live there — but this build host is ${args["host-platform"]}. Build iOS service apps on a Mac.`
|
|
100
|
+
: `TypeScript services compile for the mobile targets the pinned compiler covers — aarch64 iOS/iOS-simulator (macOS host) and aarch64 Android — but this build pairs host ${args["host-platform"]} with target ${args["target-platform"]}.`,
|
|
101
|
+
);
|
|
102
|
+
process.exit(2);
|
|
103
|
+
}
|
|
104
|
+
} else if (cross) {
|
|
105
|
+
const desktopHost = ["macos", "linux", "windows"].includes(hostOs);
|
|
106
|
+
const admitted = desktopHost &&
|
|
107
|
+
(targetOs === "linux" ||
|
|
108
|
+
(targetOs === "windows" && targetAbi === "gnu") ||
|
|
109
|
+
(targetOs === "macos" && hostOs === "macos"));
|
|
110
|
+
if (!admitted) {
|
|
111
|
+
console.error(
|
|
112
|
+
targetOs === "windows" && targetAbi !== "gnu"
|
|
113
|
+
? `TypeScript services for a cross-target Windows build (${args["target-platform"]}) require the GNU ABI: Zig supplies that target's CRT and system libraries, while an MSVC target needs a native Windows toolchain. Build ${targetArch}-windows-msvc on a matching Windows host, or cross-compile as "${targetArch}-windows-gnu".`
|
|
114
|
+
: targetOs === "macos"
|
|
115
|
+
? `TypeScript services for a macOS target (${args["target-platform"]}) compile on a macOS build host only — Apple linking needs the host toolchain's SDK — but this build host is ${args["host-platform"]}. Build macOS service apps on a Mac.`
|
|
116
|
+
: `TypeScript services compile for desktop targets the pinned compiler covers — Linux and Windows GNU from a macOS/Linux/Windows build host, macOS from a macOS host — but this build pairs host ${args["host-platform"]} with target ${args["target-platform"]}.`,
|
|
117
|
+
);
|
|
118
|
+
process.exit(2);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Runtime localization is narrower than executable cross-compilation. The
|
|
123
|
+
// in-process archive's object merger supports native Linux through host
|
|
124
|
+
// binutils, cross-ELF for x86_64/aarch64 (Android included), COFF for
|
|
125
|
+
// x86_64, and Mach-O — iOS device and simulator included — on a macOS host.
|
|
126
|
+
// Keep this preflight in lockstep with the pinned compiler's compileLibrary guard
|
|
127
|
+
// so `in_process` refusals teach before compiler work.
|
|
128
|
+
if (args["out-archive"]) {
|
|
129
|
+
const archiveSupported =
|
|
130
|
+
(targetOs === "linux" && (!cross || ["x86_64", "aarch64"].includes(targetArch))) ||
|
|
131
|
+
(targetOs === "windows" && targetArch === "x86_64" && (!cross || targetAbi === "gnu")) ||
|
|
132
|
+
((targetOs === "macos" || iosTarget) && hostOs === "macos");
|
|
133
|
+
if (!archiveSupported) {
|
|
134
|
+
console.error(
|
|
135
|
+
`TypeScript in-process services cannot build a runtime-localized archive for ${args["target-platform"]} from ${args["host-platform"]}. ` +
|
|
136
|
+
"The pinned compiler supports native Linux, cross-Linux x86_64/aarch64 (Android included), native Windows x86_64, cross-Windows x86_64 GNU, and macOS/iOS targets on a macOS host. " +
|
|
137
|
+
"Use the child carrier (the desktop default), or choose a supported in-process target.",
|
|
138
|
+
);
|
|
139
|
+
process.exit(2);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
for (const key of ["stage", "manifest", "contract", "out-exe", "out-archive", "compiler-js", "zig-exe", "android-ndk"]) {
|
|
143
|
+
if (args[key]) args[key] = path.resolve(args[key]);
|
|
144
|
+
}
|
|
145
|
+
const argv0 = args.compiler
|
|
146
|
+
? (fs.existsSync(args.compiler) ? [args.compiler] : args.compiler.split(/\s+/))
|
|
147
|
+
: [process.execPath, args["compiler-js"]];
|
|
148
|
+
|
|
149
|
+
// Cross compiles hand the compiler its zig-cc lane: the target triple as
|
|
150
|
+
// SCRIPTC_TARGET (the compiler's own mobile spellings for iOS/Android), and
|
|
151
|
+
// (when the build graph supplied its own zig) that zig's directory at the
|
|
152
|
+
// front of PATH so the lane never depends on an ambient install. An Android
|
|
153
|
+
// build threads the NDK location the same way (--android-ndk becomes
|
|
154
|
+
// ANDROID_NDK_ROOT for the compiler's sysroot discovery). Native compiles
|
|
155
|
+
// keep the process environment untouched.
|
|
156
|
+
const compileEnv = cross
|
|
157
|
+
? {
|
|
158
|
+
...process.env,
|
|
159
|
+
SCRIPTC_CC: "zigcc",
|
|
160
|
+
SCRIPTC_TARGET: scriptcTarget,
|
|
161
|
+
...(args["zig-exe"]
|
|
162
|
+
? { PATH: `${path.dirname(args["zig-exe"])}${path.delimiter}${process.env.PATH ?? ""}` }
|
|
163
|
+
: {}),
|
|
164
|
+
...(args["android-ndk"] && androidTarget ? { ANDROID_NDK_ROOT: args["android-ndk"] } : {}),
|
|
165
|
+
}
|
|
166
|
+
: process.env;
|
|
167
|
+
|
|
168
|
+
function isNpmStaticRefusal(output) {
|
|
169
|
+
const percent = output.match(/compile statically\s+\d+\s+\((\d+)%\)/i);
|
|
170
|
+
return (percent !== null && Number(percent[1]) < 100)
|
|
171
|
+
|| /SC-NPM-STATIC|\bSC2013\b|\bSC4020\b|island fallback|cannot compile statically|static coverage[^\n]*dynamic/i.test(output);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function printNpmStaticTeaching() {
|
|
175
|
+
console.error(
|
|
176
|
+
"native check: this service package does not clear scriptc's static tier. " +
|
|
177
|
+
"Choose another exact vendored package, port/vendor a static-compatible implementation, or wait for compiler support; " +
|
|
178
|
+
"Native SDK never enables npm auto-fallback or --dynamic.",
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const manifest = JSON.parse(fs.readFileSync(args.manifest, "utf8"));
|
|
183
|
+
const pin = manifest.dependencies?.scriptc;
|
|
184
|
+
const contract = JSON.parse(fs.readFileSync(args.contract, "utf8"));
|
|
185
|
+
if (typeof pin !== "string" || !/^\d+\.\d+\.\d+$/.test(pin)) {
|
|
186
|
+
console.error("packages/core/package.json carries no exact scriptc pin");
|
|
187
|
+
process.exit(2);
|
|
188
|
+
}
|
|
189
|
+
if (contract.compiler_version !== pin) {
|
|
190
|
+
console.error(`services contract echoes scriptc ${contract.compiler_version}, but packages/core pins ${pin} — regenerate the contract with this SDK`);
|
|
191
|
+
process.exit(2);
|
|
192
|
+
}
|
|
193
|
+
const probe = spawnSync(argv0[0], [...argv0.slice(1), "-v"], { encoding: "utf8" });
|
|
194
|
+
const reported = (probe.stdout ?? "").trim();
|
|
195
|
+
if (probe.status !== 0 || reported !== pin) {
|
|
196
|
+
console.error(`service compiler reports ${reported || "no version"}, but the SDK pins ${pin}`);
|
|
197
|
+
process.exit(2);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const work = fs.mkdtempSync(path.join(os.tmpdir(), "native-external-services-"));
|
|
201
|
+
try {
|
|
202
|
+
fs.cpSync(args.stage, work, { recursive: true });
|
|
203
|
+
const staticPackages = Array.isArray(contract.packages) ? contract.packages.map((entry) => entry.name) : [];
|
|
204
|
+
if (staticPackages.length > 0) {
|
|
205
|
+
const coverage = spawnSync(argv0[0], [
|
|
206
|
+
...argv0.slice(1),
|
|
207
|
+
"coverage",
|
|
208
|
+
"service_host_main.ts",
|
|
209
|
+
"--npm-static",
|
|
210
|
+
staticPackages.join(","),
|
|
211
|
+
], { cwd: work, encoding: "utf8", env: compileEnv });
|
|
212
|
+
const coverageOutput = `${coverage.stdout ?? ""}${coverage.stderr ?? ""}`;
|
|
213
|
+
if (coverage.status !== 0 || isNpmStaticRefusal(coverageOutput)) {
|
|
214
|
+
if (coverage.stdout) process.stdout.write(coverage.stdout);
|
|
215
|
+
if (coverage.stderr) process.stderr.write(coverage.stderr);
|
|
216
|
+
if (isNpmStaticRefusal(coverageOutput)) printNpmStaticTeaching();
|
|
217
|
+
process.exit(coverage.status && coverage.status !== 0 ? coverage.status : 1);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
if (args["out-exe"]) {
|
|
221
|
+
const built = path.join(work, targetOs === "windows" ? "service-host.exe" : "service-host");
|
|
222
|
+
const compileArgs = [...argv0.slice(1), "build", "service_host_main.ts", "-o", built];
|
|
223
|
+
if (staticPackages.length > 0) compileArgs.push("--npm-static", staticPackages.join(","));
|
|
224
|
+
const result = spawnSync(argv0[0], compileArgs, {
|
|
225
|
+
cwd: work,
|
|
226
|
+
encoding: "utf8",
|
|
227
|
+
env: compileEnv,
|
|
228
|
+
});
|
|
229
|
+
if (result.stdout) process.stdout.write(result.stdout);
|
|
230
|
+
if (result.stderr) process.stderr.write(result.stderr);
|
|
231
|
+
if (result.status !== 0) {
|
|
232
|
+
if (isNpmStaticRefusal(`${result.stdout ?? ""}${result.stderr ?? ""}`)) printNpmStaticTeaching();
|
|
233
|
+
process.exit(result.status ?? 1);
|
|
234
|
+
}
|
|
235
|
+
if (!fs.existsSync(built)) {
|
|
236
|
+
console.error("the external service compile produced no executable");
|
|
237
|
+
process.exit(1);
|
|
238
|
+
}
|
|
239
|
+
fs.mkdirSync(path.dirname(args["out-exe"]), { recursive: true });
|
|
240
|
+
fs.copyFileSync(built, args["out-exe"]);
|
|
241
|
+
fs.chmodSync(args["out-exe"], 0o755);
|
|
242
|
+
}
|
|
243
|
+
if (args["out-archive"]) {
|
|
244
|
+
// The in-process carrier's library archive: the staged profile pins
|
|
245
|
+
// the entry (service_inproc_main.ts), the fixed symbol family, runtime
|
|
246
|
+
// localization, and thread-instanced state. Library mode takes no
|
|
247
|
+
// --npm-static: bare npm specifiers are static-or-refuse there, and
|
|
248
|
+
// the vendored packages already staged under node_modules resolve
|
|
249
|
+
// automatically.
|
|
250
|
+
if (!fs.existsSync(path.join(work, "service_profile.json")) || !fs.existsSync(path.join(work, "service_inproc_main.ts"))) {
|
|
251
|
+
console.error("the service stage carries no in-process entry/profile (stage with --inproc-main and --inproc-profile)");
|
|
252
|
+
process.exit(2);
|
|
253
|
+
}
|
|
254
|
+
const builtArchive = path.join(work, "native_sdk_services");
|
|
255
|
+
const result = spawnSync(argv0[0], [
|
|
256
|
+
...argv0.slice(1),
|
|
257
|
+
"build",
|
|
258
|
+
"--lib",
|
|
259
|
+
"--profile",
|
|
260
|
+
"service_profile.json",
|
|
261
|
+
"-o",
|
|
262
|
+
builtArchive,
|
|
263
|
+
], { cwd: work, encoding: "utf8", env: compileEnv });
|
|
264
|
+
if (result.stdout) process.stdout.write(result.stdout);
|
|
265
|
+
if (result.stderr) process.stderr.write(result.stderr);
|
|
266
|
+
if (result.status !== 0) {
|
|
267
|
+
if (isNpmStaticRefusal(`${result.stdout ?? ""}${result.stderr ?? ""}`)) printNpmStaticTeaching();
|
|
268
|
+
process.exit(result.status ?? 1);
|
|
269
|
+
}
|
|
270
|
+
const producedArchive = [`${builtArchive}.lib.a`, builtArchive].find((name) => fs.existsSync(name));
|
|
271
|
+
if (!producedArchive) {
|
|
272
|
+
console.error("the external service compile produced no archive (expected native_sdk_services.lib.a)");
|
|
273
|
+
process.exit(1);
|
|
274
|
+
}
|
|
275
|
+
fs.mkdirSync(path.dirname(args["out-archive"]), { recursive: true });
|
|
276
|
+
fs.copyFileSync(producedArchive, args["out-archive"]);
|
|
277
|
+
}
|
|
278
|
+
} finally {
|
|
279
|
+
fs.rmSync(work, { recursive: true, force: true });
|
|
280
|
+
}
|
|
@@ -54,7 +54,10 @@ function tsFilesUnder(dir) {
|
|
|
54
54
|
const walk = (sub) => {
|
|
55
55
|
for (const entry of fs.readdirSync(path.join(dir, sub), { withFileTypes: true })) {
|
|
56
56
|
const rel = sub === "" ? entry.name : `${sub}/${entry.name}`;
|
|
57
|
-
|
|
57
|
+
// The service class has its own compile lane. Keeping it out of the
|
|
58
|
+
// core scratch tree also prevents vendored package declarations from
|
|
59
|
+
// participating in the core surface's mechanical alias dedupe.
|
|
60
|
+
if (entry.isDirectory() && rel !== "services") walk(rel);
|
|
58
61
|
else if (entry.isFile() && rel.endsWith(".ts") && !rel.endsWith(".d.ts")) out.push(rel);
|
|
59
62
|
}
|
|
60
63
|
};
|
|
@@ -73,6 +76,7 @@ function resolveSpecifiers(text, rel) {
|
|
|
73
76
|
.replaceAll('"@native-sdk/core/text"', `"${toSdk("text.ts")}"`)
|
|
74
77
|
.replaceAll('"@native-sdk/core/events"', `"${toSdk("events.ts")}"`)
|
|
75
78
|
.replaceAll('"@native-sdk/core"', `"${toSdk("core.ts")}"`)
|
|
79
|
+
.replaceAll('"@native-sdk/services"', `"${path.posix.relative(path.posix.dirname(rel), "services.gen.ts").startsWith(".") ? path.posix.relative(path.posix.dirname(rel), "services.gen.ts") : `./${path.posix.relative(path.posix.dirname(rel), "services.gen.ts")}`}"`)
|
|
76
80
|
.replace(/readonly ([A-Za-z_][A-Za-z0-9_]*(?:<[A-Za-z_, ]*>)?)\[\]/g, "$1[]")
|
|
77
81
|
.replace(/(?<![A-Za-z0-9_])Bytes(?![A-Za-z0-9_])/g, "Uint8Array")
|
|
78
82
|
.split("\n")
|
|
@@ -146,3 +150,7 @@ fs.writeFileSync(path.join(args.out, "sdk", "core.ts"), dedupeAliases(fs.readFil
|
|
|
146
150
|
// the profile's entry spelling expects.
|
|
147
151
|
fs.copyFileSync(args.facade, path.join(args.out, "core_facade.ts"));
|
|
148
152
|
fs.copyFileSync(args.profile, path.join(args.out, "profile.json"));
|
|
153
|
+
if (args["services-client"]) {
|
|
154
|
+
const client = resolveSpecifiers(fs.readFileSync(args["services-client"], "utf8"), "services.gen.ts");
|
|
155
|
+
fs.writeFileSync(path.join(args.out, "services.gen.ts"), client);
|
|
156
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Stage the typed service executable: ordinary service TypeScript plus
|
|
3
|
+
// corewire's generated service_host_main.ts. One narrow mechanical lowering
|
|
4
|
+
// is required by the pinned compiler: an escaping `throw { kind, message }` becomes
|
|
5
|
+
// an Error subclass carrying kind in `.name`, the object form its reachable
|
|
6
|
+
// static catch tier cannot inspect yet. NS1067 rejects locally caught tagged
|
|
7
|
+
// records; shared core-class modules receive the same transform in this
|
|
8
|
+
// scratch tree so a throw crossing the service boundary keeps its pair.
|
|
9
|
+
|
|
10
|
+
import fs from "node:fs";
|
|
11
|
+
import path from "node:path";
|
|
12
|
+
import crypto from "node:crypto";
|
|
13
|
+
import ts from "@typescript/old";
|
|
14
|
+
|
|
15
|
+
function argsOf(argv) {
|
|
16
|
+
const args = {};
|
|
17
|
+
for (let i = 2; i < argv.length; i += 2) args[argv[i]?.replace(/^--/, "")] = argv[i + 1];
|
|
18
|
+
for (const name of ["src", "host-main", "out"]) {
|
|
19
|
+
if (!args[name]) {
|
|
20
|
+
console.error("usage: stage_external_services.mjs --src <app src> --host-main <generated.ts> --out <stage>");
|
|
21
|
+
process.exit(2);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return args;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function unwrapExpression(expression) {
|
|
28
|
+
while (ts.isAsExpression(expression) || ts.isTypeAssertionExpression(expression) || ts.isParenthesizedExpression(expression)) {
|
|
29
|
+
expression = expression.expression;
|
|
30
|
+
}
|
|
31
|
+
return expression;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function taggedThrowParts(expression) {
|
|
35
|
+
const value = unwrapExpression(expression);
|
|
36
|
+
if (!ts.isObjectLiteralExpression(value) || value.properties.length !== 2) return null;
|
|
37
|
+
const [kind, message] = value.properties;
|
|
38
|
+
if (!ts.isPropertyAssignment(kind) || !ts.isIdentifier(kind.name) || kind.name.text !== "kind" || !ts.isStringLiteral(kind.initializer)) return null;
|
|
39
|
+
if (!ts.isPropertyAssignment(message) || !ts.isIdentifier(message.name) || message.name.text !== "message") return null;
|
|
40
|
+
return { kind: kind.initializer, message: message.initializer };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function lowerTaggedThrows(text, fileName) {
|
|
44
|
+
const source = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
45
|
+
const replacements = [];
|
|
46
|
+
const visit = (node) => {
|
|
47
|
+
if (ts.isThrowStatement(node) && node.expression) {
|
|
48
|
+
const parts = taggedThrowParts(node.expression);
|
|
49
|
+
if (parts) {
|
|
50
|
+
replacements.push({
|
|
51
|
+
start: node.expression.getStart(source),
|
|
52
|
+
end: node.expression.end,
|
|
53
|
+
text: `new __nativeSdkTaggedError(${parts.kind.getText(source)}, ${parts.message.getText(source)})`,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
ts.forEachChild(node, visit);
|
|
58
|
+
};
|
|
59
|
+
visit(source);
|
|
60
|
+
if (replacements.length === 0) return text;
|
|
61
|
+
|
|
62
|
+
let insertAt = 0;
|
|
63
|
+
for (const statement of source.statements) {
|
|
64
|
+
if (!ts.isImportDeclaration(statement)) break;
|
|
65
|
+
insertAt = statement.end;
|
|
66
|
+
}
|
|
67
|
+
replacements.push({
|
|
68
|
+
start: insertAt,
|
|
69
|
+
end: insertAt,
|
|
70
|
+
text: `${insertAt === 0 ? "" : "\n"}\nclass __nativeSdkTaggedError extends Error {\n readonly kind: string;\n\n constructor(kind: string, message: unknown) {\n super(String(message));\n this.kind = kind;\n this.name = kind;\n }\n}\n`,
|
|
71
|
+
});
|
|
72
|
+
replacements.sort((a, b) => b.start - a.start);
|
|
73
|
+
for (const replacement of replacements) {
|
|
74
|
+
text = text.slice(0, replacement.start) + replacement.text + text.slice(replacement.end);
|
|
75
|
+
}
|
|
76
|
+
return text;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function serviceCapabilityImport(text, rel) {
|
|
80
|
+
const target = path.posix.relative(path.posix.dirname(rel), "__native_sdk_service_types.ts");
|
|
81
|
+
const specifier = target.startsWith(".") ? target : `./${target}`;
|
|
82
|
+
return text.replace(/(from\s+["'])@native-sdk\/core(["'])/g, `$1${specifier}$2`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function copyTsTree(from, to, sub = "") {
|
|
86
|
+
for (const entry of fs.readdirSync(path.join(from, sub), { withFileTypes: true })) {
|
|
87
|
+
const rel = sub ? `${sub}/${entry.name}` : entry.name;
|
|
88
|
+
if (entry.isDirectory() && rel !== "services/vendor") copyTsTree(from, to, rel);
|
|
89
|
+
else if (entry.isFile() && entry.name.endsWith(".ts") && !entry.name.endsWith(".d.ts")) {
|
|
90
|
+
const destination = path.join(to, rel);
|
|
91
|
+
fs.mkdirSync(path.dirname(destination), { recursive: true });
|
|
92
|
+
const source = fs.readFileSync(path.join(from, rel), "utf8");
|
|
93
|
+
fs.writeFileSync(destination, lowerTaggedThrows(serviceCapabilityImport(source, rel), rel));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function packagePath(root, name) {
|
|
99
|
+
if (!/^(?:@[a-z0-9._-]+\/)?[a-z0-9._-]+$/i.test(name)) {
|
|
100
|
+
throw new Error(`service package name ${JSON.stringify(name)} is not a safe npm package name`);
|
|
101
|
+
}
|
|
102
|
+
return path.join(root, ...name.split("/"));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function packageContentHash(root) {
|
|
106
|
+
const files = [];
|
|
107
|
+
const walk = (current, prefix = "") => {
|
|
108
|
+
for (const entry of fs.readdirSync(current, { withFileTypes: true })) {
|
|
109
|
+
const rel = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
110
|
+
const absolute = path.join(current, entry.name);
|
|
111
|
+
if (entry.isSymbolicLink()) throw new Error(`vendored package contains a symlink: ${rel}`);
|
|
112
|
+
if (entry.isDirectory()) walk(absolute, rel);
|
|
113
|
+
else if (entry.isFile()) files.push({ rel, absolute });
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
walk(root);
|
|
117
|
+
files.sort((a, b) => a.rel < b.rel ? -1 : a.rel > b.rel ? 1 : 0);
|
|
118
|
+
const hash = crypto.createHash("sha256");
|
|
119
|
+
hash.update("native-sdk.service-package.v1\0");
|
|
120
|
+
for (const file of files) {
|
|
121
|
+
const bytes = fs.readFileSync(file.absolute);
|
|
122
|
+
const size = Buffer.alloc(8);
|
|
123
|
+
size.writeBigUInt64LE(BigInt(bytes.length));
|
|
124
|
+
hash.update(file.rel);
|
|
125
|
+
hash.update("\0");
|
|
126
|
+
hash.update(size);
|
|
127
|
+
hash.update(bytes);
|
|
128
|
+
}
|
|
129
|
+
return hash.digest("hex");
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function stagePackages(src, out, contractPath) {
|
|
133
|
+
if (!contractPath) return;
|
|
134
|
+
const contract = JSON.parse(fs.readFileSync(contractPath, "utf8"));
|
|
135
|
+
const packages = contract.packages ?? [];
|
|
136
|
+
const vendorRoot = path.join(src, "services", "vendor");
|
|
137
|
+
for (const packageEntry of packages) {
|
|
138
|
+
const source = packagePath(vendorRoot, packageEntry.name);
|
|
139
|
+
if (!fs.statSync(source, { throwIfNoEntry: false })?.isDirectory()) {
|
|
140
|
+
throw new Error(`service package ${packageEntry.name}@${packageEntry.version} is not checked in at ${source}`);
|
|
141
|
+
}
|
|
142
|
+
const packageJson = JSON.parse(fs.readFileSync(path.join(source, "package.json"), "utf8"));
|
|
143
|
+
if (packageJson.name !== packageEntry.name || packageJson.version !== packageEntry.version) {
|
|
144
|
+
throw new Error(`vendored ${packageEntry.name} identifies itself as ${packageJson.name ?? "?"}@${packageJson.version ?? "?"}, expected ${packageEntry.name}@${packageEntry.version}`);
|
|
145
|
+
}
|
|
146
|
+
const actual = packageContentHash(source);
|
|
147
|
+
if (actual !== packageEntry.content_hash) {
|
|
148
|
+
throw new Error(`vendored ${packageEntry.name}@${packageEntry.version} hashes to ${actual}, expected ${packageEntry.content_hash}; re-run native vendor or restore the checked-in package bytes`);
|
|
149
|
+
}
|
|
150
|
+
const destination = packagePath(path.join(out, "node_modules"), packageEntry.name);
|
|
151
|
+
fs.mkdirSync(path.dirname(destination), { recursive: true });
|
|
152
|
+
fs.cpSync(source, destination, { recursive: true, errorOnExist: true, force: false });
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const args = argsOf(process.argv);
|
|
157
|
+
fs.rmSync(args.out, { recursive: true, force: true });
|
|
158
|
+
fs.mkdirSync(args.out, { recursive: true });
|
|
159
|
+
copyTsTree(args.src, args.out);
|
|
160
|
+
fs.writeFileSync(path.join(args.out, "__native_sdk_service_types.ts"), `
|
|
161
|
+
export interface ServiceCancellation {
|
|
162
|
+
readonly cancelled: () => boolean;
|
|
163
|
+
readonly throwIfCancelled: () => void;
|
|
164
|
+
}
|
|
165
|
+
`);
|
|
166
|
+
stagePackages(args.src, args.out, args.contract);
|
|
167
|
+
fs.copyFileSync(args["host-main"], path.join(args.out, "service_host_main.ts"));
|
|
168
|
+
// The in-process carrier's library entry and its compiler profile stage
|
|
169
|
+
// beside the child host main; the archive compile resolves the profile's
|
|
170
|
+
// entry relative to the profile file, so both land at the stage root.
|
|
171
|
+
if (args["inproc-main"]) fs.copyFileSync(args["inproc-main"], path.join(args.out, "service_inproc_main.ts"));
|
|
172
|
+
if (args["inproc-profile"]) fs.copyFileSync(args["inproc-profile"], path.join(args.out, "service_profile.json"));
|