Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
Merge branch 'main' into fix#420
Browse files Browse the repository at this point in the history
  • Loading branch information
jesec committed Feb 3, 2022
2 parents ed29344 + 37cc27e commit fcb2828
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 24 deletions.
59 changes: 38 additions & 21 deletions prelude/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -1191,23 +1191,32 @@ 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);
throw new Error('UNEXPECTED-24');
});
}

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;
}
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
};
Expand All @@ -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);
Expand Down Expand Up @@ -2154,6 +2170,8 @@ function payloadFileSync(pointer) {
// Example: /tmp/pkg/<hash>
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;
Expand All @@ -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/<hash>/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);
}

Expand Down
1 change: 1 addition & 0 deletions test/test-1130/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run-time
3 changes: 3 additions & 0 deletions test/test-1130/files/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = {};
3 changes: 3 additions & 0 deletions test/test-1130/files/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = {};
32 changes: 32 additions & 0 deletions test/test-1130/main.js
Original file line number Diff line number Diff line change
@@ -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));
6 changes: 6 additions & 0 deletions test/test-1130/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"bin": "read.js",
"pkg": {
"scripts": "files/*.js"
}
}
32 changes: 32 additions & 0 deletions test/test-1130/read.js
Original file line number Diff line number Diff line change
@@ -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()}`);
}
1 change: 1 addition & 0 deletions test/test-938-withfiletypes/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run-time
25 changes: 25 additions & 0 deletions test/test-938-withfiletypes/main.js
Original file line number Diff line number Diff line change
@@ -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));
3 changes: 3 additions & 0 deletions test/test-938-withfiletypes/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"bin": "read.js"
}
35 changes: 35 additions & 0 deletions test/test-938-withfiletypes/read.js
Original file line number Diff line number Diff line change
@@ -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');
}
});
}
});
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit fcb2828

Please sign in to comment.