wayBetter for X

This commit is contained in:
CRTkafa 2026-10-01 06:01:13 +07:00
commit 2f1e35ba20
720 changed files with 147465 additions and 0 deletions

View file

@ -0,0 +1,51 @@
import { describe, expect, test } from 'bun:test';
import { Hotfix, NativeRelease, compareSemver, pickUpdate } from './index';
describe('compareSemver', () => {
test('orders numerically, not lexically', () => {
expect(compareSemver('0.10.0', '0.9.9')).toBe(1);
expect(compareSemver('1.0.0', '1.0.0')).toBe(0);
expect(compareSemver('1.2.3', '1.2.4')).toBe(-1);
});
test('prereleases sort before their release', () => {
expect(compareSemver('1.0.0-beta.2', '1.0.0')).toBe(-1);
expect(compareSemver('1.0.0-beta.10', '1.0.0-beta.2')).toBe(1);
expect(compareSemver('1.0.0-alpha', '1.0.0-beta')).toBe(-1);
});
test('rejects invalid versions', () => {
expect(() => compareSemver('v1', '1.0.0')).toThrow();
});
});
describe('pickUpdate', () => {
const release = NativeRelease.parse({
version: '0.3.0',
pubDate: '2026-09-27T12:00:00Z',
platforms: { 'windows-x86_64': { url: 'https://example.invalid/app.zip', signature: 'sig' } },
});
test('offers a newer release for a published platform', () => {
expect(pickUpdate(release, 'windows', 'x86_64', '0.2.0')?.version).toBe('0.3.0');
});
test('offers nothing when current, newer, or the platform is missing', () => {
expect(pickUpdate(release, 'windows', 'x86_64', '0.3.0')).toBeNull();
expect(pickUpdate(release, 'windows', 'x86_64', '1.0.0')).toBeNull();
expect(pickUpdate(release, 'darwin', 'aarch64', '0.2.0')).toBeNull();
});
test('refuses plain http artifact URLs', () => {
expect(() => NativeRelease.parse({ ...release, platforms: { 'windows-x86_64': { url: 'http://x.invalid/a', signature: 's' } } })).toThrow();
});
});
describe('Hotfix', () => {
const base = { version: 1, expires: '2099-01-01T00:00:00Z', minNative: '0.2.0' };
test('accepts data and fills defaults', () => {
expect(Hotfix.parse(base)).toEqual({ ...base, selectors: {}, flags: {} });
expect(Hotfix.parse({ ...base, selectors: { composer: 'div[role=textbox]' }, flags: { a: true, b: 2, c: 'x' } }).flags.b).toBe(2);
});
test('rejects unknown keys, nested flags and bad selector keys', () => {
expect(Hotfix.safeParse({ ...base, script: 'alert(1)' }).success).toBe(false);
expect(Hotfix.safeParse({ ...base, flags: { a: { b: 1 } } }).success).toBe(false);
expect(Hotfix.safeParse({ ...base, selectors: { 'bad key': 'div' } }).success).toBe(false);
expect(Hotfix.safeParse({ ...base, version: -1 }).success).toBe(false);
});
});

View file

@ -0,0 +1,96 @@
// Shared wire contracts between the wayBetter for X desktop app and its update API.
import { z } from 'zod';
/** Tauri updater `{{target}}` values. */
export const UpdateTarget = z.enum(['windows', 'darwin', 'linux']);
/** Tauri updater `{{arch}}` values. */
export const UpdateArch = z.enum(['x86_64', 'aarch64', 'i686', 'armv7']);
export type UpdateTarget = z.infer<typeof UpdateTarget>;
export type UpdateArch = z.infer<typeof UpdateArch>;
const SEMVER = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z.-]+))?$/;
export const Semver = z.string().regex(SEMVER, 'expected a semantic version like 1.2.3');
const HttpsUrl = z.string().url().refine(value => value.startsWith('https://'), 'https URLs only');
/** One signed native artifact, keyed by `<target>-<arch>` (Tauri's platform key). */
export const NativeArtifact = z.object({
url: HttpsUrl,
/** minisign signature produced by `tauri signer sign` (base64 text). */
signature: z.string().min(1),
});
/** A published native release, as the API stores it. */
export const NativeRelease = z.object({
version: Semver,
notes: z.string().max(10_000).default(''),
pubDate: z.string().datetime(),
platforms: z.record(z.string().regex(/^(windows|darwin|linux)-(x86_64|aarch64|i686|armv7)$/), NativeArtifact),
});
export type NativeRelease = z.infer<typeof NativeRelease>;
/** Body of Tauri's dynamic update endpoint when an update is available (HTTP 200). */
export const TauriUpdateResponse = z.object({
version: Semver,
notes: z.string(),
pub_date: z.string().datetime(),
url: HttpsUrl,
signature: z.string().min(1),
});
export type TauriUpdateResponse = z.infer<typeof TauriUpdateResponse>;
/** Compares two semantic versions; prerelease versions sort before their release. */
export function compareSemver(a: string, b: string): number {
const pa = SEMVER.exec(a);
const pb = SEMVER.exec(b);
if (!pa || !pb) throw new Error(`invalid semver: ${pa ? b : a}`);
for (let i = 1; i <= 3; i++) {
const diff = Number(pa[i]) - Number(pb[i]);
if (diff !== 0) return Math.sign(diff);
}
const [preA, preB] = [pa[4], pb[4]];
if (preA === preB) return 0;
if (preA === undefined) return 1;
if (preB === undefined) return -1;
const partsA = preA.split('.');
const partsB = preB.split('.');
for (let i = 0; i < Math.max(partsA.length, partsB.length); i++) {
const [x, y] = [partsA[i], partsB[i]];
if (x === undefined) return -1;
if (y === undefined) return 1;
const [nx, ny] = [/^\d+$/.test(x), /^\d+$/.test(y)];
if (nx && ny && Number(x) !== Number(y)) return Math.sign(Number(x) - Number(y));
if (nx !== ny) return nx ? -1 : 1;
if (x !== y) return x < y ? -1 : 1;
}
return 0;
}
/** The update to offer a client on `target`/`arch` running `current`, or null. */
export function pickUpdate(release: NativeRelease, target: UpdateTarget, arch: UpdateArch, current: string): TauriUpdateResponse | null {
if (compareSemver(release.version, current) <= 0) return null;
const artifact = release.platforms[`${target}-${arch}`];
if (!artifact) return null;
return { version: release.version, notes: release.notes, pub_date: release.pubDate, url: artifact.url, signature: artifact.signature };
}
/** Update channels. */
export const Channel = z.enum(['stable', 'beta']);
export type Channel = z.infer<typeof Channel>;
const HotfixKey = z.string().regex(/^[A-Za-z0-9_.-]{1,64}$/);
/**
* Signed data-only hotfix (selectors and flags for x.com markup changes). Never code.
* Mirrors `Hotfix` in apps/desktop/src-tauri/src/hotfix.rs, which re-validates it.
*/
export const Hotfix = z.object({
/** Monotonic: clients refuse anything at or below the version they hold. */
version: z.number().int().nonnegative(),
/** Clients ignore the hotfix after this instant (blocks stale replay). */
expires: z.string().datetime({ offset: true }),
minNative: Semver,
selectors: z.record(HotfixKey, z.string().min(1).max(1024)).default({}),
flags: z.record(HotfixKey, z.union([z.boolean(), z.number(), z.string().max(256)])).default({}),
}).strict();
export type Hotfix = z.infer<typeof Hotfix>;