ag-common 0.0.741 → 0.0.743
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/api/helpers/apigw.d.ts +30 -0
- package/dist/api/helpers/apigw.js +100 -0
- package/dist/api/helpers/index.d.ts +1 -0
- package/dist/api/helpers/index.js +1 -0
- package/dist/api/helpers/ssmInfra/apigw.d.ts +22 -0
- package/dist/api/helpers/ssmInfra/apigw.js +46 -0
- package/dist/api/helpers/ssmInfra/index.d.ts +1 -0
- package/dist/api/helpers/ssmInfra/index.js +1 -0
- package/dist/ui/helpers/cookie/raw.d.ts +6 -2
- package/dist/ui/helpers/cookie/raw.js +1 -1
- package/package.json +2 -1
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sends a message to a specific WebSocket connection
|
|
3
|
+
*/
|
|
4
|
+
export declare function sendWebSocketMessage<T>({ connectionId, message, endpoint, }: {
|
|
5
|
+
connectionId: string;
|
|
6
|
+
message: T;
|
|
7
|
+
endpoint: string;
|
|
8
|
+
}): Promise<boolean>;
|
|
9
|
+
/**
|
|
10
|
+
* Deletes a WebSocket connection
|
|
11
|
+
*/
|
|
12
|
+
export declare function deleteConnection({ connectionId, endpoint, }: {
|
|
13
|
+
connectionId: string;
|
|
14
|
+
endpoint: string;
|
|
15
|
+
}): Promise<boolean>;
|
|
16
|
+
/**
|
|
17
|
+
* Checks if a WebSocket connection is still active
|
|
18
|
+
*/
|
|
19
|
+
export declare function isConnectionActive({ connectionId, endpoint, }: {
|
|
20
|
+
connectionId: string;
|
|
21
|
+
endpoint: string;
|
|
22
|
+
}): Promise<boolean>;
|
|
23
|
+
/**
|
|
24
|
+
* Broadcasts a message to multiple WebSocket connections
|
|
25
|
+
*/
|
|
26
|
+
export declare function broadcastMessage<T>({ connectionIds, message, endpoint, }: {
|
|
27
|
+
connectionIds: string[];
|
|
28
|
+
message: T;
|
|
29
|
+
endpoint: string;
|
|
30
|
+
}): Promise<boolean>;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.sendWebSocketMessage = sendWebSocketMessage;
|
|
13
|
+
exports.deleteConnection = deleteConnection;
|
|
14
|
+
exports.isConnectionActive = isConnectionActive;
|
|
15
|
+
exports.broadcastMessage = broadcastMessage;
|
|
16
|
+
const client_apigatewaymanagementapi_1 = require("@aws-sdk/client-apigatewaymanagementapi");
|
|
17
|
+
const common_1 = require("../../common");
|
|
18
|
+
/**
|
|
19
|
+
* Sends a message to a specific WebSocket connection
|
|
20
|
+
*/
|
|
21
|
+
function sendWebSocketMessage(_a) {
|
|
22
|
+
return __awaiter(this, arguments, void 0, function* ({ connectionId, message, endpoint, }) {
|
|
23
|
+
const wsClient = new client_apigatewaymanagementapi_1.ApiGatewayManagementApiClient({
|
|
24
|
+
endpoint,
|
|
25
|
+
});
|
|
26
|
+
try {
|
|
27
|
+
const s = yield wsClient.send(new client_apigatewaymanagementapi_1.PostToConnectionCommand({
|
|
28
|
+
ConnectionId: connectionId,
|
|
29
|
+
Data: JSON.stringify(message),
|
|
30
|
+
}));
|
|
31
|
+
if (s.$metadata.httpStatusCode === 200) {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
(0, common_1.warn)('Failed to send WebSocket message:', e.message);
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Deletes a WebSocket connection
|
|
44
|
+
*/
|
|
45
|
+
function deleteConnection(_a) {
|
|
46
|
+
return __awaiter(this, arguments, void 0, function* ({ connectionId, endpoint, }) {
|
|
47
|
+
const wsClient = new client_apigatewaymanagementapi_1.ApiGatewayManagementApiClient({
|
|
48
|
+
endpoint,
|
|
49
|
+
});
|
|
50
|
+
try {
|
|
51
|
+
const s = yield wsClient.send(new client_apigatewaymanagementapi_1.DeleteConnectionCommand({
|
|
52
|
+
ConnectionId: connectionId,
|
|
53
|
+
}));
|
|
54
|
+
if (s.$metadata.httpStatusCode === 200) {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
catch (e) {
|
|
60
|
+
(0, common_1.warn)('Failed to delete WebSocket connection:', e.message);
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Checks if a WebSocket connection is still active
|
|
67
|
+
*/
|
|
68
|
+
function isConnectionActive(_a) {
|
|
69
|
+
return __awaiter(this, arguments, void 0, function* ({ connectionId, endpoint, }) {
|
|
70
|
+
const wsClient = new client_apigatewaymanagementapi_1.ApiGatewayManagementApiClient({
|
|
71
|
+
endpoint,
|
|
72
|
+
});
|
|
73
|
+
try {
|
|
74
|
+
const s = yield wsClient.send(new client_apigatewaymanagementapi_1.GetConnectionCommand({
|
|
75
|
+
ConnectionId: connectionId,
|
|
76
|
+
}));
|
|
77
|
+
if (s.$metadata.httpStatusCode === 200) {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
catch (e) {
|
|
83
|
+
(0, common_1.warn)('Failed to check if WebSocket connection is active:', e.message);
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Broadcasts a message to multiple WebSocket connections
|
|
90
|
+
*/
|
|
91
|
+
function broadcastMessage(_a) {
|
|
92
|
+
return __awaiter(this, arguments, void 0, function* ({ connectionIds, message, endpoint, }) {
|
|
93
|
+
const results = yield Promise.all(connectionIds.map((connectionId) => sendWebSocketMessage({
|
|
94
|
+
connectionId,
|
|
95
|
+
message,
|
|
96
|
+
endpoint,
|
|
97
|
+
})));
|
|
98
|
+
return results.every((r) => r);
|
|
99
|
+
});
|
|
100
|
+
}
|
|
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./api"), exports);
|
|
18
|
+
__exportStar(require("./apigw"), exports);
|
|
18
19
|
__exportStar(require("./aws"), exports);
|
|
19
20
|
__exportStar(require("./dynamo"), exports);
|
|
20
21
|
__exportStar(require("./enforceDynamoProvisionCap"), exports);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Stack } from 'aws-cdk-lib';
|
|
2
|
+
import { aws_iam as iam } from 'aws-cdk-lib';
|
|
3
|
+
import type { IWebSocketApi, WebSocketStage } from 'aws-cdk-lib/aws-apigatewayv2';
|
|
4
|
+
import { WebSocketApi } from 'aws-cdk-lib/aws-apigatewayv2';
|
|
5
|
+
import type { IUser } from 'aws-cdk-lib/aws-iam';
|
|
6
|
+
export declare const generateApigwRef: ({ stack, baseKey, }: {
|
|
7
|
+
stack: Stack;
|
|
8
|
+
baseKey: string;
|
|
9
|
+
}) => {
|
|
10
|
+
api: IWebSocketApi;
|
|
11
|
+
stageName: string;
|
|
12
|
+
};
|
|
13
|
+
export declare const generateApigwSSMParams: ({ stack, api, baseKey, stage, }: {
|
|
14
|
+
stack: Stack;
|
|
15
|
+
api: WebSocketApi;
|
|
16
|
+
baseKey: string;
|
|
17
|
+
stage: WebSocketStage;
|
|
18
|
+
}) => void;
|
|
19
|
+
export declare function grantWebSocketManageConnectionsToUser(stack: Stack, apiGw: IWebSocketApi, stage: string, user: IUser): void;
|
|
20
|
+
export declare function grantWebSocketManageConnectionsToRole(stack: Stack, apiGw: IWebSocketApi, stage: string, ref: {
|
|
21
|
+
addToRolePolicy: (statement: iam.PolicyStatement) => void;
|
|
22
|
+
}): void;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateApigwSSMParams = exports.generateApigwRef = void 0;
|
|
4
|
+
exports.grantWebSocketManageConnectionsToUser = grantWebSocketManageConnectionsToUser;
|
|
5
|
+
exports.grantWebSocketManageConnectionsToRole = grantWebSocketManageConnectionsToRole;
|
|
6
|
+
const aws_cdk_lib_1 = require("aws-cdk-lib");
|
|
7
|
+
const aws_apigatewayv2_1 = require("aws-cdk-lib/aws-apigatewayv2");
|
|
8
|
+
const ssm_1 = require("../ssm");
|
|
9
|
+
const generateApigwRef = ({ stack, baseKey, }) => {
|
|
10
|
+
const webSocketId = (0, ssm_1.loadSsmString)({ stack, path: `${baseKey}/id` });
|
|
11
|
+
const apiEndpoint = (0, ssm_1.loadSsmString)({ stack, path: `${baseKey}/endpoint` });
|
|
12
|
+
const stageName = (0, ssm_1.loadSsmString)({ stack, path: `${baseKey}/stage` });
|
|
13
|
+
const api = aws_apigatewayv2_1.WebSocketApi.fromWebSocketApiAttributes(stack, baseKey, {
|
|
14
|
+
webSocketId,
|
|
15
|
+
apiEndpoint,
|
|
16
|
+
});
|
|
17
|
+
return { api, stageName };
|
|
18
|
+
};
|
|
19
|
+
exports.generateApigwRef = generateApigwRef;
|
|
20
|
+
const generateApigwSSMParams = ({ stack, api, baseKey, stage, }) => {
|
|
21
|
+
(0, ssm_1.saveToSsm)({ stack, path: `${baseKey}/id`, value: api.apiId });
|
|
22
|
+
(0, ssm_1.saveToSsm)({ stack, path: `${baseKey}/endpoint`, value: api.apiEndpoint });
|
|
23
|
+
(0, ssm_1.saveToSsm)({ stack, path: `${baseKey}/stage`, value: stage.stageName });
|
|
24
|
+
};
|
|
25
|
+
exports.generateApigwSSMParams = generateApigwSSMParams;
|
|
26
|
+
function grantWebSocketManageConnectionsToUser(stack, apiGw, stage, user) {
|
|
27
|
+
const statement = new aws_cdk_lib_1.aws_iam.PolicyStatement({
|
|
28
|
+
actions: ['execute-api:ManageConnections'],
|
|
29
|
+
resources: [
|
|
30
|
+
`arn:aws:execute-api:${stack.region}:${stack.account}:${apiGw.apiId}/${stage}/*`,
|
|
31
|
+
],
|
|
32
|
+
});
|
|
33
|
+
const policy = new aws_cdk_lib_1.aws_iam.ManagedPolicy(stack, 'WebSocketConnectionPolicy', {
|
|
34
|
+
statements: [statement],
|
|
35
|
+
});
|
|
36
|
+
user.addManagedPolicy(policy);
|
|
37
|
+
}
|
|
38
|
+
function grantWebSocketManageConnectionsToRole(stack, apiGw, stage, ref) {
|
|
39
|
+
const statement = new aws_cdk_lib_1.aws_iam.PolicyStatement({
|
|
40
|
+
actions: ['execute-api:ManageConnections'],
|
|
41
|
+
resources: [
|
|
42
|
+
`arn:aws:execute-api:${stack.region}:${stack.account}:${apiGw.apiId}/${stage}/*`,
|
|
43
|
+
],
|
|
44
|
+
});
|
|
45
|
+
ref.addToRolePolicy(statement);
|
|
46
|
+
}
|
|
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./apigw"), exports);
|
|
17
18
|
__exportStar(require("./dynamo"), exports);
|
|
18
19
|
__exportStar(require("./s3"), exports);
|
|
19
20
|
__exportStar(require("./sqs"), exports);
|
|
@@ -10,7 +10,7 @@ export declare const getCookie: ({ name, cookieDocument, }: {
|
|
|
10
10
|
cookieDocument?: string;
|
|
11
11
|
}) => string | undefined;
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
13
|
+
* Sets a cookie with the specified name and value
|
|
14
14
|
* @param param0
|
|
15
15
|
* @returns
|
|
16
16
|
*/
|
|
@@ -18,7 +18,11 @@ export declare function setCookie({ name, value, expiryDays, }: {
|
|
|
18
18
|
name: string;
|
|
19
19
|
value: string;
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
21
|
+
* Number of days until the cookie expires
|
|
22
|
+
* - Default: 1 day
|
|
23
|
+
* - Use 0 to set a session cookie (expires when browser closes)
|
|
24
|
+
* - Use a very large number (e.g. 36500 for ~100 years) to effectively disable expiration
|
|
25
|
+
* - Use negative number to delete the cookie
|
|
22
26
|
*/
|
|
23
27
|
expiryDays?: number;
|
|
24
28
|
}): void;
|
|
@@ -28,7 +28,7 @@ exports.getAllCookies = getAllCookies;
|
|
|
28
28
|
const getCookie = ({ name, cookieDocument, }) => (0, exports.getAllCookies)(cookieDocument)[name];
|
|
29
29
|
exports.getCookie = getCookie;
|
|
30
30
|
/**
|
|
31
|
-
*
|
|
31
|
+
* Sets a cookie with the specified name and value
|
|
32
32
|
* @param param0
|
|
33
33
|
* @returns
|
|
34
34
|
*/
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.0.
|
|
2
|
+
"version": "0.0.743",
|
|
3
3
|
"name": "ag-common",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"test": "globstar -- node --import tsx --test \"src/**/*.test.ts\""
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
+
"@aws-sdk/client-apigatewaymanagementapi": "^3.804.0",
|
|
21
22
|
"@aws-sdk/client-dynamodb": "^3.788.0",
|
|
22
23
|
"@aws-sdk/client-s3": "^3.787.0",
|
|
23
24
|
"@aws-sdk/client-ses": "^3.787.0",
|