@qpjoy/mx-launcher-embed-sdk 0.2.0

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 ADDED
@@ -0,0 +1,176 @@
1
+ # @qpjoy/mx-launcher-embed-sdk
2
+
3
+ `launcher-embed-sdk` is the role SDK for applications hosted by a standalone
4
+ launcher. Embed apps are consumers. They do not own local networking, users,
5
+ permissions, release policy, downloads, or rollout decisions.
6
+
7
+ ## Allowed Responsibilities
8
+
9
+ Embed SDK may:
10
+
11
+ - discover a running standalone broker.
12
+ - connect and handshake with the broker.
13
+ - request scoped capabilities.
14
+ - subscribe to broker events.
15
+ - request app install, launch, update check, or update apply when granted.
16
+ - expose a safe preload bridge to renderer code.
17
+ - read signed local binding cache only to find the required standalone channel.
18
+
19
+ Embed SDK must not:
20
+
21
+ - apply WireGuard.
22
+ - write DNS, PAC, system proxy, or resolver state.
23
+ - allocate or claim a ProductNetwork IP.
24
+ - store long-lived Internal tokens.
25
+ - make independent rollout decisions.
26
+ - install or update itself without broker approval.
27
+
28
+ ## Bootstrap
29
+
30
+ Embed app startup:
31
+
32
+ 1. Read the embed manifest.
33
+ 2. If launched by a standalone broker, trust the launch envelope after signature
34
+ and nonce validation.
35
+ 3. Otherwise read signed local binding cache to find
36
+ `standaloneChannelProductId`.
37
+ 4. Find a compatible running broker in the channel registry.
38
+ 5. Handshake and request capabilities.
39
+ 6. If no broker is available, return a typed state such as
40
+ `standalone-required`, with enough metadata for UI to prompt the user to open
41
+ MX-H2I or the configured standalone.
42
+
43
+ Embed should not directly fetch privileged server policy before broker
44
+ connection. The broker refreshes Internal policy and returns scoped results.
45
+
46
+ ## Minimal API Shape
47
+
48
+ Future implementation should keep the public API small:
49
+
50
+ ```ts
51
+ import { createEmbedLauncher } from '@qpjoy/mx-launcher-embed-sdk';
52
+
53
+ const launcher = createEmbedLauncher({
54
+ productId: 'h2o',
55
+ standaloneChannelProductId: 'mx-h2i',
56
+ requiredCapabilities: ['user.session', 'network.proxy', 'network.status']
57
+ });
58
+
59
+ const connection = await launcher.connect();
60
+ if (!connection.ok) {
61
+ console.warn(connection.state, connection.message);
62
+ }
63
+
64
+ launcher.on('auth.changed', (event) => {});
65
+ launcher.on('release.available', (event) => {});
66
+
67
+ await launcher.request('network.proxy', {
68
+ url: '/internal/v1/h2o/tasks'
69
+ });
70
+ ```
71
+
72
+ Renderer APIs should be exposed through preload with explicit allowlists. Do not
73
+ expose a raw IPC or transport object to untrusted renderer code.
74
+
75
+ ## Runtime Contract v0.1
76
+
77
+ The shared core package exposes `createLauncherEmbedManifest()` and
78
+ `launcherEmbedRuntimeContractVersion`. AppCenter records, admin records, and
79
+ embed apps should use this shape for the first product-grade contract:
80
+
81
+ - `runtimeContractVersion`: `0.1`
82
+ - `launcherMode`: `embed`
83
+ - `network.scope`: `broker-session`
84
+ - `embed.standaloneChannelProductId`: the owning standalone channel, for
85
+ example `mx-h2i`
86
+ - `requiredCapabilities`: the broker-scoped API surface the app needs
87
+
88
+ The first event vocabulary is intentionally small:
89
+
90
+ - `embed.init`, `embed.ready`, `embed.error`, `embed.logs`
91
+ - `auth.changed`, `network.changed`, `permission.changed`
92
+ - `app.open`, `app.close`
93
+
94
+ The first request vocabulary is also broker-owned:
95
+
96
+ - `user.session`
97
+ - `network.status`
98
+ - `network.proxy`
99
+ - `network.dns.policy`
100
+ - `network.pac.policy`
101
+ - `app.open`, `app.close`, `app.logs`, `app.update.check`
102
+
103
+ An embed app receives state through the broker session or through a preload
104
+ bridge owned by its Electron main process. It should not read WireGuard keys,
105
+ touch routes, apply PAC, or write DNS directly.
106
+
107
+ The high-level embed API is broker-session based. The package still exposes a
108
+ `legacyNetwork` object for migration tests that need to call the old
109
+ Internal lease APIs explicitly, but production embed apps should not allocate a
110
+ runtime lease or apply WireGuard through that path.
111
+
112
+ ## State Model
113
+
114
+ Use explicit states instead of a single `connected` boolean:
115
+
116
+ ```txt
117
+ idle
118
+ discovering-broker
119
+ standalone-required
120
+ broker-incompatible
121
+ capability-denied
122
+ connected
123
+ network-ready
124
+ blocked
125
+ disconnected
126
+ ```
127
+
128
+ `connected` means the broker handshake succeeded. It does not mean every
129
+ capability is ready. For example, an embed app may be connected while
130
+ `network.proxy` is blocked by policy or by standalone data-plane repair.
131
+
132
+ ## AppCenter And H2O
133
+
134
+ AppCenter is an embed app with elevated capabilities such as `catalog.read`,
135
+ `app.install`, `app.launch`, and `app.update.manage`. It still depends on the
136
+ MX-H2I standalone broker for execution.
137
+
138
+ H2O is **Home To Oversea**, an AppCenter built-in embed network plugin. It is a
139
+ Clash-like user surface for proxy mode, PAC, Split DNS, and Internal/oversea
140
+ status, but it should still use the same broker-session contract as other
141
+ AppCenter applications. H2O should only request the capabilities it needs and
142
+ should not know whether MX-H2I uses WireGuard, PAC, system proxy, or another
143
+ future data-plane implementation.
144
+
145
+ ## Release Behavior
146
+
147
+ Embed apps are distributed as signed bundles. The embed SDK can ask for update
148
+ status, but installation and rollback are broker-owned:
149
+
150
+ ```txt
151
+ embed app -> app.update.check -> standalone broker -> release policy -> OSS/CDN
152
+ standalone broker -> verify signature/hash -> install cache -> launch envelope
153
+ ```
154
+
155
+ The SDK npm version is not the user-facing app version. Embed manifests should
156
+ record both:
157
+
158
+ - `sdkVersion`: build-time SDK package version.
159
+ - `appVersion`: user-facing embed application version.
160
+
161
+ ## K8s Admin Expectations
162
+
163
+ K8s admin should show, for every embed app:
164
+
165
+ - configured standalone channel.
166
+ - required capabilities.
167
+ - granted capabilities.
168
+ - broker compatibility.
169
+ - active install version.
170
+ - latest eligible release.
171
+ - rollout decision.
172
+ - last launch error.
173
+ - audit history.
174
+
175
+ Admin operations should target the standalone broker, not the embed process, for
176
+ privileged work such as install, update, rollback, and cache refresh.
@@ -0,0 +1,109 @@
1
+ import { routePlanFromSnapshot, shouldRefreshNetwork, type AnonymousEnrollment, type AnonymousEnrollmentRequest, type LauncherBrokerCapabilitySession, type LauncherBrokerHandshakeDeniedReason, type LauncherCapability, type LauncherChannelRecord, type LauncherClient, type LauncherClientOptions, type LauncherEmbedConnectionState, type LauncherManifest, type LauncherNetworkLease, type LauncherNetworkLeaseInput, type LauncherNetworkSession, type LauncherNetworkSnapshot, type LauncherNetworkSnapshotInput, type LauncherProductNetwork, type LauncherRoutePlan, type LauncherWireGuardKeyPair, type LauncherWireGuardKeyProvider } from '@qpjoy/mx-launcher-core';
2
+ export interface EmbedLauncherOptions extends LauncherClientOptions {
3
+ productId?: string;
4
+ appId?: string;
5
+ standaloneChannelProductId?: string;
6
+ requiredCapabilities?: LauncherCapability[];
7
+ sdkVersion?: string;
8
+ appVersion?: string;
9
+ displayName?: string;
10
+ description?: string;
11
+ packageName?: string;
12
+ category?: string;
13
+ protocolVersion?: string;
14
+ minBrokerAbiVersion?: string;
15
+ runtimeContractVersion?: string;
16
+ launchWithoutBroker?: 'blocked' | 'prompt-open-standalone';
17
+ channelRegistry?: LauncherChannelRegistrySource;
18
+ maxChannelHeartbeatAgeMs?: number;
19
+ requestImpl?: EmbedBrokerRequestHandler;
20
+ installId?: string;
21
+ deviceId?: string;
22
+ siteId?: string;
23
+ publicKey?: string;
24
+ privateKey?: string;
25
+ keyPair?: LauncherWireGuardKeyPair;
26
+ keyProvider?: LauncherWireGuardKeyProvider;
27
+ deviceLabel?: string;
28
+ }
29
+ export interface EmbedSnapshotOptions extends Omit<LauncherNetworkSnapshotInput, 'appId' | 'launcherMode' | 'installId' | 'deviceId' | 'siteId' | 'publicKey'> {
30
+ installId?: string;
31
+ deviceId?: string;
32
+ siteId?: string | null;
33
+ publicKey?: string | null;
34
+ }
35
+ export interface EmbedLeaseOptions extends Omit<LauncherNetworkLeaseInput, 'productId' | 'mode' | 'installId' | 'deviceId' | 'siteId' | 'publicKey'> {
36
+ installId?: string;
37
+ deviceId?: string;
38
+ siteId?: string | null;
39
+ publicKey?: string | null;
40
+ }
41
+ export interface EmbedNetworkSessionOptions extends EmbedLeaseOptions {
42
+ privateKey?: string | null;
43
+ keyPair?: LauncherWireGuardKeyPair | null;
44
+ keyProvider?: LauncherWireGuardKeyProvider | null;
45
+ snapshotRequestId?: string | null;
46
+ }
47
+ export type LauncherChannelRegistrySource = LauncherChannelRecord[] | (() => LauncherChannelRecord[] | Promise<LauncherChannelRecord[]>);
48
+ export type EmbedBrokerRequestHandler = (session: LauncherBrokerCapabilitySession, requestName: string, payload?: unknown) => unknown | Promise<unknown>;
49
+ export interface EmbedConnectOptions extends Omit<AnonymousEnrollmentRequest, 'productId' | 'platform' | 'installId' | 'deviceId' | 'siteId' | 'publicKey'> {
50
+ installId?: string;
51
+ deviceId?: string;
52
+ siteId?: string;
53
+ publicKey?: string;
54
+ platform?: string;
55
+ }
56
+ export interface EmbedConnectionResult {
57
+ enrollment: AnonymousEnrollment;
58
+ snapshot: LauncherNetworkSnapshot;
59
+ routePlan: LauncherRoutePlan;
60
+ }
61
+ export interface EmbedBrokerConnectOptions {
62
+ requestedCapabilities?: LauncherCapability[];
63
+ channelRegistry?: LauncherChannelRegistrySource;
64
+ installId?: string | null;
65
+ deviceId?: string | null;
66
+ userId?: string | null;
67
+ requestId?: string | null;
68
+ }
69
+ export interface EmbedLauncherConnection {
70
+ ok: boolean;
71
+ state: LauncherEmbedConnectionState;
72
+ session: LauncherBrokerCapabilitySession | null;
73
+ reason: LauncherBrokerHandshakeDeniedReason | null;
74
+ message: string;
75
+ channel: LauncherChannelRecord | null;
76
+ missingCapabilities: LauncherCapability[];
77
+ }
78
+ export type EmbedLauncherEventName = 'broker.connected' | 'broker.disconnected' | 'auth.changed' | 'capability.changed' | 'network.ready' | 'network.blocked' | 'release.available' | 'app.install.progress' | 'app.updated';
79
+ export type EmbedLauncherEventHandler = (event: {
80
+ name: EmbedLauncherEventName;
81
+ state: LauncherEmbedConnectionState;
82
+ session: LauncherBrokerCapabilitySession | null;
83
+ payload?: unknown;
84
+ }) => void;
85
+ export interface EmbedLegacyNetworkApi {
86
+ getProduct(): Promise<LauncherProductNetwork>;
87
+ enrollLease(options?: EmbedLeaseOptions): Promise<LauncherNetworkLease>;
88
+ connectNetwork(options?: EmbedNetworkSessionOptions): Promise<LauncherNetworkSession>;
89
+ createSnapshot(options?: EmbedSnapshotOptions): Promise<LauncherNetworkSnapshot>;
90
+ createRoutePlan(options?: EmbedSnapshotOptions): Promise<LauncherRoutePlan>;
91
+ connectAnonymous(options?: EmbedConnectOptions): Promise<EmbedConnectionResult>;
92
+ }
93
+ export interface EmbedLauncher {
94
+ readonly mode: 'embed';
95
+ readonly productId: string;
96
+ readonly appId: string;
97
+ readonly standaloneChannelProductId: string;
98
+ readonly requiredCapabilities: LauncherCapability[];
99
+ readonly manifest: LauncherManifest;
100
+ readonly client: LauncherClient;
101
+ readonly legacyNetwork: EmbedLegacyNetworkApi;
102
+ connect(options?: EmbedBrokerConnectOptions): Promise<EmbedLauncherConnection>;
103
+ request<T = unknown>(requestName: string, payload?: unknown): Promise<T>;
104
+ on(eventName: EmbedLauncherEventName, handler: EmbedLauncherEventHandler): () => void;
105
+ off(eventName: EmbedLauncherEventName, handler: EmbedLauncherEventHandler): void;
106
+ shouldRefreshNetwork: typeof shouldRefreshNetwork;
107
+ }
108
+ export declare function createEmbedLauncher(options: EmbedLauncherOptions): EmbedLauncher;
109
+ export { routePlanFromSnapshot, shouldRefreshNetwork };
package/dist/index.js ADDED
@@ -0,0 +1,284 @@
1
+ import { brokerAbiVersion, createLauncherEmbedManifest, createLauncherClient, createLauncherNetworkSession, launcherEmbedRuntimeContractVersion, launcherBrokerCompatibility, launcherProtocolVersion, routePlanFromSnapshot, shouldRefreshNetwork } from '@qpjoy/mx-launcher-core';
2
+ export function createEmbedLauncher(options) {
3
+ const productId = requiredProductId(options.productId ?? options.appId);
4
+ const appId = requiredProductId(options.appId ?? options.productId);
5
+ const standaloneChannelProductId = normalizeProductId(options.standaloneChannelProductId ?? 'mx-h2i');
6
+ const requiredCapabilities = uniqueCapabilities(options.requiredCapabilities ?? ['user.session']);
7
+ const client = createLauncherClient(options);
8
+ const manifest = createLauncherEmbedManifest({
9
+ appId,
10
+ productId,
11
+ displayName: options.displayName,
12
+ description: options.description,
13
+ packageName: options.packageName,
14
+ category: options.category,
15
+ sdkVersion: options.sdkVersion,
16
+ appVersion: options.appVersion,
17
+ protocolVersion: options.protocolVersion,
18
+ sdkAbiVersion: options.minBrokerAbiVersion ?? brokerAbiVersion,
19
+ runtimeContractVersion: options.runtimeContractVersion ?? launcherEmbedRuntimeContractVersion,
20
+ standaloneChannelProductId,
21
+ launchWithoutBroker: options.launchWithoutBroker,
22
+ requiredCapabilities
23
+ });
24
+ const handlers = new Map();
25
+ let currentState = 'idle';
26
+ let currentSession = null;
27
+ async function createSnapshot(input = {}) {
28
+ return client.createSnapshot({
29
+ ...input,
30
+ appId: productId,
31
+ launcherMode: 'embed',
32
+ installId: input.installId ?? options.installId,
33
+ deviceId: input.deviceId ?? options.deviceId,
34
+ siteId: input.siteId ?? options.siteId,
35
+ publicKey: input.publicKey ?? options.publicKey
36
+ });
37
+ }
38
+ async function enrollLease(input = {}) {
39
+ return client.enrollLease({
40
+ ...input,
41
+ appId: input.appId ?? productId,
42
+ productId,
43
+ mode: 'embed',
44
+ installId: input.installId ?? options.installId,
45
+ deviceId: input.deviceId ?? options.deviceId,
46
+ siteId: input.siteId ?? options.siteId,
47
+ publicKey: input.publicKey ?? options.publicKey,
48
+ deviceLabel: input.deviceLabel ?? options.deviceLabel,
49
+ requestedBy: input.requestedBy ?? 'embed-sdk'
50
+ });
51
+ }
52
+ async function connectNetwork(input = {}) {
53
+ return createLauncherNetworkSession(client, {
54
+ ...input,
55
+ productId,
56
+ appId: productId,
57
+ mode: 'embed',
58
+ launcherMode: 'embed',
59
+ installId: input.installId ?? options.installId,
60
+ deviceId: input.deviceId ?? options.deviceId,
61
+ siteId: input.siteId ?? options.siteId,
62
+ publicKey: input.publicKey ?? options.publicKey,
63
+ privateKey: input.privateKey ?? options.privateKey,
64
+ keyPair: input.keyPair ?? options.keyPair,
65
+ keyProvider: input.keyProvider ?? options.keyProvider,
66
+ deviceLabel: input.deviceLabel ?? options.deviceLabel,
67
+ requestedBy: input.requestedBy ?? 'embed-sdk'
68
+ });
69
+ }
70
+ function setState(state, eventName, payload) {
71
+ currentState = state;
72
+ emit(eventName, payload);
73
+ }
74
+ function emit(name, payload) {
75
+ const event = { name, state: currentState, session: currentSession, payload };
76
+ for (const handler of handlers.get(name) ?? [])
77
+ handler(event);
78
+ }
79
+ const legacyNetwork = {
80
+ getProduct() {
81
+ return client.getProduct(productId);
82
+ },
83
+ enrollLease,
84
+ connectNetwork,
85
+ createSnapshot,
86
+ async createRoutePlan(input) {
87
+ return routePlanFromSnapshot(await createSnapshot(input));
88
+ },
89
+ async connectAnonymous(input = {}) {
90
+ const enrollment = await client.enrollAnonymous({
91
+ ...input,
92
+ productId,
93
+ platform: input.platform ?? 'embed',
94
+ installId: input.installId ?? options.installId,
95
+ deviceId: input.deviceId ?? options.deviceId,
96
+ siteId: input.siteId ?? options.siteId,
97
+ publicKey: input.publicKey ?? options.publicKey,
98
+ deviceLabel: input.deviceLabel ?? options.deviceLabel,
99
+ relayMode: input.relayMode ?? 'h2i'
100
+ });
101
+ const snapshot = await createSnapshot({
102
+ installId: enrollment.installId,
103
+ deviceId: enrollment.deviceId,
104
+ siteId: enrollment.siteId,
105
+ publicKey: enrollment.publicKey ?? input.publicKey ?? options.publicKey,
106
+ requestId: input.requestId
107
+ });
108
+ return {
109
+ enrollment,
110
+ snapshot,
111
+ routePlan: routePlanFromSnapshot(snapshot)
112
+ };
113
+ }
114
+ };
115
+ const launcher = {
116
+ mode: 'embed',
117
+ productId,
118
+ appId,
119
+ standaloneChannelProductId,
120
+ requiredCapabilities,
121
+ manifest,
122
+ client,
123
+ legacyNetwork,
124
+ async connect(input = {}) {
125
+ setState('discovering-broker', 'broker.disconnected');
126
+ const requestedCapabilities = uniqueCapabilities(input.requestedCapabilities ?? requiredCapabilities);
127
+ const registry = await resolveChannelRegistry(input.channelRegistry ?? options.channelRegistry);
128
+ const channel = registry.find((record) => normalizeProductId(record.productId) === standaloneChannelProductId && channelIsFresh(record, options.maxChannelHeartbeatAgeMs)) ?? null;
129
+ if (!channel) {
130
+ currentSession = null;
131
+ setState('standalone-required', 'network.blocked', { standaloneChannelProductId });
132
+ return deniedConnection('standalone-required', `Open ${standaloneChannelProductId} before launching ${appId}.`, null, []);
133
+ }
134
+ const request = buildHandshakeRequest({
135
+ appId,
136
+ productId,
137
+ standaloneChannelProductId,
138
+ requestedCapabilities,
139
+ options,
140
+ input
141
+ });
142
+ const compatibility = launcherBrokerCompatibility({
143
+ embedProtocolVersion: request.protocolVersion,
144
+ brokerProtocolVersion: channel.protocolVersion,
145
+ minBrokerAbiVersion: request.minBrokerAbiVersion,
146
+ brokerAbiVersion: channel.brokerAbiVersion
147
+ });
148
+ if (!compatibility.compatible) {
149
+ currentSession = null;
150
+ setState('broker-incompatible', 'network.blocked', compatibility);
151
+ return deniedConnection('broker-incompatible', compatibility.message, channel, []);
152
+ }
153
+ const missingCapabilities = requestedCapabilities.filter((capability) => !channel.capabilities.includes(capability));
154
+ if (missingCapabilities.length) {
155
+ currentSession = null;
156
+ setState('capability-denied', 'capability.changed', { missingCapabilities });
157
+ return deniedConnection('capability-denied', `Broker channel ${channel.productId} does not grant: ${missingCapabilities.join(', ')}.`, channel, missingCapabilities);
158
+ }
159
+ const session = buildBrokerSession({
160
+ appId,
161
+ productId,
162
+ standaloneChannelProductId,
163
+ channel,
164
+ grantedCapabilities: requestedCapabilities,
165
+ installId: input.installId ?? options.installId ?? null,
166
+ deviceId: input.deviceId ?? options.deviceId ?? null,
167
+ userId: input.userId ?? null
168
+ });
169
+ currentSession = session;
170
+ setState('connected', 'broker.connected', session);
171
+ if (requestedCapabilities.includes('network.status') || requestedCapabilities.includes('network.proxy')) {
172
+ setState('network-ready', 'network.ready', session);
173
+ }
174
+ return {
175
+ ok: true,
176
+ state: currentState,
177
+ session,
178
+ reason: null,
179
+ message: 'Connected to standalone broker channel.',
180
+ channel,
181
+ missingCapabilities: []
182
+ };
183
+ },
184
+ async request(requestName, payload) {
185
+ if (!currentSession) {
186
+ throw new Error('Embed launcher is not connected to a standalone broker');
187
+ }
188
+ if (!currentSession.grantedCapabilities.includes(requestName)) {
189
+ throw new Error(`Capability is not granted for embed broker session: ${requestName}`);
190
+ }
191
+ if (!options.requestImpl) {
192
+ throw new Error('No embed broker request transport is configured');
193
+ }
194
+ return options.requestImpl(currentSession, requestName, payload);
195
+ },
196
+ on(eventName, handler) {
197
+ const set = handlers.get(eventName) ?? new Set();
198
+ set.add(handler);
199
+ handlers.set(eventName, set);
200
+ return () => launcher.off(eventName, handler);
201
+ },
202
+ off(eventName, handler) {
203
+ handlers.get(eventName)?.delete(handler);
204
+ },
205
+ shouldRefreshNetwork
206
+ };
207
+ return launcher;
208
+ }
209
+ export { routePlanFromSnapshot, shouldRefreshNetwork };
210
+ function requiredProductId(productId) {
211
+ const trimmed = productId?.trim();
212
+ if (!trimmed)
213
+ throw new Error('Embed launcher productId is required');
214
+ return trimmed;
215
+ }
216
+ function normalizeProductId(productId) {
217
+ const normalized = productId.trim().toLowerCase();
218
+ if (!normalized)
219
+ throw new Error('Launcher productId is required');
220
+ return normalized;
221
+ }
222
+ function uniqueCapabilities(capabilities) {
223
+ return [...new Set(capabilities.map((capability) => String(capability || '').trim()).filter(Boolean))];
224
+ }
225
+ async function resolveChannelRegistry(source) {
226
+ if (!source)
227
+ return [];
228
+ const records = typeof source === 'function' ? await source() : source;
229
+ return records.filter((record) => record && record.productId && record.socketPath);
230
+ }
231
+ function channelIsFresh(record, maxAgeMs) {
232
+ if (!maxAgeMs || maxAgeMs <= 0)
233
+ return true;
234
+ const heartbeat = Date.parse(record.heartbeatAt);
235
+ if (!Number.isFinite(heartbeat))
236
+ return false;
237
+ return Date.now() - heartbeat <= maxAgeMs;
238
+ }
239
+ function buildHandshakeRequest(input) {
240
+ return {
241
+ appId: input.appId,
242
+ productId: input.productId,
243
+ launcherMode: 'embed',
244
+ sdkVersion: input.options.sdkVersion,
245
+ appVersion: input.options.appVersion,
246
+ protocolVersion: input.options.protocolVersion ?? launcherProtocolVersion,
247
+ minBrokerAbiVersion: input.options.minBrokerAbiVersion ?? brokerAbiVersion,
248
+ standaloneChannelProductId: input.standaloneChannelProductId,
249
+ requestedCapabilities: input.requestedCapabilities,
250
+ installId: input.input.installId ?? input.options.installId ?? null,
251
+ deviceId: input.input.deviceId ?? input.options.deviceId ?? null,
252
+ userId: input.input.userId ?? null,
253
+ requestId: input.input.requestId ?? null
254
+ };
255
+ }
256
+ function buildBrokerSession(input) {
257
+ return {
258
+ sessionId: `embed_${input.appId}_${input.channel.instanceId}`,
259
+ appId: input.appId,
260
+ productId: input.productId,
261
+ launcherMode: 'embed',
262
+ networkScope: 'broker-session',
263
+ standaloneChannelProductId: input.standaloneChannelProductId,
264
+ channel: input.channel,
265
+ grantedCapabilities: input.grantedCapabilities,
266
+ deniedCapabilities: [],
267
+ userId: input.userId,
268
+ installId: input.installId,
269
+ deviceId: input.deviceId,
270
+ issuedAt: new Date().toISOString(),
271
+ expiresAt: null
272
+ };
273
+ }
274
+ function deniedConnection(reason, message, channel, missingCapabilities) {
275
+ return {
276
+ ok: false,
277
+ state: reason === 'broker-incompatible' ? 'broker-incompatible' : reason === 'capability-denied' ? 'capability-denied' : 'standalone-required',
278
+ session: null,
279
+ reason,
280
+ message,
281
+ channel,
282
+ missingCapabilities
283
+ };
284
+ }
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@qpjoy/mx-launcher-embed-sdk",
3
+ "version": "0.2.0",
4
+ "description": "Embed SDK shell for products that carry MX Launcher network capability inside the app.",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "README.md",
17
+ "package.json"
18
+ ],
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "dependencies": {
23
+ "@qpjoy/mx-launcher-core": "^0.2.0"
24
+ },
25
+ "devDependencies": {
26
+ "typescript": "^5.7.3"
27
+ },
28
+ "scripts": {
29
+ "build": "tsc -p tsconfig.json",
30
+ "typecheck": "tsc -p tsconfig.json --noEmit"
31
+ }
32
+ }