@ocap/gql 1.32.0 → 1.32.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/esm/http.d.mts +4 -2
- package/esm/http.mjs +9 -8
- package/lib/http.cjs +8 -7
- package/lib/http.d.cts +4 -2
- package/package.json +4 -4
package/esm/http.d.mts
CHANGED
|
@@ -24,10 +24,10 @@ interface RequestContext {
|
|
|
24
24
|
remoteAddress?: string;
|
|
25
25
|
};
|
|
26
26
|
}
|
|
27
|
-
declare const formatContext: (ctx?: RequestContext) => IResolverContext;
|
|
27
|
+
declare const formatContext: (ctx?: RequestContext, clientIpHeaders?: readonly string[]) => IResolverContext;
|
|
28
28
|
type Resolver = IResolver;
|
|
29
29
|
type ResolverFn = (args: unknown, ctx: unknown) => Promise<WrapResolverResult>;
|
|
30
|
-
declare const createResolvers: (resolver: Resolver) => Record<string, ResolverFn>;
|
|
30
|
+
declare const createResolvers: (resolver: Resolver, clientIpHeaders?: readonly string[]) => Record<string, ResolverFn>;
|
|
31
31
|
interface HttpHandlerOptions {
|
|
32
32
|
resolver?: IResolver;
|
|
33
33
|
onError?: (err: GraphQLError) => void;
|
|
@@ -60,6 +60,8 @@ interface HttpHandlerOptions {
|
|
|
60
60
|
* Ignored when `slowLogMs` is 0.
|
|
61
61
|
*/
|
|
62
62
|
sqlTiming?: boolean;
|
|
63
|
+
/** Optional isolated env map (e.g. `profile.http.adminEnv`) for `CHAIN_CLIENT_IP_HEADERS` resolution. */
|
|
64
|
+
env?: Record<string, string | undefined>;
|
|
63
65
|
}
|
|
64
66
|
/**
|
|
65
67
|
* Aliases of one root field that share an argument shape. The shape key lists the argument names present (sorted),
|
package/esm/http.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import schemaSource from "@ocap/schema";
|
|
2
|
-
import { resolveClientIp } from "@ocap/util/lib/client-ip";
|
|
2
|
+
import { getTrustedClientIpHeaders, resolveClientIp } from "@ocap/util/lib/client-ip";
|
|
3
3
|
import { GraphQLError, NoSchemaIntrospectionCustomRule, buildSchema, execute, getOperationAST, parse, validate } from "graphql";
|
|
4
4
|
import get from "lodash/get.js";
|
|
5
5
|
|
|
@@ -22,7 +22,7 @@ function readHeaderCaseInsensitive(headers, targetLower) {
|
|
|
22
22
|
if (Object.prototype.hasOwnProperty.call(headers, targetLower)) return headers[targetLower];
|
|
23
23
|
for (const [k, v] of Object.entries(headers)) if (k.toLowerCase() === targetLower) return v;
|
|
24
24
|
}
|
|
25
|
-
const formatContext = (ctx = {}) => {
|
|
25
|
+
const formatContext = (ctx = {}, clientIpHeaders) => {
|
|
26
26
|
const featureSwitch = {};
|
|
27
27
|
const gasStakeHeaders = {
|
|
28
28
|
token: get(ctx, "headers[x-gas-payer-sig]", ""),
|
|
@@ -30,7 +30,7 @@ const formatContext = (ctx = {}) => {
|
|
|
30
30
|
};
|
|
31
31
|
const request = {
|
|
32
32
|
userAgent: get(ctx, "headers.user-agent"),
|
|
33
|
-
ip: resolveClientIp(ctx) || "-",
|
|
33
|
+
ip: resolveClientIp(ctx, clientIpHeaders !== void 0 ? { headers: clientIpHeaders } : void 0) || "-",
|
|
34
34
|
id: get(ctx, "headers[x-request-id]") || get(ctx, "headers[X-Request-ID]") || "-"
|
|
35
35
|
};
|
|
36
36
|
const forwardedHop = readHeaderCaseInsensitive(ctx.headers, "x-ocap-forwarded-hop");
|
|
@@ -237,12 +237,12 @@ const resolvers = [
|
|
|
237
237
|
];
|
|
238
238
|
/** resolver fns whose rows reach the client only through the GraphQL `IndexedTransaction` type */
|
|
239
239
|
const TX_PROJECTION_FNS = new Set(["listTransactions", "listAssetTransactions"]);
|
|
240
|
-
const createResolvers = (resolver) => resolvers.reduce((acc, { fn, dataKey }) => {
|
|
240
|
+
const createResolvers = (resolver, clientIpHeaders) => resolvers.reduce((acc, { fn, dataKey }) => {
|
|
241
241
|
acc[fn] = async (args, ctx) => {
|
|
242
242
|
const res = await wrapResolver(fn, dataKey, () => {
|
|
243
243
|
const method = resolver[fn];
|
|
244
244
|
if (typeof method === "function") {
|
|
245
|
-
const resolverCtx = formatContext(ctx);
|
|
245
|
+
const resolverCtx = formatContext(ctx, clientIpHeaders);
|
|
246
246
|
if (TX_PROJECTION_FNS.has(fn)) resolverCtx.graphqlTxProjection = true;
|
|
247
247
|
return method.call(resolver, args, resolverCtx);
|
|
248
248
|
}
|
|
@@ -1022,9 +1022,9 @@ const parseEnvBoolean = (raw) => {
|
|
|
1022
1022
|
if (normalized === "1" || normalized === "true") return true;
|
|
1023
1023
|
if (normalized === "0" || normalized === "false") return false;
|
|
1024
1024
|
};
|
|
1025
|
-
const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, graphiql = parseEnvBoolean(process.env.GQL_GRAPHIQL ?? process.env.GRAPHQL_GRAPHIQL) ?? process.env.NODE_ENV !== "production", introspection = parseEnvBoolean(process.env.GQL_INTROSPECTION ?? process.env.GRAPHQL_INTROSPECTION) ?? process.env.NODE_ENV !== "production", showStack = process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test", maxQueryDepth = MAX_QUERY_DEPTH, maxTotalSelections = parseEnvPositiveInt(process.env.GQL_MAX_TOTAL_SELECTIONS) ?? MAX_TOTAL_SELECTIONS, maxAliases = parseEnvPositiveInt(process.env.GQL_MAX_ALIASES) ?? MAX_ALIASES, slowLogMs = parseEnvPositiveInt(process.env.GQL_SLOW_LOG_MS) ?? DEFAULT_SLOW_LOG_MS, onSlowRequest = defaultSlowRequestHandler, stallLogMs = parseEnvPositiveInt(process.env.GQL_STALL_LOG_MS) ?? slowLogMs, onEventLoopStall = defaultStallHandler, sqlTiming: sqlTimingEnabled = parseEnvBoolean(process.env.GQL_SQL_TIMING) ?? isCheapAsyncContextRuntime() }) => {
|
|
1025
|
+
const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, graphiql = parseEnvBoolean(process.env.GQL_GRAPHIQL ?? process.env.GRAPHQL_GRAPHIQL) ?? process.env.NODE_ENV !== "production", introspection = parseEnvBoolean(process.env.GQL_INTROSPECTION ?? process.env.GRAPHQL_INTROSPECTION) ?? process.env.NODE_ENV !== "production", showStack = process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test", maxQueryDepth = MAX_QUERY_DEPTH, maxTotalSelections = parseEnvPositiveInt(process.env.GQL_MAX_TOTAL_SELECTIONS) ?? MAX_TOTAL_SELECTIONS, maxAliases = parseEnvPositiveInt(process.env.GQL_MAX_ALIASES) ?? MAX_ALIASES, slowLogMs = parseEnvPositiveInt(process.env.GQL_SLOW_LOG_MS) ?? DEFAULT_SLOW_LOG_MS, onSlowRequest = defaultSlowRequestHandler, stallLogMs = parseEnvPositiveInt(process.env.GQL_STALL_LOG_MS) ?? slowLogMs, onEventLoopStall = defaultStallHandler, sqlTiming: sqlTimingEnabled = parseEnvBoolean(process.env.GQL_SQL_TIMING) ?? isCheapAsyncContextRuntime(), env }) => {
|
|
1026
1026
|
const schema = buildSchema(schemaSource);
|
|
1027
|
-
const rootValue = createResolvers(resolver);
|
|
1027
|
+
const rootValue = createResolvers(resolver, env !== void 0 ? getTrustedClientIpHeaders(env.CHAIN_CLIENT_IP_HEADERS) : void 0);
|
|
1028
1028
|
const stallMonitor = slowLogMs > 0 && stallLogMs > 0 && canProbeEventLoop() ? createStallMonitor(stallLogMs, onEventLoopStall, rootValue) : null;
|
|
1029
1029
|
const sqlTiming = slowLogMs > 0 && sqlTimingEnabled ? getSqlTimingContext() : null;
|
|
1030
1030
|
const formatGraphQLError = (err) => {
|
|
@@ -1291,6 +1291,7 @@ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, gra
|
|
|
1291
1291
|
/** Express / connect adapter. Behavior is unchanged from the pre-split `createHttpHandler`. */
|
|
1292
1292
|
const createHttpHandler = (options) => {
|
|
1293
1293
|
const engine = createGraphQLEngine(options);
|
|
1294
|
+
const clientIpHeaders = options.env !== void 0 ? getTrustedClientIpHeaders(options.env.CHAIN_CLIENT_IP_HEADERS) : void 0;
|
|
1294
1295
|
return async (req, res) => {
|
|
1295
1296
|
const result = await engine({
|
|
1296
1297
|
receivedAt: nowMs(),
|
|
@@ -1299,7 +1300,7 @@ const createHttpHandler = (options) => {
|
|
|
1299
1300
|
headers: req.headers,
|
|
1300
1301
|
body: req.body,
|
|
1301
1302
|
readBody: (limit) => readRawBody(req, limit),
|
|
1302
|
-
clientIp: resolveClientIp(req) || void 0
|
|
1303
|
+
clientIp: resolveClientIp(req, clientIpHeaders !== void 0 ? { headers: clientIpHeaders } : void 0) || void 0
|
|
1303
1304
|
}, req);
|
|
1304
1305
|
res.statusCode = result.status;
|
|
1305
1306
|
for (const [name, value] of Object.entries(result.headers)) res.setHeader(name, value);
|
package/lib/http.cjs
CHANGED
|
@@ -25,7 +25,7 @@ function readHeaderCaseInsensitive(headers, targetLower) {
|
|
|
25
25
|
if (Object.prototype.hasOwnProperty.call(headers, targetLower)) return headers[targetLower];
|
|
26
26
|
for (const [k, v] of Object.entries(headers)) if (k.toLowerCase() === targetLower) return v;
|
|
27
27
|
}
|
|
28
|
-
const formatContext = (ctx = {}) => {
|
|
28
|
+
const formatContext = (ctx = {}, clientIpHeaders) => {
|
|
29
29
|
const featureSwitch = {};
|
|
30
30
|
const gasStakeHeaders = {
|
|
31
31
|
token: (0, lodash_get.default)(ctx, "headers[x-gas-payer-sig]", ""),
|
|
@@ -33,7 +33,7 @@ const formatContext = (ctx = {}) => {
|
|
|
33
33
|
};
|
|
34
34
|
const request = {
|
|
35
35
|
userAgent: (0, lodash_get.default)(ctx, "headers.user-agent"),
|
|
36
|
-
ip: (0, _ocap_util_lib_client_ip.resolveClientIp)(ctx) || "-",
|
|
36
|
+
ip: (0, _ocap_util_lib_client_ip.resolveClientIp)(ctx, clientIpHeaders !== void 0 ? { headers: clientIpHeaders } : void 0) || "-",
|
|
37
37
|
id: (0, lodash_get.default)(ctx, "headers[x-request-id]") || (0, lodash_get.default)(ctx, "headers[X-Request-ID]") || "-"
|
|
38
38
|
};
|
|
39
39
|
const forwardedHop = readHeaderCaseInsensitive(ctx.headers, "x-ocap-forwarded-hop");
|
|
@@ -240,12 +240,12 @@ const resolvers = [
|
|
|
240
240
|
];
|
|
241
241
|
/** resolver fns whose rows reach the client only through the GraphQL `IndexedTransaction` type */
|
|
242
242
|
const TX_PROJECTION_FNS = new Set(["listTransactions", "listAssetTransactions"]);
|
|
243
|
-
const createResolvers = (resolver) => resolvers.reduce((acc, { fn, dataKey }) => {
|
|
243
|
+
const createResolvers = (resolver, clientIpHeaders) => resolvers.reduce((acc, { fn, dataKey }) => {
|
|
244
244
|
acc[fn] = async (args, ctx) => {
|
|
245
245
|
const res = await wrapResolver(fn, dataKey, () => {
|
|
246
246
|
const method = resolver[fn];
|
|
247
247
|
if (typeof method === "function") {
|
|
248
|
-
const resolverCtx = formatContext(ctx);
|
|
248
|
+
const resolverCtx = formatContext(ctx, clientIpHeaders);
|
|
249
249
|
if (TX_PROJECTION_FNS.has(fn)) resolverCtx.graphqlTxProjection = true;
|
|
250
250
|
return method.call(resolver, args, resolverCtx);
|
|
251
251
|
}
|
|
@@ -1025,9 +1025,9 @@ const parseEnvBoolean = (raw) => {
|
|
|
1025
1025
|
if (normalized === "1" || normalized === "true") return true;
|
|
1026
1026
|
if (normalized === "0" || normalized === "false") return false;
|
|
1027
1027
|
};
|
|
1028
|
-
const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, graphiql = parseEnvBoolean(process.env.GQL_GRAPHIQL ?? process.env.GRAPHQL_GRAPHIQL) ?? process.env.NODE_ENV !== "production", introspection = parseEnvBoolean(process.env.GQL_INTROSPECTION ?? process.env.GRAPHQL_INTROSPECTION) ?? process.env.NODE_ENV !== "production", showStack = process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test", maxQueryDepth = MAX_QUERY_DEPTH, maxTotalSelections = parseEnvPositiveInt(process.env.GQL_MAX_TOTAL_SELECTIONS) ?? MAX_TOTAL_SELECTIONS, maxAliases = parseEnvPositiveInt(process.env.GQL_MAX_ALIASES) ?? MAX_ALIASES, slowLogMs = parseEnvPositiveInt(process.env.GQL_SLOW_LOG_MS) ?? DEFAULT_SLOW_LOG_MS, onSlowRequest = defaultSlowRequestHandler, stallLogMs = parseEnvPositiveInt(process.env.GQL_STALL_LOG_MS) ?? slowLogMs, onEventLoopStall = defaultStallHandler, sqlTiming: sqlTimingEnabled = parseEnvBoolean(process.env.GQL_SQL_TIMING) ?? isCheapAsyncContextRuntime() }) => {
|
|
1028
|
+
const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, graphiql = parseEnvBoolean(process.env.GQL_GRAPHIQL ?? process.env.GRAPHQL_GRAPHIQL) ?? process.env.NODE_ENV !== "production", introspection = parseEnvBoolean(process.env.GQL_INTROSPECTION ?? process.env.GRAPHQL_INTROSPECTION) ?? process.env.NODE_ENV !== "production", showStack = process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test", maxQueryDepth = MAX_QUERY_DEPTH, maxTotalSelections = parseEnvPositiveInt(process.env.GQL_MAX_TOTAL_SELECTIONS) ?? MAX_TOTAL_SELECTIONS, maxAliases = parseEnvPositiveInt(process.env.GQL_MAX_ALIASES) ?? MAX_ALIASES, slowLogMs = parseEnvPositiveInt(process.env.GQL_SLOW_LOG_MS) ?? DEFAULT_SLOW_LOG_MS, onSlowRequest = defaultSlowRequestHandler, stallLogMs = parseEnvPositiveInt(process.env.GQL_STALL_LOG_MS) ?? slowLogMs, onEventLoopStall = defaultStallHandler, sqlTiming: sqlTimingEnabled = parseEnvBoolean(process.env.GQL_SQL_TIMING) ?? isCheapAsyncContextRuntime(), env }) => {
|
|
1029
1029
|
const schema = (0, graphql.buildSchema)(_ocap_schema.default);
|
|
1030
|
-
const rootValue = createResolvers(resolver);
|
|
1030
|
+
const rootValue = createResolvers(resolver, env !== void 0 ? (0, _ocap_util_lib_client_ip.getTrustedClientIpHeaders)(env.CHAIN_CLIENT_IP_HEADERS) : void 0);
|
|
1031
1031
|
const stallMonitor = slowLogMs > 0 && stallLogMs > 0 && canProbeEventLoop() ? createStallMonitor(stallLogMs, onEventLoopStall, rootValue) : null;
|
|
1032
1032
|
const sqlTiming = slowLogMs > 0 && sqlTimingEnabled ? getSqlTimingContext() : null;
|
|
1033
1033
|
const formatGraphQLError = (err) => {
|
|
@@ -1294,6 +1294,7 @@ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, gra
|
|
|
1294
1294
|
/** Express / connect adapter. Behavior is unchanged from the pre-split `createHttpHandler`. */
|
|
1295
1295
|
const createHttpHandler = (options) => {
|
|
1296
1296
|
const engine = createGraphQLEngine(options);
|
|
1297
|
+
const clientIpHeaders = options.env !== void 0 ? (0, _ocap_util_lib_client_ip.getTrustedClientIpHeaders)(options.env.CHAIN_CLIENT_IP_HEADERS) : void 0;
|
|
1297
1298
|
return async (req, res) => {
|
|
1298
1299
|
const result = await engine({
|
|
1299
1300
|
receivedAt: nowMs(),
|
|
@@ -1302,7 +1303,7 @@ const createHttpHandler = (options) => {
|
|
|
1302
1303
|
headers: req.headers,
|
|
1303
1304
|
body: req.body,
|
|
1304
1305
|
readBody: (limit) => readRawBody(req, limit),
|
|
1305
|
-
clientIp: (0, _ocap_util_lib_client_ip.resolveClientIp)(req) || void 0
|
|
1306
|
+
clientIp: (0, _ocap_util_lib_client_ip.resolveClientIp)(req, clientIpHeaders !== void 0 ? { headers: clientIpHeaders } : void 0) || void 0
|
|
1306
1307
|
}, req);
|
|
1307
1308
|
res.statusCode = result.status;
|
|
1308
1309
|
for (const [name, value] of Object.entries(result.headers)) res.setHeader(name, value);
|
package/lib/http.d.cts
CHANGED
|
@@ -24,10 +24,10 @@ interface RequestContext {
|
|
|
24
24
|
remoteAddress?: string;
|
|
25
25
|
};
|
|
26
26
|
}
|
|
27
|
-
declare const formatContext: (ctx?: RequestContext) => IResolverContext;
|
|
27
|
+
declare const formatContext: (ctx?: RequestContext, clientIpHeaders?: readonly string[]) => IResolverContext;
|
|
28
28
|
type Resolver = IResolver;
|
|
29
29
|
type ResolverFn = (args: unknown, ctx: unknown) => Promise<WrapResolverResult>;
|
|
30
|
-
declare const createResolvers: (resolver: Resolver) => Record<string, ResolverFn>;
|
|
30
|
+
declare const createResolvers: (resolver: Resolver, clientIpHeaders?: readonly string[]) => Record<string, ResolverFn>;
|
|
31
31
|
interface HttpHandlerOptions {
|
|
32
32
|
resolver?: IResolver;
|
|
33
33
|
onError?: (err: GraphQLError) => void;
|
|
@@ -60,6 +60,8 @@ interface HttpHandlerOptions {
|
|
|
60
60
|
* Ignored when `slowLogMs` is 0.
|
|
61
61
|
*/
|
|
62
62
|
sqlTiming?: boolean;
|
|
63
|
+
/** Optional isolated env map (e.g. `profile.http.adminEnv`) for `CHAIN_CLIENT_IP_HEADERS` resolution. */
|
|
64
|
+
env?: Record<string, string | undefined>;
|
|
63
65
|
}
|
|
64
66
|
/**
|
|
65
67
|
* Aliases of one root field that share an argument shape. The shape key lists the argument names present (sorted),
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.32.
|
|
6
|
+
"version": "1.32.2",
|
|
7
7
|
"description": "Resolver middleware for ocap adapters",
|
|
8
8
|
"type": "module",
|
|
9
9
|
"main": "./lib/index.cjs",
|
|
@@ -51,9 +51,9 @@
|
|
|
51
51
|
],
|
|
52
52
|
"license": "MIT",
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@arcblock/ws": "1.32.
|
|
55
|
-
"@ocap/schema": "1.32.
|
|
56
|
-
"@ocap/util": "1.32.
|
|
54
|
+
"@arcblock/ws": "1.32.2",
|
|
55
|
+
"@ocap/schema": "1.32.2",
|
|
56
|
+
"@ocap/util": "1.32.2",
|
|
57
57
|
"graphql": "^16.13.2",
|
|
58
58
|
"lodash": "^4.17.23"
|
|
59
59
|
},
|