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