@middy/ws-response 3.0.0-alpha.6
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 +21 -0
- package/README.md +102 -0
- package/index.d.ts +12 -0
- package/index.js +67 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017-2022 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# Middy ws-response middleware
|
|
2
|
+
|
|
3
|
+
<div align="center">
|
|
4
|
+
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.png"/>
|
|
5
|
+
</div>
|
|
6
|
+
|
|
7
|
+
<div align="center">
|
|
8
|
+
<p><strong>WebSocket (ws) response middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
|
|
9
|
+
</div>
|
|
10
|
+
|
|
11
|
+
<div align="center">
|
|
12
|
+
<p>
|
|
13
|
+
<a href="http://badge.fury.io/js/%40middy%2Fws-response">
|
|
14
|
+
<img src="https://badge.fury.io/js/%40middy%2Fws-response.svg" alt="npm version" style="max-width:100%;">
|
|
15
|
+
</a>
|
|
16
|
+
<a href="https://snyk.io/test/github/middyjs/middy">
|
|
17
|
+
<img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;">
|
|
18
|
+
</a>
|
|
19
|
+
<a href="https://standardjs.com/">
|
|
20
|
+
<img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
|
|
21
|
+
</a>
|
|
22
|
+
<a href="https://gitter.im/middyjs/Lobby">
|
|
23
|
+
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
|
|
24
|
+
</a>
|
|
25
|
+
</p>
|
|
26
|
+
</div>
|
|
27
|
+
|
|
28
|
+
Post message to WebSocket connection.
|
|
29
|
+
|
|
30
|
+
## Install
|
|
31
|
+
|
|
32
|
+
To install this middleware you can use NPM:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm install --save @middy/ws-responder
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Options
|
|
39
|
+
- `AwsClient` (object) (default `AWS.ApiGatewayManagementApi`): AWS.ApiGatewayManagementApi class constructor (e.g. that has been instrumented with AWS XRay). Must be from `aws-sdk` v2.
|
|
40
|
+
- `awsClientOptions` (object) (optional): Options to pass to AWS.ApiGatewayManagementApi class constructor.
|
|
41
|
+
- `awsClientAssumeRole` (string) (optional): Internal key where secrets are stored. See [@middy/sts](/packages/sts/README.md) on to set this.
|
|
42
|
+
- `awsClientCapture` (function) (optional): Enable XRay by passing `captureAWSClient` from `aws-xray-sdk` in.
|
|
43
|
+
- `disablePrefetch` (boolean) (default `false`): On cold start requests will trigger early if they can. Setting `awsClientAssumeRole` disables prefetch.
|
|
44
|
+
|
|
45
|
+
NOTES:
|
|
46
|
+
- Lambda is required to have IAM permission for `execute-api:ManageConnections`
|
|
47
|
+
- If `awsClientOptions.endpoint` is not set it will be set using `event.requestContext.{domainName,stage}`
|
|
48
|
+
- If response does not contain `ConnectId`, it will be set from `event.requestContext.connectionId`
|
|
49
|
+
|
|
50
|
+
## Sample usage
|
|
51
|
+
### API Gateway
|
|
52
|
+
```javascript
|
|
53
|
+
import middy from '@middy/core'
|
|
54
|
+
import wsResonse from '@middy/ws-responder'
|
|
55
|
+
|
|
56
|
+
export const handler = middy((event, context) => {
|
|
57
|
+
return 'message'
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
handler
|
|
61
|
+
.use(wsResonse())
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### General
|
|
65
|
+
```javascript
|
|
66
|
+
import middy from '@middy/core'
|
|
67
|
+
import wsResonse from '@middy/ws-responder'
|
|
68
|
+
|
|
69
|
+
export const handler = middy((event, context) => {
|
|
70
|
+
return {
|
|
71
|
+
ConnectionId: '...',
|
|
72
|
+
Data: 'message'
|
|
73
|
+
}
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
handler
|
|
77
|
+
.use(wsResonse({
|
|
78
|
+
awsClientOptions: {
|
|
79
|
+
endpoint: '...'
|
|
80
|
+
}
|
|
81
|
+
}))
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
## Middy documentation and examples
|
|
87
|
+
|
|
88
|
+
For more documentation and examples, refers to the main [Middy monorepo on GitHub](https://github.com/middyjs/middy) or [Middy official website](https://middy.js.org).
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
## Contributing
|
|
92
|
+
|
|
93
|
+
Everyone is very welcome to contribute to this repository. Feel free to [raise issues](https://github.com/middyjs/middy/issues) or to [submit Pull Requests](https://github.com/middyjs/middy/pulls).
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
## License
|
|
97
|
+
|
|
98
|
+
Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
|
|
99
|
+
|
|
100
|
+
<a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
|
|
101
|
+
<img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
|
|
102
|
+
</a>
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ApiGatewayManagementApi } from 'aws-sdk'
|
|
2
|
+
import middy from '@middy/core'
|
|
3
|
+
import { Options as MiddyOptions } from '@middy/util'
|
|
4
|
+
|
|
5
|
+
interface Options<S = ApiGatewayManagementApi>
|
|
6
|
+
extends Pick<MiddyOptions<S, ApiGatewayManagementApi.Types.ClientConfiguration>,
|
|
7
|
+
'AwsClient' | 'awsClientOptions' | 'awsClientAssumeRole' | 'awsClientCapture' | 'disablePrefetch'> {
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
declare function wsResponse (options?: Options): middy.MiddlewareObj
|
|
11
|
+
|
|
12
|
+
export default wsResponse
|
package/index.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import ApiGatewayManagementApi from 'aws-sdk/clients/apigatewaymanagementapi.js';
|
|
2
|
+
import { canPrefetch, createClient, createPrefetchClient } from '@middy/util';
|
|
3
|
+
const defaults = {
|
|
4
|
+
AwsClient: ApiGatewayManagementApi,
|
|
5
|
+
awsClientOptions: {},
|
|
6
|
+
awsClientAssumeRole: undefined,
|
|
7
|
+
awsClientCapture: undefined,
|
|
8
|
+
disablePrefetch: false
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const wsResponderMiddleware = opts => {
|
|
12
|
+
const options = { ...defaults,
|
|
13
|
+
...opts
|
|
14
|
+
};
|
|
15
|
+
let client;
|
|
16
|
+
|
|
17
|
+
if (canPrefetch(options) && options.awsClientOptions.endpoint) {
|
|
18
|
+
client = createPrefetchClient(options);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const wsResponderMiddlewareAfter = async request => {
|
|
22
|
+
normalizeWsResponse(request);
|
|
23
|
+
const {
|
|
24
|
+
response
|
|
25
|
+
} = request;
|
|
26
|
+
if (!response.ConnectionId) return;
|
|
27
|
+
|
|
28
|
+
if (!options.awsClientOptions.endpoint && request.event.requestContext) {
|
|
29
|
+
options.awsClientOptions.endpoint = request.event.requestContext.domainName + '/' + request.event.requestContext.stage;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (!client) {
|
|
33
|
+
client = await createClient(options, request);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
await client.postToConnection(response).promise();
|
|
37
|
+
request.response = {
|
|
38
|
+
statusCode: 200
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
after: wsResponderMiddlewareAfter
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const normalizeWsResponse = request => {
|
|
48
|
+
var _response, _response2, _response3, _request$event$reques;
|
|
49
|
+
|
|
50
|
+
let {
|
|
51
|
+
response
|
|
52
|
+
} = request;
|
|
53
|
+
|
|
54
|
+
if (response === undefined) {
|
|
55
|
+
response = {};
|
|
56
|
+
} else if (((_response = response) === null || _response === void 0 ? void 0 : _response.Data) === undefined && ((_response2 = response) === null || _response2 === void 0 ? void 0 : _response2.ConnectionId) === undefined) {
|
|
57
|
+
response = {
|
|
58
|
+
Data: response
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
(_response3 = response).ConnectionId ?? (_response3.ConnectionId = (_request$event$reques = request.event.requestContext) === null || _request$event$reques === void 0 ? void 0 : _request$event$reques.connectionId);
|
|
63
|
+
request.response = response;
|
|
64
|
+
return response;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export default wsResponderMiddleware;
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@middy/ws-response",
|
|
3
|
+
"version": "3.0.0-alpha.6",
|
|
4
|
+
"description": "WebSocket response handling middleware for the middy framework",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=14"
|
|
8
|
+
},
|
|
9
|
+
"engineStrict": true,
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"exports": "./index.js",
|
|
14
|
+
"types": "index.d.ts",
|
|
15
|
+
"files": [
|
|
16
|
+
"index.js",
|
|
17
|
+
"index.d.ts"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "npm run test:unit",
|
|
21
|
+
"test:unit": "ava"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"keywords": [
|
|
25
|
+
"Lambda",
|
|
26
|
+
"Middleware",
|
|
27
|
+
"Serverless",
|
|
28
|
+
"Framework",
|
|
29
|
+
"AWS",
|
|
30
|
+
"AWS Lambda",
|
|
31
|
+
"Middy",
|
|
32
|
+
"API Gateway",
|
|
33
|
+
"WebSocket",
|
|
34
|
+
"PostToConnection"
|
|
35
|
+
],
|
|
36
|
+
"author": {
|
|
37
|
+
"name": "Middy contributors",
|
|
38
|
+
"url": "https://github.com/middyjs/middy/graphs/contributors"
|
|
39
|
+
},
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "github:middyjs/middy",
|
|
43
|
+
"directory": "packages/sts"
|
|
44
|
+
},
|
|
45
|
+
"bugs": {
|
|
46
|
+
"url": "https://github.com/middyjs/middy/issues"
|
|
47
|
+
},
|
|
48
|
+
"homepage": "https://github.com/middyjs/middy#readme",
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@middy/util": "^3.0.0-alpha.6"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@middy/core": "^3.0.0-alpha.6",
|
|
54
|
+
"aws-sdk": "^2.939.0",
|
|
55
|
+
"aws-xray-sdk": "^3.3.3"
|
|
56
|
+
},
|
|
57
|
+
"gitHead": "176660ed3e0716d6bfb635c77251b301e0e24720"
|
|
58
|
+
}
|