-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Downloading anon circuits using webworkers (#121)
closes #123
- Loading branch information
Showing
5 changed files
with
189 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
packages/react-providers/src/worker/circuitWorkerScript.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* eslint-disable no-restricted-globals */ | ||
/* eslint-disable import/no-anonymous-default-export */ | ||
|
||
import { IBaseWorkerResponse } from './useWebWorker' | ||
|
||
export interface ICircuit { | ||
zKeyData: Uint8Array | ||
zKeyHash: string | ||
zKeyURI: string | ||
vKeyData: Uint8Array | ||
vKeyHash: string | ||
vKeyURI: string | ||
wasmData: Uint8Array | ||
wasmHash: string | ||
wasmURI: string | ||
} | ||
|
||
export interface ICircuitWorkerRequest { | ||
circuits: { | ||
zKeyURI: string | ||
zKeyHash: string | ||
vKeyURI: string | ||
vKeyHash: string | ||
wasmURI: string | ||
wasmHash: string | ||
} | ||
} | ||
|
||
export interface IWorkerResponse extends IBaseWorkerResponse<ICircuit> {} | ||
|
||
export const fetchDataInChunks = async (url: string) => { | ||
return fetch(url) | ||
.then((res) => res.arrayBuffer()) | ||
.then((res) => new Uint8Array(res)) | ||
} | ||
|
||
export default () => { | ||
self.addEventListener('message', async (e: MessageEvent<ICircuitWorkerRequest>) => { | ||
try { | ||
const { circuits } = e.data | ||
|
||
async function fetchDataInChunks(uri: string) { | ||
const response = await fetch(uri) | ||
const reader = response.body?.getReader() as ReadableStreamDefaultReader | ||
const contentLength = +(response.headers?.get('Content-Length') ?? 0) | ||
|
||
const chunks = [] | ||
let receivedLength = 0 | ||
|
||
while (true) { | ||
const { done, value } = await reader.read() | ||
|
||
if (done) break | ||
|
||
chunks.push(value) | ||
receivedLength += value.length | ||
|
||
// Check for content length, break if all content received | ||
if (contentLength && receivedLength >= contentLength) break | ||
} | ||
|
||
const concatenatedArray = new Uint8Array(receivedLength) | ||
let offset = 0 | ||
|
||
for (const chunk of chunks) { | ||
concatenatedArray.set(chunk, offset) | ||
offset += chunk.length | ||
} | ||
|
||
return concatenatedArray | ||
} | ||
|
||
const [zKeyData, vKeyData, wasmData] = await Promise.all([ | ||
fetchDataInChunks(circuits.zKeyURI), | ||
fetchDataInChunks(circuits.vKeyURI), | ||
fetchDataInChunks(circuits.wasmURI), | ||
]) | ||
|
||
const circuitsData: ICircuit = { | ||
zKeyData, | ||
zKeyURI: circuits.zKeyURI, | ||
zKeyHash: circuits.zKeyHash, | ||
vKeyData, | ||
vKeyURI: circuits.vKeyURI, | ||
vKeyHash: circuits.vKeyHash, | ||
wasmData, | ||
wasmURI: circuits.wasmURI, | ||
wasmHash: circuits.wasmHash, | ||
} | ||
return postMessage({ result: circuitsData } as IWorkerResponse) | ||
} catch (error) { | ||
return postMessage({ error } as IWorkerResponse) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { useCallback, useEffect, useState } from 'react' | ||
|
||
export interface IBaseWorkerResponse<T> { | ||
result: T | ||
error?: any | ||
} | ||
|
||
export const useWebWorker = <TResult, TWorkerPayload>(worker: Worker) => { | ||
const [running, setRunning] = useState(false) | ||
const [error, setError] = useState<any>() | ||
const [result, setResult] = useState<TResult>() | ||
|
||
const startProcessing = useCallback( | ||
(data: TWorkerPayload) => { | ||
setRunning(true) | ||
worker.postMessage(data) | ||
}, | ||
[worker] | ||
) | ||
|
||
useEffect(() => { | ||
const onMessage = (event: MessageEvent<IBaseWorkerResponse<TResult>>) => { | ||
setRunning(false) | ||
setError(event.data.error) | ||
setResult(event.data.result) | ||
} | ||
worker.addEventListener('message', onMessage) | ||
return () => worker.removeEventListener('message', onMessage) | ||
}, [worker]) | ||
|
||
return { | ||
startProcessing, | ||
running, | ||
error, | ||
result, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const createWebWorker = (worker: any) => { | ||
const code = worker.toString() | ||
const blob = new Blob(['(' + code + ')()']) | ||
const workerURL = (window as any).MockedWindowURL || window.URL || window.webkitURL | ||
const blobURL = workerURL.createObjectURL(blob) | ||
return new Worker(blobURL) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters