-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
263 lines (227 loc) · 7.55 KB
/
index.js
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
var util = require('util');
var stream = require('stream');
var fs = require('fs');
var os = require('os');
var crypto = require('crypto');
var childProcess = require('child_process');
var async = require('async');
var dbg = require('debug');
var debug = dbg('bpgencoder');
var debugConstructor = dbg('bpgencoder:constructor');
var debugWriter = dbg('bpgencoder:writer');
var debugReader = dbg('bpgencoder:reader');
// Byte values that *all* JPG and PNG images start with, respectively.
var JPG_HEX = 'ffd8';
var PNG_HEX = '89504e47';
function randomString(length, chars) {
if(!chars) {
throw new Error('Argument \'chars\' is undefined');
}
var charsLength = chars.length;
if(charsLength > 256) {
throw new Error('Argument \'chars\' should not have more than 256 characters'
+ ', otherwise unpredictability will be broken');
}
var randomBytes = crypto.randomBytes(length)
var result = new Array(length);
var cursor = 0;
for (var i = 0; i < length; i++) {
cursor += randomBytes[i];
result[i] = chars[cursor % charsLength]
};
return result.join('');
}
function randomAsciiString(length) {
return randomString(length,
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');
}
/*
* A duplex stream that takes in either a PNG or JPG stream, and outputs a BPG
* stream.
*/
module.exports = Encoder;
function Encoder() {
stream.Duplex.call(this);
/*
* Determines whether or not the original image file has been copied.
*/
this.copied = false;
/*
* Determines whether or not we are in the process of reading.
*/
this.reading = false;
/*
* The path where we will be temporarily storing our buffer. Note: this will
* **not** have a file extension.
*/
this.filepath = os.tmpdir() + randomAsciiString(16);
debugConstructor('Path to store file: %s', this.filepath);
/*
* This is what will be writing the original file to the temporary
* destination.
*/
this._writeStream = fs
.createWriteStream(this.filepath);
this.on('_source-finished', function () {
debugConstructor('Done writing input stream to %s', this.filepath);
this.copied = true;
}.bind(this));
this.on('pipe', function (src) {
debugConstructor('A stream has been piped');
var ended = false;
var onEnd = function () {
if (!ended) {
debugConstructor('The piped stream has ended');
ended = true;
this.copied = true;
this.emit('_source-finished');
}
}.bind(this);
src.on('error', function (error) {
debugConstructor('An error occurred %s', error.message);
this.emit('error', error);
}.bind(this));
// These might be called before the source stream has finished writing.
src.on('end', onEnd);
src.on('close', onEnd);
}.bind(this));
}
util.inherits(Encoder, stream.Duplex);
/*
* Override of the Duplex#_write method. This will simply be writing out the
* inputted chunks to the temporary file.
*/
Encoder.prototype._write = function (chunk, encoding, callback) {
debugWriter('Writing chunk of size %d to disk', chunk.length);
this._writeStream.write(chunk, callback);
};
/*
* Override of the Duplex#_read method. This will simply be reading in the
* outputted BPG image.
*/
Encoder.prototype._read = function () {
// Used to keep the code DRY.
var beginReadable = function () {
if (this.reading) {
debugReader('Attempted to start reading.');
return;
}
this.reading = true;
debugReader('Starting to read the original image from disk');
async.waterfall([
// Get the format of the given file.
function (callback) {
debugReader('Reading from %s', this.filepath);
var tempReadStream = fs.createReadStream(this.filepath);
var stoppedReading = false;
tempReadStream.on('readable', function () {
debugReader('Did we stop reading? %s', stoppedReading ? 'Yes' : 'No');
if (stoppedReading) { return; }
stoppedReading = true;
debugReader('Stopped the read');
tempReadStream.pause();
var chunk = tempReadStream.read(4);
var format;
try {
format = Encoder.getImageFormatCode(chunk);
} catch (e) {
return callback(e);
}
debugReader('Image is of format %s', format);
callback(null, format);
});
tempReadStream.on('error', function (e) {
if (stoppedReading) { return; }
debugReader('An error occurred while reading from disk', e.message);
this.emit('error', e);
}.bind(this));
}.bind(this),
// Rename the file into the appropriate format.
function (format, callback) {
var destFilepath = this.filepath + '.' + format;
debugReader('Will rename to to %s', destFilepath);
fs.rename(this.filepath, destFilepath, function (err) {
debugReader('Done renaming to %s', destFilepath);
if (err) { return callback(err); }
callback(null, destFilepath);
});
}.bind(this),
// Encode the image into BPG format.
function (filepath, callback) {
var bpgpath = this.filepath + '.' + 'bpg';
debugReader('Will temporarily write the BPG file to %s', bpgpath);
var bpgenc = childProcess.spawn(
'bpgenc', [ filepath, '-o', bpgpath ]
);
bpgenc.on('close', function (code) {
debugReader('The encoder has exitted with code %d', code);
fs.unlink(filepath);
if (code) {
var error = new Error(
'The BPG encoder exitted with a non-zero error code ' + code
+ '.'
);
debugReader('An error occurred: %s', error.message);
return callback(
error
);
}
callback(null, bpgpath);
});
}.bind(this),
], function (err, bpgpath) {
if (err) { this.emit('error', err); }
var ended = false;
// Called when the stream has ended.
var onEnd = function () {
if (!ended) {
debugReader('The reader has ended');
fs.unlink(bpgpath);
ended = true;
this.push(null);
}
}.bind(this);
// This will be reading the generated BPG image, and pushing it out onto
// the listener.
debugReader('The BPG image file should be at %s', bpgpath);
var readStream = fs.createReadStream(bpgpath);
readStream.on('data', function (data) {
this.push(data);
}.bind(this));
readStream.on('close', onEnd);
readStream.on('end', onEnd);
readStream.on('error', function (e) { this.emit(e); }.bind(this))
}.bind(this));
}.bind(this);
if (!this.copied) {
this.on('_source-finished', function () {
beginReadable();
}.bind(this));
return;
}
beginReadable();
};
/*
* Determines whether or not the buffer represents JPG.
*/
Encoder.isJPG = function (buffer) {
var bufferChunk = buffer.slice(0, JPG_HEX.length / 2).toString('hex');
debug('Buffer\'s first two bytes: %s', bufferChunk);
return JPG_HEX === bufferChunk;
};
/*
* Determines whther or not the buffer represents PNG.
*/
Encoder.isPNG = function (buffer) {
var bufferChunk = buffer.slice(0, PNG_HEX.length / 2).toString('hex');
debug('Buffer\'s first four bytes: %s', bufferChunk);
return PNG_HEX === bufferChunk;
};
/*
* Gets the image format code of the supplied buffer.
*/
Encoder.getImageFormatCode = function (buffer) {
if (Encoder.isJPG(buffer)) { return 'jpg'; }
else if (Encoder.isPNG(buffer)) { return 'png'; }
throw new Error('Unknown format');
};