@sockethub/client 5.0.0-alpha.4 → 5.0.0-alpha.5

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.
@@ -1,14 +0,0 @@
1
- export interface ActivityObjectManager {
2
- create(obj: unknown): unknown;
3
- delete(id: string): boolean;
4
- list(): Array<string>;
5
- get(id: string, expand?: boolean): unknown;
6
- }
7
- export interface ASManager {
8
- Stream(meta: unknown): unknown;
9
- Object: ActivityObjectManager;
10
- emit(event: any, obj: any): void;
11
- on(event: any, func: any): void;
12
- once(event: any, func: any): void;
13
- off(event: any, funcName: any): void;
14
- }
@@ -1 +0,0 @@
1
- {"version":3,"file":"sockethub-client.js","sourceRoot":"/","sources":["sockethub-client.ts"],"names":[],"mappings":";;;;;AAAA,iDAA8C;AAC9C,mFAAoD;AA0BpD,MAAM,eAAe;IAanB,YAAY,MAAM;QAXV,WAAM,GAAiB;YAC7B,aAAa,EAAE,IAAI,GAAG,EAAE;YACxB,iBAAiB,EAAE,IAAI,GAAG,EAAE;YAC5B,SAAS,EAAE,IAAI,GAAG,EAAE;YACpB,MAAM,EAAE,IAAI,GAAG,EAAE;SAClB,CAAC;QAGK,WAAM,GAAG,KAAK,CAAC;QACf,UAAK,GAAG,IAAI,CAAC;QAGlB,IAAI,CAAE,MAAM,EAAE;YAAE,MAAM,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAC;SAAE;QACnF,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QACtB,IAAI,CAAC,eAAe,GAAG,IAAA,0BAAS,EAAC,EAAC,WAAW,EAAE,CAAC,aAAa,CAAC,EAAC,CAAC,CAAC;QAEjE,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,mBAAmB,EAAE,CAAC;QACzC,IAAI,CAAC,wBAAwB,EAAE,CAAC;QAEhC,IAAI,CAAC,eAAe,CAAC,EAAE,CAAC,wBAAwB,EAAE,CAAC,GAAG,EAAE,EAAE;YACxD,MAAM,CAAC,I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a;IACb,MAAM,CAAC,eAAe,GAAG,eAAe,CAAC;CAC1C"}
@@ -1,24 +0,0 @@
1
- /*!
2
- * EventEmitter2
3
- * https://github.com/hij1nx/EventEmitter2
4
- *
5
- * Copyright (c) 2013 hij1nx
6
- * Licensed under the MIT license.
7
- */
8
-
9
- /*!
10
- * activity-streams
11
- * https://github.com/silverbucket/activity-streams
12
- *
13
- * Developed and Maintained by:
14
- * Nick Jennings <nick@silverbucket.net>
15
- *
16
- * activity-streams is released under the MIT (see LICENSE).
17
- *
18
- * You don't have to do anything special to choose one license or the other
19
- * and you don't have to notify anyone which license you are using.
20
- * Please see the corresponding license file for details of these licenses.
21
- * You are free to use, modify and distribute this software, but all copyright
22
- * information must remain.
23
- *
24
- */
@@ -1,237 +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
- '@sockethub/activity-streams': (config: any) => {}
13
- });
14
- expect(() => {
15
- // @ts-ignore
16
- const junk = new SockethubClient();
17
- }).to.throw("SockethubClient requires a socket.io instance");
18
- });
19
- });
20
-
21
- describe("SockethubClient", () => {
22
- let asinstance: any, socket: any, sc: any, sandbox: any;
23
-
24
- beforeEach(() => {
25
- sandbox = sandbox = sinon.createSandbox();
26
- socket = new EventEmitter2();
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
- asinstance = new EventEmitter2();
31
- sandbox.spy(asinstance, 'on');
32
- sandbox.spy(asinstance, 'emit');
33
- asinstance.Stream = sandbox.stub();
34
- asinstance.Object = {
35
- create: sandbox.stub(),
36
- };
37
- const SockethubClient = proxyquire('./sockethub-client', {
38
- '@sockethub/activity-streams': (config: any) => {
39
- return asinstance;
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(asinstance).to.be.eql(sc.ActivityStreams);
55
- expect(typeof asinstance.Stream).to.equal('function');
56
- expect(typeof sc.ActivityStreams.Object.create).to.equal('function');
57
- });
58
-
59
- it("contains the socket property", () => {
60
- expect(sc.socket instanceof EventEmitter2).to.be.true;
61
- // the object we passed in should not be the publically available one
62
- expect(sc.socket.__instance).to.not.equal('socketio');
63
- expect(sc.debug).to.be.true;
64
- expect(sc.online).to.be.false;
65
- });
66
-
67
- it("registers listeners for ActivityStream events", () => {
68
- expect(asinstance.on.callCount).to.equal(1);
69
- expect(asinstance.on.calledWithMatch('activity-object-create')).to.be.true;
70
- });
71
-
72
- it("registers a listeners for socket events", () => {
73
- expect(socket.on.callCount).to.equal(5);
74
- expect(socket.on.calledWithMatch('activity-object')).to.be.true;
75
- expect(socket.on.calledWithMatch('connect')).to.be.true;
76
- expect(socket.on.calledWithMatch('connect_error')).to.be.true;
77
- expect(socket.on.calledWithMatch('disconnect')).to.be.true;
78
- expect(socket.on.calledWithMatch('message')).to.be.true;
79
- });
80
-
81
- describe("event handling", () => {
82
- it("activity-object", (done) => {
83
- socket.emit('activity-object', {foo:"bar"});
84
- setTimeout(() => {
85
- sandbox.assert.calledWith(asinstance.Object.create, {foo:"bar"});
86
- done();
87
- }, 0);
88
- });
89
-
90
- it("activity-object-create", (done) => {
91
- asinstance.emit('activity-object-create', {foo:"bar"});
92
- setTimeout(() => {
93
- expect(socket.emit.callCount).to.equal(1);
94
- expect(socket.emit.calledWithMatch('activity-object', {foo:"bar"})).to.be.true;
95
- done();
96
- }, 0);
97
- });
98
-
99
- it("connect", (done) => {
100
- expect(sc.online).to.be.false;
101
- sc.socket.on("connect", () => {
102
- expect(sc.online).to.be.true;
103
- expect(sc.socket._emit.callCount).to.equal(1);
104
- expect(sc.socket._emit.calledWithMatch('connect'));
105
- done();
106
- });
107
- socket.emit("connect");
108
- });
109
-
110
- it("disconnect", (done) => {
111
- sc.online = true;
112
- sc.socket.on("disconnect", () => {
113
- expect(sc.online).to.be.false;
114
- expect(sc.socket._emit.callCount).to.equal(1);
115
- expect(sc.socket._emit.calledWithMatch('disconnect'));
116
- done();
117
- });
118
- socket.emit("disconnect");
119
- });
120
-
121
- it("connect_error", (done) => {
122
- sc.socket.on("connect_error", () => {
123
- expect(sc.socket._emit.callCount).to.equal(1);
124
- expect(sc.socket._emit.calledWithMatch('connect_error'));
125
- done();
126
- });
127
- socket.emit("connect_error");
128
- });
129
-
130
- it("message", (done) => {
131
- sc.socket.on("message", () => {
132
- expect(sc.socket._emit.callCount).to.equal(1);
133
- expect(sc.socket._emit.calledWithMatch('message'));
134
- done();
135
- });
136
- socket.emit("message");
137
- });
138
- });
139
-
140
- describe("event emitting", () => {
141
- it("message (no actor)", () => {
142
- sc.online = true;
143
- const callback = () => {};
144
- expect(() => {
145
- sc.socket.emit("message", {foo:"bar"}, callback);
146
- }).to.throw("actor property not present");
147
- });
148
-
149
- it("message", (done) => {
150
- sc.online = true;
151
- const callback = () => {};
152
- socket.once("message", (data: any, cb: any) => {
153
- expect(data).to.be.eql({actor: "bar", type: "bar"});
154
- expect(cb).to.be.eql(callback);
155
- done();
156
- });
157
- sc.socket.emit("message", {actor:"bar", type: "bar"}, callback);
158
- });
159
-
160
- it("message (join)", (done) => {
161
- sc.online = true;
162
- const callback = () => {};
163
- socket.once("message", (data: any, cb: any) => {
164
- expect(data).to.be.eql({actor: "bar", type: "join"});
165
- expect(cb).to.be.eql(callback);
166
- done();
167
- });
168
- sc.socket.emit("message", {actor:"bar", type: "join"}, callback);
169
- });
170
-
171
- it("message (leave)", (done) => {
172
- sc.online = true;
173
- const callback = () => {};
174
- socket.once("message", (data: any, cb: any) => {
175
- expect(data).to.be.eql({actor: "bar", type: "leave"});
176
- expect(cb).to.be.eql(callback);
177
- done();
178
- });
179
- sc.socket.emit("message", {actor:"bar", type: "leave"}, callback);
180
- });
181
-
182
- it("message (connect)", (done) => {
183
- sc.online = true;
184
- const callback = () => {};
185
- socket.once("message", (data: any, cb: any) => {
186
- expect(data).to.be.eql({actor: "bar", type: "connect"});
187
- expect(cb).to.be.eql(callback);
188
- done();
189
- });
190
- sc.socket.emit("message", {actor:"bar", type: "connect"}, callback);
191
- });
192
-
193
- it("message (disconnect)", (done) => {
194
- sc.online = true;
195
- const callback = () => {};
196
- socket.once("message", (data: any, cb: any) => {
197
- expect(data).to.be.eql({actor: "bar", type: "disconnect"});
198
- expect(cb).to.be.eql(callback);
199
- done();
200
- });
201
- sc.socket.emit("message", {actor:"bar", type: "disconnect"}, callback);
202
- });
203
-
204
- it("message (offline)", (done) => {
205
- sc.online = false;
206
- const callback = () => {};
207
- socket.once("message", (data: any, cb: any) => {
208
- expect(data).to.be.eql({actor: "bar"});
209
- expect(cb).to.be.eql(callback);
210
- done();
211
- });
212
- sc.socket.emit("message", {actor:"bar"}, callback);
213
- });
214
-
215
- it("activity-object", (done) => {
216
- sc.online = true;
217
- const callback = () => {};
218
- socket.once("activity-object", (data: any, cb: any) => {
219
- expect(data).to.be.eql({actor: "bar"});
220
- expect(cb).to.be.eql(callback);
221
- done();
222
- });
223
- sc.socket.emit("activity-object", {actor:"bar"}, callback);
224
- });
225
-
226
- it("credentials", (done) => {
227
- sc.online = true;
228
- const callback = () => {};
229
- socket.once("credentials", (data: any, cb: any) => {
230
- expect(data).to.be.eql({actor: "bar"});
231
- expect(cb).to.be.eql(callback);
232
- done();
233
- });
234
- sc.socket.emit("credentials", {actor:"bar"}, callback);
235
- });
236
- });
237
- });
@@ -1,176 +0,0 @@
1
- import { EventEmitter2 } from 'eventemitter2';
2
- import ASFactory from '@sockethub/activity-streams';
3
- import {IActivityObject, IActivityStream} from "@sockethub/schemas";
4
-
5
- export interface ActivityObjectManager {
6
- create(obj: unknown): unknown;
7
- delete(id: string): boolean;
8
- list(): Array<string>,
9
- get(id: string, expand?: boolean): unknown;
10
- }
11
-
12
- export interface ASManager {
13
- Stream(meta: unknown): unknown,
14
- Object: ActivityObjectManager,
15
- emit(event, obj): void;
16
- on(event, func): void;
17
- once(event, func): void;
18
- off(event, funcName): void;
19
- }
20
-
21
- interface EventMapping {
22
- credentials: Map<string, IActivityStream>;
23
- 'activity-object': Map<string, IActivityObject>;
24
- connect: Map<string, IActivityStream>;
25
- join: Map<string, IActivityStream>;
26
- }
27
-
28
- class SockethubClient {
29
- private _socket;
30
- private events: EventMapping = {
31
- 'credentials': new Map(),
32
- 'activity-object': new Map(),
33
- 'connect': new Map(),
34
- 'join': new Map()
35
- };
36
- public socket;
37
- public ActivityStreams: ASManager;
38
- public online = false;
39
- public debug = true;
40
-
41
- constructor(socket) {
42
- if (! socket) { throw new Error('SockethubClient requires a socket.io instance'); }
43
- this._socket = socket;
44
- this.ActivityStreams = ASFactory({specialObjs: ['credentials']});
45
-
46
- this.socket = this.createPublicEmitter();
47
- this.registerSocketIOHandlers();
48
-
49
- this.ActivityStreams.on('activity-object-create', (obj) => {
50
- socket.emit('activity-object', obj, (err) => {
51
- if (err) { console.error('failed to create activity-object ', err); }
52
- else { this.eventActivityObject(obj); }
53
- });
54
- });
55
-
56
- socket.on('activity-object', (obj) => {
57
- this.ActivityStreams.Object.create(obj);
58
- });
59
- }
60
-
61
- private createPublicEmitter(): EventEmitter2 {
62
- const socket = new EventEmitter2({
63
- wildcard: true,
64
- verboseMemoryLeak: false
65
- });
66
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
67
- // @ts-ignore
68
- socket._emit = socket.emit;
69
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
70
- // @ts-ignore
71
- socket.emit = (event, content, callback): void => {
72
- if (event === 'credentials') {
73
- this.eventCredentials(content);
74
- } else if (event === 'activity-object') {
75
- this.eventActivityObject(content);
76
- } else if (event === 'message') {
77
- this.eventMessage(content);
78
- }
79
- this._socket.emit(event, content, callback);
80
- };
81
- return socket;
82
- }
83
-
84
- private eventActivityObject(content: IActivityObject) {
85
- if (content.id) {
86
- this.events['activity-object'].set(content.id, content);
87
- }
88
- }
89
-
90
- private eventCredentials(content: IActivityStream) {
91
- if ((content.object) && (content.object.type === 'credentials')) {
92
- const key: string = content.actor.id || content.actor as unknown as string;
93
- this.events['credentials'].set(key, content);
94
- }
95
- }
96
-
97
- private eventMessage(content: IActivityObject) {
98
- if (! this.online) { return; }
99
- // either store or delete the specified content onto the storedJoins map,
100
- // for reply once we're back online.
101
- const key = SockethubClient.getKey(content as IActivityStream);
102
- if (content.type === 'join' || content.type === 'connect') {
103
- this.events[content.type].set(key, content as IActivityStream);
104
- } else if (content.type === 'leave') {
105
- this.events['join'].delete(key);
106
- } else if (content.type === 'disconnect') {
107
- this.events['connect'].delete(key);
108
- }
109
- }
110
-
111
- private static getKey(content: IActivityStream) {
112
- const actor = content.actor?.id || content.actor;
113
- if (! actor) {
114
- throw new Error("actor property not present for message type: " + content?.type);
115
- }
116
- const target = content.target ? content.target.id || content.target : '';
117
- return actor + '-' + target;
118
- }
119
-
120
- private log(msg: string, obj?: unknown) {
121
- if (this.debug) {
122
- // eslint-disable-next-line security-node/detect-crlf
123
- console.log(msg, obj);
124
- }
125
- }
126
-
127
- private registerSocketIOHandlers() {
128
- // middleware for events which don't deal in AS objects
129
- const callHandler = (event: string) => {
130
- return (obj, cb) => {
131
- if (event === 'connect') {
132
- this.online = true;
133
- this.replay('activity-object', this.events['activity-object']);
134
- this.replay('credentials', this.events['credentials']);
135
- this.replay('message', this.events['connect']);
136
- this.replay('message', this.events['join']);
137
- } else if (event === 'disconnect') {
138
- this.online = false;
139
- }
140
- this.socket._emit(event, obj, cb);
141
- };
142
- };
143
-
144
- // register for events that give us information on connection status
145
- this._socket.on('connect', callHandler('connect'));
146
- this._socket.on('connect_error', callHandler('connect_error'));
147
- this._socket.on('disconnect', callHandler('disconnect'));
148
-
149
- // use as a middleware to receive incoming Sockethub messages and unpack them
150
- // using the ActivityStreams library before passing them along to the app.
151
- this._socket.on('message', (obj, cb) => {
152
- this.socket._emit('message', this.ActivityStreams.Stream(obj), cb);
153
- });
154
- }
155
-
156
- private replay(name: string, asMap: Map<string, unknown>) {
157
- asMap.forEach((obj) => {
158
- this.log(`replaying ${name}`, obj);
159
- this._socket.emit(name, obj);
160
- });
161
- }
162
- }
163
-
164
- if (typeof module === 'object' && module.exports) {
165
- module.exports = SockethubClient;
166
- }
167
-
168
- if (typeof exports === 'object') {
169
- exports = SockethubClient; // lgtm [js/useless-assignment-to-local]
170
- }
171
-
172
- if (typeof window === 'object') {
173
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
174
- // @ts-ignore
175
- window.SockethubClient = SockethubClient;
176
- }
package/tsconfig.json DELETED
@@ -1,20 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.base.json",
3
- "compilerOptions": {
4
- "allowJs": false,
5
- "outDir": "dist",
6
- "rootDir": "src",
7
- "baseUrl": "/",
8
- "sourceRoot": "/",
9
- "mapRoot": "/",
10
- "lib": [
11
- "dom"
12
- ]
13
- },
14
- "include": [
15
- "src"
16
- ],
17
- "exclude": [
18
- "**/*.test.ts"
19
- ]
20
- }
@@ -1,14 +0,0 @@
1
- const path = require('path');
2
-
3
- module.exports = {
4
- entry: './dist/sockethub-client.js',
5
- mode: "production",
6
- optimization: {
7
- minimize: true
8
- },
9
- output: {
10
- filename: './dist/sockethub-client.min.js',
11
- path: path.resolve(__dirname, './'),
12
- libraryTarget: 'umd'
13
- }
14
- };
@@ -1,14 +0,0 @@
1
- const path = require('path');
2
-
3
- module.exports = {
4
- entry: './dist/sockethub-client.js',
5
- mode: "production",
6
- optimization: {
7
- minimize: false
8
- },
9
- output: {
10
- filename: './dist/sockethub-client.js',
11
- path: path.resolve(__dirname, './'),
12
- libraryTarget: 'umd'
13
- }
14
- };