Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Support for CBOR | MessagePack #514

Closed
sinclairzx81 opened this issue Feb 20, 2023 · 5 comments
Closed

Feature Request: Support for CBOR | MessagePack #514

sinclairzx81 opened this issue Feb 20, 2023 · 5 comments
Assignees
Labels
good first issue Good for newcomers hacktoberfest help wanted Extra attention is needed

Comments

@sinclairzx81
Copy link
Contributor

Feature Request

As Typia is looking to support Protocol Buffers, was wondering if similar codecs would be possible for either CBOR RFC 8949 or MessagePack.

Both CBOR and MessagePack provide compact JSON binary serialization, and whose data structures may be a bit more representative of types expressible in structural languages like TypeScript (as it's just binary JSON). Also, as Typia would have forward knowledge of types, there may be potential to implement extremely fast encode() / decode() that wouldn't be possible using plain JavaScript implementations.

Primary Usecase

  • Faster Encode / Decode of Binary messages over Web Sockets

Reference Links

https://msgpack.org/index.html
https://cbor.io/

Current Implementations

https://www.npmjs.com/package/@msgpack/msgpack
https://www.npmjs.com/package/cbor

For consideration.

@samchon
Copy link
Owner

samchon commented Feb 20, 2023

Message Pack seems not hard to implement, but I need to rename Protocol Buffer related functions before.

I'll try this feature, but cannot sure when.

@samchon samchon self-assigned this Feb 20, 2023
@samchon samchon added enhancement New feature or request documentation Improvements or additions to documentation labels Feb 20, 2023
@samchon
Copy link
Owner

samchon commented Feb 24, 2023

If you don't have enough time to wait next Protocol Buffer implementation, you can send a PR and I'll review it with pleasure.

I saw spec documents of MessagePack and it seems good to challenge. You can reference how stringify() be implemented when developing msgpackEncode() function. About the msgpackDecode() function, I think no merit to implement it in typia and just using @msgpack/msgpack seems better.

About CBOR... well, it seems so hard and would better to wait next Protocol Buffer implementation.

@sinclairzx81
Copy link
Contributor Author

sinclairzx81 commented Feb 24, 2023

@samchon Heya, there's no urgency on this, just thought it might be a cool feature, particularly if you can get faster encode performance through type introspection. Was wondering tho, perhaps instead of implementing msgpack internal to typia, maybe a plugin system might be more flexible (and easier to implement)

Plugins

Given there's many many different encoding schemes (json, xml, protobuf, msgpack, bson, cbor, csv, etc), perhaps something like the following might be good way to allow users to plugin any encoder but still get the type assertions.

import typia from 'typia'
import * as msgpack from '@msgpack/msgpack' 

typia.codec('msgpack', { // or json, xml, csv, maybe protobuf (would need to figure out the IDL for it)
  encode: (data) => msgpack.encode(data),
  decode: (data) => msgpack.decode(data)
})

const encoded = typia.encode<T>('msgpack', data)
const decoded = typia.decode<T>('msgpack', encoded)

Encode

const encoded = typia.encode<string>('msgpack', 'hello')

Transpiles to

const encoded = (codec, value) => {
   // assert BEFORE encode
   if(!(typeof value === 'string')) throw Error('todo: diagnostics')
   const codec = typia.get_codec(codec)
   return codec.encode(data)
}('msgpack', 'hello')

Decode

const decoded = typia.decode<string>('msgpack', encoded)

Transpiles to

const decoded = (codec, encoded) => {
   const codec = typia.get_codec(codec)
   const value = codec.decode(encoded)
   // assert AFTER decode
   if(!(typeof value === 'string')) throw Error('todo: diagnostics')
}('msgpack', encoded)

Usecase

Possible fast check / verify for http request bodies (for varying content mime types)

typia.codec('application/json', {
  encode: (data) => {...},
  decode: (data) => {...}
})
typia.codec('application/msgpack', {
  encode: (data) => {...},
  decode: (data) => {...}
})
typia.codec('text/xml', {
  encode: (data) => {...},
  decode: (data) => {...}
})
function decodeBody<T>(request: HttpRequest): T {
 return typia.decode<T>(request.headers.contentType, request.body)
}

This may also help remove the need for explicit methods like encodeMsgPack() and encodeProtoBuf(), and just express them uniformly via plugins.

@samchon
Copy link
Owner

samchon commented Aug 29, 2023

https://github.com/samchon/typia/releases/tag/v5.0.0

Completed to implement Protocol Buffer functions.

Will try this feature at end of this year. If someone who wants to try it earlier than me, it is okay.

@samchon samchon added help wanted Extra attention is needed good first issue Good for newcomers hacktoberfest and removed documentation Improvements or additions to documentation enhancement New feature or request labels Jan 17, 2024
@samchon
Copy link
Owner

samchon commented Jan 19, 2024

Close as no one had challenged.

@samchon samchon closed this as not planned Won't fix, can't repro, duplicate, stale Jan 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers hacktoberfest help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants