6 // Test if we're in Node via presence of "global" not absence of "window"
7 // to support hybrid environments like Electron
8 if(typeof global !== 'undefined') {
9 var Buffer = require('buffer').Buffer; // TODO just use global Buffer
13 * A class representation of the BSON Binary type.
16 * - **BSON.BSON_BINARY_SUBTYPE_DEFAULT**, default BSON type.
17 * - **BSON.BSON_BINARY_SUBTYPE_FUNCTION**, BSON function type.
18 * - **BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY**, BSON byte array type.
19 * - **BSON.BSON_BINARY_SUBTYPE_UUID**, BSON uuid type.
20 * - **BSON.BSON_BINARY_SUBTYPE_MD5**, BSON md5 type.
21 * - **BSON.BSON_BINARY_SUBTYPE_USER_DEFINED**, BSON user defined type.
24 * @param {Buffer} buffer a buffer object containing the binary data.
25 * @param {Number} [subType] the option binary type.
28 function Binary(buffer, subType) {
29 if(!(this instanceof Binary)) return new Binary(buffer, subType);
31 this._bsontype = 'Binary';
33 if(buffer instanceof Number) {
34 this.sub_type = buffer;
37 this.sub_type = subType == null ? BSON_BINARY_SUBTYPE_DEFAULT : subType;
41 if(buffer != null && !(buffer instanceof Number)) {
42 // Only accept Buffer, Uint8Array or Arrays
43 if(typeof buffer == 'string') {
44 // Different ways of writing the length of the string for the different types
45 if(typeof Buffer != 'undefined') {
46 this.buffer = new Buffer(buffer);
47 } else if(typeof Uint8Array != 'undefined' || (Object.prototype.toString.call(buffer) == '[object Array]')) {
48 this.buffer = writeStringToArray(buffer);
50 throw new Error("only String, Buffer, Uint8Array or Array accepted");
55 this.position = buffer.length;
57 if(typeof Buffer != 'undefined') {
58 this.buffer = new Buffer(Binary.BUFFER_SIZE);
59 } else if(typeof Uint8Array != 'undefined'){
60 this.buffer = new Uint8Array(new ArrayBuffer(Binary.BUFFER_SIZE));
62 this.buffer = new Array(Binary.BUFFER_SIZE);
64 // Set position to start of buffer
70 * Updates this binary with byte_value.
73 * @param {string} byte_value a single byte we wish to write.
75 Binary.prototype.put = function put(byte_value) {
76 // If it's a string and a has more than one character throw an error
77 if(byte_value['length'] != null && typeof byte_value != 'number' && byte_value.length != 1) throw new Error("only accepts single character String, Uint8Array or Array");
78 if(typeof byte_value != 'number' && byte_value < 0 || byte_value > 255) throw new Error("only accepts number in a valid unsigned byte range 0-255");
80 // Decode the byte value once
81 var decoded_byte = null;
82 if(typeof byte_value == 'string') {
83 decoded_byte = byte_value.charCodeAt(0);
84 } else if(byte_value['length'] != null) {
85 decoded_byte = byte_value[0];
87 decoded_byte = byte_value;
90 if(this.buffer.length > this.position) {
91 this.buffer[this.position++] = decoded_byte;
93 if(typeof Buffer != 'undefined' && Buffer.isBuffer(this.buffer)) {
94 // Create additional overflow buffer
95 var buffer = new Buffer(Binary.BUFFER_SIZE + this.buffer.length);
96 // Combine the two buffers together
97 this.buffer.copy(buffer, 0, 0, this.buffer.length);
99 this.buffer[this.position++] = decoded_byte;
102 // Create a new buffer (typed or normal array)
103 if(Object.prototype.toString.call(this.buffer) == '[object Uint8Array]') {
104 buffer = new Uint8Array(new ArrayBuffer(Binary.BUFFER_SIZE + this.buffer.length));
106 buffer = new Array(Binary.BUFFER_SIZE + this.buffer.length);
109 // We need to copy all the content to the new array
110 for(var i = 0; i < this.buffer.length; i++) {
111 buffer[i] = this.buffer[i];
114 // Reassign the buffer
115 this.buffer = buffer;
117 this.buffer[this.position++] = decoded_byte;
123 * Writes a buffer or string to the binary.
126 * @param {(Buffer|string)} string a string or buffer to be written to the Binary BSON object.
127 * @param {number} offset specify the binary of where to write the content.
130 Binary.prototype.write = function write(string, offset) {
131 offset = typeof offset == 'number' ? offset : this.position;
133 // If the buffer is to small let's extend the buffer
134 if(this.buffer.length < offset + string.length) {
136 // If we are in node.js
137 if(typeof Buffer != 'undefined' && Buffer.isBuffer(this.buffer)) {
138 buffer = new Buffer(this.buffer.length + string.length);
139 this.buffer.copy(buffer, 0, 0, this.buffer.length);
140 } else if(Object.prototype.toString.call(this.buffer) == '[object Uint8Array]') {
141 // Create a new buffer
142 buffer = new Uint8Array(new ArrayBuffer(this.buffer.length + string.length))
144 for(var i = 0; i < this.position; i++) {
145 buffer[i] = this.buffer[i];
149 // Assign the new buffer
150 this.buffer = buffer;
153 if(typeof Buffer != 'undefined' && Buffer.isBuffer(string) && Buffer.isBuffer(this.buffer)) {
154 string.copy(this.buffer, offset, 0, string.length);
155 this.position = (offset + string.length) > this.position ? (offset + string.length) : this.position;
156 // offset = string.length
157 } else if(typeof Buffer != 'undefined' && typeof string == 'string' && Buffer.isBuffer(this.buffer)) {
158 this.buffer.write(string, offset, 'binary');
159 this.position = (offset + string.length) > this.position ? (offset + string.length) : this.position;
160 // offset = string.length;
161 } else if(Object.prototype.toString.call(string) == '[object Uint8Array]'
162 || Object.prototype.toString.call(string) == '[object Array]' && typeof string != 'string') {
163 for(var i = 0; i < string.length; i++) {
164 this.buffer[offset++] = string[i];
167 this.position = offset > this.position ? offset : this.position;
168 } else if(typeof string == 'string') {
169 for(var i = 0; i < string.length; i++) {
170 this.buffer[offset++] = string.charCodeAt(i);
173 this.position = offset > this.position ? offset : this.position;
178 * Reads **length** bytes starting at **position**.
181 * @param {number} position read from the given position in the Binary.
182 * @param {number} length the number of bytes to read.
185 Binary.prototype.read = function read(position, length) {
186 length = length && length > 0
190 // Let's return the data based on the type we have
191 if(this.buffer['slice']) {
192 return this.buffer.slice(position, position + length);
194 // Create a buffer to keep the result
195 var buffer = typeof Uint8Array != 'undefined' ? new Uint8Array(new ArrayBuffer(length)) : new Array(length);
196 for(var i = 0; i < length; i++) {
197 buffer[i] = this.buffer[position++];
205 * Returns the value of this binary as a string.
210 Binary.prototype.value = function value(asRaw) {
211 asRaw = asRaw == null ? false : asRaw;
213 // Optimize to serialize for the situation where the data == size of buffer
214 if(asRaw && typeof Buffer != 'undefined' && Buffer.isBuffer(this.buffer) && this.buffer.length == this.position)
217 // If it's a node.js buffer object
218 if(typeof Buffer != 'undefined' && Buffer.isBuffer(this.buffer)) {
219 return asRaw ? this.buffer.slice(0, this.position) : this.buffer.toString('binary', 0, this.position);
222 // we support the slice command use it
223 if(this.buffer['slice'] != null) {
224 return this.buffer.slice(0, this.position);
226 // Create a new buffer to copy content to
227 var newBuffer = Object.prototype.toString.call(this.buffer) == '[object Uint8Array]' ? new Uint8Array(new ArrayBuffer(this.position)) : new Array(this.position);
229 for(var i = 0; i < this.position; i++) {
230 newBuffer[i] = this.buffer[i];
236 return convertArraytoUtf8BinaryString(this.buffer, 0, this.position);
245 * @return {number} the length of the binary.
247 Binary.prototype.length = function length() {
248 return this.position;
254 Binary.prototype.toJSON = function() {
255 return this.buffer != null ? this.buffer.toString('base64') : '';
261 Binary.prototype.toString = function(format) {
262 return this.buffer != null ? this.buffer.slice(0, this.position).toString(format) : '';
266 * Binary default subtype
269 var BSON_BINARY_SUBTYPE_DEFAULT = 0;
274 var writeStringToArray = function(data) {
276 var buffer = typeof Uint8Array != 'undefined' ? new Uint8Array(new ArrayBuffer(data.length)) : new Array(data.length);
277 // Write the content to the buffer
278 for(var i = 0; i < data.length; i++) {
279 buffer[i] = data.charCodeAt(i);
281 // Write the string to the buffer
286 * Convert Array ot Uint8Array to Binary String
290 var convertArraytoUtf8BinaryString = function(byteArray, startIndex, endIndex) {
292 for(var i = startIndex; i < endIndex; i++) {
293 result = result + String.fromCharCode(byteArray[i]);
298 Binary.BUFFER_SIZE = 256;
303 * @classconstant SUBTYPE_DEFAULT
305 Binary.SUBTYPE_DEFAULT = 0;
309 * @classconstant SUBTYPE_DEFAULT
311 Binary.SUBTYPE_FUNCTION = 1;
313 * Byte Array BSON type
315 * @classconstant SUBTYPE_DEFAULT
317 Binary.SUBTYPE_BYTE_ARRAY = 2;
321 * @classconstant SUBTYPE_DEFAULT
323 Binary.SUBTYPE_UUID_OLD = 3;
327 * @classconstant SUBTYPE_DEFAULT
329 Binary.SUBTYPE_UUID = 4;
333 * @classconstant SUBTYPE_DEFAULT
335 Binary.SUBTYPE_MD5 = 5;
339 * @classconstant SUBTYPE_DEFAULT
341 Binary.SUBTYPE_USER_DEFINED = 128;
346 module.exports = Binary;
347 module.exports.Binary = Binary;