From 930aa0b65445a5d4bbff62a4db31fd9dd46dca3b Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Tue, 12 Nov 2024 15:25:56 -0600 Subject: [PATCH] Adds XML support. Fixes #53. --- package.json | 1 + src/AssetCache.js | 10 ++++++++-- src/RemoteAssetCache.js | 10 +++++++++- test/RemoteAssetCacheTest.js | 26 ++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 7c025ec..8205d92 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "prettier": "^3.3.3" }, "dependencies": { + "@rgrove/parse-xml": "^4.2.0", "debug": "^4.3.7", "flat-cache": "^6.1.1", "p-queue": "6.6.2" diff --git a/src/AssetCache.js b/src/AssetCache.js index fd38dcd..022224c 100644 --- a/src/AssetCache.js +++ b/src/AssetCache.js @@ -195,6 +195,12 @@ class AssetCache { } getCachedContentsPath(type = "buffer") { + if(type === "xml") { + type = "text"; + } else if(type === "parsed-xml") { + type = "json"; + } + return `${this.cachePath}.${type}`; } @@ -227,7 +233,7 @@ class AssetCache { await this.ensureDir(); } - if (type === "json") { + if (type === "json" || type === "parsed-xml") { contents = JSON.stringify(contents); } @@ -250,7 +256,7 @@ class AssetCache { let contentPath = this.getCachedContentsPath(type); debug(`Fetching from cache ${contentPath}`); - if (type === "json") { + if (type === "json" || type === "parsed-xml") { return require(contentPath); } diff --git a/src/RemoteAssetCache.js b/src/RemoteAssetCache.js index 1731057..94eebb0 100644 --- a/src/RemoteAssetCache.js +++ b/src/RemoteAssetCache.js @@ -1,3 +1,5 @@ +const { parseXml } = require('@rgrove/parse-xml'); + const Sources = require("./Sources.js"); const AssetCache = require("./AssetCache.js"); // const debug = require("debug")("Eleventy:Fetch"); @@ -30,6 +32,10 @@ class RemoteAssetCache extends AssetCache { source: AssetCache.getCacheKey(source, options), }; + if(options.type === "xml" || options.type === "parsed-xml") { + cacheKey.type = options.type; + } + if (options.fetchOptions) { if (options.fetchOptions.method && options.fetchOptions.method !== "GET") { cacheKey.method = options.fetchOptions.method; @@ -80,8 +86,10 @@ class RemoteAssetCache extends AssetCache { async getResponseValue(response, type) { if (type === "json") { return response.json(); - } else if (type === "text") { + } else if (type === "text" || type === "xml") { return response.text(); + } else if(type === "parsed-xml") { + return parseXml(await response.text()); } return Buffer.from(await response.arrayBuffer()); } diff --git a/test/RemoteAssetCacheTest.js b/test/RemoteAssetCacheTest.js index 92c9a03..cce8cf7 100644 --- a/test/RemoteAssetCacheTest.js +++ b/test/RemoteAssetCacheTest.js @@ -312,3 +312,29 @@ test("supports async functions that throw", async (t) => { await asset.destroy(); } catch (e) {} }); + +test("type: xml", async (t) => { + let feedUrl = "https://www.11ty.dev/blog/feed.xml"; + let ac = new RemoteAssetCache(feedUrl, ".cache", { + type: "xml" + }); + let xml = await ac.fetch(); + t.true(xml.length > 50) + + try { + await ac.destroy(); + } catch (e) {} +}); + +test("type: parsed-xml", async (t) => { + let feedUrl = "https://www.11ty.dev/blog/feed.xml"; + let ac = new RemoteAssetCache(feedUrl, ".cache", { + type: "parsed-xml" + }); + let xml = await ac.fetch(); + t.is(xml.children.length, 1) + + try { + await ac.destroy(); + } catch (e) {} +});