All files index.js

100% Statements 1585/1585
100% Branches 259/259
100% Functions 42/42
100% Lines 1585/1585

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 15861x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 396x 396x 396x 396x 1x 1x 2172x 2172x 2172x 1x 1x 1204x 1204x 1204x 1x 1x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 12722x 1900x 1900x 1900x 1900x 1900x 12722x 12722x 12722x 3988x 3988x 3988x 3988x 2112x 2112x 2112x 3988x 12722x 12722x 12722x 12722x 12722x 12722x 7x 7x 7x 7x 7x 12722x 1x 1x 1x 1x 7809x 7809x 7809x 7809x 7809x 7809x 7809x 2876x 2876x 1x 1x 1x 28x 28x 1x 1x 1x 18522x 18522x 1x 1x 1x 1x 1x 978192x 978192x 978192x 978192x 978192x 977424x 977424x 978192x 978192x 1x 1x 1x 442469x 442469x 442469x 417x 417x 442052x 442052x 442052x 442052x 1x 1x 1x 12x 12x 12x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2292x 2292x 2292x 2292x 2292x 2292x 2292x 2292x 2292x 2292x 2292x 2292x 2292x 2292x 2292x 2292x 2292x 2292x 2292x 2292x 2292x 396x 396x 396x 396x 396x 396x 396x 396x 396x 442x 442x 442x 2x 2x 2x 2x 2x 2x 2x 440x 440x 442x 4x 4x 4x 4x 4x 4x 436x 436x 436x 436x 436x 400x 400x 400x 400x 400x 400x 400x 400x 400x 396x 396x 396x 396x 396x 396x 396x 396x 396x 400x 400x 400x 400x 400x 400x 436x 436x 442x 400x 400x 400x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 396x 396x 396x 396x 396x 792x 792x 792x 792x 792x 792x 792x 396x 396x 396x 396x 792x 792x 792x 24x 24x 24x 24x 24x 24x 24x 8x 8x 24x 24x 792x 792x 396x 396x 396x 1896x 1896x 1896x 1896x 1896x 1896x 1896x 1896x 1896x 1896x 1896x 1896x 1896x 1896x 1896x 1896x 1896x 2292x 930x 892250x 892250x 892250x 892250x 1784500x 1784500x 930x 930x 1896x 1896x 1896x 1896x 1896x 1896x 1896x 1896x 1896x 1896x 1896x 1896x 3729x 3729x 3729x 1861x 1861x 3729x 1896x 1896x 3329x 3329x 1864x 1864x 1864x 1864x 1864x 3329x 1494x 1494x 1162x 1162x 1162x 1162x 1494x 1864x 1864x 1864x 3329x 1896x 1896x 3608x 3608x 1865x 1865x 1865x 3608x 2121x 2121x 959x 959x 959x 959x 959x 2121x 1865x 1865x 1865x 3608x 1896x 1896x 1896x 1896x 1896x 1896x 1896x 1896x 488x 488x 488x 488x 488x 488x 488x 302x 302x 302x 302x 238x 238x 302x 488x 488x 488x 1896x 1896x 1896x 1896x 1896x 1896x 372x 372x 372x 372x 372x 212x 212x 1896x 1896x 1896x 1896x 1896x 1797544x 1797544x 1797544x 1797544x 1797544x 622969x 622969x 620519x 620519x 622969x 622969x 622969x 622959x 622935x 622959x 622959x 24x 24x 622935x 622935x 622935x 622935x 622959x 622969x 1174575x 1174575x 1174575x 1174575x 1174575x 1174307x 1174307x 1174307x 1174575x 1174575x 1174575x 1174133x 1174133x 1174133x 1174127x 1174127x 1174127x 6x 6x 6x 6x 1174133x 1174133x 1174133x 1174133x 1174575x 1797544x 1797544x 1797544x 1896x 1896x 1896x 2292x 1836x 1836x 1792x 1792x 1792x 1792x 1836x 1836x 1836x 362x 362x 1836x 1836x 1836x 791x 791x 1836x 1836x 2292x 1x 1x 1x 1x 1723210x 1723210x 1723210x 408x 408x 408x 1722802x 1722802x 1x 1x 1x 1174127x 1174127x 1174127x 786x 786x 1173341x 1174127x 1173317x 1173317x 1173317x 1173317x 1173317x 1173317x 1173317x 537240x 537240x 537240x 537240x 537240x 537240x 537240x 537240x 537240x 537240x 537240x 4x 4x 537240x 537240x 537240x 537240x 537240x 1173317x 1174127x 1174127x 6336x 6336x 6x 6x 6330x 6330x 6330x 6330x 1173311x 1174127x 1174127x 1158339x 1158339x 1158339x 1810x 1810x 1810x 1810x 1158339x 1158339x 6x 6x 6x 6x 6x 1158339x 1158339x 1158339x 535980x 535788x 535788x 1158339x 1158339x 620519x 620519x 620519x 1158339x 1158339x 24x 24x 1158339x 1158147x 1158147x 1158147x 14972x 14972x 14972x 1174127x 99x 99x 1174127x 1174127x 6x 6x 1174127x 1174127x 1548x 1452x 1452x 1174127x 1174127x 12341x 12341x 12245x 12341x 11005x 11005x 11005x 11005x 11005x 12245x 12245x 12245x 12341x 3496x 3496x 3496x 3496x 3496x 12x 12x 12x 12x 12x 12x 12x 3496x 12245x 12245x 1684x 1684x 1684x 1683x 1683x 1684x 12245x 12341x 12341x 12341x 12341x 12341x 1588x 1588x 10657x 10657x 10657x 10657x 12245x 12341x 6x 6x 6x 6x 6x 12239x 12239x 99x 99x 99x 99x 12245x 12245x 12245x 1174127x 1174127x 954x 954x 1174127x 1174127x 24x 24x 1174127x 1x 1x 1x 2884x 2884x 2884x 2884x 2884x 2876x 2876x 1x 1x 1x 791x 791x 791x 791x 791x 1x 1x 1x 4018x 4018x 2558x 2558x 2558x 4018x 4018x 4018x 4018x 1x 1x 1x 20110x 20110x 20110x 13765x 13765x 13765x 13765x 20110x 12297x 12297x 13765x 13765x 13765x 20110x 20110x 20110x 20110x 20110x 20110x 12297x 12297x 12297x 13765x 13765x 20110x 20110x 20110x 20110x 20110x 20110x 1x 1x 1x 977424x 977424x 977424x 977424x 977424x 977424x 977424x 977424x 977424x 977424x 977424x 977424x 977424x 977424x 5817x 5817x 971607x 971607x 971607x 971607x 977424x 977424x 833244x 977424x 977424x 434526x 434526x 537081x 537081x 537081x 537081x 977424x 10x 10x 537071x 537071x 986x 986x 536085x 536085x 536085x 536085x 537081x 537081x 537081x 537081x 537081x 537081x 537081x 537081x 537081x 537081x 537081x 537081x 537081x 1x 1x 1x 1012021x 1012021x 1012021x 1012021x 1012021x 6x 6x 1012015x 1012015x 70474743x 70474743x 70474743x 70474743x 70474743x 69854206x 69854206x 620537x 620537x 620537x 620537x 620537x 620537x 70474743x 1012015x 1012015x 574x 574x 574x 1012015x 1012015x 620537x 620537x 620537x 620537x 620537x 620537x 620537x 620537x 620537x 620537x 620537x 620537x 620537x 620537x 620537x 620537x 620537x 620537x 620537x 620531x 620531x 620537x 620537x 620537x 620537x 620537x 442040x 442040x 442040x 442040x 442040x 442040x 620537x 620537x 620537x 620537x 1012015x 1012015x 1012021x 1631978x 1631978x 1631978x 1631978x 1631978x 3146x 3146x 1628832x 1628832x 1628832x 1628832x 1628832x 1628832x 1631978x 1008869x 1008869x 619963x 619963x 619963x 619963x 1631978x 308867x 308867x 619963x 619963x 619963x 619963x 1631978x 308867x 308867x 1631978x 1x 1x 1x 1012015x 1012015x 1012015x 1012015x 1012015x 6x 6x 1012009x 1012009x 1012009x 1012009x 1012015x 1012015x 1012015x 1012015x 1012009x 1012009x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6878x 6878x 6878x 2x 2x 2x 6876x 6876x 6878x 456x 456x 8x 8x 456x 6420x 6420x 6420x 6420x 24x 24x 6396x 6396x 6420x 1x 1x 6395x 6395x 6420x 1x 1x 6420x 6842x 6842x 6842x 6878x 6878x 6878x 6836x 6836x 6836x 444x 444x 444x 444x 444x 444x 444x 444x 444x 444x 444x 444x 444x 444x 444x 444x 444x 444x 444x 444x 444x 444x 444x 416x 416x 416x 416x 6x 6x 6x 6x 6x 6x 6x 410x 444x 444x 444x 6392x 6392x 6392x 6392x 6836x 6068x 6068x 6068x 6068x 6068x 6392x 6392x 6836x 6878x 6878x 6878x 36x 36x 6806x 6806x 6806x 6806x 6878x 6819x 6819x 6819x 6819x 6804x 6804x 6804x 15x 15x 15x 2x 2x 2x 2x 1x 1x 1x 880x 880x 880x 880x 880x 880x 880x 880x 880x 880x 880x 2x 2x 880x 880x 880x 880x 880x 880x 4x 4x 4x 880x 880x 216x 216x 216x 216x 216x 880x 880x 214x 214x 214x 4x 4x 880x 1x 1x 1x 876x 876x 876x 876x 1x 1x 1x 846x 846x 846x 846x 846x 846x 846x 846x 834x 834x 846x 846x 4x 4x 4x 4x 4x 4x 4x 846x 846x 1x 1x 1x 1x  
/**
# bpmux   [![Build Status](https://github.com/davedoesdev/bpmux/workflows/ci/badge.svg)](https://github.com/davedoesdev/bpmux/actions) [![Coverage Status](https://coveralls.io/repos/davedoesdev/bpmux/badge.png?branch=master&service=github)](https://coveralls.io/r/davedoesdev/bpmux?branch=master) [![NPM version](https://badge.fury.io/js/bpmux.png)](http://badge.fury.io/js/bpmux)
 
Node stream multiplexing with back-pressure on each stream.
 
- Run more than one [`stream.Duplex`](https://nodejs.org/api/stream.html#stream_class_stream_duplex) over a carrier `Duplex`.
- Exerts back-pressure on each multiplexed stream and the underlying carrier stream.
- Each multiplexed stream's back-pressure is handled separately while respecting the carrier's capacity. [This prevents a slow or paused stream affecting other streams](#comparison). This does incur an overhead so if you don't care about this feature you might want to look elsewhere.
- Unit tests with 100% coverage.
- Tested with TCP streams. You'll get better performance if you [disable Nagle](https://nodejs.org/dist/latest-v10.x/docs/api/net.html#net_socket_setnodelay_nodelay).
- Works in the browser!
  - Tested with [Primus](https://github.com/primus/primus) (using [primus-backpressure](https://github.com/davedoesdev/primus-backpressure)).
  - Tested with HTTP/2 streams (using [browser-http2-duplex](https://github.com/davedoesdev/browser-http2-duplex)). Also tested Node-to-Node using `http2`.
  - Browser unit tests using [webpack](http://webpack.github.io/) and [nwjs](http://nwjs.io/).
- **See the [errors](#errors) section for information on why multiplexed streams error when their carrier stream closes before they do.**
 
The API is described [here](#api).
 
## Example
 
Multiplexing multiple streams over a single TCP stream:
 
```javascript
var net = require('net'),
    crypto = require('crypto'),
    assert = require('assert'),
    BPMux = require('bpmux').BPMux,
    sent = [];
 
net.createServer(function (c)
{
    var received = [], ended = 0;
 
    new BPMux(c).on('handshake', function (duplex)
    {
        var accum = '';
 
        duplex.on('readable', function ()
        {
            var data = this.read();
            if (data)
            {
                accum += data.toString('hex');
            }
        });
 
        duplex.on('end', function ()
        {
            received.push(accum);
 
            ended += 1;
            assert(ended <= 10);
            if (ended === 10)
            {
                assert.deepEqual(received.sort(), sent.sort());
            }
        });
    });
}).listen(7000, function ()
{
    var mux = new BPMux(net.createConnection(7000)), i;
 
    function multiplex(n)
    {
        var data = crypto.randomBytes(n * 100);
        mux.multiplex().end(data);
        sent.push(data.toString('hex'));
    }
 
    for (i = 1; i <= 10; i += 1)
    {
        multiplex(i);
    }
});
```
 
## Another Example
 
Multiple return pipes to the browser, multiplexed over a single Primus connection:
 
```javascript
var PrimusDuplex = require('primus-backpressure').PrimusDuplex,
    BPMux = require('bpmux').BPMux,
    http = require('http'),
    path = require('path'),
    crypto = require('crypto'),
    stream = require('stream'),
    assert = require('assert'),
    finalhandler = require('finalhandler'),
    serve_static = require('serve-static'),
    Primus = require('primus'),
    serve = serve_static(__dirname);
 
http.createServer(function (req, res)
{
    serve(req, res, finalhandler(req, res));
}).listen(7500, function ()
{
    var primus = new Primus(this);
 
    primus.on('connection', function (spark)
    {
        var mux = new BPMux(new PrimusDuplex(spark)), ended = 0, i;
 
        function multiplex(n)
        {
            var buf = crypto.randomBytes(10 * 1024),
                buf_stream = new stream.PassThrough(),
                bufs = [],
                duplex = mux.multiplex({ handshake_data: Buffer.from([n]) });
 
            buf_stream.end(buf);
            buf_stream.pipe(duplex);
 
            duplex.on('readable', function ()
            {
                var data;
 
                while (true)
                {
                    data = this.read();
                    if (data === null)
                    {
                        break;
                    }
                    bufs.push(data);
                }
            });
 
            duplex.on('end', function ()
            {
                console.log('end', n);
                ended += 1;
                assert(ended <= 10);
                assert.deepEqual(Buffer.concat(bufs), buf);
            });
        }
 
        for (i = 0; i < 10; i += 1)
        {
            multiplex(i);
        }
    });
    
    console.log('Point your browser to http://localhost:7500/loader.html');
});
```
 
The HTML (`loader.html`) for the browser-side of this example:
 
```html
<html>
  <head>
    <title>BPMux Test Runner</title>
    <script type="text/javascript" src="/primus/primus.js"></script>
    <script type="text/javascript" src="bundle.js"></script>
    <script type="text/javascript" src="loader.js"></script>
  </head>
  <body onload='doit()'>
  </body>
</html>
```
 
The browser-side code (`loader.js`):
 
```javascript
function doit()
{
    var mux = new BPMux(new PrimusDuplex(new Primus({ strategy: false })));
 
    mux.on('handshake', function (duplex, handshake_data)
    {
        console.log("handshake", handshake_data[0]);
        duplex.pipe(duplex);
 
        duplex.on('end', function ()
        {
            console.log('end', handshake_data[0]);
        });
    });
}
```
 
The browser-side dependencies (`bundle.js`) can be produced by webpack from:
 
```javascript
PrimusDuplex = require('primus-backpressure').PrimusDuplex;
BPMux = require('bpmux').BPMux;
```
 
## Comparison
 
### [multiplex](https://github.com/maxogden/multiplex) library
 
Multiplexing libraries which don't exert backpressure on individual streams
suffer from starvation. A stream which doesn't read its data stops other streams
on the multiplex getting their data.
 
Here's a test using the [multiplex](https://github.com/maxogden/multiplex)
library:
 
```javascript
// Uses https://github.com/maxogden/multiplex (npm install multiplex)
// Backpressure is exerted across the multiplex as a whole, not individual streams.
// This means a stream which doesn't read its data starves the other streams.
 
const fs = require('fs');
const net = require('net');
const multiplex = require('multiplex');
 
require('net').createServer(c => {
    c.pipe(multiplex((stream, id) => {
        stream.on('data', function(d) {
            console.log('data', id, d.length);
            if (id === '0') {
                this.pause();
            }
        });
    }));
}).listen(7000, () => {
    const plex = multiplex();
    plex.pipe(net.createConnection(7000));
 
    const stream1 = plex.createStream();
    const stream2 = plex.createStream();
 
    fs.createReadStream('/dev/urandom').pipe(stream1);
    fs.createReadStream('/dev/urandom').pipe(stream2);
});
```
 
When the first stream is paused, backpressure is applied to the second stream
too, even though it hasn't been paused. If you run this example, you'll see:
 
```bash
$ node multiplex.js 
data 0 65536
data 1 65536
```
 
bpmux doesn't suffer from this problem since backpressure is exerted on each
stream separately. Here's the same test:
 
```javascript
// BPMux exerts backpressure on individual streams so a stream which doesn't
// read its data doesn't starve the other streams.
 
const fs = require('fs');
const net = require('net');
const { BPMux } = require('bpmux');
 
require('net').createServer(c => {
    new BPMux(c).on('handshake', stream => {
        stream.on('data', function (d) {
            console.log('data', stream._chan, d.length);
            if (stream._chan === 0) {
                this.pause();
            }
        });
    });
}).listen(7000, () => {
    const mux = new BPMux(net.createConnection(7000));
 
    const stream1 = mux.multiplex();
    const stream2 = mux.multiplex();
 
    fs.createReadStream('/dev/urandom').pipe(stream1);
    fs.createReadStream('/dev/urandom').pipe(stream2);
});
```
 
The second stream continues to receive data when the first stream is paused:
 
```bash
data 0 16384
data 1 16384
data 1 16384
data 1 16384
data 1 16384
data 1 16384
data 1 16384
data 1 16384
data 1 16384
data 1 16384
data 1 16384
data 1 16384
data 1 16384
data 1 16384
...
```
 
### HTTP/2 sessions
 
[HTTP/2 sessions](https://nodejs.org/dist/latest-v16.x/docs/api/http2.html#class-http2session)
do exert backpressure on individual streams, as this test shows:
 
```javascript
const fs = require('fs');
const http2 = require('http2');
 
const server = http2.createServer();
server.on('stream', (stream, headers) => {
    stream.on('data', function (d) {
        console.log('data', headers[':path'], d.length);
        if (headers[':path'] === '/stream1') {
            this.pause();
        }
    });
});
server.listen(8000);
 
const client = http2.connect('http://localhost:8000');
 
const stream1 = client.request({ ':path': '/stream1' }, { endStream: false });
const stream2 = client.request({ ':path': '/stream2' }, { endStream: false });
 
fs.createReadStream('/dev/urandom').pipe(stream1);
fs.createReadStream('/dev/urandom').pipe(stream2);
```
 
```
data /stream1 16384
data /stream2 16384
data /stream2 16348
data /stream2 35
data /stream2 16384
data /stream2 16384
data /stream2 1
data /stream2 16384
data /stream2 16366
data /stream2 18
data /stream2 16384
data /stream2 16382
data /stream2 2
data /stream2 16384
...
```
 
If you pass a pair of sessions (one client, one server) to [`BPMux()`](#bpmuxcarrier-options),
they will be used for multiplexing streams, with no additional overhead. This is useful if
you want to use the bpmux API.
 
## Errors
 
bpmux will emit `error` events on multiplexed streams if their underlying
(carrier) stream closes before they have closed. The error object will have one
of the following messages:
 
```
carrier stream finished before duplex finished
carrier stream ended before end message received
```
 
and have a property `carrier_done` set to `true`.
 
As this is an `error` event, you must register an event listener on multiplexed
streams [if you don't want the Node process to exit](https://nodejs.org/dist/latest-v13.x/docs/api/events.html#events_error_events).
 
The reasoning behind emitting `error` events on open multiplexed streams when
their carrier closes is:
 
- If you're reading from a stream and it hasn't ended before the carrier closes then there may be some data that you'll never receive. This is an error state.
 
- If you're writing to a stream and it hasn't finished before the carrier closes then your application should be informed about it straight away. If it's performing some heavy calculation, for example, then it has a chance to cancel it before writing the result to the stream.
 
If you do register `error` event listeners, make sure you do so for streams
you multiplex using [`multiplex()`](#bpmuxprototypemultiplexoptions) _and_
for streams you receive using the [`handshake`](#bpmuxeventshandshakeduplex-handshake_data-delay_handshake) or [`peer_multiplex`](#bpmuxeventspeer_multiplexduplex) events.
 
`BPMux` objects will also re-emit any `error` events their carrier stream emits.
 
## Installation
 
```shell
npm install bpmux
```
 
## Licence
 
[MIT](LICENCE)
 
## Test
 
Over TCP (long test):
 
```shell
grunt test
```
 
Over TCP (quick test):
 
```shell
grunt test-fast
```
 
Over Primus (using nwjs to run browser- and server-side):
 
```shell
grunt test-browser
```
 
The examples at the top of this page:
 
```shell
grunt test-examples
```
 
## Code Coverage
 
```shell
grunt coverage
```
 
[c8](https://github.com/bcoe/c8) results are available [here](http://rawgit.davedoesdev.com/davedoesdev/bpmux/master/coverage/lcov-report/index.html).
 
Coveralls page is [here](https://coveralls.io/r/davedoesdev/bpmux).
 
## Lint
 
```shell
grunt lint
```
 
# API
*/
/*eslint-env node */
'use strict';
 
var util = require('util'),
    stream = require('stream'),
    Duplex = stream.Duplex,
    Writable = stream.Writable,
    EventEmitter = require('events').EventEmitter,
    frame = require('frame-stream'),
    max_seq = Math.pow(2, 32),
    TYPE_END = 0,
    TYPE_HANDSHAKE = 1,
    TYPE_STATUS = 2,
    TYPE_FINISHED_STATUS = 3,
    TYPE_DATA = 4,
    TYPE_PRE_HANDSHAKE = 5,
    TYPE_ERROR_END = 6,
    TYPE_KEEP_ALIVE = 7;
 
/**
Class for holding a pair of HTTP/2 sessions.
 
Pass this to [BPMux()](#bpmuxcarrier-options) and it will use the sessions'
existing support for multiplexing streams. Both [client](https://nodejs.org/dist/latest-v16.x/docs/api/http2.html#class-clienthttp2session) and [server](https://nodejs.org/dist/latest-v16.x/docs/api/http2.html#class-serverhttp2session) sessions
are required because HTTP/2 push streams are unidirectional.
 
@param {ClientHttp2Session} client Client session
@param {ServerHttp2Session} server Server session
*/
class Http2Sessions
{
    constructor(client, server)
    {
        this._client = client;
        this._server = server;
    }
 
    get client()
    {
        return this._client;
    }
 
    get server()
    {
        return this._server;
    }
}
 
function BPDuplex(options, mux, chan)
{
    Duplex.call(this, options);
 
    options = Object.assign(
    {
        max_write_size: 0
    }, options);
 
    this._mux = mux;
    this.mux = mux;
    this._chan = chan;
    this._max_write_size = options.max_write_size;
    this._check_read_overflow = options.check_read_overflow !== false;
    this._seq = 0;
    this._remote_free = 0;
    this._set_remote_free = false;
    this._data = null;
    this._cb = null;
    this._index = 0;
    this._finished = false;
    this._ended = false;
    this._removed = false;
    this._handshake_sent = false;
    this._handshake_received = false;
    this._end_pending = false;
    this._error_end = false;
    this._error_end_pending = false;
 
    function finish()
    {
        this._finished = true;
        this._mux._send_end(this);
        this._check_remove();
    }
    this.once('finish', finish);
 
    this.once('close', function ()
    {
        this.removeListener('finish', finish);
        if (!this._finished)
        {
            this._finished = true;
            this._mux._send_end(this);
        }
        this._check_remove();
    });
 
    mux.duplexes.set(chan, this);
 
    if ((mux._max_open > 0) && (mux.duplexes.size === mux._max_open))
    {
        setImmediate(function ()
        {
            mux.emit('full');
        });
    }
}
 
util.inherits(BPDuplex, Duplex);
 
BPDuplex.prototype._check_remove = function ()
{
    // Don't call _remove if not ended because the duplex may have closed
    // due to a local destroy and so data may still come from the peer
    // (but be ignored because we don't push to destroyed streams).
    // Duplex will be removed when TYPE_END is received.
    if (this._finished && this._ended && !this._removed)
    {
        this._mux._remove(this);
    }
};
 
BPDuplex.prototype.get_channel = function ()
{
    return this._chan;
};
 
BPDuplex.prototype._send_handshake = function (handshake_data)
{
    this._mux._send_handshake(this, handshake_data || Buffer.alloc(0));
};
 
BPDuplex.prototype._read = function () { return undefined; };
 
BPDuplex.prototype.read = function (size, send_status)
{
    var r = Duplex.prototype.read.call(this, size);
 
    if (send_status !== false)
    {
        this._mux._send_status(this);
    }
 
    return r;
};
 
BPDuplex.prototype._write = function (data, encoding, cb)
{
    if (data.length === 0)
    {
        return cb();
    }
 
    this._data = data;
    this._cb = cb;
    this._mux._send();
};
 
BPDuplex.prototype.peer_error_then_end = function (chunk, encoding, cb)
{
    this._error_end = true;
    return this.end(chunk, encoding, cb);
};
 
/**
Constructor for a `BPMux` object which multiplexes more than one [`stream.Duplex`](https://nodejs.org/api/stream.html#stream_class_stream_duplex) over a carrier `Duplex`.
 
@constructor
@extends events.EventEmitter
 
@param {Duplex|Http2Sessions} carrier The `Duplex` stream over which other `Duplex` streams will be multiplexed.
 
@param {Object} [options] Configuration options. This is passed down to [`frame-stream`](https://github.com/davedoesdev/frame-stream). It also supports the following additional properties:
- `{Object} [peer_multiplex_options]` When your `BPMux` object detects a new multiplexed stream from the peer on the carrier, it creates a new `Duplex` and emits a [`peer_multiplex`](#bpmuxeventspeer_multiplexduplex) event. When it creates the `Duplex`, it uses `peer_multiplex_options` to configure it with the following options:
 
  - `{Integer} [max_write_size]` Maximum number of bytes to write to the `Duplex` at once, regardless of how many bytes the peer is free to receive. Defaults to 0 (no limit).
 
  - `{Boolean} [check_read_overflow]` Whether to check if more data than expected is being received. If `true` and the `Duplex`'s high-water mark for reading is exceeded then the `Duplex` emits an `error` event. This should not normally occur unless you add data yourself using [`readable.unshift`](http://nodejs.org/api/stream.html#stream_readable_unshift_chunk) &mdash; in which case you should set `check_read_overflow` to `false`. Defaults to `true`.
 
- `{Function} [parse_handshake_data(handshake_data)]` When a new stream is multiplexed, the `BPMux` objects at each end of the carrier exchange a handshake message. You can supply application-specific handshake data to add to the handshake message (see [`BPMux.prototype.multiplex`](#bpmuxprototypemultiplexoptions) and [`BPMux.events.handshake`](#bpmuxeventshandshakeduplex-handshake_data-delay_handshake)). By default, when handshake data from the peer is received, it's passed to your application as a raw [`Buffer`](https://nodejs.org/api/buffer.html#buffer_buffer). Use `parse_handshake_data` to specify a custom parser. It will receive the `Buffer` as an argument and should return a value which makes sense to your application.
 
- `{Boolean} [coalesce_writes]` Whether to batch together writes to the carrier. When the carrier indicates it's ready to receive data, its spare capacity is shared equally between the multiplexed streams. By default, the data from each stream is written separately to the carrier. Specify `true` to write all the data to the carrier in a single write. Depending on the carrier, this can be more performant.
 
- `{Boolean} [high_channels]` `BPMux` assigns unique channel numbers to multiplexed streams. By default, it assigns numbers in the range [0..2^31). If your application can synchronise the two `BPMux` instances on each end of the carrier stream so they never call [`multiplex`](https://github.com/davedoesdev/bpmux#bpmuxprototypemultiplexoptions) at the same time then you don't need to worry about channel number clashes. For example, one side of the carrier could always call [`multiplex`](https://github.com/davedoesdev/bpmux#bpmuxprototypemultiplexoptions) and the other listen for [`handshake`](https://github.com/davedoesdev/bpmux#bpmuxeventshandshakeduplex-handshake_data-delay_handshake) events. Or they could take it in turns. If you can't synchronise both sides of the carrier, you can get one side to use a different range by specifying `high_channels` as `true`. The `BPMux` with `high_channels` set to `true` will assign channel numbers in the range [2^31..2^32).
 
- `{Integer} [max_open]` Maximum number of multiplexed streams that can be open at a time. Defaults to 0 (no maximum).
 
- `{Integer} [max_header_size]` `BPMux` adds a control header to each message it sends, which the receiver reads into memory. The header is of variable length &mdash; for example, handshake messages contain handshake data which can be supplied by the application. `max_header_size` is the maximum number of header bytes to read into memory. If a larger header is received, `BPMux` emits an `error` event. Defaults to 0 (no limit).
 
- `{Integer|false}` `keep_alive` Send a single byte keep-alive message every N milliseconds. Defaults to 30000 (30 seconds). Pass `false` to disable.
*/
function BPMux(carrier, options)
{
    EventEmitter.call(this, options);
 
    options = Object.assign(
    {
        max_open: 0,
        max_header_size: 0,
        keep_alive: 30 * 1000
    }, options);
 
    this._max_duplexes = Math.pow(2, 31);
    this._max_open = options.max_open;
    this.duplexes = new Map();
    this._chan = 0;
    this._chan_offset = options.high_channels ? this._max_duplexes : 0;
    this._parse_handshake_data = options.parse_handshake_data;
    this.carrier = carrier;
 
    if (carrier instanceof Http2Sessions)
    {
        const http2 = require('http2');
        const http2_options = options.http2 || {};
        const response_headers = {
            ...http2_options.headers,
            [http2.constants.HTTP2_HEADER_STATUS]: 200,
            [http2.constants.HTTP2_HEADER_CONTENT_TYPE]: 'application/octet-stream',
        };
 
        carrier.server.on('stream', (duplex, headers) =>
        {
            if ((this._max_open > 0) && (this.duplexes.size === this._max_open))
            {
                this.emit('full');
                return duplex.respond({
                    [http2.constants.HTTP2_HEADER_STATUS]: 503
                }, {
                    endStream: true
                });
            }
            const channel = Buffer.from(headers['bpmux-channel'], 'base64').readUint32BE();
            if (this.duplexes.has(channel))
            {
                return duplex.respond({
                    [http2.constants.HTTP2_HEADER_STATUS]: 409
                }, {
                    endStream: true
                });
            }
            duplex.cork();
            this._add_http2_duplex(duplex, channel);
            this.emit('peer_multiplex', duplex);
            let handshake_delayed = false;
            this._parse_http2_handshake(duplex, headers, () =>
            {
                handshake_delayed = true;
                let delayed_handshake;
                const uncork = duplex.uncork;
                duplex.uncork = () =>
                {
                    duplex.uncork = uncork;
                    if (!duplex.destroyed) // Node 12 calls uncork on end even if destroyed
                    {
                        duplex.respond({
                            ...response_headers,
                            ...this._make_http2_handshake(delayed_handshake)
                        });
                        duplex._handshake_sent = true;
                        this.emit('handshake_sent', duplex, true);
                        duplex.emit('handshake_sent', true);
                        duplex.uncork();
                    }
                };
                return handshake =>
                {
                    delayed_handshake = handshake;
                    duplex.uncork();
                };
            });
            if (handshake_delayed)
            {
                this.emit('pre_handshake_sent', duplex, true);
                duplex.emit('pre_handshake_sent', true);
            }
            else
            {
                duplex.respond({
                    ...response_headers,
                    ...this._make_http2_handshake()
                });
                duplex._handshake_sent = true;
                this.emit('handshake_sent', duplex, true);
                duplex.emit('handshake_sent', true);
                duplex.uncork();
            }
        });
 
        let closed = 0;
        for (const session of [carrier.client, carrier.server])
        {
            session.on('close', () =>
            {
                // Note: http2 sessions only close once all their
                // streams have closed so we don't need to go
                // through the duplexes here and close them
                if (++closed === 2)
                {
                    this.emit('finish');
                    this.emit('end');
                    this.emit('close');
                }
            });
 
            session.on('error', err =>
            {
                for (const duplex of this.duplexes.values())
                {
                    if ((duplex.session === session) &&
                        !duplex.destroyed &&
                        (duplex.listenerCount('error') > 0))
                    {
                        duplex.emit('error', err);
                    }
                }
                this.emit('error', err);
            });
        }
 
        return;
    }
 
    this._max_header_size = options.max_header_size;
    this._finished = false;
    this._ended = false;
    this._header_buffers = [];
    this._header_buffer_len = 0;
    this._reading_duplex = null;
    this._peer_multiplex_options = options.peer_multiplex_options;
    this._coalesce_writes = options.coalesce_writes;
    this._sending = false;
    this._send_requested = false;
    this._keep_alive_id = null;
    this._keep_alive_paused = false;
 
    this._out_stream = frame.encode(options);
 
    if (this._coalesce_writes)
    {
        this._out_stream._pushFrameData = function (bufs)
        {
            var i;
            for (i = 0; i < bufs.length; i += 1)
            {
                this.push(bufs[i]);
            }
        };
    }
 
    this._out_stream.pipe(carrier);
 
    this._in_stream = frame.decode(Object.assign({}, options,
        {
            unbuffered: true
        }));
    carrier.pipe(this._in_stream);
 
    var ths = this;
 
    function check_close()
    {
        if (ths._finished && ths._ended)
        {
            ths.emit('close');
        }
    }
 
    function finish()
    {
        if (ths._finished) { return; }
        ths._finished = true;
 
        clearInterval(ths._keep_alive_id);
 
        for (var duplex of ths.duplexes.values())
        {
            if (!duplex._finished && !duplex.destroyed)
            {
                const err = new Error('carrier stream finished before duplex finished');
                err.carrier_done = true;
                duplex.destroy(err);
            }
        }
 
        ths.emit('finish');
        check_close();
    }
 
    function end()
    {
        if (ths._ended) { return; }
        ths._ended = true;
 
        for (var duplex of ths.duplexes.values())
        {
            if (!duplex._ended && !duplex.destroyed)
            {
                duplex._ended = true; // we won't get any more messages for it
                const err = new Error('carrier stream ended before end message received');
                err.carrier_done = true;
                duplex.destroy(err);
            }
        }
 
        ths.emit('end');
        check_close();
    }
 
    carrier.on('finish', finish);
    carrier.on('close', finish);
 
    this._in_stream.on('end', end);
    carrier.on('close', end);
 
    function error(err)
    {
        // check_remove() is always called when _finished or _ended is set on a duplex,
        // so it will have been removed from duplex. Apart from above in end(),
        // where the duplex is destroyed, which we check below anyway (check_remove()
        // will eventually be called there too via the duplex's 'close' handler).
        for (var duplex of ths.duplexes.values())
        {
            if (!duplex._removed && // in case destroyed by previous iteration's error handler
                !duplex.destroyed &&
                (duplex.listenerCount('error') > 0))
            {
                duplex.emit('error', err);
            }
        }
 
        ths.emit('error', err);
    }
 
    carrier.on('error', error);
    this._in_stream.on('error', error);
    this._out_stream.on('error', error);
 
    this._out_stream.on('drain', function ()
    {
        ths._send();
 
        if (this._writableState.length < this._writableState.highWaterMark)
        {
            ths.emit('drain');
        }
    });
 
    this._in_stream.pipe(new Writable(
    {
        write: function (data, encoding, cb)
        {
            var duplex = ths._reading_duplex;
 
            if (duplex)
            {
                if (data.frameEnd)
                {
                    ths._reading_duplex = null;
                }
 
                if (!duplex._readableState.ended && !duplex.destroyed)
                {
                    if (duplex._check_read_overflow &&
                        ((duplex._readableState.length + data.length) >
                         duplex._readableState.highWaterMark))
                    {
                        duplex.emit('error', new Error('too much data'));
                    }
                    else
                    {
                        duplex.push(data);
                    }
                }
            }
            else
            {
                if ((ths._max_header_size <= 0) || 
                    (ths._header_buffer_len < ths._max_header_size))
                {
                    ths._header_buffers.push(data);
                    ths._header_buffer_len += data.length;
                }
 
                if (data.frameEnd)
                {
                    if ((ths._max_header_size <= 0) ||
                        (ths._header_buffer_len < ths._max_header_size))
                    {
                        ths._process_header(Buffer.concat(ths._header_buffers,
                                                          ths._header_buffer_len));
                    }
                    else
                    {
                        ths.emit('error', new Error('header too big'));
                    }
 
                    ths._header_buffers = [];
                    ths._header_buffer_len = 0;
                }
            }
 
            cb();
        }
    }));
 
    if (options.keep_alive !== false)
    {
        const orig_end = this.carrier.end;
        this.carrier.end = function ()
        {
            // If an error stops all data being written, we may not get 'finish'
            clearInterval(ths._keep_alive_id);
            return orig_end.apply(this, arguments);
        };
 
        this._out_stream.on('drain', function ()
        {
            ths._keep_alive_paused = false;
        });
 
        this._keep_alive_id = setInterval(function ()
        {
            ths._send_keep_alive();
        }, options.keep_alive);
    }
}
 
util.inherits(BPMux, EventEmitter);
 
BPMux.prototype._check_buffer = function (buf, size)
{
    if (buf.length < size)
    {
        this.emit('error', new Error('short buffer length ' + buf.length + ' < ' + size));
        return false;
    }
 
    return true;
};
 
BPMux.prototype._process_header = function (buf)
{
    if ((buf.length > 0) && (buf[0] === TYPE_KEEP_ALIVE))
    {
        return this.emit('keep_alive');
    }
 
    if (!this._check_buffer(buf, 5)) { return; }
 
    var ths = this,
        type = buf.readUInt8(0, true),
        chan = buf.readUInt32BE(1, true),
        duplex = this.duplexes.get(chan);
 
    function handle_status()
    {
        let free = buf.readUInt32BE(5, true);
        const seq = buf.length === 13 ? buf.readUInt32BE(9, true) : 0;
 
        free = duplex._max_write_size > 0 ?
            Math.min(free, duplex._max_write_size) : free;
 
        duplex._remote_free = seq + free - duplex._seq;
 
        if (duplex._seq < seq)
        {
            duplex._remote_free -= max_seq;
        }
 
        duplex._set_remote_free = true;
 
        ths._send();
    }
 
    if ((type !== TYPE_FINISHED_STATUS) && !duplex)
    {
        if ((this._max_open > 0) && (this.duplexes.size === this._max_open))
        {
            return this.emit('full');
        }
 
        duplex = new BPDuplex(this._peer_multiplex_options, this, chan);
        this.emit('peer_multiplex', duplex);
    }
 
    if (duplex && duplex._handshake_received)
    {
        switch (type)
        {
            case TYPE_END:
                duplex._ended = true;
                duplex._check_remove();
                duplex.push(null);
                break;
 
            case TYPE_ERROR_END:
                duplex._ended = true;
                duplex._check_remove();
                duplex.emit('error', new Error('peer error'));
                duplex.push(null);
                break;
 
            case TYPE_STATUS:
            case TYPE_FINISHED_STATUS:
                if (!this._check_buffer(buf, 13)) { return; }
                handle_status();
                break;
 
            case TYPE_DATA:
                duplex._remote_seq = buf.slice(5);
                this._reading_duplex = duplex;
                break;
 
            default:
                this.emit('error', new Error('unknown type: ' + type));
                break;
        }
 
        return;
    }
 
    switch (type)
    {
        case TYPE_END:
            duplex._end_pending = true;
            break;
 
        case TYPE_ERROR_END:
            duplex._error_end_pending = true;
            break;
 
        case TYPE_PRE_HANDSHAKE:
            if (!this._check_buffer(buf, 9)) { return; }
            handle_status();
            break;
 
        case TYPE_HANDSHAKE:
        {
            if (!this._check_buffer(buf, 9)) { return; }
            if (duplex._seq === 0)
            {
                const free = buf.readUInt32BE(5, true);
                duplex._remote_free = duplex._max_write_size > 0 ?
                    Math.min(free, duplex._max_write_size) : free;
                duplex._set_remote_free = true;
            }
            duplex._handshake_received = true;
            let handshake_data = buf.slice(9);
            if (this._parse_handshake_data)
            {
                try
                {
                    handshake_data = this._parse_handshake_data(handshake_data);
                }
                catch (ex)
                {
                    if (duplex.listenerCount('error') > 0)
                    {
                        duplex.emit('error', ex);
                    }
                    this.emit('error', ex);
                }
            }
            let handshake_delayed = false;
            const delay_handshake = () =>
            {
                handshake_delayed = true;
                return function (handshake_data)
                {
                    duplex._send_handshake(handshake_data);
                };
            };
            const dhs = duplex._handshake_sent ? null : delay_handshake;
            this.emit('handshake', duplex, handshake_data, dhs);
            duplex.emit('handshake', handshake_data, dhs);
            if (handshake_delayed)
            {
                this._send_handshake(duplex);
            }
            else
            {
                duplex._send_handshake();
            }
            if (duplex._error_end_pending)
            {
                duplex._ended = true;
                duplex._check_remove();
                duplex.emit('error', new Error('peer error'));
                duplex.push(null);
            }
            else if (duplex._end_pending)
            {
                duplex._ended = true;
                duplex._check_remove();
                duplex.push(null);
            }
            this._send();
            break;
        }
 
        case TYPE_FINISHED_STATUS:
            // from old duplex
            break;
 
        default:
            this.emit('error', new Error('expected handshake, got: ' + type));
            break;
    }
};
 
BPMux.prototype._remove = function (duplex)
{
    duplex._removed = true;
 
    if (this.duplexes.delete(duplex._chan))
    {
        this.emit('removed', duplex);
    }
};
 
BPMux.prototype._send_keep_alive = function ()
{
    var buf = Buffer.alloc(1);
    buf.writeUInt8(TYPE_KEEP_ALIVE, 0, true);
 
    this._keep_alive_paused = this._keep_alive_paused || !this._out_stream.write(buf);
};
 
BPMux.prototype._send_end = function (duplex)
{
    if (this._finished) { return; }
 
    var buf = Buffer.alloc(1 + 4);
 
    buf.writeUInt8(duplex._error_end ? TYPE_ERROR_END : TYPE_END, 0, true);
    buf.writeUInt32BE(duplex._chan, 1, true);
 
    this._out_stream.write(buf);
};
 
BPMux.prototype._send_handshake = function (duplex, handshake_data)
{
    if (this._finished) { return; }
    if (duplex._handshake_sent) { return this._send(); }
    
    var buf, size = 1 + 4 + 4;
 
    if (handshake_data)
    {
        size += handshake_data.length;
    }
 
    buf = Buffer.alloc(size);
 
    buf.writeUInt8(handshake_data ? TYPE_HANDSHAKE : TYPE_PRE_HANDSHAKE, 0, true);
    buf.writeUInt32BE(duplex._chan, 1, true);
    buf.writeUInt32BE(Math.max(duplex._readableState.highWaterMark - duplex._readableState.length, 0), 5, true);
 
    if (handshake_data)
    {
        handshake_data.copy(buf, 9);
        duplex._handshake_sent = true;
    }
 
    var r = this._out_stream.write(buf),
        evname = handshake_data ? 'handshake_sent' : 'pre_handshake_sent';
 
    this.emit(evname, duplex, r);
    duplex.emit(evname, r);
 
    this._send();
};
 
BPMux.prototype._send_status = function (duplex)
{
    // Note: Status messages are sent regardless of remote_free
    // (if the remote peer isn't doing anything it could never be sent and it
    // could be waiting for a status update). 
    // This means every time the app calls read(), a status message wlll be sent
    // to the remote peer. If you want to control when status messages are
    // sent then use the second parameter of read(), send_status.
    // To force sending a status message without actually reading any data,
    // call read(0).
 
    if (this._finished ||
        (duplex._remote_seq === undefined) ||
        (this._reading_duplex === duplex))
    {
        return;
    }
 
    var free = Math.max(duplex._readableState.highWaterMark - duplex._readableState.length, 0),
        buf;
 
    if (duplex._prev_status &&
        (duplex._prev_status.free === free) &&
        // we don't care about contents here, just if it's changed
        (duplex._prev_status.seq === duplex._remote_seq))
    {
        return;
    }
 
    var type;
 
    if (!duplex._handshake_sent)
    {
        type = TYPE_PRE_HANDSHAKE;
    }
    else if (duplex._finished)
    {
        type = TYPE_FINISHED_STATUS;
    }
    else
    {
        type = TYPE_STATUS;
    }
 
    buf = Buffer.alloc(1 + 4 + 4 + duplex._remote_seq.length);
    buf.writeUInt8(type, 0, true);
    buf.writeUInt32BE(duplex._chan, 1, true);
    buf.writeUInt32BE(free, 5, true);
    duplex._remote_seq.copy(buf, 9);
 
    duplex._prev_status = {
        free: free,
        seq: duplex._remote_seq
    };
 
    this._out_stream.write(buf);
};
 
BPMux.prototype.__send = function ()
{
    var ths = this, space, output, n;
 
    if (this._finished)
    {
        return;
    }
 
    function push_output(duplex)
    {
        if ((duplex._data === null) ||
            (duplex._remote_free <= 0) ||
            !duplex._handshake_sent)
        {
            return;
        }
 
        output.push(
        {
            duplex: duplex,
            size: Math.min(duplex._remote_free, duplex._data.length - duplex._index)
        });
    }
 
    function sort_output(a, b)
    {
        return a.size < b.size;
    }
 
    function write_output(info)
    {
        var size = Math.min(info.size, Math.max(Math.floor(space / n), 1)),
            buf = Buffer.alloc(1 + 4 + 4),
            buf2 = info.duplex._data.slice(info.duplex._index, info.duplex._index + size),
            cb;
 
        info.duplex._seq = (info.duplex._seq + size) % max_seq;
        
        buf.writeUInt8(TYPE_DATA, 0, true);
        buf.writeUInt32BE(info.duplex._chan, 1, true);
        buf.writeUInt32BE(info.duplex._seq, 5, true);
 
        info.duplex._set_remote_free = false;
 
        ths._out_stream.write(buf);
        ths._out_stream.write(buf2);
 
        if (!info.duplex._set_remote_free)
        {
            info.duplex._remote_free -= size;
        }
 
        info.duplex._index += size;
 
        if (info.duplex._index === info.duplex._data.length)
        {
            info.duplex._data = null;
            info.duplex._index = 0;
            cb = info.duplex._cb;
            info.duplex._cb = null;
            setImmediate(cb);
        }
 
        space = Math.max(space - size, 0);
        n -= 1;
    }
 
    while (true) // eslint-disable-line no-constant-condition
    {
        space = this._out_stream._writableState.highWaterMark - this._out_stream._writableState.length;
        output = [];
 
        if (space <= 0)
        {
            break;
        }
        
        this.duplexes.forEach(push_output);
        
        n = output.length;
 
        if (n === 0)
        {
            break;
        }
 
        output.sort(sort_output);
 
        if (this._coalesce_writes)
        {
            this.carrier.cork();
        }
 
        output.forEach(write_output);
        
        if (this._coalesce_writes)
        {
            this.carrier.uncork();
        }
    }
};
 
BPMux.prototype._send = function ()
{
    this._send_requested = true;
 
    if (this._sending)
    {
        return;
    }
 
    this._sending = true;
 
    while (this._send_requested)
    {
        this._send_requested = false;
        this.__send();
    }
 
    this._sending = false;
};
 
/**
Multiplex a new `stream.Duplex` over the carrier.
 
@param {Object} [options] Configuration options:
- `{Buffer} [handshake_data]` Application-specific handshake data to send to the peer. When a new stream is multiplexed, the `BPMux` objects at each end of the carrier exchange a handshake message. You can optionally supply handshake data to add to the handshake message here. The peer application will receive this when its `BPMux` object emits a [`handshake`](#bpmuxeventshandshakeduplex-handshake_data-delay_handshake) event. Defaults to a zero-length `Buffer`.
  
- `{Integer} [max_write_size]` Maximum number of bytes to write to the `Duplex` at once, regardless of how many bytes the peer is free to receive. Defaults to 0 (no limit).
 
- `{Boolean} [check_read_overflow]` Whether to check if more data than expected is being received. If `true` and the `Duplex`'s high-water mark for reading is exceeded then the `Duplex` emits an `error` event. This should not normally occur unless you add data yourself using [`readable.unshift`](http://nodejs.org/api/stream.html#stream_readable_unshift_chunk) &mdash; in which case you should set `check_read_overflow` to `false`. Defaults to `true`.
 
- `{Integer} [channel]` Unique number for the new stream. `BPMux` identifies each multiplexed stream by giving it a unique number, which it allocates automatically. If you want to do the allocation yourself, specify a channel number here. It's very unlikely you'll need to do this but the option is there. `Duplex` objects managed by `BPMux` expose a `get_channel` method to retrieve their channel number. Defaults to automatic allocation.
  
@return {Duplex} The new `Duplex` which is multiplexed over the carrier. This supports back-pressure using the stream [`readable`](https://nodejs.org/dist/latest-v4.x/docs/api/stream.html#stream_event_readable) event and [`write`](https://nodejs.org/dist/latest-v4.x/docs/api/stream.html#stream_writable_write_chunk_encoding_callback) method.
 
@throws {Error} If there are no channel numbers left to allocate to the new stream, the maximum number of open multiplexed streams would be exceeded or the carrier has finished or ended.
*/
BPMux.prototype.multiplex = function (options)
{
    if ((this._max_open > 0) && (this.duplexes.size === this._max_open))
    {
        this.emit('full');
        throw new Error('full');
    }
 
    if (this.carrier instanceof Http2Sessions)
    {
        if (this.carrier.client.closed || this.carrier.client.destroyed)
        {
            throw new Error('closed');
        }
    }
    else
    {
        if (this.carrier.destroyed)
        {
            throw new Error('closed');
        }
 
        if (this.carrier._writableState.ending)
        {
            throw new Error('finished');
        }
 
        if (this.carrier._readableState.ended)
        {
            throw new Error('ended');
        }
    }
 
    var ths = this, chan, next;
 
    options = options || {};
 
    function done(channel)
    {
        if (ths.carrier instanceof Http2Sessions)
        {
            const http2 = require('http2');
            const chan = Buffer.alloc(4);
            chan.writeUInt32BE(channel);
            const http2_options = options.http2 || {};
            const duplex = ths.carrier.client.request({
                [http2.constants.HTTP2_HEADER_PATH]: '/',
                [http2.constants.HTTP2_HEADER_METHOD]: 'POST',
                ...http2_options.headers,
                'bpmux-channel': chan.toString('base64'),
                ...ths._make_http2_handshake(options.handshake_data)
            }, {
                ...http2_options.options,
                endStream: false,
                waitForTrailers: true
            });
            ths._add_http2_duplex(duplex, channel);
            setImmediate(() =>
            {
                duplex._handshake_sent = true;
                ths.emit('handshake_sent', duplex, true);
                duplex.emit('handshake_sent', true);
            });
            duplex.on('response', headers =>
            {
                const status = headers[http2.constants.HTTP2_HEADER_STATUS];
                if (status !== 200)
                {
                    const msg = `peer returned status ${status} for channel ${channel}`;
                    const err = new Error(msg);
                    err.status = status;
                    err.duplex = duplex;
                    duplex.destroy(err);
                    return ths.emit('error', err);
                }
                ths._parse_http2_handshake(duplex, headers, null);
            });
            return duplex;
        }
 
        var duplex = new BPDuplex(options, ths, channel);
 
        if (!options._delay_handshake)
        {
            setImmediate(function ()
            {
                duplex._send_handshake(options.handshake_data);
            });
        }
 
        return duplex;
    }
 
    if (options.channel !== undefined)
    {
        return this.duplexes.get(options.channel) || done(options.channel);
    }
 
    chan = this._chan;
 
    do
    {
        next = (chan + 1) % this._max_duplexes;
 
        if (!this.duplexes.has(chan + this._chan_offset))
        {
            this._chan = next;
            return done(chan + this._chan_offset);
        }
 
        chan = next;
    }
    while (chan !== this._chan);
 
    this.emit('full');
    throw new Error('full');
};
 
BPMux.prototype._add_http2_duplex = function (duplex, channel)
{
    duplex._mux = this;
    duplex.mux = this;
    duplex._chan = channel;
    duplex.get_channel = () => channel;
    duplex._handshake_sent = false;
    duplex._handshake_received = false;
    duplex._error_end = false;
    this.duplexes.set(channel, duplex);
    if ((this._max_open > 0) && (this.duplexes.size === this._max_open))
    {
        this.emit('full');
    }
    duplex.on('close', () =>
    {
        this.duplexes.delete(channel);
        this.emit('removed', duplex);
    });
    duplex.peer_error_then_end = function (chunk, encoding, cb)
    {
        this._error_end = true;
        return this.end(chunk, encoding, cb);
    };
    duplex.on('wantTrailers', function ()
    {
        this.sendTrailers(
        {
            'bpmux-error': this._error_end.toString()
        });
    });
    duplex.on('trailers', function (headers)
    {
        if (headers['bpmux-error'] === 'true')
        {
            this.emit('error', new Error('peer error'));
        }
    });
};
 
BPMux.prototype._make_http2_handshake = function (handshake)
{
    return {
        'bpmux-handshake': (handshake || Buffer.alloc(0)).toString('base64')
    };
};
 
BPMux.prototype._parse_http2_handshake = function (duplex, headers, delay_handshake)
{
    duplex._handshake_received = true;
    let handshake_data = Buffer.alloc(0);
    try
    {
        handshake_data = Buffer.from(headers['bpmux-handshake'], 'base64');
        if (this._parse_handshake_data)
        {
            handshake_data = this._parse_handshake_data(handshake_data);
        }
    }
    catch (ex)
    {
        if (duplex.listenerCount('error') > 0)
        {
            duplex.emit('error', ex);
        }
        this.emit('error', ex);
    }
    this.emit('handshake', duplex, handshake_data, delay_handshake);
    duplex.emit('handshake', handshake_data, delay_handshake);
};
 
exports.BPMux = BPMux;
exports.Http2Sessions = Http2Sessions;