Skip to content

Commit 930aa0b

Browse files
committed
Adds XML support. Fixes #53.
1 parent c6b31f3 commit 930aa0b

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"prettier": "^3.3.3"
4646
},
4747
"dependencies": {
48+
"@rgrove/parse-xml": "^4.2.0",
4849
"debug": "^4.3.7",
4950
"flat-cache": "^6.1.1",
5051
"p-queue": "6.6.2"

src/AssetCache.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ class AssetCache {
195195
}
196196

197197
getCachedContentsPath(type = "buffer") {
198+
if(type === "xml") {
199+
type = "text";
200+
} else if(type === "parsed-xml") {
201+
type = "json";
202+
}
203+
198204
return `${this.cachePath}.${type}`;
199205
}
200206

@@ -227,7 +233,7 @@ class AssetCache {
227233
await this.ensureDir();
228234
}
229235

230-
if (type === "json") {
236+
if (type === "json" || type === "parsed-xml") {
231237
contents = JSON.stringify(contents);
232238
}
233239

@@ -250,7 +256,7 @@ class AssetCache {
250256
let contentPath = this.getCachedContentsPath(type);
251257
debug(`Fetching from cache ${contentPath}`);
252258

253-
if (type === "json") {
259+
if (type === "json" || type === "parsed-xml") {
254260
return require(contentPath);
255261
}
256262

src/RemoteAssetCache.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { parseXml } = require('@rgrove/parse-xml');
2+
13
const Sources = require("./Sources.js");
24
const AssetCache = require("./AssetCache.js");
35
// const debug = require("debug")("Eleventy:Fetch");
@@ -30,6 +32,10 @@ class RemoteAssetCache extends AssetCache {
3032
source: AssetCache.getCacheKey(source, options),
3133
};
3234

35+
if(options.type === "xml" || options.type === "parsed-xml") {
36+
cacheKey.type = options.type;
37+
}
38+
3339
if (options.fetchOptions) {
3440
if (options.fetchOptions.method && options.fetchOptions.method !== "GET") {
3541
cacheKey.method = options.fetchOptions.method;
@@ -80,8 +86,10 @@ class RemoteAssetCache extends AssetCache {
8086
async getResponseValue(response, type) {
8187
if (type === "json") {
8288
return response.json();
83-
} else if (type === "text") {
89+
} else if (type === "text" || type === "xml") {
8490
return response.text();
91+
} else if(type === "parsed-xml") {
92+
return parseXml(await response.text());
8593
}
8694
return Buffer.from(await response.arrayBuffer());
8795
}

test/RemoteAssetCacheTest.js

+26
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,29 @@ test("supports async functions that throw", async (t) => {
312312
await asset.destroy();
313313
} catch (e) {}
314314
});
315+
316+
test("type: xml", async (t) => {
317+
let feedUrl = "https://www.11ty.dev/blog/feed.xml";
318+
let ac = new RemoteAssetCache(feedUrl, ".cache", {
319+
type: "xml"
320+
});
321+
let xml = await ac.fetch();
322+
t.true(xml.length > 50)
323+
324+
try {
325+
await ac.destroy();
326+
} catch (e) {}
327+
});
328+
329+
test("type: parsed-xml", async (t) => {
330+
let feedUrl = "https://www.11ty.dev/blog/feed.xml";
331+
let ac = new RemoteAssetCache(feedUrl, ".cache", {
332+
type: "parsed-xml"
333+
});
334+
let xml = await ac.fetch();
335+
t.is(xml.children.length, 1)
336+
337+
try {
338+
await ac.destroy();
339+
} catch (e) {}
340+
});

0 commit comments

Comments
 (0)