| 63 | * *This example has the folder `/home/jvilk` with subfile `someFile.txt` and subfolder `someDir`.* |
| 64 | */ |
| 65 | export default class XmlHttpRequest extends BaseFileSystem implements FileSystem { |
| 66 | public static readonly Name = "XmlHttpRequest"; |
| 67 | |
| 68 | public static readonly Options: FileSystemOptions = { |
| 69 | index: { |
| 70 | type: ["string", "object"], |
| 71 | optional: true, |
| 72 | description: "URL to a file index as a JSON file or the file index object itself, generated with the make_xhrfs_index script. Defaults to `index.json`." |
| 73 | }, |
| 74 | baseUrl: { |
| 75 | type: "string", |
| 76 | optional: true, |
| 77 | description: "Used as the URL prefix for fetched files. Default: Fetch files relative to the index." |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | /** |
| 82 | * Construct an XmlHttpRequest file system backend with the given options. |
| 83 | */ |
| 84 | public static Create(opts: XmlHttpRequestOptions, cb: BFSCallback<XmlHttpRequest>): void { |
| 85 | if (opts.index === undefined) { |
| 86 | opts.index = `index.json`; |
| 87 | } |
| 88 | if (typeof(opts.index) === "string") { |
| 89 | XmlHttpRequest.FromURL(opts.index, cb, opts.baseUrl, false); |
| 90 | } else { |
| 91 | cb(null, new XmlHttpRequest(opts.index, opts.baseUrl, false)); |
| 92 | } |
| 93 | } |
| 94 | public static isAvailable(): boolean { |
| 95 | return typeof(XMLHttpRequest) !== "undefined" && XMLHttpRequest !== null; |
| 96 | } |
| 97 | /** |
| 98 | * **Deprecated. Please use XmlHttpRequest.Create() method instead to construct XmlHttpRequest objects.** |
| 99 | * |
| 100 | * Constructs an XmlHttpRequest object using the directory listing at the given URL. |
| 101 | * Uses the base URL as the URL prefix for fetched files. |
| 102 | * @param cb Called when the file system has been instantiated, or if an error occurs. |
| 103 | */ |
| 104 | public static FromURL(url: string, cb: BFSCallback<XmlHttpRequest>, baseUrl = url.slice(0, url.lastIndexOf('/') + 1), deprecateMsg = true): void { |
| 105 | if (deprecateMsg) { |
| 106 | console.warn(`[XmlHttpRequest] XmlHttpRequest.FromURL() is deprecated and will be removed in the next major release. Please use 'XmlHttpRequest.Create({ index: "${url}", baseUrl: "${baseUrl}" }, cb)' instead.`); |
| 107 | } |
| 108 | asyncDownloadFile(url, "json", (e, data?) => { |
| 109 | if (e) { |
| 110 | cb(e); |
| 111 | } else { |
| 112 | cb(null, new XmlHttpRequest(data, baseUrl, false)); |
| 113 | } |
| 114 | }); |
| 115 | } |
| 116 | |
| 117 | public readonly prefixUrl: string; |
| 118 | private _index: FileIndex<{}>; |
| 119 | /** |
| 120 | * **Deprecated. Please use XmlHttpRequest.Create() method instead to construct XmlHttpRequest objects.** |
| 121 | * |
| 122 | * Constructs the file system. You must provide the directory listing as a JSON object |
nothing calls this directly
no outgoing calls
no test coverage detected