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: add it-cbor-stream module #148

Merged
merged 3 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/it-cbor-stream/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This project is dual licensed under MIT and Apache-2.0.

MIT: https://www.opensource.org/licenses/mit
Apache-2.0: https://www.apache.org/licenses/license-2.0
5 changes: 5 additions & 0 deletions packages/it-cbor-stream/LICENSE-APACHE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
19 changes: 19 additions & 0 deletions packages/it-cbor-stream/LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
71 changes: 71 additions & 0 deletions packages/it-cbor-stream/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# it-cbor-stream

[![codecov](https://img.shields.io/codecov/c/github/achingbrain/it.svg?style=flat-square)](https://codecov.io/gh/achingbrain/it)
[![CI](https://img.shields.io/github/actions/workflow/status/achingbrain/it/js-test-and-release.yml?branch=main\&style=flat-square)](https://github.com/achingbrain/it/actions/workflows/js-test-and-release.yml?query=branch%3Amain)

> Read and write CBOR messages over a duplex stream

# About

<!--

!IMPORTANT!

Everything in this README between "# About" and "# Install" is automatically
generated and will be overwritten the next time the doc generator is run.

To make changes to this section, please update the @packageDocumentation section
of src/index.js or src/index.ts

To experiment with formatting, please run "npm run docs" from the root of this
repo and examine the changes made.

-->

This module makes it easy to send and receive length-prefixed CBOR encoded
messages over streams.

## Example

```typescript
import { cborStream } from 'it-cbor-stream'

const stream = cborStream(duplex)

// write a message to the stream
stream.write({
foo: 'bar'
})

// read a message from the stream
const res = await stream.read()
```

# Install

```console
$ npm i it-cbor-stream
```

## Browser `<script>` tag

Loading this module through a script tag will make its exports available as `ItCborStream` in the global namespace.

```html
<script src="https://unpkg.com/it-cbor-stream/dist/index.min.js"></script>
```

# API Docs

- <https://achingbrain.github.io/it/modules/it_cbor_stream.html>

# License

Licensed under either of

- Apache 2.0, ([LICENSE-APACHE](https://github.com/achingbrain/it/blob/main/packages/it-cbor-stream/LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT ([LICENSE-MIT](https://github.com/achingbrain/it/blob/main/packages/it-cbor-stream/LICENSE-MIT) / <http://opensource.org/licenses/MIT>)

# Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
68 changes: 68 additions & 0 deletions packages/it-cbor-stream/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"name": "it-cbor-stream",
"version": "0.0.0",
"description": "Read and write CBOR messages over a duplex stream",
"author": "Alex Potsides <[email protected]>",
"license": "Apache-2.0 OR MIT",
"homepage": "https://github.com/achingbrain/it/tree/main/packages/it-cbor-stream#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/achingbrain/it.git"
},
"bugs": {
"url": "https://github.com/achingbrain/it/issues"
},
"publishConfig": {
"access": "public",
"provenance": true
},
"type": "module",
"types": "./dist/src/index.d.ts",
"files": [
"src",
"dist",
"!dist/test",
"!**/*.tsbuildinfo"
],
"exports": {
".": {
"types": "./dist/src/index.d.ts",
"import": "./dist/src/index.js"
}
},
"eslintConfig": {
"extends": "ipfs",
"parserOptions": {
"project": true,
"sourceType": "module"
}
},
"scripts": {
"clean": "aegir clean",
"lint": "aegir lint",
"dep-check": "aegir dep-check",
"build": "aegir build",
"test": "aegir test",
"test:chrome": "aegir test -t browser --cov",
"test:chrome-webworker": "aegir test -t webworker",
"test:firefox": "aegir test -t browser -- --browser firefox",
"test:firefox-webworker": "aegir test -t webworker -- --browser firefox",
"test:node": "aegir test -t node --cov",
"test:electron-main": "aegir test -t electron-main",
"docs": "aegir docs",
"release": "aegir release"
},
"dependencies": {
"cborg": "^4.2.7",
"it-length-prefixed-stream": "^1.0.0",
"it-stream-types": "^2.0.1"
},
"devDependencies": {
"aegir": "^45.0.8",
"it-map": "^3.0.0",
"it-pair": "^2.0.6",
"it-to-buffer": "^4.0.0",
"uint8-varint": "^2.0.4",
"uint8arrays": "^5.1.0"
}
}
84 changes: 84 additions & 0 deletions packages/it-cbor-stream/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* @packageDocumentation
*
* This module makes it easy to send and receive length-prefixed CBOR encoded
* messages over streams.
*
* @example
*
* ```typescript
* import { cborStream } from 'it-cbor-stream'
* import { MessageType } from './src/my-message-type.js'
*
* const stream = cborStream(duplex)
*
* // write a message to the stream
* stream.write({
* foo: 'bar'
* })
*
* // read a message from the stream
* const res = await stream.read()
* ```
*/

import { encode, decode } from 'cborg'
import { lpStream } from 'it-length-prefixed-stream'
import type { EncodeOptions, DecodeOptions } from 'cborg/interface'
import type { LengthPrefixedStreamOpts } from 'it-length-prefixed-stream'
import type { Duplex } from 'it-stream-types'

export interface AbortOptions {
signal?: AbortSignal
}

/**
* Convenience methods for working with CBOR streams
*/
export interface CBORStream <Stream = unknown> {
/**
* Read the next length-prefixed byte array from the stream and decode it as CBOR
*/
read<T>(options?: AbortOptions): Promise<T>

/**
* Encode the passed object as a CBOR message and write it's length-prefixed bytes to the stream
*/
write<T>(data: T, options?: AbortOptions): Promise<void>

/**
* Encode the passed objects as CBOR messages and write their length-prefixed bytes to the stream as a single write
*/
writeV<T>(input: T[], options?: AbortOptions): Promise<void>

/**
* Returns the underlying stream
*/
unwrap(): Stream
}

export function cborStream <Stream extends Duplex<any, any, any>> (duplex: Stream, opts?: LengthPrefixedStreamOpts): CBORStream<Stream> {
const lp = lpStream(duplex, opts)

const W: CBORStream<Stream> = {
read: async (options?: AbortOptions & DecodeOptions) => {
// readLP, decode
const value = await lp.read(options)

return decode(value.subarray(), options)
},
write: async (message, options?: AbortOptions & EncodeOptions) => {
// encode, writeLP
await lp.write(encode(message, options), options)
},
writeV: async (messages, options?: AbortOptions & EncodeOptions) => {
// encode, writeLP
await lp.writeV(messages.map(message => encode(message, options)), options)

Check warning on line 76 in packages/it-cbor-stream/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/it-cbor-stream/src/index.ts#L75-L76

Added lines #L75 - L76 were not covered by tests
},
unwrap: () => {
return lp.unwrap()
}
}

return W
}
87 changes: 87 additions & 0 deletions packages/it-cbor-stream/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { expect } from 'aegir/chai'
import { encode } from 'cborg'
import map from 'it-map'
import { pair } from 'it-pair'
import toBuffer from 'it-to-buffer'
import * as varint from 'uint8-varint'
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
import { cborStream } from '../src/index.js'
import type { CBORStream } from '../src/index.js'

/* eslint-env mocha */
/* eslint-disable max-nested-callbacks */

describe('it-cbor-stream', () => {
let w: CBORStream<any>

beforeEach(async () => {
w = cborStream(pair<Uint8Array>())
})

it('unwraps underlying stream', () => {
const stream = pair<Uint8Array>()
const w = cborStream(stream)

expect(w.unwrap()).to.equal(stream)
})

it('encode/decode', async () => {
const input = {
foo: 'bar'
}

void w.write(input)

const output = await w.read()

expect(output).to.deep.equal(input)
})

it('reads remaining data from unwrapped stream in one buffer', async () => {
const message = {
foo: 'bar'
}
const messageBuf = encode(message)
const extraData = Uint8Array.from([0, 1, 2, 3, 4, 5])

const w = cborStream({
source: (async function * () {
yield uint8ArrayConcat([
varint.encode(messageBuf.byteLength),
messageBuf,
extraData
])
}()),
sink: async () => {}
})

const read = await w.read()
expect(read).to.deep.equal(message)

const rest = await toBuffer(map(w.unwrap().source, (buf) => buf.subarray()))
expect(rest).to.equalBytes(extraData)
})

it('reads remaining data from unwrapped stream in multiple buffers', async () => {
const message = {
foo: 'bar'
}
const messageBuf = encode(message)
const extraData = Uint8Array.from([0, 1, 2, 3, 4, 5])

const w = cborStream({
source: (async function * () {
yield varint.encode(messageBuf.byteLength)
yield messageBuf
yield extraData
}()),
sink: async () => {}
})

const read = await w.read()
expect(read).to.deep.equal(message)

const rest = await toBuffer(map(w.unwrap().source, (buf) => buf.subarray()))
expect(rest).to.equalBytes(extraData)
})
})
23 changes: 23 additions & 0 deletions packages/it-cbor-stream/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"extends": "aegir/src/config/tsconfig.aegir.json",
"compilerOptions": {
"outDir": "dist",
"emitDeclarationOnly": false,
"module": "ES2020"
},
"include": [
"src",
"test"
],
"references": [
{
"path": "../it-length-prefixed-stream"
},
{
"path": "../it-map"
},
{
"path": "../it-to-buffer"
}
]
}
5 changes: 5 additions & 0 deletions packages/it-cbor-stream/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"entryPoints": [
"./src/index.ts"
]
}
Loading