gx
chenyc
2025-06-12 7b72ac13a83764a662159d4a49b7fffb90476ecb
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
var test = require("tape").test
var spigot = require("stream-spigot")
var concat = require("concat-stream")
 
var meter
 
// Stats
test("load", function (t) {
  t.plan(1)
 
  meter = require("../")
  t.ok(meter, "loaded module")
})
 
test("no max (passthrough)", function (t) {
  t.plan(2)
 
  var m = meter()
 
  var content = "ABCD1234"
 
  function match(d) {
    t.equals(d.toString(), content)
    t.equals(m.bytes, 8)
  }
 
  spigot([content]).pipe(m).pipe(concat(match))
})
 
test("under max", function (t) {
  t.plan(2)
 
  var m = meter(100)
 
  var content = "ABCD1234"
 
  function match(d) {
    t.equals(d.toString(), content)
    t.equals(m.bytes, 8)
  }
 
  spigot([content]).pipe(m).pipe(concat(match))
})
 
test("stops at meter", function (t) {
  t.plan(3)
 
  var chunks = 0
 
  function match(d) {
    t.fail()
  }
 
  var c = concat(match)
 
  var m = meter(10)
  m.on("error", function (e) {
    t.ok(e.message, "Stream exceeded specified max of 10 bytes.")
    // 12 because read frame is 4, so the 3rd read will put it over the max at 12 bytes
    t.equals(c.getBody().toString(), "ABCDEFGHIJKL")
    t.equals(m.bytes, 12)
  })
 
  spigot(["ABCD", "EFGH", "IJKL", "MNOP", "QRST", "UVWX", "YZ"]).pipe(m).pipe(c)
})