Socket
Socket
Sign inDemoInstall

bson

Package Overview
Dependencies
3
Maintainers
6
Versions
161
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 4.6.5 to 4.7.0

2

bower.json

@@ -25,3 +25,3 @@ {

],
"version": "4.6.5"
"version": "4.7.0"
}

@@ -600,4 +600,2 @@ import { Buffer } from 'buffer';

declare const kId_2: unique symbol;
/**

@@ -1170,6 +1168,4 @@ * A class representing a 64-bit integer

*/
export declare class UUID {
_bsontype: 'UUID';
export declare class UUID extends Binary {
static cacheHexString: boolean;
/* Excluded from this release type: [kId] */
/* Excluded from this release type: __id */

@@ -1189,5 +1185,2 @@ /**

/**
* Generate a 16 byte uuid v4 buffer used in UUIDs
*/
/**
* Returns the UUID id as a 32 or 36 character hex string representation, excluding/including dashes (defaults to 36 character dash separated)

@@ -1194,0 +1187,0 @@ * @param includeDashes - should the string exclude dash-separators.

"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Binary = void 0;
exports.UUID = exports.Binary = void 0;
var buffer_1 = require("buffer");
var ensure_buffer_1 = require("./ensure_buffer");
var uuid_utils_1 = require("./uuid_utils");
var uuid_1 = require("./uuid");
var utils_1 = require("./parser/utils");
var error_1 = require("./error");
var constants_1 = require("./constants");
/**

@@ -180,3 +196,3 @@ * A class representation of the BSON Binary type.

if (this.sub_type === Binary.SUBTYPE_UUID) {
return new uuid_1.UUID(this.buffer.slice(0, this.position));
return new UUID(this.buffer.slice(0, this.position));
}

@@ -209,3 +225,3 @@ throw new error_1.BSONError("Binary sub_type \"".concat(this.sub_type, "\" is not supported for converting to UUID. Only \"").concat(Binary.SUBTYPE_UUID, "\" is currently supported."));

}
return new Binary(data, type);
return type === constants_1.BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type);
};

@@ -249,2 +265,165 @@ /** @internal */

Object.defineProperty(Binary.prototype, '_bsontype', { value: 'Binary' });
var UUID_BYTE_LENGTH = 16;
/**
* A class representation of the BSON UUID type.
* @public
*/
var UUID = /** @class */ (function (_super) {
__extends(UUID, _super);
/**
* Create an UUID type
*
* @param input - Can be a 32 or 36 character hex string (dashes excluded/included) or a 16 byte binary Buffer.
*/
function UUID(input) {
var _this = this;
var bytes;
var hexStr;
if (input == null) {
bytes = UUID.generate();
}
else if (input instanceof UUID) {
bytes = buffer_1.Buffer.from(input.buffer);
hexStr = input.__id;
}
else if (ArrayBuffer.isView(input) && input.byteLength === UUID_BYTE_LENGTH) {
bytes = (0, ensure_buffer_1.ensureBuffer)(input);
}
else if (typeof input === 'string') {
bytes = (0, uuid_utils_1.uuidHexStringToBuffer)(input);
}
else {
throw new error_1.BSONTypeError('Argument passed in UUID constructor must be a UUID, a 16 byte Buffer or a 32/36 character hex string (dashes excluded/included, format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).');
}
_this = _super.call(this, bytes, constants_1.BSON_BINARY_SUBTYPE_UUID_NEW) || this;
_this.__id = hexStr;
return _this;
}
Object.defineProperty(UUID.prototype, "id", {
/**
* The UUID bytes
* @readonly
*/
get: function () {
return this.buffer;
},
set: function (value) {
this.buffer = value;
if (UUID.cacheHexString) {
this.__id = (0, uuid_utils_1.bufferToUuidHexString)(value);
}
},
enumerable: false,
configurable: true
});
/**
* Returns the UUID id as a 32 or 36 character hex string representation, excluding/including dashes (defaults to 36 character dash separated)
* @param includeDashes - should the string exclude dash-separators.
* */
UUID.prototype.toHexString = function (includeDashes) {
if (includeDashes === void 0) { includeDashes = true; }
if (UUID.cacheHexString && this.__id) {
return this.__id;
}
var uuidHexString = (0, uuid_utils_1.bufferToUuidHexString)(this.id, includeDashes);
if (UUID.cacheHexString) {
this.__id = uuidHexString;
}
return uuidHexString;
};
/**
* Converts the id into a 36 character (dashes included) hex string, unless a encoding is specified.
*/
UUID.prototype.toString = function (encoding) {
return encoding ? this.id.toString(encoding) : this.toHexString();
};
/**
* Converts the id into its JSON string representation.
* A 36 character (dashes included) hex string in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
*/
UUID.prototype.toJSON = function () {
return this.toHexString();
};
/**
* Compares the equality of this UUID with `otherID`.
*
* @param otherId - UUID instance to compare against.
*/
UUID.prototype.equals = function (otherId) {
if (!otherId) {
return false;
}
if (otherId instanceof UUID) {
return otherId.id.equals(this.id);
}
try {
return new UUID(otherId).id.equals(this.id);
}
catch (_a) {
return false;
}
};
/**
* Creates a Binary instance from the current UUID.
*/
UUID.prototype.toBinary = function () {
return new Binary(this.id, Binary.SUBTYPE_UUID);
};
/**
* Generates a populated buffer containing a v4 uuid
*/
UUID.generate = function () {
var bytes = (0, utils_1.randomBytes)(UUID_BYTE_LENGTH);
// Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
// Kindly borrowed from https://github.com/uuidjs/uuid/blob/master/src/v4.js
bytes[6] = (bytes[6] & 0x0f) | 0x40;
bytes[8] = (bytes[8] & 0x3f) | 0x80;
return buffer_1.Buffer.from(bytes);
};
/**
* Checks if a value is a valid bson UUID
* @param input - UUID, string or Buffer to validate.
*/
UUID.isValid = function (input) {
if (!input) {
return false;
}
if (input instanceof UUID) {
return true;
}
if (typeof input === 'string') {
return (0, uuid_utils_1.uuidValidateString)(input);
}
if ((0, utils_1.isUint8Array)(input)) {
// check for length & uuid version (https://tools.ietf.org/html/rfc4122#section-4.1.3)
if (input.length !== UUID_BYTE_LENGTH) {
return false;
}
return (input[6] & 0xf0) === 0x40 && (input[8] & 0x80) === 0x80;
}
return false;
};
/**
* Creates an UUID from a hex string representation of an UUID.
* @param hexString - 32 or 36 character hex string (dashes excluded/included).
*/
UUID.createFromHexString = function (hexString) {
var buffer = (0, uuid_utils_1.uuidHexStringToBuffer)(hexString);
return new UUID(buffer);
};
/**
* Converts to a string representation of this Id.
*
* @returns return the 36 character hex string representation.
* @internal
*/
UUID.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
return this.inspect();
};
UUID.prototype.inspect = function () {
return "new UUID(\"".concat(this.toHexString(), "\")");
};
return UUID;
}(Binary));
exports.UUID = UUID;
//# sourceMappingURL=binary.js.map

@@ -8,2 +8,3 @@ "use strict";

Object.defineProperty(exports, "Binary", { enumerable: true, get: function () { return binary_1.Binary; } });
Object.defineProperty(exports, "UUID", { enumerable: true, get: function () { return binary_1.UUID; } });
var code_1 = require("./code");

@@ -43,6 +44,2 @@ Object.defineProperty(exports, "Code", { enumerable: true, get: function () { return code_1.Code; } });

Object.defineProperty(exports, "Timestamp", { enumerable: true, get: function () { return timestamp_1.Timestamp; } });
var uuid_1 = require("./uuid");
Object.defineProperty(exports, "UUID", { enumerable: true, get: function () { return uuid_1.UUID; } });
var binary_2 = require("./binary");
var code_2 = require("./code");
var constants_1 = require("./constants");

@@ -83,17 +80,6 @@ Object.defineProperty(exports, "BSON_BINARY_SUBTYPE_BYTE_ARRAY", { enumerable: true, get: function () { return constants_1.BSON_BINARY_SUBTYPE_BYTE_ARRAY; } });

Object.defineProperty(exports, "BSON_INT64_MIN", { enumerable: true, get: function () { return constants_1.BSON_INT64_MIN; } });
var db_ref_2 = require("./db_ref");
var decimal128_2 = require("./decimal128");
var double_2 = require("./double");
var extended_json_2 = require("./extended_json");
Object.defineProperty(exports, "EJSON", { enumerable: true, get: function () { return extended_json_2.EJSON; } });
var int_32_2 = require("./int_32");
var long_2 = require("./long");
var max_key_2 = require("./max_key");
var min_key_2 = require("./min_key");
var objectid_2 = require("./objectid");
var regexp_2 = require("./regexp");
var symbol_2 = require("./symbol");
var timestamp_2 = require("./timestamp");
Object.defineProperty(exports, "LongWithoutOverridesClass", { enumerable: true, get: function () { return timestamp_2.LongWithoutOverridesClass; } });
var uuid_2 = require("./uuid");
var error_2 = require("./error");

@@ -248,3 +234,3 @@ Object.defineProperty(exports, "BSONError", { enumerable: true, get: function () { return error_2.BSONError; } });

Long: long_1.Long,
UUID: uuid_1.UUID,
UUID: binary_1.UUID,
Map: map_1.Map,

@@ -251,0 +237,0 @@ MaxKey: max_key_1.MaxKey,

@@ -343,2 +343,5 @@ "use strict";

value = new binary_1.Binary(buffer.slice(index, index + binarySize), subType);
if (subType === constants.BSON_BINARY_SUBTYPE_UUID_NEW) {
value = value.toUUID();
}
}

@@ -369,4 +372,7 @@ }

}
else if (subType === constants.BSON_BINARY_SUBTYPE_UUID_NEW) {
value = new binary_1.Binary(buffer.slice(index, index + binarySize), subType).toUUID();
}
else {
value = new binary_1.Binary(_buffer, subType);
value = new binary_1.Binary(buffer.slice(index, index + binarySize), subType);
}

@@ -373,0 +379,0 @@ }

@@ -18,3 +18,3 @@ {

"types": "bson.d.ts",
"version": "4.6.5",
"version": "4.7.0",
"author": {

@@ -45,2 +45,3 @@ "name": "The MongoDB NodeJS Team",

"array-includes": "^3.1.3",
"array.prototype.flatmap": "^1.3.0",
"benchmark": "^2.1.4",

@@ -47,0 +48,0 @@ "chai": "^4.2.0",

import { Buffer } from 'buffer';
import { ensureBuffer } from './ensure_buffer';
import { uuidHexStringToBuffer } from './uuid_utils';
import { UUID, UUIDExtended } from './uuid';
import { bufferToUuidHexString, uuidHexStringToBuffer, uuidValidateString } from './uuid_utils';
import { isUint8Array, randomBytes } from './parser/utils';
import type { EJSONOptions } from './extended_json';
import { BSONError, BSONTypeError } from './error';
import { BSON_BINARY_SUBTYPE_UUID_NEW } from './constants';

@@ -280,3 +281,3 @@ /** @public */

}
return new Binary(data, type);
return type === BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type);
}

@@ -296,1 +297,187 @@

Object.defineProperty(Binary.prototype, '_bsontype', { value: 'Binary' });
/** @public */
export type UUIDExtended = {
$uuid: string;
};
const UUID_BYTE_LENGTH = 16;
/**
* A class representation of the BSON UUID type.
* @public
*/
export class UUID extends Binary {
static cacheHexString: boolean;
/** UUID hexString cache @internal */
private __id?: string;
/**
* Create an UUID type
*
* @param input - Can be a 32 or 36 character hex string (dashes excluded/included) or a 16 byte binary Buffer.
*/
constructor(input?: string | Buffer | UUID) {
let bytes;
let hexStr;
if (input == null) {
bytes = UUID.generate();
} else if (input instanceof UUID) {
bytes = Buffer.from(input.buffer);
hexStr = input.__id;
} else if (ArrayBuffer.isView(input) && input.byteLength === UUID_BYTE_LENGTH) {
bytes = ensureBuffer(input);
} else if (typeof input === 'string') {
bytes = uuidHexStringToBuffer(input);
} else {
throw new BSONTypeError(
'Argument passed in UUID constructor must be a UUID, a 16 byte Buffer or a 32/36 character hex string (dashes excluded/included, format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).'
);
}
super(bytes, BSON_BINARY_SUBTYPE_UUID_NEW);
this.__id = hexStr;
}
/**
* The UUID bytes
* @readonly
*/
get id(): Buffer {
return this.buffer;
}
set id(value: Buffer) {
this.buffer = value;
if (UUID.cacheHexString) {
this.__id = bufferToUuidHexString(value);
}
}
/**
* Returns the UUID id as a 32 or 36 character hex string representation, excluding/including dashes (defaults to 36 character dash separated)
* @param includeDashes - should the string exclude dash-separators.
* */
toHexString(includeDashes = true): string {
if (UUID.cacheHexString && this.__id) {
return this.__id;
}
const uuidHexString = bufferToUuidHexString(this.id, includeDashes);
if (UUID.cacheHexString) {
this.__id = uuidHexString;
}
return uuidHexString;
}
/**
* Converts the id into a 36 character (dashes included) hex string, unless a encoding is specified.
*/
toString(encoding?: string): string {
return encoding ? this.id.toString(encoding) : this.toHexString();
}
/**
* Converts the id into its JSON string representation.
* A 36 character (dashes included) hex string in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
*/
toJSON(): string {
return this.toHexString();
}
/**
* Compares the equality of this UUID with `otherID`.
*
* @param otherId - UUID instance to compare against.
*/
equals(otherId: string | Buffer | UUID): boolean {
if (!otherId) {
return false;
}
if (otherId instanceof UUID) {
return otherId.id.equals(this.id);
}
try {
return new UUID(otherId).id.equals(this.id);
} catch {
return false;
}
}
/**
* Creates a Binary instance from the current UUID.
*/
toBinary(): Binary {
return new Binary(this.id, Binary.SUBTYPE_UUID);
}
/**
* Generates a populated buffer containing a v4 uuid
*/
static generate(): Buffer {
const bytes = randomBytes(UUID_BYTE_LENGTH);
// Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
// Kindly borrowed from https://github.com/uuidjs/uuid/blob/master/src/v4.js
bytes[6] = (bytes[6] & 0x0f) | 0x40;
bytes[8] = (bytes[8] & 0x3f) | 0x80;
return Buffer.from(bytes);
}
/**
* Checks if a value is a valid bson UUID
* @param input - UUID, string or Buffer to validate.
*/
static isValid(input: string | Buffer | UUID): boolean {
if (!input) {
return false;
}
if (input instanceof UUID) {
return true;
}
if (typeof input === 'string') {
return uuidValidateString(input);
}
if (isUint8Array(input)) {
// check for length & uuid version (https://tools.ietf.org/html/rfc4122#section-4.1.3)
if (input.length !== UUID_BYTE_LENGTH) {
return false;
}
return (input[6] & 0xf0) === 0x40 && (input[8] & 0x80) === 0x80;
}
return false;
}
/**
* Creates an UUID from a hex string representation of an UUID.
* @param hexString - 32 or 36 character hex string (dashes excluded/included).
*/
static createFromHexString(hexString: string): UUID {
const buffer = uuidHexStringToBuffer(hexString);
return new UUID(buffer);
}
/**
* Converts to a string representation of this Id.
*
* @returns return the 36 character hex string representation.
* @internal
*/
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
return `new UUID("${this.toHexString()}")`;
}
}
import { Buffer } from 'buffer';
import { Binary } from './binary';
import { Binary, UUID } from './binary';
import { Code } from './code';

@@ -23,5 +23,4 @@ import { DBRef } from './db_ref';

import { Timestamp } from './timestamp';
import { UUID } from './uuid';
export { BinaryExtended, BinaryExtendedLegacy, BinarySequence } from './binary';
export { CodeExtended } from './code';
export type { UUIDExtended, BinaryExtended, BinaryExtendedLegacy, BinarySequence } from './binary';
export type { CodeExtended } from './code';
export {

@@ -63,22 +62,18 @@ BSON_BINARY_SUBTYPE_BYTE_ARRAY,

} from './constants';
export { DBRefLike } from './db_ref';
export { Decimal128Extended } from './decimal128';
export { DoubleExtended } from './double';
export { EJSON, EJSONOptions } from './extended_json';
export { Int32Extended } from './int_32';
export { LongExtended } from './long';
export { MaxKeyExtended } from './max_key';
export { MinKeyExtended } from './min_key';
export { ObjectIdExtended, ObjectIdLike } from './objectid';
export { BSONRegExpExtended, BSONRegExpExtendedLegacy } from './regexp';
export { BSONSymbolExtended } from './symbol';
export type { DBRefLike } from './db_ref';
export type { Decimal128Extended } from './decimal128';
export type { DoubleExtended } from './double';
export type { EJSONOptions } from './extended_json';
export { EJSON } from './extended_json';
export type { Int32Extended } from './int_32';
export type { LongExtended } from './long';
export type { MaxKeyExtended } from './max_key';
export type { MinKeyExtended } from './min_key';
export type { ObjectIdExtended, ObjectIdLike } from './objectid';
export type { BSONRegExpExtended, BSONRegExpExtendedLegacy } from './regexp';
export type { BSONSymbolExtended } from './symbol';
export type { LongWithoutOverrides, TimestampExtended, TimestampOverrides } from './timestamp';
export { LongWithoutOverridesClass } from './timestamp';
export type { SerializeOptions, DeserializeOptions };
export {
LongWithoutOverrides,
LongWithoutOverridesClass,
TimestampExtended,
TimestampOverrides
} from './timestamp';
export { UUIDExtended } from './uuid';
export { SerializeOptions, DeserializeOptions };
export {
Code,

@@ -85,0 +80,0 @@ Map,

@@ -420,2 +420,5 @@ import { Buffer } from 'buffer';

value = new Binary(buffer.slice(index, index + binarySize), subType);
if (subType === constants.BSON_BINARY_SUBTYPE_UUID_NEW) {
value = value.toUUID();
}
}

@@ -446,4 +449,6 @@ } else {

value = _buffer;
} else if (subType === constants.BSON_BINARY_SUBTYPE_UUID_NEW) {
value = new Binary(buffer.slice(index, index + binarySize), subType).toUUID();
} else {
value = new Binary(_buffer, subType);
value = new Binary(buffer.slice(index, index + binarySize), subType);
}

@@ -450,0 +455,0 @@ }

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc