11import fetch from 'node-fetch' ;
2- const defaultEndpoint = 'https://update.reactnative.cn/api' ;
3- let host = process . env . PUSHY_REGISTRY || defaultEndpoint ;
4- import fs from 'fs' ;
5- import request from 'request' ;
2+ import fs from 'node:fs' ;
3+ import util from 'node:util' ;
4+ import path from 'node:path' ;
65import ProgressBar from 'progress' ;
76import packageJson from '../package.json' ;
87import tcpp from 'tcp-ping' ;
9- import util from 'util' ;
10- import path from 'path' ;
118import filesizeParser from 'filesize-parser' ;
129import { pricingPageUrl } from './utils' ;
10+ import { Session } from 'types' ;
1311
1412const tcpPing = util . promisify ( tcpp . ping ) ;
1513
16- let session = undefined ;
17- let savedSession = undefined ;
14+ let session : Session | undefined ;
15+ let savedSession : Session | undefined ;
16+
17+ const defaultEndpoint = 'https://update.reactnative.cn/api' ;
18+ let host = process . env . PUSHY_REGISTRY || defaultEndpoint ;
1819
1920const userAgent = `react-native-update-cli/${ packageJson . version } ` ;
2021
2122export const getSession = function ( ) {
2223 return session ;
2324} ;
2425
25- export const replaceSession = function ( newSession ) {
26+ export const replaceSession = function ( newSession : { token : string } ) {
2627 session = newSession ;
2728} ;
2829
@@ -59,7 +60,7 @@ export const closeSession = function () {
5960 host = process . env . PUSHY_REGISTRY || defaultEndpoint ;
6061} ;
6162
62- async function query ( url , options ) {
63+ async function query ( url : string , options : fetch . RequestInit ) {
6364 const resp = await fetch ( url , options ) ;
6465 const text = await resp . text ( ) ;
6566 let json ;
@@ -84,8 +85,8 @@ async function query(url, options) {
8485 return json ;
8586}
8687
87- function queryWithoutBody ( method ) {
88- return function ( api ) {
88+ function queryWithoutBody ( method : string ) {
89+ return function ( api : string ) {
8990 return query ( host + api , {
9091 method,
9192 headers : {
@@ -96,8 +97,8 @@ function queryWithoutBody(method) {
9697 } ;
9798}
9899
99- function queryWithBody ( method ) {
100- return function ( api , body ) {
100+ function queryWithBody ( method : string ) {
101+ return function ( api : string , body : Record < string , any > ) {
101102 return query ( host + api , {
102103 method,
103104 headers : {
@@ -115,12 +116,11 @@ export const post = queryWithBody('POST');
115116export const put = queryWithBody ( 'PUT' ) ;
116117export const doDelete = queryWithBody ( 'DELETE' ) ;
117118
118- export async function uploadFile ( fn , key ) {
119+ export async function uploadFile ( fn : string , key : string ) {
119120 const { url, backupUrl, formData, maxSize } = await post ( '/upload' , {
120121 ext : path . extname ( fn ) ,
121122 } ) ;
122123 let realUrl = url ;
123-
124124 if ( backupUrl ) {
125125 if ( global . USE_ACC_OSS ) {
126126 realUrl = backupUrl ;
@@ -141,9 +141,9 @@ export async function uploadFile(fn, key) {
141141 const fileSize = fs . statSync ( fn ) . size ;
142142 if ( maxSize && fileSize > filesizeParser ( maxSize ) ) {
143143 throw new Error (
144- `此文件大小${ ( fileSize / 1048576 ) . toFixed (
144+ `此文件大小 ${ ( fileSize / 1048576 ) . toFixed (
145145 1 ,
146- ) } m, 超出当前额度${ maxSize } 。您可以考虑升级付费业务以提升此额度。详情请访问: ${ pricingPageUrl } `,
146+ ) } m , 超出当前额度 ${ maxSize } 。您可以考虑升级付费业务以提升此额度。详情请访问: ${ pricingPageUrl } `,
147147 ) ;
148148 }
149149
@@ -153,34 +153,30 @@ export async function uploadFile(fn, key) {
153153 total : fileSize ,
154154 } ) ;
155155
156- const info = await new Promise ( ( resolve , reject ) => {
157- if ( key ) {
158- formData . key = key ;
159- }
160- formData . file = fs . createReadStream ( fn ) ;
156+ const form = new FormData ( ) ;
161157
162- formData . file . on ( 'data' , function ( data ) {
163- bar . tick ( data . length ) ;
164- } ) ;
165- request . post (
166- realUrl ,
167- {
168- formData,
169- } ,
170- ( err , resp , body ) => {
171- if ( err ) {
172- return reject ( err ) ;
173- }
174- if ( resp . statusCode > 299 ) {
175- return reject (
176- Object . assign ( new Error ( body ) , {
177- status : resp . statusCode ,
178- } ) ,
179- ) ;
180- }
181- resolve ( { hash : formData . key } ) ;
182- } ,
183- ) ;
158+ Object . entries ( formData ) . forEach ( ( [ k , v ] ) => {
159+ form . set ( k , v ) ;
160+ } ) ;
161+ const fileStream = fs . createReadStream ( fn ) ;
162+ fileStream . on ( 'data' , function ( data ) {
163+ bar . tick ( data . length ) ;
164+ } ) ;
165+
166+ if ( key ) {
167+ form . set ( 'key' , key ) ;
168+ }
169+ form . set ( 'file' , fileStream ) ;
170+
171+ const res = await fetch ( url , {
172+ method : 'POST' ,
173+ body : form ,
184174 } ) ;
185- return info ;
175+
176+ if ( res . status > 299 ) {
177+ throw new Error ( `${ res . status } : ${ await res . text ( ) } ` ) ;
178+ }
179+
180+ // const body = await response.json();
181+ return { hash : key } ;
186182}
0 commit comments