@softistx/alxia-graphql 0.0.0-stage → 0.20.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/LICENSE +21 -0
- package/README.md +445 -2
- package/dist/bounded-stream.d.ts +37 -0
- package/dist/bounded-stream.d.ts.map +1 -0
- package/dist/endpoint.d.ts +17 -0
- package/dist/endpoint.d.ts.map +1 -0
- package/dist/executor.d.ts +49 -0
- package/dist/executor.d.ts.map +1 -0
- package/dist/explorer.d.ts +24 -0
- package/dist/explorer.d.ts.map +1 -0
- package/dist/graphql.d.ts +63 -0
- package/dist/graphql.d.ts.map +1 -0
- package/dist/handler.d.ts +11 -0
- package/dist/handler.d.ts.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +621 -0
- package/dist/index.js.map +21 -0
- package/dist/once-per-request.d.ts +28 -0
- package/dist/once-per-request.d.ts.map +1 -0
- package/dist/operation.d.ts +3 -0
- package/dist/operation.d.ts.map +1 -0
- package/dist/options.d.ts +38 -0
- package/dist/options.d.ts.map +1 -0
- package/dist/removed.d.ts +14 -0
- package/dist/removed.d.ts.map +1 -0
- package/dist/sandbox.d.ts +15 -0
- package/dist/sandbox.d.ts.map +1 -0
- package/dist/testing/client.d.ts +95 -0
- package/dist/testing/client.d.ts.map +1 -0
- package/dist/testing/index.d.ts +2 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +73 -0
- package/dist/testing/index.js.map +10 -0
- package/dist/ws.d.ts +30 -0
- package/dist/ws.d.ts.map +1 -0
- package/docs/README.md +31 -0
- package/docs/guide/context.md +351 -0
- package/docs/guide/endpoint.md +293 -0
- package/docs/guide/executor.md +75 -0
- package/docs/guide/ide.md +160 -0
- package/docs/guide/plugins.md +316 -0
- package/docs/guide/production.md +374 -0
- package/docs/guide/testing.md +171 -0
- package/docs/guide/websockets.md +329 -0
- package/docs/migrating-to-graxia.md +408 -0
- package/docs/roadmap.md +122 -0
- package/docs/troubleshooting.md +963 -0
- package/package.json +69 -5
package/dist/index.js
ADDED
|
@@ -0,0 +1,621 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import {
|
|
3
|
+
fieldRateLimit,
|
|
4
|
+
limitFromStore
|
|
5
|
+
} from "@softistx/graxia";
|
|
6
|
+
|
|
7
|
+
// src/bounded-stream.ts
|
|
8
|
+
var DONE = { done: true, value: undefined };
|
|
9
|
+
function boundedStream(source, options) {
|
|
10
|
+
const { idleMs, totalMs, checkEveryMs, check, signal } = options;
|
|
11
|
+
const debounceMs = options.checkDebounceMs ?? 0;
|
|
12
|
+
const mapError = options.mapError ?? ((error) => error);
|
|
13
|
+
const iterator = Symbol.asyncIterator in source ? source[Symbol.asyncIterator]() : source;
|
|
14
|
+
const startedAt = Date.now();
|
|
15
|
+
let lastEventAt = startedAt;
|
|
16
|
+
let lastCheckAt = startedAt;
|
|
17
|
+
let finished = false;
|
|
18
|
+
let interrupt;
|
|
19
|
+
let pending;
|
|
20
|
+
let released;
|
|
21
|
+
const release = () => {
|
|
22
|
+
finished = true;
|
|
23
|
+
interrupt?.();
|
|
24
|
+
released ??= (async () => {
|
|
25
|
+
try {
|
|
26
|
+
await iterator.return?.(undefined);
|
|
27
|
+
} catch {}
|
|
28
|
+
})();
|
|
29
|
+
return released;
|
|
30
|
+
};
|
|
31
|
+
const onAbort = () => void release();
|
|
32
|
+
if (signal?.aborted)
|
|
33
|
+
release();
|
|
34
|
+
else
|
|
35
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
36
|
+
const detach = () => signal?.removeEventListener("abort", onAbort);
|
|
37
|
+
const checked = async () => {
|
|
38
|
+
await check();
|
|
39
|
+
lastCheckAt = Date.now();
|
|
40
|
+
};
|
|
41
|
+
const wait = (read, ms) => new Promise((resolve) => {
|
|
42
|
+
const settle = (woke) => {
|
|
43
|
+
clearTimeout(timer);
|
|
44
|
+
interrupt = undefined;
|
|
45
|
+
resolve(woke);
|
|
46
|
+
};
|
|
47
|
+
const timer = setTimeout(() => settle({ kind: "timer" }), ms);
|
|
48
|
+
interrupt = () => settle({ kind: "interrupted" });
|
|
49
|
+
read.then((result) => settle({ kind: "read", result }), (error) => settle({ kind: "failed", error }));
|
|
50
|
+
});
|
|
51
|
+
const step = async () => {
|
|
52
|
+
for (;; ) {
|
|
53
|
+
if (finished)
|
|
54
|
+
return DONE;
|
|
55
|
+
const t = Date.now();
|
|
56
|
+
const left = [totalMs - (t - startedAt), idleMs - (t - lastEventAt)];
|
|
57
|
+
if (Math.min(...left) <= 0) {
|
|
58
|
+
await release();
|
|
59
|
+
return DONE;
|
|
60
|
+
}
|
|
61
|
+
const dueIn = checkEveryMs - (t - lastCheckAt);
|
|
62
|
+
if (dueIn <= 0) {
|
|
63
|
+
await checked();
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (!pending) {
|
|
67
|
+
pending = iterator.next();
|
|
68
|
+
pending.catch(() => {});
|
|
69
|
+
}
|
|
70
|
+
const woke = await wait(pending, Math.max(1, Math.min(dueIn, ...left)));
|
|
71
|
+
if (woke.kind === "interrupted") {
|
|
72
|
+
await release();
|
|
73
|
+
return DONE;
|
|
74
|
+
}
|
|
75
|
+
if (woke.kind === "timer")
|
|
76
|
+
continue;
|
|
77
|
+
pending = undefined;
|
|
78
|
+
if (woke.kind === "failed")
|
|
79
|
+
throw woke.error;
|
|
80
|
+
if (woke.result.done) {
|
|
81
|
+
await release();
|
|
82
|
+
return DONE;
|
|
83
|
+
}
|
|
84
|
+
lastEventAt = Date.now();
|
|
85
|
+
if (lastEventAt - lastCheckAt >= debounceMs)
|
|
86
|
+
await checked();
|
|
87
|
+
if (finished)
|
|
88
|
+
return DONE;
|
|
89
|
+
return { done: false, value: woke.result.value };
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
const stream = {
|
|
93
|
+
[Symbol.asyncIterator]: () => stream,
|
|
94
|
+
next: async () => {
|
|
95
|
+
try {
|
|
96
|
+
const result = await step();
|
|
97
|
+
if (result.done)
|
|
98
|
+
detach();
|
|
99
|
+
return result;
|
|
100
|
+
} catch (error) {
|
|
101
|
+
await release();
|
|
102
|
+
detach();
|
|
103
|
+
throw mapError(error);
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
return: async () => {
|
|
107
|
+
await release();
|
|
108
|
+
detach();
|
|
109
|
+
return DONE;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
return stream;
|
|
113
|
+
}
|
|
114
|
+
// src/executor.ts
|
|
115
|
+
import { createGraxia } from "@softistx/graxia";
|
|
116
|
+
|
|
117
|
+
// src/explorer.ts
|
|
118
|
+
import {
|
|
119
|
+
apolloSandbox,
|
|
120
|
+
graphiql
|
|
121
|
+
} from "@softistx/graxia-graphiql";
|
|
122
|
+
function defined(values) {
|
|
123
|
+
return Object.fromEntries(Object.entries(values).filter(([, value]) => value !== undefined));
|
|
124
|
+
}
|
|
125
|
+
function sandboxPlugin(sandbox) {
|
|
126
|
+
const options = {
|
|
127
|
+
enabled: true,
|
|
128
|
+
...defined({ title: sandbox?.title }),
|
|
129
|
+
initialState: defined({
|
|
130
|
+
document: sandbox?.initialDocument,
|
|
131
|
+
headers: sandbox?.initialHeaders,
|
|
132
|
+
includeCookies: sandbox?.includeCookies
|
|
133
|
+
})
|
|
134
|
+
};
|
|
135
|
+
return apolloSandbox(options);
|
|
136
|
+
}
|
|
137
|
+
function explorerPlugins(options, devs) {
|
|
138
|
+
const { ide, graphiql: graphiqlOptions, sandbox } = options;
|
|
139
|
+
if (ide === "apollo-sandbox")
|
|
140
|
+
return [sandboxPlugin(sandbox)];
|
|
141
|
+
if (ide === false || graphiqlOptions === false)
|
|
142
|
+
return [];
|
|
143
|
+
return [
|
|
144
|
+
graphiql({
|
|
145
|
+
enabled: ide === "graphiql" ? true : (request) => devs.get(request) === true,
|
|
146
|
+
...graphiqlOptions
|
|
147
|
+
})
|
|
148
|
+
];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// src/operation.ts
|
|
152
|
+
import {
|
|
153
|
+
reportOperation,
|
|
154
|
+
startOperation
|
|
155
|
+
} from "@softistx/alxia";
|
|
156
|
+
import { createStateKey, definePlugin } from "@softistx/graxia";
|
|
157
|
+
import {
|
|
158
|
+
getOperationAST
|
|
159
|
+
} from "graphql";
|
|
160
|
+
var nameKey = createStateKey("alxia-operation-name");
|
|
161
|
+
var openKey = createStateKey("alxia-operation-open");
|
|
162
|
+
function describe(document, name) {
|
|
163
|
+
const operation = getOperationAST(document, name);
|
|
164
|
+
if (operation === undefined || operation === null)
|
|
165
|
+
return;
|
|
166
|
+
return { type: operation.operation, name: operation.name?.value };
|
|
167
|
+
}
|
|
168
|
+
var isStream = (result) => typeof result === "object" && result !== null && (Symbol.asyncIterator in result);
|
|
169
|
+
var isIncremental = (result) => typeof result === "object" && result !== null && ("initialResult" in result) && typeof result.subsequentResults?.next === "function";
|
|
170
|
+
var carriesErrors = (chunk) => {
|
|
171
|
+
if (typeof chunk !== "object" || chunk === null)
|
|
172
|
+
return false;
|
|
173
|
+
const payload = chunk;
|
|
174
|
+
if (Array.isArray(payload.errors) && payload.errors.length > 0)
|
|
175
|
+
return true;
|
|
176
|
+
return [payload.incremental, payload.completed].some((entries) => Array.isArray(entries) && entries.some((entry) => typeof entry === "object" && entry !== null && Array.isArray(entry.errors) && entry.errors.length > 0));
|
|
177
|
+
};
|
|
178
|
+
function observed(chunks, progress, end) {
|
|
179
|
+
const wrapped = {
|
|
180
|
+
async next() {
|
|
181
|
+
try {
|
|
182
|
+
const step = await chunks.next();
|
|
183
|
+
if (step.done === true)
|
|
184
|
+
end(progress.outcome);
|
|
185
|
+
else if (carriesErrors(step.value))
|
|
186
|
+
progress.outcome = "errors";
|
|
187
|
+
return step;
|
|
188
|
+
} catch (error) {
|
|
189
|
+
progress.outcome = "errors";
|
|
190
|
+
end("errors");
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
return(value) {
|
|
195
|
+
end(progress.outcome);
|
|
196
|
+
return chunks.return(value);
|
|
197
|
+
},
|
|
198
|
+
throw(error) {
|
|
199
|
+
return chunks.throw(error);
|
|
200
|
+
},
|
|
201
|
+
[Symbol.asyncIterator]() {
|
|
202
|
+
return wrapped;
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
return wrapped;
|
|
206
|
+
}
|
|
207
|
+
var failed = (result) => typeof result === "object" && result !== null && ("errors" in result) ? "errors" : "ok";
|
|
208
|
+
function reportsOperations() {
|
|
209
|
+
return definePlugin({
|
|
210
|
+
name: "alxia-report-operations",
|
|
211
|
+
onParse: ({ params, state }) => {
|
|
212
|
+
state.set(nameKey, params.operationName ?? null);
|
|
213
|
+
},
|
|
214
|
+
onValidate: ({ document, serverContext, state }) => {
|
|
215
|
+
return ({ errors }) => {
|
|
216
|
+
if (errors === undefined || errors.length === 0)
|
|
217
|
+
return;
|
|
218
|
+
const report = describe(document, state.get(nameKey));
|
|
219
|
+
if (report !== undefined)
|
|
220
|
+
startOperation(serverContext, report)("errors");
|
|
221
|
+
};
|
|
222
|
+
},
|
|
223
|
+
onExecute: ({ args }) => {
|
|
224
|
+
const { document, operationName, contextValue } = args;
|
|
225
|
+
const report = describe(document, operationName);
|
|
226
|
+
if (report === undefined)
|
|
227
|
+
return;
|
|
228
|
+
reportOperation(contextValue, report);
|
|
229
|
+
const end = startOperation(contextValue, report);
|
|
230
|
+
return ({ result, setResult }) => {
|
|
231
|
+
if (!isIncremental(result))
|
|
232
|
+
return end(failed(result));
|
|
233
|
+
const progress = {
|
|
234
|
+
outcome: carriesErrors(result.initialResult) ? "errors" : "ok"
|
|
235
|
+
};
|
|
236
|
+
const signal = contextValue.signal;
|
|
237
|
+
const onAbort = () => finish(progress.outcome);
|
|
238
|
+
const finish = (outcome) => {
|
|
239
|
+
signal?.removeEventListener("abort", onAbort);
|
|
240
|
+
end(outcome);
|
|
241
|
+
};
|
|
242
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
243
|
+
const subsequentResults = observed(result.subsequentResults, progress, finish);
|
|
244
|
+
setResult({ ...result, subsequentResults });
|
|
245
|
+
};
|
|
246
|
+
},
|
|
247
|
+
onSubscribe: ({ args, state }) => {
|
|
248
|
+
const { document, operationName, contextValue } = args;
|
|
249
|
+
const report = describe(document, operationName);
|
|
250
|
+
if (report === undefined)
|
|
251
|
+
return;
|
|
252
|
+
const context = contextValue;
|
|
253
|
+
reportOperation(context, report);
|
|
254
|
+
const end = startOperation(context, report);
|
|
255
|
+
const open = { outcome: "ok" };
|
|
256
|
+
state.set(openKey, open);
|
|
257
|
+
context.signal?.addEventListener("abort", () => end(open.outcome), {
|
|
258
|
+
once: true
|
|
259
|
+
});
|
|
260
|
+
return ({ result }) => {
|
|
261
|
+
if (isStream(result))
|
|
262
|
+
return;
|
|
263
|
+
open.outcome = failed(result);
|
|
264
|
+
end(open.outcome);
|
|
265
|
+
};
|
|
266
|
+
},
|
|
267
|
+
onResultChunk: ({ chunk, state }) => {
|
|
268
|
+
const open = state.get(openKey);
|
|
269
|
+
if (open !== undefined && failed(chunk) === "errors") {
|
|
270
|
+
open.outcome = "errors";
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// src/removed.ts
|
|
277
|
+
var FIXES = {
|
|
278
|
+
logging: "`logging` was Yoga's. Unexpected failures go to graxia's `onUnexpected` (console.error by default, off in production); log requests with `@softistx/alxia-logger`",
|
|
279
|
+
requestId: "`requestId` was Yoga's. Graxia generates its own `x-request-id` and never echoes the client's; the app's own id is `@softistx/alxia`'s",
|
|
280
|
+
cors: "`cors` is gone. Mount `@softistx/alxia-cors` on the app before `graphql()`",
|
|
281
|
+
sameOrigin: "`sameOrigin` is gone. Use `csrf: { sameOrigin: true }` (off by default; the CSRF header check is on)"
|
|
282
|
+
};
|
|
283
|
+
function rejectRemoved(options, caller = "graphql(app") {
|
|
284
|
+
for (const name of Object.keys(FIXES)) {
|
|
285
|
+
if (name in options && options[name] !== undefined) {
|
|
286
|
+
const call = caller === "graphql(app" ? `graphql(app, { ${name} })` : `graphqlExecutor({ ${name} })`;
|
|
287
|
+
throw new TypeError(`${call}: ${FIXES[name]}`);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// src/options.ts
|
|
293
|
+
function limitsOf(legacy, bodyLimit, limits) {
|
|
294
|
+
return {
|
|
295
|
+
...legacy.maxTokens === undefined ? {} : { tokens: legacy.maxTokens },
|
|
296
|
+
...legacy.maxNesting === undefined ? {} : { nesting: legacy.maxNesting },
|
|
297
|
+
...bodyLimit === undefined ? {} : { bodyBytes: bodyLimit },
|
|
298
|
+
...limits
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
function cachesOf(parseCache, caches) {
|
|
302
|
+
if (caches !== undefined)
|
|
303
|
+
return caches;
|
|
304
|
+
if (parseCache === undefined || parseCache === false) {
|
|
305
|
+
return parseCache === false ? false : undefined;
|
|
306
|
+
}
|
|
307
|
+
return {
|
|
308
|
+
parse: {
|
|
309
|
+
...parseCache.maxBytes === undefined ? {} : { maxBytes: parseCache.maxBytes },
|
|
310
|
+
...parseCache.max === undefined ? {} : { max: parseCache.max },
|
|
311
|
+
...parseCache.ttl === undefined ? {} : { ttlMs: parseCache.ttl }
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
function subscriptionsOf(ws) {
|
|
316
|
+
if (ws === false)
|
|
317
|
+
return { sse: true, ws: false };
|
|
318
|
+
const { keepAlive } = ws;
|
|
319
|
+
if (keepAlive === undefined)
|
|
320
|
+
return { sse: true, ws: true };
|
|
321
|
+
return {
|
|
322
|
+
sse: true,
|
|
323
|
+
ws: { keepAliveMs: keepAlive === false ? 0 : Math.ceil(keepAlive) }
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
function telemetryOf(telemetry) {
|
|
327
|
+
if (telemetry === false)
|
|
328
|
+
return false;
|
|
329
|
+
return { operationSpans: false, ...telemetry };
|
|
330
|
+
}
|
|
331
|
+
function graxiaOptions(options, wiring) {
|
|
332
|
+
rejectRemoved(options, wiring.caller);
|
|
333
|
+
const {
|
|
334
|
+
maxTokens,
|
|
335
|
+
maxNesting,
|
|
336
|
+
parseCache,
|
|
337
|
+
maskedErrors,
|
|
338
|
+
ide,
|
|
339
|
+
graphiql,
|
|
340
|
+
sandbox,
|
|
341
|
+
path: _path,
|
|
342
|
+
ws: _ws,
|
|
343
|
+
bodyLimit: _bodyLimit,
|
|
344
|
+
plugins = [],
|
|
345
|
+
limits,
|
|
346
|
+
caches,
|
|
347
|
+
telemetry,
|
|
348
|
+
...rest
|
|
349
|
+
} = options;
|
|
350
|
+
return {
|
|
351
|
+
...rest,
|
|
352
|
+
telemetry: telemetryOf(telemetry),
|
|
353
|
+
path: false,
|
|
354
|
+
plugins: [
|
|
355
|
+
...plugins,
|
|
356
|
+
...explorerPlugins({ ide, graphiql, sandbox }, wiring.devs ?? new WeakMap),
|
|
357
|
+
reportsOperations()
|
|
358
|
+
],
|
|
359
|
+
limits: limitsOf({ maxTokens, maxNesting }, wiring.bodyLimit, limits),
|
|
360
|
+
caches: cachesOf(parseCache, caches),
|
|
361
|
+
...maskedErrors === false ? { maskErrors: false } : {},
|
|
362
|
+
subscriptions: subscriptionsOf(wiring.ws)
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// src/executor.ts
|
|
367
|
+
function plain(error) {
|
|
368
|
+
return typeof error.toJSON === "function" ? error.toJSON() : error;
|
|
369
|
+
}
|
|
370
|
+
function graphqlExecutor(options) {
|
|
371
|
+
const graxia = createGraxia({
|
|
372
|
+
...graxiaOptions({ ...options, ide: false }, { ws: false, caller: "graphqlExecutor(" }),
|
|
373
|
+
csrf: false,
|
|
374
|
+
subscriptions: { sse: false, ws: false }
|
|
375
|
+
});
|
|
376
|
+
const run = async (request, context, call) => {
|
|
377
|
+
const body = await graxia.execute({
|
|
378
|
+
document: request.query,
|
|
379
|
+
...request.operationName !== undefined && {
|
|
380
|
+
operationName: request.operationName
|
|
381
|
+
},
|
|
382
|
+
...request.variables !== undefined && {
|
|
383
|
+
variables: request.variables
|
|
384
|
+
},
|
|
385
|
+
...call?.signal !== undefined && { signal: call.signal },
|
|
386
|
+
serverContext: context
|
|
387
|
+
});
|
|
388
|
+
return {
|
|
389
|
+
...body.data !== undefined && { data: body.data },
|
|
390
|
+
...body.errors !== undefined && {
|
|
391
|
+
errors: body.errors.map(plain)
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
};
|
|
395
|
+
return Object.assign(run, { dispose: () => graxia.dispose() });
|
|
396
|
+
}
|
|
397
|
+
// src/graphql.ts
|
|
398
|
+
import {
|
|
399
|
+
createGraxia as createGraxia2
|
|
400
|
+
} from "@softistx/graxia";
|
|
401
|
+
|
|
402
|
+
// src/endpoint.ts
|
|
403
|
+
import { GraxiaConfigError } from "@softistx/graxia";
|
|
404
|
+
var DEFAULT_BODY_LIMIT = 1048576;
|
|
405
|
+
var HOST_LIMIT_OFF = Number.MAX_SAFE_INTEGER;
|
|
406
|
+
function declareEndpoint(app, path, bodyLimit, mount) {
|
|
407
|
+
const bodyBytes = bodyLimit ?? app.bodyLimitInForce ?? DEFAULT_BODY_LIMIT;
|
|
408
|
+
if (!(Number.isInteger(bodyBytes) && bodyBytes > 0)) {
|
|
409
|
+
throw new GraxiaConfigError("limits.bodyBytes must be a positive integer");
|
|
410
|
+
}
|
|
411
|
+
const route = app;
|
|
412
|
+
const mounted = mount(bodyBytes);
|
|
413
|
+
route.get(path, mounted.handler);
|
|
414
|
+
route.post(path, { bodyLimit: HOST_LIMIT_OFF }, mounted.handler);
|
|
415
|
+
if (mounted.socket !== undefined) {
|
|
416
|
+
route.ws(mounted.socket.path, mounted.socket.middleware, mounted.socket.handlers);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// src/handler.ts
|
|
421
|
+
import {
|
|
422
|
+
isDev,
|
|
423
|
+
shutdownSignal
|
|
424
|
+
} from "@softistx/alxia";
|
|
425
|
+
function serverContext(ctx) {
|
|
426
|
+
const {
|
|
427
|
+
params: _params,
|
|
428
|
+
query: _query,
|
|
429
|
+
headers: _headers,
|
|
430
|
+
cookies: _cookies,
|
|
431
|
+
body: _body,
|
|
432
|
+
reply: _reply,
|
|
433
|
+
redirect: _redirect,
|
|
434
|
+
...server
|
|
435
|
+
} = ctx;
|
|
436
|
+
return server;
|
|
437
|
+
}
|
|
438
|
+
function graphqlHandler(graxia, devs) {
|
|
439
|
+
return async function graphql(ctx) {
|
|
440
|
+
const { reply } = ctx;
|
|
441
|
+
devs.set(ctx.request, isDev(ctx));
|
|
442
|
+
const response = await graxia.fetch(ctx.request, serverContext(ctx));
|
|
443
|
+
const body = endedOnShutdown(response, shutdownSignal(ctx));
|
|
444
|
+
return reply(response.status, body ?? undefined, {
|
|
445
|
+
headers: new Headers(response.headers)
|
|
446
|
+
});
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
function endedOnShutdown(response, signal) {
|
|
450
|
+
const body = response.body;
|
|
451
|
+
const type = response.headers.get("content-type") ?? "";
|
|
452
|
+
if (body === null || !/text\/event-stream|multipart\/mixed/.test(type)) {
|
|
453
|
+
return body;
|
|
454
|
+
}
|
|
455
|
+
const reader = body.getReader();
|
|
456
|
+
const end = () => {
|
|
457
|
+
signal.removeEventListener("abort", end);
|
|
458
|
+
reader.cancel();
|
|
459
|
+
try {
|
|
460
|
+
controller.close();
|
|
461
|
+
} catch {}
|
|
462
|
+
};
|
|
463
|
+
let controller;
|
|
464
|
+
return new ReadableStream({
|
|
465
|
+
start(started) {
|
|
466
|
+
controller = started;
|
|
467
|
+
if (signal.aborted)
|
|
468
|
+
end();
|
|
469
|
+
else
|
|
470
|
+
signal.addEventListener("abort", end, { once: true });
|
|
471
|
+
},
|
|
472
|
+
async pull(controller) {
|
|
473
|
+
const result = await reader.read().catch((error) => {
|
|
474
|
+
signal.removeEventListener("abort", end);
|
|
475
|
+
throw error;
|
|
476
|
+
});
|
|
477
|
+
if (result.done) {
|
|
478
|
+
signal.removeEventListener("abort", end);
|
|
479
|
+
try {
|
|
480
|
+
controller.close();
|
|
481
|
+
} catch {}
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
controller.enqueue(result.value);
|
|
485
|
+
},
|
|
486
|
+
cancel: (reason) => {
|
|
487
|
+
signal.removeEventListener("abort", end);
|
|
488
|
+
return reader.cancel(reason);
|
|
489
|
+
}
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// src/ws.ts
|
|
494
|
+
var KEY = "graxiaSocket";
|
|
495
|
+
function checkKeepAlive(options) {
|
|
496
|
+
const { keepAlive } = options;
|
|
497
|
+
if (keepAlive === undefined || keepAlive === false)
|
|
498
|
+
return;
|
|
499
|
+
const valid = typeof keepAlive === "number" && Number.isFinite(keepAlive) && keepAlive > 0 && keepAlive <= 2 ** 31 - 1;
|
|
500
|
+
if (!valid) {
|
|
501
|
+
throw new TypeError(`graphql(app, { ws: { keepAlive: ${keepAlive} } }): keepAlive is the milliseconds between pings, a positive number up to ${2 ** 31 - 1}, or false for none`);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
function graphqlSocket(graxia, endpoint, options) {
|
|
505
|
+
async function graphqlWs(ctx, next) {
|
|
506
|
+
let captured;
|
|
507
|
+
const standIn = {
|
|
508
|
+
upgrade(_request, given) {
|
|
509
|
+
captured = given;
|
|
510
|
+
return true;
|
|
511
|
+
}
|
|
512
|
+
};
|
|
513
|
+
const host = serverContext(ctx);
|
|
514
|
+
const refused = await graxia.upgrade(ctx.request, standIn, host);
|
|
515
|
+
if (refused !== undefined || captured === undefined)
|
|
516
|
+
return refused;
|
|
517
|
+
for (const [name, value] of new Headers(captured.headers)) {
|
|
518
|
+
ctx.set.headers.set(name, value);
|
|
519
|
+
}
|
|
520
|
+
return next({ [KEY]: captured.data });
|
|
521
|
+
}
|
|
522
|
+
const handlers = {
|
|
523
|
+
open(socket) {
|
|
524
|
+
Object.assign(socket.raw.data, socket.data[KEY]);
|
|
525
|
+
graxia.websocket.open?.(socket.raw);
|
|
526
|
+
},
|
|
527
|
+
async message(socket, message) {
|
|
528
|
+
await graxia.websocket.message?.(socket.raw, message);
|
|
529
|
+
},
|
|
530
|
+
async close(socket, code, reason) {
|
|
531
|
+
await graxia.websocket.close?.(socket.raw, code, reason);
|
|
532
|
+
},
|
|
533
|
+
drain(socket) {
|
|
534
|
+
graxia.websocket.drain?.(socket.raw);
|
|
535
|
+
}
|
|
536
|
+
};
|
|
537
|
+
return { path: options.path ?? endpoint, middleware: graphqlWs, handlers };
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// src/graphql.ts
|
|
541
|
+
function graphql(app, options) {
|
|
542
|
+
const { path = "/graphql", ws = false, bodyLimit } = options;
|
|
543
|
+
if (typeof ws === "object")
|
|
544
|
+
checkKeepAlive(ws);
|
|
545
|
+
const devs = new WeakMap;
|
|
546
|
+
declareEndpoint(app, path, bodyLimit, (bodyBytes) => {
|
|
547
|
+
const graxia = createGraxia2(graxiaOptions(options, {
|
|
548
|
+
devs,
|
|
549
|
+
ws: ws === false ? false : ws === true ? {} : ws,
|
|
550
|
+
bodyLimit: bodyBytes
|
|
551
|
+
}));
|
|
552
|
+
app.onStop(() => graxia.dispose());
|
|
553
|
+
return {
|
|
554
|
+
handler: graphqlHandler(graxia, devs),
|
|
555
|
+
socket: ws === false ? undefined : graphqlSocket(graxia, path, ws === true ? {} : ws)
|
|
556
|
+
};
|
|
557
|
+
});
|
|
558
|
+
return app;
|
|
559
|
+
}
|
|
560
|
+
// src/once-per-request.ts
|
|
561
|
+
var caches = new WeakMap;
|
|
562
|
+
function oncePerRequest(request, key, load) {
|
|
563
|
+
let cache = caches.get(request);
|
|
564
|
+
if (!cache) {
|
|
565
|
+
cache = new Map;
|
|
566
|
+
caches.set(request, cache);
|
|
567
|
+
}
|
|
568
|
+
if (cache.has(key))
|
|
569
|
+
return cache.get(key);
|
|
570
|
+
const loaded = load();
|
|
571
|
+
cache.set(key, loaded);
|
|
572
|
+
if (loaded instanceof Promise) {
|
|
573
|
+
const mine = cache;
|
|
574
|
+
loaded.catch(() => {
|
|
575
|
+
if (mine.get(key) === loaded)
|
|
576
|
+
mine.delete(key);
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
return loaded;
|
|
580
|
+
}
|
|
581
|
+
function clearRequestCache(request) {
|
|
582
|
+
caches.delete(request);
|
|
583
|
+
}
|
|
584
|
+
function freshPerMutation(resolvers) {
|
|
585
|
+
if (Array.isArray(resolvers)) {
|
|
586
|
+
return resolvers.map((map) => freshPerMutation(map));
|
|
587
|
+
}
|
|
588
|
+
return wrapMutation(resolvers);
|
|
589
|
+
}
|
|
590
|
+
function wrapMutation(resolvers) {
|
|
591
|
+
const mutation = resolvers.Mutation;
|
|
592
|
+
if (!mutation)
|
|
593
|
+
return resolvers;
|
|
594
|
+
const wrapped = {};
|
|
595
|
+
for (const [name, field] of Object.entries(mutation)) {
|
|
596
|
+
if (typeof field !== "function") {
|
|
597
|
+
wrapped[name] = field;
|
|
598
|
+
continue;
|
|
599
|
+
}
|
|
600
|
+
wrapped[name] = function(parent, args, context, info) {
|
|
601
|
+
if (typeof context === "object" && context !== null) {
|
|
602
|
+
clearRequestCache(context);
|
|
603
|
+
}
|
|
604
|
+
return field.call(this, parent, args, context, info);
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
return { ...resolvers, Mutation: wrapped };
|
|
608
|
+
}
|
|
609
|
+
export {
|
|
610
|
+
boundedStream,
|
|
611
|
+
clearRequestCache,
|
|
612
|
+
fieldRateLimit,
|
|
613
|
+
freshPerMutation,
|
|
614
|
+
graphql,
|
|
615
|
+
graphqlExecutor,
|
|
616
|
+
limitFromStore,
|
|
617
|
+
oncePerRequest
|
|
618
|
+
};
|
|
619
|
+
|
|
620
|
+
//# debugId=4994F7144E33FEBC64756E2164756E21
|
|
621
|
+
//# sourceMappingURL=index.js.map
|