Compare commits
23 Commits
Author | SHA1 | Date |
---|---|---|
wss-git | 53524ca5a0 | |
千风 | 545b575b2e | |
heimanba | 25e6625cb2 | |
wss-git | 645a184c06 | |
wss-git | 7486b7ee59 | |
wss-git | e930d19e57 | |
wss-git | 0bc9d61b5f | |
wss-git | 0b9712838c | |
wss-git | 24e0676581 | |
wss-git | f0468eddf3 | |
wss-git | 93d6d628c2 | |
wss-git | 978e57fef7 | |
wss-git | bf6593e33c | |
wss-git | 2c0a0b628d | |
wss-git | c1a2067fb0 | |
wss-git | f8c5754072 | |
千风 | 9c10415e0f | |
千风 | 9c111e5b06 | |
wss-git | f7670a02aa | |
wss-git | 724001f038 | |
wss-git | 3f41b8c50a | |
wss-git | ec36588ae7 | |
wss-git | 2c3bcc6164 |
|
@ -5,5 +5,3 @@ node_modules
|
|||
package-lock.json
|
||||
.idea
|
||||
.vscode
|
||||
lib
|
||||
|
||||
|
|
4
.signore
4
.signore
|
@ -1,8 +1,7 @@
|
|||
node_modules
|
||||
.s
|
||||
tsconfig.json
|
||||
LICENSE
|
||||
package-lok.json
|
||||
package-lock.json
|
||||
.prettierrc.js
|
||||
.gitignore
|
||||
|
||||
|
@ -15,3 +14,4 @@ package-lok.json
|
|||
README.md
|
||||
./example
|
||||
./.signore
|
||||
./src
|
||||
|
|
File diff suppressed because one or more lines are too long
15
package.json
15
package.json
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "fc-remote-invoke",
|
||||
"version": "0.0.17",
|
||||
"version": "0.0.22",
|
||||
"description": "This is a component demo for Serverless Devs Tool ",
|
||||
"keywords": [
|
||||
"Serverless",
|
||||
|
@ -13,7 +13,7 @@
|
|||
"author": "Serverless-Devs",
|
||||
"contributors": [],
|
||||
"license": "MIT",
|
||||
"main": "./lib/index.js",
|
||||
"main": "./dist/index.js",
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
|
@ -24,14 +24,17 @@
|
|||
"scripts": {
|
||||
"start": "npm run watch",
|
||||
"watch": "tsc -w",
|
||||
"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"
|
||||
"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"
|
||||
},
|
||||
"dependencies": {
|
||||
"@serverless-devs/core": "^0.0.*",
|
||||
"@serverless-devs/core": "latest",
|
||||
"form-data": "^4.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,
|
||||
|
@ -44,6 +47,6 @@
|
|||
"ts-node": "^8.10.2",
|
||||
"typedoc": "^0.20.35",
|
||||
"typescript": "^3.9.7",
|
||||
"@vercel/ncc": "^0.24.0"
|
||||
"esbuild": "^0.14.0"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ Type: Component
|
|||
Name: fc-remote-invoke
|
||||
Provider:
|
||||
- 其它
|
||||
Version: 0.0.17
|
||||
Version: 0.0.22
|
||||
Description: 初始化component模板
|
||||
HomePage: https://www.serverless-devs.com
|
||||
Tags: #标签详情
|
||||
|
|
10
src/index.ts
10
src/index.ts
|
@ -19,6 +19,7 @@ export default class FcRemoteInvoke {
|
|||
credentials,
|
||||
isHelp,
|
||||
invocationType,
|
||||
statefulAsyncInvocationId,
|
||||
} = await this.handlerInputs(inputs);
|
||||
await this.report('fc-remote-invoke', 'invoke', credentials?.AccountID);
|
||||
|
||||
|
@ -30,10 +31,10 @@ export default class FcRemoteInvoke {
|
|||
let fcClient;
|
||||
if (!props.domainName) {
|
||||
const fcCommon = await core.loadComponent('devsapp/fc-common');
|
||||
fcClient = await fcCommon.makeFcClient(inputs);
|
||||
fcClient = await fcCommon.makeFcClient({ ...inputs, props: { region: props.region }});
|
||||
}
|
||||
const remoteInvoke = new RemoteInvoke(fcClient, credentials.AccountID);
|
||||
await remoteInvoke.invoke(props, eventPayload, { invocationType });
|
||||
await remoteInvoke.invoke(props, eventPayload, { invocationType, statefulAsyncInvocationId });
|
||||
}
|
||||
|
||||
private async report(componentName: string, command: string, accountID: string): Promise<void> {
|
||||
|
@ -50,11 +51,12 @@ 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'],
|
||||
string: ['invocation-type', 'event', 'event-file', 'region', 'domain-name','service-name', 'function-name', 'qualifier', 'stateful-async-invocation-id'],
|
||||
alias: {
|
||||
'help': 'h',
|
||||
'event': 'e',
|
||||
'event-file': 'f',
|
||||
'event-stdin': 's',
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -73,6 +75,7 @@ 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: 判断三个值有几个真
|
||||
|
@ -111,6 +114,7 @@ export default class FcRemoteInvoke {
|
|||
eventPayload,
|
||||
isHelp: false,
|
||||
invocationType: _.upperFirst(invocationType),
|
||||
statefulAsyncInvocationId,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
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;
|
||||
}
|
|
@ -13,7 +13,7 @@ export default class RemoteInvoke {
|
|||
this.accountId = accountId;
|
||||
}
|
||||
|
||||
async invoke (props: IProperties, eventPayload: IEventPayload, { invocationType }) {
|
||||
async invoke (props: IProperties, eventPayload: IEventPayload, { invocationType, statefulAsyncInvocationId }) {
|
||||
const event = await Event.eventPriority(eventPayload);
|
||||
logger.debug(`event: ${event}`);
|
||||
|
||||
|
@ -32,12 +32,13 @@ 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);
|
||||
}
|
||||
}
|
||||
|
@ -48,9 +49,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);
|
||||
|
@ -74,13 +75,15 @@ export default class RemoteInvoke {
|
|||
functionName,
|
||||
event,
|
||||
qualifier = 'LATEST',
|
||||
invocationType
|
||||
invocationType,
|
||||
statefulAsyncInvocationId
|
||||
}) {
|
||||
|
||||
if (invocationType === 'Sync') {
|
||||
const rs = await this.fcClient.invokeFunction(serviceName, functionName, event, {
|
||||
'X-Fc-Log-Type': 'Tail',
|
||||
'X-Fc-Invocation-Type': invocationType
|
||||
'X-Fc-Invocation-Code-Version': 'Latest',
|
||||
'X-Fc-Invocation-Type': invocationType,
|
||||
}, qualifier);
|
||||
|
||||
this.showLog(rs.headers['x-fc-log-result']);
|
||||
|
@ -88,8 +91,11 @@ 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-Type': invocationType
|
||||
'X-Fc-Invocation-Code-Version': 'Latest',
|
||||
'X-Fc-Invocation-Type': invocationType,
|
||||
'X-Fc-Stateful-Async-Invocation-Id': statefulAsyncInvocationId || "",
|
||||
}, qualifier);
|
||||
const rId = headers['x-fc-request-id'];
|
||||
|
||||
|
@ -114,6 +120,9 @@ export default class RemoteInvoke {
|
|||
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';
|
||||
}
|
||||
|
||||
let resp;
|
||||
try {
|
||||
|
@ -146,14 +155,14 @@ 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');
|
||||
}
|
||||
|
@ -177,4 +186,4 @@ export default class RemoteInvoke {
|
|||
throw new Error('handler event error. Example: https://github.com/devsapp/fc-remote-invoke/blob/master/example/http.json');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
"./node_modules/@types"
|
||||
],
|
||||
"rootDir": "src",
|
||||
"outDir": "lib"
|
||||
"outDir": "dist"
|
||||
},
|
||||
"exclude": [
|
||||
"**/node_modules/**",
|
||||
|
|
Loading…
Reference in New Issue