@bratel/dgit 0.0.13 → 0.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,8 @@
1
- import { PackageInfo, GithubLinkInfo } from './type';
2
- export declare const GetPackageInfo: () => PackageInfo;
1
+ import type { GithubLinkInfo, PackageInfo } from './type';
2
+ export declare function GetPackageInfo(): PackageInfo;
3
3
  export declare const GITHUB_ADDRESS = "https://github.com/";
4
4
  export declare const isHttpsLink: (link: string) => boolean;
5
- export declare const ParseGithubHttpsLink: (httpsLink: string) => GithubLinkInfo;
5
+ export declare function ParseGithubHttpsLink(httpsLink: string): GithubLinkInfo;
6
6
  export declare const TextEllipsis: (text: string, maxLen: number) => string;
7
- export declare const MakeDirs: (dirs: string) => void;
8
- export declare const AddExtraRandomQs: (origin: string) => string;
7
+ export declare function MakeDirs(dirs: string): void;
8
+ export declare function AddExtraRandomQs(origin: string): string;
package/lib/cmd/utils.js CHANGED
@@ -1,16 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AddExtraRandomQs = exports.MakeDirs = exports.TextEllipsis = exports.ParseGithubHttpsLink = exports.isHttpsLink = exports.GITHUB_ADDRESS = exports.GetPackageInfo = void 0;
3
+ exports.TextEllipsis = exports.isHttpsLink = exports.GITHUB_ADDRESS = void 0;
4
+ exports.GetPackageInfo = GetPackageInfo;
5
+ exports.ParseGithubHttpsLink = ParseGithubHttpsLink;
6
+ exports.MakeDirs = MakeDirs;
7
+ exports.AddExtraRandomQs = AddExtraRandomQs;
4
8
  const tslib_1 = require("tslib");
5
- const fs_1 = tslib_1.__importDefault(require("fs"));
6
- const path_1 = tslib_1.__importDefault(require("path"));
7
- exports.GetPackageInfo = () => {
8
- const buffer = fs_1.default.readFileSync(path_1.default.resolve(__dirname, '../../package.json'));
9
+ const node_fs_1 = tslib_1.__importDefault(require("node:fs"));
10
+ const node_path_1 = tslib_1.__importDefault(require("node:path"));
11
+ function GetPackageInfo() {
12
+ const buffer = node_fs_1.default.readFileSync(node_path_1.default.resolve(__dirname, '../../package.json'));
9
13
  return JSON.parse(buffer.toString());
10
- };
14
+ }
11
15
  exports.GITHUB_ADDRESS = 'https://github.com/';
12
- exports.isHttpsLink = (link) => link.trim().startsWith(exports.GITHUB_ADDRESS);
13
- exports.ParseGithubHttpsLink = (httpsLink) => {
16
+ const isHttpsLink = (link) => link.trim().startsWith(exports.GITHUB_ADDRESS);
17
+ exports.isHttpsLink = isHttpsLink;
18
+ function ParseGithubHttpsLink(httpsLink) {
14
19
  let nextLink = httpsLink.trim().slice(exports.GITHUB_ADDRESS.length);
15
20
  let index = nextLink.indexOf('/');
16
21
  if (index === -1)
@@ -62,26 +67,27 @@ exports.ParseGithubHttpsLink = (httpsLink) => {
62
67
  relativePath,
63
68
  type,
64
69
  };
65
- };
66
- exports.TextEllipsis = (text, maxLen) => (text.length >= maxLen ? `${text.slice(0, maxLen)}...` : text);
67
- exports.MakeDirs = (dirs) => {
70
+ }
71
+ const TextEllipsis = (text, maxLen) => (text.length >= maxLen ? `${text.slice(0, maxLen)}...` : text);
72
+ exports.TextEllipsis = TextEllipsis;
73
+ function MakeDirs(dirs) {
68
74
  const mkdirs = (dir, callback) => {
69
- if (fs_1.default.existsSync(dir)) {
75
+ if (node_fs_1.default.existsSync(dir)) {
70
76
  callback && callback();
71
77
  return;
72
78
  }
73
- mkdirs(path_1.default.dirname(dir), () => {
74
- fs_1.default.mkdirSync(dir);
79
+ mkdirs(node_path_1.default.dirname(dir), () => {
80
+ node_fs_1.default.mkdirSync(dir);
75
81
  callback && callback();
76
82
  });
77
83
  };
78
- if (fs_1.default.existsSync(dirs))
84
+ if (node_fs_1.default.existsSync(dirs))
79
85
  return;
80
86
  mkdirs(dirs);
81
- };
82
- exports.AddExtraRandomQs = (origin) => {
83
- if (origin.indexOf('?') !== -1) {
87
+ }
88
+ function AddExtraRandomQs(origin) {
89
+ if (origin.includes('?')) {
84
90
  return `${origin}&_t=${Math.random()}`;
85
91
  }
86
92
  return `${origin}?_t=${Math.random()}`;
87
- };
93
+ }
package/lib/dgit.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { DgitGlobalOption, RepoOptionType, DgitLifeCycle, DgitLoadGitTree } from './type';
2
- declare const dgit: (repoOption: RepoOptionType, dPath: string, dgitOptions?: DgitGlobalOption | undefined, hooks?: (DgitLifeCycle & DgitLoadGitTree) | undefined) => Promise<void>;
1
+ import type { DgitGlobalOption, DgitLifeCycle, DgitLoadGitTree, RepoOptionType } from './type';
2
+ declare function dgit(repoOption: RepoOptionType, dPath: string, dgitOptions?: DgitGlobalOption, hooks?: DgitLifeCycle & DgitLoadGitTree): Promise<void>;
3
3
  export default dgit;
package/lib/dgit.js CHANGED
@@ -1,179 +1,184 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
- const fs_1 = tslib_1.__importDefault(require("fs"));
5
- const path_1 = tslib_1.__importDefault(require("path"));
4
+ const node_fs_1 = tslib_1.__importDefault(require("node:fs"));
5
+ const node_path_1 = tslib_1.__importDefault(require("node:path"));
6
+ const node_process_1 = tslib_1.__importDefault(require("node:process"));
6
7
  const async_1 = tslib_1.__importDefault(require("async"));
7
- const repo_1 = tslib_1.__importDefault(require("./repo"));
8
+ const utils_1 = require("./cmd/utils");
8
9
  const log_1 = require("./log");
10
+ const repo_1 = tslib_1.__importDefault(require("./repo"));
9
11
  const request_1 = require("./request");
10
- const utils_1 = require("./cmd/utils");
11
12
  const UserAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36';
12
13
  const DEFAULT_PARALLEL_LIMIT = 10;
13
14
  const MAX_PARALLEL_LIMIT = 100;
14
15
  const JSON_STRINGIFY_PADDING = 2;
15
- const dgit = (repoOption, dPath, dgitOptions, hooks) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
16
- const { username, password, token, githubLink, proxy = '', } = repoOption;
17
- let { owner, repoName, ref = 'master', relativePath = '.', } = repoOption;
18
- if (githubLink && utils_1.isHttpsLink(githubLink)) {
19
- const parseResult = utils_1.ParseGithubHttpsLink(githubLink);
20
- owner = parseResult.owner;
21
- repoName = parseResult.repoName;
22
- ref = parseResult.ref;
23
- relativePath = parseResult.relativePath;
24
- }
25
- if (!owner || !repoName) {
26
- throw new Error('invalid repo option.');
27
- }
28
- const logger = log_1.createLogger(dgitOptions);
29
- const { exclude = [], include = [] } = dgitOptions || {};
30
- let { parallelLimit = DEFAULT_PARALLEL_LIMIT } = dgitOptions || {};
31
- if (!parallelLimit || parallelLimit <= 0) {
32
- logger('parallelLimit value is invalid.');
33
- parallelLimit = DEFAULT_PARALLEL_LIMIT;
34
- }
35
- parallelLimit > MAX_PARALLEL_LIMIT && (parallelLimit = MAX_PARALLEL_LIMIT);
36
- const { onSuccess, onError, onProgress, onFinish, onRetry, onResolved, beforeLoadTree, afterLoadTree, } = hooks || {};
37
- let onSuccessResolve = () => { };
38
- let onErrorReject = () => { };
39
- const prom = new Promise((resolve, reject) => {
40
- onSuccessResolve = resolve;
41
- onErrorReject = reject;
42
- });
43
- const { getRepoTreeUrl, getDownloadUrl } = repo_1.default(owner, repoName, ref, proxy);
44
- const url = getRepoTreeUrl();
45
- const headers = {
46
- 'User-Agent': UserAgent,
47
- Authorization: token ? `token ${token}` : undefined,
48
- };
49
- const auth = username && password ?
50
- {
51
- user: username,
52
- pass: password,
53
- sendImmediately: true,
54
- } :
55
- undefined;
56
- const options = {
57
- url, headers, auth,
58
- };
59
- const destPath = path_1.default.isAbsolute(dPath) ? dPath : path_1.default.resolve(process.cwd(), dPath);
60
- logger(' request repo tree options.');
61
- logger(JSON.stringify(options, null, JSON_STRINGIFY_PADDING));
62
- try {
63
- logger(' loading remote repo tree...');
64
- beforeLoadTree && beforeLoadTree();
65
- const body = yield request_1.requestGetPromise(options, dgitOptions || {}, {
66
- onRetry() {
67
- logger(` request ${url} failed. Retrying...`);
68
- onRetry && onRetry();
69
- },
70
- });
71
- logger(' loading remote repo tree succeed.');
72
- afterLoadTree && afterLoadTree();
73
- const result = JSON.parse(body);
74
- if (!result.tree || result.tree.length <= 0) {
75
- throw new Error('404 repo not found!');
16
+ function dgit(repoOption, dPath, dgitOptions, hooks) {
17
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
18
+ const { username, password, token, githubLink, proxy = '', } = repoOption;
19
+ let { owner, repoName, ref = 'master', relativePath = '.', } = repoOption;
20
+ if (githubLink && (0, utils_1.isHttpsLink)(githubLink)) {
21
+ const parseResult = (0, utils_1.ParseGithubHttpsLink)(githubLink);
22
+ owner = parseResult.owner;
23
+ repoName = parseResult.repoName;
24
+ ref = parseResult.ref;
25
+ relativePath = parseResult.relativePath;
26
+ }
27
+ if (!owner || !repoName) {
28
+ throw new Error('invalid repo option.');
29
+ }
30
+ const logger = (0, log_1.createLogger)(dgitOptions);
31
+ const { exclude = [], include = [] } = dgitOptions || {};
32
+ let { parallelLimit = DEFAULT_PARALLEL_LIMIT } = dgitOptions || {};
33
+ if (!parallelLimit || parallelLimit <= 0) {
34
+ logger('parallelLimit value is invalid.');
35
+ parallelLimit = DEFAULT_PARALLEL_LIMIT;
76
36
  }
77
- const treeNodeList = result.tree;
78
- const includeTreeNodeList = treeNodeList.filter(node => {
79
- const nPath = path_1.default.resolve(__dirname, node.path);
80
- const rPath = path_1.default.resolve(__dirname, relativePath);
81
- if (!nPath.startsWith(rPath) || node.type !== 'blob') {
82
- return false;
37
+ parallelLimit > MAX_PARALLEL_LIMIT && (parallelLimit = MAX_PARALLEL_LIMIT);
38
+ const { onSuccess, onError, onProgress, onFinish, onRetry, onResolved, beforeLoadTree, afterLoadTree, } = hooks || {};
39
+ let onSuccessResolve = () => { };
40
+ let onErrorReject = () => { };
41
+ const prom = new Promise((resolve, reject) => {
42
+ onSuccessResolve = resolve;
43
+ onErrorReject = reject;
44
+ });
45
+ const { getRepoTreeUrl, getDownloadUrl } = (0, repo_1.default)(owner, repoName, ref, proxy);
46
+ const url = getRepoTreeUrl();
47
+ const headers = {
48
+ 'User-Agent': UserAgent,
49
+ 'Authorization': token ? `token ${token}` : undefined,
50
+ };
51
+ const auth = username && password
52
+ ? {
53
+ user: username,
54
+ pass: password,
55
+ sendImmediately: true,
83
56
  }
84
- if (exclude.some(v => nPath.startsWith(path_1.default.resolve(rPath, v))) &&
85
- include.every(v => !nPath.startsWith(path_1.default.resolve(rPath, v)))) {
86
- return false;
57
+ : undefined;
58
+ const options = {
59
+ url,
60
+ headers,
61
+ auth,
62
+ };
63
+ const destPath = node_path_1.default.isAbsolute(dPath) ? dPath : node_path_1.default.resolve(node_process_1.default.cwd(), dPath);
64
+ logger(' request repo tree options.');
65
+ logger(JSON.stringify(options, null, JSON_STRINGIFY_PADDING));
66
+ try {
67
+ logger(' loading remote repo tree...');
68
+ beforeLoadTree && beforeLoadTree();
69
+ const body = yield (0, request_1.requestGetPromise)(options, dgitOptions || {}, {
70
+ onRetry() {
71
+ logger(` request ${url} failed. Retrying...`);
72
+ onRetry && onRetry();
73
+ },
74
+ });
75
+ logger(' loading remote repo tree succeed.');
76
+ afterLoadTree && afterLoadTree();
77
+ const result = JSON.parse(body);
78
+ if (!result.tree || result.tree.length <= 0) {
79
+ throw new Error('404 repo not found!');
87
80
  }
88
- return true;
89
- });
90
- if (includeTreeNodeList.length <= 0) {
91
- throw new Error(`404 repo ${relativePath} not found!`);
92
- }
93
- const totalStatus = includeTreeNodeList.reduce((prev, cur) => {
94
- if (cur.type === 'blob') {
95
- prev.size += cur.size;
96
- prev.count++;
81
+ const treeNodeList = result.tree;
82
+ const includeTreeNodeList = treeNodeList.filter((node) => {
83
+ const nPath = node_path_1.default.resolve(__dirname, node.path);
84
+ const rPath = node_path_1.default.resolve(__dirname, relativePath);
85
+ if (!nPath.startsWith(rPath) || node.type !== 'blob') {
86
+ return false;
87
+ }
88
+ if (exclude.some(v => nPath.startsWith(node_path_1.default.resolve(rPath, v)))
89
+ && include.every(v => !nPath.startsWith(node_path_1.default.resolve(rPath, v)))) {
90
+ return false;
91
+ }
92
+ return true;
93
+ });
94
+ if (includeTreeNodeList.length <= 0) {
95
+ throw new Error(`404 repo ${relativePath} not found!`);
97
96
  }
98
- return prev;
99
- }, { size: 0, count: 0 });
100
- let currentSize = 0;
101
- let currentCount = 0;
102
- onResolved &&
103
- onResolved({
97
+ const totalStatus = includeTreeNodeList.reduce((prev, cur) => {
98
+ if (cur.type === 'blob') {
99
+ prev.size += cur.size;
100
+ prev.count++;
101
+ }
102
+ return prev;
103
+ }, { size: 0, count: 0 });
104
+ let currentSize = 0;
105
+ let currentCount = 0;
106
+ onResolved
107
+ && onResolved({
108
+ currentSize,
109
+ currentCount,
110
+ totalSize: totalStatus.size,
111
+ totalCount: totalStatus.count,
112
+ });
113
+ logger(' include files resolved.');
114
+ logger('', JSON.stringify({
104
115
  currentSize,
105
116
  currentCount,
106
117
  totalSize: totalStatus.size,
107
118
  totalCount: totalStatus.count,
108
- });
109
- logger(' include files resolved.');
110
- logger('', JSON.stringify({
111
- currentSize,
112
- currentCount,
113
- totalSize: totalStatus.size,
114
- totalCount: totalStatus.count,
115
- }));
116
- async_1.default.eachLimit(includeTreeNodeList, parallelLimit, (node, callback) => {
117
- const downloadUrl = getDownloadUrl(node.path);
118
- const rPath = path_1.default.resolve(destPath, relativePath);
119
- const tPath = path_1.default.resolve(destPath, node.path);
120
- const root = path_1.default.resolve(destPath, '.');
121
- let targetPath;
122
- if (rPath === tPath) {
123
- targetPath = path_1.default.resolve(destPath, path_1.default.basename(tPath));
124
- }
125
- else {
126
- targetPath = tPath.replace(rPath, root);
127
- }
128
- logger('', node.path, relativePath, targetPath);
129
- if (!fs_1.default.existsSync(path_1.default.dirname(targetPath))) {
130
- utils_1.MakeDirs(path_1.default.dirname(targetPath));
131
- }
132
- const ws = fs_1.default.createWriteStream(targetPath);
133
- logger(` downloading from ${downloadUrl}...`);
134
- request_1.requestOnStream(downloadUrl, ws, dgitOptions || {}, {
135
- onSuccess() {
136
- currentCount++;
137
- currentSize += node.size;
138
- logger(` write file ${node.path} succeed.
119
+ }));
120
+ async_1.default.eachLimit(includeTreeNodeList, parallelLimit, (node, callback) => {
121
+ const downloadUrl = getDownloadUrl(node.path);
122
+ const rPath = node_path_1.default.resolve(destPath, relativePath);
123
+ const tPath = node_path_1.default.resolve(destPath, node.path);
124
+ const root = node_path_1.default.resolve(destPath, '.');
125
+ let targetPath;
126
+ if (rPath === tPath) {
127
+ targetPath = node_path_1.default.resolve(destPath, node_path_1.default.basename(tPath));
128
+ }
129
+ else {
130
+ targetPath = tPath.replace(rPath, root);
131
+ }
132
+ logger('', node.path, relativePath, targetPath);
133
+ if (!node_fs_1.default.existsSync(node_path_1.default.dirname(targetPath))) {
134
+ (0, utils_1.MakeDirs)(node_path_1.default.dirname(targetPath));
135
+ }
136
+ const ws = node_fs_1.default.createWriteStream(targetPath);
137
+ logger(` downloading from ${downloadUrl}...`);
138
+ (0, request_1.requestOnStream)(downloadUrl, ws, dgitOptions || {}, {
139
+ onSuccess() {
140
+ currentCount++;
141
+ currentSize += node.size;
142
+ logger(` write file ${node.path} succeed.
139
143
  size: [${currentSize}/${totalStatus.size}],
140
144
  count: [${currentCount}/${totalStatus.count}]`);
141
- onProgress &&
142
- onProgress({
143
- totalCount: totalStatus.count,
144
- totalSize: totalStatus.size,
145
- currentSize,
146
- currentCount,
147
- }, node);
148
- callback();
149
- },
150
- onError(err) {
151
- logger('', err);
152
- callback(new Error(` request ${downloadUrl} failed.`));
153
- },
154
- onRetry() {
155
- logger(` request ${downloadUrl} failed. Retrying...`);
156
- onRetry && onRetry();
157
- },
145
+ onProgress
146
+ && onProgress({
147
+ totalCount: totalStatus.count,
148
+ totalSize: totalStatus.size,
149
+ currentSize,
150
+ currentCount,
151
+ }, node);
152
+ callback();
153
+ },
154
+ onError(err) {
155
+ logger('', err);
156
+ callback(new Error(` request ${downloadUrl} failed.`));
157
+ },
158
+ onRetry() {
159
+ logger(` request ${downloadUrl} failed. Retrying...`);
160
+ onRetry && onRetry();
161
+ },
162
+ });
163
+ }, (err) => {
164
+ if (err) {
165
+ onError && onError(err);
166
+ onFinish && onFinish();
167
+ onErrorReject(err);
168
+ }
169
+ else {
170
+ onSuccess && onSuccess();
171
+ onFinish && onFinish();
172
+ onSuccessResolve();
173
+ }
158
174
  });
159
- }, err => {
160
- if (err) {
161
- onError && onError(err);
162
- onFinish && onFinish();
163
- onErrorReject(err);
164
- }
165
- else {
166
- onSuccess && onSuccess();
167
- onFinish && onFinish();
168
- onSuccessResolve();
169
- }
170
- });
171
- }
172
- catch (error) {
173
- onError && onError(error);
174
- onFinish && onFinish();
175
- onErrorReject(error);
176
- }
177
- return prom;
178
- });
175
+ }
176
+ catch (error) {
177
+ onError && onError(error);
178
+ onFinish && onFinish();
179
+ onErrorReject(error);
180
+ }
181
+ return prom;
182
+ });
183
+ }
179
184
  exports.default = dgit;
package/lib/log.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { DgitGlobalOption } from './type';
2
- export declare const createLogger: (option?: DgitGlobalOption | undefined) => (...message: any[]) => void;
1
+ import type { DgitGlobalOption } from './type';
2
+ export declare function createLogger(option?: DgitGlobalOption): (...message: any[]) => void;
package/lib/log.js CHANGED
@@ -1,12 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createLogger = void 0;
3
+ exports.createLogger = createLogger;
4
4
  const DEFAULT_PREFIX = '[dgit-logger]';
5
- exports.createLogger = (option) => (...message) => {
6
- if (option && option.log) {
7
- const prefix = option ?
8
- option.logPrefix || DEFAULT_PREFIX :
9
- DEFAULT_PREFIX;
10
- console.log(prefix, ...message, '\n');
11
- }
12
- };
5
+ function createLogger(option) {
6
+ return (...message) => {
7
+ if (option && option.log) {
8
+ const prefix = option
9
+ ? option.logPrefix || DEFAULT_PREFIX
10
+ : DEFAULT_PREFIX;
11
+ console.log(prefix, ...message, '\n');
12
+ }
13
+ };
14
+ }
package/lib/repo.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- declare const repoUtils: (owner: string, repoName: string, ref: string, proxy: string) => {
1
+ declare function repoUtils(owner: string, repoName: string, ref: string, proxy: string): {
2
2
  getRepoTreeUrl: () => string;
3
3
  getDownloadUrl: (path: string) => string;
4
4
  };
package/lib/repo.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const repoUtils = (owner, repoName, ref, proxy) => ({
4
- getRepoTreeUrl: () => `https://api.github.com/repos/${owner}/${repoName}/git/trees/${ref}?recursive=1`,
5
- getDownloadUrl: (path) => `${proxy ? `${proxy}/` : ''}https://raw.githubusercontent.com/${owner}/${repoName}/${ref}/${path}`,
6
- });
3
+ function repoUtils(owner, repoName, ref, proxy) {
4
+ return {
5
+ getRepoTreeUrl: () => `https://api.github.com/repos/${owner}/${repoName}/git/trees/${ref}?recursive=1`,
6
+ getDownloadUrl: (path) => `${proxy ? `${proxy}/` : ''}https://raw.githubusercontent.com/${owner}/${repoName}/${ref}/${path}`,
7
+ };
8
+ }
7
9
  exports.default = repoUtils;
package/lib/request.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import fs from 'fs';
2
- import { UrlOptions, CoreOptions } from 'request';
3
- import { DgitGlobalOption, DgitLifeCycle } from './type';
4
- declare type RequestOption = UrlOptions & CoreOptions;
5
- export declare const requestGetPromise: (options: RequestOption, dgitOptions: DgitGlobalOption, hooks?: DgitLifeCycle | undefined) => Promise<any>;
6
- export declare const requestOnStream: (url: string, ws: fs.WriteStream, dgitOptions: DgitGlobalOption, hooks?: DgitLifeCycle | undefined) => void;
1
+ import type fs from 'node:fs';
2
+ import type { CoreOptions, UrlOptions } from 'request';
3
+ import type { DgitGlobalOption, DgitLifeCycle } from './type';
4
+ type RequestOption = UrlOptions & CoreOptions;
5
+ export declare function requestGetPromise(options: RequestOption, dgitOptions: DgitGlobalOption, hooks?: DgitLifeCycle): Promise<any>;
6
+ export declare function requestOnStream(url: string, ws: fs.WriteStream, dgitOptions: DgitGlobalOption, hooks?: DgitLifeCycle): void;
7
7
  export {};
package/lib/request.js CHANGED
@@ -1,13 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.requestOnStream = exports.requestGetPromise = void 0;
3
+ exports.requestGetPromise = requestGetPromise;
4
+ exports.requestOnStream = requestOnStream;
4
5
  const tslib_1 = require("tslib");
5
6
  const request_1 = tslib_1.__importDefault(require("request"));
6
7
  const utils_1 = require("./cmd/utils");
7
8
  const log_1 = require("./log");
8
9
  const REQUEST_RETRY_DELAY = 1500;
9
10
  const DEFAULT_MAX_RETRY_COUNT = 5;
10
- const requestGet = (options, maxRetryCount, hooks) => {
11
+ function requestGet(options, maxRetryCount, hooks) {
11
12
  const { onSuccess, onError, onFinish, onRetry, } = hooks || {};
12
13
  request_1.default.get(options, (err, _, body) => {
13
14
  if (err) {
@@ -25,33 +26,35 @@ const requestGet = (options, maxRetryCount, hooks) => {
25
26
  onSuccess && onSuccess(body);
26
27
  onFinish && onFinish();
27
28
  });
28
- };
29
- exports.requestGetPromise = (options, dgitOptions, hooks) => new Promise((resolve, reject) => {
30
- const { maxRetryCount = DEFAULT_MAX_RETRY_COUNT } = dgitOptions;
31
- const { onSuccess, onError, onFinish, onRetry, } = hooks || {};
32
- const newHooks = {
33
- onSuccess(data) {
34
- resolve(data);
35
- onSuccess && onSuccess(data);
36
- },
37
- onError(err) {
38
- reject(err);
39
- onError && onError(err);
40
- },
41
- onFinish,
42
- onRetry,
43
- };
44
- requestGet(options, maxRetryCount, newHooks);
45
- });
46
- exports.requestOnStream = (url, ws, dgitOptions, hooks) => {
29
+ }
30
+ function requestGetPromise(options, dgitOptions, hooks) {
31
+ return new Promise((resolve, reject) => {
32
+ const { maxRetryCount = DEFAULT_MAX_RETRY_COUNT } = dgitOptions;
33
+ const { onSuccess, onError, onFinish, onRetry, } = hooks || {};
34
+ const newHooks = {
35
+ onSuccess(data) {
36
+ resolve(data);
37
+ onSuccess && onSuccess(data);
38
+ },
39
+ onError(err) {
40
+ reject(err);
41
+ onError && onError(err);
42
+ },
43
+ onFinish,
44
+ onRetry,
45
+ };
46
+ requestGet(options, maxRetryCount, newHooks);
47
+ });
48
+ }
49
+ function requestOnStream(url, ws, dgitOptions, hooks) {
47
50
  const { maxRetryCount = DEFAULT_MAX_RETRY_COUNT } = dgitOptions;
48
- const logger = log_1.createLogger(dgitOptions);
51
+ const logger = (0, log_1.createLogger)(dgitOptions);
49
52
  const { onSuccess, onError, onFinish, onRetry, } = hooks || {};
50
53
  const fn = (retryCount) => {
51
- const downloadUrl = utils_1.AddExtraRandomQs(url);
54
+ const downloadUrl = (0, utils_1.AddExtraRandomQs)(url);
52
55
  logger(` dowloading from ${downloadUrl}...`);
53
- request_1.default(encodeURI(downloadUrl))
54
- .on('error', err => {
56
+ (0, request_1.default)(encodeURI(downloadUrl))
57
+ .on('error', (err) => {
55
58
  if (retryCount <= 0) {
56
59
  onError && onError(err);
57
60
  onFinish && onFinish();
@@ -72,4 +75,4 @@ exports.requestOnStream = (url, ws, dgitOptions, hooks) => {
72
75
  logger(` ${url}, write stream failed.`);
73
76
  });
74
77
  fn(maxRetryCount);
75
- };
78
+ }
package/lib/type.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare type GithubRelativePath = string;
1
+ export type GithubRelativePath = string;
2
2
  export interface DgitGlobalOption {
3
3
  maxRetryCount?: number;
4
4
  parallelLimit?: number;