@fedify/elysia 2.3.12 → 2.3.13
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 +117 -27
- package/dist/index.js +117 -27
- package/package.json +7 -4
package/dist/index.cjs
CHANGED
|
@@ -1,36 +1,126 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
//#region src/index.ts
|
|
3
3
|
const fedify = (federation, contextDataFactory) => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
4
|
+
const notAcceptableRequests = /* @__PURE__ */ new WeakSet();
|
|
5
|
+
return (app) => {
|
|
6
|
+
observeApplicationParents(app);
|
|
7
|
+
return app.decorate("federation", federation).onRequest(async ({ request, set, federation }) => {
|
|
8
|
+
notAcceptableRequests.delete(request);
|
|
9
|
+
let notFound = false;
|
|
10
|
+
let notAcceptable = false;
|
|
11
|
+
const contextData = await contextDataFactory(request);
|
|
12
|
+
const response = await federation.fetch(request, {
|
|
13
|
+
contextData,
|
|
14
|
+
onNotFound: () => {
|
|
15
|
+
notFound = true;
|
|
16
|
+
return new Response("Not found", { status: 404 });
|
|
17
|
+
},
|
|
18
|
+
onNotAcceptable: () => {
|
|
19
|
+
notAcceptable = true;
|
|
20
|
+
notAcceptableRequests.add(request);
|
|
21
|
+
return new Response("Not acceptable", {
|
|
22
|
+
status: 406,
|
|
23
|
+
headers: {
|
|
24
|
+
"Content-Type": "text/plain",
|
|
25
|
+
Vary: "Accept"
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
if (!notFound && !notAcceptable) {
|
|
31
|
+
set.status = response.status;
|
|
32
|
+
if (response.body) return response;
|
|
33
|
+
response.headers.forEach((value, key) => {
|
|
34
|
+
set.headers[key] = value;
|
|
22
35
|
});
|
|
36
|
+
return new Response(null, { status: response.status });
|
|
37
|
+
}
|
|
38
|
+
}).onTransform(({ request }) => {
|
|
39
|
+
notAcceptableRequests.delete(request);
|
|
40
|
+
}).onError(async function notAcceptableFallback(context) {
|
|
41
|
+
const { request, code, set } = context;
|
|
42
|
+
if (code !== "NOT_FOUND" || !notAcceptableRequests.has(request) || context.route || context.error.message !== "NOT_FOUND") return;
|
|
43
|
+
notAcceptableRequests.delete(request);
|
|
44
|
+
const activeApp = findApplication(app, context.store);
|
|
45
|
+
const hooks = activeApp.event.error ?? [];
|
|
46
|
+
const index = hooks.findIndex((hook) => hook.fn === notAcceptableFallback);
|
|
47
|
+
for (const hook of index < 0 ? [] : hooks.slice(index + 1)) {
|
|
48
|
+
const response = await hook.fn(context);
|
|
49
|
+
if (activeApp.config.aot === false ? response != null : response !== void 0) return response;
|
|
23
50
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
51
|
+
const vary = [];
|
|
52
|
+
for (const header of Object.keys(set.headers)) {
|
|
53
|
+
const name = header.toLowerCase();
|
|
54
|
+
if (name === "vary") vary.push(...String(set.headers[header]).split(","));
|
|
55
|
+
if ([
|
|
56
|
+
"content-type",
|
|
57
|
+
"vary",
|
|
58
|
+
"content-encoding",
|
|
59
|
+
"content-language",
|
|
60
|
+
"content-range",
|
|
61
|
+
"content-length",
|
|
62
|
+
"etag",
|
|
63
|
+
"last-modified",
|
|
64
|
+
"transfer-encoding"
|
|
65
|
+
].includes(name)) delete set.headers[header];
|
|
66
|
+
}
|
|
67
|
+
const fields = vary.map((field) => field.trim()).filter(Boolean);
|
|
68
|
+
if (!fields.some((field) => field === "*" || field.toLowerCase() === "accept")) fields.push("Accept");
|
|
69
|
+
set.status = 406;
|
|
70
|
+
return new Response("Not acceptable", {
|
|
71
|
+
status: 406,
|
|
72
|
+
headers: {
|
|
73
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
74
|
+
Vary: fields.join(", ")
|
|
75
|
+
}
|
|
30
76
|
});
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}).as("global");
|
|
77
|
+
}).as("global");
|
|
78
|
+
};
|
|
34
79
|
};
|
|
80
|
+
const applicationParents = /* @__PURE__ */ new WeakMap();
|
|
81
|
+
function observeApplicationParents(app) {
|
|
82
|
+
if (applicationParents.has(app)) return;
|
|
83
|
+
const parents = /* @__PURE__ */ new Set();
|
|
84
|
+
applicationParents.set(app, parents);
|
|
85
|
+
let getParent = app.getParent;
|
|
86
|
+
const rememberParent = () => {
|
|
87
|
+
const parent = getParent?.call(app);
|
|
88
|
+
if (parent == null || parent === app) return;
|
|
89
|
+
for (const reference of parents) {
|
|
90
|
+
const value = reference.deref();
|
|
91
|
+
if (value === parent) return;
|
|
92
|
+
if (value == null) parents.delete(reference);
|
|
93
|
+
}
|
|
94
|
+
parents.add(new WeakRef(parent));
|
|
95
|
+
observeApplicationParents(parent);
|
|
96
|
+
};
|
|
97
|
+
rememberParent();
|
|
98
|
+
Object.defineProperty(app, "getParent", {
|
|
99
|
+
configurable: true,
|
|
100
|
+
enumerable: true,
|
|
101
|
+
get: () => getParent,
|
|
102
|
+
set: (value) => {
|
|
103
|
+
getParent = value;
|
|
104
|
+
rememberParent();
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
function findApplication(app, store) {
|
|
109
|
+
const pending = [app];
|
|
110
|
+
const visited = /* @__PURE__ */ new Set();
|
|
111
|
+
while (pending.length > 0) {
|
|
112
|
+
const candidate = pending.pop();
|
|
113
|
+
if (visited.has(candidate)) continue;
|
|
114
|
+
visited.add(candidate);
|
|
115
|
+
if (candidate.singleton.store === store) return candidate;
|
|
116
|
+
const parents = applicationParents.get(candidate);
|
|
117
|
+
for (const reference of parents ?? []) {
|
|
118
|
+
const parent = reference.deref();
|
|
119
|
+
if (parent == null) parents?.delete(reference);
|
|
120
|
+
else pending.push(parent);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return app;
|
|
124
|
+
}
|
|
35
125
|
//#endregion
|
|
36
126
|
exports.fedify = fedify;
|
package/dist/index.js
CHANGED
|
@@ -1,35 +1,125 @@
|
|
|
1
1
|
//#region src/index.ts
|
|
2
2
|
const fedify = (federation, contextDataFactory) => {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
3
|
+
const notAcceptableRequests = /* @__PURE__ */ new WeakSet();
|
|
4
|
+
return (app) => {
|
|
5
|
+
observeApplicationParents(app);
|
|
6
|
+
return app.decorate("federation", federation).onRequest(async ({ request, set, federation }) => {
|
|
7
|
+
notAcceptableRequests.delete(request);
|
|
8
|
+
let notFound = false;
|
|
9
|
+
let notAcceptable = false;
|
|
10
|
+
const contextData = await contextDataFactory(request);
|
|
11
|
+
const response = await federation.fetch(request, {
|
|
12
|
+
contextData,
|
|
13
|
+
onNotFound: () => {
|
|
14
|
+
notFound = true;
|
|
15
|
+
return new Response("Not found", { status: 404 });
|
|
16
|
+
},
|
|
17
|
+
onNotAcceptable: () => {
|
|
18
|
+
notAcceptable = true;
|
|
19
|
+
notAcceptableRequests.add(request);
|
|
20
|
+
return new Response("Not acceptable", {
|
|
21
|
+
status: 406,
|
|
22
|
+
headers: {
|
|
23
|
+
"Content-Type": "text/plain",
|
|
24
|
+
Vary: "Accept"
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
if (!notFound && !notAcceptable) {
|
|
30
|
+
set.status = response.status;
|
|
31
|
+
if (response.body) return response;
|
|
32
|
+
response.headers.forEach((value, key) => {
|
|
33
|
+
set.headers[key] = value;
|
|
21
34
|
});
|
|
35
|
+
return new Response(null, { status: response.status });
|
|
36
|
+
}
|
|
37
|
+
}).onTransform(({ request }) => {
|
|
38
|
+
notAcceptableRequests.delete(request);
|
|
39
|
+
}).onError(async function notAcceptableFallback(context) {
|
|
40
|
+
const { request, code, set } = context;
|
|
41
|
+
if (code !== "NOT_FOUND" || !notAcceptableRequests.has(request) || context.route || context.error.message !== "NOT_FOUND") return;
|
|
42
|
+
notAcceptableRequests.delete(request);
|
|
43
|
+
const activeApp = findApplication(app, context.store);
|
|
44
|
+
const hooks = activeApp.event.error ?? [];
|
|
45
|
+
const index = hooks.findIndex((hook) => hook.fn === notAcceptableFallback);
|
|
46
|
+
for (const hook of index < 0 ? [] : hooks.slice(index + 1)) {
|
|
47
|
+
const response = await hook.fn(context);
|
|
48
|
+
if (activeApp.config.aot === false ? response != null : response !== void 0) return response;
|
|
22
49
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
50
|
+
const vary = [];
|
|
51
|
+
for (const header of Object.keys(set.headers)) {
|
|
52
|
+
const name = header.toLowerCase();
|
|
53
|
+
if (name === "vary") vary.push(...String(set.headers[header]).split(","));
|
|
54
|
+
if ([
|
|
55
|
+
"content-type",
|
|
56
|
+
"vary",
|
|
57
|
+
"content-encoding",
|
|
58
|
+
"content-language",
|
|
59
|
+
"content-range",
|
|
60
|
+
"content-length",
|
|
61
|
+
"etag",
|
|
62
|
+
"last-modified",
|
|
63
|
+
"transfer-encoding"
|
|
64
|
+
].includes(name)) delete set.headers[header];
|
|
65
|
+
}
|
|
66
|
+
const fields = vary.map((field) => field.trim()).filter(Boolean);
|
|
67
|
+
if (!fields.some((field) => field === "*" || field.toLowerCase() === "accept")) fields.push("Accept");
|
|
68
|
+
set.status = 406;
|
|
69
|
+
return new Response("Not acceptable", {
|
|
70
|
+
status: 406,
|
|
71
|
+
headers: {
|
|
72
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
73
|
+
Vary: fields.join(", ")
|
|
74
|
+
}
|
|
29
75
|
});
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}).as("global");
|
|
76
|
+
}).as("global");
|
|
77
|
+
};
|
|
33
78
|
};
|
|
79
|
+
const applicationParents = /* @__PURE__ */ new WeakMap();
|
|
80
|
+
function observeApplicationParents(app) {
|
|
81
|
+
if (applicationParents.has(app)) return;
|
|
82
|
+
const parents = /* @__PURE__ */ new Set();
|
|
83
|
+
applicationParents.set(app, parents);
|
|
84
|
+
let getParent = app.getParent;
|
|
85
|
+
const rememberParent = () => {
|
|
86
|
+
const parent = getParent?.call(app);
|
|
87
|
+
if (parent == null || parent === app) return;
|
|
88
|
+
for (const reference of parents) {
|
|
89
|
+
const value = reference.deref();
|
|
90
|
+
if (value === parent) return;
|
|
91
|
+
if (value == null) parents.delete(reference);
|
|
92
|
+
}
|
|
93
|
+
parents.add(new WeakRef(parent));
|
|
94
|
+
observeApplicationParents(parent);
|
|
95
|
+
};
|
|
96
|
+
rememberParent();
|
|
97
|
+
Object.defineProperty(app, "getParent", {
|
|
98
|
+
configurable: true,
|
|
99
|
+
enumerable: true,
|
|
100
|
+
get: () => getParent,
|
|
101
|
+
set: (value) => {
|
|
102
|
+
getParent = value;
|
|
103
|
+
rememberParent();
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
function findApplication(app, store) {
|
|
108
|
+
const pending = [app];
|
|
109
|
+
const visited = /* @__PURE__ */ new Set();
|
|
110
|
+
while (pending.length > 0) {
|
|
111
|
+
const candidate = pending.pop();
|
|
112
|
+
if (visited.has(candidate)) continue;
|
|
113
|
+
visited.add(candidate);
|
|
114
|
+
if (candidate.singleton.store === store) return candidate;
|
|
115
|
+
const parents = applicationParents.get(candidate);
|
|
116
|
+
for (const reference of parents ?? []) {
|
|
117
|
+
const parent = reference.deref();
|
|
118
|
+
if (parent == null) parents?.delete(reference);
|
|
119
|
+
else pending.push(parent);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return app;
|
|
123
|
+
}
|
|
34
124
|
//#endregion
|
|
35
125
|
export { fedify };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fedify/elysia",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.13",
|
|
4
4
|
"description": "Integrate Fedify with Elysia",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Fedify",
|
|
@@ -47,16 +47,19 @@
|
|
|
47
47
|
],
|
|
48
48
|
"peerDependencies": {
|
|
49
49
|
"elysia": "^1.3.6",
|
|
50
|
-
"@fedify/fedify": "^2.3.
|
|
50
|
+
"@fedify/fedify": "^2.3.13"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"bun-types": "^1.2.19",
|
|
54
54
|
"tsdown": "^0.22.0",
|
|
55
|
-
"typescript": "^6.0.0"
|
|
55
|
+
"typescript": "^6.0.0",
|
|
56
|
+
"@fedify/fixture": "2.0.0"
|
|
56
57
|
},
|
|
57
58
|
"scripts": {
|
|
58
59
|
"build:self": "tsdown",
|
|
59
60
|
"build": "pnpm --filter @fedify/elysia... run build:self",
|
|
60
|
-
"prepublish": "pnpm build"
|
|
61
|
+
"prepublish": "pnpm build",
|
|
62
|
+
"test": "node --experimental-transform-types --test",
|
|
63
|
+
"test:bun": "bun test"
|
|
61
64
|
}
|
|
62
65
|
}
|