@graphql-tools/executor-legacy-ws 1.1.35 → 1.1.36
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 +120 -79
- package/esm/index.js +120 -79
- package/package.json +2 -2
package/cjs/index.js
CHANGED
|
@@ -19,78 +19,117 @@ var LEGACY_WS;
|
|
|
19
19
|
LEGACY_WS["ERROR"] = "error";
|
|
20
20
|
LEGACY_WS["COMPLETE"] = "complete";
|
|
21
21
|
})(LEGACY_WS || (exports.LEGACY_WS = LEGACY_WS = {}));
|
|
22
|
+
function isConnectionParamsRecord(value) {
|
|
23
|
+
return value != null && typeof value === 'object' && !Array.isArray(value);
|
|
24
|
+
}
|
|
22
25
|
function buildWSLegacyExecutor(subscriptionsEndpoint, WebSocketImpl, options) {
|
|
23
|
-
|
|
24
|
-
let
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
rejectUnauthorized: options?.rejectUnauthorized ?? true,
|
|
31
|
-
skipUTF8Validation: true,
|
|
32
|
-
});
|
|
33
|
-
websocket.onopen = () => {
|
|
34
|
-
let payload = {};
|
|
35
|
-
switch (typeof options?.connectionParams) {
|
|
36
|
-
case 'function':
|
|
37
|
-
payload = options?.connectionParams();
|
|
38
|
-
break;
|
|
39
|
-
case 'object':
|
|
40
|
-
payload = options?.connectionParams;
|
|
41
|
-
break;
|
|
42
|
-
}
|
|
43
|
-
payload = Object.assign(payload, executorConnectionParams);
|
|
44
|
-
websocket.send(JSON.stringify({
|
|
45
|
-
type: LEGACY_WS.CONNECTION_INIT,
|
|
46
|
-
payload,
|
|
47
|
-
}), (error) => {
|
|
48
|
-
if (error) {
|
|
49
|
-
errorHandler(error);
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
};
|
|
53
|
-
websocket.onerror = event => {
|
|
54
|
-
errorHandler(event.error);
|
|
55
|
-
};
|
|
56
|
-
websocket.onclose = () => {
|
|
57
|
-
websocket = null;
|
|
58
|
-
};
|
|
26
|
+
const disposers = new Set();
|
|
27
|
+
let operationSeq = 0;
|
|
28
|
+
function baseConnectionParams() {
|
|
29
|
+
const connectionParams = options?.connectionParams;
|
|
30
|
+
if (typeof connectionParams === 'function') {
|
|
31
|
+
const resolved = connectionParams();
|
|
32
|
+
return isConnectionParamsRecord(resolved) ? { ...resolved } : {};
|
|
59
33
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (websocket != null) {
|
|
63
|
-
websocket.send(JSON.stringify({
|
|
64
|
-
type: LEGACY_WS.CONNECTION_TERMINATE,
|
|
65
|
-
}));
|
|
66
|
-
websocket.terminate();
|
|
67
|
-
websocket = null;
|
|
34
|
+
if (isConnectionParamsRecord(connectionParams)) {
|
|
35
|
+
return { ...connectionParams };
|
|
68
36
|
}
|
|
69
|
-
|
|
37
|
+
return {};
|
|
38
|
+
}
|
|
39
|
+
function requestConnectionParams(request) {
|
|
40
|
+
const fromExtensions = request.extensions?.['connectionParams'];
|
|
41
|
+
return isConnectionParamsRecord(fromExtensions) ? { ...fromExtensions } : {};
|
|
42
|
+
}
|
|
70
43
|
const executor = function legacyExecutor(request) {
|
|
71
|
-
//
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
const id =
|
|
44
|
+
// Bound to this execution only. A later request must not inherit keys it omits.
|
|
45
|
+
const connectionParams = {
|
|
46
|
+
...baseConnectionParams(),
|
|
47
|
+
...requestConnectionParams(request),
|
|
48
|
+
};
|
|
49
|
+
operationSeq += 1;
|
|
50
|
+
const id = `${operationSeq.toString(36)}-${Math.random().toString(36).slice(2)}`;
|
|
78
51
|
return (0, utils_1.observableToAsyncIterable)({
|
|
79
52
|
subscribe(observer) {
|
|
80
|
-
|
|
53
|
+
let closed = false;
|
|
54
|
+
const websocket = new WebSocketImpl(subscriptionsEndpoint, 'graphql-ws', {
|
|
55
|
+
followRedirects: true,
|
|
56
|
+
headers: options?.headers,
|
|
57
|
+
rejectUnauthorized: options?.rejectUnauthorized ?? true,
|
|
58
|
+
skipUTF8Validation: true,
|
|
59
|
+
});
|
|
60
|
+
function closeSocket(sendStop) {
|
|
61
|
+
if (closed) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
closed = true;
|
|
65
|
+
disposers.delete(disposeSocket);
|
|
66
|
+
if (websocket.readyState === isomorphic_ws_1.default.OPEN) {
|
|
67
|
+
if (sendStop) {
|
|
68
|
+
websocket.send(JSON.stringify({
|
|
69
|
+
type: LEGACY_WS.STOP,
|
|
70
|
+
id,
|
|
71
|
+
}));
|
|
72
|
+
}
|
|
73
|
+
websocket.send(JSON.stringify({
|
|
74
|
+
type: LEGACY_WS.CONNECTION_TERMINATE,
|
|
75
|
+
}));
|
|
76
|
+
}
|
|
77
|
+
websocket.terminate();
|
|
78
|
+
}
|
|
79
|
+
function endWithError(err) {
|
|
80
|
+
if (closed) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
closeSocket(false);
|
|
81
84
|
observer.error(err);
|
|
85
|
+
observer.complete();
|
|
82
86
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
87
|
+
function disposeSocket() {
|
|
88
|
+
if (closed) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
closeSocket(false);
|
|
92
|
+
observer.complete();
|
|
86
93
|
}
|
|
94
|
+
disposers.add(disposeSocket);
|
|
95
|
+
websocket.onopen = () => {
|
|
96
|
+
if (closed) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
websocket.send(JSON.stringify({
|
|
100
|
+
type: LEGACY_WS.CONNECTION_INIT,
|
|
101
|
+
payload: connectionParams,
|
|
102
|
+
}), (error) => {
|
|
103
|
+
if (error) {
|
|
104
|
+
endWithError(error);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
websocket.onerror = event => {
|
|
109
|
+
endWithError(event.error);
|
|
110
|
+
};
|
|
111
|
+
websocket.onclose = () => {
|
|
112
|
+
if (closed) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
closed = true;
|
|
116
|
+
disposers.delete(disposeSocket);
|
|
117
|
+
observer.complete();
|
|
118
|
+
};
|
|
87
119
|
websocket.onmessage = event => {
|
|
88
|
-
|
|
120
|
+
if (closed) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
let data;
|
|
124
|
+
try {
|
|
125
|
+
data = JSON.parse(event.data.toString('utf-8'));
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
endWithError(error);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
89
131
|
switch (data.type) {
|
|
90
132
|
case LEGACY_WS.CONNECTION_ACK: {
|
|
91
|
-
if (websocket == null) {
|
|
92
|
-
throw new Error(`WebSocket connection is not found!`);
|
|
93
|
-
}
|
|
94
133
|
websocket.send(JSON.stringify({
|
|
95
134
|
type: LEGACY_WS.START,
|
|
96
135
|
id,
|
|
@@ -101,52 +140,54 @@ function buildWSLegacyExecutor(subscriptionsEndpoint, WebSocketImpl, options) {
|
|
|
101
140
|
},
|
|
102
141
|
}), (error) => {
|
|
103
142
|
if (error) {
|
|
104
|
-
|
|
143
|
+
endWithError(error);
|
|
105
144
|
}
|
|
106
145
|
});
|
|
107
146
|
break;
|
|
108
147
|
}
|
|
109
148
|
case LEGACY_WS.CONNECTION_ERROR: {
|
|
110
|
-
|
|
149
|
+
endWithError(data.payload);
|
|
111
150
|
break;
|
|
112
151
|
}
|
|
113
152
|
case LEGACY_WS.CONNECTION_KEEP_ALIVE: {
|
|
114
153
|
break;
|
|
115
154
|
}
|
|
116
155
|
case LEGACY_WS.DATA: {
|
|
156
|
+
if (data.id !== id) {
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
117
159
|
observer.next(data.payload);
|
|
118
160
|
break;
|
|
119
161
|
}
|
|
162
|
+
case LEGACY_WS.ERROR: {
|
|
163
|
+
if (data.id !== id) {
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
endWithError(data.payload);
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
120
169
|
case LEGACY_WS.COMPLETE: {
|
|
121
|
-
if (
|
|
122
|
-
|
|
123
|
-
type: LEGACY_WS.CONNECTION_TERMINATE,
|
|
124
|
-
}), (error) => {
|
|
125
|
-
if (error) {
|
|
126
|
-
errorHandler(error);
|
|
127
|
-
}
|
|
128
|
-
});
|
|
170
|
+
if (data.id !== id) {
|
|
171
|
+
break;
|
|
129
172
|
}
|
|
173
|
+
closeSocket(false);
|
|
130
174
|
observer.complete();
|
|
131
|
-
cleanupWebsocket();
|
|
132
175
|
break;
|
|
133
176
|
}
|
|
134
177
|
}
|
|
135
178
|
};
|
|
136
179
|
return {
|
|
137
180
|
unsubscribe: () => {
|
|
138
|
-
|
|
139
|
-
websocket?.send(JSON.stringify({
|
|
140
|
-
type: LEGACY_WS.STOP,
|
|
141
|
-
id,
|
|
142
|
-
}));
|
|
143
|
-
}
|
|
144
|
-
cleanupWebsocket();
|
|
181
|
+
closeSocket(true);
|
|
145
182
|
},
|
|
146
183
|
};
|
|
147
184
|
},
|
|
148
185
|
});
|
|
149
186
|
};
|
|
150
|
-
executor[Symbol.dispose] =
|
|
187
|
+
executor[Symbol.dispose] = () => {
|
|
188
|
+
for (const disposeSocket of [...disposers]) {
|
|
189
|
+
disposeSocket();
|
|
190
|
+
}
|
|
191
|
+
};
|
|
151
192
|
return executor;
|
|
152
193
|
}
|
package/esm/index.js
CHANGED
|
@@ -14,78 +14,117 @@ export var LEGACY_WS;
|
|
|
14
14
|
LEGACY_WS["ERROR"] = "error";
|
|
15
15
|
LEGACY_WS["COMPLETE"] = "complete";
|
|
16
16
|
})(LEGACY_WS || (LEGACY_WS = {}));
|
|
17
|
+
function isConnectionParamsRecord(value) {
|
|
18
|
+
return value != null && typeof value === 'object' && !Array.isArray(value);
|
|
19
|
+
}
|
|
17
20
|
export function buildWSLegacyExecutor(subscriptionsEndpoint, WebSocketImpl, options) {
|
|
18
|
-
|
|
19
|
-
let
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
rejectUnauthorized: options?.rejectUnauthorized ?? true,
|
|
26
|
-
skipUTF8Validation: true,
|
|
27
|
-
});
|
|
28
|
-
websocket.onopen = () => {
|
|
29
|
-
let payload = {};
|
|
30
|
-
switch (typeof options?.connectionParams) {
|
|
31
|
-
case 'function':
|
|
32
|
-
payload = options?.connectionParams();
|
|
33
|
-
break;
|
|
34
|
-
case 'object':
|
|
35
|
-
payload = options?.connectionParams;
|
|
36
|
-
break;
|
|
37
|
-
}
|
|
38
|
-
payload = Object.assign(payload, executorConnectionParams);
|
|
39
|
-
websocket.send(JSON.stringify({
|
|
40
|
-
type: LEGACY_WS.CONNECTION_INIT,
|
|
41
|
-
payload,
|
|
42
|
-
}), (error) => {
|
|
43
|
-
if (error) {
|
|
44
|
-
errorHandler(error);
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
};
|
|
48
|
-
websocket.onerror = event => {
|
|
49
|
-
errorHandler(event.error);
|
|
50
|
-
};
|
|
51
|
-
websocket.onclose = () => {
|
|
52
|
-
websocket = null;
|
|
53
|
-
};
|
|
21
|
+
const disposers = new Set();
|
|
22
|
+
let operationSeq = 0;
|
|
23
|
+
function baseConnectionParams() {
|
|
24
|
+
const connectionParams = options?.connectionParams;
|
|
25
|
+
if (typeof connectionParams === 'function') {
|
|
26
|
+
const resolved = connectionParams();
|
|
27
|
+
return isConnectionParamsRecord(resolved) ? { ...resolved } : {};
|
|
54
28
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (websocket != null) {
|
|
58
|
-
websocket.send(JSON.stringify({
|
|
59
|
-
type: LEGACY_WS.CONNECTION_TERMINATE,
|
|
60
|
-
}));
|
|
61
|
-
websocket.terminate();
|
|
62
|
-
websocket = null;
|
|
29
|
+
if (isConnectionParamsRecord(connectionParams)) {
|
|
30
|
+
return { ...connectionParams };
|
|
63
31
|
}
|
|
64
|
-
|
|
32
|
+
return {};
|
|
33
|
+
}
|
|
34
|
+
function requestConnectionParams(request) {
|
|
35
|
+
const fromExtensions = request.extensions?.['connectionParams'];
|
|
36
|
+
return isConnectionParamsRecord(fromExtensions) ? { ...fromExtensions } : {};
|
|
37
|
+
}
|
|
65
38
|
const executor = function legacyExecutor(request) {
|
|
66
|
-
//
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const id =
|
|
39
|
+
// Bound to this execution only. A later request must not inherit keys it omits.
|
|
40
|
+
const connectionParams = {
|
|
41
|
+
...baseConnectionParams(),
|
|
42
|
+
...requestConnectionParams(request),
|
|
43
|
+
};
|
|
44
|
+
operationSeq += 1;
|
|
45
|
+
const id = `${operationSeq.toString(36)}-${Math.random().toString(36).slice(2)}`;
|
|
73
46
|
return observableToAsyncIterable({
|
|
74
47
|
subscribe(observer) {
|
|
75
|
-
|
|
48
|
+
let closed = false;
|
|
49
|
+
const websocket = new WebSocketImpl(subscriptionsEndpoint, 'graphql-ws', {
|
|
50
|
+
followRedirects: true,
|
|
51
|
+
headers: options?.headers,
|
|
52
|
+
rejectUnauthorized: options?.rejectUnauthorized ?? true,
|
|
53
|
+
skipUTF8Validation: true,
|
|
54
|
+
});
|
|
55
|
+
function closeSocket(sendStop) {
|
|
56
|
+
if (closed) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
closed = true;
|
|
60
|
+
disposers.delete(disposeSocket);
|
|
61
|
+
if (websocket.readyState === WebSocket.OPEN) {
|
|
62
|
+
if (sendStop) {
|
|
63
|
+
websocket.send(JSON.stringify({
|
|
64
|
+
type: LEGACY_WS.STOP,
|
|
65
|
+
id,
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
68
|
+
websocket.send(JSON.stringify({
|
|
69
|
+
type: LEGACY_WS.CONNECTION_TERMINATE,
|
|
70
|
+
}));
|
|
71
|
+
}
|
|
72
|
+
websocket.terminate();
|
|
73
|
+
}
|
|
74
|
+
function endWithError(err) {
|
|
75
|
+
if (closed) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
closeSocket(false);
|
|
76
79
|
observer.error(err);
|
|
80
|
+
observer.complete();
|
|
77
81
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
82
|
+
function disposeSocket() {
|
|
83
|
+
if (closed) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
closeSocket(false);
|
|
87
|
+
observer.complete();
|
|
81
88
|
}
|
|
89
|
+
disposers.add(disposeSocket);
|
|
90
|
+
websocket.onopen = () => {
|
|
91
|
+
if (closed) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
websocket.send(JSON.stringify({
|
|
95
|
+
type: LEGACY_WS.CONNECTION_INIT,
|
|
96
|
+
payload: connectionParams,
|
|
97
|
+
}), (error) => {
|
|
98
|
+
if (error) {
|
|
99
|
+
endWithError(error);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
};
|
|
103
|
+
websocket.onerror = event => {
|
|
104
|
+
endWithError(event.error);
|
|
105
|
+
};
|
|
106
|
+
websocket.onclose = () => {
|
|
107
|
+
if (closed) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
closed = true;
|
|
111
|
+
disposers.delete(disposeSocket);
|
|
112
|
+
observer.complete();
|
|
113
|
+
};
|
|
82
114
|
websocket.onmessage = event => {
|
|
83
|
-
|
|
115
|
+
if (closed) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
let data;
|
|
119
|
+
try {
|
|
120
|
+
data = JSON.parse(event.data.toString('utf-8'));
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
endWithError(error);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
84
126
|
switch (data.type) {
|
|
85
127
|
case LEGACY_WS.CONNECTION_ACK: {
|
|
86
|
-
if (websocket == null) {
|
|
87
|
-
throw new Error(`WebSocket connection is not found!`);
|
|
88
|
-
}
|
|
89
128
|
websocket.send(JSON.stringify({
|
|
90
129
|
type: LEGACY_WS.START,
|
|
91
130
|
id,
|
|
@@ -96,52 +135,54 @@ export function buildWSLegacyExecutor(subscriptionsEndpoint, WebSocketImpl, opti
|
|
|
96
135
|
},
|
|
97
136
|
}), (error) => {
|
|
98
137
|
if (error) {
|
|
99
|
-
|
|
138
|
+
endWithError(error);
|
|
100
139
|
}
|
|
101
140
|
});
|
|
102
141
|
break;
|
|
103
142
|
}
|
|
104
143
|
case LEGACY_WS.CONNECTION_ERROR: {
|
|
105
|
-
|
|
144
|
+
endWithError(data.payload);
|
|
106
145
|
break;
|
|
107
146
|
}
|
|
108
147
|
case LEGACY_WS.CONNECTION_KEEP_ALIVE: {
|
|
109
148
|
break;
|
|
110
149
|
}
|
|
111
150
|
case LEGACY_WS.DATA: {
|
|
151
|
+
if (data.id !== id) {
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
112
154
|
observer.next(data.payload);
|
|
113
155
|
break;
|
|
114
156
|
}
|
|
157
|
+
case LEGACY_WS.ERROR: {
|
|
158
|
+
if (data.id !== id) {
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
endWithError(data.payload);
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
115
164
|
case LEGACY_WS.COMPLETE: {
|
|
116
|
-
if (
|
|
117
|
-
|
|
118
|
-
type: LEGACY_WS.CONNECTION_TERMINATE,
|
|
119
|
-
}), (error) => {
|
|
120
|
-
if (error) {
|
|
121
|
-
errorHandler(error);
|
|
122
|
-
}
|
|
123
|
-
});
|
|
165
|
+
if (data.id !== id) {
|
|
166
|
+
break;
|
|
124
167
|
}
|
|
168
|
+
closeSocket(false);
|
|
125
169
|
observer.complete();
|
|
126
|
-
cleanupWebsocket();
|
|
127
170
|
break;
|
|
128
171
|
}
|
|
129
172
|
}
|
|
130
173
|
};
|
|
131
174
|
return {
|
|
132
175
|
unsubscribe: () => {
|
|
133
|
-
|
|
134
|
-
websocket?.send(JSON.stringify({
|
|
135
|
-
type: LEGACY_WS.STOP,
|
|
136
|
-
id,
|
|
137
|
-
}));
|
|
138
|
-
}
|
|
139
|
-
cleanupWebsocket();
|
|
176
|
+
closeSocket(true);
|
|
140
177
|
},
|
|
141
178
|
};
|
|
142
179
|
},
|
|
143
180
|
});
|
|
144
181
|
};
|
|
145
|
-
executor[Symbol.dispose] =
|
|
182
|
+
executor[Symbol.dispose] = () => {
|
|
183
|
+
for (const disposeSocket of [...disposers]) {
|
|
184
|
+
disposeSocket();
|
|
185
|
+
}
|
|
186
|
+
};
|
|
146
187
|
return executor;
|
|
147
188
|
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-tools/executor-legacy-ws",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.36",
|
|
4
4
|
"description": "A set of utils for faster development of GraphQL tools",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"peerDependencies": {
|
|
7
7
|
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@graphql-tools/utils": "^12.0.
|
|
10
|
+
"@graphql-tools/utils": "^12.0.2",
|
|
11
11
|
"@types/ws": "^8.0.0",
|
|
12
12
|
"isomorphic-ws": "^5.0.0",
|
|
13
13
|
"tslib": "^2.4.0",
|