chore: mover utils a utils.js

This commit is contained in:
Cat /dev/Nulo 2023-12-19 12:40:53 -03:00
parent 6c3776ffd2
commit 2b1b72b91e
2 changed files with 26 additions and 23 deletions

View file

@ -1,5 +1,5 @@
import { mkdir, writeFile } from "node:fs/promises"; import { mkdir, writeFile } from "node:fs/promises";
import { join, normalize } from "node:path"; import { join } from "node:path";
import { targetsPorDefecto } from "./config.js"; import { targetsPorDefecto } from "./config.js";
import { generateDataJsonFromCkan } from "./ckan_to_datajson.js"; import { generateDataJsonFromCkan } from "./ckan_to_datajson.js";
import { zData } from "common/schema.js"; import { zData } from "common/schema.js";
@ -10,6 +10,7 @@ import {
} from "./network.js"; } from "./network.js";
import { createWriteStream } from "node:fs"; import { createWriteStream } from "node:fs";
import pMap from "p-map"; import pMap from "p-map";
import { sanitizeSuffix, shuffleArray, hasDuplicates } from "./utils.js";
let urls = process.argv.slice(2); let urls = process.argv.slice(2);
if (urls.length < 1) { if (urls.length < 1) {
@ -166,14 +167,6 @@ async function downloadDistWithRetries({ dist, dataset, url, outputPath }) {
* @prop {Date=} waitUntil * @prop {Date=} waitUntil
*/ */
// https://security.stackexchange.com/a/123723
/**
* @argument {string} path
*/
function sanitizeSuffix(path) {
return normalize(path).replace(/^(\.\.(\/|\\|$))+/, "");
}
/** /**
* @param {DownloadJob[]} jobs * @param {DownloadJob[]} jobs
* @param {string} id * @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 * @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; 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]];
}
}

24
downloader/utils.js Normal file
View file

@ -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]];
}
}