Skip to content

Commit c04a2df

Browse files
aduh95danielleadams
authored andcommitted
fs: refactor to use more primordials
PR-URL: #36196 Reviewed-By: James M Snell <[email protected]>
1 parent a9ac86d commit c04a2df

File tree

9 files changed

+104
-68
lines changed

9 files changed

+104
-68
lines changed

lib/fs.js

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,22 @@ const kIoMaxLength = 2 ** 31 - 1;
3333
// in case they are created but never used due to an exception.
3434

3535
const {
36-
Map,
36+
ArrayPrototypePush,
37+
BigIntPrototypeToString,
3738
MathMax,
3839
Number,
3940
NumberIsSafeInteger,
4041
ObjectCreate,
4142
ObjectDefineProperties,
4243
ObjectDefineProperty,
4344
Promise,
45+
ReflectApply,
46+
RegExpPrototypeExec,
47+
SafeMap,
4448
String,
49+
StringPrototypeCharCodeAt,
50+
StringPrototypeIndexOf,
51+
StringPrototypeSlice,
4552
} = primordials;
4653

4754
const { fs: constants } = internalBinding('constants');
@@ -329,7 +336,7 @@ function readFile(path, options, callback) {
329336
}
330337
if (context.isUserFd) {
331338
process.nextTick(function tick(context) {
332-
readFileAfterOpen.call({ context }, null, path);
339+
ReflectApply(readFileAfterOpen, { context }, [null, path]);
333340
}, context);
334341
return;
335342
}
@@ -414,7 +421,7 @@ function readFileSync(path, options) {
414421
buffer = Buffer.allocUnsafe(8192);
415422
bytesRead = tryReadSync(fd, isUserFd, buffer, 0, 8192);
416423
if (bytesRead !== 0) {
417-
buffers.push(buffer.slice(0, bytesRead));
424+
ArrayPrototypePush(buffers, buffer.slice(0, bytesRead));
418425
}
419426
pos += bytesRead;
420427
} while (bytesRead !== 0);
@@ -1580,7 +1587,7 @@ function watch(filename, options, listener) {
15801587
}
15811588

15821589

1583-
const statWatchers = new Map();
1590+
const statWatchers = new SafeMap();
15841591

15851592
function watchFile(filename, options, listener) {
15861593
filename = getValidatedPath(filename);
@@ -1652,13 +1659,13 @@ if (isWindows) {
16521659
// slash.
16531660
const splitRootRe = /^(?:[a-zA-Z]:|[\\/]{2}[^\\/]+[\\/][^\\/]+)?[\\/]*/;
16541661
splitRoot = function splitRoot(str) {
1655-
return splitRootRe.exec(str)[0];
1662+
return RegExpPrototypeExec(splitRootRe, str)[0];
16561663
};
16571664
} else {
16581665
splitRoot = function splitRoot(str) {
16591666
for (let i = 0; i < str.length; ++i) {
1660-
if (str.charCodeAt(i) !== CHAR_FORWARD_SLASH)
1661-
return str.slice(0, i);
1667+
if (StringPrototypeCharCodeAt(str, i) !== CHAR_FORWARD_SLASH)
1668+
return StringPrototypeSlice(str, 0, i);
16621669
}
16631670
return str;
16641671
};
@@ -1679,7 +1686,7 @@ let nextPart;
16791686
if (isWindows) {
16801687
nextPart = function nextPart(p, i) {
16811688
for (; i < p.length; ++i) {
1682-
const ch = p.charCodeAt(i);
1689+
const ch = StringPrototypeCharCodeAt(p, i);
16831690

16841691
// Check for a separator character
16851692
if (ch === CHAR_BACKWARD_SLASH || ch === CHAR_FORWARD_SLASH)
@@ -1688,7 +1695,9 @@ if (isWindows) {
16881695
return -1;
16891696
};
16901697
} else {
1691-
nextPart = function nextPart(p, i) { return p.indexOf('/', i); };
1698+
nextPart = function nextPart(p, i) {
1699+
return StringPrototypeIndexOf(p, '/', i);
1700+
};
16921701
}
16931702

16941703
const emptyObj = ObjectCreate(null);
@@ -1740,13 +1749,13 @@ function realpathSync(p, options) {
17401749
const result = nextPart(p, pos);
17411750
previous = current;
17421751
if (result === -1) {
1743-
const last = p.slice(pos);
1752+
const last = StringPrototypeSlice(p, pos);
17441753
current += last;
17451754
base = previous + last;
17461755
pos = p.length;
17471756
} else {
1748-
current += p.slice(pos, result + 1);
1749-
base = previous + p.slice(pos, result);
1757+
current += StringPrototypeSlice(p, pos, result + 1);
1758+
base = previous + StringPrototypeSlice(p, pos, result);
17501759
pos = result + 1;
17511760
}
17521761

@@ -1783,8 +1792,8 @@ function realpathSync(p, options) {
17831792
let linkTarget = null;
17841793
let id;
17851794
if (!isWindows) {
1786-
const dev = stats[0].toString(32);
1787-
const ino = stats[7].toString(32);
1795+
const dev = BigIntPrototypeToString(stats[0], 32);
1796+
const ino = BigIntPrototypeToString(stats[7], 32);
17881797
id = `${dev}:${ino}`;
17891798
if (seenLinks[id]) {
17901799
linkTarget = seenLinks[id];
@@ -1804,7 +1813,7 @@ function realpathSync(p, options) {
18041813
}
18051814

18061815
// Resolve the link, then start over
1807-
p = pathModule.resolve(resolvedLink, p.slice(pos));
1816+
p = pathModule.resolve(resolvedLink, StringPrototypeSlice(p, pos));
18081817

18091818
// Skip over roots
18101819
current = base = splitRoot(p);
@@ -1883,13 +1892,13 @@ function realpath(p, options, callback) {
18831892
const result = nextPart(p, pos);
18841893
previous = current;
18851894
if (result === -1) {
1886-
const last = p.slice(pos);
1895+
const last = StringPrototypeSlice(p, pos);
18871896
current += last;
18881897
base = previous + last;
18891898
pos = p.length;
18901899
} else {
1891-
current += p.slice(pos, result + 1);
1892-
base = previous + p.slice(pos, result);
1900+
current += StringPrototypeSlice(p, pos, result + 1);
1901+
base = previous + StringPrototypeSlice(p, pos, result);
18931902
pos = result + 1;
18941903
}
18951904

@@ -1919,8 +1928,8 @@ function realpath(p, options, callback) {
19191928
// `dev`/`ino` always return 0 on windows, so skip the check.
19201929
let id;
19211930
if (!isWindows) {
1922-
const dev = stats.dev.toString(32);
1923-
const ino = stats.ino.toString(32);
1931+
const dev = BigIntPrototypeToString(stats.dev, 32);
1932+
const ino = BigIntPrototypeToString(stats.ino, 32);
19241933
id = `${dev}:${ino}`;
19251934
if (seenLinks[id]) {
19261935
return gotTarget(null, seenLinks[id]);
@@ -1944,7 +1953,7 @@ function realpath(p, options, callback) {
19441953

19451954
function gotResolvedLink(resolvedLink) {
19461955
// Resolve the link, then start over
1947-
p = pathModule.resolve(resolvedLink, p.slice(pos));
1956+
p = pathModule.resolve(resolvedLink, StringPrototypeSlice(p, pos));
19481957
current = base = splitRoot(p);
19491958
pos = current.length;
19501959

lib/internal/fs/dir.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
'use strict';
22

33
const {
4+
ArrayPrototypePush,
5+
ArrayPrototypeSlice,
6+
ArrayPrototypeSplice,
7+
FunctionPrototypeBind,
48
ObjectDefineProperty,
59
Symbol,
610
SymbolAsyncIterator,
@@ -61,9 +65,10 @@ class Dir {
6165

6266
validateUint32(this[kDirOptions].bufferSize, 'options.bufferSize', true);
6367

64-
this[kDirReadPromisified] =
65-
internalUtil.promisify(this[kDirReadImpl]).bind(this, false);
66-
this[kDirClosePromisified] = internalUtil.promisify(this.close).bind(this);
68+
this[kDirReadPromisified] = FunctionPrototypeBind(
69+
internalUtil.promisify(this[kDirReadImpl]), this, false);
70+
this[kDirClosePromisified] = FunctionPrototypeBind(
71+
internalUtil.promisify(this.close), this);
6772
}
6873

6974
get path() {
@@ -86,14 +91,15 @@ class Dir {
8691
}
8792

8893
if (this[kDirOperationQueue] !== null) {
89-
this[kDirOperationQueue].push(() => {
94+
ArrayPrototypePush(this[kDirOperationQueue], () => {
9095
this[kDirReadImpl](maybeSync, callback);
9196
});
9297
return;
9398
}
9499

95100
if (this[kDirBufferedEntries].length > 0) {
96-
const [ name, type ] = this[kDirBufferedEntries].splice(0, 2);
101+
const [ name, type ] =
102+
ArrayPrototypeSplice(this[kDirBufferedEntries], 0, 2);
97103
if (maybeSync)
98104
process.nextTick(getDirent, this[kDirPath], name, type, callback);
99105
else
@@ -113,7 +119,7 @@ class Dir {
113119
return callback(err, result);
114120
}
115121

116-
this[kDirBufferedEntries] = result.slice(2);
122+
this[kDirBufferedEntries] = ArrayPrototypeSlice(result, 2);
117123
getDirent(this[kDirPath], result[0], result[1], callback);
118124
};
119125

@@ -135,7 +141,8 @@ class Dir {
135141
}
136142

137143
if (this[kDirBufferedEntries].length > 0) {
138-
const [ name, type ] = this[kDirBufferedEntries].splice(0, 2);
144+
const [ name, type ] =
145+
ArrayPrototypeSplice(this[kDirBufferedEntries], 0, 2);
139146
return getDirent(this[kDirPath], name, type);
140147
}
141148

@@ -152,7 +159,7 @@ class Dir {
152159
return result;
153160
}
154161

155-
this[kDirBufferedEntries] = result.slice(2);
162+
this[kDirBufferedEntries] = ArrayPrototypeSlice(result, 2);
156163
return getDirent(this[kDirPath], result[0], result[1]);
157164
}
158165

lib/internal/fs/promises.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ const kReadFileMaxChunkSize = 2 ** 14;
1010
const kWriteFileMaxChunkSize = 2 ** 14;
1111

1212
const {
13+
ArrayPrototypePush,
1314
Error,
1415
MathMax,
1516
MathMin,
1617
NumberIsSafeInteger,
1718
Promise,
1819
PromisePrototypeFinally,
20+
PromisePrototypeThen,
1921
PromiseResolve,
2022
Symbol,
2123
Uint8Array,
@@ -178,18 +180,21 @@ class FileHandle extends JSTransferable {
178180
this[kRefs]--;
179181
if (this[kRefs] === 0) {
180182
this[kFd] = -1;
181-
this[kClosePromise] = this[kHandle].close().finally(() => {
182-
this[kClosePromise] = undefined;
183-
});
183+
this[kClosePromise] = PromisePrototypeFinally(
184+
this[kHandle].close(),
185+
() => { this[kClosePromise] = undefined; }
186+
);
184187
} else {
185-
this[kClosePromise] = new Promise((resolve, reject) => {
186-
this[kCloseResolve] = resolve;
187-
this[kCloseReject] = reject;
188-
}).finally(() => {
189-
this[kClosePromise] = undefined;
190-
this[kCloseReject] = undefined;
191-
this[kCloseResolve] = undefined;
192-
});
188+
this[kClosePromise] = PromisePrototypeFinally(
189+
new Promise((resolve, reject) => {
190+
this[kCloseResolve] = resolve;
191+
this[kCloseReject] = reject;
192+
}), () => {
193+
this[kClosePromise] = undefined;
194+
this[kCloseReject] = undefined;
195+
this[kCloseResolve] = undefined;
196+
}
197+
);
193198
}
194199

195200
return this[kClosePromise];
@@ -243,9 +248,11 @@ async function fsCall(fn, handle, ...args) {
243248
handle[kRefs]--;
244249
if (handle[kRefs] === 0) {
245250
handle[kFd] = -1;
246-
handle[kHandle]
247-
.close()
248-
.then(handle[kCloseResolve], handle[kCloseReject]);
251+
PromisePrototypeThen(
252+
handle[kHandle].close(),
253+
handle[kCloseResolve],
254+
handle[kCloseReject]
255+
);
249256
}
250257
}
251258
}
@@ -307,7 +314,7 @@ async function readFileHandle(filehandle, options) {
307314
await read(filehandle, buf, 0, chunkSize, -1);
308315
endOfFile = bytesRead === 0;
309316
if (bytesRead > 0)
310-
chunks.push(buffer.slice(0, bytesRead));
317+
ArrayPrototypePush(chunks, buffer.slice(0, bytesRead));
311318
} while (!endOfFile);
312319

313320
const result = Buffer.concat(chunks);

lib/internal/fs/read_file_context.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
'use strict';
22

33
const {
4+
ArrayPrototypePush,
45
MathMin,
6+
ReflectApply,
57
} = primordials;
68

79
const { Buffer } = require('buffer');
@@ -42,7 +44,7 @@ function readFileAfterRead(err, bytesRead) {
4244
// Unknown size, just read until we don't get bytes.
4345
const buffer = bytesRead === kReadFileUnknownBufferLength ?
4446
context.buffer : context.buffer.slice(0, bytesRead);
45-
context.buffers.push(buffer);
47+
ArrayPrototypePush(context.buffers, buffer);
4648
}
4749
context.read();
4850
}
@@ -118,7 +120,7 @@ class ReadFileContext {
118120
close(err) {
119121
if (this.isUserFd) {
120122
process.nextTick(function tick(context) {
121-
readFileAfterClose.call({ context }, null);
123+
ReflectApply(readFileAfterClose, { context }, [null]);
122124
}, this);
123125
return;
124126
}

lib/internal/fs/rimraf.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
'use strict';
88

99
const {
10+
ArrayPrototypeForEach,
1011
Promise,
11-
Set,
12+
SafeSet,
1213
} = primordials;
1314

1415
const { Buffer } = require('buffer');
@@ -30,8 +31,8 @@ const {
3031
const { sep } = require('path');
3132
const { setTimeout } = require('timers');
3233
const { sleep } = require('internal/util');
33-
const notEmptyErrorCodes = new Set(['ENOTEMPTY', 'EEXIST', 'EPERM']);
34-
const retryErrorCodes = new Set(
34+
const notEmptyErrorCodes = new SafeSet(['ENOTEMPTY', 'EEXIST', 'EPERM']);
35+
const retryErrorCodes = new SafeSet(
3536
['EBUSY', 'EMFILE', 'ENFILE', 'ENOTEMPTY', 'EPERM']);
3637
const isWindows = process.platform === 'win32';
3738
const epermHandler = isWindows ? fixWinEPERM : _rmdir;
@@ -139,7 +140,7 @@ function _rmchildren(path, options, callback) {
139140

140141
let done = false;
141142

142-
files.forEach((child) => {
143+
ArrayPrototypeForEach(files, (child) => {
143144
const childPath = Buffer.concat([pathBuf, separator, child]);
144145

145146
rimraf(childPath, options, (err) => {
@@ -240,7 +241,7 @@ function _rmdirSync(path, options, originalErr) {
240241
// around that issue by retrying on Windows.
241242
const pathBuf = Buffer.from(path);
242243

243-
readdirSync(pathBuf, readdirEncoding).forEach((child) => {
244+
ArrayPrototypeForEach(readdirSync(pathBuf, readdirEncoding), (child) => {
244245
const childPath = Buffer.concat([pathBuf, separator, child]);
245246

246247
rimrafSync(childPath, options);

0 commit comments

Comments
 (0)