-
Notifications
You must be signed in to change notification settings - Fork 43
/
config.js
executable file
·891 lines (862 loc) · 22.5 KB
/
config.js
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
const chokidar = require('chokidar')
const convict = require('convict')
const domainManager = require('./dadi/lib/models/domain-manager')
const fs = require('fs')
const logger = require('@dadi/logger')
const objectPath = require('object-path')
const path = require('path')
// Define a schema
const schema = {
server: {
host: {
doc: 'The IP address the application will run on',
format: 'ipaddress',
default: '0.0.0.0'
},
port: {
doc: 'The port number the application will bind to',
format: 'port',
default: 8080,
env: 'PORT'
},
redirectPort: {
doc: 'Port to redirect http connections to https from',
format: 'port',
default: 0,
env: 'REDIRECT_PORT'
},
name: {
doc: 'Server name',
format: String,
default: 'DADI CDN'
},
protocol: {
doc: 'The protocol the web application will use',
format: String,
default: 'http',
env: 'PROTOCOL'
},
sslPassphrase: {
doc: 'The passphrase of the SSL private key',
format: String,
default: '',
env: 'SSL_PRIVATE_KEY_PASSPHRASE'
},
sslPrivateKeyPath: {
doc: 'The filename of the SSL private key',
format: String,
default: '',
env: 'SSL_PRIVATE_KEY_PATH'
},
sslCertificatePath: {
doc: 'The filename of the SSL certificate',
format: String,
default: '',
env: 'SSL_CERTIFICATE_PATH'
},
sslIntermediateCertificatePath: {
doc: 'The filename of an SSL intermediate certificate, if any',
format: String,
default: '',
env: 'SSL_INTERMEDIATE_CERTIFICATE_PATH'
},
sslIntermediateCertificatePaths: {
doc:
'The filenames of SSL intermediate certificates, overrides sslIntermediateCertificate (singular)',
format: Array,
default: [],
env: 'SSL_INTERMEDIATE_CERTIFICATE_PATHS'
},
enableHTTP2: {
doc:
'When `server.protocol = https` use http2 as the primary response method. Fallback to http1 for unsupported clients.',
format: Boolean,
default: true
}
},
publicUrl: {
host: {
doc:
'The host of the URL where the CDN instance can be publicly accessed at',
format: '*',
default: null,
env: 'URL_HOST'
},
port: {
doc:
'The port of the URL where the CDN instance can be publicly accessed at',
format: '*',
default: 80,
env: 'URL_PORT'
},
protocol: {
doc:
'The protocol of the URL where the CDN instance can be publicly accessed at',
format: 'String',
default: 'http',
env: 'URL_PROTOCOL'
}
},
logging: {
enabled: {
doc: 'If true, logging is enabled using the following settings.',
format: Boolean,
default: false
},
level: {
doc: 'Sets the logging level.',
format: ['debug', 'info', 'warn', 'error', 'trace'],
default: 'info'
},
path: {
doc: 'The absolute or relative path to the directory for log files.',
format: String,
default: './log'
},
filename: {
doc: 'The name to use for the log file, without extension.',
format: String,
default: 'cdn'
},
extension: {
doc: 'The extension to use for the log file.',
format: String,
default: 'log'
},
accessLog: {
enabled: {
doc:
"If true, HTTP access logging is enabled. The log file name is similar to the setting used for normal logging, with the addition of 'access'. For example `cdn.access.log`.",
format: Boolean,
default: true
}
},
aws: {
accessKeyId: {
doc: 'Access key ID for AWS logging',
format: String,
default: '',
env: 'AWS_ACCESS_KEY'
},
secretAccessKey: {
doc: 'Secret access key for AWS logging',
format: String,
default: '',
env: 'AWS_SECRET_KEY'
},
region: {
doc: 'Region for AWS logging',
format: String,
default: '',
env: 'AWS_REGION'
}
}
},
notFound: {
statusCode: {
doc: 'If set, overrides the status code in the case of a 404',
format: Number,
default: 404,
allowDomainOverride: true
},
images: {
enabled: {
doc: 'If true, returns a default image when request returns a 404',
format: Boolean,
default: false,
allowDomainOverride: true
},
path: {
doc: 'The path to the default image',
format: String,
default: './images/missing.png',
allowDomainOverride: true
}
}
},
images: {
directory: {
enabled: {
doc: 'If true, image files will be loaded from the filesystem',
format: Boolean,
default: false,
allowDomainOverride: true
},
path: {
doc: 'The path to the image directory',
format: String,
default: './images',
allowDomainOverride: true
}
},
s3: {
enabled: {
doc:
'If true, image files may be requested from Amazon S3 Buckets or Digital Ocean Spaces',
format: Boolean,
default: false
},
accessKey: {
doc:
'The access key used to connect to Amazon or Digital Ocean services for image files',
format: String,
default: '',
env: 'AWS_S3_IMAGES_ACCESS_KEY'
},
secretKey: {
doc:
'The secret used to connect to Amazon or Digital Ocean services for image files',
format: String,
default: '',
env: 'AWS_S3_IMAGES_SECRET_KEY'
},
bucketName: {
doc:
'The Amazon S3 Bucket or Digital Ocean Space that contains the image files',
format: String,
default: '',
env: 'AWS_S3_IMAGES_BUCKET_NAME'
},
region: {
doc:
'The Amazon S3 or Digital Ocean region the Bucket/Space is served from',
format: String,
default: '',
env: 'AWS_S3_IMAGES_REGION'
},
endpoint: {
doc:
'The endpoint used to access Digital Ocean Spaces. Not required for Amazon S3.',
format: String,
default: '',
env: 'AWS_S3_IMAGES_ENDPOINT'
}
},
remote: {
enabled: {
doc: 'If true, image files will be requested from a remote host',
format: Boolean,
default: false,
allowDomainOverride: true
},
path: {
doc:
'The remote host to request images from, for example http://media.example.com',
format: String,
default: '',
allowDomainOverride: true
},
allowFullURL: {
doc: 'If true, images can be loaded from any remote URL',
format: Boolean,
default: true,
allowDomainOverride: true
}
}
},
assets: {
directory: {
enabled: {
doc: 'If true, asset files will be loaded from the filesystem',
format: Boolean,
default: false,
allowDomainOverride: true
},
path: {
doc: 'The path to the assets directory',
format: String,
default: './public',
allowDomainOverride: true
}
},
s3: {
enabled: {
doc:
'If true, asset files may be requested from Amazon S3 Buckets or Digital Ocean Spaces',
format: Boolean,
default: false
},
accessKey: {
doc:
'The access key used to connect to Amazon or Digital Ocean services for asset files',
format: String,
default: '',
env: 'AWS_S3_ASSETS_ACCESS_KEY'
},
secretKey: {
doc:
'The secret used to connect to Amazon or Digital Ocean services for asset files',
format: String,
default: '',
env: 'AWS_S3_ASSETS_SECRET_KEY'
},
bucketName: {
doc:
'The Amazon S3 Bucket or Digital Ocean Space that contains the asset files',
format: String,
default: '',
env: 'AWS_S3_ASSETS_BUCKET_NAME'
},
region: {
doc:
'The Amazon S3 or Digital Ocean region the Bucket/Space is served from',
format: String,
default: '',
env: 'AWS_S3_ASSETS_REGION'
},
endpoint: {
doc:
'The endpoint used to access Digital Ocean Spaces. Not required for Amazon S3.',
format: String,
default: '',
env: 'AWS_S3_ASSETS_ENDPOINT'
}
},
remote: {
enabled: {
doc: 'If true, asset files will be requested from a remote host',
format: Boolean,
default: false,
allowDomainOverride: true
},
path: {
doc:
'The remote host to request assets from, for example http://media.example.com',
format: String,
default: '',
allowDomainOverride: true
},
allowFullURL: {
doc: 'If true, assets can be loaded from any remote URL',
format: Boolean,
default: true,
allowDomainOverride: true
}
}
},
caching: {
expireAt: {
doc: 'Cron-style pattern specifying when the cache should be expired',
format: String,
default: null,
allowDomainOverride: true
},
ttl: {
doc: 'Amount of time, in seconds, after which cached items should expire',
format: Number,
default: 3600,
allowDomainOverride: true
},
cache404: {
doc: 'Whether to cache responses for requests that returned 404',
format: Boolean,
default: true,
allowDomainOverride: true
},
directory: {
enabled: {
doc: 'If true, cache files will be saved to the filesystem',
format: Boolean,
default: true,
env: 'CACHE_ENABLE_DIRECTORY'
},
path: {
doc: 'The relative path to the cache directory',
format: String,
default: './cache/'
}
},
redis: {
enabled: {
doc: 'If true, cache files will be saved to the specified Redis server',
format: Boolean,
default: false,
env: 'CACHE_ENABLE_REDIS'
},
host: {
doc: 'The Redis server host',
format: String,
default: '',
env: 'REDIS_HOST'
},
port: {
doc: 'The port for the Redis server',
format: 'port',
default: 6379,
env: 'REDIS_PORT'
},
password: {
doc: '',
format: String,
default: '',
env: 'REDIS_PASSWORD'
}
}
},
status: {
enabled: {
doc: 'If true, status endpoint is enabled.',
format: Boolean,
default: true
},
requireAuthentication: {
doc: 'If true, status endpoint requires authentication.',
format: Boolean,
default: true
},
standalone: {
doc: 'If true, status endpoint will run on an standalone address/port.',
format: Boolean,
default: false
},
port: {
doc:
'Accept connections on the specified port. A value of zero will assign a random port.',
format: Number,
default: 8003,
env: 'STATUS_PORT'
},
routes: {
doc:
'An array of routes to test. Each route object must contain properties `route` and `expectedResponseTime`. Note, `expectedResponseTime` is seconds.',
format: Array,
default: [
{
route: '/test.jpg?format=png&quality=50&width=800&height=600',
expectedResponseTime: 0.025
}
]
}
},
security: {
maxWidth: {
doc: 'The maximum width, in pixels, for an output image',
format: Number,
default: 2048
},
maxHeight: {
doc: 'The maximum height, in pixels, for an output image',
format: Number,
default: 1024
}
},
auth: {
tokenUrl: {
doc: 'Endpoint for requesting bearer tokens',
format: String,
default: '/token'
},
clientId: {
doc: 'Client ID used to access protected endpoints',
format: String,
default: null,
env: 'AUTH_TOKEN_ID',
allowDomainOverride: true
},
secret: {
doc: 'Client secret used to access protected endpoints',
format: String,
default: null,
env: 'AUTH_TOKEN_SECRET',
allowDomainOverride: true
},
tokenTtl: {
doc: 'Lifetime of bearer tokens (in seconds)',
format: Number,
default: 1800,
env: 'AUTH_TOKEN_TTL',
allowDomainOverride: true
},
privateKey: {
doc: 'Private key for signing JSON Web Tokens',
format: String,
env: 'AUTH_KEY',
default: null,
allowDomainOverride: true
}
},
cloudfront: {
enabled: {
doc: 'Enable Amazon CloudFront',
format: Boolean,
default: false
},
accessKey: {
doc: 'CloudFront access key',
format: String,
default: '',
env: 'CLOUDFRONT_ACCESS_KEY'
},
secretKey: {
doc: 'CloudFront secret key',
format: String,
default: '',
env: 'CLOUDFRONT_SECRET_KEY'
},
distribution: {
doc: 'Name of the CloudFront distribution to use',
format: String,
default: '',
env: 'CLOUDFRONT_DISTRIBUTION'
}
},
cluster: {
doc:
'If true, CDN runs in cluster mode, starting a worker for each CPU core',
format: Boolean,
default: true
},
paths: {
plugins: {
doc: 'Path to plugins directory',
format: String,
default: 'workspace/plugins',
allowDomainOverride: true
},
recipes: {
doc: 'Path to recipes directory',
format: String,
default: 'workspace/recipes',
allowDomainOverride: true
},
routes: {
doc: 'Path to routes directory',
format: String,
default: 'workspace/routes',
allowDomainOverride: true
}
},
headers: {
useGzipCompression: {
doc:
"If true, uses gzip compression and adds a 'Content-Encoding:gzip' header to the response.",
format: Boolean,
default: true,
allowDomainOverride: true
},
cacheControl: {
doc:
'A set of cache control headers based on specified mimetypes or paths',
format: Object,
default: {
default: 'public, max-age=3600',
paths: [],
mimetypes: [
{'text/css': 'public, max-age=86400'},
{'text/javascript': 'public, max-age=86400'},
{'application/javascript': 'public, max-age=86400'}
]
},
allowDomainOverride: true
}
},
robots: {
doc: 'The path to a robots.txt file',
format: String,
default: ''
},
env: {
doc: 'The applicaton environment.',
format: String,
default: 'development',
env: 'NODE_ENV',
arg: 'node_env'
},
geolocation: {
enabled: {
doc: 'Enable geolocation',
format: Boolean,
default: false
},
method: {
doc: 'Method to use for geolocation',
format: ['maxmind', 'remote'],
default: 'maxmind'
},
maxmind: {
countryDbPath: {
doc: 'Path to Maxmind country database',
format: String,
default: __dirname + '/vendor/maxmind-country.mmdb'
}
},
remote: {
url: {
doc:
'Remote URL to be used for geolocation. {key}, {secret} and {ip} will be replaced by the API key, secret and IP to locate, respectively',
format: String,
default: ''
},
key: {
doc: 'Key to be used with remote geolocation service',
format: String,
default: '',
env: 'GEOLOCATION_REMOTE_KEY'
},
secret: {
doc: 'Secret to be used with remote geolocation service',
format: String,
default: '',
env: 'GEOLOCATION_REMOTE_SECRET'
},
countryPath: {
doc: 'Path to the country code within the response object',
format: String,
default: 'location.country.isoCode'
}
}
},
network: {
url: {
doc:
'Remote URL to be used for network test service. {key}, {secret} and {ip} will be replaced by the API key, secret and IP to locate, respectively',
format: String,
default: ''
},
key: {
doc: 'Key to be used with network test service',
format: String,
default: '',
env: 'NETWORK_REMOTE_KEY'
},
secret: {
doc: 'Secret to be used with network test service',
format: String,
default: '',
env: 'NETWORK_REMOTE_SECRET'
},
path: {
doc: 'Path to the network type within the response object',
format: String,
default: 'speed.connectionType'
}
},
engines: {
sharp: {
kernel: {
doc: 'The kernel to use for image reduction',
format: ['nearest', 'cubic', 'lanczos2', 'lanczos3'],
default: 'lanczos3'
},
interpolator: {
doc: 'The interpolator to use for image enlargement',
format: [
'nearest',
'bilinear',
'vertexSplitQuadraticBasisSpline',
'bicubic',
'locallyBoundedBicubic',
'nohalo'
],
default: 'bicubic'
},
centreSampling: {
doc:
'Whether to use *magick centre sampling convention instead of corner sampling',
format: Boolean,
default: false
}
}
},
experimental: {
jsTranspiling: {
doc:
'Whether to enable experimental support for on-demand JavaScript transpiling',
format: Boolean,
default: false,
env: 'JSTRANSPILING',
allowDomainOverride: true
}
},
dadiNetwork: {
enableConfigurationAPI: {
doc: 'Whether to enable domain configuration endpoints',
format: Boolean,
default: false
}
},
multiDomain: {
directory: {
doc: 'Path to domains directory',
format: 'String',
default: 'domains'
},
enabled: {
doc: 'Enable multi-domain configuration for this CDN instance',
format: Boolean,
default: false
}
},
http: {
followRedirects: {
doc:
'The number of redirects to follow when retrieving assets via HTTP requests',
format: Number,
default: 10,
allowDomainOverride: true
}
},
defaultFiles: {
doc:
'An array of filenames that can be used as fallback defaults when no path is specified',
format: Array,
default: []
}
}
const Config = function() {
this.loadFile(this.configPath())
this.watcher = chokidar
.watch(this.configPath(), {usePolling: true})
.on('all', (event, filePath) => {
this.loadFile(this.configPath())
})
this.domainSchema = {}
this.createDomainSchema(schema, this.domainSchema)
const domainsDirectory = this.get('multiDomain.directory')
// Watch the domains directory for new & removed domain configurations.
this.domainsWatcher = chokidar
.watch(domainsDirectory, {
awaitWriteFinish: true,
depth: 1,
usePolling: true
})
.on('addDir', (event, filePath) => {
this.loadDomainConfigs()
})
.on('unlinkDir', (event, filePath) => {
// Wait 3 sec for the delete to finish before rescanning
setTimeout(() => {
this.loadDomainConfigs()
}, 3000)
})
}
Config.prototype = convict(schema)
/**
* Retrieves the full path for the configuration file associated
* with the current environment,
*
* @return {String}
*/
Config.prototype.configPath = function() {
const environment = this.get('env')
return `./config/config.${environment}.json`
}
/**
* Creates a Convict schema for domains, including only the properties
* that can be overridden at domain level, as well as the default values
* obtained from the main config.
*
* @param {Object} schema - main schema
* @param {Object} target - variable to write the schema to
* @param {Array} tail - helper variable for recursion
*/
Config.prototype.createDomainSchema = function(schema, target, tail = []) {
if (!schema || typeof schema !== 'object') return
if (schema.allowDomainOverride) {
const path = tail.join('.')
objectPath.set(
target,
path,
Object.assign({}, schema, {
default: this.get(path)
})
)
return
}
Object.keys(schema).forEach(key => {
this.createDomainSchema(schema[key], target, tail.concat(key))
})
}
/**
* A reference to the original `get` method from convict.
*
* @type {Function}
*/
Config.prototype._get = Config.prototype.get
/**
* Gets a configuration value for a domain if the property can
* be defined at domain level *and* a domain name is supplied.
* Otherwise, behaves as the native `get` method from Convict.
*
* @param {String} path - config property
* @param {String} domain - domain name
* @return {Object}
*/
Config.prototype.get = function(path, domain) {
if (
domain === undefined ||
this.domainConfigs[domain] === undefined ||
!objectPath.get(schema, `${path}.allowDomainOverride`)
) {
return this._get(path)
}
return this.domainConfigs[domain].get(path)
}
Config.prototype.loadDomainConfig = function(domain, domainConfig) {
this.domainConfigs[domain] = convict(this.domainSchema)
this.domainConfigs[domain].load(domainConfig)
}
/**
* Builds a hash map with a Convict instance for each configured
* domain.
*
* @return {Object}
*/
Config.prototype.loadDomainConfigs = function() {
if (!this.get('multiDomain.enabled')) {
return {}
}
const domainsDirectory = this.get('multiDomain.directory')
const configs = {}
domainManager
.scanDomains(domainsDirectory)
.getDomains()
.forEach(({domain, path: domainPath}) => {
const configPath = path.join(
domainPath,
`config/config.${this.get('env')}.json`
)
try {
const file = fs.statSync(configPath)
if (file.isFile()) {
configs[domain] = convict(this.domainSchema)
configs[domain].loadFile(configPath)
}
} catch (err) {
logger.info(
{module: 'config'},
`'${this.get('env')}' config not found for domain ${domain}`
)
}
})
this.domainConfigs = configs
return configs
}
/**
* A reference to the original `set` method from convict.
*
* @type {Function}
*/
Config.prototype._set = Config.prototype.set
/**
* Sets a configuration value for a given domain name, if one
* is specified. If not, the method behaves like the original
* `set` method from Convict.
*
* @param {String} path
* @param {Object} value
* @param {String} domain
*/
Config.prototype.set = function(path, value, domain) {
if (
domain === undefined ||
this.domainConfigs[domain] === undefined ||
!objectPath.get(schema, `${path}.allowDomainOverride`)
) {
return this._set(path, value)
}
return this.domainConfigs[domain].set(path, value)
}
module.exports = new Config()
module.exports.Config = Config
module.exports.schema = schema