@@ -173,121 +173,93 @@ Mongo.Collection.prototype.attachSchema = function c2AttachSchema(ss, options) {
173173 return null ;
174174 } ;
175175 } ) ;
176+
177+ function getArgumentsAndValidationContext ( methodName , args , async ) {
178+ let options = isInsertType ( methodName ) ? args [ 1 ] : args [ 2 ] ;
179+
180+ // Support missing options arg
181+ if ( ! options || typeof options === 'function' ) {
182+ options = { } ;
183+ }
184+
185+ let validationContext = { } ;
186+ if ( this . _c2 && options . bypassCollection2 !== true ) {
187+ let userId = null ;
188+ try {
189+ // https://github.com/aldeed/meteor-collection2/issues/175
190+ userId = Meteor . userId ( ) ;
191+ } catch ( err ) { }
192+
193+ [ args , validationContext ] = doValidate (
194+ this ,
195+ methodName ,
196+ args ,
197+ Meteor . isServer || this . _connection === null , // getAutoValues
198+ userId ,
199+ Meteor . isServer , // isFromTrustedCode
200+ async
201+ ) ;
202+
203+ if ( ! args ) {
204+ // doValidate already called the callback or threw the error, so we're done.
205+ // But insert should always return an ID to match core behavior.
206+ return isInsertType ( methodName ) ? this . _makeNewID ( ) : undefined ;
207+ }
208+ } else {
209+ // We still need to adjust args because insert does not take options
210+ if ( isInsertType ( methodName ) && typeof args [ 1 ] !== 'function' ) args . splice ( 1 , 1 ) ;
211+ }
212+
213+ return [ args , validationContext ] ;
214+ }
176215
177- function _methodMutation ( async , methodName ) {
216+ function _methodMutation ( async , methodName ) {
178217 const _super = Meteor . isFibersDisabled
179- ? Mongo . Collection . prototype [ methodName ]
180- : Mongo . Collection . prototype [ methodName . replace ( 'Async' , '' ) ] ;
181-
218+ ? Mongo . Collection . prototype [ methodName ]
219+ : Mongo . Collection . prototype [ methodName . replace ( 'Async' , '' ) ] ;
220+
182221 if ( ! _super ) return ;
183-
184222 Mongo . Collection . prototype [ methodName ] = function ( ...args ) {
185- let options = isInsertType ( methodName ) ? args [ 1 ] : args [ 2 ] ;
186-
187- // Support missing options arg
188- if ( ! options || typeof options === 'function' ) {
189- options = { } ;
190- }
191-
192- let validationContext = { } ;
193- let error ;
194- if ( this . _c2 && options . bypassCollection2 !== true ) {
195- let userId = null ;
196- try {
197- // https://github.com/aldeed/meteor-collection2/issues/175
198- userId = Meteor . userId ( ) ;
199- } catch ( err ) { }
200-
201- [ args , validationContext ] = doValidate (
202- this ,
203- methodName ,
204- args ,
205- Meteor . isServer || this . _connection === null , // getAutoValues
206- userId ,
207- Meteor . isServer , // isFromTrustedCode
208- async
209- ) ;
210-
211- if ( ! args ) {
212- // doValidate already called the callback or threw the error, so we're done.
213- // But insert should always return an ID to match core behavior.
214- return isInsertType ( methodName ) ? this . _makeNewID ( ) : undefined ;
215- }
216- } else {
217- // We still need to adjust args because insert does not take options
218- if ( isInsertType ( methodName ) && typeof args [ 1 ] !== 'function' ) args . splice ( 1 , 1 ) ;
219- }
220-
221- if ( async && ! Meteor . isFibersDisabled ) {
222- try {
223- this [ methodName . replace ( 'Async' , '' ) ] . isCalledFromAsync = true ;
224- _super . isCalledFromAsync = true ;
225- return Promise . resolve ( _super . apply ( this , args ) ) ;
226- } catch ( err ) {
227- const addValidationErrorsPropName =
228- typeof validationContext . addValidationErrors === 'function'
229- ? 'addValidationErrors'
230- : 'addInvalidKeys' ;
231- parsingServerError ( [ err ] , validationContext , addValidationErrorsPropName ) ;
232- error = getErrorObject ( validationContext , err . message , err . code ) ;
233- return Promise . reject ( error ) ;
234- }
235- } else {
236- return _super . apply ( this , args ) ;
237- }
223+ [ args , validationContext ] = getArgumentsAndValidationContext . call ( this , methodName , args , async ) ;
224+
225+ if ( async && ! Meteor . isFibersDisabled ) {
226+ try {
227+ this [ methodName . replace ( 'Async' , '' ) ] . isCalledFromAsync = true ;
228+ _super . isCalledFromAsync = true ;
229+ return Promise . resolve ( _super . apply ( this , args ) ) ;
230+ } catch ( err ) {
231+ const addValidationErrorsPropName =
232+ typeof validationContext . addValidationErrors === 'function'
233+ ? 'addValidationErrors'
234+ : 'addInvalidKeys' ;
235+ parsingServerError ( [ err ] , validationContext , addValidationErrorsPropName ) ;
236+ const error = getErrorObject ( validationContext , err . message , err . code ) ;
237+ return Promise . reject ( error ) ;
238+ }
239+ } else {
240+ return _super . apply ( this , args ) ;
241+ }
238242 } ;
239- }
240-
241- function _methodMutationAsync ( methodName ) {
243+ }
244+
245+ function _methodMutationAsync ( methodName ) {
242246 const _super = Mongo . Collection . prototype [ methodName ] ;
243247 Mongo . Collection . prototype [ methodName ] = async function ( ...args ) {
244- let options = isInsertType ( methodName ) ? args [ 1 ] : args [ 2 ] ;
245-
246- // Support missing options arg
247- if ( ! options || typeof options === 'function' ) {
248- options = { } ;
249- }
250-
251- let validationContext = { } ;
252- if ( this . _c2 && options . bypassCollection2 !== true ) {
253- let userId = null ;
254- try {
255- // https://github.com/aldeed/meteor-collection2/issues/175
256- userId = Meteor . userId ( ) ;
257- } catch ( err ) { }
258-
259- [ args , validationContext ] = doValidate (
260- this ,
261- methodName ,
262- args ,
263- Meteor . isServer || this . _connection === null , // getAutoValues
264- userId ,
265- Meteor . isServer , // isFromTrustedCode
266- true
267- ) ;
268-
269- if ( ! args ) {
270- // doValidate already called the callback or threw the error, so we're done.
271- // But insert should always return an ID to match core behavior.
272- return isInsertType ( methodName ) ? this . _makeNewID ( ) : undefined ;
273- }
274- } else {
275- // We still need to adjust args because insert does not take options
276- if ( methodName === 'insert' && typeof args [ 1 ] !== 'function' ) args . splice ( 1 , 1 ) ;
277- }
278-
279- try {
280- return await _super . apply ( this , args ) ;
281- } catch ( err ) {
282- const addValidationErrorsPropName =
283- typeof validationContext . addValidationErrors === 'function'
284- ? 'addValidationErrors'
285- : 'addInvalidKeys' ;
286- parsingServerError ( [ err ] , validationContext , addValidationErrorsPropName ) ;
287- throw getErrorObject ( validationContext , err . message , err . code ) ;
288- }
248+ [ args , validationContext ] = getArgumentsAndValidationContext . call ( this , methodName , args , true ) ;
249+
250+ try {
251+ return await _super . apply ( this , args ) ;
252+ } catch ( err ) {
253+ const addValidationErrorsPropName =
254+ typeof validationContext . addValidationErrors === 'function'
255+ ? 'addValidationErrors'
256+ : 'addInvalidKeys' ;
257+ parsingServerError ( [ err ] , validationContext , addValidationErrorsPropName ) ;
258+ throw getErrorObject ( validationContext , err . message , err . code ) ;
259+ }
289260 } ;
290- }
261+ }
262+
291263
292264 // Wrap DB write operation methods
293265 if ( Mongo . Collection . prototype . insertAsync ) {
0 commit comments