/** * Determine if the given buffer is utf8. * @param {Buffer} buffer */ export default function isBufferUtf8Representable(buffer) { const utfEncodedBuffer = buffer.toString('utf8'); const reconstructedBuffer = Buffer.from(utfEncodedBuffer, 'utf8'); return reconstructedBuffer.equals(buffer); }
describe('#getFileStream', function getFileStreamSuite() { this.timeout(timeout); before(function before() { // utils.handleRatelimit(bot, 'getFileStream', this); }); it('should get a file stream', function test(done) { const fileStream = bot.getFileStream(FILE_ID); assert.ok(fileStream instanceof stream.Readable); assert.equal(fileStream.path, FILE_ID); fileStream.on('info', (info) => { assert.ok(info); assert.ok(utils.isTelegramFileURI(info.uri), `${info.uri} is not a file URI`); fileStream.pipe(concat(function readFile(buffer) { buffer.equals(fs.readFileSync(FILE_PATH)); // sync :( return done(); })); }); }); });
const isAbortMarker = ({ key }) => { // Handle null/undefined keys. if (!key) return false // Cast key to buffer defensively return Buffer.from(key).equals(ABORTED_MESSAGE_KEY) }
serverInUdp({ buffer, fail }) { const userKey = this.readProperty('ss-stream-cipher', 'key'); const payload = buffer.slice(0, -8); // const uid = buffer.slice(-8, -4); const packet_hmac = buffer.slice(-4); const packet_hmac_calc = this.createHmac(buffer.slice(0, -4), userKey).slice(0, 4); if (!packet_hmac_calc.equals(packet_hmac)) { return fail(`unexpected hmac when verify client udp packet, dump=${dumpHex(buffer)}`); } return payload; }
async _verifyFile (filePath) { const patchLocation = path.join(this._downloadPath, filePath); const patchTarget = path.join(path.join(__dirname, `..`), filePath); const patchBuffer = await FileExists(patchLocation) ? await fs.promises.readFile(patchLocation) : Buffer.alloc(0); const patchedBuffer = await FileExists(patchTarget) ? await fs.promises.readFile(patchTarget) : Buffer.alloc(0); return patchBuffer.equals(patchedBuffer); }
function testStream(method, bufferFile, resultFile, t, params) { const writeStream = new BufferWriter(); createReadStream(join(__dirname, '/fixtures/', bufferFile)) .pipe(method(params)) .pipe(writeStream); writeStream.on('finish', function() { const result = readFileSync(join(__dirname, '/fixtures/', resultFile)); t.truthy(writeStream.data.equals(result)); t.end(); }); }
it('Time', () => { const buffer = Buffer.alloc(16); const compBuf = Buffer.alloc(16); expect(serialize.time({secs: 1234560, nsecs:565600}, buffer, 0)).to.equal(8); compBuf.writeInt32LE(1234560, 0); compBuf.writeInt32LE(565600, 4); expect(buffer.equals(compBuf)).to.be.true; expect(serialize.time({secs: -89855022, nsecs:14545}, buffer, 8)).to.equal(16); compBuf.writeInt32LE(-89855022, 8); compBuf.writeInt32LE(14545, 12); expect(buffer.equals(compBuf)).to.be.true; });
describe('AdvancedBuffer#clear', function() { it('should throw when pass a non-buffer to put() ', function() { const buffer = new AdvancedBuffer({ getPacketLength: () => 0 }); buffer.put(Buffer.from([0x00])); buffer.clear(); expect(buffer.final().equals(Buffer.alloc(0))).toBe(true); }); });
it('Uint32 Array', () => { const buffer = Buffer.alloc(16); const compBuf = Buffer.alloc(16); expect(serialize.Array.uint32([0, 65534], buffer, 0, -1)).to.equal(12); compBuf.writeUInt32LE(2); compBuf.writeUInt32LE(0, 4); compBuf.writeUInt32LE(65534, 8); expect(buffer.equals(compBuf)).to.be.true; expect(serialize.Array.uint32([99], buffer, 12, 1)).to.equal(16); compBuf.writeUInt32LE(99, 12); expect(buffer.equals(compBuf)).to.be.true; });
clientInUdp({ buffer, fail }) { const userKey = this.readProperty('ss-stream-cipher', 'key'); const payload = buffer.slice(0, -4); const payload_hmac = buffer.slice(-4); const payload_hmac_calc = this.createHmac(payload, userKey).slice(0, 4); if (!payload_hmac_calc.equals(payload_hmac)) { return fail(`unexpected hmac when verify server udp packet, dump=${dumpHex(buffer)}`); } return payload; }
it('Time Array', () => { const buffer = Buffer.alloc(28); const compBuf = Buffer.alloc(28); expect(serialize.Array.time([{secs: 1234560, nsecs:565600}, {secs: 0, nsecs: 0}], buffer, 0, -1)).to.equal(20); compBuf.writeUInt32LE(2, 0); compBuf.writeInt32LE(1234560, 4); compBuf.writeInt32LE(565600, 8); expect(buffer.equals(compBuf)).to.be.true; expect(serialize.Array.time([{secs: -89855022, nsecs:14545}], buffer, 20, 1)).to.equal(28); compBuf.writeInt32LE(-89855022, 20); compBuf.writeInt32LE(14545, 24); expect(buffer.equals(compBuf)).to.be.true; });
/** * Determine if the given buffer is utf8. * @param {Buffer} buffer */ export default function isBufferUtf8Representable(buffer) { const utfEncodedBuffer = buffer.toString('utf8'); const reconstructedBuffer = Buffer.from(utfEncodedBuffer, 'utf8'); return reconstructedBuffer.equals(buffer); }