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