Skip to content

Commit

Permalink
chore: add runtime checking of types into binary protocol (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-greene-ck authored Aug 15, 2019
1 parent 1cdf0ad commit 9e5c1e6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
8 changes: 4 additions & 4 deletions packages/thrift-integration/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ export function createClientServer(
'/calculate',
(req: express.Request, res: express.Response): void => {
const work: IWorkArgs = {
num1: req.query.left,
num2: req.query.right,
num1: Number(req.query.left),
num2: Number(req.query.right),
op: symbolToOperation(req.query.op),
}

Expand All @@ -127,8 +127,8 @@ export function createClientServer(
'/calculate-overwrite',
(req: express.Request, res: express.Response): void => {
const work: Work = new Work({
num1: req.query.left,
num2: req.query.right,
num1: Number(req.query.left),
num2: Number(req.query.right),
op: symbolToOperation(req.query.op),
})

Expand Down
34 changes: 28 additions & 6 deletions packages/thrift-server-core/src/main/protocols/BinaryProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,29 +112,49 @@ export class BinaryProtocol extends TProtocol {
}

public writeByte(byte: number): void {
this.transport.write(Buffer.from([byte]))
if (typeof byte === 'number') {
this.transport.write(Buffer.from([byte]))
} else {
throw new TypeError(`Expected number but found type ${typeof byte}`)
}
}

public writeI16(i16: number): void {
this.transport.write(binary.writeI16(Buffer.alloc(2), i16))
if (typeof i16 === 'number') {
this.transport.write(binary.writeI16(Buffer.alloc(2), i16))
} else {
throw new TypeError(`Expected number but found type ${typeof i16}`)
}
}

public writeI32(i32: number): void {
this.transport.write(binary.writeI32(Buffer.alloc(4), i32))
if (typeof i32 === 'number') {
this.transport.write(binary.writeI32(Buffer.alloc(4), i32))
} else {
throw new TypeError(`Expected number but found type ${typeof i32}`)
}
}

public writeI64(i64: number | Int64): void {
if (typeof i64 === 'number') {
this.transport.write(new Int64(i64).buffer)
} else if (typeof i64 === 'string') {
this.transport.write(Int64.fromDecimalString(i64).buffer)
} else {
} else if (i64 instanceof Int64) {
this.transport.write(i64.buffer)
} else {
throw new TypeError(
`Expected Int64 or number but found type ${typeof i64}`,
)
}
}

public writeDouble(dub: number): void {
this.transport.write(binary.writeDouble(Buffer.alloc(8), dub))
if (typeof dub === 'number') {
this.transport.write(binary.writeDouble(Buffer.alloc(8), dub))
} else {
throw new TypeError(`Expected number but found type ${typeof dub}`)
}
}

public writeStringOrBinary(
Expand All @@ -149,7 +169,9 @@ export class BinaryProtocol extends TProtocol {
this.writeI32(data.length)
this.transport.write(data)
} else {
throw new TypeError(`Argument of type ${typeof data} should be buffer or string`)
throw new TypeError(
`Argument of type ${typeof data} should be buffer or string`,
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,12 @@ export class BufferedTransport extends TTransport {
}

public write(buf: Buffer): void {
this.outBuffers.push(buf)
this.outCount += buf.length
if (buf instanceof Buffer) {
this.outBuffers.push(buf)
this.outCount += buf.length
} else {
throw new TypeError(`Expected buffer but found type ${typeof buf}`)
}
}

public flush(): Buffer {
Expand Down

0 comments on commit 9e5c1e6

Please sign in to comment.