Skip to content

Commit 347c8fb

Browse files
committed
refactor: share push JSON detection and clean extras validation
1 parent ea1c4d7 commit 347c8fb

4 files changed

Lines changed: 17 additions & 17 deletions

File tree

src/core/dispatch.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { isDeepLinkTarget } from './open-target.ts';
2020
import type { RawSnapshotNode } from '../utils/snapshot.ts';
2121
import type { CliFlags } from '../utils/command-schema.ts';
2222
import { emitDiagnostic, withDiagnosticTimer } from '../utils/diagnostics.ts';
23+
import { looksLikeInlineJson } from '../utils/json-input.ts';
2324

2425
export type BatchStep = {
2526
command: string;
@@ -557,10 +558,6 @@ async function readNotificationPayload(payloadArg: string): Promise<Record<strin
557558
}
558559
}
559560

560-
function looksLikeInlineJson(value: string): boolean {
561-
return (value.startsWith('{') && value.endsWith('}')) || (value.startsWith('[') && value.endsWith(']'));
562-
}
563-
564561
async function resolvePushPayloadText(payloadArg: string, trimmedArg: string): Promise<string> {
565562
const filePayload = await tryReadPushPayloadFile(payloadArg);
566563
if (filePayload !== null) return filePayload;

src/daemon/handlers/session.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
isClickLikeCommand,
3333
parseReplaySeriesFlags,
3434
} from '../script-utils.ts';
35+
import { looksLikeInlineJson } from '../../utils/json-input.ts';
3536

3637
type ReinstallOps = {
3738
ios: (device: DeviceInfo, app: string, appPath: string) => Promise<{ bundleId: string }>;
@@ -703,10 +704,7 @@ function maybeResolvePushPayloadPath(payloadArg: string, cwd?: string): string {
703704
}
704705
return resolvedPath;
705706
}
706-
if (
707-
(trimmed.startsWith('{') && trimmed.endsWith('}')) ||
708-
(trimmed.startsWith('[') && trimmed.endsWith(']'))
709-
) {
707+
if (looksLikeInlineJson(trimmed)) {
710708
return trimmed;
711709
}
712710
throw new AppError('INVALID_ARGS', `Push payload file not found: ${resolvedPath}`);

src/platforms/android/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -595,17 +595,17 @@ export async function pushAndroidNotification(
595595
if (receiver) {
596596
args.push('-n', receiver);
597597
}
598-
const extras = payload.extras ?? {};
599-
let extrasCount = 0;
600-
if (extras && typeof extras === 'object' && !Array.isArray(extras)) {
601-
for (const [key, rawValue] of Object.entries(extras)) {
602-
if (!key) continue;
603-
appendBroadcastExtra(args, key, rawValue);
604-
extrasCount += 1;
605-
}
606-
} else if (payload.extras !== undefined) {
598+
const rawExtras = payload.extras;
599+
if (rawExtras !== undefined && (typeof rawExtras !== 'object' || rawExtras === null || Array.isArray(rawExtras))) {
607600
throw new AppError('INVALID_ARGS', 'Android push payload extras must be an object');
608601
}
602+
const extras = rawExtras ?? {};
603+
let extrasCount = 0;
604+
for (const [key, rawValue] of Object.entries(extras)) {
605+
if (!key) continue;
606+
appendBroadcastExtra(args, key, rawValue);
607+
extrasCount += 1;
608+
}
609609
await runCmd('adb', adbArgs(device, args));
610610
return { action, extrasCount };
611611
}

src/utils/json-input.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function looksLikeInlineJson(value: string): boolean {
2+
const trimmed = value.trim();
3+
return (trimmed.startsWith('{') && trimmed.endsWith('}')) || (trimmed.startsWith('[') && trimmed.endsWith(']'));
4+
}
5+

0 commit comments

Comments
 (0)