-
Notifications
You must be signed in to change notification settings - Fork 1
/
extensions.js
69 lines (61 loc) · 1.39 KB
/
extensions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const assert = require('nanoassert')
/**
* Normalize a key into a string.
* @private
*/
function normalizeKey(key) {
if (key && 'string' !== typeof key && 'function' === typeof key.toString) {
key = key.toString()
}
return key
}
/**
* A special `Map` for extensions.
* @class Extensions
* @extends Map
*/
class Extensions extends Map {
/**
* Overloads `Map.prototype.set()` to handle `ID` and `Buffer` like
* inputs as `key`.
* @param {Mixed} key
* @param {Mixed} extension
*/
set(key, extension) {
key = normalizeKey(key)
assert(key && 4 === key.length, 'invalid extension key length')
return super.set(key, extension)
}
/**
* Overloads `Map.prototype.get()` to handle `ID` and `Buffer` like
* inputs as `key`.
* @param {Mixed} key
*/
get(key) {
return super.get(normalizeKey(key))
}
/**
* Overloads `Map.prototype.delete()` to handle `ID` and `Buffer` like
* inputs as `key`.
* @param {Mixed} key
*/
delete(key) {
key = normalizeKey(key)
assert(key && 4 === key.length, 'invalid extension key length')
return super.delete(key)
}
/**
* Overloads `Map.prototype.has()` to handle `ID` and `Buffer` like
* inputs as `key`.
* @param {Mixed} key
*/
has(key) {
return super.has(normalizeKey(key))
}
}
/**
* Module exports.
*/
module.exports = Object.assign(new Extensions(), {
Extensions
})