caldav-adapter
Version:
CalDAV server for Node.js and Koa. Modernized and maintained for Forward Email.
44 lines (34 loc) • 1.11 kB
JavaScript
const xpath = require('xpath');
module.exports = {};
const namespaces = {
D: 'DAV:',
CAL: 'urn:ietf:params:xml:ns:caldav',
CS: 'http://calendarserver.org/ns/',
ICAL: 'http://apple.com/ns/ical/'
};
module.exports.namespaces = namespaces;
const nsLookup = {
'DAV:': 'D',
'urn:ietf:params:xml:ns:caldav': 'CAL',
'http://calendarserver.org/ns/': 'CS',
'http://apple.com/ns/ical/': 'ICAL'
};
module.exports.nsLookup = nsLookup;
const select = xpath.useNamespaces(namespaces);
function get(path, doc) {
// Validate that doc is a proper XML document
// Return empty array for null/invalid documents instead of throwing
// This handles cases where the request body was not received or parsed
if (!doc || typeof doc !== 'object') {
return [];
}
return select(path, doc);
}
module.exports.get = get;
function getWithChildren(path, doc) {
const propNode = get(path, doc);
// eslint-disable-next-line unicorn/prefer-spread
const children = propNode[0] ? Array.from(propNode[0].childNodes) : [];
return { propNode, children };
}
module.exports.getWithChildren = getWithChildren;