diff --git a/prelude/bootstrap.js b/prelude/bootstrap.js index df34da631..6704873e4 100644 --- a/prelude/bootstrap.js +++ b/prelude/bootstrap.js @@ -1191,6 +1191,7 @@ function payloadFileSync(pointer) { return entries.map((entry) => { const ff = path.join(path_, entry); const entity = findVirtualFileSystemEntry(ff); + if (!entity) return undefined; if (entity[STORE_BLOB] || entity[STORE_CONTENT]) return new Dirent(entry, 1); if (entity[STORE_LINKS]) return new Dirent(entry, 2); @@ -1198,16 +1199,24 @@ function payloadFileSync(pointer) { }); } - function readdirRoot(path_, cb) { + function readdirRoot(path_, options, cb) { + function addSnapshot(entries) { + if (options && options.withFileTypes) { + entries.push(new Dirent('snapshot', 2)); + } else { + entries.push('snapshot'); + } + } + if (cb) { - ancestor.readdir(path_, (error, entries) => { + ancestor.readdir(path_, options, (error, entries) => { if (error) return cb(error); - entries.push('snapshot'); + addSnapshot(entries); cb(null, entries); }); } else { - const entries = ancestor.readdirSync(path_); - entries.push('snapshot'); + const entries = ancestor.readdirSync(path_, options); + addSnapshot(entries); return entries; } } @@ -1224,10 +1233,8 @@ function payloadFileSync(pointer) { } } - function readdirFromSnapshot(path_, isRoot, cb) { + function readdirFromSnapshot(path_, cb) { const cb2 = cb || rethrow; - if (isRoot) return readdirRoot(path_, cb); - const entity = findVirtualFileSystemEntry(path_); if (!entity) { @@ -1257,17 +1264,22 @@ function payloadFileSync(pointer) { if (!insideSnapshot(path_) && !isRoot) { return ancestor.readdirSync.apply(fs, arguments); } + if (insideMountpoint(path_)) { return ancestor.readdirSync.apply(fs, translateNth(arguments, 0, path_)); } const options = readdirOptions(options_, false); - if (!options || options.withFileTypes) { + if (isRoot) { + return readdirRoot(path_, options); + } + + if (!options) { return ancestor.readdirSync.apply(fs, arguments); } - let entries = readdirFromSnapshot(path_, isRoot); + let entries = readdirFromSnapshot(path_); if (options.withFileTypes) entries = getFileTypes(path_, entries); return entries; }; @@ -1283,13 +1295,17 @@ function payloadFileSync(pointer) { } const options = readdirOptions(options_, true); + const callback = dezalgo(maybeCallback(arguments)); - if (!options || options.withFileTypes) { + if (isRoot) { + return readdirRoot(path_, options, callback); + } + + if (!options) { return ancestor.readdir.apply(fs, arguments); } - const callback = dezalgo(maybeCallback(arguments)); - readdirFromSnapshot(path_, isRoot, (error, entries) => { + readdirFromSnapshot(path_, (error, entries) => { if (error) return callback(error); if (options.withFileTypes) entries = getFileTypes(path_, entries); callback(null, entries); @@ -2154,6 +2170,8 @@ function payloadFileSync(pointer) { // Example: /tmp/pkg/ const tmpFolder = path.join(tmpdir(), 'pkg', hash); + createDirRecursively(tmpFolder); + // Example: moduleFolder = /snapshot/appname/node_modules/sharp/build/Release const parts = moduleFolder.split(path.sep); const mIndex = parts.indexOf('node_modules') + 1; @@ -2168,18 +2186,17 @@ function payloadFileSync(pointer) { // Example: modulePkgFolder = /snapshot/appname/node_modules/sharp const modulePkgFolder = parts.slice(0, mIndex + 1).join(path.sep); - if (!fs.existsSync(tmpFolder)) { - // here we copy all files from the snapshot module folder to temporary folder - // we keep the module folder structure to prevent issues with modules that are statically - // linked using relative paths (Fix #1075) - createDirRecursively(tmpFolder); - copyFolderRecursiveSync(modulePkgFolder, tmpFolder); - } + // here we copy all files from the snapshot module folder to temporary folder + // we keep the module folder structure to prevent issues with modules that are statically + // linked using relative paths (Fix #1075) + copyFolderRecursiveSync(modulePkgFolder, tmpFolder); // Example: /tmp/pkg//sharp/build/Release/sharp.node newPath = path.join(tmpFolder, modulePackagePath, moduleBaseName); } else { - // simple load the file in the temporary folder + fs.copyFileSync(modulePath, path.join(tmpFolder, moduleBaseName)); + + // load the copied file in the temporary folder newPath = path.join(tmpFolder, moduleBaseName); } diff --git a/test/test-1130/.gitignore b/test/test-1130/.gitignore new file mode 100644 index 000000000..0aa2b58a1 --- /dev/null +++ b/test/test-1130/.gitignore @@ -0,0 +1 @@ +run-time \ No newline at end of file diff --git a/test/test-1130/files/a.js b/test/test-1130/files/a.js new file mode 100644 index 000000000..8b46fbbaa --- /dev/null +++ b/test/test-1130/files/a.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = {}; diff --git a/test/test-1130/files/b.js b/test/test-1130/files/b.js new file mode 100644 index 000000000..8b46fbbaa --- /dev/null +++ b/test/test-1130/files/b.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = {}; diff --git a/test/test-1130/main.js b/test/test-1130/main.js new file mode 100644 index 000000000..811b673b4 --- /dev/null +++ b/test/test-1130/main.js @@ -0,0 +1,32 @@ +#!/usr/bin/env node + +// Thanks to @roberttod +// https://github.com/vercel/pkg/blob/59b1afdb39613777150c17f77b45595864ba072e/test/test-1103-readdirsync-withfiletypes/main.js + +'use strict'; + +const path = require('path'); +const assert = require('assert'); +const utils = require('../utils.js'); + +assert(!module.parent); +assert(__dirname === process.cwd()); + +const target = process.argv[2] || 'host'; +const input = './read.js'; +const output = './run-time/test-output.exe'; + +utils.mkdirp.sync(path.dirname(output)); +utils.pkg.sync(['--target', target, '--output', output, '.']); + +let left, right; +left = utils.spawn.sync('node', [path.basename(input)], { + cwd: path.dirname(input), +}); + +right = utils.spawn.sync(output, [], { + cwd: path.dirname(input), +}); + +assert.strictEqual(left, right); +utils.vacuum.sync(path.dirname(output)); diff --git a/test/test-1130/package.json b/test/test-1130/package.json new file mode 100644 index 000000000..be8e3ed22 --- /dev/null +++ b/test/test-1130/package.json @@ -0,0 +1,6 @@ +{ + "bin": "read.js", + "pkg": { + "scripts": "files/*.js" + } +} diff --git a/test/test-1130/read.js b/test/test-1130/read.js new file mode 100644 index 000000000..d80912bdc --- /dev/null +++ b/test/test-1130/read.js @@ -0,0 +1,32 @@ +'use strict'; + +// Thanks to @roberttod +// https://github.com/vercel/pkg/blob/59b1afdb39613777150c17f77b45595864ba072e/test/test-1103-readdirsync-withfiletypes/read.js + +const fs = require('fs'); +const path = require('path'); + +console.log('Starting sync read'); + +console.log( + serializeFiles( + fs.readdirSync(path.join(__dirname, 'files'), { withFileTypes: true }) + ) +); + +console.log('Finishing sync read'); + +console.log('Starting async read'); + +fs.readdir( + path.join(__dirname, 'files'), + { withFileTypes: true }, + (_err, files) => { + console.log(serializeFiles(files)); + console.log('Finishing async read'); + } +); + +function serializeFiles(files) { + return files.map((file) => `name: ${file.name}, isFile: ${file.isFile()}`); +} diff --git a/test/test-938-withfiletypes/.gitignore b/test/test-938-withfiletypes/.gitignore new file mode 100644 index 000000000..0aa2b58a1 --- /dev/null +++ b/test/test-938-withfiletypes/.gitignore @@ -0,0 +1 @@ +run-time \ No newline at end of file diff --git a/test/test-938-withfiletypes/main.js b/test/test-938-withfiletypes/main.js new file mode 100644 index 000000000..220f2e530 --- /dev/null +++ b/test/test-938-withfiletypes/main.js @@ -0,0 +1,25 @@ +#!/usr/bin/env node + +'use strict'; + +const path = require('path'); +const assert = require('assert'); +const utils = require('../utils.js'); + +assert(!module.parent); +assert(__dirname === process.cwd()); + +const target = process.argv[2] || 'host'; +const input = './read.js'; +const output = './run-time/test-output.exe'; + +utils.mkdirp.sync(path.dirname(output)); +utils.pkg.sync(['--target', target, '--output', output, '.']); + +let right; +right = utils.spawn.sync(output, [], { + cwd: path.dirname(input), +}); + +assert.strictEqual('ok\n', right); +utils.vacuum.sync(path.dirname(output)); diff --git a/test/test-938-withfiletypes/package.json b/test/test-938-withfiletypes/package.json new file mode 100644 index 000000000..c8b48b197 --- /dev/null +++ b/test/test-938-withfiletypes/package.json @@ -0,0 +1,3 @@ +{ + "bin": "read.js" +} diff --git a/test/test-938-withfiletypes/read.js b/test/test-938-withfiletypes/read.js new file mode 100644 index 000000000..af3a8477b --- /dev/null +++ b/test/test-938-withfiletypes/read.js @@ -0,0 +1,35 @@ +'use strict'; + +const fs = require('fs'); + +function isLastEntryString(files) { + const last = files[files.length - 1]; + return typeof last === 'string'; +} + +function isLastEntryDirent(files) { + const last = files[files.length - 1]; + return ( + typeof last === 'object' && + typeof last.name === 'string' && + typeof last.type === 'number' + ); +} + +// readdir root and verify that last pushed entry (snapshot in the binary) has the expected type + +// expect Dirent array +const a = fs.readdirSync('/', { encoding: 'utf8', withFileTypes: true }); +fs.readdir('/', { encoding: 'utf8', withFileTypes: true }, (err, b) => { + if (err) throw err; + if (isLastEntryDirent(a) && isLastEntryDirent(b)) { + // expect string array + const c = fs.readdirSync('/', { encoding: 'utf8', withFileTypes: false }); + fs.readdir('/', { encoding: 'utf8', withFileTypes: false }, (err_, d) => { + if (err_) throw err_; + if (isLastEntryString(c) && isLastEntryString(d)) { + console.log('ok'); + } + }); + } +}); diff --git a/yarn.lock b/yarn.lock index 8d9d46565..d7bf6bcb5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1859,9 +1859,9 @@ node-abi@^2.21.0: semver "^5.4.1" node-fetch@^2.6.6: - version "2.6.6" - resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.6.tgz#1751a7c01834e8e1697758732e9efb6eeadfaf89" - integrity sha512-Z8/6vRlTUChSdIgMa51jxQ4lrw/Jy5SOW10ObaA47/RElsAN2c5Pn8bTgFGWn/ibwzXTE8qwr1Yzx28vsecXEA== + version "2.6.7" + resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad" + integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ== dependencies: whatwg-url "^5.0.0"