Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/utilities/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './parse_jsonl_stream';
export * from './streams_utilities';
export * from './webhook_payload_template';
export * from './crypto';
export * from './url_params_utils';
30 changes: 30 additions & 0 deletions packages/utilities/src/url_params_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Encodes object (e.g. input for actor) to a string hash.
*/
export function encodeInput<T extends object>(input: T) {
const data = JSON.stringify(input);
const buffer = Buffer.from(data, 'utf8');

return buffer.toString('base64url');
}

/**
* Decodes a string hash produced via `encodeInput` back into the original object.
*/
export function decodeInput(urlHash: string) {
const buffer = Buffer.from(urlHash, 'base64url');
const decoded = buffer.toString('utf8');

return JSON.parse(decoded);
}

/**
* Extract import statements from the code.
*/
export function separateImports(code: string): { code: string; imports: string } {
const lines = code.split('\n');
return {
code: lines.filter((line) => !line.trim().startsWith('import')).join('\n'),
imports: lines.filter((line) => line.trim().startsWith('import')).join('\n'),
};
}
48 changes: 48 additions & 0 deletions test/url-params-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { encodeInput, decodeInput, separateImports } from '@apify/utilities';

const input = {
code: `import { PlaywrightCrawler, Dataset } from 'crawlee';

// PlaywrightCrawler crawls the web using a headless
// browser controlled by the Playwright library.
const crawler = new PlaywrightCrawler({
// Use the requestHandler to process each of the crawled pages.
async requestHandler({ request, page, enqueueLinks, log }) {
const title = await page.title();
log.info(\`Title of \${request.loadedUrl} is '\${title}'\`);

// Save results as JSON to ./storage/datasets/default
await Dataset.pushData({ title, url: request.loadedUrl });

// Extract links from the current page
// and add them to the crawling queue.
await enqueueLinks();
},
// Uncomment this option to see the browser window.
// headless: false,
});

// Add first URL to the queue and start the crawl.
await crawler.run(['https://crawlee.dev']);`,
};

test('encode/decode', async () => {
const hash = encodeInput(input);
const decoded = decodeInput(hash);

expect(input).toEqual(decoded);
});

test('import extraction', async () => {
const { code, imports } = separateImports(input.code);
const codeLines = code.split('\n');
const importLines = imports.split('\n');

for (const line of codeLines) {
expect(line).not.toMatch(/^import/);
}

for (const line of importLines) {
expect(line).toMatch(/^import/);
}
});