ink-framework-contacts 0.0.0-stage → 0.1.0-alpha.16
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 +21 -0
- package/ink-native.json +6 -0
- package/package.json +20 -4
- package/src/decode.ts +51 -0
- package/src/index.ts +31 -0
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Vandam Dinh
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/ink-native.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ink-framework-contacts",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
}
|
|
3
|
+
"version": "0.1.0-alpha.16",
|
|
4
|
+
"exports": {
|
|
5
|
+
".": "./src/index.ts"
|
|
6
|
+
},
|
|
7
|
+
"peerDependencies": {
|
|
8
|
+
"ink": "npm:ink-framework@0.1.0-alpha.16"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/No-Scrolling/ink.git",
|
|
14
|
+
"directory": "packages/contacts"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"src",
|
|
18
|
+
"*.json",
|
|
19
|
+
"*.ts",
|
|
20
|
+
"*.md"
|
|
21
|
+
]
|
|
22
|
+
}
|
package/src/decode.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { NativeError } from "ink/native";
|
|
2
|
+
import type { Contact, ContactDetails, LabeledValue } from "./index";
|
|
3
|
+
|
|
4
|
+
const LISTS = ["phones", "emails", "addresses", "websites", "events"] as const;
|
|
5
|
+
|
|
6
|
+
function isContact(value: unknown): value is Contact {
|
|
7
|
+
return typeof value === "object" && value !== null
|
|
8
|
+
&& typeof Reflect.get(value, "id") === "string" && typeof Reflect.get(value, "name") === "string";
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function isLabeledValue(value: unknown): value is LabeledValue {
|
|
12
|
+
return typeof value === "object" && value !== null
|
|
13
|
+
&& typeof Reflect.get(value, "label") === "string" && typeof Reflect.get(value, "value") === "string";
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function decodeContactPage(json: string, offset: number): { contacts: Contact[]; next: number | null } {
|
|
17
|
+
const page: unknown = JSON.parse(json);
|
|
18
|
+
if (typeof page !== "object" || page === null) throw new NativeError("protocol", "Invalid contact list");
|
|
19
|
+
const contacts: unknown = Reflect.get(page, "contacts");
|
|
20
|
+
const next: unknown = Reflect.get(page, "next");
|
|
21
|
+
if (!Array.isArray(contacts) || !contacts.every(isContact) || !(next === null || typeof next === "number" && Number.isSafeInteger(next))) {
|
|
22
|
+
throw new NativeError("protocol", "Invalid contact list");
|
|
23
|
+
}
|
|
24
|
+
if (next !== null && next <= offset) throw new NativeError("protocol", "Contact pagination did not advance");
|
|
25
|
+
return { contacts, next };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Joins pages into one list. A contact that moves between pages while they load is kept once, where it first appeared. */
|
|
29
|
+
export async function collectContacts(load: (offset: number) => Promise<string>, signal?: AbortSignal): Promise<Contact[]> {
|
|
30
|
+
const contacts = new Map<string, Contact>();
|
|
31
|
+
for (let offset: number | null = 0; offset !== null;) {
|
|
32
|
+
signal?.throwIfAborted();
|
|
33
|
+
const page = decodeContactPage(await load(offset), offset);
|
|
34
|
+
for (const contact of page.contacts) contacts.set(contact.id, contact);
|
|
35
|
+
offset = page.next;
|
|
36
|
+
}
|
|
37
|
+
return [...contacts.values()];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function decodeContactDetails(json: string): ContactDetails | null {
|
|
41
|
+
const contact: unknown = JSON.parse(json);
|
|
42
|
+
if (contact === null) return null;
|
|
43
|
+
if (!isContact(contact) || !LISTS.every(key => {
|
|
44
|
+
const list: unknown = Reflect.get(contact, key);
|
|
45
|
+
return Array.isArray(list) && list.every(isLabeledValue);
|
|
46
|
+
}) || !["organization", "note"].every(key => {
|
|
47
|
+
const text: unknown = Reflect.get(contact, key);
|
|
48
|
+
return text === null || typeof text === "string";
|
|
49
|
+
})) throw new NativeError("protocol", "Invalid contact details");
|
|
50
|
+
return contact as ContactDetails;
|
|
51
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { callNative } from "ink/native";
|
|
2
|
+
import { collectContacts, decodeContactDetails } from "./decode";
|
|
3
|
+
|
|
4
|
+
export type Contact = { id: string; name: string };
|
|
5
|
+
export type LabeledValue = { label: string; value: string };
|
|
6
|
+
export type ContactDetails = {
|
|
7
|
+
id: string;
|
|
8
|
+
name: string;
|
|
9
|
+
phones: LabeledValue[];
|
|
10
|
+
emails: LabeledValue[];
|
|
11
|
+
addresses: LabeledValue[];
|
|
12
|
+
websites: LabeledValue[];
|
|
13
|
+
events: LabeledValue[];
|
|
14
|
+
organization: string | null;
|
|
15
|
+
note: string | null;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// Each page is one bridge message, capped at 1 MiB. Rows are typically 100–200 bytes, so most address books
|
|
19
|
+
// take one call; Android also ends a page at 512 KiB, which covers unusually long names and lookup keys.
|
|
20
|
+
const PAGE_SIZE = 2000;
|
|
21
|
+
|
|
22
|
+
export const contacts = {
|
|
23
|
+
/** Every contact with a name, sorted by name. `query` filters names the way Android's contact search does. */
|
|
24
|
+
async list(options: { query?: string; signal?: AbortSignal } = {}): Promise<Contact[]> {
|
|
25
|
+
return collectContacts(offset => callNative("contacts", "list", { query: options.query, offset, limit: PAGE_SIZE }, { timeoutMs: 120_000, signal: options.signal }), options.signal);
|
|
26
|
+
},
|
|
27
|
+
/** Resolves with the contact's details, or null when it no longer exists. */
|
|
28
|
+
async get(id: string, options: { signal?: AbortSignal } = {}): Promise<ContactDetails | null> {
|
|
29
|
+
return decodeContactDetails(await callNative("contacts", "get", { id }, { signal: options.signal }));
|
|
30
|
+
},
|
|
31
|
+
};
|
package/README.md
DELETED