The fetch() API brought to AssemblyScript
npm install as-fetchUndici-Fetch Provides much faster http requests with Undici.
npm install undici-fetchAdd the --exportTable flag
CommonJS
+ const FetchImport = require("as-fetch/imports")
+ const Fetch = new FetchImport()
const imports = {
+ ...Fetch.wasmImports,
}
const wasmModule = loader.instantiateSync(...)
+ Fetch.wasmExports = wasmModule.exportsESM/Browser
+ import { FetchImport } = from "as-fetch/imports.esm.js"
+ const Fetch = new FetchImport()
const imports = {
+ ...Fetch.wasmImports,
}
const wasmModule = loader.instantiateSync(...)
+ Fetch.wasmExports = wasmModule.exportsGET Request
import { fetch } from "as-fetch";
fetch("https://example.com/get", {
method: "GET",
mode: "no-cors",
headers: [],
}).then((response) => {
const text = response.text();
console.log("Response: " + text);
});POST Request
import { fetch } from "as-fetch";
fetch("https://example.com/post", {
method: "POST",
mode: "no-cors",
headers: [["content-type", "text/plain"]],
body: String.UTF8.encode("Hello World"),
}).then((response) => {
const text = response.text();
console.log("Response: " + text);
});Note: Performance of as-fetch depends on the backend. 😉
100 Requests
Fetch: 847ms -- Undici-Fetch: 21ms -- Node-Fetch: 97ms
1,000 Requests
Fetch: 5,255ms -- Undici-Fetch: 186ms -- Node-Fetch: 657ms
10,000 Requests
Fetch: 46,897ms -- Undici-Fetch: 904ms -- Node-Fetch: 4,795ms
Thank you, Aaron Turner for creating the main implementation of the API!