@ocap/gql 1.31.9 → 1.32.1
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 +5 -0
- package/esm/http.d.mts +14 -1
- package/esm/http.mjs +71 -20
- package/lib/http.cjs +71 -20
- package/lib/http.d.cts +14 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -54,6 +54,11 @@ responses are byte-identical with the logs on or off.
|
|
|
54
54
|
time of all aliases / of the slowest alias, and statement count); `rows` (total length of the returned lists, e.g.
|
|
55
55
|
`transactions`); `shapes` (aliases bucketed by argument shape, at most 8 buckets plus `other`, each with `count`,
|
|
56
56
|
`rows`, `sqlMs`).
|
|
57
|
+
- `clientIp` / `host` / `cfRay` / `userAgent` (#1922): where the request came from, so a slow line can be matched
|
|
58
|
+
with the access log. `clientIp` follows `@ocap/util/lib/client-ip`: the headers in `CHAIN_CLIENT_IP_HEADERS`
|
|
59
|
+
(default `x-real-ip`; `cf-connecting-ip` for a Cloudflare Tunnel node), read only when the TCP peer is a loopback
|
|
60
|
+
address, else the socket address. `createFetchHandler` (Workers) always uses `cf-connecting-ip`, then `x-real-ip`. `host` is
|
|
61
|
+
capped at 160 chars, `cfRay` at 64; absent headers are omitted. Stall entries carry `clientIp` and `userAgent`.
|
|
57
62
|
- SQL time is measured where the statements run (#1891): `@ocap/indexdb-sqlite` and `@ocap/statedb-sqlite` time the
|
|
58
63
|
synchronous part of every Kysely `executeQuery` (better-sqlite3 / bun:sqlite run the whole statement there) and add
|
|
59
64
|
it to the alias that issued it, found through an `AsyncLocalStorage` the handler runs each root field call in
|
package/esm/http.d.mts
CHANGED
|
@@ -14,12 +14,15 @@ interface WrapResolverResult {
|
|
|
14
14
|
}
|
|
15
15
|
declare const wrapResolver: (_name: string, keys: string | string[], callback: () => Promise<ResolverResult | null> | ResolverResult | null | string) => Promise<WrapResolverResult>;
|
|
16
16
|
interface RequestContext {
|
|
17
|
-
headers?: Record<string, string>;
|
|
17
|
+
headers?: Record<string, string | string[] | undefined>;
|
|
18
18
|
ip?: string;
|
|
19
19
|
_remoteAddress?: string;
|
|
20
20
|
connection?: {
|
|
21
21
|
remoteAddress?: string;
|
|
22
22
|
};
|
|
23
|
+
socket?: {
|
|
24
|
+
remoteAddress?: string;
|
|
25
|
+
};
|
|
23
26
|
}
|
|
24
27
|
declare const formatContext: (ctx?: RequestContext) => IResolverContext;
|
|
25
28
|
type Resolver = IResolver;
|
|
@@ -114,6 +117,8 @@ interface StallInFlightEntry {
|
|
|
114
117
|
/** Root field name -> number of aliases selecting it; names the schema does not resolve are summed as `unknown`. */
|
|
115
118
|
fields?: Record<string, number>;
|
|
116
119
|
userAgent?: string;
|
|
120
|
+
/** Client address as resolved by `@ocap/util/lib/client-ip` (#1922). */
|
|
121
|
+
clientIp?: string;
|
|
117
122
|
}
|
|
118
123
|
/** `[gql] event loop stall` report: the probe timer fired `maxMs` late in the window starting at `windowStart`. */
|
|
119
124
|
interface EventLoopStallReport {
|
|
@@ -158,6 +163,12 @@ interface SlowRequestReport {
|
|
|
158
163
|
sqlQueries?: number;
|
|
159
164
|
fields: Record<string, SlowRequestFieldStat>;
|
|
160
165
|
userAgent?: string;
|
|
166
|
+
/** Client address as resolved by `@ocap/util/lib/client-ip` (#1922); joins the line with the access log. */
|
|
167
|
+
clientIp?: string;
|
|
168
|
+
/** `Host` header, at most 160 chars. */
|
|
169
|
+
host?: string;
|
|
170
|
+
/** Cloudflare `cf-ray` request id, at most 64 chars; absent when the request did not pass Cloudflare. */
|
|
171
|
+
cfRay?: string;
|
|
161
172
|
}
|
|
162
173
|
/** Must equal `SQL_TIMING_KEY` of `@ocap/util/lib/sql-timing` (a test checks it); kept here so gql has no util dep. */
|
|
163
174
|
declare const SQL_TIMING_KEY: unique symbol;
|
|
@@ -195,6 +206,8 @@ interface GqlRequestLike {
|
|
|
195
206
|
readBody: (limit: number) => Promise<string>;
|
|
196
207
|
/** `performance.now()` when the adapter received the request; defaults to the engine's own start. */
|
|
197
208
|
receivedAt?: number;
|
|
209
|
+
/** Client address resolved by the adapter (`@ocap/util/lib/client-ip`), reported in the slow / stall lines. */
|
|
210
|
+
clientIp?: string;
|
|
198
211
|
}
|
|
199
212
|
/** Transport-agnostic response produced by the engine. */
|
|
200
213
|
interface GqlResponseLike {
|
package/esm/http.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import schemaSource from "@ocap/schema";
|
|
2
|
+
import { resolveClientIp } from "@ocap/util/lib/client-ip";
|
|
2
3
|
import { GraphQLError, NoSchemaIntrospectionCustomRule, buildSchema, execute, getOperationAST, parse, validate } from "graphql";
|
|
3
4
|
import get from "lodash/get.js";
|
|
4
5
|
|
|
@@ -7,7 +8,7 @@ const wrapResolver = async (_name, keys, callback) => {
|
|
|
7
8
|
const result = await callback();
|
|
8
9
|
const resultsMap = {};
|
|
9
10
|
if (Array.isArray(keys)) keys.forEach((key) => {
|
|
10
|
-
resultsMap[key] = result[key];
|
|
11
|
+
resultsMap[key] = result ? result[key] : null;
|
|
11
12
|
});
|
|
12
13
|
else resultsMap[keys] = result;
|
|
13
14
|
return {
|
|
@@ -16,18 +17,28 @@ const wrapResolver = async (_name, keys, callback) => {
|
|
|
16
17
|
...resultsMap
|
|
17
18
|
};
|
|
18
19
|
};
|
|
20
|
+
function readHeaderCaseInsensitive(headers, targetLower) {
|
|
21
|
+
if (!headers) return void 0;
|
|
22
|
+
if (Object.prototype.hasOwnProperty.call(headers, targetLower)) return headers[targetLower];
|
|
23
|
+
for (const [k, v] of Object.entries(headers)) if (k.toLowerCase() === targetLower) return v;
|
|
24
|
+
}
|
|
19
25
|
const formatContext = (ctx = {}) => {
|
|
26
|
+
const featureSwitch = {};
|
|
27
|
+
const gasStakeHeaders = {
|
|
28
|
+
token: get(ctx, "headers[x-gas-payer-sig]", ""),
|
|
29
|
+
pk: get(ctx, "headers[x-gas-payer-pk]", "")
|
|
30
|
+
};
|
|
31
|
+
const request = {
|
|
32
|
+
userAgent: get(ctx, "headers.user-agent"),
|
|
33
|
+
ip: resolveClientIp(ctx) || "-",
|
|
34
|
+
id: get(ctx, "headers[x-request-id]") || get(ctx, "headers[X-Request-ID]") || "-"
|
|
35
|
+
};
|
|
36
|
+
const forwardedHop = readHeaderCaseInsensitive(ctx.headers, "x-ocap-forwarded-hop");
|
|
20
37
|
return {
|
|
21
|
-
featureSwitch
|
|
22
|
-
gasStakeHeaders
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
},
|
|
26
|
-
request: {
|
|
27
|
-
userAgent: get(ctx, "headers.user-agent"),
|
|
28
|
-
ip: get(ctx, "headers.x-real-ip") || ctx.ip || ctx._remoteAddress || ctx.connection?.remoteAddress || "-",
|
|
29
|
-
id: get(ctx, "headers[x-request-id]") || get(ctx, "headers[X-Request-ID]") || "-"
|
|
30
|
-
}
|
|
38
|
+
featureSwitch,
|
|
39
|
+
gasStakeHeaders,
|
|
40
|
+
request,
|
|
41
|
+
...forwardedHop !== void 0 ? { forwardedHop } : {}
|
|
31
42
|
};
|
|
32
43
|
};
|
|
33
44
|
const resolvers = [
|
|
@@ -139,9 +150,17 @@ const resolvers = [
|
|
|
139
150
|
fn: "getLedgerEntry",
|
|
140
151
|
dataKey: "entry"
|
|
141
152
|
},
|
|
153
|
+
{
|
|
154
|
+
fn: "getLedgerInfo",
|
|
155
|
+
dataKey: "info"
|
|
156
|
+
},
|
|
142
157
|
{
|
|
143
158
|
fn: "getMerkleProof",
|
|
144
|
-
dataKey: [
|
|
159
|
+
dataKey: [
|
|
160
|
+
"proof",
|
|
161
|
+
"checkpoint",
|
|
162
|
+
"entryHashV2From"
|
|
163
|
+
]
|
|
145
164
|
},
|
|
146
165
|
{
|
|
147
166
|
fn: "getTokenDistribution",
|
|
@@ -331,6 +350,15 @@ const collectRootFieldCounts = (documentAST, operationName, maxFragmentDepth = M
|
|
|
331
350
|
return names;
|
|
332
351
|
};
|
|
333
352
|
const headerValue = (v) => Array.isArray(v) ? v[0] : v;
|
|
353
|
+
/** `host` / `cfRay` of the slow-request line; header values are client-controlled, so they are length-capped. */
|
|
354
|
+
const sourceHeaders = (headers) => {
|
|
355
|
+
const host = headerValue(headers.host);
|
|
356
|
+
const cfRay = headerValue(headers["cf-ray"]);
|
|
357
|
+
return {
|
|
358
|
+
...host ? { host: host.slice(0, 160) } : {},
|
|
359
|
+
...cfRay ? { cfRay: cfRay.slice(0, 64) } : {}
|
|
360
|
+
};
|
|
361
|
+
};
|
|
334
362
|
const MAX_SHAPES_PER_FIELD = 8;
|
|
335
363
|
const MAX_STALL_ENTRIES = 20;
|
|
336
364
|
const MAX_RECENT_DONE = 64;
|
|
@@ -414,6 +442,7 @@ const createStallMonitor = (requestedMs, report, rootValue) => {
|
|
|
414
442
|
}
|
|
415
443
|
const ua = headerValue(rec.headers["user-agent"]);
|
|
416
444
|
if (ua) entry.userAgent = ua.slice(0, 160);
|
|
445
|
+
if (rec.clientIp) entry.clientIp = rec.clientIp;
|
|
417
446
|
return entry;
|
|
418
447
|
};
|
|
419
448
|
const tick = () => {
|
|
@@ -1157,12 +1186,27 @@ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, gra
|
|
|
1157
1186
|
} catch (contextError) {
|
|
1158
1187
|
return sendJSON(400, { errors: [formatAnyError(contextError)] });
|
|
1159
1188
|
}
|
|
1160
|
-
const
|
|
1189
|
+
const responseData = aliasMap && result.data != null ? restoreNumericAliasesInData(result.data, aliasMap) : result.data;
|
|
1190
|
+
const formattedResult = { data: responseData };
|
|
1161
1191
|
if (result.errors && result.errors.length > 0) formattedResult.errors = result.errors.map(formatError);
|
|
1162
1192
|
const status = result.data == null ? 500 : 200;
|
|
1163
|
-
|
|
1193
|
+
const extraHeaders = {};
|
|
1194
|
+
const forwardedHop = readHeaderCaseInsensitive(req.headers, "x-ocap-forwarded-hop");
|
|
1195
|
+
const sentTxHash = responseData?.sendTx?.hash;
|
|
1196
|
+
if (forwardedHop !== void 0 && typeof sentTxHash === "string" && sentTxHash && (!result.errors || result.errors.length === 0)) {
|
|
1197
|
+
const signFn = resolver.signForwardedSendTxReceipt;
|
|
1198
|
+
if (typeof signFn === "function") {
|
|
1199
|
+
const signedReceipt = await signFn.call(resolver, sentTxHash);
|
|
1200
|
+
if (signedReceipt?.signature && signedReceipt?.proposer) {
|
|
1201
|
+
extraHeaders["X-Ocap-Forward-Receipt-Sig"] = signedReceipt.signature;
|
|
1202
|
+
extraHeaders["X-Ocap-Forward-Proposer"] = signedReceipt.proposer;
|
|
1203
|
+
}
|
|
1204
|
+
if (signedReceipt?.commitProof) extraHeaders["X-Ocap-Forward-Commit-Proof"] = signedReceipt.commitProof;
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
if (slowLogMs <= 0) return sendJSON(status, formattedResult, extraHeaders);
|
|
1164
1208
|
const executedAt = nowMs();
|
|
1165
|
-
const response = sendJSON(status, formattedResult);
|
|
1209
|
+
const response = sendJSON(status, formattedResult, extraHeaders);
|
|
1166
1210
|
const finishedAt = nowMs();
|
|
1167
1211
|
const totalMs = finishedAt - startedAt;
|
|
1168
1212
|
const queuedMs = rec ? Math.max(0, startedAt - rec.receivedAt) : 0;
|
|
@@ -1222,7 +1266,9 @@ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, gra
|
|
|
1222
1266
|
sqlQueries
|
|
1223
1267
|
} : {},
|
|
1224
1268
|
fields,
|
|
1225
|
-
userAgent: headerValue(req.headers["user-agent"])?.slice(0, 160)
|
|
1269
|
+
userAgent: headerValue(req.headers["user-agent"])?.slice(0, 160),
|
|
1270
|
+
...req.clientIp ? { clientIp: req.clientIp } : {},
|
|
1271
|
+
...sourceHeaders(req.headers)
|
|
1226
1272
|
});
|
|
1227
1273
|
} catch {}
|
|
1228
1274
|
return response;
|
|
@@ -1231,7 +1277,8 @@ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, gra
|
|
|
1231
1277
|
return async (req, contextValue) => {
|
|
1232
1278
|
const rec = {
|
|
1233
1279
|
receivedAt: req.receivedAt ?? nowMs(),
|
|
1234
|
-
headers: req.headers
|
|
1280
|
+
headers: req.headers,
|
|
1281
|
+
clientIp: req.clientIp
|
|
1235
1282
|
};
|
|
1236
1283
|
stallMonitor?.begin(rec);
|
|
1237
1284
|
try {
|
|
@@ -1251,7 +1298,8 @@ const createHttpHandler = (options) => {
|
|
|
1251
1298
|
url: req.url,
|
|
1252
1299
|
headers: req.headers,
|
|
1253
1300
|
body: req.body,
|
|
1254
|
-
readBody: (limit) => readRawBody(req, limit)
|
|
1301
|
+
readBody: (limit) => readRawBody(req, limit),
|
|
1302
|
+
clientIp: resolveClientIp(req) || void 0
|
|
1255
1303
|
}, req);
|
|
1256
1304
|
res.statusCode = result.status;
|
|
1257
1305
|
for (const [name, value] of Object.entries(result.headers)) res.setHeader(name, value);
|
|
@@ -1283,6 +1331,7 @@ const readFetchBody = async (request, limit) => {
|
|
|
1283
1331
|
}
|
|
1284
1332
|
return text + decoder.decode();
|
|
1285
1333
|
};
|
|
1334
|
+
const FETCH_CLIENT_IP_HEADERS = ["cf-connecting-ip", "x-real-ip"];
|
|
1286
1335
|
/**
|
|
1287
1336
|
* Fetch-native adapter (Cloudflare Workers / Durable Objects, Deno, Bun): `(Request) => Promise<Response>`.
|
|
1288
1337
|
* Same engine, same guards (depth limit, batch guard, introspection off in production, 100kb body cap) as
|
|
@@ -1297,15 +1346,17 @@ const createFetchHandler = (options) => {
|
|
|
1297
1346
|
headers[key.toLowerCase()] = value;
|
|
1298
1347
|
});
|
|
1299
1348
|
const url = new URL(request.url);
|
|
1349
|
+
const clientIp = resolveClientIp({ headers }, { headers: FETCH_CLIENT_IP_HEADERS }) || void 0;
|
|
1300
1350
|
const result = await engine({
|
|
1301
1351
|
receivedAt,
|
|
1302
1352
|
method: request.method,
|
|
1303
1353
|
url: `${url.pathname}${url.search}`,
|
|
1304
1354
|
headers,
|
|
1305
|
-
readBody: (limit) => readFetchBody(request, limit)
|
|
1355
|
+
readBody: (limit) => readFetchBody(request, limit),
|
|
1356
|
+
clientIp
|
|
1306
1357
|
}, {
|
|
1307
1358
|
headers,
|
|
1308
|
-
ip:
|
|
1359
|
+
ip: clientIp
|
|
1309
1360
|
});
|
|
1310
1361
|
return new Response(result.body, {
|
|
1311
1362
|
status: result.status,
|
package/lib/http.cjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
2
|
let _ocap_schema = require("@ocap/schema");
|
|
3
3
|
_ocap_schema = require_rolldown_runtime.__toESM(_ocap_schema);
|
|
4
|
+
let _ocap_util_lib_client_ip = require("@ocap/util/lib/client-ip");
|
|
4
5
|
let graphql = require("graphql");
|
|
5
6
|
let lodash_get = require("lodash/get");
|
|
6
7
|
lodash_get = require_rolldown_runtime.__toESM(lodash_get);
|
|
@@ -10,7 +11,7 @@ const wrapResolver = async (_name, keys, callback) => {
|
|
|
10
11
|
const result = await callback();
|
|
11
12
|
const resultsMap = {};
|
|
12
13
|
if (Array.isArray(keys)) keys.forEach((key) => {
|
|
13
|
-
resultsMap[key] = result[key];
|
|
14
|
+
resultsMap[key] = result ? result[key] : null;
|
|
14
15
|
});
|
|
15
16
|
else resultsMap[keys] = result;
|
|
16
17
|
return {
|
|
@@ -19,18 +20,28 @@ const wrapResolver = async (_name, keys, callback) => {
|
|
|
19
20
|
...resultsMap
|
|
20
21
|
};
|
|
21
22
|
};
|
|
23
|
+
function readHeaderCaseInsensitive(headers, targetLower) {
|
|
24
|
+
if (!headers) return void 0;
|
|
25
|
+
if (Object.prototype.hasOwnProperty.call(headers, targetLower)) return headers[targetLower];
|
|
26
|
+
for (const [k, v] of Object.entries(headers)) if (k.toLowerCase() === targetLower) return v;
|
|
27
|
+
}
|
|
22
28
|
const formatContext = (ctx = {}) => {
|
|
29
|
+
const featureSwitch = {};
|
|
30
|
+
const gasStakeHeaders = {
|
|
31
|
+
token: (0, lodash_get.default)(ctx, "headers[x-gas-payer-sig]", ""),
|
|
32
|
+
pk: (0, lodash_get.default)(ctx, "headers[x-gas-payer-pk]", "")
|
|
33
|
+
};
|
|
34
|
+
const request = {
|
|
35
|
+
userAgent: (0, lodash_get.default)(ctx, "headers.user-agent"),
|
|
36
|
+
ip: (0, _ocap_util_lib_client_ip.resolveClientIp)(ctx) || "-",
|
|
37
|
+
id: (0, lodash_get.default)(ctx, "headers[x-request-id]") || (0, lodash_get.default)(ctx, "headers[X-Request-ID]") || "-"
|
|
38
|
+
};
|
|
39
|
+
const forwardedHop = readHeaderCaseInsensitive(ctx.headers, "x-ocap-forwarded-hop");
|
|
23
40
|
return {
|
|
24
|
-
featureSwitch
|
|
25
|
-
gasStakeHeaders
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
},
|
|
29
|
-
request: {
|
|
30
|
-
userAgent: (0, lodash_get.default)(ctx, "headers.user-agent"),
|
|
31
|
-
ip: (0, lodash_get.default)(ctx, "headers.x-real-ip") || ctx.ip || ctx._remoteAddress || ctx.connection?.remoteAddress || "-",
|
|
32
|
-
id: (0, lodash_get.default)(ctx, "headers[x-request-id]") || (0, lodash_get.default)(ctx, "headers[X-Request-ID]") || "-"
|
|
33
|
-
}
|
|
41
|
+
featureSwitch,
|
|
42
|
+
gasStakeHeaders,
|
|
43
|
+
request,
|
|
44
|
+
...forwardedHop !== void 0 ? { forwardedHop } : {}
|
|
34
45
|
};
|
|
35
46
|
};
|
|
36
47
|
const resolvers = [
|
|
@@ -142,9 +153,17 @@ const resolvers = [
|
|
|
142
153
|
fn: "getLedgerEntry",
|
|
143
154
|
dataKey: "entry"
|
|
144
155
|
},
|
|
156
|
+
{
|
|
157
|
+
fn: "getLedgerInfo",
|
|
158
|
+
dataKey: "info"
|
|
159
|
+
},
|
|
145
160
|
{
|
|
146
161
|
fn: "getMerkleProof",
|
|
147
|
-
dataKey: [
|
|
162
|
+
dataKey: [
|
|
163
|
+
"proof",
|
|
164
|
+
"checkpoint",
|
|
165
|
+
"entryHashV2From"
|
|
166
|
+
]
|
|
148
167
|
},
|
|
149
168
|
{
|
|
150
169
|
fn: "getTokenDistribution",
|
|
@@ -334,6 +353,15 @@ const collectRootFieldCounts = (documentAST, operationName, maxFragmentDepth = M
|
|
|
334
353
|
return names;
|
|
335
354
|
};
|
|
336
355
|
const headerValue = (v) => Array.isArray(v) ? v[0] : v;
|
|
356
|
+
/** `host` / `cfRay` of the slow-request line; header values are client-controlled, so they are length-capped. */
|
|
357
|
+
const sourceHeaders = (headers) => {
|
|
358
|
+
const host = headerValue(headers.host);
|
|
359
|
+
const cfRay = headerValue(headers["cf-ray"]);
|
|
360
|
+
return {
|
|
361
|
+
...host ? { host: host.slice(0, 160) } : {},
|
|
362
|
+
...cfRay ? { cfRay: cfRay.slice(0, 64) } : {}
|
|
363
|
+
};
|
|
364
|
+
};
|
|
337
365
|
const MAX_SHAPES_PER_FIELD = 8;
|
|
338
366
|
const MAX_STALL_ENTRIES = 20;
|
|
339
367
|
const MAX_RECENT_DONE = 64;
|
|
@@ -417,6 +445,7 @@ const createStallMonitor = (requestedMs, report, rootValue) => {
|
|
|
417
445
|
}
|
|
418
446
|
const ua = headerValue(rec.headers["user-agent"]);
|
|
419
447
|
if (ua) entry.userAgent = ua.slice(0, 160);
|
|
448
|
+
if (rec.clientIp) entry.clientIp = rec.clientIp;
|
|
420
449
|
return entry;
|
|
421
450
|
};
|
|
422
451
|
const tick = () => {
|
|
@@ -1160,12 +1189,27 @@ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, gra
|
|
|
1160
1189
|
} catch (contextError) {
|
|
1161
1190
|
return sendJSON(400, { errors: [formatAnyError(contextError)] });
|
|
1162
1191
|
}
|
|
1163
|
-
const
|
|
1192
|
+
const responseData = aliasMap && result.data != null ? restoreNumericAliasesInData(result.data, aliasMap) : result.data;
|
|
1193
|
+
const formattedResult = { data: responseData };
|
|
1164
1194
|
if (result.errors && result.errors.length > 0) formattedResult.errors = result.errors.map(formatError);
|
|
1165
1195
|
const status = result.data == null ? 500 : 200;
|
|
1166
|
-
|
|
1196
|
+
const extraHeaders = {};
|
|
1197
|
+
const forwardedHop = readHeaderCaseInsensitive(req.headers, "x-ocap-forwarded-hop");
|
|
1198
|
+
const sentTxHash = responseData?.sendTx?.hash;
|
|
1199
|
+
if (forwardedHop !== void 0 && typeof sentTxHash === "string" && sentTxHash && (!result.errors || result.errors.length === 0)) {
|
|
1200
|
+
const signFn = resolver.signForwardedSendTxReceipt;
|
|
1201
|
+
if (typeof signFn === "function") {
|
|
1202
|
+
const signedReceipt = await signFn.call(resolver, sentTxHash);
|
|
1203
|
+
if (signedReceipt?.signature && signedReceipt?.proposer) {
|
|
1204
|
+
extraHeaders["X-Ocap-Forward-Receipt-Sig"] = signedReceipt.signature;
|
|
1205
|
+
extraHeaders["X-Ocap-Forward-Proposer"] = signedReceipt.proposer;
|
|
1206
|
+
}
|
|
1207
|
+
if (signedReceipt?.commitProof) extraHeaders["X-Ocap-Forward-Commit-Proof"] = signedReceipt.commitProof;
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
if (slowLogMs <= 0) return sendJSON(status, formattedResult, extraHeaders);
|
|
1167
1211
|
const executedAt = nowMs();
|
|
1168
|
-
const response = sendJSON(status, formattedResult);
|
|
1212
|
+
const response = sendJSON(status, formattedResult, extraHeaders);
|
|
1169
1213
|
const finishedAt = nowMs();
|
|
1170
1214
|
const totalMs = finishedAt - startedAt;
|
|
1171
1215
|
const queuedMs = rec ? Math.max(0, startedAt - rec.receivedAt) : 0;
|
|
@@ -1225,7 +1269,9 @@ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, gra
|
|
|
1225
1269
|
sqlQueries
|
|
1226
1270
|
} : {},
|
|
1227
1271
|
fields,
|
|
1228
|
-
userAgent: headerValue(req.headers["user-agent"])?.slice(0, 160)
|
|
1272
|
+
userAgent: headerValue(req.headers["user-agent"])?.slice(0, 160),
|
|
1273
|
+
...req.clientIp ? { clientIp: req.clientIp } : {},
|
|
1274
|
+
...sourceHeaders(req.headers)
|
|
1229
1275
|
});
|
|
1230
1276
|
} catch {}
|
|
1231
1277
|
return response;
|
|
@@ -1234,7 +1280,8 @@ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, gra
|
|
|
1234
1280
|
return async (req, contextValue) => {
|
|
1235
1281
|
const rec = {
|
|
1236
1282
|
receivedAt: req.receivedAt ?? nowMs(),
|
|
1237
|
-
headers: req.headers
|
|
1283
|
+
headers: req.headers,
|
|
1284
|
+
clientIp: req.clientIp
|
|
1238
1285
|
};
|
|
1239
1286
|
stallMonitor?.begin(rec);
|
|
1240
1287
|
try {
|
|
@@ -1254,7 +1301,8 @@ const createHttpHandler = (options) => {
|
|
|
1254
1301
|
url: req.url,
|
|
1255
1302
|
headers: req.headers,
|
|
1256
1303
|
body: req.body,
|
|
1257
|
-
readBody: (limit) => readRawBody(req, limit)
|
|
1304
|
+
readBody: (limit) => readRawBody(req, limit),
|
|
1305
|
+
clientIp: (0, _ocap_util_lib_client_ip.resolveClientIp)(req) || void 0
|
|
1258
1306
|
}, req);
|
|
1259
1307
|
res.statusCode = result.status;
|
|
1260
1308
|
for (const [name, value] of Object.entries(result.headers)) res.setHeader(name, value);
|
|
@@ -1286,6 +1334,7 @@ const readFetchBody = async (request, limit) => {
|
|
|
1286
1334
|
}
|
|
1287
1335
|
return text + decoder.decode();
|
|
1288
1336
|
};
|
|
1337
|
+
const FETCH_CLIENT_IP_HEADERS = ["cf-connecting-ip", "x-real-ip"];
|
|
1289
1338
|
/**
|
|
1290
1339
|
* Fetch-native adapter (Cloudflare Workers / Durable Objects, Deno, Bun): `(Request) => Promise<Response>`.
|
|
1291
1340
|
* Same engine, same guards (depth limit, batch guard, introspection off in production, 100kb body cap) as
|
|
@@ -1300,15 +1349,17 @@ const createFetchHandler = (options) => {
|
|
|
1300
1349
|
headers[key.toLowerCase()] = value;
|
|
1301
1350
|
});
|
|
1302
1351
|
const url = new URL(request.url);
|
|
1352
|
+
const clientIp = (0, _ocap_util_lib_client_ip.resolveClientIp)({ headers }, { headers: FETCH_CLIENT_IP_HEADERS }) || void 0;
|
|
1303
1353
|
const result = await engine({
|
|
1304
1354
|
receivedAt,
|
|
1305
1355
|
method: request.method,
|
|
1306
1356
|
url: `${url.pathname}${url.search}`,
|
|
1307
1357
|
headers,
|
|
1308
|
-
readBody: (limit) => readFetchBody(request, limit)
|
|
1358
|
+
readBody: (limit) => readFetchBody(request, limit),
|
|
1359
|
+
clientIp
|
|
1309
1360
|
}, {
|
|
1310
1361
|
headers,
|
|
1311
|
-
ip:
|
|
1362
|
+
ip: clientIp
|
|
1312
1363
|
});
|
|
1313
1364
|
return new Response(result.body, {
|
|
1314
1365
|
status: result.status,
|
package/lib/http.d.cts
CHANGED
|
@@ -14,12 +14,15 @@ interface WrapResolverResult {
|
|
|
14
14
|
}
|
|
15
15
|
declare const wrapResolver: (_name: string, keys: string | string[], callback: () => Promise<ResolverResult | null> | ResolverResult | null | string) => Promise<WrapResolverResult>;
|
|
16
16
|
interface RequestContext {
|
|
17
|
-
headers?: Record<string, string>;
|
|
17
|
+
headers?: Record<string, string | string[] | undefined>;
|
|
18
18
|
ip?: string;
|
|
19
19
|
_remoteAddress?: string;
|
|
20
20
|
connection?: {
|
|
21
21
|
remoteAddress?: string;
|
|
22
22
|
};
|
|
23
|
+
socket?: {
|
|
24
|
+
remoteAddress?: string;
|
|
25
|
+
};
|
|
23
26
|
}
|
|
24
27
|
declare const formatContext: (ctx?: RequestContext) => IResolverContext;
|
|
25
28
|
type Resolver = IResolver;
|
|
@@ -114,6 +117,8 @@ interface StallInFlightEntry {
|
|
|
114
117
|
/** Root field name -> number of aliases selecting it; names the schema does not resolve are summed as `unknown`. */
|
|
115
118
|
fields?: Record<string, number>;
|
|
116
119
|
userAgent?: string;
|
|
120
|
+
/** Client address as resolved by `@ocap/util/lib/client-ip` (#1922). */
|
|
121
|
+
clientIp?: string;
|
|
117
122
|
}
|
|
118
123
|
/** `[gql] event loop stall` report: the probe timer fired `maxMs` late in the window starting at `windowStart`. */
|
|
119
124
|
interface EventLoopStallReport {
|
|
@@ -158,6 +163,12 @@ interface SlowRequestReport {
|
|
|
158
163
|
sqlQueries?: number;
|
|
159
164
|
fields: Record<string, SlowRequestFieldStat>;
|
|
160
165
|
userAgent?: string;
|
|
166
|
+
/** Client address as resolved by `@ocap/util/lib/client-ip` (#1922); joins the line with the access log. */
|
|
167
|
+
clientIp?: string;
|
|
168
|
+
/** `Host` header, at most 160 chars. */
|
|
169
|
+
host?: string;
|
|
170
|
+
/** Cloudflare `cf-ray` request id, at most 64 chars; absent when the request did not pass Cloudflare. */
|
|
171
|
+
cfRay?: string;
|
|
161
172
|
}
|
|
162
173
|
/** Must equal `SQL_TIMING_KEY` of `@ocap/util/lib/sql-timing` (a test checks it); kept here so gql has no util dep. */
|
|
163
174
|
declare const SQL_TIMING_KEY: unique symbol;
|
|
@@ -195,6 +206,8 @@ interface GqlRequestLike {
|
|
|
195
206
|
readBody: (limit: number) => Promise<string>;
|
|
196
207
|
/** `performance.now()` when the adapter received the request; defaults to the engine's own start. */
|
|
197
208
|
receivedAt?: number;
|
|
209
|
+
/** Client address resolved by the adapter (`@ocap/util/lib/client-ip`), reported in the slow / stall lines. */
|
|
210
|
+
clientIp?: string;
|
|
198
211
|
}
|
|
199
212
|
/** Transport-agnostic response produced by the engine. */
|
|
200
213
|
interface GqlResponseLike {
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.
|
|
6
|
+
"version": "1.32.1",
|
|
7
7
|
"description": "Resolver middleware for ocap adapters",
|
|
8
8
|
"type": "module",
|
|
9
9
|
"main": "./lib/index.cjs",
|
|
@@ -51,13 +51,13 @@
|
|
|
51
51
|
],
|
|
52
52
|
"license": "MIT",
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@arcblock/ws": "1.
|
|
55
|
-
"@ocap/schema": "1.
|
|
54
|
+
"@arcblock/ws": "1.32.1",
|
|
55
|
+
"@ocap/schema": "1.32.1",
|
|
56
|
+
"@ocap/util": "1.32.1",
|
|
56
57
|
"graphql": "^16.13.2",
|
|
57
58
|
"lodash": "^4.17.23"
|
|
58
59
|
},
|
|
59
60
|
"devDependencies": {
|
|
60
|
-
"@ocap/util": "1.31.9",
|
|
61
61
|
"@types/express": "^4.17.23",
|
|
62
62
|
"@types/lodash": "^4.17.13",
|
|
63
63
|
"express": "^4.22.1"
|