jtcsv 2.2.8 → 3.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 +204 -115
- package/bin/jtcsv.ts +2612 -0
- 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 +1262 -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 +1736 -0
- package/dist/jtcsv-core.cjs.js.map +1 -0
- package/dist/jtcsv-core.esm.js +1708 -0
- package/dist/jtcsv-core.esm.js.map +1 -0
- package/dist/jtcsv-core.umd.js +1742 -0
- package/dist/jtcsv-core.umd.js.map +1 -0
- package/dist/jtcsv-full.cjs.js +2241 -0
- package/dist/jtcsv-full.cjs.js.map +1 -0
- package/dist/jtcsv-full.esm.js +2209 -0
- package/dist/jtcsv-full.esm.js.map +1 -0
- package/dist/jtcsv-full.umd.js +2247 -0
- package/dist/jtcsv-full.umd.js.map +1 -0
- package/dist/jtcsv-workers.esm.js +768 -0
- package/dist/jtcsv-workers.esm.js.map +1 -0
- package/dist/jtcsv-workers.umd.js +782 -0
- package/dist/jtcsv-workers.umd.js.map +1 -0
- package/dist/jtcsv.cjs.js +1996 -2048
- package/dist/jtcsv.cjs.js.map +1 -1
- package/dist/jtcsv.esm.js +1992 -2048
- package/dist/jtcsv.esm.js.map +1 -1
- package/dist/jtcsv.umd.js +2157 -2209
- 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 +112 -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/dist/src/web-server/index.js +648 -0
- 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 +665 -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 +446 -0
- package/examples/advanced/csv-parser.worker.ts +89 -0
- package/examples/advanced/nested-objects-example.ts +306 -0
- package/examples/advanced/performance-optimization.ts +504 -0
- package/examples/advanced/run-demo-server.ts +116 -0
- package/examples/advanced/web-worker-usage.html +874 -0
- package/examples/async-multithreaded-example.ts +335 -0
- package/examples/cli-advanced-usage.md +290 -0
- package/examples/{cli-batch-processing.js → cli-batch-processing.ts} +38 -38
- package/examples/{cli-tool.js → cli-tool.ts} +5 -8
- package/examples/{error-handling.js → error-handling.ts} +356 -324
- package/examples/{express-api.js → express-api.ts} +161 -164
- package/examples/{large-dataset-example.js → large-dataset-example.ts} +201 -182
- package/examples/{ndjson-processing.js → ndjson-processing.ts} +456 -434
- package/examples/{plugin-excel-exporter.js → plugin-excel-exporter.ts} +6 -7
- package/examples/react-integration.tsx +637 -0
- package/examples/{schema-validation.js → schema-validation.ts} +2 -2
- package/examples/simple-usage.ts +194 -0
- package/examples/{streaming-example.js → streaming-example.ts} +12 -12
- package/index.d.ts +187 -18
- package/package.json +75 -81
- package/plugins.d.ts +37 -0
- package/schema.d.ts +103 -0
- package/src/browser/browser-functions.ts +402 -0
- package/src/browser/core.ts +152 -0
- package/src/browser/csv-to-json-browser.d.ts +3 -0
- package/src/browser/csv-to-json-browser.ts +494 -0
- package/src/browser/{errors-browser.js → errors-browser.ts} +305 -197
- package/src/browser/extensions/plugins.ts +93 -0
- package/src/browser/extensions/workers.ts +39 -0
- package/src/browser/globals.d.ts +5 -0
- package/src/browser/index.ts +192 -0
- package/src/browser/json-to-csv-browser.d.ts +3 -0
- package/src/browser/json-to-csv-browser.ts +338 -0
- package/src/browser/streams.ts +403 -0
- package/src/browser/workers/{csv-parser.worker.js → csv-parser.worker.ts} +3 -3
- package/src/browser/workers/{worker-pool.js → worker-pool.ts} +51 -30
- package/src/core/delimiter-cache.ts +320 -0
- package/src/core/{node-optimizations.js → node-optimizations.ts} +448 -407
- package/src/core/plugin-system.ts +588 -0
- package/src/core/transform-hooks.ts +566 -0
- package/src/engines/{fast-path-engine-new.js → fast-path-engine-new.ts} +11 -2
- package/src/engines/{fast-path-engine.js → fast-path-engine.ts} +79 -53
- package/src/errors.ts +1 -0
- package/src/formats/{ndjson-parser.js → ndjson-parser.ts} +24 -16
- package/src/formats/{tsv-parser.js → tsv-parser.ts} +18 -17
- package/src/{index-with-plugins.js → index-with-plugins.ts} +381 -357
- package/src/types/index.ts +275 -0
- package/src/utils/bom-utils.ts +373 -0
- package/src/utils/encoding-support.ts +155 -0
- package/src/utils/{schema-validator.js → schema-validator.ts} +814 -589
- package/src/utils/transform-loader.ts +389 -0
- package/src/utils/validators.ts +35 -0
- package/src/utils/zod-adapter.ts +280 -0
- package/src/web-server/{index.js → index.ts} +19 -19
- package/src/workers/csv-multithreaded.ts +310 -0
- package/src/workers/csv-parser.worker.ts +227 -0
- package/src/workers/worker-pool.ts +409 -0
- package/bin/jtcsv.js +0 -2462
- package/csv-to-json.js +0 -688
- package/errors.js +0 -208
- package/examples/simple-usage.js +0 -282
- package/index.js +0 -68
- package/json-save.js +0 -254
- package/json-to-csv.js +0 -526
- package/plugins/README.md +0 -91
- package/plugins/express-middleware/README.md +0 -64
- package/plugins/express-middleware/example.js +0 -136
- package/plugins/express-middleware/index.d.ts +0 -114
- package/plugins/express-middleware/index.js +0 -360
- package/plugins/express-middleware/package.json +0 -52
- package/plugins/fastify-plugin/index.js +0 -406
- 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/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/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/api-convert.js +0 -69
- package/plugins/nextjs-api/index.js +0 -387
- package/plugins/nextjs-api/package.json +0 -63
- package/plugins/nextjs-api/route.js +0 -371
- package/plugins/nuxt/README.md +0 -24
- package/plugins/nuxt/index.js +0 -21
- package/plugins/nuxt/package.json +0 -35
- package/plugins/nuxt/runtime/composables/useJtcsv.js +0 -6
- package/plugins/nuxt/runtime/plugin.js +0 -6
- 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/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/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/package.json +0 -34
- package/src/browser/browser-functions.js +0 -219
- package/src/browser/csv-to-json-browser.js +0 -700
- package/src/browser/index.js +0 -113
- package/src/browser/json-to-csv-browser.js +0 -309
- package/src/browser/streams.js +0 -393
- package/src/core/delimiter-cache.js +0 -186
- package/src/core/plugin-system.js +0 -476
- package/src/core/transform-hooks.js +0 -350
- package/src/errors.js +0 -26
- package/src/utils/transform-loader.js +0 -205
- package/stream-csv-to-json.js +0 -542
- package/stream-json-to-csv.js +0 -464
- /package/examples/{web-workers-advanced.js → web-workers-advanced.ts} +0 -0
package/dist/jtcsv.cjs.js
CHANGED
|
@@ -2,1496 +2,1340 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
6
5
|
// Система ошибок для браузерной версии jtcsv
|
|
7
6
|
// Адаптирована для работы без Node.js специфичных API
|
|
8
|
-
|
|
9
7
|
/**
|
|
10
8
|
* Базовый класс ошибки jtcsv
|
|
11
9
|
*/
|
|
12
10
|
class JTCSVError extends Error {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
11
|
+
constructor(message, code = 'JTCSV_ERROR', details = {}) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = 'JTCSVError';
|
|
14
|
+
this.code = code;
|
|
15
|
+
this.details = details;
|
|
16
|
+
this.hint = details.hint;
|
|
17
|
+
this.docs = details.docs;
|
|
18
|
+
this.context = details.context;
|
|
19
|
+
// Сохранение stack trace
|
|
20
|
+
if (Error.captureStackTrace) {
|
|
21
|
+
Error.captureStackTrace(this, JTCSVError);
|
|
22
|
+
}
|
|
22
23
|
}
|
|
23
|
-
}
|
|
24
24
|
}
|
|
25
|
-
|
|
26
25
|
/**
|
|
27
26
|
* Ошибка валидации
|
|
28
27
|
*/
|
|
29
28
|
class ValidationError extends JTCSVError {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
constructor(message, details = {}) {
|
|
30
|
+
super(message, 'VALIDATION_ERROR', details);
|
|
31
|
+
this.name = 'ValidationError';
|
|
32
|
+
}
|
|
34
33
|
}
|
|
35
|
-
|
|
36
34
|
/**
|
|
37
35
|
* Ошибка безопасности
|
|
38
36
|
*/
|
|
39
37
|
class SecurityError extends JTCSVError {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
constructor(message, details = {}) {
|
|
39
|
+
super(message, 'SECURITY_ERROR', details);
|
|
40
|
+
this.name = 'SecurityError';
|
|
41
|
+
}
|
|
44
42
|
}
|
|
45
|
-
|
|
46
43
|
/**
|
|
47
44
|
* Ошибка файловой системы (адаптирована для браузера)
|
|
48
45
|
*/
|
|
49
46
|
class FileSystemError extends JTCSVError {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (originalError && originalError.code) {
|
|
57
|
-
this.code = originalError.code;
|
|
47
|
+
constructor(message, originalError, details = {}) {
|
|
48
|
+
super(message, 'FILE_SYSTEM_ERROR', { ...details, originalError });
|
|
49
|
+
this.name = 'FileSystemError';
|
|
50
|
+
if (originalError && originalError.code) {
|
|
51
|
+
this.code = originalError.code;
|
|
52
|
+
}
|
|
58
53
|
}
|
|
59
|
-
}
|
|
60
54
|
}
|
|
61
|
-
|
|
62
55
|
/**
|
|
63
56
|
* Ошибка парсинга
|
|
64
57
|
*/
|
|
65
58
|
class ParsingError extends JTCSVError {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
this.name = 'ParsingError';
|
|
72
|
-
this.lineNumber = lineNumber;
|
|
73
|
-
}
|
|
59
|
+
constructor(message, lineNumber, details = {}) {
|
|
60
|
+
super(message, 'PARSING_ERROR', { ...details, lineNumber });
|
|
61
|
+
this.name = 'ParsingError';
|
|
62
|
+
this.lineNumber = lineNumber;
|
|
63
|
+
}
|
|
74
64
|
}
|
|
75
|
-
|
|
76
65
|
/**
|
|
77
66
|
* Ошибка превышения лимита
|
|
78
67
|
*/
|
|
79
68
|
class LimitError extends JTCSVError {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
this.name = 'LimitError';
|
|
87
|
-
this.limit = limit;
|
|
88
|
-
this.actual = actual;
|
|
89
|
-
}
|
|
69
|
+
constructor(message, limit, actual, details = {}) {
|
|
70
|
+
super(message, 'LIMIT_ERROR', { ...details, limit, actual });
|
|
71
|
+
this.name = 'LimitError';
|
|
72
|
+
this.limit = limit;
|
|
73
|
+
this.actual = actual;
|
|
74
|
+
}
|
|
90
75
|
}
|
|
91
|
-
|
|
92
76
|
/**
|
|
93
77
|
* Ошибка конфигурации
|
|
94
78
|
*/
|
|
95
79
|
class ConfigurationError extends JTCSVError {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
80
|
+
constructor(message, details = {}) {
|
|
81
|
+
super(message, 'CONFIGURATION_ERROR', details);
|
|
82
|
+
this.name = 'ConfigurationError';
|
|
83
|
+
}
|
|
100
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* Коды ошибок
|
|
87
|
+
*/
|
|
101
88
|
const ERROR_CODES = {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
89
|
+
JTCSV_ERROR: 'JTCSV_ERROR',
|
|
90
|
+
VALIDATION_ERROR: 'VALIDATION_ERROR',
|
|
91
|
+
SECURITY_ERROR: 'SECURITY_ERROR',
|
|
92
|
+
FILE_SYSTEM_ERROR: 'FILE_SYSTEM_ERROR',
|
|
93
|
+
PARSING_ERROR: 'PARSING_ERROR',
|
|
94
|
+
LIMIT_ERROR: 'LIMIT_ERROR',
|
|
95
|
+
CONFIGURATION_ERROR: 'CONFIGURATION_ERROR',
|
|
96
|
+
INVALID_INPUT: 'INVALID_INPUT',
|
|
97
|
+
SECURITY_VIOLATION: 'SECURITY_VIOLATION',
|
|
98
|
+
FILE_NOT_FOUND: 'FILE_NOT_FOUND',
|
|
99
|
+
PARSE_FAILED: 'PARSE_FAILED',
|
|
100
|
+
SIZE_LIMIT: 'SIZE_LIMIT',
|
|
101
|
+
INVALID_CONFIG: 'INVALID_CONFIG',
|
|
102
|
+
UNKNOWN_ERROR: 'UNKNOWN_ERROR'
|
|
116
103
|
};
|
|
117
|
-
|
|
118
104
|
/**
|
|
119
105
|
* Безопасное выполнение функции с обработкой ошибок
|
|
120
|
-
*
|
|
121
|
-
* @param
|
|
122
|
-
* @param
|
|
123
|
-
* @param
|
|
124
|
-
* @returns
|
|
106
|
+
*
|
|
107
|
+
* @param fn - Функция для выполнения
|
|
108
|
+
* @param errorCode - Код ошибки по умолчанию
|
|
109
|
+
* @param errorDetails - Детали ошибки
|
|
110
|
+
* @returns Результат выполнения функции
|
|
125
111
|
*/
|
|
126
112
|
function safeExecute(fn, errorCode = 'UNKNOWN_ERROR', errorDetails = {}) {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
} catch (error) {
|
|
133
|
-
// Если ошибка уже является JTCSVError, перебросить её
|
|
134
|
-
if (error instanceof JTCSVError) {
|
|
135
|
-
throw error;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// Определить тип ошибки на основе сообщения или кода
|
|
139
|
-
let enhancedError;
|
|
140
|
-
const errorMessage = error.message || String(error);
|
|
141
|
-
if (errorMessage.includes('validation') || errorMessage.includes('Validation')) {
|
|
142
|
-
enhancedError = new ValidationError(errorMessage, {
|
|
143
|
-
...errorDetails,
|
|
144
|
-
originalError: error
|
|
145
|
-
});
|
|
146
|
-
} else if (errorMessage.includes('security') || errorMessage.includes('Security')) {
|
|
147
|
-
enhancedError = new SecurityError(errorMessage, {
|
|
148
|
-
...errorDetails,
|
|
149
|
-
originalError: error
|
|
150
|
-
});
|
|
151
|
-
} else if (errorMessage.includes('parsing') || errorMessage.includes('Parsing')) {
|
|
152
|
-
enhancedError = new ParsingError(errorMessage, null, {
|
|
153
|
-
...errorDetails,
|
|
154
|
-
originalError: error
|
|
155
|
-
});
|
|
156
|
-
} else if (errorMessage.includes('limit') || errorMessage.includes('Limit')) {
|
|
157
|
-
enhancedError = new LimitError(errorMessage, null, null, {
|
|
158
|
-
...errorDetails,
|
|
159
|
-
originalError: error
|
|
160
|
-
});
|
|
161
|
-
} else if (errorMessage.includes('configuration') || errorMessage.includes('Configuration')) {
|
|
162
|
-
enhancedError = new ConfigurationError(errorMessage, {
|
|
163
|
-
...errorDetails,
|
|
164
|
-
originalError: error
|
|
165
|
-
});
|
|
166
|
-
} else if (errorMessage.includes('file') || errorMessage.includes('File')) {
|
|
167
|
-
enhancedError = new FileSystemError(errorMessage, error, errorDetails);
|
|
168
|
-
} else {
|
|
169
|
-
// Общая ошибка
|
|
170
|
-
enhancedError = new JTCSVError(errorMessage, errorCode, {
|
|
171
|
-
...errorDetails,
|
|
172
|
-
originalError: error
|
|
173
|
-
});
|
|
113
|
+
try {
|
|
114
|
+
if (typeof fn === 'function') {
|
|
115
|
+
return fn();
|
|
116
|
+
}
|
|
117
|
+
throw new ValidationError('Function expected');
|
|
174
118
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
119
|
+
catch (error) {
|
|
120
|
+
// Если ошибка уже является JTCSVError, перебросить её
|
|
121
|
+
if (error instanceof JTCSVError) {
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
// Определить тип ошибки на основе сообщения или кода
|
|
125
|
+
let enhancedError;
|
|
126
|
+
const errorMessage = error.message || String(error);
|
|
127
|
+
if (errorMessage.includes('validation') || errorMessage.includes('Validation')) {
|
|
128
|
+
enhancedError = new ValidationError(errorMessage, { ...errorDetails, originalError: error });
|
|
129
|
+
}
|
|
130
|
+
else if (errorMessage.includes('security') || errorMessage.includes('Security')) {
|
|
131
|
+
enhancedError = new SecurityError(errorMessage, { ...errorDetails, originalError: error });
|
|
132
|
+
}
|
|
133
|
+
else if (errorMessage.includes('parsing') || errorMessage.includes('Parsing')) {
|
|
134
|
+
enhancedError = new ParsingError(errorMessage, undefined, { ...errorDetails, originalError: error });
|
|
135
|
+
}
|
|
136
|
+
else if (errorMessage.includes('limit') || errorMessage.includes('Limit')) {
|
|
137
|
+
enhancedError = new LimitError(errorMessage, null, null, { ...errorDetails, originalError: error });
|
|
138
|
+
}
|
|
139
|
+
else if (errorMessage.includes('configuration') || errorMessage.includes('Configuration')) {
|
|
140
|
+
enhancedError = new ConfigurationError(errorMessage, { ...errorDetails, originalError: error });
|
|
141
|
+
}
|
|
142
|
+
else if (errorMessage.includes('file') || errorMessage.includes('File')) {
|
|
143
|
+
enhancedError = new FileSystemError(errorMessage, error, errorDetails);
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
// Общая ошибка
|
|
147
|
+
enhancedError = new JTCSVError(errorMessage, errorCode, { ...errorDetails, originalError: error });
|
|
148
|
+
}
|
|
149
|
+
// Сохранить оригинальный stack trace если возможно
|
|
150
|
+
if (error.stack) {
|
|
151
|
+
enhancedError.stack = error.stack;
|
|
152
|
+
}
|
|
153
|
+
throw enhancedError;
|
|
179
154
|
}
|
|
180
|
-
throw enhancedError;
|
|
181
|
-
}
|
|
182
155
|
}
|
|
183
|
-
|
|
184
156
|
/**
|
|
185
157
|
* Асинхронная версия safeExecute
|
|
186
158
|
*/
|
|
187
159
|
async function safeExecuteAsync(fn, errorCode = 'UNKNOWN_ERROR', errorDetails = {}) {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
160
|
+
try {
|
|
161
|
+
if (typeof fn === 'function') {
|
|
162
|
+
return await fn();
|
|
163
|
+
}
|
|
164
|
+
throw new ValidationError('Function expected');
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
// Если ошибка уже является JTCSVError, перебросить её
|
|
168
|
+
if (error instanceof JTCSVError) {
|
|
169
|
+
throw error;
|
|
170
|
+
}
|
|
171
|
+
// Определить тип ошибки
|
|
172
|
+
let enhancedError;
|
|
173
|
+
const errorMessage = error.message || String(error);
|
|
174
|
+
if (errorMessage.includes('validation') || errorMessage.includes('Validation')) {
|
|
175
|
+
enhancedError = new ValidationError(errorMessage, { ...errorDetails, originalError: error });
|
|
176
|
+
}
|
|
177
|
+
else if (errorMessage.includes('security') || errorMessage.includes('Security')) {
|
|
178
|
+
enhancedError = new SecurityError(errorMessage, { ...errorDetails, originalError: error });
|
|
179
|
+
}
|
|
180
|
+
else if (errorMessage.includes('parsing') || errorMessage.includes('Parsing')) {
|
|
181
|
+
enhancedError = new ParsingError(errorMessage, undefined, { ...errorDetails, originalError: error });
|
|
182
|
+
}
|
|
183
|
+
else if (errorMessage.includes('limit') || errorMessage.includes('Limit')) {
|
|
184
|
+
enhancedError = new LimitError(errorMessage, null, null, { ...errorDetails, originalError: error });
|
|
185
|
+
}
|
|
186
|
+
else if (errorMessage.includes('configuration') || errorMessage.includes('Configuration')) {
|
|
187
|
+
enhancedError = new ConfigurationError(errorMessage, { ...errorDetails, originalError: error });
|
|
188
|
+
}
|
|
189
|
+
else if (errorMessage.includes('file') || errorMessage.includes('File')) {
|
|
190
|
+
enhancedError = new FileSystemError(errorMessage, error, errorDetails);
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
enhancedError = new JTCSVError(errorMessage, errorCode, { ...errorDetails, originalError: error });
|
|
194
|
+
}
|
|
195
|
+
if (error.stack) {
|
|
196
|
+
enhancedError.stack = error.stack;
|
|
197
|
+
}
|
|
198
|
+
throw enhancedError;
|
|
191
199
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Создать сообщение об ошибке
|
|
203
|
+
*/
|
|
204
|
+
function createErrorMessage(error, includeStack = false) {
|
|
205
|
+
let message = error.message || 'Unknown error';
|
|
195
206
|
if (error instanceof JTCSVError) {
|
|
196
|
-
|
|
207
|
+
message = `[${error.code}] ${message}`;
|
|
208
|
+
if (error instanceof ParsingError && error.lineNumber) {
|
|
209
|
+
message += ` (line ${error.lineNumber})`;
|
|
210
|
+
}
|
|
211
|
+
if (error instanceof LimitError && error.limit && error.actual) {
|
|
212
|
+
message += ` (limit: ${error.limit}, actual: ${error.actual})`;
|
|
213
|
+
}
|
|
214
|
+
if (error.hint) {
|
|
215
|
+
message += `\nHint: ${error.hint}`;
|
|
216
|
+
}
|
|
217
|
+
if (error.docs) {
|
|
218
|
+
message += `\nDocs: ${error.docs}`;
|
|
219
|
+
}
|
|
197
220
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
...errorDetails,
|
|
220
|
-
originalError: error
|
|
221
|
-
});
|
|
222
|
-
} else if (errorMessage.includes('configuration') || errorMessage.includes('Configuration')) {
|
|
223
|
-
enhancedError = new ConfigurationError(errorMessage, {
|
|
224
|
-
...errorDetails,
|
|
225
|
-
originalError: error
|
|
226
|
-
});
|
|
227
|
-
} else if (errorMessage.includes('file') || errorMessage.includes('File')) {
|
|
228
|
-
enhancedError = new FileSystemError(errorMessage, error, errorDetails);
|
|
229
|
-
} else {
|
|
230
|
-
enhancedError = new JTCSVError(errorMessage, errorCode, {
|
|
231
|
-
...errorDetails,
|
|
232
|
-
originalError: error
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
if (error.stack) {
|
|
236
|
-
enhancedError.stack = error.stack;
|
|
237
|
-
}
|
|
238
|
-
throw enhancedError;
|
|
239
|
-
}
|
|
221
|
+
if (includeStack && error.stack) {
|
|
222
|
+
message += `\n${error.stack}`;
|
|
223
|
+
}
|
|
224
|
+
return message;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Обработка ошибки
|
|
228
|
+
*/
|
|
229
|
+
function handleError(error, options = {}) {
|
|
230
|
+
const { log = true, throw: shouldThrow = false, format = true } = options;
|
|
231
|
+
const message = format ? createErrorMessage(error) : error.message;
|
|
232
|
+
if (log) {
|
|
233
|
+
console.error(`[jtcsv] ${message}`);
|
|
234
|
+
if (error instanceof JTCSVError && error.details) {
|
|
235
|
+
console.error('Error details:', error.details);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (shouldThrow) {
|
|
239
|
+
throw error;
|
|
240
|
+
}
|
|
241
|
+
return message;
|
|
240
242
|
}
|
|
241
|
-
|
|
242
243
|
// Экспорт для Node.js совместимости
|
|
243
244
|
if (typeof module !== 'undefined' && module.exports) {
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
245
|
+
module.exports = {
|
|
246
|
+
JTCSVError,
|
|
247
|
+
ValidationError,
|
|
248
|
+
SecurityError,
|
|
249
|
+
FileSystemError,
|
|
250
|
+
ParsingError,
|
|
251
|
+
LimitError,
|
|
252
|
+
ConfigurationError,
|
|
253
|
+
ERROR_CODES,
|
|
254
|
+
safeExecute,
|
|
255
|
+
safeExecuteAsync,
|
|
256
|
+
createErrorMessage,
|
|
257
|
+
handleError
|
|
258
|
+
};
|
|
256
259
|
}
|
|
257
260
|
|
|
258
261
|
// Браузерная версия JSON to CSV конвертера
|
|
259
262
|
// Адаптирована для работы в браузере без Node.js API
|
|
260
|
-
|
|
261
|
-
|
|
262
263
|
/**
|
|
263
264
|
* Валидация входных данных и опций
|
|
264
265
|
* @private
|
|
265
266
|
*/
|
|
266
267
|
function validateInput(data, options) {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
// Validate options
|
|
273
|
-
if (options && typeof options !== 'object') {
|
|
274
|
-
throw new ConfigurationError('Options must be an object');
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
// Validate delimiter
|
|
278
|
-
if (options !== null && options !== void 0 && options.delimiter && typeof options.delimiter !== 'string') {
|
|
279
|
-
throw new ConfigurationError('Delimiter must be a string');
|
|
280
|
-
}
|
|
281
|
-
if (options !== null && options !== void 0 && options.delimiter && options.delimiter.length !== 1) {
|
|
282
|
-
throw new ConfigurationError('Delimiter must be a single character');
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
// Validate renameMap
|
|
286
|
-
if (options !== null && options !== void 0 && options.renameMap && typeof options.renameMap !== 'object') {
|
|
287
|
-
throw new ConfigurationError('renameMap must be an object');
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
// Validate maxRecords
|
|
291
|
-
if (options && options.maxRecords !== undefined) {
|
|
292
|
-
if (typeof options.maxRecords !== 'number' || options.maxRecords <= 0) {
|
|
293
|
-
throw new ConfigurationError('maxRecords must be a positive number');
|
|
268
|
+
// Validate data
|
|
269
|
+
if (!Array.isArray(data)) {
|
|
270
|
+
throw new ValidationError('Input data must be an array');
|
|
294
271
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
if ((options === null || options === void 0 ? void 0 : options.preventCsvInjection) !== undefined && typeof options.preventCsvInjection !== 'boolean') {
|
|
299
|
-
throw new ConfigurationError('preventCsvInjection must be a boolean');
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
// Validate rfc4180Compliant
|
|
303
|
-
if ((options === null || options === void 0 ? void 0 : options.rfc4180Compliant) !== undefined && typeof options.rfc4180Compliant !== 'boolean') {
|
|
304
|
-
throw new ConfigurationError('rfc4180Compliant must be a boolean');
|
|
305
|
-
}
|
|
306
|
-
return true;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
/**
|
|
310
|
-
* Конвертирует JSON данные в CSV формат
|
|
311
|
-
*
|
|
312
|
-
* @param {Array<Object>} data - Массив объектов для конвертации в CSV
|
|
313
|
-
* @param {Object} [options] - Опции конфигурации
|
|
314
|
-
* @param {string} [options.delimiter=';'] - CSV разделитель
|
|
315
|
-
* @param {boolean} [options.includeHeaders=true] - Включать ли заголовки
|
|
316
|
-
* @param {Object} [options.renameMap={}] - Маппинг переименования заголовков
|
|
317
|
-
* @param {Object} [options.template={}] - Шаблон для порядка колонок
|
|
318
|
-
* @param {number} [options.maxRecords] - Максимальное количество записей
|
|
319
|
-
* @param {boolean} [options.preventCsvInjection=true] - Защита от CSV инъекций
|
|
320
|
-
* @param {boolean} [options.rfc4180Compliant=true] - Соответствие RFC 4180
|
|
321
|
-
* @returns {string} CSV строка
|
|
322
|
-
*/
|
|
323
|
-
function jsonToCsv(data, options = {}) {
|
|
324
|
-
return safeExecute(() => {
|
|
325
|
-
// Валидация входных данных
|
|
326
|
-
validateInput(data, options);
|
|
327
|
-
const opts = options && typeof options === 'object' ? options : {};
|
|
328
|
-
const {
|
|
329
|
-
delimiter = ';',
|
|
330
|
-
includeHeaders = true,
|
|
331
|
-
renameMap = {},
|
|
332
|
-
template = {},
|
|
333
|
-
maxRecords,
|
|
334
|
-
preventCsvInjection = true,
|
|
335
|
-
rfc4180Compliant = true
|
|
336
|
-
} = opts;
|
|
337
|
-
|
|
338
|
-
// Обработка пустых данных
|
|
339
|
-
if (data.length === 0) {
|
|
340
|
-
return '';
|
|
272
|
+
// Validate options
|
|
273
|
+
if (options && typeof options !== 'object') {
|
|
274
|
+
throw new ConfigurationError('Options must be an object');
|
|
341
275
|
}
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
console.warn('⚠️ Warning: Processing >1M records in memory may be slow.\n' + '💡 Consider processing data in batches or using Web Workers for large files.\n' + '📊 Current size: ' + data.length.toLocaleString() + ' records');
|
|
276
|
+
// Validate delimiter
|
|
277
|
+
if (options?.delimiter && typeof options.delimiter !== 'string') {
|
|
278
|
+
throw new ConfigurationError('Delimiter must be a string');
|
|
346
279
|
}
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
if (maxRecords && data.length > maxRecords) {
|
|
350
|
-
throw new LimitError(`Data size exceeds maximum limit of ${maxRecords} records`, maxRecords, data.length);
|
|
280
|
+
if (options?.delimiter && options.delimiter.length !== 1) {
|
|
281
|
+
throw new ConfigurationError('Delimiter must be a single character');
|
|
351
282
|
}
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
data.forEach(item => {
|
|
356
|
-
if (!item || typeof item !== 'object') {
|
|
357
|
-
return;
|
|
358
|
-
}
|
|
359
|
-
Object.keys(item).forEach(key => allKeys.add(key));
|
|
360
|
-
});
|
|
361
|
-
const originalKeys = Array.from(allKeys);
|
|
362
|
-
|
|
363
|
-
// Применение rename map для создания заголовков
|
|
364
|
-
const headers = originalKeys.map(key => renameMap[key] || key);
|
|
365
|
-
|
|
366
|
-
// Создание обратного маппинга
|
|
367
|
-
const reverseRenameMap = {};
|
|
368
|
-
originalKeys.forEach((key, index) => {
|
|
369
|
-
reverseRenameMap[headers[index]] = key;
|
|
370
|
-
});
|
|
371
|
-
|
|
372
|
-
// Применение порядка из шаблона
|
|
373
|
-
let finalHeaders = headers;
|
|
374
|
-
if (Object.keys(template).length > 0) {
|
|
375
|
-
const templateHeaders = Object.keys(template).map(key => renameMap[key] || key);
|
|
376
|
-
const extraHeaders = headers.filter(h => !templateHeaders.includes(h));
|
|
377
|
-
finalHeaders = [...templateHeaders, ...extraHeaders];
|
|
283
|
+
// Validate renameMap
|
|
284
|
+
if (options?.renameMap && typeof options.renameMap !== 'object') {
|
|
285
|
+
throw new ConfigurationError('renameMap must be an object');
|
|
378
286
|
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
const escapeValue = value => {
|
|
385
|
-
if (value === null || value === undefined || value === '') {
|
|
386
|
-
return '';
|
|
387
|
-
}
|
|
388
|
-
const stringValue = String(value);
|
|
389
|
-
|
|
390
|
-
// Защита от CSV инъекций
|
|
391
|
-
let escapedValue = stringValue;
|
|
392
|
-
if (preventCsvInjection && /^[=+\-@]/.test(stringValue)) {
|
|
393
|
-
escapedValue = "'" + stringValue;
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
// Соответствие RFC 4180
|
|
397
|
-
const needsQuoting = rfc4180Compliant ? escapedValue.includes(delimiter) || escapedValue.includes('"') || escapedValue.includes('\n') || escapedValue.includes('\r') : escapedValue.includes(delimiter) || escapedValue.includes('"') || escapedValue.includes('\n') || escapedValue.includes('\r');
|
|
398
|
-
if (needsQuoting) {
|
|
399
|
-
return `"${escapedValue.replace(/"/g, '""')}"`;
|
|
400
|
-
}
|
|
401
|
-
return escapedValue;
|
|
402
|
-
};
|
|
403
|
-
|
|
404
|
-
// Построение CSV строк
|
|
405
|
-
const rows = [];
|
|
406
|
-
|
|
407
|
-
// Добавление заголовков
|
|
408
|
-
if (includeHeaders && finalHeaders.length > 0) {
|
|
409
|
-
rows.push(finalHeaders.join(delimiter));
|
|
287
|
+
// Validate maxRecords
|
|
288
|
+
if (options && options.maxRecords !== undefined) {
|
|
289
|
+
if (typeof options.maxRecords !== 'number' || options.maxRecords <= 0) {
|
|
290
|
+
throw new ConfigurationError('maxRecords must be a positive number');
|
|
291
|
+
}
|
|
410
292
|
}
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
if (!item || typeof item !== 'object') {
|
|
415
|
-
continue;
|
|
416
|
-
}
|
|
417
|
-
const row = finalHeaders.map(header => {
|
|
418
|
-
const originalKey = reverseRenameMap[header] || header;
|
|
419
|
-
const value = item[originalKey];
|
|
420
|
-
return escapeValue(value);
|
|
421
|
-
}).join(delimiter);
|
|
422
|
-
rows.push(row);
|
|
293
|
+
// Validate preventCsvInjection
|
|
294
|
+
if (options?.preventCsvInjection !== undefined && typeof options.preventCsvInjection !== 'boolean') {
|
|
295
|
+
throw new ConfigurationError('preventCsvInjection must be a boolean');
|
|
423
296
|
}
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
297
|
+
// Validate rfc4180Compliant
|
|
298
|
+
if (options?.rfc4180Compliant !== undefined && typeof options.rfc4180Compliant !== 'boolean') {
|
|
299
|
+
throw new ConfigurationError('rfc4180Compliant must be a boolean');
|
|
300
|
+
}
|
|
301
|
+
if (options?.normalizeQuotes !== undefined && typeof options.normalizeQuotes !== 'boolean') {
|
|
302
|
+
throw new ConfigurationError('normalizeQuotes must be a boolean');
|
|
303
|
+
}
|
|
304
|
+
return true;
|
|
431
305
|
}
|
|
432
|
-
|
|
433
306
|
/**
|
|
434
|
-
*
|
|
435
|
-
*
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
* @returns {string} Развернутое строковое значение
|
|
441
|
-
*/
|
|
442
|
-
function deepUnwrap(value, depth = 0, maxDepth = 5, visited = new Set()) {
|
|
443
|
-
// Проверка глубины
|
|
444
|
-
if (depth >= maxDepth) {
|
|
445
|
-
return '[Too Deep]';
|
|
446
|
-
}
|
|
447
|
-
if (value === null || value === undefined) {
|
|
448
|
-
return '';
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
// Обработка циклических ссылок
|
|
452
|
-
if (typeof value === 'object') {
|
|
453
|
-
if (visited.has(value)) {
|
|
454
|
-
return '[Circular Reference]';
|
|
307
|
+
* Экранирование CSV значений для предотвращения инъекций
|
|
308
|
+
* @private
|
|
309
|
+
*/
|
|
310
|
+
function escapeCsvValue(value, preventInjection = true) {
|
|
311
|
+
if (value === null || value === undefined) {
|
|
312
|
+
return '';
|
|
455
313
|
}
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
314
|
+
const str = String(value);
|
|
315
|
+
const isPotentialFormula = (input) => {
|
|
316
|
+
let idx = 0;
|
|
317
|
+
while (idx < input.length) {
|
|
318
|
+
const code = input.charCodeAt(idx);
|
|
319
|
+
if (code === 32 || code === 9 || code === 10 || code === 13 || code === 0xfeff) {
|
|
320
|
+
idx++;
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
break;
|
|
324
|
+
}
|
|
325
|
+
if (idx < input.length && (input[idx] === '"' || input[idx] === "'")) {
|
|
326
|
+
idx++;
|
|
327
|
+
while (idx < input.length) {
|
|
328
|
+
const code = input.charCodeAt(idx);
|
|
329
|
+
if (code === 32 || code === 9) {
|
|
330
|
+
idx++;
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
if (idx >= input.length) {
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
339
|
+
const char = input[idx];
|
|
340
|
+
return char === '=' || char === '+' || char === '-' || char === '@';
|
|
341
|
+
};
|
|
342
|
+
// Экранирование формул для предотвращения CSV инъекций
|
|
343
|
+
if (preventInjection && isPotentialFormula(str)) {
|
|
344
|
+
return "'" + str;
|
|
473
345
|
}
|
|
474
|
-
|
|
475
|
-
|
|
346
|
+
// Экранирование кавычек и переносов строк
|
|
347
|
+
if (str.includes('"') || str.includes('\n') || str.includes('\r') || str.includes(',')) {
|
|
348
|
+
return '"' + str.replace(/"/g, '""') + '"';
|
|
476
349
|
}
|
|
477
|
-
|
|
478
|
-
// Сериализация сложных объектов
|
|
479
|
-
try {
|
|
480
|
-
return JSON.stringify(value);
|
|
481
|
-
} catch (error) {
|
|
482
|
-
if (error.message.includes('circular') || error.message.includes('Converting circular')) {
|
|
483
|
-
return '[Circular Reference]';
|
|
484
|
-
}
|
|
485
|
-
return '[Unstringifiable Object]';
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
// Примитивные значения
|
|
490
|
-
return String(value);
|
|
350
|
+
return str;
|
|
491
351
|
}
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
const processed = {};
|
|
508
|
-
for (const key in item) {
|
|
509
|
-
if (Object.prototype.hasOwnProperty.call(item, key)) {
|
|
510
|
-
const value = item[key];
|
|
511
|
-
if (value && typeof value === 'object') {
|
|
512
|
-
processed[key] = deepUnwrap(value);
|
|
513
|
-
} else {
|
|
514
|
-
processed[key] = value;
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
return processed;
|
|
519
|
-
});
|
|
352
|
+
function normalizeQuotesInField$2(value) {
|
|
353
|
+
// Не нормализуем кавычки в JSON-строках - это ломает структуру JSON
|
|
354
|
+
// Проверяем, выглядит ли значение как JSON (объект или массив)
|
|
355
|
+
if ((value.startsWith('{') && value.endsWith('}')) ||
|
|
356
|
+
(value.startsWith('[') && value.endsWith(']'))) {
|
|
357
|
+
return value; // Возвращаем как есть для JSON
|
|
358
|
+
}
|
|
359
|
+
let normalized = value.replace(/"{2,}/g, '"');
|
|
360
|
+
// Убираем правило, которое ломает JSON: не заменяем "," на ","
|
|
361
|
+
// normalized = normalized.replace(/"\s*,\s*"/g, ',');
|
|
362
|
+
normalized = normalized.replace(/"\n/g, '\n').replace(/\n"/g, '\n');
|
|
363
|
+
if (normalized.length >= 2 && normalized.startsWith('"') && normalized.endsWith('"')) {
|
|
364
|
+
normalized = normalized.slice(1, -1);
|
|
365
|
+
}
|
|
366
|
+
return normalized;
|
|
520
367
|
}
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
if (
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
368
|
+
function normalizePhoneValue$2(value) {
|
|
369
|
+
const trimmed = value.trim();
|
|
370
|
+
if (trimmed === '') {
|
|
371
|
+
return trimmed;
|
|
372
|
+
}
|
|
373
|
+
return trimmed.replace(/["'\\]/g, '');
|
|
374
|
+
}
|
|
375
|
+
function normalizeValueForCsv$1(value, key, normalizeQuotes) {
|
|
376
|
+
if (!normalizeQuotes || typeof value !== 'string') {
|
|
377
|
+
return value;
|
|
378
|
+
}
|
|
379
|
+
const base = normalizeQuotesInField$2(value);
|
|
380
|
+
if (!key) {
|
|
381
|
+
return base;
|
|
382
|
+
}
|
|
383
|
+
const phoneKeys = new Set(['phone', 'phonenumber', 'phone_number', 'tel', 'telephone']);
|
|
384
|
+
if (phoneKeys.has(String(key).toLowerCase())) {
|
|
385
|
+
return normalizePhoneValue$2(base);
|
|
386
|
+
}
|
|
387
|
+
return base;
|
|
529
388
|
}
|
|
530
|
-
|
|
531
|
-
// Браузерная версия CSV to JSON конвертера
|
|
532
|
-
// Адаптирована для работы в браузере без Node.js API
|
|
533
|
-
|
|
534
|
-
|
|
535
389
|
/**
|
|
536
|
-
*
|
|
390
|
+
* Извлечение всех уникальных ключей из массива объектов
|
|
537
391
|
* @private
|
|
538
392
|
*/
|
|
539
|
-
function
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
throw new ConfigurationError('Delimiter must be a string');
|
|
548
|
-
}
|
|
549
|
-
if (options !== null && options !== void 0 && options.delimiter && options.delimiter.length !== 1) {
|
|
550
|
-
throw new ConfigurationError('Delimiter must be a single character');
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
// Validate autoDetect
|
|
554
|
-
if ((options === null || options === void 0 ? void 0 : options.autoDetect) !== undefined && typeof options.autoDetect !== 'boolean') {
|
|
555
|
-
throw new ConfigurationError('autoDetect must be a boolean');
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
// Validate candidates
|
|
559
|
-
if (options !== null && options !== void 0 && options.candidates && !Array.isArray(options.candidates)) {
|
|
560
|
-
throw new ConfigurationError('candidates must be an array');
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
// Validate maxRows
|
|
564
|
-
if ((options === null || options === void 0 ? void 0 : options.maxRows) !== undefined && (typeof options.maxRows !== 'number' || options.maxRows <= 0)) {
|
|
565
|
-
throw new ConfigurationError('maxRows must be a positive number');
|
|
566
|
-
}
|
|
567
|
-
if ((options === null || options === void 0 ? void 0 : options.warnExtraFields) !== undefined && typeof options.warnExtraFields !== 'boolean') {
|
|
568
|
-
throw new ConfigurationError('warnExtraFields must be a boolean');
|
|
569
|
-
}
|
|
570
|
-
return true;
|
|
393
|
+
function extractAllKeys(data) {
|
|
394
|
+
const keys = new Set();
|
|
395
|
+
for (const item of data) {
|
|
396
|
+
if (item && typeof item === 'object') {
|
|
397
|
+
Object.keys(item).forEach(key => keys.add(key));
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
return Array.from(keys);
|
|
571
401
|
}
|
|
572
|
-
|
|
573
402
|
/**
|
|
574
|
-
*
|
|
575
|
-
*
|
|
403
|
+
* Конвертация массива объектов в CSV строку
|
|
404
|
+
*
|
|
405
|
+
* @param data - Массив объектов для конвертации
|
|
406
|
+
* @param options - Опции конвертации
|
|
407
|
+
* @returns CSV строка
|
|
576
408
|
*/
|
|
577
|
-
function
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
409
|
+
function jsonToCsv$1(data, options = {}) {
|
|
410
|
+
return safeExecute(() => {
|
|
411
|
+
validateInput(data, options);
|
|
412
|
+
if (data.length === 0) {
|
|
413
|
+
return '';
|
|
414
|
+
}
|
|
415
|
+
// Настройки по умолчанию
|
|
416
|
+
const delimiter = options.delimiter || ';';
|
|
417
|
+
const includeHeaders = options.includeHeaders !== false;
|
|
418
|
+
const maxRecords = options.maxRecords || data.length;
|
|
419
|
+
const preventInjection = options.preventCsvInjection !== false;
|
|
420
|
+
const rfc4180Compliant = options.rfc4180Compliant !== false;
|
|
421
|
+
const normalizeQuotes = options.normalizeQuotes !== false;
|
|
422
|
+
// Ограничение количества записей
|
|
423
|
+
const limitedData = data.slice(0, maxRecords);
|
|
424
|
+
// Извлечение всех ключей
|
|
425
|
+
const allKeys = extractAllKeys(limitedData);
|
|
426
|
+
// Применение renameMap если есть
|
|
427
|
+
const renameMap = options.renameMap || {};
|
|
428
|
+
const finalKeys = allKeys.map(key => renameMap[key] || key);
|
|
429
|
+
// Создание CSV строки
|
|
430
|
+
const lines = [];
|
|
431
|
+
// Заголовки
|
|
432
|
+
if (includeHeaders) {
|
|
433
|
+
const headerLine = finalKeys.map(key => escapeCsvValue(key, preventInjection)).join(delimiter);
|
|
434
|
+
lines.push(headerLine);
|
|
435
|
+
}
|
|
436
|
+
// Данные
|
|
437
|
+
for (const item of limitedData) {
|
|
438
|
+
const rowValues = allKeys.map(key => {
|
|
439
|
+
const value = item?.[key];
|
|
440
|
+
const normalized = normalizeValueForCsv$1(value, key, normalizeQuotes);
|
|
441
|
+
return escapeCsvValue(normalized, preventInjection);
|
|
442
|
+
});
|
|
443
|
+
lines.push(rowValues.join(delimiter));
|
|
444
|
+
}
|
|
445
|
+
// RFC 4180 compliance: CRLF line endings
|
|
446
|
+
if (rfc4180Compliant) {
|
|
447
|
+
return lines.join('\r\n');
|
|
448
|
+
}
|
|
449
|
+
return lines.join('\n');
|
|
450
|
+
});
|
|
583
451
|
}
|
|
584
|
-
|
|
585
452
|
/**
|
|
586
|
-
*
|
|
587
|
-
* @private
|
|
453
|
+
* Асинхронная версия jsonToCsv
|
|
588
454
|
*/
|
|
589
|
-
function
|
|
590
|
-
|
|
591
|
-
let currentField = '';
|
|
592
|
-
let insideQuotes = false;
|
|
593
|
-
let escapeNext = false;
|
|
594
|
-
for (let i = 0; i < line.length; i++) {
|
|
595
|
-
const char = line[i];
|
|
596
|
-
if (escapeNext) {
|
|
597
|
-
currentField += char;
|
|
598
|
-
escapeNext = false;
|
|
599
|
-
continue;
|
|
600
|
-
}
|
|
601
|
-
if (char === '\\') {
|
|
602
|
-
if (i + 1 === line.length) {
|
|
603
|
-
// Обратный слеш в конце строки
|
|
604
|
-
currentField += char;
|
|
605
|
-
} else if (line[i + 1] === '\\') {
|
|
606
|
-
// Двойной обратный слеш
|
|
607
|
-
currentField += char;
|
|
608
|
-
i++; // Пропустить следующий слеш
|
|
609
|
-
} else {
|
|
610
|
-
// Экранирование следующего символа
|
|
611
|
-
escapeNext = true;
|
|
612
|
-
}
|
|
613
|
-
continue;
|
|
614
|
-
}
|
|
615
|
-
if (char === '"') {
|
|
616
|
-
if (insideQuotes) {
|
|
617
|
-
if (i + 1 < line.length && line[i + 1] === '"') {
|
|
618
|
-
// Экранированная кавычка внутри кавычек
|
|
619
|
-
currentField += '"';
|
|
620
|
-
i++; // Пропустить следующую кавычку
|
|
621
|
-
|
|
622
|
-
// Проверка конца поля
|
|
623
|
-
let isEndOfField = false;
|
|
624
|
-
let j = i + 1;
|
|
625
|
-
while (j < line.length && (line[j] === ' ' || line[j] === '\t')) {
|
|
626
|
-
j++;
|
|
627
|
-
}
|
|
628
|
-
if (j === line.length || line[j] === delimiter) {
|
|
629
|
-
isEndOfField = true;
|
|
630
|
-
}
|
|
631
|
-
if (isEndOfField) {
|
|
632
|
-
insideQuotes = false;
|
|
633
|
-
}
|
|
634
|
-
} else {
|
|
635
|
-
// Проверка конца поля
|
|
636
|
-
let isEndOfField = false;
|
|
637
|
-
let j = i + 1;
|
|
638
|
-
while (j < line.length && (line[j] === ' ' || line[j] === '\t')) {
|
|
639
|
-
j++;
|
|
640
|
-
}
|
|
641
|
-
if (j === line.length || line[j] === delimiter) {
|
|
642
|
-
isEndOfField = true;
|
|
643
|
-
}
|
|
644
|
-
if (isEndOfField) {
|
|
645
|
-
insideQuotes = false;
|
|
646
|
-
} else {
|
|
647
|
-
currentField += '"';
|
|
648
|
-
}
|
|
649
|
-
}
|
|
650
|
-
} else {
|
|
651
|
-
// Начало поля в кавычках
|
|
652
|
-
insideQuotes = true;
|
|
653
|
-
}
|
|
654
|
-
continue;
|
|
655
|
-
}
|
|
656
|
-
if (!insideQuotes && char === delimiter) {
|
|
657
|
-
// Конец поля
|
|
658
|
-
fields.push(currentField);
|
|
659
|
-
currentField = '';
|
|
660
|
-
continue;
|
|
661
|
-
}
|
|
662
|
-
currentField += char;
|
|
663
|
-
}
|
|
664
|
-
|
|
665
|
-
// Обработка незавершенного экранирования
|
|
666
|
-
if (escapeNext) {
|
|
667
|
-
currentField += '\\';
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
// Добавление последнего поля
|
|
671
|
-
fields.push(currentField);
|
|
672
|
-
|
|
673
|
-
// Проверка незакрытых кавычек
|
|
674
|
-
if (insideQuotes) {
|
|
675
|
-
throw new ParsingError('Unclosed quotes in CSV', lineNumber);
|
|
676
|
-
}
|
|
677
|
-
|
|
678
|
-
// Валидация количества полей
|
|
679
|
-
if (fields.length === 0) {
|
|
680
|
-
throw new ParsingError('No fields found', lineNumber);
|
|
681
|
-
}
|
|
682
|
-
return fields;
|
|
455
|
+
async function jsonToCsvAsync$1(data, options = {}) {
|
|
456
|
+
return jsonToCsv$1(data, options);
|
|
683
457
|
}
|
|
684
|
-
|
|
685
458
|
/**
|
|
686
|
-
*
|
|
687
|
-
*
|
|
459
|
+
* Создает итератор для потоковой конвертации JSON в CSV
|
|
460
|
+
*
|
|
461
|
+
* @param data - Массив объектов или async итератор
|
|
462
|
+
* @param options - Опции конвертации
|
|
463
|
+
* @returns AsyncIterator с CSV чанками
|
|
688
464
|
*/
|
|
689
|
-
function
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
465
|
+
async function* jsonToCsvIterator(data, options = {}) {
|
|
466
|
+
validateInput(Array.isArray(data) ? data : [], options);
|
|
467
|
+
const delimiter = options.delimiter || ';';
|
|
468
|
+
const includeHeaders = options.includeHeaders !== false;
|
|
469
|
+
const preventInjection = options.preventCsvInjection !== false;
|
|
470
|
+
const rfc4180Compliant = options.rfc4180Compliant !== false;
|
|
471
|
+
const normalizeQuotes = options.normalizeQuotes !== false;
|
|
472
|
+
let allKeys = [];
|
|
473
|
+
let renameMap = {};
|
|
474
|
+
// Если данные - массив, обрабатываем как массив
|
|
475
|
+
if (Array.isArray(data)) {
|
|
476
|
+
if (data.length === 0) {
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
allKeys = extractAllKeys(data);
|
|
480
|
+
renameMap = options.renameMap || {};
|
|
481
|
+
const finalKeys = allKeys.map(key => renameMap[key] || key);
|
|
482
|
+
// Заголовки
|
|
483
|
+
if (includeHeaders) {
|
|
484
|
+
const headerLine = finalKeys.map(key => escapeCsvValue(key, preventInjection)).join(delimiter);
|
|
485
|
+
yield headerLine + (rfc4180Compliant ? '\r\n' : '\n');
|
|
486
|
+
}
|
|
487
|
+
// Данные
|
|
488
|
+
for (const item of data) {
|
|
489
|
+
const rowValues = allKeys.map(key => {
|
|
490
|
+
const value = item?.[key];
|
|
491
|
+
const normalized = normalizeValueForCsv$1(value, key, normalizeQuotes);
|
|
492
|
+
return escapeCsvValue(normalized, preventInjection);
|
|
493
|
+
});
|
|
494
|
+
yield rowValues.join(delimiter) + (rfc4180Compliant ? '\r\n' : '\n');
|
|
495
|
+
}
|
|
710
496
|
}
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
497
|
+
else {
|
|
498
|
+
// Для async итератора нужна другая логика
|
|
499
|
+
throw new ValidationError('Async iterators not yet implemented in browser version');
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Асинхронная версия jsonToCsvIterator (псевдоним)
|
|
504
|
+
*/
|
|
505
|
+
const jsonToCsvIteratorAsync = jsonToCsvIterator;
|
|
506
|
+
/**
|
|
507
|
+
* Безопасная конвертация с обработкой ошибок
|
|
508
|
+
*
|
|
509
|
+
* @param data - Массив объектов
|
|
510
|
+
* @param options - Опции конвертации
|
|
511
|
+
* @returns CSV строка или null при ошибке
|
|
512
|
+
*/
|
|
513
|
+
function jsonToCsvSafe(data, options = {}) {
|
|
514
|
+
try {
|
|
515
|
+
return jsonToCsv$1(data, options);
|
|
718
516
|
}
|
|
719
|
-
|
|
720
|
-
|
|
517
|
+
catch (error) {
|
|
518
|
+
console.error('JSON to CSV conversion error:', error);
|
|
519
|
+
return null;
|
|
721
520
|
}
|
|
722
|
-
}
|
|
723
|
-
|
|
724
|
-
// Пустые строки как null
|
|
725
|
-
if (result === '') {
|
|
726
|
-
return null;
|
|
727
|
-
}
|
|
728
|
-
return result;
|
|
729
|
-
}
|
|
730
|
-
function isSimpleCsv(csv) {
|
|
731
|
-
return csv.indexOf('"') === -1 && csv.indexOf('\\') === -1;
|
|
732
|
-
}
|
|
733
|
-
function parseSimpleCsv(csv, delimiter, options) {
|
|
734
|
-
const {
|
|
735
|
-
hasHeaders = true,
|
|
736
|
-
renameMap = {},
|
|
737
|
-
trim = true,
|
|
738
|
-
parseNumbers = false,
|
|
739
|
-
parseBooleans = false,
|
|
740
|
-
maxRows
|
|
741
|
-
} = options;
|
|
742
|
-
const result = [];
|
|
743
|
-
let headers = null;
|
|
744
|
-
let fieldStart = 0;
|
|
745
|
-
let currentRow = [];
|
|
746
|
-
let rowHasData = false;
|
|
747
|
-
let rowCount = 0;
|
|
748
|
-
const finalizeRow = fields => {
|
|
749
|
-
if (fields.length === 1 && fields[0].trim() === '') {
|
|
750
|
-
return;
|
|
751
|
-
}
|
|
752
|
-
if (!headers) {
|
|
753
|
-
if (hasHeaders) {
|
|
754
|
-
headers = fields.map(header => {
|
|
755
|
-
const trimmed = trim ? header.trim() : header;
|
|
756
|
-
return renameMap[trimmed] || trimmed;
|
|
757
|
-
});
|
|
758
|
-
return;
|
|
759
|
-
}
|
|
760
|
-
headers = fields.map((_, index) => `column${index + 1}`);
|
|
761
|
-
}
|
|
762
|
-
rowCount++;
|
|
763
|
-
if (maxRows && rowCount > maxRows) {
|
|
764
|
-
throw new LimitError(`CSV size exceeds maximum limit of ${maxRows} rows`, maxRows, rowCount);
|
|
765
|
-
}
|
|
766
|
-
const row = {};
|
|
767
|
-
const fieldCount = Math.min(fields.length, headers.length);
|
|
768
|
-
for (let i = 0; i < fieldCount; i++) {
|
|
769
|
-
row[headers[i]] = parseCsvValue(fields[i], {
|
|
770
|
-
trim,
|
|
771
|
-
parseNumbers,
|
|
772
|
-
parseBooleans
|
|
773
|
-
});
|
|
774
|
-
}
|
|
775
|
-
result.push(row);
|
|
776
|
-
};
|
|
777
|
-
let i = 0;
|
|
778
|
-
while (i <= csv.length) {
|
|
779
|
-
const char = i < csv.length ? csv[i] : '\n';
|
|
780
|
-
if (char !== '\r' && char !== '\n' && char !== ' ' && char !== '\t') {
|
|
781
|
-
rowHasData = true;
|
|
782
|
-
}
|
|
783
|
-
if (char === delimiter || char === '\n' || char === '\r' || i === csv.length) {
|
|
784
|
-
const field = csv.slice(fieldStart, i);
|
|
785
|
-
currentRow.push(field);
|
|
786
|
-
if (char === '\n' || char === '\r' || i === csv.length) {
|
|
787
|
-
if (rowHasData || currentRow.length > 1) {
|
|
788
|
-
finalizeRow(currentRow);
|
|
789
|
-
}
|
|
790
|
-
currentRow = [];
|
|
791
|
-
rowHasData = false;
|
|
792
|
-
}
|
|
793
|
-
if (char === '\r' && csv[i + 1] === '\n') {
|
|
794
|
-
i++;
|
|
795
|
-
}
|
|
796
|
-
fieldStart = i + 1;
|
|
797
|
-
}
|
|
798
|
-
i++;
|
|
799
|
-
}
|
|
800
|
-
return result;
|
|
801
521
|
}
|
|
802
|
-
|
|
803
522
|
/**
|
|
804
|
-
*
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
*/
|
|
810
|
-
function autoDetectDelimiter(csv, candidates = [';', ',', '\t', '|']) {
|
|
811
|
-
if (!csv || typeof csv !== 'string') {
|
|
812
|
-
return ';'; // значение по умолчанию
|
|
813
|
-
}
|
|
814
|
-
const lines = csv.split('\n').filter(line => line.trim().length > 0);
|
|
815
|
-
if (lines.length === 0) {
|
|
816
|
-
return ';'; // значение по умолчанию
|
|
817
|
-
}
|
|
818
|
-
|
|
819
|
-
// Использование первой непустой строки для определения
|
|
820
|
-
const firstLine = lines[0];
|
|
821
|
-
const counts = {};
|
|
822
|
-
candidates.forEach(delim => {
|
|
823
|
-
const escapedDelim = delim.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
824
|
-
const regex = new RegExp(escapedDelim, 'g');
|
|
825
|
-
const matches = firstLine.match(regex);
|
|
826
|
-
counts[delim] = matches ? matches.length : 0;
|
|
827
|
-
});
|
|
828
|
-
|
|
829
|
-
// Поиск разделителя с максимальным количеством
|
|
830
|
-
let maxCount = -1;
|
|
831
|
-
let detectedDelimiter = ';'; // значение по умолчанию
|
|
832
|
-
|
|
833
|
-
for (const [delim, count] of Object.entries(counts)) {
|
|
834
|
-
if (count > maxCount) {
|
|
835
|
-
maxCount = count;
|
|
836
|
-
detectedDelimiter = delim;
|
|
523
|
+
* Асинхронная версия jsonToCsvSafe
|
|
524
|
+
*/
|
|
525
|
+
async function jsonToCsvSafeAsync(data, options = {}) {
|
|
526
|
+
try {
|
|
527
|
+
return await jsonToCsvAsync$1(data, options);
|
|
837
528
|
}
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
529
|
+
catch (error) {
|
|
530
|
+
console.error('JSON to CSV conversion error:', error);
|
|
531
|
+
return null;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
// Экспорт для Node.js совместимости
|
|
535
|
+
if (typeof module !== 'undefined' && module.exports) {
|
|
536
|
+
module.exports = {
|
|
537
|
+
jsonToCsv: jsonToCsv$1,
|
|
538
|
+
jsonToCsvAsync: jsonToCsvAsync$1,
|
|
539
|
+
jsonToCsvIterator,
|
|
540
|
+
jsonToCsvIteratorAsync,
|
|
541
|
+
jsonToCsvSafe,
|
|
542
|
+
jsonToCsvSafeAsync
|
|
543
|
+
};
|
|
845
544
|
}
|
|
846
545
|
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
* @param {Object} [options.renameMap={}] - Маппинг переименования заголовков
|
|
857
|
-
* @param {boolean} [options.trim=true] - Обрезать пробелы
|
|
858
|
-
* @param {boolean} [options.parseNumbers=false] - Парсить числовые значения
|
|
859
|
-
* @param {boolean} [options.parseBooleans=false] - Парсить булевы значения
|
|
860
|
-
* @param {number} [options.maxRows] - Максимальное количество строк
|
|
861
|
-
* @returns {Array<Object>} JSON массив
|
|
862
|
-
*/
|
|
863
|
-
function csvToJson(csv, options = {}) {
|
|
864
|
-
return safeExecute(() => {
|
|
865
|
-
// Валидация ввода
|
|
866
|
-
validateCsvInput(csv, options);
|
|
867
|
-
const opts = options && typeof options === 'object' ? options : {};
|
|
868
|
-
const {
|
|
869
|
-
delimiter,
|
|
870
|
-
autoDetect = true,
|
|
871
|
-
candidates = [';', ',', '\t', '|'],
|
|
872
|
-
hasHeaders = true,
|
|
873
|
-
renameMap = {},
|
|
874
|
-
trim = true,
|
|
875
|
-
parseNumbers = false,
|
|
876
|
-
parseBooleans = false,
|
|
877
|
-
maxRows,
|
|
878
|
-
warnExtraFields = true
|
|
879
|
-
} = opts;
|
|
546
|
+
var jsonToCsvBrowser = /*#__PURE__*/Object.freeze({
|
|
547
|
+
__proto__: null,
|
|
548
|
+
jsonToCsv: jsonToCsv$1,
|
|
549
|
+
jsonToCsvAsync: jsonToCsvAsync$1,
|
|
550
|
+
jsonToCsvIterator: jsonToCsvIterator,
|
|
551
|
+
jsonToCsvIteratorAsync: jsonToCsvIteratorAsync,
|
|
552
|
+
jsonToCsvSafe: jsonToCsvSafe,
|
|
553
|
+
jsonToCsvSafeAsync: jsonToCsvSafeAsync
|
|
554
|
+
});
|
|
880
555
|
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
556
|
+
// Браузерная версия CSV to JSON конвертера
|
|
557
|
+
// Адаптирована для работы в браузере без Node.js API
|
|
558
|
+
/**
|
|
559
|
+
* Валидация опций парсинга
|
|
560
|
+
* @private
|
|
561
|
+
*/
|
|
562
|
+
function validateCsvOptions(options) {
|
|
563
|
+
// Validate options
|
|
564
|
+
if (options && typeof options !== 'object') {
|
|
565
|
+
throw new ConfigurationError('Options must be an object');
|
|
885
566
|
}
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
if (csv.trim() === '') {
|
|
890
|
-
return [];
|
|
891
|
-
}
|
|
892
|
-
if (isSimpleCsv(csv)) {
|
|
893
|
-
return parseSimpleCsv(csv, finalDelimiter, {
|
|
894
|
-
hasHeaders,
|
|
895
|
-
renameMap,
|
|
896
|
-
trim,
|
|
897
|
-
parseNumbers,
|
|
898
|
-
parseBooleans,
|
|
899
|
-
maxRows
|
|
900
|
-
});
|
|
567
|
+
// Validate delimiter
|
|
568
|
+
if (options?.delimiter && typeof options.delimiter !== 'string') {
|
|
569
|
+
throw new ConfigurationError('Delimiter must be a string');
|
|
901
570
|
}
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
const lines = [];
|
|
905
|
-
let currentLine = '';
|
|
906
|
-
let insideQuotes = false;
|
|
907
|
-
for (let i = 0; i < csv.length; i++) {
|
|
908
|
-
const char = csv[i];
|
|
909
|
-
if (char === '"') {
|
|
910
|
-
if (insideQuotes && i + 1 < csv.length && csv[i + 1] === '"') {
|
|
911
|
-
// Экранированная кавычка внутри кавычек
|
|
912
|
-
currentLine += '"';
|
|
913
|
-
i++; // Пропустить следующую кавычку
|
|
914
|
-
} else {
|
|
915
|
-
// Переключение режима кавычек
|
|
916
|
-
insideQuotes = !insideQuotes;
|
|
917
|
-
}
|
|
918
|
-
currentLine += char;
|
|
919
|
-
continue;
|
|
920
|
-
}
|
|
921
|
-
if (char === '\n' && !insideQuotes) {
|
|
922
|
-
// Конец строки (вне кавычек)
|
|
923
|
-
lines.push(currentLine);
|
|
924
|
-
currentLine = '';
|
|
925
|
-
continue;
|
|
926
|
-
}
|
|
927
|
-
if (char === '\r') {
|
|
928
|
-
// Игнорировать carriage return
|
|
929
|
-
continue;
|
|
930
|
-
}
|
|
931
|
-
currentLine += char;
|
|
571
|
+
if (options?.delimiter && options.delimiter.length !== 1) {
|
|
572
|
+
throw new ConfigurationError('Delimiter must be a single character');
|
|
932
573
|
}
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
lines.push(currentLine);
|
|
574
|
+
// Validate autoDetect
|
|
575
|
+
if (options?.autoDetect !== undefined && typeof options.autoDetect !== 'boolean') {
|
|
576
|
+
throw new ConfigurationError('autoDetect must be a boolean');
|
|
937
577
|
}
|
|
938
|
-
|
|
939
|
-
|
|
578
|
+
// Validate candidates
|
|
579
|
+
if (options?.candidates && !Array.isArray(options.candidates)) {
|
|
580
|
+
throw new ConfigurationError('candidates must be an array');
|
|
940
581
|
}
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
console.warn('⚠️ Warning: Processing >1M records in memory may be slow.\n' + '💡 Consider using Web Workers for better performance with large files.\n' + '📊 Current size: ' + lines.length.toLocaleString() + ' rows');
|
|
582
|
+
// Validate maxRows
|
|
583
|
+
if (options?.maxRows !== undefined && (typeof options.maxRows !== 'number' || options.maxRows <= 0)) {
|
|
584
|
+
throw new ConfigurationError('maxRows must be a positive number');
|
|
945
585
|
}
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
if (maxRows && lines.length > maxRows) {
|
|
949
|
-
throw new LimitError(`CSV size exceeds maximum limit of ${maxRows} rows`, maxRows, lines.length);
|
|
586
|
+
if (options?.warnExtraFields !== undefined && typeof options.warnExtraFields !== 'boolean') {
|
|
587
|
+
throw new ConfigurationError('warnExtraFields must be a boolean');
|
|
950
588
|
}
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
// Парсинг заголовков если есть
|
|
955
|
-
if (hasHeaders && lines.length > 0) {
|
|
956
|
-
try {
|
|
957
|
-
headers = parseCsvLine(lines[0], 1, finalDelimiter).map(header => {
|
|
958
|
-
const trimmed = trim ? header.trim() : header;
|
|
959
|
-
return renameMap[trimmed] || trimmed;
|
|
960
|
-
});
|
|
961
|
-
startIndex = 1;
|
|
962
|
-
} catch (error) {
|
|
963
|
-
if (error instanceof ParsingError) {
|
|
964
|
-
throw new ParsingError(`Failed to parse headers: ${error.message}`, 1);
|
|
965
|
-
}
|
|
966
|
-
throw error;
|
|
967
|
-
}
|
|
968
|
-
} else {
|
|
969
|
-
// Генерация числовых заголовков из первой строки
|
|
970
|
-
try {
|
|
971
|
-
const firstLineFields = parseCsvLine(lines[0], 1, finalDelimiter);
|
|
972
|
-
headers = firstLineFields.map((_, index) => `column${index + 1}`);
|
|
973
|
-
} catch (error) {
|
|
974
|
-
if (error instanceof ParsingError) {
|
|
975
|
-
throw new ParsingError(`Failed to parse first line: ${error.message}`, 1);
|
|
976
|
-
}
|
|
977
|
-
throw error;
|
|
978
|
-
}
|
|
589
|
+
if (options?.repairRowShifts !== undefined && typeof options.repairRowShifts !== 'boolean') {
|
|
590
|
+
throw new ConfigurationError('repairRowShifts must be a boolean');
|
|
979
591
|
}
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
// Предупреждение о лишних полях
|
|
1005
|
-
const isDev = typeof process !== 'undefined' && process.env && process.env.NODE_ENV === 'development';
|
|
1006
|
-
if (fields.length > headers.length && warnExtraFields && isDev) {
|
|
1007
|
-
console.warn(`[jtcsv] Line ${i + 1}: ${fields.length - headers.length} extra fields ignored`);
|
|
1008
|
-
}
|
|
1009
|
-
result.push(row);
|
|
1010
|
-
} catch (error) {
|
|
1011
|
-
if (error instanceof ParsingError) {
|
|
1012
|
-
throw new ParsingError(`Line ${i + 1}: ${error.message}`, i + 1);
|
|
592
|
+
if (options?.normalizeQuotes !== undefined && typeof options.normalizeQuotes !== 'boolean') {
|
|
593
|
+
throw new ConfigurationError('normalizeQuotes must be a boolean');
|
|
594
|
+
}
|
|
595
|
+
return true;
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* Автоматическое определение разделителя
|
|
599
|
+
* @private
|
|
600
|
+
*/
|
|
601
|
+
function autoDetectDelimiter$1(text, candidates = [',', ';', '\t', '|']) {
|
|
602
|
+
if (!text || typeof text !== 'string') {
|
|
603
|
+
return ',';
|
|
604
|
+
}
|
|
605
|
+
const firstLine = text.split('\n')[0];
|
|
606
|
+
if (!firstLine) {
|
|
607
|
+
return ',';
|
|
608
|
+
}
|
|
609
|
+
let bestCandidate = ',';
|
|
610
|
+
let bestCount = 0;
|
|
611
|
+
for (const candidate of candidates) {
|
|
612
|
+
const count = (firstLine.match(new RegExp(candidate.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g')) || []).length;
|
|
613
|
+
if (count > bestCount) {
|
|
614
|
+
bestCount = count;
|
|
615
|
+
bestCandidate = candidate;
|
|
1013
616
|
}
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
}
|
|
1017
|
-
return result;
|
|
1018
|
-
}, 'PARSE_FAILED', {
|
|
1019
|
-
function: 'csvToJson'
|
|
1020
|
-
});
|
|
1021
|
-
}
|
|
1022
|
-
async function* csvToJsonIterator(input, options = {}) {
|
|
1023
|
-
const opts = options && typeof options === 'object' ? options : {};
|
|
1024
|
-
validateCsvOptions(opts);
|
|
1025
|
-
if (typeof input === 'string') {
|
|
1026
|
-
const rows = csvToJson(input, options);
|
|
1027
|
-
for (const row of rows) {
|
|
1028
|
-
yield row;
|
|
1029
|
-
}
|
|
1030
|
-
return;
|
|
1031
|
-
}
|
|
1032
|
-
const {
|
|
1033
|
-
delimiter,
|
|
1034
|
-
autoDetect = true,
|
|
1035
|
-
candidates = [';', ',', '\t', '|'],
|
|
1036
|
-
hasHeaders = true,
|
|
1037
|
-
renameMap = {},
|
|
1038
|
-
trim = true,
|
|
1039
|
-
parseNumbers = false,
|
|
1040
|
-
parseBooleans = false,
|
|
1041
|
-
maxRows
|
|
1042
|
-
} = opts;
|
|
1043
|
-
const stream = input instanceof Blob && input.stream ? input.stream() : input;
|
|
1044
|
-
if (!stream || typeof stream.getReader !== 'function') {
|
|
1045
|
-
throw new ValidationError('Input must be a CSV string, Blob/File, or ReadableStream');
|
|
1046
|
-
}
|
|
1047
|
-
const reader = stream.getReader();
|
|
1048
|
-
const decoder = new TextDecoder('utf-8');
|
|
1049
|
-
let buffer = '';
|
|
1050
|
-
let insideQuotes = false;
|
|
1051
|
-
let headers = null;
|
|
1052
|
-
let rowCount = 0;
|
|
1053
|
-
let lineNumber = 0;
|
|
1054
|
-
let finalDelimiter = delimiter;
|
|
1055
|
-
let delimiterResolved = Boolean(finalDelimiter);
|
|
1056
|
-
const processFields = fields => {
|
|
1057
|
-
if (fields.length === 1 && fields[0].trim() === '') {
|
|
1058
|
-
return null;
|
|
1059
|
-
}
|
|
1060
|
-
rowCount++;
|
|
1061
|
-
if (maxRows && rowCount > maxRows) {
|
|
1062
|
-
throw new LimitError(`CSV size exceeds maximum limit of ${maxRows} rows`, maxRows, rowCount);
|
|
1063
|
-
}
|
|
1064
|
-
const row = {};
|
|
1065
|
-
const fieldCount = Math.min(fields.length, headers.length);
|
|
1066
|
-
for (let j = 0; j < fieldCount; j++) {
|
|
1067
|
-
row[headers[j]] = parseCsvValue(fields[j], {
|
|
1068
|
-
trim,
|
|
1069
|
-
parseNumbers,
|
|
1070
|
-
parseBooleans
|
|
1071
|
-
});
|
|
1072
|
-
}
|
|
1073
|
-
return row;
|
|
1074
|
-
};
|
|
1075
|
-
const processLine = line => {
|
|
1076
|
-
lineNumber++;
|
|
1077
|
-
let cleanLine = line;
|
|
1078
|
-
if (cleanLine.endsWith('\r')) {
|
|
1079
|
-
cleanLine = cleanLine.slice(0, -1);
|
|
1080
|
-
}
|
|
1081
|
-
if (!delimiterResolved) {
|
|
1082
|
-
if (!finalDelimiter && autoDetect) {
|
|
1083
|
-
finalDelimiter = autoDetectDelimiter(cleanLine, candidates);
|
|
1084
|
-
}
|
|
1085
|
-
finalDelimiter = finalDelimiter || ';';
|
|
1086
|
-
delimiterResolved = true;
|
|
1087
|
-
}
|
|
1088
|
-
if (cleanLine.trim() === '') {
|
|
1089
|
-
return null;
|
|
1090
|
-
}
|
|
1091
|
-
if (!headers) {
|
|
1092
|
-
if (hasHeaders) {
|
|
1093
|
-
headers = parseCsvLine(cleanLine, lineNumber, finalDelimiter).map(header => {
|
|
1094
|
-
const trimmed = trim ? header.trim() : header;
|
|
1095
|
-
return renameMap[trimmed] || trimmed;
|
|
1096
|
-
});
|
|
1097
|
-
return null;
|
|
1098
|
-
}
|
|
1099
|
-
const fields = parseCsvLine(cleanLine, lineNumber, finalDelimiter);
|
|
1100
|
-
headers = fields.map((_, index) => `column${index + 1}`);
|
|
1101
|
-
return processFields(fields);
|
|
1102
|
-
}
|
|
1103
|
-
const fields = parseCsvLine(cleanLine, lineNumber, finalDelimiter);
|
|
1104
|
-
return processFields(fields);
|
|
1105
|
-
};
|
|
1106
|
-
while (true) {
|
|
1107
|
-
const {
|
|
1108
|
-
value,
|
|
1109
|
-
done
|
|
1110
|
-
} = await reader.read();
|
|
1111
|
-
if (done) {
|
|
1112
|
-
break;
|
|
1113
|
-
}
|
|
1114
|
-
buffer += decoder.decode(value, {
|
|
1115
|
-
stream: true
|
|
1116
|
-
});
|
|
1117
|
-
let start = 0;
|
|
1118
|
-
for (let i = 0; i < buffer.length; i++) {
|
|
1119
|
-
const char = buffer[i];
|
|
1120
|
-
if (char === '"') {
|
|
1121
|
-
if (insideQuotes && buffer[i + 1] === '"') {
|
|
1122
|
-
i++;
|
|
1123
|
-
continue;
|
|
1124
|
-
}
|
|
1125
|
-
insideQuotes = !insideQuotes;
|
|
1126
|
-
continue;
|
|
1127
|
-
}
|
|
1128
|
-
if (char === '\n' && !insideQuotes) {
|
|
1129
|
-
const line = buffer.slice(start, i);
|
|
1130
|
-
start = i + 1;
|
|
1131
|
-
const row = processLine(line);
|
|
1132
|
-
if (row) {
|
|
1133
|
-
yield row;
|
|
1134
|
-
}
|
|
1135
|
-
}
|
|
1136
|
-
}
|
|
1137
|
-
buffer = buffer.slice(start);
|
|
1138
|
-
}
|
|
1139
|
-
if (buffer.length > 0) {
|
|
1140
|
-
const row = processLine(buffer);
|
|
1141
|
-
if (row) {
|
|
1142
|
-
yield row;
|
|
1143
|
-
}
|
|
1144
|
-
}
|
|
1145
|
-
if (insideQuotes) {
|
|
1146
|
-
throw new ParsingError('Unclosed quotes in CSV', lineNumber);
|
|
1147
|
-
}
|
|
617
|
+
}
|
|
618
|
+
return bestCandidate;
|
|
1148
619
|
}
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
if (typeof module !== 'undefined' && module.exports) {
|
|
1152
|
-
module.exports = {
|
|
1153
|
-
csvToJson,
|
|
1154
|
-
autoDetectDelimiter,
|
|
1155
|
-
csvToJsonIterator
|
|
1156
|
-
};
|
|
620
|
+
function isEmptyValue(value) {
|
|
621
|
+
return value === undefined || value === null || value === '';
|
|
1157
622
|
}
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
623
|
+
function hasOddQuotes(value) {
|
|
624
|
+
if (typeof value !== 'string') {
|
|
625
|
+
return false;
|
|
626
|
+
}
|
|
627
|
+
let count = 0;
|
|
628
|
+
for (let i = 0; i < value.length; i++) {
|
|
629
|
+
if (value[i] === '"') {
|
|
630
|
+
count++;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
return count % 2 === 1;
|
|
1162
634
|
}
|
|
1163
|
-
function
|
|
1164
|
-
|
|
635
|
+
function hasAnyQuotes(value) {
|
|
636
|
+
return typeof value === 'string' && value.includes('"');
|
|
1165
637
|
}
|
|
1166
|
-
function
|
|
1167
|
-
|
|
638
|
+
function normalizeQuotesInField$1(value) {
|
|
639
|
+
if (typeof value !== 'string') {
|
|
640
|
+
return value;
|
|
641
|
+
}
|
|
642
|
+
// Не нормализуем кавычки в JSON-строках - это ломает структуру JSON
|
|
643
|
+
// Проверяем, выглядит ли значение как JSON (объект или массив)
|
|
644
|
+
if ((value.startsWith('{') && value.endsWith('}')) ||
|
|
645
|
+
(value.startsWith('[') && value.endsWith(']'))) {
|
|
646
|
+
return value; // Возвращаем как есть для JSON
|
|
647
|
+
}
|
|
648
|
+
let normalized = value.replace(/"{2,}/g, '"');
|
|
649
|
+
// Убираем правило, которое ломает JSON: не заменяем "," на ","
|
|
650
|
+
// normalized = normalized.replace(/"\s*,\s*"/g, ',');
|
|
651
|
+
normalized = normalized.replace(/"\n/g, '\n').replace(/\n"/g, '\n');
|
|
652
|
+
if (normalized.length >= 2 && normalized.startsWith('"') && normalized.endsWith('"')) {
|
|
653
|
+
normalized = normalized.slice(1, -1);
|
|
654
|
+
}
|
|
655
|
+
return normalized;
|
|
1168
656
|
}
|
|
1169
|
-
function
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
controller.close();
|
|
1179
|
-
return;
|
|
1180
|
-
}
|
|
1181
|
-
controller.enqueue(value);
|
|
1182
|
-
} catch (error) {
|
|
1183
|
-
controller.error(error);
|
|
1184
|
-
}
|
|
1185
|
-
},
|
|
1186
|
-
cancel() {
|
|
1187
|
-
if (iterator.return) {
|
|
1188
|
-
iterator.return();
|
|
1189
|
-
}
|
|
1190
|
-
}
|
|
1191
|
-
});
|
|
657
|
+
function normalizePhoneValue$1(value) {
|
|
658
|
+
if (typeof value !== 'string') {
|
|
659
|
+
return value;
|
|
660
|
+
}
|
|
661
|
+
const trimmed = value.trim();
|
|
662
|
+
if (trimmed === '') {
|
|
663
|
+
return trimmed;
|
|
664
|
+
}
|
|
665
|
+
return trimmed.replace(/["'\\]/g, '');
|
|
1192
666
|
}
|
|
1193
|
-
function
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
}
|
|
1205
|
-
return
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
667
|
+
function normalizeRowQuotes(row, headers) {
|
|
668
|
+
const normalized = {};
|
|
669
|
+
const phoneKeys = new Set(['phone', 'phonenumber', 'phone_number', 'tel', 'telephone']);
|
|
670
|
+
for (const header of headers) {
|
|
671
|
+
const baseValue = normalizeQuotesInField$1(row[header]);
|
|
672
|
+
if (phoneKeys.has(String(header).toLowerCase())) {
|
|
673
|
+
normalized[header] = normalizePhoneValue$1(baseValue);
|
|
674
|
+
}
|
|
675
|
+
else {
|
|
676
|
+
normalized[header] = baseValue;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
return normalized;
|
|
680
|
+
}
|
|
681
|
+
function looksLikeUserAgent(value) {
|
|
682
|
+
if (typeof value !== 'string') {
|
|
683
|
+
return false;
|
|
684
|
+
}
|
|
685
|
+
return /Mozilla\/|Opera\/|MSIE|AppleWebKit|Gecko|Safari|Chrome\//.test(value);
|
|
686
|
+
}
|
|
687
|
+
function isHexColor(value) {
|
|
688
|
+
return typeof value === 'string' && /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test(value);
|
|
689
|
+
}
|
|
690
|
+
function repairShiftedRows(rows, headers, options = {}) {
|
|
691
|
+
if (!Array.isArray(rows) || rows.length === 0 || headers.length === 0) {
|
|
692
|
+
return rows;
|
|
693
|
+
}
|
|
694
|
+
const headerCount = headers.length;
|
|
695
|
+
const merged = [];
|
|
696
|
+
let index = 0;
|
|
697
|
+
while (index < rows.length) {
|
|
698
|
+
const row = rows[index];
|
|
699
|
+
if (!row || typeof row !== 'object') {
|
|
700
|
+
merged.push(row);
|
|
701
|
+
index++;
|
|
702
|
+
continue;
|
|
703
|
+
}
|
|
704
|
+
const values = headers.map((header) => row[header]);
|
|
705
|
+
let lastNonEmpty = -1;
|
|
706
|
+
for (let i = headerCount - 1; i >= 0; i--) {
|
|
707
|
+
if (!isEmptyValue(values[i])) {
|
|
708
|
+
lastNonEmpty = i;
|
|
709
|
+
break;
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
const missingCount = headerCount - 1 - lastNonEmpty;
|
|
713
|
+
if (lastNonEmpty >= 0 && missingCount > 0 && index + 1 < rows.length) {
|
|
714
|
+
const nextRow = rows[index + 1];
|
|
715
|
+
if (nextRow && typeof nextRow === 'object') {
|
|
716
|
+
const nextValues = headers.map((header) => nextRow[header]);
|
|
717
|
+
const nextTrailingEmpty = nextValues
|
|
718
|
+
.slice(headerCount - missingCount)
|
|
719
|
+
.every((value) => isEmptyValue(value));
|
|
720
|
+
const leadValues = nextValues
|
|
721
|
+
.slice(0, missingCount)
|
|
722
|
+
.filter((value) => !isEmptyValue(value));
|
|
723
|
+
const shouldMerge = nextTrailingEmpty
|
|
724
|
+
&& leadValues.length > 0
|
|
725
|
+
&& (hasOddQuotes(values[lastNonEmpty]) || hasAnyQuotes(values[lastNonEmpty]));
|
|
726
|
+
if (shouldMerge) {
|
|
727
|
+
const toAppend = leadValues.map((value) => String(value));
|
|
728
|
+
if (toAppend.length > 0) {
|
|
729
|
+
const base = isEmptyValue(values[lastNonEmpty]) ? '' : String(values[lastNonEmpty]);
|
|
730
|
+
values[lastNonEmpty] = base ? `${base}\n${toAppend.join('\n')}` : toAppend.join('\n');
|
|
731
|
+
}
|
|
732
|
+
for (let i = 0; i < missingCount; i++) {
|
|
733
|
+
values[lastNonEmpty + 1 + i] = nextValues[missingCount + i];
|
|
734
|
+
}
|
|
735
|
+
const mergedRow = {};
|
|
736
|
+
for (let i = 0; i < headerCount; i++) {
|
|
737
|
+
mergedRow[headers[i]] = values[i];
|
|
738
|
+
}
|
|
739
|
+
merged.push(mergedRow);
|
|
740
|
+
index += 2;
|
|
741
|
+
continue;
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
if (index + 1 < rows.length && headerCount >= 6) {
|
|
746
|
+
const nextRow = rows[index + 1];
|
|
747
|
+
if (nextRow && typeof nextRow === 'object') {
|
|
748
|
+
const nextHex = nextRow[headers[4]];
|
|
749
|
+
const nextUserAgentHead = nextRow[headers[2]];
|
|
750
|
+
const nextUserAgentTail = nextRow[headers[3]];
|
|
751
|
+
const shouldMergeUserAgent = isEmptyValue(values[4])
|
|
752
|
+
&& isEmptyValue(values[5])
|
|
753
|
+
&& isHexColor(nextHex)
|
|
754
|
+
&& (looksLikeUserAgent(nextUserAgentHead) || looksLikeUserAgent(nextUserAgentTail));
|
|
755
|
+
if (shouldMergeUserAgent) {
|
|
756
|
+
const addressParts = [values[3], nextRow[headers[0]], nextRow[headers[1]]]
|
|
757
|
+
.filter((value) => !isEmptyValue(value))
|
|
758
|
+
.map((value) => String(value));
|
|
759
|
+
values[3] = addressParts.join('\n');
|
|
760
|
+
const uaHead = isEmptyValue(nextUserAgentHead) ? '' : String(nextUserAgentHead);
|
|
761
|
+
const uaTail = isEmptyValue(nextUserAgentTail) ? '' : String(nextUserAgentTail);
|
|
762
|
+
const joiner = uaHead && uaTail ? (uaTail.startsWith(' ') ? '' : ',') : '';
|
|
763
|
+
values[4] = uaHead + joiner + uaTail;
|
|
764
|
+
values[5] = String(nextHex);
|
|
765
|
+
const mergedRow = {};
|
|
766
|
+
for (let i = 0; i < headerCount; i++) {
|
|
767
|
+
mergedRow[headers[i]] = values[i];
|
|
768
|
+
}
|
|
769
|
+
merged.push(mergedRow);
|
|
770
|
+
index += 2;
|
|
771
|
+
continue;
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
merged.push(row);
|
|
776
|
+
index++;
|
|
777
|
+
}
|
|
778
|
+
if (options.normalizeQuotes) {
|
|
779
|
+
return merged.map((row) => normalizeRowQuotes(row, headers));
|
|
780
|
+
}
|
|
781
|
+
return merged;
|
|
782
|
+
}
|
|
783
|
+
/**
|
|
784
|
+
* Парсинг CSV строки в массив объектов
|
|
785
|
+
*
|
|
786
|
+
* @param csvText - CSV текст для парсинга
|
|
787
|
+
* @param options - Опции парсинга
|
|
788
|
+
* @returns Массив объектов
|
|
789
|
+
*/
|
|
790
|
+
function csvToJson$1(csvText, options = {}) {
|
|
791
|
+
return safeExecute(() => {
|
|
792
|
+
validateCsvOptions(options);
|
|
793
|
+
if (typeof csvText !== 'string') {
|
|
794
|
+
throw new ValidationError('CSV text must be a string');
|
|
795
|
+
}
|
|
796
|
+
if (csvText.trim() === '') {
|
|
797
|
+
return [];
|
|
798
|
+
}
|
|
799
|
+
// Определение разделителя
|
|
800
|
+
const delimiter = options.delimiter ||
|
|
801
|
+
(options.autoDetect !== false ? autoDetectDelimiter$1(csvText, options.candidates) : ',');
|
|
802
|
+
// Разделение на строки
|
|
803
|
+
const lines = csvText.split('\n').filter(line => line.trim() !== '');
|
|
804
|
+
if (lines.length === 0) {
|
|
805
|
+
return [];
|
|
806
|
+
}
|
|
807
|
+
// Парсинг заголовков
|
|
808
|
+
const headers = lines[0].split(delimiter).map(h => h.trim());
|
|
809
|
+
const { repairRowShifts = true, normalizeQuotes = true } = options || {};
|
|
810
|
+
// Ограничение количества строк
|
|
811
|
+
const maxRows = options.maxRows || Infinity;
|
|
812
|
+
const dataRows = lines.slice(1, Math.min(lines.length, maxRows + 1));
|
|
813
|
+
// Парсинг данных
|
|
814
|
+
const result = [];
|
|
815
|
+
for (let i = 0; i < dataRows.length; i++) {
|
|
816
|
+
const line = dataRows[i];
|
|
817
|
+
const values = line.split(delimiter);
|
|
818
|
+
const row = {};
|
|
819
|
+
for (let j = 0; j < headers.length; j++) {
|
|
820
|
+
const header = headers[j];
|
|
821
|
+
const value = j < values.length ? values[j].trim() : '';
|
|
822
|
+
// Попытка парсинга чисел
|
|
823
|
+
if (/^-?\d+(\.\d+)?$/.test(value)) {
|
|
824
|
+
row[header] = parseFloat(value);
|
|
825
|
+
}
|
|
826
|
+
else if (value.toLowerCase() === 'true' || value.toLowerCase() === 'false') {
|
|
827
|
+
row[header] = value.toLowerCase() === 'true';
|
|
828
|
+
}
|
|
829
|
+
else {
|
|
830
|
+
row[header] = value;
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
result.push(row);
|
|
834
|
+
}
|
|
835
|
+
if (repairRowShifts) {
|
|
836
|
+
return repairShiftedRows(result, headers, { normalizeQuotes });
|
|
837
|
+
}
|
|
838
|
+
if (normalizeQuotes) {
|
|
839
|
+
return result.map((row) => normalizeRowQuotes(row, headers));
|
|
840
|
+
}
|
|
841
|
+
return result;
|
|
842
|
+
});
|
|
843
|
+
}
|
|
844
|
+
/**
|
|
845
|
+
* Асинхронная версия csvToJson
|
|
846
|
+
*/
|
|
847
|
+
async function csvToJsonAsync$1(csvText, options = {}) {
|
|
848
|
+
return csvToJson$1(csvText, options);
|
|
849
|
+
}
|
|
850
|
+
/**
|
|
851
|
+
* Создает итератор для потокового парсинга CSV
|
|
852
|
+
*
|
|
853
|
+
* @param input - CSV текст, File или Blob
|
|
854
|
+
* @param options - Опции парсинга
|
|
855
|
+
* @returns AsyncGenerator
|
|
856
|
+
*/
|
|
857
|
+
async function* csvToJsonIterator$1(input, options = {}) {
|
|
858
|
+
validateCsvOptions(options);
|
|
859
|
+
let csvText;
|
|
860
|
+
if (typeof input === 'string') {
|
|
861
|
+
csvText = input;
|
|
862
|
+
}
|
|
863
|
+
else if (input instanceof File || input instanceof Blob) {
|
|
864
|
+
csvText = await input.text();
|
|
865
|
+
}
|
|
866
|
+
else {
|
|
867
|
+
throw new ValidationError('Input must be string, File or Blob');
|
|
868
|
+
}
|
|
869
|
+
if (csvText.trim() === '') {
|
|
870
|
+
return;
|
|
871
|
+
}
|
|
872
|
+
// Определение разделителя
|
|
873
|
+
const delimiter = options.delimiter ||
|
|
874
|
+
(options.autoDetect !== false ? autoDetectDelimiter$1(csvText, options.candidates) : ',');
|
|
875
|
+
// Разделение на строки
|
|
876
|
+
const lines = csvText.split('\n').filter(line => line.trim() !== '');
|
|
877
|
+
if (lines.length === 0) {
|
|
878
|
+
return;
|
|
879
|
+
}
|
|
880
|
+
// Парсинг заголовков
|
|
881
|
+
const headers = lines[0].split(delimiter).map(h => h.trim());
|
|
882
|
+
const { repairRowShifts = true, normalizeQuotes = true } = options || {};
|
|
883
|
+
// Ограничение количества строк
|
|
884
|
+
const maxRows = options.maxRows || Infinity;
|
|
885
|
+
const dataRows = lines.slice(1, Math.min(lines.length, maxRows + 1));
|
|
886
|
+
// Возврат данных по одной строке
|
|
887
|
+
const parsedRows = [];
|
|
888
|
+
for (let i = 0; i < dataRows.length; i++) {
|
|
889
|
+
const line = dataRows[i];
|
|
890
|
+
const values = line.split(delimiter);
|
|
891
|
+
const row = {};
|
|
892
|
+
for (let j = 0; j < headers.length; j++) {
|
|
893
|
+
const header = headers[j];
|
|
894
|
+
const value = j < values.length ? values[j].trim() : '';
|
|
895
|
+
// Try parsing numbers
|
|
896
|
+
if (/^-?\d+(\.\d+)?$/.test(value)) {
|
|
897
|
+
row[header] = parseFloat(value);
|
|
898
|
+
}
|
|
899
|
+
else if (value.toLowerCase() === 'true' || value.toLowerCase() === 'false') {
|
|
900
|
+
row[header] = value.toLowerCase() === 'true';
|
|
901
|
+
}
|
|
902
|
+
else {
|
|
903
|
+
row[header] = value;
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
parsedRows.push(row);
|
|
907
|
+
}
|
|
908
|
+
const finalRows = repairRowShifts
|
|
909
|
+
? repairShiftedRows(parsedRows, headers, { normalizeQuotes })
|
|
910
|
+
: (normalizeQuotes
|
|
911
|
+
? parsedRows.map((row) => normalizeRowQuotes(row, headers))
|
|
912
|
+
: parsedRows);
|
|
913
|
+
for (const row of finalRows) {
|
|
914
|
+
yield row;
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
/**
|
|
918
|
+
* Асинхронная версия csvToJsonIterator (псевдоним)
|
|
919
|
+
*/
|
|
920
|
+
const csvToJsonIteratorAsync = csvToJsonIterator$1;
|
|
921
|
+
/**
|
|
922
|
+
* Парсинг CSV с обработкой ошибок
|
|
923
|
+
*
|
|
924
|
+
* @param csvText - CSV текст
|
|
925
|
+
* @param options - Опции парсинга
|
|
926
|
+
* @returns Результат парсинга или null при ошибке
|
|
927
|
+
*/
|
|
928
|
+
function parseCsvSafe(csvText, options = {}) {
|
|
929
|
+
try {
|
|
930
|
+
return csvToJson$1(csvText, options);
|
|
931
|
+
}
|
|
932
|
+
catch (error) {
|
|
933
|
+
console.error('CSV parsing error:', error);
|
|
934
|
+
return null;
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
/**
|
|
938
|
+
* Асинхронная версия parseCsvSafe
|
|
939
|
+
*/
|
|
940
|
+
async function parseCsvSafeAsync(csvText, options = {}) {
|
|
941
|
+
try {
|
|
942
|
+
return await csvToJsonAsync$1(csvText, options);
|
|
943
|
+
}
|
|
944
|
+
catch (error) {
|
|
945
|
+
console.error('CSV parsing error:', error);
|
|
946
|
+
return null;
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
// Экспорт для Node.js совместимости
|
|
950
|
+
if (typeof module !== 'undefined' && module.exports) {
|
|
951
|
+
module.exports = {
|
|
952
|
+
csvToJson: csvToJson$1,
|
|
953
|
+
csvToJsonAsync: csvToJsonAsync$1,
|
|
954
|
+
csvToJsonIterator: csvToJsonIterator$1,
|
|
955
|
+
csvToJsonIteratorAsync,
|
|
956
|
+
parseCsvSafe,
|
|
957
|
+
parseCsvSafeAsync,
|
|
958
|
+
autoDetectDelimiter: autoDetectDelimiter$1
|
|
959
|
+
};
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
var csvToJsonBrowser = /*#__PURE__*/Object.freeze({
|
|
963
|
+
__proto__: null,
|
|
964
|
+
csvToJson: csvToJson$1,
|
|
965
|
+
csvToJsonAsync: csvToJsonAsync$1,
|
|
966
|
+
csvToJsonIterator: csvToJsonIterator$1,
|
|
967
|
+
csvToJsonIteratorAsync: csvToJsonIteratorAsync,
|
|
968
|
+
parseCsvSafe: parseCsvSafe,
|
|
969
|
+
parseCsvSafeAsync: parseCsvSafeAsync
|
|
970
|
+
});
|
|
971
|
+
|
|
972
|
+
const PHONE_KEYS = new Set(['phone', 'phonenumber', 'phone_number', 'tel', 'telephone']);
|
|
973
|
+
function isReadableStream(value) {
|
|
974
|
+
return value && typeof value.getReader === 'function';
|
|
975
|
+
}
|
|
976
|
+
function isAsyncIterable(value) {
|
|
977
|
+
return value && typeof value[Symbol.asyncIterator] === 'function';
|
|
978
|
+
}
|
|
979
|
+
function isIterable(value) {
|
|
980
|
+
return value && typeof value[Symbol.iterator] === 'function';
|
|
981
|
+
}
|
|
982
|
+
function createReadableStreamFromIterator(iterator) {
|
|
983
|
+
return new ReadableStream({
|
|
984
|
+
async pull(controller) {
|
|
985
|
+
try {
|
|
986
|
+
const { value, done } = await iterator.next();
|
|
987
|
+
if (done) {
|
|
988
|
+
controller.close();
|
|
989
|
+
return;
|
|
990
|
+
}
|
|
991
|
+
controller.enqueue(value);
|
|
992
|
+
}
|
|
993
|
+
catch (error) {
|
|
994
|
+
controller.error(error);
|
|
995
|
+
}
|
|
996
|
+
},
|
|
997
|
+
cancel() {
|
|
998
|
+
if (iterator.return) {
|
|
999
|
+
iterator.return();
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1236
1002
|
});
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
}
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
throw new ValidationError('Input must be an array, iterable, string, Blob, or ReadableStream');
|
|
1310
|
-
}
|
|
1311
|
-
function validateStreamOptions(options) {
|
|
1312
|
-
if (options && typeof options !== 'object') {
|
|
1313
|
-
throw new ConfigurationError('Options must be an object');
|
|
1314
|
-
}
|
|
1315
|
-
if (options !== null && options !== void 0 && options.delimiter && typeof options.delimiter !== 'string') {
|
|
1316
|
-
throw new ConfigurationError('Delimiter must be a string');
|
|
1317
|
-
}
|
|
1318
|
-
if (options !== null && options !== void 0 && options.delimiter && options.delimiter.length !== 1) {
|
|
1319
|
-
throw new ConfigurationError('Delimiter must be a single character');
|
|
1320
|
-
}
|
|
1321
|
-
if (options !== null && options !== void 0 && options.renameMap && typeof options.renameMap !== 'object') {
|
|
1322
|
-
throw new ConfigurationError('renameMap must be an object');
|
|
1323
|
-
}
|
|
1324
|
-
if ((options === null || options === void 0 ? void 0 : options.maxRecords) !== undefined) {
|
|
1325
|
-
if (typeof options.maxRecords !== 'number' || options.maxRecords <= 0) {
|
|
1326
|
-
throw new ConfigurationError('maxRecords must be a positive number');
|
|
1327
|
-
}
|
|
1328
|
-
}
|
|
1329
|
-
}
|
|
1330
|
-
function escapeCsvValue(value, options) {
|
|
1331
|
-
const {
|
|
1332
|
-
delimiter,
|
|
1333
|
-
preventCsvInjection = true,
|
|
1334
|
-
rfc4180Compliant = true
|
|
1335
|
-
} = options;
|
|
1336
|
-
if (value === null || value === undefined || value === '') {
|
|
1337
|
-
return '';
|
|
1338
|
-
}
|
|
1339
|
-
const stringValue = String(value);
|
|
1340
|
-
let escapedValue = stringValue;
|
|
1341
|
-
if (preventCsvInjection && /^[=+\-@]/.test(stringValue)) {
|
|
1342
|
-
escapedValue = "'" + stringValue;
|
|
1343
|
-
}
|
|
1344
|
-
const needsQuoting = rfc4180Compliant ? escapedValue.includes(delimiter) || escapedValue.includes('"') || escapedValue.includes('\n') || escapedValue.includes('\r') : escapedValue.includes(delimiter) || escapedValue.includes('"') || escapedValue.includes('\n') || escapedValue.includes('\r');
|
|
1345
|
-
if (needsQuoting) {
|
|
1346
|
-
return `"${escapedValue.replace(/"/g, '""')}"`;
|
|
1347
|
-
}
|
|
1348
|
-
return escapedValue;
|
|
1349
|
-
}
|
|
1350
|
-
function buildHeaderState(keys, options) {
|
|
1351
|
-
const renameMap = options.renameMap || {};
|
|
1352
|
-
const template = options.template || {};
|
|
1353
|
-
const originalKeys = Array.isArray(options.headers) ? options.headers : keys;
|
|
1354
|
-
const headers = originalKeys.map(key => renameMap[key] || key);
|
|
1355
|
-
const reverseRenameMap = {};
|
|
1356
|
-
originalKeys.forEach((key, index) => {
|
|
1357
|
-
reverseRenameMap[headers[index]] = key;
|
|
1358
|
-
});
|
|
1359
|
-
let finalHeaders = headers;
|
|
1360
|
-
if (Object.keys(template).length > 0) {
|
|
1361
|
-
const templateHeaders = Object.keys(template).map(key => renameMap[key] || key);
|
|
1362
|
-
const extraHeaders = headers.filter(h => !templateHeaders.includes(h));
|
|
1363
|
-
finalHeaders = [...templateHeaders, ...extraHeaders];
|
|
1364
|
-
}
|
|
1365
|
-
return {
|
|
1366
|
-
headers: finalHeaders,
|
|
1367
|
-
reverseRenameMap
|
|
1368
|
-
};
|
|
1003
|
+
}
|
|
1004
|
+
function detectInputFormat(input, options) {
|
|
1005
|
+
if (options && options.inputFormat) {
|
|
1006
|
+
return options.inputFormat;
|
|
1007
|
+
}
|
|
1008
|
+
if (typeof input === 'string') {
|
|
1009
|
+
const trimmed = input.trim();
|
|
1010
|
+
if (trimmed === '') {
|
|
1011
|
+
return 'unknown';
|
|
1012
|
+
}
|
|
1013
|
+
// Проверка на NDJSON (каждая строка - валидный JSON)
|
|
1014
|
+
if (trimmed.includes('\n')) {
|
|
1015
|
+
const lines = trimmed.split('\n').filter(line => line.trim() !== '');
|
|
1016
|
+
if (lines.length > 0) {
|
|
1017
|
+
try {
|
|
1018
|
+
JSON.parse(lines[0]);
|
|
1019
|
+
return 'ndjson';
|
|
1020
|
+
}
|
|
1021
|
+
catch {
|
|
1022
|
+
// Не NDJSON
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
// Проверка на JSON
|
|
1027
|
+
try {
|
|
1028
|
+
const parsed = JSON.parse(trimmed);
|
|
1029
|
+
if (Array.isArray(parsed) || (parsed && typeof parsed === 'object')) {
|
|
1030
|
+
return 'json';
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
catch {
|
|
1034
|
+
// Не JSON
|
|
1035
|
+
}
|
|
1036
|
+
// Проверка на CSV
|
|
1037
|
+
if (trimmed.includes(',') || trimmed.includes(';') || trimmed.includes('\t')) {
|
|
1038
|
+
return 'csv';
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
return 'unknown';
|
|
1042
|
+
}
|
|
1043
|
+
function normalizeQuotesInField(value) {
|
|
1044
|
+
// Не нормализуем кавычки в JSON-строках - это ломает структуру JSON
|
|
1045
|
+
// Проверяем, выглядит ли значение как JSON (объект или массив)
|
|
1046
|
+
if ((value.startsWith('{') && value.endsWith('}')) ||
|
|
1047
|
+
(value.startsWith('[') && value.endsWith(']'))) {
|
|
1048
|
+
return value; // Возвращаем как есть для JSON
|
|
1049
|
+
}
|
|
1050
|
+
let normalized = value.replace(/"{2,}/g, '"');
|
|
1051
|
+
// Убираем правило, которое ломает JSON: не заменяем "," на ","
|
|
1052
|
+
// normalized = normalized.replace(/"\s*,\s*"/g, ',');
|
|
1053
|
+
normalized = normalized.replace(/"\n/g, '\n').replace(/\n"/g, '\n');
|
|
1054
|
+
if (normalized.length >= 2 && normalized.startsWith('"') && normalized.endsWith('"')) {
|
|
1055
|
+
normalized = normalized.slice(1, -1);
|
|
1056
|
+
}
|
|
1057
|
+
return normalized;
|
|
1058
|
+
}
|
|
1059
|
+
function normalizePhoneValue(value) {
|
|
1060
|
+
const trimmed = value.trim();
|
|
1061
|
+
if (trimmed === '') {
|
|
1062
|
+
return trimmed;
|
|
1063
|
+
}
|
|
1064
|
+
return trimmed.replace(/["'\\]/g, '');
|
|
1065
|
+
}
|
|
1066
|
+
function normalizeValueForCsv(value, key, normalizeQuotes) {
|
|
1067
|
+
if (!normalizeQuotes || typeof value !== 'string') {
|
|
1068
|
+
return value;
|
|
1069
|
+
}
|
|
1070
|
+
const base = normalizeQuotesInField(value);
|
|
1071
|
+
if (key && PHONE_KEYS.has(String(key).toLowerCase())) {
|
|
1072
|
+
return normalizePhoneValue(base);
|
|
1073
|
+
}
|
|
1074
|
+
return base;
|
|
1369
1075
|
}
|
|
1370
1076
|
async function* jsonToCsvChunkIterator(input, options = {}) {
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
if (
|
|
1404
|
-
|
|
1405
|
-
}
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1077
|
+
const format = detectInputFormat(input, options);
|
|
1078
|
+
if (format === 'csv') {
|
|
1079
|
+
throw new ValidationError('Input appears to be CSV, not JSON');
|
|
1080
|
+
}
|
|
1081
|
+
// Вспомогательная функция для создания асинхронного итератора
|
|
1082
|
+
function toAsyncIterator(iterable) {
|
|
1083
|
+
if (isAsyncIterable(iterable)) {
|
|
1084
|
+
return iterable[Symbol.asyncIterator]();
|
|
1085
|
+
}
|
|
1086
|
+
if (isIterable(iterable)) {
|
|
1087
|
+
const syncIterator = iterable[Symbol.iterator]();
|
|
1088
|
+
return {
|
|
1089
|
+
next: () => Promise.resolve(syncIterator.next()),
|
|
1090
|
+
return: syncIterator.return ? () => Promise.resolve(syncIterator.return()) : undefined,
|
|
1091
|
+
throw: syncIterator.throw ? (error) => Promise.resolve(syncIterator.throw(error)) : undefined
|
|
1092
|
+
};
|
|
1093
|
+
}
|
|
1094
|
+
throw new ValidationError('Input is not iterable');
|
|
1095
|
+
}
|
|
1096
|
+
let iterator;
|
|
1097
|
+
if (isAsyncIterable(input) || isIterable(input)) {
|
|
1098
|
+
iterator = toAsyncIterator(input);
|
|
1099
|
+
}
|
|
1100
|
+
else if (typeof input === 'string') {
|
|
1101
|
+
const parsed = JSON.parse(input);
|
|
1102
|
+
if (Array.isArray(parsed)) {
|
|
1103
|
+
iterator = toAsyncIterator(parsed);
|
|
1104
|
+
}
|
|
1105
|
+
else {
|
|
1106
|
+
iterator = toAsyncIterator([parsed]);
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
else if (Array.isArray(input)) {
|
|
1110
|
+
iterator = toAsyncIterator(input);
|
|
1111
|
+
}
|
|
1112
|
+
else {
|
|
1113
|
+
iterator = toAsyncIterator([input]);
|
|
1114
|
+
}
|
|
1115
|
+
const delimiter = options.delimiter || ';';
|
|
1116
|
+
const includeHeaders = options.includeHeaders !== false;
|
|
1117
|
+
const preventInjection = options.preventCsvInjection !== false;
|
|
1118
|
+
const normalizeQuotes = options.normalizeQuotes !== false;
|
|
1119
|
+
const isPotentialFormula = (input) => {
|
|
1120
|
+
let idx = 0;
|
|
1121
|
+
while (idx < input.length) {
|
|
1122
|
+
const code = input.charCodeAt(idx);
|
|
1123
|
+
if (code === 32 || code === 9 || code === 10 || code === 13 || code === 0xfeff) {
|
|
1124
|
+
idx++;
|
|
1125
|
+
continue;
|
|
1126
|
+
}
|
|
1127
|
+
break;
|
|
1128
|
+
}
|
|
1129
|
+
if (idx < input.length && (input[idx] === '"' || input[idx] === "'")) {
|
|
1130
|
+
idx++;
|
|
1131
|
+
while (idx < input.length) {
|
|
1132
|
+
const code = input.charCodeAt(idx);
|
|
1133
|
+
if (code === 32 || code === 9) {
|
|
1134
|
+
idx++;
|
|
1135
|
+
continue;
|
|
1136
|
+
}
|
|
1137
|
+
break;
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
if (idx >= input.length) {
|
|
1141
|
+
return false;
|
|
1142
|
+
}
|
|
1143
|
+
const char = input[idx];
|
|
1144
|
+
return char === '=' || char === '+' || char === '-' || char === '@';
|
|
1145
|
+
};
|
|
1146
|
+
let isFirstChunk = true;
|
|
1147
|
+
let headers = [];
|
|
1148
|
+
while (true) {
|
|
1149
|
+
const { value, done } = await iterator.next();
|
|
1150
|
+
if (done)
|
|
1151
|
+
break;
|
|
1152
|
+
const item = value;
|
|
1153
|
+
if (isFirstChunk) {
|
|
1154
|
+
// Извлечение заголовков из первого элемента
|
|
1155
|
+
headers = Object.keys(item);
|
|
1156
|
+
if (includeHeaders) {
|
|
1157
|
+
const headerLine = headers.map(header => {
|
|
1158
|
+
const escaped = header.includes('"') ? `"${header.replace(/"/g, '""')}"` : header;
|
|
1159
|
+
return preventInjection && isPotentialFormula(escaped) ? `'${escaped}` : escaped;
|
|
1160
|
+
}).join(delimiter);
|
|
1161
|
+
yield headerLine + '\n';
|
|
1162
|
+
}
|
|
1163
|
+
isFirstChunk = false;
|
|
1164
|
+
}
|
|
1165
|
+
const row = headers.map(header => {
|
|
1166
|
+
const value = item[header];
|
|
1167
|
+
const normalized = normalizeValueForCsv(value, header, normalizeQuotes);
|
|
1168
|
+
const strValue = normalized === null || normalized === undefined ? '' : String(normalized);
|
|
1169
|
+
if (strValue.includes('"') || strValue.includes('\n') || strValue.includes('\r') || strValue.includes(delimiter)) {
|
|
1170
|
+
return `"${strValue.replace(/"/g, '""')}"`;
|
|
1171
|
+
}
|
|
1172
|
+
if (preventInjection && isPotentialFormula(strValue)) {
|
|
1173
|
+
return `'${strValue}`;
|
|
1174
|
+
}
|
|
1175
|
+
return strValue;
|
|
1176
|
+
}).join(delimiter);
|
|
1177
|
+
yield row + '\n';
|
|
1178
|
+
}
|
|
1433
1179
|
}
|
|
1434
1180
|
async function* jsonToNdjsonChunkIterator(input, options = {}) {
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1181
|
+
const format = detectInputFormat(input, options);
|
|
1182
|
+
// Вспомогательная функция для создания асинхронного итератора
|
|
1183
|
+
function toAsyncIterator(iterable) {
|
|
1184
|
+
if (isAsyncIterable(iterable)) {
|
|
1185
|
+
return iterable[Symbol.asyncIterator]();
|
|
1186
|
+
}
|
|
1187
|
+
if (isIterable(iterable)) {
|
|
1188
|
+
const syncIterator = iterable[Symbol.iterator]();
|
|
1189
|
+
return {
|
|
1190
|
+
next: () => Promise.resolve(syncIterator.next()),
|
|
1191
|
+
return: syncIterator.return ? () => Promise.resolve(syncIterator.return()) : undefined,
|
|
1192
|
+
throw: syncIterator.throw ? (error) => Promise.resolve(syncIterator.throw(error)) : undefined
|
|
1193
|
+
};
|
|
1194
|
+
}
|
|
1195
|
+
throw new ValidationError('Input is not iterable');
|
|
1196
|
+
}
|
|
1197
|
+
let iterator;
|
|
1198
|
+
if (isAsyncIterable(input) || isIterable(input)) {
|
|
1199
|
+
iterator = toAsyncIterator(input);
|
|
1200
|
+
}
|
|
1201
|
+
else if (typeof input === 'string') {
|
|
1202
|
+
if (format === 'ndjson') {
|
|
1203
|
+
const lines = input.split('\n').filter(line => line.trim() !== '');
|
|
1204
|
+
iterator = toAsyncIterator(lines);
|
|
1205
|
+
}
|
|
1206
|
+
else {
|
|
1207
|
+
const parsed = JSON.parse(input);
|
|
1208
|
+
if (Array.isArray(parsed)) {
|
|
1209
|
+
iterator = toAsyncIterator(parsed);
|
|
1210
|
+
}
|
|
1211
|
+
else {
|
|
1212
|
+
iterator = toAsyncIterator([parsed]);
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
else if (Array.isArray(input)) {
|
|
1217
|
+
iterator = toAsyncIterator(input);
|
|
1218
|
+
}
|
|
1219
|
+
else {
|
|
1220
|
+
iterator = toAsyncIterator([input]);
|
|
1221
|
+
}
|
|
1222
|
+
while (true) {
|
|
1223
|
+
const { value, done } = await iterator.next();
|
|
1224
|
+
if (done)
|
|
1225
|
+
break;
|
|
1226
|
+
let jsonStr;
|
|
1227
|
+
if (typeof value === 'string') {
|
|
1228
|
+
try {
|
|
1229
|
+
// Проверяем, является ли строка валидным JSON
|
|
1230
|
+
JSON.parse(value);
|
|
1231
|
+
jsonStr = value;
|
|
1232
|
+
}
|
|
1233
|
+
catch {
|
|
1234
|
+
// Если нет, сериализуем как JSON
|
|
1235
|
+
jsonStr = JSON.stringify(value);
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
else {
|
|
1239
|
+
jsonStr = JSON.stringify(value);
|
|
1240
|
+
}
|
|
1241
|
+
yield jsonStr + '\n';
|
|
1439
1242
|
}
|
|
1440
|
-
yield JSON.stringify(item) + '\n';
|
|
1441
|
-
}
|
|
1442
1243
|
}
|
|
1443
1244
|
async function* csvToJsonChunkIterator(input, options = {}) {
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
if (
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
}
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1245
|
+
if (typeof input === 'string') {
|
|
1246
|
+
// Используем csvToJsonIterator из csv-to-json-browser
|
|
1247
|
+
yield* csvToJsonIterator$1(input, options);
|
|
1248
|
+
}
|
|
1249
|
+
else if (input instanceof File || input instanceof Blob) {
|
|
1250
|
+
const text = await input.text();
|
|
1251
|
+
yield* csvToJsonIterator$1(text, options);
|
|
1252
|
+
}
|
|
1253
|
+
else if (isReadableStream(input)) {
|
|
1254
|
+
const reader = input.getReader();
|
|
1255
|
+
const decoder = new TextDecoder();
|
|
1256
|
+
let buffer = '';
|
|
1257
|
+
try {
|
|
1258
|
+
while (true) {
|
|
1259
|
+
const { value, done } = await reader.read();
|
|
1260
|
+
if (done)
|
|
1261
|
+
break;
|
|
1262
|
+
buffer += decoder.decode(value, { stream: true });
|
|
1263
|
+
// Обработка буфера по строкам
|
|
1264
|
+
const lines = buffer.split('\n');
|
|
1265
|
+
buffer = lines.pop() || '';
|
|
1266
|
+
// TODO: Реализовать парсинг CSV из чанков
|
|
1267
|
+
// Пока просто возвращаем сырые строки
|
|
1268
|
+
for (const line of lines) {
|
|
1269
|
+
if (line.trim()) {
|
|
1270
|
+
yield { raw: line };
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
// Обработка остатка буфера
|
|
1275
|
+
if (buffer.trim()) {
|
|
1276
|
+
yield { raw: buffer };
|
|
1277
|
+
}
|
|
1278
|
+
}
|
|
1279
|
+
finally {
|
|
1280
|
+
reader.releaseLock();
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
else {
|
|
1284
|
+
throw new ValidationError('Unsupported input type for CSV streaming');
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
function jsonToCsvStream$1(input, options = {}) {
|
|
1288
|
+
const iterator = jsonToCsvChunkIterator(input, options);
|
|
1289
|
+
return createReadableStreamFromIterator(iterator);
|
|
1290
|
+
}
|
|
1291
|
+
function jsonToNdjsonStream$1(input, options = {}) {
|
|
1292
|
+
const iterator = jsonToNdjsonChunkIterator(input, options);
|
|
1293
|
+
return createReadableStreamFromIterator(iterator);
|
|
1294
|
+
}
|
|
1295
|
+
function csvToJsonStream$1(input, options = {}) {
|
|
1296
|
+
const iterator = csvToJsonChunkIterator(input, options);
|
|
1297
|
+
return createReadableStreamFromIterator(iterator);
|
|
1298
|
+
}
|
|
1299
|
+
/**
|
|
1300
|
+
* Асинхронная версия jsonToCsvStream
|
|
1301
|
+
*/
|
|
1302
|
+
async function jsonToCsvStreamAsync(input, options = {}) {
|
|
1303
|
+
return jsonToCsvStream$1(input, options);
|
|
1304
|
+
}
|
|
1305
|
+
/**
|
|
1306
|
+
* Асинхронная версия jsonToNdjsonStream
|
|
1307
|
+
*/
|
|
1308
|
+
async function jsonToNdjsonStreamAsync(input, options = {}) {
|
|
1309
|
+
return jsonToNdjsonStream$1(input, options);
|
|
1310
|
+
}
|
|
1311
|
+
/**
|
|
1312
|
+
* Асинхронная версия csvToJsonStream
|
|
1313
|
+
*/
|
|
1314
|
+
async function csvToJsonStreamAsync(input, options = {}) {
|
|
1315
|
+
return csvToJsonStream$1(input, options);
|
|
1474
1316
|
}
|
|
1317
|
+
// Экспорт для Node.js совместимости
|
|
1475
1318
|
if (typeof module !== 'undefined' && module.exports) {
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1319
|
+
module.exports = {
|
|
1320
|
+
jsonToCsvStream: jsonToCsvStream$1,
|
|
1321
|
+
jsonToCsvStreamAsync,
|
|
1322
|
+
jsonToNdjsonStream: jsonToNdjsonStream$1,
|
|
1323
|
+
jsonToNdjsonStreamAsync,
|
|
1324
|
+
csvToJsonStream: csvToJsonStream$1,
|
|
1325
|
+
csvToJsonStreamAsync,
|
|
1326
|
+
createReadableStreamFromIterator
|
|
1327
|
+
};
|
|
1481
1328
|
}
|
|
1482
1329
|
|
|
1483
1330
|
// Браузерные специфичные функции для jtcsv
|
|
1484
1331
|
// Функции, которые работают только в браузере
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
1332
|
/**
|
|
1488
1333
|
* Скачивает JSON данные как CSV файл
|
|
1489
|
-
*
|
|
1490
|
-
* @param
|
|
1491
|
-
* @param
|
|
1492
|
-
* @param
|
|
1493
|
-
*
|
|
1494
|
-
*
|
|
1334
|
+
*
|
|
1335
|
+
* @param data - Массив объектов для конвертации
|
|
1336
|
+
* @param filename - Имя файла для скачивания (по умолчанию 'data.csv')
|
|
1337
|
+
* @param options - Опции для jsonToCsv
|
|
1338
|
+
*
|
|
1495
1339
|
* @example
|
|
1496
1340
|
* const data = [
|
|
1497
1341
|
* { id: 1, name: 'John' },
|
|
@@ -1500,770 +1344,869 @@ if (typeof module !== 'undefined' && module.exports) {
|
|
|
1500
1344
|
* downloadAsCsv(data, 'users.csv', { delimiter: ',' });
|
|
1501
1345
|
*/
|
|
1502
1346
|
function downloadAsCsv(data, filename = 'data.csv', options = {}) {
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
// Создание URL для Blob
|
|
1530
|
-
const url = URL.createObjectURL(blob);
|
|
1531
|
-
|
|
1532
|
-
// Настройка ссылки
|
|
1533
|
-
link.setAttribute('href', url);
|
|
1534
|
-
link.setAttribute('download', filename);
|
|
1535
|
-
link.style.visibility = 'hidden';
|
|
1536
|
-
|
|
1537
|
-
// Добавление в DOM и клик
|
|
1538
|
-
document.body.appendChild(link);
|
|
1539
|
-
link.click();
|
|
1540
|
-
|
|
1541
|
-
// Очистка
|
|
1542
|
-
setTimeout(() => {
|
|
1347
|
+
// Проверка что мы в браузере
|
|
1348
|
+
if (typeof window === 'undefined') {
|
|
1349
|
+
throw new ValidationError('downloadAsCsv() работает только в браузере. Используйте saveAsCsv() в Node.js');
|
|
1350
|
+
}
|
|
1351
|
+
// Валидация имени файла
|
|
1352
|
+
if (typeof filename !== 'string' || filename.trim() === '') {
|
|
1353
|
+
throw new ValidationError('Filename must be a non-empty string');
|
|
1354
|
+
}
|
|
1355
|
+
// Добавление расширения .csv если его нет
|
|
1356
|
+
if (!filename.toLowerCase().endsWith('.csv')) {
|
|
1357
|
+
filename += '.csv';
|
|
1358
|
+
}
|
|
1359
|
+
// Конвертация в CSV
|
|
1360
|
+
const csv = jsonToCsv$1(data, options);
|
|
1361
|
+
// Создание Blob
|
|
1362
|
+
const blob = new Blob([csv], {
|
|
1363
|
+
type: 'text/csv;charset=utf-8;'
|
|
1364
|
+
});
|
|
1365
|
+
// Создание ссылки для скачивания
|
|
1366
|
+
const link = document.createElement('a');
|
|
1367
|
+
const url = URL.createObjectURL(blob);
|
|
1368
|
+
link.setAttribute('href', url);
|
|
1369
|
+
link.setAttribute('download', filename);
|
|
1370
|
+
link.style.visibility = 'hidden';
|
|
1371
|
+
document.body.appendChild(link);
|
|
1372
|
+
link.click();
|
|
1543
1373
|
document.body.removeChild(link);
|
|
1544
|
-
URL
|
|
1545
|
-
|
|
1374
|
+
// Освобождение URL
|
|
1375
|
+
setTimeout(() => URL.revokeObjectURL(url), 100);
|
|
1546
1376
|
}
|
|
1547
|
-
|
|
1548
1377
|
/**
|
|
1549
|
-
*
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
*
|
|
1556
|
-
*
|
|
1557
|
-
*
|
|
1558
|
-
*
|
|
1378
|
+
* Асинхронная версия downloadAsCsv
|
|
1379
|
+
*/
|
|
1380
|
+
async function downloadAsCsvAsync$1(data, filename = 'data.csv', options = {}) {
|
|
1381
|
+
return downloadAsCsv(data, filename, options);
|
|
1382
|
+
}
|
|
1383
|
+
/**
|
|
1384
|
+
* Парсит CSV файл из input[type="file"]
|
|
1385
|
+
*
|
|
1386
|
+
* @param file - File объект из input
|
|
1387
|
+
* @param options - Опции для csvToJson
|
|
1388
|
+
* @returns Promise с распарсенными данными
|
|
1559
1389
|
*/
|
|
1560
1390
|
async function parseCsvFile(file, options = {}) {
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
throw new ValidationError('Input must be a File object');
|
|
1569
|
-
}
|
|
1570
|
-
|
|
1571
|
-
// Проверка расширения файла
|
|
1572
|
-
if (!file.name.toLowerCase().endsWith('.csv')) {
|
|
1573
|
-
throw new ValidationError('File must have .csv extension');
|
|
1574
|
-
}
|
|
1575
|
-
|
|
1576
|
-
// Проверка размера файла (предупреждение для больших файлов)
|
|
1577
|
-
const MAX_SIZE_WARNING = 50 * 1024 * 1024; // 50MB
|
|
1578
|
-
if (file.size > MAX_SIZE_WARNING && process.env.NODE_ENV !== 'production') {
|
|
1579
|
-
console.warn(`⚠️ Warning: Processing large file (${(file.size / 1024 / 1024).toFixed(2)}MB).\n` + '💡 Consider using Web Workers for better performance.\n' + '🔧 Tip: Use parseCSVWithWorker() for files > 10MB.');
|
|
1580
|
-
}
|
|
1581
|
-
return new Promise((resolve, reject) => {
|
|
1582
|
-
const reader = new FileReader();
|
|
1583
|
-
reader.onload = function (event) {
|
|
1584
|
-
try {
|
|
1585
|
-
const csvText = event.target.result;
|
|
1586
|
-
const json = csvToJson(csvText, options);
|
|
1587
|
-
resolve(json);
|
|
1588
|
-
} catch (error) {
|
|
1589
|
-
reject(error);
|
|
1590
|
-
}
|
|
1591
|
-
};
|
|
1592
|
-
reader.onerror = function () {
|
|
1593
|
-
reject(new ValidationError('Ошибка чтения файла'));
|
|
1594
|
-
};
|
|
1595
|
-
reader.onabort = function () {
|
|
1596
|
-
reject(new ValidationError('Чтение файла прервано'));
|
|
1597
|
-
};
|
|
1598
|
-
|
|
1599
|
-
// Чтение как текст
|
|
1600
|
-
reader.readAsText(file, 'UTF-8');
|
|
1601
|
-
});
|
|
1391
|
+
if (!(file instanceof File)) {
|
|
1392
|
+
throw new ValidationError('parseCsvFile() ожидает объект File');
|
|
1393
|
+
}
|
|
1394
|
+
// Чтение файла как текст
|
|
1395
|
+
const text = await file.text();
|
|
1396
|
+
// Парсинг CSV
|
|
1397
|
+
return csvToJson$1(text, options);
|
|
1602
1398
|
}
|
|
1603
|
-
|
|
1604
1399
|
/**
|
|
1605
|
-
*
|
|
1400
|
+
* Парсит CSV файл потоково
|
|
1606
1401
|
*
|
|
1607
|
-
* @param
|
|
1608
|
-
* @param
|
|
1609
|
-
* @returns
|
|
1402
|
+
* @param file - File объект
|
|
1403
|
+
* @param options - Опции для потокового парсинга
|
|
1404
|
+
* @returns AsyncIterator с данными
|
|
1610
1405
|
*/
|
|
1611
1406
|
function parseCsvFileStream(file, options = {}) {
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1407
|
+
if (!(file instanceof File)) {
|
|
1408
|
+
throw new ValidationError('parseCsvFileStream() ожидает объект File');
|
|
1409
|
+
}
|
|
1410
|
+
// Используем csvToJsonIterator из импортированного модуля
|
|
1411
|
+
return csvToJsonIterator$1(file, options);
|
|
1412
|
+
}
|
|
1413
|
+
/**
|
|
1414
|
+
* Создает поток для конвертации JSON в CSV
|
|
1415
|
+
*
|
|
1416
|
+
* @param options - Опции для jsonToCsv
|
|
1417
|
+
* @returns ReadableStream
|
|
1418
|
+
*/
|
|
1419
|
+
function jsonToCsvStream(options = {}) {
|
|
1420
|
+
return jsonToCsvStream$1(options);
|
|
1421
|
+
}
|
|
1422
|
+
/**
|
|
1423
|
+
* Создает поток для конвертации JSON в NDJSON
|
|
1424
|
+
*
|
|
1425
|
+
* @param options - Опции для конвертации
|
|
1426
|
+
* @returns ReadableStream
|
|
1427
|
+
*/
|
|
1428
|
+
function jsonToNdjsonStream(options = {}) {
|
|
1429
|
+
return jsonToNdjsonStream$1(options);
|
|
1430
|
+
}
|
|
1431
|
+
/**
|
|
1432
|
+
* Создает поток для парсинга CSV в JSON
|
|
1433
|
+
*
|
|
1434
|
+
* @param options - Опции для csvToJson
|
|
1435
|
+
* @returns ReadableStream
|
|
1436
|
+
*/
|
|
1437
|
+
function csvToJsonStream(options = {}) {
|
|
1438
|
+
return csvToJsonStream$1(options);
|
|
1439
|
+
}
|
|
1440
|
+
/**
|
|
1441
|
+
* Загружает CSV файл по URL
|
|
1442
|
+
*
|
|
1443
|
+
* @param url - URL CSV файла
|
|
1444
|
+
* @param options - Опции для csvToJson
|
|
1445
|
+
* @returns Promise с распарсенными данными
|
|
1446
|
+
*/
|
|
1447
|
+
async function loadCsvFromUrl(url, options = {}) {
|
|
1448
|
+
if (typeof window === 'undefined') {
|
|
1449
|
+
throw new ValidationError('loadCsvFromUrl() работает только в браузере');
|
|
1450
|
+
}
|
|
1451
|
+
const response = await fetch(url);
|
|
1452
|
+
if (!response.ok) {
|
|
1453
|
+
throw new ValidationError(`Failed to load CSV from URL: ${response.status} ${response.statusText}`);
|
|
1454
|
+
}
|
|
1455
|
+
const text = await response.text();
|
|
1456
|
+
return csvToJson$1(text, options);
|
|
1457
|
+
}
|
|
1458
|
+
/**
|
|
1459
|
+
* Асинхронная версия loadCsvFromUrl
|
|
1460
|
+
*/
|
|
1461
|
+
async function loadCsvFromUrlAsync(url, options = {}) {
|
|
1462
|
+
return loadCsvFromUrl(url, options);
|
|
1463
|
+
}
|
|
1464
|
+
/**
|
|
1465
|
+
* Экспортирует данные в CSV и открывает в новой вкладке
|
|
1466
|
+
*
|
|
1467
|
+
* @param data - Данные для экспорта
|
|
1468
|
+
* @param options - Опции для jsonToCsv
|
|
1469
|
+
*/
|
|
1470
|
+
function openCsvInNewTab(data, options = {}) {
|
|
1471
|
+
if (typeof window === 'undefined') {
|
|
1472
|
+
throw new ValidationError('openCsvInNewTab() работает только в браузере');
|
|
1473
|
+
}
|
|
1474
|
+
const csv = jsonToCsv$1(data, options);
|
|
1475
|
+
const blob = new Blob([csv], { type: 'text/csv;charset=utf-8' });
|
|
1476
|
+
const url = URL.createObjectURL(blob);
|
|
1477
|
+
window.open(url, '_blank');
|
|
1478
|
+
// Освобождение URL через некоторое время
|
|
1479
|
+
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
|
1480
|
+
}
|
|
1481
|
+
/**
|
|
1482
|
+
* Асинхронная версия openCsvInNewTab
|
|
1483
|
+
*/
|
|
1484
|
+
async function openCsvInNewTabAsync(data, options = {}) {
|
|
1485
|
+
return openCsvInNewTab(data, options);
|
|
1486
|
+
}
|
|
1487
|
+
/**
|
|
1488
|
+
* Копирует CSV в буфер обмена
|
|
1489
|
+
*
|
|
1490
|
+
* @param data - Данные для копирования
|
|
1491
|
+
* @param options - Опции для jsonToCsv
|
|
1492
|
+
* @returns Promise с результатом копирования
|
|
1493
|
+
*/
|
|
1494
|
+
async function copyCsvToClipboard(data, options = {}) {
|
|
1495
|
+
if (typeof window === 'undefined' || !navigator.clipboard) {
|
|
1496
|
+
throw new ValidationError('copyCsvToClipboard() требует поддержки Clipboard API');
|
|
1497
|
+
}
|
|
1498
|
+
const csv = jsonToCsv$1(data, options);
|
|
1499
|
+
try {
|
|
1500
|
+
await navigator.clipboard.writeText(csv);
|
|
1501
|
+
return true;
|
|
1502
|
+
}
|
|
1503
|
+
catch (error) {
|
|
1504
|
+
console.error('Failed to copy to clipboard:', error);
|
|
1505
|
+
return false;
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
/**
|
|
1509
|
+
* Сохраняет CSV в localStorage
|
|
1510
|
+
*
|
|
1511
|
+
* @param key - Ключ для сохранения
|
|
1512
|
+
* @param data - Данные для сохранения
|
|
1513
|
+
* @param options - Опции для jsonToCsv
|
|
1514
|
+
*/
|
|
1515
|
+
function saveCsvToLocalStorage(key, data, options = {}) {
|
|
1516
|
+
if (typeof window === 'undefined' || !localStorage) {
|
|
1517
|
+
throw new ValidationError('saveCsvToLocalStorage() требует localStorage');
|
|
1518
|
+
}
|
|
1519
|
+
const csv = jsonToCsv$1(data, options);
|
|
1520
|
+
localStorage.setItem(key, csv);
|
|
1521
|
+
}
|
|
1522
|
+
/**
|
|
1523
|
+
* Загружает CSV из localStorage
|
|
1524
|
+
*
|
|
1525
|
+
* @param key - Ключ для загрузки
|
|
1526
|
+
* @param options - Опции для csvToJson
|
|
1527
|
+
* @returns Распарсенные данные или null
|
|
1528
|
+
*/
|
|
1529
|
+
function loadCsvFromLocalStorage(key, options = {}) {
|
|
1530
|
+
if (typeof window === 'undefined' || !localStorage) {
|
|
1531
|
+
throw new ValidationError('loadCsvFromLocalStorage() требует localStorage');
|
|
1532
|
+
}
|
|
1533
|
+
const csv = localStorage.getItem(key);
|
|
1534
|
+
if (!csv) {
|
|
1535
|
+
return null;
|
|
1536
|
+
}
|
|
1537
|
+
return csvToJson$1(csv, options);
|
|
1538
|
+
}
|
|
1539
|
+
/**
|
|
1540
|
+
* Асинхронная версия loadCsvFromLocalStorage
|
|
1541
|
+
*/
|
|
1542
|
+
async function loadCsvFromLocalStorageAsync(key, options = {}) {
|
|
1543
|
+
return loadCsvFromLocalStorage(key, options);
|
|
1622
1544
|
}
|
|
1623
|
-
|
|
1624
1545
|
/**
|
|
1625
1546
|
* Создает CSV файл из JSON данных (альтернатива downloadAsCsv)
|
|
1626
1547
|
* Возвращает Blob вместо автоматического скачивания
|
|
1627
|
-
*
|
|
1628
|
-
* @param
|
|
1629
|
-
* @param
|
|
1630
|
-
* @returns
|
|
1548
|
+
*
|
|
1549
|
+
* @param data - Массив объектов
|
|
1550
|
+
* @param options - Опции для jsonToCsv
|
|
1551
|
+
* @returns CSV Blob
|
|
1631
1552
|
*/
|
|
1632
1553
|
function createCsvBlob(data, options = {}) {
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1554
|
+
const csv = jsonToCsv$1(data, options);
|
|
1555
|
+
return new Blob([csv], {
|
|
1556
|
+
type: 'text/csv;charset=utf-8;'
|
|
1557
|
+
});
|
|
1558
|
+
}
|
|
1559
|
+
/**
|
|
1560
|
+
* Асинхронная версия createCsvBlob
|
|
1561
|
+
*/
|
|
1562
|
+
async function createCsvBlobAsync(data, options = {}) {
|
|
1563
|
+
return createCsvBlob(data, options);
|
|
1637
1564
|
}
|
|
1638
|
-
|
|
1639
1565
|
/**
|
|
1640
1566
|
* Парсит CSV строку из Blob
|
|
1641
|
-
*
|
|
1642
|
-
* @param
|
|
1643
|
-
* @param
|
|
1644
|
-
* @returns
|
|
1567
|
+
*
|
|
1568
|
+
* @param blob - CSV Blob
|
|
1569
|
+
* @param options - Опции для csvToJson
|
|
1570
|
+
* @returns Promise с JSON данными
|
|
1645
1571
|
*/
|
|
1646
1572
|
async function parseCsvBlob(blob, options = {}) {
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1573
|
+
if (!(blob instanceof Blob)) {
|
|
1574
|
+
throw new ValidationError('Input must be a Blob object');
|
|
1575
|
+
}
|
|
1576
|
+
return new Promise((resolve, reject) => {
|
|
1577
|
+
const reader = new FileReader();
|
|
1578
|
+
reader.onload = function (event) {
|
|
1579
|
+
try {
|
|
1580
|
+
const csvText = event.target?.result;
|
|
1581
|
+
const json = csvToJson$1(csvText, options);
|
|
1582
|
+
resolve(json);
|
|
1583
|
+
}
|
|
1584
|
+
catch (error) {
|
|
1585
|
+
reject(error);
|
|
1586
|
+
}
|
|
1587
|
+
};
|
|
1588
|
+
reader.onerror = function () {
|
|
1589
|
+
reject(new ValidationError('Ошибка чтения Blob'));
|
|
1590
|
+
};
|
|
1591
|
+
reader.readAsText(blob, 'UTF-8');
|
|
1592
|
+
});
|
|
1593
|
+
}
|
|
1594
|
+
/**
|
|
1595
|
+
* Асинхронная версия parseCsvBlob
|
|
1596
|
+
*/
|
|
1597
|
+
async function parseCsvBlobAsync(blob, options = {}) {
|
|
1598
|
+
return parseCsvBlob(blob, options);
|
|
1666
1599
|
}
|
|
1667
|
-
|
|
1668
1600
|
// Экспорт для Node.js совместимости
|
|
1669
1601
|
if (typeof module !== 'undefined' && module.exports) {
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1602
|
+
module.exports = {
|
|
1603
|
+
downloadAsCsv,
|
|
1604
|
+
downloadAsCsvAsync: downloadAsCsvAsync$1,
|
|
1605
|
+
parseCsvFile,
|
|
1606
|
+
parseCsvFileStream,
|
|
1607
|
+
createCsvBlob,
|
|
1608
|
+
createCsvBlobAsync,
|
|
1609
|
+
parseCsvBlob,
|
|
1610
|
+
parseCsvBlobAsync,
|
|
1611
|
+
jsonToCsvStream,
|
|
1612
|
+
jsonToNdjsonStream,
|
|
1613
|
+
csvToJsonStream,
|
|
1614
|
+
loadCsvFromUrl,
|
|
1615
|
+
loadCsvFromUrlAsync,
|
|
1616
|
+
openCsvInNewTab,
|
|
1617
|
+
openCsvInNewTabAsync,
|
|
1618
|
+
copyCsvToClipboard,
|
|
1619
|
+
saveCsvToLocalStorage,
|
|
1620
|
+
loadCsvFromLocalStorage,
|
|
1621
|
+
loadCsvFromLocalStorageAsync
|
|
1622
|
+
};
|
|
1680
1623
|
}
|
|
1681
1624
|
|
|
1682
1625
|
// Worker Pool для параллельной обработки CSV
|
|
1683
1626
|
// Использует Comlink для простой коммуникации с Web Workers
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
1627
|
// Проверка поддержки Web Workers
|
|
1687
1628
|
const WORKERS_SUPPORTED = typeof Worker !== 'undefined';
|
|
1688
1629
|
function isTransferableBuffer(value) {
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
}
|
|
1692
|
-
if (typeof SharedArrayBuffer !== 'undefined' && value instanceof SharedArrayBuffer) {
|
|
1693
|
-
return false;
|
|
1694
|
-
}
|
|
1695
|
-
return true;
|
|
1696
|
-
}
|
|
1697
|
-
function collectTransferables(args) {
|
|
1698
|
-
const transferables = [];
|
|
1699
|
-
const collectFromValue = value => {
|
|
1700
|
-
if (!value) {
|
|
1701
|
-
return;
|
|
1630
|
+
if (!(value instanceof ArrayBuffer)) {
|
|
1631
|
+
return false;
|
|
1702
1632
|
}
|
|
1703
|
-
if (
|
|
1704
|
-
|
|
1705
|
-
return;
|
|
1633
|
+
if (typeof SharedArrayBuffer !== 'undefined' && value instanceof SharedArrayBuffer) {
|
|
1634
|
+
return false;
|
|
1706
1635
|
}
|
|
1707
|
-
|
|
1708
|
-
transferables.push(value.buffer);
|
|
1709
|
-
return;
|
|
1710
|
-
}
|
|
1711
|
-
if (Array.isArray(value)) {
|
|
1712
|
-
value.forEach(collectFromValue);
|
|
1713
|
-
}
|
|
1714
|
-
};
|
|
1715
|
-
args.forEach(collectFromValue);
|
|
1716
|
-
return transferables.length ? transferables : null;
|
|
1636
|
+
return true;
|
|
1717
1637
|
}
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1638
|
+
function collectTransferables(args) {
|
|
1639
|
+
const transferables = [];
|
|
1640
|
+
const collectFromValue = (value) => {
|
|
1641
|
+
if (!value) {
|
|
1642
|
+
return;
|
|
1643
|
+
}
|
|
1644
|
+
if (isTransferableBuffer(value)) {
|
|
1645
|
+
transferables.push(value);
|
|
1646
|
+
return;
|
|
1647
|
+
}
|
|
1648
|
+
if (ArrayBuffer.isView(value) && isTransferableBuffer(value.buffer)) {
|
|
1649
|
+
transferables.push(value.buffer);
|
|
1650
|
+
return;
|
|
1651
|
+
}
|
|
1652
|
+
if (Array.isArray(value)) {
|
|
1653
|
+
value.forEach(collectFromValue);
|
|
1654
|
+
}
|
|
1655
|
+
};
|
|
1656
|
+
args.forEach(collectFromValue);
|
|
1657
|
+
return transferables.length ? transferables : null;
|
|
1658
|
+
}
|
|
1659
|
+
/**
|
|
1660
|
+
* Опции для Worker Pool
|
|
1661
|
+
* @typedef {Object} WorkerPoolOptions
|
|
1662
|
+
* @property {number} [workerCount=4] - Количество workers в pool
|
|
1663
|
+
* @property {number} [maxQueueSize=100] - Максимальный размер очереди задач
|
|
1664
|
+
* @property {boolean} [autoScale=true] - Автоматическое масштабирование pool
|
|
1665
|
+
* @property {number} [idleTimeout=60000] - Таймаут простоя worker (мс)
|
|
1726
1666
|
*/
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
*
|
|
1730
|
-
* @
|
|
1731
|
-
* @property {number}
|
|
1732
|
-
* @property {number}
|
|
1733
|
-
* @property {number}
|
|
1734
|
-
* @property {number}
|
|
1735
|
-
* @property {number}
|
|
1736
|
-
* @property {number} tasksFailed - Неудачные задачи
|
|
1667
|
+
/**
|
|
1668
|
+
* Статистика Worker Pool
|
|
1669
|
+
* @typedef {Object} WorkerPoolStats
|
|
1670
|
+
* @property {number} totalWorkers - Всего workers
|
|
1671
|
+
* @property {number} activeWorkers - Активные workers
|
|
1672
|
+
* @property {number} idleWorkers - Простаивающие workers
|
|
1673
|
+
* @property {number} queueSize - Размер очереди
|
|
1674
|
+
* @property {number} tasksCompleted - Завершенные задачи
|
|
1675
|
+
* @property {number} tasksFailed - Неудачные задачи
|
|
1737
1676
|
*/
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
*
|
|
1741
|
-
* @
|
|
1742
|
-
* @property {number}
|
|
1743
|
-
* @property {number}
|
|
1744
|
-
* @property {number}
|
|
1745
|
-
* @property {number} speed - Скорость обработки (элементов/сек)
|
|
1677
|
+
/**
|
|
1678
|
+
* Прогресс обработки задачи
|
|
1679
|
+
* @typedef {Object} TaskProgress
|
|
1680
|
+
* @property {number} processed - Обработано элементов
|
|
1681
|
+
* @property {number} total - Всего элементов
|
|
1682
|
+
* @property {number} percentage - Процент выполнения
|
|
1683
|
+
* @property {number} speed - Скорость обработки (элементов/сек)
|
|
1746
1684
|
*/
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
* Worker Pool для параллельной обработки CSV
|
|
1685
|
+
/**
|
|
1686
|
+
* Worker Pool для параллельной обработки CSV
|
|
1750
1687
|
*/
|
|
1751
1688
|
class WorkerPool {
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
}
|
|
1782
|
-
|
|
1783
|
-
/**
|
|
1784
|
-
* Инициализация workers
|
|
1785
|
-
* @private
|
|
1786
|
-
*/
|
|
1787
|
-
initializeWorkers() {
|
|
1788
|
-
const {
|
|
1789
|
-
workerCount
|
|
1790
|
-
} = this.options;
|
|
1791
|
-
for (let i = 0; i < workerCount; i++) {
|
|
1792
|
-
this.createWorker();
|
|
1793
|
-
}
|
|
1794
|
-
this.updateStats();
|
|
1795
|
-
}
|
|
1796
|
-
|
|
1797
|
-
/**
|
|
1798
|
-
* Создает нового worker
|
|
1799
|
-
* @private
|
|
1800
|
-
*/
|
|
1801
|
-
createWorker() {
|
|
1802
|
-
try {
|
|
1803
|
-
const worker = new Worker(this.workerScript, {
|
|
1804
|
-
type: 'module'
|
|
1805
|
-
});
|
|
1806
|
-
worker.id = `worker-${this.workers.length}`;
|
|
1807
|
-
worker.status = 'idle';
|
|
1808
|
-
worker.lastUsed = Date.now();
|
|
1809
|
-
worker.taskId = null;
|
|
1810
|
-
|
|
1811
|
-
// Обработчики событий
|
|
1812
|
-
worker.onmessage = event => this.handleWorkerMessage(worker, event);
|
|
1813
|
-
worker.onerror = error => this.handleWorkerError(worker, error);
|
|
1814
|
-
worker.onmessageerror = error => this.handleWorkerMessageError(worker, error);
|
|
1815
|
-
this.workers.push(worker);
|
|
1816
|
-
this.stats.totalWorkers++;
|
|
1817
|
-
this.stats.idleWorkers++;
|
|
1818
|
-
return worker;
|
|
1819
|
-
} catch (error) {
|
|
1820
|
-
throw new ConfigurationError(`Не удалось создать worker: ${error.message}`);
|
|
1821
|
-
}
|
|
1822
|
-
}
|
|
1823
|
-
|
|
1824
|
-
/**
|
|
1825
|
-
* Обработка сообщений от worker
|
|
1826
|
-
* @private
|
|
1827
|
-
*/
|
|
1828
|
-
handleWorkerMessage(worker, event) {
|
|
1829
|
-
const {
|
|
1830
|
-
data
|
|
1831
|
-
} = event;
|
|
1832
|
-
if (data.type === 'PROGRESS') {
|
|
1833
|
-
this.handleProgress(worker, data);
|
|
1834
|
-
} else if (data.type === 'RESULT') {
|
|
1835
|
-
this.handleResult(worker, data);
|
|
1836
|
-
} else if (data.type === 'ERROR') {
|
|
1837
|
-
this.handleWorkerTaskError(worker, data);
|
|
1838
|
-
}
|
|
1839
|
-
}
|
|
1840
|
-
|
|
1841
|
-
/**
|
|
1842
|
-
* Обработка прогресса задачи
|
|
1843
|
-
* @private
|
|
1844
|
-
*/
|
|
1845
|
-
handleProgress(worker, progressData) {
|
|
1846
|
-
const taskId = worker.taskId;
|
|
1847
|
-
if (taskId && this.activeTasks.has(taskId)) {
|
|
1848
|
-
const task = this.activeTasks.get(taskId);
|
|
1849
|
-
if (task.onProgress) {
|
|
1850
|
-
task.onProgress({
|
|
1851
|
-
processed: progressData.processed,
|
|
1852
|
-
total: progressData.total,
|
|
1853
|
-
percentage: progressData.processed / progressData.total * 100,
|
|
1854
|
-
speed: progressData.speed || 0
|
|
1855
|
-
});
|
|
1856
|
-
}
|
|
1689
|
+
/**
|
|
1690
|
+
* Создает новый Worker Pool
|
|
1691
|
+
* @param {string} workerScript - URL скрипта worker
|
|
1692
|
+
* @param {WorkerPoolOptions} [options] - Опции pool
|
|
1693
|
+
*/
|
|
1694
|
+
constructor(workerScript, options = {}) {
|
|
1695
|
+
if (!WORKERS_SUPPORTED) {
|
|
1696
|
+
throw new ValidationError('Web Workers не поддерживаются в этом браузере');
|
|
1697
|
+
}
|
|
1698
|
+
this.workerScript = workerScript;
|
|
1699
|
+
this.options = {
|
|
1700
|
+
workerCount: 4,
|
|
1701
|
+
maxQueueSize: 100,
|
|
1702
|
+
autoScale: true,
|
|
1703
|
+
idleTimeout: 60000,
|
|
1704
|
+
...options
|
|
1705
|
+
};
|
|
1706
|
+
this.workers = [];
|
|
1707
|
+
this.taskQueue = [];
|
|
1708
|
+
this.activeTasks = new Map();
|
|
1709
|
+
this.stats = {
|
|
1710
|
+
totalWorkers: 0,
|
|
1711
|
+
activeWorkers: 0,
|
|
1712
|
+
idleWorkers: 0,
|
|
1713
|
+
queueSize: 0,
|
|
1714
|
+
tasksCompleted: 0,
|
|
1715
|
+
tasksFailed: 0
|
|
1716
|
+
};
|
|
1717
|
+
this.initializeWorkers();
|
|
1857
1718
|
}
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
// Освобождение worker
|
|
1870
|
-
worker.status = 'idle';
|
|
1871
|
-
worker.lastUsed = Date.now();
|
|
1872
|
-
worker.taskId = null;
|
|
1873
|
-
this.stats.activeWorkers--;
|
|
1874
|
-
this.stats.idleWorkers++;
|
|
1875
|
-
|
|
1876
|
-
// Завершение задачи
|
|
1877
|
-
task.resolve(resultData.data);
|
|
1878
|
-
this.activeTasks.delete(taskId);
|
|
1879
|
-
this.stats.tasksCompleted++;
|
|
1880
|
-
|
|
1881
|
-
// Обработка следующей задачи в очереди
|
|
1882
|
-
this.processQueue();
|
|
1883
|
-
this.updateStats();
|
|
1719
|
+
/**
|
|
1720
|
+
* Инициализация workers
|
|
1721
|
+
* @private
|
|
1722
|
+
*/
|
|
1723
|
+
initializeWorkers() {
|
|
1724
|
+
const { workerCount } = this.options;
|
|
1725
|
+
for (let i = 0; i < workerCount; i++) {
|
|
1726
|
+
this.createWorker();
|
|
1727
|
+
}
|
|
1728
|
+
this.updateStats();
|
|
1884
1729
|
}
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
if (errorData.details) {
|
|
1909
|
-
workerError.details = errorData.details;
|
|
1910
|
-
}
|
|
1911
|
-
task.reject(workerError);
|
|
1912
|
-
this.activeTasks.delete(taskId);
|
|
1913
|
-
this.stats.tasksFailed++;
|
|
1914
|
-
|
|
1915
|
-
// Обработка следующей задачи
|
|
1916
|
-
this.processQueue();
|
|
1917
|
-
this.updateStats();
|
|
1730
|
+
/**
|
|
1731
|
+
* Создает нового worker
|
|
1732
|
+
* @private
|
|
1733
|
+
*/
|
|
1734
|
+
createWorker() {
|
|
1735
|
+
try {
|
|
1736
|
+
const worker = new Worker(this.workerScript, { type: 'module' });
|
|
1737
|
+
worker.id = `worker-${this.workers.length}`;
|
|
1738
|
+
worker.status = 'idle';
|
|
1739
|
+
worker.lastUsed = Date.now();
|
|
1740
|
+
worker.taskId = null;
|
|
1741
|
+
// Обработчики событий
|
|
1742
|
+
worker.onmessage = (event) => this.handleWorkerMessage(worker, event);
|
|
1743
|
+
worker.onerror = (error) => this.handleWorkerError(worker, error);
|
|
1744
|
+
worker.onmessageerror = (error) => this.handleWorkerMessageError(worker, error);
|
|
1745
|
+
this.workers.push(worker);
|
|
1746
|
+
this.stats.totalWorkers++;
|
|
1747
|
+
this.stats.idleWorkers++;
|
|
1748
|
+
return worker;
|
|
1749
|
+
}
|
|
1750
|
+
catch (error) {
|
|
1751
|
+
throw new ConfigurationError(`Не удалось создать worker: ${error.message}`);
|
|
1752
|
+
}
|
|
1918
1753
|
}
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1754
|
+
/**
|
|
1755
|
+
* Обработка сообщений от worker
|
|
1756
|
+
* @private
|
|
1757
|
+
*/
|
|
1758
|
+
handleWorkerMessage(worker, event) {
|
|
1759
|
+
const { data } = event;
|
|
1760
|
+
if (data.type === 'PROGRESS') {
|
|
1761
|
+
this.handleProgress(worker, data);
|
|
1762
|
+
}
|
|
1763
|
+
else if (data.type === 'RESULT') {
|
|
1764
|
+
this.handleResult(worker, data);
|
|
1765
|
+
}
|
|
1766
|
+
else if (data.type === 'ERROR') {
|
|
1767
|
+
this.handleWorkerTaskError(worker, data);
|
|
1768
|
+
}
|
|
1769
|
+
}
|
|
1770
|
+
/**
|
|
1771
|
+
* Обработка прогресса задачи
|
|
1772
|
+
* @private
|
|
1773
|
+
*/
|
|
1774
|
+
handleProgress(worker, progressData) {
|
|
1775
|
+
const taskId = worker.taskId;
|
|
1776
|
+
if (taskId && this.activeTasks.has(taskId)) {
|
|
1777
|
+
const task = this.activeTasks.get(taskId);
|
|
1778
|
+
if (task.onProgress) {
|
|
1779
|
+
task.onProgress({
|
|
1780
|
+
processed: progressData.processed,
|
|
1781
|
+
total: progressData.total,
|
|
1782
|
+
percentage: (progressData.processed / progressData.total) * 100,
|
|
1783
|
+
speed: progressData.speed || 0
|
|
1784
|
+
});
|
|
1785
|
+
}
|
|
1786
|
+
}
|
|
1787
|
+
}
|
|
1788
|
+
/**
|
|
1789
|
+
* Обработка результата задачи
|
|
1790
|
+
* @private
|
|
1791
|
+
*/
|
|
1792
|
+
handleResult(worker, resultData) {
|
|
1793
|
+
const taskId = worker.taskId;
|
|
1794
|
+
if (taskId && this.activeTasks.has(taskId)) {
|
|
1795
|
+
const task = this.activeTasks.get(taskId);
|
|
1796
|
+
// Освобождение worker
|
|
1797
|
+
worker.status = 'idle';
|
|
1798
|
+
worker.lastUsed = Date.now();
|
|
1799
|
+
worker.taskId = null;
|
|
1800
|
+
this.stats.activeWorkers--;
|
|
1801
|
+
this.stats.idleWorkers++;
|
|
1802
|
+
// Завершение задачи
|
|
1803
|
+
task.resolve(resultData.data);
|
|
1804
|
+
this.activeTasks.delete(taskId);
|
|
1805
|
+
this.stats.tasksCompleted++;
|
|
1806
|
+
// Обработка следующей задачи в очереди
|
|
1807
|
+
this.processQueue();
|
|
1808
|
+
this.updateStats();
|
|
1809
|
+
}
|
|
1810
|
+
}
|
|
1811
|
+
/**
|
|
1812
|
+
* Обработка ошибки задачи
|
|
1813
|
+
* @private
|
|
1814
|
+
*/
|
|
1815
|
+
handleWorkerTaskError(worker, errorData) {
|
|
1816
|
+
const taskId = worker.taskId;
|
|
1817
|
+
if (taskId && this.activeTasks.has(taskId)) {
|
|
1818
|
+
const task = this.activeTasks.get(taskId);
|
|
1819
|
+
// Освобождение worker
|
|
1820
|
+
worker.status = 'idle';
|
|
1821
|
+
worker.lastUsed = Date.now();
|
|
1822
|
+
worker.taskId = null;
|
|
1823
|
+
this.stats.activeWorkers--;
|
|
1824
|
+
this.stats.idleWorkers++;
|
|
1825
|
+
// Завершение с ошибкой
|
|
1826
|
+
const workerError = new Error(errorData.message || 'Ошибка в worker');
|
|
1827
|
+
if (errorData.code) {
|
|
1828
|
+
workerError.code = errorData.code;
|
|
1829
|
+
}
|
|
1830
|
+
if (errorData.details) {
|
|
1831
|
+
workerError.details = errorData.details;
|
|
1832
|
+
}
|
|
1833
|
+
task.reject(workerError);
|
|
1834
|
+
this.activeTasks.delete(taskId);
|
|
1835
|
+
this.stats.tasksFailed++;
|
|
1836
|
+
// Обработка следующей задачи
|
|
1837
|
+
this.processQueue();
|
|
1838
|
+
this.updateStats();
|
|
1839
|
+
}
|
|
1840
|
+
}
|
|
1841
|
+
/**
|
|
1842
|
+
* Обработка ошибок worker
|
|
1843
|
+
* @private
|
|
1844
|
+
*/
|
|
1845
|
+
handleWorkerError(worker, error) {
|
|
1846
|
+
console.error(`Worker ${worker.id} error:`, error);
|
|
1847
|
+
// Перезапуск worker
|
|
1848
|
+
this.restartWorker(worker);
|
|
1849
|
+
}
|
|
1850
|
+
/**
|
|
1851
|
+
* Обработка ошибок сообщений
|
|
1852
|
+
* @private
|
|
1853
|
+
*/
|
|
1854
|
+
handleWorkerMessageError(worker, error) {
|
|
1855
|
+
console.error(`Worker ${worker.id} message error:`, error);
|
|
1856
|
+
}
|
|
1857
|
+
/**
|
|
1858
|
+
* Перезапуск worker
|
|
1859
|
+
* @private
|
|
1860
|
+
*/
|
|
1861
|
+
restartWorker(worker) {
|
|
1862
|
+
const index = this.workers.indexOf(worker);
|
|
1863
|
+
if (index !== -1) {
|
|
1864
|
+
// Завершение старого worker
|
|
1865
|
+
worker.terminate();
|
|
1866
|
+
// Удаление из статистики
|
|
1867
|
+
if (worker.status === 'active') {
|
|
1868
|
+
this.stats.activeWorkers--;
|
|
1869
|
+
}
|
|
1870
|
+
else {
|
|
1871
|
+
this.stats.idleWorkers--;
|
|
1872
|
+
}
|
|
1873
|
+
this.stats.totalWorkers--;
|
|
1874
|
+
// Создание нового worker
|
|
1875
|
+
const newWorker = this.createWorker();
|
|
1876
|
+
this.workers[index] = newWorker;
|
|
1877
|
+
// Перезапуск задачи если была активна
|
|
1878
|
+
if (worker.taskId && this.activeTasks.has(worker.taskId)) {
|
|
1879
|
+
const task = this.activeTasks.get(worker.taskId);
|
|
1880
|
+
this.executeTask(newWorker, task);
|
|
1881
|
+
}
|
|
1882
|
+
}
|
|
1883
|
+
}
|
|
1884
|
+
/**
|
|
1885
|
+
* Выполнение задачи на worker
|
|
1886
|
+
* @private
|
|
1887
|
+
*/
|
|
1888
|
+
executeTask(worker, task) {
|
|
1889
|
+
worker.status = 'active';
|
|
1890
|
+
worker.lastUsed = Date.now();
|
|
1891
|
+
worker.taskId = task.id;
|
|
1954
1892
|
this.stats.idleWorkers--;
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1893
|
+
this.stats.activeWorkers++;
|
|
1894
|
+
// Отправка задачи в worker
|
|
1895
|
+
const payload = {
|
|
1896
|
+
type: 'EXECUTE',
|
|
1897
|
+
taskId: task.id,
|
|
1898
|
+
method: task.method,
|
|
1899
|
+
args: task.args,
|
|
1900
|
+
options: task.options
|
|
1901
|
+
};
|
|
1902
|
+
if (task.transferList && task.transferList.length) {
|
|
1903
|
+
worker.postMessage(payload, task.transferList);
|
|
1904
|
+
}
|
|
1905
|
+
else {
|
|
1906
|
+
worker.postMessage(payload);
|
|
1907
|
+
}
|
|
1967
1908
|
}
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1909
|
+
/**
|
|
1910
|
+
* Обработка очереди задач
|
|
1911
|
+
* @private
|
|
1912
|
+
*/
|
|
1913
|
+
processQueue() {
|
|
1914
|
+
if (this.taskQueue.length === 0) {
|
|
1915
|
+
return;
|
|
1916
|
+
}
|
|
1917
|
+
while (this.taskQueue.length > 0) {
|
|
1918
|
+
const idleWorker = this.workers.find(w => w.status === 'idle');
|
|
1919
|
+
if (!idleWorker) {
|
|
1920
|
+
if (this.options.autoScale && this.workers.length < this.options.maxQueueSize) {
|
|
1921
|
+
this.createWorker();
|
|
1922
|
+
continue;
|
|
1923
|
+
}
|
|
1924
|
+
break;
|
|
1925
|
+
}
|
|
1926
|
+
const task = this.taskQueue.shift();
|
|
1927
|
+
this.stats.queueSize--;
|
|
1928
|
+
this.executeTask(idleWorker, task);
|
|
1929
|
+
}
|
|
1930
|
+
this.updateStats();
|
|
1931
|
+
}
|
|
1932
|
+
/**
|
|
1933
|
+
* Обновление статистики
|
|
1934
|
+
* @private
|
|
1935
|
+
*/
|
|
1936
|
+
updateStats() {
|
|
1937
|
+
this.stats.queueSize = this.taskQueue.length;
|
|
1938
|
+
}
|
|
1939
|
+
/**
|
|
1940
|
+
* Выполнение задачи через pool
|
|
1941
|
+
* @param {string} method - Метод для вызова в worker
|
|
1942
|
+
* @param {Array} args - Аргументы метода
|
|
1943
|
+
* @param {Object} [options] - Опции задачи
|
|
1944
|
+
* @param {Function} [onProgress] - Callback прогресса
|
|
1945
|
+
* @returns {Promise<unknown>} Результат выполнения
|
|
1946
|
+
*/
|
|
1947
|
+
async exec(method, args = [], options = {}, onProgress = null) {
|
|
1948
|
+
return new Promise((resolve, reject) => {
|
|
1949
|
+
// Проверка размера очереди
|
|
1950
|
+
if (this.taskQueue.length >= this.options.maxQueueSize) {
|
|
1951
|
+
reject(new Error('Очередь задач переполнена'));
|
|
1952
|
+
return;
|
|
1953
|
+
}
|
|
1954
|
+
// Создание задачи
|
|
1955
|
+
const taskId = `task-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
1956
|
+
const { transfer, ...taskOptions } = options || {};
|
|
1957
|
+
const transferList = transfer || collectTransferables(args);
|
|
1958
|
+
const task = {
|
|
1959
|
+
id: taskId,
|
|
1960
|
+
method,
|
|
1961
|
+
args,
|
|
1962
|
+
options: taskOptions,
|
|
1963
|
+
transferList,
|
|
1964
|
+
onProgress,
|
|
1965
|
+
resolve,
|
|
1966
|
+
reject,
|
|
1967
|
+
createdAt: Date.now()
|
|
1968
|
+
};
|
|
1969
|
+
// Добавление в очередь
|
|
1970
|
+
this.taskQueue.push(task);
|
|
1971
|
+
this.stats.queueSize++;
|
|
1972
|
+
// Запуск обработки очереди
|
|
1973
|
+
this.processQueue();
|
|
1974
|
+
this.updateStats();
|
|
1975
|
+
});
|
|
1976
|
+
}
|
|
1977
|
+
/**
|
|
1978
|
+
* Получение статистики pool
|
|
1979
|
+
* @returns {WorkerPoolStats} Статистика
|
|
1980
|
+
*/
|
|
1981
|
+
getStats() {
|
|
1982
|
+
return { ...this.stats };
|
|
1983
|
+
}
|
|
1984
|
+
/**
|
|
1985
|
+
* Очистка простаивающих workers
|
|
1986
|
+
*/
|
|
1987
|
+
cleanupIdleWorkers() {
|
|
1988
|
+
const now = Date.now();
|
|
1989
|
+
const { idleTimeout } = this.options;
|
|
1990
|
+
for (let i = this.workers.length - 1; i >= 0; i--) {
|
|
1991
|
+
const worker = this.workers[i];
|
|
1992
|
+
if (worker.status === 'idle' && (now - worker.lastUsed) > idleTimeout) {
|
|
1993
|
+
// Сохранение минимального количества workers
|
|
1994
|
+
if (this.workers.length > 1) {
|
|
1995
|
+
worker.terminate();
|
|
1996
|
+
this.workers.splice(i, 1);
|
|
1997
|
+
this.stats.totalWorkers--;
|
|
1998
|
+
this.stats.idleWorkers--;
|
|
1999
|
+
}
|
|
2000
|
+
}
|
|
2001
|
+
}
|
|
2002
|
+
}
|
|
2003
|
+
/**
|
|
2004
|
+
* Завершение всех workers
|
|
2005
|
+
*/
|
|
2006
|
+
terminate() {
|
|
2007
|
+
this.workers.forEach(worker => {
|
|
2008
|
+
worker.terminate();
|
|
2009
|
+
});
|
|
2010
|
+
this.workers = [];
|
|
2011
|
+
this.taskQueue = [];
|
|
2012
|
+
this.activeTasks.clear();
|
|
2013
|
+
// Сброс статистики
|
|
2014
|
+
this.stats = {
|
|
2015
|
+
totalWorkers: 0,
|
|
2016
|
+
activeWorkers: 0,
|
|
2017
|
+
idleWorkers: 0,
|
|
2018
|
+
queueSize: 0,
|
|
2019
|
+
tasksCompleted: 0,
|
|
2020
|
+
tasksFailed: 0
|
|
2021
|
+
};
|
|
1993
2022
|
}
|
|
1994
|
-
}
|
|
1995
|
-
|
|
1996
|
-
/**
|
|
1997
|
-
* Обработка очереди задач
|
|
1998
|
-
* @private
|
|
1999
|
-
*/
|
|
2000
|
-
processQueue() {
|
|
2001
|
-
if (this.taskQueue.length === 0) {
|
|
2002
|
-
return;
|
|
2003
|
-
}
|
|
2004
|
-
while (this.taskQueue.length > 0) {
|
|
2005
|
-
const idleWorker = this.workers.find(w => w.status === 'idle');
|
|
2006
|
-
if (!idleWorker) {
|
|
2007
|
-
if (this.options.autoScale && this.workers.length < this.options.maxQueueSize) {
|
|
2008
|
-
this.createWorker();
|
|
2009
|
-
continue;
|
|
2010
|
-
}
|
|
2011
|
-
break;
|
|
2012
|
-
}
|
|
2013
|
-
const task = this.taskQueue.shift();
|
|
2014
|
-
this.stats.queueSize--;
|
|
2015
|
-
this.executeTask(idleWorker, task);
|
|
2016
|
-
}
|
|
2017
|
-
this.updateStats();
|
|
2018
|
-
}
|
|
2019
|
-
|
|
2020
|
-
/**
|
|
2021
|
-
* Обновление статистики
|
|
2022
|
-
* @private
|
|
2023
|
-
*/
|
|
2024
|
-
updateStats() {
|
|
2025
|
-
this.stats.queueSize = this.taskQueue.length;
|
|
2026
|
-
}
|
|
2027
|
-
|
|
2028
|
-
/**
|
|
2029
|
-
* Выполнение задачи через pool
|
|
2030
|
-
* @param {string} method - Метод для вызова в worker
|
|
2031
|
-
* @param {Array} args - Аргументы метода
|
|
2032
|
-
* @param {Object} [options] - Опции задачи
|
|
2033
|
-
* @param {Function} [onProgress] - Callback прогресса
|
|
2034
|
-
* @returns {Promise<any>} Результат выполнения
|
|
2035
|
-
*/
|
|
2036
|
-
async exec(method, args = [], options = {}, onProgress = null) {
|
|
2037
|
-
return new Promise((resolve, reject) => {
|
|
2038
|
-
// Проверка размера очереди
|
|
2039
|
-
if (this.taskQueue.length >= this.options.maxQueueSize) {
|
|
2040
|
-
reject(new Error('Очередь задач переполнена'));
|
|
2041
|
-
return;
|
|
2042
|
-
}
|
|
2043
|
-
|
|
2044
|
-
// Создание задачи
|
|
2045
|
-
const taskId = `task-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
2046
|
-
const {
|
|
2047
|
-
transfer,
|
|
2048
|
-
...taskOptions
|
|
2049
|
-
} = options || {};
|
|
2050
|
-
const transferList = transfer || collectTransferables(args);
|
|
2051
|
-
const task = {
|
|
2052
|
-
id: taskId,
|
|
2053
|
-
method,
|
|
2054
|
-
args,
|
|
2055
|
-
options: taskOptions,
|
|
2056
|
-
transferList,
|
|
2057
|
-
onProgress,
|
|
2058
|
-
resolve,
|
|
2059
|
-
reject,
|
|
2060
|
-
createdAt: Date.now()
|
|
2061
|
-
};
|
|
2062
|
-
|
|
2063
|
-
// Добавление в очередь
|
|
2064
|
-
this.taskQueue.push(task);
|
|
2065
|
-
this.stats.queueSize++;
|
|
2066
|
-
|
|
2067
|
-
// Запуск обработки очереди
|
|
2068
|
-
this.processQueue();
|
|
2069
|
-
this.updateStats();
|
|
2070
|
-
});
|
|
2071
|
-
}
|
|
2072
|
-
|
|
2073
|
-
/**
|
|
2074
|
-
* Получение статистики pool
|
|
2075
|
-
* @returns {WorkerPoolStats} Статистика
|
|
2076
|
-
*/
|
|
2077
|
-
getStats() {
|
|
2078
|
-
return {
|
|
2079
|
-
...this.stats
|
|
2080
|
-
};
|
|
2081
|
-
}
|
|
2082
|
-
|
|
2083
|
-
/**
|
|
2084
|
-
* Очистка простаивающих workers
|
|
2085
|
-
*/
|
|
2086
|
-
cleanupIdleWorkers() {
|
|
2087
|
-
const now = Date.now();
|
|
2088
|
-
const {
|
|
2089
|
-
idleTimeout
|
|
2090
|
-
} = this.options;
|
|
2091
|
-
for (let i = this.workers.length - 1; i >= 0; i--) {
|
|
2092
|
-
const worker = this.workers[i];
|
|
2093
|
-
if (worker.status === 'idle' && now - worker.lastUsed > idleTimeout) {
|
|
2094
|
-
// Сохранение минимального количества workers
|
|
2095
|
-
if (this.workers.length > 1) {
|
|
2096
|
-
worker.terminate();
|
|
2097
|
-
this.workers.splice(i, 1);
|
|
2098
|
-
this.stats.totalWorkers--;
|
|
2099
|
-
this.stats.idleWorkers--;
|
|
2100
|
-
}
|
|
2101
|
-
}
|
|
2102
|
-
}
|
|
2103
|
-
}
|
|
2104
|
-
|
|
2105
|
-
/**
|
|
2106
|
-
* Завершение всех workers
|
|
2107
|
-
*/
|
|
2108
|
-
terminate() {
|
|
2109
|
-
this.workers.forEach(worker => {
|
|
2110
|
-
worker.terminate();
|
|
2111
|
-
});
|
|
2112
|
-
this.workers = [];
|
|
2113
|
-
this.taskQueue = [];
|
|
2114
|
-
this.activeTasks.clear();
|
|
2115
|
-
|
|
2116
|
-
// Сброс статистики
|
|
2117
|
-
this.stats = {
|
|
2118
|
-
totalWorkers: 0,
|
|
2119
|
-
activeWorkers: 0,
|
|
2120
|
-
idleWorkers: 0,
|
|
2121
|
-
queueSize: 0,
|
|
2122
|
-
tasksCompleted: 0,
|
|
2123
|
-
tasksFailed: 0
|
|
2124
|
-
};
|
|
2125
|
-
}
|
|
2126
2023
|
}
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
*
|
|
2130
|
-
* @
|
|
2131
|
-
* @returns {WorkerPool} Worker Pool
|
|
2024
|
+
/**
|
|
2025
|
+
* Создает Worker Pool для обработки CSV
|
|
2026
|
+
* @param {WorkerPoolOptions} [options] - Опции pool
|
|
2027
|
+
* @returns {WorkerPool} Worker Pool
|
|
2132
2028
|
*/
|
|
2133
2029
|
function createWorkerPool(options = {}) {
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2030
|
+
// Используем встроенный worker скрипт
|
|
2031
|
+
const baseUrl = typeof document !== 'undefined'
|
|
2032
|
+
? document.baseURI
|
|
2033
|
+
: (typeof self !== 'undefined' && self.location
|
|
2034
|
+
? self.location.href
|
|
2035
|
+
: '');
|
|
2036
|
+
const workerScript = new URL('./csv-parser.worker.js', baseUrl).href;
|
|
2037
|
+
return new WorkerPool(workerScript, options);
|
|
2137
2038
|
}
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
*
|
|
2141
|
-
* @param {
|
|
2142
|
-
* @param {
|
|
2143
|
-
* @
|
|
2144
|
-
* @returns {Promise<Array<Object>>} JSON данные
|
|
2039
|
+
/**
|
|
2040
|
+
* Парсит CSV с использованием Web Workers
|
|
2041
|
+
* @param {string|File} csvInput - CSV строка или File объект
|
|
2042
|
+
* @param {Object} [options] - Опции парсинга
|
|
2043
|
+
* @param {Function} [onProgress] - Callback прогресса
|
|
2044
|
+
* @returns {Promise<Array<Object>>} JSON данные
|
|
2145
2045
|
*/
|
|
2146
2046
|
async function parseCSVWithWorker(csvInput, options = {}, onProgress = null) {
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
if (csvInput
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2047
|
+
// Создание pool если нужно
|
|
2048
|
+
const poolHolder = parseCSVWithWorker;
|
|
2049
|
+
if (!poolHolder.pool) {
|
|
2050
|
+
poolHolder.pool = createWorkerPool();
|
|
2051
|
+
}
|
|
2052
|
+
const pool = poolHolder.pool;
|
|
2053
|
+
// Подготовка CSV строки
|
|
2054
|
+
// ?????????? CSV ??????
|
|
2055
|
+
let csvPayload = csvInput;
|
|
2056
|
+
let transfer = null;
|
|
2057
|
+
if (csvInput instanceof File) {
|
|
2058
|
+
const buffer = await readFileAsArrayBuffer(csvInput);
|
|
2059
|
+
csvPayload = new Uint8Array(buffer);
|
|
2060
|
+
transfer = [buffer];
|
|
2061
|
+
}
|
|
2062
|
+
else if (csvInput instanceof ArrayBuffer) {
|
|
2063
|
+
csvPayload = csvInput;
|
|
2064
|
+
transfer = [csvInput];
|
|
2065
|
+
}
|
|
2066
|
+
else if (ArrayBuffer.isView(csvInput)) {
|
|
2067
|
+
csvPayload = csvInput;
|
|
2068
|
+
if (csvInput.buffer instanceof ArrayBuffer) {
|
|
2069
|
+
transfer = [csvInput.buffer];
|
|
2070
|
+
}
|
|
2071
|
+
}
|
|
2072
|
+
else if (typeof csvInput !== 'string') {
|
|
2073
|
+
throw new ValidationError('Input must be a CSV string, File, or ArrayBuffer');
|
|
2074
|
+
}
|
|
2075
|
+
// ????????? ?????? ????? pool
|
|
2076
|
+
const execOptions = transfer ? { transfer } : {};
|
|
2077
|
+
return pool.exec('parseCSV', [csvPayload, options], execOptions, onProgress);
|
|
2178
2078
|
}
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
*
|
|
2182
|
-
* @private
|
|
2079
|
+
/**
|
|
2080
|
+
* Чтение файла как текст
|
|
2081
|
+
* @private
|
|
2183
2082
|
*/
|
|
2184
2083
|
async function readFileAsArrayBuffer(file) {
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2084
|
+
return new Promise((resolve, reject) => {
|
|
2085
|
+
const reader = new FileReader();
|
|
2086
|
+
reader.onload = (event) => resolve(event.target.result);
|
|
2087
|
+
reader.onerror = (error) => reject(error);
|
|
2088
|
+
reader.readAsArrayBuffer(file);
|
|
2089
|
+
});
|
|
2191
2090
|
}
|
|
2192
|
-
|
|
2193
2091
|
// Экспорт для Node.js совместимости
|
|
2194
2092
|
if (typeof module !== 'undefined' && module.exports) {
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2093
|
+
module.exports = {
|
|
2094
|
+
WorkerPool,
|
|
2095
|
+
createWorkerPool,
|
|
2096
|
+
parseCSVWithWorker
|
|
2097
|
+
};
|
|
2200
2098
|
}
|
|
2201
2099
|
|
|
2202
2100
|
var workerPool = /*#__PURE__*/Object.freeze({
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2101
|
+
__proto__: null,
|
|
2102
|
+
WorkerPool: WorkerPool,
|
|
2103
|
+
createWorkerPool: createWorkerPool,
|
|
2104
|
+
parseCSVWithWorker: parseCSVWithWorker
|
|
2207
2105
|
});
|
|
2208
2106
|
|
|
2209
2107
|
// Браузерный entry point для jtcsv
|
|
2210
2108
|
// Экспортирует все функции с поддержкой браузера
|
|
2211
|
-
|
|
2109
|
+
const { jsonToCsv, preprocessData, deepUnwrap } = jsonToCsvBrowser;
|
|
2110
|
+
const { csvToJson, csvToJsonIterator, autoDetectDelimiter } = csvToJsonBrowser;
|
|
2111
|
+
/**
|
|
2112
|
+
* Ленивая инициализация Worker Pool
|
|
2113
|
+
*/
|
|
2212
2114
|
async function createWorkerPoolLazy(options = {}) {
|
|
2213
|
-
|
|
2214
|
-
|
|
2115
|
+
const mod = await Promise.resolve().then(function () { return workerPool; });
|
|
2116
|
+
return mod.createWorkerPool(options);
|
|
2215
2117
|
}
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2118
|
+
/**
|
|
2119
|
+
* Ленивый парсинг CSV с использованием Worker
|
|
2120
|
+
*/
|
|
2121
|
+
async function parseCSVWithWorkerLazy(csvInput, options = {}, onProgress) {
|
|
2122
|
+
const mod = await Promise.resolve().then(function () { return workerPool; });
|
|
2123
|
+
return mod.parseCSVWithWorker(csvInput, options, onProgress);
|
|
2124
|
+
}
|
|
2125
|
+
/**
|
|
2126
|
+
* Асинхронная версия jsonToCsv
|
|
2127
|
+
*/
|
|
2128
|
+
async function jsonToCsvAsync(data, options = {}) {
|
|
2129
|
+
return jsonToCsv(data, options);
|
|
2130
|
+
}
|
|
2131
|
+
/**
|
|
2132
|
+
* Асинхронная версия csvToJson
|
|
2133
|
+
*/
|
|
2134
|
+
async function csvToJsonAsync(csv, options = {}) {
|
|
2135
|
+
return csvToJson(csv, options);
|
|
2136
|
+
}
|
|
2137
|
+
/**
|
|
2138
|
+
* Асинхронная версия parseCsvFile
|
|
2139
|
+
*/
|
|
2140
|
+
async function parseCsvFileAsync(file, options = {}) {
|
|
2141
|
+
return parseCsvFile(file, options);
|
|
2142
|
+
}
|
|
2143
|
+
/**
|
|
2144
|
+
* Асинхронная версия autoDetectDelimiter
|
|
2145
|
+
*/
|
|
2146
|
+
async function autoDetectDelimiterAsync(csv) {
|
|
2147
|
+
return autoDetectDelimiter(csv);
|
|
2148
|
+
}
|
|
2149
|
+
/**
|
|
2150
|
+
* Асинхронная версия downloadAsCsv
|
|
2151
|
+
*/
|
|
2152
|
+
async function downloadAsCsvAsync(data, filename = 'export.csv', options = {}) {
|
|
2153
|
+
return downloadAsCsv(data, filename, options);
|
|
2219
2154
|
}
|
|
2220
|
-
|
|
2221
2155
|
// Основной экспорт
|
|
2222
2156
|
const jtcsv = {
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2157
|
+
// JSON to CSV функции
|
|
2158
|
+
jsonToCsv,
|
|
2159
|
+
preprocessData,
|
|
2160
|
+
downloadAsCsv,
|
|
2161
|
+
deepUnwrap,
|
|
2162
|
+
// CSV to JSON функции
|
|
2163
|
+
csvToJson,
|
|
2164
|
+
csvToJsonIterator,
|
|
2165
|
+
parseCsvFile,
|
|
2166
|
+
parseCsvFileStream,
|
|
2167
|
+
jsonToCsvStream,
|
|
2168
|
+
jsonToNdjsonStream,
|
|
2169
|
+
csvToJsonStream,
|
|
2170
|
+
autoDetectDelimiter,
|
|
2171
|
+
// Web Workers функции
|
|
2172
|
+
createWorkerPool,
|
|
2173
|
+
parseCSVWithWorker,
|
|
2174
|
+
createWorkerPoolLazy,
|
|
2175
|
+
parseCSVWithWorkerLazy,
|
|
2176
|
+
// Асинхронные функции
|
|
2177
|
+
jsonToCsvAsync,
|
|
2178
|
+
csvToJsonAsync,
|
|
2179
|
+
parseCsvFileAsync,
|
|
2180
|
+
autoDetectDelimiterAsync,
|
|
2181
|
+
downloadAsCsvAsync,
|
|
2182
|
+
// Error classes
|
|
2183
|
+
ValidationError,
|
|
2184
|
+
SecurityError,
|
|
2185
|
+
FileSystemError,
|
|
2186
|
+
ParsingError,
|
|
2187
|
+
LimitError,
|
|
2188
|
+
ConfigurationError,
|
|
2189
|
+
ERROR_CODES,
|
|
2190
|
+
// Удобные алиасы
|
|
2191
|
+
parse: csvToJson,
|
|
2192
|
+
unparse: jsonToCsv,
|
|
2193
|
+
parseAsync: csvToJsonAsync,
|
|
2194
|
+
unparseAsync: jsonToCsvAsync,
|
|
2195
|
+
// Версия
|
|
2196
|
+
version: '2.0.0-browser'
|
|
2255
2197
|
};
|
|
2256
|
-
|
|
2257
2198
|
// Экспорт для разных сред
|
|
2258
2199
|
if (typeof module !== 'undefined' && module.exports) {
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
}
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2200
|
+
// Node.js CommonJS
|
|
2201
|
+
module.exports = jtcsv;
|
|
2202
|
+
}
|
|
2203
|
+
else if (typeof define === 'function' && define.amd) {
|
|
2204
|
+
// AMD
|
|
2205
|
+
define([], () => jtcsv);
|
|
2206
|
+
}
|
|
2207
|
+
else if (typeof window !== 'undefined') {
|
|
2208
|
+
// Браузер (глобальная переменная)
|
|
2209
|
+
window.jtcsv = jtcsv;
|
|
2267
2210
|
}
|
|
2268
2211
|
|
|
2269
2212
|
exports.ConfigurationError = ConfigurationError;
|
|
@@ -2274,20 +2217,25 @@ exports.ParsingError = ParsingError;
|
|
|
2274
2217
|
exports.SecurityError = SecurityError;
|
|
2275
2218
|
exports.ValidationError = ValidationError;
|
|
2276
2219
|
exports.autoDetectDelimiter = autoDetectDelimiter;
|
|
2220
|
+
exports.autoDetectDelimiterAsync = autoDetectDelimiterAsync;
|
|
2277
2221
|
exports.createWorkerPool = createWorkerPool;
|
|
2278
2222
|
exports.createWorkerPoolLazy = createWorkerPoolLazy;
|
|
2279
2223
|
exports.csvToJson = csvToJson;
|
|
2224
|
+
exports.csvToJsonAsync = csvToJsonAsync;
|
|
2280
2225
|
exports.csvToJsonIterator = csvToJsonIterator;
|
|
2281
2226
|
exports.csvToJsonStream = csvToJsonStream;
|
|
2282
2227
|
exports.deepUnwrap = deepUnwrap;
|
|
2283
2228
|
exports.default = jtcsv;
|
|
2284
2229
|
exports.downloadAsCsv = downloadAsCsv;
|
|
2230
|
+
exports.downloadAsCsvAsync = downloadAsCsvAsync;
|
|
2285
2231
|
exports.jsonToCsv = jsonToCsv;
|
|
2232
|
+
exports.jsonToCsvAsync = jsonToCsvAsync;
|
|
2286
2233
|
exports.jsonToCsvStream = jsonToCsvStream;
|
|
2287
2234
|
exports.jsonToNdjsonStream = jsonToNdjsonStream;
|
|
2288
2235
|
exports.parseCSVWithWorker = parseCSVWithWorker;
|
|
2289
2236
|
exports.parseCSVWithWorkerLazy = parseCSVWithWorkerLazy;
|
|
2290
2237
|
exports.parseCsvFile = parseCsvFile;
|
|
2238
|
+
exports.parseCsvFileAsync = parseCsvFileAsync;
|
|
2291
2239
|
exports.parseCsvFileStream = parseCsvFileStream;
|
|
2292
2240
|
exports.preprocessData = preprocessData;
|
|
2293
2241
|
//# sourceMappingURL=jtcsv.cjs.js.map
|