buffer.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*!
  2. * Module dependencies.
  3. */
  4. 'use strict';
  5. const MongooseBuffer = require('../types/buffer');
  6. const SchemaBufferOptions = require('../options/schemaBufferOptions');
  7. const SchemaType = require('../schemaType');
  8. const createJSONSchemaTypeDefinition = require('../helpers/createJSONSchemaTypeDefinition');
  9. const handleBitwiseOperator = require('./operators/bitwise');
  10. const utils = require('../utils');
  11. const Binary = MongooseBuffer.Binary;
  12. const CastError = SchemaType.CastError;
  13. /**
  14. * Buffer SchemaType constructor
  15. *
  16. * @param {String} key
  17. * @param {Object} options
  18. * @param {Object} schemaOptions
  19. * @param {Schema} parentSchema
  20. * @inherits SchemaType
  21. * @api public
  22. */
  23. function SchemaBuffer(key, options, _schemaOptions, parentSchema) {
  24. SchemaType.call(this, key, options, 'Buffer', parentSchema);
  25. }
  26. /**
  27. * This schema type's name, to defend against minifiers that mangle
  28. * function names.
  29. *
  30. * @api public
  31. */
  32. SchemaBuffer.schemaName = 'Buffer';
  33. SchemaBuffer.defaultOptions = {};
  34. /*!
  35. * Inherits from SchemaType.
  36. */
  37. SchemaBuffer.prototype = Object.create(SchemaType.prototype);
  38. SchemaBuffer.prototype.constructor = SchemaBuffer;
  39. SchemaBuffer.prototype.OptionsConstructor = SchemaBufferOptions;
  40. /*!
  41. * ignore
  42. */
  43. SchemaBuffer._checkRequired = v => !!v?.length;
  44. /**
  45. * Sets a default option for all Buffer instances.
  46. *
  47. * #### Example:
  48. *
  49. * // Make all buffers have `required` of true by default.
  50. * mongoose.Schema.Buffer.set('required', true);
  51. *
  52. * const User = mongoose.model('User', new Schema({ test: Buffer }));
  53. * new User({ }).validateSync().errors.test.message; // Path `test` is required.
  54. *
  55. * @param {String} option The option you'd like to set the value for
  56. * @param {Any} value value for option
  57. * @return {undefined}
  58. * @function set
  59. * @static
  60. * @api public
  61. */
  62. SchemaBuffer.set = SchemaType.set;
  63. SchemaBuffer.setters = [];
  64. /**
  65. * Attaches a getter for all Buffer instances
  66. *
  67. * #### Example:
  68. *
  69. * // Always convert to string when getting an ObjectId
  70. * mongoose.Schema.Types.Buffer.get(v => v.toString('hex'));
  71. *
  72. * const Model = mongoose.model('Test', new Schema({ buf: Buffer } }));
  73. * typeof (new Model({ buf: Buffer.fromString('hello') }).buf); // 'string'
  74. *
  75. * @param {Function} getter
  76. * @return {this}
  77. * @function get
  78. * @static
  79. * @api public
  80. */
  81. SchemaBuffer.get = SchemaType.get;
  82. /**
  83. * Override the function the required validator uses to check whether a string
  84. * passes the `required` check.
  85. *
  86. * #### Example:
  87. *
  88. * // Allow empty strings to pass `required` check
  89. * mongoose.Schema.Types.String.checkRequired(v => v != null);
  90. *
  91. * const M = mongoose.model({ buf: { type: Buffer, required: true } });
  92. * new M({ buf: Buffer.from('') }).validateSync(); // validation passes!
  93. *
  94. * @param {Function} fn
  95. * @return {Function}
  96. * @function checkRequired
  97. * @static
  98. * @api public
  99. */
  100. SchemaBuffer.checkRequired = SchemaType.checkRequired;
  101. /**
  102. * Check if the given value satisfies a required validator. To satisfy a
  103. * required validator, a buffer must not be null or undefined and have
  104. * non-zero length.
  105. *
  106. * @param {Any} value
  107. * @param {Document} doc
  108. * @return {Boolean}
  109. * @api public
  110. */
  111. SchemaBuffer.prototype.checkRequired = function(value, doc) {
  112. if (SchemaType._isRef(this, value, doc, true)) {
  113. return !!value;
  114. }
  115. return this.constructor._checkRequired(value);
  116. };
  117. /**
  118. * Casts contents
  119. *
  120. * @param {Object} value
  121. * @param {Document} doc document that triggers the casting
  122. * @param {Boolean} init
  123. * @api private
  124. */
  125. SchemaBuffer.prototype.cast = function(value, doc, init, prev, options) {
  126. let ret;
  127. if (SchemaType._isRef(this, value, doc, init)) {
  128. if (value?.isMongooseBuffer) {
  129. return value;
  130. }
  131. if (Buffer.isBuffer(value)) {
  132. if (!value?.isMongooseBuffer) {
  133. value = new MongooseBuffer(value, [this.path, doc]);
  134. if (this.options.subtype != null) {
  135. value._subtype = this.options.subtype;
  136. }
  137. }
  138. return value;
  139. }
  140. if (value instanceof Binary) {
  141. ret = new MongooseBuffer(value.value(true), [this.path, doc]);
  142. if (typeof value.sub_type !== 'number') {
  143. throw new CastError('Buffer', value, this.path, null, this);
  144. }
  145. ret._subtype = value.sub_type;
  146. return ret;
  147. }
  148. if (value == null || utils.isNonBuiltinObject(value)) {
  149. return this._castRef(value, doc, init, options);
  150. }
  151. }
  152. // documents
  153. if (value?._id) {
  154. value = value._id;
  155. }
  156. if (value?.isMongooseBuffer) {
  157. return value;
  158. }
  159. if (Buffer.isBuffer(value)) {
  160. if (!value?.isMongooseBuffer) {
  161. value = new MongooseBuffer(value, [this.path, doc]);
  162. if (this.options.subtype != null) {
  163. value._subtype = this.options.subtype;
  164. }
  165. }
  166. return value;
  167. }
  168. if (value instanceof Binary) {
  169. ret = new MongooseBuffer(value.value(true), [this.path, doc]);
  170. if (typeof value.sub_type !== 'number') {
  171. throw new CastError('Buffer', value, this.path, null, this);
  172. }
  173. ret._subtype = value.sub_type;
  174. return ret;
  175. }
  176. if (value === null) {
  177. return value;
  178. }
  179. const type = typeof value;
  180. if (
  181. type === 'string' || type === 'number' || Array.isArray(value) ||
  182. (type === 'object' && value.type === 'Buffer' && Array.isArray(value.data)) // gh-6863
  183. ) {
  184. if (type === 'number') {
  185. value = [value];
  186. }
  187. ret = new MongooseBuffer(value, [this.path, doc]);
  188. if (this.options.subtype != null) {
  189. ret._subtype = this.options.subtype;
  190. }
  191. return ret;
  192. }
  193. if (utils.isPOJO(value) && (value.$binary instanceof Binary || typeof value.$binary === 'string')) {
  194. const buf = this.cast(Buffer.from(value.$binary, 'base64'));
  195. if (value.$type != null) {
  196. buf._subtype = value.$type;
  197. return buf;
  198. }
  199. }
  200. throw new CastError('Buffer', value, this.path, null, this);
  201. };
  202. /**
  203. * Sets the default [subtype](https://studio3t.com/whats-new/best-practices-uuid-mongodb/)
  204. * for this buffer. You can find a [list of allowed subtypes here](https://api.mongodb.com/python/current/api/bson/binary.html).
  205. *
  206. * #### Example:
  207. *
  208. * const s = new Schema({ uuid: { type: Buffer, subtype: 4 });
  209. * const M = db.model('M', s);
  210. * const m = new M({ uuid: 'test string' });
  211. * m.uuid._subtype; // 4
  212. *
  213. * @param {Number} subtype the default subtype
  214. * @return {SchemaType} this
  215. * @api public
  216. */
  217. SchemaBuffer.prototype.subtype = function(subtype) {
  218. this.options.subtype = subtype;
  219. return this;
  220. };
  221. /*!
  222. * ignore
  223. */
  224. function handleSingle(val, context) {
  225. return this.castForQuery(null, val, context);
  226. }
  227. const $conditionalHandlers = {
  228. ...SchemaType.prototype.$conditionalHandlers,
  229. $bitsAllClear: handleBitwiseOperator,
  230. $bitsAnyClear: handleBitwiseOperator,
  231. $bitsAllSet: handleBitwiseOperator,
  232. $bitsAnySet: handleBitwiseOperator,
  233. $gt: handleSingle,
  234. $gte: handleSingle,
  235. $lt: handleSingle,
  236. $lte: handleSingle
  237. };
  238. /**
  239. * Contains the handlers for different query operators for this schema type.
  240. * For example, `$conditionalHandlers.$exists` is the function Mongoose calls to cast `$exists` filter operators.
  241. *
  242. * @property $conditionalHandlers
  243. * @memberOf SchemaBuffer
  244. * @instance
  245. * @api public
  246. */
  247. Object.defineProperty(SchemaBuffer.prototype, '$conditionalHandlers', {
  248. enumerable: false,
  249. value: $conditionalHandlers
  250. });
  251. /**
  252. * Casts contents for queries.
  253. *
  254. * @param {String} $conditional
  255. * @param {any} [value]
  256. * @api private
  257. */
  258. SchemaBuffer.prototype.castForQuery = function($conditional, val, context) {
  259. let handler;
  260. if ($conditional != null) {
  261. handler = this.$conditionalHandlers[$conditional];
  262. if (!handler) {
  263. throw new Error('Can\'t use ' + $conditional + ' with Buffer.');
  264. }
  265. return handler.call(this, val);
  266. }
  267. let casted;
  268. try {
  269. casted = this.applySetters(val, context);
  270. } catch (err) {
  271. if (err instanceof CastError && err.path === this.path && this.$fullPath != null) {
  272. err.path = this.$fullPath;
  273. }
  274. throw err;
  275. }
  276. return casted ? casted.toObject({ transform: false, virtuals: false }) : casted;
  277. };
  278. /**
  279. * Returns this schema type's representation in a JSON schema.
  280. *
  281. * @param [options]
  282. * @param [options.useBsonType=false] If true, return a representation with `bsonType` for use with MongoDB's `$jsonSchema`.
  283. * @returns {Object} JSON schema properties
  284. */
  285. SchemaBuffer.prototype.toJSONSchema = function toJSONSchema(options) {
  286. const isRequired = this.options.required && typeof this.options.required !== 'function';
  287. return createJSONSchemaTypeDefinition('string', 'binData', options?.useBsonType, isRequired);
  288. };
  289. SchemaBuffer.prototype.autoEncryptionType = function autoEncryptionType() {
  290. return 'binData';
  291. };
  292. /*!
  293. * Module exports.
  294. */
  295. module.exports = SchemaBuffer;