sockethub 5.0.0-alpha.2 → 5.0.0-alpha.4

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 (101) hide show
  1. package/bin/sockethub +2 -29
  2. package/build.js +21 -0
  3. package/package.json +15 -65
  4. package/{dist/defaults.json → sockethub.config.json} +8 -7
  5. package/tsconfig.json +6 -68
  6. package/README.md +0 -98
  7. package/config.json.example +0 -32
  8. package/dist/bootstrap/init.js +0 -74
  9. package/dist/bootstrap/init.js.map +0 -1
  10. package/dist/bootstrap/platforms.js +0 -103
  11. package/dist/common.js +0 -20
  12. package/dist/common.js.map +0 -1
  13. package/dist/config.js +0 -60
  14. package/dist/config.js.map +0 -1
  15. package/dist/crypto.js +0 -38
  16. package/dist/crypto.js.map +0 -1
  17. package/dist/janitor.js +0 -98
  18. package/dist/janitor.js.map +0 -1
  19. package/dist/middleware/create-activity-object.js +0 -19
  20. package/dist/middleware/create-activity-object.js.map +0 -1
  21. package/dist/middleware/expand-activity-stream.js +0 -33
  22. package/dist/middleware/expand-activity-stream.js.map +0 -1
  23. package/dist/middleware/expand-activity-stream.test.data.js +0 -360
  24. package/dist/middleware/expand-activity-stream.test.data.js.map +0 -1
  25. package/dist/middleware/store-credentials.js +0 -19
  26. package/dist/middleware/store-credentials.js.map +0 -1
  27. package/dist/middleware/validate.js +0 -77
  28. package/dist/middleware/validate.js.map +0 -1
  29. package/dist/middleware/validate.test.data.js +0 -321
  30. package/dist/middleware/validate.test.data.js.map +0 -1
  31. package/dist/middleware.js +0 -54
  32. package/dist/middleware.js.map +0 -1
  33. package/dist/platform-instance.js +0 -226
  34. package/dist/platform-instance.js.map +0 -1
  35. package/dist/platform.js +0 -186
  36. package/dist/platform.js.map +0 -1
  37. package/dist/process-manager.js +0 -78
  38. package/dist/process-manager.js.map +0 -1
  39. package/dist/routes.js +0 -84
  40. package/dist/routes.js.map +0 -1
  41. package/dist/serve.js +0 -102
  42. package/dist/serve.js.map +0 -1
  43. package/dist/sockethub-client.js +0 -2604
  44. package/dist/sockethub-client.js.map +0 -1
  45. package/dist/sockethub-client.min.js +0 -2
  46. package/dist/sockethub-client.min.js.LICENSE.txt +0 -24
  47. package/dist/sockethub.js +0 -130
  48. package/dist/sockethub.js.map +0 -1
  49. package/dist/store.js +0 -17
  50. package/dist/store.js.map +0 -1
  51. package/src/bootstrap/init.d.ts +0 -8
  52. package/src/bootstrap/init.ts +0 -91
  53. package/src/bootstrap/platforms.js +0 -103
  54. package/src/common.test.ts +0 -54
  55. package/src/common.ts +0 -14
  56. package/src/config.d.ts +0 -2
  57. package/src/config.test.ts +0 -28
  58. package/src/config.ts +0 -72
  59. package/src/crypto.d.ts +0 -5
  60. package/src/crypto.test.ts +0 -41
  61. package/src/crypto.ts +0 -41
  62. package/src/defaults.json +0 -32
  63. package/src/janitor.d.ts +0 -8
  64. package/src/janitor.ts +0 -89
  65. package/src/middleware/create-activity-object.test.ts +0 -10
  66. package/src/middleware/create-activity-object.ts +0 -13
  67. package/src/middleware/expand-activity-stream.test.data.ts +0 -365
  68. package/src/middleware/expand-activity-stream.test.ts +0 -78
  69. package/src/middleware/expand-activity-stream.ts +0 -27
  70. package/src/middleware/store-credentials.test.ts +0 -72
  71. package/src/middleware/store-credentials.ts +0 -16
  72. package/src/middleware/validate.d.ts +0 -1
  73. package/src/middleware/validate.test.data.ts +0 -320
  74. package/src/middleware/validate.test.ts +0 -47
  75. package/src/middleware/validate.ts +0 -49
  76. package/src/middleware.d.ts +0 -21
  77. package/src/middleware.test.ts +0 -148
  78. package/src/middleware.ts +0 -52
  79. package/src/platform-instance.test.ts +0 -237
  80. package/src/platform-instance.ts +0 -236
  81. package/src/platform.ts +0 -189
  82. package/src/process-manager.ts +0 -64
  83. package/src/routes.test.ts +0 -100
  84. package/src/routes.ts +0 -93
  85. package/src/serve.ts +0 -79
  86. package/src/sockethub-client.test.ts +0 -235
  87. package/src/sockethub-client.ts +0 -164
  88. package/src/sockethub.d.ts +0 -1
  89. package/src/sockethub.ts +0 -184
  90. package/src/store.test.ts +0 -26
  91. package/src/store.ts +0 -17
  92. package/test/init-suite.js +0 -41
  93. package/test/sockethub-suite.js +0 -25
  94. package/views/examples/dummy.ejs +0 -93
  95. package/views/examples/feeds.ejs +0 -90
  96. package/views/examples/irc.ejs +0 -239
  97. package/views/examples/shared.js +0 -72
  98. package/views/examples/xmpp.ejs +0 -191
  99. package/views/index.ejs +0 -17
  100. package/webpack.minified.config.js +0 -14
  101. package/webpack.normal.config.js +0 -14
package/src/platform.ts DELETED
@@ -1,189 +0,0 @@
1
- import debug from 'debug';
2
- import hash from "object-hash";
3
- import config from './config';
4
- import Queue from 'bull';
5
- import { getPlatformId, decryptJobData } from "./common";
6
- import { ActivityStream, JobDataDecrypted, JobEncrypted } from "./sockethub";
7
- import { MessageFromParent } from './platform-instance';
8
- import { getSessionStore } from "./store";
9
-
10
- // command-line params
11
- const parentId = process.argv[2];
12
- const platformName = process.argv[3];
13
- let identifier = process.argv[4];
14
- const loggerPrefix = `sockethub:platform:${platformName}:${identifier}`;
15
- let logger = debug(loggerPrefix);
16
-
17
- const PlatformModule = require(`sockethub-platform-${platformName}`);
18
-
19
- let queueStarted = false;
20
- let parentSecret1: string, parentSecret2: string;
21
-
22
- logger(`platform handler initialized for ${platformName} ${identifier}`);
23
-
24
- export interface PlatformSession {
25
- debug(msg: string): void;
26
- sendToClient(msg: ActivityStream, special?: string): void;
27
- updateActor(credentials: object): void;
28
- }
29
-
30
- /**
31
- * Handle any uncaught errors from the platform by alerting the worker and shutting down.
32
- */
33
- process.on('uncaughtException', (err) => {
34
- console.log('EXCEPTION IN PLATFORM');
35
- // eslint-disable-next-line security-node/detect-crlf
36
- console.log(err.stack);
37
- process.send(['error', err.toString()]);
38
- process.exit(1);
39
- });
40
-
41
- /**
42
- * Incoming messages from the worker to this platform. Data is an array, the first property is the
43
- * method to call, the rest are params.
44
- */
45
- process.on('message', (data: MessageFromParent) => {
46
- if (data[0] === 'secrets') {
47
- parentSecret1 = data[1].parentSecret1;
48
- parentSecret2 = data[1].parentSecret2;
49
- startQueueListener();
50
- }
51
- });
52
-
53
-
54
- /**
55
- * Initialize platform module
56
- */
57
- const platformSession: PlatformSession = {
58
- debug: debug(`sockethub:platform:${platformName}:${identifier}`),
59
- sendToClient: getSendFunction('message'),
60
- updateActor: updateActor
61
- };
62
- const platform = new PlatformModule(platformSession);
63
-
64
- /**
65
- * Get the credentials stored for this user in this sessions store, if given the correct
66
- * sessionSecret.
67
- * @param actorId
68
- * @param sessionId
69
- * @param sessionSecret
70
- * @param cb
71
- */
72
- function getCredentials(actorId: string, sessionId: string, sessionSecret: string, cb: Function) {
73
- if (platform.config.noCredentials) { return cb(); }
74
- const store = getSessionStore(parentId, parentSecret1, sessionId, sessionSecret);
75
- store.get(actorId, (err, credentials) => {
76
- if (platform.config.persist) {
77
- // don't continue if we don't get credentials
78
- if (err) { return cb(err); }
79
- } else if (! credentials) {
80
- // also skip if this is a non-persist platform with no credentials
81
- return cb();
82
- }
83
-
84
- if (platform.credentialsHash) {
85
- if (platform.credentialsHash !== hash(credentials.object)) {
86
- return cb('provided credentials do not match existing platform instance for actor '
87
- + platform.actor.id);
88
- }
89
- } else {
90
- platform.credentialsHash = hash(credentials.object);
91
- }
92
- cb(undefined, credentials);
93
- });
94
- }
95
-
96
- /**
97
- * Returns a function used to handle completed jobs from the platform code (the `done` callback).
98
- * @param secret the secret used to decrypt credentials
99
- */
100
- function getJobHandler(secret: string) {
101
- return (job: JobEncrypted, done: Function) => {
102
- const jobData: JobDataDecrypted = decryptJobData(job, secret);
103
- const jobLog = debug(`${loggerPrefix}:${jobData.sessionId}`);
104
- jobLog(`received ${jobData.title} ${jobData.msg.type}`);
105
- const sessionSecret = jobData.msg.sessionSecret;
106
- delete jobData.msg.sessionSecret;
107
-
108
- return getCredentials(jobData.msg.actor.id, jobData.sessionId, sessionSecret,
109
- (err, credentials) => {
110
- if (err) { return done(new Error(err)); }
111
- let jobCallbackCalled = false;
112
- const doneCallback = (err, result) => {
113
- if (jobCallbackCalled) { return; }
114
- jobCallbackCalled = true;
115
- if (err) {
116
- jobLog(`errored ${jobData.title} ${jobData.msg.type}`);
117
- let errMsg;
118
- // some error objects (eg. TimeoutError) don't interoplate correctly to human-readable
119
- // so we have to do this little dance
120
- try {
121
- errMsg = err.toString();
122
- } catch (e) {
123
- errMsg = err;
124
- }
125
- done(new Error(errMsg));
126
- } else {
127
- jobLog(`completed ${jobData.title} ${jobData.msg.type}`);
128
- done(null, result);
129
- }
130
- };
131
- if ((Array.isArray(platform.config.requireCredentials)) &&
132
- (platform.config.requireCredentials.includes(jobData.msg.type))) {
133
- // add the credentials object if this method requires it
134
- platform[jobData.msg.type](jobData.msg, credentials, doneCallback);
135
- } else if (platform.config.persist) {
136
- if (platform.initialized) {
137
- platform[jobData.msg.type](jobData.msg, doneCallback);
138
- } else {
139
- done(new Error(`${jobData.msg.type} called on uninitialized platform`));
140
- }
141
- } else {
142
- platform[jobData.msg.type](jobData.msg, doneCallback);
143
- }
144
- });
145
- };
146
- }
147
-
148
- /**
149
- * Get an function which sends a message to the parent thread (PlatformInstance). The platform
150
- * can call that function to send messages back to the client.
151
- * @param command string containing the type of command to be sent. 'message' or 'close'
152
- */
153
- function getSendFunction(command: string) {
154
- return function (msg: ActivityStream, special?: string) {
155
- process.send([command, msg, special]);
156
- };
157
- }
158
-
159
- /**
160
- * When a user changes it's actor name, the channel identifier changes, we need to ensure that
161
- * both the queue thread (listening on the channel for jobs) and the logging object are updated.
162
- * @param credentials
163
- */
164
- function updateActor(credentials) {
165
- identifier = getPlatformId(platformName, credentials.actor.id);
166
- logger(`platform actor updated to ${credentials.actor.id} identifier ${identifier}`);
167
- logger = debug(`sockethub:platform:${identifier}`);
168
- platform.credentialsHash = hash(credentials.object);
169
- platform.debug = debug(`sockethub:platform:${platformName}:${identifier}`);
170
- process.send(['updateActor', undefined, identifier]);
171
- startQueueListener(true);
172
- }
173
-
174
- /**
175
- * starts listening on the queue for incoming jobs
176
- * @param refresh boolean if the param is true, we re-init the queue.process
177
- * (used when identifier changes)
178
- */
179
- function startQueueListener(refresh: boolean = false) {
180
- const secret = parentSecret1 + parentSecret2;
181
- if ((queueStarted) && (!refresh)) {
182
- logger('start queue called multiple times, skipping');
183
- return;
184
- }
185
- const queue = new Queue(parentId + identifier, { redis: config.get('redis') });
186
- queueStarted = true;
187
- logger('listening on the queue for incoming jobs');
188
- queue.process(getJobHandler(secret));
189
- }
@@ -1,64 +0,0 @@
1
- import init from './bootstrap/init';
2
- import PlatformInstance, {
3
- platformInstances, PlatformInstanceParams, MessageFromParent } from "./platform-instance";
4
- import { getPlatformId } from "./common";
5
-
6
- class ProcessManager {
7
- private readonly parentId: string;
8
- private readonly parentSecret1: string;
9
- private readonly parentSecret2: string;
10
-
11
- constructor(parentId: string, parentSecret1: string, parentSecret2: string) {
12
- this.parentId = parentId;
13
- this.parentSecret1 = parentSecret1;
14
- this.parentSecret2 = parentSecret2;
15
- }
16
-
17
- get(platform: string, actorId: string, sessionId?: string): PlatformInstance {
18
- const platformDetails = init.platforms.get(platform);
19
- let pi;
20
-
21
- if (platformDetails.config.persist) {
22
- // ensure process is started - one for each actor
23
- pi = this.ensureProcess(platform, sessionId, actorId);
24
- } else {
25
- // ensure process is started - one for all jobs
26
- pi = this.ensureProcess(platform);
27
- }
28
- pi.config = platformDetails.config;
29
- return pi;
30
- }
31
-
32
- private createPlatformInstance(identifier: string, platform: string,
33
- actor?: string): PlatformInstance {
34
- const secrets: MessageFromParent = [
35
- 'secrets', {
36
- parentSecret1: this.parentSecret1,
37
- parentSecret2: this.parentSecret2
38
- }
39
- ];
40
- const platformInstanceConfig: PlatformInstanceParams = {
41
- identifier: identifier,
42
- platform: platform,
43
- parentId: this.parentId,
44
- actor: actor
45
- };
46
- const platformInstance = new PlatformInstance(platformInstanceConfig);
47
- platformInstance.initQueue(this.parentSecret1 + this.parentSecret2);
48
- platformInstance.process.send(secrets);
49
- return platformInstance;
50
- }
51
-
52
- private ensureProcess(platform: string, sessionId?: string, actor?: string): PlatformInstance {
53
- const identifier = getPlatformId(platform, actor);
54
- const platformInstance = platformInstances.get(identifier) ||
55
- this.createPlatformInstance(identifier, platform, actor);
56
- if (sessionId) {
57
- platformInstance.registerSession(sessionId);
58
- }
59
- platformInstances.set(identifier, platformInstance);
60
- return platformInstance;
61
- }
62
- }
63
-
64
- export default ProcessManager;
@@ -1,100 +0,0 @@
1
- import { expect } from 'chai';
2
- import * as sinon from 'sinon';
3
- import { existsSync } from "fs";
4
-
5
- import routes, { basePaths, examplePaths, examplePages } from "./routes";
6
-
7
- describe('routes/base', () => {
8
-
9
- afterEach(() => {
10
- sinon.restore();
11
- });
12
-
13
- it('can find each of the base files it serves', () => {
14
- Object.values(basePaths).forEach((fwd: string) => {
15
- try {
16
- expect(existsSync(fwd)).to.be.true;
17
- } catch (e) {
18
- throw new Error(`Unable to resolve path ${fwd}`);
19
- }
20
- });
21
- });
22
-
23
- it('can find each of the example files it serves', () => {
24
- Object.values(examplePaths).forEach((fwd: string) => {
25
- try {
26
- expect(existsSync(fwd)).to.be.true;
27
- } catch (e) {
28
- throw new Error(`Unable to resolve path ${fwd}`);
29
- }
30
- });
31
- });
32
-
33
- it('can find each of the example page files it serves', () => {
34
- Object.values(examplePages).forEach((fwd: string) => {
35
- try {
36
- expect(existsSync(fwd)).to.be.true;
37
- } catch (e) {
38
- throw new Error(`Unable to resolve path ${fwd}`);
39
- }
40
- });
41
- });
42
-
43
- it('adds base routes', () => {
44
- const app = {
45
- get: sinon.spy()
46
- };
47
- routes.setup(app, false);
48
- sinon.assert.callCount(
49
- app.get,
50
- Object.keys(basePaths).length
51
- );
52
- });
53
-
54
- it('adds base and example routes', () => {
55
- const app = {
56
- get: sinon.spy()
57
- };
58
- routes.setup(app, true);
59
- sinon.assert.callCount(
60
- app.get,
61
- Object.keys(basePaths).length
62
- + Object.keys(examplePaths).length
63
- + Object.keys(examplePages).length
64
- );
65
- });
66
-
67
- it('handles calls to base routes as expected', () => {
68
- let routeHandlers = {};
69
- let app = {
70
- get: (path, route) => {
71
- routeHandlers[path] = route;
72
- }
73
- };
74
- routes.setup(app, true);
75
-
76
- function verifyPathRoutes(pathMap) {
77
- Object.keys(pathMap).forEach((path) => {
78
- const res = {
79
- setHeader: sinon.spy(),
80
- sendFile: sinon.spy()
81
- };
82
- expect(pathMap[path].endsWith('.ejs')).to.be.false;
83
- routeHandlers[path]({url: path}, res);
84
- sinon.assert.called(res.setHeader);
85
- sinon.assert.calledWith(res.sendFile, pathMap[path]);
86
- });
87
- }
88
- verifyPathRoutes(basePaths);
89
- verifyPathRoutes(examplePaths);
90
-
91
- Object.keys(examplePages).forEach((path) => {
92
- const res = {
93
- render: sinon.spy()
94
- };
95
- expect(examplePages[path].endsWith('.ejs')).to.be.true;
96
- routeHandlers[path]({url: path}, res);
97
- sinon.assert.called(res.render);
98
- });
99
- });
100
- });
package/src/routes.ts DELETED
@@ -1,93 +0,0 @@
1
- import path from 'path';
2
- import config from "./config";
3
- import debug from 'debug';
4
-
5
- const debug_scope = process.env.DEBUG || '',
6
- logger = debug('sockethub:routes'),
7
- address = config.get('public:protocol') + '://' +
8
- config.get('public:host') + ':' +
9
- config.get('public:port') +
10
- config.get('public:path');
11
-
12
- export const basePaths = {
13
- '/sockethub-client.js': path.resolve(`${__dirname}/../dist/sockethub-client.js`),
14
- '/sockethub-client.min.js': path.resolve(`${__dirname}/../dist/sockethub-client.min.js`),
15
- '/sockethub-client.js.map': path.resolve(`${__dirname}/../dist/sockethub-client.js.map`),
16
- '/socket.io.js': path.resolve(`${__dirname}/../node_modules/socket.io/client-dist/socket.io.js`),
17
- '/socket.io.js.map': path.resolve(
18
- `${__dirname}/../node_modules/socket.io/client-dist/socket.io.js.map`),
19
- };
20
-
21
- export const examplePaths = {
22
- '/jquery.js': path.resolve(`${__dirname}/../node_modules/jquery/dist/jquery.min.js`),
23
- '/jquery.min.map': path.resolve(`${__dirname}/../node_modules/jquery/dist/jquery.min.map`),
24
- '/examples/shared.js': path.resolve(`${__dirname}/../views/examples/shared.js`)
25
- };
26
-
27
- export const examplePages = {
28
- '/': path.resolve(`${__dirname}/../views/index.ejs`),
29
- '/examples/dummy': path.resolve(`${__dirname}/../views/examples/dummy.ejs`),
30
- '/examples/feeds': path.resolve(`${__dirname}/../views/examples/feeds.ejs`),
31
- '/examples/irc': path.resolve(`${__dirname}/../views/examples/irc.ejs`),
32
- '/examples/xmpp': path.resolve(`${__dirname}/../views/examples/xmpp.ejs`)
33
- };
34
-
35
-
36
- function prepFileRoutes(pathMap) {
37
- let _routes = [];
38
- Object.keys(pathMap).forEach((key) => {
39
- _routes.push({
40
- meta: {
41
- method: 'GET',
42
- path: key
43
- },
44
- route: (req, res) => {
45
- logger(`serving resource ${req.url}`);
46
- res.setHeader('Access-Control-Allow-Origin', '*');
47
- res.sendFile(pathMap[req.url]);
48
- }
49
- });
50
- });
51
- return _routes;
52
- }
53
- const baseRoutes = prepFileRoutes(basePaths);
54
- const exampleRoutes = prepFileRoutes(examplePaths);
55
-
56
-
57
- Object.keys(examplePages).forEach((key) => {
58
- exampleRoutes.push({
59
- meta: {
60
- method: 'GET',
61
- path: key
62
- },
63
- route: (req, res) => {
64
- logger(`serving page ${req.url}`);
65
- res.render(examplePages[req.url], {
66
- debug_scope: debug_scope,
67
- address: address,
68
- });
69
- }
70
- });
71
- });
72
-
73
- function addRoute(app) {
74
- return (route) => {
75
- app[route.meta.method.toLowerCase()](
76
- route.meta.path,
77
- route.route
78
- );
79
- };
80
- }
81
-
82
- /**
83
- * Setup
84
- */
85
- const routes = {
86
- setup: function (app: any, examplesEnabled: boolean = config.get('examples:enabled')) {
87
- baseRoutes.forEach(addRoute(app));
88
- if (examplesEnabled) {
89
- exampleRoutes.forEach(addRoute(app));
90
- }
91
- }
92
- };
93
- export default routes;
package/src/serve.ts DELETED
@@ -1,79 +0,0 @@
1
- import debug from 'debug';
2
- import bodyParser from 'body-parser';
3
- import express from 'express';
4
- import * as HTTP from 'http';
5
- import { Server } from 'socket.io';
6
-
7
- import config from './config';
8
- import routes from './routes';
9
-
10
- const log = debug('sockethub:services:http');
11
-
12
- /**
13
- * Handles the initialization and access of service objects.
14
- *
15
- * - HTTP Server
16
- * - Express (service resources and example routes)
17
- * - Socket.io (bidirectional websocket communication)
18
- */
19
- class Serve {
20
- io: Server;
21
- http: HTTP.Server;
22
-
23
- /**
24
- * Starts the services needed for sockethub to operate. After this command completes,
25
- * the `http` and `io` class properties will be set.
26
- */
27
- start() {
28
- // initialize express and socket.io objects
29
- const app = Serve.initExpress();
30
- this.http = new HTTP.Server(app);
31
- this.io = new Server(this.http, {
32
- path: config.get('service:path'),
33
- cors: {
34
- origin: "*",
35
- methods: [ "GET", "POST" ]
36
- }
37
- });
38
- routes.setup(app);
39
- this.startListener();
40
- }
41
-
42
- private startListener() {
43
- this.http.listen(config.get('service:port'), config.get('service:host'), () => {
44
- log(`sockethub listening on ` +
45
- `http://${config.get('service:host')}:${config.get('service:port')}`);
46
- });
47
- };
48
-
49
- private static initExpress() {
50
- let app = express();
51
- // templating engines
52
- app.set('view engine', 'ejs');
53
- // use bodyParser
54
- app.use(bodyParser.urlencoded({extended: true}));
55
- app.use(bodyParser.json());
56
- return app;
57
- }
58
- }
59
-
60
- const serve = new Serve();
61
-
62
- export interface SocketInstance {
63
- id: string;
64
- emit: Function;
65
- }
66
-
67
- export async function getSocket(sessionId: string): Promise<SocketInstance> {
68
- const sockets: Array<SocketInstance> = await serve.io.fetchSockets();
69
- return new Promise((resolve, reject) => {
70
- for (let socket of sockets) {
71
- if (sessionId === socket.id) {
72
- return resolve(socket);
73
- }
74
- }
75
- return reject(`unable to find socket for sessionId ${sessionId}`);
76
- });
77
- }
78
-
79
- export default serve;