@fedify/hono 1.9.0-dev.1461

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 ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright 2024–2025 Hong Minhee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,65 @@
1
+ <!-- deno-fmt-ignore-file -->
2
+
3
+ @fedify/hono: Integrate Fedify with Hono
4
+ ========================================
5
+
6
+ [![JSR][JSR badge]][JSR]
7
+ [![npm][npm badge]][npm]
8
+ [![Follow @fedify@hollo.social][@fedify@hollo.social badge]][@fedify@hollo.social]
9
+
10
+ *This package is available since Fedify 1.9.0.*
11
+
12
+ This package provides a simple way to integrate [Fedify] with [Hono].
13
+
14
+ The integration code looks like this:
15
+
16
+ ~~~~ typescript
17
+ import { createFederation } from "@fedify/fedify";
18
+ import { federation } from "@fedify/hono";
19
+ import { Hono } from "hono";
20
+
21
+ const fedi = createFederation<string>({
22
+ // Omitted for brevity; see the related section for details.
23
+ });
24
+
25
+ const app = new Hono();
26
+ app.use(federation(fedi, (ctx) => "context data"));
27
+ ~~~~
28
+
29
+ How it works
30
+ ------------
31
+
32
+ Fedify behaves as a middleware that wraps around the Hono request handler.
33
+ The middleware intercepts the incoming HTTP requests and dispatches them to
34
+ the appropriate handler based on the request path and the `Accept` header
35
+ (i.e., content negotiation). This architecture allows Fedify and your Hono
36
+ application to coexist in the same domain and port.
37
+
38
+ For example, if you make a request to */.well-known/webfinger* Fedify will
39
+ handle the request by itself, but if you make a request to */users/alice*
40
+ (assuming your Hono app has a handler for `/users/:handle`) with `Accept:
41
+ text/html` header, Fedify will dispatch the request to the Hono app's
42
+ appropriate handler for `/users/:handle`. Or if you define an actor dispatcher
43
+ for `/users/{handle}` in Fedify, and the request is made with `Accept:
44
+ application/activity+json` header, Fedify will dispatch the request to the
45
+ appropriate actor dispatcher.
46
+
47
+ Installation
48
+ ------------
49
+
50
+ ~~~~ sh
51
+ deno add jsr:@fedify/hono # Deno
52
+ npm add @fedify/hono # npm
53
+ pnpm add @fedify/hono # pnpm
54
+ yarn add @fedify/hono # Yarn
55
+ bun add @fedify/hono # Bun
56
+ ~~~~
57
+
58
+ [JSR]: https://jsr.io/@fedify/hono
59
+ [JSR badge]: https://jsr.io/badges/@fedify/hono
60
+ [npm]: https://www.npmjs.com/package/@fedify/hono
61
+ [npm badge]: https://img.shields.io/npm/v/@fedify/hono?logo=npm
62
+ [@fedify@hollo.social badge]: https://fedi-badge.deno.dev/@fedify@hollo.social/followers.svg
63
+ [@fedify@hollo.social]: https://hollo.social/@fedify
64
+ [Fedify]: https://fedify.dev/
65
+ [Hono]: https://hono.dev/
package/dist/mod.d.ts ADDED
@@ -0,0 +1,39 @@
1
+ import { Federation } from "@fedify/fedify/federation";
2
+
3
+ //#region src/mod.d.ts
4
+
5
+ interface HonoRequest {
6
+ raw: Request;
7
+ }
8
+ interface HonoContext {
9
+ req: HonoRequest;
10
+ res: Response;
11
+ }
12
+ type HonoMiddleware<THonoContext extends HonoContext> = (ctx: THonoContext, next: () => Promise<void>) => Promise<Response | void>;
13
+ /**
14
+ * A factory function to create a context data for the {@link Federation}
15
+ * object.
16
+ *
17
+ * @template TContextData A type of the context data for the {@link Federation}
18
+ * object.
19
+ * @template THonoContext A type of the Hono context.
20
+ * @param context A Hono context object.
21
+ * @returns A context data for the {@link Federation} object.
22
+ * @since 1.9.0
23
+ */
24
+ type ContextDataFactory<TContextData, THonoContext> = (context: THonoContext) => TContextData | Promise<TContextData>;
25
+ /**
26
+ * Create a Hono middleware to integrate with the {@link Federation} object.
27
+ *
28
+ * @template TContextData A type of the context data for the {@link Federation}
29
+ * object.
30
+ * @template THonoContext A type of the Hono context.
31
+ * @param federation A {@link Federation} object to integrate with Hono.
32
+ * @param contextDataFactory A function to create a context data for the
33
+ * {@link Federation} object.
34
+ * @returns A Hono middleware.
35
+ * @since 1.9.0
36
+ */
37
+ declare function federation<TContextData, THonoContext extends HonoContext>(federation: Federation<TContextData>, contextDataFactory: ContextDataFactory<TContextData, THonoContext>): HonoMiddleware<THonoContext>;
38
+ //#endregion
39
+ export { ContextDataFactory, federation };
package/dist/mod.js ADDED
@@ -0,0 +1,45 @@
1
+ //#region src/mod.ts
2
+ /**
3
+ * Create a Hono middleware to integrate with the {@link Federation} object.
4
+ *
5
+ * @template TContextData A type of the context data for the {@link Federation}
6
+ * object.
7
+ * @template THonoContext A type of the Hono context.
8
+ * @param federation A {@link Federation} object to integrate with Hono.
9
+ * @param contextDataFactory A function to create a context data for the
10
+ * {@link Federation} object.
11
+ * @returns A Hono middleware.
12
+ * @since 1.9.0
13
+ */
14
+ function federation(federation$1, contextDataFactory) {
15
+ return async (ctx, next) => {
16
+ let contextData = contextDataFactory(ctx);
17
+ if (contextData instanceof Promise) contextData = await contextData;
18
+ return await federation$1.fetch(ctx.req.raw, {
19
+ contextData,
20
+ ...integrateFetchOptions(ctx, next)
21
+ });
22
+ };
23
+ }
24
+ function integrateFetchOptions(ctx, next) {
25
+ return {
26
+ async onNotFound(_req) {
27
+ await next();
28
+ return ctx.res;
29
+ },
30
+ async onNotAcceptable(_req) {
31
+ await next();
32
+ if (ctx.res.status !== 404) return ctx.res;
33
+ return new Response("Not acceptable", {
34
+ status: 406,
35
+ headers: {
36
+ "Content-Type": "text/plain",
37
+ Vary: "Accept"
38
+ }
39
+ });
40
+ }
41
+ };
42
+ }
43
+
44
+ //#endregion
45
+ export { federation };
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "@fedify/hono",
3
+ "version": "1.9.0-dev.1461+7957aafe",
4
+ "description": "Integrate Fedify with Hono",
5
+ "keywords": [
6
+ "Fedify",
7
+ "ActivityPub",
8
+ "Fediverse",
9
+ "Hono"
10
+ ],
11
+ "author": {
12
+ "name": "Hong Minhee",
13
+ "email": "hong@minhee.org",
14
+ "url": "https://hongminhee.org/"
15
+ },
16
+ "homepage": "https://fedify.dev/",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/fedify-dev/fedify.git",
20
+ "directory": "packages/hono"
21
+ },
22
+ "license": "MIT",
23
+ "bugs": {
24
+ "url": "https://github.com/fedify-dev/fedify/issues"
25
+ },
26
+ "funding": [
27
+ "https://opencollective.com/fedify",
28
+ "https://github.com/sponsors/dahlia"
29
+ ],
30
+ "type": "module",
31
+ "main": "./dist/mod.js",
32
+ "module": "./dist/mod.js",
33
+ "types": "./dist/mod.d.ts",
34
+ "exports": {
35
+ ".": {
36
+ "require": {
37
+ "types": "./dist/mod.d.ts",
38
+ "import": "./dist/mod.js",
39
+ "default": "./dist/mod.js"
40
+ },
41
+ "import": {
42
+ "types": "./dist/mod.d.ts",
43
+ "import": "./dist/mod.js",
44
+ "default": "./dist/mod.js"
45
+ }
46
+ },
47
+ "./package.json": "./package.json"
48
+ },
49
+ "files": [
50
+ "dist/",
51
+ "package.json"
52
+ ],
53
+ "peerDependencies": {
54
+ "hono": "^4.0.0",
55
+ "@fedify/fedify": "1.9.0-dev.1461+7957aafe"
56
+ },
57
+ "devDependencies": {
58
+ "tsdown": "^0.12.9",
59
+ "typescript": "^5.9.2"
60
+ },
61
+ "scripts": {
62
+ "build": "tsdown",
63
+ "prepublish": "tsdown",
64
+ "test": "deno task codegen && tsdown && cd dist/ && node --test"
65
+ }
66
+ }