Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bacb2cb

Browse filesBrowse files
jasnelltargos
authored andcommitted
fs: refactor fs module
PR-URL: #20764 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Backport-PR-URL: #21172
1 parent db0bb52 commit bacb2cb
Copy full SHA for bacb2cb

File tree

Expand file treeCollapse file tree

12 files changed

+1143
-981
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

12 files changed

+1143
-981
lines changed
Open diff view settings
Collapse file

‎lib/fs.js‎

Copy file name to clipboardExpand all lines: lib/fs.js
+430-931Lines changed: 430 additions & 931 deletions
Large diffs are not rendered by default.
Collapse file
+108Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
'use strict';
2+
3+
const { Buffer } = require('buffer');
4+
const { FSReqWrap, close, read } = process.binding('fs');
5+
6+
const kReadFileBufferLength = 8 * 1024;
7+
8+
function readFileAfterRead(err, bytesRead) {
9+
const context = this.context;
10+
11+
if (err)
12+
return context.close(err);
13+
14+
if (bytesRead === 0)
15+
return context.close();
16+
17+
context.pos += bytesRead;
18+
19+
if (context.size !== 0) {
20+
if (context.pos === context.size)
21+
context.close();
22+
else
23+
context.read();
24+
} else {
25+
// unknown size, just read until we don't get bytes.
26+
context.buffers.push(context.buffer.slice(0, bytesRead));
27+
context.read();
28+
}
29+
}
30+
31+
function readFileAfterClose(err) {
32+
const context = this.context;
33+
const callback = context.callback;
34+
let buffer = null;
35+
36+
if (context.err || err)
37+
return callback(context.err || err);
38+
39+
try {
40+
if (context.size === 0)
41+
buffer = Buffer.concat(context.buffers, context.pos);
42+
else if (context.pos < context.size)
43+
buffer = context.buffer.slice(0, context.pos);
44+
else
45+
buffer = context.buffer;
46+
47+
if (context.encoding)
48+
buffer = buffer.toString(context.encoding);
49+
} catch (err) {
50+
return callback(err);
51+
}
52+
53+
callback(null, buffer);
54+
}
55+
56+
class ReadFileContext {
57+
constructor(callback, encoding) {
58+
this.fd = undefined;
59+
this.isUserFd = undefined;
60+
this.size = undefined;
61+
this.callback = callback;
62+
this.buffers = null;
63+
this.buffer = null;
64+
this.pos = 0;
65+
this.encoding = encoding;
66+
this.err = null;
67+
}
68+
69+
read() {
70+
let buffer;
71+
let offset;
72+
let length;
73+
74+
if (this.size === 0) {
75+
buffer = this.buffer = Buffer.allocUnsafeSlow(kReadFileBufferLength);
76+
offset = 0;
77+
length = kReadFileBufferLength;
78+
} else {
79+
buffer = this.buffer;
80+
offset = this.pos;
81+
length = Math.min(kReadFileBufferLength, this.size - this.pos);
82+
}
83+
84+
const req = new FSReqWrap();
85+
req.oncomplete = readFileAfterRead;
86+
req.context = this;
87+
88+
read(this.fd, buffer, offset, length, -1, req);
89+
}
90+
91+
close(err) {
92+
const req = new FSReqWrap();
93+
req.oncomplete = readFileAfterClose;
94+
req.context = this;
95+
this.err = err;
96+
97+
if (this.isUserFd) {
98+
process.nextTick(function tick() {
99+
req.oncomplete(null);
100+
});
101+
return;
102+
}
103+
104+
close(this.fd, req);
105+
}
106+
}
107+
108+
module.exports = ReadFileContext;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.