-
Notifications
You must be signed in to change notification settings - Fork 109
/
bin.ts
434 lines (378 loc) · 15.5 KB
/
bin.ts
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/usr/bin/env node
import '@babel/polyfill';
import 'fs-posix';
import S3, { NextToken, ObjectList, RoutingRules } from 'aws-sdk/clients/s3';
import yargs from 'yargs';
import { CACHE_FILES, GatsbyRedirect, Params, S3PluginOptions } from './constants';
import { readJson } from 'fs-extra';
import klaw from 'klaw';
import PrettyError from 'pretty-error';
import streamToPromise from 'stream-to-promise';
import ora from 'ora';
import chalk from 'chalk';
import { Readable } from 'stream';
import { join, relative, resolve, sep } from 'path';
import { resolve as resolveUrl } from 'url';
import fs from 'fs';
import util from 'util';
import minimatch from 'minimatch';
import mime from 'mime';
import inquirer from 'inquirer';
import { config as awsConfig } from 'aws-sdk';
import { createHash } from 'crypto';
import isCI from 'is-ci';
import { getS3WebsiteDomainUrl, withoutLeadingSlash } from './util';
import { AsyncFunction, asyncify, parallelLimit } from 'async';
import proxy from 'proxy-agent';
const pe = new PrettyError();
const OBJECTS_TO_REMOVE_PER_REQUEST = 1000;
const promisifiedParallelLimit: <T, E = Error>(
tasks: Array<AsyncFunction<T, E>>,
limit: number
) => // Have to cast this due to https://github.com/DefinitelyTyped/DefinitelyTyped/issues/20497
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Promise<T[]> = util.promisify(parallelLimit) as any;
const guessRegion = (s3: S3, constraint?: string): string | undefined =>
constraint ?? s3.config.region ?? awsConfig.region;
const getBucketInfo = async (config: S3PluginOptions, s3: S3): Promise<{ exists: boolean; region?: string }> => {
try {
const { $response } = await s3.getBucketLocation({ Bucket: config.bucketName }).promise();
const responseData = $response.data as S3.GetBucketLocationOutput | null; // Fix type to be possibly `null` instead of possibly `void`
const detectedRegion = guessRegion(s3, responseData?.LocationConstraint);
return {
exists: true,
region: detectedRegion,
};
} catch (ex) {
if (ex.code === 'NoSuchBucket') {
return {
exists: false,
region: guessRegion(s3),
};
}
throw ex;
}
};
const getParams = (path: string, params: Params): Partial<S3.Types.PutObjectRequest> => {
let returned = {};
for (const key of Object.keys(params)) {
if (minimatch(path, key)) {
returned = {
...returned,
...params[key],
};
}
}
return returned;
};
const listAllObjects = async (s3: S3, bucketName: string, bucketPrefix: string | undefined): Promise<ObjectList> => {
const list: ObjectList = [];
let token: NextToken | undefined;
do {
const response = await s3
.listObjectsV2({
Bucket: bucketName,
ContinuationToken: token,
Prefix: bucketPrefix,
})
.promise();
if (response.Contents) {
list.push(...response.Contents);
}
token = response.NextContinuationToken;
} while (token);
return list;
};
const createSafeS3Key = (key: string): string => {
if (sep === '\\') {
return key.replace(/\\/g, '/');
}
return key;
};
export interface DeployArguments {
yes?: boolean;
bucket?: string;
userAgent?: string;
}
export const deploy = async ({ yes, bucket, userAgent }: DeployArguments = {}) => {
const spinner = ora({ text: 'Retrieving bucket info...', color: 'magenta', stream: process.stdout }).start();
let dontPrompt = yes;
const uploadQueue: Array<AsyncFunction<void, Error>> = [];
try {
const config: S3PluginOptions = await readJson(CACHE_FILES.config);
const params: Params = await readJson(CACHE_FILES.params);
const routingRules: RoutingRules = await readJson(CACHE_FILES.routingRules);
const redirectObjects: GatsbyRedirect[] = fs.existsSync(CACHE_FILES.redirectObjects)
? await readJson(CACHE_FILES.redirectObjects)
: [];
// Override the bucket name if it is set via command line
if (bucket) {
config.bucketName = bucket;
}
let httpOptions = {};
if (process.env.HTTP_PROXY) {
httpOptions = {
agent: proxy(process.env.HTTP_PROXY),
};
}
httpOptions = {
agent: process.env.HTTP_PROXY ? proxy(process.env.HTTP_PROXY) : undefined,
timeout: config.timeout,
connectTimeout: config.connectTimeout,
...httpOptions,
};
const s3 = new S3({
region: config.region,
endpoint: config.customAwsEndpointHostname,
customUserAgent: userAgent ?? '',
httpOptions,
logger: config.verbose ? console : undefined,
retryDelayOptions: {
customBackoff: process.env.fixedRetryDelay ? () => Number(config.fixedRetryDelay) : undefined,
},
});
const { exists, region } = await getBucketInfo(config, s3);
if (isCI && !dontPrompt) {
dontPrompt = true;
}
if (!dontPrompt) {
spinner.stop();
console.log(chalk`
{underline Please review the following:} ({dim pass -y next time to skip this})
Deploying to bucket: {cyan.bold ${config.bucketName}}
In region: {yellow.bold ${region ?? 'UNKNOWN!'}}
Gatsby will: ${
!exists
? chalk`{bold.greenBright CREATE}`
: chalk`{bold.blueBright UPDATE} {dim (any existing website configuration will be overwritten!)}`
}
`);
const { confirm } = await inquirer.prompt([
{
message: 'OK?',
name: 'confirm',
type: 'confirm',
},
]);
if (!confirm) {
throw new Error('User aborted!');
}
spinner.start();
}
spinner.text = 'Configuring bucket...';
spinner.color = 'yellow';
if (!exists) {
const createParams: S3.Types.CreateBucketRequest = {
Bucket: config.bucketName,
ACL: config.acl === null ? undefined : config.acl ?? 'public-read',
};
if (config.region) {
createParams.CreateBucketConfiguration = {
LocationConstraint: config.region,
};
}
await s3.createBucket(createParams).promise();
if (config.enableS3StaticWebsiteHosting) {
const publicBlockConfig: S3.Types.DeletePublicAccessBlockRequest = {
Bucket: config.bucketName,
};
await s3.deletePublicAccessBlock(publicBlockConfig).promise();
}
}
if (config.enableS3StaticWebsiteHosting) {
const websiteConfig: S3.Types.PutBucketWebsiteRequest = {
Bucket: config.bucketName,
WebsiteConfiguration: {
IndexDocument: {
Suffix: 'index.html',
},
ErrorDocument: {
Key: '404.html',
},
},
};
if (routingRules.length) {
websiteConfig.WebsiteConfiguration.RoutingRules = routingRules;
}
await s3.putBucketWebsite(websiteConfig).promise();
}
spinner.text = 'Listing objects...';
spinner.color = 'green';
const objects = await listAllObjects(s3, config.bucketName, config.bucketPrefix);
const keyToETagMap = objects.reduce((acc: { [key: string]: string }, curr) => {
if (curr.Key && curr.ETag) {
acc[curr.Key] = curr.ETag;
}
return acc;
}, {});
spinner.color = 'cyan';
spinner.text = 'Syncing...';
const publicDir = resolve('./public');
const stream = klaw(publicDir);
const isKeyInUse: { [objectKey: string]: boolean } = {};
stream.on('data', ({ path, stats }) => {
if (!stats.isFile()) {
return;
}
uploadQueue.push(
asyncify(async () => {
let key = createSafeS3Key(relative(publicDir, path));
if (config.bucketPrefix) {
key = `${config.bucketPrefix}/${key}`;
}
const readStream = fs.createReadStream(path);
const hashStream = readStream.pipe(createHash('md5').setEncoding('hex'));
const data = await streamToPromise(hashStream);
const tag = `"${data}"`;
const objectUnchanged = keyToETagMap[key] === tag;
isKeyInUse[key] = true;
if (!objectUnchanged) {
try {
const upload = new S3.ManagedUpload({
service: s3,
params: {
Bucket: config.bucketName,
Key: key,
Body: fs.createReadStream(path),
ACL: config.acl === null ? undefined : config.acl ?? 'public-read',
ContentType: mime.getType(path) ?? 'application/octet-stream',
...getParams(key, params),
},
});
upload.on('httpUploadProgress', evt => {
spinner.text = chalk`Syncing...
{dim Uploading {cyan ${key}} ${evt.loaded.toString()}/${evt.total.toString()}}`;
});
await upload.promise();
spinner.text = chalk`Syncing...\n{dim Uploaded {cyan ${key}}}`;
} catch (ex) {
console.error(ex);
process.exit(1);
}
}
})
);
});
const base = config.protocol && config.hostname ? `${config.protocol}://${config.hostname}` : null;
redirectObjects.forEach(redirect =>
uploadQueue.push(
asyncify(async () => {
const { fromPath, toPath: redirectPath } = redirect;
const redirectLocation = base ? resolveUrl(base, redirectPath) : redirectPath;
let key = withoutLeadingSlash(fromPath);
if (key.endsWith('/')) {
key = join(key, 'index.html');
}
key = createSafeS3Key(key);
if (config.bucketPrefix) {
key = withoutLeadingSlash(`${config.bucketPrefix}/${key}`);
}
const tag = `"${createHash('md5')
.update(redirectLocation)
.digest('hex')}"`;
const objectUnchanged = keyToETagMap[key] === tag;
isKeyInUse[key] = true;
if (objectUnchanged) {
// object with exact hash already exists, abort.
return;
}
try {
const upload = new S3.ManagedUpload({
service: s3,
params: {
Bucket: config.bucketName,
Key: key,
Body: redirectLocation,
ACL: config.acl === null ? undefined : config.acl ?? 'public-read',
ContentType: 'application/octet-stream',
WebsiteRedirectLocation: redirectLocation,
...getParams(key, params),
},
});
await upload.promise();
spinner.text = chalk`Syncing...
{dim Created Redirect {cyan ${key}} => {cyan ${redirectLocation}}}\n`;
} catch (ex) {
spinner.fail(chalk`Upload failure for object {cyan ${key}}`);
console.error(pe.render(ex));
process.exit(1);
}
})
)
);
await streamToPromise(stream as Readable);
await promisifiedParallelLimit(uploadQueue, config.parallelLimit as number);
if (config.removeNonexistentObjects) {
const objectsToRemove = objects
.map(obj => ({ Key: obj.Key as string }))
.filter(obj => {
if (!obj.Key || isKeyInUse[obj.Key]) return false;
for (const glob of config.retainObjectsPatterns ?? []) {
if (minimatch(obj.Key, glob)) {
return false;
}
}
return true;
});
for (let i = 0; i < objectsToRemove.length; i += OBJECTS_TO_REMOVE_PER_REQUEST) {
const objectsToRemoveInThisRequest = objectsToRemove.slice(i, i + OBJECTS_TO_REMOVE_PER_REQUEST);
spinner.text = `Removing objects ${i + 1} to ${i + objectsToRemoveInThisRequest.length} of ${
objectsToRemove.length
}`;
await s3
.deleteObjects({
Bucket: config.bucketName,
Delete: {
Objects: objectsToRemoveInThisRequest,
Quiet: true,
},
})
.promise();
}
}
spinner.succeed('Synced.');
if (config.enableS3StaticWebsiteHosting) {
const s3WebsiteDomain = getS3WebsiteDomainUrl(region ?? 'us-east-1');
console.log(chalk`
{bold Your website is online at:}
{blue.underline http://${config.bucketName}.${s3WebsiteDomain}}
`);
} else {
console.log(chalk`
{bold Your website has now been published to:}
{blue.underline ${config.bucketName}}
`);
}
} catch (ex) {
spinner.fail('Failed.');
console.error(pe.render(ex));
process.exit(1);
}
};
yargs
.command(
['deploy', '$0'],
"Deploy bucket. If it doesn't exist, it will be created. Otherwise, it will be updated.",
args =>
args
.option('yes', {
alias: 'y',
describe: 'Skip confirmation prompt',
boolean: true,
})
.option('bucket', {
alias: 'b',
describe: 'Bucket name (if you wish to override default bucket name)',
})
.option('userAgent', {
describe: 'Allow appending custom text to the User Agent string (Used in automated tests)',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}) as any,
deploy as (args: { yes: boolean; bucket: string; userAgent: string }) => void
)
.wrap(yargs.terminalWidth())
.demandCommand(1, `Pass --help to see all available commands and options.`)
.strict()
.showHelpOnFail(true)
.recommendCommands()
.parse(process.argv.slice(2));