From 1ded4995d6a571d6762b22384b921d97e2e81ff5 Mon Sep 17 00:00:00 2001 From: MsVoxxie Date: Sun, 15 Jun 2025 06:33:05 -0400 Subject: [PATCH] Update ampapi.js Fixes a bad JSON parse crash. --- ampapi.js | 139 +++++++++++++++++++++++++++--------------------------- 1 file changed, 70 insertions(+), 69 deletions(-) diff --git a/ampapi.js b/ampapi.js index 91db7bd..f4f57a4 100644 --- a/ampapi.js +++ b/ampapi.js @@ -1,79 +1,80 @@ exports.AMPAPI = function (baseUri) { - const request = require('request'); - var self = this; - this.baseUri = baseUri; - this.sessionId = ""; - this.dataSource = ""; - this.API = { Core: { GetAPISpec: [] } }; - this.initAsync = async function (stage2) { - this.dataSource = (baseUri.endsWith("/") ? - baseUri.substring(0, baseUri.lastIndexOf("/")) : - baseUri) - + "/API"; + const request = require('request'); + var self = this; + this.baseUri = baseUri; + this.sessionId = ''; + this.dataSource = ''; + this.API = { Core: { GetAPISpec: [] } }; + this.initAsync = async function (stage2) { + this.dataSource = (baseUri.endsWith('/') ? baseUri.substring(0, baseUri.lastIndexOf('/')) : baseUri) + '/API'; - for (const module of Object.keys(this.API)) { - var methods = this.API[module]; - this[module] = {}; + for (const module of Object.keys(this.API)) { + var methods = this.API[module]; + this[module] = {}; - for (const method of Object.keys(methods)) { - this[module][method + "Async"] = function (params) { - var args = Array.prototype.slice.call(arguments, 0); - return self.APICall(module, method, args); - }; - } - } + for (const method of Object.keys(methods)) { + this[module][method + 'Async'] = function (params) { + var args = Array.prototype.slice.call(arguments, 0); + return self.APICall(module, method, args); + }; + } + } - if (stage2) { - return true; - } - else { - var result = await this.Core.GetAPISpecAsync(); - if (result != null) { - this.API = result; - return await this.initAsync(true); - } - else { - return false; - } - } - } - this.APICall = function (module, methodName, args) { - var data = {}; + if (stage2) { + return true; + } else { + var result = await this.Core.GetAPISpecAsync(); + if (result != null) { + this.API = result; + return await this.initAsync(true); + } else { + return false; + } + } + }; + this.APICall = function (module, methodName, args) { + var data = {}; - var methodParams = this.API[module][methodName].Parameters; - var methodParamsLength = methodParams != null ? methodParams.length : 0; + var methodParams = this.API[module][methodName].Parameters; + var methodParamsLength = methodParams != null ? methodParams.length : 0; - for (var a = 0; a < methodParamsLength; a++) { - var argName = methodParams[a].Name; + for (var a = 0; a < methodParamsLength; a++) { + var argName = methodParams[a].Name; - var val = args[a]; + var val = args[a]; - data[argName] = val; - } + data[argName] = val; + } - var URI = `${this.dataSource}/${module}/${methodName}`; - data.SESSIONID = this.sessionId; + var URI = `${this.dataSource}/${module}/${methodName}`; + data.SESSIONID = this.sessionId; - return new Promise(function (resolve, reject) { - request.post({ - headers: { "Accept": "application/vnd.cubecoders-ampapi", "User-Agent": "CCL/AMPAPI-NodeJS" }, - url: URI, - body: JSON.stringify(data), - }, function (error, res, body) { - if (error) { - reject(error, res); - } - else if (!error) { - var result = JSON.parse(body); - - if (result != null && Object.keys(result).length === 1 && result.result != undefined) { - resolve(result.result); - } - else { - resolve(result); - } - } - }); - }); - } -}; \ No newline at end of file + return new Promise(function (resolve, reject) { + request.post( + { + headers: { Accept: 'application/vnd.cubecoders-ampapi', 'User-Agent': 'CCL/AMPAPI-NodeJS' }, + url: URI, + body: JSON.stringify(data), + }, + function (error, res, body) { + if (error) { + reject(error, res); + } else if (!error) { + let result; + try { + result = JSON.parse(body); + } catch (e) { + // Patch: If not valid JSON, reject cleanly instead of crashing the app. + return reject(new Error(`AMPAPI: Invalid JSON response from ${URI}: ${body.slice(0, 100)}`)); + } + if (result != null && Object.keys(result).length === 1 && result.result != undefined) { + resolve(result.result); + } else { + resolve(result); + } + } + } + ); + }); + }; +};