refactor: get ipfs ref
This commit is contained in:
		
							parent
							
								
									b658c00a09
								
							
						
					
					
						commit
						6acfec8141
					
				
					 3 changed files with 64 additions and 34 deletions
				
			
		|  | @ -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
 | ||||
|  |  | |||
							
								
								
									
										69
									
								
								src/mscz.ts
									
										
									
									
									
								
							
							
						
						
									
										69
									
								
								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<string> => { | ||||
|   // 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<string> => { | ||||
|   // 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<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) | ||||
|       // 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 | ||||
|  |  | |||
|  | @ -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)}` | ||||
|   } | ||||
| } | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue