@smithy/eventstream-serde-universal 2.0.15 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,27 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EventStreamMarshaller = void 0;
4
- const eventstream_codec_1 = require("@smithy/eventstream-codec");
5
- const getChunkedStream_1 = require("./getChunkedStream");
6
- const getUnmarshalledStream_1 = require("./getUnmarshalledStream");
7
- class EventStreamMarshaller {
8
- constructor({ utf8Encoder, utf8Decoder }) {
9
- this.eventStreamCodec = new eventstream_codec_1.EventStreamCodec(utf8Encoder, utf8Decoder);
10
- this.utfEncoder = utf8Encoder;
11
- }
12
- deserialize(body, deserializer) {
13
- const inputStream = (0, getChunkedStream_1.getChunkedStream)(body);
14
- return new eventstream_codec_1.SmithyMessageDecoderStream({
15
- messageStream: new eventstream_codec_1.MessageDecoderStream({ inputStream, decoder: this.eventStreamCodec }),
16
- deserializer: (0, getUnmarshalledStream_1.getMessageUnmarshaller)(deserializer, this.utfEncoder),
17
- });
18
- }
19
- serialize(inputStream, serializer) {
20
- return new eventstream_codec_1.MessageEncoderStream({
21
- messageStream: new eventstream_codec_1.SmithyMessageEncoderStream({ inputStream, serializer }),
22
- encoder: this.eventStreamCodec,
23
- includeEndFrame: true,
24
- });
25
- }
26
- }
27
- exports.EventStreamMarshaller = EventStreamMarshaller;
1
+ module.exports = require("./index.js");
@@ -1,34 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MockEventMessageSource = void 0;
4
- const stream_1 = require("stream");
5
- class MockEventMessageSource extends stream_1.Readable {
6
- constructor(options) {
7
- super(options);
8
- this.readCount = 0;
9
- this.data = Buffer.concat(options.messages);
10
- this.emitSize = options.emitSize;
11
- this.throwError = options.throwError;
12
- }
13
- _read() {
14
- const self = this;
15
- if (this.readCount === this.data.length) {
16
- if (this.throwError) {
17
- process.nextTick(function () {
18
- self.emit("error", new Error("Throwing an error!"));
19
- });
20
- return;
21
- }
22
- else {
23
- this.push(null);
24
- return;
25
- }
26
- }
27
- const bytesLeft = this.data.length - this.readCount;
28
- const numBytesToSend = Math.min(bytesLeft, this.emitSize);
29
- const chunk = this.data.slice(this.readCount, this.readCount + numBytesToSend);
30
- this.readCount += numBytesToSend;
31
- this.push(chunk);
32
- }
33
- }
34
- exports.MockEventMessageSource = MockEventMessageSource;
1
+ module.exports = require("../index.js");
@@ -1,7 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.exception = exports.endEventMessage = exports.statsEventMessage = exports.recordEventMessage = void 0;
4
- exports.recordEventMessage = Buffer.from("AAAA0AAAAFX31gVLDTptZXNzYWdlLXR5cGUHAAVldmVudAs6ZXZlbnQtdHlwZQcAB1JlY29yZHMNOmNvbnRlbnQtdHlwZQcAGGFwcGxpY2F0aW9uL29jdGV0LXN0cmVhbTEsRm9vLFdoZW4gbGlmZSBnaXZlcyB5b3UgZm9vLi4uCjIsQmFyLG1ha2UgQmFyIQozLEZpenosU29tZXRpbWVzIHBhaXJlZCB3aXRoLi4uCjQsQnV6eix0aGUgaW5mYW1vdXMgQnV6eiEKzxKeSw==", "base64");
5
- exports.statsEventMessage = Buffer.from("AAAA0QAAAEM+YpmqDTptZXNzYWdlLXR5cGUHAAVldmVudAs6ZXZlbnQtdHlwZQcABVN0YXRzDTpjb250ZW50LXR5cGUHAAh0ZXh0L3htbDxTdGF0cyB4bWxucz0iIj48Qnl0ZXNTY2FubmVkPjEyNjwvQnl0ZXNTY2FubmVkPjxCeXRlc1Byb2Nlc3NlZD4xMjY8L0J5dGVzUHJvY2Vzc2VkPjxCeXRlc1JldHVybmVkPjEwNzwvQnl0ZXNSZXR1cm5lZD48L1N0YXRzPiJ0pLk=", "base64");
6
- exports.endEventMessage = Buffer.from("AAAAOAAAACjBxoTUDTptZXNzYWdlLXR5cGUHAAVldmVudAs6ZXZlbnQtdHlwZQcAA0VuZM+X05I=", "base64");
7
- exports.exception = Buffer.from("AAAAtgAAAF8BcW64DTpjb250ZW50LXR5cGUHABhhcHBsaWNhdGlvbi9vY3RldC1zdHJlYW0NOm1lc3NhZ2UtdHlwZQcACWV4Y2VwdGlvbg86ZXhjZXB0aW9uLXR5cGUHAAlFeGNlcHRpb25UaGlzIGlzIGEgbW9kZWxlZCBleGNlcHRpb24gZXZlbnQgdGhhdCB3b3VsZCBiZSB0aHJvd24gaW4gZGVzZXJpYWxpemVyLj6Gc60=", "base64");
1
+ module.exports = require("../index.js");
@@ -1,70 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getChunkedStream = void 0;
4
- function getChunkedStream(source) {
5
- let currentMessageTotalLength = 0;
6
- let currentMessagePendingLength = 0;
7
- let currentMessage = null;
8
- let messageLengthBuffer = null;
9
- const allocateMessage = (size) => {
10
- if (typeof size !== "number") {
11
- throw new Error("Attempted to allocate an event message where size was not a number: " + size);
12
- }
13
- currentMessageTotalLength = size;
14
- currentMessagePendingLength = 4;
15
- currentMessage = new Uint8Array(size);
16
- const currentMessageView = new DataView(currentMessage.buffer);
17
- currentMessageView.setUint32(0, size, false);
18
- };
19
- const iterator = async function* () {
20
- const sourceIterator = source[Symbol.asyncIterator]();
21
- while (true) {
22
- const { value, done } = await sourceIterator.next();
23
- if (done) {
24
- if (!currentMessageTotalLength) {
25
- return;
26
- }
27
- else if (currentMessageTotalLength === currentMessagePendingLength) {
28
- yield currentMessage;
29
- }
30
- else {
31
- throw new Error("Truncated event message received.");
32
- }
33
- return;
34
- }
35
- const chunkLength = value.length;
36
- let currentOffset = 0;
37
- while (currentOffset < chunkLength) {
38
- if (!currentMessage) {
39
- const bytesRemaining = chunkLength - currentOffset;
40
- if (!messageLengthBuffer) {
41
- messageLengthBuffer = new Uint8Array(4);
42
- }
43
- const numBytesForTotal = Math.min(4 - currentMessagePendingLength, bytesRemaining);
44
- messageLengthBuffer.set(value.slice(currentOffset, currentOffset + numBytesForTotal), currentMessagePendingLength);
45
- currentMessagePendingLength += numBytesForTotal;
46
- currentOffset += numBytesForTotal;
47
- if (currentMessagePendingLength < 4) {
48
- break;
49
- }
50
- allocateMessage(new DataView(messageLengthBuffer.buffer).getUint32(0, false));
51
- messageLengthBuffer = null;
52
- }
53
- const numBytesToWrite = Math.min(currentMessageTotalLength - currentMessagePendingLength, chunkLength - currentOffset);
54
- currentMessage.set(value.slice(currentOffset, currentOffset + numBytesToWrite), currentMessagePendingLength);
55
- currentMessagePendingLength += numBytesToWrite;
56
- currentOffset += numBytesToWrite;
57
- if (currentMessageTotalLength && currentMessageTotalLength === currentMessagePendingLength) {
58
- yield currentMessage;
59
- currentMessage = null;
60
- currentMessageTotalLength = 0;
61
- currentMessagePendingLength = 0;
62
- }
63
- }
64
- }
65
- };
66
- return {
67
- [Symbol.asyncIterator]: iterator,
68
- };
69
- }
70
- exports.getChunkedStream = getChunkedStream;
1
+ module.exports = require("./index.js");
@@ -1,52 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getMessageUnmarshaller = exports.getUnmarshalledStream = void 0;
4
- function getUnmarshalledStream(source, options) {
5
- const messageUnmarshaller = getMessageUnmarshaller(options.deserializer, options.toUtf8);
6
- return {
7
- [Symbol.asyncIterator]: async function* () {
8
- for await (const chunk of source) {
9
- const message = options.eventStreamCodec.decode(chunk);
10
- const type = await messageUnmarshaller(message);
11
- if (type === undefined)
12
- continue;
13
- yield type;
14
- }
15
- },
16
- };
17
- }
18
- exports.getUnmarshalledStream = getUnmarshalledStream;
19
- function getMessageUnmarshaller(deserializer, toUtf8) {
20
- return async function (message) {
21
- const { value: messageType } = message.headers[":message-type"];
22
- if (messageType === "error") {
23
- const unmodeledError = new Error(message.headers[":error-message"].value || "UnknownError");
24
- unmodeledError.name = message.headers[":error-code"].value;
25
- throw unmodeledError;
26
- }
27
- else if (messageType === "exception") {
28
- const code = message.headers[":exception-type"].value;
29
- const exception = { [code]: message };
30
- const deserializedException = await deserializer(exception);
31
- if (deserializedException.$unknown) {
32
- const error = new Error(toUtf8(message.body));
33
- error.name = code;
34
- throw error;
35
- }
36
- throw deserializedException[code];
37
- }
38
- else if (messageType === "event") {
39
- const event = {
40
- [message.headers[":event-type"].value]: message,
41
- };
42
- const deserialized = await deserializer(event);
43
- if (deserialized.$unknown)
44
- return;
45
- return deserialized;
46
- }
47
- else {
48
- throw Error(`Unrecognizable event type: ${message.headers[":event-type"].value}`);
49
- }
50
- };
51
- }
52
- exports.getMessageUnmarshaller = getMessageUnmarshaller;
1
+ module.exports = require("./index.js");
package/dist-cjs/index.js CHANGED
@@ -1,5 +1,180 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- tslib_1.__exportStar(require("./EventStreamMarshaller"), exports);
5
- tslib_1.__exportStar(require("./provider"), exports);
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ EventStreamMarshaller: () => EventStreamMarshaller,
24
+ eventStreamSerdeProvider: () => eventStreamSerdeProvider
25
+ });
26
+ module.exports = __toCommonJS(src_exports);
27
+
28
+ // src/EventStreamMarshaller.ts
29
+ var import_eventstream_codec = require("@smithy/eventstream-codec");
30
+
31
+ // src/getChunkedStream.ts
32
+ function getChunkedStream(source) {
33
+ let currentMessageTotalLength = 0;
34
+ let currentMessagePendingLength = 0;
35
+ let currentMessage = null;
36
+ let messageLengthBuffer = null;
37
+ const allocateMessage = /* @__PURE__ */ __name((size) => {
38
+ if (typeof size !== "number") {
39
+ throw new Error("Attempted to allocate an event message where size was not a number: " + size);
40
+ }
41
+ currentMessageTotalLength = size;
42
+ currentMessagePendingLength = 4;
43
+ currentMessage = new Uint8Array(size);
44
+ const currentMessageView = new DataView(currentMessage.buffer);
45
+ currentMessageView.setUint32(0, size, false);
46
+ }, "allocateMessage");
47
+ const iterator = /* @__PURE__ */ __name(async function* () {
48
+ const sourceIterator = source[Symbol.asyncIterator]();
49
+ while (true) {
50
+ const { value, done } = await sourceIterator.next();
51
+ if (done) {
52
+ if (!currentMessageTotalLength) {
53
+ return;
54
+ } else if (currentMessageTotalLength === currentMessagePendingLength) {
55
+ yield currentMessage;
56
+ } else {
57
+ throw new Error("Truncated event message received.");
58
+ }
59
+ return;
60
+ }
61
+ const chunkLength = value.length;
62
+ let currentOffset = 0;
63
+ while (currentOffset < chunkLength) {
64
+ if (!currentMessage) {
65
+ const bytesRemaining = chunkLength - currentOffset;
66
+ if (!messageLengthBuffer) {
67
+ messageLengthBuffer = new Uint8Array(4);
68
+ }
69
+ const numBytesForTotal = Math.min(
70
+ 4 - currentMessagePendingLength,
71
+ // remaining bytes to fill the messageLengthBuffer
72
+ bytesRemaining
73
+ // bytes left in chunk
74
+ );
75
+ messageLengthBuffer.set(
76
+ // @ts-ignore error TS2532: Object is possibly 'undefined' for value
77
+ value.slice(currentOffset, currentOffset + numBytesForTotal),
78
+ currentMessagePendingLength
79
+ );
80
+ currentMessagePendingLength += numBytesForTotal;
81
+ currentOffset += numBytesForTotal;
82
+ if (currentMessagePendingLength < 4) {
83
+ break;
84
+ }
85
+ allocateMessage(new DataView(messageLengthBuffer.buffer).getUint32(0, false));
86
+ messageLengthBuffer = null;
87
+ }
88
+ const numBytesToWrite = Math.min(
89
+ currentMessageTotalLength - currentMessagePendingLength,
90
+ // number of bytes left to complete message
91
+ chunkLength - currentOffset
92
+ // number of bytes left in the original chunk
93
+ );
94
+ currentMessage.set(
95
+ // @ts-ignore error TS2532: Object is possibly 'undefined' for value
96
+ value.slice(currentOffset, currentOffset + numBytesToWrite),
97
+ currentMessagePendingLength
98
+ );
99
+ currentMessagePendingLength += numBytesToWrite;
100
+ currentOffset += numBytesToWrite;
101
+ if (currentMessageTotalLength && currentMessageTotalLength === currentMessagePendingLength) {
102
+ yield currentMessage;
103
+ currentMessage = null;
104
+ currentMessageTotalLength = 0;
105
+ currentMessagePendingLength = 0;
106
+ }
107
+ }
108
+ }
109
+ }, "iterator");
110
+ return {
111
+ [Symbol.asyncIterator]: iterator
112
+ };
113
+ }
114
+ __name(getChunkedStream, "getChunkedStream");
115
+
116
+ // src/getUnmarshalledStream.ts
117
+ function getMessageUnmarshaller(deserializer, toUtf8) {
118
+ return async function(message) {
119
+ const { value: messageType } = message.headers[":message-type"];
120
+ if (messageType === "error") {
121
+ const unmodeledError = new Error(message.headers[":error-message"].value || "UnknownError");
122
+ unmodeledError.name = message.headers[":error-code"].value;
123
+ throw unmodeledError;
124
+ } else if (messageType === "exception") {
125
+ const code = message.headers[":exception-type"].value;
126
+ const exception = { [code]: message };
127
+ const deserializedException = await deserializer(exception);
128
+ if (deserializedException.$unknown) {
129
+ const error = new Error(toUtf8(message.body));
130
+ error.name = code;
131
+ throw error;
132
+ }
133
+ throw deserializedException[code];
134
+ } else if (messageType === "event") {
135
+ const event = {
136
+ [message.headers[":event-type"].value]: message
137
+ };
138
+ const deserialized = await deserializer(event);
139
+ if (deserialized.$unknown)
140
+ return;
141
+ return deserialized;
142
+ } else {
143
+ throw Error(`Unrecognizable event type: ${message.headers[":event-type"].value}`);
144
+ }
145
+ };
146
+ }
147
+ __name(getMessageUnmarshaller, "getMessageUnmarshaller");
148
+
149
+ // src/EventStreamMarshaller.ts
150
+ var _EventStreamMarshaller = class _EventStreamMarshaller {
151
+ constructor({ utf8Encoder, utf8Decoder }) {
152
+ this.eventStreamCodec = new import_eventstream_codec.EventStreamCodec(utf8Encoder, utf8Decoder);
153
+ this.utfEncoder = utf8Encoder;
154
+ }
155
+ deserialize(body, deserializer) {
156
+ const inputStream = getChunkedStream(body);
157
+ return new import_eventstream_codec.SmithyMessageDecoderStream({
158
+ messageStream: new import_eventstream_codec.MessageDecoderStream({ inputStream, decoder: this.eventStreamCodec }),
159
+ // @ts-expect-error Type 'T' is not assignable to type 'Record<string, any>'
160
+ deserializer: getMessageUnmarshaller(deserializer, this.utfEncoder)
161
+ });
162
+ }
163
+ serialize(inputStream, serializer) {
164
+ return new import_eventstream_codec.MessageEncoderStream({
165
+ messageStream: new import_eventstream_codec.SmithyMessageEncoderStream({ inputStream, serializer }),
166
+ encoder: this.eventStreamCodec,
167
+ includeEndFrame: true
168
+ });
169
+ }
170
+ };
171
+ __name(_EventStreamMarshaller, "EventStreamMarshaller");
172
+ var EventStreamMarshaller = _EventStreamMarshaller;
173
+
174
+ // src/provider.ts
175
+ var eventStreamSerdeProvider = /* @__PURE__ */ __name((options) => new EventStreamMarshaller(options), "eventStreamSerdeProvider");
176
+ // Annotate the CommonJS export names for ESM import in node:
177
+ 0 && (module.exports = {
178
+ EventStreamMarshaller,
179
+ eventStreamSerdeProvider
180
+ });
@@ -1,6 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.eventStreamSerdeProvider = void 0;
4
- const EventStreamMarshaller_1 = require("./EventStreamMarshaller");
5
- const eventStreamSerdeProvider = (options) => new EventStreamMarshaller_1.EventStreamMarshaller(options);
6
- exports.eventStreamSerdeProvider = eventStreamSerdeProvider;
1
+ module.exports = require("./index.js");
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@smithy/eventstream-serde-universal",
3
- "version": "2.0.15",
3
+ "version": "2.1.0",
4
4
  "scripts": {
5
5
  "build": "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types && yarn build:types:downlevel'",
6
- "build:cjs": "yarn g:tsc -p tsconfig.cjs.json",
6
+ "build:cjs": "node ../../scripts/inline eventstream-serde-universal",
7
7
  "build:es": "yarn g:tsc -p tsconfig.es.json",
8
8
  "build:types": "yarn g:tsc -p tsconfig.types.json",
9
9
  "build:types:downlevel": "downlevel-dts dist-types dist-types/ts3.4",
@@ -22,12 +22,12 @@
22
22
  },
23
23
  "license": "Apache-2.0",
24
24
  "dependencies": {
25
- "@smithy/eventstream-codec": "^2.0.15",
26
- "@smithy/types": "^2.7.0",
25
+ "@smithy/eventstream-codec": "^2.1.0",
26
+ "@smithy/types": "^2.9.0",
27
27
  "tslib": "^2.5.0"
28
28
  },
29
29
  "devDependencies": {
30
- "@smithy/util-utf8": "^2.0.2",
30
+ "@smithy/util-utf8": "^2.1.0",
31
31
  "@tsconfig/recommended": "1.0.1",
32
32
  "@types/node": "^14.14.31",
33
33
  "concurrently": "7.0.0",