@nodeart/cloudflare-provisioning 1.0.13 → 1.0.15

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.
@@ -11,10 +11,10 @@ jobs:
11
11
  build:
12
12
  runs-on: ubuntu-latest
13
13
  steps:
14
- - uses: actions/checkout@v3
15
- - uses: actions/setup-node@v3
14
+ - uses: actions/checkout@v4
15
+ - uses: actions/setup-node@v4
16
16
  with:
17
- node-version: 16
17
+ node-version: 24
18
18
  - run: npm ci
19
19
  - run: npm test
20
20
 
@@ -22,10 +22,10 @@ jobs:
22
22
  needs: build
23
23
  runs-on: ubuntu-latest
24
24
  steps:
25
- - uses: actions/checkout@v3
26
- - uses: actions/setup-node@v3
25
+ - uses: actions/checkout@v4
26
+ - uses: actions/setup-node@v4
27
27
  with:
28
- node-version: 16
28
+ node-version: 24
29
29
  registry-url: https://registry.npmjs.org/
30
30
  - run: npm ci
31
31
  - run: npm publish
@@ -10,9 +10,9 @@ jobs:
10
10
  build:
11
11
  runs-on: ubuntu-latest
12
12
  steps:
13
- - uses: actions/checkout@v3
14
- - uses: actions/setup-node@v3
13
+ - uses: actions/checkout@v4
14
+ - uses: actions/setup-node@v4
15
15
  with:
16
- node-version: 16
16
+ node-version: 24
17
17
  - run: npm ci
18
18
  - run: npm test
package/cloudflare.js CHANGED
@@ -210,7 +210,43 @@ class CloudFlare {
210
210
  response = await body.text()
211
211
  }
212
212
 
213
- if (statusCode !== 200) {
213
+ if (statusCode === 404) {
214
+ console.log('Firewall ruleset was not found. Initializing firewall ruleset creation...')
215
+ const createRulesetUrl = CLOUDFLARE_API_URL + `zones/${this.zoneId}/rulesets`
216
+ const payload = {
217
+ name: 'Custom firewall ruleset',
218
+ kind: 'zone',
219
+ phase: 'http_request_firewall_custom',
220
+ rules: []
221
+ }
222
+
223
+ const { statusCode: createStatusCode, body: createBody } = await this.requestWithDelay(createRulesetUrl, {
224
+ method: 'POST',
225
+ headers: {
226
+ ...this.authorizationHeaders,
227
+ 'Content-Type': 'application/json'
228
+ },
229
+ body: JSON.stringify(payload)
230
+ })
231
+
232
+ let createResponse
233
+ try {
234
+ createResponse = await createBody.json()
235
+ } catch (e) {
236
+ createResponse = await createBody.text()
237
+ }
238
+
239
+ if (createStatusCode !== 200) {
240
+ throw new Error(`Could not create firewall ruleset: ${createStatusCode}, error: ${JSON.stringify(createResponse)}`)
241
+ }
242
+
243
+ const { id, rules } = createResponse?.result ?? {}
244
+ if (!id) {
245
+ throw new Error(`Could not get firewall rules ruleset ID: got ${id}, received value: ${JSON.stringify(createResponse)}`)
246
+ }
247
+
248
+ return { id, rules: rules ?? [] }
249
+ } else if (statusCode !== 200) {
214
250
  throw new Error(`Could not get firewall rules: ${statusCode}, error: ${JSON.stringify(response)}`)
215
251
  }
216
252
 
@@ -424,27 +460,33 @@ class CloudFlare {
424
460
  }
425
461
 
426
462
  async rewriteRedirectRules (redirectRules) {
427
- const { id: rulesetId, rules: currentRedirectRules } = await this.getRedirectRules()
463
+ const { id: rulesetId } = await this.getRedirectRules()
428
464
 
429
465
  if (!rulesetId) {
430
466
  console.error(`Could not update redirect rules for domain ${this.domain}: custom firewall ruleset id is not found`)
431
467
  throw new Error('Custom redirect ruleset id is not found')
432
468
  }
433
469
 
434
- for (const redirectRule of redirectRules) {
435
- const currentRedirectRule = currentRedirectRules?.find(
436
- rule => rule.description === redirectRule.description
437
- )
470
+ const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/rulesets/${rulesetId}`
438
471
 
439
- try {
440
- if (currentRedirectRule) {
441
- await this.updateRedirectRule(rulesetId, currentRedirectRule.id, redirectRule)
442
- } else {
443
- await this.createRedirectRule(rulesetId, redirectRule)
444
- }
445
- } catch (error) {
446
- console.error(`Could not update redirect rule for domain ${this.domain}: ${JSON.stringify(redirectRule)}, error: ${error}`)
447
- }
472
+ const { statusCode, body } = await this.requestWithDelay(url, {
473
+ method: 'PUT',
474
+ headers: {
475
+ ...this.authorizationHeaders,
476
+ 'Content-Type': 'application/json'
477
+ },
478
+ body: JSON.stringify({ rules: redirectRules })
479
+ })
480
+
481
+ let response
482
+ try {
483
+ response = await body.json()
484
+ } catch (e) {
485
+ response = await body.text()
486
+ }
487
+
488
+ if (statusCode !== 200) {
489
+ throw new Error(`Could not update redirect rules: ${statusCode}, error: ${JSON.stringify(response)}`)
448
490
  }
449
491
  }
450
492
 
@@ -547,6 +589,9 @@ class CloudFlare {
547
589
  const response = await body.json()
548
590
 
549
591
  if (statusCode !== 200) {
592
+ if (response && response.errors && response.errors.find(e => e.code === 1015)) {
593
+ return response
594
+ }
550
595
  throw new Error(`Could not set HTTP2: ${statusCode}, error: ${JSON.stringify(response)}`)
551
596
  }
552
597
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nodeart/cloudflare-provisioning",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {