diff --git a/README.md b/README.md index 584bb6d..d74a4e9 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,9 @@ services: ```` -$ s exec -- invoke --invocation-type sync --event ${payload} -$ s exec -- invoke --invocation-type async --event-file ${path} -$ s exec -- invoke --event-stdin +$ s invoke --invocation-type sync --event ${payload} +$ s invoke --invocation-type async --event-file ${path} +$ s invoke --event-stdin ```` ## CLI 用法 diff --git a/example/http.json b/example/http.json index b13fda6..cdec5d3 100644 --- a/example/http.json +++ b/example/http.json @@ -1,11 +1,16 @@ { - "body": 123, "method": "POST", "headers": { - "key": "value" + "key": "value", + "Content-Type": "application/json" }, "queries": { "key": "value" }, - "path": "string" + "body": { + "abc": "body", + "tmpNasZipPath": "/tmpNasZipPath", + "body": "body" + }, + "path": "download" } \ No newline at end of file diff --git a/package.json b/package.json index 48bf67b..22578a3 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,6 @@ "build": "ncc build src/index.ts -m -e @serverless-devs/core -o lib" }, "dependencies": { - "@alicloud/fc2": "^2.2.2", "@serverless-devs/core": "^0.0.*", "form-data": "^4.0.0", "fs-extra": "^10.0.0", diff --git a/src/common/help.ts b/src/common/help.ts index a5c840c..a79f227 100644 --- a/src/common/help.ts +++ b/src/common/help.ts @@ -75,9 +75,9 @@ export default [ content: [ '$ s invoke', '$ s invoke', - '$ s exec -- invoke --invocation-type sync --event ', - '$ s exec -- invoke --event-file ', - '$ s exec -- invoke --event-stdin', + '$ s invoke --invocation-type sync --event ', + '$ s invoke --event-file ', + '$ s invoke --event-stdin', ], }, { diff --git a/src/index.ts b/src/index.ts index 134ff11..99260b6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,6 @@ 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 { /** @@ -30,7 +29,8 @@ export default class FcRemoteInvoke { let fcClient; if (!props.domainName) { - fcClient = await Client.buildFcClient(props.region, credentials); + const fcCommon = await core.loadComponent('devsapp/fc-common'); + fcClient = await fcCommon.makeFcClient(inputs); } const remoteInvoke = new RemoteInvoke(fcClient, credentials.AccountID); await remoteInvoke.invoke(props, eventPayload, { invocationType }); diff --git a/src/lib/client.ts b/src/lib/client.ts deleted file mode 100644 index 62df63f..0000000 --- a/src/lib/client.ts +++ /dev/null @@ -1,114 +0,0 @@ -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 { - 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/event.ts b/src/lib/event.ts index e3b46cf..8fb3d66 100644 --- a/src/lib/event.ts +++ b/src/lib/event.ts @@ -16,9 +16,10 @@ 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' }) @@ -32,7 +33,10 @@ export default class File { rl.on('line', (line) => { event += line }) - rl.on('close', () => resolve(event)) + rl.on('close', () => { + logger.log(''); + resolve(event) + }) rl.on('SIGINT', () => reject(new Error('^C'))) }) diff --git a/src/lib/remote-invoke.ts b/src/lib/remote-invoke.ts index 65351ba..4fd1849 100644 --- a/src/lib/remote-invoke.ts +++ b/src/lib/remote-invoke.ts @@ -101,7 +101,7 @@ export default class RemoteInvoke { const q = qualifier ? `.${qualifier}` : ''; event.path = `/proxy/${serviceName}${q}/${functionName}/${event.path || ''}`; - logger.log(`https://${this.accountId}.${region}.fc.aliyuncs.com/2016-08-15/proxy/${serviceName}${q}/${functionName}/`); + logger.log(`Request url: https://${this.accountId}.${region}.fc.aliyuncs.com/2016-08-15/proxy/${serviceName}${q}/${functionName}/`); await this.request(event) } @@ -109,8 +109,11 @@ export default class RemoteInvoke { * @param event: { body, headers, method, queries, path } * path 组装后的路径 /proxy/serviceName/functionName/path , */ - async request (event) { - const { headers, queries, method, path: p, body } = this.handlerHttpParmase(event); + async request(event) { + const { headers = {}, queries, method = 'GET', path: p, body } = event; + if (!headers['X-Fc-Log-Type']) { + headers['X-Fc-Log-Type'] = 'Tail'; + } let resp; try { @@ -125,8 +128,7 @@ 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); @@ -158,41 +160,6 @@ export default class RemoteInvoke { } } - 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');