@fedify/express 2.0.0-pr.412.1559 → 2.0.0-pr.412.1794
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/dist/index.cjs +99 -0
- package/dist/index.d.cts +9 -0
- package/package.json +11 -7
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
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_buffer = __toESM(require("node:buffer"));
|
|
25
|
+
const node_stream = __toESM(require("node:stream"));
|
|
26
|
+
|
|
27
|
+
//#region src/index.ts
|
|
28
|
+
function integrateFederation(federation, contextDataFactory) {
|
|
29
|
+
return (req, res, next) => {
|
|
30
|
+
const request = fromERequest(req);
|
|
31
|
+
const contextData = contextDataFactory(req);
|
|
32
|
+
const contextDataPromise = contextData instanceof Promise ? contextData : Promise.resolve(contextData);
|
|
33
|
+
contextDataPromise.then(async (contextData$1) => {
|
|
34
|
+
let notFound = false;
|
|
35
|
+
let notAcceptable = false;
|
|
36
|
+
const response = await federation.fetch(request, {
|
|
37
|
+
contextData: contextData$1,
|
|
38
|
+
onNotFound: () => {
|
|
39
|
+
notFound = true;
|
|
40
|
+
next();
|
|
41
|
+
return new Response("Not found", { status: 404 });
|
|
42
|
+
},
|
|
43
|
+
onNotAcceptable: () => {
|
|
44
|
+
notAcceptable = true;
|
|
45
|
+
next();
|
|
46
|
+
return new Response("Not acceptable", {
|
|
47
|
+
status: 406,
|
|
48
|
+
headers: {
|
|
49
|
+
"Content-Type": "text/plain",
|
|
50
|
+
Vary: "Accept"
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
if (notFound || notAcceptable && req.route != null) return;
|
|
56
|
+
await setEResponse(res, response);
|
|
57
|
+
res.end();
|
|
58
|
+
res.status = () => res;
|
|
59
|
+
res.send = () => res;
|
|
60
|
+
res.end = () => res;
|
|
61
|
+
res.json = () => res;
|
|
62
|
+
res.removeHeader = () => res;
|
|
63
|
+
res.setHeader = () => res;
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function fromERequest(req) {
|
|
68
|
+
const url = `${req.protocol}://${req.header("Host") ?? req.hostname}${req.url}`;
|
|
69
|
+
const headers = new Headers();
|
|
70
|
+
for (const [key, value] of Object.entries(req.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: req.method,
|
|
74
|
+
headers,
|
|
75
|
+
duplex: "half",
|
|
76
|
+
body: req.method === "GET" || req.method === "HEAD" ? void 0 : node_stream.Readable.toWeb(req)
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
function setEResponse(res, response) {
|
|
80
|
+
res.status(response.status);
|
|
81
|
+
response.headers.forEach((value, key) => res.setHeader(key, value));
|
|
82
|
+
if (response.body == null) return Promise.resolve();
|
|
83
|
+
const body = response.body;
|
|
84
|
+
return new Promise((resolve) => {
|
|
85
|
+
const reader = body.getReader();
|
|
86
|
+
reader.read().then(function read({ done, value }) {
|
|
87
|
+
if (done) {
|
|
88
|
+
reader.releaseLock();
|
|
89
|
+
resolve();
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
res.write(node_buffer.Buffer.from(value));
|
|
93
|
+
reader.read().then(read);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
//#endregion
|
|
99
|
+
exports.integrateFederation = integrateFederation;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Federation } from "@fedify/fedify";
|
|
2
|
+
import { NextFunction, Request, Response } from "express";
|
|
3
|
+
|
|
4
|
+
//#region src/index.d.ts
|
|
5
|
+
type Middleware = (req: Request, res: Response, next: NextFunction) => void;
|
|
6
|
+
type ContextDataFactory<TContextData> = (req: Request) => TContextData | Promise<TContextData>;
|
|
7
|
+
declare function integrateFederation<TContextData>(federation: Federation<TContextData>, contextDataFactory: ContextDataFactory<TContextData>): Middleware;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { ContextDataFactory, integrateFederation };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fedify/express",
|
|
3
|
-
"version": "2.0.0-pr.412.
|
|
3
|
+
"version": "2.0.0-pr.412.1794+5c393341",
|
|
4
4
|
"description": "Integrate Fedify with Express",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Fedify",
|
|
@@ -28,14 +28,18 @@
|
|
|
28
28
|
],
|
|
29
29
|
"type": "module",
|
|
30
30
|
"module": "./dist/index.js",
|
|
31
|
+
"main": "./dist/index.cjs",
|
|
31
32
|
"types": "./dist/index.d.ts",
|
|
32
33
|
"exports": {
|
|
33
34
|
".": {
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"default": "./dist/index.
|
|
38
|
-
}
|
|
35
|
+
"types": {
|
|
36
|
+
"import": "./dist/index.d.ts",
|
|
37
|
+
"require": "./dist/index.d.cts",
|
|
38
|
+
"default": "./dist/index.d.ts"
|
|
39
|
+
},
|
|
40
|
+
"import": "./dist/index.js",
|
|
41
|
+
"require": "./dist/index.cjs",
|
|
42
|
+
"default": "./dist/index.js"
|
|
39
43
|
},
|
|
40
44
|
"./package.json": "./package.json"
|
|
41
45
|
},
|
|
@@ -45,7 +49,7 @@
|
|
|
45
49
|
],
|
|
46
50
|
"peerDependencies": {
|
|
47
51
|
"express": "^4.0.0",
|
|
48
|
-
"@fedify/fedify": "2.0.0-pr.412.
|
|
52
|
+
"@fedify/fedify": "^2.0.0-pr.412.1794+5c393341"
|
|
49
53
|
},
|
|
50
54
|
"devDependencies": {
|
|
51
55
|
"@types/express": "^4.0.0",
|