@graphql-tools/executor-legacy-ws 1.0.0 → 1.0.1-rc-20230612102029-16c7ce78
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/cjs/index.js +60 -35
- package/esm/index.js +58 -34
- package/package.json +1 -1
package/cjs/index.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.buildWSLegacyExecutor = exports.LEGACY_WS = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
4
5
|
const utils_1 = require("@graphql-tools/utils");
|
|
5
6
|
const graphql_1 = require("graphql");
|
|
7
|
+
const isomorphic_ws_1 = tslib_1.__importDefault(require("isomorphic-ws"));
|
|
6
8
|
var LEGACY_WS;
|
|
7
9
|
(function (LEGACY_WS) {
|
|
8
10
|
LEGACY_WS["CONNECTION_INIT"] = "connection_init";
|
|
@@ -15,35 +17,46 @@ var LEGACY_WS;
|
|
|
15
17
|
LEGACY_WS["DATA"] = "data";
|
|
16
18
|
LEGACY_WS["ERROR"] = "error";
|
|
17
19
|
LEGACY_WS["COMPLETE"] = "complete";
|
|
18
|
-
})(LEGACY_WS
|
|
20
|
+
})(LEGACY_WS || (exports.LEGACY_WS = LEGACY_WS = {}));
|
|
19
21
|
function buildWSLegacyExecutor(subscriptionsEndpoint, WebSocketImpl, options) {
|
|
20
|
-
const observerById = new Map();
|
|
21
22
|
let websocket = null;
|
|
22
|
-
const ensureWebsocket = () => {
|
|
23
|
-
websocket
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
23
|
+
const ensureWebsocket = (errorHandler = err => console.error(err)) => {
|
|
24
|
+
if (websocket == null || websocket.readyState !== isomorphic_ws_1.default.OPEN) {
|
|
25
|
+
websocket = new WebSocketImpl(subscriptionsEndpoint, 'graphql-ws', {
|
|
26
|
+
followRedirects: true,
|
|
27
|
+
headers: options?.headers,
|
|
28
|
+
rejectUnauthorized: false,
|
|
29
|
+
skipUTF8Validation: true,
|
|
30
|
+
});
|
|
31
|
+
websocket.onopen = () => {
|
|
32
|
+
let payload = {};
|
|
33
|
+
switch (typeof options?.connectionParams) {
|
|
34
|
+
case 'function':
|
|
35
|
+
payload = options?.connectionParams();
|
|
36
|
+
break;
|
|
37
|
+
case 'object':
|
|
38
|
+
payload = options?.connectionParams;
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
websocket.send(JSON.stringify({
|
|
42
|
+
type: LEGACY_WS.CONNECTION_INIT,
|
|
43
|
+
payload,
|
|
44
|
+
}), (error) => {
|
|
45
|
+
if (error) {
|
|
46
|
+
errorHandler(error);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
websocket.onerror = event => {
|
|
51
|
+
errorHandler(event.error);
|
|
52
|
+
};
|
|
53
|
+
websocket.onclose = () => {
|
|
54
|
+
websocket = null;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
44
57
|
};
|
|
45
58
|
const cleanupWebsocket = () => {
|
|
46
|
-
if (websocket != null
|
|
59
|
+
if (websocket != null) {
|
|
47
60
|
websocket.send(JSON.stringify({
|
|
48
61
|
type: LEGACY_WS.CONNECTION_TERMINATE,
|
|
49
62
|
}));
|
|
@@ -55,6 +68,9 @@ function buildWSLegacyExecutor(subscriptionsEndpoint, WebSocketImpl, options) {
|
|
|
55
68
|
const id = Date.now().toString();
|
|
56
69
|
return (0, utils_1.observableToAsyncIterable)({
|
|
57
70
|
subscribe(observer) {
|
|
71
|
+
function errorHandler(err) {
|
|
72
|
+
observer.error(err);
|
|
73
|
+
}
|
|
58
74
|
ensureWebsocket();
|
|
59
75
|
if (websocket == null) {
|
|
60
76
|
throw new Error(`WebSocket connection is not found!`);
|
|
@@ -74,7 +90,11 @@ function buildWSLegacyExecutor(subscriptionsEndpoint, WebSocketImpl, options) {
|
|
|
74
90
|
variables: request.variables,
|
|
75
91
|
operationName: request.operationName,
|
|
76
92
|
},
|
|
77
|
-
}))
|
|
93
|
+
}), (error) => {
|
|
94
|
+
if (error) {
|
|
95
|
+
errorHandler(error);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
78
98
|
break;
|
|
79
99
|
}
|
|
80
100
|
case LEGACY_WS.CONNECTION_ERROR: {
|
|
@@ -89,12 +109,15 @@ function buildWSLegacyExecutor(subscriptionsEndpoint, WebSocketImpl, options) {
|
|
|
89
109
|
break;
|
|
90
110
|
}
|
|
91
111
|
case LEGACY_WS.COMPLETE: {
|
|
92
|
-
if (websocket
|
|
93
|
-
|
|
112
|
+
if (websocket != null) {
|
|
113
|
+
websocket.send(JSON.stringify({
|
|
114
|
+
type: LEGACY_WS.CONNECTION_TERMINATE,
|
|
115
|
+
}), (error) => {
|
|
116
|
+
if (error) {
|
|
117
|
+
errorHandler(error);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
94
120
|
}
|
|
95
|
-
websocket.send(JSON.stringify({
|
|
96
|
-
type: LEGACY_WS.CONNECTION_TERMINATE,
|
|
97
|
-
}));
|
|
98
121
|
observer.complete();
|
|
99
122
|
cleanupWebsocket();
|
|
100
123
|
break;
|
|
@@ -103,10 +126,12 @@ function buildWSLegacyExecutor(subscriptionsEndpoint, WebSocketImpl, options) {
|
|
|
103
126
|
};
|
|
104
127
|
return {
|
|
105
128
|
unsubscribe: () => {
|
|
106
|
-
websocket?.
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
129
|
+
if (websocket?.readyState === isomorphic_ws_1.default.OPEN) {
|
|
130
|
+
websocket?.send(JSON.stringify({
|
|
131
|
+
type: LEGACY_WS.STOP,
|
|
132
|
+
id,
|
|
133
|
+
}));
|
|
134
|
+
}
|
|
110
135
|
cleanupWebsocket();
|
|
111
136
|
},
|
|
112
137
|
};
|
package/esm/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { observableToAsyncIterable } from '@graphql-tools/utils';
|
|
2
2
|
import { print } from 'graphql';
|
|
3
|
+
import WebSocket from 'isomorphic-ws';
|
|
3
4
|
export var LEGACY_WS;
|
|
4
5
|
(function (LEGACY_WS) {
|
|
5
6
|
LEGACY_WS["CONNECTION_INIT"] = "connection_init";
|
|
@@ -14,33 +15,44 @@ export var LEGACY_WS;
|
|
|
14
15
|
LEGACY_WS["COMPLETE"] = "complete";
|
|
15
16
|
})(LEGACY_WS || (LEGACY_WS = {}));
|
|
16
17
|
export function buildWSLegacyExecutor(subscriptionsEndpoint, WebSocketImpl, options) {
|
|
17
|
-
const observerById = new Map();
|
|
18
18
|
let websocket = null;
|
|
19
|
-
const ensureWebsocket = () => {
|
|
20
|
-
websocket
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
19
|
+
const ensureWebsocket = (errorHandler = err => console.error(err)) => {
|
|
20
|
+
if (websocket == null || websocket.readyState !== WebSocket.OPEN) {
|
|
21
|
+
websocket = new WebSocketImpl(subscriptionsEndpoint, 'graphql-ws', {
|
|
22
|
+
followRedirects: true,
|
|
23
|
+
headers: options?.headers,
|
|
24
|
+
rejectUnauthorized: false,
|
|
25
|
+
skipUTF8Validation: true,
|
|
26
|
+
});
|
|
27
|
+
websocket.onopen = () => {
|
|
28
|
+
let payload = {};
|
|
29
|
+
switch (typeof options?.connectionParams) {
|
|
30
|
+
case 'function':
|
|
31
|
+
payload = options?.connectionParams();
|
|
32
|
+
break;
|
|
33
|
+
case 'object':
|
|
34
|
+
payload = options?.connectionParams;
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
websocket.send(JSON.stringify({
|
|
38
|
+
type: LEGACY_WS.CONNECTION_INIT,
|
|
39
|
+
payload,
|
|
40
|
+
}), (error) => {
|
|
41
|
+
if (error) {
|
|
42
|
+
errorHandler(error);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
websocket.onerror = event => {
|
|
47
|
+
errorHandler(event.error);
|
|
48
|
+
};
|
|
49
|
+
websocket.onclose = () => {
|
|
50
|
+
websocket = null;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
41
53
|
};
|
|
42
54
|
const cleanupWebsocket = () => {
|
|
43
|
-
if (websocket != null
|
|
55
|
+
if (websocket != null) {
|
|
44
56
|
websocket.send(JSON.stringify({
|
|
45
57
|
type: LEGACY_WS.CONNECTION_TERMINATE,
|
|
46
58
|
}));
|
|
@@ -52,6 +64,9 @@ export function buildWSLegacyExecutor(subscriptionsEndpoint, WebSocketImpl, opti
|
|
|
52
64
|
const id = Date.now().toString();
|
|
53
65
|
return observableToAsyncIterable({
|
|
54
66
|
subscribe(observer) {
|
|
67
|
+
function errorHandler(err) {
|
|
68
|
+
observer.error(err);
|
|
69
|
+
}
|
|
55
70
|
ensureWebsocket();
|
|
56
71
|
if (websocket == null) {
|
|
57
72
|
throw new Error(`WebSocket connection is not found!`);
|
|
@@ -71,7 +86,11 @@ export function buildWSLegacyExecutor(subscriptionsEndpoint, WebSocketImpl, opti
|
|
|
71
86
|
variables: request.variables,
|
|
72
87
|
operationName: request.operationName,
|
|
73
88
|
},
|
|
74
|
-
}))
|
|
89
|
+
}), (error) => {
|
|
90
|
+
if (error) {
|
|
91
|
+
errorHandler(error);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
75
94
|
break;
|
|
76
95
|
}
|
|
77
96
|
case LEGACY_WS.CONNECTION_ERROR: {
|
|
@@ -86,12 +105,15 @@ export function buildWSLegacyExecutor(subscriptionsEndpoint, WebSocketImpl, opti
|
|
|
86
105
|
break;
|
|
87
106
|
}
|
|
88
107
|
case LEGACY_WS.COMPLETE: {
|
|
89
|
-
if (websocket
|
|
90
|
-
|
|
108
|
+
if (websocket != null) {
|
|
109
|
+
websocket.send(JSON.stringify({
|
|
110
|
+
type: LEGACY_WS.CONNECTION_TERMINATE,
|
|
111
|
+
}), (error) => {
|
|
112
|
+
if (error) {
|
|
113
|
+
errorHandler(error);
|
|
114
|
+
}
|
|
115
|
+
});
|
|
91
116
|
}
|
|
92
|
-
websocket.send(JSON.stringify({
|
|
93
|
-
type: LEGACY_WS.CONNECTION_TERMINATE,
|
|
94
|
-
}));
|
|
95
117
|
observer.complete();
|
|
96
118
|
cleanupWebsocket();
|
|
97
119
|
break;
|
|
@@ -100,10 +122,12 @@ export function buildWSLegacyExecutor(subscriptionsEndpoint, WebSocketImpl, opti
|
|
|
100
122
|
};
|
|
101
123
|
return {
|
|
102
124
|
unsubscribe: () => {
|
|
103
|
-
websocket?.
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
125
|
+
if (websocket?.readyState === WebSocket.OPEN) {
|
|
126
|
+
websocket?.send(JSON.stringify({
|
|
127
|
+
type: LEGACY_WS.STOP,
|
|
128
|
+
id,
|
|
129
|
+
}));
|
|
130
|
+
}
|
|
107
131
|
cleanupWebsocket();
|
|
108
132
|
},
|
|
109
133
|
};
|
package/package.json
CHANGED