XLSX worker protocol
retab-ui/xlsx-worker-protocolFreeUtility
Typed worker messages, parse limits, and error codes for the XLSX viewer.
Install it
npx shadcn@latest add https://ui.retab.com/r/xlsx-worker-protocol.jsonSource
1import type { CompactSheet } from "@/lib/xlsx-workbook";23export interface XlsxParseLimits {4 maxRowMajorIndex: number;5 maxNonEmptyCells: number;6 maxTextChars: number;7}89export const XLSX_PARSE_LIMITS: XlsxParseLimits = {10 maxRowMajorIndex: 0xffffffff,11 maxNonEmptyCells: 1_000_000,12 maxTextChars: 50_000_000,13};1415export type XlsxWorkerErrorCode =16 | "parse_failed"17 | "range_too_large"18 | "too_many_cells"19 | "text_too_large";2021export type XlsxWorkerRequest = {22 type: "parse_workbook";23 buffer: ArrayBuffer;24};2526export type XlsxWorkerResponse =27 | { type: "workbook"; sheets: CompactSheet[] }28 | { type: "error"; message: string; code: XlsxWorkerErrorCode };2930export class XlsxWorkerError extends Error {31 constructor(32 readonly code: XlsxWorkerErrorCode,33 message: string,34 ) {35 super(message);36 this.name = "XlsxWorkerError";37 }38}3940// ZIP local-file-header variants ("PK\x03\x04", empty, and spanned archives)41// cover .xlsx/.xlsm/.xlsb/.ods. The OLE2 / Compound File Binary header covers42// legacy binary .xls.43const SPREADSHEET_CONTAINER_PREFIXES: ReadonlyArray<ReadonlyArray<number>> = [44 [0x50, 0x4b, 0x03, 0x04],45 [0x50, 0x4b, 0x05, 0x06],46 [0x50, 0x4b, 0x07, 0x08],47 [0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1],48];4950function hasBytePrefix(51 bytes: Uint8Array,52 prefix: ReadonlyArray<number>,53): boolean {54 if (bytes.length < prefix.length) return false;55 for (let i = 0; i < prefix.length; i++) {56 if (bytes[i] !== prefix[i]) return false;57 }58 return true;59}6061/**62 * True when the buffer begins with a recognized spreadsheet container63 * signature: a ZIP archive (.xlsx/.xlsm/.xlsb/.ods) or an OLE2 compound file64 * (legacy .xls). This guards SheetJS's lenient format sniffer, which would65 * otherwise coerce arbitrary bytes — a mislabeled PDF, image, or text file —66 * into a degenerate single-cell sheet instead of surfacing a parse error.67 */68export function isSpreadsheetContainer(buffer: ArrayBuffer): boolean {69 const bytes = new Uint8Array(buffer);70 return SPREADSHEET_CONTAINER_PREFIXES.some((prefix) =>71 hasBytePrefix(bytes, prefix),72 );73}
What it pulls in
Other registry items
Files it writes
More from retab-ui
All 130 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| CSV parsercsv | retab-ui | Utilities | Free | no deps · 4 files | |
| Document source modeldocument-source | retab-ui | Utilities | Free | no deps | |
| DOCX Document Resourcedocx-document-resource | retab-ui | Utilities | Free | no deps · 5 files | |
| Effect Keyeffect-key | retab-ui | Utilities | Free | no deps | |
| PDF Document Resourcepdf-document-resource | retab-ui | Utilities | Free | 1 dep · 6 files | |
| Segment Interactionsegment-interaction | retab-ui | Utilities | Free | no deps | |
| Segments modelsegments | retab-ui | Utilities | Free | no deps | |
| Utilsutils | retab-ui | Utilities | Free | 2 deps |
