Skip to content

Commit c1c5768

Browse files
authored
Prototype protobuf-es usage (#70)
1 parent 9c70ccf commit c1c5768

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+8346
-14538
lines changed

buf.gen.yaml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
---
22
version: "v2"
33
plugins:
4-
- remote: "buf.build/community/timostamm-protobuf-ts:v2.9.1"
4+
- remote: "buf.build/bufbuild/es:v2.4.0"
55
out: "src/spicedb-common/protodefs"
6+
include_imports: true
67
opt:
7-
- "long_type_string"
8-
- "generate_dependencies"
9-
- "optimize_code_size"
8+
- "target=ts"
109
inputs:
11-
- module: "buf.build/authzed/api:v1.40.0"
10+
- module: "buf.build/authzed/api:v1.41.0"
1211
paths:
1312
- "authzed/api/v0"
1413
- git_repo: "https://github.com/authzed/spicedb"

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
"dependencies": {
77
"@apollo/client": "^3.7.3",
88
"@apollo/link-context": "^2.0.0-beta.3",
9+
"@bufbuild/protobuf": "^2.4.0",
10+
"@connectrpc/connect": "^2.0.2",
11+
"@connectrpc/connect-web": "^2.0.2",
912
"@fontsource/roboto": "^5.1.1",
1013
"@fortawesome/fontawesome-svg-core": "^6.4.0",
1114
"@fortawesome/free-solid-svg-icons": "^6.2.1",
@@ -16,8 +19,6 @@
1619
"@material-ui/icons": "^4.11.3",
1720
"@material-ui/lab": "^4.0.0-alpha.61",
1821
"@monaco-editor/react": "^4.3.1",
19-
"@protobuf-ts/grpcweb-transport": "^2.9.4",
20-
"@protobuf-ts/plugin": "^2.6.0",
2122
"@radix-ui/react-alert-dialog": "^1.1.11",
2223
"@radix-ui/react-select": "^2.2.2",
2324
"@radix-ui/react-slot": "^1.2.0",

src/components/CheckDebugTraceView.tsx

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ import {
44
CheckDebugTrace,
55
CheckDebugTrace_Permissionship,
66
CheckDebugTrace_PermissionType,
7-
} from "../spicedb-common/protodefs/authzed/api/v1/debug";
8-
import {
9-
Struct,
10-
Value,
11-
} from "../spicedb-common/protodefs/google/protobuf/struct";
7+
} from "../spicedb-common/protodefs/authzed/api/v1/debug_pb";
8+
import type { JsonObject, JsonValue } from "@bufbuild/protobuf";
129
import { createStyles, makeStyles, Theme } from "@material-ui/core/styles";
1310
import CheckCircleIcon from "@material-ui/icons/CheckCircle";
1411
import ChevronRightIcon from "@material-ui/icons/ChevronRight";
@@ -105,8 +102,8 @@ export function CheckDebugTraceView(props: {
105102
);
106103
});
107104

108-
if (t.resolution.oneofKind === "subProblems") {
109-
t.resolution.subProblems.traces.forEach(appendExpanded);
105+
if (t.resolution.case === "subProblems") {
106+
t.resolution.value.traces.forEach(appendExpanded);
110107
}
111108
};
112109

@@ -145,18 +142,16 @@ function CheckDebugTraceItems(props: {
145142
const isNotMember = hasNotPermission(props.trace);
146143

147144
const children =
148-
props.trace.resolution.oneofKind === "subProblems"
149-
? props.trace.resolution.subProblems.traces.map(
150-
(subTrace, index) => {
151-
return (
152-
<CheckDebugTraceItems
153-
key={index}
154-
trace={subTrace}
155-
localParseService={props.localParseService}
156-
/>
157-
);
158-
},
159-
)
145+
props.trace.resolution.case === "subProblems"
146+
? props.trace.resolution.value.traces.map((subTrace, index) => {
147+
return (
148+
<CheckDebugTraceItems
149+
key={index}
150+
trace={subTrace}
151+
localParseService={props.localParseService}
152+
/>
153+
);
154+
})
160155
: [];
161156

162157
if (
@@ -285,14 +280,18 @@ function CaveatTreeItem(props: {
285280
);
286281
}
287282

288-
function ContextTreeView(context: Struct | undefined) {
283+
function ContextTreeView(context: JsonObject | undefined) {
289284
if (context === undefined) {
290285
return null;
291286
}
292287

293-
return Object.keys(context.fields).map((key) => {
288+
if (context === null) {
289+
return null;
290+
}
291+
292+
return Object.keys(context).map((key) => {
294293
let label = <span>{key}</span>;
295-
const [value, isItemValue] = ContextTreeValue(context?.fields[key]);
294+
const [value, isItemValue] = ContextTreeValue(context[key]);
296295
if (!isItemValue) {
297296
label = (
298297
<span>
@@ -309,31 +308,25 @@ function ContextTreeView(context: Struct | undefined) {
309308
});
310309
}
311310

312-
function ContextTreeValue(value: Value) {
313-
switch (value.kind.oneofKind) {
314-
case "nullValue":
315-
return [<code>null</code>, false];
316-
317-
case "numberValue":
318-
return [<code>{value.kind.numberValue.toString()}</code>, false];
319-
320-
case "stringValue":
321-
return [<code>{value.kind.stringValue}</code>, false];
322-
323-
case "boolValue":
324-
return [<code>{value.kind.boolValue.toString()}</code>, false];
325-
326-
case "structValue":
327-
return [ContextTreeView(value.kind.structValue), true];
328-
329-
case "listValue":
330-
return [
331-
value.kind.listValue.values.map((v) => {
332-
return <TreeItem nodeId="">{ContextTreeValue(v)}</TreeItem>;
333-
}),
334-
true,
335-
];
311+
function ContextTreeValue(value: JsonValue) {
312+
if (value === null) {
313+
return [<code>null</code>, false];
336314
}
337-
338-
return [null, false];
315+
if (typeof value === "boolean") {
316+
return [<code>{value.toString()}</code>, false];
317+
}
318+
if (Array.isArray(value)) {
319+
return [
320+
value.map((v) => {
321+
return <TreeItem nodeId="">{ContextTreeValue(v)}</TreeItem>;
322+
}),
323+
true,
324+
];
325+
}
326+
// NOTE: we've already handled null and array above, so this will match on objects.
327+
if (typeof value === "object") {
328+
return [ContextTreeView(value), true];
329+
}
330+
// If we've gotten this far, we have a number or a string and we can render it straight out.
331+
return [<code>{value}</code>, false];
339332
}

src/components/DatastoreRelationshipEditor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
import {
1717
DeveloperError,
1818
DeveloperError_Source,
19-
} from "../spicedb-common/protodefs/developer/v1/developer";
19+
} from "../spicedb-common/protodefs/developer/v1/developer_pb";
2020
import { Theme } from "@glideapps/glide-data-grid";
2121
import { useCallback, useMemo, useState } from "react";
2222
import useDeepCompareEffect from "use-deep-compare-effect";

src/components/EditorDisplay.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { RelationshipFound } from "../spicedb-common/parsing";
99
import {
1010
DeveloperError,
1111
DeveloperWarning,
12-
} from "../spicedb-common/protodefs/developer/v1/developer";
12+
} from "../spicedb-common/protodefs/developer/v1/developer_pb";
1313
import { createStyles, makeStyles } from "@material-ui/core/styles";
1414
import useMediaQuery from "@material-ui/core/useMediaQuery";
1515
import Editor, { DiffEditor, useMonaco } from "@monaco-editor/react";

src/components/EmbeddedPlayground.tsx

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import {
44
RelationshipFound,
55
parseRelationship,
66
} from "../spicedb-common/parsing";
7-
import { DeveloperServiceClient } from "../spicedb-common/protodefs/authzed/api/v0/developer.client";
8-
import { GrpcWebFetchTransport } from "@protobuf-ts/grpcweb-transport";
9-
import { RpcError } from "@protobuf-ts/runtime-rpc";
107
import {
8+
CheckOperationParametersSchema,
119
CheckOperationsResult,
1210
CheckOperationsResult_Membership,
13-
} from "../spicedb-common/protodefs/developer/v1/developer";
11+
CheckOperationsResultSchema,
12+
} from "../spicedb-common/protodefs/developer/v1/developer_pb";
13+
import { DeveloperService } from "../spicedb-common/protodefs/authzed/api/v0/developer_pb";
14+
import { createGrpcWebTransport } from "@connectrpc/connect-web";
15+
import { createClient, ConnectError } from "@connectrpc/connect";
1416
import { useDeveloperService } from "../spicedb-common/services/developerservice";
1517
import {
1618
faCaretDown,
@@ -44,6 +46,7 @@ import { ShareLoader } from "./ShareLoader";
4446

4547
import { ParsedObjectDefinition } from "../spicedb-common/parsers/dsl/dsl";
4648
import "./fonts.css";
49+
import { create } from "@bufbuild/protobuf";
4750

4851
const useStyles = makeStyles(() =>
4952
createStyles({
@@ -281,9 +284,13 @@ function EmbeddedPlaygroundUI(props: { datastore: DataStore }) {
281284
return;
282285
}
283286

284-
const service = new DeveloperServiceClient(
285-
new GrpcWebFetchTransport({ baseUrl: developerEndpoint }),
287+
const client = createClient(
288+
DeveloperService,
289+
createGrpcWebTransport({
290+
baseUrl: developerEndpoint,
291+
}),
286292
);
293+
287294
const schema = datastore.getSingletonByKind(
288295
DataStoreItemKind.SCHEMA,
289296
).editableContents!;
@@ -299,7 +306,7 @@ function EmbeddedPlaygroundUI(props: { datastore: DataStore }) {
299306

300307
// Invoke sharing.
301308
try {
302-
const { response } = await service.share({
309+
const response = await client.share({
303310
schema,
304311
relationshipsYaml,
305312
assertionsYaml,
@@ -308,7 +315,7 @@ function EmbeddedPlaygroundUI(props: { datastore: DataStore }) {
308315
const reference = response.shareReference;
309316
window.open(`${window.location.origin}/s/${reference}`);
310317
} catch (error: unknown) {
311-
if (error instanceof RpcError) {
318+
if (error instanceof ConnectError) {
312319
showAlert({
313320
title: "Error sharing",
314321
content: error.message,
@@ -482,14 +489,17 @@ function EmbeddedQuery(props: { services: Services }) {
482489
}
483490

484491
const request = devService.newRequest(schemaText, relsText);
485-
let checkResult: CheckOperationsResult = {
486-
membership: CheckOperationsResult_Membership.UNKNOWN,
487-
};
488-
request?.check(
492+
let checkResult: CheckOperationsResult = create(
493+
CheckOperationsResultSchema,
489494
{
495+
membership: CheckOperationsResult_Membership.UNKNOWN,
496+
},
497+
);
498+
request?.check(
499+
create(CheckOperationParametersSchema, {
490500
resource: relationship.resourceAndRelation!,
491501
subject: relationship.subject!,
492-
},
502+
}),
493503
(r: CheckOperationsResult) => {
494504
checkResult = r;
495505
},

src/components/FullPlayground.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { DiscordChatCrate } from "../playground-ui/DiscordChatCrate";
33
import { useGoogleAnalytics } from "../playground-ui/GoogleAnalyticsHook";
44
import TabLabel from "../playground-ui/TabLabel";
55
import { Example } from "../spicedb-common/examples";
6-
import { DeveloperServiceClient } from "../spicedb-common/protodefs/authzed/api/v0/developer.client";
7-
import { GrpcWebFetchTransport } from "@protobuf-ts/grpcweb-transport";
8-
import { RpcError } from "@protobuf-ts/runtime-rpc";
6+
import { DeveloperService } from "../spicedb-common/protodefs/authzed/api/v0/developer_pb";
7+
import { createGrpcWebTransport } from "@connectrpc/connect-web";
8+
import { createClient, ConnectError } from "@connectrpc/connect";
99
import { useDeveloperService } from "../spicedb-common/services/developerservice";
1010
import { useZedTerminalService } from "../spicedb-common/services/zedterminalservice";
1111
import { parseValidationYAML } from "../spicedb-common/validationfileformat";
@@ -498,8 +498,11 @@ export function ThemedAppView(props: { datastore: DataStore }) {
498498
status: SharingStatus.SHARING,
499499
});
500500

501-
const service = new DeveloperServiceClient(
502-
new GrpcWebFetchTransport({ baseUrl: developerEndpoint }),
501+
const client = createClient(
502+
DeveloperService,
503+
createGrpcWebTransport({
504+
baseUrl: developerEndpoint,
505+
}),
503506
);
504507

505508
const schema = datastore.getSingletonByKind(
@@ -517,7 +520,7 @@ export function ThemedAppView(props: { datastore: DataStore }) {
517520

518521
// Invoke sharing.
519522
try {
520-
const { response } = await service.share({
523+
const response = await client.share({
521524
schema,
522525
relationshipsYaml,
523526
assertionsYaml,
@@ -533,7 +536,7 @@ export function ThemedAppView(props: { datastore: DataStore }) {
533536
shareReference: reference,
534537
});
535538
} catch (error: unknown) {
536-
if (error instanceof RpcError) {
539+
if (error instanceof ConnectError) {
537540
showAlert({
538541
title: "Error sharing",
539542
content: error.message,

src/components/ShareLoader.tsx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { useAlert } from "../playground-ui/AlertProvider";
22
import { useConfirmDialog } from "../playground-ui/ConfirmDialogProvider";
33
import LoadingView from "../playground-ui/LoadingView";
4-
import { DeveloperServiceClient } from "../spicedb-common/protodefs/authzed/api/v0/developer.client";
5-
import { GrpcWebFetchTransport } from "@protobuf-ts/grpcweb-transport";
6-
import { LookupShareResponse_LookupStatus } from "../spicedb-common/protodefs/authzed/api/v0/developer";
7-
import { RpcError } from "@protobuf-ts/runtime-rpc";
4+
import { LookupShareResponse_LookupStatus } from "../spicedb-common/protodefs/authzed/api/v0/developer_pb";
5+
import { DeveloperService } from "../spicedb-common/protodefs/authzed/api/v0/developer_pb";
6+
import { createGrpcWebTransport } from "@connectrpc/connect-web";
7+
import { createClient, ConnectError } from "@connectrpc/connect";
88
import Alert from "@material-ui/lab/Alert";
99
import React, { useEffect, useState } from "react";
1010
import "react-reflex/styles.css";
@@ -66,8 +66,11 @@ export function ShareLoader(props: {
6666
if (!endpoint) {
6767
return;
6868
}
69-
const service = new DeveloperServiceClient(
70-
new GrpcWebFetchTransport({ baseUrl: endpoint }),
69+
const client = createClient(
70+
DeveloperService,
71+
createGrpcWebTransport({
72+
baseUrl: endpoint,
73+
}),
7174
);
7275

7376
// TODO: use routing for this instead of string manipulation
@@ -80,12 +83,11 @@ export function ShareLoader(props: {
8083
const shareReference = pieces[0];
8184

8285
try {
83-
const { response, status } = await service.lookupShared({
86+
const response = await client.lookupShared({
8487
shareReference,
8588
});
8689
if (
87-
status.code ===
88-
LookupShareResponse_LookupStatus.FAILED_TO_LOOKUP.toString()
90+
response.status === LookupShareResponse_LookupStatus.FAILED_TO_LOOKUP
8991
) {
9092
setLoadingStatus(SharedLoadingStatus.LOAD_ERROR);
9193
if (props.sharedRequired) {
@@ -103,8 +105,7 @@ export function ShareLoader(props: {
103105

104106
// Unknown reference.
105107
if (
106-
status.code ===
107-
LookupShareResponse_LookupStatus.UNKNOWN_REFERENCE.toString()
108+
response.status === LookupShareResponse_LookupStatus.UNKNOWN_REFERENCE
108109
) {
109110
setLoadingStatus(SharedLoadingStatus.LOAD_ERROR);
110111
if (props.sharedRequired) {
@@ -162,10 +163,10 @@ export function ShareLoader(props: {
162163

163164
setLoadingStatus(SharedLoadingStatus.LOADED);
164165
} catch (error: unknown) {
165-
if (error instanceof RpcError)
166+
if (error instanceof ConnectError)
166167
await showAlert({
167168
title: "Error loading shared playground",
168-
content: error?.message,
169+
content: error.message,
169170
buttonTitle: "Okay",
170171
});
171172
navigate({ to: "/", replace: true });

src/components/panels/errordisplays.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
DeveloperError,
33
DeveloperError_Source,
44
DeveloperWarning,
5-
} from "../../spicedb-common/protodefs/developer/v1/developer";
5+
} from "../../spicedb-common/protodefs/developer/v1/developer_pb";
66
import { Theme, createStyles, makeStyles } from "@material-ui/core/styles";
77
import Alert from "@material-ui/lab/Alert";
88
import "react-reflex/styles.css";

src/components/panels/problems.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { RelationshipFound } from "../../spicedb-common/parsing";
33
import {
44
DeveloperError,
55
DeveloperWarning,
6-
} from "../../spicedb-common/protodefs/developer/v1/developer";
6+
} from "../../spicedb-common/protodefs/developer/v1/developer_pb";
77
import Paper from "@material-ui/core/Paper";
88
import { createStyles, makeStyles, Theme } from "@material-ui/core/styles";
99
import ErrorOutlineIcon from "@material-ui/icons/ErrorOutline";

0 commit comments

Comments
 (0)