Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

buffer.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*!
  2. * Module dependencies.
  3. */
  4. 'use strict';
  5. const handleBitwiseOperator = require('./operators/bitwise');
  6. const utils = require('../utils');
  7. const populateModelSymbol = require('../helpers/symbols').populateModelSymbol;
  8. const MongooseBuffer = require('../types/buffer');
  9. const SchemaType = require('../schematype');
  10. const Binary = MongooseBuffer.Binary;
  11. const CastError = SchemaType.CastError;
  12. let Document;
  13. /**
  14. * Buffer SchemaType constructor
  15. *
  16. * @param {String} key
  17. * @param {Object} options
  18. * @inherits SchemaType
  19. * @api public
  20. */
  21. function SchemaBuffer(key, options) {
  22. SchemaType.call(this, key, options, 'Buffer');
  23. }
  24. /**
  25. * This schema type's name, to defend against minifiers that mangle
  26. * function names.
  27. *
  28. * @api public
  29. */
  30. SchemaBuffer.schemaName = 'Buffer';
  31. /*!
  32. * Inherits from SchemaType.
  33. */
  34. SchemaBuffer.prototype = Object.create(SchemaType.prototype);
  35. SchemaBuffer.prototype.constructor = SchemaBuffer;
  36. /*!
  37. * ignore
  38. */
  39. SchemaBuffer._checkRequired = v => !!(v && v.length);
  40. /**
  41. * Override the function the required validator uses to check whether a string
  42. * passes the `required` check.
  43. *
  44. * ####Example:
  45. *
  46. * // Allow empty strings to pass `required` check
  47. * mongoose.Schema.Types.String.checkRequired(v => v != null);
  48. *
  49. * const M = mongoose.model({ buf: { type: Buffer, required: true } });
  50. * new M({ buf: Buffer.from('') }).validateSync(); // validation passes!
  51. *
  52. * @param {Function} fn
  53. * @return {Function}
  54. * @function checkRequired
  55. * @static
  56. * @api public
  57. */
  58. SchemaBuffer.checkRequired = SchemaType.checkRequired;
  59. /**
  60. * Check if the given value satisfies a required validator. To satisfy a
  61. * required validator, a buffer must not be null or undefined and have
  62. * non-zero length.
  63. *
  64. * @param {Any} value
  65. * @param {Document} doc
  66. * @return {Boolean}
  67. * @api public
  68. */
  69. SchemaBuffer.prototype.checkRequired = function(value, doc) {
  70. if (SchemaType._isRef(this, value, doc, true)) {
  71. return !!value;
  72. }
  73. return this.constructor._checkRequired(value);
  74. };
  75. /**
  76. * Casts contents
  77. *
  78. * @param {Object} value
  79. * @param {Document} doc document that triggers the casting
  80. * @param {Boolean} init
  81. * @api private
  82. */
  83. SchemaBuffer.prototype.cast = function(value, doc, init) {
  84. let ret;
  85. if (SchemaType._isRef(this, value, doc, init)) {
  86. // wait! we may need to cast this to a document
  87. if (value === null || value === undefined) {
  88. return value;
  89. }
  90. // lazy load
  91. Document || (Document = require('./../document'));
  92. if (value instanceof Document) {
  93. value.$__.wasPopulated = true;
  94. return value;
  95. }
  96. // setting a populated path
  97. if (Buffer.isBuffer(value)) {
  98. return value;
  99. } else if (!utils.isObject(value)) {
  100. throw new CastError('buffer', value, this.path);
  101. }
  102. // Handle the case where user directly sets a populated
  103. // path to a plain object; cast to the Model used in
  104. // the population query.
  105. const path = doc.$__fullPath(this.path);
  106. const owner = doc.ownerDocument ? doc.ownerDocument() : doc;
  107. const pop = owner.populated(path, true);
  108. ret = new pop.options[populateModelSymbol](value);
  109. ret.$__.wasPopulated = true;
  110. return ret;
  111. }
  112. // documents
  113. if (value && value._id) {
  114. value = value._id;
  115. }
  116. if (value && value.isMongooseBuffer) {
  117. return value;
  118. }
  119. if (Buffer.isBuffer(value)) {
  120. if (!value || !value.isMongooseBuffer) {
  121. value = new MongooseBuffer(value, [this.path, doc]);
  122. if (this.options.subtype != null) {
  123. value._subtype = this.options.subtype;
  124. }
  125. }
  126. return value;
  127. }
  128. if (value instanceof Binary) {
  129. ret = new MongooseBuffer(value.value(true), [this.path, doc]);
  130. if (typeof value.sub_type !== 'number') {
  131. throw new CastError('buffer', value, this.path);
  132. }
  133. ret._subtype = value.sub_type;
  134. return ret;
  135. }
  136. if (value === null) {
  137. return value;
  138. }
  139. const type = typeof value;
  140. if (
  141. type === 'string' || type === 'number' || Array.isArray(value) ||
  142. (type === 'object' && value.type === 'Buffer' && Array.isArray(value.data)) // gh-6863
  143. ) {
  144. if (type === 'number') {
  145. value = [value];
  146. }
  147. ret = new MongooseBuffer(value, [this.path, doc]);
  148. if (this.options.subtype != null) {
  149. ret._subtype = this.options.subtype;
  150. }
  151. return ret;
  152. }
  153. throw new CastError('buffer', value, this.path);
  154. };
  155. /**
  156. * Sets the default [subtype](https://studio3t.com/whats-new/best-practices-uuid-mongodb/)
  157. * for this buffer. You can find a [list of allowed subtypes here](http://api.mongodb.com/python/current/api/bson/binary.html).
  158. *
  159. * ####Example:
  160. *
  161. * var s = new Schema({ uuid: { type: Buffer, subtype: 4 });
  162. * var M = db.model('M', s);
  163. * var m = new M({ uuid: 'test string' });
  164. * m.uuid._subtype; // 4
  165. *
  166. * @param {Number} subtype the default subtype
  167. * @return {SchemaType} this
  168. * @api public
  169. */
  170. SchemaBuffer.prototype.subtype = function(subtype) {
  171. this.options.subtype = subtype;
  172. return this;
  173. };
  174. /*!
  175. * ignore
  176. */
  177. function handleSingle(val) {
  178. return this.castForQuery(val);
  179. }
  180. SchemaBuffer.prototype.$conditionalHandlers =
  181. utils.options(SchemaType.prototype.$conditionalHandlers, {
  182. $bitsAllClear: handleBitwiseOperator,
  183. $bitsAnyClear: handleBitwiseOperator,
  184. $bitsAllSet: handleBitwiseOperator,
  185. $bitsAnySet: handleBitwiseOperator,
  186. $gt: handleSingle,
  187. $gte: handleSingle,
  188. $lt: handleSingle,
  189. $lte: handleSingle
  190. });
  191. /**
  192. * Casts contents for queries.
  193. *
  194. * @param {String} $conditional
  195. * @param {any} [value]
  196. * @api private
  197. */
  198. SchemaBuffer.prototype.castForQuery = function($conditional, val) {
  199. let handler;
  200. if (arguments.length === 2) {
  201. handler = this.$conditionalHandlers[$conditional];
  202. if (!handler) {
  203. throw new Error('Can\'t use ' + $conditional + ' with Buffer.');
  204. }
  205. return handler.call(this, val);
  206. }
  207. val = $conditional;
  208. const casted = this._castForQuery(val);
  209. return casted ? casted.toObject({ transform: false, virtuals: false }) : casted;
  210. };
  211. /*!
  212. * Module exports.
  213. */
  214. module.exports = SchemaBuffer;