customized-fabric 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/customizedFabric/ClipartObject/constants.js +12 -0
- package/lib/customizedFabric/ClipartObject/index.js +145 -0
- package/lib/customizedFabric/ClipartObject/interfaces.js +2 -0
- package/lib/customizedFabric/ImagePlaceholderObject/constants.js +12 -0
- package/lib/customizedFabric/ImagePlaceholderObject/index.js +140 -0
- package/lib/customizedFabric/ImagePlaceholderObject/interfaces.js +2 -0
- package/lib/customizedFabric/TextInputObject/constants.js +14 -0
- package/lib/customizedFabric/TextInputObject/index.js +188 -0
- package/lib/customizedFabric/TextInputObject/interfaces.js +2 -0
- package/lib/customizedFabric/constants.js +12 -0
- package/lib/customizedFabric/index.js +54 -0
- package/lib/customizedFabric/interfaces.js +19 -0
- package/lib/customizedFabric/utils.js +90 -0
- package/lib/index.js +1 -36
- package/lib/utils/objectId/bson_value.js +12 -0
- package/lib/utils/objectId/constants.js +107 -0
- package/lib/utils/objectId/error.js +79 -0
- package/lib/utils/objectId/index.js +281 -0
- package/lib/utils/objectId/parser/utils.js +31 -0
- package/lib/utils/objectId/utils/byte_utils.js +28 -0
- package/lib/utils/objectId/utils/node_byte_utils.js +98 -0
- package/lib/utils/objectId/utils/web_byte_utils.js +124 -0
- package/package.json +4 -1
- package/src/ClipartObject/constants.ts +0 -9
- package/src/ClipartObject/index.ts +0 -156
- package/src/ClipartObject/interfaces.ts +0 -29
- package/src/ImagePlaceholderObject/constants.ts +0 -9
- package/src/ImagePlaceholderObject/index.ts +0 -155
- package/src/ImagePlaceholderObject/interfaces.ts +0 -21
- package/src/TextInputObject/constants.ts +0 -11
- package/src/TextInputObject/index.ts +0 -205
- package/src/TextInputObject/interfaces.ts +0 -40
- package/src/constants.ts +0 -10
- package/src/index.ts +0 -62
- package/src/interfaces.ts +0 -3
- package/src/objectId/bson_value.ts +0 -18
- package/src/objectId/constants.ts +0 -141
- package/src/objectId/error.ts +0 -85
- package/src/objectId/index.ts +0 -354
- package/src/objectId/parser/utils.ts +0 -29
- package/src/objectId/utils/byte_utils.ts +0 -72
- package/src/objectId/utils/node_byte_utils.ts +0 -173
- package/src/objectId/utils/web_byte_utils.ts +0 -212
- package/src/utils.ts +0 -93
- package/tsconfig.json +0 -110
@@ -0,0 +1,90 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.getObject = exports.lockAllObjects = exports.lockObject = exports.loadImageFromFile = exports.isFontLoaded = exports.loadFontFromUrl = void 0;
|
4
|
+
const fabric_1 = require("fabric");
|
5
|
+
const constants_1 = require("./constants");
|
6
|
+
const _1 = require(".");
|
7
|
+
const ImagePlaceholderObject_1 = require("./ImagePlaceholderObject");
|
8
|
+
const loadFontFromUrl = async (name, url) => {
|
9
|
+
if (!name || !url)
|
10
|
+
return;
|
11
|
+
const font = new FontFace(name, `url(${url})`);
|
12
|
+
await font.load();
|
13
|
+
document.fonts.add(font);
|
14
|
+
};
|
15
|
+
exports.loadFontFromUrl = loadFontFromUrl;
|
16
|
+
const isFontLoaded = (name) => {
|
17
|
+
let isLoaded = false;
|
18
|
+
document.fonts.forEach((font) => {
|
19
|
+
if (name == font.family) {
|
20
|
+
isLoaded = true;
|
21
|
+
}
|
22
|
+
});
|
23
|
+
return isLoaded;
|
24
|
+
};
|
25
|
+
exports.isFontLoaded = isFontLoaded;
|
26
|
+
const loadImageFromFile = (image) => {
|
27
|
+
return new Promise((resolve) => {
|
28
|
+
const reader = new FileReader();
|
29
|
+
reader.onload = function (event) {
|
30
|
+
const image = new Image();
|
31
|
+
image.src = event?.target?.result;
|
32
|
+
image.onload = function () {
|
33
|
+
resolve(new fabric_1.fabric.Image(image));
|
34
|
+
};
|
35
|
+
};
|
36
|
+
reader.readAsDataURL(image);
|
37
|
+
});
|
38
|
+
};
|
39
|
+
exports.loadImageFromFile = loadImageFromFile;
|
40
|
+
const lockObject = (object, locked, selectable) => {
|
41
|
+
object.set({
|
42
|
+
hasControls: !locked,
|
43
|
+
lockMovementX: locked,
|
44
|
+
lockMovementY: locked,
|
45
|
+
lockRotation: locked,
|
46
|
+
lockScalingX: locked,
|
47
|
+
lockScalingY: locked,
|
48
|
+
lockUniScaling: locked,
|
49
|
+
lockSkewingX: locked,
|
50
|
+
lockSkewingY: locked,
|
51
|
+
lockScalingFlip: locked,
|
52
|
+
locked: locked,
|
53
|
+
selectable: selectable ?? !locked,
|
54
|
+
});
|
55
|
+
};
|
56
|
+
exports.lockObject = lockObject;
|
57
|
+
const lockAllObjects = (canvas, locked) => {
|
58
|
+
canvas._objects.map((object) => {
|
59
|
+
(0, exports.lockObject)(object, locked);
|
60
|
+
});
|
61
|
+
};
|
62
|
+
exports.lockAllObjects = lockAllObjects;
|
63
|
+
const getObject = (object, options) => {
|
64
|
+
switch (object.type) {
|
65
|
+
case constants_1.OBJECT_TYPES.textInput: {
|
66
|
+
const textInputObject = new _1.TextInput({
|
67
|
+
...object,
|
68
|
+
hideStroke: options?.isOriginal,
|
69
|
+
});
|
70
|
+
return textInputObject;
|
71
|
+
}
|
72
|
+
case constants_1.OBJECT_TYPES.clipart: {
|
73
|
+
const clipartObject = new _1.Clipart({
|
74
|
+
...object,
|
75
|
+
hideStroke: options?.isOriginal,
|
76
|
+
});
|
77
|
+
return clipartObject;
|
78
|
+
}
|
79
|
+
case constants_1.OBJECT_TYPES.imagePlaceHolder: {
|
80
|
+
const imagePlaceHolderObject = new ImagePlaceholderObject_1.ImagePlaceholder({
|
81
|
+
...object,
|
82
|
+
hideStroke: options?.isOriginal,
|
83
|
+
});
|
84
|
+
return imagePlaceHolderObject;
|
85
|
+
}
|
86
|
+
default:
|
87
|
+
return;
|
88
|
+
}
|
89
|
+
};
|
90
|
+
exports.getObject = getObject;
|
package/lib/index.js
CHANGED
@@ -14,39 +14,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
15
|
};
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
-
|
18
|
-
const TextInputObject_1 = require("./TextInputObject");
|
19
|
-
const ClipartObject_1 = require("./ClipartObject");
|
20
|
-
const ImagePlaceholderObject_1 = require("./ImagePlaceholderObject");
|
21
|
-
const constants_1 = require("./constants");
|
22
|
-
fabric_1.fabric.TextInput = TextInputObject_1.TextInput;
|
23
|
-
fabric_1.fabric.Clipart = ClipartObject_1.Clipart;
|
24
|
-
fabric_1.fabric.ImagePlaceholder = ImagePlaceholderObject_1.ImagePlaceholder;
|
25
|
-
fabric_1.fabric.Object.prototype.transparentCorners = false;
|
26
|
-
fabric_1.fabric.Object.prototype.cornerColor = "black";
|
27
|
-
fabric_1.fabric.Object.prototype.cornerStyle = "circle";
|
28
|
-
fabric_1.fabric.Object.prototype.cornerStrokeColor = "black";
|
29
|
-
fabric_1.fabric.Object.prototype.borderColor = "black";
|
30
|
-
fabric_1.fabric.Object.prototype.toObject = (function (toObject) {
|
31
|
-
return function () {
|
32
|
-
switch (this.type) {
|
33
|
-
case constants_1.OBJECT_TYPES.textInput: {
|
34
|
-
return fabric_1.fabric.util.object.extend(toObject.call(this), (0, TextInputObject_1.toTextInputObject)(this));
|
35
|
-
}
|
36
|
-
case constants_1.OBJECT_TYPES.clipart: {
|
37
|
-
return fabric_1.fabric.util.object.extend(toObject.call(this), (0, ClipartObject_1.toClipartObject)(this));
|
38
|
-
}
|
39
|
-
case constants_1.OBJECT_TYPES.imagePlaceHolder: {
|
40
|
-
return fabric_1.fabric.util.object.extend(toObject.call(this), (0, ImagePlaceholderObject_1.toImagePlaceholderObject)(this));
|
41
|
-
}
|
42
|
-
default: {
|
43
|
-
return {};
|
44
|
-
}
|
45
|
-
}
|
46
|
-
};
|
47
|
-
})(fabric_1.fabric.Object.prototype.toObject);
|
48
|
-
__exportStar(require("./TextInputObject"), exports);
|
49
|
-
__exportStar(require("./ClipartObject"), exports);
|
50
|
-
__exportStar(require("./ImagePlaceholderObject"), exports);
|
51
|
-
__exportStar(require("./interfaces"), exports);
|
52
|
-
exports.default = fabric_1.fabric;
|
17
|
+
__exportStar(require("./customizedFabric"), exports);
|
@@ -0,0 +1,12 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.BSONValue = void 0;
|
4
|
+
const constants_1 = require("./constants");
|
5
|
+
/** @public */
|
6
|
+
class BSONValue {
|
7
|
+
/** @internal */
|
8
|
+
get [Symbol.for("@@mdb.bson.version")]() {
|
9
|
+
return constants_1.BSON_MAJOR_VERSION;
|
10
|
+
}
|
11
|
+
}
|
12
|
+
exports.BSONValue = BSONValue;
|
@@ -0,0 +1,107 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.BSONType = exports.BSON_BINARY_SUBTYPE_USER_DEFINED = exports.BSON_BINARY_SUBTYPE_COLUMN = exports.BSON_BINARY_SUBTYPE_ENCRYPTED = exports.BSON_BINARY_SUBTYPE_MD5 = exports.BSON_BINARY_SUBTYPE_UUID_NEW = exports.BSON_BINARY_SUBTYPE_UUID = exports.BSON_BINARY_SUBTYPE_BYTE_ARRAY = exports.BSON_BINARY_SUBTYPE_FUNCTION = exports.BSON_BINARY_SUBTYPE_DEFAULT = exports.BSON_DATA_MAX_KEY = exports.BSON_DATA_MIN_KEY = exports.BSON_DATA_DECIMAL128 = exports.BSON_DATA_LONG = exports.BSON_DATA_TIMESTAMP = exports.BSON_DATA_INT = exports.BSON_DATA_CODE_W_SCOPE = exports.BSON_DATA_SYMBOL = exports.BSON_DATA_CODE = exports.BSON_DATA_DBPOINTER = exports.BSON_DATA_REGEXP = exports.BSON_DATA_NULL = exports.BSON_DATA_DATE = exports.BSON_DATA_BOOLEAN = exports.BSON_DATA_OID = exports.BSON_DATA_UNDEFINED = exports.BSON_DATA_BINARY = exports.BSON_DATA_ARRAY = exports.BSON_DATA_OBJECT = exports.BSON_DATA_STRING = exports.BSON_DATA_NUMBER = exports.JS_INT_MIN = exports.JS_INT_MAX = exports.BSON_INT64_MIN = exports.BSON_INT64_MAX = exports.BSON_INT32_MIN = exports.BSON_INT32_MAX = exports.BSON_MAJOR_VERSION = void 0;
|
4
|
+
/** @internal */
|
5
|
+
exports.BSON_MAJOR_VERSION = 5;
|
6
|
+
/** @internal */
|
7
|
+
exports.BSON_INT32_MAX = 0x7fffffff;
|
8
|
+
/** @internal */
|
9
|
+
exports.BSON_INT32_MIN = -0x80000000;
|
10
|
+
/** @internal */
|
11
|
+
exports.BSON_INT64_MAX = Math.pow(2, 63) - 1;
|
12
|
+
/** @internal */
|
13
|
+
exports.BSON_INT64_MIN = -Math.pow(2, 63);
|
14
|
+
/**
|
15
|
+
* Any integer up to 2^53 can be precisely represented by a double.
|
16
|
+
* @internal
|
17
|
+
*/
|
18
|
+
exports.JS_INT_MAX = Math.pow(2, 53);
|
19
|
+
/**
|
20
|
+
* Any integer down to -2^53 can be precisely represented by a double.
|
21
|
+
* @internal
|
22
|
+
*/
|
23
|
+
exports.JS_INT_MIN = -Math.pow(2, 53);
|
24
|
+
/** Number BSON Type @internal */
|
25
|
+
exports.BSON_DATA_NUMBER = 1;
|
26
|
+
/** String BSON Type @internal */
|
27
|
+
exports.BSON_DATA_STRING = 2;
|
28
|
+
/** Object BSON Type @internal */
|
29
|
+
exports.BSON_DATA_OBJECT = 3;
|
30
|
+
/** Array BSON Type @internal */
|
31
|
+
exports.BSON_DATA_ARRAY = 4;
|
32
|
+
/** Binary BSON Type @internal */
|
33
|
+
exports.BSON_DATA_BINARY = 5;
|
34
|
+
/** Binary BSON Type @internal */
|
35
|
+
exports.BSON_DATA_UNDEFINED = 6;
|
36
|
+
/** ObjectId BSON Type @internal */
|
37
|
+
exports.BSON_DATA_OID = 7;
|
38
|
+
/** Boolean BSON Type @internal */
|
39
|
+
exports.BSON_DATA_BOOLEAN = 8;
|
40
|
+
/** Date BSON Type @internal */
|
41
|
+
exports.BSON_DATA_DATE = 9;
|
42
|
+
/** null BSON Type @internal */
|
43
|
+
exports.BSON_DATA_NULL = 10;
|
44
|
+
/** RegExp BSON Type @internal */
|
45
|
+
exports.BSON_DATA_REGEXP = 11;
|
46
|
+
/** Code BSON Type @internal */
|
47
|
+
exports.BSON_DATA_DBPOINTER = 12;
|
48
|
+
/** Code BSON Type @internal */
|
49
|
+
exports.BSON_DATA_CODE = 13;
|
50
|
+
/** Symbol BSON Type @internal */
|
51
|
+
exports.BSON_DATA_SYMBOL = 14;
|
52
|
+
/** Code with Scope BSON Type @internal */
|
53
|
+
exports.BSON_DATA_CODE_W_SCOPE = 15;
|
54
|
+
/** 32 bit Integer BSON Type @internal */
|
55
|
+
exports.BSON_DATA_INT = 16;
|
56
|
+
/** Timestamp BSON Type @internal */
|
57
|
+
exports.BSON_DATA_TIMESTAMP = 17;
|
58
|
+
/** Long BSON Type @internal */
|
59
|
+
exports.BSON_DATA_LONG = 18;
|
60
|
+
/** Decimal128 BSON Type @internal */
|
61
|
+
exports.BSON_DATA_DECIMAL128 = 19;
|
62
|
+
/** MinKey BSON Type @internal */
|
63
|
+
exports.BSON_DATA_MIN_KEY = 0xff;
|
64
|
+
/** MaxKey BSON Type @internal */
|
65
|
+
exports.BSON_DATA_MAX_KEY = 0x7f;
|
66
|
+
/** Binary Default Type @internal */
|
67
|
+
exports.BSON_BINARY_SUBTYPE_DEFAULT = 0;
|
68
|
+
/** Binary Function Type @internal */
|
69
|
+
exports.BSON_BINARY_SUBTYPE_FUNCTION = 1;
|
70
|
+
/** Binary Byte Array Type @internal */
|
71
|
+
exports.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
|
72
|
+
/** Binary Deprecated UUID Type @deprecated Please use BSON_BINARY_SUBTYPE_UUID_NEW @internal */
|
73
|
+
exports.BSON_BINARY_SUBTYPE_UUID = 3;
|
74
|
+
/** Binary UUID Type @internal */
|
75
|
+
exports.BSON_BINARY_SUBTYPE_UUID_NEW = 4;
|
76
|
+
/** Binary MD5 Type @internal */
|
77
|
+
exports.BSON_BINARY_SUBTYPE_MD5 = 5;
|
78
|
+
/** Encrypted BSON type @internal */
|
79
|
+
exports.BSON_BINARY_SUBTYPE_ENCRYPTED = 6;
|
80
|
+
/** Column BSON type @internal */
|
81
|
+
exports.BSON_BINARY_SUBTYPE_COLUMN = 7;
|
82
|
+
/** Binary User Defined Type @internal */
|
83
|
+
exports.BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
|
84
|
+
/** @public */
|
85
|
+
exports.BSONType = Object.freeze({
|
86
|
+
double: 1,
|
87
|
+
string: 2,
|
88
|
+
object: 3,
|
89
|
+
array: 4,
|
90
|
+
binData: 5,
|
91
|
+
undefined: 6,
|
92
|
+
objectId: 7,
|
93
|
+
bool: 8,
|
94
|
+
date: 9,
|
95
|
+
null: 10,
|
96
|
+
regex: 11,
|
97
|
+
dbPointer: 12,
|
98
|
+
javascript: 13,
|
99
|
+
symbol: 14,
|
100
|
+
javascriptWithScope: 15,
|
101
|
+
int: 16,
|
102
|
+
timestamp: 17,
|
103
|
+
long: 18,
|
104
|
+
decimal: 19,
|
105
|
+
minKey: -1,
|
106
|
+
maxKey: 127,
|
107
|
+
});
|
@@ -0,0 +1,79 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.BSONRuntimeError = exports.BSONVersionError = exports.BSONError = void 0;
|
4
|
+
const constants_1 = require("./constants");
|
5
|
+
/**
|
6
|
+
* @public
|
7
|
+
* @category Error
|
8
|
+
*
|
9
|
+
* `BSONError` objects are thrown when BSON ecounters an error.
|
10
|
+
*
|
11
|
+
* This is the parent class for all the other errors thrown by this library.
|
12
|
+
*/
|
13
|
+
class BSONError extends Error {
|
14
|
+
/**
|
15
|
+
* @internal
|
16
|
+
* The underlying algorithm for isBSONError may change to improve how strict it is
|
17
|
+
* about determining if an input is a BSONError. But it must remain backwards compatible
|
18
|
+
* with previous minors & patches of the current major version.
|
19
|
+
*/
|
20
|
+
get bsonError() {
|
21
|
+
return true;
|
22
|
+
}
|
23
|
+
get name() {
|
24
|
+
return "BSONError";
|
25
|
+
}
|
26
|
+
constructor(message) {
|
27
|
+
super(message);
|
28
|
+
}
|
29
|
+
/**
|
30
|
+
* @public
|
31
|
+
*
|
32
|
+
* All errors thrown from the BSON library inherit from `BSONError`.
|
33
|
+
* This method can assist with determining if an error originates from the BSON library
|
34
|
+
* even if it does not pass an `instanceof` check against this class' constructor.
|
35
|
+
*
|
36
|
+
* @param value - any javascript value that needs type checking
|
37
|
+
*/
|
38
|
+
static isBSONError(value) {
|
39
|
+
return (value != null &&
|
40
|
+
typeof value === "object" &&
|
41
|
+
"bsonError" in value &&
|
42
|
+
value.bsonError === true &&
|
43
|
+
// Do not access the following properties, just check existence
|
44
|
+
"name" in value &&
|
45
|
+
"message" in value &&
|
46
|
+
"stack" in value);
|
47
|
+
}
|
48
|
+
}
|
49
|
+
exports.BSONError = BSONError;
|
50
|
+
/**
|
51
|
+
* @public
|
52
|
+
* @category Error
|
53
|
+
*/
|
54
|
+
class BSONVersionError extends BSONError {
|
55
|
+
get name() {
|
56
|
+
return "BSONVersionError";
|
57
|
+
}
|
58
|
+
constructor() {
|
59
|
+
super(`Unsupported BSON version, bson types must be from bson ${constants_1.BSON_MAJOR_VERSION}.0 or later`);
|
60
|
+
}
|
61
|
+
}
|
62
|
+
exports.BSONVersionError = BSONVersionError;
|
63
|
+
/**
|
64
|
+
* @public
|
65
|
+
* @category Error
|
66
|
+
*
|
67
|
+
* An error generated when BSON functions encounter an unexpected input
|
68
|
+
* or reaches an unexpected/invalid internal state
|
69
|
+
*
|
70
|
+
*/
|
71
|
+
class BSONRuntimeError extends BSONError {
|
72
|
+
get name() {
|
73
|
+
return "BSONRuntimeError";
|
74
|
+
}
|
75
|
+
constructor(message) {
|
76
|
+
super(message);
|
77
|
+
}
|
78
|
+
}
|
79
|
+
exports.BSONRuntimeError = BSONRuntimeError;
|
@@ -0,0 +1,281 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.ObjectId = void 0;
|
4
|
+
const bson_value_1 = require("./bson_value");
|
5
|
+
const error_1 = require("./error");
|
6
|
+
const utils_1 = require("./parser/utils");
|
7
|
+
const byte_utils_1 = require("./utils/byte_utils");
|
8
|
+
// Regular expression that checks for hex value
|
9
|
+
const checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$");
|
10
|
+
// Unique sequence for the current process (initialized on first use)
|
11
|
+
let PROCESS_UNIQUE = null;
|
12
|
+
const kId = Symbol("id");
|
13
|
+
/**
|
14
|
+
* A class representation of the BSON ObjectId type.
|
15
|
+
* @public
|
16
|
+
* @category BSONType
|
17
|
+
*/
|
18
|
+
class ObjectId extends bson_value_1.BSONValue {
|
19
|
+
get _bsontype() {
|
20
|
+
return "ObjectId";
|
21
|
+
}
|
22
|
+
/**
|
23
|
+
* Create an ObjectId type
|
24
|
+
*
|
25
|
+
* @param inputId - Can be a 24 character hex string, 12 byte binary Buffer, or a number.
|
26
|
+
*/
|
27
|
+
constructor(inputId) {
|
28
|
+
super();
|
29
|
+
// workingId is set based on type of input and whether valid id exists for the input
|
30
|
+
let workingId;
|
31
|
+
if (typeof inputId === "object" && inputId && "id" in inputId) {
|
32
|
+
if (typeof inputId.id !== "string" && !ArrayBuffer.isView(inputId.id)) {
|
33
|
+
throw new error_1.BSONError("Argument passed in must have an id that is of type string or Buffer");
|
34
|
+
}
|
35
|
+
if ("toHexString" in inputId &&
|
36
|
+
typeof inputId.toHexString === "function") {
|
37
|
+
workingId = byte_utils_1.ByteUtils.fromHex(inputId.toHexString());
|
38
|
+
}
|
39
|
+
else {
|
40
|
+
workingId = inputId.id;
|
41
|
+
}
|
42
|
+
}
|
43
|
+
else {
|
44
|
+
workingId = inputId;
|
45
|
+
}
|
46
|
+
// the following cases use workingId to construct an ObjectId
|
47
|
+
if (workingId == null || typeof workingId === "number") {
|
48
|
+
// The most common use case (blank id, new objectId instance)
|
49
|
+
// Generate a new id
|
50
|
+
this[kId] = ObjectId.generate(typeof workingId === "number" ? workingId : undefined);
|
51
|
+
}
|
52
|
+
else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
|
53
|
+
// If intstanceof matches we can escape calling ensure buffer in Node.js environments
|
54
|
+
this[kId] = byte_utils_1.ByteUtils.toLocalBufferType(workingId);
|
55
|
+
}
|
56
|
+
else if (typeof workingId === "string") {
|
57
|
+
if (workingId.length === 12) {
|
58
|
+
// TODO(NODE-4361): Remove string of length 12 support
|
59
|
+
const bytes = byte_utils_1.ByteUtils.fromUTF8(workingId);
|
60
|
+
if (bytes.byteLength === 12) {
|
61
|
+
this[kId] = bytes;
|
62
|
+
}
|
63
|
+
else {
|
64
|
+
throw new error_1.BSONError("Argument passed in must be a string of 12 bytes");
|
65
|
+
}
|
66
|
+
}
|
67
|
+
else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
|
68
|
+
this[kId] = byte_utils_1.ByteUtils.fromHex(workingId);
|
69
|
+
}
|
70
|
+
else {
|
71
|
+
throw new error_1.BSONError("Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer");
|
72
|
+
}
|
73
|
+
}
|
74
|
+
else {
|
75
|
+
throw new error_1.BSONError("Argument passed in does not match the accepted types");
|
76
|
+
}
|
77
|
+
// If we are caching the hex string
|
78
|
+
if (ObjectId.cacheHexString) {
|
79
|
+
this.__id = byte_utils_1.ByteUtils.toHex(this.id);
|
80
|
+
}
|
81
|
+
}
|
82
|
+
/**
|
83
|
+
* The ObjectId bytes
|
84
|
+
* @readonly
|
85
|
+
*/
|
86
|
+
get id() {
|
87
|
+
return this[kId];
|
88
|
+
}
|
89
|
+
set id(value) {
|
90
|
+
this[kId] = value;
|
91
|
+
if (ObjectId.cacheHexString) {
|
92
|
+
this.__id = byte_utils_1.ByteUtils.toHex(value);
|
93
|
+
}
|
94
|
+
}
|
95
|
+
/** Returns the ObjectId id as a 24 character hex string representation */
|
96
|
+
toHexString() {
|
97
|
+
if (ObjectId.cacheHexString && this.__id) {
|
98
|
+
return this.__id;
|
99
|
+
}
|
100
|
+
const hexString = byte_utils_1.ByteUtils.toHex(this.id);
|
101
|
+
if (ObjectId.cacheHexString && !this.__id) {
|
102
|
+
this.__id = hexString;
|
103
|
+
}
|
104
|
+
return hexString;
|
105
|
+
}
|
106
|
+
/**
|
107
|
+
* Update the ObjectId index
|
108
|
+
* @internal
|
109
|
+
*/
|
110
|
+
static getInc() {
|
111
|
+
return (ObjectId.index = (ObjectId.index + 1) % 0xffffff);
|
112
|
+
}
|
113
|
+
/**
|
114
|
+
* Generate a 12 byte id buffer used in ObjectId's
|
115
|
+
*
|
116
|
+
* @param time - pass in a second based timestamp.
|
117
|
+
*/
|
118
|
+
static generate(time) {
|
119
|
+
if ("number" !== typeof time) {
|
120
|
+
time = Math.floor(Date.now() / 1000);
|
121
|
+
}
|
122
|
+
const inc = ObjectId.getInc();
|
123
|
+
const buffer = byte_utils_1.ByteUtils.allocate(12);
|
124
|
+
// 4-byte timestamp
|
125
|
+
byte_utils_1.BSONDataView.fromUint8Array(buffer).setUint32(0, time, false);
|
126
|
+
// set PROCESS_UNIQUE if yet not initialized
|
127
|
+
if (PROCESS_UNIQUE === null) {
|
128
|
+
PROCESS_UNIQUE = byte_utils_1.ByteUtils.randomBytes(5);
|
129
|
+
}
|
130
|
+
// 5-byte process unique
|
131
|
+
buffer[4] = PROCESS_UNIQUE[0];
|
132
|
+
buffer[5] = PROCESS_UNIQUE[1];
|
133
|
+
buffer[6] = PROCESS_UNIQUE[2];
|
134
|
+
buffer[7] = PROCESS_UNIQUE[3];
|
135
|
+
buffer[8] = PROCESS_UNIQUE[4];
|
136
|
+
// 3-byte counter
|
137
|
+
buffer[11] = inc & 0xff;
|
138
|
+
buffer[10] = (inc >> 8) & 0xff;
|
139
|
+
buffer[9] = (inc >> 16) & 0xff;
|
140
|
+
return buffer;
|
141
|
+
}
|
142
|
+
/**
|
143
|
+
* Converts the id into a 24 character hex string for printing, unless encoding is provided.
|
144
|
+
* @param encoding - hex or base64
|
145
|
+
*/
|
146
|
+
toString(encoding) {
|
147
|
+
// Is the id a buffer then use the buffer toString method to return the format
|
148
|
+
if (encoding === "base64")
|
149
|
+
return byte_utils_1.ByteUtils.toBase64(this.id);
|
150
|
+
if (encoding === "hex")
|
151
|
+
return this.toHexString();
|
152
|
+
return this.toHexString();
|
153
|
+
}
|
154
|
+
/** Converts to its JSON the 24 character hex string representation. */
|
155
|
+
toJSON() {
|
156
|
+
return this.toHexString();
|
157
|
+
}
|
158
|
+
/**
|
159
|
+
* Compares the equality of this ObjectId with `otherID`.
|
160
|
+
*
|
161
|
+
* @param otherId - ObjectId instance to compare against.
|
162
|
+
*/
|
163
|
+
equals(otherId) {
|
164
|
+
if (otherId === undefined || otherId === null) {
|
165
|
+
return false;
|
166
|
+
}
|
167
|
+
if (otherId instanceof ObjectId) {
|
168
|
+
return (this[kId][11] === otherId[kId][11] &&
|
169
|
+
byte_utils_1.ByteUtils.equals(this[kId], otherId[kId]));
|
170
|
+
}
|
171
|
+
if (typeof otherId === "string" &&
|
172
|
+
ObjectId.isValid(otherId) &&
|
173
|
+
otherId.length === 12 &&
|
174
|
+
(0, utils_1.isUint8Array)(this.id)) {
|
175
|
+
return byte_utils_1.ByteUtils.equals(this.id, byte_utils_1.ByteUtils.fromISO88591(otherId));
|
176
|
+
}
|
177
|
+
if (typeof otherId === "string" &&
|
178
|
+
ObjectId.isValid(otherId) &&
|
179
|
+
otherId.length === 24) {
|
180
|
+
return otherId.toLowerCase() === this.toHexString();
|
181
|
+
}
|
182
|
+
if (typeof otherId === "string" &&
|
183
|
+
ObjectId.isValid(otherId) &&
|
184
|
+
otherId.length === 12) {
|
185
|
+
return byte_utils_1.ByteUtils.equals(byte_utils_1.ByteUtils.fromUTF8(otherId), this.id);
|
186
|
+
}
|
187
|
+
if (typeof otherId === "object" &&
|
188
|
+
"toHexString" in otherId &&
|
189
|
+
typeof otherId.toHexString === "function") {
|
190
|
+
const otherIdString = otherId.toHexString();
|
191
|
+
const thisIdString = this.toHexString().toLowerCase();
|
192
|
+
return (typeof otherIdString === "string" &&
|
193
|
+
otherIdString.toLowerCase() === thisIdString);
|
194
|
+
}
|
195
|
+
return false;
|
196
|
+
}
|
197
|
+
/** Returns the generation date (accurate up to the second) that this ID was generated. */
|
198
|
+
getTimestamp() {
|
199
|
+
const timestamp = new Date();
|
200
|
+
const time = byte_utils_1.BSONDataView.fromUint8Array(this.id).getUint32(0, false);
|
201
|
+
timestamp.setTime(Math.floor(time) * 1000);
|
202
|
+
return timestamp;
|
203
|
+
}
|
204
|
+
/** @internal */
|
205
|
+
static createPk() {
|
206
|
+
return new ObjectId();
|
207
|
+
}
|
208
|
+
/**
|
209
|
+
* Creates an ObjectId from a second based number, with the rest of the ObjectId zeroed out. Used for comparisons or sorting the ObjectId.
|
210
|
+
*
|
211
|
+
* @param time - an integer number representing a number of seconds.
|
212
|
+
*/
|
213
|
+
static createFromTime(time) {
|
214
|
+
const buffer = byte_utils_1.ByteUtils.fromNumberArray([
|
215
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
216
|
+
]);
|
217
|
+
// Encode time into first 4 bytes
|
218
|
+
byte_utils_1.BSONDataView.fromUint8Array(buffer).setUint32(0, time, false);
|
219
|
+
// Return the new objectId
|
220
|
+
return new ObjectId(buffer);
|
221
|
+
}
|
222
|
+
/**
|
223
|
+
* Creates an ObjectId from a hex string representation of an ObjectId.
|
224
|
+
*
|
225
|
+
* @param hexString - create a ObjectId from a passed in 24 character hexstring.
|
226
|
+
*/
|
227
|
+
static createFromHexString(hexString) {
|
228
|
+
if (hexString?.length !== 24) {
|
229
|
+
throw new error_1.BSONError("hex string must be 24 characters");
|
230
|
+
}
|
231
|
+
return new ObjectId(byte_utils_1.ByteUtils.fromHex(hexString));
|
232
|
+
}
|
233
|
+
/** Creates an ObjectId instance from a base64 string */
|
234
|
+
static createFromBase64(base64) {
|
235
|
+
if (base64?.length !== 16) {
|
236
|
+
throw new error_1.BSONError("base64 string must be 16 characters");
|
237
|
+
}
|
238
|
+
return new ObjectId(byte_utils_1.ByteUtils.fromBase64(base64));
|
239
|
+
}
|
240
|
+
/**
|
241
|
+
* Checks if a value is a valid bson ObjectId
|
242
|
+
*
|
243
|
+
* @param id - ObjectId instance to validate.
|
244
|
+
*/
|
245
|
+
static isValid(id) {
|
246
|
+
if (id == null)
|
247
|
+
return false;
|
248
|
+
try {
|
249
|
+
new ObjectId(id);
|
250
|
+
return true;
|
251
|
+
}
|
252
|
+
catch {
|
253
|
+
return false;
|
254
|
+
}
|
255
|
+
}
|
256
|
+
/** @internal */
|
257
|
+
toExtendedJSON() {
|
258
|
+
if (this.toHexString)
|
259
|
+
return { $oid: this.toHexString() };
|
260
|
+
return { $oid: this.toString("hex") };
|
261
|
+
}
|
262
|
+
/** @internal */
|
263
|
+
static fromExtendedJSON(doc) {
|
264
|
+
return new ObjectId(doc.$oid);
|
265
|
+
}
|
266
|
+
/**
|
267
|
+
* Converts to a string representation of this Id.
|
268
|
+
*
|
269
|
+
* @returns return the 24 character hex string representation.
|
270
|
+
* @internal
|
271
|
+
*/
|
272
|
+
[Symbol.for("nodejs.util.inspect.custom")]() {
|
273
|
+
return this.inspect();
|
274
|
+
}
|
275
|
+
inspect() {
|
276
|
+
return `new ObjectId("${this.toHexString()}")`;
|
277
|
+
}
|
278
|
+
}
|
279
|
+
exports.ObjectId = ObjectId;
|
280
|
+
/** @internal */
|
281
|
+
ObjectId.index = Math.floor(Math.random() * 0xffffff);
|
@@ -0,0 +1,31 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.isDate = exports.isMap = exports.isRegExp = exports.isBigUInt64Array = exports.isBigInt64Array = exports.isUint8Array = exports.isAnyArrayBuffer = void 0;
|
4
|
+
function isAnyArrayBuffer(value) {
|
5
|
+
return ["[object ArrayBuffer]", "[object SharedArrayBuffer]"].includes(Object.prototype.toString.call(value));
|
6
|
+
}
|
7
|
+
exports.isAnyArrayBuffer = isAnyArrayBuffer;
|
8
|
+
function isUint8Array(value) {
|
9
|
+
return Object.prototype.toString.call(value) === "[object Uint8Array]";
|
10
|
+
}
|
11
|
+
exports.isUint8Array = isUint8Array;
|
12
|
+
function isBigInt64Array(value) {
|
13
|
+
return Object.prototype.toString.call(value) === "[object BigInt64Array]";
|
14
|
+
}
|
15
|
+
exports.isBigInt64Array = isBigInt64Array;
|
16
|
+
function isBigUInt64Array(value) {
|
17
|
+
return Object.prototype.toString.call(value) === "[object BigUint64Array]";
|
18
|
+
}
|
19
|
+
exports.isBigUInt64Array = isBigUInt64Array;
|
20
|
+
function isRegExp(d) {
|
21
|
+
return Object.prototype.toString.call(d) === "[object RegExp]";
|
22
|
+
}
|
23
|
+
exports.isRegExp = isRegExp;
|
24
|
+
function isMap(d) {
|
25
|
+
return Object.prototype.toString.call(d) === "[object Map]";
|
26
|
+
}
|
27
|
+
exports.isMap = isMap;
|
28
|
+
function isDate(d) {
|
29
|
+
return Object.prototype.toString.call(d) === "[object Date]";
|
30
|
+
}
|
31
|
+
exports.isDate = isDate;
|
@@ -0,0 +1,28 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.BSONDataView = exports.ByteUtils = void 0;
|
4
|
+
const node_byte_utils_1 = require("./node_byte_utils");
|
5
|
+
const web_byte_utils_1 = require("./web_byte_utils");
|
6
|
+
/**
|
7
|
+
* Check that a global Buffer exists that is a function and
|
8
|
+
* does not have a '_isBuffer' property defined on the prototype
|
9
|
+
* (this is to prevent using the npm buffer)
|
10
|
+
*/
|
11
|
+
const hasGlobalBuffer = typeof Buffer === "function" && Buffer.prototype?._isBuffer !== true;
|
12
|
+
/**
|
13
|
+
* This is the only ByteUtils that should be used across the rest of the BSON library.
|
14
|
+
*
|
15
|
+
* The type annotation is important here, it asserts that each of the platform specific
|
16
|
+
* utils implementations are compatible with the common one.
|
17
|
+
*
|
18
|
+
* @internal
|
19
|
+
*/
|
20
|
+
exports.ByteUtils = hasGlobalBuffer
|
21
|
+
? node_byte_utils_1.nodeJsByteUtils
|
22
|
+
: web_byte_utils_1.webByteUtils;
|
23
|
+
class BSONDataView extends DataView {
|
24
|
+
static fromUint8Array(input) {
|
25
|
+
return new DataView(input.buffer, input.byteOffset, input.byteLength);
|
26
|
+
}
|
27
|
+
}
|
28
|
+
exports.BSONDataView = BSONDataView;
|