From 60d32f04f4fd8b6797d9a92e30616c04a8fdf51d Mon Sep 17 00:00:00 2001 From: Ken Fukuyama Date: Sat, 13 Dec 2014 22:09:05 +0900 Subject: [PATCH] multiparty definition file --- multiparty/multiparty-tests.ts | 72 +++++++++++++++++++++ multiparty/multiparty.d.ts | 111 +++++++++++++++++++++++++++++++++ 2 files changed, 183 insertions(+) create mode 100644 multiparty/multiparty-tests.ts create mode 100644 multiparty/multiparty.d.ts diff --git a/multiparty/multiparty-tests.ts b/multiparty/multiparty-tests.ts new file mode 100644 index 000000000..47e6a62f7 --- /dev/null +++ b/multiparty/multiparty-tests.ts @@ -0,0 +1,72 @@ +/// +/// +import multiparty = require('multiparty'); +import http = require('http'); +import util = require('util'); + +http.createServer(function (req: http.ServerRequest, res: http.ServerResponse) { + if (req.url === '/upload' && req.method === 'POST') { + var count = 0; + var form = new multiparty.Form(); + + // Errors may be emitted + // Note that if you are listening to 'part' events, the same error may be + // emitted from the `form` and the `part`. + form.on('error', function (err: Error) { + console.log('Error parsing form: ' + err); + }); + + // Parts are emitted when parsing the form + form.on('part', function (part: multiparty.Part) { + // You *must* act on the part by reading it + // NOTE: if you want to ignore it, just call "part.resume()" + + if (!!part.filename) { + // filename is exists when this is a file + count++; + console.log('got field named ' + part.name + ' and got file named ' + part.filename); + // ignore file's content here + part.resume(); + } else { + // filename doesn't exist when this is a field and not a file + console.log('got field named ' + part.name); + // ignore field's content + part.resume(); + } + + part.on('error', function (err: Error) { + // decide what to do + console.log('Error on part event: ' + err); + }); + }); + + form.on('progress', function (bytesReceived: number, bytesExpected: number) { + // decide what to do + console.log('BytesReceived: ' + bytesReceived, 'BytesExpected: ', bytesExpected); + }); + + form.on('field', function (name: string, value: string) { + // decide what to do + console.log('Field Name: ' + name + ', Field Value: ' + value); + }); + + // Close emitted after form parsed + form.on('close', function () { + console.log('Upload completed!'); + res.end('Received ' + count + ' files'); + }); + + // Parse req + form.parse(req); + } + + // show a file upload form + res.writeHead(200, {'content-type': 'text/html'}); + res.end( + '
' + + '
' + + '
' + + '' + + '
' + ); +}).listen(8080); \ No newline at end of file diff --git a/multiparty/multiparty.d.ts b/multiparty/multiparty.d.ts new file mode 100644 index 000000000..1ef97c94c --- /dev/null +++ b/multiparty/multiparty.d.ts @@ -0,0 +1,111 @@ +// Type definitions for node-multiparty +// Project: https://github.com/andrewrk/node-multiparty +// Definitions by: Ken Fukuyama +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module "multiparty" { + import http = require('http'); + import events = require('events'); + import stream = require('stream'); + + export class Form extends events.EventEmitter { + constructor(options?: FormOptions); + /** + * Parses an incoming node.js request containing form data. + * This will cause form to emit events based off the incoming request + * @param request + * @param callback + */ + parse(request: http.ServerRequest, callback?: (error: Error, fields: any, files: any) => any): void; + } + + export interface File { + /** + * same as name - the field name for this file + */ + fieldName: string; + /** + * the filename that the user reports for the file + */ + originalFileName: string; + /** + * the absolute path of the uploaded file on disk + */ + path: string; + /** + * the HTTP headers that were sent along with this file + */ + headers: any; + /** + * size of the file in bytes + */ + size: number; + } + + interface Part extends stream.Readable { + /** + * the headers for this part. For example, you may be interested in content-type + */ + headers: any; + /** + * the field name for this part + */ + name: string; + /** + * only if the part is an incoming file + */ + filename: string; + /** + * the byte offset of this part in the request body + */ + byteOffset: number; + /** + * assuming that this is the last part in the request, this is the size of this part in bytes. + * You could use this, for example, to set the Content-Length header if uploading to S3. + * If the part had a Content-Length header then that value is used here instead. + */ + byteCount: number; + } + + export interface FormOptions { + /** + * sets encoding for the incoming form fields. Defaults to utf8. + */ + encoding?:string; + /** + * Limits the amount of memory all fields (not files) can allocate in bytes. + * If this value is exceeded, an error event is emitted. The default size is 2MB. + */ + maxFieldsSize?:number; + /** + * Limits the number of fields that will be parsed before emitting an error event. + * A file counts as a field in this case. Defaults to 1000. + */ + maxFields?:number; + /** + * Only relevant when autoFiles is true. + * Limits the total bytes accepted for all files combined. + * If this value is exceeded, an error event is emitted. + * The default is Infinity. + */ + maxFilesSize?:number; + /** + * Enables field events and disables part events for fields. + * This is automatically set to true if you add a field listener. + */ + autoFields?:boolean; + /** + * Enables file events and disables part events for files. + * This is automatically set to true if you add a file listener. + */ + autoFiles?:boolean; + /** + * Only relevant when autoFiles is true. + * The directory for placing file uploads in. + * You can move them later using fs.rename(). Defaults to os.tmpDir(). + */ + uploadDir?:string; + } +} \ No newline at end of file