Skip to content

Commit 5dd3b64

Browse files
committed
ts
1 parent 31ec3ed commit 5dd3b64

File tree

6 files changed

+221
-197
lines changed

6 files changed

+221
-197
lines changed

bun.lockb

1.05 KB
Binary file not shown.

src/api.js renamed to src/api.ts

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
import 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';
65
import ProgressBar from 'progress';
76
import packageJson from '../package.json';
87
import tcpp from 'tcp-ping';
9-
import util from 'util';
10-
import path from 'path';
118
import filesizeParser from 'filesize-parser';
129
import { pricingPageUrl } from './utils';
10+
import { Session } from 'types';
1311

1412
const 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

1920
const userAgent = `react-native-update-cli/${packageJson.version}`;
2021

2122
export 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');
115116
export const put = queryWithBody('PUT');
116117
export 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
}

src/bundle.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import path from 'path';
1+
import path from 'node:path';
22
import { getRNVersion, translateOptions } from './utils';
33
import * as fs from 'fs-extra';
44
import { ZipFile } from 'yazl';
55
import { open as openZipFile } from 'yauzl';
66
import { question, printVersionCommand } from './utils';
77
import { checkPlatform } from './app';
8-
import { spawn, spawnSync } from 'child_process';
8+
import { spawn, spawnSync } from 'node:child_process';
99
const g2js = require('gradle-to-js/lib/parser');
1010
import os from 'os';
1111
const properties = require('properties');
File renamed without changes.

src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
declare global {
2+
var NO_INTERACTIVE: boolean;
3+
var USE_ACC_OSS: boolean;
4+
}
5+
6+
export interface Session {
7+
token: string;
8+
}

0 commit comments

Comments
 (0)