@ripple-ts/adapter 0.2.214 → 0.2.216
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/package.json +1 -1
- package/src/rpc.js +38 -3
- package/tsconfig.json +8 -0
- package/types/rpc.d.ts +21 -2
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
package/src/rpc.js
CHANGED
|
@@ -65,12 +65,15 @@ export function patch_global_fetch(async_context) {
|
|
|
65
65
|
// This prevents layered wrapping when createHandler() or getDevAsyncContext()
|
|
66
66
|
// is called more than once in the same process (tests, hot reload, etc.).
|
|
67
67
|
if (/** @type {RipplePatchedFetch} */ (globalThis.fetch).__ripple_patched) {
|
|
68
|
-
return ()
|
|
68
|
+
return { restore() {}, set_handler(/** @type {any} */ _h) {} };
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
/** @type {typeof globalThis.fetch} */
|
|
72
72
|
const original_fetch = globalThis.fetch;
|
|
73
73
|
|
|
74
|
+
/** @type {((request: Request) => Promise<Response>) | null} */
|
|
75
|
+
let internal_handler = null;
|
|
76
|
+
|
|
74
77
|
/**
|
|
75
78
|
* @param {string | Request | URL} input
|
|
76
79
|
* @param {RequestInit} [init]
|
|
@@ -93,6 +96,26 @@ export function patch_global_fetch(async_context) {
|
|
|
93
96
|
input = new URL(relative, context.origin);
|
|
94
97
|
}
|
|
95
98
|
}
|
|
99
|
+
|
|
100
|
+
// Short-circuit same-origin requests: route them directly through
|
|
101
|
+
// the handler in-process instead of making a real network request.
|
|
102
|
+
// This avoids issues on serverless platforms (e.g. Vercel Deployment
|
|
103
|
+
// Protection blocking server-to-server calls) and eliminates the
|
|
104
|
+
// latency of a redundant network round-trip + cold start.
|
|
105
|
+
if (internal_handler !== null) {
|
|
106
|
+
const resolved_url =
|
|
107
|
+
typeof input === 'string' ? input : input instanceof Request ? input.url : input.href;
|
|
108
|
+
|
|
109
|
+
try {
|
|
110
|
+
const resolved_origin = new URL(resolved_url).origin;
|
|
111
|
+
if (resolved_origin === context.origin) {
|
|
112
|
+
const request = input instanceof Request ? input : new Request(input, init);
|
|
113
|
+
return internal_handler(request);
|
|
114
|
+
}
|
|
115
|
+
} catch {
|
|
116
|
+
// Not a valid URL — fall through to real fetch
|
|
117
|
+
}
|
|
118
|
+
}
|
|
96
119
|
}
|
|
97
120
|
|
|
98
121
|
return original_fetch(input, init);
|
|
@@ -107,8 +130,20 @@ export function patch_global_fetch(async_context) {
|
|
|
107
130
|
|
|
108
131
|
globalThis.fetch = /** @type {typeof globalThis.fetch} */ (patched_fetch);
|
|
109
132
|
|
|
110
|
-
return
|
|
111
|
-
|
|
133
|
+
return {
|
|
134
|
+
/** Restore the original fetch. */
|
|
135
|
+
restore() {
|
|
136
|
+
globalThis.fetch = original_fetch;
|
|
137
|
+
},
|
|
138
|
+
/**
|
|
139
|
+
* Set the handler used for same-origin short-circuit.
|
|
140
|
+
* Called by createHandler() after the handler function is created.
|
|
141
|
+
*
|
|
142
|
+
* @param {(request: Request) => Promise<Response>} handler
|
|
143
|
+
*/
|
|
144
|
+
set_handler(handler) {
|
|
145
|
+
internal_handler = handler;
|
|
146
|
+
},
|
|
112
147
|
};
|
|
113
148
|
}
|
|
114
149
|
|
package/tsconfig.json
ADDED
package/types/rpc.d.ts
CHANGED
|
@@ -64,11 +64,30 @@ export type HandleRpcOptions = {
|
|
|
64
64
|
*/
|
|
65
65
|
export function derive_origin(request: Request, trust_proxy: boolean): string;
|
|
66
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Return value of patch_global_fetch with methods to control the patched fetch.
|
|
69
|
+
*/
|
|
70
|
+
export type PatchedFetchHandle = {
|
|
71
|
+
/** Restore the original fetch. */
|
|
72
|
+
restore: () => void;
|
|
73
|
+
/**
|
|
74
|
+
* Set the handler used for same-origin short-circuit.
|
|
75
|
+
* When set, same-origin requests are routed directly through the handler
|
|
76
|
+
* in-process instead of making a real network request.
|
|
77
|
+
*/
|
|
78
|
+
set_handler: (handler: (request: Request) => Promise<Response>) => void;
|
|
79
|
+
};
|
|
80
|
+
|
|
67
81
|
/**
|
|
68
82
|
* Patch globalThis.fetch to resolve relative URLs using the async context.
|
|
69
|
-
* Returns a
|
|
83
|
+
* Returns a handle with `restore()` and `set_handler()` methods.
|
|
84
|
+
*
|
|
85
|
+
* When `set_handler()` is called with a request handler, same-origin fetch
|
|
86
|
+
* calls (matching the async context's origin) are routed directly through
|
|
87
|
+
* the handler in-process, avoiding network round-trips and issues with
|
|
88
|
+
* serverless platforms (e.g. Vercel Deployment Protection).
|
|
70
89
|
*/
|
|
71
|
-
export function patch_global_fetch(async_context: AsyncContext):
|
|
90
|
+
export function patch_global_fetch(async_context: AsyncContext): PatchedFetchHandle;
|
|
72
91
|
|
|
73
92
|
/**
|
|
74
93
|
* Build a hash → RpcEntry lookup from rpcModules.
|