Skip to content

Commit 935f784

Browse files
committed
refactor: pass logger to utils/common,qetag,storage
1 parent 85a09fa commit 935f784

File tree

5 files changed

+48
-43
lines changed

5 files changed

+48
-43
lines changed

src/core/config.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,12 @@ export const config = {
105105
// data dir for caclulate diff files. it's optimization.
106106
dataDir: process.env.DATA_DIR || os.tmpdir(),
107107
// storageType which is your binary package files store. options value is ("local" | "qiniu" | "s3"| "oss" || "tencentcloud")
108-
storageType: process.env.STORAGE_TYPE || 'local',
108+
storageType: (process.env.STORAGE_TYPE || 'local') as
109+
| 'local'
110+
| 'qiniu'
111+
| 's3'
112+
| 'oss'
113+
| 'tencentcloud',
109114
// options value is (true | false), when it's true, it will cache updateCheck results in redis.
110115
updateCheckCache: toBool(process.env.UPDATE_CHECK_CACHE),
111116
// options value is (true | false), when it's true, it will cache rollout results in redis

src/core/services/package-manager.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,19 @@ class PackageManager {
205205
return dataCenterManager.getPackageInfo(packageHash);
206206
}
207207
const downloadURL = getBlobDownloadUrl(blobHash);
208-
return createFileFromRequest(downloadURL, path.join(workDirectoryPath, blobHash)).then(
209-
() => {
210-
return unzipFile(
211-
path.join(workDirectoryPath, blobHash),
212-
path.join(workDirectoryPath, 'current'),
213-
).then((outputPath) => {
214-
return dataCenterManager.storePackage(outputPath, true, logger);
215-
});
216-
},
217-
);
208+
return createFileFromRequest(
209+
downloadURL,
210+
path.join(workDirectoryPath, blobHash),
211+
logger,
212+
).then(() => {
213+
return unzipFile(
214+
path.join(workDirectoryPath, blobHash),
215+
path.join(workDirectoryPath, 'current'),
216+
logger,
217+
).then((outputPath) => {
218+
return dataCenterManager.storePackage(outputPath, true, logger);
219+
});
220+
});
218221
});
219222
}
220223

@@ -269,6 +272,7 @@ class PackageManager {
269272
return createFileFromRequest(
270273
downloadURL,
271274
path.join(workDirectoryPath, diffManifestBlobHash),
275+
logger,
272276
).then(() => {
273277
const dataCenterContentPath = path.join(workDirectoryPath, 'dataCenter');
274278
copySync(originDataCenter.contentPath, dataCenterContentPath);
@@ -294,8 +298,8 @@ class PackageManager {
294298
dataCenterContentPath,
295299
hotCodePushFile,
296300
).then((data) => {
297-
return qetag(data.path).then((diffHash) => {
298-
return uploadFileToStorage(diffHash, fileName).then(() => {
301+
return qetag(data.path, logger).then((diffHash) => {
302+
return uploadFileToStorage(diffHash, fileName, logger).then(() => {
299303
const stats = fs.statSync(fileName);
300304
return PackagesDiff.create({
301305
package_id: packageId,
@@ -413,9 +417,9 @@ class PackageManager {
413417
const directoryPath = path.join(directoryPathParent, 'current');
414418
logger.debug(`releasePackage generate an random dir path: ${directoryPath}`);
415419
return Promise.all([
416-
qetag(filePath),
420+
qetag(filePath, logger),
417421
createEmptyFolder(directoryPath).then(() => {
418-
return unzipFile(filePath, directoryPath);
422+
return unzipFile(filePath, directoryPath, logger);
419423
}),
420424
])
421425
.then(([blobHash]) => {
@@ -463,12 +467,12 @@ class PackageManager {
463467
logger.debug(e.message);
464468
throw e;
465469
}
466-
return qetag(manifestFile);
470+
return qetag(manifestFile, logger);
467471
})
468472
.then((manifestHash) => {
469473
return Promise.all([
470-
uploadFileToStorage(manifestHash, manifestFile),
471-
uploadFileToStorage(blobHash, filePath),
474+
uploadFileToStorage(manifestHash, manifestFile, logger),
475+
uploadFileToStorage(blobHash, filePath, logger),
472476
]).then(() => [packageHash, manifestHash, blobHash]);
473477
});
474478
});

src/core/utils/common.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { pipeline } from 'stream';
44
import util from 'util';
55
import extract from 'extract-zip';
66
import fsextra from 'fs-extra';
7-
import { logger } from 'kv-logger';
7+
import { Logger } from 'kv-logger';
88
import _ from 'lodash';
99
import fetch from 'node-fetch';
1010
import validator from 'validator';
@@ -87,7 +87,7 @@ export function validatorVersion(versionNo: string) {
8787
return [flag, min, max];
8888
}
8989

90-
export async function createFileFromRequest(url, filePath) {
90+
export async function createFileFromRequest(url: string, filePath: string, logger: Logger) {
9191
try {
9292
await fs.promises.stat(filePath);
9393
return;
@@ -135,37 +135,33 @@ export function createEmptyFolderSync(folderPath: string) {
135135
fsextra.mkdirsSync(folderPath);
136136
}
137137

138-
export async function unzipFile(zipFile: string, outputPath: string) {
138+
export async function unzipFile(zipFile: string, outputPath: string, logger: Logger) {
139139
try {
140140
logger.debug(`unzipFile check zipFile ${zipFile} fs.R_OK`);
141141
fs.accessSync(zipFile, fs.constants.R_OK);
142142
logger.debug(`Pass unzipFile file ${zipFile}`);
143143
} catch (err) {
144-
logger.error(err);
145144
throw new AppError(err.message);
146145
}
147146

148147
try {
149148
await extract(zipFile, { dir: outputPath });
150149
logger.debug(`unzipFile success`);
151150
} catch (err) {
152-
logger.error(err);
153151
throw new AppError(`it's not a zipFile`);
154152
}
155153
return outputPath;
156154
}
157155

158156
export function getBlobDownloadUrl(blobUrl: string): string {
159157
let fileName = blobUrl;
160-
const storageType = _.get(config, 'common.storageType');
161-
const downloadUrl = _.get(config, `${storageType}.downloadUrl`);
158+
const { storageType } = config.common;
159+
const { downloadUrl } = config[storageType];
162160
if (storageType === 'local') {
163161
fileName = `${blobUrl.substring(0, 2).toLowerCase()}/${blobUrl}`;
164162
}
165163
if (!validator.isURL(downloadUrl)) {
166-
const e = new AppError(`Please config ${storageType}.downloadUrl in config.js`);
167-
logger.error(e);
168-
throw e;
164+
throw new AppError(`Please config ${storageType}.downloadUrl in config.js`);
169165
}
170166
return `${downloadUrl}/${fileName}`;
171167
}

src/core/utils/qetag.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Buffer } from 'buffer';
22
import crypto from 'crypto';
33
import fs from 'fs';
44
import { Stream, Readable } from 'stream';
5-
import { logger } from 'kv-logger';
5+
import { Logger } from 'kv-logger';
66
import { AppError } from '../app-error';
77

88
// 计算文件的eTag,参数为buffer或者readableStream或者文件路径
@@ -87,7 +87,7 @@ function getEtag(buffer: string | Stream | Buffer, callback: (etag: string) => v
8787
}
8888

8989
// TODO: support only files (string)?
90-
export function qetag(buffer: string | Stream | Buffer): Promise<string> {
90+
export function qetag(buffer: string | Stream | Buffer, logger: Logger): Promise<string> {
9191
if (typeof buffer === 'string') {
9292
// it's a file
9393
try {

src/core/utils/storage.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import ALY from 'aliyun-sdk';
55
import AWS from 'aws-sdk';
66
import COS from 'cos-nodejs-sdk-v5';
77
import fsextra from 'fs-extra';
8-
import { logger } from 'kv-logger';
8+
import { Logger } from 'kv-logger';
99
import _ from 'lodash';
1010
import qiniu from 'qiniu';
1111

1212
import { AppError } from '../app-error';
1313
import { config } from '../config';
1414

15-
function uploadFileToLocal(key: string, filePath: string): Promise<void> {
15+
function uploadFileToLocal(key: string, filePath: string, logger: Logger): Promise<void> {
1616
return new Promise((resolve, reject) => {
1717
logger.info(`try uploadFileToLocal`, {
1818
key,
@@ -76,7 +76,7 @@ function uploadFileToLocal(key: string, filePath: string): Promise<void> {
7676
});
7777
}
7878

79-
function uploadFileToS3(key: string, filePath: string): Promise<void> {
79+
function uploadFileToS3(key: string, filePath: string, logger: Logger): Promise<void> {
8080
return new Promise((resolve, reject) => {
8181
logger.info('try uploadFileToS3', { key });
8282
AWS.config.update({
@@ -111,7 +111,7 @@ function uploadFileToS3(key: string, filePath: string): Promise<void> {
111111
});
112112
}
113113

114-
function uploadFileToOSS(key: string, filePath: string): Promise<void> {
114+
function uploadFileToOSS(key: string, filePath: string, logger: Logger): Promise<void> {
115115
logger.info('try uploadFileToOSS', { key });
116116
const ossStream = ALYOSSStream(
117117
new ALY.OSS({
@@ -151,7 +151,7 @@ function getUploadTokenQiniu(mac: qiniu.auth.digest.Mac, bucket: string, key: st
151151
return putPolicy.uploadToken(mac);
152152
}
153153

154-
function uploadFileToQiniu(key: string, filePath: string): Promise<void> {
154+
function uploadFileToQiniu(key: string, filePath: string, logger: Logger): Promise<void> {
155155
return new Promise((resolve, reject) => {
156156
logger.info('try uploadFileToQiniu', { key });
157157
const accessKey = _.get(config, 'qiniu.accessKey');
@@ -202,7 +202,7 @@ function uploadFileToQiniu(key: string, filePath: string): Promise<void> {
202202
});
203203
}
204204

205-
function uploadFileToTencentCloud(key: string, filePath: string): Promise<void> {
205+
function uploadFileToTencentCloud(key: string, filePath: string, logger: Logger): Promise<void> {
206206
return new Promise((resolve, reject) => {
207207
logger.info('try uploadFileToTencentCloud', { key });
208208
const cosIn = new COS({
@@ -228,19 +228,19 @@ function uploadFileToTencentCloud(key: string, filePath: string): Promise<void>
228228
});
229229
}
230230

231-
export function uploadFileToStorage(key: string, filePath: string): Promise<void> {
232-
const storageType = _.get(config, 'common.storageType');
231+
export function uploadFileToStorage(key: string, filePath: string, logger: Logger): Promise<void> {
232+
const { storageType } = config.common;
233233
switch (storageType) {
234234
case 'local':
235-
return uploadFileToLocal(key, filePath);
235+
return uploadFileToLocal(key, filePath, logger);
236236
case 's3':
237-
return uploadFileToS3(key, filePath);
237+
return uploadFileToS3(key, filePath, logger);
238238
case 'oss':
239-
return uploadFileToOSS(key, filePath);
239+
return uploadFileToOSS(key, filePath, logger);
240240
case 'qiniu':
241-
return uploadFileToQiniu(key, filePath);
241+
return uploadFileToQiniu(key, filePath, logger);
242242
case 'tencentcloud':
243-
return uploadFileToTencentCloud(key, filePath);
243+
return uploadFileToTencentCloud(key, filePath, logger);
244244
default:
245245
throw new AppError(`${storageType} storageType does not support.`);
246246
}

0 commit comments

Comments
 (0)