From 000c3853bed532203169619c302ec6d50b02605a Mon Sep 17 00:00:00 2001 From: T6 Date: Wed, 19 Apr 2023 10:42:01 -0400 Subject: [PATCH] fix: running in browser (#913) --- _tasks/dnt.ts | 4 +++ deps/shims/upgradeWebSocket.node.ts | 5 +-- deps/shims/ws.ts | 1 + patterns/compat/pjs_sender.ts | 2 +- patterns/signature/polkadot.ts | 2 +- rune/ArrayRune.ts | 2 ++ rune/FnRune.ts | 2 ++ rune/Rune.ts | 48 +++++++++++++++++------------ rune/ValueRune.ts | 2 ++ rune/_empty.js | 2 ++ rune/mod.ts | 5 +-- 11 files changed, 49 insertions(+), 26 deletions(-) create mode 100644 rune/_empty.js diff --git a/_tasks/dnt.ts b/_tasks/dnt.ts index b0e206c67..700fa67f0 100755 --- a/_tasks/dnt.ts +++ b/_tasks/dnt.ts @@ -56,6 +56,10 @@ await Promise.all([ name: "./patterns/signature/polkadot", path: "./patterns/signature/polkadot.ts", }, + { + name: "./patterns/compat/pjs_sender", + path: "./patterns/compat/pjs_sender.ts", + }, { name: "./patterns/consensus", path: "./patterns/consensus/mod.ts", diff --git a/deps/shims/upgradeWebSocket.node.ts b/deps/shims/upgradeWebSocket.node.ts index 1fbeb714a..f08fe8b32 100644 --- a/deps/shims/upgradeWebSocket.node.ts +++ b/deps/shims/upgradeWebSocket.node.ts @@ -1,8 +1,9 @@ -import { WebSocketServer } from "./ws.ts" +import * as WS from "./ws.ts" -const wss = new WebSocketServer({ noServer: true }) +let wss: WS.WebSocketServer | undefined export function upgradeWebSocket(request: Request) { + wss ??= new WS.WebSocketServer({ noServer: true }) // Set by http.node.ts const [req, socket, head] = (request as any)._upgrade return { diff --git a/deps/shims/ws.ts b/deps/shims/ws.ts index 32713c0bc..87549f230 100644 --- a/deps/shims/ws.ts +++ b/deps/shims/ws.ts @@ -1,3 +1,4 @@ // This will be shimmed to the `ws` npm package by DNT export declare const WebSocketServer: any +export declare type WebSocketServer = any diff --git a/patterns/compat/pjs_sender.ts b/patterns/compat/pjs_sender.ts index 653c7aae9..13042b315 100644 --- a/patterns/compat/pjs_sender.ts +++ b/patterns/compat/pjs_sender.ts @@ -2,7 +2,7 @@ import { hex, ss58 } from "../../crypto/mod.ts" import * as $ from "../../deps/scale.ts" import { AddressPrefixChain, Chain, ChainRune } from "../../fluent/ChainRune.ts" import { ExtrinsicSender } from "../../fluent/ExtrinsicRune.ts" -import { Rune, RunicArgs } from "../../rune/Rune.ts" +import { Rune, RunicArgs } from "../../rune/mod.ts" export type PjsSigner = { signPayload(payload: any): Promise<{ signature: string }> } diff --git a/patterns/signature/polkadot.ts b/patterns/signature/polkadot.ts index 25c4fac79..9e516a0b1 100644 --- a/patterns/signature/polkadot.ts +++ b/patterns/signature/polkadot.ts @@ -2,7 +2,7 @@ import { Polkadot } from "@capi/polkadot" import { AddressPrefixChain, Chain, ChainRune } from "../../fluent/ChainRune.ts" import { ExtrinsicSender, SignatureData } from "../../fluent/ExtrinsicRune.ts" import { $, hex, ss58, ValueRune } from "../../mod.ts" -import { Rune, RunicArgs } from "../../rune/Rune.ts" +import { Rune, RunicArgs } from "../../rune/mod.ts" import { Era } from "../../scale_info/overrides/Era.ts" export interface SignatureProps { diff --git a/rune/ArrayRune.ts b/rune/ArrayRune.ts index 592ef17c6..f057ef15a 100644 --- a/rune/ArrayRune.ts +++ b/rune/ArrayRune.ts @@ -14,3 +14,5 @@ export class ArrayRune extends Rune { .into(ArrayRune) } } + +Rune.ArrayRune = ArrayRune diff --git a/rune/FnRune.ts b/rune/FnRune.ts index 4a74b3ba7..a470f63ee 100644 --- a/rune/FnRune.ts +++ b/rune/FnRune.ts @@ -8,3 +8,5 @@ export class FnRune any, U> extends Rune { return Rune.tuple([this.as(Rune), ...args]).map(([fn, ...args]) => fn(...args)) } } + +Rune.FnRune = FnRune diff --git a/rune/Rune.ts b/rune/Rune.ts index bbac95c35..57235a36b 100644 --- a/rune/Rune.ts +++ b/rune/Rune.ts @@ -1,8 +1,11 @@ import { deferred } from "../deps/std/async.ts" import { getOrInit } from "../util/state.ts" -import { ArrayRune, FnRune, ValueRune } from "./mod.ts" import { EventSource, Receipt, Timeline } from "./Timeline.ts" +// Hack to work around circularity issues +// @deno-types="./mod.ts" +import * as _ from "./_empty.js" + export class Batch { constructor( readonly timeline = new Timeline(), @@ -41,9 +44,12 @@ export class Batch { declare const _T: unique symbol declare const _U: unique symbol -export namespace Rune { +export declare namespace Rune { export type T = R extends { [_T]: infer T } ? T : R export type U = R extends { [_U]: infer U } ? U : never + export import ValueRune = _.ValueRune + export import ArrayRune = _.ArrayRune + export import FnRune = _.FnRune } export interface Rune { @@ -89,11 +95,11 @@ export class Rune { } static constant(value: T) { - return ValueRune.new(RunConstant, value) + return Rune.ValueRune.new(RunConstant, value) } - static resolve(value: V): ValueRune, Rune.U> { - return value instanceof Rune ? value.into(ValueRune) : Rune.constant(value) + static resolve(value: V): Rune.ValueRune, Rune.U> { + return value instanceof Rune ? value.into(Rune.ValueRune) : Rune.constant(value) } static str(strings: TemplateStringsArray, ..._values: RunicArgs) { @@ -111,24 +117,24 @@ export class Rune { static tuple( runes: [...R], - ): ValueRune<{ [K in keyof R]: Rune.T }, Rune.U> + ): Rune.ValueRune<{ [K in keyof R]: Rune.T }, Rune.U> static tuple( runes: [...R], - ): ValueRune[], Rune.U> { - return ValueRune.new(RunLs, runes.map(Rune.resolve)) + ): Rune.ValueRune[], Rune.U> { + return Rune.ValueRune.new(RunLs, runes.map(Rune.resolve)) } static array(runes: RunicArgs) { - return ValueRune.new(RunLs, RunicArgs.resolve(runes)).into(ArrayRune) + return Rune.ValueRune.new(RunLs, RunicArgs.resolve(runes)).into(Rune.ArrayRune) } static fn(...[fn]: RunicArgs T]>) { - return Rune.resolve(fn).into(FnRune) + return Rune.resolve(fn).into(Rune.FnRune) } static object( runes: R, - ): ValueRune<{ [K in keyof R]: Rune.T }, Rune.U> { + ): Rune.ValueRune<{ [K in keyof R]: Rune.T }, Rune.U> { const keys = Object.keys(runes) const values = Object.values(runes) return Rune.tuple(values).map((values) => { @@ -139,25 +145,27 @@ export class Rune { static captureUnhandled( sources: [...R], fn: ( - ...runes: { [K in keyof R]: ValueRune, never> } + ...runes: { [K in keyof R]: Rune.ValueRune, never> } ) => Rune, - ): ValueRune | U2> + ): Rune.ValueRune | U2> static captureUnhandled( sources: any[], - fn: (...runes: ValueRune, never>[]) => Rune, - ): ValueRune | U2> { + fn: (...runes: Rune.ValueRune, never>[]) => Rune, + ): Rune.ValueRune | U2> { const symbol = Symbol() - return ValueRune.new( + return Rune.ValueRune.new( RunCaptureUnhandled>, fn( - ...sources.map((source) => ValueRune.new(RunBubbleUnhandled, Rune.resolve(source), symbol)), + ...sources.map((source) => + Rune.ValueRune.new(RunBubbleUnhandled, Rune.resolve(source), symbol) + ), ), symbol, ) } - static asyncIter(fn: (signal: AbortSignal) => AsyncIterable): ValueRune { - return ValueRune.new(RunAsyncIter, fn) + static asyncIter(fn: (signal: AbortSignal) => AsyncIterable): Rune.ValueRune { + return Rune.ValueRune.new(RunAsyncIter, fn) } static _placeholder() { @@ -390,7 +398,7 @@ export namespace RunicArgs { export type U = X extends unknown[] ? Rune.U : Rune.U export function resolve( args: RunicArgs, - ): { [K in keyof A]: ValueRune> } + ): { [K in keyof A]: Rune.ValueRune> } export function resolve(args: any): any { return args instanceof Array ? args.map(Rune.resolve) diff --git a/rune/ValueRune.ts b/rune/ValueRune.ts index 70d16b526..bbf907d89 100644 --- a/rune/ValueRune.ts +++ b/rune/ValueRune.ts @@ -163,6 +163,8 @@ class RunMap extends Run { } } +Rune.ValueRune = ValueRune + class RunHandle extends Run | T3, U | U2> { child alt diff --git a/rune/_empty.js b/rune/_empty.js new file mode 100644 index 000000000..7e172157f --- /dev/null +++ b/rune/_empty.js @@ -0,0 +1,2 @@ + +// does nothing at runtime diff --git a/rune/mod.ts b/rune/mod.ts index d23dfdf09..edbd2d8ba 100644 --- a/rune/mod.ts +++ b/rune/mod.ts @@ -1,9 +1,10 @@ -// moderate +export * from "./Rune.ts" + +// moderate --exclude Rune.ts export * from "./ArrayRune.ts" export * from "./FnRune.ts" export * from "./Id.ts" export * from "./MetaRune.ts" -export * from "./Rune.ts" export * from "./Timeline.ts" export * from "./ValueRune.ts"