@strapi/data-transfer 5.61.1 → 5.63.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.
Files changed (96) hide show
  1. package/dist/directory/providers/destination/index.d.ts +1 -1
  2. package/dist/directory/providers/destination/index.d.ts.map +1 -1
  3. package/dist/directory/providers/destination/index.js +15 -31
  4. package/dist/directory/providers/destination/index.js.map +1 -1
  5. package/dist/directory/providers/destination/index.mjs +15 -31
  6. package/dist/directory/providers/destination/index.mjs.map +1 -1
  7. package/dist/directory/providers/destination/utils.d.ts +1 -2
  8. package/dist/directory/providers/destination/utils.d.ts.map +1 -1
  9. package/dist/directory/providers/destination/utils.js +7 -36
  10. package/dist/directory/providers/destination/utils.js.map +1 -1
  11. package/dist/directory/providers/destination/utils.mjs +7 -36
  12. package/dist/directory/providers/destination/utils.mjs.map +1 -1
  13. package/dist/directory/providers/source/index.d.ts.map +1 -1
  14. package/dist/directory/providers/source/index.js +7 -7
  15. package/dist/directory/providers/source/index.js.map +1 -1
  16. package/dist/directory/providers/source/index.mjs +7 -7
  17. package/dist/directory/providers/source/index.mjs.map +1 -1
  18. package/dist/engine/index.d.ts.map +1 -1
  19. package/dist/engine/index.js +45 -32
  20. package/dist/engine/index.js.map +1 -1
  21. package/dist/engine/index.mjs +43 -30
  22. package/dist/engine/index.mjs.map +1 -1
  23. package/dist/file/providers/destination/index.d.ts +1 -1
  24. package/dist/file/providers/destination/index.d.ts.map +1 -1
  25. package/dist/file/providers/destination/index.js +46 -48
  26. package/dist/file/providers/destination/index.js.map +1 -1
  27. package/dist/file/providers/destination/index.mjs +46 -48
  28. package/dist/file/providers/destination/index.mjs.map +1 -1
  29. package/dist/file/providers/destination/utils.d.ts +1 -2
  30. package/dist/file/providers/destination/utils.d.ts.map +1 -1
  31. package/dist/file/providers/destination/utils.js +28 -37
  32. package/dist/file/providers/destination/utils.js.map +1 -1
  33. package/dist/file/providers/destination/utils.mjs +28 -37
  34. package/dist/file/providers/destination/utils.mjs.map +1 -1
  35. package/dist/file/providers/source/index.d.ts +1 -1
  36. package/dist/file/providers/source/index.d.ts.map +1 -1
  37. package/dist/file/providers/source/index.js +85 -61
  38. package/dist/file/providers/source/index.js.map +1 -1
  39. package/dist/file/providers/source/index.mjs +84 -60
  40. package/dist/file/providers/source/index.mjs.map +1 -1
  41. package/dist/strapi/providers/local-destination/index.d.ts.map +1 -1
  42. package/dist/strapi/providers/local-destination/index.js +12 -5
  43. package/dist/strapi/providers/local-destination/index.js.map +1 -1
  44. package/dist/strapi/providers/local-destination/index.mjs +13 -6
  45. package/dist/strapi/providers/local-destination/index.mjs.map +1 -1
  46. package/dist/strapi/providers/local-source/configuration.d.ts.map +1 -1
  47. package/dist/strapi/providers/local-source/configuration.js +4 -3
  48. package/dist/strapi/providers/local-source/configuration.js.map +1 -1
  49. package/dist/strapi/providers/local-source/configuration.mjs +2 -1
  50. package/dist/strapi/providers/local-source/configuration.mjs.map +1 -1
  51. package/dist/strapi/providers/local-source/index.d.ts.map +1 -1
  52. package/dist/strapi/providers/local-source/index.js +12 -2
  53. package/dist/strapi/providers/local-source/index.js.map +1 -1
  54. package/dist/strapi/providers/local-source/index.mjs +11 -1
  55. package/dist/strapi/providers/local-source/index.mjs.map +1 -1
  56. package/dist/strapi/remote/handlers/push.js +0 -1
  57. package/dist/strapi/remote/handlers/push.js.map +1 -1
  58. package/dist/strapi/remote/handlers/push.mjs +0 -1
  59. package/dist/strapi/remote/handlers/push.mjs.map +1 -1
  60. package/dist/utils/buffered-entry-writer.d.ts +21 -0
  61. package/dist/utils/buffered-entry-writer.d.ts.map +1 -0
  62. package/dist/utils/buffered-entry-writer.js +95 -0
  63. package/dist/utils/buffered-entry-writer.js.map +1 -0
  64. package/dist/utils/buffered-entry-writer.mjs +93 -0
  65. package/dist/utils/buffered-entry-writer.mjs.map +1 -0
  66. package/dist/utils/defer-writable.d.ts +11 -0
  67. package/dist/utils/defer-writable.d.ts.map +1 -0
  68. package/dist/utils/defer-writable.js +106 -0
  69. package/dist/utils/defer-writable.js.map +1 -0
  70. package/dist/utils/defer-writable.mjs +104 -0
  71. package/dist/utils/defer-writable.mjs.map +1 -0
  72. package/dist/utils/esm-deps.d.ts +16 -0
  73. package/dist/utils/esm-deps.d.ts.map +1 -0
  74. package/dist/utils/esm-deps.js +33 -0
  75. package/dist/utils/esm-deps.js.map +1 -0
  76. package/dist/utils/esm-deps.mjs +31 -0
  77. package/dist/utils/esm-deps.mjs.map +1 -0
  78. package/dist/utils/jsonl-write-stream.d.ts +13 -0
  79. package/dist/utils/jsonl-write-stream.d.ts.map +1 -0
  80. package/dist/utils/jsonl-write-stream.js +75 -0
  81. package/dist/utils/jsonl-write-stream.js.map +1 -0
  82. package/dist/utils/jsonl-write-stream.mjs +73 -0
  83. package/dist/utils/jsonl-write-stream.mjs.map +1 -0
  84. package/dist/utils/transaction.d.ts +6 -1
  85. package/dist/utils/transaction.d.ts.map +1 -1
  86. package/dist/utils/transaction.js +47 -7
  87. package/dist/utils/transaction.js.map +1 -1
  88. package/dist/utils/transaction.mjs +47 -8
  89. package/dist/utils/transaction.mjs.map +1 -1
  90. package/dist/utils/unknown-error.d.ts +7 -0
  91. package/dist/utils/unknown-error.d.ts.map +1 -0
  92. package/dist/utils/unknown-error.js +30 -0
  93. package/dist/utils/unknown-error.js.map +1 -0
  94. package/dist/utils/unknown-error.mjs +27 -0
  95. package/dist/utils/unknown-error.mjs.map +1 -0
  96. package/package.json +7 -10
@@ -0,0 +1,93 @@
1
+ import { Writable } from 'node:stream';
2
+
3
+ const toError = (err)=>err instanceof Error ? err : new Error(String(err));
4
+ /**
5
+ * Writable that buffers chunks and writes them as entries once `maxSize` would be exceeded.
6
+ *
7
+ * - Flush failures reach the stream callbacks instead of becoming unhandled rejections.
8
+ * - Write, final and destroy share one flush chain, so flushes never overlap.
9
+ * - Destroyed without an error: flush what is left and report a failure to the destroy callback.
10
+ * - Destroyed with an error: discard the buffer that has not started flushing, but wait for a flush
11
+ * that is already running before this stream closes. The original error is kept. Callers that
12
+ * wait for this close, including the JSONL write stream, keep rollback from racing that write.
13
+ */ const createBufferedEntryWriter = ({ maxSize, writeEntry, flushOnFinal = false })=>{
14
+ let buffer = '';
15
+ let inFlight;
16
+ const flush = ()=>{
17
+ const run = async ()=>{
18
+ if (!buffer) {
19
+ return;
20
+ }
21
+ // Take the buffer first, a flush already running owns what it took
22
+ const content = buffer;
23
+ buffer = '';
24
+ await writeEntry(content);
25
+ };
26
+ const next = (inFlight ?? Promise.resolve()).catch(()=>undefined).then(run);
27
+ inFlight = next;
28
+ const clear = ()=>{
29
+ if (inFlight === next) {
30
+ inFlight = undefined;
31
+ }
32
+ };
33
+ next.then(clear, clear);
34
+ return next;
35
+ };
36
+ return new Writable({
37
+ async final (callback) {
38
+ if (!flushOnFinal) {
39
+ callback();
40
+ return;
41
+ }
42
+ try {
43
+ await flush();
44
+ } catch (err) {
45
+ callback(toError(err));
46
+ return;
47
+ }
48
+ callback();
49
+ },
50
+ async destroy (err, callback) {
51
+ if (err) {
52
+ // Discard the buffer that has not started flushing. A flush that is already running
53
+ // finishes first, and its own failure must not replace the destroy error.
54
+ buffer = '';
55
+ await inFlight?.catch(()=>undefined);
56
+ callback(err);
57
+ return;
58
+ }
59
+ try {
60
+ await flush();
61
+ } catch (flushError) {
62
+ callback(toError(flushError));
63
+ return;
64
+ }
65
+ callback(null);
66
+ },
67
+ async write (chunk, _encoding, callback) {
68
+ const size = chunk.length;
69
+ if (size > maxSize) {
70
+ callback(new Error(`payload too large: ${size}>${maxSize}`));
71
+ return;
72
+ }
73
+ try {
74
+ if (buffer.length + size > maxSize) {
75
+ await flush();
76
+ }
77
+ } catch (err) {
78
+ callback(toError(err));
79
+ return;
80
+ }
81
+ // Destroyed while flushing: the error path discards data, so do not buffer this chunk
82
+ if (this.destroyed) {
83
+ callback();
84
+ return;
85
+ }
86
+ buffer += chunk;
87
+ callback(null);
88
+ }
89
+ });
90
+ };
91
+
92
+ export { createBufferedEntryWriter };
93
+ //# sourceMappingURL=buffered-entry-writer.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"buffered-entry-writer.mjs","sources":["../../src/utils/buffered-entry-writer.ts"],"sourcesContent":["import { Writable } from 'node:stream';\n\nconst toError = (err: unknown): Error => (err instanceof Error ? err : new Error(String(err)));\n\ninterface BufferedEntryWriterOptions {\n maxSize: number;\n /** Writes one full buffer as a new entry (file). Called one at a time, in order. */\n writeEntry: (content: string) => Promise<void>;\n /** Also flush the remaining buffer when the writable side ends. */\n flushOnFinal?: boolean;\n}\n\n/**\n * Writable that buffers chunks and writes them as entries once `maxSize` would be exceeded.\n *\n * - Flush failures reach the stream callbacks instead of becoming unhandled rejections.\n * - Write, final and destroy share one flush chain, so flushes never overlap.\n * - Destroyed without an error: flush what is left and report a failure to the destroy callback.\n * - Destroyed with an error: discard the buffer that has not started flushing, but wait for a flush\n * that is already running before this stream closes. The original error is kept. Callers that\n * wait for this close, including the JSONL write stream, keep rollback from racing that write.\n */\nexport const createBufferedEntryWriter = ({\n maxSize,\n writeEntry,\n flushOnFinal = false,\n}: BufferedEntryWriterOptions) => {\n let buffer = '';\n let inFlight: Promise<void> | undefined;\n\n const flush = (): Promise<void> => {\n const run = async () => {\n if (!buffer) {\n return;\n }\n\n // Take the buffer first, a flush already running owns what it took\n const content = buffer;\n buffer = '';\n await writeEntry(content);\n };\n\n const next = (inFlight ?? Promise.resolve()).catch(() => undefined).then(run);\n inFlight = next;\n\n const clear = () => {\n if (inFlight === next) {\n inFlight = undefined;\n }\n };\n next.then(clear, clear);\n\n return next;\n };\n\n return new Writable({\n async final(callback) {\n if (!flushOnFinal) {\n callback();\n return;\n }\n\n try {\n await flush();\n } catch (err) {\n callback(toError(err));\n return;\n }\n\n callback();\n },\n\n async destroy(err, callback) {\n if (err) {\n // Discard the buffer that has not started flushing. A flush that is already running\n // finishes first, and its own failure must not replace the destroy error.\n buffer = '';\n await inFlight?.catch(() => undefined);\n callback(err);\n return;\n }\n\n try {\n await flush();\n } catch (flushError) {\n callback(toError(flushError));\n return;\n }\n\n callback(null);\n },\n\n async write(chunk, _encoding, callback) {\n const size = chunk.length;\n\n if (size > maxSize) {\n callback(new Error(`payload too large: ${size}>${maxSize}`));\n return;\n }\n\n try {\n if (buffer.length + size > maxSize) {\n await flush();\n }\n } catch (err) {\n callback(toError(err));\n return;\n }\n\n // Destroyed while flushing: the error path discards data, so do not buffer this chunk\n if (this.destroyed) {\n callback();\n return;\n }\n\n buffer += chunk;\n\n callback(null);\n },\n });\n};\n"],"names":["toError","err","Error","String","createBufferedEntryWriter","maxSize","writeEntry","flushOnFinal","buffer","inFlight","flush","run","content","next","Promise","resolve","catch","undefined","then","clear","Writable","final","callback","destroy","flushError","write","chunk","_encoding","size","length","destroyed"],"mappings":";;AAEA,MAAMA,OAAAA,GAAU,CAACC,GAAAA,GAAyBA,GAAAA,YAAeC,QAAQD,GAAAA,GAAM,IAAIC,MAAMC,MAAAA,CAAOF,GAAAA,CAAAA,CAAAA;AAUxF;;;;;;;;;IAUO,MAAMG,yBAAAA,GAA4B,CAAC,EACxCC,OAAO,EACPC,UAAU,EACVC,YAAAA,GAAe,KAAK,EACO,GAAA;AAC3B,IAAA,IAAIC,MAAAA,GAAS,EAAA;IACb,IAAIC,QAAAA;AAEJ,IAAA,MAAMC,KAAAA,GAAQ,IAAA;AACZ,QAAA,MAAMC,GAAAA,GAAM,UAAA;AACV,YAAA,IAAI,CAACH,MAAAA,EAAQ;AACX,gBAAA;AACF,YAAA;;AAGA,YAAA,MAAMI,OAAAA,GAAUJ,MAAAA;YAChBA,MAAAA,GAAS,EAAA;AACT,YAAA,MAAMF,UAAAA,CAAWM,OAAAA,CAAAA;AACnB,QAAA,CAAA;AAEA,QAAA,MAAMC,IAAAA,GAAQJ,CAAAA,QAAAA,IAAYK,OAAAA,CAAQC,OAAO,EAAC,EAAGC,KAAK,CAAC,IAAMC,SAAAA,CAAAA,CAAWC,IAAI,CAACP,GAAAA,CAAAA;QACzEF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MG,MAAM;AAEzB,YAAA,IAAID,OAAOvB,OAAAA,EAAS;gBAClBiB,QAAAA,CAAS,IAAIpB,MAAM,CAAC,mBAAmB,EAAE0B,IAAAA,CAAK,CAAC,EAAEvB,OAAAA,CAAAA,CAAS,CAAA,CAAA;AAC1D,gBAAA;AACF,YAAA;YAEA,IAAI;AACF,gBAAA,IAAIG,MAAAA,CAAOqB,MAAM,GAAGD,IAAAA,GAAOvB,OAAAA,EAAS;oBAClC,MAAMK,KAAAA,EAAAA;AACR,gBAAA;AACF,YAAA,CAAA,CAAE,OAAOT,GAAAA,EAAK;AACZqB,gBAAAA,QAAAA,CAAStB,OAAAA,CAAQC,GAAAA,CAAAA,CAAAA;AACjB,gBAAA;AACF,YAAA;;YAGA,IAAI,IAAI,CAAC6B,SAAS,EAAE;AAClBR,gBAAAA,QAAAA,EAAAA;AACA,gBAAA;AACF,YAAA;YAEAd,MAAAA,IAAUkB,KAAAA;YAEVJ,QAAAA,CAAS,IAAA,CAAA;AACX,QAAA;AACF,KAAA,CAAA;AACF;;;;"}
@@ -0,0 +1,11 @@
1
+ import { Writable } from 'node:stream';
2
+ /**
3
+ * A Writable that can be handed back before its real destination exists.
4
+ * Writes wait until `open` resolves, and ending the stream waits until that destination ends.
5
+ * Destroy waits until that destination closes, so a flush it is already running finishes
6
+ * before a caller that waits for this stream, including rollback, continues.
7
+ * A failure in `open`, or an error from the destination, destroys this stream with that error.
8
+ */
9
+ declare const deferWritable: (open: () => Promise<Writable>) => Writable;
10
+ export { deferWritable };
11
+ //# sourceMappingURL=defer-writable.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defer-writable.d.ts","sourceRoot":"","sources":["../../src/utils/defer-writable.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAYvC;;;;;;GAMG;AACH,QAAA,MAAM,aAAa,GAAI,MAAM,MAAM,OAAO,CAAC,QAAQ,CAAC,KAAG,QAkHtD,CAAC;AAEF,OAAO,EAAE,aAAa,EAAE,CAAC"}
@@ -0,0 +1,106 @@
1
+ 'use strict';
2
+
3
+ var node_stream = require('node:stream');
4
+ var unknownError = require('./unknown-error.js');
5
+
6
+ /**
7
+ * A Writable that can be handed back before its real destination exists.
8
+ * Writes wait until `open` resolves, and ending the stream waits until that destination ends.
9
+ * Destroy waits until that destination closes, so a flush it is already running finishes
10
+ * before a caller that waits for this stream, including rollback, continues.
11
+ * A failure in `open`, or an error from the destination, destroys this stream with that error.
12
+ */ const deferWritable = (open)=>{
13
+ let target;
14
+ // Writable does not call write() again until the previous callback runs, so at most one
15
+ // write can be waiting for the destination.
16
+ let pending;
17
+ const opening = Promise.resolve().then(open).then((destination)=>{
18
+ target = destination;
19
+ // Swallow a destroy error when this proxy is already destroyed. Otherwise an
20
+ // in-flight flush that fails while we are waiting for close becomes unhandled.
21
+ destination.on('error', (error)=>{
22
+ if (!proxy.destroyed) {
23
+ proxy.destroy(error);
24
+ }
25
+ });
26
+ if (proxy.destroyed) {
27
+ return;
28
+ }
29
+ if (pending) {
30
+ const { chunk, encoding, callback } = pending;
31
+ pending = undefined;
32
+ destination.write(chunk, encoding, callback);
33
+ }
34
+ }, (error)=>{
35
+ proxy.destroy(unknownError.errorFromUnknown(error));
36
+ });
37
+ const proxy = new node_stream.Writable({
38
+ objectMode: true,
39
+ write (chunk, encoding, callback) {
40
+ if (target) {
41
+ target.write(chunk, encoding, callback);
42
+ return;
43
+ }
44
+ pending = {
45
+ chunk,
46
+ encoding,
47
+ callback
48
+ };
49
+ },
50
+ final (callback) {
51
+ opening.then(()=>{
52
+ if (!target) {
53
+ callback(new Error('Write stream ended before it was ready'));
54
+ return;
55
+ }
56
+ target.end(callback);
57
+ }).catch((error)=>{
58
+ callback(unknownError.errorFromUnknown(error));
59
+ });
60
+ },
61
+ destroy (error, callback) {
62
+ if (pending) {
63
+ const { callback: pendingCallback } = pending;
64
+ pending = undefined;
65
+ pendingCallback(error ?? new Error('Stream destroyed'));
66
+ }
67
+ let finished = false;
68
+ const finish = ()=>{
69
+ if (finished) {
70
+ return;
71
+ }
72
+ finished = true;
73
+ callback(error);
74
+ };
75
+ // Rollback starts only after pipeline() rejects, and pipeline() waits for this close.
76
+ // Waiting here keeps a destination flush that is already running ahead of that rollback.
77
+ const waitForClose = (stream)=>{
78
+ if (stream.closed) {
79
+ finish();
80
+ return;
81
+ }
82
+ stream.once('close', finish);
83
+ if (!stream.destroyed) {
84
+ stream.destroy(error ?? undefined);
85
+ }
86
+ };
87
+ if (target) {
88
+ waitForClose(target);
89
+ return;
90
+ }
91
+ opening.then(()=>{
92
+ if (target) {
93
+ waitForClose(target);
94
+ return;
95
+ }
96
+ finish();
97
+ }, ()=>{
98
+ finish();
99
+ });
100
+ }
101
+ });
102
+ return proxy;
103
+ };
104
+
105
+ exports.deferWritable = deferWritable;
106
+ //# sourceMappingURL=defer-writable.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defer-writable.js","sources":["../../src/utils/defer-writable.ts"],"sourcesContent":["import { Writable } from 'node:stream';\n\nimport { errorFromUnknown } from './unknown-error';\n\ntype WriteCallback = (error?: Error | null) => void;\n\ninterface PendingWrite {\n chunk: unknown;\n encoding: BufferEncoding;\n callback: WriteCallback;\n}\n\n/**\n * A Writable that can be handed back before its real destination exists.\n * Writes wait until `open` resolves, and ending the stream waits until that destination ends.\n * Destroy waits until that destination closes, so a flush it is already running finishes\n * before a caller that waits for this stream, including rollback, continues.\n * A failure in `open`, or an error from the destination, destroys this stream with that error.\n */\nconst deferWritable = (open: () => Promise<Writable>): Writable => {\n let target: Writable | undefined;\n // Writable does not call write() again until the previous callback runs, so at most one\n // write can be waiting for the destination.\n let pending: PendingWrite | undefined;\n\n const opening = Promise.resolve()\n .then(open)\n .then(\n (destination) => {\n target = destination;\n // Swallow a destroy error when this proxy is already destroyed. Otherwise an\n // in-flight flush that fails while we are waiting for close becomes unhandled.\n destination.on('error', (error: Error) => {\n if (!proxy.destroyed) {\n proxy.destroy(error);\n }\n });\n\n if (proxy.destroyed) {\n return;\n }\n\n if (pending) {\n const { chunk, encoding, callback } = pending;\n pending = undefined;\n destination.write(chunk, encoding, callback);\n }\n },\n (error: unknown) => {\n proxy.destroy(errorFromUnknown(error));\n }\n );\n\n const proxy = new Writable({\n objectMode: true,\n\n write(chunk, encoding, callback) {\n if (target) {\n target.write(chunk, encoding, callback);\n return;\n }\n\n pending = { chunk, encoding, callback };\n },\n\n final(callback) {\n opening\n .then(() => {\n if (!target) {\n callback(new Error('Write stream ended before it was ready'));\n return;\n }\n\n target.end(callback);\n })\n .catch((error: unknown) => {\n callback(errorFromUnknown(error));\n });\n },\n\n destroy(error, callback) {\n if (pending) {\n const { callback: pendingCallback } = pending;\n pending = undefined;\n pendingCallback(error ?? new Error('Stream destroyed'));\n }\n\n let finished = false;\n const finish = () => {\n if (finished) {\n return;\n }\n\n finished = true;\n callback(error);\n };\n\n // Rollback starts only after pipeline() rejects, and pipeline() waits for this close.\n // Waiting here keeps a destination flush that is already running ahead of that rollback.\n const waitForClose = (stream: Writable) => {\n if (stream.closed) {\n finish();\n return;\n }\n\n stream.once('close', finish);\n if (!stream.destroyed) {\n stream.destroy(error ?? undefined);\n }\n };\n\n if (target) {\n waitForClose(target);\n return;\n }\n\n opening.then(\n () => {\n if (target) {\n waitForClose(target);\n return;\n }\n\n finish();\n },\n () => {\n finish();\n }\n );\n },\n });\n\n return proxy;\n};\n\nexport { deferWritable };\n"],"names":["deferWritable","open","target","pending","opening","Promise","resolve","then","destination","on","error","proxy","destroyed","destroy","chunk","encoding","callback","undefined","write","errorFromUnknown","Writable","objectMode","final","Error","end","catch","pendingCallback","finished","finish","waitForClose","stream","closed","once"],"mappings":";;;;;AAYA;;;;;;IAOA,MAAMA,gBAAgB,CAACC,IAAAA,GAAAA;IACrB,IAAIC,MAAAA;;;IAGJ,IAAIC,OAAAA;IAEJ,MAAMC,OAAAA,GAAUC,QAAQC,OAAO,EAAA,CAC5BC,IAAI,CAACN,IAAAA,C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AACb,gBAAA;AACF,YAAA;AAEAE,YAAAA,OAAAA,CAAQG,IAAI,CACV,IAAA;AACE,gBAAA,IAAIL,MAAAA,EAAQ;oBACV2B,YAAAA,CAAa3B,MAAAA,CAAAA;AACb,oBAAA;AACF,gBAAA;AAEA0B,gBAAAA,MAAAA,EAAAA;YACF,CAAA,EACA,IAAA;AACEA,gBAAAA,MAAAA,EAAAA;AACF,YAAA,CAAA,CAAA;AAEJ,QAAA;AACF,KAAA,CAAA;IAEA,OAAOjB,KAAAA;AACT;;;;"}
@@ -0,0 +1,104 @@
1
+ import { Writable } from 'node:stream';
2
+ import { errorFromUnknown } from './unknown-error.mjs';
3
+
4
+ /**
5
+ * A Writable that can be handed back before its real destination exists.
6
+ * Writes wait until `open` resolves, and ending the stream waits until that destination ends.
7
+ * Destroy waits until that destination closes, so a flush it is already running finishes
8
+ * before a caller that waits for this stream, including rollback, continues.
9
+ * A failure in `open`, or an error from the destination, destroys this stream with that error.
10
+ */ const deferWritable = (open)=>{
11
+ let target;
12
+ // Writable does not call write() again until the previous callback runs, so at most one
13
+ // write can be waiting for the destination.
14
+ let pending;
15
+ const opening = Promise.resolve().then(open).then((destination)=>{
16
+ target = destination;
17
+ // Swallow a destroy error when this proxy is already destroyed. Otherwise an
18
+ // in-flight flush that fails while we are waiting for close becomes unhandled.
19
+ destination.on('error', (error)=>{
20
+ if (!proxy.destroyed) {
21
+ proxy.destroy(error);
22
+ }
23
+ });
24
+ if (proxy.destroyed) {
25
+ return;
26
+ }
27
+ if (pending) {
28
+ const { chunk, encoding, callback } = pending;
29
+ pending = undefined;
30
+ destination.write(chunk, encoding, callback);
31
+ }
32
+ }, (error)=>{
33
+ proxy.destroy(errorFromUnknown(error));
34
+ });
35
+ const proxy = new Writable({
36
+ objectMode: true,
37
+ write (chunk, encoding, callback) {
38
+ if (target) {
39
+ target.write(chunk, encoding, callback);
40
+ return;
41
+ }
42
+ pending = {
43
+ chunk,
44
+ encoding,
45
+ callback
46
+ };
47
+ },
48
+ final (callback) {
49
+ opening.then(()=>{
50
+ if (!target) {
51
+ callback(new Error('Write stream ended before it was ready'));
52
+ return;
53
+ }
54
+ target.end(callback);
55
+ }).catch((error)=>{
56
+ callback(errorFromUnknown(error));
57
+ });
58
+ },
59
+ destroy (error, callback) {
60
+ if (pending) {
61
+ const { callback: pendingCallback } = pending;
62
+ pending = undefined;
63
+ pendingCallback(error ?? new Error('Stream destroyed'));
64
+ }
65
+ let finished = false;
66
+ const finish = ()=>{
67
+ if (finished) {
68
+ return;
69
+ }
70
+ finished = true;
71
+ callback(error);
72
+ };
73
+ // Rollback starts only after pipeline() rejects, and pipeline() waits for this close.
74
+ // Waiting here keeps a destination flush that is already running ahead of that rollback.
75
+ const waitForClose = (stream)=>{
76
+ if (stream.closed) {
77
+ finish();
78
+ return;
79
+ }
80
+ stream.once('close', finish);
81
+ if (!stream.destroyed) {
82
+ stream.destroy(error ?? undefined);
83
+ }
84
+ };
85
+ if (target) {
86
+ waitForClose(target);
87
+ return;
88
+ }
89
+ opening.then(()=>{
90
+ if (target) {
91
+ waitForClose(target);
92
+ return;
93
+ }
94
+ finish();
95
+ }, ()=>{
96
+ finish();
97
+ });
98
+ }
99
+ });
100
+ return proxy;
101
+ };
102
+
103
+ export { deferWritable };
104
+ //# sourceMappingURL=defer-writable.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defer-writable.mjs","sources":["../../src/utils/defer-writable.ts"],"sourcesContent":["import { Writable } from 'node:stream';\n\nimport { errorFromUnknown } from './unknown-error';\n\ntype WriteCallback = (error?: Error | null) => void;\n\ninterface PendingWrite {\n chunk: unknown;\n encoding: BufferEncoding;\n callback: WriteCallback;\n}\n\n/**\n * A Writable that can be handed back before its real destination exists.\n * Writes wait until `open` resolves, and ending the stream waits until that destination ends.\n * Destroy waits until that destination closes, so a flush it is already running finishes\n * before a caller that waits for this stream, including rollback, continues.\n * A failure in `open`, or an error from the destination, destroys this stream with that error.\n */\nconst deferWritable = (open: () => Promise<Writable>): Writable => {\n let target: Writable | undefined;\n // Writable does not call write() again until the previous callback runs, so at most one\n // write can be waiting for the destination.\n let pending: PendingWrite | undefined;\n\n const opening = Promise.resolve()\n .then(open)\n .then(\n (destination) => {\n target = destination;\n // Swallow a destroy error when this proxy is already destroyed. Otherwise an\n // in-flight flush that fails while we are waiting for close becomes unhandled.\n destination.on('error', (error: Error) => {\n if (!proxy.destroyed) {\n proxy.destroy(error);\n }\n });\n\n if (proxy.destroyed) {\n return;\n }\n\n if (pending) {\n const { chunk, encoding, callback } = pending;\n pending = undefined;\n destination.write(chunk, encoding, callback);\n }\n },\n (error: unknown) => {\n proxy.destroy(errorFromUnknown(error));\n }\n );\n\n const proxy = new Writable({\n objectMode: true,\n\n write(chunk, encoding, callback) {\n if (target) {\n target.write(chunk, encoding, callback);\n return;\n }\n\n pending = { chunk, encoding, callback };\n },\n\n final(callback) {\n opening\n .then(() => {\n if (!target) {\n callback(new Error('Write stream ended before it was ready'));\n return;\n }\n\n target.end(callback);\n })\n .catch((error: unknown) => {\n callback(errorFromUnknown(error));\n });\n },\n\n destroy(error, callback) {\n if (pending) {\n const { callback: pendingCallback } = pending;\n pending = undefined;\n pendingCallback(error ?? new Error('Stream destroyed'));\n }\n\n let finished = false;\n const finish = () => {\n if (finished) {\n return;\n }\n\n finished = true;\n callback(error);\n };\n\n // Rollback starts only after pipeline() rejects, and pipeline() waits for this close.\n // Waiting here keeps a destination flush that is already running ahead of that rollback.\n const waitForClose = (stream: Writable) => {\n if (stream.closed) {\n finish();\n return;\n }\n\n stream.once('close', finish);\n if (!stream.destroyed) {\n stream.destroy(error ?? undefined);\n }\n };\n\n if (target) {\n waitForClose(target);\n return;\n }\n\n opening.then(\n () => {\n if (target) {\n waitForClose(target);\n return;\n }\n\n finish();\n },\n () => {\n finish();\n }\n );\n },\n });\n\n return proxy;\n};\n\nexport { deferWritable };\n"],"names":["deferWritable","open","target","pending","opening","Promise","resolve","then","destination","on","error","proxy","destroyed","destroy","chunk","encoding","callback","undefined","write","errorFromUnknown","Writable","objectMode","final","Error","end","catch","pendingCallback","finished","finish","waitForClose","stream","closed","once"],"mappings":";;;AAYA;;;;;;IAOA,MAAMA,gBAAgB,CAACC,IAAAA,GAAAA;IACrB,IAAIC,MAAAA;;;IAGJ,IAAIC,OAAAA;IAEJ,MAAMC,OAAAA,GAAUC,QAAQC,OAAO,EAAA,CAC5BC,IAAI,CAACN,IAAAA,CAAAA,CACLM,IAAI,CACH,CAACC,WAAAA,GAAAA;QACCN,MAAAA,GAASM,WAAAA;;;QAGTA,WAAAA,CAAYC,EAAE,CAAC,OAAA,EAAS,CAACC,KAAAA,GAAAA;YACvB,IAAI,CAACC,KAAAA,CAAMC,SAAS,EAAE;AACpBD,gBAAAA,KAAAA,CAAME,OAAO,CAACH,KAAAA,CAAAA;AAChB,YAAA;AACF,QAAA,CAAA,CAAA;QAEA,IAAIC,KAAAA,CAAMC,SAAS,EAAE;AACnB,YAAA;AACF,QAAA;AAEA,QAAA,IAAIT,OAAAA,EAAS;AACX,YAAA,MAAM,EAAEW,KAAK,EAAEC,QAAQ,EAAEC,QAAQ,EAAE,GAAGb,OAAAA;YACtCA,OAAAA,GAAUc,SAAAA;YACVT,WAAAA,CAAYU,KAAK,CAACJ,KAAAA,EAAOC,QAAAA,EAAUC,QAAAA,CAAAA;AACrC,QAAA;AACF,IAAA,CAAA,EACA,CAACN,KAAAA,GAAAA;QACCC,KAAAA,CAAME,OAAO,CAACM,gBAAAA,CAAiBT,KAAAA,CAAAA,CAAAA;AACjC,IAAA,CAAA,CAAA;IAGJ,MAAMC,KAAAA,GAAQ,IAAIS,QAAAA,CAAS;QACzBC,UAAAA,EAAY,IAAA;AAEZH,QAAAA,KAAAA,CAAAA,CAAMJ,KAAK,EAAEC,QAAQ,EAAEC,QAAQ,EAAA;AAC7B,YAAA,IAAId,MAAAA,EAAQ;gBACVA,MAAAA,CAAOgB,KAAK,CAACJ,KAAAA,EAAOC,QAAAA,EAAUC,QAAAA,CAAAA;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CAAOE,IAAI,CAAC,OAAA,EAASJ,MAAAA,CAAAA;gBACrB,IAAI,CAACE,MAAAA,CAAOlB,SAAS,EAAE;oBACrBkB,MAAAA,CAAOjB,OAAO,CAACH,KAAAA,IAASO,SAAAA,CAAAA;AAC1B,gBAAA;AACF,YAAA,CAAA;AAEA,YAAA,IAAIf,MAAAA,EAAQ;gBACV2B,YAAAA,CAAa3B,MAAAA,CAAAA;AACb,gBAAA;AACF,YAAA;AAEAE,YAAAA,OAAAA,CAAQG,IAAI,CACV,IAAA;AACE,gBAAA,IAAIL,MAAAA,EAAQ;oBACV2B,YAAAA,CAAa3B,MAAAA,CAAAA;AACb,oBAAA;AACF,gBAAA;AAEA0B,gBAAAA,MAAAA,EAAAA;YACF,CAAA,EACA,IAAA;AACEA,gBAAAA,MAAAA,EAAAA;AACF,YAAA,CAAA,CAAA;AAEJ,QAAA;AACF,KAAA,CAAA;IAEA,OAAOjB,KAAAA;AACT;;;;"}
@@ -0,0 +1,16 @@
1
+ type StreamChainModule = typeof import('stream-chain');
2
+ type JsonlParserStreamModule = typeof import('stream-chain/jsonl/parserStream.js');
3
+ type JsonlStringerStreamModule = typeof import('stream-chain/jsonl/stringerStream.js');
4
+ export interface StreamDeps {
5
+ chain: StreamChainModule['chainUnchecked'];
6
+ parser: JsonlParserStreamModule['parserStream'];
7
+ stringer: JsonlStringerStreamModule['stringerStream'];
8
+ }
9
+ /**
10
+ * Loads stream-chain via dynamic import (CJS-safe) and caches it.
11
+ * The package is ESM-only, so it cannot be loaded with require() on Node < 20.19 / 22.12
12
+ * or under Jest without --experimental-vm-modules.
13
+ */
14
+ export declare const loadStreamDeps: () => Promise<StreamDeps>;
15
+ export {};
16
+ //# sourceMappingURL=esm-deps.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"esm-deps.d.ts","sourceRoot":"","sources":["../../src/utils/esm-deps.ts"],"names":[],"mappings":"AAAA,KAAK,iBAAiB,GAAG,cAAc,cAAc,CAAC,CAAC;AACvD,KAAK,uBAAuB,GAAG,cAAc,oCAAoC,CAAC,CAAC;AACnF,KAAK,yBAAyB,GAAG,cAAc,sCAAsC,CAAC,CAAC;AAEvF,MAAM,WAAW,UAAU;IACzB,KAAK,EAAE,iBAAiB,CAAC,gBAAgB,CAAC,CAAC;IAC3C,MAAM,EAAE,uBAAuB,CAAC,cAAc,CAAC,CAAC;IAChD,QAAQ,EAAE,yBAAyB,CAAC,gBAAgB,CAAC,CAAC;CACvD;AAID;;;;GAIG;AACH,eAAO,MAAM,cAAc,QAAO,OAAO,CAAC,UAAU,CA0BnD,CAAC"}
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ let cached;
4
+ /**
5
+ * Loads stream-chain via dynamic import (CJS-safe) and caches it.
6
+ * The package is ESM-only, so it cannot be loaded with require() on Node < 20.19 / 22.12
7
+ * or under Jest without --experimental-vm-modules.
8
+ */ const loadStreamDeps = ()=>{
9
+ if (cached !== undefined) {
10
+ return cached;
11
+ }
12
+ // Drop a rejected load so a later transfer can try again. A resolved load stays cached.
13
+ const pending = Promise.all([
14
+ import('stream-chain'),
15
+ // Extension is required: stream-chain's "./*" export does not resolve the extensionless subpath.
16
+ import('stream-chain/jsonl/parserStream.js'),
17
+ import('stream-chain/jsonl/stringerStream.js')
18
+ ]).then(([streamChain, jsonlParser, jsonlStringer])=>({
19
+ chain: streamChain.chainUnchecked,
20
+ parser: jsonlParser.parserStream,
21
+ stringer: jsonlStringer.stringerStream
22
+ })).catch((error)=>{
23
+ if (cached === pending) {
24
+ cached = undefined;
25
+ }
26
+ throw error;
27
+ });
28
+ cached = pending;
29
+ return pending;
30
+ };
31
+
32
+ exports.loadStreamDeps = loadStreamDeps;
33
+ //# sourceMappingURL=esm-deps.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"esm-deps.js","sources":["../../src/utils/esm-deps.ts"],"sourcesContent":["type StreamChainModule = typeof import('stream-chain');\ntype JsonlParserStreamModule = typeof import('stream-chain/jsonl/parserStream.js');\ntype JsonlStringerStreamModule = typeof import('stream-chain/jsonl/stringerStream.js');\n\nexport interface StreamDeps {\n chain: StreamChainModule['chainUnchecked'];\n parser: JsonlParserStreamModule['parserStream'];\n stringer: JsonlStringerStreamModule['stringerStream'];\n}\n\nlet cached: Promise<StreamDeps> | undefined;\n\n/**\n * Loads stream-chain via dynamic import (CJS-safe) and caches it.\n * The package is ESM-only, so it cannot be loaded with require() on Node < 20.19 / 22.12\n * or under Jest without --experimental-vm-modules.\n */\nexport const loadStreamDeps = (): Promise<StreamDeps> => {\n if (cached !== undefined) {\n return cached;\n }\n\n // Drop a rejected load so a later transfer can try again. A resolved load stays cached.\n const pending = Promise.all([\n import('stream-chain'),\n // Extension is required: stream-chain's \"./*\" export does not resolve the extensionless subpath.\n import('stream-chain/jsonl/parserStream.js'), // eslint-disable-line import/extensions\n import('stream-chain/jsonl/stringerStream.js'), // eslint-disable-line import/extensions\n ])\n .then(([streamChain, jsonlParser, jsonlStringer]) => ({\n chain: streamChain.chainUnchecked,\n parser: jsonlParser.parserStream,\n stringer: jsonlStringer.stringerStream,\n }))\n .catch((error: unknown) => {\n if (cached === pending) {\n cached = undefined;\n }\n throw error;\n });\n\n cached = pending;\n return pending;\n};\n"],"names":["cached","loadStreamDeps","undefined","pending","Promise","all","then","streamChain","jsonlParser","jsonlStringer","chain","chainUnchecked","parser","parserStream","stringer","stringerStream","catch","error"],"mappings":";;AAUA,IAAIA,MAAAA;AAEJ;;;;UAKaC,cAAAA,GAAiB,IAAA;AAC5B,IAAA,IAAID,WAAWE,SAAAA,EAAW;QACxB,OAAOF,MAAAA;AACT,IAAA;;IAGA,MAAMG,OAAAA,GAAUC,OAAAA,CAAQC,GAAG,CAAC;AAC1B,QAAA,OAAO,cAAA,CAAA;;AAEP,QAAA,OAAO,oCAAA,CAAA;AACP,QAAA,OAAO,sCAAA;KACR,CAAA,CACEC,IAAI,CAAC,CAAC,CAACC,aAAaC,WAAAA,EAAaC,aAAAA,CAAc,IAAM;AACpDC,YAAAA,KAAAA,EAAOH,YAAYI,cAAc;AACjCC,YAAAA,MAAAA,EAAQJ,YAAYK,YAAY;AAChCC,YAAAA,QAAAA,EAAUL,cAAcM;SAC1B,CAAA,CAAA,CACCC,KAAK,CAAC,CAACC,KAAAA,GAAAA;AACN,QAAA,IAAIjB,WAAWG,OAAAA,EAAS;YACtBH,MAAAA,GAASE,SAAAA;AACX,QAAA;QACA,MAAMe,KAAAA;AACR,IAAA,CAAA,CAAA;IAEFjB,MAAAA,GAASG,OAAAA;IACT,OAAOA,OAAAA;AACT;;;;"}
@@ -0,0 +1,31 @@
1
+ let cached;
2
+ /**
3
+ * Loads stream-chain via dynamic import (CJS-safe) and caches it.
4
+ * The package is ESM-only, so it cannot be loaded with require() on Node < 20.19 / 22.12
5
+ * or under Jest without --experimental-vm-modules.
6
+ */ const loadStreamDeps = ()=>{
7
+ if (cached !== undefined) {
8
+ return cached;
9
+ }
10
+ // Drop a rejected load so a later transfer can try again. A resolved load stays cached.
11
+ const pending = Promise.all([
12
+ import('stream-chain'),
13
+ // Extension is required: stream-chain's "./*" export does not resolve the extensionless subpath.
14
+ import('stream-chain/jsonl/parserStream.js'),
15
+ import('stream-chain/jsonl/stringerStream.js')
16
+ ]).then(([streamChain, jsonlParser, jsonlStringer])=>({
17
+ chain: streamChain.chainUnchecked,
18
+ parser: jsonlParser.parserStream,
19
+ stringer: jsonlStringer.stringerStream
20
+ })).catch((error)=>{
21
+ if (cached === pending) {
22
+ cached = undefined;
23
+ }
24
+ throw error;
25
+ });
26
+ cached = pending;
27
+ return pending;
28
+ };
29
+
30
+ export { loadStreamDeps };
31
+ //# sourceMappingURL=esm-deps.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"esm-deps.mjs","sources":["../../src/utils/esm-deps.ts"],"sourcesContent":["type StreamChainModule = typeof import('stream-chain');\ntype JsonlParserStreamModule = typeof import('stream-chain/jsonl/parserStream.js');\ntype JsonlStringerStreamModule = typeof import('stream-chain/jsonl/stringerStream.js');\n\nexport interface StreamDeps {\n chain: StreamChainModule['chainUnchecked'];\n parser: JsonlParserStreamModule['parserStream'];\n stringer: JsonlStringerStreamModule['stringerStream'];\n}\n\nlet cached: Promise<StreamDeps> | undefined;\n\n/**\n * Loads stream-chain via dynamic import (CJS-safe) and caches it.\n * The package is ESM-only, so it cannot be loaded with require() on Node < 20.19 / 22.12\n * or under Jest without --experimental-vm-modules.\n */\nexport const loadStreamDeps = (): Promise<StreamDeps> => {\n if (cached !== undefined) {\n return cached;\n }\n\n // Drop a rejected load so a later transfer can try again. A resolved load stays cached.\n const pending = Promise.all([\n import('stream-chain'),\n // Extension is required: stream-chain's \"./*\" export does not resolve the extensionless subpath.\n import('stream-chain/jsonl/parserStream.js'), // eslint-disable-line import/extensions\n import('stream-chain/jsonl/stringerStream.js'), // eslint-disable-line import/extensions\n ])\n .then(([streamChain, jsonlParser, jsonlStringer]) => ({\n chain: streamChain.chainUnchecked,\n parser: jsonlParser.parserStream,\n stringer: jsonlStringer.stringerStream,\n }))\n .catch((error: unknown) => {\n if (cached === pending) {\n cached = undefined;\n }\n throw error;\n });\n\n cached = pending;\n return pending;\n};\n"],"names":["cached","loadStreamDeps","undefined","pending","Promise","all","then","streamChain","jsonlParser","jsonlStringer","chain","chainUnchecked","parser","parserStream","stringer","stringerStream","catch","error"],"mappings":"AAUA,IAAIA,MAAAA;AAEJ;;;;UAKaC,cAAAA,GAAiB,IAAA;AAC5B,IAAA,IAAID,WAAWE,SAAAA,EAAW;QACxB,OAAOF,MAAAA;AACT,IAAA;;IAGA,MAAMG,OAAAA,GAAUC,OAAAA,CAAQC,GAAG,CAAC;AAC1B,QAAA,OAAO,cAAA,CAAA;;AAEP,QAAA,OAAO,oCAAA,CAAA;AACP,QAAA,OAAO,sCAAA;KACR,CAAA,CACEC,IAAI,CAAC,CAAC,CAACC,aAAaC,WAAAA,EAAaC,aAAAA,CAAc,IAAM;AACpDC,YAAAA,KAAAA,EAAOH,YAAYI,cAAc;AACjCC,YAAAA,MAAAA,EAAQJ,YAAYK,YAAY;AAChCC,YAAAA,QAAAA,EAAUL,cAAcM;SAC1B,CAAA,CAAA,CACCC,KAAK,CAAC,CAACC,KAAAA,GAAAA;AACN,QAAA,IAAIjB,WAAWG,OAAAA,EAAS;YACtBH,MAAAA,GAASE,SAAAA;AACX,QAAA;QACA,MAAMe,KAAAA;AACR,IAAA,CAAA,CAAA;IAEFjB,MAAAA,GAASG,OAAAA;IACT,OAAOA,OAAAA;AACT;;;;"}
@@ -0,0 +1,13 @@
1
+ import { Writable } from 'node:stream';
2
+ /**
3
+ * Object-mode Writable that serializes each chunk as one JSONL line into `entryStream`.
4
+ *
5
+ * It finishes only once `entryStream` has finished and closed. Entry writers flush their last
6
+ * file when destroyed without an error. When destroyed with an error they discard the buffer that
7
+ * has not started flushing. This stream waits for a flush that is already running to finish
8
+ * before it closes, so a rollback that waits for this stream does not race that write.
9
+ * Any `entryStream` error, including one raised after the input ended, fails this stream.
10
+ */
11
+ declare const createJsonlWriteStream: (entryStream: Writable) => Writable;
12
+ export { createJsonlWriteStream };
13
+ //# sourceMappingURL=jsonl-write-stream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsonl-write-stream.d.ts","sourceRoot":"","sources":["../../src/utils/jsonl-write-stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAMvC;;;;;;;;GAQG;AACH,QAAA,MAAM,sBAAsB,GAAI,aAAa,QAAQ,KAAG,QAkEvD,CAAC;AAEF,OAAO,EAAE,sBAAsB,EAAE,CAAC"}
@@ -0,0 +1,75 @@
1
+ 'use strict';
2
+
3
+ var node_stream = require('node:stream');
4
+ var promises = require('node:stream/promises');
5
+ var deferWritable = require('./defer-writable.js');
6
+ var esmDeps = require('./esm-deps.js');
7
+
8
+ /**
9
+ * Object-mode Writable that serializes each chunk as one JSONL line into `entryStream`.
10
+ *
11
+ * It finishes only once `entryStream` has finished and closed. Entry writers flush their last
12
+ * file when destroyed without an error. When destroyed with an error they discard the buffer that
13
+ * has not started flushing. This stream waits for a flush that is already running to finish
14
+ * before it closes, so a rollback that waits for this stream does not race that write.
15
+ * Any `entryStream` error, including one raised after the input ended, fails this stream.
16
+ */ const createJsonlWriteStream = (entryStream)=>{
17
+ return deferWritable.deferWritable(async ()=>{
18
+ const { stringer } = await esmDeps.loadStreamDeps();
19
+ const jsonl = stringer();
20
+ // Registered before pipeline() so the cause is known before pipeline destroys `jsonl`;
21
+ // otherwise the next write fails with ERR_STREAM_DESTROYED instead of the real error.
22
+ let entryError;
23
+ entryStream.once('error', (error)=>{
24
+ entryError = error;
25
+ });
26
+ const written = promises.pipeline(jsonl, entryStream);
27
+ const writer = new node_stream.Writable({
28
+ objectMode: true,
29
+ write (chunk, encoding, callback) {
30
+ if (entryError) {
31
+ callback(entryError);
32
+ return;
33
+ }
34
+ jsonl.write(chunk, encoding, (error)=>{
35
+ callback(error ? entryError ?? error : null);
36
+ });
37
+ },
38
+ final (callback) {
39
+ jsonl.end();
40
+ written.then(()=>callback(), callback);
41
+ },
42
+ destroy (error, callback) {
43
+ // Pass the error through. An error-free destroy flushes the entry buffer and would write
44
+ // a partial file while rollback deletes the export. Wait for the entry stream to close so
45
+ // a flush that is already running finishes before that rollback.
46
+ let finished = false;
47
+ const finish = ()=>{
48
+ if (finished) {
49
+ return;
50
+ }
51
+ finished = true;
52
+ callback(error);
53
+ };
54
+ if (!jsonl.destroyed) {
55
+ jsonl.destroy(error ?? undefined);
56
+ }
57
+ if (entryStream.closed) {
58
+ finish();
59
+ return;
60
+ }
61
+ entryStream.once('close', finish);
62
+ if (!entryStream.destroyed) {
63
+ entryStream.destroy(error ?? undefined);
64
+ }
65
+ }
66
+ });
67
+ written.catch((error)=>{
68
+ writer.destroy(error);
69
+ });
70
+ return writer;
71
+ });
72
+ };
73
+
74
+ exports.createJsonlWriteStream = createJsonlWriteStream;
75
+ //# sourceMappingURL=jsonl-write-stream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsonl-write-stream.js","sources":["../../src/utils/jsonl-write-stream.ts"],"sourcesContent":["import { Writable } from 'node:stream';\nimport { pipeline } from 'node:stream/promises';\n\nimport { deferWritable } from './defer-writable';\nimport { loadStreamDeps } from './esm-deps';\n\n/**\n * Object-mode Writable that serializes each chunk as one JSONL line into `entryStream`.\n *\n * It finishes only once `entryStream` has finished and closed. Entry writers flush their last\n * file when destroyed without an error. When destroyed with an error they discard the buffer that\n * has not started flushing. This stream waits for a flush that is already running to finish\n * before it closes, so a rollback that waits for this stream does not race that write.\n * Any `entryStream` error, including one raised after the input ended, fails this stream.\n */\nconst createJsonlWriteStream = (entryStream: Writable): Writable => {\n return deferWritable(async () => {\n const { stringer } = await loadStreamDeps();\n const jsonl = stringer();\n\n // Registered before pipeline() so the cause is known before pipeline destroys `jsonl`;\n // otherwise the next write fails with ERR_STREAM_DESTROYED instead of the real error.\n let entryError: Error | undefined;\n entryStream.once('error', (error: Error) => {\n entryError = error;\n });\n\n const written = pipeline(jsonl, entryStream);\n\n const writer = new Writable({\n objectMode: true,\n write(chunk, encoding, callback) {\n if (entryError) {\n callback(entryError);\n return;\n }\n\n jsonl.write(chunk, encoding, (error) => {\n callback(error ? (entryError ?? error) : null);\n });\n },\n final(callback) {\n jsonl.end();\n written.then(() => callback(), callback);\n },\n destroy(error, callback) {\n // Pass the error through. An error-free destroy flushes the entry buffer and would write\n // a partial file while rollback deletes the export. Wait for the entry stream to close so\n // a flush that is already running finishes before that rollback.\n let finished = false;\n const finish = () => {\n if (finished) {\n return;\n }\n\n finished = true;\n callback(error);\n };\n\n if (!jsonl.destroyed) {\n jsonl.destroy(error ?? undefined);\n }\n\n if (entryStream.closed) {\n finish();\n return;\n }\n\n entryStream.once('close', finish);\n if (!entryStream.destroyed) {\n entryStream.destroy(error ?? undefined);\n }\n },\n });\n\n written.catch((error: Error) => {\n writer.destroy(error);\n });\n\n return writer;\n });\n};\n\nexport { createJsonlWriteStream };\n"],"names":["createJsonlWriteStream","entryStream","deferWritable","stringer","loadStreamDeps","jsonl","entryError","once","error","written","pipeline","writer","Writable","objectMode","write","chunk","encoding","callback","final","end","then","destroy","finished","finish","destroyed","undefined","closed","catch"],"mappings":";;;;;;;AAMA;;;;;;;;IASA,MAAMA,yBAAyB,CAACC,WAAAA,GAAAA;AAC9B,IAAA,OAAOC,2BAAAA,CAAc,UAAA;AACnB,QAAA,MAAM,EAAEC,QAAQ,EAAE,GAAG,MAAMC,sBAAAA,EAAAA;AAC3B,QAAA,MAAMC,KAAAA,GAAQF,QAAAA,EAAAA;;;QAId,IAAIG,UAAAA;QACJL,WAAAA,CAAYM,IAAI,CAAC,OAAA,EAAS,CAACC,KAAAA,GAAAA;YACzBF,UAAAA,GAAaE,KAAAA;AACf,QAAA,CAAA,CAAA;QAEA,MAAMC,OAAAA,GAAUC,kBAASL,KAAAA,EAAOJ,WAAAA,CAAAA;QAEhC,MAAMU,MAAAA,GAAS,IAAIC,oBAAAA,CAAS;YAC1BC,UAAAA,EAAY,IAAA;AACZC,YAAAA,KAAAA,CAAAA,CAAMC,KAAK,EAAEC,QAAQ,EAAEC,QAAQ,EAAA;AAC7B,gBAAA,IAAIX,UAAAA,EAAY;oBACdW,QAAAA,CAASX,UAAAA,CAAAA;AACT,oBAAA;AACF,gBAAA;AAEAD,gBAAAA,KAAAA,CAAMS,KAAK,CAACC,KAAAA,EAAOC,QAAAA,EAAU,CAACR,KAAAA,GAAAA;oBAC5BS,QAAAA,CAAST,KAAAA,GAASF,cAAcE,KAAAA,GAAS,IAAA,CAAA;AAC3C,gBAAA,CAAA,CAAA;AACF,YAAA,CAAA;AACAU,YAAAA,KAAAA,CAAAA,CAAMD,QAAQ,EAAA;AACZZ,gBAAAA,KAAAA,CAAMc,GAAG,EAAA;gBACTV,OAAAA,CAAQW,IAAI,CAAC,IAAMH,QAAAA,EAAAA,EAAYA,QAAAA,CAAAA;AACjC,YAAA,CAAA;YACAI,OAAAA,CAAAA,CAAQb,KAAK,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