Skip to content

Commit 5c4a5ce

Browse files
authored
readdir-options (vercel#854)
* fix isRootPath, add options_ * readdirOptions * separate entries * modify entries * mock dirent, getFileTypes * fix test-fs-layer-2 temporarily
1 parent 58f80bd commit 5c4a5ce

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-4
lines changed

prelude/bootstrap.js

+67-4
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ console.log(translateNth(["", "a+"], 0, "d:\\snapshot\\countly\\plugins-ext\\123
164164
// /////////////////////////////////////////////////////////////////
165165

166166
function isRootPath (p) {
167+
if (p === '.') p = require('path').resolve(p);
167168
return require('path').dirname(p) === p;
168169
}
169170

@@ -697,6 +698,7 @@ function payloadFileSync (pointer) {
697698

698699
var encoding = options.encoding;
699700
assertEncoding(encoding);
701+
700702
var buffer = readFileFromSnapshot(path);
701703
if (encoding) buffer = buffer.toString(encoding);
702704
return buffer;
@@ -738,6 +740,49 @@ function payloadFileSync (pointer) {
738740
// readdir ///////////////////////////////////////////////////////
739741
// ///////////////////////////////////////////////////////////////
740742

743+
function readdirOptions (options, hasCallback) {
744+
if (!options || (hasCallback && typeof options === 'function')) {
745+
return { encoding: null };
746+
} else if (typeof options === 'string') {
747+
return { encoding: options };
748+
} else if (typeof options === 'object') {
749+
return options;
750+
} else {
751+
return null;
752+
}
753+
}
754+
755+
function Dirent (name, type) {
756+
this.name = name;
757+
this.type = type;
758+
}
759+
760+
Dirent.prototype.isDirectory = function () {
761+
return this.type === 2;
762+
};
763+
764+
Dirent.prototype.isFile = function () {
765+
return this.type === 1;
766+
};
767+
768+
Dirent.prototype.isBlockDevice =
769+
Dirent.prototype.isCharacterDevice =
770+
Dirent.prototype.isSymbolicLink =
771+
Dirent.prototype.isFIFO =
772+
Dirent.prototype.isSocket = function () {
773+
return false;
774+
};
775+
776+
function getFileTypes (path_, entries) {
777+
return entries.map(function (entry) {
778+
var path = require('path').join(path_, entry);
779+
var entity = VIRTUAL_FILESYSTEM[path];
780+
if (entity[STORE_BLOB] || entity[STORE_CONTENT]) return new Dirent(entry, 1);
781+
if (entity[STORE_LINKS]) return new Dirent(entry, 2);
782+
throw new Error('UNEXPECTED-24');
783+
});
784+
}
785+
741786
function readdirRoot (path, cb) {
742787
if (cb) {
743788
ancestor.readdir(path, function (error, entries) {
@@ -780,7 +825,7 @@ function payloadFileSync (pointer) {
780825
return cb2(new Error('UNEXPECTED-25'));
781826
}
782827

783-
fs.readdirSync = function (path) {
828+
fs.readdirSync = function (path, options_) {
784829
var isRoot = isRootPath(path);
785830

786831
if (!insideSnapshot(path) && !isRoot) {
@@ -790,10 +835,18 @@ function payloadFileSync (pointer) {
790835
return ancestor.readdirSync.apply(fs, translateNth(arguments, 0, path));
791836
}
792837

793-
return readdirFromSnapshot(path, isRoot);
838+
var options = readdirOptions(options_, false);
839+
840+
if (!options) {
841+
return ancestor.readdirSync.apply(fs, arguments);
842+
}
843+
844+
var entries = readdirFromSnapshot(path, isRoot);
845+
if (options.withFileTypes) entries = getFileTypes(path, entries);
846+
return entries;
794847
};
795848

796-
fs.readdir = function (path) {
849+
fs.readdir = function (path, options_) {
797850
var isRoot = isRootPath(path);
798851

799852
if (!insideSnapshot(path) && !isRoot) {
@@ -803,8 +856,18 @@ function payloadFileSync (pointer) {
803856
return ancestor.readdir.apply(fs, translateNth(arguments, 0, path));
804857
}
805858

859+
var options = readdirOptions(options_, true);
860+
861+
if (!options) {
862+
return ancestor.readdir.apply(fs, arguments);
863+
}
864+
806865
var callback = dezalgo(maybeCallback(arguments));
807-
readdirFromSnapshot(path, isRoot, callback);
866+
readdirFromSnapshot(path, isRoot, function (error, entries) {
867+
if (error) return callback(error);
868+
if (options.withFileTypes) entries = getFileTypes(path, entries);
869+
callback(null, entries);
870+
});
808871
};
809872

810873
// ///////////////////////////////////////////////////////////////

test/test-50-fs-runtime-layer-2/main.js

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ right = right.split('\n');
5757
// right may have less lines, premature exit,
5858
// less trusted, so using left.length here
5959
for (let i = 0; i < left.length; i += 1) {
60+
// TODO remove when latest node12 lands pkg-fetch
61+
if (left[i] === 'The "fd" argument must be of type number. Received type string (\'incorrect fd as string\')' &&
62+
right[i] === 'The "fd" argument must be of type number. Received type string') continue;
6063
assert.equal(left[i], right[i]);
6164
}
6265

0 commit comments

Comments
 (0)