@peerbit/native-backbone 0.1.0 → 0.1.2

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.
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peerbit/native-backbone",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Native transaction backbone for Peerbit",
5
5
  "sideEffects": [
6
6
  "./dist/wasm/native_backbone.js"
package/src/index.ts CHANGED
@@ -2116,6 +2116,7 @@ type WasmModule = {
2116
2116
 
2117
2117
  let wasmModulePromise: Promise<WasmModule> | undefined;
2118
2118
  let wasmInitialized = false;
2119
+ let wasmInitPromise: Promise<void> | undefined;
2119
2120
 
2120
2121
  const loadWasm = async (): Promise<WasmModule> => {
2121
2122
  if (!wasmModulePromise) {
@@ -2127,28 +2128,31 @@ const loadWasm = async (): Promise<WasmModule> => {
2127
2128
 
2128
2129
  const wasm = await wasmModulePromise;
2129
2130
  if (!wasmInitialized) {
2130
- const processLike = (
2131
- globalThis as { process?: { versions?: { node?: string } } }
2132
- ).process;
2133
- if (processLike?.versions?.node) {
2134
- const fsPromises = "fs/promises";
2135
- const { readFile } = (await import(
2136
- /* @vite-ignore */ fsPromises
2137
- )) as typeof import("fs/promises");
2138
- const bytes = await readFile(
2139
- new URL("../wasm/native_backbone_bg.wasm", import.meta.url),
2140
- );
2141
- wasm.initSync({ module: bytes });
2142
- } else {
2143
- await wasm.default({
2144
- module_or_path: new URL(
2145
- "../wasm/native_backbone_bg.wasm",
2146
- import.meta.url,
2147
- ),
2148
- });
2149
- }
2150
- wasmInitialized = true;
2131
+ wasmInitPromise ??= (async () => {
2132
+ const processLike = (
2133
+ globalThis as { process?: { versions?: { node?: string } } }
2134
+ ).process;
2135
+ if (processLike?.versions?.node) {
2136
+ const fsPromises = "fs/promises";
2137
+ const { readFile } = (await import(
2138
+ /* @vite-ignore */ fsPromises
2139
+ )) as typeof import("fs/promises");
2140
+ const bytes = await readFile(
2141
+ new URL("../wasm/native_backbone_bg.wasm", import.meta.url),
2142
+ );
2143
+ wasm.initSync({ module: bytes });
2144
+ } else {
2145
+ await wasm.default({
2146
+ module_or_path: new URL(
2147
+ "../wasm/native_backbone_bg.wasm",
2148
+ import.meta.url,
2149
+ ),
2150
+ });
2151
+ }
2152
+ wasmInitialized = true;
2153
+ })();
2151
2154
  }
2155
+ await wasmInitPromise;
2152
2156
 
2153
2157
  return wasm;
2154
2158
  };
@@ -3082,13 +3086,30 @@ const isNotFoundError = (error: unknown): boolean => {
3082
3086
  return maybeError?.code === "ENOENT" || maybeError?.name === "NotFoundError";
3083
3087
  };
3084
3088
 
3089
+ // Non-literal specifiers keep browser bundlers (esbuild statically resolves
3090
+ // literal dynamic imports and hard-fails on node builtins) from following
3091
+ // these node-only imports; they are only reached on node runtimes.
3085
3092
  let nodeFsModule: Promise<NativeBackboneNodeFs> | undefined;
3086
- const importNodeFs = (): Promise<NativeBackboneNodeFs> =>
3087
- (nodeFsModule ??= import("node:fs/promises"));
3093
+ const importNodeFs = (): Promise<NativeBackboneNodeFs> => {
3094
+ if (!nodeFsModule) {
3095
+ const fsPromises = "node:fs/promises";
3096
+ nodeFsModule = import(
3097
+ /* @vite-ignore */ fsPromises
3098
+ ) as Promise<NativeBackboneNodeFs>;
3099
+ }
3100
+ return nodeFsModule;
3101
+ };
3088
3102
 
3089
3103
  let nodePathJoin: Promise<(...parts: string[]) => string> | undefined;
3090
- const importNodePathJoin = (): Promise<(...parts: string[]) => string> =>
3091
- (nodePathJoin ??= import("node:path").then((mod) => mod.join));
3104
+ const importNodePathJoin = (): Promise<(...parts: string[]) => string> => {
3105
+ if (!nodePathJoin) {
3106
+ const pathModule = "node:path";
3107
+ nodePathJoin = (
3108
+ import(/* @vite-ignore */ pathModule) as Promise<typeof import("path")>
3109
+ ).then((mod) => mod.join);
3110
+ }
3111
+ return nodePathJoin;
3112
+ };
3092
3113
 
3093
3114
  const validateCoordinatePersistenceName = (name: string): string => {
3094
3115
  if (