@sap/async-xsjs 1.0.2

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.
Files changed (178) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/LICENSE +37 -0
  3. package/README.md +445 -0
  4. package/differences.md +162 -0
  5. package/docs/$.Application.html +262 -0
  6. package/docs/$.Session.html +674 -0
  7. package/docs/$.db.CallableStatement.html +2524 -0
  8. package/docs/$.db.Connection.html +511 -0
  9. package/docs/$.db.ParameterMetaData.html +805 -0
  10. package/docs/$.db.PreparedStatement.html +1796 -0
  11. package/docs/$.db.ResultSet.html +1308 -0
  12. package/docs/$.db.ResultSetMetaData.html +800 -0
  13. package/docs/$.db.SQLException.html +259 -0
  14. package/docs/$.db.html +773 -0
  15. package/docs/$.hdb.ColumnMetadata.html +438 -0
  16. package/docs/$.hdb.Connection.html +663 -0
  17. package/docs/$.hdb.ProcedureResult.html +280 -0
  18. package/docs/$.hdb.ResultSet.html +324 -0
  19. package/docs/$.hdb.ResultSetIterator.html +315 -0
  20. package/docs/$.hdb.ResultSetMetaData.html +259 -0
  21. package/docs/$.hdb.SQLException.html +259 -0
  22. package/docs/$.hdb.html +557 -0
  23. package/docs/$.html +471 -0
  24. package/docs/$.jobs.Job.html +783 -0
  25. package/docs/$.jobs.JobLog.html +380 -0
  26. package/docs/$.jobs.JobSchedules.html +852 -0
  27. package/docs/$.jobs.html +238 -0
  28. package/docs/$.net.Destination.html +304 -0
  29. package/docs/$.net.Mail.Part.html +510 -0
  30. package/docs/$.net.Mail.html +504 -0
  31. package/docs/$.net.SMTPConnection.html +347 -0
  32. package/docs/$.net.html +749 -0
  33. package/docs/$.net.http.Client.html +562 -0
  34. package/docs/$.net.http.Destination.html +237 -0
  35. package/docs/$.net.http.Request.html +567 -0
  36. package/docs/$.net.http.html +292 -0
  37. package/docs/$.security.AntiVirus.html +361 -0
  38. package/docs/$.security.Store.html +636 -0
  39. package/docs/$.security.crypto.html +414 -0
  40. package/docs/$.security.html +247 -0
  41. package/docs/$.security.x509.html +373 -0
  42. package/docs/$.text.analysis.Session.html +983 -0
  43. package/docs/$.text.analysis.html +242 -0
  44. package/docs/$.text.html +246 -0
  45. package/docs/$.text.mining.Session.html +2018 -0
  46. package/docs/$.text.mining.html +242 -0
  47. package/docs/$.trace.html +525 -0
  48. package/docs/$.util.SAXParser.html +955 -0
  49. package/docs/$.util.Zip.html +474 -0
  50. package/docs/$.util.codec.html +414 -0
  51. package/docs/$.util.compression.html +357 -0
  52. package/docs/$.util.html +325 -0
  53. package/docs/$.util.sql.html +290 -0
  54. package/docs/$.web.Body.html +333 -0
  55. package/docs/$.web.EntityList.html +296 -0
  56. package/docs/$.web.TupelList.html +496 -0
  57. package/docs/$.web.WebEntityRequest.html +393 -0
  58. package/docs/$.web.WebEntityResponse.html +392 -0
  59. package/docs/$.web.WebRequest.html +560 -0
  60. package/docs/$.web.WebResponse.html +609 -0
  61. package/docs/$.web.html +246 -0
  62. package/docs/Copyright-SAP.html +39 -0
  63. package/docs/Disclaimer-SAP.html +55 -0
  64. package/docs/index.html +232 -0
  65. package/docs/styles/jsdoc-default.css +382 -0
  66. package/lib/AppConfig.js +36 -0
  67. package/lib/AuditLogger.js +41 -0
  68. package/lib/cacert.js +26 -0
  69. package/lib/ctypes.js +153 -0
  70. package/lib/destinations/dest-provider.js +57 -0
  71. package/lib/index.js +235 -0
  72. package/lib/jobs/Action.js +40 -0
  73. package/lib/jobs/Job.js +100 -0
  74. package/lib/jobs/JobManager.js +150 -0
  75. package/lib/jobs/JobsRuntime.js +133 -0
  76. package/lib/jobs/SqlScriptJobRunner.js +36 -0
  77. package/lib/jobs/XsjsJobRunner.js +78 -0
  78. package/lib/jobs/index.js +11 -0
  79. package/lib/logging.js +16 -0
  80. package/lib/middleware.js +125 -0
  81. package/lib/odata/ODataService.js +125 -0
  82. package/lib/odata/index.js +7 -0
  83. package/lib/odata/service-factory.js +26 -0
  84. package/lib/passport-noauth.js +17 -0
  85. package/lib/routes.js +115 -0
  86. package/lib/runtime.js +740 -0
  87. package/lib/sandbox.js +40 -0
  88. package/lib/utils/XsJsFunctionRunner.js +57 -0
  89. package/lib/utils/XsJsLibFunctionRunner.js +57 -0
  90. package/lib/utils/buffer-utils.js +77 -0
  91. package/lib/utils/compression-utils.js +14 -0
  92. package/lib/utils/date-utils.js +104 -0
  93. package/lib/utils/errors/HttpError.js +20 -0
  94. package/lib/utils/errors/wrap-app-error.js +18 -0
  95. package/lib/utils/index.js +17 -0
  96. package/lib/utils/xs-function-runner.js +51 -0
  97. package/lib/utils/xs-types.js +21 -0
  98. package/lib/utils/xspath.js +36 -0
  99. package/lib/utils/xsstack.js +28 -0
  100. package/lib/views/error.html +28 -0
  101. package/lib/xsjs/Application.js +28 -0
  102. package/lib/xsjs/Locale.js +53 -0
  103. package/lib/xsjs/Session.js +31 -0
  104. package/lib/xsjs/constants.js +71 -0
  105. package/lib/xsjs/db/common/DbBase.js +85 -0
  106. package/lib/xsjs/db/common/DbOptions.js +163 -0
  107. package/lib/xsjs/db/common/arguments-validation.js +102 -0
  108. package/lib/xsjs/db/common/connection.js +12 -0
  109. package/lib/xsjs/db/common/enums.js +93 -0
  110. package/lib/xsjs/db/common/execute-batch.js +38 -0
  111. package/lib/xsjs/db/common/parse-time.js +139 -0
  112. package/lib/xsjs/db/dbapi/CallableStatement.js +192 -0
  113. package/lib/xsjs/db/dbapi/Connection.js +78 -0
  114. package/lib/xsjs/db/dbapi/DB.js +39 -0
  115. package/lib/xsjs/db/dbapi/ParameterMetaData.js +118 -0
  116. package/lib/xsjs/db/dbapi/PreparedStatement.js +78 -0
  117. package/lib/xsjs/db/dbapi/ResultSet.js +220 -0
  118. package/lib/xsjs/db/dbapi/ResultSetMetaData.js +116 -0
  119. package/lib/xsjs/db/dbapi/Statement.js +514 -0
  120. package/lib/xsjs/db/dbapi/conversions.js +113 -0
  121. package/lib/xsjs/db/dbapi/fetch-rows.js +32 -0
  122. package/lib/xsjs/db/hdbapi/Connection.js +525 -0
  123. package/lib/xsjs/db/hdbapi/HDB.js +32 -0
  124. package/lib/xsjs/db/hdbapi/ResultSetIterator.js +40 -0
  125. package/lib/xsjs/db/hdbapi/convert.js +77 -0
  126. package/lib/xsjs/db/hdbapi/table-string-parser.js +52 -0
  127. package/lib/xsjs/db/index.js +4 -0
  128. package/lib/xsjs/index.js +13 -0
  129. package/lib/xsjs/jobs/Job.js +228 -0
  130. package/lib/xsjs/jobs/Jobs.js +11 -0
  131. package/lib/xsjs/jobs/Logs.js +127 -0
  132. package/lib/xsjs/jobs/Schedule.js +110 -0
  133. package/lib/xsjs/jobs/Schedules.js +108 -0
  134. package/lib/xsjs/net/Destination.js +43 -0
  135. package/lib/xsjs/net/http/Client.js +220 -0
  136. package/lib/xsjs/net/http/HTTP.js +72 -0
  137. package/lib/xsjs/net/index.js +5 -0
  138. package/lib/xsjs/net/smtp/Mail.js +38 -0
  139. package/lib/xsjs/net/smtp/Part.js +30 -0
  140. package/lib/xsjs/net/smtp/SMTPConnection.js +39 -0
  141. package/lib/xsjs/net/smtp/index.js +18 -0
  142. package/lib/xsjs/net/smtp/nodemailer-util.js +77 -0
  143. package/lib/xsjs/require.js +39 -0
  144. package/lib/xsjs/security/AntiVirus.js +31 -0
  145. package/lib/xsjs/security/Store.js +119 -0
  146. package/lib/xsjs/security/crypto.js +23 -0
  147. package/lib/xsjs/security/index.js +5 -0
  148. package/lib/xsjs/security/x509.js +12 -0
  149. package/lib/xsjs/text/analysis/Session.js +128 -0
  150. package/lib/xsjs/text/index.js +30 -0
  151. package/lib/xsjs/text/mining/Session.js +82 -0
  152. package/lib/xsjs/trace/trace.js +41 -0
  153. package/lib/xsjs/util/SAXParser.js +174 -0
  154. package/lib/xsjs/util/Zip.js +220 -0
  155. package/lib/xsjs/util/codec.js +33 -0
  156. package/lib/xsjs/util/compression.js +24 -0
  157. package/lib/xsjs/util/index.js +22 -0
  158. package/lib/xsjs/web/BasicWebEntity.js +41 -0
  159. package/lib/xsjs/web/EntityList.js +11 -0
  160. package/lib/xsjs/web/TupelLists/CookiesTupelList.js +47 -0
  161. package/lib/xsjs/web/TupelLists/HeadersTupelList.js +55 -0
  162. package/lib/xsjs/web/TupelLists/ParametersTupelList.js +83 -0
  163. package/lib/xsjs/web/TupelLists/TupelListBase.js +45 -0
  164. package/lib/xsjs/web/WebBody.js +135 -0
  165. package/lib/xsjs/web/WebEntityRequest.js +40 -0
  166. package/lib/xsjs/web/WebEntityResponse.js +26 -0
  167. package/lib/xsjs/web/WebRequest.js +209 -0
  168. package/lib/xsjs/web/WebResponse.js +183 -0
  169. package/lib/xsjs/web/index.js +4 -0
  170. package/lib/xsjs/web/utils/HeadersParser.js +53 -0
  171. package/lib/xsjs/web/utils/HttpRequestParser.js +93 -0
  172. package/lib/xsjs/web/utils/MultipartParser.js +163 -0
  173. package/lib/xsjs/web/utils/MultipartResponseBuilder.js +73 -0
  174. package/lib/xsjs/web/utils/SetCookieParser.js +32 -0
  175. package/lib/xsjslib/TextBundleWrapper.js +46 -0
  176. package/lib/xsjslib/index.js +11 -0
  177. package/npm-shrinkwrap.json +11540 -0
  178. package/package.json +84 -0
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+
3
+ var crypto = require('crypto');
4
+ var buffUtils = require('../../utils/buffer-utils');
5
+
6
+ function hashInput(data, key, algorithmType) {
7
+ data = buffUtils.getData(data);
8
+ var algorithm = key ? crypto.createHmac(algorithmType, key) : crypto.createHash(algorithmType);
9
+ algorithm.update(data, 'utf8');
10
+ return buffUtils.toArrayBuffer(algorithm.digest());
11
+ }
12
+
13
+ exports.md5 = function(data, key) {
14
+ return hashInput(data, key, 'md5');
15
+ };
16
+
17
+ exports.sha1 = function(data, key) {
18
+ return hashInput(data, key, 'sha1');
19
+ };
20
+
21
+ exports.sha256 = function(data, key) {
22
+ return hashInput(data, key, 'sha256');
23
+ };
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ exports.crypto = require('./crypto');
4
+ exports.AntiVirus = require('./AntiVirus');
5
+ exports.x509 = require('./x509');
@@ -0,0 +1,12 @@
1
+ 'use strict';
2
+
3
+ var x509 = require('@sap/node-jwt');
4
+ var buffUtils = require('../../utils/buffer-utils');
5
+
6
+ exports.getIssuer = function(data) {
7
+ return new x509('').getIssuer(buffUtils.getData(data));
8
+ };
9
+
10
+ exports.getSubject = function(data) {
11
+ return new x509('').getSubject(buffUtils.getData(data));
12
+ };
@@ -0,0 +1,128 @@
1
+ 'use strict';
2
+
3
+ var ta = require('@sap/textanalysis');
4
+ var bufferUtils = require('../../../utils/buffer-utils');
5
+
6
+ module.exports = Session;
7
+
8
+ function Session($db, params) {
9
+ this.configuration = params.configuration;
10
+ this.client = (async () => { await $db.getConnection()._client; })();
11
+ this.schema = $db._dbReqOptions._globalOptions.schema;
12
+ }
13
+
14
+ Session.prototype.analyze = async function (params) {
15
+ return await analyze(this, params);
16
+ };
17
+
18
+ function analyze(session, params) {
19
+ var values = normalizeInput(params);
20
+ values.CONFIGURATION_SCHEMA_NAME = session.schema;
21
+ values.CONFIGURATION = session.configuration;
22
+ return new Promise ((resolve, reject) => {
23
+
24
+ ta.analyze(values, session.client, function (err, parameters, rows) {
25
+ if (err) {
26
+ return reject(err);
27
+ }
28
+ var tokens = [], entities = [], metadata = [], grammaticalRoles = [];
29
+ if (rows) {
30
+ for (var i = 0; i < rows.length; i++) {
31
+ var row = rows[i];
32
+ if (row.RULE === 'Entity Extraction') {
33
+ var entity = {
34
+ id: row.COUNTER,
35
+ text: row.TOKEN,
36
+ normalizedForm: row.NORMALIZED,
37
+ labelPath: row.TYPE,
38
+ offset: row.OFFSET,
39
+ paragraph: row.PARAGRAPH,
40
+ sentence: row.SENTENCE,
41
+ parent: row.PARENT
42
+ };
43
+ entities.push(entity);
44
+ }
45
+ else if (row.RULE === 'LXP') {
46
+ var token = {
47
+ token: row.TOKEN,
48
+ normalizedToken: row.NORMALIZED,
49
+ partOfSpeech: row.TYPE,
50
+ offset: row.OFFSET,
51
+ paragraph: row.PARAGRAPH,
52
+ sentence: row.SENTENCE
53
+ // TODO: stems
54
+ };
55
+ tokens.push(token);
56
+ }
57
+ else if (row.RULE === 'Grammatical Role') {
58
+ var grammaticalRoleGovernor = {
59
+ id: row.PARENT,
60
+ dependencyType: row.TYPE
61
+ };
62
+ var grammaticalRole = {
63
+ id: row.COUNTER,
64
+ offset: row.OFFSET,
65
+ paragraph: row.PARAGRAPH,
66
+ sentence: row.SENTENCE,
67
+ text: row.TOKEN,
68
+ governors: [grammaticalRoleGovernor]
69
+ };
70
+ grammaticalRoles.push(grammaticalRole);
71
+ }
72
+ else if (row.RULE === 'Metadata') {
73
+ var meta = {
74
+ id: row.COUNTER,
75
+ token: row.TOKEN,
76
+ tokenType: row.TYPE,
77
+ offset: row.OFFSET,
78
+ paragraph: row.PARAGRAPH,
79
+ parent: row.PARENT
80
+ };
81
+ metadata.push(meta);
82
+ }
83
+ }
84
+ }
85
+ return resolve({
86
+ language: parameters.LANGUAGE_CODE,
87
+ mimeType: parameters.MIME_TYPE,
88
+ textSize: 0, // not returned by TA_ANALYZE
89
+ plaintext: parameters.PLAINTEXT,
90
+ tokens: tokens,
91
+ entities: entities,
92
+ metadata: metadata,
93
+ grammaticalRoles: grammaticalRoles
94
+ });
95
+ });
96
+ });
97
+ }
98
+
99
+ function normalizeInput(params) {
100
+ var mappings = {
101
+ 'inputDocumentText': 'DOCUMENT_TEXT',
102
+ 'language': 'LANGUAGE_CODE',
103
+ 'mimeType': 'MIME_TYPE',
104
+ 'tokenSeparators': 'TOKEN_SEPARATORS',
105
+ 'languageDetection': 'LANGUAGE_DETECTION'
106
+ };
107
+
108
+ var values = {
109
+ RETURN_PLAINTEXT: params.includePlainText ? 1 : 0
110
+ };
111
+
112
+ Object.keys(mappings).forEach(function (xscProp) {
113
+ if (params.hasOwnProperty(xscProp) && params[xscProp] !== undefined) {
114
+ var prop = mappings[xscProp];
115
+ values[prop] = params[xscProp];
116
+ }
117
+ });
118
+
119
+ if (params.inputDocumentBinaryContent) {
120
+ values.DOCUMENT_BINARY = extractBinary(params.inputDocumentBinaryContent);
121
+ }
122
+
123
+ return values;
124
+ }
125
+
126
+ function extractBinary(binContent) {
127
+ return bufferUtils.isBinary(binContent) ? bufferUtils.toBuffer(binContent) : Buffer.from(binContent);
128
+ }
@@ -0,0 +1,30 @@
1
+ 'use strict';
2
+
3
+ var MiningSession = require('./mining/Session');
4
+ var AnalysisSession = require('./analysis/Session');
5
+
6
+ exports.create = createTextObject;
7
+
8
+
9
+ function createTextObject($db) {
10
+ return {
11
+ mining: createMiningObject($db),
12
+ analysis: createAnalysisObject($db)
13
+ };
14
+ }
15
+
16
+ function createMiningObject($db) {
17
+ return {
18
+ Session: function (params) {
19
+ return new MiningSession($db, params);
20
+ }
21
+ };
22
+ }
23
+
24
+ function createAnalysisObject($db) {
25
+ return {
26
+ Session: function (params) {
27
+ return new AnalysisSession($db, params);
28
+ }
29
+ };
30
+ }
@@ -0,0 +1,82 @@
1
+ 'use strict';
2
+
3
+ var textmining = require('@sap/textmining');
4
+
5
+ module.exports = Session;
6
+
7
+ function Session($db, params) {
8
+ this._tm = new textmining({
9
+ client: async () => { await determineClient($db, params); },
10
+ referenceTable: params.referenceTable,
11
+ referenceColumn: params.referenceColumn
12
+ });
13
+ }
14
+
15
+ Session.prototype.categorizeKNN = function (params) {
16
+ return new Promise ((resolve, reject) => {
17
+ this._tm.categorizeKNN(params, function(err, result) {
18
+ if (err) { return reject(err);}
19
+ resolve(result);
20
+ });
21
+ });
22
+ };
23
+
24
+ Session.prototype.getRelatedDocuments = function (params) {
25
+ return new Promise ((resolve, reject) => {
26
+ this._tm.getRelatedDocuments(params, function(err, result) {
27
+ if (err) { return reject(err);}
28
+ resolve(result);
29
+ });
30
+ });
31
+ };
32
+
33
+ Session.prototype.getRelatedTerms = function (params) {
34
+ return new Promise ((resolve, reject) => {
35
+ this._tm.getRelatedTerms(params, function(err, result) {
36
+ if (err) { return reject(err);}
37
+ resolve(result);
38
+ });
39
+ });
40
+ };
41
+
42
+ Session.prototype.getRelevantTerms = function (params) {
43
+ return new Promise ((resolve, reject) => {
44
+ this._tm.getRelevantTerms(params, function(err, result) {
45
+ if (err) { return reject(err);}
46
+ resolve(result);
47
+ });
48
+ });
49
+ };
50
+
51
+ Session.prototype.getRelevantDocuments = function (params) {
52
+ return new Promise ((resolve, reject) => {
53
+ this._tm.getRelevantDocuments(params, function(err, result) {
54
+ if (err) { return reject(err);}
55
+ resolve(result);
56
+ });
57
+ });
58
+ };
59
+
60
+ Session.prototype.getSuggestedTerms = function (params) {
61
+ return new Promise ((resolve, reject) => {
62
+ this._tm.getSuggestedTerms(params, function(err, result) {
63
+ if (err) { return reject(err);}
64
+ resolve(result);
65
+ });
66
+ });
67
+ };
68
+
69
+ Session.prototype.initialize = function (params) {
70
+ return new Promise ((resolve, reject) => {
71
+ this._tm.initialize(params, function(err, result) {
72
+ if (err) { return reject(err);}
73
+ resolve(result);
74
+ });
75
+ });
76
+ };
77
+
78
+ async function determineClient($db, params) {
79
+ var conn = params.connection || await $db.getConnection();
80
+ return conn._client;
81
+ }
82
+
@@ -0,0 +1,41 @@
1
+ 'use strict';
2
+
3
+ var logging = require('../../logging');
4
+
5
+
6
+ exports.createTrace = function (options) {
7
+ var id = options.id;
8
+ var location = options.location;
9
+ var req = options.req;
10
+
11
+ var tracer;
12
+ if (id) {
13
+ tracer = logging.appContext.createLogContext({ id: id }).getTracer(location);
14
+ } else if (req && req.loggingContext) {
15
+ tracer = req.loggingContext.getTracer(location);
16
+ } else if (location) {
17
+ tracer = logging.appContext.createLogContext({ id: '' }).getTracer(location);
18
+ } else {
19
+ tracer = logging.tracer;
20
+ }
21
+ return buildTraceObject(tracer);
22
+ };
23
+
24
+ function buildTraceObject(tracer) {
25
+ return {
26
+ debug: tracer.debug.bind(tracer),
27
+ info: tracer.info.bind(tracer),
28
+ warning: tracer.warning.bind(tracer),
29
+ warn: tracer.warning.bind(tracer),
30
+ error: tracer.error.bind(tracer),
31
+ fatal: tracer.fatal.bind(tracer),
32
+
33
+ isDebugEnabled: tracer.isEnabled.bind(tracer, 'debug'),
34
+ isInfoEnabled: tracer.isEnabled.bind(tracer, 'info'),
35
+ isWarningEnabled: tracer.isEnabled.bind(tracer, 'warning'),
36
+ isErrorEnabled: tracer.isEnabled.bind(tracer, 'error'),
37
+ isFatalEnabled: tracer.isEnabled.bind(tracer, 'fatal'),
38
+
39
+ _tracer: tracer
40
+ };
41
+ }
@@ -0,0 +1,174 @@
1
+ 'use strict';
2
+
3
+ var _ = require('lodash');
4
+ var sax = require('sax');
5
+ var VError = require('verror');
6
+
7
+ var buffUtils = require('../../utils/buffer-utils');
8
+ var compressionUtils = require('../../utils/compression-utils');
9
+ var isWebBody = require('../../utils/xs-types').isWebBody;
10
+
11
+ module.exports = SAXParser;
12
+
13
+ var UNSUPPORTED_HANDLERS = ['attlistDeclHandler', 'endDoctypeDeclHandler',
14
+ 'endNameSpaceDeclHandler', 'entityDeclHandler', 'externalEntityRefHandler',
15
+ 'notationDeclHandler', 'processingInstructionHandler', 'startDoctypeDeclHandler',
16
+ 'startNameSpaceDeclHandler', 'xmlDeclHandler'];
17
+
18
+ var UNSUPPORTED_PROPERTIES = ['currentByteIndex', 'currentColumnNumber', 'currentLineNumber'];
19
+
20
+ function SAXParser() {
21
+ if (!(this instanceof SAXParser)) {
22
+ return new SAXParser();
23
+ }
24
+ this._parser = getNewParser();
25
+ this._parseStarted = false;
26
+
27
+ setStartCDataSectionHandler.call(this);
28
+ setEndCDataSectionHandler.call(this);
29
+ setStartElementHandler.call(this);
30
+ setEndElementHandler.call(this);
31
+ setCharacterDataHandler.call(this);
32
+ setCommentHandler.call(this);
33
+ setUnsupported.call(this);
34
+ }
35
+
36
+ SAXParser.prototype.parse = function (xml, encoding) {
37
+ if (this._parseStarted) {
38
+ throw new Error('SAXParser.parse already started');
39
+ }
40
+ this._parseStarted = true;
41
+ var parser = this._parser;
42
+ return new Promise((resolve, reject) => {
43
+ parser.onend = resolve;
44
+ parser.onerror = reject;
45
+ parser.write(getXML(xml, encoding)).close();
46
+ });
47
+ };
48
+
49
+
50
+ SAXParser.prototype.reset = function () {
51
+ this._parseStarted = false;
52
+ this._parser = getNewParser();
53
+ };
54
+
55
+ SAXParser.prototype.resume = function () {
56
+ throwNotSupported('resume');
57
+ };
58
+
59
+ SAXParser.prototype.stop = function (isResumable) { // eslint-disable-line no-unused-vars
60
+ throwNotSupported('stop');
61
+ };
62
+
63
+ function setUnsupported() {
64
+ UNSUPPORTED_HANDLERS.forEach(function (property) {
65
+ Object.defineProperty(this, property, {
66
+ set: throwNotSupported.bind(null, property)
67
+ });
68
+ }, this);
69
+
70
+ UNSUPPORTED_PROPERTIES.forEach(function (property) {
71
+ var notSupported = throwNotSupported.bind(null, property);
72
+ Object.defineProperty(this, property, {
73
+ set: notSupported,
74
+ get: notSupported
75
+ });
76
+ }, this);
77
+ }
78
+
79
+ function resolveEncoding(encoding) {
80
+ if (!encoding) {
81
+ return 'utf8';
82
+ }
83
+ var bufferEncoding = buffUtils.toBufferEncoding(encoding);
84
+ if (bufferEncoding) {
85
+ return bufferEncoding;
86
+ }
87
+ throw new VError('Encoding %s not supported', encoding);
88
+ }
89
+
90
+ function extractXmlContent(arg) {
91
+ if (!_.isString(arg) && !compressionUtils.isWebBodyOrArrayBuffer(arg)) {
92
+ throw new Error('xml must be String, ArrayBuffer or WebBody');
93
+ }
94
+ return isWebBody(arg) ? arg._retrieveContent() : buffUtils.getData(arg);
95
+ }
96
+
97
+ function getXML(xml, encoding) {
98
+ var data = extractXmlContent(xml);
99
+ if (_.isString(data)) {
100
+ return data;
101
+ }
102
+ var bufferEncoding = resolveEncoding(encoding);
103
+ return data.toString(bufferEncoding);
104
+ }
105
+
106
+ function getNewParser() {
107
+ return sax.parser(true, {
108
+ xmlns: true,
109
+ position: true,
110
+ strictEntities: false
111
+ });
112
+ }
113
+
114
+ function setHandler(eventName, handlerName, numberOfArguments) {
115
+ Object.defineProperty(this, handlerName, {
116
+ set: function (handler) {
117
+ checkHandler(handler, handlerName, numberOfArguments);
118
+ this._parser[eventName] = handler;
119
+ }
120
+ });
121
+ }
122
+
123
+ function setStartCDataSectionHandler() {
124
+ setHandler.call(this, 'onopencdata', 'startCDataSectionHandler', 0);
125
+ }
126
+
127
+ function setEndCDataSectionHandler() {
128
+ setHandler.call(this, 'onclosecdata', 'endCDataSectionHandler', 0);
129
+ }
130
+
131
+ function setStartElementHandler() {
132
+ Object.defineProperty(this, 'startElementHandler', {
133
+ set: function (startElementHandler) {
134
+ checkHandler(startElementHandler, 'startElementHandler', 2);
135
+ this._parser.onopentag = function startElementHandlerWrapper(node) {
136
+ var attributes = node.attributes;
137
+ attributes = _.omitBy(attributes, function (att) {
138
+ return att.prefix === 'xmlns';
139
+ });
140
+ attributes = _.mapValues(attributes, function (attribute) {
141
+ return attribute.value;
142
+ });
143
+ startElementHandler(node.name, attributes);
144
+ };
145
+ }
146
+ });
147
+ }
148
+
149
+ function setEndElementHandler() {
150
+ setHandler.call(this, 'onclosetag', 'endElementHandler', 1);
151
+ }
152
+
153
+ function setCharacterDataHandler() {
154
+ setHandler.call(this, 'ontext', 'characterDataHandler', 1);
155
+ }
156
+
157
+ function setCommentHandler() {
158
+ setHandler.call(this, 'oncomment', 'commentHandler', 1);
159
+ }
160
+
161
+ // function parseXml(parser, xml, cb) {
162
+ // parser.onend = cb;
163
+ // parser.write(xml).close();
164
+ // }
165
+
166
+ function throwNotSupported(item) {
167
+ throw new Error(item + ' not supported');
168
+ }
169
+
170
+ function checkHandler(handler, handlerName, numberOfArguments) {
171
+ if (typeof handler !== 'function' || handler.length !== numberOfArguments) {
172
+ throw new VError('Expected "%s" to be a function with %d arguments', handlerName, numberOfArguments);
173
+ }
174
+ }