@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
package/lib/sandbox.js ADDED
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ var _ = require('lodash');
4
+ var vm = require('vm');
5
+ var ctypes = require('./ctypes');
6
+
7
+ module.exports.create = create;
8
+
9
+ function create(xsruntime, additionalContext) {
10
+ var sandbox = vm.createContext(_.extend(
11
+ {
12
+ $ : xsruntime,
13
+ ctypes : ctypes,
14
+ console : console,
15
+ xsruntime : xsruntime,
16
+ Uint8Array : Uint8Array // eslint-disable-line no-undef
17
+ }, additionalContext));
18
+ vm.runInContext('\
19
+ if (!Number.isInteger) { Number.isInteger = function(i) { return typeof i === "number"; } };\
20
+ String.prototype.contains = function(str) {return this.indexOf(str) >= 0;};\
21
+ String.prototype.startsWith = function(str) {return this.indexOf(str) === 0;};\
22
+ String.prototype.endsWith = function(str) {\
23
+ var l = this.lastIndexOf(str);\
24
+ return l === -1 ? false : l + str.length === this.length;\
25
+ };\
26
+ [\'Array\',\'String\'].forEach(function(fn) {\
27
+ var obj = eval(fn);\
28
+ Object.getOwnPropertyNames(obj.prototype).filter(function(p) {\
29
+ return typeof obj.prototype[p] === \'function\'}).forEach(function(o)\
30
+ {\
31
+ obj[o] = function()\
32
+ {\
33
+ var args = Array.prototype.slice.call(arguments);\
34
+ var f = args.shift();\
35
+ return obj.prototype[o].apply(f, args);\
36
+ }\
37
+ });\
38
+ });', sandbox);
39
+ return sandbox;
40
+ }
@@ -0,0 +1,57 @@
1
+ 'use strict';
2
+
3
+ var assert = require('assert');
4
+ var util = require('util');
5
+ var runXsFunction = require('./xs-function-runner').runXsFunction;
6
+
7
+ module.exports = XsJsFunctionRunner;
8
+
9
+
10
+ /**
11
+ * Constructs an objects that can execute functions from given xsjs script.
12
+ *
13
+ * @param {Runtime} xsjs runtime, see lib/runtime.js
14
+ * @param {string} pathToScript path to xsjs script, e.g. /foo/bar.xsjs
15
+ * @param {object} context execution context defining global variables
16
+ */
17
+ function XsJsFunctionRunner(runtime, pathToScript, context) {
18
+ assert(runtime && typeof runtime === 'object', 'Valid runtime should be provided');
19
+ assert(typeof pathToScript === 'string', 'Valid path to script should be provided');
20
+ assert(context && typeof context === 'object', 'Valid context should be provided');
21
+
22
+ validateScript(runtime, pathToScript);
23
+
24
+ this._scriptRunner = createScriptRunner(runtime, pathToScript, context);
25
+ }
26
+
27
+ /**
28
+ * Executes function from the script provided in constructor. Uses {xs-function-runner#runXsFunction}, see this
29
+ * function for more details on how it works.
30
+ *
31
+ * @param functionName
32
+ * @param thisArg
33
+ * @param argsArray
34
+ * @param cb
35
+ */
36
+ XsJsFunctionRunner.prototype.run = async function(functionName, thisArg, argsArray) {
37
+ return await runXsFunction(this._scriptRunner, functionName, thisArg, argsArray);
38
+ };
39
+
40
+ function validateScript(runtime, pathToScript) {
41
+ var script = runtime.getXsjsModule(pathToScript);
42
+ if (!script) {
43
+ throw new Error(util.format('Script "%s" not found', pathToScript));
44
+ }
45
+ }
46
+
47
+ function createScriptRunner(runtime, pathToScript, context) {
48
+ return {
49
+ runtime: runtime,
50
+ context: context,
51
+ pathToScript: pathToScript,
52
+ runScript: async function() {
53
+ var module = await runtime.runXsjs(pathToScript, context);
54
+ return module.namespace.default;
55
+ }
56
+ };
57
+ }
@@ -0,0 +1,57 @@
1
+ 'use strict';
2
+
3
+ var assert = require('assert');
4
+ var util = require('util');
5
+ var runXsFunction = require('./xs-function-runner').runXsFunction;
6
+
7
+ module.exports = XsJsLibFunctionRunner;
8
+
9
+ /**
10
+ * Constructs an object that can execute functions from given xsjslib script.
11
+ *
12
+ * @param {Runtime} xsjs runtime, see lib/runtime.js
13
+ * @param {string} libId library ID according to XS1 rules, example:
14
+ * 'foo.bar.mylib' corresponding to /foo/bar/mylib.xsjslib
15
+ * @param {object} context execution context defining global variables, if not provided, a default one is created
16
+ */
17
+ function XsJsLibFunctionRunner(runtime, libId, context) {
18
+ assert(runtime && typeof runtime === 'object', 'Valid runtime should be provided');
19
+ assert(typeof libId === 'string', 'Valid path to script should be provided');
20
+ assert(context && typeof context === 'object', 'Valid context should be provided');
21
+
22
+ validateLibrary(runtime, libId);
23
+
24
+ this._scriptRunner = createScriptRunner(runtime, libId, context);
25
+ }
26
+
27
+ /**
28
+ * Executes function from the script provided in constructor. Uses {xs-function-runner#runXsFunction}, see this
29
+ * function for more details on how it works.
30
+ *
31
+ * @param functionName
32
+ * @param thisArg
33
+ * @param argsArray
34
+ * @param cb
35
+ */
36
+ XsJsLibFunctionRunner.prototype.run = async function(functionName, thisArg, argsArray) {
37
+ return await runXsFunction(this._scriptRunner, functionName, thisArg, argsArray);
38
+ };
39
+
40
+ function validateLibrary(runtime, libId) {
41
+ var module = runtime.getLibraryModule(libId);
42
+ if (!module) {
43
+ throw new Error(util.format('Xsjs library with id "%s" not found', libId));
44
+ }
45
+ }
46
+
47
+ function createScriptRunner(runtime, libId, context) {
48
+ return {
49
+ runtime: runtime,
50
+ context: context,
51
+ pathToScript: libId,
52
+ runScript: async function() {
53
+ var module = await runtime.runXsjslib(libId, context);
54
+ return module.namespace.default;
55
+ }
56
+ };
57
+ }
@@ -0,0 +1,77 @@
1
+ 'use strict';
2
+
3
+ var _ = require('lodash');
4
+ var hexRegex = /^(?:[0-9a-fA-F]{2})*$/;
5
+
6
+ module.exports = {
7
+
8
+ isArrayBuffer: function (arg) {
9
+ return Object.prototype.toString.call(arg) === '[object ArrayBuffer]';
10
+ },
11
+
12
+ isBuffer: function (arg) {
13
+ return Buffer.isBuffer(arg);
14
+ },
15
+
16
+ isBinary: function (arg) {
17
+ return module.exports.isBuffer(arg) || module.exports.isArrayBuffer(arg);
18
+ },
19
+
20
+ toArrayBuffer: function (data) {
21
+ if (module.exports.isArrayBuffer(data)) {
22
+ return data;
23
+ }
24
+
25
+ var ab = new ArrayBuffer(data.length); // eslint-disable-line no-undef
26
+ var view = new Uint8Array(ab); // eslint-disable-line no-undef
27
+ for (var i = 0; i < data.length; ++i) {
28
+ view[i] = data[i];
29
+ }
30
+ return ab;
31
+ },
32
+
33
+ hexStringToBuffer: function (data) {
34
+ if (!hexRegex.test(data)) {
35
+ throw new TypeError('Invalid hex string: ' + data);
36
+ }
37
+
38
+ return Buffer.from(data, 'hex');
39
+ },
40
+
41
+ toBuffer: function (data) {
42
+ if (module.exports.isBuffer(data)) {
43
+ return data;
44
+ }
45
+
46
+ if (_.isString(data)) {
47
+ return Buffer.from(data);
48
+ }
49
+
50
+ var array = new Uint8Array(data); // eslint-disable-line no-undef
51
+ return Buffer.from(array);
52
+ },
53
+
54
+ bodyToBuffer: function (webBody) {
55
+ var rawContent = webBody._retrieveContent();
56
+ return _.isString(rawContent) ? Buffer.from(rawContent, 'utf8') : rawContent;
57
+ },
58
+
59
+ toBufferEncoding: function (encoding) {
60
+ encoding = encoding.toLowerCase();
61
+ switch (encoding) {
62
+ case 'utf-8': return 'utf8';
63
+ case 'utf-16': return 'utf16le';
64
+ case 'us-ascii': return 'ascii';
65
+ default: return Buffer.isEncoding(encoding) ? encoding : null;
66
+ }
67
+ },
68
+
69
+ getData: function (data) {
70
+ if (!_.isString(data) && !module.exports.isArrayBuffer(data)) {
71
+ throw new Error('First argument must be string or ArrayBuffer.');
72
+ }
73
+
74
+ return module.exports.isArrayBuffer(data) ? module.exports.toBuffer(data) : data;
75
+ }
76
+
77
+ };
@@ -0,0 +1,14 @@
1
+ 'use strict';
2
+
3
+ var WebBody = require('../xsjs/web/WebBody');
4
+ var buffUtils = require('./buffer-utils');
5
+
6
+ module.exports = {
7
+ isWebBodyOrArrayBuffer: function(parameter) {
8
+ return parameter instanceof WebBody || buffUtils.isArrayBuffer(parameter);
9
+ },
10
+
11
+ extractBuffer: function (source) {
12
+ return (source instanceof WebBody) ? buffUtils.bodyToBuffer(source) : buffUtils.toBuffer(source);
13
+ }
14
+ };
@@ -0,0 +1,104 @@
1
+ 'use strict';
2
+
3
+ const FRACTIONAL_SECONDS_DIGITS = {
4
+ JS_DATE: 3,
5
+ STRING: 7
6
+ };
7
+
8
+ module.exports = {
9
+ dateToJsDate: dateToJsDate,
10
+ timeToJsDate: timeToJsDate,
11
+ dateTimeToJsDate: dateTimeToJsDate,
12
+ normalizeDateTimeString: normalizeDateTimeString
13
+ };
14
+
15
+ function dateToJsDate(date, treatAsUTC) {
16
+ return createDateObject({
17
+ year: date.substring(0, 4),
18
+ month: parseInt(date.substring(5, 7)) - 1,
19
+ day: date.substring(8),
20
+ hour: 0,
21
+ minute: 0,
22
+ second: 0,
23
+ millis: 0
24
+ }, treatAsUTC);
25
+ }
26
+
27
+ function timeToJsDate(time, treatAsUTC) {
28
+ return createDateObject({
29
+ year: 0,
30
+ month: 0,
31
+ day: 0,
32
+ hour: time.substring(0, 2),
33
+ minute: time.substring(3, 5),
34
+ second: time.substring(6),
35
+ millis: 0
36
+ }, treatAsUTC);
37
+ }
38
+
39
+ function dateTimeToJsDate(dateTime, treatAsUTC) {
40
+ var targetLength = FRACTIONAL_SECONDS_DIGITS.JS_DATE;
41
+ var millis = dateTime.substring(20);
42
+ if (millis.length < targetLength) {
43
+ millis += '0'.repeat(targetLength - millis.length);
44
+ } else if (millis.length > targetLength) {
45
+ millis = millis.substring(0, targetLength);
46
+ }
47
+
48
+ return createDateObject({
49
+ year: dateTime.substring(0, 4),
50
+ month: parseInt(dateTime.substring(5, 7)) - 1,
51
+ day: dateTime.substring(8, 10),
52
+ hour: dateTime.substring(11, 13),
53
+ minute: dateTime.substring(14, 16),
54
+ second: dateTime.substring(17, 19),
55
+ millis: millis
56
+ }, treatAsUTC);
57
+ }
58
+
59
+ function createDateObject(values, treatAsUTC) {
60
+ var date;
61
+ if (treatAsUTC) {
62
+ date = new Date(Date.UTC(values.year,
63
+ values.month,
64
+ values.day,
65
+ values.hour,
66
+ values.minute,
67
+ values.second,
68
+ values.millis));
69
+ } else {
70
+ date = new Date(values.year,
71
+ values.month,
72
+ values.day,
73
+ values.hour,
74
+ values.minute,
75
+ values.second,
76
+ values.millis);
77
+ }
78
+
79
+ // Workaround: two-digit years map to 1900-1999 which is not desired
80
+ if (values.year >= 0 && values.year < 100) {
81
+ if (treatAsUTC) {
82
+ date.setUTCFullYear(values.year);
83
+ } else {
84
+ date.setFullYear(values.year);
85
+ }
86
+ }
87
+
88
+ return date;
89
+ }
90
+
91
+ function normalizeDateTimeString(dateTime) {
92
+ var parts = dateTime.split('.');
93
+ if (parts.length === 1) {
94
+ return dateTime;
95
+ }
96
+
97
+ var fractionalSeconds = parts[1];
98
+ var targetLength = FRACTIONAL_SECONDS_DIGITS.STRING;
99
+ if (fractionalSeconds.length <= targetLength) {
100
+ return dateTime;
101
+ }
102
+
103
+ return parts[0] + '.' + parts[1].substring(0, targetLength);
104
+ }
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+
3
+ var util = require('util');
4
+ var http = require('http');
5
+
6
+ module.exports = HttpError;
7
+
8
+ function HttpError(status, msg) {
9
+ if (typeof status === 'string' && !msg) {
10
+ msg = status;
11
+ status = undefined;
12
+ }
13
+ this.status = status || 500;
14
+ this.message = msg || http.STATUS_CODES[this.status] || (this.status + '');
15
+ Error.captureStackTrace(this, HttpError);
16
+ }
17
+
18
+ util.inherits(HttpError, Error);
19
+
20
+ HttpError.prototype.name = 'HttpError';
@@ -0,0 +1,18 @@
1
+ 'use strict';
2
+
3
+ var _ = require('lodash');
4
+
5
+ module.exports = function wrapAppError(err) {
6
+ if (_.isError(err)) {
7
+ return err;
8
+ }
9
+ if (err && err.message && err.code) {
10
+ // error objects coming from @sap/hana-client
11
+ // are not instances of Error
12
+ var e = new Error(err.message);
13
+ e.code = err.code;
14
+ e.sqlState = err.sqlState;
15
+ return e;
16
+ }
17
+ return new Error(err);
18
+ };
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+
3
+ var xspath = require('./xspath');
4
+ exports.toResourceId = xspath.toResourceId;
5
+ exports.toPath = xspath.toPath;
6
+ exports.toXSObjectId = xspath.toXSObjectId;
7
+
8
+ var callstack = require('./xsstack');
9
+ exports.getCallingXsjs = callstack.getCallingXsjs;
10
+ exports.getCallingXsjsRelativePath = callstack.getCallingXsjsRelativePath;
11
+
12
+ var xsjsrunner = require('./xs-function-runner');
13
+ exports.runXsFunction = xsjsrunner.runXsFunction;
14
+ exports.createXsFunctionThisArg = xsjsrunner.createXsFunctionThisArg;
15
+
16
+ exports.XsJsFunctionRunner = require('./XsJsFunctionRunner');
17
+ exports.XsJsLibFunctionRunner = require('./XsJsLibFunctionRunner');
@@ -0,0 +1,51 @@
1
+ 'use strict';
2
+
3
+ var assert = require('assert');
4
+ var util = require('util');
5
+
6
+ exports.runXsFunction = runXsFunction;
7
+ exports.createXsFunctionThisArg = createXsFunctionThisArg;
8
+
9
+ /**
10
+ * Executes the given function from the xsjs script provided in the constructor.
11
+ *
12
+ * Note that although xsjs code is executed in a fiber, this function will run it asynchronously,
13
+ * i.e. it will return when xsjs code executes the first async/blocking operation.
14
+ *
15
+ * @param {scriptRunner} object of type <code>{ runtime: {}, context: {}, pathToScript: '', runScript: function() {} }</code>.
16
+ * Will be used to get the executed script where function with name 'functionName'
17
+ * should exist and will be executed.
18
+ * @param {string} functionName name of the function to execute
19
+ * @param {object} thisArg the value of `this` provided to the function, can be null or undefined
20
+ * @param {array} argsArray function arguments, can be null or undefined
21
+ */
22
+ async function runXsFunction(scriptRunner, functionName, thisArg, argsArray) {
23
+ assert(typeof scriptRunner === 'object', 'Valid script runner should to be provided');
24
+ assert(functionName && typeof functionName === 'string', 'Invalid function name');
25
+ assert(!argsArray || Array.isArray(argsArray), 'Pass function arguments as an array');
26
+ var rt = scriptRunner.runtime;
27
+ var context = scriptRunner.context;
28
+
29
+ var module = null;
30
+ try {
31
+ module = await scriptRunner.runScript();
32
+ var jsFunction = module[functionName];
33
+ if (!jsFunction) {
34
+ throw new Error(util.format('Function "%s" not found in script "%s"', functionName, scriptRunner.pathToScript));
35
+ }
36
+ if (typeof jsFunction !== 'function') {
37
+ throw new Error(util.format('"%s" is not a function', functionName));
38
+ }
39
+ return jsFunction.apply(thisArg, argsArray);
40
+ } finally {
41
+ rt.cleanupContext(context);
42
+ rt.triggerGc();
43
+ }
44
+ }
45
+
46
+ function createXsFunctionThisArg(context) {
47
+ return {
48
+ $: context,
49
+ xsengine: context
50
+ };
51
+ }
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ var Zip = require('../xsjs/util/Zip');
4
+ var WebBody = require('../xsjs/web/WebBody');
5
+ var BasicWebEntity = require('../xsjs/web/BasicWebEntity');
6
+
7
+ module.exports.isWebBody = isWebBody;
8
+ module.exports.isZip = isZip;
9
+ module.exports.isBasicWebEntity = isBasicWebEntity;
10
+
11
+ function isWebBody(obj) {
12
+ return obj instanceof WebBody;
13
+ }
14
+
15
+ function isZip(obj) {
16
+ return obj instanceof Zip;
17
+ }
18
+
19
+ function isBasicWebEntity(obj) {
20
+ return obj instanceof BasicWebEntity;
21
+ }
@@ -0,0 +1,36 @@
1
+ 'use strict';
2
+
3
+ var util = require('util');
4
+
5
+ exports.toResourceId = function (pathname) {
6
+ var pos = pathname.lastIndexOf('.');
7
+ if (pos < 0) {
8
+ throw new Error('File without extension: ' + pathname);
9
+ }
10
+ return pathname.substring(1, pos).replace(/\/|\\/g, '.');
11
+ };
12
+
13
+
14
+ exports.toPath = function (packagename, filename) {
15
+ var normalizedPackage = packagename ? ('/' + packagename.replace(/\./g, '/')) : '';
16
+ return util.format('%s/%s', normalizedPackage, filename || '');
17
+ };
18
+
19
+ exports.toXSObjectId = function (packagename, objectname) {
20
+ if (typeof packagename !== 'string') {
21
+ throw new TypeError('Packagename should be a string and is required to build XS object ID');
22
+ }
23
+
24
+ if (objectname && (typeof objectname !== 'string')) {
25
+ throw new TypeError('Object name provided to build XS object ID is not a string');
26
+ }
27
+
28
+ var packName = packagename.trim();
29
+ var objName = (objectname) ? objectname.trim() : '';
30
+
31
+ if (packName && objName) {
32
+ return packName + '.' + objName;
33
+ }
34
+
35
+ return packName;
36
+ };
@@ -0,0 +1,28 @@
1
+ 'use strict';
2
+
3
+ var callsite = require('callsite');
4
+ var path = require('path');
5
+
6
+ exports.getCallingXsjs = function () {
7
+ var stack = callsite();
8
+ var frame;
9
+ while ((frame = stack.shift())) {
10
+ var file = frame.getFileName();
11
+ var ext = path.extname(file);
12
+ if (ext === '.xsjs' || ext === '.xsjslib') {
13
+ return file;
14
+ }
15
+ }
16
+ };
17
+
18
+ exports.getCallingXsjsRelativePath = function (rootDirs) {
19
+ var file = this.getCallingXsjs();
20
+ var callingXsjs;
21
+ rootDirs.forEach(function (dir) {
22
+ var isCurrentWorkingDir = path.relative(dir, file).indexOf('..') === -1;
23
+ if (isCurrentWorkingDir) {
24
+ callingXsjs = path.relative(dir, file);
25
+ }
26
+ });
27
+ return callingXsjs;
28
+ };
@@ -0,0 +1,28 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1">
7
+ <title><%= title %></title>
8
+ <style type="text/css">
9
+ body {
10
+ padding: 20px 50px;
11
+ font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
12
+ color: #333;
13
+ }
14
+ pre {
15
+ padding: 10px;
16
+ background-color: #f9f9f9;
17
+ -webkit-border-radius: 5px;
18
+ -moz-border-radius: 5px;
19
+ border-radius: 5px;
20
+ border: 1px solid #eee;
21
+ }
22
+ </style>
23
+ </head>
24
+ <body>
25
+ <h1><%= title %></h1>
26
+ <%= stacktrace %>
27
+ </body>
28
+ </html>
@@ -0,0 +1,28 @@
1
+ 'use strict';
2
+
3
+ module.exports = Application;
4
+
5
+ function Application(locale, webResponse) {
6
+ Object.defineProperties(this, {
7
+ _langValue: {
8
+ value: locale.applicationLanguage,
9
+ writable: true
10
+ },
11
+ _webResponse: {
12
+ value: webResponse
13
+ }
14
+ });
15
+ }
16
+
17
+ Object.defineProperties(Application.prototype, {
18
+ language: {
19
+ set: function (value) {
20
+ this._langValue = value;
21
+ this._webResponse.cookies.set('xsAppLanguage', value);
22
+ },
23
+ get: function () {
24
+ return this._langValue;
25
+ },
26
+ enumerable: true
27
+ }
28
+ });
@@ -0,0 +1,53 @@
1
+ 'use strict';
2
+
3
+ var acceptLanguage = require('accept-language');
4
+
5
+ module.exports = Locale;
6
+
7
+ function Locale(req) {
8
+ this.applicationLanguage = resolveApplicationLanguage(req);
9
+ this.sessionLanguage = resolveSessionLanguage(req);
10
+ this.requestLanguage = resolveRequestLanguage(req, this);
11
+ if (!this.sessionLanguage) {
12
+ this.sessionLanguage = this.requestLanguage;
13
+ }
14
+ this.dbLocale = this.requestLanguage.replace(/-/g, '_');
15
+ }
16
+
17
+ function resolveApplicationLanguage(req) {
18
+ var appLanguage = req.cookies['xsAppLanguage'];
19
+ return appLanguage ? appLanguage : '';
20
+ }
21
+
22
+ function resolveSessionLanguage(req) {
23
+ // Note: In the XS Classic the $.session.language is taken from the "xsSessionLanguage" cookie.
24
+ // If not present, it fallbacks to the locale of the HANA user
25
+ // (which is not applicable in XS Advanced since we work with a single technical user for HANA db connections).
26
+
27
+ return req.cookies['xsSessionLanguage'] || '';
28
+ }
29
+
30
+ function resolveRequestLanguage(req, locale) {
31
+ var sapLang = req.headers['x-sap-request-language'];
32
+ if (sapLang) {
33
+ return sapLang;
34
+ }
35
+
36
+ // Note: Changing the value of $.application.language does not affect the value of $.request.language.
37
+ // Thus, the value that takes part in the fallback logic is not the value of $.request.language (in the JS layer),
38
+ // but the value of the corresponding incoming cookie ("xsAppLanguage").
39
+ if (locale.applicationLanguage) {
40
+ return locale.applicationLanguage;
41
+ }
42
+
43
+ if (locale.sessionLanguage) {
44
+ return locale.sessionLanguage;
45
+ }
46
+
47
+ var languagesByPreference = acceptLanguage.parse(req.headers['accept-language']);
48
+ if (Array.isArray(languagesByPreference) && languagesByPreference.length) {
49
+ return languagesByPreference[0].value;
50
+ }
51
+
52
+ return '';
53
+ }