Skip to content

Commit addc2c8

Browse files
committed
Merge pull request #3 from danyg/master
Binary safe
2 parents 21563f5 + 6b74c3a commit addc2c8

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ results
1414

1515
node_modules
1616
npm-debug.log
17+
/test/justified.srt

lib/endpoints.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,18 @@ ep.prototype.get = function(action, params, cb) {
3434

3535
//console.log(this.options);
3636

37-
var data = '';
37+
var data = [];
3838
var req = http.request(this.options);
3939
req.on('response', function (response) {
4040

4141
//to avoid problems with subtitle with a chatset different than utf-8
42-
response.setEncoding('binary');
4342

4443
response.on('data', function (chunk) {
45-
data += chunk;
44+
if(!!chunk && chunk.length > 0){
45+
data.push(chunk);
46+
}
4647
}).on('end', function(){
47-
cb(null, this.statusCode, data);
48+
cb(null, this.statusCode, Buffer.concat(data));
4849
});
4950
}).on('error', function(e) {
5051
cb(e);
@@ -77,17 +78,19 @@ ep.prototype.post = function(action, file, params, cb) {
7778
}).on('end', function(){
7879
cb(null, this.statusCode, data);
7980
});
81+
req.end();
8082
}).on('error', function(e) {
8183
cb(e);
84+
req.end();
8285
});
83-
req.end();
8486
};
8587

8688

8789
/** protocol methods */
8890
ep.prototype.available_languages = function(cb){
8991
this.get('languages', {}, function(err, status, res) {
9092
if(err) return cb(err);
93+
res = res.toString();
9194

9295
if(status == 400) return cb('Bad Request');
9396

@@ -101,6 +104,7 @@ ep.prototype.search_subtitles = function(hash, versions, cb){
101104

102105
this.get('search', params, function(err, status, res){
103106
if(err) return cb(err);
107+
res = res.toString();
104108

105109
if(status == 400) return cb('Bad Request');
106110
if(status == 404) return cb(null, ''); // no subtitle found
@@ -117,7 +121,7 @@ ep.prototype.download_subtitle = function(hash, lang, path, cb){
117121
if(status == 400) return cb('Bad Request');
118122
if(status == 404) return cb(null, ''); // no subtitle found
119123

120-
fs.writeFile(path, res, function(err) {
124+
fs.writeFile(path, res, {encoding: 'binary'}, function(err) {
121125
if(err) return cb(err);
122126

123127
cb(null, path);

test/justified-FR-ANSI.srt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
1
2+
00:00:05,000 --> 00:00:15,000
3+
Attention: Ceci est une légende de test.
4+
5+
2
6+
00:00:25,000 --> 00:00:40,000
7+
SubDB - une base de données libre de sous-titres.
8+
http://thesubdb.com
9+

test/test.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,26 @@ describe('Subdb', function() {
7373

7474
describe('#download_subtitle', function(){
7575
it('should download subtitle', function(done) {
76-
var path = process.cwd()+'/test/justified.srt';
76+
var path = process.cwd()+'/test/justified.srt',
77+
properlyEncoded = process.cwd() + '/test/justified-FR-ANSI.srt'
78+
;
79+
80+
var bufferExpectedFile = fs.readFileSync(properlyEncoded);
81+
7782
fs.unlink(path, function() {
7883
var subdb = new SubDb();
7984
subdb.api.download_subtitle('edc1981d6459c6111fe36205b4aff6c2', 'fr', path, function(err, res){
8085
if(err) return done(err);
8186

82-
fs.stat(res, function(err, stat) {
83-
if(err) return done(err);
87+
var bufferActualFile = fs.readFileSync(path);
88+
89+
assert.equal(bufferActualFile.length, bufferExpectedFile.length);
8490

85-
assert.equal(stat.size, 179);
91+
for(var i = 0; i < bufferActualFile.length; i++){
92+
assert.equal(bufferActualFile[i], bufferExpectedFile[i]);
93+
}
8694

87-
done();
88-
});
95+
done();
8996
});
9097
});
9198
});

0 commit comments

Comments
 (0)