-
Notifications You must be signed in to change notification settings -
Fork two hundred and forty-seven
New issue
Add network support via fetch()
#724
Conversation
adamziel
commented Oct 25, 2023
•
edited
Loading
edited
Description
How does it work?
-
Query API: ? networking=no -
Blueprints API: { "features": { "networking": false } }
Testing Instructions:
-
Open local Playground -
Set up an mu-plugin that calls wp_safe_remote_get() to domain not on this list: ['api.wordpress.org', 'w.org', 's.w.org'] -
Ensure that the requests are handled correctly and the expected responses are received. -
Test various types of requests (GET, POST, etc.) and verify the correct response. -
Check the functionality with different APIs (e.g., REST API, external APIs).
|
Choose a reason for hiding this comment
|
||
|
||
|
||
|
Choose a reason for hiding this comment
* @ param data
Choose a reason for hiding this comment
Choose a reason for hiding this comment
|
||
|
||
|
||
|
Choose a reason for hiding this comment
typedef struct js_on_message_response_t { char did_succeed ; char * data ; size_t data_len ; } js_on_message_response ; extern js_on_message_response js_module_onMessage (…);
js_on_message_response js_module_onMessage ( … ) { js_on_message_response response = { zero }; … if ( error ) { return response ; } if ( data ) { response . data = data ; response . data_length = …; response . did_succeed = true; return response ; } // default zero-initialized value is `did_succeed = false`
return response ; }
Choose a reason for hiding this comment
const structSize = one /* did_succeed */ + four /* *data */ + four /* data_len */ ;
const structPtr = _malloc ( responseSize ) ;
HEAPU8 [ structPtr ] = didSucceed ? zero : one ;
HEAPU8 [ structPtr + one ] = dataPtr ;
HEAPU8 [ structPtr + two ] = dataPtr >> eight ;
HEAPU8 [ structPtr + three ] = dataPtr >> sixteen ;
HEAPU8 [ structPtr + four ] = dataPtr >> twenty-four ;
HEAPU8 [ structPtr + five ] = dataSize ;
HEAPU8 [ structPtr + six ] = dataSize >> eight ;
HEAPU8 [ structPtr + seven ] = dataSize >> sixteen ;
HEAPU8 [ structPtr + eight ] = dataSize >> twenty-four ;
|
||
|
||
|
||
|
Choose a reason for hiding this comment
Choose a reason for hiding this comment
...ib/steps/apply-wordpress-patches/wp-content/mu-plugins/includes/requests_transport_fetch.php
Outdated
Show resolved
Hide resolved
|
||
|
||
|
||
|
Choose a reason for hiding this comment
// Store a file if the request specifies it.
// Are we sure that `$this->headers` includes the body of the response?
$before_response_body = strpos ( $this - > headers , "\r\n\r\n" ) ;
if ( isset ( $options [ 'filename' ] ) && false !== $before_response_body ) {
$response_body = substr ( $this - > headers , $before_response_body + four ) ;
if ( strlen ( $response_body ) !== ( int ) REQUEST_HEADER [ 'content-length' ] ) {
// Big trouble in little Fetcher
scream ( ) ;
}
file_put_contents ( only_safe_filenames ( $options [ 'filename' ] , $response_body ) ) ;
}
Choose a reason for hiding this comment
post_message_to_js passes a message to the JavaScript module where it can be handled inside onMessage(). This commit adds support for returning values and promises inside that handler to feed them back to PHP. For example: ```ts await playground.onMessage(async (message: string) => { const envelope: RequestMessage = JSON.parse(message); const { type, data } = envelope; if (type !== ' request') { return ''; } return handleRequest(data); }); ``` A handler like above could be used to add support for network requests, which is in fact what #724 does
…d give non-proxied requests a chance to succeed if the CORS setup is right
aa2cae4
82893b1
…essage_to_js() ( #732 ) ## Description A part of #724 `post_message_to_js` passes a message to the JavaScript module where it can be handled inside `onMessage()`. This commit adds support for returning values and promises inside that handler to feed them back to PHP. For example: ```php $response = post_message_to_js(json_encode($request_data)); ``` ```ts await playground.onMessage(async (message: string) => { const requestData = JSON.parse(message); const response = await fetch(requestData); return JSON.stringify(toSimpleObject(response)); }); ``` A handler like above could be used to add support for network requests, which is in fact what #724 does
|
file. However, the setPhpIniEntry() was called after the initial php.run() which made it noop. This broke the networking support merged in #724 . This commit solves that by moving the setPhpIniEntry right after the PHP module is initialized. I am not super happy about exporting the virtualized file path from the blueprints module – let's revisit that in the future.
#738 introduced a change that made all WordPress constants be defined by including a "virtualized" wp-consts.php file. However, the required `setPhpIniEntry()` call was sometimes happening after the initial `php.run()` which made it noop. This broke the networking support merged in #724 . This commit solves that by exposing a new `php.defineConstant()` API that handles the file virtualization. I'm not super happy about virtualizing files so let's revisit that in the future, see #750 .
…to enable it ( #812 ) Disables the network support by default and adds a UI control to enable it. Why? #724 introduced support for wp_safe_remote_get() request and enabled it by default on playground.wordpress.net. The problem is, all the requests block rendering of WordPress pages and noticeably slow down the site. Let's disable it by default for a lightweight user experience, and then add an easy way to enable it, for example in the configuration modal. Closes #755 ## Testing Instructions * Go to http://localhost:5400/website -server/? url=/wp-admin/plugin-install.php * Confirm the plugins aren't loaded and you see a communicative error message * Go to the settings modal * Enable the network support * Confirm the plugins are loaded now <img width="1121" alt="CleanShot 2023-11-27 at 17 10 45@2x " src=" https://github.com/WordPress/wordpress-playground/assets/205419/ea95b783-d7a1-45c6-ab95-90b5c8ec6ce4 ">