fix(patches): restore PeerTube View patches and add fetch diagnostic
Re-add PeerTube View activity patches that were prematurely removed in
e52e98c5c — the upstream fork doesn't reliably include these fixes on
all server deployments, causing noisy "Unsupported activity type" errors.
Also add fetch diagnostic patch to surface the real cause of
"TypeError: fetch failed" when posting articles via the form controller.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -4,8 +4,8 @@
|
|||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"postinstall": "xattr -w com.apple.fileprovider.ignore#P 1 node_modules 2>/dev/null || true && node scripts/patch-lightningcss.mjs && node scripts/patch-endpoint-media-scope.mjs && node scripts/patch-endpoint-media-sharp-runtime.mjs && node scripts/patch-frontend-sharp-runtime.mjs && node scripts/patch-endpoint-files-upload-route.mjs && node scripts/patch-endpoint-files-upload-locales.mjs && node scripts/patch-endpoint-activitypub-locales.mjs && node scripts/patch-endpoint-homepage-locales.mjs && node scripts/patch-endpoint-homepage-identity-defaults.mjs && node scripts/patch-federation-unlisted-guards.mjs && node scripts/patch-endpoint-micropub-where-note-visibility.mjs && node scripts/patch-endpoint-posts-ai-fields.mjs && node scripts/patch-endpoint-posts-ai-cleanup.mjs && node scripts/patch-endpoint-podroll-opml-upload.mjs && node scripts/patch-preset-eleventy-ai-frontmatter.mjs && node scripts/patch-micropub-ai-block-resync.mjs && node scripts/patch-frontend-serviceworker-file.mjs && node scripts/patch-endpoint-comments-locales.mjs && node scripts/patch-endpoint-posts-locales.mjs && node scripts/patch-endpoint-conversations-locales.mjs && node scripts/patch-conversations-collection-guards.mjs && node scripts/patch-indiekit-routes-rate-limits.mjs && node scripts/patch-indiekit-error-production-stack.mjs && node scripts/patch-indieauth-devmode-guard.mjs && node scripts/patch-listening-endpoint-runtime-guards.mjs && node scripts/patch-endpoint-github-changelog-categories.mjs && node scripts/patch-endpoint-blogroll-feeds-alias.mjs && node scripts/patch-endpoint-posts-uid-lookup.mjs && node scripts/patch-endpoint-posts-prefill-url.mjs && node scripts/patch-conversations-bluesky-self-filter.mjs && node scripts/patch-conversations-bluesky-cursor-fix.mjs && node scripts/patch-endpoint-micropub-source-filter.mjs && node scripts/patch-endpoint-posts-search-tags.mjs && node scripts/patch-syndicate-force-checked-default.mjs && node scripts/patch-ap-url-lookup-api.mjs && node scripts/patch-ap-allow-private-address.mjs && node scripts/patch-ap-normalize-nested-tags.mjs",
|
"postinstall": "xattr -w com.apple.fileprovider.ignore#P 1 node_modules 2>/dev/null || true && node scripts/patch-lightningcss.mjs && node scripts/patch-endpoint-media-scope.mjs && node scripts/patch-endpoint-media-sharp-runtime.mjs && node scripts/patch-frontend-sharp-runtime.mjs && node scripts/patch-endpoint-files-upload-route.mjs && node scripts/patch-endpoint-files-upload-locales.mjs && node scripts/patch-endpoint-activitypub-locales.mjs && node scripts/patch-endpoint-homepage-locales.mjs && node scripts/patch-endpoint-homepage-identity-defaults.mjs && node scripts/patch-federation-unlisted-guards.mjs && node scripts/patch-endpoint-micropub-where-note-visibility.mjs && node scripts/patch-endpoint-posts-ai-fields.mjs && node scripts/patch-endpoint-posts-ai-cleanup.mjs && node scripts/patch-endpoint-podroll-opml-upload.mjs && node scripts/patch-preset-eleventy-ai-frontmatter.mjs && node scripts/patch-micropub-ai-block-resync.mjs && node scripts/patch-frontend-serviceworker-file.mjs && node scripts/patch-endpoint-comments-locales.mjs && node scripts/patch-endpoint-posts-locales.mjs && node scripts/patch-endpoint-conversations-locales.mjs && node scripts/patch-conversations-collection-guards.mjs && node scripts/patch-indiekit-routes-rate-limits.mjs && node scripts/patch-indiekit-error-production-stack.mjs && node scripts/patch-indieauth-devmode-guard.mjs && node scripts/patch-listening-endpoint-runtime-guards.mjs && node scripts/patch-endpoint-github-changelog-categories.mjs && node scripts/patch-endpoint-blogroll-feeds-alias.mjs && node scripts/patch-endpoint-posts-uid-lookup.mjs && node scripts/patch-endpoint-posts-prefill-url.mjs && node scripts/patch-conversations-bluesky-self-filter.mjs && node scripts/patch-conversations-bluesky-cursor-fix.mjs && node scripts/patch-endpoint-micropub-source-filter.mjs && node scripts/patch-endpoint-posts-search-tags.mjs && node scripts/patch-syndicate-force-checked-default.mjs && node scripts/patch-ap-url-lookup-api.mjs && node scripts/patch-ap-allow-private-address.mjs && node scripts/patch-inbox-skip-view-activity-parse.mjs && node scripts/patch-inbox-ignore-view-activity.mjs && node scripts/patch-endpoint-posts-fetch-diagnostic.mjs && node scripts/patch-ap-normalize-nested-tags.mjs",
|
||||||
"serve":"export NODE_ENV=${NODE_ENV:-production} INDIEKIT_DEBUG=${INDIEKIT_DEBUG:-0} && node scripts/preflight-production-security.mjs && node scripts/preflight-mongo-connection.mjs && node scripts/preflight-activitypub-rsa-key.mjs && node scripts/preflight-activitypub-profile-urls.mjs && node scripts/patch-lightningcss.mjs && node scripts/patch-endpoint-media-scope.mjs && node scripts/patch-endpoint-media-sharp-runtime.mjs && node scripts/patch-frontend-sharp-runtime.mjs && node scripts/patch-endpoint-files-upload-route.mjs && node scripts/patch-endpoint-files-upload-locales.mjs && node scripts/patch-endpoint-activitypub-locales.mjs && node scripts/patch-endpoint-homepage-locales.mjs && node scripts/patch-endpoint-homepage-identity-defaults.mjs && node scripts/patch-federation-unlisted-guards.mjs && node scripts/patch-endpoint-micropub-where-note-visibility.mjs && node scripts/patch-endpoint-posts-ai-fields.mjs && node scripts/patch-endpoint-posts-ai-cleanup.mjs && node scripts/patch-endpoint-podroll-opml-upload.mjs && node scripts/patch-preset-eleventy-ai-frontmatter.mjs && node scripts/patch-micropub-ai-block-resync.mjs && node scripts/patch-frontend-serviceworker-file.mjs && node scripts/patch-endpoint-comments-locales.mjs && node scripts/patch-endpoint-posts-locales.mjs && node scripts/patch-endpoint-conversations-locales.mjs && node scripts/patch-conversations-collection-guards.mjs && node scripts/patch-indiekit-routes-rate-limits.mjs && node scripts/patch-indiekit-error-production-stack.mjs && node scripts/patch-indieauth-devmode-guard.mjs && node scripts/patch-listening-endpoint-runtime-guards.mjs && node scripts/patch-endpoint-github-changelog-categories.mjs && node scripts/patch-microsub-reader-ap-dispatch.mjs && node scripts/patch-endpoint-blogroll-feeds-alias.mjs && node scripts/patch-endpoint-posts-uid-lookup.mjs && node scripts/patch-endpoint-posts-prefill-url.mjs && node scripts/patch-conversations-bluesky-self-filter.mjs && node scripts/patch-conversations-bluesky-cursor-fix.mjs && node scripts/patch-endpoint-micropub-source-filter.mjs && node scripts/patch-endpoint-posts-search-tags.mjs && node scripts/patch-syndicate-force-checked-default.mjs && node scripts/patch-ap-url-lookup-api.mjs && node scripts/patch-ap-allow-private-address.mjs && node scripts/patch-ap-normalize-nested-tags.mjs && node node_modules/@indiekit/indiekit/bin/cli.js serve --config indiekit.config.mjs",
|
"serve":"export NODE_ENV=${NODE_ENV:-production} INDIEKIT_DEBUG=${INDIEKIT_DEBUG:-0} && node scripts/preflight-production-security.mjs && node scripts/preflight-mongo-connection.mjs && node scripts/preflight-activitypub-rsa-key.mjs && node scripts/preflight-activitypub-profile-urls.mjs && node scripts/patch-lightningcss.mjs && node scripts/patch-endpoint-media-scope.mjs && node scripts/patch-endpoint-media-sharp-runtime.mjs && node scripts/patch-frontend-sharp-runtime.mjs && node scripts/patch-endpoint-files-upload-route.mjs && node scripts/patch-endpoint-files-upload-locales.mjs && node scripts/patch-endpoint-activitypub-locales.mjs && node scripts/patch-endpoint-homepage-locales.mjs && node scripts/patch-endpoint-homepage-identity-defaults.mjs && node scripts/patch-federation-unlisted-guards.mjs && node scripts/patch-endpoint-micropub-where-note-visibility.mjs && node scripts/patch-endpoint-posts-ai-fields.mjs && node scripts/patch-endpoint-posts-ai-cleanup.mjs && node scripts/patch-endpoint-podroll-opml-upload.mjs && node scripts/patch-preset-eleventy-ai-frontmatter.mjs && node scripts/patch-micropub-ai-block-resync.mjs && node scripts/patch-frontend-serviceworker-file.mjs && node scripts/patch-endpoint-comments-locales.mjs && node scripts/patch-endpoint-posts-locales.mjs && node scripts/patch-endpoint-conversations-locales.mjs && node scripts/patch-conversations-collection-guards.mjs && node scripts/patch-indiekit-routes-rate-limits.mjs && node scripts/patch-indiekit-error-production-stack.mjs && node scripts/patch-indieauth-devmode-guard.mjs && node scripts/patch-listening-endpoint-runtime-guards.mjs && node scripts/patch-endpoint-github-changelog-categories.mjs && node scripts/patch-microsub-reader-ap-dispatch.mjs && node scripts/patch-endpoint-blogroll-feeds-alias.mjs && node scripts/patch-endpoint-posts-uid-lookup.mjs && node scripts/patch-endpoint-posts-prefill-url.mjs && node scripts/patch-conversations-bluesky-self-filter.mjs && node scripts/patch-conversations-bluesky-cursor-fix.mjs && node scripts/patch-endpoint-micropub-source-filter.mjs && node scripts/patch-endpoint-posts-search-tags.mjs && node scripts/patch-syndicate-force-checked-default.mjs && node scripts/patch-ap-url-lookup-api.mjs && node scripts/patch-ap-allow-private-address.mjs && node scripts/patch-inbox-skip-view-activity-parse.mjs && node scripts/patch-inbox-ignore-view-activity.mjs && node scripts/patch-endpoint-posts-fetch-diagnostic.mjs && node scripts/patch-ap-normalize-nested-tags.mjs && node node_modules/@indiekit/indiekit/bin/cli.js serve --config indiekit.config.mjs",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
|
|||||||
116
scripts/patch-endpoint-posts-fetch-diagnostic.mjs
Normal file
116
scripts/patch-endpoint-posts-fetch-diagnostic.mjs
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
import { access, readFile, writeFile } from "node:fs/promises";
|
||||||
|
|
||||||
|
const filePath = "node_modules/@indiekit/endpoint-posts/lib/endpoint.js";
|
||||||
|
|
||||||
|
const marker = "// [patch] fetch-diagnostic";
|
||||||
|
|
||||||
|
async function exists(p) {
|
||||||
|
try {
|
||||||
|
await access(p);
|
||||||
|
return true;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(await exists(filePath))) {
|
||||||
|
console.log("[postinstall] endpoint-posts endpoint.js not found — skipping fetch-diagnostic patch");
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
const source = await readFile(filePath, "utf8");
|
||||||
|
|
||||||
|
if (source.includes(marker)) {
|
||||||
|
console.log("[postinstall] endpoint-posts fetch-diagnostic patch already applied");
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wrap the fetch calls to log the underlying cause on failure
|
||||||
|
const oldPost = ` async post(url, accessToken, jsonBody = false) {
|
||||||
|
const endpointResponse = await fetch(url, {`;
|
||||||
|
|
||||||
|
const newPost = ` ${marker}
|
||||||
|
async post(url, accessToken, jsonBody = false) {
|
||||||
|
let endpointResponse;
|
||||||
|
try {
|
||||||
|
endpointResponse = await fetch(url, {`;
|
||||||
|
|
||||||
|
const oldPostEnd = ` });
|
||||||
|
|
||||||
|
if (!endpointResponse.ok) {
|
||||||
|
throw await IndiekitError.fromFetch(endpointResponse);
|
||||||
|
}
|
||||||
|
|
||||||
|
return endpointResponse.status === 204
|
||||||
|
? { success_description: endpointResponse.headers.get("location") }
|
||||||
|
: await endpointResponse.json();
|
||||||
|
},
|
||||||
|
};`;
|
||||||
|
|
||||||
|
const newPostEnd = ` });
|
||||||
|
} catch (fetchError) {
|
||||||
|
const cause = fetchError.cause || fetchError;
|
||||||
|
console.error("[endpoint-posts] fetch failed for POST %s — %s: %s", url, cause.code || cause.name, cause.message);
|
||||||
|
if (cause.cause) console.error("[endpoint-posts] nested cause: %s", cause.cause.message || cause.cause);
|
||||||
|
throw fetchError;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!endpointResponse.ok) {
|
||||||
|
throw await IndiekitError.fromFetch(endpointResponse);
|
||||||
|
}
|
||||||
|
|
||||||
|
return endpointResponse.status === 204
|
||||||
|
? { success_description: endpointResponse.headers.get("location") }
|
||||||
|
: await endpointResponse.json();
|
||||||
|
},
|
||||||
|
};`;
|
||||||
|
|
||||||
|
const oldGet = ` async get(url, accessToken) {
|
||||||
|
const endpointResponse = await fetch(url, {`;
|
||||||
|
|
||||||
|
const newGet = ` async get(url, accessToken) {
|
||||||
|
let endpointResponse;
|
||||||
|
try {
|
||||||
|
endpointResponse = await fetch(url, {`;
|
||||||
|
|
||||||
|
const oldGetEnd = ` });
|
||||||
|
|
||||||
|
if (!endpointResponse.ok) {
|
||||||
|
throw await IndiekitError.fromFetch(endpointResponse);
|
||||||
|
}
|
||||||
|
|
||||||
|
const body = await endpointResponse.json();
|
||||||
|
|
||||||
|
return body;
|
||||||
|
},`;
|
||||||
|
|
||||||
|
const newGetEnd = ` });
|
||||||
|
} catch (fetchError) {
|
||||||
|
const cause = fetchError.cause || fetchError;
|
||||||
|
console.error("[endpoint-posts] fetch failed for GET %s — %s: %s", url, cause.code || cause.name, cause.message);
|
||||||
|
if (cause.cause) console.error("[endpoint-posts] nested cause: %s", cause.cause.message || cause.cause);
|
||||||
|
throw fetchError;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!endpointResponse.ok) {
|
||||||
|
throw await IndiekitError.fromFetch(endpointResponse);
|
||||||
|
}
|
||||||
|
|
||||||
|
const body = await endpointResponse.json();
|
||||||
|
|
||||||
|
return body;
|
||||||
|
},`;
|
||||||
|
|
||||||
|
let updated = source;
|
||||||
|
updated = updated.replace(oldPost, newPost);
|
||||||
|
updated = updated.replace(oldPostEnd, newPostEnd);
|
||||||
|
updated = updated.replace(oldGet, newGet);
|
||||||
|
updated = updated.replace(oldGetEnd, newGetEnd);
|
||||||
|
|
||||||
|
if (!updated.includes(marker)) {
|
||||||
|
console.warn("[postinstall] Skipping endpoint-posts fetch-diagnostic patch: upstream format changed");
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
await writeFile(filePath, updated, "utf8");
|
||||||
|
console.log("[postinstall] Patched endpoint-posts with fetch diagnostic logging");
|
||||||
113
scripts/patch-inbox-ignore-view-activity.mjs
Normal file
113
scripts/patch-inbox-ignore-view-activity.mjs
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
/**
|
||||||
|
* Patch: silently ignore PeerTube View (WatchAction) activities in the inbox.
|
||||||
|
*
|
||||||
|
* PeerTube broadcasts a non-standard ActivityStreams `View` activity to all
|
||||||
|
* followers whenever someone watches a video. Fedify has no built-in handler
|
||||||
|
* registered for this type, which causes a noisy
|
||||||
|
* "Unsupported activity type" error in the federation inbox log on every view.
|
||||||
|
*
|
||||||
|
* Fix: register a no-op `.on(View, ...)` handler at the end of the inbox
|
||||||
|
* listener chain so Fedify accepts and silently discards these activities
|
||||||
|
* instead of logging them as errors.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { access, readFile, writeFile } from "node:fs/promises";
|
||||||
|
|
||||||
|
const candidates = [
|
||||||
|
"node_modules/@rmdes/indiekit-endpoint-activitypub/lib/inbox-listeners.js",
|
||||||
|
"node_modules/@indiekit/indiekit/node_modules/@rmdes/indiekit-endpoint-activitypub/lib/inbox-listeners.js",
|
||||||
|
];
|
||||||
|
|
||||||
|
const patchSpecs = [
|
||||||
|
{
|
||||||
|
name: "inbox-ignore-view-activity-import",
|
||||||
|
marker: "// View imported",
|
||||||
|
oldSnippet: ` Undo,
|
||||||
|
Update,
|
||||||
|
} from "@fedify/fedify/vocab";`,
|
||||||
|
newSnippet: ` Undo,
|
||||||
|
Update,
|
||||||
|
View, // View imported
|
||||||
|
} from "@fedify/fedify/vocab";`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "inbox-ignore-view-activity-handler",
|
||||||
|
marker: "// PeerTube View handler",
|
||||||
|
oldSnippet: ` console.info(\`[ActivityPub] Flag received from \${reporterName} — \${reportedIds.length} objects reported\`);
|
||||||
|
} catch (error) {
|
||||||
|
console.warn("[ActivityPub] Flag handler error:", error.message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}`,
|
||||||
|
newSnippet: ` console.info(\`[ActivityPub] Flag received from \${reporterName} — \${reportedIds.length} objects reported\`);
|
||||||
|
} catch (error) {
|
||||||
|
console.warn("[ActivityPub] Flag handler error:", error.message);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
// ── View (PeerTube watch) ─────────────────────────────────────────────
|
||||||
|
// PeerTube broadcasts View (WatchAction) activities to all followers
|
||||||
|
// whenever someone watches a video. Fedify has no built-in handler for
|
||||||
|
// this type, producing noisy "Unsupported activity type" log errors.
|
||||||
|
// Silently accept and discard. // PeerTube View handler
|
||||||
|
.on(View, async () => {});
|
||||||
|
}`,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
async function exists(filePath) {
|
||||||
|
try {
|
||||||
|
await access(filePath);
|
||||||
|
return true;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const checkedFiles = new Set();
|
||||||
|
const patchedFiles = new Set();
|
||||||
|
|
||||||
|
for (const spec of patchSpecs) {
|
||||||
|
let foundAnyTarget = false;
|
||||||
|
|
||||||
|
for (const filePath of candidates) {
|
||||||
|
if (!(await exists(filePath))) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
foundAnyTarget = true;
|
||||||
|
checkedFiles.add(filePath);
|
||||||
|
|
||||||
|
const source = await readFile(filePath, "utf8");
|
||||||
|
|
||||||
|
if (spec.marker && source.includes(spec.marker)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!source.includes(spec.oldSnippet)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const updated = source.replace(spec.oldSnippet, spec.newSnippet);
|
||||||
|
|
||||||
|
if (updated === source) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
await writeFile(filePath, updated, "utf8");
|
||||||
|
patchedFiles.add(filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!foundAnyTarget) {
|
||||||
|
console.log(`[postinstall] ${spec.name}: no target files found`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (checkedFiles.size === 0) {
|
||||||
|
console.log("[postinstall] No inbox-listeners files found for View activity patch");
|
||||||
|
} else if (patchedFiles.size === 0) {
|
||||||
|
console.log("[postinstall] inbox-ignore-view-activity patches already applied");
|
||||||
|
} else {
|
||||||
|
console.log(
|
||||||
|
`[postinstall] Patched inbox-ignore-view-activity in ${patchedFiles.size}/${checkedFiles.size} file(s)`,
|
||||||
|
);
|
||||||
|
}
|
||||||
190
scripts/patch-inbox-skip-view-activity-parse.mjs
Normal file
190
scripts/patch-inbox-skip-view-activity-parse.mjs
Normal file
@@ -0,0 +1,190 @@
|
|||||||
|
/**
|
||||||
|
* Patch: skip PeerTube View (WatchAction) activities before Fedify parses them.
|
||||||
|
*
|
||||||
|
* PeerTube's View activities embed Schema.org extensions such as
|
||||||
|
* `InteractionCounter` that Fedify's JSON-LD deserializer doesn't recognise.
|
||||||
|
* This causes a hard "Failed to parse activity" error *before* any inbox
|
||||||
|
* listener is reached, so the .on(View, ...) no-op handler added earlier
|
||||||
|
* never fires.
|
||||||
|
*
|
||||||
|
* Root cause of the previous (broken) patch: Express's JSON body parser only
|
||||||
|
* handles `application/json`, not `application/activity+json`. So `req.body`
|
||||||
|
* is always undefined for ActivityPub inbox POSTs, meaning the check
|
||||||
|
* `req.body?.type === "View"` never matched and Fedify still received the raw
|
||||||
|
* stream.
|
||||||
|
*
|
||||||
|
* Fix (two changes to federation-bridge.js):
|
||||||
|
*
|
||||||
|
* 1. In createFedifyMiddleware: for ActivityPub POST requests where the body
|
||||||
|
* hasn't been parsed yet, buffer the raw stream, JSON-parse it, and store
|
||||||
|
* the result on req.body before the guard runs. Then check type === "View"
|
||||||
|
* and return 200 if so (preventing retries from the sender).
|
||||||
|
*
|
||||||
|
* 2. In fromExpressRequest: extend the content-type check to also handle
|
||||||
|
* `application/activity+json` and `application/ld+json` bodies (i.e. use
|
||||||
|
* JSON.stringify(req.body) to reconstruct the stream), so that non-View
|
||||||
|
* ActivityPub activities are forwarded correctly to Fedify.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { access, readFile, writeFile } from "node:fs/promises";
|
||||||
|
|
||||||
|
const candidates = [
|
||||||
|
"node_modules/@rmdes/indiekit-endpoint-activitypub/lib/federation-bridge.js",
|
||||||
|
"node_modules/@indiekit/indiekit/node_modules/@rmdes/indiekit-endpoint-activitypub/lib/federation-bridge.js",
|
||||||
|
];
|
||||||
|
|
||||||
|
const patchSpecs = [
|
||||||
|
// --- Patch 1: extend fromExpressRequest to handle activity+json bodies ---
|
||||||
|
{
|
||||||
|
name: "from-express-request-activity-json-fix",
|
||||||
|
marker: "// PeerTube activity+json body fix",
|
||||||
|
oldSnippet: ` if (ct.includes("application/json")) {
|
||||||
|
body = JSON.stringify(req.body);
|
||||||
|
} else if (ct.includes("application/x-www-form-urlencoded")) {`,
|
||||||
|
newSnippet: ` // PeerTube activity+json body fix
|
||||||
|
if (ct.includes("application/json") || ct.includes("activity+json") || ct.includes("ld+json")) {
|
||||||
|
body = JSON.stringify(req.body);
|
||||||
|
} else if (ct.includes("application/x-www-form-urlencoded")) {`,
|
||||||
|
},
|
||||||
|
|
||||||
|
// --- Patch 2a: replace the old (broken) v1 guard with the buffering v2 guard ---
|
||||||
|
// Handles the case where the previous version of this script was already run.
|
||||||
|
{
|
||||||
|
name: "inbox-skip-view-activity-parse-v2",
|
||||||
|
marker: "// PeerTube View parse skip v2",
|
||||||
|
oldSnippet: ` // Short-circuit PeerTube View (WatchAction) activities before Fedify
|
||||||
|
// attempts JSON-LD parsing. Fedify's vocab parser throws on PeerTube's
|
||||||
|
// Schema.org extensions (e.g. InteractionCounter), causing a
|
||||||
|
// "Failed to parse activity" error. Return 200 to prevent retries.
|
||||||
|
// PeerTube View parse skip
|
||||||
|
if (req.method === "POST" && req.body?.type === "View") {
|
||||||
|
return res.status(200).end();
|
||||||
|
}
|
||||||
|
const request = fromExpressRequest(req);`,
|
||||||
|
newSnippet: ` // Short-circuit PeerTube View (WatchAction) activities before Fedify
|
||||||
|
// attempts JSON-LD parsing. Fedify's vocab parser throws on PeerTube's
|
||||||
|
// Schema.org extensions (e.g. InteractionCounter), causing a
|
||||||
|
// "Failed to parse activity" error. Return 200 to prevent retries.
|
||||||
|
// PeerTube View parse skip v2
|
||||||
|
const _apct = req.headers["content-type"] || "";
|
||||||
|
if (
|
||||||
|
req.method === "POST" &&
|
||||||
|
!req.body &&
|
||||||
|
req.readable &&
|
||||||
|
(_apct.includes("activity+json") || _apct.includes("ld+json"))
|
||||||
|
) {
|
||||||
|
// Express doesn't parse application/activity+json, so buffer it ourselves.
|
||||||
|
const _chunks = [];
|
||||||
|
for await (const _chunk of req) {
|
||||||
|
_chunks.push(Buffer.isBuffer(_chunk) ? _chunk : Buffer.from(_chunk));
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
req.body = JSON.parse(Buffer.concat(_chunks).toString("utf8"));
|
||||||
|
} catch {
|
||||||
|
req.body = {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (req.method === "POST" && req.body?.type === "View") {
|
||||||
|
return res.status(200).end();
|
||||||
|
}
|
||||||
|
const request = fromExpressRequest(req);`,
|
||||||
|
},
|
||||||
|
|
||||||
|
// --- Patch 2b: apply the buffering v2 guard on a fresh (unpatched) file ---
|
||||||
|
// Handles the case where neither v1 nor v2 patch has been applied yet.
|
||||||
|
{
|
||||||
|
name: "inbox-skip-view-activity-parse-v2-fresh",
|
||||||
|
marker: "// PeerTube View parse skip v2",
|
||||||
|
oldSnippet: ` return async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const request = fromExpressRequest(req);`,
|
||||||
|
newSnippet: ` return async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
// Short-circuit PeerTube View (WatchAction) activities before Fedify
|
||||||
|
// attempts JSON-LD parsing. Fedify's vocab parser throws on PeerTube's
|
||||||
|
// Schema.org extensions (e.g. InteractionCounter), causing a
|
||||||
|
// "Failed to parse activity" error. Return 200 to prevent retries.
|
||||||
|
// PeerTube View parse skip v2
|
||||||
|
const _apct = req.headers["content-type"] || "";
|
||||||
|
if (
|
||||||
|
req.method === "POST" &&
|
||||||
|
!req.body &&
|
||||||
|
req.readable &&
|
||||||
|
(_apct.includes("activity+json") || _apct.includes("ld+json"))
|
||||||
|
) {
|
||||||
|
// Express doesn't parse application/activity+json, so buffer it ourselves.
|
||||||
|
const _chunks = [];
|
||||||
|
for await (const _chunk of req) {
|
||||||
|
_chunks.push(Buffer.isBuffer(_chunk) ? _chunk : Buffer.from(_chunk));
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
req.body = JSON.parse(Buffer.concat(_chunks).toString("utf8"));
|
||||||
|
} catch {
|
||||||
|
req.body = {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (req.method === "POST" && req.body?.type === "View") {
|
||||||
|
return res.status(200).end();
|
||||||
|
}
|
||||||
|
const request = fromExpressRequest(req);`,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
async function exists(filePath) {
|
||||||
|
try {
|
||||||
|
await access(filePath);
|
||||||
|
return true;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const checkedFiles = new Set();
|
||||||
|
const patchedFiles = new Set();
|
||||||
|
|
||||||
|
for (const spec of patchSpecs) {
|
||||||
|
let foundAnyTarget = false;
|
||||||
|
|
||||||
|
for (const filePath of candidates) {
|
||||||
|
if (!(await exists(filePath))) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
foundAnyTarget = true;
|
||||||
|
checkedFiles.add(filePath);
|
||||||
|
|
||||||
|
const source = await readFile(filePath, "utf8");
|
||||||
|
|
||||||
|
if (spec.marker && source.includes(spec.marker)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!source.includes(spec.oldSnippet)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const updated = source.replace(spec.oldSnippet, spec.newSnippet);
|
||||||
|
|
||||||
|
if (updated === source) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
await writeFile(filePath, updated, "utf8");
|
||||||
|
patchedFiles.add(filePath);
|
||||||
|
console.log(`[postinstall] Applied ${spec.name} to ${filePath}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!foundAnyTarget) {
|
||||||
|
console.log(`[postinstall] ${spec.name}: no target files found`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (checkedFiles.size === 0) {
|
||||||
|
console.log("[postinstall] No federation-bridge files found for View activity parse-skip patch");
|
||||||
|
} else if (patchedFiles.size === 0) {
|
||||||
|
console.log("[postinstall] inbox-skip-view-activity-parse patch already up to date");
|
||||||
|
} else {
|
||||||
|
console.log(
|
||||||
|
`[postinstall] Patched inbox-skip-view-activity-parse in ${patchedFiles.size}/${checkedFiles.size} file(s)`,
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user