@@ -34,17 +34,18 @@ ep.prototype.get = function(action, params, cb) {
34
34
35
35
//console.log(this.options);
36
36
37
- var data = '' ;
37
+ var data = [ ] ;
38
38
var req = http . request ( this . options ) ;
39
39
req . on ( 'response' , function ( response ) {
40
40
41
41
//to avoid problems with subtitle with a chatset different than utf-8
42
- response . setEncoding ( 'binary' ) ;
43
42
44
43
response . on ( 'data' , function ( chunk ) {
45
- data += chunk ;
44
+ if ( ! ! chunk && chunk . length > 0 ) {
45
+ data . push ( chunk ) ;
46
+ }
46
47
} ) . on ( 'end' , function ( ) {
47
- cb ( null , this . statusCode , data ) ;
48
+ cb ( null , this . statusCode , Buffer . concat ( data ) ) ;
48
49
} ) ;
49
50
} ) . on ( 'error' , function ( e ) {
50
51
cb ( e ) ;
@@ -77,17 +78,19 @@ ep.prototype.post = function(action, file, params, cb) {
77
78
} ) . on ( 'end' , function ( ) {
78
79
cb ( null , this . statusCode , data ) ;
79
80
} ) ;
81
+ req . end ( ) ;
80
82
} ) . on ( 'error' , function ( e ) {
81
83
cb ( e ) ;
84
+ req . end ( ) ;
82
85
} ) ;
83
- req . end ( ) ;
84
86
} ;
85
87
86
88
87
89
/** protocol methods */
88
90
ep . prototype . available_languages = function ( cb ) {
89
91
this . get ( 'languages' , { } , function ( err , status , res ) {
90
92
if ( err ) return cb ( err ) ;
93
+ res = res . toString ( ) ;
91
94
92
95
if ( status == 400 ) return cb ( 'Bad Request' ) ;
93
96
@@ -101,6 +104,7 @@ ep.prototype.search_subtitles = function(hash, versions, cb){
101
104
102
105
this . get ( 'search' , params , function ( err , status , res ) {
103
106
if ( err ) return cb ( err ) ;
107
+ res = res . toString ( ) ;
104
108
105
109
if ( status == 400 ) return cb ( 'Bad Request' ) ;
106
110
if ( status == 404 ) return cb ( null , '' ) ; // no subtitle found
@@ -117,7 +121,7 @@ ep.prototype.download_subtitle = function(hash, lang, path, cb){
117
121
if ( status == 400 ) return cb ( 'Bad Request' ) ;
118
122
if ( status == 404 ) return cb ( null , '' ) ; // no subtitle found
119
123
120
- fs . writeFile ( path , res , function ( err ) {
124
+ fs . writeFile ( path , res , { encoding : 'binary' } , function ( err ) {
121
125
if ( err ) return cb ( err ) ;
122
126
123
127
cb ( null , path ) ;
0 commit comments