-
Notifications
You must be signed in to change notification settings - Fork 585
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create read stream copy in case of file descriptor
- Loading branch information
Showing
5 changed files
with
61 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import * as fs from "fs"; | ||
|
||
import { fsCreateReadStream } from "./fsCreateReadStream"; | ||
|
||
jest.setTimeout(30000); | ||
|
||
describe(fsCreateReadStream.name, () => { | ||
const mockFileContents = fs.readFileSync(__filename, "utf8"); | ||
|
||
it("uses file descriptor if defined", (done) => { | ||
fs.promises.open(__filename, "r").then((fd) => { | ||
if ((fd as any).createReadStream) { | ||
const readStream = (fd as any).createReadStream(); | ||
const readStreamCopy = fsCreateReadStream(readStream); | ||
|
||
const chunks: Array<Buffer> = []; | ||
readStreamCopy.on("data", (chunk) => { | ||
chunks.push(chunk); | ||
}); | ||
readStreamCopy.on("end", () => { | ||
const outputFileContents = Buffer.concat(chunks).toString(); | ||
expect(outputFileContents).toEqual(mockFileContents); | ||
done(); | ||
}); | ||
} else { | ||
console.log(`Skipping createReadStream test as it's not available.`); | ||
done(); | ||
} | ||
}); | ||
}); | ||
|
||
it("uses start and end if file descriptor is not defined", (done) => { | ||
const readStream = fs.createReadStream(__filename); | ||
const readStreamCopy = fsCreateReadStream(readStream); | ||
|
||
const chunks: Array<Buffer> = []; | ||
readStreamCopy.on("data", (chunk) => { | ||
chunks.push(chunk); | ||
}); | ||
readStreamCopy.on("end", () => { | ||
const outputFileContents = Buffer.concat(chunks).toString(); | ||
expect(outputFileContents).toEqual(mockFileContents); | ||
done(); | ||
}); | ||
}); | ||
}); |
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,8 @@ | ||
import { createReadStream, ReadStream } from "fs"; | ||
|
||
export const fsCreateReadStream = (readStream: ReadStream) => | ||
createReadStream(readStream.path, { | ||
fd: (readStream as any).fd, | ||
start: (readStream as any).start, | ||
end: (readStream as any).end, | ||
}); |
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