@sockethub/server 5.0.0-alpha.3 → 5.0.0-alpha.6
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 +54 -60
- package/bin/sockethub +4 -3
- package/package.json +42 -60
- package/res/socket.io.js +4908 -0
- package/res/sockethub-client.js +602 -0
- package/res/sockethub-client.min.js +19 -0
- package/sockethub.config.example.json +2 -3
- package/src/bootstrap/init.d.ts +20 -7
- package/src/bootstrap/init.test.ts +211 -0
- package/src/bootstrap/init.ts +152 -75
- package/src/bootstrap/load-platforms.ts +151 -0
- package/src/config.test.ts +27 -22
- package/src/config.ts +82 -78
- package/src/defaults.json +24 -16
- package/src/index.ts +67 -27
- package/src/janitor.test.ts +211 -0
- package/src/janitor.ts +145 -77
- package/src/listener.ts +151 -57
- package/src/middleware/create-activity-object.test.ts +28 -8
- package/src/middleware/create-activity-object.ts +17 -8
- package/src/middleware/expand-activity-stream.test.data.ts +332 -346
- package/src/middleware/expand-activity-stream.test.ts +65 -66
- package/src/middleware/expand-activity-stream.ts +29 -19
- package/src/middleware/store-credentials.test.ts +74 -62
- package/src/middleware/store-credentials.ts +15 -15
- package/src/middleware/validate.test.data.ts +240 -242
- package/src/middleware/validate.test.ts +39 -78
- package/src/middleware/validate.ts +63 -39
- package/src/middleware.test.ts +168 -138
- package/src/middleware.ts +62 -43
- package/src/platform-instance.test.ts +507 -213
- package/src/platform-instance.ts +337 -219
- package/src/platform.test.ts +375 -0
- package/src/platform.ts +306 -139
- package/src/process-manager.ts +75 -51
- package/src/routes.test.ts +43 -89
- package/src/routes.ts +40 -77
- package/src/sentry.test.ts +106 -0
- package/src/sentry.ts +19 -0
- package/src/sockethub.ts +186 -153
- package/src/util.ts +5 -0
- package/coverage/tmp/coverage-93126-1649152190997-0.json +0 -1
- package/dist/bootstrap/init.d.ts +0 -18
- package/dist/bootstrap/init.js +0 -63
- package/dist/bootstrap/init.js.map +0 -1
- package/dist/bootstrap/platforms.js +0 -75
- package/dist/common.d.ts +0 -3
- package/dist/common.js +0 -20
- package/dist/common.js.map +0 -1
- package/dist/config.d.ts +0 -6
- package/dist/config.js +0 -102
- package/dist/config.js.map +0 -1
- package/dist/crypto.d.ts +0 -10
- package/dist/crypto.js +0 -38
- package/dist/crypto.js.map +0 -1
- package/dist/defaults.json +0 -28
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -25
- package/dist/index.js.map +0 -1
- package/dist/janitor.d.ts +0 -15
- package/dist/janitor.js +0 -89
- package/dist/janitor.js.map +0 -1
- package/dist/listener.d.ts +0 -28
- package/dist/listener.js +0 -91
- package/dist/listener.js.map +0 -1
- package/dist/middleware/create-activity-object.d.ts +0 -6
- 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.d.ts +0 -2
- 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.d.ts +0 -480
- 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.d.ts +0 -3
- package/dist/middleware/store-credentials.js +0 -19
- package/dist/middleware/store-credentials.js.map +0 -1
- package/dist/middleware/validate.d.ts +0 -2
- package/dist/middleware/validate.js +0 -58
- package/dist/middleware/validate.js.map +0 -1
- package/dist/middleware/validate.test.data.d.ts +0 -532
- package/dist/middleware/validate.test.data.js +0 -263
- package/dist/middleware/validate.test.data.js.map +0 -1
- package/dist/middleware.d.ts +0 -10
- package/dist/middleware.js +0 -54
- package/dist/middleware.js.map +0 -1
- package/dist/platform-instance.d.ts +0 -77
- package/dist/platform-instance.js +0 -211
- package/dist/platform-instance.js.map +0 -1
- package/dist/platform.d.ts +0 -6
- package/dist/platform.js +0 -187
- package/dist/platform.js.map +0 -1
- package/dist/process-manager.d.ts +0 -11
- package/dist/process-manager.js +0 -78
- package/dist/process-manager.js.map +0 -1
- package/dist/routes.d.ts +0 -13
- package/dist/routes.js +0 -83
- package/dist/routes.js.map +0 -1
- package/dist/sockethub.d.ts +0 -39
- package/dist/sockethub.js +0 -119
- package/dist/sockethub.js.map +0 -1
- package/dist/store.d.ts +0 -5
- package/dist/store.js +0 -17
- package/dist/store.js.map +0 -1
- package/src/bootstrap/platforms.js +0 -75
- package/src/common.test.ts +0 -54
- package/src/common.ts +0 -14
- package/src/config.d.ts +0 -2
- package/src/crypto.d.ts +0 -5
- package/src/crypto.test.ts +0 -41
- package/src/crypto.ts +0 -41
- package/src/janitor.d.ts +0 -8
- package/src/middleware/validate.d.ts +0 -1
- package/src/middleware.d.ts +0 -21
- package/src/sockethub.d.ts +0 -1
- package/src/store.test.ts +0 -28
- package/src/store.ts +0 -17
- package/test/init-suite.js +0 -41
- package/test/queue.functional.test.js +0 -0
- package/test/sockethub-suite.js +0 -25
- package/tsconfig.json +0 -18
- 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/src/listener.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
1
|
+
import { existsSync, writeFileSync } from "node:fs";
|
|
2
|
+
import * as HTTP from "node:http";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import bodyParser from "body-parser";
|
|
6
|
+
import debug from "debug";
|
|
7
|
+
import express from "express";
|
|
8
|
+
import rateLimit from "express-rate-limit";
|
|
9
|
+
import { Server } from "socket.io";
|
|
6
10
|
|
|
7
|
-
import config from
|
|
8
|
-
import routes from
|
|
11
|
+
import config from "./config.js";
|
|
12
|
+
import routes from "./routes.js";
|
|
13
|
+
const require = createRequire(import.meta.url);
|
|
9
14
|
|
|
10
|
-
const log = debug(
|
|
15
|
+
const log = debug("sockethub:server:listener");
|
|
11
16
|
|
|
12
17
|
/**
|
|
13
18
|
* Handles the initialization and access of Sockethub resources.
|
|
@@ -17,63 +22,152 @@ const log = debug('sockethub:server:listener');
|
|
|
17
22
|
* - Socket.io (bidirectional websocket communication)
|
|
18
23
|
*/
|
|
19
24
|
class Listener {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
25
|
+
io: Server;
|
|
26
|
+
http: HTTP.Server;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Starts the services needed for Sockethub to operate. After this command completes,
|
|
30
|
+
* the `http` and `io` class properties will be set.
|
|
31
|
+
*/
|
|
32
|
+
start() {
|
|
33
|
+
// initialize express and socket.io objects
|
|
34
|
+
const app = Listener.initExpress();
|
|
35
|
+
this.http = new HTTP.Server(app);
|
|
36
|
+
this.io = new Server(this.http, {
|
|
37
|
+
path: config.get("sockethub:path") as string,
|
|
38
|
+
cors: {
|
|
39
|
+
origin: "*",
|
|
40
|
+
methods: ["GET", "POST"],
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
routes.setup(app);
|
|
45
|
+
|
|
46
|
+
if (config.get("examples")) {
|
|
47
|
+
this.addExamplesRoutes(app);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
this.startHttp();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Resolves the path to the examples static files from @sockethub/examples package.
|
|
55
|
+
* Returns null if the package is not installed.
|
|
56
|
+
*/
|
|
57
|
+
private resolveExamplesPath(): string | null {
|
|
58
|
+
try {
|
|
59
|
+
const examplesPkgPath = require.resolve(
|
|
60
|
+
"@sockethub/examples/package.json",
|
|
61
|
+
);
|
|
62
|
+
const examplesDir = path.join(
|
|
63
|
+
path.dirname(examplesPkgPath),
|
|
64
|
+
"build",
|
|
65
|
+
);
|
|
66
|
+
if (existsSync(examplesDir)) {
|
|
67
|
+
log(
|
|
68
|
+
`examples resolved from @sockethub/examples: ${examplesDir}`,
|
|
69
|
+
);
|
|
70
|
+
return examplesDir;
|
|
71
|
+
}
|
|
72
|
+
log(
|
|
73
|
+
`@sockethub/examples found but build directory missing: ${examplesDir}`,
|
|
74
|
+
);
|
|
75
|
+
return null;
|
|
76
|
+
} catch {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
private addExamplesRoutes(app) {
|
|
82
|
+
const examplesPath = this.resolveExamplesPath();
|
|
83
|
+
|
|
84
|
+
if (!examplesPath) {
|
|
85
|
+
console.error(
|
|
86
|
+
"\n❌ Error: --examples flag requires @sockethub/examples package\n\n" +
|
|
87
|
+
"The examples package is not installed. To use the examples feature, install it:\n\n" +
|
|
88
|
+
" bun add @sockethub/examples\n\n" +
|
|
89
|
+
"Or run sockethub without the --examples flag.\n",
|
|
90
|
+
);
|
|
91
|
+
process.exit(1);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Set up rate limiter to prevent DoS attacks on file system access
|
|
95
|
+
const limiter = rateLimit({
|
|
96
|
+
windowMs: 1 * 60 * 1000, // 1 minute
|
|
97
|
+
max: 60, // max 60 requests per windowMs
|
|
98
|
+
standardHeaders: true,
|
|
99
|
+
legacyHeaders: false,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// Write runtime config for the examples app
|
|
103
|
+
writeFileSync(
|
|
104
|
+
path.join(examplesPath, "config.json"),
|
|
105
|
+
JSON.stringify({
|
|
106
|
+
sockethub: config.get("sockethub"),
|
|
107
|
+
public: config.get("public"),
|
|
108
|
+
}),
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
app.use(express.static(examplesPath));
|
|
112
|
+
|
|
113
|
+
const examplesIndex = path.join(examplesPath, "index.html");
|
|
114
|
+
app.get("*", limiter, (req, res) => {
|
|
115
|
+
res.sendFile(examplesIndex);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
log(
|
|
119
|
+
`examples served at http://${config.get("sockethub:host")}:${config.get(
|
|
120
|
+
"sockethub:port",
|
|
121
|
+
)}`,
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private startHttp() {
|
|
126
|
+
this.http.listen(
|
|
127
|
+
config.get("sockethub:port"),
|
|
128
|
+
config.get("sockethub:host") as number,
|
|
129
|
+
() => {
|
|
130
|
+
log(
|
|
131
|
+
`sockethub listening on ws://${config.get("sockethub:host")}:${config.get(
|
|
132
|
+
"sockethub:port",
|
|
133
|
+
)}`,
|
|
134
|
+
);
|
|
135
|
+
},
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private static initExpress() {
|
|
140
|
+
const app = express();
|
|
141
|
+
// templating engines
|
|
142
|
+
app.set("view engine", "ejs");
|
|
143
|
+
// use bodyParser
|
|
144
|
+
app.use(bodyParser.urlencoded({ extended: true }));
|
|
145
|
+
app.use(bodyParser.json());
|
|
146
|
+
return app;
|
|
147
|
+
}
|
|
58
148
|
}
|
|
59
149
|
|
|
60
150
|
const listener = new Listener();
|
|
61
151
|
|
|
152
|
+
interface EmitFunction {
|
|
153
|
+
(type: string, data: unknown);
|
|
154
|
+
}
|
|
155
|
+
|
|
62
156
|
export interface SocketInstance {
|
|
63
|
-
|
|
64
|
-
|
|
157
|
+
id: string;
|
|
158
|
+
emit: EmitFunction;
|
|
65
159
|
}
|
|
66
160
|
|
|
67
161
|
export async function getSocket(sessionId: string): Promise<SocketInstance> {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
162
|
+
const sockets: Array<SocketInstance> = await listener.io.fetchSockets();
|
|
163
|
+
return new Promise((resolve, reject) => {
|
|
164
|
+
for (const socket of sockets) {
|
|
165
|
+
if (sessionId === socket.id) {
|
|
166
|
+
return resolve(socket);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return reject(`unable to find socket for sessionId ${sessionId}`);
|
|
170
|
+
});
|
|
77
171
|
}
|
|
78
172
|
|
|
79
173
|
export default listener;
|
|
@@ -1,10 +1,30 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
import createActivityObject from "./create-activity-object.js";
|
|
2
3
|
|
|
3
|
-
describe(
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
describe("Middleware: createActivityObject", () => {
|
|
5
|
+
it("Calls activity.Object.create and fails with invalid property", async () => {
|
|
6
|
+
expect(() => {
|
|
7
|
+
createActivityObject({ foo: "bar" }, (o) => {
|
|
8
|
+
expect(o).not.toEqual({ foo: "bar" });
|
|
9
|
+
});
|
|
10
|
+
}).toThrow("ActivityStreams validation failed: the \"object\" property requires an 'id' property. Example: { id: \"user@example.com\", type: \"person\" }");
|
|
8
11
|
});
|
|
9
|
-
|
|
10
|
-
|
|
12
|
+
it("Calls activity.Object.create with incoming data", async () => {
|
|
13
|
+
createActivityObject(
|
|
14
|
+
{
|
|
15
|
+
id: "test",
|
|
16
|
+
context: "foo",
|
|
17
|
+
type: "bar",
|
|
18
|
+
content: "some text",
|
|
19
|
+
},
|
|
20
|
+
(o) => {
|
|
21
|
+
expect(o).toEqual({
|
|
22
|
+
id: "test",
|
|
23
|
+
context: "foo",
|
|
24
|
+
type: "bar",
|
|
25
|
+
content: "some text",
|
|
26
|
+
});
|
|
27
|
+
},
|
|
28
|
+
);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -1,13 +1,22 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
1
|
+
import { ASFactory } from "@sockethub/activity-streams";
|
|
2
|
+
import type { ActivityObject } from "@sockethub/schemas";
|
|
3
|
+
|
|
4
|
+
import config from "../config.js";
|
|
5
|
+
import type { MiddlewareChainInterface } from "../middleware.js";
|
|
6
|
+
|
|
7
|
+
const activity = ASFactory(
|
|
8
|
+
config.get("packageConfig:@sockethub/activity-streams"),
|
|
9
|
+
);
|
|
4
10
|
|
|
5
11
|
/**
|
|
6
|
-
* A simple middleware wrapper for the activity-streams Object.create method.
|
|
12
|
+
* A simple middleware wrapper for the activity-streams `Object.create` method.
|
|
7
13
|
* @param obj
|
|
8
14
|
* @param done
|
|
9
15
|
*/
|
|
10
|
-
export default function createActivityObject(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
16
|
+
export default function createActivityObject(
|
|
17
|
+
obj: ActivityObject,
|
|
18
|
+
done: MiddlewareChainInterface,
|
|
19
|
+
) {
|
|
20
|
+
activity.Object.create(obj);
|
|
21
|
+
done(obj);
|
|
22
|
+
}
|