From 2b1b72b91e67bd682c5ee6b3f37cf8f40b3ac697 Mon Sep 17 00:00:00 2001 From: Nulo Date: Tue, 19 Dec 2023 12:40:53 -0300 Subject: [PATCH] chore: mover utils a utils.js --- downloader/download_json.js | 25 ++----------------------- downloader/utils.js | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 23 deletions(-) create mode 100644 downloader/utils.js diff --git a/downloader/download_json.js b/downloader/download_json.js index 9aeebdf..ee46c88 100644 --- a/downloader/download_json.js +++ b/downloader/download_json.js @@ -1,5 +1,5 @@ import { mkdir, writeFile } from "node:fs/promises"; -import { join, normalize } from "node:path"; +import { join } from "node:path"; import { targetsPorDefecto } from "./config.js"; import { generateDataJsonFromCkan } from "./ckan_to_datajson.js"; import { zData } from "common/schema.js"; @@ -10,6 +10,7 @@ import { } from "./network.js"; import { createWriteStream } from "node:fs"; import pMap from "p-map"; +import { sanitizeSuffix, shuffleArray, hasDuplicates } from "./utils.js"; let urls = process.argv.slice(2); if (urls.length < 1) { @@ -166,14 +167,6 @@ async function downloadDistWithRetries({ dist, dataset, url, outputPath }) { * @prop {Date=} waitUntil */ -// https://security.stackexchange.com/a/123723 -/** - * @argument {string} path - */ -function sanitizeSuffix(path) { - return normalize(path).replace(/^(\.\.(\/|\\|$))+/, ""); -} - /** * @param {DownloadJob[]} jobs * @param {string} id @@ -188,11 +181,6 @@ function chequearIdsDuplicados(jobs, id) { ); } } -// https://stackoverflow.com/a/7376645 -/** @argument {any[]} array */ -function hasDuplicates(array) { - return new Set(array).size !== array.length; -} /** * @param {{ dataset: import("common/schema.js").Dataset, dist: import("common/schema.js").Distribution, url?: URL }} job @@ -228,12 +216,3 @@ function patchUrl(url) { } return url; } - -// https://stackoverflow.com/a/12646864 -/** @param {any[]} array */ -function shuffleArray(array) { - for (let i = array.length - 1; i > 0; i--) { - const j = Math.floor(Math.random() * (i + 1)); - [array[i], array[j]] = [array[j], array[i]]; - } -} diff --git a/downloader/utils.js b/downloader/utils.js new file mode 100644 index 0000000..77e8790 --- /dev/null +++ b/downloader/utils.js @@ -0,0 +1,24 @@ +import { normalize } from "node:path"; + +// https://security.stackexchange.com/a/123723 +/** + * @argument {string} path + */ +export function sanitizeSuffix(path) { + return normalize(path).replace(/^(\.\.(\/|\\|$))+/, ""); +} + +// https://stackoverflow.com/a/7376645 +/** @argument {any[]} array */ +export function hasDuplicates(array) { + return new Set(array).size !== array.length; +} + +// https://stackoverflow.com/a/12646864 +/** @param {any[]} array */ +export function shuffleArray(array) { + for (let i = array.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [array[i], array[j]] = [array[j], array[i]]; + } +}