@oino-ts/common 0.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.
@@ -0,0 +1,466 @@
1
+ "use strict";
2
+ /*
3
+ * This Source Code Form is subject to the terms of the Mozilla Public
4
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
5
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.OINOParser = void 0;
9
+ const index_js_1 = require("../../db/src/index.js");
10
+ /**
11
+ * Static factory class for easily creating things based on data
12
+ *
13
+ */
14
+ class OINOParser {
15
+ /**
16
+ * Create data rows from request body based on the datamodel.
17
+ *
18
+ * @param datamodel datamodel of the api
19
+ * @param data data as a string
20
+ * @param requestParams parameters
21
+ *
22
+ */
23
+ static createRows(datamodel, data, requestParams) {
24
+ let result = [];
25
+ if (typeof data == "string") {
26
+ result = this.createRowsFromText(datamodel, data, requestParams);
27
+ }
28
+ else if (data instanceof Buffer) {
29
+ result = this.createRowsFromBlob(datamodel, data, requestParams);
30
+ }
31
+ else if (typeof data == "object") {
32
+ result = [this.createRowFromObject(datamodel, data)];
33
+ }
34
+ return result;
35
+ }
36
+ /**
37
+ * Create data rows from request body based on the datamodel.
38
+ *
39
+ * @param datamodel datamodel of the api
40
+ * @param data data as a string
41
+ * @param requestParams parameters
42
+ *
43
+ */
44
+ static createRowsFromText(datamodel, data, requestParams) {
45
+ if ((requestParams.requestType == index_js_1.OINOContentType.json) || (requestParams.requestType == undefined)) {
46
+ return this._createRowFromJson(datamodel, data);
47
+ }
48
+ else if (requestParams.requestType == index_js_1.OINOContentType.csv) {
49
+ return this._createRowFromCsv(datamodel, data);
50
+ }
51
+ else if (requestParams.requestType == index_js_1.OINOContentType.formdata) {
52
+ return this._createRowFromFormdata(datamodel, Buffer.from(data, "utf8"), requestParams.multipartBoundary || "");
53
+ }
54
+ else if (requestParams.requestType == index_js_1.OINOContentType.urlencode) {
55
+ return this._createRowFromUrlencoded(datamodel, data);
56
+ }
57
+ else if (requestParams.requestType == index_js_1.OINOContentType.html) {
58
+ index_js_1.OINOLog.error("HTML can't be used as an input content type!", { contentType: index_js_1.OINOContentType.html });
59
+ return [];
60
+ }
61
+ else {
62
+ index_js_1.OINOLog.error("Unrecognized input content type!", { contentType: requestParams.requestType });
63
+ return [];
64
+ }
65
+ }
66
+ /**
67
+ * Create data rows from request body based on the datamodel.
68
+ *
69
+ * @param datamodel datamodel of the api
70
+ * @param data data as an Buffer
71
+ * @param requestParams parameters
72
+ *
73
+ */
74
+ static createRowsFromBlob(datamodel, data, requestParams) {
75
+ if ((requestParams.requestType == index_js_1.OINOContentType.json) || (requestParams.requestType == undefined)) {
76
+ return this._createRowFromJson(datamodel, data.toString()); // JSON is always a string
77
+ }
78
+ else if (requestParams.requestType == index_js_1.OINOContentType.csv) {
79
+ return this._createRowFromCsv(datamodel, data.toString()); // binary data has to be base64 encoded so it's a string
80
+ }
81
+ else if (requestParams.requestType == index_js_1.OINOContentType.formdata) {
82
+ return this._createRowFromFormdata(datamodel, data, requestParams.multipartBoundary || "");
83
+ }
84
+ else if (requestParams.requestType == index_js_1.OINOContentType.urlencode) {
85
+ return this._createRowFromUrlencoded(datamodel, data.toString()); // data is urlencoded so it's a string
86
+ }
87
+ else if (requestParams.requestType == index_js_1.OINOContentType.html) {
88
+ index_js_1.OINOLog.error("HTML can't be used as an input content type!", { contentType: index_js_1.OINOContentType.html });
89
+ return [];
90
+ }
91
+ else {
92
+ index_js_1.OINOLog.error("Unrecognized input content type!", { contentType: requestParams.requestType });
93
+ return [];
94
+ }
95
+ }
96
+ /**
97
+ * Create one data row from javascript object based on the datamodel.
98
+ * NOTE! Data assumed to be unserialized i.e. of the native type (string, number, boolean, Buffer)
99
+ *
100
+ * @param datamodel datamodel of the api
101
+ * @param data data as javascript object
102
+ *
103
+ */
104
+ static createRowFromObject(datamodel, data) {
105
+ const fields = datamodel.fields;
106
+ let result = new Array(fields.length);
107
+ for (let i = 0; i < fields.length; i++) {
108
+ result[i] = data[fields[i].name];
109
+ }
110
+ return result;
111
+ }
112
+ static _findCsvLineEnd(csvData, start) {
113
+ const n = csvData.length;
114
+ if (start >= n) {
115
+ return start;
116
+ }
117
+ let end = start;
118
+ let quote_open = false;
119
+ while (end < n) {
120
+ if (csvData[end] == "\"") {
121
+ if (!quote_open) {
122
+ quote_open = true;
123
+ }
124
+ else if ((end < n - 1) && (csvData[end + 1] == "\"")) {
125
+ end++;
126
+ }
127
+ else {
128
+ quote_open = false;
129
+ }
130
+ }
131
+ else if ((!quote_open) && (csvData[end] == "\r")) {
132
+ return end;
133
+ }
134
+ end++;
135
+ }
136
+ return n;
137
+ }
138
+ static _parseCsvLine(csvLine) {
139
+ let result = [];
140
+ const n = csvLine.length;
141
+ let start = 0;
142
+ let end = 0;
143
+ let quote_open = false;
144
+ let has_quotes = false;
145
+ let has_escaped_quotes = false;
146
+ let found_field = false;
147
+ while (end < n) {
148
+ if (csvLine[end] == "\"") {
149
+ if (!quote_open) {
150
+ quote_open = true;
151
+ }
152
+ else if ((end < n - 1) && (csvLine[end + 1] == "\"")) {
153
+ end++;
154
+ has_escaped_quotes = true;
155
+ }
156
+ else {
157
+ has_quotes = true;
158
+ quote_open = false;
159
+ }
160
+ }
161
+ if ((!quote_open) && ((end == n - 1) || (csvLine[end] == ","))) {
162
+ found_field = true;
163
+ if (end == n - 1) {
164
+ end++;
165
+ }
166
+ }
167
+ if (found_field) {
168
+ // console.log("OINODB_csvParseLine: next field=" + csvLine.substring(start,end) + ", start="+start+", end="+end)
169
+ let field_str;
170
+ if (has_quotes) {
171
+ field_str = csvLine.substring(start + 1, end - 1);
172
+ }
173
+ else if (start == end) {
174
+ field_str = undefined;
175
+ }
176
+ else {
177
+ field_str = csvLine.substring(start, end);
178
+ if (field_str == "null") {
179
+ field_str = null;
180
+ }
181
+ }
182
+ result.push(field_str);
183
+ has_quotes = false;
184
+ has_escaped_quotes = true;
185
+ found_field = false;
186
+ start = end + 1;
187
+ }
188
+ end++;
189
+ }
190
+ return result;
191
+ }
192
+ static _createRowFromCsv(datamodel, data) {
193
+ let result = [];
194
+ const n = data.length;
195
+ let start = 0;
196
+ let end = this._findCsvLineEnd(data, start);
197
+ const header_str = data.substring(start, end);
198
+ const headers = this._parseCsvLine(header_str);
199
+ let field_to_header_mapping = new Array(datamodel.fields.length);
200
+ let headers_found = false;
201
+ for (let i = 0; i < field_to_header_mapping.length; i++) {
202
+ field_to_header_mapping[i] = headers.indexOf(datamodel.fields[i].name);
203
+ headers_found = headers_found || (field_to_header_mapping[i] >= 0);
204
+ }
205
+ // OINOLog.debug("createRowFromCsv", {headers:headers, field_to_header_mapping:field_to_header_mapping})
206
+ if (!headers_found) {
207
+ return result;
208
+ }
209
+ start = end + 1;
210
+ end = start;
211
+ while (end < n) {
212
+ while ((start < n) && ((data[start] == "\r") || (data[start] == "\n"))) {
213
+ start++;
214
+ }
215
+ if (start >= n) {
216
+ return result;
217
+ }
218
+ end = this._findCsvLineEnd(data, start);
219
+ const row_data = this._parseCsvLine(data.substring(start, end));
220
+ const row = new Array(field_to_header_mapping.length);
221
+ let has_data = false;
222
+ for (let i = 0; i < datamodel.fields.length; i++) {
223
+ const field = datamodel.fields[i];
224
+ let j = field_to_header_mapping[i];
225
+ let value = row_data[j];
226
+ if ((value === undefined) || (value === null)) { // null/undefined-decoding built into the parser
227
+ row[i] = value;
228
+ }
229
+ else if ((j >= 0) && (j < row_data.length)) {
230
+ value = index_js_1.OINOStr.decode(value, index_js_1.OINOContentType.csv);
231
+ if (value && (field.fieldParams.isPrimaryKey || field.fieldParams.isForeignKey) && (field instanceof index_js_1.OINONumberDataField) && (datamodel.api.hashid)) {
232
+ value = datamodel.api.hashid.decode(value);
233
+ }
234
+ row[i] = field.deserializeCell(value);
235
+ }
236
+ else {
237
+ row[i] = undefined;
238
+ }
239
+ has_data = has_data || (row[i] !== undefined);
240
+ }
241
+ // console.log("createRowFromCsv: next row=" + row)
242
+ if (has_data) {
243
+ result.push(row);
244
+ }
245
+ else {
246
+ index_js_1.OINOLog.warning("createRowFromCsv: empty row skipped");
247
+ }
248
+ start = end;
249
+ end = start;
250
+ }
251
+ return result;
252
+ }
253
+ static _createRowFromJsonObj(obj, datamodel) {
254
+ // console.log("createRowFromJsonObj: obj=" + JSON.stringify(obj))
255
+ const fields = datamodel.fields;
256
+ let result = new Array(fields.length);
257
+ let has_data = false;
258
+ // console.log("createRowFromJsonObj: " + result)
259
+ for (let i = 0; i < fields.length; i++) {
260
+ const field = fields[i];
261
+ let value = obj[field.name];
262
+ // console.log("createRowFromJsonObj: key=" + field.name + ", val=" + val)
263
+ if ((value === null) || (value === undefined)) { // must be checed first as null is an object
264
+ result[i] = value;
265
+ }
266
+ else if (Array.isArray(value) || typeof value === "object") {
267
+ result[i] = JSON.stringify(value).replaceAll("\"", "\\\""); // only single level deep objects, rest is handled as JSON-strings
268
+ }
269
+ else if (typeof value === "string") {
270
+ value = index_js_1.OINOStr.decode(value, index_js_1.OINOContentType.json);
271
+ if (value && (field.fieldParams.isPrimaryKey || field.fieldParams.isForeignKey) && (field instanceof index_js_1.OINONumberDataField) && (datamodel.api.hashid)) {
272
+ value = datamodel.api.hashid.decode(value);
273
+ }
274
+ result[i] = field.deserializeCell(value);
275
+ }
276
+ else {
277
+ result[i] = value; // value types are passed as-is
278
+ }
279
+ has_data = has_data || (result[i] !== undefined);
280
+ // console.log("createRowFromJsonObj: result["+i+"]=" + result[i])
281
+ }
282
+ // console.log("createRowFromJsonObj: " + result)
283
+ if (has_data) {
284
+ return result;
285
+ }
286
+ else {
287
+ index_js_1.OINOLog.warning("createRowFromJsonObj: empty row skipped");
288
+ return undefined;
289
+ }
290
+ }
291
+ static _createRowFromJson(datamodel, data) {
292
+ let result = [];
293
+ // console.log("OINORowFactoryJson: data=" + data)
294
+ const obj = JSON.parse(data);
295
+ if (Array.isArray(obj)) {
296
+ obj.forEach(row => {
297
+ const data_row = this._createRowFromJsonObj(row, datamodel);
298
+ if (data_row !== undefined) {
299
+ result.push(data_row);
300
+ }
301
+ });
302
+ }
303
+ else {
304
+ const data_row = this._createRowFromJsonObj(obj, datamodel);
305
+ if (data_row !== undefined) {
306
+ result.push(data_row);
307
+ }
308
+ }
309
+ return result;
310
+ }
311
+ static _findMultipartBoundary(formData, multipartBoundary, start) {
312
+ let n = formData.indexOf(multipartBoundary, start);
313
+ if (n >= 0) {
314
+ n += multipartBoundary.length + 2;
315
+ }
316
+ else {
317
+ n = formData.length;
318
+ }
319
+ return n;
320
+ }
321
+ static _parseMultipartLine(data, start) {
322
+ let line_end = data.indexOf('\r\n', start);
323
+ if (line_end >= start) {
324
+ return data.subarray(start, line_end).toString();
325
+ }
326
+ else {
327
+ return '';
328
+ }
329
+ }
330
+ static _multipartHeaderRegex = /Content-Disposition\: (form-data|file); name=\"([^\"]+)\"(; filename=.*)?/i;
331
+ static _createRowFromFormdata(datamodel, data, multipartBoundary) {
332
+ let result = [];
333
+ try {
334
+ const n = data.length;
335
+ let start = this._findMultipartBoundary(data, multipartBoundary, 0);
336
+ let end = this._findMultipartBoundary(data, multipartBoundary, start);
337
+ // OINOLog.debug("createRowFromFormdata: enter", {start:start, end:end, multipartBoundary:multipartBoundary})
338
+ const row = new Array(datamodel.fields.length);
339
+ let has_data = false;
340
+ while (end < n) {
341
+ // OINOLog.debug("createRowFromFormdata: next block", {start:start, end:end, block:data.substring(start, end)})
342
+ let block_ok = true;
343
+ let l = this._parseMultipartLine(data, start);
344
+ // OINOLog.debug("createRowFromFormdata: next line", {start:start, end:end, line:l})
345
+ start += l.length + 2;
346
+ const header_matches = OINOParser._multipartHeaderRegex.exec(l);
347
+ if (!header_matches) {
348
+ index_js_1.OINOLog.warning("OINODbFactory.createRowFromFormdata: unsupported block skipped!", { header_line: l });
349
+ block_ok = false;
350
+ }
351
+ else {
352
+ const field_name = header_matches[2];
353
+ const is_file = header_matches[3] != null;
354
+ let is_base64 = false;
355
+ const field_index = datamodel.findFieldIndexByName(field_name);
356
+ // OINOLog.debug("createRowFromFormdata: header", {field_name:field_name, field_index:field_index, is_file:is_file, is_base64:is_base64})
357
+ if (field_index < 0) {
358
+ index_js_1.OINOLog.warning("OINODbFactory.createRowFromFormdata: form field not found and skipped!", { field_name: field_name });
359
+ block_ok = false;
360
+ }
361
+ else {
362
+ const field = datamodel.fields[field_index];
363
+ l = this._parseMultipartLine(data, start);
364
+ // OINOLog.debug("createRowFromFormdata: next line", {start:start, end:end, line:l})
365
+ while (block_ok && (l != '')) {
366
+ if (l.startsWith('Content-Type:') && (l.indexOf('multipart/mixed') >= 0)) {
367
+ index_js_1.OINOLog.warning("OINODbFactory.createRowFromFormdata: mixed multipart files not supported and skipped!", { header_line: l });
368
+ block_ok = false;
369
+ }
370
+ else if (l.startsWith('Content-Transfer-Encoding:') && (l.indexOf('BASE64') >= 0)) {
371
+ is_base64 = true;
372
+ }
373
+ start += l.length + 2;
374
+ l = this._parseMultipartLine(data, start);
375
+ // OINOLog.debug("createRowFromFormdata: next line", {start:start, end:end, line:l})
376
+ }
377
+ start += 2;
378
+ if (!block_ok) {
379
+ index_js_1.OINOLog.warning("OINODbFactory.createRowFromFormdata: invalid block skipped", { field_name: field_name });
380
+ }
381
+ else if (start + multipartBoundary.length + 2 >= end) {
382
+ // OINOLog.debug("OINODbFactory.createRowFromFormdata: null value", {field_name:field_name})
383
+ row[field_index] = null;
384
+ }
385
+ else if (is_file) {
386
+ if (is_base64) {
387
+ const value = this._parseMultipartLine(data, start).trim();
388
+ row[field_index] = field.deserializeCell(index_js_1.OINOStr.decode(value, index_js_1.OINOContentType.formdata));
389
+ }
390
+ else {
391
+ const e = this._findMultipartBoundary(data, multipartBoundary, start);
392
+ const value = data.subarray(start, e - 2);
393
+ row[field_index] = value;
394
+ }
395
+ // console.log("OINODbFactory.createRowFromFormdata: file field", {field_name:field_name, value:row[field_index]})
396
+ }
397
+ else {
398
+ let value = index_js_1.OINOStr.decode(this._parseMultipartLine(data, start).trim(), index_js_1.OINOContentType.formdata);
399
+ // OINOLog.debug("OINODbFactory.createRowFromFormdata: parse form field", {field_name:field_name, value:value})
400
+ if (value && (field.fieldParams.isPrimaryKey || field.fieldParams.isForeignKey) && (field instanceof index_js_1.OINONumberDataField) && (datamodel.api.hashid)) {
401
+ value = datamodel.api.hashid.decode(value);
402
+ }
403
+ row[field_index] = field.deserializeCell(value);
404
+ }
405
+ has_data = has_data || (row[field_index] !== undefined);
406
+ }
407
+ }
408
+ start = end;
409
+ end = this._findMultipartBoundary(data, multipartBoundary, start);
410
+ }
411
+ // OINOLog.debug("createRowFromFormdata: next row", {row:row})
412
+ if (has_data) {
413
+ result.push(row);
414
+ }
415
+ else {
416
+ index_js_1.OINOLog.warning("createRowFromFormdata: empty row skipped");
417
+ }
418
+ }
419
+ catch (e) {
420
+ index_js_1.OINOLog.error("createRowFromFormdata: error parsing formdata", { exception: e.message });
421
+ }
422
+ return result;
423
+ }
424
+ static _createRowFromUrlencoded(datamodel, data) {
425
+ // OINOLog.debug("createRowFromUrlencoded: enter", {data:data})
426
+ let result = [];
427
+ const row = new Array(datamodel.fields.length);
428
+ let has_data = false;
429
+ const data_parts = data.trim().split('&');
430
+ try {
431
+ for (let i = 0; i < data_parts.length; i++) {
432
+ const param_parts = data_parts[i].split('=');
433
+ // OINOLog.debug("createRowFromUrlencoded: next param", {param_parts:param_parts})
434
+ if (param_parts.length == 2) {
435
+ const key = index_js_1.OINOStr.decodeUrlencode(param_parts[0]) || "";
436
+ const field_index = datamodel.findFieldIndexByName(key);
437
+ if (field_index < 0) {
438
+ index_js_1.OINOLog.info("createRowFromUrlencoded: param field not found", { field: key });
439
+ }
440
+ else {
441
+ const field = datamodel.fields[field_index];
442
+ let value = index_js_1.OINOStr.decode(param_parts[1], index_js_1.OINOContentType.urlencode);
443
+ if (value && (field.fieldParams.isPrimaryKey || field.fieldParams.isForeignKey) && (field instanceof index_js_1.OINONumberDataField) && (datamodel.api.hashid)) {
444
+ value = datamodel.api.hashid.decode(value);
445
+ }
446
+ row[field_index] = field.deserializeCell(value);
447
+ has_data = has_data || (row[field_index] !== undefined);
448
+ }
449
+ }
450
+ // const value = requestParams[]
451
+ }
452
+ if (has_data) {
453
+ result.push(row);
454
+ }
455
+ else {
456
+ index_js_1.OINOLog.warning("createRowFromUrlencoded: empty row skipped");
457
+ }
458
+ }
459
+ catch (e) {
460
+ index_js_1.OINOLog.error("createRowFromUrlencoded: error parsing urlencoded data", { exception: e.message });
461
+ }
462
+ // console.log("createRowFromUrlencoded: next row=" + row)
463
+ return result;
464
+ }
465
+ }
466
+ exports.OINOParser = OINOParser;
@@ -0,0 +1,216 @@
1
+ "use strict";
2
+ /*
3
+ * This Source Code Form is subject to the terms of the Mozilla Public
4
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
5
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.OINOHttpResult = exports.OINOResult = void 0;
9
+ const node_crypto_1 = require("node:crypto");
10
+ const _1 = require(".");
11
+ /**
12
+ * OINO API request result object with returned data and/or http status code/message and
13
+ * error / warning messages.
14
+ *
15
+ */
16
+ class OINOResult {
17
+ /** Wheter request was successfully executed */
18
+ success;
19
+ /** HTTP status code */
20
+ statusCode;
21
+ /** HTTP status message */
22
+ statusMessage;
23
+ /** Error / warning messages */
24
+ messages;
25
+ /**
26
+ * Constructor of OINOResult.
27
+ *
28
+ */
29
+ constructor() {
30
+ this.success = true;
31
+ this.statusCode = 200;
32
+ this.statusMessage = "OK";
33
+ this.messages = [];
34
+ }
35
+ /**
36
+ * Copy values from different result.
37
+ *
38
+ * @param result source value
39
+ */
40
+ copy(result) {
41
+ this.success = result.success;
42
+ this.statusCode = result.statusCode;
43
+ this.statusMessage = result.statusMessage;
44
+ this.messages = result.messages.slice();
45
+ }
46
+ /**
47
+ * Set HTTP OK status (does not reset messages).
48
+ *
49
+ */
50
+ setOk() {
51
+ this.success = true;
52
+ this.statusCode = 200;
53
+ this.statusMessage = "OK";
54
+ }
55
+ /**
56
+ * Set HTTP error status using given code and message. Returns self reference for chaining.
57
+ *
58
+ * @param statusCode HTTP status code
59
+ * @param statusMessage HTTP status message
60
+ * @param operation operation where error occured
61
+ *
62
+ */
63
+ setError(statusCode, statusMessage, operation) {
64
+ this.success = false;
65
+ this.statusCode = statusCode;
66
+ if (this.statusMessage != "OK") {
67
+ this.messages.push(this.statusMessage); // latest error becomes status, but if there was something non-trivial, add it to the messages
68
+ }
69
+ if (statusMessage.startsWith(_1.OINO_ERROR_PREFIX)) {
70
+ this.statusMessage = statusMessage;
71
+ }
72
+ else {
73
+ this.statusMessage = _1.OINO_ERROR_PREFIX + " (" + operation + "): " + statusMessage;
74
+ }
75
+ return this;
76
+ }
77
+ /**
78
+ * Add warning message. Returns self reference for chaining.
79
+ *
80
+ * @param message HTTP status message
81
+ * @param operation operation where warning occured
82
+ *
83
+ */
84
+ addWarning(message, operation) {
85
+ message = message.trim();
86
+ if (message) {
87
+ this.messages.push(_1.OINO_WARNING_PREFIX + " (" + operation + "): " + message);
88
+ }
89
+ return this;
90
+ }
91
+ /**
92
+ * Add info message. Returns self reference for chaining.
93
+ *
94
+ * @param message HTTP status message
95
+ * @param operation operation where info occured
96
+ *
97
+ */
98
+ addInfo(message, operation) {
99
+ message = message.trim();
100
+ if (message) {
101
+ this.messages.push(_1.OINO_INFO_PREFIX + " (" + operation + "): " + message);
102
+ }
103
+ return this;
104
+ }
105
+ /**
106
+ * Add debug message. Returns self reference for chaining.
107
+ *
108
+ * @param message HTTP status message
109
+ * @param operation operation where debug occured
110
+ *
111
+ */
112
+ addDebug(message, operation) {
113
+ message = message.trim();
114
+ if (message) {
115
+ this.messages.push(_1.OINO_DEBUG_PREFIX + " (" + operation + "): " + message);
116
+ }
117
+ return this;
118
+ }
119
+ /**
120
+ * Copy given messages to HTTP headers.
121
+ *
122
+ * @param headers HTTP headers
123
+ * @param copyErrors wether error messages should be copied (default true)
124
+ * @param copyWarnings wether warning messages should be copied (default false)
125
+ * @param copyInfos wether info messages should be copied (default false)
126
+ * @param copyDebug wether debug messages should be copied (default false)
127
+ *
128
+ */
129
+ copyMessagesToHeaders(headers, copyErrors = true, copyWarnings = false, copyInfos = false, copyDebug = false) {
130
+ let j = 1;
131
+ for (let i = 0; i < this.messages.length; i++) {
132
+ const message = this.messages[i].replaceAll("\r", " ").replaceAll("\n", " ");
133
+ if (copyErrors && message.startsWith(_1.OINO_ERROR_PREFIX)) {
134
+ headers.append('X-OINO-MESSAGE-' + j, message);
135
+ j++;
136
+ }
137
+ if (copyWarnings && message.startsWith(_1.OINO_WARNING_PREFIX)) {
138
+ headers.append('X-OINO-MESSAGE-' + j, message);
139
+ j++;
140
+ }
141
+ if (copyInfos && message.startsWith(_1.OINO_INFO_PREFIX)) {
142
+ headers.append('X-OINO-MESSAGE-' + j, message);
143
+ j++;
144
+ }
145
+ if (copyDebug && message.startsWith(_1.OINO_DEBUG_PREFIX)) {
146
+ headers.append('X-OINO-MESSAGE-' + j, message);
147
+ j++;
148
+ }
149
+ }
150
+ }
151
+ /**
152
+ * Print result for logging.
153
+ *
154
+ */
155
+ printLog() {
156
+ return "OINOResult: statusCode=" + this.statusCode + ", statusMessage=" + this.statusMessage + ", messages=[" + this.messages.join(", ") + "]";
157
+ }
158
+ }
159
+ exports.OINOResult = OINOResult;
160
+ /**
161
+ * Specialized result for HTTP responses.
162
+ */
163
+ class OINOHttpResult extends OINOResult {
164
+ _etag;
165
+ /** HTTP body data */
166
+ body;
167
+ /** HTTP cache expiration value */
168
+ expires;
169
+ /** HTTP cache last-modified value */
170
+ lastModified;
171
+ /**
172
+ * Constructor for a `OINOHttpResult`
173
+ *
174
+ * @param body HTTP body
175
+ *
176
+ */
177
+ constructor(body) {
178
+ super();
179
+ this.body = body;
180
+ this.expires = 0;
181
+ this.lastModified = 0;
182
+ this._etag = "";
183
+ }
184
+ /**
185
+ * Get the ETag value for the body opportunistically, i.e. don't calculate until requested and reuse value.
186
+ *
187
+ */
188
+ getEtag() {
189
+ if (this._etag == "") {
190
+ const hash = (0, node_crypto_1.createHash)("sha256");
191
+ this._etag = hash.update(this.body).digest("hex");
192
+ }
193
+ return this._etag;
194
+ }
195
+ /**
196
+ * Get a Response object from the result values.
197
+ *
198
+ * @param headers HTTP headers (overrides existing values)
199
+ */
200
+ getResponse(headers) {
201
+ const result = new Response(this.body, { status: this.statusCode, statusText: this.statusMessage, headers: headers });
202
+ result.headers.set('Content-Length', this.body.length.toString());
203
+ if (this.lastModified > 0) {
204
+ result.headers.set('Last-Modified', new Date(this.lastModified).toUTCString());
205
+ }
206
+ if (this.expires >= 0) {
207
+ result.headers.set('Expires', Math.round(this.expires).toString());
208
+ if (this.expires == 0) {
209
+ result.headers.set('Pragma', 'no-cache');
210
+ }
211
+ }
212
+ result.headers.set("ETag", this.getEtag());
213
+ return result;
214
+ }
215
+ }
216
+ exports.OINOHttpResult = OINOHttpResult;