Quark #

Modules #

Quark v1.4.0 Documentation

Contents

quark/fs #

Enum: FileOpenFlag #

FileOpenFlag #

Opening file Flags

  • FOPEN_ACCMODE
  • FOPEN_RDONLY
    Open a read-only file. The file must exist.
  • FOPEN_WRONLY
    Open a write-only file. The file must exist.
  • FOPEN_RDWR
    Open a file for reading and writing. The file must exist.
  • FOPEN_CREAT
    Create the file if it does not exist
  • FOPEN_EXCL
    Create a file as executable
  • FOPEN_NOCTTY
    No Controlling TTY
  • FOPEN_TRUNC
    Clear the file contents if the file exists
  • FOPEN_APPEND
    Move the file read cursor to the end if the file exists
  • FOPEN_NONBLOCK
    Blocking mode, usually using the open function
  • FOPEN_R
    r, open the file for reading only, the file must exist
  • FOPEN_W
    w, Open a write-only file. If the file exists, the file length is cleared to zero, that is, the file content will disappear. If the file does not exist, it will be created.
  • FOPEN_A
    a, Open a write-only file in append mode. If the file does not exist, it will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained.
  • FOPEN_RP
    r+, Open a file for reading and writing. The file must exist.
  • FOPEN_WP
    w+, Open the file for reading and writing. If the file exists, the file length will be cleared to zero, that is, the file content will disappear. If the file does not exist, it will be created.
  • FOPEN_AP
    a+, Open a readable and writable file in append mode. If the file does not exist, it will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained.

Enum: FileType #

FileType #

File type

  • FTYPE_UNKNOWN
    Unknown Type
  • FTYPE_FILE
    Normal File
  • FTYPE_DIR
    Directory)
  • FTYPE_LINK
    Symbolic link files(Soft Link)
  • FTYPE_FIFO
    Named pipes, a special type of file used for interprocess communication (IPC)
  • FTYPE_SOCKET
    Socket
  • FTYPE_CHAR
    Unbuffered character devices, such as keyboard and mouse (/dev/input/), terminal (/dev/tty)
  • FTYPE_BLOCK
    Provides buffered, block-oriented (fixed-size data blocks) I/O access, such as hard disk drives (/dev/sda, /dev/sda1, /dev/nvme0n1p1)

defaultMode #

Create and set the default mode value of the file, which is related to the file's permissions. This is an int integer type value

const defaultMode

StreamResponseCallback #

  • @callback StreamResponseCallback(stream)
  • @param stream: {StreamResponse}

Interface: Dirent #

Dirent #

The result returned by calling readdir/readdirSync

dirent.name #

File name

name: string

dirent.pathname #

The full path to the file

pathname: string

dirent.type #

File Type

type: FileType

Class: FileStat #

FileStat #

filestat.isValid() #

Is the file valid?

isValid(): boolean

filestat.isFile() #

Is it a normal file?

isFile(): boolean

filestat.isDir() #

Is it a directory?

isDir(): boolean

Is it a symbolic link?

isLink(): boolean

filestat.isSock() #

Is it a socket?

isSock(): boolean

filestat.mode() #

File permission mask

mode(): number

filestat.type() #

File type

type(): FileType

filestat.group() #

System Group ID

group(): number

filestat.owner() #

System User ID

owner(): number

filestat.size() #

File size

size(): number

Number of hard links to the file

nlink(): number

filestat.ino() #

The file system specific "Inode" number of the file

ino(): number

filestat.blksize() #

The file system block size used for I/O operations

blksize(): number

filestat.blocks() #

The number of blocks allocated for this file

blocks(): number

filestat.flags() #

flags

flags(): number

filestat.gen() #

gen

gen(): number

filestat.dev() #

The device ID that contains the file

dev(): number

filestat.rdev() #

If the file represents a device, the numeric device identifier

rdev(): number

filestat.atime() #

Timestamp indicating the last time this file was accessed

atime(): number

filestat.mtime() #

A timestamp indicating when this file was last modified

mtime(): number

filestat.ctime() #

Timestamp indicating the last time the file's status changed

ctime(): number

filestat.birthtime() #

A timestamp indicating when this file was created

birthtime(): number

Interface: Stream #

Stream #

stream.pause() #

Pause stream reading

pause(): void

stream.resume() #

Resume stream reading

resume(): void

Interface: StreamResponse #

StreamResponse #

streamresponse.size #

Data size

size: number

streamresponse.total #

Total data size

total: number

streamresponse.data #

It is Data

data: Uint8Array

streamresponse.ended #

Is it over

ended: boolean

Class: AsyncTask #

AsyncTask #

Extends: Promise Type parameters: T

asynctask.id #

Asynchronous I/O task id

readonly id: 

asynctask.complete #

Whether the asynchronous task is completed

readonly complete: 

asynctask.abort(reason?) #

Abort I/O task

abort(reason?: Error): void

Parameters:

  • reason? — If the parameter is passed in, an exception will be thrown

chmodSync(path,mode?) #

Synchronously change file permissions

chmodSync('my_file.txt', 0o775)
chmodSync(path: string, mode?: Uint): void

chownSync(path,owner,group) #

Synchronize the owner and group of the configuration file

chownSync(path: string, owner: Uint, group: Uint): void

mkdirSync(path,mode?) #

Create a directory synchronously. If the path already exists, an exception will be thrown.

mkdirSync(mode?: Uint, path: string): void

Parameters:

mkdirsSync(path,mode?) #

Synchronously recursively create directories, and no exception is thrown if the directory exists

mkdirsSync(mode?: Uint, path: string): void

Parameters:

renameSync(name,newName) #

Synchronously rename files and directories

renameSync(name: string, newName: string): void

linkSync(src,target) #

Synchronously create file hard links

linkSync(src: string, target: string): void

Parameters:

  • src — Original file path
  • target — Link target path

unlinkSync(path) #

Synchronously delete the file hard link. If the file has only one link, the file will be physically deleted.

unlinkSync(path: string): void

rmdirSync(path) #

Synchronously delete the file directory, the file directory must be empty

rmdirSync(path: string): void

readdirSync(path) #

Synchronously read the file directory file list

readdirSync(path: string): Dirent[]

statSync(path) #

Synchronously read file status information

statSync(path: string): FileStat

existsSync(path) #

Synchronously check if a file exists

existsSync(path: string): boolean

isFileSync(path) #

Synchronously checks if the path is a file, and returns false if the path does not exist

isFileSync(path: string): boolean

isDirectorySync(path) #

Synchronously checks if the path is a directory, and returns false if the path does not exist

isDirectorySync(path: string): boolean

readableSync(path) #

Synchronously checks if the path is readable, returning false if the path does not exist

readableSync(path: string): boolean

writableSync(path) #

Synchronously checks if the path is writable, returning false if the path does not exist

writableSync(path: string): boolean

executableSync(path) #

Synchronously checks if it is executable, returns false if the path does not exist

executableSync(path: string): boolean

chmodRecursionSync(path,mode?) #

Synchronously recursively set the file permission attribute mode (TODO: will block the calling thread, please use with caution)

chmodRecursionSync(mode?: Uint, path: string): void

Parameters:

For example:

fs.chmodRecursionSync(mypath, 0755);

chownRecursionSync(path,owner,group) #

Synchronously recursively set file owner and group attributes (TODO: will block the calling thread, please use with caution)

chownRecursionSync(owner: Uint, group: Uint, path: string): void

Parameters:

  • owner — System User ID
  • group — System Group ID

removeRecursionSync(path) #

Synchronous recursive deletion of directories or files, use this method with caution, it may cause the thread to be blocked for a long time

removeRecursionSync(path: string): void

copyRecursionSync(path,target) #

Synchronous recursive copy of files, use this method with caution, it may cause the thread to be blocked for a long time

copyRecursionSync(path: string, target: string): void

copySync(path,target) #

Copy files synchronously (TODO: will block the calling thread, please use with caution) Same as: copy files and directoriescopyRecursionSync(path,target)

copySync(path: string, target: string): void

writeFileSync(path,data,size?) #

Synchronously write data to a file

writeFileSync(size?: Uint, path: string, data: Uint8Array): Uint

Parameters:

  • size? — Write all data when no parameters are passed

writeFileSync(path,data,encoding?) #

Synchronously write a string to a file

writeFileSync(encoding?: Encoding, path: string, data: string): Uint

Parameters:

  • encoding? — If not passed in, the default encoding is utf-8

readFileSync(path) #

Reading files synchronously

readFileSync(path: string): Uint8Array

readFileSync(path,encoding) #

Synchronously read a file as a string

readFileSync(encoding: Encoding, path: string): string

Parameters:

  • encoding — Decode data to string encoding

openSync(path,flags?) #

Opens a file by path synchronously and returns an open file handle

openSync(flags?: FileOpenFlag, path: string): Uint

Parameters:

  • flags? — Open file flags mask Default as FileOpenFlag.FOPEN_R

closeSync(fd) #

Close the file handle synchronously

closeSync(fd: Uint): void

readSync(fd,out,size?,offsetFd?) #

Synchronously read the file contents from the file handle

readSync(fd: Uint, out: Uint8Array, size?: Int, offsetFd?: Int): Uint

Parameters:

  • fd — Open file handles
  • out — Read the file and save it here
  • size? — If not passed or passed in -1, the length of the out parameter is used
  • offsetFd? — If not passed or -1 is passed, the internal offset value of the file handle is used (it will advance after each read)

Returns: Returns the size of the data actually read

writeSync(fd,data,size?,offsetFd?) #

Synchronously write data to a file handle

writeSync(fd: Uint, data: Uint8Array, size?: Int, offsetFd?: Int): Uint

Parameters:

  • fd — Open file handles
  • data — Data to be written
  • size? — If not passed or -1 is passed, all data will be written
  • offsetFd? — If not passed or -1 is passed, the internal offset value of the file handle is used (it will advance after each write)

Returns: The actual size of the data written

writeSync(fd,data,offsetFd?) #

Write data to the file handle synchronously and encode the data using utf-8

writeSync(fd: Uint, data: string, offsetFd?: Int): Uint

Parameters:

  • fd — Open file handles
  • data — The string to be written
  • offsetFd? — If not passed or -1 is passed, the internal offset value of the file handle is used (it will advance after each write)

Returns: The actual size of the data written

writeSync(fd,data,encoding,offsetFd?) #

Synchronously write data to a file handle

writeSync(fd: Uint, data: string, encoding: Encoding, offsetFd?: Int): Uint

Parameters:

  • fd — Open file handles
  • data — Data to be written
  • encoding — Encoding Type
  • offsetFd? — If not passed or -1 is passed, the internal offset value of the file handle is used (it will advance after each write)

Returns: The actual size of the data written

chmod(path,mode?) #

Please ref: sync method chmodSync(path,mode?)

chmod(path: string, mode?: Uint)

chown(path,owner,group) #

Please ref: sync method chownSync(path,owner,group)

chown(path: string, owner: Uint, group: Uint)

chmod(path,mode?) #

Please ref: sync method mkdirSync(path,mode?)

chmod(path: string, mode?: Uint)

mkdirs(path,mode?) #

Recursively create directories. This method will create a directory tree in sequence, and will not throw an exception if the directory exists.

Ref: sync method [mkdirsSync(path[,mode])]

mkdirs(path: string, mode?: Uint): Promise

Parameters:

Returns:

For example:

fs.mkdirs(mypath).then(()=>{
    // Success
}).catch(err=>{
    // Fail
});

rename(name,newName) #

Please ref: sync method renameSync(name,newName)

rename(name: string, newName: string)

Please ref: sync method linkSync(src,target)

link(src: string, target: string)

Please ref: sync method unlinkSync(path)

unlink(path: string)

rmdir(path) #

Please ref: sync method rmdirSync(path)

rmdir(path: string)

readdir(path) #

Read directory listing information. Throws an exception if failed. Returns an Array of Dirent if successful.

readdir(path: string): Promise<Dirent[]>

For example:

// Prints:
// {
//   name: "cp.txt",
//   pathname: "file:///var/mobile/Containers/Data/Application/64DAC3FC-A4FD-4274-A2E7-B834EE4930B4/Documents/test/cp.txt",
//   type: 1
// }
fs.readdir(mydir).then(dirents=>{
    for (var dirent of dirents) {
        // TODO...
        console.log(dirent);
    }
}).catch(err=>{
    // Fail
});

stat(path) #

Please ref: sync method statSync(path)

stat(path: string): Promise<FileStat>

exists(path) #

Please ref: sync method existsSync(path)

exists(path: string): Promise<boolean>

isFile(path) #

Please ref: sync method isFileSync(path)

isFile(path: string): Promise<boolean>

isDirectory(path) #

Please ref: sync method isDirectorySync(path)

isDirectory(path: string): Promise<boolean>

readable(path) #

Please ref: sync method readableSync(path)

readable(path: string): Promise<boolean>

writable(path) #

Please ref: sync method writableSync(path)

writable(path: string): Promise<boolean>

executable(path) #

Please ref: sync method executableSync(path)

executable(path: string): Promise<boolean>

chmodRecursion(path,mode?) #

Asynchronously recursively set the mode attribute of a file or directory

chmodRecursion(path: string, mode?: Uint): AsyncTask

Parameters:

Returns:

Example:

// `mypath`For the file path, it can be a file or a directory
fs.chmodR(mypath, 0755).then(function() {
    console.log('Success');
}).catch(err=>{
    console.log('Fail');
});
var id = fs.chmodR(mydir, 0775);
fs.abort(id);

chownRecursion(path,owner,group) #

Asynchronously recursively set the owner and group attributes of files or directories

chownRecursion(path: string, owner: Uint, group: Uint): AsyncTask<void>

For example:

var task = chownRecursion(mypath, 501, 501);
fs.abort(task.id); // force abort task

removeRecursion(path) #

Recursively delete files and directories

removeRecursion(path: string): AsyncTask<void>

For example:

var task = fs.removeRecursion(mypath);
task.then(()=>{
    // Success
}).catch(err=>{
    // Fail
});
// The deletion task can be aborted by id
fs.abort(task.id);

copyRecursion(path,target) #

Recursively copy files The difference between copyRecursion() and copy() is that copy() can only copy a single file

copyRecursion(path: string, target: string): AsyncTask<void>

For example:

fs.copy(source, target).then(()=>{
    // Success
}).catch(err=>{
    // Fail
});

copy(path,target) #

Copy a single file Ref: copyRecursion(path,target)

copy(path: string, target: string): AsyncTask<void>

readStream(path,cb) #

Read file contents using asynchronous streaming

readStream(path: string, cb: StreamResponseCallback): AsyncTask<void>

Parameters:

  • path — Read the target path
  • cb — Asynchronous stream callback function

abort(id) #

Force abort a running asynchronous task by id If a meaningless id is passed in or the task to which the id belongs has been completed, no processing will be done

abort(id: Int): void

For example:

var a = fs.chmod(mypath, 0o755);
var b = fs.chown(mypath, 501, 501);
var c = fs.copy(mypath, newpath);
// force abort task
fs.abort(a.id);
fs.abort(b.id);
fs.abort(c.id);

writeFile(path,data,size?) #

Please ref: sync method writeFileSync(path,data,size?)

writeFile(path: string, data: Uint8Array, size?: Uint): Promise<Uint>

writeFile(path,data,encoding?) #

Please ref: sync method writeFileSync(path,data,encoding?)

writeFile(path: string, data: string, encoding?: Encoding): Promise<Uint>

readFile(path) #

Please ref: sync method readFileSync(path)

readFile(path: string): Promise<Uint8Array>

readFile(path,encoding) #

Please ref: sync method readFileSync(path,encoding)

readFile(path: string, encoding: Encoding): Promise<string>

open(path,flags?) #

Please ref: sync method openSync(path,flags?)

open(path: string, flags?: FileOpenFlag): Promise<Uint>

close(fd) #

Please ref: sync method closeSync(fd)

close(fd: Uint): Promise<void>

read(fd,out,size?,offsetFd?) #

Please ref: sync method readSync(fd,out,size?,offsetFd?)

read(fd: Uint, out: Uint8Array, size?: Int, offsetFd?: Int): Promise<Uint>

write(fd,data,size?,offsetFd?) #

Please ref: sync method writeSync(fd,data,size?,offsetFd?)

write(fd: Uint, data: Uint8Array, size?: Int, offsetFd?: Int): Promise<Uint>

write(fd,data,offsetFd?) #

Please ref: sync method writeSync(fd,data,offsetFd?)

write(fd: Uint, data: string, offsetFd?: Int): Promise<Uint>

write(fd,data,encoding,offsetFd?) #

Please ref: sync method writeSync(fd,data,encoding,offsetFd?)

write(fd: Uint, data: string, encoding: Encoding, offsetFd?: Int): Promise<Uint>

Interface: Reader #

Reader #

The methods provided here can perform basic read operations on URI paths of different protocols

Currently supported path types:

  • http:// or https:// - can be read synchronously or asynchronously, but cannot read directories or test existence, readdirSync() returns an empty array and isFileSync() always returns false.

  • file:// local file path. /var/data or var/data can be used as local paths without error.

  • zip:// This is a way to represent the path in the zip package, zip:///var/data/test.zip@/a.txt This path represents the a.txt file in zip:///var/data/test.zip. Note that this path must exist in the local file system

reader.readFile(path) #

Reading file data

readFile(path: string): AsyncTask<Uint8Array>

For example:

reader.readFile('http://xxx.com/test.txt').then(()=>{
    // Success
}).catch(err=>{
    // Fail
})

reader.readFile(path,encoding) #

Read file data and decode it into a string

readFile(path: string, encoding: Encoding): AsyncTask<string>

reader.readStream(path,cb) #

Read file data by asynchronous streaming

readStream(path: string, cb: StreamResponseCallback): AsyncTask<void>

For example:

// async read file stream
reader.readStream('http://www.baidu.com', function(d){}));
reader.readStream('file:///var/data/test.txt', function(d){}));
reader.readStream('zip:///var/data/test.zip@aa.txt', function(d){
    // Success
    console.log(d.data.length, d.ended);
});

reader.readFileSync(path) #

Please ref: method Reader.readFile(path)

readFileSync(path: string): Uint8Array

reader.readFileSync(path,encoding) #

Please ref: method Reader.readFile(path,encoding)

readFileSync(path: string, encoding: Encoding): string

reader.existsSync(path) #

同步测试文件或目录是否存在,如果文件存在会返回false 这个方法不能处理http://https://类型的路径,如果传入这种路径立即返回false Synchronously test whether a file or directory exists. If the file exists, it will return false This method cannot handle paths of the http:// and https:// type. If such a path is passed, it will immediately return false

existsSync(path: string): boolean

reader.isFileSync(path) #

同步检查路径是否为文件,如果路径不存在返回 false 这个方法不能处理http://https://类型的路径,如果传入这种路径立即返回false Synchronously check if the path is a file, and return false if the path does not exist This method cannot handle http:// and https:// type paths, and immediately returns false if such a path is passed

isFileSync(path: string): boolean

reader.isDirectorySync(path) #

同步检查路径是否为目录,如果路径不存在返回 false 这个方法不能处理http://https://类型的路径,如果传入这种路径立即返回false Synchronously check if the path is a directory, and return false if the path does not exist This method cannot handle http:// and https:// type paths, and immediately returns false if such a path is passed in

isDirectorySync(path: string): boolean

reader.readdirSync(path) #

同步读取目录文件列表, 这个方法不能处理http://https://类型的路径,如果传入这种路径立即返回一个空数组Array, 这个方法也不会抛出异常,如果不能读取路径,只会返回空数组Array Synchronously read the directory file list, This method cannot handle http:// and https:// type paths. If such a path is passed, an empty array Array will be immediately returned. This method will not throw an exception. If the path cannot be read, it will only return an empty array Array

readdirSync(path: string): Dirent[]

reader.abort(id) #

Abort an asynchronous task by its id

abort(id: Uint): void

reader.clear() #

To clear cache of reader

clear(): void

reader #

const reader: Reader