Skip to content

Commit d8d823a

Browse files
authored
refactor waku internals (#2115)
1 parent 521bc76 commit d8d823a

16 files changed

Lines changed: 103 additions & 42 deletions

File tree

packages/waku/src/adapters/aws-lambda.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@ import { unstable_createServerEntryAdapter as createServerEntryAdapter } from 'w
88
import {
99
unstable_constants as constants,
1010
unstable_honoMiddleware as honoMiddleware,
11+
unstable_runWithContext as runWithContext,
1112
} from 'waku/internals';
1213
import type { BuildOptions } from './aws-lambda-build-enhancer.js';
1314

1415
const { DIST_PUBLIC } = constants;
15-
const { contextMiddleware, rscMiddleware, middlewareRunner } = honoMiddleware;
16+
const { rscMiddleware, middlewareRunner } = honoMiddleware;
17+
18+
function contextMiddleware(): MiddlewareHandler {
19+
return (c, next) => {
20+
const req = c.req.raw;
21+
return runWithContext(req, next);
22+
};
23+
}
1624

1725
const DEFAULT_BODY_LIMIT_MAX_SIZE = 100 * 1024 * 1024;
1826

packages/waku/src/adapters/bun.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,19 @@ import { unstable_createServerEntryAdapter as createServerEntryAdapter } from 'w
77
import {
88
unstable_constants as constants,
99
unstable_honoMiddleware as honoMiddleware,
10+
unstable_runWithContext as runWithContext,
1011
} from 'waku/internals';
1112
import type { BuildOptions } from './bun-build-enhancer.js';
1213

1314
const { DIST_PUBLIC } = constants;
14-
const { contextMiddleware, rscMiddleware, middlewareRunner } = honoMiddleware;
15+
const { rscMiddleware, middlewareRunner } = honoMiddleware;
16+
17+
function contextMiddleware(): MiddlewareHandler {
18+
return (c, next) => {
19+
const req = c.req.raw;
20+
return runWithContext(req, next);
21+
};
22+
}
1523

1624
const DEFAULT_BODY_LIMIT_MAX_SIZE = 100 * 1024 * 1024;
1725

packages/waku/src/adapters/cloudflare.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,19 @@ import {
1010
unstable_consumeMultiplexedStream as consumeMultiplexedStream,
1111
unstable_honoMiddleware as honoMiddleware,
1212
unstable_produceMultiplexedStream as produceMultiplexedStream,
13+
unstable_runWithContext as runWithContext,
1314
} from 'waku/internals';
1415
import type { BuildOptions } from './cloudflare-build-enhancer.js';
1516

1617
const { DIST_PUBLIC } = constants;
17-
const { contextMiddleware, rscMiddleware, middlewareRunner } = honoMiddleware;
18+
const { rscMiddleware, middlewareRunner } = honoMiddleware;
19+
20+
function contextMiddleware(): MiddlewareHandler {
21+
return (c, next) => {
22+
const req = c.req.raw;
23+
return runWithContext(req, next);
24+
};
25+
}
1826

1927
const DEFAULT_BODY_LIMIT_MAX_SIZE = 100 * 1024 * 1024;
2028

packages/waku/src/adapters/deno.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,19 @@ import { unstable_createServerEntryAdapter as createServerEntryAdapter } from 'w
77
import {
88
unstable_constants as constants,
99
unstable_honoMiddleware as honoMiddleware,
10+
unstable_runWithContext as runWithContext,
1011
} from 'waku/internals';
1112
import type { BuildOptions } from './deno-build-enhancer.js';
1213

1314
const { DIST_PUBLIC } = constants;
14-
const { contextMiddleware, rscMiddleware, middlewareRunner } = honoMiddleware;
15+
const { rscMiddleware, middlewareRunner } = honoMiddleware;
16+
17+
function contextMiddleware(): MiddlewareHandler {
18+
return (c, next) => {
19+
const req = c.req.raw;
20+
return runWithContext(req, next);
21+
};
22+
}
1523

1624
const DEFAULT_BODY_LIMIT_MAX_SIZE = 100 * 1024 * 1024;
1725

packages/waku/src/adapters/edge.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,25 @@ import { bodyLimit } from 'hono/body-limit';
33
import { Hono } from 'hono/tiny';
44
import type { ImportGlobFunction } from 'vite/types/importGlob.d.ts';
55
import { unstable_createServerEntryAdapter as createServerEntryAdapter } from 'waku/adapter-builders';
6-
import { unstable_honoMiddleware as honoMiddleware } from 'waku/internals';
6+
import {
7+
unstable_honoMiddleware as honoMiddleware,
8+
unstable_runWithContext as runWithContext,
9+
} from 'waku/internals';
710

811
declare global {
912
interface ImportMeta {
1013
glob: ImportGlobFunction;
1114
}
1215
}
1316

14-
const { contextMiddleware, rscMiddleware, middlewareRunner } = honoMiddleware;
17+
const { rscMiddleware, middlewareRunner } = honoMiddleware;
18+
19+
function contextMiddleware(): MiddlewareHandler {
20+
return (c, next) => {
21+
const req = c.req.raw;
22+
return runWithContext(req, next);
23+
};
24+
}
1525

1626
const DEFAULT_BODY_LIMIT_MAX_SIZE = 100 * 1024 * 1024;
1727

packages/waku/src/adapters/netlify.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@ import { unstable_createServerEntryAdapter as createServerEntryAdapter } from 'w
55
import {
66
unstable_constants as constants,
77
unstable_honoMiddleware as honoMiddleware,
8+
unstable_runWithContext as runWithContext,
89
} from 'waku/internals';
910
import type { BuildOptions } from './netlify-build-enhancer.js';
1011

1112
const { DIST_PUBLIC } = constants;
12-
const { contextMiddleware, rscMiddleware, middlewareRunner } = honoMiddleware;
13+
const { rscMiddleware, middlewareRunner } = honoMiddleware;
14+
15+
function contextMiddleware(): MiddlewareHandler {
16+
return (c, next) => {
17+
const req = c.req.raw;
18+
return runWithContext(req, next);
19+
};
20+
}
1321

1422
const DEFAULT_BODY_LIMIT_MAX_SIZE = 100 * 1024 * 1024;
1523

packages/waku/src/adapters/node.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@ import { unstable_createServerEntryAdapter as createServerEntryAdapter } from 'w
88
import {
99
unstable_constants as constants,
1010
unstable_honoMiddleware as honoMiddleware,
11+
unstable_runWithContext as runWithContext,
1112
} from 'waku/internals';
1213
import type { BuildOptions } from './node-build-enhancer.js';
1314

1415
const { DIST_PUBLIC } = constants;
15-
const { contextMiddleware, rscMiddleware, middlewareRunner } = honoMiddleware;
16+
const { rscMiddleware, middlewareRunner } = honoMiddleware;
17+
18+
function contextMiddleware(): MiddlewareHandler {
19+
return (c, next) => {
20+
const req = c.req.raw;
21+
return runWithContext(req, next);
22+
};
23+
}
1624

1725
const DEFAULT_BODY_LIMIT_MAX_SIZE = 100 * 1024 * 1024;
1826

packages/waku/src/adapters/vercel.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@ import { unstable_createServerEntryAdapter as createServerEntryAdapter } from 'w
66
import {
77
unstable_constants as constants,
88
unstable_honoMiddleware as honoMiddleware,
9+
unstable_runWithContext as runWithContext,
910
} from 'waku/internals';
1011
import type { BuildOptions } from './vercel-build-enhancer.js';
1112

1213
const { DIST_PUBLIC } = constants;
13-
const { contextMiddleware, rscMiddleware, middlewareRunner } = honoMiddleware;
14+
const { rscMiddleware, middlewareRunner } = honoMiddleware;
15+
16+
function contextMiddleware(): MiddlewareHandler {
17+
return (c, next) => {
18+
const req = c.req.raw;
19+
return runWithContext(req, next);
20+
};
21+
}
1422

1523
const DEFAULT_BODY_LIMIT_MAX_SIZE = 100 * 1024 * 1024;
1624
(global as any).__WAKU_HONO_NODE_SERVER_GET_REQUEST_LISTENER__ =

packages/waku/src/internals.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export {
55
produceMultiplexedStream as unstable_produceMultiplexedStream,
66
consumeMultiplexedStream as unstable_consumeMultiplexedStream,
77
} from './lib/utils/stream.js';
8+
export { runWithContext as unstable_runWithContext } from './lib/context.js';

packages/waku/src/lib/context.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ type Context = {
88

99
const contextStorage = new AsyncLocalStorage<Context>();
1010

11-
export function INTERNAL_runWithContext<T>(req: Request, next: () => T): T {
11+
/**
12+
* This is an internal function and not for public use.
13+
*/
14+
export function runWithContext<T>(req: Request, next: () => T): T {
1215
const context: Context = {
1316
req,
1417
nonce: undefined,
@@ -20,9 +23,7 @@ export function INTERNAL_runWithContext<T>(req: Request, next: () => T): T {
2023
export function getContext() {
2124
const context = contextStorage.getStore();
2225
if (!context) {
23-
throw new Error(
24-
'Context is not available. Make sure to use the context middleware.',
25-
);
26+
throw new Error('Context is not available.');
2627
}
2728
return context;
2829
}

0 commit comments

Comments
 (0)