-
Notifications
You must be signed in to change notification settings - Fork 105
feat(util-stream): buffering of stream chunks #1523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
--- | ||
"@smithy/util-stream": minor | ||
--- | ||
|
||
utility for buffering stream chunks |
This file contains hidden or 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 hidden or 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,37 @@ | ||
/** | ||
* Aggregates byteArrays on demand. | ||
* @internal | ||
*/ | ||
export class ByteArrayCollector { | ||
public byteLength = 0; | ||
private byteArrays = [] as Uint8Array[]; | ||
|
||
public constructor(public readonly allocByteArray: (size: number) => Uint8Array) {} | ||
|
||
public push(byteArray: Uint8Array) { | ||
this.byteArrays.push(byteArray); | ||
this.byteLength += byteArray.byteLength; | ||
} | ||
|
||
public flush() { | ||
if (this.byteArrays.length === 1) { | ||
const bytes = this.byteArrays[0]; | ||
this.reset(); | ||
return bytes; | ||
} | ||
const aggregation = this.allocByteArray(this.byteLength); | ||
let cursor = 0; | ||
for (let i = 0; i < this.byteArrays.length; ++i) { | ||
const bytes = this.byteArrays[i]; | ||
aggregation.set(bytes, cursor); | ||
cursor += bytes.byteLength; | ||
} | ||
this.reset(); | ||
return aggregation; | ||
} | ||
|
||
private reset() { | ||
this.byteArrays = []; | ||
this.byteLength = 0; | ||
} | ||
} |
This file contains hidden or 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,76 @@ | ||
import { Readable } from "node:stream"; | ||
import { describe, expect, test as it, vi } from "vitest"; | ||
|
||
import { createBufferedReadable } from "./createBufferedReadable"; | ||
import { headStream } from "./headStream"; | ||
|
||
describe("Buffered Readable stream", () => { | ||
function stringStream(size: number, chunkSize: number) { | ||
async function* generate() { | ||
while (size > 0) { | ||
yield "a".repeat(chunkSize); | ||
size -= chunkSize; | ||
} | ||
} | ||
return Readable.from(generate()); | ||
} | ||
function byteStream(size: number, chunkSize: number) { | ||
async function* generate() { | ||
while (size > 0) { | ||
yield Buffer.from(new Uint8Array(chunkSize)); | ||
size -= chunkSize; | ||
} | ||
} | ||
return Readable.from(generate()); | ||
} | ||
const logger = { | ||
debug: vi.fn(), | ||
info: vi.fn(), | ||
warn: vi.fn(), | ||
error() {}, | ||
}; | ||
|
||
it("should join upstream chunks if they are too small (stringStream)", async () => { | ||
const upstream = stringStream(1024, 8); | ||
const downstream = createBufferedReadable(upstream, 64); | ||
|
||
let upstreamChunkCount = 0; | ||
upstream.on("data", () => { | ||
upstreamChunkCount += 1; | ||
}); | ||
|
||
let downstreamChunkCount = 0; | ||
downstream.on("data", () => { | ||
downstreamChunkCount += 1; | ||
}); | ||
|
||
await headStream(downstream, Infinity); | ||
|
||
expect(upstreamChunkCount).toEqual(128); | ||
expect(downstreamChunkCount).toEqual(16); | ||
}); | ||
|
||
it("should join upstream chunks if they are too small (byteStream)", async () => { | ||
const upstream = byteStream(1031, 7); | ||
const downstream = createBufferedReadable(upstream, 49, logger); | ||
|
||
let upstreamChunkCount = 0; | ||
upstream.on("data", () => { | ||
upstreamChunkCount += 1; | ||
}); | ||
|
||
let downstreamChunkCount = 0; | ||
downstream.on("data", () => { | ||
downstreamChunkCount += 1; | ||
}); | ||
|
||
await headStream(downstream, Infinity); | ||
|
||
expect(Math.ceil(1031 / 7)).toBe(148); | ||
expect(Math.ceil(1031 / 49)).toBe(22); | ||
|
||
expect(upstreamChunkCount).toEqual(148); | ||
expect(downstreamChunkCount).toEqual(22); | ||
expect(logger.warn).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains hidden or 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,78 @@ | ||
import type { Logger } from "@smithy/types"; | ||
import { Readable } from "node:stream"; | ||
|
||
import { ByteArrayCollector } from "./ByteArrayCollector"; | ||
import type { BufferStore, Modes } from "./createBufferedReadableStream"; | ||
import { createBufferedReadableStream, flush, merge, modeOf, sizeOf } from "./createBufferedReadableStream"; | ||
import { isReadableStream } from "./stream-type-check"; | ||
|
||
/** | ||
* @internal | ||
* @param upstream - any Readable or ReadableStream. | ||
* @param size - byte or character length minimum. Buffering occurs when a chunk fails to meet this value. | ||
* @param onBuffer - for emitting warnings when buffering occurs. | ||
* @returns another stream of the same data and stream class, but buffers chunks until | ||
* the minimum size is met, except for the last chunk. | ||
*/ | ||
export function createBufferedReadable(upstream: Readable, size: number, logger?: Logger): Readable; | ||
export function createBufferedReadable(upstream: ReadableStream, size: number, logger?: Logger): ReadableStream; | ||
export function createBufferedReadable( | ||
upstream: Readable | ReadableStream, | ||
size: number, | ||
logger?: Logger | ||
): Readable | ReadableStream { | ||
if (isReadableStream(upstream)) { | ||
return createBufferedReadableStream(upstream, size, logger); | ||
} | ||
const downstream = new Readable({ read() {} }); | ||
let streamBufferingLoggedWarning = false; | ||
let bytesSeen = 0; | ||
|
||
const buffers = [ | ||
"", | ||
new ByteArrayCollector((size) => new Uint8Array(size)), | ||
new ByteArrayCollector((size) => Buffer.from(new Uint8Array(size))), | ||
] as BufferStore; | ||
let mode: Modes | -1 = -1; | ||
|
||
upstream.on("data", (chunk) => { | ||
const chunkMode = modeOf(chunk); | ||
if (mode !== chunkMode) { | ||
if (mode >= 0) { | ||
downstream.push(flush(buffers, mode)); | ||
} | ||
mode = chunkMode; | ||
} | ||
if (mode === -1) { | ||
downstream.push(chunk); | ||
return; | ||
} | ||
|
||
const chunkSize = sizeOf(chunk); | ||
bytesSeen += chunkSize; | ||
const bufferSize = sizeOf(buffers[mode]); | ||
if (chunkSize >= size && bufferSize === 0) { | ||
// skip writing to the intermediate buffer | ||
// because the upstream chunk is already large enough. | ||
downstream.push(chunk); | ||
} else { | ||
// buffer and potentially flush the data downstream. | ||
const newSize = merge(buffers, mode, chunk); | ||
if (!streamBufferingLoggedWarning && bytesSeen > size * 2) { | ||
streamBufferingLoggedWarning = true; | ||
logger?.warn( | ||
`@smithy/util-stream - stream chunk size ${chunkSize} is below threshold of ${size}, automatically buffering.` | ||
); | ||
} | ||
if (newSize >= size) { | ||
downstream.push(flush(buffers, mode)); | ||
} | ||
} | ||
}); | ||
upstream.on("end", () => { | ||
downstream.push(flush(buffers, mode)); | ||
downstream.push(null); | ||
}); | ||
|
||
return downstream; | ||
} |
107 changes: 107 additions & 0 deletions
107
packages/util-stream/src/createBufferedReadableStream.browser.spec.ts
This file contains hidden or 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,107 @@ | ||
import { Readable } from "node:stream"; | ||
import { describe, expect, test as it, vi } from "vitest"; | ||
|
||
import { createBufferedReadable } from "./createBufferedReadableStream"; | ||
|
||
describe("Buffered ReadableStream", () => { | ||
function stringStream(size: number, chunkSize: number) { | ||
async function* generate() { | ||
while (size > 0) { | ||
yield "a".repeat(chunkSize); | ||
size -= chunkSize; | ||
} | ||
} | ||
return Readable.toWeb(Readable.from(generate())); | ||
} | ||
function byteStream(size: number, chunkSize: number) { | ||
async function* generate() { | ||
while (size > 0) { | ||
yield new Uint8Array(chunkSize); | ||
size -= chunkSize; | ||
} | ||
} | ||
return Readable.toWeb(Readable.from(generate())); | ||
} | ||
|
||
const logger = { | ||
debug: vi.fn(), | ||
info: vi.fn(), | ||
warn: vi.fn(), | ||
error() {}, | ||
}; | ||
|
||
it("should join upstream chunks if they are too small (stringStream)", async () => { | ||
let upstreamChunkCount = 0; | ||
let downstreamChunkCount = 0; | ||
|
||
const upstream = stringStream(1024, 8); | ||
const upstreamReader = upstream.getReader(); | ||
|
||
const midstream = new ReadableStream({ | ||
async pull(controller) { | ||
const { value, done } = await upstreamReader.read(); | ||
if (done) { | ||
controller.close(); | ||
} else { | ||
expect(value.length).toBe(8); | ||
upstreamChunkCount += 1; | ||
controller.enqueue(value); | ||
} | ||
}, | ||
}); | ||
const downstream = createBufferedReadable(midstream, 64); | ||
const reader = downstream.getReader(); | ||
|
||
while (true) { | ||
const { done, value } = await reader.read(); | ||
if (done) { | ||
break; | ||
} else { | ||
downstreamChunkCount += 1; | ||
expect(value.length).toBe(64); | ||
} | ||
} | ||
|
||
expect(upstreamChunkCount).toEqual(128); | ||
expect(downstreamChunkCount).toEqual(16); | ||
}); | ||
|
||
it("should join upstream chunks if they are too small (byteStream)", async () => { | ||
let upstreamChunkCount = 0; | ||
let downstreamChunkCount = 0; | ||
|
||
const upstream = byteStream(1031, 7); | ||
const upstreamReader = upstream.getReader(); | ||
|
||
const midstream = new ReadableStream({ | ||
async pull(controller) { | ||
const { value, done } = await upstreamReader.read(); | ||
if (done) { | ||
controller.close(); | ||
} else { | ||
expect(value.length).toBe(7); | ||
upstreamChunkCount += 1; | ||
controller.enqueue(value); | ||
} | ||
}, | ||
}); | ||
const downstream = createBufferedReadable(midstream, 49, logger); | ||
const downstreamReader = downstream.getReader(); | ||
|
||
while (true) { | ||
const { done, value } = await downstreamReader.read(); | ||
if (done) { | ||
break; | ||
} else { | ||
downstreamChunkCount += 1; | ||
if (value.byteLength > 7) { | ||
expect(value.byteLength).toBe(49); | ||
} | ||
} | ||
} | ||
|
||
expect(upstreamChunkCount).toEqual(148); | ||
expect(downstreamChunkCount).toEqual(22); | ||
expect(logger.warn).toHaveBeenCalled(); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will flush() always create a new aggregate buffer, even if there's only one chunk?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated to return single bytearray without copy