From 5fbc750db7c8b3b0d4c68aea4a9a00a31c6b3e9a Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Sat, 20 Feb 2010 19:16:57 -0800 Subject: [PATCH] multipart no longer depends on Promise --- doc/api.txt | 21 ++++++++++++--------- lib/multipart.js | 14 ++++++++------ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/doc/api.txt b/doc/api.txt index dcdc66a966..e79f904a8f 100644 --- a/doc/api.txt +++ b/doc/api.txt @@ -1254,18 +1254,21 @@ Node. To use it, +require("multipart")+. + See the Stream class below. -+multipart.cat(message)+ :: - Returns a promise. - - on success: Returns a multipart.Stream object representing the completed - message. The body of each part is saved on the `body` member. - - on error: Returns an instanceof Error object. This indicates - that the message was malformed in some way. ++multipart.cat(message, callback)+ :: + On success, +callback+ is called with +(null, stream)+ where +stream+ is a + +multipart.Stream+ object representing the completed message. The body of + each part is saved on the `body` member. + - *Note*: This function saves the *entire* message into memory. As such, - it is ill-suited to parsing actual incoming messages from an HTTP request! + On error, +callback+ is called with +(err)+ where +err+ is an instanceof + the +Error+ object. This indicates that the message was malformed in some + way. + + + *Note*: This function saves the *entire* message into memory. As such, it + is ill-suited to parsing actual incoming messages from an HTTP request! If a user uploads a very large file, then it may cause serious problems. No checking is done to ensure that the file does not overload the memory. - Only use multipart.cat with known and trusted input! + Only use +multipart.cat+ with known and trusted input! + === +multipart.Stream+ diff --git a/lib/multipart.js b/lib/multipart.js index 9dff309ecb..54db1e93d4 100644 --- a/lib/multipart.js +++ b/lib/multipart.js @@ -37,9 +37,8 @@ function parse (message) { // rack up as much memory usage as they can manage. This function // buffers the whole message, which is very convenient, but also // very much the wrong thing to do in most cases. -function cat (message) { - var p = new (events.Promise), - stream = parse(message); +function cat (message, callback) { + var stream = parse(message); stream.files = {}; stream.fields = {}; stream.addListener("partBegin", function (part) { @@ -49,9 +48,12 @@ function cat (message) { stream.addListener("body", function (chunk) { stream.part.body = (stream.part.body || "") + chunk; }); - stream.addListener("error", function (e) { p.emitError(e) }); - stream.addListener("complete", function () { p.emitSuccess(stream) }); - return p; + stream.addListener("error", function (e) { p.emitError(e) + if (callback) callback(e); + }); + stream.addListener("complete", function () { + if (callback) callback(null, stream); + }); }; // events: