Comparing version 0.0.2 to 0.2.0
29
index.js
'use strict' | ||
var JID = require('./lib/JID') | ||
var tag = require('./lib/tag') | ||
const JID = require('./lib/JID') | ||
const escaping = require('./lib/escaping') | ||
const parse = require('./lib/parse') | ||
module.exports = function createJID (a, b, c) { | ||
if (Array.isArray(a)) { | ||
return tag.apply(null, arguments) | ||
function jid(...args) { | ||
if (!args[1] && !args[2]) { | ||
return parse(...args) | ||
} | ||
return new JID(...args) | ||
} | ||
return new JID(a, b, c) | ||
} | ||
module.exports.JID = JID | ||
module.exports.tag = tag | ||
module.exports.equal = function (a, b) { | ||
exports = module.exports = jid.bind() | ||
exports.jid = jid | ||
exports.JID = JID | ||
exports.equal = function (a, b) { | ||
return a.equals(b) | ||
} | ||
module.exports.is = function (a) { | ||
return a instanceof JID | ||
} | ||
exports.detectEscape = escaping.detect | ||
exports.escapeLocal = escaping.escape | ||
exports.unescapeLocal = escaping.unescape | ||
exports.parse = parse |
'use strict' | ||
module.exports.detect = function (local) { | ||
if (!local) return false | ||
if (!local) { | ||
return false | ||
} | ||
// remove all escaped sequences | ||
var tmp = local | ||
// Remove all escaped sequences | ||
const tmp = local | ||
.replace(/\\20/g, '') | ||
@@ -19,9 +21,8 @@ .replace(/\\22/g, '') | ||
// detect if we have unescaped sequences | ||
var search = tmp.search(/\\| |"|&|'|\/|:|<|>|@/g) | ||
// Detect if we have unescaped sequences | ||
const search = tmp.search(/\\| |"|&|'|\/|:|<|>|@/g) | ||
if (search === -1) { | ||
return false | ||
} else { | ||
return true | ||
} | ||
return true | ||
} | ||
@@ -37,3 +38,5 @@ | ||
module.exports.escape = function (local) { | ||
if (local === null) return null | ||
if (local === null) { | ||
return null | ||
} | ||
@@ -63,3 +66,5 @@ return local | ||
module.exports.unescape = function (local) { | ||
if (local === null) return null | ||
if (local === null) { | ||
return null | ||
} | ||
@@ -66,0 +71,0 @@ return local |
192
lib/JID.js
'use strict' | ||
var escaping = require('./escaping') | ||
const escaping = require('./escaping') | ||
@@ -13,98 +13,96 @@ /** | ||
*/ | ||
function JID (a, b, c) { | ||
this._local = null | ||
this.user = null // DEPRECATED | ||
this._domain = null | ||
this._resource = null | ||
class JID { | ||
constructor(local, domain, resource) { | ||
if (typeof domain !== 'string' || !domain) { | ||
throw new TypeError(`Invalid domain.`) | ||
} | ||
this.setDomain(domain) | ||
this.setLocal(typeof local === 'string' ? local : '') | ||
this.setResource(typeof resource === 'string' ? resource : '') | ||
} | ||
if (a && (!b) && (!c)) { | ||
this.parseJID(a) | ||
} else if (b) { | ||
this.setLocal(a) | ||
this.setDomain(b) | ||
this.setResource(c) | ||
} else { | ||
throw new Error('Argument error') | ||
[Symbol.toPrimitive](hint) { | ||
if (hint === 'number') { | ||
return NaN | ||
} | ||
return this.toString() | ||
} | ||
} | ||
JID.prototype.parseJID = function (s) { | ||
var resourceStart = s.indexOf('/') | ||
if (resourceStart !== -1) { | ||
this.setResource(s.substr(resourceStart + 1)) | ||
s = s.substr(0, resourceStart) | ||
toString(unescape) { | ||
let s = this._domain | ||
if (this._local) { | ||
s = this.getLocal(unescape) + '@' + s | ||
} | ||
if (this._resource) { | ||
s = s + '/' + this._resource | ||
} | ||
return s | ||
} | ||
var atStart = s.indexOf('@') | ||
if (atStart !== -1) { | ||
this.setLocal(s.substr(0, atStart)) | ||
s = s.substr(atStart + 1) | ||
/** | ||
* Convenience method to distinguish users | ||
* */ | ||
bare() { | ||
if (this._resource) { | ||
return new JID(this._local, this._domain, null) | ||
} | ||
return this | ||
} | ||
this.setDomain(s) | ||
} | ||
/** | ||
* Comparison function | ||
* */ | ||
equals(other) { | ||
return (this._local === other._local) && | ||
(this._domain === other._domain) && | ||
(this._resource === other._resource) | ||
} | ||
JID.prototype.toString = function (unescape) { | ||
var s = this._domain | ||
if (this._local) s = this.getLocal(unescape) + '@' + s | ||
if (this._resource) s = s + '/' + this._resource | ||
return s | ||
} | ||
/** | ||
* http://xmpp.org/rfcs/rfc6122.html#addressing-localpart | ||
* */ | ||
setLocal(local, escape) { | ||
escape = escape || escaping.detect(local) | ||
/** | ||
* Convenience method to distinguish users | ||
**/ | ||
JID.prototype.bare = function () { | ||
if (this._resource) { | ||
return new JID(this._local, this._domain, null) | ||
} else { | ||
if (escape) { | ||
local = escaping.escape(local) | ||
} | ||
this._local = local && local.toLowerCase() | ||
return this | ||
} | ||
} | ||
getLocal(unescape) { | ||
unescape = unescape || false | ||
let local = null | ||
/** | ||
* Comparison function | ||
**/ | ||
JID.prototype.equals = function (other) { | ||
return (this._local === other._local) && | ||
(this._domain === other._domain) && | ||
(this._resource === other._resource) | ||
} | ||
if (unescape) { | ||
local = escaping.unescape(this._local) | ||
} else { | ||
local = this._local | ||
} | ||
/** | ||
* http://xmpp.org/rfcs/rfc6122.html#addressing-localpart | ||
**/ | ||
JID.prototype.setLocal = function (local, escape) { | ||
escape = escape || escaping.detect(local) | ||
return local | ||
} | ||
if (escape) { | ||
local = escaping.escape(local) | ||
/** | ||
* http://xmpp.org/rfcs/rfc6122.html#addressing-domain | ||
*/ | ||
setDomain(domain) { | ||
this._domain = domain.toLowerCase() | ||
return this | ||
} | ||
getDomain() { | ||
return this._domain | ||
} | ||
this._local = local && local.toLowerCase() | ||
this.user = this._local | ||
return this | ||
} | ||
JID.prototype.setUser = function () { | ||
console.log('JID.setUser: Use JID.setLocal instead') | ||
this.setLocal.apply(this, arguments) | ||
} | ||
JID.prototype.getUser = function () { | ||
console.log('JID.getUser: Use JID.getLocal instead') | ||
return this.getLocal.apply(this, arguments) | ||
} | ||
JID.prototype.getLocal = function (unescape) { | ||
unescape = unescape || false | ||
var local = null | ||
if (unescape) { | ||
local = escaping.unescape(this._local) | ||
} else { | ||
local = this._local | ||
/** | ||
* http://xmpp.org/rfcs/rfc6122.html#addressing-resourcepart | ||
*/ | ||
setResource(resource) { | ||
this._resource = resource | ||
return this | ||
} | ||
return local | ||
getResource() { | ||
return this._resource | ||
} | ||
} | ||
@@ -114,43 +112,15 @@ | ||
get: JID.prototype.getLocal, | ||
set: JID.prototype.setLocal | ||
set: JID.prototype.setLocal, | ||
}) | ||
/** | ||
* http://xmpp.org/rfcs/rfc6122.html#addressing-domain | ||
*/ | ||
JID.prototype.setDomain = function (domain) { | ||
this._domain = domain.toLowerCase() | ||
return this | ||
} | ||
JID.prototype.getDomain = function () { | ||
return this._domain | ||
} | ||
Object.defineProperty(JID.prototype, 'domain', { | ||
get: JID.prototype.getDomain, | ||
set: JID.prototype.setDomain | ||
set: JID.prototype.setDomain, | ||
}) | ||
/** | ||
* http://xmpp.org/rfcs/rfc6122.html#addressing-resourcepart | ||
*/ | ||
JID.prototype.setResource = function (resource) { | ||
this._resource = resource | ||
return this | ||
} | ||
JID.prototype.getResource = function () { | ||
return this._resource | ||
} | ||
Object.defineProperty(JID.prototype, 'resource', { | ||
get: JID.prototype.getResource, | ||
set: JID.prototype.setResource | ||
set: JID.prototype.setResource, | ||
}) | ||
JID.prototype.detectEscape = escaping.detectEscape // DEPRECATED | ||
JID.prototype.escapeLocal = escaping.escape // DEPRECATED | ||
JID.prototype.unescapeLocal = escaping.unescape // DEPRECATED | ||
module.exports = JID |
@@ -7,3 +7,3 @@ { | ||
"bugs": "http://github.com/node-xmpp/node-xmpp/issues", | ||
"version": "0.0.2", | ||
"version": "0.2.0", | ||
"license": "ISC", | ||
@@ -13,7 +13,3 @@ "keywords": [ | ||
"JID" | ||
], | ||
"scripts": { | ||
"prepublish": "npm run bundle", | ||
"bundle": "../../node_modules/.bin/browserify index.js -s JID -o bundle.js" | ||
} | ||
] | ||
} |
@@ -22,16 +22,15 @@ JID | ||
```javascript | ||
var JID = require('@xmpp/jid') | ||
```js | ||
var jid = require('@xmpp/jid') | ||
/* | ||
* All return an instance of JID.JID, the new operator is optional. | ||
* All return an instance of jid.JID | ||
*/ | ||
var addr = new JID('alice@wonderland.net/rabbithole') // OK | ||
var addr = JID`${'alice'}@${'wonderland.net'}/${'rabbithole'}` // OK, es6 tagged template string | ||
var addr = new JID('alice', 'wonderland.net', 'rabbithole') // BEST; see section on escaping below | ||
var addr = jid('alice@wonderland.net/rabbithole') | ||
var addr = jid('alice', 'wonderland.net', 'rabbithole') | ||
addr instanceof JID.JID // true | ||
addr instanceof jid.JID // true | ||
// domain JIDs are created passing the domain as the first argument | ||
var addr = JID('wonderland.net') | ||
var addr = jid('wonderland.net') | ||
@@ -70,5 +69,3 @@ /* | ||
// same as | ||
JID.equal(addr, some_jid) | ||
JID.is(addr) // returns true if the passed argument is an instance of JID.JID, false otherwise | ||
jid.equal(addr, some_jid) | ||
``` | ||
@@ -78,18 +75,19 @@ | ||
The [XEP-0106](http://xmpp.org/extensions/xep-0106.html) defines a method to escape and unescape characters that aren't allowed in the local part of the JID. This library fully implement it but because `@` and `/` are ones of them and used as JID separators, you should always prefer the following syntax | ||
The [XEP-0106](http://xmpp.org/extensions/xep-0106.html) defines a method to escape and unescape characters that aren't allowed in the local part of the JID. This library fully implement it. | ||
```javascript | ||
// GOOD | ||
new JID(local, domain, resource) | ||
```js | ||
const addr = jid('contact@example.net', 'xmpp.net') | ||
addr.toString() // contact\40example.net@xmpp.net | ||
// for display purpose only | ||
addr.toString(true) // contact@example.net@xmpp.net | ||
``` | ||
over | ||
For user input, use | ||
```javascript | ||
// BAD | ||
new JID(local@domain/resource) | ||
```js | ||
jid('contact@example.net', 'xmpp.net') | ||
// over | ||
jid('contact@example.net@xmpp.net') | ||
``` | ||
for user input. | ||
## References | ||
@@ -96,0 +94,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
15
1
18842
526
100
2