From a322b75bdf7950eeba1544e7bbc504a9cf684448 Mon Sep 17 00:00:00 2001 From: Adrien Castex Date: Sun, 4 Jun 2017 14:48:09 +0200 Subject: [PATCH] Added a middleware for the 'SimpleVirtualStoredContentManager' class --- lib/manager/VirtualStoredFSManager.d.ts | 7 ++++++- lib/manager/VirtualStoredFSManager.js | 23 ++++++++++++++++++----- src/manager/VirtualStoredFSManager.ts | 24 +++++++++++++++++++++--- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/lib/manager/VirtualStoredFSManager.d.ts b/lib/manager/VirtualStoredFSManager.d.ts index ec13b1c6..94cbc729 100644 --- a/lib/manager/VirtualStoredFSManager.d.ts +++ b/lib/manager/VirtualStoredFSManager.d.ts @@ -20,12 +20,17 @@ export declare abstract class VirtualStoredContentManager implements IVirtualSto allocate(callback: ReturnCallback): any; allocate(options: any, callback: ReturnCallback): any; } +export interface IVirtualStoredContentManagerMiddleware { + readStream(stream: Readable, callback: (stream: Readable) => void): any; + writeStream(stream: Writable, callback: (stream: Writable) => void): any; +} export declare class SimpleVirtualStoredContentManager extends VirtualStoredContentManager { storeFolderPath: string; + middleware: IVirtualStoredContentManagerMiddleware; initialized: boolean; uid: string; cid: number; - constructor(storeFolderPath: string); + constructor(storeFolderPath: string, middleware?: IVirtualStoredContentManagerMiddleware); initialize(callback: (error: Error) => void): void; read(contentUid: string, _callback: ReturnCallback): void; write(contentUid: string, _callback: ReturnCallback): void; diff --git a/lib/manager/VirtualStoredFSManager.js b/lib/manager/VirtualStoredFSManager.js index b29c8493..ce3dcf39 100644 --- a/lib/manager/VirtualStoredFSManager.js +++ b/lib/manager/VirtualStoredFSManager.js @@ -36,9 +36,10 @@ var VirtualStoredContentManager = (function () { exports.VirtualStoredContentManager = VirtualStoredContentManager; var SimpleVirtualStoredContentManager = (function (_super) { __extends(SimpleVirtualStoredContentManager, _super); - function SimpleVirtualStoredContentManager(storeFolderPath) { + function SimpleVirtualStoredContentManager(storeFolderPath, middleware) { var _this = _super.call(this) || this; _this.storeFolderPath = storeFolderPath; + _this.middleware = middleware; _this.initialized = false; _this.uid = 'SimpleVirtualStoredContentManager_1.3.3'; _this.cid = 0; @@ -65,21 +66,33 @@ var SimpleVirtualStoredContentManager = (function (_super) { }); }; SimpleVirtualStoredContentManager.prototype.read = function (contentUid, _callback) { + var _this = this; var callback = function (_1, _2) { return process.nextTick(function () { return _callback(_1, _2); }); }; fs.open(path.join(this.storeFolderPath, contentUid), 'r', function (e, fd) { if (e) callback(e, null); - else - callback(null, fs.createReadStream(null, { fd: fd })); + else { + var stream = fs.createReadStream(null, { fd: fd }); + if (!_this.middleware) + callback(null, stream); + else + _this.middleware.readStream(stream, function (s) { return callback(null, s); }); + } }); }; SimpleVirtualStoredContentManager.prototype.write = function (contentUid, _callback) { + var _this = this; var callback = function (_1, _2) { return process.nextTick(function () { return _callback(_1, _2); }); }; fs.open(path.join(this.storeFolderPath, contentUid), 'w', function (e, fd) { if (e) callback(e, null); - else - callback(null, fs.createWriteStream(null, { fd: fd })); + else { + var stream = fs.createWriteStream(null, { fd: fd }); + if (!_this.middleware) + callback(null, stream); + else + _this.middleware.writeStream(stream, function (s) { return callback(null, s); }); + } }); }; SimpleVirtualStoredContentManager.prototype._allocate = function (options, _callback) { diff --git a/src/manager/VirtualStoredFSManager.ts b/src/manager/VirtualStoredFSManager.ts index fae83bea..d7b061af 100644 --- a/src/manager/VirtualStoredFSManager.ts +++ b/src/manager/VirtualStoredFSManager.ts @@ -51,13 +51,19 @@ export abstract class VirtualStoredContentManager implements IVirtualStoredConte } } +export interface IVirtualStoredContentManagerMiddleware +{ + readStream(stream : Readable, callback : (stream : Readable) => void); + writeStream(stream : Writable, callback : (stream : Writable) => void); +} + export class SimpleVirtualStoredContentManager extends VirtualStoredContentManager { initialized : boolean = false; uid : string = 'SimpleVirtualStoredContentManager_1.3.3'; cid : number = 0; - constructor(public storeFolderPath : string) + constructor(public storeFolderPath : string, public middleware ?: IVirtualStoredContentManagerMiddleware) { super(); } @@ -95,7 +101,13 @@ export class SimpleVirtualStoredContentManager extends VirtualStoredContentManag if(e) callback(e, null); else - callback(null, fs.createReadStream(null, { fd })); + { + const stream = fs.createReadStream(null, { fd }); + if(!this.middleware) + callback(null, stream); + else + this.middleware.readStream(stream, (s) => callback(null, s)); + } }) } @@ -107,7 +119,13 @@ export class SimpleVirtualStoredContentManager extends VirtualStoredContentManag if(e) callback(e, null); else - callback(null, fs.createWriteStream(null, { fd })); + { + const stream = fs.createWriteStream(null, { fd }); + if(!this.middleware) + callback(null, stream); + else + this.middleware.writeStream(stream, (s) => callback(null, s)); + } }) }