@celljs/mvc 4.1.0 → 4.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.
Files changed (42) hide show
  1. package/README.md +13 -1
  2. package/esm/node/module.d.ts +1 -1
  3. package/esm/node/view/file-view.js +4 -1
  4. package/esm/node/view/file-view.js.map +1 -1
  5. package/esm/node/view/html-view.d.ts +8 -1
  6. package/esm/node/view/html-view.js +12 -5
  7. package/esm/node/view/html-view.js.map +1 -1
  8. package/esm/node/view/index.d.ts +1 -0
  9. package/esm/node/view/index.js +1 -0
  10. package/esm/node/view/index.js.map +1 -1
  11. package/esm/node/view/template.d.ts +33 -0
  12. package/esm/node/view/template.js +161 -0
  13. package/esm/node/view/template.js.map +1 -0
  14. package/lib/node/module.d.ts +1 -1
  15. package/lib/node/mvc-http.spec.js +7 -15
  16. package/lib/node/mvc-http.spec.js.map +1 -1
  17. package/lib/node/view/file-view.d.ts.map +1 -1
  18. package/lib/node/view/file-view.js +4 -1
  19. package/lib/node/view/file-view.js.map +1 -1
  20. package/lib/node/view/html-view.d.ts +8 -1
  21. package/lib/node/view/html-view.d.ts.map +1 -1
  22. package/lib/node/view/html-view.js +12 -8
  23. package/lib/node/view/html-view.js.map +1 -1
  24. package/lib/node/view/index.d.ts +1 -0
  25. package/lib/node/view/index.d.ts.map +1 -1
  26. package/lib/node/view/index.js +1 -0
  27. package/lib/node/view/index.js.map +1 -1
  28. package/lib/node/view/template.d.ts +34 -0
  29. package/lib/node/view/template.d.ts.map +1 -0
  30. package/lib/node/view/template.js +165 -0
  31. package/lib/node/view/template.js.map +1 -0
  32. package/lib/node/view/template.spec.d.ts +2 -0
  33. package/lib/node/view/template.spec.d.ts.map +1 -0
  34. package/lib/node/view/template.spec.js +35 -0
  35. package/lib/node/view/template.spec.js.map +1 -0
  36. package/package.json +7 -8
  37. package/src/node/mvc-http.spec.ts +9 -16
  38. package/src/node/view/file-view.ts +4 -1
  39. package/src/node/view/html-view.ts +14 -6
  40. package/src/node/view/index.ts +2 -1
  41. package/src/node/view/template.spec.ts +34 -0
  42. package/src/node/view/template.ts +170 -0
@@ -1 +1 @@
1
- {"version":3,"file":"html-view.js","sourceRoot":"","sources":["../../../src/node/view/html-view.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,uCAAgD;AAChD,mDAAuC;AACvC,+CAA+C;AAC/C,wDAAgC;AAChC,+BAA4B;AAC5B,+CAA4C;AAE5C,6CAAoD;AACpD,uCAAyC;AAGlC,IAAM,QAAQ,GAAd,MAAM,QAAQ;IAAd;QAEM,gBAAW,GAAG,gBAAS,CAAC,SAAS,CAAC;QAElC,aAAQ,GAAG,GAAG,CAAC;IAkB5B,CAAC;IAbG,KAAK,CAAC,MAAM,CAAC,KAAU,EAAE,EAAE,QAAQ,EAAgB;QAC/C,MAAM,QAAQ,GAAG,cAAO,CAAC,UAAU,EAAE,CAAC,QAAQ,CAAC;QAC/C,MAAM,EAAE,WAAW,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC;QAC5C,MAAM,QAAQ,GAAG,MAAM,IAAA,mBAAQ,EAAC,IAAA,WAAI,EAAC,SAAS,EAAE,WAAW,EAAE,QAAS,CAAC,IAAI,CAAC,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC;QACpG,IAAI,KAAK,EAAE,CAAC;YACR,kBAAQ,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QAC7B,CAAC;QACD,QAAQ,CAAC,IAAI,GAAG,kBAAQ,CAAC,MAAM,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IACrD,CAAC;IAED,OAAO,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAgB;;QACxC,OAAO,OAAO,CAAC,OAAO,CAAC,QAAQ,KAAK,qBAAc,KAAI,MAAA,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAE,IAAI,0CAAE,QAAQ,CAAC,WAAW,CAAC,CAAA,CAAC,CAAC;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C;QACL,CAAC;QACD,QAAQ,CAAC,IAAI,GAAG,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC3C,CAAC;IAED,OAAO,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAgB;;QACxC,OAAO,OAAO,CAAC,OAAO,CAAC,QAAQ,KAAK,qBAAc,KAAI,MAAA,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAE,IAAI,0CAAE,QAAQ,CAAC,WAAW,CAAC,CAAA,CAAC,CAAC;IACjG,CAAC;CACJ,CAAA;;AAtBsB;IADlB,IAAA,YAAK,EAAC,eAAe,CAAC;;yCAC8C;mBAP5D,QAAQ;IADpB,IAAA,gBAAS,EAAC,oBAAI,CAAC;GACH,QAAQ,CA6BpB"}
@@ -2,6 +2,7 @@ export * from './json-view';
2
2
  export * from './text-view';
3
3
  export * from './view-provider';
4
4
  export * from './html-view';
5
+ export * from './template';
5
6
  export * from './file-view';
6
7
  export * from './json-view-template-renderer';
7
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/node/view/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,+BAA+B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/node/view/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,+BAA+B,CAAC"}
@@ -18,6 +18,7 @@ __exportStar(require("./json-view"), exports);
18
18
  __exportStar(require("./text-view"), exports);
19
19
  __exportStar(require("./view-provider"), exports);
20
20
  __exportStar(require("./html-view"), exports);
21
+ __exportStar(require("./template"), exports);
21
22
  __exportStar(require("./file-view"), exports);
22
23
  __exportStar(require("./json-view-template-renderer"), exports);
23
24
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/node/view/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,8CAA4B;AAC5B,8CAA4B;AAC5B,kDAAgC;AAChC,8CAA4B;AAC5B,8CAA4B;AAC5B,gEAA8C"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/node/view/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,8CAA4B;AAC5B,8CAA4B;AAC5B,kDAAgC;AAChC,8CAA4B;AAC5B,6CAA2B;AAC3B,8CAA4B;AAC5B,gEAA8C"}
@@ -0,0 +1,34 @@
1
+ /** `path` is the name split at dots; empty for `.`. */
2
+ type Token = {
3
+ type: 'text';
4
+ value: string;
5
+ } | {
6
+ type: 'name' | '&';
7
+ path: string[];
8
+ } | {
9
+ type: '#' | '^';
10
+ path: string[];
11
+ children: Token[];
12
+ };
13
+ /**
14
+ * A logic-less template with the syntax of Mustache (the subset Cell supports):
15
+ *
16
+ * - `{{name}}` writes a value HTML-escaped, `{{{name}}}` and `{{& name}}` as it is; `name` can be a path (`user.name`)
17
+ * or `.` for the current item; a missing value writes nothing;
18
+ * - `{{#name}}...{{/name}}` repeats for each item of a list, enters an object, or shows when the value is truthy;
19
+ * - `{{^name}}...{{/name}}` shows when the value is falsy or an empty list;
20
+ * - `{{! comment }}` writes nothing.
21
+ *
22
+ * Partials (`{{> name}}`) and other delimiters (`{{=<% %>=}}`) are not supported and fail when the template is compiled.
23
+ */
24
+ export declare class Template {
25
+ protected readonly source: string;
26
+ protected readonly tokens: Token[];
27
+ constructor(source: string);
28
+ render(model: unknown): string;
29
+ protected parse(source: string): Token[];
30
+ protected lookup(path: string[], contexts: unknown[]): unknown;
31
+ protected renderTokens(tokens: Token[], contexts: unknown[]): string;
32
+ }
33
+ export {};
34
+ //# sourceMappingURL=template.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"template.d.ts","sourceRoot":"","sources":["../../../src/node/view/template.ts"],"names":[],"mappings":"AAUA,uDAAuD;AACvD,KAAK,KAAK,GACJ;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAC/B;IAAE,IAAI,EAAE,MAAM,GAAG,GAAG,CAAC;IAAC,IAAI,EAAE,MAAM,EAAE,CAAA;CAAE,GACtC;IAAE,IAAI,EAAE,GAAG,GAAG,GAAG,CAAC;IAAC,IAAI,EAAE,MAAM,EAAE,CAAC;IAAC,QAAQ,EAAE,KAAK,EAAE,CAAA;CAAE,CAAC;AAS7D;;;;;;;;;;GAUG;AACH,qBAAa,QAAQ;IAIL,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM;IAF7C,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,KAAK,EAAE,CAAC;IAEnC,YAA+B,MAAM,EAAE,MAAM,EAE5C;IAED,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAE7B;IAEG,SAAS,CAAC,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,KAAK,EAAE,CAqE3C;IAED,SAAS,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,OAAO,CAgB7D;IAED,SAAS,CAAC,YAAY,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,MAAM,CAiCnE;CACJ"}
@@ -0,0 +1,165 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Template = void 0;
4
+ const ESCAPES = {
5
+ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', '\'': '&#39;', '/': '&#x2F;', '`': '&#x60;', '=': '&#x3D;'
6
+ };
7
+ const UNSAFE = /[&<>"'`=/]/;
8
+ function escapeHtml(text) {
9
+ return UNSAFE.test(text) ? text.replace(/[&<>"'`=/]/g, c => ESCAPES[c]) : text;
10
+ }
11
+ function toPath(name) {
12
+ return name === '.' ? [] : name.split('.');
13
+ }
14
+ /** Section, inverted section, end of section and comment tags alone on a line take the line with them. */
15
+ const STANDALONE = new Set(['#', '^', '/', '!']);
16
+ /**
17
+ * A logic-less template with the syntax of Mustache (the subset Cell supports):
18
+ *
19
+ * - `{{name}}` writes a value HTML-escaped, `{{{name}}}` and `{{& name}}` as it is; `name` can be a path (`user.name`)
20
+ * or `.` for the current item; a missing value writes nothing;
21
+ * - `{{#name}}...{{/name}}` repeats for each item of a list, enters an object, or shows when the value is truthy;
22
+ * - `{{^name}}...{{/name}}` shows when the value is falsy or an empty list;
23
+ * - `{{! comment }}` writes nothing.
24
+ *
25
+ * Partials (`{{> name}}`) and other delimiters (`{{=<% %>=}}`) are not supported and fail when the template is compiled.
26
+ */
27
+ class Template {
28
+ constructor(source) {
29
+ this.source = source;
30
+ this.tokens = this.parse(source);
31
+ }
32
+ render(model) {
33
+ return this.renderTokens(this.tokens, [model]);
34
+ }
35
+ parse(source) {
36
+ const root = [];
37
+ const stack = [];
38
+ let current = root;
39
+ let i = 0;
40
+ while (i < source.length) {
41
+ const open = source.indexOf('{{', i);
42
+ if (open < 0) {
43
+ current.push({ type: 'text', value: source.slice(i) });
44
+ break;
45
+ }
46
+ let text = source.slice(i, open);
47
+ let type;
48
+ let name;
49
+ let end;
50
+ if (source[open + 2] === '{') {
51
+ const close = source.indexOf('}}}', open + 3);
52
+ if (close < 0) {
53
+ throw new Error(`Unclosed tag at ${open}`);
54
+ }
55
+ type = '&';
56
+ name = source.slice(open + 3, close).trim();
57
+ end = close + 3;
58
+ }
59
+ else {
60
+ const close = source.indexOf('}}', open + 2);
61
+ if (close < 0) {
62
+ throw new Error(`Unclosed tag at ${open}`);
63
+ }
64
+ const content = source.slice(open + 2, close).trim();
65
+ type = '#^/!&>='.includes(content[0]) ? content[0] : 'name';
66
+ name = type === 'name' ? content : content.slice(1).trim();
67
+ end = close + 2;
68
+ }
69
+ if (type === '>' || type === '=') {
70
+ throw new Error(`${type === '>' ? 'Partials' : 'Custom delimiters'} are not supported: ${source.slice(open, end)}`);
71
+ }
72
+ if (STANDALONE.has(type)) {
73
+ const lineStart = source.lastIndexOf('\n', open - 1) + 1;
74
+ const lineEnd = source.indexOf('\n', end);
75
+ const before = source.slice(Math.max(lineStart, i), open);
76
+ const after = source.slice(end, lineEnd < 0 ? source.length : lineEnd);
77
+ if (lineStart >= i && /^[ \t]*$/.test(before) && /^[ \t]*\r?$/.test(after)) {
78
+ text = text.slice(0, text.length - before.length);
79
+ end = lineEnd < 0 ? source.length : lineEnd + 1;
80
+ }
81
+ }
82
+ if (text) {
83
+ current.push({ type: 'text', value: text });
84
+ }
85
+ if (type === '#' || type === '^') {
86
+ const section = { type, path: toPath(name), children: [] };
87
+ current.push(section);
88
+ stack.push({ name, children: current });
89
+ current = section.children;
90
+ }
91
+ else if (type === '/') {
92
+ const section = stack.pop();
93
+ if (!section || section.name !== name) {
94
+ throw new Error(`Unopened section "${name}" at ${open}`);
95
+ }
96
+ current = section.children;
97
+ }
98
+ else if (type !== '!') {
99
+ current.push({ type: type, path: toPath(name) });
100
+ }
101
+ i = end;
102
+ }
103
+ if (stack.length) {
104
+ throw new Error(`Unclosed section "${stack[stack.length - 1].name}"`);
105
+ }
106
+ return root;
107
+ }
108
+ lookup(path, contexts) {
109
+ if (path.length === 0) {
110
+ return contexts[contexts.length - 1];
111
+ }
112
+ const first = path[0];
113
+ for (let i = contexts.length - 1; i >= 0; i--) {
114
+ const context = contexts[i];
115
+ if (context !== null && typeof context === 'object' && first in context) {
116
+ let value = context[first];
117
+ for (let j = 1; j < path.length; j++) {
118
+ value = value === null || value === undefined ? undefined : value[path[j]];
119
+ }
120
+ return typeof value === 'function' ? value.call(context) : value;
121
+ }
122
+ }
123
+ return undefined;
124
+ }
125
+ renderTokens(tokens, contexts) {
126
+ let out = '';
127
+ for (const token of tokens) {
128
+ if (token.type === 'text') {
129
+ out += token.value;
130
+ continue;
131
+ }
132
+ const value = this.lookup(token.path, contexts);
133
+ if (token.type === '#') {
134
+ if (Array.isArray(value)) {
135
+ for (const item of value) {
136
+ contexts.push(item);
137
+ out += this.renderTokens(token.children, contexts);
138
+ contexts.pop();
139
+ }
140
+ }
141
+ else if (typeof value === 'object' || typeof value === 'string' || typeof value === 'number') {
142
+ if (value) {
143
+ contexts.push(value);
144
+ out += this.renderTokens(token.children, contexts);
145
+ contexts.pop();
146
+ }
147
+ }
148
+ else if (value) {
149
+ out += this.renderTokens(token.children, contexts);
150
+ }
151
+ }
152
+ else if (token.type === '^') {
153
+ if (!value || (Array.isArray(value) && value.length === 0)) {
154
+ out += this.renderTokens(token.children, contexts);
155
+ }
156
+ }
157
+ else if (value !== null && value !== undefined) {
158
+ out += token.type === '&' ? String(value) : escapeHtml(String(value));
159
+ }
160
+ }
161
+ return out;
162
+ }
163
+ }
164
+ exports.Template = Template;
165
+ //# sourceMappingURL=template.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"template.js","sourceRoot":"","sources":["../../../src/node/view/template.ts"],"names":[],"mappings":";;;AAAA,MAAM,OAAO,GAA2B;IACpC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,QAAQ;CACpH,CAAC;AAEF,MAAM,MAAM,GAAG,YAAY,CAAC;AAE5B,SAAS,UAAU,CAAC,IAAY;IAC5B,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,aAAa,EAAE,CAAC,CAAC,EAAE,CAAC,OAAO,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AAK,CAAC,IAAI,KAAK,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;YAC1E,CAAC;QACL,CAAC;QACD,OAAO,GAAG,CAAC;IACf,CAAC;CACJ"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=template.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"template.spec.d.ts","sourceRoot":"","sources":["../../../src/node/view/template.spec.ts"],"names":[],"mappings":""}
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const bun_test_1 = require("bun:test");
4
+ const chai_1 = require("chai");
5
+ const template_1 = require("./template");
6
+ (0, bun_test_1.describe)('Template', () => {
7
+ // What mustache 4.2.0 rendered.
8
+ const cases = [
9
+ ['Hi {{name}}!', { name: '<b>&"\'/`=' }, 'Hi &lt;b&gt;&amp;&quot;&#39;&#x2F;&#x60;&#x3D;!'],
10
+ ['{{{raw}}}{{&raw}}', { raw: '<i>' }, '<i><i>'],
11
+ ['{{#list}}[{{.}}]{{/list}}', { list: [1, 2] }, '[1][2]'],
12
+ ['{{#user}}{{name}}{{/user}}{{^user}}none{{/user}}', { user: { name: 'u' } }, 'u'],
13
+ ['{{^items}}empty{{/items}}', { items: [] }, 'empty'],
14
+ ['{{a.b.c}}|{{missing}}|{{n}}', { a: { b: { c: 3 } }, n: 0 }, '3||0'],
15
+ ['<ul>\n {{#xs}}\n <li>{{name}}</li>\n {{/xs}}\n</ul>\n', { xs: [{ name: 'a' }, { name: 'b' }] }, '<ul>\n <li>a</li>\n <li>b</li>\n</ul>\n'],
16
+ ['{{! comment }}x{{#t}}y{{/t}}', { t: true }, 'xy'],
17
+ ['{{#o}}{{k}}-{{top}}{{/o}}', { o: { k: 1 }, top: 'T' }, '1-T'],
18
+ ['{{#f}}x{{/f}}', { f: false }, ''],
19
+ ['{{ name }}', { name: 'sp' }, 'sp'],
20
+ ['{{a.missing.deep}}', { a: {} }, ''],
21
+ ['{{#xs}}{{name}}{{/xs}}', { xs: [{ name: 'a' }, {}], name: 'outer' }, 'aouter'],
22
+ ['{{greet}}', { who: 'x', greet() { return `hi ${this.who}`; } }, 'hi x']
23
+ ];
24
+ for (const [source, model, expected] of cases) {
25
+ (0, bun_test_1.it)(`renders ${JSON.stringify(source)}`, () => {
26
+ (0, chai_1.expect)(new template_1.Template(source).render(model)).to.equal(expected);
27
+ });
28
+ }
29
+ (0, bun_test_1.it)('fails on templates it does not support or cannot read', () => {
30
+ for (const source of ['{{> header}}', '{{=<% %>=}}', '{{#a}}x', '{{/a}}', '{{#a}}{{/b}}', '{{name', '{{{raw}}']) {
31
+ (0, chai_1.expect)(() => new template_1.Template(source), source).to.throw();
32
+ }
33
+ });
34
+ });
35
+ //# sourceMappingURL=template.spec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"template.spec.js","sourceRoot":"","sources":["../../../src/node/view/template.spec.ts"],"names":[],"mappings":";;AAAA,uCAAwC;AACxC,+BAA8B;AAC9B,yCAAsC;AAEtC,IAAA,mBAAQ,EAAC,UAAU,EAAE,GAAG,EAAE;IACtB,gCAAgC;IAChC,MAAM,KAAK,GAAgC;QACvC,CAAC,cAAc,EAAE,EAAE,IAAI,EAAE,YAAY,EAAE,EAAE,iDAAiD,CAAC;QAC3F,CAAC,mBAAmB,EAAE,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE,QAAQ,CAAC;QAC/C,CAAC,2BAA2B,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,QAAQ,CAAC;QACzD,CAAC,kDAAkD,EAAE,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,CAAC;QAClF,CAAC,2BAA2B,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,EAAE,OAAO,CAAC;QACrD,C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,MAAM,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QAClE,CAAC,CAAC,CAAC;IACP,CAAC;IAED,IAAA,aAAE,EAAC,uDAAuD,EAAE,GAAG,EAAE;QAC7D,KAAK,MAAM,MAAM,IAAI,CAAC,cAAc,EAAE,aAAa,EAAE,SAAS,EAAE,QAAQ,EAAE,cAAc,EAAE,QAAQ,EAAE,UAAU,CAAC,EAAE,CAAC;YAC9G,IAAA,aAAM,EAAC,GAAG,EAAE,CAAC,IAAI,mBAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC;QAC1D,CAAC;IACL,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/mvc",
3
- "version": "4.1.0",
3
+ "version": "4.2.0",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "cell-component"
@@ -14,6 +14,9 @@
14
14
  "url": "https://github.com/cellbang/cell.git"
15
15
  },
16
16
  "license": "MIT",
17
+ "engines": {
18
+ "node": ">=18.0.0"
19
+ },
17
20
  "main": "lib/common/index.js",
18
21
  "typings": "lib/common/index.d.ts",
19
22
  "files": [
@@ -29,13 +32,9 @@
29
32
  "watch": "tsc -b -w --preserveWatchOutput"
30
33
  },
31
34
  "dependencies": {
32
- "@celljs/core": "4.1.0",
33
- "@celljs/http": "4.1.0",
34
- "@celljs/web": "4.1.0",
35
- "mustache": "^4.2.0"
36
- },
37
- "devDependencies": {
38
- "@types/mustache": "^4.2.2"
35
+ "@celljs/core": "4.2.0",
36
+ "@celljs/http": "4.2.0",
37
+ "@celljs/web": "4.2.0"
39
38
  },
40
39
  "publishConfig": {
41
40
  "access": "public"
@@ -1,7 +1,6 @@
1
1
  import { describe, it, afterEach } from 'bun:test';
2
2
  import { expect } from 'chai';
3
- import { createServer, Server } from 'node:http';
4
- import { createRequire } from 'node:module';
3
+ import { Server } from 'node:http';
5
4
  import { AddressInfo } from 'node:net';
6
5
  // 按 CLI 生成的后端入口加载顺序导入:core 的 static module → web 的 module → mvc 的 module(见 @celljs/vite 的
7
6
  // ContainerGenerator 与各包 package.json 的 cell 字段)。不要导入 application-factory:它会顺带登记 CommonApplication,
@@ -11,12 +10,12 @@ import coreNodeStatic from '@celljs/core/lib/node/static-module';
11
10
  import webCommonModule from '@celljs/web/lib/common/module';
12
11
  import webNodeModule from '@celljs/web/lib/node/module';
13
12
  import mvcNodeModule from './module';
14
- import { ContainerFactory } from '@celljs/core/lib/common/container/container-factory';
13
+ import { Container } from '@celljs/core/lib/common/container/container';
15
14
  import { ContainerProvider } from '@celljs/core/lib/common/container/container-provider';
16
15
  import { autoBind } from '@celljs/core/lib/common/container/auto-bind';
17
16
  import { currentThis } from '@celljs/core/lib/common/utils';
18
17
  import { Application } from '@celljs/core/lib/common/application/application-protocol';
19
- import { Context, Dispatcher, NotFoundError, HttpError } from '@celljs/web/lib/node';
18
+ import { NotFoundError, HttpError, HttpServerUtil } from '@celljs/web/lib/node';
20
19
  import {
21
20
  Controller, Get, Post, Put, Delete, Patch, All, Param, Query, Body, Header, Cookie, Session, Req, Res, Text, Json, Catch
22
21
  } from './annotation';
@@ -24,12 +23,11 @@ import {
24
23
  /**
25
24
  * mvc 的 HTTP 行为约定(阶段 1.E 行为测试):经真实 HTTP 请求断言路由、参数解析、响应与异常。
26
25
  *
27
- * 请求路径与 express-adapter 的 http-application-entry 相同:express 收到请求 → new Context(req, res) →
28
- * Context.run → Dispatcher.dispatch。express 是 @celljs/web 的依赖,这里从 web 的位置解析,mvc 不新增依赖。
26
+ * 请求路径与 express-adapter 的 http-application-entry 相同:HttpServerUtil.createHttpServer 收到请求 →
27
+ * new Context(req, res) → Context.run → Dispatcher.dispatch。
29
28
  * 配置是 core / web / mvc / http 的 cell.yml 合并后的默认值(CLI 会做这一步;这里照抄,保持与 cell.yml 一致)。
30
29
  * 阶段 2 换 HTTP 管线时,只需要替换 serve(),用例本身不用改。
31
30
  */
32
- const express = createRequire(require.resolve('@celljs/web/package.json'))('express');
33
31
 
34
32
  const DEFAULT_CONFIG = {
35
33
  cell: {
@@ -59,16 +57,11 @@ afterEach(async () => {
59
57
  /** 启动应用(在调用之前定义好的控制器都会被装配),返回指向它的 fetch。 */
60
58
  async function serve(): Promise<(path: string, init?: RequestInit) => Promise<Response>> {
61
59
  currentThis.cellProps = structuredClone(DEFAULT_CONFIG);
62
- const container = ContainerFactory.create(coreCommonStatic, coreNodeStatic, webCommonModule, webNodeModule, mvcNodeModule, autoBind());
60
+ const container = new Container(coreCommonStatic, coreNodeStatic, webCommonModule, webNodeModule, mvcNodeModule, autoBind());
63
61
  ContainerProvider.set(container);
64
62
  const app = container.get<Application>(Application);
65
63
  await app.start();
66
- const handler = express();
67
- handler.all('*', (req: any, res: any) => {
68
- const ctx = new Context(req, res);
69
- Context.run(() => container.get<Dispatcher<Context>>(Dispatcher).dispatch(ctx));
70
- });
71
- server = createServer(handler);
64
+ server = HttpServerUtil.createHttpServer(HttpServerUtil.createHandler(container));
72
65
  await new Promise<void>(r => server!.listen(0, '127.0.0.1', r));
73
66
  const base = `http://127.0.0.1:${(server!.address() as AddressInfo).port}`;
74
67
  return (path, init) => fetch(base + path, init);
@@ -177,7 +170,7 @@ describe('mvc 参数解析', () => {
177
170
  expect(BodyCtl).to.be.a('function');
178
171
  });
179
172
 
180
- it('现状:非法 JSON 请求体返回 500(body-parser 标的 400 没被采用),处理函数不会被调用', async () => {
173
+ it('非法 JSON 请求体返回 400,处理函数不会被调用', async () => {
181
174
  let called = false;
182
175
  @Controller('bad')
183
176
  class Bad {
@@ -186,7 +179,7 @@ describe('mvc 参数解析', () => {
186
179
  }
187
180
  const f = await serve();
188
181
  const res = await f('/bad', { method: 'POST', headers: JSON_HEADERS, body: '{oops' });
189
- expect(res.status).to.equal(500);
182
+ expect(res.status).to.equal(400);
190
183
  expect(await res.text()).to.not.match(/\n\s+at /);
191
184
  expect(called).to.equal(false);
192
185
  expect(Bad).to.be.a('function');
@@ -59,7 +59,10 @@ export class FileView implements View {
59
59
 
60
60
  protected async handleError(response: Response, reason: string | Error, status: number = HttpStatus.INTERNAL_SERVER_ERROR): Promise<void> {
61
61
  this.logger.error(reason);
62
- response.status(status).send('Unable to download file.').end();
62
+ if (!response.headersSent) {
63
+ response.statusCode = status;
64
+ }
65
+ response.end('Unable to download file.');
63
66
  }
64
67
 
65
68
  support({ viewName }: ViewMetadata): Promise<boolean> {
@@ -1,13 +1,14 @@
1
1
  import { Component, Value } from '@celljs/core';
2
2
  import { View } from './view-protocol';
3
3
  import { Context } from '@celljs/web/lib/node';
4
- import Mustache from 'mustache';
5
4
  import { join } from 'path';
6
5
  import { readFile } from 'node:fs/promises';
7
6
  import { ViewMetadata } from '../annotation/view';
8
7
  import { HTML_VIEW_NAME } from '../annotation/html';
9
8
  import { MediaType } from '@celljs/http';
9
+ import { Template } from './template';
10
10
 
11
+ /** Renders `@Html('page.mustache')` with {@link Template}, from `cell.mustache.baseViewDir`. */
11
12
  @Component(View)
12
13
  export class HtmlView implements View {
13
14
 
@@ -16,16 +17,23 @@ export class HtmlView implements View {
16
17
  readonly priority = 500;
17
18
 
18
19
  @Value('cell.mustache')
19
- protected readonly options: any;
20
+ protected readonly options: { baseViewDir: string, cache?: boolean };
21
+
22
+ /** Compiled templates by file, when `cell.mustache.cache` is on. */
23
+ protected readonly templates = new Map<string, Template>();
20
24
 
21
25
  async render(model: any, { metadata }: ViewMetadata): Promise<void> {
22
26
  const response = Context.getCurrent().response;
23
27
  const { baseViewDir, cache } = this.options;
24
- const template = await readFile(join(__dirname, baseViewDir, metadata!.file), { encoding: 'utf8' });
25
- if (cache) {
26
- Mustache.parse(template);
28
+ const file = join(__dirname, baseViewDir, metadata!.file);
29
+ let template = this.templates.get(file);
30
+ if (!template) {
31
+ template = new Template(await readFile(file, { encoding: 'utf8' }));
32
+ if (cache) {
33
+ this.templates.set(file, template);
34
+ }
27
35
  }
28
- response.body = Mustache.render(template, model);
36
+ response.body = template.render(model);
29
37
  }
30
38
 
31
39
  support({ viewName, metadata }: ViewMetadata): Promise<boolean> {
@@ -2,5 +2,6 @@ export * from './json-view';
2
2
  export * from './text-view';
3
3
  export * from './view-provider';
4
4
  export * from './html-view';
5
+ export * from './template';
5
6
  export * from './file-view';
6
- export * from './json-view-template-renderer';
7
+ export * from './json-view-template-renderer';
@@ -0,0 +1,34 @@
1
+ import { describe, it } from 'bun:test';
2
+ import { expect } from 'chai';
3
+ import { Template } from './template';
4
+
5
+ describe('Template', () => {
6
+ // What mustache 4.2.0 rendered.
7
+ const cases: [string, unknown, string][] = [
8
+ ['Hi {{name}}!', { name: '<b>&"\'/`=' }, 'Hi &lt;b&gt;&amp;&quot;&#39;&#x2F;&#x60;&#x3D;!'],
9
+ ['{{{raw}}}{{&raw}}', { raw: '<i>' }, '<i><i>'],
10
+ ['{{#list}}[{{.}}]{{/list}}', { list: [1, 2] }, '[1][2]'],
11
+ ['{{#user}}{{name}}{{/user}}{{^user}}none{{/user}}', { user: { name: 'u' } }, 'u'],
12
+ ['{{^items}}empty{{/items}}', { items: [] }, 'empty'],
13
+ ['{{a.b.c}}|{{missing}}|{{n}}', { a: { b: { c: 3 } }, n: 0 }, '3||0'],
14
+ ['<ul>\n {{#xs}}\n <li>{{name}}</li>\n {{/xs}}\n</ul>\n', { xs: [{ name: 'a' }, { name: 'b' }] }, '<ul>\n <li>a</li>\n <li>b</li>\n</ul>\n'],
15
+ ['{{! comment }}x{{#t}}y{{/t}}', { t: true }, 'xy'],
16
+ ['{{#o}}{{k}}-{{top}}{{/o}}', { o: { k: 1 }, top: 'T' }, '1-T'],
17
+ ['{{#f}}x{{/f}}', { f: false }, ''],
18
+ ['{{ name }}', { name: 'sp' }, 'sp'],
19
+ ['{{a.missing.deep}}', { a: {} }, ''],
20
+ ['{{#xs}}{{name}}{{/xs}}', { xs: [{ name: 'a' }, {}], name: 'outer' }, 'aouter'],
21
+ ['{{greet}}', { who: 'x', greet() { return `hi ${(this as { who: string }).who}`; } }, 'hi x']
22
+ ];
23
+ for (const [source, model, expected] of cases) {
24
+ it(`renders ${JSON.stringify(source)}`, () => {
25
+ expect(new Template(source).render(model)).to.equal(expected);
26
+ });
27
+ }
28
+
29
+ it('fails on templates it does not support or cannot read', () => {
30
+ for (const source of ['{{> header}}', '{{=<% %>=}}', '{{#a}}x', '{{/a}}', '{{#a}}{{/b}}', '{{name', '{{{raw}}']) {
31
+ expect(() => new Template(source), source).to.throw();
32
+ }
33
+ });
34
+ });