@syntropika/tembra-sdk 0.2.2 → 0.3.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.
Files changed (241) hide show
  1. package/README.md +43 -17
  2. package/dist/authoring/types.d.ts +64 -3
  3. package/dist/authoring/wit.d.ts +6 -0
  4. package/dist/bridge/commands.d.ts +1190 -0
  5. package/dist/bridge/identifiers.d.ts +2 -0
  6. package/dist/bridge/kernel.d.ts +18 -107
  7. package/dist/bridge/protocol.d.ts +3 -138
  8. package/dist/browser.d.ts +14 -1
  9. package/dist/browser.js +141 -3332
  10. package/dist/chunks/browser-b8jamxw2.js +3399 -0
  11. package/dist/chunks/execution-nsqc6nrt.js +1419 -0
  12. package/dist/chunks/index-01zk0xqx.js +160 -0
  13. package/dist/chunks/index-3fjdfdfd.js +152 -0
  14. package/dist/chunks/index-aaymjn7v.js +787 -0
  15. package/dist/chunks/index-c1w7z9hx.js +356 -0
  16. package/dist/chunks/index-h8t0n42q.js +738 -0
  17. package/dist/chunks/index-hfds9mzp.js +308 -0
  18. package/dist/chunks/index-kr4x67wt.js +177 -0
  19. package/dist/chunks/index-kr6bnh34.js +117 -0
  20. package/dist/chunks/index-kyt9dj0a.js +1421 -0
  21. package/dist/chunks/index-n1wxzc9w.js +830 -0
  22. package/dist/chunks/index-pvac426d.js +74 -0
  23. package/dist/chunks/index-smyds5ce.js +3234 -0
  24. package/dist/chunks/index-sse8aapb.js +121 -0
  25. package/dist/chunks/index-v6heph77.js +643 -0
  26. package/dist/chunks/index-xrdvnv37.js +3448 -0
  27. package/dist/chunks/index-za4mg94e.js +26 -0
  28. package/dist/chunks/index-zkqmnjj8.js +167 -0
  29. package/dist/coordinator.js +9 -500
  30. package/dist/error.d.ts +2 -0
  31. package/dist/errors.d.ts +3 -0
  32. package/dist/guest.d.ts +173 -0
  33. package/dist/guest.js +26 -0
  34. package/dist/host.d.ts +36 -30
  35. package/dist/host.js +37 -3310
  36. package/dist/http/bytes.d.ts +8 -0
  37. package/dist/http/events.d.ts +7 -0
  38. package/dist/http.d.ts +45 -0
  39. package/dist/http.js +291 -0
  40. package/dist/index.d.ts +2 -0
  41. package/dist/index.js +49 -862
  42. package/dist/infrastructure/rpc/client.d.ts +2 -1
  43. package/dist/infrastructure/rpc/resources.d.ts +28 -0
  44. package/dist/interpreter/abi.d.ts +54 -0
  45. package/dist/interpreter/acquire.d.ts +18 -0
  46. package/dist/interpreter/browser-network.d.ts +52 -0
  47. package/dist/interpreter/browser-port.d.ts +55 -0
  48. package/dist/interpreter/browser.d.ts +79 -0
  49. package/dist/interpreter/cli.d.ts +5 -0
  50. package/dist/interpreter/codec.d.ts +42 -0
  51. package/dist/interpreter/component/execution.d.ts +42 -0
  52. package/dist/interpreter/component/host.d.ts +69 -0
  53. package/dist/interpreter/component/instance.d.ts +40 -0
  54. package/dist/interpreter/component/kernel.d.ts +120 -0
  55. package/dist/interpreter/component/names.d.ts +5 -0
  56. package/dist/interpreter/component/port.d.ts +85 -0
  57. package/dist/interpreter/component/prepared.d.ts +97 -0
  58. package/dist/interpreter/component/resources.d.ts +45 -0
  59. package/dist/interpreter/component/revision.d.ts +87 -0
  60. package/dist/interpreter/component/scope.d.ts +116 -0
  61. package/dist/interpreter/component/streams.d.ts +28 -0
  62. package/dist/interpreter/component/values.d.ts +79 -0
  63. package/dist/interpreter/component/wasi-filesystem.d.ts +21 -0
  64. package/dist/interpreter/component/wasi-host.d.ts +64 -0
  65. package/dist/interpreter/component/wasi-http.d.ts +33 -0
  66. package/dist/interpreter/component/wasi-sockets.d.ts +183 -0
  67. package/dist/interpreter/component/wasi.d.ts +41 -0
  68. package/dist/interpreter/core.d.ts +64 -0
  69. package/dist/interpreter/fetch-http-provider.d.ts +17 -0
  70. package/dist/interpreter/filesystem/contract.d.ts +85 -0
  71. package/dist/interpreter/filesystem/memory.d.ts +3 -0
  72. package/dist/interpreter/filesystem/opfs.d.ts +3 -0
  73. package/dist/interpreter/filesystem/scope.d.ts +35 -0
  74. package/dist/interpreter/filesystem.d.ts +8 -0
  75. package/dist/interpreter/gate.d.ts +6 -0
  76. package/dist/interpreter/host-protocol.d.ts +100 -0
  77. package/dist/interpreter/http-ingress-fetch.d.ts +7 -0
  78. package/dist/interpreter/http-ingress.d.ts +96 -0
  79. package/dist/interpreter/http-provider.d.ts +85 -0
  80. package/dist/interpreter/io.d.ts +20 -0
  81. package/dist/interpreter/mobile.d.ts +149 -0
  82. package/dist/interpreter/native-ingress.d.ts +95 -0
  83. package/dist/interpreter/node-port.d.ts +25 -0
  84. package/dist/interpreter/node.d.ts +36 -0
  85. package/dist/interpreter/revision.d.ts +64 -0
  86. package/dist/interpreter/scope.d.ts +89 -0
  87. package/dist/interpreter/socket-addresses.d.ts +50 -0
  88. package/dist/interpreter/socket-provider.d.ts +110 -0
  89. package/dist/interpreter/standard-cli.d.ts +34 -0
  90. package/dist/interpreter/standard-codec.d.ts +96 -0
  91. package/dist/interpreter/standard-filesystem.d.ts +47 -0
  92. package/dist/interpreter/standard-host.d.ts +71 -0
  93. package/dist/interpreter/standard-http-values.d.ts +159 -0
  94. package/dist/interpreter/standard-http.d.ts +9 -0
  95. package/dist/interpreter/standard-ingress.d.ts +63 -0
  96. package/dist/interpreter/standard-sockets.d.ts +177 -0
  97. package/dist/interpreter/standard-streams.d.ts +218 -0
  98. package/dist/interpreter/standard-system.d.ts +35 -0
  99. package/dist/interpreter/standard-utf8.d.ts +4 -0
  100. package/dist/interpreter/standard.d.ts +18 -0
  101. package/dist/interpreter/types.d.ts +152 -0
  102. package/dist/interpreter/wit-shape.d.ts +92 -0
  103. package/dist/manager.js +5 -332
  104. package/dist/msgpack.js +9 -615
  105. package/dist/node.d.ts +8 -1
  106. package/dist/node.js +248 -3321
  107. package/dist/rpc.js +196 -699
  108. package/dist/runtime/adapters.d.ts +120 -0
  109. package/dist/runtime/adapters.js +38 -0
  110. package/dist/runtime/calls.d.ts +7 -6
  111. package/dist/runtime/lifecycle.d.ts +3 -1
  112. package/dist/runtime/orbs.d.ts +5 -4
  113. package/dist/runtime/references.d.ts +27 -1
  114. package/dist/runtime/reload.d.ts +5 -3
  115. package/dist/runtime/scopes.d.ts +1 -1
  116. package/dist/runtime/signals.d.ts +4 -0
  117. package/dist/runtime/wit-client.d.ts +38 -0
  118. package/dist/telemetry.js +4 -25
  119. package/dist/tooling/build.d.ts +2 -2
  120. package/dist/tooling/client-resources.d.ts +23 -0
  121. package/dist/tooling/client.d.ts +10 -0
  122. package/dist/tooling/component-glue.d.ts +17 -0
  123. package/dist/tooling/component.d.ts +11 -0
  124. package/dist/tooling/index.d.ts +3 -1
  125. package/dist/tooling/index.js +1155 -102
  126. package/dist/tooling/resolver.d.ts +4 -4
  127. package/dist/tooling/typecheck.d.ts +2 -1
  128. package/dist/tooling/types.d.ts +27 -12
  129. package/dist/tooling/wit.d.ts +9 -0
  130. package/dist/wasi/wasi-clocks-types.d.ts +2 -0
  131. package/dist/wasi/wasi-http-client.d.ts +5 -0
  132. package/dist/wasi/wasi-http-types.d.ts +285 -0
  133. package/dist/wasi/wasi-sockets-ip-name-lookup.d.ts +23 -0
  134. package/dist/wasi/wasi-sockets-types.d.ts +146 -0
  135. package/package.json +62 -35
  136. package/src/authoring/declaration.ts +1 -1
  137. package/src/authoring/types.ts +53 -3
  138. package/src/authoring/wit.ts +15 -0
  139. package/src/bridge/commands.ts +1105 -0
  140. package/src/bridge/identifiers.ts +2 -0
  141. package/src/bridge/kernel.ts +56 -98
  142. package/src/bridge/protocol.ts +3 -142
  143. package/src/browser.ts +90 -6
  144. package/src/context-key.ts +4 -7
  145. package/src/error.ts +1 -1
  146. package/src/errors.ts +7 -0
  147. package/src/guest.ts +327 -0
  148. package/src/host.ts +128 -23
  149. package/src/http/bytes.ts +87 -0
  150. package/src/http/events.ts +34 -0
  151. package/src/http.ts +272 -0
  152. package/src/index.ts +3 -0
  153. package/src/infrastructure/rpc/client.ts +136 -17
  154. package/src/infrastructure/rpc/resources.ts +123 -0
  155. package/src/interpreter/abi.ts +69 -0
  156. package/src/interpreter/acquire.ts +58 -0
  157. package/src/interpreter/browser-network.ts +170 -0
  158. package/src/interpreter/browser-port.ts +425 -0
  159. package/src/interpreter/browser.ts +340 -0
  160. package/src/interpreter/cli.ts +30 -0
  161. package/src/interpreter/codec.ts +298 -0
  162. package/src/interpreter/component/execution.ts +264 -0
  163. package/src/interpreter/component/host.ts +409 -0
  164. package/src/interpreter/component/instance.ts +138 -0
  165. package/src/interpreter/component/kernel.ts +611 -0
  166. package/src/interpreter/component/names.ts +13 -0
  167. package/src/interpreter/component/port.ts +598 -0
  168. package/src/interpreter/component/prepared.ts +194 -0
  169. package/src/interpreter/component/resources.ts +345 -0
  170. package/src/interpreter/component/revision.ts +702 -0
  171. package/src/interpreter/component/scope.ts +447 -0
  172. package/src/interpreter/component/streams.ts +180 -0
  173. package/src/interpreter/component/values.ts +254 -0
  174. package/src/interpreter/component/wasi-filesystem.ts +472 -0
  175. package/src/interpreter/component/wasi-host.ts +197 -0
  176. package/src/interpreter/component/wasi-http.ts +939 -0
  177. package/src/interpreter/component/wasi-sockets.ts +958 -0
  178. package/src/interpreter/component/wasi.ts +238 -0
  179. package/src/interpreter/core.ts +386 -0
  180. package/src/interpreter/fetch-http-provider.ts +98 -0
  181. package/src/interpreter/filesystem/contract.ts +138 -0
  182. package/src/interpreter/filesystem/memory.ts +180 -0
  183. package/src/interpreter/filesystem/opfs.ts +294 -0
  184. package/src/interpreter/filesystem/scope.ts +390 -0
  185. package/src/interpreter/filesystem.ts +20 -0
  186. package/src/interpreter/gate.ts +55 -0
  187. package/src/interpreter/host-protocol.ts +242 -0
  188. package/src/interpreter/http-ingress-fetch.ts +133 -0
  189. package/src/interpreter/http-ingress.ts +203 -0
  190. package/src/interpreter/http-provider.ts +105 -0
  191. package/src/interpreter/io.ts +66 -0
  192. package/src/interpreter/mobile.ts +385 -0
  193. package/src/interpreter/native-ingress.ts +550 -0
  194. package/src/interpreter/node-port.ts +218 -0
  195. package/src/interpreter/node.ts +114 -0
  196. package/src/interpreter/revision.ts +450 -0
  197. package/src/interpreter/scope.ts +453 -0
  198. package/src/interpreter/socket-addresses.ts +246 -0
  199. package/src/interpreter/socket-provider.ts +126 -0
  200. package/src/interpreter/standard-cli.ts +184 -0
  201. package/src/interpreter/standard-codec.ts +403 -0
  202. package/src/interpreter/standard-filesystem.ts +545 -0
  203. package/src/interpreter/standard-host.ts +840 -0
  204. package/src/interpreter/standard-http-values.ts +624 -0
  205. package/src/interpreter/standard-http.ts +622 -0
  206. package/src/interpreter/standard-ingress.ts +344 -0
  207. package/src/interpreter/standard-sockets.ts +847 -0
  208. package/src/interpreter/standard-streams.ts +710 -0
  209. package/src/interpreter/standard-system.ts +182 -0
  210. package/src/interpreter/standard-utf8.ts +72 -0
  211. package/src/interpreter/standard.ts +283 -0
  212. package/src/interpreter/types.ts +163 -0
  213. package/src/interpreter/wit-shape.ts +175 -0
  214. package/src/node.ts +43 -14
  215. package/src/runtime/activities.ts +6 -6
  216. package/src/runtime/adapters.ts +314 -0
  217. package/src/runtime/calls.ts +213 -90
  218. package/src/runtime/context.ts +7 -7
  219. package/src/runtime/lifecycle.ts +36 -17
  220. package/src/runtime/orbs.ts +81 -39
  221. package/src/runtime/programs.ts +16 -8
  222. package/src/runtime/references.ts +128 -11
  223. package/src/runtime/reload.ts +71 -30
  224. package/src/runtime/resources.ts +2 -2
  225. package/src/runtime/scopes.ts +17 -17
  226. package/src/runtime/session.ts +3 -1
  227. package/src/runtime/signals.ts +31 -0
  228. package/src/runtime/wit-client.ts +228 -0
  229. package/src/tooling/build.ts +152 -54
  230. package/src/tooling/client-resources.ts +74 -0
  231. package/src/tooling/client.ts +531 -0
  232. package/src/tooling/component-glue.ts +288 -0
  233. package/src/tooling/component.ts +225 -0
  234. package/src/tooling/index.ts +3 -7
  235. package/src/tooling/resolver.ts +42 -25
  236. package/src/tooling/typecheck.ts +59 -4
  237. package/src/tooling/types.ts +27 -12
  238. package/src/tooling/wit.ts +188 -0
  239. package/dist/embedded.d.ts +0 -1
  240. package/dist/embedded.js +0 -1
  241. package/src/embedded.ts +0 -301
@@ -1,83 +1,181 @@
1
1
  import { createHash } from "node:crypto";
2
- import { mkdir, rename, writeFile } from "node:fs/promises";
2
+ import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
3
3
  import path from "node:path";
4
4
  import ts from "typescript";
5
5
 
6
- import { external, resolver } from "./resolver.ts";
6
+ import { NodeContract } from "@syntropika/tembra-engine-node";
7
+ import { query } from "../interpreter/types.ts";
8
+ import { witTypes } from "./wit.ts";
9
+ import { resolver } from "./resolver.ts";
7
10
  import { typecheck } from "./typecheck.ts";
8
- import { Platform, type Artifact, type BuildConfig } from "./types.ts";
11
+ import { prepareBrowserComponent } from "./component.ts";
12
+ import type { Artifact, BuildConfig } from "./types.ts";
9
13
 
10
- /** Resolves packages at build time and emits exactly one content-addressed ESM file per target. */
14
+ /** Resolve dependencies and WIT types, then emit one closed Wasm artifact per Orb. */
11
15
  export async function build(config: BuildConfig): Promise<readonly Artifact[]> {
16
+ if (typeof Bun === "undefined") throw new Error("Tembra build requires Bun");
12
17
  const root = path.resolve(config.root ?? process.cwd());
13
18
  const outDir = path.resolve(root, config.outDir ?? "dist/tembra");
14
19
  const artifacts: Artifact[] = [];
20
+ const declarations = new Map<string, Awaited<ReturnType<typeof witTypes>>>();
21
+ const worlds = new Map<string, string | undefined>();
22
+ for (const target of Object.values(config.builds)) {
23
+ if (target.entry.endsWith(".wasm")) continue;
24
+ const file = path.resolve(root, target.wit);
25
+ if (worlds.has(file) && worlds.get(file) !== target.world)
26
+ throw new Error(`Source builds for ${target.wit} must select the same WIT world`);
27
+ worlds.set(file, target.world);
28
+ }
29
+ const identities = new Map<string, string>();
30
+ const editorFiles = new Map<string, string>();
31
+ for (const [wit, world] of worlds) {
32
+ const types = await witTypes(wit, world, root);
33
+ declarations.set(wit, types);
34
+ for (const [identity, digest] of types.imports) {
35
+ const previous = identities.get(identity);
36
+ if (previous !== undefined && previous !== digest)
37
+ throw new Error(`Conflicting WIT declarations for ${identity} in one project`);
38
+ identities.set(identity, digest);
39
+ }
40
+ for (const [file, contents] of types.files) editorFiles.set(file, contents);
41
+ }
42
+ // Reject conflicting contracts before publishing any editor declaration.
43
+ for (const [file, contents] of editorFiles) {
44
+ const previous = await readFile(file, "utf8").catch((error: NodeJS.ErrnoException) => {
45
+ if (error.code === "ENOENT") return undefined;
46
+ throw error;
47
+ });
48
+ if (previous === contents) continue;
49
+ await mkdir(path.dirname(file), { recursive: true });
50
+ await writeFile(file, contents);
51
+ }
15
52
  for (const [name, target] of Object.entries(config.builds)) {
16
- if (!/^[\w-]+$/.test(name) || !Object.values(Platform).includes(target.platform))
17
- throw new Error("Invalid build name or platform");
53
+ if (!/^[\w-]+$/.test(name)) throw new Error("Invalid build name");
18
54
  const entry = path.resolve(root, target.entry);
19
- if (target.typecheck !== false) typecheck(entry, root);
20
- const result = await Bun.build({
21
- entrypoints: [target.platform === Platform.Embedded ? "tembra:entry" : entry],
22
- root,
23
- format: target.platform === Platform.Embedded ? "iife" : "esm",
24
- target: target.platform === Platform.Node ? "node" : "browser",
25
- splitting: false,
26
- minify: target.minify ?? true,
27
- ...(target.define ? { define: { ...target.define } } : {}),
28
- plugins: [resolver(root, target)],
29
- });
30
- if (!result.success) throw new AggregateError(result.logs, `Build ${name} failed`);
31
- if (result.outputs.length !== 1 || result.outputs[0]?.kind !== "entry-point")
32
- throw new Error(
33
- `Build ${name} requires external assets; publish them explicitly instead of silently expanding the orb bundle`,
55
+ const wit = path.resolve(root, target.wit);
56
+ const compiled = entry.endsWith(".wasm");
57
+ const types = compiled ? undefined : declarations.get(wit);
58
+ let bytes: Uint8Array;
59
+ const contract = types?.contract ?? new NodeContract(path.dirname(wit), target.world);
60
+ if (compiled) {
61
+ if (!target.apiVersion)
62
+ throw new Error(`Build ${name} requires apiVersion for a compiled Orb`);
63
+ bytes = contract.componentArtifact(
64
+ await Bun.file(entry).bytes(),
65
+ name,
66
+ target.apiVersion,
67
+ target.contributes,
68
+ target.references,
34
69
  );
35
- const output = result.outputs[0]!;
36
- const source = await output.text();
37
- const parsed = ts.createSourceFile(
38
- "bundle.js",
39
- source,
40
- ts.ScriptTarget.Latest,
41
- true,
42
- ts.ScriptKind.JS,
43
- );
44
- const hasDefault = parsed.statements.some(
45
- (statement) =>
46
- ts.isExportAssignment(statement) ||
47
- (ts.isExportDeclaration(statement) &&
48
- statement.exportClause &&
49
- ts.isNamedExports(statement.exportClause) &&
50
- statement.exportClause.elements.some((element) => element.name.text === "default")),
51
- );
52
- if (target.platform !== Platform.Embedded && !hasDefault)
53
- throw new Error(`Build ${name} must export a default orb`);
54
- /** Reject imports that the selected runtime cannot resolve. */
55
- function verify(node: ts.Node): void {
56
- let specifier: ts.Expression | undefined;
57
- if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node))
58
- specifier = node.moduleSpecifier;
59
- if (ts.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword)
60
- specifier = node.arguments[0];
61
- if (specifier && (!ts.isStringLiteralLike(specifier) || !external(specifier.text, target)))
62
- throw new Error(`Build ${name} contains an unresolved import`);
63
- ts.forEachChild(node, verify);
70
+ } else {
71
+ if (target.browser !== undefined)
72
+ throw new Error(`Build ${name} is a source Orb; browsers run it without preparation`);
73
+ if (target.apiVersion !== undefined)
74
+ throw new Error(`Build ${name} declares apiVersion in its source Orb`);
75
+ if (target.contributes !== undefined)
76
+ throw new Error(`Build ${name} declares contributions in its source Orb`);
77
+ if (target.references !== undefined)
78
+ throw new Error(`Build ${name} declares reference views in its source Orb`);
79
+ const program = typecheck(entry, root, types!.files, ["tembra-wasm"]);
80
+ const description = query<{ imports: { contract?: string; type_only?: boolean }[] }>(
81
+ contract,
82
+ { kind: "describe" },
83
+ );
84
+ const imports = new Set(
85
+ description.imports.flatMap((item) =>
86
+ item.type_only === false && item.contract ? [item.contract] : [],
87
+ ),
88
+ );
89
+ const result = await Bun.build({
90
+ entrypoints: ["tembra:entry"],
91
+ root,
92
+ format: "iife",
93
+ target: "browser",
94
+ splitting: false,
95
+ minify: target.minify ?? true,
96
+ ...(target.define ? { define: { ...target.define } } : {}),
97
+ plugins: [resolver(root, target, imports)],
98
+ }).catch((error: unknown) => {
99
+ if (error instanceof AggregateError)
100
+ throw new AggregateError(
101
+ error.errors,
102
+ `Build ${name} failed: ${error.errors.map(String).join("\n")}`,
103
+ );
104
+ throw error;
105
+ });
106
+ if (!result.success) throw new AggregateError(result.logs, `Build ${name} failed`);
107
+ if (result.outputs.length !== 1 || result.outputs[0]?.kind !== "entry-point")
108
+ throw new Error(`Build ${name} requires exactly one closed bundle`);
109
+ const source = await result.outputs[0]!.text();
110
+ const parsed = ts.createSourceFile(
111
+ "bundle.js",
112
+ source,
113
+ ts.ScriptTarget.Latest,
114
+ true,
115
+ ts.ScriptKind.JS,
116
+ );
117
+ /** Reject module operations that cannot resolve in the selected runtime. */
118
+ function verify(node: ts.Node): void {
119
+ let specifier: ts.Expression | undefined;
120
+ if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node))
121
+ specifier = node.moduleSpecifier;
122
+ if (
123
+ ts.isCallExpression(node) &&
124
+ (node.expression.kind === ts.SyntaxKind.ImportKeyword ||
125
+ (ts.isIdentifier(node.expression) && node.expression.text === "require"))
126
+ )
127
+ specifier = node.arguments[0];
128
+ if (specifier) throw new Error(`Build ${name} contains an unresolved import`);
129
+ ts.forEachChild(node, verify);
130
+ }
131
+ verify(parsed);
132
+ const registration = identity(program!, entry);
133
+ bytes = contract.sourceArtifact(source, registration.name, registration.apiVersion);
64
134
  }
65
- verify(parsed);
66
- const bytes = Buffer.from(source);
67
135
  const hash = createHash("sha256").update(bytes).digest("hex");
68
136
  const directory = path.join(outDir, name);
69
- const destination = path.join(directory, `orb.${hash}.js`);
137
+ const destination = path.join(directory, `orb.${hash}.wasm`);
70
138
  await mkdir(directory, { recursive: true });
71
139
  const temporary = `${destination}.${process.pid}.tmp`;
72
140
  await writeFile(temporary, bytes);
73
141
  await rename(temporary, destination);
142
+ const browser =
143
+ compiled && target.browser
144
+ ? (await prepareBrowserComponent(bytes, hash, path.join(directory, "browser"))).module
145
+ : undefined;
74
146
  artifacts.push({
75
147
  name,
76
- platform: target.platform,
77
148
  path: destination,
78
149
  sha256: hash,
79
150
  bytes: bytes.length,
151
+ ...(browser ? { browser } : {}),
80
152
  });
81
153
  }
82
154
  return artifacts;
83
155
  }
156
+
157
+ /** Read literal registration metadata from types without evaluating the author module. */
158
+ function identity(program: ts.Program, entry: string): { name: string; apiVersion: number } {
159
+ const checker = program.getTypeChecker();
160
+ const file = program.getSourceFile(entry)!;
161
+ const module = checker.getSymbolAtLocation(file);
162
+ const exported =
163
+ module && checker.getExportsOfModule(module).find((symbol) => symbol.name === "default");
164
+ if (!exported) throw new Error("Wasm source must export a default orb");
165
+ const definition = checker.getTypeOfSymbolAtLocation(exported, file);
166
+ const property = (name: string) => {
167
+ const symbol = definition.getProperty(name);
168
+ return symbol && checker.getTypeOfSymbolAtLocation(symbol, file);
169
+ };
170
+ const name = property("name");
171
+ const version = property("apiVersion");
172
+ if (
173
+ !name?.isStringLiteral() ||
174
+ !version?.isNumberLiteral() ||
175
+ !name.value ||
176
+ !Number.isInteger(version.value) ||
177
+ version.value <= 0
178
+ )
179
+ throw new Error("Wasm source requires literal name and positive apiVersion metadata");
180
+ return { name: name.value, apiVersion: version.value };
181
+ }
@@ -0,0 +1,74 @@
1
+ /** Name one resource per nominal definition, preserving transparent WIT aliases. */
2
+ interface ResourceGraph {
3
+ readonly types: readonly {
4
+ readonly id: number;
5
+ readonly definition: {
6
+ readonly kind: string | { readonly type?: string | number | null | undefined };
7
+ readonly owner?: string;
8
+ readonly name?: string;
9
+ };
10
+ }[];
11
+ }
12
+
13
+ /** Generated names, nominal identities and transparent alias declarations. */
14
+ interface ResourceNames {
15
+ readonly names: ReadonlyMap<number, string>;
16
+ readonly nominals: ReadonlyMap<number, string>;
17
+ readonly aliases: readonly string[];
18
+ canonical(id: number): number;
19
+ }
20
+
21
+ /** Group local and imported resource aliases by their original WIT definition. */
22
+ export function resourceNames(
23
+ graph: ResourceGraph,
24
+ resources: Readonly<Record<string, number>>,
25
+ identity: string,
26
+ occupied: Set<string>,
27
+ ): ResourceNames {
28
+ const definitions = new Map(graph.types.map(({ id, definition }) => [id, definition]));
29
+ const canonical = (id: number): number => {
30
+ const kind = definitions.get(id)?.kind;
31
+ return typeof kind === "object" && typeof kind.type === "number" ? canonical(kind.type) : id;
32
+ };
33
+ const groups = new Map<number, string[]>();
34
+ for (const [name, id] of Object.entries(resources)) {
35
+ const key = canonical(id);
36
+ const aliases = groups.get(key) ?? [];
37
+ aliases.push(name);
38
+ groups.set(key, aliases);
39
+ }
40
+ const names = new Map<number, string>();
41
+ const nominals = new Map<number, string>();
42
+ const aliases: string[] = [];
43
+ const allocate = (name: string): string => {
44
+ let spelling = name
45
+ .split("-")
46
+ .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
47
+ .join("");
48
+ while (occupied.has(spelling)) spelling += "_";
49
+ occupied.add(spelling);
50
+ return spelling;
51
+ };
52
+ for (const [id, spellings] of groups) {
53
+ const definition = definitions.get(id);
54
+ if (definition?.kind !== "resource" || !definition.owner || !definition.name)
55
+ throw new Error(`${identity}: resource has no portable nominal identity`);
56
+ const preferred =
57
+ definition.owner === identity && spellings.includes(definition.name)
58
+ ? definition.name
59
+ : spellings[0]!;
60
+ const name = allocate(preferred);
61
+ names.set(id, name);
62
+ nominals.set(id, `${definition.owner}#${definition.name}`);
63
+ for (const spelling of spellings) {
64
+ if (spelling === preferred) continue;
65
+ const alias = allocate(spelling);
66
+ aliases.push(
67
+ `/** The transparent WIT resource alias \`${spelling}\`. */`,
68
+ `export type ${alias} = ${name};`,
69
+ "",
70
+ );
71
+ }
72
+ }
73
+ return { names, nominals, aliases, canonical };
74
+ }