-
Notifications
You must be signed in to change notification settings - Fork 1
/
chunk.js
245 lines (217 loc) · 5.56 KB
/
chunk.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
const { ID } = require('./id')
const assert = require('nanoassert')
/**
* A `Chunk` is a container for bytes with an ID and size.
* @class Chunk
* @extends Buffer (Uint8Array)
*/
class Chunk extends Uint8Array {
/**
* Return the header of a `Chunks` buffer.
* @static
* @param {Buffer|Chunk} buffer
* @return {Object}
*/
static header(buffer) {
// istanbul ignore next
const size = buffer.length >= 8 ? buffer.readUInt32BE(4) : 0
const id = ID.from(buffer.slice(0, 4))
return { id, size }
}
/**
* Create a new `Chunk` from a given `buffer` with options.
* @static
* @param {Buffer} buffer
* @return {Chunk}
*/
static from(buffer, opts) {
if (!opts) {
opts = {}
}
if (buffer instanceof Chunk) {
return new this(buffer.id, {
ancestor: opts.ancestor || buffer.ancestor,
size: opts.size || buffer.length,
})
}
buffer = Buffer.from(buffer)
const { ancestor } = opts
// istanbul ignore next
const size = opts.size || (buffer.length >= 8 ? buffer.readUInt32BE(4) : buffer.length) || buffer.length
const id = ID.from(buffer.slice(0, 4))
const chunk = new this(id, { size: size, ancestor })
chunk.set(buffer.slice(8, 8 + size))
return chunk
}
/**
* `Chunk` class constructor.
* @param {String|Buffer|ID|Uint8Array|Number} id
* @param {Object} opts
* @param {Number} opts.size
* @param {?(Chunk)} opts.ancestor
*/
constructor(id, opts) {
assert(id, 'Expecting `id` to be something. Got: ' + typeof id)
assert(opts && 'object' === typeof opts, 'Expecting `options` to be an object')
assert(opts.size >= 0, 'Expecting `options.size > 0`')
const { ancestor = null } = opts
const { size } = opts
const pad = size % 2 ? 1 : 0
super(size + pad)
Object.defineProperties(this, {
ancestor: { value: ancestor , enumerable: false, writable: true },
size: { value: size, enumerable: false, writable: true },
id: { value: ID.from(id), enumerable: false },
})
}
/**
* Return a pointer to the underlying data for this chunk as a `Buffer`.
* @accessor
* @type {Buffer}
*/
get data() {
return Buffer.from(this.buffer)
}
/**
* Set bytes on the instance at an optional offset.
* @param {Buffer} bytes
* @param {?(Number)} offset
*/
set(bytes, offset) {
if ('string' === typeof bytes) {
bytes = Buffer.from(bytes)
}
return super.set(bytes, offset)
}
/**
* Map over the chunks in this chunk returning a new `Chunk` instance.
* @return {Chunk}
*/
map(...args) {
const mapped = Buffer.from(this.toArray().map(...args))
const chunk = new this.constructor(this.id, {
ancestor: this,
size: mapped.length,
})
chunk.set(mapped)
return chunk
}
/**
* Filter over the chunks in this chunk returning a new `Chunk` instance.
* @return {Chunk}
*/
filter(...args) {
const filtered = Buffer.from(this.toArray().filter(...args))
const chunk = new this.constructor(this.id, {
ancestor: this,
size: filtered.length,
})
chunk.set(filtered)
return chunk
}
/**
* Creates a new `Chunk` instance as a slice from this instance.
* @return {Chunk}
*/
slice(...args) {
const sliced = Buffer.from(this.toArray().slice(...args))
const chunk = new this.constructor(this.id, {
ancestor: this,
size: sliced.length,
})
chunk.set(sliced)
return chunk
}
/**
* Convert this instance into an `Array`.
* @return {Array}
*/
toArray() {
return Array.from(this)
}
/**
* Converts the `Chunk` to a `Buffer`, including the ID and size bytes.
* @return {Buffer}
*/
toBuffer() {
const { data } = this
const size = Buffer.alloc(4)
// istanbul ignore next
const pad = this.length % 2 && 0 == data[data.length - 1] ? -1 : 0
const id = this.id.toBuffer()
size.writeUIntBE(this.length + pad, 0, 4)
return Buffer.concat([ id, size, data ])
}
}
// inherit `Buffer`, working around 'DEP0005'
Object.setPrototypeOf(Chunk.prototype, Buffer.prototype)
/**
* A `Chunk` iterator that implements the _Iterator Protocol_ and
* satifies the _Iterable_ interface requirements.
* @class ChunkIterator
*/
class ChunkIterator {
/**
* An alias to the `ChunkIterator` class constructor.
* @static
* @param {Buffer} buffer
* @param {?(Number)} offset
* @return {ChunkIterator}
*/
static from(buffer, offset) {
return new this(buffer, offset)
}
/**
* `ChunkIterator` class constructor.
* @private
* @param {Buffer} buffer
* @param {?(Number)} offset
*/
// istanbul-ignore-next
constructor(buffer, offset) {
this.buffer = buffer
this.offset = offset || 0
}
/**
* `true` when the iterator is done.
* @accessor
* @type {Boolean}
*/
get done() {
return this.offset >= this.buffer.length
}
/**
* A `Chunk` value for the current iteration.
* @accessor
* @type {?(Chunk)}
*/
get value() {
const { buffer, offset, done } = this
return false === done ? Chunk.from(buffer.slice(offset)) : null
}
/**
* Implements `@@iterator` for `for..of` support.
* @return {Iterable}
*/
[Symbol.iterator]() {
return this
}
/**
* Returns the next `value` and `done` state for this iteration.
* @return {Object}
*/
next() {
const { value, done } = this
if (value && !done) {
this.offset += Math.min(value.length + 8, this.buffer.length)
}
return { value, done }
}
}
/**
* Module exports.
*/
module.exports = {
Chunk,
ChunkIterator,
}