@skyramp/skyramp 1.3.18 → 1.3.19

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/lib.js +57 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skyramp/skyramp",
3
- "version": "1.3.18",
3
+ "version": "1.3.19",
4
4
  "description": "module for leveraging skyramp cli functionality",
5
5
  "scripts": {
6
6
  "lint": "eslint 'src/**/*.js' 'src/**/*.ts' --fix",
package/src/lib.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const koffi = require('koffi');
2
2
  const path = require('path');
3
+ const fs = require('fs');
3
4
 
4
5
  const platform = process.platform;
5
6
  const arch = process.arch;
@@ -26,7 +27,63 @@ if (platform === 'linux') {
26
27
  if (!libPath) {
27
28
  throw new Error(`Unsupported platform or architecture: ${platform}-${arch}`);
28
29
  }
30
+
29
31
  const libFullPath = path.join(__dirname, '..', 'lib', libPath);
32
+
33
+ /**
34
+ * Wait for library file to be available (handles case where npm module is used
35
+ * before library file is fully downloaded during installation).
36
+ * Uses synchronous polling since Node.js module loading is synchronous.
37
+ *
38
+ * @param {string} filePath - Path to the library file
39
+ * @param {number} timeoutMs - Maximum time to wait in milliseconds
40
+ * @param {number} intervalMs - Check interval in milliseconds
41
+ */
42
+ function waitForFileSync(filePath, timeoutMs = 180000, intervalMs = 500) {
43
+ const startTime = Date.now();
44
+ let warningShown = false;
45
+ const { execSync } = require('child_process');
46
+
47
+ while (Date.now() - startTime < timeoutMs) {
48
+ if (fs.existsSync(filePath)) {
49
+ if (warningShown) {
50
+ console.log(`Library file is now available: ${filePath}`);
51
+ }
52
+ return;
53
+ }
54
+
55
+ // Show warning on first check if file is missing
56
+ if (!warningShown) {
57
+ console.warn(
58
+ `Warning: Library file not yet available: ${filePath}\n` +
59
+ `Waiting for up to ${timeoutMs / 1000} seconds for the file to become available...\n` +
60
+ `This may happen if the module is used before installation is fully complete.`
61
+ );
62
+ warningShown = true;
63
+ }
64
+
65
+ // Synchronous sleep using platform-specific commands
66
+ try {
67
+ if (platform === 'win32') {
68
+ execSync(`timeout /t ${Math.ceil(intervalMs / 1000)} /nobreak >nul 2>&1`, { stdio: 'ignore' });
69
+ } else {
70
+ execSync(`sleep ${intervalMs / 1000}`, { stdio: 'ignore' });
71
+ }
72
+ } catch (e) {
73
+ // If sleep command fails, fall back to a simple check without delay
74
+ // This prevents the loop from running too fast
75
+ }
76
+ }
77
+
78
+ throw new Error(
79
+ `Library file not found after waiting ${timeoutMs / 1000} seconds: ${filePath}\n` +
80
+ `This may happen if the npm module is used before the native library is fully downloaded.\n` +
81
+ `Please ensure the installation completed successfully.`
82
+ );
83
+ }
84
+
85
+ // Wait for the library file to be available before loading
86
+ waitForFileSync(libFullPath, 180000); // Wait up to 3 minutes
30
87
  const lib = koffi.load(libFullPath);
31
88
 
32
89
  module.exports = lib;