Skip to content

Commit

Permalink
fix: fixed build & refactor icons
Browse files Browse the repository at this point in the history
  • Loading branch information
onmax committed Nov 18, 2024
1 parent fc8cb80 commit 910e562
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 24 deletions.
1 change: 1 addition & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import process from 'node:process'
import { consola } from 'consola'
import topLevelAwait from 'vite-plugin-top-level-await'
import wasm from 'vite-plugin-wasm'

Expand Down
3 changes: 1 addition & 2 deletions packages/nimiq-rewards-calculator/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { posSupplyAt } from 'nimiq-supply-calculator'
import { SUPPLY_AT_PROOF_OF_STAKE_FORK_DATE } from 'nimiq-supply-calculator/utils'
import { posSupplyAt, SUPPLY_AT_PROOF_OF_STAKE_FORK_DATE } from 'nimiq-supply-calculator'

// = 1 - POS_SUPPLY_DECAY ** (1000 * 60 * 60 * 24)
const DECAY_PER_DAY = 0.0003432600460362
Expand Down
5 changes: 5 additions & 0 deletions packages/nimiq-supply-calculator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
"types": "./dist/pow.d.ts",
"import": "./dist/pow.mjs",
"require": "./dist/pow.cjs"
},
"./constants": {
"types": "./dist/constants.d.ts",
"import": "./dist/constants.mjs",
"require": "./dist/constants.cjs"
}
},
"main": "./dist/index.mjs",
Expand Down
2 changes: 2 additions & 0 deletions packages/nimiq-supply-calculator/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './constants'
export * from './pos'
export * from './pow'
export * from './utils'
60 changes: 38 additions & 22 deletions server/utils/icon.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ValidatorJSON } from './schemas'
import { Buffer } from 'node:buffer'
import { consola } from 'consola'
import { createIdenticon, getIdenticonsParams } from 'identicons-esm'
import { optimize } from 'svgo'
Expand All @@ -21,35 +22,50 @@ export async function getBrandingParameters(address: string, { icon: _icon, acce
let icon: string = _icon

if (icon.startsWith('data:image/svg+xml')) {
// Handle both base64 and URL-encoded SVGs
const isBase64 = icon.includes(';base64,')
const encoded = icon
.replace(/^data:image\/svg\+xml;base64,/, '')
.replace(/^data:image\/svg\+xml,/, '')
.trim()

let svgContent: string
try {
// Decode the SVG content properly
const svgContent = decodeURIComponent(
icon.replace(/^data:image\/svg\/xml;base64,/, '')
.replace(/^data:image\/svg\+xml,/, ''),
).trim()

// Validate SVG content
if (!svgContent.startsWith('<svg') && !svgContent.startsWith('<?xml')) {
throw new Error(`Invalid SVG content for ${address}`)
}

// Optimize with error handling
const optimizedSvg = optimize(svgContent, {
plugins: [{ name: 'preset-default' }],
js2svg: { pretty: false, indent: 2 },
})
svgContent = isBase64
? Buffer.from(encoded, 'base64').toString('utf-8')
: decodeURIComponent(encoded)
svgContent = svgContent.trim()
}
catch (error) {
consola.error(`Error decoding SVG content for ${address}: ${error}`)
return { icon: _icon, accentColor: accentColor!, hasDefaultIcon: false }
}

// Validate SVG content
if (!svgContent.startsWith('<svg') && !svgContent.startsWith('<?xml')) {
consola.error(`Invalid SVG content for ${address}. Starting with: ${svgContent.slice(0, 50)}. Ending with: ${svgContent.slice(-50)}`)
return { icon: _icon, accentColor: accentColor!, hasDefaultIcon: false }
}

if (!optimizedSvg.data) {
consola.warn(`SVGO optimization failed for ${address}`)
}
// Optimize with error handling
let optimizedSvg: string

icon = `data:image/svg+xml,${encodeURIComponent(optimizedSvg.data)}`
try {
optimizedSvg = optimize(svgContent, {
plugins: [{ name: 'preset-default' }],
js2svg: { pretty: false, indent: 2 },
}).data
}
catch (error) {
console.error(`Error optimizing SVG for ${address}: ${error}`)
// Fallback to original icon if optimization fails
consola.error(`Error optimizing SVG for ${address}: ${error}`)
return { icon: _icon, accentColor: accentColor!, hasDefaultIcon: false }
}

if (!optimizedSvg) {
consola.warn(`SVGO optimization failed for ${address}`)
}

icon = `data:image/svg+xml,${encodeURIComponent(optimizedSvg)}`
}

return { icon, accentColor: accentColor!, hasDefaultIcon: false }
Expand Down

0 comments on commit 910e562

Please sign in to comment.