From fcbcf283270628c7b2a28c2a41c63a1095fe49d2 Mon Sep 17 00:00:00 2001 From: William Killerud Date: Thu, 8 May 2025 14:19:28 +0200 Subject: [PATCH] fix: update eik/common to use the shared fetch --- package.json | 5 ++--- src/index.js | 61 ++-------------------------------------------------- 2 files changed, 4 insertions(+), 62 deletions(-) diff --git a/package.json b/package.json index 1da4d6e..7296877 100644 --- a/package.json +++ b/package.json @@ -43,9 +43,8 @@ }, "homepage": "https://github.com/eik-lib/node-client#readme", "dependencies": { - "@eik/common": "5.0.4", - "abslog": "2.4.4", - "undici": "7.8.0" + "@eik/common": "5.1.0", + "abslog": "2.4.4" }, "devDependencies": { "@eik/eslint-config": "1.0.13", diff --git a/src/index.js b/src/index.js index b771cb3..afe635e 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,4 @@ import { helpers } from "@eik/common"; -import { request, interceptors, Agent } from "undici"; import { join } from "path"; import { Asset } from "./asset.js"; @@ -8,63 +7,12 @@ const trimSlash = (value = "") => { return value; }; -/** - * @param {string[]} urls - * @param {object} options - * @param {number} options.maxRedirections - undici option for limiting redirects - * @returns {Promise} - */ -const fetchImportMaps = async (urls = [], options) => { - try { - const maps = urls.map(async (map) => { - const response = await request(map, { - dispatcher: new Agent().compose( - interceptors.redirect({ - maxRedirections: options.maxRedirections, - }), - ), - }); - - if (response.statusCode === 404) { - throw new Error("Import map could not be found on server"); - } else if (response.statusCode >= 400 && response.statusCode < 500) { - throw new Error("Server rejected client request"); - } else if (response.statusCode >= 500) { - throw new Error("Server error"); - } - let contentType = response.headers["content-type"]; - if (!Array.isArray(contentType)) contentType = [contentType]; - - if (!contentType.find((type) => type.startsWith("application/json"))) { - const content = await response.body.text(); - if (content.length === 0) { - throw new Error( - `${map} did not return JSON, got an empty response. HTTP status: ${response.statusCode}`, - ); - } - throw new Error( - `${map} did not return JSON, got: ${content}. HTTP status: ${response.statusCode}`, - ); - } - - const json = await response.body.json(); - return /** @type {ImportMap}*/ (json); - }); - return await Promise.all(maps); - } catch (err) { - throw new Error( - `Unable to load import map file from server: ${err.message}`, - ); - } -}; - /** * @typedef {object} Options * @property {string} [base=null] * @property {boolean} [development=false] * @property {boolean} [loadMaps=false] * @property {string} [path=process.cwd()] - * @property {number} [maxRedirections=2] Maximum number of redirects when looking up URLs. */ /** @@ -138,7 +86,6 @@ export default class Eik { #path; #base; #maps; - #maxRedirections; /** * @param {Options} options @@ -148,7 +95,6 @@ export default class Eik { loadMaps = false, base = "", path = process.cwd(), - maxRedirections = 2, } = {}) { this.#development = development; this.#loadMaps = loadMaps; @@ -156,7 +102,6 @@ export default class Eik { this.#path = path; this.#base = trimSlash(base); this.#maps = []; - this.#maxRedirections = maxRedirections; } /** @@ -167,11 +112,9 @@ export default class Eik { * use in {@link maps}. */ async load() { - this.#config = await helpers.getDefaults(this.#path); + this.#config = helpers.getDefaults(this.#path); if (this.#loadMaps) { - this.#maps = await fetchImportMaps(this.#config.map, { - maxRedirections: this.#maxRedirections, - }); + this.#maps = await helpers.fetchImportMaps(this.#config.map); } }