sockethub 4.0.1 → 5.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/README.md +3 -3
  2. package/bin/sockethub +23 -19
  3. package/dist/bootstrap/init.js +14 -4
  4. package/dist/bootstrap/init.js.map +1 -1
  5. package/dist/bootstrap/platforms.js +81 -69
  6. package/dist/common.js +10 -12
  7. package/dist/common.js.map +1 -1
  8. package/dist/config.js +4 -22
  9. package/dist/config.js.map +1 -1
  10. package/dist/crypto.js +7 -8
  11. package/dist/crypto.js.map +1 -1
  12. package/dist/janitor.js +14 -9
  13. package/dist/janitor.js.map +1 -1
  14. package/dist/middleware/create-activity-object.js +19 -0
  15. package/dist/middleware/create-activity-object.js.map +1 -0
  16. package/dist/middleware/expand-activity-stream.js +33 -0
  17. package/dist/middleware/expand-activity-stream.js.map +1 -0
  18. package/dist/middleware/expand-activity-stream.test.data.js +360 -0
  19. package/dist/middleware/expand-activity-stream.test.data.js.map +1 -0
  20. package/dist/middleware/store-credentials.js +19 -0
  21. package/dist/middleware/store-credentials.js.map +1 -0
  22. package/dist/middleware/validate.js +77 -0
  23. package/dist/middleware/validate.js.map +1 -0
  24. package/dist/middleware/validate.test.data.js +321 -0
  25. package/dist/middleware/validate.test.data.js.map +1 -0
  26. package/dist/middleware.js +47 -49
  27. package/dist/middleware.js.map +1 -1
  28. package/dist/platform-instance.js +84 -66
  29. package/dist/platform-instance.js.map +1 -1
  30. package/dist/platform.js +50 -25
  31. package/dist/platform.js.map +1 -1
  32. package/dist/process-manager.js +7 -4
  33. package/dist/process-manager.js.map +1 -1
  34. package/dist/routes.js +8 -6
  35. package/dist/routes.js.map +1 -1
  36. package/dist/serve.js +3 -3
  37. package/dist/serve.js.map +1 -1
  38. package/dist/sockethub-client.js +2604 -0
  39. package/dist/sockethub-client.js.map +1 -0
  40. package/dist/sockethub-client.min.js +2 -0
  41. package/dist/sockethub-client.min.js.LICENSE.txt +24 -0
  42. package/dist/sockethub.js +75 -58
  43. package/dist/sockethub.js.map +1 -1
  44. package/dist/store.js +17 -0
  45. package/dist/store.js.map +1 -0
  46. package/package.json +48 -44
  47. package/src/bootstrap/init.ts +16 -2
  48. package/src/bootstrap/platforms.js +14 -18
  49. package/src/common.test.ts +44 -33
  50. package/src/common.ts +9 -17
  51. package/src/config.test.ts +16 -38
  52. package/src/config.ts +1 -23
  53. package/src/crypto.test.ts +15 -17
  54. package/src/crypto.ts +4 -5
  55. package/src/janitor.ts +19 -12
  56. package/src/middleware/create-activity-object.test.ts +10 -0
  57. package/src/middleware/create-activity-object.ts +13 -0
  58. package/src/middleware/expand-activity-stream.test.data.ts +365 -0
  59. package/src/middleware/expand-activity-stream.test.ts +78 -0
  60. package/src/middleware/expand-activity-stream.ts +27 -0
  61. package/src/middleware/store-credentials.test.ts +72 -0
  62. package/src/middleware/store-credentials.ts +16 -0
  63. package/src/{validate.d.ts → middleware/validate.d.ts} +0 -0
  64. package/src/middleware/validate.test.data.ts +320 -0
  65. package/src/middleware/validate.test.ts +47 -0
  66. package/src/middleware/validate.ts +49 -0
  67. package/src/middleware.test.ts +148 -0
  68. package/src/middleware.ts +46 -51
  69. package/src/platform-instance.test.ts +224 -196
  70. package/src/platform-instance.ts +74 -58
  71. package/src/platform.ts +44 -24
  72. package/src/process-manager.ts +7 -4
  73. package/src/routes.test.ts +32 -19
  74. package/src/routes.ts +7 -5
  75. package/src/serve.ts +1 -1
  76. package/src/sockethub-client.test.ts +235 -0
  77. package/src/sockethub-client.ts +164 -0
  78. package/src/sockethub.ts +96 -93
  79. package/src/store.test.ts +26 -0
  80. package/src/store.ts +17 -0
  81. package/tsconfig.json +8 -8
  82. package/views/examples/dummy.ejs +7 -7
  83. package/views/examples/feeds.ejs +10 -10
  84. package/views/examples/irc.ejs +65 -59
  85. package/views/examples/shared.js +31 -29
  86. package/views/examples/xmpp.ejs +49 -58
  87. package/webpack.minified.config.js +14 -0
  88. package/webpack.normal.config.js +14 -0
  89. package/coverage/clover.xml +0 -190
  90. package/coverage/coverage-final.json +0 -6
  91. package/coverage/lcov-report/base.css +0 -224
  92. package/coverage/lcov-report/block-navigation.js +0 -79
  93. package/coverage/lcov-report/common.ts.html +0 -143
  94. package/coverage/lcov-report/config.ts.html +0 -359
  95. package/coverage/lcov-report/crypto.ts.html +0 -203
  96. package/coverage/lcov-report/favicon.png +0 -0
  97. package/coverage/lcov-report/index.html +0 -171
  98. package/coverage/lcov-report/platform-instance.ts.html +0 -740
  99. package/coverage/lcov-report/prettify.css +0 -1
  100. package/coverage/lcov-report/prettify.js +0 -2
  101. package/coverage/lcov-report/routes.ts.html +0 -353
  102. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  103. package/coverage/lcov-report/sorter.js +0 -170
  104. package/coverage/lcov-report/src/common.ts.html +0 -143
  105. package/coverage/lcov-report/src/config.ts.html +0 -359
  106. package/coverage/lcov-report/src/crypto.ts.html +0 -182
  107. package/coverage/lcov-report/src/index.html +0 -156
  108. package/coverage/lcov-report/src/platform-instance.ts.html +0 -740
  109. package/coverage/lcov-report/src/routes/base.ts.html +0 -359
  110. package/coverage/lcov-report/src/routes/examples.ts.html +0 -311
  111. package/coverage/lcov-report/src/routes/index.html +0 -111
  112. package/coverage/lcov-report/src/services/http.ts.html +0 -239
  113. package/coverage/lcov-report/src/services/index.html +0 -111
  114. package/coverage/lcov-report/src/services/redis.ts.html +0 -140
  115. package/coverage/lcov-report/src/shared-resources.ts.html +0 -104
  116. package/coverage/lcov.info +0 -336
  117. package/coverage/tmp/coverage-21649-1621596477257-0.json +0 -1
  118. package/dist/bootstrap/platforms.js.map +0 -1
  119. package/dist/js/client.js +0 -177
  120. package/dist/js/client.js.map +0 -1
  121. package/dist/resource-manager.js +0 -66
  122. package/dist/resource-manager.js.map +0 -1
  123. package/dist/routes/base.js +0 -92
  124. package/dist/routes/base.js.map +0 -1
  125. package/dist/routes/examples.js +0 -93
  126. package/dist/routes/examples.js.map +0 -1
  127. package/dist/services/http.js +0 -67
  128. package/dist/services/http.js.map +0 -1
  129. package/dist/services/redis.js +0 -23
  130. package/dist/services/redis.js.map +0 -1
  131. package/dist/shared-resources.js +0 -11
  132. package/dist/shared-resources.js.map +0 -1
  133. package/dist/validate.js +0 -157
  134. package/dist/validate.js.map +0 -1
  135. package/jest.config.js +0 -18
  136. package/src/js/client.js +0 -190
  137. package/src/validate.ts +0 -147
  138. package/test/middleware-suite.js +0 -101
  139. package/test/validate-suite.js +0 -338
package/dist/validate.js DELETED
@@ -1,157 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
- Object.defineProperty(o, "default", { enumerable: true, value: v });
11
- }) : function(o, v) {
12
- o["default"] = v;
13
- });
14
- var __importStar = (this && this.__importStar) || function (mod) {
15
- if (mod && mod.__esModule) return mod;
16
- var result = {};
17
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
- __setModuleDefault(result, mod);
19
- return result;
20
- };
21
- var __importDefault = (this && this.__importDefault) || function (mod) {
22
- return (mod && mod.__esModule) ? mod : { "default": mod };
23
- };
24
- Object.defineProperty(exports, "__esModule", { value: true });
25
- /**
26
- * responsible for handling the validation and expansion (when applicable) of all incoming objects
27
- */
28
- const tv4_1 = __importDefault(require("tv4"));
29
- const debug_1 = __importDefault(require("debug"));
30
- const urijs_1 = __importDefault(require("urijs"));
31
- const activity_streams_1 = __importDefault(require("activity-streams"));
32
- const SockethubSchemas = __importStar(require("sockethub-schemas"));
33
- const init_1 = __importDefault(require("./bootstrap/init"));
34
- const config_1 = __importDefault(require("./config"));
35
- const activity = activity_streams_1.default(config_1.default.get('activity-streams:opts'));
36
- // load sockethub-activity-stream schema and register it with tv4
37
- // http://sockethub.org/schemas/v0/activity-stream#
38
- tv4_1.default.addSchema(SockethubSchemas.ActivityStream.id, SockethubSchemas.ActivityStream);
39
- // load sockethub-activity-object schema and register it with tv4
40
- // http://sockethub.org/schemas/v0/activity-object#
41
- tv4_1.default.addSchema(SockethubSchemas.ActivityObject.id, SockethubSchemas.ActivityObject);
42
- // educated guess on what the displayName is, if it's not defined
43
- // since we know the @id is a URI, we prioritize by username, then fragment (no case yet for path)
44
- function ensureDisplayName(msg) {
45
- if ((msg['@id']) && (!msg.displayName)) {
46
- const uri = new urijs_1.default(msg['@id']);
47
- return uri.username() || getUriFragment(uri) || uri.path();
48
- }
49
- return msg.displayName;
50
- }
51
- function ensureObject(msg) {
52
- return !((typeof msg !== 'object') || (Array.isArray(msg)));
53
- }
54
- function errorHandler(type, msg, next) {
55
- return (err) => {
56
- return next(false, err, type, msg);
57
- };
58
- }
59
- // expand given prop to full object if they are just strings
60
- // FIXME are we sure this works? What's propName for?
61
- function expandProp(propName, prop) {
62
- return (typeof prop === 'string') ? activity.Object.get(prop, true) : prop;
63
- }
64
- function expandStream(msg) {
65
- msg.actor.displayName = ensureDisplayName(msg.actor);
66
- if (msg.target) {
67
- msg.target.displayName = ensureDisplayName(msg.target);
68
- }
69
- return msg;
70
- }
71
- function getUriFragment(uri) {
72
- const frag = uri.fragment();
73
- return (frag) ? '#' + frag : undefined;
74
- }
75
- function processActivityObject(msg, error, next) {
76
- if (!validateActivityObject(msg)) {
77
- return error(`activity-object schema validation failed: ${tv4_1.default.error.dataPath} = ${tv4_1.default.error.message}`);
78
- }
79
- msg.displayName = ensureDisplayName(msg);
80
- return next(true, msg); // passed validation, on to next handler in middleware chain
81
- }
82
- function processActivityStream(msg, error, next) {
83
- let stream;
84
- try { // expands the AS object to a full object with the expected properties
85
- stream = activity.Stream(msg);
86
- }
87
- catch (e) {
88
- return error(e);
89
- }
90
- msg = stream; // overwrite message with expanded as object stream
91
- if (!validateActivityStream(msg)) {
92
- return error(`actvity-stream schema validation failed: ${tv4_1.default.error.dataPath}: ${tv4_1.default.error.message}`);
93
- }
94
- // passed validation, on to next handler in middleware chain
95
- return next(true, expandStream(msg));
96
- }
97
- function processCredentials(msg, error, next) {
98
- msg.actor = expandProp('actor', msg.actor);
99
- msg.target = expandProp('target', msg.target);
100
- let credentialsSchema = tv4_1.default.getSchema(`http://sockethub.org/schemas/v0/context/${msg.context}/credentials`);
101
- if (!credentialsSchema) {
102
- return error(`no credentials schema found for ${msg.context} context`);
103
- }
104
- if (!validateCredentials(msg, credentialsSchema)) {
105
- return error(`credentials schema validation failed: ${tv4_1.default.error.dataPath} = ${tv4_1.default.error.message}`);
106
- }
107
- // passed validation, on to next handler in middleware chain
108
- return next(true, expandStream(msg));
109
- }
110
- function validateActivityObject(msg) {
111
- return tv4_1.default.validate({ object: msg }, SockethubSchemas.ActivityObject);
112
- }
113
- function validateCredentials(msg, schema) {
114
- return tv4_1.default.validate(msg, schema);
115
- }
116
- function validateActivityStream(msg) {
117
- // TODO figure out a way to allow for special objects from platforms, without
118
- // ignoring failed activity stream schema checks
119
- if (!tv4_1.default.validate(msg, SockethubSchemas.ActivityStream)) {
120
- return tv4_1.default.getSchema(`http://sockethub.org/schemas/v0/context/${msg.context}/messages`);
121
- }
122
- return true;
123
- }
124
- // called when registered with the middleware function, define the type of validation
125
- // that will be called when the middleware eventually does.
126
- function validate(type, sockethubId) {
127
- const sessionLog = debug_1.default(`sockethub:validate:${sockethubId}`);
128
- // called by the middleware with the message and the next (callback) in the chain
129
- return (next, msg) => {
130
- sessionLog('applying schema validation for ' + type);
131
- const error = errorHandler(type, msg, next);
132
- if (!ensureObject(msg)) {
133
- error(`message received is not an object.`);
134
- }
135
- else if (type === 'activity-object') {
136
- processActivityObject(msg, error, next);
137
- }
138
- else if (typeof msg.context !== 'string') {
139
- error('message must contain a context property');
140
- }
141
- else if (!init_1.default.platforms.has(msg.context)) {
142
- error(`platform context ${msg.context} not registered with this sockethub instance.`);
143
- }
144
- else if ((type === 'message') && (typeof msg['@type'] !== 'string')) {
145
- error('message must contain a @type property.');
146
- }
147
- else if (type === 'credentials') {
148
- processCredentials(msg, error, next);
149
- }
150
- else {
151
- processActivityStream(msg, error, next);
152
- }
153
- };
154
- }
155
- exports.default = validate;
156
- ;
157
- //# sourceMappingURL=validate.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"validate.js","sourceRoot":"","sources":["../src/validate.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA;;GAEG;AACH,8CAAsB;AACtB,kDAA0B;AAC1B,kDAAwB;AACxB,wEAA+C;AAC/C,oEAAsD;AAEtD,4DAAoC;AACpC,sDAA8B;AAE9B,MAAM,QAAQ,GAAG,0BAAe,CAAC,gBAAM,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC,CAAC;AAEtE,iEAAiE;AACjE,mDAAmD;AACnD,aAAG,CAAC,SAAS,CAAC,gBAAgB,CAAC,cAAc,CAAC,EAAE,EAAE,gBAAgB,CAAC,cAAc,CAAC,CAAC;AACnF,iEAAiE;AACjE,mDAAmD;AACnD,aAAG,CAAC,SAAS,CAAC,gBAAgB,CAAC,cAAc,CAAC,EAAE,EAAE,gBAAgB,CAAC,cAAc,CAAC,CAAC;AAEnF,iEAAiE;AACjE,kGAAkG;AAClG,SAAS,iBAAiB,CAAC,GAAQ;IACjC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAE,GAAG,CAAC,WAAW,CAAC,EAAE;QACvC,MAAM,GAAG,GAAG,IAAI,eAAG,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC;QAChC,OAAO,GAAG,CAAC,QAAQ,EAAE,IAAI,cAAc,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,IAAI,EAAE,CAAC;KAC5D;IACD,OAAO,GAAG,CAAC,WA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,KAAK,CAAC,oCAAoC,CAAC,CAAC;SAC7C;aAAM,IAAI,IAAI,KAAK,iBAAiB,EAAE;YACrC,qBAAqB,CAAC,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;SACzC;aAAM,IAAI,OAAO,GAAG,CAAC,OAAO,KAAK,QAAQ,EAAE;YAC1C,KAAK,CAAC,yCAAyC,CAAC,CAAC;SAClD;aAAM,IAAI,CAAE,cAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE;YAC5C,KAAK,CAAC,oBAAoB,GAAG,CAAC,OAAO,+CAA+C,CAAC,CAAC;SACvF;aAAM,IAAI,CAAC,IAAI,KAAK,SAAS,CAAC,IAAI,CAAC,OAAO,GAAG,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC,EAAE;YACrE,KAAK,CAAC,wCAAwC,CAAC,CAAC;SACjD;aAAM,IAAI,IAAI,KAAK,aAAa,EAAE;YACjC,kBAAkB,CAAC,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;SACtC;aAAM;YACL,qBAAqB,CAAC,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;SACzC;IACH,CAAC,CAAC;AACJ,CAAC;AAvBD,2BAuBC;AAAA,CAAC"}
package/jest.config.js DELETED
@@ -1,18 +0,0 @@
1
- module.exports = {
2
- preset: 'ts-jest',
3
- testEnvironment: 'node',
4
- coverageThreshold: {
5
- global: {
6
- branches: 75,
7
- functions: 80,
8
- lines: 90,
9
- statements: 90,
10
- },
11
- },
12
- coverageReporters: [
13
- 'json',
14
- 'lcov',
15
- 'text',
16
- 'clover',
17
- ]
18
- };
package/src/js/client.js DELETED
@@ -1,190 +0,0 @@
1
- (function (global, factory) {
2
-
3
- if ( typeof module === 'object' && typeof module.exports === 'object' ) {
4
- let ASFactory = require('activity-streams');
5
- module.exports = (global.document) ? factory(global, ASFactory) : factory({}, ASFactory);
6
- } else {
7
- // @ts-ignore
8
- if (! global.ASFactory) {
9
- throw new Error('sockethub-client.js depends on the activity-streams module.');
10
- }
11
- // @ts-ignore
12
- factory(global, global.ASFactory);
13
- }
14
-
15
- }((typeof window !== 'undefined') ? window : this, function (scope, ASFactory) {
16
-
17
- function SockethubClient(socket) {
18
- this.socket = socket;
19
- this.ActivityStreams = ASFactory({specialObjs: ['credentials']});
20
- this.online = false;
21
- this.debug = false;
22
-
23
- this.__registerSocketIOHandlers();
24
-
25
- this.ActivityStreams.on('activity-object-create', (obj) => {
26
- socket.emit('activity-object', obj);
27
- });
28
-
29
- socket.on('activity-object', (obj) => {
30
- this.ActivityStreams.Object.create(obj);
31
- });
32
- }
33
-
34
- SockethubClient.prototype.log = function (msg, obj) {
35
- if (this.debug) {
36
- console.log(msg, obj);
37
- }
38
- };
39
-
40
- SockethubClient.prototype.__handlers = {
41
- // wrapping the `on` method in order to automatically unpack Activity Streams objects
42
- // that come in on the 'messages' channel, so that app developers don't need to.
43
- message: function (msg) {
44
- console.warn(`message event received with no handler registered: `, msg);
45
- },
46
- completed: function (msg) {
47
- console.warn('completed event received with no handler registered: ', msg);
48
- },
49
- failed: function (msg) {
50
- console.warn('failed event received with no handler registered: ', msg);
51
- },
52
- reconnecting: function (msg) {
53
- console.warn('reconnecting: ', msg);
54
- },
55
- reconnect_failed: function (msg) {
56
- console.warn('reconnect failed: ', msg);
57
- },
58
- connect: function () {
59
- console.warn('connected.');
60
- },
61
- reconnect: function (msg) {
62
- console.warn('reconnected: ', msg);
63
- },
64
- disconnect: function (msg) {
65
- console.warn('disconnected: ', msg);
66
- }
67
- };
68
-
69
- SockethubClient.prototype.__registerSocketIOHandlers = function () {
70
- let storedCredentials = new Map(),
71
- storedActivityObjects = new Map(),
72
- storedConnects = new Map(),
73
- storedJoins = new Map();
74
-
75
- // middleware for events which don't deal in AS objects
76
- const callHandler = (event) => {
77
- return (obj) => {
78
- if (event === 'reconnect') {
79
- this.online = true;
80
- this.__replay('activity-object', storedActivityObjects);
81
- this.__replay('credentials', storedCredentials);
82
- this.__replay('message', storedConnects);
83
- this.__replay('message', storedJoins);
84
- } else if (event === 'connect') {
85
- this.online = true;
86
- } else if (event === 'disconnect') {
87
- this.online = false;
88
- }
89
- this.__handlers[event](obj);
90
- };
91
- };
92
-
93
- // register for events that give us information on connection status
94
- this.socket.on('reconnecting', callHandler('reconnecting'));
95
- this.socket.on('reconnect_failed', callHandler('reconnect_failed'));
96
- this.socket.on('connect', callHandler('connect'));
97
- this.socket.on('reconnect', callHandler('reconnect'));
98
- this.socket.on('disconnect', callHandler('disconnect'));
99
-
100
- // do our middle-ware stuff then call the stored handler
101
- this.socket.on('message', this.__unpackAndCallHandler('message'));
102
- this.socket.on('completed', this.__unpackAndCallHandler('completed'));
103
- this.socket.on('failed', this.__unpackAndCallHandler('failed'));
104
-
105
- // we over-ride socketIO's emit method in order to catch credentials, activity-objects,
106
- // and joins, and store them for replay if we connect.
107
- // joins are only stored until confirmation is received that it was sent, then we remove it
108
- // from the map.
109
- this.socket._emit = this.socket.emit;
110
- this.socket.emit = (channel, content) => {
111
- if (typeof channel !== 'string') {
112
- throw new Error('emit function requires a channel name [string] to be specified as the'
113
- + ' first parameter.');
114
- }
115
- switch (channel) {
116
- case 'credentials':
117
- if ((content.object) && (content.object['@type'] === 'credentials')) {
118
- storedCredentials.set(content.actor['@id'] || content.actor, content);
119
- }
120
- break;
121
- case 'activity-object':
122
- if (content['@id']) {
123
- storedActivityObjects.set(content['@id'], content);
124
- }
125
- break;
126
- case 'message':
127
- if (this.online) {
128
- // either store or delete the specified content onto the storedJoins map,
129
- // for reply once we're back online.
130
- if (content['@type'] === 'join') {
131
- storedJoins['set'](this.__getKey(content), content);
132
- } else if (content['@type'] === 'leave') {
133
- storedJoins['delete'](this.__getKey(content), content);
134
- } if (content['@type'] === 'connect') {
135
- storedConnects['set'](this.__getKey(content), content);
136
- } else if (content['@type'] === 'disconnect') {
137
- storedConnects['delete'](this.__getKey(content), content);
138
- }
139
- } else {
140
- // reject message if we're not online
141
- this.socket._emit('failed', content);
142
- }
143
- break;
144
- }
145
- this.socket._emit(channel, content);
146
- };
147
-
148
- this.socket._on = this.socket.on;
149
- this.socket.on = (event, cb) => {
150
- if (Object.keys(this.__handlers).includes(event)) {
151
- // store the desired message channel callback, because we use our own first
152
- this.__handlers[event] = cb;
153
- } else {
154
- // pass on any other handlers
155
- this.socket._on(event, cb);
156
- }
157
- };
158
- };
159
-
160
- SockethubClient.prototype.__replay = function (name, asMap) {
161
- asMap.forEach((obj) => {
162
- this.log(`replaying ${name}`, obj);
163
- this.socket._emit(name, obj);
164
- });
165
- };
166
-
167
- // use as a middleware to receive incoming Sockethub messages and unpack them
168
- // using the ActivityStreams library before passing them along to the app.
169
- SockethubClient.prototype.__unpackAndCallHandler = function (event) {
170
- return (obj) => {
171
- let unpackedObject = this.ActivityStreams.Stream(obj);
172
- this.__handlers[event](unpackedObject);
173
- };
174
- };
175
-
176
- SockethubClient.prototype.__getKey = function (content) {
177
- let actor = content.actor['@id'] || content.actor;
178
- let target = content.target ? content.target['@id'] || content.target : '';
179
- return actor + '-' + target;
180
- };
181
-
182
- if ( typeof define === 'function' && define.amd ) {
183
- define([], function () {
184
- return SockethubClient;
185
- });
186
- }
187
-
188
- scope.SockethubClient = SockethubClient;
189
- return SockethubClient;
190
- }));
package/src/validate.ts DELETED
@@ -1,147 +0,0 @@
1
- /**
2
- * responsible for handling the validation and expansion (when applicable) of all incoming objects
3
- */
4
- import tv4 from 'tv4';
5
- import debug from 'debug';
6
- import URI from 'urijs';
7
- import ActivityStreams from 'activity-streams';
8
- import * as SockethubSchemas from 'sockethub-schemas';
9
-
10
- import init from './bootstrap/init';
11
- import config from './config';
12
-
13
- const activity = ActivityStreams(config.get('activity-streams:opts'));
14
-
15
- // load sockethub-activity-stream schema and register it with tv4
16
- // http://sockethub.org/schemas/v0/activity-stream#
17
- tv4.addSchema(SockethubSchemas.ActivityStream.id, SockethubSchemas.ActivityStream);
18
- // load sockethub-activity-object schema and register it with tv4
19
- // http://sockethub.org/schemas/v0/activity-object#
20
- tv4.addSchema(SockethubSchemas.ActivityObject.id, SockethubSchemas.ActivityObject);
21
-
22
- // educated guess on what the displayName is, if it's not defined
23
- // since we know the @id is a URI, we prioritize by username, then fragment (no case yet for path)
24
- function ensureDisplayName(msg: any) {
25
- if ((msg['@id']) && (! msg.displayName)) {
26
- const uri = new URI(msg['@id']);
27
- return uri.username() || getUriFragment(uri) || uri.path();
28
- }
29
- return msg.displayName;
30
- }
31
-
32
- function ensureObject(msg: any) {
33
- return !((typeof msg !== 'object') || (Array.isArray(msg)));
34
- }
35
-
36
- function errorHandler(type: any, msg: any, next: any) {
37
- return (err) => {
38
- return next(false, err, type, msg);
39
- };
40
- }
41
-
42
- // expand given prop to full object if they are just strings
43
- // FIXME are we sure this works? What's propName for?
44
- function expandProp(propName, prop) {
45
- return (typeof prop === 'string') ? activity.Object.get(prop, true) : prop;
46
- }
47
-
48
- function expandStream(msg) {
49
- msg.actor.displayName = ensureDisplayName(msg.actor);
50
- if (msg.target) {
51
- msg.target.displayName = ensureDisplayName(msg.target);
52
- }
53
- return msg;
54
- }
55
-
56
- function getUriFragment(uri: any) {
57
- const frag = uri.fragment();
58
- return (frag) ? '#' + frag : undefined;
59
- }
60
-
61
- function processActivityObject(msg: any, error: any, next: any) {
62
- if (! validateActivityObject(msg)) {
63
- return error(
64
- `activity-object schema validation failed: ${tv4.error.dataPath} = ${tv4.error.message}`
65
- );
66
- }
67
- msg.displayName = ensureDisplayName(msg);
68
- return next(true, msg); // passed validation, on to next handler in middleware chain
69
- }
70
-
71
- function processActivityStream(msg: any, error: any, next: any) {
72
- let stream;
73
- try { // expands the AS object to a full object with the expected properties
74
- stream = activity.Stream(msg);
75
- } catch (e) {
76
- return error(e);
77
- }
78
- msg = stream; // overwrite message with expanded as object stream
79
-
80
- if (! validateActivityStream(msg)) {
81
- return error(
82
- `actvity-stream schema validation failed: ${tv4.error.dataPath}: ${tv4.error.message}`);
83
- }
84
- // passed validation, on to next handler in middleware chain
85
- return next(true, expandStream(msg));
86
- }
87
-
88
- function processCredentials(msg: any, error: any, next: any) {
89
- msg.actor = expandProp('actor', msg.actor);
90
- msg.target = expandProp('target', msg.target);
91
- let credentialsSchema = tv4.getSchema(
92
- `http://sockethub.org/schemas/v0/context/${msg.context}/credentials`);
93
- if (! credentialsSchema) {
94
- return error(`no credentials schema found for ${msg.context} context`);
95
- }
96
-
97
- if (! validateCredentials(msg, credentialsSchema)) {
98
- return error(
99
- `credentials schema validation failed: ${tv4.error.dataPath} = ${tv4.error.message}`);
100
- }
101
- // passed validation, on to next handler in middleware chain
102
- return next(true, expandStream(msg));
103
- }
104
-
105
- function validateActivityObject(msg: any) {
106
- return tv4.validate({ object: msg }, SockethubSchemas.ActivityObject);
107
- }
108
-
109
- function validateCredentials(msg: any, schema: any) {
110
- return tv4.validate(msg, schema);
111
- }
112
-
113
- function validateActivityStream(msg: any) {
114
- // TODO figure out a way to allow for special objects from platforms, without
115
- // ignoring failed activity stream schema checks
116
- if (! tv4.validate(msg, SockethubSchemas.ActivityStream)) {
117
- return tv4.getSchema(`http://sockethub.org/schemas/v0/context/${msg.context}/messages`);
118
- }
119
- return true;
120
- }
121
-
122
- // called when registered with the middleware function, define the type of validation
123
- // that will be called when the middleware eventually does.
124
- export default function validate(type: string, sockethubId: string) {
125
- const sessionLog = debug(`sockethub:validate:${sockethubId}`);
126
- // called by the middleware with the message and the next (callback) in the chain
127
- return (next, msg) => {
128
- sessionLog('applying schema validation for ' + type);
129
- const error = errorHandler(type, msg, next);
130
-
131
- if (! ensureObject(msg)) {
132
- error(`message received is not an object.`);
133
- } else if (type === 'activity-object') {
134
- processActivityObject(msg, error, next);
135
- } else if (typeof msg.context !== 'string') {
136
- error('message must contain a context property');
137
- } else if (! init.platforms.has(msg.context)) {
138
- error(`platform context ${msg.context} not registered with this sockethub instance.`);
139
- } else if ((type === 'message') && (typeof msg['@type'] !== 'string')) {
140
- error('message must contain a @type property.');
141
- } else if (type === 'credentials') {
142
- processCredentials(msg, error, next);
143
- } else {
144
- processActivityStream(msg, error, next);
145
- }
146
- };
147
- };
@@ -1,101 +0,0 @@
1
- if (typeof define !== 'function') {
2
- let define = require('amdefine')(module);
3
- }
4
- define(['require', '../dist/middleware'], function (require, Middleware) {
5
- return [
6
- {
7
- desc: 'src/middleware',
8
- abortOnFail: true,
9
- beforeEach: function (env, test) {
10
- env.middleware = new Middleware.default((...params) => {
11
- this.fail(params);
12
- });
13
- test.done();
14
- },
15
- tests: [
16
- {
17
- desc: 'basic 1 input param',
18
- run: function (env, test) {
19
- test.assertType(env.middleware.chain, 'function');
20
- const m = env.middleware.chain((next, input) => {
21
- next(true, input);
22
- }, (input) => {
23
- test.assert(input, 'alacadabra');
24
- });
25
- m('alacadabra');
26
- }
27
- },
28
-
29
- {
30
- desc: 'basic 1 input param with error',
31
- run: function (env, test) {
32
- const middleware = new Middleware.default((err, input) => {
33
- test.assertAnd(err, 'incorrect input');
34
- test.assert(input, 'foobar');
35
- });
36
- const m = middleware.chain((next, input) => {
37
- next(false, 'incorrect input', input);
38
- }, (input) => {
39
- test.fail('should not reach final function in chain');
40
- });
41
- m('foobar');
42
- }
43
- },
44
-
45
- {
46
- desc: 'basic 1 input param, 5 funcs',
47
- run: function (env, test) {
48
-
49
- const m = env.middleware.chain((next, input) => {
50
- next(true, ++input);
51
- }, (next, input) => {
52
- next(true, ++input);
53
- }, (next, input) => {
54
- next(true, ++input);
55
- }, (next, input) => {
56
- next(true, ++input);
57
- }, (input) => {
58
- test.assert(input, 4);
59
- });
60
- m(0);
61
- }
62
- },
63
-
64
- {
65
- desc: 'basic 2 inputs param, 2 funcs',
66
- run: function (env, test) {
67
- const cb = function () {
68
- test.done();
69
- };
70
- const m = env.middleware.chain((next, input, cb) => {
71
- next(true, ++input, cb);
72
- }, (input, cb) => {
73
- test.assertAnd(input, 1);
74
- cb();
75
- });
76
- m(0, cb);
77
- }
78
- },
79
-
80
- {
81
- desc: 'basic 4 input params, 5 funcs',
82
- run: function (env, test) {
83
-
84
- const m = env.middleware.chain((next, input1, input2, input3, input4) => {
85
- next(true, ++input1, input2, input3, input4);
86
- }, (next, input1, input2, input3, input4) => {
87
- next(true, input1, ++input2, input3, input4);
88
- }, (next, input1, input2, input3, input4) => {
89
- next(true, input1, input2, ++input3, input4);
90
- }, (next, input1, input2, input3, input4) => {
91
- next(true, input1, input2, input3, ++input4);
92
- }, (input1, input2, input3, input4) => {
93
- test.assert([input1, input2, input3, input4], [1, 1, 4, 6]);
94
- });
95
- m(0, 0, 2, 5);
96
- }
97
- }
98
- ]
99
- }
100
- ];
101
- });