Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public static longFromBigInt(number: BN) {
// Emulate Java BigInteger.longValue(), following the spec at 5.1.3 https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html
let byteLength = Math.max(8, number.byteLength())
const bytes = number.toTwos(8 * byteLength).toArray('be', byteLength)
const truncatedBytes = bytes.slice(bytes.length - 8, bytes.length)
return Long.fromBytesBE(truncatedBytes)
}
public static fromByteArray(bytes: Buffer): any {
// Read 1 byte for type
let type = bytes.readUInt8(0)
// Read 4 bytes for length
let length = bytes.readUInt32BE(1)
// Switch on type
switch (type) {
case DataTypes.BOOLEAN: {
return bytes.readUInt8(5) ? true : false
}
case DataTypes.NUMBER: {
return Long.fromBytesBE([...bytes.slice(5, 13)]).toNumber()
}
case DataTypes.STRING: {
return bytes.slice(5, 5 + length).toString('utf8')
}
case DataTypes.BYTES: {
return new RadixBase64(bytes.slice(5, 5 + length))
}
case DataTypes.OBJECT: {
return this.fromObjectByteArray(bytes)
}
case DataTypes.ARRAY: {
let output: Array = []
let offset = 5
while (offset < length) {
// Read 2nd to 5th bytes to find out the length of the value
);
log.mgmt(
"[%s] Acquiring lock to get the management req res link.",
this._context.namespace.connectionId
);
await defaultLock.acquire(this.managementLock, () => {
return this._init();
});
const result = await this._mgmtReqResLink!.sendRequest(request);
const sequenceNumbers = result.body[Constants.sequenceNumbers];
const sequenceNumbersAsLong = [];
for (let i = 0; i < sequenceNumbers.length; i++) {
if (typeof sequenceNumbers[i] === "number") {
sequenceNumbersAsLong.push(Long.fromNumber(sequenceNumbers[i]));
} else {
sequenceNumbersAsLong.push(Long.fromBytesBE(sequenceNumbers[i]));
}
}
return sequenceNumbersAsLong;
} catch (err) {
const error = translate(err);
log.error(
"An error occurred while sending the request to schedule messages to " +
"$management endpoint: %O",
error
);
throw error;
}
}
if (msg.message_annotations[Constants.scheduledEnqueueTime] != null) {
sbmsg.scheduledEnqueueTimeUtc = msg.message_annotations[Constants.scheduledEnqueueTime];
}
}
const props: any = {};
if (msg.message_annotations != null) {
if (msg.message_annotations[Constants.deadLetterSource] != null) {
props.deadLetterSource = msg.message_annotations[Constants.deadLetterSource];
}
if (msg.message_annotations[Constants.enqueueSequenceNumber] != null) {
props.enqueuedSequenceNumber = msg.message_annotations[Constants.enqueueSequenceNumber];
}
if (msg.message_annotations[Constants.sequenceNumber] != null) {
if (Buffer.isBuffer(msg.message_annotations[Constants.sequenceNumber])) {
props.sequenceNumber = Long.fromBytesBE(msg.message_annotations[Constants.sequenceNumber]);
} else {
props.sequenceNumber = Long.fromNumber(msg.message_annotations[Constants.sequenceNumber]);
}
}
if (msg.message_annotations[Constants.enqueuedTime] != null) {
props.enqueuedTimeUtc = new Date(msg.message_annotations[Constants.enqueuedTime] as number);
}
if (msg.message_annotations[Constants.lockedUntil] != null) {
props.lockedUntilUtc = new Date(msg.message_annotations[Constants.lockedUntil] as number);
}
}
if (msg.ttl != null && msg.ttl >= Constants.maxDurationValue - props.enqueuedTimeUtc.getTime()) {
props.expiresAtUtc = new Date(Constants.maxDurationValue);
} else {
props.expiresAtUtc = new Date(props.enqueuedTimeUtc.getTime() + msg.ttl!);
}
export function convertTicksToDate(buf: number[]): Date {
const epochMicroDiff: number = 621355968000000000;
const longValue: Long = Long.fromBytesBE(buf);
const timeInMS = longValue
.sub(epochMicroDiff)
.div(10000)
.toNumber();
const result = new Date(timeInMS);
log.utils("The converted date is: %s", result.toString());
return result;
}
if (msg.message_annotations[Constants.scheduledEnqueueTime] != null) {
sbmsg.scheduledEnqueueTimeUtc = msg.message_annotations[Constants.scheduledEnqueueTime];
}
}
const props: any = {};
if (msg.message_annotations != null) {
if (msg.message_annotations[Constants.deadLetterSource] != null) {
props.deadLetterSource = msg.message_annotations[Constants.deadLetterSource];
}
if (msg.message_annotations[Constants.enqueueSequenceNumber] != null) {
props.enqueuedSequenceNumber = msg.message_annotations[Constants.enqueueSequenceNumber];
}
if (msg.message_annotations[Constants.sequenceNumber] != null) {
if (Buffer.isBuffer(msg.message_annotations[Constants.sequenceNumber])) {
props.sequenceNumber = Long.fromBytesBE(msg.message_annotations[Constants.sequenceNumber]);
} else {
props.sequenceNumber = Long.fromNumber(msg.message_annotations[Constants.sequenceNumber]);
}
}
if (msg.message_annotations[Constants.enqueuedTime] != null) {
props.enqueuedTimeUtc = new Date(msg.message_annotations[Constants.enqueuedTime] as number);
}
if (msg.message_annotations[Constants.lockedUntil] != null) {
props.lockedUntilUtc = new Date(msg.message_annotations[Constants.lockedUntil] as number);
}
}
if (msg.ttl != null && msg.ttl >= Constants.maxDurationValue - props.enqueuedTimeUtc.getTime()) {
props.expiresAtUtc = new Date(Constants.maxDurationValue);
} else {
props.expiresAtUtc = new Date(props.enqueuedTimeUtc.getTime() + msg.ttl!);
}