Skip to content

Commit

Permalink
feat: finish stream
Browse files Browse the repository at this point in the history
  • Loading branch information
0xtsukino committed Nov 29, 2024
1 parent ccdbef1 commit efa831c
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 12 deletions.
47 changes: 35 additions & 12 deletions demo/web-to-web-p2p/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import React, {
ReactElement,
ReactNode,
useCallback,
useEffect,
useState,
} from 'react';
import React, { ReactElement, useCallback, useEffect, useState } from 'react';
import { createRoot } from 'react-dom/client';
import * as Comlink from 'comlink';
import {
Prover as TProver,
Verifier as TVerifier,
Presentation as TPresentation,
Commit,
NotaryServer,
Transcript,
} from 'tlsn-js';
import { PresentationJSON } from 'tlsn-js/build/types';
import './app.scss';
import WebscoketStream from './stream';

const { init, Prover, Presentation, Verifier }: any = Comlink.wrap(
const { init, Prover, Verifier }: any = Comlink.wrap(
new Worker(new URL('./worker.ts', import.meta.url)),
);

Expand All @@ -43,6 +35,37 @@ function App(): ReactElement {
})();
}, []);

useEffect(() => {
(async () => {
// Set up stream for prover
(async () => {
const proverStream = new WebscoketStream(
'ws://localhost:3001?id=prover',
);
const reader = await proverStream.reader();
while (true) {
const { done, value } = await reader.read();
if (done) {
console.log('stream finished');
break;
}
console.log(`Received data from stream:`, await value.text());
}
})();

// Set up stream for verifier
(async () => {
const verifierStream = new WebscoketStream(
'ws://localhost:3001?id=verifier',
);
const writer = await verifierStream.writer();
writer.write('Hello');
writer.write('World!');
writer.close();
})();
})();
}, []);

const addProverLog = useCallback((log: string) => {
pLogs = pLogs.concat(`${new Date().toLocaleTimeString()} - ${log}`);
setProverMessages(pLogs);
Expand Down Expand Up @@ -102,7 +125,7 @@ function App(): ReactElement {
});
addProverLog('request sent');
const transcript = await prover.transcript();
console.log(transcript);

addProverLog('response received');
addProverLog('transcript.sent');
addProverLog(transcript.sent);
Expand Down
62 changes: 62 additions & 0 deletions demo/web-to-web-p2p/src/stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
export default class WebscoketStream {
client: WebSocket;
readable: Promise<ReadableStream>;
writable: Promise<WritableStream>;
constructor(url: string) {
const client = new WebSocket(url);

const deferredReadable = defer<ReadableStream>();
const deferredWritable = defer<WritableStream>();
this.client = client;
this.readable = deferredReadable.promise;
this.writable = deferredWritable.promise;

client.onopen = () => {
const readable = new ReadableStream({
start(controller) {
client.onmessage = async (event) => {
controller.enqueue(event.data);
};
},
cancel() {
client.close();
},
});

const writable = new WritableStream({
write(chunk) {
client.send(chunk);
},
close() {
client.close();
},
});

deferredReadable.resolve(readable);
deferredWritable.resolve(writable);
};
}

async reader() {
return this.readable.then((stream) => stream.getReader());
}

async writer() {
return this.writable.then((stream) => stream.getWriter());
}
}

function defer<value = any>(): {
promise: Promise<value>;
resolve: (val: value | Promise<value>) => void;
reject: (err: any) => void;
} {
let resolve: (val: value | Promise<value>) => void,
reject: (err: any) => void;
const promise: Promise<value> = new Promise((res, rej) => {
resolve = res;
reject = rej;
});

return { promise, resolve: resolve!, reject: reject! };
}

0 comments on commit efa831c

Please sign in to comment.