wendang 0.2.2 → 0.2.4
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";function isWorkbookDocument(e){return!!e&&"object"==typeof e&&"workbook"===e.type}function record(e){return!!e&&"object"==typeof e&&!Array.isArray(e)}function index(e,o){return/^(0|[1-9]\d*)$/.test(e)&&Number(e)<o}function workbookCellText(e){return e?e.p?.body?.dataStream?e.p.body.dataStream.replace(/[\r\n]+$/,""):void 0!==e.v&&null!==e.v?String(e.v):e.f??"":""}Object.defineProperty(exports,"__esModule",{value:!0}),exports.WORKBOOK_LIMITS=void 0,exports.isWorkbookDocument=isWorkbookDocument,exports.emptyWorkbook=function emptyWorkbook(e){return{type:"workbook",formatVersion:1,workbook:{id:e,name:"工作簿",appVersion:"1.1.0",locale:"zhCN",sheetOrder:["sheet1"],sheets:{sheet1:{id:"sheet1",name:"Sheet1",rowCount:1e3,columnCount:26,cellData:{}}}}}},exports.validateWorkbook=function validateWorkbook(e){const fail=e=>{throw new Error(`工作表格式无效:${e}`)};isWorkbookDocument(e)&&1===e.formatVersion&&record(e.workbook)||fail("不支持的版本");const o=e,t=JSON.stringify(o);(new TextEncoder).encode(t).byteLength>exports.WORKBOOK_LIMITS.bytes&&fail("超过 4MB");let r=0;const walk=(e,o)=>{if((++r>5e5||o>40)&&fail("结构过大或嵌套过深"),"number"!=typeof e||Number.isFinite(e)||fail(
|
|
1
|
+
"use strict";function isWorkbookDocument(e){return!!e&&"object"==typeof e&&"workbook"===e.type}function record(e){return!!e&&"object"==typeof e&&!Array.isArray(e)}function index(e,o){return/^(0|[1-9]\d*)$/.test(e)&&Number(e)<o}function workbookCellText(e){return e?e.p?.body?.dataStream?e.p.body.dataStream.replace(/[\r\n]+$/,""):void 0!==e.v&&null!==e.v?String(e.v):e.f??"":""}Object.defineProperty(exports,"__esModule",{value:!0}),exports.WORKBOOK_LIMITS=void 0,exports.isWorkbookDocument=isWorkbookDocument,exports.emptyWorkbook=function emptyWorkbook(e){return{type:"workbook",formatVersion:1,workbook:{id:e,name:"工作簿",appVersion:"1.1.0",locale:"zhCN",sheetOrder:["sheet1"],sheets:{sheet1:{id:"sheet1",name:"Sheet1",rowCount:1e3,columnCount:26,cellData:{}}}}}},exports.validateWorkbook=function validateWorkbook(e){const fail=e=>{throw new Error(`工作表格式无效:${e}`)};isWorkbookDocument(e)&&1===e.formatVersion&&record(e.workbook)||fail("不支持的版本");const o=e,t=JSON.stringify(o);(new TextEncoder).encode(t).byteLength>exports.WORKBOOK_LIMITS.bytes&&fail("超过 4MB");let r=0;const walk=(e,o,t)=>{if((++r>5e5||o>40)&&fail("结构过大或嵌套过深"),"number"!=typeof e||Number.isFinite(e)||fail(`包含非法数值(${t} = ${String(e)})`),Array.isArray(e))e.forEach((e,r)=>walk(e,o+1,`${t}[${r}]`));else if(record(e)){Object.getPrototypeOf(e)!==Object.prototype&&null!==Object.getPrototypeOf(e)&&fail("包含非法对象");for(const[r,n]of Object.entries(e))["__proto__","constructor","prototype"].includes(r)&&fail("包含非法属性"),walk(n,o+1,t?`${t}.${r}`:r)}};walk(o,0,"");const n=o.workbook;("string"!=typeof n.id||!n.id||n.id.length>128||"string"!=typeof n.name||n.name.length>256)&&fail("工作簿名称或标识无效"),Array.isArray(n.sheetOrder)&&n.sheetOrder.length&&!(n.sheetOrder.length>exports.WORKBOOK_LIMITS.sheets)&&new Set(n.sheetOrder).size===n.sheetOrder.length&&record(n.sheets)&&Object.keys(n.sheets).length===n.sheetOrder.length||fail("表页数量或顺序无效");let s=0;const i=new Set;for(const e of n.sheetOrder){("string"!=typeof e||!e||e.length>128)&&fail("表页标识无效");const o=n.sheets[e];if((!record(o)||o.id!==e||"string"!=typeof o.name||!o.name.trim()||o.name.length>31||/[\\/*?:[\]]/.test(o.name)||i.has(o.name.toLowerCase()))&&fail("表页名称无效或重复(最多 31 字符)"),i.add(o.name.toLowerCase()),(!Number.isInteger(o.rowCount)||o.rowCount<1||o.rowCount>exports.WORKBOOK_LIMITS.rows||!Number.isInteger(o.columnCount)||o.columnCount<1||o.columnCount>exports.WORKBOOK_LIMITS.columns)&&fail("行列超出上限"),void 0!==o.cellData){record(o.cellData)||fail("单元格结构无效");for(const[e,t]of Object.entries(o.cellData)){index(e,o.rowCount)&&record(t)||fail("单元格行号无效");for(const[e,r]of Object.entries(t))++s>exports.WORKBOOK_LIMITS.cells&&fail("单元格超过 50000 个"),index(e,o.columnCount)||fail("单元格列号无效"),null!==r&&(record(r)||fail("单元格结构无效"),void 0===r.v||null===r.v||["string","number","boolean"].includes(typeof r.v)||fail("单元格值无效"),"string"==typeof r.v&&r.v.length>16e3&&fail("单元格文字过长"),void 0!==r.f&&("string"!=typeof r.f||r.f.length>4e3)&&fail("公式无效"),void 0!==r.t&&(!Number.isInteger(r.t)||Number(r.t)<0||Number(r.t)>4)&&fail("单元格类型无效"),void 0===r.s||null===r.s||"string"==typeof r.s||record(r.s)||fail("单元格样式无效"),void 0===r.p||record(r.p)&&(void 0===r.p.body||record(r.p.body)&&"string"==typeof r.p.body.dataStream&&!(r.p.body.dataStream.length>16e3))||fail("单元格富文本无效"))}}for(const[e,t]of[[o.rowData,o.rowCount],[o.columnData,o.columnCount]])void 0===e||record(e)&&!Object.keys(e).some(e=>!index(e,t))||fail("行列属性越界");if(void 0!==o.mergeData){(!Array.isArray(o.mergeData)||o.mergeData.length>exports.WORKBOOK_LIMITS.cells)&&fail("合并区域无效");const e=new Set;for(const t of o.mergeData){(!record(t)||![t.startRow,t.endRow,t.startColumn,t.endColumn].every(Number.isInteger)||t.startRow<0||t.endRow<t.startRow||t.endRow>=o.rowCount||t.startColumn<0||t.endColumn<t.startColumn||t.endColumn>=o.columnCount)&&fail("合并区域越界");const r=(t.endRow-t.startRow+1)*(t.endColumn-t.startColumn+1);e.size+r>2e5&&fail("合并区域总范围过大");for(let o=t.startRow;o<=t.endRow;o++)for(let r=t.startColumn;r<=t.endColumn;r++){const t=o*exports.WORKBOOK_LIMITS.columns+r;e.has(t)&&fail("合并区域重叠"),e.add(t)}}}}return JSON.parse(t)},exports.sanitizeNonFiniteNumbers=function sanitizeNonFiniteNumbers(e){const o=[],join=(e,o)=>e?`${e}.${o}`:o,walk=(e,t)=>{if(Array.isArray(e))e.forEach((r,n)=>{"number"!=typeof r||Number.isFinite(r)?walk(r,`${t}[${n}]`):(o.push(`${t}[${n}]`),e[n]=null)});else if(record(e))for(const[r,n]of Object.entries(e))"number"!=typeof n||Number.isFinite(n)?walk(n,join(t,r)):(o.push(join(t,r)),delete e[r])};return walk(e,""),{value:e,dropped:o}},exports.workbookCellText=workbookCellText,exports.workbookText=function workbookText(e){return e.workbook.sheetOrder.map(o=>{const t=e.workbook.sheets[o];return`${t.name} ${Object.values(t.cellData??{}).flatMap(e=>Object.values(e).map(workbookCellText)).join(" ")}`}).join(" ").slice(0,2e5)},exports.WORKBOOK_LIMITS={sheets:20,rows:1e4,columns:200,cells:5e4,bytes:4194304};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export const WORKBOOK_LIMITS={sheets:20,rows:1e4,columns:200,cells:5e4,bytes:4194304};export function isWorkbookDocument(e){return!!e&&"object"==typeof e&&"workbook"===e.type}export function emptyWorkbook(e){return{type:"workbook",formatVersion:1,workbook:{id:e,name:"工作簿",appVersion:"1.1.0",locale:"zhCN",sheetOrder:["sheet1"],sheets:{sheet1:{id:"sheet1",name:"Sheet1",rowCount:1e3,columnCount:26,cellData:{}}}}}}function record(e){return!!e&&"object"==typeof e&&!Array.isArray(e)}function index(e,
|
|
1
|
+
export const WORKBOOK_LIMITS={sheets:20,rows:1e4,columns:200,cells:5e4,bytes:4194304};export function isWorkbookDocument(e){return!!e&&"object"==typeof e&&"workbook"===e.type}export function emptyWorkbook(e){return{type:"workbook",formatVersion:1,workbook:{id:e,name:"工作簿",appVersion:"1.1.0",locale:"zhCN",sheetOrder:["sheet1"],sheets:{sheet1:{id:"sheet1",name:"Sheet1",rowCount:1e3,columnCount:26,cellData:{}}}}}}function record(e){return!!e&&"object"==typeof e&&!Array.isArray(e)}function index(e,t){return/^(0|[1-9]\d*)$/.test(e)&&Number(e)<t}export function validateWorkbook(e){const fail=e=>{throw new Error(`工作表格式无效:${e}`)};isWorkbookDocument(e)&&1===e.formatVersion&&record(e.workbook)||fail("不支持的版本");const t=e,o=JSON.stringify(t);(new TextEncoder).encode(o).byteLength>WORKBOOK_LIMITS.bytes&&fail("超过 4MB");let r=0;const walk=(e,t,o)=>{if((++r>5e5||t>40)&&fail("结构过大或嵌套过深"),"number"!=typeof e||Number.isFinite(e)||fail(`包含非法数值(${o} = ${String(e)})`),Array.isArray(e))e.forEach((e,r)=>walk(e,t+1,`${o}[${r}]`));else if(record(e)){Object.getPrototypeOf(e)!==Object.prototype&&null!==Object.getPrototypeOf(e)&&fail("包含非法对象");for(const[r,n]of Object.entries(e))["__proto__","constructor","prototype"].includes(r)&&fail("包含非法属性"),walk(n,t+1,o?`${o}.${r}`:r)}};walk(t,0,"");const n=t.workbook;("string"!=typeof n.id||!n.id||n.id.length>128||"string"!=typeof n.name||n.name.length>256)&&fail("工作簿名称或标识无效"),Array.isArray(n.sheetOrder)&&n.sheetOrder.length&&!(n.sheetOrder.length>WORKBOOK_LIMITS.sheets)&&new Set(n.sheetOrder).size===n.sheetOrder.length&&record(n.sheets)&&Object.keys(n.sheets).length===n.sheetOrder.length||fail("表页数量或顺序无效");let s=0;const a=new Set;for(const e of n.sheetOrder){("string"!=typeof e||!e||e.length>128)&&fail("表页标识无效");const t=n.sheets[e];if((!record(t)||t.id!==e||"string"!=typeof t.name||!t.name.trim()||t.name.length>31||/[\\/*?:[\]]/.test(t.name)||a.has(t.name.toLowerCase()))&&fail("表页名称无效或重复(最多 31 字符)"),a.add(t.name.toLowerCase()),(!Number.isInteger(t.rowCount)||t.rowCount<1||t.rowCount>WORKBOOK_LIMITS.rows||!Number.isInteger(t.columnCount)||t.columnCount<1||t.columnCount>WORKBOOK_LIMITS.columns)&&fail("行列超出上限"),void 0!==t.cellData){record(t.cellData)||fail("单元格结构无效");for(const[e,o]of Object.entries(t.cellData)){index(e,t.rowCount)&&record(o)||fail("单元格行号无效");for(const[e,r]of Object.entries(o))++s>WORKBOOK_LIMITS.cells&&fail("单元格超过 50000 个"),index(e,t.columnCount)||fail("单元格列号无效"),null!==r&&(record(r)||fail("单元格结构无效"),void 0===r.v||null===r.v||["string","number","boolean"].includes(typeof r.v)||fail("单元格值无效"),"string"==typeof r.v&&r.v.length>16e3&&fail("单元格文字过长"),void 0!==r.f&&("string"!=typeof r.f||r.f.length>4e3)&&fail("公式无效"),void 0!==r.t&&(!Number.isInteger(r.t)||Number(r.t)<0||Number(r.t)>4)&&fail("单元格类型无效"),void 0===r.s||null===r.s||"string"==typeof r.s||record(r.s)||fail("单元格样式无效"),void 0===r.p||record(r.p)&&(void 0===r.p.body||record(r.p.body)&&"string"==typeof r.p.body.dataStream&&!(r.p.body.dataStream.length>16e3))||fail("单元格富文本无效"))}}for(const[e,o]of[[t.rowData,t.rowCount],[t.columnData,t.columnCount]])void 0===e||record(e)&&!Object.keys(e).some(e=>!index(e,o))||fail("行列属性越界");if(void 0!==t.mergeData){(!Array.isArray(t.mergeData)||t.mergeData.length>WORKBOOK_LIMITS.cells)&&fail("合并区域无效");const e=new Set;for(const o of t.mergeData){(!record(o)||![o.startRow,o.endRow,o.startColumn,o.endColumn].every(Number.isInteger)||o.startRow<0||o.endRow<o.startRow||o.endRow>=t.rowCount||o.startColumn<0||o.endColumn<o.startColumn||o.endColumn>=t.columnCount)&&fail("合并区域越界");const r=(o.endRow-o.startRow+1)*(o.endColumn-o.startColumn+1);e.size+r>2e5&&fail("合并区域总范围过大");for(let t=o.startRow;t<=o.endRow;t++)for(let r=o.startColumn;r<=o.endColumn;r++){const o=t*WORKBOOK_LIMITS.columns+r;e.has(o)&&fail("合并区域重叠"),e.add(o)}}}}return JSON.parse(o)}export function sanitizeNonFiniteNumbers(e){const t=[],join=(e,t)=>e?`${e}.${t}`:t,walk=(e,o)=>{if(Array.isArray(e))e.forEach((r,n)=>{"number"!=typeof r||Number.isFinite(r)?walk(r,`${o}[${n}]`):(t.push(`${o}[${n}]`),e[n]=null)});else if(record(e))for(const[r,n]of Object.entries(e))"number"!=typeof n||Number.isFinite(n)?walk(n,join(o,r)):(t.push(join(o,r)),delete e[r])};return walk(e,""),{value:e,dropped:t}}export function workbookCellText(e){return e?e.p?.body?.dataStream?e.p.body.dataStream.replace(/[\r\n]+$/,""):void 0!==e.v&&null!==e.v?String(e.v):e.f??"":""}export function workbookText(e){return e.workbook.sheetOrder.map(t=>{const o=e.workbook.sheets[t];return`${o.name} ${Object.values(o.cellData??{}).flatMap(e=>Object.values(e).map(workbookCellText)).join(" ")}`}).join(" ").slice(0,2e5)}
|
|
@@ -50,5 +50,9 @@ export declare const WORKBOOK_LIMITS: {
|
|
|
50
50
|
export declare function isWorkbookDocument(value: unknown): value is WorkbookDocument;
|
|
51
51
|
export declare function emptyWorkbook(id: string): WorkbookDocument;
|
|
52
52
|
export declare function validateWorkbook(raw: unknown): WorkbookDocument;
|
|
53
|
+
export declare function sanitizeNonFiniteNumbers(raw: unknown): {
|
|
54
|
+
value: unknown;
|
|
55
|
+
dropped: string[];
|
|
56
|
+
};
|
|
53
57
|
export declare function workbookCellText(cell: WorkbookCell | null | undefined): string;
|
|
54
58
|
export declare function workbookText(doc: WorkbookDocument): string;
|