diff --git a/packages/hash-stream-node/src/fsCreateReadStream.spec.ts b/packages/hash-stream-node/src/fsCreateReadStream.spec.ts deleted file mode 100644 index 83f2f4b35a652..0000000000000 --- a/packages/hash-stream-node/src/fsCreateReadStream.spec.ts +++ /dev/null @@ -1,46 +0,0 @@ -import * as fs from "fs"; - -import { fsCreateReadStream } from "./fsCreateReadStream"; - -jest.setTimeout(30000); - -describe(fsCreateReadStream.name, () => { - const mockFileContents = fs.readFileSync(__filename, "utf8"); - - it.skip("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 = []; - 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 = []; - readStreamCopy.on("data", (chunk) => { - chunks.push(chunk); - }); - readStreamCopy.on("end", () => { - const outputFileContents = Buffer.concat(chunks).toString(); - expect(outputFileContents).toEqual(mockFileContents); - done(); - }); - }); -}); diff --git a/packages/hash-stream-node/src/fsCreateReadStream.ts b/packages/hash-stream-node/src/fsCreateReadStream.ts deleted file mode 100644 index f5ec73bdbee69..0000000000000 --- a/packages/hash-stream-node/src/fsCreateReadStream.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { createReadStream, ReadStream } from "fs"; - -export const fsCreateReadStream = (readStream: ReadStream) => - createReadStream(readStream.path, { - // ToDo: Removed to fix https://github.com/aws/aws-sdk-js-v3/issues/3368 - // fd: (readStream as any).fd, - start: (readStream as any).start, - end: (readStream as any).end, - }); diff --git a/packages/hash-stream-node/src/isFileStream.spec.ts b/packages/hash-stream-node/src/isFileStream.spec.ts deleted file mode 100644 index d8985dd8f21e2..0000000000000 --- a/packages/hash-stream-node/src/isFileStream.spec.ts +++ /dev/null @@ -1,34 +0,0 @@ -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); - }); -}); diff --git a/packages/hash-stream-node/src/isFileStream.ts b/packages/hash-stream-node/src/isFileStream.ts deleted file mode 100644 index 3117afd0d9149..0000000000000 --- a/packages/hash-stream-node/src/isFileStream.ts +++ /dev/null @@ -1,7 +0,0 @@ -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"; diff --git a/packages/hash-stream-node/src/readableStreamHasher.spec.ts b/packages/hash-stream-node/src/readableStreamHasher.spec.ts index e1f27a932dbda..1a2d89b814893 100644 --- a/packages/hash-stream-node/src/readableStreamHasher.spec.ts +++ b/packages/hash-stream-node/src/readableStreamHasher.spec.ts @@ -1,16 +1,10 @@ import { Hash } from "@aws-sdk/types"; -import { createReadStream } from "fs"; import { Readable, Writable } from "stream"; -import { fsCreateReadStream } from "./fsCreateReadStream"; import { HashCalculator } from "./HashCalculator"; -import { isFileStream } from "./isFileStream"; import { readableStreamHasher } from "./readableStreamHasher"; -jest.mock("./fsCreateReadStream"); jest.mock("./HashCalculator"); -jest.mock("./isFileStream"); -jest.mock("fs"); describe(readableStreamHasher.name, () => { const mockDigest = jest.fn(); @@ -44,7 +38,6 @@ describe(readableStreamHasher.name, () => { (HashCalculator as unknown as jest.Mock).mockImplementation( (hash) => new MockHashCalculator(hash, mockHashCalculatorWrite, mockHashCalculatorEnd) ); - (isFileStream as unknown as jest.Mock).mockReturnValue(false); mockDigest.mockResolvedValue(mockHash); }); @@ -52,17 +45,6 @@ describe(readableStreamHasher.name, () => { jest.clearAllMocks(); }); - it("creates a copy in case of fileStream", () => { - (fsCreateReadStream as jest.Mock).mockReturnValue(new Readable({ read: (size) => {} })); - (isFileStream as unknown as jest.Mock).mockReturnValue(true); - - const fsReadStream = createReadStream(__filename); - readableStreamHasher(mockHashCtor, fsReadStream); - - expect(isFileStream).toHaveBeenCalledWith(fsReadStream); - expect(fsCreateReadStream).toHaveBeenCalledWith(fsReadStream); - }); - it("computes hash for a readable stream", async () => { const readableStream = new Readable({ read: (size) => {} }); const hashPromise = readableStreamHasher(mockHashCtor, readableStream); @@ -86,7 +68,7 @@ describe(readableStreamHasher.name, () => { expect(mockHashCalculatorEnd).toHaveBeenCalledTimes(1); }); - it("throws if readable stream is not a file stream and has started reading", async () => { + it("throws if readable stream has started reading", async () => { const readableStream = new Readable({ read: (size) => {} }); // Simulate readableFlowing to true. readableStream.resume(); diff --git a/packages/hash-stream-node/src/readableStreamHasher.ts b/packages/hash-stream-node/src/readableStreamHasher.ts index 985b2c4330ff1..16c56f46157c9 100644 --- a/packages/hash-stream-node/src/readableStreamHasher.ts +++ b/packages/hash-stream-node/src/readableStreamHasher.ts @@ -1,24 +1,20 @@ import { HashConstructor, StreamHasher } from "@aws-sdk/types"; import { Readable } from "stream"; -import { fsCreateReadStream } from "./fsCreateReadStream"; import { HashCalculator } from "./HashCalculator"; -import { isFileStream } from "./isFileStream"; export const readableStreamHasher: StreamHasher = (hashCtor: HashConstructor, readableStream: Readable) => { - // Throw if readableStream is already flowing and it's not a file stream. - if (!isFileStream(readableStream) && readableStream.readableFlowing !== null) { + // Throw if readableStream is already flowing. + if (readableStream.readableFlowing !== null) { throw new Error("Unable to calculate hash for flowing readable stream"); } - const streamToPipe = isFileStream(readableStream) ? fsCreateReadStream(readableStream) : readableStream; - const hash = new hashCtor(); const hashCalculator = new HashCalculator(hash); - streamToPipe.pipe(hashCalculator); + readableStream.pipe(hashCalculator); return new Promise((resolve, reject) => { - streamToPipe.on("error", (err: Error) => { + readableStream.on("error", (err: Error) => { // if the source errors, the destination stream needs to manually end hashCalculator.end(); reject(err);