@farcaster/frame-host-react-native 0.0.0-canary-20250508220243 → 0.0.0-canary-20250508234838
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/dist/rn-comlink-helpers.d.ts +15 -0
- package/dist/rn-comlink-helpers.js +61 -0
- package/dist/rn-comlink.d.ts +29 -0
- package/dist/rn-comlink.js +73 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/webviewAdapter.js +22 -8
- package/package.json +2 -2
- package/src/rn-comlink-helpers.ts +108 -0
- package/src/rn-comlink.ts +130 -0
- package/src/webviewAdapter.ts +31 -9
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type Remote, type TransferHandler, type WireValue } from '@farcaster/frame-host';
|
|
2
|
+
interface SerializedRnFunction {
|
|
3
|
+
__isRNProxiedFunction: true;
|
|
4
|
+
id: string;
|
|
5
|
+
type: 'function';
|
|
6
|
+
}
|
|
7
|
+
export interface ReactNativeWebViewProxy {
|
|
8
|
+
_callWebViewProxiedFunction: (id: string, args: WireValue[]) => Promise<WireValue>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Creates a Comlink TransferHandler for deserializing functions proxied from WebView
|
|
12
|
+
* and serializing function stubs created by this handler.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createReactNativeFunctionDeserializer(getWebViewProxy: () => Remote<ReactNativeWebViewProxy> | undefined): TransferHandler<Function, SerializedRnFunction>;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { WireValueType, fromWireValue, toWireValue, } from '@farcaster/frame-host';
|
|
2
|
+
function processArgumentsForRN(argumentList) {
|
|
3
|
+
const processed = argumentList.map((arg) => toWireValue(arg));
|
|
4
|
+
const wireValues = processed.map((p) => p[0]);
|
|
5
|
+
const transferables = processed.flatMap((p) => p[1]);
|
|
6
|
+
if (transferables.length > 0) {
|
|
7
|
+
console.warn('[RN Host Comlink] Transferables found in arguments to WebView proxied function are ignored.');
|
|
8
|
+
}
|
|
9
|
+
return [wireValues, []];
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Creates a Comlink TransferHandler for deserializing functions proxied from WebView
|
|
13
|
+
* and serializing function stubs created by this handler.
|
|
14
|
+
*/
|
|
15
|
+
export function createReactNativeFunctionDeserializer(getWebViewProxy) {
|
|
16
|
+
return {
|
|
17
|
+
// canHandle checks if a given value is a Function stub that this handler can serialize.
|
|
18
|
+
canHandle: (value) => {
|
|
19
|
+
return (typeof value === 'function' &&
|
|
20
|
+
Object.hasOwn(value, '__functionId__') &&
|
|
21
|
+
typeof value.__functionId__ === 'string');
|
|
22
|
+
},
|
|
23
|
+
// deserialize converts the wire format (SerializedRnFunction) into a callable Function stub.
|
|
24
|
+
deserialize: (serializedValue) => {
|
|
25
|
+
const functionId = serializedValue.id;
|
|
26
|
+
console.debug(`[RN Host Comlink] Creating stub for WebView function ID: ${functionId}`);
|
|
27
|
+
const webViewFunctionStub = async (...args) => {
|
|
28
|
+
const webViewProxy = getWebViewProxy();
|
|
29
|
+
if (!webViewProxy || !webViewProxy._callWebViewProxiedFunction) {
|
|
30
|
+
console.error('[RN Host Comlink] WebView proxy or _callWebViewProxiedFunction not available.');
|
|
31
|
+
throw new Error('[RN Host Comlink] WebView proxy not available. Cannot call remote function.');
|
|
32
|
+
}
|
|
33
|
+
console.debug(`[RN Host Comlink] Calling WebView function ID: ${functionId} with args:`, args);
|
|
34
|
+
const [wireArgs] = processArgumentsForRN(args);
|
|
35
|
+
try {
|
|
36
|
+
const typedProxy = webViewProxy;
|
|
37
|
+
const wireResult = await typedProxy._callWebViewProxiedFunction(functionId, wireArgs);
|
|
38
|
+
if (wireResult &&
|
|
39
|
+
wireResult.type === WireValueType.HANDLER &&
|
|
40
|
+
wireResult.name === 'throw') {
|
|
41
|
+
return fromWireValue(wireResult);
|
|
42
|
+
}
|
|
43
|
+
return fromWireValue(wireResult);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
console.error(`[RN Host Comlink] Error in stub for function ID ${functionId}:`, error);
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
webViewFunctionStub.__functionId__ = functionId;
|
|
51
|
+
return webViewFunctionStub;
|
|
52
|
+
},
|
|
53
|
+
// serialize converts a Function stub (that this handler knows about) into its wire format.
|
|
54
|
+
serialize: (fnStub) => {
|
|
55
|
+
// canHandle should have already verified this is one of our stubs.
|
|
56
|
+
const id = fnStub.__functionId__;
|
|
57
|
+
console.debug(`[RN Host Comlink] Serializing function stub ID: ${id}`);
|
|
58
|
+
return [{ __isRNProxiedFunction: true, id, type: 'function' }, []];
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type FrameHost, type Endpoint as FrameHostEndpoint, type Remote as FrameHostRemote, releaseProxy } from '@farcaster/frame-host';
|
|
2
|
+
import type { Provider } from 'ox/Provider';
|
|
3
|
+
import { type ReactNativeWebViewProxy } from './rn-comlink-helpers';
|
|
4
|
+
export { releaseProxy };
|
|
5
|
+
/**
|
|
6
|
+
* Ensures React Native specific Comlink transfer handlers are registered globally.
|
|
7
|
+
*/
|
|
8
|
+
export declare function ensureRnComlinkHandlers(): void;
|
|
9
|
+
/**
|
|
10
|
+
* Wraps a WebView endpoint with Comlink, ensuring RN handlers are set up.
|
|
11
|
+
* Crucially, it sets the webViewProxyInstance needed by the deserializer's callback.
|
|
12
|
+
*
|
|
13
|
+
* If this is not called, deserializing the function will fail.
|
|
14
|
+
*/
|
|
15
|
+
export declare function wrapWebViewForRnComlink<T extends ReactNativeWebViewProxy>(endpoint: FrameHostEndpoint): FrameHostRemote<T>;
|
|
16
|
+
/**
|
|
17
|
+
* Exposes the Host SDK object to the WebView, ensuring RN handlers are set up
|
|
18
|
+
* on the Comlink instance used for the exposure.
|
|
19
|
+
* Replicates logic from `@farcaster/frame-host`'s `exposeToEndpoint` helper
|
|
20
|
+
* but makes sure to set up the RN handlers first.
|
|
21
|
+
*
|
|
22
|
+
* @returns Cleanup function if any event listeners were added (e.g., provider events)
|
|
23
|
+
*/
|
|
24
|
+
export declare function exposeToWebViewWithRnComlink({ endpoint, sdk, ethProvider, debug, }: {
|
|
25
|
+
endpoint: FrameHostEndpoint;
|
|
26
|
+
sdk: Omit<FrameHost, 'ethProviderRequestV2'>;
|
|
27
|
+
ethProvider?: Provider;
|
|
28
|
+
debug?: boolean;
|
|
29
|
+
}): (() => void) | undefined;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { expose, releaseProxy, transferHandlers, wrap, } from '@farcaster/frame-host';
|
|
2
|
+
import { forwardProviderEvents, wrapProviderRequest, } from '@farcaster/frame-host/src/helpers/provider';
|
|
3
|
+
import { wrapHandlers } from '@farcaster/frame-host/src/helpers/sdk';
|
|
4
|
+
import { createReactNativeFunctionDeserializer, } from './rn-comlink-helpers';
|
|
5
|
+
// Re-export releaseProxy symbol for use in cleanup
|
|
6
|
+
export { releaseProxy };
|
|
7
|
+
let rnHandlersInitialized = false;
|
|
8
|
+
let webViewProxyInstance = null;
|
|
9
|
+
let rnFunctionDeserializer = null;
|
|
10
|
+
/**
|
|
11
|
+
* Ensures React Native specific Comlink transfer handlers are registered globally.
|
|
12
|
+
*/
|
|
13
|
+
export function ensureRnComlinkHandlers() {
|
|
14
|
+
if (rnHandlersInitialized) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const getWebViewProxy = () => {
|
|
18
|
+
return webViewProxyInstance === null ? undefined : webViewProxyInstance;
|
|
19
|
+
};
|
|
20
|
+
rnFunctionDeserializer =
|
|
21
|
+
createReactNativeFunctionDeserializer(getWebViewProxy);
|
|
22
|
+
transferHandlers.set('rn_function', rnFunctionDeserializer);
|
|
23
|
+
console.debug('[RN Host Comlink] Registered ReactNativeFunctionDeserializer via ensureRnComlinkHandlers.');
|
|
24
|
+
rnHandlersInitialized = true;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Wraps a WebView endpoint with Comlink, ensuring RN handlers are set up.
|
|
28
|
+
* Crucially, it sets the webViewProxyInstance needed by the deserializer's callback.
|
|
29
|
+
*
|
|
30
|
+
* If this is not called, deserializing the function will fail.
|
|
31
|
+
*/
|
|
32
|
+
export function wrapWebViewForRnComlink(endpoint) {
|
|
33
|
+
ensureRnComlinkHandlers();
|
|
34
|
+
const proxy = wrap(endpoint);
|
|
35
|
+
webViewProxyInstance = proxy;
|
|
36
|
+
return proxy;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Exposes the Host SDK object to the WebView, ensuring RN handlers are set up
|
|
40
|
+
* on the Comlink instance used for the exposure.
|
|
41
|
+
* Replicates logic from `@farcaster/frame-host`'s `exposeToEndpoint` helper
|
|
42
|
+
* but makes sure to set up the RN handlers first.
|
|
43
|
+
*
|
|
44
|
+
* @returns Cleanup function if any event listeners were added (e.g., provider events)
|
|
45
|
+
*/
|
|
46
|
+
export function exposeToWebViewWithRnComlink({ endpoint, sdk, ethProvider, debug = false, }) {
|
|
47
|
+
ensureRnComlinkHandlers();
|
|
48
|
+
const extendedSdk = wrapHandlers(sdk);
|
|
49
|
+
let providerCleanup;
|
|
50
|
+
if (ethProvider) {
|
|
51
|
+
extendedSdk.ethProviderRequestV2 = wrapProviderRequest({
|
|
52
|
+
provider: ethProvider,
|
|
53
|
+
debug,
|
|
54
|
+
});
|
|
55
|
+
try {
|
|
56
|
+
providerCleanup = forwardProviderEvents({
|
|
57
|
+
provider: ethProvider,
|
|
58
|
+
endpoint: endpoint,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
catch (e) {
|
|
62
|
+
console.error('[RN Host Comlink] Error setting up provider event forwarding:', e);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
try {
|
|
66
|
+
expose(extendedSdk, endpoint);
|
|
67
|
+
console.debug('[RN Host Comlink] Exposed SDK to WebView via exposeToWebViewWithRnComlink');
|
|
68
|
+
}
|
|
69
|
+
catch (e) {
|
|
70
|
+
console.warn('[RN Host Comlink] Error exposing API (may already be exposed):', e);
|
|
71
|
+
}
|
|
72
|
+
return providerCleanup;
|
|
73
|
+
}
|