ecto 5.1.1 → 5.1.2

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/README.md CHANGED
@@ -7,6 +7,7 @@
7
7
  [![GitHub license](https://img.shields.io/github/license/jaredwray/ecto)](https://github.com/jaredwray/ecto/blob/master/LICENSE)
8
8
  [![codecov](https://codecov.io/gh/jaredwray/ecto/branch/main/graph/badge.svg?token=dpbFqSW5Kh)](https://codecov.io/gh/jaredwray/ecto)
9
9
  [![npm](https://img.shields.io/npm/dm/ecto)](https://npmjs.com/package/ecto)
10
+ [![Drydock review](https://img.shields.io/endpoint?url=https%3A%2F%2Fdrydock.org%2Fpublic%2Fbadge%2Fnpm%2Fecto)](https://drydock.org/diff/ecto)
10
11
 
11
12
  -----
12
13
 
@@ -730,8 +731,8 @@ type RenderContext = {
730
731
  data?: Record<string, unknown>; // Template data (modifiable)
731
732
  engineName: string; // Engine being used
732
733
  rootTemplatePath?: string; // Root path for partials
733
- filePathOutput?: string; // Output file path
734
- cached: boolean; // True if result came from cache
734
+ filePathOutput?: string; // Output file path (modifiable; used when writing)
735
+ cached: boolean; // True when this call was served from cache
735
736
  };
736
737
 
737
738
  type RenderResult = {
@@ -764,9 +765,17 @@ ecto.onHook(EctoEvents.afterRender, (renderResult) => {
764
765
  const output = await ecto.render('<%= name %>', { name: 'World' });
765
766
  ```
766
767
 
768
+ ## Hooks and Caching
769
+
770
+ The async cache (`cache`) and sync cache (`cacheSync`) store the template engine output taken after the before-hook and before the after-hook. The cache key is the original source, data, and engine name.
771
+
772
+ On a cache hit, `beforeRender` / `beforeRenderSync` still run with `cached: true`, and the stored engine output is reused. Edits those hooks make to `source` or `data` are rendered on a cache miss. `afterRender` / `afterRenderSync` run on every call, including cache hits, and can still change `result` and the file written from `filePathOutput`.
773
+
774
+ `renderSync` uses Hookified's synchronous runner, which skips handlers declared with `async`.
775
+
767
776
  ## Hook Objects, IDs, and Errors
768
777
 
769
- Ecto extends [Hookified](https://hookified.org), so you can also register hooks via the `Hook` class. Every hook has a unique `id` (auto-generated unless supplied) for later lookup or removal across events. Pass `throwOnHookError: true` to the `Ecto` constructor to make hook errors fail the render instead of just emitting on `error`.
778
+ Ecto extends [Hookified](https://hookified.org), so constructor options such as `throwOnHookError` are passed through, and you can register hooks via the `Hook` class. Every hook has a unique `id` (auto-generated unless supplied) for later lookup or removal across events. The default is to emit `error` and continue rendering. Pass `throwOnHookError: true` to make a hook error reject `render` and throw from `renderSync`.
770
779
 
771
780
  ```javascript
772
781
  import { Ecto, EctoEvents } from 'ecto';
package/dist/ecto.d.mts CHANGED
@@ -226,7 +226,8 @@ export declare class Ecto extends Hookified {
226
226
  */
227
227
  get liquid(): Liquid;
228
228
  /**
229
- * Asynchronously render a template source with data using the specified engine
229
+ * Asynchronously render a template source with data using the specified engine.
230
+ * `beforeRender` can modify the source and data. `afterRender` can modify the returned string.
230
231
  * @param {string} source - The template source string to render
231
232
  * @param {Record<string, unknown>} [data] - Data object to pass to the template engine
232
233
  * @param {string} [engineName] - Name of the engine to use (e.g., 'ejs', 'pug'). Defaults to defaultEngine
@@ -238,7 +239,9 @@ export declare class Ecto extends Hookified {
238
239
  */
239
240
  render(source: string, data?: Record<string, unknown>, engineName?: string, rootTemplatePath?: string, filePathOutput?: string): Promise<string>;
240
241
  /**
241
- * Synchronously render a template source with data using the specified engine
242
+ * Synchronously render a template source with data using the specified engine.
243
+ * `beforeRenderSync` can modify the source and data. `afterRenderSync` can modify the returned string.
244
+ * Async hook handlers are skipped.
242
245
  * @param {string} source - The template source string to render
243
246
  * @param {Record<string, unknown>} [data] - Data object to pass to the template engine
244
247
  * @param {string} [engineName] - Name of the engine to use (e.g., 'ejs', 'pug'). Defaults to defaultEngine
@@ -381,6 +384,23 @@ export declare class Ecto extends Hookified {
381
384
  * const body = ecto.removeFrontMatter('---\ntitle: Test\n---\nContent');
382
385
  */
383
386
  removeFrontMatter(source: string): string;
387
+ /**
388
+ * Run an async render hook.
389
+ * Hookified throws from `emit('error')` when no error listener is attached.
390
+ * A temporary listener keeps that from aborting the render unless
391
+ * `throwOnHookError` is set. Handler failures then reject the render.
392
+ * @param {EctoEvents} event - Hook event name
393
+ * @param {T} payload - Context or result object passed to handlers
394
+ * @returns {Promise<void>}
395
+ */
396
+ private invokeHook;
397
+ /**
398
+ * Run a synchronous render hook. Async handlers are skipped by Hookified.
399
+ * @param {EctoEvents} event - Hook event name
400
+ * @param {T} payload - Context or result object passed to handlers
401
+ * @returns {void}
402
+ */
403
+ private invokeHookSync;
384
404
  /**
385
405
  * Write content to a file asynchronously, creating directories if needed
386
406
  * @private
package/dist/ecto.mjs CHANGED
@@ -244,6 +244,16 @@ var Pug = class extends BaseEngine {
244
244
  };
245
245
  //#endregion
246
246
  //#region src/ecto.ts
247
+ /**
248
+ * Hook failure that should reject the render when `throwOnHookError` is set.
249
+ * Hookified has already emitted `error` when any listeners are attached.
250
+ */
251
+ var HookRenderError = class extends Error {
252
+ constructor(cause) {
253
+ super(cause.message, { cause });
254
+ this.name = "HookRenderError";
255
+ }
256
+ };
247
257
  let EctoEvents = /* @__PURE__ */ function(EctoEvents) {
248
258
  EctoEvents["cacheHit"] = "cacheHit";
249
259
  EctoEvents["cacheMiss"] = "cacheMiss";
@@ -272,7 +282,7 @@ var Ecto = class extends Hookified {
272
282
  * @param {EctoOptions} [options] - The options for the ecto engine
273
283
  */
274
284
  constructor(options) {
275
- super();
285
+ super(options);
276
286
  this._ejs = new EJS(options?.engineOptions?.ejs);
277
287
  this._markdown = new Markdown(options?.engineOptions?.markdown);
278
288
  this._pug = new Pug(options?.engineOptions?.pug);
@@ -381,7 +391,8 @@ var Ecto = class extends Hookified {
381
391
  return this._liquid;
382
392
  }
383
393
  /**
384
- * Asynchronously render a template source with data using the specified engine
394
+ * Asynchronously render a template source with data using the specified engine.
395
+ * `beforeRender` can modify the source and data. `afterRender` can modify the returned string.
385
396
  * @param {string} source - The template source string to render
386
397
  * @param {Record<string, unknown>} [data] - Data object to pass to the template engine
387
398
  * @param {string} [engineName] - Name of the engine to use (e.g., 'ejs', 'pug'). Defaults to defaultEngine
@@ -408,13 +419,13 @@ var Ecto = class extends Hookified {
408
419
  filePathOutput,
409
420
  cached: true
410
421
  };
411
- await this.hook("beforeRender", context);
422
+ await this.invokeHook("beforeRender", context);
412
423
  const renderResult = {
413
424
  result: cachedResult,
414
425
  context
415
426
  };
416
- await this.hook("afterRender", renderResult);
417
- await this.writeFile(filePathOutput, renderResult.result);
427
+ await this.invokeHook("afterRender", renderResult);
428
+ await this.writeFile(context.filePathOutput, renderResult.result);
418
429
  return renderResult.result;
419
430
  }
420
431
  this.emit("cacheMiss", `Cache miss for key: ${cacheKey}`);
@@ -427,28 +438,28 @@ var Ecto = class extends Hookified {
427
438
  filePathOutput,
428
439
  cached: false
429
440
  };
430
- await this.hook("beforeRender", context);
441
+ await this.invokeHook("beforeRender", context);
431
442
  const renderEngine = this.getRenderEngine(renderEngineName);
432
443
  renderEngine.rootTemplatePath = context.rootTemplatePath;
433
- let result = await renderEngine.render(context.source, context.data);
444
+ const rendered = await renderEngine.render(context.source, context.data);
434
445
  const renderResult = {
435
- result,
446
+ result: rendered,
436
447
  context
437
448
  };
438
- await this.hook("afterRender", renderResult);
439
- result = renderResult.result;
440
- if (this._cache) await this._cache.set(cacheKey, result);
441
- await this.writeFile(filePathOutput, result);
442
- return result;
449
+ await this.invokeHook("afterRender", renderResult);
450
+ if (this._cache) await this._cache.set(cacheKey, rendered);
451
+ await this.writeFile(context.filePathOutput, renderResult.result);
452
+ return renderResult.result;
443
453
  } catch (error) {
444
- /* v8 ignore next -- @preserve */
454
+ if (error instanceof HookRenderError) throw error.cause;
445
455
  this.emit("error", error);
446
- /* v8 ignore next -- @preserve */
447
456
  return "";
448
457
  }
449
458
  }
450
459
  /**
451
- * Synchronously render a template source with data using the specified engine
460
+ * Synchronously render a template source with data using the specified engine.
461
+ * `beforeRenderSync` can modify the source and data. `afterRenderSync` can modify the returned string.
462
+ * Async hook handlers are skipped.
452
463
  * @param {string} source - The template source string to render
453
464
  * @param {Record<string, unknown>} [data] - Data object to pass to the template engine
454
465
  * @param {string} [engineName] - Name of the engine to use (e.g., 'ejs', 'pug'). Defaults to defaultEngine
@@ -475,13 +486,13 @@ var Ecto = class extends Hookified {
475
486
  filePathOutput,
476
487
  cached: true
477
488
  };
478
- this.hookSync("beforeRenderSync", context);
489
+ this.invokeHookSync("beforeRenderSync", context);
479
490
  const renderResult = {
480
491
  result: cachedResult,
481
492
  context
482
493
  };
483
- this.hookSync("afterRenderSync", renderResult);
484
- this.writeFileSync(filePathOutput, renderResult.result);
494
+ this.invokeHookSync("afterRenderSync", renderResult);
495
+ this.writeFileSync(context.filePathOutput, renderResult.result);
485
496
  return renderResult.result;
486
497
  }
487
498
  this.emit("cacheMiss", `Cache miss for key: ${cacheKey}`);
@@ -494,20 +505,20 @@ var Ecto = class extends Hookified {
494
505
  filePathOutput,
495
506
  cached: false
496
507
  };
497
- this.hookSync("beforeRenderSync", context);
508
+ this.invokeHookSync("beforeRenderSync", context);
498
509
  const renderEngine = this.getRenderEngine(renderEngineName);
499
510
  renderEngine.rootTemplatePath = context.rootTemplatePath;
500
- let result = renderEngine.renderSync(context.source, context.data);
511
+ const rendered = renderEngine.renderSync(context.source, context.data);
501
512
  const renderResult = {
502
- result,
513
+ result: rendered,
503
514
  context
504
515
  };
505
- this.hookSync("afterRenderSync", renderResult);
506
- result = renderResult.result;
507
- if (this._cacheSync) this._cacheSync.set(cacheKey, result);
508
- this.writeFileSync(filePathOutput, result);
509
- return result;
516
+ this.invokeHookSync("afterRenderSync", renderResult);
517
+ if (this._cacheSync) this._cacheSync.set(cacheKey, rendered);
518
+ this.writeFileSync(context.filePathOutput, renderResult.result);
519
+ return renderResult.result;
510
520
  } catch (error) {
521
+ if (error instanceof HookRenderError) throw error.cause;
511
522
  this.emit("error", error);
512
523
  return "";
513
524
  }
@@ -825,6 +836,57 @@ var Ecto = class extends Hookified {
825
836
  return new Writr(source).body;
826
837
  }
827
838
  /**
839
+ * Run an async render hook.
840
+ * Hookified throws from `emit('error')` when no error listener is attached.
841
+ * A temporary listener keeps that from aborting the render unless
842
+ * `throwOnHookError` is set. Handler failures then reject the render.
843
+ * @param {EctoEvents} event - Hook event name
844
+ * @param {T} payload - Context or result object passed to handlers
845
+ * @returns {Promise<void>}
846
+ */
847
+ async invokeHook(event, payload) {
848
+ if (!this.throwOnHookError) {
849
+ const ignoreHookError = () => {};
850
+ const swallowEmpty = this.listenerCount("error") === 0;
851
+ if (swallowEmpty) this.on("error", ignoreHookError);
852
+ try {
853
+ await this.hook(event, payload);
854
+ } finally {
855
+ if (swallowEmpty) this.off("error", ignoreHookError);
856
+ }
857
+ return;
858
+ }
859
+ try {
860
+ await this.hook(event, payload);
861
+ } catch (error) {
862
+ throw new HookRenderError(error);
863
+ }
864
+ }
865
+ /**
866
+ * Run a synchronous render hook. Async handlers are skipped by Hookified.
867
+ * @param {EctoEvents} event - Hook event name
868
+ * @param {T} payload - Context or result object passed to handlers
869
+ * @returns {void}
870
+ */
871
+ invokeHookSync(event, payload) {
872
+ if (!this.throwOnHookError) {
873
+ const ignoreHookError = () => {};
874
+ const swallowEmpty = this.listenerCount("error") === 0;
875
+ if (swallowEmpty) this.on("error", ignoreHookError);
876
+ try {
877
+ this.hookSync(event, payload);
878
+ } finally {
879
+ if (swallowEmpty) this.off("error", ignoreHookError);
880
+ }
881
+ return;
882
+ }
883
+ try {
884
+ this.hookSync(event, payload);
885
+ } catch (error) {
886
+ throw new HookRenderError(error);
887
+ }
888
+ }
889
+ /**
828
890
  * Write content to a file asynchronously, creating directories if needed
829
891
  * @private
830
892
  * @param {string} [filePath] - The path to write the file to
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ecto",
3
- "version": "5.1.1",
3
+ "version": "5.1.2",
4
4
  "description": "Modern Template Consolidation Engine for EJS, Markdown, Pug, Nunjucks, Liquid, and Handlebars",
5
5
  "type": "module",
6
6
  "main": "./dist/ecto.mjs",
@@ -42,30 +42,30 @@
42
42
  "liquidjs"
43
43
  ],
44
44
  "dependencies": {
45
- "@jaredwray/fumanchu": "^4.7.3",
45
+ "@jaredwray/fumanchu": "^4.7.4",
46
46
  "cacheable": "^2.5.0",
47
- "ejs": "^6.0.1",
48
- "hookified": "^3.0.3",
49
- "liquidjs": "^10.29.0",
47
+ "ejs": "^7.0.2",
48
+ "hookified": "^3.0.4",
49
+ "liquidjs": "^10.30.0",
50
50
  "nunjucks": "^3.2.4",
51
51
  "pug": "^3.0.4",
52
52
  "writr": "^6.1.6"
53
53
  },
54
54
  "devDependencies": {
55
- "@biomejs/biome": "^2.5.14",
55
+ "@biomejs/biome": "^2.5.15",
56
56
  "@faker-js/faker": "^10.6.0",
57
57
  "@types/ejs": "^3.1.5",
58
- "@types/node": "^24.13.6",
58
+ "@types/node": "^24.19.2",
59
59
  "@types/nunjucks": "^3.2.6",
60
60
  "@types/pug": "^2.0.10",
61
- "@vitest/coverage-v8": "^5.0.1",
62
- "docula": "^3.0.0",
61
+ "@vitest/coverage-v8": "^5.0.5",
62
+ "docula": "^3.0.2",
63
63
  "rimraf": "^6.1.3",
64
- "tsdown": "^0.23.0",
64
+ "tsdown": "^0.23.1",
65
65
  "typescript": "^7.0.2",
66
- "vite": "^8.3.0",
67
- "vitest": "^5.0.1",
68
- "wrangler": "^4.135.0"
66
+ "vite": "^8.3.4",
67
+ "vitest": "^5.0.5",
68
+ "wrangler": "^4.151.2"
69
69
  },
70
70
  "files": [
71
71
  "dist",