Skip to content

Commit

Permalink
feat: implement sync car decode (#253)
Browse files Browse the repository at this point in the history
* feat: implement sync car decode

* fix: reverse root encoding

* revert to block order but retain root order
  • Loading branch information
Gozala authored Mar 7, 2023
1 parent 6e48019 commit 40acaac
Show file tree
Hide file tree
Showing 11 changed files with 430 additions and 409 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
},
"devDependencies": {
"mocha": "^10.1.0",
"prettier": "2.7.1",
"typescript": "^4.9.4"
"prettier": "2.8.4",
"typescript": "^4.9.5"
},
"prettier": {
"trailingComma": "es5",
Expand Down
8 changes: 4 additions & 4 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@
},
"devDependencies": {
"@types/chai": "^4.3.3",
"@types/mocha": "^9.1.0",
"@types/mocha": "^10.0.1",
"@ucanto/core": "workspace:^",
"@ucanto/principal": "workspace:^",
"@ucanto/transport": "workspace:^",
"@web-std/fetch": "^4.1.0",
"@web-std/file": "^3.0.2",
"c8": "^7.11.0",
"c8": "^7.13.0",
"chai": "^4.3.6",
"mocha": "^10.1.0",
"nyc": "^15.1.0",
"playwright-test": "^8.1.1",
"typescript": "^4.9.4"
"playwright-test": "^8.2.0",
"typescript": "^4.9.5"
},
"type": "module",
"main": "src/lib.js",
Expand Down
10 changes: 5 additions & 5 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@
"build": "tsc --build"
},
"dependencies": {
"@ipld/car": "^5.0.3",
"@ipld/car": "^5.1.0",
"@ipld/dag-cbor": "^9.0.0",
"@ipld/dag-ucan": "^3.2.0",
"@ucanto/interface": "workspace:^",
"multiformats": "^11.0.0"
},
"devDependencies": {
"@types/chai": "^4.3.3",
"@types/mocha": "^9.1.0",
"@types/mocha": "^10.0.1",
"@ucanto/principal": "workspace:^",
"c8": "^7.11.0",
"c8": "^7.13.0",
"chai": "^4.3.6",
"mocha": "^10.1.0",
"nyc": "^15.1.0",
"playwright-test": "^8.1.1",
"typescript": "^4.9.4"
"playwright-test": "^8.2.0",
"typescript": "^4.9.5"
},
"type": "module",
"main": "src/lib.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/interface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"multiformats": "^11.0.0"
},
"devDependencies": {
"typescript": "^4.9.4"
"typescript": "^4.9.5"
},
"exports": {
".": {
Expand Down
10 changes: 5 additions & 5 deletions packages/principal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@
},
"dependencies": {
"@ipld/dag-ucan": "^3.2.0",
"@noble/ed25519": "^1.7.1",
"@noble/ed25519": "^1.7.3",
"@ucanto/interface": "workspace:^",
"multiformats": "^11.0.0",
"one-webcrypto": "^1.0.3"
},
"devDependencies": {
"@types/chai": "^4.3.3",
"@types/mocha": "^9.1.0",
"c8": "^7.11.0",
"@types/mocha": "^10.0.1",
"c8": "^7.13.0",
"chai": "^4.3.6",
"mocha": "^10.1.0",
"nyc": "^15.1.0",
"playwright-test": "^8.1.1",
"typescript": "^4.9.4"
"playwright-test": "^8.2.0",
"typescript": "^4.9.5"
},
"type": "module",
"main": "src/lib.js",
Expand Down
8 changes: 4 additions & 4 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@
"devDependencies": {
"@types/chai": "^4.3.3",
"@types/chai-subset": "^1.3.3",
"@types/mocha": "^9.1.0",
"@types/mocha": "^10.0.1",
"@ucanto/client": "workspace:^",
"@ucanto/principal": "workspace:^",
"@ucanto/transport": "workspace:^",
"@web-std/fetch": "^4.1.0",
"@web-std/file": "^3.0.2",
"c8": "^7.11.0",
"c8": "^7.13.0",
"chai": "^4.3.6",
"chai-subset": "^1.6.0",
"mocha": "^10.1.0",
"multiformats": "^11.0.0",
"nyc": "^15.1.0",
"playwright-test": "^8.1.1",
"typescript": "^4.9.4"
"playwright-test": "^8.2.0",
"typescript": "^4.9.5"
},
"exports": {
".": {
Expand Down
10 changes: 5 additions & 5 deletions packages/transport/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@
"build": "tsc --build"
},
"dependencies": {
"@ipld/car": "^5.0.3",
"@ipld/car": "^5.1.0",
"@ipld/dag-cbor": "^9.0.0",
"@ucanto/core": "workspace:^",
"@ucanto/interface": "workspace:^",
"multiformats": "^11.0.0"
},
"devDependencies": {
"@types/chai": "^4.3.3",
"@types/mocha": "^9.1.0",
"@types/mocha": "^10.0.1",
"@ucanto/principal": "workspace:^",
"@web-std/fetch": "^4.1.0",
"c8": "^7.11.0",
"c8": "^7.13.0",
"chai": "^4.3.6",
"mocha": "^10.1.0",
"nyc": "^15.1.0",
"playwright-test": "^8.1.1",
"typescript": "^4.9.4"
"playwright-test": "^8.2.0",
"typescript": "^4.9.5"
},
"type": "module",
"typesVersions": {
Expand Down
2 changes: 1 addition & 1 deletion packages/transport/src/car.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const decode = async ({ headers, body }) => {
)
}

const { roots, blocks } = await CAR.decode(body)
const { roots, blocks } = CAR.decode(body)

const invocations = []

Expand Down
44 changes: 24 additions & 20 deletions packages/transport/src/car/codec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as API from '@ucanto/interface'
import * as CARWriter from '@ipld/car/buffer-writer'
import { CarReader } from '@ipld/car/reader'
import { CarBufferReader, CarBufferWriter } from '@ipld/car'
import { base32 } from 'multiformats/bases/base32'
import { UCAN, createLink } from '@ucanto/core'
import { sha256 } from 'multiformats/hashes/sha2'
Expand Down Expand Up @@ -33,8 +32,8 @@ class Writer {
const id = block.cid.toString(base32)
if (!this.written.has(id)) {
this.blocks.push(block)
this.byteLength += CARWriter.blockLength(
/** @type {CARWriter.Block} */ (block)
this.byteLength += CarBufferWriter.blockLength(
/** @type {CarBufferWriter.Block} */ (block)
)
this.written.add(id)
}
Expand All @@ -46,25 +45,28 @@ class Writer {
*/
flush(...rootBlocks) {
const roots = []
// We reverse the roots so that the first root is the last block in the CAR
for (const block of rootBlocks.reverse()) {
const id = block.cid.toString(base32)
if (!this.written.has(id)) {
this.blocks.unshift(block)
this.byteLength += CARWriter.blockLength({
cid: /** @type {CARWriter.CID} */ (block.cid),
this.byteLength += CarBufferWriter.blockLength({
cid: /** @type {CarBufferWriter.CID} */ (block.cid),
bytes: block.bytes,
})
this.written.add(id)
}
roots.push(/** @type {CARWriter.CID} */ (block.cid))

// We unshift here because we want to preserve the order of the roots
roots.unshift(/** @type {CarBufferWriter.CID} */ (block.cid))
}

this.byteLength += CARWriter.headerLength({ roots })
this.byteLength += CarBufferWriter.headerLength({ roots })

const buffer = new ArrayBuffer(this.byteLength)
const writer = CARWriter.createWriter(buffer, { roots })
const writer = CarBufferWriter.createWriter(buffer, { roots })

for (const block of /** @type {CARWriter.Block[]} */ (this.blocks)) {
for (const block of /** @type {CarBufferWriter.Block[]} */ (this.blocks)) {
writer.write(block)
}

Expand All @@ -89,20 +91,22 @@ export const encode = ({ roots = [], blocks }) => {

/**
* @param {API.ByteView<Partial<Model>>} bytes
* @returns {Promise<Model>}
* @returns {Model}
*/
export const decode = async bytes => {
const reader = await /** @type {any} */ (CarReader.fromBytes(bytes))
/** @type {{_header: { roots: CARWriter.CID[] }, _keys: string[], _blocks: UCAN.Block[] }} */
const { _header, _blocks, _keys } = reader
export const decode = bytes => {
const reader = CarBufferReader.fromBytes(bytes)
const roots = []
const blocks = new Map()
const index = _header.roots.map(cid => _keys.indexOf(String(cid)))

for (const [n, block] of _blocks.entries()) {
if (index.includes(n)) {
roots.push(/** @type {Block} */ (block))
} else {
for (const root of reader.getRoots()) {
const block = reader.get(root)
if (block) {
roots.push(block)
}
}

for (const block of reader.blocks()) {
if (!roots.includes(block)) {
blocks.set(block.cid.toString(), block)
}
}
Expand Down
12 changes: 6 additions & 6 deletions packages/validator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@
"build": "tsc --build"
},
"dependencies": {
"@ipld/car": "^5.0.3",
"@ipld/dag-cbor": "^8.0.0",
"@ipld/car": "^5.1.0",
"@ipld/dag-cbor": "^9.0.0",
"@ucanto/core": "workspace:^",
"@ucanto/interface": "workspace:^",
"multiformats": "^11.0.0"
},
"devDependencies": {
"@types/chai": "^4.3.3",
"@types/chai-subset": "^1.3.3",
"@types/mocha": "^9.1.0",
"@types/mocha": "^10.0.1",
"@ucanto/client": "workspace:^",
"@ucanto/principal": "workspace:^",
"c8": "^7.11.0",
"c8": "^7.13.0",
"chai": "^4.3.6",
"chai-subset": "^1.6.0",
"mocha": "^10.1.0",
"nyc": "^15.1.0",
"playwright-test": "^8.1.1",
"typescript": "^4.9.4"
"playwright-test": "^8.2.0",
"typescript": "^4.9.5"
},
"type": "module",
"main": "src/lib.js",
Expand Down
Loading

0 comments on commit 40acaac

Please sign in to comment.