serverless-simple-middleware 0.0.60 → 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 -0
- package/README.md +0 -1
- package/dist/aws/config.d.ts +2 -2
- package/dist/aws/config.js +6 -4
- package/dist/aws/define.d.ts +0 -12
- package/dist/aws/define.js +2 -1
- package/dist/aws/index.js +17 -6
- package/dist/aws/simple.d.ts +18 -15
- package/dist/aws/simple.js +317 -225
- package/dist/index.js +17 -6
- 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 +12 -9
- package/dist/middleware/base.d.ts +8 -8
- package/dist/middleware/base.js +9 -8
- package/dist/middleware/build.d.ts +1 -1
- package/dist/middleware/build.js +68 -45
- package/dist/middleware/index.d.ts +2 -2
- package/dist/middleware/index.js +20 -8
- package/dist/middleware/logger.js +8 -4
- package/dist/middleware/mysql.d.ts +3 -3
- package/dist/middleware/mysql.js +17 -13
- package/dist/middleware/trace.d.ts +2 -2
- package/dist/middleware/trace.js +21 -21
- package/dist/utils/index.js +16 -5
- package/dist/utils/logger.d.ts +3 -3
- package/dist/utils/logger.js +9 -7
- package/dist/utils/misc.d.ts +1 -1
- package/dist/utils/misc.js +10 -3
- package/package.json +18 -10
- package/src/aws/config.ts +1 -1
- package/src/aws/define.ts +0 -19
- package/src/aws/simple.ts +306 -204
- package/src/internal/AwsError.ts +13 -0
- package/src/internal/s3.ts +75 -0
- package/src/middleware/aws.ts +1 -1
- package/src/middleware/base.ts +5 -4
- package/src/middleware/build.ts +31 -31
- package/src/middleware/mysql.ts +11 -7
- package/src/middleware/trace.ts +15 -19
- package/src/utils/misc.ts +11 -2
package/src/aws/simple.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
CloudfrontSignedCookiesOutput,
|
|
3
|
+
getSignedCookies,
|
|
4
|
+
} from '@aws-sdk/cloudfront-signer';
|
|
5
|
+
|
|
2
6
|
import * as fs from 'fs';
|
|
3
7
|
import * as os from 'os';
|
|
4
8
|
import { nanoid } from 'nanoid/non-secure';
|
|
@@ -6,22 +10,38 @@ import { nanoid } from 'nanoid/non-secure';
|
|
|
6
10
|
import { getLogger, stringifyError } from '../utils';
|
|
7
11
|
import { SimpleAWSConfig } from './config';
|
|
8
12
|
|
|
13
|
+
import { AWSComponent, SQSMessageBody } from './define';
|
|
14
|
+
import { DynamoDB, DynamoDBClient } from '@aws-sdk/client-dynamodb';
|
|
9
15
|
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
AbortMultipartUploadCommand,
|
|
17
|
+
CompleteMultipartUploadCommand,
|
|
18
|
+
CopyObjectCommand,
|
|
19
|
+
CreateMultipartUploadCommand,
|
|
20
|
+
DeleteObjectCommand,
|
|
21
|
+
GetObjectCommand,
|
|
22
|
+
HeadObjectCommand,
|
|
23
|
+
ListObjectsV2Command,
|
|
24
|
+
ListPartsCommand,
|
|
25
|
+
PutObjectCommand,
|
|
26
|
+
S3,
|
|
27
|
+
UploadPartCommand,
|
|
28
|
+
UploadPartCopyCommand,
|
|
29
|
+
} from '@aws-sdk/client-s3';
|
|
30
|
+
import { SQS } from '@aws-sdk/client-sqs';
|
|
31
|
+
import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb';
|
|
32
|
+
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
33
|
+
import { PresignerOptions } from '../internal/s3';
|
|
34
|
+
import { Upload } from '@aws-sdk/lib-storage';
|
|
15
35
|
|
|
16
36
|
const logger = getLogger(__filename);
|
|
17
37
|
|
|
18
38
|
export class SimpleAWS {
|
|
19
|
-
private queueUrls: { [queueName: string]: string };
|
|
39
|
+
private queueUrls: { [queueName: string]: string } = {};
|
|
20
40
|
private config: SimpleAWSConfig;
|
|
21
|
-
private lazyS3:
|
|
22
|
-
private lazySqs:
|
|
23
|
-
private lazyDynamodb:
|
|
24
|
-
private lazyDynamodbAdmin:
|
|
41
|
+
private lazyS3: S3 | undefined;
|
|
42
|
+
private lazySqs: SQS | undefined;
|
|
43
|
+
private lazyDynamodb: DynamoDBDocument | undefined;
|
|
44
|
+
private lazyDynamodbAdmin: DynamoDB | undefined;
|
|
25
45
|
|
|
26
46
|
constructor(config?: SimpleAWSConfig) {
|
|
27
47
|
this.config = config || new SimpleAWSConfig();
|
|
@@ -35,28 +55,37 @@ export class SimpleAWS {
|
|
|
35
55
|
|
|
36
56
|
get s3() {
|
|
37
57
|
if (this.lazyS3 === undefined) {
|
|
38
|
-
this.lazyS3 = new
|
|
58
|
+
this.lazyS3 = new S3(this.config.get(AWSComponent.s3) || {});
|
|
39
59
|
}
|
|
40
60
|
return this.lazyS3;
|
|
41
61
|
}
|
|
62
|
+
|
|
42
63
|
get sqs() {
|
|
43
64
|
if (this.lazySqs === undefined) {
|
|
44
|
-
this.lazySqs = new
|
|
65
|
+
this.lazySqs = new SQS(this.config.get(AWSComponent.sqs) || {});
|
|
45
66
|
}
|
|
46
67
|
return this.lazySqs;
|
|
47
68
|
}
|
|
69
|
+
|
|
48
70
|
get dynamodb() {
|
|
49
71
|
if (this.lazyDynamodb === undefined) {
|
|
50
|
-
this.lazyDynamodb =
|
|
51
|
-
this.config.get(AWSComponent.dynamodb),
|
|
72
|
+
this.lazyDynamodb = DynamoDBDocument.from(
|
|
73
|
+
new DynamoDBClient(this.config.get(AWSComponent.dynamodb) || {}),
|
|
74
|
+
{
|
|
75
|
+
marshallOptions: {
|
|
76
|
+
convertEmptyValues: true,
|
|
77
|
+
removeUndefinedValues: true,
|
|
78
|
+
},
|
|
79
|
+
},
|
|
52
80
|
);
|
|
53
81
|
}
|
|
54
82
|
return this.lazyDynamodb;
|
|
55
83
|
}
|
|
84
|
+
|
|
56
85
|
get dynamodbAdmin() {
|
|
57
86
|
if (this.lazyDynamodbAdmin === undefined) {
|
|
58
|
-
this.lazyDynamodbAdmin = new
|
|
59
|
-
this.config.get(AWSComponent.dynamodb),
|
|
87
|
+
this.lazyDynamodbAdmin = new DynamoDB(
|
|
88
|
+
this.config.get(AWSComponent.dynamodb) || {},
|
|
60
89
|
);
|
|
61
90
|
}
|
|
62
91
|
return this.lazyDynamodbAdmin;
|
|
@@ -66,11 +95,9 @@ export class SimpleAWS {
|
|
|
66
95
|
if (this.queueUrls[queueName] !== undefined) {
|
|
67
96
|
return this.queueUrls[queueName];
|
|
68
97
|
}
|
|
69
|
-
const urlResult = await this.sqs
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
})
|
|
73
|
-
.promise();
|
|
98
|
+
const urlResult = await this.sqs.getQueueUrl({
|
|
99
|
+
QueueName: queueName,
|
|
100
|
+
});
|
|
74
101
|
logger.stupid(`urlResult`, urlResult);
|
|
75
102
|
if (!urlResult.QueueUrl) {
|
|
76
103
|
throw new Error(`No queue url with name[${queueName}]`);
|
|
@@ -82,26 +109,22 @@ export class SimpleAWS {
|
|
|
82
109
|
logger.debug(`Send message[${data.key}] to queue.`);
|
|
83
110
|
logger.stupid(`data`, data);
|
|
84
111
|
const queueUrl = await this.getQueueUrl(queueName);
|
|
85
|
-
const sendResult = await this.sqs
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
})
|
|
91
|
-
.promise();
|
|
112
|
+
const sendResult = await this.sqs.sendMessage({
|
|
113
|
+
QueueUrl: queueUrl,
|
|
114
|
+
MessageBody: JSON.stringify(data),
|
|
115
|
+
DelaySeconds: 0,
|
|
116
|
+
});
|
|
92
117
|
logger.stupid(`sendResult`, sendResult);
|
|
93
118
|
|
|
94
|
-
const attrResult = await this.sqs
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
})
|
|
99
|
-
.promise();
|
|
119
|
+
const attrResult = await this.sqs.getQueueAttributes({
|
|
120
|
+
QueueUrl: queueUrl,
|
|
121
|
+
AttributeNames: ['ApproximateNumberOfMessages'],
|
|
122
|
+
});
|
|
100
123
|
logger.stupid(`attrResult`, attrResult);
|
|
101
124
|
if (!attrResult.Attributes) {
|
|
102
125
|
return 0;
|
|
103
126
|
}
|
|
104
|
-
return +attrResult.Attributes
|
|
127
|
+
return +(attrResult.Attributes?.ApproximateNumberOfMessages || 0);
|
|
105
128
|
};
|
|
106
129
|
|
|
107
130
|
public dequeue = async <T>(
|
|
@@ -112,14 +135,12 @@ export class SimpleAWS {
|
|
|
112
135
|
): Promise<Array<SQSMessageBody<T>>> => {
|
|
113
136
|
logger.debug(`Receive message from queue[${queueName}].`);
|
|
114
137
|
const queueUrl = await this.getQueueUrl(queueName);
|
|
115
|
-
const receiveResult = await this.sqs
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
})
|
|
122
|
-
.promise();
|
|
138
|
+
const receiveResult = await this.sqs.receiveMessage({
|
|
139
|
+
QueueUrl: queueUrl,
|
|
140
|
+
MaxNumberOfMessages: fetchSize,
|
|
141
|
+
WaitTimeSeconds: waitSeconds,
|
|
142
|
+
VisibilityTimeout: visibilityTimeout,
|
|
143
|
+
});
|
|
123
144
|
logger.stupid(`receiveResult`, receiveResult);
|
|
124
145
|
if (
|
|
125
146
|
receiveResult.Messages === undefined ||
|
|
@@ -172,42 +193,29 @@ export class SimpleAWS {
|
|
|
172
193
|
queueName: string,
|
|
173
194
|
handle: string,
|
|
174
195
|
seconds: number,
|
|
175
|
-
): Promise<string> =>
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
ReceiptHandle: handle,
|
|
184
|
-
VisibilityTimeout: seconds,
|
|
185
|
-
},
|
|
186
|
-
(err, changeResult) => {
|
|
187
|
-
if (err) {
|
|
188
|
-
reject(err);
|
|
189
|
-
} else {
|
|
190
|
-
logger.stupid(`changeResult`, changeResult);
|
|
191
|
-
resolve(handle);
|
|
192
|
-
}
|
|
193
|
-
},
|
|
194
|
-
);
|
|
195
|
-
})
|
|
196
|
-
.catch(reject);
|
|
196
|
+
): Promise<string> => {
|
|
197
|
+
logger.debug(`Change visibilityTimeout of ${handle} to ${seconds}secs.`);
|
|
198
|
+
const queueUrl = await this.getQueueUrl(queueName);
|
|
199
|
+
|
|
200
|
+
await this.sqs.changeMessageVisibility({
|
|
201
|
+
QueueUrl: queueUrl,
|
|
202
|
+
ReceiptHandle: handle,
|
|
203
|
+
VisibilityTimeout: seconds,
|
|
197
204
|
});
|
|
198
205
|
|
|
206
|
+
return handle;
|
|
207
|
+
};
|
|
208
|
+
|
|
199
209
|
public completeMessage = async (
|
|
200
210
|
queueName: string,
|
|
201
211
|
handle: string,
|
|
202
212
|
): Promise<string> => {
|
|
203
213
|
logger.debug(`Complete a message with handle[${handle}]`);
|
|
204
214
|
const queueUrl = await this.getQueueUrl(queueName);
|
|
205
|
-
const deleteResult = await this.sqs
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
})
|
|
210
|
-
.promise();
|
|
215
|
+
const deleteResult = await this.sqs.deleteMessage({
|
|
216
|
+
QueueUrl: queueUrl,
|
|
217
|
+
ReceiptHandle: handle,
|
|
218
|
+
});
|
|
211
219
|
logger.stupid(`deleteResult`, deleteResult);
|
|
212
220
|
return handle;
|
|
213
221
|
};
|
|
@@ -224,15 +232,13 @@ export class SimpleAWS {
|
|
|
224
232
|
const end = Math.min(start + chunkSize, handles.length);
|
|
225
233
|
const sublist = handles.slice(start, end);
|
|
226
234
|
const queueUrl = await this.getQueueUrl(queueName);
|
|
227
|
-
const deletesResult = await this.sqs
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
})
|
|
235
|
-
.promise();
|
|
235
|
+
const deletesResult = await this.sqs.deleteMessageBatch({
|
|
236
|
+
QueueUrl: queueUrl,
|
|
237
|
+
Entries: sublist.map((handle) => ({
|
|
238
|
+
Id: (++index).toString(),
|
|
239
|
+
ReceiptHandle: handle,
|
|
240
|
+
})),
|
|
241
|
+
});
|
|
236
242
|
logger.stupid(`deleteResult`, deletesResult);
|
|
237
243
|
}
|
|
238
244
|
return handles;
|
|
@@ -244,15 +250,14 @@ export class SimpleAWS {
|
|
|
244
250
|
localPath: string,
|
|
245
251
|
): Promise<string> => {
|
|
246
252
|
logger.debug(`Get a stream of item[${key}] from bucket[${bucket}]`);
|
|
247
|
-
const
|
|
248
|
-
|
|
249
|
-
.createReadStream();
|
|
253
|
+
const { Body } = await this.s3.getObject({ Bucket: bucket, Key: key });
|
|
254
|
+
|
|
250
255
|
return new Promise<string>((resolve, reject) =>
|
|
251
|
-
|
|
252
|
-
.on('error', error => reject(error))
|
|
256
|
+
(Body as NodeJS.ReadableStream)
|
|
257
|
+
.on('error', (error) => reject(error))
|
|
253
258
|
.pipe(fs.createWriteStream(localPath))
|
|
254
259
|
.on('finish', () => resolve(localPath))
|
|
255
|
-
.on('error', error => reject(error)),
|
|
260
|
+
.on('error', (error) => reject(error)),
|
|
256
261
|
);
|
|
257
262
|
};
|
|
258
263
|
|
|
@@ -283,17 +288,13 @@ export class SimpleAWS {
|
|
|
283
288
|
key: string,
|
|
284
289
|
): Promise<Buffer> => {
|
|
285
290
|
logger.debug(`Read item[${key}] from bucket[${bucket}]`);
|
|
286
|
-
const
|
|
287
|
-
Bucket: bucket,
|
|
288
|
-
Key: key,
|
|
289
|
-
};
|
|
291
|
+
const { Body } = await this.s3.getObject({ Bucket: bucket, Key: key });
|
|
290
292
|
|
|
291
|
-
const
|
|
292
|
-
if (
|
|
293
|
-
return data.Body as Buffer;
|
|
294
|
-
} else {
|
|
293
|
+
const buffer = await Body?.transformToByteArray();
|
|
294
|
+
if (!buffer) {
|
|
295
295
|
throw new Error(`Failed to read file ${key} from bucket ${bucket}`);
|
|
296
296
|
}
|
|
297
|
+
return Buffer.from(buffer);
|
|
297
298
|
};
|
|
298
299
|
|
|
299
300
|
public upload = async (
|
|
@@ -302,14 +303,18 @@ export class SimpleAWS {
|
|
|
302
303
|
key: string,
|
|
303
304
|
): Promise<string> => {
|
|
304
305
|
logger.debug(`Upload item[${key}] into bucket[${bucket}]`);
|
|
305
|
-
const
|
|
306
|
-
.
|
|
306
|
+
const upload = new Upload({
|
|
307
|
+
client: this.s3,
|
|
308
|
+
params: {
|
|
307
309
|
Bucket: bucket,
|
|
308
310
|
Key: key,
|
|
309
311
|
Body: fs.createReadStream(localPath),
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
|
|
312
|
+
},
|
|
313
|
+
partSize: 5 * 1024 * 1024, // 5MB
|
|
314
|
+
queueSize: 4,
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
await upload.done();
|
|
313
318
|
return key;
|
|
314
319
|
};
|
|
315
320
|
|
|
@@ -319,14 +324,17 @@ export class SimpleAWS {
|
|
|
319
324
|
buffer: Buffer,
|
|
320
325
|
): Promise<string> => {
|
|
321
326
|
logger.debug(`Upload item[${key}] into bucket[${bucket}]`);
|
|
322
|
-
const
|
|
323
|
-
.
|
|
327
|
+
const upload = new Upload({
|
|
328
|
+
client: this.s3,
|
|
329
|
+
params: {
|
|
324
330
|
Bucket: bucket,
|
|
325
331
|
Key: key,
|
|
326
332
|
Body: buffer,
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
|
|
333
|
+
},
|
|
334
|
+
partSize: 5 * 1024 * 1024, // 5MB
|
|
335
|
+
queueSize: 4,
|
|
336
|
+
});
|
|
337
|
+
await upload.done();
|
|
330
338
|
return key;
|
|
331
339
|
};
|
|
332
340
|
|
|
@@ -344,43 +352,161 @@ export class SimpleAWS {
|
|
|
344
352
|
if (!fs.existsSync(tempFile)) {
|
|
345
353
|
return;
|
|
346
354
|
}
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
}
|
|
355
|
+
try {
|
|
356
|
+
await fs.promises.unlink(tempFile);
|
|
357
|
+
} catch (error) {
|
|
351
358
|
const msg = `Error during writeFile: unlink file ${tempFile}: ${stringifyError(
|
|
352
359
|
error,
|
|
353
360
|
)}`;
|
|
354
361
|
logger.error(msg);
|
|
355
|
-
}
|
|
362
|
+
}
|
|
356
363
|
}
|
|
357
364
|
};
|
|
358
365
|
|
|
359
|
-
public getSignedUrl
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
|
|
366
|
+
public async getSignedUrl(options: PresignerOptions): Promise<string> {
|
|
367
|
+
const { expiresIn = 600, unhoistableHeaders } = options;
|
|
368
|
+
switch (options.operation) {
|
|
369
|
+
case 'putObject': {
|
|
370
|
+
const cmd = new PutObjectCommand({
|
|
371
|
+
Bucket: options.bucket,
|
|
372
|
+
Key: options.key,
|
|
373
|
+
...options.params,
|
|
374
|
+
});
|
|
375
|
+
return getSignedUrl(this.s3, cmd, {
|
|
376
|
+
expiresIn: expiresIn,
|
|
377
|
+
unhoistableHeaders,
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
case 'getObject': {
|
|
381
|
+
const cmd = new GetObjectCommand({
|
|
382
|
+
Bucket: options.bucket,
|
|
383
|
+
Key: options.key,
|
|
384
|
+
...options.params,
|
|
385
|
+
});
|
|
386
|
+
return getSignedUrl(this.s3, cmd, {
|
|
387
|
+
expiresIn: expiresIn,
|
|
388
|
+
unhoistableHeaders,
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
case 'deleteObject': {
|
|
392
|
+
const cmd = new DeleteObjectCommand({
|
|
393
|
+
Bucket: options.bucket,
|
|
394
|
+
Key: options.key,
|
|
395
|
+
...options.params,
|
|
396
|
+
});
|
|
397
|
+
return getSignedUrl(this.s3, cmd, {
|
|
398
|
+
expiresIn: expiresIn,
|
|
399
|
+
unhoistableHeaders,
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
case 'headObject': {
|
|
403
|
+
const cmd = new HeadObjectCommand({
|
|
404
|
+
Bucket: options.bucket,
|
|
405
|
+
Key: options.key,
|
|
406
|
+
...options.params,
|
|
407
|
+
});
|
|
408
|
+
return getSignedUrl(this.s3, cmd, {
|
|
409
|
+
expiresIn: expiresIn,
|
|
410
|
+
unhoistableHeaders,
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
case 'copyObject': {
|
|
414
|
+
const cmd = new CopyObjectCommand({
|
|
415
|
+
Bucket: options.bucket,
|
|
416
|
+
Key: options.key,
|
|
417
|
+
...options.params,
|
|
418
|
+
});
|
|
419
|
+
return getSignedUrl(this.s3, cmd, {
|
|
420
|
+
expiresIn: expiresIn,
|
|
421
|
+
unhoistableHeaders,
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
case 'uploadPart': {
|
|
425
|
+
const cmd = new UploadPartCommand({
|
|
426
|
+
Bucket: options.bucket,
|
|
427
|
+
Key: options.key,
|
|
428
|
+
...options.params,
|
|
429
|
+
});
|
|
430
|
+
return getSignedUrl(this.s3, cmd, {
|
|
431
|
+
expiresIn: expiresIn,
|
|
432
|
+
unhoistableHeaders,
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
case 'uploadPartCopy': {
|
|
436
|
+
const cmd = new UploadPartCopyCommand({
|
|
437
|
+
Bucket: options.bucket,
|
|
438
|
+
Key: options.key,
|
|
439
|
+
...options.params,
|
|
440
|
+
});
|
|
441
|
+
return getSignedUrl(this.s3, cmd, {
|
|
442
|
+
expiresIn: expiresIn,
|
|
443
|
+
unhoistableHeaders,
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
case 'listObjectsV2': {
|
|
447
|
+
const cmd = new ListObjectsV2Command({
|
|
448
|
+
Bucket: options.bucket,
|
|
449
|
+
...options.params,
|
|
450
|
+
});
|
|
451
|
+
return getSignedUrl(this.s3, cmd, {
|
|
452
|
+
expiresIn: expiresIn,
|
|
453
|
+
unhoistableHeaders,
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
case 'createMultipartUpload': {
|
|
457
|
+
const cmd = new CreateMultipartUploadCommand({
|
|
458
|
+
Bucket: options.bucket,
|
|
459
|
+
Key: options.key,
|
|
460
|
+
...options.params,
|
|
461
|
+
});
|
|
462
|
+
return getSignedUrl(this.s3, cmd, {
|
|
463
|
+
expiresIn: expiresIn,
|
|
464
|
+
unhoistableHeaders,
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
case 'completeMultipartUpload': {
|
|
468
|
+
const cmd = new CompleteMultipartUploadCommand({
|
|
469
|
+
Bucket: options.bucket,
|
|
470
|
+
Key: options.key,
|
|
471
|
+
...options.params,
|
|
472
|
+
});
|
|
473
|
+
return getSignedUrl(this.s3, cmd, {
|
|
474
|
+
expiresIn: expiresIn,
|
|
475
|
+
unhoistableHeaders,
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
case 'abortMultipartUpload': {
|
|
479
|
+
const cmd = new AbortMultipartUploadCommand({
|
|
480
|
+
Bucket: options.bucket,
|
|
481
|
+
Key: options.key,
|
|
482
|
+
...options.params,
|
|
483
|
+
});
|
|
484
|
+
return getSignedUrl(this.s3, cmd, {
|
|
485
|
+
expiresIn: expiresIn,
|
|
486
|
+
unhoistableHeaders,
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
case 'listParts': {
|
|
490
|
+
const cmd = new ListPartsCommand({
|
|
491
|
+
Bucket: options.bucket,
|
|
492
|
+
Key: options.key,
|
|
493
|
+
...options.params,
|
|
494
|
+
});
|
|
495
|
+
return getSignedUrl(this.s3, cmd, {
|
|
496
|
+
expiresIn: expiresIn,
|
|
497
|
+
unhoistableHeaders,
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
375
502
|
|
|
376
503
|
public getSignedCookie = (
|
|
377
504
|
keyPairId: string,
|
|
378
505
|
privateKey: string,
|
|
379
506
|
url: string,
|
|
380
507
|
expires: number,
|
|
381
|
-
):
|
|
382
|
-
const
|
|
383
|
-
const policy = {
|
|
508
|
+
): CloudfrontSignedCookiesOutput => {
|
|
509
|
+
const policy = JSON.stringify({
|
|
384
510
|
Statement: [
|
|
385
511
|
{
|
|
386
512
|
Resource: url,
|
|
@@ -389,20 +515,12 @@ export class SimpleAWS {
|
|
|
389
515
|
},
|
|
390
516
|
},
|
|
391
517
|
],
|
|
392
|
-
};
|
|
393
|
-
const ret = signer.getSignedCookie({ policy: JSON.stringify(policy) });
|
|
394
|
-
return ret;
|
|
395
|
-
};
|
|
518
|
+
});
|
|
396
519
|
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
params?: S3SignedUrlParams,
|
|
402
|
-
): S3SignedUrlResult => {
|
|
403
|
-
return this.getSignedUrl(bucketName, key, 'getObject', {
|
|
404
|
-
...params,
|
|
405
|
-
ResponseContentDisposition: `attachment; filename="${fileName}"`,
|
|
520
|
+
return getSignedCookies({
|
|
521
|
+
keyPairId,
|
|
522
|
+
privateKey,
|
|
523
|
+
policy,
|
|
406
524
|
});
|
|
407
525
|
};
|
|
408
526
|
|
|
@@ -414,16 +532,14 @@ export class SimpleAWS {
|
|
|
414
532
|
logger.debug(
|
|
415
533
|
`Read an item with key[${JSON.stringify(key)}] from ${tableName}.`,
|
|
416
534
|
);
|
|
417
|
-
const getResult = await this.dynamodb
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
})
|
|
422
|
-
.promise();
|
|
535
|
+
const getResult = await this.dynamodb.get({
|
|
536
|
+
TableName: tableName,
|
|
537
|
+
Key: key,
|
|
538
|
+
});
|
|
423
539
|
logger.stupid(`getResult`, getResult);
|
|
424
540
|
const item: T | undefined =
|
|
425
541
|
getResult !== undefined && getResult.Item !== undefined
|
|
426
|
-
? (
|
|
542
|
+
? (getResult.Item as any as T) // Casts forcefully.
|
|
427
543
|
: defaultValue;
|
|
428
544
|
logger.stupid(`item`, item);
|
|
429
545
|
return item;
|
|
@@ -439,21 +555,19 @@ export class SimpleAWS {
|
|
|
439
555
|
);
|
|
440
556
|
logger.stupid(`keyValues`, columnValues);
|
|
441
557
|
const expressions = Object.keys(columnValues)
|
|
442
|
-
.map(column => `${column} = :${column}`)
|
|
558
|
+
.map((column) => `${column} = :${column}`)
|
|
443
559
|
.join(', ');
|
|
444
560
|
const attributeValues = Object.keys(columnValues)
|
|
445
|
-
.map(column => [`:${column}`, columnValues[column]])
|
|
561
|
+
.map((column) => [`:${column}`, columnValues[column]])
|
|
446
562
|
.reduce((obj, pair) => ({ ...obj, [pair[0]]: pair[1] }), {});
|
|
447
563
|
logger.stupid(`expressions`, expressions);
|
|
448
564
|
logger.stupid(`attributeValues`, attributeValues);
|
|
449
|
-
const updateResult = await this.dynamodb
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
})
|
|
456
|
-
.promise();
|
|
565
|
+
const updateResult = await this.dynamodb.update({
|
|
566
|
+
TableName: tableName,
|
|
567
|
+
Key: key,
|
|
568
|
+
UpdateExpression: `set ${expressions}`,
|
|
569
|
+
ExpressionAttributeValues: attributeValues,
|
|
570
|
+
});
|
|
457
571
|
logger.stupid(`updateResult`, updateResult);
|
|
458
572
|
return updateResult;
|
|
459
573
|
};
|
|
@@ -462,11 +576,9 @@ export class SimpleAWS {
|
|
|
462
576
|
|
|
463
577
|
public setupQueue = async (queueName: string) => {
|
|
464
578
|
try {
|
|
465
|
-
const listResult = await this.sqs
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
})
|
|
469
|
-
.promise();
|
|
579
|
+
const listResult = await this.sqs.listQueues({
|
|
580
|
+
QueueNamePrefix: queueName,
|
|
581
|
+
});
|
|
470
582
|
if (listResult.QueueUrls) {
|
|
471
583
|
for (const queueUrl of listResult.QueueUrls) {
|
|
472
584
|
if (queueUrl.endsWith(queueName)) {
|
|
@@ -479,11 +591,9 @@ export class SimpleAWS {
|
|
|
479
591
|
logger.debug(`No Queue[${queueName}] exists due to ${error}`);
|
|
480
592
|
}
|
|
481
593
|
logger.debug(`Create a queue[${queueName}] newly.`);
|
|
482
|
-
const createResult = await this.sqs
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
})
|
|
486
|
-
.promise();
|
|
594
|
+
const createResult = await this.sqs.createQueue({
|
|
595
|
+
QueueName: queueName,
|
|
596
|
+
});
|
|
487
597
|
logger.stupid(`createResult`, createResult);
|
|
488
598
|
return true;
|
|
489
599
|
};
|
|
@@ -496,10 +606,10 @@ export class SimpleAWS {
|
|
|
496
606
|
},
|
|
497
607
|
) => {
|
|
498
608
|
try {
|
|
499
|
-
const listResult = await this.s3.listBuckets()
|
|
609
|
+
const listResult = await this.s3.listBuckets();
|
|
500
610
|
if (
|
|
501
611
|
listResult.Buckets &&
|
|
502
|
-
listResult.Buckets.map(each => each.Name).includes(bucketName)
|
|
612
|
+
listResult.Buckets.map((each) => each.Name).includes(bucketName)
|
|
503
613
|
) {
|
|
504
614
|
logger.debug(`Bucket[${bucketName}] already exists.`);
|
|
505
615
|
return true;
|
|
@@ -508,27 +618,23 @@ export class SimpleAWS {
|
|
|
508
618
|
logger.debug(`No bucket[${bucketName}] exists due to ${error}`);
|
|
509
619
|
}
|
|
510
620
|
logger.debug(`Create a bucket[${bucketName}] newly.`);
|
|
511
|
-
const createResult = await this.s3
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
})
|
|
515
|
-
.promise();
|
|
621
|
+
const createResult = await this.s3.createBucket({
|
|
622
|
+
Bucket: bucketName,
|
|
623
|
+
});
|
|
516
624
|
logger.stupid(`createResult`, createResult);
|
|
517
625
|
if (cors) {
|
|
518
|
-
const corsResult = await this.s3
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
})
|
|
531
|
-
.promise();
|
|
626
|
+
const corsResult = await this.s3.putBucketCors({
|
|
627
|
+
Bucket: bucketName,
|
|
628
|
+
CORSConfiguration: {
|
|
629
|
+
CORSRules: [
|
|
630
|
+
{
|
|
631
|
+
AllowedHeaders: ['*'],
|
|
632
|
+
AllowedMethods: cors.methods,
|
|
633
|
+
AllowedOrigins: cors.origins,
|
|
634
|
+
},
|
|
635
|
+
],
|
|
636
|
+
},
|
|
637
|
+
});
|
|
532
638
|
logger.stupid(`corsResult`, corsResult);
|
|
533
639
|
}
|
|
534
640
|
return true;
|
|
@@ -536,7 +642,7 @@ export class SimpleAWS {
|
|
|
536
642
|
|
|
537
643
|
public setupDynamoDb = async (tableName: string, keyColumn: string) => {
|
|
538
644
|
try {
|
|
539
|
-
const listResult = await this.dynamodbAdmin.listTables()
|
|
645
|
+
const listResult = await this.dynamodbAdmin.listTables();
|
|
540
646
|
if (listResult.TableNames && listResult.TableNames.includes(tableName)) {
|
|
541
647
|
logger.debug(`Table[${tableName}] already exists.`);
|
|
542
648
|
return true;
|
|
@@ -545,19 +651,15 @@ export class SimpleAWS {
|
|
|
545
651
|
logger.debug(`No table[${tableName}] exists due to ${error}`);
|
|
546
652
|
}
|
|
547
653
|
logger.debug(`Create a table[${tableName}] newly.`);
|
|
548
|
-
const createResult = await this.dynamodbAdmin
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
WriteCapacityUnits: 10,
|
|
558
|
-
},
|
|
559
|
-
})
|
|
560
|
-
.promise();
|
|
654
|
+
const createResult = await this.dynamodbAdmin.createTable({
|
|
655
|
+
TableName: tableName,
|
|
656
|
+
KeySchema: [{ AttributeName: keyColumn, KeyType: 'HASH' }],
|
|
657
|
+
AttributeDefinitions: [{ AttributeName: keyColumn, AttributeType: 'S' }],
|
|
658
|
+
ProvisionedThroughput: {
|
|
659
|
+
ReadCapacityUnits: 30,
|
|
660
|
+
WriteCapacityUnits: 10,
|
|
661
|
+
},
|
|
662
|
+
});
|
|
561
663
|
logger.stupid(`createResult`, createResult);
|
|
562
664
|
return true;
|
|
563
665
|
};
|