Source: node_modules/bson/lib/bson/binary.js

  1. /**
  2. * Module dependencies.
  3. * @ignore
  4. */
  5. // Test if we're in Node via presence of "global" not absence of "window"
  6. // to support hybrid environments like Electron
  7. if (typeof global !== 'undefined') {
  8. var Buffer = require('buffer').Buffer; // TODO just use global Buffer
  9. }
  10. /**
  11. * A class representation of the BSON Binary type.
  12. *
  13. * Sub types
  14. * - **BSON.BSON_BINARY_SUBTYPE_DEFAULT**, default BSON type.
  15. * - **BSON.BSON_BINARY_SUBTYPE_FUNCTION**, BSON function type.
  16. * - **BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY**, BSON byte array type.
  17. * - **BSON.BSON_BINARY_SUBTYPE_UUID**, BSON uuid type.
  18. * - **BSON.BSON_BINARY_SUBTYPE_MD5**, BSON md5 type.
  19. * - **BSON.BSON_BINARY_SUBTYPE_USER_DEFINED**, BSON user defined type.
  20. *
  21. * @class
  22. * @param {Buffer} buffer a buffer object containing the binary data.
  23. * @param {Number} [subType] the option binary type.
  24. * @return {Binary}
  25. */
  26. function Binary(buffer, subType) {
  27. if (!(this instanceof Binary)) return new Binary(buffer, subType);
  28. if (
  29. buffer != null &&
  30. !(typeof buffer === 'string') &&
  31. !Buffer.isBuffer(buffer) &&
  32. !(buffer instanceof Uint8Array) &&
  33. !Array.isArray(buffer)
  34. ) {
  35. throw new Error('only String, Buffer, Uint8Array or Array accepted');
  36. }
  37. this._bsontype = 'Binary';
  38. if (buffer instanceof Number) {
  39. this.sub_type = buffer;
  40. this.position = 0;
  41. } else {
  42. this.sub_type = subType == null ? BSON_BINARY_SUBTYPE_DEFAULT : subType;
  43. this.position = 0;
  44. }
  45. if (buffer != null && !(buffer instanceof Number)) {
  46. // Only accept Buffer, Uint8Array or Arrays
  47. if (typeof buffer === 'string') {
  48. // Different ways of writing the length of the string for the different types
  49. if (typeof Buffer !== 'undefined') {
  50. this.buffer = new Buffer(buffer);
  51. } else if (
  52. typeof Uint8Array !== 'undefined' ||
  53. Object.prototype.toString.call(buffer) === '[object Array]'
  54. ) {
  55. this.buffer = writeStringToArray(buffer);
  56. } else {
  57. throw new Error('only String, Buffer, Uint8Array or Array accepted');
  58. }
  59. } else {
  60. this.buffer = buffer;
  61. }
  62. this.position = buffer.length;
  63. } else {
  64. if (typeof Buffer !== 'undefined') {
  65. this.buffer = new Buffer(Binary.BUFFER_SIZE);
  66. } else if (typeof Uint8Array !== 'undefined') {
  67. this.buffer = new Uint8Array(new ArrayBuffer(Binary.BUFFER_SIZE));
  68. } else {
  69. this.buffer = new Array(Binary.BUFFER_SIZE);
  70. }
  71. // Set position to start of buffer
  72. this.position = 0;
  73. }
  74. }
  75. /**
  76. * Updates this binary with byte_value.
  77. *
  78. * @method
  79. * @param {string} byte_value a single byte we wish to write.
  80. */
  81. Binary.prototype.put = function put(byte_value) {
  82. // If it's a string and a has more than one character throw an error
  83. if (byte_value['length'] != null && typeof byte_value !== 'number' && byte_value.length !== 1)
  84. throw new Error('only accepts single character String, Uint8Array or Array');
  85. if ((typeof byte_value !== 'number' && byte_value < 0) || byte_value > 255)
  86. throw new Error('only accepts number in a valid unsigned byte range 0-255');
  87. // Decode the byte value once
  88. var decoded_byte = null;
  89. if (typeof byte_value === 'string') {
  90. decoded_byte = byte_value.charCodeAt(0);
  91. } else if (byte_value['length'] != null) {
  92. decoded_byte = byte_value[0];
  93. } else {
  94. decoded_byte = byte_value;
  95. }
  96. if (this.buffer.length > this.position) {
  97. this.buffer[this.position++] = decoded_byte;
  98. } else {
  99. if (typeof Buffer !== 'undefined' && Buffer.isBuffer(this.buffer)) {
  100. // Create additional overflow buffer
  101. var buffer = new Buffer(Binary.BUFFER_SIZE + this.buffer.length);
  102. // Combine the two buffers together
  103. this.buffer.copy(buffer, 0, 0, this.buffer.length);
  104. this.buffer = buffer;
  105. this.buffer[this.position++] = decoded_byte;
  106. } else {
  107. buffer = null;
  108. // Create a new buffer (typed or normal array)
  109. if (Object.prototype.toString.call(this.buffer) === '[object Uint8Array]') {
  110. buffer = new Uint8Array(new ArrayBuffer(Binary.BUFFER_SIZE + this.buffer.length));
  111. } else {
  112. buffer = new Array(Binary.BUFFER_SIZE + this.buffer.length);
  113. }
  114. // We need to copy all the content to the new array
  115. for (var i = 0; i < this.buffer.length; i++) {
  116. buffer[i] = this.buffer[i];
  117. }
  118. // Reassign the buffer
  119. this.buffer = buffer;
  120. // Write the byte
  121. this.buffer[this.position++] = decoded_byte;
  122. }
  123. }
  124. };
  125. /**
  126. * Writes a buffer or string to the binary.
  127. *
  128. * @method
  129. * @param {(Buffer|string)} string a string or buffer to be written to the Binary BSON object.
  130. * @param {number} offset specify the binary of where to write the content.
  131. * @return {null}
  132. */
  133. Binary.prototype.write = function write(string, offset) {
  134. offset = typeof offset === 'number' ? offset : this.position;
  135. // If the buffer is to small let's extend the buffer
  136. if (this.buffer.length < offset + string.length) {
  137. var buffer = null;
  138. // If we are in node.js
  139. if (typeof Buffer !== 'undefined' && Buffer.isBuffer(this.buffer)) {
  140. buffer = new Buffer(this.buffer.length + string.length);
  141. this.buffer.copy(buffer, 0, 0, this.buffer.length);
  142. } else if (Object.prototype.toString.call(this.buffer) === '[object Uint8Array]') {
  143. // Create a new buffer
  144. buffer = new Uint8Array(new ArrayBuffer(this.buffer.length + string.length));
  145. // Copy the content
  146. for (var i = 0; i < this.position; i++) {
  147. buffer[i] = this.buffer[i];
  148. }
  149. }
  150. // Assign the new buffer
  151. this.buffer = buffer;
  152. }
  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 (
  158. typeof Buffer !== 'undefined' &&
  159. typeof string === 'string' &&
  160. Buffer.isBuffer(this.buffer)
  161. ) {
  162. this.buffer.write(string, offset, 'binary');
  163. this.position = offset + string.length > this.position ? offset + string.length : this.position;
  164. // offset = string.length;
  165. } else if (
  166. Object.prototype.toString.call(string) === '[object Uint8Array]' ||
  167. (Object.prototype.toString.call(string) === '[object Array]' && typeof string !== 'string')
  168. ) {
  169. for (i = 0; i < string.length; i++) {
  170. this.buffer[offset++] = string[i];
  171. }
  172. this.position = offset > this.position ? offset : this.position;
  173. } else if (typeof string === 'string') {
  174. for (i = 0; i < string.length; i++) {
  175. this.buffer[offset++] = string.charCodeAt(i);
  176. }
  177. this.position = offset > this.position ? offset : this.position;
  178. }
  179. };
  180. /**
  181. * Reads **length** bytes starting at **position**.
  182. *
  183. * @method
  184. * @param {number} position read from the given position in the Binary.
  185. * @param {number} length the number of bytes to read.
  186. * @return {Buffer}
  187. */
  188. Binary.prototype.read = function read(position, length) {
  189. length = length && length > 0 ? length : this.position;
  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);
  193. } else {
  194. // Create a buffer to keep the result
  195. var buffer =
  196. typeof Uint8Array !== 'undefined'
  197. ? new Uint8Array(new ArrayBuffer(length))
  198. : new Array(length);
  199. for (var i = 0; i < length; i++) {
  200. buffer[i] = this.buffer[position++];
  201. }
  202. }
  203. // Return the buffer
  204. return buffer;
  205. };
  206. /**
  207. * Returns the value of this binary as a string.
  208. *
  209. * @method
  210. * @return {string}
  211. */
  212. Binary.prototype.value = function value(asRaw) {
  213. asRaw = asRaw == null ? false : asRaw;
  214. // Optimize to serialize for the situation where the data == size of buffer
  215. if (
  216. asRaw &&
  217. typeof Buffer !== 'undefined' &&
  218. Buffer.isBuffer(this.buffer) &&
  219. this.buffer.length === this.position
  220. )
  221. return this.buffer;
  222. // If it's a node.js buffer object
  223. if (typeof Buffer !== 'undefined' && Buffer.isBuffer(this.buffer)) {
  224. return asRaw
  225. ? this.buffer.slice(0, this.position)
  226. : this.buffer.toString('binary', 0, this.position);
  227. } else {
  228. if (asRaw) {
  229. // we support the slice command use it
  230. if (this.buffer['slice'] != null) {
  231. return this.buffer.slice(0, this.position);
  232. } else {
  233. // Create a new buffer to copy content to
  234. var newBuffer =
  235. Object.prototype.toString.call(this.buffer) === '[object Uint8Array]'
  236. ? new Uint8Array(new ArrayBuffer(this.position))
  237. : new Array(this.position);
  238. // Copy content
  239. for (var i = 0; i < this.position; i++) {
  240. newBuffer[i] = this.buffer[i];
  241. }
  242. // Return the buffer
  243. return newBuffer;
  244. }
  245. } else {
  246. return convertArraytoUtf8BinaryString(this.buffer, 0, this.position);
  247. }
  248. }
  249. };
  250. /**
  251. * Length.
  252. *
  253. * @method
  254. * @return {number} the length of the binary.
  255. */
  256. Binary.prototype.length = function length() {
  257. return this.position;
  258. };
  259. /**
  260. * @ignore
  261. */
  262. Binary.prototype.toJSON = function() {
  263. return this.buffer != null ? this.buffer.toString('base64') : '';
  264. };
  265. /**
  266. * @ignore
  267. */
  268. Binary.prototype.toString = function(format) {
  269. return this.buffer != null ? this.buffer.slice(0, this.position).toString(format) : '';
  270. };
  271. /**
  272. * Binary default subtype
  273. * @ignore
  274. */
  275. var BSON_BINARY_SUBTYPE_DEFAULT = 0;
  276. /**
  277. * @ignore
  278. */
  279. var writeStringToArray = function(data) {
  280. // Create a buffer
  281. var buffer =
  282. typeof Uint8Array !== 'undefined'
  283. ? new Uint8Array(new ArrayBuffer(data.length))
  284. : new Array(data.length);
  285. // Write the content to the buffer
  286. for (var i = 0; i < data.length; i++) {
  287. buffer[i] = data.charCodeAt(i);
  288. }
  289. // Write the string to the buffer
  290. return buffer;
  291. };
  292. /**
  293. * Convert Array ot Uint8Array to Binary String
  294. *
  295. * @ignore
  296. */
  297. var convertArraytoUtf8BinaryString = function(byteArray, startIndex, endIndex) {
  298. var result = '';
  299. for (var i = startIndex; i < endIndex; i++) {
  300. result = result + String.fromCharCode(byteArray[i]);
  301. }
  302. return result;
  303. };
  304. Binary.BUFFER_SIZE = 256;
  305. /**
  306. * Default BSON type
  307. *
  308. * @classconstant SUBTYPE_DEFAULT
  309. **/
  310. Binary.SUBTYPE_DEFAULT = 0;
  311. /**
  312. * Function BSON type
  313. *
  314. * @classconstant SUBTYPE_DEFAULT
  315. **/
  316. Binary.SUBTYPE_FUNCTION = 1;
  317. /**
  318. * Byte Array BSON type
  319. *
  320. * @classconstant SUBTYPE_DEFAULT
  321. **/
  322. Binary.SUBTYPE_BYTE_ARRAY = 2;
  323. /**
  324. * OLD UUID BSON type
  325. *
  326. * @classconstant SUBTYPE_DEFAULT
  327. **/
  328. Binary.SUBTYPE_UUID_OLD = 3;
  329. /**
  330. * UUID BSON type
  331. *
  332. * @classconstant SUBTYPE_DEFAULT
  333. **/
  334. Binary.SUBTYPE_UUID = 4;
  335. /**
  336. * MD5 BSON type
  337. *
  338. * @classconstant SUBTYPE_DEFAULT
  339. **/
  340. Binary.SUBTYPE_MD5 = 5;
  341. /**
  342. * User BSON type
  343. *
  344. * @classconstant SUBTYPE_DEFAULT
  345. **/
  346. Binary.SUBTYPE_USER_DEFINED = 128;
  347. /**
  348. * Expose.
  349. */
  350. module.exports = Binary;
  351. module.exports.Binary = Binary;