source: trip-planner-front/node_modules/hpack.js/lib/hpack/decompressor.js@ e29cc2e

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

initial commit

  • Property mode set to 100644
File size: 2.6 KB
Line 
1var hpack = require('../hpack');
2var utils = hpack.utils;
3var decoder = hpack.decoder;
4var table = hpack.table;
5var assert = utils.assert;
6
7var inherits = require('inherits');
8var Duplex = require('readable-stream').Duplex;
9
10function Decompressor(options) {
11 Duplex.call(this, {
12 readableObjectMode: true
13 });
14
15 this._decoder = decoder.create();
16 this._table = table.create(options.table);
17}
18inherits(Decompressor, Duplex);
19module.exports = Decompressor;
20
21Decompressor.create = function create(options) {
22 return new Decompressor(options);
23};
24
25Decompressor.prototype._read = function _read() {
26 // We only push!
27};
28
29Decompressor.prototype._write = function _write(data, enc, cb) {
30 this._decoder.push(data);
31
32 cb(null);
33};
34
35Decompressor.prototype.execute = function execute(cb) {
36 while (!this._decoder.isEmpty()) {
37 try {
38 this._execute();
39 } catch (err) {
40 if (cb)
41 return done(err);
42 else
43 return this.emit('error', err);
44 }
45 }
46
47 if (cb)
48 done(null);
49
50 function done(err) {
51 process.nextTick(function() {
52 cb(err);
53 });
54 }
55};
56
57Decompressor.prototype.updateTableSize = function updateTableSize(size) {
58 this._table.updateSize(size);
59};
60
61Decompressor.prototype._execute = function _execute() {
62 var isIndexed = this._decoder.decodeBit();
63 if (isIndexed)
64 return this._processIndexed();
65
66 var isIncremental = this._decoder.decodeBit();
67 var neverIndex = 0;
68 if (!isIncremental) {
69 var isUpdate = this._decoder.decodeBit();
70 if (isUpdate)
71 return this._processUpdate();
72
73 neverIndex = this._decoder.decodeBit();
74 }
75
76 this._processLiteral(isIncremental, neverIndex);
77};
78
79Decompressor.prototype._processIndexed = function _processIndexed() {
80 var index = this._decoder.decodeInt();
81
82 var lookup = this._table.lookup(index);
83 this.push({ name: lookup.name, value: lookup.value, neverIndex: false });
84};
85
86Decompressor.prototype._processLiteral = function _processLiteral(inc, never) {
87 var index = this._decoder.decodeInt();
88
89 var name;
90 var nameSize;
91
92 // Literal header-name too
93 if (index === 0) {
94 name = this._decoder.decodeStr();
95 nameSize = name.length;
96 name = utils.stringify(name);
97 } else {
98 var lookup = this._table.lookup(index);
99 nameSize = lookup.nameSize;
100 name = lookup.name;
101 }
102
103 var value = this._decoder.decodeStr();
104 var valueSize = value.length;
105 value = utils.stringify(value);
106
107 if (inc)
108 this._table.add(name, value, nameSize, valueSize);
109
110 this.push({ name: name, value: value, neverIndex: never !== 0});
111};
112
113Decompressor.prototype._processUpdate = function _processUpdate() {
114 var size = this._decoder.decodeInt();
115 this.updateTableSize(size);
116};
Note: See TracBrowser for help on using the repository browser.