@yao-pkg/pkg 6.1.0 → 6.2.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.
package/lib-es5/sea.js CHANGED
@@ -109,14 +109,22 @@ function getNodeOs(platform) {
109
109
  }
110
110
  /** Get the node arch based on target arch */
111
111
  function getNodeArch(arch) {
112
- const allowedArchs = ['x64', 'arm64', 'armv7l', 'ppc64', 's390x'];
112
+ const allowedArchs = [
113
+ 'x64',
114
+ 'arm64',
115
+ 'armv7l',
116
+ 'ppc64',
117
+ 's390x',
118
+ 'riscv64',
119
+ 'loong64',
120
+ ];
113
121
  if (!allowedArchs.includes(arch)) {
114
122
  throw new Error(`Unsupported architecture: ${arch}`);
115
123
  }
116
124
  return arch;
117
125
  }
118
126
  /** Get latest node version based on the provided partial version */
119
- async function getNodeVersion(nodeVersion) {
127
+ async function getNodeVersion(os, arch, nodeVersion) {
120
128
  // validate nodeVersion using regex. Allowed formats: 16, 16.0, 16.0.0
121
129
  const regex = /^\d{1,2}(\.\d{1,2}){0,2}$/;
122
130
  if (!regex.test(nodeVersion)) {
@@ -129,18 +137,30 @@ async function getNodeVersion(nodeVersion) {
129
137
  if (parts.length === 3) {
130
138
  return nodeVersion;
131
139
  }
132
- const response = await fetch('https://nodejs.org/dist/index.json');
140
+ let url;
141
+ switch (arch) {
142
+ case 'riscv64':
143
+ case 'loong64':
144
+ url = 'https://unofficial-builds.nodejs.org/download/release/index.json';
145
+ break;
146
+ default:
147
+ url = 'https://nodejs.org/dist/index.json';
148
+ break;
149
+ }
150
+ const response = await fetch(url);
133
151
  if (!response.ok) {
134
152
  throw new Error('Failed to fetch node versions');
135
153
  }
136
154
  const versions = await response.json();
137
- const latestVersion = versions
138
- .map((v) => v.version)
139
- .find((v) => v.startsWith(`v${nodeVersion}`));
140
- if (!latestVersion) {
155
+ const nodeOS = os === 'darwin' ? 'osx' : os;
156
+ const latestVersionAndFiles = versions
157
+ .map((v) => [v.version, v.files])
158
+ .find(([v, files]) => v.startsWith(`v${nodeVersion}`) &&
159
+ files.find((f) => f.startsWith(`${nodeOS}-${arch}`)));
160
+ if (!latestVersionAndFiles) {
141
161
  throw new Error(`Node version ${nodeVersion} not found`);
142
162
  }
143
- return latestVersion;
163
+ return latestVersionAndFiles[0];
144
164
  }
145
165
  /** Fetch, validate and extract nodejs binary. Returns a path to it */
146
166
  async function getNodejsExecutable(target, opts) {
@@ -154,12 +174,23 @@ async function getNodejsExecutable(target, opts) {
154
174
  if (opts.useLocalNode) {
155
175
  return process.execPath;
156
176
  }
157
- const nodeVersion = await getNodeVersion(target.nodeRange.replace('node', ''));
158
177
  const os = getNodeOs(target.platform);
159
178
  const arch = getNodeArch(target.arch);
179
+ const nodeVersion = await getNodeVersion(os, arch, target.nodeRange.replace('node', ''));
160
180
  const fileName = `node-${nodeVersion}-${os}-${arch}.${os === 'win' ? 'zip' : 'tar.gz'}`;
161
- const url = `https://nodejs.org/dist/${nodeVersion}/${fileName}`;
162
- const checksumUrl = `https://nodejs.org/dist/${nodeVersion}/SHASUMS256.txt`;
181
+ let url;
182
+ let checksumUrl;
183
+ switch (arch) {
184
+ case 'riscv64':
185
+ case 'loong64':
186
+ url = `https://unofficial-builds.nodejs.org/download/release/${nodeVersion}/${fileName}`;
187
+ checksumUrl = `https://unofficial-builds.nodejs.org/download/release/${nodeVersion}/SHASUMS256.txt`;
188
+ break;
189
+ default:
190
+ url = `https://nodejs.org/dist/${nodeVersion}/${fileName}`;
191
+ checksumUrl = `https://nodejs.org/dist/${nodeVersion}/SHASUMS256.txt`;
192
+ break;
193
+ }
163
194
  const downloadDir = (0, path_1.join)((0, os_1.homedir)(), '.pkg-cache', 'sea');
164
195
  // Ensure the download directory exists
165
196
  if (!(await exists(downloadDir))) {
@@ -215,6 +246,7 @@ async function sea(entryPoint, opts) {
215
246
  // create a temporary directory for the processing work
216
247
  const tmpDir = (0, path_1.join)((0, os_1.tmpdir)(), 'pkg-sea', `${Date.now()}`);
217
248
  await (0, promises_1.mkdir)(tmpDir, { recursive: true });
249
+ const previousDirectory = process.cwd();
218
250
  try {
219
251
  // change working directory to the temp directory
220
252
  process.chdir(tmpDir);
@@ -262,6 +294,7 @@ async function sea(entryPoint, opts) {
262
294
  await (0, promises_1.rm)(tmpDir, { recursive: true }).catch(() => {
263
295
  log_1.log.warn(`Failed to cleanup the temp directory ${tmpDir}`);
264
296
  });
297
+ process.chdir(previousDirectory);
265
298
  }
266
299
  }
267
300
  exports.default = sea;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yao-pkg/pkg",
3
- "version": "6.1.0",
3
+ "version": "6.2.0",
4
4
  "description": "Package your Node.js project into an executable",
5
5
  "main": "lib-es5/index.js",
6
6
  "license": "MIT",
@@ -25,7 +25,7 @@
25
25
  "@babel/generator": "^7.23.0",
26
26
  "@babel/parser": "^7.23.0",
27
27
  "@babel/types": "^7.23.0",
28
- "@yao-pkg/pkg-fetch": "3.5.17",
28
+ "@yao-pkg/pkg-fetch": "3.5.18",
29
29
  "into-stream": "^6.0.0",
30
30
  "minimist": "^1.2.6",
31
31
  "multistream": "^4.1.0",
@@ -1722,12 +1722,16 @@ function payloadFileSync(pointer) {
1722
1722
  fs.promises.stat = util.promisify(fs.stat);
1723
1723
  fs.promises.lstat = util.promisify(fs.lstat);
1724
1724
 
1725
- /*
1726
1725
  fs.promises.read = util.promisify(fs.read);
1727
1726
  fs.promises.realpath = util.promisify(fs.realpath);
1728
1727
  fs.promises.fstat = util.promisify(fs.fstat);
1728
+ fs.promises.statfs = util.promisify(fs.fstat);
1729
1729
  fs.promises.access = util.promisify(fs.access);
1730
- */
1730
+
1731
+ // TODO: all promises methods that try to edit files in snapshot should throw
1732
+ // TODO implement missing methods
1733
+ // fs.promises.readlink ?
1734
+ // fs.promises.opendir ?
1731
1735
  }
1732
1736
 
1733
1737
  // ///////////////////////////////////////////////////////////////
@@ -80,7 +80,7 @@ function humanSize(bytes) {
80
80
  return totalSize;
81
81
  }
82
82
  function wrap(obj, name) {
83
- const f = fs[name];
83
+ const f = obj[name];
84
84
  obj[name] = (...args) => {
85
85
  const args1 = Object.values(args);
86
86
  console.log(
@@ -111,6 +111,7 @@ function humanSize(bytes) {
111
111
  wrap(fs, 'open');
112
112
  wrap(fs, 'readSync');
113
113
  wrap(fs, 'read');
114
+ wrap(fs, 'readFile');
114
115
  wrap(fs, 'writeSync');
115
116
  wrap(fs, 'write');
116
117
  wrap(fs, 'closeSync');
@@ -131,6 +132,18 @@ function humanSize(bytes) {
131
132
  wrap(fs, 'exists');
132
133
  wrap(fs, 'accessSync');
133
134
  wrap(fs, 'access');
135
+
136
+ wrap(fs.promises, 'open');
137
+ wrap(fs.promises, 'read');
138
+ wrap(fs.promises, 'readFile');
139
+ wrap(fs.promises, 'write');
140
+ wrap(fs.promises, 'readdir');
141
+ wrap(fs.promises, 'realpath');
142
+ wrap(fs.promises, 'stat');
143
+ wrap(fs.promises, 'lstat');
144
+ wrap(fs.promises, 'fstat');
145
+ wrap(fs.promises, 'access');
146
+ wrap(fs.promises, 'copyFile');
134
147
  }
135
148
  }
136
149
  })();