@f-o-t/ofx 2.1.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +137 -22
  2. package/dist/index.d.ts +414 -138
  3. package/dist/index.js +607 -208
  4. package/package.json +6 -7
package/README.md CHANGED
@@ -260,6 +260,62 @@ console.log("Accounts:", result.accounts);
260
260
  console.log("Balances:", result.balances);
261
261
  ```
262
262
 
263
+ ### Batch Streaming Functions
264
+
265
+ For processing multiple OFX files in a single operation with progress tracking.
266
+
267
+ #### `parseBatchStream(files): AsyncGenerator<BatchStreamEvent>`
268
+
269
+ Parses multiple OFX files sequentially, yielding events as they are parsed. Ideal for importing multiple bank statements at once.
270
+
271
+ ```typescript
272
+ import { parseBatchStream, type BatchFileInput } from "@fot/ofx";
273
+ import { readFileSync } from "node:fs";
274
+
275
+ const files: BatchFileInput[] = [
276
+ { filename: "january.ofx", buffer: new Uint8Array(readFileSync("january.ofx")) },
277
+ { filename: "february.ofx", buffer: new Uint8Array(readFileSync("february.ofx")) },
278
+ { filename: "march.ofx", buffer: new Uint8Array(readFileSync("march.ofx")) },
279
+ ];
280
+
281
+ for await (const event of parseBatchStream(files)) {
282
+ switch (event.type) {
283
+ case "file_start":
284
+ console.log(`Processing: ${event.filename}`);
285
+ break;
286
+ case "transaction":
287
+ console.log(`File ${event.fileIndex}: ${event.data.NAME} - ${event.data.TRNAMT}`);
288
+ break;
289
+ case "file_complete":
290
+ console.log(`Completed ${event.filename}: ${event.transactionCount} transactions`);
291
+ break;
292
+ case "file_error":
293
+ console.error(`Error in ${event.filename}: ${event.error}`);
294
+ break;
295
+ case "batch_complete":
296
+ console.log(`Batch done: ${event.totalTransactions} transactions from ${event.totalFiles} files`);
297
+ break;
298
+ }
299
+ }
300
+ ```
301
+
302
+ #### `parseBatchStreamToArray(files): Promise<BatchParsedFile[]>`
303
+
304
+ Collects all batch results into an array for easier processing.
305
+
306
+ ```typescript
307
+ import { parseBatchStreamToArray } from "@fot/ofx";
308
+
309
+ const results = await parseBatchStreamToArray(files);
310
+
311
+ for (const file of results) {
312
+ console.log(`${file.filename}: ${file.transactions.length} transactions`);
313
+ if (file.error) {
314
+ console.error(` Error: ${file.error}`);
315
+ }
316
+ }
317
+ ```
318
+
263
319
  ## Encoding Support
264
320
 
265
321
  The library automatically detects and handles various character encodings commonly used in OFX files, especially from Brazilian banks.
@@ -408,36 +464,95 @@ type StreamEvent =
408
464
  | { type: "complete"; transactionCount: number };
409
465
  ```
410
466
 
411
- ## Schemas
467
+ ## Validation & Schemas
468
+
469
+ All parsing and generation functions use Zod schemas for runtime validation, ensuring type safety and data integrity.
412
470
 
413
- All Zod schemas are exported for custom validation:
471
+ ### Input Validation for Generation
472
+
473
+ When generating OFX files, you can validate your inputs using the exported schemas:
414
474
 
415
475
  ```typescript
416
- import { schemas } from "@fot/ofx";
476
+ import {
477
+ generateBankStatement,
478
+ generateBankStatementOptionsSchema,
479
+ type GenerateBankStatementOptions,
480
+ } from "@fot/ofx";
481
+
482
+ // Validate options before generating
483
+ const options: GenerateBankStatementOptions = {
484
+ bankId: "123456",
485
+ accountId: "987654321",
486
+ accountType: "CHECKING",
487
+ currency: "USD",
488
+ startDate: new Date("2025-01-01"),
489
+ endDate: new Date("2025-01-31"),
490
+ transactions: [],
491
+ };
492
+
493
+ // Runtime validation
494
+ const validatedOptions = generateBankStatementOptionsSchema.parse(options);
495
+ const statement = generateBankStatement(validatedOptions);
496
+ ```
417
497
 
418
- const customTransactionSchema = schemas.transaction.extend({
498
+ ### Available Schemas
499
+
500
+ All Zod schemas are exported for custom validation and extension:
501
+
502
+ ```typescript
503
+ import {
504
+ transactionSchema,
505
+ bankAccountSchema,
506
+ ofxDocumentSchema,
507
+ } from "@fot/ofx";
508
+
509
+ // Extend schemas for custom validation
510
+ const customTransactionSchema = transactionSchema.extend({
419
511
  customField: z.string(),
420
512
  });
421
513
  ```
422
514
 
423
- Available schemas:
424
-
425
- - `schemas.transaction`
426
- - `schemas.transactionType`
427
- - `schemas.transactionList`
428
- - `schemas.bankAccount`
429
- - `schemas.creditCardAccount`
430
- - `schemas.accountType`
431
- - `schemas.balance`
432
- - `schemas.status`
433
- - `schemas.financialInstitution`
434
- - `schemas.signOnResponse`
435
- - `schemas.bankStatementResponse`
436
- - `schemas.creditCardStatementResponse`
437
- - `schemas.ofxDocument`
438
- - `schemas.ofxHeader`
439
- - `schemas.ofxResponse`
440
- - `schemas.ofxDate`
515
+ **Parsing Schemas:**
516
+ - `ofxDocumentSchema` - Complete OFX document
517
+ - `ofxHeaderSchema` - OFX file header
518
+ - `ofxResponseSchema` - OFX response body
519
+ - `transactionSchema` - Individual transaction
520
+ - `transactionTypeSchema` - Transaction type enum
521
+ - `transactionListSchema` - List of transactions
522
+ - `bankAccountSchema` - Bank account information
523
+ - `creditCardAccountSchema` - Credit card account information
524
+ - `accountTypeSchema` - Account type enum
525
+ - `balanceSchema` - Balance information
526
+ - `statusSchema` - Status response
527
+ - `financialInstitutionSchema` - Financial institution info
528
+ - `signOnResponseSchema` - Sign-on response
529
+ - `ofxDateSchema` - OFX date with timezone
530
+
531
+ **Generation Schemas:**
532
+ - `generateHeaderOptionsSchema` - OFX header generation options
533
+ - `generateTransactionInputSchema` - Transaction input for generation
534
+ - `generateBankStatementOptionsSchema` - Bank statement generation options
535
+ - `generateCreditCardStatementOptionsSchema` - Credit card statement generation options
536
+
537
+ ## Security
538
+
539
+ This library includes several security features to protect against malicious OFX files:
540
+
541
+ ### Prototype Pollution Protection
542
+
543
+ The SGML parser is protected against prototype pollution attacks. Malicious OFX files attempting to inject `__proto__`, `constructor`, or `prototype` tags are safely ignored, preventing potential remote code execution.
544
+
545
+ ### Input Validation
546
+
547
+ All parsing functions validate input data against strict Zod schemas, rejecting malformed or invalid OFX data before processing. This prevents:
548
+ - Type confusion attacks
549
+ - Invalid date/number formats
550
+ - Missing required fields
551
+ - Unexpected data structures
552
+
553
+ ### Safe Entity Decoding
554
+
555
+ HTML entities in OFX text fields are decoded using a whitelist approach, preventing XSS-style attacks through crafted entity sequences
441
556
 
442
557
  ## Performance
443
558