Skip to content
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
8 changes: 8 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ module.exports = {
'no-undef-init': 'error',
'no-undef': 'error',
'no-unexpected-multiline': 'error',
'no-unused-expressions': ['error', { allowShortCircuit: true }],
'no-unused-labels': 'error',
'no-unused-vars': [
'error', {
args: 'none',
caughtErrors: 'all',
caughtErrorsIgnorePattern: '^ignore'
}],
'no-var': 'error',
'object-curly-spacing': 'off',
'one-var': ['error', 'never'],
Expand Down
30 changes: 5 additions & 25 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 @@ -24,30 +24,10 @@ jobs:
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install, production
uses: bahmutov/npm-install@v1
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
- name: Install cli test tools
uses: bahmutov/npm-install@v1
with:
working-directory: cli
install-command: npm install --no-save mock-stdio
- name: Install
run: yarn install --no-progress --no-lockfile
- name: Test ${{ matrix.node-version }}
run: npm run lcov
run: npm run coverage
- name: Coveralls Parallel
uses: coverallsapp/github-action@master
with:
Expand All @@ -56,7 +36,7 @@ jobs:
parallel: true
finish:
needs: build
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
steps:
- name: Coveralls Finished
uses: coverallsapp/github-action@master
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ typings.json
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: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
engine-strict=true
package-lock=false
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
239 changes: 30 additions & 209 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,221 +1,42 @@
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})
```
This is a monorepo that holds a few related packages:

2) use stream mode. Catch the `data`, `finish`, and `error` events. Make sure to call `end()` when you're done.
- [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

```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 */)
## Tooling

enc.end(['foo', 1, false])
```
- Install with `npm run yi`, which uses yarn. I'm doing that because a modern
version of yarn is available on all of the node versions in GitHub Actions.
Once all of the node versions I'm supporting have npm 7+, that will
probably work with some small tweaks. The important thing (for example) is
that the `cbor-cli` package ends up depending on the local version of
`cbor`.
- monorepo-wide scripts:
- `deploy`: build and deploy `cbor-web` and all of the actions
- `coverage`: run tests and report coverage; look in `coverage/lcov-report/index.html`.
- `lint`: run eslint over all projects

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

## Supported types
If you really need to get at a specific rev from GitHub, you can no longer do
`npm install hildjj/node-cbor`. Instead you need:

The following types are supported for encoding:
npm install https://gitpkg.now.sh/hildjj/node-cbor/packages/cbor?master

* 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)
## Supported Node.js versions

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.
Loading