@aws-sdk/middleware-websocket 3.485.0 → 3.495.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.
- package/dist-cjs/EventStreamPayloadHandler.js +1 -35
- package/dist-cjs/WebsocketSignatureV4.js +1 -30
- package/dist-cjs/eventstream-payload-handler-provider.js +1 -6
- package/dist-cjs/get-event-signing-stream.js +1 -44
- package/dist-cjs/getWebSocketPlugin.js +1 -12
- package/dist-cjs/index.js +390 -7
- package/dist-cjs/middleware-session-id.js +1 -21
- package/dist-cjs/middleware-websocket-endpoint.js +1 -36
- package/dist-cjs/utils.js +1 -5
- package/dist-cjs/websocket-configuration.js +1 -16
- package/dist-cjs/websocket-fetch-handler.js +1 -148
- package/package.json +12 -11
|
@@ -1,35 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.EventStreamPayloadHandler = void 0;
|
|
4
|
-
const eventstream_codec_1 = require("@smithy/eventstream-codec");
|
|
5
|
-
const get_event_signing_stream_1 = require("./get-event-signing-stream");
|
|
6
|
-
class EventStreamPayloadHandler {
|
|
7
|
-
constructor(options) {
|
|
8
|
-
this.messageSigner = options.messageSigner;
|
|
9
|
-
this.eventStreamCodec = new eventstream_codec_1.EventStreamCodec(options.utf8Encoder, options.utf8Decoder);
|
|
10
|
-
}
|
|
11
|
-
async handle(next, args, context = {}) {
|
|
12
|
-
const request = args.request;
|
|
13
|
-
const { body: payload, headers, query } = request;
|
|
14
|
-
if (!(payload instanceof ReadableStream)) {
|
|
15
|
-
throw new Error("Eventstream payload must be a ReadableStream.");
|
|
16
|
-
}
|
|
17
|
-
const placeHolderStream = new TransformStream();
|
|
18
|
-
request.body = placeHolderStream.readable;
|
|
19
|
-
let result;
|
|
20
|
-
try {
|
|
21
|
-
result = await next(args);
|
|
22
|
-
}
|
|
23
|
-
catch (e) {
|
|
24
|
-
request.body.cancel();
|
|
25
|
-
throw e;
|
|
26
|
-
}
|
|
27
|
-
const match = (headers["authorization"] || "").match(/Signature=([\w]+)$/);
|
|
28
|
-
const priorSignature = (match || [])[1] || (query && query["X-Amz-Signature"]) || "";
|
|
29
|
-
const signingStream = (0, get_event_signing_stream_1.getEventSigningTransformStream)(priorSignature, await this.messageSigner(), this.eventStreamCodec);
|
|
30
|
-
const signedPayload = payload.pipeThrough(signingStream);
|
|
31
|
-
signedPayload.pipeThrough(placeHolderStream);
|
|
32
|
-
return result;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
exports.EventStreamPayloadHandler = EventStreamPayloadHandler;
|
|
1
|
+
module.exports = require("./index.js");
|
|
@@ -1,30 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.WebsocketSignatureV4 = void 0;
|
|
4
|
-
const protocol_http_1 = require("@smithy/protocol-http");
|
|
5
|
-
const utils_1 = require("./utils");
|
|
6
|
-
class WebsocketSignatureV4 {
|
|
7
|
-
constructor(options) {
|
|
8
|
-
this.signer = options.signer;
|
|
9
|
-
}
|
|
10
|
-
presign(originalRequest, options = {}) {
|
|
11
|
-
return this.signer.presign(originalRequest, options);
|
|
12
|
-
}
|
|
13
|
-
async sign(toSign, options) {
|
|
14
|
-
if (protocol_http_1.HttpRequest.isInstance(toSign) && (0, utils_1.isWebSocketRequest)(toSign)) {
|
|
15
|
-
const signedRequest = await this.signer.presign({ ...toSign, body: "" }, {
|
|
16
|
-
...options,
|
|
17
|
-
expiresIn: 60,
|
|
18
|
-
unsignableHeaders: new Set(Object.keys(toSign.headers).filter((header) => header !== "host")),
|
|
19
|
-
});
|
|
20
|
-
return {
|
|
21
|
-
...signedRequest,
|
|
22
|
-
body: toSign.body,
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
return this.signer.sign(toSign, options);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
exports.WebsocketSignatureV4 = WebsocketSignatureV4;
|
|
1
|
+
module.exports = require("./index.js");
|
|
@@ -1,6 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.eventStreamPayloadHandlerProvider = void 0;
|
|
4
|
-
const EventStreamPayloadHandler_1 = require("./EventStreamPayloadHandler");
|
|
5
|
-
const eventStreamPayloadHandlerProvider = (options) => new EventStreamPayloadHandler_1.EventStreamPayloadHandler(options);
|
|
6
|
-
exports.eventStreamPayloadHandlerProvider = eventStreamPayloadHandlerProvider;
|
|
1
|
+
module.exports = require("./index.js");
|
|
@@ -1,44 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getEventSigningTransformStream = void 0;
|
|
4
|
-
const util_hex_encoding_1 = require("@smithy/util-hex-encoding");
|
|
5
|
-
const getEventSigningTransformStream = (initialSignature, messageSigner, eventStreamCodec) => {
|
|
6
|
-
let priorSignature = initialSignature;
|
|
7
|
-
const transformer = {
|
|
8
|
-
start() { },
|
|
9
|
-
async transform(chunk, controller) {
|
|
10
|
-
try {
|
|
11
|
-
const now = new Date();
|
|
12
|
-
const dateHeader = {
|
|
13
|
-
":date": { type: "timestamp", value: now },
|
|
14
|
-
};
|
|
15
|
-
const signedMessage = await messageSigner.sign({
|
|
16
|
-
message: {
|
|
17
|
-
body: chunk,
|
|
18
|
-
headers: dateHeader,
|
|
19
|
-
},
|
|
20
|
-
priorSignature: priorSignature,
|
|
21
|
-
}, {
|
|
22
|
-
signingDate: now,
|
|
23
|
-
});
|
|
24
|
-
priorSignature = signedMessage.signature;
|
|
25
|
-
const serializedSigned = eventStreamCodec.encode({
|
|
26
|
-
headers: {
|
|
27
|
-
...dateHeader,
|
|
28
|
-
":chunk-signature": {
|
|
29
|
-
type: "binary",
|
|
30
|
-
value: (0, util_hex_encoding_1.fromHex)(signedMessage.signature),
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
body: chunk,
|
|
34
|
-
});
|
|
35
|
-
controller.enqueue(serializedSigned);
|
|
36
|
-
}
|
|
37
|
-
catch (error) {
|
|
38
|
-
controller.error(error);
|
|
39
|
-
}
|
|
40
|
-
},
|
|
41
|
-
};
|
|
42
|
-
return new TransformStream({ ...transformer });
|
|
43
|
-
};
|
|
44
|
-
exports.getEventSigningTransformStream = getEventSigningTransformStream;
|
|
1
|
+
module.exports = require("./index.js");
|
|
@@ -1,12 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getWebSocketPlugin = void 0;
|
|
4
|
-
const middleware_session_id_1 = require("./middleware-session-id");
|
|
5
|
-
const middleware_websocket_endpoint_1 = require("./middleware-websocket-endpoint");
|
|
6
|
-
const getWebSocketPlugin = (config, options) => ({
|
|
7
|
-
applyToStack: (clientStack) => {
|
|
8
|
-
clientStack.addRelativeTo((0, middleware_websocket_endpoint_1.websocketEndpointMiddleware)(config, options), middleware_websocket_endpoint_1.websocketEndpointMiddlewareOptions);
|
|
9
|
-
clientStack.add((0, middleware_session_id_1.injectSessionIdMiddleware)(), middleware_session_id_1.injectSessionIdMiddlewareOptions);
|
|
10
|
-
},
|
|
11
|
-
});
|
|
12
|
-
exports.getWebSocketPlugin = getWebSocketPlugin;
|
|
1
|
+
module.exports = require("./index.js");
|
package/dist-cjs/index.js
CHANGED
|
@@ -1,7 +1,390 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
+
WebSocketFetchHandler: () => WebSocketFetchHandler,
|
|
24
|
+
eventStreamPayloadHandlerProvider: () => eventStreamPayloadHandlerProvider,
|
|
25
|
+
getWebSocketPlugin: () => getWebSocketPlugin,
|
|
26
|
+
resolveWebSocketConfig: () => resolveWebSocketConfig
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(src_exports);
|
|
29
|
+
|
|
30
|
+
// src/EventStreamPayloadHandler.ts
|
|
31
|
+
var import_eventstream_codec = require("@smithy/eventstream-codec");
|
|
32
|
+
|
|
33
|
+
// src/get-event-signing-stream.ts
|
|
34
|
+
var import_util_hex_encoding = require("@smithy/util-hex-encoding");
|
|
35
|
+
var getEventSigningTransformStream = /* @__PURE__ */ __name((initialSignature, messageSigner, eventStreamCodec) => {
|
|
36
|
+
let priorSignature = initialSignature;
|
|
37
|
+
const transformer = {
|
|
38
|
+
start() {
|
|
39
|
+
},
|
|
40
|
+
async transform(chunk, controller) {
|
|
41
|
+
try {
|
|
42
|
+
const now = /* @__PURE__ */ new Date();
|
|
43
|
+
const dateHeader = {
|
|
44
|
+
":date": { type: "timestamp", value: now }
|
|
45
|
+
};
|
|
46
|
+
const signedMessage = await messageSigner.sign(
|
|
47
|
+
{
|
|
48
|
+
message: {
|
|
49
|
+
body: chunk,
|
|
50
|
+
headers: dateHeader
|
|
51
|
+
},
|
|
52
|
+
priorSignature
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
signingDate: now
|
|
56
|
+
}
|
|
57
|
+
);
|
|
58
|
+
priorSignature = signedMessage.signature;
|
|
59
|
+
const serializedSigned = eventStreamCodec.encode({
|
|
60
|
+
headers: {
|
|
61
|
+
...dateHeader,
|
|
62
|
+
":chunk-signature": {
|
|
63
|
+
type: "binary",
|
|
64
|
+
value: (0, import_util_hex_encoding.fromHex)(signedMessage.signature)
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
body: chunk
|
|
68
|
+
});
|
|
69
|
+
controller.enqueue(serializedSigned);
|
|
70
|
+
} catch (error) {
|
|
71
|
+
controller.error(error);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
return new TransformStream({ ...transformer });
|
|
76
|
+
}, "getEventSigningTransformStream");
|
|
77
|
+
|
|
78
|
+
// src/EventStreamPayloadHandler.ts
|
|
79
|
+
var _EventStreamPayloadHandler = class _EventStreamPayloadHandler {
|
|
80
|
+
constructor(options) {
|
|
81
|
+
this.messageSigner = options.messageSigner;
|
|
82
|
+
this.eventStreamCodec = new import_eventstream_codec.EventStreamCodec(options.utf8Encoder, options.utf8Decoder);
|
|
83
|
+
}
|
|
84
|
+
async handle(next, args, context = {}) {
|
|
85
|
+
const request = args.request;
|
|
86
|
+
const { body: payload, headers, query } = request;
|
|
87
|
+
if (!(payload instanceof ReadableStream)) {
|
|
88
|
+
throw new Error("Eventstream payload must be a ReadableStream.");
|
|
89
|
+
}
|
|
90
|
+
const placeHolderStream = new TransformStream();
|
|
91
|
+
request.body = placeHolderStream.readable;
|
|
92
|
+
let result;
|
|
93
|
+
try {
|
|
94
|
+
result = await next(args);
|
|
95
|
+
} catch (e) {
|
|
96
|
+
request.body.cancel();
|
|
97
|
+
throw e;
|
|
98
|
+
}
|
|
99
|
+
const match = (headers["authorization"] || "").match(/Signature=([\w]+)$/);
|
|
100
|
+
const priorSignature = (match || [])[1] || query && query["X-Amz-Signature"] || "";
|
|
101
|
+
const signingStream = getEventSigningTransformStream(
|
|
102
|
+
priorSignature,
|
|
103
|
+
await this.messageSigner(),
|
|
104
|
+
this.eventStreamCodec
|
|
105
|
+
);
|
|
106
|
+
const signedPayload = payload.pipeThrough(signingStream);
|
|
107
|
+
signedPayload.pipeThrough(placeHolderStream);
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
__name(_EventStreamPayloadHandler, "EventStreamPayloadHandler");
|
|
112
|
+
var EventStreamPayloadHandler = _EventStreamPayloadHandler;
|
|
113
|
+
|
|
114
|
+
// src/eventstream-payload-handler-provider.ts
|
|
115
|
+
var eventStreamPayloadHandlerProvider = /* @__PURE__ */ __name((options) => new EventStreamPayloadHandler(options), "eventStreamPayloadHandlerProvider");
|
|
116
|
+
|
|
117
|
+
// src/middleware-session-id.ts
|
|
118
|
+
var injectSessionIdMiddleware = /* @__PURE__ */ __name(() => (next) => async (args) => {
|
|
119
|
+
const requestParams = {
|
|
120
|
+
...args.input
|
|
121
|
+
};
|
|
122
|
+
const response = await next(args);
|
|
123
|
+
const output = response.output;
|
|
124
|
+
if (requestParams.SessionId && output.SessionId == null) {
|
|
125
|
+
output.SessionId = requestParams.SessionId;
|
|
126
|
+
}
|
|
127
|
+
return response;
|
|
128
|
+
}, "injectSessionIdMiddleware");
|
|
129
|
+
var injectSessionIdMiddlewareOptions = {
|
|
130
|
+
step: "initialize",
|
|
131
|
+
name: "injectSessionIdMiddleware",
|
|
132
|
+
tags: ["WEBSOCKET", "EVENT_STREAM"],
|
|
133
|
+
override: true
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
// src/middleware-websocket-endpoint.ts
|
|
137
|
+
var import_protocol_http = require("@smithy/protocol-http");
|
|
138
|
+
var websocketEndpointMiddleware = /* @__PURE__ */ __name((config, options) => (next) => (args) => {
|
|
139
|
+
var _a, _b;
|
|
140
|
+
const { request } = args;
|
|
141
|
+
if (import_protocol_http.HttpRequest.isInstance(request) && ((_b = (_a = config.requestHandler.metadata) == null ? void 0 : _a.handlerProtocol) == null ? void 0 : _b.toLowerCase().includes("websocket"))) {
|
|
142
|
+
request.protocol = "wss:";
|
|
143
|
+
request.method = "GET";
|
|
144
|
+
request.path = `${request.path}-websocket`;
|
|
145
|
+
const { headers } = request;
|
|
146
|
+
delete headers["Content-Type"];
|
|
147
|
+
delete headers["x-amz-content-sha256"];
|
|
148
|
+
for (const name of Object.keys(headers)) {
|
|
149
|
+
if (name.indexOf(options.headerPrefix) === 0) {
|
|
150
|
+
const chunkedName = name.replace(options.headerPrefix, "");
|
|
151
|
+
request.query[chunkedName] = headers[name];
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (headers["x-amz-user-agent"]) {
|
|
155
|
+
request.query["user-agent"] = headers["x-amz-user-agent"];
|
|
156
|
+
}
|
|
157
|
+
request.headers = { host: headers.host ?? request.hostname };
|
|
158
|
+
}
|
|
159
|
+
return next(args);
|
|
160
|
+
}, "websocketEndpointMiddleware");
|
|
161
|
+
var websocketEndpointMiddlewareOptions = {
|
|
162
|
+
name: "websocketEndpointMiddleware",
|
|
163
|
+
tags: ["WEBSOCKET", "EVENT_STREAM"],
|
|
164
|
+
relation: "after",
|
|
165
|
+
toMiddleware: "eventStreamHeaderMiddleware",
|
|
166
|
+
override: true
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
// src/getWebSocketPlugin.ts
|
|
170
|
+
var getWebSocketPlugin = /* @__PURE__ */ __name((config, options) => ({
|
|
171
|
+
applyToStack: (clientStack) => {
|
|
172
|
+
clientStack.addRelativeTo(websocketEndpointMiddleware(config, options), websocketEndpointMiddlewareOptions);
|
|
173
|
+
clientStack.add(injectSessionIdMiddleware(), injectSessionIdMiddlewareOptions);
|
|
174
|
+
}
|
|
175
|
+
}), "getWebSocketPlugin");
|
|
176
|
+
|
|
177
|
+
// src/WebsocketSignatureV4.ts
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
// src/utils.ts
|
|
181
|
+
var isWebSocketRequest = /* @__PURE__ */ __name((request) => request.protocol === "ws:" || request.protocol === "wss:", "isWebSocketRequest");
|
|
182
|
+
|
|
183
|
+
// src/WebsocketSignatureV4.ts
|
|
184
|
+
var _WebsocketSignatureV4 = class _WebsocketSignatureV4 {
|
|
185
|
+
constructor(options) {
|
|
186
|
+
this.signer = options.signer;
|
|
187
|
+
}
|
|
188
|
+
presign(originalRequest, options = {}) {
|
|
189
|
+
return this.signer.presign(originalRequest, options);
|
|
190
|
+
}
|
|
191
|
+
async sign(toSign, options) {
|
|
192
|
+
if (import_protocol_http.HttpRequest.isInstance(toSign) && isWebSocketRequest(toSign)) {
|
|
193
|
+
const signedRequest = await this.signer.presign(
|
|
194
|
+
{ ...toSign, body: "" },
|
|
195
|
+
{
|
|
196
|
+
...options,
|
|
197
|
+
// presigned url must be expired within 1 min.
|
|
198
|
+
expiresIn: 60,
|
|
199
|
+
// Not to sign headers. Transcribe-streaming WebSocket
|
|
200
|
+
// request omits headers except for required 'host' header. If we sign
|
|
201
|
+
// the other headers, the signature could be mismatch.
|
|
202
|
+
unsignableHeaders: new Set(Object.keys(toSign.headers).filter((header) => header !== "host"))
|
|
203
|
+
}
|
|
204
|
+
);
|
|
205
|
+
return {
|
|
206
|
+
...signedRequest,
|
|
207
|
+
body: toSign.body
|
|
208
|
+
};
|
|
209
|
+
} else {
|
|
210
|
+
return this.signer.sign(toSign, options);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
__name(_WebsocketSignatureV4, "WebsocketSignatureV4");
|
|
215
|
+
var WebsocketSignatureV4 = _WebsocketSignatureV4;
|
|
216
|
+
|
|
217
|
+
// src/websocket-configuration.ts
|
|
218
|
+
var resolveWebSocketConfig = /* @__PURE__ */ __name((input) => ({
|
|
219
|
+
...input,
|
|
220
|
+
signer: async (authScheme) => {
|
|
221
|
+
const signerObj = await input.signer(authScheme);
|
|
222
|
+
if (validateSigner(signerObj)) {
|
|
223
|
+
return new WebsocketSignatureV4({ signer: signerObj });
|
|
224
|
+
}
|
|
225
|
+
throw new Error("Expected WebsocketSignatureV4 signer, please check the client constructor.");
|
|
226
|
+
}
|
|
227
|
+
}), "resolveWebSocketConfig");
|
|
228
|
+
var validateSigner = /* @__PURE__ */ __name((signer) => !!signer, "validateSigner");
|
|
229
|
+
|
|
230
|
+
// src/websocket-fetch-handler.ts
|
|
231
|
+
var import_util_format_url = require("@aws-sdk/util-format-url");
|
|
232
|
+
var import_eventstream_serde_browser = require("@smithy/eventstream-serde-browser");
|
|
233
|
+
var import_fetch_http_handler = require("@smithy/fetch-http-handler");
|
|
234
|
+
|
|
235
|
+
var DEFAULT_WS_CONNECTION_TIMEOUT_MS = 2e3;
|
|
236
|
+
var _WebSocketFetchHandler = class _WebSocketFetchHandler {
|
|
237
|
+
constructor(options, httpHandler = new import_fetch_http_handler.FetchHttpHandler()) {
|
|
238
|
+
this.metadata = {
|
|
239
|
+
handlerProtocol: "websocket/h1.1"
|
|
240
|
+
};
|
|
241
|
+
this.sockets = {};
|
|
242
|
+
this.httpHandler = httpHandler;
|
|
243
|
+
if (typeof options === "function") {
|
|
244
|
+
this.configPromise = options().then((opts) => opts ?? {});
|
|
245
|
+
} else {
|
|
246
|
+
this.configPromise = Promise.resolve(options ?? {});
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Destroys the WebSocketHandler.
|
|
251
|
+
* Closes all sockets from the socket pool.
|
|
252
|
+
*/
|
|
253
|
+
destroy() {
|
|
254
|
+
for (const [key, sockets] of Object.entries(this.sockets)) {
|
|
255
|
+
for (const socket of sockets) {
|
|
256
|
+
socket.close(1e3, `Socket closed through destroy() call`);
|
|
257
|
+
}
|
|
258
|
+
delete this.sockets[key];
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
async handle(request) {
|
|
262
|
+
if (!isWebSocketRequest(request)) {
|
|
263
|
+
return this.httpHandler.handle(request);
|
|
264
|
+
}
|
|
265
|
+
const url = (0, import_util_format_url.formatUrl)(request);
|
|
266
|
+
const socket = new WebSocket(url);
|
|
267
|
+
if (!this.sockets[url]) {
|
|
268
|
+
this.sockets[url] = [];
|
|
269
|
+
}
|
|
270
|
+
this.sockets[url].push(socket);
|
|
271
|
+
socket.binaryType = "arraybuffer";
|
|
272
|
+
const { connectionTimeout = DEFAULT_WS_CONNECTION_TIMEOUT_MS } = await this.configPromise;
|
|
273
|
+
await this.waitForReady(socket, connectionTimeout);
|
|
274
|
+
const { body } = request;
|
|
275
|
+
const bodyStream = getIterator(body);
|
|
276
|
+
const asyncIterable = this.connect(socket, bodyStream);
|
|
277
|
+
const outputPayload = toReadableStream(asyncIterable);
|
|
278
|
+
return {
|
|
279
|
+
response: new import_protocol_http.HttpResponse({
|
|
280
|
+
statusCode: 200,
|
|
281
|
+
// indicates connection success
|
|
282
|
+
body: outputPayload
|
|
283
|
+
})
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Removes all closing/closed sockets from the socket pool for URL.
|
|
288
|
+
*/
|
|
289
|
+
removeNotUsableSockets(url) {
|
|
290
|
+
this.sockets[url] = (this.sockets[url] ?? []).filter(
|
|
291
|
+
(socket) => ![WebSocket.CLOSING, WebSocket.CLOSED].includes(socket.readyState)
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
waitForReady(socket, connectionTimeout) {
|
|
295
|
+
return new Promise((resolve, reject) => {
|
|
296
|
+
const timeout = setTimeout(() => {
|
|
297
|
+
this.removeNotUsableSockets(socket.url);
|
|
298
|
+
reject({
|
|
299
|
+
$metadata: {
|
|
300
|
+
httpStatusCode: 500
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
}, connectionTimeout);
|
|
304
|
+
socket.onopen = () => {
|
|
305
|
+
clearTimeout(timeout);
|
|
306
|
+
resolve();
|
|
307
|
+
};
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
connect(socket, data) {
|
|
311
|
+
let streamError = void 0;
|
|
312
|
+
let socketErrorOccurred = false;
|
|
313
|
+
let reject = /* @__PURE__ */ __name(() => {
|
|
314
|
+
}, "reject");
|
|
315
|
+
let resolve = /* @__PURE__ */ __name(() => {
|
|
316
|
+
}, "resolve");
|
|
317
|
+
socket.onmessage = (event) => {
|
|
318
|
+
resolve({
|
|
319
|
+
done: false,
|
|
320
|
+
value: new Uint8Array(event.data)
|
|
321
|
+
});
|
|
322
|
+
};
|
|
323
|
+
socket.onerror = (error) => {
|
|
324
|
+
socketErrorOccurred = true;
|
|
325
|
+
socket.close();
|
|
326
|
+
reject(error);
|
|
327
|
+
};
|
|
328
|
+
socket.onclose = () => {
|
|
329
|
+
this.removeNotUsableSockets(socket.url);
|
|
330
|
+
if (socketErrorOccurred)
|
|
331
|
+
return;
|
|
332
|
+
if (streamError) {
|
|
333
|
+
reject(streamError);
|
|
334
|
+
} else {
|
|
335
|
+
resolve({
|
|
336
|
+
done: true,
|
|
337
|
+
value: void 0
|
|
338
|
+
// unchecked because done=true.
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
const outputStream = {
|
|
343
|
+
[Symbol.asyncIterator]: () => ({
|
|
344
|
+
next: () => {
|
|
345
|
+
return new Promise((_resolve, _reject) => {
|
|
346
|
+
resolve = _resolve;
|
|
347
|
+
reject = _reject;
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
})
|
|
351
|
+
};
|
|
352
|
+
const send = /* @__PURE__ */ __name(async () => {
|
|
353
|
+
try {
|
|
354
|
+
for await (const inputChunk of data) {
|
|
355
|
+
socket.send(inputChunk);
|
|
356
|
+
}
|
|
357
|
+
} catch (err) {
|
|
358
|
+
streamError = err;
|
|
359
|
+
} finally {
|
|
360
|
+
socket.close(1e3);
|
|
361
|
+
}
|
|
362
|
+
}, "send");
|
|
363
|
+
send();
|
|
364
|
+
return outputStream;
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
__name(_WebSocketFetchHandler, "WebSocketFetchHandler");
|
|
368
|
+
var WebSocketFetchHandler = _WebSocketFetchHandler;
|
|
369
|
+
var getIterator = /* @__PURE__ */ __name((stream) => {
|
|
370
|
+
if (stream[Symbol.asyncIterator]) {
|
|
371
|
+
return stream;
|
|
372
|
+
}
|
|
373
|
+
if (isReadableStream(stream)) {
|
|
374
|
+
return (0, import_eventstream_serde_browser.readableStreamtoIterable)(stream);
|
|
375
|
+
}
|
|
376
|
+
return {
|
|
377
|
+
[Symbol.asyncIterator]: async function* () {
|
|
378
|
+
yield stream;
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
}, "getIterator");
|
|
382
|
+
var toReadableStream = /* @__PURE__ */ __name((asyncIterable) => typeof ReadableStream === "function" ? (0, import_eventstream_serde_browser.iterableToReadableStream)(asyncIterable) : asyncIterable, "toReadableStream");
|
|
383
|
+
var isReadableStream = /* @__PURE__ */ __name((payload) => typeof ReadableStream === "function" && payload instanceof ReadableStream, "isReadableStream");
|
|
384
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
385
|
+
0 && (module.exports = {
|
|
386
|
+
WebSocketFetchHandler,
|
|
387
|
+
eventStreamPayloadHandlerProvider,
|
|
388
|
+
getWebSocketPlugin,
|
|
389
|
+
resolveWebSocketConfig
|
|
390
|
+
});
|
|
@@ -1,21 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.injectSessionIdMiddlewareOptions = exports.injectSessionIdMiddleware = void 0;
|
|
4
|
-
const injectSessionIdMiddleware = () => (next) => async (args) => {
|
|
5
|
-
const requestParams = {
|
|
6
|
-
...args.input,
|
|
7
|
-
};
|
|
8
|
-
const response = await next(args);
|
|
9
|
-
const output = response.output;
|
|
10
|
-
if (requestParams.SessionId && output.SessionId == null) {
|
|
11
|
-
output.SessionId = requestParams.SessionId;
|
|
12
|
-
}
|
|
13
|
-
return response;
|
|
14
|
-
};
|
|
15
|
-
exports.injectSessionIdMiddleware = injectSessionIdMiddleware;
|
|
16
|
-
exports.injectSessionIdMiddlewareOptions = {
|
|
17
|
-
step: "initialize",
|
|
18
|
-
name: "injectSessionIdMiddleware",
|
|
19
|
-
tags: ["WEBSOCKET", "EVENT_STREAM"],
|
|
20
|
-
override: true,
|
|
21
|
-
};
|
|
1
|
+
module.exports = require("./index.js");
|
|
@@ -1,36 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.websocketEndpointMiddlewareOptions = exports.websocketEndpointMiddleware = void 0;
|
|
4
|
-
const protocol_http_1 = require("@smithy/protocol-http");
|
|
5
|
-
const websocketEndpointMiddleware = (config, options) => (next) => (args) => {
|
|
6
|
-
var _a, _b, _c;
|
|
7
|
-
const { request } = args;
|
|
8
|
-
if (protocol_http_1.HttpRequest.isInstance(request) &&
|
|
9
|
-
((_b = (_a = config.requestHandler.metadata) === null || _a === void 0 ? void 0 : _a.handlerProtocol) === null || _b === void 0 ? void 0 : _b.toLowerCase().includes("websocket"))) {
|
|
10
|
-
request.protocol = "wss:";
|
|
11
|
-
request.method = "GET";
|
|
12
|
-
request.path = `${request.path}-websocket`;
|
|
13
|
-
const { headers } = request;
|
|
14
|
-
delete headers["Content-Type"];
|
|
15
|
-
delete headers["x-amz-content-sha256"];
|
|
16
|
-
for (const name of Object.keys(headers)) {
|
|
17
|
-
if (name.indexOf(options.headerPrefix) === 0) {
|
|
18
|
-
const chunkedName = name.replace(options.headerPrefix, "");
|
|
19
|
-
request.query[chunkedName] = headers[name];
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
if (headers["x-amz-user-agent"]) {
|
|
23
|
-
request.query["user-agent"] = headers["x-amz-user-agent"];
|
|
24
|
-
}
|
|
25
|
-
request.headers = { host: (_c = headers.host) !== null && _c !== void 0 ? _c : request.hostname };
|
|
26
|
-
}
|
|
27
|
-
return next(args);
|
|
28
|
-
};
|
|
29
|
-
exports.websocketEndpointMiddleware = websocketEndpointMiddleware;
|
|
30
|
-
exports.websocketEndpointMiddlewareOptions = {
|
|
31
|
-
name: "websocketEndpointMiddleware",
|
|
32
|
-
tags: ["WEBSOCKET", "EVENT_STREAM"],
|
|
33
|
-
relation: "after",
|
|
34
|
-
toMiddleware: "eventStreamHeaderMiddleware",
|
|
35
|
-
override: true,
|
|
36
|
-
};
|
|
1
|
+
module.exports = require("./index.js");
|
package/dist-cjs/utils.js
CHANGED
|
@@ -1,5 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isWebSocketRequest = void 0;
|
|
4
|
-
const isWebSocketRequest = (request) => request.protocol === "ws:" || request.protocol === "wss:";
|
|
5
|
-
exports.isWebSocketRequest = isWebSocketRequest;
|
|
1
|
+
module.exports = require("./index.js");
|
|
@@ -1,16 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.resolveWebSocketConfig = void 0;
|
|
4
|
-
const WebsocketSignatureV4_1 = require("./WebsocketSignatureV4");
|
|
5
|
-
const resolveWebSocketConfig = (input) => ({
|
|
6
|
-
...input,
|
|
7
|
-
signer: async (authScheme) => {
|
|
8
|
-
const signerObj = await input.signer(authScheme);
|
|
9
|
-
if (validateSigner(signerObj)) {
|
|
10
|
-
return new WebsocketSignatureV4_1.WebsocketSignatureV4({ signer: signerObj });
|
|
11
|
-
}
|
|
12
|
-
throw new Error("Expected WebsocketSignatureV4 signer, please check the client constructor.");
|
|
13
|
-
},
|
|
14
|
-
});
|
|
15
|
-
exports.resolveWebSocketConfig = resolveWebSocketConfig;
|
|
16
|
-
const validateSigner = (signer) => !!signer;
|
|
1
|
+
module.exports = require("./index.js");
|
|
@@ -1,148 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.WebSocketFetchHandler = void 0;
|
|
4
|
-
const util_format_url_1 = require("@aws-sdk/util-format-url");
|
|
5
|
-
const eventstream_serde_browser_1 = require("@smithy/eventstream-serde-browser");
|
|
6
|
-
const fetch_http_handler_1 = require("@smithy/fetch-http-handler");
|
|
7
|
-
const protocol_http_1 = require("@smithy/protocol-http");
|
|
8
|
-
const utils_1 = require("./utils");
|
|
9
|
-
const DEFAULT_WS_CONNECTION_TIMEOUT_MS = 2000;
|
|
10
|
-
class WebSocketFetchHandler {
|
|
11
|
-
constructor(options, httpHandler = new fetch_http_handler_1.FetchHttpHandler()) {
|
|
12
|
-
this.metadata = {
|
|
13
|
-
handlerProtocol: "websocket/h1.1",
|
|
14
|
-
};
|
|
15
|
-
this.sockets = {};
|
|
16
|
-
this.httpHandler = httpHandler;
|
|
17
|
-
if (typeof options === "function") {
|
|
18
|
-
this.configPromise = options().then((opts) => opts !== null && opts !== void 0 ? opts : {});
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
this.configPromise = Promise.resolve(options !== null && options !== void 0 ? options : {});
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
destroy() {
|
|
25
|
-
for (const [key, sockets] of Object.entries(this.sockets)) {
|
|
26
|
-
for (const socket of sockets) {
|
|
27
|
-
socket.close(1000, `Socket closed through destroy() call`);
|
|
28
|
-
}
|
|
29
|
-
delete this.sockets[key];
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
async handle(request) {
|
|
33
|
-
if (!(0, utils_1.isWebSocketRequest)(request)) {
|
|
34
|
-
return this.httpHandler.handle(request);
|
|
35
|
-
}
|
|
36
|
-
const url = (0, util_format_url_1.formatUrl)(request);
|
|
37
|
-
const socket = new WebSocket(url);
|
|
38
|
-
if (!this.sockets[url]) {
|
|
39
|
-
this.sockets[url] = [];
|
|
40
|
-
}
|
|
41
|
-
this.sockets[url].push(socket);
|
|
42
|
-
socket.binaryType = "arraybuffer";
|
|
43
|
-
const { connectionTimeout = DEFAULT_WS_CONNECTION_TIMEOUT_MS } = await this.configPromise;
|
|
44
|
-
await this.waitForReady(socket, connectionTimeout);
|
|
45
|
-
const { body } = request;
|
|
46
|
-
const bodyStream = getIterator(body);
|
|
47
|
-
const asyncIterable = this.connect(socket, bodyStream);
|
|
48
|
-
const outputPayload = toReadableStream(asyncIterable);
|
|
49
|
-
return {
|
|
50
|
-
response: new protocol_http_1.HttpResponse({
|
|
51
|
-
statusCode: 200,
|
|
52
|
-
body: outputPayload,
|
|
53
|
-
}),
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
removeNotUsableSockets(url) {
|
|
57
|
-
var _a;
|
|
58
|
-
this.sockets[url] = ((_a = this.sockets[url]) !== null && _a !== void 0 ? _a : []).filter((socket) => ![WebSocket.CLOSING, WebSocket.CLOSED].includes(socket.readyState));
|
|
59
|
-
}
|
|
60
|
-
waitForReady(socket, connectionTimeout) {
|
|
61
|
-
return new Promise((resolve, reject) => {
|
|
62
|
-
const timeout = setTimeout(() => {
|
|
63
|
-
this.removeNotUsableSockets(socket.url);
|
|
64
|
-
reject({
|
|
65
|
-
$metadata: {
|
|
66
|
-
httpStatusCode: 500,
|
|
67
|
-
},
|
|
68
|
-
});
|
|
69
|
-
}, connectionTimeout);
|
|
70
|
-
socket.onopen = () => {
|
|
71
|
-
clearTimeout(timeout);
|
|
72
|
-
resolve();
|
|
73
|
-
};
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
connect(socket, data) {
|
|
77
|
-
let streamError = undefined;
|
|
78
|
-
let socketErrorOccurred = false;
|
|
79
|
-
let reject = () => { };
|
|
80
|
-
let resolve = () => { };
|
|
81
|
-
socket.onmessage = (event) => {
|
|
82
|
-
resolve({
|
|
83
|
-
done: false,
|
|
84
|
-
value: new Uint8Array(event.data),
|
|
85
|
-
});
|
|
86
|
-
};
|
|
87
|
-
socket.onerror = (error) => {
|
|
88
|
-
socketErrorOccurred = true;
|
|
89
|
-
socket.close();
|
|
90
|
-
reject(error);
|
|
91
|
-
};
|
|
92
|
-
socket.onclose = () => {
|
|
93
|
-
this.removeNotUsableSockets(socket.url);
|
|
94
|
-
if (socketErrorOccurred)
|
|
95
|
-
return;
|
|
96
|
-
if (streamError) {
|
|
97
|
-
reject(streamError);
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
resolve({
|
|
101
|
-
done: true,
|
|
102
|
-
value: undefined,
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
const outputStream = {
|
|
107
|
-
[Symbol.asyncIterator]: () => ({
|
|
108
|
-
next: () => {
|
|
109
|
-
return new Promise((_resolve, _reject) => {
|
|
110
|
-
resolve = _resolve;
|
|
111
|
-
reject = _reject;
|
|
112
|
-
});
|
|
113
|
-
},
|
|
114
|
-
}),
|
|
115
|
-
};
|
|
116
|
-
const send = async () => {
|
|
117
|
-
try {
|
|
118
|
-
for await (const inputChunk of data) {
|
|
119
|
-
socket.send(inputChunk);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
catch (err) {
|
|
123
|
-
streamError = err;
|
|
124
|
-
}
|
|
125
|
-
finally {
|
|
126
|
-
socket.close(1000);
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
send();
|
|
130
|
-
return outputStream;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
exports.WebSocketFetchHandler = WebSocketFetchHandler;
|
|
134
|
-
const getIterator = (stream) => {
|
|
135
|
-
if (stream[Symbol.asyncIterator]) {
|
|
136
|
-
return stream;
|
|
137
|
-
}
|
|
138
|
-
if (isReadableStream(stream)) {
|
|
139
|
-
return (0, eventstream_serde_browser_1.readableStreamtoIterable)(stream);
|
|
140
|
-
}
|
|
141
|
-
return {
|
|
142
|
-
[Symbol.asyncIterator]: async function* () {
|
|
143
|
-
yield stream;
|
|
144
|
-
},
|
|
145
|
-
};
|
|
146
|
-
};
|
|
147
|
-
const toReadableStream = (asyncIterable) => typeof ReadableStream === "function" ? (0, eventstream_serde_browser_1.iterableToReadableStream)(asyncIterable) : asyncIterable;
|
|
148
|
-
const isReadableStream = (payload) => typeof ReadableStream === "function" && payload instanceof ReadableStream;
|
|
1
|
+
module.exports = require("./index.js");
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aws-sdk/middleware-websocket",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.495.0",
|
|
4
4
|
"main": "./dist-cjs/index.js",
|
|
5
5
|
"module": "./dist-es/index.js",
|
|
6
6
|
"types": "./dist-types/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types'",
|
|
9
|
-
"build:cjs": "
|
|
9
|
+
"build:cjs": "node ../../scripts/compilation/inline middleware-websocket",
|
|
10
10
|
"build:es": "tsc -p tsconfig.es.json",
|
|
11
11
|
"build:include:deps": "lerna run --scope $npm_package_name --include-dependencies build",
|
|
12
12
|
"build:types": "tsc -p tsconfig.types.json",
|
|
@@ -21,15 +21,16 @@
|
|
|
21
21
|
},
|
|
22
22
|
"license": "Apache-2.0",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@aws-sdk/middleware-signing": "3.
|
|
25
|
-
"@aws-sdk/types": "3.
|
|
26
|
-
"@aws-sdk/util-format-url": "3.
|
|
27
|
-
"@smithy/eventstream-
|
|
28
|
-
"@smithy/
|
|
29
|
-
"@smithy/
|
|
30
|
-
"@smithy/
|
|
31
|
-
"@smithy/
|
|
32
|
-
"@smithy/
|
|
24
|
+
"@aws-sdk/middleware-signing": "3.495.0",
|
|
25
|
+
"@aws-sdk/types": "3.495.0",
|
|
26
|
+
"@aws-sdk/util-format-url": "3.495.0",
|
|
27
|
+
"@smithy/eventstream-codec": "^2.1.0",
|
|
28
|
+
"@smithy/eventstream-serde-browser": "^2.1.0",
|
|
29
|
+
"@smithy/fetch-http-handler": "^2.4.0",
|
|
30
|
+
"@smithy/protocol-http": "^3.1.0",
|
|
31
|
+
"@smithy/signature-v4": "^2.1.0",
|
|
32
|
+
"@smithy/types": "^2.9.0",
|
|
33
|
+
"@smithy/util-hex-encoding": "^2.1.0",
|
|
33
34
|
"tslib": "^2.5.0"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|