@pluv/platform-cloudflare 0.30.2 → 0.32.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/.turbo/turbo-build.log +8 -8
- package/CHANGELOG.md +142 -0
- package/dist/index.d.mts +7 -8
- package/dist/index.d.ts +7 -8
- package/dist/index.js +9 -7
- package/dist/index.mjs +9 -7
- package/package.json +6 -6
- package/src/CloudflarePlatform.ts +9 -8
- package/src/createPluvHandler.ts +14 -12
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @pluv/platform-cloudflare@0.
|
|
2
|
+
> @pluv/platform-cloudflare@0.32.0 build /home/runner/work/pluv/pluv/packages/platform-cloudflare
|
|
3
3
|
> tsup src/index.ts --format esm,cjs --dts
|
|
4
4
|
|
|
5
5
|
[34mCLI[39m Building entry: src/index.ts
|
|
@@ -8,11 +8,11 @@
|
|
|
8
8
|
[34mCLI[39m Target: es6
|
|
9
9
|
[34mESM[39m Build start
|
|
10
10
|
[34mCJS[39m Build start
|
|
11
|
-
[32mESM[39m [1mdist/index.mjs [22m[32m11.
|
|
12
|
-
[32mESM[39m ⚡️ Build success in
|
|
13
|
-
[32mCJS[39m [1mdist/index.js [22m[32m12.
|
|
14
|
-
[32mCJS[39m ⚡️ Build success in
|
|
11
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m11.66 KB[39m
|
|
12
|
+
[32mESM[39m ⚡️ Build success in 64ms
|
|
13
|
+
[32mCJS[39m [1mdist/index.js [22m[32m12.76 KB[39m
|
|
14
|
+
[32mCJS[39m ⚡️ Build success in 64ms
|
|
15
15
|
[34mDTS[39m Build start
|
|
16
|
-
[32mDTS[39m ⚡️ Build success in
|
|
17
|
-
[32mDTS[39m [1mdist/index.d.mts [22m[32m4.
|
|
18
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[32m4.
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 995ms
|
|
17
|
+
[32mDTS[39m [1mdist/index.d.mts [22m[32m4.32 KB[39m
|
|
18
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m4.32 KB[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,147 @@
|
|
|
1
1
|
# @pluv/platform-cloudflare
|
|
2
2
|
|
|
3
|
+
## 0.32.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [cde5305]
|
|
8
|
+
- @pluv/io@0.32.0
|
|
9
|
+
- @pluv/persistence-cloudflare-transactional-storage@0.32.0
|
|
10
|
+
- @pluv/types@0.32.0
|
|
11
|
+
|
|
12
|
+
## 0.31.0
|
|
13
|
+
|
|
14
|
+
### Minor Changes
|
|
15
|
+
|
|
16
|
+
- b3c31d7: **BREAKING**
|
|
17
|
+
|
|
18
|
+
Fixed platform context types. This will require additional properties when registering a websocket and creating authorization tokens. See example below:
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
// @pluv/platform-node example
|
|
22
|
+
|
|
23
|
+
import { platformNode } from "@pluv/platform-node";
|
|
24
|
+
import { createIO } from "@pluv/io";
|
|
25
|
+
import type { IncomingMessage } from "node:http";
|
|
26
|
+
import { z } from "zod";
|
|
27
|
+
|
|
28
|
+
const io = createIO({
|
|
29
|
+
// If using a function authorize parameter, `req` is now available as a param
|
|
30
|
+
authorize: ({ req }) => ({
|
|
31
|
+
required: true,
|
|
32
|
+
secret: "MY-CUSTOM-SECRET",
|
|
33
|
+
user: z.object({
|
|
34
|
+
id: z.string(),
|
|
35
|
+
}),
|
|
36
|
+
}),
|
|
37
|
+
platformNode(),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Before
|
|
41
|
+
io.createToken({
|
|
42
|
+
room: "my-custom-room",
|
|
43
|
+
user: { id: "abc123" },
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// After
|
|
47
|
+
io.createToken({
|
|
48
|
+
room: "my-custom-room",
|
|
49
|
+
user: { id: "abc123" },
|
|
50
|
+
|
|
51
|
+
// Previously not required, but now required
|
|
52
|
+
req: req as IncomingMessage,
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
// @pluv/platform-cloudflare example
|
|
58
|
+
|
|
59
|
+
import { platformCloudflare } from "@pluv/platform-cloudflare";
|
|
60
|
+
import { createIO } from "@pluv/io";
|
|
61
|
+
import { z } from "zod";
|
|
62
|
+
|
|
63
|
+
const io = createIO({
|
|
64
|
+
// If using a function authorize parameter, `env` and `request` are now available as params
|
|
65
|
+
authorize: ({ env, request }) => ({
|
|
66
|
+
required: true,
|
|
67
|
+
secret: "MY-CUSTOM-SECRET",
|
|
68
|
+
user: z.object({
|
|
69
|
+
id: z.string(),
|
|
70
|
+
}),
|
|
71
|
+
}),
|
|
72
|
+
platformCloudflare(),
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// Before
|
|
76
|
+
io.createToken({
|
|
77
|
+
room: "my-custom-room",
|
|
78
|
+
user: { id: "abc123" },
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// After
|
|
82
|
+
io.createToken({
|
|
83
|
+
room: "my-custom-room",
|
|
84
|
+
user: { id: "abc123" },
|
|
85
|
+
|
|
86
|
+
// Previously not required, but now required
|
|
87
|
+
env: env as Env,
|
|
88
|
+
request: request as Request,
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
- 0f98064: **BREAKING**
|
|
93
|
+
|
|
94
|
+
Update `authorize` params so that `roomId` is renamed to `room` and more platform-specific parameters are exposed. See example below:
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
// @pluv/platform-cloudflare example
|
|
98
|
+
import { createPluvHandler } from "@pluv/platform-cloudflare";
|
|
99
|
+
|
|
100
|
+
// Before
|
|
101
|
+
createPluvHandler({
|
|
102
|
+
// ...
|
|
103
|
+
authorize: ({ roomId }) => {
|
|
104
|
+
// ...
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// After
|
|
109
|
+
createPluvHandler({
|
|
110
|
+
// ...
|
|
111
|
+
authorize: ({ env, request, room }) => {
|
|
112
|
+
// ...
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
// @pluv/platform-node example
|
|
119
|
+
import { createPluvHandler } from "@pluv/platform-node";
|
|
120
|
+
|
|
121
|
+
// Before
|
|
122
|
+
createPluvHandler({
|
|
123
|
+
// ...
|
|
124
|
+
authorize: ({ roomId }) => {
|
|
125
|
+
// ...
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// After
|
|
130
|
+
createPluvHandler({
|
|
131
|
+
// ...
|
|
132
|
+
authorize: ({ req, room }) => {
|
|
133
|
+
// ...
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Patch Changes
|
|
139
|
+
|
|
140
|
+
- Updated dependencies [b3c31d7]
|
|
141
|
+
- @pluv/io@0.31.0
|
|
142
|
+
- @pluv/persistence-cloudflare-transactional-storage@0.31.0
|
|
143
|
+
- @pluv/types@0.31.0
|
|
144
|
+
|
|
3
145
|
## 0.30.2
|
|
4
146
|
|
|
5
147
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AbstractWebSocket, WebSocketSerializedState, AbstractEventMap, AbstractListener, AbstractWebSocketConfig, AbstractPlatformConfig, WebSocketRegistrationMode, AbstractPlatform, ConvertWebSocketConfig, PluvServer } from '@pluv/io';
|
|
1
|
+
import { AbstractWebSocket, WebSocketSerializedState, AbstractEventMap, AbstractListener, AbstractWebSocketConfig, AbstractPlatformConfig, WebSocketRegistrationMode, AbstractPlatform, ConvertWebSocketConfig, PluvServer, InferInitContextType } from '@pluv/io';
|
|
2
2
|
import { JsonObject, MaybePromise, Maybe, InferIOAuthorizeUser, InferIOAuthorize, InferIOAuthorizeRequired, Id } from '@pluv/types';
|
|
3
3
|
|
|
4
4
|
type CloudflareWebSocketConfig = AbstractWebSocketConfig;
|
|
@@ -17,14 +17,15 @@ declare class CloudflareWebSocket extends AbstractWebSocket<WebSocket> {
|
|
|
17
17
|
|
|
18
18
|
type CloudflarePlatformConfig<TEnv extends Record<string, any> = {}> = AbstractPlatformConfig<{
|
|
19
19
|
env: TEnv;
|
|
20
|
-
}, {
|
|
21
20
|
state: DurableObjectState;
|
|
22
21
|
}> & {
|
|
23
22
|
mode?: WebSocketRegistrationMode;
|
|
24
23
|
};
|
|
25
24
|
declare class CloudflarePlatform<TEnv extends Record<string, any> = {}> extends AbstractPlatform<CloudflareWebSocket, {
|
|
26
25
|
env: TEnv;
|
|
26
|
+
request: Request;
|
|
27
27
|
}, {
|
|
28
|
+
env: TEnv;
|
|
28
29
|
state: DurableObjectState;
|
|
29
30
|
}> {
|
|
30
31
|
readonly _registrationMode: WebSocketRegistrationMode;
|
|
@@ -37,7 +38,6 @@ declare class CloudflarePlatform<TEnv extends Record<string, any> = {}> extends
|
|
|
37
38
|
getWebSockets(): readonly WebSocket[];
|
|
38
39
|
initialize(config: AbstractPlatformConfig<{
|
|
39
40
|
env: TEnv;
|
|
40
|
-
}, {
|
|
41
41
|
state: DurableObjectState;
|
|
42
42
|
}>): this;
|
|
43
43
|
parseData(data: string | ArrayBuffer): Record<string, any>;
|
|
@@ -46,11 +46,10 @@ declare class CloudflarePlatform<TEnv extends Record<string, any> = {}> extends
|
|
|
46
46
|
private _getDetachedState;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
type AuthorizeFunction<TPluvServer extends PluvServer<CloudflarePlatform, any, any, any>> = (ctx: AuthorizeFunctionContext) => MaybePromise<Maybe<InferIOAuthorizeUser<InferIOAuthorize<TPluvServer>>>>;
|
|
49
|
+
type AuthorizeFunctionContext<TPluvServer extends PluvServer<any, any, any, any>> = {
|
|
50
|
+
room: string;
|
|
51
|
+
} & InferInitContextType<TPluvServer extends PluvServer<infer IPlatform, any, any, any> ? IPlatform : never>;
|
|
52
|
+
type AuthorizeFunction<TPluvServer extends PluvServer<any, any, any, any>> = (ctx: AuthorizeFunctionContext<TPluvServer>) => MaybePromise<Maybe<InferIOAuthorizeUser<InferIOAuthorize<TPluvServer>>>>;
|
|
54
53
|
type CreatePluvHandlerConfig<TPluvServer extends PluvServer<CloudflarePlatform, any, any, any>, TEnv extends Record<string, any>> = {
|
|
55
54
|
binding: string;
|
|
56
55
|
endpoint?: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AbstractWebSocket, WebSocketSerializedState, AbstractEventMap, AbstractListener, AbstractWebSocketConfig, AbstractPlatformConfig, WebSocketRegistrationMode, AbstractPlatform, ConvertWebSocketConfig, PluvServer } from '@pluv/io';
|
|
1
|
+
import { AbstractWebSocket, WebSocketSerializedState, AbstractEventMap, AbstractListener, AbstractWebSocketConfig, AbstractPlatformConfig, WebSocketRegistrationMode, AbstractPlatform, ConvertWebSocketConfig, PluvServer, InferInitContextType } from '@pluv/io';
|
|
2
2
|
import { JsonObject, MaybePromise, Maybe, InferIOAuthorizeUser, InferIOAuthorize, InferIOAuthorizeRequired, Id } from '@pluv/types';
|
|
3
3
|
|
|
4
4
|
type CloudflareWebSocketConfig = AbstractWebSocketConfig;
|
|
@@ -17,14 +17,15 @@ declare class CloudflareWebSocket extends AbstractWebSocket<WebSocket> {
|
|
|
17
17
|
|
|
18
18
|
type CloudflarePlatformConfig<TEnv extends Record<string, any> = {}> = AbstractPlatformConfig<{
|
|
19
19
|
env: TEnv;
|
|
20
|
-
}, {
|
|
21
20
|
state: DurableObjectState;
|
|
22
21
|
}> & {
|
|
23
22
|
mode?: WebSocketRegistrationMode;
|
|
24
23
|
};
|
|
25
24
|
declare class CloudflarePlatform<TEnv extends Record<string, any> = {}> extends AbstractPlatform<CloudflareWebSocket, {
|
|
26
25
|
env: TEnv;
|
|
26
|
+
request: Request;
|
|
27
27
|
}, {
|
|
28
|
+
env: TEnv;
|
|
28
29
|
state: DurableObjectState;
|
|
29
30
|
}> {
|
|
30
31
|
readonly _registrationMode: WebSocketRegistrationMode;
|
|
@@ -37,7 +38,6 @@ declare class CloudflarePlatform<TEnv extends Record<string, any> = {}> extends
|
|
|
37
38
|
getWebSockets(): readonly WebSocket[];
|
|
38
39
|
initialize(config: AbstractPlatformConfig<{
|
|
39
40
|
env: TEnv;
|
|
40
|
-
}, {
|
|
41
41
|
state: DurableObjectState;
|
|
42
42
|
}>): this;
|
|
43
43
|
parseData(data: string | ArrayBuffer): Record<string, any>;
|
|
@@ -46,11 +46,10 @@ declare class CloudflarePlatform<TEnv extends Record<string, any> = {}> extends
|
|
|
46
46
|
private _getDetachedState;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
type AuthorizeFunction<TPluvServer extends PluvServer<CloudflarePlatform, any, any, any>> = (ctx: AuthorizeFunctionContext) => MaybePromise<Maybe<InferIOAuthorizeUser<InferIOAuthorize<TPluvServer>>>>;
|
|
49
|
+
type AuthorizeFunctionContext<TPluvServer extends PluvServer<any, any, any, any>> = {
|
|
50
|
+
room: string;
|
|
51
|
+
} & InferInitContextType<TPluvServer extends PluvServer<infer IPlatform, any, any, any> ? IPlatform : never>;
|
|
52
|
+
type AuthorizeFunction<TPluvServer extends PluvServer<any, any, any, any>> = (ctx: AuthorizeFunctionContext<TPluvServer>) => MaybePromise<Maybe<InferIOAuthorizeUser<InferIOAuthorize<TPluvServer>>>>;
|
|
54
53
|
type CreatePluvHandlerConfig<TPluvServer extends PluvServer<CloudflarePlatform, any, any, any>, TEnv extends Record<string, any>> = {
|
|
55
54
|
binding: string;
|
|
56
55
|
endpoint?: string;
|
package/dist/index.js
CHANGED
|
@@ -68,6 +68,7 @@ var createPluvHandler = (config) => {
|
|
|
68
68
|
const { authorize, binding, endpoint = "/api/pluv", modify, io } = config;
|
|
69
69
|
const DurableObject = class {
|
|
70
70
|
constructor(state, env) {
|
|
71
|
+
this._env = env;
|
|
71
72
|
this._room = io.createRoom(state.id.toString(), { env, state });
|
|
72
73
|
}
|
|
73
74
|
webSocketClose(ws, code, reason, wasClean) {
|
|
@@ -100,7 +101,7 @@ var createPluvHandler = (config) => {
|
|
|
100
101
|
}
|
|
101
102
|
const { 0: client, 1: server } = new WebSocketPair();
|
|
102
103
|
const token = new URL(request.url).searchParams.get("token");
|
|
103
|
-
yield this._room.register(server, { token });
|
|
104
|
+
yield this._room.register(server, { env: this._env, request, token });
|
|
104
105
|
return new Response(null, { status: 101, webSocket: client });
|
|
105
106
|
});
|
|
106
107
|
}
|
|
@@ -118,22 +119,23 @@ var createPluvHandler = (config) => {
|
|
|
118
119
|
const matcher = (0, import_path_to_regexp.match)(`${endpoint}/authorize`);
|
|
119
120
|
const matched = matcher(pathname);
|
|
120
121
|
if (!matched) return null;
|
|
121
|
-
const
|
|
122
|
-
if (!
|
|
122
|
+
const room = searchParams.get("room");
|
|
123
|
+
if (!room) {
|
|
123
124
|
return new Response("Not found", {
|
|
124
125
|
headers: { "Content-Type": "text/plain" },
|
|
125
126
|
status: 404
|
|
126
127
|
});
|
|
127
128
|
}
|
|
128
129
|
try {
|
|
129
|
-
const user = yield authorize({ request,
|
|
130
|
+
const user = yield authorize({ env, request, room });
|
|
130
131
|
if (!user) throw new Error();
|
|
131
132
|
const namespace = getDurableObjectNamespace(env);
|
|
132
|
-
const durableObjectId = namespace.idFromName(
|
|
133
|
+
const durableObjectId = namespace.idFromName(room);
|
|
133
134
|
const token = yield io.createToken({
|
|
134
135
|
env,
|
|
135
136
|
room: durableObjectId.toString(),
|
|
136
|
-
user
|
|
137
|
+
user,
|
|
138
|
+
request
|
|
137
139
|
});
|
|
138
140
|
return new Response(token, {
|
|
139
141
|
headers: { "Content-Type": "text/plain" },
|
|
@@ -307,7 +309,7 @@ var CloudflarePlatform = class _CloudflarePlatform extends import_io2.AbstractPl
|
|
|
307
309
|
}
|
|
308
310
|
initialize(config) {
|
|
309
311
|
var _a;
|
|
310
|
-
const context = (_a = config.context) != null ? _a : __spreadValues(
|
|
312
|
+
const context = (_a = config.context) != null ? _a : __spreadValues({}, this._roomContext);
|
|
311
313
|
if (!context.env || !context.state) {
|
|
312
314
|
throw new Error("Could not derive platform context");
|
|
313
315
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -44,6 +44,7 @@ var createPluvHandler = (config) => {
|
|
|
44
44
|
const { authorize, binding, endpoint = "/api/pluv", modify, io } = config;
|
|
45
45
|
const DurableObject = class {
|
|
46
46
|
constructor(state, env) {
|
|
47
|
+
this._env = env;
|
|
47
48
|
this._room = io.createRoom(state.id.toString(), { env, state });
|
|
48
49
|
}
|
|
49
50
|
webSocketClose(ws, code, reason, wasClean) {
|
|
@@ -76,7 +77,7 @@ var createPluvHandler = (config) => {
|
|
|
76
77
|
}
|
|
77
78
|
const { 0: client, 1: server } = new WebSocketPair();
|
|
78
79
|
const token = new URL(request.url).searchParams.get("token");
|
|
79
|
-
yield this._room.register(server, { token });
|
|
80
|
+
yield this._room.register(server, { env: this._env, request, token });
|
|
80
81
|
return new Response(null, { status: 101, webSocket: client });
|
|
81
82
|
});
|
|
82
83
|
}
|
|
@@ -94,22 +95,23 @@ var createPluvHandler = (config) => {
|
|
|
94
95
|
const matcher = match(`${endpoint}/authorize`);
|
|
95
96
|
const matched = matcher(pathname);
|
|
96
97
|
if (!matched) return null;
|
|
97
|
-
const
|
|
98
|
-
if (!
|
|
98
|
+
const room = searchParams.get("room");
|
|
99
|
+
if (!room) {
|
|
99
100
|
return new Response("Not found", {
|
|
100
101
|
headers: { "Content-Type": "text/plain" },
|
|
101
102
|
status: 404
|
|
102
103
|
});
|
|
103
104
|
}
|
|
104
105
|
try {
|
|
105
|
-
const user = yield authorize({ request,
|
|
106
|
+
const user = yield authorize({ env, request, room });
|
|
106
107
|
if (!user) throw new Error();
|
|
107
108
|
const namespace = getDurableObjectNamespace(env);
|
|
108
|
-
const durableObjectId = namespace.idFromName(
|
|
109
|
+
const durableObjectId = namespace.idFromName(room);
|
|
109
110
|
const token = yield io.createToken({
|
|
110
111
|
env,
|
|
111
112
|
room: durableObjectId.toString(),
|
|
112
|
-
user
|
|
113
|
+
user,
|
|
114
|
+
request
|
|
113
115
|
});
|
|
114
116
|
return new Response(token, {
|
|
115
117
|
headers: { "Content-Type": "text/plain" },
|
|
@@ -283,7 +285,7 @@ var CloudflarePlatform = class _CloudflarePlatform extends AbstractPlatform {
|
|
|
283
285
|
}
|
|
284
286
|
initialize(config) {
|
|
285
287
|
var _a;
|
|
286
|
-
const context = (_a = config.context) != null ? _a : __spreadValues(
|
|
288
|
+
const context = (_a = config.context) != null ? _a : __spreadValues({}, this._roomContext);
|
|
287
289
|
if (!context.env || !context.state) {
|
|
288
290
|
throw new Error("Could not derive platform context");
|
|
289
291
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pluv/platform-cloudflare",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.32.0",
|
|
4
4
|
"description": "@pluv/io adapter for cloudflare workers",
|
|
5
5
|
"author": "leedavidcs",
|
|
6
6
|
"license": "MIT",
|
|
@@ -18,17 +18,17 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"path-to-regexp": "^8.2.0",
|
|
21
|
-
"@pluv/io": "^0.
|
|
22
|
-
"@pluv/persistence-cloudflare-transactional-storage": "^0.
|
|
23
|
-
"@pluv/types": "^0.
|
|
21
|
+
"@pluv/io": "^0.32.0",
|
|
22
|
+
"@pluv/persistence-cloudflare-transactional-storage": "^0.32.0",
|
|
23
|
+
"@pluv/types": "^0.32.0"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@cloudflare/workers-types": "^4.20240925.0",
|
|
27
27
|
"eslint": "^8.57.0",
|
|
28
28
|
"tsup": "^8.3.0",
|
|
29
29
|
"typescript": "^5.6.2",
|
|
30
|
-
"@pluv/tsconfig": "^0.
|
|
31
|
-
"eslint-config-pluv": "^0.
|
|
30
|
+
"@pluv/tsconfig": "^0.32.0",
|
|
31
|
+
"eslint-config-pluv": "^0.32.0"
|
|
32
32
|
},
|
|
33
33
|
"scripts": {
|
|
34
34
|
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
AbstractPlatformConfig,
|
|
3
3
|
ConvertWebSocketConfig,
|
|
4
|
+
InferInitContextType,
|
|
4
5
|
WebSocketRegistrationMode,
|
|
5
6
|
WebSocketSerializedState,
|
|
6
7
|
} from "@pluv/io";
|
|
@@ -9,15 +10,15 @@ import { PersistenceCloudflareTransactionalStorage } from "@pluv/persistence-clo
|
|
|
9
10
|
import { CloudflareWebSocket } from "./CloudflareWebSocket";
|
|
10
11
|
import { DEFAULT_REGISTRATION_MODE } from "./constants";
|
|
11
12
|
|
|
12
|
-
export type CloudflarePlatformConfig<TEnv extends Record<string, any> = {}> = AbstractPlatformConfig<
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
> & { mode?: WebSocketRegistrationMode };
|
|
13
|
+
export type CloudflarePlatformConfig<TEnv extends Record<string, any> = {}> = AbstractPlatformConfig<{
|
|
14
|
+
env: TEnv;
|
|
15
|
+
state: DurableObjectState;
|
|
16
|
+
}> & { mode?: WebSocketRegistrationMode };
|
|
16
17
|
|
|
17
18
|
export class CloudflarePlatform<TEnv extends Record<string, any> = {}> extends AbstractPlatform<
|
|
18
19
|
CloudflareWebSocket,
|
|
19
|
-
{ env: TEnv },
|
|
20
|
-
{ state: DurableObjectState }
|
|
20
|
+
{ env: TEnv; request: Request },
|
|
21
|
+
{ env: TEnv; state: DurableObjectState }
|
|
21
22
|
> {
|
|
22
23
|
readonly _registrationMode: WebSocketRegistrationMode;
|
|
23
24
|
|
|
@@ -91,8 +92,8 @@ export class CloudflarePlatform<TEnv extends Record<string, any> = {}> extends A
|
|
|
91
92
|
return detachedState.getWebSockets() ?? [];
|
|
92
93
|
}
|
|
93
94
|
|
|
94
|
-
public initialize(config: AbstractPlatformConfig<{ env: TEnv
|
|
95
|
-
const context = config.context ?? { ...this.
|
|
95
|
+
public initialize(config: AbstractPlatformConfig<{ env: TEnv; state: DurableObjectState }>): this {
|
|
96
|
+
const context = config.context ?? { ...this._roomContext };
|
|
96
97
|
|
|
97
98
|
if (!context.env || !context.state) {
|
|
98
99
|
throw new Error("Could not derive platform context");
|
package/src/createPluvHandler.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { IORoom, PluvServer } from "@pluv/io";
|
|
1
|
+
import type { InferInitContextType, IORoom, PluvServer } from "@pluv/io";
|
|
2
2
|
import type {
|
|
3
3
|
Id,
|
|
4
4
|
InferIOAuthorize,
|
|
@@ -10,12 +10,11 @@ import type {
|
|
|
10
10
|
import { match } from "path-to-regexp";
|
|
11
11
|
import { CloudflarePlatform } from "./CloudflarePlatform";
|
|
12
12
|
|
|
13
|
-
export
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
ctx: AuthorizeFunctionContext,
|
|
13
|
+
export type AuthorizeFunctionContext<TPluvServer extends PluvServer<any, any, any, any>> = {
|
|
14
|
+
room: string;
|
|
15
|
+
} & InferInitContextType<TPluvServer extends PluvServer<infer IPlatform, any, any, any> ? IPlatform : never>;
|
|
16
|
+
export type AuthorizeFunction<TPluvServer extends PluvServer<any, any, any, any>> = (
|
|
17
|
+
ctx: AuthorizeFunctionContext<TPluvServer>,
|
|
19
18
|
) => MaybePromise<Maybe<InferIOAuthorizeUser<InferIOAuthorize<TPluvServer>>>>;
|
|
20
19
|
|
|
21
20
|
export type CreatePluvHandlerConfig<
|
|
@@ -52,9 +51,11 @@ export const createPluvHandler = <TPluvServer extends PluvServer<CloudflarePlatf
|
|
|
52
51
|
const { authorize, binding, endpoint = "/api/pluv", modify, io } = config;
|
|
53
52
|
|
|
54
53
|
const DurableObject = class implements DurableObject {
|
|
54
|
+
private _env: Id<InferCloudflarePluvHandlerEnv<TPluvServer>>;
|
|
55
55
|
private _room: IORoom<CloudflarePlatform>;
|
|
56
56
|
|
|
57
57
|
constructor(state: DurableObjectState, env: Id<InferCloudflarePluvHandlerEnv<TPluvServer>>) {
|
|
58
|
+
this._env = env;
|
|
58
59
|
this._room = io.createRoom(state.id.toString(), { env, state });
|
|
59
60
|
}
|
|
60
61
|
|
|
@@ -93,7 +94,7 @@ export const createPluvHandler = <TPluvServer extends PluvServer<CloudflarePlatf
|
|
|
93
94
|
const { 0: client, 1: server } = new WebSocketPair();
|
|
94
95
|
const token = new URL(request.url).searchParams.get("token");
|
|
95
96
|
|
|
96
|
-
await this._room.register(server, { token });
|
|
97
|
+
await this._room.register(server, { env: this._env, request, token });
|
|
97
98
|
|
|
98
99
|
return new Response(null, { status: 101, webSocket: client });
|
|
99
100
|
}
|
|
@@ -118,9 +119,9 @@ export const createPluvHandler = <TPluvServer extends PluvServer<CloudflarePlatf
|
|
|
118
119
|
|
|
119
120
|
if (!matched) return null;
|
|
120
121
|
|
|
121
|
-
const
|
|
122
|
+
const room = searchParams.get("room");
|
|
122
123
|
|
|
123
|
-
if (!
|
|
124
|
+
if (!room) {
|
|
124
125
|
return new Response("Not found", {
|
|
125
126
|
headers: { "Content-Type": "text/plain" },
|
|
126
127
|
status: 404,
|
|
@@ -128,17 +129,18 @@ export const createPluvHandler = <TPluvServer extends PluvServer<CloudflarePlatf
|
|
|
128
129
|
}
|
|
129
130
|
|
|
130
131
|
try {
|
|
131
|
-
const user = await authorize({ request,
|
|
132
|
+
const user = await authorize({ env, request, room } as AuthorizeFunctionContext<TPluvServer>);
|
|
132
133
|
|
|
133
134
|
if (!user) throw new Error();
|
|
134
135
|
|
|
135
136
|
const namespace = getDurableObjectNamespace(env);
|
|
136
|
-
const durableObjectId = namespace.idFromName(
|
|
137
|
+
const durableObjectId = namespace.idFromName(room);
|
|
137
138
|
|
|
138
139
|
const token = await io.createToken({
|
|
139
140
|
env,
|
|
140
141
|
room: durableObjectId.toString(),
|
|
141
142
|
user,
|
|
143
|
+
request,
|
|
142
144
|
});
|
|
143
145
|
|
|
144
146
|
return new Response(token, {
|