@canopy-io/node 0.25.1 → 0.26.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 +6 -0
- package/README.md +12 -7
- package/dist/index.cjs +95 -35
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +397 -19
- package/dist/index.d.ts +397 -19
- package/dist/index.js +95 -35
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1352,6 +1352,19 @@ function understood(page, path) {
|
|
|
1352
1352
|
var GRANTS_PATH = (type, id) => type === "service" ? `/api/v1/oauth-clients/${encodeURIComponent(id)}/grants` : `/api/v1/identities/${encodeURIComponent(id)}/grants`;
|
|
1353
1353
|
var subjectKey = (type, id) => `${type}:${id}`;
|
|
1354
1354
|
var PARENTS_PATH = "/api/v1/nodes/parents";
|
|
1355
|
+
function nextWindowEdge(holdings, now) {
|
|
1356
|
+
let edge = Number.POSITIVE_INFINITY;
|
|
1357
|
+
for (const windows of holdings.roots.values()) {
|
|
1358
|
+
for (const window of windows) {
|
|
1359
|
+
for (const at of [window.from, window.to]) {
|
|
1360
|
+
if (at !== null && at > now && at < edge) {
|
|
1361
|
+
edge = at;
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1365
|
+
}
|
|
1366
|
+
return edge;
|
|
1367
|
+
}
|
|
1355
1368
|
function inEffect(window, now) {
|
|
1356
1369
|
return (window.from === null || window.from <= now) && (window.to === null || window.to > now);
|
|
1357
1370
|
}
|
|
@@ -1399,6 +1412,7 @@ var LocalAuthorizer = class {
|
|
|
1399
1412
|
stats = {
|
|
1400
1413
|
grantFetches: 0,
|
|
1401
1414
|
grantPages: 0,
|
|
1415
|
+
grantNotModified: 0,
|
|
1402
1416
|
treeRequests: 0,
|
|
1403
1417
|
treePages: 0,
|
|
1404
1418
|
treeNotModified: 0
|
|
@@ -2009,50 +2023,87 @@ var LocalAuthorizer = class {
|
|
|
2009
2023
|
*
|
|
2010
2024
|
* Every page carries the subject's deny roots whole, beside its rows; they
|
|
2011
2025
|
* are gathered from each page, so a read is one snapshot of both.
|
|
2026
|
+
*
|
|
2027
|
+
* Revalidate rather than re-read. The first page is asked for with the
|
|
2028
|
+
* validator the held grants came with, and a `304` means nothing that
|
|
2029
|
+
* decides this subject's access has changed: the grants stay in memory and
|
|
2030
|
+
* only their expiry moves. Canopy answers it after reading the subject's
|
|
2031
|
+
* access versions alone, so the common refresh costs one small read on both
|
|
2032
|
+
* sides. Only the first page is conditional; its validator covers every
|
|
2033
|
+
* page.
|
|
2034
|
+
*
|
|
2035
|
+
* A read over several pages is not confirmed the way the hierarchy's is.
|
|
2036
|
+
* The validator kept is the first page's, taken before any later page was
|
|
2037
|
+
* read, so a change made during the walk moves the subject's version past
|
|
2038
|
+
* it, and the next refresh reads every page again.
|
|
2012
2039
|
*/
|
|
2013
2040
|
async fetchGrantRoots(identityId, type, stillCurrent = () => true) {
|
|
2014
2041
|
this.stats.grantFetches += 1;
|
|
2042
|
+
const key = subjectKey(type, identityId);
|
|
2043
|
+
const held = this.grants.get(key);
|
|
2015
2044
|
const path = GRANTS_PATH(type, identityId);
|
|
2016
2045
|
const denies = /* @__PURE__ */ new Map();
|
|
2017
|
-
const
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
}
|
|
2039
|
-
{ page: 1, take: PAGE_SIZE },
|
|
2040
|
-
{ maxPages: MAX_PAGES }
|
|
2046
|
+
const query = { take: PAGE_SIZE, include: "windows" };
|
|
2047
|
+
const gather = (page) => {
|
|
2048
|
+
const read = understood(
|
|
2049
|
+
page,
|
|
2050
|
+
path
|
|
2051
|
+
).denies;
|
|
2052
|
+
for (const deny of read ?? []) {
|
|
2053
|
+
const nodes = denies.get(deny.permission);
|
|
2054
|
+
if (nodes) {
|
|
2055
|
+
nodes.add(deny.node_id);
|
|
2056
|
+
} else {
|
|
2057
|
+
denies.set(deny.permission, /* @__PURE__ */ new Set([deny.node_id]));
|
|
2058
|
+
}
|
|
2059
|
+
}
|
|
2060
|
+
return page;
|
|
2061
|
+
};
|
|
2062
|
+
this.stats.grantPages += 1;
|
|
2063
|
+
const first = await this.client.requestConditional(
|
|
2064
|
+
"GET",
|
|
2065
|
+
path,
|
|
2066
|
+
held?.etag ?? void 0,
|
|
2067
|
+
{ ...this.readOptions, query: { ...query, page: 1 } }
|
|
2041
2068
|
);
|
|
2069
|
+
if (!first.modified) {
|
|
2070
|
+
if (held) {
|
|
2071
|
+
this.stats.grantNotModified += 1;
|
|
2072
|
+
return this.keepGrants(key, held.holdings, held.etag, stillCurrent);
|
|
2073
|
+
}
|
|
2074
|
+
return this.keepGrants(
|
|
2075
|
+
key,
|
|
2076
|
+
{ roots: /* @__PURE__ */ new Map(), denies },
|
|
2077
|
+
null,
|
|
2078
|
+
stillCurrent
|
|
2079
|
+
);
|
|
2080
|
+
}
|
|
2081
|
+
const rows = [...gather(first.data).items];
|
|
2082
|
+
if (spansPages(first.data)) {
|
|
2083
|
+
const rest = paginate(
|
|
2084
|
+
(pageQuery) => {
|
|
2085
|
+
this.stats.grantPages += 1;
|
|
2086
|
+
return this.client.request("GET", path, {
|
|
2087
|
+
...this.readOptions,
|
|
2088
|
+
query: { ...pageQuery, include: query.include }
|
|
2089
|
+
}).then(gather);
|
|
2090
|
+
},
|
|
2091
|
+
{ page: first.data.pagination.page + 1, take: PAGE_SIZE },
|
|
2092
|
+
{ maxPages: MAX_PAGES - 1 }
|
|
2093
|
+
);
|
|
2094
|
+
for await (const page of rest.pages()) {
|
|
2095
|
+
for (const row of page.items) {
|
|
2096
|
+
rows.push(row);
|
|
2097
|
+
}
|
|
2098
|
+
}
|
|
2099
|
+
}
|
|
2042
2100
|
const roots = /* @__PURE__ */ new Map();
|
|
2043
|
-
const
|
|
2044
|
-
let boundary = Number.POSITIVE_INFINITY;
|
|
2045
|
-
for await (const row of rows) {
|
|
2101
|
+
for (const row of rows) {
|
|
2046
2102
|
const window = {
|
|
2047
2103
|
node: row.node_id,
|
|
2048
2104
|
from: row.effective_from ? Date.parse(row.effective_from) : null,
|
|
2049
2105
|
to: row.effective_to ? Date.parse(row.effective_to) : null
|
|
2050
2106
|
};
|
|
2051
|
-
for (const edge of [window.from, window.to]) {
|
|
2052
|
-
if (edge !== null && edge > now && edge < boundary) {
|
|
2053
|
-
boundary = edge;
|
|
2054
|
-
}
|
|
2055
|
-
}
|
|
2056
2107
|
const windows = roots.get(row.permission);
|
|
2057
2108
|
if (windows) {
|
|
2058
2109
|
windows.push(window);
|
|
@@ -2060,11 +2111,20 @@ var LocalAuthorizer = class {
|
|
|
2060
2111
|
roots.set(row.permission, [window]);
|
|
2061
2112
|
}
|
|
2062
2113
|
}
|
|
2063
|
-
|
|
2114
|
+
return this.keepGrants(key, { roots, denies }, first.etag, stillCurrent);
|
|
2115
|
+
}
|
|
2116
|
+
/**
|
|
2117
|
+
* Hold a subject's grants until the TTL, or until the nearest window edge
|
|
2118
|
+
* still ahead when that comes sooner, so a grant takes effect or ends on
|
|
2119
|
+
* time.
|
|
2120
|
+
*/
|
|
2121
|
+
keepGrants(key, holdings, etag, stillCurrent) {
|
|
2122
|
+
const now = this.now();
|
|
2064
2123
|
if (stillCurrent()) {
|
|
2065
|
-
this.grants.set(
|
|
2124
|
+
this.grants.set(key, {
|
|
2066
2125
|
holdings,
|
|
2067
|
-
|
|
2126
|
+
etag,
|
|
2127
|
+
expiresAt: Math.min(now + this.ttlMs, nextWindowEdge(holdings, now))
|
|
2068
2128
|
});
|
|
2069
2129
|
}
|
|
2070
2130
|
return holdings;
|