Documentation
¶
Overview ¶
Package url gives a beastjs.VM the globals URL and URLSearchParams, as the URL standard (https://url.spec.whatwg.org/) defines them, with the parsing done in Go.
QuickJS has neither. A bundle that needs them, such as a router that resolves links with new URL(path, base) on every render, carries a polyfill, which costs 40 to 90 µs a URL on this engine. With Register the same call takes about 4 µs. A polyfill that tests for a native URL first, as core-js does, then does not install itself.
What a script can use is the whole of the standard: the constructor with and without a base, URL.canParse and URL.parse, every getter and setter of URL, searchParams linked to the URL both ways, toString and toJSON, and URLSearchParams with its constructor forms (a string, a sequence of pairs, a record), append, delete and has with an optional value, get, getAll, set, sort, forEach, the iterators, toString and size.
The parser is internal/whatwgurl, which follows the standard of 2026 and passes the web-platform-tests URL data (see docs/internals/changes/url.md for the list of cases it does not). Hosts are processed by IDNA with Unicode 17 tables.
Limits ¶
No string longer than 65,536 UTF-16 code units (its length in javascript) goes from a script into Go, and none comes back, so that what Go does for a call is over in a few milliseconds and its memory is known before it starts:
- new URL throws a TypeError, URL.parse is null and URL.canParse false for an input or a base longer than that, as given, before spaces are trimmed, and for one whose href would be longer than that (the href is the input percent-encoded, so 7,281 CJK characters in a path make one too long);
- a setter leaves the URL as it was when the value is longer than that or the href would be, and the href setter throws a TypeError;
- the host of a special scheme (http, https, ws, wss, ftp, file), which IDNA processes, is not a host when it is not ASCII and longer than 1,024 units after percent-decoding, or once IDNA maps it, or once it is normalized, and the host of another scheme, which IDNA does not process, has no limit but the href's;
- a name or a value of a query string longer than that, as written, when it has a + or a %, and one whose encoding would be longer than that, is a RangeError, of the constructor of URLSearchParams and of toString;
- append, delete, set and sort of the searchParams of a URL throw a RangeError, and change neither the list nor the URL, when the href would be longer than that.
Within these, the eval timeout and beastjs.VM.Interrupt end the script at the next call of Go, which looks at the stop flag as it starts, or at the engine's next poll, whichever comes first, and a call checks the room under the memory limit (beastjs.VM.SetMemoryLimit) for what Go may take before it reads a string, and ends with the out of memory error when it does not fit. The lists of URLSearchParams are split, sorted and walked in javascript, under the engine's own checks. A URL, and the list of a URLSearchParams, are javascript objects: they take their memory from the VM and give it back with the garbage collector. Go keeps nothing of them between calls, but for the last base each VM parsed and the last record it made, at most 3.5 KiB a VM, until the VM closes, and for the buffer in which package beastjs converts a long string, which holds the bytes of the last one it read, at most 192 KiB through this package, until the next one (see docs/internals/changes/url.md).
Differences from a browser ¶
URL.createObjectURL and URL.revokeObjectURL are not there, as the VM has no Blob. The messages of the TypeErrors are worded differently, though the errors are the same. A punycode label of more than 1024 characters is refused, as golang.org/x/net/idna refuses it.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Register ¶
Register installs URL and URLSearchParams on vm's global object. See the package documentation for what they do and what they do not.
Call it on every VM that is to have them, before any code of the page runs on vm: it takes then the built-ins its code uses, so that a page changing Array.prototype, Object.prototype, String.prototype and the like afterwards changes nothing URL does. A script that replaces the global URL, or a member of URL.prototype, replaces it for itself and nothing else.
Registering a second time on the same VM is an error, as it is for intl.Register. Nothing has to be released: vm.Close does it all. It costs one load of the script's bytecode (compiled once per process) and the making of the two classes.