@pluv/platform-cloudflare 0.37.3 → 0.37.5

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.
@@ -1,5 +1,5 @@
1
1
 
2
- > @pluv/platform-cloudflare@0.37.3 build /home/runner/work/pluv/pluv/packages/platform-cloudflare
2
+ > @pluv/platform-cloudflare@0.37.5 build /home/runner/work/pluv/pluv/packages/platform-cloudflare
3
3
  > tsup src/index.ts --format esm,cjs --dts
4
4
 
5
5
  CLI Building entry: src/index.ts
@@ -8,11 +8,11 @@
8
8
  CLI Target: es6
9
9
  ESM Build start
10
10
  CJS Build start
11
- ESM dist/index.mjs 12.36 KB
12
- ESM ⚡️ Build success in 107ms
13
- CJS dist/index.js 13.49 KB
14
- CJS ⚡️ Build success in 108ms
11
+ CJS dist/index.js 13.95 KB
12
+ CJS ⚡️ Build success in 41ms
13
+ ESM dist/index.mjs 12.82 KB
14
+ ESM ⚡️ Build success in 42ms
15
15
  DTS Build start
16
- DTS ⚡️ Build success in 1082ms
17
- DTS dist/index.d.mts 6.61 KB
18
- DTS dist/index.d.ts 6.61 KB
16
+ DTS ⚡️ Build success in 968ms
17
+ DTS dist/index.d.mts 6.63 KB
18
+ DTS dist/index.d.ts 6.63 KB
package/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @pluv/platform-cloudflare
2
2
 
3
+ ## 0.37.5
4
+
5
+ ### Patch Changes
6
+
7
+ - 9a9ecee: Fixed an issue where hibernated Cloudflare Worker websockets were not updating their last ping timer and causing them to be disconnected after some time.
8
+ - Updated dependencies [9a9ecee]
9
+ - @pluv/io@0.37.5
10
+ - @pluv/persistence-cloudflare-transactional-storage@0.37.5
11
+ - @pluv/types@0.37.5
12
+
13
+ ## 0.37.4
14
+
15
+ ### Patch Changes
16
+
17
+ - @pluv/io@0.37.4
18
+ - @pluv/persistence-cloudflare-transactional-storage@0.37.4
19
+ - @pluv/types@0.37.4
20
+
3
21
  ## 0.37.3
4
22
 
5
23
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -72,7 +72,7 @@ declare class CloudflarePlatform<TEnv extends Record<string, any> = {}, TMeta ex
72
72
  initialize(config: AbstractPlatformConfig<CloudflarePlatformRoomContext<TEnv, TMeta>>): this;
73
73
  parseData(data: string | ArrayBuffer): Record<string, any>;
74
74
  randomUUID(): string;
75
- setSerializedState(webSocket: CloudflareWebSocket, state: WebSocketSerializedState): void;
75
+ setSerializedState(webSocket: CloudflareWebSocket, state: WebSocketSerializedState): WebSocketSerializedState;
76
76
  private _getDetachedState;
77
77
  }
78
78
 
package/dist/index.d.ts CHANGED
@@ -72,7 +72,7 @@ declare class CloudflarePlatform<TEnv extends Record<string, any> = {}, TMeta ex
72
72
  initialize(config: AbstractPlatformConfig<CloudflarePlatformRoomContext<TEnv, TMeta>>): this;
73
73
  parseData(data: string | ArrayBuffer): Record<string, any>;
74
74
  randomUUID(): string;
75
- setSerializedState(webSocket: CloudflareWebSocket, state: WebSocketSerializedState): void;
75
+ setSerializedState(webSocket: CloudflareWebSocket, state: WebSocketSerializedState): WebSocketSerializedState;
76
76
  private _getDetachedState;
77
77
  }
78
78
 
package/dist/index.js CHANGED
@@ -218,7 +218,14 @@ var CloudflareWebSocket = class extends import_io.AbstractWebSocket {
218
218
  const deserialized = this.webSocket.deserializeAttachment();
219
219
  const state = (_a = deserialized == null ? void 0 : deserialized.state) != null ? _a : null;
220
220
  if (!state) throw new Error("Could not get websocket state");
221
- return state;
221
+ const currentPing = state.timers.ping;
222
+ const lastPing = this._platform.getLastPing(this);
223
+ if (!lastPing) return state;
224
+ if (currentPing >= lastPing) return state;
225
+ const newState = __spreadProps(__spreadValues({}, state), {
226
+ timers: __spreadProps(__spreadValues({}, state.timers), { ping: lastPing })
227
+ });
228
+ return this._platform.setSerializedState(this, newState);
222
229
  }
223
230
  set state(state) {
224
231
  const deserialized = this.webSocket.deserializeAttachment();
@@ -298,7 +305,7 @@ var CloudflarePlatform = class _CloudflarePlatform extends import_io2.AbstractPl
298
305
  }
299
306
  convertWebSocket(webSocket, config) {
300
307
  const { room } = config;
301
- return new CloudflareWebSocket(webSocket, { persistence: this.persistence, room });
308
+ return new CloudflareWebSocket(webSocket, { persistence: this.persistence, platform: this, room });
302
309
  }
303
310
  getLastPing(webSocket) {
304
311
  var _a;
@@ -323,14 +330,13 @@ var CloudflarePlatform = class _CloudflarePlatform extends import_io2.AbstractPl
323
330
  var _a;
324
331
  const detachedState = this._getDetachedState();
325
332
  if (!detachedState) return [];
326
- return (_a = detachedState.getWebSockets()) != null ? _a : [];
333
+ const webSockets = (_a = detachedState.getWebSockets()) != null ? _a : [];
334
+ return webSockets;
327
335
  }
328
336
  initialize(config) {
329
337
  var _a;
330
338
  const roomContext = (_a = config.roomContext) != null ? _a : __spreadValues({}, this._roomContext);
331
- if (!roomContext.env || !roomContext.state) {
332
- throw new Error("Could not derive platform roomContext");
333
- }
339
+ if (!roomContext.env || !roomContext.state) throw new Error("Could not derive platform roomContext");
334
340
  return new _CloudflarePlatform({
335
341
  roomContext: {
336
342
  env: roomContext.env,
@@ -354,11 +360,13 @@ var CloudflarePlatform = class _CloudflarePlatform extends import_io2.AbstractPl
354
360
  var _a;
355
361
  const deserialized = (_a = webSocket.webSocket.deserializeAttachment()) != null ? _a : {};
356
362
  webSocket.webSocket.serializeAttachment(__spreadProps(__spreadValues({}, deserialized), { state }));
363
+ return state;
357
364
  }
358
365
  _getDetachedState() {
359
366
  var _a, _b;
360
367
  if (this._config.registrationMode !== "detached") return null;
361
- return (_b = (_a = this._roomContext) == null ? void 0 : _a.state) != null ? _b : null;
368
+ const detachedState = (_b = (_a = this._roomContext) == null ? void 0 : _a.state) != null ? _b : null;
369
+ return detachedState;
362
370
  }
363
371
  };
364
372
 
package/dist/index.mjs CHANGED
@@ -193,7 +193,14 @@ var CloudflareWebSocket = class extends AbstractWebSocket {
193
193
  const deserialized = this.webSocket.deserializeAttachment();
194
194
  const state = (_a = deserialized == null ? void 0 : deserialized.state) != null ? _a : null;
195
195
  if (!state) throw new Error("Could not get websocket state");
196
- return state;
196
+ const currentPing = state.timers.ping;
197
+ const lastPing = this._platform.getLastPing(this);
198
+ if (!lastPing) return state;
199
+ if (currentPing >= lastPing) return state;
200
+ const newState = __spreadProps(__spreadValues({}, state), {
201
+ timers: __spreadProps(__spreadValues({}, state.timers), { ping: lastPing })
202
+ });
203
+ return this._platform.setSerializedState(this, newState);
197
204
  }
198
205
  set state(state) {
199
206
  const deserialized = this.webSocket.deserializeAttachment();
@@ -273,7 +280,7 @@ var CloudflarePlatform = class _CloudflarePlatform extends AbstractPlatform {
273
280
  }
274
281
  convertWebSocket(webSocket, config) {
275
282
  const { room } = config;
276
- return new CloudflareWebSocket(webSocket, { persistence: this.persistence, room });
283
+ return new CloudflareWebSocket(webSocket, { persistence: this.persistence, platform: this, room });
277
284
  }
278
285
  getLastPing(webSocket) {
279
286
  var _a;
@@ -298,14 +305,13 @@ var CloudflarePlatform = class _CloudflarePlatform extends AbstractPlatform {
298
305
  var _a;
299
306
  const detachedState = this._getDetachedState();
300
307
  if (!detachedState) return [];
301
- return (_a = detachedState.getWebSockets()) != null ? _a : [];
308
+ const webSockets = (_a = detachedState.getWebSockets()) != null ? _a : [];
309
+ return webSockets;
302
310
  }
303
311
  initialize(config) {
304
312
  var _a;
305
313
  const roomContext = (_a = config.roomContext) != null ? _a : __spreadValues({}, this._roomContext);
306
- if (!roomContext.env || !roomContext.state) {
307
- throw new Error("Could not derive platform roomContext");
308
- }
314
+ if (!roomContext.env || !roomContext.state) throw new Error("Could not derive platform roomContext");
309
315
  return new _CloudflarePlatform({
310
316
  roomContext: {
311
317
  env: roomContext.env,
@@ -329,11 +335,13 @@ var CloudflarePlatform = class _CloudflarePlatform extends AbstractPlatform {
329
335
  var _a;
330
336
  const deserialized = (_a = webSocket.webSocket.deserializeAttachment()) != null ? _a : {};
331
337
  webSocket.webSocket.serializeAttachment(__spreadProps(__spreadValues({}, deserialized), { state }));
338
+ return state;
332
339
  }
333
340
  _getDetachedState() {
334
341
  var _a, _b;
335
342
  if (this._config.registrationMode !== "detached") return null;
336
- return (_b = (_a = this._roomContext) == null ? void 0 : _a.state) != null ? _b : null;
343
+ const detachedState = (_b = (_a = this._roomContext) == null ? void 0 : _a.state) != null ? _b : null;
344
+ return detachedState;
337
345
  }
338
346
  };
339
347
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pluv/platform-cloudflare",
3
- "version": "0.37.3",
3
+ "version": "0.37.5",
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.37.3",
22
- "@pluv/persistence-cloudflare-transactional-storage": "^0.37.3",
23
- "@pluv/types": "^0.37.3"
21
+ "@pluv/io": "^0.37.5",
22
+ "@pluv/persistence-cloudflare-transactional-storage": "^0.37.5",
23
+ "@pluv/types": "^0.37.5"
24
24
  },
25
25
  "devDependencies": {
26
26
  "@cloudflare/workers-types": "^4.20250303.0",
27
27
  "eslint": "^8.57.1",
28
28
  "tsup": "^8.4.0",
29
29
  "typescript": "^5.8.2",
30
- "@pluv/tsconfig": "^0.37.3",
31
- "eslint-config-pluv": "^0.37.3"
30
+ "@pluv/tsconfig": "^0.37.5",
31
+ "eslint-config-pluv": "^0.37.5"
32
32
  },
33
33
  "scripts": {
34
34
  "build": "tsup src/index.ts --format esm,cjs --dts",
@@ -96,7 +96,7 @@ export class CloudflarePlatform<
96
96
  public convertWebSocket(webSocket: WebSocket, config: ConvertWebSocketConfig): CloudflareWebSocket {
97
97
  const { room } = config;
98
98
 
99
- return new CloudflareWebSocket(webSocket, { persistence: this.persistence, room });
99
+ return new CloudflareWebSocket(webSocket, { persistence: this.persistence, platform: this, room });
100
100
  }
101
101
 
102
102
  public getLastPing(webSocket: CloudflareWebSocket): number | null {
@@ -129,15 +129,15 @@ export class CloudflarePlatform<
129
129
 
130
130
  if (!detachedState) return [];
131
131
 
132
- return detachedState.getWebSockets() ?? [];
132
+ const webSockets = detachedState.getWebSockets() ?? [];
133
+
134
+ return webSockets;
133
135
  }
134
136
 
135
137
  public initialize(config: AbstractPlatformConfig<CloudflarePlatformRoomContext<TEnv, TMeta>>): this {
136
138
  const roomContext = config.roomContext ?? { ...this._roomContext };
137
139
 
138
- if (!roomContext.env || !roomContext.state) {
139
- throw new Error("Could not derive platform roomContext");
140
- }
140
+ if (!roomContext.env || !roomContext.state) throw new Error("Could not derive platform roomContext");
141
141
 
142
142
  return new CloudflarePlatform<TEnv, TMeta>({
143
143
  roomContext: {
@@ -163,15 +163,22 @@ export class CloudflarePlatform<
163
163
  return crypto.randomUUID();
164
164
  }
165
165
 
166
- public setSerializedState(webSocket: CloudflareWebSocket, state: WebSocketSerializedState): void {
166
+ public setSerializedState(
167
+ webSocket: CloudflareWebSocket,
168
+ state: WebSocketSerializedState,
169
+ ): WebSocketSerializedState {
167
170
  const deserialized = webSocket.webSocket.deserializeAttachment() ?? {};
168
171
 
169
172
  webSocket.webSocket.serializeAttachment({ ...deserialized, state });
173
+
174
+ return state;
170
175
  }
171
176
 
172
177
  private _getDetachedState(): DurableObjectState | null {
173
178
  if (this._config.registrationMode !== "detached") return null;
174
179
 
175
- return this._roomContext?.state ?? null;
180
+ const detachedState = this._roomContext?.state ?? null;
181
+
182
+ return detachedState;
176
183
  }
177
184
  }
@@ -43,7 +43,18 @@ export class CloudflareWebSocket extends AbstractWebSocket<WebSocket> {
43
43
 
44
44
  if (!state) throw new Error("Could not get websocket state");
45
45
 
46
- return state;
46
+ const currentPing = state.timers.ping;
47
+ const lastPing = this._platform.getLastPing(this);
48
+
49
+ if (!lastPing) return state;
50
+ if (currentPing >= lastPing) return state;
51
+
52
+ const newState: WebSocketSerializedState = {
53
+ ...state,
54
+ timers: { ...state.timers, ping: lastPing },
55
+ };
56
+
57
+ return this._platform.setSerializedState(this, newState);
47
58
  }
48
59
 
49
60
  public set state(state: WebSocketSerializedState) {