Skip to content

Commit d684936

Browse files
authored
Merge pull request #39 from terminatorheart/feat/add_cookies
feat: 发起请求时透传用户环境cookie等信息
2 parents 80b73bf + 81eed0e commit d684936

5 files changed

Lines changed: 45 additions & 3 deletions

File tree

‎src/app-updator.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ export abstract class AppUpdator extends EventEmitter {
232232
progressHandle: (data: any) => {
233233
this.emit(EventType.UPDATE_DOWNLOAD_PROGRESS, { ...data, executeType });
234234
},
235+
headers: this.options?.headers,
235236
} as IDownloadFileOptions);
236237
this.logger.info('downloadUpdateFile:Downloaded');
237238
this.setState(StateType.Downloaded);

‎src/common/types/index.d.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export interface IAppUpdatorOptions {
5252
autoDownload?: boolean;
5353
productName?: string;
5454
getWindowsHelperExeDir?: () => string;
55+
headers?: Record<string, string>;
5556
}
5657

5758
interface IProgressHandleArg {
@@ -65,6 +66,7 @@ export interface IDownloadFileOptions {
6566
signature: string;
6667
targetDir: string;
6768
progressHandle: (progress: IProgressHandleArg) => void;
69+
headers?: Record<string, string>;
6870
}
6971

7072
export interface IAvailableUpdate {

‎src/utils/download-file.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { createWriteStream } from '.';
1212
* @param progressHandle 下载状态回调
1313
* @return
1414
*/
15-
export const downloadFile = async ({ logger, url, signature, targetDir, progressHandle }: IDownloadFileOptions) => {
15+
export const downloadFile = async ({ logger, url, signature, targetDir, progressHandle, headers }: IDownloadFileOptions) => {
1616
logger.info('downloadFile#downloadFile (start)');
1717
const writeStream = createWriteStream(targetDir);
1818
let currentLength = 0;
@@ -26,6 +26,7 @@ export const downloadFile = async ({ logger, url, signature, targetDir, progress
2626
streaming: true,
2727
followRedirect: true,
2828
timeout: 10 * 60 * 1000,
29+
headers,
2930
})
3031
.then((res: any) => {
3132
const totalLength = res.headers['content-length'];

‎src/utils/index.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export const existFile = async (path: string): Promise<boolean> => {
9292
};
9393

9494
export const requestUpdateInfo = async (options: IAppUpdatorOptions): Promise<IUpdateInfo> => {
95-
const { url } = options;
95+
const { url, headers } = options;
9696
if (!url) {
9797
throw new Error('request url can\'t be empty');
9898
}
@@ -102,6 +102,7 @@ export const requestUpdateInfo = async (options: IAppUpdatorOptions): Promise<IU
102102
res = await urllib.request(url, {
103103
dataType: 'json',
104104
timeout: 10 * 1000,
105+
headers,
105106
});
106107
} catch (e) {
107108
throw e;

‎test/utils/index.test.ts‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ let mockRequire = require('mock-require');
44

55
function generateMockUrllib() {
66
let response = [] as any;
7+
let lastRequestOptions = null as any;
78

89
mockRequire('urllib', {
9-
request: () => {
10+
request: (_url: string, options: any) => {
11+
lastRequestOptions = options;
1012
return response;
1113
},
1214
});
@@ -15,6 +17,7 @@ function generateMockUrllib() {
1517
setReturn: (array: any) => {
1618
response = array;
1719
},
20+
getLastRequestOptions: () => lastRequestOptions,
1821
};
1922
}
2023
const mockUrllib = generateMockUrllib();
@@ -93,5 +96,39 @@ describe('test/utils/index.test.ts', () => {
9396
const result = await requestUpdateInfo(updator);
9497
assert.equal(result.version, '7.2.2');
9598
});
99+
100+
it('应将 headers 透传给 urllib.request', async () => {
101+
const headers = { Cookie: 'session=abc123; token=xyz' };
102+
const updator = {
103+
url: 'https://github.com',
104+
headers,
105+
};
106+
mockUrllib.setReturn({
107+
status: 200,
108+
data: {
109+
isUpdate: true,
110+
version: '1.0.0',
111+
},
112+
});
113+
await requestUpdateInfo(updator);
114+
const requestOptions = mockUrllib.getLastRequestOptions();
115+
assert.deepEqual(requestOptions.headers, headers);
116+
});
117+
118+
it('未传 headers 时不影响请求', async () => {
119+
const updator = {
120+
url: 'https://github.com',
121+
};
122+
mockUrllib.setReturn({
123+
status: 200,
124+
data: {
125+
isUpdate: true,
126+
version: '1.0.0',
127+
},
128+
});
129+
await requestUpdateInfo(updator);
130+
const requestOptions = mockUrllib.getLastRequestOptions();
131+
assert.equal(requestOptions.headers, undefined);
132+
});
96133
});
97134
});

0 commit comments

Comments
 (0)