ag-common 0.0.741 → 0.0.742
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.
|
@@ -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);
|
|
@@ -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.742",
|
|
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",
|