'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
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
var Stream = require("stream")
var writeMethods = ["write", "end", "destroy"]
var readMethods = ["resume", "pause"]
var readEvents = ["data", "close"]
var slice = Array.prototype.slice
 
module.exports = duplex
 
function forEach (arr, fn) {
    if (arr.forEach) {
        return arr.forEach(fn)
    }
 
    for (var i = 0; i < arr.length; i++) {
        fn(arr[i], i)
    }
}
 
function duplex(writer, reader) {
    var stream = new Stream()
    var ended = false
 
    forEach(writeMethods, proxyWriter)
 
    forEach(readMethods, proxyReader)
 
    forEach(readEvents, proxyStream)
 
    reader.on("end", handleEnd)
 
    writer.on("drain", function() {
      stream.emit("drain")
    })
 
    writer.on("error", reemit)
    reader.on("error", reemit)
 
    stream.writable = writer.writable
    stream.readable = reader.readable
 
    return stream
 
    function proxyWriter(methodName) {
        stream[methodName] = method
 
        function method() {
            return writer[methodName].apply(writer, arguments)
        }
    }
 
    function proxyReader(methodName) {
        stream[methodName] = method
 
        function method() {
            stream.emit(methodName)
            var func = reader[methodName]
            if (func) {
                return func.apply(reader, arguments)
            }
            reader.emit(methodName)
        }
    }
 
    function proxyStream(methodName) {
        reader.on(methodName, reemit)
 
        function reemit() {
            var args = slice.call(arguments)
            args.unshift(methodName)
            stream.emit.apply(stream, args)
        }
    }
 
    function handleEnd() {
        if (ended) {
            return
        }
        ended = true
        var args = slice.call(arguments)
        args.unshift("end")
        stream.emit.apply(stream, args)
    }
 
    function reemit(err) {
        stream.emit("error", err)
    }
}