xml-rpc-ts
v1.0.2
Published
XML-RPC library in TypeScript - Based on XML-RPC specifications (https://xmlrpc.com/spec.md)
Maintainers
Readme
XML-RPC TypeScript Library
A complete XML-RPC implementation in TypeScript, based on the XML-RPC specifications.
Features
- Full XML-RPC support: Implements the complete XML-RPC specification
- Type-safe: Written in TypeScript with full type definitions
- Client & Server: Can act as both RPC client and server
- Introspection methods: Built-in support for
system.listMethods,system.methodHelp,system.methodSignature - Custom types: Support for custom object types with parser directives
Supported Types
Scalar Values
| XML-RPC Type | TypeScript Type |
|--------------|-----------------|
| <i4> / <int> | number (integer) |
| <boolean> | boolean |
| <string> | string |
| <double> | number (floating point) |
| <dateTime.iso8601> | Date |
| <base64> | Base64 |
Complex Types
| XML-RPC Type | TypeScript Type |
|--------------|-----------------|
| <struct> | object / Record<string, unknown> |
| <array> | unknown[] |
Installation
cd typescript
npm installUsage
Basic Example
import { RPCParser, RPCServer, Base64 } from './index';
// Create a server
const server = new RPCServer();
// Add a method
server.addMethod(
'getBook',
(id: string) => ({
id,
title: 'My Book',
price: 19.99
}),
'Get a book by ID',
['string'],
'struct'
);
// Call method locally
const result = await server.callMethodLocallyWithObjects('getBook', 'book-123');
if (result.success) {
console.log(result.data);
}RPC Client
import { RPCClient, RPCTransport } from './index';
// Implement your transport (e.g., HTTP, XMPP)
const transport: RPCTransport = {
async sendRequest(peerJid, peerResource, queryElement, msDelay) {
// Send the XML request and return the response
// ...
}
};
const client = new RPCClient(transport);
// Call remote method
const result = await client.callMethodAsync<string[]>(
{ jid: '[email protected]' },
'resource',
'system.listMethods',
60000
);
if (result.success) {
console.log('Available methods:', result.data);
}RPC Server with Access Control
const server = new RPCServer();
// Define access control
server.defineDelegateToAllowRPCRequests((methodName, peerJid, resource) => {
// Only allow requests from specific JIDs
return peerJid.endsWith('@mycompany.com');
});
// Add methods
server.addMethod('privateMethod', () => 'secret', 'A private method');Parsing XML-RPC Values
import { RPCParser } from './index';
import { parseXml, getChildElement } from './xml-utils';
// Parse XML to object
const xml = '<value><struct><member><name>id</name><value><string>123</string></value></member></struct></value>';
const doc = parseXml(`<root>${xml}</root>`);
const valueNode = getChildElement(doc.documentElement, 'value')!;
const result = RPCParser.getObjectFromValueNode(valueNode);
if (result.success) {
console.log(result.data); // { id: '123' }
}
// Convert object to XML
const obj = { name: 'John', age: 30 };
const xmlResult = RPCParser.getValueNodeFromObject(obj);
if (xmlResult.success) {
console.log(getOuterXml(xmlResult.data!));
}Running Tests
npm testBuilding
npm run buildFile Structure
typescript/
├── index.ts # Main entry point
├── types.ts # Type definitions
├── xml-utils.ts # XML utilities
├── rpc-parser.ts # XML-RPC parser
├── rpc-server.ts # RPC Server
├── rpc-client.ts # RPC Client
├── models.ts # Example models
├── tests.ts # Tests
├── package.json # NPM configuration
├── tsconfig.json # TypeScript configuration
└── README.md # This fileAPI Reference
RPCParser
Static class for parsing XML-RPC values.
getObjectFromValueNode<T>(node, typeConstructor?): Parse<value>node to objectgetValueNodeFromObject(obj, typeConstructor?, doc?): Convert object to<value>nodecreateXmlNodeForRPCMethodCall(methodName, ...params): Create<methodCall>nodecreateXmlNodeForRPCMethodResponse(result?): Create<methodResponse>nodeparseMethodCall(node): Parse<methodCall>nodeparseMethodResponse(node): Parse<methodResponse>node
RPCServer
RPC Server for handling method calls.
addMethod(name, handler, help, paramTypes, returnType): Add a methodremoveMethod(name?, handler?): Remove a methodcallMethodLocally(name, nodeParams): Call method with XML nodescallMethodLocallyWithObjects(name, ...params): Call method with objectsdefineDelegateToAllowRPCRequests(delegate): Set access control
RPCClient
RPC Client for calling remote methods.
callMethodAsync<T>(peer, resource, method, timeout, ...params): Call remote methodlistMethodsAsync(peer, resource, timeout): Get list of methodsgetMethodHelpAsync(peer, resource, method, timeout): Get method helpgetMethodSignaturesAsync(peer, resource, method, timeout): Get method signatures
License
MIT
