binary.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Binary = void 0;
  4. var buffer_1 = require("buffer");
  5. var ensure_buffer_1 = require("./ensure_buffer");
  6. var uuid_utils_1 = require("./uuid_utils");
  7. var uuid_1 = require("./uuid");
  8. /**
  9. * A class representation of the BSON Binary type.
  10. * @public
  11. */
  12. var Binary = /** @class */ (function () {
  13. /**
  14. * @param buffer - a buffer object containing the binary data.
  15. * @param subType - the option binary type.
  16. */
  17. function Binary(buffer, subType) {
  18. if (!(this instanceof Binary))
  19. return new Binary(buffer, subType);
  20. if (!(buffer == null) &&
  21. !(typeof buffer === 'string') &&
  22. !ArrayBuffer.isView(buffer) &&
  23. !(buffer instanceof ArrayBuffer) &&
  24. !Array.isArray(buffer)) {
  25. throw new TypeError('Binary can only be constructed from string, Buffer, TypedArray, or Array<number>');
  26. }
  27. this.sub_type = subType !== null && subType !== void 0 ? subType : Binary.BSON_BINARY_SUBTYPE_DEFAULT;
  28. if (buffer == null) {
  29. // create an empty binary buffer
  30. this.buffer = buffer_1.Buffer.alloc(Binary.BUFFER_SIZE);
  31. this.position = 0;
  32. }
  33. else {
  34. if (typeof buffer === 'string') {
  35. // string
  36. this.buffer = buffer_1.Buffer.from(buffer, 'binary');
  37. }
  38. else if (Array.isArray(buffer)) {
  39. // number[]
  40. this.buffer = buffer_1.Buffer.from(buffer);
  41. }
  42. else {
  43. // Buffer | TypedArray | ArrayBuffer
  44. this.buffer = ensure_buffer_1.ensureBuffer(buffer);
  45. }
  46. this.position = this.buffer.byteLength;
  47. }
  48. }
  49. /**
  50. * Updates this binary with byte_value.
  51. *
  52. * @param byteValue - a single byte we wish to write.
  53. */
  54. Binary.prototype.put = function (byteValue) {
  55. // If it's a string and a has more than one character throw an error
  56. if (typeof byteValue === 'string' && byteValue.length !== 1) {
  57. throw new TypeError('only accepts single character String');
  58. }
  59. else if (typeof byteValue !== 'number' && byteValue.length !== 1)
  60. throw new TypeError('only accepts single character Uint8Array or Array');
  61. // Decode the byte value once
  62. var decodedByte;
  63. if (typeof byteValue === 'string') {
  64. decodedByte = byteValue.charCodeAt(0);
  65. }
  66. else if (typeof byteValue === 'number') {
  67. decodedByte = byteValue;
  68. }
  69. else {
  70. decodedByte = byteValue[0];
  71. }
  72. if (decodedByte < 0 || decodedByte > 255) {
  73. throw new TypeError('only accepts number in a valid unsigned byte range 0-255');
  74. }
  75. if (this.buffer.length > this.position) {
  76. this.buffer[this.position++] = decodedByte;
  77. }
  78. else {
  79. var buffer = buffer_1.Buffer.alloc(Binary.BUFFER_SIZE + this.buffer.length);
  80. // Combine the two buffers together
  81. this.buffer.copy(buffer, 0, 0, this.buffer.length);
  82. this.buffer = buffer;
  83. this.buffer[this.position++] = decodedByte;
  84. }
  85. };
  86. /**
  87. * Writes a buffer or string to the binary.
  88. *
  89. * @param sequence - a string or buffer to be written to the Binary BSON object.
  90. * @param offset - specify the binary of where to write the content.
  91. */
  92. Binary.prototype.write = function (sequence, offset) {
  93. offset = typeof offset === 'number' ? offset : this.position;
  94. // If the buffer is to small let's extend the buffer
  95. if (this.buffer.length < offset + sequence.length) {
  96. var buffer = buffer_1.Buffer.alloc(this.buffer.length + sequence.length);
  97. this.buffer.copy(buffer, 0, 0, this.buffer.length);
  98. // Assign the new buffer
  99. this.buffer = buffer;
  100. }
  101. if (ArrayBuffer.isView(sequence)) {
  102. this.buffer.set(ensure_buffer_1.ensureBuffer(sequence), offset);
  103. this.position =
  104. offset + sequence.byteLength > this.position ? offset + sequence.length : this.position;
  105. }
  106. else if (typeof sequence === 'string') {
  107. this.buffer.write(sequence, offset, sequence.length, 'binary');
  108. this.position =
  109. offset + sequence.length > this.position ? offset + sequence.length : this.position;
  110. }
  111. };
  112. /**
  113. * Reads **length** bytes starting at **position**.
  114. *
  115. * @param position - read from the given position in the Binary.
  116. * @param length - the number of bytes to read.
  117. */
  118. Binary.prototype.read = function (position, length) {
  119. length = length && length > 0 ? length : this.position;
  120. // Let's return the data based on the type we have
  121. return this.buffer.slice(position, position + length);
  122. };
  123. /**
  124. * Returns the value of this binary as a string.
  125. * @param asRaw - Will skip converting to a string
  126. * @remarks
  127. * This is handy when calling this function conditionally for some key value pairs and not others
  128. */
  129. Binary.prototype.value = function (asRaw) {
  130. asRaw = !!asRaw;
  131. // Optimize to serialize for the situation where the data == size of buffer
  132. if (asRaw && this.buffer.length === this.position) {
  133. return this.buffer;
  134. }
  135. // If it's a node.js buffer object
  136. if (asRaw) {
  137. return this.buffer.slice(0, this.position);
  138. }
  139. return this.buffer.toString('binary', 0, this.position);
  140. };
  141. /** the length of the binary sequence */
  142. Binary.prototype.length = function () {
  143. return this.position;
  144. };
  145. /** @internal */
  146. Binary.prototype.toJSON = function () {
  147. return this.buffer.toString('base64');
  148. };
  149. /** @internal */
  150. Binary.prototype.toString = function (format) {
  151. return this.buffer.toString(format);
  152. };
  153. /** @internal */
  154. Binary.prototype.toExtendedJSON = function (options) {
  155. options = options || {};
  156. var base64String = this.buffer.toString('base64');
  157. var subType = Number(this.sub_type).toString(16);
  158. if (options.legacy) {
  159. return {
  160. $binary: base64String,
  161. $type: subType.length === 1 ? '0' + subType : subType
  162. };
  163. }
  164. return {
  165. $binary: {
  166. base64: base64String,
  167. subType: subType.length === 1 ? '0' + subType : subType
  168. }
  169. };
  170. };
  171. /** @internal */
  172. Binary.prototype.toUUID = function () {
  173. if (this.sub_type === Binary.SUBTYPE_UUID) {
  174. return new uuid_1.UUID(this.buffer.slice(0, this.position));
  175. }
  176. throw new Error("Binary sub_type \"" + this.sub_type + "\" is not supported for converting to UUID. Only \"" + Binary.SUBTYPE_UUID + "\" is currently supported.");
  177. };
  178. /** @internal */
  179. Binary.fromExtendedJSON = function (doc, options) {
  180. options = options || {};
  181. var data;
  182. var type;
  183. if ('$binary' in doc) {
  184. if (options.legacy && typeof doc.$binary === 'string' && '$type' in doc) {
  185. type = doc.$type ? parseInt(doc.$type, 16) : 0;
  186. data = buffer_1.Buffer.from(doc.$binary, 'base64');
  187. }
  188. else {
  189. if (typeof doc.$binary !== 'string') {
  190. type = doc.$binary.subType ? parseInt(doc.$binary.subType, 16) : 0;
  191. data = buffer_1.Buffer.from(doc.$binary.base64, 'base64');
  192. }
  193. }
  194. }
  195. else if ('$uuid' in doc) {
  196. type = 4;
  197. data = uuid_utils_1.uuidHexStringToBuffer(doc.$uuid);
  198. }
  199. if (!data) {
  200. throw new TypeError("Unexpected Binary Extended JSON format " + JSON.stringify(doc));
  201. }
  202. return new Binary(data, type);
  203. };
  204. /** @internal */
  205. Binary.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
  206. return this.inspect();
  207. };
  208. Binary.prototype.inspect = function () {
  209. var asBuffer = this.value(true);
  210. return "new Binary(Buffer.from(\"" + asBuffer.toString('hex') + "\", \"hex\"), " + this.sub_type + ")";
  211. };
  212. /**
  213. * Binary default subtype
  214. * @internal
  215. */
  216. Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
  217. /** Initial buffer default size */
  218. Binary.BUFFER_SIZE = 256;
  219. /** Default BSON type */
  220. Binary.SUBTYPE_DEFAULT = 0;
  221. /** Function BSON type */
  222. Binary.SUBTYPE_FUNCTION = 1;
  223. /** Byte Array BSON type */
  224. Binary.SUBTYPE_BYTE_ARRAY = 2;
  225. /** Deprecated UUID BSON type @deprecated Please use SUBTYPE_UUID */
  226. Binary.SUBTYPE_UUID_OLD = 3;
  227. /** UUID BSON type */
  228. Binary.SUBTYPE_UUID = 4;
  229. /** MD5 BSON type */
  230. Binary.SUBTYPE_MD5 = 5;
  231. /** User BSON type */
  232. Binary.SUBTYPE_USER_DEFINED = 128;
  233. return Binary;
  234. }());
  235. exports.Binary = Binary;
  236. Object.defineProperty(Binary.prototype, '_bsontype', { value: 'Binary' });
  237. //# sourceMappingURL=binary.js.map