@fnproject/fdk

中文文档
0.0.114发布于 2026/11/04Apache-2.0CommonJS构建来源已验证近 30 天下载 2.2万

Function Developer Kit for Fn

原始使用文档
文档目录6 节
  1. Creating a Node Function
  2. Function Context
  3. Asynchronous function responses
  4. Handling non-json input and output
  5. Using HTTP headers and setting HTTP status codes
  6. Fn and Node.js Dependencies

Fn Function Developer Kit for Node.js

This Function Developer Kit makes it easy to deploy Node.js functions to Fn. It currently supports default (cold) and hot functions using the JSON format.

Creating a Node Function

Writing a Node.js function is simply a matter of writing a handler function that you pass to the FDK to invoke each time your function is called.

Start by creating a node function with fn init and installing the FDK:

fn init --runtime node nodefunc
cd nodefunc

This creates a simple hello world function in func.js:

const fdk=require('@fnproject/fdk');

fdk.handle(function(input){
  let name = 'World';
  if (input.name) {
    name = input.name;
  }
  return {'message': 'Hello ' + name}
})

The handler function takes the string input that is sent to the function and returns a response string. Using the FDK you don't have to worry about reading input from standard input and writing to standard output to return your response. The FDK let's you focus on your function logic and not the mechanics.

Now run it!

fn deploy --local --app fdkdemo 
fn invoke fdkdemo nodefunc 

Now you have a basic running Node function that you can modify and add what you want.

echo -n '{"name": "Tom"}' | fn invoke fdkdemo nodefunc

You should see the result

{"message": "Hello Tom"}

Function Context

Function invocation context details are available through an optional function argument. To receive a context object, simply add a second argument to your handler function. in the following example the callID is obtained from the context and included in the response message:

const fdk=require('@fnproject/fdk');

fdk.handle(function(input, ctx){
  let name = 'World';
  if (input) {
    name = input;
  }
  return 'Hello ' + name + ' from Node call ' + ctx.callID + '!';
})

The context contains other context information about the request such as:

  • ctx.config : An Object containing function config variables (from the environment ) (read only)
  • ctx.headers : an object containing input headers for the event as lists of strings (read only)
  • ctx.deadline : a Date object indicating when the function call must be processed by
  • ctx.callID : The call ID of the current call
  • ctx.fnID : The Function ID of the current function
  • ctx.memory : Amount of ram in MB allocated to this function
  • ctx.contentType : The incoming request content type (if set, otherwise null)
  • ctx.setResponseHeader(key,values...) : Sets a response header to one or more values
  • ctx.addResponseHeader(key,values...) : Appends values to an existing response header
  • ctx.responseContentType set/read the response content type of the function (read/write)
  • ctx.httpGateway The HTTP Gateway context for this function (if set) see HTTPGatewayContext below

Asynchronous function responses

You return an asynchronous response from a function by returning a Javascript Promise from the function body:

const fdk=require('@fnproject/fdk');

fdk.handle(function(input, ctx){
  return new Promise((resolve,reject)=>{
     setTimeout(()=>resolve("Hello"),1000);
  });
})

You can also use async/await calling conventions in functions.

Handling non-json input and output

By default the FDK will try and convert input into a JSON object, or fall back to its string format otherwise.

Likewise by default the output of a function will be treated as a JSON object and converted using JSON.stringify.

To change the handling of the input you can add an additional options parameter to fdk.handle that specifies the input handling strategy:

function myfunction(input,ctx){}

fdk.handle(myfunction, {inputMode: 'string'})

valid input modes are:

  • json (the default) attempts to parse the input as json or falls back to raw (possibly binary) string value otherwise
  • string always treats input as a string
  • buffer reads input into a Buffer object and passes this to your function

To change the output handling of your function from the default you should wrap the result value using a response decorator:

function myfunction(input,ctx){
   return fdk.rawResult("Some string")
}

fdk.handle(myfunction)

the available decorators are:

  • rawResult({string|Buffer}) passes the result directly to the response - the value can be a string or a buffer - this will not encode quotes on string objects
  • streamResult({ReadableStream}) pipes the contents of a ReadableStream into the output - this allows processing of data from files or HTTP responses

Using HTTP headers and setting HTTP status codes

You can read http headers passed into a function invocation using ctx.protocol.header(key), this returns the first header value of the header matching key (after canonicalization) and ctx.protocol.headers which returns an object containing all headers.

const fdk=require('@fnproject/fdk');

fdk.handle(function(input, ctx){
  
  let hctx = ctx.httpGateway
  console.log("Request URL" , hctx.requestURL)
  
  console.log("Authorization header:" , hctx.getHeader("Authorization"))
  console.log( hctx.headers) // prints e.g. { "Content-Type": ["application/json"],"Accept":["application/json","text/plain"] } 
})

Outbound headers and the HTTP status code can be modified in a similar way:

const fdk=require('@fnproject/fdk');

fdk.handle(function(input, ctx){
    let hctx = ctx.httpGateway

   hctx.setResponseHeader("Location","http://example.com")
   hctx.statusCode = 302
})

The HTTPGatewayContext object has a similar interface to Context but accesses only the HTTP headers of the function:

  • hctx.requestURL : Get the http request URL of the function as received by the gateway (null if not set)
  • hctx.method : Get the HTTP request method used to invoke the gateway
  • hctx.headers : Get the HTTP headers of the incoming request (read-only)
  • hctx.statusCode : Set the the HTTP status code of the HTTP resposne & hctx.setResponseHeader(key,values..), hctx.addResponseHeader(key,values) Set/add response headers

Fn and Node.js Dependencies

Fn handles Node.js dependencies in the following way:

  • If a package.json is present without a node_modules directory, an Fn build runs an npm install within the build process and installs your dependencies.
  • If the node_modules is present, Fn assumes you have provided the dependencies yourself and no installation is performed.

版本与发布记录

@fnproject/fdk 共发布 93 个版本,首次发布于 2018/02/16;过去 12 个月发布了 18 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.0.114latest2026/11/0412 天前
0.0.1132026/10/1630 天前
0.0.1122026/10/081 个月前
0.0.1112026/10/061 个月前
0.0.1082026/09/241 个月前
0.0.1062026/09/231 个月前

关于 @fnproject/fdk 的常见问题

@fnproject/fdk 是什么?

Function Developer Kit for Fn

如何安装 @fnproject/fdk?

在项目目录运行 npm install @fnproject/fdk,或使用 pnpm add / yarn add / bun add。

@fnproject/fdk 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/fnproject__fdk 是否存在。

@fnproject/fdk 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@fnproject/fdk 可以商用吗?

@fnproject/fdk 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@fnproject/fdk 的下载量有多少?

根据 npm 官方下载统计,@fnproject/fdk 在 2026/10/15 – 2026/11/13 这 30 天内被下载 22,316 次,日均约 744 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fnproject/fdk 还在维护吗?

最新版本 0.0.114 发布于 2026/11/04(12 天前);至今共发布 93 个版本,其中过去 12 个月发布了 18 个;首次发布于 2018/02/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。