@buoy-gg/impersonate 7.0.41 → 7.0.43
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/README.md +12 -80
- package/lib/commonjs/impersonate/components/DataNukeSettings.js +8 -675
- package/lib/commonjs/impersonate/components/ImpersonateBanner.js +1 -221
- package/lib/commonjs/impersonate/components/ImpersonateHistoryList.js +1 -181
- package/lib/commonjs/impersonate/components/ImpersonateModal.js +1 -319
- package/lib/commonjs/impersonate/components/ImpersonateOverlay.js +1 -26
- package/lib/commonjs/impersonate/components/ImpersonateStatusBar.js +1 -130
- package/lib/commonjs/impersonate/components/UserAvatar.js +1 -146
- package/lib/commonjs/impersonate/components/UserCard.js +1 -201
- package/lib/commonjs/impersonate/components/UserSearchView.js +1 -227
- package/lib/commonjs/impersonate/hooks/asyncStoragePeer.js +1 -17
- package/lib/commonjs/impersonate/hooks/asyncStoragePeer.web.js +1 -11
- package/lib/commonjs/impersonate/hooks/useAutoClearAsyncStorage.js +1 -134
- package/lib/commonjs/impersonate/hooks/useAutoClearReactQuery.js +1 -155
- package/lib/commonjs/impersonate/hooks/useAutoClearReactQuery.web.js +1 -20
- package/lib/commonjs/impersonate/hooks/useAutoClearRedux.js +1 -188
- package/lib/commonjs/impersonate/hooks/useAutoClearRedux.web.js +1 -27
- package/lib/commonjs/impersonate/hooks/useImpersonate.js +1 -215
- package/lib/commonjs/impersonate/hooks/useImpersonateHistory.js +1 -56
- package/lib/commonjs/impersonate/types/index.js +1 -16
- package/lib/commonjs/impersonate/types/types.js +1 -1
- package/lib/commonjs/impersonate/utils/impersonateListener.js +1 -350
- package/lib/commonjs/impersonate/utils/impersonateStore.js +1 -689
- package/lib/commonjs/index.js +1 -150
- package/lib/commonjs/preset.js +1 -232
- package/lib/commonjs/sync/impersonateSyncAdapter.js +1 -170
- package/lib/module/impersonate/components/DataNukeSettings.js +8 -670
- package/lib/module/impersonate/components/ImpersonateBanner.js +1 -215
- package/lib/module/impersonate/components/ImpersonateHistoryList.js +1 -176
- package/lib/module/impersonate/components/ImpersonateModal.js +1 -314
- package/lib/module/impersonate/components/ImpersonateOverlay.js +1 -21
- package/lib/module/impersonate/components/ImpersonateStatusBar.js +1 -125
- package/lib/module/impersonate/components/UserAvatar.js +1 -140
- package/lib/module/impersonate/components/UserCard.js +1 -196
- package/lib/module/impersonate/components/UserSearchView.js +1 -222
- package/lib/module/impersonate/hooks/asyncStoragePeer.js +1 -13
- package/lib/module/impersonate/hooks/asyncStoragePeer.web.js +1 -6
- package/lib/module/impersonate/hooks/useAutoClearAsyncStorage.js +1 -130
- package/lib/module/impersonate/hooks/useAutoClearReactQuery.js +1 -151
- package/lib/module/impersonate/hooks/useAutoClearReactQuery.web.js +1 -16
- package/lib/module/impersonate/hooks/useAutoClearRedux.js +1 -183
- package/lib/module/impersonate/hooks/useAutoClearRedux.web.js +1 -22
- package/lib/module/impersonate/hooks/useImpersonate.js +1 -212
- package/lib/module/impersonate/hooks/useImpersonateHistory.js +1 -52
- package/lib/module/impersonate/types/index.js +1 -3
- package/lib/module/impersonate/types/types.js +1 -1
- package/lib/module/impersonate/utils/impersonateListener.js +1 -341
- package/lib/module/impersonate/utils/impersonateStore.js +1 -686
- package/lib/module/index.js +1 -117
- package/lib/module/preset.js +1 -216
- package/lib/module/sync/impersonateSyncAdapter.js +1 -162
- package/lib/web/index.mjs +7 -8
- package/package.json +3 -6
- package/lib/typescript/impersonate/components/DataNukeSettings.d.ts.map +0 -1
- package/lib/typescript/impersonate/components/ImpersonateBanner.d.ts.map +0 -1
- package/lib/typescript/impersonate/components/ImpersonateHistoryList.d.ts.map +0 -1
- package/lib/typescript/impersonate/components/ImpersonateModal.d.ts.map +0 -1
- package/lib/typescript/impersonate/components/ImpersonateOverlay.d.ts.map +0 -1
- package/lib/typescript/impersonate/components/ImpersonateStatusBar.d.ts.map +0 -1
- package/lib/typescript/impersonate/components/UserAvatar.d.ts.map +0 -1
- package/lib/typescript/impersonate/components/UserCard.d.ts.map +0 -1
- package/lib/typescript/impersonate/components/UserSearchView.d.ts.map +0 -1
- package/lib/typescript/impersonate/hooks/asyncStoragePeer.d.ts.map +0 -1
- package/lib/typescript/impersonate/hooks/asyncStoragePeer.web.d.ts.map +0 -1
- package/lib/typescript/impersonate/hooks/useAutoClearAsyncStorage.d.ts.map +0 -1
- package/lib/typescript/impersonate/hooks/useAutoClearReactQuery.d.ts.map +0 -1
- package/lib/typescript/impersonate/hooks/useAutoClearReactQuery.web.d.ts.map +0 -1
- package/lib/typescript/impersonate/hooks/useAutoClearRedux.d.ts.map +0 -1
- package/lib/typescript/impersonate/hooks/useAutoClearRedux.web.d.ts.map +0 -1
- package/lib/typescript/impersonate/hooks/useImpersonate.d.ts.map +0 -1
- package/lib/typescript/impersonate/hooks/useImpersonateHistory.d.ts.map +0 -1
- package/lib/typescript/impersonate/types/index.d.ts.map +0 -1
- package/lib/typescript/impersonate/types/types.d.ts.map +0 -1
- package/lib/typescript/impersonate/utils/impersonateListener.d.ts.map +0 -1
- package/lib/typescript/impersonate/utils/impersonateStore.d.ts.map +0 -1
- package/lib/typescript/index.d.ts.map +0 -1
- package/lib/typescript/preset.d.ts.map +0 -1
- package/lib/typescript/sync/impersonateSyncAdapter.d.ts.map +0 -1
package/lib/module/index.js
CHANGED
|
@@ -1,117 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @buoy-gg/impersonate
|
|
5
|
-
*
|
|
6
|
-
* User impersonation tool for React Native DevTools.
|
|
7
|
-
* Injects custom headers into fetch/XHR requests when impersonating.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```tsx
|
|
11
|
-
* import { createImpersonateTool } from '@buoy-gg/impersonate';
|
|
12
|
-
*
|
|
13
|
-
* const impersonateTool = createImpersonateTool({
|
|
14
|
-
* onSearchUsers: async (query) => {
|
|
15
|
-
* const res = await api.searchUsers(query);
|
|
16
|
-
* return res.users;
|
|
17
|
-
* },
|
|
18
|
-
* onClearReactQuery: () => queryClient.clear(),
|
|
19
|
-
* });
|
|
20
|
-
*
|
|
21
|
-
* <FloatingDevTools apps={[impersonateTool]} />
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
// =============================================================================
|
|
26
|
-
// FACTORY (Primary entry point)
|
|
27
|
-
// =============================================================================
|
|
28
|
-
|
|
29
|
-
export { createImpersonateTool, ImpersonateIcon } from "./preset";
|
|
30
|
-
|
|
31
|
-
// =============================================================================
|
|
32
|
-
// EXTERNAL SYNC (Adapter for @buoy-gg/external-sync's useExternalSync)
|
|
33
|
-
// =============================================================================
|
|
34
|
-
|
|
35
|
-
export { createImpersonateSyncAdapter, impersonateSyncAdapter } from "./sync/impersonateSyncAdapter";
|
|
36
|
-
|
|
37
|
-
// =============================================================================
|
|
38
|
-
// COMPONENTS (For custom UI implementations)
|
|
39
|
-
// =============================================================================
|
|
40
|
-
|
|
41
|
-
// `ImpersonateToolView` is the same tool rendered inline (header bar + body)
|
|
42
|
-
// instead of inside a JsModal — for hosts that show tools in a panel.
|
|
43
|
-
export { ImpersonateModal, ImpersonateToolView } from "./impersonate/components/ImpersonateModal";
|
|
44
|
-
export { ImpersonateBanner, ImpersonateBannerMinimal } from "./impersonate/components/ImpersonateBanner";
|
|
45
|
-
/**
|
|
46
|
-
* Auto-rendered by FloatingDevTools when this package is installed — the
|
|
47
|
-
* host does not need to mount it. Renders the floating impersonation
|
|
48
|
-
* banner, which is only visible while an impersonation session is active.
|
|
49
|
-
*/
|
|
50
|
-
export { ImpersonateOverlay } from "./impersonate/components/ImpersonateOverlay";
|
|
51
|
-
export { UserSearchView } from "./impersonate/components/UserSearchView";
|
|
52
|
-
export { DataNukeSettings as DataNukeSettingsComponent } from "./impersonate/components/DataNukeSettings";
|
|
53
|
-
export { ImpersonateHistoryList } from "./impersonate/components/ImpersonateHistoryList";
|
|
54
|
-
export { ImpersonateStatusBar } from "./impersonate/components/ImpersonateStatusBar";
|
|
55
|
-
|
|
56
|
-
// =============================================================================
|
|
57
|
-
// HOOKS (For consuming impersonation state in custom components)
|
|
58
|
-
// =============================================================================
|
|
59
|
-
|
|
60
|
-
export { useImpersonate } from "./impersonate/hooks/useImpersonate";
|
|
61
|
-
export { useImpersonateHistory } from "./impersonate/hooks/useImpersonateHistory";
|
|
62
|
-
|
|
63
|
-
// =============================================================================
|
|
64
|
-
// TYPES (For TypeScript users)
|
|
65
|
-
// =============================================================================
|
|
66
|
-
|
|
67
|
-
// =============================================================================
|
|
68
|
-
// LOW-LEVEL EXPORTS (For advanced use cases)
|
|
69
|
-
// =============================================================================
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Direct access to the impersonation store singleton.
|
|
73
|
-
* Use this for custom integrations or debugging.
|
|
74
|
-
*
|
|
75
|
-
* @example
|
|
76
|
-
* ```typescript
|
|
77
|
-
* import { impersonateStore } from '@buoy-gg/impersonate';
|
|
78
|
-
*
|
|
79
|
-
* // Check if impersonating
|
|
80
|
-
* const state = impersonateStore.getState();
|
|
81
|
-
* if (state.isActive) {
|
|
82
|
-
* console.log('Impersonating:', state.currentUser?.email);
|
|
83
|
-
* }
|
|
84
|
-
*
|
|
85
|
-
* // Subscribe to changes
|
|
86
|
-
* const unsubscribe = impersonateStore.subscribe((state) => {
|
|
87
|
-
* console.log('Impersonation state changed:', state);
|
|
88
|
-
* });
|
|
89
|
-
* ```
|
|
90
|
-
*/
|
|
91
|
-
export { impersonateStore } from "./impersonate/utils/impersonateStore";
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Direct access to the impersonate listener for advanced control.
|
|
95
|
-
*
|
|
96
|
-
* @example
|
|
97
|
-
* ```typescript
|
|
98
|
-
* import {
|
|
99
|
-
* impersonateListener,
|
|
100
|
-
* startImpersonateListener,
|
|
101
|
-
* setImpersonateConfig,
|
|
102
|
-
* } from '@buoy-gg/impersonate';
|
|
103
|
-
*
|
|
104
|
-
* // Manually start the listener
|
|
105
|
-
* startImpersonateListener();
|
|
106
|
-
*
|
|
107
|
-
* // Configure impersonation
|
|
108
|
-
* setImpersonateConfig({
|
|
109
|
-
* headerKey: 'x-custom-impersonate-header',
|
|
110
|
-
* userId: 'user_123',
|
|
111
|
-
* });
|
|
112
|
-
*
|
|
113
|
-
* // Check status
|
|
114
|
-
* console.log('Listener active:', impersonateListener().isListening);
|
|
115
|
-
* ```
|
|
116
|
-
*/
|
|
117
|
-
export { impersonateListener, startImpersonateListener, stopImpersonateListener, setImpersonateConfig, isImpersonating, getImpersonatedUserId } from "./impersonate/utils/impersonateListener";
|
|
1
|
+
"use strict";export{createImpersonateTool,ImpersonateIcon}from"./preset";export{createImpersonateSyncAdapter,impersonateSyncAdapter}from"./sync/impersonateSyncAdapter";export{ImpersonateModal,ImpersonateToolView}from"./impersonate/components/ImpersonateModal";export{ImpersonateBanner,ImpersonateBannerMinimal}from"./impersonate/components/ImpersonateBanner";export{ImpersonateOverlay}from"./impersonate/components/ImpersonateOverlay";export{UserSearchView}from"./impersonate/components/UserSearchView";export{DataNukeSettings as DataNukeSettingsComponent}from"./impersonate/components/DataNukeSettings";export{ImpersonateHistoryList}from"./impersonate/components/ImpersonateHistoryList";export{ImpersonateStatusBar}from"./impersonate/components/ImpersonateStatusBar";export{useImpersonate}from"./impersonate/hooks/useImpersonate";export{useImpersonateHistory}from"./impersonate/hooks/useImpersonateHistory";export{impersonateStore}from"./impersonate/utils/impersonateStore";export{impersonateListener,startImpersonateListener,stopImpersonateListener,setImpersonateConfig,isImpersonating,getImpersonatedUserId}from"./impersonate/utils/impersonateListener";
|
package/lib/module/preset.js
CHANGED
|
@@ -1,216 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
import { getAsyncStorage } from './impersonate/hooks/asyncStoragePeer';
|
|
4
|
-
/**
|
|
5
|
-
* Impersonate Tool Preset
|
|
6
|
-
*
|
|
7
|
-
* Factory function to create a configured impersonate tool for FloatingDevTools.
|
|
8
|
-
* No default preset is exported because configuration (onSearchUsers) is required.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import React from "react";
|
|
12
|
-
import { ImpersonateModal } from "./impersonate/components/ImpersonateModal";
|
|
13
|
-
import { ImpersonateBanner } from "./impersonate/components/ImpersonateBanner";
|
|
14
|
-
import { impersonateStore } from "./impersonate/utils/impersonateStore";
|
|
15
|
-
import { impersonateListener } from "./impersonate/utils/impersonateListener";
|
|
16
|
-
import { registerImpersonateSearchUsers } from "./sync/impersonateSyncAdapter";
|
|
17
|
-
// =============================================================================
|
|
18
|
-
// EAGER BOOTSTRAP
|
|
19
|
-
// =============================================================================
|
|
20
|
-
// Patch fetch/XHR and restore persisted state the moment createImpersonateTool
|
|
21
|
-
// runs (typically at module load in the host app). If we waited until the
|
|
22
|
-
// modal mounts (the original lazy design), every request fired during app
|
|
23
|
-
// boot — bridge polls, user-data fetches, etc. — goes out without the
|
|
24
|
-
// impersonation header, even when the user already had impersonation active
|
|
25
|
-
// from a previous session. React Query then caches the real-account response,
|
|
26
|
-
// so even after the listener does eventually patch, no refetch happens.
|
|
27
|
-
//
|
|
28
|
-
// Side effect: any later fetch interceptor (e.g. @buoy-gg/network) installed
|
|
29
|
-
// AFTER us will wrap our wrapper, so its captured init.headers won't show the
|
|
30
|
-
// impersonation header. The wire request still includes it.
|
|
31
|
-
|
|
32
|
-
let bootstrapStarted = false;
|
|
33
|
-
async function bootstrapImpersonate() {
|
|
34
|
-
if (typeof window === 'undefined' || bootstrapStarted) return;
|
|
35
|
-
bootstrapStarted = true;
|
|
36
|
-
|
|
37
|
-
// eslint-disable-next-line no-console
|
|
38
|
-
console.log("[impersonate] bootstrap: start");
|
|
39
|
-
|
|
40
|
-
// Patch fetch/XHR synchronously so any request between now and the async
|
|
41
|
-
// storage load goes through our wrapper. The wrapper is a no-op until
|
|
42
|
-
// userId is set (which happens once initializeAsync resolves).
|
|
43
|
-
if (!impersonateListener().isListening) {
|
|
44
|
-
impersonateListener().startListening();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// React Native: require AsyncStorage. require() is more reliable under
|
|
48
|
-
// Metro than dynamic import(); on web this throws and the store falls
|
|
49
|
-
// back to localStorage handled synchronously in its constructor.
|
|
50
|
-
try {
|
|
51
|
-
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
52
|
-
const AsyncStorage = getAsyncStorage();
|
|
53
|
-
if (!AsyncStorage || typeof AsyncStorage.getItem !== "function") {
|
|
54
|
-
// eslint-disable-next-line no-console
|
|
55
|
-
console.warn("[impersonate] bootstrap: AsyncStorage shape unexpected", Object.keys(AsyncStorage ?? {}));
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
const adapter = {
|
|
59
|
-
getItem: key => AsyncStorage.getItem(key),
|
|
60
|
-
setItem: (key, value) => AsyncStorage.setItem(key, value)
|
|
61
|
-
};
|
|
62
|
-
impersonateStore.setAsyncStorage(adapter);
|
|
63
|
-
// eslint-disable-next-line no-console
|
|
64
|
-
console.log("[impersonate] bootstrap: AsyncStorage adapter installed");
|
|
65
|
-
await impersonateStore.initializeAsync(adapter);
|
|
66
|
-
// eslint-disable-next-line no-console
|
|
67
|
-
console.log("[impersonate] bootstrap: initializeAsync done");
|
|
68
|
-
} catch (e) {
|
|
69
|
-
// eslint-disable-next-line no-console
|
|
70
|
-
console.warn("[impersonate] bootstrap: failed", e);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// =============================================================================
|
|
75
|
-
// ICON COMPONENT
|
|
76
|
-
// =============================================================================
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Impersonate tool icon — a masked figure with a second identity behind it.
|
|
80
|
-
*
|
|
81
|
-
* The artwork lives in shared/icons/impersonate.json (Buoy Icon Format) and is
|
|
82
|
-
* generated into @buoy-gg/floating-tools-core, so React Native, the desktop
|
|
83
|
-
* dashboard and Flutter all render the same drawing. Edit the JSON, then
|
|
84
|
-
* `pnpm icons`.
|
|
85
|
-
*/
|
|
86
|
-
import { ImpersonateIcon, IMPERSONATE_ICON_COLOR } from "@buoy-gg/shared-ui";
|
|
87
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
88
|
-
export { ImpersonateIcon, IMPERSONATE_ICON_COLOR };
|
|
89
|
-
|
|
90
|
-
// =============================================================================
|
|
91
|
-
// WRAPPER COMPONENT
|
|
92
|
-
// =============================================================================
|
|
93
|
-
|
|
94
|
-
// =============================================================================
|
|
95
|
-
// FACTORY FUNCTION
|
|
96
|
-
// =============================================================================
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Create an impersonate tool with custom configuration
|
|
100
|
-
*
|
|
101
|
-
* The tool automatically displays a floating banner when impersonation is active.
|
|
102
|
-
* This can be toggled in the Settings tab or via developer defaults.
|
|
103
|
-
*
|
|
104
|
-
* @example
|
|
105
|
-
* ```tsx
|
|
106
|
-
* import { createImpersonateTool } from '@buoy-gg/impersonate';
|
|
107
|
-
* import { queryClient } from './queryClient';
|
|
108
|
-
*
|
|
109
|
-
* const impersonateTool = createImpersonateTool({
|
|
110
|
-
* onSearchUsers: async (query) => {
|
|
111
|
-
* const response = await api.searchUsers({ email: query });
|
|
112
|
-
* return response.users.map(u => ({
|
|
113
|
-
* id: u.id,
|
|
114
|
-
* displayName: u.name,
|
|
115
|
-
* email: u.email,
|
|
116
|
-
* metadata: { role: u.role, createdAt: u.createdAt }
|
|
117
|
-
* }));
|
|
118
|
-
* },
|
|
119
|
-
* onClearReactQuery: () => queryClient.clear(),
|
|
120
|
-
* onClearRedux: () => store.dispatch({ type: 'RESET' }),
|
|
121
|
-
*
|
|
122
|
-
* // Optional: Developer defaults (override hardcoded, overridden by persisted)
|
|
123
|
-
* defaults: {
|
|
124
|
-
* headerKey: 'x-admin-impersonate',
|
|
125
|
-
* showBanner: true, // Show floating banner when impersonating
|
|
126
|
-
* dataNukeSettings: {
|
|
127
|
-
* reactQuery: true,
|
|
128
|
-
* redux: false,
|
|
129
|
-
* },
|
|
130
|
-
* },
|
|
131
|
-
*
|
|
132
|
-
* // Optional: Hide settings tab for simple testing
|
|
133
|
-
* showSettingsTab: true,
|
|
134
|
-
* });
|
|
135
|
-
*
|
|
136
|
-
* // In your app — mount the banner globally (outside FloatingDevTools)
|
|
137
|
-
* // so it survives reloads and stays visible after the modal is closed.
|
|
138
|
-
* <FloatingDevTools apps={[impersonateTool]} />
|
|
139
|
-
* <ImpersonateBanner position="top" offset={50} />
|
|
140
|
-
* ```
|
|
141
|
-
*/
|
|
142
|
-
export function createImpersonateTool(config) {
|
|
143
|
-
const {
|
|
144
|
-
id = "impersonate",
|
|
145
|
-
name = "IMPERSONATE",
|
|
146
|
-
description = "User impersonation for admin testing",
|
|
147
|
-
defaults,
|
|
148
|
-
showSettingsTab = true,
|
|
149
|
-
onSearchUsers,
|
|
150
|
-
onClearReactQuery,
|
|
151
|
-
onClearRedux,
|
|
152
|
-
onClearAsyncStorage,
|
|
153
|
-
onClearMMKV
|
|
154
|
-
} = config;
|
|
155
|
-
|
|
156
|
-
// Initialize store with developer defaults if provided
|
|
157
|
-
if (defaults) {
|
|
158
|
-
impersonateStore.setDeveloperDefaults(defaults);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// Make the app's user search available to the zero-config external-sync
|
|
162
|
-
// adapter (FloatingDevTools auto-sync), so the desktop dashboard can proxy
|
|
163
|
-
// searches without separate wiring.
|
|
164
|
-
registerImpersonateSearchUsers(onSearchUsers ?? null);
|
|
165
|
-
|
|
166
|
-
// Patch fetch and restore persisted impersonation state up-front. Fire and
|
|
167
|
-
// forget — bootstrapImpersonate guards itself against double-invocation.
|
|
168
|
-
void bootstrapImpersonate();
|
|
169
|
-
|
|
170
|
-
// Create the tool configuration object
|
|
171
|
-
const tool = {
|
|
172
|
-
id,
|
|
173
|
-
name,
|
|
174
|
-
description,
|
|
175
|
-
slot: "both",
|
|
176
|
-
icon: ({
|
|
177
|
-
size,
|
|
178
|
-
slot
|
|
179
|
-
}) => /*#__PURE__*/_jsx(ImpersonateIcon, {
|
|
180
|
-
size: slot === "dial" ? size * 1.5 : size,
|
|
181
|
-
color: IMPERSONATE_ICON_COLOR
|
|
182
|
-
}),
|
|
183
|
-
component: props => /*#__PURE__*/_jsx(ImpersonateModal, {
|
|
184
|
-
...props,
|
|
185
|
-
onSearchUsers: onSearchUsers,
|
|
186
|
-
onClearReactQuery: onClearReactQuery,
|
|
187
|
-
onClearRedux: onClearRedux,
|
|
188
|
-
onClearAsyncStorage: onClearAsyncStorage,
|
|
189
|
-
onClearMMKV: onClearMMKV,
|
|
190
|
-
showSettingsTab: showSettingsTab
|
|
191
|
-
}),
|
|
192
|
-
props: {
|
|
193
|
-
enableSharedModalDimensions: false
|
|
194
|
-
},
|
|
195
|
-
// Export banner component for host app to use
|
|
196
|
-
Banner: ImpersonateBanner
|
|
197
|
-
};
|
|
198
|
-
return tool;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// =============================================================================
|
|
202
|
-
// STYLES
|
|
203
|
-
// =============================================================================
|
|
204
|
-
|
|
205
|
-
// =============================================================================
|
|
206
|
-
// NOTE: No default preset export
|
|
207
|
-
// =============================================================================
|
|
208
|
-
// Unlike other tools, the impersonate tool REQUIRES configuration
|
|
209
|
-
// (specifically the onSearchUsers callback). Therefore, we don't export
|
|
210
|
-
// a default preset. Users must call createImpersonateTool() with their config.
|
|
211
|
-
//
|
|
212
|
-
// If you want a basic preset for testing without search:
|
|
213
|
-
//
|
|
214
|
-
// export const impersonateToolPresetBasic = createImpersonateTool({
|
|
215
|
-
// onSearchUsers: async () => [],
|
|
216
|
-
// });
|
|
1
|
+
"use strict";import{getAsyncStorage as A}from"./impersonate/hooks/asyncStoragePeer";import O from"react";import{ImpersonateModal as C}from"./impersonate/components/ImpersonateModal";import{ImpersonateBanner as h}from"./impersonate/components/ImpersonateBanner";import{impersonateStore as s}from"./impersonate/utils/impersonateStore";import{impersonateListener as p}from"./impersonate/utils/impersonateListener";import{registerImpersonateSearchUsers as M}from"./sync/impersonateSyncAdapter";let m=false;async function R(){if(typeof window==="undefined"||m)return;m=true;console.log("[impersonate] bootstrap: start");if(!p().isListening){p().startListening()}try{const e=A();if(!e||typeof e.getItem!=="function"){console.warn("[impersonate] bootstrap: AsyncStorage shape unexpected",Object.keys(e??{}));return}const o={getItem:t=>e.getItem(t),setItem:(t,n)=>e.setItem(t,n)};s.setAsyncStorage(o);console.log("[impersonate] bootstrap: AsyncStorage adapter installed");await s.initializeAsync(o);console.log("[impersonate] bootstrap: initializeAsync done")}catch(e){console.warn("[impersonate] bootstrap: failed",e)}}import{ImpersonateIcon as c,IMPERSONATE_ICON_COLOR as l}from"@buoy-gg/shared-ui";import{jsx as f}from"react/jsx-runtime";export{c as ImpersonateIcon,l as IMPERSONATE_ICON_COLOR};export function createImpersonateTool(e){const{id:o="impersonate",name:t="IMPERSONATE",description:n="User impersonation for admin testing",defaults:a,showSettingsTab:d=true,onSearchUsers:i,onClearReactQuery:g,onClearRedux:u,onClearAsyncStorage:S,onClearMMKV:y}=e;if(a){s.setDeveloperDefaults(a)}M(i??null);void R();const I={id:o,name:t,description:n,slot:"both",icon:({size:r,slot:b})=>f(c,{size:b==="dial"?r*1.5:r,color:l}),component:r=>f(C,{...r,onSearchUsers:i,onClearReactQuery:g,onClearRedux:u,onClearAsyncStorage:S,onClearMMKV:y,showSettingsTab:d}),props:{enableSharedModalDimensions:false},Banner:h};return I}
|
|
@@ -1,162 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
import { isOverWireBudget } from "@buoy-gg/shared-ui";
|
|
4
|
-
import { impersonateStore } from "../impersonate/utils/impersonateStore";
|
|
5
|
-
// Registered by createImpersonateTool, so the zero-config adapter (see
|
|
6
|
-
// FloatingDevTools' auto external sync) can proxy the dashboard's user
|
|
7
|
-
// search without the app wiring anything twice.
|
|
8
|
-
let registeredSearchUsers = null;
|
|
9
|
-
|
|
10
|
-
/** @internal Called by createImpersonateTool with the app's onSearchUsers. */
|
|
11
|
-
export function registerImpersonateSearchUsers(handler) {
|
|
12
|
-
registeredSearchUsers = handler;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* `data:` avatar URLs (and any URL over this) are NOT copied onto the
|
|
17
|
-
* snapshot. UserCard renders initials, not the image, but apps still put
|
|
18
|
-
* base64 photos on `avatarUrl` / dump the API user into `metadata`. Ten
|
|
19
|
-
* history entries of those blow the 2MB emit budget and drop the panel.
|
|
20
|
-
*/
|
|
21
|
-
export const SNAPSHOT_URI_INLINE_LIMIT = 2 * 1024;
|
|
22
|
-
export const SNAPSHOT_METADATA_INLINE_LIMIT = 16 * 1024;
|
|
23
|
-
const METADATA_ON_DEVICE = Object.freeze({
|
|
24
|
-
__buoyOmitted: "user-metadata",
|
|
25
|
-
note: "User metadata stays on the device."
|
|
26
|
-
});
|
|
27
|
-
export function toWireUri(uri) {
|
|
28
|
-
if (!uri) return uri;
|
|
29
|
-
if (uri.startsWith("data:")) {
|
|
30
|
-
const comma = uri.indexOf(",");
|
|
31
|
-
const header = comma >= 0 ? uri.slice(0, Math.min(comma + 1, 64)) : "data:";
|
|
32
|
-
return `${header}[${uri.length} chars]`;
|
|
33
|
-
}
|
|
34
|
-
if (uri.length <= SNAPSHOT_URI_INLINE_LIMIT) return uri;
|
|
35
|
-
return `${uri.slice(0, SNAPSHOT_URI_INLINE_LIMIT)}… [${uri.length - SNAPSHOT_URI_INLINE_LIMIT} more]`;
|
|
36
|
-
}
|
|
37
|
-
function toWireMetadata(metadata) {
|
|
38
|
-
if (!metadata) return metadata;
|
|
39
|
-
const role = typeof metadata.role === "string" ? metadata.role : undefined;
|
|
40
|
-
if (!isOverWireBudget(metadata, SNAPSHOT_METADATA_INLINE_LIMIT)) return metadata;
|
|
41
|
-
// Keep `role` so UserCard still badges the impersonated user.
|
|
42
|
-
return role ? {
|
|
43
|
-
role,
|
|
44
|
-
...METADATA_ON_DEVICE
|
|
45
|
-
} : {
|
|
46
|
-
...METADATA_ON_DEVICE
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
function toWireUser(user) {
|
|
50
|
-
return {
|
|
51
|
-
...user,
|
|
52
|
-
avatarUrl: toWireUri(user.avatarUrl),
|
|
53
|
-
metadata: toWireMetadata(user.metadata)
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
function toWireState(state) {
|
|
57
|
-
return {
|
|
58
|
-
...state,
|
|
59
|
-
currentUser: state.currentUser ? toWireUser(state.currentUser) : null,
|
|
60
|
-
history: state.history.map(entry => ({
|
|
61
|
-
...entry,
|
|
62
|
-
user: toWireUser(entry.user)
|
|
63
|
-
}))
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Users this device has actually seen — search results from the app's own
|
|
69
|
-
* directory. `startImpersonation` refuses anything else: a live eval showed
|
|
70
|
-
* the model INVENTING a plausible user object when search returned nothing,
|
|
71
|
-
* and impersonating a fabricated user is a silent half-state (headers patched
|
|
72
|
-
* for a person who does not exist). History and the current user also count,
|
|
73
|
-
* so the desktop's re-impersonate-from-history flow keeps working.
|
|
74
|
-
*/
|
|
75
|
-
const seenUserIds = new Set();
|
|
76
|
-
function rememberUsers(users) {
|
|
77
|
-
for (const u of users) {
|
|
78
|
-
if (u && typeof u === "object" && u.id) seenUserIds.add(String(u.id));
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
function isKnownUser(user) {
|
|
82
|
-
if (!user || typeof user !== "object" || !user.id) return false;
|
|
83
|
-
if (seenUserIds.has(String(user.id))) return true;
|
|
84
|
-
const state = impersonateStore.getState();
|
|
85
|
-
if (state.currentUser?.id === user.id) return true;
|
|
86
|
-
return state.history.some(entry => entry.user.id === user.id);
|
|
87
|
-
}
|
|
88
|
-
function buildActions(onSearchUsers) {
|
|
89
|
-
return {
|
|
90
|
-
searchUsers: async params => {
|
|
91
|
-
const query = String(params?.query ?? "");
|
|
92
|
-
const users = await onSearchUsers(query);
|
|
93
|
-
if (Array.isArray(users)) rememberUsers(users);
|
|
94
|
-
return Array.isArray(users) ? users.map(toWireUser) : users;
|
|
95
|
-
},
|
|
96
|
-
startImpersonation: async params => {
|
|
97
|
-
const user = params?.user;
|
|
98
|
-
if (!isKnownUser(user)) {
|
|
99
|
-
throw new Error("That user isn't in this app's directory. Call searchUsers and impersonate one of its results — a user object that didn't come from search impersonates nobody.");
|
|
100
|
-
}
|
|
101
|
-
await impersonateStore.startImpersonation(user);
|
|
102
|
-
return {
|
|
103
|
-
ok: true,
|
|
104
|
-
userId: user.id
|
|
105
|
-
};
|
|
106
|
-
},
|
|
107
|
-
stopImpersonation: () => impersonateStore.stopImpersonation(),
|
|
108
|
-
pauseImpersonation: () => impersonateStore.pauseImpersonation(),
|
|
109
|
-
resumeImpersonation: () => impersonateStore.resumeImpersonation(),
|
|
110
|
-
updateSettings: params => {
|
|
111
|
-
// The documented shape is { settings: {...} }, but the flat patch is the
|
|
112
|
-
// obvious guess and used to crash with "Cannot read property 'headerKey'
|
|
113
|
-
// of undefined". Accept both; refuse anything else by name.
|
|
114
|
-
|
|
115
|
-
const p = params ?? {};
|
|
116
|
-
const settings = p.settings ?? (["headerKey", "ignorePatterns", "showBanner", "dataNukeSettings"].some(k => k in p) ? p : undefined);
|
|
117
|
-
if (!settings) throw new Error("updateSettings needs { settings: { headerKey?, ignorePatterns?, showBanner?, dataNukeSettings? } }.");
|
|
118
|
-
return impersonateStore.updateSettings(settings);
|
|
119
|
-
},
|
|
120
|
-
removeFromHistory: params => impersonateStore.removeFromHistory(params.userId),
|
|
121
|
-
clearHistory: () => impersonateStore.clearHistory()
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Create a sync adapter for the impersonate tool, consumed by
|
|
126
|
-
* @buoy-gg/external-sync's `useExternalSync` (structurally matches its
|
|
127
|
-
* ToolSyncAdapter interface so this package doesn't need a dependency on it).
|
|
128
|
-
*
|
|
129
|
-
* The dashboard mirrors the impersonation state and forwards every mutation
|
|
130
|
-
* back as an action — header patching, data nuking and persistence all run
|
|
131
|
-
* on the device, so impersonating from the desktop behaves exactly like
|
|
132
|
-
* tapping the modal on the phone.
|
|
133
|
-
*/
|
|
134
|
-
export function createImpersonateSyncAdapter({
|
|
135
|
-
onSearchUsers
|
|
136
|
-
}) {
|
|
137
|
-
return {
|
|
138
|
-
version: 1,
|
|
139
|
-
getSnapshot: () => toWireState(impersonateStore.getState()),
|
|
140
|
-
subscribe: onChange => impersonateStore.subscribe(onChange),
|
|
141
|
-
actions: buildActions(onSearchUsers)
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Zero-config variant used by FloatingDevTools' auto external sync. User
|
|
147
|
-
* search proxies to the onSearchUsers the app passed to
|
|
148
|
-
* createImpersonateTool; if the tool was never configured, the search action
|
|
149
|
-
* reports a descriptive error to the dashboard.
|
|
150
|
-
*/
|
|
151
|
-
export const impersonateSyncAdapter = {
|
|
152
|
-
dispose: () => impersonateStore.disposeAccountSession(),
|
|
153
|
-
version: 1,
|
|
154
|
-
getSnapshot: () => toWireState(impersonateStore.getState()),
|
|
155
|
-
subscribe: onChange => impersonateStore.subscribe(onChange),
|
|
156
|
-
actions: buildActions(query => {
|
|
157
|
-
if (!registeredSearchUsers) {
|
|
158
|
-
throw new Error("No onSearchUsers configured — pass it to createImpersonateTool()");
|
|
159
|
-
}
|
|
160
|
-
return registeredSearchUsers(query);
|
|
161
|
-
})
|
|
162
|
-
};
|
|
1
|
+
"use strict";import{isOverWireBudget as f}from"@buoy-gg/shared-ui";import{impersonateStore as n}from"../impersonate/utils/impersonateStore";let o=null;export function registerImpersonateSearchUsers(e){o=e}export const SNAPSHOT_URI_INLINE_LIMIT=2*1024;export const SNAPSHOT_METADATA_INLINE_LIMIT=16*1024;const a=Object.freeze({__buoyOmitted:"user-metadata",note:"User metadata stays on the device."});export function toWireUri(e){if(!e)return e;if(e.startsWith("data:")){const t=e.indexOf(",");const r=t>=0?e.slice(0,Math.min(t+1,64)):"data:";return`${r}[${e.length} chars]`}if(e.length<=SNAPSHOT_URI_INLINE_LIMIT)return e;return`${e.slice(0,SNAPSHOT_URI_INLINE_LIMIT)}\u2026 [${e.length-SNAPSHOT_URI_INLINE_LIMIT} more]`}function m(e){if(!e)return e;const t=typeof e.role==="string"?e.role:void 0;if(!f(e,SNAPSHOT_METADATA_INLINE_LIMIT))return e;return t?{role:t,...a}:{...a}}function i(e){return{...e,avatarUrl:toWireUri(e.avatarUrl),metadata:m(e.metadata)}}function c(e){return{...e,currentUser:e.currentUser?i(e.currentUser):null,history:e.history.map(t=>({...t,user:i(t.user)}))}}const u=new Set;function h(e){for(const t of e){if(t&&typeof t==="object"&&t.id)u.add(String(t.id))}}function g(e){if(!e||typeof e!=="object"||!e.id)return false;if(u.has(String(e.id)))return true;const t=n.getState();if(t.currentUser?.id===e.id)return true;return t.history.some(r=>r.user.id===e.id)}function d(e){return{searchUsers:async t=>{const r=String(t?.query??"");const s=await e(r);if(Array.isArray(s))h(s);return Array.isArray(s)?s.map(i):s},startImpersonation:async t=>{const r=t?.user;if(!g(r)){throw new Error("That user isn't in this app's directory. Call searchUsers and impersonate one of its results \u2014 a user object that didn't come from search impersonates nobody.")}await n.startImpersonation(r);return{ok:true,userId:r.id}},stopImpersonation:()=>n.stopImpersonation(),pauseImpersonation:()=>n.pauseImpersonation(),resumeImpersonation:()=>n.resumeImpersonation(),updateSettings:t=>{const r=t??{};const s=r.settings??(["headerKey","ignorePatterns","showBanner","dataNukeSettings"].some(p=>p in r)?r:void 0);if(!s)throw new Error("updateSettings needs { settings: { headerKey?, ignorePatterns?, showBanner?, dataNukeSettings? } }.");return n.updateSettings(s)},removeFromHistory:t=>n.removeFromHistory(t.userId),clearHistory:()=>n.clearHistory()}}export function createImpersonateSyncAdapter({onSearchUsers:e}){return{version:1,getSnapshot:()=>c(n.getState()),subscribe:t=>n.subscribe(t),actions:d(e)}}export const impersonateSyncAdapter={dispose:()=>n.disposeAccountSession(),version:1,getSnapshot:()=>c(n.getState()),subscribe:e=>n.subscribe(e),actions:d(e=>{if(!o){throw new Error("No onSearchUsers configured \u2014 pass it to createImpersonateTool()")}return o(e)})};
|