diff --git a/cf_list_create.js b/cf_list_create.js index bdef4eb..61260f7 100644 --- a/cf_list_create.js +++ b/cf_list_create.js @@ -1,6 +1,7 @@ import fs from 'fs'; -import { DRY_RUN, LIST_ITEM_LIMIT } from './lib/constants.js'; -import { createZeroTrustLists } from './lib/api.js'; +import { DRY_RUN, FAST_MODE, LIST_ITEM_LIMIT } from './lib/constants.js'; +import { createZeroTrustListsAtOnce, createZeroTrustListsOneByOne } from './lib/api.js'; +import { truncateArray } from './lib/utils.js'; if (!process.env.CI) console.log(`List item limit set to ${LIST_ITEM_LIMIT}`); @@ -99,7 +100,7 @@ fs.readFile('input.csv', 'utf8', async (err, data) => { // Trim array to 300,000 domains if it's longer than that if (domains.length > LIST_ITEM_LIMIT) { console.warn(`${domains.length} domains found in input.csv - input has to be trimmed to ${LIST_ITEM_LIMIT} domains`); - domains = trimArray(domains, LIST_ITEM_LIMIT); + domains = truncateArray(domains, LIST_ITEM_LIMIT); } const listsToCreate = Math.ceil(domains.length / 1000); @@ -110,9 +111,10 @@ fs.readFile('input.csv', 'utf8', async (err, data) => { // TODO: we should probably continue, just without making any real requests to Cloudflare if (DRY_RUN) return console.log('Dry run complete - no lists were created. If this was not intended, please remove the DRY_RUN environment variable and try again.'); - await createZeroTrustLists(domains) -}); + if (FAST_MODE) { + createZeroTrustListsAtOnce(items); + return; + } -function trimArray(arr, size) { - return arr.slice(0, size); -} + createZeroTrustListsOneByOne(items); +}); diff --git a/lib/api.js b/lib/api.js index 628ec9e..ccd36c7 100644 --- a/lib/api.js +++ b/lib/api.js @@ -1,4 +1,4 @@ -import { FAST_MODE, LIST_ITEM_SIZE } from "./constants.js"; +import { LIST_ITEM_SIZE } from "./constants.js"; import { requestGateway } from "./helpers.js"; /** @@ -24,7 +24,7 @@ const createZeroTrustList = (name, items) => * Creates Zero Trust lists sequentially. * @param {string[]} items The domains. */ -const createZeroTrustListsOneByOne = async (items) => { +export const createZeroTrustListsOneByOne = async (items) => { let totalListNumber = Math.ceil(items.length / LIST_ITEM_SIZE); for (let i = 0, listNumber = 1; i < items.length; i += LIST_ITEM_SIZE) { @@ -49,7 +49,7 @@ const createZeroTrustListsOneByOne = async (items) => { * Creates all Zero Trust lists at once. * @param {string[]} items The domains. */ -const createZeroTrustListsAtOnce = async (items) => { +export const createZeroTrustListsAtOnce = async (items) => { const totalListNumber = Math.ceil(items.length / LIST_ITEM_SIZE); const requests = []; @@ -70,16 +70,3 @@ const createZeroTrustListsAtOnce = async (items) => { console.error(`Error occurred while creating lists - ${err.toString()}`); } }; - -/** - * Create Zero Trust lists. - * @param {string[]} items The domains. - * @returns {Promise} - */ -export const createZeroTrustLists = (items) => { - if (FAST_MODE) { - return createZeroTrustListsAtOnce(items); - } - - return createZeroTrustListsOneByOne(items); -}; diff --git a/lib/utils.js b/lib/utils.js index 9c1037f..1d13e0c 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -4,3 +4,11 @@ */ export const sleep = (ms = 350) => new Promise((resolve) => setTimeout(resolve, ms)); + +/** + * Truncates an array to the specified size. + * @param {any[]} arr The array to be truncated. + * @param {number} size The size to which the array will be truncated. + * @returns {any[]} + */ +export const truncateArray = (arr, size) => arr.slice(0, size);