diff --git a/packages/it-cbor-stream/LICENSE b/packages/it-cbor-stream/LICENSE new file mode 100644 index 00000000..20ce483c --- /dev/null +++ b/packages/it-cbor-stream/LICENSE @@ -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 diff --git a/packages/it-cbor-stream/LICENSE-APACHE b/packages/it-cbor-stream/LICENSE-APACHE new file mode 100644 index 00000000..14478a3b --- /dev/null +++ b/packages/it-cbor-stream/LICENSE-APACHE @@ -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. diff --git a/packages/it-cbor-stream/LICENSE-MIT b/packages/it-cbor-stream/LICENSE-MIT new file mode 100644 index 00000000..72dc60d8 --- /dev/null +++ b/packages/it-cbor-stream/LICENSE-MIT @@ -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. diff --git a/packages/it-cbor-stream/README.md b/packages/it-cbor-stream/README.md new file mode 100644 index 00000000..ec54d108 --- /dev/null +++ b/packages/it-cbor-stream/README.md @@ -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 + + + +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 ` +``` + +# API Docs + +- + +# License + +Licensed under either of + +- Apache 2.0, ([LICENSE-APACHE](https://github.com/achingbrain/it/blob/main/packages/it-cbor-stream/LICENSE-APACHE) / ) +- MIT ([LICENSE-MIT](https://github.com/achingbrain/it/blob/main/packages/it-cbor-stream/LICENSE-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. diff --git a/packages/it-cbor-stream/package.json b/packages/it-cbor-stream/package.json new file mode 100644 index 00000000..37413d40 --- /dev/null +++ b/packages/it-cbor-stream/package.json @@ -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 ", + "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" + } +} diff --git a/packages/it-cbor-stream/src/index.ts b/packages/it-cbor-stream/src/index.ts new file mode 100644 index 00000000..59324f4b --- /dev/null +++ b/packages/it-cbor-stream/src/index.ts @@ -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 { + /** + * Read the next length-prefixed byte array from the stream and decode it as CBOR + */ + read(options?: AbortOptions): Promise + + /** + * Encode the passed object as a CBOR message and write it's length-prefixed bytes to the stream + */ + write(data: T, options?: AbortOptions): Promise + + /** + * Encode the passed objects as CBOR messages and write their length-prefixed bytes to the stream as a single write + */ + writeV(input: T[], options?: AbortOptions): Promise + + /** + * Returns the underlying stream + */ + unwrap(): Stream +} + +export function cborStream > (duplex: Stream, opts?: LengthPrefixedStreamOpts): CBORStream { + const lp = lpStream(duplex, opts) + + const W: CBORStream = { + 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) + }, + unwrap: () => { + return lp.unwrap() + } + } + + return W +} diff --git a/packages/it-cbor-stream/test/index.spec.ts b/packages/it-cbor-stream/test/index.spec.ts new file mode 100644 index 00000000..db3db730 --- /dev/null +++ b/packages/it-cbor-stream/test/index.spec.ts @@ -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 + + beforeEach(async () => { + w = cborStream(pair()) + }) + + it('unwraps underlying stream', () => { + const stream = pair() + 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) + }) +}) diff --git a/packages/it-cbor-stream/tsconfig.json b/packages/it-cbor-stream/tsconfig.json new file mode 100644 index 00000000..02ec6baf --- /dev/null +++ b/packages/it-cbor-stream/tsconfig.json @@ -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" + } + ] +} diff --git a/packages/it-cbor-stream/typedoc.json b/packages/it-cbor-stream/typedoc.json new file mode 100644 index 00000000..f599dc72 --- /dev/null +++ b/packages/it-cbor-stream/typedoc.json @@ -0,0 +1,5 @@ +{ + "entryPoints": [ + "./src/index.ts" + ] +}