mikser-io 6.12.0 → 6.15.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mikser-io",
3
- "version": "6.12.0",
3
+ "version": "6.15.0",
4
4
  "description": "<p align=\"center\"> <img src=\"mikser-lockup-stacked.svg\" alt=\"mikser\" width=\"198\" /> </p>",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/api.js CHANGED
@@ -87,24 +87,41 @@ export function useRenderer(runtime, { defaultTimeout = 30_000 } = {}) {
87
87
  * `runtime.process()` cycle — within that cycle, mikser's worker pool
88
88
  * renders the batch in parallel.
89
89
  *
90
- * By default the entity is **not** kept in the catalog: after the
91
- * render resolves, the catalog row is pruned so it doesn't accumulate.
92
- * The rendered output file is always kept on disk — the bytes are the
93
- * work product. Pass `catalog: true` to also keep the catalog row,
94
- * useful if you'll re-render the same entity later or query it via
95
- * `findEntities`.
90
+ * Two control flags mirror mikser's default-keep-everything behavior;
91
+ * both opt-out via strict `=== false`:
92
+ *
93
+ * - `catalog: true` (default) — keep the entity in the catalog after
94
+ * the render. Pass `catalog: false` to prune the catalog row;
95
+ * useful for on-demand renders where the metadata row would just
96
+ * accumulate.
97
+ * - `save: true` (default) — write the rendered output to disk at
98
+ * `<outputFolder>/<entity.destination>`. Pass `save: false` to
99
+ * skip the final disk write; the bytes still come back via
100
+ * `output.result` for you to pipe wherever you want (HTTP
101
+ * response, S3, …). For layouts with a postprocessor (e.g.
102
+ * `*.html-pdf.*`), the intermediate file is still written so the
103
+ * postprocessor can consume it; only the FINAL output is skipped.
104
+ *
105
+ * The rendered output's bytes are always returned in `output.result`
106
+ * regardless of either flag — `save` only affects whether they also
107
+ * end up on disk.
96
108
  *
97
109
  * @param {object} entity - any entity-shaped object
98
110
  * @param {object} [opts]
99
111
  * @param {number} [opts.timeout] - override the default timeout
100
- * @param {boolean} [opts.catalog=false] - keep the catalog row after render
112
+ * @param {boolean} [opts.catalog=true] - keep the catalog row after render
113
+ * @param {boolean} [opts.save=true] - write the rendered output to disk
101
114
  * @returns {Promise<{output, entity}>}
102
115
  */
103
- async function render(entity, { timeout = defaultTimeout, catalog = false } = {}) {
116
+ async function render(entity, { timeout = defaultTimeout, catalog = true, save = true } = {}) {
104
117
  const result = await new Promise((resolve, reject) => {
105
118
  const correlationId = randomUUID()
119
+ const stamped = { ...entity, _correlationId: correlationId }
120
+ // Only stamp _save when explicitly opting out — keeps the
121
+ // entity object clean for the common case.
122
+ if (save === false) stamped._save = false
106
123
  pending.push({
107
- entity: { ...entity, _correlationId: correlationId },
124
+ entity: stamped,
108
125
  correlationId,
109
126
  timeout,
110
127
  resolve,
@@ -114,13 +131,15 @@ export function useRenderer(runtime, { defaultTimeout = 30_000 } = {}) {
114
131
  if (!cycleRunning) setImmediate(runBatch)
115
132
  })
116
133
 
117
- if (!catalog) {
118
- // Prune the catalog row so it doesn't accumulate, but LEAVE
119
- // the rendered output file on disk — the bytes are the work
120
- // product. We deliberately bypass the journal/DELETE path
121
- // here (which would also unlink the file via engine.js's
122
- // manifest cleanup) and splice the entity out of the
123
- // in-memory catalog directly.
134
+ if (catalog === false) {
135
+ // Explicit opt-out: prune the catalog row so it doesn't
136
+ // accumulate. The rendered output file stays on disk — the
137
+ // bytes are the work product. We deliberately bypass the
138
+ // journal/DELETE path here (which would also unlink the file
139
+ // via engine.js's manifest cleanup) and splice the entity
140
+ // out of the in-memory catalog directly. Strict equality so
141
+ // ambiguous inputs (null, "false", 0) fall through to the
142
+ // default of keeping the row.
124
143
  const entities = runtime.catalog?.data?.entities
125
144
  if (entities) {
126
145
  const idx = entities.findIndex(e => e.id === result.entity.id)
@@ -148,13 +148,16 @@ export default ({
148
148
 
149
149
  router.post('/render', auth, async (req, res) => {
150
150
  try {
151
- // Pull `catalog` out as a control flag; everything else
152
- // is treated as the entity. Default is NOT to keep the
153
- // catalog row (the rendered output stays on disk either
154
- // way). Send `catalog: true` to keep the row, e.g. if
155
- // you'll re-render or query the entity later.
156
- const { catalog, ...entityShape } = req.body
157
- const { output, entity } = await render(entityShape, { catalog })
151
+ // Body shape mirrors the JS API: entity fields at top
152
+ // level, control flags grouped under `options`. Forwarded
153
+ // straight to render(entity, options). Defaults match
154
+ // mikser's lifecycle (save and keep the catalog row);
155
+ // strict opt-outs via the literal `false`:
156
+ // options.catalog: false → prune the catalog row
157
+ // options.save: false → skip the final disk write
158
+ // (bytes still in the response)
159
+ const { options = {}, ...entityShape } = req.body
160
+ const { output, entity } = await render(entityShape, options)
158
161
  await sendRenderOutput(res, output, entity)
159
162
  } catch (err) {
160
163
  logger.error('Api render error: %s', err.message)
@@ -370,13 +370,32 @@ export default ({
370
370
  onComplete(async ({ entity, options, output }) => {
371
371
  const logger = useLogger()
372
372
  if (entity.layout && !options?.ignore && output.result != null) {
373
- const destinationFile = path.join(runtime.options.outputFolder, entity.destination)
374
- await mkdir(path.dirname(destinationFile), { recursive: true })
375
- try {
376
- await unlink(destinationFile)
377
- } catch { }
378
- await writeFile(destinationFile, output.result)
379
- logger.debug('Layout render finished: %s', entity.destination.replace(runtime.options.workingFolder, ''))
373
+ // `_save: false` (stamped by useRenderer when called with
374
+ // { save: false }) opts out of writing the final output to
375
+ // disk. The bytes still come back to the caller via
376
+ // output.result. Strict equality — only the literal `false`
377
+ // opts out, matching the catalog-flag pattern.
378
+ //
379
+ // The intermediate file (when a postprocessor will run next)
380
+ // must still be written so the postprocessor can consume it,
381
+ // so we only honour `_save: false` for the FINAL output —
382
+ // detected as either "no postprocessor configured" or "we're
383
+ // running on the postprocess side already" (origin set).
384
+ const isFinal = !entity.layout.postprocessor || entity.origin != null
385
+ const skipWrite = entity._save === false && isFinal
386
+
387
+ if (!skipWrite) {
388
+ const destinationFile = path.join(runtime.options.outputFolder, entity.destination)
389
+ await mkdir(path.dirname(destinationFile), { recursive: true })
390
+ try {
391
+ await unlink(destinationFile)
392
+ } catch { }
393
+ await writeFile(destinationFile, output.result)
394
+ logger.debug('Layout render finished: %s', entity.destination.replace(runtime.options.workingFolder, ''))
395
+ } else {
396
+ logger.debug('Layout render finished (save:false, bytes only): %s', entity.id)
397
+ }
398
+
380
399
  if (entity.origin && entity.origin !== entity.destination) {
381
400
  // Don't unlink the origin if it was the same path we just
382
401
  // wrote to (post plugins that produce the same extension as