UNPKG

5.14 kBJavaScriptView Raw
1/*
2
3 miniwrite
4
5 https://github.com/Bartvds/miniwrite
6
7 Copyright (c) 2013 Bart van der Schoor
8
9 Permission is hereby granted, free of charge, to any person
10 obtaining a copy of this software and associated documentation
11 files (the "Software"), to deal in the Software without
12 restriction, including without limitation the rights to use,
13 copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the
15 Software is furnished to do so, subject to the following
16 conditions:
17
18 The above copyright notice and this permission notice shall be
19 included in all copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
32
33(function () {
34 'use strict';
35
36 var core = require('./core');
37 var mkdirp = require('mkdirp');
38 var path = require('path');
39 var fs = require('fs');
40
41 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
42
43 // node.js stream
44 function stream(nodeStream, linebreak) {
45 linebreak = (typeof linebreak !== 'undefined' ? linebreak : '\n');
46
47 var mw = core.base();
48 mw.enabled = true;
49 mw.stream = nodeStream;
50 mw.linebreak = linebreak;
51 mw.writeln = function (line) {
52 if (mw.enabled) {
53 mw.stream.write(line + linebreak);
54 }
55 };
56 mw.toString = function () {
57 return '<miniwrite-stream>';
58 };
59 return mw;
60 }
61
62 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
63
64 // node.js file stream
65 //TODO add auto close?
66 //TODO this is buggy: won't flush on exit()
67 function disk(file, linebreak) {
68 var mw = core.base();
69 mw.linebreak = (typeof linebreak !== 'undefined' ? linebreak : '\n');
70 mw.enabled = true;
71 mw.file = file;
72 var stream;
73 var start = 0;
74 var buffer = core.buffer();
75 var streaming = {
76 writeln: function (line) {
77 var buff = new Buffer(line + mw.linebreak, 'utf8');
78 start += buff.length;
79 stream.write(buff);
80 }
81 };
82 var active = buffer;
83 var splitter = core.splitter({
84 writeln: function (line) {
85 active.writeln(line);
86 }
87 });
88
89 var opening = false;
90 var initing = false;
91 (function () {
92 initing = true;
93 function finalKill(err) {
94 initing = false;
95 if (err) {
96 console.log(err);
97 return;
98 }
99 // auto start
100 start = 0;
101 if (buffer.lines.length > 0) {
102 open(mw.file);
103 return;
104 }
105 // and done already
106 if (flushCallback.length > 0) {
107 doFlush();
108 }
109 }
110
111 fs.exists(mw.file, function (exists) {
112 if (!exists) {
113 finalKill();
114 return;
115 }
116 fs.truncate(mw.file, 0, finalKill);
117 });
118 }());
119
120 function open(dest) {
121 if (!stream && !opening) {
122 opening = true;
123 mkdirp(path.dirname(dest), function (err) {
124 /*jshint -W115 */
125 opening = false;
126 if (err) {
127 console.log(err);
128 return;
129 }
130 stream = fs.createWriteStream(dest, {
131 start: start,
132 flags: (start === 0 ? 'w' : 'a'),
133 encoding: 'utf8',
134 mode: '0644'
135 });
136 active = streaming;
137 //flush buffer
138 buffer.lines.forEach(function (line) {
139 active.writeln(line);
140 });
141 buffer.clear();
142
143 // and done already
144 if (flushCallback.length > 0) {
145 doFlush();
146 }
147 });
148 }
149 }
150
151 mw.writeln = function (line) {
152 //console.log('writeln >' + line + '<');
153 if (mw.enabled) {
154 if (!stream) {
155 open(mw.file);
156 }
157 splitter.writeln(line);
158 }
159 };
160
161 var flushCallback = [];
162
163 function doFlush() {
164 if (initing || opening) {
165 return;
166 }
167 active = buffer;
168 var callbackList = flushCallback;
169 flushCallback = [];
170 if (stream) {
171 stream.on('finish', function () {
172 callbackList.forEach(function (callback) {
173 callback(mw);
174 });
175 });
176 stream.end();
177 stream = null;
178 return;
179 }
180 process.nextTick(function () {
181 callbackList.forEach(function (callback) {
182 callback(mw);
183 });
184 });
185 }
186
187 mw.flush = function (callback) {
188 flushCallback.push(callback);
189 doFlush();
190 };
191 mw.toString = function () {
192 return '<miniwrite-stream>';
193 };
194 return mw;
195 }
196
197 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
198
199 // assemble exports
200 var io = {
201 stream: stream,
202 disk: disk
203 };
204 module.exports = io;
205
206}).call();