diff --git a/src/index.ts b/src/index.ts index c929644..b83c253 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,16 +5,45 @@ 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 { - async report(componentName: string, command: string, accountID: string): Promise { + /** + * event 函数本地调试 + * @param inputs + * @returns + */ + async invoke(inputs: InputProps): Promise { + const { + props, + eventPayload, + credentials, + isHelp, + invocationType, + } = await this.handlerInputs(inputs); + await this.report('fc-remote-invoke', 'invoke', credentials?.AccountID); + + if (isHelp) { + core.help(HELP); + return; + } + + let fcClient; + if (!props.domainName) { + fcClient = await Client.buildFcClient(props.region, credentials); + } + const remoteInvoke = new RemoteInvoke(fcClient, credentials.AccountID); + await remoteInvoke.invoke(props, eventPayload, { invocationType }); + } + + private async report(componentName: string, command: string, accountID: string): Promise { core.reportComponent(componentName, { command, uid: accountID, }); } - async handlerInputs(inputs: InputProps): Promise { + private async handlerInputs(inputs: InputProps): Promise { // 去除 args 的行首以及行尾的空格 const args: string = (inputs?.args || '').replace(/(^\s*)|(\s*$)/g, ''); logger.debug(`input args: ${args}`); @@ -85,27 +114,4 @@ export default class FcRemoteInvoke { }; } - /** - * event 函数本地调试 - * @param inputs - * @returns - */ - public async invoke(inputs: InputProps): Promise { - const { - props, - eventPayload, - credentials, - isHelp, - invocationType, - } = await this.handlerInputs(inputs); - await this.report('fc-remote-invoke', 'invoke', credentials?.AccountID); - - if (isHelp) { - core.help(HELP); - return; - } - - const remoteInvoke = new RemoteInvoke(props.region, credentials, props.domainName); - await remoteInvoke.invoke(props, eventPayload, { invocationType }); - } } diff --git a/src/lib/client.ts b/src/lib/client.ts index 542ab13..db34f9a 100644 --- a/src/lib/client.ts +++ b/src/lib/client.ts @@ -1,14 +1,24 @@ import FC from '@alicloud/fc2'; +import * as core from '@serverless-devs/core'; import { ICredentials } from '../interface/entity'; export default class Client { - static buildFcClient(region: string, credentials: ICredentials) { + 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 { + 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; + } } \ No newline at end of file diff --git a/src/lib/remote-invoke.ts b/src/lib/remote-invoke.ts index 25860ee..37798c1 100644 --- a/src/lib/remote-invoke.ts +++ b/src/lib/remote-invoke.ts @@ -1,6 +1,5 @@ import _ from 'lodash'; import got from 'got'; -import Client from './client'; import { IProperties, IEventPayload } from '../interface/entity'; import Event from './event'; import logger from '../common/logger'; @@ -9,11 +8,9 @@ export default class RemoteInvoke { fcClient: any; accountId: string; - constructor(region: string, credentials, domainName) { - if (!domainName) { - this.accountId = credentials.AccountID; - this.fcClient = Client.buildFcClient(region, credentials); - } + constructor(fcClient: any, accountId: string) { + this.fcClient = fcClient; + this.accountId = accountId; } async invoke (props: IProperties, eventPayload: IEventPayload, { invocationType }) {