Skip to content

Commit

Permalink
default to gzip compression for rpc (keybase#23402)
Browse files Browse the repository at this point in the history
* default to gzip compression for rpc

* use enum exported from rpc lib

* bump framed-msgpack-rpc

* add compressed transport tests
  • Loading branch information
marceloneil authored Apr 1, 2020
1 parent fe99105 commit 6f2376a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 13 deletions.
6 changes: 6 additions & 0 deletions shared/custom-d.ts/framed-msgpack-rpc/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ declare module 'framed-msgpack-rpc' {
set_opt(k: string, v: any): void
}

export const dispatch: {
COMPRESSION_TYPE_NONE: number
COMPRESSION_TYPE_GZIP: number
}

export namespace transport {
class RobustTransport {
constructor(options: Object)
invoke(
i: {
program: string
ctype: number
method: string
args: [Object]
notify: boolean
Expand Down
69 changes: 61 additions & 8 deletions shared/engine/__tests__/transport-shared.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-env jest */
import rpc from 'framed-msgpack-rpc'
import {TransportShared} from '../transport-shared'
import {SendArg} from '../index.platform'

Expand Down Expand Up @@ -37,27 +38,79 @@ describe('TransportShared', () => {
arg1: 5,
arg2: 'value',
}
const invokeArg = {args: [args], method: 'myMethod', notify: false, program: 'myProgram'}

const expectedMessage = [0, 1, 'myProgram.myMethod', [args]]
const invokeArgUncompressed = {
args: [args],
ctype: rpc.dispatch.COMPRESSION_TYPE_NONE,
method: 'myMethod',
notify: false,
program: 'myProgram',
}
const invokeArgCompressed = {
args: [args],
// compressed by default
// ctype: rpc.dispatch.compression_type_gzip
method: 'myMethod',
notify: false,
program: 'myProgram',
}

const expectedMessageUncompressed = [4, 1, 0, 'myProgram.myMethod', [args]]
const expectedMessageCompressed = [
4,
1,
1,
'myProgram.myMethod',
Buffer.from('1f8b08000000000000039bd8b424b128dd9015441a2d2d4bcc294d0500b0970c8c13000000', 'hex'),
]

it('invoke uncompressed', () => {
const t = new FakeTransportShared()

t.connected = true
// Since connected is true, this should call send.
// @ts-ignore codemode issue
t.invoke(invokeArgUncompressed, () => {})
expect(t.lastMessage).toEqual(expectedMessageUncompressed)
})

it('invoke uncompressed queued', () => {
const t = new FakeTransportShared()

t.connected = false
// Since connected is false, this should queue up the message.
// @ts-ignore codemode issue
t.invoke(invokeArgUncompressed, () => {})
expect(t.lastMessage).toBe(null)

t.connected = true
// _flush_queue is defined in Transport in transport.iced in
// framed-msgpack-rpc.
//
// Since connected is true, this should call send.
//
// @ts-ignore codemode issue
t._flush_queue()
expect(t.lastMessage).toEqual(expectedMessageUncompressed)
})

it('invoke', () => {
it('invoke compressed', () => {
const t = new FakeTransportShared()

t.connected = true
// Since connected is true, this should call send.
// @ts-ignore codemode issue
t.invoke(invokeArg, () => {})
expect(t.lastMessage).toEqual(expectedMessage)
t.invoke(invokeArgCompressed, () => {})
expect(t.lastMessage).toEqual(expectedMessageCompressed)
})

it('invoke queued', () => {
it('invoke compressed queued', () => {
const t = new FakeTransportShared()

t.connected = false
// Since connected is false, this should queue up the message.
// @ts-ignore codemode issue
t.invoke(invokeArg, () => {})
t.invoke(invokeArgCompressed, () => {})
expect(t.lastMessage).toBe(null)

t.connected = true
Expand All @@ -68,6 +121,6 @@ describe('TransportShared', () => {
//
// @ts-ignore codemode issue
t._flush_queue()
expect(t.lastMessage).toEqual(expectedMessage)
expect(t.lastMessage).toEqual(expectedMessageCompressed)
})
})
4 changes: 4 additions & 0 deletions shared/engine/transport-shared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ function rpcLog(info: {method: string; reason: string; extra?: Object; type: str

type InvokeArgs = {
program: string
ctype: number
method: string
args: [Object]
notify: boolean
Expand Down Expand Up @@ -174,6 +175,9 @@ class TransportShared extends RobustTransport {

// Override RobustTransport.invoke.
invoke(arg: InvokeArgs, cb: any) {
if (arg.ctype == undefined) {
arg.ctype = rpc.dispatch.COMPRESSION_TYPE_GZIP // default to gzip compression
}
const wrappedInvoke = _wrap({
enforceOnlyOnce: true,
extra: arg.args[0],
Expand Down
2 changes: 1 addition & 1 deletion shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"expo-image-picker": "7.0.0",
"expo-permissions": "7.0.0",
"expo-sms": "7.0.0",
"framed-msgpack-rpc": "1.1.22",
"framed-msgpack-rpc": "1.1.23",
"google-libphonenumber": "3.2.2",
"hoist-non-react-statics": "3.3.2",
"iced-runtime": "1.0.3",
Expand Down
8 changes: 4 additions & 4 deletions shared/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6334,10 +6334,10 @@ fragment-cache@^0.2.1:
dependencies:
map-cache "^0.2.2"

[email protected].22:
version "1.1.22"
resolved "https://registry.yarnpkg.com/framed-msgpack-rpc/-/framed-msgpack-rpc-1.1.22.tgz#ff352f3004272cc61079f6b8d1e570da657e0904"
integrity sha512-Qkx8oJVCK8zuJLivlb24ONMCB5zRpQikcNilHwWJUQn/gBkjelSkLnWahJheunw0deRyh9yMq6MKVdiNNdzvJg==
[email protected].23:
version "1.1.23"
resolved "https://registry.yarnpkg.com/framed-msgpack-rpc/-/framed-msgpack-rpc-1.1.23.tgz#79a055d76e10dfeef02d18fbd7c0c587c4c50f95"
integrity sha512-cP49D7tObCLclfsluLt7POypi/489v3W8J1FcDIQHsUQwqZ4K0gaMlhaO7gHYUcqZAupII/A0DHOadpOOaKs6g==
dependencies:
iced-error "0.0.9"
iced-lock "^1.0.2"
Expand Down

0 comments on commit 6f2376a

Please sign in to comment.