Skip to content

Commit 63f9eed

Browse files
committed
Use Spyglass API to get vanilla-mcdoc symbols
1 parent 331a4e2 commit 63f9eed

File tree

2 files changed

+49
-19
lines changed

2 files changed

+49
-19
lines changed

src/app/services/DataFetcher.ts

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ declare var __LATEST_VERSION__: string
1212
export const latestVersion = __LATEST_VERSION__ ?? ''
1313
const mcmetaUrl = 'https://raw.githubusercontent.com/misode/mcmeta'
1414
const mcmetaTarballUrl = 'https://github.com/misode/mcmeta/tarball'
15-
const vanillaMcdocUrl = 'https://raw.githubusercontent.com/SpyglassMC/vanilla-mcdoc'
1615
const changesUrl = 'https://raw.githubusercontent.com/misode/technical-changes'
1716
const fixesUrl = 'https://raw.githubusercontent.com/misode/mcfixes'
1817
const versionDiffUrl = 'https://mcmeta-diff.misode.workers.dev'
@@ -48,19 +47,6 @@ export function getVersionChecksum(versionId: VersionId) {
4847
return version.ref
4948
}
5049

51-
export interface VanillaMcdocSymbols {
52-
ref: string,
53-
mcdoc: Record<string, unknown>,
54-
'mcdoc/dispatcher': Record<string, Record<string, unknown>>,
55-
}
56-
export async function fetchVanillaMcdoc(): Promise<VanillaMcdocSymbols> {
57-
try {
58-
return cachedFetch<VanillaMcdocSymbols>(`${vanillaMcdocUrl}/generated/symbols.json`, { refresh: true })
59-
} catch (e) {
60-
throw new Error(`Error occured while fetching vanilla-mcdoc: ${message(e)}`)
61-
}
62-
}
63-
6450
export async function fetchDependencyMcdoc(dependency: string) {
6551
try {
6652
return cachedFetch(`/mcdoc/${dependency}.mcdoc`, { decode: res => res.text(), refresh: true })
@@ -504,3 +490,41 @@ async function applyPatches() {
504490
localStorage.setItem(CACHE_PATCH, i.toFixed())
505491
}
506492
}
493+
494+
export async function fetchWithCache(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {
495+
const cache = await caches.open(CACHE_NAME)
496+
const request = new Request(input, init)
497+
const cachedResponse = await cache.match(request)
498+
const cachedEtag = cachedResponse?.headers.get('ETag')
499+
if (cachedEtag) {
500+
request.headers.set('If-None-Match', cachedEtag)
501+
}
502+
try {
503+
const response = await fetch(request)
504+
if (response.status === 304 && cachedResponse) {
505+
console.log(`[fetchWithCache] reusing cache for ${request.url}`)
506+
return cachedResponse
507+
} else if (!response.ok) {
508+
let message = response.statusText
509+
try {
510+
message = (await response.json()).message
511+
} catch (e) {}
512+
throw new TypeError(`${response.status} ${message}`)
513+
} else {
514+
try {
515+
await cache.put(request, response.clone())
516+
console.log(`[fetchWithCache] updated cache for ${request.url}`)
517+
} catch (e) {
518+
console.warn('[fetchWithCache] put cache', e)
519+
}
520+
return response
521+
}
522+
} catch (e) {
523+
console.warn('[fetchWithCache] fetch', e)
524+
if (cachedResponse) {
525+
console.log(`[fetchWithCache] falling back to cache for ${request.url}`)
526+
return cachedResponse
527+
}
528+
throw e
529+
}
530+
}

src/app/services/Spyglass.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import { TextDocument } from 'vscode-languageserver-textdocument'
1212
import type { ConfigGenerator } from '../Config.js'
1313
import siteConfig from '../Config.js'
1414
import { computeIfAbsent, genPath } from '../Utils.js'
15-
import type { VanillaMcdocSymbols, VersionMeta } from './DataFetcher.js'
16-
import { fetchBlockStates, fetchRegistries, fetchVanillaMcdoc, fetchVersions, getVersionChecksum } from './DataFetcher.js'
15+
import type { VersionMeta } from './DataFetcher.js'
16+
import { fetchBlockStates, fetchRegistries, fetchVersions, fetchWithCache, getVersionChecksum } from './DataFetcher.js'
1717
import { IndexedDbFileSystem } from './FileSystem.js'
1818
import type { VersionId } from './Versions.js'
1919

20+
const SPYGLASS_API = 'https://api.spyglassmc.com'
21+
2022
export const CACHE_URI = 'file:///cache/'
2123
export const ROOT_URI = 'file:///root/'
2224
export const DEPENDENCY_URI = `${ROOT_URI}dependency/`
@@ -367,10 +369,10 @@ async function compressBall(files: [string, string][]): Promise<Uint8Array> {
367369
const initialize: core.ProjectInitializer = async (ctx) => {
368370
const { config, logger, meta, externals, cacheRoot } = ctx
369371

370-
const vanillaMcdoc = await fetchVanillaMcdoc()
372+
const vanillaMcdocRes = await fetchWithCache(`${SPYGLASS_API}/vanilla-mcdoc/symbols`)
371373
meta.registerSymbolRegistrar('vanilla-mcdoc', {
372-
checksum: vanillaMcdoc.ref,
373-
registrar: vanillaMcdocRegistrar(vanillaMcdoc),
374+
checksum: vanillaMcdocRes.headers.get('ETag') ?? '',
375+
registrar: vanillaMcdocRegistrar(await vanillaMcdocRes.json()),
374376
})
375377

376378
meta.registerDependencyProvider('@misode-mcdoc', async () => {
@@ -479,6 +481,10 @@ function registerAttributes(meta: core.MetaRegistry, release: ReleaseVersion, ve
479481

480482
const VanillaMcdocUri = 'mcdoc://vanilla-mcdoc/symbols.json'
481483

484+
interface VanillaMcdocSymbols {
485+
mcdoc: Record<string, unknown>,
486+
'mcdoc/dispatcher': Record<string, Record<string, unknown>>,
487+
}
482488
function vanillaMcdocRegistrar(vanillaMcdoc: VanillaMcdocSymbols): core.SymbolRegistrar {
483489
return (symbols) => {
484490
const start = performance.now()

0 commit comments

Comments
 (0)