@kosatyi/ejs 0.0.110 → 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.
@@ -383,10 +383,10 @@ class EjsCompiler {
383
383
  const result = Function.apply(null, params.concat(OUTPUT));
384
384
  result.source = `(function(${params.join(',')}){\n${OUTPUT}\n});`;
385
385
  return result
386
- } catch (error) {
387
- error.filename = path;
388
- error.source = OUTPUT;
389
- throw error
386
+ } catch (e) {
387
+ e.filename = path;
388
+ e.source = OUTPUT;
389
+ error(0, e);
390
390
  }
391
391
  }
392
392
  }
@@ -498,13 +498,11 @@ const element = (tag, attrs, content) => {
498
498
  const resolve = (list) => {
499
499
  return Promise.all(list || [])
500
500
  .then((list) => list.join(''))
501
- .catch((e) => {
502
- return error(500, e)
503
- })
501
+ .catch((e) => error(0, e))
504
502
  };
505
503
 
506
504
  const reject = (e) => {
507
- return Promise.reject(error(500, e))
505
+ return Promise.reject(error(0, e))
508
506
  };
509
507
 
510
508
  const EjsBuffer = () => {
@@ -542,6 +540,7 @@ const EjsBuffer = () => {
542
540
  const PARENT = Symbol('EjsContext.parentTemplate');
543
541
 
544
542
  const createContext$1 = (config, methods) => {
543
+ const globals = config.globals || [];
545
544
  const {
546
545
  BLOCKS,
547
546
  MACRO,
@@ -553,40 +552,41 @@ const createContext$1 = (config, methods) => {
553
552
  COMPONENT,
554
553
  ELEMENT,
555
554
  } = config.vars;
556
- const globals = config.globals || [];
557
- function EjsContext(data) {
558
- this[PARENT] = null;
559
- this[BLOCKS] = {};
560
- this[MACRO] = {};
561
- Object.assign(
562
- this,
563
- omit(data, [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT]),
564
- );
555
+ class Context {
556
+ constructor(data) {
557
+ this[PARENT] = null;
558
+ this[BLOCKS] = {};
559
+ this[MACRO] = {};
560
+ Object.assign(
561
+ this,
562
+ omit(data, [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT]),
563
+ );
564
+ }
565
565
  }
566
- Object.defineProperty(EjsContext.prototype, BUFFER, {
567
- value: EjsBuffer(),
568
- });
569
- Object.defineProperty(EjsContext.prototype, BLOCKS, {
570
- value: {},
571
- writable: true,
572
- });
573
- Object.defineProperty(EjsContext.prototype, MACRO, {
574
- value: {},
575
- writable: true,
576
- });
577
- Object.defineProperty(EjsContext.prototype, LAYOUT, {
578
- value: false,
579
- writable: true,
580
- });
581
- Object.defineProperty(EjsContext.prototype, EXTEND, {
582
- value: false,
583
- writable: true,
584
- });
585
- Object.defineProperty(EjsContext.prototype, PARENT, {
586
- value: null,
587
- writable: true,
588
- });
589
- Object.defineProperties(EjsContext.prototype, {
566
+ Object.defineProperties(Context.prototype, {
567
+ [BUFFER]: {
568
+ value: EjsBuffer(),
569
+ },
570
+ [BLOCKS]: {
571
+ value: {},
572
+ writable: true,
573
+ },
574
+ [MACRO]: {
575
+ value: {},
576
+ writable: true,
577
+ },
578
+ [LAYOUT]: {
579
+ value: false,
580
+ writable: true,
581
+ },
582
+ [EXTEND]: {
583
+ value: false,
584
+ writable: true,
585
+ },
586
+ [PARENT]: {
587
+ value: null,
588
+ writable: true,
589
+ },
590
590
  setParentTemplate: {
591
591
  value(value) {
592
592
  this[PARENT] = value;
@@ -599,32 +599,34 @@ const createContext$1 = (config, methods) => {
599
599
  },
600
600
  },
601
601
  useEscapeValue: {
602
- get: () => escapeValue,
602
+ value() {
603
+ return escapeValue
604
+ },
603
605
  },
604
606
  useComponent: {
605
- get() {
607
+ value() {
606
608
  if (isFunction(this[COMPONENT])) {
607
609
  return this[COMPONENT].bind(this)
608
610
  } else {
609
611
  return function () {
610
- throw new Error(`${COMPONENT} must be a function`)
612
+ error(2, `${COMPONENT} must be a function`);
611
613
  }
612
614
  }
613
615
  },
614
616
  },
615
617
  useElement: {
616
- get() {
618
+ value() {
617
619
  if (isFunction(this[ELEMENT])) {
618
620
  return this[ELEMENT].bind(this)
619
621
  } else {
620
622
  return () => {
621
- throw new Error(`${ELEMENT} must be a function`)
623
+ error(2, `${ELEMENT} must be a function`);
622
624
  }
623
625
  }
624
626
  },
625
627
  },
626
628
  useBuffer: {
627
- get() {
629
+ value() {
628
630
  return this[BUFFER]
629
631
  },
630
632
  },
@@ -677,17 +679,18 @@ const createContext$1 = (config, methods) => {
677
679
  },
678
680
  echo: {
679
681
  value() {
680
- return [].slice.call(arguments).forEach(this.useBuffer)
682
+ return [].slice.call(arguments).forEach(this.useBuffer())
681
683
  },
682
684
  },
683
685
  fn: {
684
686
  value(callback) {
687
+ const buffer = this.useBuffer();
685
688
  const context = this;
686
689
  return function () {
687
690
  if (isFunction(callback)) {
688
- context.useBuffer.backup();
689
- context.useBuffer(callback.apply(context, arguments));
690
- return context.useBuffer.restore()
691
+ buffer.backup();
692
+ buffer(callback.apply(context, arguments));
693
+ return buffer.restore()
691
694
  }
692
695
  }
693
696
  },
@@ -804,14 +807,22 @@ const createContext$1 = (config, methods) => {
804
807
  value() {},
805
808
  writable: true,
806
809
  },
810
+ require: {
811
+ value() {},
812
+ writable: true,
813
+ },
814
+ render: {
815
+ value() {},
816
+ writable: true,
817
+ },
807
818
  });
808
819
  Object.entries(methods).forEach(([name, value]) => {
809
820
  if (isFunction(value) && globals.includes(name)) {
810
- value = value.bind(EjsContext.prototype);
821
+ value = value.bind(Context.prototype);
811
822
  }
812
- EjsContext.prototype[name] = value;
823
+ Context.prototype[name] = value;
813
824
  });
814
- return EjsContext
825
+ return Context
815
826
  };
816
827
 
817
828
  class EjsContext {
@@ -849,6 +860,10 @@ class EjsInstance {
849
860
  'compile',
850
861
  'helpers',
851
862
  ]
863
+ /**
864
+ *
865
+ * @param {EjsConfig} options
866
+ */
852
867
  constructor(options = {}) {
853
868
  bindContext(this, this.constructor.exports);
854
869
  this.#methods = {};
@@ -911,10 +926,10 @@ class EjsInstance {
911
926
  .then((callback) =>
912
927
  callback.apply(data, [
913
928
  data,
914
- data.useComponent,
915
- data.useElement,
916
- data.useBuffer,
917
- data.useEscapeValue,
929
+ data.useComponent(),
930
+ data.useElement(),
931
+ data.useBuffer(),
932
+ data.useEscapeValue(),
918
933
  ]),
919
934
  )
920
935
  }
@@ -936,9 +951,6 @@ class EjsInstance {
936
951
  }
937
952
  }
938
953
 
939
- /**
940
- * @type {{[p:string]:Function}}
941
- */
942
954
  const templateCache = {};
943
955
 
944
956
  const getOrigin = (url, secure) => {
@@ -947,37 +959,23 @@ const getOrigin = (url, secure) => {
947
959
  return url.origin
948
960
  };
949
961
 
962
+ const resolver = async (path, name, error) => {
963
+ if (isFunction(templateCache[name])) {
964
+ return templateCache[name]
965
+ }
966
+ error(1, `template ${name} not found`);
967
+ };
968
+
950
969
  const { render, createContext, helpers, configure } = new EjsInstance({
951
970
  cache: false,
952
971
  strict: true,
953
- async resolver(path, name, error) {
954
- if (isFunction(templateCache[name])) {
955
- return templateCache[name]
956
- }
957
- error(404, `template ${name} not found`);
958
- },
972
+ resolver,
959
973
  });
960
974
 
961
- /**
962
- * @param {{[p:string],Function}} templates
963
- */
964
975
  function useTemplates(templates = {}) {
965
976
  Object.assign(templateCache, templates);
966
977
  }
967
978
 
968
- /**
969
- * @typedef {{[p:string]:any}} HonoContext
970
- * @property {function(*):Promise<Response>} html
971
- * @property {function():Promise<Response>} notFound
972
- * @property {function(methods:{}):void} helpers
973
- * @property {function(name:string,data:{}):Promise<string>} render
974
- * @property {function(name:string,data:{}):Promise<string>} ejs
975
- * @property {EjsContext} data
976
- */
977
- /**
978
- * @param {RendererParams} options
979
- * @return {(function(c:HonoContext, next): Promise<any>)|*}
980
- */
981
979
  function useRenderer(options = {}) {
982
980
  useTemplates(options.templates ?? {});
983
981
  return async (c, next) => {