├── .gitignore ├── LICENSE ├── README.md ├── lib └── server-session.js ├── package.js ├── smart.json ├── tests └── test-both.js └── versions.json /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/* 2 | .build* 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Matteo De Micheli 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Meteor ServerSession 2 | ===================== 3 | 4 | This package provides a simple serverside implementation of the [Session](http://docs.meteor.com/#session). You can use it for site wide configuration, the server side session does not differ per user. It uses the same API: 5 | 6 | ```javascript 7 | ServerSession.set(key, value); 8 | ServerSession.get(key); 9 | ServerSession.equals(key, expected, identical = true); 10 | ``` 11 | You can also define a condition, which returns true or false (if false it won't set any value) 12 | ```javascript 13 | ServerSession.setCondition(function (key, value)); // Should only be invoked on the server 14 | ``` 15 | 16 | There's no setDefault now, as it doesn't make any sense to me as of now, write a ticket if you think otherwise. 17 | 18 | ```sh 19 | meteor add matteodem:server-session 20 | ``` 21 | 22 | -------------------------------------------------------------------------------- /lib/server-session.js: -------------------------------------------------------------------------------- 1 | /*global Meteor, ServerSession:true, console */ 2 | /*jslint eqeq: true */ 3 | ServerSession = (function () { 4 | 'use strict'; 5 | 6 | var Collection = new Meteor.Collection('serversession'), 7 | getSessionValue = function (obj) { 8 | if (_.isObject(obj)) { 9 | return obj.value; 10 | } 11 | 12 | return obj; 13 | }, 14 | checkForKey = function (key) { 15 | if ("undefined" === typeof key) { 16 | throw new Error('Please provide a key!'); 17 | } 18 | }, 19 | condition = function () { 20 | return true; 21 | }; 22 | 23 | if (Meteor.isServer) { 24 | Meteor.publish('serversession', function () { 25 | return Collection.find(); 26 | }); 27 | 28 | Collection.allow({ 29 | 'insert' : function () { 30 | return false; 31 | }, 32 | 'update' : function () { 33 | return false; 34 | }, 35 | 'remove' : function () { 36 | return false; 37 | } 38 | }); 39 | } 40 | 41 | if (Meteor.isClient) { 42 | Meteor.subscribe('serversession'); 43 | } 44 | 45 | Meteor.methods({ 46 | 'serversession_set' : function (key, value) { 47 | // Check again, since Meteor.methods is encapsulated 48 | checkForKey(key); 49 | 50 | if (!condition(key, value)) { 51 | throw new Meteor.Error('Condition failed (has to be specified on server!)'); 52 | } 53 | 54 | if (!Collection.findOne({ 'key' : key })) { 55 | Collection.insert({ 'key' : key, 'value' : value }); 56 | return; 57 | } 58 | 59 | Collection.update({ 'key' : key }, { $set : { 'value' : value }}); 60 | } 61 | }); 62 | 63 | // Return public API 64 | return { 65 | 'set' : function (key, value) { 66 | checkForKey(key); 67 | Meteor.call('serversession_set', key, value); 68 | }, 69 | 'get' : function (key) { 70 | var sessionObj = Collection.findOne({ 'key' : key }); 71 | return getSessionValue(sessionObj); 72 | }, 73 | 'equals' : function (key, expected, identical) { 74 | var sessionObj = Collection.findOne({ 'key' : key }), 75 | value = getSessionValue(sessionObj); 76 | 77 | if (_.isObject(value) && _.isObject(expected)) { 78 | return _(value).isEqual(expected); 79 | } 80 | 81 | if (false === identical) { 82 | return expected == value; 83 | } 84 | 85 | return expected === value; 86 | }, 87 | 'setCondition' : function (newCondition) { 88 | if (Meteor.isClient) { 89 | throw new Meteor.Error('You have to set conditions on the server, not the client!'); 90 | } 91 | condition = newCondition; 92 | } 93 | }; 94 | }()); -------------------------------------------------------------------------------- /package.js: -------------------------------------------------------------------------------- 1 | Package.describe({ 2 | name: 'matteodem:server-session', 3 | summary : "Serverside Session through a Meteor.Collection (get, set, equals etc.)", 4 | version: "0.4.2", 5 | git: "https://github.com/matteodem/meteor-server-session.git" 6 | }); 7 | 8 | Package.on_use(function (api) { 9 | api.versionsFrom('METEOR@0.9.0'); 10 | 11 | api.use('underscore', 'client'); 12 | api.use('underscore', 'server'); 13 | api.use(['livedata', 'mongo-livedata'], ['client', 'server']); 14 | 15 | api.export('ServerSession'); 16 | 17 | api.add_files([ 18 | 'lib/server-session.js' 19 | ], ['client', 'server'] 20 | ); 21 | }); 22 | 23 | Package.on_test(function (api) { 24 | api.use( 25 | ['matteodem:server-session', 'tinytest', 'test-helpers'] 26 | ); 27 | 28 | api.add_files( 29 | 'tests/test-both.js', 30 | ['client', 'server'] 31 | ); 32 | }); -------------------------------------------------------------------------------- /smart.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "server-session", 3 | "description": "Serverside Session through a Meteor.Collection (get, set, equals etc.)", 4 | "homepage": "https://github.com/matteodem/meteor-server-session", 5 | "author": "Matteo De Micheli", 6 | "version": "0.4.1", 7 | "git": "https://github.com/matteodem/meteor-server-session.git" 8 | } -------------------------------------------------------------------------------- /tests/test-both.js: -------------------------------------------------------------------------------- 1 | Tinytest.add('ServerSession - set', function (test) { 2 | if (Meteor.isServer) { 3 | test.isUndefined(ServerSession.setCondition(function () { 4 | return true; 5 | })); 6 | } 7 | 8 | test.throws(function () { 9 | ServerSession.set(); 10 | }, Error, 'Should throw an error if no key and value are provided when using set' 11 | ); 12 | 13 | test.isUndefined(ServerSession.set('testKey', 'testValue')); 14 | test.equal('testValue', ServerSession.get('testKey')); 15 | ServerSession.set('testKey'); 16 | test.isTrue(undefined == ServerSession.get('testKey') || null == ServerSession.get('testKey')); 17 | }); 18 | 19 | Tinytest.add('ServerSession - get', function (test) { 20 | ServerSession.set('null', null); 21 | ServerSession.set('aBool', true); 22 | ServerSession.set('anInteger', 12345); 23 | ServerSession.set('aString', 'meIzAString'); 24 | ServerSession.set('anArray', [1, 2, 3, ['4', '5', '6', [12, 23, 34]]]); 25 | ServerSession.set('anObject', { 'keyOfObject' : 'value', 'keyOfObj' : {'a' : ['b', 'c'] } }); 26 | 27 | test.equal(null, ServerSession.get('null')); 28 | test.equal(true, ServerSession.get('aBool')); 29 | test.equal(12345, ServerSession.get('anInteger')); 30 | test.equal('meIzAString', ServerSession.get('aString')); 31 | test.equal([1, 2, 3, ['4', '5', '6', [12, 23, 34]]], ServerSession.get('anArray')); 32 | test.equal({ 'keyOfObject' : 'value', 'keyOfObj' : {'a' : ['b', 'c'] } }, ServerSession.get('anObject')); 33 | }); 34 | 35 | Tinytest.add('ServerSession - equals (identical)', function (test) { 36 | ServerSession.set('null', null); 37 | ServerSession.set('true', true); 38 | ServerSession.set('54321', 54321); 39 | ServerSession.set('aRandomString', 'aRandomString1'); 40 | ServerSession.set('array', [1, 2, 3, ['4', '5', '6', [11, 12, 31]]]); 41 | ServerSession.set('object', { 'keyOfObject' : '_value'}); 42 | ServerSession.set('emptyObj', {}); 43 | ServerSession.set('emptyArray', []); 44 | 45 | test.isTrue(ServerSession.equals('null', null)); 46 | test.isTrue(ServerSession.equals('true', true)); 47 | test.isTrue(ServerSession.equals('54321', 54321)); 48 | test.isTrue(ServerSession.equals('emptyObj', {})); 49 | test.isTrue(ServerSession.equals('emptyArray', [])); 50 | test.isTrue(ServerSession.equals('array', [1, 2, 3, ['4', '5', '6', [11, 12, 31]]])); 51 | 52 | test.isFalse(ServerSession.equals('emptyObj', [])); 53 | test.isFalse(ServerSession.equals('emptyArray', {})); 54 | test.isFalse(ServerSession.equals('aRandomString', 'aRandomString')); 55 | test.isFalse(ServerSession.equals('array', [1, 2, 3, ['4', '5', '6', [11, 12, 30]]])); 56 | test.isFalse(ServerSession.equals('object', { 'keyOfObject' : 'value'})); 57 | }); 58 | 59 | Tinytest.add('ServerSession - equals (not identical)', function (test) { 60 | ServerSession.set('54321', 1); 61 | ServerSession.set('null', null); 62 | ServerSession.set('true', true); 63 | ServerSession.set('aStringString', 'stringity'); 64 | 65 | test.isTrue(ServerSession.equals('null', undefined, false)); 66 | test.isTrue(ServerSession.equals('true', !null, false)); 67 | test.isTrue(ServerSession.equals('54321', true, false)); 68 | test.isFalse(ServerSession.equals('aStringString', 'stringity11', false)); 69 | }); 70 | 71 | if (Meteor.isServer) { 72 | Tinytest.add('ServerSession - Conditions', function (test) { 73 | test.isUndefined(ServerSession.setCondition(function () { 74 | return false; 75 | })); 76 | 77 | test.throws(function () { 78 | ServerSession.set('theKey', { foo : 'bar' }); 79 | }, Meteor.Error); 80 | 81 | test.isFalse(ServerSession.equals('theKey', { foo : 'bar' })); 82 | 83 | // Also test with return true 84 | test.isUndefined(ServerSession.setCondition(function () { 85 | return true; 86 | })); 87 | 88 | ServerSession.set('newKey', { foo : 'bar' }); 89 | test.equal('bar', ServerSession.get('newKey').foo); 90 | ServerSession.set('newKey', undefined); 91 | }); 92 | } -------------------------------------------------------------------------------- /versions.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": [ 3 | [ 4 | "application-configuration", 5 | "1.0.0" 6 | ], 7 | [ 8 | "binary-heap", 9 | "1.0.0" 10 | ], 11 | [ 12 | "callback-hook", 13 | "1.0.0" 14 | ], 15 | [ 16 | "check", 17 | "1.0.0" 18 | ], 19 | [ 20 | "deps", 21 | "1.0.0" 22 | ], 23 | [ 24 | "ejson", 25 | "1.0.0" 26 | ], 27 | [ 28 | "follower-livedata", 29 | "1.0.0" 30 | ], 31 | [ 32 | "geojson-utils", 33 | "1.0.0" 34 | ], 35 | [ 36 | "id-map", 37 | "1.0.0" 38 | ], 39 | [ 40 | "json", 41 | "1.0.0" 42 | ], 43 | [ 44 | "livedata", 45 | "1.0.7" 46 | ], 47 | [ 48 | "logging", 49 | "1.0.0" 50 | ], 51 | [ 52 | "meteor", 53 | "1.0.2" 54 | ], 55 | [ 56 | "minimongo", 57 | "1.0.0" 58 | ], 59 | [ 60 | "mongo-livedata", 61 | "1.0.3" 62 | ], 63 | [ 64 | "ordered-dict", 65 | "1.0.0" 66 | ], 67 | [ 68 | "random", 69 | "1.0.0" 70 | ], 71 | [ 72 | "retry", 73 | "1.0.0" 74 | ], 75 | [ 76 | "underscore", 77 | "1.0.0" 78 | ] 79 | ], 80 | "pluginDependencies": [], 81 | "toolVersion": "meteor-tool@1.0.26", 82 | "format": "1.0" 83 | } --------------------------------------------------------------------------------