Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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);
});
});
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);
});
});
it('throws if APIFY_DEFAULT_KEY_VALUE_STORE_ID env var is not defined and we use cloud storage', async () => {
delete process.env[ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID];
delete process.env[ENV_VARS.LOCAL_STORAGE_DIR];
process.env[ENV_VARS.TOKEN] = 'xxx';
const errMsg = 'The \'APIFY_DEFAULT_KEY_VALUE_STORE_ID\' environment variable is not defined';
await expect(Apify.setValue('KEY', {})).to.be.rejectedWith(errMsg);
delete process.env[ENV_VARS.TOKEN];
});
it('throws on invalid args', async () => {
process.env[ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID] = '1234';
process.env[ENV_VARS.LOCAL_STORAGE_DIR] = LOCAL_STORAGE_DIR;
await expect(Apify.getValue()).to.be.rejectedWith('Parameter "key" of type String must be provided');
await expect(Apify.getValue({})).to.be.rejectedWith('Parameter "key" of type String must be provided');
await expect(Apify.getValue('')).to.be.rejectedWith('The "key" parameter cannot be empty');
await expect(Apify.getValue(null)).to.be.rejectedWith('Parameter "key" of type String must be provided');
delete process.env[ENV_VARS.LOCAL_STORAGE_DIR];
});
import { cryptoRandomObjectId } from 'apify-shared/utilities';
import { LOCAL_STORAGE_SUBDIRS, LOCAL_ENV_VARS, ENV_VARS } from 'apify-shared/consts';
import fs from 'fs-extra';
import path from 'path';
import log from 'apify-shared/log';
import { LOCAL_STORAGE_DIR } from './_helper';
const DEFAULT_FOLDERS = Object.values(LOCAL_STORAGE_SUBDIRS)
.concat([
`${LOCAL_STORAGE_SUBDIRS.keyValueStores}/${LOCAL_ENV_VARS[ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID]}`,
'live_view',
]);
/**
* Emulates storage for testing purposes.
* Creates an unique folder with default structure.
* This class should be used in all tests that are using the storage.
*
* Basic usage: Create and initialize `LocalStorageDirEmulator` in beforeAll hook,
* call `clean()` in afterEach hook and finally call `destroy()` in afterAll hook.
*/
class LocalStorageDirEmulator {
constructor(localStorageDir = cryptoRandomObjectId(10)) {
this.localStorageDir = path.join(LOCAL_STORAGE_DIR, localStorageDir);
log.debug(`Created local storage emulation in folder ${this.localStorageDir}`);
}
const getLocalKeyValueStorePath = (storeId) => {
const envVar = ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID;
const storeDir = storeId || process.env[envVar] || LOCAL_ENV_VARS[envVar];
return path.join(getLocalStorageDir(), LOCAL_STORAGE_SUBDIRS.keyValueStores, storeDir);
};
const getLocalDatasetPath = (storeId) => {
export const openKeyValueStore = (storeIdOrName, options = {}) => {
checkParamOrThrow(storeIdOrName, 'storeIdOrName', 'Maybe String');
checkParamOrThrow(options, 'options', 'Object');
ensureTokenOrLocalStorageEnvExists('key value store');
const { forceCloud = false } = options;
checkParamOrThrow(forceCloud, 'options.forceCloud', 'Boolean');
return process.env[ENV_VARS.LOCAL_STORAGE_DIR] && !forceCloud
? openLocalStorage(storeIdOrName, ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID, KeyValueStoreLocal, storesCache)
: openRemoteStorage(storeIdOrName, ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID, KeyValueStore, storesCache, getOrCreateKeyValueStore);
};
export const openKeyValueStore = (storeIdOrName, options = {}) => {
checkParamOrThrow(storeIdOrName, 'storeIdOrName', 'Maybe String');
checkParamOrThrow(options, 'options', 'Object');
ensureTokenOrLocalStorageEnvExists('key value store');
const { forceCloud = false } = options;
checkParamOrThrow(forceCloud, 'options.forceCloud', 'Boolean');
return process.env[ENV_VARS.LOCAL_STORAGE_DIR] && !forceCloud
? openLocalStorage(storeIdOrName, ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID, KeyValueStoreLocal, storesCache)
: openRemoteStorage(storeIdOrName, ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID, KeyValueStore, storesCache, getOrCreateKeyValueStore);
};
build: 'latest',
timeoutSecs: 0,
memoryMbytes: 2048,
},
},
};
exports.ACTS_TEMPLATE_LIST = Object.keys(exports.ACTS_TEMPLATES);
exports.DEFAULT_ACT_TEMPLATE = 'basic';
exports.DEFAULT_LOCAL_STORES_ID = 'default';
exports.LOCAL_ENV_VARS = {
[ENV_VARS.LOCAL_EMULATION_DIR]: DEFAULT_LOCAL_EMULATION_DIR,
[ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID]: exports.DEFAULT_LOCAL_STORES_ID,
[ENV_VARS.DEFAULT_DATASET_ID]: exports.DEFAULT_LOCAL_STORES_ID,
[ENV_VARS.DEFAULT_REQUEST_QUEUE_ID]: exports.DEFAULT_LOCAL_STORES_ID,
[ENV_VARS.PROXY_HOSTNAME]: DEFAULT_PROXY_HOSTNAME,
[ENV_VARS.PROXY_PORT]: DEFAULT_PROXY_PORT.toString(),
};
exports.EMPTY_LOCAL_CONFIG = {
name: null,
actId: null,
version: {
versionNumber: '0.1',
buildTag: 'latest',
envVars: [],
sourceType: 'TARBALL',
tarballUrl: null,
},