Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

upgrade

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

upgrade

A simple Node websocket library to handle http upgrades.

  • 1.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
17K
decreased by-14.14%
Maintainers
1
Weekly downloads
 
Created
Source

Upgrade

A simple Node websocket library to handle http upgrades.

Version 1.0.2


  • See: NPM Page
  • See: Github Repo

Installation

npm install upgrade


### Usage
###### Here is an example of a basic upgrade setup:
> ```JavaScript
// server.js
var http = require('http');
var upgrade = require('upgrade');
var server = http.createServer();
server.on('upgrade', function (req, socket) {
    var send = upgrade.getSend(socket);
    upgrade.writeHead(req, socket);
    socket.on('data', function (buff) {
        var data = upgrade.getData(buff);
        console.log('> ' + data);
    });
    send('Welcome to the Server!');
});
server.listen(8000);
// client.js
var socket = new WebSocket('ws://localhost:8000');
socket.onopen = function () {
    socket.send('Hello Server!');
    console.log('> Socket Open.');
};
socket.onmessage = function (evt) {
    var msg = evt.data;
    console.log('> ' + msg);
};

writeHead(req, socket)

Write upgrade handshake header to socket.

Parameters

  • {ServerRequest} req - ServerRequest from HTTPServer.
  • {Socket} socket - Socket from upgrade event.
server.on('upgrade', function (req, socket) {
    upgrade.writeHead(req, socket);

getData(buffer)

Removes mask from incoming frame.

Parameters

  • {Buffer} buffer - Buffer object from data event of WebSocket.

Returns {String} Unmasked data.

socket.on('data', function (buff) {
    var data = upgrade.getData(buff);

frameData(msg)

Wraps data in a websocket frame. Note that UpgradeJS does not support payloads larger than 125 bytes.

Parameters

  • {String} msg - Some data to wrap.

Returns {Buffer} Hex encoded Buffer.
Throws {RangeError}

var data = exports.frameData(message);
socket.write(data);

getSend(socket)

Convenience method to lock send behavior to a specific socket.

Parameters

  • {Socket} socket - The socket to communicate over.

Returns {Function} Send behavior using a specific socket.

var send = upgrade.getSend(socket);
send('foo');
send('bar');

send(msg, socket)

Convenience method for sending framed data.

Parameters

  • {String} msg - Data to send across websocket.
  • {Socket} socket - The socket to commincate over.

Throws {RangeError}

upgrade.send('foo', socket);
upgrade.send('bar', socket);

By Dan Cobb: cobbdb@gmail.com - petitgibier.sytes.net
License: MIT

Keywords

FAQs

Package last updated on 21 May 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc