Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Integrate Spyglass web API #1746

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open

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

4 changes: 2 additions & 2 deletions docs/developer/api.adoc
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
:page-layout: default
:page-title: API
:page-title: Web API
:page-parent: Developer Guides

:toc:

== Introduction

The Spyglass API provides access to various information that is helpful for data pack/resource pack
The Spyglass Web API provides access to various information that is helpful for data pack/resource pack
toolings. It uses https://github.com/misode/mcmeta[misode/mcmeta] and
https://github.com/SpyglassMC/vanilla-mcdoc[SpyglassMC/vanilla-mcdoc] under the hood and provides a
few advantages over using the GitHub API directly:
Expand Down
16 changes: 16 additions & 0 deletions package-lock.json

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

28 changes: 4 additions & 24 deletions packages/core/src/common/externals/BrowserExternals.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { decode as arrayBufferFromBase64, encode as arrayBufferToBase64 } from 'base64-arraybuffer'
import pako from 'pako'
import { fileUtil } from '../../service/fileUtil.js'
import type {
ExternalDownloader,
ExternalDownloaderOptions,
RemoteUriString,
} from './downloader.js'
import type {
ExternalEventEmitter,
ExternalFileSystem,
Expand Down Expand Up @@ -53,24 +48,6 @@ export class BrowserEventEmitter implements ExternalEventEmitter {
}
}

class BrowserExternalDownloader implements ExternalDownloader {
async get(uri: RemoteUriString, options: ExternalDownloaderOptions = {}): Promise<Uint8Array> {
const headers = new Headers()
for (const [name, value] of Object.entries(options?.headers ?? {})) {
const values = typeof value === 'string' ? [value] : value
for (const v of values) {
headers.append(name, v)
}
}
const res = await fetch(uri, { headers, redirect: 'follow' })
if (!res.ok) {
throw new Error(`Status code ${res.status}: ${res.ok}`)
} else {
return new Uint8Array(await res.arrayBuffer())
}
}
}

class BrowserFsWatcher implements FsWatcher {
on(event: string, listener: (...args: any[]) => unknown): this {
if (event === 'ready') {
Expand Down Expand Up @@ -188,7 +165,6 @@ export const BrowserExternals: Externals = {
return uint8ArrayToHex(new Uint8Array(hash))
},
},
downloader: new BrowserExternalDownloader(),
error: {
createKind(kind, message) {
return new Error(`${kind}: ${message}`)
Expand All @@ -199,6 +175,10 @@ export const BrowserExternals: Externals = {
},
event: { EventEmitter: BrowserEventEmitter },
fs: new BrowserFileSystem(),
web: {
fetch,
getCache: () => window.caches.open('spyglassmc'),
},
}

function uint8ArrayToHex(array: Uint8Array) {
Expand Down
Loading