Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

Use promisifyServerListen with LiveServer

Ondra Urban09622883a635
diff --git a/src/puppeteer_live_view_server.js b/src/puppeteer_live_view_server.js
index 1c5006fb7..77d291c76 100644
--- a/src/puppeteer_live_view_server.js
+++ b/src/puppeteer_live_view_server.js
@@ -3,6 +3,7 @@ import EventEmitter from 'events';
import Promise from 'bluebird';
import WebSocket from 'ws';
import log from 'apify-shared/log';
+import { promisifyServerListen } from 'apify-shared/utilities';
import { checkParamOrThrow } from 'apify-client/build/utils';
import { layout, indexPage, detailPage, errorPage } from './puppeteer_live_view_client';
@@ -324,23 +325,20 @@ export default class PuppeteerLiveViewServer extends EventEmitter {
* @return {Promise} resolves when HTTP server starts listening
*/
startServer() {
- return new Promise((resolve, reject) => {
- const port = this.customPort == null ? process.env.APIFY_CONTAINER_PORT : this.customPort;
- if (port == null) {
- return reject(new Error('Neither options.port nor the environment variable APIFY_CONTAINER_PORT is set.' +
+ const port = this.customPort == null ? process.env.APIFY_CONTAINER_PORT : this.customPort;
+ if (port == null) {
+ return Promise.reject(new Error('Neither options.port nor the environment variable APIFY_CONTAINER_PORT is set.' +
'LiveViewServer cannot be started.'));
- }
+ }
- const server = http.createServer(this._httpRequestListener.bind(this));
- const wss = new WebSocket.Server({ server });
- wss.on('connection', this._wsRequestListener.bind(this));
- server.listen(port, (err) => {
- if (err) reject(err);
- log.info(`Live view server is listening on port ${port}.`);
+ const server = http.createServer(this._httpRequestListener.bind(this));
+ const wss = new WebSocket.Server({ server });
+ wss.on('connection', this._wsRequestListener.bind(this));
+ return promisifyServerListen(server)(port)
+ .then(() => {
+ log.info(`Live view server is listening on port ${server.address().port}.`);
this.httpServer = server;
- resolve();
});
- });
}
/**