| 19 | } |
| 20 | |
| 21 | async function fetchCodeframe(u: string, line?: number) { |
| 22 | try { |
| 23 | const res = await fetch(u); |
| 24 | const text = await res.text(); |
| 25 | const hash = res.headers && (res.headers as any).get ? (res.headers as any).get('X-NS-Source-Hash') || '' : ''; |
| 26 | const lines = String(text || '').split('\n'); |
| 27 | const L = line && line > 0 ? line : 1; |
| 28 | const start = Math.max(1, L - 4), |
| 29 | end = Math.min(lines.length, L + 3); |
| 30 | let context = ''; |
| 31 | for (let i = start; i <= end; i++) { |
| 32 | const mark = i === L ? '>' : ' '; |
| 33 | const num = String(i).padStart(4, ' '); |
| 34 | context += `${mark}${num}: ${lines[i - 1]}\n`; |
| 35 | } |
| 36 | console.warn('[ns-entry][diag]', u === (globalThis as any).__NS_ENTRY_LAST_TARGET__ ? 'sanitized' : 'raw', hash ? `(hash ${hash})` : '', '\n' + context); |
| 37 | } catch (fe: any) { |
| 38 | try { |
| 39 | console.warn('[ns-entry][diag] fetch failed', u, fe && (fe.message || fe)); |
| 40 | } catch {} |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | export default async function startEntry(opts: EntryOpts) { |
| 45 | const ORIGIN = String(opts.origin || ''); |