-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lib-storage): improve performance by reducing buffer copies (#5078)
* feat(lib-storage): improve performance by reducing buffer copies * test(lib-storage): add e2e tests
- Loading branch information
Showing
12 changed files
with
313 additions
and
222 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,5 @@ | ||
module.exports = { | ||
preset: "ts-jest", | ||
testMatch: ["**/*.e2e.spec.ts"], | ||
bail: true, | ||
}; |
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
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 |
---|---|---|
@@ -1,29 +1,37 @@ | ||
import { Buffer } from "buffer"; | ||
import { Buffer } from "buffer"; // do not remove this import: Node.js buffer or buffer NPM module for browser. | ||
import { Readable } from "stream"; | ||
|
||
import { getChunkBuffer } from "./chunks/getChunkBuffer"; | ||
import { getChunkStream } from "./chunks/getChunkStream"; | ||
import { getChunkUint8Array } from "./chunks/getChunkUint8Array"; | ||
import { getDataReadable } from "./chunks/getDataReadable"; | ||
import { getDataReadableStream } from "./chunks/getDataReadableStream"; | ||
import { BodyDataTypes } from "./types"; | ||
import type { RawDataPart } from "./Upload"; | ||
|
||
export const getChunk = (data: BodyDataTypes, partSize: number) => { | ||
if (data instanceof Buffer) { | ||
return getChunkBuffer(data, partSize); | ||
} else if (data instanceof Readable) { | ||
export const getChunk = (data: BodyDataTypes, partSize: number): AsyncGenerator<RawDataPart, void, undefined> => { | ||
if (data instanceof Uint8Array) { | ||
// includes Buffer (extends Uint8Array) | ||
return getChunkUint8Array(data, partSize); | ||
} | ||
|
||
if (data instanceof Readable) { | ||
return getChunkStream<Readable>(data, partSize, getDataReadable); | ||
} else if (data instanceof String || typeof data === "string" || data instanceof Uint8Array) { | ||
// chunk Strings, Uint8Array. | ||
return getChunkBuffer(Buffer.from(data), partSize); | ||
} | ||
|
||
if (data instanceof String || typeof data === "string") { | ||
return getChunkUint8Array(Buffer.from(data), partSize); | ||
} | ||
|
||
if (typeof (data as any).stream === "function") { | ||
// approximate support for Blobs. | ||
return getChunkStream<ReadableStream>((data as any).stream(), partSize, getDataReadableStream); | ||
} else if (data instanceof ReadableStream) { | ||
} | ||
|
||
if (data instanceof ReadableStream) { | ||
return getChunkStream<ReadableStream>(data, partSize, getDataReadableStream); | ||
} else { | ||
throw new Error( | ||
"Body Data is unsupported format, expected data to be one of: string | Uint8Array | Buffer | Readable | ReadableStream | Blob;." | ||
); | ||
} | ||
|
||
throw new Error( | ||
"Body Data is unsupported format, expected data to be one of: string | Uint8Array | Buffer | Readable | ReadableStream | Blob;." | ||
); | ||
}; |
Oops, something went wrong.