@zintrust/socket 4.0.0 → 4.0.1
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/dist/build-manifest.json +14 -10
- package/dist/index.d.ts +26 -4
- package/dist/index.js +117 -25
- package/package.json +2 -2
package/dist/build-manifest.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zintrust/socket",
|
|
3
|
-
"version": "
|
|
4
|
-
"buildDate": "2026-11-
|
|
3
|
+
"version": "3.9.1",
|
|
4
|
+
"buildDate": "2026-11-22T07:25:33.074Z",
|
|
5
5
|
"buildEnvironment": {
|
|
6
|
-
"node": "
|
|
7
|
-
"platform": "
|
|
8
|
-
"arch": "
|
|
6
|
+
"node": "v26.13.1",
|
|
7
|
+
"platform": "darwin",
|
|
8
|
+
"arch": "arm64"
|
|
9
9
|
},
|
|
10
10
|
"git": {
|
|
11
|
-
"commit": "
|
|
12
|
-
"branch": "
|
|
11
|
+
"commit": "1bcdcccd",
|
|
12
|
+
"branch": "release"
|
|
13
13
|
},
|
|
14
14
|
"package": {
|
|
15
15
|
"engines": {
|
|
@@ -21,13 +21,17 @@
|
|
|
21
21
|
]
|
|
22
22
|
},
|
|
23
23
|
"files": {
|
|
24
|
+
"build-manifest.json": {
|
|
25
|
+
"size": 1085,
|
|
26
|
+
"sha256": "e0e62b09815eeb681536a932ac4d50f24245fec098b648694aa0999622073d36"
|
|
27
|
+
},
|
|
24
28
|
"index.d.ts": {
|
|
25
|
-
"size":
|
|
26
|
-
"sha256": "
|
|
29
|
+
"size": 2446,
|
|
30
|
+
"sha256": "6f51d2b999e7a2953bafa9b3327fc2abc24d352962aea241ca00378377cf65c4"
|
|
27
31
|
},
|
|
28
32
|
"index.js": {
|
|
29
33
|
"size": 40405,
|
|
30
|
-
"sha256": "
|
|
34
|
+
"sha256": "f6c4039f684b02ee74dc39f5e5dc55ec9e8ed715f92bb8a38f2f6370efac0b39"
|
|
31
35
|
},
|
|
32
36
|
"register.d.ts": {
|
|
33
37
|
"size": 16,
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type IRequest, type IRouter } from '@zintrust/core/http';
|
|
2
|
-
import { type SocketRouteRegistrar } from '@zintrust/core/runtime';
|
|
2
|
+
import { type SocketNodeUpgradeInput, type SocketRouteRegistrar, type SocketRuntimeDiagnostics, type SocketWorkerContext } from '@zintrust/core/runtime';
|
|
3
3
|
type ServerSideSocketPublishInput = Readonly<{
|
|
4
4
|
channels: readonly string[];
|
|
5
5
|
event: string;
|
|
@@ -15,20 +15,42 @@ declare const publishSocketEventFromServer: (input: ServerSideSocketPublishInput
|
|
|
15
15
|
event: string;
|
|
16
16
|
deliveries: number;
|
|
17
17
|
}>;
|
|
18
|
-
declare const socketRuntime:
|
|
18
|
+
declare const socketRuntime: Readonly<{
|
|
19
|
+
name: string;
|
|
20
|
+
isEnabled: () => boolean;
|
|
21
|
+
describe: () => SocketRuntimeDiagnostics;
|
|
22
|
+
canHandleNodeUpgrade: (input: SocketNodeUpgradeInput) => boolean;
|
|
23
|
+
handleNodeUpgrade: (input: SocketNodeUpgradeInput) => Promise<boolean>;
|
|
24
|
+
canHandleWorkerRequest: (request: Request) => boolean;
|
|
25
|
+
handleWorkerRequest: (request: Request, context: SocketWorkerContext) => Promise<Response | null>;
|
|
26
|
+
}>;
|
|
19
27
|
declare const registerSocketRoutes: (router: IRouter) => void;
|
|
20
28
|
declare const socketRouteRegistrar: SocketRouteRegistrar;
|
|
21
29
|
export declare const SocketPackage: Readonly<{
|
|
22
|
-
runtime:
|
|
23
|
-
|
|
30
|
+
runtime: Readonly<{
|
|
31
|
+
name: string;
|
|
32
|
+
isEnabled: () => boolean;
|
|
33
|
+
describe: () => SocketRuntimeDiagnostics;
|
|
34
|
+
canHandleNodeUpgrade: (input: SocketNodeUpgradeInput) => boolean;
|
|
35
|
+
handleNodeUpgrade: (input: SocketNodeUpgradeInput) => Promise<boolean>;
|
|
36
|
+
canHandleWorkerRequest: (request: Request) => boolean;
|
|
37
|
+
handleWorkerRequest: (request: Request, context: SocketWorkerContext) => Promise<Response | null>;
|
|
38
|
+
}>;
|
|
39
|
+
routeRegistrar: Readonly<{
|
|
40
|
+
registerRoutes: (router: IRouter) => void;
|
|
41
|
+
}>;
|
|
24
42
|
publish: (channels: string[], event: string, data: unknown, excludeSocketId?: string) => number;
|
|
25
43
|
registerRoutes: (router: IRouter) => void;
|
|
26
44
|
}>;
|
|
27
45
|
export declare class ZintrustSocketHub {
|
|
28
46
|
private readonly settings;
|
|
29
47
|
private readonly state;
|
|
48
|
+
private readonly durableState;
|
|
30
49
|
constructor(_state: unknown, env: unknown);
|
|
31
50
|
fetch(request: Request): Promise<Response>;
|
|
51
|
+
webSocketMessage(ws: WebSocket, message: string | ArrayBuffer): void;
|
|
52
|
+
webSocketClose(ws: WebSocket, _code: number, _reason: string, _wasClean: boolean): void;
|
|
53
|
+
webSocketError(ws: WebSocket, _error: unknown): void;
|
|
32
54
|
private handlePublishRequest;
|
|
33
55
|
}
|
|
34
56
|
export declare const publishSocketEvent: (channels: string[], event: string, data: unknown, excludeSocketId?: string) => number;
|
package/dist/index.js
CHANGED
|
@@ -405,20 +405,6 @@ const createNodePeer = (state, socket) => {
|
|
|
405
405
|
socket.on('error', () => detachPeer(state, peer));
|
|
406
406
|
return peer;
|
|
407
407
|
};
|
|
408
|
-
const createWorkerPeer = (state, socket) => {
|
|
409
|
-
const peer = {
|
|
410
|
-
id: createSocketId(),
|
|
411
|
-
subscriptions: new Set(),
|
|
412
|
-
sendText(text) {
|
|
413
|
-
socket.send(text);
|
|
414
|
-
},
|
|
415
|
-
close(code = 1000, reason = '') {
|
|
416
|
-
socket.close(code, reason);
|
|
417
|
-
},
|
|
418
|
-
};
|
|
419
|
-
state.peers.set(peer.id, peer);
|
|
420
|
-
return peer;
|
|
421
|
-
};
|
|
422
408
|
const attachNodePeer = async (state, input, settings) => {
|
|
423
409
|
const key = input.request.headers['sec-websocket-key'];
|
|
424
410
|
const secKey = Array.isArray(key) ? key[0] : key;
|
|
@@ -465,15 +451,6 @@ const attachNodePeer = async (state, input, settings) => {
|
|
|
465
451
|
}
|
|
466
452
|
return true;
|
|
467
453
|
};
|
|
468
|
-
const attachWorkerPeer = (state, socket, settings) => {
|
|
469
|
-
const peer = createWorkerPeer(state, socket);
|
|
470
|
-
emitConnectionEstablished(peer, settings);
|
|
471
|
-
socket.addEventListener('message', (event) => {
|
|
472
|
-
void handleClientMessage(state, peer, decodeText(event.data), settings);
|
|
473
|
-
});
|
|
474
|
-
socket.addEventListener('close', () => detachPeer(state, peer));
|
|
475
|
-
socket.addEventListener('error', () => detachPeer(state, peer));
|
|
476
|
-
};
|
|
477
454
|
const getSocketAppKey = (requestPath, settings) => {
|
|
478
455
|
return parseSocketPath(requestPath, settings);
|
|
479
456
|
};
|
|
@@ -990,9 +967,47 @@ export const SocketPackage = Object.freeze({
|
|
|
990
967
|
export class ZintrustSocketHub {
|
|
991
968
|
settings;
|
|
992
969
|
state;
|
|
970
|
+
durableState;
|
|
993
971
|
constructor(_state, env) {
|
|
994
972
|
this.settings = getSocketRuntimeSettings(env);
|
|
995
973
|
this.state = createSocketState();
|
|
974
|
+
this.durableState = _state;
|
|
975
|
+
// Rebuild in-memory peer/channel maps from hibernated WebSockets so state
|
|
976
|
+
// survives eviction. The maps act as a warm cache; attachments are the
|
|
977
|
+
// source of truth across hibernate/wake cycles.
|
|
978
|
+
if (this.durableState.getWebSockets) {
|
|
979
|
+
for (const ws of this.durableState.getWebSockets()) {
|
|
980
|
+
const attachment = ws.deserializeAttachment();
|
|
981
|
+
if (attachment === null || attachment === undefined)
|
|
982
|
+
continue;
|
|
983
|
+
const peerId = attachment.peerId;
|
|
984
|
+
const subscriptions = attachment.subscriptions ?? [];
|
|
985
|
+
if (typeof peerId !== 'string')
|
|
986
|
+
continue;
|
|
987
|
+
const peer = {
|
|
988
|
+
id: peerId,
|
|
989
|
+
subscriptions: new Set(subscriptions),
|
|
990
|
+
sendText(text) {
|
|
991
|
+
ws.send(text);
|
|
992
|
+
},
|
|
993
|
+
close(code = 1000, reason = '') {
|
|
994
|
+
ws.close(code, reason);
|
|
995
|
+
},
|
|
996
|
+
};
|
|
997
|
+
this.state.peers.set(peerId, peer);
|
|
998
|
+
for (const channel of subscriptions) {
|
|
999
|
+
addPeerToChannel(this.state, channel, peer);
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
// Auto-respond to pusher:ping with pusher:pong so keepalive pings do not
|
|
1004
|
+
// force a full Durable Object wake.
|
|
1005
|
+
if (this.durableState.setWebSocketAutoResponse) {
|
|
1006
|
+
this.durableState.setWebSocketAutoResponse({
|
|
1007
|
+
request: /pusher:ping/,
|
|
1008
|
+
response: '{"event":"pusher:pong","data":{}}',
|
|
1009
|
+
});
|
|
1010
|
+
}
|
|
996
1011
|
}
|
|
997
1012
|
async fetch(request) {
|
|
998
1013
|
const url = new URL(request.url);
|
|
@@ -1016,10 +1031,87 @@ export class ZintrustSocketHub {
|
|
|
1016
1031
|
const pair = new WebSocketPairRef();
|
|
1017
1032
|
const client = pair[0];
|
|
1018
1033
|
const server = pair[1];
|
|
1019
|
-
|
|
1020
|
-
|
|
1034
|
+
// Use the hibernation API so the Durable Object can hibernate while idle
|
|
1035
|
+
// instead of staying resident for the full WebSocket lifetime.
|
|
1036
|
+
this.durableState.acceptWebSocket(server);
|
|
1037
|
+
// Allocate a peer and announce the connection immediately so clients
|
|
1038
|
+
// receive `pusher:connection_established` without waiting to send a
|
|
1039
|
+
// message. Peer id and subscriptions are persisted on the socket
|
|
1040
|
+
// attachment for hibernation survival.
|
|
1041
|
+
const peerId = createSocketId();
|
|
1042
|
+
const peer = {
|
|
1043
|
+
id: peerId,
|
|
1044
|
+
subscriptions: new Set(),
|
|
1045
|
+
sendText(text) {
|
|
1046
|
+
server.send(text);
|
|
1047
|
+
},
|
|
1048
|
+
close(code = 1000, reason = '') {
|
|
1049
|
+
server.close(code, reason);
|
|
1050
|
+
},
|
|
1051
|
+
};
|
|
1052
|
+
this.state.peers.set(peerId, peer);
|
|
1053
|
+
emitConnectionEstablished(peer, this.settings);
|
|
1054
|
+
server.serializeAttachment({ peerId, subscriptions: [] });
|
|
1021
1055
|
return new Response(null, { status: 101, webSocket: client });
|
|
1022
1056
|
}
|
|
1057
|
+
webSocketMessage(ws, message) {
|
|
1058
|
+
const text = decodeText(message);
|
|
1059
|
+
const attachment = ws.deserializeAttachment();
|
|
1060
|
+
const attachmentPeerId = attachment?.peerId;
|
|
1061
|
+
let peerId = attachmentPeerId;
|
|
1062
|
+
let peer;
|
|
1063
|
+
if (peerId) {
|
|
1064
|
+
peer = this.state.peers.get(peerId);
|
|
1065
|
+
}
|
|
1066
|
+
// First message from a socket without an attachment: allocate a peer and
|
|
1067
|
+
// announce the connection.
|
|
1068
|
+
if (peer === undefined) {
|
|
1069
|
+
peerId = createSocketId();
|
|
1070
|
+
peer = {
|
|
1071
|
+
id: peerId,
|
|
1072
|
+
subscriptions: new Set(),
|
|
1073
|
+
sendText(msg) {
|
|
1074
|
+
ws.send(msg);
|
|
1075
|
+
},
|
|
1076
|
+
close(code = 1000, reason = '') {
|
|
1077
|
+
ws.close(code, reason);
|
|
1078
|
+
},
|
|
1079
|
+
};
|
|
1080
|
+
this.state.peers.set(peerId, peer);
|
|
1081
|
+
emitConnectionEstablished(peer, this.settings);
|
|
1082
|
+
}
|
|
1083
|
+
void handleClientMessage(this.state, peer, text, this.settings);
|
|
1084
|
+
// Persist per-socket peer id and subscriptions on the attachment so they
|
|
1085
|
+
// survive hibernation/eviction.
|
|
1086
|
+
const updatedPeer = this.state.peers.get(peer.id);
|
|
1087
|
+
if (updatedPeer) {
|
|
1088
|
+
ws.serializeAttachment({
|
|
1089
|
+
peerId: updatedPeer.id,
|
|
1090
|
+
subscriptions: Array.from(updatedPeer.subscriptions),
|
|
1091
|
+
});
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
webSocketClose(ws, _code, _reason, _wasClean) {
|
|
1095
|
+
const attachment = ws.deserializeAttachment();
|
|
1096
|
+
if (attachment?.peerId) {
|
|
1097
|
+
const peer = this.state.peers.get(attachment.peerId);
|
|
1098
|
+
if (peer) {
|
|
1099
|
+
detachPeer(this.state, peer);
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
webSocketError(ws, _error) {
|
|
1104
|
+
// Detach the peer on error. Kept separate from webSocketClose so that
|
|
1105
|
+
// error events can be observed and handled independently of
|
|
1106
|
+
// clean/abnormal WebSocket closures.
|
|
1107
|
+
const attachment = ws.deserializeAttachment();
|
|
1108
|
+
if (attachment?.peerId) {
|
|
1109
|
+
const peer = this.state.peers.get(attachment.peerId);
|
|
1110
|
+
if (peer) {
|
|
1111
|
+
detachPeer(this.state, peer);
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1023
1115
|
async handlePublishRequest(request) {
|
|
1024
1116
|
const body = parseJsonObject(await request.text());
|
|
1025
1117
|
if (body === null) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zintrust/socket",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.1",
|
|
4
4
|
"description": "Unified socket runtime for ZinTrust.",
|
|
5
5
|
"private": false,
|
|
6
6
|
"type": "module",
|
|
@@ -38,4 +38,4 @@
|
|
|
38
38
|
"build": "tsc -p tsconfig.json",
|
|
39
39
|
"prepublishOnly": "npm run build"
|
|
40
40
|
}
|
|
41
|
-
}
|
|
41
|
+
}
|