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
16 changes: 14 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-restricted-syntax */
import { request } from 'undici';
import { join } from 'path';
import { join } from 'node:path';
import { URL } from 'node:url';
import loader from '@eik/common-config-loader';
import Asset from './asset.js';

Expand All @@ -9,6 +10,8 @@ const trimSlash = (value = '') => {
return value;
};

const ABSOLUTE_URL_REGEX = /^[a-zA-Z][a-zA-Z\d+\-.]*?:/;

const fetchImportMaps = async (urls = []) => {
try {
const maps = urls.map(async (map) => {
Expand Down Expand Up @@ -95,8 +98,17 @@ export default class NodeClient {

file(file = '') {
const base = this.base();
let value;
if (base && ABSOLUTE_URL_REGEX.test(base)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to use path.isAbsolute here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checked. isAbsolute is not doing the same thing as my test. my check is for an absolute URL, but path.isAbsolute checks for an absolute path. So isAbsolute('/test') is true but ABSOLUTE_URL_REGEX.test('/test') is false

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good point.

const baseURL = new URL(base);
const subPath = join(baseURL.pathname, file);
value = new URL(subPath, base).toString();
} else {
value = join(base, '/', file);
}

return new Asset({
value: `${base}${file}`,
value
});
}

Expand Down
41 changes: 41 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,47 @@ tap.test('Client - Retrieve a file path - Development mode is set to "true" - Ba
t.end();
});

tap.test('Client - Retrieve a file path - Development mode is set to "true" - Base is set to absolute URL - file without starting slash', async (t) => {
const client = new NodeClient({
development: true,
base: 'http://localhost:7777/prefix/',
path: t.context.fixture,
});
await client.load();

const resolved = client.file('some/path/foo.js');

t.equal(resolved.value, 'http://localhost:7777/prefix/some/path/foo.js');
t.end();
});

tap.test('Client - Retrieve a file path - Development mode is set to "true" - Base is set to relative path - file without starting slash', async (t) => {
const client = new NodeClient({
development: true,
base: '/prefix/',
path: t.context.fixture,
});
await client.load();

const resolved = client.file('some/path/foo.js');

t.equal(resolved.value, '/prefix/some/path/foo.js');
t.end();
});

tap.test('Client - Retrieve a file path - Development mode is set to "true" - Base is unset - file without starting slash', async (t) => {
const client = new NodeClient({
development: true,
path: t.context.fixture,
});
await client.load();

const resolved = client.file('some/path/foo.js');

t.equal(resolved.value, '/some/path/foo.js');
t.end();
});

tap.test('Client - Load maps', async (t) => {
const client = new NodeClient({
loadMaps: true,
Expand Down