From ada15796a61fba39630e062aa3b41c50fc301285 Mon Sep 17 00:00:00 2001 From: leoz Date: Tue, 30 Apr 2024 20:12:27 -0400 Subject: [PATCH] Format --- .eslintrc.cjs | 14 ++--- README.md | 10 ++-- __tests__/fetchXml.spec.js | 16 +++--- __tests__/messageToXml.spec.js | 46 ++++++++-------- babel.config.cjs | 12 ++--- jest.config.js | 6 +-- package.json | 2 +- src/index.js | 6 +-- src/lib/xmlrpc-message.js | 98 +++++++++++++++++----------------- src/lib/xmlrpc-parser.js | 79 +++++++++++++-------------- src/lib/xmlrpc-response.js | 4 +- 11 files changed, 145 insertions(+), 148 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 8821f85..0360583 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -5,19 +5,19 @@ module.exports = { browser: true, es6: true, node: true, - 'jest/globals': true + "jest/globals": true, }, - extends: ['standard', 'prettier'], + extends: ["standard", "prettier"], globals: { - Atomics: 'readonly', - SharedArrayBuffer: 'readonly' + Atomics: "readonly", + SharedArrayBuffer: "readonly", }, parserOptions: { ecmaVersion: 2018, - sourceType: 'module' + sourceType: "module", }, rules: { - semi: [2, 'always'] + semi: [2, "always"], }, - plugins: ['jest'] + plugins: ["jest"], }; diff --git a/README.md b/README.md index bed9bd8..610a26b 100644 --- a/README.md +++ b/README.md @@ -15,16 +15,16 @@ npm install xmlrpc-parser ```javascript // -import fetch from 'cross-fetch'; -import { XmlRpcMessage, XmlRpcResponse } from 'xmlrpc-parser'; +import fetch from "cross-fetch"; +import { XmlRpcMessage, XmlRpcResponse } from "xmlrpc-parser"; // -const method = 'examples.getStateName'; +const method = "examples.getStateName"; const params = 23; const message = new XmlRpcMessage(method, [params]); const input = message.xml(); -const URL = 'http://betty.userland.com/RPC2'; +const URL = "http://betty.userland.com/RPC2"; // -const res = await fetch(URL, { method: 'post', body: input }); +const res = await fetch(URL, { method: "post", body: input }); const xml = await res.text(); // const response = new XmlRpcResponse(); diff --git a/__tests__/fetchXml.spec.js b/__tests__/fetchXml.spec.js index 91dfe2a..571b97c 100644 --- a/__tests__/fetchXml.spec.js +++ b/__tests__/fetchXml.spec.js @@ -1,21 +1,21 @@ // -import fetch from 'cross-fetch'; -import { XmlRpcMessage, XmlRpcResponse } from '../src/index.js'; +import fetch from "cross-fetch"; +import { XmlRpcMessage, XmlRpcResponse } from "../src/index.js"; -describe('Fetch and parse XML funtion', () => { - test('It should fetch and parse XML', async () => { - const method = 'examples.getStateName'; +describe("Fetch and parse XML funtion", () => { + test("It should fetch and parse XML", async () => { + const method = "examples.getStateName"; const params = 23; const message = new XmlRpcMessage(method, [params]); const input = message.xml(); - const URL = 'http://betty.userland.com/RPC2'; + const URL = "http://betty.userland.com/RPC2"; // - const res = await fetch(URL, { method: 'post', body: input }); + const res = await fetch(URL, { method: "post", body: input }); const xml = await res.text(); // const response = new XmlRpcResponse(); const result = await response.parse(xml); // - expect('Minnesota').toBe(result.params[0]); + expect("Minnesota").toBe(result.params[0]); }); }); diff --git a/__tests__/messageToXml.spec.js b/__tests__/messageToXml.spec.js index e264240..c78598b 100644 --- a/__tests__/messageToXml.spec.js +++ b/__tests__/messageToXml.spec.js @@ -1,36 +1,36 @@ // -import { XmlRpcMessage } from '../src/index.js'; +import { XmlRpcMessage } from "../src/index.js"; -describe('Fetch XML funtion', () => { - test('it should fetch XML', () => { - const method = 'MyCall'; +describe("Fetch XML funtion", () => { + test("it should fetch XML", () => { + const method = "MyCall"; const params = { a: 1, - b: 2 + b: 2, }; const message = new XmlRpcMessage(method, [params]); const input = message.xml(); const output = '\n' + - '\n' + - 'MyCall\n' + - '\n' + - '\n' + - '\n' + - '\n' + - 'a\n' + - '1\n' + - '\n' + - '\n' + - 'b\n' + - '2\n' + - '\n' + - '\n' + - '\n' + - '\n' + - '\n' + - ''; + "\n" + + "MyCall\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "a\n" + + "1\n" + + "\n" + + "\n" + + "b\n" + + "2\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + ""; expect(input).toEqual(output); }); diff --git a/babel.config.cjs b/babel.config.cjs index 1a84b02..51b098d 100644 --- a/babel.config.cjs +++ b/babel.config.cjs @@ -3,12 +3,12 @@ module.exports = { presets: [ [ - '@babel/preset-env', + "@babel/preset-env", { targets: { - node: 'current' - } - } - ] - ] + node: "current", + }, + }, + ], + ], }; diff --git a/jest.config.js b/jest.config.js index f48778b..3a968ca 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,10 +1,10 @@ // jest.config.js export default { - testEnvironment: 'node', + testEnvironment: "node", transform: { - '^.+\\.jsx?$': 'babel-jest' + "^.+\\.jsx?$": "babel-jest", }, - verbose: true + verbose: true, }; diff --git a/package.json b/package.json index bfd4852..84e29e4 100644 --- a/package.json +++ b/package.json @@ -44,4 +44,4 @@ "eslint-plugin-standard": "^5.0.0", "jest": "^29.3.1" } -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index 16e3e3c..63fd7fa 100644 --- a/src/index.js +++ b/src/index.js @@ -1,9 +1,9 @@ /** * @license xrpc - * (c) 2016 - 2022 Leonid Zolotarev + * (c) 2016 - 2024 Leonid Zolotarev * (c) 2012 - 2013 Nathan Cartwright * License: MIT */ -export { default as XmlRpcMessage } from './lib/xmlrpc-message.js'; -export { default as XmlRpcResponse } from './lib/xmlrpc-response.js'; +export { default as XmlRpcMessage } from "./lib/xmlrpc-message.js"; +export { default as XmlRpcResponse } from "./lib/xmlrpc-response.js"; diff --git a/src/lib/xmlrpc-message.js b/src/lib/xmlrpc-message.js index 3477c76..d413fcc 100644 --- a/src/lib/xmlrpc-message.js +++ b/src/lib/xmlrpc-message.js @@ -10,7 +10,7 @@ // let xml = msg.xml(); // function XMLRPCMessage(methodname, params) { - this.method = methodname || 'system.listMethods'; + this.method = methodname || "system.listMethods"; this.params = params || []; return this; } @@ -19,38 +19,38 @@ XMLRPCMessage.prototype.xml = function () { let xml = '\n'; if (this.method) { - xml += '\n'; - xml += '' + this.method + '\n'; + xml += "\n"; + xml += "" + this.method + "\n"; } else { - xml += '\n'; + xml += "\n"; } if (!this.is_fault) { - xml += '\n'; + xml += "\n"; } else { - xml += '\n'; + xml += "\n"; } for (let i = 0; i < this.params.length; i++) { if (!this.is_fault) { - xml += '\n'; + xml += "\n"; } - xml += '' + this.getParamXML(this.params[i]) + '\n'; + xml += "" + this.getParamXML(this.params[i]) + "\n"; if (!this.is_fault) { - xml += '\n'; + xml += "\n"; } } if (!this.is_fault) { - xml += '\n'; + xml += "\n"; } else { - xml += '\n'; + xml += "\n"; } if (this.method) { - xml += ''; + xml += ""; } else { - xml += ''; + xml += ""; } return xml; @@ -61,16 +61,16 @@ XMLRPCMessage.prototype.getParamXML = function (data) { let xml; const type = this.dataTypeOf(data); switch (type) { - case 'date': + case "date": xml = this.toDateXML(data); break; - case 'array': + case "array": xml = this.toArrayXML(data); break; - case 'struct': + case "struct": xml = this.toStructXML(data); break; - case 'boolean': + case "boolean": xml = this.toBooleanXML(data); break; default: @@ -84,55 +84,55 @@ XMLRPCMessage.prototype.dataTypeOf = function (o) { let type = typeof o; type = type.toLowerCase(); switch (type) { - case 'number': - if (Math.round(o) === o) type = 'i4'; - else type = 'double'; + case "number": + if (Math.round(o) === o) type = "i4"; + else type = "double"; break; - case 'object': - if (o.constructor === Date) type = 'date'; - else if (o.constructor === Array) type = 'array'; - else type = 'struct'; + case "object": + if (o.constructor === Date) type = "date"; + else if (o.constructor === Array) type = "array"; + else type = "struct"; break; } return type; }; XMLRPCMessage.prototype.toValueXML = function (type, data) { - const xml = '<' + type + '>' + data + ''; + const xml = "<" + type + ">" + data + ""; return xml; }; XMLRPCMessage.prototype.toBooleanXML = function (data) { const value = data === true ? 1 : 0; - const xml = '' + value + ''; + const xml = "" + value + ""; return xml; }; XMLRPCMessage.prototype.toDateXML = function (data) { - let xml = ''; + let xml = ""; xml += this.dateToISO8601(data); - xml += ''; + xml += ""; return xml; }; XMLRPCMessage.prototype.toArrayXML = function (data) { - let xml = '\n'; + let xml = "\n"; for (let i = 0; i < data.length; i++) { - xml += '' + this.getParamXML(data[i]) + '\n'; + xml += "" + this.getParamXML(data[i]) + "\n"; } - xml += '\n'; + xml += "\n"; return xml; }; XMLRPCMessage.prototype.toStructXML = function (data) { - let xml = '\n'; + let xml = "\n"; for (const i in data) { - xml += '\n'; - xml += '' + i + '\n'; - xml += '' + this.getParamXML(data[i]) + '\n'; - xml += '\n'; + xml += "\n"; + xml += "" + i + "\n"; + xml += "" + this.getParamXML(data[i]) + "\n"; + xml += "\n"; } - xml += '\n'; + xml += "\n"; return xml; }; @@ -161,41 +161,41 @@ XMLRPCMessage.prototype.dateToISO8601 = function dateToISO8601( format = 6; } if (!offset) { - offset = 'Z'; + offset = "Z"; date = dateIn; } else { const d = offset.match(/([-+])([0-9]{2}):([0-9]{2})/); let offsetnum = Number(d[2]) * 60 + Number(d[3]); - offsetnum *= d[1] === '-' ? -1 : 1; + offsetnum *= d[1] === "-" ? -1 : 1; date = new Date(Number(Number(dateIn) + offsetnum * 60000)); } const zeropad = function (num) { - return (num < 10 ? '0' : '') + num; + return (num < 10 ? "0" : "") + num; }; - let str = ''; + let str = ""; str += date.getUTCFullYear(); if (format > 1) { - str += '-' + zeropad(date.getUTCMonth() + 1); + str += "-" + zeropad(date.getUTCMonth() + 1); } if (format > 2) { - str += '-' + zeropad(date.getUTCDate()); + str += "-" + zeropad(date.getUTCDate()); } if (format > 3) { str += - 'T' + zeropad(date.getUTCHours()) + ':' + zeropad(date.getUTCMinutes()); + "T" + zeropad(date.getUTCHours()) + ":" + zeropad(date.getUTCMinutes()); } if (format > 5) { const secs = Number( date.getUTCSeconds() + - '.' + - (date.getUTCMilliseconds() < 100 ? '0' : '') + - zeropad(date.getUTCMilliseconds()) + "." + + (date.getUTCMilliseconds() < 100 ? "0" : "") + + zeropad(date.getUTCMilliseconds()) ); - str += ':' + zeropad(secs); + str += ":" + zeropad(secs); } else if (format > 4) { - str += ':' + zeropad(date.getUTCSeconds()); + str += ":" + zeropad(date.getUTCSeconds()); } if (format > 3) { diff --git a/src/lib/xmlrpc-parser.js b/src/lib/xmlrpc-parser.js index 0f928e3..cdc06f9 100644 --- a/src/lib/xmlrpc-parser.js +++ b/src/lib/xmlrpc-parser.js @@ -1,6 +1,6 @@ // -import xml from 'sax-parser'; -import b64 from 'utf8-base64'; +import xml from "sax-parser"; +import b64 from "utf8-base64"; function XmlRpcParser(events) { this.events = events; @@ -9,7 +9,7 @@ function XmlRpcParser(events) { method: false, is_response: false, is_fault: false, - params: [] + params: [], }; this.finished = false; this.complete = false; @@ -41,7 +41,7 @@ XmlRpcParser.prototype.finish = function finish() { if (instance.complete) { instance.events.onDone(instance.data); } else { - instance.events.onError('End of input reached before document complete'); + instance.events.onError("End of input reached before document complete"); } }, 0); @@ -61,23 +61,23 @@ XmlRpcParser.prototype.getHandler = function getHandler() { instance.cdataStack.push([]); switch (elem) { // Detect a methodResponse. - case 'methodResponse': + case "methodResponse": instance.data.is_response = true; break; // Detect a response that's a fault. - case 'fault': + case "fault": instance.data.is_fault = true; break; // Push an empty object onto the stack to collect members - case 'struct': + case "struct": instance.valueStack.push({}); break; // Push an empty array onto the stack to collect values - case 'array': + case "array": instance.valueStack.push([]); break; // Assume an implicit string, to begin with. - case 'value': + case "value": instance.isImplicitString = true; break; } @@ -92,85 +92,82 @@ XmlRpcParser.prototype.getHandler = function getHandler() { // React to the end of elements. cb.onEndElementNS(function (elem, prefix, uri) { - const cdata = instance.cdataStack - .pop() - .join('') - .trim(); + const cdata = instance.cdataStack.pop().join("").trim(); instance.elementStack.pop(); let currValue; switch (elem) { // The end of a call or response means a successful end of parsing, - case 'methodCall': - case 'methodResponse': + case "methodCall": + case "methodResponse": instance.complete = true; instance.finish(); break; // Record the method name from CDATA, when encountered. - case 'methodName': + case "methodName": instance.data.method = cdata; break; // The end of a param element or a fault means the current // value on the stack is complete. - case 'param': - case 'fault': + case "param": + case "fault": instance.data.params.push(instance.valueStack.pop()); break; // Push this member name onto the stack for safekeeping // until a value comes along. - case 'name': + case "name": instance.nameStack.push(cdata); break; // The end of a member is where we pick up the last // encountered name and value and stick the pair into the // current struct under construction - case 'member': { - const currName = instance.nameStack.pop(); - currValue = instance.valueStack.pop(); - instance.valueStack[instance.valueStack.length - 1][ - currName - ] = currValue; - } + case "member": + { + const currName = instance.nameStack.pop(); + currValue = instance.valueStack.pop(); + instance.valueStack[instance.valueStack.length - 1][currName] = + currValue; + } break; // Explicit string, i4, int, and double values are treated the same. // TODO: Should these be parsed more strictly? // TODO: Lots of repetition here, feels icky. - case 'string': - case 'i4': - case 'int': - case 'double': + case "string": + case "i4": + case "int": + case "double": instance.isImplicitString = false; instance.valueStack.push(cdata); break; // Parse a boolean into the JS equivalent. - case 'boolean': + case "boolean": instance.isImplicitString = false; - instance.valueStack.push(cdata === 'true' || cdata === '1'); + instance.valueStack.push(cdata === "true" || cdata === "1"); break; // Parse a date from its ISO 8601 representation. - case 'dateTime.iso8601': + case "dateTime.iso8601": instance.isImplicitString = false; instance.valueStack.push(parseISO8601(cdata)); break; // Decode a base64 into its binary form. - case 'base64': + case "base64": instance.isImplicitString = false; instance.valueStack.push(b64.B64.decode(cdata)); break; // The end of an array / struct also cancels implicit string - case 'struct': - case 'array': + case "struct": + case "array": instance.isImplicitString = false; break; // The end of a value can mean the end of an implicit // string and also the end of an array item. - case 'value': + case "value": if (instance.isImplicitString) { // No type element found within the value element, // so assume an implicit string. instance.valueStack.push(cdata); } if ( - instance.elementStack[instance.elementStack.length - 1] === 'data' + instance.elementStack[instance.elementStack.length - 1] === "data" ) { // Nested inside a data element, so assume an array. currValue = instance.valueStack.pop(); @@ -194,7 +191,7 @@ XmlRpcParser.prototype.getHandler = function getHandler() { function parseISO8601(string) { const regexp = - '([0-9]{4})(-?([0-9]{2})(-?([0-9]{2})(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(.([0-9]+))?)?(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?'; + "([0-9]{4})(-?([0-9]{2})(-?([0-9]{2})(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(.([0-9]+))?)?(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?"; let offset = 0; const dOut = new Date(); @@ -217,11 +214,11 @@ function parseISO8601(string) { date.setSeconds(d[10]); } if (d[12]) { - date.setMilliseconds(Number('0.' + d[12]) * 1000); + date.setMilliseconds(Number("0." + d[12]) * 1000); } if (d[14]) { offset = Number(d[16]) * 60 + Number(d[17]); - offset *= d[15] === '-' ? 1 : -1; + offset *= d[15] === "-" ? 1 : -1; } offset -= date.getTimezoneOffset(); diff --git a/src/lib/xmlrpc-response.js b/src/lib/xmlrpc-response.js index e30a0b3..588a36c 100644 --- a/src/lib/xmlrpc-response.js +++ b/src/lib/xmlrpc-response.js @@ -1,5 +1,5 @@ // -import XmlRpcParser from './xmlrpc-parser.js'; +import XmlRpcParser from "./xmlrpc-parser.js"; function XMLRPCResponse() { return this; @@ -13,7 +13,7 @@ XMLRPCResponse.prototype.parse = function (str) { }, onError: function (err) { reject(err); - } + }, }); return parser.parseString(str).finish(); });