@ait-co/polyfill 0.1.5 → 0.1.6
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 +53 -3
- package/dist/auto.cjs +919 -0
- package/dist/auto.cjs.map +1 -0
- package/dist/auto.d.cts +1 -0
- package/dist/auto.js +18 -6
- package/dist/auto.js.map +1 -1
- package/dist/detect.cjs +84 -0
- package/dist/detect.cjs.map +1 -0
- package/dist/detect.d.cts +50 -0
- package/dist/detect.d.cts.map +1 -0
- package/dist/index.cjs +982 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +222 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +26 -27
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +68 -8
- package/dist/index.js.map +1 -1
- package/dist/shims/clipboard.cjs +192 -0
- package/dist/shims/clipboard.cjs.map +1 -0
- package/dist/shims/clipboard.d.cts +26 -0
- package/dist/shims/clipboard.d.cts.map +1 -0
- package/dist/shims/geolocation.cjs +356 -0
- package/dist/shims/geolocation.cjs.map +1 -0
- package/dist/shims/geolocation.d.cts +41 -0
- package/dist/shims/geolocation.d.cts.map +1 -0
- package/dist/shims/network.cjs +290 -0
- package/dist/shims/network.cjs.map +1 -0
- package/dist/shims/network.d.cts +56 -0
- package/dist/shims/network.d.cts.map +1 -0
- package/dist/shims/share.cjs +239 -0
- package/dist/shims/share.cjs.map +1 -0
- package/dist/shims/share.d.cts +26 -0
- package/dist/shims/share.d.cts.map +1 -0
- package/dist/shims/vibrate-semantic.d.ts +27 -0
- package/dist/shims/vibrate-semantic.d.ts.map +1 -0
- package/dist/shims/vibrate-semantic.js +150 -0
- package/dist/shims/vibrate-semantic.js.map +1 -0
- package/dist/shims/vibrate.cjs +235 -0
- package/dist/shims/vibrate.cjs.map +1 -0
- package/dist/shims/vibrate.d.cts +43 -0
- package/dist/shims/vibrate.d.cts.map +1 -0
- package/dist/shims/vibrate.d.ts +16 -5
- package/dist/shims/vibrate.d.ts.map +1 -1
- package/dist/shims/vibrate.js +19 -7
- package/dist/shims/vibrate.js.map +1 -1
- package/package.json +73 -19
package/dist/shims/vibrate.d.ts
CHANGED
|
@@ -2,10 +2,18 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* `navigator.vibrate` shim.
|
|
4
4
|
*
|
|
5
|
-
* Inside Apps in Toss → best-effort mapping to SDK `generateHapticFeedback
|
|
5
|
+
* Inside Apps in Toss → best-effort mapping to SDK `generateHapticFeedback`.
|
|
6
|
+
* Single-duration calls land in three buckets so the qualitative SDK haptic
|
|
7
|
+
* tracks intensity a little more closely than the original two-bucket split:
|
|
6
8
|
* - `vibrate(0)` → no-op (web standard: cancels pending vibration)
|
|
7
|
-
* - `vibrate(
|
|
8
|
-
* - `vibrate(
|
|
9
|
+
* - `vibrate(1..20ms)` → `tickWeak`
|
|
10
|
+
* - `vibrate(21..45ms)` → `tickMedium`
|
|
11
|
+
* - `vibrate(>=46ms)` → `basicMedium`
|
|
12
|
+
* - `vibrate(number[])` → iterates "on" segments (even indices) as `tap`
|
|
13
|
+
* pulses with `setTimeout` gaps. Per-element ms mapping is intentionally
|
|
14
|
+
* skipped: arrays in the wild are mostly rhythmic patterns, and the SDK
|
|
15
|
+
* has no "stronger heavy" variant to reach for, so per-pulse precision
|
|
16
|
+
* buys little. Callers needing intent should use `vibrateSemantic`.
|
|
9
17
|
*
|
|
10
18
|
* Outside Apps in Toss → defers to the browser's native `navigator.vibrate`,
|
|
11
19
|
* or returns `false` when unavailable (matches the spec — browsers that don't
|
|
@@ -19,14 +27,17 @@
|
|
|
19
27
|
* Caveats (documented in CLAUDE.md as the known lossy trade-off):
|
|
20
28
|
* - SDK haptics are qualitative ("tickWeak", "basicMedium"), not millisecond
|
|
21
29
|
* durations. The shim approximates intensity from duration but cannot
|
|
22
|
-
* reproduce exact patterns.
|
|
30
|
+
* reproduce exact patterns. Length-only mapping cannot recover semantic
|
|
31
|
+
* intent (success vs. error vs. warning); use `vibrateSemantic` for that.
|
|
23
32
|
* - Arrays are fired sequentially via `setTimeout`; gaps between pulses are
|
|
24
33
|
* honoured only as "time until the next tap", not as silent-vs-vibrating.
|
|
25
34
|
* - `vibrate` is spec'd as **synchronous**; the SDK call is async. We return
|
|
26
35
|
* `true` immediately (fire-and-forget). Errors from the SDK are swallowed.
|
|
27
36
|
*/
|
|
37
|
+
type HapticType = 'tickWeak' | 'tap' | 'tickMedium' | 'softMedium' | 'basicWeak' | 'basicMedium' | 'success' | 'error' | 'wiggle' | 'confetti';
|
|
38
|
+
declare function haptic(type: HapticType): Promise<void>;
|
|
28
39
|
declare function installVibrateShim(): () => void;
|
|
29
40
|
declare function uninstallVibrateShim(): void;
|
|
30
41
|
//#endregion
|
|
31
|
-
export { installVibrateShim, uninstallVibrateShim };
|
|
42
|
+
export { haptic, installVibrateShim, uninstallVibrateShim };
|
|
32
43
|
//# sourceMappingURL=vibrate.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vibrate.d.ts","names":[],"sources":["../../src/shims/vibrate.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"vibrate.d.ts","names":[],"sources":["../../src/shims/vibrate.ts"],"mappings":";;;;;;;AAuEA;;;;;;;;;AA+DA;;;;;AA4BA;;;;;;;;;;;;;;;KAvGK,UAAA;AAAA,iBAYiB,MAAA,CAAO,IAAA,EAAM,UAAA,GAAa,OAAA;AAAA,iBA+DhC,kBAAA,CAAA;AAAA,iBA4BA,oBAAA,CAAA"}
|
package/dist/shims/vibrate.js
CHANGED
|
@@ -128,10 +128,18 @@ function restoreObjectMethods(snapshot) {
|
|
|
128
128
|
/**
|
|
129
129
|
* `navigator.vibrate` shim.
|
|
130
130
|
*
|
|
131
|
-
* Inside Apps in Toss → best-effort mapping to SDK `generateHapticFeedback
|
|
131
|
+
* Inside Apps in Toss → best-effort mapping to SDK `generateHapticFeedback`.
|
|
132
|
+
* Single-duration calls land in three buckets so the qualitative SDK haptic
|
|
133
|
+
* tracks intensity a little more closely than the original two-bucket split:
|
|
132
134
|
* - `vibrate(0)` → no-op (web standard: cancels pending vibration)
|
|
133
|
-
* - `vibrate(
|
|
134
|
-
* - `vibrate(
|
|
135
|
+
* - `vibrate(1..20ms)` → `tickWeak`
|
|
136
|
+
* - `vibrate(21..45ms)` → `tickMedium`
|
|
137
|
+
* - `vibrate(>=46ms)` → `basicMedium`
|
|
138
|
+
* - `vibrate(number[])` → iterates "on" segments (even indices) as `tap`
|
|
139
|
+
* pulses with `setTimeout` gaps. Per-element ms mapping is intentionally
|
|
140
|
+
* skipped: arrays in the wild are mostly rhythmic patterns, and the SDK
|
|
141
|
+
* has no "stronger heavy" variant to reach for, so per-pulse precision
|
|
142
|
+
* buys little. Callers needing intent should use `vibrateSemantic`.
|
|
135
143
|
*
|
|
136
144
|
* Outside Apps in Toss → defers to the browser's native `navigator.vibrate`,
|
|
137
145
|
* or returns `false` when unavailable (matches the spec — browsers that don't
|
|
@@ -145,7 +153,8 @@ function restoreObjectMethods(snapshot) {
|
|
|
145
153
|
* Caveats (documented in CLAUDE.md as the known lossy trade-off):
|
|
146
154
|
* - SDK haptics are qualitative ("tickWeak", "basicMedium"), not millisecond
|
|
147
155
|
* durations. The shim approximates intensity from duration but cannot
|
|
148
|
-
* reproduce exact patterns.
|
|
156
|
+
* reproduce exact patterns. Length-only mapping cannot recover semantic
|
|
157
|
+
* intent (success vs. error vs. warning); use `vibrateSemantic` for that.
|
|
149
158
|
* - Arrays are fired sequentially via `setTimeout`; gaps between pulses are
|
|
150
159
|
* honoured only as "time until the next tap", not as silent-vs-vibrating.
|
|
151
160
|
* - `vibrate` is spec'd as **synchronous**; the SDK call is async. We return
|
|
@@ -153,7 +162,8 @@ function restoreObjectMethods(snapshot) {
|
|
|
153
162
|
*/
|
|
154
163
|
const BACKUP_KEY = Symbol.for("@ait-co/polyfill/vibrate.original");
|
|
155
164
|
const SNAPSHOT_KEY = Symbol.for("@ait-co/polyfill/vibrate.snapshot");
|
|
156
|
-
const
|
|
165
|
+
const TICK_WEAK_MAX_MS = 20;
|
|
166
|
+
const TICK_MEDIUM_MAX_MS = 45;
|
|
157
167
|
async function haptic(type) {
|
|
158
168
|
const fn = (await loadTossSdk())?.generateHapticFeedback;
|
|
159
169
|
if (typeof fn === "function") try {
|
|
@@ -161,7 +171,9 @@ async function haptic(type) {
|
|
|
161
171
|
} catch {}
|
|
162
172
|
}
|
|
163
173
|
function durationToHaptic(duration) {
|
|
164
|
-
|
|
174
|
+
if (duration <= TICK_WEAK_MAX_MS) return "tickWeak";
|
|
175
|
+
if (duration <= TICK_MEDIUM_MAX_MS) return "tickMedium";
|
|
176
|
+
return "basicMedium";
|
|
165
177
|
}
|
|
166
178
|
function vibrateShim(pattern) {
|
|
167
179
|
const arr = Array.isArray(pattern) ? pattern : [pattern];
|
|
@@ -215,6 +227,6 @@ function uninstallVibrateShim() {
|
|
|
215
227
|
delete host[SNAPSHOT_KEY];
|
|
216
228
|
}
|
|
217
229
|
//#endregion
|
|
218
|
-
export { installVibrateShim, uninstallVibrateShim };
|
|
230
|
+
export { haptic, installVibrateShim, uninstallVibrateShim };
|
|
219
231
|
|
|
220
232
|
//# sourceMappingURL=vibrate.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vibrate.js","names":[],"sources":["../../src/detect.ts","../../src/shims/_install-helpers.ts","../../src/shims/vibrate.ts"],"sourcesContent":["/**\n * Environment detection: are we running inside Apps in Toss, or a plain browser?\n *\n * Strategy: call the SDK's `getAppsInTossGlobals()` — a synchronous export\n * that returns the runtime's Toss globals (deploymentId, brand name, …)\n * inside the Apps in Toss runtime and throws (RN bridge unavailable)\n * anywhere else. The SDK itself is an **optional** peer dependency; if its\n * module can't be imported we are definitely not inside Toss.\n *\n * Just having the SDK module resolvable is not enough — apps can bundle it\n * and still run in a plain browser. We need the bridge probe to confirm.\n *\n * UA sniffing (spoofable) is avoided. We do call `getAppsInTossGlobals`, but\n * that's a constant read from the bridge — no permission dialogs, no\n * analytics fire. In a plain browser the bridge lookup fails fast (sync\n * throw, microsecond-scale), so the startup cost is negligible.\n */\n\nlet cached: boolean | undefined;\n\n/**\n * Reset the cached detection result. Primarily for tests.\n */\nexport function resetDetection(): void {\n cached = undefined;\n}\n\n/**\n * Synchronous read of the cached detection result. Returns:\n * - `true` / `false` if an override is active or the async detection has\n * already resolved\n * - `undefined` if detection hasn't run yet\n *\n * Used by spec-sync APIs (e.g. `navigator.canShare`) that can't `await`\n * detection.\n */\nexport function isTossEnvironmentCached(): boolean | undefined {\n const force = globalThis.__AIT_POLYFILL_FORCE__;\n if (force === 'toss') return true;\n if (force === 'browser') return false;\n return cached;\n}\n\n/**\n * Returns `true` iff we detect we are running in an environment where the\n * Apps in Toss SDK (`@apps-in-toss/web-framework`) is present and usable.\n *\n * Async because we use dynamic `import()` to probe the optional peer dep\n * without forcing it into the consumer's bundle.\n */\nexport async function isTossEnvironment(): Promise<boolean> {\n // Override check precedes cache so `devtools` / tests can flip the result\n // mid-session without a `resetDetection()` call.\n const force = globalThis.__AIT_POLYFILL_FORCE__;\n if (force === 'toss') return true;\n if (force === 'browser') return false;\n\n if (cached !== undefined) return cached;\n\n const mod = await loadTossSdk();\n if (typeof mod?.getAppsInTossGlobals !== 'function') {\n cached = false;\n return cached;\n }\n // Inside Toss the bridge returns a populated globals object. In a plain\n // browser the RN bridge isn't attached and the call throws — that's our\n // signal. Any non-throwing call with an object return is treated as Toss.\n try {\n const globals = mod.getAppsInTossGlobals();\n cached = Boolean(globals) && typeof globals === 'object';\n } catch {\n cached = false;\n }\n return cached;\n}\n\n/**\n * Lazy SDK accessor — returns the module if available, else `null`. Callers\n * are expected to `await` and null-check. Never throws.\n */\nexport async function loadTossSdk(): Promise<typeof import('@apps-in-toss/web-framework') | null> {\n try {\n return await import('@apps-in-toss/web-framework');\n } catch {\n return null;\n }\n}\n","/**\n * Shared helpers for installing shims on `navigator`.\n *\n * Chromium now marks a handful of `navigator` properties (e.g. `geolocation`,\n * `clipboard`) as non-configurable **own** properties on the instance. That\n * means a plain `Object.defineProperty(navigator, 'x', …)` throws\n * `TypeError: Cannot redefine property`.\n *\n * The workaround is to shim at the prototype level — `Navigator.prototype`\n * keeps these as configurable accessors, so we can swap them there and every\n * instance that falls through to the prototype (including `window.navigator`)\n * sees the shim. We only reach for the prototype when the instance-level\n * assignment refuses.\n *\n * For restoration we remember the descriptor chain (instance + prototype) so\n * `uninstall()` puts the browser back in its original state.\n */\n\ntype PropertyLocation = 'instance' | 'prototype';\n\nexport interface InstallSnapshot {\n /** Where we ended up writing the shim. */\n location: PropertyLocation;\n /** Original descriptor at that location (may be undefined if nothing was there). */\n originalDescriptor: PropertyDescriptor | undefined;\n /** `true` iff the original property lived on the instance before we touched it. */\n instanceHadOwn: boolean;\n}\n\n/**\n * Install `descriptor` at `navigator[prop]`. Prefer instance-level; if the\n * browser refuses (property is non-configurable on the instance), install on\n * `Navigator.prototype` instead.\n *\n * Returns a snapshot describing where the original value was, which\n * `restoreNavigatorProperty` uses to undo the install.\n */\nexport function installNavigatorProperty(\n prop: string,\n descriptor: PropertyDescriptor,\n): InstallSnapshot {\n const nav = navigator as unknown as Record<PropertyKey, unknown>;\n const instanceDesc = Object.getOwnPropertyDescriptor(nav, prop);\n const instanceHadOwn = instanceDesc !== undefined;\n\n // Fast path: instance-level property is missing or configurable.\n if (!instanceDesc || instanceDesc.configurable) {\n try {\n Object.defineProperty(nav, prop, descriptor);\n return { location: 'instance', originalDescriptor: instanceDesc, instanceHadOwn };\n } catch {\n // Fall through to prototype-level install.\n }\n }\n\n // Prototype-level install. Drop the instance-level shadow so the prototype\n // accessor is visible to readers on `navigator`.\n const proto = Object.getPrototypeOf(nav) as object;\n const protoDesc = Object.getOwnPropertyDescriptor(proto, prop);\n\n if (instanceHadOwn) {\n // Try to remove the instance-level shadow. On non-configurable it throws —\n // we deliberately ignore that; prototype-level install still wins because\n // the prototype accessor shows through when we read via `navigator[prop]`.\n try {\n delete nav[prop];\n } catch {\n /* non-configurable own — leave it; prototype install still useful */\n }\n }\n\n Object.defineProperty(proto, prop, descriptor);\n return { location: 'prototype', originalDescriptor: protoDesc, instanceHadOwn };\n}\n\n/**\n * Reverse the install recorded in `snapshot`. If the original descriptor was\n * `undefined` (property didn't exist before), delete the property instead of\n * re-defining it.\n */\nexport function restoreNavigatorProperty(prop: string, snapshot: InstallSnapshot): void {\n const target =\n snapshot.location === 'instance'\n ? (navigator as unknown as Record<PropertyKey, unknown>)\n : (Object.getPrototypeOf(navigator) as object);\n\n if (snapshot.originalDescriptor) {\n try {\n Object.defineProperty(target, prop, snapshot.originalDescriptor);\n } catch {\n /* descriptor was non-configurable upstream; we can't undo — rare. */\n }\n } else {\n try {\n delete (target as Record<PropertyKey, unknown>)[prop];\n } catch {\n /* non-configurable — rare. */\n }\n }\n\n // If our install pushed past an instance shadow, we leave the instance alone\n // — the descriptor we captured for `instanceHadOwn: true` lives on the\n // instance and was not modified at install time.\n}\n\n/**\n * Method-level install snapshot. Captured per-key so `restoreObjectMethods`\n * can distinguish \"was an own property, reassign it\" from \"was inherited,\n * delete the override so the prototype method surfaces again\".\n */\nexport interface MethodInstallSnapshot {\n target: object;\n methods: Record<string, { hadOwn: boolean; original: unknown }>;\n}\n\n/**\n * Mutate methods on an existing object rather than replacing the object\n * itself. This is the path we take for `navigator.geolocation`, `navigator.share`,\n * and `navigator.vibrate` in Chromium, where the slot on `navigator` is a\n * non-configurable own property that we cannot replace — but the methods\n * themselves (or the methods on the referenced object) are still\n * `configurable: true, writable: true`.\n *\n * Each replacement is installed via plain assignment. If any slot is not\n * writable (e.g. frozen object), install is aborted and previously-applied\n * replacements are rolled back, so the caller observes an atomic \"all or\n * nothing\" failure as `null`. The caller is expected to degrade gracefully\n * (e.g. log a one-time `console.warn`) when that happens.\n */\nexport function installObjectMethods(\n target: object,\n replacements: Record<string, (...args: never[]) => unknown>,\n): MethodInstallSnapshot | null {\n const methods: Record<string, { hadOwn: boolean; original: unknown }> = {};\n const applied: string[] = [];\n const bag = target as Record<string, unknown>;\n\n for (const key of Object.keys(replacements)) {\n const hadOwn = Object.hasOwn(target, key);\n const original = bag[key];\n try {\n bag[key] = replacements[key] as unknown;\n } catch {\n // Non-writable / frozen. Roll back and return null.\n for (const applieKey of applied) {\n const prev = methods[applieKey];\n if (!prev) continue;\n if (prev.hadOwn) {\n bag[applieKey] = prev.original;\n } else {\n delete bag[applieKey];\n }\n }\n return null;\n }\n // Verify the assignment actually stuck — silent-failure descriptors (e.g.\n // `writable: false` without strict mode) can skip the throw and leave the\n // original value in place. Treat that the same as a throw.\n if (bag[key] !== (replacements[key] as unknown)) {\n for (const applieKey of applied) {\n const prev = methods[applieKey];\n if (!prev) continue;\n if (prev.hadOwn) {\n bag[applieKey] = prev.original;\n } else {\n delete bag[applieKey];\n }\n }\n return null;\n }\n methods[key] = { hadOwn, original };\n applied.push(key);\n }\n\n return { target, methods };\n}\n\n/**\n * Reverse an `installObjectMethods` snapshot. Reassigns originals for slots\n * that were own properties before install; deletes the override for slots\n * that were inherited (so the prototype method surfaces again).\n */\nexport function restoreObjectMethods(snapshot: MethodInstallSnapshot): void {\n const bag = snapshot.target as Record<string, unknown>;\n for (const key of Object.keys(snapshot.methods)) {\n const entry = snapshot.methods[key];\n if (!entry) continue;\n try {\n if (entry.hadOwn) {\n bag[key] = entry.original;\n } else {\n delete bag[key];\n }\n } catch {\n /* frozen between install and restore — rare. */\n }\n }\n}\n","/**\n * `navigator.vibrate` shim.\n *\n * Inside Apps in Toss → best-effort mapping to SDK `generateHapticFeedback`:\n * - `vibrate(0)` → no-op (web standard: cancels pending vibration)\n * - `vibrate(number)`: short (< 40ms) → `tickWeak`, long (≥ 40ms) → `basicMedium`\n * - `vibrate(number[])`: iterate \"on\" segments (even indices) as `tap` pulses\n *\n * Outside Apps in Toss → defers to the browser's native `navigator.vibrate`,\n * or returns `false` when unavailable (matches the spec — browsers that don't\n * support vibration simply return `false`).\n *\n * Install strategy: **method-level** on `navigator`. We assign our wrapper to\n * `navigator.vibrate` (creating an own shadow over the prototype method) and\n * delete it on uninstall so the prototype re-surfaces. We do not mutate\n * `Navigator.prototype` itself — browsers may mark it non-configurable.\n *\n * Caveats (documented in CLAUDE.md as the known lossy trade-off):\n * - SDK haptics are qualitative (\"tickWeak\", \"basicMedium\"), not millisecond\n * durations. The shim approximates intensity from duration but cannot\n * reproduce exact patterns.\n * - Arrays are fired sequentially via `setTimeout`; gaps between pulses are\n * honoured only as \"time until the next tap\", not as silent-vs-vibrating.\n * - `vibrate` is spec'd as **synchronous**; the SDK call is async. We return\n * `true` immediately (fire-and-forget). Errors from the SDK are swallowed.\n */\n\nimport { isTossEnvironment, loadTossSdk } from '../detect.js';\nimport {\n installObjectMethods,\n type MethodInstallSnapshot,\n restoreObjectMethods,\n} from './_install-helpers.js';\n\nconst BACKUP_KEY = Symbol.for('@ait-co/polyfill/vibrate.original');\nconst SNAPSHOT_KEY = Symbol.for('@ait-co/polyfill/vibrate.snapshot');\n\ntype VibrateFn = (pattern: VibratePattern) => boolean;\n\ninterface BackupHost {\n [BACKUP_KEY]?: VibrateFn | undefined;\n [SNAPSHOT_KEY]?: MethodInstallSnapshot | undefined;\n}\n\nconst SHORT_VIBRATION_MS = 40;\n\ntype HapticType =\n | 'tickWeak'\n | 'tap'\n | 'tickMedium'\n | 'softMedium'\n | 'basicWeak'\n | 'basicMedium'\n | 'success'\n | 'error'\n | 'wiggle'\n | 'confetti';\n\nasync function haptic(type: HapticType): Promise<void> {\n const sdk = await loadTossSdk();\n const fn = (sdk as { generateHapticFeedback?: unknown } | null)?.generateHapticFeedback;\n if (typeof fn === 'function') {\n try {\n await (fn as (o: { type: HapticType }) => Promise<void>)({ type });\n } catch {\n // Best-effort; spec-level `vibrate` cannot surface errors.\n }\n }\n}\n\nfunction durationToHaptic(duration: number): HapticType {\n return duration < SHORT_VIBRATION_MS ? 'tickWeak' : 'basicMedium';\n}\n\nfunction vibrateShim(pattern: VibratePattern): boolean {\n // Matches the spec: `vibrate(0)` or `vibrate([])` cancels pending vibration.\n // We can't cancel an in-flight SDK haptic (no cancel API), but we still\n // forward the cancel to the browser fallback so native vibration stops.\n const arr = Array.isArray(pattern) ? pattern : [pattern];\n if (arr.length === 0 || arr.every((n) => n === 0)) {\n void (async () => {\n if (!(await isTossEnvironment())) {\n const host = navigator as unknown as BackupHost;\n host[BACKUP_KEY]?.(pattern);\n }\n })();\n return true;\n }\n\n void (async () => {\n if (await isTossEnvironment()) {\n if (!Array.isArray(pattern)) {\n await haptic(durationToHaptic(pattern));\n return;\n }\n // Even indices = \"on\" durations, odd indices = pauses. `pattern[i]` is\n // `number | undefined` under `noUncheckedIndexedAccess`; the `undefined`\n // case only arises on out-of-bounds, which our length bound prevents.\n for (let i = 0; i < pattern.length; i += 2) {\n const on = pattern[i];\n if (on === undefined) break;\n if (on > 0) {\n await haptic('tap');\n }\n const pause = pattern[i + 1];\n if (typeof pause === 'number' && pause > 0) {\n await new Promise<void>((r) => setTimeout(r, pause));\n }\n }\n return;\n }\n const host = navigator as unknown as BackupHost;\n const original = host[BACKUP_KEY];\n original?.(pattern);\n })();\n\n return true;\n}\n\nexport function installVibrateShim(): () => void {\n if (typeof navigator === 'undefined') {\n return () => {};\n }\n\n const host = navigator as unknown as BackupHost;\n if (BACKUP_KEY in host) {\n return () => uninstallVibrateShim();\n }\n\n const nav = navigator as Navigator & { vibrate?: VibrateFn };\n // Capture the native method BEFORE we patch, bound to `navigator` so our\n // fallback call keeps the correct `this` and never recurses into our shim.\n host[BACKUP_KEY] = nav.vibrate ? nav.vibrate.bind(navigator) : undefined;\n\n const snapshot = installObjectMethods(navigator, {\n vibrate: vibrateShim as (...args: never[]) => unknown,\n });\n\n if (!snapshot) {\n delete host[BACKUP_KEY];\n return () => uninstallVibrateShim();\n }\n\n host[SNAPSHOT_KEY] = snapshot;\n return uninstallVibrateShim;\n}\n\nexport function uninstallVibrateShim(): void {\n if (typeof navigator === 'undefined') return;\n const host = navigator as unknown as BackupHost;\n if (!(BACKUP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,KAAK,EACP,OAAM,OAAO,MAAM;IAErB,MAAM,QAAQ,QAAQ,IAAI;AAC1B,QAAI,OAAO,UAAU,YAAY,QAAQ,EACvC,OAAM,IAAI,SAAe,MAAM,WAAW,GAAG,MAAM,CAAC;;AAGxD;;EAGF,MAAM,WADO,UACS;AACtB,aAAW,QAAQ;KACjB;AAEJ,QAAO;;AAGT,SAAgB,qBAAiC;AAC/C,KAAI,OAAO,cAAc,YACvB,cAAa;CAGf,MAAM,OAAO;AACb,KAAI,cAAc,KAChB,cAAa,sBAAsB;CAGrC,MAAM,MAAM;AAGZ,MAAK,cAAc,IAAI,UAAU,IAAI,QAAQ,KAAK,UAAU,GAAG,KAAA;CAE/D,MAAM,WAAW,qBAAqB,WAAW,EAC/C,SAAS,aACV,CAAC;AAEF,KAAI,CAAC,UAAU;AACb,SAAO,KAAK;AACZ,eAAa,sBAAsB;;AAGrC,MAAK,gBAAgB;AACrB,QAAO;;AAGT,SAAgB,uBAA6B;AAC3C,KAAI,OAAO,cAAc,YAAa;CACtC,MAAM,OAAO;AACb,KAAI,EAAE,cAAc,MAAO;CAE3B,MAAM,WAAW,KAAK;AACtB,KAAI,SAAU,sBAAqB,SAAS;AAC5C,QAAO,KAAK;AACZ,QAAO,KAAK"}
|
|
1
|
+
{"version":3,"file":"vibrate.js","names":[],"sources":["../../src/detect.ts","../../src/shims/_install-helpers.ts","../../src/shims/vibrate.ts"],"sourcesContent":["/**\n * Environment detection: are we running inside Apps in Toss, or a plain browser?\n *\n * Strategy: call the SDK's `getAppsInTossGlobals()` — a synchronous export\n * that returns the runtime's Toss globals (deploymentId, brand name, …)\n * inside the Apps in Toss runtime and throws (RN bridge unavailable)\n * anywhere else. The SDK itself is an **optional** peer dependency; if its\n * module can't be imported we are definitely not inside Toss.\n *\n * Just having the SDK module resolvable is not enough — apps can bundle it\n * and still run in a plain browser. We need the bridge probe to confirm.\n *\n * UA sniffing (spoofable) is avoided. We do call `getAppsInTossGlobals`, but\n * that's a constant read from the bridge — no permission dialogs, no\n * analytics fire. In a plain browser the bridge lookup fails fast (sync\n * throw, microsecond-scale), so the startup cost is negligible.\n */\n\nlet cached: boolean | undefined;\n\n/**\n * Reset the cached detection result. Primarily for tests.\n */\nexport function resetDetection(): void {\n cached = undefined;\n}\n\n/**\n * Synchronous read of the cached detection result. Returns:\n * - `true` / `false` if an override is active or the async detection has\n * already resolved\n * - `undefined` if detection hasn't run yet\n *\n * Used by spec-sync APIs (e.g. `navigator.canShare`) that can't `await`\n * detection.\n */\nexport function isTossEnvironmentCached(): boolean | undefined {\n const force = globalThis.__AIT_POLYFILL_FORCE__;\n if (force === 'toss') return true;\n if (force === 'browser') return false;\n return cached;\n}\n\n/**\n * Returns `true` iff we detect we are running in an environment where the\n * Apps in Toss SDK (`@apps-in-toss/web-framework`) is present and usable.\n *\n * Async because we use dynamic `import()` to probe the optional peer dep\n * without forcing it into the consumer's bundle.\n */\nexport async function isTossEnvironment(): Promise<boolean> {\n // Override check precedes cache so `devtools` / tests can flip the result\n // mid-session without a `resetDetection()` call.\n const force = globalThis.__AIT_POLYFILL_FORCE__;\n if (force === 'toss') return true;\n if (force === 'browser') return false;\n\n if (cached !== undefined) return cached;\n\n const mod = await loadTossSdk();\n if (typeof mod?.getAppsInTossGlobals !== 'function') {\n cached = false;\n return cached;\n }\n // Inside Toss the bridge returns a populated globals object. In a plain\n // browser the RN bridge isn't attached and the call throws — that's our\n // signal. Any non-throwing call with an object return is treated as Toss.\n try {\n const globals = mod.getAppsInTossGlobals();\n cached = Boolean(globals) && typeof globals === 'object';\n } catch {\n cached = false;\n }\n return cached;\n}\n\n/**\n * Lazy SDK accessor — returns the module if available, else `null`. Callers\n * are expected to `await` and null-check. Never throws.\n */\nexport async function loadTossSdk(): Promise<typeof import('@apps-in-toss/web-framework') | null> {\n try {\n return await import('@apps-in-toss/web-framework');\n } catch {\n return null;\n }\n}\n","/**\n * Shared helpers for installing shims on `navigator`.\n *\n * Chromium now marks a handful of `navigator` properties (e.g. `geolocation`,\n * `clipboard`) as non-configurable **own** properties on the instance. That\n * means a plain `Object.defineProperty(navigator, 'x', …)` throws\n * `TypeError: Cannot redefine property`.\n *\n * The workaround is to shim at the prototype level — `Navigator.prototype`\n * keeps these as configurable accessors, so we can swap them there and every\n * instance that falls through to the prototype (including `window.navigator`)\n * sees the shim. We only reach for the prototype when the instance-level\n * assignment refuses.\n *\n * For restoration we remember the descriptor chain (instance + prototype) so\n * `uninstall()` puts the browser back in its original state.\n */\n\ntype PropertyLocation = 'instance' | 'prototype';\n\nexport interface InstallSnapshot {\n /** Where we ended up writing the shim. */\n location: PropertyLocation;\n /** Original descriptor at that location (may be undefined if nothing was there). */\n originalDescriptor: PropertyDescriptor | undefined;\n /** `true` iff the original property lived on the instance before we touched it. */\n instanceHadOwn: boolean;\n}\n\n/**\n * Install `descriptor` at `navigator[prop]`. Prefer instance-level; if the\n * browser refuses (property is non-configurable on the instance), install on\n * `Navigator.prototype` instead.\n *\n * Returns a snapshot describing where the original value was, which\n * `restoreNavigatorProperty` uses to undo the install.\n */\nexport function installNavigatorProperty(\n prop: string,\n descriptor: PropertyDescriptor,\n): InstallSnapshot {\n const nav = navigator as unknown as Record<PropertyKey, unknown>;\n const instanceDesc = Object.getOwnPropertyDescriptor(nav, prop);\n const instanceHadOwn = instanceDesc !== undefined;\n\n // Fast path: instance-level property is missing or configurable.\n if (!instanceDesc || instanceDesc.configurable) {\n try {\n Object.defineProperty(nav, prop, descriptor);\n return { location: 'instance', originalDescriptor: instanceDesc, instanceHadOwn };\n } catch {\n // Fall through to prototype-level install.\n }\n }\n\n // Prototype-level install. Drop the instance-level shadow so the prototype\n // accessor is visible to readers on `navigator`.\n const proto = Object.getPrototypeOf(nav) as object;\n const protoDesc = Object.getOwnPropertyDescriptor(proto, prop);\n\n if (instanceHadOwn) {\n // Try to remove the instance-level shadow. On non-configurable it throws —\n // we deliberately ignore that; prototype-level install still wins because\n // the prototype accessor shows through when we read via `navigator[prop]`.\n try {\n delete nav[prop];\n } catch {\n /* non-configurable own — leave it; prototype install still useful */\n }\n }\n\n Object.defineProperty(proto, prop, descriptor);\n return { location: 'prototype', originalDescriptor: protoDesc, instanceHadOwn };\n}\n\n/**\n * Reverse the install recorded in `snapshot`. If the original descriptor was\n * `undefined` (property didn't exist before), delete the property instead of\n * re-defining it.\n */\nexport function restoreNavigatorProperty(prop: string, snapshot: InstallSnapshot): void {\n const target =\n snapshot.location === 'instance'\n ? (navigator as unknown as Record<PropertyKey, unknown>)\n : (Object.getPrototypeOf(navigator) as object);\n\n if (snapshot.originalDescriptor) {\n try {\n Object.defineProperty(target, prop, snapshot.originalDescriptor);\n } catch {\n /* descriptor was non-configurable upstream; we can't undo — rare. */\n }\n } else {\n try {\n delete (target as Record<PropertyKey, unknown>)[prop];\n } catch {\n /* non-configurable — rare. */\n }\n }\n\n // If our install pushed past an instance shadow, we leave the instance alone\n // — the descriptor we captured for `instanceHadOwn: true` lives on the\n // instance and was not modified at install time.\n}\n\n/**\n * Method-level install snapshot. Captured per-key so `restoreObjectMethods`\n * can distinguish \"was an own property, reassign it\" from \"was inherited,\n * delete the override so the prototype method surfaces again\".\n */\nexport interface MethodInstallSnapshot {\n target: object;\n methods: Record<string, { hadOwn: boolean; original: unknown }>;\n}\n\n/**\n * Mutate methods on an existing object rather than replacing the object\n * itself. This is the path we take for `navigator.geolocation`, `navigator.share`,\n * and `navigator.vibrate` in Chromium, where the slot on `navigator` is a\n * non-configurable own property that we cannot replace — but the methods\n * themselves (or the methods on the referenced object) are still\n * `configurable: true, writable: true`.\n *\n * Each replacement is installed via plain assignment. If any slot is not\n * writable (e.g. frozen object), install is aborted and previously-applied\n * replacements are rolled back, so the caller observes an atomic \"all or\n * nothing\" failure as `null`. The caller is expected to degrade gracefully\n * (e.g. log a one-time `console.warn`) when that happens.\n */\nexport function installObjectMethods(\n target: object,\n replacements: Record<string, (...args: never[]) => unknown>,\n): MethodInstallSnapshot | null {\n const methods: Record<string, { hadOwn: boolean; original: unknown }> = {};\n const applied: string[] = [];\n const bag = target as Record<string, unknown>;\n\n for (const key of Object.keys(replacements)) {\n const hadOwn = Object.hasOwn(target, key);\n const original = bag[key];\n try {\n bag[key] = replacements[key] as unknown;\n } catch {\n // Non-writable / frozen. Roll back and return null.\n for (const applieKey of applied) {\n const prev = methods[applieKey];\n if (!prev) continue;\n if (prev.hadOwn) {\n bag[applieKey] = prev.original;\n } else {\n delete bag[applieKey];\n }\n }\n return null;\n }\n // Verify the assignment actually stuck — silent-failure descriptors (e.g.\n // `writable: false` without strict mode) can skip the throw and leave the\n // original value in place. Treat that the same as a throw.\n if (bag[key] !== (replacements[key] as unknown)) {\n for (const applieKey of applied) {\n const prev = methods[applieKey];\n if (!prev) continue;\n if (prev.hadOwn) {\n bag[applieKey] = prev.original;\n } else {\n delete bag[applieKey];\n }\n }\n return null;\n }\n methods[key] = { hadOwn, original };\n applied.push(key);\n }\n\n return { target, methods };\n}\n\n/**\n * Reverse an `installObjectMethods` snapshot. Reassigns originals for slots\n * that were own properties before install; deletes the override for slots\n * that were inherited (so the prototype method surfaces again).\n */\nexport function restoreObjectMethods(snapshot: MethodInstallSnapshot): void {\n const bag = snapshot.target as Record<string, unknown>;\n for (const key of Object.keys(snapshot.methods)) {\n const entry = snapshot.methods[key];\n if (!entry) continue;\n try {\n if (entry.hadOwn) {\n bag[key] = entry.original;\n } else {\n delete bag[key];\n }\n } catch {\n /* frozen between install and restore — rare. */\n }\n }\n}\n","/**\n * `navigator.vibrate` shim.\n *\n * Inside Apps in Toss → best-effort mapping to SDK `generateHapticFeedback`.\n * Single-duration calls land in three buckets so the qualitative SDK haptic\n * tracks intensity a little more closely than the original two-bucket split:\n * - `vibrate(0)` → no-op (web standard: cancels pending vibration)\n * - `vibrate(1..20ms)` → `tickWeak`\n * - `vibrate(21..45ms)` → `tickMedium`\n * - `vibrate(>=46ms)` → `basicMedium`\n * - `vibrate(number[])` → iterates \"on\" segments (even indices) as `tap`\n * pulses with `setTimeout` gaps. Per-element ms mapping is intentionally\n * skipped: arrays in the wild are mostly rhythmic patterns, and the SDK\n * has no \"stronger heavy\" variant to reach for, so per-pulse precision\n * buys little. Callers needing intent should use `vibrateSemantic`.\n *\n * Outside Apps in Toss → defers to the browser's native `navigator.vibrate`,\n * or returns `false` when unavailable (matches the spec — browsers that don't\n * support vibration simply return `false`).\n *\n * Install strategy: **method-level** on `navigator`. We assign our wrapper to\n * `navigator.vibrate` (creating an own shadow over the prototype method) and\n * delete it on uninstall so the prototype re-surfaces. We do not mutate\n * `Navigator.prototype` itself — browsers may mark it non-configurable.\n *\n * Caveats (documented in CLAUDE.md as the known lossy trade-off):\n * - SDK haptics are qualitative (\"tickWeak\", \"basicMedium\"), not millisecond\n * durations. The shim approximates intensity from duration but cannot\n * reproduce exact patterns. Length-only mapping cannot recover semantic\n * intent (success vs. error vs. warning); use `vibrateSemantic` for that.\n * - Arrays are fired sequentially via `setTimeout`; gaps between pulses are\n * honoured only as \"time until the next tap\", not as silent-vs-vibrating.\n * - `vibrate` is spec'd as **synchronous**; the SDK call is async. We return\n * `true` immediately (fire-and-forget). Errors from the SDK are swallowed.\n */\n\nimport { isTossEnvironment, loadTossSdk } from '../detect.js';\nimport {\n installObjectMethods,\n type MethodInstallSnapshot,\n restoreObjectMethods,\n} from './_install-helpers.js';\n\nconst BACKUP_KEY = Symbol.for('@ait-co/polyfill/vibrate.original');\nconst SNAPSHOT_KEY = Symbol.for('@ait-co/polyfill/vibrate.snapshot');\n\ntype VibrateFn = (pattern: VibratePattern) => boolean;\n\ninterface BackupHost {\n [BACKUP_KEY]?: VibrateFn | undefined;\n [SNAPSHOT_KEY]?: MethodInstallSnapshot | undefined;\n}\n\n// Mapping thresholds chosen so the existing dog-food cases keep their old\n// haptic (vibrate(20) → tickWeak, vibrate(50)+ → basicMedium) while a 21–45ms\n// nudge — too long for \"tick\", too short for \"basic\" — gets `tickMedium`.\nconst TICK_WEAK_MAX_MS = 20;\nconst TICK_MEDIUM_MAX_MS = 45;\n\ntype HapticType =\n | 'tickWeak'\n | 'tap'\n | 'tickMedium'\n | 'softMedium'\n | 'basicWeak'\n | 'basicMedium'\n | 'success'\n | 'error'\n | 'wiggle'\n | 'confetti';\n\nexport async function haptic(type: HapticType): Promise<void> {\n const sdk = await loadTossSdk();\n const fn = (sdk as { generateHapticFeedback?: unknown } | null)?.generateHapticFeedback;\n if (typeof fn === 'function') {\n try {\n await (fn as (o: { type: HapticType }) => Promise<void>)({ type });\n } catch {\n // Best-effort; spec-level `vibrate` cannot surface errors.\n }\n }\n}\n\nfunction durationToHaptic(duration: number): HapticType {\n if (duration <= TICK_WEAK_MAX_MS) return 'tickWeak';\n if (duration <= TICK_MEDIUM_MAX_MS) return 'tickMedium';\n return 'basicMedium';\n}\n\nfunction vibrateShim(pattern: VibratePattern): boolean {\n // Matches the spec: `vibrate(0)` or `vibrate([])` cancels pending vibration.\n // We can't cancel an in-flight SDK haptic (no cancel API), but we still\n // forward the cancel to the browser fallback so native vibration stops.\n const arr = Array.isArray(pattern) ? pattern : [pattern];\n if (arr.length === 0 || arr.every((n) => n === 0)) {\n void (async () => {\n if (!(await isTossEnvironment())) {\n const host = navigator as unknown as BackupHost;\n host[BACKUP_KEY]?.(pattern);\n }\n })();\n return true;\n }\n\n void (async () => {\n if (await isTossEnvironment()) {\n if (!Array.isArray(pattern)) {\n await haptic(durationToHaptic(pattern));\n return;\n }\n // Even indices = \"on\" durations, odd indices = pauses. `pattern[i]` is\n // `number | undefined` under `noUncheckedIndexedAccess`; the `undefined`\n // case only arises on out-of-bounds, which our length bound prevents.\n for (let i = 0; i < pattern.length; i += 2) {\n const on = pattern[i];\n if (on === undefined) break;\n if (on > 0) {\n await haptic('tap');\n }\n const pause = pattern[i + 1];\n if (typeof pause === 'number' && pause > 0) {\n await new Promise<void>((r) => setTimeout(r, pause));\n }\n }\n return;\n }\n const host = navigator as unknown as BackupHost;\n const original = host[BACKUP_KEY];\n original?.(pattern);\n })();\n\n return true;\n}\n\nexport function installVibrateShim(): () => void {\n if (typeof navigator === 'undefined') {\n return () => {};\n }\n\n const host = navigator as unknown as BackupHost;\n if (BACKUP_KEY in host) {\n return () => uninstallVibrateShim();\n }\n\n const nav = navigator as Navigator & { vibrate?: VibrateFn };\n // Capture the native method BEFORE we patch, bound to `navigator` so our\n // fallback call keeps the correct `this` and never recurses into our shim.\n host[BACKUP_KEY] = nav.vibrate ? nav.vibrate.bind(navigator) : undefined;\n\n const snapshot = installObjectMethods(navigator, {\n vibrate: vibrateShim as (...args: never[]) => unknown,\n });\n\n if (!snapshot) {\n delete host[BACKUP_KEY];\n return () => uninstallVibrateShim();\n }\n\n host[SNAPSHOT_KEY] = snapshot;\n return uninstallVibrateShim;\n}\n\nexport function uninstallVibrateShim(): void {\n if (typeof navigator === 'undefined') return;\n const host = navigator as unknown as BackupHost;\n if (!(BACKUP_KEY in host)) return;\n\n const snapshot = host[SNAPSHOT_KEY];\n if (snapshot) restoreObjectMethods(snapshot);\n delete host[BACKUP_KEY];\n delete host[SNAPSHOT_KEY];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAkBA,IAAI;;;;;;;;AAgCJ,eAAsB,oBAAsC;CAG1D,MAAM,QAAQ,WAAW;AACzB,KAAI,UAAU,OAAQ,QAAO;AAC7B,KAAI,UAAU,UAAW,QAAO;AAEhC,KAAI,WAAW,KAAA,EAAW,QAAO;CAEjC,MAAM,MAAM,MAAM,aAAa;AAC/B,KAAI,OAAO,KAAK,yBAAyB,YAAY;AACnD,WAAS;AACT,SAAO;;AAKT,KAAI;EACF,MAAM,UAAU,IAAI,sBAAsB;AAC1C,WAAS,QAAQ,QAAQ,IAAI,OAAO,YAAY;SAC1C;AACN,WAAS;;AAEX,QAAO;;;;;;AAOT,eAAsB,cAA4E;AAChG,KAAI;AACF,SAAO,MAAM,OAAO;SACd;AACN,SAAO;;;;;;;;;;;;;;;;;;;AC6CX,SAAgB,qBACd,QACA,cAC8B;CAC9B,MAAM,UAAkE,EAAE;CAC1E,MAAM,UAAoB,EAAE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C,MAAM,OAAO;AACb,KAAI,EAAE,cAAc,MAAO;CAE3B,MAAM,WAAW,KAAK;AACtB,KAAI,SAAU,sBAAqB,SAAS;AAC5C,QAAO,KAAK;AACZ,QAAO,KAAK"}
|
package/package.json
CHANGED
|
@@ -1,52 +1,106 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ait-co/polyfill",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"description": "Polyfill so you can build Apps in Toss mini-apps with standard Web APIs (navigator.clipboard, navigator.geolocation, ...) instead of the proprietary SDK",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
7
7
|
"node": ">=20"
|
|
8
8
|
},
|
|
9
|
-
"main": "./dist/index.
|
|
9
|
+
"main": "./dist/index.cjs",
|
|
10
|
+
"module": "./dist/index.js",
|
|
10
11
|
"types": "./dist/index.d.ts",
|
|
11
12
|
"exports": {
|
|
12
13
|
".": {
|
|
13
|
-
"
|
|
14
|
-
|
|
14
|
+
"import": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"require": {
|
|
19
|
+
"types": "./dist/index.d.cts",
|
|
20
|
+
"default": "./dist/index.cjs"
|
|
21
|
+
}
|
|
15
22
|
},
|
|
16
23
|
"./clipboard": {
|
|
17
|
-
"
|
|
18
|
-
|
|
24
|
+
"import": {
|
|
25
|
+
"types": "./dist/shims/clipboard.d.ts",
|
|
26
|
+
"default": "./dist/shims/clipboard.js"
|
|
27
|
+
},
|
|
28
|
+
"require": {
|
|
29
|
+
"types": "./dist/shims/clipboard.d.cts",
|
|
30
|
+
"default": "./dist/shims/clipboard.cjs"
|
|
31
|
+
}
|
|
19
32
|
},
|
|
20
33
|
"./geolocation": {
|
|
21
|
-
"
|
|
22
|
-
|
|
34
|
+
"import": {
|
|
35
|
+
"types": "./dist/shims/geolocation.d.ts",
|
|
36
|
+
"default": "./dist/shims/geolocation.js"
|
|
37
|
+
},
|
|
38
|
+
"require": {
|
|
39
|
+
"types": "./dist/shims/geolocation.d.cts",
|
|
40
|
+
"default": "./dist/shims/geolocation.cjs"
|
|
41
|
+
}
|
|
23
42
|
},
|
|
24
43
|
"./share": {
|
|
25
|
-
"
|
|
26
|
-
|
|
44
|
+
"import": {
|
|
45
|
+
"types": "./dist/shims/share.d.ts",
|
|
46
|
+
"default": "./dist/shims/share.js"
|
|
47
|
+
},
|
|
48
|
+
"require": {
|
|
49
|
+
"types": "./dist/shims/share.d.cts",
|
|
50
|
+
"default": "./dist/shims/share.cjs"
|
|
51
|
+
}
|
|
27
52
|
},
|
|
28
53
|
"./vibrate": {
|
|
29
|
-
"
|
|
30
|
-
|
|
54
|
+
"import": {
|
|
55
|
+
"types": "./dist/shims/vibrate.d.ts",
|
|
56
|
+
"default": "./dist/shims/vibrate.js"
|
|
57
|
+
},
|
|
58
|
+
"require": {
|
|
59
|
+
"types": "./dist/shims/vibrate.d.cts",
|
|
60
|
+
"default": "./dist/shims/vibrate.cjs"
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"./vibrate-semantic": {
|
|
64
|
+
"types": "./dist/shims/vibrate-semantic.d.ts",
|
|
65
|
+
"import": "./dist/shims/vibrate-semantic.js"
|
|
31
66
|
},
|
|
32
67
|
"./network": {
|
|
33
|
-
"
|
|
34
|
-
|
|
68
|
+
"import": {
|
|
69
|
+
"types": "./dist/shims/network.d.ts",
|
|
70
|
+
"default": "./dist/shims/network.js"
|
|
71
|
+
},
|
|
72
|
+
"require": {
|
|
73
|
+
"types": "./dist/shims/network.d.cts",
|
|
74
|
+
"default": "./dist/shims/network.cjs"
|
|
75
|
+
}
|
|
35
76
|
},
|
|
36
77
|
"./detect": {
|
|
37
|
-
"
|
|
38
|
-
|
|
78
|
+
"import": {
|
|
79
|
+
"types": "./dist/detect.d.ts",
|
|
80
|
+
"default": "./dist/detect.js"
|
|
81
|
+
},
|
|
82
|
+
"require": {
|
|
83
|
+
"types": "./dist/detect.d.cts",
|
|
84
|
+
"default": "./dist/detect.cjs"
|
|
85
|
+
}
|
|
39
86
|
},
|
|
40
87
|
"./auto": {
|
|
41
|
-
"
|
|
42
|
-
|
|
88
|
+
"import": {
|
|
89
|
+
"types": "./dist/auto.d.ts",
|
|
90
|
+
"default": "./dist/auto.js"
|
|
91
|
+
},
|
|
92
|
+
"require": {
|
|
93
|
+
"types": "./dist/auto.d.cts",
|
|
94
|
+
"default": "./dist/auto.cjs"
|
|
95
|
+
}
|
|
43
96
|
}
|
|
44
97
|
},
|
|
45
98
|
"files": [
|
|
46
99
|
"dist"
|
|
47
100
|
],
|
|
48
101
|
"sideEffects": [
|
|
49
|
-
"./dist/auto.js"
|
|
102
|
+
"./dist/auto.js",
|
|
103
|
+
"./dist/auto.cjs"
|
|
50
104
|
],
|
|
51
105
|
"peerDependencies": {
|
|
52
106
|
"@apps-in-toss/web-framework": ">=2.4.0 <3.0.0"
|