Skip to content

Commit

Permalink
feat: use code generation script for frequently used contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
HJunyuan committed Jul 26, 2024
1 parent 08ce6c7 commit 7851cd7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 372 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"test:vc": "scripts/runVcTest.sh",
"lint": "eslint . --ext .ts,.json --max-warnings 0",
"lint:fix": "npm run lint -- --fix",
"fetch-v4-contexts": "npx ts-node scripts/fetchV4Contexts.ts",
"generate-v4-fixtures": "npx ts-node scripts/generateV4JsonFixtures.ts",
"generate-v4-json-schemas": "npx ts-node scripts/generateV4JsonSchemas.ts",
"publish:schema": "./scripts/publishSchema.sh",
Expand Down
24 changes: 24 additions & 0 deletions scripts/fetchV4Contexts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import fs from "fs";
import path from "path";
import { ContextUrl } from "../src/4.0/context";

const OUTPUT_DIR = path.resolve("./src/4.0/contexts/__generated__");

// make sure the output directory exists
if (fs.existsSync(OUTPUT_DIR)) {
fs.rmSync(OUTPUT_DIR, { recursive: true });
}
fs.mkdirSync(OUTPUT_DIR, { recursive: true });

const CONTEXTS_TO_FETCH = Object.values(ContextUrl);

const sb: string[] = [`const contextsMap = new Map<string, string>();`];
Promise.all(
CONTEXTS_TO_FETCH.map(async (url) => {
const context = await (await fetch(url)).json();
sb.push(`contextsMap.set("${url}", \`${JSON.stringify(context)}\`);`);
})
).then(() => {
sb.push(`export { contextsMap };`);
fs.writeFileSync(path.join(OUTPUT_DIR, "index.ts"), sb.join("\n"));
});
9 changes: 5 additions & 4 deletions src/4.0/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fetch } from "cross-fetch";
import { expand, Options, JsonLdDocument } from "jsonld";
import { HARDCODED_CONTEXTS } from "./contexts/hardcodedContexts";
import { contextsMap } from "./contexts/__generated__";

export const ContextUrl = {
w3c_vc_v2: "https://www.w3.org/ns/credentials/v2",
Expand All @@ -12,7 +12,7 @@ export const ContextType = {
OAV4Context: "OpenAttestationCredential",
} as const;

const contextCache: Map<string, any> = new Map();
const contextCache: Map<string, Record<any, any>> = new Map();

let isFirstLoad = true;
// https://github.com/digitalbazaar/jsonld.js?tab=readme-ov-file#custom-document-loader
Expand All @@ -21,8 +21,9 @@ const documentLoader: Options.DocLoader["documentLoader"] = async (url, _) => {
// On first load: Preload hardcoded contexts so that no network call is necessary
if (isFirstLoad) {
isFirstLoad = false;
for (const url of Object.values(ContextUrl)) {
contextCache.set(url, HARDCODED_CONTEXTS[url]);
for (const [url, value] of contextsMap) {
const parsed = JSON.parse(value);
contextCache.set(url, parsed);
}
}
if (contextCache.get(url)) {
Expand Down
4 changes: 4 additions & 0 deletions src/4.0/contexts/__generated__/index.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7851cd7

Please sign in to comment.