@derivesome/sockets 1.0.2 → 1.0.4
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/.package.json.~undo-tree~ +4 -2
- package/dist/cjs/server/plugin.d.ts +14 -1
- package/dist/cjs/server/plugin.d.ts.map +1 -1
- package/dist/cjs/server/plugin.js +38 -0
- package/dist/cjs/server/plugin.js.map +1 -1
- package/dist/esm/server/plugin.d.ts +14 -1
- package/dist/esm/server/plugin.d.ts.map +1 -1
- package/dist/esm/server/plugin.js +36 -1
- package/dist/esm/server/plugin.js.map +1 -1
- package/package.json +2 -2
- package/package.json~ +1 -1
- package/src/server/.plugin.ts.~undo-tree~ +207 -2
- package/src/server/plugin.ts +54 -1
- package/src/server/plugin.ts~ +54 -1
package/src/server/plugin.ts
CHANGED
|
@@ -1,8 +1,61 @@
|
|
|
1
1
|
import { Subscription } from "@derivesome/core";
|
|
2
2
|
import { ServerEvent } from "./events";
|
|
3
|
+
import { InferPattern, PatLoose, pattern } from "p-lens";
|
|
4
|
+
import { serial } from "binencode";
|
|
5
|
+
|
|
6
|
+
export type PluginWatcher<P extends PatLoose> = {
|
|
7
|
+
pattern: P;
|
|
8
|
+
handler: (data: InferPattern<P>) => void | Promise<void>;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type CreatePluginConfig = {
|
|
12
|
+
name: string;
|
|
13
|
+
handler?: Subscription<ServerEvent>;
|
|
14
|
+
watchers?: PluginWatcher<any>[];
|
|
15
|
+
onDestroy?: (() => void | Promise<void>) | undefined | null;
|
|
16
|
+
};
|
|
3
17
|
|
|
4
18
|
export type Plugin = {
|
|
5
19
|
name: string;
|
|
6
20
|
handler: Subscription<ServerEvent>;
|
|
7
|
-
onDestroy?: () => void | Promise<void
|
|
21
|
+
onDestroy?: (() => void | Promise<void>) | undefined | null;
|
|
8
22
|
};
|
|
23
|
+
|
|
24
|
+
export function createWatcher<P extends PatLoose>(
|
|
25
|
+
pat: P,
|
|
26
|
+
handler: (data: InferPattern<P>) => void | Promise<void>,
|
|
27
|
+
): PluginWatcher<P> {
|
|
28
|
+
return {
|
|
29
|
+
pattern: pat,
|
|
30
|
+
handler,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function createPlugin(cfg: CreatePluginConfig): Plugin {
|
|
35
|
+
return {
|
|
36
|
+
name: cfg.name,
|
|
37
|
+
onDestroy: cfg.onDestroy,
|
|
38
|
+
handler: async (event) => {
|
|
39
|
+
try {
|
|
40
|
+
const cfgHandler = cfg.handler;
|
|
41
|
+
if (cfgHandler) {
|
|
42
|
+
await cfgHandler(event);
|
|
43
|
+
}
|
|
44
|
+
const watchers = cfg.watchers || [];
|
|
45
|
+
if (watchers.length <= 0) return;
|
|
46
|
+
|
|
47
|
+
if (event.type === "client-message") {
|
|
48
|
+
const decoded = serial.toJS(event.message);
|
|
49
|
+
|
|
50
|
+
for (const watcher of watchers) {
|
|
51
|
+
if (pattern.is.matching(decoded, watcher.pattern as any)) {
|
|
52
|
+
await watcher.handler(decoded);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
} catch (e) {
|
|
57
|
+
console.error(e);
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
package/src/server/plugin.ts~
CHANGED
|
@@ -1,8 +1,61 @@
|
|
|
1
1
|
import { Subscription } from "@derivesome/core";
|
|
2
2
|
import { ServerEvent } from "./events";
|
|
3
|
+
import { InferPattern, PatLoose, pattern } from "p-lens";
|
|
4
|
+
import { serial } from "binencode";
|
|
5
|
+
|
|
6
|
+
export type PluginWatcher<P extends PatLoose> = {
|
|
7
|
+
pattern: P;
|
|
8
|
+
handler: (data: InferPattern<P>) => void | Promise<void>;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type CreatePluginConfig = {
|
|
12
|
+
name: string;
|
|
13
|
+
handler?: Subscription<ServerEvent>;
|
|
14
|
+
watchers?: PluginWatcher<any>[];
|
|
15
|
+
onDestroy?: (() => void | Promise<void>) | undefined | null;
|
|
16
|
+
};
|
|
3
17
|
|
|
4
18
|
export type Plugin = {
|
|
5
19
|
name: string;
|
|
6
20
|
handler: Subscription<ServerEvent>;
|
|
7
|
-
onDestroy?: () => void | Promise<void
|
|
21
|
+
onDestroy?: (() => void | Promise<void>) | undefined | null;
|
|
8
22
|
};
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
export function createWatcher<P extends PatLoose>(pat: P, handler: (data: InferPattern<P>) => void | Promise<void>): PluginWatcher<P> {
|
|
26
|
+
return {
|
|
27
|
+
pattern: pat,
|
|
28
|
+
handler
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function createPlugin(
|
|
33
|
+
cfg: CreatePluginConfig,
|
|
34
|
+
): Plugin {
|
|
35
|
+
return {
|
|
36
|
+
name: cfg.name,
|
|
37
|
+
onDestroy: cfg.onDestroy,
|
|
38
|
+
handler: async (event) => {
|
|
39
|
+
try {
|
|
40
|
+
const cfgHandler = cfg.handler;
|
|
41
|
+
if (cfgHandler) {
|
|
42
|
+
await cfgHandler(event);
|
|
43
|
+
}
|
|
44
|
+
const watchers = cfg.watchers || [];
|
|
45
|
+
if (watchers.length <= 0) return;
|
|
46
|
+
|
|
47
|
+
if (event.type === "client-message") {
|
|
48
|
+
const decoded = serial.toJS(event.message);
|
|
49
|
+
|
|
50
|
+
for (const watcher of watchers) {
|
|
51
|
+
if (pattern.is.matching(decoded, watcher.pattern as any)) {
|
|
52
|
+
await watcher.handler(decoded);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
} catch (e) {
|
|
57
|
+
console.error(e);
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|