Skip to content

Commit c32d3ef

Browse files
committed
Add Base URL field to decoder-ring to make it more flexible
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
1 parent 2419db5 commit c32d3ef

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

res/decoder-ring/decoder.js

+29-12
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ class StartupError extends Error {}
44
* We need to know the bundle path before we can fetch the sourcemap files. In a production environment, we can guess
55
* it using this.
66
*/
7-
async function getBundleName() {
8-
const res = await fetch("../index.html");
7+
async function getBundleName(baseUrl) {
8+
const res = await fetch(new URL("index.html", baseUrl).toString());
99
if (!res.ok) {
1010
throw new StartupError(`Couldn't fetch index.html to prefill bundle; ${res.status} ${res.statusText}`);
1111
}
@@ -25,15 +25,15 @@ function validateBundle(value) {
2525
* The purpose of this is just to validate that the user entered a real bundle, and provide feedback.
2626
*/
2727
const bundleCache = new Map();
28-
function bundleSubject(bundle) {
28+
function bundleSubject(baseUrl, bundle) {
2929
if (!bundle.match(/^[0-9a-f]{20}$/)) throw new Error("Bad input");
3030
if (bundleCache.has(bundle)) {
3131
return bundleCache.get(bundle);
3232
}
3333
const fetcher = new rxjs.BehaviorSubject(Pending.of());
3434
bundleCache.set(bundle, fetcher);
3535

36-
fetch(`../bundles/${bundle}/bundle.js.map`).then((res) => {
36+
fetch(new URL(`bundles/${bundle}/bundle.js.map`, baseUrl).toString()).then((res) => {
3737
res.body.cancel(); /* Bail on the download immediately - it could be big! */
3838
const status = res.ok;
3939
if (status) {
@@ -145,6 +145,7 @@ function ProgressBar({ fetchStatus }) {
145145
* The main component.
146146
*/
147147
function BundlePicker() {
148+
const [baseUrl, setBaseUrl] = React.useState(new URL("..", window.location).toString());
148149
const [bundle, setBundle] = React.useState("");
149150
const [file, setFile] = React.useState("");
150151
const [line, setLine] = React.useState("1");
@@ -153,19 +154,25 @@ function BundlePicker() {
153154
const [bundleFetchStatus, setBundleFetchStatus] = React.useState(None);
154155
const [fileFetchStatus, setFileFetchStatus] = React.useState(None);
155156

156-
/* At startup, try to fill in the bundle name for the user */
157+
/* On baseUrl change, try to fill in the bundle name for the user */
157158
React.useEffect(() => {
158-
getBundleName().then((name) => {
159+
console.log("DEBUG", baseUrl);
160+
getBundleName(baseUrl).then((name) => {
161+
console.log("DEBUG", name);
159162
if (bundle === "" && validateBundle(name) !== None) {
160163
setBundle(name);
161164
}
162165
}, console.log.bind(console));
163-
}, []);
164-
166+
}, [baseUrl]);
165167

166168
/* ------------------------- */
167169
/* Follow user state changes */
168170
/* ------------------------- */
171+
const onBaseUrlChange = React.useCallback((event) => {
172+
const value = event.target.value;
173+
setBaseUrl(value);
174+
}, []);
175+
169176
const onBundleChange = React.useCallback((event) => {
170177
const value = event.target.value;
171178
setBundle(value);
@@ -195,14 +202,14 @@ function BundlePicker() {
195202
React.useEffect(() =>
196203
validateBundle(bundle).fold({
197204
some: (value) => {
198-
const subscription = bundleSubject(value)
205+
const subscription = bundleSubject(baseUrl, value)
199206
.pipe(rxjs.operators.map(Some.of))
200207
.subscribe(setBundleFetchStatus);
201208
return () => subscription.unsubscribe();
202209
},
203210
none: () => setBundleFetchStatus(None),
204211
}),
205-
[bundle]);
212+
[baseUrl, bundle]);
206213

207214
/* Whenever a valid javascript file is input, see if it corresponds to a sourcemap file and initiate a fetch
208215
* if so. */
@@ -211,7 +218,7 @@ function BundlePicker() {
211218
setFileFetchStatus(None);
212219
return;
213220
}
214-
const observable = fetchAsSubject(`../bundles/${bundle}/${file}.map`)
221+
const observable = fetchAsSubject(new URL(`bundles/${bundle}/${file}.map`, baseUrl).toString())
215222
.pipe(
216223
rxjs.operators.map((fetchStatus) => fetchStatus.flatMap(value => {
217224
try {
@@ -224,7 +231,7 @@ function BundlePicker() {
224231
);
225232
const subscription = observable.subscribe(setFileFetchStatus);
226233
return () => subscription.unsubscribe();
227-
}, [bundle, file]);
234+
}, [baseUrl, bundle, file]);
228235

229236
/*
230237
* Whenever we have a valid fetched sourcemap, and a valid line, attempt to find the original position from the
@@ -255,6 +262,16 @@ function BundlePicker() {
255262
/* ------ */
256263
return e('div', {},
257264
e('div', { className: 'inputs' },
265+
e('div', { className: 'baseUrl' },
266+
e('label', { htmlFor: 'baseUrl'}, 'Base URL'),
267+
e('input', {
268+
name: 'baseUrl',
269+
required: true,
270+
pattern: ".+",
271+
onChange: onBaseUrlChange,
272+
value: baseUrl,
273+
}),
274+
),
258275
e('div', { className: 'bundle' },
259276
e('label', { htmlFor: 'bundle'}, 'Bundle'),
260277
e('input', {

0 commit comments

Comments
 (0)