@nodeart/cloudflare-provisioning 1.0.11 → 1.0.13

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/cloudflare.js CHANGED
@@ -6,9 +6,10 @@ const { request } = require('undici')
6
6
  const CLOUDFLARE_API_URL = 'https://api.cloudflare.com/client/v4/'
7
7
 
8
8
  class CloudFlare {
9
- constructor (zoneId, domain, options) {
9
+ constructor (zoneId, domain, options, requestDelay = 0) {
10
10
  this.zoneId = zoneId
11
11
  this.domain = domain
12
+ this.requestDelay = requestDelay
12
13
 
13
14
  this.authorizationHeaders = null
14
15
  if (options.email !== undefined && options.apiKey !== undefined) {
@@ -28,7 +29,7 @@ class CloudFlare {
28
29
  async setIPv6 (value) {
29
30
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/settings/ipv6`
30
31
 
31
- const { statusCode, body } = await request(url, {
32
+ const { statusCode, body } = await this.requestWithDelay(url, {
32
33
  method: 'PATCH',
33
34
  headers: {
34
35
  ...this.authorizationHeaders,
@@ -49,7 +50,7 @@ class CloudFlare {
49
50
  async setEmailObfuscation (value) {
50
51
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/settings/email_obfuscation`
51
52
 
52
- const { statusCode, body } = await request(url, {
53
+ const { statusCode, body } = await this.requestWithDelay(url, {
53
54
  method: 'PATCH',
54
55
  headers: {
55
56
  ...this.authorizationHeaders,
@@ -70,7 +71,7 @@ class CloudFlare {
70
71
  async setSSL (value) {
71
72
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/settings/ssl`
72
73
 
73
- const { statusCode, body } = await request(url, {
74
+ const { statusCode, body } = await this.requestWithDelay(url, {
74
75
  method: 'PATCH',
75
76
  headers: {
76
77
  ...this.authorizationHeaders,
@@ -91,7 +92,7 @@ class CloudFlare {
91
92
  async setBrotli (value) {
92
93
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/settings/brotli`
93
94
 
94
- const { statusCode, body } = await request(url, {
95
+ const { statusCode, body } = await this.requestWithDelay(url, {
95
96
  method: 'PATCH',
96
97
  headers: {
97
98
  ...this.authorizationHeaders,
@@ -112,7 +113,7 @@ class CloudFlare {
112
113
  async getDNSRecords () {
113
114
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/dns_records`
114
115
 
115
- const { statusCode, body } = await request(url, {
116
+ const { statusCode, body } = await this.requestWithDelay(url, {
116
117
  method: 'GET',
117
118
  headers: {
118
119
  ...this.authorizationHeaders,
@@ -152,7 +153,7 @@ class CloudFlare {
152
153
  async createDNSRecord (dnsRecord) {
153
154
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/dns_records`
154
155
 
155
- const { statusCode, body } = await request(url, {
156
+ const { statusCode, body } = await this.requestWithDelay(url, {
156
157
  method: 'POST',
157
158
  headers: {
158
159
  ...this.authorizationHeaders,
@@ -173,7 +174,7 @@ class CloudFlare {
173
174
  async updateDNSRecord (id, dnsRecord) {
174
175
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/dns_records/${id}`
175
176
 
176
- const { statusCode, body } = await request(url, {
177
+ const { statusCode, body } = await this.requestWithDelay(url, {
177
178
  method: 'PATCH',
178
179
  headers: {
179
180
  ...this.authorizationHeaders,
@@ -194,7 +195,7 @@ class CloudFlare {
194
195
  async getFirewallRules () {
195
196
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/rulesets/phases/http_request_firewall_custom/entrypoint`
196
197
 
197
- const { statusCode, body } = await request(url, {
198
+ const { statusCode, body } = await this.requestWithDelay(url, {
198
199
  method: 'GET',
199
200
  headers: {
200
201
  ...this.authorizationHeaders,
@@ -228,7 +229,7 @@ class CloudFlare {
228
229
  const rule = { ...firewallRule, ...filter }
229
230
  delete rule.filter
230
231
 
231
- const { statusCode, body } = await request(url, {
232
+ const { statusCode, body } = await this.requestWithDelay(url, {
232
233
  method: 'POST',
233
234
  headers: {
234
235
  ...this.authorizationHeaders,
@@ -258,7 +259,7 @@ class CloudFlare {
258
259
  const rule = { ...firewallRule, ...filter }
259
260
  delete rule.filter
260
261
 
261
- const { statusCode, body } = await request(url, {
262
+ const { statusCode, body } = await this.requestWithDelay(url, {
262
263
  method: 'PATCH',
263
264
  headers: {
264
265
  ...this.authorizationHeaders,
@@ -309,7 +310,7 @@ class CloudFlare {
309
310
  async getRedirectRules () {
310
311
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/rulesets/phases/http_request_dynamic_redirect/entrypoint`
311
312
 
312
- const { statusCode, body } = await request(url, {
313
+ const { statusCode, body } = await this.requestWithDelay(url, {
313
314
  method: 'GET',
314
315
  headers: {
315
316
  ...this.authorizationHeaders,
@@ -335,7 +336,7 @@ class CloudFlare {
335
336
  rules: []
336
337
  }
337
338
 
338
- const { statusCode: createStatusCode, body: createBody } = await request(createRulesetUrl, {
339
+ const { statusCode: createStatusCode, body: createBody } = await this.requestWithDelay(createRulesetUrl, {
339
340
  method: 'POST',
340
341
  headers: {
341
342
  ...this.authorizationHeaders,
@@ -373,7 +374,7 @@ class CloudFlare {
373
374
  async createRedirectRule (rulesetId, redirectRule) {
374
375
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/rulesets/${rulesetId}/rules`
375
376
 
376
- const { statusCode, body } = await request(url, {
377
+ const { statusCode, body } = await this.requestWithDelay(url, {
377
378
  method: 'POST',
378
379
  headers: {
379
380
  ...this.authorizationHeaders,
@@ -399,7 +400,7 @@ class CloudFlare {
399
400
  async updateRedirectRule (rulesetId, ruleId, redirectRule) {
400
401
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/rulesets/${rulesetId}/rules/${ruleId}`
401
402
 
402
- const { statusCode, body } = await request(url, {
403
+ const { statusCode, body } = await this.requestWithDelay(url, {
403
404
  method: 'PATCH',
404
405
  headers: {
405
406
  ...this.authorizationHeaders,
@@ -450,7 +451,7 @@ class CloudFlare {
450
451
  async setPolish (value) {
451
452
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/settings/polish`
452
453
 
453
- const { statusCode, body } = await request(url, {
454
+ const { statusCode, body } = await this.requestWithDelay(url, {
454
455
  method: 'PATCH',
455
456
  headers: {
456
457
  ...this.authorizationHeaders,
@@ -471,7 +472,7 @@ class CloudFlare {
471
472
  async setMinify (value) {
472
473
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/settings/minify`
473
474
 
474
- const { statusCode, body } = await request(url, {
475
+ const { statusCode, body } = await this.requestWithDelay(url, {
475
476
  method: 'PATCH',
476
477
  headers: {
477
478
  ...this.authorizationHeaders,
@@ -492,7 +493,7 @@ class CloudFlare {
492
493
  async setHTTP2Prioritization (value) {
493
494
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/settings/h2_prioritization`
494
495
 
495
- const { statusCode, body } = await request(url, {
496
+ const { statusCode, body } = await this.requestWithDelay(url, {
496
497
  method: 'PATCH',
497
498
  headers: {
498
499
  ...this.authorizationHeaders,
@@ -513,7 +514,7 @@ class CloudFlare {
513
514
  async setPrefetchURLs (value) {
514
515
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/settings/prefetch_preload`
515
516
 
516
- const { statusCode, body } = await request(url, {
517
+ const { statusCode, body } = await this.requestWithDelay(url, {
517
518
  method: 'PATCH',
518
519
  headers: {
519
520
  ...this.authorizationHeaders,
@@ -534,7 +535,7 @@ class CloudFlare {
534
535
  async setHttp2 (value) {
535
536
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/settings/http2`
536
537
 
537
- const { statusCode, body } = await request(url, {
538
+ const { statusCode, body } = await this.requestWithDelay(url, {
538
539
  method: 'PATCH',
539
540
  headers: {
540
541
  ...this.authorizationHeaders,
@@ -555,7 +556,7 @@ class CloudFlare {
555
556
  async setHttp3 (value) {
556
557
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/settings/http3`
557
558
 
558
- const { statusCode, body } = await request(url, {
559
+ const { statusCode, body } = await this.requestWithDelay(url, {
559
560
  method: 'PATCH',
560
561
  headers: {
561
562
  ...this.authorizationHeaders,
@@ -576,7 +577,7 @@ class CloudFlare {
576
577
  async set0RTT (value) {
577
578
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/settings/0rtt`
578
579
 
579
- const { statusCode, body } = await request(url, {
580
+ const { statusCode, body } = await this.requestWithDelay(url, {
580
581
  method: 'PATCH',
581
582
  headers: {
582
583
  ...this.authorizationHeaders,
@@ -597,7 +598,7 @@ class CloudFlare {
597
598
  async setArgoSmartRouting (value) {
598
599
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/argo/smart_routing`
599
600
 
600
- const { statusCode, body } = await request(url, {
601
+ const { statusCode, body } = await this.requestWithDelay(url, {
601
602
  method: 'PATCH',
602
603
  headers: {
603
604
  ...this.authorizationHeaders,
@@ -618,7 +619,7 @@ class CloudFlare {
618
619
  async getPageRules () {
619
620
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/pagerules`
620
621
 
621
- const { statusCode, body } = await request(url, {
622
+ const { statusCode, body } = await this.requestWithDelay(url, {
622
623
  method: 'GET',
623
624
  headers: {
624
625
  ...this.authorizationHeaders,
@@ -638,7 +639,7 @@ class CloudFlare {
638
639
  async updatePageRule (pageRuleId, pageRule) {
639
640
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/pagerules/${pageRuleId}`
640
641
 
641
- const { statusCode, body } = await request(url, {
642
+ const { statusCode, body } = await this.requestWithDelay(url, {
642
643
  method: 'PUT',
643
644
  headers: {
644
645
  ...this.authorizationHeaders,
@@ -659,7 +660,7 @@ class CloudFlare {
659
660
  async createPageRule (pageRule) {
660
661
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/pagerules`
661
662
 
662
- const { statusCode, body } = await request(url, {
663
+ const { statusCode, body } = await this.requestWithDelay(url, {
663
664
  method: 'POST',
664
665
  headers: {
665
666
  ...this.authorizationHeaders,
@@ -708,7 +709,7 @@ class CloudFlare {
708
709
  async getAvailablePageRules () {
709
710
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/pagerules/settings`
710
711
 
711
- const { statusCode, body } = await request(url, {
712
+ const { statusCode, body } = await this.requestWithDelay(url, {
712
713
  method: 'GET',
713
714
  headers: {
714
715
  ...this.authorizationHeaders,
@@ -728,7 +729,7 @@ class CloudFlare {
728
729
  async createWorkerRoute (workerRoute) {
729
730
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/workers/routes`
730
731
 
731
- const { statusCode, body } = await request(url, {
732
+ const { statusCode, body } = await this.requestWithDelay(url, {
732
733
  method: 'POST',
733
734
  headers: {
734
735
  ...this.authorizationHeaders,
@@ -762,7 +763,7 @@ class CloudFlare {
762
763
  async getWorkerRoutes () {
763
764
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/workers/routes`
764
765
 
765
- const { statusCode, body } = await request(url, {
766
+ const { statusCode, body } = await this.requestWithDelay(url, {
766
767
  method: 'GET',
767
768
  headers: {
768
769
  ...this.authorizationHeaders,
@@ -782,7 +783,7 @@ class CloudFlare {
782
783
  async deleteWorkerRoute (routeId) {
783
784
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/workers/routes/${routeId}`
784
785
 
785
- const { statusCode, body } = await request(url, {
786
+ const { statusCode, body } = await this.requestWithDelay(url, {
786
787
  method: 'DELETE',
787
788
  headers: {
788
789
  ...this.authorizationHeaders,
@@ -826,7 +827,7 @@ class CloudFlare {
826
827
  async setHotlinkProtection (value) {
827
828
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/settings/hotlink_protection`
828
829
 
829
- const { statusCode, body } = await request(url, {
830
+ const { statusCode, body } = await this.requestWithDelay(url, {
830
831
  method: 'PATCH',
831
832
  headers: {
832
833
  ...this.authorizationHeaders,
@@ -895,7 +896,7 @@ class CloudFlare {
895
896
 
896
897
  async getClientCerts () {
897
898
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/origin_tls_client_auth`
898
- const { statusCode, body } = await request(url, {
899
+ const { statusCode, body } = await this.requestWithDelay(url, {
899
900
  method: 'GET',
900
901
  headers: {
901
902
  ...this.authorizationHeaders,
@@ -914,7 +915,7 @@ class CloudFlare {
914
915
 
915
916
  async getCaCerts () {
916
917
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/acm/custom_trust_store`
917
- const { statusCode, body } = await request(url, {
918
+ const { statusCode, body } = await this.requestWithDelay(url, {
918
919
  method: 'GET',
919
920
  headers: {
920
921
  ...this.authorizationHeaders,
@@ -933,7 +934,7 @@ class CloudFlare {
933
934
 
934
935
  async deleteClientCert (certId) {
935
936
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/origin_tls_client_auth/${certId}`
936
- const { statusCode, body } = await request(url, {
937
+ const { statusCode, body } = await this.requestWithDelay(url, {
937
938
  method: 'DELETE',
938
939
  headers: {
939
940
  ...this.authorizationHeaders,
@@ -951,7 +952,7 @@ class CloudFlare {
951
952
 
952
953
  async deleteCaCert (certId) {
953
954
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/acm/custom_trust_store/${certId}`
954
- const { statusCode, body } = await request(url, {
955
+ const { statusCode, body } = await this.requestWithDelay(url, {
955
956
  method: 'DELETE',
956
957
  headers: {
957
958
  ...this.authorizationHeaders,
@@ -975,7 +976,7 @@ class CloudFlare {
975
976
  private_key: clientKey.replace(/\r?\n/g, '\n')
976
977
  }
977
978
 
978
- const { statusCode, body } = await request(url, {
979
+ const { statusCode, body } = await this.requestWithDelay(url, {
979
980
  method: 'POST',
980
981
  headers: {
981
982
  ...this.authorizationHeaders,
@@ -1005,7 +1006,7 @@ class CloudFlare {
1005
1006
  certificate: caCert.replace(/\r?\n/g, '\n')
1006
1007
  }
1007
1008
 
1008
- const { statusCode, body } = await request(url, {
1009
+ const { statusCode, body } = await this.requestWithDelay(url, {
1009
1010
  method: 'POST',
1010
1011
  headers: {
1011
1012
  ...this.authorizationHeaders,
@@ -1030,7 +1031,7 @@ class CloudFlare {
1030
1031
 
1031
1032
  async enableTLSClientAuth () {
1032
1033
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/settings/tls_client_auth`
1033
- const { statusCode, body } = await request(url, {
1034
+ const { statusCode, body } = await this.requestWithDelay(url, {
1034
1035
  method: 'PATCH',
1035
1036
  headers: {
1036
1037
  ...this.authorizationHeaders,
@@ -1047,6 +1048,16 @@ class CloudFlare {
1047
1048
 
1048
1049
  console.log(`Enabled TSL Client Auth setting for domain ${this.domain}`, new Date().toISOString())
1049
1050
  }
1051
+
1052
+ async requestWithDelay (url, options) {
1053
+ const response = await request(url, options)
1054
+ await delay(this.requestDelay)
1055
+ return response
1056
+ }
1057
+ }
1058
+
1059
+ function delay (delayMs) {
1060
+ return new Promise(resolve => setTimeout(resolve, delayMs))
1050
1061
  }
1051
1062
 
1052
1063
  module.exports = CloudFlare
package/index.js CHANGED
@@ -37,6 +37,7 @@ async function applyCloudflareSettings (config) {
37
37
  const accountEmail = process.env.CLOUDFLARE_EMAIL
38
38
  const accountKey = process.env.CLOUDFLARE_API_KEY
39
39
  const accountToken = process.env.CLOUDFLARE_TOKEN
40
+ const requestDelayMs = isNaN(parseInt(process.env.DELAY_MS, 10)) ? 500 : parseInt(process.env.DELAY_MS, 10)
40
41
 
41
42
  if (config.enabled === false) {
42
43
  console.log('Config is disabled and would not be applied:', config.domains)
@@ -51,16 +52,12 @@ async function applyCloudflareSettings (config) {
51
52
  throw new Error('Cloudflare zone ID is not defined')
52
53
  }
53
54
 
54
- let options
55
- if (site.token) {
56
- options = { token: site.token }
57
- } else if (accountToken) {
58
- options = { token: accountToken }
59
- } else {
60
- options = { email: accountEmail, apiKey: accountKey }
61
- }
55
+ const options =
56
+ (site.token && { token: site.token }) ||
57
+ (accountToken && { token: accountToken }) ||
58
+ { email: accountEmail, apiKey: accountKey }
62
59
 
63
- const cloudFlare = new CloudFlare(zoneId, site.domain, options)
60
+ const cloudFlare = new CloudFlare(zoneId, site.domain, options, requestDelayMs)
64
61
  const domainSettings = substituteDomainName(settings, site.domain)
65
62
 
66
63
  for (const [key, value] of Object.entries(domainSettings)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nodeart/cloudflare-provisioning",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/template.js CHANGED
@@ -23,7 +23,7 @@ module.exports = {
23
23
  },
24
24
  workers: {
25
25
  disableApi: {
26
- pattern: '*$DOMAIN/api/*',
26
+ pattern: 'www.$DOMAIN/api/*',
27
27
  script: null
28
28
  },
29
29
  rootDomainCookies: {