@xapi-js/core 1.1.1 → 1.2.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 +11 -28
- package/dist/index.cjs +764 -747
- package/dist/index.d.cts +237 -233
- package/dist/index.d.ts +237 -233
- package/dist/index.js +724 -695
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,745 +1,774 @@
|
|
|
1
|
-
|
|
2
|
-
import { StaxXmlWriter } from "stax-xml";
|
|
1
|
+
import { StaxXmlWriterSync } from "stax-xml";
|
|
3
2
|
import * as txml from "txml";
|
|
4
3
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
//#region src/types.ts
|
|
5
|
+
/**
|
|
6
|
+
* Defines the possible types of rows in an X-API dataset (e.g., "insert", "update", "delete").
|
|
7
|
+
*/
|
|
8
|
+
const rowType = [
|
|
9
|
+
"insert",
|
|
10
|
+
"update",
|
|
11
|
+
"delete"
|
|
12
|
+
];
|
|
13
|
+
/**
|
|
14
|
+
* Defines the possible data types for columns in an X-API dataset.
|
|
15
|
+
*/
|
|
16
|
+
const columnType = [
|
|
17
|
+
"STRING",
|
|
18
|
+
"INT",
|
|
19
|
+
"FLOAT",
|
|
20
|
+
"DECIMAL",
|
|
21
|
+
"BIGDECIMAL",
|
|
22
|
+
"DATE",
|
|
23
|
+
"DATETIME",
|
|
24
|
+
"TIME",
|
|
25
|
+
"BLOB"
|
|
26
|
+
];
|
|
27
|
+
/**
|
|
28
|
+
* Xplatform version definition for X-API XML.
|
|
29
|
+
*/
|
|
30
|
+
const XplatformVersion = {
|
|
31
|
+
xmlns: "http://www.tobesoft.com/platform/Dataset",
|
|
32
|
+
version: "4000"
|
|
11
33
|
};
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
34
|
+
/**
|
|
35
|
+
* Nexacro version definition for X-API XML.
|
|
36
|
+
*/
|
|
37
|
+
const NexaVersion = {
|
|
38
|
+
xmlns: "http://www.nexacroplatform.com/platform/dataset",
|
|
39
|
+
version: "4000"
|
|
15
40
|
};
|
|
41
|
+
/**
|
|
42
|
+
* Custom error class for column type related issues.
|
|
43
|
+
*/
|
|
16
44
|
var ColumnTypeError = class extends Error {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
45
|
+
constructor(message) {
|
|
46
|
+
super(message);
|
|
47
|
+
this.name = "ColumnTypeError";
|
|
48
|
+
}
|
|
21
49
|
};
|
|
22
50
|
var InvalidXmlError = class extends Error {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
51
|
+
constructor(message) {
|
|
52
|
+
super(message);
|
|
53
|
+
this.name = "InvalidXmlError";
|
|
54
|
+
}
|
|
27
55
|
};
|
|
28
56
|
|
|
29
|
-
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/xapi-data.ts
|
|
59
|
+
/**
|
|
60
|
+
* Represents the root of an X-API XML structure, containing datasets and parameters.
|
|
61
|
+
*/
|
|
62
|
+
var XapiRoot = class {
|
|
63
|
+
/** An array of datasets within the X-API root. */
|
|
64
|
+
datasets = [];
|
|
65
|
+
/** Parameters associated with the X-API root. */
|
|
66
|
+
parameters = { params: [] };
|
|
67
|
+
/**
|
|
68
|
+
* Creates an instance of XapiRoot.
|
|
69
|
+
* @param datasets - Initial array of datasets.
|
|
70
|
+
* @param parameters - Initial parameters.
|
|
71
|
+
*/
|
|
72
|
+
constructor(datasets = [], parameters = { params: [] }) {
|
|
73
|
+
this.datasets = datasets;
|
|
74
|
+
this.parameters = parameters;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Adds a dataset to the X-API root.
|
|
78
|
+
* @param dataset - The dataset to add.
|
|
79
|
+
*/
|
|
80
|
+
addDataset(dataset) {
|
|
81
|
+
this.datasets.push(dataset);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Retrieves a dataset by its ID.
|
|
85
|
+
* @param id - The ID of the dataset to retrieve.
|
|
86
|
+
* @returns The Dataset object, or undefined if not found.
|
|
87
|
+
*/
|
|
88
|
+
getDataset(id) {
|
|
89
|
+
return this.datasets.find((dataset) => dataset.id === id);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Adds a parameter to the X-API root.
|
|
93
|
+
* @param parameter - The parameter to add.
|
|
94
|
+
*/
|
|
95
|
+
addParameter(parameter) {
|
|
96
|
+
this.parameters.params.push(parameter);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Retrieves a parameter by its ID.
|
|
100
|
+
* @param id - The ID of the parameter to retrieve.
|
|
101
|
+
* @returns The Parameter object, or undefined if not found.
|
|
102
|
+
*/
|
|
103
|
+
getParameter(id) {
|
|
104
|
+
return this.parameters.params.find((param) => param.id === id);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Sets all parameters for the X-API root.
|
|
108
|
+
* @param parameters - The XapiParameters object to set.
|
|
109
|
+
*/
|
|
110
|
+
setParameters(parameters) {
|
|
111
|
+
this.parameters = parameters;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Sets the value of a specific parameter, or adds it if it doesn't exist.
|
|
115
|
+
* @param id - The ID of the parameter.
|
|
116
|
+
* @param value - The value to set for the parameter.
|
|
117
|
+
*/
|
|
118
|
+
setParameter(id, value) {
|
|
119
|
+
const param = this.parameters.params.find((p) => p.id === id);
|
|
120
|
+
if (param) param.value = value;
|
|
121
|
+
else this.addParameter({
|
|
122
|
+
id,
|
|
123
|
+
value
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Returns the number of parameters.
|
|
128
|
+
* @returns The count of parameters.
|
|
129
|
+
*/
|
|
130
|
+
parameterSize() {
|
|
131
|
+
return this.parameters.params.length;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Returns the number of datasets.
|
|
135
|
+
* @returns The count of datasets.
|
|
136
|
+
*/
|
|
137
|
+
datasetSize() {
|
|
138
|
+
return this.datasets.length;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Iterates over the parameters.
|
|
142
|
+
* @returns An IterableIterator for parameters.
|
|
143
|
+
*/
|
|
144
|
+
*iterParameters() {
|
|
145
|
+
for (const param of this.parameters.params) yield param;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Iterates over the datasets.
|
|
149
|
+
* @returns An IterableIterator for datasets.
|
|
150
|
+
*/
|
|
151
|
+
*iterDatasets() {
|
|
152
|
+
for (const dataset of this.datasets) yield dataset;
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* Represents a dataset within an X-API XML structure.
|
|
157
|
+
*/
|
|
158
|
+
var Dataset = class {
|
|
159
|
+
/** The ID of the dataset. */
|
|
160
|
+
id;
|
|
161
|
+
constColumns = [];
|
|
162
|
+
columns = [];
|
|
163
|
+
/** An array of rows in the dataset. */
|
|
164
|
+
rows = [];
|
|
165
|
+
_columnIndexMap = /* @__PURE__ */ new Map();
|
|
166
|
+
/**
|
|
167
|
+
* Creates an instance of Dataset.
|
|
168
|
+
* @param id - The ID of the dataset.
|
|
169
|
+
* @param constColumns - Initial array of constant columns.
|
|
170
|
+
* @param columns - Initial array of columns.
|
|
171
|
+
* @param rows - Initial array of rows.
|
|
172
|
+
*/
|
|
173
|
+
constructor(id, constColumns = [], columns = [], rows = []) {
|
|
174
|
+
this.id = id;
|
|
175
|
+
this.constColumns = constColumns;
|
|
176
|
+
this.columns = columns;
|
|
177
|
+
this.rows = rows;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Adds a column definition to the dataset.
|
|
181
|
+
* @param column - The column definition to add.
|
|
182
|
+
*/
|
|
183
|
+
addColumn(column) {
|
|
184
|
+
this.columns.push(column);
|
|
185
|
+
this._columnIndexMap.set(column.id, this.columns.length - 1);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Adds a constant column definition to the dataset.
|
|
189
|
+
* @param column - The constant column definition to add.
|
|
190
|
+
*/
|
|
191
|
+
addConstColumn(column) {
|
|
192
|
+
this.constColumns.push(column);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Creates a new empty row and adds it to the dataset.
|
|
196
|
+
* @returns The index of the newly created row.
|
|
197
|
+
*/
|
|
198
|
+
newRow() {
|
|
199
|
+
this.rows.push({ cols: [] });
|
|
200
|
+
return this.rows.length - 1;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Adds an existing row to the dataset.
|
|
204
|
+
* @param row - The row to add.
|
|
205
|
+
*/
|
|
206
|
+
addRow(row) {
|
|
207
|
+
this.rows.push(row);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Retrieves the index of a column by its ID.
|
|
211
|
+
* @param columnId - The ID of the column.
|
|
212
|
+
* @returns The index of the column, or undefined if not found.
|
|
213
|
+
*/
|
|
214
|
+
getColumnIndex(columnId) {
|
|
215
|
+
return this._columnIndexMap.get(columnId);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Retrieves the column definition by its ID.
|
|
219
|
+
* @param columnId - The ID of the column.
|
|
220
|
+
* @returns The Column object, or undefined if not found.
|
|
221
|
+
*/
|
|
222
|
+
getColumnInfo(columnId) {
|
|
223
|
+
const colIndex = this.getColumnIndex(columnId);
|
|
224
|
+
if (colIndex !== void 0) return this.columns[colIndex];
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Retrieves the constant column definition by its ID.
|
|
228
|
+
* @param columnId - The ID of the constant column.
|
|
229
|
+
* @returns The ConstColumn object, or undefined if not found.
|
|
230
|
+
*/
|
|
231
|
+
getConstColumnInfo(columnId) {
|
|
232
|
+
return this.constColumns.find((col) => col.id === columnId);
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Retrieves the value of a column in a specific row.
|
|
236
|
+
* @param rowIdx - The index of the row.
|
|
237
|
+
* @param columnId - The ID of the column.
|
|
238
|
+
* @returns The value of the column, or undefined if the row or column is not found.
|
|
239
|
+
* @throws {Error} if the column ID is not found in the dataset.
|
|
240
|
+
*/
|
|
241
|
+
getColumn(rowIdx, columnId) {
|
|
242
|
+
if (rowIdx < this.rows.length) {
|
|
243
|
+
const colIndex = this.getColumnIndex(columnId);
|
|
244
|
+
if (colIndex === void 0) throw new Error(`Column with id ${columnId} not found in dataset ${this.id}`);
|
|
245
|
+
return this.rows[rowIdx].cols[colIndex]?.value;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Retrieves the original value of a column in a specific row (for OrgRow).
|
|
250
|
+
* @param rowIdx - The index of the row.
|
|
251
|
+
* @param columnId - The ID of the column.
|
|
252
|
+
* @returns The original value of the column, or undefined if the row or column is not found.
|
|
253
|
+
* @throws {Error} if the column ID is not found in the dataset.
|
|
254
|
+
*/
|
|
255
|
+
getOrgColumn(rowIdx, columnId) {
|
|
256
|
+
if (rowIdx < this.rows.length) {
|
|
257
|
+
const colIndex = this.getColumnIndex(columnId);
|
|
258
|
+
if (colIndex === void 0) throw new Error(`Column with id ${columnId} not found in dataset ${this.id}`);
|
|
259
|
+
return (this.rows[rowIdx].orgRow?.[colIndex])?.value;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Sets the value of a column in a specific row.
|
|
264
|
+
* @param rowIdx - The index of the row.
|
|
265
|
+
* @param columnId - The ID of the column.
|
|
266
|
+
* @param value - The value to set.
|
|
267
|
+
* @throws {Error} if the row index is out of bounds or the column ID is not found.
|
|
268
|
+
*/
|
|
269
|
+
setColumn(rowIdx, columnId, value) {
|
|
270
|
+
if (rowIdx < this.rows.length) {
|
|
271
|
+
const colIndex = this.getColumnIndex(columnId);
|
|
272
|
+
if (colIndex === void 0) throw new Error(`Column with id ${columnId} not found in dataset ${this.id}`);
|
|
273
|
+
this.rows[rowIdx].cols[colIndex] = {
|
|
274
|
+
id: columnId,
|
|
275
|
+
value
|
|
276
|
+
};
|
|
277
|
+
} else throw new Error(`Row index ${rowIdx} out of bounds in dataset ${this.id}`);
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Iterates over the constant column definitions.
|
|
281
|
+
* @returns An IterableIterator for constant columns.
|
|
282
|
+
*/
|
|
283
|
+
*iterConstColumns() {
|
|
284
|
+
for (const column of this.constColumns) yield column;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Iterates over the column definitions.
|
|
288
|
+
* @returns An IterableIterator for columns.
|
|
289
|
+
*/
|
|
290
|
+
*iterColumns() {
|
|
291
|
+
for (const column of this.columns) yield column;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Iterates over the rows in the dataset.
|
|
295
|
+
* @returns An IterableIterator for rows.
|
|
296
|
+
*/
|
|
297
|
+
*iterRows() {
|
|
298
|
+
for (const row of this.rows) yield row;
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Returns the number of column definitions.
|
|
302
|
+
* @returns The count of columns.
|
|
303
|
+
*/
|
|
304
|
+
columnSize() {
|
|
305
|
+
return this.columns.length;
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Returns the number of constant column definitions.
|
|
309
|
+
* @returns The count of constant columns.
|
|
310
|
+
*/
|
|
311
|
+
constColumnSize() {
|
|
312
|
+
return this.constColumns.length;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Returns the number of rows in the dataset.
|
|
316
|
+
* @returns The count of rows.
|
|
317
|
+
*/
|
|
318
|
+
rowSize() {
|
|
319
|
+
return this.rows.length;
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
//#endregion
|
|
324
|
+
//#region src/utils.ts
|
|
30
325
|
function arrayBufferToString(buffer) {
|
|
31
|
-
|
|
32
|
-
return decoder.decode(buffer);
|
|
326
|
+
return new TextDecoder().decode(buffer);
|
|
33
327
|
}
|
|
328
|
+
/**
|
|
329
|
+
* Creates an array of entities for parsing XML, including control characters.
|
|
330
|
+
* @returns An array of objects, each with an `entity` (e.g., "") and its `value` (e.g., "\x01").
|
|
331
|
+
*/
|
|
34
332
|
function makeParseEntities() {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
333
|
+
const entities$1 = [];
|
|
334
|
+
for (let i = 1; i <= 32; i++) entities$1.push({
|
|
335
|
+
entity: `&#${i};`,
|
|
336
|
+
value: String.fromCharCode(i)
|
|
337
|
+
});
|
|
338
|
+
return entities$1;
|
|
40
339
|
}
|
|
340
|
+
/**
|
|
341
|
+
* Creates an array of entities for writing XML, including control characters.
|
|
342
|
+
* @returns An array of objects, each with an `entity` (e.g., "") and its `value` (e.g., "\x01").
|
|
343
|
+
*/
|
|
41
344
|
function makeWriterEntities() {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
345
|
+
const entities$1 = [];
|
|
346
|
+
for (let i = 1; i <= 32; i++) entities$1.push({
|
|
347
|
+
entity: `&#${i};`,
|
|
348
|
+
value: String.fromCharCode(i)
|
|
349
|
+
});
|
|
350
|
+
return entities$1;
|
|
47
351
|
}
|
|
352
|
+
/**
|
|
353
|
+
* Converts a Base64 string to a Uint8Array.
|
|
354
|
+
* @param base64String - The Base64 string to convert.
|
|
355
|
+
* @returns A Uint8Array.
|
|
356
|
+
*/
|
|
48
357
|
function base64ToUint8Array(base64String) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
return bytes;
|
|
358
|
+
const binaryString = atob(base64String);
|
|
359
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
360
|
+
for (let i = 0; i < binaryString.length; i++) bytes[i] = binaryString.charCodeAt(i);
|
|
361
|
+
return bytes;
|
|
55
362
|
}
|
|
363
|
+
/**
|
|
364
|
+
* Converts a Uint8Array to a Base64 string.
|
|
365
|
+
* @param uint8Array - The Uint8Array to convert.
|
|
366
|
+
* @returns A Base64 string.
|
|
367
|
+
*/
|
|
56
368
|
function uint8ArrayToBase64(uint8Array) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
return btoa(binaryString);
|
|
369
|
+
let binaryString = "";
|
|
370
|
+
for (let i = 0; i < uint8Array.length; i++) binaryString += String.fromCharCode(uint8Array[i]);
|
|
371
|
+
return btoa(binaryString);
|
|
62
372
|
}
|
|
373
|
+
/**
|
|
374
|
+
* Converts a string representation of a date/time to a Date object.
|
|
375
|
+
* Supports "yyyyMMdd", "yyyyMMddHHmmss", "yyyyMMddHHmmssSSS", and "HHmmss" formats.
|
|
376
|
+
* @param value - The string to convert.
|
|
377
|
+
* @returns A Date object if the string is a valid date/time, otherwise undefined.
|
|
378
|
+
*/
|
|
63
379
|
function stringToDate(value) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
if (year < 1970 || year > 9999 || month < 0 || month > 11 || day < 1 || day > 31 || hours < 0 || hours > 23 || minutes < 0 || minutes > 59 || seconds < 0 || seconds > 59 || milliseconds < 0 || milliseconds > 999) {
|
|
97
|
-
return void 0;
|
|
98
|
-
}
|
|
99
|
-
const date = new Date(year, month, day, hours, minutes, seconds, milliseconds);
|
|
100
|
-
if (isNaN(date.getTime())) {
|
|
101
|
-
return void 0;
|
|
102
|
-
}
|
|
103
|
-
return date;
|
|
380
|
+
if (!value) return void 0;
|
|
381
|
+
let year;
|
|
382
|
+
let month;
|
|
383
|
+
let day;
|
|
384
|
+
let hours = 0;
|
|
385
|
+
let minutes = 0;
|
|
386
|
+
let seconds = 0;
|
|
387
|
+
let milliseconds = 0;
|
|
388
|
+
if (value.length < 6 || value.length > 16) return;
|
|
389
|
+
if (value.length >= 8) {
|
|
390
|
+
year = parseInt(value.substring(0, 4), 10);
|
|
391
|
+
month = parseInt(value.substring(4, 6), 10) - 1;
|
|
392
|
+
day = parseInt(value.substring(6, 8), 10);
|
|
393
|
+
} else {
|
|
394
|
+
year = 1970;
|
|
395
|
+
month = 0;
|
|
396
|
+
day = 1;
|
|
397
|
+
}
|
|
398
|
+
if (value.length === 6) {
|
|
399
|
+
hours = parseInt(value.substring(0, 2), 10);
|
|
400
|
+
minutes = parseInt(value.substring(2, 4), 10);
|
|
401
|
+
seconds = parseInt(value.substring(4, 6), 10);
|
|
402
|
+
} else if (value.length >= 10) {
|
|
403
|
+
hours = parseInt(value.substring(8, 10), 10);
|
|
404
|
+
minutes = parseInt(value.substring(10, 12), 10);
|
|
405
|
+
seconds = parseInt(value.substring(12, 14), 10);
|
|
406
|
+
}
|
|
407
|
+
if (value.length === 16) milliseconds = parseInt(value.substring(14, 16), 10);
|
|
408
|
+
if (year < 1970 || year > 9999 || month < 0 || month > 11 || day < 1 || day > 31 || hours < 0 || hours > 23 || minutes < 0 || minutes > 59 || seconds < 0 || seconds > 59 || milliseconds < 0 || milliseconds > 999) return;
|
|
409
|
+
const date = new Date(year, month, day, hours, minutes, seconds, milliseconds);
|
|
410
|
+
if (isNaN(date.getTime())) return;
|
|
411
|
+
return date;
|
|
104
412
|
}
|
|
413
|
+
/**
|
|
414
|
+
* Converts a Date object to a string representation based on the specified column type.
|
|
415
|
+
* @param date - The Date object to convert.
|
|
416
|
+
* @param type - The column type ("DATE", "DATETIME", or "TIME").
|
|
417
|
+
* @returns A string representation of the date/time.
|
|
418
|
+
*/
|
|
105
419
|
function dateToString(date, type) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
return `${hours}${minutes}${seconds}`;
|
|
119
|
-
default:
|
|
120
|
-
return "";
|
|
121
|
-
}
|
|
420
|
+
const year = date.getFullYear().toString().padStart(4, "0");
|
|
421
|
+
const month = (date.getMonth() + 1).toString().padStart(2, "0");
|
|
422
|
+
const day = date.getDate().toString().padStart(2, "0");
|
|
423
|
+
const hours = date.getHours().toString().padStart(2, "0");
|
|
424
|
+
const minutes = date.getMinutes().toString().padStart(2, "0");
|
|
425
|
+
const seconds = date.getSeconds().toString().padStart(2, "0");
|
|
426
|
+
switch (type) {
|
|
427
|
+
case "DATE": return `${year}${month}${day}`;
|
|
428
|
+
case "DATETIME": return `${year}${month}${day}${hours}${minutes}${seconds}`;
|
|
429
|
+
case "TIME": return `${hours}${minutes}${seconds}`;
|
|
430
|
+
default: return "";
|
|
431
|
+
}
|
|
122
432
|
}
|
|
433
|
+
/**
|
|
434
|
+
* A WritableStream that collects all written Uint8Array chunks and provides them as a single string.
|
|
435
|
+
*/
|
|
123
436
|
var StringWritableStream = class extends WritableStream {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
437
|
+
result = "";
|
|
438
|
+
constructor() {
|
|
439
|
+
const decoder = new TextDecoder();
|
|
440
|
+
super({
|
|
441
|
+
write: (chunk) => {
|
|
442
|
+
this.result += decoder.decode(chunk, { stream: true });
|
|
443
|
+
},
|
|
444
|
+
close: () => {
|
|
445
|
+
this.result += decoder.decode();
|
|
446
|
+
}
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* Returns the collected string result.
|
|
451
|
+
* @returns The concatenated string from all written chunks.
|
|
452
|
+
*/
|
|
453
|
+
getResult() {
|
|
454
|
+
return this.result;
|
|
455
|
+
}
|
|
143
456
|
};
|
|
457
|
+
/**
|
|
458
|
+
* Converts a string to a ReadableStream of Uint8Array.
|
|
459
|
+
* @param str - The string to convert.
|
|
460
|
+
* @returns A ReadableStream containing the string as Uint8Array.
|
|
461
|
+
*/
|
|
144
462
|
function stringToReadableStream(str) {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
controller.close();
|
|
151
|
-
}
|
|
152
|
-
});
|
|
463
|
+
const bytes = new TextEncoder().encode(str);
|
|
464
|
+
return new ReadableStream({ start(controller) {
|
|
465
|
+
controller.enqueue(bytes);
|
|
466
|
+
controller.close();
|
|
467
|
+
} });
|
|
153
468
|
}
|
|
469
|
+
/**
|
|
470
|
+
* Converts a value to the specified ColumnType.
|
|
471
|
+
* @param value - The value to convert.
|
|
472
|
+
* @param type - The target ColumnType.
|
|
473
|
+
* @returns The converted value, or the original value if conversion fails or is not applicable.
|
|
474
|
+
* @throws {ColumnTypeError} if the column type is unsupported.
|
|
475
|
+
*/
|
|
154
476
|
function convertToColumnType(value, type) {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
return value;
|
|
178
|
-
default:
|
|
179
|
-
throw new ColumnTypeError(`Unsupported column type: ${type}`);
|
|
180
|
-
}
|
|
477
|
+
switch (type) {
|
|
478
|
+
case "INT":
|
|
479
|
+
case "BIGDECIMAL":
|
|
480
|
+
const intValue = parseInt(value, 10);
|
|
481
|
+
return isNaN(intValue) ? value : intValue;
|
|
482
|
+
case "FLOAT":
|
|
483
|
+
const floatValue = parseFloat(value);
|
|
484
|
+
return isNaN(floatValue) ? value : floatValue;
|
|
485
|
+
case "DECIMAL":
|
|
486
|
+
const decimalValue = parseFloat(value);
|
|
487
|
+
return isNaN(decimalValue) ? value : decimalValue;
|
|
488
|
+
case "DATE":
|
|
489
|
+
case "DATETIME":
|
|
490
|
+
case "TIME": return stringToDate(value) || value;
|
|
491
|
+
case "BLOB": try {
|
|
492
|
+
return base64ToUint8Array(value);
|
|
493
|
+
} catch {
|
|
494
|
+
return value;
|
|
495
|
+
}
|
|
496
|
+
case "STRING": return value;
|
|
497
|
+
default: throw new ColumnTypeError(`Unsupported column type: ${type}`);
|
|
498
|
+
}
|
|
181
499
|
}
|
|
500
|
+
/**
|
|
501
|
+
* Converts an XapiValueType to its string representation based on the specified ColumnType.
|
|
502
|
+
* @param value - The value to convert.
|
|
503
|
+
* @param type - The ColumnType to guide the conversion.
|
|
504
|
+
* @returns The string representation of the value.
|
|
505
|
+
*/
|
|
182
506
|
function convertToString(value, type) {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
case "STRING":
|
|
196
|
-
return String(value);
|
|
197
|
-
default:
|
|
198
|
-
return String(value);
|
|
199
|
-
}
|
|
507
|
+
switch (type) {
|
|
508
|
+
case "INT":
|
|
509
|
+
case "BIGDECIMAL":
|
|
510
|
+
case "FLOAT":
|
|
511
|
+
case "DECIMAL": return String(value);
|
|
512
|
+
case "DATE":
|
|
513
|
+
case "DATETIME":
|
|
514
|
+
case "TIME": return dateToString(value, type);
|
|
515
|
+
case "BLOB": return uint8ArrayToBase64(value);
|
|
516
|
+
case "STRING": return String(value);
|
|
517
|
+
default: return String(value);
|
|
518
|
+
}
|
|
200
519
|
}
|
|
201
|
-
|
|
520
|
+
const entities = makeParseEntities();
|
|
202
521
|
function _unescapeXml(str) {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
522
|
+
if (!str) return str;
|
|
523
|
+
const regex = new RegExp(entities.map((e) => e.entity).join("|"), "g");
|
|
524
|
+
return str.replace(regex, (match) => {
|
|
525
|
+
const entity = entities.find((e) => e.entity === match);
|
|
526
|
+
return entity ? entity.value : match;
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
function isXapiRoot(value) {
|
|
530
|
+
return value instanceof XapiRoot;
|
|
209
531
|
}
|
|
210
532
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
parameters = { params: [] };
|
|
217
|
-
/**
|
|
218
|
-
* Creates an instance of XapiRoot.
|
|
219
|
-
* @param datasets - Initial array of datasets.
|
|
220
|
-
* @param parameters - Initial parameters.
|
|
221
|
-
*/
|
|
222
|
-
constructor(datasets = [], parameters = { params: [] }) {
|
|
223
|
-
this.datasets = datasets;
|
|
224
|
-
this.parameters = parameters;
|
|
225
|
-
}
|
|
226
|
-
/**
|
|
227
|
-
* Adds a dataset to the X-API root.
|
|
228
|
-
* @param dataset - The dataset to add.
|
|
229
|
-
*/
|
|
230
|
-
addDataset(dataset) {
|
|
231
|
-
this.datasets.push(dataset);
|
|
232
|
-
}
|
|
233
|
-
/**
|
|
234
|
-
* Retrieves a dataset by its ID.
|
|
235
|
-
* @param id - The ID of the dataset to retrieve.
|
|
236
|
-
* @returns The Dataset object, or undefined if not found.
|
|
237
|
-
*/
|
|
238
|
-
getDataset(id) {
|
|
239
|
-
return this.datasets.find((dataset) => dataset.id === id);
|
|
240
|
-
}
|
|
241
|
-
/**
|
|
242
|
-
* Adds a parameter to the X-API root.
|
|
243
|
-
* @param parameter - The parameter to add.
|
|
244
|
-
*/
|
|
245
|
-
addParameter(parameter) {
|
|
246
|
-
this.parameters.params.push(parameter);
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* Retrieves a parameter by its ID.
|
|
250
|
-
* @param id - The ID of the parameter to retrieve.
|
|
251
|
-
* @returns The Parameter object, or undefined if not found.
|
|
252
|
-
*/
|
|
253
|
-
getParameter(id) {
|
|
254
|
-
return this.parameters.params.find((param) => param.id === id);
|
|
255
|
-
}
|
|
256
|
-
/**
|
|
257
|
-
* Sets all parameters for the X-API root.
|
|
258
|
-
* @param parameters - The XapiParameters object to set.
|
|
259
|
-
*/
|
|
260
|
-
setParameters(parameters) {
|
|
261
|
-
this.parameters = parameters;
|
|
262
|
-
}
|
|
263
|
-
/**
|
|
264
|
-
* Sets the value of a specific parameter, or adds it if it doesn't exist.
|
|
265
|
-
* @param id - The ID of the parameter.
|
|
266
|
-
* @param value - The value to set for the parameter.
|
|
267
|
-
*/
|
|
268
|
-
setParameter(id, value) {
|
|
269
|
-
const param = this.parameters.params.find((p) => p.id === id);
|
|
270
|
-
if (param) {
|
|
271
|
-
param.value = value;
|
|
272
|
-
} else {
|
|
273
|
-
this.addParameter({ id, value });
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
/**
|
|
277
|
-
* Returns the number of parameters.
|
|
278
|
-
* @returns The count of parameters.
|
|
279
|
-
*/
|
|
280
|
-
parameterSize() {
|
|
281
|
-
return this.parameters.params.length;
|
|
282
|
-
}
|
|
283
|
-
/**
|
|
284
|
-
* Returns the number of datasets.
|
|
285
|
-
* @returns The count of datasets.
|
|
286
|
-
*/
|
|
287
|
-
datasetSize() {
|
|
288
|
-
return this.datasets.length;
|
|
289
|
-
}
|
|
290
|
-
/**
|
|
291
|
-
* Iterates over the parameters.
|
|
292
|
-
* @returns An IterableIterator for parameters.
|
|
293
|
-
*/
|
|
294
|
-
*iterParameters() {
|
|
295
|
-
for (const param of this.parameters.params) {
|
|
296
|
-
yield param;
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
/**
|
|
300
|
-
* Iterates over the datasets.
|
|
301
|
-
* @returns An IterableIterator for datasets.
|
|
302
|
-
*/
|
|
303
|
-
*iterDatasets() {
|
|
304
|
-
for (const dataset of this.datasets) {
|
|
305
|
-
yield dataset;
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
};
|
|
309
|
-
var Dataset = class {
|
|
310
|
-
/** The ID of the dataset. */
|
|
311
|
-
id;
|
|
312
|
-
constColumns = [];
|
|
313
|
-
columns = [];
|
|
314
|
-
/** An array of rows in the dataset. */
|
|
315
|
-
rows = [];
|
|
316
|
-
_columnIndexMap = /* @__PURE__ */ new Map();
|
|
317
|
-
/**
|
|
318
|
-
* Creates an instance of Dataset.
|
|
319
|
-
* @param id - The ID of the dataset.
|
|
320
|
-
* @param constColumns - Initial array of constant columns.
|
|
321
|
-
* @param columns - Initial array of columns.
|
|
322
|
-
* @param rows - Initial array of rows.
|
|
323
|
-
*/
|
|
324
|
-
constructor(id, constColumns = [], columns = [], rows = []) {
|
|
325
|
-
this.id = id;
|
|
326
|
-
this.constColumns = constColumns;
|
|
327
|
-
this.columns = columns;
|
|
328
|
-
this.rows = rows;
|
|
329
|
-
}
|
|
330
|
-
/**
|
|
331
|
-
* Adds a column definition to the dataset.
|
|
332
|
-
* @param column - The column definition to add.
|
|
333
|
-
*/
|
|
334
|
-
addColumn(column) {
|
|
335
|
-
this.columns.push(column);
|
|
336
|
-
this._columnIndexMap.set(column.id, this.columns.length - 1);
|
|
337
|
-
}
|
|
338
|
-
/**
|
|
339
|
-
* Adds a constant column definition to the dataset.
|
|
340
|
-
* @param column - The constant column definition to add.
|
|
341
|
-
*/
|
|
342
|
-
addConstColumn(column) {
|
|
343
|
-
this.constColumns.push(column);
|
|
344
|
-
}
|
|
345
|
-
/**
|
|
346
|
-
* Creates a new empty row and adds it to the dataset.
|
|
347
|
-
* @returns The index of the newly created row.
|
|
348
|
-
*/
|
|
349
|
-
newRow() {
|
|
350
|
-
this.rows.push({ cols: [] });
|
|
351
|
-
return this.rows.length - 1;
|
|
352
|
-
}
|
|
353
|
-
/**
|
|
354
|
-
* Adds an existing row to the dataset.
|
|
355
|
-
* @param row - The row to add.
|
|
356
|
-
*/
|
|
357
|
-
addRow(row) {
|
|
358
|
-
this.rows.push(row);
|
|
359
|
-
}
|
|
360
|
-
/**
|
|
361
|
-
* Retrieves the index of a column by its ID.
|
|
362
|
-
* @param columnId - The ID of the column.
|
|
363
|
-
* @returns The index of the column, or undefined if not found.
|
|
364
|
-
*/
|
|
365
|
-
getColumnIndex(columnId) {
|
|
366
|
-
return this._columnIndexMap.get(columnId);
|
|
367
|
-
}
|
|
368
|
-
/**
|
|
369
|
-
* Retrieves the column definition by its ID.
|
|
370
|
-
* @param columnId - The ID of the column.
|
|
371
|
-
* @returns The Column object, or undefined if not found.
|
|
372
|
-
*/
|
|
373
|
-
getColumnInfo(columnId) {
|
|
374
|
-
const colIndex = this.getColumnIndex(columnId);
|
|
375
|
-
if (colIndex !== void 0) {
|
|
376
|
-
return this.columns[colIndex];
|
|
377
|
-
}
|
|
378
|
-
return void 0;
|
|
379
|
-
}
|
|
380
|
-
/**
|
|
381
|
-
* Retrieves the constant column definition by its ID.
|
|
382
|
-
* @param columnId - The ID of the constant column.
|
|
383
|
-
* @returns The ConstColumn object, or undefined if not found.
|
|
384
|
-
*/
|
|
385
|
-
getConstColumnInfo(columnId) {
|
|
386
|
-
return this.constColumns.find((col) => col.id === columnId);
|
|
387
|
-
}
|
|
388
|
-
/**
|
|
389
|
-
* Retrieves the value of a column in a specific row.
|
|
390
|
-
* @param rowIdx - The index of the row.
|
|
391
|
-
* @param columnId - The ID of the column.
|
|
392
|
-
* @returns The value of the column, or undefined if the row or column is not found.
|
|
393
|
-
* @throws {Error} if the column ID is not found in the dataset.
|
|
394
|
-
*/
|
|
395
|
-
getColumn(rowIdx, columnId) {
|
|
396
|
-
if (rowIdx < this.rows.length) {
|
|
397
|
-
const colIndex = this.getColumnIndex(columnId);
|
|
398
|
-
if (colIndex === void 0) {
|
|
399
|
-
throw new Error(`Column with id ${columnId} not found in dataset ${this.id}`);
|
|
400
|
-
}
|
|
401
|
-
const col = this.rows[rowIdx].cols[colIndex];
|
|
402
|
-
return col?.value;
|
|
403
|
-
}
|
|
404
|
-
return void 0;
|
|
405
|
-
}
|
|
406
|
-
/**
|
|
407
|
-
* Retrieves the original value of a column in a specific row (for OrgRow).
|
|
408
|
-
* @param rowIdx - The index of the row.
|
|
409
|
-
* @param columnId - The ID of the column.
|
|
410
|
-
* @returns The original value of the column, or undefined if the row or column is not found.
|
|
411
|
-
* @throws {Error} if the column ID is not found in the dataset.
|
|
412
|
-
*/
|
|
413
|
-
getOrgColumn(rowIdx, columnId) {
|
|
414
|
-
if (rowIdx < this.rows.length) {
|
|
415
|
-
const colIndex = this.getColumnIndex(columnId);
|
|
416
|
-
if (colIndex === void 0) {
|
|
417
|
-
throw new Error(`Column with id ${columnId} not found in dataset ${this.id}`);
|
|
418
|
-
}
|
|
419
|
-
const col = this.rows[rowIdx].orgRow?.[colIndex];
|
|
420
|
-
return col?.value;
|
|
421
|
-
}
|
|
422
|
-
return void 0;
|
|
423
|
-
}
|
|
424
|
-
/**
|
|
425
|
-
* Sets the value of a column in a specific row.
|
|
426
|
-
* @param rowIdx - The index of the row.
|
|
427
|
-
* @param columnId - The ID of the column.
|
|
428
|
-
* @param value - The value to set.
|
|
429
|
-
* @throws {Error} if the row index is out of bounds or the column ID is not found.
|
|
430
|
-
*/
|
|
431
|
-
setColumn(rowIdx, columnId, value) {
|
|
432
|
-
if (rowIdx < this.rows.length) {
|
|
433
|
-
const colIndex = this.getColumnIndex(columnId);
|
|
434
|
-
if (colIndex === void 0) {
|
|
435
|
-
throw new Error(`Column with id ${columnId} not found in dataset ${this.id}`);
|
|
436
|
-
}
|
|
437
|
-
this.rows[rowIdx].cols[colIndex] = { id: columnId, value };
|
|
438
|
-
} else {
|
|
439
|
-
throw new Error(`Row index ${rowIdx} out of bounds in dataset ${this.id}`);
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
/**
|
|
443
|
-
* Iterates over the constant column definitions.
|
|
444
|
-
* @returns An IterableIterator for constant columns.
|
|
445
|
-
*/
|
|
446
|
-
*iterConstColumns() {
|
|
447
|
-
for (const column of this.constColumns) {
|
|
448
|
-
yield column;
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
/**
|
|
452
|
-
* Iterates over the column definitions.
|
|
453
|
-
* @returns An IterableIterator for columns.
|
|
454
|
-
*/
|
|
455
|
-
*iterColumns() {
|
|
456
|
-
for (const column of this.columns) {
|
|
457
|
-
yield column;
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
/**
|
|
461
|
-
* Iterates over the rows in the dataset.
|
|
462
|
-
* @returns An IterableIterator for rows.
|
|
463
|
-
*/
|
|
464
|
-
*iterRows() {
|
|
465
|
-
for (const row of this.rows) {
|
|
466
|
-
yield row;
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
/**
|
|
470
|
-
* Returns the number of column definitions.
|
|
471
|
-
* @returns The count of columns.
|
|
472
|
-
*/
|
|
473
|
-
columnSize() {
|
|
474
|
-
return this.columns.length;
|
|
475
|
-
}
|
|
476
|
-
/**
|
|
477
|
-
* Returns the number of constant column definitions.
|
|
478
|
-
* @returns The count of constant columns.
|
|
479
|
-
*/
|
|
480
|
-
constColumnSize() {
|
|
481
|
-
return this.constColumns.length;
|
|
482
|
-
}
|
|
483
|
-
/**
|
|
484
|
-
* Returns the number of rows in the dataset.
|
|
485
|
-
* @returns The count of rows.
|
|
486
|
-
*/
|
|
487
|
-
rowSize() {
|
|
488
|
-
return this.rows.length;
|
|
489
|
-
}
|
|
490
|
-
};
|
|
491
|
-
|
|
492
|
-
// src/handler.ts
|
|
493
|
-
var defaultOptions = {
|
|
494
|
-
xapiVersion: NexaVersion,
|
|
495
|
-
parseToTypes: true
|
|
496
|
-
};
|
|
497
|
-
var _options = {
|
|
498
|
-
...defaultOptions
|
|
533
|
+
//#endregion
|
|
534
|
+
//#region src/handler.ts
|
|
535
|
+
let _options = {
|
|
536
|
+
xapiVersion: NexaVersion,
|
|
537
|
+
parseToTypes: true
|
|
499
538
|
};
|
|
539
|
+
/**
|
|
540
|
+
* Initializes the X-API handler with the provided options.
|
|
541
|
+
* @param options - The options to initialize the X-API handler.
|
|
542
|
+
* @returns void
|
|
543
|
+
*/
|
|
500
544
|
function initXapi(options) {
|
|
501
|
-
|
|
502
|
-
...options
|
|
503
|
-
};
|
|
545
|
+
_options = { ...options };
|
|
504
546
|
}
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
547
|
+
/**
|
|
548
|
+
* Parses an XML string into an XapiRoot object using txml.
|
|
549
|
+
* @param xml - The string containing the XML data.
|
|
550
|
+
* @returns A Promise that resolves to an XapiRoot object.
|
|
551
|
+
*/
|
|
552
|
+
function parse(xml) {
|
|
553
|
+
const parsedXml = txml.parse(xml);
|
|
554
|
+
const xapiRoot = new XapiRoot();
|
|
555
|
+
let rootElement;
|
|
556
|
+
for (let i = 0; i < parsedXml.length; i++) if (parsedXml[i].tagName === "Root") {
|
|
557
|
+
rootElement = parsedXml[i];
|
|
558
|
+
break;
|
|
559
|
+
}
|
|
560
|
+
if (rootElement === void 0) return xapiRoot;
|
|
561
|
+
const rootChildren = rootElement.children;
|
|
562
|
+
if (rootChildren && rootChildren.length > 0) for (let i = 0; i < rootChildren.length; i++) {
|
|
563
|
+
const child = rootChildren[i];
|
|
564
|
+
const tagName = child.tagName;
|
|
565
|
+
if (tagName === "Parameters") parseParameters(child, xapiRoot);
|
|
566
|
+
else if (tagName === "Dataset") {
|
|
567
|
+
if (!child.attributes || !child.attributes.id) throw new InvalidXmlError("Dataset element must have an 'id' attribute");
|
|
568
|
+
const datasetId = child.attributes.id;
|
|
569
|
+
const dataset = new Dataset(datasetId);
|
|
570
|
+
xapiRoot.addDataset(dataset);
|
|
571
|
+
const datasetChildren = child.children;
|
|
572
|
+
let columnInfoElement;
|
|
573
|
+
let rowsElement;
|
|
574
|
+
if (datasetChildren) for (let j = 0; j < datasetChildren.length; j++) {
|
|
575
|
+
const datasetChild = datasetChildren[j];
|
|
576
|
+
const childTagName = datasetChild.tagName;
|
|
577
|
+
if (childTagName === "ColumnInfo") columnInfoElement = datasetChild;
|
|
578
|
+
else if (childTagName === "Rows") rowsElement = datasetChild;
|
|
579
|
+
if (columnInfoElement && rowsElement) break;
|
|
580
|
+
}
|
|
581
|
+
parseColumnInfo(columnInfoElement, dataset);
|
|
582
|
+
parseRows(rowsElement, dataset);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
return xapiRoot;
|
|
531
586
|
}
|
|
532
587
|
function parseValue(value, type = "STRING") {
|
|
533
|
-
|
|
534
|
-
|
|
588
|
+
value = _unescapeXml(value);
|
|
589
|
+
return _options.parseToTypes ? convertToColumnType(value, type) : value;
|
|
535
590
|
}
|
|
536
591
|
function parseColumnInfo(columnInfoElement, dataset) {
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
592
|
+
if (!columnInfoElement) throw new InvalidXmlError("ColumnInfo element is missing in the dataset");
|
|
593
|
+
const children = columnInfoElement.children;
|
|
594
|
+
if (!children) return;
|
|
595
|
+
for (let i = 0; i < children.length; i++) {
|
|
596
|
+
const colInfo = children[i];
|
|
597
|
+
const tagName = colInfo.tagName;
|
|
598
|
+
if (tagName === "ConstColumn") {
|
|
599
|
+
const attrs = colInfo.attributes;
|
|
600
|
+
const sizeStr = attrs?.size;
|
|
601
|
+
dataset.addConstColumn({
|
|
602
|
+
id: attrs?.id,
|
|
603
|
+
size: sizeStr ? parseInt(sizeStr, 10) : 0,
|
|
604
|
+
type: attrs?.type || "STRING",
|
|
605
|
+
value: parseValue(attrs?.value, attrs?.type)
|
|
606
|
+
});
|
|
607
|
+
} else if (tagName === "Column") {
|
|
608
|
+
const attrs = colInfo.attributes;
|
|
609
|
+
const sizeStr = attrs?.size;
|
|
610
|
+
dataset.addColumn({
|
|
611
|
+
id: attrs?.id,
|
|
612
|
+
size: sizeStr ? parseInt(sizeStr, 10) : 0,
|
|
613
|
+
type: attrs?.type || "STRING"
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
}
|
|
554
617
|
}
|
|
555
618
|
function parseRows(rowsElement, dataset) {
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
619
|
+
const rows = rowsElement?.children;
|
|
620
|
+
if (!rows) return;
|
|
621
|
+
const datasetRows = dataset.rows;
|
|
622
|
+
for (let i = 0; i < rows.length; i++) {
|
|
623
|
+
const r = rows[i];
|
|
624
|
+
const tagName = r.tagName;
|
|
625
|
+
if (tagName === "Row") {
|
|
626
|
+
const rowIndex = dataset.newRow();
|
|
627
|
+
const currentRow = datasetRows[rowIndex];
|
|
628
|
+
currentRow.type = r.attributes?.type || void 0;
|
|
629
|
+
const cols = r.children;
|
|
630
|
+
if (!cols) continue;
|
|
631
|
+
for (let j = 0; j < cols.length; j++) {
|
|
632
|
+
const col = cols[j];
|
|
633
|
+
const colTagName = col.tagName;
|
|
634
|
+
if (colTagName === "Col") {
|
|
635
|
+
const colId = col.attributes?.id;
|
|
636
|
+
const value = col.children?.[0];
|
|
637
|
+
const columnInfo = dataset.getColumnInfo(colId);
|
|
638
|
+
if (!columnInfo) throw new InvalidXmlError(`Column with id ${colId} not found in dataset ${dataset.id}`);
|
|
639
|
+
const castedValue = parseValue(value, columnInfo.type);
|
|
640
|
+
currentRow.cols.push({
|
|
641
|
+
id: colId,
|
|
642
|
+
value: castedValue
|
|
643
|
+
});
|
|
644
|
+
} else if (colTagName === "OrgRow") {
|
|
645
|
+
const orgRow = [];
|
|
646
|
+
currentRow.orgRow = orgRow;
|
|
647
|
+
const orgCols = col.children;
|
|
648
|
+
if (!orgCols) continue;
|
|
649
|
+
for (let k = 0; k < orgCols.length; k++) {
|
|
650
|
+
const orgCol = orgCols[k];
|
|
651
|
+
if (orgCol.tagName === "Col") {
|
|
652
|
+
const colId = orgCol.attributes?.id;
|
|
653
|
+
const value = orgCol.children?.[0];
|
|
654
|
+
const columnInfo = dataset.getColumnInfo(colId);
|
|
655
|
+
const castedValue = parseValue(value, columnInfo.type);
|
|
656
|
+
orgRow.push({
|
|
657
|
+
id: colId,
|
|
658
|
+
value: castedValue
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
} else if (tagName === "Col") throw new InvalidXmlError("Row must be defined before Col");
|
|
665
|
+
else if (tagName === "OrgRow") throw new InvalidXmlError("Row must be defined before OrgRow");
|
|
666
|
+
}
|
|
589
667
|
}
|
|
590
668
|
function parseParameters(parametersElement, xapiRoot) {
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
669
|
+
if (!parametersElement) return;
|
|
670
|
+
const children = parametersElement.children;
|
|
671
|
+
if (!children) return;
|
|
672
|
+
for (let i = 0; i < children.length; i++) {
|
|
673
|
+
const p = children[i];
|
|
674
|
+
if (p.tagName === "Parameter") {
|
|
675
|
+
const attrs = p.attributes;
|
|
676
|
+
const id = attrs?.id;
|
|
677
|
+
const type = attrs?.type || "STRING";
|
|
678
|
+
const value = p.children?.[0];
|
|
679
|
+
xapiRoot.addParameter({
|
|
680
|
+
id,
|
|
681
|
+
type,
|
|
682
|
+
value: parseValue(value, type)
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
}
|
|
604
686
|
}
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
687
|
+
function write(root) {
|
|
688
|
+
const writer = new StaxXmlWriterSync({
|
|
689
|
+
addEntities: makeWriterEntities(),
|
|
690
|
+
encoding: "UTF-8",
|
|
691
|
+
indentString: " ",
|
|
692
|
+
prettyPrint: true
|
|
693
|
+
});
|
|
694
|
+
writer.writeStartDocument("1.0", "UTF-8");
|
|
695
|
+
writer.writeStartElement("Root", { attributes: {
|
|
696
|
+
xmlns: _options.xapiVersion?.xmlns || NexaVersion.xmlns,
|
|
697
|
+
version: _options.xapiVersion?.version || NexaVersion.version
|
|
698
|
+
} });
|
|
699
|
+
if (root.parameterSize() > 0) writeParameters(writer, root.iterParameters());
|
|
700
|
+
if (root.datasetSize() > 0) {
|
|
701
|
+
writer.writeStartElement("Datasets");
|
|
702
|
+
for (const dataset of root.iterDatasets()) writeDataset(writer, dataset);
|
|
703
|
+
writer.writeEndElement();
|
|
704
|
+
}
|
|
705
|
+
writer.writeEndElement();
|
|
706
|
+
return writer.getXmlString();
|
|
609
707
|
}
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
});
|
|
625
|
-
if (root.parameterSize() > 0) {
|
|
626
|
-
await writeParameters(writer, root.iterParameters());
|
|
627
|
-
}
|
|
628
|
-
if (root.datasetSize() > 0) {
|
|
629
|
-
await writer.writeStartElement("Datasets");
|
|
630
|
-
for (const dataset of root.iterDatasets()) {
|
|
631
|
-
await writeDataset(writer, dataset);
|
|
632
|
-
}
|
|
633
|
-
await writer.writeEndElement();
|
|
634
|
-
}
|
|
635
|
-
await writer.writeEndElement();
|
|
708
|
+
function writeParameters(writer, iterator) {
|
|
709
|
+
if (iterator) {
|
|
710
|
+
writer.writeStartElement("Parameters");
|
|
711
|
+
for (const parameter of iterator) {
|
|
712
|
+
writer.writeStartElement("Parameter", { attributes: { id: parameter.id } });
|
|
713
|
+
if (parameter.type !== void 0) writer.writeAttribute("type", parameter.type);
|
|
714
|
+
if (parameter.value !== void 0) if (typeof parameter.value === "string") writer.writeAttribute("value", parameter.value);
|
|
715
|
+
else if (parameter.value instanceof Date) writer.writeAttribute("value", dateToString(parameter.value, parameter.type));
|
|
716
|
+
else if (parameter.value instanceof Uint8Array) writer.writeAttribute("value", uint8ArrayToBase64(parameter.value));
|
|
717
|
+
else writer.writeAttribute("value", String(parameter.value));
|
|
718
|
+
writer.writeEndElement();
|
|
719
|
+
}
|
|
720
|
+
writer.writeEndElement();
|
|
721
|
+
}
|
|
636
722
|
}
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
723
|
+
function writeDataset(writer, dataset) {
|
|
724
|
+
writer.writeStartElement("Dataset", { attributes: { id: dataset.id } });
|
|
725
|
+
if (dataset.constColumnSize() > 0 || dataset.columnSize() > 0) {
|
|
726
|
+
writer.writeStartElement("ColumnInfo");
|
|
727
|
+
for (const constCol of dataset.iterConstColumns()) writer.writeStartElement("ConstColumn", {
|
|
728
|
+
attributes: {
|
|
729
|
+
id: constCol.id,
|
|
730
|
+
size: String(constCol.size),
|
|
731
|
+
type: constCol.type,
|
|
732
|
+
value: constCol.value !== void 0 ? String(constCol.value) : ""
|
|
733
|
+
},
|
|
734
|
+
selfClosing: true
|
|
735
|
+
});
|
|
736
|
+
for (const col of dataset.iterColumns()) writer.writeStartElement("Column", {
|
|
737
|
+
attributes: {
|
|
738
|
+
id: col.id,
|
|
739
|
+
size: String(col.size),
|
|
740
|
+
type: col.type
|
|
741
|
+
},
|
|
742
|
+
selfClosing: true
|
|
743
|
+
});
|
|
744
|
+
writer.writeEndElement();
|
|
745
|
+
}
|
|
746
|
+
writer.writeStartElement("Rows");
|
|
747
|
+
for (const row of dataset.iterRows()) {
|
|
748
|
+
if (row.type) writer.writeStartElement("Row", { attributes: { type: row.type } });
|
|
749
|
+
else writer.writeStartElement("Row");
|
|
750
|
+
for (const col of row.cols) writeColumn(writer, dataset, col);
|
|
751
|
+
if (row.orgRow && row.orgRow.length > 0) {
|
|
752
|
+
writer.writeStartElement("OrgRow");
|
|
753
|
+
for (const orgCol of row.orgRow) writeColumn(writer, dataset, orgCol);
|
|
754
|
+
writer.writeEndElement();
|
|
755
|
+
}
|
|
756
|
+
writer.writeEndElement();
|
|
757
|
+
}
|
|
758
|
+
writer.writeEndElement();
|
|
759
|
+
writer.writeEndElement();
|
|
660
760
|
}
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
value: constCol.value !== void 0 ? String(constCol.value) : ""
|
|
672
|
-
},
|
|
673
|
-
selfClosing: true
|
|
674
|
-
});
|
|
675
|
-
}
|
|
676
|
-
for (const col of dataset.iterColumns()) {
|
|
677
|
-
await writer.writeStartElement("Column", {
|
|
678
|
-
attributes: {
|
|
679
|
-
id: col.id,
|
|
680
|
-
size: String(col.size),
|
|
681
|
-
type: col.type
|
|
682
|
-
},
|
|
683
|
-
selfClosing: true
|
|
684
|
-
});
|
|
685
|
-
}
|
|
686
|
-
await writer.writeEndElement();
|
|
687
|
-
}
|
|
688
|
-
await writer.writeStartElement("Rows");
|
|
689
|
-
for (const row of dataset.iterRows()) {
|
|
690
|
-
if (row.type) {
|
|
691
|
-
await writer.writeStartElement("Row", { attributes: { type: row.type } });
|
|
692
|
-
} else {
|
|
693
|
-
await writer.writeStartElement("Row");
|
|
694
|
-
}
|
|
695
|
-
for (const col of row.cols) {
|
|
696
|
-
await writeColumn(writer, dataset, col);
|
|
697
|
-
}
|
|
698
|
-
if (row.orgRow && row.orgRow.length > 0) {
|
|
699
|
-
await writer.writeStartElement("OrgRow");
|
|
700
|
-
for (const orgCol of row.orgRow) {
|
|
701
|
-
await writeColumn(writer, dataset, orgCol);
|
|
702
|
-
}
|
|
703
|
-
await writer.writeEndElement();
|
|
704
|
-
}
|
|
705
|
-
await writer.writeEndElement();
|
|
706
|
-
}
|
|
707
|
-
await writer.writeEndElement();
|
|
708
|
-
await writer.writeEndElement();
|
|
761
|
+
function writeColumn(writer, dataset, col) {
|
|
762
|
+
if (col.value !== void 0 && col.value !== null) {
|
|
763
|
+
const colInfo = dataset.getColumnInfo(col.id);
|
|
764
|
+
writer.writeStartElement("Col", { attributes: { id: col.id } });
|
|
765
|
+
writer.writeCharacters(convertToString(col.value, colInfo.type));
|
|
766
|
+
writer.writeEndElement();
|
|
767
|
+
} else writer.writeStartElement("Col", {
|
|
768
|
+
attributes: { id: col.id },
|
|
769
|
+
selfClosing: true
|
|
770
|
+
});
|
|
709
771
|
}
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
await writer.writeStartElement("Col", { attributes: { id: col.id } });
|
|
714
|
-
await writer.writeCharacters(convertToString(col.value, colInfo.type));
|
|
715
|
-
await writer.writeEndElement();
|
|
716
|
-
} else {
|
|
717
|
-
await writer.writeStartElement("Col", { attributes: { id: col.id }, selfClosing: true });
|
|
718
|
-
}
|
|
719
|
-
}
|
|
720
|
-
export {
|
|
721
|
-
ColumnTypeError,
|
|
722
|
-
Dataset,
|
|
723
|
-
InvalidXmlError,
|
|
724
|
-
NexaVersion,
|
|
725
|
-
StringWritableStream,
|
|
726
|
-
XapiRoot,
|
|
727
|
-
XplatformVersion,
|
|
728
|
-
_unescapeXml,
|
|
729
|
-
arrayBufferToString,
|
|
730
|
-
base64ToUint8Array,
|
|
731
|
-
columnType,
|
|
732
|
-
convertToColumnType,
|
|
733
|
-
convertToString,
|
|
734
|
-
dateToString,
|
|
735
|
-
initXapi,
|
|
736
|
-
makeParseEntities,
|
|
737
|
-
makeWriterEntities,
|
|
738
|
-
parse2 as parse,
|
|
739
|
-
rowType,
|
|
740
|
-
stringToDate,
|
|
741
|
-
stringToReadableStream,
|
|
742
|
-
uint8ArrayToBase64,
|
|
743
|
-
write,
|
|
744
|
-
writeString
|
|
745
|
-
};
|
|
772
|
+
|
|
773
|
+
//#endregion
|
|
774
|
+
export { ColumnTypeError, Dataset, InvalidXmlError, NexaVersion, StringWritableStream, XapiRoot, XplatformVersion, _unescapeXml, arrayBufferToString, base64ToUint8Array, columnType, convertToColumnType, convertToString, dateToString, initXapi, isXapiRoot, makeParseEntities, makeWriterEntities, parse, rowType, stringToDate, stringToReadableStream, uint8ArrayToBase64, write };
|