@pluv/platform-cloudflare 0.19.0 → 0.21.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 +15 -15
- package/CHANGELOG.md +85 -0
- package/README.md +0 -50
- package/dist/index.d.mts +32 -10
- package/dist/index.d.ts +32 -10
- package/dist/index.js +238 -19
- package/dist/index.mjs +239 -18
- package/package.json +9 -9
- package/src/CloudflarePlatform.ts +114 -4
- package/src/CloudflareWebSocket.ts +48 -13
- package/src/PersistanceCloudflare.ts +100 -0
- package/src/constants.ts +3 -0
- package/src/createPluvHandler.ts +26 -12
- package/src/index.ts +2 -1
- package/src/platformCloudflare.ts +3 -2
- package/src/utils/index.ts +1 -0
- package/src/utils/partitionByLength.ts +8 -0
package/src/createPluvHandler.ts
CHANGED
|
@@ -52,19 +52,37 @@ export const createPluvHandler = <TPluvServer extends PluvServer<CloudflarePlatf
|
|
|
52
52
|
const { authorize, binding, endpoint = "/api/pluv", modify, io } = config;
|
|
53
53
|
|
|
54
54
|
const DurableObject = class implements DurableObject {
|
|
55
|
-
private
|
|
56
|
-
private _io: IORoom<CloudflarePlatform>;
|
|
55
|
+
private _room: IORoom<CloudflarePlatform>;
|
|
57
56
|
|
|
58
57
|
constructor(state: DurableObjectState, env: Id<InferCloudflarePluvHandlerEnv<TPluvServer>>) {
|
|
59
|
-
this.
|
|
60
|
-
this._io = io.getRoom(state.id.toString(), { env });
|
|
58
|
+
this._room = io.getRoom(state.id.toString(), { env, state });
|
|
61
59
|
}
|
|
62
60
|
|
|
63
|
-
webSocketClose(ws: WebSocket, code: number, reason: string, wasClean: boolean): void | Promise<void> {
|
|
61
|
+
public webSocketClose(ws: WebSocket, code: number, reason: string, wasClean: boolean): void | Promise<void> {
|
|
62
|
+
if (io._registrationMode !== "detached") return;
|
|
64
63
|
|
|
65
|
-
|
|
64
|
+
const handler = this._room.onClose(ws);
|
|
66
65
|
|
|
67
|
-
|
|
66
|
+
handler({ code, reason });
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public webSocketError(ws: WebSocket, error: unknown): void | Promise<void> {
|
|
70
|
+
if (io._registrationMode !== "detached") return;
|
|
71
|
+
|
|
72
|
+
const handler = this._room.onError(ws);
|
|
73
|
+
|
|
74
|
+
const eventError = error instanceof Error ? error : new Error("Internal Error");
|
|
75
|
+
|
|
76
|
+
handler({ error: eventError, message: eventError.message });
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public webSocketMessage(ws: WebSocket, message: string | ArrayBuffer): void | Promise<void> {
|
|
80
|
+
if (io._registrationMode !== "detached") return;
|
|
81
|
+
|
|
82
|
+
const handler = this._room.onMessage(ws);
|
|
83
|
+
|
|
84
|
+
handler({ data: message });
|
|
85
|
+
}
|
|
68
86
|
|
|
69
87
|
async fetch(request: Request<any, CfProperties<any>>): Promise<Response> {
|
|
70
88
|
const isWSRequest = request.headers.get("Upgrade") === "websocket";
|
|
@@ -77,11 +95,7 @@ export const createPluvHandler = <TPluvServer extends PluvServer<CloudflarePlatf
|
|
|
77
95
|
|
|
78
96
|
const token = new URL(request.url).searchParams.get("token");
|
|
79
97
|
|
|
80
|
-
await this.
|
|
81
|
-
env: this._env,
|
|
82
|
-
request,
|
|
83
|
-
token,
|
|
84
|
-
});
|
|
98
|
+
await this._room.register(server, { token });
|
|
85
99
|
|
|
86
100
|
return new Response(null, { status: 101, webSocket: client });
|
|
87
101
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export type { CloudflarePlatformConfig } from "./CloudflarePlatform";
|
|
2
|
+
export { createPluvHandler } from "./createPluvHandler";
|
|
1
3
|
export type {
|
|
2
4
|
AuthorizeFunction,
|
|
3
5
|
AuthorizeFunctionContext,
|
|
@@ -5,5 +7,4 @@ export type {
|
|
|
5
7
|
CreatePluvHandlerResult,
|
|
6
8
|
PluvHandlerFetch,
|
|
7
9
|
} from "./createPluvHandler";
|
|
8
|
-
export { createPluvHandler } from "./createPluvHandler";
|
|
9
10
|
export { platformCloudflare } from "./platformCloudflare";
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import type { CloudflarePlatformConfig } from "./CloudflarePlatform";
|
|
1
2
|
import { CloudflarePlatform } from "./CloudflarePlatform";
|
|
2
3
|
|
|
3
|
-
export const platformCloudflare = <TEnv extends Record<string, any> = {}>(): CloudflarePlatform<TEnv> => {
|
|
4
|
-
return new CloudflarePlatform<TEnv>();
|
|
4
|
+
export const platformCloudflare = <TEnv extends Record<string, any> = {}>(config: CloudflarePlatformConfig<TEnv> = {}): CloudflarePlatform<TEnv> => {
|
|
5
|
+
return new CloudflarePlatform<TEnv>(config);
|
|
5
6
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { partitionByLength } from "./partitionByLength";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const partitionByLength = <T>(arr: readonly T[] | T[], length: number): readonly (readonly T[])[] => {
|
|
2
|
+
if (!arr.length) return [];
|
|
3
|
+
|
|
4
|
+
const head = arr.slice(0, length);
|
|
5
|
+
const tail = arr.slice(length);
|
|
6
|
+
|
|
7
|
+
return [head, ...partitionByLength(tail, length)];
|
|
8
|
+
};
|