Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit 4cdd4d9

Browse files
author
Shuai Wang
authored
Replace node-fetch with axios to support using proxy (#1200)
* replace node-fetch with axios * fix indent * fix type annotations and indentations * fix imports and lint * remove a console statement * fix type checking * fix type and return type * retrigger * fix as any * updaye pnpm-lock * retrigger
1 parent 58c9630 commit 4cdd4d9

File tree

18 files changed

+264
-105
lines changed

18 files changed

+264
-105
lines changed

common/config/rush/pnpm-lock.yaml

Lines changed: 19 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/chatdown/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"latest-version": "^4.0.0",
1919
"mime-types": "^2.1.18",
2020
"minimist": "^1.2.0",
21-
"node-fetch": "~2.6.0",
21+
"axios":"~0.21.1",
22+
"https-proxy-agent": "^5.0.0",
2223
"please-upgrade-node": "^3.0.1",
2324
"semver": "^5.5.1",
2425
"tslib": "^2.0.3",
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License.
4+
*/
5+
6+
const url = require('url')
7+
const httpsProxyAgent = require('https-proxy-agent')
8+
9+
const httpsProxy = function(config) {
10+
const parsed = url.parse(config.url)
11+
const protocol = parsed.protocol
12+
if (protocol !== 'https:') {
13+
return config
14+
}
15+
16+
const envProxy = process.env['HTTPS_PROXY'] || process.env['https_proxy']
17+
if (envProxy) {
18+
const parsed = url.parse(envProxy)
19+
const proxyOpt =
20+
{
21+
hostname: parsed.hostname,
22+
port: parsed.port
23+
}
24+
25+
if (parsed.auth) {
26+
proxyOpt.auth = parsed.auth
27+
}
28+
29+
config.httpsAgent = httpsProxyAgent(proxyOpt)
30+
//Disable direct proxy
31+
config.proxy = false
32+
}
33+
34+
return config
35+
}
36+
37+
module.exports = httpsProxy

packages/chatdown/utils/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ const ChannelAccount = require('./serializable/channelAccount');
1717
const ConversationAccount = require('./serializable/conversationAccount');
1818
const Attachment = require('./serializable/attachment');
1919
const chalk = require('chalk');
20-
const request = require('node-fetch');
20+
const axios = require('axios');
21+
const httpsProxy = require('./httpsProxy');
2122
const NEWLINE = require('os').EOL;
2223
let activityId = 1;
24+
axios.interceptors.request.use(httpsProxy);
2325

2426
// Matches [someActivityOrInstruction=value]
2527
const commandRegExp = /(?:\[)([\s\S]*?)(?:])/i;
@@ -482,8 +484,8 @@ async function addAttachment(activity, arg) {
482484
contentType = mime.lookup(contentUrl) || cardContentTypes[path.extname(contentUrl)];
483485

484486
if (!contentType && contentUrl && contentUrl.indexOf('http') == 0) {
485-
let response = await request(contentUrl, {method: 'HEAD'});
486-
contentType = response.headers.get('content-type').split(';')[0];
487+
let response = await axios.get(contentUrl, {method: 'HEAD'});
488+
contentType = response.headers['content-type'].split(';')[0];
487489
}
488490
}
489491

packages/lg/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"tslib": "^2.0.3",
1818
"read-text-file": "^1.1.0",
1919
"readline-sync": "^1.4.10",
20-
"node-fetch": "^2.1.2"
20+
"axios":"~0.21.1",
21+
"https-proxy-agent": "^5.0.0"
2122
},
2223
"devDependencies": {
2324
"@oclif/dev-cli": "^1.22.2",

packages/lg/src/utils/helper.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import * as path from 'path'
1010
import * as fs from 'fs-extra'
1111
import {CLIError} from '@microsoft/bf-cli-command'
12+
import {httpsProxy} from './https-proxy'
1213
// eslint-disable-next-line node/no-extraneous-require
13-
const fetch = require('node-fetch')
14+
const axios = require('axios')
15+
axios.interceptors.request.use(httpsProxy)
1416
const NEWLINE = require('os').EOL
1517
const ANY_NEWLINE = /\r\n|\r|\n/g
1618

@@ -118,21 +120,20 @@ export class Helper {
118120
let tUri = `${baseUri}?api-version=3.0&to=${translateOption.to_lang}&includeAlignment=true`
119121
if (translateOption.src_lang) tUri += `&from=${translateOption.src_lang}`
120122
const options = {
121-
method: 'POST',
122-
body: JSON.stringify(payload),
123123
headers: {
124124
'Content-Type': 'application/json',
125125
'Ocp-Apim-Subscription-Key': translateOption.subscriptionKey,
126126
'Ocp-Apim-Subscription-Region': translateOption.region,
127127
'X-ClientTraceId': Helper.get_guid(),
128128
},
129129
}
130-
const res = await fetch(tUri, options)
131-
if (!res.ok) {
130+
131+
const res = await axios.post(tUri, payload, options)
132+
if (res.status !== 200) {
132133
throw (new CLIError('Text translator service call failed with [' + res.status + '] : ' + res.statusText + '.\nPlease check key & language code validity'))
133134
}
134135

135-
const data = await res.json()
136+
const data = res.data
136137
return data
137138
}
138139

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License.
4+
*/
5+
6+
import httpsProxyAgent from 'https-proxy-agent'
7+
import {AxiosRequestConfig} from 'axios'
8+
import {Url, parse} from 'url'
9+
10+
export function httpsProxy(config: AxiosRequestConfig) {
11+
const parsed: Url = parse(config.url || '')
12+
const protocol = parsed.protocol
13+
if (protocol !== 'https:') {
14+
return config
15+
}
16+
17+
const envProxy = process.env.HTTPS_PROXY || process.env.https_proxy
18+
if (envProxy) {
19+
const parsed: Url = parse(envProxy)
20+
const proxyOpt: Record<string, string | undefined> =
21+
{
22+
hostname: parsed.hostname,
23+
port: parsed.port,
24+
}
25+
26+
if (parsed.auth) {
27+
proxyOpt.auth = parsed.auth
28+
}
29+
30+
config.httpsAgent = httpsProxyAgent(proxyOpt)
31+
// Disable direct proxy
32+
config.proxy = false
33+
}
34+
35+
return config
36+
}

packages/lu/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
"globby": "^10.0.1",
4747
"intercept-stdout": "^0.1.2",
4848
"lodash": "^4.17.19",
49-
"node-fetch": "~2.6.0",
5049
"semver": "^5.5.1",
5150
"tslib": "^2.0.3"
5251
},

packages/lu/src/parser/lu/luMerger.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ const parserObject = require('./../lufile/classes/parserObject');
1616
const txtfile = require('./../lufile/read-text-file');
1717
const BuildDiagnostic = require('./../lufile/diagnostic').BuildDiagnostic;
1818
const LUISObjNameEnum = require('./../utils/enums/luisobjenum');
19-
const fetch = require('node-fetch');
19+
const axios = require('axios');
20+
const httpsProxy = require('../utils/httpsProxy')
21+
axios.interceptors.request.use(httpsProxy)
2022

2123
module.exports = {
2224
/**
@@ -455,7 +457,7 @@ const resolveLuUriContent = async function(srcId, toResolve, luObjects) {
455457
if (uri !== undefined) {
456458
let response;
457459
try {
458-
response = await fetch(uri, { method: 'GET' });
460+
response = await axios.get(uri);
459461
} catch (err) {
460462
// throw, invalid URI
461463
let errorMsg = `URI: "${uri}" appears to be invalid. Please double check the URI or re-try this parse when you are connected to the internet.`;
@@ -465,7 +467,7 @@ const resolveLuUriContent = async function(srcId, toResolve, luObjects) {
465467

466468
throw (new exception(retCode.errorCode.INVALID_URI, error.toString(), [error]));
467469
}
468-
var res = await response.buffer();
470+
var res = await response.data;
469471
var encodedRes = helpers.fixBuffer(res);
470472
luObjects.push(new luObject(encodedRes, new luOptions(toResolve.filePath, toResolve.includeInCollate)));
471473
}

0 commit comments

Comments
 (0)