Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

fix: multiple runs with proxy providers #581

Merged
merged 5 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion deps/std/bytes/mod.ts

This file was deleted.

1 change: 1 addition & 0 deletions deps/std/http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "https://deno.land/[email protected]/http/mod.ts"
1 change: 0 additions & 1 deletion deps/std/http/file_server.ts

This file was deleted.

1 change: 0 additions & 1 deletion deps/std/http/http_status.ts

This file was deleted.

1 change: 0 additions & 1 deletion deps/std/http/server.ts

This file was deleted.

8 changes: 4 additions & 4 deletions fluent/rpc_runes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ class RunRpcClient<DV, SED, HED, CED> extends Run<rpc.Client<DV, SED, HED, CED>,
}

client?: rpc.Client<DV, SED, HED, CED>
_evaluate(): Promise<rpc.Client<DV, SED, HED, CED>> {
return Promise.resolve(this.client ??= new rpc.Client(this.provider, this.discoveryValue))
async _evaluate(): Promise<rpc.Client<DV, SED, HED, CED>> {
return this.client ??= new rpc.Client(this.provider, this.discoveryValue)
}

override cleanup(): void {
this.client?.discard()
override async cleanup(): Promise<void> {
await this.client?.discard()
super.cleanup()
}
}
Expand Down
2 changes: 1 addition & 1 deletion main.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion patterns/ink/InkRune.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
58 changes: 28 additions & 30 deletions providers/frame/FrameProxyProvider.ts
Original file line number Diff line number Diff line change
@@ -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<Response> {
override async handle(request: Request, pathInfo: PathInfo): Promise<Response> {
if (
pathInfo.vRuntime && pathInfo.vRuntime !== "latest" && !pathInfo.filePath
&& request.headers.get("upgrade") === "websocket"
Expand All @@ -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<string>
Expand Down Expand Up @@ -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"]<Chain>()
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"]<Chain>()

export const discoveryValue = "${url}"
export const rawClient = new C.rpc.Client(C.rpc.proxyProvider, discoveryValue)
`)
}
}
2 changes: 1 addition & 1 deletion rpc/provider/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class ProviderSendError<Data> extends Error {
}

export class ProviderHandlerError<Data> extends Error {
override readonly name = "ProviderInternalError"
override readonly name = "ProviderHandlerError"
constructor(override readonly cause: Data) {
super()
}
Expand Down
4 changes: 3 additions & 1 deletion rpc/provider/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server/factories.ts
Original file line number Diff line number Diff line change
@@ -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/")
Expand Down
6 changes: 4 additions & 2 deletions server/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down