Skip to content
Permalink
Newer
Older
100644 182 lines (168 sloc) 5.06 KB
Ignoring revisions in .git-blame-ignore-revs.
February 1, 2022 18:01
1
'use strict'
2
3
const {Buffer} = require('buffer')
4
const encoder = require('./encoder')
5
const decoder = require('./decoder')
6
const {MT} = require('./constants')
7
8
/**
9
* Wrapper around a JavaScript Map object that allows the keys to be
10
* any complex type. The base Map object allows this, but will only
11
* compare the keys by identity, not by value. CborMap translates keys
12
* to CBOR first (and base64's them to ensure by-value comparison).
13
*
14
* This is not a subclass of Object, because it would be tough to get
15
* the semantics to be an exact match.
16
*
17
* @extends Map
18
*/
19
class CborMap extends Map {
20
/**
21
* Creates an instance of CborMap.
22
*
23
* @param {Iterable<any>} [iterable] An Array or other iterable
24
* object whose elements are key-value pairs (arrays with two elements, e.g.
25
* <code>[[ 1, 'one' ],[ 2, 'two' ]]</code>). Each key-value pair is added
26
* to the new CborMap; null values are treated as undefined.
27
*/
28
constructor(iterable) {
29
super(iterable)
30
}
31
32
/**
33
* @ignore
34
*/
35
static _encode(key) {
36
return encoder.encodeCanonical(key).toString('base64')
37
}
38
39
/**
40
* @ignore
41
*/
42
static _decode(key) {
43
return decoder.decodeFirstSync(key, 'base64')
44
}
45
46
/**
47
* Retrieve a specified element.
48
*
49
* @param {any} key The key identifying the element to retrieve.
50
* Can be any type, which will be serialized into CBOR and compared by
51
* value.
52
* @returns {any} The element if it exists, or <code>undefined</code>.
53
*/
54
get(key) {
55
return super.get(CborMap._encode(key))
56
}
57
58
/**
59
* Adds or updates an element with a specified key and value.
60
*
61
* @param {any} key The key identifying the element to store.
62
* Can be any type, which will be serialized into CBOR and compared by
63
* value.
64
* @param {any} val The element to store.
65
* @returns {this} This object.
66
*/
67
set(key, val) {
68
return super.set(CborMap._encode(key), val)
69
}
70
71
/**
72
* Removes the specified element.
73
*
74
* @param {any} key The key identifying the element to delete. Can be any
75
* type, which will be serialized into CBOR and compared by value.
76
* @returns {boolean} True if an element in the Map object existed and has
77
* been removed, or false if the element does not exist.
78
*/
79
delete(key) {
80
return super.delete(CborMap._encode(key))
81
}
82
83
/**
84
* Does an element with the specified key exist?
85
*
86
* @param {any} key The key identifying the element to check.
87
* Can be any type, which will be serialized into CBOR and compared by
88
* value.
89
* @returns {boolean} True if an element with the specified key exists in
90
* the Map object; otherwise false.
91
*/
92
has(key) {
93
return super.has(CborMap._encode(key))
94
}
95
96
/**
97
* Returns a new Iterator object that contains the keys for each element
98
* in the Map object in insertion order. The keys are decoded into their
99
* original format.
100
*
101
* @yields {any} The keys of the map.
102
*/
103
*keys() {
104
for (const k of super.keys()) {
105
yield CborMap._decode(k)
106
}
107
}
108
109
/* eslint-disable jsdoc/require-returns-check */
110
/**
111
* Returns a new Iterator object that contains the [key, value] pairs for
112
* each element in the Map object in insertion order.
113
*
114
* @yields {any[]} Key value pairs.
115
* @returns {IterableIterator<any, any>} Key value pairs.
116
*/
117
*entries() {
118
for (const kv of super.entries()) {
119
yield [CborMap._decode(kv[0]), kv[1]]
120
}
121
}
122
/* eslint-enable jsdoc/require-returns-check */
123
124
/**
125
* Returns a new Iterator object that contains the [key, value] pairs for
126
* each element in the Map object in insertion order.
127
*
128
* @returns {IterableIterator} Key value pairs.
129
*/
130
[Symbol.iterator]() {
131
return this.entries()
132
}
133
134
/**
135
* Executes a provided function once per each key/value pair in the Map
136
* object, in insertion order.
137
*
138
* @param {function(any, any, Map): undefined} fun Function to execute for
139
* each element, which takes a value, a key, and the Map being traversed.
140
* @param {any} thisArg Value to use as this when executing callback.
141
* @throws {TypeError} Invalid function.
142
*/
143
forEach(fun, thisArg) {
144
if (typeof fun !== 'function') {
145
throw new TypeError('Must be function')
146
}
147
for (const kv of super.entries()) {
148
fun.call(this, kv[1], CborMap._decode(kv[0]), this)
149
}
150
}
151
152
/**
153
* Push the simple value onto the CBOR stream.
154
*
155
* @param {object} gen The generator to push onto.
156
* @returns {boolean} True on success.
157
*/
158
encodeCBOR(gen) {
159
if (!gen._pushInt(this.size, MT.MAP)) {
160
return false
161
}
162
if (gen.canonical) {
163
const entries = Array.from(super.entries())
164
.map(kv => [Buffer.from(kv[0], 'base64'), kv[1]])
165
entries.sort((a, b) => a[0].compare(b[0]))
166
for (const kv of entries) {
167
if (!(gen.push(kv[0]) && gen.pushAny(kv[1]))) {
168
return false
169
}
170
}
171
} else {
172
for (const kv of super.entries()) {
173
if (!(gen.push(Buffer.from(kv[0], 'base64')) && gen.pushAny(kv[1]))) {
174
return false
175
}
176
}
177
}
178
return true
179
}
180
}
181
182
module.exports = CborMap