File I/O in PEX.
npm install pex-ioimport * as io from "pex-io";
try {
const text = await io.loadText("assets/hello.txt");
// => DOMString
const json = await io.loadJson("assets/color.json");
// => Object
const image = await io.loadImage("assets/pex.png");
// => HTMLImageElement
const blob = await io.loadBlob("assets/data");
// => Blob
const arrayBuffer = await io.loadArrayBuffer("assets/data.bin");
// => ArrayBuffer
const bytes = await io.loadBytes("assets/data.bin");
// => Uint8Array
} catch (error) {
console.log(error);
}- ImageOptions :
object - VideoOptions :
object - Resource :
object - LoadedResource :
string|object|HTMLImageElement|HTMLVideoElement|Blob|ArrayBuffer|Uint8Array|Error
- pex-io
- .loadText(url, [fetchOptions]) ⇒
Promise.<string> - .loadJson(url, [fetchOptions]) ⇒
Promise.<JSON> - .loadArrayBuffer(url, [fetchOptions]) ⇒
Promise.<ArrayBuffer> - .loadBytes(url, [fetchOptions]) ⇒
Promise.<Uint8Array> - .loadBlob(url, [fetchOptions]) ⇒
Promise.<Blob> - .loadImage(urlOrImageProperties, [fetchOptions]) ⇒
Promise.<HTMLImageElement> - .loadVideo(urlOrVideoProperties, [fetchOptions]) ⇒
Promise.<HTMLVideoElement> - .load(resources) ⇒
Promise.<Object.<string, LoadedResource>>
- .loadText(url, [fetchOptions]) ⇒
Load an item and parse the Response as text.
Kind: static method of pex-io
| Param | Type |
|---|---|
| url | RequestInfo | URL |
| [fetchOptions] | RequestInit |
Load an item and parse the Response as json.
Kind: static method of pex-io
| Param | Type |
|---|---|
| url | RequestInfo | URL |
| [fetchOptions] | RequestInit |
Load an item and parse the Response as arrayBuffer.
Kind: static method of pex-io
| Param | Type |
|---|---|
| url | RequestInfo | URL |
| [fetchOptions] | RequestInit |
Load an item and parse the Response as bytes.
Kind: static method of pex-io
| Param | Type |
|---|---|
| url | RequestInfo | URL |
| [fetchOptions] | RequestInit |
Load an item and parse the Response as blob.
Kind: static method of pex-io
| Param | Type |
|---|---|
| url | RequestInfo | URL |
| [fetchOptions] | RequestInit |
Create and load a HTML Image. If fetchOptions are specified, load and parse the Response as blob to set the "src" property.
Kind: static method of pex-io
| Param | Type |
|---|---|
| urlOrImageProperties | string | URL | ImageOptions |
| [fetchOptions] | RequestInit |
Create and load a HTML Video. If fetchOptions are specified, load and parse the Response as blob to set the "src" property.
Kind: static method of pex-io
| Param | Type |
|---|---|
| urlOrVideoProperties | string | URL | VideoOptions |
| [fetchOptions] | RequestInit |
Loads resources from a named map.
Kind: static method of pex-io
| Param | Type |
|---|---|
| resources | Object.<string, Resource> |
Example
const resources = {
hello: { text: "assets/hello.txt" },
data: { json: "assets/data.json" },
img: { image: "assets/tex.jpg" },
video: { video: "assets/video.mp4" },
blob: { blob: "assets/blob" },
hdrImg: {
arrayBuffer: "assets/tex.hdr",
options: { mode: "no-cors" },
},
bytes: { bytes: "assets/tex.hdr" },
};
const res = await io.load(resources);
res.hello; // => string
res.data; // => Object
res.img; // => HTMLImageElement
res.video; // => HTMLVideoElement
res.blob; // => Blob
res.hdrImg; // => ArrayBuffer
res.bytes; // => Uint8ArrayKind: global typedef Properties
| Name | Type | Description |
|---|---|---|
| url | string | URL |
|
| ...rest | any |
HTMLImageElement#properties |
Kind: global typedef Properties
| Name | Type | Default | Description |
|---|---|---|---|
| url | string | URL |
||
| [readyEvent] | string |
"'canplaythrough'" |
Event resolving the promise. "canplaythrough" might never fire depending on "preload" and platform policies (eg. iOS, data saver): use an earlier event like "loadedmetadata" or "canplay" instead. |
| ...rest | any |
HTMLVideoElement#properties |
Kind: global typedef Properties
| Name | Type | Description |
|---|---|---|
| [text] | string | URL |
|
| [json] | string | URL |
|
| [image] | string | URL | ImageOptions |
|
| [video] | string | URL | VideoOptions |
|
| [blob] | string | URL |
|
| [arrayBuffer] | string | URL |
|
| [bytes] | string | URL |
|
| [options] | RequestInit |
Request#parameters |
LoadedResource : string | object | HTMLImageElement | HTMLVideoElement | Blob | ArrayBuffer | Uint8Array | Error
Kind: global typedef
MIT. See license file.