2.3.0 • Published 2 years ago

@sisyphus.js/runtime v2.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@sisyphus.js/runtime

npm.io

sisyphus.js 核心运行时,用于仅支持 Json 接口访问的代码生成,包含预编译的 well-known protos 与一些基础类型定义。

基础类型映射

sisyphus.js 将基础类型映射成常用 TypeScript 类型,参考下表:

ProtobufTypeScript
boolboolean
doublenumber
floatnumber
int32number
uint32number
sint32number
fixed32number
sfixed32number
int64number | string
uint64number | string
sint64number | string
fixed64number | string
sfixed64number | string
stringstring
bytesbase64 encoded string

Json 类型映射

sisyphus.js 还支持 Json 映射标准,将一些特殊消息类型映射为特殊的 TypeScript 类型。

ProtobufTypescriptComment
google.protobuf.Any{'@type': '<typeUrl>', ...fields}为消息体额外添加一个 @type 字段,如果消息有特殊的 mapping 规则,则会采用 {'@type': '<typeUrl>', value: <value>}
google.protobuf.Duration"${number}s"一个以 s 结尾的数字字符串
google.protobuf.FieldMaskstring一个以 , 分割的字符串
google.protobuf.TimestampstringISO 标准的时间字符串
google.protobuf.DoubleValuenumber
google.protobuf.FloatValuenumber
google.protobuf.Int32Valuenumber
google.protobuf.UInt32Valuenumber
google.protobuf.Int64Valuenumber | string
google.protobuf.UInt64Valuenumber | string
google.protobuf.BoolValueboolean
google.protobuf.StringValuestring
google.protobuf.BytesValuebase64 encoded string
google.protobuf.Valuejson value
google.protobuf.ListValuejson array
google.protobuf.Structjson object
google.protobuf.NullValuenull

Flow API

sisyphus.js 还定义了基于 Promise 的 Flow<T> 接口,用于 streaming API。

const serverFlow: Flow<EchoResponse> = echo.chat(flow<EchoRequest>(async emitter => {
    emitter({
        content: "hi!"
    })
    emitter({
        content: "we are chatting by sisyphus flow!"
    })
}))

await collect(serverFlow, async it => {
    console.log(it)
})

工具方法

sisyphus.js 也为一些 well-known 类型提供了额外的拓展与工具方法。

Any

import {Any} from '@sisyphus.js/runtime'

if (Any.isAny(anyMessage)) {
    // 判断一个消息实体是否是 Any 的包装类型

    switch (Any.typeOf(anyMessage)) {
        case 'google.protobuf.Duration':
            // 获取 Any 包装的消息实体全名
            break
    }
}

Base64

import {base64Decode, base64Encode} from '@sisyphus.js/runtime'

const buff = base64Encode(base64Decode("SGVsbG8sIPCfjI0="))

Duration

import {Duration} from '@sisyphus.js/runtime'

const [secounds, nanos] = Duration.toPayload("1.2s") // 将 Duration 字符串转化为整数秒与整数纳秒
const duration = Duration.fromPayload([secounds, nanos])

FieldMask

import {FieldMask} from '@sisyphus.js/runtime'

const [content, status] = FieldMask.toPayload("content, status") // 将 FieldMask 字符串转化为字符串数组
const mask = FieldMask.fromPayload([content, status])

Timestamp

import {Timestamp} from '@sisyphus.js/runtime'

const [secounds, nanos] = Timestamp.toPayload("2021-06-24T13:45:86Z") // 将 Timestamp 字符串转化为整数秒与整数纳秒
const timestamp = Timestamp.fromPayload([secounds, nanos])
const now = Timestamp.now()
const fromDate = Timestamp.fromDate(new Date())
2.3.0

2 years ago

2.1.2

2 years ago

2.2.0

2 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.1.6

2 years ago

2.1.5

2 years ago

2.1.1

2 years ago

2.0.7

2 years ago

2.0.8

2 years ago

2.1.0

2 years ago

2.0.5

3 years ago

2.0.6

3 years ago

2.0.4

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago