Merge pull request #2140 from liorm/master

Added streaming function to express request object (mimic the response interfaces).
This commit is contained in:
Masahiro Wakame
2014-05-08 23:00:21 +09:00
4 changed files with 107 additions and 1 deletions
+1
View File
@@ -88,6 +88,7 @@ All definitions files include a header with the author and editors, so at some p
* [GoJS](http://gojs.net/) (by [Barbara Duckworth](https://github.com/barbara42))
* [Greasemonkey](http://www.greasespot.net/) (by [Kota Saito](https://github.com/kotas))
* [GreenSock Animation Platform (GSAP)](http://www.greensock.com/get-started-js/) (by [Robert S.](https://github.com/codeBelt))
* [gridfs-stream](https://github.com/aheckmann/gridfs-stream) (by [Lior Mualem](https://github.com/liorm))
* [Grunt JS](http://gruntjs.com/) (by [Jeff May](https://github.com/jeffmay), [Basarat Ali Syed](https://github.com/basarat) and [San Chen](https://github.com/bigsan))
* [Google API Client](https://code.google.com/p/google-api-javascript-client/) (by [Frank M](https://github.com/sgtfrankieboy))
* [Google App Engine Channel API](https://developers.google.com/appengine/docs/java/channel/javascript) (by [vvakame](https://github.com/vvakame))
+1 -1
View File
@@ -240,7 +240,7 @@ declare module "express" {
count: number;
}
interface Request extends Express.Request {
interface Request extends http.ServerRequest, Express.Request {
session: Session;
+37
View File
@@ -0,0 +1,37 @@
/// <reference path="gridfs-stream.d.ts" />
/// <reference path="../mongodb/mongodb.d.ts" />
/// <reference path="../node/node.d.ts" />
// Samples from:
// https://github.com/aheckmann/gridfs-stream
import fs = require('fs');
import mongo = require('mongodb');
import Grid = require('gridfs-stream');
// create or use an existing mongodb-native db instance
var db = new mongo.Db('gridfs-stream-test', new mongo.Server("127.0.0.1", 27017));
var gfs = Grid(db, mongo);
// streaming to gridfs
var writestream = gfs.createWriteStream({
filename: 'my_file.txt'
});
fs.createReadStream('blob.txt').pipe(writestream);
// streaming from gridfs
var readstream = gfs.createReadStream({
filename: 'my_file.txt'
});
//error handling, e.g. file does not exist
readstream.on('error', function (err: any) {
console.log('An error occurred!', err);
throw err;
});
var bufs: Array<any> = [];
readstream.on('data', function(d: any){ bufs.push(d); });
readstream.on('end', function() {
var buf = Buffer.concat(bufs);
});
+68
View File
@@ -0,0 +1,68 @@
// Type definitions for gridfs-stream 0.5.0
// Project: https://github.com/aheckmann/gridfs-stream
// Definitions by: Lior Mualem <https://github.com/liorm/>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/// <reference path="../node/node.d.ts" />
/// <reference path="../mongodb/mongodb.d.ts" />
declare module GridFSStream {
export interface Range {
startPos: number;
endPos: number;
}
export interface Options {
_id?: string;
filename?: string;
mode?: string;
range?: Range;
// any other options from the GridStore may be passed too, e.g.
chunkSize?: number;
content_type?: string;
root?: string;
metadata?: any;
}
export interface WriteStream extends NodeJS.WritableStream {
writable: boolean;
name: string;
id: string;
options: Options;
mode: string;
}
export interface ReadStream extends NodeJS.ReadableStream {
readable: boolean;
paused: boolean;
}
}
declare module "gridfs-stream" {
import mongo = require('mongodb');
// Merged declaration, g is both a callable function and a namespace
function g(db: any, mongo: any): g.Grid;
module g {
export class Grid {
files: mongo.Collection;
collection(name?: string): mongo.Collection;
createWriteStream(options?: GridFSStream.Options): GridFSStream.WriteStream;
createReadStream(options?: GridFSStream.Options): GridFSStream.ReadStream;
createWriteStream(options?: string): GridFSStream.WriteStream;
createReadStream(options?: string): GridFSStream.ReadStream;
remove(options: GridFSStream.Options, callback: (err: Error) => void): void;
exist(options: GridFSStream.Options, callback: (err: Error, found: boolean) => void): void;
}
}
export = g;
}