Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ssssota committed Nov 27, 2024
1 parent 48dd843 commit 043cc1e
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 77 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"editor.defaultFormatter": "biomejs.biome",
"editor.formatOnSave": true
"editor.defaultFormatter": "biomejs.biome",
"editor.formatOnSave": true
}
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
# apple-app-site-association

Check apple-app-site-association file.

## Usage

Reference types:

```typescript
import type { AppleAppSiteAssociation } from 'apple-app-site-association';
const aasa = {
applinks: {
// ...
}
} as const satisfies AppleAppSiteAssociation;
```

Test universal links:

```typescript
import { test } from 'node:test';
import { createAssert } from 'apple-app-site-association';
const assert = createAssert(YOUR_DOMAIN, AASA_JSON);
test("universal link works", async () => {
await assert.universalLink("/path/to/your/app", "match");
await assert.universalLink("/#donot_deeplinking", "block");
});
```

Use `swcutil` command directly:

```typescript
import { swcutil } from 'apple-app-site-association/swcutil';
const { status, stdout, stderr } = await swcutil({
// ...
});
```
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc",
"clean": "node -e \"fs.rmSync('dist',{recursive:true,force:true})\"",
"prepare": "npm run clean && npm run build"
"prepare": "npm run clean && npm run build",
"fmt": "biome check --write ."
},
"keywords": [
"aasa",
Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { swcutil } from "./swcutil.js";
export { verify, createVerifier } from "./verify.js";
export { createAssert } from "./test.js";
export { verify } from "./verify.js";
export type * from "./types.js";
3 changes: 1 addition & 2 deletions src/swcutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ export type Result = {
};

export function swcutil(options: Options): Promise<Result> {
const res = spawn("sudo", [
"swcutil",
const res = spawn("swcutil", [
...buildSharedOptionArgs(options),
...buildCommandArgs(options),
]);
Expand Down
25 changes: 0 additions & 25 deletions src/test.ts

This file was deleted.

38 changes: 2 additions & 36 deletions src/verify.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
import { swcutil } from "./swcutil.js";
import type { JsonOrPath, PromiseOr, VerifyResult } from "./types.js";

/**
* String literal types with auto-completion
* @see https://github.com/Microsoft/TypeScript/issues/29729
*/
export type StringLiteralUnion<T> = T | (string & Record<never, never>);
type AASAUrl = StringLiteralUnion<
| `https://app-site-association.cdn-apple.com/a/v1/${string}`
| `https://${string}/.well-known/apple-app-site-association`
>;

export function createVerifier(
domain: string,
/** path / object / url(download it if specified) */
json?: PromiseOr<JsonOrPath | AASAUrl>,
): (path: string) => Promise<VerifyResult> {
const aasaPromise = resolveAASA(domain, json);
return async (path: string) => await verify(domain, path, await aasaPromise);
}
import type { JsonOrPath, VerifyResult } from "./types.js";

export async function verify(
domain: string,
Expand All @@ -31,27 +12,12 @@ export async function verify(
json,
url: url(domain, path),
});
if (res.status !== 0) throw new Error(res.stderr);
const out = res.stdout.trimEnd();
if (out.endsWith("matched.")) return "match";
if (out.endsWith("blocked match.")) return "block";
return "unset";
}

function resolveAASA(
domain: string,
/** path / object / url(download it if specified) */
json?: PromiseOr<JsonOrPath | AASAUrl>,
): PromiseOr<JsonOrPath> {
if (json === undefined) {
return fetch(
`https://app-site-association.cdn-apple.com/a/v1/${domain}`,
).then((res) => res.json());
}
if (typeof json === "string" && json.startsWith("https://")) {
return fetch(json).then((res) => res.json());
}
return json;
}
function url(domain: string, path: string): string {
return new URL(path, `https://${domain}`).href;
}
22 changes: 13 additions & 9 deletions tests/node.test.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import * as assert from "node:assert";
import { test } from "node:test";
import { createAssert } from "apple-app-site-association";
const assert = createAssert("zozo.jp");
import { verify } from "apple-app-site-association";

test("single", async () => {
await assert.universalLink("/", "match");
const json = await fetch(
"https://zozo.jp/.well-known/apple-app-site-association",
).then((res) => res.json());

test("/sale/ should match", async () => {
assert.strictEqual(await verify("zozo.jp", "/sale/", json), "match");
});

test("multiple", async () => {
await assert.universalLinks([
["/", "match"],
["/#nondeeplinking", "block"],
]);
test("#nondeeplinking should block", async () => {
assert.strictEqual(
await verify("zozo.jp", "/sale/#nondeeplinking", json),
"block",
);
});

0 comments on commit 043cc1e

Please sign in to comment.