-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fs: add stream utilities to
FileHandle
PR-URL: #40009 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
Showing
3 changed files
with
186 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
// The following tests validate base functionality for the fs.promises | ||
// FileHandle.write method. | ||
|
||
const fs = require('fs'); | ||
const { open } = fs.promises; | ||
const path = require('path'); | ||
const tmpdir = require('../common/tmpdir'); | ||
const assert = require('assert'); | ||
const { finished } = require('stream/promises'); | ||
const { buffer } = require('stream/consumers'); | ||
const tmpDir = tmpdir.path; | ||
|
||
tmpdir.refresh(); | ||
|
||
async function validateWrite() { | ||
const filePathForHandle = path.resolve(tmpDir, 'tmp-write.txt'); | ||
const fileHandle = await open(filePathForHandle, 'w'); | ||
const buffer = Buffer.from('Hello world'.repeat(100), 'utf8'); | ||
|
||
const stream = fileHandle.createWriteStream(); | ||
stream.end(buffer); | ||
await finished(stream); | ||
|
||
const readFileData = fs.readFileSync(filePathForHandle); | ||
assert.deepStrictEqual(buffer, readFileData); | ||
} | ||
|
||
async function validateRead() { | ||
const filePathForHandle = path.resolve(tmpDir, 'tmp-read.txt'); | ||
const buf = Buffer.from('Hello world'.repeat(100), 'utf8'); | ||
|
||
fs.writeFileSync(filePathForHandle, buf); | ||
|
||
const fileHandle = await open(filePathForHandle); | ||
assert.deepStrictEqual( | ||
await buffer(fileHandle.createReadStream()), | ||
buf | ||
); | ||
} | ||
|
||
Promise.all([ | ||
validateWrite(), | ||
validateRead(), | ||
]).then(common.mustCall()); |