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.

Using jsonStringifyExtended() for stringification

Marek Trunkat51ad4911f9e8
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6292dd1bc..7d184f8f0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,9 @@
+xxxxxxxxxxxxxxxxxxx
+===================
+- Stringification to JSON of actor input in `Apify.call()`, `Apify.callTask()` and `Apify.metamorph()`
+ now also supports functions via `func.toString()`. The same holds for record body in `setValue()`
+ method of key-value store.
+
0.14.5 / 2019-05-06
===================
- Update Puppeteer to 1.15.0.
@@ -70,7 +76,7 @@
- **BREAKING CHANGE:** Added `handleRequestTimeoutSecs` option to `BasicCrawler` with a 60 second default.
- **DEPRECATED:** `PuppeteerPool` options in the `PuppeteerCrawler` constructor are now deprecated.
Please use the new `puppeteerPoolOptions` argument of type `Object` to pass them. `launchPuppeteerFunction`
- and `launchPuppeteerOptions` are still available as shortcuts for convenience.
+ and `launchPuppeteerOptions` are still available as shortcuts for convenience.
- `CheerioCrawler` and `PuppeteerCrawler` now automatically set `handleRequestTimeoutSecs` to 10 times
their `handlePageTimeoutSecs`. This is a precaution that should keep requests from hanging forever.
- Added `options.prepareRequestFunction()` to `CheerioCrawler` constructor to enable modification
diff --git a/package.json b/package.json
index 117fbdcce..49f0c08c8 100644
--- a/package.json
+++ b/package.json
@@ -51,7 +51,7 @@
"dependencies": {
"@apify/ps-tree": "^1.1.3",
"apify-client": "^0.5.14",
- "apify-shared": "^0.1.41",
+ "apify-shared": "^0.1.43",
"cheerio": "^1.0.0-rc.2",
"content-type": "^1.0.4",
"express": "^4.16.4",
@@ -61,10 +61,10 @@
"jquery": "^3.3.1",
"mime": "^2.3.1",
"proxy-chain": "^0.2.5",
- "socket.io": "^2.2.0",
"request": "^2.88.0",
"request-promise-native": "^1.0.5",
"rimraf": "^2.6.2",
+ "socket.io": "^2.2.0",
"underscore": "^1.9.1",
"ws": "^6.1.2",
"xregexp": "^4.2.4"
diff --git a/src/key_value_store.js b/src/key_value_store.js
index 78a1e17ec..76ae52fec 100644
--- a/src/key_value_store.js
+++ b/src/key_value_store.js
@@ -6,6 +6,7 @@ import mime from 'mime';
import LruCache from 'apify-shared/lru_cache';
import { KEY_VALUE_STORE_KEY_REGEX } from 'apify-shared/regexs';
import { ENV_VARS, LOCAL_STORAGE_SUBDIRS, KEY_VALUE_STORE_KEYS } from 'apify-shared/consts';
+import { jsonStringifyExtended } from 'apify-shared/utilities';
import { checkParamOrThrow, parseBody } from 'apify-client/build/utils';
import {
addCharsetToContentType, apifyClient, ensureDirExists, openRemoteStorage, openLocalStorage, ensureTokenOrLocalStorageEnvExists,
@@ -76,7 +77,7 @@ export const maybeStringify = (value, options) => {
try {
// Format JSON to simplify debugging, the overheads with compression is negligible
- value = JSON.stringify(value, null, 2);
+ value = jsonStringifyExtended(value, null, 2);
} catch (e) {
// Give more meaningful error message
if (e.message && e.message.indexOf('Invalid string length') >= 0) {
@@ -87,7 +88,7 @@ export const maybeStringify = (value, options) => {
if (value === undefined) {
throw new Error('The "value" parameter was stringified to JSON and returned undefined. '
- + 'Make sure you\'re not trying to stringify a Function.');
+ + 'Make sure you\'re not trying to stringify an undefined value.');
}
}
diff --git a/test/actor.js b/test/actor.js
index 3aee11d90..b62774523 100644
--- a/test/actor.js
+++ b/test/actor.js
@@ -1001,11 +1001,11 @@ describe('Apify.metamorph()', () => {
actsMock.restore();
});
- it('stringifies to JSON', async () => {
+ it('stringifies to JSON including functions', async () => {
const runId = 'some-run-id';
const actorId = 'some-actor-id';
const targetActorId = 'some-target-actor-id';
- const input = { foo: 'bar' };
+ const input = { foo: 'bar', func: () => { return 123; } };
process.env[ENV_VARS.ACTOR_ID] = actorId;
process.env[ENV_VARS.ACTOR_RUN_ID] = runId;
@@ -1016,7 +1016,10 @@ describe('Apify.metamorph()', () => {
runId,
actId: actorId,
targetActorId,
- body: JSON.stringify(input, null, 2),
+ body: `{
+ "foo": "bar",
+ "func": "() => {\\n return 123;\\n }"
+}`,
contentType: 'application/json; charset=utf-8',
build: undefined,
})
diff --git a/test/key_value_store.js b/test/key_value_store.js
index 0e351bfbe..4b6493748 100644
--- a/test/key_value_store.js
+++ b/test/key_value_store.js
@@ -230,10 +230,9 @@ describe('KeyValueStore', () => {
circularObj.xxx = circularObj;
const circularErrMsg = 'The "value" parameter cannot be stringified to JSON: Converting circular structure to JSON';
const undefinedErrMsg = 'The "value" parameter was stringified to JSON and returned undefined. '
- + 'Make sure you\'re not trying to stringify a Function.';
+ + 'Make sure you\'re not trying to stringify an undefined value.';
await expect(Apify.setValue('key', circularObj)).to.be.rejectedWith(circularErrMsg);
await expect(Apify.setValue('key', undefined)).to.be.rejectedWith(undefinedErrMsg);
- await expect(Apify.setValue('key', () => {})).to.be.rejectedWith(undefinedErrMsg);
await expect(Apify.setValue('key')).to.be.rejectedWith(undefinedErrMsg);
const contTypeRedundantErrMsg = 'The "options.contentType" parameter must not be used when removing the record';