[GH-ISSUE #1420] Invalid Promise object in Script runner breaks require of full lodash suite #1413

Open
opened 2026-03-03 19:51:17 +03:00 by kerem · 14 comments
Owner

Originally created by @rolfb on GitHub (Oct 27, 2022).
Original GitHub issue: https://github.com/ProxymanApp/Proxyman/issues/1420

Originally assigned to: @NghiaTranUIT on GitHub.

Description

The Promise object in the Script runner does not support Promise.resolve.
The solution is either to remove the invalid Promise object or implement full support.
Uncomment the code in the example script below to watch it run correctly.

Steps to Reproduce

  1. Download the full package of lodash to user scripts location:
curl https://raw.githubusercontent.com/lodash/lodash/4.17.15-npm/lodash.min.js \
> ~/Library/Application\ Support/com.proxyman.NSProxy/users/lodash.min.js
  1. Create a script which uses lodash:
/*
    Uncomment the next line to make it work as a quick fix
*/
// delete(Promise);

const lodash = require("@users/lodash.min.js")
const object = { 'a': [{ 'b': { 'c': 3 } }] };
 
console.log(lodash.get(object, 'a[0].b.c'));
  1. Make a trigger and trigger it

Current Behavior

Lodash fails to load because Promise.resolve doesn't exist with error: "[...].resolve is not a function"

Expected Behavior

It should have executed and logged the value 3 to the console

Environment

  • App version: Version 3.12.0 (31200
  • macOS version: macOS Monterey
Originally created by @rolfb on GitHub (Oct 27, 2022). Original GitHub issue: https://github.com/ProxymanApp/Proxyman/issues/1420 Originally assigned to: @NghiaTranUIT on GitHub. ## Description The Promise object in the Script runner does not support Promise.resolve. The solution is either to remove the invalid Promise object or implement full support. Uncomment the code in the example script below to watch it run correctly. ## Steps to Reproduce <!-- Add relevant code and/or a live example --> 1. Download the full package of lodash to user scripts location: ```sh curl https://raw.githubusercontent.com/lodash/lodash/4.17.15-npm/lodash.min.js \ > ~/Library/Application\ Support/com.proxyman.NSProxy/users/lodash.min.js ``` 2. Create a script which uses lodash: ```javascript /* Uncomment the next line to make it work as a quick fix */ // delete(Promise); const lodash = require("@users/lodash.min.js") const object = { 'a': [{ 'b': { 'c': 3 } }] }; console.log(lodash.get(object, 'a[0].b.c')); ``` 3. Make a trigger and trigger it ## Current Behavior Lodash fails to load because Promise.resolve doesn't exist with error: "[...].resolve is not a function" ## Expected Behavior It should have executed and logged the value `3` to the console ## Environment - App version: Version 3.12.0 (31200 - macOS version: macOS Monterey
Author
Owner

@NghiaTranUIT commented on GitHub (Oct 27, 2022):

Not sure if you use async on the script? async/await has been supported on the latest Proxyman.

Ref: https://docs.proxyman.io/scripting/async-await-request

The lodash library has been bundled to the app too. Ref: https://docs.proxyman.io/scripting/built-in-js-libraries

You can simply use it:

const Lodash = require('@libs/lodash.js');

As far as I remember, I need to modify the lib to make it works with the Scripting. It's built on top of JavaScriptCore framework from Apple, so it might not fully work like the JS env on the Web Browser.

<!-- gh-comment-id:1293593268 --> @NghiaTranUIT commented on GitHub (Oct 27, 2022): Not sure if you use `async` on the script? async/await has been supported on the latest Proxyman. Ref: https://docs.proxyman.io/scripting/async-await-request The `lodash` library has been bundled to the app too. Ref: https://docs.proxyman.io/scripting/built-in-js-libraries You can simply use it: ```js const Lodash = require('@libs/lodash.js'); ``` As far as I remember, I need to modify the lib to make it works with the Scripting. It's built on top of JavaScriptCore framework from Apple, so it might not fully work like the JS env on the Web Browser.
Author
Owner

@rolfb commented on GitHub (Oct 27, 2022):

The built-in version of lodash is a subset (just core) and missing a lot. An example is _.get, but there are plenty more.

The example reproduction script does not use any async/await features. The problem lies with libraries that checks if Promise exists and then tries to wrap it internally such as lodash utility functions, and the Proxyman Script Runner provides a faked or functionally impaired Promise object which fools guard clauses.

<!-- gh-comment-id:1293646167 --> @rolfb commented on GitHub (Oct 27, 2022): The built-in version of lodash is a subset (just core) and missing a lot. An example is `_.get`, but there are plenty more. The example reproduction script does not use any async/await features. The problem lies with libraries that checks if Promise exists and then tries to wrap it internally such as lodash utility functions, and the Proxyman Script Runner provides a faked or functionally impaired Promise object which fools guard clauses.
Author
Owner

@rolfb commented on GitHub (Oct 27, 2022):

@NghiaTranUIT isn't all of the Promise API supported by JavascriptCore by default?

<!-- gh-comment-id:1293718700 --> @rolfb commented on GitHub (Oct 27, 2022): @NghiaTranUIT isn't all of the Promise API supported by JavascriptCore by default?
Author
Owner

@NghiaTranUIT commented on GitHub (Oct 28, 2022):

It supports Promise, but not fully. From what I experience, I have to implement methods to make Promise work.

<!-- gh-comment-id:1294286329 --> @NghiaTranUIT commented on GitHub (Oct 28, 2022): It supports Promise, but not fully. From what I experience, I have to implement methods to make Promise work.
Author
Owner

@rolfb commented on GitHub (Oct 28, 2022):

I propose either to remove the Promise object or make Promise.resolve work to avoid confusion.
Here's an example snippet you can run and re-run to verify:

const promise1 = Promise.resolve(123);

promise1.then((value) => {
  console.log(value);
  // expected output: 123
});

By the way, forgot to mention I really like the app. Thank you :)

<!-- gh-comment-id:1294540566 --> @rolfb commented on GitHub (Oct 28, 2022): I propose either to remove the Promise object or make Promise.resolve work to avoid confusion. Here's an example snippet you can run and re-run to verify: ```javascript const promise1 = Promise.resolve(123); promise1.then((value) => { console.log(value); // expected output: 123 }); ``` By the way, forgot to mention I really like the app. Thank you :)
Author
Owner

@NghiaTranUIT commented on GitHub (Oct 29, 2022):

Thanks. I will look into it. The reason why we introduce the Customize Promise is that we've implemented the await $http.get(). JavascriptCore doesn't work well with the JS Promise mechanism. (Or maybe I'm doing wrong 🧐)

<!-- gh-comment-id:1295683893 --> @NghiaTranUIT commented on GitHub (Oct 29, 2022): Thanks. I will look into it. The reason why we introduce the Customize Promise is that we've implemented the `await $http.get()`. JavascriptCore doesn't work well with the JS Promise mechanism. (Or maybe I'm doing wrong 🧐)
Author
Owner

@rolfb commented on GitHub (Oct 31, 2022):

I'm not sure, but Promise.resolve is available in my local jsc helper so the code example below works.

On a Mac you can launch it using this command in the terminal:
/System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Helpers/jsc

const promise1 = Promise.resolve(123);

promise1.then((value) => {
  print(value);
  // expected output: 123
});
<!-- gh-comment-id:1296790047 --> @rolfb commented on GitHub (Oct 31, 2022): I'm not sure, but Promise.resolve is available in my local `jsc` helper so the code example below works. On a Mac you can launch it using this command in the terminal: `/System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Helpers/jsc` ```javascript const promise1 = Promise.resolve(123); promise1.then((value) => { print(value); // expected output: 123 }); ```
Author
Owner

@StanislavMayorov commented on GitHub (Dec 16, 2023):

@NghiaTranUIT HI. It's very confusing that build-in lodash contains only 10 of 200 functions. Could you fix it and add full lodash please?
Current lodash version Build: lodash include="camelCase,deburr,escapeRegExp,kebabCase,snakeCase,startCase,". Proxyman version 4.15.0.

<!-- gh-comment-id:1858861677 --> @StanislavMayorov commented on GitHub (Dec 16, 2023): @NghiaTranUIT HI. It's very confusing that build-in lodash contains only 10 of 200 functions. Could you fix it and add full lodash please? Current lodash version `Build: lodash include="camelCase,deburr,escapeRegExp,kebabCase,snakeCase,startCase,"`. Proxyman version 4.15.0.
Author
Owner

@NghiaTranUIT commented on GitHub (Dec 17, 2023):

@StanislavMayorov let's me update to the latest lodash and share with you a Beta build 👍

<!-- gh-comment-id:1859009521 --> @NghiaTranUIT commented on GitHub (Dec 17, 2023): @StanislavMayorov let's me update to the latest lodash and share with you a Beta build 👍
Author
Owner

@StanislavMayorov commented on GitHub (Dec 17, 2023):

@NghiaTranUIT many thanks!

<!-- gh-comment-id:1859159814 --> @StanislavMayorov commented on GitHub (Dec 17, 2023): @NghiaTranUIT many thanks!
Author
Owner

@NghiaTranUIT commented on GitHub (Dec 18, 2023):

cc @StanislavMayorov : You can try this Beta build
cc @rolfb : Sorry that this bug takes so long to fix. It's finally fixed on the Beta build.

Changelog

How to use

  1. As we're using the Beta build, we have to Delete the current lodash.js first at ~/Library/Application Support/com.proxyman.NSProxy/addons/libs/lodash.js (On the production build, this step is not required)
  2. Download Beta: https://download.proxyman.io/beta/Proxyman_4.15.0_Update_Lodash_and_fix_Promise.dmg
  3. Done

Here is the script I tested:

const Lodash = require('@libs/lodash.js');

async function onRequest(context, url, request) {
  console.log(Lodash.camelCase("proxyman llc"));
  console.log(Lodash.takeRight([1, 2, 3]))

  // Done
  return request;
}
<!-- gh-comment-id:1859510566 --> @NghiaTranUIT commented on GitHub (Dec 18, 2023): cc @StanislavMayorov : You can try this Beta build cc @rolfb : Sorry that this bug takes so long to fix. It's finally fixed on the Beta build. ### Changelog - Fix the Promise issue (from @rolfb ) (https://github.com/ProxymanApp/Proxyman/issues/1887) - Update to the latest Lodash lib ### How to use 1. As we're using the Beta build, we have to Delete the current `lodash.js` first at `~/Library/Application Support/com.proxyman.NSProxy/addons/libs/lodash.js` (On the production build, this step is not required) 2. Download Beta: https://download.proxyman.io/beta/Proxyman_4.15.0_Update_Lodash_and_fix_Promise.dmg 3. Done Here is the script I tested: ```js const Lodash = require('@libs/lodash.js'); async function onRequest(context, url, request) { console.log(Lodash.camelCase("proxyman llc")); console.log(Lodash.takeRight([1, 2, 3])) // Done return request; } ```
Author
Owner

@StanislavMayorov commented on GitHub (Dec 18, 2023):

@NghiaTranUIT great! thank you. Now _.get and _.cloneDeep works.

<!-- gh-comment-id:1861404483 --> @StanislavMayorov commented on GitHub (Dec 18, 2023): @NghiaTranUIT great! thank you. Now `_.get` and `_.cloneDeep` works.
Author
Owner

@StanislavMayorov commented on GitHub (Dec 18, 2023):

I don't know if it's for purpose or not in the docs const Lodash = require('@libs/lodash.js'), but common usage is underscore for lodash const _ = require('@libs/lodash.js').

<!-- gh-comment-id:1861417540 --> @StanislavMayorov commented on GitHub (Dec 18, 2023): I don't know if it's for purpose or not in the [docs](https://docs.proxyman.io/scripting/built-in-js-libraries#3.-how-to-import-my-own-js-library) `const Lodash = require('@libs/lodash.js')`, but common usage is underscore for lodash `const _ = require('@libs/lodash.js')`.
Author
Owner

@NghiaTranUIT commented on GitHub (Dec 19, 2023):

Awesome. Glad to know it works 😄

You're right, I will update the docs to use the _ 👍

<!-- gh-comment-id:1861923979 --> @NghiaTranUIT commented on GitHub (Dec 19, 2023): Awesome. Glad to know it works 😄 You're right, I will update the docs to use the `_` 👍
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/Proxyman#1413
No description provided.