A TCP server that listens for own filepaths and streams out its indicated file or directory. Got a method to consume from such peers. And a simple access control mechanism.
npm install --save fs-plug
var plug = require('fs-plug')
// alice and bob on two different computers in the same local network
var a = plug()
var b = plug()
// alice allows file to be consumed by peers requesting it
a.whitelist(__filename)
// listen for connections
a.listen(10000, function() {
// bobs consume config
var conf = {
port: 10000,
host: "localhost",
type: "file",
remotePath: __filename,
localPath: "example"
}
// bob consuming from alice
b.consume(conf, function(err, localPath) {
if (err) return console.error(err)
console.log("file saved as:", localPath)
a.close()
b.close()
})
})
Create a new plug. Options default to:
{
dereference: false, // follow symlinks when looking up requested files?
enforceWhitelist: true, // only serve files if they have been whitelisted before?
timeout: 500 // max number of ms to wait for initial bytes when consuming
passphrase: undefined // if buffer or string require this passphrase for every request
whitelist: undefined // string[] seed the plug's whitelist with these file paths
interval: 250 // emit interval for the bytes-supplied and bytes-consumed events
}
onconsumer
will be called every time a file or directory has been supplied to a consumer or in case of an error during that process.
Consume from another plug.
conf
must be an object with the following properties:
{
port: number,
host: string,
type: string, // either 'file' or 'directory'
remotePath: string, // absolute filepath on serving machine
localPath: string // local filepath for consumed file
passphrase: string // sesameopen
}
Whitelist a file or directory on your machine to be shared with requesting consumers. Whitelisting is not required if a plug has been instantiated with !opts.enforceWhitelist
.
Disallow sharing a resource if the plug has been instantiated with opts.enforceWhitelist
.
Toggle requiring a whitelist check by passing a boolean.
Clear the plug's whitelist.
Set or reset the passphrase for authorizing inbound requests. passphrase
msut be a Buffer
or string
.
Read-only property indicating the number of files and directories supplied.
Read-only property indicating the number of files and directories consumed.
Emitted while writing to a socket. num
is the number of bytes supplied through the socket indicated by socketid
.
Emitted while consuming from a socket. num
is the number of bytes consumed so far through the socket indicated by socketid
.