@native-sdk/cli 0.8.4 → 0.9.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/build/app.zig +859 -62
- package/build.zig +543 -13
- package/build.zig.zon +1 -0
- package/native-sdk.d.ts +2 -0
- package/package.json +12 -11
- package/packages/core/compile-surface/core.ts +365 -2
- 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/npm_static_spike.mjs +64 -0
- package/packages/core/scripts/run_external_core_compiler.mjs +77 -6
- package/packages/core/scripts/run_external_service_compiler.mjs +240 -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 +149 -0
- package/packages/core/sdk/core.ts +529 -12
- package/packages/core/sdk/events.d.ts +36 -0
- package/packages/core/sdk/events.ts +57 -0
- package/packages/core/src/checker.ts +468 -15
- package/packages/core/src/cli.ts +143 -3
- package/packages/core/src/contract.ts +26 -5
- package/packages/core/src/devhost.ts +1847 -16
- package/packages/core/src/devhost_journal.mjs +181 -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 +82 -16
- package/packages/core/src/frontend.ts +204 -34
- 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/core/references/project-anatomy.md +1 -1
- package/skill-data/native-ui/SKILL.md +47 -0
- package/skill-data/ts-core/SKILL.md +80 -31
- package/skill-data/ts-services/SKILL.md +126 -0
- package/skill-data/ts-services/references/service-surface.md +603 -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 +104 -4
- package/src/app_runner/ts_core_main.zig +249 -8
- package/src/automation/snapshot.zig +23 -5
- package/src/embed/app_exports.zig +11 -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 +7 -0
- package/src/embed/tests.zig +213 -0
- package/src/embed/types.zig +51 -0
- package/src/embed/ui_host.zig +119 -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 +1 -1
- package/src/platform/linux/gtk_host.c +89 -17
- package/src/platform/linux/gtk_host.h +3 -0
- package/src/platform/linux/root.zig +40 -18
- package/src/platform/macos/appkit_host.h +16 -3
- package/src/platform/macos/appkit_host.m +536 -36
- package/src/platform/macos/cef_host.mm +248 -36
- package/src/platform/macos/root.zig +244 -34
- package/src/platform/null_platform.zig +223 -19
- package/src/platform/null_platform_tests.zig +74 -0
- package/src/platform/root.zig +9 -0
- package/src/platform/types.zig +179 -15
- package/src/platform/windows/root.zig +187 -21
- package/src/platform/windows/webview2_host.cpp +293 -46
- 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 +46 -0
- package/src/runtime/api.zig +11 -0
- package/src/runtime/automation_snapshot.zig +4 -0
- package/src/runtime/bridge_permission_tests.zig +23 -2
- package/src/runtime/bridge_responses.zig +3 -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 +133 -1
- package/src/runtime/credentials_store.zig +115 -0
- package/src/runtime/effects.zig +2295 -113
- package/src/runtime/effects_credentials_tests.zig +332 -0
- package/src/runtime/effects_host_tests.zig +208 -1
- package/src/runtime/effects_relational_tests.zig +314 -0
- package/src/runtime/effects_store_tests.zig +211 -0
- package/src/runtime/flow.zig +14 -1
- package/src/runtime/gpu_surface_events.zig +6 -1
- package/src/runtime/native_command_event_tests.zig +42 -0
- package/src/runtime/persist_store.zig +666 -0
- package/src/runtime/record_store.zig +788 -0
- package/src/runtime/relational_store.zig +854 -0
- package/src/runtime/root.zig +49 -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 +1 -1
- package/src/runtime/session_journal.zig +102 -0
- package/src/runtime/session_record.zig +177 -0
- package/src/runtime/session_replay.zig +109 -4
- 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 +6 -0
- package/src/runtime/system_services.zig +49 -10
- package/src/runtime/tests.zig +4 -0
- package/src/runtime/ts_core_host.zig +788 -86
- package/src/runtime/ts_core_host_tests.zig +231 -11
- package/src/runtime/ts_ui_app.zig +499 -1
- package/src/runtime/ui_app.zig +142 -16
- package/src/runtime/ui_app_tests.zig +155 -5
- package/src/runtime/validation.zig +22 -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 +161 -4
- package/src/tooling/ts_core.zig +283 -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 +378 -23
- 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
|
@@ -0,0 +1,240 @@
|
|
|
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
|
+
|
|
13
|
+
import { spawnSync } from "node:child_process";
|
|
14
|
+
import fs from "node:fs";
|
|
15
|
+
import os from "node:os";
|
|
16
|
+
import path from "node:path";
|
|
17
|
+
|
|
18
|
+
function parseArgs(argv) {
|
|
19
|
+
const args = {};
|
|
20
|
+
for (let i = 2; i < argv.length; i += 2) {
|
|
21
|
+
const key = argv[i];
|
|
22
|
+
const value = argv[i + 1];
|
|
23
|
+
if (!key?.startsWith("--") || value === undefined) {
|
|
24
|
+
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>] (--compiler <cmd> | --compiler-js <main.js>)");
|
|
25
|
+
process.exit(2);
|
|
26
|
+
}
|
|
27
|
+
args[key.slice(2)] = value;
|
|
28
|
+
}
|
|
29
|
+
for (const required of ["stage", "manifest", "contract", "host-platform", "target-platform"]) {
|
|
30
|
+
if (!args[required]) {
|
|
31
|
+
console.error(`run_external_service_compiler.mjs: missing --${required}`);
|
|
32
|
+
process.exit(2);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (!args["out-exe"] && !args["out-archive"]) {
|
|
36
|
+
console.error("run_external_service_compiler.mjs: supply --out-exe, --out-archive, or both");
|
|
37
|
+
process.exit(2);
|
|
38
|
+
}
|
|
39
|
+
if (!args.compiler && !args["compiler-js"]) {
|
|
40
|
+
console.error("run_external_service_compiler.mjs: supply --compiler or --compiler-js");
|
|
41
|
+
process.exit(2);
|
|
42
|
+
}
|
|
43
|
+
return args;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const args = parseArgs(process.argv);
|
|
47
|
+
|
|
48
|
+
// The pairing matrix the pinned compiler covers. Same-triple compiles run
|
|
49
|
+
// the native lane (host clang, no cross env). Different triples run the
|
|
50
|
+
// compiler's zig-cc lane: Linux and Windows GNU targets build from any
|
|
51
|
+
// desktop host (the compiler cross-compiles its runtime and localizes ELF and
|
|
52
|
+
// COFF objects itself); macOS targets build on a macOS host only, where Apple
|
|
53
|
+
// linking rides the host toolchain's SDK.
|
|
54
|
+
const platformOs = (platform) => (platform.split("-")[1] ?? "").split(".")[0];
|
|
55
|
+
const platformArch = (platform) => platform.split("-")[0] ?? "";
|
|
56
|
+
const platformAbi = (platform) => (platform.split("-")[2] ?? "").split(".")[0];
|
|
57
|
+
const hostArch = platformArch(args["host-platform"]);
|
|
58
|
+
const hostOs = platformOs(args["host-platform"]);
|
|
59
|
+
const hostAbi = platformAbi(args["host-platform"]);
|
|
60
|
+
const targetOs = platformOs(args["target-platform"]);
|
|
61
|
+
const targetArch = platformArch(args["target-platform"]);
|
|
62
|
+
const targetAbi = platformAbi(args["target-platform"]);
|
|
63
|
+
// Zig names a native Windows host with the GNU ABI by default, while a user
|
|
64
|
+
// may explicitly target MSVC on that same machine. That remains native
|
|
65
|
+
// compiler work; an exact native GNU triple does too.
|
|
66
|
+
const nativeWindows = hostOs === "windows" && targetOs === "windows" && hostArch === targetArch &&
|
|
67
|
+
(targetAbi === hostAbi || targetAbi === "msvc");
|
|
68
|
+
const cross = args["target-platform"] !== args["host-platform"] && !nativeWindows;
|
|
69
|
+
if (cross) {
|
|
70
|
+
const desktopHost = ["macos", "linux", "windows"].includes(hostOs);
|
|
71
|
+
const admitted = desktopHost &&
|
|
72
|
+
(targetOs === "linux" ||
|
|
73
|
+
(targetOs === "windows" && targetAbi === "gnu") ||
|
|
74
|
+
(targetOs === "macos" && hostOs === "macos"));
|
|
75
|
+
if (!admitted) {
|
|
76
|
+
console.error(
|
|
77
|
+
targetOs === "windows" && targetAbi !== "gnu"
|
|
78
|
+
? `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".`
|
|
79
|
+
: targetOs === "macos"
|
|
80
|
+
? `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.`
|
|
81
|
+
: `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"]}.`,
|
|
82
|
+
);
|
|
83
|
+
process.exit(2);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Runtime localization is narrower than executable cross-compilation. The
|
|
88
|
+
// in-process archive's object merger supports native Linux through host
|
|
89
|
+
// binutils, cross-ELF for x86_64/aarch64, COFF for x86_64, and Mach-O on a
|
|
90
|
+
// macOS host. Keep this preflight in lockstep with ScriptC 0.0.28's
|
|
91
|
+
// compileLibrary guard so `in_process` refusals teach before compiler work.
|
|
92
|
+
if (args["out-archive"]) {
|
|
93
|
+
const archiveSupported =
|
|
94
|
+
(targetOs === "linux" && (!cross || ["x86_64", "aarch64"].includes(targetArch))) ||
|
|
95
|
+
(targetOs === "windows" && targetArch === "x86_64" && (!cross || targetAbi === "gnu")) ||
|
|
96
|
+
(targetOs === "macos" && hostOs === "macos");
|
|
97
|
+
if (!archiveSupported) {
|
|
98
|
+
console.error(
|
|
99
|
+
`TypeScript in-process services cannot build a runtime-localized archive for ${args["target-platform"]} from ${args["host-platform"]}. ` +
|
|
100
|
+
"The pinned compiler supports native Linux, cross-Linux x86_64/aarch64, native Windows x86_64, cross-Windows x86_64 GNU, and macOS targets on a macOS host. " +
|
|
101
|
+
"Use the child carrier (the default), or choose a supported in-process target.",
|
|
102
|
+
);
|
|
103
|
+
process.exit(2);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
for (const key of ["stage", "manifest", "contract", "out-exe", "out-archive", "compiler-js", "zig-exe"]) {
|
|
107
|
+
if (args[key]) args[key] = path.resolve(args[key]);
|
|
108
|
+
}
|
|
109
|
+
const argv0 = args.compiler
|
|
110
|
+
? (fs.existsSync(args.compiler) ? [args.compiler] : args.compiler.split(/\s+/))
|
|
111
|
+
: [process.execPath, args["compiler-js"]];
|
|
112
|
+
|
|
113
|
+
// Cross compiles hand the compiler its zig-cc lane: the target triple as
|
|
114
|
+
// SCRIPTC_TARGET, and (when the build graph supplied its own zig) that
|
|
115
|
+
// zig's directory at the front of PATH so the lane never depends on an
|
|
116
|
+
// ambient install. Native compiles keep the process environment untouched.
|
|
117
|
+
const compileEnv = cross
|
|
118
|
+
? {
|
|
119
|
+
...process.env,
|
|
120
|
+
SCRIPTC_CC: "zigcc",
|
|
121
|
+
SCRIPTC_TARGET: args["target-platform"],
|
|
122
|
+
...(args["zig-exe"]
|
|
123
|
+
? { PATH: `${path.dirname(args["zig-exe"])}${path.delimiter}${process.env.PATH ?? ""}` }
|
|
124
|
+
: {}),
|
|
125
|
+
}
|
|
126
|
+
: process.env;
|
|
127
|
+
|
|
128
|
+
function isNpmStaticRefusal(output) {
|
|
129
|
+
const percent = output.match(/compile statically\s+\d+\s+\((\d+)%\)/i);
|
|
130
|
+
return (percent !== null && Number(percent[1]) < 100)
|
|
131
|
+
|| /SC-NPM-STATIC|\bSC2013\b|\bSC4020\b|island fallback|cannot compile statically|static coverage[^\n]*dynamic/i.test(output);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function printNpmStaticTeaching() {
|
|
135
|
+
console.error(
|
|
136
|
+
"native check: this service package does not clear scriptc's static tier. " +
|
|
137
|
+
"Choose another exact vendored package, port/vendor a static-compatible implementation, or wait for compiler support; " +
|
|
138
|
+
"Native SDK never enables npm auto-fallback or --dynamic.",
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const manifest = JSON.parse(fs.readFileSync(args.manifest, "utf8"));
|
|
143
|
+
const pin = manifest.dependencies?.scriptc;
|
|
144
|
+
const contract = JSON.parse(fs.readFileSync(args.contract, "utf8"));
|
|
145
|
+
if (typeof pin !== "string" || !/^\d+\.\d+\.\d+$/.test(pin)) {
|
|
146
|
+
console.error("packages/core/package.json carries no exact scriptc pin");
|
|
147
|
+
process.exit(2);
|
|
148
|
+
}
|
|
149
|
+
if (contract.compiler_version !== pin) {
|
|
150
|
+
console.error(`services contract echoes scriptc ${contract.compiler_version}, but packages/core pins ${pin} — regenerate the contract with this SDK`);
|
|
151
|
+
process.exit(2);
|
|
152
|
+
}
|
|
153
|
+
const probe = spawnSync(argv0[0], [...argv0.slice(1), "-v"], { encoding: "utf8" });
|
|
154
|
+
const reported = (probe.stdout ?? "").trim();
|
|
155
|
+
if (probe.status !== 0 || reported !== pin) {
|
|
156
|
+
console.error(`service compiler reports ${reported || "no version"}, but the SDK pins ${pin}`);
|
|
157
|
+
process.exit(2);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const work = fs.mkdtempSync(path.join(os.tmpdir(), "native-external-services-"));
|
|
161
|
+
try {
|
|
162
|
+
fs.cpSync(args.stage, work, { recursive: true });
|
|
163
|
+
const staticPackages = Array.isArray(contract.packages) ? contract.packages.map((entry) => entry.name) : [];
|
|
164
|
+
if (staticPackages.length > 0) {
|
|
165
|
+
const coverage = spawnSync(argv0[0], [
|
|
166
|
+
...argv0.slice(1),
|
|
167
|
+
"coverage",
|
|
168
|
+
"service_host_main.ts",
|
|
169
|
+
"--npm-static",
|
|
170
|
+
staticPackages.join(","),
|
|
171
|
+
], { cwd: work, encoding: "utf8", env: compileEnv });
|
|
172
|
+
const coverageOutput = `${coverage.stdout ?? ""}${coverage.stderr ?? ""}`;
|
|
173
|
+
if (coverage.status !== 0 || isNpmStaticRefusal(coverageOutput)) {
|
|
174
|
+
if (coverage.stdout) process.stdout.write(coverage.stdout);
|
|
175
|
+
if (coverage.stderr) process.stderr.write(coverage.stderr);
|
|
176
|
+
if (isNpmStaticRefusal(coverageOutput)) printNpmStaticTeaching();
|
|
177
|
+
process.exit(coverage.status && coverage.status !== 0 ? coverage.status : 1);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (args["out-exe"]) {
|
|
181
|
+
const built = path.join(work, targetOs === "windows" ? "service-host.exe" : "service-host");
|
|
182
|
+
const compileArgs = [...argv0.slice(1), "build", "service_host_main.ts", "-o", built];
|
|
183
|
+
if (staticPackages.length > 0) compileArgs.push("--npm-static", staticPackages.join(","));
|
|
184
|
+
const result = spawnSync(argv0[0], compileArgs, {
|
|
185
|
+
cwd: work,
|
|
186
|
+
encoding: "utf8",
|
|
187
|
+
env: compileEnv,
|
|
188
|
+
});
|
|
189
|
+
if (result.stdout) process.stdout.write(result.stdout);
|
|
190
|
+
if (result.stderr) process.stderr.write(result.stderr);
|
|
191
|
+
if (result.status !== 0) {
|
|
192
|
+
if (isNpmStaticRefusal(`${result.stdout ?? ""}${result.stderr ?? ""}`)) printNpmStaticTeaching();
|
|
193
|
+
process.exit(result.status ?? 1);
|
|
194
|
+
}
|
|
195
|
+
if (!fs.existsSync(built)) {
|
|
196
|
+
console.error("the external service compile produced no executable");
|
|
197
|
+
process.exit(1);
|
|
198
|
+
}
|
|
199
|
+
fs.mkdirSync(path.dirname(args["out-exe"]), { recursive: true });
|
|
200
|
+
fs.copyFileSync(built, args["out-exe"]);
|
|
201
|
+
fs.chmodSync(args["out-exe"], 0o755);
|
|
202
|
+
}
|
|
203
|
+
if (args["out-archive"]) {
|
|
204
|
+
// The in-process carrier's library archive: the staged profile pins
|
|
205
|
+
// the entry (service_inproc_main.ts), the fixed symbol family, runtime
|
|
206
|
+
// localization, and thread-instanced state. Library mode takes no
|
|
207
|
+
// --npm-static: bare npm specifiers are static-or-refuse there, and
|
|
208
|
+
// the vendored packages already staged under node_modules resolve
|
|
209
|
+
// automatically.
|
|
210
|
+
if (!fs.existsSync(path.join(work, "service_profile.json")) || !fs.existsSync(path.join(work, "service_inproc_main.ts"))) {
|
|
211
|
+
console.error("the service stage carries no in-process entry/profile (stage with --inproc-main and --inproc-profile)");
|
|
212
|
+
process.exit(2);
|
|
213
|
+
}
|
|
214
|
+
const builtArchive = path.join(work, "native_sdk_services");
|
|
215
|
+
const result = spawnSync(argv0[0], [
|
|
216
|
+
...argv0.slice(1),
|
|
217
|
+
"build",
|
|
218
|
+
"--lib",
|
|
219
|
+
"--profile",
|
|
220
|
+
"service_profile.json",
|
|
221
|
+
"-o",
|
|
222
|
+
builtArchive,
|
|
223
|
+
], { cwd: work, encoding: "utf8", env: compileEnv });
|
|
224
|
+
if (result.stdout) process.stdout.write(result.stdout);
|
|
225
|
+
if (result.stderr) process.stderr.write(result.stderr);
|
|
226
|
+
if (result.status !== 0) {
|
|
227
|
+
if (isNpmStaticRefusal(`${result.stdout ?? ""}${result.stderr ?? ""}`)) printNpmStaticTeaching();
|
|
228
|
+
process.exit(result.status ?? 1);
|
|
229
|
+
}
|
|
230
|
+
const producedArchive = [`${builtArchive}.lib.a`, builtArchive].find((name) => fs.existsSync(name));
|
|
231
|
+
if (!producedArchive) {
|
|
232
|
+
console.error("the external service compile produced no archive (expected native_sdk_services.lib.a)");
|
|
233
|
+
process.exit(1);
|
|
234
|
+
}
|
|
235
|
+
fs.mkdirSync(path.dirname(args["out-archive"]), { recursive: true });
|
|
236
|
+
fs.copyFileSync(producedArchive, args["out-archive"]);
|
|
237
|
+
}
|
|
238
|
+
} finally {
|
|
239
|
+
fs.rmSync(work, { recursive: true, force: true });
|
|
240
|
+
}
|
|
@@ -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 scriptc 0.0.28: 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"));
|