@pluv/platform-cloudflare 5.0.1 → 5.0.3

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.
@@ -0,0 +1,105 @@
1
+ import { AbstractEventMap, AbstractListener, AbstractPlatform, AbstractPlatformConfig, AbstractWebSocket, AbstractWebSocketConfig, ConvertWebSocketConfig, CreateIOParams, InferInitContextType, PluvContext, PluvIOAuthorize, WebSocketRegistrationMode, WebSocketSerializedState, WebSocketSession } from "@pluv/io";
2
+ import { BaseUser, IOAuthorize, Id, InferIOAuthorizeUser, Json, JsonObject } from "@pluv/types";
3
+ import { CrdtLibraryType, NoopCrdtDocFactory } from "@pluv/crdt";
4
+ //#region src/CloudflareWebSocket.d.ts
5
+ type CloudflareWebSocketConfig = AbstractWebSocketConfig;
6
+ declare class CloudflareWebSocket<TAuthorize extends IOAuthorize<any, any> | null = null> extends AbstractWebSocket<WebSocket, TAuthorize> {
7
+ set presence(presence: JsonObject | null);
8
+ get readyState(): 0 | 1 | 2 | 3;
9
+ get session(): WebSocketSession<TAuthorize>;
10
+ get sessionId(): string;
11
+ get state(): WebSocketSerializedState;
12
+ set state(state: WebSocketSerializedState);
13
+ set user(user: InferIOAuthorizeUser<TAuthorize>);
14
+ constructor(webSocket: WebSocket, config: CloudflareWebSocketConfig);
15
+ addEventListener<TType extends keyof AbstractEventMap>(type: TType, handler: AbstractListener<TType>): void;
16
+ close(code?: number, reason?: string): void;
17
+ send(message: string | ArrayBuffer | ArrayBufferView): void;
18
+ terminate(code?: number): void;
19
+ }
20
+ //#endregion
21
+ //#region src/CloudflarePlatform.d.ts
22
+ type CloudflarePlatformRoomContext<TEnv extends Record<string, any>, TMeta extends Record<string, Json>> = {
23
+ env: TEnv;
24
+ state: DurableObjectState;
25
+ } & (keyof TMeta extends never ? {
26
+ meta?: undefined;
27
+ } : {
28
+ meta: TMeta;
29
+ });
30
+ type CloudflarePlatformConfig<TEnv extends Record<string, any> = {}, TMeta extends Record<string, Json> = {}> = AbstractPlatformConfig<CloudflarePlatformRoomContext<TEnv, TMeta>> & {
31
+ mode?: WebSocketRegistrationMode;
32
+ };
33
+ declare class CloudflarePlatform<TAuthorize extends IOAuthorize<any, any> | null = null, TEnv extends Record<string, any> = {}, TMeta extends Record<string, Json> = {}> extends AbstractPlatform<CloudflareWebSocket<TAuthorize>, {
34
+ env: TEnv;
35
+ request: Request;
36
+ }, CloudflarePlatformRoomContext<TEnv, TMeta>, {
37
+ authorize: {
38
+ secret: true;
39
+ };
40
+ handleMode: "io";
41
+ requireAuth: false;
42
+ registrationMode: WebSocketRegistrationMode;
43
+ listeners: {
44
+ onRoomDestroyed: true;
45
+ onRoomMessage: true;
46
+ onStorageDestroyed: true;
47
+ onStorageUpdated: true;
48
+ onUserConnected: true;
49
+ onUserDisconnected: true;
50
+ };
51
+ router: true;
52
+ }> {
53
+ readonly id: string;
54
+ readonly _config: {
55
+ authorize: {
56
+ secret: true;
57
+ };
58
+ handleMode: "io";
59
+ registrationMode: WebSocketRegistrationMode;
60
+ requireAuth: false;
61
+ listeners: {
62
+ onRoomDestroyed: true;
63
+ onRoomMessage: true;
64
+ onStorageDestroyed: true;
65
+ onStorageUpdated: true;
66
+ onUserConnected: true;
67
+ onUserDisconnected: true;
68
+ };
69
+ router: true;
70
+ };
71
+ readonly _name = "platformCloudflare";
72
+ constructor(config: CloudflarePlatformConfig<TEnv, TMeta>);
73
+ acceptWebSocket(webSocket: CloudflareWebSocket<TAuthorize>): Promise<void>;
74
+ convertWebSocket(webSocket: WebSocket, config: ConvertWebSocketConfig): CloudflareWebSocket<TAuthorize>;
75
+ getLastPing(webSocket: CloudflareWebSocket<TAuthorize>): number | null;
76
+ getSerializedState(webSocket: WebSocket): WebSocketSerializedState | null;
77
+ getSessionId(webSocket: WebSocket): string | null;
78
+ getWebSockets(): readonly WebSocket[];
79
+ initialize(config: AbstractPlatformConfig<CloudflarePlatformRoomContext<TEnv, TMeta>>): this;
80
+ parseData(data: string | ArrayBuffer): Record<string, any>;
81
+ randomUUID(): string;
82
+ setSerializedState(webSocket: CloudflareWebSocket<TAuthorize>, state: WebSocketSerializedState): WebSocketSerializedState;
83
+ private _getDetachedState;
84
+ }
85
+ //#endregion
86
+ //#region src/utils/identity.d.ts
87
+ declare const identity: <T extends unknown>(x: T) => T;
88
+ //#endregion
89
+ //#region src/infer.d.ts
90
+ type InferCallback<TEnv extends Record<string, any> = {}, TMeta extends Record<string, Json> = {}> = (i: typeof identity) => {
91
+ env?: (io: TEnv) => TEnv;
92
+ meta?: (io: TMeta) => TMeta;
93
+ };
94
+ declare const infer: <TEnv extends Record<string, any> = {}, TMeta extends Record<string, Json> = {}>(callback: InferCallback<TEnv, TMeta>) => InferCallback<TEnv, TMeta>;
95
+ //#endregion
96
+ //#region src/platformCloudflare.d.ts
97
+ type PlatformCloudflareCreateIOParams<TEnv extends Record<string, any> = {}, TMeta extends Record<string, Json> = {}, TContext extends Record<string, any> = {}, TUser extends BaseUser | null = null, TCrdt extends CrdtLibraryType<any> = CrdtLibraryType<NoopCrdtDocFactory>> = Id<CloudflarePlatformConfig<TEnv, TMeta> & Omit<CreateIOParams<CloudflarePlatform<IOAuthorize<TUser, TContext>, TEnv, TMeta>, TContext, TUser, TCrdt>, "authorize" | "context" | "platform"> & {
98
+ authorize?: PluvIOAuthorize<CloudflarePlatform<IOAuthorize<TUser, TContext>, TEnv, TMeta>, TUser, InferInitContextType<CloudflarePlatform<IOAuthorize<TUser, TContext>, TEnv, TMeta>>>;
99
+ context?: PluvContext<CloudflarePlatform<IOAuthorize<TUser, any>, TEnv, TMeta>, TContext>;
100
+ types?: InferCallback<TEnv, TMeta>;
101
+ }>;
102
+ declare const platformCloudflare: <TEnv extends Record<string, any> = {}, TMeta extends Record<string, Json> = {}, TContext extends Record<string, any> = {}, TUser extends BaseUser | null = null, TCrdt extends CrdtLibraryType<any> = CrdtLibraryType<NoopCrdtDocFactory>>(config?: PlatformCloudflareCreateIOParams<TEnv, TMeta, TContext, TUser, TCrdt>) => CreateIOParams<CloudflarePlatform<IOAuthorize<TUser, TContext>, TEnv, TMeta>, TContext, TUser, TCrdt>;
103
+ //#endregion
104
+ export { type CloudflarePlatform, type CloudflarePlatformConfig, type InferCallback, type PlatformCloudflareCreateIOParams, infer, platformCloudflare };
105
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/CloudflareWebSocket.ts","../src/CloudflarePlatform.ts","../src/utils/identity.ts","../src/infer.ts","../src/platformCloudflare.ts"],"mappings":";;;;KAiBY,4BAA4B;cAE3B,oBACT,mBAAmB,6CACb,kBAAkB,WAAW;MACxB,SAAS,UAAU;MAUnB;MAIA,WAAW,iBAAiB;MAc5B;MAWA,SAAS;MAoBT,MAAM,OAAO;MAMb,KAAK,MAAM,qBAAqB;EAM3C,YAAY,WAAW,WAAW,QAAQ;EAqBnC,iBAAiB,oBAAoB,kBACxC,MAAM,OACN,SAAS,iBAAiB;EAKvB,MAAM,eAAe;EAUrB,KAAK,kBAAkB,cAAc;EAMrC,UAAU;;;;KC7HT,8BACR,aAAa,qBACb,cAAc,eAAe;EAE7B,KAAK;EACL,OAAO;WACA;EAAwB;;EAAuB,MAAM;;KAEpD,yBACR,aAAa,0BACb,cAAc,eAAe,cAC7B,uBAAuB,8BAA8B,MAAM;EAC3D,OAAO;;cAGE,mBACT,mBAAmB,qCACnB,aAAa,0BACb,cAAc,eAAe,oBACvB,iBACN,oBAAoB;EAClB,KAAK;EAAM,SAAS;GACtB,8BAA8B,MAAM;EAEhC;IACI;;EAEJ;EACA;EACA,kBAAkB;EAClB;IACI;IACA;IACA;IACA;IACA;IACA;;EAEJ;;WAGY;WACA;;MAiBJ;;IAEQ;;;;MAIR;MACA;MACA;MACA;MACA;MACA;;;;WA3BI;EAEhB,YAAY,QAAQ,yBAAyB,MAAM;EA0CtC,gBAAgB,WAAW,oBAAoB,cAAc;EAYnE,iBACH,WAAW,WACX,QAAQ,yBACT,oBAAoB;EAUhB,YAAY,WAAW,oBAAoB;EAU3C,mBAAmB,WAAW,YAAY;EAM1C,aAAa,WAAW;EASxB,0BAA0B;EAU1B,WACH,QAAQ,uBAAuB,8BAA8B,MAAM;EAoBhE,UAAU,eAAe,cAAc;EAQvC;EAIA,mBACH,WAAW,oBAAoB,aAC/B,OAAO,2BACR;UAQK;;;;cC3MC,WAAY,mBAAiB,GAAK,MAAI;;;KCGvC,cACR,aAAa,0BACb,cAAc,eAAe,eAC5B,UAAU;EACX,OAAO,IAAI,SAAS;EACpB,QAAQ,IAAI,UAAU;;cAGb,QACT,aAAa,0BACb,cAAc,eAAe,YAAU,UAE7B,cAAc,MAAM,WAAM,cAAA,MAAA;;;KCR5B,iCACR,aAAa,0BACb,cAAc,eAAe,YAC7B,iBAAiB,0BACjB,cAAc,wBACd,cAAc,uBAAuB,gBAAgB,uBACrD,GACA,yBAAyB,MAAM,SAC3B,KACI,eACI,mBAAmB,YAAY,OAAO,WAAW,MAAM,QACvD,UACA,OACA;EAIJ,YAAY,gBACR,mBAAmB,YAAY,OAAO,WAAW,MAAM,QACvD,OACA,qBAAqB,mBAAmB,YAAY,OAAO,WAAW,MAAM;EAEhF,UAAU,YACN,mBAAmB,YAAY,aAAa,MAAM,QAClD;EAEJ,QAAQ,cAAc,MAAM;;cAI3B,qBACT,aAAa,0BACb,cAAc,eAAe,YAC7B,iBAAiB,0BACjB,cAAc,wBACd,cAAc,uBAAuB,gBAAgB,qBAAmB,SAEhE,iCAAiC,MAAM,OAAO,UAAU,OAAO,WACxE,eACC,mBAAmB,YAAY,OAAO,WAAW,MAAM,QACvD,UACA,OACA"}
package/dist/index.mjs ADDED
@@ -0,0 +1,223 @@
1
+ import { AbstractPlatform, AbstractWebSocket } from "@pluv/io";
2
+ import { PersistenceCloudflareTransactionalStorage } from "@pluv/persistence-cloudflare-transactional-storage";
3
+ //#region src/infer.ts
4
+ const infer = (callback) => callback;
5
+ //#endregion
6
+ //#region src/CloudflareWebSocket.ts
7
+ var CloudflareWebSocket = class extends AbstractWebSocket {
8
+ set presence(presence) {
9
+ const state = this.webSocket.deserializeAttachment().state;
10
+ this.webSocket.serializeAttachment({
11
+ ...this.webSocket.deserializeAttachment(),
12
+ state: {
13
+ ...state,
14
+ presence
15
+ }
16
+ });
17
+ }
18
+ get readyState() {
19
+ return this.webSocket.readyState;
20
+ }
21
+ get session() {
22
+ const deserialized = this.webSocket.deserializeAttachment();
23
+ const sessionId = deserialized.sessionId;
24
+ const state = this.state;
25
+ const user = deserialized.user ?? null;
26
+ return {
27
+ ...state,
28
+ id: sessionId,
29
+ user,
30
+ webSocket: this
31
+ };
32
+ }
33
+ get sessionId() {
34
+ const deserialized = this.webSocket.deserializeAttachment() ?? {};
35
+ const sessionId = deserialized.sessionId ?? `p_${crypto.randomUUID()}`;
36
+ if (typeof deserialized.sessionId !== "string") this.webSocket.serializeAttachment({
37
+ ...deserialized,
38
+ sessionId
39
+ });
40
+ return sessionId;
41
+ }
42
+ get state() {
43
+ const state = this.webSocket.deserializeAttachment()?.state ?? null;
44
+ if (!state) throw new Error("Could not get websocket state");
45
+ const currentPing = state.timers.ping;
46
+ const lastPing = this._platform.getLastPing(this);
47
+ if (!lastPing) return state;
48
+ if (currentPing >= lastPing) return state;
49
+ const newState = {
50
+ ...state,
51
+ timers: {
52
+ ...state.timers,
53
+ ping: lastPing
54
+ }
55
+ };
56
+ return this._platform.setSerializedState(this, newState);
57
+ }
58
+ set state(state) {
59
+ const deserialized = this.webSocket.deserializeAttachment();
60
+ this.webSocket.serializeAttachment({
61
+ ...deserialized,
62
+ state
63
+ });
64
+ }
65
+ set user(user) {
66
+ const deserialized = this.webSocket.deserializeAttachment();
67
+ this.webSocket.serializeAttachment({
68
+ ...deserialized,
69
+ user
70
+ });
71
+ }
72
+ constructor(webSocket, config) {
73
+ const { room } = config;
74
+ super(webSocket, config);
75
+ const state = {
76
+ presence: null,
77
+ quit: false,
78
+ room,
79
+ timers: {
80
+ ping: (/* @__PURE__ */ new Date()).getTime(),
81
+ presence: null
82
+ }
83
+ };
84
+ webSocket.serializeAttachment({
85
+ state,
86
+ ...webSocket.deserializeAttachment()
87
+ });
88
+ }
89
+ addEventListener(type, handler) {
90
+ this.webSocket.addEventListener(type, handler);
91
+ }
92
+ close(code, reason) {
93
+ if (![this.CONNECTING, this.OPEN].some((readyState) => readyState === this.readyState)) return;
94
+ this.webSocket.close(code, reason);
95
+ }
96
+ send(message) {
97
+ if (this.readyState !== this.OPEN) return;
98
+ this.webSocket.send(message);
99
+ }
100
+ terminate(code = 1011) {
101
+ return this.webSocket.close(code, "Terminated");
102
+ }
103
+ };
104
+ //#endregion
105
+ //#region src/CloudflarePlatform.ts
106
+ var CloudflarePlatform = class CloudflarePlatform extends AbstractPlatform {
107
+ id = crypto.randomUUID();
108
+ _config;
109
+ _name = "platformCloudflare";
110
+ constructor(config) {
111
+ super({
112
+ ...config,
113
+ ...config.roomContext && config.mode === "detached" ? { persistence: config.persistence ?? new PersistenceCloudflareTransactionalStorage({ mode: "sqlite" }) } : {}
114
+ });
115
+ this._config = {
116
+ authorize: { secret: true },
117
+ handleMode: "io",
118
+ registrationMode: config.mode ?? "detached",
119
+ requireAuth: false,
120
+ listeners: {
121
+ onRoomDestroyed: true,
122
+ onRoomMessage: true,
123
+ onStorageDestroyed: true,
124
+ onStorageUpdated: true,
125
+ onUserConnected: true,
126
+ onUserDisconnected: true
127
+ },
128
+ router: true
129
+ };
130
+ const detachedState = this._getDetachedState();
131
+ if (!detachedState) return;
132
+ detachedState.setWebSocketAutoResponse(new WebSocketRequestResponsePair("{\"type\":\"$ping\",\"data\":{}}", JSON.stringify({
133
+ type: "$pong",
134
+ data: {}
135
+ })));
136
+ }
137
+ async acceptWebSocket(webSocket) {
138
+ const detachedState = this._getDetachedState();
139
+ if (!detachedState) {
140
+ webSocket.webSocket.accept();
141
+ return;
142
+ }
143
+ detachedState.acceptWebSocket(webSocket.webSocket);
144
+ }
145
+ convertWebSocket(webSocket, config) {
146
+ const { room } = config;
147
+ return new CloudflareWebSocket(webSocket, {
148
+ persistence: this.persistence,
149
+ platform: this,
150
+ room
151
+ });
152
+ }
153
+ getLastPing(webSocket) {
154
+ const detachedState = this._getDetachedState();
155
+ if (!detachedState) return null;
156
+ return detachedState.getWebSocketAutoResponseTimestamp(webSocket.webSocket)?.getTime() ?? null;
157
+ }
158
+ getSerializedState(webSocket) {
159
+ return webSocket.deserializeAttachment()?.state ?? null;
160
+ }
161
+ getSessionId(webSocket) {
162
+ const sessionId = (webSocket.deserializeAttachment() ?? {}).sessionId;
163
+ if (typeof sessionId !== "string") return null;
164
+ return sessionId;
165
+ }
166
+ getWebSockets() {
167
+ const detachedState = this._getDetachedState();
168
+ if (!detachedState) return [];
169
+ return detachedState.getWebSockets() ?? [];
170
+ }
171
+ initialize(config) {
172
+ const ctx = config.roomContext ?? { ...this._roomContext };
173
+ if (!ctx.env || !ctx.state) throw new Error("Could not derive platform roomContext");
174
+ const roomContext = {
175
+ env: ctx.env,
176
+ meta: ctx.meta,
177
+ state: ctx.state
178
+ };
179
+ return new CloudflarePlatform({
180
+ roomContext,
181
+ mode: this._config.registrationMode,
182
+ persistence: this.persistence.initialize(roomContext),
183
+ pubSub: this.pubSub
184
+ })._initialize();
185
+ }
186
+ parseData(data) {
187
+ if (typeof data === "string") return JSON.parse(data);
188
+ const decoder = new TextDecoder("utf8");
189
+ return JSON.parse(decoder.decode(data));
190
+ }
191
+ randomUUID() {
192
+ return crypto.randomUUID();
193
+ }
194
+ setSerializedState(webSocket, state) {
195
+ const deserialized = webSocket.webSocket.deserializeAttachment() ?? {};
196
+ webSocket.webSocket.serializeAttachment({
197
+ ...deserialized,
198
+ state
199
+ });
200
+ return state;
201
+ }
202
+ _getDetachedState() {
203
+ if (this._config.registrationMode !== "detached") return null;
204
+ return this._roomContext?.state ?? null;
205
+ }
206
+ };
207
+ //#endregion
208
+ //#region src/platformCloudflare.ts
209
+ const platformCloudflare = (config = {}) => {
210
+ const { authorize, context, crdt, debug, limits } = config;
211
+ return {
212
+ authorize,
213
+ context,
214
+ crdt,
215
+ debug,
216
+ limits,
217
+ platform: () => new CloudflarePlatform(config)
218
+ };
219
+ };
220
+ //#endregion
221
+ export { infer, platformCloudflare };
222
+
223
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/infer.ts","../src/CloudflareWebSocket.ts","../src/constants.ts","../src/CloudflarePlatform.ts","../src/platformCloudflare.ts"],"sourcesContent":["import type { Json } from \"@pluv/types\";\nimport type { identity } from \"./utils\";\n\nexport type InferCallback<\n TEnv extends Record<string, any> = {},\n TMeta extends Record<string, Json> = {},\n> = (i: typeof identity) => {\n env?: (io: TEnv) => TEnv;\n meta?: (io: TMeta) => TMeta;\n};\n\nexport const infer = <\n TEnv extends Record<string, any> = {},\n TMeta extends Record<string, Json> = {},\n>(\n callback: InferCallback<TEnv, TMeta>,\n) => callback;\n","import type {\n AbstractEventMap,\n AbstractListener,\n AbstractWebSocketConfig,\n WebSocketSerializedState,\n WebSocketSession,\n} from \"@pluv/io\";\nimport { AbstractWebSocket } from \"@pluv/io\";\nimport type { InferIOAuthorizeUser, IOAuthorize, JsonObject } from \"@pluv/types\";\n\nexport interface CloudflareWebSocketEventMap {\n close: CloseEvent;\n message: MessageEvent;\n open: Event;\n error: ErrorEvent;\n}\n\nexport type CloudflareWebSocketConfig = AbstractWebSocketConfig;\n\nexport class CloudflareWebSocket<\n TAuthorize extends IOAuthorize<any, any> | null = null,\n> extends AbstractWebSocket<WebSocket, TAuthorize> {\n public set presence(presence: JsonObject | null) {\n const deserialized = this.webSocket.deserializeAttachment();\n const state = deserialized.state;\n\n this.webSocket.serializeAttachment({\n ...this.webSocket.deserializeAttachment(),\n state: { ...state, presence },\n });\n }\n\n public get readyState(): 0 | 1 | 2 | 3 {\n return this.webSocket.readyState as 0 | 1 | 2 | 3;\n }\n\n public get session(): WebSocketSession<TAuthorize> {\n const deserialized = this.webSocket.deserializeAttachment();\n const sessionId = deserialized.sessionId as string;\n const state = this.state;\n const user = (deserialized.user ?? null) as InferIOAuthorizeUser<TAuthorize>;\n\n return {\n ...state,\n id: sessionId,\n user,\n webSocket: this,\n };\n }\n\n public get sessionId(): string {\n const deserialized = this.webSocket.deserializeAttachment() ?? {};\n const sessionId = deserialized.sessionId ?? `p_${crypto.randomUUID()}`;\n\n if (typeof deserialized.sessionId !== \"string\") {\n this.webSocket.serializeAttachment({ ...deserialized, sessionId });\n }\n\n return sessionId;\n }\n\n public get state(): WebSocketSerializedState {\n const deserialized = this.webSocket.deserializeAttachment();\n const state = deserialized?.state ?? null;\n\n if (!state) throw new Error(\"Could not get websocket state\");\n\n const currentPing = state.timers.ping;\n const lastPing = this._platform.getLastPing(this);\n\n if (!lastPing) return state;\n if (currentPing >= lastPing) return state;\n\n const newState: WebSocketSerializedState = {\n ...state,\n timers: { ...state.timers, ping: lastPing },\n };\n\n return this._platform.setSerializedState(this, newState);\n }\n\n public set state(state: WebSocketSerializedState) {\n const deserialized = this.webSocket.deserializeAttachment();\n\n this.webSocket.serializeAttachment({ ...deserialized, state });\n }\n\n public set user(user: InferIOAuthorizeUser<TAuthorize>) {\n const deserialized = this.webSocket.deserializeAttachment();\n\n this.webSocket.serializeAttachment({ ...deserialized, user });\n }\n\n constructor(webSocket: WebSocket, config: CloudflareWebSocketConfig) {\n const { room } = config;\n\n super(webSocket, config);\n\n const state: WebSocketSerializedState = {\n presence: null,\n quit: false,\n room,\n timers: {\n ping: new Date().getTime(),\n presence: null,\n },\n };\n\n webSocket.serializeAttachment({\n state,\n ...webSocket.deserializeAttachment(),\n });\n }\n\n public addEventListener<TType extends keyof AbstractEventMap>(\n type: TType,\n handler: AbstractListener<TType>,\n ) {\n this.webSocket.addEventListener(type, handler as any);\n }\n\n public close(code?: number, reason?: string): void {\n const canClose = [this.CONNECTING, this.OPEN].some(\n (readyState) => readyState === this.readyState,\n );\n\n if (!canClose) return;\n\n this.webSocket.close(code, reason);\n }\n\n public send(message: string | ArrayBuffer | ArrayBufferView): void {\n if (this.readyState !== this.OPEN) return;\n\n this.webSocket.send(message);\n }\n\n public terminate(code: number = 1011): void {\n return this.webSocket.close(code, \"Terminated\");\n }\n}\n","import type { WebSocketRegistrationMode } from \"@pluv/io\";\n\nexport const DEFAULT_REGISTRATION_MODE: WebSocketRegistrationMode = \"detached\";\nexport const GARBAGE_COLLECT_INTERVAL_MS = 60_000;\n","import type {\n AbstractPlatformConfig,\n ConvertWebSocketConfig,\n WebSocketRegistrationMode,\n WebSocketSerializedState,\n} from \"@pluv/io\";\nimport { AbstractPlatform } from \"@pluv/io\";\nimport { PersistenceCloudflareTransactionalStorage } from \"@pluv/persistence-cloudflare-transactional-storage\";\nimport type { IOAuthorize, Json } from \"@pluv/types\";\nimport { CloudflareWebSocket } from \"./CloudflareWebSocket\";\nimport { DEFAULT_REGISTRATION_MODE } from \"./constants\";\n\nexport type CloudflarePlatformRoomContext<\n TEnv extends Record<string, any>,\n TMeta extends Record<string, Json>,\n> = {\n env: TEnv;\n state: DurableObjectState;\n} & (keyof TMeta extends never ? { meta?: undefined } : { meta: TMeta });\n\nexport type CloudflarePlatformConfig<\n TEnv extends Record<string, any> = {},\n TMeta extends Record<string, Json> = {},\n> = AbstractPlatformConfig<CloudflarePlatformRoomContext<TEnv, TMeta>> & {\n mode?: WebSocketRegistrationMode;\n};\n\nexport class CloudflarePlatform<\n TAuthorize extends IOAuthorize<any, any> | null = null,\n TEnv extends Record<string, any> = {},\n TMeta extends Record<string, Json> = {},\n> extends AbstractPlatform<\n CloudflareWebSocket<TAuthorize>,\n { env: TEnv; request: Request },\n CloudflarePlatformRoomContext<TEnv, TMeta>,\n {\n authorize: {\n secret: true;\n };\n handleMode: \"io\";\n requireAuth: false;\n registrationMode: WebSocketRegistrationMode;\n listeners: {\n onRoomDestroyed: true;\n onRoomMessage: true;\n onStorageDestroyed: true;\n onStorageUpdated: true;\n onUserConnected: true;\n onUserDisconnected: true;\n };\n router: true;\n }\n> {\n public readonly id = crypto.randomUUID();\n public readonly _config;\n public readonly _name = \"platformCloudflare\";\n\n constructor(config: CloudflarePlatformConfig<TEnv, TMeta>) {\n super({\n ...config,\n ...(config.roomContext && config.mode === \"detached\"\n ? {\n persistence:\n config.persistence ??\n new PersistenceCloudflareTransactionalStorage({ mode: \"sqlite\" }),\n }\n : {}),\n });\n\n this._config = {\n authorize: {\n secret: true as const,\n },\n handleMode: \"io\" as const,\n registrationMode: config.mode ?? DEFAULT_REGISTRATION_MODE,\n requireAuth: false as const,\n listeners: {\n onRoomDestroyed: true as const,\n onRoomMessage: true as const,\n onStorageDestroyed: true as const,\n onStorageUpdated: true as const,\n onUserConnected: true as const,\n onUserDisconnected: true as const,\n },\n router: true as const,\n };\n\n const detachedState = this._getDetachedState();\n\n if (!detachedState) return;\n\n detachedState.setWebSocketAutoResponse(\n new WebSocketRequestResponsePair(\n '{\"type\":\"$ping\",\"data\":{}}',\n JSON.stringify({ type: \"$pong\", data: {} }),\n ),\n );\n }\n\n public async acceptWebSocket(webSocket: CloudflareWebSocket<TAuthorize>): Promise<void> {\n const detachedState = this._getDetachedState();\n\n if (!detachedState) {\n webSocket.webSocket.accept();\n\n return;\n }\n\n detachedState.acceptWebSocket(webSocket.webSocket);\n }\n\n public convertWebSocket(\n webSocket: WebSocket,\n config: ConvertWebSocketConfig,\n ): CloudflareWebSocket<TAuthorize> {\n const { room } = config;\n\n return new CloudflareWebSocket<TAuthorize>(webSocket, {\n persistence: this.persistence,\n platform: this,\n room,\n });\n }\n\n public getLastPing(webSocket: CloudflareWebSocket<TAuthorize>): number | null {\n const detachedState = this._getDetachedState();\n\n if (!detachedState) return null;\n\n const timestamp = detachedState.getWebSocketAutoResponseTimestamp(webSocket.webSocket);\n\n return timestamp?.getTime() ?? null;\n }\n\n public getSerializedState(webSocket: WebSocket): WebSocketSerializedState | null {\n const deserialized = webSocket.deserializeAttachment();\n\n return deserialized?.state ?? null;\n }\n\n public getSessionId(webSocket: WebSocket): string | null {\n const deserialized = webSocket.deserializeAttachment() ?? {};\n const sessionId = deserialized.sessionId;\n\n if (typeof sessionId !== \"string\") return null;\n\n return sessionId;\n }\n\n public getWebSockets(): readonly WebSocket[] {\n const detachedState = this._getDetachedState();\n\n if (!detachedState) return [];\n\n const webSockets = detachedState.getWebSockets() ?? [];\n\n return webSockets;\n }\n\n public initialize(\n config: AbstractPlatformConfig<CloudflarePlatformRoomContext<TEnv, TMeta>>,\n ): this {\n const ctx = config.roomContext ?? { ...this._roomContext };\n\n if (!ctx.env || !ctx.state) throw new Error(\"Could not derive platform roomContext\");\n\n const roomContext = {\n env: ctx.env,\n meta: ctx.meta,\n state: ctx.state,\n } as CloudflarePlatformRoomContext<TEnv, TMeta>;\n\n return new CloudflarePlatform<TAuthorize, TEnv, TMeta>({\n roomContext,\n mode: this._config.registrationMode,\n persistence: this.persistence.initialize(roomContext),\n pubSub: this.pubSub,\n })._initialize() as this;\n }\n\n public parseData(data: string | ArrayBuffer): Record<string, any> {\n if (typeof data === \"string\") return JSON.parse(data);\n\n const decoder = new TextDecoder(\"utf8\");\n\n return JSON.parse(decoder.decode(data));\n }\n\n public randomUUID(): string {\n return crypto.randomUUID();\n }\n\n public setSerializedState(\n webSocket: CloudflareWebSocket<TAuthorize>,\n state: WebSocketSerializedState,\n ): WebSocketSerializedState {\n const deserialized = webSocket.webSocket.deserializeAttachment() ?? {};\n\n webSocket.webSocket.serializeAttachment({ ...deserialized, state });\n\n return state;\n }\n\n private _getDetachedState(): DurableObjectState | null {\n if (this._config.registrationMode !== \"detached\") return null;\n\n const detachedState = this._roomContext?.state ?? null;\n\n return detachedState;\n }\n}\n","import type { CrdtLibraryType, NoopCrdtDocFactory } from \"@pluv/crdt\";\nimport type { CreateIOParams, InferInitContextType, PluvContext, PluvIOAuthorize } from \"@pluv/io\";\nimport type { BaseUser, Id, IOAuthorize, Json } from \"@pluv/types\";\nimport type { CloudflarePlatformConfig } from \"./CloudflarePlatform\";\nimport { CloudflarePlatform } from \"./CloudflarePlatform\";\nimport type { InferCallback } from \"./infer\";\n\nexport type PlatformCloudflareCreateIOParams<\n TEnv extends Record<string, any> = {},\n TMeta extends Record<string, Json> = {},\n TContext extends Record<string, any> = {},\n TUser extends BaseUser | null = null,\n TCrdt extends CrdtLibraryType<any> = CrdtLibraryType<NoopCrdtDocFactory>,\n> = Id<\n CloudflarePlatformConfig<TEnv, TMeta> &\n Omit<\n CreateIOParams<\n CloudflarePlatform<IOAuthorize<TUser, TContext>, TEnv, TMeta>,\n TContext,\n TUser,\n TCrdt\n >,\n \"authorize\" | \"context\" | \"platform\"\n > & {\n authorize?: PluvIOAuthorize<\n CloudflarePlatform<IOAuthorize<TUser, TContext>, TEnv, TMeta>,\n TUser,\n InferInitContextType<CloudflarePlatform<IOAuthorize<TUser, TContext>, TEnv, TMeta>>\n >;\n context?: PluvContext<\n CloudflarePlatform<IOAuthorize<TUser, any>, TEnv, TMeta>,\n TContext\n >;\n types?: InferCallback<TEnv, TMeta>;\n }\n>;\n\nexport const platformCloudflare = <\n TEnv extends Record<string, any> = {},\n TMeta extends Record<string, Json> = {},\n TContext extends Record<string, any> = {},\n TUser extends BaseUser | null = null,\n TCrdt extends CrdtLibraryType<any> = CrdtLibraryType<NoopCrdtDocFactory>,\n>(\n config: PlatformCloudflareCreateIOParams<TEnv, TMeta, TContext, TUser, TCrdt> = {},\n): CreateIOParams<\n CloudflarePlatform<IOAuthorize<TUser, TContext>, TEnv, TMeta>,\n TContext,\n TUser,\n TCrdt\n> => {\n const { authorize, context, crdt, debug, limits } = config;\n\n return {\n authorize,\n context,\n crdt,\n debug,\n limits,\n platform: () => new CloudflarePlatform<IOAuthorize<TUser, TContext>, TEnv, TMeta>(config),\n };\n};\n"],"mappings":";;;AAWA,MAAa,SAIT,aACC;;;ACGL,IAAa,sBAAb,cAEU,kBAAyC;CAC/C,IAAW,SAAS,UAA6B;EAE7C,MAAM,QADe,KAAK,UAAU,sBACX,CAAC,CAAC;EAE3B,KAAK,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AC,CAAC,CAAC,YAAY;CACnB;CAEA,UAAiB,MAAiD;EAC9D,IAAI,OAAO,SAAS,UAAU,OAAO,KAAK,MAAM,IAAI;EAEpD,MAAM,UAAU,IAAI,YAAY,MAAM;EAEtC,OAAO,KAAK,MAAM,QAAQ,OAAO,IAAI,CAAC;CAC1C;CAEA,aAA4B;EACxB,OAAO,OAAO,WAAW;CAC7B;CAEA,mBACI,WACA,OACwB;EACxB,MAAM,eAAe,UAAU,UAAU,sBAAsB,KAAK,CAAC;EAErE,UAAU,UAAU,oBAAoB;GAAE,GAAG;GAAc;EAAM,CAAC;EAElE,OAAO;CACX;CAEA,oBAAuD;EACnD,IAAI,KAAK,QAAQ,qBAAqB,YAAY,OAAO;EAIzD,OAFsB,KAAK,cAAc,SAAS;CAGtD;AACJ;;;AC7KA,MAAa,sBAOT,SAAgF,CAAC,MAMhF;CACD,MAAM,EAAE,WAAW,SAAS,MAAM,OAAO,WAAW;CAEpD,OAAO;EACH;EACA;EACA;EACA;EACA;EACA,gBAAgB,IAAI,mBAA8D,MAAM;CAC5F;AACJ"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pluv/platform-cloudflare",
3
- "version": "5.0.1",
3
+ "version": "5.0.3",
4
4
  "description": "@pluv/io adapter for cloudflare workers",
5
5
  "author": {
6
6
  "email": "david@pluv.io",
@@ -16,23 +16,24 @@
16
16
  },
17
17
  "type": "module",
18
18
  "types": "./dist/index.d.mts",
19
+ "files": [
20
+ "dist"
21
+ ],
19
22
  "publishConfig": {
20
23
  "access": "public"
21
24
  },
22
25
  "dependencies": {
23
- "@pluv/crdt": "^5.0.1",
24
- "@pluv/io": "^5.0.1",
25
- "@pluv/persistence-cloudflare-transactional-storage": "^5.0.1",
26
- "@pluv/types": "^5.0.1",
26
+ "@pluv/crdt": "^5.0.3",
27
+ "@pluv/io": "^5.0.3",
28
+ "@pluv/persistence-cloudflare-transactional-storage": "^5.0.3",
29
+ "@pluv/types": "^5.0.3",
27
30
  "path-to-regexp": "^8.4.2"
28
31
  },
29
32
  "devDependencies": {
30
- "@cloudflare/workers-types": "^5.20260823.1",
31
- "@pluv/tsconfig": "^5.0.1",
32
- "eslint": "^10.9.0",
33
- "eslint-config-pluv": "^5.0.1",
33
+ "@cloudflare/workers-types": "^5.20260830.1",
34
+ "@pluv/tsconfig": "^5.0.3",
34
35
  "tsdown": "0.22.14",
35
- "typescript": "^5.9.3"
36
+ "typescript": "^7.0.2"
36
37
  },
37
38
  "exports": {
38
39
  ".": "./dist/index.mjs",
@@ -41,7 +42,6 @@
41
42
  "scripts": {
42
43
  "build": "tsdown",
43
44
  "dev": "tsdown --watch",
44
- "lint": "eslint \"src/**/*.ts*\" --fix --max-warnings 0",
45
45
  "typecheck": "tsc --noEmit",
46
46
  "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
47
47
  }