Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macOS-latest, windows-latest]
node-version: [20, 22]
node-version: [20, 22, 24]
runs-on: ${{ matrix.os }}

steps:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"dependencies": {
"@eik/common": "5.0.4",
"abslog": "2.4.4",
"undici": "6.21.2"
"undici": "7.8.0"
},
"devDependencies": {
"@eik/eslint-config": "1.0.13",
Expand Down
50 changes: 41 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { helpers } from "@eik/common";
import { request } from "undici";
import { request, interceptors, Agent } from "undici";
import { join } from "path";
import { Asset } from "./asset.js";

Expand All @@ -8,21 +8,47 @@ const trimSlash = (value = "") => {
return value;
};

const fetchImportMaps = async (urls = []) => {
/**
* @param {string[]} urls
* @param {object} options
* @param {number} options.maxRedirections - undici option for limiting redirects
* @returns {Promise<ImportMap[]>}
*/
const fetchImportMaps = async (urls = [], options) => {
try {
const maps = urls.map(async (map) => {
const { statusCode, body } = await request(map, {
maxRedirections: 2,
const response = await request(map, {
dispatcher: new Agent().compose(
interceptors.redirect({
maxRedirections: options.maxRedirections,
}),
),
});

if (statusCode === 404) {
if (response.statusCode === 404) {
throw new Error("Import map could not be found on server");
} else if (statusCode >= 400 && statusCode < 500) {
} else if (response.statusCode >= 400 && response.statusCode < 500) {
throw new Error("Server rejected client request");
} else if (statusCode >= 500) {
} else if (response.statusCode >= 500) {
throw new Error("Server error");
}
return body.json();
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) {
Expand All @@ -38,6 +64,7 @@ const fetchImportMaps = async (urls = []) => {
* @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.
*/

/**
Expand Down Expand Up @@ -111,6 +138,7 @@ export default class Eik {
#path;
#base;
#maps;
#maxRedirections;

/**
* @param {Options} options
Expand All @@ -120,13 +148,15 @@ export default class Eik {
loadMaps = false,
base = "",
path = process.cwd(),
maxRedirections = 2,
} = {}) {
this.#development = development;
this.#loadMaps = loadMaps;
this.#config = {};
this.#path = path;
this.#base = trimSlash(base);
this.#maps = [];
this.#maxRedirections = maxRedirections;
}

/**
Expand All @@ -139,7 +169,9 @@ export default class Eik {
async load() {
this.#config = await helpers.getDefaults(this.#path);
if (this.#loadMaps) {
this.#maps = await fetchImportMaps(this.#config.map);
this.#maps = await fetchImportMaps(this.#config.map, {
maxRedirections: this.#maxRedirections,
});
}
}

Expand Down