reorganized code

This commit is contained in:
Viet Huynh
2023-09-09 21:43:57 +07:00
parent 61d6c03704
commit ed5f24b6ec
3 changed files with 21 additions and 24 deletions
+10 -8
View File
@@ -1,6 +1,7 @@
import fs from 'fs'; import fs from 'fs';
import { DRY_RUN, LIST_ITEM_LIMIT } from './lib/constants.js'; import { DRY_RUN, FAST_MODE, LIST_ITEM_LIMIT } from './lib/constants.js';
import { createZeroTrustLists } from './lib/api.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}`); 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 // Trim array to 300,000 domains if it's longer than that
if (domains.length > LIST_ITEM_LIMIT) { 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`); 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); 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 // 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.'); 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);
});
+3 -16
View File
@@ -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"; import { requestGateway } from "./helpers.js";
/** /**
@@ -24,7 +24,7 @@ const createZeroTrustList = (name, items) =>
* Creates Zero Trust lists sequentially. * Creates Zero Trust lists sequentially.
* @param {string[]} items The domains. * @param {string[]} items The domains.
*/ */
const createZeroTrustListsOneByOne = async (items) => { export const createZeroTrustListsOneByOne = async (items) => {
let totalListNumber = Math.ceil(items.length / LIST_ITEM_SIZE); let totalListNumber = Math.ceil(items.length / LIST_ITEM_SIZE);
for (let i = 0, listNumber = 1; i < items.length; i += 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. * Creates all Zero Trust lists at once.
* @param {string[]} items The domains. * @param {string[]} items The domains.
*/ */
const createZeroTrustListsAtOnce = async (items) => { export const createZeroTrustListsAtOnce = async (items) => {
const totalListNumber = Math.ceil(items.length / LIST_ITEM_SIZE); const totalListNumber = Math.ceil(items.length / LIST_ITEM_SIZE);
const requests = []; const requests = [];
@@ -70,16 +70,3 @@ const createZeroTrustListsAtOnce = async (items) => {
console.error(`Error occurred while creating lists - ${err.toString()}`); console.error(`Error occurred while creating lists - ${err.toString()}`);
} }
}; };
/**
* Create Zero Trust lists.
* @param {string[]} items The domains.
* @returns {Promise<void>}
*/
export const createZeroTrustLists = (items) => {
if (FAST_MODE) {
return createZeroTrustListsAtOnce(items);
}
return createZeroTrustListsOneByOne(items);
};
+8
View File
@@ -4,3 +4,11 @@
*/ */
export const sleep = (ms = 350) => export const sleep = (ms = 350) =>
new Promise((resolve) => setTimeout(resolve, ms)); 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);