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
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
|
-
}
|
package/src/process-manager.ts
DELETED
|
@@ -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;
|
package/src/routes.test.ts
DELETED
|
@@ -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;
|