@chatpanel/bridge 0.11.24 → 0.11.25
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/README.md +7 -7
- package/package.json +1 -13
- package/scripts/sync-channels.mjs +8 -90
- package/scripts/sync-events.mjs +7 -78
- package/scripts/sync-pii.mjs +7 -68
- package/src/api-compat.js +5 -294
- package/src/channels/adapters/telegram.js +3 -298
- package/src/channels/bridge.js +1 -59
- package/src/channels/eventlog.js +3 -57
- package/src/channels/gateway.js +1 -114
- package/src/channels/invoke.js +1 -172
- package/src/channels/normalize.js +1 -58
- package/src/channels/pairing.js +1 -95
- package/src/channels/service.js +1 -329
- package/src/channels/stream.js +5 -104
- package/src/cli-errors.js +3 -136
- package/src/connections.js +1 -160
- package/src/connectors.js +1 -100
- package/src/engines/antigravity.js +5 -235
- package/src/engines/args.js +1 -47
- package/src/engines/claude.js +6 -586
- package/src/engines/cli-agents.js +7 -312
- package/src/engines/codex.js +6 -481
- package/src/engines/custom.js +17 -577
- package/src/engines/prompt.js +6 -32
- package/src/engines/stream-formats.js +2 -222
- package/src/entitlement.js +1 -77
- package/src/env.js +3 -628
- package/src/events/capability.js +1 -134
- package/src/events/event.js +1 -183
- package/src/events/reach.js +1 -31
- package/src/events/ref.js +1 -63
- package/src/events/scm-connection.js +1 -188
- package/src/events/scopes.js +1 -20
- package/src/events/skill-manifest.js +1 -338
- package/src/events/skill-scan.js +3 -217
- package/src/events/view.js +1 -96
- package/src/mcp-capabilities.js +2 -139
- package/src/mcp-local.js +0 -0
- package/src/mcp-quarantine.js +1 -110
- package/src/net.js +1 -107
- package/src/pii/index.js +1 -29
- package/src/pii/net.js +1 -111
- package/src/pii/pii-detect.js +2 -296
- package/src/pii/pii-redact.js +1 -470
- package/src/pii/pipeline.js +1 -137
- package/src/pii/sanitize.js +2 -179
- package/src/pii/tool-harness.js +1 -184
- package/src/pii/tool-rank.js +1 -110
- package/src/proc.js +2 -91
- package/src/relay-timeout.js +1 -21
- package/src/runs.js +1 -49
- package/src/sanitize.js +1 -7
- package/src/scm.js +1 -76
- package/src/server.js +10 -1492
- package/src/service.js +11 -176
- package/src/sign-in.js +1 -91
- package/src/skills.js +1 -399
- package/src/ssrf.js +1 -70
- package/src/update.js +1 -216
- package/src/workdir.js +1 -108
- package/src/worktree.js +3 -125
package/src/engines/prompt.js
CHANGED
|
@@ -1,35 +1,9 @@
|
|
|
1
|
-
import
|
|
1
|
+
import{stripHidden as u}from"../sanitize.js";function a(t){return t==="assistant"?"Assistant":"User"}function p(t=[],c=""){const o=String(c||"").trim(),r=Array.isArray(t)?t:[],e=r.slice(0,-1),s=r[r.length-1],n=[];if(o&&n.push(o),e.length){const l=e.map(i=>`${a(i.role)}: ${String(i.content||"").trim()}`).join(`
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
3
|
+
`);n.push(`Conversation history (context only; do not answer these prior turns):
|
|
4
|
+
${l}`)}return s&&n.push(`Current ${a(s.role).toLowerCase()} message (answer this message now; use the history above only as context):
|
|
5
|
+
${String(s.content||"").trim()}`),u(n.join(`
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
const cleanSystem = String(system || '').trim();
|
|
9
|
-
const list = Array.isArray(messages) ? messages : [];
|
|
10
|
-
const history = list.slice(0, -1);
|
|
11
|
-
const current = list[list.length - 1];
|
|
12
|
-
const parts = [];
|
|
7
|
+
---
|
|
13
8
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
if (history.length) {
|
|
17
|
-
const transcript = history
|
|
18
|
-
.map((m) => `${roleLabel(m.role)}: ${String(m.content || '').trim()}`)
|
|
19
|
-
.join('\n\n');
|
|
20
|
-
parts.push(`Conversation history (context only; do not answer these prior turns):\n${transcript}`);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
if (current) {
|
|
24
|
-
parts.push(
|
|
25
|
-
`Current ${roleLabel(current.role).toLowerCase()} message (answer this message now; use the history above only as context):\n${String(current.content || '').trim()}`,
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// De-steganography on the final prompt before it reaches the local agent. The
|
|
30
|
-
// extension already scrubs when its redaction is on, but the bridge is also a
|
|
31
|
-
// public localhost endpoint other clients can call — so strip invisible/format
|
|
32
|
-
// Unicode here too (hidden instructions via Tag chars, zero-width-split values,
|
|
33
|
-
// injected fingerprint markers). See src/sanitize.js.
|
|
34
|
-
return stripHidden(parts.join('\n\n---\n\n'));
|
|
35
|
-
}
|
|
9
|
+
`))}export{p as buildCliPrompt};
|
|
@@ -1,222 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
// Every headless CLI prints its turn in some shape: plain text, or one of a
|
|
4
|
-
// handful of NDJSON dialects. Previously each dialect was an inline `else if`
|
|
5
|
-
// branch in runSpec(), so a new agent with a new dialect meant editing the
|
|
6
|
-
// runner. That's the reinvention this registry removes: a format is a named
|
|
7
|
-
// plugin here, and an agent spec just names it (`format: 'copilot-json'`).
|
|
8
|
-
//
|
|
9
|
-
// Contract — a format is a factory `(emit) => parser` where parser has:
|
|
10
|
-
// push(chunk: string) feed raw stdout; emit deltas/tools/status as they parse
|
|
11
|
-
// finish(): string the final answer when nothing was streamed (fallback)
|
|
12
|
-
// get streamed(): bool true once any delta was emitted
|
|
13
|
-
//
|
|
14
|
-
// Emitted event types match the bridge's SSE vocabulary: delta | reasoning |
|
|
15
|
-
// tool | status. (`done`/`usage`/`error` stay the runner's job.)
|
|
16
|
-
|
|
17
|
-
import { handleMessage } from './claude.js';
|
|
18
|
-
|
|
19
|
-
// Many CLIs colourize even when piped; strip ANSI from anything we treat as text.
|
|
20
|
-
const ANSI_RE = /\u001b\[[0-9;?]*[ -/]*[@-~]/g;
|
|
21
|
-
export const stripAnsi = (s) => String(s).replace(ANSI_RE, '');
|
|
22
|
-
|
|
23
|
-
// Shared NDJSON pump: buffers partial lines and hands complete JSON objects to
|
|
24
|
-
// `onEvent`. Every JSON dialect below is line-delimited, so they all reuse this.
|
|
25
|
-
function ndjson(onEvent) {
|
|
26
|
-
let buf = '';
|
|
27
|
-
return (chunk) => {
|
|
28
|
-
buf += chunk;
|
|
29
|
-
let nl;
|
|
30
|
-
while ((nl = buf.indexOf('\n')) >= 0) {
|
|
31
|
-
const line = buf.slice(0, nl).trim();
|
|
32
|
-
buf = buf.slice(nl + 1);
|
|
33
|
-
if (!line.startsWith('{')) continue;
|
|
34
|
-
let ev;
|
|
35
|
-
try {
|
|
36
|
-
ev = JSON.parse(line);
|
|
37
|
-
} catch {
|
|
38
|
-
continue; // a partial/garbage line is not fatal — keep streaming
|
|
39
|
-
}
|
|
40
|
-
onEvent(ev);
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// --- text ------------------------------------------------------------------
|
|
46
|
-
// Anything that just prints a reply.
|
|
47
|
-
function textFormat(emit) {
|
|
48
|
-
let streamed = false;
|
|
49
|
-
return {
|
|
50
|
-
push(chunk) {
|
|
51
|
-
streamed = true;
|
|
52
|
-
emit({ type: 'delta', text: stripAnsi(chunk) });
|
|
53
|
-
},
|
|
54
|
-
finish: () => '',
|
|
55
|
-
get streamed() {
|
|
56
|
-
return streamed;
|
|
57
|
-
},
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// --- claude-stream-json ----------------------------------------------------
|
|
62
|
-
// Claude Code's `--output-format stream-json`, parsed by the Claude engine.
|
|
63
|
-
function claudeStreamJson(emit) {
|
|
64
|
-
let streamed = false;
|
|
65
|
-
let result = '';
|
|
66
|
-
const pump = ndjson((msg) => {
|
|
67
|
-
const r = handleMessage(msg, emit, streamed);
|
|
68
|
-
if (r.streamed) streamed = true;
|
|
69
|
-
if (r.result != null) result = r.result;
|
|
70
|
-
});
|
|
71
|
-
return {
|
|
72
|
-
push: pump,
|
|
73
|
-
finish: () => result,
|
|
74
|
-
get streamed() {
|
|
75
|
-
return streamed;
|
|
76
|
-
},
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// --- opencode-json ---------------------------------------------------------
|
|
81
|
-
// opencode `run --format json`: text parts, tool events, errors.
|
|
82
|
-
function opencodeJson(emit) {
|
|
83
|
-
let streamed = false;
|
|
84
|
-
const pump = ndjson((ev) => {
|
|
85
|
-
if (ev.type === 'text' && ev.part?.text) {
|
|
86
|
-
streamed = true;
|
|
87
|
-
emit({ type: 'delta', text: ev.part.text });
|
|
88
|
-
} else if (ev.type === 'tool' || ev.type === 'tool_use') {
|
|
89
|
-
// Phase-based where opencode gives us enough to pair a call with its outcome: its tool
|
|
90
|
-
// parts carry a callID and a state that moves running → completed/error. Without that we
|
|
91
|
-
// still emit a start, so the step at least appears (the old behaviour).
|
|
92
|
-
const p = ev.part || {};
|
|
93
|
-
const name = p.tool || p.name || p.type || 'tool';
|
|
94
|
-
const callId = p.callID || p.callId || p.id || undefined;
|
|
95
|
-
const state = p.state || {};
|
|
96
|
-
const status = String(state.status || '').toLowerCase();
|
|
97
|
-
if (status === 'completed' || status === 'error') {
|
|
98
|
-
emit({
|
|
99
|
-
type: 'tool', name, phase: 'done', callId,
|
|
100
|
-
status: status === 'error' ? 'error' : 'ok',
|
|
101
|
-
result: String(state.output || state.error || '').slice(0, 4000),
|
|
102
|
-
});
|
|
103
|
-
} else {
|
|
104
|
-
emit({ type: 'tool', name, phase: 'start', callId, input: state.input || p.input, summary: '' });
|
|
105
|
-
}
|
|
106
|
-
} else if (ev.type === 'error') {
|
|
107
|
-
const msg = ev.error?.data?.message || ev.error?.message || ev.error?.name || 'error';
|
|
108
|
-
emit({ type: 'status', text: String(msg).slice(0, 300) });
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
return {
|
|
112
|
-
push: pump,
|
|
113
|
-
finish: () => '',
|
|
114
|
-
get streamed() {
|
|
115
|
-
return streamed;
|
|
116
|
-
},
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// --- copilot-json ----------------------------------------------------------
|
|
121
|
-
// GitHub Copilot CLI `--output-format json` (verified against 1.0.80).
|
|
122
|
-
//
|
|
123
|
-
// Event vocabulary (only the ones we surface):
|
|
124
|
-
// assistant.message_delta data.deltaContent -> streamed answer text
|
|
125
|
-
// assistant.message data.content -> whole answer (fallback)
|
|
126
|
-
// assistant.reasoning data.content -> thinking (usually opaque/empty)
|
|
127
|
-
// tool.execution_start data.toolName -> activity strip
|
|
128
|
-
// tool.execution_complete data.success/result -> surface failures
|
|
129
|
-
// session.auto_mode_resolved data.chosenModel -> which model `auto` picked
|
|
130
|
-
// result exitCode/usage -> terminal event
|
|
131
|
-
//
|
|
132
|
-
// Copilot reports a denied tool as a COMPLETED call whose result explains the
|
|
133
|
-
// permission gap, so a user on a low permission mode otherwise sees a confident
|
|
134
|
-
// "I can't do that" with no hint that ChatPanel gated it. Detect that shape and
|
|
135
|
-
// say which setting to raise.
|
|
136
|
-
const PERMISSION_RE = /permission denied|could not request permission|not allowed|denied by|requires approval/i;
|
|
137
|
-
|
|
138
|
-
function copilotJson(emit) {
|
|
139
|
-
let streamed = false;
|
|
140
|
-
let result = '';
|
|
141
|
-
let permissionHinted = false;
|
|
142
|
-
|
|
143
|
-
const hintPermissions = (text) => {
|
|
144
|
-
if (permissionHinted || !PERMISSION_RE.test(String(text || ''))) return;
|
|
145
|
-
permissionHinted = true;
|
|
146
|
-
emit({
|
|
147
|
-
type: 'status',
|
|
148
|
-
text: 'Copilot was denied a tool/URL — raise this agent’s Permission mode in Settings.',
|
|
149
|
-
});
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
const pump = ndjson((ev) => {
|
|
153
|
-
const type = ev?.type || '';
|
|
154
|
-
const d = ev?.data || {};
|
|
155
|
-
switch (type) {
|
|
156
|
-
case 'assistant.message_delta':
|
|
157
|
-
if (d.deltaContent) {
|
|
158
|
-
streamed = true;
|
|
159
|
-
emit({ type: 'delta', text: d.deltaContent });
|
|
160
|
-
}
|
|
161
|
-
break;
|
|
162
|
-
case 'assistant.message':
|
|
163
|
-
// Full turn text. Keep as the fallback answer for the non-streaming case
|
|
164
|
-
// (`--stream off`, or a turn that only produced a final message).
|
|
165
|
-
if (typeof d.content === 'string' && d.content) result = d.content;
|
|
166
|
-
break;
|
|
167
|
-
case 'assistant.reasoning':
|
|
168
|
-
if (d.content) emit({ type: 'reasoning', text: String(d.content) });
|
|
169
|
-
break;
|
|
170
|
-
case 'tool.execution_start':
|
|
171
|
-
emit({ type: 'tool', name: d.toolName || 'tool', summary: '' });
|
|
172
|
-
break;
|
|
173
|
-
case 'tool.execution_complete':
|
|
174
|
-
if (d.success === false) {
|
|
175
|
-
const msg = typeof d.result === 'string' ? d.result : d.result?.error || d.result?.message || '';
|
|
176
|
-
if (msg) emit({ type: 'status', text: String(msg).slice(0, 300) });
|
|
177
|
-
hintPermissions(msg);
|
|
178
|
-
} else {
|
|
179
|
-
hintPermissions(typeof d.result === 'string' ? d.result : '');
|
|
180
|
-
}
|
|
181
|
-
break;
|
|
182
|
-
case 'session.auto_mode_resolved':
|
|
183
|
-
if (d.chosenModel) emit({ type: 'status', text: `model: ${d.chosenModel}` });
|
|
184
|
-
break;
|
|
185
|
-
case 'session.mcp_server_status_changed':
|
|
186
|
-
// Only worth surfacing when our own browser-tool server fails to attach.
|
|
187
|
-
if (d.status === 'failed' && d.error) {
|
|
188
|
-
emit({ type: 'status', text: `MCP ${d.serverName}: ${String(d.error).slice(0, 160)}` });
|
|
189
|
-
}
|
|
190
|
-
break;
|
|
191
|
-
case 'result':
|
|
192
|
-
// Terminal event; `exitCode` is authoritative for failure (the process
|
|
193
|
-
// can still exit 0 while a tool failed). Surface a non-zero code.
|
|
194
|
-
if (ev.exitCode) emit({ type: 'status', text: `copilot exited ${ev.exitCode}` });
|
|
195
|
-
break;
|
|
196
|
-
default:
|
|
197
|
-
break;
|
|
198
|
-
}
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
return {
|
|
202
|
-
push: pump,
|
|
203
|
-
finish: () => result,
|
|
204
|
-
get streamed() {
|
|
205
|
-
return streamed;
|
|
206
|
-
},
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// The registry. Adding a dialect = adding one entry here; agent specs reference
|
|
211
|
-
// it by name, so no runner change is needed.
|
|
212
|
-
export const STREAM_FORMATS = {
|
|
213
|
-
text: textFormat,
|
|
214
|
-
'claude-stream-json': claudeStreamJson,
|
|
215
|
-
'opencode-json': opencodeJson,
|
|
216
|
-
'copilot-json': copilotJson,
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
export function createStreamParser(format, emit) {
|
|
220
|
-
const make = STREAM_FORMATS[format] || STREAM_FORMATS.text;
|
|
221
|
-
return make(emit);
|
|
222
|
-
}
|
|
1
|
+
import{handleMessage as d}from"./claude.js";const m=/\u001b\[[0-9;?]*[ -/]*[@-~]/g,f=e=>String(e).replace(m,"");function c(e){let s="";return n=>{s+=n;let r;for(;(r=s.indexOf(`
|
|
2
|
+
`))>=0;){const o=s.slice(0,r).trim();if(s=s.slice(r+1),!o.startsWith("{"))continue;let a;try{a=JSON.parse(o)}catch{continue}e(a)}}}function g(e){let s=!1;return{push(n){s=!0,e({type:"delta",text:f(n)})},finish:()=>"",get streamed(){return s}}}function y(e){let s=!1,n="";return{push:c(o=>{const a=d(o,e,s);a.streamed&&(s=!0),a.result!=null&&(n=a.result)}),finish:()=>n,get streamed(){return s}}}function x(e){let s=!1;return{push:c(r=>{if(r.type==="text"&&r.part?.text)s=!0,e({type:"delta",text:r.part.text});else if(r.type==="tool"||r.type==="tool_use"){const o=r.part||{},a=o.tool||o.name||o.type||"tool",i=o.callID||o.callId||o.id||void 0,l=o.state||{},t=String(l.status||"").toLowerCase();e(t==="completed"||t==="error"?{type:"tool",name:a,phase:"done",callId:i,status:t==="error"?"error":"ok",result:String(l.output||l.error||"").slice(0,4e3)}:{type:"tool",name:a,phase:"start",callId:i,input:l.input||o.input,summary:""})}else if(r.type==="error"){const o=r.error?.data?.message||r.error?.message||r.error?.name||"error";e({type:"status",text:String(o).slice(0,300)})}}),finish:()=>"",get streamed(){return s}}}const h=/permission denied|could not request permission|not allowed|denied by|requires approval/i;function S(e){let s=!1,n="",r=!1;const o=i=>{r||!h.test(String(i||""))||(r=!0,e({type:"status",text:"Copilot was denied a tool/URL — raise this agent’s Permission mode in Settings."}))};return{push:c(i=>{const l=i?.type||"",t=i?.data||{};switch(l){case"assistant.message_delta":t.deltaContent&&(s=!0,e({type:"delta",text:t.deltaContent}));break;case"assistant.message":typeof t.content=="string"&&t.content&&(n=t.content);break;case"assistant.reasoning":t.content&&e({type:"reasoning",text:String(t.content)});break;case"tool.execution_start":e({type:"tool",name:t.toolName||"tool",summary:""});break;case"tool.execution_complete":if(t.success===!1){const u=typeof t.result=="string"?t.result:t.result?.error||t.result?.message||"";u&&e({type:"status",text:String(u).slice(0,300)}),o(u)}else o(typeof t.result=="string"?t.result:"");break;case"session.auto_mode_resolved":t.chosenModel&&e({type:"status",text:`model: ${t.chosenModel}`});break;case"session.mcp_server_status_changed":t.status==="failed"&&t.error&&e({type:"status",text:`MCP ${t.serverName}: ${String(t.error).slice(0,160)}`});break;case"result":i.exitCode&&e({type:"status",text:`copilot exited ${i.exitCode}`});break;default:break}}),finish:()=>n,get streamed(){return s}}}const p={text:g,"claude-stream-json":y,"opencode-json":x,"copilot-json":S};function k(e,s){return(p[e]||p.text)(s)}export{p as STREAM_FORMATS,k as createStreamParser,f as stripAnsi};
|
package/src/entitlement.js
CHANGED
|
@@ -1,77 +1 @@
|
|
|
1
|
-
|
|
2
|
-
// token with an ECDSA P-256 private key; the bridge ships the matching public key
|
|
3
|
-
// and verifies the token's signature locally (no network). Gates Pro features such
|
|
4
|
-
// as "bring your own" custom CLI agents.
|
|
5
|
-
//
|
|
6
|
-
// Token format (identical to the extension's, extension/js/license.js):
|
|
7
|
-
// token = base64url(JSON payload) + "." + base64url(raw ECDSA signature)
|
|
8
|
-
// signed over UTF-8(head); payload = { typ:'ent', plan, install_id, sub, exp }
|
|
9
|
-
//
|
|
10
|
-
// Keep ENTITLEMENT_PUBLIC_JWK in sync with the extension's copy.
|
|
11
|
-
|
|
12
|
-
import { webcrypto } from 'node:crypto';
|
|
13
|
-
|
|
14
|
-
const ENTITLEMENT_PUBLIC_JWK = {
|
|
15
|
-
kty: 'EC',
|
|
16
|
-
crv: 'P-256',
|
|
17
|
-
x: 'CmgKLC4e3xDMvwhbjVqF7jbDe1JhC1KKQi8JN3qVX_4',
|
|
18
|
-
y: 'r40l6fQiyCcJYqW-SvB4VoSyn4F36yhSt82ZAOSo78E',
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
const PRO_PLANS = new Set(['pro', 'team']);
|
|
22
|
-
|
|
23
|
-
const b64urlToBytes = (s) => {
|
|
24
|
-
const norm = s.replace(/-/g, '+').replace(/_/g, '/');
|
|
25
|
-
return new Uint8Array(Buffer.from(norm, 'base64'));
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
let keyPromise = null;
|
|
29
|
-
function publicKey() {
|
|
30
|
-
if (!keyPromise) {
|
|
31
|
-
keyPromise = webcrypto.subtle.importKey(
|
|
32
|
-
'jwk',
|
|
33
|
-
ENTITLEMENT_PUBLIC_JWK,
|
|
34
|
-
{ name: 'ECDSA', namedCurve: 'P-256' },
|
|
35
|
-
false,
|
|
36
|
-
['verify'],
|
|
37
|
-
);
|
|
38
|
-
}
|
|
39
|
-
return keyPromise;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// Verify a server entitlement token. Returns its payload, or null. Checks the
|
|
43
|
-
// ECDSA signature, the token type, and expiry. install_id binding is handled by
|
|
44
|
-
// the extension; the bridge checks the signature.
|
|
45
|
-
export async function verifyEntitlement(token) {
|
|
46
|
-
if (!token || typeof token !== 'string' || token.indexOf('.') < 0) return null;
|
|
47
|
-
const [head, sig] = token.split('.');
|
|
48
|
-
const enc = new TextEncoder();
|
|
49
|
-
let ok = false;
|
|
50
|
-
try {
|
|
51
|
-
ok = await webcrypto.subtle.verify(
|
|
52
|
-
{ name: 'ECDSA', hash: 'SHA-256' },
|
|
53
|
-
await publicKey(),
|
|
54
|
-
b64urlToBytes(sig),
|
|
55
|
-
enc.encode(head),
|
|
56
|
-
);
|
|
57
|
-
} catch {
|
|
58
|
-
return null;
|
|
59
|
-
}
|
|
60
|
-
if (!ok) return null;
|
|
61
|
-
let payload;
|
|
62
|
-
try {
|
|
63
|
-
payload = JSON.parse(new TextDecoder().decode(b64urlToBytes(head)));
|
|
64
|
-
} catch {
|
|
65
|
-
return null;
|
|
66
|
-
}
|
|
67
|
-
if (payload.typ !== 'ent') return null;
|
|
68
|
-
// exp is required; the worker always mints a finite exp, so requiring one is non-breaking.
|
|
69
|
-
if (typeof payload.exp !== 'number' || !Number.isFinite(payload.exp) || Date.now() > payload.exp) return null;
|
|
70
|
-
return payload;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// True when `token` is a valid, unexpired Pro (or Team) entitlement.
|
|
74
|
-
export async function isProEntitled(token) {
|
|
75
|
-
const p = await verifyEntitlement(token);
|
|
76
|
-
return !!(p && PRO_PLANS.has(p.plan));
|
|
77
|
-
}
|
|
1
|
+
import{webcrypto as l}from"node:crypto";const c={kty:"EC",crv:"P-256",x:"CmgKLC4e3xDMvwhbjVqF7jbDe1JhC1KKQi8JN3qVX_4",y:"r40l6fQiyCcJYqW-SvB4VoSyn4F36yhSt82ZAOSo78E"},s=new Set(["pro","team"]),o=e=>{const t=e.replace(/-/g,"+").replace(/_/g,"/");return new Uint8Array(Buffer.from(t,"base64"))};let r=null;function y(){return r||(r=l.subtle.importKey("jwk",c,{name:"ECDSA",namedCurve:"P-256"},!1,["verify"])),r}async function f(e){if(!e||typeof e!="string"||e.indexOf(".")<0)return null;const[t,a]=e.split("."),u=new TextEncoder;let i=!1;try{i=await l.subtle.verify({name:"ECDSA",hash:"SHA-256"},await y(),o(a),u.encode(t))}catch{return null}if(!i)return null;let n;try{n=JSON.parse(new TextDecoder().decode(o(t)))}catch{return null}return n.typ!=="ent"||typeof n.exp!="number"||!Number.isFinite(n.exp)||Date.now()>n.exp?null:n}async function m(e){const t=await f(e);return!!(t&&s.has(t.plan))}export{m as isProEntitled,f as verifyEntitlement};
|