Skip to content

Commit 933c0ed

Browse files
committed
fix: fetch polyfill availability
Signed-off-by: Sam Gammon <sam@elide.dev>
1 parent 4a5880c commit 933c0ed

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

elide/runtime/js/polyfills/fetch.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
/**
99
* Experimental fetch API shim backed by Java 11 HttpClient.
1010
*/
11-
12-
(class Fetch {
11+
class Fetch {
1312
#private;
1413

1514
static {
@@ -26,6 +25,12 @@
2625
const ByteBuffer = Java.type('java.nio.ByteBuffer');
2726
const Base64 = Java.type('java.util.Base64');
2827
const StandardCharsets = Java.type('java.nio.charset.StandardCharsets');
28+
const specialHttp2Headers = new Set();
29+
specialHttp2Headers.add(':authority');
30+
specialHttp2Headers.add(':method');
31+
specialHttp2Headers.add(':path');
32+
specialHttp2Headers.add(':scheme');
33+
specialHttp2Headers.add(':status');
2934

3035
function parseAndValidateURL(url) {
3136
try {
@@ -41,15 +46,13 @@
4146
throw new TypeError("Invalid URL: " + e.message);
4247
}
4348
}
44-
4549
function validateHeaderName(name) {
4650
const validHttpToken = /^[\^_`a-zA-Z\-0-9!#$%&'*+.|~]+$/;
47-
if (!validHttpToken.test(name)) {
51+
if (!specialHttp2Headers.has(name) && !validHttpToken.test(name)) {
4852
throw new TypeError(`Header name must be a valid HTTP token: [${name}]`);
4953
}
5054
return name;
5155
}
52-
5356
function validateHeaderValue(name, value) {
5457
const invalidFieldValueChar = /[^\t\x20-\x7e\x80-\xff]/;
5558
if (invalidFieldValueChar.exec(value) !== null) {
@@ -63,15 +66,13 @@
6366
function defineToStringTag(constructor) {
6467
Object.defineProperty(constructor.prototype, Symbol.toStringTag, {value: constructor.name, configurable: true, writable: true, enumerable: false});
6568
}
66-
6769
// Wraps a byte[] to facilitate type checking in Blob constructor.
6870
class ByteArrayWrapper extends Fetch {
6971
constructor(byteArray) {
7072
super();
7173
this.#private = byteArray;
7274
}
7375
}
74-
7576
class Blob {
7677
#size = 0;
7778
#byteArray;
@@ -148,7 +149,6 @@
148149
function asBlob(body) {
149150
return body instanceof Blob ? body : new Blob(body);
150151
}
151-
152152
function fillHeaders(headers, init) {
153153
if (init instanceof Headers) {
154154
init = init.entries();
@@ -284,7 +284,6 @@
284284
return method;
285285
}
286286
}
287-
288287
function makeRequest(init, uri, url, source) {
289288
// https://fetch.spec.whatwg.org/#requests
290289
let referrer = init.referrer;
@@ -722,7 +721,7 @@
722721
httpRequestBuilder.method(request.method, bodyPublisher);
723722
httpRequestBuilder.setHeader("Accept", "*/*");
724723
httpRequestBuilder.setHeader("Accept-Encoding", ""); // compression not supported
725-
httpRequestBuilder.setHeader("User-Agent", "graaljs-fetch");
724+
httpRequestBuilder.setHeader("User-Agent", `${navigator.userAgent}/graaljs-fetch/Node.js`);
726725
if (requestPrivate.referrer !== 'client') {
727726
httpRequestBuilder.setHeader("Referer", requestPrivate.referrer); // [sic]
728727
}
@@ -921,10 +920,11 @@
921920
default: return "";
922921
};
923922
}
924-
925923
Object.entries({"fetch": fetch, "Headers": Headers, "Request": Request, "Response": Response}).forEach((entry) => {
926924
const [name, fn] = entry;
927925
Object.defineProperty(globalThis, name, {value: fn, configurable: true, writable: true, enumerable: false});
928926
});
929927
}
930-
});
928+
};
929+
930+
globalThis['Fetch'] = Fetch;

0 commit comments

Comments
 (0)