swisseph-wasm 0.0.1 → 0.0.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swisseph-wasm",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "High-precision Swiss Ephemeris WebAssembly library for astronomical calculations in JavaScript",
5
5
  "type": "module",
6
6
  "main": "src/swisseph.js",
@@ -53,12 +53,12 @@
53
53
  "license": "GPL-3.0-or-later",
54
54
  "repository": {
55
55
  "type": "git",
56
- "url": "git+https://github.com/prolaxu/swisseph-wasm.git"
56
+ "url": "git+https://github.com/prolaxu/swiss-wasm.git"
57
57
  },
58
58
  "bugs": {
59
- "url": "https://github.com/prolaxu/swisseph-wasm/issues"
59
+ "url": "https://github.com/prolaxu/swiss-wasm/issues"
60
60
  },
61
- "homepage": "https://github.com/prolaxu/swisseph-wasm#readme",
61
+ "homepage": "https://github.com/prolaxu/swiss-wasm#readme",
62
62
  "engines": {
63
63
  "node": ">=14.0.0"
64
64
  },
package/wsam/swisseph.js CHANGED
@@ -11,8 +11,12 @@ var Swisseph = ( () => {
11
11
  readyPromiseReject = reject
12
12
  }
13
13
  );
14
- var ENVIRONMENT_IS_WEB = true;
15
- var ENVIRONMENT_IS_WORKER = false;
14
+ // Detect environment properly
15
+ var ENVIRONMENT_IS_NODE = typeof process !== 'undefined' && process.versions && process.versions.node;
16
+ var ENVIRONMENT_IS_WEB = typeof window !== 'undefined' || (typeof importScripts === 'function' && !ENVIRONMENT_IS_NODE);
17
+ var ENVIRONMENT_IS_WORKER = typeof importScripts === 'function';
18
+
19
+
16
20
  Module["expectedDataFileDownloads"] ??= 0;
17
21
  Module["expectedDataFileDownloads"]++;
18
22
  ( () => {
@@ -28,12 +32,61 @@ var Swisseph = ( () => {
28
32
  PACKAGE_PATH = encodeURIComponent(location.pathname.substring(0, location.pathname.lastIndexOf("/")) + "/")
29
33
  }
30
34
  var PACKAGE_NAME = "wsam/swisseph.data";
31
- var REMOTE_PACKAGE_BASE = import.meta.resolve("./swisseph.data");
35
+ var REMOTE_PACKAGE_BASE;
36
+
37
+ if (ENVIRONMENT_IS_NODE) {
38
+ // In Node.js, resolve relative to the current module
39
+ REMOTE_PACKAGE_BASE = new URL("./swisseph.data", import.meta.url).href;
40
+ } else {
41
+ // In browser, use import.meta.resolve if available, otherwise relative path
42
+ REMOTE_PACKAGE_BASE = import.meta.resolve ? import.meta.resolve("./swisseph.data") : "./swisseph.data";
43
+ }
44
+
32
45
  var REMOTE_PACKAGE_NAME = Module["locateFile"] ? Module["locateFile"](REMOTE_PACKAGE_BASE, "") : REMOTE_PACKAGE_BASE;
33
46
  var REMOTE_PACKAGE_SIZE = metadata["remote_package_size"];
34
47
  function fetchRemotePackage(packageName, packageSize, callback, errback) {
35
48
  Module["dataFileDownloads"] ??= {};
36
- fetch(packageName).catch(cause => Promise.reject(new Error(`Network Error: ${packageName}`,{
49
+
50
+ // Cross-platform fetch function
51
+ async function crossPlatformFetch(url) {
52
+ if (ENVIRONMENT_IS_NODE) {
53
+ // Node.js environment - use fs to read local files
54
+ try {
55
+ const { readFile } = await import('fs/promises');
56
+ const { fileURLToPath } = await import('url');
57
+ const { resolve, dirname } = await import('path');
58
+
59
+ let filePath;
60
+ if (url.startsWith('file://')) {
61
+ filePath = fileURLToPath(url);
62
+ } else if (url.startsWith('./') || url.startsWith('../')) {
63
+ // Resolve relative to current module
64
+ const currentDir = dirname(fileURLToPath(import.meta.url));
65
+ filePath = resolve(currentDir, url);
66
+ } else {
67
+ filePath = url;
68
+ }
69
+
70
+ const data = await readFile(filePath);
71
+ return {
72
+ ok: true,
73
+ arrayBuffer: () => Promise.resolve(data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength))
74
+ };
75
+ } catch (error) {
76
+ return {
77
+ ok: false,
78
+ status: 404,
79
+ url: url,
80
+ error: error
81
+ };
82
+ }
83
+ } else {
84
+ // Browser environment - use standard fetch
85
+ return fetch(url);
86
+ }
87
+ }
88
+
89
+ crossPlatformFetch(packageName).catch(cause => Promise.reject(new Error(`Network Error: ${packageName}`,{
37
90
  cause
38
91
  }))).then(response => {
39
92
  if (!response.ok) {
@@ -221,16 +274,78 @@ var Swisseph = ( () => {
221
274
  }
222
275
  {
223
276
  readAsync = async url => {
224
- var response = await fetch(url, {
225
- credentials: "same-origin"
226
- });
227
- if (response.ok) {
228
- return response.arrayBuffer()
277
+ if (ENVIRONMENT_IS_NODE) {
278
+ // Node.js environment
279
+ try {
280
+ const { readFile } = await import('fs/promises');
281
+ const { fileURLToPath } = await import('url');
282
+ const { resolve, dirname } = await import('path');
283
+
284
+ let filePath;
285
+ if (url.startsWith('file://')) {
286
+ filePath = fileURLToPath(url);
287
+ } else if (url.startsWith('./') || url.startsWith('../')) {
288
+ const currentDir = dirname(fileURLToPath(import.meta.url));
289
+ filePath = resolve(currentDir, url);
290
+ } else {
291
+ filePath = url;
292
+ }
293
+
294
+ console.log('DEBUG: readAsync Node.js file read:', { url, filePath });
295
+
296
+ console.log('DEBUG: readAsync Node.js file read:', { url, filePath });
297
+
298
+ console.log('DEBUG: Node.js file read attempt:', { url, filePath });
299
+
300
+ const data = await readFile(filePath);
301
+ console.log('DEBUG: File read successful, size:', data.length);
302
+ return data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
303
+ } catch (error) {
304
+ console.log('DEBUG: File read failed:', error.message);
305
+ throw new Error(`File read error: ${url} - ${error.message}`);
306
+ }
307
+ } else {
308
+ // Browser environment
309
+ var response = await fetch(url, {
310
+ credentials: "same-origin"
311
+ });
312
+ if (response.ok) {
313
+ return response.arrayBuffer()
314
+ }
315
+ throw new Error(response.status + " : " + response.url)
229
316
  }
230
- throw new Error(response.status + " : " + response.url)
231
317
  }
232
318
  }
233
- } else {}
319
+ } else {
320
+ // Node.js environment
321
+ if (ENVIRONMENT_IS_NODE) {
322
+ readAsync = async url => {
323
+ try {
324
+ const { readFile } = await import('fs/promises');
325
+ const { fileURLToPath } = await import('url');
326
+ const { resolve, dirname } = await import('path');
327
+
328
+ let filePath;
329
+ if (url.startsWith('file://')) {
330
+ filePath = fileURLToPath(url);
331
+ } else if (url.startsWith('./') || url.startsWith('../')) {
332
+ const currentDir = dirname(fileURLToPath(import.meta.url));
333
+ filePath = resolve(currentDir, url);
334
+ } else {
335
+ filePath = url;
336
+ }
337
+
338
+ const data = await readFile(filePath);
339
+ return data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
340
+ } catch (error) {
341
+ throw new Error(`File read error: ${url} - ${error.message}`);
342
+ }
343
+ };
344
+
345
+ // We'll set readBinary to null for now since we have readAsync
346
+ readBinary = null;
347
+ }
348
+ }
234
349
  var out = Module["print"] || console.log.bind(console);
235
350
  var err = Module["printErr"] || console.error.bind(console);
236
351
  Object.assign(Module, moduleOverrides);
@@ -328,7 +443,34 @@ var Swisseph = ( () => {
328
443
  var dataURIPrefix = "data:application/octet-stream;base64,";
329
444
  var isDataURI = filename => filename.startsWith(dataURIPrefix);
330
445
  function findWasmBinary() {
331
- var f = import.meta.resolve("./swisseph.wasm");
446
+ var f;
447
+ if (ENVIRONMENT_IS_NODE) {
448
+ // In Node.js, resolve relative to the current module
449
+ f = new URL("./swisseph.wasm", import.meta.url).href;
450
+ } else {
451
+ // In browser, try different approaches for different bundlers
452
+ if (import.meta.resolve) {
453
+ try {
454
+ f = import.meta.resolve("./swisseph.wasm");
455
+ } catch (e) {
456
+ // Fallback for bundlers that don't support import.meta.resolve
457
+ f = "./swisseph.wasm";
458
+ }
459
+ } else {
460
+ // Fallback for older browsers or bundlers
461
+ f = "./swisseph.wasm";
462
+ }
463
+
464
+ // For Vite and other dev servers, try to use a more reliable path
465
+ if (typeof window !== 'undefined' && window.location) {
466
+ const currentPath = window.location.pathname;
467
+ if (currentPath.includes('node_modules')) {
468
+ // We're likely in a dev environment, use relative path
469
+ f = "./swisseph.wasm";
470
+ }
471
+ }
472
+ }
473
+
332
474
  if (!isDataURI(f)) {
333
475
  return locateFile(f)
334
476
  }
@@ -339,9 +481,11 @@ var Swisseph = ( () => {
339
481
  if (file == wasmBinaryFile && wasmBinary) {
340
482
  return new Uint8Array(wasmBinary)
341
483
  }
484
+
342
485
  if (readBinary) {
343
486
  return readBinary(file)
344
487
  }
488
+
345
489
  throw "both async and sync fetching of the wasm failed"
346
490
  }
347
491
  async function getWasmBinary(binaryFile) {
@@ -349,7 +493,9 @@ var Swisseph = ( () => {
349
493
  try {
350
494
  var response = await readAsync(binaryFile);
351
495
  return new Uint8Array(response)
352
- } catch {}
496
+ } catch (error) {
497
+ // Fall back to sync loading
498
+ }
353
499
  }
354
500
  return getBinarySync(binaryFile)
355
501
  }
@@ -364,7 +510,7 @@ var Swisseph = ( () => {
364
510
  }
365
511
  }
366
512
  async function instantiateAsync(binary, binaryFile, imports) {
367
- if (!binary && typeof WebAssembly.instantiateStreaming == "function" && !isDataURI(binaryFile) && typeof fetch == "function") {
513
+ if (!binary && typeof WebAssembly.instantiateStreaming == "function" && !isDataURI(binaryFile) && !ENVIRONMENT_IS_NODE && typeof fetch == "function") {
368
514
  try {
369
515
  var response = fetch(binaryFile, {
370
516
  credentials: "same-origin"