Browse Source

more code cleanup

main
Silberengel 4 weeks ago
parent
commit
070ae84525
  1. 18
      src/lib/services/logger.ts
  2. 8
      src/lib/services/nostr/nip19-utils.ts
  3. 39
      src/lib/types/highlight-js.d.ts
  4. 13
      src/lib/types/logger.ts
  5. 80
      src/lib/types/nip19.ts
  6. 9
      src/routes/+page.svelte
  7. 2
      src/routes/docs/+page.svelte
  8. 2
      src/routes/docs/nip34/+page.svelte
  9. 2
      src/routes/docs/nip34/spec/+page.svelte
  10. 4
      src/routes/repos/[npub]/[repo]/+page.svelte

18
src/lib/services/logger.ts

@ -4,21 +4,23 @@
* Browser-safe: falls back to console in browser environments * Browser-safe: falls back to console in browser environments
*/ */
function createConsoleLogger() { import type { Logger } from '../types/logger.js';
function createConsoleLogger(): Logger {
return { return {
info: (...args: any[]) => console.log('[INFO]', ...args), info: (...args: unknown[]) => console.log('[INFO]', ...args),
error: (...args: any[]) => console.error('[ERROR]', ...args), error: (...args: unknown[]) => console.error('[ERROR]', ...args),
warn: (...args: any[]) => console.warn('[WARN]', ...args), warn: (...args: unknown[]) => console.warn('[WARN]', ...args),
debug: (...args: any[]) => console.debug('[DEBUG]', ...args), debug: (...args: unknown[]) => console.debug('[DEBUG]', ...args),
trace: (...args: any[]) => console.trace('[TRACE]', ...args), trace: (...args: unknown[]) => console.trace('[TRACE]', ...args),
fatal: (...args: any[]) => console.error('[FATAL]', ...args) fatal: (...args: unknown[]) => console.error('[FATAL]', ...args)
}; };
} }
// Check if we're in a Node.js environment // Check if we're in a Node.js environment
const isNode = typeof process !== 'undefined' && process.versions?.node; const isNode = typeof process !== 'undefined' && process.versions?.node;
let logger: any; let logger: Logger;
if (isNode) { if (isNode) {
// Server-side: use pino // Server-side: use pino

8
src/lib/services/nostr/nip19-utils.ts

@ -3,6 +3,7 @@
*/ */
import { nip19 } from 'nostr-tools'; import { nip19 } from 'nostr-tools';
import type { DecodedNevent, DecodedNaddr, DecodedNote } from '../../types/nip19.js';
export interface DecodedEvent { export interface DecodedEvent {
type: 'nevent' | 'naddr' | 'note'; type: 'nevent' | 'naddr' | 'note';
@ -27,7 +28,7 @@ export function decodeNostrAddress(input: string): DecodedEvent | null {
const decoded = nip19.decode(trimmed); const decoded = nip19.decode(trimmed);
if (decoded.type === 'nevent') { if (decoded.type === 'nevent') {
const data = decoded.data as { id: string; pubkey?: string; relays?: string[] }; const data = decoded.data as DecodedNevent['data'];
return { return {
type: 'nevent', type: 'nevent',
id: data.id, id: data.id,
@ -35,7 +36,7 @@ export function decodeNostrAddress(input: string): DecodedEvent | null {
relays: data.relays relays: data.relays
}; };
} else if (decoded.type === 'naddr') { } else if (decoded.type === 'naddr') {
const data = decoded.data as { pubkey: string; kind: number; identifier: string; relays?: string[] }; const data = decoded.data as DecodedNaddr['data'];
return { return {
type: 'naddr', type: 'naddr',
pubkey: data.pubkey, pubkey: data.pubkey,
@ -44,9 +45,10 @@ export function decodeNostrAddress(input: string): DecodedEvent | null {
relays: data.relays relays: data.relays
}; };
} else if (decoded.type === 'note') { } else if (decoded.type === 'note') {
const data = decoded.data as DecodedNote['data'];
return { return {
type: 'note', type: 'note',
id: decoded.data as string id: data
}; };
} }
} catch (e) { } catch (e) {

39
src/lib/types/highlight-js.d.ts vendored

@ -0,0 +1,39 @@
/**
* Type definitions for highlight.js
* These are minimal types for the parts we use
*/
declare module 'highlight.js' {
export interface HighlightResult {
value: string;
language?: string;
relevance?: number;
}
export interface HighlightOptions {
language?: string;
ignoreIllegals?: boolean;
}
export interface AutoHighlightResult extends HighlightResult {
secondBest?: HighlightResult;
}
export interface Language {
name?: string;
aliases?: string[];
keywords?: Record<string, any>;
contains?: any[];
}
export interface HLJSApi {
highlight(code: string, options: HighlightOptions): HighlightResult;
highlightAuto(code: string, options?: { languageSubset?: string[] }): AutoHighlightResult;
getLanguage(name: string): Language | undefined;
registerLanguage(name: string, language: (hljs: HLJSApi) => Language): void;
listLanguages(): string[];
}
const hljs: HLJSApi;
export default hljs;
}

13
src/lib/types/logger.ts

@ -0,0 +1,13 @@
/**
* Logger interface for consistent logging across the application
* Compatible with both pino (server-side) and console (browser-side)
*/
export interface Logger {
info(...args: unknown[]): void;
error(...args: unknown[]): void;
warn(...args: unknown[]): void;
debug(...args: unknown[]): void;
trace(...args: unknown[]): void;
fatal(...args: unknown[]): void;
}

80
src/lib/types/nip19.ts

@ -0,0 +1,80 @@
/**
* Type definitions for NIP-19 bech32 encoded entities
* These types help with proper typing of nip19.decode() return values
*/
/**
* Decoded npub (public key)
*/
export interface DecodedNpub {
type: 'npub';
data: string; // hex pubkey
}
/**
* Decoded nsec (private key)
*/
export interface DecodedNsec {
type: 'nsec';
data: Uint8Array; // private key bytes
}
/**
* Decoded note (event ID)
*/
export interface DecodedNote {
type: 'note';
data: string; // hex event ID
}
/**
* Decoded nevent (event reference)
*/
export interface DecodedNevent {
type: 'nevent';
data: {
id: string; // hex event ID
pubkey?: string; // hex pubkey
relays?: string[]; // relay hints
kind?: number; // event kind
};
}
/**
* Decoded naddr (parameterized replaceable event)
*/
export interface DecodedNaddr {
type: 'naddr';
data: {
pubkey: string; // hex pubkey
kind: number; // event kind
identifier: string; // d-tag value
relays?: string[]; // relay hints
};
}
/**
* Union type for all decoded NIP-19 entities
*/
export type DecodedNip19 = DecodedNpub | DecodedNsec | DecodedNote | DecodedNevent | DecodedNaddr;
/**
* Type guard to check if decoded value is an npub
*/
export function isDecodedNpub(decoded: DecodedNip19): decoded is DecodedNpub {
return decoded.type === 'npub';
}
/**
* Type guard to check if decoded value is a nevent
*/
export function isDecodedNevent(decoded: DecodedNip19): decoded is DecodedNevent {
return decoded.type === 'nevent';
}
/**
* Type guard to check if decoded value is an naddr
*/
export function isDecodedNaddr(decoded: DecodedNip19): decoded is DecodedNaddr {
return decoded.type === 'naddr';
}

9
src/routes/+page.svelte

@ -311,8 +311,13 @@
let queryHex = ''; let queryHex = '';
try { try {
const decoded = nip19.decode(query); const decoded = nip19.decode(query);
if (decoded.type === 'naddr' || decoded.type === 'nevent') { if (decoded.type === 'nevent') {
queryHex = (decoded.data as any).id || ''; const data = decoded.data as { id: string };
queryHex = data.id || '';
} else if (decoded.type === 'naddr') {
// For naddr, we can't extract an event ID directly, skip
} else if (decoded.type === 'note') {
queryHex = decoded.data as string;
} }
} catch { } catch {
// Not a bech32 encoded value // Not a bech32 encoded value

2
src/routes/docs/+page.svelte

@ -14,7 +14,7 @@
const hljsModule = await import('highlight.js'); const hljsModule = await import('highlight.js');
const hljs = hljsModule.default || hljsModule; const hljs = hljsModule.default || hljsModule;
const md: any = new MarkdownIt({ const md = new MarkdownIt({
highlight: function (str: string, lang: string): string { highlight: function (str: string, lang: string): string {
if (lang && hljs.getLanguage(lang)) { if (lang && hljs.getLanguage(lang)) {
try { try {

2
src/routes/docs/nip34/+page.svelte

@ -14,7 +14,7 @@
const hljsModule = await import('highlight.js'); const hljsModule = await import('highlight.js');
const hljs = hljsModule.default || hljsModule; const hljs = hljsModule.default || hljsModule;
const md: any = new MarkdownIt({ const md = new MarkdownIt({
highlight: function (str: string, lang: string): string { highlight: function (str: string, lang: string): string {
if (lang && hljs.getLanguage(lang)) { if (lang && hljs.getLanguage(lang)) {
try { try {

2
src/routes/docs/nip34/spec/+page.svelte

@ -14,7 +14,7 @@
const hljsModule = await import('highlight.js'); const hljsModule = await import('highlight.js');
const hljs = hljsModule.default || hljsModule; const hljs = hljsModule.default || hljsModule;
const md: any = new MarkdownIt({ const md = new MarkdownIt({
highlight: function (str: string, lang: string): string { highlight: function (str: string, lang: string): string {
if (lang && hljs.getLanguage(lang)) { if (lang && hljs.getLanguage(lang)) {
try { try {

4
src/routes/repos/[npub]/[repo]/+page.svelte

@ -128,7 +128,7 @@
const hljsModule = await import('highlight.js'); const hljsModule = await import('highlight.js');
const hljs = hljsModule.default || hljsModule; const hljs = hljsModule.default || hljsModule;
const md: any = new MarkdownIt({ const md = new MarkdownIt({
highlight: function (str: string, lang: string): string { highlight: function (str: string, lang: string): string {
if (lang && hljs.getLanguage(lang)) { if (lang && hljs.getLanguage(lang)) {
try { try {
@ -212,7 +212,7 @@
// Register AsciiDoc language if needed (not in highlight.js by default) // Register AsciiDoc language if needed (not in highlight.js by default)
if (lang === 'asciidoc' && !hljs.getLanguage('asciidoc')) { if (lang === 'asciidoc' && !hljs.getLanguage('asciidoc')) {
hljs.registerLanguage('asciidoc', function(hljs: any) { hljs.registerLanguage('asciidoc', function(hljs) {
return { return {
name: 'AsciiDoc', name: 'AsciiDoc',
aliases: ['adoc', 'asciidoc', 'ad'], aliases: ['adoc', 'asciidoc', 'ad'],

Loading…
Cancel
Save