jtcsv 3.0.0 → 3.1.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 +205 -146
- package/bin/jtcsv.ts +280 -202
- package/browser.d.ts +142 -0
- package/dist/benchmark.js +446 -0
- package/dist/benchmark.js.map +1 -0
- package/dist/bin/jtcsv.js +1940 -0
- package/dist/bin/jtcsv.js.map +1 -0
- package/dist/csv-to-json.js +1261 -0
- package/dist/csv-to-json.js.map +1 -0
- package/dist/errors.js +291 -0
- package/dist/errors.js.map +1 -0
- package/dist/eslint.config.js +147 -0
- package/dist/eslint.config.js.map +1 -0
- package/dist/index-core.js +95 -0
- package/dist/index-core.js.map +1 -0
- package/dist/index.js +93 -0
- package/dist/index.js.map +1 -0
- package/dist/json-save.js +229 -0
- package/dist/json-save.js.map +1 -0
- package/dist/json-to-csv.js +576 -0
- package/dist/json-to-csv.js.map +1 -0
- package/dist/jtcsv-core.cjs.js +336 -7
- package/dist/jtcsv-core.cjs.js.map +1 -1
- package/dist/jtcsv-core.esm.js +336 -7
- package/dist/jtcsv-core.esm.js.map +1 -1
- package/dist/jtcsv-core.umd.js +336 -7
- package/dist/jtcsv-core.umd.js.map +1 -1
- package/dist/jtcsv-full.cjs.js +336 -7
- package/dist/jtcsv-full.cjs.js.map +1 -1
- package/dist/jtcsv-full.esm.js +336 -7
- package/dist/jtcsv-full.esm.js.map +1 -1
- package/dist/jtcsv-full.umd.js +336 -7
- package/dist/jtcsv-full.umd.js.map +1 -1
- package/dist/jtcsv-workers.esm.js +9 -0
- package/dist/jtcsv-workers.esm.js.map +1 -1
- package/dist/jtcsv-workers.umd.js +9 -0
- package/dist/jtcsv-workers.umd.js.map +1 -1
- package/dist/jtcsv.cjs.js +1998 -2092
- package/dist/jtcsv.cjs.js.map +1 -1
- package/dist/jtcsv.esm.js +1994 -2092
- package/dist/jtcsv.esm.js.map +1 -1
- package/dist/jtcsv.umd.js +2157 -2251
- package/dist/jtcsv.umd.js.map +1 -1
- package/dist/plugins/express-middleware/index.js +350 -0
- package/dist/plugins/express-middleware/index.js.map +1 -0
- package/dist/plugins/fastify-plugin/index.js +315 -0
- package/dist/plugins/fastify-plugin/index.js.map +1 -0
- package/dist/plugins/hono/index.js +111 -0
- package/dist/plugins/hono/index.js.map +1 -0
- package/dist/plugins/nestjs/index.js +192 -0
- package/dist/plugins/nestjs/index.js.map +1 -0
- package/dist/plugins/nuxt/index.js +53 -0
- package/dist/plugins/nuxt/index.js.map +1 -0
- package/dist/plugins/remix/index.js +133 -0
- package/dist/plugins/remix/index.js.map +1 -0
- package/dist/plugins/sveltekit/index.js +155 -0
- package/dist/plugins/sveltekit/index.js.map +1 -0
- package/dist/plugins/trpc/index.js +136 -0
- package/dist/plugins/trpc/index.js.map +1 -0
- package/dist/run-demo.js +49 -0
- package/dist/run-demo.js.map +1 -0
- package/dist/src/browser/browser-functions.js +193 -0
- package/dist/src/browser/browser-functions.js.map +1 -0
- package/dist/src/browser/core.js +123 -0
- package/dist/src/browser/core.js.map +1 -0
- package/dist/src/browser/csv-to-json-browser.js +353 -0
- package/dist/src/browser/csv-to-json-browser.js.map +1 -0
- package/dist/src/browser/errors-browser.js +219 -0
- package/dist/src/browser/errors-browser.js.map +1 -0
- package/dist/src/browser/extensions/plugins.js +106 -0
- package/dist/src/browser/extensions/plugins.js.map +1 -0
- package/dist/src/browser/extensions/workers.js +66 -0
- package/dist/src/browser/extensions/workers.js.map +1 -0
- package/dist/src/browser/index.js +140 -0
- package/dist/src/browser/index.js.map +1 -0
- package/dist/src/browser/json-to-csv-browser.js +225 -0
- package/dist/src/browser/json-to-csv-browser.js.map +1 -0
- package/dist/src/browser/streams.js +340 -0
- package/dist/src/browser/streams.js.map +1 -0
- package/dist/src/browser/workers/csv-parser.worker.js +264 -0
- package/dist/src/browser/workers/csv-parser.worker.js.map +1 -0
- package/dist/src/browser/workers/worker-pool.js +338 -0
- package/dist/src/browser/workers/worker-pool.js.map +1 -0
- package/dist/src/core/delimiter-cache.js +196 -0
- package/dist/src/core/delimiter-cache.js.map +1 -0
- package/dist/src/core/node-optimizations.js +279 -0
- package/dist/src/core/node-optimizations.js.map +1 -0
- package/dist/src/core/plugin-system.js +399 -0
- package/dist/src/core/plugin-system.js.map +1 -0
- package/dist/src/core/transform-hooks.js +348 -0
- package/dist/src/core/transform-hooks.js.map +1 -0
- package/dist/src/engines/fast-path-engine-new.js +262 -0
- package/dist/src/engines/fast-path-engine-new.js.map +1 -0
- package/dist/src/engines/fast-path-engine.js +671 -0
- package/dist/src/engines/fast-path-engine.js.map +1 -0
- package/dist/src/errors.js +18 -0
- package/dist/src/errors.js.map +1 -0
- package/dist/src/formats/ndjson-parser.js +332 -0
- package/dist/src/formats/ndjson-parser.js.map +1 -0
- package/dist/src/formats/tsv-parser.js +230 -0
- package/dist/src/formats/tsv-parser.js.map +1 -0
- package/dist/src/index-with-plugins.js +259 -0
- package/dist/src/index-with-plugins.js.map +1 -0
- package/dist/src/types/index.js +3 -0
- package/dist/src/types/index.js.map +1 -0
- package/dist/src/utils/bom-utils.js +267 -0
- package/dist/src/utils/bom-utils.js.map +1 -0
- package/dist/src/utils/encoding-support.js +77 -0
- package/dist/src/utils/encoding-support.js.map +1 -0
- package/dist/src/utils/schema-validator.js +609 -0
- package/dist/src/utils/schema-validator.js.map +1 -0
- package/dist/src/utils/transform-loader.js +281 -0
- package/dist/src/utils/transform-loader.js.map +1 -0
- package/dist/src/utils/validators.js +40 -0
- package/dist/src/utils/validators.js.map +1 -0
- package/dist/src/utils/zod-adapter.js +144 -0
- package/dist/src/utils/zod-adapter.js.map +1 -0
- package/{src → dist/src}/web-server/index.js +251 -286
- package/dist/src/web-server/index.js.map +1 -0
- package/dist/src/workers/csv-multithreaded.js +211 -0
- package/dist/src/workers/csv-multithreaded.js.map +1 -0
- package/dist/src/workers/csv-parser.worker.js +179 -0
- package/dist/src/workers/csv-parser.worker.js.map +1 -0
- package/dist/src/workers/worker-pool.js +228 -0
- package/dist/src/workers/worker-pool.js.map +1 -0
- package/dist/stream-csv-to-json.js +664 -0
- package/dist/stream-csv-to-json.js.map +1 -0
- package/dist/stream-json-to-csv.js +389 -0
- package/dist/stream-json-to-csv.js.map +1 -0
- package/examples/advanced/conditional-transformations.ts +2 -2
- package/examples/advanced/performance-optimization.ts +2 -2
- package/examples/cli-advanced-usage.md +2 -0
- package/examples/cli-tool.ts +1 -1
- package/examples/large-dataset-example.ts +2 -2
- package/examples/simple-usage.ts +2 -2
- package/examples/streaming-example.ts +1 -1
- package/index.d.ts +186 -15
- package/package.json +243 -305
- package/plugins.d.ts +37 -0
- package/schema.d.ts +103 -0
- package/src/browser/csv-to-json-browser.ts +233 -3
- package/src/browser/errors-browser.ts +45 -28
- package/src/browser/json-to-csv-browser.ts +81 -5
- package/src/browser/streams.ts +73 -6
- package/src/core/delimiter-cache.ts +21 -11
- package/src/core/plugin-system.ts +343 -155
- package/src/core/transform-hooks.ts +20 -12
- package/src/engines/fast-path-engine.ts +48 -32
- package/src/errors.ts +1 -72
- package/src/formats/ndjson-parser.ts +6 -0
- package/src/formats/tsv-parser.ts +6 -0
- package/src/types/index.ts +21 -1
- package/src/utils/validators.ts +35 -0
- package/src/web-server/index.ts +1 -1
- package/bin/jtcsv.js +0 -2532
- package/csv-to-json.js +0 -711
- package/errors.js +0 -394
- package/examples/advanced/conditional-transformations.js +0 -446
- package/examples/advanced/csv-parser.worker.js +0 -89
- package/examples/advanced/nested-objects-example.js +0 -306
- package/examples/advanced/performance-optimization.js +0 -504
- package/examples/advanced/run-demo-server.js +0 -116
- package/examples/cli-batch-processing.js +0 -38
- package/examples/cli-tool.js +0 -183
- package/examples/error-handling.js +0 -338
- package/examples/express-api.js +0 -164
- package/examples/large-dataset-example.js +0 -182
- package/examples/ndjson-processing.js +0 -434
- package/examples/plugin-excel-exporter.js +0 -406
- package/examples/schema-validation.js +0 -640
- package/examples/simple-usage.js +0 -282
- package/examples/streaming-example.js +0 -418
- package/examples/web-workers-advanced.js +0 -28
- package/index.js +0 -82
- package/json-save.js +0 -255
- package/json-to-csv.js +0 -668
- package/plugins/README.md +0 -91
- package/plugins/express-middleware/README.md +0 -83
- package/plugins/express-middleware/example.js +0 -135
- package/plugins/express-middleware/example.ts +0 -135
- package/plugins/express-middleware/index.d.ts +0 -114
- package/plugins/express-middleware/index.js +0 -512
- package/plugins/express-middleware/index.ts +0 -557
- package/plugins/express-middleware/package.json +0 -52
- package/plugins/fastify-plugin/index.js +0 -404
- package/plugins/fastify-plugin/index.ts +0 -443
- package/plugins/fastify-plugin/package.json +0 -55
- package/plugins/hono/README.md +0 -28
- package/plugins/hono/index.d.ts +0 -12
- package/plugins/hono/index.js +0 -36
- package/plugins/hono/index.ts +0 -226
- package/plugins/hono/package.json +0 -35
- package/plugins/nestjs/README.md +0 -35
- package/plugins/nestjs/index.d.ts +0 -25
- package/plugins/nestjs/index.js +0 -77
- package/plugins/nestjs/index.ts +0 -201
- package/plugins/nestjs/package.json +0 -37
- package/plugins/nextjs-api/README.md +0 -57
- package/plugins/nextjs-api/examples/ConverterComponent.jsx +0 -386
- package/plugins/nextjs-api/examples/ConverterComponent.tsx +0 -386
- package/plugins/nextjs-api/examples/api-convert.js +0 -67
- package/plugins/nextjs-api/examples/api-convert.ts +0 -67
- package/plugins/nextjs-api/index.js +0 -387
- package/plugins/nextjs-api/index.tsx +0 -339
- package/plugins/nextjs-api/package.json +0 -63
- package/plugins/nextjs-api/route.js +0 -370
- package/plugins/nextjs-api/route.ts +0 -370
- package/plugins/nuxt/README.md +0 -24
- package/plugins/nuxt/index.js +0 -21
- package/plugins/nuxt/index.ts +0 -94
- package/plugins/nuxt/package.json +0 -35
- package/plugins/nuxt/runtime/composables/useJtcsv.js +0 -6
- package/plugins/nuxt/runtime/composables/useJtcsv.ts +0 -100
- package/plugins/nuxt/runtime/plugin.js +0 -6
- package/plugins/nuxt/runtime/plugin.ts +0 -71
- package/plugins/remix/README.md +0 -26
- package/plugins/remix/index.d.ts +0 -16
- package/plugins/remix/index.js +0 -62
- package/plugins/remix/index.ts +0 -260
- package/plugins/remix/package.json +0 -35
- package/plugins/sveltekit/README.md +0 -28
- package/plugins/sveltekit/index.d.ts +0 -17
- package/plugins/sveltekit/index.js +0 -54
- package/plugins/sveltekit/index.ts +0 -301
- package/plugins/sveltekit/package.json +0 -33
- package/plugins/trpc/README.md +0 -25
- package/plugins/trpc/index.d.ts +0 -7
- package/plugins/trpc/index.js +0 -32
- package/plugins/trpc/index.ts +0 -267
- package/plugins/trpc/package.json +0 -34
- package/src/browser/browser-functions.js +0 -219
- package/src/browser/core.js +0 -92
- package/src/browser/csv-to-json-browser.js +0 -722
- package/src/browser/errors-browser.js +0 -212
- package/src/browser/extensions/plugins.js +0 -92
- package/src/browser/extensions/workers.js +0 -39
- package/src/browser/index.js +0 -113
- package/src/browser/json-to-csv-browser.js +0 -319
- package/src/browser/streams.js +0 -403
- package/src/browser/workers/csv-parser.worker.js +0 -377
- package/src/browser/workers/worker-pool.js +0 -527
- package/src/core/delimiter-cache.js +0 -200
- package/src/core/node-optimizations.js +0 -408
- package/src/core/plugin-system.js +0 -494
- package/src/core/transform-hooks.js +0 -350
- package/src/engines/fast-path-engine-new.js +0 -338
- package/src/engines/fast-path-engine.js +0 -844
- package/src/errors.js +0 -26
- package/src/formats/ndjson-parser.js +0 -467
- package/src/formats/tsv-parser.js +0 -339
- package/src/index-with-plugins.js +0 -378
- package/src/utils/bom-utils.js +0 -259
- package/src/utils/encoding-support.js +0 -124
- package/src/utils/schema-validator.js +0 -594
- package/src/utils/transform-loader.js +0 -205
- package/src/utils/zod-adapter.js +0 -170
- package/stream-csv-to-json.js +0 -560
- package/stream-json-to-csv.js +0 -465
package/plugins/README.md
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
# JTCSV framework integrations
|
|
2
|
-
|
|
3
|
-
This folder contains optional adapters published as separate packages. Each adapter depends on `jtcsv`.
|
|
4
|
-
|
|
5
|
-
## Available packages
|
|
6
|
-
- @jtcsv/express-middleware
|
|
7
|
-
- @jtcsv/fastify
|
|
8
|
-
- @jtcsv/nextjs
|
|
9
|
-
- @jtcsv/nestjs
|
|
10
|
-
- @jtcsv/remix
|
|
11
|
-
- @jtcsv/nuxt
|
|
12
|
-
- @jtcsv/sveltekit
|
|
13
|
-
- @jtcsv/hono
|
|
14
|
-
- @jtcsv/trpc
|
|
15
|
-
|
|
16
|
-
## Main package shortcuts
|
|
17
|
-
If you already depend on `jtcsv`, the adapters are also exported from the main package:
|
|
18
|
-
- jtcsv/express
|
|
19
|
-
- jtcsv/fastify
|
|
20
|
-
- jtcsv/nextjs
|
|
21
|
-
- jtcsv/nextjs/route
|
|
22
|
-
- jtcsv/nestjs
|
|
23
|
-
- jtcsv/remix
|
|
24
|
-
- jtcsv/nuxt
|
|
25
|
-
- jtcsv/sveltekit
|
|
26
|
-
- jtcsv/hono
|
|
27
|
-
- jtcsv/trpc
|
|
28
|
-
|
|
29
|
-
## Express
|
|
30
|
-
```bash
|
|
31
|
-
npm install @jtcsv/express-middleware express jtcsv
|
|
32
|
-
```
|
|
33
|
-
```javascript
|
|
34
|
-
const express = require('express');
|
|
35
|
-
const { middleware } = require('@jtcsv/express-middleware');
|
|
36
|
-
|
|
37
|
-
const app = express();
|
|
38
|
-
app.use(express.json());
|
|
39
|
-
app.use(express.text({ type: 'text/csv' }));
|
|
40
|
-
app.use(middleware());
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
## Fastify
|
|
44
|
-
```bash
|
|
45
|
-
npm install @jtcsv/fastify fastify fastify-plugin jtcsv
|
|
46
|
-
```
|
|
47
|
-
```javascript
|
|
48
|
-
const fastify = require('fastify')();
|
|
49
|
-
await fastify.register(require('@jtcsv/fastify'), { prefix: '/api' });
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
## Next.js
|
|
53
|
-
```bash
|
|
54
|
-
npm install @jtcsv/nextjs jtcsv
|
|
55
|
-
```
|
|
56
|
-
```javascript
|
|
57
|
-
import handler from '@jtcsv/nextjs/route';
|
|
58
|
-
export default handler;
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
## NestJS
|
|
62
|
-
```bash
|
|
63
|
-
npm install @jtcsv/nestjs jtcsv
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
## Remix
|
|
67
|
-
```bash
|
|
68
|
-
npm install @jtcsv/remix jtcsv
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
## Nuxt
|
|
72
|
-
```bash
|
|
73
|
-
npm install @jtcsv/nuxt jtcsv
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
## SvelteKit
|
|
77
|
-
```bash
|
|
78
|
-
npm install @jtcsv/sveltekit jtcsv
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
## Hono
|
|
82
|
-
```bash
|
|
83
|
-
npm install @jtcsv/hono jtcsv
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
## tRPC
|
|
87
|
-
```bash
|
|
88
|
-
npm install @jtcsv/trpc jtcsv
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
See each package README in this folder for API details.
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
# @jtcsv/express-middleware
|
|
2
|
-
|
|
3
|
-
Express middleware that converts CSV/JSON payloads and exposes the converted data on `req.converted`.
|
|
4
|
-
|
|
5
|
-
## Install
|
|
6
|
-
```bash
|
|
7
|
-
npm install @jtcsv/express-middleware express jtcsv
|
|
8
|
-
```
|
|
9
|
-
|
|
10
|
-
## Quick start
|
|
11
|
-
```javascript
|
|
12
|
-
const express = require('express');
|
|
13
|
-
const { middleware } = require('@jtcsv/express-middleware');
|
|
14
|
-
|
|
15
|
-
const app = express();
|
|
16
|
-
app.use(express.json());
|
|
17
|
-
app.use(express.text({ type: 'text/csv' }));
|
|
18
|
-
app.use(middleware());
|
|
19
|
-
|
|
20
|
-
app.post('/api/convert', (req, res) => {
|
|
21
|
-
res.json(req.converted);
|
|
22
|
-
});
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
## Options
|
|
26
|
-
The middleware detects input/output format based on `Content-Type`, `Accept`, and `?format=csv`.
|
|
27
|
-
|
|
28
|
-
```javascript
|
|
29
|
-
app.use(middleware({
|
|
30
|
-
autoDetect: true,
|
|
31
|
-
delimiter: ',',
|
|
32
|
-
enableFastPath: true,
|
|
33
|
-
preventCsvInjection: true,
|
|
34
|
-
rfc4180Compliant: true,
|
|
35
|
-
// Security & DoS protection
|
|
36
|
-
maxFileSize: '500MB', // Maximum allowed file size (default: 500MB)
|
|
37
|
-
maxFieldSize: 1024 * 1024, // Maximum field size in bytes (default: 1MB)
|
|
38
|
-
timeout: 300000, // Processing timeout in milliseconds (default: 5 minutes)
|
|
39
|
-
// Conversion options
|
|
40
|
-
conversionOptions: {
|
|
41
|
-
parseNumbers: true,
|
|
42
|
-
parseBooleans: true
|
|
43
|
-
}
|
|
44
|
-
}));
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
**Security notes**:
|
|
48
|
-
- `maxFileSize` rejects requests with `Content-Length` exceeding the limit (HTTP 413).
|
|
49
|
-
- `maxFieldSize` limits individual field size during parsing (helps prevent memory exhaustion).
|
|
50
|
-
- `timeout` cancels long-running conversions and returns HTTP 503.
|
|
51
|
-
- For additional protection, combine with `express-rate-limit` and body parser limits.
|
|
52
|
-
|
|
53
|
-
**Example with rate limiting**:
|
|
54
|
-
```javascript
|
|
55
|
-
const rateLimit = require('express-rate-limit');
|
|
56
|
-
const importLimiter = rateLimit({
|
|
57
|
-
windowMs: 15 * 60 * 1000, // 15 minutes
|
|
58
|
-
max: 10 // limit each IP to 10 requests per window
|
|
59
|
-
});
|
|
60
|
-
app.post('/api/import', importLimiter, middleware());
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
## Helpers
|
|
64
|
-
```javascript
|
|
65
|
-
const {
|
|
66
|
-
csvToJsonRoute,
|
|
67
|
-
jsonToCsvRoute,
|
|
68
|
-
uploadCsvRoute,
|
|
69
|
-
healthCheck
|
|
70
|
-
} = require('@jtcsv/express-middleware');
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
## req.converted shape
|
|
74
|
-
```json
|
|
75
|
-
{
|
|
76
|
-
"data": "...",
|
|
77
|
-
"format": "json",
|
|
78
|
-
"inputFormat": "csv",
|
|
79
|
-
"outputFormat": "json",
|
|
80
|
-
"stats": { "inputSize": 0, "outputSize": 0, "processingTime": 0, "conversion": "csv->json" },
|
|
81
|
-
"options": {}
|
|
82
|
-
}
|
|
83
|
-
```
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
/**
|
|
3
|
-
* Пример использования Express middleware для JTCSV
|
|
4
|
-
*
|
|
5
|
-
* Запуск: node example.js
|
|
6
|
-
* Затем отправьте запросы:
|
|
7
|
-
* - POST /api/convert с JSON телом → получите CSV
|
|
8
|
-
* - POST /api/convert с CSV телом → получите JSON
|
|
9
|
-
* - POST /api/csv-to-json → конвертация CSV в JSON
|
|
10
|
-
* - POST /api/json-to-csv → конвертация JSON в CSV
|
|
11
|
-
* - GET /api/health → проверка состояния
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
const express = require('express');
|
|
15
|
-
const bodyParser = require('body-parser');
|
|
16
|
-
const {
|
|
17
|
-
middleware,
|
|
18
|
-
csvToJsonRoute,
|
|
19
|
-
jsonToCsvRoute,
|
|
20
|
-
healthCheck
|
|
21
|
-
} = require('./index');
|
|
22
|
-
|
|
23
|
-
const app = express();
|
|
24
|
-
const PORT = process.env.PORT || 3000;
|
|
25
|
-
|
|
26
|
-
// Middleware для парсинга JSON и текста
|
|
27
|
-
app.use(bodyParser.json());
|
|
28
|
-
app.use(bodyParser.text({ type: 'text/csv' }));
|
|
29
|
-
|
|
30
|
-
// Добавляем время начала обработки запроса
|
|
31
|
-
app.use((req, res, next) => {
|
|
32
|
-
req.startTime = Date.now();
|
|
33
|
-
next();
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
// Основное middleware для автоматической конвертации
|
|
37
|
-
app.use(middleware({
|
|
38
|
-
maxSize: '50mb',
|
|
39
|
-
delimiter: ',',
|
|
40
|
-
enableFastPath: true,
|
|
41
|
-
preventCsvInjection: true
|
|
42
|
-
}));
|
|
43
|
-
|
|
44
|
-
// Роуты для конкретных операций
|
|
45
|
-
app.post('/api/csv-to-json', csvToJsonRoute({
|
|
46
|
-
delimiter: ',',
|
|
47
|
-
parseNumbers: true,
|
|
48
|
-
parseBooleans: true
|
|
49
|
-
}));
|
|
50
|
-
|
|
51
|
-
app.post('/api/json-to-csv', jsonToCsvRoute({
|
|
52
|
-
delimiter: ',',
|
|
53
|
-
includeHeaders: true,
|
|
54
|
-
preventCsvInjection: true
|
|
55
|
-
}));
|
|
56
|
-
|
|
57
|
-
// Health check
|
|
58
|
-
app.get('/api/health', healthCheck());
|
|
59
|
-
|
|
60
|
-
// Пример роута, использующего автоматическую конвертацию
|
|
61
|
-
app.post('/api/convert', (req, res) => {
|
|
62
|
-
if (!req.converted) {
|
|
63
|
-
return res.status(400).json({
|
|
64
|
-
success: false,
|
|
65
|
-
error: 'No data to convert'
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
res.json({
|
|
70
|
-
success: true,
|
|
71
|
-
conversion: req.converted.conversion,
|
|
72
|
-
data: req.converted.data,
|
|
73
|
-
stats: {
|
|
74
|
-
...req.converted.stats,
|
|
75
|
-
totalTime: Date.now() - req.startTime
|
|
76
|
-
},
|
|
77
|
-
format: req.converted.outputFormat
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
// Пример роута для скачивания CSV
|
|
82
|
-
app.post('/api/download-csv', (req, res) => {
|
|
83
|
-
if (!req.converted || req.converted.outputFormat !== 'csv') {
|
|
84
|
-
return res.status(400).json({
|
|
85
|
-
success: false,
|
|
86
|
-
error: 'CSV data not available'
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
res.setHeader('Content-Type', 'text/csv');
|
|
91
|
-
res.setHeader('Content-Disposition', 'attachment; filename="converted.csv"');
|
|
92
|
-
res.send(req.converted.data);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
// Обработка ошибок
|
|
96
|
-
app.use((err, req, res, next) => {
|
|
97
|
-
console.error('Server error:', err);
|
|
98
|
-
res.status(500).json({
|
|
99
|
-
success: false,
|
|
100
|
-
error: 'Internal server error',
|
|
101
|
-
message: process.env.NODE_ENV === 'development' ? err.message : undefined
|
|
102
|
-
});
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
// 404 handler
|
|
106
|
-
app.use((req, res) => {
|
|
107
|
-
res.status(404).json({
|
|
108
|
-
success: false,
|
|
109
|
-
error: 'Route not found',
|
|
110
|
-
availableRoutes: [
|
|
111
|
-
'POST /api/convert',
|
|
112
|
-
'POST /api/csv-to-json',
|
|
113
|
-
'POST /api/json-to-csv',
|
|
114
|
-
'POST /api/download-csv',
|
|
115
|
-
'GET /api/health'
|
|
116
|
-
]
|
|
117
|
-
});
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
// Запуск сервера
|
|
121
|
-
app.listen(PORT, () => {
|
|
122
|
-
console.log(`🚀 JTCSV Express server запущен на порту ${PORT}`);
|
|
123
|
-
console.log('📚 Примеры запросов:');
|
|
124
|
-
console.log(` curl -X POST http://localhost:${PORT}/api/convert \
|
|
125
|
-
-H "Content-Type: application/json" \
|
|
126
|
-
-d '[{"name":"John","age":30},{"name":"Jane","age":25}]'`);
|
|
127
|
-
console.log();
|
|
128
|
-
console.log(` curl -X POST http://localhost:${PORT}/api/convert \
|
|
129
|
-
-H "Content-Type: text/csv" \
|
|
130
|
-
-d 'name,age\nJohn,30\nJane,25'`);
|
|
131
|
-
console.log();
|
|
132
|
-
console.log(` curl -X GET http://localhost:${PORT}/api/health`);
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
module.exports = app;
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
/**
|
|
3
|
-
* Пример использования Express middleware для JTCSV
|
|
4
|
-
*
|
|
5
|
-
* Запуск: node example.js
|
|
6
|
-
* Затем отправьте запросы:
|
|
7
|
-
* - POST /api/convert с JSON телом → получите CSV
|
|
8
|
-
* - POST /api/convert с CSV телом → получите JSON
|
|
9
|
-
* - POST /api/csv-to-json → конвертация CSV в JSON
|
|
10
|
-
* - POST /api/json-to-csv → конвертация JSON в CSV
|
|
11
|
-
* - GET /api/health → проверка состояния
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import express from "express";
|
|
15
|
-
import bodyParser from "body-parser";
|
|
16
|
-
const {
|
|
17
|
-
middleware,
|
|
18
|
-
csvToJsonRoute,
|
|
19
|
-
jsonToCsvRoute,
|
|
20
|
-
healthCheck
|
|
21
|
-
} = await import("./index");
|
|
22
|
-
|
|
23
|
-
const app = express();
|
|
24
|
-
const PORT = process.env.PORT || 3000;
|
|
25
|
-
|
|
26
|
-
// Middleware для парсинга JSON и текста
|
|
27
|
-
app.use(bodyParser.json());
|
|
28
|
-
app.use(bodyParser.text({ type: 'text/csv' }));
|
|
29
|
-
|
|
30
|
-
// Добавляем время начала обработки запроса
|
|
31
|
-
app.use((req, res, next) => {
|
|
32
|
-
req.startTime = Date.now();
|
|
33
|
-
next();
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
// Основное middleware для автоматической конвертации
|
|
37
|
-
app.use(middleware({
|
|
38
|
-
maxSize: '50mb',
|
|
39
|
-
delimiter: ',',
|
|
40
|
-
enableFastPath: true,
|
|
41
|
-
preventCsvInjection: true
|
|
42
|
-
}));
|
|
43
|
-
|
|
44
|
-
// Роуты для конкретных операций
|
|
45
|
-
app.post('/api/csv-to-json', csvToJsonRoute({
|
|
46
|
-
delimiter: ',',
|
|
47
|
-
parseNumbers: true,
|
|
48
|
-
parseBooleans: true
|
|
49
|
-
}));
|
|
50
|
-
|
|
51
|
-
app.post('/api/json-to-csv', jsonToCsvRoute({
|
|
52
|
-
delimiter: ',',
|
|
53
|
-
includeHeaders: true,
|
|
54
|
-
preventCsvInjection: true
|
|
55
|
-
}));
|
|
56
|
-
|
|
57
|
-
// Health check
|
|
58
|
-
app.get('/api/health', healthCheck());
|
|
59
|
-
|
|
60
|
-
// Пример роута, использующего автоматическую конвертацию
|
|
61
|
-
app.post('/api/convert', (req, res) => {
|
|
62
|
-
if (!req.converted) {
|
|
63
|
-
return res.status(400).json({
|
|
64
|
-
success: false,
|
|
65
|
-
error: 'No data to convert'
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
res.json({
|
|
70
|
-
success: true,
|
|
71
|
-
conversion: req.converted.conversion,
|
|
72
|
-
data: req.converted.data,
|
|
73
|
-
stats: {
|
|
74
|
-
...req.converted.stats,
|
|
75
|
-
totalTime: Date.now() - req.startTime
|
|
76
|
-
},
|
|
77
|
-
format: req.converted.outputFormat
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
// Пример роута для скачивания CSV
|
|
82
|
-
app.post('/api/download-csv', (req, res) => {
|
|
83
|
-
if (!req.converted || req.converted.outputFormat !== 'csv') {
|
|
84
|
-
return res.status(400).json({
|
|
85
|
-
success: false,
|
|
86
|
-
error: 'CSV data not available'
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
res.setHeader('Content-Type', 'text/csv');
|
|
91
|
-
res.setHeader('Content-Disposition', 'attachment; filename="converted.csv"');
|
|
92
|
-
res.send(req.converted.data);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
// Обработка ошибок
|
|
96
|
-
app.use((err, req, res, next) => {
|
|
97
|
-
console.error('Server error:', err);
|
|
98
|
-
res.status(500).json({
|
|
99
|
-
success: false,
|
|
100
|
-
error: 'Internal server error',
|
|
101
|
-
message: process.env.NODE_ENV === 'development' ? err.message : undefined
|
|
102
|
-
});
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
// 404 handler
|
|
106
|
-
app.use((req, res) => {
|
|
107
|
-
res.status(404).json({
|
|
108
|
-
success: false,
|
|
109
|
-
error: 'Route not found',
|
|
110
|
-
availableRoutes: [
|
|
111
|
-
'POST /api/convert',
|
|
112
|
-
'POST /api/csv-to-json',
|
|
113
|
-
'POST /api/json-to-csv',
|
|
114
|
-
'POST /api/download-csv',
|
|
115
|
-
'GET /api/health'
|
|
116
|
-
]
|
|
117
|
-
});
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
// Запуск сервера
|
|
121
|
-
app.listen(PORT, () => {
|
|
122
|
-
console.log(`🚀 JTCSV Express server запущен на порту ${PORT}`);
|
|
123
|
-
console.log('📚 Примеры запросов:');
|
|
124
|
-
console.log(` curl -X POST http://localhost:${PORT}/api/convert \
|
|
125
|
-
-H "Content-Type: application/json" \
|
|
126
|
-
-d '[{"name":"John","age":30},{"name":"Jane","age":25}]'`);
|
|
127
|
-
console.log();
|
|
128
|
-
console.log(` curl -X POST http://localhost:${PORT}/api/convert \
|
|
129
|
-
-H "Content-Type: text/csv" \
|
|
130
|
-
-d 'name,age\nJohn,30\nJane,25'`);
|
|
131
|
-
console.log();
|
|
132
|
-
console.log(` curl -X GET http://localhost:${PORT}/api/health`);
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
export default app;
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* TypeScript definitions для JTCSV Express Middleware
|
|
3
|
-
*
|
|
4
|
-
* @version 1.0.0
|
|
5
|
-
* @date 2026-01-23
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
declare module '@jtcsv/express-middleware' {
|
|
9
|
-
import { RequestHandler } from 'express';
|
|
10
|
-
import { CsvToJsonOptions, JsonToCsvOptions } from 'jtcsv';
|
|
11
|
-
|
|
12
|
-
export interface JtcsvMiddlewareOptions {
|
|
13
|
-
/** Максимальный размер тела запроса */
|
|
14
|
-
maxSize?: string;
|
|
15
|
-
|
|
16
|
-
/** Автоматическое определение формата */
|
|
17
|
-
autoDetect?: boolean;
|
|
18
|
-
|
|
19
|
-
/** Разделитель CSV */
|
|
20
|
-
delimiter?: string;
|
|
21
|
-
|
|
22
|
-
/** Включить Fast-Path Engine */
|
|
23
|
-
enableFastPath?: boolean;
|
|
24
|
-
|
|
25
|
-
/** Защита от CSV инъекций */
|
|
26
|
-
preventCsvInjection?: boolean;
|
|
27
|
-
|
|
28
|
-
/** Соответствие RFC 4180 */
|
|
29
|
-
rfc4180Compliant?: boolean;
|
|
30
|
-
|
|
31
|
-
/** Дополнительные опции конвертации */
|
|
32
|
-
conversionOptions?: CsvToJsonOptions | JsonToCsvOptions;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export interface ConversionStats {
|
|
36
|
-
/** Размер входных данных в байтах */
|
|
37
|
-
inputSize: number;
|
|
38
|
-
|
|
39
|
-
/** Размер выходных данных в байтах */
|
|
40
|
-
outputSize: number;
|
|
41
|
-
|
|
42
|
-
/** Время обработки в миллисекундах */
|
|
43
|
-
processingTime: number;
|
|
44
|
-
|
|
45
|
-
/** Тип конвертации */
|
|
46
|
-
conversion: string;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export interface ConvertedData {
|
|
50
|
-
/** Конвертированные данные */
|
|
51
|
-
data: any;
|
|
52
|
-
|
|
53
|
-
/** Формат выходных данных */
|
|
54
|
-
format: 'json' | 'csv';
|
|
55
|
-
|
|
56
|
-
/** Формат входных данных */
|
|
57
|
-
inputFormat: 'json' | 'csv' | 'unknown';
|
|
58
|
-
|
|
59
|
-
/** Формат выходных данных */
|
|
60
|
-
outputFormat: 'json' | 'csv';
|
|
61
|
-
|
|
62
|
-
/** Статистика конвертации */
|
|
63
|
-
stats: ConversionStats;
|
|
64
|
-
|
|
65
|
-
/** Использованные опции */
|
|
66
|
-
options: CsvToJsonOptions | JsonToCsvOptions;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// Расширяем интерфейс Request Express
|
|
70
|
-
declare global {
|
|
71
|
-
namespace Express {
|
|
72
|
-
interface Request {
|
|
73
|
-
/** Конвертированные данные */
|
|
74
|
-
converted?: ConvertedData;
|
|
75
|
-
|
|
76
|
-
/** Время начала обработки запроса */
|
|
77
|
-
startTime?: number;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Express middleware для автоматической конвертации CSV/JSON
|
|
84
|
-
*/
|
|
85
|
-
export function middleware(options?: JtcsvMiddlewareOptions): RequestHandler;
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Express route для конвертации CSV в JSON
|
|
89
|
-
*/
|
|
90
|
-
export function csvToJsonRoute(options?: CsvToJsonOptions): RequestHandler;
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Express route для конвертации JSON в CSV
|
|
94
|
-
*/
|
|
95
|
-
export function jsonToCsvRoute(options?: JsonToCsvOptions): RequestHandler;
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Express route для загрузки CSV файла
|
|
99
|
-
*/
|
|
100
|
-
export function uploadCsvRoute(options?: CsvToJsonOptions): RequestHandler;
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Health check endpoint
|
|
104
|
-
*/
|
|
105
|
-
export function healthCheck(): RequestHandler;
|
|
106
|
-
|
|
107
|
-
// Aliases
|
|
108
|
-
export const jtcsvMiddleware: typeof middleware;
|
|
109
|
-
export const createMiddleware: typeof middleware;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export {};
|
|
113
|
-
|
|
114
|
-
|