@hyper-fetch/orpc 10.0.0 → 10.1.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/README.md +89 -37
- package/__tests__/features/adapter/adapter.lifecycle.spec.ts +23 -0
- package/__tests__/features/adapter/adapter.schema.spec.ts +19 -0
- package/__tests__/features/stream-adapter/stream-adapter.lifecycle.spec.ts +54 -5
- package/__tests__/features/websocket-adapter/websocket-adapter.environment.spec.ts +6 -0
- package/__tests__/features/websocket-adapter/websocket-adapter.handshake-error.spec.ts +3 -0
- package/__tests__/features/websocket-adapter/websocket-adapter.lifecycle.spec.ts +122 -0
- package/dist/adapter/adapter.fetch.d.ts.map +1 -1
- package/dist/index.mjs +93 -184
- package/dist/index.mjs.map +1 -1
- package/dist/stream-adapter/stream-adapter.d.ts.map +1 -1
- package/dist/websocket-adapter/websocket-adapter.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/adapter/adapter.fetch.ts +4 -3
- package/src/stream-adapter/stream-adapter.ts +42 -122
- package/src/websocket-adapter/websocket-adapter.ts +76 -178
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Adapter, Time, defaultMapper, getErrorMessage, parseResponse, stringifyKey, stringifyQueryParams } from "@hyper-fetch/core";
|
|
2
2
|
import { StandardRPCJsonSerializer, StandardRPCSerializer, getMalformedResponseErrorCode } from "@orpc/client/standard";
|
|
3
3
|
import { ErrorEvent, ORPCError, createORPCErrorFromJson, isORPCErrorJson, isORPCErrorStatus } from "@orpc/client";
|
|
4
|
-
import { SocketAdapter, WebsocketReadyState, getSocketError, getSocketUrl, getWebsocketConstructor } from "@hyper-fetch/sockets";
|
|
4
|
+
import { SocketAdapter, WebsocketReadyState, createReconnectingConnection, getSocketError, getSocketUrl, getWebsocketConstructor } from "@hyper-fetch/sockets";
|
|
5
5
|
//#region src/adapter/adapter.constants.ts
|
|
6
6
|
var defaultTimeout = Time.SEC * 5;
|
|
7
7
|
var orpcExtra = { headers: {} };
|
|
@@ -231,9 +231,8 @@ var getOrpcAdapter = ({ queryMethod: defaultQueryMethod = "POST", customJsonSeri
|
|
|
231
231
|
const total = getBodySize(httpRequest.body);
|
|
232
232
|
if (total) onRequestProgress({
|
|
233
233
|
total,
|
|
234
|
-
loaded:
|
|
234
|
+
loaded: 0
|
|
235
235
|
});
|
|
236
|
-
onRequestEnd();
|
|
237
236
|
const response = await fetch(httpRequest.url, {
|
|
238
237
|
...restOptions,
|
|
239
238
|
method: httpRequest.method,
|
|
@@ -241,6 +240,7 @@ var getOrpcAdapter = ({ queryMethod: defaultQueryMethod = "POST", customJsonSeri
|
|
|
241
240
|
body: httpRequest.body,
|
|
242
241
|
signal: controller.signal
|
|
243
242
|
});
|
|
243
|
+
onRequestEnd();
|
|
244
244
|
onResponseStart();
|
|
245
245
|
const responseHeaders = {};
|
|
246
246
|
response.headers.forEach((value, key) => {
|
|
@@ -623,10 +623,8 @@ var OrpcStreamAdapter = ({ customJsonSerializers } = {}) => {
|
|
|
623
623
|
name: "orpc-stream",
|
|
624
624
|
confirmsSubscriptions: true
|
|
625
625
|
}).setQueryParamsMapper(stringifyQueryParams).setConnector((bindings) => {
|
|
626
|
-
const { socket, adapter, getQueryParams,
|
|
627
|
-
const autoConnect = adapter.adapterOptions?.autoConnect ?? true;
|
|
626
|
+
const { socket, adapter, getQueryParams, onConnectFailed } = bindings;
|
|
628
627
|
let connection;
|
|
629
|
-
let pendingConnection;
|
|
630
628
|
const subscriptions = createOrpcSubscriptions({
|
|
631
629
|
bindings,
|
|
632
630
|
serializer,
|
|
@@ -660,7 +658,7 @@ var OrpcStreamAdapter = ({ customJsonSerializers } = {}) => {
|
|
|
660
658
|
timedOut: false
|
|
661
659
|
};
|
|
662
660
|
transport.timer = setTimeout(() => {
|
|
663
|
-
|
|
661
|
+
startStream(subscription, attempts);
|
|
664
662
|
}, subscription.lastEventRetry ?? socket.reconnectTime);
|
|
665
663
|
subscription.transport = transport;
|
|
666
664
|
};
|
|
@@ -760,84 +758,47 @@ var OrpcStreamAdapter = ({ customJsonSerializers } = {}) => {
|
|
|
760
758
|
scheduleRestart(subscription, transport.attempts + 1);
|
|
761
759
|
});
|
|
762
760
|
};
|
|
763
|
-
const openConnection = async () => {
|
|
764
|
-
const details = await onConnect();
|
|
765
|
-
if (!details) return adapter.connected;
|
|
766
|
-
if (typeof (details.adapterOptions?.fetch ?? globalThis.fetch) !== "function") {
|
|
767
|
-
onConnectFailed({ error: /* @__PURE__ */ new Error("fetch is not available in this environment") });
|
|
768
|
-
return false;
|
|
769
|
-
}
|
|
770
|
-
connection = details;
|
|
771
|
-
onConnected();
|
|
772
|
-
[...subscriptions.values()].forEach((subscription) => {
|
|
773
|
-
if (!subscription.transport) startStream(subscription, 0);
|
|
774
|
-
});
|
|
775
|
-
return true;
|
|
776
|
-
};
|
|
777
|
-
const connect = () => {
|
|
778
|
-
if (adapter.connecting && pendingConnection) return pendingConnection;
|
|
779
|
-
const attempt = openConnection();
|
|
780
|
-
pendingConnection = attempt;
|
|
781
|
-
const release = () => {
|
|
782
|
-
if (pendingConnection === attempt) pendingConnection = void 0;
|
|
783
|
-
};
|
|
784
|
-
attempt.then(release, release);
|
|
785
|
-
return attempt;
|
|
786
|
-
};
|
|
787
761
|
/** Aborts every stream. Listeners stay registered and keep the id of their last event for the next connection. */
|
|
788
|
-
const
|
|
789
|
-
|
|
790
|
-
connection = void 0;
|
|
791
|
-
[...subscriptions.values()].forEach(stopStream);
|
|
792
|
-
onDisconnect();
|
|
793
|
-
if (!manual) adapter.setForceClosed(false);
|
|
794
|
-
if (wasActive) onDisconnected();
|
|
795
|
-
return true;
|
|
762
|
+
const stopStreams = () => {
|
|
763
|
+
subscriptions.values().forEach(stopStream);
|
|
796
764
|
};
|
|
797
|
-
/**
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
const
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
}
|
|
810
|
-
return connect();
|
|
765
|
+
/**
|
|
766
|
+
* There is no shared transport - the connection is the set of streams opened with its details. It is ready at
|
|
767
|
+
* once, and it is never lost as a whole: every stream fails and starts again on its own (`scheduleRestart`).
|
|
768
|
+
*/
|
|
769
|
+
const connectionLifecycle = createReconnectingConnection({
|
|
770
|
+
bindings,
|
|
771
|
+
autoConnect: adapter.adapterOptions?.autoConnect ?? true,
|
|
772
|
+
open: (attempt) => {
|
|
773
|
+
const details = attempt.connection;
|
|
774
|
+
if (typeof (details.adapterOptions?.fetch ?? globalThis.fetch) !== "function") {
|
|
775
|
+
onConnectFailed({ error: /* @__PURE__ */ new Error("fetch is not available in this environment") });
|
|
776
|
+
return null;
|
|
811
777
|
}
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
};
|
|
817
|
-
if (autoConnect) connect();
|
|
818
|
-
const unmountOnline = socket.appManager.events.onOnline(() => {
|
|
819
|
-
if (connection) {
|
|
820
|
-
[...subscriptions.values()].forEach((subscription) => {
|
|
821
|
-
const { transport } = subscription;
|
|
822
|
-
if (transport && !transport.controller) {
|
|
823
|
-
clearTimeout(transport.timer);
|
|
824
|
-
startStream(subscription, transport.attempts);
|
|
825
|
-
}
|
|
778
|
+
connection = details;
|
|
779
|
+
attempt.ready();
|
|
780
|
+
subscriptions.values().forEach((subscription) => {
|
|
781
|
+
if (!subscription.transport) startStream(subscription, 0);
|
|
826
782
|
});
|
|
827
|
-
return
|
|
783
|
+
return { close: () => {
|
|
784
|
+
connection = void 0;
|
|
785
|
+
stopStreams();
|
|
786
|
+
} };
|
|
828
787
|
}
|
|
829
|
-
if (autoConnect && !adapter.forceClosed) connect();
|
|
830
788
|
});
|
|
789
|
+
const emit = () => {
|
|
790
|
+
throw new Error("Cannot emit events in the oRPC stream adapter, it only receives events of subscriptions");
|
|
791
|
+
};
|
|
831
792
|
return {
|
|
832
|
-
connect,
|
|
833
|
-
reconnect,
|
|
834
|
-
disconnect,
|
|
793
|
+
connect: connectionLifecycle.connect,
|
|
794
|
+
reconnect: connectionLifecycle.reconnect,
|
|
795
|
+
disconnect: connectionLifecycle.disconnect,
|
|
835
796
|
listen: subscriptions.listen,
|
|
836
797
|
emit,
|
|
837
798
|
dispose: () => {
|
|
838
|
-
|
|
799
|
+
connectionLifecycle.dispose();
|
|
839
800
|
connection = void 0;
|
|
840
|
-
|
|
801
|
+
stopStreams();
|
|
841
802
|
}
|
|
842
803
|
};
|
|
843
804
|
});
|
|
@@ -952,12 +913,9 @@ var OrpcWebsocketAdapter = ({ customJsonSerializers } = {}) => {
|
|
|
952
913
|
name: "orpc-websocket",
|
|
953
914
|
confirmsSubscriptions: true
|
|
954
915
|
}).setQueryParamsMapper(stringifyQueryParams).setConnector((bindings) => {
|
|
955
|
-
const {
|
|
956
|
-
const autoConnect = adapter.adapterOptions?.autoConnect ?? true;
|
|
916
|
+
const { adapter, getQueryParams, onConnectFailed, onError } = bindings;
|
|
957
917
|
let websocket;
|
|
958
918
|
let options;
|
|
959
|
-
let pendingConnection;
|
|
960
|
-
let timeout;
|
|
961
919
|
let lastRequestId = 0;
|
|
962
920
|
const requests = /* @__PURE__ */ new Map();
|
|
963
921
|
const subscriptions = createOrpcSubscriptions({
|
|
@@ -1065,135 +1023,86 @@ var OrpcWebsocketAdapter = ({ customJsonSerializers } = {}) => {
|
|
|
1065
1023
|
}
|
|
1066
1024
|
subscriptions.receive(subscription, message.event);
|
|
1067
1025
|
};
|
|
1068
|
-
/**
|
|
1069
|
-
const
|
|
1070
|
-
clearTimeout(timeout);
|
|
1071
|
-
timeout = setTimeout(() => {
|
|
1072
|
-
reconnect();
|
|
1073
|
-
}, delay);
|
|
1074
|
-
};
|
|
1075
|
-
const openConnection = async () => {
|
|
1076
|
-
const connection = await onConnect();
|
|
1077
|
-
if (!connection) return adapter.connected;
|
|
1078
|
-
clearTimeout(timeout);
|
|
1079
|
-
options = connection.adapterOptions;
|
|
1080
|
-
const url = getSocketUrl(connection.url, getQueryParams(connection.queryParams));
|
|
1026
|
+
/** Creates the transport, reporting a failed attempt when the environment or the connection details are invalid */
|
|
1027
|
+
const createWebsocket = (url) => {
|
|
1081
1028
|
const WebSocketConstructor = getWebsocketConstructor(options);
|
|
1082
1029
|
if (!WebSocketConstructor) {
|
|
1083
1030
|
onConnectFailed({ error: /* @__PURE__ */ new Error("WebSocket is not available in this environment") });
|
|
1084
|
-
return
|
|
1031
|
+
return null;
|
|
1085
1032
|
}
|
|
1086
|
-
let instance;
|
|
1087
1033
|
try {
|
|
1088
|
-
|
|
1034
|
+
return new WebSocketConstructor(url, options?.protocols);
|
|
1089
1035
|
} catch (error) {
|
|
1090
1036
|
onConnectFailed({ error: toError(error) });
|
|
1091
|
-
return
|
|
1037
|
+
return null;
|
|
1092
1038
|
}
|
|
1093
|
-
instance.binaryType = "arraybuffer";
|
|
1094
|
-
websocket = instance;
|
|
1095
|
-
const isCurrent = () => websocket === instance;
|
|
1096
|
-
scheduleReconnect(socket.connectionTimeout);
|
|
1097
|
-
return new Promise((resolve) => {
|
|
1098
|
-
instance.addEventListener("open", () => {
|
|
1099
|
-
clearTimeout(timeout);
|
|
1100
|
-
onConnected();
|
|
1101
|
-
[...subscriptions.values()].forEach((subscription) => {
|
|
1102
|
-
if (!subscription.transport) startSubscription(subscription);
|
|
1103
|
-
});
|
|
1104
|
-
resolve(true);
|
|
1105
|
-
});
|
|
1106
|
-
/** The transport is gone - report it and get the connection back unless it was closed normally */
|
|
1107
|
-
const onTransportClosed = (event) => {
|
|
1108
|
-
websocket = void 0;
|
|
1109
|
-
clearTimeout(timeout);
|
|
1110
|
-
releaseAll();
|
|
1111
|
-
onDisconnected();
|
|
1112
|
-
onError({ error: new Error(getSocketError(event)) });
|
|
1113
|
-
if (event.code !== 1e3) scheduleReconnect();
|
|
1114
|
-
};
|
|
1115
|
-
instance.addEventListener("close", (event) => {
|
|
1116
|
-
resolve(false);
|
|
1117
|
-
if (!isCurrent()) return;
|
|
1118
|
-
onTransportClosed(event);
|
|
1119
|
-
});
|
|
1120
|
-
instance.addEventListener("error", () => {
|
|
1121
|
-
if (!isCurrent() || instance.readyState !== WebsocketReadyState.CONNECTING) return;
|
|
1122
|
-
resolve(false);
|
|
1123
|
-
try {
|
|
1124
|
-
instance.close();
|
|
1125
|
-
} catch {}
|
|
1126
|
-
onTransportClosed(Object.assign(new Event("close"), { code: 1006 }));
|
|
1127
|
-
});
|
|
1128
|
-
instance.addEventListener("message", (event) => {
|
|
1129
|
-
if (isCurrent()) handleMessage(event.data);
|
|
1130
|
-
});
|
|
1131
|
-
});
|
|
1132
|
-
};
|
|
1133
|
-
const connect = () => {
|
|
1134
|
-
if (adapter.connecting && pendingConnection) return pendingConnection;
|
|
1135
|
-
const attempt = openConnection();
|
|
1136
|
-
pendingConnection = attempt;
|
|
1137
|
-
const release = () => {
|
|
1138
|
-
if (pendingConnection === attempt) pendingConnection = void 0;
|
|
1139
|
-
};
|
|
1140
|
-
attempt.then(release, release);
|
|
1141
|
-
return attempt;
|
|
1142
1039
|
};
|
|
1143
1040
|
/**
|
|
1144
|
-
* Closes
|
|
1145
|
-
*
|
|
1041
|
+
* Closes a detached transport. The server cancels every subscription of the connection, the listeners stay
|
|
1042
|
+
* registered and keep the id of their last event for the next connection. Resolves on the close - or on an
|
|
1043
|
+
* error, as a transport that fails while closing may report only `error` (Node.js 22).
|
|
1146
1044
|
*/
|
|
1147
|
-
const
|
|
1148
|
-
clearTimeout(timeout);
|
|
1149
|
-
const current = websocket;
|
|
1045
|
+
const closeWebsocket = (transport) => {
|
|
1150
1046
|
websocket = void 0;
|
|
1151
1047
|
releaseAll();
|
|
1152
|
-
|
|
1153
|
-
onDisconnect();
|
|
1154
|
-
if (!manual) adapter.setForceClosed(false);
|
|
1155
|
-
if (wasActive) onDisconnected();
|
|
1156
|
-
if (!current) return true;
|
|
1048
|
+
if (transport.readyState === WebsocketReadyState.CLOSED) return Promise.resolve();
|
|
1157
1049
|
return new Promise((resolve) => {
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
const disconnect = () => {
|
|
1165
|
-
return closeConnection({ manual: true });
|
|
1166
|
-
};
|
|
1167
|
-
const reconnect = async () => {
|
|
1168
|
-
adapter.setForceClosed(false);
|
|
1169
|
-
await onReconnect({
|
|
1170
|
-
disconnect: () => closeConnection({ manual: false }),
|
|
1171
|
-
connect: async () => {
|
|
1172
|
-
if (adapter.forceClosed) {
|
|
1173
|
-
adapter.setReconnectionAttempts(0);
|
|
1174
|
-
return false;
|
|
1175
|
-
}
|
|
1176
|
-
return connect();
|
|
1050
|
+
transport.addEventListener("close", () => resolve(), { once: true });
|
|
1051
|
+
transport.addEventListener("error", () => resolve(), { once: true });
|
|
1052
|
+
try {
|
|
1053
|
+
transport.close(1e3);
|
|
1054
|
+
} catch {
|
|
1055
|
+
resolve();
|
|
1177
1056
|
}
|
|
1178
1057
|
});
|
|
1179
1058
|
};
|
|
1059
|
+
const connection = createReconnectingConnection({
|
|
1060
|
+
bindings,
|
|
1061
|
+
autoConnect: adapter.adapterOptions?.autoConnect ?? true,
|
|
1062
|
+
open: (attempt) => {
|
|
1063
|
+
options = attempt.connection.adapterOptions;
|
|
1064
|
+
const transport = createWebsocket(getSocketUrl(attempt.connection.url, getQueryParams(attempt.connection.queryParams)));
|
|
1065
|
+
if (!transport) return null;
|
|
1066
|
+
transport.binaryType = "arraybuffer";
|
|
1067
|
+
websocket = transport;
|
|
1068
|
+
/** The transport is gone - report it and let the engine decide about the next attempt */
|
|
1069
|
+
const onTransportClosed = (event) => {
|
|
1070
|
+
attempt.lost({
|
|
1071
|
+
code: event.code,
|
|
1072
|
+
reason: event.reason || void 0
|
|
1073
|
+
});
|
|
1074
|
+
onError({ error: new Error(getSocketError(event)) });
|
|
1075
|
+
};
|
|
1076
|
+
transport.addEventListener("open", () => {
|
|
1077
|
+
attempt.ready();
|
|
1078
|
+
if (!attempt.isCurrent()) return;
|
|
1079
|
+
subscriptions.values().forEach((subscription) => {
|
|
1080
|
+
if (!subscription.transport) startSubscription(subscription);
|
|
1081
|
+
});
|
|
1082
|
+
});
|
|
1083
|
+
transport.addEventListener("close", (event) => {
|
|
1084
|
+
if (attempt.isCurrent()) onTransportClosed(event);
|
|
1085
|
+
});
|
|
1086
|
+
transport.addEventListener("error", () => {
|
|
1087
|
+
if (!attempt.isCurrent() || transport.readyState !== WebsocketReadyState.CONNECTING) return;
|
|
1088
|
+
onTransportClosed(Object.assign(new Event("close"), { code: 1006 }));
|
|
1089
|
+
});
|
|
1090
|
+
transport.addEventListener("message", (event) => {
|
|
1091
|
+
if (attempt.isCurrent()) handleMessage(event.data);
|
|
1092
|
+
});
|
|
1093
|
+
return { close: () => closeWebsocket(transport) };
|
|
1094
|
+
}
|
|
1095
|
+
});
|
|
1180
1096
|
const emit = () => {
|
|
1181
1097
|
throw new Error("Cannot emit events in the oRPC WebSocket adapter, it only receives events of subscriptions");
|
|
1182
1098
|
};
|
|
1183
|
-
if (autoConnect) connect();
|
|
1184
|
-
const unmountOnline = socket.appManager.events.onOnline(() => {
|
|
1185
|
-
if (autoConnect && !adapter.connected && !adapter.forceClosed) connect();
|
|
1186
|
-
});
|
|
1187
1099
|
return {
|
|
1188
|
-
connect,
|
|
1189
|
-
reconnect,
|
|
1190
|
-
disconnect,
|
|
1100
|
+
connect: connection.connect,
|
|
1101
|
+
reconnect: connection.reconnect,
|
|
1102
|
+
disconnect: connection.disconnect,
|
|
1191
1103
|
listen: subscriptions.listen,
|
|
1192
1104
|
emit,
|
|
1193
|
-
dispose:
|
|
1194
|
-
unmountOnline();
|
|
1195
|
-
clearTimeout(timeout);
|
|
1196
|
-
}
|
|
1105
|
+
dispose: connection.dispose
|
|
1197
1106
|
};
|
|
1198
1107
|
});
|
|
1199
1108
|
};
|