node-minecraft-protocol icon indicating copy to clipboard operation
node-minecraft-protocol copied to clipboard

Refactoring Client to extend Stream.

Open plexigras opened this issue 8 years ago • 6 comments

I think client should be build on top of a stream.

so you can do ~~client.pipe(fs.createWriteStream('protocol.log'))~~

client.pipe(new Transform({ writableObjectMode: true, transform(chunk, enc, cb) {
  this.push(JSON.stringify(chunk,null,2);
  cb();
}})).pipe(fs.createWriteStream('protocol.log'));

stream -> [ serializer ] -> [ compressor ] -> framer -> [ cipher ] -> socket socket -> [ decipher ] -> splitter -> [ decompressor ] -> deseralizer -> stream

plexigras avatar Jul 14 '17 09:07 plexigras

What should the data event be ? the packet event ?

On July 14, 2017 11:39:57 AM GMT+02:00, plexigras [email protected] wrote:

I think client should be build on top of a stream.

so i can do client.pipe(fs.createWriteStream('protocol.log'))

stream -> [ serializer ] -> [ compressor ] -> framer -> [ cipher ] -> socket socket-> [ decipher ] -> splitter -> [ decompressor ] -> deseralizer -> stream

-- You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub: https://github.com/PrismarineJS/node-minecraft-protocol/issues/504

-- Sent from my Android device with K-9 Mail. Please excuse my brevity.

roblabla avatar Jul 14 '17 10:07 roblabla

also' it won't work, because the other end of the pipe expects a string or a buffer, but we send an object. I think this would be better handled by having the user define his own stream from the nmp events.

On July 14, 2017 11:39:57 AM GMT+02:00, plexigras [email protected] wrote:

I think client should be build on top of a stream.

so i can do client.pipe(fs.createWriteStream('protocol.log'))

stream -> [ serializer ] -> [ compressor ] -> framer -> [ cipher ] -> socket socket-> [ decipher ] -> splitter -> [ decompressor ] -> deseralizer -> stream

-- You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub: https://github.com/PrismarineJS/node-minecraft-protocol/issues/504

-- Sent from my Android device with K-9 Mail. Please excuse my brevity.

roblabla avatar Jul 14 '17 10:07 roblabla

the data would be whatever comes out of the deserializer

plexigras avatar Jul 14 '17 10:07 plexigras

the deserializer gives objects. IIRC, you can't pipe objects to a non-object stream. You'd have to serialize it to JSON. That's the point I was trying to make ^^'

roblabla avatar Jul 17 '17 12:07 roblabla

well the serializer readable part is object mode https://github.com/ProtoDef-io/node-protodef/blob/master/src/serializer.js#L5 I don't think that's a problem.

The difference between the deserializer and your "stream" is only that that "stream" would handle all the states, hence the user of the stream wouldn't need to care about states.

That just means piping and unpiping the serializer and deserializer to that stream on change of state.

I guess we could then just listen on data of that stream to emit packet event, and write on that stream instead of writing directly on the serializer (for nmp client and server implementation). And expose the stream for people that want to use it directly.

rom1504 avatar Jul 17 '17 13:07 rom1504

Ah now I see @roblabla point. Yes you still won't be able to do

client.pipe(fs.createWriteStream('protocol.log'))

directly.

But you will be able to do

client.pipe(new Transform({writableObjectMode: true,transform(chunk,enc,cb){this.push(JSON.stringify(chunk,null,2);cb();}})).pipe(fs.createWriteStream('protocol.log'))

rom1504 avatar Jul 17 '17 13:07 rom1504