@spinajs/templates-puppeteer 2.0.438

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # @spinajs/templates-puppeteer
2
+
3
+ Base Puppeteer renderer for SpineJS templates. This package provides shared logic for rendering templates using Puppeteer.
4
+
5
+ ## Installation
6
+
7
+ ``bash
8
+ npm install @spinajs/templates-puppeteer
9
+ ``
@@ -0,0 +1,15 @@
1
+ declare const pdf: {
2
+ templates: {
3
+ pdf: {
4
+ static: {
5
+ portRange: number[];
6
+ };
7
+ args: {
8
+ headless: boolean;
9
+ };
10
+ options: {};
11
+ };
12
+ };
13
+ };
14
+ export default pdf;
15
+ //# sourceMappingURL=pdf.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pdf.d.ts","sourceRoot":"","sources":["../../../src/config/pdf.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,GAAG;;;;;;;;;;;;CAYR,CAAC;AAEF,eAAe,GAAG,CAAC"}
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const pdf = {
4
+ templates: {
5
+ pdf: {
6
+ static: {
7
+ portRange: [3000, 4000],
8
+ },
9
+ args: {
10
+ headless: true,
11
+ },
12
+ options: {},
13
+ },
14
+ },
15
+ };
16
+ exports.default = pdf;
17
+ //# sourceMappingURL=pdf.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pdf.js","sourceRoot":"","sources":["../../../src/config/pdf.ts"],"names":[],"mappings":";;AAAA,MAAM,GAAG,GAAG;IACV,SAAS,EAAE;QACT,GAAG,EAAE;YACH,MAAM,EAAE;gBACN,SAAS,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC;aACxB;YACD,IAAI,EAAE;gBACJ,QAAQ,EAAE,IAAI;aACf;YACD,OAAO,EAAE,EAAE;SACZ;KACF;CACF,CAAC;AAEF,kBAAe,GAAG,CAAC"}
@@ -0,0 +1,60 @@
1
+ import { Browser, Page, LaunchOptions } from 'puppeteer';
2
+ import { TemplateRenderer, Templates } from '@spinajs/templates';
3
+ import { Log } from '@spinajs/log';
4
+ import * as http from 'http';
5
+ import '@spinajs/templates-pug';
6
+ export interface IPuppeteerRendererOptions {
7
+ static: {
8
+ portRange: number[];
9
+ };
10
+ args: LaunchOptions;
11
+ /**
12
+ * Optional path to Chrome/Chromium executable.
13
+ * Useful when running in environments where Puppeteer cannot download Chromium.
14
+ * If provided, overrides args.executablePath.
15
+ */
16
+ executablePath?: string;
17
+ renderDurationWarning: number;
18
+ navigationTimeout?: number;
19
+ renderTimeout?: number;
20
+ /**
21
+ * Debug options
22
+ */
23
+ debug?: {
24
+ /**
25
+ * If true, browser will remain open after rendering for inspection
26
+ * Use it with headless: false in args to see the browser window ( puppetter.launch args )
27
+ */
28
+ close?: boolean;
29
+ };
30
+ }
31
+ export declare abstract class PuppeteerRenderer extends TemplateRenderer {
32
+ protected abstract Options: IPuppeteerRendererOptions;
33
+ protected Log: Log;
34
+ protected TemplatingService: Templates;
35
+ renderToFile(template: string, model: any, filePath: string, language?: string): Promise<void>;
36
+ /**
37
+ * Abstract method to perform specific rendering (PDF or image)
38
+ */
39
+ protected abstract performRender(page: Page, filePath: string): Promise<void>;
40
+ render(_templateName: string, _model: unknown, _language?: string): Promise<string>;
41
+ protected compile(_path: string): Promise<void>;
42
+ protected runLocalServer(basePath: string): Promise<http.Server>;
43
+ /**
44
+ * Enhanced browser cleanup with error handling
45
+ */
46
+ protected safeBrowserCleanup(browser: Browser): Promise<void>;
47
+ /**
48
+ * Force close browser with process termination
49
+ */
50
+ protected forceCloseBrowser(browser: Browser): Promise<void>;
51
+ /**
52
+ * Enhanced server cleanup with timeout
53
+ */
54
+ protected safeServerCleanup(server: http.Server): Promise<void>;
55
+ /**
56
+ * Add page event listeners with cleanup function
57
+ */
58
+ protected addPageEventListeners(page: any): () => void;
59
+ }
60
+ //# sourceMappingURL=index.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E,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;cAKhF,OAAO,CAAC,KAAK,EAAE,MAAM;cAErB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC;IA2CtE;;OAEG;cACa,kBAAkB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAoBnE;;OAEG;cACa,iBAAiB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAYlE;;OAEG;cACa,iBAAiB,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IA2BrE;;OAEG;IACH,SAAS,CAAC,qBAAqB,CAAC,IAAI,EAAE,GAAG,GAAG,MAAM,IAAI;CA0BvD"}
@@ -0,0 +1,273 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.PuppeteerRenderer = void 0;
16
+ const puppeteer_1 = __importDefault(require("puppeteer"));
17
+ const exceptions_1 = require("@spinajs/exceptions");
18
+ const templates_1 = require("@spinajs/templates");
19
+ const di_1 = require("@spinajs/di");
20
+ const path_1 = require("path");
21
+ const log_1 = require("@spinajs/log");
22
+ const express_1 = __importDefault(require("express"));
23
+ const cors_1 = __importDefault(require("cors"));
24
+ require("@spinajs/templates-pug");
25
+ const lodash_1 = __importDefault(require("lodash"));
26
+ class PuppeteerRenderer extends templates_1.TemplateRenderer {
27
+ async renderToFile(template, model, filePath, language) {
28
+ let server = null;
29
+ let browser = null;
30
+ try {
31
+ this.Log.timeStart(`puppeteer-template-rendering-${filePath}`);
32
+ this.Log.trace(`Rendering template ${template} to file ${filePath}`);
33
+ const templateBasePath = (0, path_1.dirname)(template);
34
+ // fire up local http server for serving images etc
35
+ // becouse chromium prevents from reading local files when not
36
+ // rendering file with file:// protocol for security reasons
37
+ server = await this.runLocalServer(templateBasePath);
38
+ const httpPort = server.address().port;
39
+ const compiledTemplate = await this.TemplatingService.render((0, path_1.join)(templateBasePath, (0, path_1.basename)(template, this.Extension)) + '.pug', {
40
+ // add template temporary server port
41
+ // so we can use it to render images in template
42
+ __http_template_port__: httpPort,
43
+ // for convinience add full url to local http server
44
+ __http_template_address__: `http://localhost:${httpPort}`,
45
+ ...model,
46
+ }, language);
47
+ const launchOptions = {
48
+ ...(this.Options?.args || {}),
49
+ ...(this.Options?.executablePath && {
50
+ executablePath: this.Options.executablePath,
51
+ }),
52
+ };
53
+ browser = await puppeteer_1.default.launch(launchOptions);
54
+ const page = await browser.newPage();
55
+ // Skip timeouts in debug mode
56
+ if (!this.Options?.debug?.close) {
57
+ page.setDefaultNavigationTimeout(this.Options?.navigationTimeout || 30000); // Default 30s
58
+ page.setDefaultTimeout(this.Options?.renderTimeout || 30000); // Default 30s
59
+ }
60
+ // Set up render timeout (skip in debug mode)
61
+ let renderTimeout;
62
+ if (!this.Options?.debug?.close) {
63
+ const timeoutMs = this.Options?.renderTimeout || 30000;
64
+ renderTimeout = setTimeout(async () => {
65
+ this.Log.warn(`Render timeout (${timeoutMs}ms) - forcing cleanup`);
66
+ try {
67
+ if (page)
68
+ await page.close().catch(() => { });
69
+ if (browser)
70
+ await this.forceCloseBrowser(browser);
71
+ }
72
+ catch (err) {
73
+ this.Log.error('Error during timeout cleanup:', err);
74
+ }
75
+ }, timeoutMs);
76
+ }
77
+ // Add event listeners with explicit cleanup tracking
78
+ const eventCleanup = this.addPageEventListeners(page);
79
+ try {
80
+ await page.setBypassCSP(true);
81
+ await page.setContent(compiledTemplate);
82
+ // Call abstract method to perform specific rendering (PDF or image)
83
+ await this.performRender(page, filePath);
84
+ // Clear timeout on successful completion
85
+ if (renderTimeout) {
86
+ clearTimeout(renderTimeout);
87
+ renderTimeout = undefined;
88
+ }
89
+ // Clean up event listeners
90
+ eventCleanup();
91
+ }
92
+ catch (renderError) {
93
+ // Clear timeout on error
94
+ if (renderTimeout) {
95
+ clearTimeout(renderTimeout);
96
+ renderTimeout = undefined;
97
+ }
98
+ this.Log.error(renderError, `Error during rendering for template ${template}`);
99
+ throw renderError;
100
+ }
101
+ }
102
+ catch (err) {
103
+ this.Log.error(err, `Error rendering template ${template} to file ${filePath}`);
104
+ throw err;
105
+ }
106
+ finally {
107
+ const duration = this.Log.timeEnd(`puppeteer-template-rendering-${filePath}`);
108
+ this.Log.trace(`Ended rendering template ${template} to file ${filePath}, took: ${duration}ms`);
109
+ if (this.Options && duration > this.Options.renderDurationWarning) {
110
+ this.Log.warn(`Rendering template ${template} to file ${filePath} took too long.`);
111
+ }
112
+ // Skip browser cleanup if debug.close is false (keep browser open for inspection)
113
+ if (browser && (!this.Options || this.Options.debug?.close !== false)) {
114
+ await this.safeBrowserCleanup(browser);
115
+ }
116
+ else if (browser) {
117
+ this.Log.info('Browser kept open for debugging (debug.close=false)');
118
+ }
119
+ if (server) {
120
+ await this.safeServerCleanup(server);
121
+ }
122
+ }
123
+ }
124
+ async render(_templateName, _model, _language) {
125
+ throw new exceptions_1.NotSupported('cannot render puppeteer template to string');
126
+ }
127
+ // no compilation at start
128
+ async compile(_path) { }
129
+ async runLocalServer(basePath) {
130
+ const self = this;
131
+ const app = (0, express_1.default)();
132
+ app.use((0, cors_1.default)());
133
+ app.use(express_1.default.static(basePath));
134
+ return new Promise((resolve, reject) => {
135
+ const server = app
136
+ // if no port is provided express will choose random port to start (available)
137
+ // if not, we will get random from range in config
138
+ .listen(!this.Options?.static?.portRange || this.Options.static.portRange.length === 0
139
+ ? 0
140
+ : lodash_1.default.random(this.Options.static.portRange[0], this.Options.static.portRange[1]))
141
+ .on('listening', function () {
142
+ self.Log.trace(`Puppeteer image server started on port ${this.address().port}`);
143
+ self.Log.trace(`Puppeteer static file dir at ${basePath}`);
144
+ resolve(this);
145
+ })
146
+ .on('error', (err) => {
147
+ self.Log.error(err, `Puppeteer image server cannot start`);
148
+ // Clean up the failed server
149
+ if (server) {
150
+ server.close(() => {
151
+ reject(err);
152
+ });
153
+ }
154
+ else {
155
+ reject(err);
156
+ }
157
+ });
158
+ // Set a timeout for server startup
159
+ setTimeout(() => {
160
+ if (!server.listening) {
161
+ server.close();
162
+ reject(new Error('Server startup timeout'));
163
+ }
164
+ }, 10000);
165
+ });
166
+ }
167
+ /**
168
+ * Enhanced browser cleanup with error handling
169
+ */
170
+ async safeBrowserCleanup(browser) {
171
+ try {
172
+ // First try to close all pages
173
+ const pages = await browser.pages();
174
+ await Promise.allSettled(pages.map(page => page.close()));
175
+ // Then close the browser normally
176
+ await browser.close();
177
+ }
178
+ catch (err) {
179
+ this.Log.warn(`Error during normal browser cleanup: ${err.message}`);
180
+ // Force kill if normal close fails
181
+ try {
182
+ await this.forceCloseBrowser(browser);
183
+ }
184
+ catch (killErr) {
185
+ this.Log.error(`Failed to force kill browser: ${killErr.message}`);
186
+ }
187
+ }
188
+ }
189
+ /**
190
+ * Force close browser with process termination
191
+ */
192
+ async forceCloseBrowser(browser) {
193
+ try {
194
+ const process = browser.process();
195
+ if (process) {
196
+ process.kill('SIGKILL');
197
+ this.Log.warn('Browser process force killed');
198
+ }
199
+ }
200
+ catch (err) {
201
+ this.Log.error(`Error force killing browser process: ${err.message}`);
202
+ }
203
+ }
204
+ /**
205
+ * Enhanced server cleanup with timeout
206
+ */
207
+ async safeServerCleanup(server) {
208
+ try {
209
+ await new Promise((resolve, reject) => {
210
+ const timeout = setTimeout(() => {
211
+ reject(new Error('Server close timeout'));
212
+ }, 5000);
213
+ server.close((err) => {
214
+ clearTimeout(timeout);
215
+ if (err)
216
+ reject(err);
217
+ else
218
+ resolve();
219
+ });
220
+ });
221
+ }
222
+ catch (err) {
223
+ this.Log.warn(`Error closing server: ${err.message}`);
224
+ // Force close connections if available
225
+ try {
226
+ if ('closeAllConnections' in server) {
227
+ server.closeAllConnections();
228
+ }
229
+ }
230
+ catch (forceErr) {
231
+ this.Log.error(`Error force closing server connections: ${forceErr.message}`);
232
+ }
233
+ }
234
+ }
235
+ /**
236
+ * Add page event listeners with cleanup function
237
+ */
238
+ addPageEventListeners(page) {
239
+ const listeners = {
240
+ console: (message) => this.Log.trace(`${message.type().substr(0, 3).toUpperCase()} ${message.text()}`),
241
+ pageerror: ({ message }) => this.Log.error(message),
242
+ response: (response) => this.Log.trace(`${response.status()} ${response.url()}`),
243
+ requestfailed: (request) => this.Log.error(`${request.failure().errorText} ${request.url()}`)
244
+ };
245
+ // Add listeners
246
+ page.on('console', listeners.console);
247
+ page.on('pageerror', listeners.pageerror);
248
+ page.on('response', listeners.response);
249
+ page.on('requestfailed', listeners.requestfailed);
250
+ // Return cleanup function
251
+ return () => {
252
+ try {
253
+ page.removeListener('console', listeners.console);
254
+ page.removeListener('pageerror', listeners.pageerror);
255
+ page.removeListener('response', listeners.response);
256
+ page.removeListener('requestfailed', listeners.requestfailed);
257
+ }
258
+ catch (err) {
259
+ this.Log.warn(`Error removing page listeners: ${err.message}`);
260
+ }
261
+ };
262
+ }
263
+ }
264
+ exports.PuppeteerRenderer = PuppeteerRenderer;
265
+ __decorate([
266
+ (0, log_1.Logger)('puppeteer-templates'),
267
+ __metadata("design:type", log_1.Log)
268
+ ], PuppeteerRenderer.prototype, "Log", void 0);
269
+ __decorate([
270
+ (0, di_1.LazyInject)(),
271
+ __metadata("design:type", templates_1.Templates)
272
+ ], PuppeteerRenderer.prototype, "TemplatingService", void 0);
273
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,0DAA+E;AAC/E,oDAAmD;AACnD,kDAAiE;AACjE,oCAAyC;AACzC,+BAA+C;AAC/C,sCAA2C;AAC3C,sDAA8B;AAE9B,gDAAwB;AAExB,kCAAgC;AAChC,oDAAuB;AAgCvB,MAAsB,iBAAkB,SAAQ,4BAAgB;IASvD,KAAK,CAAC,YAAY,CAAC,QAAgB,EAAE,KAAU,EAAE,QAAgB,EAAE,QAAiB;QACzF,IAAI,MAAM,GAAgB,IAAI,CAAC;QAC/B,IAAI,OAAO,GAAY,IAAI,CAAC;QAC5B,IAAI,CAAC;YACH,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,gCAAgC,QAAQ,EAAE,CAAC,CAAC;YAC/D,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,sBAAsB,QAAQ,YAAY,QAAQ,EAAE,CAAC,CAAC;YAErE,MAAM,gBAAgB,GAAG,IAAA,cAAO,EAAC,QAAQ,CAAC,CAAC;YAE3C,mDAAmD;YACnD,8DAA8D;YAC9D,4DAA4D;YAC5D,MAAM,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,gBAAgB,CAAC,CAAC;YACrD,MAAM,QAAQ,GAAI,MAAM,CAAC,OAAO,EAAkB,CAAC,IAAI,CAAC;YAExD,MAAM,gBAAgB,GAAG,MAAM,IAAI,CAAC,iBAAiB,CAAC,MAAM,CAC1D,IAAA,WAAI,EAAC,gBAAgB,EAAE,IAAA,eAAQ,EAAC,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC,GAAG,MAAM,EACnE;gBACE,qCAAqC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AAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,kCAAkC,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC;YACjE,CAAC;QACH,CAAC,CAAC;IACJ,CAAC;CACF;AAjRD,8CAiRC;AA7QW;IADT,IAAA,YAAM,EAAC,qBAAqB,CAAC;8BACf,SAAG;8CAAC;AAGT;IADT,IAAA,eAAU,GAAE;8BACgB,qBAAS;4DAAC"}
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -0,0 +1,15 @@
1
+ declare const pdf: {
2
+ templates: {
3
+ pdf: {
4
+ static: {
5
+ portRange: number[];
6
+ };
7
+ args: {
8
+ headless: boolean;
9
+ };
10
+ options: {};
11
+ };
12
+ };
13
+ };
14
+ export default pdf;
15
+ //# sourceMappingURL=pdf.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pdf.d.ts","sourceRoot":"","sources":["../../../src/config/pdf.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,GAAG;;;;;;;;;;;;CAYR,CAAC;AAEF,eAAe,GAAG,CAAC"}
@@ -0,0 +1,15 @@
1
+ const pdf = {
2
+ templates: {
3
+ pdf: {
4
+ static: {
5
+ portRange: [3000, 4000],
6
+ },
7
+ args: {
8
+ headless: true,
9
+ },
10
+ options: {},
11
+ },
12
+ },
13
+ };
14
+ export default pdf;
15
+ //# sourceMappingURL=pdf.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pdf.js","sourceRoot":"","sources":["../../../src/config/pdf.ts"],"names":[],"mappings":"AAAA,MAAM,GAAG,GAAG;IACV,SAAS,EAAE;QACT,GAAG,EAAE;YACH,MAAM,EAAE;gBACN,SAAS,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC;aACxB;YACD,IAAI,EAAE;gBACJ,QAAQ,EAAE,IAAI;aACf;YACD,OAAO,EAAE,EAAE;SACZ;KACF;CACF,CAAC;AAEF,eAAe,GAAG,CAAC"}
@@ -0,0 +1,60 @@
1
+ import { Browser, Page, LaunchOptions } from 'puppeteer';
2
+ import { TemplateRenderer, Templates } from '@spinajs/templates';
3
+ import { Log } from '@spinajs/log';
4
+ import * as http from 'http';
5
+ import '@spinajs/templates-pug';
6
+ export interface IPuppeteerRendererOptions {
7
+ static: {
8
+ portRange: number[];
9
+ };
10
+ args: LaunchOptions;
11
+ /**
12
+ * Optional path to Chrome/Chromium executable.
13
+ * Useful when running in environments where Puppeteer cannot download Chromium.
14
+ * If provided, overrides args.executablePath.
15
+ */
16
+ executablePath?: string;
17
+ renderDurationWarning: number;
18
+ navigationTimeout?: number;
19
+ renderTimeout?: number;
20
+ /**
21
+ * Debug options
22
+ */
23
+ debug?: {
24
+ /**
25
+ * If true, browser will remain open after rendering for inspection
26
+ * Use it with headless: false in args to see the browser window ( puppetter.launch args )
27
+ */
28
+ close?: boolean;
29
+ };
30
+ }
31
+ export declare abstract class PuppeteerRenderer extends TemplateRenderer {
32
+ protected abstract Options: IPuppeteerRendererOptions;
33
+ protected Log: Log;
34
+ protected TemplatingService: Templates;
35
+ renderToFile(template: string, model: any, filePath: string, language?: string): Promise<void>;
36
+ /**
37
+ * Abstract method to perform specific rendering (PDF or image)
38
+ */
39
+ protected abstract performRender(page: Page, filePath: string): Promise<void>;
40
+ render(_templateName: string, _model: unknown, _language?: string): Promise<string>;
41
+ protected compile(_path: string): Promise<void>;
42
+ protected runLocalServer(basePath: string): Promise<http.Server>;
43
+ /**
44
+ * Enhanced browser cleanup with error handling
45
+ */
46
+ protected safeBrowserCleanup(browser: Browser): Promise<void>;
47
+ /**
48
+ * Force close browser with process termination
49
+ */
50
+ protected forceCloseBrowser(browser: Browser): Promise<void>;
51
+ /**
52
+ * Enhanced server cleanup with timeout
53
+ */
54
+ protected safeServerCleanup(server: http.Server): Promise<void>;
55
+ /**
56
+ * Add page event listeners with cleanup function
57
+ */
58
+ protected addPageEventListeners(page: any): () => void;
59
+ }
60
+ //# sourceMappingURL=index.d.ts.map
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