Skip to content

Commit

Permalink
Merge pull request #449 from Meteor-Community-Packages/fix-bypasscoll…
Browse files Browse the repository at this point in the history
…ection2

Make compatible with the new RC
  • Loading branch information
jankapunkt authored Jun 14, 2024
2 parents a0a3944 + 3a12b53 commit 282c242
Show file tree
Hide file tree
Showing 10 changed files with 547 additions and 641 deletions.
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*

- [4.0.0-beta.5](#400-beta.3)
- [4.0.2](#402)
- [4.0.1](#401)
- [4.0.0](#400)
- [3.5.0](#350)
- [3.4.1](#341)
- [3.4.0](#340)
Expand Down Expand Up @@ -78,6 +80,16 @@

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## 4.0.2

- Make collection2 compatible with the newly released RC
- Move common code between `_methodMutation` and `_methodMutationAsync` into its own code

## 4.0.1

- Fix dynamic import https://github.com/Meteor-Community-Packages/meteor-collection2/pull/450


## 4.0.0

- Make collection2 compatible with Meteor 3.0 thanks to the [awesome work](https://github.com/Meteor-Community-Packages/meteor-collection2/pull/443) by @klablink
Expand Down
29 changes: 15 additions & 14 deletions package/collection2/.versions
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
aldeed:collection2@4.0.1
aldeed:simple-schema@1.13.1
aldeed:collection2@4.0.2
aldeed:simple-schema@2.0.0-rc.1
allow-deny@1.1.1
babel-compiler@7.10.5
babel-runtime@1.5.1
base64@1.0.12
binary-heap@1.0.11
boilerplate-generator@1.7.2
callback-hook@1.5.1
check@1.3.2
check@1.4.1
ddp@1.4.1
ddp-client@2.6.1
ddp-common@1.4.0
ddp-server@2.7.0
ddp-client@2.6.2
ddp-common@1.4.1
ddp-server@2.7.1
diff-sequence@1.1.2
dynamic-import@0.7.3
ecmascript@0.16.8
Expand All @@ -24,17 +24,18 @@ geojson-utils@1.0.11
http@1.0.10
id-map@1.1.1
inter-process-messaging@0.1.1
local-test:aldeed:collection2@4.0.1
logging@1.3.3
local-test:aldeed:collection2@4.0.2
logging@1.3.4
mdg:validation-error@0.5.1
meteor@1.11.5
meteortesting:browser-tests@1.6.0-beta300.0
meteortesting:mocha@3.1.0-beta300.0
meteortesting:mocha-core@8.3.1-beta300.0
minimongo@1.9.3
meteortesting:browser-tests@1.6.0
meteortesting:mocha@3.1.0-rc.1
meteortesting:mocha-core@8.3.1-rc300.1
minimongo@1.9.4
modern-browsers@0.1.10
modules@0.20.0
modules-runtime@0.13.1
mongo@1.16.8
mongo@1.16.10
mongo-decimal@0.1.3
mongo-dev-server@1.1.0
mongo-id@1.0.8
Expand All @@ -50,7 +51,7 @@ routepolicy@1.1.1
socket-stream-client@0.5.2
tracker@1.3.3
typescript@4.9.5
underscore@1.6.0
underscore@1.6.1
url@1.3.2
webapp@1.13.8
webapp-hashing@1.1.1
186 changes: 79 additions & 107 deletions package/collection2/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,121 +173,93 @@ Mongo.Collection.prototype.attachSchema = function c2AttachSchema(ss, options) {
return null;
};
});

function getArgumentsAndValidationContext(methodName, args, async) {
let options = isInsertType(methodName) ? args[1] : args[2];

// Support missing options arg
if (!options || typeof options === 'function') {
options = {};
}

let validationContext = {};
if (this._c2 && options.bypassCollection2 !== true) {
let userId = null;
try {
// https://github.com/aldeed/meteor-collection2/issues/175
userId = Meteor.userId();
} catch (err) {}

[args, validationContext] = doValidate(
this,
methodName,
args,
Meteor.isServer || this._connection === null, // getAutoValues
userId,
Meteor.isServer, // isFromTrustedCode
async
);

if (!args) {
// doValidate already called the callback or threw the error, so we're done.
// But insert should always return an ID to match core behavior.
return isInsertType(methodName) ? this._makeNewID() : undefined;
}
} else {
// We still need to adjust args because insert does not take options
if (isInsertType(methodName) && typeof args[1] !== 'function') args.splice(1, 1);
}

return [args, validationContext];
}

function _methodMutation(async, methodName) {
function _methodMutation(async, methodName) {
const _super = Meteor.isFibersDisabled
? Mongo.Collection.prototype[methodName]
: Mongo.Collection.prototype[methodName.replace('Async', '')];

? Mongo.Collection.prototype[methodName]
: Mongo.Collection.prototype[methodName.replace('Async', '')];
if (!_super) return;

Mongo.Collection.prototype[methodName] = function (...args) {
let options = isInsertType(methodName) ? args[1] : args[2];

// Support missing options arg
if (!options || typeof options === 'function') {
options = {};
}

let validationContext = {};
let error;
if (this._c2 && options.bypassCollection2 !== true) {
let userId = null;
try {
// https://github.com/aldeed/meteor-collection2/issues/175
userId = Meteor.userId();
} catch (err) {}

[args, validationContext] = doValidate(
this,
methodName,
args,
Meteor.isServer || this._connection === null, // getAutoValues
userId,
Meteor.isServer, // isFromTrustedCode
async
);

if (!args) {
// doValidate already called the callback or threw the error, so we're done.
// But insert should always return an ID to match core behavior.
return isInsertType(methodName) ? this._makeNewID() : undefined;
}
} else {
// We still need to adjust args because insert does not take options
if (isInsertType(methodName) && typeof args[1] !== 'function') args.splice(1, 1);
}

if (async && !Meteor.isFibersDisabled) {
try {
this[methodName.replace('Async', '')].isCalledFromAsync = true;
_super.isCalledFromAsync = true;
return Promise.resolve(_super.apply(this, args));
} catch (err) {
const addValidationErrorsPropName =
typeof validationContext.addValidationErrors === 'function'
? 'addValidationErrors'
: 'addInvalidKeys';
parsingServerError([err], validationContext, addValidationErrorsPropName);
error = getErrorObject(validationContext, err.message, err.code);
return Promise.reject(error);
}
} else {
return _super.apply(this, args);
}
[args, validationContext] = getArgumentsAndValidationContext.call(this, methodName, args, async);

if (async && !Meteor.isFibersDisabled) {
try {
this[methodName.replace('Async', '')].isCalledFromAsync = true;
_super.isCalledFromAsync = true;
return Promise.resolve(_super.apply(this, args));
} catch (err) {
const addValidationErrorsPropName =
typeof validationContext.addValidationErrors === 'function'
? 'addValidationErrors'
: 'addInvalidKeys';
parsingServerError([err], validationContext, addValidationErrorsPropName);
const error = getErrorObject(validationContext, err.message, err.code);
return Promise.reject(error);
}
} else {
return _super.apply(this, args);
}
};
}

function _methodMutationAsync(methodName) {
}
function _methodMutationAsync(methodName) {
const _super = Mongo.Collection.prototype[methodName];
Mongo.Collection.prototype[methodName] = async function (...args) {
let options = isInsertType(methodName) ? args[1] : args[2];

// Support missing options arg
if (!options || typeof options === 'function') {
options = {};
}

let validationContext = {};
if (this._c2 && options.bypassCollection2 !== true) {
let userId = null;
try {
// https://github.com/aldeed/meteor-collection2/issues/175
userId = Meteor.userId();
} catch (err) {}

[args, validationContext] = doValidate(
this,
methodName,
args,
Meteor.isServer || this._connection === null, // getAutoValues
userId,
Meteor.isServer, // isFromTrustedCode
true
);

if (!args) {
// doValidate already called the callback or threw the error, so we're done.
// But insert should always return an ID to match core behavior.
return isInsertType(methodName) ? this._makeNewID() : undefined;
}
} else {
// We still need to adjust args because insert does not take options
if (methodName === 'insert' && typeof args[1] !== 'function') args.splice(1, 1);
}

try {
return await _super.apply(this, args);
} catch (err) {
const addValidationErrorsPropName =
typeof validationContext.addValidationErrors === 'function'
? 'addValidationErrors'
: 'addInvalidKeys';
parsingServerError([err], validationContext, addValidationErrorsPropName);
throw getErrorObject(validationContext, err.message, err.code);
}
[args, validationContext] = getArgumentsAndValidationContext.call(this, methodName, args, true);

try {
return await _super.apply(this, args);
} catch (err) {
const addValidationErrorsPropName =
typeof validationContext.addValidationErrors === 'function'
? 'addValidationErrors'
: 'addInvalidKeys';
parsingServerError([err], validationContext, addValidationErrorsPropName);
throw getErrorObject(validationContext, err.message, err.code);
}
};
}
}


// Wrap DB write operation methods
if (Mongo.Collection.prototype.insertAsync) {
Expand Down
17 changes: 9 additions & 8 deletions package/collection2/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Package.describe({
name: 'aldeed:collection2',
summary:
'Automatic validation of Meteor Mongo insert and update operations on the client and server',
version: '4.0.1',
version: '4.0.2',
documentation: '../../README.md',
git: 'https://github.com/aldeed/meteor-collection2.git'
});
Expand All @@ -16,14 +16,14 @@ Npm.depends({
});

Package.onUse(function (api) {
api.versionsFrom(['1.12.1', '2.3', '3.0-beta.0']);
api.versionsFrom(['1.12.1', '2.3', '3.0-rc.4']);
api.use('mongo');
api.imply('mongo');
api.use('minimongo');
api.use('ejson');
api.use('raix:eventemitter@1.0.0');
api.use('ecmascript');
api.use('aldeed:simple-schema@2.0.0-beta300.0 || 1.13.1');
api.use('raix:eventemitter@1.0.0');
api.use('aldeed:simple-schema@1.13.1 || 2.0.0-beta300.0');

api.addFiles(['./collection2.js']);

Expand All @@ -34,8 +34,9 @@ Package.onUse(function (api) {
});

Package.onTest(function (api) {
api.versionsFrom(['1.12.1', '2.3', '3.0-rc.4']);
api.use([
'meteortesting:mocha@3.1.0-beta300.0',
'aldeed:collection2@4.0.1'
])
});
'meteortesting:mocha@3.1.0-rc.1',
'aldeed:collection2@4.0.2'
]);
});
30 changes: 15 additions & 15 deletions tests/.meteor/packages
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
# 'meteor add' and 'meteor remove' will edit this file for you,
# but you can also edit it by hand.

meteor-base@1.5.2-beta300.0 # Packages every Meteor app needs to have
mongo@2.0.0-beta300.0 # The database Meteor supports right now
reactive-var@1.0.13-beta300.0 # Reactive variable for tracker
meteor-base@1.5.2-rc300.4 # Packages every Meteor app needs to have
mongo@2.0.0-rc300.4 # The database Meteor supports right now
reactive-var@1.0.13-rc300.4 # Reactive variable for tracker
jquery # Helpful client-side library
tracker@1.3.3-beta300.0 # Meteor's client-side reactive programming library
tracker@1.3.4-rc300.4 # Meteor's client-side reactive programming library

standard-minifier-css@1.9.3-beta300.0 # CSS minifier run for production mode
standard-minifier-js@3.0.0-beta300.0 # JS minifier run for production mode
es5-shim@4.8.1-beta300.0 # ECMAScript 5 compatibility for older browsers.
ecmascript@0.16.8-beta300.0 # Enable ECMAScript2015+ syntax in app code
shell-server@0.6.0-beta300.0 # Server-side component of the `meteor shell` command
standard-minifier-css@1.9.3-rc300.4 # CSS minifier run for production mode
standard-minifier-js@3.0.0-rc300.4 # JS minifier run for production mode
es5-shim@4.8.1-rc300.4 # ECMAScript 5 compatibility for older browsers.
ecmascript@0.16.9-rc300.4 # Enable ECMAScript2015+ syntax in app code
shell-server@0.6.0-rc300.4 # Server-side component of the `meteor shell` command

autopublish@1.0.8-beta300.0 # Publish all data to the clients (for prototyping)
insecure@1.0.8-beta300.0 # Allow all DB writes from clients (for prototyping)
autopublish@1.0.8-rc300.4 # Publish all data to the clients (for prototyping)
insecure@1.0.8-rc300.4 # Allow all DB writes from clients (for prototyping)

underscore@1.0.14-beta300.0
dynamic-import@0.7.4-beta300.0
underscore@1.6.2-rc300.4
dynamic-import@0.7.4-rc300.4

aldeed:simple-schema
aldeed:collection2@4.0.0
aldeed:simple-schema@2.0.0-beta300.0
aldeed:collection2@4.0.2-beta.2
meteortesting:mocha@3.1.0-beta300.0
meteortesting:mocha-core@8.3.0-beta300.0
2 changes: 1 addition & 1 deletion tests/.meteor/release
Original file line number Diff line number Diff line change
@@ -1 +1 @@
METEOR@3.0-beta.0
METEOR@3.0-rc.4
Loading

0 comments on commit 282c242

Please sign in to comment.