cgserver 8.9.3 → 8.9.5

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 (176) hide show
  1. package/README.md +2 -0
  2. package/dist/lib/Framework/AI/AiObject.js +10 -10
  3. package/dist/lib/Framework/AI/Astar.js +133 -133
  4. package/dist/lib/Framework/AI/BehaviorAI.js +307 -307
  5. package/dist/lib/Framework/AI/Entity.js +33 -33
  6. package/dist/lib/Framework/AI/Point.js +68 -68
  7. package/dist/lib/Framework/AI/TriggerMgr.js +201 -201
  8. package/dist/lib/Framework/Config/Config.js +53 -53
  9. package/dist/lib/Framework/Config/DbConfig.js +14 -14
  10. package/dist/lib/Framework/Config/FrameworkConfig.js +208 -207
  11. package/dist/lib/Framework/Config/IServerConfig.js +26 -26
  12. package/dist/lib/Framework/Config/_error_.js +35 -35
  13. package/dist/lib/Framework/Core/Core.js +665 -639
  14. package/dist/lib/Framework/Core/Timer.js +116 -116
  15. package/dist/lib/Framework/Database/Decorator/AutoIncrement.js +11 -11
  16. package/dist/lib/Framework/Database/Decorator/DBCache.js +64 -64
  17. package/dist/lib/Framework/Database/Decorator/NotNull.js +11 -11
  18. package/dist/lib/Framework/Database/Decorator/PrimaryKey.js +11 -11
  19. package/dist/lib/Framework/Database/Decorator/Property.js +61 -61
  20. package/dist/lib/Framework/Database/Decorator/Table.js +15 -15
  21. package/dist/lib/Framework/Database/Decorator/Type.js +20 -20
  22. package/dist/lib/Framework/Database/MSSqlManager.js +53 -53
  23. package/dist/lib/Framework/Database/MongoBaseService.js +117 -117
  24. package/dist/lib/Framework/Database/MongoManager.js +467 -467
  25. package/dist/lib/Framework/Database/MysqlBaseService.js +281 -281
  26. package/dist/lib/Framework/Database/MysqlManager.js +179 -179
  27. package/dist/lib/Framework/Database/RedisManager.js +61 -61
  28. package/dist/lib/Framework/Decorator/AdminValidate.js +19 -19
  29. package/dist/lib/Framework/Decorator/AuthorityValidate.js +22 -22
  30. package/dist/lib/Framework/Decorator/CreatorValidate.js +19 -19
  31. package/dist/lib/Framework/Decorator/JsonAdminValidate.js +15 -15
  32. package/dist/lib/Framework/Decorator/JsonAuthorityValidate.js +20 -20
  33. package/dist/lib/Framework/Decorator/JsonCreatorValidate.js +19 -19
  34. package/dist/lib/Framework/Decorator/SyncCall.js +42 -42
  35. package/dist/lib/Framework/Decorator/SyncCallServer.js +77 -77
  36. package/dist/lib/Framework/Logic/CacheTool.js +73 -73
  37. package/dist/lib/Framework/Logic/EventTool.js +27 -27
  38. package/dist/lib/Framework/Logic/HttpTool.js +75 -75
  39. package/dist/lib/Framework/Logic/Log.js +140 -140
  40. package/dist/lib/Framework/Logic/SyncQueueTool.js +43 -43
  41. package/dist/lib/Framework/Service/MongoAccountService.js +367 -367
  42. package/dist/lib/Framework/Service/MongoCacheService.js +42 -42
  43. package/dist/lib/Framework/Service/MongoUserService.js +100 -100
  44. package/dist/lib/Framework/Service/MysqlAccountService.js +441 -441
  45. package/dist/lib/Framework/Service/MysqlUserService.js +171 -171
  46. package/dist/lib/Framework/Service/ini.js +36 -36
  47. package/dist/lib/Framework/SocketServer/IClientWebSocket.js +44 -44
  48. package/dist/lib/Framework/SocketServer/IRpc.js +12 -12
  49. package/dist/lib/Framework/SocketServer/IRpcClientWebSocket.js +63 -63
  50. package/dist/lib/Framework/SocketServer/IRpcServerWebSocket.js +63 -63
  51. package/dist/lib/Framework/SocketServer/IServerWebSocket.js +75 -75
  52. package/dist/lib/Framework/SocketServer/ISocketServer.js +205 -205
  53. package/dist/lib/Framework/SocketServer/IWebSocket.js +195 -195
  54. package/dist/lib/Framework/SocketServer/ProtoFilter/GoogleProtoFilter.js +57 -57
  55. package/dist/lib/Framework/SocketServer/ProtoFilter/IProtoFilter.js +8 -8
  56. package/dist/lib/Framework/SocketServer/ProtoFilter/JsonProtoFilter.js +29 -29
  57. package/dist/lib/Framework/SocketServer/ProtoFilter/ProtoFactory.js +31 -31
  58. package/dist/lib/Framework/ThirdParty/AlipayTool.js +203 -203
  59. package/dist/lib/Framework/ThirdParty/Alisms.js +46 -46
  60. package/dist/lib/Framework/ThirdParty/AppleTool.js +234 -234
  61. package/dist/lib/Framework/ThirdParty/CgMq.js +132 -132
  62. package/dist/lib/Framework/ThirdParty/EmailTool.js +33 -33
  63. package/dist/lib/Framework/ThirdParty/OpenSocial.js +32 -32
  64. package/dist/lib/Framework/ThirdParty/QQTool.js +118 -118
  65. package/dist/lib/Framework/ThirdParty/QiniuTool.js +21 -21
  66. package/dist/lib/Framework/ThirdParty/Rpc.js +63 -63
  67. package/dist/lib/Framework/ThirdParty/WechatOATool.js +61 -61
  68. package/dist/lib/Framework/ThirdParty/WechatTool.js +74 -74
  69. package/dist/lib/Framework/WebServer/Controller/BaseController.js +112 -112
  70. package/dist/lib/Framework/WebServer/Controller/MongoBaseUserController.js +169 -169
  71. package/dist/lib/Framework/WebServer/Controller/MysqlBaseUserController.js +169 -169
  72. package/dist/lib/Framework/WebServer/Engine/ControllerManager.js +112 -112
  73. package/dist/lib/Framework/WebServer/Engine/Engine.js +152 -152
  74. package/dist/lib/Framework/WebServer/Engine/RazorJs.js +544 -544
  75. package/dist/lib/Framework/WebServer/Engine/Request.js +237 -237
  76. package/dist/lib/Framework/WebServer/Engine/Response.js +100 -100
  77. package/dist/lib/Framework/WebServer/IWebServer.js +59 -59
  78. package/dist/lib/Framework/cgserver.js +121 -121
  79. package/dist/lib/Framework/index.js +202 -202
  80. package/dist/lib/test/test.js +21 -21
  81. package/dist/types/Framework/AI/AiObject.d.ts +5 -5
  82. package/dist/types/Framework/AI/Astar.d.ts +38 -38
  83. package/dist/types/Framework/AI/BehaviorAI.d.ts +24 -24
  84. package/dist/types/Framework/AI/Entity.d.ts +12 -12
  85. package/dist/types/Framework/AI/Point.d.ts +17 -17
  86. package/dist/types/Framework/AI/TriggerMgr.d.ts +52 -52
  87. package/dist/types/Framework/Config/Config.d.ts +12 -12
  88. package/dist/types/Framework/Config/DbConfig.d.ts +10 -10
  89. package/dist/types/Framework/Config/FrameworkConfig.d.ts +204 -203
  90. package/dist/types/Framework/Config/IServerConfig.d.ts +9 -9
  91. package/dist/types/Framework/Config/_error_.d.ts +99 -99
  92. package/dist/types/Framework/Core/Core.d.ts +111 -108
  93. package/dist/types/Framework/Core/Timer.d.ts +16 -16
  94. package/dist/types/Framework/Database/Decorator/AutoIncrement.d.ts +1 -1
  95. package/dist/types/Framework/Database/Decorator/DBCache.d.ts +23 -23
  96. package/dist/types/Framework/Database/Decorator/NotNull.d.ts +1 -1
  97. package/dist/types/Framework/Database/Decorator/PrimaryKey.d.ts +1 -1
  98. package/dist/types/Framework/Database/Decorator/Property.d.ts +57 -57
  99. package/dist/types/Framework/Database/Decorator/Table.d.ts +1 -1
  100. package/dist/types/Framework/Database/Decorator/Type.d.ts +1 -1
  101. package/dist/types/Framework/Database/MSSqlManager.d.ts +27 -27
  102. package/dist/types/Framework/Database/MongoBaseService.d.ts +81 -81
  103. package/dist/types/Framework/Database/MongoManager.d.ts +155 -155
  104. package/dist/types/Framework/Database/MysqlBaseService.d.ts +34 -34
  105. package/dist/types/Framework/Database/MysqlManager.d.ts +53 -53
  106. package/dist/types/Framework/Database/RedisManager.d.ts +24 -24
  107. package/dist/types/Framework/Decorator/AdminValidate.d.ts +1 -1
  108. package/dist/types/Framework/Decorator/AuthorityValidate.d.ts +2 -2
  109. package/dist/types/Framework/Decorator/CreatorValidate.d.ts +1 -1
  110. package/dist/types/Framework/Decorator/JsonAdminValidate.d.ts +1 -1
  111. package/dist/types/Framework/Decorator/JsonAuthorityValidate.d.ts +2 -2
  112. package/dist/types/Framework/Decorator/JsonCreatorValidate.d.ts +1 -1
  113. package/dist/types/Framework/Decorator/SyncCall.d.ts +12 -12
  114. package/dist/types/Framework/Decorator/SyncCallServer.d.ts +14 -14
  115. package/dist/types/Framework/Logic/CacheTool.d.ts +27 -27
  116. package/dist/types/Framework/Logic/EventTool.d.ts +13 -13
  117. package/dist/types/Framework/Logic/HttpTool.d.ts +15 -15
  118. package/dist/types/Framework/Logic/Log.d.ts +25 -25
  119. package/dist/types/Framework/Logic/SyncQueueTool.d.ts +21 -21
  120. package/dist/types/Framework/Service/MongoAccountService.d.ts +69 -69
  121. package/dist/types/Framework/Service/MongoCacheService.d.ts +14 -14
  122. package/dist/types/Framework/Service/MongoUserService.d.ts +35 -35
  123. package/dist/types/Framework/Service/MysqlAccountService.d.ts +74 -74
  124. package/dist/types/Framework/Service/MysqlUserService.d.ts +33 -33
  125. package/dist/types/Framework/Service/ini.d.ts +29 -29
  126. package/dist/types/Framework/SocketServer/IClientWebSocket.d.ts +17 -17
  127. package/dist/types/Framework/SocketServer/IRpc.d.ts +8 -8
  128. package/dist/types/Framework/SocketServer/IRpcClientWebSocket.d.ts +16 -16
  129. package/dist/types/Framework/SocketServer/IRpcServerWebSocket.d.ts +16 -16
  130. package/dist/types/Framework/SocketServer/IServerWebSocket.d.ts +28 -28
  131. package/dist/types/Framework/SocketServer/ISocketServer.d.ts +45 -45
  132. package/dist/types/Framework/SocketServer/IWebSocket.d.ts +60 -60
  133. package/dist/types/Framework/SocketServer/ProtoFilter/GoogleProtoFilter.d.ts +9 -9
  134. package/dist/types/Framework/SocketServer/ProtoFilter/IProtoFilter.d.ts +9 -9
  135. package/dist/types/Framework/SocketServer/ProtoFilter/JsonProtoFilter.d.ts +6 -6
  136. package/dist/types/Framework/SocketServer/ProtoFilter/ProtoFactory.d.ts +8 -8
  137. package/dist/types/Framework/ThirdParty/AlipayTool.d.ts +109 -109
  138. package/dist/types/Framework/ThirdParty/Alisms.d.ts +14 -14
  139. package/dist/types/Framework/ThirdParty/AppleTool.d.ts +131 -131
  140. package/dist/types/Framework/ThirdParty/CgMq.d.ts +51 -51
  141. package/dist/types/Framework/ThirdParty/EmailTool.d.ts +5 -5
  142. package/dist/types/Framework/ThirdParty/OpenSocial.d.ts +7 -7
  143. package/dist/types/Framework/ThirdParty/QQTool.d.ts +41 -41
  144. package/dist/types/Framework/ThirdParty/QiniuTool.d.ts +6 -6
  145. package/dist/types/Framework/ThirdParty/Rpc.d.ts +20 -20
  146. package/dist/types/Framework/ThirdParty/WechatOATool.d.ts +13 -13
  147. package/dist/types/Framework/ThirdParty/WechatTool.d.ts +20 -20
  148. package/dist/types/Framework/WebServer/Controller/BaseController.d.ts +33 -33
  149. package/dist/types/Framework/WebServer/Controller/MongoBaseUserController.d.ts +27 -27
  150. package/dist/types/Framework/WebServer/Controller/MysqlBaseUserController.d.ts +27 -27
  151. package/dist/types/Framework/WebServer/Engine/ControllerManager.d.ts +35 -35
  152. package/dist/types/Framework/WebServer/Engine/Engine.d.ts +22 -21
  153. package/dist/types/Framework/WebServer/Engine/RazorJs.d.ts +21 -21
  154. package/dist/types/Framework/WebServer/Engine/Request.d.ts +36 -35
  155. package/dist/types/Framework/WebServer/Engine/Response.d.ts +30 -30
  156. package/dist/types/Framework/WebServer/IWebServer.d.ts +20 -20
  157. package/dist/types/Framework/cgserver.d.ts +27 -27
  158. package/dist/types/Framework/index.d.ts +91 -91
  159. package/dist/types/test/test.d.ts +1 -1
  160. package/package.json +1 -1
  161. package/dist/lib/Framework/WebServer/Decorator/AdminValidate.js +0 -19
  162. package/dist/lib/Framework/WebServer/Decorator/AuthorityValidate.js +0 -22
  163. package/dist/lib/Framework/WebServer/Decorator/CreatorValidate.js +0 -19
  164. package/dist/lib/Framework/WebServer/Decorator/JsonAdminValidate.js +0 -15
  165. package/dist/lib/Framework/WebServer/Decorator/JsonAuthorityValidate.js +0 -20
  166. package/dist/lib/Framework/WebServer/Decorator/JsonCreatorValidate.js +0 -19
  167. package/dist/lib/Framework/WebServer/Decorator/SyncCall.js +0 -41
  168. package/dist/lib/Framework/WebServer/Decorator/SyncCallServer.js +0 -77
  169. package/dist/types/Framework/WebServer/Decorator/AdminValidate.d.ts +0 -1
  170. package/dist/types/Framework/WebServer/Decorator/AuthorityValidate.d.ts +0 -2
  171. package/dist/types/Framework/WebServer/Decorator/CreatorValidate.d.ts +0 -1
  172. package/dist/types/Framework/WebServer/Decorator/JsonAdminValidate.d.ts +0 -1
  173. package/dist/types/Framework/WebServer/Decorator/JsonAuthorityValidate.d.ts +0 -2
  174. package/dist/types/Framework/WebServer/Decorator/JsonCreatorValidate.d.ts +0 -1
  175. package/dist/types/Framework/WebServer/Decorator/SyncCall.d.ts +0 -12
  176. package/dist/types/Framework/WebServer/Decorator/SyncCallServer.d.ts +0 -14
@@ -1,544 +1,544 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RazorJs = void 0;
4
- const Log_1 = require("./../../Logic/Log");
5
- 'use strict';
6
- var _suffix = ".cshtml";
7
- var _MATCH_HTML = /[&<>\'"]/g;
8
- var _ENCODE_HTML_RULES = {
9
- '&': '&amp;',
10
- '<': '&lt;',
11
- '>': '&gt;',
12
- '"': '&#34;',
13
- "'": '&#39;'
14
- };
15
- var encode_char = function (c) {
16
- return _ENCODE_HTML_RULES[c] || c;
17
- };
18
- var utils = {
19
- regExpChars: /[|\\{}()[\]^$+*?.]/g,
20
- escapeRegExpChars: function (string) {
21
- // istanbul ignore if
22
- if (!string) {
23
- return '';
24
- }
25
- return String(string).replace(this.regExpChars, '\\$&');
26
- },
27
- escapeFuncStr: 'var _ENCODE_HTML_RULES = {\n'
28
- + ' "&": "&amp;"\n'
29
- + ' , "<": "&lt;"\n'
30
- + ' , ">": "&gt;"\n'
31
- + ' , \'"\': "&#34;"\n'
32
- + ' , "\'": "&#39;"\n'
33
- + ' }\n'
34
- + ' , _MATCH_HTML = /[&<>\'"]/g;\n'
35
- + 'function encode_char(c) {\n'
36
- + ' return _ENCODE_HTML_RULES[c] || c;\n'
37
- + '};\n',
38
- escapeXML: function (markup) {
39
- return markup == undefined
40
- ? ''
41
- : String(markup)
42
- .replace(_MATCH_HTML, encode_char);
43
- },
44
- shallowCopy: function (to, from) {
45
- from = from || {};
46
- for (var p in from) {
47
- to[p] = from[p];
48
- }
49
- return to;
50
- },
51
- cache: {
52
- _data: {},
53
- set: function (key, val) {
54
- this._data[key] = val;
55
- },
56
- get: function (key) {
57
- return this._data[key];
58
- },
59
- reset: function () {
60
- this._data = {};
61
- }
62
- },
63
- };
64
- utils.escapeXML.toString = function () {
65
- return Function.prototype.toString.call(this) + ';\n' + this.escapeFuncStr;
66
- };
67
- var fs = require('fs');
68
- var scopeOptionWarned = false;
69
- var _DEFAULT_DELIMITER = '%';
70
- var _DEFAULT_LOCALS_NAME = 'locals';
71
- var _REGEX_STRING = '(<%%|<%=|<%-|<%_|<%#|<%|%>|-%>|_%>)';
72
- var _OPTS = ['cache', 'filename', 'delimiter', 'scope', 'context',
73
- 'debug', 'compileDebug', 'client', '_with', 'rmWhitespace',
74
- 'strict', 'localsName'
75
- ];
76
- var _TRAILING_SEMCOL = /;\s*$/;
77
- var _BOM = /^\uFEFF/;
78
- class RazorJs {
79
- _request = null;
80
- _response = null;
81
- cache = utils.cache;
82
- localsName = _DEFAULT_LOCALS_NAME;
83
- constructor() {
84
- }
85
- resolveInclude(name, filename) {
86
- var path = require('path'), dirname = path.dirname, extname = path.extname, resolve = path.resolve, includePath = resolve(dirname(filename), name), ext = extname(name);
87
- if (!ext) {
88
- includePath += _suffix;
89
- }
90
- return includePath;
91
- }
92
- _handleCache(options, template) {
93
- var fn;
94
- var path = options.filename;
95
- var hasTemplate = arguments.length > 1;
96
- if (options.cache) {
97
- if (!path) {
98
- throw new Error('cache option requires a filename');
99
- }
100
- fn = exports.cache.get(path);
101
- if (fn) {
102
- return fn;
103
- }
104
- if (!hasTemplate) {
105
- template = fs.readFileSync(path).toString().replace(_BOM, '');
106
- }
107
- }
108
- else if (!hasTemplate) {
109
- // istanbul ignore if: should not happen at all
110
- if (!path) {
111
- throw new Error('Internal Razor error: no file name or template '
112
- + 'provided');
113
- }
114
- template = fs.readFileSync(path).toString().replace(_BOM, '');
115
- }
116
- fn = this.compile(template, options);
117
- if (options.cache) {
118
- this.cache.set(path, fn);
119
- }
120
- return fn;
121
- }
122
- includeFile(path, options) {
123
- var opts = utils.shallowCopy({}, options);
124
- if (!opts.filename) {
125
- throw new Error('`include` requires the \'filename\' option.');
126
- }
127
- opts.filename = this.resolveInclude(path, opts.filename);
128
- return this._handleCache(opts);
129
- }
130
- includeSource(path, options) {
131
- var opts = utils.shallowCopy({}, options), includePath, template;
132
- includePath = path;
133
- /*
134
- if (!opts.filename)
135
- {
136
- includePath = path;
137
- }
138
- else
139
- {
140
- includePath = this.resolveInclude(path, opts.filename);
141
- }
142
- */
143
- /*解决view里面使用其他controller的action返回的分部view,,,但是该功能暂时搁置,因为按照现在的写法无法支持异步操作
144
- * 而nodejs异步操作是特点所以不去扼杀它的优势了
145
- if (includePath.lastIndexOf(".") <= 0)
146
- {
147
- engine.getEngine().getHtmlFromPath(this._request,this._response,includePath)
148
- return null
149
- }
150
- * */
151
- template = fs.readFileSync(includePath).toString().replace(_BOM, '');
152
- options.filename = includePath;
153
- opts.filename = includePath;
154
- var templ = new Template(this, template, opts);
155
- templ.generateSource();
156
- return templ.source;
157
- }
158
- _cpOptsInData(data, opts) {
159
- _OPTS.forEach(function (p) {
160
- if (typeof data[p] != 'undefined') {
161
- opts[p] = data[p];
162
- }
163
- });
164
- }
165
- compile(template, opts) {
166
- var templ;
167
- // v1 compat
168
- // 'scope' is 'context'
169
- // FIXME: Remove this in a future version
170
- if (opts && opts.scope) {
171
- if (!scopeOptionWarned) {
172
- console.warn('`scope` option is deprecated and will be removed in Razor 3');
173
- scopeOptionWarned = true;
174
- }
175
- if (!opts.context) {
176
- opts.context = opts.scope;
177
- }
178
- delete opts.scope;
179
- }
180
- templ = new Template(this, template, opts);
181
- return templ.compile();
182
- }
183
- render(req, res, tmpl_path, data, opts) {
184
- this._request = req;
185
- this._response = res;
186
- var cs_path = tmpl_path + ".cshtml";
187
- var template = fs.readFileSync(cs_path).toString();
188
- if (!template) {
189
- var h5_path = tmpl_path + ".html";
190
- var html = fs.readFileSync(h5_path).toString();
191
- if (html) {
192
- return html;
193
- }
194
- Log_1.GLog.error("no view:" + tmpl_path);
195
- return "Server Error(No View)";
196
- }
197
- data = data || {};
198
- opts = opts || {};
199
- var fn;
200
- // No options object -- if there are optiony names
201
- // in the data, copy them to options
202
- if (arguments.length == 2) {
203
- this._cpOptsInData(data, opts);
204
- }
205
- return this._handleCache(opts, template)(data);
206
- }
207
- renderFile() {
208
- var args = Array.prototype.slice.call(arguments), path = args.shift(), cb = args.pop(), data = args.shift() || {}, opts = args.pop() || {}, result;
209
- // Don't pollute passed in opts obj with new vals
210
- opts = utils.shallowCopy({}, opts);
211
- // No options object -- if there are optiony names
212
- // in the data, copy them to options
213
- if (arguments.length == 3) {
214
- // Express 4
215
- if (data.settings && data.settings['view options']) {
216
- this._cpOptsInData(data.settings['view options'], opts);
217
- }
218
- // Express 3 and lower
219
- else {
220
- this._cpOptsInData(data, opts);
221
- }
222
- }
223
- opts.filename = path;
224
- try {
225
- result = this._handleCache(opts)(data);
226
- }
227
- catch (err) {
228
- return cb(err);
229
- }
230
- return cb(null, result);
231
- }
232
- clearCache() {
233
- this.cache.reset();
234
- }
235
- }
236
- exports.RazorJs = RazorJs;
237
- var rethrow = function (err, str, filename, lineno) {
238
- var lines = str.split('\n'), start = Math.max(lineno - 3, 0), end = Math.min(lines.length, lineno + 3);
239
- // Error context
240
- var context = lines.slice(start, end).map(function (line, i) {
241
- var curr = i + start + 1;
242
- return (curr == lineno ? ' >> ' : ' ')
243
- + curr
244
- + '| '
245
- + line;
246
- }).join('\n');
247
- // Alter exception message
248
- err.path = filename;
249
- err.message = (filename || 'razor') + ':'
250
- + lineno + '\n'
251
- + context + '\n\n'
252
- + err.message;
253
- throw err;
254
- };
255
- class Template {
256
- _razorJs = null;
257
- templateText = "";
258
- mode = null;
259
- truncate = false;
260
- currentLine = 1;
261
- source = "";
262
- dependencies = [];
263
- opts = null;
264
- regex = null;
265
- modes = {
266
- EVAL: 'eval',
267
- ESCAPED: 'escaped',
268
- RAW: 'raw',
269
- COMMENT: 'comment',
270
- LITERAL: 'literal'
271
- };
272
- constructor(razorjs, text, opts) {
273
- this._razorJs = razorjs;
274
- opts = opts || {};
275
- this.templateText = text;
276
- var options = {
277
- client: opts.client || false,
278
- escapeFunction: opts.escape || utils.escapeXML,
279
- compileDebug: opts.compileDebug !== false,
280
- debug: !!opts.debug,
281
- filename: opts.filename,
282
- delimiter: opts.delimiter || exports.delimiter || _DEFAULT_DELIMITER,
283
- strict: opts.strict || false,
284
- context: opts.context,
285
- cache: opts.cache || false,
286
- rmWhitespace: opts.rmWhitespace,
287
- localsName: opts.localsName || exports.localsName || _DEFAULT_LOCALS_NAME,
288
- _with: false,
289
- };
290
- if (!options.strict) {
291
- options._with = typeof opts._with != 'undefined' ? opts._with : true;
292
- }
293
- this.opts = options;
294
- this.regex = this.createRegex();
295
- }
296
- createRegex() {
297
- var str = _REGEX_STRING, delim = utils.escapeRegExpChars(this.opts.delimiter);
298
- str = str.replace(/%/g, delim);
299
- return new RegExp(str);
300
- }
301
- compile() {
302
- var src, fn, opts = this.opts, prepended = '', appended = '', escape = opts.escapeFunction;
303
- if (opts.rmWhitespace) {
304
- // Have to use two separate replace here as `^` and `$` operators don't
305
- // work well with `\r`.
306
- this.templateText =
307
- this.templateText.replace(/\r/g, '').replace(/^\s+|\s+$/gm, '');
308
- }
309
- // Slurp spaces and tabs before <%_ and after _%>
310
- this.templateText =
311
- this.templateText.replace(/[ \t]*<%_/gm, '<%_').replace(/_%>[ \t]*/gm, '_%>');
312
- if (!this.source) {
313
- this.generateSource();
314
- prepended += ' var __output = [], __append = __output.push.bind(__output);' + '\n';
315
- if (opts._with !== false) {
316
- prepended += ' with (' + opts.localsName + ' || {}) {' + '\n';
317
- appended += ' }' + '\n';
318
- }
319
- appended += ' return __output.join("");' + '\n';
320
- this.source = prepended + this.source + appended;
321
- }
322
- if (opts.compileDebug) {
323
- src = 'var __line = 1' + '\n'
324
- + ' , __lines = ' + JSON.stringify(this.templateText) + '\n'
325
- + ' , __filename = ' + (opts.filename ?
326
- JSON.stringify(opts.filename) : 'undefined') + ';' + '\n'
327
- + 'try {' + '\n'
328
- + this.source
329
- + '} catch (e) {' + '\n'
330
- + ' rethrow(e, __lines, __filename, __line);' + '\n'
331
- + '}' + '\n';
332
- }
333
- else {
334
- src = this.source;
335
- }
336
- if (opts.debug) {
337
- console.log(src);
338
- }
339
- if (opts.client) {
340
- src = 'escape = escape || ' + escape.toString() + ';' + '\n' + src;
341
- if (opts.compileDebug) {
342
- src = 'rethrow = rethrow || ' + rethrow.toString() + ';' + '\n' + src;
343
- }
344
- }
345
- if (opts.strict) {
346
- src = '"use strict";\n' + src;
347
- }
348
- try {
349
- fn = new Function(opts.localsName + ', escape, include, rethrow', src);
350
- }
351
- catch (e) {
352
- // istanbul ignore else
353
- if (e instanceof SyntaxError) {
354
- if (opts.filename) {
355
- e.message += ' in ' + opts.filename;
356
- }
357
- e.message += ' while compiling razor';
358
- }
359
- throw e;
360
- }
361
- if (opts.client) {
362
- fn.dependencies = this.dependencies;
363
- return fn;
364
- }
365
- // Return a callable function which will execute the function
366
- // created by the source-code, with the passed data as locals
367
- // Adds a local `include` function which allows full recursive include
368
- var returnedFn = (data) => {
369
- var include = (path, includeData) => {
370
- var d = utils.shallowCopy({}, data);
371
- if (includeData) {
372
- d = utils.shallowCopy(d, includeData);
373
- }
374
- return this._razorJs.includeFile(path, opts)(d);
375
- };
376
- return fn.apply(opts.context, [data || {}, escape, include, rethrow]);
377
- };
378
- returnedFn.dependencies = this.dependencies;
379
- return returnedFn;
380
- }
381
- generateSource() {
382
- var self = this;
383
- var matches = this.parseTemplateText();
384
- var d = this.opts.delimiter;
385
- if (matches && matches.length) {
386
- matches.forEach(function (line, index) {
387
- var opening;
388
- var closing;
389
- var include;
390
- var includeOpts;
391
- var includeSrc;
392
- // If this is an opening tag, check for closing tags
393
- // FIXME: May end up with some false positives here
394
- // Better to store modes as k/v with '<' + delimiter as key
395
- // Then this can simply check against the map
396
- if (line.indexOf('<' + d) === 0 // If it is a tag
397
- && line.indexOf('<' + d + d) !== 0) { // and is not escaped
398
- closing = matches[index + 2];
399
- if (!(closing == d + '>' || closing == '-' + d + '>' || closing == '_' + d + '>')) {
400
- throw new Error('Could not find matching close tag for "' + line + '".');
401
- }
402
- }
403
- // HACK: backward-compat `include` preprocessor directives
404
- if ((include = line.match(/^\s*include\s+(\S+)/))) {
405
- opening = matches[index - 1];
406
- // Must be in EVAL or RAW mode
407
- if (opening && (opening == '<' + d || opening == '<' + d + '-' || opening == '<' + d + '_')) {
408
- includeOpts = utils.shallowCopy({}, self.opts);
409
- includeSrc = self._razorJs.includeSource(include[1], includeOpts);
410
- includeSrc = ' ; (function(){' + '\n' + includeSrc +
411
- ' ; })()' + '\n';
412
- self.source += includeSrc;
413
- self.dependencies.push(self._razorJs.resolveInclude(include[1], includeOpts.filename));
414
- return;
415
- }
416
- }
417
- self.scanLine(line);
418
- });
419
- }
420
- }
421
- parseTemplateText() {
422
- var str = this.templateText, pat = this.regex, result = pat.exec(str), arr = [], firstPos, lastPos;
423
- while (result) {
424
- firstPos = result.index;
425
- lastPos = pat.lastIndex;
426
- if (firstPos !== 0) {
427
- arr.push(str.substring(0, firstPos));
428
- str = str.slice(firstPos);
429
- }
430
- arr.push(result[0]);
431
- str = str.slice(result[0].length);
432
- result = pat.exec(str);
433
- }
434
- if (str) {
435
- arr.push(str);
436
- }
437
- return arr;
438
- }
439
- scanLine(line) {
440
- var self = this, d = this.opts.delimiter, newLineCount = 0;
441
- function _addOutput() {
442
- if (self.truncate) {
443
- // Only replace single leading linebreak in the line after
444
- // -%> tag -- this is the single, trailing linebreak
445
- // after the tag that the truncation mode replaces
446
- // Handle Win / Unix / old Mac linebreaks -- do the \r\n
447
- // combo first in the regex-or
448
- line = line.replace(/^(?:\r\n|\r|\n)/, '');
449
- self.truncate = false;
450
- }
451
- else if (self.opts.rmWhitespace) {
452
- // Gotta be more careful here.
453
- // .replace(/^(\s*)\n/, '$1') might be more appropriate here but as
454
- // rmWhitespace already removes trailing spaces anyway so meh.
455
- line = line.replace(/^\n/, '');
456
- }
457
- if (!line) {
458
- return;
459
- }
460
- // Preserve literal slashes
461
- line = line.replace(/\\/g, '\\\\');
462
- // Convert linebreaks
463
- line = line.replace(/\n/g, '\\n');
464
- line = line.replace(/\r/g, '\\r');
465
- // Escape double-quotes
466
- // - this will be the delimiter during execution
467
- line = line.replace(/"/g, '\\"');
468
- self.source += ' ; __append("' + line + '")' + '\n';
469
- }
470
- newLineCount = (line.split('\n').length - 1);
471
- switch (line) {
472
- case '<' + d:
473
- case '<' + d + '_':
474
- this.mode = this.modes.EVAL;
475
- break;
476
- case '<' + d + '=':
477
- this.mode = this.modes.ESCAPED;
478
- break;
479
- case '<' + d + '-':
480
- this.mode = this.modes.RAW;
481
- break;
482
- case '<' + d + '#':
483
- this.mode = this.modes.COMMENT;
484
- break;
485
- case '<' + d + d:
486
- this.mode = this.modes.LITERAL;
487
- this.source += ' ; __append("' + line.replace('<' + d + d, '<' + d) + '")' + '\n';
488
- break;
489
- case d + '>':
490
- case '-' + d + '>':
491
- case '_' + d + '>':
492
- if (this.mode == this.modes.LITERAL) {
493
- _addOutput();
494
- }
495
- this.mode = null;
496
- this.truncate = line.indexOf('-') === 0 || line.indexOf('_') === 0;
497
- break;
498
- default:
499
- // In script mode, depends on type of tag
500
- if (this.mode) {
501
- // If '//' is found without a line break, add a line break.
502
- switch (this.mode) {
503
- case this.modes.EVAL:
504
- case this.modes.ESCAPED:
505
- case this.modes.RAW:
506
- if (line.lastIndexOf('//') > line.lastIndexOf('\n')) {
507
- line += '\n';
508
- }
509
- }
510
- switch (this.mode) {
511
- // Just executing code
512
- case this.modes.EVAL:
513
- this.source += ' ; ' + line + '\n';
514
- break;
515
- // Exec, esc, and output
516
- case this.modes.ESCAPED:
517
- this.source += ' ; __append(escape(' +
518
- line.replace(_TRAILING_SEMCOL, '').trim() + '))' + '\n';
519
- break;
520
- // Exec and output
521
- case this.modes.RAW:
522
- this.source += ' ; __append(' +
523
- line.replace(_TRAILING_SEMCOL, '').trim() + ')' + '\n';
524
- break;
525
- case this.modes.COMMENT:
526
- // Do nothing
527
- break;
528
- // Literal <%% mode, append as raw output
529
- case this.modes.LITERAL:
530
- _addOutput();
531
- break;
532
- }
533
- }
534
- // In string mode, just add the output
535
- else {
536
- _addOutput();
537
- }
538
- }
539
- if (self.opts.compileDebug && newLineCount) {
540
- this.currentLine += newLineCount;
541
- this.source += ' ; __line = ' + this.currentLine + '\n';
542
- }
543
- }
544
- }
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RazorJs = void 0;
4
+ const Log_1 = require("./../../Logic/Log");
5
+ 'use strict';
6
+ var _suffix = ".cshtml";
7
+ var _MATCH_HTML = /[&<>\'"]/g;
8
+ var _ENCODE_HTML_RULES = {
9
+ '&': '&amp;',
10
+ '<': '&lt;',
11
+ '>': '&gt;',
12
+ '"': '&#34;',
13
+ "'": '&#39;'
14
+ };
15
+ var encode_char = function (c) {
16
+ return _ENCODE_HTML_RULES[c] || c;
17
+ };
18
+ var utils = {
19
+ regExpChars: /[|\\{}()[\]^$+*?.]/g,
20
+ escapeRegExpChars: function (string) {
21
+ // istanbul ignore if
22
+ if (!string) {
23
+ return '';
24
+ }
25
+ return String(string).replace(this.regExpChars, '\\$&');
26
+ },
27
+ escapeFuncStr: 'var _ENCODE_HTML_RULES = {\n'
28
+ + ' "&": "&amp;"\n'
29
+ + ' , "<": "&lt;"\n'
30
+ + ' , ">": "&gt;"\n'
31
+ + ' , \'"\': "&#34;"\n'
32
+ + ' , "\'": "&#39;"\n'
33
+ + ' }\n'
34
+ + ' , _MATCH_HTML = /[&<>\'"]/g;\n'
35
+ + 'function encode_char(c) {\n'
36
+ + ' return _ENCODE_HTML_RULES[c] || c;\n'
37
+ + '};\n',
38
+ escapeXML: function (markup) {
39
+ return markup == undefined
40
+ ? ''
41
+ : String(markup)
42
+ .replace(_MATCH_HTML, encode_char);
43
+ },
44
+ shallowCopy: function (to, from) {
45
+ from = from || {};
46
+ for (var p in from) {
47
+ to[p] = from[p];
48
+ }
49
+ return to;
50
+ },
51
+ cache: {
52
+ _data: {},
53
+ set: function (key, val) {
54
+ this._data[key] = val;
55
+ },
56
+ get: function (key) {
57
+ return this._data[key];
58
+ },
59
+ reset: function () {
60
+ this._data = {};
61
+ }
62
+ },
63
+ };
64
+ utils.escapeXML.toString = function () {
65
+ return Function.prototype.toString.call(this) + ';\n' + this.escapeFuncStr;
66
+ };
67
+ var fs = require('fs');
68
+ var scopeOptionWarned = false;
69
+ var _DEFAULT_DELIMITER = '%';
70
+ var _DEFAULT_LOCALS_NAME = 'locals';
71
+ var _REGEX_STRING = '(<%%|<%=|<%-|<%_|<%#|<%|%>|-%>|_%>)';
72
+ var _OPTS = ['cache', 'filename', 'delimiter', 'scope', 'context',
73
+ 'debug', 'compileDebug', 'client', '_with', 'rmWhitespace',
74
+ 'strict', 'localsName'
75
+ ];
76
+ var _TRAILING_SEMCOL = /;\s*$/;
77
+ var _BOM = /^\uFEFF/;
78
+ class RazorJs {
79
+ _request = null;
80
+ _response = null;
81
+ cache = utils.cache;
82
+ localsName = _DEFAULT_LOCALS_NAME;
83
+ constructor() {
84
+ }
85
+ resolveInclude(name, filename) {
86
+ var path = require('path'), dirname = path.dirname, extname = path.extname, resolve = path.resolve, includePath = resolve(dirname(filename), name), ext = extname(name);
87
+ if (!ext) {
88
+ includePath += _suffix;
89
+ }
90
+ return includePath;
91
+ }
92
+ _handleCache(options, template) {
93
+ var fn;
94
+ var path = options.filename;
95
+ var hasTemplate = arguments.length > 1;
96
+ if (options.cache) {
97
+ if (!path) {
98
+ throw new Error('cache option requires a filename');
99
+ }
100
+ fn = exports.cache.get(path);
101
+ if (fn) {
102
+ return fn;
103
+ }
104
+ if (!hasTemplate) {
105
+ template = fs.readFileSync(path).toString().replace(_BOM, '');
106
+ }
107
+ }
108
+ else if (!hasTemplate) {
109
+ // istanbul ignore if: should not happen at all
110
+ if (!path) {
111
+ throw new Error('Internal Razor error: no file name or template '
112
+ + 'provided');
113
+ }
114
+ template = fs.readFileSync(path).toString().replace(_BOM, '');
115
+ }
116
+ fn = this.compile(template, options);
117
+ if (options.cache) {
118
+ this.cache.set(path, fn);
119
+ }
120
+ return fn;
121
+ }
122
+ includeFile(path, options) {
123
+ var opts = utils.shallowCopy({}, options);
124
+ if (!opts.filename) {
125
+ throw new Error('`include` requires the \'filename\' option.');
126
+ }
127
+ opts.filename = this.resolveInclude(path, opts.filename);
128
+ return this._handleCache(opts);
129
+ }
130
+ includeSource(path, options) {
131
+ var opts = utils.shallowCopy({}, options), includePath, template;
132
+ includePath = path;
133
+ /*
134
+ if (!opts.filename)
135
+ {
136
+ includePath = path;
137
+ }
138
+ else
139
+ {
140
+ includePath = this.resolveInclude(path, opts.filename);
141
+ }
142
+ */
143
+ /*解决view里面使用其他controller的action返回的分部view,,,但是该功能暂时搁置,因为按照现在的写法无法支持异步操作
144
+ * 而nodejs异步操作是特点所以不去扼杀它的优势了
145
+ if (includePath.lastIndexOf(".") <= 0)
146
+ {
147
+ engine.getEngine().getHtmlFromPath(this._request,this._response,includePath)
148
+ return null
149
+ }
150
+ * */
151
+ template = fs.readFileSync(includePath).toString().replace(_BOM, '');
152
+ options.filename = includePath;
153
+ opts.filename = includePath;
154
+ var templ = new Template(this, template, opts);
155
+ templ.generateSource();
156
+ return templ.source;
157
+ }
158
+ _cpOptsInData(data, opts) {
159
+ _OPTS.forEach(function (p) {
160
+ if (typeof data[p] != 'undefined') {
161
+ opts[p] = data[p];
162
+ }
163
+ });
164
+ }
165
+ compile(template, opts) {
166
+ var templ;
167
+ // v1 compat
168
+ // 'scope' is 'context'
169
+ // FIXME: Remove this in a future version
170
+ if (opts && opts.scope) {
171
+ if (!scopeOptionWarned) {
172
+ console.warn('`scope` option is deprecated and will be removed in Razor 3');
173
+ scopeOptionWarned = true;
174
+ }
175
+ if (!opts.context) {
176
+ opts.context = opts.scope;
177
+ }
178
+ delete opts.scope;
179
+ }
180
+ templ = new Template(this, template, opts);
181
+ return templ.compile();
182
+ }
183
+ render(req, res, tmpl_path, data, opts) {
184
+ this._request = req;
185
+ this._response = res;
186
+ var cs_path = tmpl_path + ".cshtml";
187
+ var template = fs.readFileSync(cs_path).toString();
188
+ if (!template) {
189
+ var h5_path = tmpl_path + ".html";
190
+ var html = fs.readFileSync(h5_path).toString();
191
+ if (html) {
192
+ return html;
193
+ }
194
+ Log_1.GLog.error("no view:" + tmpl_path);
195
+ return "Server Error(No View)";
196
+ }
197
+ data = data || {};
198
+ opts = opts || {};
199
+ var fn;
200
+ // No options object -- if there are optiony names
201
+ // in the data, copy them to options
202
+ if (arguments.length == 2) {
203
+ this._cpOptsInData(data, opts);
204
+ }
205
+ return this._handleCache(opts, template)(data);
206
+ }
207
+ renderFile() {
208
+ var args = Array.prototype.slice.call(arguments), path = args.shift(), cb = args.pop(), data = args.shift() || {}, opts = args.pop() || {}, result;
209
+ // Don't pollute passed in opts obj with new vals
210
+ opts = utils.shallowCopy({}, opts);
211
+ // No options object -- if there are optiony names
212
+ // in the data, copy them to options
213
+ if (arguments.length == 3) {
214
+ // Express 4
215
+ if (data.settings && data.settings['view options']) {
216
+ this._cpOptsInData(data.settings['view options'], opts);
217
+ }
218
+ // Express 3 and lower
219
+ else {
220
+ this._cpOptsInData(data, opts);
221
+ }
222
+ }
223
+ opts.filename = path;
224
+ try {
225
+ result = this._handleCache(opts)(data);
226
+ }
227
+ catch (err) {
228
+ return cb(err);
229
+ }
230
+ return cb(null, result);
231
+ }
232
+ clearCache() {
233
+ this.cache.reset();
234
+ }
235
+ }
236
+ exports.RazorJs = RazorJs;
237
+ var rethrow = function (err, str, filename, lineno) {
238
+ var lines = str.split('\n'), start = Math.max(lineno - 3, 0), end = Math.min(lines.length, lineno + 3);
239
+ // Error context
240
+ var context = lines.slice(start, end).map(function (line, i) {
241
+ var curr = i + start + 1;
242
+ return (curr == lineno ? ' >> ' : ' ')
243
+ + curr
244
+ + '| '
245
+ + line;
246
+ }).join('\n');
247
+ // Alter exception message
248
+ err.path = filename;
249
+ err.message = (filename || 'razor') + ':'
250
+ + lineno + '\n'
251
+ + context + '\n\n'
252
+ + err.message;
253
+ throw err;
254
+ };
255
+ class Template {
256
+ _razorJs = null;
257
+ templateText = "";
258
+ mode = null;
259
+ truncate = false;
260
+ currentLine = 1;
261
+ source = "";
262
+ dependencies = [];
263
+ opts = null;
264
+ regex = null;
265
+ modes = {
266
+ EVAL: 'eval',
267
+ ESCAPED: 'escaped',
268
+ RAW: 'raw',
269
+ COMMENT: 'comment',
270
+ LITERAL: 'literal'
271
+ };
272
+ constructor(razorjs, text, opts) {
273
+ this._razorJs = razorjs;
274
+ opts = opts || {};
275
+ this.templateText = text;
276
+ var options = {
277
+ client: opts.client || false,
278
+ escapeFunction: opts.escape || utils.escapeXML,
279
+ compileDebug: opts.compileDebug !== false,
280
+ debug: !!opts.debug,
281
+ filename: opts.filename,
282
+ delimiter: opts.delimiter || exports.delimiter || _DEFAULT_DELIMITER,
283
+ strict: opts.strict || false,
284
+ context: opts.context,
285
+ cache: opts.cache || false,
286
+ rmWhitespace: opts.rmWhitespace,
287
+ localsName: opts.localsName || exports.localsName || _DEFAULT_LOCALS_NAME,
288
+ _with: false,
289
+ };
290
+ if (!options.strict) {
291
+ options._with = typeof opts._with != 'undefined' ? opts._with : true;
292
+ }
293
+ this.opts = options;
294
+ this.regex = this.createRegex();
295
+ }
296
+ createRegex() {
297
+ var str = _REGEX_STRING, delim = utils.escapeRegExpChars(this.opts.delimiter);
298
+ str = str.replace(/%/g, delim);
299
+ return new RegExp(str);
300
+ }
301
+ compile() {
302
+ var src, fn, opts = this.opts, prepended = '', appended = '', escape = opts.escapeFunction;
303
+ if (opts.rmWhitespace) {
304
+ // Have to use two separate replace here as `^` and `$` operators don't
305
+ // work well with `\r`.
306
+ this.templateText =
307
+ this.templateText.replace(/\r/g, '').replace(/^\s+|\s+$/gm, '');
308
+ }
309
+ // Slurp spaces and tabs before <%_ and after _%>
310
+ this.templateText =
311
+ this.templateText.replace(/[ \t]*<%_/gm, '<%_').replace(/_%>[ \t]*/gm, '_%>');
312
+ if (!this.source) {
313
+ this.generateSource();
314
+ prepended += ' var __output = [], __append = __output.push.bind(__output);' + '\n';
315
+ if (opts._with !== false) {
316
+ prepended += ' with (' + opts.localsName + ' || {}) {' + '\n';
317
+ appended += ' }' + '\n';
318
+ }
319
+ appended += ' return __output.join("");' + '\n';
320
+ this.source = prepended + this.source + appended;
321
+ }
322
+ if (opts.compileDebug) {
323
+ src = 'var __line = 1' + '\n'
324
+ + ' , __lines = ' + JSON.stringify(this.templateText) + '\n'
325
+ + ' , __filename = ' + (opts.filename ?
326
+ JSON.stringify(opts.filename) : 'undefined') + ';' + '\n'
327
+ + 'try {' + '\n'
328
+ + this.source
329
+ + '} catch (e) {' + '\n'
330
+ + ' rethrow(e, __lines, __filename, __line);' + '\n'
331
+ + '}' + '\n';
332
+ }
333
+ else {
334
+ src = this.source;
335
+ }
336
+ if (opts.debug) {
337
+ console.log(src);
338
+ }
339
+ if (opts.client) {
340
+ src = 'escape = escape || ' + escape.toString() + ';' + '\n' + src;
341
+ if (opts.compileDebug) {
342
+ src = 'rethrow = rethrow || ' + rethrow.toString() + ';' + '\n' + src;
343
+ }
344
+ }
345
+ if (opts.strict) {
346
+ src = '"use strict";\n' + src;
347
+ }
348
+ try {
349
+ fn = new Function(opts.localsName + ', escape, include, rethrow', src);
350
+ }
351
+ catch (e) {
352
+ // istanbul ignore else
353
+ if (e instanceof SyntaxError) {
354
+ if (opts.filename) {
355
+ e.message += ' in ' + opts.filename;
356
+ }
357
+ e.message += ' while compiling razor';
358
+ }
359
+ throw e;
360
+ }
361
+ if (opts.client) {
362
+ fn.dependencies = this.dependencies;
363
+ return fn;
364
+ }
365
+ // Return a callable function which will execute the function
366
+ // created by the source-code, with the passed data as locals
367
+ // Adds a local `include` function which allows full recursive include
368
+ var returnedFn = (data) => {
369
+ var include = (path, includeData) => {
370
+ var d = utils.shallowCopy({}, data);
371
+ if (includeData) {
372
+ d = utils.shallowCopy(d, includeData);
373
+ }
374
+ return this._razorJs.includeFile(path, opts)(d);
375
+ };
376
+ return fn.apply(opts.context, [data || {}, escape, include, rethrow]);
377
+ };
378
+ returnedFn.dependencies = this.dependencies;
379
+ return returnedFn;
380
+ }
381
+ generateSource() {
382
+ var self = this;
383
+ var matches = this.parseTemplateText();
384
+ var d = this.opts.delimiter;
385
+ if (matches && matches.length) {
386
+ matches.forEach(function (line, index) {
387
+ var opening;
388
+ var closing;
389
+ var include;
390
+ var includeOpts;
391
+ var includeSrc;
392
+ // If this is an opening tag, check for closing tags
393
+ // FIXME: May end up with some false positives here
394
+ // Better to store modes as k/v with '<' + delimiter as key
395
+ // Then this can simply check against the map
396
+ if (line.indexOf('<' + d) === 0 // If it is a tag
397
+ && line.indexOf('<' + d + d) !== 0) { // and is not escaped
398
+ closing = matches[index + 2];
399
+ if (!(closing == d + '>' || closing == '-' + d + '>' || closing == '_' + d + '>')) {
400
+ throw new Error('Could not find matching close tag for "' + line + '".');
401
+ }
402
+ }
403
+ // HACK: backward-compat `include` preprocessor directives
404
+ if ((include = line.match(/^\s*include\s+(\S+)/))) {
405
+ opening = matches[index - 1];
406
+ // Must be in EVAL or RAW mode
407
+ if (opening && (opening == '<' + d || opening == '<' + d + '-' || opening == '<' + d + '_')) {
408
+ includeOpts = utils.shallowCopy({}, self.opts);
409
+ includeSrc = self._razorJs.includeSource(include[1], includeOpts);
410
+ includeSrc = ' ; (function(){' + '\n' + includeSrc +
411
+ ' ; })()' + '\n';
412
+ self.source += includeSrc;
413
+ self.dependencies.push(self._razorJs.resolveInclude(include[1], includeOpts.filename));
414
+ return;
415
+ }
416
+ }
417
+ self.scanLine(line);
418
+ });
419
+ }
420
+ }
421
+ parseTemplateText() {
422
+ var str = this.templateText, pat = this.regex, result = pat.exec(str), arr = [], firstPos, lastPos;
423
+ while (result) {
424
+ firstPos = result.index;
425
+ lastPos = pat.lastIndex;
426
+ if (firstPos !== 0) {
427
+ arr.push(str.substring(0, firstPos));
428
+ str = str.slice(firstPos);
429
+ }
430
+ arr.push(result[0]);
431
+ str = str.slice(result[0].length);
432
+ result = pat.exec(str);
433
+ }
434
+ if (str) {
435
+ arr.push(str);
436
+ }
437
+ return arr;
438
+ }
439
+ scanLine(line) {
440
+ var self = this, d = this.opts.delimiter, newLineCount = 0;
441
+ function _addOutput() {
442
+ if (self.truncate) {
443
+ // Only replace single leading linebreak in the line after
444
+ // -%> tag -- this is the single, trailing linebreak
445
+ // after the tag that the truncation mode replaces
446
+ // Handle Win / Unix / old Mac linebreaks -- do the \r\n
447
+ // combo first in the regex-or
448
+ line = line.replace(/^(?:\r\n|\r|\n)/, '');
449
+ self.truncate = false;
450
+ }
451
+ else if (self.opts.rmWhitespace) {
452
+ // Gotta be more careful here.
453
+ // .replace(/^(\s*)\n/, '$1') might be more appropriate here but as
454
+ // rmWhitespace already removes trailing spaces anyway so meh.
455
+ line = line.replace(/^\n/, '');
456
+ }
457
+ if (!line) {
458
+ return;
459
+ }
460
+ // Preserve literal slashes
461
+ line = line.replace(/\\/g, '\\\\');
462
+ // Convert linebreaks
463
+ line = line.replace(/\n/g, '\\n');
464
+ line = line.replace(/\r/g, '\\r');
465
+ // Escape double-quotes
466
+ // - this will be the delimiter during execution
467
+ line = line.replace(/"/g, '\\"');
468
+ self.source += ' ; __append("' + line + '")' + '\n';
469
+ }
470
+ newLineCount = (line.split('\n').length - 1);
471
+ switch (line) {
472
+ case '<' + d:
473
+ case '<' + d + '_':
474
+ this.mode = this.modes.EVAL;
475
+ break;
476
+ case '<' + d + '=':
477
+ this.mode = this.modes.ESCAPED;
478
+ break;
479
+ case '<' + d + '-':
480
+ this.mode = this.modes.RAW;
481
+ break;
482
+ case '<' + d + '#':
483
+ this.mode = this.modes.COMMENT;
484
+ break;
485
+ case '<' + d + d:
486
+ this.mode = this.modes.LITERAL;
487
+ this.source += ' ; __append("' + line.replace('<' + d + d, '<' + d) + '")' + '\n';
488
+ break;
489
+ case d + '>':
490
+ case '-' + d + '>':
491
+ case '_' + d + '>':
492
+ if (this.mode == this.modes.LITERAL) {
493
+ _addOutput();
494
+ }
495
+ this.mode = null;
496
+ this.truncate = line.indexOf('-') === 0 || line.indexOf('_') === 0;
497
+ break;
498
+ default:
499
+ // In script mode, depends on type of tag
500
+ if (this.mode) {
501
+ // If '//' is found without a line break, add a line break.
502
+ switch (this.mode) {
503
+ case this.modes.EVAL:
504
+ case this.modes.ESCAPED:
505
+ case this.modes.RAW:
506
+ if (line.lastIndexOf('//') > line.lastIndexOf('\n')) {
507
+ line += '\n';
508
+ }
509
+ }
510
+ switch (this.mode) {
511
+ // Just executing code
512
+ case this.modes.EVAL:
513
+ this.source += ' ; ' + line + '\n';
514
+ break;
515
+ // Exec, esc, and output
516
+ case this.modes.ESCAPED:
517
+ this.source += ' ; __append(escape(' +
518
+ line.replace(_TRAILING_SEMCOL, '').trim() + '))' + '\n';
519
+ break;
520
+ // Exec and output
521
+ case this.modes.RAW:
522
+ this.source += ' ; __append(' +
523
+ line.replace(_TRAILING_SEMCOL, '').trim() + ')' + '\n';
524
+ break;
525
+ case this.modes.COMMENT:
526
+ // Do nothing
527
+ break;
528
+ // Literal <%% mode, append as raw output
529
+ case this.modes.LITERAL:
530
+ _addOutput();
531
+ break;
532
+ }
533
+ }
534
+ // In string mode, just add the output
535
+ else {
536
+ _addOutput();
537
+ }
538
+ }
539
+ if (self.opts.compileDebug && newLineCount) {
540
+ this.currentLine += newLineCount;
541
+ this.source += ' ; __line = ' + this.currentLine + '\n';
542
+ }
543
+ }
544
+ }