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

Commit

Permalink
chore: misc server cleanup (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
harrysolovay authored Dec 24, 2022
1 parent e066c9c commit 6a9dbb5
Show file tree
Hide file tree
Showing 23 changed files with 116 additions and 102 deletions.
2 changes: 1 addition & 1 deletion _tasks/dnt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { build } from "https://deno.land/x/dnt@0.26.0/mod.ts"
import { build } from "../deps/dnt.ts"
import * as fs from "../deps/std/fs.ts"
import * as path from "../deps/std/path.ts"

Expand Down
6 changes: 3 additions & 3 deletions _tasks/run_browser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { contentType } from "https://deno.land/x/[email protected]/mod.ts"
import { Application, send } from "https://deno.land/x/[email protected]/mod.ts"
import { babel, babelPresetTypeScript } from "https://escad.dev/deps/babel.ts"
import { babel, babelPresetTypeScript } from "../deps/babel.ts"
import { contentType } from "../deps/media_types.ts"
import { Application, send } from "../deps/oak.ts"
import * as path from "../deps/std/path.ts"

const dirname = path.dirname(path.fromFileUrl(import.meta.url))
Expand Down
1 change: 1 addition & 0 deletions deps/babel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "https://escad.dev/deps/babel.ts"
1 change: 1 addition & 0 deletions deps/dnt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "https://deno.land/x/[email protected]/mod.ts"
1 change: 1 addition & 0 deletions deps/escape.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "https://deno.land/x/[email protected]/mod.ts"
1 change: 1 addition & 0 deletions deps/media_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "https://deno.land/x/[email protected]/mod.ts"
1 change: 1 addition & 0 deletions deps/oak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "https://deno.land/x/[email protected]/mod.ts"
1 change: 1 addition & 0 deletions deps/s3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "https://deno.land/x/[email protected]/mod.ts"
1 change: 1 addition & 0 deletions deps/shiki.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "https://esm.sh/[email protected]?bundle"
1 change: 1 addition & 0 deletions deps/std/http/file_server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "https://deno.land/[email protected]/http/file_server.ts"
1 change: 1 addition & 0 deletions deps/std/http/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "https://deno.land/[email protected]/http/server.ts"
52 changes: 3 additions & 49 deletions server/cache.ts → server/cache/base.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import * as $ from "../deps/scale.ts"
import * as fs from "../deps/std/fs.ts"
import * as path from "../deps/std/path.ts"
import { getOrInit, PermanentMemo, TimedMemo, WeakMemo } from "../util/mod.ts"
import * as $ from "../../deps/scale.ts"
import { getOrInit, TimedMemo, WeakMemo } from "../../util/mod.ts"

export abstract class Cache {
export abstract class CacheBase {
constructor(readonly signal: AbortSignal) {
this.stringMemo = new TimedMemo<string, string>(-1, this.signal)
}
Expand Down Expand Up @@ -43,47 +41,3 @@ export abstract class Cache {
return this.listMemo.run(prefix, () => this._list(prefix))
}
}

export class FsCache extends Cache {
constructor(readonly location: string, signal: AbortSignal) {
super(signal)
}

async _getRaw(key: string, init: () => Promise<Uint8Array>) {
const file = path.join(this.location, key)
try {
return await Deno.readFile(file)
} catch (e) {
if (!(e instanceof Deno.errors.NotFound)) throw e
const content = await init()
await fs.ensureDir(path.dirname(file))
await Deno.writeFile(file, content)
return content
}
}

async _list(prefix: string): Promise<string[]> {
try {
const result = []
for await (const entry of Deno.readDir(path.join(this.location, prefix))) {
result.push(entry.name)
}
return result
} catch (e) {
if (e instanceof Deno.errors.NotFound) {
return []
}
throw e
}
}
}

export class InMemoryCache extends Cache {
memo = new PermanentMemo<string, Uint8Array>()
_getRaw(key: string, init: () => Promise<Uint8Array>): Promise<Uint8Array> {
return Promise.resolve(this.memo.run(key, init))
}
_list(): Promise<string[]> {
throw new Error("unimplemented")
}
}
37 changes: 37 additions & 0 deletions server/cache/fs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as fs from "../../deps/std/fs.ts"
import * as path from "../../deps/std/path.ts"
import { CacheBase } from "./base.ts"

export class FsCache extends CacheBase {
constructor(readonly location: string, signal: AbortSignal) {
super(signal)
}

async _getRaw(key: string, init: () => Promise<Uint8Array>) {
const file = path.join(this.location, key)
try {
return await Deno.readFile(file)
} catch (e) {
if (!(e instanceof Deno.errors.NotFound)) throw e
const content = await init()
await fs.ensureDir(path.dirname(file))
await Deno.writeFile(file, content)
return content
}
}

async _list(prefix: string): Promise<string[]> {
try {
const result = []
for await (const entry of Deno.readDir(path.join(this.location, prefix))) {
result.push(entry.name)
}
return result
} catch (e) {
if (e instanceof Deno.errors.NotFound) {
return []
}
throw e
}
}
}
12 changes: 12 additions & 0 deletions server/cache/memory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { PermanentMemo } from "../../util/mod.ts"
import { CacheBase } from "./base.ts"

export class InMemoryCache extends CacheBase {
memo = new PermanentMemo<string, Uint8Array>()
_getRaw(key: string, init: () => Promise<Uint8Array>): Promise<Uint8Array> {
return Promise.resolve(this.memo.run(key, init))
}
_list(): Promise<string[]> {
throw new Error("unimplemented")
}
}
4 changes: 4 additions & 0 deletions server/cache/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./base.ts"
export * from "./fs.ts"
export * from "./memory.ts"
export * from "./s3.ts"
7 changes: 4 additions & 3 deletions server/s3.ts → server/cache/s3.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { S3Bucket, S3BucketConfig } from "https://deno.land/x/[email protected]/mod.ts"
import { Cache } from "./cache.ts"
import { S3Bucket, S3BucketConfig } from "../../deps/s3.ts"
import { CacheBase } from "./base.ts"

export class S3Cache extends Cache {
export class S3Cache extends CacheBase {
bucket

constructor(config: S3BucketConfig, signal: AbortSignal) {
super(signal)
this.bucket = new S3Bucket(config)
Expand Down
4 changes: 2 additions & 2 deletions server/capi_repo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PermanentMemo, TimedMemo } from "../util/memo.ts"
import { getFullSha, getSha, SHA_ABBREV_LENGTH } from "./git_utils.ts"
import { CodegenServer } from "./server.ts"
import { Server } from "./server.ts"

const TAGS_TTL = 60_000 // 1 minute
const BRANCHES_TTL = 60_000 // 1 minute
Expand All @@ -11,7 +11,7 @@ export const R_TAG_VERSION = /^v?(\d+\.\d+\.\d+[^\/]*)$/
export const R_REF_VERSION = /^ref:([^\/]*)$/
export const R_SHA_VERSION = /^sha:([0-9a-f]+)$/

export abstract class CapiCodegenServer extends CodegenServer {
export abstract class CapiCodegenServer extends Server {
async normalizeVersion(version: string) {
if (version === this.mainVersion) return version
const tagMatch = R_TAG_VERSION.exec(version)
Expand Down
2 changes: 1 addition & 1 deletion server/codecs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Codec } from "../deps/scale.ts"
import { assertEquals } from "../deps/std/testing/asserts.ts"
import { DeriveCodec } from "../scale_info/mod.ts"
import * as testClients from "../test_util/clients/mod.ts"
import { InMemoryCache } from "./cache.ts"
import { InMemoryCache } from "./cache/mod.ts"
import { LocalCapiCodegenServer } from "./local.ts"
import { highlighterPromise } from "./server.ts"

Expand Down
29 changes: 29 additions & 0 deletions server/completions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "https://deno.land/x/[email protected]/cli/schemas/registry-completions.v2.json",
"version": 2,
"registries": [
{
"schema": "/:version(@[^/]*)?/:file*",
"variables": [
{ "key": "version", "url": "/autocomplete/version" },
{ "key": "file", "url": "/${version}/autocomplete/moduleFile/${file}" }
]
},
{
"schema": "/:version(@[^/]*)/:_proxy(proxy)/:chainUrl(dev:\\w*|wss?:[^/]*)/:chainVersion(@[^/]+)/:file*",
"variables": [
{ "key": "version", "url": "/autocomplete/version" },
{ "key": "_proxy", "url": "/autocomplete/null" },
{ "key": "chainUrl", "url": "/${version}/autocomplete/chainUrl/${chainUrl}" },
{
"key": "chainVersion",
"url": "/${version}/autocomplete/chainVersion/${chainUrl}/${chainVersion}"
},
{
"key": "file",
"url": "/${version}/autocomplete/chainFile/${chainUrl}/${chainVersion}/${file}"
}
]
}
]
}
2 changes: 1 addition & 1 deletion server/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PermanentMemo } from "../util/memo.ts"
import { S3Cache } from "./cache/mod.ts"
import { CapiCodegenServer, GITHUB_API_REPO, json } from "./capi_repo.ts"
import { S3Cache } from "./s3.ts"

const DENO_DEPLOY_USER_ID = 75045203

Expand Down
4 changes: 2 additions & 2 deletions server/local.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PermanentMemo } from "../util/memo.ts"
import { Cache, FsCache } from "./cache.ts"
import { CacheBase, FsCache } from "./cache/mod.ts"
import { CapiCodegenServer } from "./capi_repo.ts"
import { getModuleIndex } from "./git_utils.ts"

Expand All @@ -8,7 +8,7 @@ const R_GITHUB_URL = /^https:\/\/raw\.githubusercontent\.com\/paritytech\/capi\/

export class LocalCapiCodegenServer extends CapiCodegenServer {
mainVersion
cache: Cache = new FsCache("target/codegen", this.abortController.signal)
cache: CacheBase = new FsCache("target/codegen", this.abortController.signal)
local = true

constructor(version?: string) {
Expand Down
2 changes: 1 addition & 1 deletion server/mod.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from "./cache.ts"
export * from "./cache/mod.ts"
export * from "./local.ts"
export * from "./server.ts"
46 changes: 7 additions & 39 deletions server/server.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { serve } from "https://deno.land/[email protected]/http/server.ts"
import { escapeHtml } from "https://deno.land/x/[email protected]/mod.ts"
import * as shiki from "https://esm.sh/[email protected]?bundle"
import { Files } from "../codegen/Files.ts"
import { codegen } from "../codegen/mod.ts"
import { escapeHtml } from "../deps/escape.ts"
import * as $ from "../deps/scale.ts"
import * as shiki from "../deps/shiki.ts"
import { serve } from "../deps/std/http/server.ts"
import * as C from "../mod.ts"
import * as T from "../test_util/mod.ts"
import * as U from "../util/mod.ts"
import { TimedMemo } from "../util/mod.ts"
import { Cache } from "./cache.ts"
import { CacheBase } from "./cache/mod.ts"

shiki.setCDN("https://unpkg.com/shiki/")
export const highlighterPromise = shiki.getHighlighter({ theme: "github-dark", langs: ["ts"] })
Expand Down Expand Up @@ -40,8 +40,8 @@ const R_WITH_CHAIN_URL = /^\/proxy\/(dev:\w+|wss?:[^\/]+)\/(?:@([^\/]+)\/)?(.*)$

const $index = $.array($.str)

export abstract class CodegenServer {
abstract cache: Cache
export abstract class Server {
abstract cache: CacheBase
abstract local: boolean
abstract mainVersion: string
abstract canHandleVersion(version: string): Promise<boolean>
Expand Down Expand Up @@ -71,7 +71,7 @@ export abstract class CodegenServer {
async root(request: Request): Promise<Response> {
const fullPath = new URL(request.url).pathname
if (fullPath === "/.well-known/deno-import-intellisense.json") {
return this.autocompleteSchema()
return await fetch(new URL("completions.json", import.meta.url))
}
const versionMatch = R_WITH_CAPI_VERSION.exec(fullPath)
if (!versionMatch) {
Expand Down Expand Up @@ -170,38 +170,6 @@ export const client = C.rpcClient(C.rpc.proxyProvider, ${JSON.stringify(chainUrl
)
}

autocompleteSchema() {
return this.json({
version: 2,
registries: [
{
schema: "/:version(@[^/]*)?/:file*",
variables: [
{ key: "version", url: "/autocomplete/version" },
{ key: "file", url: "/${version}/autocomplete/moduleFile/${file}" },
],
},
{
schema:
"/:version(@[^/]*)/:_proxy(proxy)/:chainUrl(dev:\\w*|wss?:[^/]*)/:chainVersion(@[^/]+)/:file*",
variables: [
{ key: "version", url: "/autocomplete/version" },
{ key: "_proxy", url: "/autocomplete/null" },
{ key: "chainUrl", url: "/${version}/autocomplete/chainUrl/${chainUrl}" },
{
key: "chainVersion",
url: "/${version}/autocomplete/chainVersion/${chainUrl}/${chainVersion}",
},
{
key: "file",
url: "/${version}/autocomplete/chainFile/${chainUrl}/${chainVersion}/${file}",
},
],
},
],
})
}

async autocompleteApi(path: string, version: string) {
const parts = path.slice(1).split("/")
if (parts[0] !== "autocomplete") return this.e404()
Expand Down

0 comments on commit 6a9dbb5

Please sign in to comment.