ag-common 0.0.740 → 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.
- package/dist/api/helpers/apigw.d.ts +30 -0
- package/dist/api/helpers/apigw.js +100 -0
- package/dist/api/helpers/dynamo/delete.js +3 -3
- package/dist/api/helpers/dynamo/get.js +9 -11
- package/dist/api/helpers/dynamo/set.js +6 -6
- package/dist/api/helpers/index.d.ts +1 -0
- package/dist/api/helpers/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
|
+
}
|
|
@@ -30,14 +30,14 @@ const batchDelete = (params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
30
30
|
const chunked = (0, array_1.chunk)(params.keys, batchSize);
|
|
31
31
|
let processed = 0;
|
|
32
32
|
yield (0, async_1.asyncForEach)(chunked, (chunk) => __awaiter(void 0, void 0, void 0, function* () {
|
|
33
|
-
const
|
|
33
|
+
const batchDeleteParams = {
|
|
34
34
|
RequestItems: {
|
|
35
35
|
[params.tableName]: chunk.map((key) => ({
|
|
36
36
|
DeleteRequest: { Key: { [params.pkName]: key } },
|
|
37
37
|
})),
|
|
38
38
|
},
|
|
39
|
-
}
|
|
40
|
-
yield (0, withRetry_1.withRetry)(() => _1.dynamoDb.send(
|
|
39
|
+
};
|
|
40
|
+
yield (0, withRetry_1.withRetry)(() => _1.dynamoDb.send(new lib_dynamodb_1.BatchWriteCommand(batchDeleteParams)), `batchdelete ${processed}/${params.keys.length}. size=${batchSize}`, {
|
|
41
41
|
maxRetries: alwaysRetry ? null : undefined,
|
|
42
42
|
});
|
|
43
43
|
processed += chunk.length;
|
|
@@ -62,11 +62,10 @@ const executeQuery = (params, startKey) => __awaiter(void 0, void 0, void 0, fun
|
|
|
62
62
|
eav[`:${skName.toLowerCase()}`] = skValue;
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
|
-
const queryParams =
|
|
65
|
+
const queryParams = Object.assign({ TableName: params.tableName, KeyConditionExpression: kce, ExpressionAttributeNames: Object.assign(Object.assign({}, ean), (_b = params.filter) === null || _b === void 0 ? void 0 : _b.attrNames), ExpressionAttributeValues: Object.assign(Object.assign({}, eav), (_c = params.filter) === null || _c === void 0 ? void 0 : _c.attrValues), ScanIndexForward: (_d = params.sortAscending) !== null && _d !== void 0 ? _d : true, Limit: (_e = params.BATCH_SIZE) !== null && _e !== void 0 ? _e : params.limit, IndexName: params.indexName, ExclusiveStartKey: startKey }, (params.filter && Object.assign({ FilterExpression: params.filter.filterExpression }, (params.filter.attrValues && {
|
|
66
66
|
ExpressionAttributeValues: Object.assign(Object.assign({}, eav), params.filter.attrValues),
|
|
67
|
-
}))))
|
|
68
|
-
|
|
69
|
-
return yield (0, withRetry_1.withRetry)(() => _1.dynamoDb.send(queryParams), 'queryDynamo');
|
|
67
|
+
}))));
|
|
68
|
+
return (0, withRetry_1.withRetry)(() => _1.dynamoDb.send(new lib_dynamodb_1.QueryCommand(queryParams)), 'queryDynamo');
|
|
70
69
|
});
|
|
71
70
|
/**
|
|
72
71
|
* Helper function that builds the scan parameters and executes the scan
|
|
@@ -78,7 +77,7 @@ const executeScan = (tableName, options, exclusiveStartKey) => __awaiter(void 0,
|
|
|
78
77
|
return acc;
|
|
79
78
|
}, {});
|
|
80
79
|
const expressionAttributeNames = Object.assign(Object.assign({}, projectionAttrs), (_b = options === null || options === void 0 ? void 0 : options.filter) === null || _b === void 0 ? void 0 : _b.attrNames);
|
|
81
|
-
const
|
|
80
|
+
const scanParams = Object.assign(Object.assign(Object.assign(Object.assign({ TableName: tableName, IndexName: options === null || options === void 0 ? void 0 : options.indexName, Limit: options === null || options === void 0 ? void 0 : options.BATCH_SIZE }, ((options === null || options === void 0 ? void 0 : options.filter) && Object.assign({ FilterExpression: options.filter.filterExpression }, (options.filter.attrValues && {
|
|
82
81
|
ExpressionAttributeValues: options.filter.attrValues,
|
|
83
82
|
})))), (Object.keys(expressionAttributeNames).length > 0 && {
|
|
84
83
|
ExpressionAttributeNames: expressionAttributeNames,
|
|
@@ -86,16 +85,15 @@ const executeScan = (tableName, options, exclusiveStartKey) => __awaiter(void 0,
|
|
|
86
85
|
ProjectionExpression: options.requiredAttributeList
|
|
87
86
|
.map((_, index) => `#proj${index}`)
|
|
88
87
|
.join(', '),
|
|
89
|
-
})), { ExclusiveStartKey: exclusiveStartKey })
|
|
90
|
-
|
|
91
|
-
return yield (0, withRetry_1.withRetry)(() => _1.dynamoDb.send(params), `scan. already seen=${exclusiveStartKey ? 'some' : '0'} items`, {
|
|
88
|
+
})), { ExclusiveStartKey: exclusiveStartKey });
|
|
89
|
+
return (0, withRetry_1.withRetry)(() => _1.dynamoDb.send(new lib_dynamodb_1.ScanCommand(scanParams)), `scan. already seen=${exclusiveStartKey ? 'some' : '0'} items`, {
|
|
92
90
|
maxRetries: (options === null || options === void 0 ? void 0 : options.alwaysRetry) ? null : undefined,
|
|
93
91
|
});
|
|
94
92
|
});
|
|
95
93
|
const getItemsDynamo = (params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
96
94
|
var _a, _b;
|
|
97
95
|
try {
|
|
98
|
-
const
|
|
96
|
+
const batchGetParams = {
|
|
99
97
|
RequestItems: {
|
|
100
98
|
[params.tableName]: {
|
|
101
99
|
Keys: params.items.map(({ pkName, pkValue }) => ({
|
|
@@ -103,8 +101,8 @@ const getItemsDynamo = (params) => __awaiter(void 0, void 0, void 0, function* (
|
|
|
103
101
|
})),
|
|
104
102
|
},
|
|
105
103
|
},
|
|
106
|
-
}
|
|
107
|
-
const result = yield (0, withRetry_1.withRetry)(() => _1.dynamoDb.send(
|
|
104
|
+
};
|
|
105
|
+
const result = yield (0, withRetry_1.withRetry)(() => _1.dynamoDb.send(new lib_dynamodb_1.BatchGetCommand(batchGetParams)), 'getItemsDynamo');
|
|
108
106
|
return {
|
|
109
107
|
data: (_b = (_a = result.Responses) === null || _a === void 0 ? void 0 : _a[params.tableName]) !== null && _b !== void 0 ? _b : [],
|
|
110
108
|
};
|
|
@@ -16,11 +16,11 @@ const async_1 = require("../../../common/helpers/async");
|
|
|
16
16
|
const withRetry_1 = require("../withRetry");
|
|
17
17
|
const _1 = require(".");
|
|
18
18
|
const putDynamo = (item, tableName, opt) => __awaiter(void 0, void 0, void 0, function* () {
|
|
19
|
-
const
|
|
19
|
+
const putParams = Object.assign({ TableName: tableName, Item: item }, ((opt === null || opt === void 0 ? void 0 : opt.pkName) && {
|
|
20
20
|
ConditionExpression: `attribute_not_exists(${opt.pkName})`,
|
|
21
|
-
}))
|
|
21
|
+
}));
|
|
22
22
|
try {
|
|
23
|
-
yield (0, withRetry_1.withRetry)(() => _1.dynamoDb.send(
|
|
23
|
+
yield (0, withRetry_1.withRetry)(() => _1.dynamoDb.send(new lib_dynamodb_1.PutCommand(putParams)), 'putDynamo');
|
|
24
24
|
return { data: undefined };
|
|
25
25
|
}
|
|
26
26
|
catch (e) {
|
|
@@ -34,12 +34,12 @@ const batchWrite = (tableName, items, opt) => __awaiter(void 0, void 0, void 0,
|
|
|
34
34
|
let processed = 0;
|
|
35
35
|
const chunked = (0, array_1.chunk)(items, batchSize);
|
|
36
36
|
yield (0, async_1.asyncForEach)(chunked, (chunk) => __awaiter(void 0, void 0, void 0, function* () {
|
|
37
|
-
const
|
|
37
|
+
const batchWriteParams = {
|
|
38
38
|
RequestItems: {
|
|
39
39
|
[tableName]: chunk.map((Item) => ({ PutRequest: { Item } })),
|
|
40
40
|
},
|
|
41
|
-
}
|
|
42
|
-
yield (0, withRetry_1.withRetry)(() => _1.dynamoDb.send(
|
|
41
|
+
};
|
|
42
|
+
yield (0, withRetry_1.withRetry)(() => _1.dynamoDb.send(new lib_dynamodb_1.BatchWriteCommand(batchWriteParams)), `batchwrite ${processed}/${items.length}. size=${batchSize}`, {
|
|
43
43
|
maxRetries: (opt === null || opt === void 0 ? void 0 : opt.alwaysRetry) ? null : undefined,
|
|
44
44
|
});
|
|
45
45
|
processed += chunk.length;
|
|
@@ -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",
|