
Security News
Bun 1.2.19 Adds Isolated Installs for Better Monorepo Support
Bun 1.2.19 introduces isolated installs for smoother monorepo workflows, along with performance boosts, new tooling, and key compatibility fixes.
suanpan_node_sdk
Advanced tools
算盘组件之间通过 Redis 消息队列进行消息通信,SDK 对 Redis 提供的 Stream API 进行了封装,提供了发送消息/对象存储等方法。
Make sure you have Node 10 or greater installed. Get the library:
$ npm install suanpan_node_sdk --save
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);
获取配置参数,以及内置一些常用参数,例如:AppId,NodeId 等
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 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.
requestId
:An identifier for this message instance.timestamp
:The time the message was created. Changes each time a message is mutated.{ 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
提供一个静态方法 onMessage
来处理发送的消息。
parameters:
example 参照 Quick Start
消息双向管道工厂类,MessageChannelFactory
提供一个静态方法 createMessageChannel
返回 MessageChannel
消息双向管道实例。
return:
example:
import { MessageChannelFactory } from 'suanpan_node_sdk';
(async () => {
const channel = MessageChannelFactory.createMessageChannel();
channel.receiveMessage().then(message => {
/** Business logic here */
});
})();
消息双向通道接口,支撑流计算消息的接收和发送。不支持用户显示创建,仅能从 MessageChannelFactory 创建
接收消息,用户可以返回的 Message 自定义消息的处理逻辑。 return:
parameters:
MessageHandler 是 Node SDK 定义的消息处理程序(此处仅表示函数类型),它作为回调函数注册到 channel 中。函数定义如下:
type MessageHandler = (message: Message, chain: MessageChain) => void | Promise<void>;
函数参数 parameters 定义中包含:
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] }
});
}
消息链,用于发送同源消息(组件的同步/异步机制)
headers
和 payload
两部分parameters:
{ 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 })
})();
消息建造者。双向消息管道传输媒介 Message 的建造者,并提供多种静态方法帮助用户构建自定义消息。
提取并仅保留 message payload,返回当前建造者实例。
parameters:
return:
提取并仅保留 message headers,返回当前建造者实例。
parameters:
return:
添加消息 payload,返回当前建造者实例。
parameters:
return:
添加消息 header,返回当前建造者实例。
parameters:
return:
用法同 .setHeader,消息 header key'name 不存在时添加
生成消息 Message 实例。 return:
Storage 是对对象存储(目前支持 Aliyun OSS以及开源 Minio)API 的封装。
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:
example:
import { Storage } from 'suanpan_node_sdk';
(async () => {
await Storage.Instance.append('studio/userId/appId/nodeId/log.txt', 'redis subscribed.');
})();
Uploads an object from a stream/Buffer/string. parameters:
example:
import { Storage } from 'suanpan_node_sdk';
(async () => {
await Storage.Instance.putObject('studio/userId/appId/nodeId/data.js', 'const f = () => { console.log("a")}');
})();
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 1000example:
import { Storage, ObjectItem } from 'suanpan_node_sdk';
(async () => {
const objects: ObjectItem[] = await Storage.Instance.listObjects('studio/userId/appId/nodeId/', true, 200);
})();
Downloads and saves the object as a file in the local filesystem. parameters:
objectName {string}
- Name store on OSSfilePath {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');
})();
Uploads contents from a file to objectName. parameters:
objectName {string}
- Name store on OSSfilePath {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');
})();
Removes an object. If delete object not exists, will also delete success. parameters:
objectName {string}
- Name store on OSSexample:
import { Storage } from 'suanpan_node_sdk';
(async () => {
await Storage.Instance.deleteObject('studio/userId/appId/nodeId/coco.ts');
})();
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);
})();
Check whether the object exists,Returns true if it exists, otherwise false parameters:
objectName {string}
- Name store on OSSexample:
import { Storage } from 'suanpan_node_sdk';
(async () => {
const exist = await Storage.Instance.checkObjectNameExist('studio/not.json');
if (exist) {
// do something
}
})();
Get an object to JSON object parameters:
objectName {string}
- Name store on OSSexample:
import { Storage } from 'suanpan_node_sdk';
(async () => {
const json = await Storage.Instance.getObjectToJSON('studio/not.json');
})();
Get an object to string. parameters:
objectName {string}
- Name store on OSSexample:
import { Storage } from 'suanpan_node_sdk';
(async () => {
const str = await Storage.Instance.getObjectToString('studio/log.txt');
})();
Get an object to Buffer. parameters:
objectName {string}
- Name store on OSSexample:
import { Storage } from 'suanpan_node_sdk';
(async () => {
const buf = await Storage.Instance.getObjectToBuffer('studio/log.txt');
})();
Get an object to read stream.
parameters:
objectName {string}
- Name store on OSSexample:
import { Storage } from 'suanpan_node_sdk';
(async () => {
const stream = await Storage.Instance.getObjectToStream('studio/demo.txt');
stream.pipe(fs.createWriteStream('some file.txt'));
})();
对象存储路径工具类。
参数名 | 参数类型 | 是否必填 | 说明 |
---|---|---|---|
userId | string | 否 | 用户 Id |
appId | string | 否 | 项目 Id |
componentId | string | 否 | 组件 Id |
nodeId | string | 否 | 节点 Id |
参数名 | 参数类型 | 是否必填 | 说明 |
---|---|---|---|
userId | string | 否 | 默认使用当前用户 Id |
参数名 | 参数类型 | 是否必填 | 说明 |
---|---|---|---|
appId | string | 否 | 默认使用当前项目Id |
参数名 | 参数类型 | 是否必填 | 说明 |
---|---|---|---|
componentId | string | 否 | 默认使用当前组件 Id |
参数名 | 参数类型 | 是否必填 | 说明 |
---|---|---|---|
nodeId | string | 否 | 默认使用当前节点 Id |
参数名 | 参数类型 | 是否必填 | 说明 |
---|---|---|---|
userId | string | 否 | 默认使用当前用户 Id |
appId | string | 否 | 默认使用当前项目Id |
参数名 | 参数类型 | 是否必填 | 说明 |
---|---|---|---|
userId | string | 否 | 默认使用当前用户 Id |
appId | string | 否 | 默认使用当前项目 Id |
nodeId | string | 否 | 默认使用当前节点 Id |
参数名 | 参数类型 | 是否必填 | 说明 |
---|---|---|---|
userId | string | 否 | 默认使用当前用户 Id |
appId | string | 否 | 默认使用当前项目 Id |
对象存储日志收集器
属性名 | 类型 | 是否必填 | 说明 |
---|---|---|---|
storagePath | string | 是 | 完整对象存储路径 objectName,例如:studio/userId/appId/nodeId/log-2021-1014.txt |
console | boolean | 否 | 默认 false。开启后日志将会 print 到标准输入输出 |
level | Loglevel | 否 | 默认 1 (debug)。日志等级 |
maxItems | number | 否 | 默认 1。表示缓存长度。当日志积累条数 >= maxItems 时,将会触发 flush 操作,将日志上传到对象存储。 |
flushInterval | number | 否 | 默认 0,单位 ms。即实时将日志保存到对象存储中。如果想控制每隔 1s 钟收集一次。设置其值为:1000 |
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');
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);
export interface MessageHeaders {
requestId: string;
timestamp: number;
extra: JSONObject;
}
export enum LogLevel {
DEBUG = 1,
INFO = 2,
WARN = 3,
ERROR = 4
}
type StorageLoggerOptions = {
storagePath: string;
console?: boolean;
level?: LogLevel;
maxItems?: number;
flushInterval?: number;
};
enum Platform {
WINDOWS = 0,
MACINTOSH = 1,
LINUX = 2
}
FAQs
suanpan sdk for node.js
The npm package suanpan_node_sdk receives a total of 124 weekly downloads. As such, suanpan_node_sdk popularity was classified as not popular.
We found that suanpan_node_sdk demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2.19 introduces isolated installs for smoother monorepo workflows, along with performance boosts, new tooling, and key compatibility fixes.
Security News
Popular npm packages like eslint-config-prettier were compromised after a phishing attack stole a maintainer’s token, spreading malicious updates.
Security News
/Research
A phishing attack targeted developers using a typosquatted npm domain (npnjs.com) to steal credentials via fake login pages - watch out for similar scams.