Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class Protocol {
```typescript
import { binread, BinaryReader } from 'binspector'

const buf = new Uint8Array([0x02, 0x01, 0x02, 0x03, 0x04]).buffer
const buf = new Uint8Array([0x02, 0x01, 0x02, 0x03, 0x04])

binread(new BinaryReader(buf), Protocol)
// => { len: 2, coords: [{ x: 1, y: 2 }, { x: 3, y: 4 }] }
Expand Down
10 changes: 5 additions & 5 deletions docs/Getting-Started-With-Binspector.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ To parse the content of a file formatted as the `Protocol` definition you will
use the following code.

```typescript
import { BinaryReader, binread }
import { promises as fs } from 'fs'
import path from 'path'
import { BinaryReader, binread } from 'binspector'
import * as fs from 'node:fs'
import * as path from 'node:path'

const data = await fs.readFile(path.join(__dirname, 'file.bin')).buffer
const data = fs.readFileSync(path.join(import.meta.dirname, 'file.bin'))
const protocol = binread(new BinaryReader(data), Protocol)
```

Expand Down Expand Up @@ -156,7 +156,7 @@ The following code snippets shows how to serialize an object into a binary
buffer based on a Binspector definition. That buffer is then saved into a file.

```typescript
import { BinaryWriter, binwrite }
import { BinaryWriter, binwrite } from 'binspector'
import { promises as fs } from 'fs'
import path from 'path'

Expand Down
6 changes: 3 additions & 3 deletions example/bitmap/bmp.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { PrimitiveSymbol, Relation, Count, Enum, IfThen, Else, Choice, Matrix, Offset, Uint8, Uint16, Uint32, Ascii, Endian, BinaryCursorEndianness } from '../../src'
import { PrimitiveSymbol, Relation, Count, Enum, IfThen, Else, Choice, Matrix, Offset, Uint8, Uint16, Uint32, Ascii, Endian, BinaryCursorEndianness } from '../../src/index.ts'
import {
OS22XBITMAPHEADER, BITMAPINFOHEADER, BITMAPV2INFOHEADER, BITMAPV3INFOHEADER, BITMAPV4INFOHEADER, BITMAPV5INFOHEADER,
} from './header'
import { printColour } from './renderer'
} from './header.ts'
import { printColour } from './renderer.ts'

enum BitmapHeaderTypes {
BM = 'BM',
Expand Down
4 changes: 2 additions & 2 deletions example/bitmap/header.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Count, Enum, Match, Uint8, Uint16, Uint32 } from '../../src'
import { BitmapCompression } from './compression'
import { Count, Enum, Match, Uint8, Uint16, Uint32 } from '../../src/index.ts'
import { BitmapCompression } from './compression.ts'

export class OS21XBITMAPHEADER {
@Uint16
Expand Down
14 changes: 7 additions & 7 deletions example/bitmap/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { promises as fs } from 'fs'
import path from 'path'
import { Bitmap } from './bmp'
import * as path from 'node:path'
import { fileURLToPath } from 'node:url'
import { Bitmap } from './bmp.ts'

test('Testing Bitmap "sample.bmp" read/write equality', async () => {
const data = await fs.readFile(path.join(__dirname, 'sample.bmp'))
expect(data.buffer).binReadWriteEquality(Bitmap)
const filename = path.join(path.dirname(fileURLToPath(import.meta.url)), 'sample.bmp')
expect(filename).fileReadWriteEquality(Bitmap)
})

test('Testing Bitmap "lena.bmp" read/write equality', async () => {
const data = await fs.readFile(path.join(__dirname, 'lena.bmp'))
expect(data.buffer).binReadWriteEquality(Bitmap)
const filename = path.join(path.dirname(fileURLToPath(import.meta.url)), 'lena.bmp')
expect(filename).fileReadWriteEquality(Bitmap)
})
4 changes: 2 additions & 2 deletions example/bson/bson.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Count, Enum, Int32, Match, PrimitiveSymbol, Relation, Select, Size, Uint16, Uint32, Uint8, Utf8, LittleEndian, NullTerminated, BinaryReader, binread, jsonify, binwrite, BinaryWriter } from '../../src'
import { Count, Enum, Int32, Match, PrimitiveSymbol, Relation, Select, Size, Uint16, Uint32, Uint8, Utf8, LittleEndian, NullTerminated, BinaryReader, binread, jsonify, binwrite, BinaryWriter } from '../../src/index.ts'

// When calling JSON.stringify on a BigInt an error will be raised by default
// We need to define the serializer for this type.
Expand Down Expand Up @@ -161,7 +161,7 @@
name: string

@Select(_ => BSONTypeMap[_.bson_type]())
data: any

Check warning on line 164 in example/bson/bson.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

toJson () {
switch (this.bson_type) {
Expand Down Expand Up @@ -218,7 +218,7 @@
return binwrite(new BinaryWriter(), Bson, this).buffer()
}

static from (buf: ArrayBuffer) {
static from (buf: ArrayBufferLike) {
return binread(new BinaryReader(buf), Bson)
}
}
18 changes: 9 additions & 9 deletions example/bson/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { promises as fs } from 'fs'
import path from 'path'
import { Bson } from './bson'
import * as path from 'node:path'
import { fileURLToPath } from 'node:url'
import { Bson } from './bson.ts'

test('Testing BSON "helloworld.bson" read/write equality', async () => {
const data = await fs.readFile(path.join(__dirname, 'helloworld.bson'))
expect(data.buffer).binReadWriteEquality(Bson)
test('Testing BSON "helloworld.bson" read/write equality', () => {
const filename = path.join(path.dirname(fileURLToPath(import.meta.url)), 'helloworld.bson')
expect(filename).fileReadWriteEquality(Bson)
})

test('Testing BSON "nested.bson" read/write equality', async () => {
const data = await fs.readFile(path.join(__dirname, 'nested.bson'))
expect(data.buffer).binReadWriteEquality(Bson)
test('Testing BSON "nested.bson" read/write equality', () => {
const filename = path.join(path.dirname(fileURLToPath(import.meta.url)), 'nested.bson')
expect(filename).fileReadWriteEquality(Bson)
})
4 changes: 2 additions & 2 deletions example/bson/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { promises as fs } from 'node:fs'
import path from 'node:path'
import { Bson } from './bson'
import { Bson } from './bson.ts'

const USAGE = `
Usage:

> deno --unstable-sloppy-imports index.ts <file>.bson
> deno run index.ts <file>.bson
`

if (process.argv[1] === import.meta.filename) {
Expand Down
2 changes: 1 addition & 1 deletion example/devicetree/devicetree.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NullTerminatedString, Choice, Relation, Count, Match, While, Enum, Peek, Offset, Until, EOF, Uint8, Uint32, Uint64, Padding, Endian, BinaryCursorEndianness } from '../../src'
import { NullTerminatedString, Choice, Relation, Count, Match, While, Enum, Peek, Offset, Until, EOF, Uint8, Uint32, Uint64, Padding, Endian, BinaryCursorEndianness } from '../../src/index.ts'

enum DTBStructureBlockToken {
FDT_BEGIN_NODE = 0x1,
Expand Down Expand Up @@ -131,7 +131,7 @@
}

function asObjectDtb (structs: DTBStructBlock[]): object {
function setObject (o: Record<string, any>, current: string[], key: string, value: any) {

Check warning on line 134 in example/devicetree/devicetree.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 134 in example/devicetree/devicetree.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
const currentObj = current.reduce((obj, k) => obj[k], o)

currentObj[key] = value
Expand Down Expand Up @@ -188,7 +188,7 @@
@NullTerminatedString()
strings: string[]

asObject (): Record<string, any> {

Check warning on line 191 in example/devicetree/devicetree.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
return Reflect.get(asObjectDtb(this.structs), '')
}
}
11 changes: 5 additions & 6 deletions example/devicetree/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { test } from '@jest/globals'
import { promises as fs } from 'fs'
import path from 'path'
import { DTB } from './devicetree'
import * as path from 'node:path'
import { fileURLToPath } from 'node:url'
import { DTB } from './devicetree.ts'

test('Testing DTB "am335x-bone.dtb" read/write equality', async () => {
const data = await fs.readFile(path.join(__dirname, 'am335x-bone.dtb'))
expect(data.buffer).binReadWriteEquality(DTB)
const filename = path.join(path.dirname(fileURLToPath(import.meta.url)), 'am335x-bone.dtb')
expect(filename).fileReadWriteEquality(DTB)
})
13 changes: 6 additions & 7 deletions example/png/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { test } from '@jest/globals'
import { promises as fs } from 'fs'
import path from 'path'
import { PNG } from './png'
import * as path from 'node:path'
import { fileURLToPath } from 'node:url'
import { PNG } from './png.ts'

test('Testing PNG "sample.png" read/write equality', async () => {
const data = await fs.readFile(path.join(__dirname, 'sample.png'))
expect(data.buffer).binReadWriteEquality(PNG)
test('Testing PNG "sample.png" read/write equality', () => {
const filename = path.join(path.dirname(fileURLToPath(import.meta.url)), 'sample.png')
expect(filename).fileReadWriteEquality(PNG)
})
2 changes: 1 addition & 1 deletion example/png/png.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Relation, Count, Match, Validate, While, Enum, Choice, Uint8, Uint16, Uint32, Ascii, Endian, BinaryCursorEndianness } from '../../src'
import { Relation, Count, Match, Validate, While, Enum, Choice, Uint8, Uint16, Uint32, Ascii, Endian, BinaryCursorEndianness } from '../../src/index.ts'

enum PNGTypes {
IHDR = 'IHDR',
Expand Down
14 changes: 12 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@ module.exports = {
'<rootDir>/example/**/*.test.ts'
],
testEnvironment: 'node',
extensionsToTreatAsEsm: ['.ts'],
transform: {
"^.+.tsx?$": ["ts-jest", {
tsconfig: "tsconfig.test.json"
'^.+.tsx?$': ['ts-jest', {
tsconfig: "tsconfig.test.json",
useESM: true,
babelConfig: {
plugins: [
["@babel/plugin-proposal-decorators", { version: "2023-11" }],
],
},
}],
},
moduleNameMapper: {
'(.+)\\.js': '$1'
},
verbose: true,
}
33 changes: 21 additions & 12 deletions jest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import { binread, binwrite, BinaryReader, BinaryWriter } from './src'
import * as fs from 'node:fs'
import { binread, binwrite, BinaryReader, BinaryWriter } from './src/index'

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace jest {
interface Matchers<R> {
toBeEqualArrayBuffer (expected: ArrayBuffer): R
toBeEqualArrayBuffer (expected: Uint8Array): R
binReadWriteEquality (ObjectDefinition: any): R

Check warning on line 9 in jest.setup.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
fileReadWriteEquality (ObjectDefinition: any): R

Check warning on line 10 in jest.setup.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
}
}
}

function equalArrayBuffer (buf1: ArrayBuffer, buf2: ArrayBuffer) {
const arr1 = new Uint8Array(buf1)
const arr2 = new Uint8Array(buf2)

function equalArrayBuffer (arr1: Uint8Array, arr2: Uint8Array) {
if (arr1.byteLength !== arr2.byteLength) {
return {
message: () => `Buffer length not matching ${arr1.byteLength} !== ${arr2.byteLength} | ${arr1} !== ${arr2}`,
pass: false
}
}

for (let i = 0; i != buf1.byteLength; i++) {
for (let i = 0; i != arr1.byteLength; i++) {
if (arr1[i] !== arr2[i]) return {
message: () => `Buffer not matching '${arr1[i]} !== ${arr2[i]}' at position ${i}`,
pass: false
Expand All @@ -35,16 +34,26 @@
}

expect.extend({
toBeEqualArrayBuffer (buf1: ArrayBuffer, buf2: ArrayBuffer) {
return equalArrayBuffer(buf1, buf2)
toBeEqualArrayBuffer (arr1: Uint8Array, arr2: Uint8Array) {
return equalArrayBuffer(arr1, arr2)
},
binReadWriteEquality (buf: ArrayBuffer, ObjectDefinition: any) {
const decoded = binread(new BinaryReader(buf), ObjectDefinition)
binReadWriteEquality (arr: ArrayBuffer, ObjectDefinition: any) {

Check warning on line 40 in jest.setup.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
const decoded = binread(new BinaryReader(arr), ObjectDefinition)

const writtenBuf = new BinaryWriter()
binwrite(writtenBuf, ObjectDefinition, decoded)

return equalArrayBuffer(new Uint8Array(arr), new Uint8Array(writtenBuf.buffer()))
},
fileReadWriteEquality (filename: string, ObjectDefinition: any) {

Check warning on line 48 in jest.setup.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
const data = fs.readFileSync(filename)

const decoded = binread(new BinaryReader(data), ObjectDefinition)

const writtenBuf = new BinaryWriter()
binwrite(writtenBuf, ObjectDefinition, decoded)

return equalArrayBuffer(buf, writtenBuf.buffer())
return equalArrayBuffer(data, new Uint8Array(writtenBuf.buffer()))
},
})

Expand Down
Loading