-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
94 additions
and
2 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
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,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(""); | ||
}); | ||
}); |
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,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); | ||
}; |
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