binary.js 9.2 KB

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