@extension-use/protocol 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +6 -2
- package/dist/index.d.ts +161 -0
- package/dist/index.js +113 -0
- package/dist/remote.d.ts +104 -0
- package/dist/remote.js +51 -0
- package/dist/schemas.d.ts +1338 -0
- package/dist/schemas.js +778 -0
- package/package.json +49 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ahmed Gamal and Extension-use contributors
|
|
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/README.md
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @extension-use/protocol
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Message protocol, error codes and zod schemas for every built-in action.
|
|
4
|
+
|
|
5
|
+
Part of **Extension-use**, which lets a web app drive a browser extension through a fixed, typed set of actions. See the main README and docs in the Extension-use repository.
|
|
6
|
+
|
|
7
|
+
License: MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @extension-use/protocol
|
|
3
|
+
*
|
|
4
|
+
* The wire format shared by every transport (frame, page, remote, native).
|
|
5
|
+
* This entry point has no runtime dependencies so clients stay small.
|
|
6
|
+
* Zod schemas for the built-in actions live in `@extension-use/protocol/schemas`.
|
|
7
|
+
*/
|
|
8
|
+
declare const PROTOCOL_VERSION: 1;
|
|
9
|
+
/** Marker put on every envelope so bridges can ignore unrelated postMessage traffic. */
|
|
10
|
+
declare const ENVELOPE_TAG: "extension-use";
|
|
11
|
+
declare const ERROR_CODES: readonly ["UNKNOWN_ACTION", "INVALID_INPUT", "NOT_GRANTED", "BLOCKED_BY_POLICY", "TARGET_NOT_FOUND", "ELEMENT_NOT_FOUND", "TIMEOUT", "CANCELLED", "NEEDS_USER_GESTURE", "NEEDS_PERMISSION", "TAB_CLOSED", "NOT_CONNECTED", "DEVICE_OFFLINE", "QUOTA_EXCEEDED", "PLAN_REQUIRED", "UNSUPPORTED_VERSION", "INTERNAL"];
|
|
12
|
+
type ErrorCode = (typeof ERROR_CODES)[number];
|
|
13
|
+
interface ErrorPayload {
|
|
14
|
+
code: ErrorCode;
|
|
15
|
+
message: string;
|
|
16
|
+
data?: unknown;
|
|
17
|
+
}
|
|
18
|
+
declare class ExtensionUseError extends Error {
|
|
19
|
+
readonly code: ErrorCode;
|
|
20
|
+
readonly data?: unknown;
|
|
21
|
+
constructor(code: ErrorCode, message: string, data?: unknown);
|
|
22
|
+
toPayload(): ErrorPayload;
|
|
23
|
+
static from(err: unknown): ExtensionUseError;
|
|
24
|
+
}
|
|
25
|
+
declare function isErrorPayload(x: unknown): x is ErrorPayload;
|
|
26
|
+
/** Glob where `*` matches any run of characters, e.g. `*://*.example.com/*`. */
|
|
27
|
+
type UrlPattern = string;
|
|
28
|
+
interface Target {
|
|
29
|
+
/** Default: the active tab of the controller's window (or the last focused window). */
|
|
30
|
+
tab?: "active" | number | {
|
|
31
|
+
urlMatch: UrlPattern;
|
|
32
|
+
};
|
|
33
|
+
/** Default: the top frame. A frameId (from frames.list), or the first frame whose URL matches. */
|
|
34
|
+
frame?: "top" | number | {
|
|
35
|
+
urlMatch: UrlPattern;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* How to find an element. Fields combine (all must match). From most to least stable:
|
|
40
|
+
* `ref` (from dom.snapshot / dom.query), `role` + `name`, `testId`, `text`, `css`.
|
|
41
|
+
*/
|
|
42
|
+
interface Locator {
|
|
43
|
+
ref?: string;
|
|
44
|
+
role?: string;
|
|
45
|
+
name?: string;
|
|
46
|
+
text?: string;
|
|
47
|
+
testId?: string;
|
|
48
|
+
css?: string;
|
|
49
|
+
/** Pick the nth match (0-based). Default 0. */
|
|
50
|
+
nth?: number;
|
|
51
|
+
/** Match `name` / `text` exactly instead of as a case-insensitive substring. */
|
|
52
|
+
exact?: boolean;
|
|
53
|
+
}
|
|
54
|
+
type TransportKind = "frame" | "page" | "remote" | "native" | "memory";
|
|
55
|
+
interface ClientInfo {
|
|
56
|
+
name?: string;
|
|
57
|
+
version?: string;
|
|
58
|
+
/** Protocol versions the client understands. */
|
|
59
|
+
protocols: number[];
|
|
60
|
+
}
|
|
61
|
+
interface HostInfo {
|
|
62
|
+
id: string;
|
|
63
|
+
name?: string;
|
|
64
|
+
version?: string;
|
|
65
|
+
sdkVersion: string;
|
|
66
|
+
protocol: number;
|
|
67
|
+
}
|
|
68
|
+
interface Capability {
|
|
69
|
+
action: string;
|
|
70
|
+
version: number;
|
|
71
|
+
/** Where the action runs: extension background, the page, or the frame bridge (extension page that hosts the iframe). */
|
|
72
|
+
where: "background" | "content" | "bridge";
|
|
73
|
+
}
|
|
74
|
+
interface Base {
|
|
75
|
+
/** Always ENVELOPE_TAG. */
|
|
76
|
+
eu: typeof ENVELOPE_TAG;
|
|
77
|
+
v: number;
|
|
78
|
+
}
|
|
79
|
+
interface HelloEnvelope extends Base {
|
|
80
|
+
kind: "hello";
|
|
81
|
+
client: ClientInfo;
|
|
82
|
+
}
|
|
83
|
+
interface WelcomeEnvelope extends Base {
|
|
84
|
+
kind: "welcome";
|
|
85
|
+
session: string;
|
|
86
|
+
host: HostInfo;
|
|
87
|
+
capabilities: Capability[];
|
|
88
|
+
/** Actions this session may call (after grants). Patterns like `dom.*`. */
|
|
89
|
+
granted: string[];
|
|
90
|
+
}
|
|
91
|
+
interface CallEnvelope extends Base {
|
|
92
|
+
kind: "call";
|
|
93
|
+
id: string;
|
|
94
|
+
action: string;
|
|
95
|
+
input: unknown;
|
|
96
|
+
target?: Target;
|
|
97
|
+
timeoutMs?: number;
|
|
98
|
+
}
|
|
99
|
+
interface ResultOkEnvelope extends Base {
|
|
100
|
+
kind: "result";
|
|
101
|
+
id: string;
|
|
102
|
+
ok: true;
|
|
103
|
+
output: unknown;
|
|
104
|
+
}
|
|
105
|
+
interface ResultErrEnvelope extends Base {
|
|
106
|
+
kind: "result";
|
|
107
|
+
id: string;
|
|
108
|
+
ok: false;
|
|
109
|
+
error: ErrorPayload;
|
|
110
|
+
}
|
|
111
|
+
type ResultEnvelope = ResultOkEnvelope | ResultErrEnvelope;
|
|
112
|
+
interface CancelEnvelope extends Base {
|
|
113
|
+
kind: "cancel";
|
|
114
|
+
id: string;
|
|
115
|
+
}
|
|
116
|
+
interface SubscribeEnvelope extends Base {
|
|
117
|
+
kind: "subscribe" | "unsubscribe";
|
|
118
|
+
events: string[];
|
|
119
|
+
}
|
|
120
|
+
interface EventEnvelope extends Base {
|
|
121
|
+
kind: "event";
|
|
122
|
+
name: string;
|
|
123
|
+
payload: unknown;
|
|
124
|
+
tabId?: number;
|
|
125
|
+
ts: number;
|
|
126
|
+
}
|
|
127
|
+
interface ProgressEnvelope extends Base {
|
|
128
|
+
kind: "progress";
|
|
129
|
+
id: string;
|
|
130
|
+
payload: unknown;
|
|
131
|
+
}
|
|
132
|
+
interface ByeEnvelope extends Base {
|
|
133
|
+
kind: "bye";
|
|
134
|
+
reason?: string;
|
|
135
|
+
}
|
|
136
|
+
type Envelope = HelloEnvelope | WelcomeEnvelope | CallEnvelope | ResultEnvelope | CancelEnvelope | SubscribeEnvelope | EventEnvelope | ProgressEnvelope | ByeEnvelope;
|
|
137
|
+
type EnvelopeKind = Envelope["kind"];
|
|
138
|
+
type DistributiveOmit<T, K extends keyof any> = T extends unknown ? Omit<T, K> : never;
|
|
139
|
+
type EnvelopeBody = DistributiveOmit<Envelope, "eu" | "v">;
|
|
140
|
+
/** Build an envelope with the tag and current protocol version filled in. */
|
|
141
|
+
declare function envelope<T extends EnvelopeBody>(body: T): T & Base;
|
|
142
|
+
/** Cheap structural check. Full validation of inputs happens in the host router. */
|
|
143
|
+
declare function isEnvelope(x: unknown): x is Envelope;
|
|
144
|
+
/** Glob match where `*` matches any characters. Used for URLs, origins and action grants. */
|
|
145
|
+
declare function matchPattern(pattern: string, value: string): boolean;
|
|
146
|
+
declare function matchAny(patterns: readonly string[], value: string): boolean;
|
|
147
|
+
/** Map of action name to its input and output types. */
|
|
148
|
+
type ActionTypeMap = Record<string, {
|
|
149
|
+
input: unknown;
|
|
150
|
+
output: unknown;
|
|
151
|
+
}>;
|
|
152
|
+
/** Map of event name to its payload type. */
|
|
153
|
+
type EventTypeMap = Record<string, unknown>;
|
|
154
|
+
interface CallOptions {
|
|
155
|
+
target?: Target;
|
|
156
|
+
timeoutMs?: number;
|
|
157
|
+
signal?: AbortSignal;
|
|
158
|
+
}
|
|
159
|
+
declare function randomId(): string;
|
|
160
|
+
|
|
161
|
+
export { type ActionTypeMap, type ByeEnvelope, type CallEnvelope, type CallOptions, type CancelEnvelope, type Capability, type ClientInfo, ENVELOPE_TAG, ERROR_CODES, type Envelope, type EnvelopeBody, type EnvelopeKind, type ErrorCode, type ErrorPayload, type EventEnvelope, type EventTypeMap, ExtensionUseError, type HelloEnvelope, type HostInfo, type Locator, PROTOCOL_VERSION, type ProgressEnvelope, type ResultEnvelope, type ResultErrEnvelope, type ResultOkEnvelope, type SubscribeEnvelope, type Target, type TransportKind, type UrlPattern, type WelcomeEnvelope, envelope, isEnvelope, isErrorPayload, matchAny, matchPattern, randomId };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var PROTOCOL_VERSION = 1;
|
|
3
|
+
var ENVELOPE_TAG = "extension-use";
|
|
4
|
+
var ERROR_CODES = [
|
|
5
|
+
"UNKNOWN_ACTION",
|
|
6
|
+
"INVALID_INPUT",
|
|
7
|
+
"NOT_GRANTED",
|
|
8
|
+
"BLOCKED_BY_POLICY",
|
|
9
|
+
"TARGET_NOT_FOUND",
|
|
10
|
+
"ELEMENT_NOT_FOUND",
|
|
11
|
+
"TIMEOUT",
|
|
12
|
+
"CANCELLED",
|
|
13
|
+
"NEEDS_USER_GESTURE",
|
|
14
|
+
"NEEDS_PERMISSION",
|
|
15
|
+
"TAB_CLOSED",
|
|
16
|
+
"NOT_CONNECTED",
|
|
17
|
+
"DEVICE_OFFLINE",
|
|
18
|
+
"QUOTA_EXCEEDED",
|
|
19
|
+
"PLAN_REQUIRED",
|
|
20
|
+
"UNSUPPORTED_VERSION",
|
|
21
|
+
"INTERNAL"
|
|
22
|
+
];
|
|
23
|
+
var ExtensionUseError = class _ExtensionUseError extends Error {
|
|
24
|
+
code;
|
|
25
|
+
data;
|
|
26
|
+
constructor(code, message, data) {
|
|
27
|
+
super(message);
|
|
28
|
+
this.name = "ExtensionUseError";
|
|
29
|
+
this.code = code;
|
|
30
|
+
if (data !== void 0) this.data = data;
|
|
31
|
+
}
|
|
32
|
+
toPayload() {
|
|
33
|
+
const payload = { code: this.code, message: this.message };
|
|
34
|
+
if (this.data !== void 0) payload.data = this.data;
|
|
35
|
+
return payload;
|
|
36
|
+
}
|
|
37
|
+
static from(err) {
|
|
38
|
+
if (err instanceof _ExtensionUseError) return err;
|
|
39
|
+
if (isErrorPayload(err)) return new _ExtensionUseError(err.code, err.message, err.data);
|
|
40
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
41
|
+
return new _ExtensionUseError("INTERNAL", message);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
function isErrorPayload(x) {
|
|
45
|
+
return typeof x === "object" && x !== null && typeof x.code === "string" && ERROR_CODES.includes(x.code) && typeof x.message === "string";
|
|
46
|
+
}
|
|
47
|
+
function envelope(body) {
|
|
48
|
+
return { eu: ENVELOPE_TAG, v: PROTOCOL_VERSION, ...body };
|
|
49
|
+
}
|
|
50
|
+
var KINDS = /* @__PURE__ */ new Set([
|
|
51
|
+
"hello",
|
|
52
|
+
"welcome",
|
|
53
|
+
"call",
|
|
54
|
+
"result",
|
|
55
|
+
"cancel",
|
|
56
|
+
"subscribe",
|
|
57
|
+
"unsubscribe",
|
|
58
|
+
"event",
|
|
59
|
+
"progress",
|
|
60
|
+
"bye"
|
|
61
|
+
]);
|
|
62
|
+
function isEnvelope(x) {
|
|
63
|
+
if (typeof x !== "object" || x === null) return false;
|
|
64
|
+
const e = x;
|
|
65
|
+
if (e.eu !== ENVELOPE_TAG || typeof e.v !== "number" || typeof e.kind !== "string") return false;
|
|
66
|
+
if (!KINDS.has(e.kind)) return false;
|
|
67
|
+
switch (e.kind) {
|
|
68
|
+
case "call":
|
|
69
|
+
return typeof e.id === "string" && typeof e.action === "string";
|
|
70
|
+
case "result":
|
|
71
|
+
return typeof e.id === "string" && typeof e.ok === "boolean";
|
|
72
|
+
case "cancel":
|
|
73
|
+
case "progress":
|
|
74
|
+
return typeof e.id === "string";
|
|
75
|
+
case "subscribe":
|
|
76
|
+
case "unsubscribe":
|
|
77
|
+
return Array.isArray(e.events);
|
|
78
|
+
case "event":
|
|
79
|
+
return typeof e.name === "string";
|
|
80
|
+
default:
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
var patternCache = /* @__PURE__ */ new Map();
|
|
85
|
+
function matchPattern(pattern, value) {
|
|
86
|
+
let re = patternCache.get(pattern);
|
|
87
|
+
if (!re) {
|
|
88
|
+
const escaped = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*");
|
|
89
|
+
re = new RegExp(`^${escaped}$`, "i");
|
|
90
|
+
patternCache.set(pattern, re);
|
|
91
|
+
}
|
|
92
|
+
return re.test(value);
|
|
93
|
+
}
|
|
94
|
+
function matchAny(patterns, value) {
|
|
95
|
+
return patterns.some((p) => matchPattern(p, value));
|
|
96
|
+
}
|
|
97
|
+
function randomId() {
|
|
98
|
+
const c = globalThis.crypto;
|
|
99
|
+
if (c?.randomUUID) return c.randomUUID();
|
|
100
|
+
return Math.random().toString(36).slice(2) + Date.now().toString(36);
|
|
101
|
+
}
|
|
102
|
+
export {
|
|
103
|
+
ENVELOPE_TAG,
|
|
104
|
+
ERROR_CODES,
|
|
105
|
+
ExtensionUseError,
|
|
106
|
+
PROTOCOL_VERSION,
|
|
107
|
+
envelope,
|
|
108
|
+
isEnvelope,
|
|
109
|
+
isErrorPayload,
|
|
110
|
+
matchAny,
|
|
111
|
+
matchPattern,
|
|
112
|
+
randomId
|
|
113
|
+
};
|
package/dist/remote.d.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { Envelope, ErrorCode } from './index.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Frames between an extension (the "device") and a relay, over one WebSocket
|
|
5
|
+
* that the extension opens. Several controller sessions are multiplexed on it;
|
|
6
|
+
* each carries ordinary protocol envelopes.
|
|
7
|
+
*
|
|
8
|
+
* extension → relay { t: "auth", token } first frame (device token)
|
|
9
|
+
* relay → extension { t: "ready", deviceId, name }
|
|
10
|
+
* relay → extension { t: "open", s, origin, label } a controller connected
|
|
11
|
+
* both directions { t: "msg", s, env } an envelope for session s
|
|
12
|
+
* relay → extension { t: "close", s } the controller went away
|
|
13
|
+
* extension → relay { t: "closed", s, reason? } the extension ended the session
|
|
14
|
+
* both directions { t: "ping" } / { t: "pong" } keep-alive (MV3 service workers need traffic every < 30 s)
|
|
15
|
+
* relay → extension { t: "error", code, message } auth failure etc., then the socket closes
|
|
16
|
+
*
|
|
17
|
+
* Controllers that use the relay's WebSocket API send one frame first,
|
|
18
|
+
* { t: "auth", token }, and then plain envelopes.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
type DeviceFrame = {
|
|
22
|
+
t: "auth";
|
|
23
|
+
token: string;
|
|
24
|
+
client?: {
|
|
25
|
+
name?: string;
|
|
26
|
+
sdkVersion?: string;
|
|
27
|
+
};
|
|
28
|
+
} | {
|
|
29
|
+
t: "ready";
|
|
30
|
+
deviceId: string;
|
|
31
|
+
name: string;
|
|
32
|
+
} | {
|
|
33
|
+
t: "open";
|
|
34
|
+
s: string;
|
|
35
|
+
origin: string;
|
|
36
|
+
label?: string;
|
|
37
|
+
} | {
|
|
38
|
+
t: "msg";
|
|
39
|
+
s: string;
|
|
40
|
+
env: Envelope;
|
|
41
|
+
} | {
|
|
42
|
+
t: "close";
|
|
43
|
+
s: string;
|
|
44
|
+
} | {
|
|
45
|
+
t: "closed";
|
|
46
|
+
s: string;
|
|
47
|
+
reason?: string;
|
|
48
|
+
} | {
|
|
49
|
+
t: "ping";
|
|
50
|
+
} | {
|
|
51
|
+
t: "pong";
|
|
52
|
+
} | {
|
|
53
|
+
t: "error";
|
|
54
|
+
code: ErrorCode | "UNAUTHORIZED";
|
|
55
|
+
message: string;
|
|
56
|
+
} | {
|
|
57
|
+
t: "agent-hello";
|
|
58
|
+
nonce: string;
|
|
59
|
+
} | {
|
|
60
|
+
t: "agent-challenge";
|
|
61
|
+
keyId: string;
|
|
62
|
+
nonce: string;
|
|
63
|
+
proof: string;
|
|
64
|
+
} | {
|
|
65
|
+
t: "agent-proof";
|
|
66
|
+
proof: string;
|
|
67
|
+
};
|
|
68
|
+
declare function isDeviceFrame(x: unknown): x is DeviceFrame;
|
|
69
|
+
/** Interval for keep-alive pings. MV3 service workers stay alive while WebSocket traffic flows at least every 30 s. */
|
|
70
|
+
declare const REMOTE_PING_MS = 20000;
|
|
71
|
+
/** Pairing, as seen by the extension. */
|
|
72
|
+
interface PairingStart {
|
|
73
|
+
pairingId: string;
|
|
74
|
+
/** Short code the person compares/enters on the relay side, like "482-913". */
|
|
75
|
+
code: string;
|
|
76
|
+
expiresAt: number;
|
|
77
|
+
}
|
|
78
|
+
type PairingStatus = {
|
|
79
|
+
status: "pending";
|
|
80
|
+
} | {
|
|
81
|
+
status: "approved";
|
|
82
|
+
deviceId: string;
|
|
83
|
+
deviceToken: string;
|
|
84
|
+
name: string;
|
|
85
|
+
} | {
|
|
86
|
+
status: "denied" | "expired";
|
|
87
|
+
};
|
|
88
|
+
/** Ports an agent's MCP server tries, in order; the extension dials all of them. */
|
|
89
|
+
declare const AGENT_PORTS: readonly number[];
|
|
90
|
+
/** WebSocket path of the local agent link. */
|
|
91
|
+
declare const AGENT_PATH = "/v1/agent";
|
|
92
|
+
declare const AGENT_KEY_PREFIX = "eu_agent_";
|
|
93
|
+
/** A new agent key: 24 random bytes, base64url, with the eu_agent_ prefix. */
|
|
94
|
+
declare function newAgentKey(): string;
|
|
95
|
+
declare function isAgentKey(key: string): boolean;
|
|
96
|
+
declare function agentNonce(): string;
|
|
97
|
+
/** Short public id of a key (first 12 hex digits of its SHA-256), so a side can tell which key the other means. */
|
|
98
|
+
declare function agentKeyId(key: string): Promise<string>;
|
|
99
|
+
/** HMAC-SHA256(key, `${role}|${a}|${b}`) in hex. */
|
|
100
|
+
declare function agentProof(key: string, role: "mcp" | "extension", a: string, b: string): Promise<string>;
|
|
101
|
+
/** Compare two proofs without an early exit. */
|
|
102
|
+
declare function sameProof(a: string, b: string): boolean;
|
|
103
|
+
|
|
104
|
+
export { AGENT_KEY_PREFIX, AGENT_PATH, AGENT_PORTS, type DeviceFrame, type PairingStart, type PairingStatus, REMOTE_PING_MS, agentKeyId, agentNonce, agentProof, isAgentKey, isDeviceFrame, newAgentKey, sameProof };
|
package/dist/remote.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// src/remote.ts
|
|
2
|
+
function isDeviceFrame(x) {
|
|
3
|
+
return typeof x === "object" && x !== null && typeof x.t === "string";
|
|
4
|
+
}
|
|
5
|
+
var REMOTE_PING_MS = 2e4;
|
|
6
|
+
var AGENT_PORTS = [47821, 47822, 47823, 47824, 47825];
|
|
7
|
+
var AGENT_PATH = "/v1/agent";
|
|
8
|
+
var AGENT_KEY_PREFIX = "eu_agent_";
|
|
9
|
+
var hex = (buf) => [...new Uint8Array(buf)].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
10
|
+
var subtle = () => {
|
|
11
|
+
const s = globalThis.crypto?.subtle;
|
|
12
|
+
if (!s) throw new Error("Web Crypto is not available");
|
|
13
|
+
return s;
|
|
14
|
+
};
|
|
15
|
+
function newAgentKey() {
|
|
16
|
+
const bytes = crypto.getRandomValues(new Uint8Array(24));
|
|
17
|
+
const b64 = btoa(String.fromCharCode(...bytes)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
18
|
+
return `${AGENT_KEY_PREFIX}${b64}`;
|
|
19
|
+
}
|
|
20
|
+
function isAgentKey(key) {
|
|
21
|
+
return new RegExp(`^${AGENT_KEY_PREFIX}[A-Za-z0-9_-]{20,}$`).test(key);
|
|
22
|
+
}
|
|
23
|
+
function agentNonce() {
|
|
24
|
+
return hex(crypto.getRandomValues(new Uint8Array(16)).buffer);
|
|
25
|
+
}
|
|
26
|
+
async function agentKeyId(key) {
|
|
27
|
+
return hex(await subtle().digest("SHA-256", new TextEncoder().encode(key))).slice(0, 12);
|
|
28
|
+
}
|
|
29
|
+
async function agentProof(key, role, a, b) {
|
|
30
|
+
const k = await subtle().importKey("raw", new TextEncoder().encode(key), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
31
|
+
return hex(await subtle().sign("HMAC", k, new TextEncoder().encode(`${role}|${a}|${b}`)));
|
|
32
|
+
}
|
|
33
|
+
function sameProof(a, b) {
|
|
34
|
+
if (typeof a !== "string" || typeof b !== "string" || a.length !== b.length) return false;
|
|
35
|
+
let diff = 0;
|
|
36
|
+
for (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
37
|
+
return diff === 0;
|
|
38
|
+
}
|
|
39
|
+
export {
|
|
40
|
+
AGENT_KEY_PREFIX,
|
|
41
|
+
AGENT_PATH,
|
|
42
|
+
AGENT_PORTS,
|
|
43
|
+
REMOTE_PING_MS,
|
|
44
|
+
agentKeyId,
|
|
45
|
+
agentNonce,
|
|
46
|
+
agentProof,
|
|
47
|
+
isAgentKey,
|
|
48
|
+
isDeviceFrame,
|
|
49
|
+
newAgentKey,
|
|
50
|
+
sameProof
|
|
51
|
+
};
|