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,23 @@
import { EventStreamCodec, MessageDecoderStream, MessageEncoderStream, SmithyMessageDecoderStream, SmithyMessageEncoderStream, } from "@smithy/eventstream-codec";
import { getChunkedStream } from "./getChunkedStream";
import { getMessageUnmarshaller } from "./getUnmarshalledStream";
export class EventStreamMarshaller {
constructor({ utf8Encoder, utf8Decoder }) {
this.eventStreamCodec = new EventStreamCodec(utf8Encoder, utf8Decoder);
this.utfEncoder = utf8Encoder;
}
deserialize(body, deserializer) {
const inputStream = getChunkedStream(body);
return new SmithyMessageDecoderStream({
messageStream: new MessageDecoderStream({ inputStream, decoder: this.eventStreamCodec }),
deserializer: getMessageUnmarshaller(deserializer, this.utfEncoder),
});
}
serialize(inputStream, serializer) {
return new MessageEncoderStream({
messageStream: new SmithyMessageEncoderStream({ inputStream, serializer }),
encoder: this.eventStreamCodec,
includeEndFrame: true,
});
}
}

View File

@@ -0,0 +1,30 @@
import { Readable } from "stream";
export class MockEventMessageSource extends Readable {
constructor(options) {
super(options);
this.readCount = 0;
this.data = Buffer.concat(options.messages);
this.emitSize = options.emitSize;
this.throwError = options.throwError;
}
_read() {
const self = this;
if (this.readCount === this.data.length) {
if (this.throwError) {
process.nextTick(function () {
self.emit("error", new Error("Throwing an error!"));
});
return;
}
else {
this.push(null);
return;
}
}
const bytesLeft = this.data.length - this.readCount;
const numBytesToSend = Math.min(bytesLeft, this.emitSize);
const chunk = this.data.slice(this.readCount, this.readCount + numBytesToSend);
this.readCount += numBytesToSend;
this.push(chunk);
}
}

View File

@@ -0,0 +1,4 @@
export const recordEventMessage = Buffer.from("AAAA0AAAAFX31gVLDTptZXNzYWdlLXR5cGUHAAVldmVudAs6ZXZlbnQtdHlwZQcAB1JlY29yZHMNOmNvbnRlbnQtdHlwZQcAGGFwcGxpY2F0aW9uL29jdGV0LXN0cmVhbTEsRm9vLFdoZW4gbGlmZSBnaXZlcyB5b3UgZm9vLi4uCjIsQmFyLG1ha2UgQmFyIQozLEZpenosU29tZXRpbWVzIHBhaXJlZCB3aXRoLi4uCjQsQnV6eix0aGUgaW5mYW1vdXMgQnV6eiEKzxKeSw==", "base64");
export const statsEventMessage = Buffer.from("AAAA0QAAAEM+YpmqDTptZXNzYWdlLXR5cGUHAAVldmVudAs6ZXZlbnQtdHlwZQcABVN0YXRzDTpjb250ZW50LXR5cGUHAAh0ZXh0L3htbDxTdGF0cyB4bWxucz0iIj48Qnl0ZXNTY2FubmVkPjEyNjwvQnl0ZXNTY2FubmVkPjxCeXRlc1Byb2Nlc3NlZD4xMjY8L0J5dGVzUHJvY2Vzc2VkPjxCeXRlc1JldHVybmVkPjEwNzwvQnl0ZXNSZXR1cm5lZD48L1N0YXRzPiJ0pLk=", "base64");
export const endEventMessage = Buffer.from("AAAAOAAAACjBxoTUDTptZXNzYWdlLXR5cGUHAAVldmVudAs6ZXZlbnQtdHlwZQcAA0VuZM+X05I=", "base64");
export const exception = Buffer.from("AAAAtgAAAF8BcW64DTpjb250ZW50LXR5cGUHABhhcHBsaWNhdGlvbi9vY3RldC1zdHJlYW0NOm1lc3NhZ2UtdHlwZQcACWV4Y2VwdGlvbg86ZXhjZXB0aW9uLXR5cGUHAAlFeGNlcHRpb25UaGlzIGlzIGEgbW9kZWxlZCBleGNlcHRpb24gZXZlbnQgdGhhdCB3b3VsZCBiZSB0aHJvd24gaW4gZGVzZXJpYWxpemVyLj6Gc60=", "base64");

View File

@@ -0,0 +1,66 @@
export function getChunkedStream(source) {
let currentMessageTotalLength = 0;
let currentMessagePendingLength = 0;
let currentMessage = null;
let messageLengthBuffer = null;
const allocateMessage = (size) => {
if (typeof size !== "number") {
throw new Error("Attempted to allocate an event message where size was not a number: " + size);
}
currentMessageTotalLength = size;
currentMessagePendingLength = 4;
currentMessage = new Uint8Array(size);
const currentMessageView = new DataView(currentMessage.buffer);
currentMessageView.setUint32(0, size, false);
};
const iterator = async function* () {
const sourceIterator = source[Symbol.asyncIterator]();
while (true) {
const { value, done } = await sourceIterator.next();
if (done) {
if (!currentMessageTotalLength) {
return;
}
else if (currentMessageTotalLength === currentMessagePendingLength) {
yield currentMessage;
}
else {
throw new Error("Truncated event message received.");
}
return;
}
const chunkLength = value.length;
let currentOffset = 0;
while (currentOffset < chunkLength) {
if (!currentMessage) {
const bytesRemaining = chunkLength - currentOffset;
if (!messageLengthBuffer) {
messageLengthBuffer = new Uint8Array(4);
}
const numBytesForTotal = Math.min(4 - currentMessagePendingLength, bytesRemaining);
messageLengthBuffer.set(value.slice(currentOffset, currentOffset + numBytesForTotal), currentMessagePendingLength);
currentMessagePendingLength += numBytesForTotal;
currentOffset += numBytesForTotal;
if (currentMessagePendingLength < 4) {
break;
}
allocateMessage(new DataView(messageLengthBuffer.buffer).getUint32(0, false));
messageLengthBuffer = null;
}
const numBytesToWrite = Math.min(currentMessageTotalLength - currentMessagePendingLength, chunkLength - currentOffset);
currentMessage.set(value.slice(currentOffset, currentOffset + numBytesToWrite), currentMessagePendingLength);
currentMessagePendingLength += numBytesToWrite;
currentOffset += numBytesToWrite;
if (currentMessageTotalLength && currentMessageTotalLength === currentMessagePendingLength) {
yield currentMessage;
currentMessage = null;
currentMessageTotalLength = 0;
currentMessagePendingLength = 0;
}
}
}
};
return {
[Symbol.asyncIterator]: iterator,
};
}

View File

@@ -0,0 +1,47 @@
export function getUnmarshalledStream(source, options) {
const messageUnmarshaller = getMessageUnmarshaller(options.deserializer, options.toUtf8);
return {
[Symbol.asyncIterator]: async function* () {
for await (const chunk of source) {
const message = options.eventStreamCodec.decode(chunk);
const type = await messageUnmarshaller(message);
if (type === undefined)
continue;
yield type;
}
},
};
}
export function getMessageUnmarshaller(deserializer, toUtf8) {
return async function (message) {
const { value: messageType } = message.headers[":message-type"];
if (messageType === "error") {
const unmodeledError = new Error(message.headers[":error-message"].value || "UnknownError");
unmodeledError.name = message.headers[":error-code"].value;
throw unmodeledError;
}
else if (messageType === "exception") {
const code = message.headers[":exception-type"].value;
const exception = { [code]: message };
const deserializedException = await deserializer(exception);
if (deserializedException.$unknown) {
const error = new Error(toUtf8(message.body));
error.name = code;
throw error;
}
throw deserializedException[code];
}
else if (messageType === "event") {
const event = {
[message.headers[":event-type"].value]: message,
};
const deserialized = await deserializer(event);
if (deserialized.$unknown)
return;
return deserialized;
}
else {
throw Error(`Unrecognizable event type: ${message.headers[":event-type"].value}`);
}
};
}

View File

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

View File

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