copilotz 1.0.0-rc.21 → 1.0.0-rc.23
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 +320 -1
- package/README.md +217 -87
- package/hosts/orgs/mod.d.ts +33 -13
- package/hosts/orgs/mod.js +183 -84
- package/hosts/web/address.d.ts +11 -0
- package/hosts/web/address.js +46 -0
- package/hosts/web/assets.js +11 -5
- package/hosts/web/entrances.d.ts +250 -0
- package/hosts/web/entrances.js +362 -0
- package/hosts/web/host.d.ts +15 -3
- package/hosts/web/host.js +31 -18
- package/hosts/web/mod.d.ts +4 -2
- package/hosts/web/mod.js +6 -0
- package/hosts/web/node/bundle.d.ts +5 -0
- package/hosts/web/node/bundle.js +1 -1
- package/hosts/web/node/mod.d.ts +6 -4
- package/hosts/web/node/mod.js +6 -4
- package/hosts/web/node/serve.d.ts +20 -23
- package/hosts/web/node/serve.js +10 -21
- package/hosts/web/routes.d.ts +23 -17
- package/hosts/web/routes.js +56 -32
- package/hosts/web/server.d.ts +89 -44
- package/hosts/web/server.js +208 -108
- package/hosts/web/signin.d.ts +7 -10
- package/hosts/web/signin.js +6 -3
- package/hosts/web/surface.d.ts +20 -0
- package/hosts/web/surface.js +42 -0
- package/hosts/web/theme/css.d.ts +2 -1
- package/hosts/web/theme/css.js +9 -2
- package/hosts/web/tickets.d.ts +14 -1
- package/hosts/web/tickets.js +30 -7
- package/hosts/web/widget.d.ts +31 -0
- package/hosts/web/widget.js +429 -0
- package/hosts/web/widgets.d.ts +43 -0
- package/hosts/web/widgets.js +117 -0
- package/package.json +5 -1
- package/plugins/access/contacts/actions/forget.js +3 -1
- package/plugins/access/contacts/actions/start.js +18 -8
- package/plugins/access/contacts/collections/contact.d.ts +2 -2
- package/plugins/access/contacts/collections/contact.js +1 -1
- package/plugins/access/contacts/contract.d.ts +5 -5
- package/plugins/access/contacts/contract.js +5 -5
- package/plugins/access/contacts/mod.d.ts +4 -0
- package/plugins/access/contacts/mod.js +4 -0
- package/plugins/access/contacts/views/contact.js +1 -0
- package/plugins/access/dev/actions/sign-in.d.ts +1 -0
- package/plugins/access/dev/actions/sign-in.js +38 -0
- package/plugins/access/dev/mod.d.ts +17 -0
- package/plugins/access/dev/mod.js +31 -0
- package/plugins/access/dev/shared/nearby.d.ts +2 -0
- package/plugins/access/dev/shared/nearby.js +27 -0
- package/plugins/access/directory/actions/invite.js +2 -2
- package/plugins/access/directory/actions/org.js +11 -8
- package/plugins/access/directory/collections/credit-code.d.ts +1 -1
- package/plugins/access/google/actions/sign-in.js +2 -0
- package/plugins/access/google/mod.d.ts +4 -2
- package/plugins/access/google/mod.js +15 -2
- package/plugins/access/identity/actions/session.js +5 -3
- package/plugins/access/identity/mod.d.ts +4 -2
- package/plugins/access/identity/mod.js +17 -2
- package/plugins/access/mod.d.ts +7 -0
- package/plugins/access/mod.js +1 -0
- package/plugins/access/spaces/shared/declared.d.ts +1 -1
- package/plugins/access/spaces/shared/rules.d.ts +16 -5
- package/plugins/access/spaces/shared/rules.js +74 -6
- package/plugins/agents/chat/actions/send.d.ts +21 -0
- package/plugins/agents/chat/actions/send.js +72 -13
- package/plugins/agents/chat/collections/file.d.ts +15 -0
- package/plugins/agents/chat/collections/file.js +12 -3
- package/plugins/agents/chat/collections/live.d.ts +11 -0
- package/plugins/agents/chat/collections/live.js +11 -1
- package/plugins/agents/chat/collections/message.d.ts +1 -1
- package/plugins/agents/chat/collections/wait.d.ts +1 -1
- package/plugins/agents/chat/contract.d.ts +3 -2
- package/plugins/agents/chat/contract.js +1 -0
- package/plugins/agents/chat/mod.d.ts +9 -3
- package/plugins/agents/chat/mod.js +28 -7
- package/plugins/agents/chat/processors/failed.js +8 -3
- package/plugins/agents/chat/processors/turn.js +6 -3
- package/plugins/agents/chat/shared/agents.js +11 -2
- package/plugins/agents/chat/shared/live.d.ts +4 -2
- package/plugins/agents/chat/shared/live.js +2 -1
- package/plugins/agents/chat/shared/types.d.ts +8 -0
- package/plugins/agents/history/collections/outline.d.ts +2 -2
- package/plugins/agents/openapi/shared/files.js +2 -0
- package/plugins/agents/routines/collections/routine.d.ts +1 -1
- package/plugins/billing/credits/collections/plan.d.ts +2 -2
- package/plugins/billing/credits/collections/room.d.ts +1 -1
- package/plugins/billing/usage/collections/rollup.d.ts +2 -2
- package/plugins/billing/usage/collections/usage.d.ts +2 -2
- package/plugins/compute/cloud/actions/pool.js +2 -0
- package/plugins/compute/cloud/actions/remote.js +4 -2
- package/plugins/compute/cloud/actions/vms.js +2 -0
- package/plugins/compute/cloud/collections/pool-vm.d.ts +1 -1
- package/plugins/compute/computers/actions/admit.js +2 -0
- package/plugins/compute/computers/actions/code.js +2 -0
- package/plugins/compute/computers/actions/enrollment.js +2 -0
- package/plugins/compute/computers/actions/work.js +3 -0
- package/plugins/compute/computers/program/version.d.ts +1 -1
- package/plugins/compute/computers/program/version.js +1 -1
- package/plugins/compute/computers/resources/downloads.js +1 -1
- package/plugins/compute/computers/resources/previews.js +5 -2
- package/plugins/interfaces/maintenance/mod.d.ts +3 -1
- package/plugins/interfaces/maintenance/mod.js +3 -1
- package/plugins/interfaces/mcp/actions/call.d.ts +4 -0
- package/plugins/interfaces/mcp/actions/call.js +120 -18
- package/plugins/interfaces/mcp/actions/conversation.d.ts +1 -0
- package/plugins/interfaces/mcp/actions/conversation.js +49 -0
- package/plugins/interfaces/mcp/actions/prune.js +5 -3
- package/plugins/interfaces/mcp/collections/opening.d.ts +24 -0
- package/plugins/interfaces/mcp/collections/opening.js +28 -0
- package/plugins/interfaces/mcp/mod.d.ts +27 -3
- package/plugins/interfaces/mcp/mod.js +42 -7
- package/plugins/interfaces/mcp/resources/server.d.ts +13 -4
- package/plugins/interfaces/mcp/resources/server.js +163 -21
- package/plugins/interfaces/mcp/resources/tools.d.ts +88 -12
- package/plugins/interfaces/mcp/resources/tools.js +113 -19
- package/plugins/interfaces/mcp/resources/widgets.d.ts +161 -0
- package/plugins/interfaces/mcp/resources/widgets.js +246 -0
- package/plugins/interfaces/mcp/shared/keys.d.ts +25 -19
- package/plugins/interfaces/mcp/shared/keys.js +55 -9
- package/plugins/interfaces/mcp/shared/settings.d.ts +70 -37
- package/plugins/interfaces/mcp/shared/settings.js +64 -39
- package/plugins/interfaces/mcp/views/offered.d.ts +1 -1
- package/plugins/interfaces/mod.d.ts +2 -1
- package/plugins/interfaces/mod.js +2 -1
- package/plugins/interfaces/voice/collections/blocked.d.ts +2 -2
- package/plugins/interfaces/voice/collections/call.d.ts +3 -3
- package/plugins/interfaces/voice/contract.d.ts +1 -1
- package/plugins/interfaces/voice/mod.d.ts +3 -3
- package/plugins/interfaces/voice/mod.js +3 -3
- package/plugins/interfaces/voice/resources/relay.d.ts +5 -3
- package/plugins/interfaces/voice/resources/relay.js +17 -6
- package/plugins/interfaces/voice/resources/tools.js +2 -0
- package/plugins/interfaces/voice/shared/settings.d.ts +5 -0
- package/plugins/interfaces/voice/views/call.d.ts +18 -11
- package/plugins/interfaces/voice/views/call.js +15 -3
- package/plugins/interfaces/voice/views/message.d.ts +1 -1
- package/plugins/interfaces/web/actions/arrive.d.ts +1 -0
- package/plugins/interfaces/web/actions/arrive.js +120 -0
- package/plugins/interfaces/web/actions/prune.d.ts +1 -0
- package/plugins/interfaces/web/actions/prune.js +51 -0
- package/plugins/interfaces/web/actions/sending.d.ts +1 -0
- package/plugins/interfaces/web/actions/sending.js +222 -0
- package/plugins/interfaces/web/actions/spent.d.ts +2 -0
- package/plugins/interfaces/web/actions/spent.js +59 -0
- package/plugins/interfaces/web/collections/rate.d.ts +122 -0
- package/plugins/interfaces/web/collections/rate.js +37 -0
- package/plugins/interfaces/web/collections/visitor.d.ts +110 -0
- package/plugins/interfaces/web/collections/visitor.js +64 -0
- package/plugins/interfaces/web/contract.d.ts +4 -0
- package/plugins/interfaces/web/contract.js +1 -0
- package/plugins/interfaces/web/lines.d.ts +36 -0
- package/plugins/interfaces/web/lines.js +78 -0
- package/plugins/interfaces/web/processors/forget.d.ts +10 -0
- package/plugins/interfaces/web/processors/forget.js +14 -0
- package/plugins/interfaces/web/resources/entrance.d.ts +47 -0
- package/plugins/interfaces/web/resources/entrance.js +83 -0
- package/plugins/interfaces/web/resources/spend.d.ts +2 -0
- package/plugins/interfaces/web/resources/spend.js +62 -0
- package/plugins/interfaces/web/shared/budget.d.ts +65 -0
- package/plugins/interfaces/web/shared/budget.js +134 -0
- package/plugins/interfaces/web/shared/lines.d.ts +158 -0
- package/plugins/interfaces/web/shared/lines.js +156 -0
- package/plugins/interfaces/web/shared/secret.d.ts +33 -0
- package/plugins/interfaces/web/shared/secret.js +86 -0
- package/plugins/interfaces/whatsapp/collections/phone.d.ts +1 -1
- package/plugins/interfaces/whatsapp/mod.d.ts +1 -1
- package/plugins/interfaces/whatsapp/mod.js +1 -1
- package/plugins/interfaces/whatsapp/views/message.d.ts +1 -1
- package/plugins/models/contract.d.ts +1 -1
- package/plugins/models/shared/generate.js +13 -4
- package/plugins/models/shared/transcribe.js +7 -1
- package/plugins/models/shared/use.d.ts +16 -2
- package/plugins/models/shared/use.js +20 -0
- package/runtime/browsers.d.ts +153 -17
- package/runtime/browsers.js +336 -30
- package/runtime/cache.js +25 -5
- package/runtime/caller.d.ts +117 -0
- package/runtime/caller.js +215 -0
- package/runtime/client.d.ts +47 -3
- package/runtime/client.js +132 -42
- package/runtime/define.d.ts +30 -9
- package/runtime/executor.js +7 -6
- package/runtime/expiring.d.ts +23 -0
- package/runtime/expiring.js +64 -0
- package/runtime/links.d.ts +27 -4
- package/runtime/links.js +49 -11
- package/runtime/memory.js +102 -15
- package/runtime/mod.d.ts +3 -1
- package/runtime/mod.js +3 -1
- package/runtime/mongo.d.ts +12 -0
- package/runtime/mongo.js +44 -0
- package/runtime/observe.js +82 -9
- package/runtime/ops.d.ts +9 -1
- package/runtime/owner.d.ts +14 -1
- package/runtime/owner.js +78 -23
- package/runtime/reads.js +2 -2
- package/runtime/registry.js +9 -3
- package/runtime/runtime.d.ts +7 -0
- package/runtime/runtime.js +1 -0
- package/runtime/timers.js +2 -0
- package/runtime/views.d.ts +1 -1
- package/runtime/views.js +2 -2
- package/runtime/wire.d.ts +1 -1
- package/stores/sql/postgres/maintenance.d.ts +8 -1
- package/stores/sql/postgres/maintenance.js +76 -9
- package/stores/sql/postgres/mod.d.ts +12 -4
- package/stores/sql/postgres/mod.js +21 -8
- package/plugins/interfaces/mcp/shared/caller.d.ts +0 -45
- package/plugins/interfaces/mcp/shared/caller.js +0 -99
|
@@ -11,7 +11,7 @@ import { constant, forget, ident, others, prepare, remembered, TABLES, } from ".
|
|
|
11
11
|
import { postgresPool } from "./pool.js";
|
|
12
12
|
import { dropTenant } from "./tenants.js";
|
|
13
13
|
import { statement } from "../shared/statement.js";
|
|
14
|
-
import { allowed, alone, connect, noRole, triggers } from "./maintenance.js";
|
|
14
|
+
import { allowed, alone, confined, connect, noRole, triggers } from "./maintenance.js";
|
|
15
15
|
import { connections } from "../shared/capped.js";
|
|
16
16
|
import { upTo, weight } from "../shared/rows.js";
|
|
17
17
|
export { postgresPool } from "./pool.js";
|
|
@@ -91,6 +91,11 @@ const unique = (events) => {
|
|
|
91
91
|
export async function postgresStore(options) {
|
|
92
92
|
if (!options.url === !options.pool)
|
|
93
93
|
throw new Error("postgresStore takes a url or a pool");
|
|
94
|
+
const storeRole = options.maintenance?.storeRole === true;
|
|
95
|
+
if (storeRole && options.maintenance?.url !== undefined) {
|
|
96
|
+
throw new Error("postgresStore's maintenance runs as a role of its own (url) or as the store's own role " +
|
|
97
|
+
"(storeRole: true), not both");
|
|
98
|
+
}
|
|
94
99
|
const tenant = options.tenant ?? "";
|
|
95
100
|
if (tenant.includes("\u0000"))
|
|
96
101
|
throw new Error("A tenant can't contain a NUL character");
|
|
@@ -124,6 +129,12 @@ export async function postgresStore(options) {
|
|
|
124
129
|
* temporary object last, whatever the statement set (in its transaction).
|
|
125
130
|
*/
|
|
126
131
|
const settled = "SELECT pg_catalog.set_config('search_path', 'pg_catalog, pg_temp', true)";
|
|
132
|
+
/**
|
|
133
|
+
* The role check on a server, on the connection maintenance SQL runs on:
|
|
134
|
+
* a role of its own holds exactly maintenance's grants; the store's own
|
|
135
|
+
* role can't run server commands or reach its files (maintenance.ts).
|
|
136
|
+
*/
|
|
137
|
+
const checked = (db) => storeRole ? confined(db) : allowed(db, schema);
|
|
127
138
|
let layout;
|
|
128
139
|
try {
|
|
129
140
|
layout = await prepare(pool, schema);
|
|
@@ -569,7 +580,7 @@ export async function postgresStore(options) {
|
|
|
569
580
|
throw triggered();
|
|
570
581
|
// What the role holds is as it was: no large object, setting or grant came of it.
|
|
571
582
|
if (pool.server)
|
|
572
|
-
await
|
|
583
|
+
await checked(counted(tx));
|
|
573
584
|
// Events it wrote into the log keep their numbers: this commit's go after them.
|
|
574
585
|
if (last !== undefined)
|
|
575
586
|
last = Math.max(last, Number(after.seq));
|
|
@@ -918,27 +929,29 @@ export async function postgresStore(options) {
|
|
|
918
929
|
/**
|
|
919
930
|
* Refuses maintenance SQL where it could reach what isn't this tenant's: a
|
|
920
931
|
* schema other tenants share; on a server, a role that holds anything
|
|
921
|
-
* beyond maintenance's grants
|
|
922
|
-
*
|
|
932
|
+
* beyond maintenance's grants, or as the store's own role, one that could
|
|
933
|
+
* run server commands or reach its files; in PGlite, another schema's
|
|
934
|
+
* tables (maintenance.ts). `db` is the connection it runs on.
|
|
923
935
|
*/
|
|
924
936
|
const reachable = async (db) => {
|
|
925
937
|
if (pool.server)
|
|
926
|
-
return await
|
|
938
|
+
return await checked(db);
|
|
927
939
|
if (await others(counted(committed), schema, tenant))
|
|
928
940
|
throw sharedSchema();
|
|
929
941
|
await alone(db, schema);
|
|
930
942
|
};
|
|
931
943
|
/**
|
|
932
944
|
* On a server, a connection of its own for one maintenance statement, as
|
|
933
|
-
* the maintenance role, closed after, within the cap
|
|
945
|
+
* the maintenance role (or the store's own), closed after, within the cap
|
|
946
|
+
* (maintenance.ts).
|
|
934
947
|
*/
|
|
935
948
|
const dedicated = async (work) => {
|
|
936
949
|
// A schema other tenants share comes first: no role would make SQL there theirs alone.
|
|
937
950
|
if (await others(counted(committed), schema, tenant))
|
|
938
951
|
throw sharedSchema();
|
|
939
|
-
|
|
952
|
+
const url = storeRole ? pool.url : options.maintenance?.url;
|
|
953
|
+
if (!url)
|
|
940
954
|
throw noRole();
|
|
941
|
-
const url = options.maintenance.url;
|
|
942
955
|
return await capped(async () => {
|
|
943
956
|
const db = await connect(url);
|
|
944
957
|
try {
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Who's calling, as far as the network says (see `McpCaller` in
|
|
3
|
-
* settings.ts): the caller's address, from the header the app's front sets,
|
|
4
|
-
* believed only through the front.
|
|
5
|
-
*
|
|
6
|
-
* - **With `front`** (the front's secret): a request without it came around
|
|
7
|
-
* the front, straight to the app: it's refused (`refused`).
|
|
8
|
-
* - **With `hops`:** the entry that many from the end of the header's list,
|
|
9
|
-
* the one the app's own proxy appended.
|
|
10
|
-
* - **An address that can't be read** (none, garbled, too few entries) is
|
|
11
|
-
* no address of its own: it's counted with every other such call, in one
|
|
12
|
-
* tight bucket (`unvouched`), never let through unlimited.
|
|
13
|
-
* - **How it's counted:** an IPv4 address by itself (also written as IPv6,
|
|
14
|
-
* `::ffff:a.b.c.d`); an IPv6 address by its /64 (one customer's network
|
|
15
|
-
* has billions of addresses).
|
|
16
|
-
* - **Exempt ranges** (a shared proxy's: OpenAI's) apply only to an address
|
|
17
|
-
* read this way, never to one the caller claims.
|
|
18
|
-
*
|
|
19
|
-
* Nothing here is kept: limits count an HMAC of what's counted.
|
|
20
|
-
*/
|
|
21
|
-
import type { McpCaller } from "./settings.js";
|
|
22
|
-
/** Headers that hold a list of addresses, each proxy appending its own: they need `hops`. */
|
|
23
|
-
export declare const LISTS: string[];
|
|
24
|
-
/** Who calls: refused (around the front), or what to count them by and whether they're exempt. */
|
|
25
|
-
export type Caller = "refused" | {
|
|
26
|
-
counted: string;
|
|
27
|
-
exempt: boolean;
|
|
28
|
-
};
|
|
29
|
-
/** Calls whose address can't be read, all counted as one. */
|
|
30
|
-
export declare const UNVOUCHED = "unvouched";
|
|
31
|
-
/** Who calls, by the app's `caller` settings; undefined without them (nothing goes by address). */
|
|
32
|
-
export declare function callerFrom(request: Request, caller: McpCaller | undefined): Caller | undefined;
|
|
33
|
-
type Bits = {
|
|
34
|
-
bits: bigint;
|
|
35
|
-
size: 32 | 128;
|
|
36
|
-
};
|
|
37
|
-
/**
|
|
38
|
-
* An address as bits: IPv4 in 32; IPv6 in 128, unless it's an IPv4 address
|
|
39
|
-
* written as one (`::ffff:a.b.c.d`, `::ffff:cb00:7163`), which is IPv4.
|
|
40
|
-
* Undefined when it isn't an address. A zone (`%eth0`) or brackets aren't taken.
|
|
41
|
-
*/
|
|
42
|
-
export declare function addressOf(text: string): Bits | undefined;
|
|
43
|
-
/** Whether `text` is a CIDR range. */
|
|
44
|
-
export declare const rangeOk: (text: string) => boolean;
|
|
45
|
-
export {};
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
/** The header the web host's front carries its secret in. */
|
|
2
|
-
const FRONT = "x-copilotz-front";
|
|
3
|
-
/** Headers that hold a list of addresses, each proxy appending its own: they need `hops`. */
|
|
4
|
-
export const LISTS = ["x-forwarded-for"];
|
|
5
|
-
/** Calls whose address can't be read, all counted as one. */
|
|
6
|
-
export const UNVOUCHED = "unvouched";
|
|
7
|
-
/** Whether two strings are the same, taking as long whatever their first difference. */
|
|
8
|
-
const same = (a, b) => {
|
|
9
|
-
let differ = a.length ^ b.length;
|
|
10
|
-
for (let i = 0; i < Math.max(a.length, b.length); i++) {
|
|
11
|
-
differ |= (a.charCodeAt(i) || 0) ^ (b.charCodeAt(i) || 0);
|
|
12
|
-
}
|
|
13
|
-
return differ === 0;
|
|
14
|
-
};
|
|
15
|
-
/** Who calls, by the app's `caller` settings; undefined without them (nothing goes by address). */
|
|
16
|
-
export function callerFrom(request, caller) {
|
|
17
|
-
if (!caller)
|
|
18
|
-
return undefined;
|
|
19
|
-
if (caller.front !== undefined) {
|
|
20
|
-
const said = request.headers.get(FRONT);
|
|
21
|
-
if (said === null || !same(said, caller.front))
|
|
22
|
-
return "refused";
|
|
23
|
-
}
|
|
24
|
-
const list = (request.headers.get(caller.header) ?? "").split(",").map((part) => part.trim())
|
|
25
|
-
.filter(Boolean);
|
|
26
|
-
const entry = caller.hops ? list.at(-caller.hops) : list.length === 1 ? list[0] : undefined;
|
|
27
|
-
const at = entry ? addressOf(entry) : undefined;
|
|
28
|
-
if (!at)
|
|
29
|
-
return { counted: UNVOUCHED, exempt: false };
|
|
30
|
-
return {
|
|
31
|
-
counted: at.size === 32 ? `4:${at.bits.toString(16)}` : `6:${(at.bits >> 64n).toString(16)}`,
|
|
32
|
-
exempt: inRanges(at, caller.exempt),
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
/** An IPv4 address's 32 bits, from "a.b.c.d". */
|
|
36
|
-
function v4(text) {
|
|
37
|
-
const parts = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(text)?.slice(1).map(Number);
|
|
38
|
-
if (!parts || parts.some((p) => p > 255))
|
|
39
|
-
return undefined;
|
|
40
|
-
return parts.reduce((n, p) => (n << 8n) | BigInt(p), 0n);
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* An address as bits: IPv4 in 32; IPv6 in 128, unless it's an IPv4 address
|
|
44
|
-
* written as one (`::ffff:a.b.c.d`, `::ffff:cb00:7163`), which is IPv4.
|
|
45
|
-
* Undefined when it isn't an address. A zone (`%eth0`) or brackets aren't taken.
|
|
46
|
-
*/
|
|
47
|
-
export function addressOf(text) {
|
|
48
|
-
const four = v4(text);
|
|
49
|
-
if (four !== undefined)
|
|
50
|
-
return { bits: four, size: 32 };
|
|
51
|
-
if (!/^[0-9a-f:.]+$/i.test(text) || text.split("::").length > 2)
|
|
52
|
-
return undefined;
|
|
53
|
-
// A dotted IPv4 at the end, as its two groups.
|
|
54
|
-
let written = text;
|
|
55
|
-
const dotted = /^(.*:)(\d{1,3}(?:\.\d{1,3}){3})$/.exec(text);
|
|
56
|
-
if (dotted) {
|
|
57
|
-
const bits = v4(dotted[2]);
|
|
58
|
-
if (bits === undefined)
|
|
59
|
-
return undefined;
|
|
60
|
-
written = `${dotted[1]}${(bits >> 16n).toString(16)}:${(bits & 0xffffn).toString(16)}`;
|
|
61
|
-
}
|
|
62
|
-
if (written.includes("."))
|
|
63
|
-
return undefined;
|
|
64
|
-
const [head, tail] = written.includes("::") ? written.split("::") : [written, undefined];
|
|
65
|
-
const left = head ? head.split(":") : [];
|
|
66
|
-
const right = tail ? tail.split(":") : [];
|
|
67
|
-
const missing = 8 - left.length - right.length;
|
|
68
|
-
if (tail === undefined ? left.length !== 8 : missing < 1)
|
|
69
|
-
return undefined;
|
|
70
|
-
const groups = [...left, ...Array(tail === undefined ? 0 : missing).fill("0"), ...right];
|
|
71
|
-
if (groups.some((g) => !/^[0-9a-f]{1,4}$/i.test(g)))
|
|
72
|
-
return undefined;
|
|
73
|
-
const bits = groups.reduce((n, g) => (n << 16n) | BigInt(parseInt(g, 16)), 0n);
|
|
74
|
-
// IPv4-mapped: ::ffff:0:0/96.
|
|
75
|
-
if (bits >> 32n === 0xffffn)
|
|
76
|
-
return { bits: bits & 0xffffffffn, size: 32 };
|
|
77
|
-
return { bits, size: 128 };
|
|
78
|
-
}
|
|
79
|
-
/** A CIDR range (`192.0.2.0/24`, `2001:db8::/32`) as its bits and prefix; undefined when malformed. */
|
|
80
|
-
function rangeOf(text) {
|
|
81
|
-
const [address, prefix, ...more] = text.split("/");
|
|
82
|
-
const at = addressOf(address ?? "");
|
|
83
|
-
const length = Number(prefix);
|
|
84
|
-
if (!at || more.length || !/^\d{1,3}$/.test(prefix ?? "") || length > at.size)
|
|
85
|
-
return undefined;
|
|
86
|
-
return { ...at, length };
|
|
87
|
-
}
|
|
88
|
-
/** Whether `text` is a CIDR range. */
|
|
89
|
-
export const rangeOk = (text) => !!rangeOf(text);
|
|
90
|
-
/** Whether an address is in any of the ranges. */
|
|
91
|
-
function inRanges(at, ranges = []) {
|
|
92
|
-
return ranges.some((text) => {
|
|
93
|
-
const range = rangeOf(text);
|
|
94
|
-
if (!range || range.size !== at.size)
|
|
95
|
-
return false;
|
|
96
|
-
const shift = BigInt(at.size - range.length);
|
|
97
|
-
return (at.bits >> shift) === (range.bits >> shift);
|
|
98
|
-
});
|
|
99
|
-
}
|