sockethub 5.0.0-alpha.2 → 5.0.0-alpha.3
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.
- package/bin/sockethub +2 -29
- package/build.js +21 -0
- package/package.json +15 -65
- package/{dist/defaults.json → sockethub.config.json} +8 -7
- package/tsconfig.json +6 -68
- package/README.md +0 -98
- package/config.json.example +0 -32
- package/dist/bootstrap/init.js +0 -74
- package/dist/bootstrap/init.js.map +0 -1
- package/dist/bootstrap/platforms.js +0 -103
- package/dist/common.js +0 -20
- package/dist/common.js.map +0 -1
- package/dist/config.js +0 -60
- package/dist/config.js.map +0 -1
- package/dist/crypto.js +0 -38
- package/dist/crypto.js.map +0 -1
- package/dist/janitor.js +0 -98
- package/dist/janitor.js.map +0 -1
- package/dist/middleware/create-activity-object.js +0 -19
- package/dist/middleware/create-activity-object.js.map +0 -1
- package/dist/middleware/expand-activity-stream.js +0 -33
- package/dist/middleware/expand-activity-stream.js.map +0 -1
- package/dist/middleware/expand-activity-stream.test.data.js +0 -360
- package/dist/middleware/expand-activity-stream.test.data.js.map +0 -1
- package/dist/middleware/store-credentials.js +0 -19
- package/dist/middleware/store-credentials.js.map +0 -1
- package/dist/middleware/validate.js +0 -77
- package/dist/middleware/validate.js.map +0 -1
- package/dist/middleware/validate.test.data.js +0 -321
- package/dist/middleware/validate.test.data.js.map +0 -1
- package/dist/middleware.js +0 -54
- package/dist/middleware.js.map +0 -1
- package/dist/platform-instance.js +0 -226
- package/dist/platform-instance.js.map +0 -1
- package/dist/platform.js +0 -186
- package/dist/platform.js.map +0 -1
- package/dist/process-manager.js +0 -78
- package/dist/process-manager.js.map +0 -1
- package/dist/routes.js +0 -84
- package/dist/routes.js.map +0 -1
- package/dist/serve.js +0 -102
- package/dist/serve.js.map +0 -1
- package/dist/sockethub-client.js +0 -2604
- package/dist/sockethub-client.js.map +0 -1
- package/dist/sockethub-client.min.js +0 -2
- package/dist/sockethub-client.min.js.LICENSE.txt +0 -24
- package/dist/sockethub.js +0 -130
- package/dist/sockethub.js.map +0 -1
- package/dist/store.js +0 -17
- package/dist/store.js.map +0 -1
- package/src/bootstrap/init.d.ts +0 -8
- package/src/bootstrap/init.ts +0 -91
- package/src/bootstrap/platforms.js +0 -103
- package/src/common.test.ts +0 -54
- package/src/common.ts +0 -14
- package/src/config.d.ts +0 -2
- package/src/config.test.ts +0 -28
- package/src/config.ts +0 -72
- package/src/crypto.d.ts +0 -5
- package/src/crypto.test.ts +0 -41
- package/src/crypto.ts +0 -41
- package/src/defaults.json +0 -32
- package/src/janitor.d.ts +0 -8
- package/src/janitor.ts +0 -89
- package/src/middleware/create-activity-object.test.ts +0 -10
- package/src/middleware/create-activity-object.ts +0 -13
- package/src/middleware/expand-activity-stream.test.data.ts +0 -365
- package/src/middleware/expand-activity-stream.test.ts +0 -78
- package/src/middleware/expand-activity-stream.ts +0 -27
- package/src/middleware/store-credentials.test.ts +0 -72
- package/src/middleware/store-credentials.ts +0 -16
- package/src/middleware/validate.d.ts +0 -1
- package/src/middleware/validate.test.data.ts +0 -320
- package/src/middleware/validate.test.ts +0 -47
- package/src/middleware/validate.ts +0 -49
- package/src/middleware.d.ts +0 -21
- package/src/middleware.test.ts +0 -148
- package/src/middleware.ts +0 -52
- package/src/platform-instance.test.ts +0 -237
- package/src/platform-instance.ts +0 -236
- package/src/platform.ts +0 -189
- package/src/process-manager.ts +0 -64
- package/src/routes.test.ts +0 -100
- package/src/routes.ts +0 -93
- package/src/serve.ts +0 -79
- package/src/sockethub-client.test.ts +0 -235
- package/src/sockethub-client.ts +0 -164
- package/src/sockethub.d.ts +0 -1
- package/src/sockethub.ts +0 -184
- package/src/store.test.ts +0 -26
- package/src/store.ts +0 -17
- package/test/init-suite.js +0 -41
- package/test/sockethub-suite.js +0 -25
- package/views/examples/dummy.ejs +0 -93
- package/views/examples/feeds.ejs +0 -90
- package/views/examples/irc.ejs +0 -239
- package/views/examples/shared.js +0 -72
- package/views/examples/xmpp.ejs +0 -191
- package/views/index.ejs +0 -17
- package/webpack.minified.config.js +0 -14
- package/webpack.normal.config.js +0 -14
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
import proxyquire from 'proxyquire';
|
|
2
|
-
import { expect } from 'chai';
|
|
3
|
-
import * as sinon from 'sinon';
|
|
4
|
-
import { EventEmitter2 } from 'eventemitter2';
|
|
5
|
-
|
|
6
|
-
proxyquire.noPreserveCache();
|
|
7
|
-
proxyquire.noCallThru();
|
|
8
|
-
|
|
9
|
-
describe("SockethubClient bad initialization", () => {
|
|
10
|
-
it("no socket.io instance", () => {
|
|
11
|
-
const SockethubClient = proxyquire('./sockethub-client', {
|
|
12
|
-
'activity-streams': (config: any) => {}
|
|
13
|
-
});
|
|
14
|
-
expect(() => {
|
|
15
|
-
const junk = new SockethubClient();
|
|
16
|
-
}).to.throw("SockethubClient requires a socket.io instance");
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
describe("SockethubClient", () => {
|
|
21
|
-
let ASManager, socket, sc, sandbox;
|
|
22
|
-
|
|
23
|
-
beforeEach(() => {
|
|
24
|
-
sandbox = sandbox = sinon.createSandbox();
|
|
25
|
-
socket = new EventEmitter2();
|
|
26
|
-
// @ts-ignore
|
|
27
|
-
socket.__instance = 'socketio'; // used to uniquely identify the object we're passing in
|
|
28
|
-
sandbox.spy(socket, 'on');
|
|
29
|
-
sandbox.spy(socket, 'emit');
|
|
30
|
-
ASManager = new EventEmitter2();
|
|
31
|
-
sandbox.spy(ASManager, 'on');
|
|
32
|
-
sandbox.spy(ASManager, 'emit');
|
|
33
|
-
ASManager.Stream = sandbox.stub();
|
|
34
|
-
ASManager.Object = {
|
|
35
|
-
create: sandbox.stub()
|
|
36
|
-
};
|
|
37
|
-
const SockethubClient = proxyquire('./sockethub-client', {
|
|
38
|
-
'activity-streams': (config: any) => {
|
|
39
|
-
return ASManager;
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
sc = new SockethubClient(socket);
|
|
43
|
-
sandbox.spy(sc.socket, 'on');
|
|
44
|
-
sandbox.spy(sc.socket, '_on');
|
|
45
|
-
sandbox.spy(sc.socket, 'emit');
|
|
46
|
-
sandbox.spy(sc.socket, '_emit');
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
afterEach(() => {
|
|
50
|
-
sinon.restore();
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it("contains the ActivityStreams property", () => {
|
|
54
|
-
expect(sc.ActivityStreams).to.be.eql(ASManager);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it("contains the socket property", () => {
|
|
58
|
-
expect(sc.socket instanceof EventEmitter2).to.be.true;
|
|
59
|
-
// the object we passed in should not be the publically available one
|
|
60
|
-
expect(sc.socket.__instance).to.not.equal('socketio');
|
|
61
|
-
expect(sc.debug).to.be.true;
|
|
62
|
-
expect(sc.online).to.be.false;
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it("registers listeners for ActivityStream events", () => {
|
|
66
|
-
expect(ASManager.on.callCount).to.equal(1);
|
|
67
|
-
expect(ASManager.on.calledWithMatch('activity-object-create')).to.be.true;
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it("registers a listeners for socket events", () => {
|
|
71
|
-
expect(socket.on.callCount).to.equal(5);
|
|
72
|
-
expect(socket.on.calledWithMatch('activity-object')).to.be.true;
|
|
73
|
-
expect(socket.on.calledWithMatch('connect')).to.be.true;
|
|
74
|
-
expect(socket.on.calledWithMatch('connect_error')).to.be.true;
|
|
75
|
-
expect(socket.on.calledWithMatch('disconnect')).to.be.true;
|
|
76
|
-
expect(socket.on.calledWithMatch('message')).to.be.true;
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
describe("event handling", () => {
|
|
80
|
-
it("activity-object", (done) => {
|
|
81
|
-
socket.emit('activity-object', {foo:"bar"});
|
|
82
|
-
setTimeout(() => {
|
|
83
|
-
sandbox.assert.calledWith(ASManager.Object.create, {foo:"bar"});
|
|
84
|
-
done();
|
|
85
|
-
}, 0);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it("activity-object-create", (done) => {
|
|
89
|
-
ASManager.emit('activity-object-create', {foo:"bar"});
|
|
90
|
-
setTimeout(() => {
|
|
91
|
-
expect(socket.emit.callCount).to.equal(1);
|
|
92
|
-
expect(socket.emit.calledWithMatch('activity-object', {foo:"bar"})).to.be.true;
|
|
93
|
-
done();
|
|
94
|
-
}, 0);
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
it("connect", (done) => {
|
|
98
|
-
expect(sc.online).to.be.false;
|
|
99
|
-
sc.socket.on("connect", () => {
|
|
100
|
-
expect(sc.online).to.be.true;
|
|
101
|
-
expect(sc.socket._emit.callCount).to.equal(1);
|
|
102
|
-
expect(sc.socket._emit.calledWithMatch('connect'));
|
|
103
|
-
done();
|
|
104
|
-
});
|
|
105
|
-
socket.emit("connect");
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
it("disconnect", (done) => {
|
|
109
|
-
sc.online = true;
|
|
110
|
-
sc.socket.on("disconnect", () => {
|
|
111
|
-
expect(sc.online).to.be.false;
|
|
112
|
-
expect(sc.socket._emit.callCount).to.equal(1);
|
|
113
|
-
expect(sc.socket._emit.calledWithMatch('disconnect'));
|
|
114
|
-
done();
|
|
115
|
-
});
|
|
116
|
-
socket.emit("disconnect");
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
it("connect_error", (done) => {
|
|
120
|
-
sc.socket.on("connect_error", () => {
|
|
121
|
-
expect(sc.socket._emit.callCount).to.equal(1);
|
|
122
|
-
expect(sc.socket._emit.calledWithMatch('connect_error'));
|
|
123
|
-
done();
|
|
124
|
-
});
|
|
125
|
-
socket.emit("connect_error");
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it("message", (done) => {
|
|
129
|
-
sc.socket.on("message", () => {
|
|
130
|
-
expect(sc.socket._emit.callCount).to.equal(1);
|
|
131
|
-
expect(sc.socket._emit.calledWithMatch('message'));
|
|
132
|
-
done();
|
|
133
|
-
});
|
|
134
|
-
socket.emit("message");
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
describe("event emitting", () => {
|
|
139
|
-
it("message (no actor)", () => {
|
|
140
|
-
sc.online = true;
|
|
141
|
-
const callback = (res) => {};
|
|
142
|
-
expect(() => {
|
|
143
|
-
sc.socket.emit("message", {foo:"bar"}, callback);
|
|
144
|
-
}).to.throw("actor property not present");
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
it("message", (done) => {
|
|
148
|
-
sc.online = true;
|
|
149
|
-
const callback = (res) => {};
|
|
150
|
-
socket.once("message", (data, cb) => {
|
|
151
|
-
expect(data).to.be.eql({actor: "bar", type: "bar"});
|
|
152
|
-
expect(cb).to.be.eql(callback);
|
|
153
|
-
done();
|
|
154
|
-
});
|
|
155
|
-
sc.socket.emit("message", {actor:"bar", type: "bar"}, callback);
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
it("message (join)", (done) => {
|
|
159
|
-
sc.online = true;
|
|
160
|
-
const callback = (res) => {};
|
|
161
|
-
socket.once("message", (data, cb) => {
|
|
162
|
-
expect(data).to.be.eql({actor: "bar", type: "join"});
|
|
163
|
-
expect(cb).to.be.eql(callback);
|
|
164
|
-
done();
|
|
165
|
-
});
|
|
166
|
-
sc.socket.emit("message", {actor:"bar", type: "join"}, callback);
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
it("message (leave)", (done) => {
|
|
170
|
-
sc.online = true;
|
|
171
|
-
const callback = (res) => {};
|
|
172
|
-
socket.once("message", (data, cb) => {
|
|
173
|
-
expect(data).to.be.eql({actor: "bar", type: "leave"});
|
|
174
|
-
expect(cb).to.be.eql(callback);
|
|
175
|
-
done();
|
|
176
|
-
});
|
|
177
|
-
sc.socket.emit("message", {actor:"bar", type: "leave"}, callback);
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
it("message (connect)", (done) => {
|
|
181
|
-
sc.online = true;
|
|
182
|
-
const callback = (res) => {};
|
|
183
|
-
socket.once("message", (data, cb) => {
|
|
184
|
-
expect(data).to.be.eql({actor: "bar", type: "connect"});
|
|
185
|
-
expect(cb).to.be.eql(callback);
|
|
186
|
-
done();
|
|
187
|
-
});
|
|
188
|
-
sc.socket.emit("message", {actor:"bar", type: "connect"}, callback);
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
it("message (disconnect)", (done) => {
|
|
192
|
-
sc.online = true;
|
|
193
|
-
const callback = (res) => {};
|
|
194
|
-
socket.once("message", (data, cb) => {
|
|
195
|
-
expect(data).to.be.eql({actor: "bar", type: "disconnect"});
|
|
196
|
-
expect(cb).to.be.eql(callback);
|
|
197
|
-
done();
|
|
198
|
-
});
|
|
199
|
-
sc.socket.emit("message", {actor:"bar", type: "disconnect"}, callback);
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
it("message (offline)", (done) => {
|
|
203
|
-
sc.online = false;
|
|
204
|
-
const callback = (res) => {};
|
|
205
|
-
socket.once("message", (data, cb) => {
|
|
206
|
-
expect(data).to.be.eql({actor: "bar"});
|
|
207
|
-
expect(cb).to.be.eql(callback);
|
|
208
|
-
done();
|
|
209
|
-
});
|
|
210
|
-
sc.socket.emit("message", {actor:"bar"}, callback);
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
it("activity-object", (done) => {
|
|
214
|
-
sc.online = true;
|
|
215
|
-
const callback = (res) => {};
|
|
216
|
-
socket.once("activity-object", (data, cb) => {
|
|
217
|
-
expect(data).to.be.eql({actor: "bar"});
|
|
218
|
-
expect(cb).to.be.eql(callback);
|
|
219
|
-
done();
|
|
220
|
-
});
|
|
221
|
-
sc.socket.emit("activity-object", {actor:"bar"}, callback);
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
it("credentials", (done) => {
|
|
225
|
-
sc.online = true;
|
|
226
|
-
const callback = (res) => {};
|
|
227
|
-
socket.once("credentials", (data, cb) => {
|
|
228
|
-
expect(data).to.be.eql({actor: "bar"});
|
|
229
|
-
expect(cb).to.be.eql(callback);
|
|
230
|
-
done();
|
|
231
|
-
});
|
|
232
|
-
sc.socket.emit("credentials", {actor:"bar"}, callback);
|
|
233
|
-
});
|
|
234
|
-
});
|
|
235
|
-
});
|
package/src/sockethub-client.ts
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import { EventEmitter2 } from 'eventemitter2';
|
|
2
|
-
import ASFactory from 'activity-streams';
|
|
3
|
-
|
|
4
|
-
export interface ActivityObjectManager {
|
|
5
|
-
create(obj: any): any;
|
|
6
|
-
delete(id: string): boolean;
|
|
7
|
-
list(): Array<string>,
|
|
8
|
-
get(id: string, expand: boolean): any;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface ASManager {
|
|
12
|
-
Stream(meta: any): any,
|
|
13
|
-
Object: ActivityObjectManager,
|
|
14
|
-
on(event, func): void;
|
|
15
|
-
once(event, func): void;
|
|
16
|
-
off(event, funcName): void;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
class SockethubClient {
|
|
20
|
-
private _socket;
|
|
21
|
-
private events = {
|
|
22
|
-
'credentials': new Map(),
|
|
23
|
-
'activity-object': new Map(),
|
|
24
|
-
'connect': new Map(),
|
|
25
|
-
'join': new Map()
|
|
26
|
-
};
|
|
27
|
-
public socket;
|
|
28
|
-
public ActivityStreams: ASManager;
|
|
29
|
-
public online = false;
|
|
30
|
-
public debug = true;
|
|
31
|
-
|
|
32
|
-
constructor(socket) {
|
|
33
|
-
if (! socket) { throw new Error('SockethubClient requires a socket.io instance'); }
|
|
34
|
-
this._socket = socket;
|
|
35
|
-
// @ts-ignore
|
|
36
|
-
this.ActivityStreams = ASFactory({specialObjs: ['credentials']});
|
|
37
|
-
|
|
38
|
-
this.socket = this.createPublicEmitter();
|
|
39
|
-
this.registerSocketIOHandlers();
|
|
40
|
-
|
|
41
|
-
this.ActivityStreams.on('activity-object-create', (obj) => {
|
|
42
|
-
socket.emit('activity-object', obj, (err) => {
|
|
43
|
-
if (err) { console.error('failed to create activity-object ', err); }
|
|
44
|
-
else { this.eventActivityObject(obj); }
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
socket.on('activity-object', (obj) => {
|
|
49
|
-
this.ActivityStreams.Object.create(obj);
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
private createPublicEmitter(): EventEmitter2 {
|
|
54
|
-
let socket = new EventEmitter2({
|
|
55
|
-
wildcard: true,
|
|
56
|
-
verboseMemoryLeak: false
|
|
57
|
-
});
|
|
58
|
-
// @ts-ignore
|
|
59
|
-
socket._emit = socket.emit;
|
|
60
|
-
socket.emit = (event, content, callback): any => {
|
|
61
|
-
if (event === 'credentials') {
|
|
62
|
-
this.eventCredentials(content);
|
|
63
|
-
} else if (event === 'activity-object') {
|
|
64
|
-
this.eventActivityObject(content);
|
|
65
|
-
} else if (event === 'message') {
|
|
66
|
-
this.eventMessage(content);
|
|
67
|
-
}
|
|
68
|
-
this._socket.emit(event, content, callback);
|
|
69
|
-
};
|
|
70
|
-
return socket;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
private eventActivityObject(content: any) {
|
|
74
|
-
if (content.id) {
|
|
75
|
-
this.events['activity-object'].set(content.id, content);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
private eventCredentials(content: any) {
|
|
80
|
-
if ((content.object) && (content.object.type === 'credentials')) {
|
|
81
|
-
this.events['credentials'].set(content.actor.id || content.actor, content);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
private eventMessage(content: any) {
|
|
86
|
-
if (! this.online) { return; }
|
|
87
|
-
// either store or delete the specified content onto the storedJoins map,
|
|
88
|
-
// for reply once we're back online.
|
|
89
|
-
const key = SockethubClient.getKey(content);
|
|
90
|
-
if (content.type === 'join' || content.type === 'connect') {
|
|
91
|
-
this.events[content.type].set(key, content);
|
|
92
|
-
} else if (content.type === 'leave') {
|
|
93
|
-
this.events['join'].delete(key);
|
|
94
|
-
} else if (content.type === 'disconnect') {
|
|
95
|
-
this.events['connect'].delete(key);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
private static getKey(content: any) {
|
|
100
|
-
let actor = content.actor?.id || content.actor;
|
|
101
|
-
if (! actor) {
|
|
102
|
-
throw new Error("actor property not present for message type: " + content?.type);
|
|
103
|
-
}
|
|
104
|
-
let target = content.target ? content.target.id || content.target : '';
|
|
105
|
-
return actor + '-' + target;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
private log(msg: string, obj?: any) {
|
|
109
|
-
if (this.debug) {
|
|
110
|
-
// eslint-disable-next-line security-node/detect-crlf
|
|
111
|
-
console.log(msg, obj);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
private registerSocketIOHandlers() {
|
|
116
|
-
// middleware for events which don't deal in AS objects
|
|
117
|
-
const callHandler = (event: string) => {
|
|
118
|
-
return (obj, cb) => {
|
|
119
|
-
if (event === 'connect') {
|
|
120
|
-
this.online = true;
|
|
121
|
-
this.replay('activity-object', this.events['activity-object']);
|
|
122
|
-
this.replay('credentials', this.events['credentials']);
|
|
123
|
-
this.replay('message', this.events['connect']);
|
|
124
|
-
this.replay('message', this.events['join']);
|
|
125
|
-
} else if (event === 'disconnect') {
|
|
126
|
-
this.online = false;
|
|
127
|
-
}
|
|
128
|
-
this.socket._emit(event, obj, cb);
|
|
129
|
-
};
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
// register for events that give us information on connection status
|
|
133
|
-
this._socket.on('connect', callHandler('connect'));
|
|
134
|
-
this._socket.on('connect_error', callHandler('connect_error'));
|
|
135
|
-
this._socket.on('disconnect', callHandler('disconnect'));
|
|
136
|
-
|
|
137
|
-
// use as a middleware to receive incoming Sockethub messages and unpack them
|
|
138
|
-
// using the ActivityStreams library before passing them along to the app.
|
|
139
|
-
this._socket.on('message', (obj, cb) => {
|
|
140
|
-
this.socket._emit('message', this.ActivityStreams.Stream(obj), cb);
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
private replay(name: string, asMap: any) {
|
|
145
|
-
asMap.forEach((obj) => {
|
|
146
|
-
this.log(`replaying ${name}`, obj);
|
|
147
|
-
this._socket.emit(name, obj);
|
|
148
|
-
});
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
if (typeof module === 'object' && module.exports) {
|
|
153
|
-
module.exports = SockethubClient;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
if (typeof exports === 'object') {
|
|
157
|
-
exports = SockethubClient; // lgtm [js/useless-assignment-to-local]
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// @ts-ignore
|
|
161
|
-
if (typeof window === 'object') {
|
|
162
|
-
// @ts-ignore
|
|
163
|
-
window.SockethubClient = SockethubClient;
|
|
164
|
-
}
|
package/src/sockethub.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/src/sockethub.ts
DELETED
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
import debug from 'debug';
|
|
2
|
-
import { Socket } from "socket.io";
|
|
3
|
-
|
|
4
|
-
import crypto from './crypto';
|
|
5
|
-
import init from './bootstrap/init';
|
|
6
|
-
import middleware from './middleware';
|
|
7
|
-
import createActivityObject from "./middleware/create-activity-object";
|
|
8
|
-
import expandActivityStream from "./middleware/expand-activity-stream";
|
|
9
|
-
import storeCredentials from "./middleware/store-credentials";
|
|
10
|
-
import validate from "./middleware/validate";
|
|
11
|
-
import janitor from './janitor';
|
|
12
|
-
import serve from './serve';
|
|
13
|
-
import ProcessManager from "./process-manager";
|
|
14
|
-
import PlatformInstance, { platformInstances } from "./platform-instance";
|
|
15
|
-
import { getSessionStore } from "./store";
|
|
16
|
-
|
|
17
|
-
const log = debug('sockethub:core');
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
export interface JobDataDecrypted {
|
|
21
|
-
title?: string;
|
|
22
|
-
msg: ActivityStream;
|
|
23
|
-
sessionId: string;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export interface JobDataEncrypted {
|
|
27
|
-
title?: string;
|
|
28
|
-
msg: string;
|
|
29
|
-
sessionId: string;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface JobDecrypted {
|
|
33
|
-
data: JobDataDecrypted,
|
|
34
|
-
remove?: Function;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface JobEncrypted {
|
|
38
|
-
data: JobDataEncrypted,
|
|
39
|
-
remove?: Function;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface ActivityStream {
|
|
43
|
-
type: string;
|
|
44
|
-
context: string;
|
|
45
|
-
actor: {
|
|
46
|
-
id: string;
|
|
47
|
-
type?: string;
|
|
48
|
-
name?: string;
|
|
49
|
-
},
|
|
50
|
-
object?: {
|
|
51
|
-
type: string;
|
|
52
|
-
content?: any;
|
|
53
|
-
},
|
|
54
|
-
target?: {
|
|
55
|
-
type: string;
|
|
56
|
-
id: string;
|
|
57
|
-
name?: string;
|
|
58
|
-
},
|
|
59
|
-
error?: string;
|
|
60
|
-
sessionSecret?: string;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function attachError(err, msg) {
|
|
64
|
-
if (typeof msg !== 'object') {
|
|
65
|
-
msg = { context: 'error' };
|
|
66
|
-
}
|
|
67
|
-
msg.error = err.toString();
|
|
68
|
-
delete msg.sessionSecret;
|
|
69
|
-
return msg;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
class Sockethub {
|
|
73
|
-
private readonly parentId: string;
|
|
74
|
-
private readonly parentSecret1: string;
|
|
75
|
-
private readonly parentSecret2: string;
|
|
76
|
-
counter: number;
|
|
77
|
-
platforms: Map<string, object>;
|
|
78
|
-
status: boolean;
|
|
79
|
-
queue: any;
|
|
80
|
-
processManager: ProcessManager;
|
|
81
|
-
|
|
82
|
-
constructor() {
|
|
83
|
-
this.counter = 0;
|
|
84
|
-
this.platforms = init.platforms;
|
|
85
|
-
this.status = false;
|
|
86
|
-
this.parentId = crypto.randToken(16);
|
|
87
|
-
this.parentSecret1 = crypto.randToken(16);
|
|
88
|
-
this.parentSecret2 = crypto.randToken(16);
|
|
89
|
-
this.processManager = new ProcessManager(
|
|
90
|
-
this.parentId, this.parentSecret1, this.parentSecret2);
|
|
91
|
-
log('sockethub session id: ' + this.parentId);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* initialization of sockethub starts here
|
|
96
|
-
*/
|
|
97
|
-
boot() {
|
|
98
|
-
if (this.status) {
|
|
99
|
-
return log('Sockethub.boot() called more than once');
|
|
100
|
-
} else {
|
|
101
|
-
this.status = true;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
log('active platforms: ', [...init.platforms.keys()]);
|
|
105
|
-
janitor.clean(); // start cleanup cycle
|
|
106
|
-
serve.start(); // start external services
|
|
107
|
-
log('registering handlers');
|
|
108
|
-
serve.io.on('connection', this.handleIncomingConnection.bind(this));
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
async removeAllPlatformInstances() {
|
|
112
|
-
for (let platform of platformInstances) {
|
|
113
|
-
await platform[1].destroy();
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
private createJob(socketId: string, platformInstance: PlatformInstance, msg): JobDataEncrypted {
|
|
118
|
-
const title = `${msg.context}-${(msg.id) ? msg.id : this.counter++}`;
|
|
119
|
-
const job: JobDataEncrypted = {
|
|
120
|
-
title: title,
|
|
121
|
-
sessionId: socketId,
|
|
122
|
-
msg: crypto.encrypt(msg, this.parentSecret1 + this.parentSecret2)
|
|
123
|
-
};
|
|
124
|
-
return job;
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
private handleIncomingConnection(socket: Socket) {
|
|
128
|
-
const sessionLog = debug('sockethub:core:' + socket.id), // session-specific debug messages
|
|
129
|
-
sessionSecret = crypto.randToken(16),
|
|
130
|
-
// store instance is session-specific
|
|
131
|
-
store = getSessionStore(this.parentId, this.parentSecret1, socket.id, sessionSecret);
|
|
132
|
-
|
|
133
|
-
sessionLog(`socket.io connection`);
|
|
134
|
-
|
|
135
|
-
socket.on('disconnect', () => {
|
|
136
|
-
sessionLog('disconnect received from client');
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
socket.on('credentials',
|
|
140
|
-
middleware('credentials')
|
|
141
|
-
.use(expandActivityStream)
|
|
142
|
-
.use(validate('credentials', socket.id))
|
|
143
|
-
.use(storeCredentials(store, sessionLog))
|
|
144
|
-
.use((err, data, next) => {
|
|
145
|
-
// error handler
|
|
146
|
-
next(attachError(err, data));
|
|
147
|
-
}).use((data, next) => { next(); })
|
|
148
|
-
.done());
|
|
149
|
-
|
|
150
|
-
// when new activity objects are created on the client side, an event is
|
|
151
|
-
// fired and we receive a copy on the server side.
|
|
152
|
-
socket.on('activity-object',
|
|
153
|
-
middleware('activity-object')
|
|
154
|
-
.use(validate('activity-object', socket.id))
|
|
155
|
-
.use(createActivityObject)
|
|
156
|
-
.use((err, data, next) => {
|
|
157
|
-
next(attachError(err, data));
|
|
158
|
-
}).use((data, next) => { next(); })
|
|
159
|
-
.done());
|
|
160
|
-
|
|
161
|
-
socket.on('message',
|
|
162
|
-
middleware('message')
|
|
163
|
-
.use(expandActivityStream)
|
|
164
|
-
.use(validate('message', socket.id))
|
|
165
|
-
.use((msg, next) => {
|
|
166
|
-
// The platform thread must find the credentials on their own using the given
|
|
167
|
-
// sessionSecret, which indicates that this specific session (socket
|
|
168
|
-
// connection) has provided credentials.
|
|
169
|
-
msg.sessionSecret = sessionSecret;
|
|
170
|
-
next(msg);
|
|
171
|
-
}).use((err, data, next) => {
|
|
172
|
-
next(attachError(err, data));
|
|
173
|
-
}).use((msg, next) => {
|
|
174
|
-
const platformInstance = this.processManager.get(msg.context, msg.actor.id, socket.id);
|
|
175
|
-
sessionLog(`queued to channel ${platformInstance.id}`);
|
|
176
|
-
const job = this.createJob(socket.id, platformInstance, msg);
|
|
177
|
-
// job validated and queued, store socket.io callback for when job is completed
|
|
178
|
-
platformInstance.completedJobHandlers.set(job.title, next);
|
|
179
|
-
platformInstance.queue.add(job);
|
|
180
|
-
}).done());
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
export default Sockethub;
|
package/src/store.test.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import proxyquire from 'proxyquire';
|
|
2
|
-
import * as sinon from 'sinon';
|
|
3
|
-
|
|
4
|
-
proxyquire.noPreserveCache();
|
|
5
|
-
proxyquire.noCallThru();
|
|
6
|
-
|
|
7
|
-
const MockSecureStore = sinon.fake();
|
|
8
|
-
|
|
9
|
-
const StoreMod = proxyquire('./store', {
|
|
10
|
-
'secure-store-redis': MockSecureStore
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
const getSessionStore = StoreMod.getSessionStore;
|
|
14
|
-
|
|
15
|
-
describe('getSessionStore', () => {
|
|
16
|
-
it('returns a valid Store object', () => {
|
|
17
|
-
const store = getSessionStore('a parent id', 'a parent secret',
|
|
18
|
-
'a session id', 'a session secret');
|
|
19
|
-
sinon.assert.calledOnce(MockSecureStore);
|
|
20
|
-
sinon.assert.calledWith(MockSecureStore, {
|
|
21
|
-
namespace: 'sockethub:a parent id:session:a session id:store',
|
|
22
|
-
secret: 'a parent secreta session secret',
|
|
23
|
-
redis: { host: '127.0.0.1', port: 6379 }
|
|
24
|
-
});
|
|
25
|
-
});
|
|
26
|
-
});
|
package/src/store.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import SecureStore from 'secure-store-redis';
|
|
2
|
-
|
|
3
|
-
import config from "./config";
|
|
4
|
-
|
|
5
|
-
export interface ISecureStoreInstance {
|
|
6
|
-
save(id: string, obj: any, cb: Function);
|
|
7
|
-
get(id: string, cb: Function);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function getSessionStore(parentId: string, parentSecret: string,
|
|
11
|
-
sessionId: string, sessionSecret: string): ISecureStoreInstance {
|
|
12
|
-
return new SecureStore({
|
|
13
|
-
namespace: 'sockethub:' + parentId + ':session:' + sessionId + ':store',
|
|
14
|
-
secret: parentSecret + sessionSecret,
|
|
15
|
-
redis: config.get('redis')
|
|
16
|
-
});
|
|
17
|
-
}
|
package/test/init-suite.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
if (typeof define !== 'function') {
|
|
2
|
-
let define = require('amdefine')(module);
|
|
3
|
-
}
|
|
4
|
-
define(['require'], function (require) {
|
|
5
|
-
return [
|
|
6
|
-
{
|
|
7
|
-
desc: 'dist/bootstrap/init',
|
|
8
|
-
abortOnFail: true,
|
|
9
|
-
setup: function (env, test) {
|
|
10
|
-
env.init = require('../dist/bootstrap/init').default;
|
|
11
|
-
test.assertType(env.init, 'object');
|
|
12
|
-
},
|
|
13
|
-
tests: [
|
|
14
|
-
{
|
|
15
|
-
desc: 'platforms',
|
|
16
|
-
run: function (env, test) {
|
|
17
|
-
test.assertTypeAnd(env.init.platforms, 'object');
|
|
18
|
-
test.assert(env.init.platforms.has('irc'), true);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
]
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
desc: 'nconf should have defaults set',
|
|
25
|
-
setup: function (env, test) {
|
|
26
|
-
env.nconf = require('nconf');
|
|
27
|
-
test.assertType(env.nconf.get, 'function');
|
|
28
|
-
},
|
|
29
|
-
tests: [
|
|
30
|
-
{
|
|
31
|
-
desc: 'whitelist and blacklist',
|
|
32
|
-
run: function (env, test) {
|
|
33
|
-
console.log('whitelist: ', typeof require('nconf').get('platforms:whitelist'));
|
|
34
|
-
test.assertTypeAnd(require('nconf').get('platforms:whitelist'), 'array');
|
|
35
|
-
test.assertType(require('nconf').get('platforms:blacklist'), 'array');
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
]
|
|
39
|
-
}
|
|
40
|
-
];
|
|
41
|
-
});
|
package/test/sockethub-suite.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
if (typeof define !== 'function') {
|
|
2
|
-
let define = require('amdefine')(module);
|
|
3
|
-
}
|
|
4
|
-
define(['require', '../dist/sockethub'], function (require, Sockethub) {
|
|
5
|
-
return [
|
|
6
|
-
{
|
|
7
|
-
desc: 'src/sockethub',
|
|
8
|
-
abortOnFail: true,
|
|
9
|
-
beforeEach: function () {
|
|
10
|
-
this.env.sockethub = new Sockethub.default();
|
|
11
|
-
this.done();
|
|
12
|
-
},
|
|
13
|
-
tests: [
|
|
14
|
-
{
|
|
15
|
-
desc: 'boot',
|
|
16
|
-
run: function () {
|
|
17
|
-
this.env.sockethub.boot();
|
|
18
|
-
this.assertTypeAnd(this.env.sockethub.platforms, 'object');
|
|
19
|
-
this.assertType(this.env.sockethub.platforms.size, 'number');
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
]
|
|
23
|
-
}
|
|
24
|
-
];
|
|
25
|
-
});
|