forked from Mirrors/doipjs
Improve XMPP parsing, remove jsdom dependency
This commit is contained in:
parent
1720c59093
commit
05fa92063a
4 changed files with 100 additions and 47 deletions
|
@ -5,8 +5,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [Unreleased]
|
||||
### Changed
|
||||
- Improved XMPP vCard data parsing
|
||||
### Fixed
|
||||
- Added missing user-agent headers
|
||||
### Removed
|
||||
- jsdom dependency
|
||||
|
||||
## [0.18.0] - 2022-11-17
|
||||
### Changed
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
"express-validator": "^6.10.0",
|
||||
"hash-wasm": "^4.9.0",
|
||||
"irc-upd": "^0.11.0",
|
||||
"jsdom": "^20.0.0",
|
||||
"merge-options": "^3.0.3",
|
||||
"openpgp": "^5.5.0",
|
||||
"query-string": "^6.14.1",
|
||||
|
@ -41,7 +40,7 @@
|
|||
},
|
||||
"scripts": {
|
||||
"release": "yarn run test && yarn run release:bundle && yarn run release:minify",
|
||||
"release:bundle": "./node_modules/.bin/browserify ./src/index.js --standalone doip -x openpgp -x jsdom -x @xmpp/client -x @xmpp/debug -x irc-upd -o ./dist/doip.js",
|
||||
"release:bundle": "./node_modules/.bin/browserify ./src/index.js --standalone doip -x openpgp -x @xmpp/client -x @xmpp/debug -x irc-upd -o ./dist/doip.js",
|
||||
"release:minify": "./node_modules/.bin/minify ./dist/doip.js > ./dist/doip.min.js",
|
||||
"license:check": "./node_modules/.bin/license-check-and-add check",
|
||||
"license:add": "./node_modules/.bin/license-check-and-add add",
|
||||
|
|
|
@ -39,18 +39,24 @@ const processURI = (uri) => {
|
|||
request: {
|
||||
fetcher: E.Fetcher.XMPP,
|
||||
access: E.ProofAccess.SERVER,
|
||||
format: E.ProofFormat.TEXT,
|
||||
format: E.ProofFormat.JSON,
|
||||
data: {
|
||||
id: `${match[1]}@${match[2]}`,
|
||||
field: 'note'
|
||||
id: `${match[1]}@${match[2]}`
|
||||
}
|
||||
}
|
||||
},
|
||||
claim: [{
|
||||
format: E.ClaimFormat.URI,
|
||||
relation: E.ClaimRelation.CONTAINS,
|
||||
path: []
|
||||
}]
|
||||
claim: [
|
||||
{
|
||||
format: E.ClaimFormat.URI,
|
||||
relation: E.ClaimRelation.CONTAINS,
|
||||
path: ['url']
|
||||
},
|
||||
{
|
||||
format: E.ClaimFormat.URI,
|
||||
relation: E.ClaimRelation.CONTAINS,
|
||||
path: ['note']
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -26,7 +26,6 @@ const jsEnv = require('browser-or-node')
|
|||
module.exports.timeout = 5000
|
||||
|
||||
if (jsEnv.isNode) {
|
||||
const jsdom = require('jsdom')
|
||||
const { client, xml } = require('@xmpp/client')
|
||||
const debug = require('@xmpp/debug')
|
||||
const validator = require('validator')
|
||||
|
@ -61,7 +60,6 @@ if (jsEnv.isNode) {
|
|||
* @async
|
||||
* @param {object} data - Data used in the request
|
||||
* @param {string} data.id - The identifier of the targeted account
|
||||
* @param {string} data.field - The vCard field to return (should be "note")
|
||||
* @param {object} opts - Options used to enable the request
|
||||
* @param {string} opts.claims.xmpp.service - The server hostname on which the library can log in
|
||||
* @param {string} opts.claims.xmpp.username - The username used to log in
|
||||
|
@ -87,14 +85,6 @@ if (jsEnv.isNode) {
|
|||
iqCaller = xmppStartRes.iqCaller
|
||||
}
|
||||
|
||||
const response = await iqCaller.request(
|
||||
xml('iq', { type: 'get', to: data.id }, xml('vCard', 'vcard-temp')),
|
||||
30 * 1000
|
||||
)
|
||||
|
||||
const vcardRow = response.getChild('vCard', 'vcard-temp').toString()
|
||||
const dom = new jsdom.JSDOM(vcardRow)
|
||||
|
||||
let timeoutHandle
|
||||
const timeoutPromise = new Promise((resolve, reject) => {
|
||||
timeoutHandle = setTimeout(
|
||||
|
@ -104,35 +94,89 @@ if (jsEnv.isNode) {
|
|||
})
|
||||
|
||||
const fetchPromise = new Promise((resolve, reject) => {
|
||||
try {
|
||||
let vcard
|
||||
|
||||
switch (data.field.toLowerCase()) {
|
||||
case 'desc':
|
||||
case 'note':
|
||||
vcard = dom.window.document.querySelector('note text')
|
||||
if (!vcard) {
|
||||
vcard = dom.window.document.querySelector('note')
|
||||
}
|
||||
if (!vcard) {
|
||||
vcard = dom.window.document.querySelector('DESC')
|
||||
}
|
||||
if (vcard) {
|
||||
vcard = vcard.textContent
|
||||
} else {
|
||||
throw new Error('No DESC or NOTE field found in vCard')
|
||||
}
|
||||
break
|
||||
|
||||
default:
|
||||
vcard = dom.window.document.querySelector(data).textContent
|
||||
break
|
||||
(async () => {
|
||||
let completed = false
|
||||
const vcard = {
|
||||
url: [],
|
||||
note: []
|
||||
}
|
||||
|
||||
// Try the vcard4 pubsub request
|
||||
if (!completed) {
|
||||
try {
|
||||
const response = await iqCaller.request(
|
||||
xml('iq', { type: 'get', to: data.id }, xml('pubsub', 'http://jabber.org/protocol/pubsub', xml('items', { node: 'urn:xmpp:vcard4', max_items: '1' }))),
|
||||
30 * 1000
|
||||
)
|
||||
|
||||
// Traverse the XML response
|
||||
response.getChild('pubsub').getChildren('items').forEach(items => {
|
||||
if (items.attrs.node === 'urn:xmpp:vcard4') {
|
||||
items.getChildren('item').forEach(item => {
|
||||
if (item.attrs.id === 'current') {
|
||||
const itemVcard = item.getChild('vcard', 'urn:ietf:params:xml:ns:vcard-4.0')
|
||||
// Find the vCard URLs
|
||||
itemVcard.getChildren('url').forEach(url => {
|
||||
vcard.url.push(url.getChildText('uri'))
|
||||
})
|
||||
// Find the vCard notes
|
||||
itemVcard.getChildren('note').forEach(note => {
|
||||
vcard.note.push(note.getChildText('text'))
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
resolve(vcard)
|
||||
completed = true
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
// // Try the vcard4 IQ request (not implemented on any server yet)
|
||||
// if (!completed) {
|
||||
// try {
|
||||
// const response = await iqCaller.request(
|
||||
// xml('iq', { type: 'get', to: data.id }, xml('vcard', 'urn:ietf:params:xml:ns:vcard-4.0' )),
|
||||
// 30 * 1000
|
||||
// )
|
||||
|
||||
// // Traverse the XML response
|
||||
|
||||
// resolve(vcard)
|
||||
// completed = true
|
||||
// } catch (_) {}
|
||||
// }
|
||||
|
||||
// Try the vcard-temp IQ request
|
||||
if (!completed) {
|
||||
try {
|
||||
const response = await iqCaller.request(
|
||||
xml('iq', { type: 'get', to: data.id }, xml('vCard', 'vcard-temp')),
|
||||
30 * 1000
|
||||
)
|
||||
|
||||
// Find the vCard URLs
|
||||
response.getChild('vCard', 'vcard-temp').getChildren('URL').forEach(url => {
|
||||
vcard.url.push(url.children[0])
|
||||
})
|
||||
// Find the vCard notes
|
||||
response.getChild('vCard', 'vcard-temp').getChildren('NOTE').forEach(note => {
|
||||
vcard.note.push(note.children[0])
|
||||
})
|
||||
response.getChild('vCard', 'vcard-temp').getChildren('DESC').forEach(note => {
|
||||
vcard.note.push(note.children[0])
|
||||
})
|
||||
|
||||
resolve(vcard)
|
||||
completed = true
|
||||
} catch (error) {
|
||||
reject(error)
|
||||
}
|
||||
}
|
||||
|
||||
xmpp.stop()
|
||||
resolve(vcard)
|
||||
} catch (error) {
|
||||
reject(error)
|
||||
}
|
||||
})()
|
||||
})
|
||||
|
||||
return Promise.race([fetchPromise, timeoutPromise]).then((result) => {
|
||||
|
|
Loading…
Reference in a new issue