serverless-simple-middleware 0.0.59 → 0.0.61
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/.nvmrc +1 -1
- package/README.md +0 -1
- package/dist/aws/config.d.ts +1 -1
- package/dist/aws/config.js +20 -13
- package/dist/aws/define.d.ts +0 -12
- package/dist/aws/define.js +5 -2
- package/dist/aws/index.d.ts +3 -3
- package/dist/aws/index.js +19 -3
- package/dist/aws/simple.d.ts +19 -16
- package/dist/aws/simple.js +761 -406
- package/dist/index.d.ts +3 -3
- package/dist/index.js +19 -3
- package/dist/internal/AwsError.d.ts +5 -0
- package/dist/internal/AwsError.js +33 -0
- package/dist/internal/s3.d.ts +38 -0
- package/dist/internal/s3.js +2 -0
- package/dist/middleware/aws.d.ts +2 -2
- package/dist/middleware/aws.js +128 -38
- package/dist/middleware/base.d.ts +3 -2
- package/dist/middleware/base.js +105 -66
- package/dist/middleware/build.d.ts +1 -1
- package/dist/middleware/build.js +240 -90
- package/dist/middleware/index.d.ts +10 -10
- package/dist/middleware/index.js +33 -16
- package/dist/middleware/logger.d.ts +2 -2
- package/dist/middleware/logger.js +74 -13
- package/dist/middleware/mysql.d.ts +5 -10
- package/dist/middleware/mysql.js +272 -173
- package/dist/middleware/trace.d.ts +4 -4
- package/dist/middleware/trace.js +198 -99
- package/dist/utils/index.d.ts +2 -2
- package/dist/utils/index.js +18 -2
- package/dist/utils/logger.d.ts +1 -1
- package/dist/utils/logger.js +33 -25
- package/dist/utils/misc.d.ts +1 -1
- package/dist/utils/misc.js +13 -4
- package/package.json +17 -12
- package/src/aws/config.ts +2 -2
- package/src/aws/define.ts +0 -19
- package/src/aws/index.ts +3 -3
- package/src/aws/simple.ts +308 -207
- package/src/index.ts +3 -3
- package/src/internal/AwsError.ts +13 -0
- package/src/internal/s3.ts +75 -0
- package/src/middleware/aws.ts +4 -4
- package/src/middleware/base.ts +20 -12
- package/src/middleware/build.ts +34 -34
- package/src/middleware/index.ts +10 -10
- package/src/middleware/logger.ts +2 -2
- package/src/middleware/mysql.ts +16 -79
- package/src/middleware/trace.ts +19 -23
- package/src/utils/index.ts +2 -2
- package/src/utils/logger.ts +1 -1
- package/src/utils/misc.ts +11 -2
- package/tsconfig.json +4 -5
package/dist/aws/simple.js
CHANGED
|
@@ -1,259 +1,457 @@
|
|
|
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
|
-
.promise();
|
|
45
|
-
logger.stupid(`attrResult`, attrResult);
|
|
46
|
-
if (!attrResult.Attributes) {
|
|
47
|
-
return 0;
|
|
48
|
-
}
|
|
49
|
-
return +attrResult.Attributes.ApproximateNumberOfMessages;
|
|
50
|
-
};
|
|
51
|
-
this.dequeue = async (queueName, fetchSize = 1, waitSeconds = 1, visibilityTimeout = 15) => {
|
|
52
|
-
logger.debug(`Receive message from queue[${queueName}].`);
|
|
53
|
-
const queueUrl = await this.getQueueUrl(queueName);
|
|
54
|
-
const receiveResult = await this.sqs
|
|
55
|
-
.receiveMessage({
|
|
56
|
-
QueueUrl: queueUrl,
|
|
57
|
-
MaxNumberOfMessages: fetchSize,
|
|
58
|
-
WaitTimeSeconds: waitSeconds,
|
|
59
|
-
VisibilityTimeout: visibilityTimeout,
|
|
60
|
-
})
|
|
61
|
-
.promise();
|
|
62
|
-
logger.stupid(`receiveResult`, receiveResult);
|
|
63
|
-
if (receiveResult.Messages === undefined ||
|
|
64
|
-
receiveResult.Messages.length === 0) {
|
|
65
|
-
return [];
|
|
66
|
-
}
|
|
67
|
-
const data = [];
|
|
68
|
-
for (const each of receiveResult.Messages) {
|
|
69
|
-
if (!each.ReceiptHandle) {
|
|
70
|
-
logger.warn(`No receipt handler: ${JSON.stringify(each)}`);
|
|
71
|
-
continue;
|
|
72
|
-
}
|
|
73
|
-
const message = {
|
|
74
|
-
handle: each.ReceiptHandle,
|
|
75
|
-
body: each.Body ? JSON.parse(each.Body) : undefined,
|
|
76
|
-
};
|
|
77
|
-
data.push(message);
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
78
43
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
50
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
51
|
+
if (ar || !(i in from)) {
|
|
52
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
53
|
+
ar[i] = from[i];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
57
|
+
};
|
|
58
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
59
|
+
exports.SimpleAWS = void 0;
|
|
60
|
+
var cloudfront_signer_1 = require("@aws-sdk/cloudfront-signer");
|
|
61
|
+
var fs = require("fs");
|
|
62
|
+
var os = require("os");
|
|
63
|
+
var non_secure_1 = require("nanoid/non-secure");
|
|
64
|
+
var utils_1 = require("../utils");
|
|
65
|
+
var config_1 = require("./config");
|
|
66
|
+
var define_1 = require("./define");
|
|
67
|
+
var client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
|
|
68
|
+
var client_s3_1 = require("@aws-sdk/client-s3");
|
|
69
|
+
var client_sqs_1 = require("@aws-sdk/client-sqs");
|
|
70
|
+
var lib_dynamodb_1 = require("@aws-sdk/lib-dynamodb");
|
|
71
|
+
var s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner");
|
|
72
|
+
var lib_storage_1 = require("@aws-sdk/lib-storage");
|
|
73
|
+
var logger = (0, utils_1.getLogger)(__filename);
|
|
74
|
+
var SimpleAWS = /** @class */ (function () {
|
|
75
|
+
function SimpleAWS(config) {
|
|
76
|
+
var _this = this;
|
|
77
|
+
this.queueUrls = {};
|
|
78
|
+
this.getQueueUrl = function (queueName) { return __awaiter(_this, void 0, void 0, function () {
|
|
79
|
+
var urlResult;
|
|
80
|
+
return __generator(this, function (_a) {
|
|
81
|
+
switch (_a.label) {
|
|
82
|
+
case 0:
|
|
83
|
+
if (this.queueUrls[queueName] !== undefined) {
|
|
84
|
+
return [2 /*return*/, this.queueUrls[queueName]];
|
|
85
|
+
}
|
|
86
|
+
return [4 /*yield*/, this.sqs.getQueueUrl({
|
|
87
|
+
QueueName: queueName,
|
|
88
|
+
})];
|
|
89
|
+
case 1:
|
|
90
|
+
urlResult = _a.sent();
|
|
91
|
+
logger.stupid("urlResult", urlResult);
|
|
92
|
+
if (!urlResult.QueueUrl) {
|
|
93
|
+
throw new Error("No queue url with name[".concat(queueName, "]"));
|
|
94
|
+
}
|
|
95
|
+
return [2 /*return*/, (this.queueUrls[queueName] = urlResult.QueueUrl)];
|
|
89
96
|
}
|
|
90
|
-
|
|
91
|
-
|
|
97
|
+
});
|
|
98
|
+
}); };
|
|
99
|
+
this.enqueue = function (queueName, data) { return __awaiter(_this, void 0, void 0, function () {
|
|
100
|
+
var queueUrl, sendResult, attrResult;
|
|
101
|
+
var _a;
|
|
102
|
+
return __generator(this, function (_b) {
|
|
103
|
+
switch (_b.label) {
|
|
104
|
+
case 0:
|
|
105
|
+
logger.debug("Send message[".concat(data.key, "] to queue."));
|
|
106
|
+
logger.stupid("data", data);
|
|
107
|
+
return [4 /*yield*/, this.getQueueUrl(queueName)];
|
|
108
|
+
case 1:
|
|
109
|
+
queueUrl = _b.sent();
|
|
110
|
+
return [4 /*yield*/, this.sqs.sendMessage({
|
|
111
|
+
QueueUrl: queueUrl,
|
|
112
|
+
MessageBody: JSON.stringify(data),
|
|
113
|
+
DelaySeconds: 0,
|
|
114
|
+
})];
|
|
115
|
+
case 2:
|
|
116
|
+
sendResult = _b.sent();
|
|
117
|
+
logger.stupid("sendResult", sendResult);
|
|
118
|
+
return [4 /*yield*/, this.sqs.getQueueAttributes({
|
|
119
|
+
QueueUrl: queueUrl,
|
|
120
|
+
AttributeNames: ['ApproximateNumberOfMessages'],
|
|
121
|
+
})];
|
|
122
|
+
case 3:
|
|
123
|
+
attrResult = _b.sent();
|
|
124
|
+
logger.stupid("attrResult", attrResult);
|
|
125
|
+
if (!attrResult.Attributes) {
|
|
126
|
+
return [2 /*return*/, 0];
|
|
127
|
+
}
|
|
128
|
+
return [2 /*return*/, +(((_a = attrResult.Attributes) === null || _a === void 0 ? void 0 : _a.ApproximateNumberOfMessages) || 0)];
|
|
92
129
|
}
|
|
130
|
+
});
|
|
131
|
+
}); };
|
|
132
|
+
this.dequeue = function (queueName_1) {
|
|
133
|
+
var args_1 = [];
|
|
134
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
135
|
+
args_1[_i - 1] = arguments[_i];
|
|
93
136
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
137
|
+
return __awaiter(_this, __spreadArray([queueName_1], args_1, true), void 0, function (queueName, fetchSize, waitSeconds, visibilityTimeout) {
|
|
138
|
+
var queueUrl, receiveResult, data, _a, _b, each, message;
|
|
139
|
+
if (fetchSize === void 0) { fetchSize = 1; }
|
|
140
|
+
if (waitSeconds === void 0) { waitSeconds = 1; }
|
|
141
|
+
if (visibilityTimeout === void 0) { visibilityTimeout = 15; }
|
|
142
|
+
return __generator(this, function (_c) {
|
|
143
|
+
switch (_c.label) {
|
|
144
|
+
case 0:
|
|
145
|
+
logger.debug("Receive message from queue[".concat(queueName, "]."));
|
|
146
|
+
return [4 /*yield*/, this.getQueueUrl(queueName)];
|
|
147
|
+
case 1:
|
|
148
|
+
queueUrl = _c.sent();
|
|
149
|
+
return [4 /*yield*/, this.sqs.receiveMessage({
|
|
150
|
+
QueueUrl: queueUrl,
|
|
151
|
+
MaxNumberOfMessages: fetchSize,
|
|
152
|
+
WaitTimeSeconds: waitSeconds,
|
|
153
|
+
VisibilityTimeout: visibilityTimeout,
|
|
154
|
+
})];
|
|
155
|
+
case 2:
|
|
156
|
+
receiveResult = _c.sent();
|
|
157
|
+
logger.stupid("receiveResult", receiveResult);
|
|
158
|
+
if (receiveResult.Messages === undefined ||
|
|
159
|
+
receiveResult.Messages.length === 0) {
|
|
160
|
+
return [2 /*return*/, []];
|
|
161
|
+
}
|
|
162
|
+
data = [];
|
|
163
|
+
for (_a = 0, _b = receiveResult.Messages; _a < _b.length; _a++) {
|
|
164
|
+
each = _b[_a];
|
|
165
|
+
if (!each.ReceiptHandle) {
|
|
166
|
+
logger.warn("No receipt handler: ".concat(JSON.stringify(each)));
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
message = {
|
|
170
|
+
handle: each.ReceiptHandle,
|
|
171
|
+
body: each.Body ? JSON.parse(each.Body) : undefined,
|
|
172
|
+
};
|
|
173
|
+
data.push(message);
|
|
174
|
+
}
|
|
175
|
+
logger.verbose("Receive a message[".concat(JSON.stringify(data), "] from queue"));
|
|
176
|
+
return [2 /*return*/, data];
|
|
112
177
|
}
|
|
113
178
|
});
|
|
114
|
-
})
|
|
115
|
-
.catch(reject);
|
|
116
|
-
});
|
|
117
|
-
this.completeMessage = async (queueName, handle) => {
|
|
118
|
-
logger.debug(`Complete a message with handle[${handle}]`);
|
|
119
|
-
const queueUrl = await this.getQueueUrl(queueName);
|
|
120
|
-
const deleteResult = await this.sqs
|
|
121
|
-
.deleteMessage({
|
|
122
|
-
QueueUrl: queueUrl,
|
|
123
|
-
ReceiptHandle: handle,
|
|
124
|
-
})
|
|
125
|
-
.promise();
|
|
126
|
-
logger.stupid(`deleteResult`, deleteResult);
|
|
127
|
-
return handle;
|
|
128
|
-
};
|
|
129
|
-
this.completeMessages = async (queueName, handles) => {
|
|
130
|
-
logger.debug(`Complete a message with handle[${handles}]`);
|
|
131
|
-
if (!handles) {
|
|
132
|
-
return handles;
|
|
133
|
-
}
|
|
134
|
-
const chunkSize = 10;
|
|
135
|
-
let index = 0;
|
|
136
|
-
for (let start = 0; start < handles.length; start += chunkSize) {
|
|
137
|
-
const end = Math.min(start + chunkSize, handles.length);
|
|
138
|
-
const sublist = handles.slice(start, end);
|
|
139
|
-
const queueUrl = await this.getQueueUrl(queueName);
|
|
140
|
-
const deletesResult = await this.sqs
|
|
141
|
-
.deleteMessageBatch({
|
|
142
|
-
QueueUrl: queueUrl,
|
|
143
|
-
Entries: sublist.map(handle => ({
|
|
144
|
-
Id: (++index).toString(),
|
|
145
|
-
ReceiptHandle: handle,
|
|
146
|
-
})),
|
|
147
|
-
})
|
|
148
|
-
.promise();
|
|
149
|
-
logger.stupid(`deleteResult`, deletesResult);
|
|
150
|
-
}
|
|
151
|
-
return handles;
|
|
152
|
-
};
|
|
153
|
-
this.download = async (bucket, key, localPath) => {
|
|
154
|
-
logger.debug(`Get a stream of item[${key}] from bucket[${bucket}]`);
|
|
155
|
-
const stream = this.s3
|
|
156
|
-
.getObject({ Bucket: bucket, Key: key })
|
|
157
|
-
.createReadStream();
|
|
158
|
-
return new Promise((resolve, reject) => stream
|
|
159
|
-
.on('error', error => reject(error))
|
|
160
|
-
.pipe(fs.createWriteStream(localPath))
|
|
161
|
-
.on('finish', () => resolve(localPath))
|
|
162
|
-
.on('error', error => reject(error)));
|
|
163
|
-
};
|
|
164
|
-
this.readFile = async (bucket, key) => {
|
|
165
|
-
logger.debug(`Read item[${key}] from bucket[${bucket}]`);
|
|
166
|
-
const tempFile = `${os.tmpdir()}/${nanoid()}`;
|
|
167
|
-
try {
|
|
168
|
-
await this.download(bucket, key, tempFile);
|
|
169
|
-
const content = await fs.promises.readFile(tempFile, {
|
|
170
|
-
encoding: 'utf-8',
|
|
171
|
-
});
|
|
172
|
-
return content;
|
|
173
|
-
}
|
|
174
|
-
finally {
|
|
175
|
-
if (fs.existsSync(tempFile)) {
|
|
176
|
-
try {
|
|
177
|
-
await fs.promises.unlink(tempFile);
|
|
178
|
-
}
|
|
179
|
-
catch (error) {
|
|
180
|
-
logger.error(`Failed to delete temp file ${tempFile}: ${stringifyError(error)}`);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
};
|
|
185
|
-
this.readFileBuffer = async (bucket, key) => {
|
|
186
|
-
logger.debug(`Read item[${key}] from bucket[${bucket}]`);
|
|
187
|
-
const params = {
|
|
188
|
-
Bucket: bucket,
|
|
189
|
-
Key: key,
|
|
190
|
-
};
|
|
191
|
-
const data = await this.s3.getObject(params).promise();
|
|
192
|
-
if (data.Body) {
|
|
193
|
-
return data.Body;
|
|
194
|
-
}
|
|
195
|
-
else {
|
|
196
|
-
throw new Error(`Failed to read file ${key} from bucket ${bucket}`);
|
|
197
|
-
}
|
|
198
|
-
};
|
|
199
|
-
this.upload = async (bucket, localPath, key) => {
|
|
200
|
-
logger.debug(`Upload item[${key}] into bucket[${bucket}]`);
|
|
201
|
-
const putResult = await this.s3
|
|
202
|
-
.upload({
|
|
203
|
-
Bucket: bucket,
|
|
204
|
-
Key: key,
|
|
205
|
-
Body: fs.createReadStream(localPath),
|
|
206
|
-
})
|
|
207
|
-
.promise();
|
|
208
|
-
logger.stupid(`putResult`, putResult);
|
|
209
|
-
return key;
|
|
210
|
-
};
|
|
211
|
-
this.uploadFromBuffer = async (bucket, key, buffer) => {
|
|
212
|
-
logger.debug(`Upload item[${key}] into bucket[${bucket}]`);
|
|
213
|
-
const putResult = await this.s3
|
|
214
|
-
.upload({
|
|
215
|
-
Bucket: bucket,
|
|
216
|
-
Key: key,
|
|
217
|
-
Body: buffer,
|
|
218
|
-
})
|
|
219
|
-
.promise();
|
|
220
|
-
logger.stupid(`putResult`, putResult);
|
|
221
|
-
return key;
|
|
179
|
+
});
|
|
222
180
|
};
|
|
223
|
-
this.
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
await fs.promises.writeFile(tempFile, content, 'utf-8');
|
|
228
|
-
await this.upload(bucket, tempFile, key);
|
|
181
|
+
this.dequeueAll = function (queueName_1) {
|
|
182
|
+
var args_1 = [];
|
|
183
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
184
|
+
args_1[_i - 1] = arguments[_i];
|
|
229
185
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
186
|
+
return __awaiter(_this, __spreadArray([queueName_1], args_1, true), void 0, function (queueName, limitSize, visibilityTimeout) {
|
|
187
|
+
var messages, maxFetchSize, eachOfMessages, _a, eachOfMessages_1, each;
|
|
188
|
+
if (limitSize === void 0) { limitSize = Number.MAX_VALUE; }
|
|
189
|
+
if (visibilityTimeout === void 0) { visibilityTimeout = 15; }
|
|
190
|
+
return __generator(this, function (_b) {
|
|
191
|
+
switch (_b.label) {
|
|
192
|
+
case 0:
|
|
193
|
+
messages = [];
|
|
194
|
+
maxFetchSize = 10;
|
|
195
|
+
_b.label = 1;
|
|
196
|
+
case 1:
|
|
197
|
+
if (!(messages.length < limitSize)) return [3 /*break*/, 3];
|
|
198
|
+
return [4 /*yield*/, this.dequeue(queueName, Math.min(limitSize - messages.length, maxFetchSize), 0, visibilityTimeout)];
|
|
199
|
+
case 2:
|
|
200
|
+
eachOfMessages = _b.sent();
|
|
201
|
+
if (!eachOfMessages || eachOfMessages.length === 0) {
|
|
202
|
+
return [3 /*break*/, 3];
|
|
203
|
+
}
|
|
204
|
+
for (_a = 0, eachOfMessages_1 = eachOfMessages; _a < eachOfMessages_1.length; _a++) {
|
|
205
|
+
each = eachOfMessages_1[_a];
|
|
206
|
+
messages.push(each);
|
|
207
|
+
}
|
|
208
|
+
return [3 /*break*/, 1];
|
|
209
|
+
case 3:
|
|
210
|
+
logger.stupid("messages", messages);
|
|
211
|
+
return [2 /*return*/, messages];
|
|
237
212
|
}
|
|
238
|
-
const msg = `Error during writeFile: unlink file ${tempFile}: ${stringifyError(error)}`;
|
|
239
|
-
logger.error(msg);
|
|
240
213
|
});
|
|
241
|
-
}
|
|
242
|
-
};
|
|
243
|
-
this.getSignedUrl = (bucketName, key, operation = 'getObject', params) => {
|
|
244
|
-
return {
|
|
245
|
-
key,
|
|
246
|
-
url: this.s3.getSignedUrl(operation, {
|
|
247
|
-
Bucket: bucketName,
|
|
248
|
-
Key: key,
|
|
249
|
-
Expires: 60 * 10,
|
|
250
|
-
...(params || {}),
|
|
251
|
-
}),
|
|
252
|
-
};
|
|
214
|
+
});
|
|
253
215
|
};
|
|
254
|
-
this.
|
|
255
|
-
|
|
256
|
-
|
|
216
|
+
this.retainMessage = function (queueName, handle, seconds) { return __awaiter(_this, void 0, void 0, function () {
|
|
217
|
+
var queueUrl;
|
|
218
|
+
return __generator(this, function (_a) {
|
|
219
|
+
switch (_a.label) {
|
|
220
|
+
case 0:
|
|
221
|
+
logger.debug("Change visibilityTimeout of ".concat(handle, " to ").concat(seconds, "secs."));
|
|
222
|
+
return [4 /*yield*/, this.getQueueUrl(queueName)];
|
|
223
|
+
case 1:
|
|
224
|
+
queueUrl = _a.sent();
|
|
225
|
+
return [4 /*yield*/, this.sqs.changeMessageVisibility({
|
|
226
|
+
QueueUrl: queueUrl,
|
|
227
|
+
ReceiptHandle: handle,
|
|
228
|
+
VisibilityTimeout: seconds,
|
|
229
|
+
})];
|
|
230
|
+
case 2:
|
|
231
|
+
_a.sent();
|
|
232
|
+
return [2 /*return*/, handle];
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
}); };
|
|
236
|
+
this.completeMessage = function (queueName, handle) { return __awaiter(_this, void 0, void 0, function () {
|
|
237
|
+
var queueUrl, deleteResult;
|
|
238
|
+
return __generator(this, function (_a) {
|
|
239
|
+
switch (_a.label) {
|
|
240
|
+
case 0:
|
|
241
|
+
logger.debug("Complete a message with handle[".concat(handle, "]"));
|
|
242
|
+
return [4 /*yield*/, this.getQueueUrl(queueName)];
|
|
243
|
+
case 1:
|
|
244
|
+
queueUrl = _a.sent();
|
|
245
|
+
return [4 /*yield*/, this.sqs.deleteMessage({
|
|
246
|
+
QueueUrl: queueUrl,
|
|
247
|
+
ReceiptHandle: handle,
|
|
248
|
+
})];
|
|
249
|
+
case 2:
|
|
250
|
+
deleteResult = _a.sent();
|
|
251
|
+
logger.stupid("deleteResult", deleteResult);
|
|
252
|
+
return [2 /*return*/, handle];
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
}); };
|
|
256
|
+
this.completeMessages = function (queueName, handles) { return __awaiter(_this, void 0, void 0, function () {
|
|
257
|
+
var chunkSize, index, start, end, sublist, queueUrl, deletesResult;
|
|
258
|
+
return __generator(this, function (_a) {
|
|
259
|
+
switch (_a.label) {
|
|
260
|
+
case 0:
|
|
261
|
+
logger.debug("Complete a message with handle[".concat(handles, "]"));
|
|
262
|
+
if (!handles) {
|
|
263
|
+
return [2 /*return*/, handles];
|
|
264
|
+
}
|
|
265
|
+
chunkSize = 10;
|
|
266
|
+
index = 0;
|
|
267
|
+
start = 0;
|
|
268
|
+
_a.label = 1;
|
|
269
|
+
case 1:
|
|
270
|
+
if (!(start < handles.length)) return [3 /*break*/, 5];
|
|
271
|
+
end = Math.min(start + chunkSize, handles.length);
|
|
272
|
+
sublist = handles.slice(start, end);
|
|
273
|
+
return [4 /*yield*/, this.getQueueUrl(queueName)];
|
|
274
|
+
case 2:
|
|
275
|
+
queueUrl = _a.sent();
|
|
276
|
+
return [4 /*yield*/, this.sqs.deleteMessageBatch({
|
|
277
|
+
QueueUrl: queueUrl,
|
|
278
|
+
Entries: sublist.map(function (handle) { return ({
|
|
279
|
+
Id: (++index).toString(),
|
|
280
|
+
ReceiptHandle: handle,
|
|
281
|
+
}); }),
|
|
282
|
+
})];
|
|
283
|
+
case 3:
|
|
284
|
+
deletesResult = _a.sent();
|
|
285
|
+
logger.stupid("deleteResult", deletesResult);
|
|
286
|
+
_a.label = 4;
|
|
287
|
+
case 4:
|
|
288
|
+
start += chunkSize;
|
|
289
|
+
return [3 /*break*/, 1];
|
|
290
|
+
case 5: return [2 /*return*/, handles];
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
}); };
|
|
294
|
+
this.download = function (bucket, key, localPath) { return __awaiter(_this, void 0, void 0, function () {
|
|
295
|
+
var Body;
|
|
296
|
+
return __generator(this, function (_a) {
|
|
297
|
+
switch (_a.label) {
|
|
298
|
+
case 0:
|
|
299
|
+
logger.debug("Get a stream of item[".concat(key, "] from bucket[").concat(bucket, "]"));
|
|
300
|
+
return [4 /*yield*/, this.s3.getObject({ Bucket: bucket, Key: key })];
|
|
301
|
+
case 1:
|
|
302
|
+
Body = (_a.sent()).Body;
|
|
303
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
304
|
+
return Body
|
|
305
|
+
.on('error', function (error) { return reject(error); })
|
|
306
|
+
.pipe(fs.createWriteStream(localPath))
|
|
307
|
+
.on('finish', function () { return resolve(localPath); })
|
|
308
|
+
.on('error', function (error) { return reject(error); });
|
|
309
|
+
})];
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
}); };
|
|
313
|
+
this.readFile = function (bucket, key) { return __awaiter(_this, void 0, void 0, function () {
|
|
314
|
+
var tempFile, content, error_1;
|
|
315
|
+
return __generator(this, function (_a) {
|
|
316
|
+
switch (_a.label) {
|
|
317
|
+
case 0:
|
|
318
|
+
logger.debug("Read item[".concat(key, "] from bucket[").concat(bucket, "]"));
|
|
319
|
+
tempFile = "".concat(os.tmpdir(), "/").concat((0, non_secure_1.nanoid)());
|
|
320
|
+
_a.label = 1;
|
|
321
|
+
case 1:
|
|
322
|
+
_a.trys.push([1, , 4, 9]);
|
|
323
|
+
return [4 /*yield*/, this.download(bucket, key, tempFile)];
|
|
324
|
+
case 2:
|
|
325
|
+
_a.sent();
|
|
326
|
+
return [4 /*yield*/, fs.promises.readFile(tempFile, {
|
|
327
|
+
encoding: 'utf-8',
|
|
328
|
+
})];
|
|
329
|
+
case 3:
|
|
330
|
+
content = _a.sent();
|
|
331
|
+
return [2 /*return*/, content];
|
|
332
|
+
case 4:
|
|
333
|
+
if (!fs.existsSync(tempFile)) return [3 /*break*/, 8];
|
|
334
|
+
_a.label = 5;
|
|
335
|
+
case 5:
|
|
336
|
+
_a.trys.push([5, 7, , 8]);
|
|
337
|
+
return [4 /*yield*/, fs.promises.unlink(tempFile)];
|
|
338
|
+
case 6:
|
|
339
|
+
_a.sent();
|
|
340
|
+
return [3 /*break*/, 8];
|
|
341
|
+
case 7:
|
|
342
|
+
error_1 = _a.sent();
|
|
343
|
+
logger.error("Failed to delete temp file ".concat(tempFile, ": ").concat((0, utils_1.stringifyError)(error_1)));
|
|
344
|
+
return [3 /*break*/, 8];
|
|
345
|
+
case 8: return [7 /*endfinally*/];
|
|
346
|
+
case 9: return [2 /*return*/];
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
}); };
|
|
350
|
+
this.readFileBuffer = function (bucket, key) { return __awaiter(_this, void 0, void 0, function () {
|
|
351
|
+
var Body, buffer;
|
|
352
|
+
return __generator(this, function (_a) {
|
|
353
|
+
switch (_a.label) {
|
|
354
|
+
case 0:
|
|
355
|
+
logger.debug("Read item[".concat(key, "] from bucket[").concat(bucket, "]"));
|
|
356
|
+
return [4 /*yield*/, this.s3.getObject({ Bucket: bucket, Key: key })];
|
|
357
|
+
case 1:
|
|
358
|
+
Body = (_a.sent()).Body;
|
|
359
|
+
return [4 /*yield*/, (Body === null || Body === void 0 ? void 0 : Body.transformToByteArray())];
|
|
360
|
+
case 2:
|
|
361
|
+
buffer = _a.sent();
|
|
362
|
+
if (!buffer) {
|
|
363
|
+
throw new Error("Failed to read file ".concat(key, " from bucket ").concat(bucket));
|
|
364
|
+
}
|
|
365
|
+
return [2 /*return*/, Buffer.from(buffer)];
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
}); };
|
|
369
|
+
this.upload = function (bucket, localPath, key) { return __awaiter(_this, void 0, void 0, function () {
|
|
370
|
+
var upload;
|
|
371
|
+
return __generator(this, function (_a) {
|
|
372
|
+
switch (_a.label) {
|
|
373
|
+
case 0:
|
|
374
|
+
logger.debug("Upload item[".concat(key, "] into bucket[").concat(bucket, "]"));
|
|
375
|
+
upload = new lib_storage_1.Upload({
|
|
376
|
+
client: this.s3,
|
|
377
|
+
params: {
|
|
378
|
+
Bucket: bucket,
|
|
379
|
+
Key: key,
|
|
380
|
+
Body: fs.createReadStream(localPath),
|
|
381
|
+
},
|
|
382
|
+
partSize: 5 * 1024 * 1024, // 5MB
|
|
383
|
+
queueSize: 4,
|
|
384
|
+
});
|
|
385
|
+
return [4 /*yield*/, upload.done()];
|
|
386
|
+
case 1:
|
|
387
|
+
_a.sent();
|
|
388
|
+
return [2 /*return*/, key];
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
}); };
|
|
392
|
+
this.uploadFromBuffer = function (bucket, key, buffer) { return __awaiter(_this, void 0, void 0, function () {
|
|
393
|
+
var upload;
|
|
394
|
+
return __generator(this, function (_a) {
|
|
395
|
+
switch (_a.label) {
|
|
396
|
+
case 0:
|
|
397
|
+
logger.debug("Upload item[".concat(key, "] into bucket[").concat(bucket, "]"));
|
|
398
|
+
upload = new lib_storage_1.Upload({
|
|
399
|
+
client: this.s3,
|
|
400
|
+
params: {
|
|
401
|
+
Bucket: bucket,
|
|
402
|
+
Key: key,
|
|
403
|
+
Body: buffer,
|
|
404
|
+
},
|
|
405
|
+
partSize: 5 * 1024 * 1024, // 5MB
|
|
406
|
+
queueSize: 4,
|
|
407
|
+
});
|
|
408
|
+
return [4 /*yield*/, upload.done()];
|
|
409
|
+
case 1:
|
|
410
|
+
_a.sent();
|
|
411
|
+
return [2 /*return*/, key];
|
|
412
|
+
}
|
|
413
|
+
});
|
|
414
|
+
}); };
|
|
415
|
+
this.writeFile = function (bucket, key, content) { return __awaiter(_this, void 0, void 0, function () {
|
|
416
|
+
var tempFile, error_2, msg;
|
|
417
|
+
return __generator(this, function (_a) {
|
|
418
|
+
switch (_a.label) {
|
|
419
|
+
case 0:
|
|
420
|
+
logger.debug("Write item[".concat(key, "] into bucket[").concat(bucket, "]"));
|
|
421
|
+
tempFile = "".concat(os.tmpdir(), "/").concat((0, non_secure_1.nanoid)());
|
|
422
|
+
_a.label = 1;
|
|
423
|
+
case 1:
|
|
424
|
+
_a.trys.push([1, , 4, 9]);
|
|
425
|
+
return [4 /*yield*/, fs.promises.writeFile(tempFile, content, 'utf-8')];
|
|
426
|
+
case 2:
|
|
427
|
+
_a.sent();
|
|
428
|
+
return [4 /*yield*/, this.upload(bucket, tempFile, key)];
|
|
429
|
+
case 3:
|
|
430
|
+
_a.sent();
|
|
431
|
+
return [3 /*break*/, 9];
|
|
432
|
+
case 4:
|
|
433
|
+
if (!fs.existsSync(tempFile)) {
|
|
434
|
+
return [2 /*return*/];
|
|
435
|
+
}
|
|
436
|
+
_a.label = 5;
|
|
437
|
+
case 5:
|
|
438
|
+
_a.trys.push([5, 7, , 8]);
|
|
439
|
+
return [4 /*yield*/, fs.promises.unlink(tempFile)];
|
|
440
|
+
case 6:
|
|
441
|
+
_a.sent();
|
|
442
|
+
return [3 /*break*/, 8];
|
|
443
|
+
case 7:
|
|
444
|
+
error_2 = _a.sent();
|
|
445
|
+
msg = "Error during writeFile: unlink file ".concat(tempFile, ": ").concat((0, utils_1.stringifyError)(error_2));
|
|
446
|
+
logger.error(msg);
|
|
447
|
+
return [3 /*break*/, 8];
|
|
448
|
+
case 8: return [7 /*endfinally*/];
|
|
449
|
+
case 9: return [2 /*return*/];
|
|
450
|
+
}
|
|
451
|
+
});
|
|
452
|
+
}); };
|
|
453
|
+
this.getSignedCookie = function (keyPairId, privateKey, url, expires) {
|
|
454
|
+
var policy = JSON.stringify({
|
|
257
455
|
Statement: [
|
|
258
456
|
{
|
|
259
457
|
Resource: url,
|
|
@@ -262,149 +460,188 @@ export class SimpleAWS {
|
|
|
262
460
|
},
|
|
263
461
|
},
|
|
264
462
|
],
|
|
265
|
-
};
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
return this.getSignedUrl(bucketName, key, 'getObject', {
|
|
271
|
-
...params,
|
|
272
|
-
ResponseContentDisposition: `attachment; filename="${fileName}"`,
|
|
463
|
+
});
|
|
464
|
+
return (0, cloudfront_signer_1.getSignedCookies)({
|
|
465
|
+
keyPairId: keyPairId,
|
|
466
|
+
privateKey: privateKey,
|
|
467
|
+
policy: policy,
|
|
273
468
|
});
|
|
274
469
|
};
|
|
275
|
-
this.getDynamoDbItem =
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
.
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
470
|
+
this.getDynamoDbItem = function (tableName, key, defaultValue) { return __awaiter(_this, void 0, void 0, function () {
|
|
471
|
+
var getResult, item;
|
|
472
|
+
return __generator(this, function (_a) {
|
|
473
|
+
switch (_a.label) {
|
|
474
|
+
case 0:
|
|
475
|
+
logger.debug("Read an item with key[".concat(JSON.stringify(key), "] from ").concat(tableName, "."));
|
|
476
|
+
return [4 /*yield*/, this.dynamodb.get({
|
|
477
|
+
TableName: tableName,
|
|
478
|
+
Key: key,
|
|
479
|
+
})];
|
|
480
|
+
case 1:
|
|
481
|
+
getResult = _a.sent();
|
|
482
|
+
logger.stupid("getResult", getResult);
|
|
483
|
+
item = getResult !== undefined && getResult.Item !== undefined
|
|
484
|
+
? getResult.Item // Casts forcefully.
|
|
485
|
+
: defaultValue;
|
|
486
|
+
logger.stupid("item", item);
|
|
487
|
+
return [2 /*return*/, item];
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
}); };
|
|
491
|
+
this.updateDynamoDbItem = function (tableName, key, columnValues) { return __awaiter(_this, void 0, void 0, function () {
|
|
492
|
+
var expressions, attributeValues, updateResult;
|
|
493
|
+
return __generator(this, function (_a) {
|
|
494
|
+
switch (_a.label) {
|
|
495
|
+
case 0:
|
|
496
|
+
logger.debug("Update an item with key[".concat(JSON.stringify(key), "] to ").concat(tableName));
|
|
497
|
+
logger.stupid("keyValues", columnValues);
|
|
498
|
+
expressions = Object.keys(columnValues)
|
|
499
|
+
.map(function (column) { return "".concat(column, " = :").concat(column); })
|
|
500
|
+
.join(', ');
|
|
501
|
+
attributeValues = Object.keys(columnValues)
|
|
502
|
+
.map(function (column) { return [":".concat(column), columnValues[column]]; })
|
|
503
|
+
.reduce(function (obj, pair) {
|
|
504
|
+
var _a;
|
|
505
|
+
return (__assign(__assign({}, obj), (_a = {}, _a[pair[0]] = pair[1], _a)));
|
|
506
|
+
}, {});
|
|
507
|
+
logger.stupid("expressions", expressions);
|
|
508
|
+
logger.stupid("attributeValues", attributeValues);
|
|
509
|
+
return [4 /*yield*/, this.dynamodb.update({
|
|
510
|
+
TableName: tableName,
|
|
511
|
+
Key: key,
|
|
512
|
+
UpdateExpression: "set ".concat(expressions),
|
|
513
|
+
ExpressionAttributeValues: attributeValues,
|
|
514
|
+
})];
|
|
515
|
+
case 1:
|
|
516
|
+
updateResult = _a.sent();
|
|
517
|
+
logger.stupid("updateResult", updateResult);
|
|
518
|
+
return [2 /*return*/, updateResult];
|
|
519
|
+
}
|
|
520
|
+
});
|
|
521
|
+
}); };
|
|
312
522
|
// Setup
|
|
313
|
-
this.setupQueue =
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
523
|
+
this.setupQueue = function (queueName) { return __awaiter(_this, void 0, void 0, function () {
|
|
524
|
+
var listResult, _i, _a, queueUrl, error_3, createResult;
|
|
525
|
+
return __generator(this, function (_b) {
|
|
526
|
+
switch (_b.label) {
|
|
527
|
+
case 0:
|
|
528
|
+
_b.trys.push([0, 2, , 3]);
|
|
529
|
+
return [4 /*yield*/, this.sqs.listQueues({
|
|
530
|
+
QueueNamePrefix: queueName,
|
|
531
|
+
})];
|
|
532
|
+
case 1:
|
|
533
|
+
listResult = _b.sent();
|
|
534
|
+
if (listResult.QueueUrls) {
|
|
535
|
+
for (_i = 0, _a = listResult.QueueUrls; _i < _a.length; _i++) {
|
|
536
|
+
queueUrl = _a[_i];
|
|
537
|
+
if (queueUrl.endsWith(queueName)) {
|
|
538
|
+
logger.debug("Queue[".concat(queueName, " => ").concat(queueUrl, "] already exists."));
|
|
539
|
+
return [2 /*return*/, true];
|
|
540
|
+
}
|
|
541
|
+
}
|
|
325
542
|
}
|
|
326
|
-
|
|
543
|
+
return [3 /*break*/, 3];
|
|
544
|
+
case 2:
|
|
545
|
+
error_3 = _b.sent();
|
|
546
|
+
logger.debug("No Queue[".concat(queueName, "] exists due to ").concat(error_3));
|
|
547
|
+
return [3 /*break*/, 3];
|
|
548
|
+
case 3:
|
|
549
|
+
logger.debug("Create a queue[".concat(queueName, "] newly."));
|
|
550
|
+
return [4 /*yield*/, this.sqs.createQueue({
|
|
551
|
+
QueueName: queueName,
|
|
552
|
+
})];
|
|
553
|
+
case 4:
|
|
554
|
+
createResult = _b.sent();
|
|
555
|
+
logger.stupid("createResult", createResult);
|
|
556
|
+
return [2 /*return*/, true];
|
|
327
557
|
}
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
558
|
+
});
|
|
559
|
+
}); };
|
|
560
|
+
this.setupStorage = function (bucketName, cors) { return __awaiter(_this, void 0, void 0, function () {
|
|
561
|
+
var listResult, error_4, createResult, corsResult;
|
|
562
|
+
return __generator(this, function (_a) {
|
|
563
|
+
switch (_a.label) {
|
|
564
|
+
case 0:
|
|
565
|
+
_a.trys.push([0, 2, , 3]);
|
|
566
|
+
return [4 /*yield*/, this.s3.listBuckets()];
|
|
567
|
+
case 1:
|
|
568
|
+
listResult = _a.sent();
|
|
569
|
+
if (listResult.Buckets &&
|
|
570
|
+
listResult.Buckets.map(function (each) { return each.Name; }).includes(bucketName)) {
|
|
571
|
+
logger.debug("Bucket[".concat(bucketName, "] already exists."));
|
|
572
|
+
return [2 /*return*/, true];
|
|
573
|
+
}
|
|
574
|
+
return [3 /*break*/, 3];
|
|
575
|
+
case 2:
|
|
576
|
+
error_4 = _a.sent();
|
|
577
|
+
logger.debug("No bucket[".concat(bucketName, "] exists due to ").concat(error_4));
|
|
578
|
+
return [3 /*break*/, 3];
|
|
579
|
+
case 3:
|
|
580
|
+
logger.debug("Create a bucket[".concat(bucketName, "] newly."));
|
|
581
|
+
return [4 /*yield*/, this.s3.createBucket({
|
|
582
|
+
Bucket: bucketName,
|
|
583
|
+
})];
|
|
584
|
+
case 4:
|
|
585
|
+
createResult = _a.sent();
|
|
586
|
+
logger.stupid("createResult", createResult);
|
|
587
|
+
if (!cors) return [3 /*break*/, 6];
|
|
588
|
+
return [4 /*yield*/, this.s3.putBucketCors({
|
|
589
|
+
Bucket: bucketName,
|
|
590
|
+
CORSConfiguration: {
|
|
591
|
+
CORSRules: [
|
|
592
|
+
{
|
|
593
|
+
AllowedHeaders: ['*'],
|
|
594
|
+
AllowedMethods: cors.methods,
|
|
595
|
+
AllowedOrigins: cors.origins,
|
|
596
|
+
},
|
|
597
|
+
],
|
|
598
|
+
},
|
|
599
|
+
})];
|
|
600
|
+
case 5:
|
|
601
|
+
corsResult = _a.sent();
|
|
602
|
+
logger.stupid("corsResult", corsResult);
|
|
603
|
+
_a.label = 6;
|
|
604
|
+
case 6: return [2 /*return*/, true];
|
|
348
605
|
}
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
return true;
|
|
606
|
+
});
|
|
607
|
+
}); };
|
|
608
|
+
this.setupDynamoDb = function (tableName, keyColumn) { return __awaiter(_this, void 0, void 0, function () {
|
|
609
|
+
var listResult, error_5, createResult;
|
|
610
|
+
return __generator(this, function (_a) {
|
|
611
|
+
switch (_a.label) {
|
|
612
|
+
case 0:
|
|
613
|
+
_a.trys.push([0, 2, , 3]);
|
|
614
|
+
return [4 /*yield*/, this.dynamodbAdmin.listTables()];
|
|
615
|
+
case 1:
|
|
616
|
+
listResult = _a.sent();
|
|
617
|
+
if (listResult.TableNames && listResult.TableNames.includes(tableName)) {
|
|
618
|
+
logger.debug("Table[".concat(tableName, "] already exists."));
|
|
619
|
+
return [2 /*return*/, true];
|
|
620
|
+
}
|
|
621
|
+
return [3 /*break*/, 3];
|
|
622
|
+
case 2:
|
|
623
|
+
error_5 = _a.sent();
|
|
624
|
+
logger.debug("No table[".concat(tableName, "] exists due to ").concat(error_5));
|
|
625
|
+
return [3 /*break*/, 3];
|
|
626
|
+
case 3:
|
|
627
|
+
logger.debug("Create a table[".concat(tableName, "] newly."));
|
|
628
|
+
return [4 /*yield*/, this.dynamodbAdmin.createTable({
|
|
629
|
+
TableName: tableName,
|
|
630
|
+
KeySchema: [{ AttributeName: keyColumn, KeyType: 'HASH' }],
|
|
631
|
+
AttributeDefinitions: [{ AttributeName: keyColumn, AttributeType: 'S' }],
|
|
632
|
+
ProvisionedThroughput: {
|
|
633
|
+
ReadCapacityUnits: 30,
|
|
634
|
+
WriteCapacityUnits: 10,
|
|
635
|
+
},
|
|
636
|
+
})];
|
|
637
|
+
case 4:
|
|
638
|
+
createResult = _a.sent();
|
|
639
|
+
logger.stupid("createResult", createResult);
|
|
640
|
+
return [2 /*return*/, true];
|
|
385
641
|
}
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
}
|
|
390
|
-
logger.debug(`Create a table[${tableName}] newly.`);
|
|
391
|
-
const createResult = await this.dynamodbAdmin
|
|
392
|
-
.createTable({
|
|
393
|
-
TableName: tableName,
|
|
394
|
-
KeySchema: [{ AttributeName: keyColumn, KeyType: 'HASH' }],
|
|
395
|
-
AttributeDefinitions: [
|
|
396
|
-
{ AttributeName: keyColumn, AttributeType: 'S' },
|
|
397
|
-
],
|
|
398
|
-
ProvisionedThroughput: {
|
|
399
|
-
ReadCapacityUnits: 30,
|
|
400
|
-
WriteCapacityUnits: 10,
|
|
401
|
-
},
|
|
402
|
-
})
|
|
403
|
-
.promise();
|
|
404
|
-
logger.stupid(`createResult`, createResult);
|
|
405
|
-
return true;
|
|
406
|
-
};
|
|
407
|
-
this.config = config || new SimpleAWSConfig();
|
|
642
|
+
});
|
|
643
|
+
}); };
|
|
644
|
+
this.config = config || new config_1.SimpleAWSConfig();
|
|
408
645
|
/**
|
|
409
646
|
* The simple cache for { queueName: queueUrl }.
|
|
410
647
|
* It can help in the only case of launching this project as offline.
|
|
@@ -412,28 +649,146 @@ export class SimpleAWS {
|
|
|
412
649
|
*/
|
|
413
650
|
this.queueUrls = {};
|
|
414
651
|
}
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
this.lazyS3
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
652
|
+
Object.defineProperty(SimpleAWS.prototype, "s3", {
|
|
653
|
+
get: function () {
|
|
654
|
+
if (this.lazyS3 === undefined) {
|
|
655
|
+
this.lazyS3 = new client_s3_1.S3(this.config.get(define_1.AWSComponent.s3) || {});
|
|
656
|
+
}
|
|
657
|
+
return this.lazyS3;
|
|
658
|
+
},
|
|
659
|
+
enumerable: false,
|
|
660
|
+
configurable: true
|
|
661
|
+
});
|
|
662
|
+
Object.defineProperty(SimpleAWS.prototype, "sqs", {
|
|
663
|
+
get: function () {
|
|
664
|
+
if (this.lazySqs === undefined) {
|
|
665
|
+
this.lazySqs = new client_sqs_1.SQS(this.config.get(define_1.AWSComponent.sqs) || {});
|
|
666
|
+
}
|
|
667
|
+
return this.lazySqs;
|
|
668
|
+
},
|
|
669
|
+
enumerable: false,
|
|
670
|
+
configurable: true
|
|
671
|
+
});
|
|
672
|
+
Object.defineProperty(SimpleAWS.prototype, "dynamodb", {
|
|
673
|
+
get: function () {
|
|
674
|
+
if (this.lazyDynamodb === undefined) {
|
|
675
|
+
this.lazyDynamodb = lib_dynamodb_1.DynamoDBDocument.from(new client_dynamodb_1.DynamoDBClient(this.config.get(define_1.AWSComponent.dynamodb) || {}), {
|
|
676
|
+
marshallOptions: {
|
|
677
|
+
convertEmptyValues: true,
|
|
678
|
+
removeUndefinedValues: true,
|
|
679
|
+
},
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
return this.lazyDynamodb;
|
|
683
|
+
},
|
|
684
|
+
enumerable: false,
|
|
685
|
+
configurable: true
|
|
686
|
+
});
|
|
687
|
+
Object.defineProperty(SimpleAWS.prototype, "dynamodbAdmin", {
|
|
688
|
+
get: function () {
|
|
689
|
+
if (this.lazyDynamodbAdmin === undefined) {
|
|
690
|
+
this.lazyDynamodbAdmin = new client_dynamodb_1.DynamoDB(this.config.get(define_1.AWSComponent.dynamodb) || {});
|
|
691
|
+
}
|
|
692
|
+
return this.lazyDynamodbAdmin;
|
|
693
|
+
},
|
|
694
|
+
enumerable: false,
|
|
695
|
+
configurable: true
|
|
696
|
+
});
|
|
697
|
+
SimpleAWS.prototype.getSignedUrl = function (options) {
|
|
698
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
699
|
+
var _a, expiresIn, unhoistableHeaders, cmd, cmd, cmd, cmd, cmd, cmd, cmd, cmd, cmd, cmd, cmd, cmd;
|
|
700
|
+
return __generator(this, function (_b) {
|
|
701
|
+
_a = options.expiresIn, expiresIn = _a === void 0 ? 600 : _a, unhoistableHeaders = options.unhoistableHeaders;
|
|
702
|
+
switch (options.operation) {
|
|
703
|
+
case 'putObject': {
|
|
704
|
+
cmd = new client_s3_1.PutObjectCommand(__assign({ Bucket: options.bucket, Key: options.key }, options.params));
|
|
705
|
+
return [2 /*return*/, (0, s3_request_presigner_1.getSignedUrl)(this.s3, cmd, {
|
|
706
|
+
expiresIn: expiresIn,
|
|
707
|
+
unhoistableHeaders: unhoistableHeaders,
|
|
708
|
+
})];
|
|
709
|
+
}
|
|
710
|
+
case 'getObject': {
|
|
711
|
+
cmd = new client_s3_1.GetObjectCommand(__assign({ Bucket: options.bucket, Key: options.key }, options.params));
|
|
712
|
+
return [2 /*return*/, (0, s3_request_presigner_1.getSignedUrl)(this.s3, cmd, {
|
|
713
|
+
expiresIn: expiresIn,
|
|
714
|
+
unhoistableHeaders: unhoistableHeaders,
|
|
715
|
+
})];
|
|
716
|
+
}
|
|
717
|
+
case 'deleteObject': {
|
|
718
|
+
cmd = new client_s3_1.DeleteObjectCommand(__assign({ Bucket: options.bucket, Key: options.key }, options.params));
|
|
719
|
+
return [2 /*return*/, (0, s3_request_presigner_1.getSignedUrl)(this.s3, cmd, {
|
|
720
|
+
expiresIn: expiresIn,
|
|
721
|
+
unhoistableHeaders: unhoistableHeaders,
|
|
722
|
+
})];
|
|
723
|
+
}
|
|
724
|
+
case 'headObject': {
|
|
725
|
+
cmd = new client_s3_1.HeadObjectCommand(__assign({ Bucket: options.bucket, Key: options.key }, options.params));
|
|
726
|
+
return [2 /*return*/, (0, s3_request_presigner_1.getSignedUrl)(this.s3, cmd, {
|
|
727
|
+
expiresIn: expiresIn,
|
|
728
|
+
unhoistableHeaders: unhoistableHeaders,
|
|
729
|
+
})];
|
|
730
|
+
}
|
|
731
|
+
case 'copyObject': {
|
|
732
|
+
cmd = new client_s3_1.CopyObjectCommand(__assign({ Bucket: options.bucket, Key: options.key }, options.params));
|
|
733
|
+
return [2 /*return*/, (0, s3_request_presigner_1.getSignedUrl)(this.s3, cmd, {
|
|
734
|
+
expiresIn: expiresIn,
|
|
735
|
+
unhoistableHeaders: unhoistableHeaders,
|
|
736
|
+
})];
|
|
737
|
+
}
|
|
738
|
+
case 'uploadPart': {
|
|
739
|
+
cmd = new client_s3_1.UploadPartCommand(__assign({ Bucket: options.bucket, Key: options.key }, options.params));
|
|
740
|
+
return [2 /*return*/, (0, s3_request_presigner_1.getSignedUrl)(this.s3, cmd, {
|
|
741
|
+
expiresIn: expiresIn,
|
|
742
|
+
unhoistableHeaders: unhoistableHeaders,
|
|
743
|
+
})];
|
|
744
|
+
}
|
|
745
|
+
case 'uploadPartCopy': {
|
|
746
|
+
cmd = new client_s3_1.UploadPartCopyCommand(__assign({ Bucket: options.bucket, Key: options.key }, options.params));
|
|
747
|
+
return [2 /*return*/, (0, s3_request_presigner_1.getSignedUrl)(this.s3, cmd, {
|
|
748
|
+
expiresIn: expiresIn,
|
|
749
|
+
unhoistableHeaders: unhoistableHeaders,
|
|
750
|
+
})];
|
|
751
|
+
}
|
|
752
|
+
case 'listObjectsV2': {
|
|
753
|
+
cmd = new client_s3_1.ListObjectsV2Command(__assign({ Bucket: options.bucket }, options.params));
|
|
754
|
+
return [2 /*return*/, (0, s3_request_presigner_1.getSignedUrl)(this.s3, cmd, {
|
|
755
|
+
expiresIn: expiresIn,
|
|
756
|
+
unhoistableHeaders: unhoistableHeaders,
|
|
757
|
+
})];
|
|
758
|
+
}
|
|
759
|
+
case 'createMultipartUpload': {
|
|
760
|
+
cmd = new client_s3_1.CreateMultipartUploadCommand(__assign({ Bucket: options.bucket, Key: options.key }, options.params));
|
|
761
|
+
return [2 /*return*/, (0, s3_request_presigner_1.getSignedUrl)(this.s3, cmd, {
|
|
762
|
+
expiresIn: expiresIn,
|
|
763
|
+
unhoistableHeaders: unhoistableHeaders,
|
|
764
|
+
})];
|
|
765
|
+
}
|
|
766
|
+
case 'completeMultipartUpload': {
|
|
767
|
+
cmd = new client_s3_1.CompleteMultipartUploadCommand(__assign({ Bucket: options.bucket, Key: options.key }, options.params));
|
|
768
|
+
return [2 /*return*/, (0, s3_request_presigner_1.getSignedUrl)(this.s3, cmd, {
|
|
769
|
+
expiresIn: expiresIn,
|
|
770
|
+
unhoistableHeaders: unhoistableHeaders,
|
|
771
|
+
})];
|
|
772
|
+
}
|
|
773
|
+
case 'abortMultipartUpload': {
|
|
774
|
+
cmd = new client_s3_1.AbortMultipartUploadCommand(__assign({ Bucket: options.bucket, Key: options.key }, options.params));
|
|
775
|
+
return [2 /*return*/, (0, s3_request_presigner_1.getSignedUrl)(this.s3, cmd, {
|
|
776
|
+
expiresIn: expiresIn,
|
|
777
|
+
unhoistableHeaders: unhoistableHeaders,
|
|
778
|
+
})];
|
|
779
|
+
}
|
|
780
|
+
case 'listParts': {
|
|
781
|
+
cmd = new client_s3_1.ListPartsCommand(__assign({ Bucket: options.bucket, Key: options.key }, options.params));
|
|
782
|
+
return [2 /*return*/, (0, s3_request_presigner_1.getSignedUrl)(this.s3, cmd, {
|
|
783
|
+
expiresIn: expiresIn,
|
|
784
|
+
unhoistableHeaders: unhoistableHeaders,
|
|
785
|
+
})];
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
return [2 /*return*/];
|
|
789
|
+
});
|
|
790
|
+
});
|
|
791
|
+
};
|
|
792
|
+
return SimpleAWS;
|
|
793
|
+
}());
|
|
794
|
+
exports.SimpleAWS = SimpleAWS;
|