documonster 0.20.2 → 0.21.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 (144) hide show
  1. package/README.md +1 -1
  2. package/README_zh.md +1 -1
  3. package/THIRD_PARTY_NOTICES.md +34 -2
  4. package/dist/esm/modules/archive/core/byte-queue.js +2 -1
  5. package/dist/esm/modules/archive/unzip/stream.browser.js +6 -0
  6. package/dist/esm/modules/csv/parse/async.js +2 -1
  7. package/dist/esm/modules/excel/bridge/csv-bridge.js +1 -1
  8. package/dist/esm/modules/excel/core/xlsx-io.browser.js +5 -9
  9. package/dist/esm/modules/excel/core/xlsx-io.js +1 -3
  10. package/dist/esm/modules/excel/core/xlsx-stream.js +3 -1
  11. package/dist/esm/modules/excel/stream/workbook-reader.browser.js +10 -10
  12. package/dist/esm/modules/excel/stream/workbook-reader.js +30 -6
  13. package/dist/esm/modules/stream/browser/collector.js +3 -5
  14. package/dist/esm/modules/stream/browser/compose.js +13 -228
  15. package/dist/esm/modules/stream/browser/duplex-pair.js +71 -0
  16. package/dist/esm/modules/stream/browser/factories.js +35 -217
  17. package/dist/esm/modules/stream/browser/pipeline.js +43 -478
  18. package/dist/esm/modules/stream/browser/utils.js +21 -247
  19. package/dist/esm/modules/stream/browser/web.js +25 -0
  20. package/dist/esm/modules/stream/buffered-stream.js +8 -6
  21. package/dist/esm/modules/stream/core/factories.js +166 -0
  22. package/dist/esm/modules/stream/core/pipeline-api.js +32 -0
  23. package/dist/esm/modules/stream/core/sink-adapter.js +94 -24
  24. package/dist/esm/modules/stream/core/stream-helpers.js +160 -0
  25. package/dist/esm/modules/stream/core/utils.js +0 -23
  26. package/dist/esm/modules/stream/core/web-conversions.js +31 -0
  27. package/dist/esm/modules/stream/core/web-pipeline.js +90 -0
  28. package/dist/esm/modules/stream/index.base.js +2 -2
  29. package/dist/esm/modules/stream/index.browser.js +13 -19
  30. package/dist/esm/modules/stream/index.js +6 -10
  31. package/dist/esm/modules/stream/kernel/classes.js +618 -0
  32. package/dist/esm/modules/stream/kernel/compose-impl.js +225 -0
  33. package/dist/esm/modules/stream/kernel/destroy.js +255 -0
  34. package/dist/esm/modules/stream/kernel/duplexify.js +355 -0
  35. package/dist/esm/modules/stream/kernel/end-of-stream.js +212 -0
  36. package/dist/esm/modules/stream/kernel/errors.js +215 -0
  37. package/dist/esm/modules/stream/kernel/from.js +202 -0
  38. package/dist/esm/modules/stream/kernel/legacy.js +35 -0
  39. package/dist/esm/modules/stream/kernel/operators.js +285 -0
  40. package/dist/esm/modules/stream/kernel/pipeline-impl.js +561 -0
  41. package/dist/esm/modules/stream/kernel/readable-impl.js +963 -0
  42. package/dist/esm/modules/stream/kernel/state.js +253 -0
  43. package/dist/esm/modules/stream/kernel/stream-utils.js +212 -0
  44. package/dist/esm/modules/stream/kernel/web-adapters.js +584 -0
  45. package/dist/esm/modules/stream/kernel/writable.js +651 -0
  46. package/dist/esm/modules/stream/node/collector.js +7 -1
  47. package/dist/esm/modules/stream/node/compose.js +21 -206
  48. package/dist/esm/modules/stream/node/factories.js +34 -238
  49. package/dist/esm/modules/stream/node/pipeline.js +40 -94
  50. package/dist/esm/modules/stream/node/utils.js +15 -239
  51. package/dist/esm/modules/stream/node/web.js +27 -0
  52. package/dist/esm/modules/stream/pull-stream.js +15 -9
  53. package/dist/esm/modules/word/writer/streaming-writer.js +3 -0
  54. package/dist/iife/THIRD_PARTY_NOTICES.md +34 -2
  55. package/dist/iife/documonster.archive.iife.min.js +4 -4
  56. package/dist/iife/documonster.csv.iife.min.js +19 -19
  57. package/dist/iife/documonster.draw.iife.min.js +1 -1
  58. package/dist/iife/documonster.excel.iife.min.js +50 -50
  59. package/dist/iife/documonster.formula.iife.min.js +1 -1
  60. package/dist/iife/documonster.markdown.iife.min.js +1 -1
  61. package/dist/iife/documonster.mermaid.iife.min.js +1 -1
  62. package/dist/iife/documonster.pdf.iife.min.js +1 -1
  63. package/dist/iife/documonster.stream.iife.min.js +2 -2
  64. package/dist/iife/documonster.word.iife.min.js +60 -60
  65. package/dist/iife/documonster.xml.iife.min.js +1 -1
  66. package/dist/types/modules/excel/core/xlsx-io.browser.d.ts +5 -9
  67. package/dist/types/modules/excel/core/xlsx-io.d.ts +1 -3
  68. package/dist/types/modules/excel/core/xlsx-stream.d.ts +3 -2
  69. package/dist/types/modules/excel/stream/workbook-reader.browser.d.ts +2 -1
  70. package/dist/types/modules/excel/stream/workbook-reader.d.ts +2 -1
  71. package/dist/types/modules/stream/browser/collector.d.ts +1 -1
  72. package/dist/types/modules/stream/browser/compose.d.ts +14 -15
  73. package/dist/types/modules/stream/browser/duplex-pair.d.ts +10 -0
  74. package/dist/types/modules/stream/browser/factories.d.ts +22 -79
  75. package/dist/types/modules/stream/browser/pipeline.d.ts +8 -40
  76. package/dist/types/modules/stream/browser/utils.d.ts +6 -47
  77. package/dist/types/modules/stream/browser/web.d.ts +18 -0
  78. package/dist/types/modules/stream/core/add-abort-signal.d.ts +2 -1
  79. package/dist/types/modules/stream/core/consumers.d.ts +1 -1
  80. package/dist/types/modules/stream/core/event-utils.d.ts +2 -1
  81. package/dist/types/modules/stream/core/factories.d.ts +129 -0
  82. package/dist/types/modules/stream/core/pipeline-api.d.ts +10 -0
  83. package/dist/types/modules/stream/core/sink-adapter.d.ts +18 -1
  84. package/dist/types/modules/stream/core/stream-helpers.d.ts +76 -0
  85. package/dist/types/modules/stream/core/utils.d.ts +0 -9
  86. package/dist/types/modules/stream/core/web-conversions.d.ts +68 -0
  87. package/dist/types/modules/stream/core/web-pipeline.d.ts +27 -0
  88. package/dist/types/modules/stream/index.base.d.ts +6 -4
  89. package/dist/types/modules/stream/index.browser.d.ts +13 -14
  90. package/dist/types/modules/stream/index.d.ts +7 -10
  91. package/dist/types/modules/stream/kernel/classes.d.ts +220 -0
  92. package/dist/types/modules/stream/kernel/compose-impl.d.ts +10 -0
  93. package/dist/types/modules/stream/kernel/destroy.d.ts +21 -0
  94. package/dist/types/modules/stream/kernel/duplexify.d.ts +31 -0
  95. package/dist/types/modules/stream/kernel/end-of-stream.d.ts +28 -0
  96. package/dist/types/modules/stream/kernel/errors.d.ts +42 -0
  97. package/dist/types/modules/stream/kernel/from.d.ts +17 -0
  98. package/dist/types/modules/stream/kernel/legacy.d.ts +20 -0
  99. package/dist/types/modules/stream/kernel/operators.d.ts +44 -0
  100. package/dist/types/modules/stream/kernel/pipeline-impl.d.ts +32 -0
  101. package/dist/types/modules/stream/kernel/readable-impl.d.ts +34 -0
  102. package/dist/types/modules/stream/kernel/state.d.ts +223 -0
  103. package/dist/types/modules/stream/kernel/stream-utils.d.ts +34 -0
  104. package/dist/types/modules/stream/kernel/web-adapters.d.ts +65 -0
  105. package/dist/types/modules/stream/kernel/writable.d.ts +107 -0
  106. package/dist/types/modules/stream/node/collector.d.ts +7 -2
  107. package/dist/types/modules/stream/node/compose.d.ts +31 -5
  108. package/dist/types/modules/stream/node/factories.d.ts +22 -85
  109. package/dist/types/modules/stream/node/pipeline.d.ts +4 -15
  110. package/dist/types/modules/stream/node/utils.d.ts +9 -49
  111. package/dist/types/modules/stream/node/web.d.ts +18 -0
  112. package/dist/types/modules/stream/pull-stream.d.ts +2 -2
  113. package/dist/types/modules/stream/types.d.ts +24 -6
  114. package/package.json +5 -3
  115. package/dist/esm/modules/stream/browser/_lazy.js +0 -30
  116. package/dist/esm/modules/stream/browser/chunk-buffer.js +0 -223
  117. package/dist/esm/modules/stream/browser/duplex.js +0 -979
  118. package/dist/esm/modules/stream/browser/helpers.js +0 -44
  119. package/dist/esm/modules/stream/browser/microtask-context.js +0 -60
  120. package/dist/esm/modules/stream/browser/passthrough.js +0 -16
  121. package/dist/esm/modules/stream/browser/pipe-manager.js +0 -161
  122. package/dist/esm/modules/stream/browser/readable.js +0 -2208
  123. package/dist/esm/modules/stream/browser/transform.js +0 -1241
  124. package/dist/esm/modules/stream/browser/writable.js +0 -1316
  125. package/dist/esm/modules/stream/core/end-args.js +0 -24
  126. package/dist/esm/modules/stream/node/writable.js +0 -125
  127. package/dist/esm/modules/stream/utils.base.js +0 -126
  128. package/dist/esm/modules/stream/utils.browser.js +0 -80
  129. package/dist/esm/modules/stream/utils.js +0 -81
  130. package/dist/types/modules/stream/browser/_lazy.d.ts +0 -20
  131. package/dist/types/modules/stream/browser/chunk-buffer.d.ts +0 -61
  132. package/dist/types/modules/stream/browser/duplex.d.ts +0 -299
  133. package/dist/types/modules/stream/browser/helpers.d.ts +0 -13
  134. package/dist/types/modules/stream/browser/microtask-context.d.ts +0 -44
  135. package/dist/types/modules/stream/browser/passthrough.d.ts +0 -11
  136. package/dist/types/modules/stream/browser/pipe-manager.d.ts +0 -38
  137. package/dist/types/modules/stream/browser/readable.d.ts +0 -379
  138. package/dist/types/modules/stream/browser/transform.d.ts +0 -314
  139. package/dist/types/modules/stream/browser/writable.d.ts +0 -238
  140. package/dist/types/modules/stream/core/end-args.d.ts +0 -20
  141. package/dist/types/modules/stream/node/writable.d.ts +0 -53
  142. package/dist/types/modules/stream/utils.base.d.ts +0 -63
  143. package/dist/types/modules/stream/utils.browser.d.ts +0 -23
  144. package/dist/types/modules/stream/utils.d.ts +0 -24
@@ -0,0 +1,215 @@
1
+ /**
2
+ * Stream kernel - Node.js-compatible error codes
3
+ *
4
+ * Errors carry the same `code` values as Node.js so that callers can branch on
5
+ * `err.code` identically in both environments.
6
+ */
7
+ import { createAbortError } from "../../../utils/errors.js";
8
+ export { createAbortError };
9
+ function coded(code, message, Ctor = Error) {
10
+ const err = new Ctor(message);
11
+ err.code = code;
12
+ return err;
13
+ }
14
+ /** `util.inspect` for the primitives Node's messages print: strings quoted, the rest as is. */
15
+ function inspectPrimitive(value) {
16
+ if (typeof value !== "string") {
17
+ return typeof value === "bigint" ? `${value}n` : String(value);
18
+ }
19
+ const quote = value.includes("'") && !value.includes('"') ? '"' : "'";
20
+ return `${quote}${value}${quote}`;
21
+ }
22
+ /** Node's `addNumericalSeparator`: `9007199254740992` → `9_007_199_254_740_992`. */
23
+ function addNumericalSeparator(value) {
24
+ let result = "";
25
+ let i = value.length;
26
+ const start = value[0] === "-" ? 1 : 0;
27
+ for (; i >= start + 4; i -= 3) {
28
+ result = `_${value.slice(i - 3, i)}${result}`;
29
+ }
30
+ return `${value.slice(0, i)}${result}`;
31
+ }
32
+ /** Node's `determineSpecificType`: how a message names the value it received. */
33
+ function determineSpecificType(value) {
34
+ if (value === null || value === undefined) {
35
+ return String(value);
36
+ }
37
+ switch (typeof value) {
38
+ case "bigint":
39
+ return `type bigint (${value}n)`;
40
+ case "number":
41
+ return `type number (${Object.is(value, -0) ? "-0" : String(value)})`;
42
+ case "boolean":
43
+ case "symbol":
44
+ return `type ${typeof value} (${String(value)})`;
45
+ case "function":
46
+ return `function ${value.name}`;
47
+ case "object": {
48
+ const ctor = value.constructor;
49
+ return ctor && "name" in ctor ? `an instance of ${ctor.name}` : "[Object: null prototype]";
50
+ }
51
+ default: {
52
+ const text = String(value);
53
+ const shown = text.length > 28 ? `${text.slice(0, 25)}...` : text;
54
+ return shown.includes("'")
55
+ ? `type string (${JSON.stringify(shown)})`
56
+ : `type string ('${shown}')`;
57
+ }
58
+ }
59
+ }
60
+ /** Primitive names Node writes as `of type …`; anything else capitalised is a class. */
61
+ const TYPE_NAMES = [
62
+ "string",
63
+ "function",
64
+ "number",
65
+ "object",
66
+ "Function",
67
+ "Object",
68
+ "boolean",
69
+ "bigint",
70
+ "symbol"
71
+ ];
72
+ const CLASS_NAME = /^([A-Z][a-z0-9]*)+$/;
73
+ function listOf(items, lead) {
74
+ if (items.length > 2) {
75
+ const last = items.pop();
76
+ return `${lead}${items.join(", ")}, or ${last}`;
77
+ }
78
+ return `${lead}${items.join(" or ")}`;
79
+ }
80
+ /**
81
+ * Node's `ERR_INVALID_ARG_TYPE`. `expected` is what Node passes — type names,
82
+ * class names or free text, as in `lib/internal/errors.js` — and the sentence
83
+ * is built the same way, so the message is Node's word for word.
84
+ */
85
+ export function errInvalidArgType(name, expected, actual) {
86
+ let message = name.endsWith(" argument")
87
+ ? `The ${name} must be `
88
+ : `The "${name}" ${name.includes(".") ? "property" : "argument"} must be `;
89
+ const types = [];
90
+ const instances = [];
91
+ const other = [];
92
+ for (const value of typeof expected === "string" ? [expected] : expected) {
93
+ if (TYPE_NAMES.includes(value)) {
94
+ types.push(value.toLowerCase());
95
+ }
96
+ else if (CLASS_NAME.test(value)) {
97
+ instances.push(value);
98
+ }
99
+ else {
100
+ other.push(value === "object" ? "Object" : value);
101
+ }
102
+ }
103
+ // "object" among class names reads as "an instance of …, or Object".
104
+ if (instances.length > 0 && types.includes("object")) {
105
+ types.splice(types.indexOf("object"), 1);
106
+ instances.push("Object");
107
+ }
108
+ if (types.length > 0) {
109
+ message += types.length === 1 ? `of type ${types[0]}` : listOf(types, "one of type ");
110
+ if (instances.length > 0 || other.length > 0) {
111
+ message += " or ";
112
+ }
113
+ }
114
+ if (instances.length > 0) {
115
+ message += listOf(instances, "an instance of ");
116
+ if (other.length > 0) {
117
+ message += " or ";
118
+ }
119
+ }
120
+ if (other.length > 0) {
121
+ if (other.length > 1) {
122
+ message += listOf(other, "one of ");
123
+ }
124
+ else {
125
+ message += `${other[0].toLowerCase() !== other[0] ? "an " : ""}${other[0]}`;
126
+ }
127
+ }
128
+ return coded("ERR_INVALID_ARG_TYPE", `${message}. Received ${determineSpecificType(actual)}`, TypeError);
129
+ }
130
+ export function errInvalidArgValue(name, value, reason = "is invalid") {
131
+ return coded("ERR_INVALID_ARG_VALUE", `The argument '${name}' ${reason}. Received ${String(value)}`, TypeError);
132
+ }
133
+ export function errInvalidReturnValue(expected, name, value) {
134
+ return coded("ERR_INVALID_RETURN_VALUE", `Expected ${expected} to be returned from the "${name}" function but got ${typeof value}.`, TypeError);
135
+ }
136
+ export function errMethodNotImplemented(method) {
137
+ return coded("ERR_METHOD_NOT_IMPLEMENTED", `The ${method} method is not implemented`);
138
+ }
139
+ export function errMissingArgs(name) {
140
+ return coded("ERR_MISSING_ARGS", `The "${name}" argument must be specified`, TypeError);
141
+ }
142
+ export function errMultipleCallback() {
143
+ return coded("ERR_MULTIPLE_CALLBACK", "Callback called multiple times");
144
+ }
145
+ export function errOutOfRange(name, range, value) {
146
+ const received = Number.isInteger(value) && Math.abs(value) > 2 ** 32
147
+ ? addNumericalSeparator(String(value))
148
+ : inspectPrimitive(value);
149
+ return coded("ERR_OUT_OF_RANGE", `The value of "${name}" is out of range. It must be ${range}. Received ${received}`, RangeError);
150
+ }
151
+ /** Node's `validateObject` (`lib/internal/validators.js`) without its options: a plain, non-null, non-array object. */
152
+ export function validateObject(value, name) {
153
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
154
+ throw errInvalidArgType(name, "Object", value);
155
+ }
156
+ }
157
+ /** Node's `validateInteger` (`lib/internal/validators.js`). */
158
+ export function validateInteger(value, name, min = Number.MIN_SAFE_INTEGER, max = Number.MAX_SAFE_INTEGER) {
159
+ if (typeof value !== "number") {
160
+ throw errInvalidArgType(name, "number", value);
161
+ }
162
+ if (!Number.isInteger(value)) {
163
+ throw errOutOfRange(name, "an integer", value);
164
+ }
165
+ if (value < min || value > max) {
166
+ throw errOutOfRange(name, `>= ${min} && <= ${max}`, value);
167
+ }
168
+ }
169
+ export function errStreamAlreadyFinished(method) {
170
+ return coded("ERR_STREAM_ALREADY_FINISHED", `Cannot call ${method} after a stream was finished`);
171
+ }
172
+ export function errStreamCannotPipe() {
173
+ return coded("ERR_STREAM_CANNOT_PIPE", "Cannot pipe, not readable");
174
+ }
175
+ export function errStreamDestroyed(method) {
176
+ return coded("ERR_STREAM_DESTROYED", `Cannot call ${method} after a stream was destroyed`);
177
+ }
178
+ export function errStreamNullValues() {
179
+ return coded("ERR_STREAM_NULL_VALUES", "May not write null values to stream", TypeError);
180
+ }
181
+ export function errStreamPrematureClose() {
182
+ return coded("ERR_STREAM_PREMATURE_CLOSE", "Premature close");
183
+ }
184
+ export function errStreamPushAfterEof() {
185
+ return coded("ERR_STREAM_PUSH_AFTER_EOF", "stream.push() after EOF");
186
+ }
187
+ export function errStreamUnableToPipe() {
188
+ return coded("ERR_STREAM_UNABLE_TO_PIPE", "Cannot pipe to a closed or destroyed stream");
189
+ }
190
+ export function errStreamUnshiftAfterEndEvent() {
191
+ return coded("ERR_STREAM_UNSHIFT_AFTER_END_EVENT", "stream.unshift() after end event");
192
+ }
193
+ export function errStreamWriteAfterEnd() {
194
+ return coded("ERR_STREAM_WRITE_AFTER_END", "write after end");
195
+ }
196
+ export function errUnknownEncoding(encoding) {
197
+ return coded("ERR_UNKNOWN_ENCODING", `Unknown encoding: ${String(encoding)}`, TypeError);
198
+ }
199
+ /**
200
+ * Combine two errors the way Node.js does when a second failure happens while
201
+ * handling the first: the outer error wins, the inner one is kept alongside.
202
+ */
203
+ export function aggregateTwoErrors(inner, outer) {
204
+ if (inner && outer && inner !== outer) {
205
+ const errors = outer.errors;
206
+ if (Array.isArray(errors)) {
207
+ errors.push(inner);
208
+ return outer;
209
+ }
210
+ const err = new AggregateError([outer, inner], outer.message);
211
+ err.code = outer.code;
212
+ return err;
213
+ }
214
+ return inner || outer;
215
+ }
@@ -0,0 +1,202 @@
1
+ /**
2
+ * Stream kernel - Readable.from()
3
+ *
4
+ * Port of Node.js `lib/internal/streams/from.js`. The source iterator is
5
+ * closed from `_destroy`, so leaving a stream early — `break` in a
6
+ * `for await`, `destroy()`, an error downstream — always runs the source's
7
+ * `finally` blocks and releases whatever it holds, whether or not another
8
+ * `_read()` ever happens.
9
+ */
10
+ import { aggregateTwoErrors, errInvalidArgType, errStreamNullValues } from "./errors.js";
11
+ import { nextTick } from "./state.js";
12
+ export function from(ReadableCtor, iterable, opts) {
13
+ if (typeof iterable === "string") {
14
+ return new ReadableCtor({
15
+ objectMode: true,
16
+ ...opts,
17
+ read() {
18
+ this.push(iterable);
19
+ this.push(null);
20
+ }
21
+ });
22
+ }
23
+ let isAsync;
24
+ let iterator;
25
+ const source = iterable;
26
+ const asyncFactory = source?.[Symbol.asyncIterator];
27
+ const syncFactory = source?.[Symbol.iterator];
28
+ if (asyncFactory) {
29
+ isAsync = true;
30
+ iterator = asyncFactory.call(source);
31
+ }
32
+ else if (typeof iterable?.getReader === "function") {
33
+ // A ReadableStream where the platform has not made it async-iterable yet.
34
+ isAsync = true;
35
+ iterator = readerValues(iterable);
36
+ }
37
+ else if (syncFactory) {
38
+ isAsync = false;
39
+ iterator = syncFactory.call(source);
40
+ }
41
+ else {
42
+ throw errInvalidArgType("iterable", ["Iterable"], iterable);
43
+ }
44
+ const readable = new ReadableCtor({ objectMode: true, highWaterMark: 1, ...opts });
45
+ const originalDestroy = readable._destroy;
46
+ let reading = false;
47
+ let isAsyncValues = false;
48
+ readable._read = function () {
49
+ if (!reading) {
50
+ reading = true;
51
+ if (isAsync) {
52
+ nextAsync();
53
+ }
54
+ else if (isAsyncValues) {
55
+ nextSyncWithAsyncValues();
56
+ }
57
+ else {
58
+ nextSyncWithSyncValues();
59
+ }
60
+ }
61
+ };
62
+ readable._destroy = function (error, cb) {
63
+ originalDestroy.call(this, error, destroyError => {
64
+ const combinedError = destroyError || error;
65
+ close(combinedError).then(() => nextTick(() => cb(combinedError)), closeError => nextTick(() => cb(aggregateTwoErrors(combinedError, closeError))));
66
+ });
67
+ };
68
+ async function close(error) {
69
+ const hadError = error !== undefined && error !== null;
70
+ if (hadError && typeof iterator.throw === "function") {
71
+ const { value, done } = await iterator.throw(error);
72
+ await value;
73
+ if (done) {
74
+ return;
75
+ }
76
+ }
77
+ if (typeof iterator.return === "function") {
78
+ const { value } = await iterator.return();
79
+ await value;
80
+ }
81
+ }
82
+ function nextSyncWithSyncValues() {
83
+ for (;;) {
84
+ try {
85
+ const { value, done } = iterator.next();
86
+ if (done) {
87
+ readable.push(null);
88
+ return;
89
+ }
90
+ if (value && typeof value.then === "function") {
91
+ changeToAsyncValues(value);
92
+ return;
93
+ }
94
+ if (value === null) {
95
+ reading = false;
96
+ throw errStreamNullValues();
97
+ }
98
+ if (readable.push(value)) {
99
+ continue;
100
+ }
101
+ reading = false;
102
+ }
103
+ catch (err) {
104
+ readable.destroy(err);
105
+ }
106
+ break;
107
+ }
108
+ }
109
+ async function changeToAsyncValues(value) {
110
+ isAsyncValues = true;
111
+ try {
112
+ const res = await value;
113
+ if (res === null) {
114
+ reading = false;
115
+ throw errStreamNullValues();
116
+ }
117
+ if (readable.push(res)) {
118
+ nextSyncWithAsyncValues();
119
+ return;
120
+ }
121
+ reading = false;
122
+ }
123
+ catch (err) {
124
+ readable.destroy(err);
125
+ }
126
+ }
127
+ async function nextSyncWithAsyncValues() {
128
+ for (;;) {
129
+ try {
130
+ const { value, done } = iterator.next();
131
+ if (done) {
132
+ readable.push(null);
133
+ return;
134
+ }
135
+ const res = value && typeof value.then === "function"
136
+ ? await value
137
+ : value;
138
+ if (res === null) {
139
+ reading = false;
140
+ throw errStreamNullValues();
141
+ }
142
+ if (readable.push(res)) {
143
+ continue;
144
+ }
145
+ reading = false;
146
+ }
147
+ catch (err) {
148
+ readable.destroy(err);
149
+ }
150
+ break;
151
+ }
152
+ }
153
+ async function nextAsync() {
154
+ for (;;) {
155
+ try {
156
+ const { value, done } = await iterator.next();
157
+ if (done) {
158
+ readable.push(null);
159
+ return;
160
+ }
161
+ if (value === null) {
162
+ reading = false;
163
+ throw errStreamNullValues();
164
+ }
165
+ if (readable.push(value)) {
166
+ continue;
167
+ }
168
+ reading = false;
169
+ }
170
+ catch (err) {
171
+ readable.destroy(err);
172
+ }
173
+ break;
174
+ }
175
+ }
176
+ return readable;
177
+ }
178
+ /**
179
+ * What `ReadableStream.prototype.values()` does: every value as read, `null`
180
+ * included (`from()` rejects it as Node does); leaving early cancels the
181
+ * stream, and the lock is released either way.
182
+ */
183
+ async function* readerValues(stream) {
184
+ const reader = stream.getReader();
185
+ let done = false;
186
+ try {
187
+ while (true) {
188
+ const result = await reader.read();
189
+ if (result.done) {
190
+ done = true;
191
+ return;
192
+ }
193
+ yield result.value;
194
+ }
195
+ }
196
+ finally {
197
+ if (!done) {
198
+ await reader.cancel().catch(() => { });
199
+ }
200
+ reader.releaseLock();
201
+ }
202
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Stream kernel - common base class
3
+ *
4
+ * The browser counterpart of Node's `Stream`: an EventEmitter with the static
5
+ * state inspectors every stream class inherits (`Readable.isDisturbed`,
6
+ * `Writable.isErrored`, …).
7
+ */
8
+ import { isDestroyed, isDisturbed, isErrored, isReadable, isWritable } from "./stream-utils.js";
9
+ import { EventEmitter } from "../../../utils/event-emitter.js";
10
+ export class Stream extends EventEmitter {
11
+ static isDestroyed(stream) {
12
+ return isDestroyed(stream);
13
+ }
14
+ static isDisturbed(stream) {
15
+ return isDisturbed(stream);
16
+ }
17
+ static isErrored(stream) {
18
+ return isErrored(stream);
19
+ }
20
+ static isReadable(stream) {
21
+ return isReadable(stream);
22
+ }
23
+ static isWritable(stream) {
24
+ return isWritable(stream);
25
+ }
26
+ }
27
+ /** Attach `fn` ahead of any user listener so stream bookkeeping runs first. */
28
+ export function prependListener(emitter, event, fn) {
29
+ if (typeof emitter.prependListener === "function") {
30
+ emitter.prependListener(event, fn);
31
+ }
32
+ else {
33
+ emitter.on(event, fn);
34
+ }
35
+ }