@bessemer/cornerstone 0.5.55 → 0.5.56

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.
@@ -37,8 +37,9 @@ export declare const isErrorEvent: (throwable: Throwable) => throwable is ErrorE
37
37
  export declare const isErrorEventException: (throwable: Throwable) => throwable is ErrorEventException;
38
38
  export declare const of: (builder: ErrorEventBuilder) => ErrorEvent;
39
39
  export declare const from: (throwable: Throwable) => ErrorEvent;
40
- export declare function propagate<ReturnType>(runnable: () => ReturnType, attributes: LazyValue<Dictionary<unknown>>): ReturnType;
41
- export declare function propagate<ReturnType>(resolver: () => Promise<ReturnType>, attributes: LazyValue<Dictionary<unknown>>): Promise<ReturnType>;
40
+ export declare function withPropagation<ReturnType>(runnable: () => ReturnType, attributes: LazyValue<Dictionary<unknown>>): ReturnType;
41
+ export declare function withPropagation<ReturnType>(resolver: () => Promise<ReturnType>, attributes: LazyValue<Dictionary<unknown>>): Promise<ReturnType>;
42
+ export declare const propagate: (throwable: Throwable, attributes: LazyValue<Dictionary<unknown>>) => never;
42
43
  export declare const findByCodeInCausalChain: (error: ErrorEvent, code: string) => ErrorEvent | undefined;
43
44
  export declare const findInCausalChain: (error: ErrorEvent, predicate: (error: ErrorEvent) => boolean) => ErrorEvent | undefined;
44
45
  export declare const aggregate: (builder: ErrorEventBuilder, causes: Array<ErrorEvent>) => ErrorEvent | undefined;
@@ -1 +1 @@
1
- {"version":3,"file":"error-event.d.ts","sourceRoot":"","sources":["../src/error-event.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAA;AAChF,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAA;AAC9D,OAAO,GAAG,EAAE,EAAE,OAAO,EAAE,MAAM,KAAK,CAAA;AAClC,OAAO,EAAY,SAAS,EAAE,MAAM,4BAA4B,CAAA;AAOhE,MAAM,MAAM,SAAS,GAAG,W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,MAAM,qBAAqB,EAAE,SAAsC,CAAA;AAC1E,eAAO,MAAM,mBAAmB,EAAE,SAAqC,CAAA;AAEvE,eAAO,MAAM,6BAA6B,EAAE,cAAc,CAAC,MAAM,CAA0B,CAAA;AAC3F,eAAO,MAAM,uBAAuB,EAAE,cAAc,CAAC,MAAM,CAAoB,CAAA;AAE/E,eAAO,MAAM,SAAS,GAAI,UAAU,iBAAiB,eAUlD,CAAA;AAEH,eAAO,MAAM,QAAQ,GAAI,UAAU,iBAAiB,eAUjD,CAAA;AAEH,eAAO,MAAM,YAAY,GAAI,UAAU,iBAAiB,eAUrD,CAAA;AAEH,eAAO,MAAM,SAAS,GAAI,UAAU,iBAAiB,eAUlD,CAAA;AAEH,eAAO,MAAM,UAAU,GAAI,UAAU,iBAAiB,eAUnD,CAAA"}
@@ -51,29 +51,29 @@ var from = (throwable) => {
51
51
  }
52
52
  return errorEventException.errorEvent;
53
53
  };
54
- function propagate(resolver, attributes) {
55
- const handleError = (error) => {
56
- if (isErrorEventException(error)) {
57
- const errorEvent = error.errorEvent;
58
- errorEvent.attributes = { ...errorEvent.attributes, ...evaluate(attributes) };
59
- throw error;
60
- } else {
61
- const errorEvent = from(error);
62
- const contextualizedEvent = of({ ...errorEvent, attributes: { ...errorEvent.attributes, ...evaluate(attributes) } });
63
- throw new ErrorEventException(contextualizedEvent, error);
64
- }
65
- };
54
+ function withPropagation(resolver, attributes) {
66
55
  try {
67
56
  let result = resolver();
68
57
  if (Promises.isPromise(result)) {
69
- return result.then((it) => it).catch((it) => handleError(it));
58
+ return result.then((it) => it).catch((it) => propagate(it, attributes));
70
59
  } else {
71
60
  return result;
72
61
  }
73
- } catch (error) {
74
- throw handleError(error);
62
+ } catch (throwable) {
63
+ throw propagate(throwable, attributes);
75
64
  }
76
65
  }
66
+ var propagate = (throwable, attributes) => {
67
+ if (isErrorEventException(throwable)) {
68
+ const errorEvent = throwable.errorEvent;
69
+ errorEvent.attributes = { ...errorEvent.attributes, ...evaluate(attributes) };
70
+ throw throwable;
71
+ } else {
72
+ const errorEvent = from(throwable);
73
+ const contextualizedEvent = of({ ...errorEvent, attributes: { ...errorEvent.attributes, ...evaluate(attributes) } });
74
+ throw new ErrorEventException(contextualizedEvent, throwable);
75
+ }
76
+ };
77
77
  var findByCodeInCausalChain = (error, code) => {
78
78
  return findInCausalChain(error, (it) => it.code === code);
79
79
  };
@@ -169,6 +169,7 @@ export {
169
169
  of,
170
170
  propagate,
171
171
  unauthorized,
172
- unhandled
172
+ unhandled,
173
+ withPropagation
173
174
  };
174
175
  //# sourceMappingURL=error-event.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/error-event.ts"],"sourcesContent":["import { Arrays, Errors, Objects, Promises } from '@bessemer/cornerstone'\nimport { Dictionary, NominalType, Throwable } from '@bessemer/cornerstone/types'\nimport { RecordAttribute } from '@bessemer/cornerstone/object'\nimport Zod, { ZodType } from 'zod'\nimport { evaluate, LazyValue } from '@bessemer/cornerstone/lazy'\n\n/*\n Represents a structured error event. The code can be mapped to a unique type of error while the\n message and attributes can provide contextual information about the error. Finally,\n an ErrorEvent could have multiple causes which get aggregated into a single parent error.\n */\nexport type ErrorCode = NominalType<string, 'ErrorCode'>\nexport const ErrorCodeSchema: ZodType<ErrorCode> = Zod.string()\n\nexport type ErrorAttribute<Type = unknown> = RecordAttribute<Type, 'ErrorAttribute'>\nexport const ErrorAttributeSchema: ZodType<ErrorAttribute> = Zod.string()\n\nconst baseErrorEventSchema = Zod.object({\n code: ErrorCodeSchema,\n message: Zod.string(),\n attributes: Zod.record(ErrorAttributeSchema, Zod.unknown()),\n})\n\nexport type ErrorEvent = Zod.infer<typeof baseErrorEventSchema> & {\n causes: Array<ErrorEvent>\n}\n\nconst ErrorEventSchema: ZodType<ErrorEvent> = baseErrorEventSchema.extend({\n causes: Zod.lazy(() => Zod.array(ErrorEventSchema)),\n})\n\n// Builder object that allows for 'partial' representation of ErrorEvents\nexport type ErrorEventBuilder = {\n code: ErrorCode\n message?: string | null\n attributes?: Record<ErrorAttribute, unknown>\n causes?: Array<ErrorEvent>\n}\n\nexport type ErrorEventAugment = Partial<ErrorEventBuilder>\n\n// An exception type that contains an ErrorEvent\nexport class ErrorEventException extends Error {\n readonly errorEvent: ErrorEvent\n\n constructor(errorEvent: ErrorEvent, cause?: unknown) {\n super(errorEvent.message ?? '', { cause })\n this.name = this.constructor.name\n this.errorEvent = errorEvent\n }\n}\n\nexport const isErrorEvent = (throwable: Throwable): throwable is ErrorEvent => {\n if (!Objects.isObject(throwable)) {\n return false\n }\n\n return 'code' in throwable && 'message' in throwable && 'attributes' in throwable && 'causes' in throwable\n}\n\nexport const isErrorEventException = (throwable: Throwable): throwable is ErrorEventException => {\n return throwable instanceof ErrorEventException\n}\n\nexport const of = (builder: ErrorEventBuilder): ErrorEvent => {\n const code = builder.code\n\n return {\n code,\n message: builder.message ?? code,\n attributes: builder.attributes ?? {},\n causes: builder.causes ?? [],\n }\n}\n\nexport const from = (throwable: Throwable): ErrorEvent => {\n if (isErrorEvent(throwable)) {\n return throwable\n }\n\n if (!Errors.isError(throwable)) {\n return unhandled()\n }\n\n const errorEventException = Errors.findInCausalChain(throwable, isErrorEventException) as ErrorEventException | undefined\n if (Objects.isNil(errorEventException)) {\n return unhandled()\n }\n\n return errorEventException.errorEvent\n}\n\nexport function propagate<ReturnType>(runnable: () => ReturnType, attributes: LazyValue<Dictionary<unknown>>): ReturnType\nexport function propagate<ReturnType>(resolver: () => Promise<ReturnType>, attributes: LazyValue<Dictionary<unknown>>): Promise<ReturnType>\nexport function propagate<ReturnType>(\n resolver: () => ReturnType | Promise<ReturnType>,\n attributes: LazyValue<Dictionary<unknown>>\n): ReturnType | Promise<ReturnType> {\n const handleError = (error: Throwable) => {\n if (isErrorEventException(error)) {\n // We just mutate the existing error event to avoid nested exceptions\n const errorEvent = error.errorEvent\n errorEvent.attributes = { ...errorEvent.attributes, ...evaluate(attributes) }\n throw error\n } else {\n const errorEvent = from(error)\n const contextualizedEvent = of({ ...errorEvent, attributes: { ...errorEvent.attributes, ...evaluate(attributes) } })\n throw new ErrorEventException(contextualizedEvent, error)\n }\n }\n\n try {\n let result = resolver()\n if (Promises.isPromise(result)) {\n return result.then((it) => it).catch((it) => handleError(it))\n } else {\n return result\n }\n } catch (error: Throwable) {\n throw handleError(error)\n }\n}\n\nexport const findByCodeInCausalChain = (error: ErrorEvent, code: string): ErrorEvent | undefined => {\n return findInCausalChain(error, (it) => it.code === code)\n}\n\n/*\n Traverses the causal chain of the ErrorEvent, searching for a predicate that matches (including matching on the parent error event)\n This is useful if you want to find whether or not a given error was caused by a specific failure. The search executes depth-first and\n will return te first matching instance that satisfies the predicate, or undefined otherwise\n */\nexport const findInCausalChain = (error: ErrorEvent, predicate: (error: ErrorEvent) => boolean): ErrorEvent | undefined => {\n if (predicate(error)) {\n return error\n }\n\n return Arrays.first(error.causes.map((it) => findInCausalChain(it, predicate)).filter(Objects.isPresent))\n}\n\nexport const aggregate = (builder: ErrorEventBuilder, causes: Array<ErrorEvent>): ErrorEvent | undefined => {\n if (causes.length === 0) {\n return undefined\n }\n\n return of({ ...builder, causes })\n}\n\nexport const UnhandledErrorCode: ErrorCode = 'error-event.unhandled'\nexport const NotFoundErrorCode: ErrorCode = 'error-event.not-found'\nexport const ForbiddenErrorCode: ErrorCode = 'error-event.forbidden'\nexport const UnauthorizedErrorCode: ErrorCode = 'error-event.unauthorized'\nexport const BadRequestErrorCode: ErrorCode = 'error-event.bad-request'\n\nexport const RequestCorrelationIdAttribute: ErrorAttribute<string> = 'requestCorrelationId'\nexport const HttpStatusCodeAttribute: ErrorAttribute<number> = 'httpStatusCode'\n\nexport const unhandled = (builder?: ErrorEventAugment) =>\n of(\n Objects.deepMerge(\n {\n code: UnhandledErrorCode,\n message: 'An Unhandled Error has occurred.',\n attributes: { [HttpStatusCodeAttribute]: 500 },\n },\n builder\n )\n )\n\nexport const notFound = (builder?: ErrorEventAugment) =>\n of(\n Objects.deepMerge(\n {\n code: NotFoundErrorCode,\n message: 'The requested Resource could not be found.',\n attributes: { [HttpStatusCodeAttribute]: 404 },\n },\n builder\n )\n )\n\nexport const unauthorized = (builder?: ErrorEventAugment) =>\n of(\n Objects.deepMerge(\n {\n code: UnauthorizedErrorCode,\n message: 'The requested Resource requires authentication.',\n attributes: { [HttpStatusCodeAttribute]: 401 },\n },\n builder\n )\n )\n\nexport const forbidden = (builder?: ErrorEventAugment) =>\n of(\n Objects.deepMerge(\n {\n code: ForbiddenErrorCode,\n message: 'The requested Resource requires additional permissions to access.',\n attributes: { [HttpStatusCodeAttribute]: 403 },\n },\n builder\n )\n )\n\nexport const badRequest = (builder?: ErrorEventAugment) =>\n of(\n Objects.deepMerge(\n {\n code: BadRequestErrorCode,\n message: 'The request is invalid and cannot be processed.',\n attributes: { [HttpStatusCodeAttribute]: 400 },\n },\n builder\n )\n )\n"],"mappings":";AAAA,SAAS,QAAQ,QAAQ,SAAS,gBAAgB;AAGlD,OAAO,SAAsB;AAC7B,SAAS,gBAA2B;AAQ7B,IAAM,kBAAsC,IAAI,OAAO;AAGvD,IAAM,uBAAgD,IAAI,OAAO;AAExE,IAAM,uBAAuB,IAAI,OAAO;AAAA,EACtC,MAAM;AAAA,EACN,SAAS,IAAI,OAAO;AAAA,EACpB,YAAY,IAAI,OAAO,sBAAsB,IAAI,QAAQ,CAAC;AAC5D,CAAC;AAMD,IAAM,mBAAwC,qBAAqB,OAAO;AAAA,EACxE,QAAQ,IAAI,KAAK,MAAM,IAAI,MAAM,gBAAgB,CAAC;AACpD,CAAC;AAaM,IAAM,sBAAN,cAAkC,MAAM;AAAA,EACpC;AAAA,EAET,YAAY,YAAwB,OAAiB;AACnD,UAAM,WAAW,WAAW,IAAI,EAAE,MAAM,CAAC;AACzC,SAAK,OAAO,KAAK,YAAY;AAC7B,SAAK,aAAa;AAAA,EACpB;AACF;AAEO,IAAM,eAAe,CAAC,cAAkD;AAC7E,MAAI,CAAC,QAAQ,SAAS,SAAS,GAAG;AAChC,WAAO;AAAA,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;AAAA,EACF;AACF;AAEK,IAAM,YAAY,CAAC,YACxB;AAAA,EACE,QAAQ;AAAA,IACN;AAAA,MACE,MAAM;AAAA,MACN,SAAS;AAAA,MACT,YAAY,EAAE,CAAC,uBAAuB,GAAG,IAAI;AAAA,IAC/C;AAAA,IACA;AAAA,EACF;AACF;AAEK,IAAM,aAAa,CAAC,YACzB;AAAA,EACE,QAAQ;AAAA,IACN;AAAA,MACE,MAAM;AAAA,MACN,SAAS;AAAA,MACT,YAAY,EAAE,CAAC,uBAAuB,GAAG,IAAI;AAAA,IAC/C;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/error-event.ts"],"sourcesContent":["import { Arrays, Errors, Objects, Promises } from '@bessemer/cornerstone'\nimport { Dictionary, NominalType, Throwable } from '@bessemer/cornerstone/types'\nimport { RecordAttribute } from '@bessemer/cornerstone/object'\nimport Zod, { ZodType } from 'zod'\nimport { evaluate, LazyValue } from '@bessemer/cornerstone/lazy'\n\n/*\n Represents a structured error event. The code can be mapped to a unique type of error while the\n message and attributes can provide contextual information about the error. Finally,\n an ErrorEvent could have multiple causes which get aggregated into a single parent error.\n */\nexport type ErrorCode = NominalType<string, 'ErrorCode'>\nexport const ErrorCodeSchema: ZodType<ErrorCode> = Zod.string()\n\nexport type ErrorAttribute<Type = unknown> = RecordAttribute<Type, 'ErrorAttribute'>\nexport const ErrorAttributeSchema: ZodType<ErrorAttribute> = Zod.string()\n\nconst baseErrorEventSchema = Zod.object({\n code: ErrorCodeSchema,\n message: Zod.string(),\n attributes: Zod.record(ErrorAttributeSchema, Zod.unknown()),\n})\n\nexport type ErrorEvent = Zod.infer<typeof baseErrorEventSchema> & {\n causes: Array<ErrorEvent>\n}\n\nconst ErrorEventSchema: ZodType<ErrorEvent> = baseErrorEventSchema.extend({\n causes: Zod.lazy(() => Zod.array(ErrorEventSchema)),\n})\n\n// Builder object that allows for 'partial' representation of ErrorEvents\nexport type ErrorEventBuilder = {\n code: ErrorCode\n message?: string | null\n attributes?: Record<ErrorAttribute, unknown>\n causes?: Array<ErrorEvent>\n}\n\nexport type ErrorEventAugment = Partial<ErrorEventBuilder>\n\n// An exception type that contains an ErrorEvent\nexport class ErrorEventException extends Error {\n readonly errorEvent: ErrorEvent\n\n constructor(errorEvent: ErrorEvent, cause?: unknown) {\n super(errorEvent.message ?? '', { cause })\n this.name = this.constructor.name\n this.errorEvent = errorEvent\n }\n}\n\nexport const isErrorEvent = (throwable: Throwable): throwable is ErrorEvent => {\n if (!Objects.isObject(throwable)) {\n return false\n }\n\n return 'code' in throwable && 'message' in throwable && 'attributes' in throwable && 'causes' in throwable\n}\n\nexport const isErrorEventException = (throwable: Throwable): throwable is ErrorEventException => {\n return throwable instanceof ErrorEventException\n}\n\nexport const of = (builder: ErrorEventBuilder): ErrorEvent => {\n const code = builder.code\n\n return {\n code,\n message: builder.message ?? code,\n attributes: builder.attributes ?? {},\n causes: builder.causes ?? [],\n }\n}\n\nexport const from = (throwable: Throwable): ErrorEvent => {\n if (isErrorEvent(throwable)) {\n return throwable\n }\n\n if (!Errors.isError(throwable)) {\n return unhandled()\n }\n\n const errorEventException = Errors.findInCausalChain(throwable, isErrorEventException) as ErrorEventException | undefined\n if (Objects.isNil(errorEventException)) {\n return unhandled()\n }\n\n return errorEventException.errorEvent\n}\n\nexport function withPropagation<ReturnType>(runnable: () => ReturnType, attributes: LazyValue<Dictionary<unknown>>): ReturnType\nexport function withPropagation<ReturnType>(resolver: () => Promise<ReturnType>, attributes: LazyValue<Dictionary<unknown>>): Promise<ReturnType>\nexport function withPropagation<ReturnType>(\n resolver: () => ReturnType | Promise<ReturnType>,\n attributes: LazyValue<Dictionary<unknown>>\n): ReturnType | Promise<ReturnType> {\n try {\n let result = resolver()\n if (Promises.isPromise(result)) {\n return result.then((it) => it).catch((it) => propagate(it, attributes))\n } else {\n return result\n }\n } catch (throwable: Throwable) {\n throw propagate(throwable, attributes)\n }\n}\n\nexport const propagate = (throwable: Throwable, attributes: LazyValue<Dictionary<unknown>>): never => {\n if (isErrorEventException(throwable)) {\n // We just mutate the existing error event to avoid nested exceptions\n const errorEvent = throwable.errorEvent\n errorEvent.attributes = { ...errorEvent.attributes, ...evaluate(attributes) }\n throw throwable\n } else {\n const errorEvent = from(throwable)\n const contextualizedEvent = of({ ...errorEvent, attributes: { ...errorEvent.attributes, ...evaluate(attributes) } })\n throw new ErrorEventException(contextualizedEvent, throwable)\n }\n}\n\nexport const findByCodeInCausalChain = (error: ErrorEvent, code: string): ErrorEvent | undefined => {\n return findInCausalChain(error, (it) => it.code === code)\n}\n\n/*\n Traverses the causal chain of the ErrorEvent, searching for a predicate that matches (including matching on the parent error event)\n This is useful if you want to find whether or not a given error was caused by a specific failure. The search executes depth-first and\n will return te first matching instance that satisfies the predicate, or undefined otherwise\n */\nexport const findInCausalChain = (error: ErrorEvent, predicate: (error: ErrorEvent) => boolean): ErrorEvent | undefined => {\n if (predicate(error)) {\n return error\n }\n\n return Arrays.first(error.causes.map((it) => findInCausalChain(it, predicate)).filter(Objects.isPresent))\n}\n\nexport const aggregate = (builder: ErrorEventBuilder, causes: Array<ErrorEvent>): ErrorEvent | undefined => {\n if (causes.length === 0) {\n return undefined\n }\n\n return of({ ...builder, causes })\n}\n\nexport const UnhandledErrorCode: ErrorCode = 'error-event.unhandled'\nexport const NotFoundErrorCode: ErrorCode = 'error-event.not-found'\nexport const ForbiddenErrorCode: ErrorCode = 'error-event.forbidden'\nexport const UnauthorizedErrorCode: ErrorCode = 'error-event.unauthorized'\nexport const BadRequestErrorCode: ErrorCode = 'error-event.bad-request'\n\nexport const RequestCorrelationIdAttribute: ErrorAttribute<string> = 'requestCorrelationId'\nexport const HttpStatusCodeAttribute: ErrorAttribute<number> = 'httpStatusCode'\n\nexport const unhandled = (builder?: ErrorEventAugment) =>\n of(\n Objects.deepMerge(\n {\n code: UnhandledErrorCode,\n message: 'An Unhandled Error has occurred.',\n attributes: { [HttpStatusCodeAttribute]: 500 },\n },\n builder\n )\n )\n\nexport const notFound = (builder?: ErrorEventAugment) =>\n of(\n Objects.deepMerge(\n {\n code: NotFoundErrorCode,\n message: 'The requested Resource could not be found.',\n attributes: { [HttpStatusCodeAttribute]: 404 },\n },\n builder\n )\n )\n\nexport const unauthorized = (builder?: ErrorEventAugment) =>\n of(\n Objects.deepMerge(\n {\n code: UnauthorizedErrorCode,\n message: 'The requested Resource requires authentication.',\n attributes: { [HttpStatusCodeAttribute]: 401 },\n },\n builder\n )\n )\n\nexport const forbidden = (builder?: ErrorEventAugment) =>\n of(\n Objects.deepMerge(\n {\n code: ForbiddenErrorCode,\n message: 'The requested Resource requires additional permissions to access.',\n attributes: { [HttpStatusCodeAttribute]: 403 },\n },\n builder\n )\n )\n\nexport const badRequest = (builder?: ErrorEventAugment) =>\n of(\n Objects.deepMerge(\n {\n code: BadRequestErrorCode,\n message: 'The request is invalid and cannot be processed.',\n attributes: { [HttpStatusCodeAttribute]: 400 },\n },\n builder\n )\n )\n"],"mappings":";AAAA,SAAS,QAAQ,QAAQ,SAAS,gBAAgB;AAGlD,OAAO,SAAsB;AAC7B,SAAS,gBAA2B;AAQ7B,IAAM,kBAAsC,IAAI,OAAO;AAGvD,IAAM,uBAAgD,IAAI,OAAO;AAExE,IAAM,uBAAuB,IAAI,OAAO;AAAA,EACtC,MAAM;AAAA,EACN,SAAS,IAAI,OAAO;AAAA,EACpB,YAAY,IAAI,OAAO,sBAAsB,IAAI,QAAQ,CAAC;AAC5D,CAAC;AAMD,IAAM,mBAAwC,qBAAqB,OAAO;AAAA,EACxE,QAAQ,IAAI,KAAK,MAAM,IAAI,MAAM,gBAAgB,CAAC;AACpD,CAAC;AAaM,IAAM,sBAAN,cAAkC,MAAM;AAAA,EACpC;AAAA,EAET,YAAY,YAAwB,OAAiB;AACnD,UAAM,WAAW,WAAW,IAAI,EAAE,MAAM,CAAC;AACzC,SAAK,OAAO,KAAK,YAAY;AAC7B,SAAK,aAAa;AAAA,EACpB;AACF;AAEO,IAAM,eAAe,CA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AA,MACN,SAAS;AAAA,MACT,YAAY,EAAE,CAAC,uBAAuB,GAAG,IAAI;AAAA,IAC/C;AAAA,IACA;AAAA,EACF;AACF;AAEK,IAAM,YAAY,CAAC,YACxB;AAAA,EACE,QAAQ;AAAA,IACN;AAAA,MACE,MAAM;AAAA,MACN,SAAS;AAAA,MACT,YAAY,EAAE,CAAC,uBAAuB,GAAG,IAAI;AAAA,IAC/C;AAAA,IACA;AAAA,EACF;AACF;AAEK,IAAM,aAAa,CAAC,YACzB;AAAA,EACE,QAAQ;AAAA,IACN;AAAA,MACE,MAAM;AAAA,MACN,SAAS;AAAA,MACT,YAAY,EAAE,CAAC,uBAAuB,GAAG,IAAI;AAAA,IAC/C;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bessemer/cornerstone",
3
3
  "type": "module",
4
- "version": "0.5.55",
4
+ "version": "0.5.56",
5
5
  "module": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "sideEffects": false,