Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Check message ID
if (typeof id !== 'number') {
stream.emit('error', new Error('Invalid messageId'))
return false
} else {
length += 2
}
// Check unsubs
if (typeof unsubs === 'object' && unsubs.length) {
for (var i = 0; i < unsubs.length; i += 1) {
if (typeof unsubs[i] !== 'string') {
stream.emit('error', new Error('Invalid unsubscriptions'))
return false
}
length += Buffer.byteLength(unsubs[i]) + 2
}
} else {
stream.emit('error', new Error('Invalid unsubscriptions'))
return false
}
// properies mqtt 5
var propertiesData = null
if (version === 5) {
propertiesData = getProperties(stream, properties)
length += propertiesData.length
}
// Header
stream.write(protocol.UNSUBSCRIBE_HEADER[1][dup ? 1 : 0][0])
// Length
} else {
length += will.payload.length + 2
}
} else {
stream.emit('error', new Error('Invalid will payload'))
return false
}
} else {
length += 2
}
}
// Username
if (username) {
if (username.length) {
length += Buffer.byteLength(username) + 2
} else {
stream.emit('error', new Error('Invalid username'))
return false
}
}
// Password
if (password) {
if (password.length) {
length += byteLength(password) + 2
} else {
stream.emit('error', new Error('Invalid password'))
return false
}
}
PacketWriter.prototype.writeString = function(value) {
// Typecast undefined into '' and numbers into strings
value = value || '';
value = value + '';
var bytes = Buffer.byteLength(value, 'utf-8');
this._allocate(bytes);
this._buffer.write(value, this._offset, 'utf-8');
this._offset += bytes;
};
throw new TypeError('The `body` option must be a plain Object or Array when the `form` or `json` option is used');
}
if (isFormData(body)) {
// Special case for https://github.com/form-data/form-data
headers['content-type'] = headers['content-type'] || `multipart/form-data; boundary=${body.getBoundary()}`;
} else if (opts.form && canBodyBeStringified) {
headers['content-type'] = headers['content-type'] || 'application/x-www-form-urlencoded';
opts.body = querystring.stringify(body);
} else if (opts.json && canBodyBeStringified) {
headers['content-type'] = headers['content-type'] || 'application/json';
opts.body = JSON.stringify(body);
}
if (is.undefined(headers['content-length']) && is.undefined(headers['transfer-encoding']) && !is.nodeStream(body)) {
const length = is.string(opts.body) ? Buffer.byteLength(opts.body) : opts.body.length;
headers['content-length'] = length;
}
// Convert buffer to stream to receive upload progress events
// see https://github.com/sindresorhus/got/pull/322
if (is.buffer(body)) {
opts.body = intoStream(body);
opts.body._buffer = body;
}
opts.method = (opts.method || 'POST').toUpperCase();
}
if (opts.hostname === 'unix') {
const matches = /(.+?):(.+)/.exec(opts.path);
it('should return 24 bytes of entropy hex encoded', () => {
let entropy = BITBOX.Crypto.randomBytes(24);
assert.equal(Buffer.byteLength(entropy), 24);
});
t.test('decoding a string of length ' + str.length, function (t) {
var buf = Buffer.allocUnsafe(3 + Buffer.byteLength(str))
buf[0] = 0xda
buf.writeUInt16BE(Buffer.byteLength(str), 1)
buf.write(str, 3)
t.equal(encoder.decode(buf), str, 'must decode correctly')
t.end()
})
rtxt.encodingLength = function (data) {
if (!data) return 2
return (Buffer.isBuffer(data) ? data.length : Buffer.byteLength(data)) + 2
}
function writeString (stream, string) {
var strlen = Buffer.byteLength(string)
writeNumber(stream, strlen)
return stream.write(string, 'utf8')
}
var body = util.removeProtocol(rule.value, true);
var isRawFile = isRawFileProtocol(protocol);
var reader = isRawFile ? getRawResByValue(body) : {
statusCode: 200,
body: body,
headers: {
'content-type': (rule.key ? mime.lookup(rule.key, defaultType) : defaultType) + '; charset=utf-8'
}
};
if (isTplProtocol(protocol)){
reader.realUrl = rule.matcher;
render(reader);
} else {
if (!isRawFile) {
var size = Buffer.byteLength(body);
var range = util.parseRange(req, size);
if (range) {
body = Buffer.from(body);
reader.body = body.slice(range.start, range.end + 1);
addRangeHeaders(reader, range, size);
}
}
reader = util.wrapResponse(reader);
reader.realUrl = rule.matcher;
res.response(reader);
}
return;
}
readFiles(util.getRuleFiles(rule), function(err, path, size) {
if (err) {