Skip to content

Instantly share code, notes, and snippets.

@ww9rivers
Last active December 10, 2019 17:48
Show Gist options
  • Select an option

  • Save ww9rivers/5cd5343951f2ac0e966bf6c19c5f96ee to your computer and use it in GitHub Desktop.

Select an option

Save ww9rivers/5cd5343951f2ac0e966bf6c19c5f96ee to your computer and use it in GitHub Desktop.
Test for reading a text file out of a tar.gz archive (https://github.com/nodejs/node/issues/30885).
const fs = require('fs');
const readline = require('readline');
const util = require('util');
const tar = require('tar');
const zlib = require('zlib');
const test = require("tap").test;
function get_nsconf (archive, nsconf) {
return new Promise((resolve, reject) => {
if (!nsconf) { nsconf = '/nsconfig/ns.conf' }
console.debug('----> Parsing NetScaler backup: '+archive);
fs.createReadStream(archive).on('error', console.error)
.pipe(zlib.createUnzip())
.pipe(new tar.Parse())
.on('entry', entry => {
if (entry.path.endsWith(nsconf)) {
if ('File' !== entry.type) {
throw new Error('Wrong type for entry: '+nsconf+' : '+entry.type);
}
resolve({tar: archive, entry: entry});
} else {
entry.resume();
}
})
});
}
test('NetScaler config read test', t => {
const archive = './tarball.tar.gz';
const nsconf = 'tap-tar.js';
get_nsconf(archive, nsconf).then(nsc => {
let count = 0;
// processing nsc as resolved by get_nsconf():
console.debug('----> Found: '+nsc.entry.path);
t.ok(nsc.tar == archive);
t.ok(nsc.entry != undefined);
readline.createInterface({
input: nsc.entry
}).on('line', line => {
count++
console.log(line);
t.ok(null !== line && undefined !== line);
}).on('close', () => {
console.log('----> File closed. Line count = '+count);
t.end();
});
console.debug('----> Closing file. Line count = '+count);
}).catch(err => {
console.log('Error: ', err, '\n', util.inspect(err.stack));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment