@smithy/eventstream-serde-universal 4.1.1 → 4.2.1

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.
package/dist-cjs/index.js CHANGED
@@ -1,182 +1,132 @@
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);
1
+ 'use strict';
19
2
 
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- EventStreamMarshaller: () => EventStreamMarshaller,
24
- eventStreamSerdeProvider: () => eventStreamSerdeProvider
25
- });
26
- module.exports = __toCommonJS(index_exports);
3
+ var eventstreamCodec = require('@smithy/eventstream-codec');
27
4
 
28
- // src/EventStreamMarshaller.ts
29
- var import_eventstream_codec = require("@smithy/eventstream-codec");
30
-
31
- // src/getChunkedStream.ts
32
5
  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.");
6
+ let currentMessageTotalLength = 0;
7
+ let currentMessagePendingLength = 0;
8
+ let currentMessage = null;
9
+ let messageLengthBuffer = null;
10
+ const allocateMessage = (size) => {
11
+ if (typeof size !== "number") {
12
+ throw new Error("Attempted to allocate an event message where size was not a number: " + size);
58
13
  }
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;
14
+ currentMessageTotalLength = size;
15
+ currentMessagePendingLength = 4;
16
+ currentMessage = new Uint8Array(size);
17
+ const currentMessageView = new DataView(currentMessage.buffer);
18
+ currentMessageView.setUint32(0, size, false);
19
+ };
20
+ const iterator = async function* () {
21
+ const sourceIterator = source[Symbol.asyncIterator]();
22
+ while (true) {
23
+ const { value, done } = await sourceIterator.next();
24
+ if (done) {
25
+ if (!currentMessageTotalLength) {
26
+ return;
27
+ }
28
+ else if (currentMessageTotalLength === currentMessagePendingLength) {
29
+ yield currentMessage;
30
+ }
31
+ else {
32
+ throw new Error("Truncated event message received.");
33
+ }
34
+ return;
35
+ }
36
+ const chunkLength = value.length;
37
+ let currentOffset = 0;
38
+ while (currentOffset < chunkLength) {
39
+ if (!currentMessage) {
40
+ const bytesRemaining = chunkLength - currentOffset;
41
+ if (!messageLengthBuffer) {
42
+ messageLengthBuffer = new Uint8Array(4);
43
+ }
44
+ const numBytesForTotal = Math.min(4 - currentMessagePendingLength, bytesRemaining);
45
+ messageLengthBuffer.set(value.slice(currentOffset, currentOffset + numBytesForTotal), currentMessagePendingLength);
46
+ currentMessagePendingLength += numBytesForTotal;
47
+ currentOffset += numBytesForTotal;
48
+ if (currentMessagePendingLength < 4) {
49
+ break;
50
+ }
51
+ allocateMessage(new DataView(messageLengthBuffer.buffer).getUint32(0, false));
52
+ messageLengthBuffer = null;
53
+ }
54
+ const numBytesToWrite = Math.min(currentMessageTotalLength - currentMessagePendingLength, chunkLength - currentOffset);
55
+ currentMessage.set(value.slice(currentOffset, currentOffset + numBytesToWrite), currentMessagePendingLength);
56
+ currentMessagePendingLength += numBytesToWrite;
57
+ currentOffset += numBytesToWrite;
58
+ if (currentMessageTotalLength && currentMessageTotalLength === currentMessagePendingLength) {
59
+ yield currentMessage;
60
+ currentMessage = null;
61
+ currentMessageTotalLength = 0;
62
+ currentMessagePendingLength = 0;
63
+ }
64
+ }
87
65
  }
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
- };
66
+ };
67
+ return {
68
+ [Symbol.asyncIterator]: iterator,
69
+ };
113
70
  }
114
- __name(getChunkedStream, "getChunkedStream");
115
71
 
116
- // src/getUnmarshalledStream.ts
117
72
  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) return;
140
- return deserialized;
141
- } else {
142
- throw Error(`Unrecognizable event type: ${message.headers[":event-type"].value}`);
143
- }
144
- };
73
+ return async function (message) {
74
+ const { value: messageType } = message.headers[":message-type"];
75
+ if (messageType === "error") {
76
+ const unmodeledError = new Error(message.headers[":error-message"].value || "UnknownError");
77
+ unmodeledError.name = message.headers[":error-code"].value;
78
+ throw unmodeledError;
79
+ }
80
+ else if (messageType === "exception") {
81
+ const code = message.headers[":exception-type"].value;
82
+ const exception = { [code]: message };
83
+ const deserializedException = await deserializer(exception);
84
+ if (deserializedException.$unknown) {
85
+ const error = new Error(toUtf8(message.body));
86
+ error.name = code;
87
+ throw error;
88
+ }
89
+ throw deserializedException[code];
90
+ }
91
+ else if (messageType === "event") {
92
+ const event = {
93
+ [message.headers[":event-type"].value]: message,
94
+ };
95
+ const deserialized = await deserializer(event);
96
+ if (deserialized.$unknown)
97
+ return;
98
+ return deserialized;
99
+ }
100
+ else {
101
+ throw Error(`Unrecognizable event type: ${message.headers[":event-type"].value}`);
102
+ }
103
+ };
145
104
  }
146
- __name(getMessageUnmarshaller, "getMessageUnmarshaller");
147
105
 
148
- // src/EventStreamMarshaller.ts
149
- var EventStreamMarshaller = class {
150
- static {
151
- __name(this, "EventStreamMarshaller");
152
- }
153
- constructor({ utf8Encoder, utf8Decoder }) {
154
- this.eventStreamCodec = new import_eventstream_codec.EventStreamCodec(utf8Encoder, utf8Decoder);
155
- this.utfEncoder = utf8Encoder;
156
- }
157
- deserialize(body, deserializer) {
158
- const inputStream = getChunkedStream(body);
159
- return new import_eventstream_codec.SmithyMessageDecoderStream({
160
- messageStream: new import_eventstream_codec.MessageDecoderStream({ inputStream, decoder: this.eventStreamCodec }),
161
- // @ts-expect-error Type 'T' is not assignable to type 'Record<string, any>'
162
- deserializer: getMessageUnmarshaller(deserializer, this.utfEncoder)
163
- });
164
- }
165
- serialize(inputStream, serializer) {
166
- return new import_eventstream_codec.MessageEncoderStream({
167
- messageStream: new import_eventstream_codec.SmithyMessageEncoderStream({ inputStream, serializer }),
168
- encoder: this.eventStreamCodec,
169
- includeEndFrame: true
170
- });
171
- }
172
- };
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:
106
+ class EventStreamMarshaller {
107
+ eventStreamCodec;
108
+ utfEncoder;
109
+ constructor({ utf8Encoder, utf8Decoder }) {
110
+ this.eventStreamCodec = new eventstreamCodec.EventStreamCodec(utf8Encoder, utf8Decoder);
111
+ this.utfEncoder = utf8Encoder;
112
+ }
113
+ deserialize(body, deserializer) {
114
+ const inputStream = getChunkedStream(body);
115
+ return new eventstreamCodec.SmithyMessageDecoderStream({
116
+ messageStream: new eventstreamCodec.MessageDecoderStream({ inputStream, decoder: this.eventStreamCodec }),
117
+ deserializer: getMessageUnmarshaller(deserializer, this.utfEncoder),
118
+ });
119
+ }
120
+ serialize(inputStream, serializer) {
121
+ return new eventstreamCodec.MessageEncoderStream({
122
+ messageStream: new eventstreamCodec.SmithyMessageEncoderStream({ inputStream, serializer }),
123
+ encoder: this.eventStreamCodec,
124
+ includeEndFrame: true,
125
+ });
126
+ }
127
+ }
177
128
 
178
- 0 && (module.exports = {
179
- EventStreamMarshaller,
180
- eventStreamSerdeProvider
181
- });
129
+ const eventStreamSerdeProvider = (options) => new EventStreamMarshaller(options);
182
130
 
131
+ exports.EventStreamMarshaller = EventStreamMarshaller;
132
+ exports.eventStreamSerdeProvider = eventStreamSerdeProvider;
@@ -2,6 +2,8 @@ import { EventStreamCodec, MessageDecoderStream, MessageEncoderStream, SmithyMes
2
2
  import { getChunkedStream } from "./getChunkedStream";
3
3
  import { getMessageUnmarshaller } from "./getUnmarshalledStream";
4
4
  export class EventStreamMarshaller {
5
+ eventStreamCodec;
6
+ utfEncoder;
5
7
  constructor({ utf8Encoder, utf8Decoder }) {
6
8
  this.eventStreamCodec = new EventStreamCodec(utf8Encoder, utf8Decoder);
7
9
  this.utfEncoder = utf8Encoder;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smithy/eventstream-serde-universal",
3
- "version": "4.1.1",
3
+ "version": "4.2.1",
4
4
  "scripts": {
5
5
  "build": "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types && yarn build:types:downlevel'",
6
6
  "build:cjs": "node ../../scripts/inline eventstream-serde-universal",
@@ -12,7 +12,9 @@
12
12
  "lint": "eslint -c ../../.eslintrc.js \"src/**/*.ts\"",
13
13
  "format": "prettier --config ../../prettier.config.js --ignore-path ../../.prettierignore --write \"**/*.{ts,md,json}\"",
14
14
  "test": "yarn g:vitest run",
15
- "test:watch": "yarn g:vitest watch"
15
+ "test:watch": "yarn g:vitest watch",
16
+ "test:integration": "yarn g:vitest run -c vitest.config.integ.mts",
17
+ "test:integration:watch": "yarn g:vitest watch -c vitest.config.integ.mts"
16
18
  },
17
19
  "main": "./dist-cjs/index.js",
18
20
  "module": "./dist-es/index.js",
@@ -24,12 +26,12 @@
24
26
  "license": "Apache-2.0",
25
27
  "sideEffects": false,
26
28
  "dependencies": {
27
- "@smithy/eventstream-codec": "^4.1.1",
28
- "@smithy/types": "^4.5.0",
29
+ "@smithy/eventstream-codec": "^4.2.1",
30
+ "@smithy/types": "^4.7.0",
29
31
  "tslib": "^2.6.2"
30
32
  },
31
33
  "devDependencies": {
32
- "@smithy/util-utf8": "^4.1.0",
34
+ "@smithy/util-utf8": "^4.2.0",
33
35
  "@types/node": "^18.11.9",
34
36
  "concurrently": "7.0.0",
35
37
  "downlevel-dts": "0.10.1",
@@ -1 +0,0 @@
1
- module.exports = require("./index.js");
@@ -1 +0,0 @@
1
- module.exports = require("./index.js");
@@ -1 +0,0 @@
1
- module.exports = require("./index.js");
@@ -1 +0,0 @@
1
- module.exports = require("./index.js");