@sockethub/client 5.0.0-alpha.3 → 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.
- package/README.md +127 -0
- package/dist/sockethub-client.js +554 -2552
- package/dist/sockethub-client.min.js +19 -2
- package/package.json +33 -40
- package/coverage/tmp/coverage-93114-1649152181263-0.json +0 -1
- package/dist/sockethub-client.d.ts +0 -14
- package/dist/sockethub-client.js.map +0 -1
- package/dist/sockethub-client.min.js.LICENSE.txt +0 -24
- package/src/sockethub-client.test.ts +0 -237
- package/src/sockethub-client.ts +0 -165
- package/tsconfig.json +0 -17
- package/webpack.minified.config.js +0 -14
- package/webpack.normal.config.js +0 -14
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export interface ActivityObjectManager {
|
|
2
|
-
create(obj: any): any;
|
|
3
|
-
delete(id: string): boolean;
|
|
4
|
-
list(): Array<string>;
|
|
5
|
-
get(id: string, expand: boolean): any;
|
|
6
|
-
}
|
|
7
|
-
export interface ASManager {
|
|
8
|
-
Stream(meta: any): any;
|
|
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;AAkBpD,MAAM,eAAe;IAanB,YAAY,MAAM;QAXV,WAAM,GAAG;YACf,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,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AO,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QAC/B,CAAC,CAAC,CAAC;IACL,CAAC;IAAA,CAAC;CACH;AAED,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,CAAC,OAAO,EAAE;IAChD,MAAM,CAAC,OAAO,GAAG,eAAe,CAAC;CAClC;AAED,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;IAC/B,OAAO,GAAG,eAAe,CAAC,CAAE,wCAAwC;CACrE;AAED,aAAa;AACb,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE;IAC9B,aAAa;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
|
-
});
|
package/src/sockethub-client.ts
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
import { EventEmitter2 } from 'eventemitter2';
|
|
2
|
-
import ASFactory from '@sockethub/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
|
-
emit(event, obj): void;
|
|
15
|
-
on(event, func): void;
|
|
16
|
-
once(event, func): void;
|
|
17
|
-
off(event, funcName): void;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
class SockethubClient {
|
|
21
|
-
private _socket;
|
|
22
|
-
private events = {
|
|
23
|
-
'credentials': new Map(),
|
|
24
|
-
'activity-object': new Map(),
|
|
25
|
-
'connect': new Map(),
|
|
26
|
-
'join': new Map()
|
|
27
|
-
};
|
|
28
|
-
public socket;
|
|
29
|
-
public ActivityStreams: ASManager;
|
|
30
|
-
public online = false;
|
|
31
|
-
public debug = true;
|
|
32
|
-
|
|
33
|
-
constructor(socket) {
|
|
34
|
-
if (! socket) { throw new Error('SockethubClient requires a socket.io instance'); }
|
|
35
|
-
this._socket = socket;
|
|
36
|
-
// @ts-ignore
|
|
37
|
-
this.ActivityStreams = ASFactory({specialObjs: ['credentials']});
|
|
38
|
-
|
|
39
|
-
this.socket = this.createPublicEmitter();
|
|
40
|
-
this.registerSocketIOHandlers();
|
|
41
|
-
|
|
42
|
-
this.ActivityStreams.on('activity-object-create', (obj) => {
|
|
43
|
-
socket.emit('activity-object', obj, (err) => {
|
|
44
|
-
if (err) { console.error('failed to create activity-object ', err); }
|
|
45
|
-
else { this.eventActivityObject(obj); }
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
socket.on('activity-object', (obj) => {
|
|
50
|
-
this.ActivityStreams.Object.create(obj);
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
private createPublicEmitter(): EventEmitter2 {
|
|
55
|
-
let socket = new EventEmitter2({
|
|
56
|
-
wildcard: true,
|
|
57
|
-
verboseMemoryLeak: false
|
|
58
|
-
});
|
|
59
|
-
// @ts-ignore
|
|
60
|
-
socket._emit = socket.emit;
|
|
61
|
-
socket.emit = (event, content, callback): any => {
|
|
62
|
-
if (event === 'credentials') {
|
|
63
|
-
this.eventCredentials(content);
|
|
64
|
-
} else if (event === 'activity-object') {
|
|
65
|
-
this.eventActivityObject(content);
|
|
66
|
-
} else if (event === 'message') {
|
|
67
|
-
this.eventMessage(content);
|
|
68
|
-
}
|
|
69
|
-
this._socket.emit(event, content, callback);
|
|
70
|
-
};
|
|
71
|
-
return socket;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
private eventActivityObject(content: any) {
|
|
75
|
-
if (content.id) {
|
|
76
|
-
this.events['activity-object'].set(content.id, content);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
private eventCredentials(content: any) {
|
|
81
|
-
if ((content.object) && (content.object.type === 'credentials')) {
|
|
82
|
-
this.events['credentials'].set(content.actor.id || content.actor, content);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
private eventMessage(content: any) {
|
|
87
|
-
if (! this.online) { return; }
|
|
88
|
-
// either store or delete the specified content onto the storedJoins map,
|
|
89
|
-
// for reply once we're back online.
|
|
90
|
-
const key = SockethubClient.getKey(content);
|
|
91
|
-
if (content.type === 'join' || content.type === 'connect') {
|
|
92
|
-
this.events[content.type].set(key, content);
|
|
93
|
-
} else if (content.type === 'leave') {
|
|
94
|
-
this.events['join'].delete(key);
|
|
95
|
-
} else if (content.type === 'disconnect') {
|
|
96
|
-
this.events['connect'].delete(key);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
private static getKey(content: any) {
|
|
101
|
-
let actor = content.actor?.id || content.actor;
|
|
102
|
-
if (! actor) {
|
|
103
|
-
throw new Error("actor property not present for message type: " + content?.type);
|
|
104
|
-
}
|
|
105
|
-
let target = content.target ? content.target.id || content.target : '';
|
|
106
|
-
return actor + '-' + target;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
private log(msg: string, obj?: any) {
|
|
110
|
-
if (this.debug) {
|
|
111
|
-
// eslint-disable-next-line security-node/detect-crlf
|
|
112
|
-
console.log(msg, obj);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
private registerSocketIOHandlers() {
|
|
117
|
-
// middleware for events which don't deal in AS objects
|
|
118
|
-
const callHandler = (event: string) => {
|
|
119
|
-
return (obj, cb) => {
|
|
120
|
-
if (event === 'connect') {
|
|
121
|
-
this.online = true;
|
|
122
|
-
this.replay('activity-object', this.events['activity-object']);
|
|
123
|
-
this.replay('credentials', this.events['credentials']);
|
|
124
|
-
this.replay('message', this.events['connect']);
|
|
125
|
-
this.replay('message', this.events['join']);
|
|
126
|
-
} else if (event === 'disconnect') {
|
|
127
|
-
this.online = false;
|
|
128
|
-
}
|
|
129
|
-
this.socket._emit(event, obj, cb);
|
|
130
|
-
};
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
// register for events that give us information on connection status
|
|
134
|
-
this._socket.on('connect', callHandler('connect'));
|
|
135
|
-
this._socket.on('connect_error', callHandler('connect_error'));
|
|
136
|
-
this._socket.on('disconnect', callHandler('disconnect'));
|
|
137
|
-
|
|
138
|
-
// use as a middleware to receive incoming Sockethub messages and unpack them
|
|
139
|
-
// using the ActivityStreams library before passing them along to the app.
|
|
140
|
-
this._socket.on('message', (obj, cb) => {
|
|
141
|
-
this.socket._emit('message', this.ActivityStreams.Stream(obj), cb);
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
private replay(name: string, asMap: any) {
|
|
146
|
-
asMap.forEach((obj) => {
|
|
147
|
-
this.log(`replaying ${name}`, obj);
|
|
148
|
-
this._socket.emit(name, obj);
|
|
149
|
-
});
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
if (typeof module === 'object' && module.exports) {
|
|
154
|
-
module.exports = SockethubClient;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
if (typeof exports === 'object') {
|
|
158
|
-
exports = SockethubClient; // lgtm [js/useless-assignment-to-local]
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// @ts-ignore
|
|
162
|
-
if (typeof window === 'object') {
|
|
163
|
-
// @ts-ignore
|
|
164
|
-
window.SockethubClient = SockethubClient;
|
|
165
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,17 +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
|
-
},
|
|
11
|
-
"include": [
|
|
12
|
-
"src"
|
|
13
|
-
],
|
|
14
|
-
"exclude": [
|
|
15
|
-
"**/*.test.ts"
|
|
16
|
-
]
|
|
17
|
-
}
|
|
@@ -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
|
-
};
|
package/webpack.normal.config.js
DELETED
|
@@ -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
|
-
};
|