Backup of db before drupal security update
[yaffs-website] / node_modules / phridge / lib / forkStdout.js
1 "use strict";
2
3 var os = require("os");
4 var util = require("util");
5 var ForkStream = require("fork-stream");
6 var Linerstream = require("linerstream");
7 var Transform = require("stream").Transform;
8
9 var messageToNode = "message to node: ";
10
11 /**
12  * Creates a fork stream which pipes messages starting with 'message to node: ' to our phridge stream
13  * and any other message to the other stream. Thus console.log() inside PhantomJS is still printed to the
14  * console while using stdout as communication channel for phridge.
15  *
16  * @param {stream.Readable} stdout
17  * @returns {{phridge: stream.Readable, cleanStdout: stream.Readable}}
18  */
19 function forkStdout(stdout) {
20     var fork;
21     var phridgeEndpoint;
22     var cleanStdoutEndpoint;
23
24     // Expecting a character stream because we're splitting messages by an EOL-character
25     stdout.setEncoding("utf8");
26
27     fork = new ForkStream({
28         classifier: function (chunk, done) {
29             chunk = chunk
30                 .slice(0, messageToNode.length);
31             done(null, chunk === messageToNode);
32         }
33     });
34
35     stdout
36         .pipe(new Linerstream())
37         .pipe(fork);
38
39     // Removes the 'message to node: '-prefix from every chunk.
40     phridgeEndpoint = fork.a.pipe(new CropPhridgePrefix({
41         encoding: "utf8"
42     }));
43
44     // We need to restore EOL-character in stdout stream
45     cleanStdoutEndpoint = fork.b.pipe(new RestoreLineBreaks({
46         encoding: "utf8"
47     }));
48
49     return {
50         phridge: phridgeEndpoint,
51         cleanStdout: cleanStdoutEndpoint
52     };
53 }
54
55 /**
56  * Appends an EOL-character to every chunk.
57  *
58  * @param {Object} options stream options
59  * @constructor
60  * @private
61  */
62 function RestoreLineBreaks(options) {
63     Transform.call(this, options);
64 }
65 util.inherits(RestoreLineBreaks, Transform);
66
67 RestoreLineBreaks.prototype._transform = function (chunk, enc, cb) {
68     this.push(chunk + os.EOL);
69     cb();
70 };
71
72 /**
73  * Removes the 'message to node: '-prefix from every chunk.
74  *
75  * @param {Object} options stream options
76  * @constructor
77  * @private
78  */
79 function CropPhridgePrefix(options) {
80     Transform.call(this, options);
81 }
82 util.inherits(CropPhridgePrefix, Transform);
83
84 CropPhridgePrefix.prototype._transform = function (chunk, enc, cb) {
85     this.push(chunk.slice(messageToNode.length));
86     cb();
87 };
88
89 module.exports = forkStdout;