@tstdl/base 0.83.27 → 0.83.28
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/application/application.js +1 -1
- package/browser/browser-controller.d.ts +26 -0
- package/browser/browser-controller.js +99 -0
- package/browser/browser.service.d.ts +32 -0
- package/browser/browser.service.js +110 -0
- package/browser/index.d.ts +4 -0
- package/browser/index.js +21 -0
- package/browser/page-controller.d.ts +56 -0
- package/browser/page-controller.js +189 -0
- package/browser/pdf-options.d.ts +32 -0
- package/browser/pdf-options.js +134 -0
- package/browser/types.d.ts +3 -0
- package/browser/types.js +16 -0
- package/core.d.ts +1 -1
- package/examples/browser/basic.d.ts +1 -0
- package/examples/browser/basic.js +19 -0
- package/examples/mail/basic.js +11 -3
- package/examples/pdf/basic.js +29 -7
- package/examples/pdf/templates/hello-name.js +10 -2
- package/package.json +3 -3
- package/pdf/pdf.service.d.ts +10 -39
- package/pdf/pdf.service.js +43 -174
- package/pool/pool.js +3 -0
- package/types.d.ts +4 -1
- package/utils/index.d.ts +1 -0
- package/utils/index.js +1 -0
- package/utils/stream/finalize-stream.d.ts +22 -1
- package/utils/stream/finalize-stream.js +14 -4
- package/utils/stream/readable-stream-from-promise.d.ts +2 -1
- package/utils/stream/readable-stream-from-promise.js +3 -2
- package/utils/type-guards.d.ts +6 -0
- package/utils/type-guards.js +26 -0
- package/utils/value-or-provider.d.ts +2 -0
- package/utils/value-or-provider.js +30 -0
package/pdf/pdf.service.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,25 +15,17 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
19
|
var pdf_service_exports = {};
|
|
30
20
|
__export(pdf_service_exports, {
|
|
31
|
-
PdfFormat: () => PdfFormat,
|
|
32
|
-
PdfMarginObject: () => PdfMarginObject,
|
|
33
|
-
PdfRenderOptions: () => PdfRenderOptions,
|
|
34
21
|
PdfService: () => PdfService,
|
|
22
|
+
PdfServiceRenderOptions: () => PdfServiceRenderOptions,
|
|
35
23
|
PdfTemplate: () => PdfTemplate,
|
|
36
24
|
pdfTemplate: () => pdfTemplate
|
|
37
25
|
});
|
|
38
26
|
module.exports = __toCommonJS(pdf_service_exports);
|
|
27
|
+
var import_browser_service = require("../browser/browser.service.js");
|
|
28
|
+
var import_pdf_options = require("../browser/pdf-options.js");
|
|
39
29
|
var import_container = require("../container/index.js");
|
|
40
30
|
var import_core = require("../core.js");
|
|
41
31
|
var import_disposable = require("../disposable/disposable.js");
|
|
@@ -44,12 +34,10 @@ var import_pool = require("../pool/pool.js");
|
|
|
44
34
|
var import_schema = require("../schema/index.js");
|
|
45
35
|
var import_templates = require("../templates/index.js");
|
|
46
36
|
var import_finalize_stream = require("../utils/stream/finalize-stream.js");
|
|
47
|
-
var import_readable_stream_adapter = require("../utils/stream/readable-stream-adapter.js");
|
|
48
37
|
var import_readable_stream_from_promise = require("../utils/stream/readable-stream-from-promise.js");
|
|
49
38
|
var import_stream_reader = require("../utils/stream/stream-reader.js");
|
|
50
39
|
var import_type_guards = require("../utils/type-guards.js");
|
|
51
40
|
var import_units = require("../utils/units.js");
|
|
52
|
-
var puppeteer = __toESM(require("puppeteer"), 1);
|
|
53
41
|
var __decorate = function(decorators, target, key, desc) {
|
|
54
42
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
55
43
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
@@ -69,56 +57,9 @@ var __param = function(paramIndex, decorator) {
|
|
|
69
57
|
decorator(target, key, paramIndex);
|
|
70
58
|
};
|
|
71
59
|
};
|
|
72
|
-
|
|
73
|
-
(function(PdfFormat2) {
|
|
74
|
-
PdfFormat2["Letter"] = "letter";
|
|
75
|
-
PdfFormat2["Legal"] = "legal";
|
|
76
|
-
PdfFormat2["Tabloid"] = "tabloid";
|
|
77
|
-
PdfFormat2["Ledger"] = "ledger";
|
|
78
|
-
PdfFormat2["A0"] = "a0";
|
|
79
|
-
PdfFormat2["A1"] = "a1";
|
|
80
|
-
PdfFormat2["A2"] = "a2";
|
|
81
|
-
PdfFormat2["A3"] = "a3";
|
|
82
|
-
PdfFormat2["A4"] = "a4";
|
|
83
|
-
PdfFormat2["A5"] = "a5";
|
|
84
|
-
PdfFormat2["A6"] = "a6";
|
|
85
|
-
})(PdfFormat || (PdfFormat = {}));
|
|
86
|
-
class PdfMarginObject {
|
|
87
|
-
top;
|
|
88
|
-
bottom;
|
|
89
|
-
right;
|
|
90
|
-
left;
|
|
91
|
-
}
|
|
92
|
-
__decorate([
|
|
93
|
-
(0, import_schema.Optional)([Number, String]),
|
|
94
|
-
__metadata("design:type", Object)
|
|
95
|
-
], PdfMarginObject.prototype, "top", void 0);
|
|
96
|
-
__decorate([
|
|
97
|
-
(0, import_schema.Optional)([Number, String]),
|
|
98
|
-
__metadata("design:type", Object)
|
|
99
|
-
], PdfMarginObject.prototype, "bottom", void 0);
|
|
100
|
-
__decorate([
|
|
101
|
-
(0, import_schema.Optional)([Number, String]),
|
|
102
|
-
__metadata("design:type", Object)
|
|
103
|
-
], PdfMarginObject.prototype, "right", void 0);
|
|
104
|
-
__decorate([
|
|
105
|
-
(0, import_schema.Optional)([Number, String]),
|
|
106
|
-
__metadata("design:type", Object)
|
|
107
|
-
], PdfMarginObject.prototype, "left", void 0);
|
|
108
|
-
class PdfRenderOptions {
|
|
60
|
+
class PdfServiceRenderOptions extends import_pdf_options.PdfRenderOptions {
|
|
109
61
|
language;
|
|
110
|
-
omitDefaultBackground;
|
|
111
|
-
renderBackground;
|
|
112
|
-
landscape;
|
|
113
|
-
format;
|
|
114
|
-
width;
|
|
115
|
-
height;
|
|
116
|
-
scale;
|
|
117
|
-
margin;
|
|
118
|
-
displayHeaderFooter;
|
|
119
62
|
waitForNetworkIdle;
|
|
120
|
-
headerTemplate;
|
|
121
|
-
footerTemplate;
|
|
122
63
|
/**
|
|
123
64
|
* Timeout for closing render context in case something went wrong.
|
|
124
65
|
* @default 60000 (1 minute)
|
|
@@ -128,80 +69,30 @@ class PdfRenderOptions {
|
|
|
128
69
|
__decorate([
|
|
129
70
|
(0, import_schema.Optional)(),
|
|
130
71
|
__metadata("design:type", String)
|
|
131
|
-
],
|
|
132
|
-
__decorate([
|
|
133
|
-
(0, import_schema.Optional)(),
|
|
134
|
-
__metadata("design:type", Boolean)
|
|
135
|
-
], PdfRenderOptions.prototype, "omitDefaultBackground", void 0);
|
|
136
|
-
__decorate([
|
|
137
|
-
(0, import_schema.Optional)(),
|
|
138
|
-
__metadata("design:type", Boolean)
|
|
139
|
-
], PdfRenderOptions.prototype, "renderBackground", void 0);
|
|
72
|
+
], PdfServiceRenderOptions.prototype, "language", void 0);
|
|
140
73
|
__decorate([
|
|
141
74
|
(0, import_schema.Optional)(),
|
|
142
75
|
__metadata("design:type", Boolean)
|
|
143
|
-
],
|
|
144
|
-
__decorate([
|
|
145
|
-
(0, import_schema.Optional)(),
|
|
146
|
-
(0, import_schema.Enumeration)(PdfFormat),
|
|
147
|
-
__metadata("design:type", String)
|
|
148
|
-
], PdfRenderOptions.prototype, "format", void 0);
|
|
149
|
-
__decorate([
|
|
150
|
-
(0, import_schema.Optional)([String, Number]),
|
|
151
|
-
__metadata("design:type", Object)
|
|
152
|
-
], PdfRenderOptions.prototype, "width", void 0);
|
|
153
|
-
__decorate([
|
|
154
|
-
(0, import_schema.Optional)([String, Number]),
|
|
155
|
-
__metadata("design:type", Object)
|
|
156
|
-
], PdfRenderOptions.prototype, "height", void 0);
|
|
76
|
+
], PdfServiceRenderOptions.prototype, "waitForNetworkIdle", void 0);
|
|
157
77
|
__decorate([
|
|
158
78
|
(0, import_schema.Optional)(),
|
|
159
79
|
__metadata("design:type", Number)
|
|
160
|
-
],
|
|
161
|
-
__decorate([
|
|
162
|
-
(0, import_schema.Optional)([String, Number, PdfMarginObject]),
|
|
163
|
-
__metadata("design:type", Object)
|
|
164
|
-
], PdfRenderOptions.prototype, "margin", void 0);
|
|
165
|
-
__decorate([
|
|
166
|
-
(0, import_schema.Optional)(),
|
|
167
|
-
__metadata("design:type", Boolean)
|
|
168
|
-
], PdfRenderOptions.prototype, "displayHeaderFooter", void 0);
|
|
169
|
-
__decorate([
|
|
170
|
-
(0, import_schema.Optional)(),
|
|
171
|
-
__metadata("design:type", Boolean)
|
|
172
|
-
], PdfRenderOptions.prototype, "waitForNetworkIdle", void 0);
|
|
173
|
-
__decorate([
|
|
174
|
-
(0, import_schema.Optional)(),
|
|
175
|
-
__metadata("design:type", String)
|
|
176
|
-
], PdfRenderOptions.prototype, "headerTemplate", void 0);
|
|
177
|
-
__decorate([
|
|
178
|
-
(0, import_schema.Optional)(),
|
|
179
|
-
__metadata("design:type", String)
|
|
180
|
-
], PdfRenderOptions.prototype, "footerTemplate", void 0);
|
|
181
|
-
__decorate([
|
|
182
|
-
(0, import_schema.Optional)(),
|
|
183
|
-
__metadata("design:type", Number)
|
|
184
|
-
], PdfRenderOptions.prototype, "timeout", void 0);
|
|
80
|
+
], PdfServiceRenderOptions.prototype, "timeout", void 0);
|
|
185
81
|
class PdfTemplate extends import_templates.Template {
|
|
186
82
|
}
|
|
187
83
|
__decorate([
|
|
188
84
|
(0, import_schema.Optional)(),
|
|
189
85
|
__metadata("design:type", Object)
|
|
190
86
|
], PdfTemplate.prototype, "options", void 0);
|
|
87
|
+
const browserArguments = ["--font-render-hinting=none", "--disable-web-security", "--disable-features=IsolateOrigins", "--disable-site-isolation-trials"];
|
|
191
88
|
let PdfService = class PdfService2 {
|
|
192
89
|
templateService;
|
|
193
90
|
logger;
|
|
194
91
|
pool;
|
|
195
|
-
constructor(templateService, logger, options = {}) {
|
|
92
|
+
constructor(templateService, browserService, logger, options = {}) {
|
|
196
93
|
this.templateService = templateService;
|
|
197
94
|
this.logger = logger;
|
|
198
|
-
|
|
199
|
-
const env = {};
|
|
200
|
-
if ((0, import_type_guards.isDefined)(options.language)) {
|
|
201
|
-
args.push(`--lang=${options.language}`);
|
|
202
|
-
env["LANGUAGE"] = options.language;
|
|
203
|
-
}
|
|
204
|
-
this.pool = new import_pool.Pool(async () => puppeteer.launch({ headless: true, args, env }), async (browser) => browser.close(), logger);
|
|
95
|
+
this.pool = new import_pool.Pool(async () => browserService.newBrowser({ headless: true, language: options.language, browserArguments }), async (controller) => controller.close(), logger);
|
|
205
96
|
}
|
|
206
97
|
[import_container.afterResolve]() {
|
|
207
98
|
import_core.disposer.add(this);
|
|
@@ -238,8 +129,8 @@ let PdfService = class PdfService2 {
|
|
|
238
129
|
* @returns pdf stream
|
|
239
130
|
*/
|
|
240
131
|
renderUrlStream(url, options) {
|
|
241
|
-
return this.renderStream(async (
|
|
242
|
-
await
|
|
132
|
+
return this.renderStream(async (controller) => {
|
|
133
|
+
await controller.navigate(url, { waitUntil: options?.waitForNetworkIdle == true ? "networkidle2" : "load" });
|
|
243
134
|
}, options);
|
|
244
135
|
}
|
|
245
136
|
/**
|
|
@@ -262,8 +153,8 @@ let PdfService = class PdfService2 {
|
|
|
262
153
|
renderTemplateStream(keyOrTemplate, templateContext, options) {
|
|
263
154
|
return this.renderStream(async (page) => {
|
|
264
155
|
const { fields: { header, body, footer }, options: optionsFromTemplate } = await this.templateService.render(keyOrTemplate, templateContext);
|
|
265
|
-
await page.setContent(body, {
|
|
266
|
-
return { ...optionsFromTemplate, headerTemplate: header, footerTemplate: footer
|
|
156
|
+
await page.setContent(body, { timeout: options?.timeout, waitUntil: options?.waitForNetworkIdle == true ? "networkidle2" : "load" });
|
|
157
|
+
return { ...optionsFromTemplate, headerTemplate: header, footerTemplate: footer };
|
|
267
158
|
}, options);
|
|
268
159
|
}
|
|
269
160
|
/**
|
|
@@ -279,67 +170,45 @@ let PdfService = class PdfService2 {
|
|
|
279
170
|
}
|
|
280
171
|
renderStream(handler, options = {}) {
|
|
281
172
|
return (0, import_readable_stream_from_promise.readableStreamFromPromise)(async () => {
|
|
282
|
-
const
|
|
173
|
+
const browserController = await this.pool.get();
|
|
174
|
+
let page;
|
|
283
175
|
try {
|
|
284
|
-
|
|
176
|
+
page = await browserController.newPage();
|
|
177
|
+
} catch (error) {
|
|
178
|
+
await this.pool.disposeInstance(browserController);
|
|
179
|
+
throw error;
|
|
180
|
+
}
|
|
181
|
+
if ((0, import_type_guards.isDefined)(options.language)) {
|
|
182
|
+
await page.setExtraHttpHeaders({ "Accept-Language": options.language });
|
|
183
|
+
}
|
|
184
|
+
const optionsFromHandler = await handler(page);
|
|
185
|
+
const pdfStream = page.renderPdfStream({ ...optionsFromHandler, ...options });
|
|
186
|
+
const timeoutRef = setTimeout(() => void pdfStream.cancel(new Error("Pdf render timed out.")), options.timeout ?? import_units.millisecondsPerMinute);
|
|
187
|
+
const close = async () => {
|
|
285
188
|
try {
|
|
286
|
-
|
|
189
|
+
clearTimeout(timeoutRef);
|
|
190
|
+
await page.close();
|
|
287
191
|
} catch (error) {
|
|
288
|
-
await this.pool.disposeInstance(
|
|
289
|
-
|
|
192
|
+
await this.pool.disposeInstance(browserController);
|
|
193
|
+
this.logger.error(error);
|
|
290
194
|
}
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
const mergedOptions = { ...optionsFromHandler, ...options };
|
|
297
|
-
const createPdfOptions = this.convertOptions(mergedOptions);
|
|
298
|
-
const pdfStream = await page.createPDFStream(createPdfOptions);
|
|
299
|
-
return (0, import_finalize_stream.finalizeStream)((0, import_readable_stream_adapter.getReadableStreamFromIterable)(pdfStream), async () => {
|
|
195
|
+
};
|
|
196
|
+
return (0, import_finalize_stream.finalizeStream)(pdfStream, {
|
|
197
|
+
beforeDone: close,
|
|
198
|
+
beforeCancel: close,
|
|
199
|
+
error: async () => {
|
|
300
200
|
clearTimeout(timeoutRef);
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
this.logger.error(error);
|
|
305
|
-
throw error;
|
|
306
|
-
} finally {
|
|
307
|
-
await this.pool.release(browser);
|
|
308
|
-
}
|
|
309
|
-
});
|
|
310
|
-
} catch (error) {
|
|
311
|
-
await this.pool.release(browser);
|
|
312
|
-
throw error;
|
|
313
|
-
}
|
|
201
|
+
await this.pool.disposeInstance(browserController);
|
|
202
|
+
}
|
|
203
|
+
});
|
|
314
204
|
});
|
|
315
205
|
}
|
|
316
|
-
convertOptions(options) {
|
|
317
|
-
const margin = (0, import_type_guards.isUndefined)(options.margin) ? void 0 : (0, import_type_guards.isObject)(options.margin) ? options.margin : {
|
|
318
|
-
top: options.margin,
|
|
319
|
-
bottom: options.margin,
|
|
320
|
-
right: options.margin,
|
|
321
|
-
left: options.margin
|
|
322
|
-
};
|
|
323
|
-
return {
|
|
324
|
-
format: options.format ?? "a4",
|
|
325
|
-
scale: options.scale,
|
|
326
|
-
landscape: options.landscape,
|
|
327
|
-
width: options.width,
|
|
328
|
-
height: options.height,
|
|
329
|
-
omitBackground: options.omitDefaultBackground,
|
|
330
|
-
printBackground: options.renderBackground,
|
|
331
|
-
margin,
|
|
332
|
-
displayHeaderFooter: options.displayHeaderFooter ?? ((0, import_type_guards.isDefined)(options.headerTemplate) || (0, import_type_guards.isDefined)(options.footerTemplate)),
|
|
333
|
-
headerTemplate: options.headerTemplate,
|
|
334
|
-
footerTemplate: options.footerTemplate
|
|
335
|
-
};
|
|
336
|
-
}
|
|
337
206
|
};
|
|
338
207
|
PdfService = __decorate([
|
|
339
208
|
(0, import_container.singleton)(),
|
|
340
|
-
__param(
|
|
341
|
-
__param(
|
|
342
|
-
__metadata("design:paramtypes", [import_templates.TemplateService, import_logger.Logger, Object])
|
|
209
|
+
__param(2, (0, import_container.resolveArg)("PdfService")),
|
|
210
|
+
__param(3, (0, import_container.injectArg)()),
|
|
211
|
+
__metadata("design:paramtypes", [import_templates.TemplateService, import_browser_service.BrowserService, import_logger.Logger, Object])
|
|
343
212
|
], PdfService);
|
|
344
213
|
function pdfTemplate(name, fields, options) {
|
|
345
214
|
return {
|
package/pool/pool.js
CHANGED
|
@@ -79,6 +79,9 @@ class Pool {
|
|
|
79
79
|
return this.get();
|
|
80
80
|
}
|
|
81
81
|
async release(instance) {
|
|
82
|
+
if (this.freeInstances.includes(instance)) {
|
|
83
|
+
throw new Error("Instance is free already.");
|
|
84
|
+
}
|
|
82
85
|
if (!this.usedInstances.has(instance)) {
|
|
83
86
|
throw new Error("Instance is not from this pool.");
|
|
84
87
|
}
|
package/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Except, UnionToIntersection } from 'type-fest';
|
|
1
|
+
import type { Except, OptionalKeysOf, UnionToIntersection } from 'type-fest';
|
|
2
2
|
export type ObjectLiteral = {};
|
|
3
3
|
export type PrimitiveTypeMap = {
|
|
4
4
|
'string': string;
|
|
@@ -112,6 +112,9 @@ export type OmitBy<T, V> = Omit<T, {
|
|
|
112
112
|
* normalize properties of a type that allow `undefined` to make them optional.
|
|
113
113
|
*/
|
|
114
114
|
export type Optionalize<T extends object> = OmitBy<T, undefined> & Partial<PickBy<T, undefined>>;
|
|
115
|
+
export type Unoptionalize<T extends object> = Simplify<OmitBy<T, undefined> & {
|
|
116
|
+
[P in OptionalKeysOf<T>]: T[P] | undefined;
|
|
117
|
+
}>;
|
|
115
118
|
export type SimplifiedOptionalize<T extends object> = Simplify<Optionalize<T>>;
|
|
116
119
|
export type Merge<T1, T2> = SimplifyObject<Except<T1, Extract<keyof T1, keyof T2>> & T2>;
|
|
117
120
|
export type Simplify<T> = T extends BuiltIn ? T : T extends readonly any[] ? SimplifyArray<T> : T extends Record ? SimplifyObject<T> : T;
|
package/utils/index.d.ts
CHANGED
package/utils/index.js
CHANGED
|
@@ -61,4 +61,5 @@ __reExport(utils_exports, require("./type-guards.js"), module.exports);
|
|
|
61
61
|
__reExport(utils_exports, require("./type-of.js"), module.exports);
|
|
62
62
|
__reExport(utils_exports, require("./units.js"), module.exports);
|
|
63
63
|
__reExport(utils_exports, require("./url-builder.js"), module.exports);
|
|
64
|
+
__reExport(utils_exports, require("./value-or-provider.js"), module.exports);
|
|
64
65
|
__reExport(utils_exports, require("./z-base32.js"), module.exports);
|
|
@@ -1 +1,22 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type ReadableStreamFinalizeEvent = {
|
|
2
|
+
type: 'done';
|
|
3
|
+
} | {
|
|
4
|
+
type: 'error';
|
|
5
|
+
error: any;
|
|
6
|
+
} | {
|
|
7
|
+
type: 'cancel';
|
|
8
|
+
reason: any;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Finalizer is called *after* the specific event occurred.
|
|
12
|
+
*/
|
|
13
|
+
export type StreamFinalizerHandler = (event: ReadableStreamFinalizeEvent) => void | Promise<void>;
|
|
14
|
+
export type FinalizeStreamHandlers = {
|
|
15
|
+
finalizer?: StreamFinalizerHandler;
|
|
16
|
+
beforeDone?: () => void | Promise<void>;
|
|
17
|
+
done?: () => void | Promise<void>;
|
|
18
|
+
error?: (error: Error) => void | Promise<void>;
|
|
19
|
+
beforeCancel?: (reason: any) => void | Promise<void>;
|
|
20
|
+
cancel?: (reason: any) => void | Promise<void>;
|
|
21
|
+
};
|
|
22
|
+
export declare function finalizeStream<T>(stream: ReadableStream<T>, finalizerOrHandlers: StreamFinalizerHandler | FinalizeStreamHandlers): ReadableStream<T>;
|
|
@@ -21,7 +21,9 @@ __export(finalize_stream_exports, {
|
|
|
21
21
|
finalizeStream: () => finalizeStream
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(finalize_stream_exports);
|
|
24
|
-
|
|
24
|
+
var import_type_guards = require("../type-guards.js");
|
|
25
|
+
function finalizeStream(stream, finalizerOrHandlers) {
|
|
26
|
+
const handlers = (0, import_type_guards.isFunction)(finalizerOrHandlers) ? { finalizer: finalizerOrHandlers } : finalizerOrHandlers;
|
|
25
27
|
let reader;
|
|
26
28
|
return new ReadableStream({
|
|
27
29
|
start() {
|
|
@@ -32,21 +34,29 @@ function finalizeStream(stream, finalizer) {
|
|
|
32
34
|
try {
|
|
33
35
|
chunk = await reader.read();
|
|
34
36
|
} catch (error) {
|
|
35
|
-
await
|
|
37
|
+
await handlers.error?.(error);
|
|
38
|
+
await handlers.finalizer?.({ type: "error", error });
|
|
36
39
|
throw error;
|
|
37
40
|
}
|
|
38
41
|
if (chunk.done) {
|
|
42
|
+
await handlers.beforeDone?.();
|
|
39
43
|
controller.close();
|
|
40
|
-
await
|
|
44
|
+
await handlers.done?.();
|
|
45
|
+
await handlers.finalizer?.({ type: "done" });
|
|
41
46
|
} else {
|
|
42
47
|
controller.enqueue(chunk.value);
|
|
43
48
|
}
|
|
44
49
|
},
|
|
45
50
|
async cancel(reason) {
|
|
46
51
|
try {
|
|
52
|
+
await handlers.beforeCancel?.(reason);
|
|
47
53
|
await reader.cancel(reason);
|
|
54
|
+
await handlers.cancel?.(reason);
|
|
55
|
+
} catch (error) {
|
|
56
|
+
await handlers.error?.(error);
|
|
57
|
+
throw error;
|
|
48
58
|
} finally {
|
|
49
|
-
await finalizer();
|
|
59
|
+
await handlers.finalizer?.({ type: "cancel", reason });
|
|
50
60
|
}
|
|
51
61
|
}
|
|
52
62
|
});
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import type { ValueOrProvider } from '../value-or-provider.js';
|
|
2
|
+
export declare function readableStreamFromPromise<T>(promiseOrProvider: ValueOrProvider<Promise<ReadableStream<T>>>): ReadableStream<T>;
|
|
@@ -21,8 +21,9 @@ __export(readable_stream_from_promise_exports, {
|
|
|
21
21
|
readableStreamFromPromise: () => readableStreamFromPromise
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(readable_stream_from_promise_exports);
|
|
24
|
-
|
|
24
|
+
var import_value_or_provider = require("../value-or-provider.js");
|
|
25
|
+
function readableStreamFromPromise(promiseOrProvider) {
|
|
25
26
|
const stream = new TransformStream();
|
|
26
|
-
|
|
27
|
+
(0, import_value_or_provider.resolveValueOrProvider)(promiseOrProvider).then(async (readable) => readable.pipeTo(stream.writable)).catch(async (error) => stream.writable.abort(error));
|
|
27
28
|
return stream.readable;
|
|
28
29
|
}
|
package/utils/type-guards.d.ts
CHANGED
|
@@ -238,3 +238,9 @@ export declare function assertReadableStream<T = any>(value: any, message?: Asse
|
|
|
238
238
|
export declare function assertNotReadableStream<T>(value: T, message?: AssertionMessage): asserts value is InferIsNotType<T, typeof isReadableStream>;
|
|
239
239
|
export declare function assertReadableStreamPass<T = any>(value: any, message?: AssertionMessage): InferIsType<typeof isReadableStream>;
|
|
240
240
|
export declare function assertNotReadableStreamPass<T>(value: T, message?: AssertionMessage): InferIsNotType<T, typeof isReadableStream>;
|
|
241
|
+
export declare function isError(value: any): value is Error;
|
|
242
|
+
export declare function isNotError<T>(value: T): value is InferIsNotType<T, typeof isError>;
|
|
243
|
+
export declare function assertError(value: any, message?: AssertionMessage): asserts value is InferIsType<typeof isError>;
|
|
244
|
+
export declare function assertNotError<T>(value: T, message?: AssertionMessage): asserts value is InferIsNotType<T, typeof isError>;
|
|
245
|
+
export declare function assertErrorPass(value: any, message?: AssertionMessage): InferIsType<typeof isError>;
|
|
246
|
+
export declare function assertNotErrorPass<T>(value: T, message?: AssertionMessage): InferIsNotType<T, typeof isError>;
|
package/utils/type-guards.js
CHANGED
|
@@ -43,6 +43,8 @@ __export(type_guards_exports, {
|
|
|
43
43
|
assertDatePass: () => assertDatePass,
|
|
44
44
|
assertDefined: () => assertDefined,
|
|
45
45
|
assertDefinedPass: () => assertDefinedPass,
|
|
46
|
+
assertError: () => assertError,
|
|
47
|
+
assertErrorPass: () => assertErrorPass,
|
|
46
48
|
assertFloat32Array: () => assertFloat32Array,
|
|
47
49
|
assertFloat32ArrayPass: () => assertFloat32ArrayPass,
|
|
48
50
|
assertFloat64Array: () => assertFloat64Array,
|
|
@@ -82,6 +84,8 @@ __export(type_guards_exports, {
|
|
|
82
84
|
assertNotDataViewPass: () => assertNotDataViewPass,
|
|
83
85
|
assertNotDate: () => assertNotDate,
|
|
84
86
|
assertNotDatePass: () => assertNotDatePass,
|
|
87
|
+
assertNotError: () => assertNotError,
|
|
88
|
+
assertNotErrorPass: () => assertNotErrorPass,
|
|
85
89
|
assertNotFloat32Array: () => assertNotFloat32Array,
|
|
86
90
|
assertNotFloat32ArrayPass: () => assertNotFloat32ArrayPass,
|
|
87
91
|
assertNotFloat64Array: () => assertNotFloat64Array,
|
|
@@ -188,6 +192,7 @@ __export(type_guards_exports, {
|
|
|
188
192
|
isDataView: () => isDataView,
|
|
189
193
|
isDate: () => isDate,
|
|
190
194
|
isDefined: () => isDefined,
|
|
195
|
+
isError: () => isError,
|
|
191
196
|
isFloat32Array: () => isFloat32Array,
|
|
192
197
|
isFloat64Array: () => isFloat64Array,
|
|
193
198
|
isFunction: () => isFunction,
|
|
@@ -207,6 +212,7 @@ __export(type_guards_exports, {
|
|
|
207
212
|
isNotBoolean: () => isNotBoolean,
|
|
208
213
|
isNotDataView: () => isNotDataView,
|
|
209
214
|
isNotDate: () => isNotDate,
|
|
215
|
+
isNotError: () => isNotError,
|
|
210
216
|
isNotFloat32Array: () => isNotFloat32Array,
|
|
211
217
|
isNotFloat64Array: () => isNotFloat64Array,
|
|
212
218
|
isNotFunction: () => isNotFunction,
|
|
@@ -1048,3 +1054,23 @@ function assertNotReadableStreamPass(value, message) {
|
|
|
1048
1054
|
assertNotReadableStream(value, message);
|
|
1049
1055
|
return value;
|
|
1050
1056
|
}
|
|
1057
|
+
function isError(value) {
|
|
1058
|
+
return value instanceof Error;
|
|
1059
|
+
}
|
|
1060
|
+
function isNotError(value) {
|
|
1061
|
+
return !isError(value);
|
|
1062
|
+
}
|
|
1063
|
+
function assertError(value, message = "Expected value to be Error.") {
|
|
1064
|
+
assert(isError(value), message);
|
|
1065
|
+
}
|
|
1066
|
+
function assertNotError(value, message = "Expected value to not be Error.") {
|
|
1067
|
+
assert(isNotError(value), message);
|
|
1068
|
+
}
|
|
1069
|
+
function assertErrorPass(value, message) {
|
|
1070
|
+
assertError(value, message);
|
|
1071
|
+
return value;
|
|
1072
|
+
}
|
|
1073
|
+
function assertNotErrorPass(value, message) {
|
|
1074
|
+
assertNotError(value, message);
|
|
1075
|
+
return value;
|
|
1076
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var value_or_provider_exports = {};
|
|
20
|
+
__export(value_or_provider_exports, {
|
|
21
|
+
resolveValueOrProvider: () => resolveValueOrProvider
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(value_or_provider_exports);
|
|
24
|
+
var import_type_guards = require("./type-guards.js");
|
|
25
|
+
function resolveValueOrProvider(valueOrProvider) {
|
|
26
|
+
if ((0, import_type_guards.isFunction)(valueOrProvider)) {
|
|
27
|
+
return valueOrProvider();
|
|
28
|
+
}
|
|
29
|
+
return valueOrProvider;
|
|
30
|
+
}
|