git.delta.rocks / jrsonnet / refs/commits / 6ac56eac7eb3

difftreelog

source

bindings/jrsonnet-web/mod.ts1.5 KiBsourcehistory
1import { assert } from "@std/assert";2import {3  format as formatRaw,4  type ImportResolver,5  WasmFormatOptions,6  WasmState,7  WasmVal,8} from "./lib/jsonnet_web.js";910export { type ImportResolver, WasmFormatOptions, WasmState, WasmVal };1112class FetchImportResolver implements ImportResolver {13  constructor(public base: string) {}1415  resolution = new Map<string, URL>();16  response = new Map<string, Response>();1718  async resolveFrom(from: string | undefined, path: string): Promise<string> {19    let resolved: URL;20    if (from) {21      resolved = new URL(path, from);22    } else {23      resolved = new URL(path, this.base);24    }25    const resolvingStr = resolved.toString();26    resolved = this.resolution.get(resolvingStr) ?? resolved;2728    const resolvedStr = resolved.toString();29    if (!this.response.has(resolvedStr)) {30      console.log(resolved);31      const v = await fetch(resolved);32      this.response.set(resolvedStr, v);33      resolved = new URL(v.url);34      this.resolution.set(resolvingStr, resolved);35    }36    return resolved.toString();37  }38  loadFileContents(resolved: string): Promise<Uint8Array> {39    console.log(resolved);40    const v = this.response.get(resolved);41    assert(v, "should be resolved");42    return v.bytes();43  }44}4546//47// try {48//   console.log("eval file");49//   await state.evaluate_file("example.jsonnet");50//   console.log("eval file done");51// } catch (e) {52//   console.log(e);53// }54//55export function format(56  code: string,57  opts: WasmFormatOptions = new WasmFormatOptions(),58): string {59  return formatRaw(code, opts);60}