@onlyflows/servicenow-mcp 2.2.0-dev.6 → 2.2.1
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/.env.example +6 -3
- package/CHANGELOG.md +404 -0
- package/README.md +580 -104
- package/dist/atf-form.d.ts +3 -15
- package/dist/atf-form.d.ts.map +1 -1
- package/dist/atf-policy.d.ts +1 -1
- package/dist/atf-policy.d.ts.map +1 -1
- package/dist/atf-policy.js +2 -0
- package/dist/atf-policy.js.map +1 -1
- package/dist/atf-result-cache.d.ts +23 -59
- package/dist/atf-result-cache.d.ts.map +1 -1
- package/dist/auth.d.ts +1 -0
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +17 -0
- package/dist/auth.js.map +1 -1
- package/dist/batch-limits.d.ts +18 -0
- package/dist/batch-limits.d.ts.map +1 -0
- package/dist/batch-limits.js +21 -0
- package/dist/batch-limits.js.map +1 -0
- package/dist/bounded-input.d.ts +49 -0
- package/dist/bounded-input.d.ts.map +1 -0
- package/dist/bounded-input.js +81 -0
- package/dist/bounded-input.js.map +1 -0
- package/dist/client.d.ts +25 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +92 -18
- package/dist/client.js.map +1 -1
- package/dist/credential-tables.d.ts +32 -0
- package/dist/credential-tables.d.ts.map +1 -0
- package/dist/credential-tables.js +58 -0
- package/dist/credential-tables.js.map +1 -0
- package/dist/encoded-query-policy.d.ts +11 -0
- package/dist/encoded-query-policy.d.ts.map +1 -1
- package/dist/encoded-query-policy.js +18 -2
- package/dist/encoded-query-policy.js.map +1 -1
- package/dist/execution-context.d.ts +14 -0
- package/dist/execution-context.d.ts.map +1 -1
- package/dist/execution-context.js +8 -0
- package/dist/execution-context.js.map +1 -1
- package/dist/field-policy.d.ts +115 -3
- package/dist/field-policy.d.ts.map +1 -1
- package/dist/field-policy.js +909 -97
- package/dist/field-policy.js.map +1 -1
- package/dist/hidden-values.d.ts +225 -0
- package/dist/hidden-values.d.ts.map +1 -0
- package/dist/hidden-values.js +748 -0
- package/dist/hidden-values.js.map +1 -0
- package/dist/html-response.d.ts +34 -0
- package/dist/html-response.d.ts.map +1 -0
- package/dist/html-response.js +72 -0
- package/dist/html-response.js.map +1 -0
- package/dist/http-entrypoint.d.ts.map +1 -1
- package/dist/http-entrypoint.js +7 -0
- package/dist/http-entrypoint.js.map +1 -1
- package/dist/http-runtime.d.ts.map +1 -1
- package/dist/http-runtime.js +8 -0
- package/dist/http-runtime.js.map +1 -1
- package/dist/incident-journal-policy.d.ts +27 -5
- package/dist/incident-journal-policy.d.ts.map +1 -1
- package/dist/incident-journal-policy.js +34 -21
- package/dist/incident-journal-policy.js.map +1 -1
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/json-shape-limits.d.ts +71 -0
- package/dist/json-shape-limits.d.ts.map +1 -0
- package/dist/json-shape-limits.js +131 -0
- package/dist/json-shape-limits.js.map +1 -0
- package/dist/metadata-cache.d.ts +84 -21
- package/dist/metadata-cache.d.ts.map +1 -1
- package/dist/metadata-cache.js +316 -196
- package/dist/metadata-cache.js.map +1 -1
- package/dist/policy-card.d.ts +146 -0
- package/dist/policy-card.d.ts.map +1 -0
- package/dist/policy-card.js +420 -0
- package/dist/policy-card.js.map +1 -0
- package/dist/profile-manager.d.ts +19 -3
- package/dist/profile-manager.d.ts.map +1 -1
- package/dist/profile-manager.js +89 -4
- package/dist/profile-manager.js.map +1 -1
- package/dist/record-identifier-policy.d.ts +2 -8
- package/dist/record-identifier-policy.d.ts.map +1 -1
- package/dist/risky-grants.d.ts +25 -0
- package/dist/risky-grants.d.ts.map +1 -0
- package/dist/risky-grants.js +77 -0
- package/dist/risky-grants.js.map +1 -0
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +26 -5
- package/dist/server.js.map +1 -1
- package/dist/setup-prompts.js +1 -0
- package/dist/setup-prompts.js.map +1 -1
- package/dist/setup.d.ts +1 -1
- package/dist/setup.d.ts.map +1 -1
- package/dist/setup.js +305 -59
- package/dist/setup.js.map +1 -1
- package/dist/stdio-runtime.d.ts +15 -0
- package/dist/stdio-runtime.d.ts.map +1 -1
- package/dist/stdio-runtime.js +60 -0
- package/dist/stdio-runtime.js.map +1 -1
- package/dist/structured-query.d.ts +100 -115
- package/dist/structured-query.d.ts.map +1 -1
- package/dist/structured-query.js +138 -17
- package/dist/structured-query.js.map +1 -1
- package/dist/table-policy.d.ts +41 -4
- package/dist/table-policy.d.ts.map +1 -1
- package/dist/table-policy.js +82 -10
- package/dist/table-policy.js.map +1 -1
- package/dist/tool-error.d.ts +34 -0
- package/dist/tool-error.d.ts.map +1 -1
- package/dist/tool-error.js +109 -1
- package/dist/tool-error.js.map +1 -1
- package/dist/tool-table-access.d.ts.map +1 -1
- package/dist/tool-table-access.js +35 -5
- package/dist/tool-table-access.js.map +1 -1
- package/dist/tools/aggregate.d.ts +13 -19
- package/dist/tools/aggregate.d.ts.map +1 -1
- package/dist/tools/aggregate.js +4 -5
- package/dist/tools/aggregate.js.map +1 -1
- package/dist/tools/all-fields-cap.d.ts.map +1 -1
- package/dist/tools/all-fields-cap.js +32 -45
- package/dist/tools/all-fields-cap.js.map +1 -1
- package/dist/tools/atf-author-module.d.ts +29 -14
- package/dist/tools/atf-author-module.d.ts.map +1 -1
- package/dist/tools/atf-author-module.js +11 -5
- package/dist/tools/atf-author-module.js.map +1 -1
- package/dist/tools/atf-contracts.d.ts +135 -829
- package/dist/tools/atf-contracts.d.ts.map +1 -1
- package/dist/tools/atf-shared.js +2 -2
- package/dist/tools/atf-shared.js.map +1 -1
- package/dist/tools/atf-step-catalog.d.ts +16 -15
- package/dist/tools/atf-step-catalog.d.ts.map +1 -1
- package/dist/tools/atf-step-catalog.js +181 -10
- package/dist/tools/atf-step-catalog.js.map +1 -1
- package/dist/tools/atf.d.ts +8 -24
- package/dist/tools/atf.d.ts.map +1 -1
- package/dist/tools/atf.js +2 -2
- package/dist/tools/atf.js.map +1 -1
- package/dist/tools/attach.d.ts +6 -22
- package/dist/tools/attach.d.ts.map +1 -1
- package/dist/tools/batch.d.ts +18 -71
- package/dist/tools/batch.d.ts.map +1 -1
- package/dist/tools/batch.js +566 -100
- package/dist/tools/batch.js.map +1 -1
- package/dist/tools/catalog.d.ts +2 -2
- package/dist/tools/catalog.d.ts.map +1 -1
- package/dist/tools/catalog.js +7 -1
- package/dist/tools/catalog.js.map +1 -1
- package/dist/tools/codesearch.d.ts +1 -13
- package/dist/tools/codesearch.d.ts.map +1 -1
- package/dist/tools/create-module.d.ts +9 -14
- package/dist/tools/create-module.d.ts.map +1 -1
- package/dist/tools/create-module.js +4 -8
- package/dist/tools/create-module.js.map +1 -1
- package/dist/tools/delete.d.ts +1 -9
- package/dist/tools/delete.d.ts.map +1 -1
- package/dist/tools/delete.js +2 -3
- package/dist/tools/delete.js.map +1 -1
- package/dist/tools/discover.d.ts +6 -14
- package/dist/tools/discover.d.ts.map +1 -1
- package/dist/tools/flow-view-contracts.d.ts +1023 -0
- package/dist/tools/flow-view-contracts.d.ts.map +1 -0
- package/dist/tools/flow-view-contracts.js +570 -0
- package/dist/tools/flow-view-contracts.js.map +1 -0
- package/dist/tools/flow-view-graph.d.ts +86 -0
- package/dist/tools/flow-view-graph.d.ts.map +1 -0
- package/dist/tools/flow-view-graph.js +993 -0
- package/dist/tools/flow-view-graph.js.map +1 -0
- package/dist/tools/flow-view-gzip.d.ts +52 -0
- package/dist/tools/flow-view-gzip.d.ts.map +1 -0
- package/dist/tools/flow-view-gzip.js +179 -0
- package/dist/tools/flow-view-gzip.js.map +1 -0
- package/dist/tools/flow-view-mermaid.d.ts +37 -0
- package/dist/tools/flow-view-mermaid.d.ts.map +1 -0
- package/dist/tools/flow-view-mermaid.js +337 -0
- package/dist/tools/flow-view-mermaid.js.map +1 -0
- package/dist/tools/flow-view-module.d.ts +61 -0
- package/dist/tools/flow-view-module.d.ts.map +1 -0
- package/dist/tools/flow-view-module.js +378 -0
- package/dist/tools/flow-view-module.js.map +1 -0
- package/dist/tools/flow-view-policy.d.ts +106 -0
- package/dist/tools/flow-view-policy.d.ts.map +1 -0
- package/dist/tools/flow-view-policy.js +462 -0
- package/dist/tools/flow-view-policy.js.map +1 -0
- package/dist/tools/flow-view-renderers.d.ts +68 -0
- package/dist/tools/flow-view-renderers.d.ts.map +1 -0
- package/dist/tools/flow-view-renderers.js +513 -0
- package/dist/tools/flow-view-renderers.js.map +1 -0
- package/dist/tools/flow-view-table.d.ts +115 -0
- package/dist/tools/flow-view-table.d.ts.map +1 -0
- package/dist/tools/flow-view-table.js +1002 -0
- package/dist/tools/flow-view-table.js.map +1 -0
- package/dist/tools/get-module.d.ts +36 -42
- package/dist/tools/get-module.d.ts.map +1 -1
- package/dist/tools/health.d.ts +9 -6
- package/dist/tools/health.d.ts.map +1 -1
- package/dist/tools/incident-journal-module.d.ts +8 -7
- package/dist/tools/incident-journal-module.d.ts.map +1 -1
- package/dist/tools/index.d.ts +4 -4
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +118 -18
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/markdown.d.ts +27 -0
- package/dist/tools/markdown.d.ts.map +1 -1
- package/dist/tools/markdown.js +104 -1
- package/dist/tools/markdown.js.map +1 -1
- package/dist/tools/nl.d.ts +1 -11
- package/dist/tools/nl.d.ts.map +1 -1
- package/dist/tools/profile.d.ts +1 -1
- package/dist/tools/profile.d.ts.map +1 -1
- package/dist/tools/profile.js +20 -1
- package/dist/tools/profile.js.map +1 -1
- package/dist/tools/query-module.d.ts +59 -71
- package/dist/tools/query-module.d.ts.map +1 -1
- package/dist/tools/query-module.js +28 -7
- package/dist/tools/query-module.js.map +1 -1
- package/dist/tools/record-write-shared.d.ts.map +1 -1
- package/dist/tools/record-write-shared.js +20 -2
- package/dist/tools/record-write-shared.js.map +1 -1
- package/dist/tools/relationships.d.ts +6 -22
- package/dist/tools/relationships.d.ts.map +1 -1
- package/dist/tools/result-envelope.d.ts +2255 -1357
- package/dist/tools/result-envelope.d.ts.map +1 -1
- package/dist/tools/result-envelope.js +24 -2
- package/dist/tools/result-envelope.js.map +1 -1
- package/dist/tools/schema-module.d.ts +36 -21
- package/dist/tools/schema-module.d.ts.map +1 -1
- package/dist/tools/schema-module.js +106 -65
- package/dist/tools/schema-module.js.map +1 -1
- package/dist/tools/script.d.ts +1 -11
- package/dist/tools/script.d.ts.map +1 -1
- package/dist/tools/syslog.d.ts +11 -18
- package/dist/tools/syslog.d.ts.map +1 -1
- package/dist/tools/syslog.js +2 -3
- package/dist/tools/syslog.js.map +1 -1
- package/dist/tools/table-hierarchy.d.ts +35 -0
- package/dist/tools/table-hierarchy.d.ts.map +1 -0
- package/dist/tools/table-hierarchy.js +74 -0
- package/dist/tools/table-hierarchy.js.map +1 -0
- package/dist/tools/tool-module.d.ts +40 -15
- package/dist/tools/tool-module.d.ts.map +1 -1
- package/dist/tools/tool-module.js +755 -205
- package/dist/tools/tool-module.js.map +1 -1
- package/dist/tools/update-module.d.ts +11 -17
- package/dist/tools/update-module.d.ts.map +1 -1
- package/dist/tools/update-module.js +10 -12
- package/dist/tools/update-module.js.map +1 -1
- package/dist/unknown-filter-fields.d.ts +52 -0
- package/dist/unknown-filter-fields.d.ts.map +1 -0
- package/dist/unknown-filter-fields.js +87 -0
- package/dist/unknown-filter-fields.js.map +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/dist/write-value-policy.d.ts +20 -12
- package/dist/write-value-policy.d.ts.map +1 -1
- package/dist/write-value-policy.js +179 -67
- package/dist/write-value-policy.js.map +1 -1
- package/docs/ADDING-TOOLS.md +22 -13
- package/docs/CLIENT-SETUP.md +110 -52
- package/docs/CROSS-CLIENT-RELEASE-MATRIX.md +2 -2
- package/docs/FLOW-VIEW.md +214 -0
- package/docs/PRODUCTION-SECURITY.md +218 -25
- package/docs/RELEASE-VALIDATION.md +2 -2
- package/docs/TESTING-OAUTH.md +1 -1
- package/docs/V2-MIGRATION.md +404 -30
- package/package.json +20 -6
- package/scripts/release-validate.mjs +56 -0
- package/server.json +22 -0
- package/tools/inspector/package-lock.json +163 -201
package/dist/metadata-cache.js
CHANGED
|
@@ -6,39 +6,53 @@
|
|
|
6
6
|
* selector. The scope must discriminate credential identity as well as
|
|
7
7
|
* instance: ServiceNow evaluates ACLs per user, and a cache hit skips the
|
|
8
8
|
* upstream read entirely, so a scope shared between identities would serve
|
|
9
|
-
* one caller's authorized rows to another.
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* which is stripped before upstream I/O.
|
|
9
|
+
* one caller's authorized rows to another. Callers can force refresh by
|
|
10
|
+
* passing the internal sysparm_force_recache=true parameter, which is stripped
|
|
11
|
+
* before upstream I/O.
|
|
13
12
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
13
|
+
* Freshness is a server watermark: the table's row count (X-Total-Count) and
|
|
14
|
+
* its newest row by sys_updated_on, read back as the stored UTC value. Each
|
|
15
|
+
* entry keeps the watermark read just before its rows were fetched, and a
|
|
16
|
+
* cache hit is served only while a new watermark read still matches it. Both
|
|
17
|
+
* sides of the comparison come from the instance, so the session timezone,
|
|
18
|
+
* date format, DST and clock skew between this host and the instance play no
|
|
19
|
+
* part, and no user lookup is needed: caching behaves the same in every auth
|
|
20
|
+
* mode.
|
|
20
21
|
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* still
|
|
22
|
+
* An insert or an update makes its row the newest, and an insert or a delete
|
|
23
|
+
* changes the count, so every write moves the watermark -- at one-second
|
|
24
|
+
* resolution. An update within the stored watermark's own second, to a row
|
|
25
|
+
* other than the newest, can still go unseen; so can a write that skips the
|
|
26
|
+
* system fields. Such a row stays stale for at most one TTL: a matching
|
|
27
|
+
* watermark refreshes recency for eviction but never extends expiresAt, so the
|
|
28
|
+
* entry still expires on schedule and forces a genuine refetch.
|
|
29
|
+
*
|
|
30
|
+
* Two memos keep the watermark from costing more than it saves. Within one
|
|
31
|
+
* tool call each table's watermark is read once, however many reads of the
|
|
32
|
+
* table the call makes. Across calls, a table whose watermark proved unusable
|
|
33
|
+
* is read straight through, without a watermark, for a while: long for causes
|
|
34
|
+
* that persist (an incomplete or future-dated watermark, a 4xx), short for
|
|
35
|
+
* transient ones (429, 5xx, timeouts, network).
|
|
25
36
|
*
|
|
26
37
|
* @module metadata-cache
|
|
27
38
|
*/
|
|
28
39
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.MAX_METADATA_CACHE_ENTRIES_PER_SCOPE = exports.MAX_METADATA_CACHE_SCOPES = exports.FORCE_RECACHE_PARAM = exports.DEFAULT_METADATA_CACHE_TABLES = exports.DEFAULT_METADATA_CACHE_TTL_MS = void 0;
|
|
40
|
+
exports.MAX_DERIVED_BYTES_PER_SCOPE = exports.MAX_DERIVED_ENTRIES_PER_SCOPE = exports.TRANSIENT_UNCACHEABLE_MS = exports.STRUCTURAL_UNCACHEABLE_MS = exports.MAX_METADATA_CACHE_ENTRIES_PER_SCOPE = exports.MAX_METADATA_CACHE_SCOPES = exports.FORCE_RECACHE_PARAM = exports.DEFAULT_METADATA_CACHE_TABLES = exports.DEFAULT_METADATA_CACHE_TTL_MS = void 0;
|
|
30
41
|
exports.metadataCacheConfigFromEnvironment = metadataCacheConfigFromEnvironment;
|
|
31
42
|
exports.createMetadataCacheConfig = createMetadataCacheConfig;
|
|
43
|
+
exports.derivedMetadataMemo = derivedMetadataMemo;
|
|
44
|
+
exports.uncachedOperations = uncachedOperations;
|
|
32
45
|
exports.createCachedServiceNowOperations = createCachedServiceNowOperations;
|
|
46
|
+
const client_js_1 = require("./client.js");
|
|
33
47
|
const config_js_1 = require("./config.js");
|
|
34
|
-
const
|
|
48
|
+
const tool_error_js_1 = require("./tool-error.js");
|
|
35
49
|
exports.DEFAULT_METADATA_CACHE_TTL_MS = 24 * 60 * 60 * 1000;
|
|
36
50
|
/**
|
|
37
51
|
* Tables cached by default.
|
|
38
52
|
*
|
|
39
|
-
* The bar for inclusion is a large payload relative to the
|
|
53
|
+
* The bar for inclusion is a large payload relative to the watermark read,
|
|
40
54
|
* not merely "this table changes rarely" -- a cache hit still costs one
|
|
41
|
-
* upstream round trip (see
|
|
55
|
+
* upstream round trip (see tableWatermark), so the saving is payload bytes,
|
|
42
56
|
* never request count. sys_properties is deliberately absent: its rows are
|
|
43
57
|
* ACL-restricted per user and routinely hold integration secrets in `value`,
|
|
44
58
|
* and the only reads of it here are three single-value build-info lookups
|
|
@@ -56,6 +70,9 @@ exports.DEFAULT_METADATA_CACHE_TABLES = Object.freeze([
|
|
|
56
70
|
]);
|
|
57
71
|
const SCOPED_CACHES = new Map();
|
|
58
72
|
const TABLE_NAME = /^[a-z][a-z0-9_]{0,79}$/u;
|
|
73
|
+
/** sys_updated_on as returned with sysparm_display_value=false (UTC). */
|
|
74
|
+
const STORED_DATE_TIME = /^(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2})$/u;
|
|
75
|
+
const ROW_COUNT = /^(?:0|[1-9]\d{0,15})$/u;
|
|
59
76
|
exports.FORCE_RECACHE_PARAM = "sysparm_force_recache";
|
|
60
77
|
const MAX_CACHE_TABLE_PATTERNS = 128;
|
|
61
78
|
const MAX_CACHE_TTL_MS = 7 * 24 * 60 * 60 * 1000;
|
|
@@ -64,22 +81,15 @@ const MAX_CACHE_TTL_MS = 7 * 24 * 60 * 60 * 1000;
|
|
|
64
81
|
* so both are capped and evicted in least-recently-used order. */
|
|
65
82
|
exports.MAX_METADATA_CACHE_SCOPES = 64;
|
|
66
83
|
exports.MAX_METADATA_CACHE_ENTRIES_PER_SCOPE = 512;
|
|
67
|
-
/**
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
* AND credential identity: two profiles on one instance can authenticate as
|
|
77
|
-
* accounts in different timezones, and sharing a resolution between them would
|
|
78
|
-
* corrupt the freshness window for both.
|
|
79
|
-
*/
|
|
80
|
-
const SCOPED_TIME_ZONES = new Map();
|
|
81
|
-
const TIME_ZONE_WARNED_SCOPES = new Set();
|
|
82
|
-
const DEFAULT_TIME_ZONE_PROPERTY = "glide.sys.default.tz";
|
|
84
|
+
/** How long a table goes uncached after its watermark proved unusable; never
|
|
85
|
+
* longer than the cache TTL. */
|
|
86
|
+
exports.STRUCTURAL_UNCACHEABLE_MS = 15 * 60 * 1000;
|
|
87
|
+
exports.TRANSIENT_UNCACHEABLE_MS = 30 * 1000;
|
|
88
|
+
/** Bounds on derived values per scope (see derivedMetadataMemo). */
|
|
89
|
+
exports.MAX_DERIVED_ENTRIES_PER_SCOPE = 512;
|
|
90
|
+
exports.MAX_DERIVED_BYTES_PER_SCOPE = 1024 * 1024;
|
|
91
|
+
/** Fixed allowance per derived value for its record and map slot. */
|
|
92
|
+
const ENTRY_OVERHEAD_BYTES = 512;
|
|
83
93
|
function metadataCacheConfigFromEnvironment(environment = process.env) {
|
|
84
94
|
const ttlMs = (0, config_js_1.parsePositiveIntegerEnv)(environment.SN_METADATA_CACHE_TTL_MS, "SN_METADATA_CACHE_TTL_MS", { max: MAX_CACHE_TTL_MS });
|
|
85
95
|
const tables = parseTablePatterns(environment.SN_METADATA_CACHE_TABLES);
|
|
@@ -94,40 +104,130 @@ function createMetadataCacheConfig(input) {
|
|
|
94
104
|
tables: normalizeTablePatterns(input?.tables ?? exports.DEFAULT_METADATA_CACHE_TABLES),
|
|
95
105
|
});
|
|
96
106
|
}
|
|
107
|
+
/** The operations each cached wrapper reads through. */
|
|
108
|
+
const UNCACHED_OPERATIONS = new WeakMap();
|
|
109
|
+
/** Each cached wrapper's scope and configuration, for derivedMetadataMemo. */
|
|
110
|
+
const WRAPPER_SCOPES = new WeakMap();
|
|
111
|
+
function derivedMetadataMemo(operations) {
|
|
112
|
+
const bound = WRAPPER_SCOPES.get(operations);
|
|
113
|
+
if (bound === undefined)
|
|
114
|
+
return undefined;
|
|
115
|
+
const { state, config } = bound;
|
|
116
|
+
return Object.freeze({
|
|
117
|
+
pending(key) {
|
|
118
|
+
return state.inflight.get(key);
|
|
119
|
+
},
|
|
120
|
+
track(key, promise) {
|
|
121
|
+
if (state.detached || state.inflight.size >= exports.MAX_DERIVED_ENTRIES_PER_SCOPE)
|
|
122
|
+
return promise;
|
|
123
|
+
state.inflight.set(key, promise);
|
|
124
|
+
// Registered before any waiter, so the entry is gone by the time a
|
|
125
|
+
// waiter sees the outcome.
|
|
126
|
+
const clear = () => {
|
|
127
|
+
if (state.inflight.get(key) === promise)
|
|
128
|
+
state.inflight.delete(key);
|
|
129
|
+
};
|
|
130
|
+
promise.then(clear, clear);
|
|
131
|
+
return promise;
|
|
132
|
+
},
|
|
133
|
+
get(key) {
|
|
134
|
+
const entry = state.derived.get(key);
|
|
135
|
+
if (entry === undefined)
|
|
136
|
+
return undefined;
|
|
137
|
+
if (entry.expiresAt <= Date.now()) {
|
|
138
|
+
removeDerived(state, key);
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
state.derived.delete(key);
|
|
142
|
+
state.derived.set(key, entry);
|
|
143
|
+
return entry.value;
|
|
144
|
+
},
|
|
145
|
+
set(key, value, bytes, ttlMs) {
|
|
146
|
+
if (state.detached)
|
|
147
|
+
return;
|
|
148
|
+
removeDerived(state, key);
|
|
149
|
+
const size = Math.max(0, Math.ceil(bytes)) + key.length + ENTRY_OVERHEAD_BYTES;
|
|
150
|
+
if (size > exports.MAX_DERIVED_BYTES_PER_SCOPE)
|
|
151
|
+
return;
|
|
152
|
+
state.derived.set(key, Object.freeze({ expiresAt: Date.now() + Math.min(ttlMs, config.ttlMs), value, bytes: size }));
|
|
153
|
+
state.derivedBytes += size;
|
|
154
|
+
while (state.derived.size > exports.MAX_DERIVED_ENTRIES_PER_SCOPE ||
|
|
155
|
+
state.derivedBytes > exports.MAX_DERIVED_BYTES_PER_SCOPE) {
|
|
156
|
+
const oldest = state.derived.keys().next();
|
|
157
|
+
if (oldest.done)
|
|
158
|
+
break;
|
|
159
|
+
removeDerived(state, oldest.value);
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
function removeDerived(state, key) {
|
|
165
|
+
const entry = state.derived.get(key);
|
|
166
|
+
if (entry === undefined)
|
|
167
|
+
return;
|
|
168
|
+
state.derived.delete(key);
|
|
169
|
+
state.derivedBytes -= entry.bytes;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* The operations underneath a cached wrapper, or `operations` itself when it
|
|
173
|
+
* is not one.
|
|
174
|
+
*
|
|
175
|
+
* Use it for a read whose answer decides a write, such as the records a bulk
|
|
176
|
+
* update or delete targets. A cache hit can be stale within the watermark's
|
|
177
|
+
* documented gaps, which a read can tolerate but a write target cannot. The
|
|
178
|
+
* read costs one request, with no watermark, and leaves the cache untouched.
|
|
179
|
+
* The internal force-recache parameter is no substitute: it costs a watermark
|
|
180
|
+
* read as well, stores a single-use entry, and reaches ServiceNow unstripped
|
|
181
|
+
* when no wrapper is in place.
|
|
182
|
+
*/
|
|
183
|
+
function uncachedOperations(operations) {
|
|
184
|
+
return UNCACHED_OPERATIONS.get(operations) ?? operations;
|
|
185
|
+
}
|
|
97
186
|
/**
|
|
98
187
|
* @param scope Cache partition key. Must identify the ServiceNow instance
|
|
99
188
|
* *and* the credential identity reading from it -- see the module docstring.
|
|
100
|
-
* @param sessionUserName ServiceNow user_name this scope authenticates as.
|
|
101
|
-
* Required to resolve the session timezone; without it the freshness probe
|
|
102
|
-
* cannot be made correct, so caching is disabled for the scope.
|
|
103
189
|
*/
|
|
104
|
-
function createCachedServiceNowOperations(operations, scope, config
|
|
190
|
+
function createCachedServiceNowOperations(operations, scope, config) {
|
|
105
191
|
if (config === undefined && process.env.NODE_ENV === "test") {
|
|
106
192
|
return operations;
|
|
107
193
|
}
|
|
108
194
|
const cacheConfig = createMetadataCacheConfig(config);
|
|
109
|
-
const
|
|
195
|
+
const state = stateForScope(scope);
|
|
196
|
+
// One watermark per table for this wrapper's lifetime, which is one tool
|
|
197
|
+
// call: the dispatcher builds a wrapper per call. A hierarchy walk then
|
|
198
|
+
// reads sys_db_object's watermark once, not once per hop. Any write the call
|
|
199
|
+
// makes clears it, so the call's later reads see its own writes. No memo
|
|
200
|
+
// spans calls: that would add staleness across calls for almost no time.
|
|
201
|
+
const watermarks = new Map();
|
|
202
|
+
const clearingWatermarks = (write) => async (...args) => {
|
|
203
|
+
try {
|
|
204
|
+
return await write(...args);
|
|
205
|
+
}
|
|
206
|
+
finally {
|
|
207
|
+
watermarks.clear();
|
|
208
|
+
}
|
|
209
|
+
};
|
|
110
210
|
const cached = Object.create(null);
|
|
111
211
|
Object.defineProperties(cached, {
|
|
112
212
|
get: {
|
|
113
|
-
value: async (path, params) => (await cachedGetWithMeta(operations,
|
|
213
|
+
value: async (path, params) => (await cachedGetWithMeta(operations, state, watermarks, cacheConfig, path, params)).data,
|
|
114
214
|
},
|
|
115
215
|
getWithMeta: {
|
|
116
|
-
value: (path, params) => cachedGetWithMeta(operations,
|
|
117
|
-
scope,
|
|
118
|
-
sessionUserName,
|
|
119
|
-
}),
|
|
216
|
+
value: (path, params) => cachedGetWithMeta(operations, state, watermarks, cacheConfig, path, params),
|
|
120
217
|
},
|
|
121
|
-
post: { value: operations.post.bind(operations) },
|
|
122
|
-
patch: { value: operations.patch.bind(operations) },
|
|
123
|
-
delete: { value: operations.delete.bind(operations) },
|
|
124
|
-
postBinary: { value: operations.postBinary.bind(operations) },
|
|
218
|
+
post: { value: clearingWatermarks(operations.post.bind(operations)) },
|
|
219
|
+
patch: { value: clearingWatermarks(operations.patch.bind(operations)) },
|
|
220
|
+
delete: { value: clearingWatermarks(operations.delete.bind(operations)) },
|
|
221
|
+
postBinary: { value: clearingWatermarks(operations.postBinary.bind(operations)) },
|
|
125
222
|
getRaw: { value: operations.getRaw.bind(operations) },
|
|
126
|
-
...(operations.saveAtfStepInputs ? { saveAtfStepInputs: { value: operations.saveAtfStepInputs.bind(operations) } } : {}),
|
|
223
|
+
...(operations.saveAtfStepInputs ? { saveAtfStepInputs: { value: clearingWatermarks(operations.saveAtfStepInputs.bind(operations)) } } : {}),
|
|
127
224
|
});
|
|
128
|
-
|
|
225
|
+
Object.freeze(cached);
|
|
226
|
+
UNCACHED_OPERATIONS.set(cached, operations);
|
|
227
|
+
WRAPPER_SCOPES.set(cached, { state, config: cacheConfig });
|
|
228
|
+
return cached;
|
|
129
229
|
}
|
|
130
|
-
async function cachedGetWithMeta(operations,
|
|
230
|
+
async function cachedGetWithMeta(operations, state, watermarks, config, path, params) {
|
|
131
231
|
// Strip the internal flag before any upstream call, including the
|
|
132
232
|
// passthrough: sysparm_force_recache is this module's own parameter and must
|
|
133
233
|
// never reach ServiceNow, whether or not the table is cached.
|
|
@@ -136,37 +236,56 @@ async function cachedGetWithMeta(operations, cache, config, path, params, identi
|
|
|
136
236
|
if (!match || !tableMatches(config.tables, match)) {
|
|
137
237
|
return operations.getWithMeta(path, cleanParams);
|
|
138
238
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
239
|
+
const now = Date.now();
|
|
240
|
+
const uncacheableUntil = state.uncacheable.get(match);
|
|
241
|
+
if (uncacheableUntil !== undefined) {
|
|
242
|
+
if (!forceRecache && uncacheableUntil > now) {
|
|
243
|
+
return operations.getWithMeta(path, cleanParams);
|
|
244
|
+
}
|
|
245
|
+
// Expired, or the caller asked for a refresh: try the watermark again.
|
|
246
|
+
state.uncacheable.delete(match);
|
|
247
|
+
watermarks.delete(match);
|
|
145
248
|
}
|
|
249
|
+
const cache = state.entries;
|
|
146
250
|
const key = cacheKey(path, cleanParams);
|
|
147
|
-
const now = Date.now();
|
|
148
251
|
const existing = cache.get(key);
|
|
149
|
-
|
|
252
|
+
// Read on a miss too, and always before the rows. An update landing between
|
|
253
|
+
// the two reads is then in the rows but not the watermark, which costs one
|
|
254
|
+
// extra refresh; read the other way round it would be in the watermark but
|
|
255
|
+
// not the rows, and the next comparison would serve it stale until expiry.
|
|
256
|
+
// A watermark memoized earlier in the call was also read before these rows.
|
|
257
|
+
const read = await callWatermark(operations, watermarks, match);
|
|
258
|
+
if (!read.usable)
|
|
259
|
+
markUncacheable(state, match, read.reason, config.ttlMs);
|
|
260
|
+
const watermark = read.usable ? read.watermark : undefined;
|
|
261
|
+
if (!forceRecache &&
|
|
262
|
+
existing !== undefined &&
|
|
263
|
+
existing.expiresAt > now &&
|
|
264
|
+
watermark !== undefined &&
|
|
265
|
+
sameWatermark(existing.watermark, watermark)) {
|
|
266
|
+
// Re-insert to mark the entry most recently used for eviction order.
|
|
150
267
|
cache.delete(key);
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
if (!changed) {
|
|
154
|
-
// Re-insert to mark the entry most recently used for eviction order.
|
|
155
|
-
cache.delete(key);
|
|
156
|
-
cache.set(key, existing);
|
|
157
|
-
return cloneCachedResult(existing);
|
|
158
|
-
}
|
|
268
|
+
cache.set(key, existing);
|
|
269
|
+
return cloneCachedResult(existing);
|
|
159
270
|
}
|
|
160
|
-
|
|
271
|
+
// A forced refresh must never leave behind the rows it was asked to replace:
|
|
272
|
+
// it is how a caller surfaces a write the watermark missed. Any other
|
|
273
|
+
// refetch leaves the entry until it is replaced -- if these rows cannot be
|
|
274
|
+
// kept, the old ones are still correct against any later watermark read that
|
|
275
|
+
// matches theirs, so they serve again once the table is cacheable again.
|
|
276
|
+
if (forceRecache)
|
|
277
|
+
cache.delete(key);
|
|
161
278
|
const fresh = await operations.getWithMeta(path, cleanParams);
|
|
162
279
|
// Only a materialized 200 body is reusable. A 204 carries no rows, and a
|
|
163
280
|
// failed read throws before reaching here, so nothing negative is cached.
|
|
164
|
-
|
|
281
|
+
// Rows without a watermark could never be revalidated, so they are not kept.
|
|
282
|
+
if (fresh.status !== 200 || fresh.data === null || watermark === undefined) {
|
|
165
283
|
return fresh;
|
|
284
|
+
}
|
|
166
285
|
cache.delete(key);
|
|
167
286
|
cache.set(key, Object.freeze({
|
|
168
|
-
expiresAt:
|
|
169
|
-
|
|
287
|
+
expiresAt: now + config.ttlMs,
|
|
288
|
+
watermark,
|
|
170
289
|
data: cloneJson(fresh.data),
|
|
171
290
|
status: fresh.status,
|
|
172
291
|
headers: Object.freeze([...fresh.headers.entries()]),
|
|
@@ -174,6 +293,22 @@ async function cachedGetWithMeta(operations, cache, config, path, params, identi
|
|
|
174
293
|
evictOldest(cache, exports.MAX_METADATA_CACHE_ENTRIES_PER_SCOPE);
|
|
175
294
|
return fresh;
|
|
176
295
|
}
|
|
296
|
+
function callWatermark(operations, watermarks, table) {
|
|
297
|
+
let pending = watermarks.get(table);
|
|
298
|
+
if (pending === undefined) {
|
|
299
|
+
pending = tableWatermark(operations, table);
|
|
300
|
+
watermarks.set(table, pending);
|
|
301
|
+
}
|
|
302
|
+
return pending;
|
|
303
|
+
}
|
|
304
|
+
function markUncacheable(state, table, reason, ttlMs) {
|
|
305
|
+
if (reason === "cancelled")
|
|
306
|
+
return;
|
|
307
|
+
const period = reason === "structural" ? exports.STRUCTURAL_UNCACHEABLE_MS : exports.TRANSIENT_UNCACHEABLE_MS;
|
|
308
|
+
state.uncacheable.delete(table);
|
|
309
|
+
state.uncacheable.set(table, Date.now() + Math.min(period, ttlMs));
|
|
310
|
+
evictOldest(state.uncacheable, exports.MAX_METADATA_CACHE_ENTRIES_PER_SCOPE);
|
|
311
|
+
}
|
|
177
312
|
/** Drop least-recently-used entries until the map is within `limit`. */
|
|
178
313
|
function evictOldest(entries, limit) {
|
|
179
314
|
while (entries.size > limit) {
|
|
@@ -183,23 +318,104 @@ function evictOldest(entries, limit) {
|
|
|
183
318
|
entries.delete(oldest.value);
|
|
184
319
|
}
|
|
185
320
|
}
|
|
186
|
-
|
|
321
|
+
/**
|
|
322
|
+
* The table's row count and newest row by sys_updated_on, read back as stored.
|
|
323
|
+
*
|
|
324
|
+
* The order is evaluated on the instance and the values are compared as opaque
|
|
325
|
+
* strings, so no date literal is ever built on this side. The count is the
|
|
326
|
+
* X-Total-Count header of the same request, which carries the table's total
|
|
327
|
+
* only while sysparm_no_count is left unset (with it set, the header still
|
|
328
|
+
* comes back but holds the page size).
|
|
329
|
+
*
|
|
330
|
+
* A count of 0 with no rows is a valid watermark for an empty table: the
|
|
331
|
+
* header counts rows before row ACLs filter them (a table whose newest row is
|
|
332
|
+
* ACL-hidden reads back a positive count and no row), so any insert moves it.
|
|
333
|
+
*
|
|
334
|
+
* The watermark is unusable, structurally, when:
|
|
335
|
+
* - the response is not a 200, or the count or a row field is missing or
|
|
336
|
+
* malformed (an unreadable sys_updated_on would compare equal forever);
|
|
337
|
+
* - the count is positive but no row came back, which is what a row ACL that
|
|
338
|
+
* filters after the limit produces when it hides the newest row;
|
|
339
|
+
* - the newest row is dated after the instance's own clock (the response Date
|
|
340
|
+
* header). Such a row stays the newest however many others are updated, so
|
|
341
|
+
* only the count would move, and in-place updates would go unseen.
|
|
342
|
+
* A thrown read is classified by readFailureReason.
|
|
343
|
+
*/
|
|
344
|
+
async function tableWatermark(operations, table) {
|
|
345
|
+
const structural = { usable: false, reason: "structural" };
|
|
187
346
|
try {
|
|
188
|
-
const response = await operations.
|
|
189
|
-
sysparm_query:
|
|
347
|
+
const response = await operations.getWithMeta(`/api/now/table/${table}`, {
|
|
348
|
+
sysparm_query: "ORDERBYDESCsys_updated_on",
|
|
190
349
|
sysparm_fields: "sys_id,sys_updated_on",
|
|
191
350
|
sysparm_limit: "1",
|
|
192
|
-
sysparm_no_count: "true",
|
|
193
351
|
sysparm_display_value: "false",
|
|
194
352
|
sysparm_exclude_reference_link: "true",
|
|
195
353
|
});
|
|
196
|
-
|
|
354
|
+
const count = response.headers.get("x-total-count") ?? "";
|
|
355
|
+
if (response.status !== 200 || !ROW_COUNT.test(count))
|
|
356
|
+
return structural;
|
|
357
|
+
const rows = response.data?.result;
|
|
358
|
+
if (!Array.isArray(rows))
|
|
359
|
+
return structural;
|
|
360
|
+
if (rows.length === 0) {
|
|
361
|
+
return count === "0"
|
|
362
|
+
? { usable: true, watermark: Object.freeze({ count: 0, sysId: "", updatedOn: "" }) }
|
|
363
|
+
: structural;
|
|
364
|
+
}
|
|
365
|
+
const row = rows[0];
|
|
366
|
+
if (typeof row !== "object" || row === null)
|
|
367
|
+
return structural;
|
|
368
|
+
const { sys_id: sysId, sys_updated_on: updatedOn } = row;
|
|
369
|
+
if (typeof sysId !== "string" || sysId === "")
|
|
370
|
+
return structural;
|
|
371
|
+
if (typeof updatedOn !== "string")
|
|
372
|
+
return structural;
|
|
373
|
+
const stored = STORED_DATE_TIME.exec(updatedOn);
|
|
374
|
+
const updatedAt = stored ? Date.parse(`${stored[1]}T${stored[2]}Z`) : Number.NaN;
|
|
375
|
+
const serverNow = Date.parse(response.headers.get("date") ?? "");
|
|
376
|
+
if (!Number.isFinite(updatedAt) || !Number.isFinite(serverNow))
|
|
377
|
+
return structural;
|
|
378
|
+
if (updatedAt > serverNow)
|
|
379
|
+
return structural;
|
|
380
|
+
return { usable: true, watermark: Object.freeze({ count: Number(count), sysId, updatedOn }) };
|
|
197
381
|
}
|
|
198
|
-
catch {
|
|
199
|
-
// A failed
|
|
200
|
-
return
|
|
382
|
+
catch (error) {
|
|
383
|
+
// A failed read must not serve stale metadata.
|
|
384
|
+
return { usable: false, reason: readFailureReason(error) };
|
|
201
385
|
}
|
|
202
386
|
}
|
|
387
|
+
/**
|
|
388
|
+
* A 403, 404, 409 or other fixed rejection of this request shape will repeat,
|
|
389
|
+
* so it is structural. A 401 (a token refresh can fix it), 429, 5xx, timeout,
|
|
390
|
+
* network or unrecognized failure is transient.
|
|
391
|
+
*/
|
|
392
|
+
function readFailureReason(error) {
|
|
393
|
+
if ((0, client_js_1.isServiceNowRequestCancellation)(error))
|
|
394
|
+
return "cancelled";
|
|
395
|
+
const descriptor = (0, tool_error_js_1.trustedToolErrorDescriptor)(error);
|
|
396
|
+
switch (descriptor?.category) {
|
|
397
|
+
case "authorization":
|
|
398
|
+
case "not_found":
|
|
399
|
+
case "conflict":
|
|
400
|
+
return "structural";
|
|
401
|
+
case "upstream":
|
|
402
|
+
// The client marks a 5xx GET retryable and any other status do_not_retry.
|
|
403
|
+
return descriptor.retry === "do_not_retry" ? "structural" : "transient";
|
|
404
|
+
default:
|
|
405
|
+
return "transient";
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Every field counts. The count catches inserts and deletes; the sys_id also
|
|
410
|
+
* catches the newest row being replaced as newest within the same second. A
|
|
411
|
+
* tie at the top that the instance orders differently between reads only
|
|
412
|
+
* costs an extra refresh.
|
|
413
|
+
*/
|
|
414
|
+
function sameWatermark(left, right) {
|
|
415
|
+
return (left.count === right.count &&
|
|
416
|
+
left.sysId === right.sysId &&
|
|
417
|
+
left.updatedOn === right.updatedOn);
|
|
418
|
+
}
|
|
203
419
|
function metadataTableFromPath(path) {
|
|
204
420
|
const match = /^\/api\/now\/table\/([a-z][a-z0-9_]{0,79})(?:\/|$)/u.exec(path);
|
|
205
421
|
return match?.[1];
|
|
@@ -219,16 +435,34 @@ function stripForceRecache(params) {
|
|
|
219
435
|
forceRecache: forceValue === "true" || forceValue === "1",
|
|
220
436
|
};
|
|
221
437
|
}
|
|
222
|
-
function
|
|
438
|
+
function stateForScope(scope) {
|
|
223
439
|
const existing = SCOPED_CACHES.get(scope);
|
|
224
440
|
if (existing) {
|
|
225
441
|
SCOPED_CACHES.delete(scope);
|
|
226
442
|
SCOPED_CACHES.set(scope, existing);
|
|
227
443
|
return existing;
|
|
228
444
|
}
|
|
229
|
-
|
|
445
|
+
// Not frozen: the derived memo's byte total and the detached flag change.
|
|
446
|
+
const next = {
|
|
447
|
+
entries: new Map(),
|
|
448
|
+
uncacheable: new Map(),
|
|
449
|
+
derived: new Map(),
|
|
450
|
+
derivedBytes: 0,
|
|
451
|
+
inflight: new Map(),
|
|
452
|
+
detached: false,
|
|
453
|
+
};
|
|
230
454
|
SCOPED_CACHES.set(scope, next);
|
|
231
|
-
|
|
455
|
+
while (SCOPED_CACHES.size > exports.MAX_METADATA_CACHE_SCOPES) {
|
|
456
|
+
const oldest = SCOPED_CACHES.entries().next();
|
|
457
|
+
if (oldest.done)
|
|
458
|
+
break;
|
|
459
|
+
const [oldScope, oldState] = oldest.value;
|
|
460
|
+
SCOPED_CACHES.delete(oldScope);
|
|
461
|
+
oldState.derived.clear();
|
|
462
|
+
oldState.derivedBytes = 0;
|
|
463
|
+
oldState.inflight.clear();
|
|
464
|
+
oldState.detached = true;
|
|
465
|
+
}
|
|
232
466
|
return next;
|
|
233
467
|
}
|
|
234
468
|
function cacheKey(path, params) {
|
|
@@ -249,120 +483,6 @@ function cloneJson(value) {
|
|
|
249
483
|
return value;
|
|
250
484
|
return JSON.parse(JSON.stringify(value));
|
|
251
485
|
}
|
|
252
|
-
/**
|
|
253
|
-
* Format an instant as ServiceNow's `YYYY-MM-DD HH:mm:ss` in the given zone.
|
|
254
|
-
*
|
|
255
|
-
* The offset is derived from the zone NAME for this specific instant, so a
|
|
256
|
-
* timestamp on either side of a DST transition resolves to the offset actually
|
|
257
|
-
* in force then. This is why the name is what gets cached.
|
|
258
|
-
*/
|
|
259
|
-
function serviceNowDateTime(epochMs, timeZone) {
|
|
260
|
-
const parts = new Intl.DateTimeFormat("en-US", {
|
|
261
|
-
timeZone,
|
|
262
|
-
hourCycle: "h23",
|
|
263
|
-
year: "numeric",
|
|
264
|
-
month: "2-digit",
|
|
265
|
-
day: "2-digit",
|
|
266
|
-
hour: "2-digit",
|
|
267
|
-
minute: "2-digit",
|
|
268
|
-
second: "2-digit",
|
|
269
|
-
}).formatToParts(new Date(epochMs));
|
|
270
|
-
const field = (type) => parts.find((part) => part.type === type)?.value ?? "00";
|
|
271
|
-
return (`${field("year")}-${field("month")}-${field("day")} ` +
|
|
272
|
-
`${field("hour")}:${field("minute")}:${field("second")}`);
|
|
273
|
-
}
|
|
274
|
-
/** True when Intl recognizes the name, so a bad value never silently formats. */
|
|
275
|
-
function isUsableTimeZone(candidate) {
|
|
276
|
-
if (typeof candidate !== "string" || candidate.trim() === "")
|
|
277
|
-
return false;
|
|
278
|
-
try {
|
|
279
|
-
new Intl.DateTimeFormat("en-US", { timeZone: candidate.trim() });
|
|
280
|
-
return true;
|
|
281
|
-
}
|
|
282
|
-
catch {
|
|
283
|
-
return false;
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
/**
|
|
287
|
-
* Resolve, and memoize per scope, the timezone the session user's
|
|
288
|
-
* `sys_updated_on` comparisons are evaluated in.
|
|
289
|
-
*
|
|
290
|
-
* Chain: the user's own `sys_user.time_zone`, then the instance default in
|
|
291
|
-
* `glide.sys.default.tz`, then unresolved. `time_zone` is not a required field
|
|
292
|
-
* and is frequently empty; an empty value means "inherit the system default"
|
|
293
|
-
* and must never be read as UTC, which is the very error this fixes.
|
|
294
|
-
*/
|
|
295
|
-
async function sessionTimeZone(operations, identity) {
|
|
296
|
-
const existing = SCOPED_TIME_ZONES.get(identity.scope);
|
|
297
|
-
if (existing) {
|
|
298
|
-
SCOPED_TIME_ZONES.delete(identity.scope);
|
|
299
|
-
SCOPED_TIME_ZONES.set(identity.scope, existing);
|
|
300
|
-
return existing;
|
|
301
|
-
}
|
|
302
|
-
const pending = resolveSessionTimeZone(operations, identity);
|
|
303
|
-
SCOPED_TIME_ZONES.set(identity.scope, pending);
|
|
304
|
-
evictOldest(SCOPED_TIME_ZONES, exports.MAX_METADATA_CACHE_SCOPES);
|
|
305
|
-
return pending;
|
|
306
|
-
}
|
|
307
|
-
async function resolveSessionTimeZone(operations, identity) {
|
|
308
|
-
const resolved = (await userTimeZone(operations, identity.sessionUserName)) ??
|
|
309
|
-
(await instanceDefaultTimeZone(operations));
|
|
310
|
-
if (resolved === undefined)
|
|
311
|
-
warnTimeZoneUnresolved(identity);
|
|
312
|
-
return resolved;
|
|
313
|
-
}
|
|
314
|
-
async function userTimeZone(operations, sessionUserName) {
|
|
315
|
-
if (typeof sessionUserName !== "string" || sessionUserName.trim() === "") {
|
|
316
|
-
return undefined;
|
|
317
|
-
}
|
|
318
|
-
return firstUsableValue(operations, "/api/now/table/sys_user", {
|
|
319
|
-
sysparm_query: `user_name=${(0, utils_js_1.escapeQueryValue)(sessionUserName.trim())}`,
|
|
320
|
-
sysparm_fields: "time_zone",
|
|
321
|
-
sysparm_limit: "1",
|
|
322
|
-
}, "time_zone");
|
|
323
|
-
}
|
|
324
|
-
async function instanceDefaultTimeZone(operations) {
|
|
325
|
-
return firstUsableValue(operations, "/api/now/table/sys_properties", {
|
|
326
|
-
sysparm_query: `name=${DEFAULT_TIME_ZONE_PROPERTY}`,
|
|
327
|
-
sysparm_fields: "value",
|
|
328
|
-
sysparm_limit: "1",
|
|
329
|
-
}, "value");
|
|
330
|
-
}
|
|
331
|
-
async function firstUsableValue(operations, path, params, field) {
|
|
332
|
-
try {
|
|
333
|
-
const response = await operations.get(path, {
|
|
334
|
-
...params,
|
|
335
|
-
sysparm_display_value: "false",
|
|
336
|
-
sysparm_exclude_reference_link: "true",
|
|
337
|
-
});
|
|
338
|
-
const row = Array.isArray(response?.result) ? response.result[0] : undefined;
|
|
339
|
-
if (typeof row !== "object" || row === null)
|
|
340
|
-
return undefined;
|
|
341
|
-
const value = row[field];
|
|
342
|
-
return isUsableTimeZone(value) ? value.trim() : undefined;
|
|
343
|
-
}
|
|
344
|
-
catch {
|
|
345
|
-
// An unreadable lookup is indistinguishable from an empty one here, and
|
|
346
|
-
// both mean the same thing: the probe cannot be made correct.
|
|
347
|
-
return undefined;
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
/**
|
|
351
|
-
* Report the degradation once per scope. Names the account, never the scope
|
|
352
|
-
* key -- that carries the credential fingerprint.
|
|
353
|
-
*/
|
|
354
|
-
function warnTimeZoneUnresolved(identity) {
|
|
355
|
-
if (TIME_ZONE_WARNED_SCOPES.has(identity.scope))
|
|
356
|
-
return;
|
|
357
|
-
TIME_ZONE_WARNED_SCOPES.add(identity.scope);
|
|
358
|
-
const account = typeof identity.sessionUserName === "string" && identity.sessionUserName.trim() !== ""
|
|
359
|
-
? `account "${identity.sessionUserName.trim()}"`
|
|
360
|
-
: "an account with no configured user name";
|
|
361
|
-
process.stderr.write(`[servicenow-mcp] WARNING: metadata caching is disabled for ${account}: the ` +
|
|
362
|
-
`ServiceNow session timezone could not be resolved. Grant read access to ` +
|
|
363
|
-
`sys_user (time_zone) and sys_properties (${DEFAULT_TIME_ZONE_PROPERTY}), or ` +
|
|
364
|
-
`set the account's timezone, to re-enable it.\n`);
|
|
365
|
-
}
|
|
366
486
|
function validateTtlMs(value) {
|
|
367
487
|
if (typeof value !== "number" ||
|
|
368
488
|
!Number.isSafeInteger(value) ||
|