-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TEMPORARY add dist/ to PR for use with gitpkg
- Loading branch information
Showing
62 changed files
with
8,093 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
node_modules | ||
dist/ | ||
coverage/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Copyright 2016-2020 Protocol Labs | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright 2016-2020 Protocol Labs | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# @ipld/dag-pb | ||
|
||
An implementation of the [DAG-PB spec](https://github.com/ipld/specs/blob/master/block-layer/codecs/dag-pb.md) for JavaScript designed for use with [multiformats](https://github.com/multiformats/js-multiformats) or via the higher-level `Block` abstraction in [@ipld/block](https://github.com/ipld/js-block). | ||
|
||
## Example | ||
|
||
```js | ||
import CID from 'multiformats/cid' | ||
import { sha256 } from 'multiformats/hashes/sha2' | ||
import * as dagPB from '@ipld/dag-pb' | ||
|
||
async function run () { | ||
const bytes = dagPB.encode({ | ||
Data: new TextEncoder().encode('Some data as a string'), | ||
Links: [] | ||
}) | ||
|
||
// also possible if you `import dagPB, { prepare } from '@ipld/dag-pb'` | ||
// const bytes = dagPB.encode(prepare('Some data as a string')) | ||
// const bytes = dagPB.encode(prepare(new TextEncoder().encode('Some data as a string'))) | ||
|
||
const hash = await sha256.digest(bytes) | ||
const cid = CID.create(1, dagPB.code, hash) | ||
|
||
console.log(cid, '=>', Buffer.from(bytes).toString('hex')) | ||
|
||
const decoded = dagPB.decode(bytes) | ||
|
||
console.log(decoded) | ||
console.log(`decoded "Data": ${new TextDecoder().decode(decoded.Data)}`) | ||
} | ||
|
||
run().catch((err) => { | ||
console.error(err) | ||
process.exit(1) | ||
}) | ||
``` | ||
|
||
## Usage | ||
|
||
`@ipld/dag-pb` is designed to be used within multiformats but can be used separately. `encode()`, `decode()`, `validate()` and `prepare()` functions are available if you pass in a `multiformats` object to the default export function. Each of these can operate independently as required. | ||
|
||
### `prepare()` | ||
|
||
The DAG-PB encoding is very strict about the Data Model forms that are passed in. The objects _must_ exactly resemble what they would if they were to undergo a round-trip of encode & decode. Therefore, extraneous or mistyped properties are not acceptable and will be rejected. See the [DAG-PB spec](https://github.com/ipld/specs/blob/master/block-layer/codecs/dag-pb.md) for full details of the acceptable schema and additional constraints. | ||
|
||
Due to this strictness, a `prepare()` function is made available which simplifies construction and allows for more flexible input forms. Prior to encoding objects, call `prepare()` to receive a new object that strictly conforms to the schema. | ||
|
||
```js | ||
import CID from 'multiformats/cid' | ||
import { prepare } from '@ipld/dag-pb' | ||
|
||
console.log(prepare({ Data: 'some data' })) | ||
// ->{ Data: Uint8Array(9) [115, 111, 109, 101, 32, 100, 97, 116, 97] } | ||
console.log(prepare({ Links: [CID.parse('bafkqabiaaebagba')] })) | ||
// -> { Links: [ { Hash: CID(bafkqabiaaebagba) } ] } | ||
``` | ||
|
||
Some features of `prepare()`: | ||
|
||
* Extraneous properties are omitted | ||
* String values for `Data` are converted | ||
* Strings are converted to `{ Data: bytes }` (as are `Uint8Array`s) | ||
* Multiple ways of finding CIDs in the `Links` array are attempted, including interpreting the whole link element as a CID, reading a `Uint8Array` as a CID | ||
* Ensuring that properties are of the correct type (link `Name` is a `string` and `Tsize` is a `number`) | ||
* `Links` array is always present, even if empty | ||
* `Links` array is properly sorted | ||
|
||
## License | ||
|
||
Licensed under either of | ||
|
||
* Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / http://www.apache.org/licenses/LICENSE-2.0) | ||
* MIT ([LICENSE-MIT](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. |
Oops, something went wrong.