Skip to content

Commit ec6565e

Browse files
committed
Better config parse for better base URLs
1 parent 06e6fc3 commit ec6565e

File tree

6 files changed

+27
-17
lines changed

6 files changed

+27
-17
lines changed

core.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/core.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/core.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

esm/loader.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ import { absoluteURL, resolve } from './utils.js';
33
import { parse } from './toml.js';
44
import { getJSON, getText } from './fetch-utils.js';
55

6+
export const getConfigURLAndType = config => {
7+
// REQUIRES INTEGRATION TEST
8+
/* c8 ignore start */
9+
let type = '';
10+
if (/\.(json|toml|txt)$/.test(config))
11+
type = RegExp.$1;
12+
else
13+
config = './config.txt';
14+
return [absoluteURL(config), type];
15+
/* c8 ignore stop */
16+
};
17+
618
/**
719
* Parse a generic config if it came from an attribute either as URL
820
* or as a serialized string. In XWorker case, accepts a pre-defined
@@ -17,21 +29,19 @@ export const getRuntime = (id, config, options = {}) => {
1729
if (config) {
1830
// REQUIRES INTEGRATION TEST
1931
/* c8 ignore start */
20-
if (config.endsWith('.json')) {
21-
options = fetch(config).then(getJSON);
22-
config = absoluteURL(config);
23-
} else if (config.endsWith('.toml')) {
24-
options = fetch(config).then(getText).then(parse);
25-
config = absoluteURL(config);
26-
} else if (!config.endsWith('.txt')) {
32+
const [absolute, type] = getConfigURLAndType(config);
33+
if (type === 'json') {
34+
options = fetch(absolute).then(getJSON);
35+
} else if (type === 'toml') {
36+
options = fetch(absolute).then(getText).then(parse);
37+
} else if (!type) {
2738
try {
2839
options = JSON.parse(config);
2940
} catch (_) {
3041
options = parse(config);
3142
}
32-
// make the config a URL to be able to retrieve relative paths from it
33-
config = absoluteURL('./config.txt');
3443
}
44+
config = absolute;
3545
/* c8 ignore stop */
3646
}
3747
return resolve(options).then(options => interpreter[id](options, config));

esm/worker/class.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as JSON from '@ungap/structured-clone/json';
22
import coincident from 'coincident/window';
33
import xworker from './xworker.js';
4-
import { assign, defineProperties, absoluteURL } from '../utils.js';
4+
import { getConfigURLAndType } from '../loader.js';
5+
import { assign, defineProperties } from '../utils.js';
56
import { getText } from '../fetch-utils.js';
67
import { Hook } from './hooks.js';
78

@@ -33,8 +34,7 @@ export default (...args) =>
3334
// provide a base url to fetch or load config files from a Worker
3435
// because there's no location at all in the Worker as it's embedded.
3536
// fallback to a generic, ignored, config.txt file to still provide a URL.
36-
const { config: c } = options;
37-
const config = absoluteURL(typeof c === 'string' ? c : './config.txt');
37+
const [ config ] = getConfigURLAndType(options.config);
3838

3939
const bootstrap = fetch(url)
4040
.then(getText)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@
7070
"coincident": "^0.11.6"
7171
},
7272
"worker": {
73-
"blob": "sha256-fXQ1VoH403DDslyuep/RYEL2i/xf/QhO9nPZ9neh3V8="
73+
"blob": "sha256-su+ZGyxquPydhkn09WaMjWjvNQcTNH1HlrJpOs5r7K0="
7474
}
7575
}

0 commit comments

Comments
 (0)