Files
accounted/lib/import/bank-file/parser.ts
T
Jakob Wennberg 1a41119682 perf(bundle): drop the BAS chart and the Node crypto polyfill from the shared client baseline (#1942)
* perf(bundle): drop the BAS chart and the Node crypto polyfill from the shared client baseline

Two chunks rode along in the first-load JS of almost every dashboard route:
the full BAS 2026 chart (315 KB uncompressed, in 81 route manifests) and
the browser polyfill for Node's crypto/vm/Buffer (327 KB, in 26 routes
incl. login and register). Neither was needed on first paint; both got
there through static imports of helpers that happen to live next to code
that needs the data or the builtin.

Node polyfill (4 pure splits, behaviour unchanged, re-exported from the
original modules for server callers):
- lib/auth/bankid-flags.ts: isBankIdEnabled (login, register, security
  settings imported it from bankid.ts, which imports crypto).
- lib/import/bank-file/formats.ts: the format registry + detection (the
  import history imported getFormat from parser.ts, which hashes).
- lib/salary/personnummer-format.ts: parsing/validation/formatting (the
  employee forms reached the encrypting personnummer.ts via tax-column).
- lib/auth/api-key-scopes.ts: scope catalogue, groups, tool map, helpers
  (the API key panel imported STAGING_SCOPES from the key generator).

BAS chart:
- lib/bookkeeping/bas-lazy.ts + use-bas-reference.ts: the chart becomes a
  dynamic import, fetched once per session after first paint; components
  that show BAS names/descriptions call useBasReference() and re-render
  when it lands. Until then (and on the server) only the hardcoded
  account-descriptions answer, so SSR and hydration agree.
- lib/bookkeeping/bas-labels.ts: class/group labels out of bas-reference.ts
  (account-descriptions needed a label and paid for the whole chart).
- lib/bookkeeping/bas-account-numbers.ts (generated, ~11 KB) +
  scripts/generate-bas-account-numbers.ts (--check) + parity test:
  isStandardBASAccountNumber for AddAccountDialog/ChartOfAccountsManager.
- lib/bookkeeping/account-classifier-{heuristic,client}.ts: the BAS-aligned
  heuristic shared by the server classifier and a client variant that uses
  the lazy chart.
- lib/bookkeeping/invoice-accounts.ts: INVOICE_FX_RATE_MISSING,
  InvoiceFxRateMissingError, getRevenueAccount, getOutputVatAccount out of
  invoice-entries.ts, whose engine import pulled account-backfill and the
  chart into SendInvoiceDialog/PaymentBookingDialog.
- CorrectOpeningBalanceDialog re-seeds names when the chart lands;
  OpeningBalanceRowEditor builds its Fuse indexes lazily; the
  ChartOfAccountsManager BAS-katalog tab awaits the chunk.

Tooling:
- scripts/perf/client-import-closure.mjs: static import closure of every
  'use client' module with the shortest chain to a target (file or bare
  specifier); found every path above without a build.
- scripts/checks/client-node-builtin.mjs wired into check:guards: a client
  module reaching a Node builtin is a hard failure (0 today).

Left as is: invoices/[id], its credit page and SendInvoiceDialog still
reach the chart through lib/invoices/issue-credit-note -> invoice-entries
-> engine -> account-backfill; splitting the engine is out of scope here.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(perf): unambiguous import-edge regex in the closure walker (CodeQL js/redos)

One quantifier per span: a greedy [^'"]* up to the specifier quote, which it
cannot cross, so a run of whitespace has a single parse. Same edges as
before (multi-line named imports, re-exports, side-effect imports; type-only
imports still skipped).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-26 15:07:49 +02:00

144 lines
5.1 KiB
TypeScript

/**
* Bank file parser: main entry point
*
* Auto-detects Swedish bank file formats and parses to normalized transactions.
* Supports Nordea, SEB, Swedbank, Handelsbanken CSV and ISO 20022 camt.053 XML.
*/
import * as crypto from 'crypto'
import type { BankFileFormat, BankFileFormatId, BankFileParseResult, ParsedBankTransaction } from './types'
// The format registry and detection live in ./formats (no Node imports) so
// client components (BankFileImportHistory) can name a format without
// pulling this module's `crypto` import into the browser bundle.
import { detectFileFormat, getAllFormats, getFormat } from './formats'
export { detectFileFormat, getAllFormats, getFormat } from './formats'
/**
* Parse a bank file with auto-detection or explicit format
*
* @param content - File content as string (already decoded)
* @param filename - Original filename (used for format detection hints)
* @param formatId - Optional explicit format to use (skips auto-detection)
*/
export function parseBankFile(
content: string,
filename: string,
formatId?: BankFileFormatId
): BankFileParseResult {
let format: BankFileFormat | undefined
if (formatId) {
format = getFormat(formatId)
if (!format) {
return {
format: formatId,
format_name: 'Unknown',
transactions: [],
date_from: null,
date_to: null,
issues: [{ row: 0, message: `Okänt format: ${formatId}`, severity: 'error' }],
stats: { total_rows: 0, parsed_rows: 0, skipped_rows: 0, total_income: 0, total_expenses: 0 },
}
}
const explicitResult = format.parse(content)
if (explicitResult.transactions.length > 0 || formatId === 'generic_csv') {
// A working explicit parse is never overridden. generic_csv is also
// exempt: it is the manual column-mapping escape hatch and its default
// mapping legitimately parses 0 rows before the user maps columns.
return explicitResult
}
// The explicit choice parsed nothing: fall back to auto-detection so an
// explicitly selected bank is never WORSE than "Automatisk identifiering".
// detectFileFormat can never return generic_csv (its detect() is always
// false), so this cannot reroute the UI into the mapping flow.
const detected = detectFileFormat(content, filename)
if (detected && detected.id !== formatId) {
const detectedResult = detected.parse(content)
if (detectedResult.transactions.length > 0) {
return {
...detectedResult,
issues: [
{
row: 0,
message: `Filen matchade inte det valda formatet (${format.name}) och tolkades istället som ${detected.name}.`,
severity: 'info',
},
...detectedResult.issues,
],
}
}
}
return explicitResult
} else {
format = detectFileFormat(content, filename) || undefined
if (!format) {
// Build diagnostic message listing which formats were tried
const tried = getAllFormats()
.filter(f => f.id !== 'generic_csv')
.map(f => f.name)
const firstLine = content.split('\n')[0]?.substring(0, 80) || ''
return {
format: 'generic_csv',
format_name: 'Unknown',
transactions: [],
date_from: null,
date_to: null,
issues: [{
row: 0,
message: `Kunde inte identifiera bankformat. Testade: ${tried.join(', ')}. Första raden: "${firstLine}". Välj bank manuellt eller använd "Annan CSV".`,
severity: 'error',
}],
stats: { total_rows: 0, parsed_rows: 0, skipped_rows: 0, total_income: 0, total_expenses: 0 },
}
}
}
return format.parse(content)
}
/**
* Generate a stable external_id for a parsed bank transaction.
*
* For CSV files: SHA-256 of (format + date + description + amount + row_index)
* For camt.053: Uses the entry reference from the XML if available
*
* Two identical transactions on the same day will get different IDs due to row_index.
*/
export function generateExternalId(
tx: ParsedBankTransaction,
formatId: BankFileFormatId,
rowIndex: number
): string {
// For camt.053, prefer the raw_line which contains the entry reference
if (formatId === 'camt053' && tx.raw_line && !tx.raw_line.startsWith('camt053_entry_')) {
return `camt053_${tx.raw_line}`
}
// Wise carries the stable transfer ID (TRANSFER-…, PLAN_ORDER-…, plus a
// `-fee` suffix for fee rows) in raw_line: use it so re-importing the same
// statement dedups exactly instead of relying on the row hash.
if (formatId === 'wise' && tx.raw_line) {
return `wise_${tx.raw_line}`
}
if (formatId === 'wise_statement' && tx.raw_line) {
return `wise_${tx.raw_line}`
}
// For CSV formats, create a composite hash
const composite = `${formatId}|${tx.date}|${tx.description}|${tx.amount}|${rowIndex}`
const hash = crypto.createHash('sha256').update(composite).digest('hex').substring(0, 16)
return `${formatId}_${hash}`
}
/**
* Generate a file hash for dedup of the same file being uploaded twice
*/
export function generateFileHash(content: string): string {
return crypto.createHash('sha256').update(content).digest('hex')
}