Skip to content

Commit 61a0f74

Browse files
Merge pull request #82 from pyscript/better-fetch
Use a better fetch utility
2 parents 17220e0 + be03f61 commit 61a0f74

File tree

11 files changed

+133
-138
lines changed

11 files changed

+133
-138
lines changed

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/exports.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// this file simply exports enough stuff to allow
22
// 3rd party libraries, including PyScript, to work
3-
export * from './fetch-utils.js';
43
export * from './index.js';
54
export * from './script-handler.js';
65
export * from './utils.js';

esm/fetch-utils.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

esm/interpreter/_utils.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import '@ungap/with-resolvers';
2+
import fetch from '@webreflection/fetch';
23

3-
import { getBuffer } from '../fetch-utils.js';
44
import { absoluteURL, all, entries, importCSS, importJS, isArray, isCSS } from '../utils.js';
55

66
export const RUNNING_IN_WORKER = !globalThis.window;
@@ -94,17 +94,16 @@ const joinPaths = (parts) => {
9494
return parts[0].startsWith('/') ? `/${res}` : res;
9595
};
9696

97-
const fetchResolved = (config_fetch, url) =>
98-
fetch(absoluteURL(url, base.get(config_fetch)));
97+
const fetchBuffer = (config_fetch, url) =>
98+
fetch(absoluteURL(url, base.get(config_fetch))).arrayBuffer();
9999

100100
export const base = new WeakMap();
101101

102102
/* c8 ignore start */
103103
export const fetchPaths = (module, interpreter, config_fetch) =>
104104
all(
105105
calculateFetchPaths(config_fetch).map(({ url, path }) =>
106-
fetchResolved(config_fetch, url)
107-
.then(getBuffer)
106+
fetchBuffer(config_fetch, url)
108107
.then((buffer) => module.writeFile(interpreter, path, buffer)),
109108
),
110109
);
@@ -146,8 +145,7 @@ const calculateFilesPaths = files => {
146145
export const fetchFiles = (module, interpreter, config_files) =>
147146
all(
148147
calculateFilesPaths(config_files).map(({ url, path }) =>
149-
fetchResolved(config_files, url)
150-
.then(getBuffer)
148+
fetchBuffer(config_files, url)
151149
.then((buffer) => module.writeFile(interpreter, path, buffer)),
152150
),
153151
);

esm/interpreter/ruby-wasm-wasi.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import fetch from '@webreflection/fetch';
2+
13
import { dedent } from '../utils.js';
24
import { fetchFiles, fetchJSModules, fetchPaths } from './_utils.js';
35

@@ -17,10 +19,9 @@ export default {
1719
module: (version = '2.2.0') =>
1820
`https://cdn.jsdelivr.net/npm/ruby-3_2-wasm-wasi@${version}/dist/browser.esm.js`,
1921
async engine({ DefaultRubyVM }, config, url) {
20-
const response = await fetch(
21-
`${url.slice(0, url.lastIndexOf('/'))}/ruby.wasm`,
22-
);
23-
const module = await WebAssembly.compile(await response.arrayBuffer());
22+
url = `${url.slice(0, url.lastIndexOf('/'))}/ruby.wasm`;
23+
const buffer = await fetch(url).arrayBuffer();
24+
const module = await WebAssembly.compile(buffer);
2425
const { vm: interpreter } = await DefaultRubyVM(module);
2526
if (config.files) await fetchFiles(this, interpreter, config.files);
2627
if (config.fetch) await fetchPaths(this, interpreter, config.fetch);

esm/loader.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import fetch from '@webreflection/fetch';
2+
13
import { interpreter } from './interpreters.js';
24
import { absoluteURL, resolve } from './utils.js';
35
import { parse } from './toml.js';
4-
import { getJSON, getText } from './fetch-utils.js';
56

67
// REQUIRES INTEGRATION TEST
78
/* c8 ignore start */
@@ -40,9 +41,9 @@ export const getRuntime = (id, config, configURL, options = {}) => {
4041
/* c8 ignore start */
4142
const [absolute, type] = getConfigURLAndType(config, configURL);
4243
if (type === 'json') {
43-
options = fetch(absolute).then(getJSON);
44+
options = fetch(absolute).json();
4445
} else if (type === 'toml') {
45-
options = fetch(absolute).then(getText).then(parse);
46+
options = fetch(absolute).text().then(parse);
4647
} else if (type === 'string') {
4748
options = parseString(config);
4849
} else if (type === 'object' && config) {

esm/script-handler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import fetch from '@webreflection/fetch';
12
import { $ } from 'basic-devtools';
23

34
import $xworker from './worker/class.js';
45
import workerURL from './worker/url.js';
56
import { getRuntime, getRuntimeID } from './loader.js';
67
import { registry } from './interpreters.js';
78
import { JSModules, all, dispatch, resolve, defineProperty, nodeInfo, registerJSModules } from './utils.js';
8-
import { getText } from './fetch-utils.js';
99

1010
const getRoot = (script) => {
1111
let parent = script;
@@ -161,7 +161,7 @@ export const handle = async (script) => {
161161
if (targetValue) targets.set(script, queryTarget(script, targetValue));
162162

163163
// start fetching external resources ASAP
164-
const source = src ? fetch(src).then(getText) : script.textContent;
164+
const source = src ? fetch(src).text() : script.textContent;
165165
details.queue = details.queue.then(() =>
166166
execute(script, source, details.XWorker, !!isAsync),
167167
);

esm/worker/class.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as JSON from '@ungap/structured-clone/json';
2+
import fetch from '@webreflection/fetch';
23
import coincident from 'coincident/window';
34
import xworker from './xworker.js';
45
import { getConfigURLAndType } from '../loader.js';
56
import { assign, create, defineProperties, importCSS, importJS } from '../utils.js';
6-
import { getText } from '../fetch-utils.js';
77
import Hook from './hook.js';
88

99
/**
@@ -38,7 +38,7 @@ export default (...args) =>
3838
const [ config ] = getConfigURLAndType(options.config, options.configURL);
3939

4040
const bootstrap = fetch(url)
41-
.then(getText)
41+
.text()
4242
.then(code => {
4343
const hooks = isHook ? this.toJSON() : void 0;
4444
postMessage.call(worker, { options, config, code, hooks });

0 commit comments

Comments
 (0)