Skip to content

Commit

Permalink
feat: better error, add some test
Browse files Browse the repository at this point in the history
  • Loading branch information
T1B0 committed Jan 18, 2024
1 parent 30521f8 commit 35af9b5
Show file tree
Hide file tree
Showing 15 changed files with 215 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/wire/identifier.utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('serializeIdentifier', () => {
);
});

it('throw on login > 255 bytes - utf8 version', () => {
it('throw on string id > 255 bytes - utf8 version', () => {
assert.throws(
() => serializeIdentifier('¥Ø£Ø'.repeat(33)),
);
Expand Down
2 changes: 1 addition & 1 deletion src/wire/identifier.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const serializeStringId = (id: string): Buffer => {
const b = Buffer.alloc(1 + 1);
const bId = Buffer.from(id);
if (bId.length < 1 || bId.length > 255)
throw new Error('identifier/name should not exceed 255 chars');
throw new Error('identifier/name should be between 1 and 255 bytes');
b.writeUInt8(STRING);
b.writeUInt8(bId.length, 1);
return Buffer.concat([
Expand Down
27 changes: 27 additions & 0 deletions src/wire/partition/partition.utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { serializePartitionParams } from './partition.utils.js';

describe('serializePartitionParams', () => {

it('serialize 2 numeric id & a uint32 into buffer', () => {
assert.deepEqual(
serializePartitionParams(12, 34, 512).length,
6 + 6 + 4
);
});

it('throw on partition count < 1', () => {
assert.throws(
() => serializePartitionParams(2, 1, 0),
);
});

it('throw on partition count > 1000', () => {
assert.throws(
() => serializePartitionParams(1, 1, 1001),
);
});

});
2 changes: 1 addition & 1 deletion src/wire/partition/partition.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const serializePartitionParams = (
) => {

if (partitionCount < 1 || partitionCount > 1000)
throw new Error('Topic partition_count should be between 1 and 1000');
throw new Error('Topic partition_count must be between 1 and 1000');

const streamIdentifier = serializeIdentifier(streamId);
const topicIdentifier = serializeIdentifier(topicId);
Expand Down
37 changes: 37 additions & 0 deletions src/wire/session/login-with-token.command.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { LOGIN_WITH_TOKEN } from './login-with-token.command.js';


describe("Login with token Command", () => {

// @warn use ascii char to keep char.length === byteLength
const token = 'thisIsBigSecretToken123';

it("serialize token into a buffer", () => {
assert.deepEqual(
LOGIN_WITH_TOKEN.serialize(token).length,
1 + token.length
);
});

it("throw on empty token", () => {
assert.throws(
() => LOGIN_WITH_TOKEN.serialize("")
);
});

it("throw on token > 255 bytes", () => {
assert.throws(
() => LOGIN_WITH_TOKEN.serialize("YoLo".repeat(65))
);
});

it("throw on login > 255 bytes - utf8 version", () => {
assert.throws(
() => LOGIN_WITH_TOKEN.serialize("¥Ø£Ø".repeat(33))
);
});

});
4 changes: 2 additions & 2 deletions src/wire/session/login-with-token.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export const LOGIN_WITH_TOKEN = {

serialize: (token: string) => {
const bToken = Buffer.from(token);
if (bToken.length > 255)
throw new Error('Token token should not exceed 255 chars');
if (bToken.length < 1 || bToken.length > 255)
throw new Error('Token length should be between 1 and 255 bytes');
const b = Buffer.alloc(1);
b.writeUInt8(bToken.length);
return Buffer.concat([
Expand Down
4 changes: 2 additions & 2 deletions src/wire/session/login.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export const LOGIN = {
const bPassword = Buffer.from(password);

if (bUsername.length < 1 || bUsername.length > 255)
throw new Error('username should not exceed 255 chars');
throw new Error('Username should be between 1 and 255 bytes');
if (bPassword.length < 1 || bPassword.length > 255)
throw new Error('password name should not exceed 255 chars');
throw new Error('Password should be between 1 and 255 bytes');

const l1 = Buffer.alloc(1);
const l2 = Buffer.alloc(1);
Expand Down
37 changes: 37 additions & 0 deletions src/wire/stream/create-stream.command.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { CREATE_STREAM } from './create-stream.command.js';

describe('CreateStream', () => {

describe('serialize', () => {

it('serialize 1 numeric id & 1 name into buffer', () => {
const name = 'test-stream';
assert.deepEqual(
CREATE_STREAM.serialize(1, name).length,
4 + 1 + name.length
);
});

it('throw on name < 1', () => {
assert.throws(
() => CREATE_STREAM.serialize(1, '')
);
});

it("throw on name > 255 bytes", () => {
assert.throws(
() => CREATE_STREAM.serialize(1, "YoLo".repeat(65))
);
});

it("throw on name > 255 bytes - utf8 version", () => {
assert.throws(
() => CREATE_STREAM.serialize(3, "¥Ø£Ø".repeat(33))
);
});

});
});
6 changes: 2 additions & 4 deletions src/wire/stream/create-stream.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export const CREATE_STREAM = {
code: 202,
serialize: (id: number, name: string) => {
const bName = Buffer.from(name);
if (bName.length > 255)
throw new Error('stream name should not exceed 255 chars');
if (bName.length < 1 || bName.length > 255)
throw new Error('Stream name should be between 1 and 255 bytes');
const b = Buffer.alloc(4 + 1);
b.writeUInt32LE(id, 0);
b.writeUInt8(bName.length, 4);
Expand All @@ -16,8 +16,6 @@ export const CREATE_STREAM = {
]);
},
deserialize: (r: CommandResponse) => {
// response status = 1011 seem to be alreadyExist code
// response status 0 & empty response seem to be OK
return r.status === 0 && r.data.length === 0;
}
};
40 changes: 40 additions & 0 deletions src/wire/token/create-token.command.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { CREATE_TOKEN } from './create-token.command.js';

describe('CreateToken', () => {

describe('serialize', () => {

const name = 'test-token';
const expiry = 1234;

it('serialize 1 name & 1 uint32 into buffer', () => {

assert.deepEqual(
CREATE_TOKEN.serialize(name, expiry).length,
4 + 1 + name.length
);
});

it('throw on name < 1', () => {
assert.throws(
() => CREATE_TOKEN.serialize('', expiry)
);
});

it("throw on name > 255 bytes", () => {
assert.throws(
() => CREATE_TOKEN.serialize("YoLo".repeat(65), expiry)
);
});

it("throw on name > 255 bytes - utf8 version", () => {
assert.throws(
() => CREATE_TOKEN.serialize("¥Ø£Ø".repeat(33), expiry)
);
});

});
});
4 changes: 2 additions & 2 deletions src/wire/token/create-token.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export const CREATE_TOKEN = {

serialize: (name: string, expiry = 600): Buffer => {
const bName = Buffer.from(name);
if (bName.length > 255)
throw new Error('Token name should not exceed 255 chars');
if (bName.length < 1 || bName.length > 255)
throw new Error('Token name should be between 1 and 255 bytes');
const b1 = Buffer.alloc(1);
b1.writeUInt8(bName.length);
const b2 = Buffer.alloc(4);
Expand Down
4 changes: 2 additions & 2 deletions src/wire/token/delete-token.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export const DELETE_TOKEN = {

serialize: (name: string): Buffer => {
const bName = Buffer.from(name);
if (bName.length > 255)
throw new Error('Token name should not exceed 255 chars');
if (bName.length < 1 || bName.length > 255)
throw new Error('Token name should be between 1 and 255 bytes');
const b = Buffer.alloc(1);
b.writeUInt8(bName.length);
return Buffer.concat([
Expand Down
59 changes: 59 additions & 0 deletions src/wire/topic/create-topic.command.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { CREATE_TOPIC } from './create-topic.command.js';

describe('CreateTopic', () => {

describe('serialize', () => {
// serialize: (
// streamId: Id,
// topicId: number,
// name: string,
// partitionCount: number,
// messageExpiry = 0,
// maxTopicSize = 0,
// replicationFactor = 1
// ) => { ... }

const name = 'test-topic';

it('serialize 1 numeric id & 1 name into buffer', () => {
assert.deepEqual(
CREATE_TOPIC.serialize(1, 2, name, 1, 0, 0, 1).length,
6 + 4 + 4 + 4 + 8 + 1 + 1 + name.length
);
});

it('throw on name < 1', () => {
assert.throws(
() => CREATE_TOPIC.serialize(1, 2, '', 1)
);
});

it("throw on name > 255 bytes", () => {
assert.throws(
() => CREATE_TOPIC.serialize(1, 2, "YoLo".repeat(65), 1)
);
});

it("throw on name > 255 bytes - utf8 version", () => {
assert.throws(
() => CREATE_TOPIC.serialize(1, 3, "¥Ø£Ø".repeat(33), 2)
);
});

it('throw on replication_factor < 1', () => {
assert.throws(
() => CREATE_TOPIC.serialize(1, 2, name, 1, 0, 0, 0),
);
});

it('throw on replication_factor > 255', () => {
assert.throws(
() => CREATE_TOPIC.serialize(1, 2, name, 1, 0, 0, 256),
);
});

});
});
4 changes: 2 additions & 2 deletions src/wire/topic/create-topic.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export const CREATE_TOPIC = {
const bName = Buffer.from(name)
if (replicationFactor < 1 || replicationFactor > 255)
throw new Error('Topic replication factor should be between 1 and 255');
if (bName.length > 255)
throw new Error('Topic name should not exceed 255 chars');
if (bName.length < 1 || bName.length > 255)
throw new Error('Topic name should be between 1 and 255 bytes');

const b = Buffer.alloc(4 + 4 + 4 + 8 + 1 + 1);
b.writeUInt32LE(topicId, 0);
Expand Down
22 changes: 0 additions & 22 deletions src/wire/user/user.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,25 +228,3 @@ export const deserializeUsers = (p: Buffer, pos = 0): BaseUser[] => {
}
return users;
};

// func DeserializeUsers(payload []byte) ([]*UserResponse, error) {
// if len(payload) == 0 {
// return nil, errors.New("Empty payload")
// }

// var result []*UserResponse
// length := len(payload)
// position := 0

// for position < length {
// response, readBytes, err := deserializeUserResponse(payload, position)
// if err != nil {
// return nil, err
// }
// result = append(result, response)
// position += readBytes
// }

// return result, nil
// }

0 comments on commit 35af9b5

Please sign in to comment.