From 1b3ba5d198a2e1ab5d2521c9b949eeaba7d87994 Mon Sep 17 00:00:00 2001 From: Eric Peterson Date: Thu, 12 May 2022 15:24:18 +0200 Subject: [PATCH] Work around node-fetch 2.x bug in TechDocs collator Signed-off-by: Eric Peterson --- .changeset/techdocs-seal-deal.md | 5 +++++ .../src/search/DefaultTechDocsCollatorFactory.ts | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 .changeset/techdocs-seal-deal.md diff --git a/.changeset/techdocs-seal-deal.md b/.changeset/techdocs-seal-deal.md new file mode 100644 index 0000000000..865d1fc837 --- /dev/null +++ b/.changeset/techdocs-seal-deal.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-techdocs-backend': patch +--- + +Fixed a bug that could cause TechDocs index generation to hang and fail when an underlying TechDocs site's `search_index.json` was empty. diff --git a/plugins/techdocs-backend/src/search/DefaultTechDocsCollatorFactory.ts b/plugins/techdocs-backend/src/search/DefaultTechDocsCollatorFactory.ts index 915b027854..d458eeaba0 100644 --- a/plugins/techdocs-backend/src/search/DefaultTechDocsCollatorFactory.ts +++ b/plugins/techdocs-backend/src/search/DefaultTechDocsCollatorFactory.ts @@ -179,7 +179,18 @@ export class DefaultTechDocsCollatorFactory implements DocumentCollatorFactory { }, }, ); - const searchIndex = await searchIndexResponse.json(); + + // todo(@backstage/techdocs-core): remove Promise.race() when node-fetch is 3.x+ + // workaround for fetch().json() hanging in node-fetch@2.x.x, fixed in 3.x.x + // https://github.com/node-fetch/node-fetch/issues/665 + const searchIndex = await Promise.race([ + searchIndexResponse.json(), + new Promise((_resolve, reject) => { + setTimeout(() => { + reject('Could not parse JSON in 5 seconds.'); + }, 5000); + }), + ]); return searchIndex.docs.map((doc: MkSearchIndexDoc) => ({ title: unescape(doc.title),