Skip to content

Commit 1c2b81b

Browse files
gooosemanScriptSmith
authored andcommitted
feat(Instagram): adds browserInstance option
1 parent aa99128 commit 1c2b81b

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

src/api/api.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {Type} from "io-ts";
2+
import {Browser} from "puppeteer";
23
import * as winston from "winston";
34
import {DType, IPlugin} from "../../plugins";
45
import {Instagram} from "./instagram";
@@ -59,6 +60,10 @@ export interface IOptionsCommon {
5960

6061
// Custom io-ts validator
6162
validator?: Type<unknown>;
63+
64+
// Pass puppeter Browser instance from outside.
65+
// Be careful to close Browser by yourself, when there is no need in it anymore.
66+
browserInstance?: Browser;
6267
}
6368

6469
export interface IOptionsFullApi extends IOptionsCommon {

src/api/instagram.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ export class Instagram<PostType> {
107107
// Puppeteer state
108108
private browser: Browser;
109109
private browserDisconnected: boolean = true;
110+
/** Browser instance passed from outside */
111+
private readonly browserInstance?: Browser;
110112
private readonly headless: boolean;
111113

112114
// Array of scraped posts and lock
@@ -207,6 +209,7 @@ export class Instagram<PostType> {
207209
this.total = options.total;
208210
this.pageQuery = pageQuery;
209211
this.edgeQuery = edgeQuery;
212+
this.browserInstance = options.browserInstance;
210213
this.headless = options.headless;
211214
this.logger = options.logger;
212215
this.silent = options.silent;
@@ -357,7 +360,11 @@ export class Instagram<PostType> {
357360
await this.page.close();
358361
}
359362

360-
if (this.finished && !this.browserDisconnected) {
363+
if (
364+
this.finished &&
365+
!this.browserDisconnected &&
366+
!this.browserInstance
367+
) {
361368
await this.browser.close();
362369
}
363370
}
@@ -705,7 +712,15 @@ export class Instagram<PostType> {
705712
}
706713

707714
// Launch browser
708-
if (!this.sameBrowser || (this.sameBrowser && !this.started)) {
715+
if (this.browserInstance) {
716+
await this.progress(Progress.LAUNCHING);
717+
this.browser = this.browserInstance;
718+
this.browserDisconnected = !this.browser.isConnected();
719+
this.browser.on(
720+
"disconnected",
721+
() => (this.browserDisconnected = true),
722+
);
723+
} else if (!this.sameBrowser || (this.sameBrowser && !this.started)) {
709724
await this.progress(Progress.LAUNCHING);
710725
this.browser = await launch(options);
711726
this.browserDisconnected = false;

tests/test.spec.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as t from "io-ts";
2-
import {Overrides, Request} from "puppeteer";
2+
import {launch, Overrides, Request} from "puppeteer";
33
import * as winston from "winston";
44
import {createApi, IPlugin} from "..";
55
import {plugins} from "..";
@@ -626,3 +626,36 @@ describe("Plugins", () => {
626626
}
627627
});
628628
});
629+
630+
describe("Browser instance passed from outside", () => {
631+
const browserOptions = {
632+
headless: true,
633+
};
634+
testWrapper("Should re-use this browser instance", async () => {
635+
const browser = await launch(browserOptions);
636+
637+
const hashtagGenerator = createApi("hashtag", hashtags[0], {
638+
browserInstance: browser,
639+
}).generator();
640+
await hashtagGenerator.next();
641+
642+
const pages = await browser.pages();
643+
644+
expect(pages.length).toBe(2);
645+
646+
await browser.close();
647+
});
648+
649+
testWrapper("Should not close browser instance", async () => {
650+
const browser = await launch(browserOptions);
651+
652+
const searchGenerator = createApi("search", "therock", {
653+
browserInstance: browser,
654+
}).generator();
655+
await searchGenerator.next();
656+
657+
expect(browser.isConnected()).toBe(true);
658+
659+
await browser.close();
660+
});
661+
});

0 commit comments

Comments
 (0)