@kosatyi/ejs 0.0.108 → 0.0.110

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/dist/cjs/index.js CHANGED
@@ -95,7 +95,7 @@ const entities = function () {
95
95
  const symbols = string => {
96
96
  return ('' + string).replace(symbolEntitiesMatch, match => '\\' + symbolEntities[match]);
97
97
  };
98
- const safeValue = (value, escape) => {
98
+ const escapeValue = (value, escape) => {
99
99
  const check = value;
100
100
  return check == null ? '' : Boolean(escape) === true ? entities(check) : check;
101
101
  };
@@ -154,12 +154,26 @@ const bindContext = function (object) {
154
154
  }
155
155
  };
156
156
 
157
- class Template {
157
+ class EjsError extends Error {
158
+ constructor(code, content) {
159
+ super(content);
160
+ this.code = code;
161
+ if (content instanceof Error) {
162
+ this.stack = content.stack;
163
+ this.message = content.message;
164
+ }
165
+ }
166
+ }
167
+ const error = (code, content) => {
168
+ throw new EjsError(code, content);
169
+ };
170
+
171
+ class EjsTemplate {
158
172
  #path;
159
173
  #resolver;
160
174
  #cache;
161
175
  #compiler;
162
- static exports = ['configure', 'get', 'compile'];
176
+ static exports = ['configure', 'get'];
163
177
  constructor(options, cache, compiler) {
164
178
  bindContext(this, this.constructor.exports);
165
179
  this.#cache = cache;
@@ -175,11 +189,11 @@ class Template {
175
189
  #resolve(template) {
176
190
  const cached = this.#cache.get(template);
177
191
  if (cached instanceof Promise) return cached;
178
- const result = Promise.resolve(this.#resolver(this.#path, template));
192
+ const result = Promise.resolve(this.#resolver(this.#path, template, error));
179
193
  this.#cache.set(template, result);
180
194
  return result;
181
195
  }
182
- compile(content, template) {
196
+ #compile(content, template) {
183
197
  const cached = this.#cache.get(template);
184
198
  if (typeof cached === 'function') return cached;
185
199
  if (typeof content === 'string') {
@@ -191,7 +205,9 @@ class Template {
191
205
  }
192
206
  }
193
207
  get(template) {
194
- return this.#resolve(template).then(content => this.compile(content, template));
208
+ return this.#resolve(template).then(content => {
209
+ return this.#compile(content, template);
210
+ });
195
211
  }
196
212
  }
197
213
 
@@ -207,9 +223,9 @@ const tokensMatch = (regex, content, callback) => {
207
223
  return match;
208
224
  });
209
225
  };
210
- class Compiler {
226
+ class EjsCompiler {
211
227
  #config = {};
212
- static exports = ['compile'];
228
+ static exports = ['configure', 'compile'];
213
229
  constructor(options) {
214
230
  bindContext(this, this.constructor.exports);
215
231
  this.configure(options);
@@ -266,7 +282,7 @@ class Compiler {
266
282
  });
267
283
  });
268
284
  OUTPUT += `');`;
269
- OUTPUT = `try{${OUTPUT}}catch(e){return ${BUFFER}.error(e,'${path}')}`;
285
+ OUTPUT = `try{${OUTPUT}}catch(e){return ${BUFFER}.error(e)}`;
270
286
  if (this.#config.strict === false) {
271
287
  OUTPUT = `with(${SCOPE}){${OUTPUT}}`;
272
288
  }
@@ -288,7 +304,7 @@ class Compiler {
288
304
  }
289
305
  }
290
306
 
291
- class Cache {
307
+ class EjsCache {
292
308
  static exports = ['load', 'set', 'get', 'exist', 'clear', 'remove', 'resolve'];
293
309
  #cache = true;
294
310
  #precompiled;
@@ -362,31 +378,13 @@ const element = (tag, attrs, content) => {
362
378
  return result.join('');
363
379
  };
364
380
 
365
- class TemplateError extends Error {
366
- name = 'TemplateError';
367
- constructor(error) {
368
- super(error);
369
- if (error instanceof Error) {
370
- this.stack = error.stack;
371
- this.filename = error.filename;
372
- this.lineno = error.lineno;
373
- }
374
- }
375
- }
376
- class TemplateNotFound extends TemplateError {
377
- name = 'TemplateNotFound';
378
- code = 404;
379
- }
380
- class TemplateSyntaxError extends TemplateError {
381
- name = 'TemplateSyntaxError';
382
- code = 500;
383
- }
384
-
385
381
  const resolve = list => {
386
- return Promise.all(list || []).then(list => list.join('')).catch(e => e);
382
+ return Promise.all(list || []).then(list => list.join('')).catch(e => {
383
+ return error(500, e);
384
+ });
387
385
  };
388
- const reject = error => {
389
- return Promise.reject(new TemplateSyntaxError(error));
386
+ const reject = e => {
387
+ return Promise.reject(error(500, e));
390
388
  };
391
389
  const EjsBuffer = () => {
392
390
  let store = [];
@@ -411,7 +409,7 @@ const EjsBuffer = () => {
411
409
  array = store.pop();
412
410
  return resolve(result);
413
411
  };
414
- EjsBuffer.error = (e, filename) => {
412
+ EjsBuffer.error = e => {
415
413
  return reject(e);
416
414
  };
417
415
  EjsBuffer.end = () => {
@@ -440,13 +438,6 @@ const createContext$1 = (config, methods) => {
440
438
  this[MACRO] = {};
441
439
  Object.assign(this, omit(data, [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT]));
442
440
  }
443
- Object.entries(methods).forEach(_ref => {
444
- let [name, value] = _ref;
445
- if (isFunction(value) && globals.includes(name)) {
446
- value = value.bind(EjsContext.prototype);
447
- }
448
- EjsContext.prototype[name] = value;
449
- });
450
441
  Object.defineProperty(EjsContext.prototype, BUFFER, {
451
442
  value: EjsBuffer()
452
443
  });
@@ -471,36 +462,31 @@ const createContext$1 = (config, methods) => {
471
462
  writable: true
472
463
  });
473
464
  Object.defineProperties(EjsContext.prototype, {
474
- /** @type {function} */
475
465
  setParentTemplate: {
476
466
  value(value) {
477
467
  this[PARENT] = value;
478
468
  return this;
479
469
  }
480
470
  },
481
- /** @type {function} */
482
471
  getParentTemplate: {
483
472
  value() {
484
473
  return this[PARENT];
485
474
  }
486
475
  },
487
- /** @type {function} */
488
- useSafeValue: {
489
- get: () => safeValue
476
+ useEscapeValue: {
477
+ get: () => escapeValue
490
478
  },
491
- /** @type {function} */
492
479
  useComponent: {
493
480
  get() {
494
481
  if (isFunction(this[COMPONENT])) {
495
482
  return this[COMPONENT].bind(this);
496
483
  } else {
497
- return () => {
484
+ return function () {
498
485
  throw new Error(`${COMPONENT} must be a function`);
499
486
  };
500
487
  }
501
488
  }
502
489
  },
503
- /** @type {function} */
504
490
  useElement: {
505
491
  get() {
506
492
  if (isFunction(this[ELEMENT])) {
@@ -512,51 +498,43 @@ const createContext$1 = (config, methods) => {
512
498
  }
513
499
  }
514
500
  },
515
- /** @type {function} */
516
501
  useBuffer: {
517
502
  get() {
518
503
  return this[BUFFER];
519
504
  }
520
505
  },
521
- /** @type {function} */
522
506
  getMacro: {
523
507
  value() {
524
508
  return this[MACRO];
525
509
  }
526
510
  },
527
- /** @type {function} */
528
511
  getBlocks: {
529
512
  value() {
530
513
  return this[BLOCKS];
531
514
  }
532
515
  },
533
- /** @type {function} */
534
516
  setExtend: {
535
517
  value(value) {
536
518
  this[EXTEND] = value;
537
519
  return this;
538
520
  }
539
521
  },
540
- /** @type {function} */
541
522
  getExtend: {
542
523
  value() {
543
524
  return this[EXTEND];
544
525
  }
545
526
  },
546
- /** @type {function} */
547
527
  setLayout: {
548
528
  value(layout) {
549
529
  this[LAYOUT] = layout;
550
530
  return this;
551
531
  }
552
532
  },
553
- /** @type {function} */
554
533
  getLayout: {
555
534
  value() {
556
535
  return this[LAYOUT];
557
536
  }
558
537
  },
559
- /** @type {function} */
560
538
  clone: {
561
539
  value(exclude_blocks) {
562
540
  const filter = [LAYOUT, EXTEND, BUFFER];
@@ -566,32 +544,29 @@ const createContext$1 = (config, methods) => {
566
544
  return omit(this, filter);
567
545
  }
568
546
  },
569
- /** @type {function} */
570
547
  extend: {
571
548
  value(layout) {
572
549
  this.setExtend(true);
573
550
  this.setLayout(layout);
574
551
  }
575
552
  },
576
- /** @type {function} */
577
553
  echo: {
578
554
  value() {
579
555
  return [].slice.call(arguments).forEach(this.useBuffer);
580
556
  }
581
557
  },
582
- /** @type {function} */
583
558
  fn: {
584
559
  value(callback) {
585
- return () => {
560
+ const context = this;
561
+ return function () {
586
562
  if (isFunction(callback)) {
587
- this.useBuffer.backup();
588
- this.useBuffer(callback.apply(this, arguments));
589
- return this.useBuffer.restore();
563
+ context.useBuffer.backup();
564
+ context.useBuffer(callback.apply(context, arguments));
565
+ return context.useBuffer.restore();
590
566
  }
591
567
  };
592
568
  }
593
569
  },
594
- /** @type {function} */
595
570
  macro: {
596
571
  value(name, callback) {
597
572
  const list = this.getMacro();
@@ -602,7 +577,6 @@ const createContext$1 = (config, methods) => {
602
577
  };
603
578
  }
604
579
  },
605
- /** @type {function} */
606
580
  call: {
607
581
  value(name) {
608
582
  const list = this.getMacro();
@@ -613,35 +587,35 @@ const createContext$1 = (config, methods) => {
613
587
  }
614
588
  }
615
589
  },
616
- /** @type {function} */
617
590
  block: {
618
591
  value(name, callback) {
619
592
  const blocks = this.getBlocks();
620
593
  blocks[name] = blocks[name] || [];
621
594
  blocks[name].push(this.fn(callback));
622
595
  if (this.getExtend()) return;
596
+ const context = this;
623
597
  const list = Object.assign([], blocks[name]);
624
- const shift = () => list.shift();
625
- const next = () => {
598
+ const shift = function () {
599
+ return list.shift();
600
+ };
601
+ const next = function () {
626
602
  const parent = shift();
627
603
  if (parent) {
628
- return () => {
629
- this.echo(parent(next()));
604
+ return function () {
605
+ context.echo(parent(next()));
630
606
  };
631
607
  } else {
632
- return () => {};
608
+ return function () {};
633
609
  }
634
610
  };
635
611
  this.echo(shift()(next()));
636
612
  }
637
613
  },
638
- /** @type {function} */
639
614
  hasBlock: {
640
615
  value(name) {
641
616
  return this.getBlocks().hasOwnProperty(name);
642
617
  }
643
618
  },
644
- /** @type {function} */
645
619
  include: {
646
620
  value(path, data, cx) {
647
621
  const context = cx === false ? {} : this.clone(true);
@@ -650,7 +624,6 @@ const createContext$1 = (config, methods) => {
650
624
  this.echo(promise);
651
625
  }
652
626
  },
653
- /** @type {function} */
654
627
  use: {
655
628
  value(path, namespace) {
656
629
  this.echo(Promise.resolve(this.require(path)).then(exports$1 => {
@@ -661,13 +634,6 @@ const createContext$1 = (config, methods) => {
661
634
  }));
662
635
  }
663
636
  },
664
- /** @type {function} */
665
- async: {
666
- value(promise, callback) {
667
- this.echo(Promise.resolve(promise).then(callback));
668
- }
669
- },
670
- /** @type {function} */
671
637
  get: {
672
638
  value(name, defaults) {
673
639
  const path = getPath(this, name, true);
@@ -676,7 +642,6 @@ const createContext$1 = (config, methods) => {
676
642
  return hasProp(result, prop) ? result[prop] : defaults;
677
643
  }
678
644
  },
679
- /** @type {function} */
680
645
  set: {
681
646
  value(name, value) {
682
647
  const path = getPath(this, name, false);
@@ -688,7 +653,6 @@ const createContext$1 = (config, methods) => {
688
653
  return result[prop] = value;
689
654
  }
690
655
  },
691
- /** @type {function} */
692
656
  each: {
693
657
  value(object, callback) {
694
658
  if (isString(object)) {
@@ -698,7 +662,6 @@ const createContext$1 = (config, methods) => {
698
662
  },
699
663
  writable: true
700
664
  },
701
- /** @type {function} */
702
665
  el: {
703
666
  value(tag, attr, content) {
704
667
  content = isFunction(content) ? this.fn(content)() : content;
@@ -706,15 +669,21 @@ const createContext$1 = (config, methods) => {
706
669
  },
707
670
  writable: true
708
671
  },
709
- /** @type {function} */
710
672
  ui: {
711
- value(layout) {},
673
+ value() {},
712
674
  writable: true
713
675
  }
714
676
  });
677
+ Object.entries(methods).forEach(_ref => {
678
+ let [name, value] = _ref;
679
+ if (isFunction(value) && globals.includes(name)) {
680
+ value = value.bind(EjsContext.prototype);
681
+ }
682
+ EjsContext.prototype[name] = value;
683
+ });
715
684
  return EjsContext;
716
685
  };
717
- class Context {
686
+ class EjsContext {
718
687
  #context;
719
688
  static exports = ['create', 'globals', 'helpers'];
720
689
  constructor(options, methods) {
@@ -745,10 +714,10 @@ class EjsInstance {
745
714
  bindContext(this, this.constructor.exports);
746
715
  this.#methods = {};
747
716
  this.#config = configSchema({}, options);
748
- this.#context = new Context(this.#config, this.#methods);
749
- this.#compiler = new Compiler(this.#config);
750
- this.#cache = new Cache(this.#config);
751
- this.#template = new Template(this.#config, this.#cache, this.#compiler);
717
+ this.#context = new EjsContext(this.#config, this.#methods);
718
+ this.#compiler = new EjsCompiler(this.#config);
719
+ this.#cache = new EjsCache(this.#config);
720
+ this.#template = new EjsTemplate(this.#config, this.#cache, this.#compiler);
752
721
  this.helpers({
753
722
  render: this.render,
754
723
  require: this.require
@@ -795,7 +764,7 @@ class EjsInstance {
795
764
  return name;
796
765
  }
797
766
  #output(path, data) {
798
- return this.#template.get(path).then(callback => callback.apply(data, [data, data.useComponent, data.useElement, data.useBuffer, data.useSafeValue]));
767
+ return this.#template.get(path).then(callback => callback.apply(data, [data, data.useComponent, data.useElement, data.useBuffer, data.useEscapeValue]));
799
768
  }
800
769
  #renderLayout(name, params, parentTemplate) {
801
770
  const data = this.createContext(params);
@@ -842,26 +811,29 @@ const expressRenderer = (configure, render) => {
842
811
  };
843
812
  };
844
813
 
845
- const readFile = (path, template) => {
846
- return fs.readFile(joinPath(path, template)).then(contents => contents.toString()).then(text => String(text));
814
+ const readFile = (path, template, error) => {
815
+ return fs.readFile(joinPath(path, template)).then(contents => contents.toString()).catch(reason => {
816
+ if (reason.code === 'ENOENT') {
817
+ error(404, `template ${template} not found`);
818
+ } else {
819
+ error(500, reason);
820
+ }
821
+ });
847
822
  };
848
823
 
849
824
  const {
850
825
  render,
851
- createContext,
852
- compile,
853
826
  helpers,
854
- preload,
855
827
  configure,
856
- create
828
+ create,
829
+ createContext,
830
+ compile,
831
+ preload
857
832
  } = new EjsInstance({
858
833
  resolver: readFile
859
834
  });
860
835
  const __express = expressRenderer(configure, render);
861
836
 
862
- exports.TemplateError = TemplateError;
863
- exports.TemplateNotFound = TemplateNotFound;
864
- exports.TemplateSyntaxError = TemplateSyntaxError;
865
837
  exports.__express = __express;
866
838
  exports.compile = compile;
867
839
  exports.configure = configure;