@middy/ws-response 3.0.0-alpha.6 → 3.0.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/README.md +32 -22
- package/index.cjs +3 -0
- package/index.js +2 -66
- package/package.json +13 -6
package/README.md
CHANGED
|
@@ -1,26 +1,36 @@
|
|
|
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
1
|
<div align="center">
|
|
2
|
+
<h1>Middy ws-response middleware</h1>
|
|
3
|
+
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/>
|
|
8
4
|
<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
5
|
<p>
|
|
13
|
-
<a href="
|
|
14
|
-
<img src="https://badge.fury.io/js/%40middy%2Fws-
|
|
6
|
+
<a href="https://www.npmjs.com/package/@middy/ws-responder?activeTab=versions">
|
|
7
|
+
<img src="https://badge.fury.io/js/%40middy%2Fws-responder.svg" alt="npm version" style="max-width:100%;">
|
|
8
|
+
</a>
|
|
9
|
+
<a href="https://packagephobia.com/result?p=@middy/ws-responder">
|
|
10
|
+
<img src="https://packagephobia.com/badge?p=@middy/ws-responder" alt="npm install size" style="max-width:100%;">
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://github.com/middyjs/middy/actions">
|
|
13
|
+
<img src="https://github.com/middyjs/middy/workflows/Tests/badge.svg" alt="GitHub Actions test status badge" style="max-width:100%;">
|
|
14
|
+
</a>
|
|
15
|
+
<br/>
|
|
16
|
+
<a href="https://standardjs.com/">
|
|
17
|
+
<img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
|
|
15
18
|
</a>
|
|
16
19
|
<a href="https://snyk.io/test/github/middyjs/middy">
|
|
17
20
|
<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
21
|
</a>
|
|
19
|
-
<a href="https://
|
|
20
|
-
<img src="https://img.shields.io/
|
|
22
|
+
<a href="https://lgtm.com/projects/g/middyjs/middy/context:javascript">
|
|
23
|
+
<img src="https://img.shields.io/lgtm/grade/javascript/g/middyjs/middy.svg?logo=lgtm&logoWidth=18" alt="Language grade: JavaScript" style="max-width:100%;">
|
|
24
|
+
</a>
|
|
25
|
+
<a href="https://bestpractices.coreinfrastructure.org/projects/5280">
|
|
26
|
+
<img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
|
|
21
27
|
</a>
|
|
28
|
+
<br/>
|
|
22
29
|
<a href="https://gitter.im/middyjs/Lobby">
|
|
23
|
-
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter"
|
|
30
|
+
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
|
|
31
|
+
</a>
|
|
32
|
+
<a href="https://stackoverflow.com/questions/tagged/middy?sort=Newest&uqlId=35052">
|
|
33
|
+
<img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
|
|
24
34
|
</a>
|
|
25
35
|
</p>
|
|
26
36
|
</div>
|
|
@@ -32,14 +42,14 @@ Post message to WebSocket connection.
|
|
|
32
42
|
To install this middleware you can use NPM:
|
|
33
43
|
|
|
34
44
|
```bash
|
|
35
|
-
npm install --save @middy/ws-
|
|
45
|
+
npm install --save @middy/ws-response
|
|
36
46
|
```
|
|
37
47
|
|
|
38
48
|
## Options
|
|
39
49
|
- `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) (
|
|
41
|
-
- `awsClientAssumeRole` (string) (
|
|
42
|
-
- `awsClientCapture` (function) (
|
|
50
|
+
- `awsClientOptions` (object) (default `undefined`): Options to pass to AWS.ApiGatewayManagementApi class constructor.
|
|
51
|
+
- `awsClientAssumeRole` (string) (default `undefined`): Internal key where secrets are stored. See [@middy/sts](/packages/sts/README.md) on to set this.
|
|
52
|
+
- `awsClientCapture` (function) (default `undefined`): Enable XRay by passing `captureAWSClient` from `aws-xray-sdk` in.
|
|
43
53
|
- `disablePrefetch` (boolean) (default `false`): On cold start requests will trigger early if they can. Setting `awsClientAssumeRole` disables prefetch.
|
|
44
54
|
|
|
45
55
|
NOTES:
|
|
@@ -51,20 +61,20 @@ NOTES:
|
|
|
51
61
|
### API Gateway
|
|
52
62
|
```javascript
|
|
53
63
|
import middy from '@middy/core'
|
|
54
|
-
import
|
|
64
|
+
import wsResponse from '@middy/ws-response'
|
|
55
65
|
|
|
56
66
|
export const handler = middy((event, context) => {
|
|
57
67
|
return 'message'
|
|
58
68
|
})
|
|
59
69
|
|
|
60
70
|
handler
|
|
61
|
-
.use(
|
|
71
|
+
.use(wsResponse())
|
|
62
72
|
```
|
|
63
73
|
|
|
64
74
|
### General
|
|
65
75
|
```javascript
|
|
66
76
|
import middy from '@middy/core'
|
|
67
|
-
import
|
|
77
|
+
import wsResponse from '@middy/ws-response'
|
|
68
78
|
|
|
69
79
|
export const handler = middy((event, context) => {
|
|
70
80
|
return {
|
|
@@ -74,7 +84,7 @@ export const handler = middy((event, context) => {
|
|
|
74
84
|
})
|
|
75
85
|
|
|
76
86
|
handler
|
|
77
|
-
.use(
|
|
87
|
+
.use(wsResponse({
|
|
78
88
|
awsClientOptions: {
|
|
79
89
|
endpoint: '...'
|
|
80
90
|
}
|
package/index.cjs
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _apigatewaymanagementapiJs=_interopRequireDefault(require("aws-sdk/clients/apigatewaymanagementapi.js"));var _util=require("@middy/util");function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}var _response;const defaults={AwsClient:_apigatewaymanagementapiJs.default,awsClientOptions:{},awsClientAssumeRole:undefined,awsClientCapture:undefined,disablePrefetch:false};const wsResponseMiddleware=opts=>{const options={...defaults,...opts};let client;if((0,_util).canPrefetch(options)&&options.awsClientOptions.endpoint){client=(0,_util).createPrefetchClient(options)}const wsResponseMiddlewareAfter=async request=>{normalizeWsResponse(request);const{response}=request;if(!response.ConnectionId)return;if(!options.awsClientOptions.endpoint&&request.event.requestContext){options.awsClientOptions.endpoint=request.event.requestContext.domainName+"/"+request.event.requestContext.stage}if(!client){client=await (0,_util).createClient(options,request)}await client.postToConnection(response).promise();request.response.statusCode=200};return{after:wsResponseMiddlewareAfter}};const normalizeWsResponse=request=>{let{response}=request;if(response===undefined){response={}}else if(response?.Data===undefined&&response?.ConnectionId===undefined){response={Data:response}}(_response=response).ConnectionId??(_response.ConnectionId=request.event.requestContext?.connectionId);request.response=response;return response};var _default=wsResponseMiddleware;exports.default=_default
|
|
2
|
+
|
|
3
|
+
//# sourceMappingURL=index.cjs.map
|
package/index.js
CHANGED
|
@@ -1,67 +1,3 @@
|
|
|
1
|
-
import ApiGatewayManagementApi from
|
|
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
|
-
};
|
|
1
|
+
var _response;import ApiGatewayManagementApi from"aws-sdk/clients/apigatewaymanagementapi.js";import{canPrefetch,createClient,createPrefetchClient}from"@middy/util";const defaults={AwsClient:ApiGatewayManagementApi,awsClientOptions:{},awsClientAssumeRole:undefined,awsClientCapture:undefined,disablePrefetch:false};const wsResponseMiddleware=opts=>{const options={...defaults,...opts};let client;if(canPrefetch(options)&&options.awsClientOptions.endpoint){client=createPrefetchClient(options)}const wsResponseMiddlewareAfter=async request=>{normalizeWsResponse(request);const{response}=request;if(!response.ConnectionId)return;if(!options.awsClientOptions.endpoint&&request.event.requestContext){options.awsClientOptions.endpoint=request.event.requestContext.domainName+"/"+request.event.requestContext.stage}if(!client){client=await createClient(options,request)}await client.postToConnection(response).promise();request.response.statusCode=200};return{after:wsResponseMiddlewareAfter}};const normalizeWsResponse=request=>{let{response}=request;if(response===undefined){response={}}else if(response?.Data===undefined&&response?.ConnectionId===undefined){response={Data:response}}(_response=response).ConnectionId??(_response.ConnectionId=request.event.requestContext?.connectionId);request.response=response;return response};export default wsResponseMiddleware
|
|
10
2
|
|
|
11
|
-
|
|
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;
|
|
3
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/ws-response",
|
|
3
|
-
"version": "3.0.0
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "WebSocket response handling middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -10,10 +10,17 @@
|
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"exports":
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./index.js",
|
|
16
|
+
"require": "./index.cjs",
|
|
17
|
+
"types": "./index.d.ts"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
14
20
|
"types": "index.d.ts",
|
|
15
21
|
"files": [
|
|
16
22
|
"index.js",
|
|
23
|
+
"index.cjs",
|
|
17
24
|
"index.d.ts"
|
|
18
25
|
],
|
|
19
26
|
"scripts": {
|
|
@@ -45,14 +52,14 @@
|
|
|
45
52
|
"bugs": {
|
|
46
53
|
"url": "https://github.com/middyjs/middy/issues"
|
|
47
54
|
},
|
|
48
|
-
"homepage": "https://
|
|
55
|
+
"homepage": "https://middy.js.org",
|
|
49
56
|
"dependencies": {
|
|
50
|
-
"@middy/util": "^3.0.0
|
|
57
|
+
"@middy/util": "^3.0.0"
|
|
51
58
|
},
|
|
52
59
|
"devDependencies": {
|
|
53
|
-
"@middy/core": "^3.0.0
|
|
60
|
+
"@middy/core": "^3.0.0",
|
|
54
61
|
"aws-sdk": "^2.939.0",
|
|
55
62
|
"aws-xray-sdk": "^3.3.3"
|
|
56
63
|
},
|
|
57
|
-
"gitHead": "
|
|
64
|
+
"gitHead": "01520fa8628a36c2f89e126cad656a16547ea0d6"
|
|
58
65
|
}
|