flare-redact 1.4.0 → 1.5.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 +114 -0
- package/README.md +115 -1
- package/SECURITY.md +36 -0
- package/bin/flare-gateway.mjs +6 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +12 -0
- package/dist/cli.js.map +1 -1
- package/dist/detectors.d.ts +17 -0
- package/dist/detectors.d.ts.map +1 -1
- package/dist/detectors.js.map +1 -1
- package/dist/engine.d.ts +5 -0
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +50 -7
- package/dist/engine.js.map +1 -1
- package/dist/gateway/body.d.ts +90 -0
- package/dist/gateway/body.d.ts.map +1 -0
- package/dist/gateway/body.js +185 -0
- package/dist/gateway/body.js.map +1 -0
- package/dist/gateway/cli.d.ts +26 -0
- package/dist/gateway/cli.d.ts.map +1 -0
- package/dist/gateway/cli.js +230 -0
- package/dist/gateway/cli.js.map +1 -0
- package/dist/gateway/config.d.ts +110 -0
- package/dist/gateway/config.d.ts.map +1 -0
- package/dist/gateway/config.js +212 -0
- package/dist/gateway/config.js.map +1 -0
- package/dist/gateway/index.d.ts +21 -0
- package/dist/gateway/index.d.ts.map +1 -0
- package/dist/gateway/index.js +21 -0
- package/dist/gateway/index.js.map +1 -0
- package/dist/gateway/runtime.d.ts +80 -0
- package/dist/gateway/runtime.d.ts.map +1 -0
- package/dist/gateway/runtime.js +0 -0
- package/dist/gateway/runtime.js.map +1 -0
- package/dist/gateway/server.d.ts +37 -0
- package/dist/gateway/server.d.ts.map +1 -0
- package/dist/gateway/server.js +560 -0
- package/dist/gateway/server.js.map +1 -0
- package/dist/pack.d.ts +88 -0
- package/dist/pack.d.ts.map +1 -0
- package/dist/pack.js +350 -0
- package/dist/pack.js.map +1 -0
- package/docker/Dockerfile +47 -0
- package/docker/README.md +219 -0
- package/docker/docker-compose.yml +55 -0
- package/docker/flare-gateway.example.json +48 -0
- package/package.json +28 -5
- package/spec/SPEC.md +469 -0
- package/spec/conformance/README.md +60 -0
- package/spec/conformance/cases.json +149 -0
- package/spec/conformance/expected.json +1683 -0
- package/spec/detectors.json +1812 -0
- package/spec/detectors.schema.json +196 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `flare-redact gateway` — run the sidecar.
|
|
3
|
+
*
|
|
4
|
+
* Everything is configurable three ways because the three deployments differ:
|
|
5
|
+
* a config file for a real installation, environment variables for a container,
|
|
6
|
+
* and flags for the thirty seconds when you are trying it out.
|
|
7
|
+
*/
|
|
8
|
+
import { readFileSync } from 'node:fs';
|
|
9
|
+
import { configFromEnv, resolveConfig, GatewayConfigError, DEFAULT_PORT, } from './config.js';
|
|
10
|
+
import { createGateway } from './server.js';
|
|
11
|
+
export const GATEWAY_HELP = `flare-redact gateway — redact what leaves your network, without touching your code
|
|
12
|
+
|
|
13
|
+
USAGE
|
|
14
|
+
flare-redact gateway [options]
|
|
15
|
+
|
|
16
|
+
Point a vendor SDK's base URL at this process. Request bodies are redacted on
|
|
17
|
+
the way out and the originals are restored in the reply, streaming included.
|
|
18
|
+
|
|
19
|
+
OPTIONS
|
|
20
|
+
--config <file> JSON configuration file
|
|
21
|
+
--host <host> bind address (default: 127.0.0.1)
|
|
22
|
+
--port <n> bind port (default: ${DEFAULT_PORT})
|
|
23
|
+
--upstream <url> forward everything to this origin
|
|
24
|
+
--route <prefix=url> add a prefixed route (repeatable)
|
|
25
|
+
--paths <a,b> only redact these JSON body paths
|
|
26
|
+
(e.g. 'messages[*].content,input,prompt')
|
|
27
|
+
--only <ids> use only these detectors
|
|
28
|
+
--enable <ids> turn on extra detectors (e.g. pii,phone)
|
|
29
|
+
--disable <ids> turn off detectors
|
|
30
|
+
--mode <m> mask | label | hash | pseudonym | surrogate
|
|
31
|
+
--token-env <n> env var holding the API bearer token
|
|
32
|
+
(default: FLARE_GATEWAY_TOKEN)
|
|
33
|
+
--no-restore do not put originals back into responses
|
|
34
|
+
--no-metrics disable the /metrics endpoint
|
|
35
|
+
--redact-text also redact form and text/* request bodies
|
|
36
|
+
--log <level> silent | info | debug (default: info)
|
|
37
|
+
--print-config print the resolved configuration and exit
|
|
38
|
+
-h, --help show this help
|
|
39
|
+
|
|
40
|
+
ENVIRONMENT
|
|
41
|
+
FLARE_UPSTREAM, FLARE_GATEWAY_HOST, FLARE_GATEWAY_PORT, FLARE_GATEWAY_TOKEN,
|
|
42
|
+
FLARE_REDACT_ONLY, FLARE_REDACT_ENABLE, FLARE_REDACT_DISABLE,
|
|
43
|
+
FLARE_REDACT_MODE, FLARE_REDACT_SECRET, FLARE_GATEWAY_MAX_BODY_BYTES,
|
|
44
|
+
FLARE_GATEWAY_TIMEOUT_MS, FLARE_GATEWAY_SESSION_TTL_MS, FLARE_GATEWAY_LOG
|
|
45
|
+
|
|
46
|
+
EXAMPLES
|
|
47
|
+
flare-redact gateway --upstream https://api.openai.com --enable pii
|
|
48
|
+
flare-redact gateway --route /openai=https://api.openai.com \\
|
|
49
|
+
--route /anthropic=https://api.anthropic.com
|
|
50
|
+
flare-redact gateway --config flare-gateway.json
|
|
51
|
+
|
|
52
|
+
# then, in your application — no other change
|
|
53
|
+
export OPENAI_BASE_URL=http://127.0.0.1:${DEFAULT_PORT}/openai/v1
|
|
54
|
+
`;
|
|
55
|
+
function csv(value, flag) {
|
|
56
|
+
if (!value)
|
|
57
|
+
throw new GatewayConfigError(`${flag} requires a comma-separated list`);
|
|
58
|
+
const items = value.split(',').map((item) => item.trim()).filter(Boolean);
|
|
59
|
+
if (!items.length)
|
|
60
|
+
throw new GatewayConfigError(`${flag} requires at least one value`);
|
|
61
|
+
return items;
|
|
62
|
+
}
|
|
63
|
+
function required(value, flag) {
|
|
64
|
+
if (value === undefined)
|
|
65
|
+
throw new GatewayConfigError(`${flag} requires a value`);
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
/** Parse gateway flags into a config layer. Throws {@link GatewayConfigError}. */
|
|
69
|
+
export function parseGatewayArgs(argv) {
|
|
70
|
+
const raw = {};
|
|
71
|
+
const policy = {};
|
|
72
|
+
const routes = [];
|
|
73
|
+
let help = false;
|
|
74
|
+
let printConfig = false;
|
|
75
|
+
let tokenEnv = 'FLARE_GATEWAY_TOKEN';
|
|
76
|
+
let configFile;
|
|
77
|
+
let paths;
|
|
78
|
+
let restore = true;
|
|
79
|
+
let redactText = false;
|
|
80
|
+
for (let index = 0; index < argv.length; index++) {
|
|
81
|
+
const flag = argv[index];
|
|
82
|
+
switch (flag) {
|
|
83
|
+
case '-h':
|
|
84
|
+
case '--help':
|
|
85
|
+
help = true;
|
|
86
|
+
break;
|
|
87
|
+
case '--print-config':
|
|
88
|
+
printConfig = true;
|
|
89
|
+
break;
|
|
90
|
+
case '--config':
|
|
91
|
+
configFile = required(argv[++index], '--config');
|
|
92
|
+
break;
|
|
93
|
+
case '--host':
|
|
94
|
+
raw.listen = { ...raw.listen, host: required(argv[++index], '--host') };
|
|
95
|
+
break;
|
|
96
|
+
case '--port':
|
|
97
|
+
raw.listen = { ...raw.listen, port: Number(required(argv[++index], '--port')) };
|
|
98
|
+
break;
|
|
99
|
+
case '--upstream':
|
|
100
|
+
raw.upstream = required(argv[++index], '--upstream');
|
|
101
|
+
break;
|
|
102
|
+
case '--route': {
|
|
103
|
+
const value = required(argv[++index], '--route');
|
|
104
|
+
const separator = value.indexOf('=');
|
|
105
|
+
if (separator < 1)
|
|
106
|
+
throw new GatewayConfigError(`--route expects <prefix>=<upstream>, received ${JSON.stringify(value)}`);
|
|
107
|
+
routes.push({ prefix: value.slice(0, separator), upstream: value.slice(separator + 1) });
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
case '--paths':
|
|
111
|
+
paths = csv(argv[++index], '--paths');
|
|
112
|
+
break;
|
|
113
|
+
case '--only':
|
|
114
|
+
policy.only = csv(argv[++index], '--only');
|
|
115
|
+
break;
|
|
116
|
+
case '--enable':
|
|
117
|
+
policy.enable = csv(argv[++index], '--enable');
|
|
118
|
+
break;
|
|
119
|
+
case '--disable':
|
|
120
|
+
policy.disable = csv(argv[++index], '--disable');
|
|
121
|
+
break;
|
|
122
|
+
case '--mode':
|
|
123
|
+
policy.mode = required(argv[++index], '--mode');
|
|
124
|
+
break;
|
|
125
|
+
case '--token-env':
|
|
126
|
+
tokenEnv = required(argv[++index], '--token-env');
|
|
127
|
+
break;
|
|
128
|
+
case '--no-restore':
|
|
129
|
+
restore = false;
|
|
130
|
+
break;
|
|
131
|
+
case '--no-metrics':
|
|
132
|
+
raw.metrics = false;
|
|
133
|
+
break;
|
|
134
|
+
case '--redact-text':
|
|
135
|
+
redactText = true;
|
|
136
|
+
break;
|
|
137
|
+
case '--log':
|
|
138
|
+
raw.log = required(argv[++index], '--log');
|
|
139
|
+
break;
|
|
140
|
+
default:
|
|
141
|
+
throw new GatewayConfigError(`unknown option: ${flag}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (Object.keys(policy).length)
|
|
145
|
+
raw.policy = policy;
|
|
146
|
+
if (routes.length)
|
|
147
|
+
raw.routes = routes;
|
|
148
|
+
// Flags that describe *how* to treat a body apply to every route the flags
|
|
149
|
+
// themselves declared, and to the shorthand `--upstream` route.
|
|
150
|
+
const perRoute = { ...(paths ? { paths } : {}), ...(redactText ? { nonJson: true } : {}) };
|
|
151
|
+
if (Object.keys(perRoute).length || !restore) {
|
|
152
|
+
const shape = {
|
|
153
|
+
...(Object.keys(perRoute).length ? { request: { ...perRoute, enabled: true } } : {}),
|
|
154
|
+
...(restore ? {} : { response: { enabled: false, streamEscape: 'json' } }),
|
|
155
|
+
};
|
|
156
|
+
if (raw.routes)
|
|
157
|
+
raw.routes = raw.routes.map((route) => ({ ...route, ...shape }));
|
|
158
|
+
else if (raw.upstream)
|
|
159
|
+
raw.routes = [{ prefix: '/', upstream: raw.upstream, ...shape }];
|
|
160
|
+
}
|
|
161
|
+
return { raw, help, printConfig, tokenEnv, ...(configFile ? { configFile } : {}) };
|
|
162
|
+
}
|
|
163
|
+
function readConfigFile(path) {
|
|
164
|
+
let text;
|
|
165
|
+
try {
|
|
166
|
+
text = readFileSync(path, 'utf8');
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
throw new GatewayConfigError(`cannot read ${path}: ${error.message}`);
|
|
170
|
+
}
|
|
171
|
+
try {
|
|
172
|
+
return JSON.parse(text);
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
throw new GatewayConfigError(`${path} is not valid JSON: ${error.message}`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Build the effective configuration. Precedence, lowest to highest: config
|
|
180
|
+
* file, environment, command line — the order in which each becomes more
|
|
181
|
+
* specific to this particular invocation.
|
|
182
|
+
*/
|
|
183
|
+
export function buildConfig(argv, env) {
|
|
184
|
+
const parsed = parseGatewayArgs(argv);
|
|
185
|
+
const file = parsed.configFile ? readConfigFile(parsed.configFile) : undefined;
|
|
186
|
+
const fromEnv = configFromEnv(env);
|
|
187
|
+
const token = env[parsed.tokenEnv];
|
|
188
|
+
const config = resolveConfig(file, fromEnv, parsed.raw, token ? { token } : undefined);
|
|
189
|
+
if (!config.routes.length) {
|
|
190
|
+
throw new GatewayConfigError('no upstream configured. Pass --upstream <url>, --route <prefix>=<url>, a --config file, or set FLARE_UPSTREAM. ' +
|
|
191
|
+
'The /v1 redaction API works without one — start with --upstream http://localhost:1 if that is all you need.');
|
|
192
|
+
}
|
|
193
|
+
return config;
|
|
194
|
+
}
|
|
195
|
+
/** Run the gateway until the process is asked to stop. Resolves with an exit code. */
|
|
196
|
+
export async function main(argv, env = process.env) {
|
|
197
|
+
let config;
|
|
198
|
+
try {
|
|
199
|
+
const parsed = parseGatewayArgs(argv);
|
|
200
|
+
if (parsed.help) {
|
|
201
|
+
process.stdout.write(GATEWAY_HELP);
|
|
202
|
+
return 0;
|
|
203
|
+
}
|
|
204
|
+
config = buildConfig(argv, env);
|
|
205
|
+
if (parsed.printConfig) {
|
|
206
|
+
process.stdout.write(JSON.stringify({ ...config, token: config.token ? '***' : undefined }, null, 2) + '\n');
|
|
207
|
+
return 0;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
process.stderr.write(`${error.message}\n`);
|
|
212
|
+
return 2;
|
|
213
|
+
}
|
|
214
|
+
const gateway = createGateway(config);
|
|
215
|
+
try {
|
|
216
|
+
await gateway.listen();
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
process.stderr.write(`failed to listen on ${config.listen.host}:${config.listen.port}: ${error.message}\n`);
|
|
220
|
+
return 2;
|
|
221
|
+
}
|
|
222
|
+
await new Promise((resolve) => {
|
|
223
|
+
const stop = () => resolve();
|
|
224
|
+
process.once('SIGINT', stop);
|
|
225
|
+
process.once('SIGTERM', stop);
|
|
226
|
+
});
|
|
227
|
+
await gateway.close();
|
|
228
|
+
return 0;
|
|
229
|
+
}
|
|
230
|
+
//# sourceMappingURL=cli.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../../src/gateway/cli.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEvC,OAAO,EACL,aAAa,EACb,aAAa,EACb,kBAAkB,EAClB,YAAY,GAGb,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAG5C,MAAM,CAAC,MAAM,YAAY,GAAG;;;;;;;;;;;8CAWkB,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;4CA+Bd,YAAY;CACvD,CAAC;AAEF,SAAS,GAAG,CAAC,KAAyB,EAAE,IAAY;IAClD,IAAI,CAAC,KAAK;QAAE,MAAM,IAAI,kBAAkB,CAAC,GAAG,IAAI,kCAAkC,CAAC,CAAC;IACpF,MAAM,KAAK,GAAG,KAAK,CAAC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CAAC;IACX,CAAC;IAED,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;QAClC,MAAM,IAAI,GAAG,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC;QAC7B,OAAO,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;QAC7B,OAAO,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IAChC,CAAC,CAAC,CAAC;IACH,MAAM,OAAO,CAAC,KAAK,EAAE,CAAC;IACtB,OAAO,CAAC,CAAC;AACX,CAAC"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gateway configuration: a file, environment variables, and command-line flags,
|
|
3
|
+
* merged in that order of increasing precedence and then validated once.
|
|
4
|
+
*
|
|
5
|
+
* The defaults are the safe ones. The listener binds to loopback, the `/v1` API
|
|
6
|
+
* refuses unauthenticated calls as soon as a token exists, bodies are capped,
|
|
7
|
+
* and an upstream must be an explicit absolute `http(s)` URL — a redaction proxy
|
|
8
|
+
* that can be pointed anywhere by a header is an open relay.
|
|
9
|
+
*/
|
|
10
|
+
import type { RedactOptions } from '../index.js';
|
|
11
|
+
export interface ListenConfig {
|
|
12
|
+
host: string;
|
|
13
|
+
port: number;
|
|
14
|
+
}
|
|
15
|
+
export interface RequestRedactionConfig {
|
|
16
|
+
/**
|
|
17
|
+
* JSON paths to redact, e.g. `["messages[*].content", "input", "prompt"]`.
|
|
18
|
+
* Omit to redact every string in the body — safest, and the default.
|
|
19
|
+
*/
|
|
20
|
+
paths?: string[];
|
|
21
|
+
/** Set false to forward request bodies untouched (response restore still works). */
|
|
22
|
+
enabled: boolean;
|
|
23
|
+
/** Also redact `application/x-www-form-urlencoded` values and plain text. */
|
|
24
|
+
nonJson: boolean;
|
|
25
|
+
}
|
|
26
|
+
export interface ResponseRestorationConfig {
|
|
27
|
+
/**
|
|
28
|
+
* Put the originals back into the upstream's answer. This is what makes the
|
|
29
|
+
* proxy transparent: the model sees placeholders, your app sees real data.
|
|
30
|
+
*/
|
|
31
|
+
enabled: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* How a placeholder inside a streamed frame is replaced. `json` escapes the
|
|
34
|
+
* original for a JSON string context, which is what every LLM streaming API
|
|
35
|
+
* puts on the wire; `none` substitutes verbatim.
|
|
36
|
+
*/
|
|
37
|
+
streamEscape: 'json' | 'none';
|
|
38
|
+
}
|
|
39
|
+
export interface RouteConfig {
|
|
40
|
+
/** Stable name used in logs and metrics. Defaults to the prefix. */
|
|
41
|
+
name: string;
|
|
42
|
+
/** Path prefix this route claims, e.g. `/openai`. `/` is the catch-all. */
|
|
43
|
+
prefix: string;
|
|
44
|
+
/** Absolute upstream origin, e.g. `https://api.openai.com`. */
|
|
45
|
+
upstream: string;
|
|
46
|
+
/** Remove `prefix` before forwarding. Default: true for anything but `/`. */
|
|
47
|
+
stripPrefix: boolean;
|
|
48
|
+
/** Policy overrides layered on top of the gateway-wide policy. */
|
|
49
|
+
policy?: RedactOptions;
|
|
50
|
+
request: RequestRedactionConfig;
|
|
51
|
+
response: ResponseRestorationConfig;
|
|
52
|
+
/** Extra headers to set on the upstream request, e.g. a vendor API version. */
|
|
53
|
+
headers?: Record<string, string>;
|
|
54
|
+
}
|
|
55
|
+
export interface GatewayConfig {
|
|
56
|
+
listen: ListenConfig;
|
|
57
|
+
/** Bearer token required by `/v1`. Unset means the API is open — loopback only. */
|
|
58
|
+
token?: string;
|
|
59
|
+
policy: RedactOptions;
|
|
60
|
+
routes: RouteConfig[];
|
|
61
|
+
sessions: {
|
|
62
|
+
ttlMs: number;
|
|
63
|
+
max: number;
|
|
64
|
+
};
|
|
65
|
+
limits: {
|
|
66
|
+
maxBodyBytes: number;
|
|
67
|
+
requestTimeoutMs: number;
|
|
68
|
+
};
|
|
69
|
+
/** Count findings per detector for logs and metrics. Costs one extra scan. */
|
|
70
|
+
auditFindings: boolean;
|
|
71
|
+
metrics: boolean;
|
|
72
|
+
log: 'silent' | 'info' | 'debug';
|
|
73
|
+
}
|
|
74
|
+
export declare class GatewayConfigError extends Error {
|
|
75
|
+
constructor(message: string);
|
|
76
|
+
}
|
|
77
|
+
export declare const DEFAULT_PORT = 8787;
|
|
78
|
+
/** Paths the gateway answers itself; a route may not claim them. */
|
|
79
|
+
export declare const RESERVED_PREFIXES: string[];
|
|
80
|
+
/** A route as written in a config file: everything but `upstream` is optional. */
|
|
81
|
+
export type RawRoute = Partial<Omit<RouteConfig, 'request' | 'response'>> & {
|
|
82
|
+
request?: Partial<RequestRedactionConfig>;
|
|
83
|
+
response?: Partial<ResponseRestorationConfig>;
|
|
84
|
+
};
|
|
85
|
+
export interface RawConfig {
|
|
86
|
+
listen?: Partial<ListenConfig>;
|
|
87
|
+
token?: string;
|
|
88
|
+
policy?: RedactOptions;
|
|
89
|
+
routes?: RawRoute[];
|
|
90
|
+
upstream?: string;
|
|
91
|
+
sessions?: Partial<GatewayConfig['sessions']>;
|
|
92
|
+
limits?: Partial<GatewayConfig['limits']>;
|
|
93
|
+
auditFindings?: boolean;
|
|
94
|
+
metrics?: boolean;
|
|
95
|
+
log?: GatewayConfig['log'];
|
|
96
|
+
}
|
|
97
|
+
/** Environment overrides, for the container case where a file is inconvenient. */
|
|
98
|
+
export declare function configFromEnv(env: NodeJS.ProcessEnv): RawConfig;
|
|
99
|
+
/**
|
|
100
|
+
* Build a validated configuration from any number of partial layers, later
|
|
101
|
+
* layers winning. Throws {@link GatewayConfigError} with a message that names
|
|
102
|
+
* the offending field — a proxy that starts with a subtly wrong upstream is
|
|
103
|
+
* worse than one that refuses to start.
|
|
104
|
+
*/
|
|
105
|
+
export declare function resolveConfig(...layers: Array<RawConfig | undefined>): GatewayConfig;
|
|
106
|
+
/** The route claiming `pathname`, or undefined when the gateway is API-only. */
|
|
107
|
+
export declare function matchRoute(routes: RouteConfig[], pathname: string): RouteConfig | undefined;
|
|
108
|
+
/** The upstream URL a request is forwarded to. */
|
|
109
|
+
export declare function upstreamUrl(route: RouteConfig, pathname: string, search: string): string;
|
|
110
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/gateway/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAEjD,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,sBAAsB;IACrC;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,oFAAoF;IACpF,OAAO,EAAE,OAAO,CAAC;IACjB,6EAA6E;IAC7E,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,yBAAyB;IACxC;;;OAGG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB;;;;OAIG;IACH,YAAY,EAAE,MAAM,GAAG,MAAM,CAAC;CAC/B;AAED,MAAM,WAAW,WAAW;IAC1B,oEAAoE;IACpE,IAAI,EAAE,MAAM,CAAC;IACb,2EAA2E;IAC3E,MAAM,EAAE,MAAM,CAAC;IACf,+DAA+D;IAC/D,QAAQ,EAAE,MAAM,CAAC;IACjB,6EAA6E;IAC7E,WAAW,EAAE,OAAO,CAAC;IACrB,kEAAkE;IAClE,MAAM,CAAC,EAAE,aAAa,CAAC;IACvB,OAAO,EAAE,sBAAsB,CAAC;IAChC,QAAQ,EAAE,yBAAyB,CAAC;IACpC,+EAA+E;IAC/E,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC;AAED,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,YAAY,CAAC;IACrB,mFAAmF;IACnF,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,aAAa,CAAC;IACtB,MAAM,EAAE,WAAW,EAAE,CAAC;IACtB,QAAQ,EAAE;QAAE,K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ClF,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,CAAC,UAAU,GAAG,SAAS,CA8B/D;AAeD;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,GAAG,MAAM,EAAE,KAAK,CAAC,SAAS,GAAG,SAAS,CAAC,GAAG,aAAa,CA6CpF;AAED,gFAAgF;AAChF,wBAAgB,UAAU,CAAC,MAAM,EAAE,WAAW,EAAE,EAAE,QAAQ,EAAE,MAAM,GAAG,WAAW,GAAG,SAAS,CAK3F;AAED,kDAAkD;AAClD,wBAAgB,WAAW,CAAC,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAIxF"}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gateway configuration: a file, environment variables, and command-line flags,
|
|
3
|
+
* merged in that order of increasing precedence and then validated once.
|
|
4
|
+
*
|
|
5
|
+
* The defaults are the safe ones. The listener binds to loopback, the `/v1` API
|
|
6
|
+
* refuses unauthenticated calls as soon as a token exists, bodies are capped,
|
|
7
|
+
* and an upstream must be an explicit absolute `http(s)` URL — a redaction proxy
|
|
8
|
+
* that can be pointed anywhere by a header is an open relay.
|
|
9
|
+
*/
|
|
10
|
+
export class GatewayConfigError extends Error {
|
|
11
|
+
constructor(message) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = 'GatewayConfigError';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export const DEFAULT_PORT = 8787;
|
|
17
|
+
const DEFAULTS = {
|
|
18
|
+
listen: { host: '127.0.0.1', port: DEFAULT_PORT },
|
|
19
|
+
policy: {},
|
|
20
|
+
routes: [],
|
|
21
|
+
sessions: { ttlMs: 30 * 60_000, max: 10_000 },
|
|
22
|
+
limits: { maxBodyBytes: 8 * 1024 * 1024, requestTimeoutMs: 120_000 },
|
|
23
|
+
auditFindings: true,
|
|
24
|
+
metrics: true,
|
|
25
|
+
log: 'info',
|
|
26
|
+
};
|
|
27
|
+
/** Paths the gateway answers itself; a route may not claim them. */
|
|
28
|
+
export const RESERVED_PREFIXES = ['/healthz', '/readyz', '/metrics', '/v1/'];
|
|
29
|
+
function normaliseUpstream(value, where) {
|
|
30
|
+
let url;
|
|
31
|
+
try {
|
|
32
|
+
url = new URL(value);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
throw new GatewayConfigError(`${where}: upstream must be an absolute URL, received ${JSON.stringify(value)}`);
|
|
36
|
+
}
|
|
37
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
|
|
38
|
+
throw new GatewayConfigError(`${where}: upstream must use http or https, received ${url.protocol}`);
|
|
39
|
+
}
|
|
40
|
+
if (url.search || url.hash) {
|
|
41
|
+
throw new GatewayConfigError(`${where}: upstream must not carry a query string or fragment`);
|
|
42
|
+
}
|
|
43
|
+
// Keep any base path, drop the trailing slash so joining is unambiguous.
|
|
44
|
+
return (url.origin + url.pathname).replace(/\/$/, '');
|
|
45
|
+
}
|
|
46
|
+
function normalisePrefix(value, where) {
|
|
47
|
+
if (!value.startsWith('/'))
|
|
48
|
+
throw new GatewayConfigError(`${where}: prefix must start with "/"`);
|
|
49
|
+
const trimmed = value.length > 1 ? value.replace(/\/$/, '') : '/';
|
|
50
|
+
for (const reserved of RESERVED_PREFIXES) {
|
|
51
|
+
if (reserved.startsWith(trimmed === '/' ? '/x' : trimmed)) {
|
|
52
|
+
throw new GatewayConfigError(`${where}: prefix ${JSON.stringify(trimmed)} would shadow the gateway's own ${reserved} endpoint`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return trimmed;
|
|
56
|
+
}
|
|
57
|
+
function normaliseRoute(raw, index) {
|
|
58
|
+
const where = `routes[${index}]`;
|
|
59
|
+
if (!raw.upstream)
|
|
60
|
+
throw new GatewayConfigError(`${where}: upstream is required`);
|
|
61
|
+
const prefix = normalisePrefix(raw.prefix ?? '/', where);
|
|
62
|
+
return {
|
|
63
|
+
name: raw.name ?? prefix,
|
|
64
|
+
prefix,
|
|
65
|
+
upstream: normaliseUpstream(raw.upstream, where),
|
|
66
|
+
stripPrefix: raw.stripPrefix ?? prefix !== '/',
|
|
67
|
+
...(raw.policy ? { policy: raw.policy } : {}),
|
|
68
|
+
request: {
|
|
69
|
+
enabled: raw.request?.enabled ?? true,
|
|
70
|
+
nonJson: raw.request?.nonJson ?? false,
|
|
71
|
+
...(raw.request?.paths?.length ? { paths: raw.request.paths } : {}),
|
|
72
|
+
},
|
|
73
|
+
response: {
|
|
74
|
+
enabled: raw.response?.enabled ?? true,
|
|
75
|
+
streamEscape: raw.response?.streamEscape ?? 'json',
|
|
76
|
+
},
|
|
77
|
+
...(raw.headers ? { headers: raw.headers } : {}),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function positiveInteger(value, where) {
|
|
81
|
+
if (value === undefined)
|
|
82
|
+
return undefined;
|
|
83
|
+
const parsed = Number(value);
|
|
84
|
+
if (!Number.isSafeInteger(parsed) || parsed <= 0) {
|
|
85
|
+
throw new GatewayConfigError(`${where} must be a positive integer, received ${JSON.stringify(value)}`);
|
|
86
|
+
}
|
|
87
|
+
return parsed;
|
|
88
|
+
}
|
|
89
|
+
function csv(value) {
|
|
90
|
+
if (!value)
|
|
91
|
+
return undefined;
|
|
92
|
+
const items = value.split(',').map((item) => item.trim()).filter(Boolean);
|
|
93
|
+
return items.length ? items : undefined;
|
|
94
|
+
}
|
|
95
|
+
/** Environment overrides, for the container case where a file is inconvenient. */
|
|
96
|
+
export function configFromEnv(env) {
|
|
97
|
+
const raw = {};
|
|
98
|
+
const host = env.FLARE_GATEWAY_HOST;
|
|
99
|
+
const port = positiveInteger(env.FLARE_GATEWAY_PORT, 'FLARE_GATEWAY_PORT');
|
|
100
|
+
if (host || port)
|
|
101
|
+
raw.listen = { ...(host ? { host } : {}), ...(port ? { port } : {}) };
|
|
102
|
+
if (env.FLARE_GATEWAY_TOKEN)
|
|
103
|
+
raw.token = env.FLARE_GATEWAY_TOKEN;
|
|
104
|
+
if (env.FLARE_UPSTREAM)
|
|
105
|
+
raw.upstream = env.FLARE_UPSTREAM;
|
|
106
|
+
const policy = {};
|
|
107
|
+
const only = csv(env.FLARE_REDACT_ONLY);
|
|
108
|
+
const enable = csv(env.FLARE_REDACT_ENABLE);
|
|
109
|
+
const disable = csv(env.FLARE_REDACT_DISABLE);
|
|
110
|
+
if (only)
|
|
111
|
+
policy.only = only;
|
|
112
|
+
if (enable)
|
|
113
|
+
policy.enable = enable;
|
|
114
|
+
if (disable)
|
|
115
|
+
policy.disable = disable;
|
|
116
|
+
if (env.FLARE_REDACT_MODE)
|
|
117
|
+
policy.mode = env.FLARE_REDACT_MODE;
|
|
118
|
+
if (env.FLARE_REDACT_SECRET)
|
|
119
|
+
policy.transformSecret = env.FLARE_REDACT_SECRET;
|
|
120
|
+
if (Object.keys(policy).length)
|
|
121
|
+
raw.policy = policy;
|
|
122
|
+
const maxBodyBytes = positiveInteger(env.FLARE_GATEWAY_MAX_BODY_BYTES, 'FLARE_GATEWAY_MAX_BODY_BYTES');
|
|
123
|
+
const requestTimeoutMs = positiveInteger(env.FLARE_GATEWAY_TIMEOUT_MS, 'FLARE_GATEWAY_TIMEOUT_MS');
|
|
124
|
+
if (maxBodyBytes || requestTimeoutMs) {
|
|
125
|
+
raw.limits = { ...(maxBodyBytes ? { maxBodyBytes } : {}), ...(requestTimeoutMs ? { requestTimeoutMs } : {}) };
|
|
126
|
+
}
|
|
127
|
+
const ttlMs = positiveInteger(env.FLARE_GATEWAY_SESSION_TTL_MS, 'FLARE_GATEWAY_SESSION_TTL_MS');
|
|
128
|
+
if (ttlMs)
|
|
129
|
+
raw.sessions = { ttlMs };
|
|
130
|
+
if (env.FLARE_GATEWAY_LOG)
|
|
131
|
+
raw.log = env.FLARE_GATEWAY_LOG;
|
|
132
|
+
if (env.FLARE_GATEWAY_METRICS === 'false')
|
|
133
|
+
raw.metrics = false;
|
|
134
|
+
if (env.FLARE_GATEWAY_AUDIT_FINDINGS === 'false')
|
|
135
|
+
raw.auditFindings = false;
|
|
136
|
+
return raw;
|
|
137
|
+
}
|
|
138
|
+
function mergeRaw(base, override) {
|
|
139
|
+
return {
|
|
140
|
+
...base,
|
|
141
|
+
...override,
|
|
142
|
+
listen: { ...base.listen, ...override.listen },
|
|
143
|
+
policy: { ...base.policy, ...override.policy },
|
|
144
|
+
sessions: { ...base.sessions, ...override.sessions },
|
|
145
|
+
limits: { ...base.limits, ...override.limits },
|
|
146
|
+
routes: override.routes ?? base.routes,
|
|
147
|
+
upstream: override.upstream ?? base.upstream,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Build a validated configuration from any number of partial layers, later
|
|
152
|
+
* layers winning. Throws {@link GatewayConfigError} with a message that names
|
|
153
|
+
* the offending field — a proxy that starts with a subtly wrong upstream is
|
|
154
|
+
* worse than one that refuses to start.
|
|
155
|
+
*/
|
|
156
|
+
export function resolveConfig(...layers) {
|
|
157
|
+
const raw = layers.filter((layer) => Boolean(layer)).reduce(mergeRaw, {});
|
|
158
|
+
const rawRoutes = raw.routes?.length
|
|
159
|
+
? raw.routes
|
|
160
|
+
: raw.upstream
|
|
161
|
+
? [{ prefix: '/', upstream: raw.upstream }]
|
|
162
|
+
: [];
|
|
163
|
+
const routes = rawRoutes.map(normaliseRoute);
|
|
164
|
+
const seen = new Set();
|
|
165
|
+
for (const route of routes) {
|
|
166
|
+
if (seen.has(route.prefix))
|
|
167
|
+
throw new GatewayConfigError(`duplicate route prefix ${JSON.stringify(route.prefix)}`);
|
|
168
|
+
seen.add(route.prefix);
|
|
169
|
+
}
|
|
170
|
+
// Longest prefix first, so `/openai/v1` wins over `/openai`.
|
|
171
|
+
routes.sort((a, b) => b.prefix.length - a.prefix.length);
|
|
172
|
+
const log = raw.log ?? DEFAULTS.log;
|
|
173
|
+
if (log !== 'silent' && log !== 'info' && log !== 'debug') {
|
|
174
|
+
throw new GatewayConfigError(`log must be silent, info or debug, received ${JSON.stringify(log)}`);
|
|
175
|
+
}
|
|
176
|
+
const port = raw.listen?.port ?? DEFAULTS.listen.port;
|
|
177
|
+
if (!Number.isInteger(port) || port < 0 || port > 65535) {
|
|
178
|
+
throw new GatewayConfigError(`listen.port must be between 0 and 65535, received ${JSON.stringify(port)}`);
|
|
179
|
+
}
|
|
180
|
+
return {
|
|
181
|
+
listen: { host: raw.listen?.host ?? DEFAULTS.listen.host, port },
|
|
182
|
+
...(raw.token ? { token: raw.token } : {}),
|
|
183
|
+
policy: raw.policy ?? DEFAULTS.policy,
|
|
184
|
+
routes,
|
|
185
|
+
sessions: {
|
|
186
|
+
ttlMs: raw.sessions?.ttlMs ?? DEFAULTS.sessions.ttlMs,
|
|
187
|
+
max: raw.sessions?.max ?? DEFAULTS.sessions.max,
|
|
188
|
+
},
|
|
189
|
+
limits: {
|
|
190
|
+
maxBodyBytes: raw.limits?.maxBodyBytes ?? DEFAULTS.limits.maxBodyBytes,
|
|
191
|
+
requestTimeoutMs: raw.limits?.requestTimeoutMs ?? DEFAULTS.limits.requestTimeoutMs,
|
|
192
|
+
},
|
|
193
|
+
auditFindings: raw.auditFindings ?? DEFAULTS.auditFindings,
|
|
194
|
+
metrics: raw.metrics ?? DEFAULTS.metrics,
|
|
195
|
+
log,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
/** The route claiming `pathname`, or undefined when the gateway is API-only. */
|
|
199
|
+
export function matchRoute(routes, pathname) {
|
|
200
|
+
return routes.find((route) => {
|
|
201
|
+
if (route.prefix === '/')
|
|
202
|
+
return true;
|
|
203
|
+
return pathname === route.prefix || pathname.startsWith(`${route.prefix}/`);
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
/** The upstream URL a request is forwarded to. */
|
|
207
|
+
export function upstreamUrl(route, pathname, search) {
|
|
208
|
+
const remainder = route.stripPrefix && route.prefix !== '/' ? pathname.slice(route.prefix.length) : pathname;
|
|
209
|
+
const path = remainder.startsWith('/') ? remainder : `/${remainder}`;
|
|
210
|
+
return `${route.upstream}${path}${search}`;
|
|
211
|
+
}
|
|
212
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/gateway/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAkEH,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAC3C,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IACnC,CAAC;CACF;AAED,MAAM,CAAC,MAAM,YAAY,GAAG,IAAI,CAAC;AAEjC,MAAM,QAAQ,GAAkB;IAC9B,MAAM,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,YAAY,EAAE;IACjD,MAAM,EAAE,EAAE;IACV,MAAM,EAAE,EAAE;IACV,QAAQ,EAAE,EAAE,KAAK,EAAE,EAAE,GAAG,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE;IAC7C,MAAM,EAAE,EAAE,YAAY,EAAE,CAAC,GAAG,IAAI,GAAG,IAAI,EAAE,gBAAgB,EAAE,OAAO,EAAE;IACpE,aAAa,EAAE,IAAI;IACnB,OAAO,EAAE,IAAI;IACb,GAAG,EAAE,MAAM;CACZ,CAAC;AAEF,oEAAoE;AACpE,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,UAAU,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,CAAC,CAAC;AAqB7E,SAAS,iBAAiB,CAAC,KAAa,EAAE,KAAa;IACrD,IAAI,GAAQ,CAAC;IACb,IAAI,CAAC;QACH,GAAG,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,kBAAkB,CAAC,GAAG,KAAK,gDAAgD,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;IAChH,CAAC;IACD,IAAI,G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AAC,MAAM,KAAK,GAAG;YAAE,OAAO,IAAI,CAAC;QACtC,OAAO,QAAQ,KAAK,KAAK,CAAC,MAAM,IAAI,QAAQ,CAAC,UAAU,CAAC,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;IAC9E,CAAC,CAAC,CAAC;AACL,CAAC;AAED,kDAAkD;AAClD,MAAM,UAAU,WAAW,CAAC,KAAkB,EAAE,QAAgB,EAAE,MAAc;IAC9E,MAAM,SAAS,GAAG,KAAK,CAAC,WAAW,IAAI,KAAK,CAAC,MAAM,KAAK,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;IAC7G,MAAM,IAAI,GAAG,SAAS,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,SAAS,EAAE,CAAC;IACrE,OAAO,GAAG,KAAK,CAAC,QAAQ,GAAG,IAAI,GAAG,MAAM,EAAE,CAAC;AAC7C,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `flare-redact/gateway` — the sidecar, as a library.
|
|
3
|
+
*
|
|
4
|
+
* Import this to embed the proxy in an existing Node service, to write tests
|
|
5
|
+
* against it, or to build a differently-shaped deployment. The CLI in
|
|
6
|
+
* `flare-redact gateway` is a thin wrapper over exactly these exports.
|
|
7
|
+
*
|
|
8
|
+
* import { resolveConfig, createGateway } from 'flare-redact/gateway';
|
|
9
|
+
*
|
|
10
|
+
* const gateway = createGateway(resolveConfig({
|
|
11
|
+
* upstream: 'https://api.openai.com',
|
|
12
|
+
* policy: { enable: ['pii'] },
|
|
13
|
+
* }));
|
|
14
|
+
* await gateway.listen();
|
|
15
|
+
*/
|
|
16
|
+
export { resolveConfig, configFromEnv, matchRoute, upstreamUrl, GatewayConfigError, DEFAULT_PORT, RESERVED_PREFIXES, type GatewayConfig, type RawConfig, type RawRoute, type RouteConfig, type ListenConfig, type RequestRedactionConfig, type ResponseRestorationConfig, } from './config.js';
|
|
17
|
+
export { parsePath, transformAtPath, transformAtPaths, redactBody, restoreBody, streamRestorer, jsonEscapedEntries, isJsonType, isFormType, isTextType, isEventStream, PathSyntaxError, type ParsedPath, type PathSegment, type BodyRedactionOptions, type BodyRedactionResult, } from './body.js';
|
|
18
|
+
export { SessionStore, Metrics, createLogger, type Logger, type LogLevel, type SessionRecord, type SessionStoreOptions, } from './runtime.js';
|
|
19
|
+
export { createGateway, type Gateway } from './server.js';
|
|
20
|
+
export { main as runGateway, buildConfig, parseGatewayArgs, GATEWAY_HELP } from './cli.js';
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/gateway/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EACL,aAAa,EACb,aAAa,EACb,UAAU,EACV,WAAW,EACX,kBAAkB,EAClB,YAAY,EACZ,iBAAiB,EACjB,KAAK,aAAa,EAClB,KAAK,SAAS,EACd,KAAK,QAAQ,EACb,KAAK,WAAW,EAChB,KAAK,YAAY,EACjB,KAAK,sBAAsB,EAC3B,KAAK,yBAAyB,GAC/B,MAAM,aAAa,CAAC;AAErB,OAAO,EACL,SAAS,EACT,eAAe,EACf,gBAAgB,EAChB,UAAU,EACV,WAAW,EACX,cAAc,EACd,kBAAkB,EAClB,UAAU,EACV,UAAU,EACV,UAAU,EACV,aAAa,EACb,eAAe,EACf,KAAK,UAAU,EACf,KAAK,WAAW,EAChB,KAAK,oBAAoB,EACzB,KAAK,mBAAmB,GACzB,MAAM,WAAW,CAAC;AAEnB,OAAO,EACL,YAAY,EACZ,OAAO,EACP,YAAY,EACZ,KAAK,MAAM,EACX,KAAK,QAAQ,EACb,KAAK,aAAa,EAClB,KAAK,mBAAmB,GACzB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,aAAa,EAAE,KAAK,OAAO,EAAE,MAAM,aAAa,CAAC;AAE1D,OAAO,EAAE,IAAI,IAAI,UAAU,EAAE,WAAW,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `flare-redact/gateway` — the sidecar, as a library.
|
|
3
|
+
*
|
|
4
|
+
* Import this to embed the proxy in an existing Node service, to write tests
|
|
5
|
+
* against it, or to build a differently-shaped deployment. The CLI in
|
|
6
|
+
* `flare-redact gateway` is a thin wrapper over exactly these exports.
|
|
7
|
+
*
|
|
8
|
+
* import { resolveConfig, createGateway } from 'flare-redact/gateway';
|
|
9
|
+
*
|
|
10
|
+
* const gateway = createGateway(resolveConfig({
|
|
11
|
+
* upstream: 'https://api.openai.com',
|
|
12
|
+
* policy: { enable: ['pii'] },
|
|
13
|
+
* }));
|
|
14
|
+
* await gateway.listen();
|
|
15
|
+
*/
|
|
16
|
+
export { resolveConfig, configFromEnv, matchRoute, upstreamUrl, GatewayConfigError, DEFAULT_PORT, RESERVED_PREFIXES, } from './config.js';
|
|
17
|
+
export { parsePath, transformAtPath, transformAtPaths, redactBody, restoreBody, streamRestorer, jsonEscapedEntries, isJsonType, isFormType, isTextType, isEventStream, PathSyntaxError, } from './body.js';
|
|
18
|
+
export { SessionStore, Metrics, createLogger, } from './runtime.js';
|
|
19
|
+
export { createGateway } from './server.js';
|
|
20
|
+
export { main as runGateway, buildConfig, parseGatewayArgs, GATEWAY_HELP } from './cli.js';
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/gateway/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EACL,aAAa,EACb,aAAa,EACb,UAAU,EACV,WAAW,EACX,kBAAkB,EAClB,YAAY,EACZ,iBAAiB,GAQlB,MAAM,aAAa,CAAC;AAErB,OAAO,EACL,SAAS,EACT,eAAe,EACf,gBAAgB,EAChB,UAAU,EACV,WAAW,EACX,cAAc,EACd,kBAAkB,EAClB,UAAU,EACV,UAAU,EACV,UAAU,EACV,aAAa,EACb,eAAe,GAKhB,MAAM,WAAW,CAAC;AAEnB,OAAO,EACL,YAAY,EACZ,OAAO,EACP,YAAY,GAKb,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,aAAa,EAAgB,MAAM,aAAa,CAAC;AAE1D,OAAO,EAAE,IAAI,IAAI,UAAU,EAAE,WAAW,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC"}
|