tspace-nfs
Version:
tspace-nfs is a Network File System (NFS) and provides both server and client capabilities for accessing files over a network.
236 lines (235 loc) • 6.9 kB
TypeScript
/// <reference types="node" />
import { Readable } from 'stream';
/**
*
* The 'NfsClient' class is a client for nfs server
* @example
* import { NfsClient } from "tspace-nfs";
* import PathSystem from 'path';
* import pathSystem from 'path';
*
* const nfs = new NfsClient({
* token : '<YOUR TOKEN>', // token
* secret : '<YOUR SECRET>', // secret
* bucket : '<YOUR BUCKET>', // bucket name
* url : '<YOUR URL>' // https://nfs-server.example.com
* })
* .onError((err, nfs) => {
* console.log('nfs client failed to connect')
* console.log(err.message)
* nfs.quit()
* })
* .onConnect((nfs) => {
* console.log('nfs client connected')
* })
*
* const mycat = 'cats/my-cat.png'
* const url = await nfs.toURL(mycat)
*
* console.log(url)
*/
declare class NfsClient {
private _directory;
private _event;
private _authorization;
private _url;
private _fileExpired;
private _ENDPOINT_CONNECT;
private _ENDPOINT_REMOVE;
private _ENDPOINT_FILE;
private _ENDPOINT_FILE_BASE64;
private _ENDPOINT_FILE_STREAM;
private _ENDPOINT_STORAGE;
private _ENDPOINT_FOLDERS;
private _ENDPOINT_UPLOAD;
private _ENDPOINT_MERGE;
private _ENDPOINT_UPLOAD_BASE64;
private _TOKEN_EXPIRED_MESSAGE;
private _credentials;
constructor({ token, secret, bucket, url }: {
token: string;
secret: string;
bucket: string;
url: string;
});
/**
* The 'default' method is used to default prefix the directory every path
*
* @param {string} directory
* @returns {this}
*/
default(directory: string): this;
/**
* The 'onError' method is used to handle the error that occurs when trying connect to nfs server
*
* @param {Function} callback
* @returns {this}
*/
onError(callback: (err: any, nfs: NfsClient) => void): this;
/**
* The 'onConnect' method is used cheke the connection to nfs server
*
* @param {Function} callback
* @returns {this}
*/
onConnect(callback: (nfs: NfsClient) => void): this;
/**
* The 'quit' method is used quit the connection and stop the serivce
*
* @return {never}
*/
quit(): never;
/**
* The 'toURL' method is used to converts a given file path to a URL
*
* @param {string} path
* @param {object} options
* @property {boolean} options.download
* @property {number} options.expired // expires in seconds
* @property {number} options.exists // checks the file exists only
* @return {promise<string>}
*/
toURL(path: string, { download, expired, exists }?: {
download?: boolean;
expired?: number;
exists?: boolean;
}): Promise<string>;
/**
* The 'toBase64' method is used to converts a given file path to base64 encoded
*
* @param {string} path
* @return {promise<string>}
*/
toBase64(path: string): Promise<string>;
/**
* The 'toStream' method is used to converts a given file path to stream format
*
* @param {string} path
* @param {string?} range
* @return {promise<string>}
*/
toStream(path: string, range?: string): Promise<Readable>;
/**
* The 'upload' method is used uploading file
*
* @param {object} obj
* @property {string} obj.file
* @property {string} obj.name
* @property {string?} obj.folder
* @property {number?} obj.chunkSize // unit mb by default 200 mb
* @return {promise<{size : number , path : string , name : string , url : string}>}
*/
upload({ file, name, extension, folder, chunkSize }: {
file: string;
name: string;
extension?: string;
folder?: string;
chunkSize?: number;
}): Promise<{
size: number;
path: string;
name: string;
url: string;
}>;
/**
* The 'save' method is used uploading file
*
* @param {object} obj
* @property {string} obj.file
* @property {string} obj.name
* @property {string?} obj.folder
* @property {number?} obj.chunkSize // unit mb by default 200 mb
* @return {promise<{size : number , path : string , name : string , url : string}>}
*/
save({ file, name, extension, folder, chunkSize }: {
file: string;
name: string;
extension?: string;
folder?: string;
chunkSize?: number;
}): Promise<{
size: number;
path: string;
name: string;
url: string;
}>;
/**
* The 'uploadBase64' method is used uploading file with base64 encoded
*
* @param {object} obj
* @property {string} obj.base64
* @property {string} obj.name
* @property {string?} obj.folder
* @return {promise<{size : number , path : string , name : string}>}
*/
uploadBase64({ base64, name, extension, folder }: {
base64: string;
name: string;
extension?: string;
folder?: string;
}): Promise<{
size: number;
path: string;
name: string;
}>;
/**
* The 'saveAS' method is used uploading file with base64 encoded
*
* @param {object} obj
* @property {string} obj.base64
* @property {string} obj.name
* @property {string?} obj.folder
* @return {promise<{size : number , path : string , name : string}>}
*/
saveAs({ base64, name, extension, folder }: {
base64: string;
name: string;
extension?: string;
folder?: string;
}): Promise<{
size: number;
path: string;
name: string;
}>;
/**
* The 'delete' method is used to delete a file
*
* @param {string} path
* @return {promise<string>}
*/
delete(path: string): Promise<void>;
/**
* The 'remove' method is used to delete a file
*
* @param {string} path
* @return {promise<string>}
*/
remove(path: string): Promise<void>;
/**
* The 'storage' method is used to get information about the storage
*
* @param {string?} folder
* @return {Promise<{name : string , size : number }[]>}
*/
storage(folder?: string): Promise<{
name: string;
size: number;
}[]>;
/**
* The 'folders' method is used to get list of folders
*
* @return {Promise<{name : string , size : number }[]>}
*/
folders(): Promise<{
name: string;
size: number;
}[]>;
private _fetch;
private _URL;
private _getConnect;
private _retryConnect;
private _normalizeFilename;
private _normalizeDefaultDirectory;
}
export { NfsClient };
export default NfsClient;