Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

previous version is no longer needed #609

Merged
merged 2 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 0 additions & 86 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,77 +35,6 @@ app.use(
}),
);

/**
* @param {string} shortName
* @param {string} publishDate
* @returns {Promise<{ previousMaturity: string, previousPublishDate: string }>}
* @throws {Promise<{ statusCode: number, error: string }>}
*/
function getPreviousVersionInfo(shortName, publishDate) {
return new Promise((resolve, reject) => {
const url = `https://www.w3.org/TR/${shortName}/`;
request.get(url, (error, response, body) => {
if (error) {
// eslint-disable-next-line prefer-promise-reject-errors
return reject({ statusCode: 400, error });
}

if (
response &&
response.statusCode >= 400 &&
response.statusCode < 500
) {
const { statusCode, statusMessage } = response;
// eslint-disable-next-line prefer-promise-reject-errors
return reject({ statusCode, error: statusMessage });
}

const { document } = new JSDOM(body).window;
const dl = document.querySelector("body div.head dl");

let thisURI;
let previousURI;
if (dl) {
// eslint-disable-next-line no-restricted-syntax
for (const dt of dl.querySelectorAll("dt")) {
const txt = dt.textContent
.toLocaleLowerCase()
.replace(":", "")
.replace("published", "")
.trim();
const dd = dt.nextElementSibling;
if (txt === "this version") {
thisURI = dd.querySelector("a").href;
} else if (/^previous version(?:s)?$/.test(txt)) {
previousURI = dd.querySelector("a").href;
}
}
}
if (!thisURI) {
// eslint-disable-next-line prefer-promise-reject-errors
return reject({
statusCode: 5000,
error: `Couldn't find a 'This version' uri in the previous version.`,
});
}

const thisDate = thisURI.match(/[1-2][0-9]{7}/)[0];
const prev =
thisDate === publishDate.replace(/-/g, "")
? previousURI
: thisURI;
const pDate = prev.match(/[1-2][0-9]{7}/)[0];

const previousMaturity = prev.match(/\/TR\/[0-9]{4}\/([A-Z]+)/)[1];
const previousPublishDate = pDate.replace(
/(\d{4})(\d{2})(\d{2})/,
"$1-$2-$3",
);
resolve({ previousMaturity, previousPublishDate });
});
});
}

async function extractTar(tarFile) {
const extract = tar.extract();
const uploadPath = await mkdtemp("uploads/");
Expand Down Expand Up @@ -227,26 +156,11 @@ app.get(
},
async (req, res) => {
const specURL = new URL(req.query.url);
const shortName = specURL.searchParams.get("shortName");
const publishDate =
specURL.searchParams.get("publishDate") || getShortIsoDate();

specURL.searchParams.set("publishDate", publishDate);

if (shortName) {
try {
const { previousMaturity, previousPublishDate } =
await getPreviousVersionInfo(shortName, publishDate);
specURL.searchParams.set("previousMaturity", previousMaturity);
specURL.searchParams.set(
"previousPublishDate",
previousPublishDate,
);
} catch ({ statusCode, error }) {
return res.status(statusCode).json({ error });
}
}

// if there's an error we get an err object with status and message, otherwise we get content
try {
const { html, errors, warnings } = await genMap[req.query.type](
Expand Down
7 changes: 0 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ const BASE_URL = "http://localhost:3000/";
const NO_URL = "?type=foo&URL=notice-that-its-in-uppercase";
const NO_TYPE = "?url=foo&TYPE=notice-that-its-in-uppercase";
const BAD_GENERATOR = "?type=fluxor&url=http://example.com/";
const BAD_SHORTNAME =
"?type=respec&url=http://example.com/%3FshortName%3Ddiplodocus";
const NO_RESPEC = "?type=respec&url=http://example.com/";
const SUCCESS1 = `?type=respec&url=https://w3c.github.io/manifest/`;
const SUCCESS2 = `?type=respec&url=https://w3c.github.io/payment-request/`;
Expand Down Expand Up @@ -55,11 +53,6 @@ describe("spec-generator", () => {
BASE_URL + BAD_GENERATOR,
FAILS_WITH(done, '{"error":"Unknown generator: fluxor"}'),
));
it("if the shortname is not valid", done =>
REQUEST.get(
BASE_URL + BAD_SHORTNAME,
FAILS_WITH(done, '{"error":"Not Found"}', 404),
));
it("if the URL does not point to a Respec document", done =>
REQUEST.get(
BASE_URL + NO_RESPEC,
Expand Down