This commit is contained in:
2025-01-04 00:34:03 +01:00
parent 41829408dc
commit 0ca14bbc19
18111 changed files with 1871397 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
import { EventStreamMarshaller as UniversalEventStreamMarshaller } from "@smithy/eventstream-serde-universal";
import { iterableToReadableStream, readableStreamtoIterable } from "./utils";
export class EventStreamMarshaller {
constructor({ utf8Encoder, utf8Decoder }) {
this.universalMarshaller = new UniversalEventStreamMarshaller({
utf8Decoder,
utf8Encoder,
});
}
deserialize(body, deserializer) {
const bodyIterable = isReadableStream(body) ? readableStreamtoIterable(body) : body;
return this.universalMarshaller.deserialize(bodyIterable, deserializer);
}
serialize(input, serializer) {
const serialziedIterable = this.universalMarshaller.serialize(input, serializer);
return typeof ReadableStream === "function" ? iterableToReadableStream(serialziedIterable) : serialziedIterable;
}
}
const isReadableStream = (body) => typeof ReadableStream === "function" && body instanceof ReadableStream;

View File

@@ -0,0 +1,3 @@
export * from "./EventStreamMarshaller";
export * from "./provider";
export * from "./utils";

View File

@@ -0,0 +1,2 @@
import { EventStreamMarshaller } from "./EventStreamMarshaller";
export const eventStreamSerdeProvider = (options) => new EventStreamMarshaller(options);

View File

@@ -0,0 +1,28 @@
export const readableStreamtoIterable = (readableStream) => ({
[Symbol.asyncIterator]: async function* () {
const reader = readableStream.getReader();
try {
while (true) {
const { done, value } = await reader.read();
if (done)
return;
yield value;
}
}
finally {
reader.releaseLock();
}
},
});
export const iterableToReadableStream = (asyncIterable) => {
const iterator = asyncIterable[Symbol.asyncIterator]();
return new ReadableStream({
async pull(controller) {
const { done, value } = await iterator.next();
if (done) {
return controller.close();
}
controller.enqueue(value);
},
});
};