You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.9 KiB

const WebSocketStream = require('websocket-stream')
const ConcatStream = require('concat-stream')
const { Duplex, Readable } = require('stream')
const { Buffer } = require('buffer')
class Streamer {
constructor(socket_uuid, node_uuid, length = 4096, position = 0) {
this.socket_uuid = socket_uuid
this.node_uuid = node_uuid
this.length = length
this.position = position
}
write(buffer) {
console.log('write buffer pre slice', buffer)
// buffer = buffer.slice(this.position, this.position + this.length)
// console.log('write buffer post slice', buffer)
const write_stream = WebSocketStream(`ws://localhost:5746/?socket_uuid=${this.socket_uuid}&node_uuid=${this.node_uuid}&length=${this.length}&position=${this.position}&writing_file=true`, {
perMessageDeflate: false,
binary: true,
})
console.log(write_stream)
console.log('writing buffer', buffer.toString(), buffer)
const read_stream = new Readable()
read_stream.push(buffer.toString())
read_stream.push(null)
read_stream.pipe(write_stream)
}
stream() {
this.ws = WebSocketStream(`ws://localhost:5746/?socket_uuid=${this.socket_uuid}&node_uuid=${this.node_uuid}&length=${this.length}&position=${this.position}`, {
perMessageDeflate: false,
binary: true,
})
return this.ws
}
async buffer() {
return this._bufferStream(this.stream())
}
_bufferStream(stream) {
const chunks = []
return new Promise((resolve, reject) => {
stream.on('data', chunk => chunks.push(chunk))
stream.on('error', reject)
stream.on('end', () => resolve(Buffer.concat(chunks)))
})
}
close() {
this.ws.close()
}
}
module.exports = exports = Streamer