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).
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
### Changed
|
||||||
|
- Improved XMPP vCard data parsing
|
||||||
### Fixed
|
### Fixed
|
||||||
- Added missing user-agent headers
|
- Added missing user-agent headers
|
||||||
|
### Removed
|
||||||
|
- jsdom dependency
|
||||||
|
|
||||||
## [0.18.0] - 2022-11-17
|
## [0.18.0] - 2022-11-17
|
||||||
### Changed
|
### Changed
|
||||||
|
|
|
@ -16,7 +16,6 @@
|
||||||
"express-validator": "^6.10.0",
|
"express-validator": "^6.10.0",
|
||||||
"hash-wasm": "^4.9.0",
|
"hash-wasm": "^4.9.0",
|
||||||
"irc-upd": "^0.11.0",
|
"irc-upd": "^0.11.0",
|
||||||
"jsdom": "^20.0.0",
|
|
||||||
"merge-options": "^3.0.3",
|
"merge-options": "^3.0.3",
|
||||||
"openpgp": "^5.5.0",
|
"openpgp": "^5.5.0",
|
||||||
"query-string": "^6.14.1",
|
"query-string": "^6.14.1",
|
||||||
|
@ -41,7 +40,7 @@
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"release": "yarn run test && yarn run release:bundle && yarn run release:minify",
|
"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",
|
"release:minify": "./node_modules/.bin/minify ./dist/doip.js > ./dist/doip.min.js",
|
||||||
"license:check": "./node_modules/.bin/license-check-and-add check",
|
"license:check": "./node_modules/.bin/license-check-and-add check",
|
||||||
"license:add": "./node_modules/.bin/license-check-and-add add",
|
"license:add": "./node_modules/.bin/license-check-and-add add",
|
||||||
|
|
|
@ -39,18 +39,24 @@ const processURI = (uri) => {
|
||||||
request: {
|
request: {
|
||||||
fetcher: E.Fetcher.XMPP,
|
fetcher: E.Fetcher.XMPP,
|
||||||
access: E.ProofAccess.SERVER,
|
access: E.ProofAccess.SERVER,
|
||||||
format: E.ProofFormat.TEXT,
|
format: E.ProofFormat.JSON,
|
||||||
data: {
|
data: {
|
||||||
id: `${match[1]}@${match[2]}`,
|
id: `${match[1]}@${match[2]}`
|
||||||
field: 'note'
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
claim: [{
|
claim: [
|
||||||
|
{
|
||||||
format: E.ClaimFormat.URI,
|
format: E.ClaimFormat.URI,
|
||||||
relation: E.ClaimRelation.CONTAINS,
|
relation: E.ClaimRelation.CONTAINS,
|
||||||
path: []
|
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
|
module.exports.timeout = 5000
|
||||||
|
|
||||||
if (jsEnv.isNode) {
|
if (jsEnv.isNode) {
|
||||||
const jsdom = require('jsdom')
|
|
||||||
const { client, xml } = require('@xmpp/client')
|
const { client, xml } = require('@xmpp/client')
|
||||||
const debug = require('@xmpp/debug')
|
const debug = require('@xmpp/debug')
|
||||||
const validator = require('validator')
|
const validator = require('validator')
|
||||||
|
@ -61,7 +60,6 @@ if (jsEnv.isNode) {
|
||||||
* @async
|
* @async
|
||||||
* @param {object} data - Data used in the request
|
* @param {object} data - Data used in the request
|
||||||
* @param {string} data.id - The identifier of the targeted account
|
* @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 {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.service - The server hostname on which the library can log in
|
||||||
* @param {string} opts.claims.xmpp.username - The username used to log in
|
* @param {string} opts.claims.xmpp.username - The username used to log in
|
||||||
|
@ -87,14 +85,6 @@ if (jsEnv.isNode) {
|
||||||
iqCaller = xmppStartRes.iqCaller
|
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
|
let timeoutHandle
|
||||||
const timeoutPromise = new Promise((resolve, reject) => {
|
const timeoutPromise = new Promise((resolve, reject) => {
|
||||||
timeoutHandle = setTimeout(
|
timeoutHandle = setTimeout(
|
||||||
|
@ -104,35 +94,89 @@ if (jsEnv.isNode) {
|
||||||
})
|
})
|
||||||
|
|
||||||
const fetchPromise = new Promise((resolve, reject) => {
|
const fetchPromise = new Promise((resolve, reject) => {
|
||||||
|
(async () => {
|
||||||
|
let completed = false
|
||||||
|
const vcard = {
|
||||||
|
url: [],
|
||||||
|
note: []
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try the vcard4 pubsub request
|
||||||
|
if (!completed) {
|
||||||
try {
|
try {
|
||||||
let vcard
|
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
|
||||||
|
)
|
||||||
|
|
||||||
switch (data.field.toLowerCase()) {
|
// Traverse the XML response
|
||||||
case 'desc':
|
response.getChild('pubsub').getChildren('items').forEach(items => {
|
||||||
case 'note':
|
if (items.attrs.node === 'urn:xmpp:vcard4') {
|
||||||
vcard = dom.window.document.querySelector('note text')
|
items.getChildren('item').forEach(item => {
|
||||||
if (!vcard) {
|
if (item.attrs.id === 'current') {
|
||||||
vcard = dom.window.document.querySelector('note')
|
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'))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
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
|
|
||||||
}
|
|
||||||
xmpp.stop()
|
|
||||||
resolve(vcard)
|
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) {
|
} catch (error) {
|
||||||
reject(error)
|
reject(error)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
xmpp.stop()
|
||||||
|
})()
|
||||||
})
|
})
|
||||||
|
|
||||||
return Promise.race([fetchPromise, timeoutPromise]).then((result) => {
|
return Promise.race([fetchPromise, timeoutPromise]).then((result) => {
|
||||||
|
|
Loading…
Reference in a new issue