diff --git a/formidable/formidable-tests.ts b/formidable/formidable-tests.ts new file mode 100644 index 000000000..f39da3f8c --- /dev/null +++ b/formidable/formidable-tests.ts @@ -0,0 +1,81 @@ +/// + +import formidable = require('formidable'); +import http = require('http'); +import util = require('util'); + +http.createServer((req, res) => { + if (req.url == '/upload' && req.method.toLowerCase() == 'post') { + // parse a file upload + var form = new formidable.IncomingForm(); + + form.parse(req, (err, fields, files) => { + res.writeHead(200, {'content-type': 'text/plain'}); + res.write('received upload:\n\n'); + res.end(util.inspect({fields: fields, files: files})); + }); + + return; + } + + // show a file upload form + res.writeHead(200, {'content-type': 'text/html'}); + res.end( + '
'+ + '
'+ + '
'+ + ''+ + '
' + ); +}); + + +var form = new formidable.IncomingForm(); + +form.encoding = 'utf-8'; +form.uploadDir = '/my/dir'; +form.keepExtensions = false; +form.maxFieldsSize = 2 * 1024 * 1024; +form.maxFields = 1000; +// form.hash = false; TODO: Waiting on unions +form.hash = 'sha1'; +form.multiples = false; + +if (form.type === 'multipart') { +} +if (form.bytesReceived > 100) { +} +if (form.bytesExpected > 100) { +} + +var req: http.ServerRequest; + +form.parse(req); +form.parse(req, (err: any, fields: formidable.Fields, files: formidable.Files) => { + var key: string; + for (key in fields) { + console.log(key, '=', fields[key]); + } + + for (key in files) { + console.log('file', key, 'is', files[key].type); + } +}); + +form.onPart = function (part: formidable.Part) { + if (!part.filename) { + form.handlePart(part); + } +}; + +var file: formidable.File; + +file.size = 0; +file.path = '/tmp/whatever'; +file.name = 'a_file'; +file.type = 'application/json'; +file.lastModifiedDate = new Date(); +file.hash = '12345'; +JSON.stringify(file.toJSON()); + +form.on('progess', (bytesReceived: number, bytesExpected: number) => {}); diff --git a/formidable/formidable.d.ts b/formidable/formidable.d.ts new file mode 100644 index 000000000..bd35c757d --- /dev/null +++ b/formidable/formidable.d.ts @@ -0,0 +1,56 @@ +// Type definitions for Formidable 1.0.16 +// Project: https://github.com/felixge/node-formidable/ +// Definitions by: Wim Looman +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module "formidable" { + import http = require("http"); + import stream = require("stream"); + import events = require("events"); + + export class IncomingForm extends events.EventEmitter { + encoding: string; + uploadDir: string; + keepExtensions: boolean; + maxFieldsSize: number; + maxFields: number; + hash: string; + multiples: boolean; + type: string; + bytesReceived: number; + bytesExpected: number; + + onPart: (part: Part) => void; + + handlePart(part: Part): void; + parse(req: http.ServerRequest, callback?: (err: any, fields: Fields, files: Files) => any): void; + } + + export interface Fields { + [key: string]: string; + } + + export interface Files { + [key: string]: File; // | File[]; + } + + export interface Part extends stream.Stream { + headers: { [key: string]: string }; + name: string; + filename?: string; + mime?: string; + } + + export interface File { + size: number; + path: string; + name: string; + type: string; + lastModifiedDate?: Date; + hash?: string; + + toJSON(): Object; + } +}