ink-framework-location 0.0.0-stage → 0.1.0-alpha.10
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 +115 -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-location",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
}
|
|
3
|
+
"version": "0.1.0-alpha.10",
|
|
4
|
+
"exports": {
|
|
5
|
+
".": "./src/index.ts"
|
|
6
|
+
},
|
|
7
|
+
"peerDependencies": {
|
|
8
|
+
"ink": "npm:ink-framework@0.1.0-alpha.10"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/No-Scrolling/ink.git",
|
|
14
|
+
"directory": "packages/location"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"src",
|
|
18
|
+
"*.json",
|
|
19
|
+
"*.ts"
|
|
20
|
+
]
|
|
21
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { callNative, NativeError } from "ink/native";
|
|
2
|
+
|
|
3
|
+
export type LocationAccuracy = "balanced" | "high";
|
|
4
|
+
export interface DefaultLocation {
|
|
5
|
+
latitude: number;
|
|
6
|
+
longitude: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface LocationFix {
|
|
10
|
+
latitude: number;
|
|
11
|
+
longitude: number;
|
|
12
|
+
accuracy: number;
|
|
13
|
+
timestamp: number;
|
|
14
|
+
provider: string;
|
|
15
|
+
}
|
|
16
|
+
export interface LocationOptions {
|
|
17
|
+
accuracy?: LocationAccuracy;
|
|
18
|
+
maximumAge?: number;
|
|
19
|
+
timeout?: number;
|
|
20
|
+
signal?: AbortSignal;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface LocationWatchOptions {
|
|
24
|
+
accuracy?: LocationAccuracy;
|
|
25
|
+
interval?: number;
|
|
26
|
+
distance?: number;
|
|
27
|
+
signal?: AbortSignal;
|
|
28
|
+
}
|
|
29
|
+
export interface LocationTrackingState {
|
|
30
|
+
running: boolean;
|
|
31
|
+
fix: LocationFix | null;
|
|
32
|
+
error: string | null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function parseFix(value: unknown): LocationFix {
|
|
36
|
+
if (typeof value !== "object" || value === null
|
|
37
|
+
|| !("latitude" in value) || typeof value.latitude !== "number" || !Number.isFinite(value.latitude) || Math.abs(value.latitude) > 90
|
|
38
|
+
|| !("longitude" in value) || typeof value.longitude !== "number" || !Number.isFinite(value.longitude) || Math.abs(value.longitude) > 180
|
|
39
|
+
|| !("accuracy" in value) || typeof value.accuracy !== "number" || !Number.isFinite(value.accuracy) || value.accuracy < 0
|
|
40
|
+
|| !("timestamp" in value) || typeof value.timestamp !== "number" || !Number.isFinite(value.timestamp)
|
|
41
|
+
|| !("provider" in value) || typeof value.provider !== "string") {
|
|
42
|
+
throw new NativeError("protocol", "Invalid native location fix");
|
|
43
|
+
}
|
|
44
|
+
return { latitude: value.latitude, longitude: value.longitude, accuracy: value.accuracy, timestamp: value.timestamp, provider: value.provider };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function updateOptions(options: LocationWatchOptions) {
|
|
48
|
+
const { accuracy = "balanced", interval = 5_000, distance = 0 } = options;
|
|
49
|
+
if (!Number.isSafeInteger(interval) || interval < 1_000 || interval > 3_600_000) {
|
|
50
|
+
throw new RangeError("Location interval must be between one second and one hour");
|
|
51
|
+
}
|
|
52
|
+
if (!Number.isFinite(distance) || distance < 0 || distance > 100_000) {
|
|
53
|
+
throw new RangeError("Location distance must be between zero and 100,000 metres");
|
|
54
|
+
}
|
|
55
|
+
return { accuracy: accuracy === "high" ? "precise" : "approximate", intervalMs: interval, distanceMetres: distance };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function parseTracking(value: unknown): LocationTrackingState {
|
|
59
|
+
if (typeof value !== "object" || value === null || !("running" in value) || typeof value.running !== "boolean"
|
|
60
|
+
|| !("fix" in value) || !("error" in value) || value.error !== null && typeof value.error !== "string") {
|
|
61
|
+
throw new NativeError("protocol", "Invalid location tracking state");
|
|
62
|
+
}
|
|
63
|
+
return { running: value.running, fix: value.fix === null ? null : parseFix(value.fix), error: value.error };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export const location = {
|
|
67
|
+
async default(): Promise<DefaultLocation | null> {
|
|
68
|
+
const value: unknown = JSON.parse(await callNative("light-sdk", "default-location", ""));
|
|
69
|
+
if (value === null) return null;
|
|
70
|
+
if (typeof value !== "object"
|
|
71
|
+
|| !("latitude" in value) || typeof value.latitude !== "number" || !Number.isFinite(value.latitude) || Math.abs(value.latitude) > 90
|
|
72
|
+
|| !("longitude" in value) || typeof value.longitude !== "number" || !Number.isFinite(value.longitude) || Math.abs(value.longitude) > 180) {
|
|
73
|
+
throw new NativeError("protocol", "Invalid default location");
|
|
74
|
+
}
|
|
75
|
+
return { latitude: value.latitude, longitude: value.longitude };
|
|
76
|
+
},
|
|
77
|
+
async *watch(options: LocationWatchOptions = {}): AsyncGenerator<LocationFix> {
|
|
78
|
+
const watch = Number(await callNative("location", "watch-start", updateOptions(options), { signal: options.signal }));
|
|
79
|
+
if (!Number.isSafeInteger(watch) || watch <= 0) throw new NativeError("protocol", "Invalid location watch");
|
|
80
|
+
let version = 0;
|
|
81
|
+
try {
|
|
82
|
+
while (!options.signal?.aborted) {
|
|
83
|
+
const value: unknown = JSON.parse(await callNative("location", "watch-next", { watch, version }, { signal: options.signal, timeoutMs: 25_000 }));
|
|
84
|
+
if (value === null) continue;
|
|
85
|
+
if (typeof value !== "object" || !("version" in value) || typeof value.version !== "number"
|
|
86
|
+
|| !Number.isSafeInteger(value.version) || value.version <= version || !("fix" in value)) {
|
|
87
|
+
throw new NativeError("protocol", "Invalid location watch update");
|
|
88
|
+
}
|
|
89
|
+
version = value.version;
|
|
90
|
+
yield parseFix(value.fix);
|
|
91
|
+
}
|
|
92
|
+
} finally {
|
|
93
|
+
await callNative("location", "watch-stop", { watch });
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
async startTracking(options: Omit<LocationWatchOptions, "signal"> = {}): Promise<LocationTrackingState> {
|
|
97
|
+
return parseTracking(JSON.parse(await callNative("location", "tracking-start", updateOptions(options))));
|
|
98
|
+
},
|
|
99
|
+
async stopTracking(): Promise<void> {
|
|
100
|
+
await callNative("location", "tracking-stop", {});
|
|
101
|
+
},
|
|
102
|
+
async getTracking(): Promise<LocationTrackingState> {
|
|
103
|
+
return parseTracking(JSON.parse(await callNative("location", "tracking-status", {})));
|
|
104
|
+
},
|
|
105
|
+
async current(options: LocationOptions = {}): Promise<LocationFix> {
|
|
106
|
+
const { accuracy = "balanced", maximumAge = 0, timeout = 15_000, signal } = options;
|
|
107
|
+
if (!Number.isSafeInteger(maximumAge) || maximumAge < 0 || maximumAge > 3_600_000) {
|
|
108
|
+
throw new RangeError("Location maximumAge must be between zero and one hour");
|
|
109
|
+
}
|
|
110
|
+
const value: unknown = JSON.parse(await callNative("location", "current", {
|
|
111
|
+
accuracy: accuracy === "high" ? "precise" : "approximate", maxAgeMs: maximumAge,
|
|
112
|
+
}, { timeoutMs: timeout, signal }));
|
|
113
|
+
return parseFix(value);
|
|
114
|
+
},
|
|
115
|
+
};
|
package/README.md
DELETED