@cedarjs/api-server 5.0.0-canary.0 → 5.0.0-canary.13874

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.
Files changed (39) hide show
  1. package/dist/bin.js +112 -38
  2. package/dist/cjs/bin.js +122 -48
  3. package/dist/cjs/createServer.d.ts.map +1 -1
  4. package/dist/cjs/createServer.js +6 -5
  5. package/dist/cjs/fastify.d.ts.map +1 -1
  6. package/dist/cjs/fastify.js +5 -0
  7. package/dist/cjs/plugins/api.d.ts.map +1 -1
  8. package/dist/cjs/plugins/graphql.d.ts.map +1 -1
  9. package/dist/cjs/plugins/graphql.js +27 -21
  10. package/dist/cjs/plugins/lambdaLoader.d.ts +16 -6
  11. package/dist/cjs/plugins/lambdaLoader.d.ts.map +1 -1
  12. package/dist/cjs/plugins/lambdaLoader.js +76 -12
  13. package/dist/cjs/requestHandlers/awsLambdaFastify.d.ts.map +1 -1
  14. package/dist/cjs/requestHandlers/awsLambdaFastify.js +6 -2
  15. package/dist/cjs/udDispatcher.d.ts +28 -0
  16. package/dist/cjs/udDispatcher.d.ts.map +1 -0
  17. package/dist/cjs/udDispatcher.js +192 -0
  18. package/dist/cjs/udFetchable.d.ts +12 -0
  19. package/dist/cjs/udFetchable.d.ts.map +1 -0
  20. package/dist/cjs/udFetchable.js +36 -0
  21. package/dist/createServer.d.ts.map +1 -1
  22. package/dist/createServer.js +3 -2
  23. package/dist/fastify.d.ts.map +1 -1
  24. package/dist/fastify.js +5 -0
  25. package/dist/plugins/api.d.ts.map +1 -1
  26. package/dist/plugins/graphql.d.ts.map +1 -1
  27. package/dist/plugins/graphql.js +27 -21
  28. package/dist/plugins/lambdaLoader.d.ts +16 -6
  29. package/dist/plugins/lambdaLoader.d.ts.map +1 -1
  30. package/dist/plugins/lambdaLoader.js +72 -10
  31. package/dist/requestHandlers/awsLambdaFastify.d.ts.map +1 -1
  32. package/dist/requestHandlers/awsLambdaFastify.js +6 -2
  33. package/dist/udDispatcher.d.ts +28 -0
  34. package/dist/udDispatcher.d.ts.map +1 -0
  35. package/dist/udDispatcher.js +158 -0
  36. package/dist/udFetchable.d.ts +12 -0
  37. package/dist/udFetchable.d.ts.map +1 -0
  38. package/dist/udFetchable.js +12 -0
  39. package/package.json +41 -12
package/dist/bin.js CHANGED
@@ -141,6 +141,9 @@ async function loadFastifyConfig() {
141
141
  if (!fs.existsSync(serverConfigPath)) {
142
142
  return serverConfigFile;
143
143
  }
144
+ if (loadedServerConfigPath !== serverConfigPath) {
145
+ isServerConfigLoaded = false;
146
+ }
144
147
  if (!isServerConfigLoaded) {
145
148
  console.log(`Loading server config from ${serverConfigPath}`);
146
149
  console.log(`Loading server config from URL file://${serverConfigPath}`);
@@ -149,11 +152,12 @@ async function loadFastifyConfig() {
149
152
  );
150
153
  const config3 = await import(pathToFileURL(serverConfigPath).href);
151
154
  serverConfigFile = { ...config3.default };
155
+ loadedServerConfigPath = serverConfigPath;
152
156
  isServerConfigLoaded = true;
153
157
  }
154
158
  return serverConfigFile;
155
159
  }
156
- var DEFAULT_OPTIONS, isServerConfigLoaded, serverConfigFile, createFastifyInstance;
160
+ var DEFAULT_OPTIONS, isServerConfigLoaded, loadedServerConfigPath, serverConfigFile, createFastifyInstance;
157
161
  var init_fastify = __esm({
158
162
  "src/fastify.ts"() {
159
163
  "use strict";
@@ -236,14 +240,18 @@ var init_awsLambdaFastify = __esm({
236
240
  );
237
241
  return {
238
242
  httpMethod: request.method,
239
- headers: request.headers,
243
+ headers: {
244
+ "x-forwarded-proto": request.protocol,
245
+ ...request.headers
246
+ },
240
247
  path: request.urlData("path"),
241
248
  queryStringParameters: qsParams,
242
249
  requestContext: {
243
250
  requestId: request.id,
244
251
  identity: {
245
252
  sourceIp: request.ip
246
- }
253
+ },
254
+ domainName: request.hostname
247
255
  },
248
256
  ...parseBody(request.rawBody || "")
249
257
  // adds `body` and `isBase64Encoded`
@@ -316,24 +324,33 @@ var init_utils2 = __esm({
316
324
  });
317
325
 
318
326
  // src/plugins/lambdaLoader.ts
319
- import path2 from "path";
327
+ import path2 from "node:path";
328
+ import { pathToFileURL as pathToFileURL2 } from "node:url";
320
329
  import ansis from "ansis";
321
330
  import fg from "fast-glob";
331
+ import { terminalLink } from "termi-link";
332
+ import { buildCedarContext, wrapLegacyHandler } from "@cedarjs/api/runtime";
322
333
  import { getPaths as getPaths2 } from "@cedarjs/project-config";
323
- var LAMBDA_FUNCTIONS, setLambdaFunctions, loadFunctionsFromDist, findApiDistFunctions, lambdaRequestHandler;
334
+ var LAMBDA_FUNCTIONS, CEDAR_HANDLERS, cedarRouteManifest, setLambdaFunctions, loadFunctionsFromDist, findApiDistFunctions, lambdaRequestHandler;
324
335
  var init_lambdaLoader = __esm({
325
336
  "src/plugins/lambdaLoader.ts"() {
326
337
  "use strict";
327
338
  init_awsLambdaFastify();
328
339
  init_utils2();
329
- LAMBDA_FUNCTIONS = {};
340
+ LAMBDA_FUNCTIONS = /* @__PURE__ */ new Map();
341
+ CEDAR_HANDLERS = /* @__PURE__ */ new Map();
342
+ cedarRouteManifest = [];
330
343
  setLambdaFunctions = async (foundFunctions) => {
331
344
  const tsImport = Date.now();
332
345
  console.log(ansis.dim.italic("Importing Server Functions... "));
346
+ cedarRouteManifest.length = 0;
347
+ LAMBDA_FUNCTIONS.clear();
348
+ CEDAR_HANDLERS.clear();
333
349
  const imports = foundFunctions.map(async (fnPath) => {
334
350
  const ts = Date.now();
335
351
  const routeName = path2.basename(fnPath).replace(".js", "");
336
- const fnImport = await import(`file://${fnPath}`);
352
+ const routePath = routeName === "graphql" ? "/graphql" : `/${routeName}`;
353
+ const fnImport = await import(pathToFileURL2(fnPath).href);
337
354
  const handler3 = (() => {
338
355
  if ("handler" in fnImport) {
339
356
  return fnImport.handler;
@@ -345,17 +362,39 @@ var init_lambdaLoader = __esm({
345
362
  }
346
363
  return void 0;
347
364
  })();
348
- LAMBDA_FUNCTIONS[routeName] = handler3;
349
- if (!handler3) {
365
+ const cedarHandler = (() => {
366
+ if ("handleRequest" in fnImport && typeof fnImport.handleRequest === "function") {
367
+ return fnImport.handleRequest;
368
+ }
369
+ if ("default" in fnImport && fnImport.default && "handleRequest" in fnImport.default && typeof fnImport.default.handleRequest === "function") {
370
+ return fnImport.default.handleRequest;
371
+ }
372
+ return void 0;
373
+ })();
374
+ if (handler3) {
375
+ LAMBDA_FUNCTIONS.set(routeName, handler3);
376
+ }
377
+ if (cedarHandler) {
378
+ CEDAR_HANDLERS.set(routeName, cedarHandler);
379
+ } else if (handler3) {
380
+ CEDAR_HANDLERS.set(routeName, wrapLegacyHandler(handler3));
381
+ }
382
+ if (!handler3 && !cedarHandler) {
350
383
  console.warn(
351
384
  routeName,
352
385
  "at",
353
386
  fnPath,
354
- "does not have a function called handler defined."
387
+ "does not have a function called handler or handleRequest defined."
355
388
  );
356
389
  }
390
+ cedarRouteManifest.push({
391
+ path: routePath,
392
+ methods: routeName === "graphql" ? ["GET", "POST", "OPTIONS"] : ["GET", "POST"],
393
+ type: routeName === "graphql" ? "graphql" : routeName === "health" ? "health" : routeName.toLowerCase().includes("auth") ? "auth" : "function",
394
+ entry: fnPath
395
+ });
357
396
  console.log(
358
- ansis.magenta("/" + routeName),
397
+ terminalLink(ansis.magenta("/" + routeName), pathToFileURL2(fnPath).href),
359
398
  ansis.dim.italic(Date.now() - ts + " ms")
360
399
  );
361
400
  });
@@ -394,14 +433,43 @@ var init_lambdaLoader = __esm({
394
433
  };
395
434
  lambdaRequestHandler = async (req, reply) => {
396
435
  const { routeName } = req.params;
397
- if (!LAMBDA_FUNCTIONS[routeName]) {
436
+ const cedarHandlerCandidate = CEDAR_HANDLERS.get(routeName);
437
+ const cedarHandler = typeof cedarHandlerCandidate === "function" ? cedarHandlerCandidate : void 0;
438
+ if (cedarHandler) {
439
+ const requestBody = req.method === "GET" || req.method === "HEAD" ? void 0 : typeof req.rawBody === "string" ? req.rawBody : req.rawBody ? Buffer.from(req.rawBody).toString() : void 0;
440
+ const href = `${req.protocol}://${req.hostname}${req.raw.url ?? "/"}`;
441
+ const request = new Request(href, {
442
+ method: req.method,
443
+ headers: req.headers,
444
+ body: requestBody
445
+ });
446
+ const ctx = await buildCedarContext(request, {
447
+ params: {
448
+ routeName
449
+ }
450
+ });
451
+ const response = await cedarHandler(request, ctx);
452
+ reply.status(response.status);
453
+ response.headers.forEach((value, name) => {
454
+ reply.header(name, value);
455
+ });
456
+ const body = await response.arrayBuffer();
457
+ reply.send(Buffer.from(body));
458
+ return;
459
+ }
460
+ const handler3 = LAMBDA_FUNCTIONS.get(routeName);
461
+ if (handler3) {
462
+ return requestHandler(req, reply, handler3);
463
+ } else {
398
464
  const errorMessage = `Function "${routeName}" was not found.`;
399
465
  req.log.error(errorMessage);
400
466
  reply.status(404);
401
467
  if (process.env.NODE_ENV === "development") {
402
468
  const devError = {
403
469
  error: errorMessage,
404
- availableFunctions: Object.keys(LAMBDA_FUNCTIONS)
470
+ availableFunctions: [
471
+ .../* @__PURE__ */ new Set([...LAMBDA_FUNCTIONS.keys(), ...CEDAR_HANDLERS.keys()])
472
+ ]
405
473
  };
406
474
  reply.send(devError);
407
475
  } else {
@@ -409,7 +477,6 @@ var init_lambdaLoader = __esm({
409
477
  }
410
478
  return;
411
479
  }
412
- return requestHandler(req, reply, LAMBDA_FUNCTIONS[routeName]);
413
480
  };
414
481
  }
415
482
  });
@@ -467,9 +534,11 @@ var graphql_exports = {};
467
534
  __export(graphql_exports, {
468
535
  redwoodFastifyGraphQLServer: () => redwoodFastifyGraphQLServer
469
536
  });
537
+ import { pathToFileURL as pathToFileURL3 } from "node:url";
470
538
  import fastifyMultiPart from "@fastify/multipart";
471
539
  import fastifyUrlData2 from "@fastify/url-data";
472
540
  import fg2 from "fast-glob";
541
+ import { buildCedarContext as buildCedarContext2 } from "@cedarjs/api/runtime";
473
542
  import { getAsyncStoreInstance as getAsyncStoreInstance3 } from "@cedarjs/context/dist/store";
474
543
  import { coerceRootPath as coerceRootPath3 } from "@cedarjs/fastify-web/dist/helpers.js";
475
544
  import { createGraphQLYoga } from "@cedarjs/graphql-server";
@@ -490,7 +559,7 @@ async function redwoodFastifyGraphQLServer(fastify2, options) {
490
559
  cwd: getPaths3().api.base,
491
560
  absolute: true
492
561
  });
493
- const filePath = `file://${graphqlFunctionPath}`;
562
+ const filePath = pathToFileURL3(graphqlFunctionPath).href;
494
563
  const { __rw_graphqlOptions } = await import(filePath);
495
564
  if (!__rw_graphqlOptions) {
496
565
  return;
@@ -498,33 +567,28 @@ async function redwoodFastifyGraphQLServer(fastify2, options) {
498
567
  redwoodOptions.graphql = __rw_graphqlOptions;
499
568
  }
500
569
  const graphqlOptions = redwoodOptions.graphql;
501
- if (graphqlOptions?.realtime) {
502
- const { useCedarRealtime } = await import("@cedarjs/realtime");
503
- const originalExtraPlugins = graphqlOptions.extraPlugins ?? [];
504
- originalExtraPlugins.push(
505
- // This type cast is needed because useCedarRealtime returns an
506
- // EnvelopPlugin and here we need a YogaPlugin. I can't change the
507
- // return type of `useCedarRealtime` yet, because it'd be a breaking
508
- // change.
509
- useCedarRealtime(graphqlOptions.realtime)
510
- );
511
- graphqlOptions.extraPlugins = originalExtraPlugins;
512
- if (graphqlOptions.realtime.subscriptions) {
513
- method.push("PUT");
514
- }
570
+ if (graphqlOptions?.realtime?.subscriptions) {
571
+ method.push("PUT");
515
572
  }
516
- const { yoga } = createGraphQLYoga(graphqlOptions);
573
+ const { yoga } = await createGraphQLYoga(graphqlOptions);
517
574
  const graphqlEndpoint = trimSlashes(yoga.graphqlEndpoint);
518
575
  const routePaths = ["", "/health", "/readiness", "/stream"];
519
576
  for (const routePath of routePaths) {
520
577
  fastify2.route({
521
578
  url: `${redwoodOptions.apiRootPath}${graphqlEndpoint}${routePath}`,
522
579
  method,
523
- handler: (req, reply) => yoga.handleNodeRequestAndResponse(req, reply, {
524
- req,
525
- reply,
526
- event: lambdaEventForFastifyRequest(req)
527
- })
580
+ handler: async (req, _reply) => {
581
+ const request = createFetchRequest(req);
582
+ const cedarContext = await buildCedarContext2(request, {
583
+ authDecoder: graphqlOptions.authDecoder
584
+ });
585
+ return yoga.handle(request, {
586
+ request,
587
+ cedarContext,
588
+ event: lambdaEventForFastifyRequest(req),
589
+ requestContext: void 0
590
+ });
591
+ }
528
592
  });
529
593
  }
530
594
  fastify2.addHook("onReady", (done) => {
@@ -544,6 +608,15 @@ async function redwoodFastifyGraphQLServer(fastify2, options) {
544
608
  function trimSlashes(path5) {
545
609
  return path5.replace(/^\/|\/$/g, "");
546
610
  }
611
+ function createFetchRequest(req) {
612
+ const requestBody = req.method === "GET" || req.method === "HEAD" ? void 0 : typeof req.body === "string" ? req.body : req.body ? JSON.stringify(req.body) : void 0;
613
+ const href = `${req.protocol}://${req.hostname}${req.raw.url ?? "/"}`;
614
+ return new Request(href, {
615
+ method: req.method,
616
+ headers: req.headers,
617
+ body: requestBody
618
+ });
619
+ }
547
620
  var init_graphql = __esm({
548
621
  "src/plugins/graphql.ts"() {
549
622
  "use strict";
@@ -553,7 +626,8 @@ var init_graphql = __esm({
553
626
 
554
627
  // src/createServer.ts
555
628
  import fs2 from "node:fs";
556
- import path3 from "path";
629
+ import path3 from "node:path";
630
+ import { pathToFileURL as pathToFileURL4 } from "node:url";
557
631
  import ansis2 from "ansis";
558
632
  import { config } from "dotenv-defaults";
559
633
  import fg3 from "fast-glob";
@@ -620,7 +694,7 @@ async function createServer(options = {}) {
620
694
  });
621
695
  if (graphqlFunctionPath) {
622
696
  const { redwoodFastifyGraphQLServer: redwoodFastifyGraphQLServer2 } = await Promise.resolve().then(() => (init_graphql(), graphql_exports));
623
- const { __rw_graphqlOptions } = await import(`file://${graphqlFunctionPath}`);
697
+ const { __rw_graphqlOptions } = await import(pathToFileURL4(graphqlFunctionPath).href);
624
698
  await server.register(redwoodFastifyGraphQLServer2, {
625
699
  redwood: {
626
700
  apiRootPath,
@@ -866,7 +940,7 @@ if (!process.env.CEDAR_ENV_FILES_LOADED) {
866
940
  process.env.CEDAR_ENV_FILES_LOADED = "true";
867
941
  }
868
942
  process.env.NODE_ENV ??= "production";
869
- yargs(hideBin(process.argv)).scriptName("rw-server").strict().alias("h", "help").alias("v", "version").command(
943
+ yargs(hideBin(process.argv)).scriptName("cedar-server").strict().alias("h", "help").alias("v", "version").command(
870
944
  "$0",
871
945
  description2,
872
946
  // @ts-expect-error The yargs types seem wrong; it's ok for builder to be a function
package/dist/cjs/bin.js CHANGED
@@ -157,6 +157,9 @@ async function loadFastifyConfig() {
157
157
  if (!import_node_fs.default.existsSync(serverConfigPath)) {
158
158
  return serverConfigFile;
159
159
  }
160
+ if (loadedServerConfigPath !== serverConfigPath) {
161
+ isServerConfigLoaded = false;
162
+ }
160
163
  if (!isServerConfigLoaded) {
161
164
  console.log(`Loading server config from ${serverConfigPath}`);
162
165
  console.log(`Loading server config from URL file://${serverConfigPath}`);
@@ -165,11 +168,12 @@ async function loadFastifyConfig() {
165
168
  );
166
169
  const config3 = await import((0, import_url.pathToFileURL)(serverConfigPath).href);
167
170
  serverConfigFile = { ...config3.default };
171
+ loadedServerConfigPath = serverConfigPath;
168
172
  isServerConfigLoaded = true;
169
173
  }
170
174
  return serverConfigFile;
171
175
  }
172
- var import_node_fs, import_path, import_url, import_fastify, import_store, import_project_config2, DEFAULT_OPTIONS, isServerConfigLoaded, serverConfigFile, createFastifyInstance;
176
+ var import_node_fs, import_path, import_url, import_fastify, import_store, import_project_config2, DEFAULT_OPTIONS, isServerConfigLoaded, loadedServerConfigPath, serverConfigFile, createFastifyInstance;
173
177
  var init_fastify = __esm({
174
178
  "src/fastify.ts"() {
175
179
  "use strict";
@@ -258,14 +262,18 @@ var init_awsLambdaFastify = __esm({
258
262
  );
259
263
  return {
260
264
  httpMethod: request.method,
261
- headers: request.headers,
265
+ headers: {
266
+ "x-forwarded-proto": request.protocol,
267
+ ...request.headers
268
+ },
262
269
  path: request.urlData("path"),
263
270
  queryStringParameters: qsParams,
264
271
  requestContext: {
265
272
  requestId: request.id,
266
273
  identity: {
267
274
  sourceIp: request.ip
268
- }
275
+ },
276
+ domainName: request.hostname
269
277
  },
270
278
  ...parseBody(request.rawBody || "")
271
279
  // adds `body` and `isBase64Encoded`
@@ -338,24 +346,33 @@ var init_utils2 = __esm({
338
346
  });
339
347
 
340
348
  // src/plugins/lambdaLoader.ts
341
- var import_path2, import_ansis, import_fast_glob, import_project_config3, LAMBDA_FUNCTIONS, setLambdaFunctions, loadFunctionsFromDist, findApiDistFunctions, lambdaRequestHandler;
349
+ var import_node_path, import_node_url, import_ansis, import_fast_glob, import_termi_link, import_runtime, import_project_config3, LAMBDA_FUNCTIONS, CEDAR_HANDLERS, cedarRouteManifest, setLambdaFunctions, loadFunctionsFromDist, findApiDistFunctions, lambdaRequestHandler;
342
350
  var init_lambdaLoader = __esm({
343
351
  "src/plugins/lambdaLoader.ts"() {
344
352
  "use strict";
345
- import_path2 = __toESM(require("path"), 1);
353
+ import_node_path = __toESM(require("node:path"), 1);
354
+ import_node_url = require("node:url");
346
355
  import_ansis = __toESM(require("ansis"), 1);
347
356
  import_fast_glob = __toESM(require("fast-glob"), 1);
357
+ import_termi_link = require("termi-link");
358
+ import_runtime = require("@cedarjs/api/runtime");
348
359
  import_project_config3 = require("@cedarjs/project-config");
349
360
  init_awsLambdaFastify();
350
361
  init_utils2();
351
- LAMBDA_FUNCTIONS = {};
362
+ LAMBDA_FUNCTIONS = /* @__PURE__ */ new Map();
363
+ CEDAR_HANDLERS = /* @__PURE__ */ new Map();
364
+ cedarRouteManifest = [];
352
365
  setLambdaFunctions = async (foundFunctions) => {
353
366
  const tsImport = Date.now();
354
367
  console.log(import_ansis.default.dim.italic("Importing Server Functions... "));
368
+ cedarRouteManifest.length = 0;
369
+ LAMBDA_FUNCTIONS.clear();
370
+ CEDAR_HANDLERS.clear();
355
371
  const imports = foundFunctions.map(async (fnPath) => {
356
372
  const ts = Date.now();
357
- const routeName = import_path2.default.basename(fnPath).replace(".js", "");
358
- const fnImport = await import(`file://${fnPath}`);
373
+ const routeName = import_node_path.default.basename(fnPath).replace(".js", "");
374
+ const routePath = routeName === "graphql" ? "/graphql" : `/${routeName}`;
375
+ const fnImport = await import((0, import_node_url.pathToFileURL)(fnPath).href);
359
376
  const handler3 = (() => {
360
377
  if ("handler" in fnImport) {
361
378
  return fnImport.handler;
@@ -367,17 +384,39 @@ var init_lambdaLoader = __esm({
367
384
  }
368
385
  return void 0;
369
386
  })();
370
- LAMBDA_FUNCTIONS[routeName] = handler3;
371
- if (!handler3) {
387
+ const cedarHandler = (() => {
388
+ if ("handleRequest" in fnImport && typeof fnImport.handleRequest === "function") {
389
+ return fnImport.handleRequest;
390
+ }
391
+ if ("default" in fnImport && fnImport.default && "handleRequest" in fnImport.default && typeof fnImport.default.handleRequest === "function") {
392
+ return fnImport.default.handleRequest;
393
+ }
394
+ return void 0;
395
+ })();
396
+ if (handler3) {
397
+ LAMBDA_FUNCTIONS.set(routeName, handler3);
398
+ }
399
+ if (cedarHandler) {
400
+ CEDAR_HANDLERS.set(routeName, cedarHandler);
401
+ } else if (handler3) {
402
+ CEDAR_HANDLERS.set(routeName, (0, import_runtime.wrapLegacyHandler)(handler3));
403
+ }
404
+ if (!handler3 && !cedarHandler) {
372
405
  console.warn(
373
406
  routeName,
374
407
  "at",
375
408
  fnPath,
376
- "does not have a function called handler defined."
409
+ "does not have a function called handler or handleRequest defined."
377
410
  );
378
411
  }
412
+ cedarRouteManifest.push({
413
+ path: routePath,
414
+ methods: routeName === "graphql" ? ["GET", "POST", "OPTIONS"] : ["GET", "POST"],
415
+ type: routeName === "graphql" ? "graphql" : routeName === "health" ? "health" : routeName.toLowerCase().includes("auth") ? "auth" : "function",
416
+ entry: fnPath
417
+ });
379
418
  console.log(
380
- import_ansis.default.magenta("/" + routeName),
419
+ (0, import_termi_link.terminalLink)(import_ansis.default.magenta("/" + routeName), (0, import_node_url.pathToFileURL)(fnPath).href),
381
420
  import_ansis.default.dim.italic(Date.now() - ts + " ms")
382
421
  );
383
422
  });
@@ -392,7 +431,7 @@ var init_lambdaLoader = __esm({
392
431
  options: options?.fastGlobOptions,
393
432
  discoverFunctionsGlob: options?.discoverFunctionsGlob
394
433
  });
395
- const i = serverFunctions.findIndex((x) => import_path2.default.basename(x) === "graphql.js");
434
+ const i = serverFunctions.findIndex((x) => import_node_path.default.basename(x) === "graphql.js");
396
435
  if (i >= 0) {
397
436
  const graphQLFn = serverFunctions.splice(i, 1)[0];
398
437
  serverFunctions.unshift(graphQLFn);
@@ -416,14 +455,43 @@ var init_lambdaLoader = __esm({
416
455
  };
417
456
  lambdaRequestHandler = async (req, reply) => {
418
457
  const { routeName } = req.params;
419
- if (!LAMBDA_FUNCTIONS[routeName]) {
458
+ const cedarHandlerCandidate = CEDAR_HANDLERS.get(routeName);
459
+ const cedarHandler = typeof cedarHandlerCandidate === "function" ? cedarHandlerCandidate : void 0;
460
+ if (cedarHandler) {
461
+ const requestBody = req.method === "GET" || req.method === "HEAD" ? void 0 : typeof req.rawBody === "string" ? req.rawBody : req.rawBody ? Buffer.from(req.rawBody).toString() : void 0;
462
+ const href = `${req.protocol}://${req.hostname}${req.raw.url ?? "/"}`;
463
+ const request = new Request(href, {
464
+ method: req.method,
465
+ headers: req.headers,
466
+ body: requestBody
467
+ });
468
+ const ctx = await (0, import_runtime.buildCedarContext)(request, {
469
+ params: {
470
+ routeName
471
+ }
472
+ });
473
+ const response = await cedarHandler(request, ctx);
474
+ reply.status(response.status);
475
+ response.headers.forEach((value, name) => {
476
+ reply.header(name, value);
477
+ });
478
+ const body = await response.arrayBuffer();
479
+ reply.send(Buffer.from(body));
480
+ return;
481
+ }
482
+ const handler3 = LAMBDA_FUNCTIONS.get(routeName);
483
+ if (handler3) {
484
+ return requestHandler(req, reply, handler3);
485
+ } else {
420
486
  const errorMessage = `Function "${routeName}" was not found.`;
421
487
  req.log.error(errorMessage);
422
488
  reply.status(404);
423
489
  if (process.env.NODE_ENV === "development") {
424
490
  const devError = {
425
491
  error: errorMessage,
426
- availableFunctions: Object.keys(LAMBDA_FUNCTIONS)
492
+ availableFunctions: [
493
+ .../* @__PURE__ */ new Set([...LAMBDA_FUNCTIONS.keys(), ...CEDAR_HANDLERS.keys()])
494
+ ]
427
495
  };
428
496
  reply.send(devError);
429
497
  } else {
@@ -431,7 +499,6 @@ var init_lambdaLoader = __esm({
431
499
  }
432
500
  return;
433
501
  }
434
- return requestHandler(req, reply, LAMBDA_FUNCTIONS[routeName]);
435
502
  };
436
503
  }
437
504
  });
@@ -506,7 +573,7 @@ async function redwoodFastifyGraphQLServer(fastify2, options) {
506
573
  cwd: (0, import_project_config4.getPaths)().api.base,
507
574
  absolute: true
508
575
  });
509
- const filePath = `file://${graphqlFunctionPath}`;
576
+ const filePath = (0, import_node_url2.pathToFileURL)(graphqlFunctionPath).href;
510
577
  const { __rw_graphqlOptions } = await import(filePath);
511
578
  if (!__rw_graphqlOptions) {
512
579
  return;
@@ -514,33 +581,28 @@ async function redwoodFastifyGraphQLServer(fastify2, options) {
514
581
  redwoodOptions.graphql = __rw_graphqlOptions;
515
582
  }
516
583
  const graphqlOptions = redwoodOptions.graphql;
517
- if (graphqlOptions?.realtime) {
518
- const { useCedarRealtime } = await import("@cedarjs/realtime");
519
- const originalExtraPlugins = graphqlOptions.extraPlugins ?? [];
520
- originalExtraPlugins.push(
521
- // This type cast is needed because useCedarRealtime returns an
522
- // EnvelopPlugin and here we need a YogaPlugin. I can't change the
523
- // return type of `useCedarRealtime` yet, because it'd be a breaking
524
- // change.
525
- useCedarRealtime(graphqlOptions.realtime)
526
- );
527
- graphqlOptions.extraPlugins = originalExtraPlugins;
528
- if (graphqlOptions.realtime.subscriptions) {
529
- method.push("PUT");
530
- }
584
+ if (graphqlOptions?.realtime?.subscriptions) {
585
+ method.push("PUT");
531
586
  }
532
- const { yoga } = (0, import_graphql_server.createGraphQLYoga)(graphqlOptions);
587
+ const { yoga } = await (0, import_graphql_server.createGraphQLYoga)(graphqlOptions);
533
588
  const graphqlEndpoint = trimSlashes(yoga.graphqlEndpoint);
534
589
  const routePaths = ["", "/health", "/readiness", "/stream"];
535
590
  for (const routePath of routePaths) {
536
591
  fastify2.route({
537
592
  url: `${redwoodOptions.apiRootPath}${graphqlEndpoint}${routePath}`,
538
593
  method,
539
- handler: (req, reply) => yoga.handleNodeRequestAndResponse(req, reply, {
540
- req,
541
- reply,
542
- event: lambdaEventForFastifyRequest(req)
543
- })
594
+ handler: async (req, _reply) => {
595
+ const request = createFetchRequest(req);
596
+ const cedarContext = await (0, import_runtime2.buildCedarContext)(request, {
597
+ authDecoder: graphqlOptions.authDecoder
598
+ });
599
+ return yoga.handle(request, {
600
+ request,
601
+ cedarContext,
602
+ event: lambdaEventForFastifyRequest(req),
603
+ requestContext: void 0
604
+ });
605
+ }
544
606
  });
545
607
  }
546
608
  fastify2.addHook("onReady", (done) => {
@@ -560,13 +622,24 @@ async function redwoodFastifyGraphQLServer(fastify2, options) {
560
622
  function trimSlashes(path5) {
561
623
  return path5.replace(/^\/|\/$/g, "");
562
624
  }
563
- var import_multipart, import_url_data2, import_fast_glob2, import_store3, import_helpers3, import_graphql_server, import_project_config4;
625
+ function createFetchRequest(req) {
626
+ const requestBody = req.method === "GET" || req.method === "HEAD" ? void 0 : typeof req.body === "string" ? req.body : req.body ? JSON.stringify(req.body) : void 0;
627
+ const href = `${req.protocol}://${req.hostname}${req.raw.url ?? "/"}`;
628
+ return new Request(href, {
629
+ method: req.method,
630
+ headers: req.headers,
631
+ body: requestBody
632
+ });
633
+ }
634
+ var import_node_url2, import_multipart, import_url_data2, import_fast_glob2, import_runtime2, import_store3, import_helpers3, import_graphql_server, import_project_config4;
564
635
  var init_graphql = __esm({
565
636
  "src/plugins/graphql.ts"() {
566
637
  "use strict";
638
+ import_node_url2 = require("node:url");
567
639
  import_multipart = __toESM(require("@fastify/multipart"), 1);
568
640
  import_url_data2 = __toESM(require("@fastify/url-data"), 1);
569
641
  import_fast_glob2 = __toESM(require("fast-glob"), 1);
642
+ import_runtime2 = require("@cedarjs/api/runtime");
570
643
  import_store3 = require("@cedarjs/context/dist/store");
571
644
  import_helpers3 = require("@cedarjs/fastify-web/dist/helpers.js");
572
645
  import_graphql_server = require("@cedarjs/graphql-server");
@@ -585,7 +658,7 @@ async function createServer(options = {}) {
585
658
  apiPort,
586
659
  apiHost
587
660
  } = resolveOptions(options);
588
- const serverConfigPath = import_path3.default.join(
661
+ const serverConfigPath = import_node_path2.default.join(
589
662
  (0, import_project_config5.getPaths)().base,
590
663
  (0, import_project_config5.getConfig)().api.serverConfig
591
664
  );
@@ -636,7 +709,7 @@ async function createServer(options = {}) {
636
709
  });
637
710
  if (graphqlFunctionPath) {
638
711
  const { redwoodFastifyGraphQLServer: redwoodFastifyGraphQLServer2 } = await Promise.resolve().then(() => (init_graphql(), graphql_exports));
639
- const { __rw_graphqlOptions } = await import(`file://${graphqlFunctionPath}`);
712
+ const { __rw_graphqlOptions } = await import((0, import_node_url3.pathToFileURL)(graphqlFunctionPath).href);
640
713
  await server.register(redwoodFastifyGraphQLServer2, {
641
714
  redwood: {
642
715
  apiRootPath,
@@ -665,12 +738,13 @@ async function createServer(options = {}) {
665
738
  };
666
739
  return server;
667
740
  }
668
- var import_node_fs2, import_path3, import_ansis2, import_dotenv_defaults, import_fast_glob3, import_fastify3, import_store4, import_project_config5;
741
+ var import_node_fs2, import_node_path2, import_node_url3, import_ansis2, import_dotenv_defaults, import_fast_glob3, import_fastify3, import_store4, import_project_config5;
669
742
  var init_createServer = __esm({
670
743
  "src/createServer.ts"() {
671
744
  "use strict";
672
745
  import_node_fs2 = __toESM(require("node:fs"), 1);
673
- import_path3 = __toESM(require("path"), 1);
746
+ import_node_path2 = __toESM(require("node:path"), 1);
747
+ import_node_url3 = require("node:url");
674
748
  import_ansis2 = __toESM(require("ansis"), 1);
675
749
  import_dotenv_defaults = require("dotenv-defaults");
676
750
  import_fast_glob3 = __toESM(require("fast-glob"), 1);
@@ -681,8 +755,8 @@ var init_createServer = __esm({
681
755
  init_api();
682
756
  if (!process.env.CEDAR_ENV_FILES_LOADED) {
683
757
  (0, import_dotenv_defaults.config)({
684
- path: import_path3.default.join((0, import_project_config5.getPaths)().base, ".env"),
685
- defaults: import_path3.default.join((0, import_project_config5.getPaths)().base, ".env.defaults"),
758
+ path: import_node_path2.default.join((0, import_project_config5.getPaths)().base, ".env"),
759
+ defaults: import_node_path2.default.join((0, import_project_config5.getPaths)().base, ".env.defaults"),
686
760
  multiline: true
687
761
  });
688
762
  process.env.CEDAR_ENV_FILES_LOADED = "true";
@@ -807,7 +881,7 @@ var init_bothCLIConfigHandler = __esm({
807
881
  });
808
882
 
809
883
  // src/bin.ts
810
- var import_path4 = __toESM(require("path"), 1);
884
+ var import_path2 = __toESM(require("path"), 1);
811
885
  var import_dotenv_defaults2 = require("dotenv-defaults");
812
886
  var import_helpers4 = require("yargs/helpers");
813
887
  var import_yargs = __toESM(require("yargs/yargs"), 1);
@@ -882,14 +956,14 @@ function builder2(yargs2) {
882
956
  init_bothCLIConfigHandler();
883
957
  if (!process.env.CEDAR_ENV_FILES_LOADED) {
884
958
  (0, import_dotenv_defaults2.config)({
885
- path: import_path4.default.join((0, import_project_config6.getPaths)().base, ".env"),
886
- defaults: import_path4.default.join((0, import_project_config6.getPaths)().base, ".env.defaults"),
959
+ path: import_path2.default.join((0, import_project_config6.getPaths)().base, ".env"),
960
+ defaults: import_path2.default.join((0, import_project_config6.getPaths)().base, ".env.defaults"),
887
961
  multiline: true
888
962
  });
889
963
  process.env.CEDAR_ENV_FILES_LOADED = "true";
890
964
  }
891
965
  process.env.NODE_ENV ??= "production";
892
- (0, import_yargs.default)((0, import_helpers4.hideBin)(process.argv)).scriptName("rw-server").strict().alias("h", "help").alias("v", "version").command(
966
+ (0, import_yargs.default)((0, import_helpers4.hideBin)(process.argv)).scriptName("cedar-server").strict().alias("h", "help").alias("v", "version").command(
893
967
  "$0",
894
968
  description2,
895
969
  // @ts-expect-error The yargs types seem wrong; it's ok for builder to be a function
@@ -1 +1 @@
1
- {"version":3,"file":"createServer.d.ts","sourceRoot":"","sources":["../../src/createServer.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EACV,mBAAmB,EACnB,MAAM,EAEP,MAAM,0BAA0B,CAAA;AAsBjC;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAsB,YAAY,CAAC,OAAO,GAAE,mBAAwB,mBAoHnE"}
1
+ {"version":3,"file":"createServer.d.ts","sourceRoot":"","sources":["../../src/createServer.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EACV,mBAAmB,EACnB,MAAM,EAEP,MAAM,0BAA0B,CAAA;AAsBjC;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAsB,YAAY,CAAC,OAAO,GAAE,mBAAwB,mBAoHnE"}