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

chore: compress ts formatter wasm #1073

Merged
merged 2 commits into from
Jun 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
5 changes: 3 additions & 2 deletions _tasks/generate_artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import { emptyDir } from "../deps/std/fs.ts"
import * as path from "../deps/std/path.ts"
import dprintConfig from "../dprint.json" assert { type: "json" }
import { devUser } from "../nets/chain_spec/addDevUsers.ts"
import { compress } from "../util/compression.ts"

export const DEV_USER_COUNT = 10_000

const artifacts: Record<string, () => Promise<Uint8Array>> = {
async tsFormatterWasm() {
async tsFormatterWasmCompressed() {
const url = dprintConfig.plugins.find((v) =>
v.startsWith("https://plugins.dprint.dev/typescript-")
)!
return await fetchBinary(url)
return await compress(await fetchBinary(url))
},
async devUserPublicKeysData() {
const publicKeys = []
Expand Down
6 changes: 3 additions & 3 deletions server/codegenHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { $metadata } from "../frame_metadata/raw/v14.ts"
import { CacheBase } from "../util/cache/base.ts"
import { WeakMemo } from "../util/memo.ts"
import { normalizePackageName, normalizeVariableName } from "../util/mod.ts"
import { tsFormatter } from "../util/tsFormatter.ts"
import { tsFormatterPromise } from "../util/tsFormatter.ts"
import { $codegenSpec, CodegenEntry } from "./CodegenSpec.ts"
import * as f from "./factories.ts"
import { getStatic } from "./getStatic.ts"
Expand Down Expand Up @@ -121,7 +121,7 @@ export function createCodegenHandler(dataCache: CacheBase, tempCache: CacheBase)
const subpath = path.slice(`/${key}/`.length)

if (!files.has(subpath)) throw f.notFound()
return tsFormatter.formatText(path, files.get(subpath)!)
return (await tsFormatterPromise).formatText(path, files.get(subpath)!)
}),
)
}
Expand Down Expand Up @@ -167,7 +167,7 @@ export function createCodegenHandler(dataCache: CacheBase, tempCache: CacheBase)
path = `${prefix}/${path}`
}
if (/\.(js|ts)$/.test(path)) {
content = tsFormatter.formatText(path, content)
content = (await tsFormatterPromise).formatText(path, content)
}
rootFiles.set(path, content)
}
Expand Down
3 changes: 0 additions & 3 deletions util/_artifacts/tsFormatterWasm.ts

This file was deleted.

3 changes: 3 additions & 0 deletions util/_artifacts/tsFormatterWasmCompressed.ts

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions util/compression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Buffer } from "../deps/std/streams.ts"

export async function compress(data: Uint8Array) {
const compressed = new Buffer()
await new Buffer(data).readable
.pipeThrough(new CompressionStream("gzip"))
.pipeTo(compressed.writable)
return compressed.bytes()
}

export async function decompress(data: Uint8Array) {
const decompressed = new Buffer()
await new Buffer(data).readable
.pipeThrough(new DecompressionStream("gzip"))
.pipeTo(decompressed.writable)
return decompressed.bytes()
}
13 changes: 9 additions & 4 deletions util/tsFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { createFromBuffer } from "../deps/dprint.ts"
import dprintConfig from "../dprint.json" assert { type: "json" }
import { tsFormatterWasm } from "./_artifacts/tsFormatterWasm.ts"
import { tsFormatterWasmCompressed } from "./_artifacts/tsFormatterWasmCompressed.ts"
import { decompress } from "./compression.ts"

export const tsFormatter = createFromBuffer(tsFormatterWasm)
const { indentWidth, lineWidth, typescript: config } = dprintConfig
tsFormatter.setConfig({ indentWidth, lineWidth }, config)
export const tsFormatterPromise = (async () => {
const tsFormatterWasm = await decompress(tsFormatterWasmCompressed)
const tsFormatter = createFromBuffer(tsFormatterWasm)
const { indentWidth, lineWidth, typescript: config } = dprintConfig
tsFormatter.setConfig({ indentWidth, lineWidth }, config)
return tsFormatter
})()