Skip to content

Commit 4fa7150

Browse files
TimothyGutargos
authored andcommitted
fs: support pseudofiles in promises.readFile
PR-URL: #21497Fixes: #21331 Refs: http://pubs.opengroup.org/onlinepubs/9699919799/functions/read.html Refs: https://groups.google.com/forum/#!topic/nodejs-dev/rxZ_RoH1Gn0 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 6f8ebc0 commit 4fa7150

3 files changed

Lines changed: 57 additions & 21 deletions

File tree

‎lib/internal/fs/promises.js‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ async function writeFileHandle(filehandle, data, options) {
125125
}while(remaining>0);
126126
}
127127

128+
// Note: This is different from kReadFileBufferLength used for non-promisified
129+
// fs.readFile.
130+
constkReadFileMaxChunkSize=16384;
131+
128132
asyncfunctionreadFileHandle(filehandle,options){
129133
conststatFields=awaitbinding.fstat(filehandle.fd,false,kUsePromises);
130134

@@ -135,22 +139,19 @@ async function readFileHandle(filehandle, options) {
135139
size=0;
136140
}
137141

138-
if(size===0)
139-
returnoptions.encoding ? '' : Buffer.alloc(0);
140-
141142
if(size>kMaxLength)
142143
thrownewERR_FS_FILE_TOO_LARGE(size);
143144

144145
constchunks=[];
145-
constchunkSize=Math.min(size,16384);
146-
lettotalRead=0;
146+
constchunkSize=size===0 ?
147+
kReadFileMaxChunkSize :
148+
Math.min(size,kReadFileMaxChunkSize);
147149
letendOfFile=false;
148150
do{
149151
constbuf=Buffer.alloc(chunkSize);
150152
const{ bytesRead, buffer }=
151-
awaitread(filehandle,buf,0,chunkSize,totalRead);
152-
totalRead+=bytesRead;
153-
endOfFile=bytesRead!==chunkSize;
153+
awaitread(filehandle,buf,0,chunkSize,-1);
154+
endOfFile=bytesRead===0;
154155
if(bytesRead>0)
155156
chunks.push(buffer.slice(0,bytesRead));
156157
}while(!endOfFile);

‎test/parallel/test-fs-promises-file-handle-readFile.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,22 @@ async function validateReadFile() {
2828
assert.deepStrictEqual(buffer,readFileData);
2929
}
3030

31+
asyncfunctionvalidateReadFileProc(){
32+
// Test to make sure reading a file under the /proc directory works. Adapted
33+
// from test-fs-read-file-sync-hostname.js.
34+
// Refs:
35+
// - https://groups.google.com/forum/#!topic/nodejs-dev/rxZ_RoH1Gn0
36+
// - https://github.com/nodejs/node/issues/21331
37+
38+
// Test is Linux-specific.
39+
if(!common.isLinux)
40+
return;
41+
42+
constfileHandle=awaitopen('/proc/sys/kernel/hostname','r');
43+
consthostname=awaitfileHandle.readFile();
44+
assert.ok(hostname.length>0);
45+
}
46+
3147
validateReadFile()
48+
.then(()=>validateReadFileProc())
3249
.then(common.mustCall());

‎test/parallel/test-fs-promises-readfile.js‎

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,35 @@ const fn = path.join(tmpdir.path, 'large-file');
1212

1313
common.crashOnUnhandledRejection();
1414

15-
// Creating large buffer with random content
16-
constbuffer=Buffer.from(
17-
Array.apply(null,{length: 16834*2})
18-
.map(Math.random)
19-
.map((number)=>(number*(1<<8)))
20-
);
21-
22-
// Writing buffer to a file then try to read it
23-
writeFile(fn,buffer)
24-
.then(()=>readFile(fn))
25-
.then((readBuffer)=>{
26-
assert.strictEqual(readBuffer.equals(buffer),true);
27-
})
15+
asyncfunctionvalidateReadFile(){
16+
// Creating large buffer with random content
17+
constbuffer=Buffer.from(
18+
Array.apply(null,{length: 16834*2})
19+
.map(Math.random)
20+
.map((number)=>(number*(1<<8)))
21+
);
22+
23+
// Writing buffer to a file then try to read it
24+
awaitwriteFile(fn,buffer);
25+
constreadBuffer=awaitreadFile(fn);
26+
assert.strictEqual(readBuffer.equals(buffer),true);
27+
}
28+
29+
asyncfunctionvalidateReadFileProc(){
30+
// Test to make sure reading a file under the /proc directory works. Adapted
31+
// from test-fs-read-file-sync-hostname.js.
32+
// Refs:
33+
// - https://groups.google.com/forum/#!topic/nodejs-dev/rxZ_RoH1Gn0
34+
// - https://github.com/nodejs/node/issues/21331
35+
36+
// Test is Linux-specific.
37+
if(!common.isLinux)
38+
return;
39+
40+
consthostname=awaitreadFile('/proc/sys/kernel/hostname');
41+
assert.ok(hostname.length>0);
42+
}
43+
44+
validateReadFile()
45+
.then(()=>validateReadFileProc())
2846
.then(common.mustCall());

0 commit comments

Comments
 (0)