---
# System prepended metadata

title: Serverless Tencent Local Dev Feature
tags: [' functions', ' feature', cli]

---

---
tags: cli, functions, feature
author: Oliver.W
---
> ticket: https://app.asana.com/0/1200011502754281/1200276428733645/f

# Serverless Tencent Local Dev Feature

## Problem to Solve:
* Dev mode need time do deploy.
* Dev mode is not stable and not find the reason yet. (Caused by SCF socket side issue.)
* The event object for SCF is difficult to create and reproduct in local env.
* User need support on function development local test & debug.

### Things need to consider
* Until release invoke and fix Dev Mode user has no way to do debug easily.
* Framework components (next.js, express, etc) are support local debug mode by framework.
* Based on usage analytics, the Node.js take over 85% of the function development cases.
* The local dev feature used only for users' function method, no need to support infrastructure like, API Gateway, Database, and etc.

## Key Result:
* User can do local debug with functional development in local (without deploy)
* User can easy generate event object and interact wtih function method locally.

## Proposed Solution:

Add `invoke local` for SCF components only. function developer should be able to use only with SCF components and show the help info under SCF components folder.

```
invoke local .................. 调用本地函数
    --function / -f ....................函数名称(如果使用SCF组件可以不指定函数名称)
    --data / -d ........................要传递给调用函数的序列化Event数据(String)
    --path / -p ........................要传递给调用函数Event的 json 文件所在路径
    --context ..........................要传递给调用函数的序列化Context数据(String)
    --contextPath / -x .................要传递给调用函数Context的 json 文件所在路径
    --env / -e .........................覆盖环境变量信息. e.g. --env VAR1=val1 --env VAR2=val2
    --config / -c ......................Path to serverless config file
```

### User Experience

* User can find out event and context examples from help doc and Tencent Cloud help doc.
    * User will get notification to check help doc when use the invoke local function.
* User will use invoke local function to do local dev and debug.
* User can pass customized event and context object in string or json file.
* After execute funciton the result should be log into console in JSON that formatted and colorized.
    * https://www.npmjs.com/package/jsome consider lib like this to make the result pretty.
    * For http res example

```sh
# 确保本地handler执行完显示的返回结果 与在scf中执行完反的数据信息一致。 （没有信息隐藏或默认不可见）

{
    "message": "Go Serverless v1.0! Your function executed successfully!"
    "code": 2021
    "nestOjb": {
        "string": "string"
    }
}
```

## Usage

### Invoke Local Funciton

```sh
--- 无法找到指定函数
Serverless: (黄色)调用的函数 {functionName} 不存在，请检查后重试。(/黄色)

--- Invoke非Component项目
Serverless: (黄色)当前命令只支持 SCF 组件，请在 SCF 组件目录内使用。(/黄色)

--- yml中没有handler字段（SCF Component）
Serverless: (黄色)调用函数未指定，请检查 serverless.yml 中的hanlder配置后重试。(/黄色)

--- 同时传递了 data 和 path
Serverless: (黄色)不能同时指定 data 与 path ，请检查后重试。(/黄色)

--- 同时传递了 context 和 contextPath
Serverless: (黄色)不能同时指定 context 与 contextPath ，请检查后重试。(/黄色)

--- 传递了无效的event参数
Serverless: (黄色)事件(event)参数数据格式错误，请检查后重试。(/黄色)

--- 传递了无效的context参数
Serverless: (黄色)上下文(context)参数数据格式错误，请检查后重试。(/黄色)

--- 传递了无效的path
Serverless: (黄色)找不到指定的路径文件 {Path}，请检查后重试。(/黄色)

--- Node的major.minor版本不匹配，(只忽略patch版本)
当前系统Node版本为 xxx, 项目指定的版本为 xxx, 建议使用相同版本进行测试。 

--- yml 中的runtime不是nodejs
Serverless: (黄色)当前命令只支持 Node.js 运行时，其他运行时暂不支持。(/黄色)

--- 调用成功（json对象形式）
... (绿色)调用成功(/绿色)

--- 调用错误 json对象形式）
... (红色)调用错误(/红色)

--- Invoke 功能错误
(红色)Serverless > 命令执行错误: {msg} (/红色)

  帮助文档：xxx
```

## Launch Plan

### Phase 1
* Enable Node.js function local dev feature.
    * Check node.js runtime before envoke local. (Error)
    * Major version should be same with the runtime defined in config. (Warning)

### Phase 2
* Enable Python and other function local dev feature with docker.

### Phase 3
* TBC

---
## Logic Path

## Usage:

### Invoke Local Function
```
TBC
```
