From 1e620c73bca7462f3002dd4a7fdbd17e2a27bcf3 Mon Sep 17 00:00:00 2001 From: ProgrammerIn-wonderland <3838shah@gmail.com> Date: Sun, 14 Sep 2025 17:01:16 -0400 Subject: [PATCH] make drivers and KV also preflightless --- src/backend/src/modules/web/lib/eggspress.js | 2 ++ src/backend/src/routers/drivers/call.js | 3 +++ src/puter-js/src/lib/utils.js | 7 ++++--- src/puter-js/src/modules/Drivers.js | 4 ++-- src/puter-js/src/modules/KV.js | 4 ++-- 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/backend/src/modules/web/lib/eggspress.js b/src/backend/src/modules/web/lib/eggspress.js index e8ef11f1..c25694ea 100644 --- a/src/backend/src/modules/web/lib/eggspress.js +++ b/src/backend/src/modules/web/lib/eggspress.js @@ -53,6 +53,8 @@ module.exports = function eggspress (route, settings, handler) { // A hack so plain text is parsed as JSON in methods which need to be lower latency/avoid the cors roundtrip if ( settings.noReallyItsJson ) mw.push(express.json({ type: '*/*' })); + mw.push(express.json({ type: (req) => req.headers['content-type'] === "text/plain;actually=json" })); + if ( settings.auth ) mw.push(require('../../../middleware/auth')); if ( settings.auth2 ) mw.push(require('../../../middleware/auth2')); diff --git a/src/backend/src/routers/drivers/call.js b/src/backend/src/routers/drivers/call.js index 66ee075e..a0ef6747 100644 --- a/src/backend/src/routers/drivers/call.js +++ b/src/backend/src/routers/drivers/call.js @@ -53,6 +53,7 @@ let _handle_multipart; module.exports = eggspress('/drivers/call', { subdomain: 'api', auth2: true, + // noReallyItsJson: true, allowedMethods: ['POST'], }, async (req, res, next) => { const x = Context.get(); @@ -64,6 +65,8 @@ module.exports = eggspress('/drivers/call', { ({ params: body, p_data_end: p_request } = await _handle_multipart(req)); } else body = req.body; + console.log("Driver call body: ") + const interface_name = body.interface; const test_mode = body.test_mode; diff --git a/src/puter-js/src/lib/utils.js b/src/puter-js/src/lib/utils.js index 76ac4bf9..941a3acf 100644 --- a/src/puter-js/src/lib/utils.js +++ b/src/puter-js/src/lib/utils.js @@ -77,7 +77,7 @@ function uuidv4(){ * * @returns {XMLHttpRequest} The initialized XMLHttpRequest object. */ -function initXhr(endpoint, APIOrigin, authToken, method= "post", contentType = "application/json;charset=UTF-8", responseType = undefined) { +function initXhr(endpoint, APIOrigin, authToken, method= "post", contentType = "text/plain;actually=json", responseType = undefined) { const xhr = new XMLHttpRequest(); xhr.open(method, APIOrigin + endpoint, true); if (authToken) @@ -279,7 +279,7 @@ async function driverCall_( resolve_func, reject_func, driverInterface, driverName, driverMethod, driverArgs, method, - contentType = 'application/json;charset=UTF-8', + contentType = 'text/plain;actually=json', settings = {}, ) { // Generate request ID for logging @@ -319,7 +319,7 @@ async function driverCall_( const success_cb = Valid.callback(options.success) ?? NOOP; const error_cb = Valid.callback(options.error) ?? NOOP; // create xhr object - const xhr = initXhr('/drivers/call', puter.APIOrigin, puter.authToken, 'POST', contentType); + const xhr = initXhr('/drivers/call', puter.APIOrigin, undefined, 'POST', contentType); // Store request info for later logging if (requestInfo) { @@ -528,6 +528,7 @@ async function driverCall_( test_mode: settings?.test_mode, method: driverMethod, args: driverArgs, + auth_token: puter.authToken })); } diff --git a/src/puter-js/src/modules/Drivers.js b/src/puter-js/src/modules/Drivers.js index d81d3cfb..4aea8734 100644 --- a/src/puter-js/src/modules/Drivers.js +++ b/src/puter-js/src/modules/Drivers.js @@ -34,8 +34,7 @@ class FetchDriverCallBackend { try { const resp = await fetch(`${this.context.APIOrigin}/drivers/call`, { headers: { - Authorization: `Bearer ${this.context.authToken}`, - 'Content-Type': 'application/json', + 'Content-Type': 'text/plain;actually=json', }, method: 'POST', body: JSON.stringify({ @@ -45,6 +44,7 @@ class FetchDriverCallBackend { : {}), method: method_name, args: parameters, + auth_token: this.context.authToken }), }); diff --git a/src/puter-js/src/modules/KV.js b/src/puter-js/src/modules/KV.js index 816294ad..05ff1107 100644 --- a/src/puter-js/src/modules/KV.js +++ b/src/puter-js/src/modules/KV.js @@ -42,8 +42,7 @@ class KV{ const resp = await fetch(`${this.APIOrigin}/drivers/call`, { method: 'POST', headers: { - 'Content-Type': 'application/json', - Authorization: `Bearer ${this.authToken}`, + 'Content-Type': 'text/plain;actually=json', }, body: JSON.stringify({ interface: 'puter-kvstore', @@ -51,6 +50,7 @@ class KV{ args: { key: gui_cache_keys, }, + auth_token: this.authToken }), }); const arr_values = await resp.json();