@tyndall/wasm 0.0.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/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # @tyndall/wasm
2
+
3
+ ## Overview
4
+ WebAssembly adapter package implementing UIAdapter contracts for WASM-backed rendering pipelines.
5
+
6
+ ## Responsibilities
7
+ - Provide WASM adapter, factory, and registry implementations
8
+ - Provide WASM instantiation helper for runtime setup
9
+
10
+ ## Public API Highlights
11
+ - createWasmAdapter
12
+ - instantiateWasmModule
13
+ - createWasmAdapterRegistry
14
+
15
+ ## Development
16
+ - Build: bun run --filter @tyndall/wasm build
17
+ - Test (from workspace root): bun test
18
+
19
+ ## Documentation
20
+ - Package specification: [spec.md](./spec.md)
21
+ - Package architecture: [architecture.md](./architecture.md)
22
+ - Package changes: [CHANGELOG.md](./CHANGELOG.md)
23
+
24
+ ## Maintenance Rules
25
+ - Keep this document aligned with implemented package behavior.
26
+ - Update spec.md and architecture.md whenever package contracts or design boundaries change.
27
+ - Record user-visible package changes in CHANGELOG.md.
@@ -0,0 +1,16 @@
1
+ import type { HeadDescriptor, HmrIntegration, UIAdapter, UIAdapterEntryContext, UIAdapterRenderContext, UIAdapterRenderResult } from "@tyndall/core";
2
+ export interface WasmAdapterRenderContext extends UIAdapterRenderContext {
3
+ instance: WebAssembly.Instance | null;
4
+ }
5
+ export interface WasmAdapterOptions {
6
+ name?: string;
7
+ createClientEntry?: (ctx: UIAdapterEntryContext) => string;
8
+ createServerEntry?: (ctx: UIAdapterEntryContext) => string;
9
+ renderToHtml?: (ctx: UIAdapterRenderContext) => Promise<UIAdapterRenderResult> | UIAdapterRenderResult;
10
+ instantiate?: (ctx: UIAdapterRenderContext) => Promise<WebAssembly.Instance | null> | WebAssembly.Instance | null;
11
+ render?: (ctx: WasmAdapterRenderContext) => Promise<unknown> | unknown;
12
+ getHead?: (ctx: UIAdapterRenderContext) => HeadDescriptor;
13
+ hmrIntegration?: HmrIntegration;
14
+ }
15
+ export declare const createWasmAdapter: (options?: WasmAdapterOptions) => UIAdapter;
16
+ //# sourceMappingURL=adapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,cAAc,EACd,SAAS,EACT,qBAAqB,EACrB,sBAAsB,EACtB,qBAAqB,EACtB,MAAM,eAAe,CAAC;AAGvB,MAAM,WAAW,wBAAyB,SAAQ,sBAAsB;IACtE,QAAQ,EAAE,WAAW,CAAC,QAAQ,GAAG,IAAI,CAAC;CACvC;AAED,MAAM,WAAW,kBAAkB;IACjC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,iBAAiB,CAAC,EAAE,CAAC,GAAG,EAAE,qBAAqB,KAAK,MAAM,CAAC;IAC3D,iBAAiB,CAAC,EAAE,CAAC,GAAG,EAAE,qBAAqB,KAAK,MAAM,CAAC;IAC3D,YAAY,CAAC,EAAE,CACb,GAAG,EAAE,sBAAsB,KACxB,OAAO,CAAC,qBAAqB,CAAC,GAAG,qBAAqB,CAAC;IAC5D,WAAW,CAAC,EAAE,CACZ,GAAG,EAAE,sBAAsB,KACxB,OAAO,CAAC,WAAW,CAAC,QAAQ,GAAG,IAAI,CAAC,GAAG,WAAW,CAAC,QAAQ,GAAG,IAAI,CAAC;IACxE,MAAM,CAAC,EAAE,CAAC,GAAG,EAAE,wBAAwB,KAAK,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC;IACvE,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,sBAAsB,KAAK,cAAc,CAAC;IAC1D,cAAc,CAAC,EAAE,cAAc,CAAC;CACjC;AA4ED,eAAO,MAAM,iBAAiB,GAAI,UAAS,kBAAuB,KAAG,SAQnE,CAAC"}
@@ -0,0 +1,72 @@
1
+ import { mergeHeadDescriptors, serializeProps } from "@tyndall/core";
2
+ const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
3
+ const resolvePageImport = (ctx) => {
4
+ const options = ctx.uiOptions ?? ctx.adapterOptions;
5
+ const pageModule = options?.pageModule;
6
+ const specifier = typeof pageModule === "string" ? pageModule : "./page";
7
+ return JSON.stringify(specifier);
8
+ };
9
+ const normalizeRenderResult = (value) => {
10
+ if (typeof value === "string") {
11
+ return { html: value };
12
+ }
13
+ if (isRecord(value) && typeof value.html === "string") {
14
+ return {
15
+ html: value.html,
16
+ head: isRecord(value.head) ? value.head : undefined,
17
+ status: typeof value.status === "number" ? value.status : undefined,
18
+ headers: isRecord(value.headers)
19
+ ? Object.fromEntries(Object.entries(value.headers).map(([key, header]) => [key, String(header)]))
20
+ : undefined,
21
+ };
22
+ }
23
+ return { html: String(value ?? "") };
24
+ };
25
+ const defaultClientEntry = (ctx) => {
26
+ const pageImport = resolvePageImport(ctx);
27
+ return [
28
+ `import Page from ${pageImport};`,
29
+ "if (Page && typeof Page.bootstrap === \"function\") {",
30
+ " void Page.bootstrap();",
31
+ "}",
32
+ ].join("\n");
33
+ };
34
+ const defaultServerEntry = (ctx) => {
35
+ const pageImport = resolvePageImport(ctx);
36
+ return [
37
+ `import Page from ${pageImport};`,
38
+ "export const renderToHtml = async (ctx) => {",
39
+ " if (Page && typeof Page.render === \"function\") {",
40
+ " const html = await Page.render(ctx.props ?? {});",
41
+ " return { html: typeof html === \"string\" ? html : String(html ?? \"\") };",
42
+ " }",
43
+ " return { html: `<div data-hyper-wasm=\\\"true\\\">${ctx.routeId}</div>` };",
44
+ "};",
45
+ ].join("\n");
46
+ };
47
+ const defaultRenderToHtml = async (ctx, options) => {
48
+ const instance = options.instantiate ? await options.instantiate(ctx) : null;
49
+ if (!options.render) {
50
+ return {
51
+ html: `<div data-hyper-wasm=\"true\" data-hyper-wasm-ready=\"${instance ? "true" : "false"}\">${ctx.routeId}</div>`,
52
+ };
53
+ }
54
+ const rendered = await options.render({ ...ctx, instance });
55
+ const normalized = normalizeRenderResult(rendered);
56
+ const providedHead = options.getHead?.(ctx);
57
+ // Keep adapter-provided head as the final override for deterministic composition.
58
+ const head = mergeHeadDescriptors(normalized.head ?? {}, providedHead ?? {});
59
+ return {
60
+ ...normalized,
61
+ head,
62
+ };
63
+ };
64
+ export const createWasmAdapter = (options = {}) => ({
65
+ name: options.name ?? "wasm",
66
+ createClientEntry: options.createClientEntry ?? defaultClientEntry,
67
+ createServerEntry: options.createServerEntry ?? defaultServerEntry,
68
+ renderToHtml: options.renderToHtml ?? ((ctx) => defaultRenderToHtml(ctx, options)),
69
+ serializeProps,
70
+ getHead: options.getHead,
71
+ hmrIntegration: options.hmrIntegration,
72
+ });
@@ -0,0 +1,6 @@
1
+ export { createWasmAdapter } from "./adapter.js";
2
+ export type { WasmAdapterOptions, WasmAdapterRenderContext } from "./adapter.js";
3
+ export { createWasmAdapterRegistry, createWasmUiAdapterFactory } from "./registry.js";
4
+ export { instantiateWasmModule } from "./wasm.js";
5
+ export type { WasmBinarySource } from "./wasm.js";
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACjD,YAAY,EAAE,kBAAkB,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AACjF,OAAO,EAAE,yBAAyB,EAAE,0BAA0B,EAAE,MAAM,eAAe,CAAC;AACtF,OAAO,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AAClD,YAAY,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { createWasmAdapter } from "./adapter.js";
2
+ export { createWasmAdapterRegistry, createWasmUiAdapterFactory } from "./registry.js";
3
+ export { instantiateWasmModule } from "./wasm.js";
@@ -0,0 +1,4 @@
1
+ import type { UIAdapterFactory, UIAdapterRegistry } from "@tyndall/core";
2
+ export declare const createWasmUiAdapterFactory: () => UIAdapterFactory;
3
+ export declare const createWasmAdapterRegistry: () => UIAdapterRegistry;
4
+ //# sourceMappingURL=registry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAkB,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAWzF,eAAO,MAAM,0BAA0B,QAAO,gBAiB7C,CAAC;AAEF,eAAO,MAAM,yBAAyB,QAAO,iBAE3C,CAAC"}
@@ -0,0 +1,18 @@
1
+ import { createWasmAdapter } from "./adapter.js";
2
+ const isFunction = (value) => typeof value === "function";
3
+ export const createWasmUiAdapterFactory = () => (options) => {
4
+ const normalized = options;
5
+ const routeRender = isFunction(normalized.routeRender)
6
+ ? normalized.routeRender
7
+ : undefined;
8
+ const routeHead = isFunction(normalized.routeHead)
9
+ ? normalized.routeHead
10
+ : undefined;
11
+ return createWasmAdapter({
12
+ render: routeRender ? ({ props }) => routeRender(props) : undefined,
13
+ getHead: routeHead ? ({ props }) => routeHead(props) : undefined,
14
+ });
15
+ };
16
+ export const createWasmAdapterRegistry = () => ({
17
+ wasm: createWasmUiAdapterFactory(),
18
+ });
package/dist/wasm.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export type WasmBinarySource = ArrayBuffer | Uint8Array;
2
+ export declare const instantiateWasmModule: (source: WasmBinarySource, imports?: WebAssembly.Imports) => Promise<WebAssembly.Instance>;
3
+ //# sourceMappingURL=wasm.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wasm.d.ts","sourceRoot":"","sources":["../src/wasm.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,gBAAgB,GAAG,WAAW,GAAG,UAAU,CAAC;AAExD,eAAO,MAAM,qBAAqB,GAChC,QAAQ,gBAAgB,EACxB,UAAS,WAAW,CAAC,OAAY,KAChC,OAAO,CAAC,WAAW,CAAC,QAAQ,CAU9B,CAAC"}
package/dist/wasm.js ADDED
@@ -0,0 +1,8 @@
1
+ export const instantiateWasmModule = async (source, imports = {}) => {
2
+ const bytes = source instanceof Uint8Array ? source : new Uint8Array(source);
3
+ const instantiated = (await WebAssembly.instantiate(bytes, imports));
4
+ if ("instance" in instantiated) {
5
+ return instantiated.instance;
6
+ }
7
+ return instantiated;
8
+ };
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@tyndall/wasm",
3
+ "version": "0.0.1",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "type": "module",
8
+ "main": "dist/index.js",
9
+ "types": "dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "bun": "./src/index.ts",
14
+ "default": "./dist/index.js"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "scripts": {
21
+ "build": "tsc -p tsconfig.json"
22
+ },
23
+ "dependencies": {
24
+ "@tyndall/core": "workspace:*",
25
+ "@tyndall/shared": "workspace:*"
26
+ }
27
+ }