Skip to content

Commit

Permalink
Working type-checked lints
Browse files Browse the repository at this point in the history
  • Loading branch information
robknight committed Sep 9, 2024
1 parent 1bfdb42 commit 5f38108
Show file tree
Hide file tree
Showing 33 changed files with 881 additions and 132 deletions.
2 changes: 1 addition & 1 deletion apps/client-web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function App() {
});

useEffect(() => {
(async () => {
void (async () => {
const { zapp, advice } = await listen();
dispatch({ type: "set-zapp", zapp });
dispatch({ type: "set-advice", advice });
Expand Down
4 changes: 2 additions & 2 deletions apps/client-web/src/client/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export function loadPODsFromStorage(): POD[] {
return pods;
}
try {
const serializedPODs = JSON.parse(storedSerializedPODs);
pods = serializedPODs.map(POD.deserialize);
const serializedPODs = JSON.parse(storedSerializedPODs) as string[];
pods = serializedPODs.map((str) => POD.deserialize(str));
} catch (e) {
// JSON parsing failed or POD deserialization failed
console.error(e);
Expand Down
2 changes: 1 addition & 1 deletion examples/test-app/src/apis/GPC.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const verified = await z.gpc.verify(proof);
)}
{proof && (
<TryIt
onClick={async () => {
onClick={() => {
try {
// setVerified(await z.gpc.verify(proof));
} catch (e) {
Expand Down
3 changes: 2 additions & 1 deletion examples/test-app/src/components/TryIt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ export function TryIt({
onClick,
label
}: {
onClick: () => void;
onClick: () => void | Promise<void>;
label: string;
}): ReactNode {
return (
<div className="flex items-center gap-2">
<span className="text-sm">Try it:</span>
{/* eslint-disable-next-line @typescript-eslint/no-misused-promises */}
<button className="btn btn-primary" onClick={onClick}>
{label}
</button>
Expand Down
4 changes: 2 additions & 2 deletions examples/test-app/src/hooks/useParcnetClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function ParcnetIframeProvider({

useEffect(() => {
if (ref.current) {
connect(zapp, ref.current, url).then((zupass) => {
void connect(zapp, ref.current, url).then((zupass) => {
setValue({
state: ClientConnectionState.CONNECTED,
z: zupass,
Expand Down Expand Up @@ -125,7 +125,7 @@ function ParcnetWebsocketProvider({
});

useEffect(() => {
connectWebsocket(zapp, url).then((api) => {
void connectWebsocket(zapp, url).then((api) => {
setValue({
state: ClientConnectionState.CONNECTED,
z: api
Expand Down
4 changes: 3 additions & 1 deletion examples/test-app/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export function getConnectionInfo(): ClientConnectionInfo {
const storedConnectionInfo = localStorage.getItem("clientConnectionInfo");
if (storedConnectionInfo) {
try {
const parsedConnectionInfo = JSON.parse(storedConnectionInfo);
const parsedConnectionInfo = JSON.parse(
storedConnectionInfo
) as ClientConnectionInfo;
if (
["iframe", "websocket"].includes(connectionInfo.type) &&
typeof connectionInfo.url === "string"
Expand Down
7 changes: 0 additions & 7 deletions examples/test-app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
{
"ts-node": {
// these options are overrides used only by ts-node
// same as the --compilerOptions flag and the TS_NODE_COMPILER_OPTIONS environment variable
"compilerOptions": {
"module": "CommonJS"
}
},
"compilerOptions": {
"resolveJsonModule": true,
"downlevelIteration": true,
Expand Down
34 changes: 20 additions & 14 deletions packages/app-connector/src/adapters/iframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function connect(
iframe.src = normalizedUrl.toString();

return new Promise<ParcnetAPI>((resolve) => {
iframe.addEventListener("load", async () => {
iframe.addEventListener("load", () => {
// Create a new MessageChannel to communicate with the iframe
const chan = new MessageChannel();

Expand All @@ -121,24 +121,30 @@ export function connect(
});

if (iframe.contentWindow) {
const contentWindow = iframe.contentWindow;
// @todo Blink (and maybe Webkit) will discard messages if there's no
// handler yet, so we need to wait a bit and/or retry until the client is
// ready
// The client takes a few seconds to load, so waiting isn't a bad solution
await new Promise<void>((resolve) => {
new Promise<void>((resolve) => {
window.setTimeout(() => resolve(), 1000);
});
// Send the other port of the message channel to the client
postWindowMessage(
iframe.contentWindow,
{
type: InitializationMessageType.PARCNET_CLIENT_CONNECT,
zapp: zapp
},
"*",
// Our RPC client has port2, send port1 to the client
[chan.port1]
);
})
.then(() => {
// Send the other port of the message channel to the client
postWindowMessage(
contentWindow,
{
type: InitializationMessageType.PARCNET_CLIENT_CONNECT,
zapp: zapp
},
"*",
// Our RPC client has port2, send port1 to the client
[chan.port1]
);
})
.catch((err) => {
console.error("Error sending initialization message", err);
});
} else {
console.error("no iframe content window!");
}
Expand Down
10 changes: 6 additions & 4 deletions packages/app-connector/src/adapters/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import { DialogController } from "./iframe.js";

class DialogControllerImpl implements DialogController {
public show(): void {
toast.info("Your PARCNET client requests interaction");
toast.info("Your PARCNET client requests interaction").catch(() => {
// Do nothing
});
}

public close(): void {
Expand All @@ -23,7 +25,7 @@ export function connectWebsocket(zapp: Zapp, url: string): Promise<ParcnetAPI> {
const ws = new WebSocket(url);
const chan = new MessageChannel();

ws.addEventListener("open", async () => {
ws.addEventListener("open", () => async () => {
const connectMsg = JSONBig.stringify({
type: InitializationMessageType.PARCNET_CLIENT_CONNECT,
zapp
Expand All @@ -33,10 +35,10 @@ export function connectWebsocket(zapp: Zapp, url: string): Promise<ParcnetAPI> {
});

ws.addEventListener("message", (ev) => {
chan.port1.postMessage(JSONBig.parse(ev.data));
chan.port1.postMessage(JSONBig.parse(ev.data as string));
});

chan.port1.addEventListener("message", async (message) => {
chan.port1.addEventListener("message", (message) => {
ws.send(JSONBig.stringify(message.data));
});

Expand Down
5 changes: 3 additions & 2 deletions packages/app-connector/src/rpc_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class ParcnetRPCConnector implements ParcnetRPC, ParcnetEvents {
* @param args - The arguments to pass to the method.
* @returns A promise that resolves to the method's return value.
*/
async #typedInvoke<
#typedInvoke<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
A extends ZodTuple<any, any>,
R extends ZodTypeAny,
Expand All @@ -100,10 +100,11 @@ export class ParcnetRPCConnector implements ParcnetRPC, ParcnetEvents {
// by safeParse.
const parsedResult = functionSchema.returnType().safeParse(result);
if (parsedResult.success) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return parsedResult.data;
} else {
throw new Error(
`Failed to parse result for ${fn}: ${parsedResult.error}`
`Failed to parse result for ${fn}: ${parsedResult.error.message}`
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/app-connector/test/parcnet-connector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ function generateRandomHex(byteLength: number): string {
// eslint-disable-next-line react-hooks/rules-of-hooks
use(chaiAsPromised);

describe("parcnet-client should work", async function () {
it("parcnet-client should throw when not connected", async function () {
describe("parcnet-client should work", function () {
it("parcnet-client should throw when not connected", function () {
const chan = new MessageChannel();

const client = new ParcnetRPCConnector(chan.port2, mockDialog);
Expand Down
4 changes: 2 additions & 2 deletions packages/app-connector/test/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RPCMessageType } from "@parcnet/client-rpc";
import { expect } from "chai";
import { DialogController, postRPCMessage } from "../src";
import { ParcnetRPCConnector } from "../src/rpc_client";
import { DialogController, postRPCMessage } from "../src/index.js";
import { ParcnetRPCConnector } from "../src/rpc_client.js";

export const mockDialog: DialogController = {
show: () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/app-connector/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"compilerOptions": {
"outDir": "dist"
},
"include": ["src"],
"include": ["src", "test"],
"exclude": ["node_modules", "dist"]
}
8 changes: 0 additions & 8 deletions packages/app-connector/tsconfig.lint.json

This file was deleted.

5 changes: 3 additions & 2 deletions packages/client-helpers/src/connection/iframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ async function handleMessage(
try {
if (functionToInvoke && typeof functionToInvoke === "function") {
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const result = await functionToInvoke.apply(object, message.args);
port.postMessage({
type: RPCMessageType.PARCNET_CLIENT_INVOKE_RESULT,
Expand Down Expand Up @@ -109,7 +110,7 @@ export async function listen(): Promise<ListenResult> {
let portMessageHandler: (message: MessageEvent) => void;

return new Promise((resolve) => {
const windowEventHandler = async (event: MessageEvent) => {
const windowEventHandler = (event: MessageEvent) => {
const data = InitializationMessageSchema.safeParse(event.data);
if (!data.success) {
return;
Expand All @@ -130,7 +131,7 @@ export async function listen(): Promise<ListenResult> {
onReady: (rpc) => {
portMessageHandler = (ev) => {
const message = RPCMessageSchema.parse(ev.data);
handleMessage(rpc, port, message);
void handleMessage(rpc, port, message);
};

port.addEventListener("message", portMessageHandler);
Expand Down
5 changes: 4 additions & 1 deletion packages/client-helpers/src/connection/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export async function listen(ws: WebSocket): Promise<ListenResult> {
return new Promise((resolve) => {
const initialEventHandler = (initialEvent: MessageEvent) => {
const data = InitializationMessageSchema.safeParse(
// eslint-disable-next-line @typescript-eslint/no-base-to-string
JSONBig.parse(initialEvent.data.toString())
);
if (!data.success) {
Expand All @@ -95,13 +96,14 @@ export async function listen(ws: WebSocket): Promise<ListenResult> {
onReady: (rpc) => {
rpcMessageHandler = (ev: MessageEvent) => {
const message = RPCMessageSchema.safeParse(
// eslint-disable-next-line @typescript-eslint/no-base-to-string
JSONBig.parse(ev.data.toString())
);
if (message.success === false) {
return;
}

handleMessage(rpc, ws, message.data);
void handleMessage(rpc, ws, message.data);
};

ws.addEventListener("message", rpcMessageHandler);
Expand Down Expand Up @@ -132,6 +134,7 @@ async function handleMessage(
try {
if (functionToInvoke && typeof functionToInvoke === "function") {
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const result = await functionToInvoke.apply(object, message.args);
socket.send(
JSONBig.stringify({
Expand Down
8 changes: 0 additions & 8 deletions packages/client-helpers/tsconfig.lint.json

This file was deleted.

3 changes: 2 additions & 1 deletion packages/client-rpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
"LICENSE"
],
"dependencies": {
"@pcd/gpc": "0.0.6",
"@parcnet/podspec": "workspace:*",
"@pcd/gpc": "0.0.6",
"zod": "^3.22.4"
},
"devDependencies": {
"@parcnet/eslint-config": "workspace:*",
"@parcnet/typescript-config": "workspace:*",
"@types/snarkjs": "^0.7.8",
"tsup": "^8.2.4",
"typescript": "^5.5"
},
Expand Down
9 changes: 6 additions & 3 deletions packages/client-rpc/src/rpc_interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { PODSchema, PodspecProofRequest } from "@parcnet/podspec";
import {
EntriesSchema,
PODSchema,
PodspecProofRequest
} from "@parcnet/podspec";
import { GPCBoundConfig, GPCProof, GPCRevealedClaims } from "@pcd/gpc";

/**
Expand All @@ -7,8 +11,7 @@ import { GPCBoundConfig, GPCProof, GPCRevealedClaims } from "@pcd/gpc";
* These interfaces are implemented in rpc_client.ts.
*/

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type PODQuery = PODSchema<any>;
export type PODQuery = PODSchema<EntriesSchema>;

export interface SubscriptionUpdateResult {
subscriptionId: string;
Expand Down
Loading

0 comments on commit 5f38108

Please sign in to comment.