[英]Error package vscode extension with vsce: invalid relative path
[英]Ignoring axios error for invalid certificates when creating a vscode extension
我目前正在开发一个发出 HTTPS GET 请求的 Visual Studio Code 扩展,并且我试图忽略无效证书,例如过期证书。
这是打字稿中一个非常简单的脚本,它可以工作:
import * as https from "https";
import axios from "axios";
try {
const test = async () => {
await axios.get(
'https://expired.badssl.com/',
{
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
}
);
};
test();
} catch (e) {
console.log(e);
}
正如预期的那样,当我编译并运行文件时,它什么也不返回,如果我将rejectUnauthorized
更改为true
,它会记录一个过期的证书错误。
但是,当我使用yo code
创建https://code.visualstudio.com/api/get-started/your-first-extension中所述的简单打字稿扩展并发出类似的 axios 请求时,它会给出证书已过期错误不管给拒绝未授权的参数rejectUnauthorized
使用yo code
模板制作的与上面类似的打字稿代码:
import * as vscode from 'vscode';
import * as https from "https";
import axios from "axios";
export async function activate(context: vscode.ExtensionContext) {
let disposable = vscode.commands.registerCommand('nameOfExtension.helloWorld', async () => {
try {
await axios.get(
'https://expired.badssl.com/',
{
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
}
);
} catch (e) {
console.log(e);
}
});
context.subscriptions.push(disposable);
}
export function deactivate() {}
(其中nameOfExtension
是package.json
中的扩展名)
扩展的工作原理:用户打开命令面板(mac 上的 cmd+P)并运行运行 axios 请求的“hello world”命令。 如果它有效,则不会发生任何事情,否则将错误消息打印到控制台。
我发现可以让扩展忽略证书的一种方法是添加代码行https.globalAgent.options.rejectUnauthorized = false
,它将 globalAgent 设置为始终为 false。
但是全局设置这不是我想做的事情,我希望rejectUnauthorized
为单个实例工作。
我想知道是否有人知道为什么示例中显示的rejectUnauthorized
方法在 vscode 上不起作用?
以下是我的想法:
附加信息:
"devDependencies": {
"@types/vscode": "^1.61.0",
"@types/glob": "^7.1.4",
"@types/mocha": "^9.0.0",
"@types/node": "14.x",
"@typescript-eslint/eslint-plugin": "^4.31.1",
"@typescript-eslint/parser": "^4.31.1",
"eslint": "^7.32.0",
"glob": "^7.1.7",
"mocha": "^9.1.1",
"typescript": "^4.4.3",
"@vscode/test-electron": "^1.6.2"
},
"dependencies": {
"axios": "^0.21.4",
"https": "^1.0.0"
}
您是否尝试过使用承诺?
否则,您可以像这样使用它:
const axios = require('axios');
return new Promise((resolve, reject) => {
axios
.get('https://expired.badssl.com/', {
responseType: 'arraybuffer',
responseEncoding: 'binary'
}).catch(e => e + 'Get reqeust failed.')
.then(response => {
const data = response.data
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.