Git Product home page Git Product logo

multipart-js's Introduction

multipart-js

A JavaScript library for parsing and writing multipart messages.

Current State

Pre-pre-alpha. Almost nothing is here, and what is here is likely completely broken.

Usage

If you're familiar with sax-js, then most of this should be pretty straightforward. Attach event handlers, call functions, close it when you're done. Please keep fingers and dangling clothing away from the state machine.

var multipart = require("multipart");

// parsing
var parser = multipart.parser();

// in all event handlers, "this" is the parser, and "this.part" is the
// part that's currently being dealt with.
parser.onpartbegin = function (part) { doSomething(part) };
parser.ondata = function (chunk) { doSomethingElse(chunk) };
parser.onend = function () { closeItUp() };

// now start feeding the message through it.
// you can do this all in one go, if you like, or one byte at a time,
// or anything in between.
parser.boundary = "foo";
var chunk;
while ( chunk = upstreamThing.getNextChunk() ) {
  parser.write(chunk);
}
parser.close();


// writing
var writer = multipart.writer();

// attach event handlers for the things we care about.
writer.ondata = function (chunk) { doSomething(chunk) };
writer.onend = function () { closeItUp() };

// now trigger the events to fire by feeding files through it.
writer.boundary = "foo";
writer.partBegin({ "content-type" : "text/plain", filename : "foo.txt" });
var chunk;
while ( chunk = getChunkOfFile() ) {
  writer.write(chunk);
}
writer.partEnd();
writer.close();

multipart-js's People

Contributors

isaacs avatar mrjjwright avatar

Watchers

 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.