binary.js 9.1 KB

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