Skip to content

Commit

Permalink
Add collect-stream-body
Browse files Browse the repository at this point in the history
  • Loading branch information
srchase committed Jun 22, 2023
1 parent 00b7150 commit 3cf4f2e
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/smithy-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dependencies": {
"@smithy/middleware-stack": "workspace:^",
"@smithy/types": "workspace:^",
"@smithy/util-stream": "workspace:^",
"tslib": "^2.5.0"
},
"engines": {
Expand Down
49 changes: 49 additions & 0 deletions packages/smithy-client/src/collect-stream-body.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Uint8ArrayBlobAdapter } from "@smithy/util-stream";

import { collectBody } from "./collect-stream-body";

describe(collectBody.name, () => {
it("passes through Uint8Array", async () => {
const body = new Uint8Array();
const arr = await collectBody(body, {
async streamCollector(stream: any) {
return new Uint8Array(stream);
},
});

expect(arr).toBe(body);
});

it("uses the contextual streamCollector", async () => {
const body = "x";
const arr = await collectBody(body, {
async streamCollector(stream: any) {
return Uint8ArrayBlobAdapter.fromString(stream);
},
});

expect(arr.transformToString()).toEqual("x");
});

it("uses the contextual streamCollector for empty string", async () => {
const body = "";
const arr = await collectBody(body, {
async streamCollector(stream: any) {
return Uint8ArrayBlobAdapter.fromString(stream);
},
});

expect(arr.transformToString()).toEqual("");
});

it("defaults to an empty Uint8Array", async () => {
const body = null;
const arr = await collectBody(body, {
async streamCollector(stream: any) {
return Uint8ArrayBlobAdapter.fromString(stream);
},
});

expect(arr.transformToString()).toEqual("");
});
});
26 changes: 26 additions & 0 deletions packages/smithy-client/src/collect-stream-body.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { SerdeContext } from "@smithy/types";
import { Uint8ArrayBlobAdapter } from "@smithy/util-stream";

/**
* @internal
*
* Collect low-level response body stream to Uint8Array.
*/
export const collectBody = async (
streamBody: any = new Uint8Array(),
context: {
streamCollector: SerdeContext["streamCollector"];
}
): Promise<Uint8ArrayBlobAdapter> => {
if (streamBody instanceof Uint8Array) {
return Uint8ArrayBlobAdapter.mutate(streamBody);
}

if (!streamBody) {
return Uint8ArrayBlobAdapter.mutate(new Uint8Array());
}

const fromContext = context.streamCollector(streamBody);

return Uint8ArrayBlobAdapter.mutate(await fromContext);
};
1 change: 1 addition & 0 deletions packages/smithy-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./NoOpLogger";
export * from "./client";
export * from "./collect-stream-body";
export * from "./command";
export * from "./constants";
export * from "./create-aggregated-client";
Expand Down
3 changes: 2 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2263,6 +2263,7 @@ __metadata:
dependencies:
"@smithy/middleware-stack": "workspace:^"
"@smithy/types": "workspace:^"
"@smithy/util-stream": "workspace:^"
"@tsconfig/recommended": 1.0.1
"@types/node": ^14.14.31
concurrently: 7.0.0
Expand Down Expand Up @@ -2517,7 +2518,7 @@ __metadata:
languageName: unknown
linkType: soft

"@smithy/util-stream@workspace:packages/util-stream":
"@smithy/util-stream@workspace:^, @smithy/util-stream@workspace:packages/util-stream":
version: 0.0.0-use.local
resolution: "@smithy/util-stream@workspace:packages/util-stream"
dependencies:
Expand Down

0 comments on commit 3cf4f2e

Please sign in to comment.