-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(hash-stream-node): support file stream in readableStreamHasher (#…
- Loading branch information
Showing
7 changed files
with
126 additions
and
3 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { createReadStream } from "fs"; | ||
import { Readable } from "stream"; | ||
|
||
import { isFileStream } from "./isFileStream"; | ||
|
||
describe(isFileStream.name, () => { | ||
describe("returns true if readablestream is fs.ReadStream", () => { | ||
it("with string path", () => { | ||
const readStream = createReadStream(__filename); | ||
expect(isFileStream(readStream)).toStrictEqual(true); | ||
}); | ||
|
||
it("with buffer path", () => { | ||
const readStream = createReadStream(Buffer.from(__filename, "utf-8")); | ||
expect(isFileStream(readStream)).toStrictEqual(true); | ||
}); | ||
|
||
it("with filehandle", async () => { | ||
const { promises } = await import("fs"); | ||
const fd = await promises.open(__filename, "r"); | ||
// @ts-expect-error createReadStream is added in v16.11.0 | ||
if (fd.createReadStream) { | ||
// @ts-expect-error createReadStream is added in v16.11.0 | ||
const readStream = fd.createReadStream(); | ||
expect(isFileStream(readStream)).toStrictEqual(true); | ||
} | ||
}); | ||
}); | ||
|
||
it("returns false if readablestream is not an fs.ReadStream", () => { | ||
const readableStream = new Readable({ read: (size) => {} }); | ||
expect(isFileStream(readableStream)).toStrictEqual(false); | ||
}); | ||
}); |
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,7 @@ | ||
import { ReadStream } from "fs"; | ||
import { Readable } from "stream"; | ||
|
||
export const isFileStream = (stream: Readable): stream is ReadStream => | ||
typeof (stream as ReadStream).path === "string" || | ||
Buffer.isBuffer((stream as ReadStream).path) || | ||
typeof (stream as any).fd === "number"; |
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