Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
seems like not my version is working at the moment
JavaScript
branch: master

This branch is 6 commits ahead, 1 commit behind isaacs:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
README.md
claudiatest.jpg
example_file.js
example_old.js
example_stream.js
example_stream2.js
package.json
parse_log

README.md

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.

Notably, the unit tests for the writer are 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.addListener("onPartBegin", function(part) {
  doSomething(part)
});
parser.addListener("onData", function(chunk) {
  doSomethingElse(chunk)
});
parser.addListener("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.addListener("onData", function(chunk) {
  doSomething(chunk)
});
writer.addListener("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();
Something went wrong with that request. Please try again.