@alxia/react-router 0.22.0 → 0.23.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 +13 -4
- package/dist/subscription/index.d.ts +1 -1
- package/dist/subscription/index.d.ts.map +1 -1
- package/dist/subscription/index.js +10 -9
- package/dist/subscription/index.js.map +3 -3
- package/dist/subscription/proxy.d.ts +26 -11
- package/dist/subscription/proxy.d.ts.map +1 -1
- package/docs/guide/subscriptions.md +14 -6
- package/docs/troubleshooting.md +8 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -997,8 +997,13 @@ connections mode, so `graphql-sse` works against it. It imports nothing and adds
|
|
|
997
997
|
|
|
998
998
|
```ts
|
|
999
999
|
// app/routes/events.ts
|
|
1000
|
-
import {
|
|
1000
|
+
import { alxiaOf } from '@alxia/react-router';
|
|
1001
|
+
import {
|
|
1002
|
+
createSubscriptionProxy,
|
|
1003
|
+
type SubscriptionArgs,
|
|
1004
|
+
} from '@alxia/react-router/subscription';
|
|
1001
1005
|
import { print } from 'graphql';
|
|
1006
|
+
import type { RouterContextProvider } from 'react-router';
|
|
1002
1007
|
import { RoomEventsDocument } from '../generated/graphql';
|
|
1003
1008
|
import { cookies } from '../session.server';
|
|
1004
1009
|
|
|
@@ -1006,8 +1011,12 @@ const events = createSubscriptionProxy({
|
|
|
1006
1011
|
endpoint: 'https://api.example.com/graphql',
|
|
1007
1012
|
allow: { RoomEvents: print(RoomEventsDocument) },
|
|
1008
1013
|
getToken: (request) => cookies.token(request),
|
|
1009
|
-
|
|
1010
|
-
|
|
1014
|
+
// the route's context is the second argument: send the trusted client address
|
|
1015
|
+
headers: (
|
|
1016
|
+
request,
|
|
1017
|
+
{ context }: SubscriptionArgs<Readonly<RouterContextProvider>>,
|
|
1018
|
+
) => ({
|
|
1019
|
+
'x-forwarded-for': context ? alxiaOf(context).ip : null,
|
|
1011
1020
|
'accept-language': request.headers.get('accept-language'),
|
|
1012
1021
|
}),
|
|
1013
1022
|
});
|
|
@@ -1288,7 +1297,7 @@ From `@alxia/react-router/subscription` (no import, no peer):
|
|
|
1288
1297
|
|
|
1289
1298
|
| export | |
|
|
1290
1299
|
| --- | --- |
|
|
1291
|
-
| `createSubscriptionProxy({ endpoint, allow, getToken, headers?, origin?, heartbeatMs?, fetch? })`, `SubscriptionProxy`, `SubscriptionProxyOptions` | a resource route's `loader` (GET) and `action` (POST) relaying GraphQL subscriptions over SSE (graphql-sse's distinct connections mode). `allow` is `{ [operationName]: document }`: the proxy sends its own document, the client's `query` and `extensions` are ignored and an unknown name is a 403. Both methods refuse a cross-site request (403) and a POST that is not JSON (415); 401 without a token; 400 for a malformed or oversize request (body 64 KiB, query string and variables 16 KiB); the bearer and `headers` upstream, a heartbeat comment every `heartbeatMs` (15000), the upstream aborted with the client, a pulled relay with a 1 MiB cap per event, an upstream 429 passed on with `Retry-After` and `retryAfter`, 502 for an upstream failure |
|
|
1300
|
+
| `createSubscriptionProxy({ endpoint, allow, getToken, headers?, origin?, heartbeatMs?, fetch? })`, `SubscriptionProxy`, `SubscriptionProxyOptions`, `SubscriptionArgs` | a resource route's `loader` (GET) and `action` (POST) relaying GraphQL subscriptions over SSE (graphql-sse's distinct connections mode). `allow` is `{ [operationName]: document }`: the proxy sends its own document, the client's `query` and `extensions` are ignored and an unknown name is a 403. Both methods refuse a cross-site request (403) and a POST that is not JSON (415); 401 without a token; 400 for a malformed or oversize request (body 64 KiB, query string and variables 16 KiB); the bearer and `headers` upstream (`headers(request, { request, context, params })`: a function gets the route's context, which never leaves it), a heartbeat comment every `heartbeatMs` (15000), the upstream aborted with the client, a pulled relay with a 1 MiB cap per event, an upstream 429 passed on with `Retry-After` and `retryAfter`, 502 for an upstream failure |
|
|
1292
1301
|
|
|
1293
1302
|
The `alxia-react-router` bin, run with `bunx`:
|
|
1294
1303
|
|
|
@@ -3,5 +3,5 @@
|
|
|
3
3
|
* BFF, as a resource route. It imports nothing (web APIs only), so it adds
|
|
4
4
|
* no peer.
|
|
5
5
|
*/
|
|
6
|
-
export { createSubscriptionProxy, type SubscriptionProxy, type SubscriptionProxyOptions, } from './proxy';
|
|
6
|
+
export { createSubscriptionProxy, type SubscriptionArgs, type SubscriptionProxy, type SubscriptionProxyOptions, } from './proxy';
|
|
7
7
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/subscription/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EACN,uBAAuB,EACvB,KAAK,iBAAiB,EACtB,KAAK,wBAAwB,GAC7B,MAAM,SAAS,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/subscription/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EACN,uBAAuB,EACvB,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,EACtB,KAAK,wBAAwB,GAC7B,MAAM,SAAS,CAAC"}
|
|
@@ -266,9 +266,9 @@ function relay(upstream, abort, heartbeatMs) {
|
|
|
266
266
|
|
|
267
267
|
// src/subscription/proxy.ts
|
|
268
268
|
var UNSAFE = /[\r\n\0]/;
|
|
269
|
-
async function upstreamHeaders(config,
|
|
269
|
+
async function upstreamHeaders(config, args, token) {
|
|
270
270
|
const { headers } = config.options;
|
|
271
|
-
const extra = typeof headers === "function" ? await headers(request) : headers;
|
|
271
|
+
const extra = typeof headers === "function" ? await headers(args.request, args) : headers;
|
|
272
272
|
const out = new Headers;
|
|
273
273
|
for (const [name, value] of extra instanceof Headers ? extra.entries() : Object.entries(extra ?? {}))
|
|
274
274
|
if (value !== null && value !== undefined)
|
|
@@ -278,7 +278,8 @@ async function upstreamHeaders(config, request, token) {
|
|
|
278
278
|
out.set("authorization", `Bearer ${token}`);
|
|
279
279
|
return out;
|
|
280
280
|
}
|
|
281
|
-
async function handle(config,
|
|
281
|
+
async function handle(config, args) {
|
|
282
|
+
const { request } = args;
|
|
282
283
|
if (isCrossSite(request, config.origin()))
|
|
283
284
|
return problem(403, "Cross-site request refused.", "FORBIDDEN");
|
|
284
285
|
const token = await config.options.getToken(request);
|
|
@@ -297,9 +298,9 @@ async function handle(config, request) {
|
|
|
297
298
|
return problem(403, "This operation is not allowed.", "FORBIDDEN");
|
|
298
299
|
let headers;
|
|
299
300
|
try {
|
|
300
|
-
headers = await upstreamHeaders(config,
|
|
301
|
+
headers = await upstreamHeaders(config, args, token);
|
|
301
302
|
} catch {
|
|
302
|
-
return problem(500, "The proxy's upstream headers are not valid (a name or a value no header can hold).", "PROXY_MISCONFIGURED");
|
|
303
|
+
return problem(500, "The proxy's upstream headers are not valid (a name or a value no header can hold, or the headers function threw).", "PROXY_MISCONFIGURED");
|
|
303
304
|
}
|
|
304
305
|
const abort = new AbortController;
|
|
305
306
|
if (request.signal.aborted)
|
|
@@ -335,7 +336,7 @@ async function handle(config, request) {
|
|
|
335
336
|
return refused;
|
|
336
337
|
}
|
|
337
338
|
function only(method, run) {
|
|
338
|
-
return async (
|
|
339
|
+
return async (args) => args.request.method === method ? run(args) : problem(405, `Use ${method}.`, "METHOD_NOT_ALLOWED", {}, { allow: method });
|
|
339
340
|
}
|
|
340
341
|
function createSubscriptionProxy(options) {
|
|
341
342
|
checkDocuments(options.allow);
|
|
@@ -348,13 +349,13 @@ function createSubscriptionProxy(options) {
|
|
|
348
349
|
upstreamFetch: options.fetch ?? ((request) => fetch(request))
|
|
349
350
|
};
|
|
350
351
|
return {
|
|
351
|
-
loader: only("GET", (
|
|
352
|
-
action: only("POST", (
|
|
352
|
+
loader: only("GET", (args) => handle(config, args)),
|
|
353
|
+
action: only("POST", (args) => handle(config, args))
|
|
353
354
|
};
|
|
354
355
|
}
|
|
355
356
|
export {
|
|
356
357
|
createSubscriptionProxy
|
|
357
358
|
};
|
|
358
359
|
|
|
359
|
-
//# debugId=
|
|
360
|
+
//# debugId=4D71F489F243030064756E2164756E21
|
|
360
361
|
//# sourceMappingURL=index.js.map
|
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
"/** An answer the proxy gives itself, as GraphQL-shaped JSON a client can read. */\nexport class Refusal extends Error {\n\tconstructor(\n\t\treadonly status: number,\n\t\tmessage: string,\n\t\treadonly code: string,\n\t) {\n\t\tsuper(message);\n\t\tthis.name = 'Refusal';\n\t}\n}\n\nexport function problem(\n\tstatus: number,\n\tmessage: string,\n\tcode: string,\n\textra: Record<string, unknown> = {},\n\theaders: HeadersInit = {},\n): Response {\n\treturn new Response(\n\t\tJSON.stringify({\n\t\t\terrors: [{ message, extensions: { code, ...extra } }],\n\t\t\t...extra,\n\t\t}),\n\t\t{\n\t\t\tstatus,\n\t\t\theaders: {\n\t\t\t\t'content-type': 'application/json',\n\t\t\t\t'cache-control': 'no-store',\n\t\t\t\t...headers,\n\t\t\t},\n\t\t},\n\t);\n}\n\nexport const refusalResponse = (refusal: Refusal): Response =>\n\tproblem(refusal.status, refusal.message, refusal.code);\n\nfunction retryAfterOf(upstream: Response, body: unknown): number | undefined {\n\tconst header = Number(upstream.headers.get('retry-after'));\n\tif (Number.isFinite(header) && header > 0) return Math.ceil(header);\n\tconst seconds = (value: unknown): number | undefined =>\n\t\ttypeof value === 'number' && Number.isFinite(value) && value > 0\n\t\t\t? Math.ceil(value)\n\t\t\t: undefined;\n\tif (typeof body !== 'object' || body === null) return undefined;\n\tconst top = body as { retryAfter?: unknown; errors?: unknown };\n\tconst first = Array.isArray(top.errors)\n\t\t? (top.errors[0] as { extensions?: { retryAfter?: unknown } } | undefined)\n\t\t: undefined;\n\treturn seconds(top.retryAfter) ?? seconds(first?.extensions?.retryAfter);\n}\n\n/** What a non-2xx upstream answer becomes: a 429 with `retryAfter`, a 502 for a 5xx, any other 4xx as it came. */\nexport async function refusedUpstream(upstream: Response): Promise<Response> {\n\tconst text = await upstream.text().catch(() => '');\n\tif (upstream.status === 429) {\n\t\tlet body: unknown;\n\t\ttry {\n\t\t\tbody = JSON.parse(text);\n\t\t} catch {}\n\t\tconst retryAfter = retryAfterOf(upstream, body);\n\t\treturn problem(\n\t\t\t429,\n\t\t\t'Too many requests.',\n\t\t\t'RATE_LIMITED',\n\t\t\tretryAfter === undefined ? {} : { retryAfter },\n\t\t\tretryAfter === undefined ? {} : { 'retry-after': String(retryAfter) },\n\t\t);\n\t}\n\tif (upstream.status >= 500)\n\t\treturn problem(502, 'The API is unavailable.', 'UPSTREAM_ERROR');\n\treturn new Response(text, {\n\t\tstatus: upstream.status,\n\t\theaders: {\n\t\t\t'content-type':\n\t\t\t\tupstream.headers.get('content-type') ?? 'application/json',\n\t\t\t'cache-control': 'no-store',\n\t\t},\n\t});\n}\n",
|
|
7
7
|
"import { Refusal } from './refusal';\n\n/** The most a body, a query string or the variables may weigh, in bytes. */\nexport const LIMITS = {\n\tbody: 64 * 1024,\n\tqueryString: 16 * 1024,\n\tvariables: 16 * 1024,\n} as const;\n\nexport interface Asked {\n\treadonly operationName: string;\n\treadonly variables?: Record<string, unknown>;\n}\n\nconst TOO_LARGE = () =>\n\tnew Refusal(400, 'The request is too large.', 'BAD_REQUEST');\nconst NOT_JSON = () =>\n\tnew Refusal(400, 'The request is not valid JSON.', 'BAD_REQUEST');\n\nfunction record(value: unknown): Record<string, unknown> | undefined {\n\treturn typeof value === 'object' && value !== null && !Array.isArray(value)\n\t\t? (value as Record<string, unknown>)\n\t\t: undefined;\n}\n\n/** The body as text, refused past `limit` bytes: `content-length` first, then a cap on what is read. */\nasync function textOf(request: Request, limit: number): Promise<string> {\n\tconst declared = Number(request.headers.get('content-length'));\n\tif (Number.isFinite(declared) && declared > limit) throw TOO_LARGE();\n\tif (request.body === null) return '';\n\tconst reader = request.body.getReader();\n\tconst chunks: Uint8Array[] = [];\n\tlet size = 0;\n\tfor (;;) {\n\t\tconst { done, value } = await reader.read();\n\t\tif (done) break;\n\t\tsize += value.byteLength;\n\t\tif (size > limit) {\n\t\t\tvoid reader.cancel().catch(() => {});\n\t\t\tthrow TOO_LARGE();\n\t\t}\n\t\tchunks.push(value);\n\t}\n\treturn new TextDecoder().decode(Buffer.concat(chunks));\n}\n\nfunction parse(text: string): unknown {\n\ttry {\n\t\treturn JSON.parse(text);\n\t} catch {\n\t\tthrow NOT_JSON();\n\t}\n}\n\n/**\n * What a client asks for: an `operationName` and its `variables`, from a\n * GET's search params or a POST's JSON body. Any `query` or `extensions` it\n * sends (graphql-sse always sends a `query`) is ignored.\n */\nexport async function readAsked(request: Request): Promise<Asked> {\n\tlet raw: Record<string, unknown> | undefined;\n\tif (request.method === 'GET') {\n\t\tconst url = new URL(request.url);\n\t\tif (url.search.length > LIMITS.queryString) throw TOO_LARGE();\n\t\tconst variables = url.searchParams.get('variables');\n\t\traw = {\n\t\t\toperationName: url.searchParams.get('operationName') ?? undefined,\n\t\t\tvariables: variables ? parse(variables) : undefined,\n\t\t};\n\t} else {\n\t\tconst type = (request.headers.get('content-type') ?? '')\n\t\t\t.split(';')[0]\n\t\t\t?.trim()\n\t\t\t.toLowerCase();\n\t\tif (type !== 'application/json')\n\t\t\tthrow new Refusal(\n\t\t\t\t415,\n\t\t\t\t'Send the request as application/json.',\n\t\t\t\t'UNSUPPORTED_MEDIA_TYPE',\n\t\t\t);\n\t\traw = record(parse(await textOf(request, LIMITS.body)));\n\t\tif (raw === undefined)\n\t\t\tthrow new Refusal(\n\t\t\t\t400,\n\t\t\t\t'The request must be a JSON object.',\n\t\t\t\t'BAD_REQUEST',\n\t\t\t);\n\t}\n\tconst { operationName, variables } = raw;\n\tif (operationName === undefined)\n\t\tthrow new Refusal(400, 'Send an \"operationName\".', 'BAD_REQUEST');\n\tif (typeof operationName !== 'string')\n\t\tthrow new Refusal(400, '\"operationName\" must be a string.', 'BAD_REQUEST');\n\tif (variables !== undefined) {\n\t\tif (record(variables) === undefined)\n\t\t\tthrow new Refusal(400, '\"variables\" must be an object.', 'BAD_REQUEST');\n\t\tif (JSON.stringify(variables).length > LIMITS.variables) throw TOO_LARGE();\n\t}\n\treturn {\n\t\toperationName,\n\t\t...(variables === undefined\n\t\t\t? {}\n\t\t\t: { variables: variables as Record<string, unknown> }),\n\t};\n}\n",
|
|
8
8
|
"/** The most one event may weigh before the stream is cut, in characters. */\nexport const MAX_EVENT = 1024 * 1024;\n\nconst encoder = new TextEncoder();\nconst decoder = new TextDecoder();\n\nexport const SSE_HEADERS = {\n\t'content-type': 'text/event-stream; charset=utf-8',\n\t'cache-control': 'no-store, no-transform',\n\t'x-accel-buffering': 'no',\n};\n\nconst FAILED =\n\t'event: next\\ndata: {\"errors\":[{\"message\":\"The API stream failed.\",\"extensions\":{\"code\":\"UPSTREAM_ERROR\"}}]}\\n\\nevent: complete\\ndata:\\n\\n';\n\n/**\n * Relays whole events (a heartbeat never lands inside one) until the upstream\n * ends or the client leaves. The stream is pulled: upstream is read only when\n * the client has taken what was queued, so a slow client holds the upstream\n * back instead of growing a buffer, and a heartbeat is skipped while one is\n * waiting.\n */\nexport function relay(\n\tupstream: Response,\n\tabort: AbortController,\n\theartbeatMs: number,\n): Response {\n\tconst reader = (upstream.body as ReadableStream<Uint8Array>).getReader();\n\tlet timer: ReturnType<typeof setInterval> | undefined;\n\tlet buffer = '';\n\tconst stop = (): void => {\n\t\tif (timer !== undefined) clearInterval(timer);\n\t\ttimer = undefined;\n\t};\n\tconst stream = new ReadableStream<Uint8Array>(\n\t\t{\n\t\t\tstart(controller) {\n\t\t\t\tif (heartbeatMs > 0)\n\t\t\t\t\ttimer = setInterval(() => {\n\t\t\t\t\t\tif ((controller.desiredSize ?? 0) > 0)\n\t\t\t\t\t\t\tcontroller.enqueue(encoder.encode(':\\n\\n'));\n\t\t\t\t\t}, heartbeatMs);\n\t\t\t},\n\t\t\tasync pull(controller) {\n\t\t\t\ttry {\n\t\t\t\t\tfor (;;) {\n\t\t\t\t\t\tconst { done, value } = await reader.read();\n\t\t\t\t\t\tif (done) {\n\t\t\t\t\t\t\tstop();\n\t\t\t\t\t\t\tif (buffer.trim() !== '')\n\t\t\t\t\t\t\t\tcontroller.enqueue(encoder.encode(`${buffer}\\n\\n`));\n\t\t\t\t\t\t\tcontroller.close();\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbuffer += decoder\n\t\t\t\t\t\t\t.decode(value, { stream: true })\n\t\t\t\t\t\t\t.replace(/\\r\\n?/g, '\\n');\n\t\t\t\t\t\tconst cut = buffer.lastIndexOf('\\n\\n');\n\t\t\t\t\t\tif (cut !== -1) {\n\t\t\t\t\t\t\tcontroller.enqueue(encoder.encode(buffer.slice(0, cut + 2)));\n\t\t\t\t\t\t\tbuffer = buffer.slice(cut + 2);\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (buffer.length > MAX_EVENT) throw new Error('event too large');\n\t\t\t\t\t}\n\t\t\t\t} catch {\n\t\t\t\t\tstop();\n\t\t\t\t\tif (abort.signal.aborted) return;\n\t\t\t\t\tabort.abort();\n\t\t\t\t\tcontroller.enqueue(encoder.encode(FAILED));\n\t\t\t\t\tcontroller.close();\n\t\t\t\t}\n\t\t\t},\n\t\t\tcancel() {\n\t\t\t\tstop();\n\t\t\t\tabort.abort();\n\t\t\t\tvoid reader.cancel().catch(() => {});\n\t\t\t},\n\t\t},\n\t\t{ highWaterMark: 1 },\n\t);\n\treturn new Response(stream, { status: 200, headers: SSE_HEADERS });\n}\n",
|
|
9
|
-
"import { isCrossSite } from '../errors/cross-site';\nimport { checkDocuments } from './documents';\nimport { readAsked } from './read';\nimport { problem, Refusal, refusalResponse, refusedUpstream } from './refusal';\nimport { relay } from './relay';\n\ntype HeaderValues = Record<string, string | null | undefined> | Headers;\n\nexport interface SubscriptionProxyOptions {\n\t/** The API's GraphQL endpoint, which serves subscriptions over SSE. */\n\treadonly endpoint: string | URL;\n\t/**\n\t * The operations the browser may start: each name with the document the\n\t * proxy sends upstream for it. The client names an operation and sends\n\t * variables; its own `query` and `extensions` are ignored, so nothing it\n\t * writes reaches the API. A name that is not a key is a 403.\n\t */\n\treadonly allow: Readonly<Record<string, string>>;\n\t/** The session's token, from the request: `(request) => cookies.token(request)`. Empty is a 401. */\n\treadonly getToken: (\n\t\trequest: Request,\n\t) => string | null | undefined | Promise<string | null | undefined>;\n\t
|
|
9
|
+
"import { isCrossSite } from '../errors/cross-site';\nimport { checkDocuments } from './documents';\nimport { readAsked } from './read';\nimport { problem, Refusal, refusalResponse, refusedUpstream } from './refusal';\nimport { relay } from './relay';\n\ntype HeaderValues = Record<string, string | null | undefined> | Headers;\n\n/**\n * What React Router gives the resource route's `loader` and `action`, and what\n * `headers` receives beside the request: the route's `context` (the app's\n * `getLoadContext` value, `RouterContextProvider`) and `params`. Both are\n * optional so a call with the request alone, as in a spec, still type-checks.\n */\nexport interface SubscriptionArgs<Context = unknown> {\n\treadonly request: Request;\n\treadonly context?: Context;\n\treadonly params?: Readonly<Record<string, string | undefined>>;\n}\n\nexport interface SubscriptionProxyOptions<Context = unknown> {\n\t/** The API's GraphQL endpoint, which serves subscriptions over SSE. */\n\treadonly endpoint: string | URL;\n\t/**\n\t * The operations the browser may start: each name with the document the\n\t * proxy sends upstream for it. The client names an operation and sends\n\t * variables; its own `query` and `extensions` are ignored, so nothing it\n\t * writes reaches the API. A name that is not a key is a 403.\n\t */\n\treadonly allow: Readonly<Record<string, string>>;\n\t/** The session's token, from the request: `(request) => cookies.token(request)`. Empty is a 401. */\n\treadonly getToken: (\n\t\trequest: Request,\n\t) => string | null | undefined | Promise<string | null | undefined>;\n\t/**\n\t * Extra upstream headers (client name, proxy secret, `X-Forwarded-For`,\n\t * `Accept-Language`), fixed or built per request. A `null` or `undefined`\n\t * value is left out. A function gets the request and, as its second\n\t * argument, the route's loader or action arguments (`context`, `params`), so\n\t * it can send the trusted client address from the app's context instead of\n\t * copying a header the browser could forge. They reach this function only,\n\t * never the browser.\n\t */\n\treadonly headers?:\n\t\t| HeaderValues\n\t\t| ((\n\t\t\t\trequest: Request,\n\t\t\t\targs: SubscriptionArgs<Context>,\n\t\t ) => HeaderValues | Promise<HeaderValues>);\n\t/** The app's own origin for the cross-site check, as `jsonActions` takes it; the request's own by default. */\n\treadonly origin?: string | (() => string | undefined);\n\t/** A comment line sent this often so a proxy or Bun's `idleTimeout` keeps the stream open. 15000 by default; `0` turns it off. */\n\treadonly heartbeatMs?: number;\n\t/** The `fetch` used upstream. The global one by default. */\n\treadonly fetch?: (request: Request) => Promise<Response>;\n}\n\nexport interface SubscriptionProxy<Context = unknown> {\n\t/** A GET: graphql-sse's distinct connections mode, the operation in the search params. */\n\tloader(args: SubscriptionArgs<Context>): Promise<Response>;\n\t/** A POST: the operation in a JSON body. */\n\taction(args: SubscriptionArgs<Context>): Promise<Response>;\n}\n\ninterface Config {\n\treadonly endpoint: URL;\n\treadonly documents: ReadonlyMap<string, string>;\n\t// `Context` is the caller's own claim about what its route passes and is never checked; `never` lets any typed `headers` function be stored.\n\treadonly options: SubscriptionProxyOptions<never>;\n\treadonly origin: () => string | undefined;\n\treadonly heartbeatMs: number;\n\treadonly upstreamFetch: (request: Request) => Promise<Response>;\n}\n\nconst UNSAFE = /[\\r\\n\\0]/;\n\nasync function upstreamHeaders(\n\tconfig: Config,\n\targs: SubscriptionArgs,\n\ttoken: string,\n): Promise<Headers> {\n\tconst { headers } = config.options;\n\tconst extra =\n\t\ttypeof headers === 'function'\n\t\t\t? await headers(args.request, args as SubscriptionArgs<never>)\n\t\t\t: headers;\n\tconst out = new Headers();\n\tfor (const [name, value] of extra instanceof Headers\n\t\t? extra.entries()\n\t\t: Object.entries(extra ?? {}))\n\t\tif (value !== null && value !== undefined) out.set(name, value);\n\tout.set('content-type', 'application/json');\n\tout.set('accept', 'text/event-stream');\n\tout.set('authorization', `Bearer ${token}`);\n\treturn out;\n}\n\nasync function handle(\n\tconfig: Config,\n\targs: SubscriptionArgs,\n): Promise<Response> {\n\tconst { request } = args;\n\tif (isCrossSite(request, config.origin()))\n\t\treturn problem(403, 'Cross-site request refused.', 'FORBIDDEN');\n\tconst token = await config.options.getToken(request);\n\tif (!token || UNSAFE.test(token))\n\t\treturn problem(401, 'Sign in to continue.', 'UNAUTHENTICATED');\n\n\tlet asked: Awaited<ReturnType<typeof readAsked>>;\n\ttry {\n\t\tasked = await readAsked(request);\n\t} catch (error) {\n\t\tif (error instanceof Refusal) return refusalResponse(error);\n\t\treturn problem(400, 'The request body could not be read.', 'BAD_REQUEST');\n\t}\n\tconst query = config.documents.get(asked.operationName);\n\tif (query === undefined)\n\t\treturn problem(403, 'This operation is not allowed.', 'FORBIDDEN');\n\n\tlet headers: Headers;\n\ttry {\n\t\theaders = await upstreamHeaders(config, args, token);\n\t} catch {\n\t\treturn problem(\n\t\t\t500,\n\t\t\t\"The proxy's upstream headers are not valid (a name or a value no header can hold, or the headers function threw).\",\n\t\t\t'PROXY_MISCONFIGURED',\n\t\t);\n\t}\n\n\tconst abort = new AbortController();\n\tif (request.signal.aborted) abort.abort();\n\telse\n\t\trequest.signal.addEventListener('abort', () => abort.abort(), {\n\t\t\tonce: true,\n\t\t});\n\tlet upstream: Response;\n\ttry {\n\t\tupstream = await config.upstreamFetch(\n\t\t\tnew Request(config.endpoint, {\n\t\t\t\tmethod: 'POST',\n\t\t\t\theaders,\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\tquery,\n\t\t\t\t\toperationName: asked.operationName,\n\t\t\t\t\t...(asked.variables === undefined\n\t\t\t\t\t\t? {}\n\t\t\t\t\t\t: { variables: asked.variables }),\n\t\t\t\t}),\n\t\t\t\tsignal: abort.signal,\n\t\t\t}),\n\t\t);\n\t} catch {\n\t\treturn problem(502, 'The API is unreachable.', 'UPSTREAM_ERROR');\n\t}\n\tconst streaming =\n\t\tupstream.ok &&\n\t\tupstream.body !== null &&\n\t\t(upstream.headers.get('content-type') ?? '').startsWith(\n\t\t\t'text/event-stream',\n\t\t);\n\tif (streaming) return relay(upstream, abort, config.heartbeatMs);\n\tif (upstream.ok) {\n\t\tabort.abort();\n\t\treturn problem(\n\t\t\t502,\n\t\t\t'The API did not answer with an event stream.',\n\t\t\t'UPSTREAM_ERROR',\n\t\t);\n\t}\n\tconst refused = await refusedUpstream(upstream);\n\tabort.abort();\n\treturn refused;\n}\n\nfunction only(\n\tmethod: string,\n\trun: (args: SubscriptionArgs) => Promise<Response>,\n): (args: SubscriptionArgs) => Promise<Response> {\n\treturn async (args) =>\n\t\targs.request.method === method\n\t\t\t? run(args)\n\t\t\t: problem(\n\t\t\t\t\t405,\n\t\t\t\t\t`Use ${method}.`,\n\t\t\t\t\t'METHOD_NOT_ALLOWED',\n\t\t\t\t\t{},\n\t\t\t\t\t{ allow: method },\n\t\t\t\t);\n}\n\n/**\n * A resource route that exposes GraphQL subscriptions to the browser without\n * giving it the API's token or a way to write its own document. See the\n * subscription guide.\n */\nexport function createSubscriptionProxy<Context = unknown>(\n\toptions: SubscriptionProxyOptions<Context>,\n): SubscriptionProxy<Context> {\n\tcheckDocuments(options.allow);\n\tconst config: Config = {\n\t\tendpoint: new URL(options.endpoint),\n\t\tdocuments: new Map(Object.entries(options.allow)),\n\t\toptions: options as SubscriptionProxyOptions<never>,\n\t\torigin: () =>\n\t\t\ttypeof options.origin === 'function' ? options.origin() : options.origin,\n\t\theartbeatMs: options.heartbeatMs ?? 15000,\n\t\tupstreamFetch: options.fetch ?? ((request) => fetch(request)),\n\t};\n\treturn {\n\t\tloader: only('GET', (args) => handle(config, args)),\n\t\taction: only('POST', (args) => handle(config, args)),\n\t};\n}\n"
|
|
10
10
|
],
|
|
11
|
-
"mappings": ";;;;;AAOA,SAAS,QAAQ,CAAC,UAA0B;AAAA,EAC3C,OAAO,SACL,QAAQ,mBAAmB,GAAG,EAC9B,QAAQ,wBAAwB,GAAG,EACnC,QAAQ,cAAc,GAAG;AAAA;AAI5B,SAAS,WAAW,CAAC,UAAuD;AAAA,EAC3E,MAAM,QAA6C,CAAC;AAAA,EACpD,IAAI,QAAQ;AAAA,EACZ,IAAI;AAAA,EACJ,IAAI,WAAW;AAAA,EACf,WAAW,KAAK,SAAS,QAAQ,EAAE,SAAS,sB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B,IAAI,IAAI,IACR,QACA,KACA,OAAO,WACP,sBACA,CAAC,GACD,EAAE,OAAO,OAAO,CACjB;AAAA;AAQG,SAAS,uBAA0C,CACzD,SAC6B;AAAA,EAC7B,eAAe,QAAQ,KAAK;AAAA,EAC5B,MAAM,SAAiB;AAAA,IACtB,UAAU,IAAI,IAAI,QAAQ,QAAQ;AAAA,IAClC,WAAW,IAAI,IAAI,OAAO,QAAQ,QAAQ,KAAK,CAAC;AAAA,IAChD;AAAA,IACA,QAAQ,MACP,OAAO,QAAQ,WAAW,aAAa,QAAQ,OAAO,IAAI,QAAQ;AAAA,IACnE,aAAa,QAAQ,eAAe;AAAA,IACpC,eAAe,QAAQ,UAAU,CAAC,YAAY,MAAM,OAAO;AAAA,EAC5D;AAAA,EACA,OAAO;AAAA,IACN,QAAQ,KAAK,OAAO,CAAC,SAAS,OAAO,QAAQ,IAAI,CAAC;AAAA,IAClD,QAAQ,KAAK,QAAQ,CAAC,SAAS,OAAO,QAAQ,IAAI,CAAC;AAAA,EACpD;AAAA;",
|
|
12
|
+
"debugId": "4D71F489F243030064756E2164756E21",
|
|
13
13
|
"names": []
|
|
14
14
|
}
|
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
type HeaderValues = Record<string, string | null | undefined> | Headers;
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* What React Router gives the resource route's `loader` and `action`, and what
|
|
4
|
+
* `headers` receives beside the request: the route's `context` (the app's
|
|
5
|
+
* `getLoadContext` value, `RouterContextProvider`) and `params`. Both are
|
|
6
|
+
* optional so a call with the request alone, as in a spec, still type-checks.
|
|
7
|
+
*/
|
|
8
|
+
export interface SubscriptionArgs<Context = unknown> {
|
|
9
|
+
readonly request: Request;
|
|
10
|
+
readonly context?: Context;
|
|
11
|
+
readonly params?: Readonly<Record<string, string | undefined>>;
|
|
12
|
+
}
|
|
13
|
+
export interface SubscriptionProxyOptions<Context = unknown> {
|
|
3
14
|
/** The API's GraphQL endpoint, which serves subscriptions over SSE. */
|
|
4
15
|
readonly endpoint: string | URL;
|
|
5
16
|
/**
|
|
@@ -11,8 +22,16 @@ export interface SubscriptionProxyOptions {
|
|
|
11
22
|
readonly allow: Readonly<Record<string, string>>;
|
|
12
23
|
/** The session's token, from the request: `(request) => cookies.token(request)`. Empty is a 401. */
|
|
13
24
|
readonly getToken: (request: Request) => string | null | undefined | Promise<string | null | undefined>;
|
|
14
|
-
/**
|
|
15
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Extra upstream headers (client name, proxy secret, `X-Forwarded-For`,
|
|
27
|
+
* `Accept-Language`), fixed or built per request. A `null` or `undefined`
|
|
28
|
+
* value is left out. A function gets the request and, as its second
|
|
29
|
+
* argument, the route's loader or action arguments (`context`, `params`), so
|
|
30
|
+
* it can send the trusted client address from the app's context instead of
|
|
31
|
+
* copying a header the browser could forge. They reach this function only,
|
|
32
|
+
* never the browser.
|
|
33
|
+
*/
|
|
34
|
+
readonly headers?: HeaderValues | ((request: Request, args: SubscriptionArgs<Context>) => HeaderValues | Promise<HeaderValues>);
|
|
16
35
|
/** The app's own origin for the cross-site check, as `jsonActions` takes it; the request's own by default. */
|
|
17
36
|
readonly origin?: string | (() => string | undefined);
|
|
18
37
|
/** A comment line sent this often so a proxy or Bun's `idleTimeout` keeps the stream open. 15000 by default; `0` turns it off. */
|
|
@@ -20,21 +39,17 @@ export interface SubscriptionProxyOptions {
|
|
|
20
39
|
/** The `fetch` used upstream. The global one by default. */
|
|
21
40
|
readonly fetch?: (request: Request) => Promise<Response>;
|
|
22
41
|
}
|
|
23
|
-
export interface SubscriptionProxy {
|
|
42
|
+
export interface SubscriptionProxy<Context = unknown> {
|
|
24
43
|
/** A GET: graphql-sse's distinct connections mode, the operation in the search params. */
|
|
25
|
-
loader(args:
|
|
26
|
-
request: Request;
|
|
27
|
-
}): Promise<Response>;
|
|
44
|
+
loader(args: SubscriptionArgs<Context>): Promise<Response>;
|
|
28
45
|
/** A POST: the operation in a JSON body. */
|
|
29
|
-
action(args:
|
|
30
|
-
request: Request;
|
|
31
|
-
}): Promise<Response>;
|
|
46
|
+
action(args: SubscriptionArgs<Context>): Promise<Response>;
|
|
32
47
|
}
|
|
33
48
|
/**
|
|
34
49
|
* A resource route that exposes GraphQL subscriptions to the browser without
|
|
35
50
|
* giving it the API's token or a way to write its own document. See the
|
|
36
51
|
* subscription guide.
|
|
37
52
|
*/
|
|
38
|
-
export declare function createSubscriptionProxy(options: SubscriptionProxyOptions): SubscriptionProxy
|
|
53
|
+
export declare function createSubscriptionProxy<Context = unknown>(options: SubscriptionProxyOptions<Context>): SubscriptionProxy<Context>;
|
|
39
54
|
export {};
|
|
40
55
|
//# sourceMappingURL=proxy.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"proxy.d.ts","sourceRoot":"","sources":["../../src/subscription/proxy.ts"],"names":[],"mappings":"AAMA,KAAK,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,GAAG,OAAO,CAAC;AAExE,MAAM,WAAW,wBAAwB;
|
|
1
|
+
{"version":3,"file":"proxy.d.ts","sourceRoot":"","sources":["../../src/subscription/proxy.ts"],"names":[],"mappings":"AAMA,KAAK,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,GAAG,OAAO,CAAC;AAExE;;;;;GAKG;AACH,MAAM,WAAW,gBAAgB,CAAC,OAAO,GAAG,OAAO;IAClD,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC;IAC3B,QAAQ,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC;CAC/D;AAED,MAAM,WAAW,wBAAwB,CAAC,OAAO,GAAG,OAAO;IAC1D,uEAAuE;IACvE,QAAQ,CAAC,QAAQ,EAAE,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13 @@ client was generated from:
|
|
|
17
17
|
|
|
18
18
|
```ts
|
|
19
19
|
// app/routes/events.ts: a resource route, served at /events
|
|
20
|
-
import {
|
|
20
|
+
import { alxiaOf } from '@alxia/react-router';
|
|
21
|
+
import {
|
|
22
|
+
createSubscriptionProxy,
|
|
23
|
+
type SubscriptionArgs,
|
|
24
|
+
} from '@alxia/react-router/subscription';
|
|
21
25
|
import { print } from 'graphql';
|
|
26
|
+
import type { RouterContextProvider } from 'react-router';
|
|
22
27
|
import { ChatMessagesDocument, RoomEventsDocument } from '../generated/graphql';
|
|
23
28
|
import { cookies } from '../session.server';
|
|
24
29
|
|
|
@@ -30,13 +35,16 @@ const events = createSubscriptionProxy({
|
|
|
30
35
|
// or the document string codegen exports, or a literal
|
|
31
36
|
},
|
|
32
37
|
getToken: (request) => cookies.token(request),
|
|
33
|
-
headers: (
|
|
38
|
+
headers: (
|
|
39
|
+
request,
|
|
40
|
+
{ context }: SubscriptionArgs<Readonly<RouterContextProvider>>,
|
|
41
|
+
) => ({
|
|
34
42
|
'x-client-name': 'my-bff',
|
|
35
43
|
'x-proxy-secret': process.env.PROXY_SECRET,
|
|
36
|
-
|
|
44
|
+
// the trusted client address (behind `proxy: trustProxy(...)`), not the browser's own header; null leaves it out
|
|
45
|
+
'x-forwarded-for': context ? alxiaOf(context).ip : null,
|
|
37
46
|
'accept-language': request.headers.get('accept-language'),
|
|
38
|
-
}),
|
|
39
|
-
});
|
|
47
|
+
}),});
|
|
40
48
|
|
|
41
49
|
export const loader = events.loader; // GET: operationName and variables in the query string
|
|
42
50
|
export const action = events.action; // POST: the same in a JSON body
|
|
@@ -98,7 +106,7 @@ adapter or reverse proxy needs to buffer it.
|
|
|
98
106
|
| `endpoint` | the API's GraphQL URL (required) |
|
|
99
107
|
| `allow` | operation name to the document sent upstream (required); each document must define `subscription <name>`, that operation alone, checked when the proxy is created |
|
|
100
108
|
| `getToken(request)` | the session's token, sync or async (required) |
|
|
101
|
-
| `headers` | a record, `Headers`, or `(request) => record
|
|
109
|
+
| `headers` | a record, `Headers`, or `(request, args) => record`, sync or async; `args` is what React Router gave the route (`request`, `context`, `params`), so a header can come from the app's context; a `null` or `undefined` value is left out. A `(request) => record` function keeps working |
|
|
102
110
|
| `origin` | the app's origin for the same-site check, a string or a getter; the request's own by default |
|
|
103
111
|
| `heartbeatMs` | 15000 |
|
|
104
112
|
| `fetch` | the `fetch` used upstream (the global one); useful in specs |
|
package/docs/troubleshooting.md
CHANGED
|
@@ -2808,9 +2808,14 @@ string, or `variables` is not an object.
|
|
|
2808
2808
|
### A subscription answers 500 `The proxy's upstream headers are not valid …`
|
|
2809
2809
|
|
|
2810
2810
|
**Why:** a value of `headers` (a forwarded `Accept-Language`, a secret from the
|
|
2811
|
-
environment) holds a line break, or a name is not a header name
|
|
2812
|
-
|
|
2813
|
-
|
|
2811
|
+
environment) holds a line break, or a name is not a header name, or the
|
|
2812
|
+
`headers` function threw (for example `alxiaOf(context)` on a request served
|
|
2813
|
+
without alxia's context). The answer's text is fixed: nothing from the error
|
|
2814
|
+
reaches the browser.
|
|
2815
|
+
|
|
2816
|
+
**Fix:** trim or validate the value before it goes in `headers`, and guard the
|
|
2817
|
+
context (`context ? alxiaOf(context).ip : null`); read the error in your own
|
|
2818
|
+
log by catching inside the function.
|
|
2814
2819
|
|
|
2815
2820
|
### A subscription answers 429 with `retryAfter`
|
|
2816
2821
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alxia/react-router",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.23.0",
|
|
4
4
|
"description": "alxia as the server of a React Router framework app: server rendering behind the app's hooks, loaders reading its typed context, the client's assets served; the code with no UI an installable app shares (`/pwa`), its hooks (`/pwa/react`), `noCache` for the worker's script (`/pwa/server`), the service worker on Workbox (`/pwa/worker`) and its build (`/pwa/build`), an API's refusals worded for the page (`/errors`), the BFF session: cookies, sign-in, step-up and mail links (`/session`), its page hooks (`/session/react`) and what it shares with the page (`/session/model`), a GraphQL subscription relayed to the browser over SSE (`/subscription`), the page's CSP nonce and inline boot scripts (`/document`) and a streaming `entry.server` (`/entry`)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|