Skip to content

Commit

Permalink
Added a middleware for the 'SimpleVirtualStoredContentManager' class
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed Jun 4, 2017
1 parent 2786bea commit a322b75
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
7 changes: 6 additions & 1 deletion lib/manager/VirtualStoredFSManager.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ export declare abstract class VirtualStoredContentManager implements IVirtualSto
allocate(callback: ReturnCallback<string>): any;
allocate(options: any, callback: ReturnCallback<string>): 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<Readable>): void;
write(contentUid: string, _callback: ReturnCallback<Writable>): void;
Expand Down
23 changes: 18 additions & 5 deletions lib/manager/VirtualStoredFSManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
24 changes: 21 additions & 3 deletions src/manager/VirtualStoredFSManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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));
}
})
}

Expand All @@ -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));
}
})
}

Expand Down

0 comments on commit a322b75

Please sign in to comment.