ink-framework-nfc 0.0.0-stage → 0.1.0-alpha.2
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 +8 -0
- package/package.json +19 -4
- package/src/index.ts +129 -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,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ink-framework-nfc",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
}
|
|
3
|
+
"version": "0.1.0-alpha.2",
|
|
4
|
+
"exports": {
|
|
5
|
+
".": "./src/index.ts"
|
|
6
|
+
},
|
|
7
|
+
"peerDependencies": {
|
|
8
|
+
"ink": "npm:ink-framework@0.1.0-alpha.2"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/No-Scrolling/ink.git",
|
|
14
|
+
"directory": "packages/nfc"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"src",
|
|
18
|
+
"*.json",
|
|
19
|
+
"*.ts"
|
|
20
|
+
]
|
|
21
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { callNative, NativeError } from "ink/native";
|
|
2
|
+
|
|
3
|
+
export interface NfcRecord {
|
|
4
|
+
kind: "text" | "uri" | "binary";
|
|
5
|
+
value: string;
|
|
6
|
+
languageTag: string;
|
|
7
|
+
mimeType: string;
|
|
8
|
+
payloadBase64: string;
|
|
9
|
+
}
|
|
10
|
+
export interface NfcTag {
|
|
11
|
+
serialNumber: string;
|
|
12
|
+
hasText: boolean;
|
|
13
|
+
text: string;
|
|
14
|
+
hasUri: boolean;
|
|
15
|
+
uri: string;
|
|
16
|
+
records: readonly NfcRecord[];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function decodeRecord(value: unknown): NfcRecord {
|
|
20
|
+
if (typeof value !== "object" || value === null
|
|
21
|
+
|| !("kind" in value) || (value.kind !== "text" && value.kind !== "uri" && value.kind !== "binary")
|
|
22
|
+
|| !("value" in value) || typeof value.value !== "string"
|
|
23
|
+
|| !("languageTag" in value) || typeof value.languageTag !== "string"
|
|
24
|
+
|| !("mimeType" in value) || typeof value.mimeType !== "string"
|
|
25
|
+
|| !("payloadBase64" in value) || typeof value.payloadBase64 !== "string") {
|
|
26
|
+
throw new NativeError("protocol", "Invalid NFC record");
|
|
27
|
+
}
|
|
28
|
+
return { kind: value.kind, value: value.value, languageTag: value.languageTag,
|
|
29
|
+
mimeType: value.mimeType, payloadBase64: value.payloadBase64 };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const nfc = {
|
|
33
|
+
connect, emulate, stopEmulation, handleApdu,
|
|
34
|
+
async read({ signal, timeout = 30_000 }: { signal?: AbortSignal; timeout?: number } = {}): Promise<NfcTag> {
|
|
35
|
+
const value: unknown = JSON.parse(await callNative("nfc", "read", "", { signal, timeoutMs: timeout }));
|
|
36
|
+
if (typeof value !== "object" || value === null
|
|
37
|
+
|| !("serialNumber" in value) || typeof value.serialNumber !== "string"
|
|
38
|
+
|| !("hasText" in value) || typeof value.hasText !== "boolean"
|
|
39
|
+
|| !("text" in value) || typeof value.text !== "string"
|
|
40
|
+
|| !("hasUri" in value) || typeof value.hasUri !== "boolean"
|
|
41
|
+
|| !("uri" in value) || typeof value.uri !== "string"
|
|
42
|
+
|| !("records" in value) || !Array.isArray(value.records)) {
|
|
43
|
+
throw new NativeError("protocol", "Invalid NFC tag");
|
|
44
|
+
}
|
|
45
|
+
return { serialNumber: value.serialNumber, hasText: value.hasText, text: value.text,
|
|
46
|
+
hasUri: value.hasUri, uri: value.uri, records: value.records.map(decodeRecord) };
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export interface NfcConnection {
|
|
51
|
+
readonly serialNumber: string;
|
|
52
|
+
readonly technology: "iso-dep" | "nfc-a";
|
|
53
|
+
readonly maxTransceiveLength: number;
|
|
54
|
+
transceive(bytes: Uint8Array, options?: { signal?: AbortSignal; timeout?: number }): Promise<Uint8Array>;
|
|
55
|
+
close(): Promise<void>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export async function connect({ technology = "iso-dep", signal, timeout = 30_000 }: {
|
|
59
|
+
technology?: "iso-dep" | "nfc-a"; signal?: AbortSignal; timeout?: number;
|
|
60
|
+
} = {}): Promise<NfcConnection> {
|
|
61
|
+
const value: unknown = JSON.parse(await callNative("nfc", "connect", { technology }, { signal, timeoutMs: timeout }));
|
|
62
|
+
if (typeof value !== "object" || value === null
|
|
63
|
+
|| !("connection" in value) || typeof value.connection !== "string"
|
|
64
|
+
|| !("serialNumber" in value) || typeof value.serialNumber !== "string"
|
|
65
|
+
|| !("technology" in value) || (value.technology !== "iso-dep" && value.technology !== "nfc-a")
|
|
66
|
+
|| !("maxTransceiveLength" in value) || typeof value.maxTransceiveLength !== "number" || !Number.isSafeInteger(value.maxTransceiveLength) || value.maxTransceiveLength <= 0) {
|
|
67
|
+
throw new NativeError("protocol", "Invalid NFC connection");
|
|
68
|
+
}
|
|
69
|
+
const connection = value.connection;
|
|
70
|
+
const maxTransceiveLength = value.maxTransceiveLength;
|
|
71
|
+
return {
|
|
72
|
+
serialNumber: value.serialNumber, technology: value.technology, maxTransceiveLength: value.maxTransceiveLength,
|
|
73
|
+
async transceive(bytes, { signal, timeout = 5000 } = {}) {
|
|
74
|
+
if (!Number.isInteger(timeout) || timeout < 1 || timeout > 10_000) throw new RangeError("NFC exchange timeout must be 1–10000 milliseconds");
|
|
75
|
+
if (bytes.length < 1 || bytes.length > maxTransceiveLength) throw new RangeError("NFC command exceeds the connection limit");
|
|
76
|
+
const result: unknown = JSON.parse(await callNative("nfc", "transceive", { connection, bytes: Array.from(bytes), timeout }, { signal, timeoutMs: timeout + 1000 }));
|
|
77
|
+
if (!Array.isArray(result) || !result.every((byte: unknown) => typeof byte === "number" && Number.isInteger(byte) && byte >= 0 && byte <= 255)) {
|
|
78
|
+
throw new NativeError("protocol", "Invalid NFC response");
|
|
79
|
+
}
|
|
80
|
+
return new Uint8Array(result);
|
|
81
|
+
},
|
|
82
|
+
async close() { await callNative("nfc", "close", { connection }); },
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export async function emulate({ aids, responses = [], fallback = "6D00", deadline = 500 }: {
|
|
87
|
+
aids: readonly string[];
|
|
88
|
+
responses?: readonly { command: string; response: string }[];
|
|
89
|
+
fallback?: string;
|
|
90
|
+
deadline?: number;
|
|
91
|
+
}): Promise<void> {
|
|
92
|
+
await callNative("nfc", "emulate", { aids, responses, fallback, deadline });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export async function stopEmulation(): Promise<void> { await callNative("nfc", "stop-emulation", {}); }
|
|
96
|
+
|
|
97
|
+
let handlingApdu = false;
|
|
98
|
+
|
|
99
|
+
export async function handleApdu(handler: (command: Uint8Array) => Uint8Array | Promise<Uint8Array>, { signal }: { signal: AbortSignal }): Promise<void> {
|
|
100
|
+
if (handlingApdu) throw new NativeError("unavailable", "An APDU handler is already running");
|
|
101
|
+
handlingApdu = true;
|
|
102
|
+
try {
|
|
103
|
+
while (!signal.aborted) {
|
|
104
|
+
let value: unknown;
|
|
105
|
+
try {
|
|
106
|
+
value = JSON.parse(await callNative("nfc", "apdu-next", {}, { signal, timeoutMs: 60_000 }));
|
|
107
|
+
} catch (error) {
|
|
108
|
+
if (error instanceof NativeError && error.kind === "timeout" && !signal.aborted) continue;
|
|
109
|
+
throw error;
|
|
110
|
+
}
|
|
111
|
+
if (typeof value !== "object" || value === null || !("id" in value) || typeof value.id !== "number"
|
|
112
|
+
|| !("command" in value) || typeof value.command !== "string" || !/^(?:[0-9A-F]{2})+$/.test(value.command)) {
|
|
113
|
+
throw new NativeError("protocol", "Invalid APDU request");
|
|
114
|
+
}
|
|
115
|
+
const hex = value.command;
|
|
116
|
+
const command = Uint8Array.from({ length: hex.length / 2 }, (_, index) => parseInt(hex.slice(index * 2, index * 2 + 2), 16));
|
|
117
|
+
const response = await handler(command);
|
|
118
|
+
if (signal.aborted) break;
|
|
119
|
+
try {
|
|
120
|
+
await callNative("nfc", "apdu-response", { id: value.id, response: Array.from(response, byte => byte.toString(16).padStart(2, "0")).join("") }, { signal });
|
|
121
|
+
} catch (error) {
|
|
122
|
+
if (!(error instanceof NativeError) || error.kind !== "unavailable") throw error;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
} finally {
|
|
126
|
+
try { await callNative("nfc", "apdu-stop", {}); }
|
|
127
|
+
finally { handlingApdu = false; }
|
|
128
|
+
}
|
|
129
|
+
}
|
package/README.md
DELETED