JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q30273F
  • License MIT

Protobuf lite runtime for sisyphus project on js platform, only support JSON format.

Package Exports

  • @sisyphus.js/runtime
  • @sisyphus.js/runtime/lib/google/protobuf/any
  • @sisyphus.js/runtime/lib/google/protobuf/any.js
  • @sisyphus.js/runtime/lib/google/protobuf/api
  • @sisyphus.js/runtime/lib/google/protobuf/api.js
  • @sisyphus.js/runtime/lib/google/protobuf/descriptor
  • @sisyphus.js/runtime/lib/google/protobuf/descriptor.js
  • @sisyphus.js/runtime/lib/google/protobuf/duration
  • @sisyphus.js/runtime/lib/google/protobuf/duration.js
  • @sisyphus.js/runtime/lib/google/protobuf/empty
  • @sisyphus.js/runtime/lib/google/protobuf/empty.js
  • @sisyphus.js/runtime/lib/google/protobuf/field_mask
  • @sisyphus.js/runtime/lib/google/protobuf/field_mask.js
  • @sisyphus.js/runtime/lib/google/protobuf/source_context
  • @sisyphus.js/runtime/lib/google/protobuf/source_context.js
  • @sisyphus.js/runtime/lib/google/protobuf/struct
  • @sisyphus.js/runtime/lib/google/protobuf/struct.js
  • @sisyphus.js/runtime/lib/google/protobuf/timestamp
  • @sisyphus.js/runtime/lib/google/protobuf/timestamp.js
  • @sisyphus.js/runtime/lib/google/protobuf/type
  • @sisyphus.js/runtime/lib/google/protobuf/type.js
  • @sisyphus.js/runtime/lib/google/protobuf/wrappers
  • @sisyphus.js/runtime/lib/google/protobuf/wrappers.js
  • @sisyphus.js/runtime/lib/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@sisyphus.js/runtime) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@sisyphus.js/runtime

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

基础类型映射

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

Protobuf TypeScript
bool boolean
double number
float number
int32 number
uint32 number
sint32 number
fixed32 number
sfixed32 number
int64 number | string
uint64 number | string
sint64 number | string
fixed64 number | string
sfixed64 number | string
string string
bytes base64 encoded string

Json 类型映射

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

Protobuf Typescript Comment
google.protobuf.Any {'@type': '<typeUrl>', ...fields} 为消息体额外添加一个 @type 字段,如果消息有特殊的 mapping 规则,则会采用 {'@type': '<typeUrl>', value: <value>}
google.protobuf.Duration "${number}s" 一个以 s 结尾的数字字符串
google.protobuf.FieldMask string 一个以 , 分割的字符串
google.protobuf.Timestamp string ISO 标准的时间字符串
google.protobuf.DoubleValue number
google.protobuf.FloatValue number
google.protobuf.Int32Value number
google.protobuf.UInt32Value number
google.protobuf.Int64Value number | string
google.protobuf.UInt64Value number | string
google.protobuf.BoolValue boolean
google.protobuf.StringValue string
google.protobuf.BytesValue base64 encoded string
google.protobuf.Value json value
google.protobuf.ListValue json array
google.protobuf.Struct json object
google.protobuf.NullValue null

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())