readAll() { const result = this.buffer.slice(this.offset) this.offset += Buffer.byteLength(this.buffer) return result }
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 }
static sizeOfVarIntBytes(value) { const size = value == null ? -1 : Buffer.byteLength(value) if (size < 0) { return Encoder.sizeOfVarInt(-1) } return Encoder.sizeOfVarInt(size) + size }
test('mapApiGatewayEventToHttpRequest: without headers', () => { const r = mapApiGatewayEventToHttpRequest() expect(r.httpRequest).toEqual({ method: 'GET', path: '/foo', headers: { 'Content-Length': Buffer.byteLength('Hello serverless!'), 'x-apigateway-event': encodeURIComponent(JSON.stringify(r.eventClone)), 'x-apigateway-context': encodeURIComponent(JSON.stringify(r.context)) }, socketPath: '/tmp/server0.sock' }) })
customValidator(value) { if (value && value.length) { const base64ImageLength = Buffer.byteLength(value.substring(value.indexOf(',') + 1), 'base64'); if (base64ImageLength > MAX_SIZE_PROFILE_PICTURE) { throw new Error('Profile picture too big'); } } }
get length() { // copy from koa const len = this.header['content-length']; const body = this.body; if (len == null) { if (!body) return; if (typeof body === 'string') return Buffer.byteLength(body); if (Buffer.isBuffer(body)) return body.length; if (isJSON(body)) return Buffer.byteLength(JSON.stringify(body)); return; } return parseInt(len, 10); }
task.plugin('gzip', {}, function * (file, opts) { opts = Object.assign({}, defaults, opts); // if there is a threshold && we don't meet it, exit if (typeof opts.threshold === 'number' && Buffer.byteLength(file.data) < opts.threshold) { return; } // clone the file object // @todo: `opts.replace` let clone = Object.assign({}, file); // modify the file extension clone.base += (opts.ext.charAt(0) === '.') ? opts.ext : `.${opts.ext}`; // compress & set data clone.data = yield zipper(clone.data, opts.options); // add to files array this._.files.push(clone); });
const sizeOfHeaders = headersArray => { let size = Encoder.sizeOfVarInt(headersArray.length) for (const header of headersArray) { const keySize = Buffer.byteLength(header.key) const valueSize = Buffer.byteLength(header.value) size += Encoder.sizeOfVarInt(keySize) + keySize if (header.value === null) { size += Encoder.sizeOfVarInt(-1) } else { size += Encoder.sizeOfVarInt(valueSize) + valueSize } } return size }
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 }
// A promisey wrapper for weibo graph requests. function graphRequest(access_token) { var postData = querystring.stringify({ access_token: access_token, }); var options = { hostname: 'api.weibo.com', path: '/oauth2/get_token_info', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': Buffer.byteLength(postData), }, }; return httpsRequest.request(options, postData); }
canReadBytes(length) { return Buffer.byteLength(this.buffer) - this.offset >= length }
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 }
customValidator(value) { if (value && value.length) { const base64ImageLength = Buffer.byteLength(value.substring(value.indexOf(',') + 1), 'base64'); if (base64ImageLength > MAX_SIZE_PROFILE_PICTURE) { throw new Error('Profile picture too big'); } } }
test('mapApiGatewayEventToHttpRequest: with headers', () => { const r = mapApiGatewayEventToHttpRequest({'x-foo': 'foo'}) expect(r.httpRequest).toEqual({ method: 'GET', path: '/foo', headers: { 'x-foo': 'foo', 'Content-Length': Buffer.byteLength('Hello serverless!'), 'x-apigateway-event': encodeURIComponent(JSON.stringify(r.eventClone)), 'x-apigateway-context': encodeURIComponent(JSON.stringify(r.context)) }, socketPath: '/tmp/server0.sock' }) })
// A promisey wrapper for weibo graph requests. function graphRequest(access_token) { var postData = querystring.stringify({ access_token: access_token }); var options = { hostname: 'api.weibo.com', path: '/oauth2/get_token_info', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': Buffer.byteLength(postData) } }; return httpsRequest.request(options, postData); }