@stealthmark/express 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/NOTICE +4 -0
- package/README.md +90 -2
- package/dist/express.d.ts +22 -0
- package/dist/express.d.ts.map +1 -0
- package/dist/express.js +77 -0
- package/dist/express.js.map +1 -0
- package/dist/fastify.d.ts +22 -0
- package/dist/fastify.d.ts.map +1 -0
- package/dist/fastify.js +75 -0
- package/dist/fastify.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/meter.d.ts +15 -0
- package/dist/meter.d.ts.map +1 -0
- package/dist/meter.js +51 -0
- package/dist/meter.js.map +1 -0
- package/package.json +59 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,91 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @stealthmark/express
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Express and Fastify middleware that detects AI agents that identify themselves, serves an agent manifest at /.well-known/agent.json and meters agent traffic.
|
|
4
|
+
|
|
5
|
+
> Detects only agents that identify themselves. It does not detect stealth bots and is not a security control.
|
|
6
|
+
|
|
7
|
+
Part of the [StealthMark](https://stealthmark.io) SDK.
|
|
8
|
+
|
|
9
|
+
> **Status:** v0.1.0, not published to npm yet. The `npm install` and `npx` commands below will work once it is published; until then build from source as described in the [repository README](https://github.com/Gastonchevarria/stealthmark-sdk#status).
|
|
10
|
+
|
|
11
|
+
## Installation from source
|
|
12
|
+
|
|
13
|
+
Until the first npm release, build the repository and install the tarballs (`@stealthmark/express` depends on `@stealthmark/core`):
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
# in a clone of the repository
|
|
17
|
+
npm install && npm run build
|
|
18
|
+
npm pack -w @stealthmark/core -w @stealthmark/express
|
|
19
|
+
# in your project
|
|
20
|
+
npm install /path/to/stealthmark-core-0.1.0.tgz /path/to/stealthmark-express-0.1.0.tgz
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Installation (after the first npm release)
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install @stealthmark/express @stealthmark/core
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Requires Node.js 20 or newer.
|
|
30
|
+
|
|
31
|
+
## Express Quickstart
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import express from 'express';
|
|
35
|
+
import { stealthmark } from '@stealthmark/express';
|
|
36
|
+
|
|
37
|
+
const app = express();
|
|
38
|
+
|
|
39
|
+
// Add the middleware near the top of your middleware stack
|
|
40
|
+
app.use(stealthmark({
|
|
41
|
+
siteName: 'My Express API',
|
|
42
|
+
capabilities: [
|
|
43
|
+
{ id: 'search', endpoint: '/api/search', method: 'GET' },
|
|
44
|
+
{ id: 'checkout', endpoint: '/api/orders', method: 'POST', hitl: true },
|
|
45
|
+
],
|
|
46
|
+
}));
|
|
47
|
+
|
|
48
|
+
app.get('/api/search', (req, res) => res.json({ results: [] }));
|
|
49
|
+
|
|
50
|
+
app.listen(3000);
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
`hitl` is advisory metadata for agents. StealthMark does not block, approve or rate-limit anything; enforce approvals in your own handler.
|
|
54
|
+
|
|
55
|
+
## Fastify Quickstart
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import Fastify from 'fastify';
|
|
59
|
+
import { stealthmarkFastify } from '@stealthmark/express';
|
|
60
|
+
|
|
61
|
+
const fastify = Fastify();
|
|
62
|
+
|
|
63
|
+
fastify.addHook('preHandler', stealthmarkFastify({
|
|
64
|
+
siteName: 'My Fastify Service',
|
|
65
|
+
}));
|
|
66
|
+
|
|
67
|
+
fastify.listen({ port: 3000 });
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Features
|
|
71
|
+
|
|
72
|
+
- **Agent detection**: detects agents that identify themselves: known AI crawler tokens in the `User-Agent` (GPTBot, ClaudeBot, PerplexityBot, ...), the `x-stealthmark-agent` header and `Accept: application/agent+json`. All of these are self-declared.
|
|
73
|
+
- **Agent manifest**: `/.well-known/agent.json` and any request with `Accept: application/agent+json` get the manifest. The default policy is `observe`: pages answer normally. With `agentPolicy: 'manifest'` detected agents get the manifest on `/` only. The middleware also answers `/.well-known/ai-plugin.json` and `/api/agent-manifest` with the manifest for every visitor, so do not use those paths for your own routes.
|
|
74
|
+
- **Next.js route discovery**: `discoverCapabilities()` lists App Router `route.ts` handlers under `app/api`. It does not scan Express or Fastify routes; declare those in `capabilities`. It needs Node (`fs`).
|
|
75
|
+
- **Usage metering (optional)**: provide `apiKey` (or `STEALTHMARK_API_KEY`) to report requests from self-identified agents as usage events to the StealthMark hosted API. Without a key the SDK makes no network calls. The default host is https://stealthmark-api-cifepjj5ca-uc.a.run.app; override it with `usageEndpoint` or `STEALTHMARK_USAGE_ENDPOINT`. Events are reported best-effort and can be dropped.
|
|
76
|
+
- **Per-site attribution**: every metered event carries `metadata.host` (the `Host` header, lowercased, no port), and the console groups and filters by it with no config. Set `trustForwardedHost: true` to use the first `X-Forwarded-Host` instead, only behind a proxy you control.
|
|
77
|
+
- **Optional `siteId`** (or `STEALTHMARK_SITE_ID`): stored on the event as `site_id`. It must be the UUID of a site registered to the same organization, otherwise it is stored as null.
|
|
78
|
+
|
|
79
|
+
## agentPolicy
|
|
80
|
+
|
|
81
|
+
`agentPolicy` sets how requests from detected agents are handled on ordinary paths:
|
|
82
|
+
|
|
83
|
+
- `'observe'` (default): the request passes through to your handlers. The response gets `x-stealthmark-agent-detected` and, when known, `x-stealthmark-agent-id`.
|
|
84
|
+
- `'manifest'`: detected agents get the manifest instead of the page, on `/` only. Other paths pass through as in `observe`. (`@stealthmark/next` serves the manifest on every path in this mode.)
|
|
85
|
+
- `'block'`: detected agents get a 403 with a JSON body. Manifest paths and `Accept: application/agent+json` requests are still answered with the manifest.
|
|
86
|
+
|
|
87
|
+
A manifest served on `/` and the 403 are chosen from the request headers, so they are sent with `Cache-Control: private, no-store` and a `Vary` header naming `Accept`, `User-Agent` and `x-stealthmark-agent` (merged with any `Vary` your app already set). The fixed manifest paths set no cache headers.
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
Apache 2.0, see [LICENSE](./LICENSE) and [NOTICE](./NOTICE).
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type StealthMarkConfig } from '@stealthmark/core';
|
|
2
|
+
export interface ExpressRequestLike {
|
|
3
|
+
method?: string;
|
|
4
|
+
url?: string;
|
|
5
|
+
path?: string;
|
|
6
|
+
secure?: boolean;
|
|
7
|
+
headers: Record<string, string | string[] | undefined>;
|
|
8
|
+
}
|
|
9
|
+
export interface ExpressResponseLike {
|
|
10
|
+
setHeader(name: string, value: string): void;
|
|
11
|
+
/** Present on real Express responses; used to keep a Vary header that earlier middleware already set. */
|
|
12
|
+
getHeader?(name: string): unknown;
|
|
13
|
+
status(code: number): ExpressResponseLike;
|
|
14
|
+
send(body: string): void;
|
|
15
|
+
}
|
|
16
|
+
export type ExpressNextFunction = (err?: unknown) => void;
|
|
17
|
+
/**
|
|
18
|
+
* Creates an Express-compatible middleware that serves the agent manifest, applies `agentPolicy`
|
|
19
|
+
* and meters requests from agents that identify themselves.
|
|
20
|
+
*/
|
|
21
|
+
export declare function stealthmarkExpress(config: StealthMarkConfig): (req: ExpressRequestLike, res: ExpressResponseLike, next: ExpressNextFunction) => void;
|
|
22
|
+
//# sourceMappingURL=express.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"express.d.ts","sourceRoot":"","sources":["../src/express.ts"],"names":[],"mappings":"AAKA,OAAO,EAAqB,KAAK,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAG9E,MAAM,WAAW,kBAAkB;IACjC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC,CAAC;CACxD;AAED,MAAM,WAAW,mBAAmB;IAClC,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7C,yGAAyG;IACzG,SAAS,CAAC,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;IAClC,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,mBAAmB,CAAC;IAC1C,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AAED,MAAM,MAAM,mBAAmB,GAAG,CAAC,GAAG,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;AAS1D;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,iBAAiB,IAIxD,KAAK,kBAAkB,EACvB,KAAK,mBAAmB,EACxB,MAAM,mBAAmB,KACxB,IAAI,CAqER"}
|
package/dist/express.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
2
|
+
// @stealthmark/express — Express Middleware Handler
|
|
3
|
+
// Detects self-identified AI agents, serves the agent manifest and meters agent traffic for Express apps
|
|
4
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
5
|
+
import { createStealthMark } from '@stealthmark/core';
|
|
6
|
+
import { NEGOTIATED_CACHE_CONTROL, isManifestPath, meterAgentRequest, meterBlockedRequest, negotiatedVary } from './meter.js';
|
|
7
|
+
const BLOCKED_BODY = { error: 'agent_blocked', message: 'This site does not accept automated agent traffic.' };
|
|
8
|
+
function markNegotiated(res) {
|
|
9
|
+
res.setHeader('Vary', negotiatedVary(res.getHeader?.('Vary')));
|
|
10
|
+
res.setHeader('Cache-Control', NEGOTIATED_CACHE_CONTROL);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Creates an Express-compatible middleware that serves the agent manifest, applies `agentPolicy`
|
|
14
|
+
* and meters requests from agents that identify themselves.
|
|
15
|
+
*/
|
|
16
|
+
export function stealthmarkExpress(config) {
|
|
17
|
+
const sm = createStealthMark(config);
|
|
18
|
+
return function stealthmarkMiddleware(req, res, next) {
|
|
19
|
+
const rawPath = req.path || req.url || '/';
|
|
20
|
+
const pathOnly = rawPath.split('?')[0];
|
|
21
|
+
const headersRecord = {};
|
|
22
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
23
|
+
if (value !== undefined) {
|
|
24
|
+
headersRecord[key.toLowerCase()] = Array.isArray(value) ? value[0] : value;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const agentDetection = sm.detect(headersRecord);
|
|
28
|
+
const isWellKnown = isManifestPath(pathOnly);
|
|
29
|
+
const isRoot = pathOnly === '/' || pathOnly === '';
|
|
30
|
+
const acceptsAgentJson = (headersRecord['accept'] ?? '').includes('application/agent+json');
|
|
31
|
+
const servesManifest = isWellKnown || (agentDetection.isAgent && (acceptsAgentJson || (isRoot && sm.agentPolicy === 'manifest')));
|
|
32
|
+
const blocked = agentDetection.isAgent && !servesManifest && sm.agentPolicy === 'block';
|
|
33
|
+
if (blocked) {
|
|
34
|
+
meterBlockedRequest(sm, headersRecord, agentDetection, pathOnly, req.method);
|
|
35
|
+
for (const [k, v] of Object.entries(sm.responseHeaders)) {
|
|
36
|
+
res.setHeader(k, v);
|
|
37
|
+
}
|
|
38
|
+
markNegotiated(res);
|
|
39
|
+
res.setHeader('Content-Type', 'application/json; charset=utf-8');
|
|
40
|
+
res.status(403).send(JSON.stringify(BLOCKED_BODY));
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
meterAgentRequest(sm, headersRecord, agentDetection, pathOnly, req.method);
|
|
44
|
+
// 1. Intercept well-known paths or agent requests on root
|
|
45
|
+
if (servesManifest) {
|
|
46
|
+
res.setHeader('Content-Type', 'application/agent+json; charset=utf-8');
|
|
47
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
48
|
+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
|
49
|
+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, x-stealthmark-agent');
|
|
50
|
+
if (!isWellKnown)
|
|
51
|
+
markNegotiated(res);
|
|
52
|
+
for (const [k, v] of Object.entries(sm.responseHeaders)) {
|
|
53
|
+
res.setHeader(k, v);
|
|
54
|
+
}
|
|
55
|
+
if (agentDetection.isAgent) {
|
|
56
|
+
res.setHeader('x-stealthmark-agent-detected', 'true');
|
|
57
|
+
if (agentDetection.agentId) {
|
|
58
|
+
res.setHeader('x-stealthmark-agent-id', agentDetection.agentId);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
res.status(200).send(sm.manifestJson);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
// 2. Non-manifest traffic: add the x-stealthmark-* marker headers and pass through
|
|
65
|
+
for (const [k, v] of Object.entries(sm.responseHeaders)) {
|
|
66
|
+
res.setHeader(k, v);
|
|
67
|
+
}
|
|
68
|
+
if (agentDetection.isAgent) {
|
|
69
|
+
res.setHeader('x-stealthmark-agent-detected', 'true');
|
|
70
|
+
if (agentDetection.agentId) {
|
|
71
|
+
res.setHeader('x-stealthmark-agent-id', agentDetection.agentId);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
next();
|
|
75
|
+
};
|
|
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EAAE,CAAC,CAAC,CAAC;QACtB,CAAC;QAED,IAAI,cAAc,CAAC,OAAO,EAAE,CAAC;YAC3B,GAAG,CAAC,SAAS,CAAC,8BAA8B,EAAE,MAAM,CAAC,CAAC;YACtD,IAAI,cAAc,CAAC,OAAO,EAAE,CAAC;gBAC3B,GAAG,CAAC,SAAS,CAAC,wBAAwB,EAAE,cAAc,CAAC,OAAO,CAAC,CAAC;YAClE,CAAC;QACH,CAAC;QAED,IAAI,EAAE,CAAC;IACT,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type StealthMarkConfig } from '@stealthmark/core';
|
|
2
|
+
export interface FastifyRequestLike {
|
|
3
|
+
method?: string;
|
|
4
|
+
url?: string;
|
|
5
|
+
protocol?: string;
|
|
6
|
+
hostname?: string;
|
|
7
|
+
headers: Record<string, string | string[] | undefined>;
|
|
8
|
+
}
|
|
9
|
+
export interface FastifyReplyLike {
|
|
10
|
+
header(name: string, value: string): FastifyReplyLike;
|
|
11
|
+
/** Present on real Fastify replies; used to keep a Vary header that earlier hooks already set. */
|
|
12
|
+
getHeader?(name: string): unknown;
|
|
13
|
+
code(statusCode: number): FastifyReplyLike;
|
|
14
|
+
send(payload: unknown): FastifyReplyLike;
|
|
15
|
+
}
|
|
16
|
+
export type FastifyHookDone = (err?: Error) => void;
|
|
17
|
+
/**
|
|
18
|
+
* Creates a Fastify preHandler hook that serves the agent manifest, applies `agentPolicy`
|
|
19
|
+
* and meters requests from agents that identify themselves.
|
|
20
|
+
*/
|
|
21
|
+
export declare function stealthmarkFastify(config: StealthMarkConfig): (request: FastifyRequestLike, reply: FastifyReplyLike, done: FastifyHookDone) => void;
|
|
22
|
+
//# sourceMappingURL=fastify.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fastify.d.ts","sourceRoot":"","sources":["../src/fastify.ts"],"names":[],"mappings":"AAKA,OAAO,EAAqB,KAAK,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAG9E,MAAM,WAAW,kBAAkB;IACjC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC,CAAC;CACxD;AAED,MAAM,WAAW,gBAAgB;IAC/B,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,gBAAgB,CAAC;IACtD,kGAAkG;IAClG,SAAS,CAAC,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;IAClC,IAAI,CAAC,UAAU,EAAE,MAAM,GAAG,gBAAgB,CAAC;IAC3C,IAAI,CAAC,OAAO,EAAE,OAAO,GAAG,gBAAgB,CAAC;CAC1C;AAED,MAAM,MAAM,eAAe,GAAG,CAAC,GAAG,CAAC,EAAE,KAAK,KAAK,IAAI,CAAC;AASpD;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,iBAAiB,IAIxD,SAAS,kBAAkB,EAC3B,OAAO,gBAAgB,EACvB,MAAM,eAAe,KACpB,IAAI,CAmER"}
|
package/dist/fastify.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
2
|
+
// @stealthmark/express — Fastify Hook Plugin
|
|
3
|
+
// Detects self-identified AI agents, serves the agent manifest and meters agent traffic for Fastify apps
|
|
4
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
5
|
+
import { createStealthMark } from '@stealthmark/core';
|
|
6
|
+
import { NEGOTIATED_CACHE_CONTROL, isManifestPath, meterAgentRequest, meterBlockedRequest, negotiatedVary } from './meter.js';
|
|
7
|
+
const BLOCKED_BODY = { error: 'agent_blocked', message: 'This site does not accept automated agent traffic.' };
|
|
8
|
+
function markNegotiated(reply) {
|
|
9
|
+
reply.header('Vary', negotiatedVary(reply.getHeader?.('Vary')));
|
|
10
|
+
reply.header('Cache-Control', NEGOTIATED_CACHE_CONTROL);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Creates a Fastify preHandler hook that serves the agent manifest, applies `agentPolicy`
|
|
14
|
+
* and meters requests from agents that identify themselves.
|
|
15
|
+
*/
|
|
16
|
+
export function stealthmarkFastify(config) {
|
|
17
|
+
const sm = createStealthMark(config);
|
|
18
|
+
return function stealthmarkPreHandler(request, reply, done) {
|
|
19
|
+
const rawPath = request.url || '/';
|
|
20
|
+
const pathOnly = rawPath.split('?')[0];
|
|
21
|
+
const headersRecord = {};
|
|
22
|
+
for (const [key, value] of Object.entries(request.headers)) {
|
|
23
|
+
if (value !== undefined) {
|
|
24
|
+
headersRecord[key.toLowerCase()] = Array.isArray(value) ? value[0] : value;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const agentDetection = sm.detect(headersRecord);
|
|
28
|
+
const isWellKnown = isManifestPath(pathOnly);
|
|
29
|
+
const isRoot = pathOnly === '/' || pathOnly === '';
|
|
30
|
+
const acceptsAgentJson = (headersRecord['accept'] ?? '').includes('application/agent+json');
|
|
31
|
+
const servesManifest = isWellKnown || (agentDetection.isAgent && (acceptsAgentJson || (isRoot && sm.agentPolicy === 'manifest')));
|
|
32
|
+
const blocked = agentDetection.isAgent && !servesManifest && sm.agentPolicy === 'block';
|
|
33
|
+
if (blocked) {
|
|
34
|
+
meterBlockedRequest(sm, headersRecord, agentDetection, pathOnly, request.method);
|
|
35
|
+
for (const [k, v] of Object.entries(sm.responseHeaders)) {
|
|
36
|
+
reply.header(k, v);
|
|
37
|
+
}
|
|
38
|
+
markNegotiated(reply);
|
|
39
|
+
reply.code(403).send(BLOCKED_BODY);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
meterAgentRequest(sm, headersRecord, agentDetection, pathOnly, request.method);
|
|
43
|
+
if (servesManifest) {
|
|
44
|
+
reply
|
|
45
|
+
.header('Content-Type', 'application/agent+json; charset=utf-8')
|
|
46
|
+
.header('Access-Control-Allow-Origin', '*')
|
|
47
|
+
.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
|
|
48
|
+
.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, x-stealthmark-agent');
|
|
49
|
+
if (!isWellKnown)
|
|
50
|
+
markNegotiated(reply);
|
|
51
|
+
for (const [k, v] of Object.entries(sm.responseHeaders)) {
|
|
52
|
+
reply.header(k, v);
|
|
53
|
+
}
|
|
54
|
+
if (agentDetection.isAgent) {
|
|
55
|
+
reply.header('x-stealthmark-agent-detected', 'true');
|
|
56
|
+
if (agentDetection.agentId) {
|
|
57
|
+
reply.header('x-stealthmark-agent-id', agentDetection.agentId);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
reply.code(200).send(sm.manifest);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
for (const [k, v] of Object.entries(sm.responseHeaders)) {
|
|
64
|
+
reply.header(k, v);
|
|
65
|
+
}
|
|
66
|
+
if (agentDetection.isAgent) {
|
|
67
|
+
reply.header('x-stealthmark-agent-detected', 'true');
|
|
68
|
+
if (agentDetection.agentId) {
|
|
69
|
+
reply.header('x-stealthmark-agent-id', agentDetection.agentId);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
done();
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=fastify.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fastify.js","sourceRoot":"","sources":["../src/fastify.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,6CAA6C;AAC7C,yGAAyG;AACzG,sEAAsE;AAEtE,OAAO,EAAE,iBAAiB,EAA0B,MAAM,mBAAmB,CAAC;AAC9E,OAAO,EAAE,wBAAwB,EAAE,cAAc,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,cAAc,EAAE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AC;YAExC,KAAK,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC,eAAe,CAAC,EAAE,CAAC;gBACxD,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;YACrB,CAAC;YAED,IAAI,cAAc,CAAC,OAAO,EAAE,CAAC;gBAC3B,KAAK,CAAC,MAAM,CAAC,8BAA8B,EAAE,MAAM,CAAC,CAAC;gBACrD,IAAI,cAAc,CAAC,OAAO,EAAE,CAAC;oBAC3B,KAAK,CAAC,MAAM,CAAC,wBAAwB,EAAE,cAAc,CAAC,OAAO,CAAC,CAAC;gBACjE,CAAC;YACH,CAAC;YAED,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC;YAClC,OAAO;QACT,CAAC;QAED,KAAK,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC,eAAe,CAAC,EAAE,CAAC;YACxD,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QACrB,CAAC;QAED,IAAI,cAAc,CAAC,OAAO,EAAE,CAAC;YAC3B,KAAK,CAAC,MAAM,CAAC,8BAA8B,EAAE,MAAM,CAAC,CAAC;YACrD,IAAI,cAAc,CAAC,OAAO,EAAE,CAAC;gBAC3B,KAAK,CAAC,MAAM,CAAC,wBAAwB,EAAE,cAAc,CAAC,OAAO,CAAC,CAAC;YACjE,CAAC;QACH,CAAC;QAED,IAAI,EAAE,CAAC;IACT,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { stealthmarkExpress, stealthmarkExpress as stealthmark } from './express.js';
|
|
2
|
+
export { stealthmarkFastify } from './fastify.js';
|
|
3
|
+
export { discoverCapabilities } from '@stealthmark/core';
|
|
4
|
+
export type { StealthMarkConfig, Capability, HttpMethod, AgentManifest, DetectionResult, RouteDiscoveryOptions, } from '@stealthmark/core';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,IAAI,WAAW,EAAE,MAAM,cAAc,CAAC;AACrF,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAElD,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAGzD,YAAY,EACV,iBAAiB,EACjB,UAAU,EACV,UAAU,EACV,aAAa,EACb,eAAe,EACf,qBAAqB,GACtB,MAAM,mBAAmB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
2
|
+
// @stealthmark/express — Public API
|
|
3
|
+
// Express and Fastify middleware: agent detection, agent manifest and usage metering
|
|
4
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
5
|
+
export { stealthmarkExpress, stealthmarkExpress as stealthmark } from './express.js';
|
|
6
|
+
export { stealthmarkFastify } from './fastify.js';
|
|
7
|
+
export { discoverCapabilities } from '@stealthmark/core';
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,oCAAoC;AACpC,qFAAqF;AACrF,sEAAsE;AAEtE,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,IAAI,WAAW,EAAE,MAAM,cAAc,CAAC;AACrF,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAElD,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC"}
|
package/dist/meter.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { DetectionResult, StealthMarkInstance } from '@stealthmark/core';
|
|
2
|
+
export declare function isManifestPath(pathOnly: string): boolean;
|
|
3
|
+
/** A response chosen from the request headers (manifest on an ordinary path, or the block 403) is never stored as the answer for that URL. */
|
|
4
|
+
export declare const NEGOTIATED_CACHE_CONTROL = "private, no-store";
|
|
5
|
+
/** Vary value that names the headers that decided the response, keeping whatever earlier middleware already put in Vary. */
|
|
6
|
+
export declare function negotiatedVary(existing: unknown): string;
|
|
7
|
+
/**
|
|
8
|
+
* Meters exactly one event for a request identified as an AI agent.
|
|
9
|
+
* Requests that do not identify themselves as agents are never recorded. The event carries the request host so the console can
|
|
10
|
+
* tell sites apart, and the site_id when the instance has one.
|
|
11
|
+
*/
|
|
12
|
+
export declare function meterAgentRequest(sm: StealthMarkInstance, headers: Record<string, string>, detection: DetectionResult, pathOnly: string, method: string | undefined): void;
|
|
13
|
+
/** Meters one agent request that the `block` policy answered with 403. */
|
|
14
|
+
export declare function meterBlockedRequest(sm: StealthMarkInstance, headers: Record<string, string>, detection: DetectionResult, pathOnly: string, method: string | undefined): void;
|
|
15
|
+
//# sourceMappingURL=meter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"meter.d.ts","sourceRoot":"","sources":["../src/meter.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,eAAe,EAAE,mBAAmB,EAAkB,MAAM,mBAAmB,CAAC;AAE9F,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAMxD;AAED,8IAA8I;AAC9I,eAAO,MAAM,wBAAwB,sBAAsB,CAAC;AAI5D,4HAA4H;AAC5H,wBAAgB,cAAc,CAAC,QAAQ,EAAE,OAAO,GAAG,MAAM,CASxD;AAqBD;;;;GAIG;AACH,wBAAgB,iBAAiB,CAC/B,EAAE,EAAE,mBAAmB,EACvB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC/B,SAAS,EAAE,eAAe,EAC1B,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,GAAG,SAAS,GACzB,IAAI,CAGN;AAED,0EAA0E;AAC1E,wBAAgB,mBAAmB,CACjC,EAAE,EAAE,mBAAmB,EACvB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC/B,SAAS,EAAE,eAAe,EAC1B,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,GAAG,SAAS,GACzB,IAAI,CAEN"}
|
package/dist/meter.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
2
|
+
// @stealthmark/express — Usage metering shared by the Express and Fastify adapters
|
|
3
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
4
|
+
import { buildEventMetadata, resolveAgentIdentifier, resolveRequestHost } from '@stealthmark/core';
|
|
5
|
+
export function isManifestPath(pathOnly) {
|
|
6
|
+
return (pathOnly === '/.well-known/agent.json' ||
|
|
7
|
+
pathOnly === '/.well-known/ai-plugin.json' ||
|
|
8
|
+
pathOnly === '/api/agent-manifest');
|
|
9
|
+
}
|
|
10
|
+
/** A response chosen from the request headers (manifest on an ordinary path, or the block 403) is never stored as the answer for that URL. */
|
|
11
|
+
export const NEGOTIATED_CACHE_CONTROL = 'private, no-store';
|
|
12
|
+
const NEGOTIATED_VARY_HEADERS = ['Accept', 'User-Agent', 'x-stealthmark-agent'];
|
|
13
|
+
/** Vary value that names the headers that decided the response, keeping whatever earlier middleware already put in Vary. */
|
|
14
|
+
export function negotiatedVary(existing) {
|
|
15
|
+
const current = Array.isArray(existing) ? existing.join(',') : typeof existing === 'string' ? existing : '';
|
|
16
|
+
const tokens = current.split(',').map((token) => token.trim()).filter((token) => token.length > 0);
|
|
17
|
+
if (tokens.includes('*'))
|
|
18
|
+
return '*';
|
|
19
|
+
const present = new Set(tokens.map((token) => token.toLowerCase()));
|
|
20
|
+
for (const name of NEGOTIATED_VARY_HEADERS) {
|
|
21
|
+
if (!present.has(name.toLowerCase()))
|
|
22
|
+
tokens.push(name);
|
|
23
|
+
}
|
|
24
|
+
return tokens.join(', ');
|
|
25
|
+
}
|
|
26
|
+
function recordEvent(sm, eventType, headers, detection, pathOnly, method) {
|
|
27
|
+
sm.recordAgentEvent({
|
|
28
|
+
event_type: eventType,
|
|
29
|
+
agent_identifier: resolveAgentIdentifier(headers, detection),
|
|
30
|
+
metadata: buildEventMetadata({
|
|
31
|
+
path: pathOnly,
|
|
32
|
+
method: method ?? 'GET',
|
|
33
|
+
host: resolveRequestHost(headers, { trustForwardedHost: sm.trustForwardedHost }),
|
|
34
|
+
}),
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Meters exactly one event for a request identified as an AI agent.
|
|
39
|
+
* Requests that do not identify themselves as agents are never recorded. The event carries the request host so the console can
|
|
40
|
+
* tell sites apart, and the site_id when the instance has one.
|
|
41
|
+
*/
|
|
42
|
+
export function meterAgentRequest(sm, headers, detection, pathOnly, method) {
|
|
43
|
+
if (!detection.isAgent)
|
|
44
|
+
return;
|
|
45
|
+
recordEvent(sm, isManifestPath(pathOnly) ? 'agent_manifest' : 'agent_request', headers, detection, pathOnly, method);
|
|
46
|
+
}
|
|
47
|
+
/** Meters one agent request that the `block` policy answered with 403. */
|
|
48
|
+
export function meterBlockedRequest(sm, headers, detection, pathOnly, method) {
|
|
49
|
+
recordEvent(sm, 'agent_blocked', headers, detection, pathOnly, method);
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=meter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"meter.js","sourceRoot":"","sources":["../src/meter.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,mFAAmF;AACnF,sEAAsE;AAEtE,OAAO,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAGnG,MAAM,UAAU,cAAc,CAAC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B,CAAC;AAED,SAAS,WAAW,CAClB,EAAuB,EACvB,SAAyB,EACzB,OAA+B,EAC/B,SAA0B,EAC1B,QAAgB,EAChB,MAA0B;IAE1B,EAAE,CAAC,gBAAgB,CAAC;QAClB,UAAU,EAAE,SAAS;QACrB,gBAAgB,EAAE,sBAAsB,CAAC,OAAO,EAAE,SAAS,CAAC;QAC5D,QAAQ,EAAE,kBAAkB,CAAC;YAC3B,IAAI,EAAE,QAAQ;YACd,MAAM,EAAE,MAAM,IAAI,KAAK;YACvB,IAAI,EAAE,kBAAkB,CAAC,OAAO,EAAE,EAAE,kBAAkB,EAAE,EAAE,CAAC,kBAAkB,EAAE,CAAC;SACjF,CAAC;KACH,CAAC,CAAC;AACL,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAC/B,EAAuB,EACvB,OAA+B,EAC/B,SAA0B,EAC1B,QAAgB,EAChB,MAA0B;IAE1B,IAAI,CAAC,SAAS,CAAC,OAAO;QAAE,OAAO;IAC/B,WAAW,CAAC,EAAE,EAAE,cAAc,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,eAAe,EAAE,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;AACvH,CAAC;AAED,0EAA0E;AAC1E,MAAM,UAAU,mBAAmB,CACjC,EAAuB,EACvB,OAA+B,EAC/B,SAA0B,EAC1B,QAAgB,EAChB,MAA0B;IAE1B,WAAW,CAAC,EAAE,EAAE,eAAe,EAAE,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;AACzE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,61 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stealthmark/express",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Express and Fastify middleware that detects self-identified AI agents, serves an agent manifest and meters agent traffic",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE",
|
|
20
|
+
"NOTICE"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsc --project tsconfig.build.json",
|
|
24
|
+
"dev": "tsc --project tsconfig.build.json --watch",
|
|
25
|
+
"test": "vitest run",
|
|
26
|
+
"typecheck": "tsc --noEmit",
|
|
27
|
+
"clean": "rm -rf dist tsconfig.build.tsbuildinfo"
|
|
28
|
+
},
|
|
29
|
+
"keywords": [
|
|
30
|
+
"express",
|
|
31
|
+
"fastify",
|
|
32
|
+
"middleware",
|
|
33
|
+
"ai-agents",
|
|
34
|
+
"agent-manifest",
|
|
35
|
+
"stealthmark"
|
|
36
|
+
],
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://github.com/Gastonchevarria/stealthmark-sdk.git",
|
|
40
|
+
"directory": "packages/express"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://stealthmark.io",
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": ">=20"
|
|
45
|
+
},
|
|
46
|
+
"author": "Gaston Chevarria <gaston_ch27@hotmail.com>",
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@stealthmark/core": "^0.1.0"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/express": "^5.0.0",
|
|
52
|
+
"typescript": "^5.7.0",
|
|
53
|
+
"vitest": "^4.1.11"
|
|
54
|
+
},
|
|
55
|
+
"bugs": {
|
|
56
|
+
"url": "https://github.com/Gastonchevarria/stealthmark-sdk/issues"
|
|
57
|
+
},
|
|
58
|
+
"publishConfig": {
|
|
59
|
+
"access": "public"
|
|
60
|
+
}
|
|
61
|
+
}
|