Code Monkey home page Code Monkey logo

hypercore-upload-server's Introduction

hypercore-upload-server

File ingestion with Hypercore over WebSockets

Installation

$ npm install hypercore-upload-server

Example

Server

See example/server.js

Client

See example/client.js

Usage

const { Server } = require('hypercore-upload-server')
const path = require('path')
const raf = require('random-access-file')

const dataDir = '/path/to/data/dir'

const server = new Server({
  // storage for Corestore and it's Hypercore instances
  storage(filename) {
    return raf(path.reolve(dataDir, filename))
  },

  // write uploaded chunks somewhere, like S3, the filesystem, or a Hyperdrive
  onwrite(key, offset, buffer, metadata, callback) {
    const filename = path.resolve(data, metadata.name)
    touch(filename, (err) => {
      if (err) { return callback(err) }
      fs.open(filename, fs.constants.O_RDWR | fs.constants.O_CREAT, (err, fd) => {
        if (err) { return callback(err) }
        fs.write(fd, buffer, 0, buffer.length, offset, (err) => {
          if (err) { return callback(err) }
          fs.close(fd, callback)
        })
      })
    })
  }
})

API

server = new Server(opts)

Create a new Server for hypercore uploads over websockets where opts can be:

{
  corestore: null, // a corestore for a hypercore/hypertrie factory
  storage: null // an optional random access storage factory for a `Corestore` instance
  onwrite: (key, offset, data, metadata, callback) => { ... }, // called when data for a key at an offset needs to be written
  gc: {
    timeout: 5 * 1000 // timeout in milliseconds before a worker thread GC's any resources
  }
}

See example/server.js for a complete example.

server.listen(port[, host[, callback]])

Listen on a given port an optional host calling callback(err) upon error or success.

License

MIT

hypercore-upload-server's People

Contributors

jwerle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  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.