Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('getInfo() should work', async () => {
const datasetName = 'stats-dataset';
const dataset = new DatasetLocal(datasetName, LOCAL_STORAGE_DIR);
await Apify.utils.sleep(2);
// Save orig env var since it persists over tests.
const originalUserId = process.env[ENV_VARS.USER_ID];
// Try empty ID
delete process.env[ENV_VARS.USER_ID];
let info = await dataset.getInfo();
expect(info).to.be.an('object');
expect(info.id).to.be.eql(datasetName);
expect(info.name).to.be.eql(datasetName);
expect(info.userId).to.be.eql(null);
expect(info.itemCount).to.be.eql(0);
expect(info.cleanItemCount).to.be.eql(0);
const cTime = info.createdAt.getTime();
let mTime = info.modifiedAt.getTime();
expect(cTime).to.be.below(Date.now() + 1);
expect(cTime).to.be.eql(mTime);
await dataset.pushData([
expect(info).to.be.an('object');
expect(info.id).to.be.eql(datasetName);
expect(info.name).to.be.eql(datasetName);
expect(info.userId).to.be.eql(userId);
expect(info.itemCount).to.be.eql(4);
expect(info.cleanItemCount).to.be.eql(4);
const cTime2 = info.createdAt.getTime();
mTime = info.modifiedAt.getTime();
aTime = info.accessedAt.getTime();
expect(cTime).to.be.eql(cTime2);
expect(mTime).to.be.below(aTime);
expect(mTime).to.be.below(now);
expect(aTime).to.be.below(now);
// Restore.
delete process.env[ENV_VARS.USER_ID];
if (originalUserId) process.env[ENV_VARS.USER_ID] = originalUserId;
});
export const newClient = () => {
const opts = {
userId: process.env[ENV_VARS.USER_ID] || null,
token: process.env[ENV_VARS.TOKEN] || null,
};
// Only set baseUrl if overridden by env var, so that 'https://api.apify.com' is used by default.
// This simplifies local development, which should run against production unless user wants otherwise.
const apiBaseUrl = process.env[ENV_VARS.API_BASE_URL];
if (apiBaseUrl) opts.baseUrl = apiBaseUrl;
return new ApifyClient(opts);
};
// Check if apify storage were purge, if not print error
if (!flags.purge) {
const isStorageEmpty = await checkIfStorageIsEmpty();
if (!isStorageEmpty) {
warning('The apify_storage directory contains a previous state, the actor will continue where it left off. '
+ 'To start from the initial state, use --purge parameter to clean the apify_storage directory.');
}
}
// Attach env vars from local config files
const localEnvVars = {
[ENV_VARS.LOCAL_STORAGE_DIR]: DEFAULT_LOCAL_STORAGE_DIR,
};
if (proxy && proxy.password) localEnvVars[ENV_VARS.PROXY_PASSWORD] = proxy.password;
if (userId) localEnvVars[ENV_VARS.USER_ID] = userId;
if (token) localEnvVars[ENV_VARS.TOKEN] = token;
if (localConfig.env) {
const updatedEnv = replaceSecretsValue(localConfig.env);
Object.assign(localEnvVars, updatedEnv);
}
// NOTE: User can overwrite env vars
const env = Object.assign(localEnvVars, process.env);
env.NODE_OPTIONS = env.NODE_OPTIONS ? `${env.NODE_OPTIONS} --max-http-header-size=80000` : '--max-http-header-size=80000';
if (!userId) {
warning('You are not logged in with your Apify Account. Some features like Apify Proxy will not work. Call "apify login" to fix that.');
}
// --max-http-header-size=80000
// Increases default size of headers. The original limit was 80kb, but from node 10+ they decided to lower it to 8kb.
// However they did not think about all the sites there with large headers,
async getInfo() {
await this.initializationPromise;
const id = this.datasetId;
const name = id === ENV_VARS.DEFAULT_DATASET_ID ? null : id;
const result = {
id,
name,
userId: process.env[ENV_VARS.USER_ID] || null,
createdAt: this.createdAt,
modifiedAt: this.modifiedAt,
accessedAt: this.accessedAt,
itemCount: this.counter,
// TODO: This number is not counted correctly!
cleanItemCount: this.counter,
};
this._updateMetadata();
return result;
}
it('reads environment variables correctly', () => {
process.env[ENV_VARS.API_BASE_URL] = 'http://www.example.com:1234/path/';
process.env[ENV_VARS.USER_ID] = 'userId';
process.env[ENV_VARS.TOKEN] = 'token';
const client = utils.newClient();
expect(client.constructor.name).to.eql('ApifyClient');
const opts = client.getOptions();
expect(opts.userId).to.eql('userId');
expect(opts.token).to.eql('token');
expect(opts.baseUrl).to.eql('http://www.example.com:1234/path/');
});
it('uses correct default if APIFY_API_BASE_URL is not defined', () => {
delete process.env[ENV_VARS.API_BASE_URL];
process.env[ENV_VARS.USER_ID] = 'userId';
process.env[ENV_VARS.TOKEN] = 'token';
const client = utils.newClient();
const opts = client.getOptions();
expect(opts.userId).to.eql('userId');
expect(opts.token).to.eql('token');
expect(opts.baseUrl).to.eql('https://api.apify.com');
});
});
});
`;
fs.writeFileSync('main.js', actCode, { flag: 'w' });
const apifyJson = loadJson.sync('apify.json');
apifyJson.env = testEnvVars;
writeJson.sync('apify.json', apifyJson);
await command.run(['run']);
const actOutputPath = path.join(getLocalKeyValueStorePath(), 'OUTPUT.json');
const localEnvVars = loadJson.sync(actOutputPath);
const auth = loadJson.sync(AUTH_FILE_PATH);
expect(localEnvVars[ENV_VARS.PROXY_PASSWORD]).to.be.eql(auth.proxy.password);
expect(localEnvVars[ENV_VARS.USER_ID]).to.be.eql(auth.id);
expect(localEnvVars[ENV_VARS.TOKEN]).to.be.eql(auth.token);
expect(localEnvVars.TEST_LOCAL).to.be.eql(testEnvVars.TEST_LOCAL);
await command.run(['logout']);
});
async getInfo() {
await this.initializationPromise;
const id = this.queueId;
const name = id === ENV_VARS.DEFAULT_REQUEST_QUEUE_ID ? null : id;
const result = {
id,
name,
userId: process.env[ENV_VARS.USER_ID] || null,
createdAt: this.createdAt,
modifiedAt: this.modifiedAt,
accessedAt: this.accessedAt,
totalRequestCount: this._handledCount + this.pendingCount,
handledRequestCount: this._handledCount,
pendingRequestCount: this.pendingCount,
};
this._updateMetadata();
return result;
}