forked from cult-of-coders/grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bero
committed
Aug 9, 2024
1 parent
06aa64c
commit c79edca
Showing
6 changed files
with
145 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { LocalCollection } from 'meteor/minimongo'; | ||
|
||
const Connection = Meteor.connection.constructor; | ||
|
||
const originalSubscribe = Connection.prototype.subscribe; | ||
Connection.prototype.subscribe = function (...args) { | ||
const handle = originalSubscribe.apply(this, args); | ||
|
||
handle.scopeQuery = function () { | ||
const query = {}; | ||
query[`_sub_${handle.subscriptionId}`] = { | ||
$exists: true, | ||
}; | ||
return query; | ||
}; | ||
|
||
return handle; | ||
}; | ||
|
||
// Recreate the convenience method. | ||
Meteor.subscribe = Meteor.connection.subscribe.bind(Meteor.connection); | ||
|
||
const originalCompileProjection = LocalCollection._compileProjection; | ||
LocalCollection._compileProjection = function (fields) { | ||
const fun = originalCompileProjection(fields); | ||
|
||
return function (obj) { | ||
const res = fun(obj); | ||
|
||
for (const field of Object.keys(res)) { | ||
if (field.lastIndexOf('_sub_', 0) === 0) { | ||
delete res[field]; | ||
} | ||
} | ||
|
||
return res; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
export const extendPublish = (newPublishArguments) => { | ||
// DDP Server constructor. | ||
const Server = Object.getPrototypeOf(Meteor.server).constructor; | ||
|
||
const originalPublish = Server.prototype.publish; | ||
Server.prototype.publish = async function (...args) { | ||
// If the first argument is an object, we let the original publish function to traverse it. | ||
if (typeof args[0] === 'object' && args[0] !== null) { | ||
await originalPublish.apply(this, args); | ||
return; | ||
} | ||
|
||
const newArgs = await newPublishArguments.apply(this, args); | ||
|
||
await originalPublish.apply(this, newArgs); | ||
}; | ||
|
||
// Because Meteor.publish is a bound function it remembers old | ||
// prototype method so we have to wrap it directly as well. | ||
const originalMeteorPublish = Meteor.publish; | ||
Meteor.publish = function (...args) { | ||
// If the first argument is an object, we let the original publish function to traverse it. | ||
if (typeof args[0] === 'object' && args[0] !== null) { | ||
originalMeteorPublish.apply(this, args); | ||
return; | ||
} | ||
|
||
const newArgs = newPublishArguments.apply(this, args); | ||
|
||
originalMeteorPublish.apply(this, newArgs); | ||
}; | ||
}; | ||
|
||
// Extend publish part | ||
|
||
extendPublish(function (name, func, options) { | ||
const newFunc = function (...args) { | ||
const publish = this; | ||
|
||
const scopeFieldName = `_sub_${publish._subscriptionId}`; | ||
|
||
let enabled = false; | ||
publish.enableScope = function () { | ||
enabled = true; | ||
}; | ||
|
||
const originalAdded = publish.added; | ||
publish.added = function (collectionName, id, fields) { | ||
// Add our scoping field. | ||
if (enabled) { | ||
fields = { ...fields }; | ||
fields[scopeFieldName] = 1; | ||
} | ||
|
||
originalAdded.call(this, collectionName, id, fields); | ||
}; | ||
|
||
const originalChanged = publish.changed; | ||
publish.changed = function (collectionName, id, fields) { | ||
// We do not allow changes to our scoping field. | ||
if (enabled && scopeFieldName in fields) { | ||
fields = { ...fields }; | ||
delete fields[scopeFieldName]; | ||
} | ||
|
||
originalChanged.call(this, collectionName, id, fields); | ||
}; | ||
|
||
func.apply(publish, args); | ||
}; | ||
|
||
return [name, newFunc, options]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,9 @@ Package.onUse(function (api) { | |
|
||
api.versionsFrom(['3.0-rc.2']); | ||
|
||
api.addFiles('lib/scoping/client.js', 'client'); | ||
api.addFiles('lib/scoping/server.js', 'server'); | ||
|
||
var packages = [ | ||
'ecmascript', | ||
'underscore', | ||
|
@@ -59,6 +62,9 @@ Package.onUse(function (api) { | |
Package.onTest(function (api) { | ||
api.use('cultofcoders:grapher'); | ||
|
||
// api.addFiles('lib/scoping/client.js', 'client'); | ||
// api.addFiles('lib/scoping/server.js', 'server'); | ||
|
||
Npm.depends({ | ||
...npmPackages, | ||
chai: '4.3.4', | ||
|
@@ -71,6 +77,7 @@ Package.onTest(function (api) { | |
'matb33:[email protected]', | ||
'reywood:[email protected]', | ||
'dburles:[email protected]', | ||
// 'peerlibrary:[email protected]', | ||
// 'herteby:[email protected]', | ||
'mongo', | ||
]; | ||
|