laf 的架构图

Laf 以后应用 Node.js 做为云函数运行时环境,从架构图能够看到执行用户云函数的申请会被路由到用户对应的app(Node.js 运行时)中,最初这个函数(代码)在该 app 中被执行。

在 laf 中有两种路由,一种是 apisix 路由负责路由动态资源托管,函数执行等申请,另一种是 laf server web 框架的路由,负责laf server的各种 api。

上面咱们抛开申请的路由过程,以及 app 的其余资源如数据库资源(mongodb),动态资源托管(minio)等,直奔主题,如何手撸一个 laf 的 Node.js 运行时。

laf 的运行时与其余函数计算平台的区别

Laf 函数计算的运行时与其余函数计算平台的运行时的外围区别在于 laf 的运行时是常驻的

常驻代表你的 app 就能够做很多高级的事件,比方反对 WebSocket 长连贯,反对全局缓存,无冷启动等等

其余很多函数计算平台的运行时采纳的是冷启动计划,就是有申请达到后才会去筹备运行时的资源,创立运行时的资源,当没有申请,利用不流动时,资源会被销毁,尽管这样能够节俭内存资源,但无奈反对很多高级的性能,导致开发习惯的割裂,还有泛滥开发者承受不了的响应时长。

laf手把手写一个 laf 运行时

首先我列举一下写一个 laf 运行时须要实现哪些货色

  • 包装一个 web 框架
  • 解决依赖装置,和网络文件的 import
  • 让性能更欠缺,包装更多的 http 申请信息,提供更多的函数运行上下文
  • 写更多的 sdk,让你的运行时能够对接其余资源,例如数据库
  • 优化 runtime,比方缓存数据,缩小对数据的 io 等
  • 提供对云编辑器的反对

当初咱们开始入手了,咱们写一个 mini 版的 laf 函数计算运行时,残缺版本的能够去 laf 的仓库看看。


别忘记了三连 star

要运行这段代码首先得装置下 express,而后 node 执行上面的代码就行了,laf 的 Node.js 运行时用 vm 模块执行云函数,这样会有不错的安全性。

上面的代码用 database 模仿将函数存储在数据库中,通过自定义的 import 解决网络文件的导入。

在解决导入文件能够思考一下如何解决循环依赖,这部分代码就没展现了,仓库里有,提醒一下,定义一个列表,每次导入就把导入的模块名 push 到列表中,这样下次导入的模块如果列表有那就是循环依赖了。

const express = require("express");const bodyParser = require("body-parser");const vm = require("vm");const app = express();const port = 3000;const database = {  cloud_functionb: `    function funcB(query, body) {      return "Received Query: " + JSON.stringify(query) + " Body: " + JSON.stringify(body);    }    module.exports = funcB(context.query, context.body);  `,  cloud_functiona: `    const resultFromFuncB = require('cloud_functionb');    function funcA(query, body) {      return "From funcA with result: " + resultFromFuncB;    }    module.exports = funcA(context.query, context.body);  `,};function customRequire(moduleName, context) {  if (moduleName.startsWith("cloud_")) {    const script = new vm.Script(database[moduleName]);    const moduleExports = {};    const customContext = {      module: { exports: moduleExports },      exports: moduleExports,      console: console,      require: (moduleName) => customRequire(moduleName, context), // Recursive for nested requires      context,    };    script.runInNewContext(customContext);    return customContext.module.exports;  } else {    return require(moduleName); // Fallback to regular require  }}app.use(bodyParser.json());app.all("/*", (req, res) => {  const funcName = req.path.substring(1); // Removes the leading slash  const context = {    query: req.query,    body: req.body,  };  const result = customRequire(funcName, context);  res.send(result);});app.listen(port, () => {  console.log(`App listening at http://localhost:${port}`);});// test// curl "http://localhost:3000/cloud_functiona?param1=value1&param2=value2"// curl "http://localhost:3000/cloud_functionb?param1=value1&param2=value2"// curl -X POST -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}' "http://localhost:3000/cloud_functiona?param1=value1&param2=value2"

这个 mini 版的运行时为了足够简略,省去了很多货色,比方没有缓存数据,数据库连贯的优化,sdk 的开发,提供云编辑器反对等。

这些在 laf 的源码里都有,如何提供对云编辑器的反对,能够看看 language-server-protocol

Laf 也快反对 LSP 了,具体实现能够参考咱们之前分享的:如何创立集成 LSP 的 Web 代码编辑器。

援用链接
[1]
api: http://api.laf.run/

[2]
laf 的仓库: https://github.com/labring/laf

[3]
laf 的源码: https://github.com/labring/laf

[4]
language-server-protocol: https://microsoft.github.io/language-server-protocol/

图片
退出 Laf 开源社区
体验像写博客一样写代码

官网链接
https://laf.run

GitHub 地址
https://github.com/labring/laf

拜访 Laf 文档
https://doc.laf.run/guide/

️逛逛论坛
https://forum.laf.run/

对于 Laf

Laf 是一款为所有开发者打造的集函数、数据库、存储为一体的云开发平台,助你像写博客一样写代码,随时随地公布上线利用!3 分钟上线 ChatGPT 利用!

人划线

sealos 以kubernetes为内核的云操作系统发行版,让云原生简略遍及

laf 写代码像写博客一样简略,什么docker kubernetes通通不关怀,我只关怀写业务!