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

feat(cbor/unstable): Introduce @std/cbor #5909

Draft
wants to merge 34 commits into
base: main
Choose a base branch
from

Conversation

BlackAsLight
Copy link
Contributor

@BlackAsLight BlackAsLight commented Sep 4, 2024

Closes #5479

This pull request introduces a CBOR implementation based off the RFC 8949 spec from scratch. At the moment it only introduces encodeCbor/decodeCbor functions (which operate in a sync manner and therefore is limited to JavaScript's limitations on memory usage around variable sizes) and Byte, Text, Array, Map, and Sequence Encoder Streams for larger amounts of data or if wanting to work with indefinite length types. The decoding stream methods are yet to be committed to this pull request.

Example

import { assert, assertEquals } from "@std/assert";
import { decodeCbor, encodeCbor } from "@std/cbor";

const rawMessage = [
  "Hello World",
  35,
  0.5,
  false,
  -1,
  null,
  Uint8Array.from([0, 1, 2, 3]),
];

const encodedMessage = encodeCbor(rawMessage);
const decodedMessage = decodeCbor(encodedMessage);

assert(decodedMessage instanceof Array);
assertEquals(decodedMessage, rawMessage);

Copy link

codecov bot commented Sep 4, 2024

Codecov Report

Attention: Patch coverage is 67.44604% with 362 lines in your changes missing coverage. Please review.

Project coverage is 95.49%. Comparing base (6a4eb6c) to head (60ca9ae).

Files with missing lines Patch % Lines
cbor/decode_stream.ts 36.07% 217 Missing and 1 partial ⚠️
cbor/encode_stream.ts 62.87% 111 Missing ⚠️
cbor/encode.ts 89.17% 17 Missing ⚠️
cbor/decode.ts 95.08% 12 Missing ⚠️
cbor/_common.ts 93.75% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5909      +/-   ##
==========================================
- Coverage   96.28%   95.49%   -0.80%     
==========================================
  Files         496      502       +6     
  Lines       39605    40717    +1112     
  Branches     5841     6027     +186     
==========================================
+ Hits        38134    38881     +747     
- Misses       1429     1793     +364     
- Partials       42       43       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@github-actions github-actions bot added the cbor label Sep 5, 2024
@kt3k
Copy link
Member

kt3k commented Sep 5, 2024

I wonder if these need to be classes when they both don't have any instance states.

How about making them just deocde and encode functions? That mirrors the structure of std/msgpack

@BlackAsLight
Copy link
Contributor Author

I wonder if these need to be classes when they both don't have any instance states.

How about making them just deocde and encode functions? That mirrors the structure of std/msgpack

Sure. I was trying to mirror the structure of TextEncoder/TextDecoder, but this other structure seems more common in the std.

@kt3k
Copy link
Member

kt3k commented Sep 5, 2024

Side note: TextDecoder stores some chunk from the previous decode when stream option is specified. Probably that is why it's implemented as a class

@iuioiua
Copy link
Collaborator

iuioiua commented Sep 9, 2024

@BlackAsLight, could you please draft this PR and un-draft once ready for us to review?

@BlackAsLight BlackAsLight reopened this Sep 9, 2024
@BlackAsLight BlackAsLight marked this pull request as draft September 9, 2024 03:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

suggestion: add CBOR encoding/decoding
3 participants