Skip to content

Commit

Permalink
Adds XML support. Fixes #53.
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Nov 12, 2024
1 parent c6b31f3 commit 930aa0b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 8 additions & 2 deletions src/AssetCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}

Expand Down Expand Up @@ -227,7 +233,7 @@ class AssetCache {
await this.ensureDir();
}

if (type === "json") {
if (type === "json" || type === "parsed-xml") {
contents = JSON.stringify(contents);
}

Expand All @@ -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);
}

Expand Down
10 changes: 9 additions & 1 deletion src/RemoteAssetCache.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down
26 changes: 26 additions & 0 deletions test/RemoteAssetCacheTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
});

0 comments on commit 930aa0b

Please sign in to comment.