npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

delivery

v0.0.6

Published

Bidirectional File Transfers For Node.js via Socket.IO

Downloads

523

Readme

Delivery.js (Experimental)

Bidirectional File Transfers For Node.js via Socket.IO

Sending files to the server, and pushing files to the client should be as easy as possible. Delivery.js uses Node.js and Socket.IO to make it easy to push files to the client, or send them to the server. Files can be pushed to the client as text (utf8) or base64 (for images and binary files).

Install

npm install delivery -g

Browser JavaScript

delivery.js can be found within lib/client.

Examples

Sending a File To a Server (Client is a browser)

Server-side code

var io  = require('socket.io').listen(5001),
    dl  = require('delivery'),
    fs  = require('fs');

io.sockets.on('connection', function(socket){
  var delivery = dl.listen(socket);
  delivery.on('receive.success',function(file){
    var params = file.params;
    fs.writeFile(file.name,file.buffer, function(err){
      if(err){
        console.log('File could not be saved.');
      }else{
        console.log('File saved.');
      };
    });
  });
});

Client-side code

$(function(){
  var socket = io.connect('http://0.0.0.0:5001');
  
  socket.on('connect', function(){
    var delivery = new Delivery(socket);

    delivery.on('delivery.connect',function(delivery){
      $("input[type=submit]").click(function(evt){
        var file = $("input[type=file]")[0].files[0];
        var extraParams = {foo: 'bar'};
        delivery.send(file, extraParams);
        evt.preventDefault();
      });
    });

    delivery.on('send.success',function(fileUID){
      console.log("file was successfully sent.");
    });
  });
});

Pushing a File to a Client (Client is a browser)

Server-side code

var io  = require('socket.io').listen(5001),
    dl  = require('delivery');

io.sockets.on('connection', function(socket){
  var delivery = dl.listen(socket);
  delivery.on('delivery.connect',function(delivery){

    delivery.send({
      name: 'sample-image.jpg',
      path : './sample-image.jpg',
      params: {foo: 'bar'}
    });

    delivery.on('send.success',function(file){
      console.log('File successfully sent to client!');
    });

  });
});

Client-side code

$(function(){
  var socket = io.connect('http://0.0.0.0:5001');
  
  socket.on('connect', function(){
    var delivery = new Delivery(socket);

    delivery.on('receive.start',function(fileUID){
      console.log('receiving a file!');
    });

    delivery.on('receive.success',function(file){
      var params = file.params;
      if (file.isImage()) {
        $('img').attr('src', file.dataURL());
      };
    });
  });
});

Transfer files between two servers

Receive file

io.sockets.on('connection', function(socket){
  
  var delivery = dl.listen(socket);
  delivery.on('receive.success',function(file){
		
    fs.writeFile(file.name, file.buffer, function(err){
      if(err){
        console.log('File could not be saved: ' + err);
      }else{
        console.log('File ' + file.name + " saved");
      };
    });
  });	
});

Send file

socket.on( 'connect', function() {
  log( "Sockets connected" );
		
  delivery = dl.listen( socket );
  delivery.connect();
	
  delivery.on('delivery.connect',function(delivery){
    delivery.send({
      name: 'sample-image.jpg',
      path : './sample-image.jpg'
    });

    delivery.on('send.success',function(file){
      console.log('File sent successfully!');
    });
  });
	
});

API

Server Functions

Importing delivery.js

dl = require('delivery');

Listen to the socket

var delivery = dl.listen(socket);

Listening to delivery.js events - delivery.on('event',fn)

delivery.on('delivery.connect',function(delivery){
  ...
});

Sending a file

delivery.send({
  name: 'fileName.png',
  path: 'path/to/file/fileName.png'
});
delivery.sendAsText({
  name: 'fileName.txt',
  path: 'path/to/file/fileName.txt'
});

Server Events

'delivery.connect'

delivery.connect is called when a client connects to the server.

delivery.on('delivery.connect',function(delivery){
  ...
});

'receive.start'

receive.start is called when the server starts receiving a file. The callback function takes a filePackage object that describes the file being sent.

delivery.on('receive.start',function(filePackage){
  console.log(filePackage.name);
});

'receive.success'

receive.success is called once the file has been successfully reveived by the server. The callback function takes a filePackage.

delivery.on('receive.success',function(file){
    fs.writeFile(file.name,file.buffer, function(err){
      if(err){
        console.log('File could not be saved.');
      }else{
        console.log('File saved.');
      };
    });
  });

'file.load'

file.load is called after .send() is called and immediately after the file is loaded. The callback function takes a filePackage.

delivery.on('file.load',function(filePackage){
  console.log(filePackage.name + " has just been loaded.");
});

'send.start'

send.start is called after .send() is called and immediately after the file begins being sent to the client. The callback function takes a filePackage.

delivery.on('send.start',function(filePackage){
  console.log(filePackage.name + " is being sent to the client.");
});

'send.success'

send.success is called after .send() is called and once confirmation is received form the client that the the file sent was successfully received. The callback function takes the uid of the file that was sent.

delivery.on('send.success',function(uid){
  console.log("File successfully sent!");
});

FilePackage

FilePackage objects encapsulate files and includes a text representation (utf8), or base64 representation of the file. They also include the file's meta data, including name, size and mimeType.

filePackage.isImage()

returns true if the file has a corresponding mime type that is an image. It is possible that this method could return false if your file is an image, but does not have a mimetype, or does not have a mimetype of image/gif, image/jpeg, image/png, image/svg+xml, image/tiff. Look for var imageFilter within delivery.js if you'd like to add additional mimetypes.

filePackage.isText()

returns true if the server used sendAsText().

filePackage.text()

returns the text representation of the file sent. If the file has been base64 encoded it returns the base64 encoded version of the file.

filePackage.dataURL()

returns the base64 representation of the file prefixed with the data and mimetype necessary to display an image within <img src=''>.

Client

Include delivery.js in your html file

<script src="/js/delivery.js"></script>

Events

Client events mirror those on the server, see server events above for more details.

##License

Delivery.js is released under the MIT license:

http://www.opensource.org/licenses/MIT

Road Map

  1. Incorporating feedback from other developers!
  2. Breaking files into pieces to help transfer larger files.
  3. md5 hash file and confirm the hash when a file has been received.
  4. ?