@middy/ws-response 6.1.6 → 6.2.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/index.d.ts +19 -16
- package/index.js +53 -55
- package/package.json +71 -74
package/index.d.ts
CHANGED
|
@@ -1,21 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import type {
|
|
2
|
+
ApiGatewayManagementApiClient,
|
|
3
|
+
ApiGatewayManagementApiClientConfig,
|
|
4
|
+
} from "@aws-sdk/client-apigatewaymanagementapi";
|
|
5
|
+
import type middy from "@middy/core";
|
|
6
|
+
import type { Options as MiddyOptions } from "@middy/util";
|
|
4
7
|
|
|
5
8
|
interface Options<
|
|
6
|
-
|
|
9
|
+
AwsApiGatewayManagementApiClient = ApiGatewayManagementApiClient,
|
|
7
10
|
> extends Pick<
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
MiddyOptions<
|
|
12
|
+
AwsApiGatewayManagementApiClient,
|
|
13
|
+
ApiGatewayManagementApiClientConfig
|
|
14
|
+
>,
|
|
15
|
+
| "AwsClient"
|
|
16
|
+
| "awsClientOptions"
|
|
17
|
+
| "awsClientAssumeRole"
|
|
18
|
+
| "awsClientCapture"
|
|
19
|
+
| "disablePrefetch"
|
|
20
|
+
> {}
|
|
18
21
|
|
|
19
|
-
declare function wsResponse
|
|
22
|
+
declare function wsResponse(options?: Options): middy.MiddlewareObj;
|
|
20
23
|
|
|
21
|
-
export default wsResponse
|
|
24
|
+
export default wsResponse;
|
package/index.js
CHANGED
|
@@ -1,73 +1,71 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
} from
|
|
2
|
+
ApiGatewayManagementApiClient,
|
|
3
|
+
PostToConnectionCommand,
|
|
4
|
+
} from "@aws-sdk/client-apigatewaymanagementapi";
|
|
5
5
|
|
|
6
6
|
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
} from
|
|
7
|
+
canPrefetch,
|
|
8
|
+
catchInvalidSignatureException,
|
|
9
|
+
createClient,
|
|
10
|
+
createPrefetchClient,
|
|
11
|
+
} from "@middy/util";
|
|
12
12
|
|
|
13
13
|
const defaults = {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
14
|
+
AwsClient: ApiGatewayManagementApiClient,
|
|
15
|
+
awsClientOptions: {}, // { endpoint }
|
|
16
|
+
awsClientAssumeRole: undefined,
|
|
17
|
+
awsClientCapture: undefined,
|
|
18
|
+
disablePrefetch: false,
|
|
19
|
+
};
|
|
20
20
|
|
|
21
21
|
const wsResponseMiddleware = (opts) => {
|
|
22
|
-
|
|
22
|
+
const options = { ...defaults, ...opts };
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
let client;
|
|
25
|
+
if (canPrefetch(options) && options.awsClientOptions.endpoint) {
|
|
26
|
+
client = createPrefetchClient(options);
|
|
27
|
+
}
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
const wsResponseMiddlewareAfter = async (request) => {
|
|
30
|
+
const normalizedResponse = normalizeWsResponse(request);
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
if (!normalizedResponse.ConnectionId) return;
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
client = await createClient(options, request)
|
|
43
|
-
}
|
|
34
|
+
if (!options.awsClientOptions.endpoint && request.event.requestContext) {
|
|
35
|
+
options.awsClientOptions.endpoint = `https://${
|
|
36
|
+
request.event.requestContext.domainName
|
|
37
|
+
}/${request.event.requestContext.stage}`;
|
|
38
|
+
}
|
|
39
|
+
if (!client) {
|
|
40
|
+
client = await createClient(options, request);
|
|
41
|
+
}
|
|
44
42
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
43
|
+
const command = new PostToConnectionCommand(normalizedResponse);
|
|
44
|
+
await client
|
|
45
|
+
.send(command)
|
|
46
|
+
.catch((e) => catchInvalidSignatureException(e, client, command));
|
|
49
47
|
|
|
50
|
-
|
|
51
|
-
|
|
48
|
+
request.response = { statusCode: 200 };
|
|
49
|
+
};
|
|
52
50
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
51
|
+
return {
|
|
52
|
+
after: wsResponseMiddlewareAfter,
|
|
53
|
+
};
|
|
54
|
+
};
|
|
57
55
|
|
|
58
56
|
// TODO move to @middy/util?
|
|
59
57
|
const normalizeWsResponse = (request) => {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
58
|
+
let { response } = request;
|
|
59
|
+
if (typeof response === "undefined") {
|
|
60
|
+
response = {};
|
|
61
|
+
} else if (
|
|
62
|
+
typeof response?.Data === "undefined" &&
|
|
63
|
+
typeof response?.ConnectionId === "undefined"
|
|
64
|
+
) {
|
|
65
|
+
response = { Data: response };
|
|
66
|
+
}
|
|
67
|
+
response.ConnectionId ??= request.event.requestContext?.connectionId;
|
|
68
|
+
return response;
|
|
69
|
+
};
|
|
72
70
|
|
|
73
|
-
export default wsResponseMiddleware
|
|
71
|
+
export default wsResponseMiddleware;
|
package/package.json
CHANGED
|
@@ -1,76 +1,73 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
"aws-xray-sdk": "^3.3.3"
|
|
74
|
-
},
|
|
75
|
-
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
|
|
2
|
+
"name": "@middy/ws-response",
|
|
3
|
+
"version": "6.2.0",
|
|
4
|
+
"description": "WebSocket response handling middleware for the middy framework",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=20"
|
|
8
|
+
},
|
|
9
|
+
"engineStrict": true,
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"module": "./index.js",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./index.d.ts",
|
|
18
|
+
"default": "./index.js"
|
|
19
|
+
},
|
|
20
|
+
"require": {
|
|
21
|
+
"default": "./index.js"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"types": "index.d.ts",
|
|
26
|
+
"files": ["index.js", "index.d.ts"],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "npm run test:unit && npm run test:fuzz",
|
|
29
|
+
"test:unit": "node --test",
|
|
30
|
+
"test:fuzz": "node --test index.fuzz.js",
|
|
31
|
+
"test:perf": "node --test index.perf.js"
|
|
32
|
+
},
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"keywords": [
|
|
35
|
+
"Lambda",
|
|
36
|
+
"Middleware",
|
|
37
|
+
"Serverless",
|
|
38
|
+
"Framework",
|
|
39
|
+
"AWS",
|
|
40
|
+
"AWS Lambda",
|
|
41
|
+
"Middy",
|
|
42
|
+
"API Gateway",
|
|
43
|
+
"WebSocket",
|
|
44
|
+
"PostToConnection"
|
|
45
|
+
],
|
|
46
|
+
"author": {
|
|
47
|
+
"name": "Middy contributors",
|
|
48
|
+
"url": "https://github.com/middyjs/middy/graphs/contributors"
|
|
49
|
+
},
|
|
50
|
+
"repository": {
|
|
51
|
+
"type": "git",
|
|
52
|
+
"url": "git+https://github.com/middyjs/middy.git",
|
|
53
|
+
"directory": "packages/sts"
|
|
54
|
+
},
|
|
55
|
+
"bugs": {
|
|
56
|
+
"url": "https://github.com/middyjs/middy/issues"
|
|
57
|
+
},
|
|
58
|
+
"homepage": "https://middy.js.org",
|
|
59
|
+
"funding": {
|
|
60
|
+
"type": "github",
|
|
61
|
+
"url": "https://github.com/sponsors/willfarrell"
|
|
62
|
+
},
|
|
63
|
+
"dependencies": {
|
|
64
|
+
"@middy/util": "6.2.0"
|
|
65
|
+
},
|
|
66
|
+
"devDependencies": {
|
|
67
|
+
"@aws-sdk/client-apigatewaymanagementapi": "^3.0.0",
|
|
68
|
+
"@middy/core": "6.2.0",
|
|
69
|
+
"@types/aws-lambda": "^8.10.97",
|
|
70
|
+
"aws-xray-sdk": "^3.3.3"
|
|
71
|
+
},
|
|
72
|
+
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
|
|
76
73
|
}
|