1 |
|
2 |
|
3 |
|
4 |
|
5 | 'use strict';
|
6 |
|
7 | const Binary = require('../driver').get().Binary;
|
8 | const utils = require('../utils');
|
9 | const Buffer = require('safe-buffer').Buffer;
|
10 |
|
11 |
|
12 | const proto = Buffer.from('').constructor.prototype;
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function MongooseBuffer(value, encode, offset) {
|
28 | const length = arguments.length;
|
29 | let val;
|
30 |
|
31 | if (length === 0 || arguments[0] === null || arguments[0] === undefined) {
|
32 | val = 0;
|
33 | } else {
|
34 | val = value;
|
35 | }
|
36 |
|
37 | let encoding;
|
38 | let path;
|
39 | let doc;
|
40 |
|
41 | if (Array.isArray(encode)) {
|
42 |
|
43 | path = encode[0];
|
44 | doc = encode[1];
|
45 | } else {
|
46 | encoding = encode;
|
47 | }
|
48 |
|
49 | let buf;
|
50 | if (typeof val === 'number' || val instanceof Number) {
|
51 | buf = Buffer.alloc(val);
|
52 | } else {
|
53 | buf = Buffer.from(val, encoding, offset);
|
54 | }
|
55 | utils.decorate(buf, MongooseBuffer.mixin);
|
56 | buf.isMongooseBuffer = true;
|
57 |
|
58 |
|
59 | buf[MongooseBuffer.pathSymbol] = path;
|
60 | buf[parentSymbol] = doc;
|
61 |
|
62 | buf._subtype = 0;
|
63 | return buf;
|
64 | }
|
65 |
|
66 | const pathSymbol = Symbol.for('mongoose#Buffer#_path');
|
67 | const parentSymbol = Symbol.for('mongoose#Buffer#_parent');
|
68 | MongooseBuffer.pathSymbol = pathSymbol;
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | MongooseBuffer.mixin = {
|
75 |
|
76 | |
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | _subtype: undefined,
|
85 |
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | _markModified: function() {
|
95 | const parent = this[parentSymbol];
|
96 |
|
97 | if (parent) {
|
98 | parent.markModified(this[MongooseBuffer.pathSymbol]);
|
99 | }
|
100 | return this;
|
101 | },
|
102 |
|
103 | |
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | write: function() {
|
112 | const written = proto.write.apply(this, arguments);
|
113 |
|
114 | if (written > 0) {
|
115 | this._markModified();
|
116 | }
|
117 |
|
118 | return written;
|
119 | },
|
120 |
|
121 | |
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | copy: function(target) {
|
135 | const ret = proto.copy.apply(this, arguments);
|
136 |
|
137 | if (target && target.isMongooseBuffer) {
|
138 | target._markModified();
|
139 | }
|
140 |
|
141 | return ret;
|
142 | }
|
143 | };
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | (
|
150 |
|
151 | ('writeUInt8 writeUInt16 writeUInt32 writeInt8 writeInt16 writeInt32 ' +
|
152 | 'writeFloat writeDouble fill ' +
|
153 | 'utf8Write binaryWrite asciiWrite set ' +
|
154 |
|
155 |
|
156 | 'writeUInt16LE writeUInt16BE writeUInt32LE writeUInt32BE ' +
|
157 | 'writeInt16LE writeInt16BE writeInt32LE writeInt32BE ' + 'writeFloatLE writeFloatBE writeDoubleLE writeDoubleBE')
|
158 | ).split(' ').forEach(function(method) {
|
159 | if (!proto[method]) {
|
160 | return;
|
161 | }
|
162 | MongooseBuffer.mixin[method] = function() {
|
163 | const ret = proto[method].apply(this, arguments);
|
164 | this._markModified();
|
165 | return ret;
|
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 | MongooseBuffer.mixin.toObject = function(options) {
|
193 | const subtype = typeof options === 'number'
|
194 | ? options
|
195 | : (this._subtype || 0);
|
196 | return new Binary(Buffer.from(this), subtype);
|
197 | };
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | MongooseBuffer.mixin.toBSON = function() {
|
209 | return new Binary(this, this._subtype || 0);
|
210 | };
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 | MongooseBuffer.mixin.equals = function(other) {
|
222 | if (!Buffer.isBuffer(other)) {
|
223 | return false;
|
224 | }
|
225 |
|
226 | if (this.length !== other.length) {
|
227 | return false;
|
228 | }
|
229 |
|
230 | for (let i = 0; i < this.length; ++i) {
|
231 | if (this[i] !== other[i]) {
|
232 | return false;
|
233 | }
|
234 | }
|
235 |
|
236 | return true;
|
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 | MongooseBuffer.mixin.subtype = function(subtype) {
|
262 | if (typeof subtype !== 'number') {
|
263 | throw new TypeError('Invalid subtype. Expected a number');
|
264 | }
|
265 |
|
266 | if (this._subtype !== subtype) {
|
267 | this._markModified();
|
268 | }
|
269 |
|
270 | this._subtype = subtype;
|
271 | };
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 | MongooseBuffer.Binary = Binary;
|
278 |
|
279 | module.exports = MongooseBuffer;
|