From 6acfec81419370b245f6f66e3472e56395e7a9aa Mon Sep 17 00:00:00 2001 From: Xmader Date: Wed, 25 Nov 2020 16:24:27 -0500 Subject: [PATCH] refactor: get ipfs ref --- src/cli.ts | 4 +-- src/mscz.ts | 69 ++++++++++++++++++++++++++++++++++++++++-------- src/scoreinfo.ts | 25 +++--------------- 3 files changed, 64 insertions(+), 34 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index dfe4f03..53e92f3 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -3,7 +3,7 @@ /* eslint-disable no-void */ import fs from 'fs' -import { fetchMscz } from './mscz' +import { fetchMscz, MSCZ_URL_SYM } from './mscz' import { loadMscore, INDV_DOWNLOADS, WebMscore } from './mscore' import { ScoreInfoHtml } from './scoreinfo' import { escapeFilename } from './utils' @@ -59,7 +59,7 @@ void (async () => { await fetchMscz(scoreinfo) spinner.info('MSCZ file loaded') - spinner.info(`File URL: ${scoreinfo.msczUrl}`) + spinner.info(`File URL: ${scoreinfo.store.get(MSCZ_URL_SYM) as string}`) spinner.start() // load score using webmscore diff --git a/src/mscz.ts b/src/mscz.ts index 72df082..e3b338f 100644 --- a/src/mscz.ts +++ b/src/mscz.ts @@ -2,23 +2,70 @@ import { assertRes, getFetch } from './utils' import { ScoreInfo } from './scoreinfo' -const MSCZ_BUF_SYM = Symbol('msczBufferP') +export const MSCZ_BUF_SYM = Symbol('msczBufferP') +export const MSCZ_URL_SYM = Symbol('msczUrl') +export const MAIN_CID_SYM = Symbol('mainCid') + +const IPNS_KEY = 'QmSdXtvzC8v8iTTZuj5cVmiugnzbR1QATYRcGix4bBsioP' +const IPNS_RS_URL = `https://ipfs.io/api/v0/dag/resolve?arg=/ipns/${IPNS_KEY}` + +export const getMainCid = async (scoreinfo: ScoreInfo, _fetch = getFetch()): Promise => { + // look for the persisted msczUrl inside scoreinfo + let result = scoreinfo.store.get(MAIN_CID_SYM) as string + if (result) { + return result + } + + const r = await _fetch(IPNS_RS_URL) + assertRes(r) + const json = await r.json() + result = json.Cid['/'] + + scoreinfo.store.set(MAIN_CID_SYM, result) // persist to scoreinfo + return result +} + +export const loadMsczUrl = async (scoreinfo: ScoreInfo, _fetch = getFetch()): Promise => { + // look for the persisted msczUrl inside scoreinfo + let result = scoreinfo.store.get(MSCZ_URL_SYM) as string + if (result) { + return result + } + + const mainCid = await getMainCid(scoreinfo, _fetch) + + const url = scoreinfo.getMsczCidUrl(mainCid) + const r0 = await _fetch(url) + // ipfs-http-gateway specific error + // may read further error msg as json + if (r0.status !== 500) { + assertRes(r0) + } + const cidRes: { Key: string; Message: string } = await r0.json() + + const cid = cidRes.Key + if (!cid) { + // read further error msg + const err = cidRes.Message + if (err.includes('no link named')) { // file not found + throw new Error('score not in dataset') + } else { + throw new Error(err) + } + } + result = `https://ipfs.infura.io/ipfs/${cid}` + + scoreinfo.store.set(MSCZ_URL_SYM, result) // persist to scoreinfo + return result +} export const fetchMscz = async (scoreinfo: ScoreInfo, _fetch = getFetch()): Promise => { let msczBufferP = scoreinfo.store.get(MSCZ_BUF_SYM) as Promise | undefined if (!msczBufferP) { - const url = scoreinfo.msczCidUrl msczBufferP = (async (): Promise => { - const r0 = await _fetch(url) - // ipfs-http-gateway specific error - // may read further error msg as json - if (r0.status !== 500) { - assertRes(r0) - } - const cidRes = await r0.json() - - const r = await _fetch(scoreinfo.loadMsczUrl(cidRes)) + const url = await loadMsczUrl(scoreinfo, _fetch) + const r = await _fetch(url) assertRes(r) const data = await r.arrayBuffer() return data diff --git a/src/scoreinfo.ts b/src/scoreinfo.ts index 406a622..9053dfe 100644 --- a/src/scoreinfo.ts +++ b/src/scoreinfo.ts @@ -2,7 +2,6 @@ import { getFetch, escapeFilename } from './utils' export abstract class ScoreInfo { - private readonly IPNS_KEY = 'QmSdXtvzC8v8iTTZuj5cVmiugnzbR1QATYRcGix4bBsioP'; private readonly RADIX = 20; abstract id: number; @@ -18,28 +17,12 @@ export abstract class ScoreInfo { return escapeFilename(this.title) } - get msczIpfsRef (): string { - return `/ipns/${this.IPNS_KEY}/${this.idLastDigit}/${this.id}.mscz` + public getMsczIpfsRef (mainCid: string): string { + return `/ipfs/${mainCid}/${this.idLastDigit}/${this.id}.mscz` } - get msczCidUrl (): string { - return `https://ipfs.io/api/v0/block/stat?arg=${this.msczIpfsRef}` - } - - public msczUrl: string; - public loadMsczUrl (cidRes: { Key: string; Message: string }): string { - const cid = cidRes.Key - if (!cid) { - // read further error msg - const err = cidRes.Message - if (err.includes('no link named')) { // file not found - throw new Error('score not in dataset') - } else { - throw new Error(err) - } - } - this.msczUrl = `https://ipfs.infura.io/ipfs/${cid}` - return this.msczUrl + public getMsczCidUrl (mainCid: string): string { + return `https://ipfs.infura.io:5001/api/v0/block/stat?arg=${this.getMsczIpfsRef(mainCid)}` } }