Skip to content

Commit

Permalink
ui/next: protobuf-over-HTTP
Browse files Browse the repository at this point in the history
  • Loading branch information
tamird committed Jun 15, 2016
1 parent 27b1fd4 commit 1aa63a3
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 32 deletions.
6 changes: 3 additions & 3 deletions ui/next/app/redux/databaseInfo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ describe("databases reducers", function () {

return {
sendAsJson: false,
body: response.encodeJSON(),
body: response.toArrayBuffer(),
};
});

Expand Down Expand Up @@ -446,7 +446,7 @@ describe("databases reducers", function () {

return {
sendAsJson: false,
body: response.encodeJSON(),
body: response.toArrayBuffer(),
};
});

Expand Down Expand Up @@ -552,7 +552,7 @@ describe("databases reducers", function () {

return {
sendAsJson: false,
body: response.encodeJSON(),
body: response.toArrayBuffer(),
};
});

Expand Down
10 changes: 5 additions & 5 deletions ui/next/app/redux/metrics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ describe("metrics reducer", function() {
fetchMock.mock("/ts/query", "post", (url: string, requestObj: RequestInit) => {
// Assert that metric store's "inFlight" is 1 or 2.
assert.isAtLeast(mockMetricsState.inFlight, 1);
assert.isAtMost(mockMetricsState.inFlight, 1);
assert.isAtMost(mockMetricsState.inFlight, 2);

let request = new protos.cockroach.ts.tspb.TimeSeriesQueryRequest(JSON.parse(requestObj.body as string));
let request = protos.cockroach.ts.tspb.TimeSeriesQueryRequest.decode(requestObj.body as ArrayBuffer);

return {
sendAsJson: false,
Expand All @@ -172,7 +172,7 @@ describe("metrics reducer", function() {
datapoints: [],
};
}),
}).encodeJSON(),
}).toArrayBuffer(),
};
});

Expand Down Expand Up @@ -232,7 +232,7 @@ describe("metrics reducer", function() {
}
successSent = true;

let request = new protos.cockroach.ts.tspb.TimeSeriesQueryRequest(JSON.parse(requestObj.body as string));
let request = protos.cockroach.ts.tspb.TimeSeriesQueryRequest.decode(requestObj.body as ArrayBuffer);

return {
sendAsJson: false,
Expand All @@ -243,7 +243,7 @@ describe("metrics reducer", function() {
datapoints: [],
};
}),
}).encodeJSON(),
}).toArrayBuffer(),
};
});

Expand Down
2 changes: 1 addition & 1 deletion ui/next/app/redux/raft.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe("raft reducer", function() {
successSent = true;
return {
sendAsJson: false,
body: new protos.cockroach.server.serverpb.RaftDebugResponse().encodeJSON(),
body: new protos.cockroach.server.serverpb.RaftDebugResponse().toArrayBuffer(),
};
});

Expand Down
12 changes: 6 additions & 6 deletions ui/next/app/redux/uiData.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,20 @@ describe("UIData reducer", function() {
fetchMock.mock("/_admin/v1/uidata", "post", (url: string, requestObj: RequestInit) => {
assert.equal(state.inFlight, 1);

let kvs = new protos.cockroach.server.serverpb.SetUIDataRequest(JSON.parse(requestObj.body as string)).getKeyValues();
let kvs = protos.cockroach.server.serverpb.SetUIDataRequest.decode(requestObj.body as ArrayBuffer).getKeyValues();

assert.equal(kvs.size, 2);

let deserialize = function(buff: ByteBuffer): any {
return JSON.parse(buff.readString(buff.limit));
let deserialize = function(buff: ByteBuffer): Object {
return JSON.parse(buff.readString(buff.limit - buff.offset));
};

assert.deepEqual(deserialize(kvs.get(uiKey1)), uiObj1);
assert.deepEqual(deserialize(kvs.get(uiKey2)), uiObj2);

return {
sendAsJson: false,
body: new protos.cockroach.server.serverpb.SetUIDataResponse().encodeJSON(),
body: new protos.cockroach.server.serverpb.SetUIDataResponse().toArrayBuffer(),
};
});

Expand Down Expand Up @@ -183,7 +183,7 @@ describe("UIData reducer", function() {
assert.equal(state.inFlight, 1);

let response = new protos.cockroach.server.serverpb.GetUIDataResponse();
let setValue = function(key: string, obj: any) {
let setValue = function(key: string, obj: Object) {
let value = new protos.cockroach.server.serverpb.GetUIDataResponse.Value();
value.setValue(ByteBuffer.fromUTF8(JSON.stringify(obj)));
response.key_values.set(key, value);
Expand All @@ -193,7 +193,7 @@ describe("UIData reducer", function() {

return {
sendAsJson: false,
body: response.encodeJSON(),
body: response.toArrayBuffer(),
};
});

Expand Down
8 changes: 4 additions & 4 deletions ui/next/app/redux/uiData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default function(state = new UIDataSet(), action: Action): UIDataSet {
/**
* setUIDataKey sets the value of the given UIData key.
*/
export function setUIDataKey(key: string, value: any): PayloadAction<KeyValue> {
export function setUIDataKey(key: string, value: Object): PayloadAction<KeyValue> {
return {
type: SET,
payload: { key, value },
Expand Down Expand Up @@ -113,7 +113,7 @@ export function fetchCompleteUIData(): Action {
*/
export interface KeyValue {
key: string;
value: any;
value: Object;
}

/**
Expand Down Expand Up @@ -156,9 +156,9 @@ export function loadUIData(...keys: string[]) {
response.getKeyValues().forEach((val, key) => {
// Responses from the server return values as ByteBuffer objects, which
// represent stringified JSON objects.
let decoded: any = null;
let decoded: Object;
let bb = val.getValue();
let str = bb.readString(bb.limit);
let str = bb.readString(bb.limit - bb.offset);
if (str) {
decoded = JSON.parse(str);
}
Expand Down
14 changes: 7 additions & 7 deletions ui/next/app/util/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe("rest api", function() {
sendAsJson: false,
body: new protos.cockroach.server.serverpb.DatabasesResponse({
databases: ["system", "test"],
}).encodeJSON(),
}).toArrayBuffer(),
};
});

Expand Down Expand Up @@ -175,7 +175,7 @@ describe("rest api", function() {
{ user: "root", privileges: ["ALL"] },
{ user: "other", privileges: [] },
],
}).encodeJSON(),
}).toArrayBuffer(),
};
});

Expand Down Expand Up @@ -235,7 +235,7 @@ describe("rest api", function() {
assert.isUndefined(requestObj.body);
return {
sendAsJson: false,
body: new protos.cockroach.server.serverpb.TableDetailsResponse().encodeJSON(),
body: new protos.cockroach.server.serverpb.TableDetailsResponse().toArrayBuffer(),
};
});

Expand Down Expand Up @@ -298,7 +298,7 @@ describe("rest api", function() {
events: [
{ event_type: "test" },
],
}).encodeJSON(),
}).toArrayBuffer(),
};
});

Expand Down Expand Up @@ -331,7 +331,7 @@ describe("rest api", function() {
events: [
{ event_type: "test" },
],
}).encodeJSON(),
}).toArrayBuffer(),
};
});

Expand Down Expand Up @@ -359,7 +359,7 @@ describe("rest api", function() {
assert.isUndefined(requestObj.body);
return {
sendAsJson: false,
body: new protos.cockroach.server.serverpb.EventsResponse().encodeJSON(),
body: new protos.cockroach.server.serverpb.EventsResponse().toArrayBuffer(),
};
});

Expand Down Expand Up @@ -416,7 +416,7 @@ describe("rest api", function() {
assert.isUndefined(requestObj.body);
return {
sendAsJson: false,
body: new protos.cockroach.server.serverpb.HealthResponse().encodeJSON(),
body: new protos.cockroach.server.serverpb.HealthResponse().toArrayBuffer(),
};
});

Expand Down
14 changes: 10 additions & 4 deletions ui/next/app/util/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,18 @@ function Fetch<TRequestMessage extends {
return timeout(fetch(url, {
method: req ? "POST" : "GET",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Accept": "application/x-protobuf",
"Content-Type": "application/x-protobuf",
},
body: req ? req.encodeJSON() : undefined,
})).then((res) => res.json<TResponse>()).then((json) => new builder(json));
body: req ? req.toArrayBuffer() : undefined,
})).then((res) => {
if (!res.ok) {
throw Error(res.statusText);
}
return res.arrayBuffer().then((buffer) => builder.decode(buffer));
});
}

// propsToQueryString is a helper function that converts a set of object
// properties to a query string
// - keys with null or undefined values will be skipped
Expand Down
2 changes: 1 addition & 1 deletion ui/next/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ System.config({
"ts": "github:frankwallis/[email protected]",
"ts-runtime": "npm:[email protected]",
"typescript": "npm:[email protected]",
"whatwg-fetch": "npm:whatwg-fetch@1.0.0",
"whatwg-fetch": "github:tamird/fetch@support-arraybuffer",
"github:dcodeIO/[email protected]": {
"long": "github:dcodeIO/[email protected]"
},
Expand Down
2 changes: 1 addition & 1 deletion ui/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"redux": "npm:redux@^3.4.0",
"redux-thunk": "npm:redux-thunk@^2.0.1",
"reselect": "npm:reselect@^2.5.1",
"whatwg-fetch": "npm:whatwg-fetch@^1.0.0"
"whatwg-fetch": "github:tamird/fetch@support-arraybuffer"
},
"devDependencies": {
"chai": "npm:chai@^3.5.0",
Expand Down

0 comments on commit 1aa63a3

Please sign in to comment.