This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
605 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ local-production.yml | |
.cache*/ | ||
Dockerfile* | ||
.dockerignore | ||
diagnostics/ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
const { Writable } = require('stream'); | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
const uuid = require('uuid'); | ||
const consts = require('../constants'); | ||
const helpers = require('../helpers'); | ||
|
||
const kRecordBufferSize = 1024 * 1024 * 10; | ||
|
||
class ClientStreamRecorder extends Writable { | ||
constructor(options) { | ||
super(options); | ||
this._sessionId = options.sessionId || uuid.v4(); | ||
this._saveDir = options.saveDir || '.'; | ||
this._bufferSize = options.bufferSize || kRecordBufferSize; | ||
this._bufferPos = 0; | ||
this._isRecording = false; | ||
this._buffer = Buffer.allocUnsafe(this._bufferSize); | ||
|
||
this.on('unpipe', () => { | ||
this._finish_recording() | ||
.then(() => this._flush_buffer()) | ||
.then(() => this.emit('flushed')); | ||
}); | ||
} | ||
|
||
/** | ||
* | ||
* @returns {string} | ||
*/ | ||
get dataPath() { | ||
return path.join(this._saveDir, this._sessionId); | ||
} | ||
|
||
_write(chunk, encoding, callback) { | ||
this._record_chunk(chunk).then(() => { | ||
callback(); | ||
}); | ||
} | ||
|
||
async _record_chunk(chunk) { | ||
this._isRecording = true; | ||
|
||
let slice = chunk; | ||
if(this._bufferPos + slice.length > this._bufferSize) { | ||
slice = chunk.slice(0, this._bufferSize - this._bufferPos); | ||
} | ||
|
||
slice.copy(this._buffer, this._bufferPos, 0, slice.length); | ||
this._bufferPos += slice.length; | ||
if(this._bufferPos === this._bufferSize) { | ||
await this._flush_buffer(); | ||
} | ||
|
||
if(slice.length < chunk.length) { | ||
await this._record_chunk(chunk.slice(slice.length)); | ||
} | ||
|
||
this._isRecording = false; | ||
this.emit('_record_chunk'); | ||
} | ||
|
||
async _flush_buffer() { | ||
await fs.ensureDir(this._saveDir); | ||
|
||
// Normalize the version size so it will be correctly parsed when streamed to a server. | ||
let zeroPad = 0; | ||
if(!await fs.pathExists(this.dataPath)) { | ||
for (let i = consts.PROTOCOL_VERSION_MIN_SIZE; i <= consts.VERSION_SIZE; i++) { | ||
zeroPad = consts.VERSION_SIZE - i; | ||
if (helpers.readUInt32(this._buffer.slice(0, i)) === consts.PROTOCOL_VERSION) break; | ||
} | ||
} | ||
|
||
const fd = await fs.open(this.dataPath, 'a'); | ||
if(zeroPad > 0) await fs.write(fd, Buffer.alloc(zeroPad, '0', 'ascii'), 0); | ||
await fs.write(fd, this._buffer, 0, this._bufferPos); | ||
await fs.close(fd); | ||
this._bufferPos = 0; | ||
} | ||
|
||
async _finish_recording() { | ||
if(!this._isRecording) return; | ||
|
||
return new Promise(resolve => { | ||
this.once('_record_chunk', () => resolve()); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = ClientStreamRecorder; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.