compact-encoding 2.6.0 → 2.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +9 -12
  2. package/package.json +1 -1
  3. package/test.js +0 -3
package/index.js CHANGED
@@ -173,19 +173,18 @@ exports.buffer = {
173
173
  decode (state) {
174
174
  const len = uint.decode(state)
175
175
  if (len === 0) return null
176
- const b = state.buffer.subarray(state.start, state.start += len)
177
- if (b.length !== len) throw new Error('Out of bounds')
178
- return b
176
+ if (state.end - state.start < len) throw new Error('Out of bounds')
177
+ return state.buffer.subarray(state.start, (state.start += len))
179
178
  }
180
179
  }
181
180
 
182
181
  const raw = exports.raw = {
183
182
  preencode (state, b) {
184
- state.end += b.length
183
+ state.end += b.byteLength
185
184
  },
186
185
  encode (state, b) {
187
186
  state.buffer.set(b, state.start)
188
- state.start += b.length
187
+ state.start += b.byteLength
189
188
  },
190
189
  decode (state) {
191
190
  const b = state.buffer.subarray(state.start, state.end)
@@ -251,9 +250,8 @@ exports.string = {
251
250
  },
252
251
  decode (state) {
253
252
  const len = uint.decode(state)
254
- const s = b4a.toString(state.buffer, 'utf-8', state.start, state.start += len)
255
- if (b4a.byteLength(s) !== len || state.start > state.end) throw new Error('Out of bounds')
256
- return s
253
+ if (state.end - state.start < len) throw new Error('Out of bounds')
254
+ return b4a.toString(state.buffer, 'utf-8', state.start, (state.start += len))
257
255
  }
258
256
  }
259
257
 
@@ -280,9 +278,8 @@ const fixed = exports.fixed = function fixed (n) {
280
278
  state.start += n
281
279
  },
282
280
  decode (state) {
283
- const b = state.buffer.subarray(state.start, state.start += n)
284
- if (b.length !== n) throw new Error('Out of bounds')
285
- return b
281
+ if (state.end - state.start < n) throw new Error('Out of bounds')
282
+ return state.buffer.subarray(state.start, (state.start += n))
286
283
  }
287
284
  }
288
285
  }
@@ -336,7 +333,7 @@ function fromCodec (enc) {
336
333
  preencode (state, m) {
337
334
  tmpM = m
338
335
  tmpBuf = enc.encode(m)
339
- state.end += tmpBuf.length
336
+ state.end += tmpBuf.byteLength
340
337
  },
341
338
  encode (state, m) {
342
339
  raw.encode(state, m === tmpM ? tmpBuf : enc.encode(m))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "compact-encoding",
3
- "version": "2.6.0",
3
+ "version": "2.6.1",
4
4
  "description": "A series of compact encoding schemes for building small and fast parsers and serializers",
5
5
  "main": "index.js",
6
6
  "dependencies": {
package/test.js CHANGED
@@ -170,9 +170,6 @@ tape('buffer', function (t) {
170
170
  t.is(state.start, state.end)
171
171
 
172
172
  t.exception(() => enc.buffer.decode(state))
173
- state.buffer = state.buffer.subarray(0, 8)
174
- state.start = 3
175
- t.exception(() => enc.buffer.decode(state), 'partial throws')
176
173
  })
177
174
 
178
175
  tape('raw', function (t) {