source: trip-planner-front/node_modules/hpack.js/lib/hpack/table.js@ 8d391a1

Last change on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.2 KB
Line 
1var hpack = require('../hpack');
2var utils = hpack.utils;
3var assert = utils.assert;
4
5function Table(options) {
6 this['static'] = hpack['static-table'];
7 this.dynamic = [];
8 this.size = 0;
9 this.maxSize = 0;
10 this.length = this['static'].table.length;
11 this.protocolMaxSize = options.maxSize;
12 this.maxSize = this.protocolMaxSize;
13 this.lookupDepth = options.lookupDepth || 32;
14}
15module.exports = Table;
16
17Table.create = function create(options) {
18 return new Table(options);
19};
20
21Table.prototype.lookup = function lookup(index) {
22 assert(index !== 0, 'Zero indexed field');
23 assert(index <= this.length, 'Indexed field OOB')
24
25 if (index <= this['static'].table.length)
26 return this['static'].table[index - 1];
27 else
28 return this.dynamic[this.length - index];
29};
30
31Table.prototype.reverseLookup = function reverseLookup(name, value) {
32 var staticEntry = this['static'].map[name];
33 if (staticEntry && staticEntry.values[value])
34 return staticEntry.values[value];
35
36 // Reverse search dynamic table (new items are at the end of it)
37 var limit = Math.max(0, this.dynamic.length - this.lookupDepth);
38 for (var i = this.dynamic.length - 1; i >= limit; i--) {
39 var entry = this.dynamic[i];
40 if (entry.name === name && entry.value === value)
41 return this.length - i;
42
43 if (entry.name === name) {
44 // Prefer smaller index
45 if (staticEntry)
46 break;
47 return -(this.length - i);
48 }
49 }
50
51 if (staticEntry)
52 return -staticEntry.index;
53
54 return 0;
55};
56
57Table.prototype.add = function add(name, value, nameSize, valueSize) {
58 var totalSize = nameSize + valueSize + 32;
59
60 this.dynamic.push({
61 name: name,
62 value: value,
63 nameSize: nameSize,
64 totalSize: totalSize
65 });
66 this.size += totalSize;
67 this.length++;
68
69 this.evict();
70};
71
72Table.prototype.evict = function evict() {
73 while (this.size > this.maxSize) {
74 var entry = this.dynamic.shift();
75 this.size -= entry.totalSize;
76 this.length--;
77 }
78 assert(this.size >= 0, 'Table size sanity check failed');
79};
80
81Table.prototype.updateSize = function updateSize(size) {
82 assert(size <= this.protocolMaxSize, 'Table size bigger than maximum');
83 this.maxSize = size;
84 this.evict();
85};
Note: See TracBrowser for help on using the repository browser.