@kosatyi/ejs 0.0.109 → 0.0.111

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.
@@ -14,11 +14,74 @@ const isArrayOfVariables = value => {
14
14
  if (!isArray(value)) return false;
15
15
  return value.filter(name => {
16
16
  const valid = jsVariableName.test(name);
17
- if (valid === false) console.log(`ejsConfig.globals: expected '${name}' to be valid variable name --> skipped`);
17
+ if (valid === false) console.log("ejsConfig.globals: expected '".concat(name, "' to be valid variable name --> skipped"));
18
18
  return valid;
19
19
  });
20
20
  };
21
21
 
22
+ function _assertClassBrand(e, t, n) {
23
+ if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n;
24
+ throw new TypeError("Private element is not present on this object");
25
+ }
26
+ function _checkPrivateRedeclaration(e, t) {
27
+ if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object");
28
+ }
29
+ function _classPrivateFieldGet2(s, a) {
30
+ return s.get(_assertClassBrand(s, a));
31
+ }
32
+ function _classPrivateFieldInitSpec(e, t, a) {
33
+ _checkPrivateRedeclaration(e, t), t.set(e, a);
34
+ }
35
+ function _classPrivateFieldSet2(s, a, r) {
36
+ return s.set(_assertClassBrand(s, a), r), r;
37
+ }
38
+ function _classPrivateMethodInitSpec(e, a) {
39
+ _checkPrivateRedeclaration(e, a), a.add(e);
40
+ }
41
+ function _defineProperty(e, r, t) {
42
+ return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
43
+ value: t,
44
+ enumerable: true,
45
+ configurable: true,
46
+ writable: true
47
+ }) : e[r] = t, e;
48
+ }
49
+ function ownKeys(e, r) {
50
+ var t = Object.keys(e);
51
+ if (Object.getOwnPropertySymbols) {
52
+ var o = Object.getOwnPropertySymbols(e);
53
+ r && (o = o.filter(function (r) {
54
+ return Object.getOwnPropertyDescriptor(e, r).enumerable;
55
+ })), t.push.apply(t, o);
56
+ }
57
+ return t;
58
+ }
59
+ function _objectSpread2(e) {
60
+ for (var r = 1; r < arguments.length; r++) {
61
+ var t = null != arguments[r] ? arguments[r] : {};
62
+ r % 2 ? ownKeys(Object(t), true).forEach(function (r) {
63
+ _defineProperty(e, r, t[r]);
64
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
65
+ Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
66
+ });
67
+ }
68
+ return e;
69
+ }
70
+ function _toPrimitive(t, r) {
71
+ if ("object" != typeof t || !t) return t;
72
+ var e = t[Symbol.toPrimitive];
73
+ if (void 0 !== e) {
74
+ var i = e.call(t, r);
75
+ if ("object" != typeof i) return i;
76
+ throw new TypeError("@@toPrimitive must return a primitive value.");
77
+ }
78
+ return ("string" === r ? String : Number)(t);
79
+ }
80
+ function _toPropertyKey(t) {
81
+ var i = _toPrimitive(t, "string");
82
+ return "symbol" == typeof i ? i : i + "";
83
+ }
84
+
22
85
  /**
23
86
  * @type {EjsConfig}
24
87
  */
@@ -92,7 +155,7 @@ const entities = function () {
92
155
  const symbols = string => {
93
156
  return ('' + string).replace(symbolEntitiesMatch, match => '\\' + symbolEntities[match]);
94
157
  };
95
- const safeValue = (value, escape) => {
158
+ const escapeValue = (value, escape) => {
96
159
  const check = value;
97
160
  return check == null ? '' : Boolean(escape) === true ? entities(check) : check;
98
161
  };
@@ -125,9 +188,7 @@ const each = (object, callback) => {
125
188
  }
126
189
  };
127
190
  const omit = (object, list) => {
128
- const result = {
129
- ...object
130
- };
191
+ const result = _objectSpread2({}, object);
131
192
  for (const key of list) {
132
193
  delete result[key];
133
194
  }
@@ -146,48 +207,70 @@ const bindContext = function (object) {
146
207
  }
147
208
  };
148
209
 
149
- class Template {
150
- #path;
151
- #resolver;
152
- #cache;
153
- #compiler;
154
- static exports = ['configure', 'get', 'compile'];
210
+ class EjsError extends Error {
211
+ constructor(code, content) {
212
+ super(content);
213
+ this.code = code;
214
+ if (content instanceof Error) {
215
+ this.stack = content.stack;
216
+ this.message = content.message;
217
+ }
218
+ }
219
+ }
220
+ const error = (code, content) => {
221
+ throw new EjsError(code, content);
222
+ };
223
+
224
+ var _path$1 = /*#__PURE__*/new WeakMap();
225
+ var _resolver = /*#__PURE__*/new WeakMap();
226
+ var _cache$2 = /*#__PURE__*/new WeakMap();
227
+ var _compiler$1 = /*#__PURE__*/new WeakMap();
228
+ var _EjsTemplate_brand = /*#__PURE__*/new WeakSet();
229
+ class EjsTemplate {
155
230
  constructor(options, cache, compiler) {
231
+ _classPrivateMethodInitSpec(this, _EjsTemplate_brand);
232
+ _classPrivateFieldInitSpec(this, _path$1, void 0);
233
+ _classPrivateFieldInitSpec(this, _resolver, void 0);
234
+ _classPrivateFieldInitSpec(this, _cache$2, void 0);
235
+ _classPrivateFieldInitSpec(this, _compiler$1, void 0);
156
236
  bindContext(this, this.constructor.exports);
157
- this.#cache = cache;
158
- this.#compiler = compiler;
159
- this.configure(options ?? {});
237
+ _classPrivateFieldSet2(_cache$2, this, cache);
238
+ _classPrivateFieldSet2(_compiler$1, this, compiler);
239
+ this.configure(options !== null && options !== void 0 ? options : {});
160
240
  }
161
241
  configure(options) {
162
- this.#path = options.path;
242
+ _classPrivateFieldSet2(_path$1, this, options.path);
163
243
  if (isFunction(options.resolver)) {
164
- this.#resolver = options.resolver;
165
- }
166
- }
167
- #resolve(template) {
168
- const cached = this.#cache.get(template);
169
- if (cached instanceof Promise) return cached;
170
- const result = Promise.resolve(this.#resolver(this.#path, template));
171
- this.#cache.set(template, result);
172
- return result;
173
- }
174
- compile(content, template) {
175
- const cached = this.#cache.get(template);
176
- if (typeof cached === 'function') return cached;
177
- if (typeof content === 'string') {
178
- content = this.#compiler.compile(content, template);
179
- }
180
- if (typeof content === 'function') {
181
- this.#cache.set(template, content);
182
- return content;
244
+ _classPrivateFieldSet2(_resolver, this, options.resolver);
183
245
  }
184
246
  }
185
247
  get(template) {
186
- return this.#resolve(template).then(content => this.compile(content, template));
248
+ return _assertClassBrand(_EjsTemplate_brand, this, _resolve).call(this, template).then(content => {
249
+ return _assertClassBrand(_EjsTemplate_brand, this, _compile).call(this, content, template);
250
+ });
251
+ }
252
+ }
253
+ function _resolve(template) {
254
+ const cached = _classPrivateFieldGet2(_cache$2, this).get(template);
255
+ if (cached instanceof Promise) return cached;
256
+ const result = Promise.resolve(_classPrivateFieldGet2(_resolver, this).call(this, _classPrivateFieldGet2(_path$1, this), template, error));
257
+ _classPrivateFieldGet2(_cache$2, this).set(template, result);
258
+ return result;
259
+ }
260
+ function _compile(content, template) {
261
+ const cached = _classPrivateFieldGet2(_cache$2, this).get(template);
262
+ if (typeof cached === 'function') return cached;
263
+ if (typeof content === 'string') {
264
+ content = _classPrivateFieldGet2(_compiler$1, this).compile(content, template);
265
+ }
266
+ if (typeof content === 'function') {
267
+ _classPrivateFieldGet2(_cache$2, this).set(template, content);
268
+ return content;
187
269
  }
188
270
  }
271
+ _defineProperty(EjsTemplate, "exports", ['configure', 'get']);
189
272
 
190
- const tokenList = [['-', (v, b, s) => `')\n${b}(${s}(${v},1))\n${b}('`], ['=', (v, b, s) => `')\n${b}(${s}(${v}))\n${b}('`], ['#', (v, b) => `')\n/**${v}**/\n${b}('`], ['', (v, b) => `')\n${v}\n${b}('`]];
273
+ const tokenList = [['-', (v, b, s) => "')\n".concat(b, "(").concat(s, "(").concat(v, ",1))\n").concat(b, "('")], ['=', (v, b, s) => "')\n".concat(b, "(").concat(s, "(").concat(v, "))\n").concat(b, "('")], ['#', (v, b) => "')\n/**".concat(v, "**/\n").concat(b, "('")], ['', (v, b) => "')\n".concat(v, "\n").concat(b, "('")]];
191
274
  const tokensMatch = (regex, content, callback) => {
192
275
  let index = 0;
193
276
  content.replace(regex, function () {
@@ -199,133 +282,138 @@ const tokensMatch = (regex, content, callback) => {
199
282
  return match;
200
283
  });
201
284
  };
202
- class Compiler {
203
- #config = {};
204
- static exports = ['compile'];
285
+ var _config$1 = /*#__PURE__*/new WeakMap();
286
+ class EjsCompiler {
205
287
  constructor(options) {
288
+ _classPrivateFieldInitSpec(this, _config$1, {});
206
289
  bindContext(this, this.constructor.exports);
207
290
  this.configure(options);
208
291
  }
209
292
  configure(options) {
210
- this.#config.strict = options.strict;
211
- this.#config.rmWhitespace = options.rmWhitespace;
212
- this.#config.token = options.token;
213
- this.#config.vars = options.vars;
214
- this.#config.globals = options.globals;
215
- this.#config.legacy = options.legacy ?? true;
216
- this.#config.slurp = {
293
+ var _options$legacy;
294
+ _classPrivateFieldGet2(_config$1, this).strict = options.strict;
295
+ _classPrivateFieldGet2(_config$1, this).rmWhitespace = options.rmWhitespace;
296
+ _classPrivateFieldGet2(_config$1, this).token = options.token;
297
+ _classPrivateFieldGet2(_config$1, this).vars = options.vars;
298
+ _classPrivateFieldGet2(_config$1, this).globals = options.globals;
299
+ _classPrivateFieldGet2(_config$1, this).legacy = (_options$legacy = options.legacy) !== null && _options$legacy !== void 0 ? _options$legacy : true;
300
+ _classPrivateFieldGet2(_config$1, this).slurp = {
217
301
  match: '[s\t\n]*',
218
- start: [this.#config.token.start, '_'],
219
- end: ['_', this.#config.token.end]
302
+ start: [_classPrivateFieldGet2(_config$1, this).token.start, '_'],
303
+ end: ['_', _classPrivateFieldGet2(_config$1, this).token.end]
220
304
  };
221
- this.#config.matches = [];
222
- this.#config.formats = [];
305
+ _classPrivateFieldGet2(_config$1, this).matches = [];
306
+ _classPrivateFieldGet2(_config$1, this).formats = [];
223
307
  for (const [symbol, format] of tokenList) {
224
- this.#config.matches.push(this.#config.token.start.concat(symbol).concat(this.#config.token.regex).concat(this.#config.token.end));
225
- this.#config.formats.push(format);
308
+ _classPrivateFieldGet2(_config$1, this).matches.push(_classPrivateFieldGet2(_config$1, this).token.start.concat(symbol).concat(_classPrivateFieldGet2(_config$1, this).token.regex).concat(_classPrivateFieldGet2(_config$1, this).token.end));
309
+ _classPrivateFieldGet2(_config$1, this).formats.push(format);
226
310
  }
227
- this.#config.regex = new RegExp(this.#config.matches.join('|').concat('|$'), 'g');
228
- this.#config.slurpStart = new RegExp([this.#config.slurp.match, this.#config.slurp.start.join('')].join(''), 'gm');
229
- this.#config.slurpEnd = new RegExp([this.#config.slurp.end.join(''), this.#config.slurp.match].join(''), 'gm');
230
- if (this.#config.globals.length) {
231
- if (this.#config.legacy) {
232
- this.#config.globalVariables = `const ${this.#config.globals.map(name => `${name}=${this.#config.vars.SCOPE}.${name}`).join(',')};`;
311
+ _classPrivateFieldGet2(_config$1, this).regex = new RegExp(_classPrivateFieldGet2(_config$1, this).matches.join('|').concat('|$'), 'g');
312
+ _classPrivateFieldGet2(_config$1, this).slurpStart = new RegExp([_classPrivateFieldGet2(_config$1, this).slurp.match, _classPrivateFieldGet2(_config$1, this).slurp.start.join('')].join(''), 'gm');
313
+ _classPrivateFieldGet2(_config$1, this).slurpEnd = new RegExp([_classPrivateFieldGet2(_config$1, this).slurp.end.join(''), _classPrivateFieldGet2(_config$1, this).slurp.match].join(''), 'gm');
314
+ if (_classPrivateFieldGet2(_config$1, this).globals.length) {
315
+ if (_classPrivateFieldGet2(_config$1, this).legacy) {
316
+ _classPrivateFieldGet2(_config$1, this).globalVariables = "const ".concat(_classPrivateFieldGet2(_config$1, this).globals.map(name => "".concat(name, "=").concat(_classPrivateFieldGet2(_config$1, this).vars.SCOPE, ".").concat(name)).join(','), ";");
233
317
  } else {
234
- this.#config.globalVariables = `const {${this.#config.globals.join(',')}} = ${this.#config.vars.SCOPE};`;
318
+ _classPrivateFieldGet2(_config$1, this).globalVariables = "const {".concat(_classPrivateFieldGet2(_config$1, this).globals.join(','), "} = ").concat(_classPrivateFieldGet2(_config$1, this).vars.SCOPE, ";");
235
319
  }
236
320
  }
237
321
  }
238
322
  compile(content, path) {
239
- const GLOBALS = this.#config.globalVariables;
323
+ const GLOBALS = _classPrivateFieldGet2(_config$1, this).globalVariables;
240
324
  const {
241
325
  SCOPE,
242
326
  SAFE,
243
327
  BUFFER,
244
328
  COMPONENT,
245
329
  ELEMENT
246
- } = this.#config.vars;
247
- if (this.#config.rmWhitespace) {
330
+ } = _classPrivateFieldGet2(_config$1, this).vars;
331
+ if (_classPrivateFieldGet2(_config$1, this).rmWhitespace) {
248
332
  content = String(content).replace(/[\r\n]+/g, '\n').replace(/^\s+|\s+$/gm, '');
249
333
  }
250
- content = String(content).replace(this.#config.slurpStart, this.#config.token.start).replace(this.#config.slurpEnd, this.#config.token.end);
251
- let OUTPUT = `${BUFFER}('`;
252
- tokensMatch(this.#config.regex, content, (params, index, offset) => {
334
+ content = String(content).replace(_classPrivateFieldGet2(_config$1, this).slurpStart, _classPrivateFieldGet2(_config$1, this).token.start).replace(_classPrivateFieldGet2(_config$1, this).slurpEnd, _classPrivateFieldGet2(_config$1, this).token.end);
335
+ let OUTPUT = "".concat(BUFFER, "('");
336
+ tokensMatch(_classPrivateFieldGet2(_config$1, this).regex, content, (params, index, offset) => {
253
337
  OUTPUT += symbols(content.slice(index, offset));
254
338
  params.forEach((value, index) => {
255
339
  if (value) {
256
- OUTPUT += this.#config.formats[index](value.trim(), BUFFER, SAFE);
340
+ OUTPUT += _classPrivateFieldGet2(_config$1, this).formats[index](value.trim(), BUFFER, SAFE);
257
341
  }
258
342
  });
259
343
  });
260
- OUTPUT += `');`;
261
- OUTPUT = `try{${OUTPUT}}catch(e){return ${BUFFER}.error(e,'${path}')}`;
262
- if (this.#config.strict === false) {
263
- OUTPUT = `with(${SCOPE}){${OUTPUT}}`;
344
+ OUTPUT += "');";
345
+ OUTPUT = "try{".concat(OUTPUT, "}catch(e){return ").concat(BUFFER, ".error(e)}");
346
+ if (_classPrivateFieldGet2(_config$1, this).strict === false) {
347
+ OUTPUT = "with(".concat(SCOPE, "){").concat(OUTPUT, "}");
264
348
  }
265
- OUTPUT = `${BUFFER}.start();${OUTPUT}return ${BUFFER}.end();`;
266
- OUTPUT += `\n//# sourceURL=${path}`;
349
+ OUTPUT = "".concat(BUFFER, ".start();").concat(OUTPUT, "return ").concat(BUFFER, ".end();");
350
+ OUTPUT += "\n//# sourceURL=".concat(path);
267
351
  if (GLOBALS) {
268
- OUTPUT = `${GLOBALS}\n${OUTPUT}`;
352
+ OUTPUT = "".concat(GLOBALS, "\n").concat(OUTPUT);
269
353
  }
270
354
  try {
271
355
  const params = [SCOPE, COMPONENT, ELEMENT, BUFFER, SAFE];
272
356
  const result = Function.apply(null, params.concat(OUTPUT));
273
- result.source = `(function(${params.join(',')}){\n${OUTPUT}\n});`;
357
+ result.source = "(function(".concat(params.join(','), "){\n").concat(OUTPUT, "\n});");
274
358
  return result;
275
- } catch (error) {
276
- error.filename = path;
277
- error.source = OUTPUT;
278
- throw error;
359
+ } catch (e) {
360
+ e.filename = path;
361
+ e.source = OUTPUT;
362
+ error(0, e);
279
363
  }
280
364
  }
281
365
  }
366
+ _defineProperty(EjsCompiler, "exports", ['configure', 'compile']);
282
367
 
283
- class Cache {
284
- static exports = ['load', 'set', 'get', 'exist', 'clear', 'remove', 'resolve'];
285
- #cache = true;
286
- #precompiled;
287
- #list = {};
368
+ var _cache$1 = /*#__PURE__*/new WeakMap();
369
+ var _precompiled = /*#__PURE__*/new WeakMap();
370
+ var _list = /*#__PURE__*/new WeakMap();
371
+ class EjsCache {
288
372
  constructor(options) {
373
+ _classPrivateFieldInitSpec(this, _cache$1, true);
374
+ _classPrivateFieldInitSpec(this, _precompiled, void 0);
375
+ _classPrivateFieldInitSpec(this, _list, {});
289
376
  bindContext(this, this.constructor.exports);
290
377
  this.configure(options);
291
378
  }
292
379
  get(key) {
293
- if (this.#cache) {
294
- return this.#list[key];
380
+ if (_classPrivateFieldGet2(_cache$1, this)) {
381
+ return _classPrivateFieldGet2(_list, this)[key];
295
382
  }
296
383
  }
297
384
  set(key, value) {
298
- if (this.#cache) {
299
- this.#list[key] = value;
385
+ if (_classPrivateFieldGet2(_cache$1, this)) {
386
+ _classPrivateFieldGet2(_list, this)[key] = value;
300
387
  }
301
388
  }
302
389
  exist(key) {
303
- if (this.#cache) {
304
- return this.#list.hasOwnProperty(key);
390
+ if (_classPrivateFieldGet2(_cache$1, this)) {
391
+ return _classPrivateFieldGet2(_list, this).hasOwnProperty(key);
305
392
  }
306
393
  }
307
394
  clear() {
308
- Object.keys(this.#list).forEach(this.remove);
395
+ Object.keys(_classPrivateFieldGet2(_list, this)).forEach(this.remove);
309
396
  }
310
397
  remove(key) {
311
- delete this.#list[key];
398
+ delete _classPrivateFieldGet2(_list, this)[key];
312
399
  }
313
400
  resolve(key) {
314
401
  return Promise.resolve(this.get(key));
315
402
  }
316
403
  load(data) {
317
- if (this.#cache) {
318
- Object.assign(this.#list, data || {});
404
+ if (_classPrivateFieldGet2(_cache$1, this)) {
405
+ Object.assign(_classPrivateFieldGet2(_list, this), data || {});
319
406
  }
320
407
  }
321
408
  configure(options) {
322
- this.#cache = options.cache;
323
- this.#precompiled = options.precompiled;
409
+ _classPrivateFieldSet2(_cache$1, this, options.cache);
410
+ _classPrivateFieldSet2(_precompiled, this, options.precompiled);
324
411
  if (typeof window === 'object') {
325
- this.load(window[this.#precompiled]);
412
+ this.load(window[_classPrivateFieldGet2(_precompiled, this)]);
326
413
  }
327
414
  }
328
415
  }
416
+ _defineProperty(EjsCache, "exports", ['load', 'set', 'get', 'exist', 'clear', 'remove', 'resolve']);
329
417
 
330
418
  const selfClosed = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
331
419
  const space = ' ';
@@ -343,7 +431,7 @@ const eachAttribute = _ref => {
343
431
  const element = (tag, attrs, content) => {
344
432
  const result = [];
345
433
  const hasClosedTag = selfClosed.indexOf(tag) === -1;
346
- const attributes = Object.entries(attrs ?? {}).map(eachAttribute).filter(e => e).join(space);
434
+ const attributes = Object.entries(attrs !== null && attrs !== void 0 ? attrs : {}).map(eachAttribute).filter(e => e).join(space);
347
435
  result.push([lt, tag, space, attributes, gt].join(''));
348
436
  if (content && hasClosedTag) {
349
437
  result.push(Array.isArray(content) ? content.join('') : content);
@@ -354,31 +442,11 @@ const element = (tag, attrs, content) => {
354
442
  return result.join('');
355
443
  };
356
444
 
357
- class TemplateError extends Error {
358
- name = 'TemplateError';
359
- constructor(error) {
360
- super(error);
361
- if (error instanceof Error) {
362
- this.stack = error.stack;
363
- this.filename = error.filename;
364
- this.lineno = error.lineno;
365
- }
366
- }
367
- }
368
- class TemplateNotFound extends TemplateError {
369
- name = 'TemplateNotFound';
370
- code = 404;
371
- }
372
- class TemplateSyntaxError extends TemplateError {
373
- name = 'TemplateSyntaxError';
374
- code = 500;
375
- }
376
-
377
445
  const resolve = list => {
378
- return Promise.all(list || []).then(list => list.join('')).catch(e => e);
446
+ return Promise.all(list || []).then(list => list.join('')).catch(e => error(0, e));
379
447
  };
380
- const reject = error => {
381
- return Promise.reject(new TemplateSyntaxError(error));
448
+ const reject = e => {
449
+ return Promise.reject(error(0, e));
382
450
  };
383
451
  const EjsBuffer = () => {
384
452
  let store = [];
@@ -403,7 +471,7 @@ const EjsBuffer = () => {
403
471
  array = store.pop();
404
472
  return resolve(result);
405
473
  };
406
- EjsBuffer.error = (e, filename) => {
474
+ EjsBuffer.error = e => {
407
475
  return reject(e);
408
476
  };
409
477
  EjsBuffer.end = () => {
@@ -414,6 +482,7 @@ const EjsBuffer = () => {
414
482
 
415
483
  const PARENT = Symbol('EjsContext.parentTemplate');
416
484
  const createContext$1 = (config, methods) => {
485
+ const globals = config.globals || [];
417
486
  const {
418
487
  BLOCKS,
419
488
  MACRO,
@@ -425,130 +494,113 @@ const createContext$1 = (config, methods) => {
425
494
  COMPONENT,
426
495
  ELEMENT
427
496
  } = config.vars;
428
- const globals = config.globals || [];
429
- function EjsContext(data) {
430
- this[PARENT] = null;
431
- this[BLOCKS] = {};
432
- this[MACRO] = {};
433
- Object.assign(this, omit(data, [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT]));
434
- }
435
- Object.entries(methods).forEach(_ref => {
436
- let [name, value] = _ref;
437
- if (isFunction(value) && globals.includes(name)) {
438
- value = value.bind(EjsContext.prototype);
497
+ class Context {
498
+ constructor(data) {
499
+ this[PARENT] = null;
500
+ this[BLOCKS] = {};
501
+ this[MACRO] = {};
502
+ Object.assign(this, omit(data, [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT]));
439
503
  }
440
- EjsContext.prototype[name] = value;
441
- });
442
- Object.defineProperty(EjsContext.prototype, BUFFER, {
443
- value: EjsBuffer()
444
- });
445
- Object.defineProperty(EjsContext.prototype, BLOCKS, {
446
- value: {},
447
- writable: true
448
- });
449
- Object.defineProperty(EjsContext.prototype, MACRO, {
450
- value: {},
451
- writable: true
452
- });
453
- Object.defineProperty(EjsContext.prototype, LAYOUT, {
454
- value: false,
455
- writable: true
456
- });
457
- Object.defineProperty(EjsContext.prototype, EXTEND, {
458
- value: false,
459
- writable: true
460
- });
461
- Object.defineProperty(EjsContext.prototype, PARENT, {
462
- value: null,
463
- writable: true
464
- });
465
- Object.defineProperties(EjsContext.prototype, {
466
- /** @type {function} */
504
+ }
505
+ Object.defineProperties(Context.prototype, {
506
+ [BUFFER]: {
507
+ value: EjsBuffer()
508
+ },
509
+ [BLOCKS]: {
510
+ value: {},
511
+ writable: true
512
+ },
513
+ [MACRO]: {
514
+ value: {},
515
+ writable: true
516
+ },
517
+ [LAYOUT]: {
518
+ value: false,
519
+ writable: true
520
+ },
521
+ [EXTEND]: {
522
+ value: false,
523
+ writable: true
524
+ },
525
+ [PARENT]: {
526
+ value: null,
527
+ writable: true
528
+ },
467
529
  setParentTemplate: {
468
530
  value(value) {
469
531
  this[PARENT] = value;
470
532
  return this;
471
533
  }
472
534
  },
473
- /** @type {function} */
474
535
  getParentTemplate: {
475
536
  value() {
476
537
  return this[PARENT];
477
538
  }
478
539
  },
479
- /** @type {function} */
480
- useSafeValue: {
481
- get: () => safeValue
540
+ useEscapeValue: {
541
+ value() {
542
+ return escapeValue;
543
+ }
482
544
  },
483
- /** @type {function} */
484
545
  useComponent: {
485
- get() {
546
+ value() {
486
547
  if (isFunction(this[COMPONENT])) {
487
548
  return this[COMPONENT].bind(this);
488
549
  } else {
489
- return () => {
490
- throw new Error(`${COMPONENT} must be a function`);
550
+ return function () {
551
+ error(2, "".concat(COMPONENT, " must be a function"));
491
552
  };
492
553
  }
493
554
  }
494
555
  },
495
- /** @type {function} */
496
556
  useElement: {
497
- get() {
557
+ value() {
498
558
  if (isFunction(this[ELEMENT])) {
499
559
  return this[ELEMENT].bind(this);
500
560
  } else {
501
561
  return () => {
502
- throw new Error(`${ELEMENT} must be a function`);
562
+ error(2, "".concat(ELEMENT, " must be a function"));
503
563
  };
504
564
  }
505
565
  }
506
566
  },
507
- /** @type {function} */
508
567
  useBuffer: {
509
- get() {
568
+ value() {
510
569
  return this[BUFFER];
511
570
  }
512
571
  },
513
- /** @type {function} */
514
572
  getMacro: {
515
573
  value() {
516
574
  return this[MACRO];
517
575
  }
518
576
  },
519
- /** @type {function} */
520
577
  getBlocks: {
521
578
  value() {
522
579
  return this[BLOCKS];
523
580
  }
524
581
  },
525
- /** @type {function} */
526
582
  setExtend: {
527
583
  value(value) {
528
584
  this[EXTEND] = value;
529
585
  return this;
530
586
  }
531
587
  },
532
- /** @type {function} */
533
588
  getExtend: {
534
589
  value() {
535
590
  return this[EXTEND];
536
591
  }
537
592
  },
538
- /** @type {function} */
539
593
  setLayout: {
540
594
  value(layout) {
541
595
  this[LAYOUT] = layout;
542
596
  return this;
543
597
  }
544
598
  },
545
- /** @type {function} */
546
599
  getLayout: {
547
600
  value() {
548
601
  return this[LAYOUT];
549
602
  }
550
603
  },
551
- /** @type {function} */
552
604
  clone: {
553
605
  value(exclude_blocks) {
554
606
  const filter = [LAYOUT, EXTEND, BUFFER];
@@ -558,32 +610,30 @@ const createContext$1 = (config, methods) => {
558
610
  return omit(this, filter);
559
611
  }
560
612
  },
561
- /** @type {function} */
562
613
  extend: {
563
614
  value(layout) {
564
615
  this.setExtend(true);
565
616
  this.setLayout(layout);
566
617
  }
567
618
  },
568
- /** @type {function} */
569
619
  echo: {
570
620
  value() {
571
- return [].slice.call(arguments).forEach(this.useBuffer);
621
+ return [].slice.call(arguments).forEach(this.useBuffer());
572
622
  }
573
623
  },
574
- /** @type {function} */
575
624
  fn: {
576
625
  value(callback) {
577
- return () => {
626
+ const buffer = this.useBuffer();
627
+ const context = this;
628
+ return function () {
578
629
  if (isFunction(callback)) {
579
- this.useBuffer.backup();
580
- this.useBuffer(callback.apply(this, arguments));
581
- return this.useBuffer.restore();
630
+ buffer.backup();
631
+ buffer(callback.apply(context, arguments));
632
+ return buffer.restore();
582
633
  }
583
634
  };
584
635
  }
585
636
  },
586
- /** @type {function} */
587
637
  macro: {
588
638
  value(name, callback) {
589
639
  const list = this.getMacro();
@@ -594,7 +644,6 @@ const createContext$1 = (config, methods) => {
594
644
  };
595
645
  }
596
646
  },
597
- /** @type {function} */
598
647
  call: {
599
648
  value(name) {
600
649
  const list = this.getMacro();
@@ -605,35 +654,35 @@ const createContext$1 = (config, methods) => {
605
654
  }
606
655
  }
607
656
  },
608
- /** @type {function} */
609
657
  block: {
610
658
  value(name, callback) {
611
659
  const blocks = this.getBlocks();
612
660
  blocks[name] = blocks[name] || [];
613
661
  blocks[name].push(this.fn(callback));
614
662
  if (this.getExtend()) return;
663
+ const context = this;
615
664
  const list = Object.assign([], blocks[name]);
616
- const shift = () => list.shift();
617
- const next = () => {
665
+ const shift = function () {
666
+ return list.shift();
667
+ };
668
+ const next = function () {
618
669
  const parent = shift();
619
670
  if (parent) {
620
- return () => {
621
- this.echo(parent(next()));
671
+ return function () {
672
+ context.echo(parent(next()));
622
673
  };
623
674
  } else {
624
- return () => {};
675
+ return function () {};
625
676
  }
626
677
  };
627
678
  this.echo(shift()(next()));
628
679
  }
629
680
  },
630
- /** @type {function} */
631
681
  hasBlock: {
632
682
  value(name) {
633
683
  return this.getBlocks().hasOwnProperty(name);
634
684
  }
635
685
  },
636
- /** @type {function} */
637
686
  include: {
638
687
  value(path, data, cx) {
639
688
  const context = cx === false ? {} : this.clone(true);
@@ -642,7 +691,6 @@ const createContext$1 = (config, methods) => {
642
691
  this.echo(promise);
643
692
  }
644
693
  },
645
- /** @type {function} */
646
694
  use: {
647
695
  value(path, namespace) {
648
696
  this.echo(Promise.resolve(this.require(path)).then(exports$1 => {
@@ -653,13 +701,6 @@ const createContext$1 = (config, methods) => {
653
701
  }));
654
702
  }
655
703
  },
656
- /** @type {function} */
657
- async: {
658
- value(promise, callback) {
659
- this.echo(Promise.resolve(promise).then(callback));
660
- }
661
- },
662
- /** @type {function} */
663
704
  get: {
664
705
  value(name, defaults) {
665
706
  const path = getPath(this, name, true);
@@ -668,7 +709,6 @@ const createContext$1 = (config, methods) => {
668
709
  return hasProp(result, prop) ? result[prop] : defaults;
669
710
  }
670
711
  },
671
- /** @type {function} */
672
712
  set: {
673
713
  value(name, value) {
674
714
  const path = getPath(this, name, false);
@@ -680,7 +720,6 @@ const createContext$1 = (config, methods) => {
680
720
  return result[prop] = value;
681
721
  }
682
722
  },
683
- /** @type {function} */
684
723
  each: {
685
724
  value(object, callback) {
686
725
  if (isString(object)) {
@@ -690,7 +729,6 @@ const createContext$1 = (config, methods) => {
690
729
  },
691
730
  writable: true
692
731
  },
693
- /** @type {function} */
694
732
  el: {
695
733
  value(tag, attr, content) {
696
734
  content = isFunction(content) ? this.fn(content)() : content;
@@ -698,49 +736,75 @@ const createContext$1 = (config, methods) => {
698
736
  },
699
737
  writable: true
700
738
  },
701
- /** @type {function} */
702
739
  ui: {
703
- value(layout) {},
740
+ value() {},
741
+ writable: true
742
+ },
743
+ require: {
744
+ value() {},
745
+ writable: true
746
+ },
747
+ render: {
748
+ value() {},
704
749
  writable: true
705
750
  }
706
751
  });
707
- return EjsContext;
752
+ Object.entries(methods).forEach(_ref => {
753
+ let [name, value] = _ref;
754
+ if (isFunction(value) && globals.includes(name)) {
755
+ value = value.bind(Context.prototype);
756
+ }
757
+ Context.prototype[name] = value;
758
+ });
759
+ return Context;
708
760
  };
709
- class Context {
710
- #context;
711
- static exports = ['create', 'globals', 'helpers'];
761
+ var _context$1 = /*#__PURE__*/new WeakMap();
762
+ class EjsContext {
712
763
  constructor(options, methods) {
764
+ _classPrivateFieldInitSpec(this, _context$1, void 0);
713
765
  bindContext(this, this.constructor.exports);
714
766
  this.configure(options, methods);
715
767
  }
716
768
  create(data) {
717
- return new this.#context(data);
769
+ return new (_classPrivateFieldGet2(_context$1, this))(data);
718
770
  }
719
771
  helpers(methods) {
720
- Object.assign(this.#context.prototype, methods);
772
+ Object.assign(_classPrivateFieldGet2(_context$1, this).prototype, methods);
721
773
  }
722
774
  configure(options, methods) {
723
- this.#context = createContext$1(options, methods);
775
+ _classPrivateFieldSet2(_context$1, this, createContext$1(options, methods));
724
776
  }
725
777
  }
778
+ _defineProperty(EjsContext, "exports", ['create', 'globals', 'helpers']);
726
779
 
780
+ var _cache = /*#__PURE__*/new WeakMap();
781
+ var _context = /*#__PURE__*/new WeakMap();
782
+ var _compiler = /*#__PURE__*/new WeakMap();
783
+ var _template = /*#__PURE__*/new WeakMap();
784
+ var _config = /*#__PURE__*/new WeakMap();
785
+ var _methods = /*#__PURE__*/new WeakMap();
786
+ var _EjsInstance_brand = /*#__PURE__*/new WeakSet();
727
787
  class EjsInstance {
728
- #cache;
729
- #context;
730
- #compiler;
731
- #template;
732
- #config = {};
733
- #methods = {};
734
- static exports = ['configure', 'create', 'createContext', 'render', 'require', 'preload', 'compile', 'helpers'];
788
+ /**
789
+ *
790
+ * @param {EjsConfig} options
791
+ */
735
792
  constructor() {
736
793
  let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
794
+ _classPrivateMethodInitSpec(this, _EjsInstance_brand);
795
+ _classPrivateFieldInitSpec(this, _cache, void 0);
796
+ _classPrivateFieldInitSpec(this, _context, void 0);
797
+ _classPrivateFieldInitSpec(this, _compiler, void 0);
798
+ _classPrivateFieldInitSpec(this, _template, void 0);
799
+ _classPrivateFieldInitSpec(this, _config, {});
800
+ _classPrivateFieldInitSpec(this, _methods, {});
737
801
  bindContext(this, this.constructor.exports);
738
- this.#methods = {};
739
- this.#config = configSchema({}, options);
740
- this.#context = new Context(this.#config, this.#methods);
741
- this.#compiler = new Compiler(this.#config);
742
- this.#cache = new Cache(this.#config);
743
- this.#template = new Template(this.#config, this.#cache, this.#compiler);
802
+ _classPrivateFieldSet2(_methods, this, {});
803
+ _classPrivateFieldSet2(_config, this, configSchema({}, options));
804
+ _classPrivateFieldSet2(_context, this, new EjsContext(_classPrivateFieldGet2(_config, this), _classPrivateFieldGet2(_methods, this)));
805
+ _classPrivateFieldSet2(_compiler, this, new EjsCompiler(_classPrivateFieldGet2(_config, this)));
806
+ _classPrivateFieldSet2(_cache, this, new EjsCache(_classPrivateFieldGet2(_config, this)));
807
+ _classPrivateFieldSet2(_template, this, new EjsTemplate(_classPrivateFieldGet2(_config, this), _classPrivateFieldGet2(_cache, this), _classPrivateFieldGet2(_compiler, this)));
744
808
  this.helpers({
745
809
  render: this.render,
746
810
  require: this.require
@@ -751,69 +815,73 @@ class EjsInstance {
751
815
  }
752
816
  configure(options) {
753
817
  if (options) {
754
- configSchema(this.#config, options);
755
- this.#context.configure(this.#config, this.#methods);
756
- this.#compiler.configure(this.#config);
757
- this.#cache.configure(this.#config);
758
- this.#template.configure(this.#config);
818
+ configSchema(_classPrivateFieldGet2(_config, this), options);
819
+ _classPrivateFieldGet2(_context, this).configure(_classPrivateFieldGet2(_config, this), _classPrivateFieldGet2(_methods, this));
820
+ _classPrivateFieldGet2(_compiler, this).configure(_classPrivateFieldGet2(_config, this));
821
+ _classPrivateFieldGet2(_cache, this).configure(_classPrivateFieldGet2(_config, this));
822
+ _classPrivateFieldGet2(_template, this).configure(_classPrivateFieldGet2(_config, this));
759
823
  }
760
- return this.#config;
824
+ return _classPrivateFieldGet2(_config, this);
761
825
  }
762
826
  createContext(data) {
763
- return this.#context.create(data);
827
+ return _classPrivateFieldGet2(_context, this).create(data);
764
828
  }
765
829
  preload(list) {
766
- return this.#cache.load(list || {});
830
+ return _classPrivateFieldGet2(_cache, this).load(list || {});
767
831
  }
768
832
  compile(content, path) {
769
- return this.#compiler.compile(content, path);
833
+ return _classPrivateFieldGet2(_compiler, this).compile(content, path);
770
834
  }
771
835
  helpers(methods) {
772
- this.#context.helpers(Object.assign(this.#methods, methods));
836
+ _classPrivateFieldGet2(_context, this).helpers(Object.assign(_classPrivateFieldGet2(_methods, this), methods));
773
837
  }
774
838
  async render(name, params) {
775
839
  const data = this.createContext(params);
776
- return this.#output(this.#path(name), data).then(this.#outputContent(name, data));
840
+ return _assertClassBrand(_EjsInstance_brand, this, _output).call(this, _assertClassBrand(_EjsInstance_brand, this, _path).call(this, name), data).then(_assertClassBrand(_EjsInstance_brand, this, _outputContent).call(this, name, data));
777
841
  }
778
842
  async require(name) {
779
843
  const data = this.createContext({});
780
- return this.#output(this.#path(name), data).then(() => data.getMacro());
781
- }
782
- #path(name) {
783
- const ext = name.split('.').pop();
784
- if (ext !== this.#config.extension) {
785
- name = [name, this.#config.extension].join('.');
786
- }
787
- return name;
844
+ return _assertClassBrand(_EjsInstance_brand, this, _output).call(this, _assertClassBrand(_EjsInstance_brand, this, _path).call(this, name), data).then(() => data.getMacro());
788
845
  }
789
- #output(path, data) {
790
- return this.#template.get(path).then(callback => callback.apply(data, [data, data.useComponent, data.useElement, data.useBuffer, data.useSafeValue]));
791
- }
792
- #renderLayout(name, params, parentTemplate) {
793
- const data = this.createContext(params);
794
- if (parentTemplate) data.setParentTemplate(parentTemplate);
795
- return this.#output(this.#path(name), data).then(this.#outputContent(name, data));
796
- }
797
- #outputContent(name, data) {
798
- return content => {
799
- if (data.getExtend()) {
800
- data.setExtend(false);
801
- return this.#renderLayout(data.getLayout(), data, name);
802
- }
803
- return content;
804
- };
846
+ }
847
+ function _path(name) {
848
+ const ext = name.split('.').pop();
849
+ if (ext !== _classPrivateFieldGet2(_config, this).extension) {
850
+ name = [name, _classPrivateFieldGet2(_config, this).extension].join('.');
805
851
  }
852
+ return name;
853
+ }
854
+ function _output(path, data) {
855
+ return _classPrivateFieldGet2(_template, this).get(path).then(callback => callback.apply(data, [data, data.useComponent(), data.useElement(), data.useBuffer(), data.useEscapeValue()]));
856
+ }
857
+ function _renderLayout(name, params, parentTemplate) {
858
+ const data = this.createContext(params);
859
+ if (parentTemplate) data.setParentTemplate(parentTemplate);
860
+ return _assertClassBrand(_EjsInstance_brand, this, _output).call(this, _assertClassBrand(_EjsInstance_brand, this, _path).call(this, name), data).then(_assertClassBrand(_EjsInstance_brand, this, _outputContent).call(this, name, data));
861
+ }
862
+ function _outputContent(name, data) {
863
+ return content => {
864
+ if (data.getExtend()) {
865
+ data.setExtend(false);
866
+ return _assertClassBrand(_EjsInstance_brand, this, _renderLayout).call(this, data.getLayout(), data, name);
867
+ }
868
+ return content;
869
+ };
806
870
  }
871
+ _defineProperty(EjsInstance, "exports", ['configure', 'create', 'createContext', 'render', 'require', 'preload', 'compile', 'helpers']);
807
872
 
808
- /**
809
- * @type {{[p:string]:Function}}
810
- */
811
873
  const templateCache = {};
812
874
  const getOrigin = (url, secure) => {
813
875
  url = URL.parse(url);
814
876
  url.protocol = secure ? 'https:' : 'http:';
815
877
  return url.origin;
816
878
  };
879
+ const resolver = async (path, name, error) => {
880
+ if (isFunction(templateCache[name])) {
881
+ return templateCache[name];
882
+ }
883
+ error(1, "template ".concat(name, " not found"));
884
+ };
817
885
  const {
818
886
  render,
819
887
  createContext,
@@ -822,43 +890,21 @@ const {
822
890
  } = new EjsInstance({
823
891
  cache: false,
824
892
  strict: true,
825
- async resolver(path, name) {
826
- if (isFunction(templateCache[name])) {
827
- return templateCache[name];
828
- } else {
829
- throw new TemplateNotFound(`template ${name} not found`);
830
- }
831
- }
893
+ resolver
832
894
  });
833
-
834
- /**
835
- * @param {{[p:string],Function}} templates
836
- */
837
895
  function useTemplates() {
838
896
  let templates = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
839
897
  Object.assign(templateCache, templates);
840
898
  }
841
-
842
- /**
843
- * @typedef {{[p:string]:any}} HonoContext
844
- * @property {function(*):Promise<Response>} html
845
- * @property {function():Promise<Response>} notFound
846
- * @property {function(methods:{}):void} helpers
847
- * @property {function(name:string,data:{}):Promise<string>} render
848
- * @property {function(name:string,data:{}):Promise<string>} ejs
849
- * @property {EjsContext} data
850
- */
851
- /**
852
- * @param {RendererParams} options
853
- * @return {(function(c:HonoContext, next): Promise<any>)|*}
854
- */
855
899
  function useRenderer() {
900
+ var _options$templates;
856
901
  let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
857
- useTemplates(options.templates ?? {});
902
+ useTemplates((_options$templates = options.templates) !== null && _options$templates !== void 0 ? _options$templates : {});
858
903
  return async (c, next) => {
904
+ var _options$secure;
859
905
  c.data = createContext({});
860
906
  c.data.set('version', options.version);
861
- c.data.set('origin', getOrigin(c.req.url, options.secure ?? true));
907
+ c.data.set('origin', getOrigin(c.req.url, (_options$secure = options.secure) !== null && _options$secure !== void 0 ? _options$secure : true));
862
908
  c.data.set('path', c.req.path);
863
909
  c.data.set('query', c.req.query());
864
910
  c.ejs = async (name, data) => render(name, Object.assign({
@@ -870,9 +916,6 @@ function useRenderer() {
870
916
  };
871
917
  }
872
918
 
873
- exports.TemplateError = TemplateError;
874
- exports.TemplateNotFound = TemplateNotFound;
875
- exports.TemplateSyntaxError = TemplateSyntaxError;
876
919
  exports.configure = configure;
877
920
  exports.createContext = createContext;
878
921
  exports.helpers = helpers;