Skip to content

Commit

Permalink
Replaced the middleware 'stream.Writable' into a 'stream.Transform'
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed Jun 4, 2017
1 parent 25d120b commit 36bc500
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
14 changes: 5 additions & 9 deletions lib/resource/virtualStored/VirtualStoredFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,18 @@ var VirtualStoredFile = (function (_super) {
return;
}
var size = 0;
var wr = new stream_1.Writable({
write: function (chunk, encoding, cb) {
var wr = new stream_1.Transform({
transform: function (chunk, encoding, cb) {
size += chunk.length;
return w.write(chunk, encoding, cb);
this.push(chunk);
cb();
}
});
wr.on('finish', function () {
_this.updateLastModified();
_this.len = size;
});
function emit(name) {
wr.on(name, function (_1, _2) { return w.emit(name, _1, _2); });
}
emit('close');
emit('error');
emit('finish');
wr.pipe(w);
callback(null, wr);
});
};
Expand Down
18 changes: 7 additions & 11 deletions src/resource/virtualStored/VirtualStoredFile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IResource, SimpleCallback, ReturnCallback, ResourceType } from '../IResource'
import { Readable, Writable } from 'stream'
import { Readable, Writable, Transform } from 'stream'
import { VirtualStoredResource } from './VirtualStoredResource'
import { FSManager } from '../../manager/FSManager'
import { VirtualStoredFSManager } from '../../manager/VirtualStoredFSManager'
Expand Down Expand Up @@ -55,24 +55,20 @@ export class VirtualStoredFile extends VirtualStoredResource

let size = 0;

const wr = new Writable({
write: (chunk, encoding, cb) => {
const wr = new Transform({
transform(chunk, encoding, cb)
{
size += chunk.length;
return w.write(chunk, encoding, cb);
this.push(chunk);
cb();
}
});
wr.on('finish', () => {
this.updateLastModified();
this.len = size;
})

function emit(name : string)
{
wr.on(name, (_1, _2) => w.emit(name, _1, _2));
}
emit('close');
emit('error');
emit('finish');
wr.pipe(w);

callback(null, wr);
});
Expand Down

0 comments on commit 36bc500

Please sign in to comment.