Skip to content

Commit cf4913d

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

3 files changed

Lines changed: 36 additions & 266 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"eslint-plugin-react": "^7.37.4",
6161
"jest": "^29.7.0",
6262
"jest-puppeteer": "^10.1.4",
63-
"libxmljs": "^1.0.11",
63+
"libxml2-wasm": "^0.5.0",
6464
"prettier": "^3.5.3",
6565
"puppeteer": "^23.9.0",
6666
"sass": "^1.81.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
});

0 commit comments

Comments
 (0)