@middy/ws-response 3.0.1 → 3.0.4
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/LICENSE +1 -1
- package/README.md +4 -3
- package/index.cjs +62 -1
- package/index.js +51 -1
- package/package.json +4 -4
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2017-2022 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
|
|
3
|
+
Copyright (c) 2017-2022 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell) and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
<a href="https://packagephobia.com/result?p=@middy/ws-responder">
|
|
10
10
|
<img src="https://packagephobia.com/badge?p=@middy/ws-responder" alt="npm install size" style="max-width:100%;">
|
|
11
11
|
</a>
|
|
12
|
-
<a href="https://github.com/middyjs/middy/actions">
|
|
13
|
-
<img src="https://github.com/middyjs/middy/workflows/
|
|
12
|
+
<a href="https://github.com/middyjs/middy/actions/workflows/tests.yml">
|
|
13
|
+
<img src="https://github.com/middyjs/middy/actions/workflows/tests.yml/badge.svg?branch=main&event=push" alt="GitHub Actions CI status badge" style="max-width:100%;">
|
|
14
14
|
</a>
|
|
15
15
|
<br/>
|
|
16
16
|
<a href="https://standardjs.com/">
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
<img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
|
|
34
34
|
</a>
|
|
35
35
|
</p>
|
|
36
|
+
<p>You can read the documentation at: <a href="https://middy.js.org/docs/middlewares/ws-responder">https://middy.js.org/docs/middlewares/ws-responder</a></p>
|
|
36
37
|
</div>
|
|
37
38
|
|
|
38
39
|
Post message to WebSocket connection.
|
|
@@ -105,7 +106,7 @@ Everyone is very welcome to contribute to this repository. Feel free to [raise i
|
|
|
105
106
|
|
|
106
107
|
## License
|
|
107
108
|
|
|
108
|
-
Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
|
|
109
|
+
Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell), and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
|
|
109
110
|
|
|
110
111
|
<a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
|
|
111
112
|
<img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
|
package/index.cjs
CHANGED
|
@@ -1,3 +1,64 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
module.exports = void 0;
|
|
6
|
+
var _apigatewaymanagementapiJs = _interopRequireDefault(require("aws-sdk/clients/apigatewaymanagementapi.js"));
|
|
7
|
+
var _util = require("@middy/util");
|
|
8
|
+
function _interopRequireDefault(obj) {
|
|
9
|
+
return obj && obj.__esModule ? obj : {
|
|
10
|
+
default: obj
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
var _response;
|
|
14
|
+
const defaults = {
|
|
15
|
+
AwsClient: _apigatewaymanagementapiJs.default,
|
|
16
|
+
awsClientOptions: {},
|
|
17
|
+
awsClientAssumeRole: undefined,
|
|
18
|
+
awsClientCapture: undefined,
|
|
19
|
+
disablePrefetch: false
|
|
20
|
+
};
|
|
21
|
+
const wsResponseMiddleware = (opts)=>{
|
|
22
|
+
const options = {
|
|
23
|
+
...defaults,
|
|
24
|
+
...opts
|
|
25
|
+
};
|
|
26
|
+
let client;
|
|
27
|
+
if ((0, _util).canPrefetch(options) && options.awsClientOptions.endpoint) {
|
|
28
|
+
client = (0, _util).createPrefetchClient(options);
|
|
29
|
+
}
|
|
30
|
+
const wsResponseMiddlewareAfter = async (request)=>{
|
|
31
|
+
normalizeWsResponse(request);
|
|
32
|
+
const { response } = request;
|
|
33
|
+
if (!response.ConnectionId) return;
|
|
34
|
+
if (!options.awsClientOptions.endpoint && request.event.requestContext) {
|
|
35
|
+
options.awsClientOptions.endpoint = request.event.requestContext.domainName + '/' + request.event.requestContext.stage;
|
|
36
|
+
}
|
|
37
|
+
if (!client) {
|
|
38
|
+
client = await (0, _util).createClient(options, request);
|
|
39
|
+
}
|
|
40
|
+
await client.postToConnection(response).promise();
|
|
41
|
+
request.response.statusCode = 200;
|
|
42
|
+
};
|
|
43
|
+
return {
|
|
44
|
+
after: wsResponseMiddlewareAfter
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
const normalizeWsResponse = (request)=>{
|
|
48
|
+
let { response } = request;
|
|
49
|
+
if (response === undefined) {
|
|
50
|
+
response = {};
|
|
51
|
+
} else if (response?.Data === undefined && response?.ConnectionId === undefined) {
|
|
52
|
+
response = {
|
|
53
|
+
Data: response
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
(_response = response).ConnectionId ?? (_response.ConnectionId = request.event.requestContext?.connectionId);
|
|
57
|
+
request.response = response;
|
|
58
|
+
return response;
|
|
59
|
+
};
|
|
60
|
+
var _default = wsResponseMiddleware;
|
|
61
|
+
module.exports = _default;
|
|
62
|
+
|
|
2
63
|
|
|
3
64
|
//# sourceMappingURL=index.cjs.map
|
package/index.js
CHANGED
|
@@ -1,3 +1,53 @@
|
|
|
1
|
-
var _response;
|
|
1
|
+
var _response;
|
|
2
|
+
import ApiGatewayManagementApi from 'aws-sdk/clients/apigatewaymanagementapi.js';
|
|
3
|
+
import { canPrefetch, createClient, createPrefetchClient } from '@middy/util';
|
|
4
|
+
const defaults = {
|
|
5
|
+
AwsClient: ApiGatewayManagementApi,
|
|
6
|
+
awsClientOptions: {},
|
|
7
|
+
awsClientAssumeRole: undefined,
|
|
8
|
+
awsClientCapture: undefined,
|
|
9
|
+
disablePrefetch: false
|
|
10
|
+
};
|
|
11
|
+
const wsResponseMiddleware = (opts)=>{
|
|
12
|
+
const options = {
|
|
13
|
+
...defaults,
|
|
14
|
+
...opts
|
|
15
|
+
};
|
|
16
|
+
let client;
|
|
17
|
+
if (canPrefetch(options) && options.awsClientOptions.endpoint) {
|
|
18
|
+
client = createPrefetchClient(options);
|
|
19
|
+
}
|
|
20
|
+
const wsResponseMiddlewareAfter = async (request)=>{
|
|
21
|
+
normalizeWsResponse(request);
|
|
22
|
+
const { response } = request;
|
|
23
|
+
if (!response.ConnectionId) return;
|
|
24
|
+
if (!options.awsClientOptions.endpoint && request.event.requestContext) {
|
|
25
|
+
options.awsClientOptions.endpoint = request.event.requestContext.domainName + '/' + request.event.requestContext.stage;
|
|
26
|
+
}
|
|
27
|
+
if (!client) {
|
|
28
|
+
client = await createClient(options, request);
|
|
29
|
+
}
|
|
30
|
+
await client.postToConnection(response).promise();
|
|
31
|
+
request.response.statusCode = 200;
|
|
32
|
+
};
|
|
33
|
+
return {
|
|
34
|
+
after: wsResponseMiddlewareAfter
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
const normalizeWsResponse = (request)=>{
|
|
38
|
+
let { response } = request;
|
|
39
|
+
if (response === undefined) {
|
|
40
|
+
response = {};
|
|
41
|
+
} else if (response?.Data === undefined && response?.ConnectionId === undefined) {
|
|
42
|
+
response = {
|
|
43
|
+
Data: response
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
(_response = response).ConnectionId ?? (_response.ConnectionId = request.event.requestContext?.connectionId);
|
|
47
|
+
request.response = response;
|
|
48
|
+
return response;
|
|
49
|
+
};
|
|
50
|
+
export default wsResponseMiddleware;
|
|
51
|
+
|
|
2
52
|
|
|
3
53
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/ws-response",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.4",
|
|
4
4
|
"description": "WebSocket response handling middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -54,12 +54,12 @@
|
|
|
54
54
|
},
|
|
55
55
|
"homepage": "https://middy.js.org",
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@middy/util": "
|
|
57
|
+
"@middy/util": "3.0.4"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
|
-
"@middy/core": "
|
|
60
|
+
"@middy/core": "3.0.4",
|
|
61
61
|
"aws-sdk": "^2.939.0",
|
|
62
62
|
"aws-xray-sdk": "^3.3.3"
|
|
63
63
|
},
|
|
64
|
-
"gitHead": "
|
|
64
|
+
"gitHead": "3e9bc83e791f943c71cd7003fc27f0a3692d83a1"
|
|
65
65
|
}
|