refactor: scoreinfo mscz url
This commit is contained in:
parent
141dac44ac
commit
92619ab3f7
|
@ -4,16 +4,16 @@ import { ScoreInfo } from './scoreinfo'
|
||||||
|
|
||||||
const MSCZ_BUF_SYM = Symbol('msczBufferP')
|
const MSCZ_BUF_SYM = Symbol('msczBufferP')
|
||||||
|
|
||||||
export const fetchMscz = async (scoreinfo: ScoreInfo): Promise<ArrayBuffer> => {
|
export const fetchMscz = async (scoreinfo: ScoreInfo, _fetch = fetch): Promise<ArrayBuffer> => {
|
||||||
let msczBufferP = scoreinfo.store.get(MSCZ_BUF_SYM) as Promise<ArrayBuffer> | undefined
|
let msczBufferP = scoreinfo.store.get(MSCZ_BUF_SYM) as Promise<ArrayBuffer> | undefined
|
||||||
|
|
||||||
if (!msczBufferP) {
|
if (!msczBufferP) {
|
||||||
const url = scoreinfo.msczCidUrl
|
const url = scoreinfo.msczCidUrl
|
||||||
msczBufferP = (async (): Promise<ArrayBuffer> => {
|
msczBufferP = (async (): Promise<ArrayBuffer> => {
|
||||||
const r0 = await fetch(url)
|
const r0 = await _fetch(url)
|
||||||
assertRes(r0)
|
assertRes(r0)
|
||||||
const { Key } = await r0.json()
|
const cidRes = await r0.json()
|
||||||
const r = await fetch(`https://ipfs.infura.io/ipfs/${Key as string}`)
|
const r = await _fetch(scoreinfo.getMsczUrl(cidRes))
|
||||||
assertRes(r)
|
assertRes(r)
|
||||||
const data = await r.arrayBuffer()
|
const data = await r.arrayBuffer()
|
||||||
return data
|
return data
|
||||||
|
|
|
@ -1,16 +1,15 @@
|
||||||
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
||||||
|
|
||||||
const IPNS_KEY = 'QmSdXtvzC8v8iTTZuj5cVmiugnzbR1QATYRcGix4bBsioP'
|
|
||||||
const RADIX = 20
|
|
||||||
|
|
||||||
export abstract class ScoreInfo {
|
export abstract class ScoreInfo {
|
||||||
|
private readonly IPNS_KEY = 'QmSdXtvzC8v8iTTZuj5cVmiugnzbR1QATYRcGix4bBsioP';
|
||||||
|
private readonly RADIX = 20;
|
||||||
|
|
||||||
abstract id: number;
|
abstract id: number;
|
||||||
abstract title: string;
|
abstract title: string;
|
||||||
|
|
||||||
public store = new Map<symbol, any>();
|
public store = new Map<symbol, any>();
|
||||||
|
|
||||||
get idLastDigit (): number {
|
get idLastDigit (): number {
|
||||||
return (+this.id) % RADIX
|
return (+this.id) % this.RADIX
|
||||||
}
|
}
|
||||||
|
|
||||||
get fileName (): string {
|
get fileName (): string {
|
||||||
|
@ -18,12 +17,18 @@ export abstract class ScoreInfo {
|
||||||
}
|
}
|
||||||
|
|
||||||
get msczIpfsRef (): string {
|
get msczIpfsRef (): string {
|
||||||
return `/ipns/${IPNS_KEY}/${this.idLastDigit}/${this.id}.mscz`
|
return `/ipns/${this.IPNS_KEY}/${this.idLastDigit}/${this.id}.mscz`
|
||||||
}
|
}
|
||||||
|
|
||||||
get msczCidUrl (): string {
|
get msczCidUrl (): string {
|
||||||
return `https://ipfs.infura.io:5001/api/v0/block/stat?arg=${this.msczIpfsRef}`
|
return `https://ipfs.infura.io:5001/api/v0/block/stat?arg=${this.msczIpfsRef}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getMsczUrl (cidRes: { Key: string }): string {
|
||||||
|
const cid = cidRes.Key
|
||||||
|
if (!cid) throw new Error('score not in dataset')
|
||||||
|
return `https://ipfs.infura.io/ipfs/${cid}`
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ScoreInfoInPage extends ScoreInfo {
|
export class ScoreInfoInPage extends ScoreInfo {
|
||||||
|
|
Loading…
Reference in New Issue