nest-puppeteer 1.1.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs ADDED
@@ -0,0 +1,298 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
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
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var __decorateClass = (decorators, target, key, kind) => {
30
+ var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
31
+ for (var i = decorators.length - 1, decorator; i >= 0; i--)
32
+ if (decorator = decorators[i])
33
+ result = (kind ? decorator(target, key, result) : decorator(result)) || result;
34
+ if (kind && result) __defProp(target, key, result);
35
+ return result;
36
+ };
37
+ var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
38
+
39
+ // src/index.ts
40
+ var index_exports = {};
41
+ __export(index_exports, {
42
+ InjectBrowser: () => InjectBrowser,
43
+ InjectContext: () => InjectContext,
44
+ InjectPage: () => InjectPage,
45
+ PuppeteerModule: () => PuppeteerModule,
46
+ getBrowserToken: () => getBrowserToken,
47
+ getContextToken: () => getContextToken,
48
+ getPageToken: () => getPageToken
49
+ });
50
+ module.exports = __toCommonJS(index_exports);
51
+
52
+ // src/puppeteer.decorators.ts
53
+ var import_common = require("@nestjs/common");
54
+
55
+ // src/puppeteer.constants.ts
56
+ var PUPPETEER_INSTANCE_NAME = "PuppeteerInstanceName";
57
+ var PUPPETEER_MODULE_OPTIONS = "PuppeteerModuleOptions";
58
+ var DEFAULT_PUPPETEER_INSTANCE_NAME = "DefaultPuppeteer";
59
+ var args = [
60
+ "--allow-insecure-localhost",
61
+ // Enables TLS/SSL errors on localhost to be ignored (no interstitial, no blocking of requests).
62
+ "--allow-http-screen-capture",
63
+ // Allow non-secure origins to use the screen capture API and the desktopCapture extension API.
64
+ "--no-zygote"
65
+ // https://codereview.chromium.org/2384163002
66
+ ];
67
+ if (typeof process.getuid === "function") {
68
+ args.push("--no-sandbox");
69
+ }
70
+ var DEFAULT_CHROME_LAUNCH_OPTIONS = {
71
+ headless: true,
72
+ pipe: process.platform !== "win32",
73
+ args
74
+ };
75
+
76
+ // src/puppeteer.util.ts
77
+ function getBrowserToken(instanceName = DEFAULT_PUPPETEER_INSTANCE_NAME) {
78
+ return `${instanceName}Browser`;
79
+ }
80
+ function getContextToken(instanceName = DEFAULT_PUPPETEER_INSTANCE_NAME) {
81
+ return `${instanceName}Context`;
82
+ }
83
+ function getPageToken(instanceName = DEFAULT_PUPPETEER_INSTANCE_NAME) {
84
+ return `${instanceName}Page`;
85
+ }
86
+
87
+ // src/puppeteer.decorators.ts
88
+ var InjectBrowser = (instanceName) => (0, import_common.Inject)(getBrowserToken(instanceName));
89
+ var InjectContext = (instanceName) => (0, import_common.Inject)(getContextToken(instanceName));
90
+ var InjectPage = (instanceName) => (0, import_common.Inject)(getPageToken(instanceName));
91
+
92
+ // src/puppeteer.module.ts
93
+ var import_common3 = require("@nestjs/common");
94
+
95
+ // src/puppeteer.providers.ts
96
+ function createPuppeteerProviders(instanceName, pages = []) {
97
+ return pages.map((page) => ({
98
+ provide: getPageToken(page),
99
+ useFactory: (context) => context.newPage(),
100
+ inject: [getContextToken(instanceName)]
101
+ }));
102
+ }
103
+
104
+ // src/puppeteer-core.module.ts
105
+ var import_common2 = require("@nestjs/common");
106
+ var import_puppeteer = __toESM(require("puppeteer"), 1);
107
+ var PuppeteerCoreModule = class {
108
+ constructor(instanceName, moduleRef) {
109
+ this.instanceName = instanceName;
110
+ this.moduleRef = moduleRef;
111
+ }
112
+ onApplicationShutdown() {
113
+ return this.onModuleDestroy();
114
+ }
115
+ static forRoot(launchOptions = DEFAULT_CHROME_LAUNCH_OPTIONS, instanceName = DEFAULT_PUPPETEER_INSTANCE_NAME) {
116
+ const instanceNameProvider = {
117
+ provide: PUPPETEER_INSTANCE_NAME,
118
+ useValue: instanceName
119
+ };
120
+ const browserProvider = {
121
+ provide: getBrowserToken(instanceName),
122
+ async useFactory() {
123
+ return await import_puppeteer.default.launch(launchOptions);
124
+ }
125
+ };
126
+ const contextProvider = {
127
+ provide: getContextToken(instanceName),
128
+ async useFactory(browser) {
129
+ return browser.createBrowserContext();
130
+ },
131
+ inject: [getBrowserToken(instanceName)]
132
+ };
133
+ const pageProvider = {
134
+ provide: getPageToken(instanceName),
135
+ async useFactory(context) {
136
+ return await context.newPage();
137
+ },
138
+ inject: [getContextToken(instanceName)]
139
+ };
140
+ return {
141
+ module: PuppeteerCoreModule,
142
+ providers: [instanceNameProvider, browserProvider, contextProvider, pageProvider],
143
+ exports: [browserProvider, contextProvider, pageProvider]
144
+ };
145
+ }
146
+ static forRootAsync(options) {
147
+ const puppeteerInstanceName = options.instanceName ?? DEFAULT_PUPPETEER_INSTANCE_NAME;
148
+ const instanceNameProvider = {
149
+ provide: PUPPETEER_INSTANCE_NAME,
150
+ useValue: puppeteerInstanceName
151
+ };
152
+ const browserProvider = {
153
+ provide: getBrowserToken(puppeteerInstanceName),
154
+ async useFactory(puppeteerModuleOptions) {
155
+ return await import_puppeteer.default.launch(
156
+ puppeteerModuleOptions.launchOptions ?? DEFAULT_CHROME_LAUNCH_OPTIONS
157
+ );
158
+ },
159
+ inject: [PUPPETEER_MODULE_OPTIONS]
160
+ };
161
+ const contextProvider = {
162
+ provide: getContextToken(puppeteerInstanceName),
163
+ async useFactory(browser) {
164
+ return await browser.createBrowserContext();
165
+ },
166
+ inject: [getBrowserToken(puppeteerInstanceName)]
167
+ };
168
+ const pageProvider = {
169
+ provide: getPageToken(puppeteerInstanceName),
170
+ async useFactory(context) {
171
+ return await context.newPage();
172
+ },
173
+ inject: [getContextToken(puppeteerInstanceName)]
174
+ };
175
+ const asyncProviders = this.createAsyncProviders(options);
176
+ return {
177
+ module: PuppeteerCoreModule,
178
+ imports: options.imports,
179
+ providers: [
180
+ ...asyncProviders,
181
+ browserProvider,
182
+ contextProvider,
183
+ pageProvider,
184
+ instanceNameProvider
185
+ ],
186
+ exports: [browserProvider, contextProvider, pageProvider]
187
+ };
188
+ }
189
+ async onModuleDestroy() {
190
+ const browser = this.moduleRef.get(getBrowserToken(this.instanceName));
191
+ if (browser?.isConnected()) await browser.close();
192
+ }
193
+ static createAsyncProviders(options) {
194
+ if (options.useExisting || options.useFactory) {
195
+ return [this.createAsyncOptionsProvider(options)];
196
+ } else if (options.useClass) {
197
+ return [
198
+ this.createAsyncOptionsProvider(options),
199
+ {
200
+ provide: options.useClass,
201
+ useClass: options.useClass
202
+ }
203
+ ];
204
+ } else {
205
+ return [];
206
+ }
207
+ }
208
+ static createAsyncOptionsProvider(options) {
209
+ if (options.useFactory) {
210
+ return {
211
+ provide: PUPPETEER_MODULE_OPTIONS,
212
+ useFactory: options.useFactory,
213
+ inject: options.inject ?? []
214
+ };
215
+ } else if (options.useExisting) {
216
+ return {
217
+ provide: PUPPETEER_MODULE_OPTIONS,
218
+ async useFactory(optionsFactory) {
219
+ return optionsFactory.createPuppeteerOptions();
220
+ },
221
+ inject: [options.useExisting]
222
+ };
223
+ } else if (options.useClass) {
224
+ return {
225
+ provide: PUPPETEER_MODULE_OPTIONS,
226
+ async useFactory(optionsFactory) {
227
+ return optionsFactory.createPuppeteerOptions();
228
+ },
229
+ inject: [options.useClass]
230
+ };
231
+ } else {
232
+ throw new Error("Invalid PuppeteerModule options");
233
+ }
234
+ }
235
+ };
236
+ PuppeteerCoreModule = __decorateClass([
237
+ (0, import_common2.Global)(),
238
+ (0, import_common2.Module)({}),
239
+ __decorateParam(0, (0, import_common2.Inject)(PUPPETEER_INSTANCE_NAME))
240
+ ], PuppeteerCoreModule);
241
+
242
+ // src/puppeteer.module.ts
243
+ var PuppeteerModule = class {
244
+ /**
245
+ * Inject the Puppeteer synchronously.
246
+ * @param options Options for the Browser to be launched
247
+ * @param instanceName A unique name for the connection. If not specified, a default name
248
+ * will be used.
249
+ */
250
+ static forRoot(options, instanceName) {
251
+ const { isGlobal, ...launchOptions } = options ?? {};
252
+ const effectiveLaunchOptions = Object.keys(launchOptions).length > 0 ? launchOptions : void 0;
253
+ return {
254
+ module: PuppeteerModule,
255
+ global: isGlobal,
256
+ imports: [PuppeteerCoreModule.forRoot(effectiveLaunchOptions, instanceName)]
257
+ };
258
+ }
259
+ /**
260
+ * Inject the Puppeteer asynchronously, allowing any dependencies such as a configuration
261
+ * service to be injected first.
262
+ * @param options Options for asynchronous injection
263
+ */
264
+ static forRootAsync(options) {
265
+ return {
266
+ module: PuppeteerModule,
267
+ global: options.isGlobal,
268
+ imports: [PuppeteerCoreModule.forRootAsync(options)]
269
+ };
270
+ }
271
+ /**
272
+ * Inject Pages.
273
+ * @param pages An array of the names of the pages to be injected.
274
+ * @param instanceName A unique name for the connection. If not specified, a default name
275
+ * will be used.
276
+ */
277
+ static forFeature(pages = [], instanceName) {
278
+ const providers = createPuppeteerProviders(instanceName, pages);
279
+ return {
280
+ module: PuppeteerModule,
281
+ providers,
282
+ exports: providers
283
+ };
284
+ }
285
+ };
286
+ PuppeteerModule = __decorateClass([
287
+ (0, import_common3.Module)({})
288
+ ], PuppeteerModule);
289
+ // Annotate the CommonJS export names for ESM import in node:
290
+ 0 && (module.exports = {
291
+ InjectBrowser,
292
+ InjectContext,
293
+ InjectPage,
294
+ PuppeteerModule,
295
+ getBrowserToken,
296
+ getContextToken,
297
+ getPageToken
298
+ });
@@ -0,0 +1,103 @@
1
+ import { ModuleMetadata, Type, InjectionToken, DynamicModule } from '@nestjs/common';
2
+ import { LaunchOptions } from 'puppeteer';
3
+
4
+ /**
5
+ * Inject the Browser object associated with a connection
6
+ * @param instanceName The unique name associated with the browser
7
+ */
8
+ declare const InjectBrowser: (instanceName?: string) => PropertyDecorator & ParameterDecorator;
9
+ /**
10
+ * Inject the Puppeteer BrowserContext object associated with a browser
11
+ * @param instanceName The unique name associated with the browser
12
+ */
13
+ declare const InjectContext: (instanceName?: string) => PropertyDecorator & ParameterDecorator;
14
+ /**
15
+ * Inject the Puppeteer Page object associated with BrowserContext
16
+ * @param instanceName The unique name associated with the instance
17
+ */
18
+ declare const InjectPage: (instanceName?: string) => PropertyDecorator & ParameterDecorator;
19
+
20
+ /**
21
+ * Options that ultimately need to be provided to create a Puppeteer instance
22
+ */
23
+ interface PuppeteerModuleOptions {
24
+ instanceName?: string;
25
+ launchOptions?: LaunchOptions;
26
+ }
27
+ interface PuppeteerOptionsFactory {
28
+ createPuppeteerOptions(): Promise<PuppeteerModuleOptions> | PuppeteerModuleOptions;
29
+ }
30
+ /**
31
+ * Options available when creating the module asynchronously. You should use only one of the
32
+ * useExisting, useClass, or useFactory options for creation.
33
+ */
34
+ interface PuppeteerModuleAsyncOptions extends Pick<ModuleMetadata, "imports"> {
35
+ /** A unique name for the instance. If not specified, a default one will be used. */
36
+ instanceName?: string;
37
+ /**
38
+ * If "true", registers `PuppeteerModule` as a global module.
39
+ * See: https://docs.nestjs.com/modules#global-modules
40
+ */
41
+ isGlobal?: boolean;
42
+ /** Reuse an injectable factory class created in another module. */
43
+ useExisting?: Type<PuppeteerOptionsFactory>;
44
+ /**
45
+ * Use an injectable factory class to populate the module options, such as URI and database name.
46
+ */
47
+ useClass?: Type<PuppeteerOptionsFactory>;
48
+ /**
49
+ * A factory function that will populate the module options, such as URI and database name.
50
+ */
51
+ useFactory?: (...args: unknown[]) => Promise<PuppeteerModuleOptions> | PuppeteerModuleOptions;
52
+ /**
53
+ * Inject any dependencies required by the Puppeteer module, such as a configuration service
54
+ * that supplies the options and instance name
55
+ */
56
+ inject?: InjectionToken[];
57
+ }
58
+
59
+ /**
60
+ * Module for the Puppeteer
61
+ */
62
+ declare class PuppeteerModule {
63
+ /**
64
+ * Inject the Puppeteer synchronously.
65
+ * @param options Options for the Browser to be launched
66
+ * @param instanceName A unique name for the connection. If not specified, a default name
67
+ * will be used.
68
+ */
69
+ static forRoot(options?: PuppeteerModuleOptions["launchOptions"] & {
70
+ isGlobal?: boolean;
71
+ }, instanceName?: string): DynamicModule;
72
+ /**
73
+ * Inject the Puppeteer asynchronously, allowing any dependencies such as a configuration
74
+ * service to be injected first.
75
+ * @param options Options for asynchronous injection
76
+ */
77
+ static forRootAsync(options: PuppeteerModuleAsyncOptions): DynamicModule;
78
+ /**
79
+ * Inject Pages.
80
+ * @param pages An array of the names of the pages to be injected.
81
+ * @param instanceName A unique name for the connection. If not specified, a default name
82
+ * will be used.
83
+ */
84
+ static forFeature(pages?: string[], instanceName?: string): DynamicModule;
85
+ }
86
+
87
+ /**
88
+ * Get a token for the Puppeteer instance for the given Browser name
89
+ * @param instanceName The unique name for the Puppeteer instance
90
+ */
91
+ declare function getBrowserToken(instanceName?: string): string;
92
+ /**
93
+ * Get a token for the Puppeteer instance for the given BrowserContext name
94
+ * @param instanceName The unique name for the Puppeteer instance
95
+ */
96
+ declare function getContextToken(instanceName?: string): string;
97
+ /**
98
+ * Get a token for the Puppeteer instance for the given Page name
99
+ * @param instanceName The unique name for the Puppeteer instance
100
+ */
101
+ declare function getPageToken(instanceName?: string): string;
102
+
103
+ export { InjectBrowser, InjectContext, InjectPage, PuppeteerModule, type PuppeteerModuleAsyncOptions, type PuppeteerModuleOptions, type PuppeteerOptionsFactory, getBrowserToken, getContextToken, getPageToken };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,103 @@
1
- export * from './puppeteer.decorators';
2
- export * from './puppeteer.module';
3
- export * from './puppeteer.util';
4
- export * from './interfaces';
1
+ import { ModuleMetadata, Type, InjectionToken, DynamicModule } from '@nestjs/common';
2
+ import { LaunchOptions } from 'puppeteer';
3
+
4
+ /**
5
+ * Inject the Browser object associated with a connection
6
+ * @param instanceName The unique name associated with the browser
7
+ */
8
+ declare const InjectBrowser: (instanceName?: string) => PropertyDecorator & ParameterDecorator;
9
+ /**
10
+ * Inject the Puppeteer BrowserContext object associated with a browser
11
+ * @param instanceName The unique name associated with the browser
12
+ */
13
+ declare const InjectContext: (instanceName?: string) => PropertyDecorator & ParameterDecorator;
14
+ /**
15
+ * Inject the Puppeteer Page object associated with BrowserContext
16
+ * @param instanceName The unique name associated with the instance
17
+ */
18
+ declare const InjectPage: (instanceName?: string) => PropertyDecorator & ParameterDecorator;
19
+
20
+ /**
21
+ * Options that ultimately need to be provided to create a Puppeteer instance
22
+ */
23
+ interface PuppeteerModuleOptions {
24
+ instanceName?: string;
25
+ launchOptions?: LaunchOptions;
26
+ }
27
+ interface PuppeteerOptionsFactory {
28
+ createPuppeteerOptions(): Promise<PuppeteerModuleOptions> | PuppeteerModuleOptions;
29
+ }
30
+ /**
31
+ * Options available when creating the module asynchronously. You should use only one of the
32
+ * useExisting, useClass, or useFactory options for creation.
33
+ */
34
+ interface PuppeteerModuleAsyncOptions extends Pick<ModuleMetadata, "imports"> {
35
+ /** A unique name for the instance. If not specified, a default one will be used. */
36
+ instanceName?: string;
37
+ /**
38
+ * If "true", registers `PuppeteerModule` as a global module.
39
+ * See: https://docs.nestjs.com/modules#global-modules
40
+ */
41
+ isGlobal?: boolean;
42
+ /** Reuse an injectable factory class created in another module. */
43
+ useExisting?: Type<PuppeteerOptionsFactory>;
44
+ /**
45
+ * Use an injectable factory class to populate the module options, such as URI and database name.
46
+ */
47
+ useClass?: Type<PuppeteerOptionsFactory>;
48
+ /**
49
+ * A factory function that will populate the module options, such as URI and database name.
50
+ */
51
+ useFactory?: (...args: unknown[]) => Promise<PuppeteerModuleOptions> | PuppeteerModuleOptions;
52
+ /**
53
+ * Inject any dependencies required by the Puppeteer module, such as a configuration service
54
+ * that supplies the options and instance name
55
+ */
56
+ inject?: InjectionToken[];
57
+ }
58
+
59
+ /**
60
+ * Module for the Puppeteer
61
+ */
62
+ declare class PuppeteerModule {
63
+ /**
64
+ * Inject the Puppeteer synchronously.
65
+ * @param options Options for the Browser to be launched
66
+ * @param instanceName A unique name for the connection. If not specified, a default name
67
+ * will be used.
68
+ */
69
+ static forRoot(options?: PuppeteerModuleOptions["launchOptions"] & {
70
+ isGlobal?: boolean;
71
+ }, instanceName?: string): DynamicModule;
72
+ /**
73
+ * Inject the Puppeteer asynchronously, allowing any dependencies such as a configuration
74
+ * service to be injected first.
75
+ * @param options Options for asynchronous injection
76
+ */
77
+ static forRootAsync(options: PuppeteerModuleAsyncOptions): DynamicModule;
78
+ /**
79
+ * Inject Pages.
80
+ * @param pages An array of the names of the pages to be injected.
81
+ * @param instanceName A unique name for the connection. If not specified, a default name
82
+ * will be used.
83
+ */
84
+ static forFeature(pages?: string[], instanceName?: string): DynamicModule;
85
+ }
86
+
87
+ /**
88
+ * Get a token for the Puppeteer instance for the given Browser name
89
+ * @param instanceName The unique name for the Puppeteer instance
90
+ */
91
+ declare function getBrowserToken(instanceName?: string): string;
92
+ /**
93
+ * Get a token for the Puppeteer instance for the given BrowserContext name
94
+ * @param instanceName The unique name for the Puppeteer instance
95
+ */
96
+ declare function getContextToken(instanceName?: string): string;
97
+ /**
98
+ * Get a token for the Puppeteer instance for the given Page name
99
+ * @param instanceName The unique name for the Puppeteer instance
100
+ */
101
+ declare function getPageToken(instanceName?: string): string;
102
+
103
+ export { InjectBrowser, InjectContext, InjectPage, PuppeteerModule, type PuppeteerModuleAsyncOptions, type PuppeteerModuleOptions, type PuppeteerOptionsFactory, getBrowserToken, getContextToken, getPageToken };