@@ -3,7 +3,7 @@ const fs = require('fs');
3
3
const Module = require ( 'module' ) ;
4
4
const { execFile } = require ( 'child_process' ) ;
5
5
const { app, autoUpdater } = require ( 'electron' ) ;
6
- const { get } = require ( 'https ' ) ;
6
+ const { get } = require ( '../utils/get ' ) ;
7
7
8
8
const paths = require ( '../paths' ) ;
9
9
@@ -32,20 +32,6 @@ const resetTracking = () => {
32
32
installing = Object . assign ( { } , base ) ;
33
33
} ;
34
34
35
- const req = url => new Promise ( res => get ( url , r => { // Minimal wrapper around https.get to include body
36
- let dat = '' ;
37
- r . on ( 'data' , b => dat += b . toString ( ) ) ;
38
-
39
- r . on ( 'end' , ( ) => res ( [ r , dat ] ) ) ;
40
- } ) ) ;
41
-
42
- const redirs = url => new Promise ( res => get ( url , r => { // Minimal wrapper around https.get to follow redirects
43
- const loc = r . headers . location ;
44
- if ( loc ) return redirs ( loc ) . then ( res ) ;
45
-
46
- res ( r ) ;
47
- } ) ) ;
48
-
49
35
exports . init = ( endpoint , { releaseChannel, version } ) => {
50
36
skipHost = settings . get ( 'SKIP_HOST_UPDATE' ) ;
51
37
skipModule = settings . get ( 'SKIP_MODULE_UPDATE' ) ;
@@ -77,10 +63,10 @@ exports.init = (endpoint, { releaseChannel, version }) => {
77
63
}
78
64
79
65
checkForUpdates ( ) {
80
- req ( this . url ) . then ( ( [ r , b ] ) => {
81
- if ( r . statusCode === 204 ) return this . emit ( 'update-not-available' ) ;
66
+ get ( this . url ) . then ( ( [ r , b ] ) => {
67
+ if ( ! b || r === 204 ) return this . emit ( 'update-not-available' ) ;
82
68
83
- this . emit ( 'update-manually' , b ) ;
69
+ this . emit ( 'update-manually' , b . toString ( ) ) ;
84
70
} ) ;
85
71
}
86
72
@@ -111,7 +97,12 @@ exports.init = (endpoint, { releaseChannel, version }) => {
111
97
} ;
112
98
113
99
const checkModules = async ( ) => {
114
- remote = JSON . parse ( ( await req ( baseUrl + '/versions.json' + qs ) ) [ 1 ] ) ;
100
+ const buf = ( await get ( baseUrl + '/versions.json' + qs ) ) [ 1 ] ;
101
+ if ( ! buf ) {
102
+ log ( 'Modules' , 'versions.json retrieval failure.' ) ;
103
+ return ;
104
+ }
105
+ remote = JSON . parse ( buf . toString ( ) ) ;
115
106
116
107
for ( const name in installed ) {
117
108
const inst = installed [ name ] . installedVersion ;
@@ -131,27 +122,22 @@ const downloadModule = async (name, ver) => {
131
122
downloading . total ++ ;
132
123
133
124
const path = join ( downloadPath , name + '-' + ver + '.zip' ) ;
134
- const file = fs . createWriteStream ( path ) ;
135
125
136
126
// log('Modules', 'Downloading', `${name}@${ver}`);
137
127
138
- let success , total , cur = 0 ;
139
- const res = await redirs ( baseUrl + '/' + name + '/' + ver + qs ) ;
140
- success = res . statusCode === 200 ;
141
- total = parseInt ( res . headers [ 'content-length' ] ?? 1 , 10 ) ;
142
-
143
- res . pipe ( file ) ;
128
+ let success , total , cur = 0 ;
129
+ const res = await get ( baseUrl + '/' + name + '/' + ver + qs ) ;
130
+ success = res [ 0 ] === 200 ;
144
131
145
- res . on ( 'data' , c => {
146
- cur += c . length ;
147
-
148
- events . emit ( 'downloading-module' , { name, cur, total } ) ;
149
- } ) ;
150
-
151
- await new Promise ( ( res ) => file . on ( 'close' , res ) ) ;
152
-
153
- if ( success ) commitManifest ( ) ;
154
- else downloading . fail ++ ;
132
+ // todo: if a progress-bar-like interface and stream-like file writing are still wanted, implement
133
+ if ( success ) {
134
+ total = parseInt ( res [ 2 ] . get ( 'content-length' ) ?? 1 , 10 ) ;
135
+ events . emit ( 'downloading-module' , { name, total, total } ) ;
136
+ fs . writeFile ( path , res [ 1 ] , e => log ( 'ModuleUpdate' , 'Writing to file failed:' , e ) ) ;
137
+ commitManifest ( ) ;
138
+ } else {
139
+ downloading . fail ++ ;
140
+ }
155
141
156
142
events . emit ( 'downloaded-module' , {
157
143
name
0 commit comments