@fedify/koa 1.9.0-pr.455.1742
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 +20 -0
- package/README.md +35 -0
- package/dist/index.cjs +95 -0
- package/dist/index.d.cts +30 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +72 -0
- package/package.json +65 -0
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,35 @@
|
|
|
1
|
+
<!-- deno-fmt-ignore-file -->
|
|
2
|
+
|
|
3
|
+
@fedify/koa: Integrate Fedify with Koa
|
|
4
|
+
=======================================
|
|
5
|
+
|
|
6
|
+
[![npm][npm badge]][npm]
|
|
7
|
+
[![Matrix][Matrix badge]][Matrix]
|
|
8
|
+
[![Follow @fedify@hollo.social][@fedify@hollo.social badge]][@fedify@hollo.social]
|
|
9
|
+
|
|
10
|
+
This package provides a simple way to integrate [Fedify] with [Koa].
|
|
11
|
+
|
|
12
|
+
Supports Koa v2.x and v3.x.
|
|
13
|
+
|
|
14
|
+
The integration code looks like this:
|
|
15
|
+
|
|
16
|
+
~~~~ typescript
|
|
17
|
+
import Koa from "koa";
|
|
18
|
+
import { createMiddleware } from "@fedify/koa";
|
|
19
|
+
import { federation } from "./federation.ts"; // Your `Federation` instance
|
|
20
|
+
|
|
21
|
+
const app = new Koa();
|
|
22
|
+
|
|
23
|
+
app.proxy = true; // Trust proxy headers
|
|
24
|
+
|
|
25
|
+
app.use(createMiddleware(federation, (ctx) => "context data goes here"));
|
|
26
|
+
~~~~
|
|
27
|
+
|
|
28
|
+
[npm]: https://www.npmjs.com/package/@fedify/koa
|
|
29
|
+
[npm badge]: https://img.shields.io/npm/v/@fedify/koa?logo=npm
|
|
30
|
+
[Matrix]: https://matrix.to/#/#fedify:matrix.org
|
|
31
|
+
[Matrix badge]: https://img.shields.io/matrix/fedify%3Amatrix.org
|
|
32
|
+
[@fedify@hollo.social badge]: https://fedi-badge.deno.dev/@fedify@hollo.social/followers.svg
|
|
33
|
+
[@fedify@hollo.social]: https://hollo.social/@fedify
|
|
34
|
+
[Fedify]: https://fedify.dev/
|
|
35
|
+
[Koa]: https://koajs.com/
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
const node_stream = __toESM(require("node:stream"));
|
|
25
|
+
|
|
26
|
+
//#region src/index.ts
|
|
27
|
+
/**
|
|
28
|
+
* Create a Koa middleware to integrate with the {@link Federation} object.
|
|
29
|
+
*
|
|
30
|
+
* @template TContextData A type of the context data for the {@link Federation}
|
|
31
|
+
* object.
|
|
32
|
+
* @template TKoaContext A type of the Koa context.
|
|
33
|
+
* @param federation A {@link Federation} object to integrate with Koa.
|
|
34
|
+
* @param contextDataFactory A function to create a context data for the
|
|
35
|
+
* {@link Federation} object.
|
|
36
|
+
* @returns A Koa middleware.
|
|
37
|
+
* @since 1.9.0
|
|
38
|
+
*/
|
|
39
|
+
function createMiddleware(federation, contextDataFactory) {
|
|
40
|
+
return async (ctx, next) => {
|
|
41
|
+
const request = toRequest(ctx);
|
|
42
|
+
const contextData = await contextDataFactory(ctx);
|
|
43
|
+
let notFound = false;
|
|
44
|
+
let notAcceptable = false;
|
|
45
|
+
const response = await federation.fetch(request, {
|
|
46
|
+
contextData,
|
|
47
|
+
onNotFound: async () => {
|
|
48
|
+
notFound = true;
|
|
49
|
+
await next();
|
|
50
|
+
return new Response("Not found", { status: 404 });
|
|
51
|
+
},
|
|
52
|
+
onNotAcceptable: async () => {
|
|
53
|
+
notAcceptable = true;
|
|
54
|
+
await next();
|
|
55
|
+
return new Response("Not acceptable", {
|
|
56
|
+
status: 406,
|
|
57
|
+
headers: {
|
|
58
|
+
"Content-Type": "text/plain",
|
|
59
|
+
Vary: "Accept"
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
if (!notFound && !notAcceptable) setResponse(ctx, response);
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function toRequest(ctx) {
|
|
68
|
+
const url = `${ctx.protocol}://${ctx.host}${ctx.url}`;
|
|
69
|
+
const headers = new Headers();
|
|
70
|
+
for (const [key, value] of Object.entries(ctx.headers)) if (Array.isArray(value)) for (const v of value) headers.append(key, v);
|
|
71
|
+
else if (typeof value === "string") headers.append(key, value);
|
|
72
|
+
return new Request(url, {
|
|
73
|
+
method: ctx.method,
|
|
74
|
+
headers,
|
|
75
|
+
duplex: "half",
|
|
76
|
+
body: ctx.method === "GET" || ctx.method === "HEAD" ? void 0 : node_stream.Readable.toWeb(ctx.req)
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
function setResponse(ctx, response) {
|
|
80
|
+
ctx.status = response.status;
|
|
81
|
+
response.headers.forEach((value, key) => ctx.set(key, value));
|
|
82
|
+
if (response.body == null) return;
|
|
83
|
+
const body = response.body;
|
|
84
|
+
const reader = body.getReader();
|
|
85
|
+
ctx.body = new node_stream.Readable({ async read() {
|
|
86
|
+
const { done, value } = await reader.read();
|
|
87
|
+
if (done) {
|
|
88
|
+
this.push(null);
|
|
89
|
+
reader.releaseLock();
|
|
90
|
+
} else this.push(value);
|
|
91
|
+
} });
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
//#endregion
|
|
95
|
+
exports.createMiddleware = createMiddleware;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Federation } from "@fedify/fedify/federation";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A factory function to create context data for the {@link Federation} object.
|
|
7
|
+
*
|
|
8
|
+
* @template TContextData A type of the context data for the {@link Federation}
|
|
9
|
+
* object.
|
|
10
|
+
* @template TKoaContext A type of the Koa context.
|
|
11
|
+
* @param context A Koa context object.
|
|
12
|
+
* @returns A context data for the {@link Federation} object.
|
|
13
|
+
* @since 1.9.0
|
|
14
|
+
*/
|
|
15
|
+
type ContextDataFactory<TContextData, TKoaContext = any> = (context: TKoaContext) => TContextData | Promise<TContextData>;
|
|
16
|
+
/**
|
|
17
|
+
* Create a Koa middleware to integrate with the {@link Federation} object.
|
|
18
|
+
*
|
|
19
|
+
* @template TContextData A type of the context data for the {@link Federation}
|
|
20
|
+
* object.
|
|
21
|
+
* @template TKoaContext A type of the Koa context.
|
|
22
|
+
* @param federation A {@link Federation} object to integrate with Koa.
|
|
23
|
+
* @param contextDataFactory A function to create a context data for the
|
|
24
|
+
* {@link Federation} object.
|
|
25
|
+
* @returns A Koa middleware.
|
|
26
|
+
* @since 1.9.0
|
|
27
|
+
*/
|
|
28
|
+
declare function createMiddleware<TContextData, TKoaContext = any>(federation: Federation<TContextData>, contextDataFactory: ContextDataFactory<TContextData, TKoaContext>): (ctx: TKoaContext, next: () => Promise<void>) => Promise<void>;
|
|
29
|
+
//#endregion
|
|
30
|
+
export { ContextDataFactory, createMiddleware };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Federation } from "@fedify/fedify/federation";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A factory function to create context data for the {@link Federation} object.
|
|
7
|
+
*
|
|
8
|
+
* @template TContextData A type of the context data for the {@link Federation}
|
|
9
|
+
* object.
|
|
10
|
+
* @template TKoaContext A type of the Koa context.
|
|
11
|
+
* @param context A Koa context object.
|
|
12
|
+
* @returns A context data for the {@link Federation} object.
|
|
13
|
+
* @since 1.9.0
|
|
14
|
+
*/
|
|
15
|
+
type ContextDataFactory<TContextData, TKoaContext = any> = (context: TKoaContext) => TContextData | Promise<TContextData>;
|
|
16
|
+
/**
|
|
17
|
+
* Create a Koa middleware to integrate with the {@link Federation} object.
|
|
18
|
+
*
|
|
19
|
+
* @template TContextData A type of the context data for the {@link Federation}
|
|
20
|
+
* object.
|
|
21
|
+
* @template TKoaContext A type of the Koa context.
|
|
22
|
+
* @param federation A {@link Federation} object to integrate with Koa.
|
|
23
|
+
* @param contextDataFactory A function to create a context data for the
|
|
24
|
+
* {@link Federation} object.
|
|
25
|
+
* @returns A Koa middleware.
|
|
26
|
+
* @since 1.9.0
|
|
27
|
+
*/
|
|
28
|
+
declare function createMiddleware<TContextData, TKoaContext = any>(federation: Federation<TContextData>, contextDataFactory: ContextDataFactory<TContextData, TKoaContext>): (ctx: TKoaContext, next: () => Promise<void>) => Promise<void>;
|
|
29
|
+
//#endregion
|
|
30
|
+
export { ContextDataFactory, createMiddleware };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { Readable } from "node:stream";
|
|
2
|
+
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
/**
|
|
5
|
+
* Create a Koa middleware to integrate with the {@link Federation} object.
|
|
6
|
+
*
|
|
7
|
+
* @template TContextData A type of the context data for the {@link Federation}
|
|
8
|
+
* object.
|
|
9
|
+
* @template TKoaContext A type of the Koa context.
|
|
10
|
+
* @param federation A {@link Federation} object to integrate with Koa.
|
|
11
|
+
* @param contextDataFactory A function to create a context data for the
|
|
12
|
+
* {@link Federation} object.
|
|
13
|
+
* @returns A Koa middleware.
|
|
14
|
+
* @since 1.9.0
|
|
15
|
+
*/
|
|
16
|
+
function createMiddleware(federation, contextDataFactory) {
|
|
17
|
+
return async (ctx, next) => {
|
|
18
|
+
const request = toRequest(ctx);
|
|
19
|
+
const contextData = await contextDataFactory(ctx);
|
|
20
|
+
let notFound = false;
|
|
21
|
+
let notAcceptable = false;
|
|
22
|
+
const response = await federation.fetch(request, {
|
|
23
|
+
contextData,
|
|
24
|
+
onNotFound: async () => {
|
|
25
|
+
notFound = true;
|
|
26
|
+
await next();
|
|
27
|
+
return new Response("Not found", { status: 404 });
|
|
28
|
+
},
|
|
29
|
+
onNotAcceptable: async () => {
|
|
30
|
+
notAcceptable = true;
|
|
31
|
+
await next();
|
|
32
|
+
return new Response("Not acceptable", {
|
|
33
|
+
status: 406,
|
|
34
|
+
headers: {
|
|
35
|
+
"Content-Type": "text/plain",
|
|
36
|
+
Vary: "Accept"
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
if (!notFound && !notAcceptable) setResponse(ctx, response);
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function toRequest(ctx) {
|
|
45
|
+
const url = `${ctx.protocol}://${ctx.host}${ctx.url}`;
|
|
46
|
+
const headers = new Headers();
|
|
47
|
+
for (const [key, value] of Object.entries(ctx.headers)) if (Array.isArray(value)) for (const v of value) headers.append(key, v);
|
|
48
|
+
else if (typeof value === "string") headers.append(key, value);
|
|
49
|
+
return new Request(url, {
|
|
50
|
+
method: ctx.method,
|
|
51
|
+
headers,
|
|
52
|
+
duplex: "half",
|
|
53
|
+
body: ctx.method === "GET" || ctx.method === "HEAD" ? void 0 : Readable.toWeb(ctx.req)
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
function setResponse(ctx, response) {
|
|
57
|
+
ctx.status = response.status;
|
|
58
|
+
response.headers.forEach((value, key) => ctx.set(key, value));
|
|
59
|
+
if (response.body == null) return;
|
|
60
|
+
const body = response.body;
|
|
61
|
+
const reader = body.getReader();
|
|
62
|
+
ctx.body = new Readable({ async read() {
|
|
63
|
+
const { done, value } = await reader.read();
|
|
64
|
+
if (done) {
|
|
65
|
+
this.push(null);
|
|
66
|
+
reader.releaseLock();
|
|
67
|
+
} else this.push(value);
|
|
68
|
+
} });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
//#endregion
|
|
72
|
+
export { createMiddleware };
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fedify/koa",
|
|
3
|
+
"version": "1.9.0-pr.455.1742+fb29545a",
|
|
4
|
+
"description": "Integrate Fedify with Koa",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"Fedify",
|
|
7
|
+
"Koa",
|
|
8
|
+
"ActivityPub",
|
|
9
|
+
"Fediverse"
|
|
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/koa"
|
|
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
|
+
"module": "./dist/index.js",
|
|
32
|
+
"main": "./dist/index.cjs",
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"exports": {
|
|
35
|
+
".": {
|
|
36
|
+
"types": {
|
|
37
|
+
"import": "./dist/index.d.ts",
|
|
38
|
+
"require": "./dist/index.d.cts",
|
|
39
|
+
"default": "./dist/index.d.ts"
|
|
40
|
+
},
|
|
41
|
+
"import": "./dist/index.js",
|
|
42
|
+
"require": "./dist/index.cjs",
|
|
43
|
+
"default": "./dist/index.js"
|
|
44
|
+
},
|
|
45
|
+
"./package.json": "./package.json"
|
|
46
|
+
},
|
|
47
|
+
"files": [
|
|
48
|
+
"dist/",
|
|
49
|
+
"package.json"
|
|
50
|
+
],
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"koa": "^2.0.0 || ^3.0.0",
|
|
53
|
+
"@fedify/fedify": "^1.9.0-pr.455.1742+fb29545a"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@types/koa": "^2.15.0",
|
|
57
|
+
"@types/node": "^22.16.0",
|
|
58
|
+
"tsdown": "^0.12.9",
|
|
59
|
+
"typescript": "^5.9.2"
|
|
60
|
+
},
|
|
61
|
+
"scripts": {
|
|
62
|
+
"build": "tsdown",
|
|
63
|
+
"prepublish": "tsdown"
|
|
64
|
+
}
|
|
65
|
+
}
|