rust-fetch 0.3.2 → 0.4.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/client.ts CHANGED
@@ -1,4 +1,4 @@
1
- import initWasm from 'wasm-module/pkg/wasm_module';
1
+ import initWasm from 'wasm-module';
2
2
 
3
3
  const wasmPromise = initWasm();
4
4
 
package/index.ts ADDED
@@ -0,0 +1,74 @@
1
+ import * as path from 'path';
2
+ import { readFileSync } from 'fs';
3
+ import initWasm from 'wasm-module';
4
+
5
+ // Type definitions
6
+ type WasmInstance = typeof initWasm;
7
+ type WasmInitStatus = {
8
+ isInitialized: boolean;
9
+ instance: WasmInstance | null;
10
+ error: Error | null;
11
+ };
12
+
13
+ // Custom error class
14
+ class WasmInitializationError extends Error {
15
+ constructor(message: string) {
16
+ super(message);
17
+ this.name = 'WasmInitializationError';
18
+ }
19
+ }
20
+
21
+ // Singleton state
22
+ const wasmState: WasmInitStatus = {
23
+ isInitialized: false,
24
+ instance: null,
25
+ error: null,
26
+ };
27
+
28
+ // WASM file path configuration
29
+ const WASM_PATH = path.join(
30
+ process.cwd(),
31
+ 'node_modules',
32
+ '@asemqaffaf/rust-fetch',
33
+ 'wasm-module',
34
+ 'pkg',
35
+ 'wasm_module_bg.wasm'
36
+ );
37
+
38
+ // Initialize WASM module
39
+ export async function initializeWasm(): Promise<WasmInstance> {
40
+ if (wasmState.isInitialized && wasmState.instance) {
41
+ return wasmState.instance;
42
+ }
43
+
44
+ try {
45
+ const wasmBuffer = readFileSync(WASM_PATH);
46
+ const instance = await initWasm(wasmBuffer);
47
+
48
+ wasmState.isInitialized = true;
49
+ wasmState.instance = instance;
50
+ wasmState.error = null;
51
+
52
+ return instance;
53
+ } catch (error) {
54
+ const initError = new WasmInitializationError(
55
+ `Failed to initialize WASM module: ${error instanceof Error ? error.message : 'Unknown error'}`
56
+ );
57
+ wasmState.error = initError;
58
+ throw initError;
59
+ }
60
+ }
61
+
62
+ // Export initialization functions and types
63
+ export { WasmInstance, WasmInitStatus, WasmInitializationError };
64
+ export default initializeWasm;
65
+ export * from 'wasm-module';
66
+
67
+ // Auto-initialize if this module is imported directly
68
+ if (require.main === module) {
69
+ initializeWasm().catch(error => {
70
+ console.error('Failed to auto-initialize WASM:', error);
71
+ process.exit(1);
72
+ });
73
+ }
74
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rust-fetch",
3
- "version": "0.3.2",
3
+ "version": "0.4.1",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
package/server.ts CHANGED
@@ -1,13 +1,78 @@
1
+ // import * as path from 'path';
2
+ // import { readFileSync } from 'fs';
3
+
4
+ // import initWasm from 'wasm-module';
5
+ // const wasmPath = path.join(process.cwd(), 'node_modules', '@asemqaffaf/rust-fetch', 'wasm-module', 'pkg', 'wasm_module_bg.wasm');
6
+ // export const wasmBuffer = readFileSync(wasmPath);
7
+
8
+ // export default initWasm;
9
+ // export * from 'wasm-module';
10
+
11
+ // (async ()=>{
12
+ // await initWasm(wasmBuffer);
13
+ // })()
14
+
1
15
  import * as path from 'path';
2
16
  import { readFileSync } from 'fs';
3
-
4
17
  import initWasm from 'wasm-module';
5
- const wasmPath = path.join(process.cwd(), 'node_modules', '@asemqaffaf/rust-fetch', 'wasm-module', 'pkg', 'wasm_module_bg.wasm');
6
- export const wasmBuffer = readFileSync(wasmPath);
7
18
 
8
- export default initWasm;
19
+ // Type definitions
20
+ type WasmInstance = typeof initWasm;
21
+ type WasmInitStatus = {
22
+ isInitialized: boolean;
23
+ instance: WasmInstance | unknown;
24
+ error: Error | unknown;
25
+ };
26
+
27
+ // Custom error class
28
+ class WasmInitializationError extends Error {
29
+ constructor(message: string) {
30
+ super(message);
31
+ this.name = 'WasmInitializationError';
32
+ }
33
+ }
34
+
35
+ // Singleton state
36
+ const wasmState: WasmInitStatus = {
37
+ isInitialized: false,
38
+ instance: null,
39
+ error: null,
40
+ };
41
+
42
+ // WASM file path configuration
43
+ const WASM_PATH = path.join(process.cwd(), 'node_modules', '@asemqaffaf/rust-fetch', 'wasm-module', 'pkg', 'wasm_module_bg.wasm');
44
+
45
+ // Initialize WASM module
46
+ export async function initializeWasm(): Promise<WasmInstance | unknown> {
47
+ if (wasmState.isInitialized && wasmState.instance) {
48
+ return wasmState.instance;
49
+ }
50
+
51
+ try {
52
+ const wasmBuffer = readFileSync(WASM_PATH);
53
+ const instance = await initWasm(wasmBuffer);
54
+
55
+ wasmState.isInitialized = true;
56
+ wasmState.instance = instance;
57
+ wasmState.error = null;
58
+
59
+ return instance;
60
+ } catch (error) {
61
+ const initError = new WasmInitializationError(`Failed to initialize WASM module: ${error instanceof Error ? error.message : 'Unknown error'}`);
62
+ wasmState.error = initError;
63
+ throw initError;
64
+ }
65
+ }
66
+
67
+ // Export initialization functions and types
68
+ export { WasmInstance, WasmInitStatus, WasmInitializationError };
69
+ export default initializeWasm;
9
70
  export * from 'wasm-module';
10
71
 
11
- (async ()=>{
12
- await initWasm(wasmBuffer);
13
- })()
72
+ // Auto-initialize if this module is imported directly
73
+ if (require.main === module) {
74
+ initializeWasm().catch((error) => {
75
+ console.error('Failed to auto-initialize WASM:', error);
76
+ process.exit(1);
77
+ });
78
+ }