@cognium/mcp-server 0.1.20 → 0.2.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 +110 -0
- package/README.md +68 -2
- package/dist/bin.d.ts +18 -0
- package/dist/bin.d.ts.map +1 -0
- package/dist/bin.js +34 -0
- package/dist/bin.js.map +1 -0
- package/dist/enablement.d.ts +67 -0
- package/dist/enablement.d.ts.map +1 -0
- package/dist/enablement.js +97 -0
- package/dist/enablement.js.map +1 -0
- package/dist/index.d.ts +13 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -20
- package/dist/index.js.map +1 -1
- package/dist/jcs.d.ts +28 -0
- package/dist/jcs.d.ts.map +1 -0
- package/dist/jcs.js +60 -0
- package/dist/jcs.js.map +1 -0
- package/dist/licence.d.ts +119 -0
- package/dist/licence.d.ts.map +1 -0
- package/dist/licence.js +234 -0
- package/dist/licence.js.map +1 -0
- package/dist/modules.d.ts +83 -0
- package/dist/modules.d.ts.map +1 -0
- package/dist/modules.js +136 -0
- package/dist/modules.js.map +1 -0
- package/dist/server.d.ts +44 -3
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +82 -8
- package/dist/server.js.map +1 -1
- package/dist/startup.d.ts +14 -0
- package/dist/startup.d.ts.map +1 -0
- package/dist/startup.js +42 -0
- package/dist/startup.js.map +1 -0
- package/dist/tools/types.d.ts +9 -0
- package/dist/tools/types.d.ts.map +1 -1
- package/dist/tools/types.js.map +1 -1
- package/dist/util/wasm.d.ts.map +1 -1
- package/dist/util/wasm.js +3 -1
- package/dist/util/wasm.js.map +1 -1
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,116 @@ All notable changes to `@cognium/mcp-server` are documented in this file.
|
|
|
4
4
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
|
|
5
5
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
6
|
|
|
7
|
+
## [0.2.0] - 2026-10-01
|
|
8
|
+
|
|
9
|
+
Tested against `circle-ir` **4.9.29** (range `4.9`) and MCP SDK **^1.30.1**. No
|
|
10
|
+
tool was added, removed or renamed, and no response schema changed: `tools/list`
|
|
11
|
+
on a default install is the same eleven tools it was on 0.1.21.
|
|
12
|
+
|
|
13
|
+
### Added
|
|
14
|
+
- **The package is importable.** `dist/index.js` is now a side-effect-free
|
|
15
|
+
library entry exporting `buildServer`, `createServer`, `ProjectCache` and the
|
|
16
|
+
types below. A host can build a server and mount it over its own transport
|
|
17
|
+
without spawning a subprocess.
|
|
18
|
+
- **Optional tool modules.** `buildServer({ modules })` takes an array of
|
|
19
|
+
`ToolModule` — `{ id, version, licence, circleIrRange, register(server, ctx) }`
|
|
20
|
+
— and registers each after the eleven built-in tools. A module receives
|
|
21
|
+
`ctx = { cache, enablement }`: the server's own analysis cache, so a module
|
|
22
|
+
need not parse a project twice, and what the install is allowed to offer.
|
|
23
|
+
- **`createServer()`** resolves the optional modules, computes enablement once
|
|
24
|
+
per process, and returns both the server and what it found.
|
|
25
|
+
- **Three states, reported not enforced.** `floor` (this package alone),
|
|
26
|
+
`extended` (an optional module loaded) and `commercial` (a licence token that
|
|
27
|
+
verified). The state is computed once from the environment and the user config
|
|
28
|
+
directory, and it decides what is *listed*; it locks no feature, and a module
|
|
29
|
+
that is installed stays installed whatever the state.
|
|
30
|
+
- **Offline licence verification** (`verifyLicence`): Ed25519 over a compact
|
|
31
|
+
signed token, with no network, no cache and nothing written. Verdicts are
|
|
32
|
+
`valid`, `expired`, `invalid-signature`, `malformed`, `no-key` and `absent`.
|
|
33
|
+
A wrong key and an altered payload are indistinguishable offline and both
|
|
34
|
+
report `invalid-signature`, which the tests assert rather than paper over.
|
|
35
|
+
No verification key is published yet, so tokens currently report `no-key`.
|
|
36
|
+
- **The token format.** `cognium-lic-v1.<base64url payload>.<base64url
|
|
37
|
+
signature>`, payload `{ kid, org, tier, iat, expiry, entitlements? }`. The
|
|
38
|
+
signature covers the payload's **canonical** form (RFC 8785), not the bytes
|
|
39
|
+
it arrived in, so a token survives being re-encoded in transit. `kid` names
|
|
40
|
+
the signing key and is itself signed, so keys rotate without invalidating
|
|
41
|
+
tokens already issued — a verifier keeps the retired key while tokens signed
|
|
42
|
+
by it are in date. The prefix is versioned, so a change to the shape is a
|
|
43
|
+
prefix bump rather than a silent break. The signature covers every member of
|
|
44
|
+
the payload, including ones this version does not read, so an issuer can add
|
|
45
|
+
a field without breaking verifiers already installed. A date-only `expiry`
|
|
46
|
+
is in date through the whole of that UTC day.
|
|
47
|
+
- **`canonicalise()`** — RFC 8785 JSON canonicalisation, forty lines and no new
|
|
48
|
+
dependency, tested against the RFC's own key-ordering vector.
|
|
49
|
+
- **One startup line** on stderr naming the version, the `circle-ir` it is
|
|
50
|
+
running against, the state, and any module loaded. Never the token, and never
|
|
51
|
+
the endpoint's value.
|
|
52
|
+
|
|
53
|
+
### Changed
|
|
54
|
+
- **The bin moved from `dist/index.js` to `dist/bin.js`.** The bin *name*,
|
|
55
|
+
`mcp-server-cognium-dev`, is unchanged, so `npx @cognium/mcp-server` and every
|
|
56
|
+
existing client configuration keep working. This is the breaking part of the
|
|
57
|
+
minor bump: anything that executed the file path directly must point at
|
|
58
|
+
`dist/bin.js`. Importing the package no longer attaches a transport to the
|
|
59
|
+
importing process's stdin and stdout.
|
|
60
|
+
- **Server name and version come from the manifest.** They were string literals
|
|
61
|
+
and had drifted to `0.1.0` while the package was at `0.1.21`, so the
|
|
62
|
+
`initialize` handshake under-reported the version. It now reports the real one.
|
|
63
|
+
- Coverage thresholds ratcheted to the newly measured values (94 statements,
|
|
64
|
+
84 branches, 96 functions, 96 lines).
|
|
65
|
+
|
|
66
|
+
### Fixed
|
|
67
|
+
- A module that is absent, broken, wrongly shaped, built against another
|
|
68
|
+
`circle-ir` minor, or throwing from `register` can no longer take the server
|
|
69
|
+
down. Each case is refused on one stderr line and the eleven built-in tools
|
|
70
|
+
serve as normal. A patch-level `circle-ir` difference is accepted on purpose:
|
|
71
|
+
that is the release-time pin check's job, not the loader's.
|
|
72
|
+
- **#409: `.ts` files are parsed with the TypeScript grammar.** The server passed
|
|
73
|
+
`circle-ir` an explicit grammar map that loaded the JavaScript grammar for
|
|
74
|
+
`typescript`, overriding the library's own default. TypeScript-only syntax then
|
|
75
|
+
parsed as error-recovered garbage: an interface method signature
|
|
76
|
+
`query(text: string): Promise<T>;` became a `query(...)` call and was reported
|
|
77
|
+
as `sql_injection` and `missing-await` on the signature line. `.js` files are
|
|
78
|
+
unaffected, and `.tsx` already used its own grammar.
|
|
79
|
+
|
|
80
|
+
This also removes an order dependence. Another library in the same process
|
|
81
|
+
that initialised `circle-ir` first, with the TypeScript grammar, decided how
|
|
82
|
+
every later tool parsed `.ts` — so the same call could answer differently
|
|
83
|
+
depending on what had run before it.
|
|
84
|
+
|
|
85
|
+
#### Consumer Impact
|
|
86
|
+
- **Results for TypeScript files change, in both directions, with no error.**
|
|
87
|
+
Applies to every tool that analyses a project: `scan`, `taint_paths`,
|
|
88
|
+
`list_entry_points`, `attack_surface_summary`, `list_reachable_sinks`,
|
|
89
|
+
`explain_finding`, `find_similar`. On 20,988 `.ts` files from public
|
|
90
|
+
repositories, comparing the two grammars: **550 taint flows removed and 502
|
|
91
|
+
added**; sinks 1,433 removed and 1,953 added. Sampled removals were misparses
|
|
92
|
+
of casts, generics and type annotations read as calls. They were not all
|
|
93
|
+
labelled, so a removed flow is not guaranteed to have been a false positive.
|
|
94
|
+
- **Many more note-level findings on TypeScript**: 38,437 added against 2,432
|
|
95
|
+
removed on that corpus, mostly `missing-public-doc`, `variable-shadowing` and
|
|
96
|
+
`leaked-global`. The JavaScript grammar dropped whole functions and classes
|
|
97
|
+
that used TypeScript-only syntax, so those passes never saw them.
|
|
98
|
+
- A consumer holding a baseline of TypeScript findings should regenerate it.
|
|
99
|
+
JavaScript, and every other language, is unchanged.
|
|
100
|
+
|
|
101
|
+
## [0.1.21] - 2026-09-30
|
|
102
|
+
|
|
103
|
+
### Changed
|
|
104
|
+
- Adopts `circle-ir@4.9.29`, a C# recall release. C# scans report substantially
|
|
105
|
+
more, and report **cross-file** C# taint flows for the first time — 4.9.28
|
|
106
|
+
produced none. Juliet C# baseline detection goes 97 → **105 of 123** (85.4%)
|
|
107
|
+
on the default taint config, CWE-81 goes 0/9 → **8/9**, and cross-file paths on
|
|
108
|
+
the Juliet `_5xx`/`_7xx` sets go from 0 to 505 (272 true positives), with
|
|
109
|
+
nothing lost. Driven by #539 (a `#if` directive hid every class member), #502
|
|
110
|
+
(I/O, network and database reads as sources), #542 (response sinks follow the
|
|
111
|
+
declared type, not the receiver name) and #530 + #503 part 2 (taint crosses a
|
|
112
|
+
`StringBuilder`, and CodeDOM/Roslyn compilation are code-injection sinks —
|
|
113
|
+
CWE-94 goes 0/10 to **10/10**, taking the 10 scored families to **115/123**,
|
|
114
|
+
93.5%). No scored C# family is at zero any more. No MCP tool, schema or
|
|
115
|
+
response-shape changes.
|
|
116
|
+
|
|
7
117
|
## [0.1.20] - 2026-09-30
|
|
8
118
|
|
|
9
119
|
### Changed
|
package/README.md
CHANGED
|
@@ -96,12 +96,78 @@ The same stdio config is bundled in the [Cognium SAST plugin](../../plugins/cogn
|
|
|
96
96
|
4. `find_similar` to catch the same pattern elsewhere.
|
|
97
97
|
5. Before proposing a fix, `check_sanitizer` any proposed wrapper against the target sink type.
|
|
98
98
|
|
|
99
|
+
## Using it as a library
|
|
100
|
+
|
|
101
|
+
The package is side-effect free to import: nothing is built, read or connected
|
|
102
|
+
until you ask for it. The stdio binary is a separate file, so mounting the
|
|
103
|
+
server over your own transport does not spawn a subprocess.
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
import { createServer } from '@cognium/mcp-server';
|
|
107
|
+
|
|
108
|
+
// Floor plus whatever optional modules this install has.
|
|
109
|
+
const { server, discovery } = await createServer();
|
|
110
|
+
console.error(discovery.enablement.state); // 'floor' | 'extended' | 'commercial'
|
|
111
|
+
await server.connect(myTransport);
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
`buildServer(options)` is the synchronous form: it registers exactly what you
|
|
115
|
+
pass and resolves nothing.
|
|
116
|
+
|
|
117
|
+
## Optional tool modules
|
|
118
|
+
|
|
119
|
+
The eleven tools above are always present. A separate package may add more by
|
|
120
|
+
exporting a `ToolModule`:
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
export default {
|
|
124
|
+
id: 'example/mcp',
|
|
125
|
+
version: '1.0.0',
|
|
126
|
+
licence: 'MIT',
|
|
127
|
+
circleIrRange: '4.9', // the circle-ir major.minor it was built against
|
|
128
|
+
register(server, ctx) {
|
|
129
|
+
// ctx.cache — the server's analysis cache, shared with every tool
|
|
130
|
+
// ctx.enablement — what this install may offer; the module decides what
|
|
131
|
+
// to register, and checks again at call time
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Set `COGNIUM_MCP_MODULES` to a comma-separated list of specifiers to control
|
|
137
|
+
what is looked for, or to `none` to look for nothing.
|
|
138
|
+
|
|
139
|
+
**The server comes up either way.** A module that is absent, broken, wrongly
|
|
140
|
+
shaped or built against a different `circle-ir` minor is refused on one line of
|
|
141
|
+
stderr, and the eleven built-in tools serve as normal.
|
|
142
|
+
|
|
143
|
+
| Variable | Effect |
|
|
144
|
+
|---|---|
|
|
145
|
+
| `COGNIUM_MCP_MODULES` | Which module specifiers to look for. `none` disables. |
|
|
146
|
+
| `COGNIUM_ENDPOINT` | An opaque endpoint passed through to modules. The server never parses or connects to it. |
|
|
147
|
+
| `COGNIUM_LICENSE` | A licence token, verified offline. Also read from `$COGNIUM_CONFIG_DIR/license`, default `~/.cognium/license`. |
|
|
148
|
+
| `COGNIUM_LICENSE_PUBKEY` | Extra verification keys as `kid=key[,kid=key]`, base64url raw Ed25519, merged over the published ones. A bare key with no `kid=` matches any token, which is a development convenience and not for production. |
|
|
149
|
+
|
|
150
|
+
A token is `cognium-lic-v1.<base64url payload>.<base64url Ed25519 signature>`.
|
|
151
|
+
The payload is `{ kid, org, tier, iat, expiry, entitlements? }` and the
|
|
152
|
+
signature covers its **canonical** form (RFC 8785), so a token survives being
|
|
153
|
+
re-encoded in transit. `kid` names the signing key, so keys rotate without
|
|
154
|
+
invalidating tokens already issued: a verifier holds the old key as long as
|
|
155
|
+
tokens signed by it are still in date. An `expiry` given as a bare date is good
|
|
156
|
+
through the whole of that UTC day. A field the verifier does not know is still
|
|
157
|
+
covered by the signature; a known field of the wrong shape is `malformed`.
|
|
158
|
+
|
|
159
|
+
Verification is offline — a published key, no network, nothing cached or
|
|
160
|
+
written. No key is baked in yet, so tokens currently report `no-key`.
|
|
161
|
+
|
|
162
|
+
No telemetry, no phone-home, and no feature is locked behind a token: the state
|
|
163
|
+
decides what is *listed*, and the terms carry the grant.
|
|
164
|
+
|
|
99
165
|
## Development
|
|
100
166
|
|
|
101
167
|
```bash
|
|
102
|
-
npm run build # tsc + chmod +x dist/
|
|
168
|
+
npm run build # tsc + chmod +x dist/bin.js
|
|
103
169
|
npm run typecheck # tsc --noEmit
|
|
104
|
-
npm test # vitest run (unit
|
|
170
|
+
npm test # vitest run (unit, three-state matrix, stdio smoke)
|
|
105
171
|
```
|
|
106
172
|
|
|
107
173
|
## License
|
package/dist/bin.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* The `@cognium/mcp-server` binary. Wires the server to stdio — the
|
|
4
|
+
* transport every MCP client (Claude Desktop, Claude Code, Cursor) speaks by
|
|
5
|
+
* default.
|
|
6
|
+
*
|
|
7
|
+
* This file is the bin and nothing else, so it may run `main()`
|
|
8
|
+
* unconditionally. The library entry is `index.ts`, which has no side
|
|
9
|
+
* effects: importing the package no longer attaches a transport to the host
|
|
10
|
+
* process's stdin and stdout, which is what made the package impossible to
|
|
11
|
+
* mount over HTTP before.
|
|
12
|
+
*
|
|
13
|
+
* WASM initialization stays deferred to the first tool invocation so
|
|
14
|
+
* `--version` and the initial handshake do not pay the ~200 ms tree-sitter
|
|
15
|
+
* cost.
|
|
16
|
+
*/
|
|
17
|
+
export {};
|
|
18
|
+
//# sourceMappingURL=bin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bin.d.ts","sourceRoot":"","sources":["../src/bin.ts"],"names":[],"mappings":";AACA;;;;;;;;;;;;;;GAcG"}
|
package/dist/bin.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* The `@cognium/mcp-server` binary. Wires the server to stdio — the
|
|
4
|
+
* transport every MCP client (Claude Desktop, Claude Code, Cursor) speaks by
|
|
5
|
+
* default.
|
|
6
|
+
*
|
|
7
|
+
* This file is the bin and nothing else, so it may run `main()`
|
|
8
|
+
* unconditionally. The library entry is `index.ts`, which has no side
|
|
9
|
+
* effects: importing the package no longer attaches a transport to the host
|
|
10
|
+
* process's stdin and stdout, which is what made the package impossible to
|
|
11
|
+
* mount over HTTP before.
|
|
12
|
+
*
|
|
13
|
+
* WASM initialization stays deferred to the first tool invocation so
|
|
14
|
+
* `--version` and the initial handshake do not pay the ~200 ms tree-sitter
|
|
15
|
+
* cost.
|
|
16
|
+
*/
|
|
17
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
18
|
+
import { createServer } from './server.js';
|
|
19
|
+
import { startupLines } from './startup.js';
|
|
20
|
+
async function main() {
|
|
21
|
+
const { server, discovery } = await createServer();
|
|
22
|
+
for (const line of startupLines(discovery)) {
|
|
23
|
+
process.stderr.write(`${line}\n`);
|
|
24
|
+
}
|
|
25
|
+
const transport = new StdioServerTransport();
|
|
26
|
+
await server.connect(transport);
|
|
27
|
+
}
|
|
28
|
+
main().catch((err) => {
|
|
29
|
+
// stderr is safe for stdio-transport servers (stdout is reserved for MCP
|
|
30
|
+
// messages).
|
|
31
|
+
process.stderr.write(`[@cognium/mcp-server] fatal: ${err.stack ?? err}\n`);
|
|
32
|
+
process.exit(1);
|
|
33
|
+
});
|
|
34
|
+
//# sourceMappingURL=bin.js.map
|
package/dist/bin.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bin.js","sourceRoot":"","sources":["../src/bin.ts"],"names":[],"mappings":";AACA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AACjF,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,KAAK,UAAU,IAAI;IACjB,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,GAAG,MAAM,YAAY,EAAE,CAAC;IACnD,KAAK,MAAM,IAAI,IAAI,YAAY,CAAC,SAAS,CAAC,EAAE,CAAC;QAC3C,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,IAAI,IAAI,CAAC,CAAC;IACpC,CAAC;IACD,MAAM,SAAS,GAAG,IAAI,oBAAoB,EAAE,CAAC;IAC7C,MAAM,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;AAClC,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;IACnB,yEAAyE;IACzE,aAAa;IACb,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,gCAAiC,GAAa,CAAC,KAAK,IAAI,GAAG,IAAI,CAAC,CAAC;IACtF,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enablement — what this install is allowed to offer, computed once.
|
|
3
|
+
*
|
|
4
|
+
* Three states, and only three:
|
|
5
|
+
*
|
|
6
|
+
* floor the server alone. Deterministic tools, MIT, always.
|
|
7
|
+
* extended an optional tool module resolved and loaded.
|
|
8
|
+
* commercial extended, plus a licence token that verified.
|
|
9
|
+
*
|
|
10
|
+
* Two rules worth stating because they are easy to get wrong:
|
|
11
|
+
*
|
|
12
|
+
* - A token that verifies but with no module loaded is **not** commercial.
|
|
13
|
+
* Commercial is extended *plus* a token; with nothing extra installed
|
|
14
|
+
* there is nothing extra to licence, so the state is floor and the token
|
|
15
|
+
* is simply recorded.
|
|
16
|
+
* - Past expiry the install behaves as `extended`, never worse. Expiry
|
|
17
|
+
* removes commercial rights; it does not take away code the user already
|
|
18
|
+
* has, and it does not switch anything off.
|
|
19
|
+
*
|
|
20
|
+
* `endpoint` is an opaque string. The server records only *whether* one is
|
|
21
|
+
* configured and passes it through untouched; it never parses it, connects to
|
|
22
|
+
* it, or learns what is on the other end. That is the module's business.
|
|
23
|
+
*
|
|
24
|
+
* Computed once per process and memoized, because the HTTP host builds a
|
|
25
|
+
* fresh server per request (stateless transport) and so has no startup of its
|
|
26
|
+
* own to compute this at. Recomputing per request would re-read the
|
|
27
|
+
* environment and the config file on every call and could report two
|
|
28
|
+
* different states inside one process.
|
|
29
|
+
*/
|
|
30
|
+
import { type LicenceVerdict } from './licence.js';
|
|
31
|
+
export type LicenceState = 'floor' | 'extended' | 'commercial';
|
|
32
|
+
export interface Enablement {
|
|
33
|
+
/** At least one optional tool module resolved and loaded. */
|
|
34
|
+
readonly installed: boolean;
|
|
35
|
+
/** A configured endpoint, verbatim and opaque, if there is one. */
|
|
36
|
+
readonly endpoint?: string;
|
|
37
|
+
/** The verdict on the configured token, or `absent` if there was none. */
|
|
38
|
+
readonly licence: LicenceVerdict;
|
|
39
|
+
readonly state: LicenceState;
|
|
40
|
+
}
|
|
41
|
+
/** Environment variables this module reads. Nothing else is consulted. */
|
|
42
|
+
export declare const ENV_ENDPOINT = "COGNIUM_ENDPOINT";
|
|
43
|
+
export declare const ENV_LICENSE = "COGNIUM_LICENSE";
|
|
44
|
+
export declare const ENV_LICENSE_PUBKEY = "COGNIUM_LICENSE_PUBKEY";
|
|
45
|
+
export declare const ENV_CONFIG_DIR = "COGNIUM_CONFIG_DIR";
|
|
46
|
+
/** Where `cognium license set` writes, when no config dir is given. */
|
|
47
|
+
export declare function configDir(env?: NodeJS.ProcessEnv): string;
|
|
48
|
+
export interface ComputeEnablementInput {
|
|
49
|
+
/** Whether an optional module loaded. Decided by the loader, not here. */
|
|
50
|
+
readonly installed: boolean;
|
|
51
|
+
readonly env?: NodeJS.ProcessEnv;
|
|
52
|
+
/** Clock injection point for expiry tests. */
|
|
53
|
+
readonly now?: Date;
|
|
54
|
+
}
|
|
55
|
+
/** Compute enablement from the environment and the user config dir. */
|
|
56
|
+
export declare function computeEnablement(input: ComputeEnablementInput): Enablement;
|
|
57
|
+
/**
|
|
58
|
+
* The process-wide enablement. First call computes it; every later call —
|
|
59
|
+
* including every request-scoped server the HTTP host builds — gets the same
|
|
60
|
+
* object back.
|
|
61
|
+
*/
|
|
62
|
+
export declare function enablementForProcess(input: ComputeEnablementInput): Enablement;
|
|
63
|
+
/** Drop the memo. For tests; nothing in the server calls this. */
|
|
64
|
+
export declare function resetEnablementForTests(): void;
|
|
65
|
+
/** The floor state, for a server built with no modules at all. */
|
|
66
|
+
export declare function floorEnablement(): Enablement;
|
|
67
|
+
//# sourceMappingURL=enablement.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enablement.d.ts","sourceRoot":"","sources":["../src/enablement.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAKH,OAAO,EAAoC,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AAErF,MAAM,MAAM,YAAY,GAAG,OAAO,GAAG,UAAU,GAAG,YAAY,CAAC;AAE/D,MAAM,WAAW,UAAU;IACzB,6DAA6D;IAC7D,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAC5B,mEAAmE;IACnE,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,0EAA0E;IAC1E,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;IACjC,QAAQ,CAAC,KAAK,EAAE,YAAY,CAAC;CAC9B;AAED,0EAA0E;AAC1E,eAAO,MAAM,YAAY,qBAAqB,CAAC;AAC/C,eAAO,MAAM,WAAW,oBAAoB,CAAC;AAC7C,eAAO,MAAM,kBAAkB,2BAA2B,CAAC;AAC3D,eAAO,MAAM,cAAc,uBAAuB,CAAC;AAEnD,uEAAuE;AACvE,wBAAgB,SAAS,CAAC,GAAG,GAAE,MAAM,CAAC,UAAwB,GAAG,MAAM,CAEtE;AAaD,MAAM,WAAW,sBAAsB;IACrC,0EAA0E;IAC1E,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAC5B,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC;IACjC,8CAA8C;IAC9C,QAAQ,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC;CACrB;AAED,uEAAuE;AACvE,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,sBAAsB,GAAG,UAAU,CAuB3E;AAID;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,sBAAsB,GAAG,UAAU,CAG9E;AAED,kEAAkE;AAClE,wBAAgB,uBAAuB,IAAI,IAAI,CAE9C;AAED,kEAAkE;AAClE,wBAAgB,eAAe,IAAI,UAAU,CAM5C"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enablement — what this install is allowed to offer, computed once.
|
|
3
|
+
*
|
|
4
|
+
* Three states, and only three:
|
|
5
|
+
*
|
|
6
|
+
* floor the server alone. Deterministic tools, MIT, always.
|
|
7
|
+
* extended an optional tool module resolved and loaded.
|
|
8
|
+
* commercial extended, plus a licence token that verified.
|
|
9
|
+
*
|
|
10
|
+
* Two rules worth stating because they are easy to get wrong:
|
|
11
|
+
*
|
|
12
|
+
* - A token that verifies but with no module loaded is **not** commercial.
|
|
13
|
+
* Commercial is extended *plus* a token; with nothing extra installed
|
|
14
|
+
* there is nothing extra to licence, so the state is floor and the token
|
|
15
|
+
* is simply recorded.
|
|
16
|
+
* - Past expiry the install behaves as `extended`, never worse. Expiry
|
|
17
|
+
* removes commercial rights; it does not take away code the user already
|
|
18
|
+
* has, and it does not switch anything off.
|
|
19
|
+
*
|
|
20
|
+
* `endpoint` is an opaque string. The server records only *whether* one is
|
|
21
|
+
* configured and passes it through untouched; it never parses it, connects to
|
|
22
|
+
* it, or learns what is on the other end. That is the module's business.
|
|
23
|
+
*
|
|
24
|
+
* Computed once per process and memoized, because the HTTP host builds a
|
|
25
|
+
* fresh server per request (stateless transport) and so has no startup of its
|
|
26
|
+
* own to compute this at. Recomputing per request would re-read the
|
|
27
|
+
* environment and the config file on every call and could report two
|
|
28
|
+
* different states inside one process.
|
|
29
|
+
*/
|
|
30
|
+
import { readFileSync } from 'node:fs';
|
|
31
|
+
import { homedir } from 'node:os';
|
|
32
|
+
import { join } from 'node:path';
|
|
33
|
+
import { verifyLicence, parsePublicKeyEnv } from './licence.js';
|
|
34
|
+
/** Environment variables this module reads. Nothing else is consulted. */
|
|
35
|
+
export const ENV_ENDPOINT = 'COGNIUM_ENDPOINT';
|
|
36
|
+
export const ENV_LICENSE = 'COGNIUM_LICENSE';
|
|
37
|
+
export const ENV_LICENSE_PUBKEY = 'COGNIUM_LICENSE_PUBKEY';
|
|
38
|
+
export const ENV_CONFIG_DIR = 'COGNIUM_CONFIG_DIR';
|
|
39
|
+
/** Where `cognium license set` writes, when no config dir is given. */
|
|
40
|
+
export function configDir(env = process.env) {
|
|
41
|
+
return env[ENV_CONFIG_DIR] ?? join(homedir(), '.cognium');
|
|
42
|
+
}
|
|
43
|
+
function tokenFromDisk(env) {
|
|
44
|
+
try {
|
|
45
|
+
const text = readFileSync(join(configDir(env), 'license'), 'utf8').trim();
|
|
46
|
+
return text === '' ? undefined : text;
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
// No file, no permission, a directory where a file should be — all mean
|
|
50
|
+
// the same thing to us: no token on disk.
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** Compute enablement from the environment and the user config dir. */
|
|
55
|
+
export function computeEnablement(input) {
|
|
56
|
+
const env = input.env ?? process.env;
|
|
57
|
+
const rawEndpoint = env[ENV_ENDPOINT]?.trim();
|
|
58
|
+
const endpoint = rawEndpoint === '' ? undefined : rawEndpoint;
|
|
59
|
+
const token = env[ENV_LICENSE]?.trim() || tokenFromDisk(env);
|
|
60
|
+
const licence = verifyLicence(token, {
|
|
61
|
+
publicKeys: parsePublicKeyEnv(env[ENV_LICENSE_PUBKEY]),
|
|
62
|
+
...(input.now ? { now: input.now } : {}),
|
|
63
|
+
});
|
|
64
|
+
let state = 'floor';
|
|
65
|
+
if (input.installed) {
|
|
66
|
+
state = licence.status === 'valid' ? 'commercial' : 'extended';
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
installed: input.installed,
|
|
70
|
+
...(endpoint ? { endpoint } : {}),
|
|
71
|
+
licence,
|
|
72
|
+
state,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
let memoized;
|
|
76
|
+
/**
|
|
77
|
+
* The process-wide enablement. First call computes it; every later call —
|
|
78
|
+
* including every request-scoped server the HTTP host builds — gets the same
|
|
79
|
+
* object back.
|
|
80
|
+
*/
|
|
81
|
+
export function enablementForProcess(input) {
|
|
82
|
+
memoized ??= computeEnablement(input);
|
|
83
|
+
return memoized;
|
|
84
|
+
}
|
|
85
|
+
/** Drop the memo. For tests; nothing in the server calls this. */
|
|
86
|
+
export function resetEnablementForTests() {
|
|
87
|
+
memoized = undefined;
|
|
88
|
+
}
|
|
89
|
+
/** The floor state, for a server built with no modules at all. */
|
|
90
|
+
export function floorEnablement() {
|
|
91
|
+
return {
|
|
92
|
+
installed: false,
|
|
93
|
+
licence: { status: 'absent', detail: 'no licence token configured' },
|
|
94
|
+
state: 'floor',
|
|
95
|
+
};
|
|
96
|
+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EAAkE;AAClE,MAAM,UAAU,eAAe;IAC7B,OAAO;QACL,SAAS,EAAE,KAAK;QAChB,OAAO,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,6BAA6B,EAAE;QACpE,KAAK,EAAE,OAAO;KACf,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
1
|
/**
|
|
3
|
-
*
|
|
4
|
-
* server to stdio transport — the transport every MCP client (Claude
|
|
5
|
-
* Desktop, Claude Code, Cursor) speaks by default.
|
|
2
|
+
* The library entry for `@cognium/mcp-server`.
|
|
6
3
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
4
|
+
* Side-effect free by design: importing this package builds nothing, reads
|
|
5
|
+
* nothing and connects nothing. A host that wants a server calls
|
|
6
|
+
* `createServer()` (floor plus whatever optional modules are installed) or
|
|
7
|
+
* `buildServer()` (exactly what it passes in). The stdio binary is `bin.ts`.
|
|
10
8
|
*/
|
|
11
|
-
export {};
|
|
9
|
+
export { buildServer, createServer, discoverModules, resetDiscoveryForTests, SERVER_NAME, SERVER_VERSION, type BuildServerOptions, type Discovery, } from './server.js';
|
|
10
|
+
export { startupLines } from './startup.js';
|
|
11
|
+
export { loadModules, moduleSpecifiers, circleIrVersion, checkCircleIrRange, majorMinor, isToolModule, DEFAULT_MODULE_SPECIFIERS, ENV_MODULES, type ToolModule, type ModuleRefusal, type LoadModulesOptions, type LoadModulesResult, } from './modules.js';
|
|
12
|
+
export { computeEnablement, enablementForProcess, resetEnablementForTests, floorEnablement, configDir, ENV_ENDPOINT, ENV_LICENSE, ENV_LICENSE_PUBKEY, ENV_CONFIG_DIR, type Enablement, type LicenceState, } from './enablement.js';
|
|
13
|
+
export { verifyLicence, encodeLicence, parsePublicKeyEnv, TOKEN_PREFIX, BUILTIN_PUBLIC_KEYS, ANY_KID, type LicencePayload, type LicenceStatus, type LicenceVerdict, type VerifyOptions, } from './licence.js';
|
|
14
|
+
export { canonicalise, canonicalBytes, JcsError } from './jcs.js';
|
|
15
|
+
export { ProjectCache, type ProjectScanOptions } from './cache.js';
|
|
16
|
+
export { type ToolContext, type ToolResult } from './tools/types.js';
|
|
12
17
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACL,WAAW,EACX,YAAY,EACZ,eAAe,EACf,sBAAsB,EACtB,WAAW,EACX,cAAc,EACd,KAAK,kBAAkB,EACvB,KAAK,SAAS,GACf,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,OAAO,EACL,WAAW,EACX,gBAAgB,EAChB,eAAe,EACf,kBAAkB,EAClB,UAAU,EACV,YAAY,EACZ,yBAAyB,EACzB,WAAW,EACX,KAAK,UAAU,EACf,KAAK,aAAa,EAClB,KAAK,kBAAkB,EACvB,KAAK,iBAAiB,GACvB,MAAM,cAAc,CAAC;AAEtB,OAAO,EACL,iBAAiB,EACjB,oBAAoB,EACpB,uBAAuB,EACvB,eAAe,EACf,SAAS,EACT,YAAY,EACZ,WAAW,EACX,kBAAkB,EAClB,cAAc,EACd,KAAK,UAAU,EACf,KAAK,YAAY,GAClB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACL,aAAa,EACb,aAAa,EACb,iBAAiB,EACjB,YAAY,EACZ,mBAAmB,EACnB,OAAO,EACP,KAAK,cAAc,EACnB,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,KAAK,aAAa,GACnB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAElE,OAAO,EAAE,YAAY,EAAE,KAAK,kBAAkB,EAAE,MAAM,YAAY,CAAC;AACnE,OAAO,EAAE,KAAK,WAAW,EAAE,KAAK,UAAU,EAAE,MAAM,kBAAkB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,24 +1,16 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
1
|
/**
|
|
3
|
-
*
|
|
4
|
-
* server to stdio transport — the transport every MCP client (Claude
|
|
5
|
-
* Desktop, Claude Code, Cursor) speaks by default.
|
|
2
|
+
* The library entry for `@cognium/mcp-server`.
|
|
6
3
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
4
|
+
* Side-effect free by design: importing this package builds nothing, reads
|
|
5
|
+
* nothing and connects nothing. A host that wants a server calls
|
|
6
|
+
* `createServer()` (floor plus whatever optional modules are installed) or
|
|
7
|
+
* `buildServer()` (exactly what it passes in). The stdio binary is `bin.ts`.
|
|
10
8
|
*/
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
main().catch((err) => {
|
|
19
|
-
// stderr is safe for stdio-transport servers (stdout is reserved for
|
|
20
|
-
// MCP messages).
|
|
21
|
-
process.stderr.write(`[mcp-server-cognium-dev] fatal: ${err.stack ?? err}\n`);
|
|
22
|
-
process.exit(1);
|
|
23
|
-
});
|
|
9
|
+
export { buildServer, createServer, discoverModules, resetDiscoveryForTests, SERVER_NAME, SERVER_VERSION, } from './server.js';
|
|
10
|
+
export { startupLines } from './startup.js';
|
|
11
|
+
export { loadModules, moduleSpecifiers, circleIrVersion, checkCircleIrRange, majorMinor, isToolModule, DEFAULT_MODULE_SPECIFIERS, ENV_MODULES, } from './modules.js';
|
|
12
|
+
export { computeEnablement, enablementForProcess, resetEnablementForTests, floorEnablement, configDir, ENV_ENDPOINT, ENV_LICENSE, ENV_LICENSE_PUBKEY, ENV_CONFIG_DIR, } from './enablement.js';
|
|
13
|
+
export { verifyLicence, encodeLicence, parsePublicKeyEnv, TOKEN_PREFIX, BUILTIN_PUBLIC_KEYS, ANY_KID, } from './licence.js';
|
|
14
|
+
export { canonicalise, canonicalBytes, JcsError } from './jcs.js';
|
|
15
|
+
export { ProjectCache } from './cache.js';
|
|
24
16
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACL,WAAW,EACX,YAAY,EACZ,eAAe,EACf,sBAAsB,EACtB,WAAW,EACX,cAAc,GAGf,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,OAAO,EACL,WAAW,EACX,gBAAgB,EAChB,eAAe,EACf,kBAAkB,EAClB,UAAU,EACV,YAAY,EACZ,yBAAyB,EACzB,WAAW,GAKZ,MAAM,cAAc,CAAC;AAEtB,OAAO,EACL,iBAAiB,EACjB,oBAAoB,EACpB,uBAAuB,EACvB,eAAe,EACf,SAAS,EACT,YAAY,EACZ,WAAW,EACX,kBAAkB,EAClB,cAAc,GAGf,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACL,aAAa,EACb,aAAa,EACb,iBAAiB,EACjB,YAAY,EACZ,mBAAmB,EACnB,OAAO,GAKR,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAElE,OAAO,EAAE,YAAY,EAA2B,MAAM,YAAY,CAAC"}
|
package/dist/jcs.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON Canonicalization Scheme (RFC 8785).
|
|
3
|
+
*
|
|
4
|
+
* One byte sequence per JSON value, so a signature over a payload survives
|
|
5
|
+
* being re-encoded by anything that handles it on the way. The licence token
|
|
6
|
+
* signs the canonical form rather than the bytes it happened to arrive in.
|
|
7
|
+
*
|
|
8
|
+
* Implemented here rather than taken as a dependency: it is forty lines, and
|
|
9
|
+
* this package keeps its dependency list short on purpose.
|
|
10
|
+
*
|
|
11
|
+
* Two pieces come free from the platform and are worth stating so nobody
|
|
12
|
+
* "fixes" them later:
|
|
13
|
+
*
|
|
14
|
+
* - `JSON.stringify` on a string already produces JCS-conformant escaping.
|
|
15
|
+
* It escapes only `"`, `\` and the C0 controls, uses the short forms
|
|
16
|
+
* (`\b \t \n \f \r`) where RFC 8785 requires them, `\uXXXX` for the rest,
|
|
17
|
+
* leaves every other code point as literal UTF-8, and escapes lone
|
|
18
|
+
* surrogates — all exactly as the RFC specifies.
|
|
19
|
+
* - `Array.prototype.sort()` with no comparator orders strings by UTF-16
|
|
20
|
+
* code unit, which is the ordering RFC 8785 §3.2.3 mandates for keys.
|
|
21
|
+
*/
|
|
22
|
+
/** Thrown for input JSON cannot represent canonically. */
|
|
23
|
+
export declare class JcsError extends Error {
|
|
24
|
+
}
|
|
25
|
+
export declare function canonicalise(value: unknown): string;
|
|
26
|
+
/** The canonical form as the bytes a signature covers. */
|
|
27
|
+
export declare function canonicalBytes(value: unknown): Buffer;
|
|
28
|
+
//# sourceMappingURL=jcs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jcs.d.ts","sourceRoot":"","sources":["../src/jcs.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,0DAA0D;AAC1D,qBAAa,QAAS,SAAQ,KAAK;CAAG;AAEtC,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAoCnD;AAED,0DAA0D;AAC1D,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAErD"}
|
package/dist/jcs.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON Canonicalization Scheme (RFC 8785).
|
|
3
|
+
*
|
|
4
|
+
* One byte sequence per JSON value, so a signature over a payload survives
|
|
5
|
+
* being re-encoded by anything that handles it on the way. The licence token
|
|
6
|
+
* signs the canonical form rather than the bytes it happened to arrive in.
|
|
7
|
+
*
|
|
8
|
+
* Implemented here rather than taken as a dependency: it is forty lines, and
|
|
9
|
+
* this package keeps its dependency list short on purpose.
|
|
10
|
+
*
|
|
11
|
+
* Two pieces come free from the platform and are worth stating so nobody
|
|
12
|
+
* "fixes" them later:
|
|
13
|
+
*
|
|
14
|
+
* - `JSON.stringify` on a string already produces JCS-conformant escaping.
|
|
15
|
+
* It escapes only `"`, `\` and the C0 controls, uses the short forms
|
|
16
|
+
* (`\b \t \n \f \r`) where RFC 8785 requires them, `\uXXXX` for the rest,
|
|
17
|
+
* leaves every other code point as literal UTF-8, and escapes lone
|
|
18
|
+
* surrogates — all exactly as the RFC specifies.
|
|
19
|
+
* - `Array.prototype.sort()` with no comparator orders strings by UTF-16
|
|
20
|
+
* code unit, which is the ordering RFC 8785 §3.2.3 mandates for keys.
|
|
21
|
+
*/
|
|
22
|
+
/** Thrown for input JSON cannot represent canonically. */
|
|
23
|
+
export class JcsError extends Error {
|
|
24
|
+
}
|
|
25
|
+
export function canonicalise(value) {
|
|
26
|
+
if (value === null)
|
|
27
|
+
return 'null';
|
|
28
|
+
switch (typeof value) {
|
|
29
|
+
case 'boolean':
|
|
30
|
+
return value ? 'true' : 'false';
|
|
31
|
+
case 'number':
|
|
32
|
+
// RFC 8785 §3.2.2.3 defers number serialization to ECMAScript's
|
|
33
|
+
// Number-to-String, which is what JSON.stringify applies. Non-finite
|
|
34
|
+
// values have no JSON form at all.
|
|
35
|
+
if (!Number.isFinite(value))
|
|
36
|
+
throw new JcsError(`cannot canonicalise ${String(value)}`);
|
|
37
|
+
return JSON.stringify(value);
|
|
38
|
+
case 'string':
|
|
39
|
+
return JSON.stringify(value);
|
|
40
|
+
case 'object':
|
|
41
|
+
break;
|
|
42
|
+
default:
|
|
43
|
+
throw new JcsError(`cannot canonicalise a ${typeof value}`);
|
|
44
|
+
}
|
|
45
|
+
if (Array.isArray(value)) {
|
|
46
|
+
return `[${value.map((v) => canonicalise(v)).join(',')}]`;
|
|
47
|
+
}
|
|
48
|
+
const entries = Object.entries(value)
|
|
49
|
+
// `undefined` is absent rather than null, matching JSON.stringify.
|
|
50
|
+
.filter(([, v]) => v !== undefined);
|
|
51
|
+
// Sort on the key as written: the default comparator is UTF-16 code unit
|
|
52
|
+
// order, which is what the RFC asks for.
|
|
53
|
+
entries.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
54
|
+
return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${canonicalise(v)}`).join(',')}}`;
|
|
55
|
+
}
|
|
56
|
+
/** The canonical form as the bytes a signature covers. */
|
|
57
|
+
export function canonicalBytes(value) {
|
|
58
|
+
return Buffer.from(canonicalise(value), 'utf8');
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=jcs.js.map
|
package/dist/jcs.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jcs.js","sourceRoot":"","sources":["../src/jcs.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,0DAA0D;AAC1D,MAAM,OAAO,QAAS,SAAQ,KAAK;CAAG;AAEtC,MAAM,UAAU,YAAY,CAAC,KAAc;IACzC,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,MAAM,CAAC;IAElC,QAAQ,OAAO,KAAK,EAAE,CAAC;QACrB,KAAK,SAAS;YACZ,OAAO,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC;QAElC,KAAK,QAAQ;YACX,gEAAgE;YAChE,qEAAqE;YACrE,mCAAmC;YACnC,IAAI,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