diff --git a/scripts/cache-asset-manifests.js b/scripts/cache-asset-manifests.js index c8644ec..821669d 100644 --- a/scripts/cache-asset-manifests.js +++ b/scripts/cache-asset-manifests.js @@ -13,7 +13,7 @@ const { argv } = require("yargs"); const PromisePool = require("es6-promise-pool"); const connectToDb = require("../src/server/db"); -const neopets = require("../src/server/neopets"); +const neopetsAssets = require("../src/server/neopets-assets"); async function cacheAssetManifests(db) { const [ @@ -30,7 +30,7 @@ async function cacheAssetManifests(db) { async function cacheAssetManifest(row) { try { - let manifest = await neopets.loadAssetManifest(row.url); + let manifest = await neopetsAssets.loadAssetManifest(row.url); // After loading, write the new manifest. We make sure to write an empty // string if there was no manifest, to signify that it doesn't exist, so diff --git a/src/server/neopets-assets.js b/src/server/neopets-assets.js new file mode 100644 index 0000000..43cd7c9 --- /dev/null +++ b/src/server/neopets-assets.js @@ -0,0 +1,39 @@ +const fetch = require("node-fetch"); + +async function loadAssetManifest(swfUrl) { + const manifestUrl = convertSwfUrlToManifestUrl(swfUrl); + const res = await fetch(manifestUrl); + if (res.status === 404) { + return null; + } else if (!res.ok) { + throw new Error( + `for asset manifest, images.neopets.com returned: ` + + `${res.status} ${res.statusText}. (${manifestUrl})` + ); + } + + const json = await res.json(); + return { + assets: json["cpmanifest"]["assets"].map((asset) => ({ + format: asset["format"], + assetData: asset["asset_data"].map((assetDatum) => ({ + path: assetDatum["url"], + })), + })), + }; +} + +const SWF_URL_PATTERN = /^http:\/\/images\.neopets\.com\/cp\/(.+?)\/swf\/(.+?)\.swf$/; + +function convertSwfUrlToManifestUrl(swfUrl) { + const match = swfUrl.match(SWF_URL_PATTERN); + if (!match) { + throw new Error(`unexpected SWF URL format: ${JSON.stringify(swfUrl)}`); + } + + const [_, type, folders] = match; + + return `http://images.neopets.com/cp/${type}/data/${folders}/manifest.json`; +} + +module.exports = { loadAssetManifest }; diff --git a/src/server/types/AppearanceLayer.js b/src/server/types/AppearanceLayer.js index 88e9c95..197e25c 100644 --- a/src/server/types/AppearanceLayer.js +++ b/src/server/types/AppearanceLayer.js @@ -1,5 +1,5 @@ -const fetch = require("node-fetch"); const { gql } = require("apollo-server"); +const { loadAssetManifest } = require("../neopets-assets"); const typeDefs = gql` enum LayerImageSize { @@ -202,29 +202,6 @@ const resolvers = { }, }; -async function loadAssetManifest(swfUrl) { - const manifestUrl = convertSwfUrlToManifestUrl(swfUrl); - const res = await fetch(manifestUrl); - if (res.status === 404) { - return null; - } else if (!res.ok) { - throw new Error( - `for asset manifest, images.neopets.com returned: ` + - `${res.status} ${res.statusText}. (${manifestUrl})` - ); - } - - const json = await res.json(); - return { - assets: json["cpmanifest"]["assets"].map((asset) => ({ - format: asset["format"], - assetData: asset["asset_data"].map((assetDatum) => ({ - path: assetDatum["url"], - })), - })), - }; -} - async function loadAndCacheAssetManifest(db, layer) { let manifest = await loadAssetManifest(layer.url); @@ -255,17 +232,4 @@ async function loadAndCacheAssetManifest(db, layer) { return manifest; } -const SWF_URL_PATTERN = /^http:\/\/images\.neopets\.com\/cp\/(.+?)\/swf\/(.+?)\.swf$/; - -function convertSwfUrlToManifestUrl(swfUrl) { - const match = swfUrl.match(SWF_URL_PATTERN); - if (!match) { - throw new Error(`unexpected SWF URL format: ${JSON.stringify(swfUrl)}`); - } - - const [_, type, folders] = match; - - return `http://images.neopets.com/cp/${type}/data/${folders}/manifest.json`; -} - module.exports = { typeDefs, resolvers };