nucleation 0.1.14 → 0.1.17

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.
@@ -0,0 +1,112 @@
1
+ // nucleation-cdn-loader.js - Custom loader for CDN usage
2
+ // This file will be imported by the user's HTML script.
3
+
4
+ // These imports are relative to this loader file within the 'pkg' directory structure on the CDN
5
+ import * as bgModule from './nucleation_bg.js'; // Imports SchematicWrapper, __wbg_set_wasm, etc.
6
+
7
+ let initialized = false;
8
+ let wasmExports = null;
9
+
10
+ /**
11
+ * Initializes the WebAssembly module.
12
+ * @param {string | URL | Request | Response | BufferSource} [wasmPathOrModule]
13
+ * Optional. Path/URL to the 'nucleation_bg.wasm' file, or a pre-fetched Response/BufferSource.
14
+ * If not provided, it will attempt to fetch 'nucleation_bg.wasm' relative to this script.
15
+ * @returns {Promise<object>} A promise that resolves to an object containing the WASM exports
16
+ * (e.g., SchematicWrapper, BlockStateWrapper).
17
+ */
18
+ async function init(wasmPathOrModule) {
19
+ if (initialized) {
20
+ // console.debug("WASM already initialized. Returning existing exports.");
21
+ return {
22
+ SchematicWrapper: bgModule.SchematicWrapper,
23
+ BlockStateWrapper: bgModule.BlockStateWrapper,
24
+ // Add other direct exports from bgModule you want to expose
25
+ debug_schematic: bgModule.debug_schematic,
26
+ debug_json_schematic: bgModule.debug_json_schematic,
27
+ start: bgModule.start,
28
+ BlockPosition: bgModule.BlockPosition
29
+ };
30
+ }
31
+
32
+ if (typeof bgModule.__wbg_set_wasm !== 'function') {
33
+ throw new Error("__wbg_set_wasm function not found in ./nucleation_bg.js. The WASM bindings seem incomplete.");
34
+ }
35
+
36
+ let input = wasmPathOrModule;
37
+ if (input === undefined) {
38
+ // Default to fetching WASM relative to this loader script's location
39
+ // On CDN, this means same directory, e.g., .../pkg/nucleation_bg.wasm
40
+ input = new URL('./nucleation_bg.wasm', import.meta.url);
41
+ // console.debug("No WASM path provided, attempting to load from default relative path:", input.href);
42
+ }
43
+
44
+ const importsObject = { './nucleation_bg.js': bgModule };
45
+ let instanceExports;
46
+
47
+ if (typeof input === 'string' || input instanceof URL || input instanceof Request) {
48
+ // console.debug("Fetching WASM from URL/Request:", input);
49
+ const response = await fetch(input);
50
+ if (!response.ok) {
51
+ throw new Error(`Failed to fetch WASM (${input}): ${response.status} ${response.statusText}`);
52
+ }
53
+ if (typeof WebAssembly.instantiateStreaming === 'function') {
54
+ const { instance } = await WebAssembly.instantiateStreaming(response, importsObject);
55
+ instanceExports = instance.exports;
56
+ } else {
57
+ const buffer = await response.arrayBuffer();
58
+ const { instance } = await WebAssembly.instantiate(buffer, importsObject);
59
+ instanceExports = instance.exports;
60
+ }
61
+ } else if (input instanceof Response) { // Pre-fetched Response object
62
+ // console.debug("Using pre-fetched Response object for WASM.");
63
+ if (typeof WebAssembly.instantiateStreaming === 'function') {
64
+ const { instance } = await WebAssembly.instantiateStreaming(input, importsObject);
65
+ instanceExports = instance.exports;
66
+ } else {
67
+ const buffer = await input.arrayBuffer();
68
+ const { instance } = await WebAssembly.instantiate(buffer, importsObject);
69
+ instanceExports = instance.exports;
70
+ }
71
+ } else if (input instanceof ArrayBuffer || input instanceof Uint8Array) { // Pre-loaded BufferSource
72
+ // console.debug("Using pre-loaded BufferSource for WASM.");
73
+ const { instance } = await WebAssembly.instantiate(input, importsObject);
74
+ instanceExports = instance.exports;
75
+ } else if (input instanceof WebAssembly.Module) { // Pre-compiled Module
76
+ // console.debug("Using pre-compiled WebAssembly.Module.");
77
+ const instance = await WebAssembly.instantiate(input, importsObject);
78
+ instanceExports = instance.exports;
79
+ }
80
+ else {
81
+ throw new TypeError('Invalid input type for WASM module/path to init().');
82
+ }
83
+
84
+ bgModule.__wbg_set_wasm(instanceExports);
85
+ // console.debug("WASM instance linked via __wbg_set_wasm.");
86
+
87
+ initialized = true;
88
+ wasmExports = {
89
+ SchematicWrapper: bgModule.SchematicWrapper,
90
+ BlockStateWrapper: bgModule.BlockStateWrapper,
91
+ debug_schematic: bgModule.debug_schematic,
92
+ debug_json_schematic: bgModule.debug_json_schematic,
93
+ start: bgModule.start,
94
+ BlockPosition: bgModule.BlockPosition
95
+ // Add any other exports from bgModule that you want to make available
96
+ };
97
+ return wasmExports;
98
+ }
99
+
100
+ // Re-export the named items so users can do:
101
+ // import init, { SchematicWrapper } from '.../nucleation-cdn-loader.js';
102
+ export {
103
+ SchematicWrapper,
104
+ BlockStateWrapper,
105
+ debug_schematic,
106
+ debug_json_schematic,
107
+ start,
108
+ BlockPosition
109
+ } from './nucleation_bg.js'; // Re-export directly from _bg.js
110
+ // These will become "live" after init() is called and __wbg_set_wasm populates the WASM instance.
111
+
112
+ export default init; // Default export is the init function
Binary file
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "Nano nano@schem.at"
6
6
  ],
7
7
  "description": "A high-performance Minecraft schematic parser and utility library",
8
- "version": "0.1.14",
8
+ "version": "0.1.17",
9
9
  "license": "MIT OR Apache-2.0",
10
10
  "repository": {
11
11
  "type": "git",
@@ -16,10 +16,11 @@
16
16
  "nucleation_bg.wasm",
17
17
  "nucleation_bg.js",
18
18
  "nucleation.d.ts",
19
+ "nucleation-cdn-loader.js",
19
20
  "README.md"
20
21
  ],
21
- "main": "nucleation.js",
22
- "types": "nucleation.d.ts",
22
+ "main": "./nucleation.js",
23
+ "types": "./nucleation.d.ts",
23
24
  "sideEffects": [
24
25
  "./nucleation.js",
25
26
  "./snippets/*"
@@ -32,7 +33,17 @@
32
33
  "webassembly",
33
34
  "voxel"
34
35
  ],
35
- "module": "nucleation.js",
36
+ "module": "./nucleation.js",
37
+ "exports": {
38
+ ".": {
39
+ "import": "./nucleation.js",
40
+ "types": "./nucleation.d.ts"
41
+ },
42
+ "./cdn-loader": {
43
+ "import": "./nucleation-cdn-loader.js"
44
+ },
45
+ "./package.json": "./package.json"
46
+ },
36
47
  "author": "Nano <nano@schem.at>",
37
48
  "homepage": "https://github.com/Schem-at/Nucleation"
38
49
  }