This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
40 changed files
with
316 additions
and
292 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
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,24 @@ | ||
import { Binary } from "./binary.ts" | ||
|
||
export interface ProxyChain { | ||
url: string | ||
binary?: never | ||
version: string | ||
} | ||
|
||
export interface BinaryChain { | ||
url?: never | ||
binary: Binary | ||
chain: string | ||
} | ||
|
||
export interface CapiConfig { | ||
server: string | ||
chains?: Record<string, ProxyChain | BinaryChain> | ||
networks?: Record<string, NetworkConfig> | ||
} | ||
|
||
export interface NetworkConfig { | ||
relay: BinaryChain & { nodes?: number } | ||
parachains: Record<string, BinaryChain & { id: number; nodes?: number }> | ||
} |
This file was deleted.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,103 +1,11 @@ | ||
export * from "./binary.ts" | ||
|
||
import { blake2_512, blake2_64, Hasher } from "../crypto/hashers.ts" | ||
import { hex } from "../crypto/mod.ts" | ||
import { $codegenSpec, CodegenEntry, CodegenSpec } from "../server/codegenSpec.ts" | ||
import { normalizePackageName, withSignal } from "../util/mod.ts" | ||
import { Binary } from "./binary.ts" | ||
import { getBinaryMetadata, getUrlMetadata } from "./getMetadata.ts" | ||
import { startNetwork } from "./startNetwork.ts" | ||
|
||
export interface ProxyChain { | ||
url: string | ||
binary?: never | ||
version: string | ||
} | ||
|
||
export interface BinaryChain { | ||
url?: never | ||
binary: Binary | ||
chain: string | ||
} | ||
|
||
export interface CapiConfig { | ||
server: string | ||
chains?: Record<string, ProxyChain | BinaryChain> | ||
networks?: Record<string, NetworkConfig> | ||
} | ||
// moderate | ||
|
||
export interface NetworkConfig { | ||
relay: BinaryChain & { nodes?: number } | ||
parachains: Record<string, BinaryChain & { id: number; nodes?: number }> | ||
} | ||
|
||
export async function processConfig(config: CapiConfig) { | ||
return withSignal(async (signal) => { | ||
const { server } = config | ||
const entries = (await Promise.all([ | ||
...Object.entries(config.chains ?? {}).map( | ||
async ([name, chain]): Promise<[string[], CodegenEntry][]> => { | ||
const metadata = chain.url !== undefined | ||
? await getUrlMetadata(chain, signal) | ||
: await getBinaryMetadata(chain, signal) | ||
const metadataHash = await uploadMetadata(server, metadata) | ||
return [[[normalizePackageName(name)], { | ||
type: "frame", | ||
metadata: metadataHash, | ||
chainName: name.replace(/^./, (x) => x.toUpperCase()), | ||
connection: chain.url !== undefined | ||
? { type: "ws", discovery: chain.url } | ||
: { type: "capnChain", name }, | ||
}]] | ||
}, | ||
), | ||
...Object.entries(config.networks ?? {}).map(async ([networkName, network]) => { | ||
const chains = await startNetwork(network, signal) | ||
return await Promise.all( | ||
Object.entries(chains).map( | ||
async ([name, [port]]): Promise<[string[], CodegenEntry]> => { | ||
const metadata = await getUrlMetadata({ url: `ws://localhost:${port}` }, signal) | ||
const metadataHash = await uploadMetadata(server, metadata) | ||
return [[ | ||
normalizePackageName(networkName), | ||
normalizePackageName(name), | ||
], { | ||
type: "frame", | ||
metadata: metadataHash, | ||
chainName: name.replace(/^./, (x) => x.toUpperCase()), | ||
connection: { type: "capnNetworkChain", network: networkName, name }, | ||
}] | ||
}, | ||
), | ||
) | ||
}), | ||
])).flat() | ||
const codegenHash = await uploadCodegenSpec(server, { | ||
type: "v0", | ||
codegen: new Map(entries), | ||
}) | ||
console.log( | ||
entries.map(([key]) => | ||
new URL(codegenHash + "/" + key.join("/") + "/mod.js", server).toString() | ||
).join("\n"), | ||
) | ||
}) | ||
} | ||
|
||
async function _upload(server: string, kind: string, data: Uint8Array, hasher: Hasher) { | ||
const hash = hasher.hash(data) | ||
const url = new URL(`upload/${kind}/${hex.encode(hash)}`, server) | ||
const exists = await fetch(url, { method: "HEAD" }) | ||
if (exists.ok) return hash | ||
const response = await fetch(url, { method: "PUT", body: data }) | ||
if (!response.ok) throw new Error(await response.text()) | ||
return hash | ||
} | ||
|
||
export async function uploadMetadata(server: string, metadata: Uint8Array) { | ||
return await _upload(server, "metadata", metadata, blake2_512) | ||
} | ||
|
||
export async function uploadCodegenSpec(server: string, spec: CodegenSpec) { | ||
return hex.encode(await _upload(server, "codegen", $codegenSpec.encode(spec), blake2_64)) | ||
} | ||
export * from "./api.ts" | ||
export * from "./binary.ts" | ||
export * from "./CapiConfig.ts" | ||
export * from "./getMetadata.ts" | ||
export * from "./processConfig.ts" | ||
export * from "./scald.ts" | ||
export * from "./server.ts" | ||
export * from "./startNetwork.ts" | ||
export * from "./testUsers.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,77 @@ | ||
export * from "./binary.ts" | ||
|
||
import { blake2_512, blake2_64, Hasher } from "../crypto/hashers.ts" | ||
import { hex } from "../crypto/mod.ts" | ||
import { $codegenSpec, CodegenEntry, CodegenSpec } from "../server/codegenSpec.ts" | ||
import { normalizePackageName, withSignal } from "../util/mod.ts" | ||
import { CapiConfig } from "./CapiConfig.ts" | ||
import { getBinaryMetadata, getUrlMetadata } from "./getMetadata.ts" | ||
import { startNetwork } from "./startNetwork.ts" | ||
|
||
export async function processConfig(config: CapiConfig) { | ||
return withSignal(async (signal) => { | ||
const { server } = config | ||
const entries = (await Promise.all([ | ||
...Object.entries(config.chains ?? {}).map( | ||
async ([name, chain]): Promise<[string[], CodegenEntry][]> => { | ||
const metadata = chain.url !== undefined | ||
? await getUrlMetadata(chain, signal) | ||
: await getBinaryMetadata(chain, signal) | ||
const metadataHash = await uploadMetadata(server, metadata) | ||
console.log(name, metadataHash) | ||
return [[[normalizePackageName(name)], { | ||
type: "frame", | ||
metadata: metadataHash, | ||
chainName: name.replace(/^./, (x) => x.toUpperCase()), | ||
connection: chain.url !== undefined | ||
? { type: "ws", discovery: chain.url } | ||
: { type: "capnChain", name }, | ||
}]] | ||
}, | ||
), | ||
...Object.entries(config.networks ?? {}).map(async ([networkName, network]) => { | ||
const chains = await startNetwork(network, signal) | ||
return await Promise.all( | ||
Object.entries(chains).map( | ||
async ([name, [port]]): Promise<[string[], CodegenEntry]> => { | ||
const metadata = await getUrlMetadata({ url: `ws://localhost:${port}` }, signal) | ||
const metadataHash = await uploadMetadata(server, metadata) | ||
return [[ | ||
normalizePackageName(networkName), | ||
normalizePackageName(name), | ||
], { | ||
type: "frame", | ||
metadata: metadataHash, | ||
chainName: name.replace(/^./, (x) => x.toUpperCase()), | ||
connection: { type: "capnNetworkChain", network: networkName, name }, | ||
}] | ||
}, | ||
), | ||
) | ||
}), | ||
])).flat() | ||
const codegenHash = await uploadCodegenSpec(server, { | ||
type: "v0", | ||
codegen: new Map(entries), | ||
}) | ||
return new URL(codegenHash + "/", server).toString() | ||
}) | ||
} | ||
|
||
async function _upload(server: string, kind: string, data: Uint8Array, hasher: Hasher) { | ||
const hash = hasher.hash(data) | ||
const url = new URL(`upload/${kind}/${hex.encode(hash)}`, server) | ||
const exists = await fetch(url, { method: "HEAD" }) | ||
if (exists.ok) return hash | ||
const response = await fetch(url, { method: "PUT", body: data }) | ||
if (!response.ok) throw new Error(await response.text()) | ||
return hash | ||
} | ||
|
||
async function uploadMetadata(server: string, metadata: Uint8Array) { | ||
return await _upload(server, "metadata", metadata, blake2_512) | ||
} | ||
|
||
async function uploadCodegenSpec(server: string, spec: CodegenSpec) { | ||
return hex.encode(await _upload(server, "codegen", $codegenSpec.encode(spec), blake2_64)) | ||
} |
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
Oops, something went wrong.