Skip to content

Commit

Permalink
support both socketio 2 and 3 (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
itajaja authored Dec 14, 2020
1 parent 4d17c22 commit e2c22e7
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 91 deletions.
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,16 @@
},
"dependencies": {
"express": "^4.17.1",
"redis": "^3.0.0",
"socket.io": "^3.0.1"
"redis": "^3.0.0"
},
"peerDependencies": {
"graphql": ">=0.12.3"
"graphql": ">=0.12.3",
"socket.io": ">2.0.0"
},
"peerDependenciesMeta": {
"socket.io": {
"optional": true
}
},
"devDependencies": {
"@4c/babel-preset": "^8.0.2",
Expand All @@ -67,7 +72,6 @@
"@types/express": "^4.17.9",
"@types/jest": "^26.0.19",
"@types/redis": "^2.8.28",
"@types/socket.io": "^2.1.12",
"@typescript-eslint/eslint-plugin": "^4.9.1",
"@typescript-eslint/parser": "^4.9.1",
"babel-jest": "^26.6.3",
Expand All @@ -87,6 +91,7 @@
"prettier": "^2.2.1",
"redis-mock": "^0.55.1",
"semantic-release": "^17.3.0",
"socket.io": "^3.0.4",
"travis-deploy-once": "^5.0.11",
"typescript": "^4.1.3",
"utility-types": "^3.10.0"
Expand Down
6 changes: 3 additions & 3 deletions src/AuthorizedSocketConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
validate,
} from 'graphql';
import { ExecutionResult } from 'graphql/execution/execute';
import IoServer from 'socket.io';
import io from 'socket.io';

import * as AsyncUtils from './AsyncUtils';
import { CredentialsManager } from './CredentialsManager';
Expand Down Expand Up @@ -62,7 +62,7 @@ const acknowledge = (cb?: () => void) => {
* - Rudimentary connection constraints (max connections)
*/
export default class AuthorizedSocketConnection<TContext, TCredentials> {
socket: IoServer.Socket;
socket: io.Socket;

config: AuthorizedSocketOptions<TContext, TCredentials>;

Expand All @@ -74,7 +74,7 @@ export default class AuthorizedSocketConnection<TContext, TCredentials> {
>;

constructor(
socket: IoServer.Socket,
socket: io.Socket,
config: AuthorizedSocketOptions<TContext, TCredentials>,
) {
this.socket = socket;
Expand Down
24 changes: 15 additions & 9 deletions src/SubscriptionServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { promisify } from 'util';

import express from 'express';
import type { GraphQLSchema } from 'graphql';
import IoServer from 'socket.io';
import type io from 'socket.io';

import AuthorizedSocketConnection from './AuthorizedSocketConnection';
import type { CreateValidationRules } from './AuthorizedSocketConnection';
Expand All @@ -23,6 +23,7 @@ export type SubscriptionServerConfig<TContext, TCredentials> = {
maxSubscriptionsPerConnection?: number;
createValidationRules?: CreateValidationRules;
createLogger?: CreateLogger;
socketIoServer: io.Server;
};

// eslint-disable-next-line @typescript-eslint/no-empty-function
Expand All @@ -33,7 +34,7 @@ export default class SubscriptionServer<TContext, TCredentials> {

log: Logger;

io: IoServer.Server;
io: io.Server;

constructor(config: SubscriptionServerConfig<TContext, TCredentials>) {
this.config = config;
Expand All @@ -42,12 +43,17 @@ export default class SubscriptionServer<TContext, TCredentials> {
config.createLogger || defaultCreateLogger;
this.log = createLogger('@4c/SubscriptionServer::Server');

this.io = new IoServer.Server({
serveClient: false,
path: this.config.path,
transports: ['websocket'],
wsEngine: 'ws',
});
this.io = config.socketIoServer;
if (!this.io) {
// eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
const IoServer = require('socket.io').Server;
this.io = new IoServer({
serveClient: false,
path: this.config.path,
transports: ['websocket'],
wsEngine: 'ws',
});
}

this.io.on('connection', this.handleConnection);
}
Expand All @@ -56,7 +62,7 @@ export default class SubscriptionServer<TContext, TCredentials> {
this.io.attach(httpServer);
}

handleConnection = (socket: IoServer.Socket) => {
handleConnection = (socket: io.Socket) => {
this.log('debug', 'new socket connection');

const request = Object.create((express as any).request);
Expand Down
117 changes: 42 additions & 75 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1793,12 +1793,15 @@
dependencies:
"@types/node" "*"

"@types/engine.io@*":
version "3.1.4"
resolved "https://registry.yarnpkg.com/@types/engine.io/-/engine.io-3.1.4.tgz#3d9472711d179daa7c95c051e50ad411e18a9bdc"
integrity sha512-98rXVukLD6/ozrQ2O80NAlWDGA4INg+tqsEReWJldqyi2fulC9V7Use/n28SWgROXKm6003ycWV4gZHoF8GA6w==
dependencies:
"@types/node" "*"
"@types/cookie@^0.4.0":
version "0.4.0"
resolved "https://registry.yarnpkg.com/@types/cookie/-/cookie-0.4.0.tgz#14f854c0f93d326e39da6e3b6f34f7d37513d108"
integrity sha512-y7mImlc/rNkvCRmg8gC3/lj87S7pTUIJ6QGjwHR9WQJcFs+ZMTOaoPrkdFA/YdbuqVEmEbb5RdhVxMkAcgOnpg==

"@types/cors@^2.8.8":
version "2.8.9"
resolved "https://registry.yarnpkg.com/@types/cors/-/cors-2.8.9.tgz#4bd1fcac72eca8d5bec93e76c7fdcbdc1bc2cd4a"
integrity sha512-zurD1ibz21BRlAOIKP8yhrxlqKx6L9VCwkB5kMiP6nZAhoF5MvC7qS1qPA7nRcr1GJolfkQC7/EAL4hdYejLtg==

"@types/express-serve-static-core@*":
version "4.17.13"
Expand Down Expand Up @@ -1918,6 +1921,11 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.19.4.tgz#cdfbb62e26c7435ed9aab9c941393cc3598e9b46"
integrity sha512-o3oj1bETk8kBwzz1WlO6JWL/AfAA3Vm6J1B3C9CsdxHYp7XgPiH7OEXPUbZTndHlRaIElrANkQfe6ZmfJb3H2w==

"@types/node@^14.14.7":
version "14.14.13"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.13.tgz#9e425079799322113ae8477297ae6ef51b8e0cdf"
integrity sha512-vbxr0VZ8exFMMAjCW8rJwaya0dMCDyYW2ZRdTyjtrCvJoENMpdUHOT/eTzvgyA5ZnqRZ/sI0NwqAxNHKYokLJQ==

"@types/normalize-package-data@^2.4.0":
version "2.4.0"
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e"
Expand Down Expand Up @@ -1975,22 +1983,6 @@
"@types/mime" "*"
"@types/node" "*"

"@types/socket.io-parser@*":
version "2.2.1"
resolved "https://registry.yarnpkg.com/@types/socket.io-parser/-/socket.io-parser-2.2.1.tgz#dc94aed303839487f4975249a32a548109ea3647"
integrity sha512-+JNb+7N7tSINyXPxAJb62+NcpC1x/fPn7z818W4xeNCdPTp6VsO/X8fCsg6+ug4a56m1v9sEiTIIUKVupcHOFQ==
dependencies:
"@types/node" "*"

"@types/socket.io@^2.1.12":
version "2.1.12"
resolved "https://registry.yarnpkg.com/@types/socket.io/-/socket.io-2.1.12.tgz#91187f826a5dd2ed1113e935815bbaf9627e5cb0"
integrity sha512-oStc5VFkpb0AsjOxQUj9ztX5Iziatyla/rjZTYbFGoVrrKwd+JU2mtxk7iSl5RGYx9WunLo6UXW1fBzQok/ZyA==
dependencies:
"@types/engine.io" "*"
"@types/node" "*"
"@types/socket.io-parser" "*"

"@types/stack-utils@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.0.tgz#7036640b4e21cc2f259ae826ce843d277dad8cff"
Expand Down Expand Up @@ -2647,6 +2639,11 @@ balanced-match@^1.0.0:
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
integrity sha1-ibTRmasr7kneFk6gK4nORi1xt2c=

[email protected]:
version "0.1.4"
resolved "https://registry.yarnpkg.com/base64-arraybuffer/-/base64-arraybuffer-0.1.4.tgz#9818c79e059b1355f97e0428a017c838e90ba812"
integrity sha1-mBjHngWbE1X5fgQooBfIOOkLqBI=

[email protected], base64id@~2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/base64id/-/base64id-2.0.0.tgz#2770ac6bc47d312af97a8bf9a634342e0cd25cb6"
Expand Down Expand Up @@ -4041,7 +4038,7 @@ debug@~4.1.0:
dependencies:
ms "^2.1.1"

debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
Expand Down Expand Up @@ -4490,14 +4487,16 @@ end-of-stream@^1.0.0, end-of-stream@^1.1.0:
once "^1.4.0"

engine.io-parser@~4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/engine.io-parser/-/engine.io-parser-4.0.1.tgz#6444c3cf2523ba4fc3bbaedd4fe425e6bcb16479"
integrity sha512-v5aZK1hlckcJDGmHz3W8xvI3NUHYc9t8QtTbqdR5OaH3S9iJZilPubauOm+vLWOMMWzpE3hiq92l9lTAHamRCg==
version "4.0.2"
resolved "https://registry.yarnpkg.com/engine.io-parser/-/engine.io-parser-4.0.2.tgz#e41d0b3fb66f7bf4a3671d2038a154024edb501e"
integrity sha512-sHfEQv6nmtJrq6TKuIz5kyEKH/qSdK56H/A+7DnAuUPWosnIZAS2NHNcPLmyjtY3cGS/MqJdZbUjW97JU72iYg==
dependencies:
base64-arraybuffer "0.1.4"

engine.io@~4.0.0:
version "4.0.2"
resolved "https://registry.yarnpkg.com/engine.io/-/engine.io-4.0.2.tgz#82d5788cb50c408102f59f3598f2e62f53191e95"
integrity sha512-sumdttqWLNjbuSMOSgDdL2xiEld9s5QZDk9VLyr4e28o+lzNNADhU3qpQDAY7cm2VZH0Otw/U0fL8mEjZ6kBMg==
version "4.0.5"
resolved "https://registry.yarnpkg.com/engine.io/-/engine.io-4.0.5.tgz#3ff6d5c72560ad93423c1ce1e807733206622a1c"
integrity sha512-Ri+whTNr2PKklxQkfbGjwEo+kCBUM4Qxk4wtLqLrhH+b1up2NFL9g9pjYWiCV/oazwB0rArnvF/ZmZN2ab5Hpg==
dependencies:
accepts "~1.3.4"
base64id "2.0.0"
Expand Down Expand Up @@ -6252,7 +6251,7 @@ import-local@^3.0.2:
pkg-dir "^4.2.0"
resolve-cwd "^3.0.0"

imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
Expand Down Expand Up @@ -7863,11 +7862,6 @@ lockfile@^1.0.4:
dependencies:
signal-exit "^3.0.2"

lodash._baseindexof@*:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=

lodash._baseuniq@~4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
Expand All @@ -7876,33 +7870,11 @@ lodash._baseuniq@~4.6.0:
lodash._createset "~4.0.0"
lodash._root "~3.0.0"

lodash._bindcallback@*:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=

lodash._cacheindexof@*:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=

lodash._createcache@*:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
dependencies:
lodash._getnative "^3.0.0"

lodash._createset@~4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=

lodash._getnative@*, lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=

lodash._reinterpolate@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
Expand Down Expand Up @@ -7953,11 +7925,6 @@ lodash.isstring@^4.0.1:
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"
integrity sha1-1SfftUVuynzJu5XV2ur4i6VKVFE=

lodash.restparam@*:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=

lodash.sortby@^4.7.0:
version "4.7.0"
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
Expand Down Expand Up @@ -8895,7 +8862,6 @@ npm@^6.14.8:
cmd-shim "^3.0.3"
columnify "~1.5.4"
config-chain "^1.1.12"
debuglog "*"
detect-indent "~5.0.0"
detect-newline "^2.1.0"
dezalgo "~1.0.3"
Expand All @@ -8910,7 +8876,6 @@ npm@^6.14.8:
has-unicode "~2.0.1"
hosted-git-info "^2.8.8"
iferr "^1.0.2"
imurmurhash "*"
infer-owner "^1.0.4"
inflight "~1.0.6"
inherits "^2.0.4"
Expand All @@ -8929,14 +8894,8 @@ npm@^6.14.8:
libnpx "^10.2.4"
lock-verify "^2.1.0"
lockfile "^1.0.4"
lodash._baseindexof "*"
lodash._baseuniq "~4.6.0"
lodash._bindcallback "*"
lodash._cacheindexof "*"
lodash._createcache "*"
lodash._getnative "*"
lodash.clonedeep "~4.5.0"
lodash.restparam "*"
lodash.union "~4.6.0"
lodash.uniq "~4.5.0"
lodash.without "~4.4.0"
Expand Down Expand Up @@ -10978,11 +10937,14 @@ socket.io-parser@~4.0.1:
component-emitter "~1.3.0"
debug "~4.1.0"

socket.io@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/socket.io/-/socket.io-3.0.1.tgz#7e60375f9154968cdae45363a4d7c0570bcfd653"
integrity sha512-oVYbCQ4sCwm4wVi+f1bsE3YFXcvd6b4JjVP8D7IZnQqBeJOKX9XrdgJWSbXqBEqUXPY3jdTqb1M3s4KFTa/IHg==
socket.io@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/socket.io/-/socket.io-3.0.4.tgz#20130a80b57e48dadb671f22e3776047cc7f9d53"
integrity sha512-Vj1jUoO75WGc9txWd311ZJJqS9Dr8QtNJJ7gk2r7dcM/yGe9sit7qOijQl3GAwhpBOz/W8CwkD7R6yob07nLbA==
dependencies:
"@types/cookie" "^0.4.0"
"@types/cors" "^2.8.8"
"@types/node" "^14.14.7"
accepts "~1.3.4"
base64id "~2.0.0"
debug "~4.1.0"
Expand Down Expand Up @@ -12553,7 +12515,12 @@ ws@^6.2.1:
dependencies:
async-limiter "~1.0.0"

ws@^7.1.2, ws@^7.2.3:
ws@^7.1.2:
version "7.4.1"
resolved "https://registry.yarnpkg.com/ws/-/ws-7.4.1.tgz#a333be02696bd0e54cea0434e21dcc8a9ac294bb"
integrity sha512-pTsP8UAfhy3sk1lSk/O/s4tjD0CRwvMnzvwr4OKGX7ZvqZtUyx4KIJB5JWbkykPoc55tixMGgTNoh3k4FkNGFQ==

ws@^7.2.3:
version "7.4.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-7.4.0.tgz#a5dd76a24197940d4a8bb9e0e152bb4503764da7"
integrity sha512-kyFwXuV/5ymf+IXhS6f0+eAFvydbaBW3zjpT6hUdAh/hbVjTIB5EHBGi0bPoCLSK2wcuz3BrEkB9LrYv1Nm4NQ==
Expand Down

0 comments on commit e2c22e7

Please sign in to comment.