22 KiB
SFTPStream events
Client/Server events
- ready() - Emitted after initial protocol version check has passed.
Server-only events
Responses to these client requests are sent using one of the methods listed further in this document under Server-only methods. The valid response(s) for each request are documented below.
-
OPEN(< integer >reqID, < string >filename, < integer >flags, < ATTRS >attrs)
flagsis a bitfield containing any of the flags defined inSFTPStream.OPEN_MODE. Use the static methodSFTPStream.flagsToString()to convert the value to a mode string to be used byfs.open()(e.g.'r').Respond using one of the following:
-
handle()- This indicates a successful opening of the file and passes the given handle back to the client to use to refer to this open file for future operations (e.g. reading, writing, closing). -
status()- Use this to indicate a failure to open the requested file.
-
-
READ(< integer >reqID, < Buffer >handle, < integer >offset, < integer >length)
Respond using one of the following:
-
data()- Use this to send the requested chunk of data back to the client. The amount of data sent is allowed to be less than thelengthrequested, for example if the file ends betweenoffsetandoffset + length. -
status()- Use this to indicate either end of file (STATUS_CODE.EOF) has been reached (offsetis past the end of the file) or if an error occurred while reading the requested part of the file.
-
-
WRITE(< integer >reqID, < Buffer >handle, < integer >offset, < Buffer >data)
Respond using:
status()- Use this to indicate success/failure of the write to the file.
-
FSTAT(< integer >reqID, < Buffer >handle)
Respond using one of the following:
-
attrs()- Use this to send the attributes for the requested file/directory back to the client. -
status()- Use this to indicate an error occurred while accessing the file/directory.
-
-
FSETSTAT(< integer >reqID, < Buffer >handle, < ATTRS >attrs)
Respond using:
status()- Use this to indicates success/failure of the setting of the given file/directory attributes.
-
CLOSE(< integer >reqID, < Buffer >handle)
Respond using:
status()- Use this to indicate success (STATUS_CODE.OK) or failure of the closing of the file identified byhandle.
-
OPENDIR(< integer >reqID, < string >path)
Respond using one of the following:
-
handle()- This indicates a successful opening of the directory and passes the given handle back to the client to use to refer to this open directory for future operations (e.g. reading directory contents, closing). -
status()- Use this to indicate a failure to open the requested directory.
-
-
READDIR(< integer >reqID, < Buffer >handle)
Respond using one of the following:
-
name()- Use this to send one or more directory listings for the open directory back to the client. -
status()- Use this to indicate either end of directory contents (STATUS_CODE.EOF) or if an error occurred while reading the directory contents.
-
-
LSTAT(< integer >reqID, < string >path)
Respond using one of the following:
-
attrs()- Use this to send the attributes for the requested file/directory back to the client. -
status()- Use this to indicate an error occurred while accessing the file/directory.
-
-
STAT(< integer >reqID, < string >path)
Respond using one of the following:
-
attrs()- Use this to send the attributes for the requested file/directory back to the client. -
status()- Use this to indicate an error occurred while accessing the file/directory.
-
-
REMOVE(< integer >reqID, < string >path)
Respond using:
status()- Use this to indicate success/failure of the removal of the file atpath.
-
RMDIR(< integer >reqID, < string >path)
Respond using:
status()- Use this to indicate success/failure of the removal of the directory atpath.
-
REALPATH(< integer >reqID, < string >path)
Respond using one of the following:
-
name()- Use this to respond with a normalized version ofpath. No file/directory attributes are required to be sent in this response. -
status()- Use this to indicate a failure in normalizingpath.
-
-
READLINK(< integer >reqID, < string >path)
Respond using one of the following:
-
name()- Use this to respond with the target of the symlink atpath. No file/directory attributes are required to be sent in this response. -
status()- Use this to indicate a failure in reading the symlink atpath.
-
-
SETSTAT(< integer >reqID, < string >path, < ATTRS >attrs)
Respond using:
status()- Use this to indicates success/failure of the setting of the given file/directory attributes.
-
MKDIR(< integer >reqID, < string >path, < ATTRS >attrs)
Respond using:
status()- Use this to indicate success/failure of the creation of the directory atpath.
-
RENAME(< integer >reqID, < string >oldPath, < string >newPath)
Respond using:
status()- Use this to indicate success/failure of the renaming of the file/directory atoldPathtonewPath.
-
SYMLINK(< integer >reqID, < string >linkPath, < string >targetPath)
Respond using:
status()- Use this to indicate success/failure of the symlink creation.
SFTPStream static constants
-
SFTPStream.STATUS_CODE - object - Contains the various status codes (for use especially with
status()):-
OK -
EOF -
NO_SUCH_FILE -
PERMISSION_DENIED -
FAILURE -
BAD_MESSAGE -
OP_UNSUPPORTED
-
-
SFTPStream.OPEN_MODE - object - Contains the various open file flags:
-
READ -
WRITE -
APPEND -
CREAT -
TRUNC -
EXCL
-
SFTPStream static methods
-
SFTPStream.stringToFlags(< string >flagsStr) - integer - Converts string flags (e.g.
'r','a+', etc.) to the appropriateSFTPStream.OPEN_MODEflag mask. Returnsnullif conversion failed. -
SFTPStream.flagsToString(< integer >flagsMask) - string - Converts flag mask (e.g. number containing
SFTPStream.OPEN_MODEvalues) to the appropriate string value. Returnsnullif conversion failed.
SFTPStream methods
-
(constructor)(< object >config[, < string >remoteIdentRaw]) - Creates and returns a new SFTPStream instance. SFTPStream instances are Duplex streams.
remoteIdentRawcan be the raw SSH identification string of the remote party. This is used to change internal behavior based on particular SFTP implementations.configcan contain:-
server - boolean - Set to
trueto create an instance in server mode. Default:false -
highWaterMark - integer - This is the
highWaterMarkto use for the stream. Default:32 * 1024 -
debug - function - Set this to a function that receives a single string argument to get detailed (local) debug information. Default: (none)
-
Client-only methods
-
fastGet(< string >remotePath, < string >localPath[, < object >options], < function >callback) - (void) - Downloads a file at
remotePathtolocalPathusing parallel reads for faster throughput.optionscan have the following properties:-
concurrency - integer - Number of concurrent reads Default:
64 -
chunkSize - integer - Size of each read in bytes Default:
32768 -
step - function(< integer >total_transferred, < integer >chunk, < integer >total) - Called every time a part of a file was transferred
callbackhas 1 parameter: < Error >err. -
-
fastPut(< string >localPath, < string >remotePath[, < object >options], < function >callback) - (void) - Uploads a file from
localPathtoremotePathusing parallel reads for faster throughput.optionscan have the following properties:-
concurrency - integer - Number of concurrent reads Default:
64 -
chunkSize - integer - Size of each read in bytes Default:
32768 -
step - function(< integer >total_transferred, < integer >chunk, < integer >total) - Called every time a part of a file was transferred
-
mode - mixed - Integer or string representing the file mode to set for the uploaded file.
callbackhas 1 parameter: < Error >err. -
-
createReadStream(< string >path[, < object >options]) - ReadStream - Returns a new readable stream for
path.optionshas the following defaults:{ flags: 'r', encoding: null, handle: null, mode: 0o666, autoClose: true }optionscan includestartandendvalues to read a range of bytes from the file instead of the entire file. Bothstartandendare inclusive and start at 0. Theencodingcan be'utf8','ascii', or'base64'.If
autoCloseis false, then the file handle won't be closed, even if there's an error. It is your responsiblity to close it and make sure there's no file handle leak. IfautoCloseis set to true (default behavior), onerrororendthe file handle will be closed automatically.An example to read the last 10 bytes of a file which is 100 bytes long:
sftp.createReadStream('sample.txt', {start: 90, end: 99}); -
createWriteStream(< string >path[, < object >options]) - WriteStream - Returns a new writable stream for
path.optionshas the following defaults:{ flags: 'w', encoding: null, mode: 0o666, autoClose: true }optionsmay also include astartoption to allow writing data at some position past the beginning of the file. Modifying a file rather than replacing it may require a flags mode of 'r+' rather than the default mode 'w'.If 'autoClose' is set to false and you pipe to this stream, this stream will not automatically close after there is no more data upstream -- allowing future pipes and/or manual writes.
-
open(< string >filename, < string >flags, [< mixed >attrs_mode, ]< function >callback) - boolean - Opens a file
filenamewithflagswith optional ATTRS object or file modeattrs_mode.flagsis any of the flags supported byfs.open(except sync flag). Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 2 parameters: < Error >err, < Buffer >handle. -
close(< Buffer >handle, < function >callback) - boolean - Closes the resource associated with
handlegiven by open() or opendir(). Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 1 parameter: < Error >err. -
readData(< Buffer >handle, < Buffer >buffer, < integer >offset, < integer >length, < integer >position, < function >callback) - boolean - Reads
lengthbytes from the resource associated withhandlestarting atpositionand stores the bytes inbufferstarting atoffset. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 4 parameters: < Error >err, < integer >bytesRead, < Buffer >buffer (offset adjusted), < integer >position. -
writeData(< Buffer >handle, < Buffer >buffer, < integer >offset, < integer >length, < integer >position, < function >callback) - boolean - Writes
lengthbytes frombufferstarting atoffsetto the resource associated withhandlestarting atposition. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 1 parameter: < Error >err. -
fstat(< Buffer >handle, < function >callback) - boolean - Retrieves attributes for the resource associated with
handle. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 2 parameters: < Error >err, < Stats >stats. -
fsetstat(< Buffer >handle, < ATTRS >attributes, < function >callback) - boolean - Sets the attributes defined in
attributesfor the resource associated withhandle. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 1 parameter: < Error >err. -
futimes(< Buffer >handle, < mixed >atime, < mixed >mtime, < function >callback) - boolean - Sets the access time and modified time for the resource associated with
handle.atimeandmtimecan be Date instances or UNIX timestamps. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 1 parameter: < Error >err. -
fchown(< Buffer >handle, < integer >uid, < integer >gid, < function >callback) - boolean - Sets the owner for the resource associated with
handle. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 1 parameter: < Error >err. -
fchmod(< Buffer >handle, < mixed >mode, < function >callback) - boolean - Sets the mode for the resource associated with
handle.modecan be an integer or a string containing an octal number. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 1 parameter: < Error >err. -
opendir(< string >path, < function >callback) - boolean - Opens a directory
path. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 2 parameters: < Error >err, < Buffer >handle. -
readdir(< mixed >location, < function >callback) - boolean - Retrieves a directory listing.
locationcan either be a Buffer containing a valid directory handle from opendir() or a string containing the path to a directory. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 2 parameters: < Error >err, < mixed >list.listis an Array of{ filename: 'foo', longname: '....', attrs: {...} }style objects (attrs is of type ATTR). Iflocationis a directory handle, this function may need to be called multiple times untillistis boolean false, which indicates that no more directory entries are available for that directory handle. -
unlink(< string >path, < function >callback) - boolean - Removes the file/symlink at
path. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 1 parameter: < Error >err. -
rename(< string >srcPath, < string >destPath, < function >callback) - boolean - Renames/moves
srcPathtodestPath. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 1 parameter: < Error >err. -
mkdir(< string >path, [< ATTRS >attributes, ]< function >callback) - boolean - Creates a new directory
path. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 1 parameter: < Error >err. -
rmdir(< string >path, < function >callback) - boolean - Removes the directory at
path. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 1 parameter: < Error >err. -
stat(< string >path, < function >callback) - boolean - Retrieves attributes for
path. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 2 parameter: < Error >err, < Stats >stats. -
lstat(< string >path, < function >callback) - boolean - Retrieves attributes for
path. Ifpathis a symlink, the link itself is stat'ed instead of the resource it refers to. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 2 parameters: < Error >err, < Stats >stats. -
setstat(< string >path, < ATTRS >attributes, < function >callback) - boolean - Sets the attributes defined in
attributesforpath. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 1 parameter: < Error >err. -
utimes(< string >path, < mixed >atime, < mixed >mtime, < function >callback) - boolean - Sets the access time and modified time for
path.atimeandmtimecan be Date instances or UNIX timestamps. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 1 parameter: < Error >err. -
chown(< string >path, < integer >uid, < integer >gid, < function >callback) - boolean - Sets the owner for
path. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 1 parameter: < Error >err. -
chmod(< string >path, < mixed >mode, < function >callback) - boolean - Sets the mode for
path.modecan be an integer or a string containing an octal number. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 1 parameter: < Error >err. -
readlink(< string >path, < function >callback) - boolean - Retrieves the target for a symlink at
path. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 2 parameters: < Error >err, < string >target. -
symlink(< string >targetPath, < string >linkPath, < function >callback) - boolean - Creates a symlink at
linkPathtotargetPath. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 1 parameter: < Error >err. -
realpath(< string >path, < function >callback) - boolean - Resolves
pathto an absolute path. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 2 parameters: < Error >err, < string >absPath. -
ext_openssh_rename(< string >srcPath, < string >destPath, < function >callback) - boolean - OpenSSH extension Performs POSIX rename(3) from
srcPathtodestPath. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 1 parameter: < Error >err. -
ext_openssh_statvfs(< string >path, < function >callback) - boolean - OpenSSH extension Performs POSIX statvfs(2) on
path. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 2 parameters: < Error >err, < object >fsInfo.fsInfocontains the information as found in the statvfs struct. -
ext_openssh_fstatvfs(< Buffer >handle, < function >callback) - boolean - OpenSSH extension Performs POSIX fstatvfs(2) on open handle
handle. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 2 parameters: < Error >err, < object >fsInfo.fsInfocontains the information as found in the statvfs struct. -
ext_openssh_hardlink(< string >targetPath, < string >linkPath, < function >callback) - boolean - OpenSSH extension Performs POSIX link(2) to create a hard link to
targetPathatlinkPath. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 1 parameter: < Error >err. -
ext_openssh_fsync(< Buffer >handle, < function >callback) - boolean - OpenSSH extension Performs POSIX fsync(3) on the open handle
handle. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.callbackhas 1 parameter: < Error >err.
Server-only methods
-
status(< integer >reqID, < integer >statusCode[, < string >message]) - boolean - Sends a status response for the request identified by
id. Returnsfalseif you should wait for thecontinueevent before sending any more traffic. -
handle(< integer >reqID, < Buffer >handle) - boolean - Sends a handle response for the request identified by
id.handlemust be less than 256 bytes and is an opaque value that could merely contain the value of a backing file descriptor or some other unique, custom value. Returnsfalseif you should wait for thecontinueevent before sending any more traffic. -
data(< integer >reqID, < mixed >data[, < string >encoding]) - boolean - Sends a data response for the request identified by
id.datacan be a Buffer or string. Ifdatais a string,encodingis the encoding ofdata. Returnsfalseif you should wait for thecontinueevent before sending any more traffic. -
name(< integer >reqID, < array >names) - boolean - Sends a name response for the request identified by
id. Returnsfalseif you should wait for thecontinueevent before sending any more traffic.namesmust be an array of object where each object can contain:-
filename - string - The entry's name.
-
longname - string - This is the
ls -l-style format for the entry (e.g.-rwxr--r-- 1 bar bar 718 Dec 8 2009 foo) -
attrs - ATTRS - This is an optional ATTRS object that contains requested/available attributes for the entry.
-
-
attrs(< integer >reqID, < ATTRS >attrs) - boolean - Sends an attrs response for the request identified by
id.attrscontains the requested/available attributes.
ATTRS
An object with the following valid properties:
-
mode - integer - Mode/permissions for the resource.
-
uid - integer - User ID of the resource.
-
gid - integer - Group ID of the resource.
-
size - integer - Resource size in bytes.
-
atime - integer - UNIX timestamp of the access time of the resource.
-
mtime - integer - UNIX timestamp of the modified time of the resource.
When supplying an ATTRS object to one of the SFTP methods:
-
atimeandmtimecan be either a Date instance or a UNIX timestamp. -
modecan either be an integer or a string containing an octal number.
Stats
An object with the same attributes as an ATTRS object with the addition of the following methods:
-
stats.isDirectory() -
stats.isFile() -
stats.isBlockDevice() -
stats.isCharacterDevice() -
stats.isSymbolicLink() -
stats.isFIFO() -
stats.isSocket()