Compare commits

..

No commits in common. "master" and "fix/error-log" have entirely different histories.

14 changed files with 192 additions and 152 deletions

2
.gitignore vendored
View File

@ -5,3 +5,5 @@ node_modules
package-lock.json
.idea
.vscode
lib

View File

@ -1,7 +1,8 @@
node_modules
.s
tsconfig.json
LICENSE
package-lock.json
package-lok.json
.prettierrc.js
.gitignore
@ -14,4 +15,3 @@ package-lock.json
README.md
./example
./.signore
./src

View File

@ -23,9 +23,9 @@ services:
````
$ s invoke --invocation-type sync --event ${payload}
$ s invoke --invocation-type async --event-file ${path}
$ s invoke --event-stdin
$ s exec -- invoke --invocation-type sync --event ${payload}
$ s exec -- invoke --invocation-type async --event-file ${path}
$ s exec -- invoke --event-stdin
````
## CLI 用法

51
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@ -1,16 +1,11 @@
{
"body": 123,
"method": "POST",
"headers": {
"key": "value",
"Content-Type": "application/json"
"key": "value"
},
"queries": {
"key": "value"
},
"body": {
"abc": "body",
"tmpNasZipPath": "/tmpNasZipPath",
"body": "body"
},
"path": "download"
"path": "string"
}

View File

@ -1,6 +1,6 @@
{
"name": "fc-remote-invoke",
"version": "0.0.22",
"version": "0.0.17",
"description": "This is a component demo for Serverless Devs Tool ",
"keywords": [
"Serverless",
@ -13,7 +13,7 @@
"author": "Serverless-Devs",
"contributors": [],
"license": "MIT",
"main": "./dist/index.js",
"main": "./lib/index.js",
"publishConfig": {
"access": "public"
},
@ -24,17 +24,15 @@
"scripts": {
"start": "npm run watch",
"watch": "tsc -w",
"prebuild": "rm -rf node_modules && rm -rf package-lock.json && npm i && rimraf dist",
"esbuild": "esbuild src/index.ts --bundle --log-level=error --minify --platform=node --format=cjs --target=node10.4 --external:@serverless-devs/core --outfile=dist/index.js",
"build": "npm run esbuild"
"prebuild": "rm -rf node_modules && rm -rf package-lock.json && npm i && rimraf lib",
"build": "ncc build src/index.ts -m -e @serverless-devs/core -o lib"
},
"dependencies": {
"@serverless-devs/core": "latest",
"form-data": "^4.0.0",
"@alicloud/fc2": "^2.2.2",
"@serverless-devs/core": "^0.0.*",
"fs-extra": "^10.0.0",
"got": "^11.8.2",
"lodash": "^4.17.21",
"qs": "^6.10.1",
"readline": "^1.3.0"
},
"autoInstall": false,
@ -47,6 +45,6 @@
"ts-node": "^8.10.2",
"typedoc": "^0.20.35",
"typescript": "^3.9.7",
"esbuild": "^0.14.0"
"@vercel/ncc": "^0.24.0"
}
}

View File

@ -2,7 +2,7 @@ Type: Component
Name: fc-remote-invoke
Provider:
- 其它
Version: 0.0.22
Version: 0.0.17
Description: 初始化component模板
HomePage: https://www.serverless-devs.com
Tags: #标签详情

View File

@ -75,9 +75,9 @@ export default [
content: [
'$ s invoke',
'$ s <ProjectName> invoke',
'$ s invoke --invocation-type sync --event <payload>',
'$ s invoke --event-file <file-path>',
'$ s invoke --event-stdin',
'$ s exec -- invoke --invocation-type sync --event <payload>',
'$ s exec -- invoke --event-file <file-path>',
'$ s exec -- invoke --event-stdin',
],
},
{

View File

@ -5,6 +5,7 @@ import HELP from './common/help';
import { InputProps, isProperties, IProperties } from './interface/entity';
// import StdoutFormatter from './common/stdout-formatter';
import RemoteInvoke from './lib/remote-invoke';
import Client from './lib/client';
export default class FcRemoteInvoke {
/**
@ -19,7 +20,6 @@ export default class FcRemoteInvoke {
credentials,
isHelp,
invocationType,
statefulAsyncInvocationId,
} = await this.handlerInputs(inputs);
await this.report('fc-remote-invoke', 'invoke', credentials?.AccountID);
@ -30,11 +30,10 @@ export default class FcRemoteInvoke {
let fcClient;
if (!props.domainName) {
const fcCommon = await core.loadComponent('devsapp/fc-common');
fcClient = await fcCommon.makeFcClient({ ...inputs, props: { region: props.region }});
fcClient = await Client.buildFcClient(props.region, credentials);
}
const remoteInvoke = new RemoteInvoke(fcClient, credentials.AccountID);
await remoteInvoke.invoke(props, eventPayload, { invocationType, statefulAsyncInvocationId });
await remoteInvoke.invoke(props, eventPayload, { invocationType });
}
private async report(componentName: string, command: string, accountID: string): Promise<void> {
@ -51,12 +50,11 @@ export default class FcRemoteInvoke {
const parsedArgs: {[key: string]: any} = core.commandParse({ ...inputs, args }, {
boolean: ['help', 'event-stdin'],
string: ['invocation-type', 'event', 'event-file', 'region', 'domain-name','service-name', 'function-name', 'qualifier', 'stateful-async-invocation-id'],
string: ['invocation-type', 'event', 'event-file', 'region', 'domain-name','service-name', 'function-name', 'qualifier'],
alias: {
'help': 'h',
'event': 'e',
'event-file': 'f',
'event-stdin': 's',
}
});
@ -75,7 +73,6 @@ export default class FcRemoteInvoke {
'event-stdin': eventStdin,
'invocation-type': invocationType = 'sync',
'domain-name': domainName,
'stateful-async-invocation-id': statefulAsyncInvocationId,
} = argsData;
const eventPayload = { event, eventFile, eventStdin };
// @ts-ignore: 判断三个值有几个真
@ -114,7 +111,6 @@ export default class FcRemoteInvoke {
eventPayload,
isHelp: false,
invocationType: _.upperFirst(invocationType),
statefulAsyncInvocationId,
};
}

114
src/lib/client.ts Normal file
View File

@ -0,0 +1,114 @@
import FC from '@alicloud/fc2';
import querystring from 'querystring';
import kitx from 'kitx';
import httpx from 'httpx';
import * as core from '@serverless-devs/core';
import { ICredentials } from '../interface/entity';
FC.prototype.costom_request = async function (method, path, query, body, headers = {}, opts = {}) {
var url = `${this.endpoint}/${this.version}${path}`;
if (query && Object.keys(query).length > 0) {
url = `${url}?${querystring.stringify(query)}`;
}
headers = Object.assign(this.buildHeaders(), this.headers, headers);
var postBody;
if (body) {
var buff = null;
if (Buffer.isBuffer(body)) {
buff = body;
headers['content-type'] = 'application/octet-stream';
} else if (typeof body === 'string') {
buff = new Buffer(body, 'utf8');
headers['content-type'] = 'application/octet-stream';
} else if ('function' === typeof body.pipe) {
buff = body;
headers['content-type'] = 'application/octet-stream';
} else {
buff = new Buffer(JSON.stringify(body), 'utf8');
headers['content-type'] = 'application/json';
}
if ('function' !== typeof body.pipe) {
const digest = kitx.md5(buff, 'hex');
const md5 = new Buffer(digest, 'utf8').toString('base64');
headers['content-length'] = buff.length;
headers['content-md5'] = md5;
}
postBody = buff;
}
var queriesToSign = null;
if (path.startsWith('/proxy/')) {
queriesToSign = query || {};
}
var signature = FC.getSignature(this.accessKeyID, this.accessKeySecret, method, `/${this.version}${path}`, headers, queriesToSign);
headers['authorization'] = signature;
const response = await httpx.request(url, {
method,
timeout: this.timeout,
headers,
data: postBody
});
var responseBody;
if (!opts['rawBuf'] || response.headers['x-fc-error-type']) {
responseBody = await httpx.read(response, 'utf8');
} else {
// @ts-ignore: .
responseBody = await httpx.read(response);
}
const contentType = response.headers['content-type'] || '';
if (contentType.startsWith('application/json')) {
try {
responseBody = JSON.parse(responseBody);
} catch (ex) {}
}
let err;
if (response.statusCode < 200 || response.statusCode >= 300) {
const code = response.statusCode;
const requestid = response.headers['x-fc-request-id'];
var errMsg;
if (responseBody.ErrorMessage) {
errMsg = responseBody.ErrorMessage;
} else {
errMsg = responseBody.errorMessage;
}
err = new Error(`${method} ${path} failed with ${code}. requestid: ${requestid}, message: ${errMsg}.`);
err.name = `FC${responseBody.ErrorCode}Error`;
// @ts-ignore: .
err.code = responseBody.ErrorCode;
}
return {
err,
code: response.statusCode,
'headers': response.headers,
'data': responseBody,
};
}
export default class Client {
static async buildFcClient(region: string, credentials: ICredentials) {
return new FC(credentials.AccountID, {
accessKeyID: credentials.AccessKeyID,
accessKeySecret: credentials.AccessKeySecret,
securityToken: credentials.SecurityToken,
region,
endpoint: await this.getFcEndpoint(),
timeout: 6000000,
})
}
private static async getFcEndpoint(): Promise<string | undefined> {
const fcDefault = await core.loadComponent('devsapp/fc-default');
const fcEndpoint: string = await fcDefault.get({ args: 'fc-endpoint' });
if (!fcEndpoint) { return undefined; }
const enableFcEndpoint: any = await fcDefault.get({ args: 'enable-fc-endpoint' });
return (enableFcEndpoint === true || enableFcEndpoint === 'true') ? fcEndpoint : undefined;
}
}

View File

@ -16,10 +16,9 @@ export default class File {
let input;
if (eventFile === '-') { // read from stdin
logger.log('Reading event data from stdin, which can be ended with Enter then Ctrl+D');
logger.log('Reading event data from stdin, which can be ended with Enter then Ctrl+D')
input = process.stdin;
} else {
logger.log('Reading event file content:');
input = fs.createReadStream(eventFile, {
encoding: 'utf-8'
})
@ -33,10 +32,7 @@ export default class File {
rl.on('line', (line) => {
event += line
})
rl.on('close', () => {
logger.log('');
resolve(event)
})
rl.on('close', () => resolve(event))
rl.on('SIGINT', () => reject(new Error('^C')))
})

View File

@ -1,34 +0,0 @@
import _ from 'lodash';
import qs from 'qs';
import FormData from 'form-data';
export default function handlerBody(contentType: string, body: any) {
if (contentType.includes('text/') || contentType.includes('application/json') || contentType.includes('application/xml')) {
if (_.isString(body)) return body;
try {
return JSON.stringify(body);
} catch (_ex) {
return body.toString();
}
}
if (contentType.includes('application/x-www-form-urlencoded')) {
return qs.stringify(body, { indices: false });
}
if (contentType.includes('multipart/form-data')) {
const form = new FormData();
try {
const newBody = _.isObject(body) ? body : JSON.parse(body);
for (const [key, value] of Object.entries(newBody)) {
form.append(key, value);
}
return form;
} catch (_ex) {
throw new Error(`Handler body error: The request header is ${contentType}, but the request body is not an object`);
}
}
return body;
}

View File

@ -13,7 +13,7 @@ export default class RemoteInvoke {
this.accountId = accountId;
}
async invoke (props: IProperties, eventPayload: IEventPayload, { invocationType, statefulAsyncInvocationId }) {
async invoke (props: IProperties, eventPayload: IEventPayload, { invocationType }) {
const event = await Event.eventPriority(eventPayload);
logger.debug(`event: ${event}`);
@ -32,13 +32,12 @@ export default class RemoteInvoke {
const payload: any = { event, serviceName, functionName, qualifier };
if (_.isEmpty(httpTriggers)) {
payload.invocationType = invocationType;
payload.statefulAsyncInvocationId = statefulAsyncInvocationId;
payload.event = event;
await this.eventInvoke(payload);
} else {
payload.region = region;
payload.event = this.getJsonEvent(event);
await this.httpInvoke(payload);
}
}
@ -49,9 +48,9 @@ export default class RemoteInvoke {
payload.headers = {};
}
payload.headers['X-Fc-Log-Type'] = 'Tail';
const { body, headers } = await got(url, payload);
this.showLog(headers['x-fc-log-result']);
logger.log('\nFC Invoke Result:', 'green');
console.log(body);
@ -75,15 +74,13 @@ export default class RemoteInvoke {
functionName,
event,
qualifier = 'LATEST',
invocationType,
statefulAsyncInvocationId
invocationType
}) {
if (invocationType === 'Sync') {
const rs = await this.fcClient.invokeFunction(serviceName, functionName, event, {
'X-Fc-Log-Type': 'Tail',
'X-Fc-Invocation-Code-Version': 'Latest',
'X-Fc-Invocation-Type': invocationType,
'X-Fc-Invocation-Type': invocationType
}, qualifier);
this.showLog(rs.headers['x-fc-log-result']);
@ -91,11 +88,8 @@ export default class RemoteInvoke {
console.log(rs.data);
console.log('\n');
} else {
logger.debug(`Stateful async invocation id: ${statefulAsyncInvocationId}`);
const { headers } = await this.fcClient.invokeFunction(serviceName, functionName, event, {
'X-Fc-Invocation-Code-Version': 'Latest',
'X-Fc-Invocation-Type': invocationType,
'X-Fc-Stateful-Async-Invocation-Id': statefulAsyncInvocationId || "",
'X-Fc-Invocation-Type': invocationType
}, qualifier);
const rId = headers['x-fc-request-id'];
@ -107,7 +101,7 @@ export default class RemoteInvoke {
const q = qualifier ? `.${qualifier}` : '';
event.path = `/proxy/${serviceName}${q}/${functionName}/${event.path || ''}`;
logger.log(`Request url: https://${this.accountId}.${region}.fc.aliyuncs.com/2016-08-15/proxy/${serviceName}${q}/${functionName}/`);
logger.log(`https://${this.accountId}.${region}.fc.aliyuncs.com/2016-08-15/proxy/${serviceName}${q}/${functionName}/`);
await this.request(event)
}
@ -115,14 +109,8 @@ export default class RemoteInvoke {
* @param event: { body, headers, method, queries, path }
* path /proxy/serviceName/functionName/path ,
*/
async request(event) {
const { headers = {}, queries, method = 'GET', path: p, body } = event;
if (!headers['X-Fc-Log-Type']) {
headers['X-Fc-Log-Type'] = 'Tail';
}
if (!headers['X-Fc-Invocation-Code-Version']) {
headers['X-Fc-Invocation-Code-Version'] = 'Latest';
}
async request (event) {
const { headers, queries, method, path: p, body } = this.handlerHttpParmase(event);
let resp;
try {
@ -137,7 +125,8 @@ export default class RemoteInvoke {
resp = await this.fcClient.costom_request('PUT', p, null, body, headers);
} else if (mt === 'DELETE') {
resp = await this.fcClient.costom_request('DELETE', p, queries, null, headers);
} else if (method.toLocaleUpperCase() === 'PATCH') {
}
else if (method.toLocaleUpperCase() === 'PATCH') {
resp = await this.fcClient.costom_request('PATCH', p, queries, body, headers);
} else if (method.toLocaleUpperCase() === 'HEAD') {
resp = await this.fcClient.costom_request('HEAD', p, queries, body, headers);
@ -155,20 +144,55 @@ export default class RemoteInvoke {
if (resp?.err) {
this.showLog(resp.headers['x-fc-log-result']);
logger.log(`\nFC Invoke Result[Code: ${resp.code}]:`, 'red');
logger.log(`\nFC Invoke Result[code: ${resp.code}]:`, 'red');
console.log(resp.data);
console.log('\n');
} else {
if (resp) {
this.showLog(resp.headers['x-fc-log-result']);
logger.log(`\nFC Invoke Result[Code: ${resp.code}]:`, 'green');
logger.log('\nFC Invoke Result[code: ${resp.code}]:', 'green');
console.log(resp.data);
console.log('\n');
}
}
}
handlerHttpParmase (event) {
const { body = '', headers = {}, method = 'GET', queries = '', path: p = '' } = event;
let postBody;
if (body) {
let buff = null;
if (Buffer.isBuffer(body)) {
buff = body;
headers['content-type'] = 'application/octet-stream';
} else if (typeof body === 'string') {
buff = Buffer.from(body, 'utf8');
headers['content-type'] = 'application/octet-stream';
} else if (typeof body.pipe === 'function') {
buff = body;
headers['content-type'] = 'application/octet-stream';
} else {
buff = Buffer.from(JSON.stringify(body), 'utf8');
headers['content-type'] = 'application/json';
}
postBody = buff;
}
if (!headers['X-Fc-Log-Type']) {
headers['X-Fc-Log-Type'] = 'Tail';
}
return {
headers,
queries,
method,
path: p,
body: postBody
}
}
private showLog(log) {
if (log) {
logger.log('========= FC invoke Logs begin =========', 'yellow');
@ -186,4 +210,4 @@ export default class RemoteInvoke {
throw new Error('handler event error. Example: https://github.com/devsapp/fc-remote-invoke/blob/master/example/http.json');
}
}
}
}

View File

@ -24,7 +24,7 @@
"./node_modules/@types"
],
"rootDir": "src",
"outDir": "dist"
"outDir": "lib"
},
"exclude": [
"**/node_modules/**",