@vitrinka/link 0.1.0
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/CHANGELOG.md +7 -0
- package/LICENSE +93 -0
- package/README.md +23 -0
- package/build/index.d.ts +76 -0
- package/build/index.js +176 -0
- package/package.json +36 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
Elastic License 2.0
|
|
2
|
+
|
|
3
|
+
URL: https://www.elastic.co/licensing/elastic-license
|
|
4
|
+
|
|
5
|
+
## Acceptance
|
|
6
|
+
|
|
7
|
+
By using the software, you agree to all of the terms and conditions below.
|
|
8
|
+
|
|
9
|
+
## Copyright License
|
|
10
|
+
|
|
11
|
+
The licensor grants you a non-exclusive, royalty-free, worldwide,
|
|
12
|
+
non-sublicensable, non-transferable license to use, copy, distribute, make
|
|
13
|
+
available, and prepare derivative works of the software, in each case subject to
|
|
14
|
+
the limitations and conditions below.
|
|
15
|
+
|
|
16
|
+
## Limitations
|
|
17
|
+
|
|
18
|
+
You may not provide the software to third parties as a hosted or managed
|
|
19
|
+
service, where the service provides users with access to any substantial set of
|
|
20
|
+
the features or functionality of the software.
|
|
21
|
+
|
|
22
|
+
You may not move, change, disable, or circumvent the license key functionality
|
|
23
|
+
in the software, and you may not remove or obscure any functionality in the
|
|
24
|
+
software that is protected by the license key.
|
|
25
|
+
|
|
26
|
+
You may not alter, remove, or obscure any licensing, copyright, or other notices
|
|
27
|
+
of the licensor in the software. Any use of the licensor’s trademarks is subject
|
|
28
|
+
to applicable law.
|
|
29
|
+
|
|
30
|
+
## Patents
|
|
31
|
+
|
|
32
|
+
The licensor grants you a license, under any patent claims the licensor can
|
|
33
|
+
license, or becomes able to license, to make, have made, use, sell, offer for
|
|
34
|
+
sale, import and have imported the software, in each case subject to the
|
|
35
|
+
limitations and conditions in this license. This license does not cover any
|
|
36
|
+
patent claims that you cause to be infringed by modifications or additions to
|
|
37
|
+
the software. If you or your company make any written claim that the software
|
|
38
|
+
infringes or contributes to infringement of any patent, your patent license for
|
|
39
|
+
the software granted under these terms ends immediately. If your company makes
|
|
40
|
+
such a claim, your patent license ends immediately for work on behalf of your
|
|
41
|
+
company.
|
|
42
|
+
|
|
43
|
+
## Notices
|
|
44
|
+
|
|
45
|
+
You must ensure that anyone who gets a copy of any part of the software from you
|
|
46
|
+
also gets a copy of these terms.
|
|
47
|
+
|
|
48
|
+
If you modify the software, you must include in any modified copies of the
|
|
49
|
+
software prominent notices stating that you have modified the software.
|
|
50
|
+
|
|
51
|
+
## No Other Rights
|
|
52
|
+
|
|
53
|
+
These terms do not imply any licenses other than those expressly granted in
|
|
54
|
+
these terms.
|
|
55
|
+
|
|
56
|
+
## Termination
|
|
57
|
+
|
|
58
|
+
If you use the software in violation of these terms, such use is not licensed,
|
|
59
|
+
and your licenses will automatically terminate. If the licensor provides you
|
|
60
|
+
with a notice of your violation, and you cease all violation of this license no
|
|
61
|
+
later than 30 days after you receive that notice, your licenses will be
|
|
62
|
+
reinstated retroactively. However, if you violate these terms after such
|
|
63
|
+
reinstatement, any additional violation of these terms will cause your licenses
|
|
64
|
+
to terminate automatically and permanently.
|
|
65
|
+
|
|
66
|
+
## No Liability
|
|
67
|
+
|
|
68
|
+
*As far as the law allows, the software comes as is, without any warranty or
|
|
69
|
+
condition, and the licensor will not be liable to you for any damages arising
|
|
70
|
+
out of these terms or the use or nature of the software, under any kind of
|
|
71
|
+
legal claim.*
|
|
72
|
+
|
|
73
|
+
## Definitions
|
|
74
|
+
|
|
75
|
+
The **licensor** is the entity offering these terms, and the **software** is the
|
|
76
|
+
software the licensor makes available under these terms, including any portion
|
|
77
|
+
of it.
|
|
78
|
+
|
|
79
|
+
**you** refers to the individual or entity agreeing to these terms.
|
|
80
|
+
|
|
81
|
+
**your company** is any legal entity, sole proprietorship, or other kind of
|
|
82
|
+
organization that you work for, plus all organizations that have control over,
|
|
83
|
+
are under the control of, or are under common control with that organization.
|
|
84
|
+
**control** means ownership of substantially all the assets of an entity, or the
|
|
85
|
+
power to direct its management and policies by vote, contract, or otherwise.
|
|
86
|
+
Control can be direct or indirect.
|
|
87
|
+
|
|
88
|
+
**your licenses** are all the licenses granted to you for the software under
|
|
89
|
+
these terms.
|
|
90
|
+
|
|
91
|
+
**use** means anything you do with the software requiring one of your licenses.
|
|
92
|
+
|
|
93
|
+
**trademark** means trademarks, service marks, and similar rights.
|
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# @vitrinka/link
|
|
2
|
+
|
|
3
|
+
The device link both vitrinka recorders authenticate with: the recorder asks
|
|
4
|
+
the server for a short code, the tester approves it in vitrinka (same device
|
|
5
|
+
via a link, or another device via the server-rendered QR), and the recorder
|
|
6
|
+
receives an ingest-only `vkr_` token. No baked secrets, no QR library.
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
import { startLink, pollLink, LinkExpired } from '@vitrinka/link';
|
|
10
|
+
|
|
11
|
+
const start = await startLink('https://app.vitrinka.ai/w/acme', { label: 'Safari on macOS · app.example.test' });
|
|
12
|
+
start.user_code; // 'ABCD-EFGH' — show it
|
|
13
|
+
start.verifyUrl; // open on the same device
|
|
14
|
+
start.qrUrl; // <img src> for the desktop→phone path (SVG from the server)
|
|
15
|
+
const linked = await pollLink(start.base, start.device_code, { interval: start.interval });
|
|
16
|
+
linked.token; // 'vkr_…' — store it, send it as the bearer
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Doors (at the base URL's origin): `POST /api/v1/cli/auth {kind:"recorder",label}` →
|
|
20
|
+
201 `{device_code, user_code, verify_path, verify_url?, qr_path, interval,
|
|
21
|
+
expires_in}`; `POST /api/v1/cli/auth/claim {device_code}` → 202 pending ·
|
|
22
|
+
200 `{token, workspace, label, expires_at}` · 404 expired (`LinkExpired`).
|
|
23
|
+
A 401 from any session door means the token is dead: forget it and link again.
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitrinka/link — the device link that mints an ingest-only recorder token.
|
|
3
|
+
*
|
|
4
|
+
* Doors live at the ORIGIN of the configured base (a base may be
|
|
5
|
+
* `https://app.vitrinka.ai/w/acme`; the link doors sit at the apex):
|
|
6
|
+
*
|
|
7
|
+
* POST {origin}/api/v1/cli/auth {kind:"recorder", label}
|
|
8
|
+
* → 201 {device_code, user_code, verify_path, verify_url?, qr_path, interval, expires_in}
|
|
9
|
+
* POST {origin}/api/v1/cli/auth/claim {device_code}
|
|
10
|
+
* → 202 pending · 200 {token, workspace, label, expires_at} · 404 expired
|
|
11
|
+
*
|
|
12
|
+
* Pure TypeScript: no DOM or React Native globals — `fetch` is taken from
|
|
13
|
+
* the options or from globalThis.
|
|
14
|
+
*/
|
|
15
|
+
export interface LinkStart {
|
|
16
|
+
/** The base the link was started for (its origin serves the doors). */
|
|
17
|
+
base: string;
|
|
18
|
+
device_code: string;
|
|
19
|
+
/** The code the tester types/sees, e.g. `ABCD-EFGH`. */
|
|
20
|
+
user_code: string;
|
|
21
|
+
verify_path: string;
|
|
22
|
+
/** Absolute approval URL — same-device path. */
|
|
23
|
+
verifyUrl: string;
|
|
24
|
+
/** Absolute URL of the server-rendered QR (SVG) — other-device path. */
|
|
25
|
+
qrUrl: string;
|
|
26
|
+
/** Poll interval in seconds (never below 2). */
|
|
27
|
+
interval: number;
|
|
28
|
+
expires_in: number;
|
|
29
|
+
}
|
|
30
|
+
export interface Linked {
|
|
31
|
+
token: string;
|
|
32
|
+
workspace: string;
|
|
33
|
+
label: string;
|
|
34
|
+
expires_at: string;
|
|
35
|
+
}
|
|
36
|
+
/** The server no longer knows the code (expired or already consumed). */
|
|
37
|
+
export declare class LinkExpired extends Error {
|
|
38
|
+
constructor(message?: string);
|
|
39
|
+
}
|
|
40
|
+
export declare class LinkError extends Error {
|
|
41
|
+
readonly status: number;
|
|
42
|
+
constructor(message: string, status: number);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Transport-status vocabulary shared by every recorder (web, Expo) — ONE copy,
|
|
46
|
+
* here in the zero-dependency package both already depend on, so a change to
|
|
47
|
+
* the retry rule cannot drift between them.
|
|
48
|
+
*/
|
|
49
|
+
export declare class VitrinkaApiError extends Error {
|
|
50
|
+
readonly status: number;
|
|
51
|
+
constructor(message: string, status: number);
|
|
52
|
+
}
|
|
53
|
+
/** A server verdict retrying can never fix (4xx minus timeout/rate-limit). */
|
|
54
|
+
export declare function permanentStatus(status: number): boolean;
|
|
55
|
+
type Fetch = typeof globalThis.fetch;
|
|
56
|
+
export interface LinkOptions {
|
|
57
|
+
fetch?: Fetch;
|
|
58
|
+
}
|
|
59
|
+
/** The control-plane origin of a base URL. */
|
|
60
|
+
export declare function linkOrigin(base: string): string;
|
|
61
|
+
/** A 401 from a session door: the stored token is dead. */
|
|
62
|
+
export declare function isUnauthorized(status: number): boolean;
|
|
63
|
+
/** Ask the server for a link code. */
|
|
64
|
+
export declare function startLink(base: string, opts: {
|
|
65
|
+
label: string;
|
|
66
|
+
} & LinkOptions): Promise<LinkStart>;
|
|
67
|
+
export interface PollOptions extends LinkOptions {
|
|
68
|
+
/** Seconds between claims (min 2). */
|
|
69
|
+
interval?: number;
|
|
70
|
+
signal?: AbortSignal;
|
|
71
|
+
/** Test seam: the sleep. */
|
|
72
|
+
sleep?: (ms: number, signal?: AbortSignal) => Promise<void>;
|
|
73
|
+
}
|
|
74
|
+
/** Claim until approved. Resolves with the token; throws LinkExpired on 404, AbortError on abort. */
|
|
75
|
+
export declare function pollLink(base: string, deviceCode: string, opts?: PollOptions): Promise<Linked>;
|
|
76
|
+
export {};
|
package/build/index.js
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitrinka/link — the device link that mints an ingest-only recorder token.
|
|
3
|
+
*
|
|
4
|
+
* Doors live at the ORIGIN of the configured base (a base may be
|
|
5
|
+
* `https://app.vitrinka.ai/w/acme`; the link doors sit at the apex):
|
|
6
|
+
*
|
|
7
|
+
* POST {origin}/api/v1/cli/auth {kind:"recorder", label}
|
|
8
|
+
* → 201 {device_code, user_code, verify_path, verify_url?, qr_path, interval, expires_in}
|
|
9
|
+
* POST {origin}/api/v1/cli/auth/claim {device_code}
|
|
10
|
+
* → 202 pending · 200 {token, workspace, label, expires_at} · 404 expired
|
|
11
|
+
*
|
|
12
|
+
* Pure TypeScript: no DOM or React Native globals — `fetch` is taken from
|
|
13
|
+
* the options or from globalThis.
|
|
14
|
+
*/
|
|
15
|
+
/** The server no longer knows the code (expired or already consumed). */
|
|
16
|
+
export class LinkExpired extends Error {
|
|
17
|
+
constructor(message = 'link code expired') {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = 'LinkExpired';
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export class LinkError extends Error {
|
|
23
|
+
constructor(message, status) {
|
|
24
|
+
super(message);
|
|
25
|
+
this.status = status;
|
|
26
|
+
this.name = 'LinkError';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Transport-status vocabulary shared by every recorder (web, Expo) — ONE copy,
|
|
31
|
+
* here in the zero-dependency package both already depend on, so a change to
|
|
32
|
+
* the retry rule cannot drift between them.
|
|
33
|
+
*/
|
|
34
|
+
export class VitrinkaApiError extends Error {
|
|
35
|
+
constructor(message, status) {
|
|
36
|
+
super(message);
|
|
37
|
+
this.status = status;
|
|
38
|
+
this.name = 'VitrinkaApiError';
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** A server verdict retrying can never fix (4xx minus timeout/rate-limit). */
|
|
42
|
+
export function permanentStatus(status) {
|
|
43
|
+
return status >= 400 && status < 500 && status !== 408 && status !== 429;
|
|
44
|
+
}
|
|
45
|
+
/** The control-plane origin of a base URL. */
|
|
46
|
+
export function linkOrigin(base) {
|
|
47
|
+
return new URL(base).origin;
|
|
48
|
+
}
|
|
49
|
+
/** A 401 from a session door: the stored token is dead. */
|
|
50
|
+
export function isUnauthorized(status) {
|
|
51
|
+
return status === 401;
|
|
52
|
+
}
|
|
53
|
+
function fetcher(opts) {
|
|
54
|
+
const f = opts.fetch ?? globalThis.fetch;
|
|
55
|
+
if (typeof f !== 'function')
|
|
56
|
+
throw new Error('vitrinka link: no fetch available');
|
|
57
|
+
return f;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Resolve a server-supplied path or URL against the link origin and accept it
|
|
61
|
+
* ONLY when it lands on that same origin over http(s). The result is rendered
|
|
62
|
+
* as a clickable href and an <img src>, so a compromised or misconfigured
|
|
63
|
+
* server must not be able to point the tester's tap or the QR anywhere else.
|
|
64
|
+
* Anything off-origin, non-http or unparsable falls back to the origin's own
|
|
65
|
+
* path (or, when even that is unusable, to the given default).
|
|
66
|
+
*/
|
|
67
|
+
function sameOriginUrl(origin, candidate, fallbackPath, defaultPath) {
|
|
68
|
+
const own = (v) => {
|
|
69
|
+
if (!v)
|
|
70
|
+
return undefined;
|
|
71
|
+
try {
|
|
72
|
+
const u = new URL(v, origin);
|
|
73
|
+
if (u.protocol !== 'http:' && u.protocol !== 'https:')
|
|
74
|
+
return undefined;
|
|
75
|
+
if (u.origin === origin)
|
|
76
|
+
return u.href;
|
|
77
|
+
// Same path, OUR origin: a server that answered with another host's URL
|
|
78
|
+
// still gets the tap and the QR pointed at the deployment we linked to.
|
|
79
|
+
return origin + u.pathname + u.search;
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
return own(candidate) ?? own(fallbackPath) ?? origin + defaultPath;
|
|
86
|
+
}
|
|
87
|
+
function requireString(w, key, status) {
|
|
88
|
+
const v = w[key];
|
|
89
|
+
if (typeof v !== 'string' || v === '')
|
|
90
|
+
throw new LinkError(`link start → malformed payload (${key})`, status);
|
|
91
|
+
return v;
|
|
92
|
+
}
|
|
93
|
+
/** Ask the server for a link code. */
|
|
94
|
+
export async function startLink(base, opts) {
|
|
95
|
+
const origin = linkOrigin(base);
|
|
96
|
+
const res = await fetcher(opts)(`${origin}/api/v1/cli/auth`, {
|
|
97
|
+
method: 'POST',
|
|
98
|
+
mode: 'cors',
|
|
99
|
+
credentials: 'omit',
|
|
100
|
+
headers: { 'content-type': 'application/json' },
|
|
101
|
+
body: JSON.stringify({ kind: 'recorder', label: opts.label }),
|
|
102
|
+
});
|
|
103
|
+
if (res.status !== 201 && res.status !== 200) {
|
|
104
|
+
throw new LinkError(`link start → ${res.status}`, res.status);
|
|
105
|
+
}
|
|
106
|
+
let raw;
|
|
107
|
+
try {
|
|
108
|
+
raw = await res.json();
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
throw new LinkError('link start → malformed payload (not JSON)', res.status);
|
|
112
|
+
}
|
|
113
|
+
if (typeof raw !== 'object' || raw === null)
|
|
114
|
+
throw new LinkError('link start → malformed payload', res.status);
|
|
115
|
+
const w = raw;
|
|
116
|
+
const deviceCode = requireString(w, 'device_code', res.status);
|
|
117
|
+
const userCode = requireString(w, 'user_code', res.status);
|
|
118
|
+
const verifyPath = requireString(w, 'verify_path', res.status);
|
|
119
|
+
const qrPath = requireString(w, 'qr_path', res.status);
|
|
120
|
+
return {
|
|
121
|
+
base,
|
|
122
|
+
device_code: deviceCode,
|
|
123
|
+
user_code: userCode,
|
|
124
|
+
verify_path: verifyPath,
|
|
125
|
+
verifyUrl: sameOriginUrl(origin, typeof w.verify_url === 'string' ? w.verify_url : undefined, verifyPath, `/cli-auth?code=${encodeURIComponent(userCode)}`),
|
|
126
|
+
qrUrl: sameOriginUrl(origin, qrPath, qrPath, `/cli-auth/qr?code=${encodeURIComponent(userCode)}`),
|
|
127
|
+
interval: Math.max(2, Number(w.interval) || 2),
|
|
128
|
+
expires_in: Number(w.expires_in) || 0,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
function defaultSleep(ms, signal) {
|
|
132
|
+
return new Promise((res, rej) => {
|
|
133
|
+
if (signal?.aborted)
|
|
134
|
+
return rej(abortError());
|
|
135
|
+
const t = setTimeout(() => {
|
|
136
|
+
signal?.removeEventListener('abort', onAbort);
|
|
137
|
+
res();
|
|
138
|
+
}, ms);
|
|
139
|
+
const onAbort = () => {
|
|
140
|
+
clearTimeout(t);
|
|
141
|
+
rej(abortError());
|
|
142
|
+
};
|
|
143
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
function abortError() {
|
|
147
|
+
const e = new Error('link polling aborted');
|
|
148
|
+
e.name = 'AbortError';
|
|
149
|
+
return e;
|
|
150
|
+
}
|
|
151
|
+
/** Claim until approved. Resolves with the token; throws LinkExpired on 404, AbortError on abort. */
|
|
152
|
+
export async function pollLink(base, deviceCode, opts = {}) {
|
|
153
|
+
const origin = linkOrigin(base);
|
|
154
|
+
const f = fetcher(opts);
|
|
155
|
+
const sleep = opts.sleep ?? defaultSleep;
|
|
156
|
+
const interval = Math.max(2, opts.interval ?? 2);
|
|
157
|
+
for (;;) {
|
|
158
|
+
if (opts.signal?.aborted)
|
|
159
|
+
throw abortError();
|
|
160
|
+
const res = await f(`${origin}/api/v1/cli/auth/claim`, {
|
|
161
|
+
method: 'POST',
|
|
162
|
+
mode: 'cors',
|
|
163
|
+
credentials: 'omit',
|
|
164
|
+
headers: { 'content-type': 'application/json' },
|
|
165
|
+
body: JSON.stringify({ device_code: deviceCode }),
|
|
166
|
+
signal: opts.signal,
|
|
167
|
+
});
|
|
168
|
+
if (res.status === 200)
|
|
169
|
+
return (await res.json());
|
|
170
|
+
if (res.status === 404 || res.status === 410)
|
|
171
|
+
throw new LinkExpired();
|
|
172
|
+
if (res.status !== 202)
|
|
173
|
+
throw new LinkError(`link claim → ${res.status}`, res.status);
|
|
174
|
+
await sleep(interval * 1000, opts.signal);
|
|
175
|
+
}
|
|
176
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vitrinka/link",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "vitrinka device link — the Netflix-style code flow that mints an ingest-only recorder token for the web and Expo recorders. Zero dependencies.",
|
|
5
|
+
"license": "Elastic-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/henderson-tech/vitrinka-kit.git",
|
|
9
|
+
"directory": "packages/link"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"source": "src/index.ts",
|
|
13
|
+
"main": "build/index.js",
|
|
14
|
+
"types": "build/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./build/index.d.ts",
|
|
18
|
+
"default": "./build/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./package.json": "./package.json"
|
|
21
|
+
},
|
|
22
|
+
"files": ["build", "README.md", "CHANGELOG.md"],
|
|
23
|
+
"sideEffects": false,
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsc -p tsconfig.build.json",
|
|
26
|
+
"typecheck": "tsc --noEmit",
|
|
27
|
+
"test": "bun test src",
|
|
28
|
+
"prepublishOnly": "tsc -p tsconfig.build.json"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/bun": "^1.2.0",
|
|
32
|
+
"@types/node": "^22.0.0",
|
|
33
|
+
"typescript": "~6.0.3"
|
|
34
|
+
},
|
|
35
|
+
"keywords": ["vitrinka", "device-link", "recorder"]
|
|
36
|
+
}
|