Skip to content

Commit 4f8cdd1

Browse files
authored
fix(lib/schemes/urn): check nid for undefined (#141)
* fix(lib/schemes/urn): check nid for undefined * Throw error on undefined nid * Restore test for equals * remove dups after rebase
1 parent eedf840 commit 4f8cdd1

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

lib/schemes.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ function urnParse (urnComponent, options) {
152152

153153
/** @type {SchemeFn} */
154154
function urnSerialize (urnComponent, options) {
155+
if (urnComponent.nid === undefined) {
156+
throw new Error('URN without nid cannot be serialized')
157+
}
155158
const scheme = options.scheme || urnComponent.scheme || 'urn'
156159
const nid = urnComponent.nid.toLowerCase()
157160
const urnScheme = `${scheme}:${options.nid || nid}`

test/equal.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ test('URN Equals', (t) => {
6565
// t.equal(URI.equal('urn:foo:a123%2C456', 'URN:FOO:a123%2c456'), true)
6666

6767
runTest(t, suite)
68+
69+
t.throws(() => {
70+
fn('urn:', 'urn:FOO:a123,456')
71+
}, 'URN without nid cannot be serialized')
72+
6873
t.end()
6974
})
7075

test/serialize.test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ test('WSS serialize', (t) => {
101101
})
102102

103103
test('URN serialize', (t) => {
104-
// example from RFC 2141
104+
// example from RFC 2141
105105
const components = {
106106
scheme: 'urn',
107107
nid: 'foo',
@@ -122,6 +122,14 @@ test('URN serialize', (t) => {
122122
uuid: 'notauuid-7dec-11d0-a765-00a0c91e6bf6'
123123
}
124124
t.equal(fastURI.serialize(uuidcomponents), 'urn:uuid:notauuid-7dec-11d0-a765-00a0c91e6bf6')
125+
126+
uuidcomponents = {
127+
scheme: 'urn',
128+
nid: undefined,
129+
uuid: 'notauuid-7dec-11d0-a765-00a0c91e6bf6'
130+
}
131+
t.throws(() => { fastURI.serialize(uuidcomponents) }, 'URN without nid cannot be serialized')
132+
125133
t.end()
126134
})
127135
test('URN NID Override', (t) => {

0 commit comments

Comments
 (0)