Skip to content

Commit

Permalink
Minor tweaks (#305)
Browse files Browse the repository at this point in the history
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
  • Loading branch information
vicb and sourcery-ai[bot] authored Sep 3, 2024
1 parent 936bf15 commit 5d5a8fa
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 44 deletions.
2 changes: 1 addition & 1 deletion apps/fetcher/src/app/trackers/meshbir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class MeshBirFetcher extends TrackerFetcher {
for (const dsId of devices) {
const tracker = this.getTracker(dsId);
if (tracker == null) {
updates.trackerErrors.set(dsId, `Not found ${tracker.account}`);
updates.trackerErrors.set(dsId, `Not found`);
continue;
}
if (validateMeshBirAccount(tracker.account) === false) {
Expand Down
2 changes: 1 addition & 1 deletion apps/fetcher/src/app/trackers/zoleo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class ZoleoFetcher extends TrackerFetcher {
for (const dsId of devices) {
const tracker = this.getTracker(dsId);
if (tracker == null) {
updates.trackerErrors.set(dsId, `Not found ${tracker.account}`);
updates.trackerErrors.set(dsId, `Not found`);
continue;
}
if (validateZoleoAccount(tracker.account) === false) {
Expand Down
127 changes: 86 additions & 41 deletions apps/fxc-server/src/app/routes/meshbir.test.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,94 @@
import { parseMessage } from './meshbir';

describe('parseMessage', () => {
it('should parse a position', () => {
expect(
parseMessage({
type: 'position',
user_id: '12345678-1234-1234-1234-123456789012',
time: 123456,
latitude: 32.1927,
longitude: 76.4506,
altitude: 1778.12,
ground_speed: 30,
}),
).toMatchInlineSnapshot(`
{
"altitude": 1778.12,
"ground_speed": 30,
"latitude": 32.1927,
"longitude": 76.4506,
"time": 123456,
"type": "position",
"user_id": "12345678-1234-1234-1234-123456789012",
}
`);
});
describe('valid messages', () => {
it('should parse a position', () => {
expect(
parseMessage({
type: 'position',
user_id: '12345678-1234-1234-1234-123456789012',
time: 123456,
latitude: 32.1927,
longitude: 76.4506,
altitude: 1778.12,
ground_speed: 30,
}),
).toMatchInlineSnapshot(`
{
"altitude": 1778.12,
"ground_speed": 30,
"latitude": 32.1927,
"longitude": 76.4506,
"time": 123456,
"type": "position",
"user_id": "12345678-1234-1234-1234-123456789012",
}
`);
});

it('should parse a text message', () => {
expect(
parseMessage({
type: 'message',
user_id: '12345678-1234-1234-1234-123456789012',
time: 123456,
message: 'hello Meshtastic',
}),
).toMatchInlineSnapshot(`
{
"message": "hello Meshtastic",
"time": 123456,
"type": "message",
"user_id": "12345678-1234-1234-1234-123456789012",
}
`);
});

it('should parse a test', () => {
expect(
parseMessage({
type: 'message',
user_id: '12345678-1234-1234-1234-123456789012',
time: 123456,
message: 'hello Meshtastic',
}),
).toMatchInlineSnapshot(`
{
"message": "hello Meshtastic",
"time": 123456,
"type": "message",
"user_id": "12345678-1234-1234-1234-123456789012",
}
`);
it('should allow extra fields', () => {
expect(
parseMessage({
type: 'message',
user_id: '12345678-1234-1234-1234-123456789012',
time: 123456,
message: 'hello Meshtastic',
extra: 123,
}),
).toMatchInlineSnapshot(`
{
"message": "hello Meshtastic",
"time": 123456,
"type": "message",
"user_id": "12345678-1234-1234-1234-123456789012",
}
`);
});
});

it('should throw on invalid message', () => {
expect(() => parseMessage({ type: 'unkown' })).toThrow();
describe('invalid messages', () => {
it('should throw on invalid message', () => {
expect(() => parseMessage({ type: 'unkown' })).toThrowErrorMatchingInlineSnapshot(`"Invalid message format"`);
});

it('should throw on invalid values', () => {
expect(() =>
parseMessage({
type: 'message',
user_id: '12345678-1234-1234-1234-123456789012',
time: '123456', // should be a number
message: 'hello Meshtastic',
}),
).toThrowErrorMatchingInlineSnapshot(`"Invalid message format"`);
});

it('should throw on missing values', () => {
expect(() =>
parseMessage({
type: 'message',
user_id: '12345678-1234-1234-1234-123456789012',
//time: 123456,
message: 'hello Meshtastic',
}),
).toThrowErrorMatchingInlineSnapshot(`"Invalid message format"`);
});
});
});
10 changes: 9 additions & 1 deletion apps/fxc-server/src/app/routes/meshbir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Secrets } from '@flyxc/secrets';
import type { Request, Response } from 'express';
import { Router } from 'express';
import type Redis from 'ioredis';
import { ZodError } from 'zod';

export function getMeshBirRouter(redis: Redis): Router {
const router = Router();
Expand Down Expand Up @@ -34,5 +35,12 @@ export function getMeshBirRouter(redis: Redis): Router {
// Parses meshbir messages.
// Throws when the message is invalid.
export function parseMessage(message: unknown): MeshBirMessage | undefined {
return textSchema.or(positionSchema).parse(message);
try {
return textSchema.or(positionSchema).parse(message);
} catch (e) {
if (e instanceof ZodError) {
throw new Error(`Invalid message format`);
}
throw new Error(`Unexpected error during message parsing`);
}
}
1 change: 1 addition & 0 deletions libs/common-node/src/lib/meshtbir.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { z } from 'zod';

// Maximum size of a single message in bytes
export const MESHBIR_MAX_MSG_SIZE = 250;
// Consume at max 1MB of memory
export const MESHBIR_MAX_MSG = Math.floor(1e6 / MESHBIR_MAX_MSG_SIZE);
Expand Down
2 changes: 2 additions & 0 deletions libs/common/src/lib/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ export function validateXContestAccount(id: string): string | false {
}

// Validates a Meshtastic ID.
//
// The format is UUID, with groups of hex digits: "8-4-4-4-12"
export function validateMeshBirAccount(id: string): string | false {
id = id.trim();
return /^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/i.test(id) ? id.toUpperCase() : false;
Expand Down

0 comments on commit 5d5a8fa

Please sign in to comment.