Skip to content

Removed usage of an easier fetch due bad error reporting #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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 docs/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions docs/zip-PO8erqJO.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/zip-PO8erqJO.js.map

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions esm/interpreter/_utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import fetch from '@webreflection/fetch';

import { absoluteURL, all, entries, importCSS, importJS, isArray, isCSS } from '../utils.js';

export const RUNNING_IN_WORKER = !globalThis.window;
Expand Down Expand Up @@ -91,8 +89,10 @@ const joinPaths = (parts) => {
return parts[0].startsWith('/') ? `/${res}` : res;
};

const fetchBuffer = (url, baseURL) =>
fetch(absoluteURL(url, baseURL)).arrayBuffer();
const fetchBuffer = (url, baseURL) => {
const absolute = absoluteURL(url, baseURL);
return fetch(absolute).then(r => r.ok ? r.arrayBuffer() : Promise.reject(new Error(`Unable to fetch ${absolute}`)));
};

export const fetchPaths = (module, interpreter, config_fetch, baseURL) =>
all(
Expand Down
4 changes: 1 addition & 3 deletions esm/interpreter/micropython.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import fetch from '@webreflection/fetch';

import { createProgress, writeFile } from './_utils.js';
import { getFormat, loader, loadProgress, registerJSModule, run, runAsync, runEvent } from './_python.js';
import { stdio, buffered } from './_io.js';
Expand Down Expand Up @@ -122,7 +120,7 @@ async function importPackages(interpreter, baseURL, packages) {
for (const mpyPackage of packages) {
if (mpyPackage.endsWith('.whl')) {
const url = absoluteURL(mpyPackage, baseURL);
const buffer = await fetch(url).arrayBuffer();
const buffer = await fetch(url).then(r => r.ok ? r.arrayBuffer() : Promise.reject(new Error(`Unable to fetch ${url}`)));
await this.writeFile(interpreter, './*', buffer, url);
}
else {
Expand Down
4 changes: 1 addition & 3 deletions esm/interpreter/ruby-wasm-wasi.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import fetch from '@webreflection/fetch';

import { dedent } from '../utils.js';
import { fetchFiles, fetchJSModules, fetchPaths } from './_utils.js';

Expand All @@ -20,7 +18,7 @@ export default {
`https://cdn.jsdelivr.net/npm/@ruby/3.2-wasm-wasi@${version}/dist/browser/+esm`,
async engine({ DefaultRubyVM }, config, url, baseURL) {
url = url.replace(/\/browser\/\+esm$/, '/ruby.wasm');
const buffer = await fetch(url).arrayBuffer();
const buffer = await fetch(url).then(r => r.ok ? r.arrayBuffer() : Promise.reject(new Error(`Unable to fetch ${url}`)));
const module = await WebAssembly.compile(buffer);
const { vm: interpreter } = await DefaultRubyVM(module);
if (config.files) await fetchFiles(this, interpreter, config.files, baseURL);
Expand Down
20 changes: 14 additions & 6 deletions esm/loader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import fetch from '@webreflection/fetch';

import { interpreter } from './interpreters.js';
import { absoluteURL, resolve } from './utils.js';
import { toml } from './3rd-party.js';
Expand All @@ -17,12 +15,23 @@ export const getConfigURLAndType = (config, configURL = './config.txt') => {
return [absoluteURL(config), type];
};

const onFetchError = absolute => () => {
throw new Error(`Unable to fetch cofig ${absolute}`);
};

export const resolveConfig = (config, configURL, options = {}) => {
const [absolute, type] = getConfigURLAndType(config, configURL);
const onError = onFetchError(absolute);
if (type === 'json') {
options = fetch(absolute).json();
options = fetch(absolute).then(
r => r.ok ? r.json() : onError(),
onError
);
} else if (type === 'toml') {
options = fetch(absolute).text().then(toml);
options = fetch(absolute).then(
r => r.ok ? r.text().then(toml) : onError(),
onError
);
} else if (type === 'string') {
options = parseString(config);
} else if (type === 'object' && config) {
Expand All @@ -38,8 +47,7 @@ const parseString = config => {
try {
return parse(config);
}
// eslint-disable-next-line no-unused-vars
catch (_) {
catch {
return toml(config);
}
};
Expand Down
3 changes: 1 addition & 2 deletions esm/script-handler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fetch from '@webreflection/fetch';
import { $ } from 'basic-devtools';

import IDBMap from '@webreflection/idb-map';
Expand Down Expand Up @@ -181,7 +180,7 @@ export const handle = async (script) => {
if (targetValue) targets.set(script, queryTarget(script, targetValue));

// start fetching external resources ASAP
const source = src ? fetch(src).text() : script.textContent;
const source = src ? fetch(src).then(r => r.ok ? r.text() : Promise.reject(new Error(`Unable to fetch ${src}`))) : script.textContent;
details.queue = details.queue.then(() =>
execute(script, source, details.XWorker, isAsync),
);
Expand Down
18 changes: 9 additions & 9 deletions esm/worker/class.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import withResolvers from '@webreflection/utils/with-resolvers';
import fetch from '@webreflection/fetch';
import xworker from './xworker.js';
import { getConfigURLAndType } from '../loader.js';
import { assign, create, defineProperties, importCSS, importJS } from '../utils.js';
Expand Down Expand Up @@ -51,16 +50,17 @@ export default (...args) =>

const resolver = withResolvers();

let bootstrap = fetch(url)
.text()
.then(code => {
let bootstrap = fetch(url).then(
async r => {
if (!r.ok) throw new Error(`Unable to fetch ${url}`);
const code = await r.text();
const hooks = isHook ? this.toJSON() : void 0;
postMessage.call(worker, { options, config, code, hooks });
})
.then(() => {
// boost postMessage performance
bootstrap = { then: fn => fn() };
});
},
).then(() => {
// boost postMessage performance
bootstrap = { then: fn => fn() };
});

defineProperties(worker, {
sync: { value: sync },
Expand Down
Loading