From e7cc20876b9af7a5685bf0cb8ab5211b772633fc Mon Sep 17 00:00:00 2001 From: Matchu Date: Thu, 22 Oct 2020 23:22:04 -0700 Subject: [PATCH] use xmlrpc to load pets, not json.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The AMFPHP gateway's json.php endpoint has always had a problem parsing pets whose names start with digits… I've dug into it before, and checked again today, and there really is just no way around it: https://github.com/silexlabs/amfphp-1.9/blob/d584b58e9581ecbd72def15d1fd31eb2408d050f/core/json/app/Actions.php#L43 And there aren't any reliable AMFPHP Node libraries out there to make the actual native AMF call. Buuuut! In today's investigation, I noticed the xmlrpc.php endpoint for the first time. And, wouldn't you know it, there's //great// reliability for something as enterprise-standard as that! So here, I've switched over to using an xmlrpc client library, which simplifies our calling code //and// makes number pets work correctly 😁 I wouldn't have done it just for the simplification, I think bringing in a library is net more complexity… but getting this finally right is a big relief. --- package.json | 3 ++- src/server/types/Pet.js | 46 ++++++++++++++++------------------------- yarn.lock | 15 +++++++++++++- 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/package.json b/package.json index 135978c..236123b 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,8 @@ "react-icons": "^3.11.0", "react-router-dom": "^5.1.2", "react-scripts": "3.4.1", - "react-transition-group": "^4.3.0" + "react-transition-group": "^4.3.0", + "xmlrpc": "^1.3.2" }, "scripts": { "start": "yarn build-cached-data && react-app-rewired start", diff --git a/src/server/types/Pet.js b/src/server/types/Pet.js index 31f6acf..d57f094 100644 --- a/src/server/types/Pet.js +++ b/src/server/types/Pet.js @@ -1,5 +1,8 @@ +const util = require("util"); + const fetch = require("node-fetch"); const { gql } = require("apollo-server"); +const xmlrpc = require("xmlrpc"); const { getPoseFromPetState } = require("../util"); const { saveModelingData } = require("../modeling"); @@ -87,38 +90,25 @@ const resolvers = { }, }; -async function loadPetMetaData(petName) { - const url = `http://www.neopets.com/amfphp/json.php/PetService.getPet/${petName}`; - const res = await fetch(url); - if (!res.ok) { - throw new Error( - `for pet meta data, neopets.com returned: ` + - `${res.status} ${res.statusText}. (${url})` - ); - } +const neopetsXmlrpcClient = xmlrpc.createClient({ + host: "www.neopets.com", + port: 80, + path: "/amfphp/xmlrpc.php", +}); +const neopetsXmlrpcCall = util + .promisify(neopetsXmlrpcClient.methodCall) + .bind(neopetsXmlrpcClient); - const json = await res.json(); - return json; +async function loadPetMetaData(petName) { + const response = await neopetsXmlrpcCall("PetService.getPet", [petName]); + return response; } async function loadCustomPetData(petName) { - const url = - `http://www.neopets.com/amfphp/json.php/CustomPetService.getViewerData` + - `/${petName}`; - const res = await fetch(url); - if (!res.ok) { - throw new Error( - `for custom pet data, neopets.com returned: ` + - `${res.status} ${res.statusText}. (${url})` - ); - } - - const json = await res.json(); - if (!json.custom_pet) { - throw new Error(`missing custom_pet data`); - } - - return json; + const response = await neopetsXmlrpcCall("CustomPetService.getViewerData", [ + petName, + ]); + return response; } function getPoseFromPetData(petMetaData, petCustomData) { diff --git a/yarn.lock b/yarn.lock index 3de47d2..2604300 100644 --- a/yarn.lock +++ b/yarn.lock @@ -16011,7 +16011,7 @@ sax@1.2.1: resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.1.tgz#7b8e656190b228e81a66aea748480d828cd2d37a" integrity sha1-e45lYZCyKOgaZq6nSEgNgozS03o= -sax@>=0.6.0, sax@^1.2.4, sax@~1.2.4: +sax@1.2.x, sax@>=0.6.0, sax@^1.2.4, sax@~1.2.4: version "1.2.4" resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9" integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw== @@ -18440,6 +18440,11 @@ xml2js@^0.4.5: sax ">=0.6.0" xmlbuilder "~11.0.0" +xmlbuilder@8.2.x: + version "8.2.2" + resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-8.2.2.tgz#69248673410b4ba42e1a6136551d2922335aa773" + integrity sha1-aSSGc0ELS6QuGmE2VR0pIjNap3M= + xmlbuilder@~11.0.0: version "11.0.1" resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-11.0.1.tgz#be9bae1c8a046e76b31127726347d0ad7002beb3" @@ -18455,6 +18460,14 @@ xmlchars@^2.1.1: resolved "https://registry.yarnpkg.com/xmlchars/-/xmlchars-2.2.0.tgz#060fe1bcb7f9c76fe2a17db86a9bc3ab894210cb" integrity sha512-JZnDKK8B0RCDw84FNdDAIpZK+JuJw+s7Lz8nksI7SIuU3UXJJslUthsi+uWBUYOwPFwW7W7PRLRfUKpxjtjFCw== +xmlrpc@^1.3.2: + version "1.3.2" + resolved "https://registry.yarnpkg.com/xmlrpc/-/xmlrpc-1.3.2.tgz#26b2ea347848d028aac7e7514b5351976de3e83d" + integrity sha1-JrLqNHhI0Ciqx+dRS1NRl23j6D0= + dependencies: + sax "1.2.x" + xmlbuilder "8.2.x" + xregexp@2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/xregexp/-/xregexp-2.0.0.tgz#52a63e56ca0b84a7f3a5f3d61872f126ad7a5943"