Skip to content

HTTP

net.fetch(request) and net.download(request, path) work off the caller’s thread and are async. Each host your plugin talks to has to be in your permissions, and so does every host a redirect sends it to:

{
"permissions": { "net": ["example.com"] }
}

"*" allows any host. A host you didn’t list fails with NotDeclared.

A request has a url, a method, and optionally headers, a body for post, put and patch, max_bytes and timeout_ms. A request that hears nothing for timeout_ms, 30 seconds unless it says, fails with Timeout. It counts from the last bytes that arrived, so a big download on a slow connection still finishes. fetch settles with the status, headers and body for any status, so check it. It keeps up to 64 MiB unless max_bytes says otherwise, and fails with TooBig past that. It follows up to 5 redirects.

download streams the body to a file instead, and settles with its size. A relative path is in your plugin’s folder for this game. Anywhere outside your plugin’s folders needs "files": true too. The file is only replaced once the whole body is in, and a status that isn’t a success fails with Status. While it runs, net.downloading(path) says how many bytes are in, out of how many, for drawing progress.

import { Wraith } from "wraith";
const net = Wraith.net();
const got = await net.fetch({ url: "https://example.com/list.json", method: "get" });
if (got.status === 200) {
// got.body is the bytes
}
await net.download({ url: "https://example.com/big.gz", method: "get" }, "big.gz");
// Each frame, while it runs
const { received, total, active } = net.downloading("big.gz");

Made with ♥ by Asgard