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

Commit

Permalink
fix: bootstrap: fs readdir and readdirSync (#1130/#1299) (#1495)
Browse files Browse the repository at this point in the history
  • Loading branch information
kldzj committed Feb 1, 2022
1 parent b599720 commit 5010411
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 13 deletions.
42 changes: 29 additions & 13 deletions prelude/bootstrap.js
Expand Up @@ -1116,23 +1116,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 @@ -1149,10 +1158,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 @@ -1182,17 +1189,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 @@ -1208,13 +1220,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
1 change: 1 addition & 0 deletions test/test-1130/.gitignore
@@ -0,0 +1 @@
run-time
3 changes: 3 additions & 0 deletions test/test-1130/files/a.js
@@ -0,0 +1,3 @@
'use strict';

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

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

0 comments on commit 5010411

Please sign in to comment.