@cratis/arc 22.0.2 → 22.2.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/dist/cjs/queries/ObservableQueryFor.js +6 -1
- package/dist/cjs/queries/ObservableQueryFor.js.map +1 -1
- package/dist/cjs/queries/QueryInstanceCache.js +6 -6
- package/dist/cjs/queries/QueryInstanceCache.js.map +1 -1
- package/dist/cjs/queries/QueryResult.js +23 -4
- package/dist/cjs/queries/QueryResult.js.map +1 -1
- package/dist/cjs/queries/QueryResultWithState.js +20 -7
- package/dist/cjs/queries/QueryResultWithState.js.map +1 -1
- package/dist/cjs/queries/ServerSentEventHubConnection.js +120 -58
- package/dist/cjs/queries/ServerSentEventHubConnection.js.map +1 -1
- package/dist/cjs/queries/WebSocketHubConnection.js +121 -51
- package/dist/cjs/queries/WebSocketHubConnection.js.map +1 -1
- package/dist/esm/queries/IQueryResult.d.ts +1 -0
- package/dist/esm/queries/IQueryResult.d.ts.map +1 -1
- package/dist/esm/queries/ObservableQueryFor.d.ts.map +1 -1
- package/dist/esm/queries/ObservableQueryFor.js +6 -1
- package/dist/esm/queries/ObservableQueryFor.js.map +1 -1
- package/dist/esm/queries/QueryInstanceCache.d.ts +1 -1
- package/dist/esm/queries/QueryInstanceCache.d.ts.map +1 -1
- package/dist/esm/queries/QueryInstanceCache.js +6 -6
- package/dist/esm/queries/QueryInstanceCache.js.map +1 -1
- package/dist/esm/queries/QueryResult.d.ts +2 -0
- package/dist/esm/queries/QueryResult.d.ts.map +1 -1
- package/dist/esm/queries/QueryResult.js +23 -4
- package/dist/esm/queries/QueryResult.js.map +1 -1
- package/dist/esm/queries/QueryResultWithState.d.ts +3 -3
- package/dist/esm/queries/QueryResultWithState.d.ts.map +1 -1
- package/dist/esm/queries/QueryResultWithState.js +20 -7
- package/dist/esm/queries/QueryResultWithState.js.map +1 -1
- package/dist/esm/queries/ServerSentEventHubConnection.d.ts +11 -4
- package/dist/esm/queries/ServerSentEventHubConnection.d.ts.map +1 -1
- package/dist/esm/queries/ServerSentEventHubConnection.js +120 -58
- package/dist/esm/queries/ServerSentEventHubConnection.js.map +1 -1
- package/dist/esm/queries/WebSocketHubConnection.d.ts +13 -2
- package/dist/esm/queries/WebSocketHubConnection.d.ts.map +1 -1
- package/dist/esm/queries/WebSocketHubConnection.js +121 -51
- package/dist/esm/queries/WebSocketHubConnection.js.map +1 -1
- package/dist/esm/queries/for_ObservableQueryFor/when_performing/with_a_not_ready_result.d.ts +2 -0
- package/dist/esm/queries/for_ObservableQueryFor/when_performing/with_a_not_ready_result.d.ts.map +1 -0
- package/dist/esm/queries/for_ObservableQueryFor/when_performing/with_a_not_ready_result.js +34 -0
- package/dist/esm/queries/for_ObservableQueryFor/when_performing/with_a_not_ready_result.js.map +1 -0
- package/dist/esm/queries/for_ObservableQueryFor/when_subscribing/with_direct_mode_and_sse_transport.js +26 -2
- package/dist/esm/queries/for_ObservableQueryFor/when_subscribing/with_direct_mode_and_sse_transport.js.map +1 -1
- package/dist/esm/queries/for_ObservableQueryFor/when_subscribing/with_direct_mode_disabled_and_sse_transport.js +60 -3
- package/dist/esm/queries/for_ObservableQueryFor/when_subscribing/with_direct_mode_disabled_and_sse_transport.js.map +1 -1
- package/dist/esm/queries/for_ObservableQueryFor/when_subscribing/with_web_socket_hub_payload_without_readiness.d.ts +2 -0
- package/dist/esm/queries/for_ObservableQueryFor/when_subscribing/with_web_socket_hub_payload_without_readiness.d.ts.map +1 -0
- package/dist/esm/queries/for_ObservableQueryFor/when_subscribing/with_web_socket_hub_payload_without_readiness.js +83 -0
- package/dist/esm/queries/for_ObservableQueryFor/when_subscribing/with_web_socket_hub_payload_without_readiness.js.map +1 -0
- package/dist/esm/queries/for_QueryInstanceCache/when_setting_last_result/with_only_readiness_changed.d.ts +2 -0
- package/dist/esm/queries/for_QueryInstanceCache/when_setting_last_result/with_only_readiness_changed.d.ts.map +1 -0
- package/dist/esm/queries/for_QueryInstanceCache/when_setting_last_result/with_only_readiness_changed.js +22 -0
- package/dist/esm/queries/for_QueryInstanceCache/when_setting_last_result/with_only_readiness_changed.js.map +1 -0
- package/dist/esm/queries/for_QueryResult/when_constructing_from_a_server_result/with_readiness_false.d.ts +2 -0
- package/dist/esm/queries/for_QueryResult/when_constructing_from_a_server_result/with_readiness_false.d.ts.map +1 -0
- package/dist/esm/queries/for_QueryResult/when_constructing_from_a_server_result/with_readiness_false.js +17 -0
- package/dist/esm/queries/for_QueryResult/when_constructing_from_a_server_result/with_readiness_false.js.map +1 -0
- package/dist/esm/queries/for_QueryResult/when_constructing_from_a_server_result/without_readiness.d.ts +2 -0
- package/dist/esm/queries/for_QueryResult/when_constructing_from_a_server_result/without_readiness.d.ts.map +1 -0
- package/dist/esm/queries/for_QueryResult/when_constructing_from_a_server_result/without_readiness.js +16 -0
- package/dist/esm/queries/for_QueryResult/when_constructing_from_a_server_result/without_readiness.js.map +1 -0
- package/dist/esm/queries/for_QueryResultWithState/when_creating_from_query_result/with_readiness_false.d.ts +2 -0
- package/dist/esm/queries/for_QueryResultWithState/when_creating_from_query_result/with_readiness_false.d.ts.map +1 -0
- package/dist/esm/queries/for_QueryResultWithState/when_creating_from_query_result/with_readiness_false.js +19 -0
- package/dist/esm/queries/for_QueryResultWithState/when_creating_from_query_result/with_readiness_false.js.map +1 -0
- package/dist/esm/queries/for_QueryResultWithState/when_creating_from_query_result/without_readiness.d.ts +2 -0
- package/dist/esm/queries/for_QueryResultWithState/when_creating_from_query_result/without_readiness.d.ts.map +1 -0
- package/dist/esm/queries/for_QueryResultWithState/when_creating_from_query_result/without_readiness.js +19 -0
- package/dist/esm/queries/for_QueryResultWithState/when_creating_from_query_result/without_readiness.js.map +1 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/given/a_deferred_promise.d.ts +7 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/given/a_deferred_promise.d.ts.map +1 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/given/a_deferred_promise.js +12 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/given/a_deferred_promise.js.map +1 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/given/a_server_sent_event_hub_connection.d.ts +2 -1
- package/dist/esm/queries/for_ServerSentEventHubConnection/given/a_server_sent_event_hub_connection.d.ts.map +1 -1
- package/dist/esm/queries/for_ServerSentEventHubConnection/given/a_server_sent_event_hub_connection.js +15 -4
- package/dist/esm/queries/for_ServerSentEventHubConnection/given/a_server_sent_event_hub_connection.js.map +1 -1
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_a_retired_event_source_fires/ignores_connected_and_error_callbacks.d.ts +2 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_a_retired_event_source_fires/ignores_connected_and_error_callbacks.d.ts.map +1 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_a_retired_event_source_fires/ignores_connected_and_error_callbacks.js +44 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_a_retired_event_source_fires/ignores_connected_and_error_callbacks.js.map +1 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_connection_changes_while_subscribe_is_in_flight/and_the_query_is_replaced.d.ts +2 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_connection_changes_while_subscribe_is_in_flight/and_the_query_is_replaced.d.ts.map +1 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_connection_changes_while_subscribe_is_in_flight/and_the_query_is_replaced.js +74 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_connection_changes_while_subscribe_is_in_flight/and_the_query_is_replaced.js.map +1 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_connection_changes_while_subscribe_is_in_flight/and_the_query_is_unsubscribed.d.ts +2 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_connection_changes_while_subscribe_is_in_flight/and_the_query_is_unsubscribed.d.ts.map +1 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_connection_changes_while_subscribe_is_in_flight/and_the_query_is_unsubscribed.js +74 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_connection_changes_while_subscribe_is_in_flight/and_the_query_is_unsubscribed.js.map +1 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_connection_changes_while_subscribe_is_in_flight/and_the_stale_request_is_rejected.d.ts +2 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_connection_changes_while_subscribe_is_in_flight/and_the_stale_request_is_rejected.d.ts.map +1 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_connection_changes_while_subscribe_is_in_flight/and_the_stale_request_is_rejected.js +59 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_connection_changes_while_subscribe_is_in_flight/and_the_stale_request_is_rejected.js.map +1 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_connection_changes_while_subscribe_is_in_flight/and_the_stale_request_returns_non_ok.d.ts +2 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_connection_changes_while_subscribe_is_in_flight/and_the_stale_request_returns_non_ok.d.ts.map +1 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_connection_changes_while_subscribe_is_in_flight/and_the_stale_request_returns_non_ok.js +58 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_connection_changes_while_subscribe_is_in_flight/and_the_stale_request_returns_non_ok.js.map +1 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_disposing/cleans_up_resources.js +6 -3
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_disposing/cleans_up_resources.js.map +1 -1
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_receiving_unauthorized/notifies_subscriber_and_removes_subscription.js +23 -6
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_receiving_unauthorized/notifies_subscriber_and_removes_subscription.js.map +1 -1
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_reconnecting/after_error.js +20 -5
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_reconnecting/after_error.js.map +1 -1
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_reconnecting/and_an_immediate_open_wins_the_backoff_race.d.ts +2 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_reconnecting/and_an_immediate_open_wins_the_backoff_race.d.ts.map +1 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_reconnecting/and_an_immediate_open_wins_the_backoff_race.js +20 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_reconnecting/and_an_immediate_open_wins_the_backoff_race.js.map +1 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_replacing_a_subscription/ignores_messages_from_the_retired_generation.d.ts +2 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_replacing_a_subscription/ignores_messages_from_the_retired_generation.d.ts.map +1 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_replacing_a_subscription/ignores_messages_from_the_retired_generation.js +68 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_replacing_a_subscription/ignores_messages_from_the_retired_generation.js.map +1 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_subscribing/sends_subscribe_post.js +38 -13
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_subscribing/sends_subscribe_post.js.map +1 -1
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_subscription_revisions_are_not_advertised/uses_legacy_protocol.d.ts +2 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_subscription_revisions_are_not_advertised/uses_legacy_protocol.d.ts.map +1 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_subscription_revisions_are_not_advertised/uses_legacy_protocol.js +54 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_subscription_revisions_are_not_advertised/uses_legacy_protocol.js.map +1 -0
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_unsubscribing/the_only_query.js +18 -4
- package/dist/esm/queries/for_ServerSentEventHubConnection/when_unsubscribing/the_only_query.js.map +1 -1
- package/dist/esm/queries/for_WebSocketHubConnection/when_receiving_unauthorized/notifies_subscriber_and_removes_subscription.js +22 -5
- package/dist/esm/queries/for_WebSocketHubConnection/when_receiving_unauthorized/notifies_subscriber_and_removes_subscription.js.map +1 -1
- package/dist/esm/queries/for_WebSocketHubConnection/when_reconnecting/ignores_a_pending_reconnect_and_stale_socket_callbacks.d.ts +2 -0
- package/dist/esm/queries/for_WebSocketHubConnection/when_reconnecting/ignores_a_pending_reconnect_and_stale_socket_callbacks.d.ts.map +1 -0
- package/dist/esm/queries/for_WebSocketHubConnection/when_reconnecting/ignores_a_pending_reconnect_and_stale_socket_callbacks.js +108 -0
- package/dist/esm/queries/for_WebSocketHubConnection/when_reconnecting/ignores_a_pending_reconnect_and_stale_socket_callbacks.js.map +1 -0
- package/dist/esm/queries/for_WebSocketHubConnection/when_reconnecting/replaces_a_closing_socket_safely.d.ts +2 -0
- package/dist/esm/queries/for_WebSocketHubConnection/when_reconnecting/replaces_a_closing_socket_safely.d.ts.map +1 -0
- package/dist/esm/queries/for_WebSocketHubConnection/when_reconnecting/replaces_a_closing_socket_safely.js +33 -0
- package/dist/esm/queries/for_WebSocketHubConnection/when_reconnecting/replaces_a_closing_socket_safely.js.map +1 -0
- package/dist/esm/queries/for_WebSocketHubConnection/when_reconnecting/resubscribes_all_queries.js +26 -4
- package/dist/esm/queries/for_WebSocketHubConnection/when_reconnecting/resubscribes_all_queries.js.map +1 -1
- package/dist/esm/queries/for_WebSocketHubConnection/when_replacing_a_subscription/ignores_stale_result_unauthorized_and_error.d.ts +2 -0
- package/dist/esm/queries/for_WebSocketHubConnection/when_replacing_a_subscription/ignores_stale_result_unauthorized_and_error.d.ts.map +1 -0
- package/dist/esm/queries/for_WebSocketHubConnection/when_replacing_a_subscription/ignores_stale_result_unauthorized_and_error.js +62 -0
- package/dist/esm/queries/for_WebSocketHubConnection/when_replacing_a_subscription/ignores_stale_result_unauthorized_and_error.js.map +1 -0
- package/dist/esm/queries/for_WebSocketHubConnection/when_subscribing/to_a_query.js +18 -5
- package/dist/esm/queries/for_WebSocketHubConnection/when_subscribing/to_a_query.js.map +1 -1
- package/dist/esm/queries/for_WebSocketHubConnection/when_subscription_revisions_are_supported/upgrades_active_subscriptions_to_strict_mode.d.ts +2 -0
- package/dist/esm/queries/for_WebSocketHubConnection/when_subscription_revisions_are_supported/upgrades_active_subscriptions_to_strict_mode.d.ts.map +1 -0
- package/dist/esm/queries/for_WebSocketHubConnection/when_subscription_revisions_are_supported/upgrades_active_subscriptions_to_strict_mode.js +56 -0
- package/dist/esm/queries/for_WebSocketHubConnection/when_subscription_revisions_are_supported/upgrades_active_subscriptions_to_strict_mode.js.map +1 -0
- package/dist/esm/queries/for_WebSocketHubConnection/when_unsubscribing/the_only_query.js +16 -3
- package/dist/esm/queries/for_WebSocketHubConnection/when_unsubscribing/the_only_query.js.map +1 -1
- package/dist/esm/queries/for_WebSocketHubConnection/when_unsubscribing/with_revision_support_not_advertised.d.ts +2 -0
- package/dist/esm/queries/for_WebSocketHubConnection/when_unsubscribing/with_revision_support_not_advertised.d.ts.map +1 -0
- package/dist/esm/queries/for_WebSocketHubConnection/when_unsubscribing/with_revision_support_not_advertised.js +33 -0
- package/dist/esm/queries/for_WebSocketHubConnection/when_unsubscribing/with_revision_support_not_advertised.js.map +1 -0
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/queries/IQueryResult.ts +10 -0
- package/queries/ObservableQueryFor.ts +44 -16
- package/queries/QueryInstanceCache.ts +42 -19
- package/queries/QueryResult.ts +145 -79
- package/queries/QueryResultWithState.ts +32 -9
- package/queries/ServerSentEventHubConnection.ts +270 -83
- package/queries/WebSocketHubConnection.ts +188 -56
- package/queries/for_ObservableQueryFor/when_performing/with_a_not_ready_result.ts +46 -0
- package/queries/for_ObservableQueryFor/when_subscribing/with_direct_mode_and_sse_transport.ts +91 -57
- package/queries/for_ObservableQueryFor/when_subscribing/with_direct_mode_disabled_and_sse_transport.ts +125 -53
- package/queries/for_ObservableQueryFor/when_subscribing/with_web_socket_hub_payload_without_readiness.ts +107 -0
- package/queries/for_QueryInstanceCache/when_setting_last_result/with_only_readiness_changed.ts +63 -0
- package/queries/for_QueryResult/when_constructing_from_a_server_result/with_readiness_false.ts +25 -0
- package/queries/for_QueryResult/when_constructing_from_a_server_result/without_readiness.ts +24 -0
- package/queries/for_QueryResultWithState/when_creating_from_query_result/with_readiness_false.ts +28 -0
- package/queries/for_QueryResultWithState/when_creating_from_query_result/without_readiness.ts +25 -0
- package/queries/for_ServerSentEventHubConnection/given/a_deferred_promise.ts +15 -0
- package/queries/for_ServerSentEventHubConnection/given/a_server_sent_event_hub_connection.ts +18 -7
- package/queries/for_ServerSentEventHubConnection/when_a_retired_event_source_fires/ignores_connected_and_error_callbacks.ts +59 -0
- package/queries/for_ServerSentEventHubConnection/when_connection_changes_while_subscribe_is_in_flight/and_the_query_is_replaced.ts +113 -0
- package/queries/for_ServerSentEventHubConnection/when_connection_changes_while_subscribe_is_in_flight/and_the_query_is_unsubscribed.ts +109 -0
- package/queries/for_ServerSentEventHubConnection/when_connection_changes_while_subscribe_is_in_flight/and_the_stale_request_is_rejected.ts +81 -0
- package/queries/for_ServerSentEventHubConnection/when_connection_changes_while_subscribe_is_in_flight/and_the_stale_request_returns_non_ok.ts +83 -0
- package/queries/for_ServerSentEventHubConnection/when_disposing/cleans_up_resources.ts +30 -21
- package/queries/for_ServerSentEventHubConnection/when_receiving_unauthorized/notifies_subscriber_and_removes_subscription.ts +56 -32
- package/queries/for_ServerSentEventHubConnection/when_reconnecting/after_error.ts +23 -6
- package/queries/for_ServerSentEventHubConnection/when_reconnecting/and_an_immediate_open_wins_the_backoff_race.ts +41 -0
- package/queries/for_ServerSentEventHubConnection/when_replacing_a_subscription/ignores_messages_from_the_retired_generation.ts +92 -0
- package/queries/for_ServerSentEventHubConnection/when_subscribing/sends_subscribe_post.ts +69 -30
- package/queries/for_ServerSentEventHubConnection/when_subscription_revisions_are_not_advertised/uses_legacy_protocol.ts +69 -0
- package/queries/for_ServerSentEventHubConnection/when_unsubscribing/the_only_query.ts +43 -24
- package/queries/for_WebSocketHubConnection/when_receiving_unauthorized/notifies_subscriber_and_removes_subscription.ts +53 -29
- package/queries/for_WebSocketHubConnection/when_reconnecting/ignores_a_pending_reconnect_and_stale_socket_callbacks.ts +131 -0
- package/queries/for_WebSocketHubConnection/when_reconnecting/replaces_a_closing_socket_safely.ts +39 -0
- package/queries/for_WebSocketHubConnection/when_reconnecting/resubscribes_all_queries.ts +72 -36
- package/queries/for_WebSocketHubConnection/when_replacing_a_subscription/ignores_stale_result_unauthorized_and_error.ts +88 -0
- package/queries/for_WebSocketHubConnection/when_subscribing/to_a_query.ts +54 -27
- package/queries/for_WebSocketHubConnection/when_subscription_revisions_are_supported/upgrades_active_subscriptions_to_strict_mode.ts +72 -0
- package/queries/for_WebSocketHubConnection/when_unsubscribing/the_only_query.ts +43 -18
- package/queries/for_WebSocketHubConnection/when_unsubscribing/with_revision_support_not_advertised.ts +44 -0
|
@@ -25,7 +25,9 @@ import { QueryResult } from './QueryResult.js';
|
|
|
25
25
|
*
|
|
26
26
|
* Multiple query subscriptions are carried over the same physical WebSocket. Each subscription
|
|
27
27
|
* is identified by a client-generated {@code queryId}; the server tags every result message with
|
|
28
|
-
* the same id so responses can be routed to the correct callback.
|
|
28
|
+
* the same id so responses can be routed to the correct callback. The client initially subscribes
|
|
29
|
+
* without revisions for compatibility with older servers, then replaces active subscriptions with
|
|
30
|
+
* revision-aware operations when the server advertises support in a Connected message.
|
|
29
31
|
*/ class WebSocketHubConnection {
|
|
30
32
|
_url;
|
|
31
33
|
_microservice;
|
|
@@ -37,12 +39,15 @@ import { QueryResult } from './QueryResult.js';
|
|
|
37
39
|
_lastPingSentTime;
|
|
38
40
|
_lastPongLatency = 0;
|
|
39
41
|
_latencySamples = [];
|
|
42
|
+
_nextRevision = 0;
|
|
43
|
+
_supportsSubscriptionRevisions = false;
|
|
44
|
+
_reconnectPending = false;
|
|
40
45
|
/**
|
|
41
46
|
* Initializes a new instance of {@link WebSocketHubConnection}.
|
|
42
|
-
* @param {string}
|
|
43
|
-
* @param {string}
|
|
47
|
+
* @param {string} _url The WebSocket URL of the hub endpoint (e.g. {@code ws://localhost:5000/.cratis/queries/ws}).
|
|
48
|
+
* @param {string} _microservice The microservice name to pass as a query argument.
|
|
44
49
|
* @param {number} pingIntervalMs How often to send keep-alive pings when the connection is idle (default: 10 000 ms).
|
|
45
|
-
* @param {IReconnectPolicy}
|
|
50
|
+
* @param {IReconnectPolicy} _policy The reconnect policy to use (default: {@link ReconnectPolicy}).
|
|
46
51
|
*/ constructor(_url, _microservice, pingIntervalMs = 10000, _policy = new ReconnectPolicy()){
|
|
47
52
|
this._url = _url;
|
|
48
53
|
this._microservice = _microservice;
|
|
@@ -85,13 +90,15 @@ import { QueryResult } from './QueryResult.js';
|
|
|
85
90
|
* @param {SubscriptionRequest} request The subscription request payload.
|
|
86
91
|
* @param {DataReceived<any>} callback Callback invoked whenever the server pushes a result for this query.
|
|
87
92
|
*/ subscribe(queryId, request, callback) {
|
|
88
|
-
|
|
93
|
+
const subscription = {
|
|
89
94
|
request,
|
|
90
|
-
callback
|
|
91
|
-
|
|
95
|
+
callback,
|
|
96
|
+
revision: this.getNextRevision()
|
|
97
|
+
};
|
|
98
|
+
this._subscriptions.set(queryId, subscription);
|
|
92
99
|
this.ensureConnected();
|
|
93
100
|
if (this._socket?.readyState === WebSocket.OPEN) {
|
|
94
|
-
this.sendSubscribeMessage(queryId,
|
|
101
|
+
this.sendSubscribeMessage(queryId, subscription);
|
|
95
102
|
}
|
|
96
103
|
// If not yet open, sendAllSubscriptions will fire in onopen.
|
|
97
104
|
}
|
|
@@ -99,12 +106,17 @@ import { QueryResult } from './QueryResult.js';
|
|
|
99
106
|
* Unsubscribe from a query on this hub connection.
|
|
100
107
|
* @param {string} queryId The identifier of the subscription to cancel.
|
|
101
108
|
*/ unsubscribe(queryId) {
|
|
109
|
+
const subscription = this._subscriptions.get(queryId);
|
|
102
110
|
this._subscriptions.delete(queryId);
|
|
103
|
-
if (this._socket?.readyState === WebSocket.OPEN) {
|
|
104
|
-
|
|
111
|
+
if (this._socket?.readyState === WebSocket.OPEN && subscription) {
|
|
112
|
+
const message = {
|
|
105
113
|
type: "Unsubscribe",
|
|
106
114
|
queryId
|
|
107
|
-
}
|
|
115
|
+
};
|
|
116
|
+
if (this._supportsSubscriptionRevisions) {
|
|
117
|
+
message.revision = subscription.revision;
|
|
118
|
+
}
|
|
119
|
+
this.sendMessage(message);
|
|
108
120
|
}
|
|
109
121
|
// If no subscriptions remain, close the connection to free resources.
|
|
110
122
|
if (this._subscriptions.size === 0) {
|
|
@@ -114,12 +126,8 @@ import { QueryResult } from './QueryResult.js';
|
|
|
114
126
|
/**
|
|
115
127
|
* Permanently close this hub connection and clean up all subscriptions.
|
|
116
128
|
*/ dispose() {
|
|
117
|
-
this._disconnected = true;
|
|
118
129
|
this._subscriptions.clear();
|
|
119
|
-
this.
|
|
120
|
-
this._policy.cancel();
|
|
121
|
-
this._socket?.close();
|
|
122
|
-
this._socket = undefined;
|
|
130
|
+
this.close();
|
|
123
131
|
}
|
|
124
132
|
ensureConnected() {
|
|
125
133
|
if (this._disconnected) {
|
|
@@ -127,75 +135,107 @@ import { QueryResult } from './QueryResult.js';
|
|
|
127
135
|
this._disconnected = false;
|
|
128
136
|
}
|
|
129
137
|
if (this._socket && (this._socket.readyState === WebSocket.OPEN || this._socket.readyState === WebSocket.CONNECTING)) {
|
|
138
|
+
this.cancelPendingReconnect();
|
|
130
139
|
return;
|
|
131
140
|
}
|
|
141
|
+
// A subscription can arrive while an earlier close is waiting in reconnect backoff. The immediate open owns
|
|
142
|
+
// the connection now, so the delayed callback must not be allowed to create a competing socket later.
|
|
143
|
+
this.cancelPendingReconnect();
|
|
132
144
|
this.openSocket();
|
|
133
145
|
}
|
|
134
146
|
close() {
|
|
135
147
|
this._disconnected = true;
|
|
136
148
|
this._keepAlive.stop();
|
|
137
149
|
this._policy.cancel();
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
// fire after a new subscription has reset _disconnected to false and opened a
|
|
141
|
-
// fresh socket. Without this, the stale onclose triggers an unintended
|
|
142
|
-
// reconnect via the back-off policy, causing a 1-10 second delay before the
|
|
143
|
-
// new page's queries receive their first data.
|
|
144
|
-
this._socket.onopen = null;
|
|
145
|
-
this._socket.onclose = null;
|
|
146
|
-
this._socket.onerror = null;
|
|
147
|
-
this._socket.onmessage = null;
|
|
148
|
-
this._socket.close();
|
|
149
|
-
}
|
|
150
|
+
this._reconnectPending = false;
|
|
151
|
+
const socket = this._socket;
|
|
150
152
|
this._socket = undefined;
|
|
153
|
+
this._supportsSubscriptionRevisions = false;
|
|
154
|
+
if (socket) {
|
|
155
|
+
this.detachAndCloseSocket(socket);
|
|
156
|
+
}
|
|
151
157
|
}
|
|
152
158
|
openSocket() {
|
|
159
|
+
const currentSocket = this._socket;
|
|
160
|
+
if (currentSocket && (currentSocket.readyState === WebSocket.OPEN || currentSocket.readyState === WebSocket.CONNECTING)) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (currentSocket) {
|
|
164
|
+
this._socket = undefined;
|
|
165
|
+
this.detachAndCloseSocket(currentSocket);
|
|
166
|
+
}
|
|
167
|
+
this._supportsSubscriptionRevisions = false;
|
|
153
168
|
let url = this._url;
|
|
154
169
|
if (this._microservice?.length > 0) {
|
|
155
170
|
const param = `${Globals.microserviceWSQueryArgument}=${encodeURIComponent(this._microservice)}`;
|
|
156
171
|
url += (url.includes('?') ? '&' : '?') + param;
|
|
157
172
|
}
|
|
158
|
-
|
|
159
|
-
this._socket
|
|
160
|
-
|
|
161
|
-
|
|
173
|
+
const socket = new WebSocket(url);
|
|
174
|
+
this._socket = socket;
|
|
175
|
+
socket.onopen = ()=>{
|
|
176
|
+
if (this._socket !== socket || this._disconnected) return;
|
|
177
|
+
console.log('Hub connection established', url);
|
|
178
|
+
this.cancelPendingReconnect();
|
|
162
179
|
this._policy.reset();
|
|
163
180
|
this._keepAlive.start();
|
|
164
181
|
this.sendAllSubscriptions();
|
|
165
182
|
};
|
|
166
|
-
|
|
167
|
-
if (this._disconnected) return;
|
|
168
|
-
console.log(
|
|
183
|
+
socket.onclose = ()=>{
|
|
184
|
+
if (this._socket !== socket || this._disconnected) return;
|
|
185
|
+
console.log('Hub connection closed', url);
|
|
186
|
+
this._socket = undefined;
|
|
187
|
+
this._supportsSubscriptionRevisions = false;
|
|
169
188
|
this._keepAlive.stop();
|
|
170
189
|
if (this._subscriptions.size === 0) return;
|
|
171
|
-
this.
|
|
190
|
+
this._reconnectPending = true;
|
|
191
|
+
const scheduled = this._policy.schedule(()=>{
|
|
192
|
+
this._reconnectPending = false;
|
|
172
193
|
if (!this._disconnected && this._subscriptions.size > 0) {
|
|
173
|
-
this.
|
|
194
|
+
this.ensureConnected();
|
|
174
195
|
}
|
|
175
196
|
}, this._url);
|
|
197
|
+
if (!scheduled) {
|
|
198
|
+
this._reconnectPending = false;
|
|
199
|
+
}
|
|
176
200
|
};
|
|
177
|
-
|
|
178
|
-
if (this._disconnected) return;
|
|
179
|
-
console.error(
|
|
201
|
+
socket.onerror = (error)=>{
|
|
202
|
+
if (this._socket !== socket || this._disconnected) return;
|
|
203
|
+
console.error('Hub connection error', url, error);
|
|
180
204
|
this._keepAlive.stop();
|
|
181
205
|
// onclose will fire after onerror, triggering reconnect
|
|
182
206
|
};
|
|
183
|
-
|
|
184
|
-
if (this._disconnected) return;
|
|
207
|
+
socket.onmessage = (ev)=>{
|
|
208
|
+
if (this._socket !== socket || this._disconnected) return;
|
|
185
209
|
this.handleMessage(ev.data);
|
|
186
210
|
};
|
|
187
211
|
}
|
|
212
|
+
cancelPendingReconnect() {
|
|
213
|
+
if (!this._reconnectPending) return;
|
|
214
|
+
this._policy.cancel();
|
|
215
|
+
this._reconnectPending = false;
|
|
216
|
+
}
|
|
217
|
+
detachAndCloseSocket(socket) {
|
|
218
|
+
socket.onopen = null;
|
|
219
|
+
socket.onclose = null;
|
|
220
|
+
socket.onerror = null;
|
|
221
|
+
socket.onmessage = null;
|
|
222
|
+
socket.close();
|
|
223
|
+
}
|
|
188
224
|
sendAllSubscriptions() {
|
|
189
225
|
for (const [queryId, sub] of this._subscriptions){
|
|
190
|
-
this.sendSubscribeMessage(queryId, sub
|
|
226
|
+
this.sendSubscribeMessage(queryId, sub);
|
|
191
227
|
}
|
|
192
228
|
}
|
|
193
|
-
sendSubscribeMessage(queryId,
|
|
194
|
-
|
|
229
|
+
sendSubscribeMessage(queryId, subscription) {
|
|
230
|
+
const message = {
|
|
195
231
|
type: "Subscribe",
|
|
196
232
|
queryId,
|
|
197
|
-
payload: request
|
|
198
|
-
}
|
|
233
|
+
payload: subscription.request
|
|
234
|
+
};
|
|
235
|
+
if (this._supportsSubscriptionRevisions) {
|
|
236
|
+
message.revision = subscription.revision;
|
|
237
|
+
}
|
|
238
|
+
this.sendMessage(message);
|
|
199
239
|
}
|
|
200
240
|
sendMessage(message) {
|
|
201
241
|
if (this._socket?.readyState === WebSocket.OPEN) {
|
|
@@ -208,6 +248,9 @@ import { QueryResult } from './QueryResult.js';
|
|
|
208
248
|
// Every received message is activity — skip keep-alive ping if data is flowing.
|
|
209
249
|
this._keepAlive.recordActivity();
|
|
210
250
|
switch(message.type){
|
|
251
|
+
case "Connected":
|
|
252
|
+
this.handleConnected(message);
|
|
253
|
+
break;
|
|
211
254
|
case "QueryResult":
|
|
212
255
|
this.handleQueryResult(message);
|
|
213
256
|
break;
|
|
@@ -215,31 +258,58 @@ import { QueryResult } from './QueryResult.js';
|
|
|
215
258
|
this.handlePong(message);
|
|
216
259
|
break;
|
|
217
260
|
case "Unauthorized":
|
|
218
|
-
console.warn(
|
|
261
|
+
console.warn('Hub: query unauthorized', message.queryId);
|
|
219
262
|
this.handleUnauthorized(message);
|
|
220
263
|
break;
|
|
221
264
|
case "Error":
|
|
222
|
-
|
|
265
|
+
if (this.isMessageForCurrentSubscription(message)) {
|
|
266
|
+
console.error('Hub: query error', message.queryId, message.payload);
|
|
267
|
+
}
|
|
223
268
|
break;
|
|
224
269
|
}
|
|
225
270
|
} catch (error) {
|
|
226
271
|
console.error('Hub: error parsing message', error);
|
|
227
272
|
}
|
|
228
273
|
}
|
|
274
|
+
handleConnected(message) {
|
|
275
|
+
if (message.supportsSubscriptionRevisions !== true || this._supportsSubscriptionRevisions) {
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
this._supportsSubscriptionRevisions = true;
|
|
279
|
+
// The legacy subscriptions sent on socket open keep this client compatible with older servers.
|
|
280
|
+
// Ordered WebSocket delivery means these revision-aware replacements are processed afterwards.
|
|
281
|
+
this.sendAllSubscriptions();
|
|
282
|
+
}
|
|
229
283
|
handleQueryResult(message) {
|
|
230
284
|
if (!message.queryId) return;
|
|
231
285
|
const sub = this._subscriptions.get(message.queryId);
|
|
232
|
-
if (!sub) return;
|
|
286
|
+
if (!sub || !this.isMessageForCurrentSubscription(message)) return;
|
|
233
287
|
const result = message.payload;
|
|
234
288
|
sub.callback(result);
|
|
235
289
|
}
|
|
236
290
|
handleUnauthorized(message) {
|
|
237
291
|
if (!message.queryId) return;
|
|
238
292
|
const sub = this._subscriptions.get(message.queryId);
|
|
239
|
-
if (!sub) return;
|
|
293
|
+
if (!sub || !this.isMessageForCurrentSubscription(message)) return;
|
|
240
294
|
this._subscriptions.delete(message.queryId);
|
|
241
295
|
sub.callback(QueryResult.unauthorized());
|
|
242
296
|
}
|
|
297
|
+
isMessageForCurrentSubscription(message) {
|
|
298
|
+
if (!message.queryId) return false;
|
|
299
|
+
if (!this._supportsSubscriptionRevisions) {
|
|
300
|
+
return message.revision === undefined && this._subscriptions.has(message.queryId);
|
|
301
|
+
}
|
|
302
|
+
return this.isValidRevision(message.revision) && this._subscriptions.get(message.queryId)?.revision === message.revision;
|
|
303
|
+
}
|
|
304
|
+
isValidRevision(revision) {
|
|
305
|
+
return revision !== undefined && Number.isSafeInteger(revision) && revision > 0;
|
|
306
|
+
}
|
|
307
|
+
getNextRevision() {
|
|
308
|
+
if (this._nextRevision >= Number.MAX_SAFE_INTEGER) {
|
|
309
|
+
throw new RangeError('WebSocket hub subscription revision exhausted the safe integer range');
|
|
310
|
+
}
|
|
311
|
+
return ++this._nextRevision;
|
|
312
|
+
}
|
|
243
313
|
handlePong(message) {
|
|
244
314
|
if (message.timestamp && this._lastPingSentTime) {
|
|
245
315
|
const latency = Date.now() - message.timestamp;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WebSocketHubConnection.js","sources":["../../../queries/WebSocketHubConnection.ts"],"sourcesContent":["// Copyright (c) Cratis. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for full license information.\n\nimport { Globals } from '../Globals';\nimport { DataReceived } from './ObservableQueryConnection';\nimport { HubConnectionKeepAlive } from './HubConnectionKeepAlive';\nimport { IReconnectPolicy } from './IReconnectPolicy';\nimport { ReconnectPolicy } from './ReconnectPolicy';\nimport { QueryResult } from './QueryResult';\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\n\n/**\n * Message types matching the backend {@link ObservableQueryHubMessageType} enum.\n * Serialized as strings by the JsonStringEnumConverter on the server.\n */\nexport enum HubMessageType {\n Subscribe = 'Subscribe',\n Unsubscribe = 'Unsubscribe',\n QueryResult = 'QueryResult',\n Unauthorized = 'Unauthorized',\n Error = 'Error',\n Ping = 'Ping',\n Pong = 'Pong',\n Connected = 'Connected',\n}\n\n/**\n * Wire format for messages exchanged over the {@link WebSocketHubConnection}.\n */\nexport interface HubMessage {\n type: HubMessageType;\n queryId?: string;\n payload?: any;\n timestamp?: number;\n\n /**\n * The server's keep-alive interval in milliseconds, or 0 when keep-alive is disabled.\n * Only present on {@link HubMessageType.Connected} messages.\n */\n keepAliveIntervalMs?: number;\n}\n\n/**\n * Matches the backend {@link ObservableQuerySubscriptionRequest} record.\n */\nexport interface SubscriptionRequest {\n queryName: string;\n arguments?: Record<string, string | null>;\n page?: number;\n pageSize?: number;\n sortBy?: string;\n sortDirection?: string;\n transferMode?: string;\n}\n\ninterface ActiveSubscription {\n request: SubscriptionRequest;\n callback: DataReceived<any>;\n}\n\n/**\n * Represents a single multiplexed WebSocket connection to the observable query hub\n * at {@code /.cratis/queries/ws}.\n *\n * Multiple query subscriptions are carried over the same physical WebSocket. Each subscription\n * is identified by a client-generated {@code queryId}; the server tags every result message with\n * the same id so responses can be routed to the correct callback.\n */\nexport class WebSocketHubConnection {\n private _socket?: WebSocket;\n private _disconnected = false;\n private _subscriptions: Map<string, ActiveSubscription> = new Map();\n private readonly _keepAlive: HubConnectionKeepAlive;\n private _lastPingSentTime?: number;\n private _lastPongLatency: number = 0;\n private _latencySamples: number[] = [];\n\n /**\n * Initializes a new instance of {@link WebSocketHubConnection}.\n * @param {string} url The WebSocket URL of the hub endpoint (e.g. {@code ws://localhost:5000/.cratis/queries/ws}).\n * @param {string} microservice The microservice name to pass as a query argument.\n * @param {number} pingIntervalMs How often to send keep-alive pings when the connection is idle (default: 10 000 ms).\n * @param {IReconnectPolicy} reconnectPolicy The reconnect policy to use (default: {@link ReconnectPolicy}).\n */\n constructor(\n private readonly _url: string,\n private readonly _microservice: string,\n pingIntervalMs: number = 10000,\n private readonly _policy: IReconnectPolicy = new ReconnectPolicy()\n ) {\n this._keepAlive = new HubConnectionKeepAlive(pingIntervalMs, () => {\n if (this._socket?.readyState === WebSocket.OPEN) {\n this._lastPingSentTime = Date.now();\n this.sendMessage({ type: HubMessageType.Ping, timestamp: this._lastPingSentTime });\n }\n });\n }\n\n /**\n * Gets the number of active query subscriptions on this connection.\n */\n get queryCount(): number {\n return this._subscriptions.size;\n }\n\n /**\n * Gets whether the WebSocket connection is currently open.\n */\n get isConnected(): boolean {\n return this._socket?.readyState === WebSocket.OPEN;\n }\n\n /**\n * Gets the latency of the last ping/pong sequence in milliseconds.\n */\n get lastPingLatency(): number {\n return this._lastPongLatency;\n }\n\n /**\n * Gets the rolling average latency in milliseconds.\n */\n get averageLatency(): number {\n if (this._latencySamples.length === 0) return 0;\n return this._latencySamples.reduce((a, b) => a + b, 0) / this._latencySamples.length;\n }\n\n /**\n * Subscribe to a query on this hub connection.\n * If the WebSocket is not yet open, the subscribe message will be sent once the connection is established.\n * @param {string} queryId Client-generated unique identifier for this subscription.\n * @param {SubscriptionRequest} request The subscription request payload.\n * @param {DataReceived<any>} callback Callback invoked whenever the server pushes a result for this query.\n */\n subscribe(queryId: string, request: SubscriptionRequest, callback: DataReceived<any>): void {\n this._subscriptions.set(queryId, { request, callback });\n this.ensureConnected();\n\n if (this._socket?.readyState === WebSocket.OPEN) {\n this.sendSubscribeMessage(queryId, request);\n }\n // If not yet open, sendAllSubscriptions will fire in onopen.\n }\n\n /**\n * Unsubscribe from a query on this hub connection.\n * @param {string} queryId The identifier of the subscription to cancel.\n */\n unsubscribe(queryId: string): void {\n this._subscriptions.delete(queryId);\n\n if (this._socket?.readyState === WebSocket.OPEN) {\n this.sendMessage({ type: HubMessageType.Unsubscribe, queryId });\n }\n\n // If no subscriptions remain, close the connection to free resources.\n if (this._subscriptions.size === 0) {\n this.close();\n }\n }\n\n /**\n * Permanently close this hub connection and clean up all subscriptions.\n */\n dispose(): void {\n this._disconnected = true;\n this._subscriptions.clear();\n this._keepAlive.stop();\n this._policy.cancel();\n this._socket?.close();\n this._socket = undefined;\n }\n\n private ensureConnected(): void {\n if (this._disconnected) {\n // Reset disconnected flag when a new subscription comes in\n this._disconnected = false;\n }\n\n if (this._socket && (this._socket.readyState === WebSocket.OPEN || this._socket.readyState === WebSocket.CONNECTING)) {\n return;\n }\n\n this.openSocket();\n }\n\n private close(): void {\n this._disconnected = true;\n this._keepAlive.stop();\n this._policy.cancel();\n if (this._socket) {\n // Detach all handlers BEFORE closing so that the async onclose event cannot\n // fire after a new subscription has reset _disconnected to false and opened a\n // fresh socket. Without this, the stale onclose triggers an unintended\n // reconnect via the back-off policy, causing a 1-10 second delay before the\n // new page's queries receive their first data.\n this._socket.onopen = null;\n this._socket.onclose = null;\n this._socket.onerror = null;\n this._socket.onmessage = null;\n this._socket.close();\n }\n this._socket = undefined;\n }\n\n private openSocket(): void {\n let url = this._url;\n if (this._microservice?.length > 0) {\n const param = `${Globals.microserviceWSQueryArgument}=${encodeURIComponent(this._microservice)}`;\n url += (url.includes('?') ? '&' : '?') + param;\n }\n\n this._socket = new WebSocket(url);\n\n this._socket.onopen = () => {\n if (this._disconnected) return;\n console.log(`Hub connection established: '${url}'`);\n this._policy.reset();\n this._keepAlive.start();\n this.sendAllSubscriptions();\n };\n\n this._socket.onclose = () => {\n if (this._disconnected) return;\n console.log(`Hub connection closed: '${url}'`);\n this._keepAlive.stop();\n if (this._subscriptions.size === 0) return;\n this._policy.schedule(() => {\n if (!this._disconnected && this._subscriptions.size > 0) {\n this.openSocket();\n }\n }, this._url);\n };\n\n this._socket.onerror = (error) => {\n if (this._disconnected) return;\n console.error(`Hub connection error: '${url}'`, error);\n this._keepAlive.stop();\n // onclose will fire after onerror, triggering reconnect\n };\n\n this._socket.onmessage = (ev) => {\n if (this._disconnected) return;\n this.handleMessage(ev.data as string);\n };\n }\n\n private sendAllSubscriptions(): void {\n for (const [queryId, sub] of this._subscriptions) {\n this.sendSubscribeMessage(queryId, sub.request);\n }\n }\n\n private sendSubscribeMessage(queryId: string, request: SubscriptionRequest): void {\n this.sendMessage({\n type: HubMessageType.Subscribe,\n queryId,\n payload: request,\n });\n }\n\n private sendMessage(message: HubMessage): void {\n if (this._socket?.readyState === WebSocket.OPEN) {\n this._socket.send(JSON.stringify(message));\n }\n }\n\n private handleMessage(rawData: string): void {\n try {\n const message = JSON.parse(rawData) as HubMessage;\n\n // Every received message is activity — skip keep-alive ping if data is flowing.\n this._keepAlive.recordActivity();\n\n switch (message.type) {\n case HubMessageType.QueryResult:\n this.handleQueryResult(message);\n break;\n case HubMessageType.Pong:\n this.handlePong(message);\n break;\n case HubMessageType.Unauthorized:\n console.warn(`Hub: query '${message.queryId}' unauthorized`);\n this.handleUnauthorized(message);\n break;\n case HubMessageType.Error:\n console.error(`Hub: query '${message.queryId}' error:`, message.payload);\n break;\n }\n } catch (error) {\n console.error('Hub: error parsing message', error);\n }\n }\n\n private handleQueryResult(message: HubMessage): void {\n if (!message.queryId) return;\n\n const sub = this._subscriptions.get(message.queryId);\n if (!sub) return;\n\n const result = message.payload as QueryResult<any>;\n sub.callback(result);\n }\n\n private handleUnauthorized(message: HubMessage): void {\n if (!message.queryId) return;\n\n const sub = this._subscriptions.get(message.queryId);\n if (!sub) return;\n\n this._subscriptions.delete(message.queryId);\n sub.callback(QueryResult.unauthorized());\n }\n\n private handlePong(message: HubMessage): void {\n if (message.timestamp && this._lastPingSentTime) {\n const latency = Date.now() - message.timestamp;\n this._lastPongLatency = latency;\n this._latencySamples.push(latency);\n\n if (this._latencySamples.length > 100) {\n this._latencySamples.shift();\n }\n }\n }\n}\n"],"names":["HubMessageType","WebSocketHubConnection","_socket","_disconnected","_subscriptions","Map","_keepAlive","_lastPingSentTime","_lastPongLatency","_latencySamples","_url","pingIntervalMs","_policy","ReconnectPolicy","_microservice","HubConnectionKeepAlive","readyState","WebSocket","OPEN","Date","now","sendMessage","type","timestamp","queryCount","size","isConnected","lastPingLatency","averageLatency","length","reduce","a","b","subscribe","queryId","request","callback","set","ensureConnected","sendSubscribeMessage","unsubscribe","delete","close","dispose","clear","stop","cancel","undefined","CONNECTING","openSocket","onopen","onclose","onerror","onmessage","url","param","Globals","microserviceWSQueryArgument","encodeURIComponent","includes","console","log","reset","start","sendAllSubscriptions","schedule","error","ev","handleMessage","data","sub","payload","message","send","JSON","stringify","rawData","parse","recordActivity","handleQueryResult","handlePong","warn","handleUnauthorized","get","result","QueryResult","unauthorized","latency","push","shift"],"mappings":";;;;;AAAA;AACA;AASA;;;IAMO,IAAKA,cAAAA,iBAAAA,SAAAA,cAAAA,EAAAA;;;;;;;;;AAAAA,IAAAA,OAAAA,cAAAA;AASX,CAAA,CAAA,EAAA;AAoCD;;;;;;;AAOC,IACM,MAAMC,sBAAAA,CAAAA;;;;IACDC,OAAAA;AACAC,IAAAA,aAAAA,GAAgB,KAAA;AAChBC,IAAAA,cAAAA,G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,CAAYC,YAAY,EAAA,CAAA;AACzC,IAAA;AAEQN,IAAAA,UAAAA,CAAWR,OAAmB,EAAQ;AAC1C,QAAA,IAAIA,QAAQjD,SAAS,IAAI,IAAI,CAAChB,iBAAiB,EAAE;AAC7C,YAAA,MAAMgF,OAAAA,GAAUpE,IAAAA,CAAKC,GAAG,EAAA,GAAKoD,QAAQjD,SAAS;YAC9C,IAAI,CAACf,gBAAgB,GAAG+E,OAAAA;AACxB,YAAA,IAAI,CAAC9E,eAAe,CAAC+E,IAAI,CAACD,OAAAA,CAAAA;AAE1B,YAAA,IAAI,IAAI,CAAC9E,eAAe,CAACoB,MAAM,GAAG,GAAA,EAAK;gBACnC,IAAI,CAACpB,eAAe,CAACgF,KAAK,EAAA;AAC9B,YAAA;AACJ,QAAA;AACJ,IAAA;AACJ;;;;"}
|
|
1
|
+
{"version":3,"file":"WebSocketHubConnection.js","sources":["../../../queries/WebSocketHubConnection.ts"],"sourcesContent":["// Copyright (c) Cratis. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for full license information.\n\nimport { Globals } from '../Globals';\nimport type { DataReceived } from './ObservableQueryConnection';\nimport { HubConnectionKeepAlive } from './HubConnectionKeepAlive';\nimport type { IReconnectPolicy } from './IReconnectPolicy';\nimport { ReconnectPolicy } from './ReconnectPolicy';\nimport { QueryResult } from './QueryResult';\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\n\n/**\n * Message types matching the backend {@link ObservableQueryHubMessageType} enum.\n * Serialized as strings by the JsonStringEnumConverter on the server.\n */\nexport enum HubMessageType {\n Subscribe = 'Subscribe',\n Unsubscribe = 'Unsubscribe',\n QueryResult = 'QueryResult',\n Unauthorized = 'Unauthorized',\n Error = 'Error',\n Ping = 'Ping',\n Pong = 'Pong',\n Connected = 'Connected',\n}\n\n/**\n * Wire format for messages exchanged over the observable query WebSocket and SSE hub transports.\n */\nexport interface HubMessage {\n type: HubMessageType;\n queryId?: string;\n\n /** Optional positive safe-integer revision used to reject stale replacement messages. */\n revision?: number;\n\n /**\n * Whether the server supports revision-aware subscription operations and result frames.\n * Only present on {@link HubMessageType.Connected} messages from capable servers.\n */\n supportsSubscriptionRevisions?: boolean;\n payload?: any;\n timestamp?: number;\n\n /**\n * The server's keep-alive interval in milliseconds, or 0 when keep-alive is disabled.\n * Only present on {@link HubMessageType.Connected} messages.\n */\n keepAliveIntervalMs?: number;\n}\n\n/**\n * Matches the backend {@link ObservableQuerySubscriptionRequest} record.\n */\nexport interface SubscriptionRequest {\n queryName: string;\n arguments?: Record<string, string | null>;\n page?: number;\n pageSize?: number;\n sortBy?: string;\n sortDirection?: string;\n transferMode?: string;\n}\n\ninterface ActiveSubscription {\n request: SubscriptionRequest;\n callback: DataReceived<any>;\n revision: number;\n}\n\n/**\n * Represents a single multiplexed WebSocket connection to the observable query hub\n * at {@code /.cratis/queries/ws}.\n *\n * Multiple query subscriptions are carried over the same physical WebSocket. Each subscription\n * is identified by a client-generated {@code queryId}; the server tags every result message with\n * the same id so responses can be routed to the correct callback. The client initially subscribes\n * without revisions for compatibility with older servers, then replaces active subscriptions with\n * revision-aware operations when the server advertises support in a Connected message.\n */\nexport class WebSocketHubConnection {\n private _socket?: WebSocket;\n private _disconnected = false;\n private _subscriptions: Map<string, ActiveSubscription> = new Map();\n private readonly _keepAlive: HubConnectionKeepAlive;\n private _lastPingSentTime?: number;\n private _lastPongLatency: number = 0;\n private _latencySamples: number[] = [];\n private _nextRevision = 0;\n private _supportsSubscriptionRevisions = false;\n private _reconnectPending = false;\n\n /**\n * Initializes a new instance of {@link WebSocketHubConnection}.\n * @param {string} _url The WebSocket URL of the hub endpoint (e.g. {@code ws://localhost:5000/.cratis/queries/ws}).\n * @param {string} _microservice The microservice name to pass as a query argument.\n * @param {number} pingIntervalMs How often to send keep-alive pings when the connection is idle (default: 10 000 ms).\n * @param {IReconnectPolicy} _policy The reconnect policy to use (default: {@link ReconnectPolicy}).\n */\n constructor(\n private readonly _url: string,\n private readonly _microservice: string,\n pingIntervalMs: number = 10000,\n private readonly _policy: IReconnectPolicy = new ReconnectPolicy(),\n ) {\n this._keepAlive = new HubConnectionKeepAlive(pingIntervalMs, () => {\n if (this._socket?.readyState === WebSocket.OPEN) {\n this._lastPingSentTime = Date.now();\n this.sendMessage({\n type: HubMessageType.Ping,\n timestamp: this._lastPingSentTime,\n });\n }\n });\n }\n\n /**\n * Gets the number of active query subscriptions on this connection.\n */\n get queryCount(): number {\n return this._subscriptions.size;\n }\n\n /**\n * Gets whether the WebSocket connection is currently open.\n */\n get isConnected(): boolean {\n return this._socket?.readyState === WebSocket.OPEN;\n }\n\n /**\n * Gets the latency of the last ping/pong sequence in milliseconds.\n */\n get lastPingLatency(): number {\n return this._lastPongLatency;\n }\n\n /**\n * Gets the rolling average latency in milliseconds.\n */\n get averageLatency(): number {\n if (this._latencySamples.length === 0) return 0;\n return (\n this._latencySamples.reduce((a, b) => a + b, 0) / this._latencySamples.length\n );\n }\n\n /**\n * Subscribe to a query on this hub connection.\n * If the WebSocket is not yet open, the subscribe message will be sent once the connection is established.\n * @param {string} queryId Client-generated unique identifier for this subscription.\n * @param {SubscriptionRequest} request The subscription request payload.\n * @param {DataReceived<any>} callback Callback invoked whenever the server pushes a result for this query.\n */\n subscribe(\n queryId: string,\n request: SubscriptionRequest,\n callback: DataReceived<any>,\n ): void {\n const subscription = { request, callback, revision: this.getNextRevision() };\n this._subscriptions.set(queryId, subscription);\n this.ensureConnected();\n\n if (this._socket?.readyState === WebSocket.OPEN) {\n this.sendSubscribeMessage(queryId, subscription);\n }\n // If not yet open, sendAllSubscriptions will fire in onopen.\n }\n\n /**\n * Unsubscribe from a query on this hub connection.\n * @param {string} queryId The identifier of the subscription to cancel.\n */\n unsubscribe(queryId: string): void {\n const subscription = this._subscriptions.get(queryId);\n this._subscriptions.delete(queryId);\n\n if (this._socket?.readyState === WebSocket.OPEN && subscription) {\n const message: HubMessage = {\n type: HubMessageType.Unsubscribe,\n queryId,\n };\n if (this._supportsSubscriptionRevisions) {\n message.revision = subscription.revision;\n }\n this.sendMessage(message);\n }\n\n // If no subscriptions remain, close the connection to free resources.\n if (this._subscriptions.size === 0) {\n this.close();\n }\n }\n\n /**\n * Permanently close this hub connection and clean up all subscriptions.\n */\n dispose(): void {\n this._subscriptions.clear();\n this.close();\n }\n\n private ensureConnected(): void {\n if (this._disconnected) {\n // Reset disconnected flag when a new subscription comes in\n this._disconnected = false;\n }\n\n if (\n this._socket &&\n (this._socket.readyState === WebSocket.OPEN ||\n this._socket.readyState === WebSocket.CONNECTING)\n ) {\n this.cancelPendingReconnect();\n return;\n }\n\n // A subscription can arrive while an earlier close is waiting in reconnect backoff. The immediate open owns\n // the connection now, so the delayed callback must not be allowed to create a competing socket later.\n this.cancelPendingReconnect();\n this.openSocket();\n }\n\n private close(): void {\n this._disconnected = true;\n this._keepAlive.stop();\n this._policy.cancel();\n this._reconnectPending = false;\n const socket = this._socket;\n this._socket = undefined;\n this._supportsSubscriptionRevisions = false;\n if (socket) {\n this.detachAndCloseSocket(socket);\n }\n }\n\n private openSocket(): void {\n const currentSocket = this._socket;\n if (\n currentSocket &&\n (currentSocket.readyState === WebSocket.OPEN ||\n currentSocket.readyState === WebSocket.CONNECTING)\n ) {\n return;\n }\n\n if (currentSocket) {\n this._socket = undefined;\n this.detachAndCloseSocket(currentSocket);\n }\n\n this._supportsSubscriptionRevisions = false;\n\n let url = this._url;\n if (this._microservice?.length > 0) {\n const param = `${Globals.microserviceWSQueryArgument}=${encodeURIComponent(this._microservice)}`;\n url += (url.includes('?') ? '&' : '?') + param;\n }\n\n const socket = new WebSocket(url);\n this._socket = socket;\n\n socket.onopen = () => {\n if (this._socket !== socket || this._disconnected) return;\n console.log('Hub connection established', url);\n this.cancelPendingReconnect();\n this._policy.reset();\n this._keepAlive.start();\n this.sendAllSubscriptions();\n };\n\n socket.onclose = () => {\n if (this._socket !== socket || this._disconnected) return;\n console.log('Hub connection closed', url);\n this._socket = undefined;\n this._supportsSubscriptionRevisions = false;\n this._keepAlive.stop();\n if (this._subscriptions.size === 0) return;\n this._reconnectPending = true;\n const scheduled = this._policy.schedule(() => {\n this._reconnectPending = false;\n if (!this._disconnected && this._subscriptions.size > 0) {\n this.ensureConnected();\n }\n }, this._url);\n if (!scheduled) {\n this._reconnectPending = false;\n }\n };\n\n socket.onerror = (error) => {\n if (this._socket !== socket || this._disconnected) return;\n console.error('Hub connection error', url, error);\n this._keepAlive.stop();\n // onclose will fire after onerror, triggering reconnect\n };\n\n socket.onmessage = (ev) => {\n if (this._socket !== socket || this._disconnected) return;\n this.handleMessage(ev.data as string);\n };\n }\n\n private cancelPendingReconnect(): void {\n if (!this._reconnectPending) return;\n\n this._policy.cancel();\n this._reconnectPending = false;\n }\n\n private detachAndCloseSocket(socket: WebSocket): void {\n socket.onopen = null;\n socket.onclose = null;\n socket.onerror = null;\n socket.onmessage = null;\n socket.close();\n }\n\n private sendAllSubscriptions(): void {\n for (const [queryId, sub] of this._subscriptions) {\n this.sendSubscribeMessage(queryId, sub);\n }\n }\n\n private sendSubscribeMessage(\n queryId: string,\n subscription: ActiveSubscription,\n ): void {\n const message: HubMessage = {\n type: HubMessageType.Subscribe,\n queryId,\n payload: subscription.request,\n };\n if (this._supportsSubscriptionRevisions) {\n message.revision = subscription.revision;\n }\n this.sendMessage(message);\n }\n\n private sendMessage(message: HubMessage): void {\n if (this._socket?.readyState === WebSocket.OPEN) {\n this._socket.send(JSON.stringify(message));\n }\n }\n\n private handleMessage(rawData: string): void {\n try {\n const message = JSON.parse(rawData) as HubMessage;\n\n // Every received message is activity — skip keep-alive ping if data is flowing.\n this._keepAlive.recordActivity();\n\n switch (message.type) {\n case HubMessageType.Connected:\n this.handleConnected(message);\n break;\n case HubMessageType.QueryResult:\n this.handleQueryResult(message);\n break;\n case HubMessageType.Pong:\n this.handlePong(message);\n break;\n case HubMessageType.Unauthorized:\n console.warn('Hub: query unauthorized', message.queryId);\n this.handleUnauthorized(message);\n break;\n case HubMessageType.Error:\n if (this.isMessageForCurrentSubscription(message)) {\n console.error(\n 'Hub: query error',\n message.queryId,\n message.payload,\n );\n }\n break;\n }\n } catch (error) {\n console.error('Hub: error parsing message', error);\n }\n }\n\n private handleConnected(message: HubMessage): void {\n if (\n message.supportsSubscriptionRevisions !== true ||\n this._supportsSubscriptionRevisions\n ) {\n return;\n }\n\n this._supportsSubscriptionRevisions = true;\n\n // The legacy subscriptions sent on socket open keep this client compatible with older servers.\n // Ordered WebSocket delivery means these revision-aware replacements are processed afterwards.\n this.sendAllSubscriptions();\n }\n\n private handleQueryResult(message: HubMessage): void {\n if (!message.queryId) return;\n\n const sub = this._subscriptions.get(message.queryId);\n if (!sub || !this.isMessageForCurrentSubscription(message)) return;\n\n const result = message.payload as QueryResult<any>;\n sub.callback(result);\n }\n\n private handleUnauthorized(message: HubMessage): void {\n if (!message.queryId) return;\n\n const sub = this._subscriptions.get(message.queryId);\n if (!sub || !this.isMessageForCurrentSubscription(message)) return;\n\n this._subscriptions.delete(message.queryId);\n sub.callback(QueryResult.unauthorized());\n }\n\n private isMessageForCurrentSubscription(message: HubMessage): boolean {\n if (!message.queryId) return false;\n\n if (!this._supportsSubscriptionRevisions) {\n return (\n message.revision === undefined &&\n this._subscriptions.has(message.queryId)\n );\n }\n\n return (\n this.isValidRevision(message.revision) &&\n this._subscriptions.get(message.queryId)?.revision === message.revision\n );\n }\n\n private isValidRevision(revision: number | undefined): revision is number {\n return revision !== undefined && Number.isSafeInteger(revision) && revision > 0;\n }\n\n private getNextRevision(): number {\n if (this._nextRevision >= Number.MAX_SAFE_INTEGER) {\n throw new RangeError(\n 'WebSocket hub subscription revision exhausted the safe integer range',\n );\n }\n\n return ++this._nextRevision;\n }\n\n private handlePong(message: HubMessage): void {\n if (message.timestamp && this._lastPingSentTime) {\n const latency = Date.now() - message.timestamp;\n this._lastPongLatency = latency;\n this._latencySamples.push(latency);\n\n if (this._latencySamples.length > 100) {\n this._latencySamples.shift();\n }\n }\n }\n}\n"],"names":["HubMessageType","WebSocketHubConnection","_socket","_disconnected","_subscriptions","Map","_keepAlive","_lastPingSentTime","_lastPongLatency","_latencySamples","_nextRevision","_supportsSubscriptionRevisions","_reconnectPending","_url","pingIntervalMs","_policy","ReconnectPolicy","_microservice","HubConnectionKeepAlive","readyState","WebSocket","OPEN","Date","now","sendMessage","type","timestamp","queryCount","size","isConnected","lastPingLatency","averageLatency","length","reduce","a","b","subscribe","queryId","request","callback","subscription","revision","getNextRevision","set","ensureConnected","sendSubscribeMessage","unsubscribe","get","delete","message","close","dispose","clear","CONNECTING","cancelPendingReconnect","openSocket","stop","cancel","socket","undefined","detachAndCloseSocket","currentSocket","url","param","Globals","microserviceWSQueryArgument","encodeURIComponent","includes","onopen","console","log","reset","start","sendAllSubscriptions","onclose","scheduled","schedule","onerror","error","onmessage","ev","handleMessage","data","sub","payload","send","JSON","stringify","rawData","parse","recordActivity","handleConnected","handleQueryResult","handlePong","warn","handleUnauthorized","isMessageForCurrentSubscription","supportsSubscriptionRevisions","result","QueryResult","unauthorized","has","isValidRevision","Number","isSafeInteger","MAX_SAFE_INTEGER","RangeError","latency","push","shift"],"mappings":";;;;;AAAA;AACA;AASA;;;IAMO,IAAKA,cAAAA,iBAAAA,SAAAA,cAAAA,EAAAA;;;;;;;;;AAAAA,IAAAA,OAAAA,cAAAA;AASX,CAAA,CAAA,EAAA;AA8CD;;;;;;;;;AASC,IACM,MAAMC,sBAAAA,CAAAA;;;;IACDC,OAAAA;AACAC,IAAAA,aAAAA,GAAgB,KAAA;AAChBC,IAAAA,cAAAA,GAAkD,IAAIC,GAAAA,EAAAA;IAC7CC,UAAAA;IACTC,iBAAAA;AACAC,IAAAA,gBAAAA,GAA2B,CAAA;AAC3BC,IAAAA,eAAAA,GAA4B,EAAE;AAC9BC,IAAAA,aAAAA,GAAgB,CAAA;AAChBC,IAAAA,8BAAAA,GAAiC,KAAA;AACjCC,IAAAA,iBAAAA,GAAoB,KAAA;AAE5B;;;;;;AAMC,QACD,WAAA,CACqBC,IAAY,EAC7B,aAA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AC/D,aAAaA,QAAAA,GAAW,CAAA;AAClF,IAAA;IAEQC,eAAAA,GAA0B;AAC9B,QAAA,IAAI,IAAI,CAAChC,aAAa,IAAI6F,MAAAA,CAAOE,gBAAgB,EAAE;AAC/C,YAAA,MAAM,IAAIC,UAAAA,CACN,sEAAA,CAAA;AAER,QAAA;QAEA,OAAO,EAAE,IAAI,CAAChG,aAAa;AAC/B,IAAA;AAEQmF,IAAAA,UAAAA,CAAW5C,OAAmB,EAAQ;AAC1C,QAAA,IAAIA,QAAQvB,SAAS,IAAI,IAAI,CAACnB,iBAAiB,EAAE;AAC7C,YAAA,MAAMoG,OAAAA,GAAUrF,IAAAA,CAAKC,GAAG,EAAA,GAAK0B,QAAQvB,SAAS;YAC9C,IAAI,CAAClB,gBAAgB,GAAGmG,OAAAA;AACxB,YAAA,IAAI,CAAClG,eAAe,CAACmG,IAAI,CAACD,OAAAA,CAAAA;AAE1B,YAAA,IAAI,IAAI,CAAClG,eAAe,CAACuB,MAAM,GAAG,GAAA,EAAK;gBACnC,IAAI,CAACvB,eAAe,CAACoG,KAAK,EAAA;AAC9B,YAAA;AACJ,QAAA;AACJ,IAAA;AACJ;;;;"}
|
package/dist/esm/queries/for_ObservableQueryFor/when_performing/with_a_not_ready_result.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"with_a_not_ready_result.d.ts","sourceRoot":"","sources":["../../../../../queries/for_ObservableQueryFor/when_performing/with_a_not_ready_result.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import * as sinon from 'sinon';
|
|
2
|
+
import { given } from '../../../given';
|
|
3
|
+
import { createFetchHelper } from '../../../helpers/fetchHelper';
|
|
4
|
+
import { an_observable_query_for } from '../given/an_observable_query_for';
|
|
5
|
+
describe('when performing with a not ready result', given(an_observable_query_for, (context) => {
|
|
6
|
+
let result;
|
|
7
|
+
let fetchHelper;
|
|
8
|
+
beforeEach(async () => {
|
|
9
|
+
fetchHelper = createFetchHelper();
|
|
10
|
+
const fetchStub = fetchHelper.stubFetch();
|
|
11
|
+
fetchStub.resolves({
|
|
12
|
+
json: sinon.stub().resolves({
|
|
13
|
+
data: null,
|
|
14
|
+
isSuccess: false,
|
|
15
|
+
isReady: false,
|
|
16
|
+
isAuthorized: true,
|
|
17
|
+
isValid: true,
|
|
18
|
+
hasExceptions: false,
|
|
19
|
+
validationResults: [],
|
|
20
|
+
exceptionMessages: [],
|
|
21
|
+
exceptionStackTrace: '',
|
|
22
|
+
paging: { page: 0, size: 0, totalItems: 0, totalPages: 0 },
|
|
23
|
+
}),
|
|
24
|
+
ok: true,
|
|
25
|
+
status: 202,
|
|
26
|
+
});
|
|
27
|
+
context.query.setOrigin('https://api.example.com');
|
|
28
|
+
result = await context.query.perform({ id: 'test-id' });
|
|
29
|
+
});
|
|
30
|
+
afterEach(() => fetchHelper.restore());
|
|
31
|
+
it('should return the not ready result', () => result.isReady.should.be.false);
|
|
32
|
+
it('should not report an exception', () => result.hasExceptions.should.be.false);
|
|
33
|
+
}));
|
|
34
|
+
//# sourceMappingURL=with_a_not_ready_result.js.map
|
package/dist/esm/queries/for_ObservableQueryFor/when_performing/with_a_not_ready_result.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"with_a_not_ready_result.js","sourceRoot":"","sources":["../../../../../queries/for_ObservableQueryFor/when_performing/with_a_not_ready_result.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AAEjE,OAAO,EAAE,uBAAuB,EAAE,MAAM,kCAAkC,CAAC;AAE3E,QAAQ,CACJ,yCAAyC,EACzC,KAAK,CAAC,uBAAuB,EAAE,CAAC,OAAO,EAAE,EAAE;IACvC,IAAI,MAA2B,CAAC;IAChC,IAAI,WAAsE,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
3
3
|
import { Globals } from '../../../Globals';
|
|
4
4
|
import { QueryTransportMethod } from '../../QueryTransportMethod';
|
|
5
5
|
import * as sinon from 'sinon';
|
|
6
|
-
describe('when subscribing with direct mode and SSE transport', given(an_observable_query_for, context => {
|
|
6
|
+
describe('when subscribing with direct mode and SSE transport', given(an_observable_query_for, (context) => {
|
|
7
7
|
let callback;
|
|
8
8
|
let subscription;
|
|
9
9
|
let capturedUrl;
|
|
10
|
+
let eventSourceInstance;
|
|
10
11
|
let originalQueryDirectMode;
|
|
11
12
|
let originalTransportMethod;
|
|
12
13
|
beforeEach(() => {
|
|
@@ -18,6 +19,7 @@ describe('when subscribing with direct mode and SSE transport', given(an_observa
|
|
|
18
19
|
callback = sinon.stub();
|
|
19
20
|
const FakeEventSourceConstructor = function (url) {
|
|
20
21
|
capturedUrl = url;
|
|
22
|
+
eventSourceInstance = this;
|
|
21
23
|
Object.assign(this, {
|
|
22
24
|
onopen: null,
|
|
23
25
|
onerror: null,
|
|
@@ -27,8 +29,24 @@ describe('when subscribing with direct mode and SSE transport', given(an_observa
|
|
|
27
29
|
removeEventListener: sinon.stub(),
|
|
28
30
|
});
|
|
29
31
|
};
|
|
30
|
-
globalThis['EventSource'] =
|
|
32
|
+
globalThis['EventSource'] =
|
|
33
|
+
FakeEventSourceConstructor;
|
|
31
34
|
subscription = context.query.subscribe(callback, { id: 'test-id' });
|
|
35
|
+
const result = {
|
|
36
|
+
data: 'ready result',
|
|
37
|
+
isSuccess: true,
|
|
38
|
+
isAuthorized: true,
|
|
39
|
+
isValid: true,
|
|
40
|
+
hasExceptions: false,
|
|
41
|
+
validationResults: [],
|
|
42
|
+
exceptionMessages: [],
|
|
43
|
+
exceptionStackTrace: '',
|
|
44
|
+
paging: { page: 0, size: 0, totalItems: 0, totalPages: 0 },
|
|
45
|
+
};
|
|
46
|
+
eventSourceInstance.onmessage?.({ data: JSON.stringify(result) });
|
|
47
|
+
eventSourceInstance.onmessage?.({
|
|
48
|
+
data: JSON.stringify({ ...result, isSuccess: false, isReady: false }),
|
|
49
|
+
});
|
|
32
50
|
});
|
|
33
51
|
afterEach(() => {
|
|
34
52
|
Globals.queryDirectMode = originalQueryDirectMode;
|
|
@@ -50,5 +68,11 @@ describe('when subscribing with direct mode and SSE transport', given(an_observa
|
|
|
50
68
|
it('should return a subscription', () => {
|
|
51
69
|
subscription.should.not.be.undefined;
|
|
52
70
|
});
|
|
71
|
+
it('should normalize omitted readiness to true', () => {
|
|
72
|
+
callback.firstCall.args[0].isReady.should.be.true;
|
|
73
|
+
});
|
|
74
|
+
it('should preserve false readiness', () => {
|
|
75
|
+
callback.secondCall.args[0].isReady.should.be.false;
|
|
76
|
+
});
|
|
53
77
|
}));
|
|
54
78
|
//# sourceMappingURL=with_direct_mode_and_sse_transport.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"with_direct_mode_and_sse_transport.js","sourceRoot":"","sources":["../../../../../queries/for_ObservableQueryFor/when_subscribing/with_direct_mode_and_sse_transport.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,uBAAuB,EAAE,MAAM,kCAAkC,CAAC;AAC3E,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAGlE,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,QAAQ,
|
|
1
|
+
{"version":3,"file":"with_direct_mode_and_sse_transport.js","sourceRoot":"","sources":["../../../../../queries/for_ObservableQueryFor/when_subscribing/with_direct_mode_and_sse_transport.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,uBAAuB,EAAE,MAAM,kCAAkC,CAAC;AAC3E,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,oBAAoB,EAAE,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AAM,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC7C,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,8BAA8B,EAAE,GAAG,EAAE;QACpC,YAAY,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,SAAS,CAAC;IACzC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,4CAA4C,EAAE,GAAG,EAAE;QAClD,QAAQ,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,CAAC;IACtD,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,iCAAiC,EAAE,GAAG,EAAE;QACvC,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,KAAK,CAAC;IACxD,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CACL,CAAC"}
|
|
@@ -4,11 +4,14 @@ import { Globals } from '../../../Globals';
|
|
|
4
4
|
import { QueryTransportMethod } from '../../QueryTransportMethod';
|
|
5
5
|
import { SSE_HUB_ROUTE } from '../../ServerSentEventQueryConnection';
|
|
6
6
|
import { resetSharedMultiplexer } from '../../ObservableQueryMultiplexer';
|
|
7
|
+
import { HubMessageType } from '../../WebSocketHubConnection';
|
|
7
8
|
import * as sinon from 'sinon';
|
|
8
|
-
describe('when subscribing with direct mode disabled and SSE transport', given(an_observable_query_for, context => {
|
|
9
|
+
describe('when subscribing with direct mode disabled and SSE transport', given(an_observable_query_for, (context) => {
|
|
9
10
|
let callback;
|
|
10
11
|
let subscription;
|
|
11
12
|
let capturedUrl;
|
|
13
|
+
let fetchStub;
|
|
14
|
+
let eventSourceInstance;
|
|
12
15
|
let originalQueryDirectMode;
|
|
13
16
|
let originalTransportMethod;
|
|
14
17
|
beforeEach(() => {
|
|
@@ -21,6 +24,7 @@ describe('when subscribing with direct mode disabled and SSE transport', given(a
|
|
|
21
24
|
callback = sinon.stub();
|
|
22
25
|
const FakeEventSourceConstructor = function (url) {
|
|
23
26
|
capturedUrl = url;
|
|
27
|
+
eventSourceInstance = this;
|
|
24
28
|
Object.assign(this, {
|
|
25
29
|
onopen: null,
|
|
26
30
|
onerror: null,
|
|
@@ -30,8 +34,44 @@ describe('when subscribing with direct mode disabled and SSE transport', given(a
|
|
|
30
34
|
removeEventListener: sinon.stub(),
|
|
31
35
|
});
|
|
32
36
|
};
|
|
33
|
-
globalThis['EventSource'] =
|
|
37
|
+
globalThis['EventSource'] =
|
|
38
|
+
FakeEventSourceConstructor;
|
|
39
|
+
fetchStub = sinon.stub().resolves({ ok: true });
|
|
40
|
+
globalThis['fetch'] = fetchStub;
|
|
34
41
|
subscription = context.query.subscribe(callback, { id: 'test-id' });
|
|
42
|
+
eventSourceInstance.onopen?.();
|
|
43
|
+
eventSourceInstance.onmessage?.({
|
|
44
|
+
data: JSON.stringify({
|
|
45
|
+
type: HubMessageType.Connected,
|
|
46
|
+
payload: 'connection-1',
|
|
47
|
+
}),
|
|
48
|
+
});
|
|
49
|
+
let subscribeRequest;
|
|
50
|
+
try {
|
|
51
|
+
subscribeRequest = JSON.parse(fetchStub.firstCall.args[1].body);
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
throw new Error('The SSE hub subscribe request was not valid JSON', {
|
|
55
|
+
cause: error,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
eventSourceInstance.onmessage?.({
|
|
59
|
+
data: JSON.stringify({
|
|
60
|
+
type: HubMessageType.QueryResult,
|
|
61
|
+
queryId: subscribeRequest.queryId,
|
|
62
|
+
payload: {
|
|
63
|
+
data: 'ready result',
|
|
64
|
+
isSuccess: true,
|
|
65
|
+
isAuthorized: true,
|
|
66
|
+
isValid: true,
|
|
67
|
+
hasExceptions: false,
|
|
68
|
+
validationResults: [],
|
|
69
|
+
exceptionMessages: [],
|
|
70
|
+
exceptionStackTrace: '',
|
|
71
|
+
paging: { page: 0, size: 0, totalItems: 0, totalPages: 0 },
|
|
72
|
+
},
|
|
73
|
+
}),
|
|
74
|
+
});
|
|
35
75
|
});
|
|
36
76
|
afterEach(() => {
|
|
37
77
|
Globals.queryDirectMode = originalQueryDirectMode;
|
|
@@ -40,6 +80,7 @@ describe('when subscribing with direct mode disabled and SSE transport', given(a
|
|
|
40
80
|
subscription.unsubscribe();
|
|
41
81
|
}
|
|
42
82
|
delete globalThis['EventSource'];
|
|
83
|
+
delete globalThis['fetch'];
|
|
43
84
|
resetSharedMultiplexer();
|
|
44
85
|
});
|
|
45
86
|
it('should connect to the centralized SSE hub endpoint', () => {
|
|
@@ -49,7 +90,23 @@ describe('when subscribing with direct mode disabled and SSE transport', given(a
|
|
|
49
90
|
capturedUrl.should.not.include('query=');
|
|
50
91
|
});
|
|
51
92
|
it('should return a subscription', () => {
|
|
52
|
-
subscription.should.
|
|
93
|
+
(subscription === undefined).should.equal(false);
|
|
94
|
+
});
|
|
95
|
+
it('should omit revisions when the server does not advertise support', () => {
|
|
96
|
+
(getSubscribeBody().revision === undefined).should.equal(true);
|
|
97
|
+
});
|
|
98
|
+
function getSubscribeBody() {
|
|
99
|
+
try {
|
|
100
|
+
return JSON.parse(fetchStub.firstCall.args[1].body);
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
throw new Error('Expected a valid subscribe request body', {
|
|
104
|
+
cause: error,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
it('should normalize omitted readiness to true', () => {
|
|
109
|
+
callback.firstCall.args[0].isReady.should.equal(true);
|
|
53
110
|
});
|
|
54
111
|
}));
|
|
55
112
|
//# sourceMappingURL=with_direct_mode_disabled_and_sse_transport.js.map
|