From 6049a3ff047e1306e52d48e357806954a60288fa Mon Sep 17 00:00:00 2001 From: Harry Solovay Date: Tue, 14 Feb 2023 13:13:37 -0500 Subject: [PATCH] fix: multiple runs with proxy providers (#581) --- deps/std/bytes/mod.ts | 1 - deps/std/http.ts | 1 + deps/std/http/file_server.ts | 1 - deps/std/http/http_status.ts | 1 - deps/std/http/server.ts | 1 - fluent/rpc_runes.ts | 8 ++-- main.ts | 2 +- patterns/ink/InkRune.ts | 2 +- providers/frame/FrameProxyProvider.ts | 58 +++++++++++++-------------- rpc/provider/errors.ts | 2 +- rpc/provider/proxy.ts | 4 +- server/factories.ts | 2 +- server/local.ts | 6 ++- 13 files changed, 44 insertions(+), 45 deletions(-) delete mode 100644 deps/std/bytes/mod.ts create mode 100644 deps/std/http.ts delete mode 100644 deps/std/http/file_server.ts delete mode 100644 deps/std/http/http_status.ts delete mode 100644 deps/std/http/server.ts diff --git a/deps/std/bytes/mod.ts b/deps/std/bytes/mod.ts deleted file mode 100644 index 9570c222e..000000000 --- a/deps/std/bytes/mod.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "https://deno.land/std@0.173.0/bytes/mod.ts" diff --git a/deps/std/http.ts b/deps/std/http.ts new file mode 100644 index 000000000..694e3c954 --- /dev/null +++ b/deps/std/http.ts @@ -0,0 +1 @@ +export * from "https://deno.land/std@0.173.0/http/mod.ts" diff --git a/deps/std/http/file_server.ts b/deps/std/http/file_server.ts deleted file mode 100644 index 38ccfc695..000000000 --- a/deps/std/http/file_server.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "https://deno.land/std@0.173.0/http/file_server.ts" diff --git a/deps/std/http/http_status.ts b/deps/std/http/http_status.ts deleted file mode 100644 index 93111d8a2..000000000 --- a/deps/std/http/http_status.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "https://deno.land/std@0.173.0/http/http_status.ts" diff --git a/deps/std/http/server.ts b/deps/std/http/server.ts deleted file mode 100644 index b37509bb7..000000000 --- a/deps/std/http/server.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "https://deno.land/std@0.173.0/http/server.ts" diff --git a/fluent/rpc_runes.ts b/fluent/rpc_runes.ts index b285b0619..c09c03d5f 100644 --- a/fluent/rpc_runes.ts +++ b/fluent/rpc_runes.ts @@ -12,12 +12,12 @@ class RunRpcClient extends Run, } client?: rpc.Client - _evaluate(): Promise> { - return Promise.resolve(this.client ??= new rpc.Client(this.provider, this.discoveryValue)) + async _evaluate(): Promise> { + return this.client ??= new rpc.Client(this.provider, this.discoveryValue) } - override cleanup(): void { - this.client?.discard() + override async cleanup(): Promise { + await this.client?.discard() super.cleanup() } } diff --git a/main.ts b/main.ts index 2067baa27..0b4ec8eef 100644 --- a/main.ts +++ b/main.ts @@ -1,5 +1,5 @@ import * as flags from "./deps/std/flags.ts" -import { serve } from "./deps/std/http/server.ts" +import { serve } from "./deps/std/http.ts" import { PolkadotDevProvider, WssProvider, ZombienetProvider } from "./providers/frame/mod.ts" import { handler } from "./server/local.ts" import { Env } from "./server/mod.ts" diff --git a/patterns/ink/InkRune.ts b/patterns/ink/InkRune.ts index 65bd37ab8..7aa9c4231 100644 --- a/patterns/ink/InkRune.ts +++ b/patterns/ink/InkRune.ts @@ -1,4 +1,4 @@ -import { equals } from "../../deps/std/bytes/mod.ts" +import { equals } from "../../deps/std/bytes.ts" import { Chain, ClientRune, CodecRune, ExtrinsicRune, state } from "../../fluent/mod.ts" import { Event } from "../../primitives/mod.ts" import { Rune, RunicArgs, ValueRune } from "../../rune/mod.ts" diff --git a/providers/frame/FrameProxyProvider.ts b/providers/frame/FrameProxyProvider.ts index 6d3d12b02..4fea151a7 100644 --- a/providers/frame/FrameProxyProvider.ts +++ b/providers/frame/FrameProxyProvider.ts @@ -1,11 +1,12 @@ import { File } from "../../codegen/frame/mod.ts" +import { deferred } from "../../deps/std/async.ts" import { Client, proxyProvider } from "../../rpc/mod.ts" import { PathInfo } from "../../server/mod.ts" import { fromPathInfo } from "../../server/PathInfo.ts" import { FrameProvider } from "./FrameProvider.ts" export abstract class FrameProxyProvider extends FrameProvider { - override handle(request: Request, pathInfo: PathInfo): Promise { + override async handle(request: Request, pathInfo: PathInfo): Promise { if ( pathInfo.vRuntime && pathInfo.vRuntime !== "latest" && !pathInfo.filePath && request.headers.get("upgrade") === "websocket" @@ -19,33 +20,30 @@ export abstract class FrameProxyProvider extends FrameProvider { const url = await this.dynamicUrl(pathInfo) const server = new WebSocket(url) const { socket: client, response } = Deno.upgradeWebSocket(request) - server.addEventListener("close", () => { - try { - client.close() - } catch {} - }) - server.addEventListener("message", (event) => { - try { - client.send(event.data) - } catch { - client.close() - server.close() - } - }) - client.addEventListener("close", async () => { - try { - server.close() - } catch {} - }) - client.addEventListener("message", async (event) => { - try { - server.send(event.data) - } catch { - client.close() - server.close() - } - }) + setup(client, server) + setup(server, client) return response + + function setup(a: WebSocket, b: WebSocket) { + const ready = deferred() + b.addEventListener("open", () => { + ready.resolve() + }) + a.addEventListener("close", async () => { + try { + b.close() + } catch {} + }) + a.addEventListener("message", async (event) => { + try { + await ready + b.send(event.data) + } catch { + a.close() + b.close() + } + }) + } } abstract dynamicUrl(pathInfo: PathInfo): Promise @@ -73,11 +71,11 @@ export abstract class FrameProxyProvider extends FrameProvider { import * as C from "./capi.ts" import type { Chain } from "./mod.ts" - export const client = C.rpcClient(C.rpc.proxyProvider, "${url}")["_asCodegen"]() + export const discoveryValue = "${url}" - export const rawClient = new C.rpc.Client(C.rpc.proxyProvider, "${url}") + export const client = C.rpcClient(C.rpc.proxyProvider, discoveryValue)["_asCodegen"]() - export const discoveryValue = "${url}" + export const rawClient = new C.rpc.Client(C.rpc.proxyProvider, discoveryValue) `) } } diff --git a/rpc/provider/errors.ts b/rpc/provider/errors.ts index ef0f6e6cf..24cf4bb90 100644 --- a/rpc/provider/errors.ts +++ b/rpc/provider/errors.ts @@ -11,7 +11,7 @@ export class ProviderSendError extends Error { } export class ProviderHandlerError extends Error { - override readonly name = "ProviderInternalError" + override readonly name = "ProviderHandlerError" constructor(override readonly cause: Data) { super() } diff --git a/rpc/provider/proxy.ts b/rpc/provider/proxy.ts index 0eef10379..fa9a1f586 100644 --- a/rpc/provider/proxy.ts +++ b/rpc/provider/proxy.ts @@ -66,7 +66,9 @@ function connection( ws.addEventListener("close", (e) => { conn.forEachListener(new ProviderHandlerError(e)) }, controller) - return new ProviderConnection(ws, () => controller.abort()) + return new ProviderConnection(ws, () => { + controller.abort() + }) }) conn.addListener(listener) return conn diff --git a/server/factories.ts b/server/factories.ts index 11a028584..fbe9b617b 100644 --- a/server/factories.ts +++ b/server/factories.ts @@ -1,6 +1,6 @@ import { escapeHtml } from "../deps/escape.ts" import * as shiki from "../deps/shiki.ts" -import { Status } from "../deps/std/http/http_status.ts" +import { Status } from "../deps/std/http.ts" import { CacheBase } from "../util/cache/base.ts" shiki.setCDN("https://unpkg.com/shiki/") diff --git a/server/local.ts b/server/local.ts index c8637f2d1..a211f88b1 100644 --- a/server/local.ts +++ b/server/local.ts @@ -18,10 +18,12 @@ export function handler(env: Env) { } const provider = env.providers[generatorId]?.[providerId] if (provider) { - return await provider.handle(request, pathInfo).catch((e) => { + try { + return await provider.handle(request, pathInfo) + } catch (e) { if (e instanceof Response) return e return f.serverError(Deno.inspect(e)) - }) + } } } for (const dir of staticDirs) {