@@ 49-140 (lines=92) @@ | ||
46 | * @ignore |
|
47 | * @api private |
|
48 | */ |
|
49 | function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefined) { |
|
50 | // If we have toBSON defined, override the current object |
|
51 | if(value && value.toBSON){ |
|
52 | value = value.toBSON(); |
|
53 | } |
|
54 | ||
55 | switch(typeof value) { |
|
56 | case 'string': |
|
57 | return 1 + Buffer.byteLength(name, 'utf8') + 1 + 4 + Buffer.byteLength(value, 'utf8') + 1; |
|
58 | case 'number': |
|
59 | if(Math.floor(value) === value && value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) { |
|
60 | if(value >= BSON.BSON_INT32_MIN && value <= BSON.BSON_INT32_MAX) { // 32 bit |
|
61 | return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (4 + 1); |
|
62 | } else { |
|
63 | return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (8 + 1); |
|
64 | } |
|
65 | } else { // 64 bit |
|
66 | return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (8 + 1); |
|
67 | } |
|
68 | case 'undefined': |
|
69 | if(isArray || !ignoreUndefined) return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (1); |
|
70 | return 0; |
|
71 | case 'boolean': |
|
72 | return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (1 + 1); |
|
73 | case 'object': |
|
74 | if(value == null || value instanceof MinKey || value instanceof MaxKey || value['_bsontype'] == 'MinKey' || value['_bsontype'] == 'MaxKey') { |
|
75 | return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (1); |
|
76 | } else if(value instanceof ObjectID || value['_bsontype'] == 'ObjectID') { |
|
77 | return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (12 + 1); |
|
78 | } else if(value instanceof Date || isDate(value)) { |
|
79 | return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (8 + 1); |
|
80 | } else if(typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) { |
|
81 | return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (1 + 4 + 1) + value.length; |
|
82 | } else if(value instanceof Long || value instanceof Double || value instanceof Timestamp |
|
83 | || value['_bsontype'] == 'Long' || value['_bsontype'] == 'Double' || value['_bsontype'] == 'Timestamp') { |
|
84 | return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (8 + 1); |
|
85 | } else if(value instanceof Decimal128 || value['_bsontype'] == 'Decimal128') { |
|
86 | return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (16 + 1); |
|
87 | } else if(value instanceof Code || value['_bsontype'] == 'Code') { |
|
88 | // Calculate size depending on the availability of a scope |
|
89 | if(value.scope != null && Object.keys(value.scope).length > 0) { |
|
90 | return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + 1 + 4 + 4 + Buffer.byteLength(value.code.toString(), 'utf8') + 1 + calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined); |
|
91 | } else { |
|
92 | return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + 1 + 4 + Buffer.byteLength(value.code.toString(), 'utf8') + 1; |
|
93 | } |
|
94 | } else if(value instanceof Binary || value['_bsontype'] == 'Binary') { |
|
95 | // Check what kind of subtype we have |
|
96 | if(value.sub_type == Binary.SUBTYPE_BYTE_ARRAY) { |
|
97 | return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (value.position + 1 + 4 + 1 + 4); |
|
98 | } else { |
|
99 | return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (value.position + 1 + 4 + 1); |
|
100 | } |
|
101 | } else if(value instanceof Symbol || value['_bsontype'] == 'Symbol') { |
|
102 | return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + Buffer.byteLength(value.value, 'utf8') + 4 + 1 + 1; |
|
103 | } else if(value instanceof DBRef || value['_bsontype'] == 'DBRef') { |
|
104 | // Set up correct object for serialization |
|
105 | var ordered_values = { |
|
106 | '$ref': value.namespace |
|
107 | , '$id' : value.oid |
|
108 | }; |
|
109 | ||
110 | // Add db reference if it exists |
|
111 | if(null != value.db) { |
|
112 | ordered_values['$db'] = value.db; |
|
113 | } |
|
114 | ||
115 | return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + 1 + calculateObjectSize(ordered_values, serializeFunctions, ignoreUndefined); |
|
116 | } else if(value instanceof RegExp || Object.prototype.toString.call(value) === '[object RegExp]') { |
|
117 | return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + 1 + Buffer.byteLength(value.source, 'utf8') + 1 |
|
118 | + (value.global ? 1 : 0) + (value.ignoreCase ? 1 : 0) + (value.multiline ? 1 : 0) + 1 |
|
119 | } else if(value instanceof BSONRegExp || value['_bsontype'] == 'BSONRegExp') { |
|
120 | return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + 1 + Buffer.byteLength(value.pattern, 'utf8') + 1 |
|
121 | + Buffer.byteLength(value.options, 'utf8') + 1 |
|
122 | } else { |
|
123 | return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + calculateObjectSize(value, serializeFunctions, ignoreUndefined) + 1; |
|
124 | } |
|
125 | case 'function': |
|
126 | // WTF for 0.4.X where typeof /someregexp/ === 'function' |
|
127 | if(value instanceof RegExp || Object.prototype.toString.call(value) === '[object RegExp]' || String.call(value) == '[object RegExp]') { |
|
128 | return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + 1 + Buffer.byteLength(value.source, 'utf8') + 1 |
|
129 | + (value.global ? 1 : 0) + (value.ignoreCase ? 1 : 0) + (value.multiline ? 1 : 0) + 1 |
|
130 | } else { |
|
131 | if(serializeFunctions && value.scope != null && Object.keys(value.scope).length > 0) { |
|
132 | return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + 1 + 4 + 4 + Buffer.byteLength(value.toString(), 'utf8') + 1 + calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined); |
|
133 | } else if(serializeFunctions) { |
|
134 | return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + 1 + 4 + Buffer.byteLength(value.toString(), 'utf8') + 1; |
|
135 | } |
|
136 | } |
|
137 | } |
|
138 | ||
139 | return 0; |
|
140 | } |
|
141 | ||
142 | var BSON = {}; |
|
143 |
@@ 16627-16716 (lines=90) @@ | ||
16624 | * @ignore |
|
16625 | * @api private |
|
16626 | */ |
|
16627 | function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefined) { |
|
16628 | // If we have toBSON defined, override the current object |
|
16629 | if (value && value.toBSON) { |
|
16630 | value = value.toBSON(); |
|
16631 | } |
|
16632 | ||
16633 | switch (typeof value) { |
|
16634 | case 'string': |
|
16635 | return 1 + Buffer.byteLength(name, 'utf8') + 1 + 4 + Buffer.byteLength(value, 'utf8') + 1; |
|
16636 | case 'number': |
|
16637 | if (Math.floor(value) === value && value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) { |
|
16638 | if (value >= BSON.BSON_INT32_MIN && value <= BSON.BSON_INT32_MAX) { |
|
16639 | // 32 bit |
|
16640 | return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (4 + 1); |
|
16641 | } else { |
|
16642 | return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1); |
|
16643 | } |
|
16644 | } else { |
|
16645 | // 64 bit |
|
16646 | return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1); |
|
16647 | } |
|
16648 | case 'undefined': |
|
16649 | if (isArray || !ignoreUndefined) return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1; |
|
16650 | return 0; |
|
16651 | case 'boolean': |
|
16652 | return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (1 + 1); |
|
16653 | case 'object': |
|
16654 | if (value == null || value instanceof MinKey || value instanceof MaxKey || value['_bsontype'] == 'MinKey' || value['_bsontype'] == 'MaxKey') { |
|
16655 | return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1; |
|
16656 | } else if (value instanceof ObjectID || value['_bsontype'] == 'ObjectID') { |
|
16657 | return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (12 + 1); |
|
16658 | } else if (value instanceof Date || isDate(value)) { |
|
16659 | return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1); |
|
16660 | } else if (typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) { |
|
16661 | return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (1 + 4 + 1) + value.length; |
|
16662 | } else if (value instanceof Long || value instanceof Double || value instanceof Timestamp || value['_bsontype'] == 'Long' || value['_bsontype'] == 'Double' || value['_bsontype'] == 'Timestamp') { |
|
16663 | return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1); |
|
16664 | } else if (value instanceof Decimal128 || value['_bsontype'] == 'Decimal128') { |
|
16665 | return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (16 + 1); |
|
16666 | } else if (value instanceof Code || value['_bsontype'] == 'Code') { |
|
16667 | // Calculate size depending on the availability of a scope |
|
16668 | if (value.scope != null && Object.keys(value.scope).length > 0) { |
|
16669 | return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1 + 4 + 4 + Buffer.byteLength(value.code.toString(), 'utf8') + 1 + calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined); |
|
16670 | } else { |
|
16671 | return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1 + 4 + Buffer.byteLength(value.code.toString(), 'utf8') + 1; |
|
16672 | } |
|
16673 | } else if (value instanceof Binary || value['_bsontype'] == 'Binary') { |
|
16674 | // Check what kind of subtype we have |
|
16675 | if (value.sub_type == Binary.SUBTYPE_BYTE_ARRAY) { |
|
16676 | return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (value.position + 1 + 4 + 1 + 4); |
|
16677 | } else { |
|
16678 | return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (value.position + 1 + 4 + 1); |
|
16679 | } |
|
16680 | } else if (value instanceof Symbol || value['_bsontype'] == 'Symbol') { |
|
16681 | return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + Buffer.byteLength(value.value, 'utf8') + 4 + 1 + 1; |
|
16682 | } else if (value instanceof DBRef || value['_bsontype'] == 'DBRef') { |
|
16683 | // Set up correct object for serialization |
|
16684 | var ordered_values = { |
|
16685 | '$ref': value.namespace, |
|
16686 | '$id': value.oid |
|
16687 | }; |
|
16688 | ||
16689 | // Add db reference if it exists |
|
16690 | if (null != value.db) { |
|
16691 | ordered_values['$db'] = value.db; |
|
16692 | } |
|
16693 | ||
16694 | return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1 + calculateObjectSize(ordered_values, serializeFunctions, ignoreUndefined); |
|
16695 | } else if (value instanceof RegExp || Object.prototype.toString.call(value) === '[object RegExp]') { |
|
16696 | return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1 + Buffer.byteLength(value.source, 'utf8') + 1 + (value.global ? 1 : 0) + (value.ignoreCase ? 1 : 0) + (value.multiline ? 1 : 0) + 1; |
|
16697 | } else if (value instanceof BSONRegExp || value['_bsontype'] == 'BSONRegExp') { |
|
16698 | return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1 + Buffer.byteLength(value.pattern, 'utf8') + 1 + Buffer.byteLength(value.options, 'utf8') + 1; |
|
16699 | } else { |
|
16700 | return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + calculateObjectSize(value, serializeFunctions, ignoreUndefined) + 1; |
|
16701 | } |
|
16702 | case 'function': |
|
16703 | // WTF for 0.4.X where typeof /someregexp/ === 'function' |
|
16704 | if (value instanceof RegExp || Object.prototype.toString.call(value) === '[object RegExp]' || String.call(value) == '[object RegExp]') { |
|
16705 | return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1 + Buffer.byteLength(value.source, 'utf8') + 1 + (value.global ? 1 : 0) + (value.ignoreCase ? 1 : 0) + (value.multiline ? 1 : 0) + 1; |
|
16706 | } else { |
|
16707 | if (serializeFunctions && value.scope != null && Object.keys(value.scope).length > 0) { |
|
16708 | return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1 + 4 + 4 + Buffer.byteLength(value.toString(), 'utf8') + 1 + calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined); |
|
16709 | } else if (serializeFunctions) { |
|
16710 | return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1 + 4 + Buffer.byteLength(value.toString(), 'utf8') + 1; |
|
16711 | } |
|
16712 | } |
|
16713 | } |
|
16714 | ||
16715 | return 0; |
|
16716 | } |
|
16717 | ||
16718 | var BSON = {}; |
|
16719 |