Skip to content

Commit

Permalink
refactor chunking (#27)
Browse files Browse the repository at this point in the history
* refactor chunking

* fix formatting
  • Loading branch information
jleni authored May 26, 2024
1 parent a051496 commit 09de767
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 8 deletions.
Binary file modified bun.lockb
Binary file not shown.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/jest": "29.5.12",
"@types/node": "^20.12.12",
"@typescript-eslint/eslint-plugin": "^7.9.0",
"@typescript-eslint/parser": "^7.9.0",
"eslint": "^9.2.0",
"@typescript-eslint/eslint-plugin": "^7.10.0",
"@typescript-eslint/parser": "^7.10.0",
"eslint": "^9.3.0",
"eslint-config-prettier": "^9.1.0",
"eslint-config-standard-with-typescript": "^39.0.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-n": "^17.7.0",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-tsdoc": "^0.2.17",
"eslint-plugin-unused-imports": "^3.2.0",
"eslint-plugin-unused-imports": "^4.0.0",
"prettier": "^3.2.5",
"ts-jest": "^29.1.2",
"ts-jest": "^29.1.3",
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
},
Expand Down
55 changes: 55 additions & 0 deletions src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,61 @@ describe('BaseApp', () => {
})
})

describe('messageToChunks', () => {
it('should split message into chunks correctly', () => {
// subclassing to expose protected method
class TestBaseApp extends BaseApp {
public messageToChunks(message: Buffer) {
return super.messageToChunks(message)
}
}

const transport = new MockTransport(Buffer.alloc(0))
const app = new TestBaseApp(transport, params)
const message = Buffer.alloc(500)
const chunks = app.messageToChunks(message)

expect(chunks.length).toBeGreaterThan(1)
chunks.forEach((chunk, index) => {
console.log(`Chunk ${index} size: ${chunk.length}`)
expect(chunk.length).toBeLessThanOrEqual(params.chunkSize)
})
})

it('should handle empty message', () => {
// subclassing to expose protected method
class TestBaseApp extends BaseApp {
public messageToChunks(message: Buffer) {
return super.messageToChunks(message)
}
}

const transport = new MockTransport(Buffer.alloc(0))
const app = new TestBaseApp(transport, params)
const message = Buffer.from('')
const chunks = app.messageToChunks(message)

expect(chunks.length).toBe(0)
})

it('should handle message exactly equal to chunk size', () => {
// subclassing to expose protected method
class TestBaseApp extends BaseApp {
public messageToChunks(message: Buffer) {
return super.messageToChunks(message)
}
}

const transport = new MockTransport(Buffer.alloc(0))
const app = new TestBaseApp(transport, params)
const message = Buffer.alloc(params.chunkSize, 'a')
const chunks = app.messageToChunks(message)

expect(chunks.length).toBe(1)
expect(chunks[0].toString()).toBe('a'.repeat(params.chunkSize))
})
})

describe('getVersion', () => {
it('should retrieve version information (5 bytes)', async () => {
const responseBuffer = Buffer.concat([
Expand Down
14 changes: 11 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,19 @@ export default class BaseApp {
* @returns An array of buffers that are ready to be sent.
*/
protected prepareChunks(path: string, message: Buffer): Buffer[] {
const chunks = []
const serializedPathBuffer = this.serializePath(path)
const chunks = this.messageToChunks(message)
chunks.unshift(serializedPathBuffer)
return chunks
}

// First chunk (only path)
chunks.push(serializedPathBuffer)
/**
* Splits a buffer into chunks of `this.CHUNK_SIZE` size.
* @param message - The message to be chunked.
* @returns An array of buffers, each representing a chunk of the original message.
*/
protected messageToChunks(message: Buffer): Buffer[] {
const chunks = []

const messageBuffer = Buffer.from(message)

Expand Down

0 comments on commit 09de767

Please sign in to comment.