Skip to content

Commit

Permalink
setup deno fmt (gh-actions)
Browse files Browse the repository at this point in the history
  • Loading branch information
hansSchall committed Nov 24, 2023
1 parent 12145e1 commit b561078
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 93 deletions.
36 changes: 36 additions & 0 deletions .github/actions/deno.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Deno

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Setup repo
uses: actions/checkout@v3

- name: Setup Deno
# uses: denoland/setup-deno@v1
uses: denoland/setup-deno@61fe2df320078202e33d7d5ad347e7dcfa0e8f31 # v1.1.2
with:
deno-version: v1.x

- name: Verify formatting
run: deno fmt --check

- name: Run linter
run: deno lint

- name: Run TSC
run: deno check mod.ts

- name: Run tests
run: deno test
5 changes: 4 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"tasks": {
"test": "deno check mod.ts && deno test"
"test": "deno fmt && deno lint && deno check mod.ts && deno test"
},
"fmt": {
"indentWidth": 4
}
}
4 changes: 2 additions & 2 deletions examples/receiver1.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* LMGU-Technik sACN-Deno
* Copyright (C) 2023 Hans Schallmoser
Expand Down Expand Up @@ -29,6 +29,6 @@ async function main() {
console.log(`Chan ${univ}/${addr} = ${value}`);
}
}
main().catch(err => {
main().catch((err) => {
console.error(err);
});
2 changes: 1 addition & 1 deletion lib/dmxAddr.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* LMGU-Technik sACN-Deno
* Copyright (C) 2023 Hans Schallmoser
Expand Down
2 changes: 1 addition & 1 deletion lib/dmxAddr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* LMGU-Technik sACN-Deno
* Copyright (C) 2023 Hans Schallmoser
Expand Down
2 changes: 1 addition & 1 deletion lib/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* LMGU-Technik sACN-Deno
* Copyright (C) 2023 Hans Schallmoser
Expand Down
10 changes: 6 additions & 4 deletions lib/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* LMGU-Technik sACN-Deno
* Copyright (C) 2023 Hans Schallmoser
Expand All @@ -21,16 +21,18 @@ export function multicastGroup(universe: number): string {
if ((universe > 0 && universe < 64000)) {
return `239.255.${universe >> 8}.${universe & 0xFF}`;
}
throw new RangeError('universe must be between 1-63999');
throw new RangeError("universe must be between 1-63999");
}

export function bufferEqual(a: Uint8Array, b: Uint8Array) {
if (a.byteLength != b.byteLength)
if (a.byteLength != b.byteLength) {
return false;
}

for (let i = 0; i < a.byteLength; i++) {
if (a[i] !== b[i])
if (a[i] !== b[i]) {
return false;
}
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference lib="deno.unstable" />

/*
/*
* LMGU-Technik sACN-Deno
* Copyright (C) 2023 Hans Schallmoser
Expand Down
44 changes: 22 additions & 22 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,29 @@ import {...} from "https://deno.land/x/sacn/mod.ts"
import { globalToDmx, Receiver } from "https://deno.land/x/sacn/mod.ts";

const receiver = new Receiver({
// options
// options
});
await receiver.addUniverse(1);

for await (const [chan, value] of receiver) {
const [univ, addr] = globalToDmx(chan);
console.log(`Chan ${univ}/${addr} = ${value}`);
const [univ, addr] = globalToDmx(chan);
console.log(`Chan ${univ}/${addr} = ${value}`);
}
```

### `new Receiver(options: ReceiverOptions)`

```typescript
interface ReceiverOptions {
// nearly every implementation uses this port
// defaults to 5568
readonly port: number;
// network interface to listen on
// defaults to all (0.0.0.0)
readonly iface: string;
// drop all non-zero start code packets
// defaults to true
readonly dmxAOnly: boolean;
// nearly every implementation uses this port
// defaults to 5568
readonly port: number;
// network interface to listen on
// defaults to all (0.0.0.0)
readonly iface: string;
// drop all non-zero start code packets
// defaults to true
readonly dmxAOnly: boolean;
}
```

Expand All @@ -66,9 +66,9 @@ Used to obtain value changes

```typescript
for await (const [chan, value] of receiver) {
// chan is a global address, this helper function can be used to split into universe and address
const [univ, addr] = globalToDmx(chan);
console.log(`Chan ${univ}/${addr} = ${value}`);
// chan is a global address, this helper function can be used to split into universe and address
const [univ, addr] = globalToDmx(chan);
console.log(`Chan ${univ}/${addr} = ${value}`);
}
```

Expand All @@ -78,18 +78,18 @@ Advanced: Used to obtain bare packets

```typescript
for await (const packet of receiver.onPacket()) {
// do stuff ...
// do stuff ...
}
```

```typescript
interface Packet {
cid: Uint8Array;
priority: number;
sequence: number;
universe: number;
data: Uint8Array;
sourceLabel: string;
cid: Uint8Array;
priority: number;
sequence: number;
universe: number;
data: Uint8Array;
sourceLabel: string;
}
```

Expand Down
50 changes: 32 additions & 18 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
export const ACN_PID = new Uint8Array([
0x41, // A
0x53, // S
0x43, // C
0x2d, // -
0x45, // E
0x31, // 1
0x2e, // .
0x31, // 1
0x37, // 7
0x00,
0x00,
0x00,
0x41, // A
0x53, // S
0x43, // C
0x2d, // -
0x45, // E
0x31, // 1
0x2e, // .
0x31, // 1
0x37, // 7
0x00,
0x00,
0x00,
]);

/**
Expand All @@ -19,19 +19,33 @@ export const ACN_PID = new Uint8Array([
* - E1.31
*/
export const DEFAULT_CID = new Uint8Array([
0x6b, 0x79, 0x6c, 0x65, 0x48, 0x65, 0x6e, 0x73, 0x65, 0x6c, 0x44, 0x65, 0x66,
0x61, 0x75, 0x6c,
0x6b,
0x79,
0x6c,
0x65,
0x48,
0x65,
0x6e,
0x73,
0x65,
0x6c,
0x44,
0x65,
0x66,
0x61,
0x75,
0x6c,
]);

export enum RootVector {
DATA = 4,
EXTENDED = 8,
DATA = 4,
EXTENDED = 8,
}
export enum FrameVector {
DATA = 2,
DATA = 2,
}
export enum DmpVector {
DATA = 2,
DATA = 2,
}

// export enum ExtendedFrameVector { SYNC = 1, DISCOVERY = 2 }
11 changes: 7 additions & 4 deletions src/packet.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* LMGU-Technik sACN-Deno
* Copyright (C) 2023 Hans Schallmoser
Expand All @@ -21,7 +21,10 @@ import { assertEquals } from "https://deno.land/std@0.207.0/assert/mod.ts";
import { buildFlagsAndLength } from "./packet.ts";

Deno.test("Flags & Length", () => {
assertEquals(buildFlagsAndLength(0, {
data: new Uint8Array(513)
}), 638 | (0x7 << 12));
assertEquals(
buildFlagsAndLength(0, {
data: new Uint8Array(513),
}),
638 | (0x7 << 12),
);
});
36 changes: 22 additions & 14 deletions src/packet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* LMGU-Technik sACN-Deno
* Copyright (C) 2023 Hans Schallmoser
Expand All @@ -17,24 +17,25 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import { ACN_PID, DmpVector, FrameVector, RootVector } from './constants.ts';
import { ACN_PID, DmpVector, FrameVector, RootVector } from "./constants.ts";

export interface Packet {
cid: Uint8Array,
priority: number,
sequence: number,
universe: number,
cid: Uint8Array;
priority: number;
sequence: number;
universe: number;
data: Uint8Array;
sourceLabel: string,
sourceLabel: string;
}

export class PacketError extends Error {

}

function constantField(value: number, expected: number, id: string) {
if (value !== expected) {
throw new PacketError(`[PacketError] ${id}: '${value}' !== '${expected}'`);
throw new PacketError(
`[PacketError] ${id}: '${value}' !== '${expected}'`,
);
}
}

Expand All @@ -51,16 +52,20 @@ export function parsePacket(packet: Uint8Array): Packet {
constantField(dv.getUint16(2), 0x0000, "RLP Postamble Size");
const packetIdentifier = new Uint8Array(packet.buffer, 4, 12);
for (let i = 0; i < 12; i++) {
constantField(packetIdentifier[i], ACN_PID[i], `RLP ACN Packet Identifier byte ${i}`);
constantField(
packetIdentifier[i],
ACN_PID[i],
`RLP ACN Packet Identifier byte ${i}`,
);
}
constantField(dv.getUint32(18), RootVector.DATA, "Root Vector");
const cid = new Uint8Array(packet.buffer, 22, 16);
constantField(dv.getUint32(40), FrameVector.DATA, "Frame Vector");
const sourceName = new Uint8Array(packet.buffer, 44, 64);
const sourceLabel = new TextDecoder()
.decode(
sourceName.slice(0,
sourceName.findLastIndex(_ => _ !== 0) + 1)); // last non-null character
sourceName.slice(0, sourceName.findLastIndex((_) => _ !== 0) + 1),
); // last non-null character
const priority = dv.getUint8(108);
// const sync = dv.getUint16(109);
const sequence = dv.getUint8(111);
Expand All @@ -86,7 +91,8 @@ export function parsePacket(packet: Uint8Array): Packet {
export function buildFlagsAndLength(pos: number, packet: {
data: Uint8Array;
}) {
return ((125 + packet.data.byteLength) - pos) & 0b1111_1111_1111 | (0x7 << 12);
return ((125 + packet.data.byteLength) - pos) & 0b1111_1111_1111 |
(0x7 << 12);
}

export function buildPacket(packet: Packet) {
Expand All @@ -100,7 +106,9 @@ export function buildPacket(packet: Packet) {
new Uint8Array(res, 22, 16).set(packet.cid);
dv.setUint16(38, buildFlagsAndLength(38, packet));
dv.setUint32(40, FrameVector.DATA);
new Uint8Array(res, 44, 64).set(new TextEncoder().encode(packet.sourceLabel));
new Uint8Array(res, 44, 64).set(
new TextEncoder().encode(packet.sourceLabel),
);
dv.setUint8(108, packet.priority);
// 109 sync
dv.setUint8(111, packet.sequence);
Expand Down
Loading

0 comments on commit b561078

Please sign in to comment.