h2lean 0.17.2 → 0.17.3

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 (2) hide show
  1. package/lib/hpack.js +25 -7
  2. package/package.json +1 -1
package/lib/hpack.js CHANGED
@@ -250,25 +250,43 @@ function huffEncode(src, start, end, dst, off) {
250
250
  // so the byte of a character is its code unit & 0xff -- the byte the buffer version
251
251
  // above would have been handed. Coding straight from the string saves the copy into
252
252
  // a scratch buffer and the view of it that the copy needed (OPTIMIZATION.md item 12).
253
+ // The accumulator is kept below 2^31 so emitting an octet is two bitwise ops
254
+ // instead of a floating-point division by POW2[nbits]. A code that does not fit
255
+ // in the remaining room is split: its high bits fill the accumulator, the whole
256
+ // octets are flushed, then its low bits are appended. Only the rare 19-30 bit
257
+ // codes take that path; digits and letters are 5-8 bits and never split.
253
258
  function huffEncodeStr(s, dst, off) {
254
259
  let acc = 0;
255
260
  let nbits = 0;
256
261
  for (let i = 0; i < s.length; i++) {
257
262
  const sym = s.charCodeAt(i) & 0xff;
258
263
  const b = HUFF_BITS[sym];
259
- acc = acc * POW2[b] + HUFF_CODES[sym];
260
- nbits += b;
264
+ const code = HUFF_CODES[sym];
265
+ const room = 31 - nbits;
266
+ if (b <= room) {
267
+ acc = (acc << b) | code;
268
+ nbits += b;
269
+ } else {
270
+ acc = (acc << room) | (code >>> (b - room));
271
+ nbits = 31;
272
+ while (nbits >= 8) {
273
+ nbits -= 8;
274
+ dst[off++] = (acc >>> nbits) & 0xff;
275
+ acc &= (1 << nbits) - 1;
276
+ }
277
+ const rest = b - room;
278
+ acc = (acc << rest) | (code & (2 ** rest - 1));
279
+ nbits += rest;
280
+ }
261
281
  while (nbits >= 8) {
262
282
  nbits -= 8;
263
- const byte = Math.floor(acc / POW2[nbits]);
264
- dst[off++] = byte;
265
- acc -= byte * POW2[nbits];
283
+ dst[off++] = (acc >>> nbits) & 0xff;
284
+ acc &= (1 << nbits) - 1;
266
285
  }
267
286
  }
268
287
  // EOS is the all-ones code, so padding the last byte with ones *is* EOS
269
288
  if (nbits > 0) {
270
- const pad = POW2[8 - nbits] - 1;
271
- dst[off++] = acc * POW2[8 - nbits] + pad;
289
+ dst[off++] = (acc << (8 - nbits)) | (2 ** (8 - nbits) - 1);
272
290
  }
273
291
  return off;
274
292
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "h2lean",
3
- "version": "0.17.2",
3
+ "version": "0.17.3",
4
4
  "description": "Pure-JavaScript HTTP/2 for Node.js — a drop-in node:http2 replacement (server and client, h2c and TLS) with zero dependencies",
5
5
  "main": "index.js",
6
6
  "files": [