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
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
61 changes: 2 additions & 59 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { helpers } from "@eik/common";
import { request, interceptors, Agent } from "undici";
import { join } from "path";
import { Asset } from "./asset.js";

Expand All @@ -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<ImportMap[]>}
*/
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.
*/

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

/**
* @param {Options} options
Expand All @@ -148,15 +95,13 @@ 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 @@ -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);
}
}

Expand Down