@superwall/runtime 1.1.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/LICENSE +105 -0
- package/README.md +93 -0
- package/dist/bridge/base64.d.ts +9 -0
- package/dist/bridge/base64.js +34 -0
- package/dist/bridge/index.d.ts +30 -0
- package/dist/bridge/index.js +49 -0
- package/dist/bridge/transport.d.ts +14 -0
- package/dist/bridge/transport.js +43 -0
- package/dist/config.d.ts +237 -0
- package/dist/config.js +138 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +11 -0
- package/dist/localization/index.d.ts +24 -0
- package/dist/localization/index.js +71 -0
- package/dist/localization/periods.d.ts +1 -0
- package/dist/localization/periods.js +753 -0
- package/dist/mock.d.ts +74 -0
- package/dist/mock.js +206 -0
- package/dist/products.d.ts +62 -0
- package/dist/products.js +105 -0
- package/dist/session/index.d.ts +103 -0
- package/dist/session/index.js +732 -0
- package/dist/session/state.d.ts +27 -0
- package/dist/session/state.js +60 -0
- package/dist/session/types.d.ts +93 -0
- package/dist/session/types.js +1 -0
- package/dist/session/variables.d.ts +121 -0
- package/dist/session/variables.js +5 -0
- package/package.json +51 -0
- package/src/bridge/base64.ts +56 -0
- package/src/bridge/index.ts +77 -0
- package/src/bridge/transport.ts +63 -0
- package/src/config.ts +408 -0
- package/src/index.ts +57 -0
- package/src/localization/index.ts +110 -0
- package/src/localization/periods.ts +790 -0
- package/src/mock.ts +333 -0
- package/src/products.ts +221 -0
- package/src/session/index.ts +1066 -0
- package/src/session/state.ts +112 -0
- package/src/session/types.ts +123 -0
- package/src/session/variables.ts +139 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { TIncomingMessageCallbackResult, TPermissionStatus } from "@superwall/protocol";
|
|
2
|
+
import type { SuperwallProduct } from "../products";
|
|
3
|
+
export type SuperwallSdkState = Readonly<Record<string, unknown>>;
|
|
4
|
+
export type SuperwallPermissionStatuses = Readonly<Record<string, TPermissionStatus>>;
|
|
5
|
+
export type SuperwallCallbackState = Readonly<{
|
|
6
|
+
result: TIncomingMessageCallbackResult["status"];
|
|
7
|
+
data?: Record<string, unknown>;
|
|
8
|
+
}>;
|
|
9
|
+
export type SuperwallCallbackStates = Readonly<Record<string, SuperwallCallbackState>>;
|
|
10
|
+
export type BuildSuperwallStateOptions<TProduct extends SuperwallProduct = SuperwallProduct> = {
|
|
11
|
+
variables: Readonly<Record<string, unknown>>;
|
|
12
|
+
products: readonly TProduct[];
|
|
13
|
+
paywall?: {
|
|
14
|
+
id: string;
|
|
15
|
+
identifier: string;
|
|
16
|
+
};
|
|
17
|
+
lastTransaction?: {
|
|
18
|
+
status: "completed" | "abandoned";
|
|
19
|
+
product: TProduct;
|
|
20
|
+
};
|
|
21
|
+
permissionStatuses?: SuperwallPermissionStatuses;
|
|
22
|
+
callbackStates?: SuperwallCallbackStates;
|
|
23
|
+
trial?: {
|
|
24
|
+
eligible?: boolean;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
export declare const buildSuperwallState: <TProduct extends SuperwallProduct = SuperwallProduct>({ variables, products, paywall, lastTransaction, permissionStatuses, callbackStates, trial, }: BuildSuperwallStateOptions<TProduct>) => SuperwallSdkState;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2
|
+
const productStateFromVariables = (variables) => {
|
|
3
|
+
const products = {};
|
|
4
|
+
if (Array.isArray(variables.products)) {
|
|
5
|
+
for (const entry of variables.products) {
|
|
6
|
+
if (!isRecord(entry))
|
|
7
|
+
continue;
|
|
8
|
+
for (const [reference, productVariables] of Object.entries(entry)) {
|
|
9
|
+
products[reference] = productVariables;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
for (const reference of ["primary", "secondary", "tertiary"]) {
|
|
14
|
+
const productVariables = variables[reference];
|
|
15
|
+
if (isRecord(productVariables) && !products[reference]) {
|
|
16
|
+
products[reference] = productVariables;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return products;
|
|
20
|
+
};
|
|
21
|
+
export const buildSuperwallState = ({ variables, products, paywall, lastTransaction, permissionStatuses = {}, callbackStates = {}, trial, }) => {
|
|
22
|
+
const productState = productStateFromVariables(variables);
|
|
23
|
+
if (trial?.eligible !== undefined) {
|
|
24
|
+
productState.hasIntroductoryOffer = trial.eligible;
|
|
25
|
+
}
|
|
26
|
+
for (const product of products) {
|
|
27
|
+
const existingProductState = productState[product.reference];
|
|
28
|
+
productState[product.reference] = {
|
|
29
|
+
...(isRecord(existingProductState) ? existingProductState : {}),
|
|
30
|
+
...product.variables,
|
|
31
|
+
productId: product.productId,
|
|
32
|
+
productIdentifier: product.productIdentifier,
|
|
33
|
+
source: product.source,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
const transactionState = {
|
|
37
|
+
didAbandonTransaction: lastTransaction?.status === "abandoned",
|
|
38
|
+
didCompleteTransaction: lastTransaction?.status === "completed",
|
|
39
|
+
};
|
|
40
|
+
productState.abandonedProductId =
|
|
41
|
+
lastTransaction?.status === "abandoned" ? lastTransaction.product.reference : "";
|
|
42
|
+
productState.purchasedProductId =
|
|
43
|
+
lastTransaction?.status === "completed" ? lastTransaction.product.reference : "";
|
|
44
|
+
if (lastTransaction) {
|
|
45
|
+
const transactionProduct = lastTransaction.product;
|
|
46
|
+
productState[lastTransaction.status === "completed" ? "purchased" : "abandoned"] =
|
|
47
|
+
transactionProduct.variables;
|
|
48
|
+
}
|
|
49
|
+
const state = {
|
|
50
|
+
...variables,
|
|
51
|
+
state: transactionState,
|
|
52
|
+
products: productState,
|
|
53
|
+
permissions: permissionStatuses,
|
|
54
|
+
callbacks: callbackStates,
|
|
55
|
+
};
|
|
56
|
+
if (paywall) {
|
|
57
|
+
state.paywall = paywall;
|
|
58
|
+
}
|
|
59
|
+
return state;
|
|
60
|
+
};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { TIncomingMessage, TIncomingMessageCallbackResult, TIncomingMessageTransactionAbandon, TIncomingMessageTransactionComplete, TOutgoingMessage } from "@superwall/protocol";
|
|
2
|
+
import type { SuperwallBridge, SuperwallBridgeOptions, SuperwallMessageLogEntry } from "../bridge";
|
|
3
|
+
import type { SuperwallConfig } from "../config";
|
|
4
|
+
import type { SuperwallProduct } from "../products";
|
|
5
|
+
import type { SuperwallCallbackStates, SuperwallPermissionStatuses, SuperwallSdkState } from "./state";
|
|
6
|
+
import type { SuperwallDeviceVariables, SuperwallExperiment, SuperwallPlacementParams, SuperwallUserVariables } from "./variables";
|
|
7
|
+
export type SuperwallRuntimeStatus = "idle" | "listening";
|
|
8
|
+
export type SuperwallPaywallIdentity = {
|
|
9
|
+
id: string;
|
|
10
|
+
identifier: string;
|
|
11
|
+
};
|
|
12
|
+
export type SuperwallPendingPurchase<out TProduct extends SuperwallProduct = SuperwallProduct> = {
|
|
13
|
+
reference: string;
|
|
14
|
+
product: TProduct;
|
|
15
|
+
};
|
|
16
|
+
export type SuperwallTransaction<TProduct extends SuperwallProduct = SuperwallProduct> = {
|
|
17
|
+
status: "completed";
|
|
18
|
+
product: TProduct;
|
|
19
|
+
message: TIncomingMessageTransactionComplete;
|
|
20
|
+
} | {
|
|
21
|
+
status: "abandoned";
|
|
22
|
+
product: TProduct;
|
|
23
|
+
message: TIncomingMessageTransactionAbandon;
|
|
24
|
+
};
|
|
25
|
+
export type SuperwallPurchaseFailure<out TProduct extends SuperwallProduct = SuperwallProduct> = {
|
|
26
|
+
status: "failed";
|
|
27
|
+
product: TProduct;
|
|
28
|
+
/**
|
|
29
|
+
* "timeout": no transaction message arrived in time. "superseded": a newer
|
|
30
|
+
* purchase for the same product, or a paywall re-open, replaced this one.
|
|
31
|
+
*/
|
|
32
|
+
reason: "timeout" | "superseded";
|
|
33
|
+
};
|
|
34
|
+
export type SuperwallPurchaseResult<TProduct extends SuperwallProduct = SuperwallProduct> = SuperwallTransaction<TProduct> | SuperwallPurchaseFailure<TProduct>;
|
|
35
|
+
export type SuperwallCallbackResult<TData = Record<string, unknown>> = {
|
|
36
|
+
status: TIncomingMessageCallbackResult["status"];
|
|
37
|
+
data?: TData;
|
|
38
|
+
message: TIncomingMessageCallbackResult;
|
|
39
|
+
};
|
|
40
|
+
export type SuperwallTrialState = {
|
|
41
|
+
/**
|
|
42
|
+
* Whether the user is eligible for an introductory offer. `undefined` until
|
|
43
|
+
* the SDK signals eligibility (or the config forces it).
|
|
44
|
+
*/
|
|
45
|
+
eligible?: boolean;
|
|
46
|
+
};
|
|
47
|
+
export type SuperwallRuntimeSnapshot<out TProduct extends SuperwallProduct = SuperwallProduct> = {
|
|
48
|
+
status: SuperwallRuntimeStatus;
|
|
49
|
+
config?: SuperwallConfig;
|
|
50
|
+
paywall?: SuperwallPaywallIdentity;
|
|
51
|
+
products: readonly TProduct[];
|
|
52
|
+
productsByReference: Readonly<Record<string, TProduct>>;
|
|
53
|
+
variables: Readonly<Record<string, unknown>>;
|
|
54
|
+
device: SuperwallDeviceVariables;
|
|
55
|
+
user: SuperwallUserVariables;
|
|
56
|
+
params: SuperwallPlacementParams;
|
|
57
|
+
experiment?: SuperwallExperiment;
|
|
58
|
+
trial: SuperwallTrialState;
|
|
59
|
+
locale: string;
|
|
60
|
+
state: SuperwallSdkState;
|
|
61
|
+
permissionStatuses: SuperwallPermissionStatuses;
|
|
62
|
+
callbackStates: SuperwallCallbackStates;
|
|
63
|
+
messages: readonly SuperwallMessageLogEntry[];
|
|
64
|
+
pendingPurchase?: SuperwallPendingPurchase<TProduct>;
|
|
65
|
+
lastTransaction?: SuperwallTransaction<TProduct>;
|
|
66
|
+
lastPurchaseFailure?: SuperwallPurchaseFailure<TProduct>;
|
|
67
|
+
};
|
|
68
|
+
export type SuperwallPurchaseOptions = {
|
|
69
|
+
shouldDismiss?: boolean;
|
|
70
|
+
/** Resolve as `{ status: "failed", reason: "timeout" }` if no transaction message arrives in time. */
|
|
71
|
+
timeoutMs?: number;
|
|
72
|
+
};
|
|
73
|
+
export type SuperwallEventMap = {
|
|
74
|
+
[E in TIncomingMessage["event_name"]]: Extract<TIncomingMessage, {
|
|
75
|
+
event_name: E;
|
|
76
|
+
}>;
|
|
77
|
+
} & {
|
|
78
|
+
/** Fires for every incoming message. */
|
|
79
|
+
message: TIncomingMessage;
|
|
80
|
+
};
|
|
81
|
+
export type SuperwallEventName = keyof SuperwallEventMap & string;
|
|
82
|
+
export type SuperwallSessionStartOptions = {
|
|
83
|
+
sendPing?: boolean;
|
|
84
|
+
ping?: Partial<Extract<TOutgoingMessage, {
|
|
85
|
+
event_name: "ping";
|
|
86
|
+
}>>;
|
|
87
|
+
};
|
|
88
|
+
export type SuperwallSessionOptions<TProduct extends SuperwallProduct = SuperwallProduct> = SuperwallBridgeOptions & {
|
|
89
|
+
bridge?: SuperwallBridge;
|
|
90
|
+
config?: SuperwallConfig;
|
|
91
|
+
products?: readonly TProduct[];
|
|
92
|
+
variables?: Record<string, unknown>;
|
|
93
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import type { TIncomingMessageExperiment } from "@superwall/protocol";
|
|
2
|
+
export type SuperwallDeviceEntitlement = {
|
|
3
|
+
identifier: string;
|
|
4
|
+
type?: string;
|
|
5
|
+
store?: string;
|
|
6
|
+
state?: string;
|
|
7
|
+
isActive?: boolean;
|
|
8
|
+
isLifetime?: boolean;
|
|
9
|
+
willRenew?: boolean;
|
|
10
|
+
latestProductId?: string;
|
|
11
|
+
productIds?: readonly string[];
|
|
12
|
+
startsAt?: number;
|
|
13
|
+
expiresAt?: number;
|
|
14
|
+
renewedAt?: number;
|
|
15
|
+
};
|
|
16
|
+
export type SuperwallDeviceTransaction = {
|
|
17
|
+
productId: string;
|
|
18
|
+
transactionId?: string;
|
|
19
|
+
subscriptionGroupId?: string;
|
|
20
|
+
store?: string;
|
|
21
|
+
isActive?: boolean;
|
|
22
|
+
isRevoked?: boolean;
|
|
23
|
+
isInGracePeriod?: boolean;
|
|
24
|
+
isInBillingRetryPeriod?: boolean;
|
|
25
|
+
willRenew?: boolean;
|
|
26
|
+
purchaseDate?: number;
|
|
27
|
+
expirationDate?: number;
|
|
28
|
+
};
|
|
29
|
+
export type SuperwallCustomerInfo = {
|
|
30
|
+
entitlements?: readonly SuperwallDeviceEntitlement[];
|
|
31
|
+
subscriptions?: readonly SuperwallDeviceTransaction[];
|
|
32
|
+
nonSubscriptions?: readonly SuperwallDeviceTransaction[];
|
|
33
|
+
isPlaceholder?: boolean;
|
|
34
|
+
};
|
|
35
|
+
export type SuperwallCapability = {
|
|
36
|
+
name: string;
|
|
37
|
+
eventNames?: readonly string[];
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Device variables the native SDK delivers via `template_variables`. The names
|
|
41
|
+
* mirror the SDK's device template exactly — there is no index signature, so a
|
|
42
|
+
* key the SDK does not send is a compile error rather than `undefined` on a
|
|
43
|
+
* device. Every field is optional: availability varies by platform and SDK
|
|
44
|
+
* version, and a paywall must render without any of them.
|
|
45
|
+
*/
|
|
46
|
+
export type SuperwallDeviceVariables = {
|
|
47
|
+
publicApiKey?: string;
|
|
48
|
+
platform?: string;
|
|
49
|
+
appUserId?: string;
|
|
50
|
+
aliases?: readonly string[];
|
|
51
|
+
vendorId?: string;
|
|
52
|
+
deviceId?: string;
|
|
53
|
+
appVersion?: string;
|
|
54
|
+
appVersionPadded?: string;
|
|
55
|
+
appBuildString?: string;
|
|
56
|
+
appBuildStringNumber?: number;
|
|
57
|
+
bundleId?: string;
|
|
58
|
+
osVersion?: string;
|
|
59
|
+
deviceModel?: string;
|
|
60
|
+
deviceLocale?: string;
|
|
61
|
+
preferredLocale?: string;
|
|
62
|
+
deviceLanguageCode?: string;
|
|
63
|
+
preferredLanguageCode?: string;
|
|
64
|
+
regionCode?: string;
|
|
65
|
+
preferredRegionCode?: string;
|
|
66
|
+
deviceCurrencyCode?: string;
|
|
67
|
+
deviceCurrencySymbol?: string;
|
|
68
|
+
interfaceType?: string;
|
|
69
|
+
interfaceStyle?: string;
|
|
70
|
+
interfaceStyleMode?: string;
|
|
71
|
+
timezoneOffset?: number;
|
|
72
|
+
radioType?: string;
|
|
73
|
+
isLowPowerModeEnabled?: boolean;
|
|
74
|
+
isApplePayAvailable?: boolean;
|
|
75
|
+
isMac?: boolean;
|
|
76
|
+
isSandbox?: string;
|
|
77
|
+
isFirstAppOpen?: boolean;
|
|
78
|
+
appInstallDate?: string;
|
|
79
|
+
daysSinceInstall?: number;
|
|
80
|
+
minutesSinceInstall?: number;
|
|
81
|
+
daysSinceLastPaywallView?: number;
|
|
82
|
+
minutesSinceLastPaywallView?: number;
|
|
83
|
+
totalPaywallViews?: number;
|
|
84
|
+
totalReviewRequests?: number;
|
|
85
|
+
utcDate?: string;
|
|
86
|
+
localDate?: string;
|
|
87
|
+
utcTime?: string;
|
|
88
|
+
localTime?: string;
|
|
89
|
+
utcDateTime?: string;
|
|
90
|
+
localDateTime?: string;
|
|
91
|
+
subscriptionStatus?: string;
|
|
92
|
+
activeEntitlements?: readonly string[];
|
|
93
|
+
activeEntitlementObjects?: readonly SuperwallDeviceEntitlement[];
|
|
94
|
+
activeProducts?: readonly string[];
|
|
95
|
+
customerInfo?: SuperwallCustomerInfo;
|
|
96
|
+
capabilitiesConfig?: readonly SuperwallCapability[];
|
|
97
|
+
sdkVersion?: string;
|
|
98
|
+
sdkVersionPadded?: string;
|
|
99
|
+
capabilities?: string;
|
|
100
|
+
platformWrapper?: string;
|
|
101
|
+
platformWrapperVersion?: string;
|
|
102
|
+
swiftVersion?: string;
|
|
103
|
+
compilerVersion?: string;
|
|
104
|
+
localResourceIds?: string;
|
|
105
|
+
};
|
|
106
|
+
export type SuperwallUserVariables = {
|
|
107
|
+
seed?: number;
|
|
108
|
+
appUserId?: string;
|
|
109
|
+
aliasId?: string;
|
|
110
|
+
applicationInstalledAt?: string;
|
|
111
|
+
} & Record<string, unknown>;
|
|
112
|
+
export type SuperwallPlacementParams = {
|
|
113
|
+
$event_name?: string;
|
|
114
|
+
event_name?: string;
|
|
115
|
+
$is_standard_event?: boolean;
|
|
116
|
+
$is_feature_gatable?: boolean;
|
|
117
|
+
$app_session_id?: string;
|
|
118
|
+
} & Record<string, unknown>;
|
|
119
|
+
/** Experiment assignment, mirroring the SDK's `experiment` message payload. */
|
|
120
|
+
export type SuperwallExperiment = Omit<TIncomingMessageExperiment, "event_name">;
|
|
121
|
+
export declare const recordFromVariables: (variables: Readonly<Record<string, unknown>>, key: string) => Record<string, unknown>;
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@superwall/runtime",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Framework-agnostic runtime for headless Superwall paywalls: bridge, session, products, localization, mock host.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"paywall",
|
|
7
|
+
"runtime",
|
|
8
|
+
"subscriptions",
|
|
9
|
+
"superwall"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/superwall/superwall/tree/main/packages/runtime#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/superwall/superwall/issues"
|
|
14
|
+
},
|
|
15
|
+
"license": "FSL-1.1-ALv2",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/superwall/superwall.git",
|
|
19
|
+
"directory": "packages/runtime"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"src"
|
|
24
|
+
],
|
|
25
|
+
"type": "module",
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"exports": {
|
|
28
|
+
".": "./src/index.ts"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsc -p tsconfig.build.json",
|
|
35
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
36
|
+
"test": "vitest",
|
|
37
|
+
"test:run": "vitest --run",
|
|
38
|
+
"test:coverage": "vitest --run --coverage",
|
|
39
|
+
"clean": "rm -rf dist coverage"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@superwall/protocol": "1.1.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/node": "^22.0.0",
|
|
46
|
+
"@vitest/coverage-v8": "^3.2.0",
|
|
47
|
+
"typescript": "5.8.3",
|
|
48
|
+
"vite": "^7.0.0",
|
|
49
|
+
"vitest": "^3.2.0"
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { TIncomingMessage } from "@superwall/protocol";
|
|
2
|
+
|
|
3
|
+
type BufferLike = {
|
|
4
|
+
from(
|
|
5
|
+
input: string,
|
|
6
|
+
encoding: "base64",
|
|
7
|
+
): {
|
|
8
|
+
toString(encoding: "utf8"): string;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type GlobalWithBuffer = typeof globalThis & {
|
|
13
|
+
Buffer?: BufferLike;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Host SDKs may encode with URL-safe alphabets and without padding (e.g.
|
|
18
|
+
* Android's Base64.URL_SAFE). atob is strict, so normalize first.
|
|
19
|
+
*/
|
|
20
|
+
const normalizeBase64 = (input: string) => {
|
|
21
|
+
const standard = input.replace(/-/g, "+").replace(/_/g, "/").replace(/\s/g, "");
|
|
22
|
+
const remainder = standard.length % 4;
|
|
23
|
+
return remainder === 0 ? standard : standard + "=".repeat((4 - remainder) % 4);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const decodeBase64Utf8 = (message: string) => {
|
|
27
|
+
if (typeof globalThis.atob === "function") {
|
|
28
|
+
const binary = globalThis.atob(normalizeBase64(message));
|
|
29
|
+
const bytes = Uint8Array.from(binary, (char) => char.charCodeAt(0));
|
|
30
|
+
return new TextDecoder().decode(bytes);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const buffer = (globalThis as GlobalWithBuffer).Buffer;
|
|
34
|
+
if (buffer) {
|
|
35
|
+
return buffer.from(normalizeBase64(message), "base64").toString("utf8");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
throw new Error("No base64 decoder is available in this runtime");
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Deliberately lenient, matching the original runtime: every object in the
|
|
43
|
+
* batch is delivered, including messages this build doesn't recognize (newer
|
|
44
|
+
* event types, or legacy quirks like the miscased `eventName: "experiment"`).
|
|
45
|
+
* Strictly validating here would let one unknown message drop the whole batch.
|
|
46
|
+
*/
|
|
47
|
+
export const decodeIncomingMessages = (message: string): TIncomingMessage[] => {
|
|
48
|
+
const parsed = JSON.parse(decodeBase64Utf8(message));
|
|
49
|
+
if (!Array.isArray(parsed)) {
|
|
50
|
+
return [];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return parsed.filter(
|
|
54
|
+
(entry): entry is TIncomingMessage => typeof entry === "object" && entry !== null,
|
|
55
|
+
);
|
|
56
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ITransport,
|
|
3
|
+
TIncomingHandler,
|
|
4
|
+
TIncomingMessage,
|
|
5
|
+
TOutgoingMessage,
|
|
6
|
+
TransportKind,
|
|
7
|
+
} from "@superwall/protocol";
|
|
8
|
+
import { decodeIncomingMessages } from "./base64";
|
|
9
|
+
import { createRuntimeTransport, detectRuntimeTransportKind } from "./transport";
|
|
10
|
+
|
|
11
|
+
export type SuperwallMessageLogEntry =
|
|
12
|
+
| { ts: Date; msg: TIncomingMessage; type: "incoming" }
|
|
13
|
+
| { ts: Date; msg: TOutgoingMessage; type: "outgoing" };
|
|
14
|
+
|
|
15
|
+
export type SuperwallBridgeOptions = {
|
|
16
|
+
transport?: ITransport;
|
|
17
|
+
transportKind?: TransportKind;
|
|
18
|
+
now?: () => Date;
|
|
19
|
+
onDecodeError?: (error: unknown) => void;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export class SuperwallBridge {
|
|
23
|
+
private transport: ITransport;
|
|
24
|
+
private incomingHandlers: TIncomingHandler[] = [];
|
|
25
|
+
private now: () => Date;
|
|
26
|
+
private onDecodeError?: (error: unknown) => void;
|
|
27
|
+
|
|
28
|
+
public messages: SuperwallMessageLogEntry[] = [];
|
|
29
|
+
|
|
30
|
+
constructor(options: SuperwallBridgeOptions = {}) {
|
|
31
|
+
this.transport =
|
|
32
|
+
options.transport ??
|
|
33
|
+
createRuntimeTransport(options.transportKind ?? detectRuntimeTransportKind());
|
|
34
|
+
this.now = options.now ?? (() => new Date());
|
|
35
|
+
this.onDecodeError = options.onDecodeError;
|
|
36
|
+
|
|
37
|
+
this.transport.addListener((message) => {
|
|
38
|
+
this.messages.push({ ts: this.now(), msg: message, type: "incoming" });
|
|
39
|
+
this.incomingHandlers.forEach((handler) => {
|
|
40
|
+
handler(message);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public start() {
|
|
46
|
+
this.transport.startListening();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public addListener(handler: TIncomingHandler) {
|
|
50
|
+
this.incomingHandlers.push(handler);
|
|
51
|
+
return () => {
|
|
52
|
+
this.incomingHandlers = this.incomingHandlers.filter((candidate) => candidate !== handler);
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public postMessage(message: TOutgoingMessage) {
|
|
57
|
+
this.messages.push({ ts: this.now(), msg: message, type: "outgoing" });
|
|
58
|
+
this.transport.postMessage(message);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public accept(messages: TIncomingMessage[]) {
|
|
62
|
+
messages.forEach((message) => {
|
|
63
|
+
this.transport.accept(message);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public accept64(message: string) {
|
|
68
|
+
try {
|
|
69
|
+
this.accept(decodeIncomingMessages(message));
|
|
70
|
+
} catch (error) {
|
|
71
|
+
this.onDecodeError?.(error);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export const createSuperwallBridge = (options: SuperwallBridgeOptions = {}) =>
|
|
77
|
+
new SuperwallBridge(options);
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { buildTransport, TransportKind, type ITransport } from "@superwall/protocol";
|
|
2
|
+
|
|
3
|
+
export { pendingHostMessages } from "@superwall/protocol";
|
|
4
|
+
|
|
5
|
+
export type RuntimeTransportDetectionOptions = {
|
|
6
|
+
search?: string;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const detectRuntimeTransportKind = (
|
|
10
|
+
options: RuntimeTransportDetectionOptions = {},
|
|
11
|
+
): TransportKind => {
|
|
12
|
+
if (typeof window === "undefined" && options.search === undefined) {
|
|
13
|
+
return TransportKind.WebKitPostMessage;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const search = options.search ?? window.location.search;
|
|
17
|
+
const urlParams = new URLSearchParams(search);
|
|
18
|
+
|
|
19
|
+
if (urlParams.get("platform") === "android") {
|
|
20
|
+
return TransportKind.Android;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (urlParams.get("transport") === "editor") {
|
|
24
|
+
return TransportKind.EditorIframe;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return TransportKind.WebKitPostMessage;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const createRuntimeTransport = (
|
|
31
|
+
transportKind: TransportKind = detectRuntimeTransportKind(),
|
|
32
|
+
): ITransport => buildTransport(transportKind);
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Detects the explicit `?sw_preview` opt-in that lets a page simulate the SDK
|
|
36
|
+
* without a native host present. "auto" mock mode requires it, so a built
|
|
37
|
+
* snapshot can never downgrade itself to placeholder products by accident.
|
|
38
|
+
*/
|
|
39
|
+
export const detectPreviewOptIn = (): boolean => {
|
|
40
|
+
if (typeof window === "undefined") {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
return new URLSearchParams(window.location.search).has("sw_preview");
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export const detectNativeSuperwallHost = (): boolean => {
|
|
47
|
+
if (typeof window === "undefined") {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// window.webkit and SWAndroid are declared globally by @superwall/protocol's
|
|
52
|
+
// transports — the SDK-injected host contract.
|
|
53
|
+
if (window.webkit?.messageHandlers?.paywallMessageHandler) {
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (typeof SWAndroid !== "undefined" && SWAndroid) {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const urlParams = new URLSearchParams(window.location.search);
|
|
62
|
+
return urlParams.get("platform") === "android" || urlParams.get("transport") === "editor";
|
|
63
|
+
};
|