Giter Site home page Giter Site logo

fs-plug's Introduction

fs-plug

build status AppVeyor Build Status


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.


Get it!

npm install --save fs-plug

Usage

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()
  })
})

API

var plug = fsPlug([opts][, onconsumer(err, mypath)])

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.

plug.consume(conf, callback(err, localPath))

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
}

plug.whitelist(filepath)

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.

plug.blacklist(filepath)

Disallow sharing a resource if the plug has been instantiated with opts.enforceWhitelist.

plug.enforceWhitelist(bool)

Toggle requiring a whitelist check by passing a boolean.

plug.clearWhitelist()

Clear the plug's whitelist.

plug.setPassphrase(passphrase)

Set or reset the passphrase for authorizing inbound requests. passphrase msut be a Buffer or string.

plug.supplied

Read-only property indicating the number of files and directories supplied.

plug.consumed

Read-only property indicating the number of files and directories consumed.

plug.on('bytes-supplied', callback(socketid, num))

Emitted while writing to a socket. num is the number of bytes supplied through the socket indicated by socketid.

plug.on('bytes-consumed', callback(socketid, num))

Emitted while consuming from a socket. num is the number of bytes consumed so far through the socket indicated by socketid.


License

MIT

fs-plug's People

Contributors

chiefbiiko avatar

Stargazers

 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.