Skip to content

Commit

Permalink
Switch to monorepo format and clean up all the things
Browse files Browse the repository at this point in the history
  • Loading branch information
hildjj committed Feb 10, 2021
1 parent a2185fa commit 5ba61cc
Show file tree
Hide file tree
Showing 153 changed files with 4,546 additions and 3,944 deletions.
28 changes: 9 additions & 19 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:
jobs:
build:

runs-on: ubuntu-latest
runs-on: ubuntu-20.04

strategy:
matrix:
Expand All @@ -25,29 +25,19 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- name: Install, production
uses: bahmutov/npm-install@v1
run: yarn install --no-progress --no-lockfile
env:
NODE_ENV: production
- name: Install test tools
uses: bahmutov/npm-install@v1
with:
install-command: npm install --no-save ava nyc garbage
- name: Overwrite cli cbor version with ..
run: npm i ..
working-directory: cli
- name: Install cli, production
uses: bahmutov/npm-install@v1
with:
working-directory: cli
env:
NODE_ENV: production
run: npm install --no-save ava nyc
- name: Install cbor test tools
run: npm install --no-save garbage bignumber.js
working-directory: ./packages/cbor
- name: Install cli test tools
uses: bahmutov/npm-install@v1
with:
working-directory: cli
install-command: npm install --no-save mock-stdio
run: npm install --no-save mock-stdio nofilter
working-directory: ./packages/cbor-cli
- name: Test ${{ matrix.node-version }}
run: npm run lcov
run: npm run coverage
- name: Coveralls Parallel
uses: coverallsapp/github-action@master
with:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ shrinkwrap.yaml
npm-debug.log
t.js
pnpm-lock.yaml
yarn.lock
yarn-error.log
7 changes: 7 additions & 0 deletions .ncurc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = {
reject: [
"marked"
]
}
1 change: 0 additions & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
engine-strict=true
package-lock=false
link-workspace-packages = true
2 changes: 2 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
changesetIgnorePatterns:
- "./packages/*/test/*.js"
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 Joe Hildebrand
Copyright (c) 2021 Joe Hildebrand

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
Expand Down
229 changes: 15 additions & 214 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,221 +1,22 @@
cbor
====
# CBOR

Encode and parse data in the Concise Binary Object Representation (CBOR) data format ([RFC7049](http://tools.ietf.org/html/rfc7049)).

Supported Node.js versions
--------------------------
## Pointers

This project now only supports versions of Node that the Node team is [currently supporting](https://github.com/nodejs/Release#release-schedule). Ava's [support statement](https://github.com/avajs/ava/blob/master/docs/support-statement.md) is what we will be using as well. Currently, that means Node `10`+ is required. If you need to support an older version of Node (back to version 6), use cbor version 5.2.x, which will get nothing but security updates from here on out.

Installation:
------------

```bash
$ npm install --save cbor
```

**NOTE**
This package now requires node.js 8.3 or higher. It will work on node.js 6, in
a less-tested, less-featureful way. Please start upgrading if it is possible
for you.

Documentation:
-------------
See the full API [documentation](http://hildjj.github.io/node-cbor/).

For a command-line interface, see [cbor-cli](https://github.com/hildjj/node-cbor/tree/master/cli).

Example:
```javascript
var cbor = require('cbor');
var assert = require('assert');

var encoded = cbor.encode(true); // returns <Buffer f5>
cbor.decodeFirst(encoded, function(error, obj) {
// error != null if there was an error
// obj is the unpacked object
assert.ok(obj === true);
});

// Use integers as keys?
var m = new Map();
m.set(1, 2);
encoded = cbor.encode(m); // <Buffer a1 01 02>
```

Allows streaming as well:

```javascript
var cbor = require('cbor');
var fs = require('fs');

var d = new cbor.Decoder();
d.on('data', function(obj){
console.log(obj);
});

var s = fs.createReadStream('foo');
s.pipe(d);

var d2 = new cbor.Decoder({input: '00', encoding: 'hex'});
d.on('data', function(obj){
console.log(obj);
});
```

There is also support for synchronous decodes:

```javascript
try {
console.log(cbor.decodeFirstSync('02')); // 2
console.log(cbor.decodeAllSync('0202')); // [2, 2]
} catch (e) {
// throws on invalid input
}
```

The sync encoding and decoding are exported as a
[leveldb encoding](https://github.com/Level/levelup#custom_encodings), as
`cbor.leveldb`.

## highWaterMark

The synchronous routines for encoding and decoding will have problems with
objects that are larger than 16kB, which the default buffer size for Node
streams. There are a few ways to fix this:

1) pass in a `highWaterMark` option with the value of the largest buffer size you think you will need:

```javascript
cbor.encodeOne(Buffer.alloc(40000), {highWaterMark: 65535})
```

2) use stream mode. Catch the `data`, `finish`, and `error` events. Make sure to call `end()` when you're done.

```javascript
const enc = new cbor.Encoder()
enc.on('data', buf => /* send the data somewhere */)
enc.on('error', console.error)
enc.on('finish', () => /* tell the consumer we are finished */)
This is a monorepo that holds a few related packages:

enc.end(['foo', 1, false])
```
- [cbor](packages/cbor): a node-centric CBOR processor
- [cbor-web](packages/cbor-web): the `cbor` package compiled for use on the
web, including all of its non-optional dependencies
- [cbor-cli](packages/cbor-cli): a set of command-line tools for working with
the `cbor` package
- Examples:
- [webpack-demo](packages/webpack-demo): bundle `cbor` using [webpack](https://webpack.js.org/)
- [parcel-demo](packages/parcel-demo): bundle `cbor` using [parcel](https://parceljs.org/)
- [browserify-demo](packages/browserify-demo): bundle `cbor` using [browserify](http://browserify.org/)
- [plain-demo](packages/plain-demo): bundle `cbor` by just using `cbor-web` directly

3) use `encodeAsync()`, which uses the approach from approach 2 to return a memory-inefficient promise for a Buffer.
## Supported Node.js versions

## Supported types

The following types are supported for encoding:

* boolean
* number (including -0, NaN, and ±Infinity)
* string
* Array, Set (encoded as Array)
* Object (including null), Map
* undefined
* Buffer
* Date,
* RegExp
* url.URL
* BigInt (If your JS version supports them)
* [bignumber](https://github.com/MikeMcl/bignumber.js)

Decoding supports the above types, including the following CBOR tag numbers:

| Tag | Generated Type |
|-----|----------------|
| 0 | Date |
| 1 | Date |
| 2 | bignumber |
| 3 | bignumber |
| 4 | bignumber |
| 5 | bignumber |
| 32 | url.URL |
| 35 | RegExp |

## Adding new Encoders

There are several ways to add a new encoder:

### `encodeCBOR` method

This is the easiest approach, if you can modify the class being encoded. Add an
`encodeCBOR` method to your class, which takes a single parameter of the encoder
currently being used. Your method should return `true` on success, else `false`.
Your method may call `encoder.push(buffer)` or `encoder.pushAny(any)` as needed.

For example:

```javascript
class Foo {
constructor () {
this.one = 1
this.two = 2
}
encodeCBOR (encoder) {
const tagged = new Tagged(64000, [this.one, this.two])
return encoder.pushAny(tagged)
}
}
```

You can also modify an existing type by monkey-patching an `encodeCBOR` function
onto its prototype, but this isn't recommended.

### `addSemanticType`

Sometimes, you want to support an existing type without modification to that
type. In this case, call `addSemanticType(type, encodeFunction)` on an existing
`Encoder` instance. The `encodeFunction` takes an encoder and an object to
encode, for example:

```javascript
class Bar {
constructor () {
this.three = 3
}
}
const enc = new Encoder()
enc.addSemanticType(Bar, (encoder, b) => {
encoder.pushAny(b.three)
})
```

## Adding new decoders

Most of the time, you will want to add support for decoding a new tag type. If
the Decoder class encounters a tag it doesn't support, it will generate a `Tagged`
instance that you can handle or ignore as needed. To have a specific type
generated instead, pass a `tags` option to the `Decoder`'s constructor, consisting
of an object with tag number keys and function values. The function will be
passed the decoded value associated with the tag, and should return the decoded
value. For the `Foo` example above, this might look like:

```javascript
const d = new Decoder({tags: { 64000: (val) => {
// check val to make sure it's an Array as expected, etc.
const foo = new Foo()
foo.one = val[0]
foo.two = val[1]
return foo
}}})
```

Developers
----------

The tests for this package use a set of test vectors from RFC 7049 appendix A by importing a machine readable version of them from https://github.com/cbor/test-vectors. For these tests to work, you will need to use the command `git submodule update --init` after cloning or pulling this code. See https://gist.github.com/gitaarik/8735255#file-git_submodules-md for more information.

Get a list of build steps with `npm run`. I use `npm run dev`, which rebuilds,
runs tests, and refreshes a browser window with coverage metrics every time I
save a `.js` file. If you don't want to run the fuzz tests every time, set
a `NO_GARBAGE` environment variable:

```
env NO_GARBAGE=1 npm run dev
```

[![Build Status](https://github.com/hildjj/node-cbor/workflows/Tests/badge.svg)](https://github.com/hildjj/node-cbor/actions?query=workflow%3ATests)
[![Coverage Status](https://coveralls.io/repos/hildjj/node-cbor/badge.svg?branch=master)](https://coveralls.io/r/hildjj/node-cbor?branch=master)
[![Dependency Status](https://david-dm.org/hildjj/node-cbor.svg)](https://david-dm.org/hildjj/node-cbor)
This project now only supports versions of Node that the Node team is [currently supporting](https://github.com/nodejs/Release#release-schedule). Ava's [support statement](https://github.com/avajs/ava/blob/master/docs/support-statement.md) is what we will be using as well. Currently, that means Node `10`+ is required. If you need to support an older version of Node (back to version 6), use cbor version 5.2.x, which will get nothing but security updates from here on out.
38 changes: 19 additions & 19 deletions docs/CborMap.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/Commented.html

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions docs/Decoder.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/Diagnose.html

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions docs/Encoder.html

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions docs/Simple.html

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions docs/Tagged.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/cbor.js.html

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions docs/commented.js.html

Large diffs are not rendered by default.

22 changes: 15 additions & 7 deletions docs/decoder.js.html

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions docs/diagnose.js.html

Large diffs are not rendered by default.

39 changes: 19 additions & 20 deletions docs/encoder.js.html

Large diffs are not rendered by default.

Loading

0 comments on commit 5ba61cc

Please sign in to comment.