@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.
@@ -92,7 +92,7 @@ const entities = function () {
92
92
  const symbols = string => {
93
93
  return ('' + string).replace(symbolEntitiesMatch, match => '\\' + symbolEntities[match]);
94
94
  };
95
- const safeValue = (value, escape) => {
95
+ const escapeValue = (value, escape) => {
96
96
  const check = value;
97
97
  return check == null ? '' : Boolean(escape) === true ? entities(check) : check;
98
98
  };
@@ -151,12 +151,26 @@ const bindContext = function (object) {
151
151
  }
152
152
  };
153
153
 
154
- class Template {
154
+ class EjsError extends Error {
155
+ constructor(code, content) {
156
+ super(content);
157
+ this.code = code;
158
+ if (content instanceof Error) {
159
+ this.stack = content.stack;
160
+ this.message = content.message;
161
+ }
162
+ }
163
+ }
164
+ const error = (code, content) => {
165
+ throw new EjsError(code, content);
166
+ };
167
+
168
+ class EjsTemplate {
155
169
  #path;
156
170
  #resolver;
157
171
  #cache;
158
172
  #compiler;
159
- static exports = ['configure', 'get', 'compile'];
173
+ static exports = ['configure', 'get'];
160
174
  constructor(options, cache, compiler) {
161
175
  bindContext(this, this.constructor.exports);
162
176
  this.#cache = cache;
@@ -172,11 +186,11 @@ class Template {
172
186
  #resolve(template) {
173
187
  const cached = this.#cache.get(template);
174
188
  if (cached instanceof Promise) return cached;
175
- const result = Promise.resolve(this.#resolver(this.#path, template));
189
+ const result = Promise.resolve(this.#resolver(this.#path, template, error));
176
190
  this.#cache.set(template, result);
177
191
  return result;
178
192
  }
179
- compile(content, template) {
193
+ #compile(content, template) {
180
194
  const cached = this.#cache.get(template);
181
195
  if (typeof cached === 'function') return cached;
182
196
  if (typeof content === 'string') {
@@ -188,7 +202,9 @@ class Template {
188
202
  }
189
203
  }
190
204
  get(template) {
191
- return this.#resolve(template).then(content => this.compile(content, template));
205
+ return this.#resolve(template).then(content => {
206
+ return this.#compile(content, template);
207
+ });
192
208
  }
193
209
  }
194
210
 
@@ -204,9 +220,9 @@ const tokensMatch = (regex, content, callback) => {
204
220
  return match;
205
221
  });
206
222
  };
207
- class Compiler {
223
+ class EjsCompiler {
208
224
  #config = {};
209
- static exports = ['compile'];
225
+ static exports = ['configure', 'compile'];
210
226
  constructor(options) {
211
227
  bindContext(this, this.constructor.exports);
212
228
  this.configure(options);
@@ -263,7 +279,7 @@ class Compiler {
263
279
  });
264
280
  });
265
281
  OUTPUT += `');`;
266
- OUTPUT = `try{${OUTPUT}}catch(e){return ${BUFFER}.error(e,'${path}')}`;
282
+ OUTPUT = `try{${OUTPUT}}catch(e){return ${BUFFER}.error(e)}`;
267
283
  if (this.#config.strict === false) {
268
284
  OUTPUT = `with(${SCOPE}){${OUTPUT}}`;
269
285
  }
@@ -285,7 +301,7 @@ class Compiler {
285
301
  }
286
302
  }
287
303
 
288
- class Cache {
304
+ class EjsCache {
289
305
  static exports = ['load', 'set', 'get', 'exist', 'clear', 'remove', 'resolve'];
290
306
  #cache = true;
291
307
  #precompiled;
@@ -359,31 +375,13 @@ const element = (tag, attrs, content) => {
359
375
  return result.join('');
360
376
  };
361
377
 
362
- class TemplateError extends Error {
363
- name = 'TemplateError';
364
- constructor(error) {
365
- super(error);
366
- if (error instanceof Error) {
367
- this.stack = error.stack;
368
- this.filename = error.filename;
369
- this.lineno = error.lineno;
370
- }
371
- }
372
- }
373
- class TemplateNotFound extends TemplateError {
374
- name = 'TemplateNotFound';
375
- code = 404;
376
- }
377
- class TemplateSyntaxError extends TemplateError {
378
- name = 'TemplateSyntaxError';
379
- code = 500;
380
- }
381
-
382
378
  const resolve = list => {
383
- return Promise.all(list || []).then(list => list.join('')).catch(e => e);
379
+ return Promise.all(list || []).then(list => list.join('')).catch(e => {
380
+ return error(500, e);
381
+ });
384
382
  };
385
- const reject = error => {
386
- return Promise.reject(new TemplateSyntaxError(error));
383
+ const reject = e => {
384
+ return Promise.reject(error(500, e));
387
385
  };
388
386
  const EjsBuffer = () => {
389
387
  let store = [];
@@ -408,7 +406,7 @@ const EjsBuffer = () => {
408
406
  array = store.pop();
409
407
  return resolve(result);
410
408
  };
411
- EjsBuffer.error = (e, filename) => {
409
+ EjsBuffer.error = e => {
412
410
  return reject(e);
413
411
  };
414
412
  EjsBuffer.end = () => {
@@ -437,13 +435,6 @@ const createContext$1 = (config, methods) => {
437
435
  this[MACRO] = {};
438
436
  Object.assign(this, omit(data, [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT]));
439
437
  }
440
- Object.entries(methods).forEach(_ref => {
441
- let [name, value] = _ref;
442
- if (isFunction(value) && globals.includes(name)) {
443
- value = value.bind(EjsContext.prototype);
444
- }
445
- EjsContext.prototype[name] = value;
446
- });
447
438
  Object.defineProperty(EjsContext.prototype, BUFFER, {
448
439
  value: EjsBuffer()
449
440
  });
@@ -468,36 +459,31 @@ const createContext$1 = (config, methods) => {
468
459
  writable: true
469
460
  });
470
461
  Object.defineProperties(EjsContext.prototype, {
471
- /** @type {function} */
472
462
  setParentTemplate: {
473
463
  value(value) {
474
464
  this[PARENT] = value;
475
465
  return this;
476
466
  }
477
467
  },
478
- /** @type {function} */
479
468
  getParentTemplate: {
480
469
  value() {
481
470
  return this[PARENT];
482
471
  }
483
472
  },
484
- /** @type {function} */
485
- useSafeValue: {
486
- get: () => safeValue
473
+ useEscapeValue: {
474
+ get: () => escapeValue
487
475
  },
488
- /** @type {function} */
489
476
  useComponent: {
490
477
  get() {
491
478
  if (isFunction(this[COMPONENT])) {
492
479
  return this[COMPONENT].bind(this);
493
480
  } else {
494
- return () => {
481
+ return function () {
495
482
  throw new Error(`${COMPONENT} must be a function`);
496
483
  };
497
484
  }
498
485
  }
499
486
  },
500
- /** @type {function} */
501
487
  useElement: {
502
488
  get() {
503
489
  if (isFunction(this[ELEMENT])) {
@@ -509,51 +495,43 @@ const createContext$1 = (config, methods) => {
509
495
  }
510
496
  }
511
497
  },
512
- /** @type {function} */
513
498
  useBuffer: {
514
499
  get() {
515
500
  return this[BUFFER];
516
501
  }
517
502
  },
518
- /** @type {function} */
519
503
  getMacro: {
520
504
  value() {
521
505
  return this[MACRO];
522
506
  }
523
507
  },
524
- /** @type {function} */
525
508
  getBlocks: {
526
509
  value() {
527
510
  return this[BLOCKS];
528
511
  }
529
512
  },
530
- /** @type {function} */
531
513
  setExtend: {
532
514
  value(value) {
533
515
  this[EXTEND] = value;
534
516
  return this;
535
517
  }
536
518
  },
537
- /** @type {function} */
538
519
  getExtend: {
539
520
  value() {
540
521
  return this[EXTEND];
541
522
  }
542
523
  },
543
- /** @type {function} */
544
524
  setLayout: {
545
525
  value(layout) {
546
526
  this[LAYOUT] = layout;
547
527
  return this;
548
528
  }
549
529
  },
550
- /** @type {function} */
551
530
  getLayout: {
552
531
  value() {
553
532
  return this[LAYOUT];
554
533
  }
555
534
  },
556
- /** @type {function} */
557
535
  clone: {
558
536
  value(exclude_blocks) {
559
537
  const filter = [LAYOUT, EXTEND, BUFFER];
@@ -563,32 +541,29 @@ const createContext$1 = (config, methods) => {
563
541
  return omit(this, filter);
564
542
  }
565
543
  },
566
- /** @type {function} */
567
544
  extend: {
568
545
  value(layout) {
569
546
  this.setExtend(true);
570
547
  this.setLayout(layout);
571
548
  }
572
549
  },
573
- /** @type {function} */
574
550
  echo: {
575
551
  value() {
576
552
  return [].slice.call(arguments).forEach(this.useBuffer);
577
553
  }
578
554
  },
579
- /** @type {function} */
580
555
  fn: {
581
556
  value(callback) {
582
- return () => {
557
+ const context = this;
558
+ return function () {
583
559
  if (isFunction(callback)) {
584
- this.useBuffer.backup();
585
- this.useBuffer(callback.apply(this, arguments));
586
- return this.useBuffer.restore();
560
+ context.useBuffer.backup();
561
+ context.useBuffer(callback.apply(context, arguments));
562
+ return context.useBuffer.restore();
587
563
  }
588
564
  };
589
565
  }
590
566
  },
591
- /** @type {function} */
592
567
  macro: {
593
568
  value(name, callback) {
594
569
  const list = this.getMacro();
@@ -599,7 +574,6 @@ const createContext$1 = (config, methods) => {
599
574
  };
600
575
  }
601
576
  },
602
- /** @type {function} */
603
577
  call: {
604
578
  value(name) {
605
579
  const list = this.getMacro();
@@ -610,35 +584,35 @@ const createContext$1 = (config, methods) => {
610
584
  }
611
585
  }
612
586
  },
613
- /** @type {function} */
614
587
  block: {
615
588
  value(name, callback) {
616
589
  const blocks = this.getBlocks();
617
590
  blocks[name] = blocks[name] || [];
618
591
  blocks[name].push(this.fn(callback));
619
592
  if (this.getExtend()) return;
593
+ const context = this;
620
594
  const list = Object.assign([], blocks[name]);
621
- const shift = () => list.shift();
622
- const next = () => {
595
+ const shift = function () {
596
+ return list.shift();
597
+ };
598
+ const next = function () {
623
599
  const parent = shift();
624
600
  if (parent) {
625
- return () => {
626
- this.echo(parent(next()));
601
+ return function () {
602
+ context.echo(parent(next()));
627
603
  };
628
604
  } else {
629
- return () => {};
605
+ return function () {};
630
606
  }
631
607
  };
632
608
  this.echo(shift()(next()));
633
609
  }
634
610
  },
635
- /** @type {function} */
636
611
  hasBlock: {
637
612
  value(name) {
638
613
  return this.getBlocks().hasOwnProperty(name);
639
614
  }
640
615
  },
641
- /** @type {function} */
642
616
  include: {
643
617
  value(path, data, cx) {
644
618
  const context = cx === false ? {} : this.clone(true);
@@ -647,7 +621,6 @@ const createContext$1 = (config, methods) => {
647
621
  this.echo(promise);
648
622
  }
649
623
  },
650
- /** @type {function} */
651
624
  use: {
652
625
  value(path, namespace) {
653
626
  this.echo(Promise.resolve(this.require(path)).then(exports$1 => {
@@ -658,13 +631,6 @@ const createContext$1 = (config, methods) => {
658
631
  }));
659
632
  }
660
633
  },
661
- /** @type {function} */
662
- async: {
663
- value(promise, callback) {
664
- this.echo(Promise.resolve(promise).then(callback));
665
- }
666
- },
667
- /** @type {function} */
668
634
  get: {
669
635
  value(name, defaults) {
670
636
  const path = getPath(this, name, true);
@@ -673,7 +639,6 @@ const createContext$1 = (config, methods) => {
673
639
  return hasProp(result, prop) ? result[prop] : defaults;
674
640
  }
675
641
  },
676
- /** @type {function} */
677
642
  set: {
678
643
  value(name, value) {
679
644
  const path = getPath(this, name, false);
@@ -685,7 +650,6 @@ const createContext$1 = (config, methods) => {
685
650
  return result[prop] = value;
686
651
  }
687
652
  },
688
- /** @type {function} */
689
653
  each: {
690
654
  value(object, callback) {
691
655
  if (isString(object)) {
@@ -695,7 +659,6 @@ const createContext$1 = (config, methods) => {
695
659
  },
696
660
  writable: true
697
661
  },
698
- /** @type {function} */
699
662
  el: {
700
663
  value(tag, attr, content) {
701
664
  content = isFunction(content) ? this.fn(content)() : content;
@@ -703,15 +666,21 @@ const createContext$1 = (config, methods) => {
703
666
  },
704
667
  writable: true
705
668
  },
706
- /** @type {function} */
707
669
  ui: {
708
- value(layout) {},
670
+ value() {},
709
671
  writable: true
710
672
  }
711
673
  });
674
+ Object.entries(methods).forEach(_ref => {
675
+ let [name, value] = _ref;
676
+ if (isFunction(value) && globals.includes(name)) {
677
+ value = value.bind(EjsContext.prototype);
678
+ }
679
+ EjsContext.prototype[name] = value;
680
+ });
712
681
  return EjsContext;
713
682
  };
714
- class Context {
683
+ class EjsContext {
715
684
  #context;
716
685
  static exports = ['create', 'globals', 'helpers'];
717
686
  constructor(options, methods) {
@@ -742,10 +711,10 @@ class EjsInstance {
742
711
  bindContext(this, this.constructor.exports);
743
712
  this.#methods = {};
744
713
  this.#config = configSchema({}, options);
745
- this.#context = new Context(this.#config, this.#methods);
746
- this.#compiler = new Compiler(this.#config);
747
- this.#cache = new Cache(this.#config);
748
- this.#template = new Template(this.#config, this.#cache, this.#compiler);
714
+ this.#context = new EjsContext(this.#config, this.#methods);
715
+ this.#compiler = new EjsCompiler(this.#config);
716
+ this.#cache = new EjsCache(this.#config);
717
+ this.#template = new EjsTemplate(this.#config, this.#cache, this.#compiler);
749
718
  this.helpers({
750
719
  render: this.render,
751
720
  require: this.require
@@ -792,7 +761,7 @@ class EjsInstance {
792
761
  return name;
793
762
  }
794
763
  #output(path, data) {
795
- return this.#template.get(path).then(callback => callback.apply(data, [data, data.useComponent, data.useElement, data.useBuffer, data.useSafeValue]));
764
+ return this.#template.get(path).then(callback => callback.apply(data, [data, data.useComponent, data.useElement, data.useBuffer, data.useEscapeValue]));
796
765
  }
797
766
  #renderLayout(name, params, parentTemplate) {
798
767
  const data = this.createContext(params);
@@ -810,30 +779,25 @@ class EjsInstance {
810
779
  }
811
780
  }
812
781
 
813
- const httpRequest = (path, template) => {
782
+ const httpRequest = (path, template, error) => {
814
783
  return fetch(joinPath(path, template)).then(response => {
815
784
  if (response.ok) return response.text();
816
- throw new TemplateNotFound(`template ${path} / ${template} not found`);
817
- }, reason => {
818
- return new TemplateError(reason);
819
- });
785
+ return error(404, `template ${template} not found`);
786
+ }, reason => error(500, reason));
820
787
  };
821
788
 
822
789
  const {
823
790
  render,
791
+ configure,
792
+ create,
793
+ helpers,
824
794
  createContext,
825
795
  compile,
826
- helpers,
827
- preload,
828
- configure,
829
- create
796
+ preload
830
797
  } = new EjsInstance({
831
798
  resolver: httpRequest
832
799
  });
833
800
 
834
- exports.TemplateError = TemplateError;
835
- exports.TemplateNotFound = TemplateNotFound;
836
- exports.TemplateSyntaxError = TemplateSyntaxError;
837
801
  exports.compile = compile;
838
802
  exports.configure = configure;
839
803
  exports.create = create;
@@ -2,9 +2,9 @@
2
2
 
3
3
  var fs = require('node:fs/promises');
4
4
  var globWatch = require('glob-watcher');
5
- var glob = require('glob');
6
5
  var node_path = require('node:path');
7
6
  var index_js = require('./index.js');
7
+ var glob = require('glob');
8
8
 
9
9
  const symbolEntities = {
10
10
  "'": "'",
@@ -40,7 +40,7 @@ const bindContext = (object, methods = []) => {
40
40
  }
41
41
  };
42
42
 
43
- class Bundler {
43
+ class EjsBundler {
44
44
  #templates = {}
45
45
  #bundlerOptions = {
46
46
  target: [],
@@ -56,7 +56,7 @@ class Bundler {
56
56
  const { compile, configure } = index_js.create(ejsOptions);
57
57
  Object.assign(this.#bundlerOptions, bundlerOptions);
58
58
  this.#compile = compile;
59
- this.#ejsOptions = configure();
59
+ this.#ejsOptions = configure({});
60
60
  this.#buildInProgress = false;
61
61
  this.#templates = {};
62
62
  }
@@ -162,11 +162,11 @@ class Bundler {
162
162
  }
163
163
 
164
164
  const bundler = (bundlerOptions = {}, ejsOptions = {}) => {
165
- return new Bundler(bundlerOptions, ejsOptions)
165
+ return new EjsBundler(bundlerOptions, ejsOptions)
166
166
  };
167
167
 
168
168
  const ejsRollupBundler = (bundlerOptions, ejsOptions) => {
169
- const bundle = new Bundler(bundlerOptions, ejsOptions);
169
+ const bundle = bundler(bundlerOptions, ejsOptions);
170
170
  return {
171
171
  name: 'ejs-bundler',
172
172
  async buildStart() {
@@ -178,6 +178,6 @@ const ejsRollupBundler = (bundlerOptions, ejsOptions) => {
178
178
  }
179
179
  };
180
180
 
181
- exports.Bundler = Bundler;
181
+ exports.EjsBundler = EjsBundler;
182
182
  exports.bundler = bundler;
183
183
  exports.ejsRollupBundler = ejsRollupBundler;
@@ -23,7 +23,7 @@ const entities = function () {
23
23
  let string = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
24
24
  return ('' + string).replace(htmlEntitiesMatch, match => htmlEntities[match]);
25
25
  };
26
- const safeValue = (value, escape) => {
26
+ const escapeValue = (value, escape) => {
27
27
  const check = value;
28
28
  return check == null ? '' : Boolean(escape) === true ? entities(check) : check;
29
29
  };
@@ -56,4 +56,4 @@ const element = (tag, attrs, content) => {
56
56
  };
57
57
 
58
58
  exports.element = element;
59
- exports.safeValue = safeValue;
59
+ exports.escapeValue = escapeValue;