@senlinz/import-export 0.1.2 → 1.0.1
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 +44 -24
- package/README.zh.md +6 -24
- package/dist/assets/imexport_wasm_bg-BUZuccZ4.wasm +0 -0
- package/dist/index.js +1 -1
- package/dist/types/ExcelDefinition.d.ts +1 -1
- package/dist/types/index.d.ts +2 -14
- package/dist/types/utils.d.ts +2 -2
- package/package.json +7 -6
- package/dist/types/runtime.d.ts +0 -20
package/README.md
CHANGED
|
@@ -10,29 +10,14 @@ High-level browser API for Excel template generation, export, and import.
|
|
|
10
10
|
pnpm add @senlinz/import-export
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
##
|
|
13
|
+
## WASM loading
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
-
|
|
18
|
-
- `importExcel`, `importExcelDynamic`, `exportExcel`, `fromExcel`, `fromExcelDynamic`, `toExcel`, `downloadExcelTemplate`, and `generateExcelTemplate` automatically initialize the bundled WASM runtime.
|
|
19
|
-
- Best for most browser applications.
|
|
20
|
-
|
|
21
|
-
### Advanced mode
|
|
22
|
-
|
|
23
|
-
- Call `initializeWasm(...)` yourself before using the Excel APIs.
|
|
24
|
-
- Use this when you want custom WASM hosting, bundler control, or to reuse bytes/modules that you loaded elsewhere.
|
|
25
|
-
- Supported manual inputs: `source`, `bytes`, or `module`.
|
|
26
|
-
- `bundledWasmSource` is also exported for self-contained demos/tests that still want explicit manual initialization.
|
|
15
|
+
- `importExcel`, `importExcelDynamic`, `exportExcel`, `fromExcel`, `fromExcelDynamic`, `toExcel`, `downloadExcelTemplate`, and `generateExcelTemplate` auto-load and initialize the bundled WASM asset on first use.
|
|
16
|
+
- In Vite and other browser ESM bundlers, no manual `initializeWasm(...)` call or `?url` import is required when using the high-level core package.
|
|
17
|
+
- If you need to manage the WASM module yourself, use `@senlinz/import-export-wasm` directly instead of this package.
|
|
27
18
|
|
|
28
19
|
```ts
|
|
29
|
-
import {
|
|
30
|
-
|
|
31
|
-
const wasmBytes = new Uint8Array(
|
|
32
|
-
await (await fetch('/assets/imexport_wasm_bg.wasm')).arrayBuffer()
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
initializeWasm({ bytes: wasmBytes });
|
|
20
|
+
import { toExcel } from '@senlinz/import-export';
|
|
36
21
|
|
|
37
22
|
const workbook = await toExcel({
|
|
38
23
|
name: 'TomAndJerry',
|
|
@@ -40,8 +25,6 @@ const workbook = await toExcel({
|
|
|
40
25
|
}, [{ name: 'Tom' }]);
|
|
41
26
|
```
|
|
42
27
|
|
|
43
|
-
If you provide invalid manual input, `initializeWasm(...)` throws a clear error immediately.
|
|
44
|
-
|
|
45
28
|
## Supported API
|
|
46
29
|
|
|
47
30
|
### Stable definition fields
|
|
@@ -165,6 +148,27 @@ const rows = await importExcel(definition);
|
|
|
165
148
|
- Grouped export objects must use `{ value?, children: [...] }` for configured `dataGroup` columns.
|
|
166
149
|
- Image columns require `imageFetcher`.
|
|
167
150
|
|
|
151
|
+
## Image fetcher with URL validation
|
|
152
|
+
|
|
153
|
+
When using `imageFetcher` for image columns, validate URLs before fetching to avoid requests to untrusted origins. The example below enforces HTTPS and checks the HTTP response status:
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
const imageFetcher = async (url: string): Promise<Uint8Array> => {
|
|
157
|
+
const parsed = new URL(url);
|
|
158
|
+
if (parsed.protocol !== 'https:') {
|
|
159
|
+
throw new Error(`Refusing to fetch image from non-HTTPS URL: ${url}`);
|
|
160
|
+
}
|
|
161
|
+
const response = await fetch(url);
|
|
162
|
+
if (!response.ok) {
|
|
163
|
+
throw new Error(`Failed to fetch image: ${response.status} ${response.statusText}`);
|
|
164
|
+
}
|
|
165
|
+
const buffer = await response.arrayBuffer();
|
|
166
|
+
return new Uint8Array(buffer);
|
|
167
|
+
};
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
For server-side contexts, also consider restricting URLs to trusted domains and rejecting private/internal network addresses. See [SECURITY.md](../../SECURITY.md) for the full image fetcher trust model and additional guidance.
|
|
171
|
+
|
|
168
172
|
## Advanced supported features
|
|
169
173
|
|
|
170
174
|
These advanced features are supported and considered part of the public API:
|
|
@@ -183,17 +187,33 @@ These advanced features are supported and considered part of the public API:
|
|
|
183
187
|
- `downloadExcelTemplate`, `exportExcel`, `importExcel`, and `importExcelDynamic` require DOM/browser APIs.
|
|
184
188
|
- `fromExcel`, `toExcel`, and `generateExcelTemplate` can be used in other runtimes when browser-compatible globals are available.
|
|
185
189
|
- `fromExcelDynamic` can also be used in other runtimes when browser-compatible globals are available.
|
|
186
|
-
-
|
|
190
|
+
- The core package initializes its emitted bundled WASM asset asynchronously before the first workbook operation.
|
|
187
191
|
|
|
188
192
|
## Examples
|
|
189
193
|
|
|
190
194
|
- [Basic browser example](./examples/basic-browser.html)
|
|
191
|
-
- [Manual WASM browser example](./examples/manual-wasm-browser.html)
|
|
192
195
|
- [Grouped export example](./examples/grouped-export.html)
|
|
193
196
|
- [Definition validation example](./examples/definition-errors.html)
|
|
194
197
|
|
|
195
198
|
These examples are covered by Playwright tests in [`./tests/import-export.spec.ts`](./tests/import-export.spec.ts).
|
|
196
199
|
|
|
200
|
+
## Test utilities
|
|
201
|
+
|
|
202
|
+
The package exposes a `testUtils` namespace for use in consumer test suites:
|
|
203
|
+
|
|
204
|
+
```ts
|
|
205
|
+
import { testUtils } from '@senlinz/import-export';
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
> **Warning:** `testUtils` is intended for testing purposes only. These helpers are internal implementation details and carry **no stability guarantee** — they may change or be removed in any release without notice.
|
|
209
|
+
|
|
210
|
+
| Helper | Description |
|
|
211
|
+
|---|---|
|
|
212
|
+
| `normalizeDefinition(definition)` | Normalizes and validates an `ExcelDefinition`, trimming strings and applying defaults. |
|
|
213
|
+
| `normalizeDynamicImportOptions(options?)` | Normalizes and validates dynamic import options (e.g. `headerRow`, `maxFileSizeBytes`). |
|
|
214
|
+
| `sanitizeTextCellValue(value, escapeFormulas?)` | Sanitizes a text cell value to prevent formula injection. Prefixes formula-like values with a single quote when `escapeFormulas` is `true` (the default). |
|
|
215
|
+
| `defaultMaxFileSizeBytes` | The default maximum file size in bytes (25 MB). |
|
|
216
|
+
|
|
197
217
|
## Known limitations
|
|
198
218
|
|
|
199
219
|
- Parent headers and data groups must be declared before dependent columns.
|
package/README.zh.md
CHANGED
|
@@ -10,29 +10,14 @@
|
|
|
10
10
|
pnpm add @senlinz/import-export
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
##
|
|
13
|
+
## WASM 加载
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
-
|
|
18
|
-
- `importExcel`、`importExcelDynamic`、`exportExcel`、`fromExcel`、`fromExcelDynamic`、`toExcel`、`downloadExcelTemplate`、`generateExcelTemplate` 会自动初始化内置 WASM 运行时。
|
|
19
|
-
- 适合绝大多数浏览器场景。
|
|
20
|
-
|
|
21
|
-
### 高级模式
|
|
22
|
-
|
|
23
|
-
- 在使用这些 Excel API 之前,先手动调用 `initializeWasm(...)`。
|
|
24
|
-
- 适用于自定义 WASM 托管、需要自己控制 bundler 行为,或希望复用已加载 bytes / module 的场景。
|
|
25
|
-
- 支持的手动输入:`source`、`bytes`、`module`。
|
|
26
|
-
- 同时也导出了 `bundledWasmSource`,方便在自包含 demo / 测试中显式走手动初始化流程。
|
|
15
|
+
- `importExcel`、`importExcelDynamic`、`exportExcel`、`fromExcel`、`fromExcelDynamic`、`toExcel`、`downloadExcelTemplate`、`generateExcelTemplate` 会在首次调用时自动加载并初始化内置 WASM 资源。
|
|
16
|
+
- 在 Vite 和其他浏览器 ESM bundler 中,使用高阶 core 包时不再需要手动调用 `initializeWasm(...)` 或自己处理 `?url`。
|
|
17
|
+
- 如果你需要自行管理 WASM 模块,请直接使用 `@senlinz/import-export-wasm`,而不是这个高阶包。
|
|
27
18
|
|
|
28
19
|
```ts
|
|
29
|
-
import {
|
|
30
|
-
|
|
31
|
-
const wasmBytes = new Uint8Array(
|
|
32
|
-
await (await fetch('/assets/imexport_wasm_bg.wasm')).arrayBuffer()
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
initializeWasm({ bytes: wasmBytes });
|
|
20
|
+
import { toExcel } from '@senlinz/import-export';
|
|
36
21
|
|
|
37
22
|
const workbook = await toExcel({
|
|
38
23
|
name: 'TomAndJerry',
|
|
@@ -40,8 +25,6 @@ const workbook = await toExcel({
|
|
|
40
25
|
}, [{ name: 'Tom' }]);
|
|
41
26
|
```
|
|
42
27
|
|
|
43
|
-
如果手动传入的 WASM 输入无效,`initializeWasm(...)` 会立即抛出清晰的错误信息。
|
|
44
|
-
|
|
45
28
|
## 支持的 API
|
|
46
29
|
|
|
47
30
|
### 稳定的 definition 字段
|
|
@@ -175,12 +158,11 @@ const result = await fromExcelDynamic(fileBytes, {
|
|
|
175
158
|
- 主要面向浏览器 ESM 运行时。
|
|
176
159
|
- `downloadExcelTemplate`、`exportExcel`、`importExcel`、`importExcelDynamic` 依赖 DOM / 浏览器 API。
|
|
177
160
|
- 当运行时提供兼容的浏览器全局对象时,也可以使用 `fromExcel`、`fromExcelDynamic`、`toExcel`、`generateExcelTemplate`。
|
|
178
|
-
-
|
|
161
|
+
- core 包会在首次执行工作簿操作前,异步初始化构建产物中的内置 WASM 资源。
|
|
179
162
|
|
|
180
163
|
## 示例
|
|
181
164
|
|
|
182
165
|
- [基础浏览器示例](./examples/basic-browser.html)
|
|
183
|
-
- [手动初始化 WASM 的浏览器示例](./examples/manual-wasm-browser.html)
|
|
184
166
|
- [分组导出示例](./examples/grouped-export.html)
|
|
185
167
|
- [Definition 校验示例](./examples/definition-errors.html)
|
|
186
168
|
|
|
Binary file
|