Framework updates
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
131
node_modules/@jqhtml/ssr/src/http-intercept.js
generated
vendored
131
node_modules/@jqhtml/ssr/src/http-intercept.js
generated
vendored
@@ -138,12 +138,132 @@ function createXHRWrapper(baseUrl, OriginalXHR) {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap an existing fetch function to inject the SSR token header on all requests
|
||||
* @param {function} fetchFn - The fetch function to wrap
|
||||
* @param {string} ssrToken - SSR token value
|
||||
* @returns {function} Wrapped fetch function
|
||||
*/
|
||||
function wrapFetchWithSsrToken(fetchFn, ssrToken) {
|
||||
return function ssrTokenFetch(url, options = {}) {
|
||||
const headers = { ...(options.headers || {}) };
|
||||
headers['X-SSR-Token'] = ssrToken;
|
||||
return fetchFn(url, { ...options, headers });
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Install SSR token injection on fetch and XHR
|
||||
* @param {Window} window - jsdom window object
|
||||
* @param {string} ssrToken - SSR token value
|
||||
*/
|
||||
function installSsrTokenIntercept(window, ssrToken) {
|
||||
if (!ssrToken) return;
|
||||
|
||||
// Wrap fetch with SSR token
|
||||
const currentFetch = window.fetch;
|
||||
window.fetch = wrapFetchWithSsrToken(currentFetch, ssrToken);
|
||||
|
||||
if (typeof global !== 'undefined') {
|
||||
global.fetch = window.fetch;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a custom jQuery $.ajaxTransport that uses Node's fetch() for real HTTP.
|
||||
*
|
||||
* This solves the problem of jsdom's XMLHttpRequest not making real network requests.
|
||||
* jQuery's $.ajax() will use this transport instead of XHR, so the chain becomes:
|
||||
* $.ajax() → ajaxTransport → Node fetch() → real HTTP request
|
||||
*
|
||||
* @param {Window} window - jsdom window object (must have jQuery on window.$)
|
||||
* @param {string} baseUrl - Base URL for resolving relative URLs
|
||||
* @param {string} [ssrToken] - Optional SSR token to include in requests
|
||||
*/
|
||||
function installAjaxTransport(window, baseUrl, ssrToken) {
|
||||
const $ = window.$;
|
||||
if (!$ || !$.ajaxTransport) return;
|
||||
|
||||
$.ajaxTransport('+*', function(options, originalOptions, jqXHR) {
|
||||
return {
|
||||
send: function(headers, completeCallback) {
|
||||
// Build absolute URL
|
||||
const url = resolveUrl(options.url || '', baseUrl);
|
||||
|
||||
// Build fetch options
|
||||
const fetchHeaders = {};
|
||||
|
||||
// Copy headers from jQuery, stripping auth headers
|
||||
for (const [name, value] of Object.entries(headers)) {
|
||||
const lowerName = name.toLowerCase();
|
||||
if (lowerName === 'authorization' || lowerName === 'cookie') continue;
|
||||
fetchHeaders[name] = value;
|
||||
}
|
||||
|
||||
// Inject SSR token if provided
|
||||
if (ssrToken) {
|
||||
fetchHeaders['X-SSR-Token'] = ssrToken;
|
||||
}
|
||||
|
||||
// Set content type if not already set
|
||||
if (options.contentType && !fetchHeaders['Content-Type']) {
|
||||
fetchHeaders['Content-Type'] = options.contentType;
|
||||
}
|
||||
|
||||
const fetchOptions = {
|
||||
method: options.type || 'GET',
|
||||
headers: fetchHeaders
|
||||
};
|
||||
|
||||
// Add body for non-GET requests
|
||||
if (options.data && options.type && options.type.toUpperCase() !== 'GET') {
|
||||
fetchOptions.body = typeof options.data === 'string'
|
||||
? options.data
|
||||
: JSON.stringify(options.data);
|
||||
}
|
||||
|
||||
// Use the globally available fetch (already has URL rewriting from createFetch)
|
||||
const fetchFn = globalThis.fetch || window.fetch;
|
||||
|
||||
fetchFn(url, fetchOptions)
|
||||
.then(async (response) => {
|
||||
const responseText = await response.text();
|
||||
const responseHeaders = {};
|
||||
if (response.headers && typeof response.headers.forEach === 'function') {
|
||||
response.headers.forEach((value, name) => {
|
||||
responseHeaders[name] = value;
|
||||
});
|
||||
}
|
||||
|
||||
const headerString = Object.entries(responseHeaders)
|
||||
.map(([k, v]) => `${k}: ${v}`)
|
||||
.join('\r\n');
|
||||
|
||||
completeCallback(
|
||||
response.status,
|
||||
response.statusText,
|
||||
{ text: responseText },
|
||||
headerString
|
||||
);
|
||||
})
|
||||
.catch((err) => {
|
||||
completeCallback(0, 'Network Error: ' + err.message, {}, '');
|
||||
});
|
||||
},
|
||||
abort: function() {
|
||||
// Cannot abort native fetch, but jQuery expects this method
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Install HTTP interception on a jsdom window
|
||||
* @param {Window} window - jsdom window object
|
||||
* @param {string} baseUrl - Base URL for URL resolution
|
||||
* @param {string} [ssrToken] - Optional SSR token for outgoing requests
|
||||
*/
|
||||
function installHttpIntercept(window, baseUrl) {
|
||||
function installHttpIntercept(window, baseUrl, ssrToken) {
|
||||
// Override fetch
|
||||
window.fetch = createFetch(baseUrl);
|
||||
|
||||
@@ -161,11 +281,18 @@ function installHttpIntercept(window, baseUrl) {
|
||||
global.XMLHttpRequest = WrappedXHR;
|
||||
}
|
||||
}
|
||||
|
||||
// Install SSR token on fetch/XHR if provided
|
||||
if (ssrToken) {
|
||||
installSsrTokenIntercept(window, ssrToken);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
resolveUrl,
|
||||
createFetch,
|
||||
createXHRWrapper,
|
||||
installHttpIntercept
|
||||
installHttpIntercept,
|
||||
installSsrTokenIntercept,
|
||||
installAjaxTransport
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user