refactor: scoreinfo mscz url
This commit is contained in:
parent
141dac44ac
commit
92619ab3f7
2 changed files with 15 additions and 10 deletions
|
@ -4,16 +4,16 @@ import { ScoreInfo } from './scoreinfo'
|
|||
|
||||
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
|
||||
|
||||
if (!msczBufferP) {
|
||||
const url = scoreinfo.msczCidUrl
|
||||
msczBufferP = (async (): Promise<ArrayBuffer> => {
|
||||
const r0 = await fetch(url)
|
||||
const r0 = await _fetch(url)
|
||||
assertRes(r0)
|
||||
const { Key } = await r0.json()
|
||||
const r = await fetch(`https://ipfs.infura.io/ipfs/${Key as string}`)
|
||||
const cidRes = await r0.json()
|
||||
const r = await _fetch(scoreinfo.getMsczUrl(cidRes))
|
||||
assertRes(r)
|
||||
const data = await r.arrayBuffer()
|
||||
return data
|
||||
|
|
|
@ -1,16 +1,15 @@
|
|||
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
||||
|
||||
const IPNS_KEY = 'QmSdXtvzC8v8iTTZuj5cVmiugnzbR1QATYRcGix4bBsioP'
|
||||
const RADIX = 20
|
||||
|
||||
export abstract class ScoreInfo {
|
||||
private readonly IPNS_KEY = 'QmSdXtvzC8v8iTTZuj5cVmiugnzbR1QATYRcGix4bBsioP';
|
||||
private readonly RADIX = 20;
|
||||
|
||||
abstract id: number;
|
||||
abstract title: string;
|
||||
|
||||
public store = new Map<symbol, any>();
|
||||
|
||||
get idLastDigit (): number {
|
||||
return (+this.id) % RADIX
|
||||
return (+this.id) % this.RADIX
|
||||
}
|
||||
|
||||
get fileName (): string {
|
||||
|
@ -18,12 +17,18 @@ export abstract class ScoreInfo {
|
|||
}
|
||||
|
||||
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 {
|
||||
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 {
|
||||
|
|
Loading…
Reference in a new issue