Skip to content

Commit

Permalink
fix: decode fixes and e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw committed May 7, 2019
1 parent a26c5e8 commit d4020df
Show file tree
Hide file tree
Showing 12 changed files with 334 additions and 667 deletions.
3 changes: 1 addition & 2 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ test/repo-tests*
logs
*.log

coverage

# Runtime data
pids
*.pid
Expand All @@ -18,6 +16,7 @@ lib-cov

# Coverage directory used by tools like istanbul
coverage
.nyc_output/

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
Expand Down
14 changes: 1 addition & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
sudo: false
language: node_js
node_js:
- 8
- 10
- stable

# Make sure we have new NPM and yarn
before_install:
- npm install -g npm

script:
- npm run lint
- npm test
- npm run coverage


before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start

after_success:
- npm run coverage-publish

env:
- CXX=g++-4.8

addons:
firefox: 'latest'
apt:
Expand Down
70 changes: 0 additions & 70 deletions CHANGELOG.md

This file was deleted.

92 changes: 42 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,92 +1,84 @@
# pull-length-prefixed
# it-length-prefixed

[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![Coverage Status](https://coveralls.io/repos/github/dignifiedquire/pull-length-prefixed/badge.svg?branch=master)](https://coveralls.io/github/dignifiedquire/pull-length-prefixed?branch=master)
[![Travis CI](https://travis-ci.org/dignifiedquire/pull-length-prefixed.svg?branch=master)](https://travis-ci.org/dignifiedquire/pull-length-prefixed)
[![Circle CI](https://circleci.com/gh/dignifiedquire/pull-length-prefixed.svg?style=svg)](https://circleci.com/gh/dignifiedquire/pull-length-prefixed)
[![Dependency Status](https://david-dm.org/dignifiedquire/pull-length-prefixed.svg?style=flat-square)](https://david-dm.org/dignifiedquire/pull-length-prefixed) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
[![Coverage Status](https://coveralls.io/repos/github/alanshaw/it-length-prefixed/badge.svg?branch=master)](https://coveralls.io/github/alanshaw/it-length-prefixed?branch=master)
[![Travis CI](https://travis-ci.org/alanshaw/it-length-prefixed.svg?branch=master)](https://travis-ci.org/alanshaw/it-length-prefixed)
[![Dependency Status](https://david-dm.org/alanshaw/it-length-prefixed.svg?style=flat-square)](https://david-dm.org/alanshaw/it-length-prefixed)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)

> Streaming length prefixed buffers with pull-streams
> Streaming length prefixed buffers with async iterators
## Table of Contents
## Install

- [Installation](#installation)
- [Usage](#usage)
- [Contribute](#contribute)
- [License](#license)

## Installation

```bash
$ npm install --save pull-length-prefixed
```sh
npm install it-length-prefixed
```

## Usage

```js
var pull = require('pull-stream')
var lp = require('pull-length-prefixed')
const pipe = require('it-pipe')
const lp = require('it-length-prefixed')

const encoded = []

// encode
pull(
pull.values([Buffer.from('hello world')]),
await pipe(
[Buffer.from('hello world')],
lp.encode(),
pull.collect(function (err, encoded) {
if (err) throw err
console.log(encoded)
// => [Buffer <0b 68 65 6c 6c 6f 20 77 6f 72 6c 64>]
})
async source => {
for await (const chunk of source) {
encoded.push(chunk)
}
}
)

console.log(encoded)
// => [Buffer <0b 68 65 6c 6c 6f 20 77 6f 72 6c 64>]

const decoded = []

// decode
pull(
pull.values(encoded), // e.g. from above
await pipe(
encoded, // e.g. from above
lp.decode(),
pull.collect(function (err, decoded) {
if (err) throw err
console.log(decoded)
// => [Buffer <68 65 6c 6c 6f 20 77 6f 72 6c 64>]
})
async source => {
for await (const chunk of source) {
decoded.push(chunk)
}
}
)

console.log(decoded)
// => [Buffer <68 65 6c 6c 6f 20 77 6f 72 6c 64>]
```

## API

### `encode([opts])`

- `opts: Object`, optional
- `fixed: false`: If true uses a fixed 4 byte Int32BE prefix instead of varint
- `poolSize: 10 * 1024`: Buffer pool size to allocate up front

By default all messages will be prefixed with a varint. If you want to use a fixed length prefix you can specify this through the `opts`.
All messages will be prefixed with a varint.

Returns a pull-stream through.
Returns a [transform](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#transform-it).

### `decode([opts])`

- `opts: Object`, optional
- `fixed: false`: If true uses a fixed 4 byte Int32BE prefix instead of varint
- `maxLength`: If provided, will not decode messages longer than the size specified, if omitted will use the current default of 4MB.

By default all messages will be prefixed with a varint. If you want to use a fixed length prefix you can specify this through the `opts`.


Returns a pull-stream through.

### `decodeFromReader(reader, [opts], cb)`

- `reader: [pull-reader](https://github.com/dominictarr/pull-reader)`
- `opts: Object`, optional. Same as for `decode`.
- `cb: Function`: Callback called with `(err, message)`.
- `maxDataLength`: If provided, will not decode messages longer than the size specified, if omitted will use the current default of 4MB.

This uses a [pull-reader](https://github.com/dominictarr/pull-reader) instance to reade and decode a single message. Useful when using [pull-handshake](https://github.com/pull-stream/pull-handshake) with length prefixed messages.
All messages will be prefixed with a varint.

Returns a [transform](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#transform-it).

## Contribute

PRs and issues gladly accepted! Check out the [issues](//github.com/dignifiedquire/pull-length-prefixed/issues).
PRs and issues gladly accepted! Check out the [issues](https://github.com/alanshaw/it-length-prefixed/issues).

## License

Expand Down
Loading

0 comments on commit d4020df

Please sign in to comment.