@makerx/graphql-core 2.3.0 → 3.0.0-beta.0
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/README.md +76 -100
- package/context.d.ts +17 -22
- package/context.js +18 -30
- package/context.js.map +1 -1
- package/context.mjs +12 -23
- package/context.mjs.map +1 -1
- package/index.d.ts +4 -4
- package/index.js +10 -10
- package/index.mjs +4 -4
- package/logging.js +4 -4
- package/logging.js.map +1 -1
- package/logging.mjs +1 -1
- package/logging.mjs.map +1 -1
- package/package.json +6 -10
- package/request-info.d.ts +22 -0
- package/request-info.js +85 -0
- package/request-info.js.map +1 -0
- package/request-info.mjs +80 -0
- package/request-info.mjs.map +1 -0
- package/shield.d.ts +1 -1
- package/shield.js.map +1 -1
- package/shield.mjs +1 -1
- package/shield.mjs.map +1 -1
- package/subscriptions/context.d.ts +12 -9
- package/subscriptions/context.js +16 -25
- package/subscriptions/context.js.map +1 -1
- package/subscriptions/context.mjs +12 -21
- package/subscriptions/context.mjs.map +1 -1
- package/subscriptions/server.js +4 -4
- package/subscriptions/server.js.map +1 -1
- package/subscriptions/server.mjs +1 -1
- package/subscriptions/server.mjs.map +1 -1
- package/testing.d.ts +0 -7
- package/testing.js +0 -68
- package/testing.js.map +0 -1
- package/testing.mjs +0 -60
- package/testing.mjs.map +0 -1
- /package/{models.d.ts → type-utils.d.ts} +0 -0
|
@@ -1,28 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { pick } from 'lodash';
|
|
1
|
+
import { pick } from 'es-toolkit/compat';
|
|
3
2
|
import { User } from '../User.mjs';
|
|
3
|
+
import { buildConnectRequestInfo } from '../request-info.mjs';
|
|
4
4
|
import { extractTokenFromConnectionParams } from './utils.mjs';
|
|
5
5
|
|
|
6
|
-
const createSubscriptionContextFactory = (
|
|
7
|
-
|
|
6
|
+
const createSubscriptionContextFactory = (config) => {
|
|
7
|
+
const { requestLogger, augmentRequestInfo, claimsToLog, requestInfoToLog, augmentContext } = config;
|
|
8
|
+
// The conditional type on CreateSubscriptionContextConfig guarantees `createUser` is provided
|
|
9
|
+
// when TUser is narrower than `User | undefined`, so defaulting is sound here.
|
|
10
|
+
const createUser = (config.createUser ?? defaultCreateUser);
|
|
8
11
|
return async (input) => {
|
|
9
12
|
const { connectRequest: req, claims } = input;
|
|
10
|
-
const xForwardedFor = req.headers['x-forwarded-for'];
|
|
11
|
-
const host = Array.isArray(xForwardedFor) ? xForwardedFor[0] : (xForwardedFor ?? req.headers.host);
|
|
12
13
|
// build request info from the connect request and socket
|
|
13
14
|
const requestInfo = {
|
|
14
|
-
|
|
15
|
-
protocol: 'ws',
|
|
16
|
-
host: host ?? '',
|
|
17
|
-
method: req.method ?? '',
|
|
18
|
-
url: req.url ?? '',
|
|
19
|
-
origin: req.headers['origin'] ?? '',
|
|
20
|
-
referer: req.headers.referer?.toString() ?? '',
|
|
21
|
-
arrLogId: req.headers['x-arr-log-id']?.toString() ?? undefined,
|
|
22
|
-
clientIp: req.headers['x-forwarded-for']?.toString() ?? req.socket.remoteAddress,
|
|
23
|
-
correlationId: req.headers['x-correlation-id']?.toString() ?? undefined,
|
|
15
|
+
...buildConnectRequestInfo(req),
|
|
24
16
|
...augmentRequestInfo?.(input),
|
|
25
17
|
};
|
|
18
|
+
const user = await createUser(input);
|
|
26
19
|
// create request logger
|
|
27
20
|
let logger;
|
|
28
21
|
if (typeof requestLogger === 'function') {
|
|
@@ -35,19 +28,17 @@ const createSubscriptionContextFactory = ({ requestLogger, augmentRequestInfo, c
|
|
|
35
28
|
if (claims && claimsToLog?.length)
|
|
36
29
|
requestLoggerMetadata.user = pick(claims, claimsToLog);
|
|
37
30
|
// build the request logger
|
|
38
|
-
logger = requestLogger(requestLoggerMetadata);
|
|
31
|
+
logger = requestLogger(requestLoggerMetadata, user);
|
|
39
32
|
}
|
|
40
33
|
else
|
|
41
34
|
logger = requestLogger;
|
|
42
35
|
const graphqlContext = {
|
|
43
36
|
requestInfo,
|
|
44
37
|
logger,
|
|
45
|
-
user
|
|
38
|
+
user,
|
|
46
39
|
started: Date.now(),
|
|
47
40
|
};
|
|
48
|
-
const augmentedGraphQLContext = augmentContext
|
|
49
|
-
? { ...graphqlContext, ...(await augmentContext(graphqlContext)) }
|
|
50
|
-
: graphqlContext;
|
|
41
|
+
const augmentedGraphQLContext = augmentContext ? { ...graphqlContext, ...(await augmentContext(graphqlContext)) } : graphqlContext;
|
|
51
42
|
return augmentedGraphQLContext;
|
|
52
43
|
};
|
|
53
44
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.mjs","sources":["../../src/subscriptions/context.ts"],"sourcesContent":["import type { Logger } from '@makerx/node-common'\nimport {
|
|
1
|
+
{"version":3,"file":"context.mjs","sources":["../../src/subscriptions/context.ts"],"sourcesContent":["import type { Logger } from '@makerx/node-common'\nimport { pick } from 'es-toolkit/compat'\nimport type { IncomingMessage } from 'http'\nimport { User } from '../User'\nimport type { CreateRequestLogger, GraphQLContext, JwtPayload, RequestInfo, RequestInfoLogKey } from '../context'\nimport { buildConnectRequestInfo } from '../request-info'\nimport { extractTokenFromConnectionParams } from './utils'\n\nexport interface SubscriptionContextInput {\n connectRequest: IncomingMessage\n connectionParams?: Readonly<Record<string, unknown>>\n claims?: JwtPayload\n}\n\nexport type CreateSubscriptionUser<T = User | undefined> = (input: SubscriptionContextInput) => Promise<T> | T\nexport type CreateSubscriptionContext<TContext = GraphQLContext> = (input: SubscriptionContextInput) => Promise<TContext>\nexport type AugmentSubscriptionRequestInfo = (input: SubscriptionContextInput) => Record<string, unknown>\n\n// `createUser` is optional when TUser is compatible with the default `User | undefined`\n// (i.e. defaultCreateUser can satisfy it), and required when TUser is narrower.\nexport type CreateSubscriptionContextConfig<\n TUser = User | undefined,\n TAugment extends Record<string, unknown> = Record<string, never>,\n TLogger extends Logger = Logger,\n> = {\n requestLogger: CreateRequestLogger<TUser, TLogger> | TLogger\n augmentRequestInfo?: AugmentSubscriptionRequestInfo\n claimsToLog?: string[]\n requestInfoToLog?: Array<RequestInfoLogKey>\n augmentContext?: (context: GraphQLContext<TLogger, RequestInfo, TUser>) => TAugment | Promise<TAugment>\n} & ([User | undefined] extends [TUser] ? { createUser?: CreateSubscriptionUser<TUser> } : { createUser: CreateSubscriptionUser<TUser> })\n\nexport const createSubscriptionContextFactory = <\n TUser = User | undefined,\n TAugment extends Record<string, unknown> = Record<string, never>,\n TLogger extends Logger = Logger,\n>(\n config: CreateSubscriptionContextConfig<TUser, TAugment, TLogger>,\n): CreateSubscriptionContext<GraphQLContext<TLogger, RequestInfo, TUser> & TAugment> => {\n const { requestLogger, augmentRequestInfo, claimsToLog, requestInfoToLog, augmentContext } = config\n // The conditional type on CreateSubscriptionContextConfig guarantees `createUser` is provided\n // when TUser is narrower than `User | undefined`, so defaulting is sound here.\n const createUser = (config.createUser ?? defaultCreateUser) as CreateSubscriptionUser<TUser>\n\n return async (input: SubscriptionContextInput) => {\n const { connectRequest: req, claims } = input\n\n // build request info from the connect request and socket\n const requestInfo: RequestInfo = {\n ...buildConnectRequestInfo(req),\n ...augmentRequestInfo?.(input),\n }\n\n const user = await createUser(input)\n\n // create request logger\n let logger: TLogger\n if (typeof requestLogger === 'function') {\n // build request logger metadata\n const requestLoggerMetadata: Record<string, unknown> = {}\n // add request info to log\n if (requestInfoToLog?.length) requestLoggerMetadata.request = pick(requestInfo, requestInfoToLog)\n // add user claims to log\n if (claims && claimsToLog?.length) requestLoggerMetadata.user = pick(claims, claimsToLog)\n // build the request logger\n logger = requestLogger(requestLoggerMetadata, user)\n } else logger = requestLogger\n\n const graphqlContext: GraphQLContext<TLogger, RequestInfo, TUser> = {\n requestInfo,\n logger,\n user,\n started: Date.now(),\n }\n\n const augmentedGraphQLContext = augmentContext ? { ...graphqlContext, ...(await augmentContext(graphqlContext)) } : graphqlContext\n\n return augmentedGraphQLContext as GraphQLContext<TLogger, RequestInfo, TUser> & TAugment\n }\n}\n\nconst defaultCreateUser: CreateSubscriptionUser<User | undefined> = ({ claims, connectionParams }) => {\n if (!claims) return Promise.resolve(undefined)\n const accessToken = extractTokenFromConnectionParams(connectionParams)\n return Promise.resolve(new User(claims, accessToken ?? ''))\n}\n"],"names":[],"mappings":";;;;;AAgCO,MAAM,gCAAgC,GAAG,CAK9C,MAAiE,KACoB;AACrF,IAAA,MAAM,EAAE,aAAa,EAAE,kBAAkB,EAAE,WAAW,EAAE,gBAAgB,EAAE,cAAc,EAAE,GAAG,MAAM;;;IAGnG,MAAM,UAAU,IAAI,MAAM,CAAC,UAAU,IAAI,iBAAiB,CAAkC;AAE5F,IAAA,OAAO,OAAO,KAA+B,KAAI;QAC/C,MAAM,EAAE,cAAc,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,KAAK;;AAG7C,QAAA,MAAM,WAAW,GAAgB;YAC/B,GAAG,uBAAuB,CAAC,GAAG,CAAC;AAC/B,YAAA,GAAG,kBAAkB,GAAG,KAAK,CAAC;SAC/B;AAED,QAAA,MAAM,IAAI,GAAG,MAAM,UAAU,CAAC,KAAK,CAAC;;AAGpC,QAAA,IAAI,MAAe;AACnB,QAAA,IAAI,OAAO,aAAa,KAAK,UAAU,EAAE;;YAEvC,MAAM,qBAAqB,GAA4B,EAAE;;YAEzD,IAAI,gBAAgB,EAAE,MAAM;gBAAE,qBAAqB,CAAC,OAAO,GAAG,IAAI,CAAC,WAAW,EAAE,gBAAgB,CAAC;;AAEjG,YAAA,IAAI,MAAM,IAAI,WAAW,EAAE,MAAM;gBAAE,qBAAqB,CAAC,IAAI,GAAG,IAAI,CAAC,MAAM,EAAE,WAAW,CAAC;;AAEzF,YAAA,MAAM,GAAG,aAAa,CAAC,qBAAqB,EAAE,IAAI,CAAC;QACrD;;YAAO,MAAM,GAAG,aAAa;AAE7B,QAAA,MAAM,cAAc,GAAgD;YAClE,WAAW;YACX,MAAM;YACN,IAAI;AACJ,YAAA,OAAO,EAAE,IAAI,CAAC,GAAG,EAAE;SACpB;QAED,MAAM,uBAAuB,GAAG,cAAc,GAAG,EAAE,GAAG,cAAc,EAAE,IAAI,MAAM,cAAc,CAAC,cAAc,CAAC,CAAC,EAAE,GAAG,cAAc;AAElI,QAAA,OAAO,uBAAiF;AAC1F,IAAA,CAAC;AACH;AAEA,MAAM,iBAAiB,GAA6C,CAAC,EAAE,MAAM,EAAE,gBAAgB,EAAE,KAAI;AACnG,IAAA,IAAI,CAAC,MAAM;AAAE,QAAA,OAAO,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC;AAC9C,IAAA,MAAM,WAAW,GAAG,gCAAgC,CAAC,gBAAgB,CAAC;AACtE,IAAA,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC,MAAM,EAAE,WAAW,IAAI,EAAE,CAAC,CAAC;AAC7D,CAAC;;;;"}
|
package/subscriptions/server.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var graphqlWs = require('graphql-ws');
|
|
4
4
|
var ws$1 = require('graphql-ws/lib/use/ws');
|
|
5
|
-
var
|
|
5
|
+
var compat = require('es-toolkit/compat');
|
|
6
6
|
var ws = require('ws');
|
|
7
7
|
var logging = require('../logging.js');
|
|
8
8
|
var utils = require('./utils.js');
|
|
@@ -38,7 +38,7 @@ function useSubscriptionsServer({ schema, httpServer, createSubscriptionContext,
|
|
|
38
38
|
const claims = await verifyToken(utils.getHost(ctx.extra.request), token);
|
|
39
39
|
ctx.extra.claims = claims;
|
|
40
40
|
logger.info(connectionEstablished, {
|
|
41
|
-
claims:
|
|
41
|
+
claims: compat.pick(claims, jwtClaimsToLog),
|
|
42
42
|
});
|
|
43
43
|
return true;
|
|
44
44
|
}
|
|
@@ -62,12 +62,12 @@ function useSubscriptionsServer({ schema, httpServer, createSubscriptionContext,
|
|
|
62
62
|
await verifyToken(utils.getHost(ctx.extra.request), token);
|
|
63
63
|
}
|
|
64
64
|
catch (error) {
|
|
65
|
-
logger.warn('Subscription connection auth token is no longer valid', { claims:
|
|
65
|
+
logger.warn('Subscription connection auth token is no longer valid', { claims: compat.pick(ctx.extra.claims, jwtClaimsToLog), error });
|
|
66
66
|
ctx.extra.socket.close(graphqlWs.CloseCode.Forbidden, 'Forbidden');
|
|
67
67
|
}
|
|
68
68
|
},
|
|
69
69
|
onDisconnect({ extra: { claims } }) {
|
|
70
|
-
logger.info('Subscription connection disconnected', { claims:
|
|
70
|
+
logger.info('Subscription connection disconnected', { claims: compat.pick(claims, jwtClaimsToLog) });
|
|
71
71
|
},
|
|
72
72
|
context: async (ctx) => {
|
|
73
73
|
return createSubscriptionContext({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.js","sources":["../../src/subscriptions/server.ts"],"sourcesContent":["import type { Logger } from '@makerx/node-common'\nimport type { GraphQLSchema } from 'graphql'\nimport { CloseCode } from 'graphql-ws'\nimport { useServer } from 'graphql-ws/lib/use/ws'\nimport type { Server } from 'http'\nimport { pick } from '
|
|
1
|
+
{"version":3,"file":"server.js","sources":["../../src/subscriptions/server.ts"],"sourcesContent":["import type { Logger } from '@makerx/node-common'\nimport type { GraphQLSchema } from 'graphql'\nimport { CloseCode } from 'graphql-ws'\nimport { useServer } from 'graphql-ws/lib/use/ws'\nimport type { Server } from 'http'\nimport { pick } from 'es-toolkit/compat'\nimport { WebSocketServer } from 'ws'\nimport type { GraphQLContext, JwtPayload } from '../context'\nimport { logSubscriptionOperation } from '../logging'\nimport type { CreateSubscriptionContext } from './context'\nimport { extractTokenFromConnectionParams, getHost } from './utils'\n\nexport function useSubscriptionsServer<TLogger extends Logger = Logger>({\n schema,\n httpServer,\n createSubscriptionContext,\n logger,\n operationLogLevel = 'info',\n path = '/graphql',\n verifyToken,\n requireAuth,\n jwtClaimsToLog = ['oid', 'iss'],\n resolveSubscriptionOperationLogger,\n}: {\n schema: GraphQLSchema\n httpServer: Server\n createSubscriptionContext: CreateSubscriptionContext\n logger: TLogger\n operationLogLevel?: keyof TLogger\n path?: string\n verifyToken?: (host: string, token: string) => Promise<JwtPayload>\n requireAuth?: boolean\n jwtClaimsToLog?: string[]\n resolveSubscriptionOperationLogger?: (context: GraphQLContext) => TLogger\n}) {\n if (requireAuth && !verifyToken) throw new Error('verifyToken must be supplied when requireAuth is true')\n\n const wsServer = new WebSocketServer({\n server: httpServer,\n path,\n })\n\n return useServer(\n {\n schema,\n onError(_ctx, message, errors) {\n logger.error('GraphQL subscriptions server error', { message, errors })\n },\n onConnect: async (ctx) => {\n const connectionEstablished = 'Subscription connection established'\n if (!verifyToken) {\n logger.info(connectionEstablished)\n return true\n }\n\n const token = extractTokenFromConnectionParams(ctx.connectionParams)\n if (!token) {\n if (requireAuth) {\n logger.error('No authorization parameter was supplied via websocket connection params')\n return false\n }\n logger.info(connectionEstablished)\n return true\n }\n\n try {\n const claims = await verifyToken(getHost(ctx.extra.request), token)\n ctx.extra.claims = claims as unknown as undefined\n logger.info(connectionEstablished, {\n claims: pick(claims, jwtClaimsToLog),\n })\n return true\n } catch (error) {\n logger.error('Failed to verify subscription connection auth token', { error })\n return false\n }\n },\n onSubscribe: async (ctx) => {\n if (!verifyToken) return\n const token = extractTokenFromConnectionParams(ctx.connectionParams)\n if (!token) {\n if (requireAuth) {\n logger.error('No authorization parameter was supplied via websocket connection params')\n ctx.extra.socket.close(CloseCode.Forbidden, 'Forbidden')\n }\n return\n }\n try {\n await verifyToken(getHost(ctx.extra.request), token)\n } catch (error) {\n logger.warn('Subscription connection auth token is no longer valid', { claims: pick(ctx.extra.claims, jwtClaimsToLog), error })\n ctx.extra.socket.close(CloseCode.Forbidden, 'Forbidden')\n }\n },\n onDisconnect({ extra: { claims } }) {\n logger.info('Subscription connection disconnected', { claims: pick(claims, jwtClaimsToLog) })\n },\n context: async (ctx) => {\n return createSubscriptionContext({\n connectRequest: ctx.extra.request,\n connectionParams: ctx.connectionParams,\n claims: ctx.extra.claims as JwtPayload | undefined,\n })\n },\n onOperation(_ctx, _message, args) {\n logSubscriptionOperation({ args, logLevel: operationLogLevel, resolveLogger: resolveSubscriptionOperationLogger })\n },\n onNext(_ctx, _message, args, { data, ...result }) {\n logSubscriptionOperation({ args, logLevel: operationLogLevel, result, resolveLogger: resolveSubscriptionOperationLogger })\n },\n },\n wsServer,\n )\n}\n"],"names":["WebSocketServer","useServer","extractTokenFromConnectionParams","getHost","pick","CloseCode","logSubscriptionOperation"],"mappings":";;;;;;;;;AAYM,SAAU,sBAAsB,CAAkC,EACtE,MAAM,EACN,UAAU,EACV,yBAAyB,EACzB,MAAM,EACN,iBAAiB,GAAG,MAAM,EAC1B,IAAI,GAAG,UAAU,EACjB,WAAW,EACX,WAAW,EACX,cAAc,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,EAC/B,kCAAkC,GAYnC,EAAA;IACC,IAAI,WAAW,IAAI,CAAC,WAAW;AAAE,QAAA,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC;AAEzG,IAAA,MAAM,QAAQ,GAAG,IAAIA,kBAAe,CAAC;AACnC,QAAA,MAAM,EAAE,UAAU;QAClB,IAAI;AACL,KAAA,CAAC;AAEF,IAAA,OAAOC,cAAS,CACd;QACE,MAAM;AACN,QAAA,OAAO,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,EAAA;YAC3B,MAAM,CAAC,KAAK,CAAC,oCAAoC,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;QACzE,CAAC;AACD,QAAA,SAAS,EAAE,OAAO,GAAG,KAAI;YACvB,MAAM,qBAAqB,GAAG,qCAAqC;YACnE,IAAI,CAAC,WAAW,EAAE;AAChB,gBAAA,MAAM,CAAC,IAAI,CAAC,qBAAqB,CAAC;AAClC,gBAAA,OAAO,IAAI;YACb;YAEA,MAAM,KAAK,GAAGC,sCAAgC,CAAC,GAAG,CAAC,gBAAgB,CAAC;YACpE,IAAI,CAAC,KAAK,EAAE;gBACV,IAAI,WAAW,EAAE;AACf,oBAAA,MAAM,CAAC,KAAK,CAAC,yEAAyE,CAAC;AACvF,oBAAA,OAAO,KAAK;gBACd;AACA,gBAAA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G,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAACC,mBAAS,CAAC,SAAS,EAAE,WAAW,CAAC;YAC1D;QACF,CAAC;AACD,QAAA,YAAY,CAAC,EAAE,KAAK,EAAE,EAAE,MAAM,EAAE,EAAE,EAAA;AAChC,YAAA,MAAM,CAAC,IAAI,CAAC,sCAAsC,EAAE,EAAE,MAAM,EAAED,WAAI,CAAC,MAAM,EAAE,cAAc,CAAC,EAAE,CAAC;QAC/F,CAAC;AACD,QAAA,OAAO,EAAE,OAAO,GAAG,KAAI;AACrB,YAAA,OAAO,yBAAyB,CAAC;AAC/B,gBAAA,cAAc,EAAE,GAAG,CAAC,KAAK,CAAC,OAAO;gBACjC,gBAAgB,EAAE,GAAG,CAAC,gBAAgB;AACtC,gBAAA,MAAM,EAAE,GAAG,CAAC,KAAK,CAAC,MAAgC;AACnD,aAAA,CAAC;QACJ,CAAC;AACD,QAAA,WAAW,CAAC,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAA;AAC9B,YAAAE,gCAAwB,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,iBAAiB,EAAE,aAAa,EAAE,kCAAkC,EAAE,CAAC;QACpH,CAAC;AACD,QAAA,MAAM,CAAC,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,GAAG,MAAM,EAAE,EAAA;AAC9C,YAAAA,gCAAwB,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,iBAAiB,EAAE,MAAM,EAAE,aAAa,EAAE,kCAAkC,EAAE,CAAC;QAC5H,CAAC;KACF,EACD,QAAQ,CACT;AACH;;;;"}
|
package/subscriptions/server.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CloseCode } from 'graphql-ws';
|
|
2
2
|
import { useServer } from 'graphql-ws/lib/use/ws';
|
|
3
|
-
import { pick } from '
|
|
3
|
+
import { pick } from 'es-toolkit/compat';
|
|
4
4
|
import { WebSocketServer } from 'ws';
|
|
5
5
|
import { logSubscriptionOperation } from '../logging.mjs';
|
|
6
6
|
import { extractTokenFromConnectionParams, getHost } from './utils.mjs';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.mjs","sources":["../../src/subscriptions/server.ts"],"sourcesContent":["import type { Logger } from '@makerx/node-common'\nimport type { GraphQLSchema } from 'graphql'\nimport { CloseCode } from 'graphql-ws'\nimport { useServer } from 'graphql-ws/lib/use/ws'\nimport type { Server } from 'http'\nimport { pick } from '
|
|
1
|
+
{"version":3,"file":"server.mjs","sources":["../../src/subscriptions/server.ts"],"sourcesContent":["import type { Logger } from '@makerx/node-common'\nimport type { GraphQLSchema } from 'graphql'\nimport { CloseCode } from 'graphql-ws'\nimport { useServer } from 'graphql-ws/lib/use/ws'\nimport type { Server } from 'http'\nimport { pick } from 'es-toolkit/compat'\nimport { WebSocketServer } from 'ws'\nimport type { GraphQLContext, JwtPayload } from '../context'\nimport { logSubscriptionOperation } from '../logging'\nimport type { CreateSubscriptionContext } from './context'\nimport { extractTokenFromConnectionParams, getHost } from './utils'\n\nexport function useSubscriptionsServer<TLogger extends Logger = Logger>({\n schema,\n httpServer,\n createSubscriptionContext,\n logger,\n operationLogLevel = 'info',\n path = '/graphql',\n verifyToken,\n requireAuth,\n jwtClaimsToLog = ['oid', 'iss'],\n resolveSubscriptionOperationLogger,\n}: {\n schema: GraphQLSchema\n httpServer: Server\n createSubscriptionContext: CreateSubscriptionContext\n logger: TLogger\n operationLogLevel?: keyof TLogger\n path?: string\n verifyToken?: (host: string, token: string) => Promise<JwtPayload>\n requireAuth?: boolean\n jwtClaimsToLog?: string[]\n resolveSubscriptionOperationLogger?: (context: GraphQLContext) => TLogger\n}) {\n if (requireAuth && !verifyToken) throw new Error('verifyToken must be supplied when requireAuth is true')\n\n const wsServer = new WebSocketServer({\n server: httpServer,\n path,\n })\n\n return useServer(\n {\n schema,\n onError(_ctx, message, errors) {\n logger.error('GraphQL subscriptions server error', { message, errors })\n },\n onConnect: async (ctx) => {\n const connectionEstablished = 'Subscription connection established'\n if (!verifyToken) {\n logger.info(connectionEstablished)\n return true\n }\n\n const token = extractTokenFromConnectionParams(ctx.connectionParams)\n if (!token) {\n if (requireAuth) {\n logger.error('No authorization parameter was supplied via websocket connection params')\n return false\n }\n logger.info(connectionEstablished)\n return true\n }\n\n try {\n const claims = await verifyToken(getHost(ctx.extra.request), token)\n ctx.extra.claims = claims as unknown as undefined\n logger.info(connectionEstablished, {\n claims: pick(claims, jwtClaimsToLog),\n })\n return true\n } catch (error) {\n logger.error('Failed to verify subscription connection auth token', { error })\n return false\n }\n },\n onSubscribe: async (ctx) => {\n if (!verifyToken) return\n const token = extractTokenFromConnectionParams(ctx.connectionParams)\n if (!token) {\n if (requireAuth) {\n logger.error('No authorization parameter was supplied via websocket connection params')\n ctx.extra.socket.close(CloseCode.Forbidden, 'Forbidden')\n }\n return\n }\n try {\n await verifyToken(getHost(ctx.extra.request), token)\n } catch (error) {\n logger.warn('Subscription connection auth token is no longer valid', { claims: pick(ctx.extra.claims, jwtClaimsToLog), error })\n ctx.extra.socket.close(CloseCode.Forbidden, 'Forbidden')\n }\n },\n onDisconnect({ extra: { claims } }) {\n logger.info('Subscription connection disconnected', { claims: pick(claims, jwtClaimsToLog) })\n },\n context: async (ctx) => {\n return createSubscriptionContext({\n connectRequest: ctx.extra.request,\n connectionParams: ctx.connectionParams,\n claims: ctx.extra.claims as JwtPayload | undefined,\n })\n },\n onOperation(_ctx, _message, args) {\n logSubscriptionOperation({ args, logLevel: operationLogLevel, resolveLogger: resolveSubscriptionOperationLogger })\n },\n onNext(_ctx, _message, args, { data, ...result }) {\n logSubscriptionOperation({ args, logLevel: operationLogLevel, result, resolveLogger: resolveSubscriptionOperationLogger })\n },\n },\n wsServer,\n )\n}\n"],"names":[],"mappings":";;;;;;;AAYM,SAAU,sBAAsB,CAAkC,EACtE,MAAM,EACN,UAAU,EACV,yBAAyB,EACzB,MAAM,EACN,iBAAiB,GAAG,MAAM,EAC1B,I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KAAI;AACrB,YAAA,OAAO,yBAAyB,CAAC;AAC/B,gBAAA,cAAc,EAAE,GAAG,CAAC,KAAK,CAAC,OAAO;gBACjC,gBAAgB,EAAE,GAAG,CAAC,gBAAgB;AACtC,gBAAA,MAAM,EAAE,GAAG,CAAC,KAAK,CAAC,MAAgC;AACnD,aAAA,CAAC;QACJ,CAAC;AACD,QAAA,WAAW,CAAC,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAA;AAC9B,YAAA,wBAAwB,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,iBAAiB,EAAE,aAAa,EAAE,kCAAkC,EAAE,CAAC;QACpH,CAAC;AACD,QAAA,MAAM,CAAC,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,GAAG,MAAM,EAAE,EAAA;AAC9C,YAAA,wBAAwB,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,iBAAiB,EAAE,MAAM,EAAE,aAAa,EAAE,kCAAkC,EAAE,CAAC;QAC5H,CAAC;KACF,EACD,QAAQ,CACT;AACH;;;;"}
|
package/testing.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { NormalizedCacheObject } from '@apollo/client/core';
|
|
2
|
-
import { ApolloClient } from '@apollo/client/core';
|
|
3
|
-
import type { ApolloClientOptions } from '@apollo/client/core/ApolloClient';
|
|
4
|
-
import type { ClientCredentialsConfig } from '@makerx/node-common';
|
|
5
|
-
export * from '@apollo/client/core';
|
|
6
|
-
export declare const createTestClient: (url: string, accessToken?: string, options?: Partial<ApolloClientOptions<NormalizedCacheObject>>) => ApolloClient<NormalizedCacheObject>;
|
|
7
|
-
export declare const createTestClientWithClientCredentials: (url: string, clientCredentialsConfig: ClientCredentialsConfig, options?: Partial<ApolloClientOptions<NormalizedCacheObject>>) => ApolloClient<NormalizedCacheObject>;
|
package/testing.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var core = require('@apollo/client/core');
|
|
4
|
-
var context = require('@apollo/client/link/context');
|
|
5
|
-
var nodeCommon = require('@makerx/node-common');
|
|
6
|
-
|
|
7
|
-
const bearerTokenLink = (accessToken) => context.setContext((_, { headers }) => {
|
|
8
|
-
if (accessToken)
|
|
9
|
-
return {
|
|
10
|
-
headers: {
|
|
11
|
-
...headers,
|
|
12
|
-
authorization: `Bearer ${accessToken}`,
|
|
13
|
-
},
|
|
14
|
-
};
|
|
15
|
-
return {
|
|
16
|
-
headers,
|
|
17
|
-
};
|
|
18
|
-
});
|
|
19
|
-
const clientCredentialsLink = (clientCredentialsConfig) => {
|
|
20
|
-
let promise;
|
|
21
|
-
return context.setContext(async () => {
|
|
22
|
-
if (!promise)
|
|
23
|
-
promise = nodeCommon.getClientCredentialsToken(clientCredentialsConfig);
|
|
24
|
-
let tokenResponse = await promise;
|
|
25
|
-
if (tokenResponse.isExpired) {
|
|
26
|
-
promise = nodeCommon.getClientCredentialsToken(clientCredentialsConfig);
|
|
27
|
-
tokenResponse = await promise;
|
|
28
|
-
}
|
|
29
|
-
return {
|
|
30
|
-
headers: {
|
|
31
|
-
authorization: `Bearer ${tokenResponse.access_token}`,
|
|
32
|
-
},
|
|
33
|
-
};
|
|
34
|
-
});
|
|
35
|
-
};
|
|
36
|
-
const httpLink = (url) => core.createHttpLink({
|
|
37
|
-
uri: url,
|
|
38
|
-
});
|
|
39
|
-
const createTestClient = (url, accessToken, options) => new core.ApolloClient({
|
|
40
|
-
link: core.from([bearerTokenLink(accessToken), httpLink(url)]),
|
|
41
|
-
cache: new core.InMemoryCache(),
|
|
42
|
-
defaultOptions: {
|
|
43
|
-
query: {
|
|
44
|
-
errorPolicy: 'all',
|
|
45
|
-
},
|
|
46
|
-
},
|
|
47
|
-
...options,
|
|
48
|
-
});
|
|
49
|
-
const createTestClientWithClientCredentials = (url, clientCredentialsConfig, options) => new core.ApolloClient({
|
|
50
|
-
link: core.from([clientCredentialsLink(clientCredentialsConfig), httpLink(url)]),
|
|
51
|
-
cache: new core.InMemoryCache(),
|
|
52
|
-
defaultOptions: {
|
|
53
|
-
query: {
|
|
54
|
-
errorPolicy: 'all',
|
|
55
|
-
},
|
|
56
|
-
},
|
|
57
|
-
...options,
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
exports.createTestClient = createTestClient;
|
|
61
|
-
exports.createTestClientWithClientCredentials = createTestClientWithClientCredentials;
|
|
62
|
-
Object.keys(core).forEach(function (k) {
|
|
63
|
-
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
64
|
-
enumerable: true,
|
|
65
|
-
get: function () { return core[k]; }
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
//# sourceMappingURL=testing.js.map
|
package/testing.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"testing.js","sources":["../src/testing.ts"],"sourcesContent":["import type { NormalizedCacheObject } from '@apollo/client/core'\nimport { ApolloClient, createHttpLink, from, InMemoryCache } from '@apollo/client/core'\nimport type { ApolloClientOptions } from '@apollo/client/core/ApolloClient'\nimport { setContext } from '@apollo/client/link/context'\nimport type { AccessTokenResponse, ClientCredentialsConfig } from '@makerx/node-common'\nimport { getClientCredentialsToken } from '@makerx/node-common'\n\nexport * from '@apollo/client/core'\n\nconst bearerTokenLink = (accessToken?: string) =>\n setContext((_, { headers }) => {\n if (accessToken)\n return {\n headers: {\n ...headers,\n authorization: `Bearer ${accessToken}`,\n },\n }\n return {\n headers,\n }\n })\n\nconst clientCredentialsLink = (clientCredentialsConfig: ClientCredentialsConfig) => {\n let promise: Promise<AccessTokenResponse> | undefined\n return setContext(async () => {\n if (!promise) promise = getClientCredentialsToken(clientCredentialsConfig)\n let tokenResponse = await promise\n if (tokenResponse.isExpired) {\n promise = getClientCredentialsToken(clientCredentialsConfig)\n tokenResponse = await promise\n }\n return {\n headers: {\n authorization: `Bearer ${tokenResponse.access_token}`,\n },\n }\n })\n}\n\nconst httpLink = (url: string) =>\n createHttpLink({\n uri: url,\n })\n\nexport const createTestClient = (\n url: string,\n accessToken?: string,\n options?: Partial<ApolloClientOptions<NormalizedCacheObject>>,\n): ApolloClient<NormalizedCacheObject> =>\n new ApolloClient({\n link: from([bearerTokenLink(accessToken), httpLink(url)]),\n cache: new InMemoryCache(),\n defaultOptions: {\n query: {\n errorPolicy: 'all',\n },\n },\n ...options,\n })\n\nexport const createTestClientWithClientCredentials = (\n url: string,\n clientCredentialsConfig: ClientCredentialsConfig,\n options?: Partial<ApolloClientOptions<NormalizedCacheObject>>,\n): ApolloClient<NormalizedCacheObject> =>\n new ApolloClient({\n link: from([clientCredentialsLink(clientCredentialsConfig), httpLink(url)]),\n cache: new InMemoryCache(),\n defaultOptions: {\n query: {\n errorPolicy: 'all',\n },\n },\n ...options,\n })\n"],"names":["setContext","getClientCredentialsToken","createHttpLink","ApolloClient","from","InMemoryCache"],"mappings":";;;;;;AASA,MAAM,eAAe,GAAG,CAAC,WAAoB,KAC3CA,kBAAU,CAAC,CAAC,CAAC,EAAE,EAAE,OAAO,EAAE,KAAI;AAC5B,IAAA,IAAI,WAAW;QACb,OAAO;AACL,YAAA,OAAO,EAAE;AACP,gBAAA,GAAG,OAAO;gBACV,aAAa,EAAE,CAAA,OAAA,EAAU,WAAW,CAAA,CAAE;AACvC,aAAA;SACF;IACH,OAAO;QACL,OAAO;KACR;AACH,CAAC,CAAC;AAEJ,MAAM,qBAAqB,GAAG,CAAC,uBAAgD,KAAI;AACjF,IAAA,IAAI,OAAiD;AACrD,IAAA,OAAOA,kBAAU,CAAC,YAAW;AAC3B,QAAA,IAAI,CAAC,OAAO;AAAE,YAAA,OAAO,GAAGC,oCAAyB,CAAC,uBAAuB,CAAC;AAC1E,QAAA,IAAI,aAAa,GAAG,MAAM,OAAO;AACjC,QAAA,IAAI,aAAa,CAAC,SAAS,EAAE;AAC3B,YAAA,OAAO,GAAGA,oCAAyB,CAAC,uBAAuB,CAAC;YAC5D,aAAa,GAAG,MAAM,OAAO;QAC/B;QACA,OAAO;AACL,YAAA,OAAO,EAAE;AACP,gBAAA,aAAa,EAAE,CAAA,OAAA,EAAU,aAAa,CAAC,YAAY,CAAA,CAAE;AACtD,aAAA;SACF;AACH,IAAA,CAAC,CAAC;AACJ,CAAC;AAED,MAAM,QAAQ,GAAG,CAAC,GAAW,KAC3BC,mBAAc,CAAC;AACb,IAAA,GAAG,EAAE,GAAG;AACT,CAAA,CAAC;AAEG,MAAM,gBAAgB,GAAG,CAC9B,GAAW,EACX,WAAoB,EACpB,OAA6D,KAE7D,IAAIC,iBAAY,CAAC;AACf,IAAA,IAAI,EAAEC,SAAI,CAAC,CAAC,eAAe,CAAC,WAAW,CAAC,EAAE,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC;IACzD,KAAK,EAAE,IAAIC,kBAAa,EAAE;AAC1B,IAAA,cAAc,EAAE;AACd,QAAA,KAAK,EAAE;AACL,YAAA,WAAW,EAAE,KAAK;AACnB,SAAA;AACF,KAAA;AACD,IAAA,GAAG,OAAO;AACX,CAAA;AAEI,MAAM,qCAAqC,GAAG,CACnD,GAAW,EACX,uBAAgD,EAChD,OAA6D,KAE7D,IAAIF,iBAAY,CAAC;AACf,IAAA,IAAI,EAAEC,SAAI,CAAC,CAAC,qBAAqB,CAAC,uBAAuB,CAAC,EAAE,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC;IAC3E,KAAK,EAAE,IAAIC,kBAAa,EAAE;AAC1B,IAAA,cAAc,EAAE;AACd,QAAA,KAAK,EAAE;AACL,YAAA,WAAW,EAAE,KAAK;AACnB,SAAA;AACF,KAAA;AACD,IAAA,GAAG,OAAO;AACX,CAAA;;;;;;;;;;;"}
|
package/testing.mjs
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { ApolloClient, InMemoryCache, from, createHttpLink } from '@apollo/client/core';
|
|
2
|
-
export * from '@apollo/client/core';
|
|
3
|
-
import { setContext } from '@apollo/client/link/context';
|
|
4
|
-
import { getClientCredentialsToken } from '@makerx/node-common';
|
|
5
|
-
|
|
6
|
-
const bearerTokenLink = (accessToken) => setContext((_, { headers }) => {
|
|
7
|
-
if (accessToken)
|
|
8
|
-
return {
|
|
9
|
-
headers: {
|
|
10
|
-
...headers,
|
|
11
|
-
authorization: `Bearer ${accessToken}`,
|
|
12
|
-
},
|
|
13
|
-
};
|
|
14
|
-
return {
|
|
15
|
-
headers,
|
|
16
|
-
};
|
|
17
|
-
});
|
|
18
|
-
const clientCredentialsLink = (clientCredentialsConfig) => {
|
|
19
|
-
let promise;
|
|
20
|
-
return setContext(async () => {
|
|
21
|
-
if (!promise)
|
|
22
|
-
promise = getClientCredentialsToken(clientCredentialsConfig);
|
|
23
|
-
let tokenResponse = await promise;
|
|
24
|
-
if (tokenResponse.isExpired) {
|
|
25
|
-
promise = getClientCredentialsToken(clientCredentialsConfig);
|
|
26
|
-
tokenResponse = await promise;
|
|
27
|
-
}
|
|
28
|
-
return {
|
|
29
|
-
headers: {
|
|
30
|
-
authorization: `Bearer ${tokenResponse.access_token}`,
|
|
31
|
-
},
|
|
32
|
-
};
|
|
33
|
-
});
|
|
34
|
-
};
|
|
35
|
-
const httpLink = (url) => createHttpLink({
|
|
36
|
-
uri: url,
|
|
37
|
-
});
|
|
38
|
-
const createTestClient = (url, accessToken, options) => new ApolloClient({
|
|
39
|
-
link: from([bearerTokenLink(accessToken), httpLink(url)]),
|
|
40
|
-
cache: new InMemoryCache(),
|
|
41
|
-
defaultOptions: {
|
|
42
|
-
query: {
|
|
43
|
-
errorPolicy: 'all',
|
|
44
|
-
},
|
|
45
|
-
},
|
|
46
|
-
...options,
|
|
47
|
-
});
|
|
48
|
-
const createTestClientWithClientCredentials = (url, clientCredentialsConfig, options) => new ApolloClient({
|
|
49
|
-
link: from([clientCredentialsLink(clientCredentialsConfig), httpLink(url)]),
|
|
50
|
-
cache: new InMemoryCache(),
|
|
51
|
-
defaultOptions: {
|
|
52
|
-
query: {
|
|
53
|
-
errorPolicy: 'all',
|
|
54
|
-
},
|
|
55
|
-
},
|
|
56
|
-
...options,
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
export { createTestClient, createTestClientWithClientCredentials };
|
|
60
|
-
//# sourceMappingURL=testing.mjs.map
|
package/testing.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"testing.mjs","sources":["../src/testing.ts"],"sourcesContent":["import type { NormalizedCacheObject } from '@apollo/client/core'\nimport { ApolloClient, createHttpLink, from, InMemoryCache } from '@apollo/client/core'\nimport type { ApolloClientOptions } from '@apollo/client/core/ApolloClient'\nimport { setContext } from '@apollo/client/link/context'\nimport type { AccessTokenResponse, ClientCredentialsConfig } from '@makerx/node-common'\nimport { getClientCredentialsToken } from '@makerx/node-common'\n\nexport * from '@apollo/client/core'\n\nconst bearerTokenLink = (accessToken?: string) =>\n setContext((_, { headers }) => {\n if (accessToken)\n return {\n headers: {\n ...headers,\n authorization: `Bearer ${accessToken}`,\n },\n }\n return {\n headers,\n }\n })\n\nconst clientCredentialsLink = (clientCredentialsConfig: ClientCredentialsConfig) => {\n let promise: Promise<AccessTokenResponse> | undefined\n return setContext(async () => {\n if (!promise) promise = getClientCredentialsToken(clientCredentialsConfig)\n let tokenResponse = await promise\n if (tokenResponse.isExpired) {\n promise = getClientCredentialsToken(clientCredentialsConfig)\n tokenResponse = await promise\n }\n return {\n headers: {\n authorization: `Bearer ${tokenResponse.access_token}`,\n },\n }\n })\n}\n\nconst httpLink = (url: string) =>\n createHttpLink({\n uri: url,\n })\n\nexport const createTestClient = (\n url: string,\n accessToken?: string,\n options?: Partial<ApolloClientOptions<NormalizedCacheObject>>,\n): ApolloClient<NormalizedCacheObject> =>\n new ApolloClient({\n link: from([bearerTokenLink(accessToken), httpLink(url)]),\n cache: new InMemoryCache(),\n defaultOptions: {\n query: {\n errorPolicy: 'all',\n },\n },\n ...options,\n })\n\nexport const createTestClientWithClientCredentials = (\n url: string,\n clientCredentialsConfig: ClientCredentialsConfig,\n options?: Partial<ApolloClientOptions<NormalizedCacheObject>>,\n): ApolloClient<NormalizedCacheObject> =>\n new ApolloClient({\n link: from([clientCredentialsLink(clientCredentialsConfig), httpLink(url)]),\n cache: new InMemoryCache(),\n defaultOptions: {\n query: {\n errorPolicy: 'all',\n },\n },\n ...options,\n })\n"],"names":[],"mappings":";;;;;AASA,MAAM,eAAe,GAAG,CAAC,WAAoB,KAC3C,UAAU,CAAC,CAAC,CAAC,EAAE,EAAE,OAAO,EAAE,KAAI;AAC5B,IAAA,IAAI,WAAW;QACb,OAAO;AACL,YAAA,OAAO,EAAE;AACP,gBAAA,GAAG,OAAO;gBACV,aAAa,EAAE,CAAA,OAAA,EAAU,WAAW,CAAA,CAAE;AACvC,aAAA;SACF;IACH,OAAO;QACL,OAAO;KACR;AACH,CAAC,CAAC;AAEJ,MAAM,qBAAqB,GAAG,CAAC,uBAAgD,KAAI;AACjF,IAAA,IAAI,OAAiD;AACrD,IAAA,OAAO,UAAU,CAAC,YAAW;AAC3B,QAAA,IAAI,CAAC,OAAO;AAAE,YAAA,OAAO,GAAG,yBAAyB,CAAC,uBAAuB,CAAC;AAC1E,QAAA,IAAI,aAAa,GAAG,MAAM,OAAO;AACjC,QAAA,IAAI,aAAa,CAAC,SAAS,EAAE;AAC3B,YAAA,OAAO,GAAG,yBAAyB,CAAC,uBAAuB,CAAC;YAC5D,aAAa,GAAG,MAAM,OAAO;QAC/B;QACA,OAAO;AACL,YAAA,OAAO,EAAE;AACP,gBAAA,aAAa,EAAE,CAAA,OAAA,EAAU,aAAa,CAAC,YAAY,CAAA,CAAE;AACtD,aAAA;SACF;AACH,IAAA,CAAC,CAAC;AACJ,CAAC;AAED,MAAM,QAAQ,GAAG,CAAC,GAAW,KAC3B,cAAc,CAAC;AACb,IAAA,GAAG,EAAE,GAAG;AACT,CAAA,CAAC;AAEG,MAAM,gBAAgB,GAAG,CAC9B,GAAW,EACX,WAAoB,EACpB,OAA6D,KAE7D,IAAI,YAA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