UNPKG

min-stream-pkt-line

Version:

A pkt-line codec packaged as a min-stream push-filter.

110 lines (94 loc) 3.49 kB
"use strict"; var pktLine = require('./.'); var bops = require('bops'); var helpers = require('min-stream-helpers'); var test = require('tape'); // Some canned data manually broken up at weird places to test deframing logic var chunked = [ '002bgit-upload-pack /js-git', '\0host=localhost\0', '00ad52bc15b3e685afe74f8331bade5ed992b8ae', 'edee HEAD\0multi_ack thin-pack side-b', 'and side-band-64k ofs-delta shallow no-pro', 'gress include-tag multi_ack_detailed agent=', 'git/1.8.1.2\n003f52bc15b3e685afe74f8331bade5', 'ed992b8aeedee refs/heads/master\n004652bc15b3', 'e685afe74f8331bade5ed992b8aeedee refs/remotes/o', 'rigin/HEAD\n004852bc15b3e685afe74f8331bade5ed992', 'b8aeedee refs/remotes/origin/master\n000000040007', 'hi\n0032want 0000000000000000000000000000000000000', '000\n0000001bhi\0ofs-delta hat party\nPACKhelloworld', 'this is a long binary blob right? Right!' ]; var messages = [ "git-upload-pack /js-git\0host=localhost\0", "52bc15b3e685afe74f8331bade5ed992b8aeedee HEAD\0multi_ack thin-pack side-band side-band-64k ofs-delta shallow no-progress include-tag multi_ack_detailed agent=git/1.8.1.2\n", "52bc15b3e685afe74f8331bade5ed992b8aeedee refs/heads/master\n", "52bc15b3e685afe74f8331bade5ed992b8aeedee refs/remotes/origin/HEAD\n", "52bc15b3e685afe74f8331bade5ed992b8aeedee refs/remotes/origin/master\n", null, // pkt-flush events are encoded as null "", // So they can be distinguished from empty strings "hi\n", "want 0000000000000000000000000000000000000000\n", null, "hi\0ofs-delta hat party\n", true, // transition to PACK mode is denoted by a `true` event. "PACKhelloworld", "this is a long binary blob right? Right!" ]; var framed = [ "002bgit-upload-pack /js-git\0host=localhost\0", "00ad52bc15b3e685afe74f8331bade5ed992b8aeedee HEAD\0multi_ack thin-pack side-band side-band-64k ofs-delta shallow no-progress include-tag multi_ack_detailed agent=git/1.8.1.2\n", "003f52bc15b3e685afe74f8331bade5ed992b8aeedee refs/heads/master\n", "004652bc15b3e685afe74f8331bade5ed992b8aeedee refs/remotes/origin/HEAD\n", "004852bc15b3e685afe74f8331bade5ed992b8aeedee refs/remotes/origin/master\n", "0000", "0004", "0007hi\n", "0032want 0000000000000000000000000000000000000000\n", "0000", "001bhi\0ofs-delta hat party\n", "PACKhelloworld", "this is a long binary blob right? Right!" ]; function toBinary(string) { if (string === null || string === true) return string; if (typeof string !== "string") return string; return bops.from(string); } function toString(binary) { if (binary === null || binary === true) return binary; return bops.to(binary); } test('decoder works as expected', function(assert) { helpers.run([ chunked.map(toBinary), pktLine.deframer, helpers.consume(onDeframed) ]); function onDeframed(err, items) { if (err) throw err; items = items.map(toString); items.forEach(function (item, i) { assert.equal(item, messages[i]); }); assert.equal(items.length, messages.length); assert.end(); } }); test('encoder works as expected', function(assert) { helpers.run([ messages.map(toBinary), pktLine.framer, helpers.consume(onFramed) ]); function onFramed(err, items) { if (err) throw err; items = items.map(toString); items.forEach(function (item, i) { assert.equal(item, framed[i]); }); assert.equal(items.length, framed.length); assert.end(); } });