@powersync/lib-services-framework 0.0.0-dev-20240620165410

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.
Files changed (102) hide show
  1. package/LICENSE +67 -0
  2. package/README.md +3 -0
  3. package/dist/alerts/alerts-index.d.ts +2 -0
  4. package/dist/alerts/alerts-index.js +3 -0
  5. package/dist/alerts/alerts-index.js.map +1 -0
  6. package/dist/alerts/definitions.d.ts +13 -0
  7. package/dist/alerts/definitions.js +2 -0
  8. package/dist/alerts/definitions.js.map +1 -0
  9. package/dist/alerts/no-op-reporter.d.ts +2 -0
  10. package/dist/alerts/no-op-reporter.js +5 -0
  11. package/dist/alerts/no-op-reporter.js.map +1 -0
  12. package/dist/codec/codec-index.d.ts +2 -0
  13. package/dist/codec/codec-index.js +3 -0
  14. package/dist/codec/codec-index.js.map +1 -0
  15. package/dist/codec/codecs.d.ts +26 -0
  16. package/dist/codec/codecs.js +60 -0
  17. package/dist/codec/codecs.js.map +1 -0
  18. package/dist/codec/parsers.d.ts +15 -0
  19. package/dist/codec/parsers.js +56 -0
  20. package/dist/codec/parsers.js.map +1 -0
  21. package/dist/container.d.ts +48 -0
  22. package/dist/container.js +61 -0
  23. package/dist/container.js.map +1 -0
  24. package/dist/errors/errors-index.d.ts +2 -0
  25. package/dist/errors/errors-index.js +3 -0
  26. package/dist/errors/errors-index.js.map +1 -0
  27. package/dist/errors/framework-errors.d.ts +50 -0
  28. package/dist/errors/framework-errors.js +104 -0
  29. package/dist/errors/framework-errors.js.map +1 -0
  30. package/dist/errors/utils.d.ts +4 -0
  31. package/dist/errors/utils.js +18 -0
  32. package/dist/errors/utils.js.map +1 -0
  33. package/dist/index.d.ts +18 -0
  34. package/dist/index.js +19 -0
  35. package/dist/index.js.map +1 -0
  36. package/dist/logger/Logger.d.ts +6 -0
  37. package/dist/logger/Logger.js +13 -0
  38. package/dist/logger/Logger.js.map +1 -0
  39. package/dist/logger/logger-index.d.ts +1 -0
  40. package/dist/logger/logger-index.js +2 -0
  41. package/dist/logger/logger-index.js.map +1 -0
  42. package/dist/router/endpoint.d.ts +7 -0
  43. package/dist/router/endpoint.js +18 -0
  44. package/dist/router/endpoint.js.map +1 -0
  45. package/dist/router/router-definitions.d.ts +44 -0
  46. package/dist/router/router-definitions.js +14 -0
  47. package/dist/router/router-definitions.js.map +1 -0
  48. package/dist/router/router-index.d.ts +3 -0
  49. package/dist/router/router-index.js +4 -0
  50. package/dist/router/router-index.js.map +1 -0
  51. package/dist/router/router-response.d.ts +24 -0
  52. package/dist/router/router-response.js +32 -0
  53. package/dist/router/router-response.js.map +1 -0
  54. package/dist/schema/definitions.d.ts +16 -0
  55. package/dist/schema/definitions.js +2 -0
  56. package/dist/schema/definitions.js.map +1 -0
  57. package/dist/schema/json-schema/keywords.d.ts +2 -0
  58. package/dist/schema/json-schema/keywords.js +27 -0
  59. package/dist/schema/json-schema/keywords.js.map +1 -0
  60. package/dist/schema/json-schema/parser.d.ts +39 -0
  61. package/dist/schema/json-schema/parser.js +100 -0
  62. package/dist/schema/json-schema/parser.js.map +1 -0
  63. package/dist/schema/schema-index.d.ts +5 -0
  64. package/dist/schema/schema-index.js +6 -0
  65. package/dist/schema/schema-index.js.map +1 -0
  66. package/dist/schema/utils.d.ts +15 -0
  67. package/dist/schema/utils.js +40 -0
  68. package/dist/schema/utils.js.map +1 -0
  69. package/dist/schema/validators/schema-validator.d.ts +21 -0
  70. package/dist/schema/validators/schema-validator.js +55 -0
  71. package/dist/schema/validators/schema-validator.js.map +1 -0
  72. package/dist/schema/validators/ts-codec-validator.d.ts +11 -0
  73. package/dist/schema/validators/ts-codec-validator.js +14 -0
  74. package/dist/schema/validators/ts-codec-validator.js.map +1 -0
  75. package/dist/signals/probes/fs-probes.d.ts +2 -0
  76. package/dist/signals/probes/fs-probes.js +43 -0
  77. package/dist/signals/probes/fs-probes.js.map +1 -0
  78. package/dist/signals/probes/memory-probes.d.ts +5 -0
  79. package/dist/signals/probes/memory-probes.js +29 -0
  80. package/dist/signals/probes/memory-probes.js.map +1 -0
  81. package/dist/signals/probes/probes.d.ts +12 -0
  82. package/dist/signals/probes/probes.js +2 -0
  83. package/dist/signals/probes/probes.js.map +1 -0
  84. package/dist/signals/signals-index.d.ts +4 -0
  85. package/dist/signals/signals-index.js +5 -0
  86. package/dist/signals/signals-index.js.map +1 -0
  87. package/dist/signals/termination-handler.d.ts +42 -0
  88. package/dist/signals/termination-handler.js +89 -0
  89. package/dist/signals/termination-handler.js.map +1 -0
  90. package/dist/system/LifeCycledSystem.d.ts +23 -0
  91. package/dist/system/LifeCycledSystem.js +32 -0
  92. package/dist/system/LifeCycledSystem.js.map +1 -0
  93. package/dist/system/system-index.d.ts +1 -0
  94. package/dist/system/system-index.js +2 -0
  95. package/dist/system/system-index.js.map +1 -0
  96. package/dist/utils/environment-variables.d.ts +11 -0
  97. package/dist/utils/environment-variables.js +51 -0
  98. package/dist/utils/environment-variables.js.map +1 -0
  99. package/dist/utils/utils-index.d.ts +1 -0
  100. package/dist/utils/utils-index.js +2 -0
  101. package/dist/utils/utils-index.js.map +1 -0
  102. package/package.json +36 -0
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
1
+ export * from './alerts/alerts-index.js';
2
+ export * as alerts from './alerts/alerts-index.js';
3
+ export * from './codec/codec-index.js';
4
+ export * as codecs from './codec/codec-index.js';
5
+ export * from './container.js';
6
+ export * from './errors/errors-index.js';
7
+ export * as errors from './errors/errors-index.js';
8
+ export * from './logger/Logger.js';
9
+ export * from './schema/schema-index.js';
10
+ export * as schema from './schema/schema-index.js';
11
+ export * from './signals/signals-index.js';
12
+ export * as signals from './signals/signals-index.js';
13
+ export * from './router/router-index.js';
14
+ export * as router from './router/router-index.js';
15
+ export * from './system/LifeCycledSystem.js';
16
+ export * as system from './system/system-index.js';
17
+ export * from './utils/utils-index.js';
18
+ export * as utils from './utils/utils-index.js';
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC;AACzC,OAAO,KAAK,MAAM,MAAM,0BAA0B,CAAC;AAEnD,cAAc,wBAAwB,CAAC;AACvC,OAAO,KAAK,MAAM,MAAM,wBAAwB,CAAC;AAEjD,cAAc,gBAAgB,CAAC;AAE/B,cAAc,0BAA0B,CAAC;AACzC,OAAO,KAAK,MAAM,MAAM,0BAA0B,CAAC;AAEnD,cAAc,oBAAoB,CAAC;AAEnC,cAAc,0BAA0B,CAAC;AACzC,OAAO,KAAK,MAAM,MAAM,0BAA0B,CAAC;AAEnD,cAAc,4BAA4B,CAAC;AAC3C,OAAO,KAAK,OAAO,MAAM,4BAA4B,CAAC;AAEtD,cAAc,0BAA0B,CAAC;AACzC,OAAO,KAAK,MAAM,MAAM,0BAA0B,CAAC;AAEnD,cAAc,8BAA8B,CAAC;AAC7C,OAAO,KAAK,MAAM,MAAM,0BAA0B,CAAC;AAEnD,cAAc,wBAAwB,CAAC;AACvC,OAAO,KAAK,KAAK,MAAM,wBAAwB,CAAC"}
@@ -0,0 +1,6 @@
1
+ import winston from 'winston';
2
+ export declare namespace Logger {
3
+ const development_format: winston.Logform.Format;
4
+ const production_format: winston.Logform.Format;
5
+ }
6
+ export declare const logger: winston.Logger;
@@ -0,0 +1,13 @@
1
+ import winston from 'winston';
2
+ export var Logger;
3
+ (function (Logger) {
4
+ Logger.development_format = winston.format.combine(winston.format.colorize({ level: true }), winston.format.simple());
5
+ Logger.production_format = winston.format.combine(winston.format.timestamp(), winston.format.json());
6
+ })(Logger || (Logger = {}));
7
+ export const logger = winston.createLogger();
8
+ // Configure logging to console as the default
9
+ logger.configure({
10
+ format: process.env.NODE_ENV == 'production' ? Logger.production_format : Logger.development_format,
11
+ transports: [new winston.transports.Console()]
12
+ });
13
+ //# sourceMappingURL=Logger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Logger.js","sourceRoot":"","sources":["../../src/logger/Logger.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,SAAS,CAAC;AAE9B,MAAM,KAAW,MAAM,CAMtB;AAND,WAAiB,MAAM;IACR,yBAAkB,GAAG,OAAO,CAAC,MAAM,CAAC,OAAO,CACtD,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,EACxC,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,CACxB,CAAC;IACW,wBAAiB,GAAG,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,SAAS,EAAE,EAAE,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;AAC7G,CAAC,EANgB,MAAM,KAAN,MAAM,QAMtB;AAED,MAAM,CAAC,MAAM,MAAM,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC;AAE7C,8CAA8C;AAC9C,MAAM,CAAC,SAAS,CAAC;IACf,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,QAAQ,IAAI,YAAY,CAAC,CAAC,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC,CAAC,MAAM,CAAC,kBAAkB;IACnG,UAAU,EAAE,CAAC,IAAI,OAAO,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC;CAC/C,CAAC,CAAC"}
@@ -0,0 +1 @@
1
+ export * from './Logger.js';
@@ -0,0 +1,2 @@
1
+ export * from './Logger.js';
2
+ //# sourceMappingURL=logger-index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger-index.js","sourceRoot":"","sources":["../../src/logger/logger-index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC"}
@@ -0,0 +1,7 @@
1
+ import { Endpoint, EndpointHandlerPayload } from './router-definitions.js';
2
+ /**
3
+ * Executes an endpoint's definition in the correct lifecycle order:
4
+ * Validations are checked. A {@link ValidationError} is thrown if validations fail.
5
+ * Authorization is checked. A {@link AuthorizationError} is thrown if checks fail.
6
+ */
7
+ export declare const executeEndpoint: <I, O, C, P extends EndpointHandlerPayload<I, C>>(endpoint: Endpoint<I, O, C, P>, payload: P) => Promise<O>;
@@ -0,0 +1,18 @@
1
+ import * as errors from '../errors/errors-index.js';
2
+ /**
3
+ * Executes an endpoint's definition in the correct lifecycle order:
4
+ * Validations are checked. A {@link ValidationError} is thrown if validations fail.
5
+ * Authorization is checked. A {@link AuthorizationError} is thrown if checks fail.
6
+ */
7
+ export const executeEndpoint = async (endpoint, payload) => {
8
+ const validation_response = await endpoint.validator?.validate(payload.params);
9
+ if (validation_response && !validation_response.valid) {
10
+ throw new errors.ValidationError(validation_response.errors);
11
+ }
12
+ const authorizer_response = await endpoint.authorize?.(payload);
13
+ if (authorizer_response && !authorizer_response.authorized) {
14
+ throw new errors.AuthorizationError(authorizer_response.errors);
15
+ }
16
+ return endpoint.handler(payload);
17
+ };
18
+ //# sourceMappingURL=endpoint.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"endpoint.js","sourceRoot":"","sources":["../../src/router/endpoint.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,2BAA2B,CAAC;AAGpD;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,KAAK,EAClC,QAA8B,EAC9B,OAAU,EACV,EAAE;IACF,MAAM,mBAAmB,GAAG,MAAM,QAAQ,CAAC,SAAS,EAAE,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAC/E,IAAI,mBAAmB,IAAI,CAAC,mBAAmB,CAAC,KAAK,EAAE;QACrD,MAAM,IAAI,MAAM,CAAC,eAAe,CAAC,mBAAmB,CAAC,MAAM,CAAC,CAAC;KAC9D;IACD,MAAM,mBAAmB,GAAG,MAAM,QAAQ,CAAC,SAAS,EAAE,CAAC,OAAO,CAAC,CAAC;IAChE,IAAI,mBAAmB,IAAI,CAAC,mBAAmB,CAAC,UAAU,EAAE;QAC1D,MAAM,IAAI,MAAM,CAAC,kBAAkB,CAAC,mBAAmB,CAAC,MAAM,CAAC,CAAC;KACjE;IAED,OAAO,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;AACnC,CAAC,CAAC"}
@@ -0,0 +1,44 @@
1
+ import { MicroValidator } from '../schema/definitions.js';
2
+ /**
3
+ * Subset of HTTP methods used in route definitions
4
+ */
5
+ export declare enum HTTPMethod {
6
+ 'DELETE' = "DELETE",
7
+ 'GET' = "GET",
8
+ 'HEAD' = "HEAD",
9
+ 'PATCH' = "PATCH",
10
+ 'POST' = "POST",
11
+ 'PUT' = "PUT",
12
+ 'OPTIONS' = "OPTIONS"
13
+ }
14
+ /**
15
+ * Response for authorization checks in route definitions
16
+ */
17
+ export type AuthorizationResponse = {
18
+ authorized: true;
19
+ } | {
20
+ authorized: false;
21
+ errors?: any[];
22
+ };
23
+ /**
24
+ * Payload which is passed to route endpoint handler functions
25
+ */
26
+ export type EndpointHandlerPayload<P, C> = {
27
+ params: P;
28
+ context: C;
29
+ };
30
+ /**
31
+ * A route endpoint handler function
32
+ */
33
+ export type EndpointHandler<P, O> = (payload: P) => O | Promise<O>;
34
+ /**
35
+ * The definition of a route endpoint
36
+ */
37
+ export type Endpoint<I, O, C, P = EndpointHandlerPayload<I, C>, H = EndpointHandler<P, O>> = {
38
+ path: string;
39
+ method: HTTPMethod;
40
+ tags?: EndpointHandler<P, Record<string, string>>;
41
+ authorize?: EndpointHandler<P, AuthorizationResponse>;
42
+ validator?: MicroValidator<I>;
43
+ handler: H;
44
+ };
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Subset of HTTP methods used in route definitions
3
+ */
4
+ export var HTTPMethod;
5
+ (function (HTTPMethod) {
6
+ HTTPMethod["DELETE"] = "DELETE";
7
+ HTTPMethod["GET"] = "GET";
8
+ HTTPMethod["HEAD"] = "HEAD";
9
+ HTTPMethod["PATCH"] = "PATCH";
10
+ HTTPMethod["POST"] = "POST";
11
+ HTTPMethod["PUT"] = "PUT";
12
+ HTTPMethod["OPTIONS"] = "OPTIONS";
13
+ })(HTTPMethod || (HTTPMethod = {}));
14
+ //# sourceMappingURL=router-definitions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"router-definitions.js","sourceRoot":"","sources":["../../src/router/router-definitions.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,CAAN,IAAY,UAQX;AARD,WAAY,UAAU;IACpB,+BAAmB,CAAA;IACnB,yBAAa,CAAA;IACb,2BAAe,CAAA;IACf,6BAAiB,CAAA;IACjB,2BAAe,CAAA;IACf,yBAAa,CAAA;IACb,iCAAqB,CAAA;AACvB,CAAC,EARW,UAAU,KAAV,UAAU,QAQrB"}
@@ -0,0 +1,3 @@
1
+ export * from './router-definitions.js';
2
+ export * from './router-response.js';
3
+ export * from './endpoint.js';
@@ -0,0 +1,4 @@
1
+ export * from './router-definitions.js';
2
+ export * from './router-response.js';
3
+ export * from './endpoint.js';
4
+ //# sourceMappingURL=router-index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"router-index.js","sourceRoot":"","sources":["../../src/router/router-index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,sBAAsB,CAAC;AACrC,cAAc,eAAe,CAAC"}
@@ -0,0 +1,24 @@
1
+ export declare const isAsyncIterable: (iterable: any) => iterable is AsyncIterable<any>;
2
+ export type RouterResponseParams<T> = {
3
+ status?: number;
4
+ data: T;
5
+ headers?: Record<string, string>;
6
+ /**
7
+ * Hook to be called after the response has been sent
8
+ */
9
+ afterSend?: () => Promise<void>;
10
+ };
11
+ /**
12
+ * Wrapper for router responses.
13
+ * A {@link EndpointHandler} can return a raw JSON object to be used as the response data
14
+ * payload or a [RouterResponse] which allows for more granular control of what is sent.
15
+ */
16
+ export declare class RouterResponse<T = unknown> {
17
+ status: number;
18
+ data: T;
19
+ headers: Record<string, string>;
20
+ afterSend: () => Promise<void>;
21
+ __micro_router_response: boolean;
22
+ static isRouterResponse(input: any): input is RouterResponse;
23
+ constructor(params: RouterResponseParams<T>);
24
+ }
@@ -0,0 +1,32 @@
1
+ export const isAsyncIterable = (iterable) => {
2
+ return iterable != null && typeof iterable === 'object' && Symbol.asyncIterator in iterable;
3
+ };
4
+ /**
5
+ * Wrapper for router responses.
6
+ * A {@link EndpointHandler} can return a raw JSON object to be used as the response data
7
+ * payload or a [RouterResponse] which allows for more granular control of what is sent.
8
+ */
9
+ export class RouterResponse {
10
+ static isRouterResponse(input) {
11
+ return input instanceof RouterResponse || input?.__micro_router_response == true;
12
+ }
13
+ constructor(params) {
14
+ this.__micro_router_response = true;
15
+ this.status = params.status || 200;
16
+ this.data = params.data;
17
+ this.headers = params.headers || {};
18
+ this.afterSend = params.afterSend ?? (() => Promise.resolve());
19
+ if (!this.headers['Content-Type']) {
20
+ if (isAsyncIterable(this.data)) {
21
+ this.headers['Content-Type'] = 'application/octet-stream';
22
+ }
23
+ else if (Buffer.isBuffer(this.data)) {
24
+ this.headers['Content-Type'] = 'application/octet-stream';
25
+ }
26
+ else {
27
+ this.headers['Content-Type'] = 'application/json';
28
+ }
29
+ }
30
+ }
31
+ }
32
+ //# sourceMappingURL=router-response.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"router-response.js","sourceRoot":"","sources":["../../src/router/router-response.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,QAAa,EAAkC,EAAE;IAC/E,OAAO,QAAQ,IAAI,IAAI,IAAI,OAAO,QAAQ,KAAK,QAAQ,IAAI,MAAM,CAAC,aAAa,IAAI,QAAQ,CAAC;AAC9F,CAAC,CAAC;AAYF;;;;GAIG;AACH,MAAM,OAAO,cAAc;IAQzB,MAAM,CAAC,gBAAgB,CAAC,KAAU;QAChC,OAAO,KAAK,YAAY,cAAc,IAAI,KAAK,EAAE,uBAAuB,IAAI,IAAI,CAAC;IACnF,CAAC;IAED,YAAY,MAA+B;QAN3C,4BAAuB,GAAG,IAAI,CAAC;QAO7B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,IAAI,GAAG,CAAC;QACnC,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;QACxB,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,IAAI,EAAE,CAAC;QACpC,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,SAAS,IAAI,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC;QAE/D,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,EAAE;YACjC,IAAI,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;gBAC9B,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,GAAG,0BAA0B,CAAC;aAC3D;iBAAM,IAAI,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;gBACrC,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,GAAG,0BAA0B,CAAC;aAC3D;iBAAM;gBACL,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,GAAG,kBAAkB,CAAC;aACnD;SACF;IACH,CAAC;CACF"}
@@ -0,0 +1,16 @@
1
+ export type JSONSchema = {
2
+ definitions?: Record<string, any>;
3
+ [key: string]: any;
4
+ };
5
+ export type IValidationRight = {
6
+ valid: true;
7
+ };
8
+ export type ValidationLeft<T = string[]> = {
9
+ valid: false;
10
+ errors: T;
11
+ };
12
+ export type ValidationResponse<T, E = string[]> = ValidationLeft<E> | IValidationRight;
13
+ export type MicroValidator<T = any, E = string[]> = {
14
+ validate: (data: T) => ValidationResponse<T, E>;
15
+ toJSONSchema?: () => JSONSchema;
16
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=definitions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/schema/definitions.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ import * as ajv from 'ajv';
2
+ export declare const BufferNodeType: ajv.KeywordDefinition;
@@ -0,0 +1,27 @@
1
+ import * as ajv from 'ajv';
2
+ export const BufferNodeType = {
3
+ keyword: 'nodeType',
4
+ metaSchema: {
5
+ type: 'string',
6
+ enum: ['buffer', 'date']
7
+ },
8
+ error: {
9
+ message: ({ schemaCode }) => {
10
+ return ajv.str `should be a ${schemaCode}`;
11
+ }
12
+ },
13
+ code(context) {
14
+ switch (context.schema) {
15
+ case 'buffer': {
16
+ return context.fail(ajv._ `!Buffer.isBuffer(${context.data})`);
17
+ }
18
+ case 'date': {
19
+ return context.fail(ajv._ `!(${context.data} instanceof Date)`);
20
+ }
21
+ default: {
22
+ context.fail(ajv._ `true`);
23
+ }
24
+ }
25
+ }
26
+ };
27
+ //# sourceMappingURL=keywords.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keywords.js","sourceRoot":"","sources":["../../../src/schema/json-schema/keywords.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,KAAK,CAAC;AAE3B,MAAM,CAAC,MAAM,cAAc,GAA0B;IACnD,OAAO,EAAE,UAAU;IACnB,UAAU,EAAE;QACV,IAAI,EAAE,QAAQ;QACd,IAAI,EAAE,CAAC,QAAQ,EAAE,MAAM,CAAC;KACzB;IACD,KAAK,EAAE;QACL,OAAO,EAAE,CAAC,EAAE,UAAU,EAAE,EAAE,EAAE;YAC1B,OAAO,GAAG,CAAC,GAAG,CAAA,eAAe,UAAU,EAAE,CAAC;QAC5C,CAAC;KACF;IACD,IAAI,CAAC,OAAO;QACV,QAAQ,OAAO,CAAC,MAAM,EAAE;YACtB,KAAK,QAAQ,CAAC,CAAC;gBACb,OAAO,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAA,oBAAoB,OAAO,CAAC,IAAI,GAAG,CAAC,CAAC;aAC/D;YACD,KAAK,MAAM,CAAC,CAAC;gBACX,OAAO,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAA,KAAK,OAAO,CAAC,IAAI,mBAAmB,CAAC,CAAC;aAChE;YACD,OAAO,CAAC,CAAC;gBACP,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAA,MAAM,CAAC,CAAC;aAC3B;SACF;IACH,CAAC;CACF,CAAC"}
@@ -0,0 +1,39 @@
1
+ import * as schema_validator from '../validators/schema-validator.js';
2
+ import * as defs from '../definitions.js';
3
+ /**
4
+ * Recursively walk a given schema resolving a list of refs that are actively used in some way by the
5
+ * root schema. This information can then later be used to prune unused definitions.
6
+ *
7
+ * This only works for top-level references to 'definitions' as this is intended to be used in
8
+ * conjunction with tools that generate schemas in deterministic ways. For a more general
9
+ * implementation one should make use of `$RefParser`.
10
+ *
11
+ * We don't use this here as it resolves to a Promise, which we want to avoid for this tool
12
+ */
13
+ export declare const findUsedRefs: (schema: any, definitions?: any, cache?: string[]) => string[];
14
+ /**
15
+ * Prune a given subschema definitions map by comparing keys against a given collection of
16
+ * definition keys that are referenced in some way, either directly or indirectly, by the
17
+ * root schema
18
+ */
19
+ export declare const pruneDefinitions: (definitions: Record<string, any>, refs: string[]) => Record<string, any>;
20
+ export type CompilerFunction = () => defs.JSONSchema;
21
+ export type ValidatorFunction = <T>(params?: schema_validator.CreateSchemaValidatorParams) => schema_validator.SchemaValidator<T>;
22
+ export type Compilers = {
23
+ compile: CompilerFunction;
24
+ validator: ValidatorFunction;
25
+ };
26
+ export type WithCompilers<T> = Compilers & {
27
+ [P in keyof T]: T[P] extends Record<string, any> ? WithCompilers<T[P]> : T[P];
28
+ };
29
+ /**
30
+ * Given a JSON Schema containing a `definitions` entry, return a Proxy representation of the same
31
+ * schema which responds to `compile` and `validator` arbitrarily deep.
32
+ *
33
+ * Calling compile on a sub-schema will 'inject' the root schema `definitions` mapping and remove
34
+ * the Proxy wrapping.
35
+ *
36
+ * Calling `validator` on a sub-schema will `compile` and then create a SchemaValidator from the
37
+ * resulting schema
38
+ */
39
+ export declare const parseJSONSchema: <T extends defs.JSONSchema>(schema: T, definitions?: Record<string, any> | undefined) => WithCompilers<T>;
@@ -0,0 +1,100 @@
1
+ import * as schema_validator from '../validators/schema-validator.js';
2
+ /**
3
+ * Recursively walk a given schema resolving a list of refs that are actively used in some way by the
4
+ * root schema. This information can then later be used to prune unused definitions.
5
+ *
6
+ * This only works for top-level references to 'definitions' as this is intended to be used in
7
+ * conjunction with tools that generate schemas in deterministic ways. For a more general
8
+ * implementation one should make use of `$RefParser`.
9
+ *
10
+ * We don't use this here as it resolves to a Promise, which we want to avoid for this tool
11
+ */
12
+ export const findUsedRefs = (schema, definitions = schema.definitions, cache = []) => {
13
+ if (Array.isArray(schema)) {
14
+ return schema
15
+ .map((subschema) => {
16
+ return findUsedRefs(subschema, definitions, cache);
17
+ })
18
+ .flat();
19
+ }
20
+ if (typeof schema === 'object') {
21
+ return Object.keys(schema).reduce((used, key) => {
22
+ const value = schema[key];
23
+ if (key === '$ref') {
24
+ if (cache.includes(value)) {
25
+ return used;
26
+ }
27
+ cache.push(value);
28
+ const subschema = definitions[value.replace('#/definitions/', '')];
29
+ used.push(value, ...findUsedRefs(subschema, definitions, cache));
30
+ return used;
31
+ }
32
+ if (key === 'definitions') {
33
+ return used;
34
+ }
35
+ return used.concat(findUsedRefs(value, definitions, cache));
36
+ }, []);
37
+ }
38
+ return [];
39
+ };
40
+ /**
41
+ * Prune a given subschema definitions map by comparing keys against a given collection of
42
+ * definition keys that are referenced in some way, either directly or indirectly, by the
43
+ * root schema
44
+ */
45
+ export const pruneDefinitions = (definitions, refs) => {
46
+ return Object.keys(definitions).reduce((pruned, key) => {
47
+ if (refs.includes(`#/definitions/${key}`)) {
48
+ pruned[key] = definitions[key];
49
+ }
50
+ return pruned;
51
+ }, {});
52
+ };
53
+ /**
54
+ * Given a JSON Schema containing a `definitions` entry, return a Proxy representation of the same
55
+ * schema which responds to `compile` and `validator` arbitrarily deep.
56
+ *
57
+ * Calling compile on a sub-schema will 'inject' the root schema `definitions` mapping and remove
58
+ * the Proxy wrapping.
59
+ *
60
+ * Calling `validator` on a sub-schema will `compile` and then create a SchemaValidator from the
61
+ * resulting schema
62
+ */
63
+ export const parseJSONSchema = (schema, definitions = schema.definitions) => {
64
+ return new Proxy(schema, {
65
+ get(target, prop) {
66
+ const compile = () => {
67
+ const schema = {
68
+ definitions: definitions,
69
+ ...target
70
+ };
71
+ if (!schema.definitions) {
72
+ return schema;
73
+ }
74
+ const used = findUsedRefs(schema);
75
+ return {
76
+ ...schema,
77
+ definitions: pruneDefinitions(schema.definitions, used)
78
+ };
79
+ };
80
+ const validator = (options) => {
81
+ return schema_validator.createSchemaValidator(compile(), options);
82
+ };
83
+ if (prop === 'compile') {
84
+ return compile;
85
+ }
86
+ if (prop === 'validator') {
87
+ return validator;
88
+ }
89
+ const subschema = target[prop];
90
+ if (Array.isArray(subschema)) {
91
+ return subschema;
92
+ }
93
+ if (typeof subschema !== 'object') {
94
+ return subschema;
95
+ }
96
+ return parseJSONSchema(subschema, definitions);
97
+ }
98
+ });
99
+ };
100
+ //# sourceMappingURL=parser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parser.js","sourceRoot":"","sources":["../../../src/schema/json-schema/parser.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,5 @@
1
+ export * from './definitions.js';
2
+ export * from './json-schema/keywords.js';
3
+ export * from './json-schema/parser.js';
4
+ export * from './validators/schema-validator.js';
5
+ export * from './validators/ts-codec-validator.js';
@@ -0,0 +1,6 @@
1
+ export * from './definitions.js';
2
+ export * from './json-schema/keywords.js';
3
+ export * from './json-schema/parser.js';
4
+ export * from './validators/schema-validator.js';
5
+ export * from './validators/ts-codec-validator.js';
6
+ //# sourceMappingURL=schema-index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema-index.js","sourceRoot":"","sources":["../../src/schema/schema-index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AAEjC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,yBAAyB,CAAC;AACxC,cAAc,kCAAkC,CAAC;AACjD,cAAc,oCAAoC,CAAC"}
@@ -0,0 +1,15 @@
1
+ import * as defs from './definitions.js';
2
+ export type Schema = {
3
+ additionalProperties?: boolean | Schema;
4
+ [key: string]: any;
5
+ };
6
+ /**
7
+ * Utility function to strip out `additionalProperties` fields from a given JSON-Schema. This can be used
8
+ * to make a schema less strict which may be necessary for certain use-cases
9
+ */
10
+ export declare const allowAdditionalProperties: <T extends Schema>(schema: T) => T;
11
+ /**
12
+ * A small utility for validating some data using a MicroValidator. Will return the valid data (typed correctly) or throw
13
+ * a validation error
14
+ */
15
+ export declare const validateData: <T>(event: any, validator: defs.MicroValidator<T, string[]>) => T;
@@ -0,0 +1,40 @@
1
+ import * as errors from '../errors/errors-index.js';
2
+ /**
3
+ * Utility function to strip out `additionalProperties` fields from a given JSON-Schema. This can be used
4
+ * to make a schema less strict which may be necessary for certain use-cases
5
+ */
6
+ export const allowAdditionalProperties = (schema) => {
7
+ return Object.keys(schema).reduce((next_schema, key) => {
8
+ const value = schema[key];
9
+ if (key === 'additionalProperties' && typeof value === 'boolean') {
10
+ return next_schema;
11
+ }
12
+ if (Array.isArray(value)) {
13
+ next_schema[key] = value.map((value) => {
14
+ if (typeof value !== 'object') {
15
+ return value;
16
+ }
17
+ return allowAdditionalProperties(value);
18
+ });
19
+ }
20
+ else if (typeof value === 'object') {
21
+ next_schema[key] = allowAdditionalProperties(value);
22
+ }
23
+ else {
24
+ next_schema[key] = value;
25
+ }
26
+ return next_schema;
27
+ }, {});
28
+ };
29
+ /**
30
+ * A small utility for validating some data using a MicroValidator. Will return the valid data (typed correctly) or throw
31
+ * a validation error
32
+ */
33
+ export const validateData = (event, validator) => {
34
+ const result = validator.validate(event);
35
+ if (!result.valid) {
36
+ throw new errors.ValidationError(result.errors);
37
+ }
38
+ return event;
39
+ };
40
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/schema/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,2BAA2B,CAAC;AAQpD;;;GAGG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,CAAmB,MAAS,EAAK,EAAE;IAC1E,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC,WAAgB,EAAE,GAAG,EAAE,EAAE;QAC1D,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;QAE1B,IAAI,GAAG,KAAK,sBAAsB,IAAI,OAAO,KAAK,KAAK,SAAS,EAAE;YAChE,OAAO,WAAW,CAAC;SACpB;QAED,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;YACxB,WAAW,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;gBACrC,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;oBAC7B,OAAO,KAAK,CAAC;iBACd;gBACD,OAAO,yBAAyB,CAAC,KAAK,CAAC,CAAC;YAC1C,CAAC,CAAC,CAAC;SACJ;aAAM,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;YACpC,WAAW,CAAC,GAAG,CAAC,GAAG,yBAAyB,CAAC,KAAK,CAAC,CAAC;SACrD;aAAM;YACL,WAAW,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;SAC1B;QAED,OAAO,WAAW,CAAC;IACrB,CAAC,EAAE,EAAE,CAAC,CAAC;AACT,CAAC,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAI,KAAU,EAAE,SAAiC,EAAK,EAAE;IAClF,MAAM,MAAM,GAAG,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACzC,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE;QACjB,MAAM,IAAI,MAAM,CAAC,eAAe,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;KACjD;IACD,OAAO,KAAK,CAAC;AACf,CAAC,CAAC"}
@@ -0,0 +1,21 @@
1
+ import AJV from 'ajv';
2
+ import * as defs from '../definitions.js';
3
+ export declare class SchemaValidatorError extends Error {
4
+ constructor(message: string);
5
+ }
6
+ export type SchemaValidator<T> = defs.MicroValidator<T>;
7
+ export type CreateSchemaValidatorParams = {
8
+ ajv?: AJV.Options;
9
+ /**
10
+ * Allow making the given schema loosely typed to allow accepting additional properties. This
11
+ * is useful in certain scenarios such as accepting kafka events that are going through a
12
+ * migration and having additional properties set
13
+ */
14
+ allowAdditional?: boolean;
15
+ fail_fast?: boolean;
16
+ };
17
+ /**
18
+ * Create a validator from a given JSON-Schema schema object. This makes uses of AJV internally
19
+ * to compile a validation function
20
+ */
21
+ export declare const createSchemaValidator: <T = any>(schema: defs.JSONSchema, params?: CreateSchemaValidatorParams) => SchemaValidator<T>;
@@ -0,0 +1,55 @@
1
+ // @ts-ignore
2
+ import AjvErrorFormatter from 'better-ajv-errors';
3
+ import AJV from 'ajv';
4
+ import * as utils from '../utils.js';
5
+ import * as keywords from '../json-schema/keywords.js';
6
+ export class SchemaValidatorError extends Error {
7
+ constructor(message) {
8
+ super(message);
9
+ this.name = this.constructor.name;
10
+ }
11
+ }
12
+ /**
13
+ * Create a validator from a given JSON-Schema schema object. This makes uses of AJV internally
14
+ * to compile a validation function
15
+ */
16
+ export const createSchemaValidator = (schema, params = {}) => {
17
+ try {
18
+ const ajv = new AJV.Ajv({
19
+ allErrors: !(params.fail_fast ?? false),
20
+ keywords: [keywords.BufferNodeType],
21
+ ...(params.ajv || {})
22
+ });
23
+ let processed_schema = schema;
24
+ if (params.allowAdditional) {
25
+ processed_schema = utils.allowAdditionalProperties(processed_schema);
26
+ }
27
+ const validator = ajv.compile(processed_schema);
28
+ return {
29
+ toJSONSchema: () => {
30
+ return schema;
31
+ },
32
+ validate: (data) => {
33
+ const valid = validator(data);
34
+ if (!valid) {
35
+ const errors = AjvErrorFormatter(processed_schema, data, validator.errors || [], {
36
+ format: 'js'
37
+ })?.map((error) => error.error);
38
+ return {
39
+ valid: false,
40
+ errors: errors || []
41
+ };
42
+ }
43
+ return {
44
+ valid: true
45
+ };
46
+ }
47
+ };
48
+ }
49
+ catch (err) {
50
+ // Here we re-throw the error because the original error thrown by AJV has a deep stack that
51
+ // obfuscates the location of the error in application code
52
+ throw new SchemaValidatorError(err.message);
53
+ }
54
+ };
55
+ //# sourceMappingURL=schema-validator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema-validator.js","sourceRoot":"","sources":["../../../src/schema/validators/schema-validator.ts"],"names":[],"mappings":"AAAA,aAAa;AACb,OAAO,iBAAiB,MAAM,mBAAmB,CAAC;AAClD,OAAO,GAAG,MAAM,KAAK,CAAC;AAGtB,OAAO,KAAK,KAAK,MAAM,aAAa,CAAC;AACrC,OAAO,KAAK,QAAQ,MAAM,4BAA4B,CAAC;AAEvD,MAAM,OAAO,oBAAqB,SAAQ,KAAK;IAC7C,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QAEf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC;IACpC,CAAC;CACF;AAiBD;;;GAGG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CACnC,MAAuB,EACvB,SAAsC,EAAE,EACpB,EAAE;IACtB,IAAI;QACF,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC;YACtB,SAAS,EAAE,CAAC,CAAC,MAAM,CAAC,SAAS,IAAI,KAAK,CAAC;YACvC,QAAQ,EAAE,CAAC,QAAQ,CAAC,cAAc,CAAC;YACnC,GAAG,CAAC,MAAM,CAAC,GAAG,IAAI,EAAE,CAAC;SACtB,CAAC,CAAC;QAEH,IAAI,gBAAgB,GAAG,MAAM,CAAC;QAC9B,IAAI,MAAM,CAAC,eAAe,EAAE;YAC1B,gBAAgB,GAAG,KAAK,CAAC,yBAAyB,CAAC,gBAAgB,CAAC,CAAC;SACtE;QAED,MAAM,SAAS,GAAG,GAAG,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC;QAEhD,OAAO;YACL,YAAY,EAAE,GAAG,EAAE;gBACjB,OAAO,MAAM,CAAC;YAChB,CAAC;YAED,QAAQ,EAAE,CAAC,IAAI,EAAE,EAAE;gBACjB,MAAM,KAAK,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;gBAE9B,IAAI,CAAC,KAAK,EAAE;oBACV,MAAM,MAAM,GAAG,iBAAiB,CAAC,gBAAgB,EAAE,IAAI,EAAE,SAAS,CAAC,MAAM,IAAI,EAAE,EAAE;wBAC/E,MAAM,EAAE,IAAI;qBACb,CAAC,EAAE,GAAG,CAAC,CAAC,KAAU,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;oBAErC,OAAO;wBACL,KAAK,EAAE,KAAK;wBACZ,MAAM,EAAE,MAAM,IAAI,EAAE;qBACrB,CAAC;iBACH;gBAED,OAAO;oBACL,KAAK,EAAE,IAAI;iBACZ,CAAC;YACJ,CAAC;SACF,CAAC;KACH;IAAC,OAAO,GAAG,EAAE;QACZ,4FAA4F;QAC5F,2DAA2D;QAC3D,MAAM,IAAI,oBAAoB,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;KAC7C;AACH,CAAC,CAAC"}
@@ -0,0 +1,11 @@
1
+ import * as t from 'ts-codec';
2
+ import * as defs from '../definitions.js';
3
+ export type TsCodecValidator<C extends t.AnyCodec, T extends t.TransformTarget = t.TransformTarget.Encoded> = T extends t.TransformTarget.Encoded ? defs.MicroValidator<t.Encoded<C>> : defs.MicroValidator<t.Decoded<C>>;
4
+ type ValidatorOptions<T extends t.TransformTarget> = Partial<Omit<t.BaseParserParams, 'target'>> & {
5
+ target?: T;
6
+ };
7
+ /**
8
+ * Create a validator from a given ts-codec codec
9
+ */
10
+ export declare const createTsCodecValidator: <C extends t.AnyCodec, T extends t.TransformTarget = t.TransformTarget.Encoded>(codec: C, options?: ValidatorOptions<T> | undefined) => TsCodecValidator<C, T>;
11
+ export {};
@@ -0,0 +1,14 @@
1
+ import * as t from 'ts-codec';
2
+ import * as schema_validator from './schema-validator.js';
3
+ import * as codecs from '../../codec/codec-index.js';
4
+ /**
5
+ * Create a validator from a given ts-codec codec
6
+ */
7
+ export const createTsCodecValidator = (codec, options) => {
8
+ const schema = t.generateJSONSchema(codec, {
9
+ ...(options || {}),
10
+ parsers: [...(options?.parsers ?? []), ...codecs.parsers]
11
+ });
12
+ return schema_validator.createSchemaValidator(schema);
13
+ };
14
+ //# sourceMappingURL=ts-codec-validator.js.map