Skip to content

Commit

Permalink
feat: universal-middleware now targets cloudflare-pages and cloudflar…
Browse files Browse the repository at this point in the history
…e-worker
  • Loading branch information
magne4000 committed Aug 29, 2024
1 parent d143533 commit d9b56f5
Show file tree
Hide file tree
Showing 32 changed files with 706 additions and 560 deletions.
2 changes: 2 additions & 0 deletions .idea/universal-handler.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export default defineConfig({
],
},
title: "universal-middleware",
description: "Write middleware once, target Hono, Express, Fastify, and more",
description:
"Write middleware once, target Hono, Express, Cloudflare, and more",
themeConfig: {
logo: {
light: "/logo.png",
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/packaging.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ The bundler plugin also accepts the following options:
```ts
universalMiddleware({
// Only generate files for selected servers. All enabled by default
servers?: ('hono' | 'express' | 'hattip' | 'fastify' | 'h3' | 'webroute')[];
servers?: ('hono' | 'express' | 'hattip' | 'fastify' | 'h3' | 'webroute' | 'cloudflare')[];
// akin to esbuild `entryNames` for generated "exports" in package.json
serversExportNames?: string;
// akin to ebsuild `entryNames` for generated "exports" in package.json
Expand Down
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ layout: home
hero:
name: "universal-middleware"
text:
tagline: "Write middleware once, target Hono, Express, Fastify, and more!"
tagline: "Write middleware once, target Hono, Express, Cloudflare, and more!"
actions:
- theme: brand
text: Guide
Expand All @@ -19,7 +19,7 @@ features:
details: Automatically generate compatible middlewares for popular servers, from a single codebase
link: /guide/introduction
- title: Support for popular servers
details: Bundle your middlewares for Hono, Express, Fastify, h3, Hattip and Webroute
details: Bundle your middlewares for Hono, Express, Fastify, Cloudflare, h3, Hattip and Webroute
link: /reference/supported-adapters
- title: Web Standards Compliance
details: Write middlewares following established web standards (WinterCG, WHATWG) to ensure consistency and future-proofing
Expand Down
1 change: 1 addition & 0 deletions docs/reference/supported-adapters.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ Supports the following server adapters:
- [Webroute](https://github.com/magne4000/universal-middleware/tree/main/packages/adapter-webroute)
- [Fastify](https://github.com/magne4000/universal-middleware/tree/main/packages/adapter-fastify)
- [h3](https://github.com/magne4000/universal-middleware/tree/main/packages/adapter-h3)
- [Cloudflare](https://github.com/magne4000/universal-middleware/tree/main/packages/adapter-cloudflare)
- TODO: elysia
63 changes: 62 additions & 1 deletion examples/tool/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,66 @@
"types": "./dist/middlewares/universal-h3-middleware-guard.middleware.d.ts",
"import": "./dist/middlewares/universal-h3-middleware-guard.middleware.js",
"default": "./dist/middlewares/universal-h3-middleware-guard.middleware.js"
},
"./dummy-handler-cloudflare": {
"types": "./dist/handlers/universal-cloudflare-handler-handler.d.ts",
"import": "./dist/handlers/universal-cloudflare-handler-handler.js",
"default": "./dist/handlers/universal-cloudflare-handler-handler.js"
},
"./middlewares/context-middleware-cloudflare": {
"types": "./dist/middlewares/universal-cloudflare-middleware-context.middleware.d.ts",
"import": "./dist/middlewares/universal-cloudflare-middleware-context.middleware.js",
"default": "./dist/middlewares/universal-cloudflare-middleware-context.middleware.js"
},
"./middlewares/headers-middleware-cloudflare": {
"types": "./dist/middlewares/universal-cloudflare-middleware-headers.middleware.d.ts",
"import": "./dist/middlewares/universal-cloudflare-middleware-headers.middleware.js",
"default": "./dist/middlewares/universal-cloudflare-middleware-headers.middleware.js"
},
"./middlewares/guard-middleware-cloudflare": {
"types": "./dist/middlewares/universal-cloudflare-middleware-guard.middleware.d.ts",
"import": "./dist/middlewares/universal-cloudflare-middleware-guard.middleware.js",
"default": "./dist/middlewares/universal-cloudflare-middleware-guard.middleware.js"
},
"./dummy-handler-cloudflare-worker": {
"types": "./dist/handlers/universal-cloudflare-worker-handler-handler.d.ts",
"import": "./dist/handlers/universal-cloudflare-worker-handler-handler.js",
"default": "./dist/handlers/universal-cloudflare-worker-handler-handler.js"
},
"./dummy-handler-cloudflare-pages": {
"types": "./dist/handlers/universal-cloudflare-pages-handler-handler.d.ts",
"import": "./dist/handlers/universal-cloudflare-pages-handler-handler.js",
"default": "./dist/handlers/universal-cloudflare-pages-handler-handler.js"
},
"./middlewares/context-middleware-cloudflare-worker": {
"types": "./dist/middlewares/universal-cloudflare-worker-middleware-context.middleware.d.ts",
"import": "./dist/middlewares/universal-cloudflare-worker-middleware-context.middleware.js",
"default": "./dist/middlewares/universal-cloudflare-worker-middleware-context.middleware.js"
},
"./middlewares/context-middleware-cloudflare-pages": {
"types": "./dist/middlewares/universal-cloudflare-pages-middleware-context.middleware.d.ts",
"import": "./dist/middlewares/universal-cloudflare-pages-middleware-context.middleware.js",
"default": "./dist/middlewares/universal-cloudflare-pages-middleware-context.middleware.js"
},
"./middlewares/headers-middleware-cloudflare-worker": {
"types": "./dist/middlewares/universal-cloudflare-worker-middleware-headers.middleware.d.ts",
"import": "./dist/middlewares/universal-cloudflare-worker-middleware-headers.middleware.js",
"default": "./dist/middlewares/universal-cloudflare-worker-middleware-headers.middleware.js"
},
"./middlewares/headers-middleware-cloudflare-pages": {
"types": "./dist/middlewares/universal-cloudflare-pages-middleware-headers.middleware.d.ts",
"import": "./dist/middlewares/universal-cloudflare-pages-middleware-headers.middleware.js",
"default": "./dist/middlewares/universal-cloudflare-pages-middleware-headers.middleware.js"
},
"./middlewares/guard-middleware-cloudflare-worker": {
"types": "./dist/middlewares/universal-cloudflare-worker-middleware-guard.middleware.d.ts",
"import": "./dist/middlewares/universal-cloudflare-worker-middleware-guard.middleware.js",
"default": "./dist/middlewares/universal-cloudflare-worker-middleware-guard.middleware.js"
},
"./middlewares/guard-middleware-cloudflare-pages": {
"types": "./dist/middlewares/universal-cloudflare-pages-middleware-guard.middleware.d.ts",
"import": "./dist/middlewares/universal-cloudflare-pages-middleware-guard.middleware.js",
"default": "./dist/middlewares/universal-cloudflare-pages-middleware-guard.middleware.js"
}
},
"author": "Joël Charles <[email protected]>",
Expand All @@ -171,7 +231,8 @@
"@universal-middleware/hono": "^0",
"@universal-middleware/webroute": "^0",
"@universal-middleware/fastify": "^0",
"@universal-middleware/h3": "^0"
"@universal-middleware/h3": "^0",
"@universal-middleware/cloudflare": "^0"
},
"sideEffects": false,
"peerDependencies": {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@
"@hattip/router": "^0.0.47",
"@hono/node-server": "^1.12.0",
"express": "^4.19.2",
"h3": "~1.12.0",
"h3": "^1.12.0",
"hattip": "^0.0.33",
"helmet": "^7.1.0",
"hono": "^4.5.6",
"serve-static": "^1.15.0",
"tsx": "^4.17.0",
"tsx": "^4.19.0",
"vite": "^5.4.1"
},
"pnpm": {
Expand All @@ -63,6 +63,7 @@
"@universal-middleware/webroute": "link:./packages/adapter-webroute",
"@universal-middleware/fastify": "link:./packages/adapter-fastify",
"@universal-middleware/h3": "link:./packages/adapter-h3",
"@universal-middleware/cloudflare": "link:./packages/adapter-cloudflare",
"universal-middleware": "link:./packages/universal-middleware",
"@webroute/middleware": "^0.9.0",
"esbuild": "^0.23.1"
Expand Down
8 changes: 4 additions & 4 deletions packages/adapter-cloudflare/functions/_middleware.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { middlewares } from "@universal-middleware/tests/utils";
import { createPageFunction } from "../src/index.js";
import { createPagesFunction } from "../src/index.js";

export const onRequest = [
createPageFunction(middlewares[0])(),
createPageFunction(middlewares[1])(),
createPageFunction(middlewares[2])(),
createPagesFunction(middlewares[0])(),
createPagesFunction(middlewares[1])(),
createPagesFunction(middlewares[2])(),
];
4 changes: 2 additions & 2 deletions packages/adapter-cloudflare/functions/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createPageFunction } from "../src/index.js";
import { createPagesFunction } from "../src/index.js";
import { handler } from "@universal-middleware/tests/utils";

export const onRequest = createPageFunction(handler)();
export const onRequest = createPagesFunction(handler)();
4 changes: 2 additions & 2 deletions packages/adapter-cloudflare/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@universal-middleware/cloudflare",
"version": "0.2.3",
"version": "0.1.0",
"type": "module",
"description": "Cloudflare Pages adapter for universal middlewares",
"files": [
Expand Down Expand Up @@ -35,7 +35,7 @@
"@universal-middleware/tests": "workspace:*",
"rimraf": "^6.0.0",
"tsup": "^8.2.4",
"tsx": "^4.17.0",
"tsx": "^4.19.0",
"typescript": "^5.5.4",
"vitest": "^2.0.5",
"wrangler": "^3.72.3"
Expand Down
32 changes: 11 additions & 21 deletions packages/adapter-cloudflare/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export type CloudflareHandler<C extends Universal.Context> = {
}>;
};

export type CloudflarePagesFunction<C extends Universal.Context> =
PagesFunction<{
[contextSymbol]: C;
}>;

export const contextSymbol = Symbol("unContext");

/**
Expand Down Expand Up @@ -52,41 +57,26 @@ export function createHandler<T extends unknown[], C extends Universal.Context>(
/**
* Creates a function handler for Cloudflare Pages
*/
export function createPageFunction<
export function createPagesFunction<
T extends unknown[],
InContext extends Universal.Context,
>(
middlewareFactory: Get<T, UniversalHandler<InContext>>,
): Get<
T,
PagesFunction<{
[contextSymbol]: InContext;
}>
>;
export function createPageFunction<
): Get<T, CloudflarePagesFunction<InContext>>;
export function createPagesFunction<
T extends unknown[],
InContext extends Universal.Context,
OutContext extends Universal.Context,
>(
middlewareFactory: Get<T, UniversalMiddleware<InContext, OutContext>>,
): Get<
T,
PagesFunction<{
[contextSymbol]: InContext;
}>
>;
export function createPageFunction<
): Get<T, CloudflarePagesFunction<InContext>>;
export function createPagesFunction<
T extends unknown[],
InContext extends Universal.Context,
OutContext extends Universal.Context,
>(
middlewareFactory: Get<T, UniversalMiddleware<InContext, OutContext>>,
): Get<
T,
PagesFunction<{
[contextSymbol]: InContext;
}>
> {
): Get<T, CloudflarePagesFunction<InContext>> {
return (...args) => {
const middleware = middlewareFactory(...args);

Expand Down
3 changes: 2 additions & 1 deletion packages/adapter-cloudflare/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export {
createHandler,
createPageFunction,
createPagesFunction,
getContext,
type CloudflareHandler,
type CloudflarePagesFunction,
} from "./common.js";
2 changes: 1 addition & 1 deletion packages/adapter-express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"helmet": "^7.1.0",
"rimraf": "^6.0.0",
"tsup": "^8.2.4",
"tsx": "^4.17.0",
"tsx": "^4.19.0",
"typescript": "^5.5.4",
"vitest": "^2.0.5"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"fastify-plugin": "^4.5.1",
"rimraf": "^6.0.0",
"tsup": "^8.2.4",
"tsx": "^4.17.0",
"tsx": "^4.19.0",
"typescript": "^5.5.4",
"vitest": "^2.0.5"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-h3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"h3": "^1.12.0",
"rimraf": "^6.0.0",
"tsup": "^8.2.4",
"tsx": "^4.17.0",
"tsx": "^4.19.0",
"typescript": "^5.5.4",
"vitest": "^2.0.5"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-hattip/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@universal-middleware/tests": "workspace:*",
"rimraf": "^6.0.0",
"tsup": "^8.2.4",
"tsx": "^4.17.0",
"tsx": "^4.19.0",
"typescript": "^5.5.4",
"vitest": "^2.0.5",
"wrangler": "^3.72.3"
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-hono/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"hono": "^4.5.6",
"rimraf": "^6.0.0",
"tsup": "^8.2.4",
"tsx": "^4.17.0",
"tsx": "^4.19.0",
"typescript": "^5.5.4",
"vitest": "^2.0.5",
"wrangler": "^3.72.3"
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-webroute/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"hono": "^4.5.6",
"rimraf": "^6.0.0",
"tsup": "^8.2.4",
"tsx": "^4.17.0",
"tsx": "^4.19.0",
"typescript": "^5.5.4",
"vitest": "^2.0.5"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/universal-middleware/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"hono-middleware",
"hattip-middleware",
"webroute-middleware",
"cloudflare-middleware",
"fastify-middleware",
"h3-middleware",
"universal-server",
Expand Down Expand Up @@ -71,6 +72,7 @@
"@universal-middleware/hono": "^0.2.3",
"@universal-middleware/webroute": "^0.2.2",
"@universal-middleware/fastify": "^0.2.2",
"@universal-middleware/cloudflare": "^0.1.0",
"oxc-transform": "^0.24.3",
"package-up": "^5.0.0",
"unplugin": "^1.12.2"
Expand Down
Loading

0 comments on commit d9b56f5

Please sign in to comment.