How to use apify-shared - 10 common examples

To help you get started, we’ve selected a few apify-shared examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github apifytech / apify-js / test / events.js View on Github external
ws.on('close', () => {
                isWsConnected = false;
            });

            expect(req.url).to.be.eql('/someRunId');

            const send = obj => ws.send(JSON.stringify(obj));

            setTimeout(() => send({ name: 'name-1', data: [1, 2, 3] }), 50);
            setTimeout(() => send({ name: 'name-1', data: { foo: 'bar' } }), 100);
            setTimeout(() => send({ name: 'name-2', data: [1] }), 50);
            setTimeout(() => send({ name: 'name-2', data: [2] }), 50);
        });

        process.env[ENV_VARS.ACTOR_EVENTS_WS_URL] = 'ws://localhost:9099/someRunId';
        process.env[ENV_VARS.TOKEN] = 'dummy';

        // Run main and store received events
        expect(isWsConnected).to.be.eql(false);
        Apify.main(async () => {
            await delayPromise(10); // Here must be short sleep to get following line to later tick
            expect(isWsConnected).to.be.eql(true);
            Apify.events.on('name-1', data => eventsReceived.push(data));
            await delayPromise(1000);
        });

        // Main will call process.exit() so we must stub it.
        const stubbedExit = sinon
            .stub(process, 'exit')
            .callsFake(async (code) => {
                expect(code).to.be.eql(0);
                expect(eventsReceived).to.be.eql([[1, 2, 3], { foo: 'bar' }]);
github apifytech / apify-js / test / request_list.js View on Github external
.returns(Promise.resolve());
        Apify.events.emit(ACTOR_EVENT_NAMES_EX.PERSIST_STATE);
        await delayPromise(1);
        expect(requestList.isStatePersisted).to.be.eql(true);

        // Do some other changes and persist it again.
        const request2 = await requestList.fetchNextRequest();
        expect(requestList.isStatePersisted).to.be.eql(false);
        await requestList.markRequestHandled(request2);
        expect(requestList.isStatePersisted).to.be.eql(false);
        mock.expects('setValue')
            .once()
            .withArgs(PERSIST_STATE_KEY, requestList.getState())
            .returns(Promise.resolve());
        Apify.events.emit(ACTOR_EVENT_NAMES_EX.PERSIST_STATE);
        await delayPromise(1);
        expect(requestList.isStatePersisted).to.be.eql(true);

        // Reclaim event doesn't change the state.
        await requestList.reclaimRequest(request1);
        expect(requestList.isStatePersisted).to.be.eql(true);

        // Now initiate new request list from saved state and check that it's same as state
        // of original request list.
        mock.expects('getValue')
            .once()
            .withArgs(PERSIST_STATE_KEY)
            .returns(Promise.resolve(requestList.getState()));
        const requestList2 = new Apify.RequestList(opts);
        await requestList2.initialize();
        expect(requestList2.getState()).to.be.eql(requestList.getState());
github apifytech / apify-js / test / autoscaling / autoscaled_pool.js View on Github external
it('should not handle tasks added after isFinishedFunction returned true', async () => {
        const isFinished = async () => count > 10;
        let count = 0;

        // Run the pool and close it after 3s.
        const pool = new AutoscaledPool({
            minConcurrency: 3,
            runTaskFunction: async () => delayPromise(1).then(() => { count++; }),
            isFinishedFunction: async () => isFinished(),
            isTaskReadyFunction: async () => !await isFinished(),
        });
        pool.maybeRunIntervalMillis = 5;

        await pool.run();
        await delayPromise(10);
        // Check finished tasks.
        expect(count).to.be.within(11, 13);
    });
github apifytech / apify-js / test / puppeteer.js View on Github external
before(() => {
    prevEnvHeadless = process.env[ENV_VARS.HEADLESS];
    process.env[ENV_VARS.HEADLESS] = '1';

    // Find free port for the proxy
    return portastic.find({ min: 50000, max: 50100 }).then((ports) => {
        return new Promise((resolve, reject) => {
            const httpServer = http.createServer();

            // Setup proxy authorization
            httpServer.authenticate = function (req, fn) {
                // parse the "Proxy-Authorization" header
                const auth = req.headers['proxy-authorization'];
                if (!auth) {
                    // optimization: don't invoke the child process if no
                    // "Proxy-Authorization" header was given
                    // console.log('not Proxy-Authorization');
                    return fn(null, false);
                }
github apifytech / apify-js / test / puppeteer.js View on Github external
before(() => {
    prevEnvHeadless = process.env[ENV_VARS.HEADLESS];
    process.env[ENV_VARS.HEADLESS] = '1';

    // Find free port for the proxy
    return portastic.find({ min: 50000, max: 50100 }).then((ports) => {
        return new Promise((resolve, reject) => {
            const httpServer = http.createServer();

            // Setup proxy authorization
            httpServer.authenticate = function (req, fn) {
                // parse the "Proxy-Authorization" header
                const auth = req.headers['proxy-authorization'];
                if (!auth) {
                    // optimization: don't invoke the child process if no
                    // "Proxy-Authorization" header was given
                    // console.log('not Proxy-Authorization');
                    return fn(null, false);
github apifytech / apify-js / test / puppeteer.js View on Github external
it('should allow to use Apify proxy', async () => {
        process.env[ENV_VARS.PROXY_PASSWORD] = 'abc123';
        process.env[ENV_VARS.PROXY_HOSTNAME] = 'my.host.com';
        process.env[ENV_VARS.PROXY_PORT] = 123;

        const mock = sinon.mock(actor);
        mock.expects('getApifyProxyUrl')
            .once()
            .withArgs({
                session: 'xxx',
                groups: ['yyy'],
                groupsParamName: 'options.apifyProxyGroups',
                sessionParamName: 'options.apifyProxySession',
            })
            .returns(null); // Return null so that it doesn't start proxy-chain

        try {
            await Apify
github apifytech / apify-js / test / puppeteer_pool.js View on Github external
it('should work', async () => {
        process.env[ENV_VARS.PROXY_PASSWORD] = 'abc123';
        process.env[ENV_VARS.PROXY_HOSTNAME] = 'my.host.com';
        process.env[ENV_VARS.PROXY_PORT] = 123;

        const pool = new Apify.PuppeteerPool({
            maxOpenPagesPerInstance: 3,
            retireInstanceAfterRequestCount: 5,
        });
        const browsers = [];

        // Open 6 pages 3 in both browsers.
        browsers.push(pool.newPage());
        browsers.push(pool.newPage());
        browsers.push(pool.newPage());
        browsers.push(pool.newPage());
        browsers.push(pool.newPage());
        browsers.push(pool.newPage());
github apifytech / apify-js / test / utils.js View on Github external
it('should use ID from shared if neither parameter nor ENV var is provided', async () => {
        const cache = new LruCache({ maxLength: 5 });
        class MyStore {
            constructor(id) {
                this.id = id;
            }
        }

        // There is some default in shared constants.
        const defaultLocalValue = LOCAL_ENV_VARS[ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID];
        expect(defaultLocalValue).to.be.a('string');
        expect(defaultLocalValue).to.have.length.above(1);

        // There is no env var!
        expect(process.env[ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID]).to.be.eql(undefined);

        const store = await utils.openLocalStorage(null, ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID, MyStore, cache);
        expect(store.id).to.eql(defaultLocalValue);
    });
});
github apifytech / apify-js / test / utils.js View on Github external
it('should use ID from ENV variable and not call getOrCreateStoreFunction parameter is not provided', async () => {
        const cache = new LruCache({ maxLength: 5 });
        class MyStore {
            constructor(id) {
                this.id = id;
            }
        }

        process.env['some-env'] = 'id-from-env';

        const store = await utils.openRemoteStorage(null, 'some-env', MyStore, cache, async () => { throw new Error('Should not be called!'); }); // eslint-disable-line
        expect(store.id).to.eql('id-from-env');

        delete process.env['some-env'];
    });
});
github apifytech / apify-js / test / utils.js View on Github external
it('should return item from cache if available and create new one otherwise', async () => {
        const cache = new LruCache({ maxLength: 5 });
        class MyStore {
            constructor(id) {
                this.id = id;
            }
        }

        delete process.env['some-env'];

        expect(cache.length()).to.be.eql(0);

        const store = await utils.openRemoteStorage('some-id', 'some-env', MyStore, cache, async () => ({ id: 'some-id' }));
        expect(store.id).to.be.eql('some-id');
        expect(cache.length()).to.be.eql(1);

        const store2 = await utils.openRemoteStorage('some-id', 'some-env', MyStore, cache, async () => { throw new Error('Should not be called!'); }); // eslint-disable-line
        expect(store2.id).to.be.eql('some-id');