Skip to content

Commit 86e1b76

Browse files
committed
Use libxml2-wasm
Trying to use this to resolve the vuln. Closes: #2158
1 parent 2b50de1 commit 86e1b76

3 files changed

Lines changed: 34 additions & 18 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"eslint-plugin-react": "^7.37.4",
6161
"jest": "^29.7.0",
6262
"jest-puppeteer": "^10.1.4",
63+
"libxml2-wasm": "^0.4.1",
6364
"libxmljs": "^1.0.11",
6465
"prettier": "^3.5.3",
6566
"puppeteer": "^23.9.0",

prebuild_tests/sitemap.test.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
import { open } from "node:fs/promises";
2-
import type { XMLDocument } from "libxmljs";
3-
import { parseXmlAsync } from "libxmljs";
1+
import fs from "node:fs";
2+
import { XmlDocument } from "libxml2-wasm";
43

54
async function loadSitemap() {
6-
const file = await open("./public/sitemap.xml");
7-
8-
const document = parseXmlAsync(await file.readFile());
5+
const document = XmlDocument.fromString(
6+
fs.readFileSync("./public/sitemap.xml").toString()
7+
);
98

109
return document;
1110
}
1211

13-
function extractUrlsFromSitemap(sitemap: XMLDocument) {
12+
function extractUrlsFromSitemap(sitemap: XmlDocument) {
1413
const urlElements = sitemap.find("*");
1514
const urls = urlElements.map((ele) => {
16-
// const name = ele.find("*")[0].childNodes()[0].text();
1715
const children = ele.find("*");
18-
const name = children[0].childNodes()[0].text();
16+
const name = children[0].find("*")[0].content;
1917
let lastmod = null;
2018
if (children.length > 1) {
21-
lastmod = children[1].childNodes()[0].text();
19+
lastmod = children[1].find("*")[0].content;
2220
}
2321
return { name, lastmod };
2422
});
@@ -29,16 +27,20 @@ function extractUrlsFromSitemap(sitemap: XMLDocument) {
2927
describe("Sitemap", () => {
3028
it("should have a version and encoding", async () => {
3129
const subject = await loadSitemap();
32-
expect(subject.version()).toEqual("1.0");
33-
expect(subject.encoding()).toEqual("UTF-8");
30+
expect(subject.get("version")).toEqual("1.0");
31+
expect(subject.get("encoding")).toEqual("UTF-8");
32+
33+
subject.dispose();
3434
});
3535

3636
it("should have a schema listed", async () => {
3737
const subject = await loadSitemap();
38-
const urlsetNode = subject.root()!;
39-
expect(urlsetNode!.namespace()!.href()).toEqual(
40-
"http://www.sitemaps.org/schemas/sitemap/0.9",
38+
const urlsetNode = subject.root!;
39+
expect(urlsetNode!.namespaceUri).toEqual(
40+
"http://www.sitemaps.org/schemas/sitemap/0.9"
4141
);
42+
43+
subject.dispose();
4244
});
4345

4446
it("should have pages for the index", async () => {
@@ -48,6 +50,8 @@ describe("Sitemap", () => {
4850
const urlNames = urls.map((url) => url.name);
4951
// index page
5052
expect(urlNames).toContain("https://hockeybuggy.com");
53+
54+
subject.dispose();
5155
});
5256

5357
it("should have pages for the blog", async () => {
@@ -64,7 +68,7 @@ describe("Sitemap", () => {
6468
expect(blogPosts.length).toBeGreaterThan(10);
6569
// All the blog posts have a lastmod date
6670
expect(
67-
blogPosts.map((post) => post.lastmod).every((post) => post !== null),
71+
blogPosts.map((post) => post.lastmod).every((post) => post !== null)
6872
).toBe(true);
6973

7074
// tags pages
@@ -78,10 +82,12 @@ describe("Sitemap", () => {
7882
expect(urlNames).toContain("https://hockeybuggy.com/blog/categories");
7983
const blogCategoriesPattern = /\/blog\/categories/;
8084
const blogCategoriesPages = urls.filter((url) =>
81-
blogCategoriesPattern.test(url.name),
85+
blogCategoriesPattern.test(url.name)
8286
);
8387
// There are category pages. 4 is an arbitary number.
8488
expect(blogCategoriesPages.length).toBeGreaterThan(4);
89+
90+
subject.dispose();
8591
});
8692

8793
it("should have pages for the project", async () => {
@@ -94,9 +100,10 @@ describe("Sitemap", () => {
94100

95101
const projectPagePattern = /\/project\/\w*/;
96102
const projectPages = urls.filter((url) =>
97-
projectPagePattern.test(url.name),
103+
projectPagePattern.test(url.name)
98104
);
99105
// There are post pages. 5 is an arbitary number.
100106
expect(projectPages.length).toBeGreaterThan(5);
107+
subject.dispose();
101108
});
102109
});

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4717,6 +4717,7 @@ __metadata:
47174717
gray-matter: "npm:^4.0.3"
47184718
jest: "npm:^29.7.0"
47194719
jest-puppeteer: "npm:^10.1.4"
4720+
libxml2-wasm: "npm:^0.4.1"
47204721
libxmljs: "npm:^1.0.11"
47214722
lodash: "npm:^4.17.21"
47224723
markdown-it: "npm:^14.1.0"
@@ -6163,6 +6164,13 @@ __metadata:
61636164
languageName: node
61646165
linkType: hard
61656166

6167+
"libxml2-wasm@npm:^0.4.1":
6168+
version: 0.4.1
6169+
resolution: "libxml2-wasm@npm:0.4.1"
6170+
checksum: 10c0/4e646b7349d332f494ef62513e16b5ed468887a1bdae30b9ff1a87465785d8c3535bcf1accd67fece9c0f357c28168a5b42297334ec010de8feadca963237bc6
6171+
languageName: node
6172+
linkType: hard
6173+
61666174
"libxmljs@npm:^1.0.11":
61676175
version: 1.0.11
61686176
resolution: "libxmljs@npm:1.0.11"

0 commit comments

Comments
 (0)