@eagl-finance/celamine-node 0.1.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.
- package/README.md +55 -0
- package/package.json +31 -0
- package/pkg/calamine_node.d.ts +4 -0
- package/pkg/calamine_node.js +166 -0
- package/pkg/calamine_node_bg.wasm +0 -0
- package/pkg/calamine_node_bg.wasm.d.ts +9 -0
package/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# @eagl/celamine-node
|
|
2
|
+
|
|
3
|
+
Lightweight WASM-based spreadsheet parser for Node.js, powered by [calamine](https://github.com/tauri-apps/calamine).
|
|
4
|
+
|
|
5
|
+
Parses **XLS** (binary BIFF), **XLSX**, **XLSB**, and **ODS** files — returns all sheets as `{ name, rows }[]`.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @eagl/celamine-node
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { getAllSheets } from "@eagl/celamine-node";
|
|
17
|
+
import { readFileSync } from "fs";
|
|
18
|
+
|
|
19
|
+
const buffer = readFileSync("spreadsheet.xlsx");
|
|
20
|
+
const sheets = getAllSheets(new Uint8Array(buffer));
|
|
21
|
+
|
|
22
|
+
for (const sheet of sheets) {
|
|
23
|
+
console.log(sheet.name, sheet.rows.length, "rows");
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## API
|
|
28
|
+
|
|
29
|
+
### `getAllSheets(data: Uint8Array): SheetData[]`
|
|
30
|
+
|
|
31
|
+
Returns all sheets from a workbook buffer.
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
type SheetData = {
|
|
35
|
+
name: string;
|
|
36
|
+
rows: (string | null)[][];
|
|
37
|
+
};
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Building from source
|
|
41
|
+
|
|
42
|
+
Requires [wasm-pack](https://rustwasm.github.io/wasm-pack/installer/) and Rust.
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
npm run build
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Supported formats
|
|
49
|
+
|
|
50
|
+
| Format | Extension | Magic bytes |
|
|
51
|
+
|--------|-----------|-------------|
|
|
52
|
+
| XLS (BIFF) | .xls | `D0 CF 11 E0` |
|
|
53
|
+
| XLSX | .xlsx | `50 4B 03 04` (ZIP) |
|
|
54
|
+
| XLSB | .xlsb | `50 4B 03 04` (ZIP) |
|
|
55
|
+
| ODS | .ods | `50 4B 03 04` (ZIP) |
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@eagl-finance/celamine-node",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Lightweight WASM-based spreadsheet parser (XLS, XLSX, ODS) powered by calamine",
|
|
5
|
+
"main": "pkg/calamine_node.js",
|
|
6
|
+
"types": "pkg/calamine_node.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"pkg/calamine_node.js",
|
|
9
|
+
"pkg/calamine_node.d.ts",
|
|
10
|
+
"pkg/calamine_node_bg.wasm",
|
|
11
|
+
"pkg/calamine_node_bg.wasm.d.ts"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "wasm-pack build --target nodejs --out-dir pkg",
|
|
15
|
+
"prepublishOnly": "npm run build"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/eagl-ai/celamine-node"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"keywords": [
|
|
23
|
+
"excel",
|
|
24
|
+
"xls",
|
|
25
|
+
"xlsx",
|
|
26
|
+
"ods",
|
|
27
|
+
"spreadsheet",
|
|
28
|
+
"calamine",
|
|
29
|
+
"wasm"
|
|
30
|
+
]
|
|
31
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/* @ts-self-types="./calamine_node.d.ts" */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {Uint8Array} data
|
|
5
|
+
* @returns {any}
|
|
6
|
+
*/
|
|
7
|
+
function getAllSheets(data) {
|
|
8
|
+
const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
|
|
9
|
+
const len0 = WASM_VECTOR_LEN;
|
|
10
|
+
const ret = wasm.getAllSheets(ptr0, len0);
|
|
11
|
+
if (ret[2]) {
|
|
12
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
13
|
+
}
|
|
14
|
+
return takeFromExternrefTable0(ret[0]);
|
|
15
|
+
}
|
|
16
|
+
exports.getAllSheets = getAllSheets;
|
|
17
|
+
|
|
18
|
+
function __wbg_get_imports() {
|
|
19
|
+
const import0 = {
|
|
20
|
+
__proto__: null,
|
|
21
|
+
__wbg_Error_4577686b3a6d9b3a: function(arg0, arg1) {
|
|
22
|
+
const ret = Error(getStringFromWasm0(arg0, arg1));
|
|
23
|
+
return ret;
|
|
24
|
+
},
|
|
25
|
+
__wbg_String_8564e559799eccda: function(arg0, arg1) {
|
|
26
|
+
const ret = String(arg1);
|
|
27
|
+
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
28
|
+
const len1 = WASM_VECTOR_LEN;
|
|
29
|
+
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
|
|
30
|
+
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
|
|
31
|
+
},
|
|
32
|
+
__wbg___wbindgen_throw_39bc967c0e5a9b58: function(arg0, arg1) {
|
|
33
|
+
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
34
|
+
},
|
|
35
|
+
__wbg_new_cbee8c0d5c479eac: function() {
|
|
36
|
+
const ret = new Array();
|
|
37
|
+
return ret;
|
|
38
|
+
},
|
|
39
|
+
__wbg_new_ed69e637b553a997: function() {
|
|
40
|
+
const ret = new Object();
|
|
41
|
+
return ret;
|
|
42
|
+
},
|
|
43
|
+
__wbg_set_4c81cfb5dc3a333c: function(arg0, arg1, arg2) {
|
|
44
|
+
arg0[arg1 >>> 0] = arg2;
|
|
45
|
+
},
|
|
46
|
+
__wbg_set_6be42768c690e380: function(arg0, arg1, arg2) {
|
|
47
|
+
arg0[arg1] = arg2;
|
|
48
|
+
},
|
|
49
|
+
__wbindgen_cast_0000000000000001: function(arg0, arg1) {
|
|
50
|
+
// Cast intrinsic for `Ref(String) -> Externref`.
|
|
51
|
+
const ret = getStringFromWasm0(arg0, arg1);
|
|
52
|
+
return ret;
|
|
53
|
+
},
|
|
54
|
+
__wbindgen_init_externref_table: function() {
|
|
55
|
+
const table = wasm.__wbindgen_externrefs;
|
|
56
|
+
const offset = table.grow(4);
|
|
57
|
+
table.set(0, undefined);
|
|
58
|
+
table.set(offset + 0, undefined);
|
|
59
|
+
table.set(offset + 1, null);
|
|
60
|
+
table.set(offset + 2, true);
|
|
61
|
+
table.set(offset + 3, false);
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
return {
|
|
65
|
+
__proto__: null,
|
|
66
|
+
"./calamine_node_bg.js": import0,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
let cachedDataViewMemory0 = null;
|
|
71
|
+
function getDataViewMemory0() {
|
|
72
|
+
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
|
|
73
|
+
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
|
|
74
|
+
}
|
|
75
|
+
return cachedDataViewMemory0;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function getStringFromWasm0(ptr, len) {
|
|
79
|
+
ptr = ptr >>> 0;
|
|
80
|
+
return decodeText(ptr, len);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
let cachedUint8ArrayMemory0 = null;
|
|
84
|
+
function getUint8ArrayMemory0() {
|
|
85
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
86
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
87
|
+
}
|
|
88
|
+
return cachedUint8ArrayMemory0;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function passArray8ToWasm0(arg, malloc) {
|
|
92
|
+
const ptr = malloc(arg.length * 1, 1) >>> 0;
|
|
93
|
+
getUint8ArrayMemory0().set(arg, ptr / 1);
|
|
94
|
+
WASM_VECTOR_LEN = arg.length;
|
|
95
|
+
return ptr;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
99
|
+
if (realloc === undefined) {
|
|
100
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
101
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
102
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
103
|
+
WASM_VECTOR_LEN = buf.length;
|
|
104
|
+
return ptr;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
let len = arg.length;
|
|
108
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
109
|
+
|
|
110
|
+
const mem = getUint8ArrayMemory0();
|
|
111
|
+
|
|
112
|
+
let offset = 0;
|
|
113
|
+
|
|
114
|
+
for (; offset < len; offset++) {
|
|
115
|
+
const code = arg.charCodeAt(offset);
|
|
116
|
+
if (code > 0x7F) break;
|
|
117
|
+
mem[ptr + offset] = code;
|
|
118
|
+
}
|
|
119
|
+
if (offset !== len) {
|
|
120
|
+
if (offset !== 0) {
|
|
121
|
+
arg = arg.slice(offset);
|
|
122
|
+
}
|
|
123
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
124
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
125
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
126
|
+
|
|
127
|
+
offset += ret.written;
|
|
128
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
WASM_VECTOR_LEN = offset;
|
|
132
|
+
return ptr;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function takeFromExternrefTable0(idx) {
|
|
136
|
+
const value = wasm.__wbindgen_externrefs.get(idx);
|
|
137
|
+
wasm.__externref_table_dealloc(idx);
|
|
138
|
+
return value;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
142
|
+
cachedTextDecoder.decode();
|
|
143
|
+
function decodeText(ptr, len) {
|
|
144
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const cachedTextEncoder = new TextEncoder();
|
|
148
|
+
|
|
149
|
+
if (!('encodeInto' in cachedTextEncoder)) {
|
|
150
|
+
cachedTextEncoder.encodeInto = function (arg, view) {
|
|
151
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
152
|
+
view.set(buf);
|
|
153
|
+
return {
|
|
154
|
+
read: arg.length,
|
|
155
|
+
written: buf.length
|
|
156
|
+
};
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
let WASM_VECTOR_LEN = 0;
|
|
161
|
+
|
|
162
|
+
const wasmPath = `${__dirname}/calamine_node_bg.wasm`;
|
|
163
|
+
const wasmBytes = require('fs').readFileSync(wasmPath);
|
|
164
|
+
const wasmModule = new WebAssembly.Module(wasmBytes);
|
|
165
|
+
let wasm = new WebAssembly.Instance(wasmModule, __wbg_get_imports()).exports;
|
|
166
|
+
wasm.__wbindgen_start();
|
|
Binary file
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
export const memory: WebAssembly.Memory;
|
|
4
|
+
export const getAllSheets: (a: number, b: number) => [number, number, number];
|
|
5
|
+
export const __wbindgen_malloc: (a: number, b: number) => number;
|
|
6
|
+
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
7
|
+
export const __wbindgen_externrefs: WebAssembly.Table;
|
|
8
|
+
export const __externref_table_dealloc: (a: number) => void;
|
|
9
|
+
export const __wbindgen_start: () => void;
|