@pluv/platform-pluv 0.30.0
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/.eslintrc.js +4 -0
- package/.turbo/turbo-build.log +18 -0
- package/CHANGELOG.md +14 -0
- package/LICENSE +21 -0
- package/dist/index.d.mts +48 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.js +144 -0
- package/dist/index.mjs +118 -0
- package/package.json +40 -0
- package/src/PluvIO.ts +111 -0
- package/src/PluvPlatform.ts +52 -0
- package/src/createIO.ts +7 -0
- package/src/index.ts +3 -0
- package/src/schemas.ts +18 -0
- package/tsconfig.json +8 -0
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
> @pluv/platform-pluv@0.30.0 build /home/runner/work/pluv/pluv/packages/platform-pluv
|
|
3
|
+
> tsup src/index.ts --format esm,cjs --dts
|
|
4
|
+
|
|
5
|
+
[34mCLI[39m Building entry: src/index.ts
|
|
6
|
+
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
|
+
[34mCLI[39m tsup v8.3.0
|
|
8
|
+
[34mCLI[39m Target: es6
|
|
9
|
+
[34mESM[39m Build start
|
|
10
|
+
[34mCJS[39m Build start
|
|
11
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m3.57 KB[39m
|
|
12
|
+
[32mESM[39m ⚡️ Build success in 61ms
|
|
13
|
+
[32mCJS[39m [1mdist/index.js [22m[32m4.75 KB[39m
|
|
14
|
+
[32mCJS[39m ⚡️ Build success in 59ms
|
|
15
|
+
[34mDTS[39m Build start
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 6690ms
|
|
17
|
+
[32mDTS[39m [1mdist/index.d.mts [22m[32m2.24 KB[39m
|
|
18
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m2.24 KB[39m
|
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Shoubhit Dash
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { AbstractPlatform, WebSocketRegistrationMode, AbstractWebSocket, ConvertWebSocketConfig, WebSocketSerializedState, AbstractPlatformConfig, PluvIOListeners, GetInitialStorageFn, JWTEncodeParams, BaseUser as BaseUser$1 } from '@pluv/io';
|
|
2
|
+
import * as hono_types from 'hono/types';
|
|
3
|
+
import * as hono from 'hono';
|
|
4
|
+
import { BaseUser, InputZodLike } from '@pluv/types';
|
|
5
|
+
|
|
6
|
+
declare class PluvPlatform extends AbstractPlatform {
|
|
7
|
+
_registrationMode: WebSocketRegistrationMode;
|
|
8
|
+
acceptWebSocket(webSocket: AbstractWebSocket): Promise<void>;
|
|
9
|
+
convertWebSocket(webSocket: any, config: ConvertWebSocketConfig): AbstractWebSocket;
|
|
10
|
+
getLastPing(webSocket: AbstractWebSocket): number | null;
|
|
11
|
+
getSerializedState(webSocket: any): WebSocketSerializedState | null;
|
|
12
|
+
getSessionId(webSocket: any): string | null;
|
|
13
|
+
getWebSockets(): readonly any[];
|
|
14
|
+
initialize(config: AbstractPlatformConfig<{}, {}>): this;
|
|
15
|
+
parseData(data: string | ArrayBuffer): Record<string, any>;
|
|
16
|
+
randomUUID(): string;
|
|
17
|
+
setSerializedState(webSocket: AbstractWebSocket, state: WebSocketSerializedState): void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface PluvAuthorize<TUser extends BaseUser> {
|
|
21
|
+
required: true;
|
|
22
|
+
secret: string;
|
|
23
|
+
user: InputZodLike<TUser>;
|
|
24
|
+
}
|
|
25
|
+
type PluvIOConfig<TUser extends BaseUser> = Partial<Pick<PluvIOListeners<PluvPlatform, PluvAuthorize<TUser>, {}, {}>, "onRoomDeleted">> & {
|
|
26
|
+
authorize: {
|
|
27
|
+
user: InputZodLike<TUser>;
|
|
28
|
+
};
|
|
29
|
+
basePath: string;
|
|
30
|
+
getInitialStorage?: GetInitialStorageFn<PluvPlatform>;
|
|
31
|
+
secretKey: string;
|
|
32
|
+
};
|
|
33
|
+
declare class PluvIO<TUser extends BaseUser> {
|
|
34
|
+
private readonly _authorize;
|
|
35
|
+
private readonly _basePath;
|
|
36
|
+
private readonly _getInitialStorage?;
|
|
37
|
+
private readonly _listeners;
|
|
38
|
+
private readonly _secretKey;
|
|
39
|
+
get fetch(): (request: Request, Env?: unknown, executionCtx?: hono.ExecutionContext) => Response | Promise<Response>;
|
|
40
|
+
get handler(): (req: Request, requestContext: hono_types.FetchEventLike) => Response | Promise<Response>;
|
|
41
|
+
private _webhooks;
|
|
42
|
+
constructor(options: PluvIOConfig<TUser>);
|
|
43
|
+
createToken(params: JWTEncodeParams<TUser, PluvPlatform>): Promise<string>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
declare const createIO: <TUser extends BaseUser$1>(config: PluvIOConfig<TUser>) => PluvIO<TUser>;
|
|
47
|
+
|
|
48
|
+
export { PluvIO, type PluvIOConfig, PluvPlatform, createIO };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { AbstractPlatform, WebSocketRegistrationMode, AbstractWebSocket, ConvertWebSocketConfig, WebSocketSerializedState, AbstractPlatformConfig, PluvIOListeners, GetInitialStorageFn, JWTEncodeParams, BaseUser as BaseUser$1 } from '@pluv/io';
|
|
2
|
+
import * as hono_types from 'hono/types';
|
|
3
|
+
import * as hono from 'hono';
|
|
4
|
+
import { BaseUser, InputZodLike } from '@pluv/types';
|
|
5
|
+
|
|
6
|
+
declare class PluvPlatform extends AbstractPlatform {
|
|
7
|
+
_registrationMode: WebSocketRegistrationMode;
|
|
8
|
+
acceptWebSocket(webSocket: AbstractWebSocket): Promise<void>;
|
|
9
|
+
convertWebSocket(webSocket: any, config: ConvertWebSocketConfig): AbstractWebSocket;
|
|
10
|
+
getLastPing(webSocket: AbstractWebSocket): number | null;
|
|
11
|
+
getSerializedState(webSocket: any): WebSocketSerializedState | null;
|
|
12
|
+
getSessionId(webSocket: any): string | null;
|
|
13
|
+
getWebSockets(): readonly any[];
|
|
14
|
+
initialize(config: AbstractPlatformConfig<{}, {}>): this;
|
|
15
|
+
parseData(data: string | ArrayBuffer): Record<string, any>;
|
|
16
|
+
randomUUID(): string;
|
|
17
|
+
setSerializedState(webSocket: AbstractWebSocket, state: WebSocketSerializedState): void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface PluvAuthorize<TUser extends BaseUser> {
|
|
21
|
+
required: true;
|
|
22
|
+
secret: string;
|
|
23
|
+
user: InputZodLike<TUser>;
|
|
24
|
+
}
|
|
25
|
+
type PluvIOConfig<TUser extends BaseUser> = Partial<Pick<PluvIOListeners<PluvPlatform, PluvAuthorize<TUser>, {}, {}>, "onRoomDeleted">> & {
|
|
26
|
+
authorize: {
|
|
27
|
+
user: InputZodLike<TUser>;
|
|
28
|
+
};
|
|
29
|
+
basePath: string;
|
|
30
|
+
getInitialStorage?: GetInitialStorageFn<PluvPlatform>;
|
|
31
|
+
secretKey: string;
|
|
32
|
+
};
|
|
33
|
+
declare class PluvIO<TUser extends BaseUser> {
|
|
34
|
+
private readonly _authorize;
|
|
35
|
+
private readonly _basePath;
|
|
36
|
+
private readonly _getInitialStorage?;
|
|
37
|
+
private readonly _listeners;
|
|
38
|
+
private readonly _secretKey;
|
|
39
|
+
get fetch(): (request: Request, Env?: unknown, executionCtx?: hono.ExecutionContext) => Response | Promise<Response>;
|
|
40
|
+
get handler(): (req: Request, requestContext: hono_types.FetchEventLike) => Response | Promise<Response>;
|
|
41
|
+
private _webhooks;
|
|
42
|
+
constructor(options: PluvIOConfig<TUser>);
|
|
43
|
+
createToken(params: JWTEncodeParams<TUser, PluvPlatform>): Promise<string>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
declare const createIO: <TUser extends BaseUser$1>(config: PluvIOConfig<TUser>) => PluvIO<TUser>;
|
|
47
|
+
|
|
48
|
+
export { PluvIO, type PluvIOConfig, PluvPlatform, createIO };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var __async = (__this, __arguments, generator) => {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
var fulfilled = (value) => {
|
|
22
|
+
try {
|
|
23
|
+
step(generator.next(value));
|
|
24
|
+
} catch (e) {
|
|
25
|
+
reject(e);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
var rejected = (value) => {
|
|
29
|
+
try {
|
|
30
|
+
step(generator.throw(value));
|
|
31
|
+
} catch (e) {
|
|
32
|
+
reject(e);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
36
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// src/index.ts
|
|
41
|
+
var src_exports = {};
|
|
42
|
+
__export(src_exports, {
|
|
43
|
+
createIO: () => createIO
|
|
44
|
+
});
|
|
45
|
+
module.exports = __toCommonJS(src_exports);
|
|
46
|
+
|
|
47
|
+
// src/PluvIO.ts
|
|
48
|
+
var import_hono = require("hono");
|
|
49
|
+
var import_vercel = require("hono/vercel");
|
|
50
|
+
|
|
51
|
+
// src/schemas.ts
|
|
52
|
+
var import_zod = require("zod");
|
|
53
|
+
var ZodEventInitialStorage = import_zod.z.object({
|
|
54
|
+
event: import_zod.z.literal("initial-storage"),
|
|
55
|
+
data: import_zod.z.object({
|
|
56
|
+
room: import_zod.z.string().nullable()
|
|
57
|
+
})
|
|
58
|
+
});
|
|
59
|
+
var ZodEventRoomDeleted = import_zod.z.object({
|
|
60
|
+
event: import_zod.z.literal("room-deleted"),
|
|
61
|
+
data: import_zod.z.object({
|
|
62
|
+
room: import_zod.z.string(),
|
|
63
|
+
storage: import_zod.z.string().nullable()
|
|
64
|
+
})
|
|
65
|
+
});
|
|
66
|
+
var ZodEvent = import_zod.z.discriminatedUnion("event", [ZodEventInitialStorage, ZodEventRoomDeleted]);
|
|
67
|
+
|
|
68
|
+
// src/PluvIO.ts
|
|
69
|
+
var PluvIO = class {
|
|
70
|
+
constructor(options) {
|
|
71
|
+
this._webhooks = new import_hono.Hono().basePath("/").post("/event", (c) => __async(this, null, function* () {
|
|
72
|
+
var _a, _b;
|
|
73
|
+
const body = yield c.req.json();
|
|
74
|
+
const parsed = ZodEvent.safeParse(body);
|
|
75
|
+
if (!parsed.success) return c.json({ data: { ok: true } }, 200);
|
|
76
|
+
const { event, data } = parsed.data;
|
|
77
|
+
switch (event) {
|
|
78
|
+
case "initial-storage": {
|
|
79
|
+
const room = data.room;
|
|
80
|
+
const storage = typeof room === "string" ? (_b = yield (_a = this._getInitialStorage) == null ? void 0 : _a.call(this, { room })) != null ? _b : null : null;
|
|
81
|
+
return c.json({ data: { storage } }, 200);
|
|
82
|
+
}
|
|
83
|
+
case "room-deleted": {
|
|
84
|
+
const room = data.room;
|
|
85
|
+
const encodedState = data.storage;
|
|
86
|
+
yield Promise.resolve(this._listeners.onRoomDeleted({ context: {}, encodedState, room }));
|
|
87
|
+
return c.json({ data: { room } }, 200);
|
|
88
|
+
}
|
|
89
|
+
default:
|
|
90
|
+
return c.json({ data: { ok: true } }, 200);
|
|
91
|
+
}
|
|
92
|
+
}));
|
|
93
|
+
const { authorize, basePath, onRoomDeleted, getInitialStorage, secretKey } = options;
|
|
94
|
+
this._authorize = {
|
|
95
|
+
required: true,
|
|
96
|
+
secret: "",
|
|
97
|
+
user: authorize.user
|
|
98
|
+
};
|
|
99
|
+
this._basePath = basePath;
|
|
100
|
+
this._getInitialStorage = getInitialStorage;
|
|
101
|
+
this._listeners = { onRoomDeleted: (event) => onRoomDeleted == null ? void 0 : onRoomDeleted(event) };
|
|
102
|
+
this._secretKey = secretKey;
|
|
103
|
+
}
|
|
104
|
+
get fetch() {
|
|
105
|
+
const app = new import_hono.Hono().basePath(this._basePath).route("/", this._webhooks);
|
|
106
|
+
return app.fetch;
|
|
107
|
+
}
|
|
108
|
+
get handler() {
|
|
109
|
+
const app = new import_hono.Hono().basePath(this._basePath).route("/", this._webhooks);
|
|
110
|
+
return (0, import_vercel.handle)(app);
|
|
111
|
+
}
|
|
112
|
+
createToken(params) {
|
|
113
|
+
return __async(this, null, function* () {
|
|
114
|
+
var _a;
|
|
115
|
+
const parsed = this._authorize.user.parse(params.user);
|
|
116
|
+
const res = yield fetch("https://pluv.io/api/participant/authorize", {
|
|
117
|
+
method: "post",
|
|
118
|
+
body: JSON.stringify({
|
|
119
|
+
maxAge: (_a = params.maxAge) != null ? _a : null,
|
|
120
|
+
room: params.room,
|
|
121
|
+
secretKey: this._secretKey,
|
|
122
|
+
user: parsed
|
|
123
|
+
})
|
|
124
|
+
}).catch(() => null);
|
|
125
|
+
if (!res || !res.ok || res.status !== 200) {
|
|
126
|
+
throw new Error("Authorization failed");
|
|
127
|
+
}
|
|
128
|
+
const token = yield res.text().catch(() => null);
|
|
129
|
+
if (typeof token !== "string") {
|
|
130
|
+
throw new Error("Authorization failed");
|
|
131
|
+
}
|
|
132
|
+
return token;
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
// src/createIO.ts
|
|
138
|
+
var createIO = (config) => {
|
|
139
|
+
return new PluvIO(config);
|
|
140
|
+
};
|
|
141
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
142
|
+
0 && (module.exports = {
|
|
143
|
+
createIO
|
|
144
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/PluvIO.ts
|
|
23
|
+
import { Hono } from "hono";
|
|
24
|
+
import { handle } from "hono/vercel";
|
|
25
|
+
|
|
26
|
+
// src/schemas.ts
|
|
27
|
+
import { z } from "zod";
|
|
28
|
+
var ZodEventInitialStorage = z.object({
|
|
29
|
+
event: z.literal("initial-storage"),
|
|
30
|
+
data: z.object({
|
|
31
|
+
room: z.string().nullable()
|
|
32
|
+
})
|
|
33
|
+
});
|
|
34
|
+
var ZodEventRoomDeleted = z.object({
|
|
35
|
+
event: z.literal("room-deleted"),
|
|
36
|
+
data: z.object({
|
|
37
|
+
room: z.string(),
|
|
38
|
+
storage: z.string().nullable()
|
|
39
|
+
})
|
|
40
|
+
});
|
|
41
|
+
var ZodEvent = z.discriminatedUnion("event", [ZodEventInitialStorage, ZodEventRoomDeleted]);
|
|
42
|
+
|
|
43
|
+
// src/PluvIO.ts
|
|
44
|
+
var PluvIO = class {
|
|
45
|
+
constructor(options) {
|
|
46
|
+
this._webhooks = new Hono().basePath("/").post("/event", (c) => __async(this, null, function* () {
|
|
47
|
+
var _a, _b;
|
|
48
|
+
const body = yield c.req.json();
|
|
49
|
+
const parsed = ZodEvent.safeParse(body);
|
|
50
|
+
if (!parsed.success) return c.json({ data: { ok: true } }, 200);
|
|
51
|
+
const { event, data } = parsed.data;
|
|
52
|
+
switch (event) {
|
|
53
|
+
case "initial-storage": {
|
|
54
|
+
const room = data.room;
|
|
55
|
+
const storage = typeof room === "string" ? (_b = yield (_a = this._getInitialStorage) == null ? void 0 : _a.call(this, { room })) != null ? _b : null : null;
|
|
56
|
+
return c.json({ data: { storage } }, 200);
|
|
57
|
+
}
|
|
58
|
+
case "room-deleted": {
|
|
59
|
+
const room = data.room;
|
|
60
|
+
const encodedState = data.storage;
|
|
61
|
+
yield Promise.resolve(this._listeners.onRoomDeleted({ context: {}, encodedState, room }));
|
|
62
|
+
return c.json({ data: { room } }, 200);
|
|
63
|
+
}
|
|
64
|
+
default:
|
|
65
|
+
return c.json({ data: { ok: true } }, 200);
|
|
66
|
+
}
|
|
67
|
+
}));
|
|
68
|
+
const { authorize, basePath, onRoomDeleted, getInitialStorage, secretKey } = options;
|
|
69
|
+
this._authorize = {
|
|
70
|
+
required: true,
|
|
71
|
+
secret: "",
|
|
72
|
+
user: authorize.user
|
|
73
|
+
};
|
|
74
|
+
this._basePath = basePath;
|
|
75
|
+
this._getInitialStorage = getInitialStorage;
|
|
76
|
+
this._listeners = { onRoomDeleted: (event) => onRoomDeleted == null ? void 0 : onRoomDeleted(event) };
|
|
77
|
+
this._secretKey = secretKey;
|
|
78
|
+
}
|
|
79
|
+
get fetch() {
|
|
80
|
+
const app = new Hono().basePath(this._basePath).route("/", this._webhooks);
|
|
81
|
+
return app.fetch;
|
|
82
|
+
}
|
|
83
|
+
get handler() {
|
|
84
|
+
const app = new Hono().basePath(this._basePath).route("/", this._webhooks);
|
|
85
|
+
return handle(app);
|
|
86
|
+
}
|
|
87
|
+
createToken(params) {
|
|
88
|
+
return __async(this, null, function* () {
|
|
89
|
+
var _a;
|
|
90
|
+
const parsed = this._authorize.user.parse(params.user);
|
|
91
|
+
const res = yield fetch("https://pluv.io/api/participant/authorize", {
|
|
92
|
+
method: "post",
|
|
93
|
+
body: JSON.stringify({
|
|
94
|
+
maxAge: (_a = params.maxAge) != null ? _a : null,
|
|
95
|
+
room: params.room,
|
|
96
|
+
secretKey: this._secretKey,
|
|
97
|
+
user: parsed
|
|
98
|
+
})
|
|
99
|
+
}).catch(() => null);
|
|
100
|
+
if (!res || !res.ok || res.status !== 200) {
|
|
101
|
+
throw new Error("Authorization failed");
|
|
102
|
+
}
|
|
103
|
+
const token = yield res.text().catch(() => null);
|
|
104
|
+
if (typeof token !== "string") {
|
|
105
|
+
throw new Error("Authorization failed");
|
|
106
|
+
}
|
|
107
|
+
return token;
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
// src/createIO.ts
|
|
113
|
+
var createIO = (config) => {
|
|
114
|
+
return new PluvIO(config);
|
|
115
|
+
};
|
|
116
|
+
export {
|
|
117
|
+
createIO
|
|
118
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pluv/platform-pluv",
|
|
3
|
+
"version": "0.30.0",
|
|
4
|
+
"description": "@pluv/io adapter for pluv.io",
|
|
5
|
+
"author": "leedavidcs",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/pluv-io/pluv",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/pluv-io/pluv.git",
|
|
11
|
+
"directory": "packages/platform-pluv"
|
|
12
|
+
},
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"module": "./dist/index.mjs",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@types/node": "^22.5.5",
|
|
21
|
+
"hono": "^4.6.3",
|
|
22
|
+
"zod": "^3.23.8",
|
|
23
|
+
"@pluv/types": "^0.30.0",
|
|
24
|
+
"@pluv/io": "^0.30.0",
|
|
25
|
+
"@pluv/crdt": "^0.30.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"eslint": "^8.57.0",
|
|
29
|
+
"tsup": "^8.3.0",
|
|
30
|
+
"typescript": "^5.6.2",
|
|
31
|
+
"@pluv/tsconfig": "^0.30.0",
|
|
32
|
+
"eslint-config-pluv": "^0.30.0"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
36
|
+
"dev": "tsup src/index.ts --format esm,cjs --watch --dts",
|
|
37
|
+
"lint": "eslint src/**/*.ts* --fix",
|
|
38
|
+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/PluvIO.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { GetInitialStorageFn, JWTEncodeParams, PluvIOListeners } from "@pluv/io";
|
|
2
|
+
import type { BaseUser, InputZodLike } from "@pluv/types";
|
|
3
|
+
import { Hono } from "hono";
|
|
4
|
+
import { handle } from "hono/vercel";
|
|
5
|
+
import type { PluvPlatform } from "./PluvPlatform";
|
|
6
|
+
import { ZodEvent } from "./schemas";
|
|
7
|
+
|
|
8
|
+
interface PluvAuthorize<TUser extends BaseUser> {
|
|
9
|
+
required: true;
|
|
10
|
+
secret: string;
|
|
11
|
+
user: InputZodLike<TUser>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type PluvIOConfig<TUser extends BaseUser> = Partial<
|
|
15
|
+
Pick<PluvIOListeners<PluvPlatform, PluvAuthorize<TUser>, {}, {}>, "onRoomDeleted">
|
|
16
|
+
> & {
|
|
17
|
+
authorize: {
|
|
18
|
+
user: InputZodLike<TUser>;
|
|
19
|
+
};
|
|
20
|
+
basePath: string;
|
|
21
|
+
getInitialStorage?: GetInitialStorageFn<PluvPlatform>;
|
|
22
|
+
secretKey: string;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export class PluvIO<TUser extends BaseUser> {
|
|
26
|
+
private readonly _authorize: PluvAuthorize<TUser>;
|
|
27
|
+
private readonly _basePath: string;
|
|
28
|
+
private readonly _getInitialStorage?: GetInitialStorageFn<PluvPlatform>;
|
|
29
|
+
private readonly _listeners: Pick<PluvIOListeners<PluvPlatform, PluvAuthorize<TUser>, {}, {}>, "onRoomDeleted">;
|
|
30
|
+
private readonly _secretKey: string;
|
|
31
|
+
|
|
32
|
+
public get fetch() {
|
|
33
|
+
const app = new Hono().basePath(this._basePath).route("/", this._webhooks);
|
|
34
|
+
|
|
35
|
+
return app.fetch;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public get handler() {
|
|
39
|
+
const app = new Hono().basePath(this._basePath).route("/", this._webhooks);
|
|
40
|
+
|
|
41
|
+
return handle(app);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private _webhooks = new Hono().basePath("/").post("/event", async (c) => {
|
|
45
|
+
const body = await c.req.json();
|
|
46
|
+
const parsed = ZodEvent.safeParse(body);
|
|
47
|
+
|
|
48
|
+
if (!parsed.success) return c.json({ data: { ok: true } }, 200);
|
|
49
|
+
|
|
50
|
+
const { event, data } = parsed.data;
|
|
51
|
+
|
|
52
|
+
switch (event) {
|
|
53
|
+
case "initial-storage": {
|
|
54
|
+
const room = data.room;
|
|
55
|
+
const storage = typeof room === "string" ? ((await this._getInitialStorage?.({ room })) ?? null) : null;
|
|
56
|
+
|
|
57
|
+
return c.json({ data: { storage } }, 200);
|
|
58
|
+
}
|
|
59
|
+
case "room-deleted": {
|
|
60
|
+
const room = data.room;
|
|
61
|
+
const encodedState = data.storage;
|
|
62
|
+
|
|
63
|
+
await Promise.resolve(this._listeners.onRoomDeleted({ context: {}, encodedState, room }));
|
|
64
|
+
|
|
65
|
+
return c.json({ data: { room } }, 200);
|
|
66
|
+
}
|
|
67
|
+
default:
|
|
68
|
+
return c.json({ data: { ok: true } }, 200);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
constructor(options: PluvIOConfig<TUser>) {
|
|
73
|
+
const { authorize, basePath, onRoomDeleted, getInitialStorage, secretKey } = options;
|
|
74
|
+
|
|
75
|
+
this._authorize = {
|
|
76
|
+
required: true,
|
|
77
|
+
secret: "",
|
|
78
|
+
user: authorize.user,
|
|
79
|
+
};
|
|
80
|
+
this._basePath = basePath;
|
|
81
|
+
this._getInitialStorage = getInitialStorage;
|
|
82
|
+
this._listeners = { onRoomDeleted: (event) => onRoomDeleted?.(event) };
|
|
83
|
+
this._secretKey = secretKey;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public async createToken(params: JWTEncodeParams<TUser, PluvPlatform>): Promise<string> {
|
|
87
|
+
const parsed = this._authorize.user.parse(params.user);
|
|
88
|
+
|
|
89
|
+
const res = await fetch("https://pluv.io/api/participant/authorize", {
|
|
90
|
+
method: "post",
|
|
91
|
+
body: JSON.stringify({
|
|
92
|
+
maxAge: params.maxAge ?? null,
|
|
93
|
+
room: params.room,
|
|
94
|
+
secretKey: this._secretKey,
|
|
95
|
+
user: parsed,
|
|
96
|
+
}),
|
|
97
|
+
}).catch(() => null);
|
|
98
|
+
|
|
99
|
+
if (!res || !res.ok || res.status !== 200) {
|
|
100
|
+
throw new Error("Authorization failed");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const token = await res.text().catch(() => null);
|
|
104
|
+
|
|
105
|
+
if (typeof token !== "string") {
|
|
106
|
+
throw new Error("Authorization failed");
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return token;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AbstractPlatform,
|
|
3
|
+
AbstractPlatformConfig,
|
|
4
|
+
AbstractWebSocket,
|
|
5
|
+
ConvertWebSocketConfig,
|
|
6
|
+
WebSocketRegistrationMode,
|
|
7
|
+
WebSocketSerializedState,
|
|
8
|
+
} from "@pluv/io";
|
|
9
|
+
|
|
10
|
+
export class PluvPlatform extends AbstractPlatform {
|
|
11
|
+
_registrationMode: WebSocketRegistrationMode = "attached";
|
|
12
|
+
|
|
13
|
+
public acceptWebSocket(webSocket: AbstractWebSocket): Promise<void> {
|
|
14
|
+
throw new Error("Not implemented");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public convertWebSocket(webSocket: any, config: ConvertWebSocketConfig): AbstractWebSocket {
|
|
18
|
+
throw new Error("Not implemented");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public getLastPing(webSocket: AbstractWebSocket): number | null {
|
|
22
|
+
throw new Error("Not implemented");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public getSerializedState(webSocket: any): WebSocketSerializedState | null {
|
|
26
|
+
throw new Error("Not implemented");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public getSessionId(webSocket: any): string | null {
|
|
30
|
+
throw new Error("Not implemented");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public getWebSockets(): readonly any[] {
|
|
34
|
+
throw new Error("Not implemented");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public initialize(config: AbstractPlatformConfig<{}, {}>): this {
|
|
38
|
+
throw new Error("Not implemented");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public parseData(data: string | ArrayBuffer): Record<string, any> {
|
|
42
|
+
throw new Error("Not implemented");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public randomUUID(): string {
|
|
46
|
+
throw new Error("Not implemented");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public setSerializedState(webSocket: AbstractWebSocket, state: WebSocketSerializedState): void {
|
|
50
|
+
throw new Error("Not implemented");
|
|
51
|
+
}
|
|
52
|
+
}
|
package/src/createIO.ts
ADDED
package/src/index.ts
ADDED
package/src/schemas.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
const ZodEventInitialStorage = z.object({
|
|
4
|
+
event: z.literal("initial-storage"),
|
|
5
|
+
data: z.object({
|
|
6
|
+
room: z.string().nullable(),
|
|
7
|
+
}),
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
const ZodEventRoomDeleted = z.object({
|
|
11
|
+
event: z.literal("room-deleted"),
|
|
12
|
+
data: z.object({
|
|
13
|
+
room: z.string(),
|
|
14
|
+
storage: z.string().nullable(),
|
|
15
|
+
}),
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
export const ZodEvent = z.discriminatedUnion("event", [ZodEventInitialStorage, ZodEventRoomDeleted]);
|