Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const headers = Object.assign(JSON.parse(JSON.stringify(ctx.req.headers)), {
'Content-Type': 'application/json'
});
// 避免与url的host冲突,删除 host
delete headers.host;
if (headers['set-cookie']) {
headers.cookie = headers['set-cookie'][0];
delete headers.cookie;
}
console.log('[faas][server] action:', url, headers);
if (body && typeof body !== 'string') {
body = JSON.stringify(body);
}
return request(url, {
headers,
method: 'POST',
body,
}).then(function (res: RequestResponse) {
// 若有 set-cookie 则写入到 ctx.res
if (res.headers && res.headers['set-cookie'] && Array.isArray(res.headers['set-cookie'])) {
const cookies = [];
for (let cookie of res.headers['set-cookie']) {
// 在开发模式下修改 cookie 的 domain
if (ctx.isDev) {
if (ctx.req.headers.host) {
// 若有 host 信息,则修改为当前 host 的根域名
let host = ctx.req.headers.host!.split('.');
cookie = cookie.replace(/domain=[^;]+;/, `domain=${host[host.length - 2]}.${host[host.length - 1]};`);
} else {
// 没有 host 信息则直接删除 domain 配置
export default function (provider: any, params: any) {
params = Object.assign({
Nonce: Math.round(Math.random() * 65535),
Region: provider.region,
SecretId: provider.secretId,
SignatureMethod: 'HmacSHA256',
Timestamp: Math.round(Date.now() / 1000) - 1,
Version: '2018-04-16',
}, params);
params = mergeData(params);
const sign = 'POSTapigateway.api.qcloud.com/v2/index.php?' + formatSignString(params);
params.Signature = crypto.createHmac('sha256', provider.secretKey).update(sign).digest('base64');
return request('https://apigateway.api.qcloud.com/v2/index.php?', {
body: params,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
method: 'POST',
}).then(function (res) {
try {
res.body = JSON.parse(res.body);
} catch (error) {
return Promise.reject(error);
}
if (res.body.code) {
console.error(res.body);
return Promise.reject(Error(JSON.stringify(res.body)));
} else {
return res.body;
}
});
export default function action (this: Tencentcloud, params: any) {
params = Object.assign({
Nonce: Math.round(Math.random() * 65535),
Region: this.config.region,
SecretId: this.config.secretId,
SignatureMethod: 'HmacSHA256',
Timestamp: Math.round(Date.now() / 1000) - 1,
Version: '2018-04-16',
}, params);
params = mergeData(params);
const sign = 'POSTscf.tencentcloudapi.com/?' + formatSignString(params);
params.Signature = crypto.createHmac('sha256', this.config.secretKey).update(sign).digest('base64');
return request('https://scf.tencentcloudapi.com/?', {
body: params,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
method: 'POST',
}).then(function (res) {
if (res.body.Response.Error) {
console.error(res.body);
return Promise.reject(res.body.Response.Error);
} else {
return res.body.Response;
}
});
}
export function invokeCloudFunction (this: Tencentcloud, name: string, data?: any, options?: {
[key: string]: any;
}) {
this.logger.debug('invokeFunction: %s %o', name, options);
if (process.env.FaasMode === 'local' && process.env.FaasLocal) {
return request(process.env.FaasLocal + '/' + name, {
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data),
method: 'POST'
});
} else {
return scf.call(this, Object.assign({
Action: 'Invoke',
FunctionName: name.replace(/[^a-zA-Z0-9-_]/g, '_'),
ClientContext: JSON.stringify(data),
InvocationType: 'Event',
Namespace: process.env.FaasEnv,
Qualifier: process.env.FaasEnv
}, options || {})).then(function (res) {
if (res.Result.ErrMsg) {