-
Notifications
You must be signed in to change notification settings - Fork 5
ZeroFS
ZeroFS constructor(ZeroPage page)
Creates a new ZeroFS instance from ZeroPage object.
Promise<String|Uint8Array> readFile(String file, "arraybuffer"|Boolean bytes=false
, Boolean required=false
)
Reads the file specified by the
file
argument. If it doesn't exist, rejectsString(`File doesn't exist: ${file}`)
. Ifrequired
istrue
, ZeroNet will try to find the file for 5 minutes, and if it still doesn't find it, reject.If the file was found, the result depends on
bytes
.
false
-> the file is decoded by UTF8 and the resulting string is returnedtrue
-> the file is returned as byte string- "arraybuffer" -> a Uint8Array object is returned
let data; try { data = JSON.parse(await zeroFS.readFile(`data/users/${address}/data.json`)); } catch(e) { data = {tableName: []}; }
Promise<String> writeFile(String file, String|Uint8Array content, "arraybuffer"|Boolean bytes=false
)
The
content
field is checked by the same rules as in readFile method (depending onbytes
). Returns filename in case the write was successful, rejects error object otherwise.await zeroFS.writeFile(`data/users/${address}/data.json`, JSON.stringify(data));
Promise<String> deleteFile(String file)
Tries to delete the file passed. If the delete was successful, returns filename, otherwise rejects error object.
await zeroFS.deleteFile(`data/users/${address}/temp.txt`);
Promise<String|Uint8Array> peekFile(String file, Number offset, Number length, "arraybuffer"|Boolean bytes)
Uses AJAX to read the part of the file specified by
offset
andlength
.offset
is counted from zero.bytes
has the same meaning as in readFile. In case the read isn't successful (i.e. the return code isn't 206), rejectsNumber(status)
(e.g.Number(404)
).
Promise<Array<String>> readDirectory(String dir, Boolean recursive=false
)
If the
recursive
argument istrue
, returns the full list of files insidedir
. If therecursive
argument isfalse
, file and directory list is returned. Empty directories are not returned.The paths exclude
dir
prefix. The result is sorted by filenames.for(const fileName of await zeroFS.readDirectory("data/users")) { if(fileName !== "content.json") { console.log("New user found:", fileName); } }
Promise<"file"|"dir"> getType(String file)
Returns
"file"
iffile
is a file,"dir"
for directories (including site root), and rejectsString(`File doesn't exist: ${file}`)
in case the file doesn't exist.if((await zeroFS.getType("data/users")) !== "dir") { console.log("Invalid data/users format"); }
Promise<Boolean> fileExists(String file)
Returns whether the file (or directory) you pass exists. Calls
fileList
command, so it works fast for big files, but slow if you've got a lot of small files in the parent directory of the file. In case you're checking for existence ofdata/users/...
, readingdata/users/.../content.json
and catching the exception will probably be faster.if(!await zeroFS.fileExists(`data/users/${address}/content.json`)) { openRegisterDialog(); }