@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.
@@ -149,7 +149,7 @@ const symbols = (string) => {
149
149
  )
150
150
  };
151
151
 
152
- const safeValue = (value, escape) => {
152
+ const escapeValue = (value, escape) => {
153
153
  const check = value;
154
154
  return check == null
155
155
  ? ''
@@ -209,12 +209,27 @@ const bindContext = (object, methods = []) => {
209
209
  }
210
210
  };
211
211
 
212
- class Template {
212
+ class EjsError extends Error {
213
+ constructor(code, content) {
214
+ super(content);
215
+ this.code = code;
216
+ if (content instanceof Error) {
217
+ this.stack = content.stack;
218
+ this.message = content.message;
219
+ }
220
+ }
221
+ }
222
+
223
+ const error = (code, content) => {
224
+ throw new EjsError(code, content)
225
+ };
226
+
227
+ class EjsTemplate {
213
228
  #path
214
229
  #resolver
215
230
  #cache
216
231
  #compiler
217
- static exports = ['configure', 'get', 'compile']
232
+ static exports = ['configure', 'get']
218
233
  constructor(options, cache, compiler) {
219
234
  bindContext(this, this.constructor.exports);
220
235
  this.#cache = cache;
@@ -230,11 +245,13 @@ class Template {
230
245
  #resolve(template) {
231
246
  const cached = this.#cache.get(template);
232
247
  if (cached instanceof Promise) return cached
233
- const result = Promise.resolve(this.#resolver(this.#path, template));
248
+ const result = Promise.resolve(
249
+ this.#resolver(this.#path, template, error),
250
+ );
234
251
  this.#cache.set(template, result);
235
252
  return result
236
253
  }
237
- compile(content, template) {
254
+ #compile(content, template) {
238
255
  const cached = this.#cache.get(template);
239
256
  if (typeof cached === 'function') return cached
240
257
  if (typeof content === 'string') {
@@ -246,9 +263,9 @@ class Template {
246
263
  }
247
264
  }
248
265
  get(template) {
249
- return this.#resolve(template).then((content) =>
250
- this.compile(content, template),
251
- )
266
+ return this.#resolve(template).then((content) => {
267
+ return this.#compile(content, template)
268
+ })
252
269
  }
253
270
  }
254
271
 
@@ -271,15 +288,13 @@ const tokensMatch = (regex, content, callback) => {
271
288
  });
272
289
  };
273
290
 
274
- class Compiler {
291
+ class EjsCompiler {
275
292
  #config = {}
276
- static exports = ['compile']
277
-
293
+ static exports = ['configure', 'compile']
278
294
  constructor(options) {
279
295
  bindContext(this, this.constructor.exports);
280
296
  this.configure(options);
281
297
  }
282
-
283
298
  configure(options) {
284
299
  this.#config.strict = options.strict;
285
300
  this.#config.rmWhitespace = options.rmWhitespace;
@@ -354,7 +369,7 @@ class Compiler {
354
369
  });
355
370
  });
356
371
  OUTPUT += `');`;
357
- OUTPUT = `try{${OUTPUT}}catch(e){return ${BUFFER}.error(e,'${path}')}`;
372
+ OUTPUT = `try{${OUTPUT}}catch(e){return ${BUFFER}.error(e)}`;
358
373
  if (this.#config.strict === false) {
359
374
  OUTPUT = `with(${SCOPE}){${OUTPUT}}`;
360
375
  }
@@ -376,7 +391,7 @@ class Compiler {
376
391
  }
377
392
  }
378
393
 
379
- class Cache {
394
+ class EjsCache {
380
395
  static exports = [
381
396
  'load',
382
397
  'set',
@@ -417,13 +432,11 @@ class Cache {
417
432
  resolve(key) {
418
433
  return Promise.resolve(this.get(key))
419
434
  }
420
-
421
435
  load(data) {
422
436
  if (this.#cache) {
423
437
  Object.assign(this.#list, data || {});
424
438
  }
425
439
  }
426
-
427
440
  configure(options) {
428
441
  this.#cache = options.cache;
429
442
  this.#precompiled = options.precompiled;
@@ -482,36 +495,16 @@ const element = (tag, attrs, content) => {
482
495
  return result.join('')
483
496
  };
484
497
 
485
- class TemplateError extends Error {
486
- name = 'TemplateError'
487
- constructor(error) {
488
- super(error);
489
- if (error instanceof Error) {
490
- this.stack = error.stack;
491
- this.filename = error.filename;
492
- this.lineno = error.lineno;
493
- }
494
- }
495
- }
496
-
497
- class TemplateNotFound extends TemplateError {
498
- name = 'TemplateNotFound'
499
- code = 404
500
- }
501
-
502
- class TemplateSyntaxError extends TemplateError {
503
- name = 'TemplateSyntaxError'
504
- code = 500
505
- }
506
-
507
498
  const resolve = (list) => {
508
499
  return Promise.all(list || [])
509
500
  .then((list) => list.join(''))
510
- .catch((e) => e)
501
+ .catch((e) => {
502
+ return error(500, e)
503
+ })
511
504
  };
512
505
 
513
- const reject = (error) => {
514
- return Promise.reject(new TemplateSyntaxError(error))
506
+ const reject = (e) => {
507
+ return Promise.reject(error(500, e))
515
508
  };
516
509
 
517
510
  const EjsBuffer = () => {
@@ -537,7 +530,7 @@ const EjsBuffer = () => {
537
530
  array = store.pop();
538
531
  return resolve(result)
539
532
  };
540
- EjsBuffer.error = (e, filename) => {
533
+ EjsBuffer.error = (e) => {
541
534
  return reject(e)
542
535
  };
543
536
  EjsBuffer.end = () => {
@@ -570,12 +563,6 @@ const createContext$1 = (config, methods) => {
570
563
  omit(data, [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT]),
571
564
  );
572
565
  }
573
- Object.entries(methods).forEach(([name, value]) => {
574
- if (isFunction(value) && globals.includes(name)) {
575
- value = value.bind(EjsContext.prototype);
576
- }
577
- EjsContext.prototype[name] = value;
578
- });
579
566
  Object.defineProperty(EjsContext.prototype, BUFFER, {
580
567
  value: EjsBuffer(),
581
568
  });
@@ -600,36 +587,31 @@ const createContext$1 = (config, methods) => {
600
587
  writable: true,
601
588
  });
602
589
  Object.defineProperties(EjsContext.prototype, {
603
- /** @type {function} */
604
590
  setParentTemplate: {
605
591
  value(value) {
606
592
  this[PARENT] = value;
607
593
  return this
608
594
  },
609
595
  },
610
- /** @type {function} */
611
596
  getParentTemplate: {
612
597
  value() {
613
598
  return this[PARENT]
614
599
  },
615
600
  },
616
- /** @type {function} */
617
- useSafeValue: {
618
- get: () => safeValue,
601
+ useEscapeValue: {
602
+ get: () => escapeValue,
619
603
  },
620
- /** @type {function} */
621
604
  useComponent: {
622
605
  get() {
623
606
  if (isFunction(this[COMPONENT])) {
624
607
  return this[COMPONENT].bind(this)
625
608
  } else {
626
- return () => {
609
+ return function () {
627
610
  throw new Error(`${COMPONENT} must be a function`)
628
611
  }
629
612
  }
630
613
  },
631
614
  },
632
- /** @type {function} */
633
615
  useElement: {
634
616
  get() {
635
617
  if (isFunction(this[ELEMENT])) {
@@ -641,51 +623,43 @@ const createContext$1 = (config, methods) => {
641
623
  }
642
624
  },
643
625
  },
644
- /** @type {function} */
645
626
  useBuffer: {
646
627
  get() {
647
628
  return this[BUFFER]
648
629
  },
649
630
  },
650
- /** @type {function} */
651
631
  getMacro: {
652
632
  value() {
653
633
  return this[MACRO]
654
634
  },
655
635
  },
656
- /** @type {function} */
657
636
  getBlocks: {
658
637
  value() {
659
638
  return this[BLOCKS]
660
639
  },
661
640
  },
662
- /** @type {function} */
663
641
  setExtend: {
664
642
  value(value) {
665
643
  this[EXTEND] = value;
666
644
  return this
667
645
  },
668
646
  },
669
- /** @type {function} */
670
647
  getExtend: {
671
648
  value() {
672
649
  return this[EXTEND]
673
650
  },
674
651
  },
675
- /** @type {function} */
676
652
  setLayout: {
677
653
  value(layout) {
678
654
  this[LAYOUT] = layout;
679
655
  return this
680
656
  },
681
657
  },
682
- /** @type {function} */
683
658
  getLayout: {
684
659
  value() {
685
660
  return this[LAYOUT]
686
661
  },
687
662
  },
688
- /** @type {function} */
689
663
  clone: {
690
664
  value(exclude_blocks) {
691
665
  const filter = [LAYOUT, EXTEND, BUFFER];
@@ -695,32 +669,29 @@ const createContext$1 = (config, methods) => {
695
669
  return omit(this, filter)
696
670
  },
697
671
  },
698
- /** @type {function} */
699
672
  extend: {
700
673
  value(layout) {
701
674
  this.setExtend(true);
702
675
  this.setLayout(layout);
703
676
  },
704
677
  },
705
- /** @type {function} */
706
678
  echo: {
707
679
  value() {
708
680
  return [].slice.call(arguments).forEach(this.useBuffer)
709
681
  },
710
682
  },
711
- /** @type {function} */
712
683
  fn: {
713
684
  value(callback) {
714
- return () => {
685
+ const context = this;
686
+ return function () {
715
687
  if (isFunction(callback)) {
716
- this.useBuffer.backup();
717
- this.useBuffer(callback.apply(this, arguments));
718
- return this.useBuffer.restore()
688
+ context.useBuffer.backup();
689
+ context.useBuffer(callback.apply(context, arguments));
690
+ return context.useBuffer.restore()
719
691
  }
720
692
  }
721
693
  },
722
694
  },
723
- /** @type {function} */
724
695
  macro: {
725
696
  value(name, callback) {
726
697
  const list = this.getMacro();
@@ -731,7 +702,6 @@ const createContext$1 = (config, methods) => {
731
702
  };
732
703
  },
733
704
  },
734
- /** @type {function} */
735
705
  call: {
736
706
  value(name) {
737
707
  const list = this.getMacro();
@@ -742,35 +712,35 @@ const createContext$1 = (config, methods) => {
742
712
  }
743
713
  },
744
714
  },
745
- /** @type {function} */
746
715
  block: {
747
716
  value(name, callback) {
748
717
  const blocks = this.getBlocks();
749
718
  blocks[name] = blocks[name] || [];
750
719
  blocks[name].push(this.fn(callback));
751
720
  if (this.getExtend()) return
721
+ const context = this;
752
722
  const list = Object.assign([], blocks[name]);
753
- const shift = () => list.shift();
754
- const next = () => {
723
+ const shift = function () {
724
+ return list.shift()
725
+ };
726
+ const next = function () {
755
727
  const parent = shift();
756
728
  if (parent) {
757
- return () => {
758
- this.echo(parent(next()));
729
+ return function () {
730
+ context.echo(parent(next()));
759
731
  }
760
732
  } else {
761
- return () => {}
733
+ return function () {}
762
734
  }
763
735
  };
764
736
  this.echo(shift()(next()));
765
737
  },
766
738
  },
767
- /** @type {function} */
768
739
  hasBlock: {
769
740
  value(name) {
770
741
  return this.getBlocks().hasOwnProperty(name)
771
742
  },
772
743
  },
773
- /** @type {function} */
774
744
  include: {
775
745
  value(path, data, cx) {
776
746
  const context = cx === false ? {} : this.clone(true);
@@ -779,7 +749,6 @@ const createContext$1 = (config, methods) => {
779
749
  this.echo(promise);
780
750
  },
781
751
  },
782
- /** @type {function} */
783
752
  use: {
784
753
  value(path, namespace) {
785
754
  this.echo(
@@ -792,13 +761,6 @@ const createContext$1 = (config, methods) => {
792
761
  );
793
762
  },
794
763
  },
795
- /** @type {function} */
796
- async: {
797
- value(promise, callback) {
798
- this.echo(Promise.resolve(promise).then(callback));
799
- },
800
- },
801
- /** @type {function} */
802
764
  get: {
803
765
  value(name, defaults) {
804
766
  const path = getPath(this, name, true);
@@ -807,7 +769,6 @@ const createContext$1 = (config, methods) => {
807
769
  return hasProp(result, prop) ? result[prop] : defaults
808
770
  },
809
771
  },
810
- /** @type {function} */
811
772
  set: {
812
773
  value(name, value) {
813
774
  const path = getPath(this, name, false);
@@ -819,7 +780,6 @@ const createContext$1 = (config, methods) => {
819
780
  return (result[prop] = value)
820
781
  },
821
782
  },
822
- /** @type {function} */
823
783
  each: {
824
784
  value(object, callback) {
825
785
  if (isString(object)) {
@@ -829,7 +789,6 @@ const createContext$1 = (config, methods) => {
829
789
  },
830
790
  writable: true,
831
791
  },
832
- /** @type {function} */
833
792
  el: {
834
793
  value(tag, attr, content) {
835
794
  content = isFunction(content) ? this.fn(content)() : content;
@@ -841,16 +800,21 @@ const createContext$1 = (config, methods) => {
841
800
  },
842
801
  writable: true,
843
802
  },
844
- /** @type {function} */
845
803
  ui: {
846
- value(layout) {},
804
+ value() {},
847
805
  writable: true,
848
806
  },
849
807
  });
808
+ Object.entries(methods).forEach(([name, value]) => {
809
+ if (isFunction(value) && globals.includes(name)) {
810
+ value = value.bind(EjsContext.prototype);
811
+ }
812
+ EjsContext.prototype[name] = value;
813
+ });
850
814
  return EjsContext
851
815
  };
852
816
 
853
- class Context {
817
+ class EjsContext {
854
818
  #context
855
819
  static exports = ['create', 'globals', 'helpers']
856
820
  constructor(options, methods) {
@@ -889,10 +853,14 @@ class EjsInstance {
889
853
  bindContext(this, this.constructor.exports);
890
854
  this.#methods = {};
891
855
  this.#config = configSchema({}, options);
892
- this.#context = new Context(this.#config, this.#methods);
893
- this.#compiler = new Compiler(this.#config);
894
- this.#cache = new Cache(this.#config);
895
- this.#template = new Template(this.#config, this.#cache, this.#compiler);
856
+ this.#context = new EjsContext(this.#config, this.#methods);
857
+ this.#compiler = new EjsCompiler(this.#config);
858
+ this.#cache = new EjsCache(this.#config);
859
+ this.#template = new EjsTemplate(
860
+ this.#config,
861
+ this.#cache,
862
+ this.#compiler,
863
+ );
896
864
  this.helpers({ render: this.render, require: this.require });
897
865
  }
898
866
  create(options) {
@@ -946,7 +914,7 @@ class EjsInstance {
946
914
  data.useComponent,
947
915
  data.useElement,
948
916
  data.useBuffer,
949
- data.useSafeValue,
917
+ data.useEscapeValue,
950
918
  ]),
951
919
  )
952
920
  }
@@ -982,12 +950,11 @@ const getOrigin = (url, secure) => {
982
950
  const { render, createContext, helpers, configure } = new EjsInstance({
983
951
  cache: false,
984
952
  strict: true,
985
- async resolver(path, name) {
953
+ async resolver(path, name, error) {
986
954
  if (isFunction(templateCache[name])) {
987
955
  return templateCache[name]
988
- } else {
989
- throw new TemplateNotFound(`template ${name} not found`)
990
956
  }
957
+ error(404, `template ${name} not found`);
991
958
  },
992
959
  });
993
960
 
@@ -1027,4 +994,4 @@ function useRenderer(options = {}) {
1027
994
  }
1028
995
  }
1029
996
 
1030
- export { TemplateError, TemplateNotFound, TemplateSyntaxError, configure, createContext, helpers, render, useRenderer, useTemplates };
997
+ export { configure, createContext, helpers, render, useRenderer, useTemplates };
Binary file