-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathenvironment.ts
332 lines (275 loc) · 8 KB
/
environment.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import tokenize, { Token } from "./tokenizer.ts";
import type { Loader } from "./loader.ts";
import { transformTemplateCode } from "./transformer.ts";
import { TemplateError, TransformError } from "./errors.ts";
export interface TemplateResult {
content: string;
[key: string]: unknown;
}
export interface Template {
(data?: Record<string, unknown>): Promise<TemplateResult>;
source: string;
code: string;
file?: string;
}
export interface TemplateSync {
(data?: Record<string, unknown>): TemplateResult;
source: string;
code: string;
file?: string;
}
export type TokenPreprocessor = (
env: Environment,
tokens: Token[],
path?: string,
) => Token[] | void;
export type Tag = (
env: Environment,
code: string,
output: string,
tokens: Token[],
) => string | undefined;
export type FilterThis = {
data: Record<string, unknown>;
env: Environment;
};
// deno-lint-ignore no-explicit-any
export type Filter = (this: FilterThis, ...args: any[]) => any;
export type Plugin = (env: Environment) => void;
export interface Options {
loader: Loader;
dataVarname: string;
autoescape: boolean;
autoDataVarname: boolean;
}
export class Environment {
cache: Map<string, Template> = new Map();
options: Options;
tags: Tag[] = [];
tokenPreprocessors: TokenPreprocessor[] = [];
filters: Record<string, Filter> = {};
utils: Record<string, unknown> = {};
constructor(options: Options) {
this.options = options;
}
use(plugin: Plugin) {
plugin(this);
}
async run(
file: string,
data: Record<string, unknown>,
from?: string,
): Promise<TemplateResult> {
const template = await this.load(file, from);
return await template(data);
}
async runString(
source: string,
data?: Record<string, unknown>,
file?: string,
): Promise<TemplateResult> {
if (file) {
const cached = this.cache.get(file);
if (cached) {
return await cached(data);
}
const template = this.compile(source, file);
this.cache.set(file, template);
return await template(data);
}
const template = this.compile(source, file);
return await template(data);
}
runStringSync(
source: string,
data?: Record<string, unknown>,
): TemplateResult {
const template = this.compile(source, "", {}, true);
return template(data);
}
compile(
source: string,
path?: string,
defaults?: Record<string, unknown>,
sync?: false,
): Template;
compile(
source: string,
path?: string,
defaults?: Record<string, unknown>,
sync?: true,
): TemplateSync;
compile(
source: string,
path?: string,
defaults?: Record<string, unknown>,
sync = false,
): Template | TemplateSync {
if (typeof source !== "string") {
throw new Error(
`The source code of "${path}" must be a string. Got ${typeof source}`,
);
}
const tokens = this.tokenize(source, path);
let code = this.compileTokens(tokens).join("\n");
const { dataVarname, autoDataVarname } = this.options;
if (autoDataVarname) {
try {
code = transformTemplateCode(code, dataVarname);
} catch (cause) {
if (cause instanceof TransformError) {
throw new TemplateError(path, source, cause.position, cause);
}
throw new Error(`Unknown error while transforming ${path}`, { cause });
}
}
const constructor = new Function(
"__file",
"__env",
"__defaults",
"__err",
`return${sync ? "" : " async"} function (${dataVarname}) {
let __pos = 0;
try {
${dataVarname} = Object.assign({}, __defaults, ${dataVarname});
const __exports = { content: "" };
${code}
return __exports;
} catch (cause) {
const template = __env.cache.get(__file);
throw new __err(__file, template?.source, __pos, cause);
}
}
`,
);
const template: Template = constructor(path, this, defaults, TemplateError);
template.file = path;
template.code = code;
template.source = source;
return template;
}
tokenize(source: string, path?: string): Token[] {
const result = tokenize(source);
let { tokens } = result;
const { position, error } = result;
if (error) {
throw new TemplateError(path, source, position, error);
}
for (const tokenPreprocessor of this.tokenPreprocessors) {
const result = tokenPreprocessor(this, tokens, path);
if (result !== undefined) {
tokens = result;
}
}
return tokens;
}
async load(file: string, from?: string): Promise<Template> {
const path = this.options.loader.resolve(from || "", file);
if (!this.cache.has(path)) {
// Remove query and hash params from path before loading
const cleanPath = path
.split("?")[0]
.split("#")[0];
const { source, data } = await this.options.loader.load(cleanPath);
const template = this.compile(source, path, data);
this.cache.set(path, template);
}
return this.cache.get(path)!;
}
compileTokens(
tokens: Token[],
outputVar = "__exports.content",
stopAt?: string[],
): string[] {
const compiled: string[] = [];
tokens:
while (tokens.length > 0) {
if (stopAt && tokens[0][0] === "tag" && stopAt.includes(tokens[0][1])) {
break;
}
const [type, code, pos] = tokens.shift()!;
if (type === "comment") {
continue;
}
if (type === "string") {
if (code !== "") {
compiled.push(`${outputVar} += ${JSON.stringify(code)};`);
}
continue;
}
if (type === "tag") {
compiled.push(`__pos = ${pos};`);
for (const tag of this.tags) {
const compiledTag = tag(this, code, outputVar, tokens);
if (typeof compiledTag === "string") {
compiled.push(compiledTag);
continue tokens;
}
}
// Unknown tag, just print it
const expression = this.compileFilters(
tokens,
code,
this.options.autoescape,
);
compiled.push(`${outputVar} += (${expression}) ?? "";`);
continue;
}
throw new Error(`Unknown token type "${type}"`);
}
return compiled;
}
compileFilters(tokens: Token[], output: string, autoescape = false): string {
let unescaped = false;
while (tokens.length > 0 && tokens[0][0] === "filter") {
const [, code] = tokens.shift()!;
const match = code.match(/^(await\s+)?([\w.]+)(?:\((.*)\))?$/);
if (!match) {
throw new Error(`Invalid filter: ${code}`);
}
const [_, isAsync, name, args] = match;
if (!this.filters[name]) {
if (name === "safe") {
unescaped = true;
} else if (isGlobal(name)) {
// If a global function
output = `${isAsync ? "await " : ""}${name}(${output}${
args ? `, ${args}` : ""
})`;
} else {
// It's a prototype's method (e.g. `String.toUpperCase()`)
output = `${isAsync ? "await " : ""}(${output})?.${name}?.(${
args ? args : ""
})`;
}
} else {
// It's a filter (e.g. filters.upper())
const { dataVarname } = this.options;
output = `${
(isAsync || checkAsync(this.filters[name])) ? "await " : ""
}__env.filters.${name}.call({data:${dataVarname},env:__env}, ${output}${
args ? `, ${args}` : ""
})`;
}
}
// Escape by default
if (autoescape && !unescaped) {
output = `__env.filters.escape(${output})`;
}
return output;
}
}
function isGlobal(name: string) {
// @ts-ignore TS doesn't know about globalThis
if (globalThis[name]) {
return true;
}
if (name.includes(".")) {
const [obj, prop] = name.split(".");
// @ts-ignore TS doesn't know about globalThis
return typeof globalThis[obj]?.[prop] === "function";
}
}
function checkAsync(fn: () => unknown): boolean {
return fn.constructor?.name === "AsyncFunction";
}