Skip to content

Commit

Permalink
Descend into <<>> and '' EDN info when commenting.
Browse files Browse the repository at this point in the history
Fixes #64.
  • Loading branch information
hildjj committed Feb 14, 2025
1 parent b0f0ef9 commit e264ac8
Show file tree
Hide file tree
Showing 10 changed files with 712 additions and 643 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
publish_dir: docs
publish_branch: gh-pages
destination_dir: docs
- run: npm pkg delete devDependencies scripts packageManager
- run: npm pkg delete devDependencies scripts packageManager pnpm
- run: npm publish --access public --provenance
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"devDependencies": {
"@cto.af/eslint-config": "5.1.9",
"c8": "10.1.3",
"cbor-edn": "0.2.1",
"eslint": "9.20.1",
"eslint-plugin-jsdoc": "50.6.3",
"eslint-plugin-markdown": "5.1.0",
Expand All @@ -72,7 +73,12 @@
"typedoc": "0.27.7",
"typescript-eslint": "8.24.0"
},
"packageManager": "[email protected]",
"pnpm": {
"overrides": {
"cbor2": "link:."
}
},
"packageManager": "[email protected]",
"engines": {
"node": ">=18.7"
}
Expand Down
773 changes: 376 additions & 397 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

73 changes: 68 additions & 5 deletions src/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import type {
} from './options.js';
import {MT, NUMBYTES, SYMS} from './constants.js';
import {type OriginalEncoding, getEncoded, saveEncoded} from './box.js';
import {getRanges, subarrayRanges, u8toHex} from './utils.js';
import {CBORcontainer} from './container.js';
import {DecodeStream} from './decodeStream.js';
import {Simple} from './simple.js';
import {Tag} from './tag.js';
import {u8toHex} from './utils.js';
import {diagnose} from './diagnostic.js';

const TD = new TextDecoder();

class CommentContainer extends CBORcontainer implements OriginalEncoding {
public depth = 0;
Expand Down Expand Up @@ -156,10 +159,70 @@ function output(
ret += '\n';
if (enc.length > numLen + 1) {
const ind = spaces((container.depth + 1) * 2);
for (let i = numLen + 1; i < enc.length; i += 8) {
ret += ind;
ret += u8toHex(enc.subarray(i, i + 8));
ret += '\n';
const ranges = getRanges(enc);
if (ranges?.length) {
ranges.sort((a, b) => {
const start = a[0] - b[0];
if (start) {
return start;
}
return b[1] - a[1];
});
let max = 0;
for (const [start, len, type] of ranges) {
if (start < max) {
continue;
}
max = start + len;
if (type === '<<') {
ret += spaces(options.minCol + 1);
ret += '--';
ret += ind;
ret += '<< ';
const buf = subarrayRanges(enc, start, start + len);
const bufRanges = getRanges(buf);
if (bufRanges) {
// The current range will always be start 0 in bufRanges, since
// we just subtracted start from it.
const rInd = bufRanges.findIndex(
([s2, l2, t2]) => (s2 === 0) && (l2 === len) && (t2 === '<<')
);
if (rInd >= 0) {
bufRanges.splice(rInd, 1);
}
}
ret += diagnose(buf);
ret += ' >>\n';
// eslint-disable-next-line @typescript-eslint/no-use-before-define
ret += comment(buf, {
initialDepth: container.depth + 1,
minCol: options.minCol,
noPrefixHex: true,
});
continue;
} else if (type === "'") {
ret += spaces(options.minCol + 1);
ret += '--';
ret += ind;
ret += "'";
ret += TD.decode(enc.subarray(start, start + len));
ret += "'\n";
}
if (start > numLen) {
for (let i = start; i < start + len; i += 8) {
const end = Math.min(i + 8, start + len);
ret += ind;
ret += u8toHex(enc.subarray(i, end));
ret += '\n';
}
}
}
} else {
for (let i = numLen + 1; i < enc.length; i += 8) {
ret += ind;
ret += u8toHex(enc.subarray(i, i + 8));
ret += '\n';
}
}
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/decodeStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
NUMBYTES,
SYMS,
} from './constants.js';
import {base64ToBytes, hexToU8} from './utils.js';
import {base64ToBytes, hexToU8, subarrayRanges} from './utils.js';
import {Simple} from './simple.js';
import {parseHalf} from './float.js';

Expand Down Expand Up @@ -58,7 +58,7 @@ export class DecodeStream implements Sliceable {
}

public toHere(begin: number): Uint8Array {
return this.#src.subarray(begin, this.#offset);
return subarrayRanges(this.#src, begin, this.#offset);
}

/**
Expand Down Expand Up @@ -237,7 +237,7 @@ export class DecodeStream implements Sliceable {
}

#read(size: number): Uint8Array {
const a = this.#src.subarray(this.#offset, (this.#offset += size));
const a = subarrayRanges(this.#src, this.#offset, (this.#offset += size));
if (a.length !== size) {
throw new Error(`Unexpected end of stream reading ${size} bytes, got ${a.length}`);
}
Expand Down
23 changes: 23 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,29 @@ export function hasRanges(u8: Range8Array): boolean {
return getRanges(u8) !== undefined;
}

export function subarrayRanges(
u8: Range8Array,
begin = 0,
end = u8.length - 1
): Range8Array {
const ret = u8.subarray(begin, end);
const ranges = getRanges(u8);
if (ranges) {
const rng: CborRange[] = [];
for (const r of ranges) {
if ((r[0] >= begin) && ((r[0] + r[1]) <= end)) {
const s: CborRange = [...r];
s[0] -= begin;
rng.push(s);
}
}
if (rng.length) {
setRanges(ret, rng);
}
}
return ret;
}

/**
* Convert hex string to Uint8Array.
*
Expand Down
15 changes: 15 additions & 0 deletions test/comment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import '../lib/types.js';
import * as cases from './cases.js';
import assert from 'node:assert/strict';
import {comment} from '../lib/comment.js';
import {parseEDN} from 'cbor-edn';
import {setRanges} from '../lib/utils.js';

Check failure on line 6 in test/comment.test.js

View workflow job for this annotation

GitHub Actions / build (18.x, ubuntu-latest)

'setRanges' is defined but never used. Allowed unused vars must match /^_[^_]/u

Check failure on line 6 in test/comment.test.js

View workflow job for this annotation

GitHub Actions / build (20.x, ubuntu-latest)

'setRanges' is defined but never used. Allowed unused vars must match /^_[^_]/u

Check failure on line 6 in test/comment.test.js

View workflow job for this annotation

GitHub Actions / build (22.x, ubuntu-latest)

'setRanges' is defined but never used. Allowed unused vars must match /^_[^_]/u

Check failure on line 6 in test/comment.test.js

View workflow job for this annotation

GitHub Actions / build (23.x, ubuntu-latest)

'setRanges' is defined but never used. Allowed unused vars must match /^_[^_]/u
import test from 'node:test';

function testAll(list, opts) {
Expand Down Expand Up @@ -32,3 +34,16 @@ test('comment bad', () => {
'0xff',
]);
});

test('EDN ranges', () => {
// This is why the pnpm override is needed in package.json for cbor2.
const bytes = parseEDN("<< 'abc' >>");
assert.equal(comment(bytes), `\
0x4443616263
44 -- Bytes (Length: 4)
-- << h'616263' >>
43 -- Bytes (Length: 3)
-- 'abc'
616263
`);
});
7 changes: 7 additions & 0 deletions web/.hostlocal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@ export default {
dir: '../docs',
port: 5500,
caSubject: '/C=US/ST=Colorado/L=Denver/O=_HostLocal/CN=_HostLocal-cbor2',
glob: [
'src/**',
'../src/**',
'!../src/version.ts',
],
exec: 'cd .. && npm run build && npm run docs',
initial: true,
};
7 changes: 3 additions & 4 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@
"license": "MIT",
"devDependencies": {
"@playwright/test": "1.50.1",
"cbor-edn": "0.1.0",
"cbor-edn": "0.2.1",
"cbor2": "link:..",
"css-loader": "7.1.2",
"hostlocal": "1.4.2",
"html-webpack-plugin": "5.6.3",
"node-inspect-extracted": "3.0.2",
"style-loader": "4.0.0",
"webpack": "5.97.1",
"webpack": "5.98.0",
"webpack-cli": "6.0.1"
},
"pnpm": {
"overrides": {
"cross-spawn": "^7.0.6",
"nanoid": "^5.0.9"
"cbor2": "link:.."
}
},
"engines": {
Expand Down
Loading

0 comments on commit e264ac8

Please sign in to comment.