带着问题看源码8-NodeRed中的Library模块

本文介绍Node-RED中的Library模块,该模块通过前后端配合实现运行数据的序列化与共享。文章涵盖模块意义、功能、实现细节及其应用场景,并提供实践案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 此模块的意义

NodeRed Library 模块是一组前后端配合使用的模块。它可将运行数据序列化到磁盘,实现数据共享。

2. NodeRed 此模块功能

当前 NodeRed 提供了两个默认的 Library 模块:example 和 local,分别实现了例子文件的读写和流程导入导出。前端提供操作界面,通过 NodeRed 提供的 Restful 接口调用后端,后端根据参数实现功能。

模块同时提供插件机制,支持自字义 Library 模块,官方目前提供了一个后端模块:node-red-library-file-store

3. NodeRed 中此模块的实现及参与者

packages/node_modules/@node-red/runtime/lib/library/index.js

提供 Restful 接口,实现插件的配置文件加载和默认加载(Local 和 example)

  1. 配置文件加载和默认加载
function init(_runtime) {
    runtime = _runtime;
    events.removeListener("registry:plugin-added",onPluginAdded);
    events.on("registry:plugin-added",onPluginAdded);

    knownTypes.flows = 'node-red';

    libraries["local"] = require("./local");
    libraries["local"].init(runtime);
    libraryConfigs["local"] = libraries["local"]

    libraries["examples"] = require("./examples");
    libraries["examples"].init(runtime);
    libraryConfigs["examples"] = libraries["examples"]

    try {
        runtimeLibraries = runtime.settings.editorTheme.library.sources;
    } catch(err) {
        runtimeLibraries = [];
    }
    // userLibraries = runtime.settings.get("library")


}
  1. 提供接口

getEntry:将已实例化对象返回,使用的反序列化模块由传入参数决定

function getEntry(library,type,path) {
    if (!knownTypes.hasOwnProperty(type)) {
        throw new Error(log._("library.unknownType",{type: type}))
    }
    if (libraries.hasOwnProperty(library)) {
        return libraries[library].getEntry(type,path);
    } else {
        throw new Error(log._("library.unknownLibrary",{library: library}))
    }
}

saveEntry:将传入的对象实例化,使用的序列化模块由传入参数决定

function saveEntry(library,type,path,meta,body) {
    if (!knownTypes.hasOwnProperty(type)) {
        throw new Error(log._("library.unknownType",{type: type}))
    }
    if (libraries.hasOwnProperty(library)) {
        if (libraries[library].saveEntry) {
            return libraries[library].saveEntry(type,path,meta,body);
        } else {
            throw new Error(log._("library.readOnly",{library: library}))
        }
    } else {
        throw new Error(log._("library.unknownLibrary",{library: library}))
    }
}

4. NodeRed 为什么这么设计,这种设计的优劣有哪些

  1. 提供接口,以插件形式扩展,符合 NodeRed 插件化平台化的思想。
  2. NodeRed 中大量使用的插件机制,利用的 JS 的模块加载,实现起来比静态语言方便很多。
  3. 若存储模块未做好接口过滤,会有数据暴露风险。

5. 应用场景分析

  1. 将流存储到远端,在各终端中共享。
  2. 将数据以插件形式序列化到数据库、文件、缓存等。

6. 实践

6.1. 存储

  1. settings.js
    // Customising the editor
  editorTheme: {
    projects: {
      // To enable the Projects feature, set this value to true
      enabled: false,
    },
    library: {
      sources: [
        {
          id: 'team-collaboration-library',
          type: 'node-red-library-file-store',
          path: '/home/hbis/.node-red/node-red-library-file-store',
          label: 'Team collaboration',
          icon: 'font-awesome/fa-users',
        },
      ],
    },
  },

  1. 安装模块
npm install @node-red/library-file-store
  1. 接口调用
Get:http://127.0.0.1:1880/library/team-collaboration-library/flows
  1. 结果

请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值