lamdera 0.19.1-1.3.2 → 0.19.3-1.5.0

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 (3) hide show
  1. package/binary.js +24 -1
  2. package/install.js +201 -1
  3. package/package.json +10 -7
package/binary.js CHANGED
@@ -29,7 +29,16 @@ module.exports = function()
29
29
  {
30
30
  if (error && error.code === 'MODULE_NOT_FOUND')
31
31
  {
32
- exitFailure(version, missingSubPackageHelp(subPackageName));
32
+ // Check if we have a previously downloaded binary from the postinstall fallback
33
+ var downloadedPath = downloadedBinPath(subPackageName, fileName);
34
+ if (fs.existsSync(downloadedPath))
35
+ {
36
+ subBinaryPath = downloadedPath;
37
+ }
38
+ else
39
+ {
40
+ exitFailure(version, missingSubPackageHelp(subPackageName));
41
+ }
33
42
  }
34
43
  else
35
44
  {
@@ -67,6 +76,20 @@ module.exports = function()
67
76
  }
68
77
 
69
78
 
79
+ // DOWNLOADED BIN PATH
80
+ //
81
+ // Returns the path where a directly-downloaded binary would be stored.
82
+ // This is used by both binary.js (to check for it) and install.js (to write it).
83
+
84
+
85
+ function downloadedBinPath(subPackageName, fileName)
86
+ {
87
+ return path.join(__dirname, 'downloaded-' + subPackageName.replace('/', '-') + '-' + fileName);
88
+ }
89
+
90
+ module.exports.downloadedBinPath = downloadedBinPath;
91
+
92
+
70
93
 
71
94
  // VERIFY PLATFORM
72
95
 
package/install.js CHANGED
@@ -1 +1,201 @@
1
- require('./binary.js')();
1
+ var fs = require('fs');
2
+ var path = require('path');
3
+ var zlib = require('zlib');
4
+ var https = require('https');
5
+ var package = require('./package.json');
6
+ var binary = require('./binary.js');
7
+
8
+
9
+
10
+ // FETCH
11
+ //
12
+ // Download a URL, following redirects. Returns a Promise that resolves to a Buffer.
13
+
14
+
15
+ function fetch(url)
16
+ {
17
+ return new Promise(function(resolve, reject)
18
+ {
19
+ https.get(url, function(res)
20
+ {
21
+ if ((res.statusCode === 301 || res.statusCode === 302) && res.headers.location)
22
+ {
23
+ return fetch(res.headers.location).then(resolve, reject);
24
+ }
25
+ if (res.statusCode !== 200)
26
+ {
27
+ return reject(new Error('Server responded with ' + res.statusCode));
28
+ }
29
+ var chunks = [];
30
+ res.on('data', function(chunk) { chunks.push(chunk); });
31
+ res.on('end', function() { resolve(Buffer.concat(chunks)); });
32
+ }).on('error', reject);
33
+ });
34
+ }
35
+
36
+
37
+
38
+ // EXTRACT FILE FROM TAR GZIP
39
+ //
40
+ // Extract a single file from a .tgz buffer using a minimal tar header parser.
41
+ // This avoids needing any external tar dependency.
42
+ // npm tarballs contain files prefixed with "package/".
43
+
44
+
45
+ function extractFileFromTarGzip(buffer, subpath)
46
+ {
47
+ try
48
+ {
49
+ buffer = zlib.unzipSync(buffer);
50
+ }
51
+ catch (err)
52
+ {
53
+ throw new Error('Invalid gzip data in archive: ' + (err && err.message || err));
54
+ }
55
+
56
+ var str = function(i, n)
57
+ {
58
+ return String.fromCharCode.apply(null, buffer.subarray(i, i + n)).replace(/\0.*$/, '');
59
+ };
60
+
61
+ var offset = 0;
62
+ subpath = 'package/' + subpath;
63
+
64
+ while (offset < buffer.length)
65
+ {
66
+ var name = str(offset, 100);
67
+ var size = parseInt(str(offset + 124, 12), 8);
68
+ offset += 512;
69
+ if (!isNaN(size))
70
+ {
71
+ if (name === subpath)
72
+ {
73
+ return buffer.subarray(offset, offset + size);
74
+ }
75
+ offset += (size + 511) & ~511;
76
+ }
77
+ }
78
+
79
+ throw new Error('Could not find ' + JSON.stringify(subpath) + ' in archive');
80
+ }
81
+
82
+
83
+
84
+ // DOWNLOAD DIRECTLY FROM NPM
85
+ //
86
+ // As a fallback when the optional dependency was not installed (e.g. due to
87
+ // npm's known bug where it prunes cross-platform optional deps from the lockfile),
88
+ // download the platform-specific package tarball directly from the npm registry
89
+ // and extract the binary from it.
90
+
91
+
92
+ async function downloadDirectlyFromNPM(subPackageName, fileName, binPath)
93
+ {
94
+ var scopelessName = subPackageName.replace('@lamdera/', '');
95
+ var url = 'https://registry.npmjs.org/' + subPackageName + '/-/' + scopelessName + '-' + package.version + '.tgz';
96
+ console.error('[lamdera] Trying to download ' + JSON.stringify(url));
97
+ try
98
+ {
99
+ fs.writeFileSync(binPath, extractFileFromTarGzip(await fetch(url), fileName));
100
+ fs.chmodSync(binPath, 0o755);
101
+ }
102
+ catch (e)
103
+ {
104
+ console.error('[lamdera] Failed to download ' + JSON.stringify(url) + ': ' + (e && e.message || e));
105
+ throw e;
106
+ }
107
+ }
108
+
109
+
110
+
111
+ // MAIN
112
+
113
+
114
+ async function checkAndPreparePackage()
115
+ {
116
+ var version = package.version.replace(/^(\d+\.\d+\.\d+)-(\d+\.\d+\.\d+).*$/, '$1');
117
+ var subPackageName = '@lamdera/compiler-' + process.platform + '-' + process.arch;
118
+ var fileName = process.platform === 'win32' ? 'lamdera.exe' : 'lamdera';
119
+
120
+ // Verify this platform is supported
121
+ if (!(subPackageName in package.optionalDependencies))
122
+ {
123
+ var situation = process.platform + '-' + process.arch;
124
+ console.error(
125
+ '-- ERROR -----------------------------------------------------------------------\n\n'
126
+ + 'The lamdera npm package does not support your platform (' + situation + ').\n\n'
127
+ + 'You can try to manually download an appropriate binary (if there is one) from:\n'
128
+ + 'https://dashboard.lamdera.app/docs/download\n\n'
129
+ + 'Or otherwise asking for help on the Lamdera Discord:\n'
130
+ + 'https://dashboard.lamdera.app/docs/discuss\n\n'
131
+ + '--------------------------------------------------------------------------------\n'
132
+ );
133
+ process.exit(1);
134
+ }
135
+
136
+ try
137
+ {
138
+ require.resolve(subPackageName + '/' + fileName);
139
+ }
140
+ catch (error)
141
+ {
142
+ if (error && error.code === 'MODULE_NOT_FOUND')
143
+ {
144
+ console.error(
145
+ '[lamdera] Failed to find package "' + subPackageName + '" on the file system\n\n'
146
+ + 'This can happen if you use the "--no-optional" flag, or if npm fails to install\n'
147
+ + 'the correct platform-specific package (a known npm bug with optional dependencies\n'
148
+ + 'and lockfiles). This install script will now attempt to download the binary directly\n'
149
+ + 'from the npm registry as a fallback.\n'
150
+ );
151
+
152
+ var binPath = binary.downloadedBinPath(subPackageName, fileName);
153
+ try
154
+ {
155
+ await downloadDirectlyFromNPM(subPackageName, fileName, binPath);
156
+ console.error('[lamdera] Successfully downloaded binary to ' + binPath);
157
+ }
158
+ catch (e)
159
+ {
160
+ console.error(
161
+ '-- ERROR -----------------------------------------------------------------------\n\n'
162
+ + 'I support your platform, but I could not find the binary package (' + subPackageName + ') for it,\n'
163
+ + 'and the fallback download from the npm registry also failed.\n\n'
164
+ + 'This can happen if you use the "--omit=optional" (or "--no-optional") npm flag.\n'
165
+ + 'The "optionalDependencies" package.json feature is used by Lamdera to install the correct\n'
166
+ + 'binary executable for your current platform. Remove that flag to use Lamdera.\n\n'
167
+ + 'This can also happen if the "node_modules" folder was copied between two operating systems\n'
168
+ + 'that need different binaries - including "virtual" operating systems like Docker and WSL.\n'
169
+ + 'If so, try installing with npm rather than copying "node_modules".\n\n'
170
+ + 'NOTE: You can avoid npm entirely by downloading directly from:\n'
171
+ + 'https://dashboard.lamdera.app/docs/download\n'
172
+ + 'All this package does is distribute a file from there.\n\n'
173
+ + '--------------------------------------------------------------------------------\n'
174
+ );
175
+ process.exit(1);
176
+ }
177
+ }
178
+ else
179
+ {
180
+ console.error(
181
+ '-- ERROR -----------------------------------------------------------------------\n\n'
182
+ + 'I had trouble requiring the binary package for your platform (' + subPackageName + '):\n\n'
183
+ + error + '\n\n'
184
+ + 'NOTE: You can avoid npm entirely by downloading directly from:\n'
185
+ + 'https://dashboard.lamdera.app/docs/download\n'
186
+ + 'All this package does is distribute a file from there.\n\n'
187
+ + '--------------------------------------------------------------------------------\n'
188
+ );
189
+ process.exit(1);
190
+ }
191
+ }
192
+ }
193
+
194
+ // Allow testing of internal functions when required as a module,
195
+ // but run main when executed directly (node install.js).
196
+ if (require.main === module)
197
+ {
198
+ checkAndPreparePackage();
199
+ }
200
+
201
+ module.exports = { fetch: fetch, extractFileFromTarGzip: extractFileFromTarGzip, downloadDirectlyFromNPM: downloadDirectlyFromNPM };
package/package.json CHANGED
@@ -1,9 +1,12 @@
1
1
  {
2
2
  "name": "lamdera",
3
- "version": "0.19.1-1.3.2",
3
+ "version": "0.19.3-1.5.0",
4
4
  "description": "npm install wrapper for Lamdera",
5
5
  "license": "BSD-3-Clause",
6
- "repository": "lamdera/compiler",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/lamdera/compiler.git"
9
+ },
7
10
  "author": "Mario Rogic",
8
11
  "type": "commonjs",
9
12
  "exports": {},
@@ -28,10 +31,10 @@
28
31
  "lamdera": "bin/lamdera"
29
32
  },
30
33
  "optionalDependencies": {
31
- "@lamdera/compiler-darwin-arm64": "0.19.1-1.3.2",
32
- "@lamdera/compiler-darwin-x64": "0.19.1-1.3.2",
33
- "@lamdera/compiler-linux-arm64": "0.19.1-1.3.2",
34
- "@lamdera/compiler-linux-x64": "0.19.1-1.3.2",
35
- "@lamdera/compiler-win32-x64": "0.19.1-1.3.2"
34
+ "@lamdera/compiler-darwin-arm64": "0.19.3-1.5.0",
35
+ "@lamdera/compiler-darwin-x64": "0.19.3-1.5.0",
36
+ "@lamdera/compiler-linux-arm64": "0.19.3-1.5.0",
37
+ "@lamdera/compiler-linux-x64": "0.19.3-1.5.0",
38
+ "@lamdera/compiler-win32-x64": "0.19.3-1.5.0"
36
39
  }
37
40
  }