Skip to content

URLConstructor

[<Interface>]
type URLConstructor =
    abstract member Create (input: U2<string,Input>, ?base: U2<string,URL>) : URL
    abstract member canParse (input: string, ?base: string) : bool
    abstract member createObjectURL (blob: _Blob) : string
    abstract member parse (input: string, ?base: string) : URL option
    abstract member revokeObjectURL (id: string) : unit
AssemblyXantham.Fable.Node

Methods

abstract member Create (input: U2<string,Input>, ?base: U2<string,URL>) : URL
abstract member canParse (input: string, ?base: string) : bool

Checks if an input relative to the base can be parsed to a URL.

const isValid = URL.canParse('/foo', 'https://example.org/'); // true

const isNotValid = URL.canParse('/foo'); // false

@since v19.9.0

Parameter
inputThe absolute or relative input URL to parse. If input is relative, then base is required. If input is absolute, the base is ignored. If input is not a string, it is converted to a string first.
baseThe base URL to resolve against if the input is not absolute. If base is not a string, it is converted to a string first.
abstract member createObjectURL (blob: _Blob) : string

Creates a 'blob:nodedata:...' URL string that represents the given Blob object and can be used to retrieve the Blob later.

import {
Blob,
resolveObjectURL,
} from 'node:buffer';

const blob = new Blob(['hello']);
const id = URL.createObjectURL(blob);

// later...

const otherBlob = resolveObjectURL(id);
console.log(otherBlob.size);

The data stored by the registered Blob will be retained in memory until URL.revokeObjectURL() is called to remove it.

Blob objects are registered within the current thread. If using Worker Threads, Blob objects registered within one Worker will not be available to other workers or the main thread.

@since v16.7.0

abstract member parse (input: string, ?base: string) : URL option

Parses a string as a URL. If base is provided, it will be used as the base URL for the purpose of resolving non-absolute input URLs. Returns null if the parameters can't be resolved to a valid URL.

@since v22.1.0

Parameter
inputThe absolute or relative input URL to parse. If input is relative, then base is required. If input is absolute, the base is ignored. If input is not a string, it is converted to a string first.
baseThe base URL to resolve against if the input is not absolute. If base is not a string, it is converted to a string first.
abstract member revokeObjectURL (id: string) : unit

Removes the stored Blob identified by the given ID. Attempting to revoke a ID that isn't registered will silently fail.

@since v16.7.0

Parameter
idA 'blob:nodedata:... URL string returned by a prior call to URL.createObjectURL().