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