@farcaster/frame-host 0.0.8 → 0.0.10
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/comlink/comlink.d.ts +155 -0
- package/dist/comlink/comlink.js +367 -0
- package/dist/comlink/index.d.ts +1 -0
- package/dist/comlink/index.js +1 -0
- package/dist/comlink/node-adapter.d.ts +13 -0
- package/dist/comlink/node-adapter.js +32 -0
- package/dist/comlink/protocol.d.ts +75 -0
- package/dist/comlink/protocol.js +6 -0
- package/dist/helpers/endpoint.d.ts +2 -1
- package/dist/helpers/endpoint.js +6 -5
- package/dist/helpers/provider.d.ts +8 -2
- package/dist/helpers/provider.js +13 -3
- package/dist/iframe.d.ts +2 -1
- package/dist/iframe.js +5 -4
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types.d.ts +2 -2
- package/package.json +1 -5
- package/src/comlink/comlink.ts +655 -0
- package/src/comlink/index.ts +1 -0
- package/src/comlink/node-adapter.ts +49 -0
- package/src/comlink/protocol.ts +111 -0
- package/src/helpers/endpoint.ts +7 -4
- package/src/helpers/provider.ts +25 -5
- package/src/iframe.ts +6 -3
- package/src/types.ts +2 -2
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2019 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export interface EventSource {
|
|
8
|
+
addEventListener(
|
|
9
|
+
type: string,
|
|
10
|
+
listener: EventListenerOrEventListenerObject,
|
|
11
|
+
options?: {}
|
|
12
|
+
): void;
|
|
13
|
+
|
|
14
|
+
removeEventListener(
|
|
15
|
+
type: string,
|
|
16
|
+
listener: EventListenerOrEventListenerObject,
|
|
17
|
+
options?: {}
|
|
18
|
+
): void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface PostMessageWithOrigin {
|
|
22
|
+
postMessage(
|
|
23
|
+
message: any,
|
|
24
|
+
targetOrigin: string,
|
|
25
|
+
transfer?: Transferable[]
|
|
26
|
+
): void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface Endpoint extends EventSource {
|
|
30
|
+
postMessage(message: any, transfer?: Transferable[]): void;
|
|
31
|
+
|
|
32
|
+
start?: () => void;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const enum WireValueType {
|
|
36
|
+
RAW = "RAW",
|
|
37
|
+
PROXY = "PROXY",
|
|
38
|
+
THROW = "THROW",
|
|
39
|
+
HANDLER = "HANDLER",
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface RawWireValue {
|
|
43
|
+
id?: string;
|
|
44
|
+
type: WireValueType.RAW;
|
|
45
|
+
value: {};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface HandlerWireValue {
|
|
49
|
+
id?: string;
|
|
50
|
+
type: WireValueType.HANDLER;
|
|
51
|
+
name: string;
|
|
52
|
+
value: unknown;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export type WireValue = RawWireValue | HandlerWireValue;
|
|
56
|
+
|
|
57
|
+
export type MessageID = string;
|
|
58
|
+
|
|
59
|
+
export const enum MessageType {
|
|
60
|
+
GET = "GET",
|
|
61
|
+
SET = "SET",
|
|
62
|
+
APPLY = "APPLY",
|
|
63
|
+
CONSTRUCT = "CONSTRUCT",
|
|
64
|
+
ENDPOINT = "ENDPOINT",
|
|
65
|
+
RELEASE = "RELEASE",
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface GetMessage {
|
|
69
|
+
id?: MessageID;
|
|
70
|
+
type: MessageType.GET;
|
|
71
|
+
path: string[];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface SetMessage {
|
|
75
|
+
id?: MessageID;
|
|
76
|
+
type: MessageType.SET;
|
|
77
|
+
path: string[];
|
|
78
|
+
value: WireValue;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface ApplyMessage {
|
|
82
|
+
id?: MessageID;
|
|
83
|
+
type: MessageType.APPLY;
|
|
84
|
+
path: string[];
|
|
85
|
+
argumentList: WireValue[];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface ConstructMessage {
|
|
89
|
+
id?: MessageID;
|
|
90
|
+
type: MessageType.CONSTRUCT;
|
|
91
|
+
path: string[];
|
|
92
|
+
argumentList: WireValue[];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface EndpointMessage {
|
|
96
|
+
id?: MessageID;
|
|
97
|
+
type: MessageType.ENDPOINT;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface ReleaseMessage {
|
|
101
|
+
id?: MessageID;
|
|
102
|
+
type: MessageType.RELEASE;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export type Message =
|
|
106
|
+
| GetMessage
|
|
107
|
+
| SetMessage
|
|
108
|
+
| ApplyMessage
|
|
109
|
+
| ConstructMessage
|
|
110
|
+
| EndpointMessage
|
|
111
|
+
| ReleaseMessage;
|
package/src/helpers/endpoint.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import * as Comlink from "comlink";
|
|
1
|
+
import * as Comlink from "../comlink";
|
|
2
2
|
import { FrameHost } from "@farcaster/frame-core";
|
|
3
3
|
import { Provider } from "ox";
|
|
4
4
|
import { forwardProviderEvents, wrapProviderRequest } from "./provider";
|
|
@@ -12,23 +12,26 @@ export function exposeToEndpoint({
|
|
|
12
12
|
sdk,
|
|
13
13
|
frameOrigin,
|
|
14
14
|
ethProvider,
|
|
15
|
+
debug = false
|
|
15
16
|
}: {
|
|
16
17
|
endpoint: HostEndpoint,
|
|
17
18
|
sdk: Omit<FrameHost, 'ethProviderRequestV2'>,
|
|
18
19
|
frameOrigin: string;
|
|
19
20
|
ethProvider?: Provider.Provider,
|
|
21
|
+
debug?: boolean
|
|
20
22
|
}) {
|
|
21
23
|
const extendedSdk = sdk as FrameHost;
|
|
22
24
|
|
|
23
25
|
let cleanup: () => void | undefined;
|
|
24
26
|
if (ethProvider) {
|
|
25
|
-
extendedSdk.ethProviderRequestV2 = wrapProviderRequest(ethProvider);
|
|
26
|
-
cleanup = forwardProviderEvents(ethProvider, endpoint);
|
|
27
|
+
extendedSdk.ethProviderRequestV2 = wrapProviderRequest({ provider: ethProvider, debug });
|
|
28
|
+
cleanup = forwardProviderEvents({ provider: ethProvider, endpoint });
|
|
27
29
|
}
|
|
28
30
|
|
|
29
|
-
Comlink.expose(extendedSdk, endpoint, [frameOrigin]);
|
|
31
|
+
const unexpose = Comlink.expose(extendedSdk, endpoint, [frameOrigin]);
|
|
30
32
|
|
|
31
33
|
return () => {
|
|
32
34
|
cleanup?.();
|
|
35
|
+
unexpose();
|
|
33
36
|
}
|
|
34
37
|
}
|
package/src/helpers/provider.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { Provider, RpcRequest, RpcResponse } from "ox";
|
|
2
2
|
import { HostEndpoint } from "../types";
|
|
3
3
|
|
|
4
|
-
export function forwardProviderEvents(
|
|
4
|
+
export function forwardProviderEvents({
|
|
5
|
+
provider,
|
|
6
|
+
endpoint
|
|
7
|
+
}: {
|
|
5
8
|
provider: Provider.Provider,
|
|
6
9
|
endpoint: HostEndpoint
|
|
7
|
-
) {
|
|
10
|
+
}) {
|
|
8
11
|
let accountsChanged: Provider.EventMap['accountsChanged'] = (accounts) => {
|
|
9
12
|
endpoint.emitEthProvider('accountsChanged', [accounts]);
|
|
10
13
|
};
|
|
@@ -45,16 +48,33 @@ export function forwardProviderEvents(
|
|
|
45
48
|
* Wraps a provider's request function with a result format that can transfer
|
|
46
49
|
* errors across scripting boundaries.
|
|
47
50
|
*/
|
|
48
|
-
export const wrapProviderRequest = (
|
|
51
|
+
export const wrapProviderRequest = ({
|
|
52
|
+
provider,
|
|
53
|
+
debug = false
|
|
54
|
+
}: {
|
|
55
|
+
provider: Provider.Provider,
|
|
56
|
+
debug?: boolean
|
|
57
|
+
}) =>
|
|
49
58
|
async (request: RpcRequest.RpcRequest) => {
|
|
50
59
|
try {
|
|
60
|
+
if (debug) {
|
|
61
|
+
console.debug("[frame-host] eth provider req: ", request)
|
|
62
|
+
}
|
|
51
63
|
const result = await provider.request(request);
|
|
52
|
-
|
|
53
|
-
return RpcResponse.from(
|
|
64
|
+
const response = RpcResponse.from(
|
|
54
65
|
{ result },
|
|
55
66
|
{ request }
|
|
56
67
|
);
|
|
68
|
+
|
|
69
|
+
if (debug) {
|
|
70
|
+
console.debug("[frame-host] eth provider res: ", response)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return response;
|
|
57
74
|
} catch (e) {
|
|
75
|
+
if (debug) {
|
|
76
|
+
console.error("provider request error", e)
|
|
77
|
+
}
|
|
58
78
|
if (e instanceof Provider.ProviderRpcError) {
|
|
59
79
|
return RpcResponse.from(
|
|
60
80
|
{
|
package/src/iframe.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import * as Comlink from "comlink";
|
|
1
|
+
import * as Comlink from "./comlink";
|
|
2
2
|
import type { Provider } from "ox";
|
|
3
3
|
import { FrameHost } from "@farcaster/frame-core";
|
|
4
4
|
import { HostEndpoint } from "./types";
|
|
@@ -52,18 +52,21 @@ export function exposeToIframe({
|
|
|
52
52
|
sdk,
|
|
53
53
|
ethProvider,
|
|
54
54
|
frameOrigin,
|
|
55
|
+
debug = false
|
|
55
56
|
}: {
|
|
56
57
|
iframe: HTMLIFrameElement;
|
|
57
58
|
sdk: Omit<FrameHost, 'ethProviderRequestV2'>;
|
|
58
59
|
frameOrigin: string;
|
|
59
60
|
ethProvider?: Provider.Provider;
|
|
61
|
+
debug?: boolean;
|
|
60
62
|
}) {
|
|
61
|
-
const endpoint = createIframeEndpoint({ iframe, targetOrigin: frameOrigin });
|
|
63
|
+
const endpoint = createIframeEndpoint({ iframe, targetOrigin: frameOrigin, debug });
|
|
62
64
|
const cleanup = exposeToEndpoint({
|
|
63
65
|
endpoint,
|
|
64
66
|
sdk,
|
|
65
67
|
ethProvider,
|
|
66
|
-
frameOrigin
|
|
68
|
+
frameOrigin,
|
|
69
|
+
debug,
|
|
67
70
|
});
|
|
68
71
|
|
|
69
72
|
return {
|
package/src/types.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import type { Endpoint } from "./comlink";
|
|
2
2
|
import type { EmitEthProvider } from "@farcaster/frame-core";
|
|
3
3
|
|
|
4
|
-
export type HostEndpoint =
|
|
4
|
+
export type HostEndpoint = Endpoint & {
|
|
5
5
|
emit: (data: any) => void;
|
|
6
6
|
emitEthProvider: EmitEthProvider;
|
|
7
7
|
};
|