Skip to content

Commit

Permalink
Merge pull request #8 from web3-storage/add_linting
Browse files Browse the repository at this point in the history
Add linting
  • Loading branch information
ice-breaker-tg authored Sep 14, 2022
2 parents 8fd79c5 + 0aeefb3 commit e26a13a
Show file tree
Hide file tree
Showing 15 changed files with 2,140 additions and 293 deletions.
22 changes: 20 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
],
"scripts": {
"build": "npm run build:api_docs && npm run build:types",
"build:types": "tsc",
"build:api_docs": "jsdoc2md -f src/index.js > API.md"
"build:types": "tsc --declaration --emitDeclarationOnly --declarationDir ./dist/types",
"build:api_docs": "jsdoc2md -f src/index.js > API.md",
"test": "npm run typecheck && npm run prettier",
"typecheck": "tsc --noEmit",
"prettier": "prettier -c '**/*.{js,ts,yml,json}' --ignore-path .gitignore",
"prettier:fix": "prettier --write '**/*.{js,ts,yml,json}' --ignore-path .gitignore"
},
"dependencies": {
"@ucanto/authority": "^0.5.0",
Expand All @@ -26,8 +30,22 @@
"tweetnacl": "^1.0.3"
},
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^3.3.0",
"ipjs": "^5.2.0",
"jsdoc-to-markdown": "^7.1.1",
"prettier": "^2.7.1",
"standard": "^17.0.0",
"tsc": "^2.0.4",
"typescript": "^4.8.3"
},
"prettier": {
"trailingComma": "es5",
"semi": false,
"singleQuote": true,
"importOrder": [
"^[./]"
],
"importOrderSeparation": true,
"importOrderSortSpecifiers": true
}
}
142 changes: 71 additions & 71 deletions patches/@ipld/car/buffer-writer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// @ts-nocheck
import varint from 'varint';
import { Token, Type } from 'cborg';
import { tokensToLength } from 'cborg/length';
import * as CBOR from '@ipld/dag-cbor';
import * as CBOR from '@ipld/dag-cbor'
import { Token, Type } from 'cborg'
import { tokensToLength } from 'cborg/length'
import varint from 'varint'

/**
* @typedef {import('@ipld/car/api').CID} CID
Expand All @@ -25,15 +25,15 @@ class CarBufferWriter {
*/
constructor(bytes, headerSize) {
/** @readonly */
this.bytes = bytes;
this.byteOffset = headerSize;
this.bytes = bytes
this.byteOffset = headerSize

/**
* @readonly
* @type {CID[]}
*/
this.roots = [];
this.headerSize = headerSize;
this.roots = []
this.headerSize = headerSize
}

/**
Expand All @@ -45,8 +45,8 @@ class CarBufferWriter {
* @returns {CarBufferWriter}
*/
addRoot(root, options) {
addRoot(this, root, options);
return this;
addRoot(this, root, options)
return this
}

/**
Expand All @@ -57,8 +57,8 @@ class CarBufferWriter {
* @returns {CarBufferWriter}
*/
write(block) {
addBlock(this, block);
return this;
addBlock(this, block)
return this
}

/**
Expand All @@ -69,7 +69,7 @@ class CarBufferWriter {
* @returns {Uint8Array}
*/
close(options) {
return close(this, options);
return close(this, options)
}
}

Expand All @@ -79,29 +79,29 @@ class CarBufferWriter {
* @param {{resize?:boolean}} options
*/
export const addRoot = (writer, root, { resize = false } = {}) => {
const { bytes, headerSize, byteOffset, roots } = writer;
writer.roots.push(root);
const size = headerLength(writer);
const { bytes, headerSize, byteOffset, roots } = writer
writer.roots.push(root)
const size = headerLength(writer)
// If there is not enough space for the new root
if (size > headerSize) {
// Check if we root would fit if we were to resize the head.
if (size - headerSize + byteOffset < bytes.byteLength) {
// If resize is enabled resize head
if (resize) {
resizeHeader(writer, size);
resizeHeader(writer, size)
// otherwise remove head and throw an error suggesting to resize
} else {
roots.pop();
roots.pop()
throw new RangeError(`Header of size ${headerSize} has no capacity for new root ${root}.
However there is a space in the buffer and you could call addRoot(root, { resize: root }) to resize header to make a space for this root.`);
However there is a space in the buffer and you could call addRoot(root, { resize: root }) to resize header to make a space for this root.`)
}
// If head would not fit even with resize pop new root and throw error
} else {
roots.pop();
throw new RangeError(`Buffer has no capacity for a new root ${root}`);
roots.pop()
throw new RangeError(`Buffer has no capacity for a new root ${root}`)
}
}
};
}

/**
* Calculates number of bytes required for storing given block in CAR. Useful in
Expand All @@ -112,96 +112,96 @@ export const addRoot = (writer, root, { resize = false } = {}) => {
* @returns {number}
*/
export const blockLength = ({ cid, bytes }) => {
const size = cid.bytes.byteLength + bytes.byteLength;
return varint.encodingLength(size) + size;
};
const size = cid.bytes.byteLength + bytes.byteLength
return varint.encodingLength(size) + size
}

/**
* @param {CarBufferWriter} writer
* @param {Block} block
*/
export const addBlock = (writer, { cid, bytes }) => {
const byteLength = cid.bytes.byteLength + bytes.byteLength;
const size = varint.encode(byteLength);
const byteLength = cid.bytes.byteLength + bytes.byteLength
const size = varint.encode(byteLength)
if (writer.byteOffset + size.length + byteLength > writer.bytes.byteLength) {
throw new RangeError('Buffer has no capacity for this block');
throw new RangeError('Buffer has no capacity for this block')
} else {
writeBytes(writer, size);
writeBytes(writer, cid.bytes);
writeBytes(writer, bytes);
writeBytes(writer, size)
writeBytes(writer, cid.bytes)
writeBytes(writer, bytes)
}
};
}

/**
* @param {CarBufferWriter} writer
* @param {object} [options]
* @param {boolean} [options.resize]
*/
export const close = (writer, { resize = false } = {}) => {
const { roots, bytes, byteOffset, headerSize } = writer;
const { roots, bytes, byteOffset, headerSize } = writer

const headerBytes = CBOR.encode({ version: 1, roots });
const varintBytes = varint.encode(headerBytes.length);
const headerBytes = CBOR.encode({ version: 1, roots })
const varintBytes = varint.encode(headerBytes.length)

const size = varintBytes.length + headerBytes.byteLength;
const offset = headerSize - size;
const size = varintBytes.length + headerBytes.byteLength
const offset = headerSize - size

// If header size estimate was accurate we just write header and return
// view into buffer.
if (offset === 0) {
writeHeader(writer, varintBytes, headerBytes);
return bytes.subarray(0, byteOffset);
writeHeader(writer, varintBytes, headerBytes)
return bytes.subarray(0, byteOffset)
// If header was overestimated and `{resize: true}` is passed resize header
} else if (resize) {
resizeHeader(writer, size);
writeHeader(writer, varintBytes, headerBytes);
return bytes.subarray(0, writer.byteOffset);
resizeHeader(writer, size)
writeHeader(writer, varintBytes, headerBytes)
return bytes.subarray(0, writer.byteOffset)
} else {
throw new RangeError(`Header size was overestimated.
You can use close({ resize: true }) to resize header`);
You can use close({ resize: true }) to resize header`)
}
};
}

/**
* @param {CarBufferWriter} writer
* @param {number} byteLength
*/
export const resizeHeader = (writer, byteLength) => {
const { bytes, headerSize } = writer;
const { bytes, headerSize } = writer
// Move data section to a new offset
bytes.set(bytes.subarray(headerSize, writer.byteOffset), byteLength);
bytes.set(bytes.subarray(headerSize, writer.byteOffset), byteLength)
// Update header size & byteOffset
writer.byteOffset += byteLength - headerSize;
writer.headerSize = byteLength;
};
writer.byteOffset += byteLength - headerSize
writer.headerSize = byteLength
}

/**
* @param {CarBufferWriter} writer
* @param {number[]|Uint8Array} bytes
*/

const writeBytes = (writer, bytes) => {
writer.bytes.set(bytes, writer.byteOffset);
writer.byteOffset += bytes.length;
};
writer.bytes.set(bytes, writer.byteOffset)
writer.byteOffset += bytes.length
}
/**
* @param {{bytes:Uint8Array}} writer
* @param {number[]} varint
* @param {Uint8Array} header
*/
const writeHeader = ({ bytes }, varint, header) => {
bytes.set(varint);
bytes.set(header, varint.length);
};
bytes.set(varint)
bytes.set(header, varint.length)
}

const headerPreludeTokens = [
new Token(Type.map, 2),
new Token(Type.string, 'version'),
new Token(Type.uint, 1),
new Token(Type.string, 'roots'),
];
]

const CID_TAG = new Token(Type.tag, 42);
const CID_TAG = new Token(Type.tag, 42)

/**
* Calculates header size given the array of byteLength for roots.
Expand All @@ -211,15 +211,15 @@ const CID_TAG = new Token(Type.tag, 42);
* @returns {number}
*/
export const calculateHeaderLength = (rootLengths) => {
const tokens = [...headerPreludeTokens];
tokens.push(new Token(Type.array, rootLengths.length));
const tokens = [...headerPreludeTokens]
tokens.push(new Token(Type.array, rootLengths.length))
for (const rootLength of rootLengths) {
tokens.push(CID_TAG);
tokens.push(new Token(Type.bytes, { length: rootLength + 1 }));
tokens.push(CID_TAG)
tokens.push(new Token(Type.bytes, { length: rootLength + 1 }))
}
const length = tokensToLength(tokens); // no options needed here because we have simple tokens
return varint.encodingLength(length) + length;
};
const length = tokensToLength(tokens) // no options needed here because we have simple tokens
return varint.encodingLength(length) + length
}

/**
* Calculates header size given the array of roots.
Expand All @@ -230,7 +230,7 @@ export const calculateHeaderLength = (rootLengths) => {
* @returns {number}
*/
export const headerLength = ({ roots }) =>
calculateHeaderLength(roots.map((cid) => cid.bytes.byteLength));
calculateHeaderLength(roots.map((cid) => cid.bytes.byteLength))

/**
* Estimates header size given a count of the roots and the expected byte length
Expand All @@ -243,7 +243,7 @@ export const headerLength = ({ roots }) =>
* @returns {number}
*/
export const estimateHeaderLength = (rootCount, rootByteLength = 36) =>
calculateHeaderLength(new Array(rootCount).fill(rootByteLength));
calculateHeaderLength(new Array(rootCount).fill(rootByteLength))

/**
* Creates synchronous CAR writer that can be used to encode blocks into a given
Expand Down Expand Up @@ -275,12 +275,12 @@ export const createWriter = (
headerSize = headerLength({ roots }),
} = {}
) => {
const bytes = new Uint8Array(buffer, byteOffset, byteLength);
const bytes = new Uint8Array(buffer, byteOffset, byteLength)

const writer = new CarBufferWriter(bytes, headerSize);
const writer = new CarBufferWriter(bytes, headerSize)
for (const root of roots) {
writer.addRoot(root);
writer.addRoot(root)
}

return writer;
};
return writer
}
5 changes: 3 additions & 2 deletions patches/@ucanto/transport/car.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// @ts-nocheck
import { Delegation } from '@ucanto/core'
import * as API from '@ucanto/interface'

import * as CAR from './car/codec.js'
import { Delegation } from '@ucanto/core'

export { CAR as codec }

Expand Down Expand Up @@ -59,7 +60,7 @@ export const decode = async ({ headers, body }) => {
invocations.push(
Delegation.create({
root,
blocks: /** @type {Map<string, API.Block>} */ (blocks),
blocks,
})
)
}
Expand Down
Loading

0 comments on commit e26a13a

Please sign in to comment.