@graphql-tools/executor-legacy-ws 0.0.1-alpha-20221108142800-3beb5fe2

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 ADDED
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildWSLegacyExecutor = exports.LEGACY_WS = void 0;
4
+ const utils_1 = require("@graphql-tools/utils");
5
+ const graphql_1 = require("graphql");
6
+ var LEGACY_WS;
7
+ (function (LEGACY_WS) {
8
+ LEGACY_WS["CONNECTION_INIT"] = "connection_init";
9
+ LEGACY_WS["CONNECTION_ACK"] = "connection_ack";
10
+ LEGACY_WS["CONNECTION_ERROR"] = "connection_error";
11
+ LEGACY_WS["CONNECTION_KEEP_ALIVE"] = "ka";
12
+ LEGACY_WS["START"] = "start";
13
+ LEGACY_WS["STOP"] = "stop";
14
+ LEGACY_WS["CONNECTION_TERMINATE"] = "connection_terminate";
15
+ LEGACY_WS["DATA"] = "data";
16
+ LEGACY_WS["ERROR"] = "error";
17
+ LEGACY_WS["COMPLETE"] = "complete";
18
+ })(LEGACY_WS = exports.LEGACY_WS || (exports.LEGACY_WS = {}));
19
+ function buildWSLegacyExecutor(subscriptionsEndpoint, WebSocketImpl, options) {
20
+ const observerById = new Map();
21
+ let websocket = null;
22
+ const ensureWebsocket = () => {
23
+ websocket = new WebSocketImpl(subscriptionsEndpoint, 'graphql-ws', {
24
+ followRedirects: true,
25
+ headers: options === null || options === void 0 ? void 0 : options.headers,
26
+ rejectUnauthorized: false,
27
+ skipUTF8Validation: true,
28
+ });
29
+ websocket.onopen = () => {
30
+ let payload = {};
31
+ switch (typeof (options === null || options === void 0 ? void 0 : options.connectionParams)) {
32
+ case 'function':
33
+ payload = options === null || options === void 0 ? void 0 : options.connectionParams();
34
+ break;
35
+ case 'object':
36
+ payload = options === null || options === void 0 ? void 0 : options.connectionParams;
37
+ break;
38
+ }
39
+ websocket.send(JSON.stringify({
40
+ type: LEGACY_WS.CONNECTION_INIT,
41
+ payload,
42
+ }));
43
+ };
44
+ };
45
+ const cleanupWebsocket = () => {
46
+ if (websocket != null && observerById.size === 0) {
47
+ websocket.send(JSON.stringify({
48
+ type: LEGACY_WS.CONNECTION_TERMINATE,
49
+ }));
50
+ websocket.terminate();
51
+ websocket = null;
52
+ }
53
+ };
54
+ return function legacyExecutor(request) {
55
+ const id = Date.now().toString();
56
+ return (0, utils_1.observableToAsyncIterable)({
57
+ subscribe(observer) {
58
+ ensureWebsocket();
59
+ if (websocket == null) {
60
+ throw new Error(`WebSocket connection is not found!`);
61
+ }
62
+ websocket.onmessage = event => {
63
+ const data = JSON.parse(event.data.toString('utf-8'));
64
+ switch (data.type) {
65
+ case LEGACY_WS.CONNECTION_ACK: {
66
+ if (websocket == null) {
67
+ throw new Error(`WebSocket connection is not found!`);
68
+ }
69
+ websocket.send(JSON.stringify({
70
+ type: LEGACY_WS.START,
71
+ id,
72
+ payload: {
73
+ query: (0, graphql_1.print)(request.document),
74
+ variables: request.variables,
75
+ operationName: request.operationName,
76
+ },
77
+ }));
78
+ break;
79
+ }
80
+ case LEGACY_WS.CONNECTION_ERROR: {
81
+ observer.error(data.payload);
82
+ break;
83
+ }
84
+ case LEGACY_WS.CONNECTION_KEEP_ALIVE: {
85
+ break;
86
+ }
87
+ case LEGACY_WS.DATA: {
88
+ observer.next(data.payload);
89
+ break;
90
+ }
91
+ case LEGACY_WS.COMPLETE: {
92
+ if (websocket == null) {
93
+ throw new Error(`WebSocket connection is not found!`);
94
+ }
95
+ websocket.send(JSON.stringify({
96
+ type: LEGACY_WS.CONNECTION_TERMINATE,
97
+ }));
98
+ observer.complete();
99
+ cleanupWebsocket();
100
+ break;
101
+ }
102
+ }
103
+ };
104
+ return {
105
+ unsubscribe: () => {
106
+ websocket === null || websocket === void 0 ? void 0 : websocket.send(JSON.stringify({
107
+ type: LEGACY_WS.STOP,
108
+ id,
109
+ }));
110
+ cleanupWebsocket();
111
+ },
112
+ };
113
+ },
114
+ });
115
+ };
116
+ }
117
+ exports.buildWSLegacyExecutor = buildWSLegacyExecutor;
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
package/esm/index.js ADDED
@@ -0,0 +1,113 @@
1
+ import { observableToAsyncIterable } from '@graphql-tools/utils';
2
+ import { print } from 'graphql';
3
+ export var LEGACY_WS;
4
+ (function (LEGACY_WS) {
5
+ LEGACY_WS["CONNECTION_INIT"] = "connection_init";
6
+ LEGACY_WS["CONNECTION_ACK"] = "connection_ack";
7
+ LEGACY_WS["CONNECTION_ERROR"] = "connection_error";
8
+ LEGACY_WS["CONNECTION_KEEP_ALIVE"] = "ka";
9
+ LEGACY_WS["START"] = "start";
10
+ LEGACY_WS["STOP"] = "stop";
11
+ LEGACY_WS["CONNECTION_TERMINATE"] = "connection_terminate";
12
+ LEGACY_WS["DATA"] = "data";
13
+ LEGACY_WS["ERROR"] = "error";
14
+ LEGACY_WS["COMPLETE"] = "complete";
15
+ })(LEGACY_WS || (LEGACY_WS = {}));
16
+ export function buildWSLegacyExecutor(subscriptionsEndpoint, WebSocketImpl, options) {
17
+ const observerById = new Map();
18
+ let websocket = null;
19
+ const ensureWebsocket = () => {
20
+ websocket = new WebSocketImpl(subscriptionsEndpoint, 'graphql-ws', {
21
+ followRedirects: true,
22
+ headers: options === null || options === void 0 ? void 0 : options.headers,
23
+ rejectUnauthorized: false,
24
+ skipUTF8Validation: true,
25
+ });
26
+ websocket.onopen = () => {
27
+ let payload = {};
28
+ switch (typeof (options === null || options === void 0 ? void 0 : options.connectionParams)) {
29
+ case 'function':
30
+ payload = options === null || options === void 0 ? void 0 : options.connectionParams();
31
+ break;
32
+ case 'object':
33
+ payload = options === null || options === void 0 ? void 0 : options.connectionParams;
34
+ break;
35
+ }
36
+ websocket.send(JSON.stringify({
37
+ type: LEGACY_WS.CONNECTION_INIT,
38
+ payload,
39
+ }));
40
+ };
41
+ };
42
+ const cleanupWebsocket = () => {
43
+ if (websocket != null && observerById.size === 0) {
44
+ websocket.send(JSON.stringify({
45
+ type: LEGACY_WS.CONNECTION_TERMINATE,
46
+ }));
47
+ websocket.terminate();
48
+ websocket = null;
49
+ }
50
+ };
51
+ return function legacyExecutor(request) {
52
+ const id = Date.now().toString();
53
+ return observableToAsyncIterable({
54
+ subscribe(observer) {
55
+ ensureWebsocket();
56
+ if (websocket == null) {
57
+ throw new Error(`WebSocket connection is not found!`);
58
+ }
59
+ websocket.onmessage = event => {
60
+ const data = JSON.parse(event.data.toString('utf-8'));
61
+ switch (data.type) {
62
+ case LEGACY_WS.CONNECTION_ACK: {
63
+ if (websocket == null) {
64
+ throw new Error(`WebSocket connection is not found!`);
65
+ }
66
+ websocket.send(JSON.stringify({
67
+ type: LEGACY_WS.START,
68
+ id,
69
+ payload: {
70
+ query: print(request.document),
71
+ variables: request.variables,
72
+ operationName: request.operationName,
73
+ },
74
+ }));
75
+ break;
76
+ }
77
+ case LEGACY_WS.CONNECTION_ERROR: {
78
+ observer.error(data.payload);
79
+ break;
80
+ }
81
+ case LEGACY_WS.CONNECTION_KEEP_ALIVE: {
82
+ break;
83
+ }
84
+ case LEGACY_WS.DATA: {
85
+ observer.next(data.payload);
86
+ break;
87
+ }
88
+ case LEGACY_WS.COMPLETE: {
89
+ if (websocket == null) {
90
+ throw new Error(`WebSocket connection is not found!`);
91
+ }
92
+ websocket.send(JSON.stringify({
93
+ type: LEGACY_WS.CONNECTION_TERMINATE,
94
+ }));
95
+ observer.complete();
96
+ cleanupWebsocket();
97
+ break;
98
+ }
99
+ }
100
+ };
101
+ return {
102
+ unsubscribe: () => {
103
+ websocket === null || websocket === void 0 ? void 0 : websocket.send(JSON.stringify({
104
+ type: LEGACY_WS.STOP,
105
+ id,
106
+ }));
107
+ cleanupWebsocket();
108
+ },
109
+ };
110
+ },
111
+ });
112
+ };
113
+ }
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@graphql-tools/executor-legacy-ws",
3
+ "version": "0.0.1-alpha-20221108142800-3beb5fe2",
4
+ "description": "A set of utils for faster development of GraphQL tools",
5
+ "sideEffects": false,
6
+ "peerDependencies": {
7
+ "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0"
8
+ },
9
+ "dependencies": {
10
+ "@types/ws": "^8.0.0",
11
+ "@graphql-tools/utils": "9.1.0",
12
+ "isomorphic-ws": "5.0.0",
13
+ "ws": "8.11.0"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "ardatan/graphql-tools",
18
+ "directory": "packages/executors/legacy-ws"
19
+ },
20
+ "author": "Arda TANRIKULU <ardatanrikulu@gmail.com>",
21
+ "license": "MIT",
22
+ "main": "cjs/index.js",
23
+ "module": "esm/index.js",
24
+ "typings": "typings/index.d.ts",
25
+ "typescript": {
26
+ "definition": "typings/index.d.ts"
27
+ },
28
+ "type": "module",
29
+ "exports": {
30
+ ".": {
31
+ "require": {
32
+ "types": "./typings/index.d.cts",
33
+ "default": "./cjs/index.js"
34
+ },
35
+ "import": {
36
+ "types": "./typings/index.d.ts",
37
+ "default": "./esm/index.js"
38
+ },
39
+ "default": {
40
+ "types": "./typings/index.d.ts",
41
+ "default": "./esm/index.js"
42
+ }
43
+ },
44
+ "./*": {
45
+ "require": {
46
+ "types": "./typings/*.d.cts",
47
+ "default": "./cjs/*.js"
48
+ },
49
+ "import": {
50
+ "types": "./typings/*.d.ts",
51
+ "default": "./esm/*.js"
52
+ },
53
+ "default": {
54
+ "types": "./typings/*.d.ts",
55
+ "default": "./esm/*.js"
56
+ }
57
+ },
58
+ "./package.json": "./package.json"
59
+ }
60
+ }
@@ -0,0 +1,20 @@
1
+ /// <reference types="ws" />
2
+ import { Executor } from '@graphql-tools/utils';
3
+ import WebSocket from 'isomorphic-ws';
4
+ export declare enum LEGACY_WS {
5
+ CONNECTION_INIT = "connection_init",
6
+ CONNECTION_ACK = "connection_ack",
7
+ CONNECTION_ERROR = "connection_error",
8
+ CONNECTION_KEEP_ALIVE = "ka",
9
+ START = "start",
10
+ STOP = "stop",
11
+ CONNECTION_TERMINATE = "connection_terminate",
12
+ DATA = "data",
13
+ ERROR = "error",
14
+ COMPLETE = "complete"
15
+ }
16
+ export interface LegacyWSExecutorOpts {
17
+ connectionParams?: Record<string, any>;
18
+ headers?: Record<string, any>;
19
+ }
20
+ export declare function buildWSLegacyExecutor(subscriptionsEndpoint: string, WebSocketImpl: typeof WebSocket, options?: LegacyWSExecutorOpts): Executor;
@@ -0,0 +1,20 @@
1
+ /// <reference types="ws" />
2
+ import { Executor } from '@graphql-tools/utils';
3
+ import WebSocket from 'isomorphic-ws';
4
+ export declare enum LEGACY_WS {
5
+ CONNECTION_INIT = "connection_init",
6
+ CONNECTION_ACK = "connection_ack",
7
+ CONNECTION_ERROR = "connection_error",
8
+ CONNECTION_KEEP_ALIVE = "ka",
9
+ START = "start",
10
+ STOP = "stop",
11
+ CONNECTION_TERMINATE = "connection_terminate",
12
+ DATA = "data",
13
+ ERROR = "error",
14
+ COMPLETE = "complete"
15
+ }
16
+ export interface LegacyWSExecutorOpts {
17
+ connectionParams?: Record<string, any>;
18
+ headers?: Record<string, any>;
19
+ }
20
+ export declare function buildWSLegacyExecutor(subscriptionsEndpoint: string, WebSocketImpl: typeof WebSocket, options?: LegacyWSExecutorOpts): Executor;