Skip to content

Commit dceb3ed

Browse files
authored
ref(service): Share parseJson, isObject, and httpError (#62)
2 parents fdffc6e + 2b98d99 commit dceb3ed

8 files changed

Lines changed: 58 additions & 64 deletions

File tree

‎README.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,8 @@ In `packages/service/src/`:
10491049
- `clients.ts`: the PROXY protocol header and the address of a client.
10501050
- `run-config.ts`: the check of the run config of a client, with its
10511051
limits.
1052-
- `errors.ts`: the log line and the client text of an unexpected error.
1052+
- `errors.ts`: the errors that are the fault of the client, and the log
1053+
line and the client text of an unexpected error.
10531054
- `addresses.ts`: the public addresses that the proxy can send requests to.
10541055
- `recorder.ts`: modes, sessions, replay, and misses.
10551056
- `recording-file.ts`: a recording file from a live response, the response
@@ -1066,6 +1067,6 @@ In `packages/service/src/`:
10661067
- `streams.ts`: merges the deltas of a recorded model stream.
10671068
- `usage.ts`: the token usage of a model response, for Sentry.
10681069
- `secrets.ts`: redaction of credentials.
1069-
- `json-strings.ts`: the one-pass scan of the JSON strings of a body, for
1070-
`secrets.ts`.
1070+
- `json-strings.ts`: the parse of a JSON body, and the one-pass scan of
1071+
the JSON strings of a body, for `secrets.ts`.
10711072
- `certificates.ts`: the certificate authority for HTTPS.

‎packages/service/src/bodies.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@
44
* the time that a body has to come.
55
*/
66
import type http from "node:http";
7+
import { httpError } from "./errors.ts";
78

89
/**
910
* A control API body over this size gets HTTP 413. Anyone can call
1011
* `POST /runs`, so the service reads no more before it checks the token.
1112
*/
1213
const MAX_CONTROL_BODY_BYTES = 1024 * 1024;
1314

14-
/** An error that answers a request with its own status. */
15-
const httpError = (status: number, message: string) =>
16-
Object.assign(new Error(message), { status });
17-
1815
/**
1916
* The bytes of request bodies that the requests of many targets can hold
2017
* at one time (`ProxyTarget.bodyBudget`). `used` counts the bodies of the

‎packages/service/src/errors.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
/**
2-
* The log line of an unexpected error and the text that goes back to the
3-
* client. Neither has the message of the error, because it can hold a
4-
* response body. Only the Sentry event has the full error.
2+
* The errors of the service: an error that is the fault of the client,
3+
* with its HTTP status (`httpError()`), and the log line and the client
4+
* text of an unexpected error. Neither of these has the message of an
5+
* unexpected error, because it can hold a response body. Only the Sentry
6+
* event has the full error.
57
*/
68

9+
/**
10+
* An error that is the fault of the client. The client gets its message
11+
* with the HTTP status `status`.
12+
*/
13+
export const httpError = (status: number, message: string) =>
14+
Object.assign(new Error(message), { status });
15+
716
/**
817
* The class, code, and HTTP status of an error, for the log. The message
918
* can hold a response body, so the log does not get it.

‎packages/service/src/json-strings.ts‎

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
11
/**
2-
* The JSON reads of the redaction of credentials (`secrets.ts`).
2+
* The JSON reads of the service.
33
*
4-
* `scanStrings()` reads JSON text in one pass and gives each string with
5-
* its depth, whether it is a field name, and its escapes. `decodeString()`
6-
* decodes one string. `parseJson()` parses a whole body.
4+
* `parseJson()` parses a whole body, for the key (`request-key.ts`) and
5+
* the token usage (`usage.ts`). `parseJsonSkipBom()` also skips a byte
6+
* order mark at the start, for the redaction of credentials
7+
* (`secrets.ts`). The key does not skip it, so that no key changes.
8+
* `scanStrings()` reads JSON text in one pass and gives each
9+
* string with its depth, whether it is a field name, and its escapes.
10+
* `decodeString()` decodes one string.
711
*/
812

9-
/**
10-
* The parsed value of a JSON text, or `undefined` when it is not JSON. A
11-
* byte order mark at the start does not count.
12-
*/
13+
/** The parsed value of a JSON text, or `undefined` when it is not JSON. */
1314
export const parseJson = (text: string): unknown => {
1415
try {
15-
return JSON.parse(text.startsWith("\uFEFF") ? text.slice(1) : text);
16+
return JSON.parse(text);
1617
} catch {
1718
return undefined;
1819
}
1920
};
2021

22+
/**
23+
* The parsed value of a JSON text, or `undefined` when it is not JSON. A
24+
* byte order mark at the start does not count.
25+
*/
26+
export const parseJsonSkipBom = (text: string): unknown =>
27+
parseJson(text.startsWith("\uFEFF") ? text.slice(1) : text);
28+
29+
/** Tells whether a parsed JSON value is an object that is not an array. */
30+
export const isObject = (json: unknown): json is Record<string, unknown> =>
31+
json !== null && typeof json === "object" && !Array.isArray(json);
32+
2133
const QUOTE = 0x22;
2234

2335
const BACKSLASH = 0x5c;

‎packages/service/src/recorder.ts‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*/
3333
import { randomBytes } from "node:crypto";
3434
import type { IncomingHttpHeaders } from "node:http";
35-
import { reportInternal } from "./errors.ts";
35+
import { httpError, reportInternal } from "./errors.ts";
3636
import { firstChange, sampleKey, toSample, withheldSample } from "./misses.ts";
3737
import {
3838
keyInWorker,
@@ -296,11 +296,9 @@ export function createRecorder(config: RecorderConfig, store: RecordingStore) {
296296
knownTotal.chars + chars > maxKnown.chars
297297
) {
298298
if (!closed) config.endRun("its sessions have too many changing values");
299-
throw Object.assign(
300-
new Error(
301-
`the sessions of this run have over ${maxKnown.values} changing values or ${maxKnown.chars} characters of them. This run has ended.`,
302-
),
303-
{ status: 400 },
299+
throw httpError(
300+
400,
301+
`the sessions of this run have over ${maxKnown.values} changing values or ${maxKnown.chars} characters of them. This run has ended.`,
304302
);
305303
}
306304
for (const [value, name] of added) session.known.set(value, name);
@@ -784,9 +782,9 @@ export function createRecorder(config: RecorderConfig, store: RecordingStore) {
784782
await finish(same, false);
785783
}
786784
if (open().length >= MAX_OPEN_SESSIONS) {
787-
throw Object.assign(
788-
new Error(`A run can have ${MAX_OPEN_SESSIONS} open sessions`),
789-
{ status: 429 },
785+
throw httpError(
786+
429,
787+
`A run can have ${MAX_OPEN_SESSIONS} open sessions`,
790788
);
791789
}
792790
const ended = [...sessions.values()].filter(
@@ -818,9 +816,7 @@ export function createRecorder(config: RecorderConfig, store: RecordingStore) {
818816
async endSession(name: string, passed: boolean): Promise<number> {
819817
const ended = open().find((other) => other.name === name);
820818
if (!ended) {
821-
throw Object.assign(new Error(`Session "${name}" is not open`), {
822-
status: 409,
823-
});
819+
throw httpError(409, `Session "${name}" is not open`);
824820
}
825821
return finish(ended, passed);
826822
},

‎packages/service/src/request-key.ts‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
import { createHash } from "node:crypto";
1616
import type { IncomingHttpHeaders } from "node:http";
17+
import { parseJson } from "./json-strings.ts";
1718
import { THINKING_BLOCK_TYPES } from "./streams.ts";
1819
import type { RecordingRule } from "@sentry/roach/types";
1920
import {
@@ -92,14 +93,6 @@ function stableStringify(
9293
.join(",")}}`;
9394
}
9495

95-
function parseJson(body: string): unknown {
96-
try {
97-
return body ? JSON.parse(body) : undefined;
98-
} catch {
99-
return undefined;
100-
}
101-
}
102-
10396
/** Marks the place of a thinking block in the text of a body. */
10497
const THINKING_MARK = /"\uE000(\d+)"/g;
10598

‎packages/service/src/secrets.ts‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
* or a copy of a value inside other text.
2222
*/
2323
import type { IncomingHttpHeaders } from "node:http";
24+
import { httpError } from "./errors.ts";
2425
import {
25-
parseJson,
26+
parseJsonSkipBom,
27+
isObject,
2628
isOpen,
2729
isJsonSpace,
2830
type Escapes,
@@ -672,10 +674,6 @@ function bodyStart(text: string, href: string, keyHeaders: string[]): number {
672674
return at;
673675
}
674676

675-
/** Tells whether a parsed JSON value is an object that is not an array. */
676-
const isObject = (json: unknown): json is Record<string, unknown> =>
677-
json !== null && typeof json === "object" && !Array.isArray(json);
678-
679677
/** Tells whether the field `name` of `json` holds a minted value. */
680678
const mintedValue = (json: Record<string, unknown>, name: string) => {
681679
const value = json[name];
@@ -691,7 +689,7 @@ const mintedValue = (json: Record<string, unknown>, name: string) => {
691689
* result, so the caller does not record the body.
692690
*/
693691
function replaceMinted(text: string): { text: string; minted: string[] } {
694-
const json = parseJson(text);
692+
const json = parseJsonSkipBom(text);
695693
const none = { text, minted: [] };
696694
if (!isObject(json)) return none;
697695
// The fields of the top object that can hold a minted value.
@@ -726,7 +724,7 @@ function replaceMinted(text: string): { text: string; minted: string[] } {
726724
// Parse the result again, and fail when a minted value stays. The pass
727725
// replaces only whole strings with strings, so the names of the fields
728726
// do not change. Thus only the names from the first parse can hold one.
729-
const again = parseJson(result);
727+
const again = parseJsonSkipBom(result);
730728
if (!isObject(again) || names.some((name) => mintedValue(again, name))) {
731729
throw new Error("a minted credential stayed in the body");
732730
}
@@ -892,7 +890,7 @@ export function toRedact(
892890
*/
893891
function bodyFields(request: RequestToRedact) {
894892
const form = isForm(request.contentType);
895-
const json = form ? undefined : parseJson(request.body);
893+
const json = form ? undefined : parseJsonSkipBom(request.body);
896894
const fields = form ? formFields(request.body) : jsonFields(json);
897895
return { form, json, fields, isValue: valueTest(fields.values) };
898896
}
@@ -1001,7 +999,7 @@ export function redactRequestWith(
1001999
}
10021000
const known = new Set(fields.values);
10031001
const body = redactWith(values, replaceStrings(result.slice(at), known));
1004-
const parsed = parseJson(body);
1002+
const parsed = parseJsonSkipBom(body);
10051003
if (
10061004
parsed === undefined ||
10071005
!showsNoField(parsed, known) ||
@@ -1045,11 +1043,9 @@ export function createSecrets(initial: string[], limit: LearnLimit) {
10451043
next.length += part.length;
10461044
}
10471045
if (next.values > limit.values || next.length > limit.length) {
1048-
throw Object.assign(
1049-
new Error(
1050-
`this run cannot learn more credentials (${limit.values} values or ${limit.length} characters, with the parts of each value). Start a new run.`,
1051-
),
1052-
{ status },
1046+
throw httpError(
1047+
status,
1048+
`this run cannot learn more credentials (${limit.values} values or ${limit.length} characters, with the parts of each value). Start a new run.`,
10531049
);
10541050
}
10551051
learned = next;

‎packages/service/src/usage.ts‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,16 @@
1515
* As in Sentry, `gen_ai.usage.input_tokens` includes the cache tokens.
1616
* Anthropic counts them apart, so the proxy adds them.
1717
*/
18+
import { isObject, parseJson } from "./json-strings.ts";
1819
import { isEventStream } from "./streams.ts";
1920

2021
type Fields = Record<string, unknown>;
2122

22-
const isObject = (value: unknown): value is Fields =>
23-
typeof value === "object" && value !== null && !Array.isArray(value);
24-
2523
const count = (value: unknown) =>
2624
typeof value === "number" && Number.isFinite(value) && value >= 0
2725
? value
2826
: undefined;
2927

30-
function parseJson(text: string): unknown {
31-
try {
32-
return JSON.parse(text);
33-
} catch {
34-
return undefined;
35-
}
36-
}
37-
3828
/** The JSON objects of a body: the body itself, or each stream event. */
3929
function bodyObjects(contentType: string | undefined, body: string): Fields[] {
4030
const values = isEventStream(contentType)

0 commit comments

Comments
 (0)