Skip to content

Instantly share code, notes, and snippets.

@molnarg
Last active January 2, 2016 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save molnarg/8375095 to your computer and use it in GitHub Desktop.
Save molnarg/8375095 to your computer and use it in GitHub Desktop.
The script used to generate the test output of node-http2-protocol for [Jxck's compression test suite](https://github.com/Jxck/hpack-test-case)
var fs = require('fs')
var compressor = require('./node_modules/node-http2-protocol/lib/compressor')
var log = require('./node_modules/node-http2-protocol/test/util').createLogger('test')
var HeaderTable = compressor.HeaderTable;
var HuffmanTable = compressor.HuffmanTable;
var HeaderSetCompressor = compressor.HeaderSetCompressor;
var HeaderSetDecompressor = compressor.HeaderSetDecompressor;
for (var i = 0; i < 31; i++) {
var headertable = new HeaderTable(log, 4096);
var n = String(i)
if (n.length == 1) n = '0' + n;
var story = JSON.parse(fs.readFileSync('raw-data/story_' + n +'.json').toString());
story.draft = 5;
story.description = 'Encoded by node-http2-protocol. Encoder wrapper script: https://gist.github.com/molnarg/8375095';
story.cases.forEach(function(c) {
c.header_table_size = 4096;
var headers = c.headers;
var compressor = new HeaderSetCompressor(log, headertable, HuffmanTable[story.context + 'HuffmanTable']);
headers.forEach(function(pair) {
var name = Object.keys(pair)[0]
var value = pair[name];
compressor.write([name, value])
})
compressor.end()
var chunk, chunks = [];
while (chunk = compressor.read()) {
chunks.push(chunk);
}
c.wire = concat(chunks).toString('hex')
});
fs.writeFileSync('node-http2-protocol/story_' + n +'.json', JSON.stringify(story, undefined, 2) + '\n')
}
// Concatenate an array of buffers into a new buffer
function concat(buffers) {
var size = 0;
for (var i = 0; i < buffers.length; i++) {
size += buffers[i].length;
}
var concatenated = new Buffer(size);
for (var cursor = 0, j = 0; j < buffers.length; cursor += buffers[j].length, j++) {
buffers[j].copy(concatenated, cursor);
}
return concatenated;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment