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.

14.2 KB
Raw Blame
import { resolve } from 'node:path';

import { FileSystemStorageBackend } from '@crawlee/fs-storage';
import { Dataset, KeyValueStore, MemoryStorageBackend, RequestQueue, serviceLocator } from '@crawlee/core';
import type { StorageBackend } from '@crawlee/types';
import { ensureDir, rm } from 'fs-extra';

import { cryptoRandomObjectId } from '@apify/utilities';

beforeEach(async () => {
    serviceLocator.setStorageBackend(new MemoryStorageBackend());
});

describe('storage aliases', () => {
    describe('Dataset.open with alias', () => {
        test('should open a dataset with an alias', async () => {
            const dataset = await Dataset.open({ alias: 'my-data' });
            expect(dataset).toBeDefined();
            expect(dataset.id).toBeDefined();
            // Alias storages are unnamed
            expect(dataset.name).toBeUndefined();
        });

        test('should return the same instance for the same alias', async () => {
            const dataset1 = await Dataset.open({ alias: 'my-data' });
            const dataset2 = await Dataset.open({ alias: 'my-data' });
            expect(dataset1).toBe(dataset2);
        });

        test('should return different instances for different aliases', async () => {
            const dataset1 = await Dataset.open({ alias: 'data-a' });
            const dataset2 = await Dataset.open({ alias: 'data-b' });
            expect(dataset1).not.toBe(dataset2);
            expect(dataset1.id).not.toBe(dataset2.id);
        });

        test('alias storage should be independent from default storage', async () => {
            const defaultDataset = await Dataset.open();
            const aliasDataset = await Dataset.open({ alias: 'other' });
            expect(defaultDataset).not.toBe(aliasDataset);
            expect(defaultDataset.id).not.toBe(aliasDataset.id);
        });

        test('should store and retrieve data independently per alias', async () => {
            const datasetA = await Dataset.open({ alias: 'store-a' });
            const datasetB = await Dataset.open({ alias: 'store-b' });

            await datasetA.pushData({ source: 'a' });
            await datasetB.pushData({ source: 'b' });

            await expect(datasetA.getData()).resolves.toMatchObject({ items: [{ source: 'a' }] });
            await expect(datasetB.getData()).resolves.toMatchObject({ items: [{ source: 'b' }] });
        });
    });

    describe('KeyValueStore.open with alias', () => {
        test('should open a KVS with an alias', async () => {
            const store = await KeyValueStore.open({ alias: 'my-store' });
            expect(store).toBeDefined();
            expect(store.id).toBeDefined();
            expect(store.name).toBeUndefined();
        });

        test('should return the same instance for the same alias', async () => {
            const store1 = await KeyValueStore.open({ alias: 'my-store' });
            const store2 = await KeyValueStore.open({ alias: 'my-store' });
            expect(store1).toBe(store2);
        });

        test('should store and retrieve values independently per alias', async () => {
            const storeA = await KeyValueStore.open({ alias: 'kvs-a' });
            const storeB = await KeyValueStore.open({ alias: 'kvs-b' });

            await storeA.setValue('key', 'value-a');
            await storeB.setValue('key', 'value-b');

            await expect(storeA.getValue('key')).resolves.toBe('value-a');
            await expect(storeB.getValue('key')).resolves.toBe('value-b');
        });
    });

    describe('RequestQueue.open with alias', () => {
        test('should return the same instance for the same alias', async () => {
            const queue1 = await RequestQueue.open({ alias: 'my-queue' });
            const queue2 = await RequestQueue.open({ alias: 'my-queue' });
            expect(queue1).toBe(queue2);
        });

        test('should return different instances for different aliases', async () => {
            const queue1 = await RequestQueue.open({ alias: 'queue-a' });
            const queue2 = await RequestQueue.open({ alias: 'queue-b' });
            expect(queue1).not.toBe(queue2);
            expect(queue1.id).not.toBe(queue2.id);
        });
    });

    describe('name/alias conflict detection', () => {
        test('should throw when opening alias that conflicts with existing named dataset', async () => {
            await Dataset.open({ name: 'shared-name' });
            await expect(Dataset.open({ alias: 'shared-name' })).rejects.toThrow(
                /Cannot open storage with alias "shared-name" because a named storage with the same identifier already exists/,
            );
        });

        test('should throw when opening named dataset that conflicts with existing alias', async () => {
            await Dataset.open({ alias: 'shared-name' });
            await expect(Dataset.open({ name: 'shared-name' })).rejects.toThrow(
                /Cannot open storage with name "shared-name" because an alias storage with the same identifier already exists\. If you meant to open the alias storage, use \{ alias: "shared-name" \} instead\./,
            );
        });

        test('should throw when opening alias that conflicts with existing named KVS', async () => {
            await KeyValueStore.open({ name: 'shared-kvs' });
            await expect(KeyValueStore.open({ alias: 'shared-kvs' })).rejects.toThrow(
                /Cannot open storage with alias "shared-kvs" because a named storage with the same identifier already exists/,
            );
        });

        test('should not conflict across different storage types', async () => {
            // A dataset name and a KVS alias with the same string should not conflict
            await Dataset.open({ name: 'cross-type' });
            const store = await KeyValueStore.open({ alias: 'cross-type' });
            expect(store).toBeDefined();
        });
    });

    describe('string identifier vs alias conflict', () => {
        test('should throw when opening a string identifier that matches an existing alias', async () => {
            await Dataset.open({ alias: 'asdf' });
            // 'asdf' as a bare string resolves to { name: 'asdf' }, which should conflict with alias 'asdf'
            await expect(Dataset.open('asdf')).rejects.toThrow(
                /Cannot open storage with name "asdf" because an alias storage with the same identifier already exists\. If you meant to open the alias storage, use \{ alias: "asdf" \} instead\./,
            );
        });

        test('should throw when opening an alias that matches an existing string-opened storage', async () => {
            await Dataset.open('asdf');
            await expect(Dataset.open({ alias: 'asdf' })).rejects.toThrow(
                /Cannot open storage with alias "asdf" because a named storage with the same identifier already exists/,
            );
        });
    });

    describe('string identifier vs alias conflict (persistent storage)', () => {
        const localStorageDir = resolve(import.meta.dirname, '..', 'tmp', 'fs-aliases', cryptoRandomObjectId(10));

        beforeEach(async () => {
            serviceLocator.reset();
            await ensureDir(localStorageDir);
            serviceLocator.setStorageBackend(new FileSystemStorageBackend({ localDataDirectory: localStorageDir }));
        });

        afterAll(async () => {
            await rm(localStorageDir, { force: true, recursive: true });
            serviceLocator.getStorageInstanceManager().clearCache();
        });

        test('should throw when opening a string identifier that matches an existing alias on disk', async () => {
            await Dataset.open({ alias: 'on-disk' });
            // With persistence, the directory 'on-disk' exists on disk. storageExists() should
            // not be fooled into treating the string as an ID.
            await expect(Dataset.open('on-disk')).rejects.toThrow(
                /Cannot open storage with name "on-disk" because an alias storage with the same identifier already exists\. If you meant to open the alias storage, use \{ alias: "on-disk" \} instead\./,
            );
        });

        test('legacy open(idOrName) re-opens the named storage on a subsequent run', async () => {
            const first = await Dataset.open('named-storage');
            await first.pushData({ run: 1 });

            // Simulate a fresh process: only the on-disk directory survives.
            serviceLocator.reset();
            const client = new FileSystemStorageBackend({ localDataDirectory: localStorageDir });
            serviceLocator.setStorageBackend(client);

            // 'named-storage' is the storage's name, not its id, so it must not be resolved as an id.
            await expect(client.storageExists('named-storage', 'Dataset')).resolves.toBe(false);

            const second = await Dataset.open('named-storage');
            expect(second.name).toBe('named-storage');
            await expect(second.getData()).resolves.toMatchObject({ items: [{ run: 1 }] });
        });

        test('a storage opened by name can be re-opened by its auto-assigned id after a reset', async () => {
            // The native storage always persists the auto-assigned id to disk (in `__metadata__.json`)
            // so it survives a reset. The directory is named after the storage's name, not its id.
            serviceLocator.reset();
            const firstClient = new FileSystemStorageBackend({
                localDataDirectory: localStorageDir,
            });
            serviceLocator.setStorageBackend(firstClient);

            const created = await Dataset.open('some-name');
            const assignedId = created.id;
            expect(assignedId).not.toBe('some-name');
            await created.pushData({ run: 1 });

            // Flush background metadata writes to disk (as a real process shutdown would).
            await firstClient.teardown();

            // Simulate a fresh process: only the on-disk directory survives.
            serviceLocator.reset();
            const client = new FileSystemStorageBackend({ localDataDirectory: localStorageDir });
            serviceLocator.setStorageBackend(client);

            // Opening by the persisted id must find the storage, even though its directory is named
            // after the name.
            await expect(client.storageExists(assignedId, 'Dataset')).resolves.toBe(true);

            const reopened = await Dataset.open(assignedId);
            expect(reopened.id).toBe(assignedId);
            expect(reopened.name).toBe('some-name');
            await expect(reopened.getData()).resolves.toMatchObject({ items: [{ run: 1 }] });
        });
    });

    describe('resolveStorageIdentifier', () => {
        test('null identifier opens default storage', async () => {
            const dataset1 = await Dataset.open(null);
            const dataset2 = await Dataset.open();
            expect(dataset1).toBe(dataset2);
        });

        test('undefined identifier opens default storage', async () => {
            const dataset1 = await Dataset.open(undefined);
            const dataset2 = await Dataset.open();
            expect(dataset1).toBe(dataset2);
        });

        test('empty object opens default storage', async () => {
            const dataset1 = await Dataset.open({});
            const dataset2 = await Dataset.open();
            expect(dataset1).toBe(dataset2);
        });

        test('the reserved __default__ alias opens default storage', async () => {
            const dataset1 = await Dataset.open({ alias: '__default__' });
            const dataset2 = await Dataset.open();
            expect(dataset1).toBe(dataset2);
        });

        test('string identifier opens named storage', async () => {
            const dataset = await Dataset.open('test-named');
            expect(dataset.name).toBe('test-named');
        });

        test('{ name } identifier opens named storage', async () => {
            const dataset = await Dataset.open({ name: 'test-named-obj' });
            expect(dataset.name).toBe('test-named-obj');
        });

        test('{ alias } identifier opens alias storage', async () => {
            const dataset = await Dataset.open({ alias: 'test-alias' });
            expect(dataset.name).toBeUndefined();
        });

        describe('at the backend level', () => {
            const localStorageDir = resolve(import.meta.dirname, '..', 'tmp', 'fs-aliases', cryptoRandomObjectId(10));

            afterAll(async () => {
                await rm(localStorageDir, { force: true, recursive: true });
            });

            test.each([
                ['MemoryStorageBackend', (): StorageBackend => new MemoryStorageBackend()],
                [
                    'FileSystemStorageBackend',
                    (): StorageBackend => new FileSystemStorageBackend({ localDataDirectory: localStorageDir }),
                ],
            ])(
                '%s opens the same default storage for the reserved alias and for no identifier',
                async (_backendName, createBackend) => {
                    const backend = createBackend();

                    const aliased = await backend.createDatasetBackend({ alias: '__default__' });

                    expect(await backend.createDatasetBackend({})).toBe(aliased);
                    expect(await backend.createDatasetBackend()).toBe(aliased);
                },
            );
        });
    });

    describe('drop with alias', () => {
        test('should be able to drop an aliased dataset and re-open it', async () => {
            const dataset1 = await Dataset.open({ alias: 'droppable' });
            await dataset1.pushData({ foo: 'bar' });
            await dataset1.drop();

            const dataset2 = await Dataset.open({ alias: 'droppable' });
            expect(dataset2).not.toBe(dataset1);
            await expect(dataset2.getData()).resolves.toMatchObject({ items: [] });
        });
    });

    describe('concurrent alias opens', () => {
        test('should handle concurrent opens of the same alias', async () => {
            const [d1, d2, d3] = await Promise.all([
                Dataset.open({ alias: 'concurrent' }),
                Dataset.open({ alias: 'concurrent' }),
                Dataset.open({ alias: 'concurrent' }),
            ]);
            expect(d1).toBe(d2);
            expect(d2).toBe(d3);
        });
    });
});