@master/css-binding-wasm-engine 0.0.0-stage → 3.0.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/dist/index.js ADDED
@@ -0,0 +1,193 @@
1
+ import { MasterCSSError } from "@master/css-schema";
2
+ // src/index.ts
3
+ const initializedInputs = /* @__PURE__ */ new WeakMap();
4
+ function sameInput(left, right) {
5
+ return left === right || String(left) === String(right);
6
+ }
7
+ async function initializeModule(module, input) {
8
+ const initializedInput = initializedInputs.get(module);
9
+ if (initializedInput !== void 0) {
10
+ if (!sameInput(initializedInput, input)) throw new MasterCSSError({
11
+ code: "WASM_INPUT_CONFLICT",
12
+ domain: "binding",
13
+ message: "A Master CSS Wasm module cannot be initialized with two different inputs."
14
+ });
15
+ return module;
16
+ }
17
+ await module.default({ module_or_path: input });
18
+ initializedInputs.set(module, input);
19
+ return module;
20
+ }
21
+ /**
22
+ * Binds the loading and session APIs to one artifact. Entry points own the artifact
23
+ * paths, so a bundler only sees the Wasm its entry can load.
24
+ */
25
+ function createWasmEngineLoader(artifact) {
26
+ let defaultModulePromise;
27
+ const importGeneratedModule = async () => await artifact.load();
28
+ async function loadWasmEngine(options = {}) {
29
+ try {
30
+ if (options.module) return await initializeModule(options.module, options.input ?? artifact.url);
31
+ if (options.input !== void 0) return await initializeModule(await importGeneratedModule(), options.input);
32
+ defaultModulePromise ??= importGeneratedModule().then((module) => initializeModule(module, artifact.url)).catch((cause) => {
33
+ defaultModulePromise = void 0;
34
+ throw cause;
35
+ });
36
+ return await defaultModulePromise;
37
+ } catch (cause) {
38
+ if (cause instanceof MasterCSSError) throw cause;
39
+ throw new MasterCSSError({
40
+ code: "WASM_LOAD_FAILED",
41
+ domain: "binding",
42
+ message: cause instanceof Error ? cause.message : "Cannot load the Master CSS engine Wasm artifact."
43
+ }, { cause });
44
+ }
45
+ }
46
+ return Object.freeze({
47
+ url: artifact.url,
48
+ loadWasmEngine,
49
+ async createWasmEngineSession(source, sessionOptions = {}, loadOptions = {}) {
50
+ return engineSession(await loadWasmEngine(loadOptions), source, sessionOptions);
51
+ },
52
+ async compileRuntimeProgram(manifestJSON, loadOptions = {}) {
53
+ return (await loadWasmEngine(loadOptions)).compileRuntimeProgram(manifestJSON);
54
+ },
55
+ async createWasmRenderSession(manifestJSON, sessionOptions = {}, loadOptions = {}) {
56
+ return renderSession(await loadWasmEngine(loadOptions), manifestJSON, sessionOptions);
57
+ }
58
+ });
59
+ }
60
+ function emittedGlobalsJSON(options) {
61
+ return options.emittedGlobals === void 0 ? void 0 : JSON.stringify(options.emittedGlobals);
62
+ }
63
+ const EMITTED_GLOBALS_FIELDS = /* @__PURE__ */ new Set(["variables", "keyframes"]);
64
+ function invalidEmittedGlobals(message) {
65
+ throw new MasterCSSError({
66
+ code: "INVALID_INPUT",
67
+ domain: "binding",
68
+ message: `Invalid Master CSS emitted globals: ${message}`
69
+ });
70
+ }
71
+ function counted(field, record) {
72
+ const entries = Object.entries(record ?? {});
73
+ for (const [name, count] of entries) if (!Number.isInteger(count) || count < 0 || count > 4294967295) invalidEmittedGlobals(`${field}.${name} must be a count.`);
74
+ return entries;
75
+ }
76
+ /** Validates what the engine's emitted globals schema accepts, then flattens it. */
77
+ function generatedEmittedGlobals(emittedGlobals = {}) {
78
+ for (const field of Object.keys(emittedGlobals)) if (!EMITTED_GLOBALS_FIELDS.has(field)) invalidEmittedGlobals(`unknown field ${field}.`);
79
+ const variables = counted("variables", emittedGlobals.variables);
80
+ const keyframes = counted("keyframes", emittedGlobals.keyframes);
81
+ const entries = [...variables, ...keyframes];
82
+ return [entries.map(([name]) => name), Uint32Array.from([
83
+ variables.length,
84
+ keyframes.length,
85
+ ...entries.map(([, count]) => count)
86
+ ])];
87
+ }
88
+ function createGeneratedEngineSession(module, source, sessionOptions) {
89
+ if (typeof source !== "string") return new module.EngineSession(source, ...generatedEmittedGlobals(sessionOptions.emittedGlobals));
90
+ return module.EngineSession.fromManifest(source, emittedGlobalsJSON(sessionOptions));
91
+ }
92
+ function engineSession(module, source, sessionOptions) {
93
+ const session = createGeneratedEngineSession(module, source, sessionOptions);
94
+ let disposed = false;
95
+ const assertActive = () => {
96
+ if (disposed) throw new MasterCSSError({
97
+ code: "SESSION_DISPOSED",
98
+ domain: "binding",
99
+ message: "The Master CSS Wasm engine session has been disposed."
100
+ });
101
+ };
102
+ return Object.freeze({
103
+ manifestJSON() {
104
+ assertActive();
105
+ return session.manifestJSON();
106
+ },
107
+ ensureClassRules(classNames) {
108
+ assertActive();
109
+ return JSON.parse(session.ensureClassRules(classNames));
110
+ },
111
+ deleteClassRules(classNames) {
112
+ assertActive();
113
+ return JSON.parse(session.deleteClassRules(classNames));
114
+ },
115
+ registerEmittedGlobals(emittedGlobals) {
116
+ assertActive();
117
+ return JSON.parse(session.registerEmittedGlobals(...generatedEmittedGlobals(emittedGlobals)));
118
+ },
119
+ replaceEmittedGlobals(emittedGlobals) {
120
+ assertActive();
121
+ return JSON.parse(session.replaceEmittedGlobals(...generatedEmittedGlobals(emittedGlobals)));
122
+ },
123
+ refresh(next, emittedGlobals) {
124
+ assertActive();
125
+ if (typeof next === "string") return JSON.parse(session.refreshManifest(next, emittedGlobals === void 0 ? void 0 : JSON.stringify(emittedGlobals)));
126
+ return JSON.parse(session.refresh(next, ...emittedGlobals === void 0 ? [] : generatedEmittedGlobals(emittedGlobals)));
127
+ },
128
+ keyframeDefinitions() {
129
+ assertActive();
130
+ return JSON.parse(session.keyframeDefinitions());
131
+ },
132
+ snapshot() {
133
+ assertActive();
134
+ return JSON.parse(session.snapshot());
135
+ },
136
+ executionState(classNames) {
137
+ assertActive();
138
+ return JSON.parse(session.executionState(classNames));
139
+ },
140
+ inspect(className) {
141
+ assertActive();
142
+ return JSON.parse(session.inspect(className));
143
+ },
144
+ dispose() {
145
+ if (disposed) return;
146
+ session.dispose();
147
+ disposed = true;
148
+ }
149
+ });
150
+ }
151
+ function renderSession(module, manifestJSON, sessionOptions) {
152
+ const session = new module.RenderSession(manifestJSON, emittedGlobalsJSON(sessionOptions));
153
+ let disposed = false;
154
+ const assertActive = () => {
155
+ if (disposed) throw new MasterCSSError({
156
+ code: "SESSION_DISPOSED",
157
+ domain: "binding",
158
+ message: "The Master CSS Wasm render session has been disposed."
159
+ });
160
+ };
161
+ return Object.freeze({
162
+ nativeDeclarationCandidates(classNames) {
163
+ assertActive();
164
+ return JSON.parse(session.nativeDeclarationCandidates([...classNames]));
165
+ },
166
+ ensureClassRules(classNames) {
167
+ assertActive();
168
+ session.ensureClasses([...classNames]);
169
+ },
170
+ ensureStylesheetResources(nativeCSS) {
171
+ assertActive();
172
+ session.ensureStylesheetResources(nativeCSS);
173
+ },
174
+ emittedGlobals() {
175
+ assertActive();
176
+ return JSON.parse(session.emittedGlobals());
177
+ },
178
+ snapshot() {
179
+ assertActive();
180
+ return JSON.parse(session.snapshot());
181
+ },
182
+ snapshotForClassNames(classNames) {
183
+ assertActive();
184
+ return JSON.parse(session.snapshotForClasses([...classNames]));
185
+ },
186
+ dispose() {
187
+ if (disposed) return;
188
+ session.dispose();
189
+ disposed = true;
190
+ }
191
+ });
192
+ }
193
+ export { createWasmEngineLoader };
package/dist/node.js ADDED
@@ -0,0 +1,17 @@
1
+ import { wasmEngine } from "./artifact.js";
2
+ import { readFile } from "node:fs/promises";
3
+ // src/node.ts
4
+ function resolveFileURL(input) {
5
+ if (input instanceof URL) return input.protocol === "file:" ? input : void 0;
6
+ if (typeof input !== "string" || !input.startsWith("file:")) return;
7
+ return new URL(input);
8
+ }
9
+ async function withNodeWasmInput(options) {
10
+ const fileURL = options.input === void 0 ? wasmEngine.url : resolveFileURL(options.input);
11
+ if (!fileURL) return options;
12
+ return {
13
+ ...options,
14
+ input: new Uint8Array(await readFile(fileURL))
15
+ };
16
+ }
17
+ export { wasmEngine, withNodeWasmInput };
@@ -0,0 +1,11 @@
1
+ // src/provider-core.ts
2
+ async function createWasmEngineProvider(loader, loadOptions = {}) {
3
+ const module = await loader.loadWasmEngine(loadOptions);
4
+ return Object.freeze({
5
+ info: JSON.parse(module.bindingInfo()),
6
+ createEngineSession: (source, emittedGlobals) => loader.createWasmEngineSession(source, { emittedGlobals }, loadOptions),
7
+ createRenderSession: (manifestJSON, emittedGlobalsJSON) => loader.createWasmRenderSession(manifestJSON, emittedGlobalsJSON === void 0 ? {} : { emittedGlobals: JSON.parse(emittedGlobalsJSON) }, loadOptions),
8
+ compileRuntimeProgram: (manifestJSON) => loader.compileRuntimeProgram(manifestJSON, loadOptions)
9
+ });
10
+ }
11
+ export { createWasmEngineProvider };
@@ -0,0 +1,3 @@
1
+ import { MasterCSSWasmEngineLoadOptions, MasterCSSWasmEngineLoadOptions as MasterCSSWasmEngineLoadOptions$1 } from "./provider.js";
2
+ declare function createMasterCSSEngineWasmProvider(options?: MasterCSSWasmEngineLoadOptions$1): Promise<object>;
3
+ export { type MasterCSSWasmEngineLoadOptions, createMasterCSSEngineWasmProvider };
@@ -0,0 +1,8 @@
1
+ import { wasmEngine } from "./artifact.js";
2
+ import { createWasmEngineProvider } from "./provider-core.js";
3
+ import { withNodeWasmInput } from "./node.js";
4
+ // src/provider-node.ts
5
+ async function createMasterCSSEngineWasmProvider(options = {}) {
6
+ return await createWasmEngineProvider(wasmEngine, await withNodeWasmInput(options));
7
+ }
8
+ export { createMasterCSSEngineWasmProvider };
@@ -0,0 +1,7 @@
1
+ type MasterCSSWasmEngineInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
2
+ interface MasterCSSWasmEngineLoadOptions {
3
+ readonly module?: object;
4
+ readonly input?: MasterCSSWasmEngineInput;
5
+ }
6
+ declare function createMasterCSSEngineWasmProvider(loadOptions?: MasterCSSWasmEngineLoadOptions): Promise<object>;
7
+ export { MasterCSSWasmEngineInput, MasterCSSWasmEngineLoadOptions, createMasterCSSEngineWasmProvider };
@@ -0,0 +1,7 @@
1
+ import { wasmEngine } from "./artifact.js";
2
+ import { createWasmEngineProvider } from "./provider-core.js";
3
+ // src/provider.ts
4
+ async function createMasterCSSEngineWasmProvider(loadOptions = {}) {
5
+ return await createWasmEngineProvider(wasmEngine, loadOptions);
6
+ }
7
+ export { createMasterCSSEngineWasmProvider };
package/package.json CHANGED
@@ -1,6 +1,47 @@
1
1
  {
2
- "name": "@master/css-binding-wasm-engine",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@master/css-binding-wasm-engine",
3
+ "type": "module",
4
+ "engines": {
5
+ "node": "^24"
6
+ },
7
+ "scripts": {
8
+ "build": "bun --bun tsdown --config ../../shared/tsdown-package.config.ts",
9
+ "build:artifacts": "cargo xtask build-wasm engine",
10
+ "dev": "bun --bun tsdown --config ../../shared/tsdown-package.config.ts --watch",
11
+ "test": "bun --bun vitest run",
12
+ "type-check": "bun --bun tsc -b tsconfig.typecheck.json",
13
+ "lint": "bun --bun eslint"
14
+ },
15
+ "license": "MIT",
16
+ "description": "Internal Master CSS engine Wasm binding provider.",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/master-co/css.git",
20
+ "directory": "packages/binding-wasm-engine"
21
+ },
22
+ "sideEffects": false,
23
+ "types": "./dist/provider.d.ts",
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/provider.d.ts",
27
+ "browser": "./dist/provider.js",
28
+ "node": "./dist/provider-node.js",
29
+ "import": "./dist/provider.js",
30
+ "default": "./dist/provider.js"
31
+ },
32
+ "./wasm": "./artifacts/mastercss_binding_wasm_engine_bg.wasm"
33
+ },
34
+ "files": [
35
+ "dist",
36
+ "artifacts"
37
+ ],
38
+ "publishConfig": {
39
+ "access": "public",
40
+ "provenance": true
41
+ },
42
+ "dependencies": {
43
+ "@master/css-schema": "3.0.0"
44
+ },
45
+ "version": "3.0.0",
46
+ "gitHead": "a5da5f2d73901082314a082d5213fd18e11ad48b"
47
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.