@imbue-ai/detent 1.4.0 → 1.6.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/README.md CHANGED
@@ -97,8 +97,9 @@ environment variable to specify a different path.
97
97
 
98
98
  An HTTP(s) request can be represented as an object that has
99
99
  several well-defined properties: `protocol`, `domain`, `port`,
100
- `path`, `method`, `headers`, `queryParams` and `body`. Using this
101
- representation, the `detent` tool uses [JSON schema](https://json-schema.org/) to:
100
+ `path`, `method`, `headers`, `queryParams`, `body` and
101
+ `parsedBody`. Using this representation, the `detent` tool uses
102
+ [JSON schema](https://json-schema.org/) to:
102
103
 
103
104
  1. Match requests to permission checks.
104
105
  2. Define the "acceptable" shape of a request that is subject to a permission check.
@@ -107,6 +108,14 @@ Some of the fields are normalized to canonical form before matching:
107
108
  `method` is always uppercase (e.g. `"GET"`), `protocol`,
108
109
  `domain` and `headers` keys are always lowercase (e.g. `"content-type"`).
109
110
 
111
+ `body` holds the raw request body as a string. `parsedBody` holds
112
+ a structured representation of the body and is only present when
113
+ the body can be parsed, which currently happens for JSON bodies
114
+ (content type `application/json` or `*+json`). Matching against
115
+ `parsedBody` lets schemas inspect individual fields of a request
116
+ body without resorting to brittle regular expressions. Support
117
+ for other body formats (e.g. XML, GraphQL) may be added later.
118
+
110
119
  #### Request schemas
111
120
 
112
121
  We use JSON schema for matching and validating request objects. For example:
@@ -7,7 +7,7 @@
7
7
  * - method: UPPERCASE (e.g. "GET")
8
8
  * - headers: keys are lowercase (e.g. "content-type")
9
9
  */
10
- declare const decomposedRequestFieldTypes: {
10
+ declare const decomposedRequestCoreFieldTypes: {
11
11
  readonly protocol: string;
12
12
  readonly domain: string;
13
13
  readonly port: number;
@@ -17,7 +17,14 @@ declare const decomposedRequestFieldTypes: {
17
17
  readonly queryParams: Readonly<Record<string, string>>;
18
18
  readonly body: string | undefined;
19
19
  };
20
- export type DecomposedRequest = Readonly<typeof decomposedRequestFieldTypes>;
20
+ /**
21
+ * The structured form of the body. Only present when the raw body could be
22
+ * parsed into a structured value, which currently happens for JSON request
23
+ * bodies. Other content types (e.g. XML, GraphQL) may be supported later.
24
+ */
25
+ export type DecomposedRequest = Readonly<typeof decomposedRequestCoreFieldTypes> & {
26
+ readonly parsedBody?: unknown;
27
+ };
21
28
  export declare const decomposedRequestPropertyNames: ReadonlySet<string>;
22
29
  export declare function decomposeRequest(request: Request): Promise<DecomposedRequest>;
23
30
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"decomposedRequest.d.ts","sourceRoot":"","sources":["../../src/decomposedRequest.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,QAAA,MAAM,2BAA2B;uBACf,MAAM;qBACR,MAAM;mBACT,MAAM;mBACL,MAAM;qBACJ,MAAM;sBACL,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;0BAC5B,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;mBAChC,MAAM,GAAG,SAAS;CACK,CAAC;AAE7C,MAAM,MAAM,iBAAiB,GAAG,QAAQ,CAAC,OAAO,2BAA2B,CAAC,CAAC;AAE7E,eAAO,MAAM,8BAA8B,EAAE,WAAW,CAAC,MAAM,CAE9D,CAAC;AAEF,wBAAsB,gBAAgB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAkCnF"}
1
+ {"version":3,"file":"decomposedRequest.d.ts","sourceRoot":"","sources":["../../src/decomposedRequest.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,QAAA,MAAM,+BAA+B;uBACnB,MAAM;qBACR,MAAM;mBACT,MAAM;mBACL,MAAM;qBACJ,MAAM;sBACL,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;0BAC5B,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;mBAChC,MAAM,GAAG,SAAS;CACK,CAAC;AAE7C;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,GAAG,QAAQ,CAAC,OAAO,+BAA+B,CAAC,GAAG;IACjF,QAAQ,CAAC,UAAU,CAAC,EAAE,OAAO,CAAC;CAC/B,CAAC;AAEF,eAAO,MAAM,8BAA8B,EAAE,WAAW,CAAC,MAAM,CAG7D,CAAC;AAkBH,wBAAsB,gBAAgB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAuCnF"}
@@ -7,7 +7,7 @@
7
7
  * - method: UPPERCASE (e.g. "GET")
8
8
  * - headers: keys are lowercase (e.g. "content-type")
9
9
  */
10
- const decomposedRequestFieldTypes = {
10
+ const decomposedRequestCoreFieldTypes = {
11
11
  protocol: '',
12
12
  domain: '',
13
13
  port: 0,
@@ -17,7 +17,25 @@ const decomposedRequestFieldTypes = {
17
17
  queryParams: {},
18
18
  body: undefined,
19
19
  };
20
- export const decomposedRequestPropertyNames = new Set(Object.keys(decomposedRequestFieldTypes));
20
+ export const decomposedRequestPropertyNames = new Set([
21
+ ...Object.keys(decomposedRequestCoreFieldTypes),
22
+ 'parsedBody',
23
+ ]);
24
+ function isJsonContentType(contentType) {
25
+ if (contentType === undefined) {
26
+ return false;
27
+ }
28
+ const mediaType = contentType.split(';', 1)[0].trim().toLowerCase();
29
+ return mediaType === 'application/json' || mediaType.endsWith('+json');
30
+ }
31
+ function tryParseJson(text) {
32
+ try {
33
+ return JSON.parse(text);
34
+ }
35
+ catch {
36
+ return undefined;
37
+ }
38
+ }
21
39
  export async function decomposeRequest(request) {
22
40
  const url = new URL(request.url);
23
41
  const headers = {};
@@ -33,9 +51,13 @@ export async function decomposeRequest(request) {
33
51
  const defaultPort = protocol === 'https' ? 443 : 80;
34
52
  const port = url.port === '' ? defaultPort : Number(url.port);
35
53
  let body;
54
+ let parsedBody;
36
55
  if (request.body !== null) {
37
56
  const text = await request.clone().text();
38
57
  body = text === '' ? undefined : text;
58
+ if (body !== undefined && isJsonContentType(headers['content-type'])) {
59
+ parsedBody = tryParseJson(body);
60
+ }
39
61
  }
40
62
  return {
41
63
  protocol,
@@ -46,6 +68,7 @@ export async function decomposeRequest(request) {
46
68
  headers,
47
69
  queryParams,
48
70
  body,
71
+ ...(parsedBody === undefined ? {} : { parsedBody }),
49
72
  };
50
73
  }
51
74
  //# sourceMappingURL=decomposedRequest.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"decomposedRequest.js","sourceRoot":"","sources":["../../src/decomposedRequest.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,MAAM,2BAA2B,GAAG;IAClC,QAAQ,EAAE,EAAY;IACtB,MAAM,EAAE,EAAY;IACpB,IAAI,EAAE,CAAW;IACjB,IAAI,EAAE,EAAY;IAClB,MAAM,EAAE,EAAY;IACpB,OAAO,EAAE,EAAsC;IAC/C,WAAW,EAAE,EAAsC;IACnD,IAAI,EAAE,SAA+B;CACK,CAAC;AAI7C,MAAM,CAAC,MAAM,8BAA8B,GAAwB,IAAI,GAAG,CACxE,MAAM,CAAC,IAAI,CAAC,2BAA2B,CAAC,CACzC,CAAC;AAEF,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAAC,OAAgB;IACrD,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAEjC,MAAM,OAAO,GAA2B,EAAE,CAAC;IAC3C,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;QACrC,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,GAAG,KAAK,CAAC;IACrC,CAAC,CAAC,CAAC;IAEH,MAAM,WAAW,GAA2B,EAAE,CAAC;IAC/C,GAAG,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;QACtC,WAAW,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IAC3B,CAAC,CAAC,CAAC;IAEH,MAAM,QAAQ,G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AE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAi4K5E,CAAC"}
1
+ {"version":3,"file":"generatedBuiltinSchemas.d.ts","sourceRoot":"","sources":["../../../src/schemas/generatedBuiltinSchemas.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,uBAAuB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAkwM5E,CAAC"}