@krate/runtime 0.5.0-beta.2 → 0.5.0-beta.3

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.
@@ -326,9 +326,15 @@ export function createKrateHandler(options) {
326
326
  const p = rs.path;
327
327
  if (p.startsWith("/__krate/") && !p.startsWith("/__krate/action/"))
328
328
  return null;
329
- const mod = modules.middleware;
330
- if (!data.hasMiddleware || !mod || typeof mod.middleware !== "function")
329
+ if (!data.hasMiddleware)
331
330
  return null;
331
+ const mod = modules.middleware;
332
+ // The named export, then the default one, as the other runtimes do. The app
333
+ // has middleware, so a module with no handler must not let requests through.
334
+ const handler = typeof mod?.middleware === "function" ? mod.middleware : mod?.default;
335
+ if (typeof handler !== "function") {
336
+ return middlewareFailed(rs, new Error("the middleware module does not export a middleware function"));
337
+ }
332
338
  let matchers;
333
339
  try {
334
340
  matchers = middlewareMatchers();
@@ -343,7 +349,7 @@ export function createKrateHandler(options) {
343
349
  let result;
344
350
  try {
345
351
  const request = attachCookies(new Request(fullURL(rs), { method: rs.method, headers: new Headers(rs.request.headers) }));
346
- result = await mod.middleware(request);
352
+ result = await handler(request);
347
353
  }
348
354
  catch (e) {
349
355
  return middlewareFailed(rs, e);
@@ -520,3 +520,16 @@ describe("createKrateHandler with a shared cache adapter", () => {
520
520
  expect(renders).toBe(2);
521
521
  });
522
522
  });
523
+ describe("middleware exports", () => {
524
+ const deny = () => new Response("no", { status: 403 });
525
+ const run = (middleware) => createKrateHandler({ target: "node", data: data(), modules: { ...modules, middleware } })(new Request("http://localhost/live"));
526
+ it("runs a default export", async () => {
527
+ expect((await run({ default: deny })).status).toBe(403);
528
+ });
529
+ it("fails closed when the module has no handler", async () => {
530
+ const error = vi.spyOn(console, "error").mockImplementation(() => { });
531
+ expect((await run({ other: 1 })).status).toBe(503);
532
+ expect((await run(undefined)).status).toBe(503);
533
+ error.mockRestore();
534
+ });
535
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@krate/runtime",
3
- "version": "0.5.0-beta.2",
3
+ "version": "0.5.0-beta.3",
4
4
  "description": "Client-side runtime for Krate - signals, hydration, SPA router, resources, and context.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",