1
0
Fork 0
forked from OpenNeo/impress
impress/src/server/neopets-assets.js
Matchu 02d7cf73bb Support HTTPS asset URLs
There are a couple spots where we parse SWF URLs to get the ID out! Most visibly, our Support tools were crashing on it. And internally, manifest loading wasn't working. (I'm not sure if this got caught or if it caused crashes in user space? I didn't see them when wearing a failing item)

Anyway, fixed now!
2021-06-12 02:29:30 -07:00

59 lines
1.8 KiB
JavaScript

import fetch from "node-fetch";
async function loadAssetManifest(swfUrl) {
const possibleManifestUrls = convertSwfUrlToPossibleManifestUrls(swfUrl);
const responses = await Promise.all(
possibleManifestUrls.map((url) => fetch(url))
);
// Print errors for any responses with unexpected statuses. We'll do this
// even if other requests succeeded, or failed with an expected 404.
for (const res of responses) {
if (!res.ok && res.status !== 404) {
console.error(
`for asset manifest, images.neopets.com returned: ` +
`${res.status} ${res.statusText}. (${res.url})`
);
}
}
const successfulResponse = responses.find((res) => res.ok);
if (!successfulResponse) {
return null;
}
const json = await successfulResponse.json();
return {
assets: json["cpmanifest"]["assets"].map((asset) => ({
format: asset["format"],
assetData: asset["asset_data"].map((assetDatum) => ({
path: assetDatum["url"],
})),
})),
};
}
const SWF_URL_PATTERN = /^https?:\/\/images\.neopets\.com\/cp\/(bio|items)\/swf\/(.+?)_([a-z0-9]+)\.swf$/;
function convertSwfUrlToPossibleManifestUrls(swfUrl) {
const match = new URL(swfUrl, "http://images.neopets.com")
.toString()
.match(SWF_URL_PATTERN);
if (!match) {
throw new Error(`unexpected SWF URL format: ${JSON.stringify(swfUrl)}`);
}
const type = match[1];
const folders = match[2];
const hash = match[3];
// TODO: There are a few potential manifest URLs in play! Long-term, we
// should get this from modeling data. But these are some good guesses!
return [
`http://images.neopets.com/cp/${type}/data/${folders}/manifest.json`,
`http://images.neopets.com/cp/${type}/data/${folders}_${hash}/manifest.json`,
];
}
module.exports = { loadAssetManifest };