@assemble-dev/shared-utils 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.
@@ -0,0 +1,230 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/redaction/index.ts
21
+ var redaction_exports = {};
22
+ __export(redaction_exports, {
23
+ RedactTracePayloadOptionsSchema: () => RedactTracePayloadOptionsSchema,
24
+ RedactionModeSchema: () => RedactionModeSchema,
25
+ defaultRedactionMode: () => defaultRedactionMode,
26
+ isBearerTokenShapedString: () => isBearerTokenShapedString,
27
+ isJwtShapedString: () => isJwtShapedString,
28
+ isProviderApiKeyShapedString: () => isProviderApiKeyShapedString,
29
+ isSecretNamedField: () => isSecretNamedField,
30
+ isSecretShapedString: () => isSecretShapedString,
31
+ redactTracePayload: () => redactTracePayload,
32
+ redactedValue: () => redactedValue,
33
+ replaceProviderApiKeySubstrings: () => replaceProviderApiKeySubstrings,
34
+ replaceSecretShapedSubstrings: () => replaceSecretShapedSubstrings,
35
+ resolveRedactionMode: () => resolveRedactionMode,
36
+ summarizeJsonValue: () => summarizeJsonValue
37
+ });
38
+ module.exports = __toCommonJS(redaction_exports);
39
+
40
+ // src/redaction/redaction-mode.ts
41
+ var import_zod = require("zod");
42
+ var RedactionModeSchema = import_zod.z.enum([
43
+ "metadata-only",
44
+ "redacted",
45
+ "full"
46
+ ]);
47
+ var defaultRedactionMode = "redacted";
48
+ function resolveRedactionMode(mode) {
49
+ return mode ?? defaultRedactionMode;
50
+ }
51
+
52
+ // src/redaction/secret-detection.ts
53
+ var redactedValue = "[REDACTED]";
54
+ var providerApiKeyPrefixes = [
55
+ "sk-",
56
+ "sk_live_",
57
+ "sk_test_",
58
+ "asm_",
59
+ "ghp_",
60
+ "gho_",
61
+ "ghs_",
62
+ "ghu_",
63
+ "github_pat_",
64
+ "xoxb-",
65
+ "xoxp-",
66
+ "xoxa-",
67
+ "xoxs-",
68
+ "pk_live_",
69
+ "rk_live_",
70
+ "whsec_",
71
+ "AKIA"
72
+ ];
73
+ var secretBodyPattern = /^[A-Za-z0-9+/=_.-]{12,}$/;
74
+ var bearerTokenPattern = /^bearer\s+\S+/i;
75
+ var jwtShapedPattern = /^eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/;
76
+ var secretFieldNameSuffixes = [
77
+ "api_key",
78
+ "apikey",
79
+ "secret",
80
+ "token",
81
+ "password"
82
+ ];
83
+ function escapeRegExpLiteral(literal) {
84
+ return literal.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
85
+ }
86
+ function buildProviderApiKeySubstringPattern() {
87
+ const escapedPrefixes = providerApiKeyPrefixes.map(escapeRegExpLiteral).join("|");
88
+ return new RegExp(`(?:${escapedPrefixes})[A-Za-z0-9+/=_.-]{12,}`, "g");
89
+ }
90
+ var providerApiKeySubstringPattern = buildProviderApiKeySubstringPattern();
91
+ var bearerTokenSubstringPattern = /bearer\s+\S+/gi;
92
+ var jwtShapedSubstringPattern = /eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}[A-Za-z0-9_.-]*/g;
93
+ function replaceProviderApiKeySubstrings(value) {
94
+ return value.replace(providerApiKeySubstringPattern, redactedValue);
95
+ }
96
+ function replaceSecretShapedSubstrings(value) {
97
+ return replaceProviderApiKeySubstrings(value).replace(bearerTokenSubstringPattern, redactedValue).replace(jwtShapedSubstringPattern, redactedValue);
98
+ }
99
+ function isProviderApiKeyShapedString(value) {
100
+ return providerApiKeyPrefixes.some(
101
+ (prefix) => value.startsWith(prefix) && secretBodyPattern.test(value.slice(prefix.length))
102
+ );
103
+ }
104
+ function isBearerTokenShapedString(value) {
105
+ return bearerTokenPattern.test(value);
106
+ }
107
+ function isJwtShapedString(value) {
108
+ return jwtShapedPattern.test(value);
109
+ }
110
+ function isSecretShapedString(value) {
111
+ return isProviderApiKeyShapedString(value) || isBearerTokenShapedString(value) || isJwtShapedString(value);
112
+ }
113
+ function isSecretNamedField(fieldName) {
114
+ const normalizedFieldName = fieldName.toLowerCase();
115
+ if (normalizedFieldName === "authorization") {
116
+ return true;
117
+ }
118
+ return secretFieldNameSuffixes.some(
119
+ (suffix) => normalizedFieldName.endsWith(suffix)
120
+ );
121
+ }
122
+
123
+ // src/redaction/summarize-json-value.ts
124
+ function summarizeJsonValue(value) {
125
+ if (value === null) {
126
+ return { kind: "null" };
127
+ }
128
+ if (typeof value === "string") {
129
+ return { kind: "string", length: value.length };
130
+ }
131
+ if (typeof value === "number") {
132
+ return { kind: "number" };
133
+ }
134
+ if (typeof value === "boolean") {
135
+ return { kind: "boolean" };
136
+ }
137
+ if (Array.isArray(value)) {
138
+ return { kind: "array", length: value.length };
139
+ }
140
+ return { kind: "object", keys: Object.keys(value) };
141
+ }
142
+
143
+ // src/redaction/redact-trace-payload.ts
144
+ var import_zod2 = require("zod");
145
+ var RedactTracePayloadOptionsSchema = import_zod2.z.object({
146
+ mode: RedactionModeSchema.optional(),
147
+ sensitiveFieldNames: import_zod2.z.array(import_zod2.z.string()).optional()
148
+ });
149
+ function redactTracePayload(payload, options) {
150
+ const mode = resolveRedactionMode(options?.mode);
151
+ if (mode === "metadata-only") {
152
+ return { payload: summarizeJsonValue(payload), redacted: true };
153
+ }
154
+ const context = {
155
+ mode,
156
+ sensitiveFieldNames: buildSensitiveFieldNameSet(
157
+ options?.sensitiveFieldNames
158
+ ),
159
+ redactedValueCount: 0
160
+ };
161
+ const redactedPayload = redactJsonValue(payload, context);
162
+ return {
163
+ payload: redactedPayload,
164
+ redacted: mode !== "full" || context.redactedValueCount > 0
165
+ };
166
+ }
167
+ function buildSensitiveFieldNameSet(fieldNames) {
168
+ return new Set(
169
+ (fieldNames ?? []).map((fieldName) => fieldName.toLowerCase())
170
+ );
171
+ }
172
+ function redactJsonValue(value, context) {
173
+ if (typeof value === "string") {
174
+ return redactStringValue(value, context);
175
+ }
176
+ if (Array.isArray(value)) {
177
+ return value.map((item) => redactJsonValue(item, context));
178
+ }
179
+ if (value !== null && typeof value === "object") {
180
+ return redactObjectValue(value, context);
181
+ }
182
+ return value;
183
+ }
184
+ function redactStringValue(value, context) {
185
+ const shouldRedactWholeString = context.mode === "full" ? isProviderApiKeyShapedString(value) : isSecretShapedString(value);
186
+ if (shouldRedactWholeString) {
187
+ context.redactedValueCount += 1;
188
+ return redactedValue;
189
+ }
190
+ const valueWithSecretsReplaced = context.mode === "full" ? replaceProviderApiKeySubstrings(value) : replaceSecretShapedSubstrings(value);
191
+ if (valueWithSecretsReplaced !== value) {
192
+ context.redactedValueCount += 1;
193
+ }
194
+ return valueWithSecretsReplaced;
195
+ }
196
+ function redactObjectValue(value, context) {
197
+ const redactedEntries = Object.entries(value).map(
198
+ ([fieldName, fieldValue]) => {
199
+ if (shouldRedactFieldValue(fieldName, context)) {
200
+ context.redactedValueCount += 1;
201
+ return [fieldName, redactedValue];
202
+ }
203
+ return [fieldName, redactJsonValue(fieldValue, context)];
204
+ }
205
+ );
206
+ return Object.fromEntries(redactedEntries);
207
+ }
208
+ function shouldRedactFieldValue(fieldName, context) {
209
+ if (context.sensitiveFieldNames.has(fieldName.toLowerCase())) {
210
+ return true;
211
+ }
212
+ return context.mode === "redacted" && isSecretNamedField(fieldName);
213
+ }
214
+ // Annotate the CommonJS export names for ESM import in node:
215
+ 0 && (module.exports = {
216
+ RedactTracePayloadOptionsSchema,
217
+ RedactionModeSchema,
218
+ defaultRedactionMode,
219
+ isBearerTokenShapedString,
220
+ isJwtShapedString,
221
+ isProviderApiKeyShapedString,
222
+ isSecretNamedField,
223
+ isSecretShapedString,
224
+ redactTracePayload,
225
+ redactedValue,
226
+ replaceProviderApiKeySubstrings,
227
+ replaceSecretShapedSubstrings,
228
+ resolveRedactionMode,
229
+ summarizeJsonValue
230
+ });
@@ -0,0 +1,55 @@
1
+ import { z } from 'zod';
2
+ import { JsonValue } from '@assemble-dev/shared-types/json';
3
+
4
+ declare const RedactionModeSchema: z.ZodEnum<{
5
+ "metadata-only": "metadata-only";
6
+ redacted: "redacted";
7
+ full: "full";
8
+ }>;
9
+ type RedactionMode = z.infer<typeof RedactionModeSchema>;
10
+ declare const defaultRedactionMode: RedactionMode;
11
+ declare function resolveRedactionMode(mode: RedactionMode | undefined): RedactionMode;
12
+
13
+ declare const redactedValue = "[REDACTED]";
14
+ declare function replaceProviderApiKeySubstrings(value: string): string;
15
+ declare function replaceSecretShapedSubstrings(value: string): string;
16
+ declare function isProviderApiKeyShapedString(value: string): boolean;
17
+ declare function isBearerTokenShapedString(value: string): boolean;
18
+ declare function isJwtShapedString(value: string): boolean;
19
+ declare function isSecretShapedString(value: string): boolean;
20
+ declare function isSecretNamedField(fieldName: string): boolean;
21
+
22
+ type JsonValueSummary = {
23
+ kind: "string";
24
+ length: number;
25
+ } | {
26
+ kind: "number";
27
+ } | {
28
+ kind: "boolean";
29
+ } | {
30
+ kind: "null";
31
+ } | {
32
+ kind: "array";
33
+ length: number;
34
+ } | {
35
+ kind: "object";
36
+ keys: string[];
37
+ };
38
+ declare function summarizeJsonValue(value: JsonValue): JsonValueSummary;
39
+
40
+ declare const RedactTracePayloadOptionsSchema: z.ZodObject<{
41
+ mode: z.ZodOptional<z.ZodEnum<{
42
+ "metadata-only": "metadata-only";
43
+ redacted: "redacted";
44
+ full: "full";
45
+ }>>;
46
+ sensitiveFieldNames: z.ZodOptional<z.ZodArray<z.ZodString>>;
47
+ }, z.core.$strip>;
48
+ type RedactTracePayloadOptions = z.infer<typeof RedactTracePayloadOptionsSchema>;
49
+ type RedactTracePayloadResult = {
50
+ payload: JsonValue;
51
+ redacted: boolean;
52
+ };
53
+ declare function redactTracePayload(payload: JsonValue, options?: RedactTracePayloadOptions): RedactTracePayloadResult;
54
+
55
+ export { type JsonValueSummary, type RedactTracePayloadOptions, RedactTracePayloadOptionsSchema, type RedactTracePayloadResult, type RedactionMode, RedactionModeSchema, defaultRedactionMode, isBearerTokenShapedString, isJwtShapedString, isProviderApiKeyShapedString, isSecretNamedField, isSecretShapedString, redactTracePayload, redactedValue, replaceProviderApiKeySubstrings, replaceSecretShapedSubstrings, resolveRedactionMode, summarizeJsonValue };
@@ -0,0 +1,55 @@
1
+ import { z } from 'zod';
2
+ import { JsonValue } from '@assemble-dev/shared-types/json';
3
+
4
+ declare const RedactionModeSchema: z.ZodEnum<{
5
+ "metadata-only": "metadata-only";
6
+ redacted: "redacted";
7
+ full: "full";
8
+ }>;
9
+ type RedactionMode = z.infer<typeof RedactionModeSchema>;
10
+ declare const defaultRedactionMode: RedactionMode;
11
+ declare function resolveRedactionMode(mode: RedactionMode | undefined): RedactionMode;
12
+
13
+ declare const redactedValue = "[REDACTED]";
14
+ declare function replaceProviderApiKeySubstrings(value: string): string;
15
+ declare function replaceSecretShapedSubstrings(value: string): string;
16
+ declare function isProviderApiKeyShapedString(value: string): boolean;
17
+ declare function isBearerTokenShapedString(value: string): boolean;
18
+ declare function isJwtShapedString(value: string): boolean;
19
+ declare function isSecretShapedString(value: string): boolean;
20
+ declare function isSecretNamedField(fieldName: string): boolean;
21
+
22
+ type JsonValueSummary = {
23
+ kind: "string";
24
+ length: number;
25
+ } | {
26
+ kind: "number";
27
+ } | {
28
+ kind: "boolean";
29
+ } | {
30
+ kind: "null";
31
+ } | {
32
+ kind: "array";
33
+ length: number;
34
+ } | {
35
+ kind: "object";
36
+ keys: string[];
37
+ };
38
+ declare function summarizeJsonValue(value: JsonValue): JsonValueSummary;
39
+
40
+ declare const RedactTracePayloadOptionsSchema: z.ZodObject<{
41
+ mode: z.ZodOptional<z.ZodEnum<{
42
+ "metadata-only": "metadata-only";
43
+ redacted: "redacted";
44
+ full: "full";
45
+ }>>;
46
+ sensitiveFieldNames: z.ZodOptional<z.ZodArray<z.ZodString>>;
47
+ }, z.core.$strip>;
48
+ type RedactTracePayloadOptions = z.infer<typeof RedactTracePayloadOptionsSchema>;
49
+ type RedactTracePayloadResult = {
50
+ payload: JsonValue;
51
+ redacted: boolean;
52
+ };
53
+ declare function redactTracePayload(payload: JsonValue, options?: RedactTracePayloadOptions): RedactTracePayloadResult;
54
+
55
+ export { type JsonValueSummary, type RedactTracePayloadOptions, RedactTracePayloadOptionsSchema, type RedactTracePayloadResult, type RedactionMode, RedactionModeSchema, defaultRedactionMode, isBearerTokenShapedString, isJwtShapedString, isProviderApiKeyShapedString, isSecretNamedField, isSecretShapedString, redactTracePayload, redactedValue, replaceProviderApiKeySubstrings, replaceSecretShapedSubstrings, resolveRedactionMode, summarizeJsonValue };
@@ -0,0 +1,32 @@
1
+ import {
2
+ RedactTracePayloadOptionsSchema,
3
+ RedactionModeSchema,
4
+ defaultRedactionMode,
5
+ isBearerTokenShapedString,
6
+ isJwtShapedString,
7
+ isProviderApiKeyShapedString,
8
+ isSecretNamedField,
9
+ isSecretShapedString,
10
+ redactTracePayload,
11
+ redactedValue,
12
+ replaceProviderApiKeySubstrings,
13
+ replaceSecretShapedSubstrings,
14
+ resolveRedactionMode,
15
+ summarizeJsonValue
16
+ } from "../chunk-XDYQIW3Q.js";
17
+ export {
18
+ RedactTracePayloadOptionsSchema,
19
+ RedactionModeSchema,
20
+ defaultRedactionMode,
21
+ isBearerTokenShapedString,
22
+ isJwtShapedString,
23
+ isProviderApiKeyShapedString,
24
+ isSecretNamedField,
25
+ isSecretShapedString,
26
+ redactTracePayload,
27
+ redactedValue,
28
+ replaceProviderApiKeySubstrings,
29
+ replaceSecretShapedSubstrings,
30
+ resolveRedactionMode,
31
+ summarizeJsonValue
32
+ };
package/package.json ADDED
@@ -0,0 +1,78 @@
1
+ {
2
+ "name": "@assemble-dev/shared-utils",
3
+ "version": "0.1.0",
4
+ "license": "Elastic-2.0",
5
+ "description": "Shared utilities for Assemble including errors, ids, auth, and redaction",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/smundhra-git/assemble.git",
9
+ "directory": "packages/shared-utils"
10
+ },
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "type": "module",
15
+ "main": "./dist/index.cjs",
16
+ "module": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.js",
22
+ "require": "./dist/index.cjs"
23
+ },
24
+ "./api-keys": {
25
+ "types": "./dist/api-keys/index.d.ts",
26
+ "import": "./dist/api-keys/index.js",
27
+ "require": "./dist/api-keys/index.cjs"
28
+ },
29
+ "./auth": {
30
+ "types": "./dist/auth/index.d.ts",
31
+ "import": "./dist/auth/index.js",
32
+ "require": "./dist/auth/index.cjs"
33
+ },
34
+ "./errors": {
35
+ "types": "./dist/errors/index.d.ts",
36
+ "import": "./dist/errors/index.js",
37
+ "require": "./dist/errors/index.cjs"
38
+ },
39
+ "./ids": {
40
+ "types": "./dist/ids/index.d.ts",
41
+ "import": "./dist/ids/index.js",
42
+ "require": "./dist/ids/index.cjs"
43
+ },
44
+ "./provider-keys": {
45
+ "types": "./dist/provider-keys/index.d.ts",
46
+ "import": "./dist/provider-keys/index.js",
47
+ "require": "./dist/provider-keys/index.cjs"
48
+ },
49
+ "./redaction": {
50
+ "types": "./dist/redaction/index.d.ts",
51
+ "import": "./dist/redaction/index.js",
52
+ "require": "./dist/redaction/index.cjs"
53
+ }
54
+ },
55
+ "files": [
56
+ "dist"
57
+ ],
58
+ "dependencies": {
59
+ "hono": "^4.12.27",
60
+ "jose": "^6.2.3",
61
+ "zod": "^4.1.13",
62
+ "@assemble-dev/shared-types": "0.1.0"
63
+ },
64
+ "devDependencies": {
65
+ "@types/node": "^20.19.0",
66
+ "eslint": "^9.30.0",
67
+ "tsup": "^8.5.0",
68
+ "typescript": "^5.8.3",
69
+ "vitest": "^3.2.4",
70
+ "@assemble-dev/config": "0.0.0"
71
+ },
72
+ "scripts": {
73
+ "build": "tsup",
74
+ "test": "vitest run",
75
+ "lint": "eslint src",
76
+ "typecheck": "tsc --noEmit"
77
+ }
78
+ }