2.2.6 • Published 2 months ago

suanpan_node_sdk v2.2.6

Weekly downloads
679
License
-
Repository
github
Last release
2 months ago

Table of contents

Introduction

算盘组件之间通过 Redis 消息队列进行消息通信,SDK 对 Redis 提供的 Stream API 进行了封装,提供了发送消息/对象存储等方法。 image.png

1.x 版本文档

SDK 目录约定

Install

Make sure you have Node 10 or greater installed. Get the library:

$ npm install suanpan_node_sdk --save

Quick Start

import { MessageListener, MessageHandler } from 'suanpan_node_sdk';

const messageHandler: MessageHandler = (message, chain) => {
  const headers = message.headers;
  const payload = message.payload;

  const requestId = headers.requestId;
  const timestamp = headers.timestamp;

  /** Business logic here */

  chain.send({ // 发送消息
    out1: 'xxxx',
    out2: true,
    out3: { success: true, result: [1, 2, 3] }
  });
}

MessageListener.onMessage(messageHandler);

Features

API Reference

Parameter

获取配置参数,以及内置一些常用参数,例如:AppId,NodeId 等

.get(key)

Parameter Class 提供一个静态方法 .get([key]) 获取配置参数。

parameters:

  • key {string}:参数名称。如果 key 为空,则获取“全部”配置参数。

example:

import { Parameter, JSONObject } from 'suanpan_node_sdk';
/**
 * 内置参数
 */
const userId = Parameter.UserId
const appId = Parameter.AppId;
const nodeId = Parameter.NodeId;
const componentId = Parameter.ComponentId;

/**
 * 获取“单个”配置参数
 */
const paramValue = Parameter.get<string>('paramKey');

/**
 * 获取“全部”配置参数
 */
const params = Parameter.get<JSONObject>();

Message

Message entity object. The constructor includes headers and payload. The message’s payload Object can not be set after the initial creation. However, the mutability of the header values themselves (or the payload Object) is intentionally left as a decision for the framework user.

constructor(headers, payload)

headers
  • headers { MessageHeaders } - Represents a collection of message headers for a message. The built-in read-only headers are:
    • requestId:An identifier for this message instance.
    • timestamp:The time the message was created. Changes each time a message is mutated.
payload
  • payload {Object} - The data type of payload should strictly comply with the input / output port conventions of suanpan, such as: { in1: 111 }, { out1: 222 }. The content wrapped by the message can be any Javascript type, and because the interface is parameterized, you can retrieve the payload in a type-safe fashion.

example:

import { Message } from 'suanpan_node_sdk';

const customHeader = {
  'x-stream-id': 'xxxx'
};

const payload = {
	out1: 100
};
const message = new Message(customHeader, payload);

MessageListener

流计算消息监听类,MessageListener 提供一个静态方法 onMessage 来处理发送的消息。

.onMessage(messageHandler)

parameters:

example 参照 Quick Start

MessageChannelFactory

消息双向管道工厂类,MessageChannelFactory 提供一个静态方法 createMessageChannel 返回 MessageChannel 消息双向管道实例。

.createMessageChannel()

return:

example:

import { MessageChannelFactory } from 'suanpan_node_sdk';

(async () => {
	const channel = MessageChannelFactory.createMessageChannel();
  channel.receiveMessage().then(message => {
    /** Business logic here */
  });
})();

MessageChannel

消息双向通道接口,支撑流计算消息的接收和发送。不支持用户显示创建,仅能从 MessageChannelFactory 创建

.receiveMessage()

接收消息,用户可以返回的 Message 自定义消息的处理逻辑。 return:

.sendMessage(message)

parameters:

MessageHandler

MessageHandler 是 Node SDK 定义的消息处理程序(此处仅表示函数类型),它作为回调函数注册到 channel 中。函数定义如下:

type MessageHandler = (message: Message, chain: MessageChain) => void | Promise<void>;

函数参数 parameters 定义中包含:

  • message { Message } - 传递的消息实体
  • chain { MessageChain } - 消息链,用于发送同源消息(组件的同步/异步机制)

example:

import { MessageHandler } from 'suanpan_node_sdk';

const messageHandler: MessageHandler = (message, chain) => {
  const headers = message.headers;
  const payload = message.payload;

  const requestId = headers.requestId;
  const timestamp = headers.timestamp;

  /** Business logic here */

  chain.send({ // 发送消息
    out1: 'xxxx',
    out2: true,
    out3: { success: true, result: [1, 2, 3] }
  });
}

MessageChain

消息链,用于发送同源消息(组件的同步/异步机制)

constructor

  • message { Message} - 消息实体,包含 headerspayload 两部分
  • channel { MessageChannel } - 消息传输双向管道

.send(payload)

parameters:

  • payload { Object } - The data type of payload should strictly comply with the output port conventions of suanpan, such as: { out1: 111 }, { out2: 222 }

example:

import { MessageChain, MessageBuilder, MessageChannelFactory } from 'suanpan_node_sdk';

(async () => {

  const requestMessage = MessageBuilder
    .withPayload({ in1: 'sss'})
    .setHeaderIfAbsent('requestId', 'uuid')
    .setHeaderIfAbsent('x-stream-id', '12345')
    .build();

  const messageChannel = MessageChannelFactory.createMessageChannel();
  const chain = new MessageChain(requestMessage, messageChannel);

  chain.send({ out1: 1234 })
})();

MessageBuilder

消息建造者。双向消息管道传输媒介 Message 的建造者,并提供多种静态方法帮助用户构建自定义消息。

.fromMessagePayload(message)

提取并仅保留 message payload,返回当前建造者实例。

parameters:

return:

.fromMessageHeaders(message)

提取并仅保留 message headers,返回当前建造者实例。

parameters:

return:

.withPayload(payload)

添加消息 payload,返回当前建造者实例。

parameters:

return:

.setHeader(key, value)

添加消息 header,返回当前建造者实例。

parameters:

  • key {string} - header key's name
  • value {string} - header key's value

return:

.setHeaderIfAbsent(key, value)

用法同 .setHeader,消息 header key'name 不存在时添加 ​

.build()

生成消息 Message 实例。 return:

Storage

Storage 是对对象存储(目前支持 Aliyun OSS以及开源 Minio)API 的封装。

.append(objectName, content)

Append an object to the bucket, it's almost same as putObject, but it can add content to existing object rather than override it. parameters:

  • objectName {string} - Name store on OSS
  • content {string} - What would you like to add to bucket

example:

import { Storage } from 'suanpan_node_sdk';

(async () => {
  await Storage.Instance.append('studio/userId/appId/nodeId/log.txt', 'redis subscribed.');
})();

.putObject(objectName, data)

Uploads an object from a stream/Buffer/string. parameters:

  • objectName {string} - Name store on OSS
  • data {String | Buffer | ReadStream} - object data

example:

import { Storage } from 'suanpan_node_sdk';

(async () => {
  await Storage.Instance.putObject('studio/userId/appId/nodeId/data.js', 'const f = () => { console.log("a")}');
})();

.listObjects(prefix, recursive)

List objects in the bucket. parameters:

  • prefix {string} - Name prefix store on OSS,such as studio/userId/appId/nodeId
  • recursive {boolean} - Default:true。Recursively find all objects under “prefix”
  • maxKeys {Number} - Max objects, default is 100, limit to 1000

example:

import { Storage, ObjectItem } from 'suanpan_node_sdk';

(async () => {
  const objects: ObjectItem[] = await Storage.Instance.listObjects('studio/userId/appId/nodeId/', true, 200);
})();

.fGetObject(objectName, filePath)

Downloads and saves the object as a file in the local filesystem. parameters:

  • objectName {string} - Name store on OSS
  • filePath {string} - Path on the local filesystem to which the object data will be written.

example:

import { Storage } from 'suanpan_node_sdk';

(async () => {
  await Storage.Instance.fGetObject('studio/userId/appId/nodeId/coco.ts', '/tmp/coco.ts');
})();

.fPutObject(objectName, filePath)

Uploads contents from a file to objectName. parameters:

  • objectName {string} - Name store on OSS
  • filePath {string} - Path of the file to be uploaded.

example:

import { Storage } from 'suanpan_node_sdk';

(async () => {
  await Storage.Instance.fPutObject('studio/userId/appId/nodeId/coco.ts', '/tmp/coco.ts');
})();

.deleteObject(objectName)

Removes an object. If delete object not exists, will also delete success. parameters:

  • objectName {string} - Name store on OSS

example:

import { Storage } from 'suanpan_node_sdk';

(async () => {
  await Storage.Instance.deleteObject('studio/userId/appId/nodeId/coco.ts');
})();

.deleteMultiObjects(objectNames)

Removes multiple objects. parameters:

  • objectNames {string[]} - List of objects in the bucket to be removed. such as['objectname1','objectname2']

example:

import { Storage } from 'suanpan_node_sdk';

(async () => {
  const objectList = ['studio/userId/appId/nodeId/coco1.ts', 'studio/userId/appId/nodeId/coco2.ts'];
  await Storage.Instance.deleteMultiObjects(objectList);
})();

.checkObjectNameExist(objectName)

Check whether the object exists,Returns true if it exists, otherwise false parameters:

  • objectName {string} - Name store on OSS

example:

import { Storage } from 'suanpan_node_sdk';

(async () => {
	const exist = await Storage.Instance.checkObjectNameExist('studio/not.json');
  if (exist) {
  	// do something
  }
})();

.getObjectToJSON(objectName)

Get an object to JSON object parameters:

  • objectName {string} - Name store on OSS

example:

import { Storage } from 'suanpan_node_sdk';

(async () => {
	const json = await Storage.Instance.getObjectToJSON('studio/not.json');
})();

.getObjectToString(objectName)

Get an object to string. parameters:

  • objectName {string} - Name store on OSS

example:

import { Storage } from 'suanpan_node_sdk';

(async () => {
	const str = await Storage.Instance.getObjectToString('studio/log.txt');
})();

.getObjectToBuffer(objectName)

Get an object to Buffer. parameters:

  • objectName {string} - Name store on OSS

example:

import { Storage } from 'suanpan_node_sdk';

(async () => {
	const buf = await Storage.Instance.getObjectToBuffer('studio/log.txt');
})();

.getObjectToStream(objectName)

Get an object to read stream.

parameters:

  • objectName {string} - Name store on OSS

example:

import { Storage } from 'suanpan_node_sdk';

(async () => {
	const stream = await Storage.Instance.getObjectToStream('studio/demo.txt');
	stream.pipe(fs.createWriteStream('some file.txt'));
})();

StoragePath

对象存储路径工具类。

constructor(options)

参数名参数类型是否必填说明
userIdstring用户 Id
appIdstring项目 Id
componentIdstring组件 Id
nodeIdstring节点 Id

.getUserPath(userId)

参数名参数类型是否必填说明
userIdstring默认使用当前用户 Id

.getAppPath(appId)

参数名参数类型是否必填说明
appIdstring默认使用当前项目Id

.getComponentPath(componentId)

参数名参数类型是否必填说明
componentIdstring默认使用当前组件 Id

.getNodePath(nodeId)

参数名参数类型是否必填说明
nodeIdstring默认使用当前节点 Id

.getAppLogPath(userId)

参数名参数类型是否必填说明
userIdstring默认使用当前用户 Id
appIdstring默认使用当前项目Id

.getNodeLogPath(userId, nodeId)

参数名参数类型是否必填说明
userIdstring默认使用当前用户 Id
appIdstring默认使用当前项目 Id
nodeIdstring默认使用当前节点 Id

.getAppTempDir(userId)

参数名参数类型是否必填说明
userIdstring默认使用当前用户 Id
appIdstring默认使用当前项目 Id

ObjectStorageLogger

对象存储日志收集器

constructor(options)

属性名类型是否必填说明
storagePathstring完整对象存储路径 objectName,例如:studio/userId/appId/nodeId/log-2021-1014.txt
consoleboolean默认 false。开启后日志将会 print 到标准输入输出
levelLoglevel默认 1 (debug)。日志等级
maxItemsnumber默认 1。表示缓存长度。当日志积累条数 >= maxItems 时,将会触发 flush 操作,将日志上传到对象存储。
flushIntervalnumber默认 0,单位 ms。即实时将日志保存到对象存储中。如果想控制每隔 1s 钟收集一次。设置其值为:1000

.info(message, flush)

.debug(message, flush)

.warn(message, flush)

.error(message, flush)

parameters:

  • message {string} - 日志内容
  • flush {boolean} - 默认:false。flush 为 true 时将会立即刷新日志到对象存储。

example:

import { StoragePath, ObjectStorageLogger } from 'suanpan_node_sdk';

const storagePath = new StoragePath();

const storageLogger = new ObjectStorageLogger({
  storagePath: storagePath.getNodeLogPath() + '/log.txt'
});

storageLogger.info('append this log');

EventLogger

  • EventLogger 提供日志的结构化存储(持久化在数据库中),模版释放后仍然保留,用于不同用户查看修改错误,用户重新部署后清除。logkit 能够直接运行在算盘 Server 中,也可以单独运行。
  • EventLogger(内部使用 socketio 作为接口的基础协议)单向传输到 logkit。用户可以自己将业务日志通过 EventLogger 发送到算盘 logkit。
  • EventLogger 默认情况下:用户代码(MessageHandler)运行时 Error,将会自动触发 EventLogger 的 error 事件,将结构化错误信息发送到 logkit。

.info(message)

.debug(message)

.warn(message)

.error(message)

parameters:

  • message {string} - logkit 日志内容

example:

import { EventLogger, MessageListener, MessageHandler } from 'suanpan_node_sdk';

const messageHandler: MessageHandler = (message, chain) => {
  const headers = message.headers;
  const payload = message.payload;

  const requestId = headers.requestId;
  const timestamp = headers.timestamp;
  
  if(!payload.in1) {
    EventLogger.Instance.error('received message is empty.');
    return;
  }

  chain.send({ // 发送消息
    out1: 'xxxx',
    out2: true,
    out3: { success: true, result: [1, 2, 3] }
  });
}

MessageListener.onMessage(messageHandler);

Type Reference

MessageHeaders

export interface MessageHeaders {
  requestId: string;
  timestamp: number;
  extra: JSONObject;
}

LogLevel

export enum LogLevel {
  DEBUG = 1,
  INFO = 2,
  WARN = 3,
  ERROR = 4
}

StorageLoggerOptions

type StorageLoggerOptions = {
    storagePath: string;
    console?: boolean;
    level?: LogLevel;
    maxItems?: number;
    flushInterval?: number;
};

Platform

enum Platform {
    WINDOWS = 0,
    MACINTOSH = 1,
    LINUX = 2
}
2.2.6

2 months ago

2.2.5

2 months ago

2.2.5-beta

2 months ago

3.1.0-beta

1 year ago

3.1.0

1 year ago

3.0.4

1 year ago

3.0.3

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

1.5.16

1 year ago

1.5.15

1 year ago

3.1.0-beta-1

1 year ago

3.0.0-beta.1

2 years ago

3.0.0-beta.3

2 years ago

3.0.0-beta.2

2 years ago

3.0.0-beta.4

2 years ago

3.0.0

2 years ago

2.3.0-beta.6

2 years ago

2.3.0-beta.7

2 years ago

2.3.0-beta.4

2 years ago

2.3.0-beta.5

2 years ago

2.3.0-beta.2

2 years ago

2.3.0-beta.3

2 years ago

2.3.0-beta

2 years ago

2.1.5-beta.1

2 years ago

2.2.1

2 years ago

2.2.0

2 years ago

2.2.3

2 years ago

2.2.2

2 years ago

2.2.4

2 years ago

2.1.5

2 years ago

2.3.0-beta.1

2 years ago

1.5.12

2 years ago

1.5.11

2 years ago

1.5.14

2 years ago

1.5.13

2 years ago

2.1.0-beta.1

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.1.0

2 years ago

2.1.0-beta

2 years ago

2.0.10

2 years ago

2.0.9

2 years ago

1.5.10

2 years ago

2.0.8

2 years ago

1.5.9

2 years ago

2.0.3

2 years ago

2.0.5

2 years ago

2.0.4

2 years ago

2.0.7

2 years ago

2.0.6

2 years ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

2.0.0-beta.33

2 years ago

2.0.0-beta.32

2 years ago

2.0.0-beta.31

2 years ago

2.0.0-beta.30

2 years ago

2.0.0-beta.37

2 years ago

2.0.0-beta.36

2 years ago

2.0.0-beta.35

2 years ago

2.0.0-beta.34

2 years ago

2.0.0-beta.38

2 years ago

2.0.0-beta.22

2 years ago

2.0.0-beta.26

2 years ago

2.0.0-beta.25

2 years ago

2.0.0-beta.24

2 years ago

2.0.0-beta.23

2 years ago

2.0.0-beta.29

2 years ago

2.0.0-beta.28

2 years ago

2.0.0-beta.27

2 years ago

2.0.0-beta.21

3 years ago

2.0.0-beta.20

3 years ago

2.0.0-beta.19

3 years ago

2.0.0-beta.18

3 years ago

2.0.0-beta.17

3 years ago

2.0.0-beta.16

3 years ago

2.0.0-beta.15

3 years ago

2.0.0-beta.14

3 years ago

2.0.0-beta.13

3 years ago

2.0.0-beta.12

3 years ago

2.0.0-beta.9

3 years ago

2.0.0-beta.8

3 years ago

2.0.0-beta.11

3 years ago

2.0.0-beta.10

3 years ago

2.0.0-beta.7

3 years ago

2.0.0-beta.6

3 years ago

2.0.0-beta.5

3 years ago

2.0.0-beta.4

3 years ago

2.0.0-beta.3

3 years ago

2.0.0-beta.2

3 years ago

2.0.0-beta.1

3 years ago

2.0.0-beta

3 years ago

1.5.8-trap-api-3

3 years ago

1.5.8-trap-api-2

3 years ago

1.5.8-trap-api-1

3 years ago

1.5.8-trap-api

3 years ago

1.5.8-port-api.5

3 years ago

1.5.8-port-api.6

3 years ago

1.5.8-port-api.4

3 years ago

1.5.8-port-api.3

3 years ago

1.5.8-port-api.2

3 years ago

1.5.8-port-api

3 years ago

1.5.8-port-api.1

3 years ago

1.5.7

3 years ago

1.5.6

3 years ago

1.5.5

3 years ago

1.5.4

3 years ago

1.5.3

3 years ago

1.5.2

3 years ago

1.5.1

3 years ago

1.5.0

3 years ago

1.5.2-beta-2

3 years ago

1.5.2-beta-1

3 years ago

1.5.2-beta-0

3 years ago

1.4.6

3 years ago

1.4.5

3 years ago

1.4.4

3 years ago

1.4.1-logkit.2

3 years ago

1.4.3

3 years ago

1.4.2

3 years ago

1.4.1-logkit.1

3 years ago

1.4.1-logkit

3 years ago

1.4.1-dev.2

3 years ago

1.4.1-dev.1

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.4.0-4

3 years ago

1.4.0-1

3 years ago

1.4.0-3

3 years ago

1.4.0-2

3 years ago

1.4.0-0

4 years ago

1.3.0

4 years ago

1.2.1-14

4 years ago

1.2.1-13

4 years ago

1.2.1-12

4 years ago

1.2.1-11

4 years ago

1.2.1-10

4 years ago

1.2.1-9

4 years ago

1.2.1-8

4 years ago

1.2.1-7

4 years ago

1.2.1-6

4 years ago

1.2.1-5

4 years ago

1.2.1-4

4 years ago

1.2.1-3

4 years ago

1.2.1-1

4 years ago

1.2.1-0

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.1.0-6

4 years ago

1.1.0-5

4 years ago

1.1.0-4

4 years ago

1.1.0-3

4 years ago

1.1.0-2

4 years ago

1.1.0-1

4 years ago

1.1.0-0

4 years ago

1.0.0

4 years ago

1.0.0-34

4 years ago

1.0.0-35

4 years ago

1.0.0-36

4 years ago

1.0.0-37

4 years ago

1.0.0-33

4 years ago

1.0.0-32

4 years ago

1.0.0-31

4 years ago

1.0.0-30

4 years ago

1.0.0-27

4 years ago

1.0.0-28

4 years ago

1.0.0-29

4 years ago

1.0.0-26

4 years ago

1.0.0-24

4 years ago

1.0.0-25

4 years ago

1.0.0-22

4 years ago

1.0.0-23

4 years ago

1.0.0-21

4 years ago

1.0.0-20

4 years ago

1.0.0-19

4 years ago

1.0.0-17

4 years ago

1.0.0-18

4 years ago

1.0.0-16

4 years ago

1.0.0-15

4 years ago

1.0.0-14

4 years ago

1.0.0-13

4 years ago

1.0.0-12

4 years ago

1.0.0-10

4 years ago

1.0.0-11

4 years ago

1.0.0-9

4 years ago

1.0.0-8

4 years ago

1.0.0-7

4 years ago

1.0.0-5

4 years ago

1.0.0-6

4 years ago

1.0.0-4

4 years ago

1.0.0-3

4 years ago

1.0.0-2

4 years ago

1.0.0-1

4 years ago

1.0.1-0

4 years ago

1.0.0-0

4 years ago

0.23.0

4 years ago

0.22.1

4 years ago

0.22.0

4 years ago

0.21.0

4 years ago

0.20.0

4 years ago

0.19.0

4 years ago

0.18.0

4 years ago

0.16.0

4 years ago

0.17.0

4 years ago

0.15.0

4 years ago

0.14.0

4 years ago

0.13.0

4 years ago

0.12.3

4 years ago

0.12.1

4 years ago

0.12.0

4 years ago

0.11.0

4 years ago

0.10.0

4 years ago

0.9.0

4 years ago

0.8.0

4 years ago

0.9.1

4 years ago

0.7.2

4 years ago

0.7.4

4 years ago

0.7.3

4 years ago

0.7.1

4 years ago

0.7.0

4 years ago

0.6.4

4 years ago

0.6.1

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.1

4 years ago

0.2.3

4 years ago

0.4.0

4 years ago

0.2.2

4 years ago

0.2.4

4 years ago

0.2.1

4 years ago