writeVarIntString(value) { if (value == null) { this.writeVarInt(-1) return this } const byteLength = Buffer.byteLength(value, 'utf8') this.writeVarInt(byteLength) this.ensureAvailable(byteLength) this.buf.write(value, this.offset, byteLength, 'utf8') this.offset += byteLength return this }
writeBytes(value) { if (value == null) { this.writeInt32(-1) return this } if (Buffer.isBuffer(value)) { // raw bytes this.ensureAvailable(INT32_SIZE + value.length) this.writeInt32(value.length) this.writeBufferInternal(value) } else { const valueToWrite = String(value) const byteLength = Buffer.byteLength(valueToWrite, 'utf8') this.ensureAvailable(INT32_SIZE + byteLength) this.writeInt32(byteLength) this.buf.write(valueToWrite, this.offset, byteLength, 'utf8') this.offset += byteLength } return this }
test('will throw an error on invalid public key on asymmetric encryption verification', async () => { const privateKey = readFileSync(`${__dirname}/assets/private-key.pem`); const publicKey = readFileSync(`${__dirname}/assets/public-key.pem`); publicKey.write('ASDF', 100); // Make the key invalid while keeping correct format await expectHttpError( withPostGraphileContext( { pgPool, jwtToken: jwt.sign({ aud: 'postgraphile' }, privateKey, { noTimestamp: true, algorithm: 'RS256', }), jwtPublicKey: publicKey, jwtVerifyOptions: { algorithms: ['RS256'], }, }, () => {}, ), 403, 'invalid signature', ); expect(pgClient.query.mock.calls).toEqual([]); });
writeVarIntBytes(value) { if (value == null) { this.writeVarInt(-1) return this } if (Buffer.isBuffer(value)) { // raw bytes this.writeVarInt(value.length) this.writeBufferInternal(value) } else { const valueToWrite = String(value) const byteLength = Buffer.byteLength(valueToWrite, 'utf8') this.writeVarInt(byteLength) this.ensureAvailable(byteLength) this.buf.write(valueToWrite, this.offset, byteLength, 'utf8') this.offset += byteLength } return this }
writeString(value) { if (value == null) { this.writeInt16(-1) return this } const byteLength = Buffer.byteLength(value, 'utf8') this.ensureAvailable(INT16_SIZE + byteLength) this.writeInt16(byteLength) this.buf.write(value, this.offset, byteLength, 'utf8') this.offset += byteLength return this }
setInterval(function() { var dummy = Buffer.allocUnsafe(1024 * 1024); dummy.write('hello'); dummy.toString()[0]; }, 100)
args.forEach(a => { this.view.setUint32(coffset, offset, true); coffset += 4; offset += Buffer.from(this.memory.buffer).write(`${a}\0`, offset); });
writeString(str, length) { const bufLen = length === undefined ? str.length : length; this._offset += this._buf.write(str, this._offset, bufLen); }
function encode(msg) { const str = JSON.stringify(msg); const len = Buffer.byteLength(str); const buf = new Buffer(len + 8); buf.writeUInt32LE(msg.id, 0, true); buf.writeUInt32LE(len, 4, true); buf.write(str, 8); return buf; }
Object.entries(env).forEach(([key, value]) => { this.view.setUint32(coffset, offset, true); coffset += 4; offset += Buffer.from(this.memory.buffer).write( `${key}=${value}\0`, offset ); });
it('throws on opening an index file with wrong metadata size', function() { const indexFile = dataDirectory + '/.index'; const metadataBuffer = Buffer.allocUnsafe(8 + 4); metadataBuffer.write("nesidx01", 0, 8, 'utf8'); metadataBuffer.writeUInt32BE(0, 8); fs.writeFileSync(indexFile, metadataBuffer); expect(() => index = new Index(indexFile)).to.throwError(/Invalid metadata size/); });
function createString(str) { const strlen128 = uleb128.encode(str.length); const strBuf = Buffer.alloc(1 + strlen128.length + str.length); strBuf[0] = 0x0b; for (let i = 0; i < strlen128.length; i++) strBuf[i + 1] = strlen128[i]; strBuf.write(str, 1 + strlen128.length); return strBuf; }
/** * Write a string to the packet buffer. * * @this {OutgoingPacket} * @param value string value to write. */ writeString(value) { const buf = Buffer.alloc(value.length + 2); buf.writeInt16BE(value.length, 0); buf.write(value, 2); this._buffer.writeBytes(buf); }
it('throws on opening an index file with too large metadata size', function() { const indexFile = dataDirectory + '/.index'; const metadataBuffer = Buffer.allocUnsafe(8 + 4 + 3); metadataBuffer.write("nesidx01", 0, 8, 'utf8'); metadataBuffer.writeUInt32BE(255, 8); metadataBuffer.write("{}\n", 12, 3, 'utf8'); fs.writeFileSync(indexFile, metadataBuffer); expect(() => index = new Index(indexFile)).to.throwError(/Invalid index file/); });
it('throws on opening an index file with invalid metadata', function() { const indexFile = dataDirectory + '/.index'; const metadataBuffer = Buffer.allocUnsafe(8 + 4 + 3); metadataBuffer.write("nesidx01", 0, 8, 'utf8'); metadataBuffer.writeUInt32BE(255, 8); metadataBuffer.write("{x$", 12, 3, 'utf8'); fs.writeFileSync(indexFile, metadataBuffer); expect(() => index = new Index(indexFile)).to.throwError(/Invalid metadata/); });