@common-stack/server-stack 9.0.1-alpha.8 → 9.0.2-alpha.2
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/lib/MainStackServer.cjs +22 -47
- package/lib/MainStackServer.cjs.map +1 -1
- package/lib/MainStackServer.mjs +22 -47
- package/lib/MainStackServer.mjs.map +1 -1
- package/lib/config/env-config.cjs +0 -3
- package/lib/config/env-config.cjs.map +1 -1
- package/lib/config/env-config.mjs +0 -3
- package/lib/config/env-config.mjs.map +1 -1
- package/lib/config/moleculer.config.cjs +7 -1
- package/lib/config/moleculer.config.cjs.map +1 -1
- package/lib/config/moleculer.config.mjs +7 -1
- package/lib/config/moleculer.config.mjs.map +1 -1
- package/lib/connectors/connection-broker.cjs +12 -8
- package/lib/connectors/connection-broker.cjs.map +1 -1
- package/lib/connectors/connection-broker.mjs +12 -8
- package/lib/connectors/connection-broker.mjs.map +1 -1
- package/lib/connectors/mongo-connector.cjs +0 -2
- package/lib/connectors/mongo-connector.cjs.map +1 -1
- package/lib/connectors/mongo-connector.mjs +0 -2
- package/lib/connectors/mongo-connector.mjs.map +1 -1
- package/lib/middleware/services.cjs +46 -11
- package/lib/middleware/services.cjs.map +1 -1
- package/lib/middleware/services.mjs +46 -11
- package/lib/middleware/services.mjs.map +1 -1
- package/lib/servers/ExpressApp.cjs +16 -5
- package/lib/servers/ExpressApp.cjs.map +1 -1
- package/lib/servers/ExpressApp.mjs +15 -4
- package/lib/servers/ExpressApp.mjs.map +1 -1
- package/lib/servers/GraphqlServer.cjs +14 -30
- package/lib/servers/GraphqlServer.cjs.map +1 -1
- package/lib/servers/GraphqlServer.mjs +12 -28
- package/lib/servers/GraphqlServer.mjs.map +1 -1
- package/lib/servers/GraphqlWs.cjs.map +1 -1
- package/lib/servers/GraphqlWs.mjs.map +1 -1
- package/lib/servers/WebsocketMultipathUpdate.cjs +36 -33
- package/lib/servers/WebsocketMultipathUpdate.cjs.map +1 -1
- package/lib/servers/WebsocketMultipathUpdate.mjs +35 -32
- package/lib/servers/WebsocketMultipathUpdate.mjs.map +1 -1
- package/package.json +9 -9
- package/lib/express-adapter.cjs +0 -73
- package/lib/express-adapter.cjs.map +0 -1
- package/lib/express-adapter.mjs +0 -73
- package/lib/express-adapter.mjs.map +0 -1
|
@@ -1,12 +1,47 @@
|
|
|
1
|
-
'use strict';require('isomorphic-fetch')
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
'use strict';require('isomorphic-fetch');/**
|
|
2
|
+
* Phase-aware context middleware. Runs only the `createContextFunc` factories
|
|
3
|
+
* whose `phase` property matches the current phase (untagged factories default
|
|
4
|
+
* to `pre-beforeware`).
|
|
5
|
+
*
|
|
6
|
+
* - `pre-beforeware` (default): seeds `req.services` with the container-built
|
|
7
|
+
* service map and merges any safe context factories. Lenient: a thrown
|
|
8
|
+
* factory is logged and skipped so the request can still reach auth.
|
|
9
|
+
* - `pre-middleware`: runs after the consumer's beforewares (e.g. auth chain),
|
|
10
|
+
* so factories that depend on `req.user` belong here. Strict: errors are
|
|
11
|
+
* propagated via `next(err)`.
|
|
12
|
+
* - `post-middleware`: runs after the consumer's middlewares. Strict.
|
|
13
|
+
*/
|
|
14
|
+
const phasedContextMiddleware = (feature, phase) => async (req, res, next) => {
|
|
15
|
+
const isPreBeforeware = phase === 'pre-beforeware';
|
|
16
|
+
try {
|
|
17
|
+
const { context, matched } = await feature.runContextFactoriesForPhase(req, res, phase);
|
|
18
|
+
if (isPreBeforeware) {
|
|
19
|
+
const services = feature.services || {};
|
|
20
|
+
req.context = { ...(req.context || {}), ...context };
|
|
21
|
+
req.services = { ...(req.services || {}), ...services, ...context };
|
|
22
|
+
}
|
|
23
|
+
else if (matched) {
|
|
24
|
+
req.context = { ...(req.context || {}), ...context };
|
|
25
|
+
req.services = { ...(req.services || {}), ...context };
|
|
26
|
+
}
|
|
9
27
|
next();
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
if (isPreBeforeware) {
|
|
31
|
+
// Lenient: some factories require auth (which has not run yet).
|
|
32
|
+
// Seed req.services with the static service map so downstream
|
|
33
|
+
// beforewares still have a usable services object.
|
|
34
|
+
// eslint-disable-next-line no-console
|
|
35
|
+
console.warn(`[phased-context] pre-beforeware factory failed (continuing):`, err?.message);
|
|
36
|
+
const services = feature.services || {};
|
|
37
|
+
if (!req.services)
|
|
38
|
+
req.services = { ...services };
|
|
39
|
+
else
|
|
40
|
+
req.services = { ...req.services, ...services };
|
|
41
|
+
next();
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
next(err);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};exports.phasedContextMiddleware=phasedContextMiddleware;//# sourceMappingURL=services.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"services.cjs","sources":["../../src/middleware/services.ts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"services.cjs","sources":["../../src/middleware/services.ts"],"sourcesContent":[null],"names":[],"mappings":"yCAsBA;;;;;;;;;;;;AAYG;AACU,MAAA,uBAAuB,GAAG,CAAC,OAAgB,EAAE,KAAmB,KAAK,OAAO,GAAG,EAAE,GAAG,EAAE,IAAI,KAAI;AACvG,IAAA,MAAM,eAAe,GAAG,KAAK,KAAK,gBAAgB,CAAC;AACnD,IAAA,IAAI;AACA,QAAA,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,MAAM,OAAO,CAAC,2BAA2B,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;QACxF,IAAI,eAAe,EAAE;AACjB,YAAA,MAAM,QAAQ,GAAI,OAAe,CAAC,QAAQ,IAAI,EAAE,CAAC;AACjD,YAAA,GAAG,CAAC,OAAO,GAAG,EAAE,IAAI,GAAG,CAAC,OAAO,I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property matches the current phase (untagged factories default
|
|
4
|
+
* to `pre-beforeware`).
|
|
5
|
+
*
|
|
6
|
+
* - `pre-beforeware` (default): seeds `req.services` with the container-built
|
|
7
|
+
* service map and merges any safe context factories. Lenient: a thrown
|
|
8
|
+
* factory is logged and skipped so the request can still reach auth.
|
|
9
|
+
* - `pre-middleware`: runs after the consumer's beforewares (e.g. auth chain),
|
|
10
|
+
* so factories that depend on `req.user` belong here. Strict: errors are
|
|
11
|
+
* propagated via `next(err)`.
|
|
12
|
+
* - `post-middleware`: runs after the consumer's middlewares. Strict.
|
|
13
|
+
*/
|
|
14
|
+
const phasedContextMiddleware = (feature, phase) => async (req, res, next) => {
|
|
15
|
+
const isPreBeforeware = phase === 'pre-beforeware';
|
|
16
|
+
try {
|
|
17
|
+
const { context, matched } = await feature.runContextFactoriesForPhase(req, res, phase);
|
|
18
|
+
if (isPreBeforeware) {
|
|
19
|
+
const services = feature.services || {};
|
|
20
|
+
req.context = { ...(req.context || {}), ...context };
|
|
21
|
+
req.services = { ...(req.services || {}), ...services, ...context };
|
|
22
|
+
}
|
|
23
|
+
else if (matched) {
|
|
24
|
+
req.context = { ...(req.context || {}), ...context };
|
|
25
|
+
req.services = { ...(req.services || {}), ...context };
|
|
26
|
+
}
|
|
9
27
|
next();
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
if (isPreBeforeware) {
|
|
31
|
+
// Lenient: some factories require auth (which has not run yet).
|
|
32
|
+
// Seed req.services with the static service map so downstream
|
|
33
|
+
// beforewares still have a usable services object.
|
|
34
|
+
// eslint-disable-next-line no-console
|
|
35
|
+
console.warn(`[phased-context] pre-beforeware factory failed (continuing):`, err?.message);
|
|
36
|
+
const services = feature.services || {};
|
|
37
|
+
if (!req.services)
|
|
38
|
+
req.services = { ...services };
|
|
39
|
+
else
|
|
40
|
+
req.services = { ...req.services, ...services };
|
|
41
|
+
next();
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
next(err);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};export{phasedContextMiddleware};//# sourceMappingURL=services.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"services.mjs","sources":["../../src/middleware/services.ts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"services.mjs","sources":["../../src/middleware/services.ts"],"sourcesContent":[null],"names":[],"mappings":"yBAsBA;;;;;;;;;;;;AAYG;AACU,MAAA,uBAAuB,GAAG,CAAC,OAAgB,EAAE,KAAmB,KAAK,OAAO,GAAG,EAAE,GAAG,EAAE,IAAI,KAAI;AACvG,IAAA,MAAM,eAAe,GAAG,KAAK,KAAK,gBAAgB,CAAC;AACnD,IAAA,IAAI;AACA,QAAA,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,MAAM,OAAO,CAAC,2BAA2B,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;QACxF,IAAI,eAAe,EAAE;AACjB,YAAA,MAAM,QAAQ,GAAI,OAAe,CAAC,QAAQ,IAAI,EAAE,CAAC;AACjD,YAAA,GAAG,CAAC,OAAO,GAAG,EAAE,IAAI,GAAG,CAAC,OAAO,IAAI,EAAE,CAAC,EAAE,GAAG,OAAO,EAAE,CAAC;AACrD,YAAA,GAAG,CAAC,QAAQ,GAAG,EAAE,IAAI,GAAG,CAAC,QAAQ,IAAI,EAAE,CAAC,EAAE,GAAG,QAAQ,EAAE,GAAG,OAAO,EAAE,CAAC;SACvE;aAAM,IAAI,OAAO,EAAE;AAChB,YAAA,GAAG,CAAC,OAAO,GAAG,EAAE,IAAI,GAAG,CAAC,OAAO,IAAI,EAAE,CAAC,EAAE,GAAG,OAAO,EAAE,CAAC;AACrD,YAAA,GAAG,CAAC,QAAQ,GAAG,EAAE,IAAI,GAAG,CAAC,QAAQ,IAAI,EAAE,CAAC,EAAE,GAAG,OAAO,EAAE,CAAC;SAC1D;AACD,QAAA,IAAI,EAAE,CAAC;KACV;IAAC,OAAO,GAAG,EAAE;QACV,IAAI,eAAe,EAAE;;;;;YAKjB,OAAO,CAAC,IAAI,CAAC,CAAA,4DAAA,CAA8D,EAAG,GAAa,EAAE,OAAO,CAAC,CAAC;AACtG,YAAA,MAAM,QAAQ,GAAI,OAAe,CAAC,QAAQ,IAAI,EAAE,CAAC;YACjD,IAAI,CAAC,GAAG,CAAC,QAAQ;AAAE,gBAAA,GAAG,CAAC,QAAQ,GAAG,EAAE,GAAG,QAAQ,EAAE,CAAC;;AAC7C,gBAAA,GAAG,CAAC,QAAQ,GAAG,EAAE,GAAG,GAAG,CAAC,QAAQ,EAAE,GAAG,QAAQ,EAAE,CAAC;AACrD,YAAA,IAAI,EAAE,CAAC;SACV;aAAM;YACH,IAAI,CAAC,GAAG,CAAC,CAAC;SACb;KACJ;AACL"}
|
|
@@ -1,15 +1,22 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var express=require('express'),bodyParser=require('body-parser'),error=require('../middleware/error.cjs'),services=require('../middleware/services.cjs');require('../middleware/sentry.cjs');var redisClient=require('../middleware/redis-client.cjs'),cors=require('../middleware/cors.cjs'),Sentry=require('@sentry/node');function _interopNamespaceDefault(e){var n=Object.create(null);if(e){Object.keys(e).forEach(function(k){if(k!=='default'){var d=Object.getOwnPropertyDescriptor(e,k);Object.defineProperty(n,k,d.get?d:{enumerable:true,get:function(){return e[k]}});}})}n.default=e;return Object.freeze(n)}var Sentry__namespace=/*#__PURE__*/_interopNamespaceDefault(Sentry);/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
2
|
function expressApp(modules, options, middlewares, http, redisClient$1) {
|
|
3
|
-
const app =
|
|
3
|
+
const app = express();
|
|
4
4
|
// FIRST PRIORITY: Add Redis client middleware at the very top
|
|
5
5
|
// This ensures req.redisClient is available to ALL downstream middleware
|
|
6
6
|
if (redisClient$1) {
|
|
7
7
|
app.use(redisClient.redisClientMiddleware(redisClient$1));
|
|
8
8
|
}
|
|
9
|
-
|
|
9
|
+
// Phase 1: pre-beforeware — runs default-tagged context factories and
|
|
10
|
+
// seeds req.services from the static service map. Lenient: a factory
|
|
11
|
+
// that throws (e.g. one that requires auth) is logged and skipped.
|
|
12
|
+
app.use(services.phasedContextMiddleware(modules, 'pre-beforeware'));
|
|
13
|
+
// Consumer beforewares (e.g. auth chains for /graphql, /api/workflow).
|
|
10
14
|
for (const applyBeforeware of modules.beforewares) {
|
|
11
15
|
applyBeforeware(app);
|
|
12
16
|
}
|
|
17
|
+
// Phase 2: pre-middleware — runs context factories tagged
|
|
18
|
+
// `phase = 'pre-middleware'` (e.g. ones that read req.user). Strict.
|
|
19
|
+
app.use(services.phasedContextMiddleware(modules, 'pre-middleware'));
|
|
13
20
|
// Don't rate limit heroku
|
|
14
21
|
app.enable('trust proxy');
|
|
15
22
|
if (middlewares !== null) {
|
|
@@ -22,17 +29,21 @@ function expressApp(modules, options, middlewares, http, redisClient$1) {
|
|
|
22
29
|
res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, X-CSP-Nonce, Content-Type, Accept');
|
|
23
30
|
next();
|
|
24
31
|
});
|
|
25
|
-
app.use(
|
|
32
|
+
app.use(bodyParser.json({
|
|
26
33
|
limit: '50mb',
|
|
27
34
|
verify: (req, res, buf) => {
|
|
28
35
|
// #Todo: Find some proper solution for it
|
|
29
36
|
req.rawBody = buf;
|
|
30
37
|
},
|
|
31
38
|
}));
|
|
32
|
-
app.use(
|
|
39
|
+
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true, parameterLimit: 50000 }));
|
|
33
40
|
for (const applyMiddleware of modules.middlewares) {
|
|
34
41
|
applyMiddleware(app);
|
|
35
42
|
}
|
|
43
|
+
// Phase 3: post-middleware — runs context factories tagged
|
|
44
|
+
// `phase = 'post-middleware'`. Strict. Skipped at zero cost when none
|
|
45
|
+
// are tagged.
|
|
46
|
+
app.use(services.phasedContextMiddleware(modules, 'post-middleware'));
|
|
36
47
|
// Sentry error handler must be after all middleware/routes but before generic error handlers
|
|
37
48
|
Sentry__namespace.setupExpressErrorHandler(app);
|
|
38
49
|
if (__DEV__) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExpressApp.cjs","sources":["../../src/servers/ExpressApp.ts"],"sourcesContent":[null],"names":["redisClient","
|
|
1
|
+
{"version":3,"file":"ExpressApp.cjs","sources":["../../src/servers/ExpressApp.ts"],"sourcesContent":[null],"names":["redisClient","redisClientMiddleware","phasedContextMiddleware","corsMiddleware","Sentry","errorMiddleware"],"mappings":"4qBAAA;AAWM,SAAU,UAAU,CAAC,OAAgB,EAAE,OAAuB,EAAE,WAAW,EAAE,IAAK,EAAEA,aAAY,EAAA;AAClG,IAAA,MAAM,GAAG,GAAG,OAAO,EAAE,CAAC;;;IAItB,IAAIA,aAAW,EAAE;QACb,GAAG,CAAC,GAAG,CAACC,iCAAqB,CAACD,aAAW,CAAC,CAAC,CAAC;KAC/C;;;;IAKD,GAAG,CAAC,GAAG,CAACE,gCAAuB,CAAC,OAAO,EAAE,gBAAgB,CAAC,CAAC,CAAC;;AAG5D,IAAA,KAAK,MAAM,eAAe,IAAI,OAAO,CAAC,WAAW,EAAE;QAC/C,eAAe,CAAC,GAAG,CAAC,CAAC;KACxB;;;IAID,GAAG,CAAC,GAAG,CAACA,gCAAuB,CAAC,OAAO,EAAE,gBAAgB,CAAC,CAAC,CAAC;;AAG5D,IAAA,GAAG,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;AAE1B,IAAA,IAAI,WAAW,KAAK,IAAI,EAAE;AACtB,QAAA,GAAG,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;KACxB;AAED,IAAA,GAAG,CAAC,GAAG,CAACC,mBAAc,CAAC,CAAC;IACxB,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,KAAI;AACvB,QAAA,GAAG,CAAC,MAAM,CAAC,kCAAkC,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC;AACrE,QAAA,GAAG,CAAC,MAAM,CAAC,8BAA8B,EAAE,qBAAqB,CAAC,CAAC;AAClE,QAAA,GAAG,CAAC,MAAM,CACN,8BAA8B,EAC9B,6EAA6E,CAChF,CAAC;AACF,QAAA,IAAI,EAAE,CAAC;AACX,KAAC,CAAC,CAAC;AAEH,IAAA,GAAG,CAAC,GAAG,CACH,UAAU,CAAC,IAAI,CAAC;AACZ,QAAA,KAAK,EAAE,MAAM;QACb,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,KAAI;;AAErB,YAAA,GAAW,CAAC,OAAO,GAAG,GAAG,CAAC;SAC9B;AACJ,KAAA,CAAC,CACL,CAAC;IACF,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,cAAc,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;AAEzF,IAAA,KAAK,MAAM,eAAe,IAAI,OAAO,CAAC,WAAW,EAAE;QAC/C,eAAe,CAAC,GAAG,CAAC,CAAC;KACxB;;;;IAKD,GAAG,CAAC,GAAG,CAACD,gCAAuB,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAC,CAAC;;AAG7D,IAAAE,iBAAM,CAAC,wBAAwB,CAAC,GAAG,CAAC,CAAC;IAErC,IAAI,OAAO,EAAE;AACT,QAAA,GAAG,CAAC,GAAG,CAACC,qBAAe,CAAC,CAAC;KAC5B;AAED,IAAA,OAAO,GAAG,CAAC;AACf"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import express from'express';import bodyParser from'body-parser';import {errorMiddleware}from'../middleware/error.mjs';import {phasedContextMiddleware}from'../middleware/services.mjs';import'../middleware/sentry.mjs';import {redisClientMiddleware}from'../middleware/redis-client.mjs';import {corsMiddleware}from'../middleware/cors.mjs';import*as Sentry from'@sentry/node';/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
2
|
function expressApp(modules, options, middlewares, http, redisClient) {
|
|
3
3
|
const app = express();
|
|
4
4
|
// FIRST PRIORITY: Add Redis client middleware at the very top
|
|
@@ -6,10 +6,17 @@ function expressApp(modules, options, middlewares, http, redisClient) {
|
|
|
6
6
|
if (redisClient) {
|
|
7
7
|
app.use(redisClientMiddleware(redisClient));
|
|
8
8
|
}
|
|
9
|
-
|
|
9
|
+
// Phase 1: pre-beforeware — runs default-tagged context factories and
|
|
10
|
+
// seeds req.services from the static service map. Lenient: a factory
|
|
11
|
+
// that throws (e.g. one that requires auth) is logged and skipped.
|
|
12
|
+
app.use(phasedContextMiddleware(modules, 'pre-beforeware'));
|
|
13
|
+
// Consumer beforewares (e.g. auth chains for /graphql, /api/workflow).
|
|
10
14
|
for (const applyBeforeware of modules.beforewares) {
|
|
11
15
|
applyBeforeware(app);
|
|
12
16
|
}
|
|
17
|
+
// Phase 2: pre-middleware — runs context factories tagged
|
|
18
|
+
// `phase = 'pre-middleware'` (e.g. ones that read req.user). Strict.
|
|
19
|
+
app.use(phasedContextMiddleware(modules, 'pre-middleware'));
|
|
13
20
|
// Don't rate limit heroku
|
|
14
21
|
app.enable('trust proxy');
|
|
15
22
|
if (middlewares !== null) {
|
|
@@ -22,17 +29,21 @@ function expressApp(modules, options, middlewares, http, redisClient) {
|
|
|
22
29
|
res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, X-CSP-Nonce, Content-Type, Accept');
|
|
23
30
|
next();
|
|
24
31
|
});
|
|
25
|
-
app.use(
|
|
32
|
+
app.use(bodyParser.json({
|
|
26
33
|
limit: '50mb',
|
|
27
34
|
verify: (req, res, buf) => {
|
|
28
35
|
// #Todo: Find some proper solution for it
|
|
29
36
|
req.rawBody = buf;
|
|
30
37
|
},
|
|
31
38
|
}));
|
|
32
|
-
app.use(
|
|
39
|
+
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true, parameterLimit: 50000 }));
|
|
33
40
|
for (const applyMiddleware of modules.middlewares) {
|
|
34
41
|
applyMiddleware(app);
|
|
35
42
|
}
|
|
43
|
+
// Phase 3: post-middleware — runs context factories tagged
|
|
44
|
+
// `phase = 'post-middleware'`. Strict. Skipped at zero cost when none
|
|
45
|
+
// are tagged.
|
|
46
|
+
app.use(phasedContextMiddleware(modules, 'post-middleware'));
|
|
36
47
|
// Sentry error handler must be after all middleware/routes but before generic error handlers
|
|
37
48
|
Sentry.setupExpressErrorHandler(app);
|
|
38
49
|
if (__DEV__) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExpressApp.mjs","sources":["../../src/servers/ExpressApp.ts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ExpressApp.mjs","sources":["../../src/servers/ExpressApp.ts"],"sourcesContent":[null],"names":[],"mappings":"oXAAA;AAWM,SAAU,UAAU,CAAC,OAAgB,EAAE,OAAuB,EAAE,WAAW,EAAE,IAAK,EAAE,WAAY,EAAA;AAClG,IAAA,MAAM,GAAG,GAAG,OAAO,EAAE,CAAC;;;IAItB,IAAI,WAAW,EAAE;QACb,GAAG,CAAC,GAAG,CAAC,qBAAqB,CAAC,WAAW,CAAC,CAAC,CAAC;KAC/C;;;;IAKD,GAAG,CAAC,GAAG,CAAC,uBAAuB,CAAC,OAAO,EAAE,gBAAgB,CAAC,CAAC,CAAC;;AAG5D,IAAA,KAAK,MAAM,eAAe,IAAI,OAAO,CAAC,WAAW,EAAE;QAC/C,eAAe,CAAC,GAAG,CAAC,CAAC;KACxB;;;IAID,GAAG,CAAC,GAAG,CAAC,uBAAuB,CAAC,OAAO,EAAE,gBAAgB,CAAC,CAAC,CAAC;;AAG5D,IAAA,GAAG,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;AAE1B,IAAA,IAAI,WAAW,KAAK,IAAI,EAAE;AACtB,QAAA,GAAG,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;KACxB;AAED,IAAA,GAAG,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'),cacheControl=require('@apollo/server/plugin/cacheControl');require('isomorphic-fetch');var
|
|
1
|
+
'use strict';var server=require('@apollo/server'),express4=require('@apollo/server/express4'),cacheControl=require('@apollo/server/plugin/cacheControl'),drainHttpServer=require('@apollo/server/plugin/drainHttpServer');require('isomorphic-fetch');var express=require('express'),cors=require('cors'),Keyv=require('keyv'),KeyvRedis=require('@keyv/redis'),utils_keyvadapter=require('@apollo/utils.keyvadapter'),ws=require('ws'),storeRedis=require('@common-stack/store-redis'),GraphqlWs=require('./GraphqlWs.cjs'),envConfig=require('../config/env-config.cjs');if ((process.env.LOG_LEVEL && process.env.LOG_LEVEL === 'trace') || process.env.LOG_LEVEL === 'debug') ;
|
|
2
2
|
// @workaround as the `dataSources` not available in Subscription (websocket) Context.
|
|
3
3
|
// https://github.com/apollographql/apollo-server/issues/1526 need to revisit in Apollo-Server v3.
|
|
4
4
|
const constructDataSourcesForSubscriptions = (context, cache, dataSources) => {
|
|
@@ -12,7 +12,7 @@ const constructDataSourcesForSubscriptions = (context, cache, dataSources) => {
|
|
|
12
12
|
};
|
|
13
13
|
class GraphqlServer {
|
|
14
14
|
app;
|
|
15
|
-
|
|
15
|
+
httpServer;
|
|
16
16
|
redisClient;
|
|
17
17
|
moduleService;
|
|
18
18
|
enableSubscription;
|
|
@@ -21,10 +21,9 @@ class GraphqlServer {
|
|
|
21
21
|
plugins;
|
|
22
22
|
logger;
|
|
23
23
|
graphqlWsServer;
|
|
24
|
-
constructor(app,
|
|
25
|
-
redisClient, moduleService, enableSubscription = true, cacheKeyGenerator, invalidateCacheKeyGenerator, plugins) {
|
|
24
|
+
constructor(app, httpServer, redisClient, moduleService, enableSubscription = true, cacheKeyGenerator, invalidateCacheKeyGenerator, plugins) {
|
|
26
25
|
this.app = app;
|
|
27
|
-
this.
|
|
26
|
+
this.httpServer = httpServer;
|
|
28
27
|
this.redisClient = redisClient;
|
|
29
28
|
this.moduleService = moduleService;
|
|
30
29
|
this.enableSubscription = enableSubscription;
|
|
@@ -33,11 +32,8 @@ class GraphqlServer {
|
|
|
33
32
|
this.plugins = plugins;
|
|
34
33
|
this.logger = this.moduleService.logger.child({ className: 'GraphqlServer' });
|
|
35
34
|
if (enableSubscription) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
// - classic express: pass the httpServer as 'server' (ws)
|
|
39
|
-
const wsServer = new expressAdapter.WebSocketServer({
|
|
40
|
-
server: this.wsServerHost,
|
|
35
|
+
const wsServer = new ws.WebSocketServer({
|
|
36
|
+
server: this.httpServer,
|
|
41
37
|
path: envConfig.config.GRAPHQL_ENDPOINT,
|
|
42
38
|
});
|
|
43
39
|
this.graphqlWsServer = new GraphqlWs.GraphqlWs(wsServer, this.moduleService, this.redisClient);
|
|
@@ -54,7 +50,7 @@ class GraphqlServer {
|
|
|
54
50
|
origin: [envConfig.config.CLIENT_URL],
|
|
55
51
|
credentials: true,
|
|
56
52
|
};
|
|
57
|
-
this.app.use(__GRAPHQL_ENDPOINT__, cors(corsOptions),
|
|
53
|
+
this.app.use(__GRAPHQL_ENDPOINT__, cors(corsOptions), express.json(), express4.expressMiddleware(apolloServer, {
|
|
58
54
|
context: async ({ req, res, connection }) => {
|
|
59
55
|
let context;
|
|
60
56
|
let addons = {};
|
|
@@ -121,29 +117,17 @@ class GraphqlServer {
|
|
|
121
117
|
const cacheSet = cacheAdapter.set.bind(cacheAdapter);
|
|
122
118
|
cacheAdapter.set = (key, value, opts) => cacheSet(key.replaceAll('fqc:', ''), value, opts);
|
|
123
119
|
const { cacheKeyGenerator, invalidateCacheKeyGenerator, logger } = this;
|
|
124
|
-
const serverPlugins = [
|
|
125
|
-
cacheControl.ApolloServerPluginCacheControl(),
|
|
126
|
-
storeRedis.invalidateCachePlugin({ cache: this.redisClient, invalidateCacheKeyGenerator }),
|
|
127
|
-
storeRedis.responseCachePlugin({ logger, cacheKeyGenerator }),
|
|
128
|
-
...(this.plugins ?? []),
|
|
129
|
-
];
|
|
130
|
-
// For classic express, add drain plugin to gracefully close the HTTP server.
|
|
131
|
-
// ultimate-express (uWebSockets.js) handles connection draining natively.
|
|
132
|
-
if (!expressAdapter.isUltimateEngine && this.wsServerHost) {
|
|
133
|
-
try {
|
|
134
|
-
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
135
|
-
const { ApolloServerPluginDrainHttpServer } = require('@apollo/server/plugin/drainHttpServer');
|
|
136
|
-
serverPlugins.push(ApolloServerPluginDrainHttpServer({ httpServer: this.wsServerHost }));
|
|
137
|
-
}
|
|
138
|
-
catch {
|
|
139
|
-
// Plugin not available — skip
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
120
|
const serverConfig = {
|
|
143
121
|
schema: this.moduleService.schema,
|
|
144
122
|
allowBatchedHttpRequests: true,
|
|
145
123
|
cache: cacheAdapter,
|
|
146
|
-
plugins:
|
|
124
|
+
plugins: [
|
|
125
|
+
drainHttpServer.ApolloServerPluginDrainHttpServer({ httpServer: this.httpServer }),
|
|
126
|
+
cacheControl.ApolloServerPluginCacheControl(),
|
|
127
|
+
storeRedis.invalidateCachePlugin({ cache: this.redisClient, invalidateCacheKeyGenerator }),
|
|
128
|
+
storeRedis.responseCachePlugin({ logger, cacheKeyGenerator }),
|
|
129
|
+
...(this.plugins ?? []),
|
|
130
|
+
],
|
|
147
131
|
};
|
|
148
132
|
if (this.enableSubscription) {
|
|
149
133
|
serverConfig.plugins.push({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GraphqlServer.cjs","sources":["../../src/servers/GraphqlServer.ts"],"sourcesContent":[null],"names":["WebSocketServer","config","GraphqlWs","
|
|
1
|
+
{"version":3,"file":"GraphqlServer.cjs","sources":["../../src/servers/GraphqlServer.ts"],"sourcesContent":[null],"names":["WebSocketServer","config","GraphqlWs","expressMiddleware","KeyvAdapter","ApolloServerPluginDrainHttpServer","ApolloServerPluginCacheControl","invalidateCachePlugin","responseCachePlugin","ApolloServer"],"mappings":"2iBAwBA,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,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@ -1,4 +1,4 @@
|
|
|
1
|
-
import {ApolloServer}from'@apollo/server';import {expressMiddleware}from'@apollo/server/express4';import {ApolloServerPluginCacheControl}from'@apollo/server/plugin/cacheControl';import'isomorphic-fetch';import
|
|
1
|
+
import {ApolloServer}from'@apollo/server';import {expressMiddleware}from'@apollo/server/express4';import {ApolloServerPluginCacheControl}from'@apollo/server/plugin/cacheControl';import {ApolloServerPluginDrainHttpServer}from'@apollo/server/plugin/drainHttpServer';import'isomorphic-fetch';import express from'express';import cors from'cors';import Keyv from'keyv';import KeyvRedis from'@keyv/redis';import {KeyvAdapter}from'@apollo/utils.keyvadapter';import {WebSocketServer}from'ws';import {invalidateCachePlugin,responseCachePlugin}from'@common-stack/store-redis';import {GraphqlWs}from'./GraphqlWs.mjs';import {config}from'../config/env-config.mjs';if ((process.env.LOG_LEVEL && process.env.LOG_LEVEL === 'trace') || process.env.LOG_LEVEL === 'debug') ;
|
|
2
2
|
// @workaround as the `dataSources` not available in Subscription (websocket) Context.
|
|
3
3
|
// https://github.com/apollographql/apollo-server/issues/1526 need to revisit in Apollo-Server v3.
|
|
4
4
|
const constructDataSourcesForSubscriptions = (context, cache, dataSources) => {
|
|
@@ -12,7 +12,7 @@ const constructDataSourcesForSubscriptions = (context, cache, dataSources) => {
|
|
|
12
12
|
};
|
|
13
13
|
class GraphqlServer {
|
|
14
14
|
app;
|
|
15
|
-
|
|
15
|
+
httpServer;
|
|
16
16
|
redisClient;
|
|
17
17
|
moduleService;
|
|
18
18
|
enableSubscription;
|
|
@@ -21,10 +21,9 @@ class GraphqlServer {
|
|
|
21
21
|
plugins;
|
|
22
22
|
logger;
|
|
23
23
|
graphqlWsServer;
|
|
24
|
-
constructor(app,
|
|
25
|
-
redisClient, moduleService, enableSubscription = true, cacheKeyGenerator, invalidateCacheKeyGenerator, plugins) {
|
|
24
|
+
constructor(app, httpServer, redisClient, moduleService, enableSubscription = true, cacheKeyGenerator, invalidateCacheKeyGenerator, plugins) {
|
|
26
25
|
this.app = app;
|
|
27
|
-
this.
|
|
26
|
+
this.httpServer = httpServer;
|
|
28
27
|
this.redisClient = redisClient;
|
|
29
28
|
this.moduleService = moduleService;
|
|
30
29
|
this.enableSubscription = enableSubscription;
|
|
@@ -33,11 +32,8 @@ class GraphqlServer {
|
|
|
33
32
|
this.plugins = plugins;
|
|
34
33
|
this.logger = this.moduleService.logger.child({ className: 'GraphqlServer' });
|
|
35
34
|
if (enableSubscription) {
|
|
36
|
-
// WebSocketServer binds to different hosts depending on engine:
|
|
37
|
-
// - ultimate-express: pass the app as 'server' (ultimate-ws)
|
|
38
|
-
// - classic express: pass the httpServer as 'server' (ws)
|
|
39
35
|
const wsServer = new WebSocketServer({
|
|
40
|
-
server: this.
|
|
36
|
+
server: this.httpServer,
|
|
41
37
|
path: config.GRAPHQL_ENDPOINT,
|
|
42
38
|
});
|
|
43
39
|
this.graphqlWsServer = new GraphqlWs(wsServer, this.moduleService, this.redisClient);
|
|
@@ -121,29 +117,17 @@ class GraphqlServer {
|
|
|
121
117
|
const cacheSet = cacheAdapter.set.bind(cacheAdapter);
|
|
122
118
|
cacheAdapter.set = (key, value, opts) => cacheSet(key.replaceAll('fqc:', ''), value, opts);
|
|
123
119
|
const { cacheKeyGenerator, invalidateCacheKeyGenerator, logger } = this;
|
|
124
|
-
const serverPlugins = [
|
|
125
|
-
ApolloServerPluginCacheControl(),
|
|
126
|
-
invalidateCachePlugin({ cache: this.redisClient, invalidateCacheKeyGenerator }),
|
|
127
|
-
responseCachePlugin({ logger, cacheKeyGenerator }),
|
|
128
|
-
...(this.plugins ?? []),
|
|
129
|
-
];
|
|
130
|
-
// For classic express, add drain plugin to gracefully close the HTTP server.
|
|
131
|
-
// ultimate-express (uWebSockets.js) handles connection draining natively.
|
|
132
|
-
if (!isUltimateEngine && this.wsServerHost) {
|
|
133
|
-
try {
|
|
134
|
-
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
135
|
-
const { ApolloServerPluginDrainHttpServer } = require('@apollo/server/plugin/drainHttpServer');
|
|
136
|
-
serverPlugins.push(ApolloServerPluginDrainHttpServer({ httpServer: this.wsServerHost }));
|
|
137
|
-
}
|
|
138
|
-
catch {
|
|
139
|
-
// Plugin not available — skip
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
120
|
const serverConfig = {
|
|
143
121
|
schema: this.moduleService.schema,
|
|
144
122
|
allowBatchedHttpRequests: true,
|
|
145
123
|
cache: cacheAdapter,
|
|
146
|
-
plugins:
|
|
124
|
+
plugins: [
|
|
125
|
+
ApolloServerPluginDrainHttpServer({ httpServer: this.httpServer }),
|
|
126
|
+
ApolloServerPluginCacheControl(),
|
|
127
|
+
invalidateCachePlugin({ cache: this.redisClient, invalidateCacheKeyGenerator }),
|
|
128
|
+
responseCachePlugin({ logger, cacheKeyGenerator }),
|
|
129
|
+
...(this.plugins ?? []),
|
|
130
|
+
],
|
|
147
131
|
};
|
|
148
132
|
if (this.enableSubscription) {
|
|
149
133
|
serverConfig.plugins.push({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GraphqlServer.mjs","sources":["../../src/servers/GraphqlServer.ts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"GraphqlServer.mjs","sources":["../../src/servers/GraphqlServer.ts"],"sourcesContent":[null],"names":[],"mappings":"4oBAwBA,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,SAAS,IAAI,OAAO,CAAC,GAAG,CAAC,SAAS,KAAK,OAAO,KAAK,OAAO,CAAC,GAAG,CAAC,SAAS,KAAK,OAAO,EAAE,CAEtG;AAED;AACA;AACA,MAAM,oCAAoC,GAAG,CAAC,OAAO,EAAE,KAAK,EAAE,WAAW,KAAI;AACzE,IAAA,MAAM,oBAAoB,GAAG,CAAC,QAAQ,KAAI;QACtC,QAAQ,CAAC,UAAU,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;AAC5C,KAAC,CAAC;AACF,IAAA,KAAK,MAAM,IAAI,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EAAE,YAAY;AACnB,YAAA,OAAO,EAAE;gBACL,iCAAiC,CAAC,EAAE,UAAU,EAAE,IAAI,CAAC,UAAU,EAAE,CAAC;AAClE,gBAAA,8BAA8B,EAAE;gBAChC,qBAAqB,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,WAAW,EAAE,2BAA2B,EAAE,CAAC;AAC/E,gBAAA,mBAAmB,CAAC,EAAE,MAAM,EAAE,iBAAiB,EAAE,CAAC;AAClD,gBAAA,IAAI,IAAI,CAAC,OAAO,IAAI,EAAE,CAAC;AAC1B,aAAA;SACJ,CAAC;AAEF,QAAA,IAAI,IAAI,CAAC,kBAAkB,EAAE;AACzB,YAAA,YAAY,CAAC,OAAO,CAAC,IAAI,CAAC;AACtB,gBAAA,MAAM,eAAe,GAAA;AACjB,oBAAA,MAAM,QAAQ,GAAG;AACb,wBAAA,MAAM,WAAW,GAAA;AACb,4BAAA,MAAM,IAAI,EAAE,eAAe,EAAE,UAAU,EAAE,CAAC;yBAC7C;qBACJ,CAAC;AACF,oBAAA,QAAQ,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAChC,oBAAA,OAAO,QAAQ,CAAC;iBACnB;AACJ,aAAA,CAAC,CAAC;SACN;AAED,QAAA,OAAO,IAAI,YAAY,CAAC,YAAY,CAAC,CAAC;KACzC;AACJ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GraphqlWs.cjs","sources":["../../src/servers/GraphqlWs.ts"],"sourcesContent":[null],"names":["useServer","createWebSocketContext"],"mappings":"sIAmDa,SAAS,CAAA;AAMN,IAAA,QAAA,CAAA;AACA,IAAA,aAAA,CAAA;AACE,IAAA,KAAA,CAAA;AAPN,IAAA,kBAAkB,CAAa;AAE/B,IAAA,MAAM,CAAU;AAExB,IAAA,WAAA,CACY,
|
|
1
|
+
{"version":3,"file":"GraphqlWs.cjs","sources":["../../src/servers/GraphqlWs.ts"],"sourcesContent":[null],"names":["useServer","createWebSocketContext"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3,"file":"GraphqlWs.mjs","sources":["../../src/servers/GraphqlWs.ts"],"sourcesContent":[null],"names":[],"mappings":"iIAmDa,SAAS,CAAA;AAMN,IAAA,QAAA,CAAA;AACA,IAAA,aAAA,CAAA;AACE,IAAA,KAAA,CAAA;AAPN,IAAA,kBAAkB,CAAa;AAE/B,IAAA,MAAM,CAAU;AAExB,IAAA,WAAA,CACY,QAAyB,EACzB,aAA6B,EAC3B,KAAsC,EAAA;QAFxC,IAAQ,CAAA,QAAA,GAAR,QAAQ,CAAiB;QACzB,IAAa,CAAA,aAAA,GAAb,aAAa,CAAgB;QAC3B,IAAK,CAAA,KAAA,GAAL,KAAK,CAAiC;QAEhD,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC;KAC3C;IAEM,MAAM,GAAA;AACT,QAAA,IAAI,CAAC,kBAAkB,GAAG,SAAS,CAC/B;AACI,YAAA,MAAM,EAAE,IAAI,CAAC,aAAa,CAAC,MAAM;;;AAGjC,YAAA,OAAO,EAAE,OAAO,GAAG,KAAK,sBAAsB,CAAC,GAAG,EAAE,IAAI,CAAC,aAAa,EAAE,IAAI,CAAC,KAAK,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCtF,SAAA,EACD,IAAI,CAAC,QAAQ,CAChB,CAAC;QACF,OAAO,IAAI,CAAC,kBAAkB,CAAC;KAClC;IAEM,UAAU,GAAA;AACb,QAAA,IAAI,IAAI,CAAC,kBAAkB,EAAE;AACzB,YAAA,IAAI,CAAC,kBAAkB,CAAC,OAAO,EAAE,CAAC;SACrC;KACJ;AACJ"}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var url=require('url'),ws=require('ws'),ws$1=require('graphql-ws/lib/use/ws'),websocketContext=require('./websocket-context.cjs'),envConfig=require('../config/env-config.cjs');function _interopNamespaceDefault(e){var n=Object.create(null);if(e){Object.keys(e).forEach(function(k){if(k!=='default'){var d=Object.getOwnPropertyDescriptor(e,k);Object.defineProperty(n,k,d.get?d:{enumerable:true,get:function(){return e[k]}});}})}n.default=e;return Object.freeze(n)}var url__namespace=/*#__PURE__*/_interopNamespaceDefault(url);class WebsocketMultiPathServer {
|
|
2
2
|
moduleService;
|
|
3
3
|
cache;
|
|
4
|
-
serverHost;
|
|
5
4
|
webSockets = {};
|
|
6
5
|
// private graphqlSubscriptionServer: GraphqlSubscriptionServer;
|
|
7
6
|
_graphqlWs;
|
|
@@ -9,33 +8,17 @@
|
|
|
9
8
|
maxConnections;
|
|
10
9
|
/** Whether connection limiting is enabled */
|
|
11
10
|
limitEnabled;
|
|
12
|
-
constructor(moduleService, cache,
|
|
13
|
-
multiplePathConfig, limitConfig) {
|
|
11
|
+
constructor(moduleService, cache, multiplePathConfig, limitConfig) {
|
|
14
12
|
this.moduleService = moduleService;
|
|
15
13
|
this.cache = cache;
|
|
16
|
-
this.serverHost = serverHost;
|
|
17
14
|
// Read from validated config (Kubernetes configmap) or use defaults
|
|
18
15
|
this.maxConnections = limitConfig?.maxConnections ?? envConfig.config.WEBSOCKET_MAX_CONNECTIONS;
|
|
19
16
|
this.limitEnabled = limitConfig?.limitEnabled ?? envConfig.config.WEBSOCKET_LIMIT_ENABLED;
|
|
20
|
-
|
|
21
|
-
// - ultimate-ws: binds to the ultimate-express app via { server: app, path }
|
|
22
|
-
// - ws: binds to the http.Server via { server: httpServer, path }
|
|
23
|
-
this._graphqlWs = new expressAdapter.WebSocketServer({
|
|
24
|
-
server: this.serverHost,
|
|
25
|
-
path: __GRAPHQL_ENDPOINT__,
|
|
26
|
-
verifyClient: (info, cb) => {
|
|
27
|
-
if (this.limitEnabled && this.isConnectionLimitReached()) {
|
|
28
|
-
this.moduleService.logger?.warn?.(`WebSocket connection limit reached (${this.maxConnections}). Rejecting new connection.`);
|
|
29
|
-
cb(false, 503, 'Service Unavailable');
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
cb(true);
|
|
33
|
-
},
|
|
34
|
-
});
|
|
17
|
+
this._graphqlWs = new ws.WebSocketServer({ noServer: true, path: __GRAPHQL_ENDPOINT__ });
|
|
35
18
|
this.webSockets[__GRAPHQL_ENDPOINT__] = this._graphqlWs;
|
|
36
19
|
// Initialize useServer ONCE for the GraphQL WebSocket server
|
|
37
20
|
// This prevents memory leaks from creating new listeners on each connection
|
|
38
|
-
ws.useServer({
|
|
21
|
+
ws$1.useServer({
|
|
39
22
|
schema: moduleService.schema,
|
|
40
23
|
// Using shared context creation function to maintain consistency with GraphqlWs
|
|
41
24
|
context: async (ctx) => websocketContext.createWebSocketContext(ctx, moduleService, cache),
|
|
@@ -45,18 +28,7 @@
|
|
|
45
28
|
continue;
|
|
46
29
|
}
|
|
47
30
|
if (!this.webSockets[key]) {
|
|
48
|
-
this.webSockets[key] = new
|
|
49
|
-
server: this.serverHost,
|
|
50
|
-
path: key,
|
|
51
|
-
verifyClient: (info, cb) => {
|
|
52
|
-
if (this.limitEnabled && this.isConnectionLimitReached()) {
|
|
53
|
-
this.moduleService.logger?.warn?.(`WebSocket connection limit reached (${this.maxConnections}). Rejecting new connection.`);
|
|
54
|
-
cb(false, 503, 'Service Unavailable');
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
cb(true);
|
|
58
|
-
},
|
|
59
|
-
});
|
|
31
|
+
this.webSockets[key] = new ws.WebSocketServer({ noServer: true });
|
|
60
32
|
this.webSockets[key].on('connection', (ws, request) => {
|
|
61
33
|
Promise.all([
|
|
62
34
|
moduleService.createContext(request, null),
|
|
@@ -66,6 +38,37 @@
|
|
|
66
38
|
}
|
|
67
39
|
}
|
|
68
40
|
}
|
|
41
|
+
httpServerUpgrade(httpServer) {
|
|
42
|
+
httpServer.on('upgrade', (request, socket, head) => {
|
|
43
|
+
const pathname = url__namespace.parse(request.url).pathname;
|
|
44
|
+
// Check connection limit before accepting new connections
|
|
45
|
+
if (this.limitEnabled && this.isConnectionLimitReached()) {
|
|
46
|
+
this.moduleService.logger?.warn?.(`WebSocket connection limit reached (${this.maxConnections}). Rejecting new connection.`);
|
|
47
|
+
socket.write('HTTP/1.1 503 Service Unavailable\r\n');
|
|
48
|
+
socket.write('Retry-After: 60\r\n');
|
|
49
|
+
socket.write('\r\n');
|
|
50
|
+
socket.destroy();
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (!this.webSockets[pathname]) {
|
|
54
|
+
// in development
|
|
55
|
+
if (pathname !== '/sockjs-node') {
|
|
56
|
+
// need to destroy
|
|
57
|
+
socket.destroy();
|
|
58
|
+
}
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
// code to run when a new connection is made
|
|
62
|
+
this.webSockets[pathname].handleUpgrade(request, socket, head, (ws) => {
|
|
63
|
+
this.webSockets[pathname].emit('connection', ws, request);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
// (new GraphqlWs(this.graphqlWs, this.moduleService, this.cache)).create();
|
|
67
|
+
// useServer({
|
|
68
|
+
// schema: this.moduleService.schema,
|
|
69
|
+
// }, this.graphqlWs);
|
|
70
|
+
return httpServer;
|
|
71
|
+
}
|
|
69
72
|
/**
|
|
70
73
|
* Get total number of active WebSocket connections across all paths
|
|
71
74
|
*/
|