@azure/schema-registry-json 1.0.1-alpha.20241204.1 → 1.0.1-alpha.20241210.2

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 (106) hide show
  1. package/dist/browser/errors.d.ts +5 -0
  2. package/dist/browser/errors.d.ts.map +1 -0
  3. package/dist/browser/index.d.ts +3 -0
  4. package/dist/browser/index.d.ts.map +1 -0
  5. package/dist/browser/index.js +5 -0
  6. package/{dist-esm/src → dist/browser}/index.js.map +1 -1
  7. package/dist/browser/jsonSchemaSerializer.d.ts +46 -0
  8. package/dist/browser/jsonSchemaSerializer.d.ts.map +1 -0
  9. package/{dist-esm/src → dist/browser}/jsonSchemaSerializer.js +3 -3
  10. package/dist/browser/jsonSchemaSerializer.js.map +1 -0
  11. package/dist/browser/logger.d.ts +5 -0
  12. package/dist/browser/logger.d.ts.map +1 -0
  13. package/dist/browser/models.d.ts +51 -0
  14. package/dist/browser/models.d.ts.map +1 -0
  15. package/dist/browser/package.json +3 -0
  16. package/dist/browser/utility.d.ts +3 -0
  17. package/dist/browser/utility.d.ts.map +1 -0
  18. package/{dist-esm/src → dist/browser}/utility.js.map +1 -1
  19. package/dist/commonjs/errors.d.ts +5 -0
  20. package/dist/commonjs/errors.d.ts.map +1 -0
  21. package/dist/commonjs/errors.js +27 -0
  22. package/dist/commonjs/errors.js.map +1 -0
  23. package/dist/commonjs/index.d.ts +3 -0
  24. package/dist/commonjs/index.d.ts.map +1 -0
  25. package/dist/commonjs/index.js +10 -0
  26. package/dist/commonjs/index.js.map +1 -0
  27. package/dist/commonjs/jsonSchemaSerializer.d.ts +46 -0
  28. package/dist/commonjs/jsonSchemaSerializer.d.ts.map +1 -0
  29. package/dist/commonjs/jsonSchemaSerializer.js +177 -0
  30. package/dist/commonjs/jsonSchemaSerializer.js.map +1 -0
  31. package/dist/commonjs/logger.d.ts +5 -0
  32. package/dist/commonjs/logger.d.ts.map +1 -0
  33. package/dist/commonjs/logger.js +11 -0
  34. package/dist/commonjs/logger.js.map +1 -0
  35. package/dist/commonjs/models.d.ts +51 -0
  36. package/dist/commonjs/models.d.ts.map +1 -0
  37. package/dist/commonjs/models.js +5 -0
  38. package/dist/commonjs/models.js.map +1 -0
  39. package/dist/commonjs/package.json +3 -0
  40. package/dist/commonjs/tsdoc-metadata.json +11 -0
  41. package/dist/commonjs/utility.d.ts +3 -0
  42. package/dist/commonjs/utility.d.ts.map +1 -0
  43. package/dist/commonjs/utility.js +10 -0
  44. package/dist/commonjs/utility.js.map +1 -0
  45. package/dist/esm/errors.d.ts +5 -0
  46. package/dist/esm/errors.d.ts.map +1 -0
  47. package/dist/esm/errors.js +23 -0
  48. package/dist/esm/errors.js.map +1 -0
  49. package/dist/esm/index.d.ts +3 -0
  50. package/dist/esm/index.d.ts.map +1 -0
  51. package/dist/esm/index.js +5 -0
  52. package/dist/esm/index.js.map +1 -0
  53. package/dist/esm/jsonSchemaSerializer.d.ts +46 -0
  54. package/dist/esm/jsonSchemaSerializer.d.ts.map +1 -0
  55. package/dist/{index.js → esm/jsonSchemaSerializer.js} +10 -52
  56. package/dist/esm/jsonSchemaSerializer.js.map +1 -0
  57. package/dist/esm/logger.d.ts +5 -0
  58. package/dist/esm/logger.d.ts.map +1 -0
  59. package/dist/esm/logger.js +8 -0
  60. package/dist/esm/logger.js.map +1 -0
  61. package/dist/esm/models.d.ts +51 -0
  62. package/dist/esm/models.d.ts.map +1 -0
  63. package/dist/esm/models.js +4 -0
  64. package/dist/esm/models.js.map +1 -0
  65. package/dist/esm/package.json +3 -0
  66. package/dist/esm/utility.d.ts +3 -0
  67. package/dist/esm/utility.d.ts.map +1 -0
  68. package/dist/esm/utility.js +7 -0
  69. package/dist/esm/utility.js.map +1 -0
  70. package/dist/react-native/errors.d.ts +5 -0
  71. package/dist/react-native/errors.d.ts.map +1 -0
  72. package/dist/react-native/errors.js +23 -0
  73. package/dist/react-native/errors.js.map +1 -0
  74. package/dist/react-native/index.d.ts +3 -0
  75. package/dist/react-native/index.d.ts.map +1 -0
  76. package/dist/react-native/index.js +5 -0
  77. package/dist/react-native/index.js.map +1 -0
  78. package/dist/react-native/jsonSchemaSerializer.d.ts +46 -0
  79. package/dist/react-native/jsonSchemaSerializer.d.ts.map +1 -0
  80. package/dist/react-native/jsonSchemaSerializer.js +173 -0
  81. package/dist/react-native/jsonSchemaSerializer.js.map +1 -0
  82. package/dist/react-native/logger.d.ts +5 -0
  83. package/dist/react-native/logger.d.ts.map +1 -0
  84. package/dist/react-native/logger.js +8 -0
  85. package/dist/react-native/logger.js.map +1 -0
  86. package/dist/react-native/models.d.ts +51 -0
  87. package/dist/react-native/models.d.ts.map +1 -0
  88. package/dist/react-native/models.js +4 -0
  89. package/dist/react-native/models.js.map +1 -0
  90. package/dist/react-native/package.json +3 -0
  91. package/dist/react-native/utility.d.ts +3 -0
  92. package/dist/react-native/utility.d.ts.map +1 -0
  93. package/dist/react-native/utility.js +7 -0
  94. package/dist/react-native/utility.js.map +1 -0
  95. package/package.json +65 -36
  96. package/dist/index.js.map +0 -1
  97. package/dist-esm/src/index.js +0 -5
  98. package/dist-esm/src/jsonSchemaSerializer.js.map +0 -1
  99. package/types/schema-registry-json.d.ts +0 -101
  100. /package/{dist-esm/src → dist/browser}/errors.js +0 -0
  101. /package/{dist-esm/src → dist/browser}/errors.js.map +0 -0
  102. /package/{dist-esm/src → dist/browser}/logger.js +0 -0
  103. /package/{dist-esm/src → dist/browser}/logger.js.map +0 -0
  104. /package/{dist-esm/src → dist/browser}/models.js +0 -0
  105. /package/{dist-esm/src → dist/browser}/models.js.map +0 -0
  106. /package/{dist-esm/src → dist/browser}/utility.js +0 -0
@@ -0,0 +1,5 @@
1
+ /** @internal */
2
+ export declare function wrapError<T>(f: () => T, message: string): T;
3
+ /** @internal */
4
+ export declare function errorWithCause(message: string, cause: Error): Error;
5
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAGA,gBAAgB;AAChB,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE,MAAM,GAAG,CAAC,CAQ3D;AAED,gBAAgB;AAChB,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,GAAG,KAAK,CASnE"}
@@ -0,0 +1,3 @@
1
+ export { JsonSchemaSerializer } from "./jsonSchemaSerializer.js";
2
+ export * from "./models.js";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAEjE,cAAc,aAAa,CAAC"}
@@ -0,0 +1,5 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+ export { JsonSchemaSerializer } from "./jsonSchemaSerializer.js";
4
+ export * from "./models.js";
5
+ //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAE9D,cAAc,UAAU,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nexport { JsonSchemaSerializer } from \"./jsonSchemaSerializer\";\n\nexport * from \"./models\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAEjE,cAAc,aAAa,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nexport { JsonSchemaSerializer } from \"./jsonSchemaSerializer.js\";\n\nexport * from \"./models.js\";\n"]}
@@ -0,0 +1,46 @@
1
+ import type { DeserializeOptions, JsonSchemaSerializerOptions, MessageContent } from "./models.js";
2
+ import type { SchemaRegistry } from "@azure/schema-registry";
3
+ /**
4
+ * Json serializer that obtains schemas from a schema registry and does not
5
+ * pack schemas into its payloads.
6
+ */
7
+ export declare class JsonSchemaSerializer<MessageT = MessageContent> {
8
+ /**
9
+ * Creates a new serializer.
10
+ *
11
+ * @param client - Schema Registry where schemas are registered and obtained.
12
+ * Usually this is a SchemaRegistryClient instance.
13
+ */
14
+ constructor(client: SchemaRegistry, options?: JsonSchemaSerializerOptions<MessageT>);
15
+ private readonly schemaGroup?;
16
+ private readonly registry;
17
+ private readonly messageAdapter?;
18
+ private readonly cacheIdByDefinition;
19
+ private readonly cacheById;
20
+ /**
21
+ * serializes the value parameter according to the input schema and creates a message
22
+ * with the serialized data.
23
+ *
24
+ * @param value - The value to serialize.
25
+ * @param schema - The Json schema to use.
26
+ * @returns A new message with the serialized value. The structure of message is
27
+ * constrolled by the message factory option.
28
+ * @throws {@link Error}
29
+ * Thrown if the schema can not be parsed or the value does not match the schema.
30
+ */
31
+ serialize(value: unknown, schema: string): Promise<MessageT>;
32
+ /**
33
+ * Deserializes the payload of the message using the schema ID in the content type
34
+ * field if no schema was provided.
35
+ *
36
+ * @param message - The message with the payload to be deserialized.
37
+ * @returns The deserialized value.
38
+ * @throws {@link Error}
39
+ * Thrown if the deserialization failed, e.g. because reader and writer schemas are incompatible.
40
+ */
41
+ deserialize<T>(message: MessageT, options?: DeserializeOptions): Promise<T>;
42
+ private getSchemaById;
43
+ private getSchemaByDefinition;
44
+ private cache;
45
+ }
46
+ //# sourceMappingURL=jsonSchemaSerializer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsonSchemaSerializer.d.ts","sourceRoot":"","sources":["../../src/jsonSchemaSerializer.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,kBAAkB,EAClB,2BAA2B,EAE3B,cAAc,EACf,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAqB,cAAc,EAAE,MAAM,wBAAwB,CAAC;AA0ChF;;;GAGG;AACH,qBAAa,oBAAoB,CAAC,QAAQ,GAAG,cAAc;IACzD;;;;;OAKG;gBACS,MAAM,EAAE,cAAc,EAAE,OAAO,CAAC,EAAE,2BAA2B,CAAC,QAAQ,CAAC;IAMnF,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAS;IACtC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAiB;IAC1C,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC,CAA2B;IAC3D,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAA8C;IAClF,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA8C;IAExE;;;;;;;;;;OAUG;IACG,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;IAuBlE;;;;;;;;OAQG;IACG,WAAW,CAAC,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,CAAC,EAAE,kBAAkB,GAAG,OAAO,CAAC,CAAC,CAAC;YAkBnE,aAAa;YAkBb,qBAAqB;IAmCnC,OAAO,CAAC,KAAK;CASd"}
@@ -1,10 +1,10 @@
1
1
  // Copyright (c) Microsoft Corporation.
2
2
  // Licensed under the MIT License.
3
3
  import { KnownSchemaFormats } from "@azure/schema-registry";
4
- import { isMessageContent } from "./utility";
5
- import { errorWithCause, wrapError } from "./errors";
4
+ import { isMessageContent } from "./utility.js";
5
+ import { errorWithCause, wrapError } from "./errors.js";
6
6
  import { LRUCache } from "lru-cache";
7
- import { logger } from "./logger";
7
+ import { logger } from "./logger.js";
8
8
  const jsonMimeType = "application/json";
9
9
  const encoder = new TextEncoder();
10
10
  const decoder = new TextDecoder();
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsonSchemaSerializer.js","sourceRoot":"","sources":["../../src/j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AC;QAClC,MAAM,IAAI,KAAK,CAAC,sEAAsE,CAAC,CAAC;IAC1F,CAAC;IACD,IAAI,gBAAgB,CAAC,CAAC,CAAC,KAAK,YAAY,EAAE,CAAC;QACzC,MAAM,IAAI,KAAK,CACb,4BAA4B,gBAAgB,CAAC,CAAC,CAAC,mEAAmE,YAAY,QAAQ,CACvI,CAAC;IACJ,CAAC;IACD,OAAO,gBAAgB,CAAC,CAAC,CAAC,CAAC;AAC7B,CAAC;AAED,SAAS,cAAc,CACrB,OAAiB,EACjB,OAAkC;IAElC,MAAM,eAAe,GAAG,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,CAAC;IACzC,IAAI,eAAe,EAAE,CAAC;QACpB,OAAO,eAAe,CAAC,OAAO,CAAC,CAAC;IAClC,CAAC;SAAM,IAAI,gBAAgB,CAAC,OAAO,CAAC,EAAE,CAAC;QACrC,OAAO,OAAO,CAAC;IACjB,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,KAAK,CACb,6HAA6H,CAC9H,CAAC;IACJ,CAAC;AACH,CAAC;AAED,SAAS,aAAa,CAAC,MAAoB;IACzC,MAAM,EAAE,GAAG,MAAM,CAAC,GAAG,IAAI,MAAM,CAAC,EAAE,CAAC;IACnC,IAAI,CAAC,EAAE,EAAE,CAAC;QACR,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAC7C,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type {\n DeserializeOptions,\n JsonSchemaSerializerOptions,\n MessageAdapter,\n MessageContent,\n} from \"./models.js\";\nimport type { SchemaDescription, SchemaRegistry } from \"@azure/schema-registry\";\nimport { KnownSchemaFormats } from \"@azure/schema-registry\";\nimport { isMessageContent } from \"./utility.js\";\nimport { errorWithCause, wrapError } from \"./errors.js\";\nimport { LRUCache } from \"lru-cache\";\nimport LRUCacheOptions = LRUCache.Options;\nimport { logger } from \"./logger.js\";\n\nconst jsonMimeType = \"application/json\";\nconst encoder = new TextEncoder();\nconst decoder = new TextDecoder();\n\ninterface CacheEntry {\n /** Schema ID */\n id: string;\n /** Schema string */\n schema: string;\n}\ninterface SchemaObject {\n id?: string;\n $id?: string;\n $schema?: string;\n}\nfunction getSchemaObject(schema: string): SchemaObject {\n return wrapError(\n () => JSON.parse(schema),\n `Parsing Json schema failed:\\n\\n\\t${schema}\\n\\nSee 'cause' for more details.`,\n );\n}\n\nconst cacheOptions: LRUCacheOptions<string, any, unknown> = {\n max: 128,\n /**\n * This is needed in order to specify `sizeCalculation` but we do not intend\n * to limit the size just yet.\n */\n maxSize: Number.MAX_VALUE,\n sizeCalculation: (_value: any, key: string) => {\n return key.length;\n },\n};\n\n/**\n * Json serializer that obtains schemas from a schema registry and does not\n * pack schemas into its payloads.\n */\nexport class JsonSchemaSerializer<MessageT = MessageContent> {\n /**\n * Creates a new serializer.\n *\n * @param client - Schema Registry where schemas are registered and obtained.\n * Usually this is a SchemaRegistryClient instance.\n */\n constructor(client: SchemaRegistry, options?: JsonSchemaSerializerOptions<MessageT>) {\n this.registry = client;\n this.schemaGroup = options?.groupName;\n this.messageAdapter = options?.messageAdapter;\n }\n\n private readonly schemaGroup?: string;\n private readonly registry: SchemaRegistry;\n private readonly messageAdapter?: MessageAdapter<MessageT>;\n private readonly cacheIdByDefinition = new LRUCache<string, string>(cacheOptions);\n private readonly cacheById = new LRUCache<string, string>(cacheOptions);\n\n /**\n * serializes the value parameter according to the input schema and creates a message\n * with the serialized data.\n *\n * @param value - The value to serialize.\n * @param schema - The Json schema to use.\n * @returns A new message with the serialized value. The structure of message is\n * constrolled by the message factory option.\n * @throws {@link Error}\n * Thrown if the schema can not be parsed or the value does not match the schema.\n */\n async serialize(value: unknown, schema: string): Promise<MessageT> {\n const entry = await this.getSchemaByDefinition(schema);\n const data = wrapError(\n () => encoder.encode(JSON.stringify(value)),\n `Json serialization failed. See 'cause' for more details. Schema ID: ${entry.id}`,\n );\n const contentType = `${jsonMimeType}+${entry.id}`;\n return this.messageAdapter\n ? this.messageAdapter.produce({\n contentType,\n data,\n })\n : /**\n * If no message consumer was provided, then a MessageContent will be\n * returned. This should work because the MessageT type parameter defaults\n * to MessageContent.\n */\n ({\n data,\n contentType,\n } as MessageContent as unknown as MessageT);\n }\n\n /**\n * Deserializes the payload of the message using the schema ID in the content type\n * field if no schema was provided.\n *\n * @param message - The message with the payload to be deserialized.\n * @returns The deserialized value.\n * @throws {@link Error}\n * Thrown if the deserialization failed, e.g. because reader and writer schemas are incompatible.\n */\n async deserialize<T>(message: MessageT, options?: DeserializeOptions): Promise<T> {\n const { data, contentType } = convertMessage(message, this.messageAdapter);\n const schemaId = getSchemaId(contentType);\n const schema = await this.getSchemaById(schemaId);\n const returnedMessage = wrapError(\n () => JSON.parse(decoder.decode(data)),\n `Json deserialization failed with schema ID (${schemaId}). See 'cause' for more details.`,\n );\n const validate = options?.validateCallback;\n if (validate) {\n wrapError(\n () => validate(returnedMessage, schema),\n `Json validation failed. See 'cause' for more details. Schema ID: ${schemaId}`,\n );\n }\n return returnedMessage as T;\n }\n\n private async getSchemaById(schemaId: string): Promise<string> {\n const cached = this.cacheById.get(schemaId);\n if (cached) {\n return cached;\n }\n const schemaResponse = await this.registry.getSchema(schemaId);\n if (!schemaResponse) {\n throw new Error(`Schema with ID '${schemaId}' not found.`);\n }\n\n if (!schemaResponse.properties.format.match(/^json$/i)) {\n throw new Error(\n `Schema with ID '${schemaResponse.properties.id}' has format '${schemaResponse.properties.format}', not 'json'.`,\n );\n }\n return this.cache(schemaResponse.definition, schemaId).schema;\n }\n\n private async getSchemaByDefinition(definition: string): Promise<CacheEntry> {\n const schemaId = this.cacheIdByDefinition.get(definition);\n if (schemaId) {\n return { id: schemaId, schema: definition };\n }\n if (!this.schemaGroup) {\n throw new Error(\n \"Schema group must have been specified in the constructor options when the client was created in order to serialize.\",\n );\n }\n const schemaObj = getSchemaObject(definition);\n const description: SchemaDescription = {\n groupName: this.schemaGroup,\n name: getSchemaName(schemaObj),\n format: KnownSchemaFormats.Json,\n definition,\n };\n let id: string;\n\n try {\n id = (await this.registry.getSchemaProperties(description)).id;\n } catch (e) {\n if ((e as any).statusCode === 404) {\n throw errorWithCause(\n `Schema '${description.name}' not found in registry group '${description.groupName}', or not found to have matching definition.`,\n e as Error,\n );\n } else {\n throw e;\n }\n }\n\n return this.cache(definition, id);\n }\n\n private cache(schema: string, id: string): CacheEntry {\n const entry = { schema, id };\n this.cacheIdByDefinition.set(schema, id);\n this.cacheById.set(id, schema);\n logger.verbose(\n `Cache entry added or updated. Total number of entries: ${this.cacheIdByDefinition.size}; Total schema length ${this.cacheIdByDefinition.calculatedSize}`,\n );\n return entry;\n }\n}\n\nfunction getSchemaId(contentType: string): string {\n const contentTypeParts = contentType.split(\"+\");\n if (contentTypeParts.length !== 2) {\n throw new Error(\"Content type was not in the expected format of MIME type + schema ID\");\n }\n if (contentTypeParts[0] !== jsonMimeType) {\n throw new Error(\n `Received content of type ${contentTypeParts[0]} but an json serializer may only be used on content that is of '${jsonMimeType}' type`,\n );\n }\n return contentTypeParts[1];\n}\n\nfunction convertMessage<MessageT>(\n message: MessageT,\n adapter?: MessageAdapter<MessageT>,\n): MessageContent {\n const messageConsumer = adapter?.consume;\n if (messageConsumer) {\n return messageConsumer(message);\n } else if (isMessageContent(message)) {\n return message;\n } else {\n throw new Error(\n `Expected either a message adapter to be provided to the serializer or the input message to have data and contentType fields`,\n );\n }\n}\n\nfunction getSchemaName(schema: SchemaObject): string {\n const id = schema.$id || schema.id;\n if (!id) {\n throw new Error(\"Schema must have an ID.\");\n }\n return id;\n}\n"]}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * The \@azure/logger configuration for the schema-registry-json package.
3
+ */
4
+ export declare const logger: import("@azure/logger").AzureLogger;
5
+ //# sourceMappingURL=logger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/logger.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,eAAO,MAAM,MAAM,qCAA6C,CAAC"}
@@ -0,0 +1,51 @@
1
+ /**
2
+ * A message that contains binary data and a content type.
3
+ */
4
+ export interface MessageContent {
5
+ /**
6
+ * The message's binary data
7
+ */
8
+ data: Uint8Array;
9
+ /**
10
+ * The message's content type
11
+ */
12
+ contentType: string;
13
+ }
14
+ /**
15
+ * MessageAdapter is an interface that converts to/from a concrete message type
16
+ * to a MessageContent
17
+ */
18
+ export interface MessageAdapter<MessageT> {
19
+ /**
20
+ * defines how to create a message from a payload and a content type
21
+ */
22
+ produce: (messageContent: MessageContent) => MessageT;
23
+ /**
24
+ * defines how to access the payload and the content type of a message
25
+ */
26
+ consume: (message: MessageT) => MessageContent;
27
+ }
28
+ /**
29
+ * Options for Schema
30
+ */
31
+ export interface JsonSchemaSerializerOptions<MessageT> {
32
+ /**
33
+ * The group name to be used when registering/looking up a schema. Must be specified
34
+ * if `serialize` will be called.
35
+ */
36
+ groupName?: string;
37
+ /**
38
+ * Message Adapter enables the serializer to produce and consume custom messages.
39
+ */
40
+ messageAdapter?: MessageAdapter<MessageT>;
41
+ }
42
+ /**
43
+ * The options to the deserialize method.
44
+ */
45
+ export interface DeserializeOptions {
46
+ /**
47
+ * Validate the value against the schema. Raise an error if the validation is not successful.
48
+ */
49
+ validateCallback?: (value: unknown, schema: string) => void;
50
+ }
51
+ //# sourceMappingURL=models.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"models.d.ts","sourceRoot":"","sources":["../../src/models.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,IAAI,EAAE,UAAU,CAAC;IACjB;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;;GAGG;AACH,MAAM,WAAW,cAAc,CAAC,QAAQ;IACtC;;OAEG;IACH,OAAO,EAAE,CAAC,cAAc,EAAE,cAAc,KAAK,QAAQ,CAAC;IACtD;;OAEG;IACH,OAAO,EAAE,CAAC,OAAO,EAAE,QAAQ,KAAK,cAAc,CAAC;CAChD;AAED;;GAEG;AACH,MAAM,WAAW,2BAA2B,CAAC,QAAQ;IACnD;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;OAEG;IACH,cAAc,CAAC,EAAE,cAAc,CAAC,QAAQ,CAAC,CAAC;CAC3C;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC;;OAEG;IACH,gBAAgB,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;CAC7D"}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,3 @@
1
+ import type { MessageContent } from "./models.js";
2
+ export declare function isMessageContent(message: unknown): message is MessageContent;
3
+ //# sourceMappingURL=utility.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utility.d.ts","sourceRoot":"","sources":["../../src/utility.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAElD,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,IAAI,cAAc,CAG5E"}
@@ -1 +1 @@
1
- {"version":3,"file":"utility.js","sourceRoot":"","sources":["../../src/utility.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAIlC,MAAM,UAAU,gBAAgB,CAAC,OAAgB;IAC/C,MAAM,WAAW,GAAG,OAAyB,CAAC;IAC9C,OAAO,WAAW,CAAC,IAAI,KAAK,SAAS,IAAI,WAAW,CAAC,WAAW,KAAK,SAAS,CAAC;AACjF,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { MessageContent } from \"./models\";\n\nexport function isMessageContent(message: unknown): message is MessageContent {\n const castMessage = message as MessageContent;\n return castMessage.data !== undefined && castMessage.contentType !== undefined;\n}\n"]}
1
+ {"version":3,"file":"utility.js","sourceRoot":"","sources":["../../src/utility.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAIlC,MAAM,UAAU,gBAAgB,CAAC,OAAgB;IAC/C,MAAM,WAAW,GAAG,OAAyB,CAAC;IAC9C,OAAO,WAAW,CAAC,IAAI,KAAK,SAAS,IAAI,WAAW,CAAC,WAAW,KAAK,SAAS,CAAC;AACjF,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { MessageContent } from \"./models.js\";\n\nexport function isMessageContent(message: unknown): message is MessageContent {\n const castMessage = message as MessageContent;\n return castMessage.data !== undefined && castMessage.contentType !== undefined;\n}\n"]}
@@ -0,0 +1,5 @@
1
+ /** @internal */
2
+ export declare function wrapError<T>(f: () => T, message: string): T;
3
+ /** @internal */
4
+ export declare function errorWithCause(message: string, cause: Error): Error;
5
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAGA,gBAAgB;AAChB,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE,MAAM,GAAG,CAAC,CAQ3D;AAED,gBAAgB;AAChB,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,GAAG,KAAK,CASnE"}
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ // Copyright (c) Microsoft Corporation.
3
+ // Licensed under the MIT License.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.wrapError = wrapError;
6
+ exports.errorWithCause = errorWithCause;
7
+ /** @internal */
8
+ function wrapError(f, message) {
9
+ let result;
10
+ try {
11
+ result = f();
12
+ }
13
+ catch (cause) {
14
+ throw errorWithCause(message, cause);
15
+ }
16
+ return result;
17
+ }
18
+ /** @internal */
19
+ function errorWithCause(message, cause) {
20
+ return new Error(message,
21
+ // TS v4.6 and below do not yet recognize the cause option in the Error constructor
22
+ // see https://medium.com/ovrsea/power-up-your-node-js-debugging-and-error-handling-with-the-new-error-cause-feature-4136c563126a
23
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
24
+ // @ts-ignore
25
+ { cause });
26
+ }
27
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;AAGlC,8BAQC;AAGD,wCASC;AArBD,gBAAgB;AAChB,SAAgB,SAAS,CAAI,CAAU,EAAE,OAAe;IACtD,IAAI,MAAS,CAAC;IACd,IAAI,CAAC;QACH,MAAM,GAAG,CAAC,EAAE,CAAC;IACf,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,cAAc,CAAC,OAAO,EAAE,KAAc,CAAC,CAAC;IAChD,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,gBAAgB;AAChB,SAAgB,cAAc,CAAC,OAAe,EAAE,KAAY;IAC1D,OAAO,IAAI,KAAK,CACd,OAAO;IACP,mFAAmF;IACnF,iIAAiI;IACjI,6DAA6D;IAC7D,aAAa;IACb,EAAE,KAAK,EAAE,CACV,CAAC;AACJ,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\n/** @internal */\nexport function wrapError<T>(f: () => T, message: string): T {\n let result: T;\n try {\n result = f();\n } catch (cause) {\n throw errorWithCause(message, cause as Error);\n }\n return result;\n}\n\n/** @internal */\nexport function errorWithCause(message: string, cause: Error): Error {\n return new Error(\n message,\n // TS v4.6 and below do not yet recognize the cause option in the Error constructor\n // see https://medium.com/ovrsea/power-up-your-node-js-debugging-and-error-handling-with-the-new-error-cause-feature-4136c563126a\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n { cause },\n );\n}\n"]}
@@ -0,0 +1,3 @@
1
+ export { JsonSchemaSerializer } from "./jsonSchemaSerializer.js";
2
+ export * from "./models.js";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAEjE,cAAc,aAAa,CAAC"}
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ // Copyright (c) Microsoft Corporation.
3
+ // Licensed under the MIT License.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.JsonSchemaSerializer = void 0;
6
+ const tslib_1 = require("tslib");
7
+ var jsonSchemaSerializer_js_1 = require("./jsonSchemaSerializer.js");
8
+ Object.defineProperty(exports, "JsonSchemaSerializer", { enumerable: true, get: function () { return jsonSchemaSerializer_js_1.JsonSchemaSerializer; } });
9
+ tslib_1.__exportStar(require("./models.js"), exports);
10
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;;;AAElC,qEAAiE;AAAxD,+HAAA,oBAAoB,OAAA;AAE7B,sDAA4B","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nexport { JsonSchemaSerializer } from \"./jsonSchemaSerializer.js\";\n\nexport * from \"./models.js\";\n"]}
@@ -0,0 +1,46 @@
1
+ import type { DeserializeOptions, JsonSchemaSerializerOptions, MessageContent } from "./models.js";
2
+ import type { SchemaRegistry } from "@azure/schema-registry";
3
+ /**
4
+ * Json serializer that obtains schemas from a schema registry and does not
5
+ * pack schemas into its payloads.
6
+ */
7
+ export declare class JsonSchemaSerializer<MessageT = MessageContent> {
8
+ /**
9
+ * Creates a new serializer.
10
+ *
11
+ * @param client - Schema Registry where schemas are registered and obtained.
12
+ * Usually this is a SchemaRegistryClient instance.
13
+ */
14
+ constructor(client: SchemaRegistry, options?: JsonSchemaSerializerOptions<MessageT>);
15
+ private readonly schemaGroup?;
16
+ private readonly registry;
17
+ private readonly messageAdapter?;
18
+ private readonly cacheIdByDefinition;
19
+ private readonly cacheById;
20
+ /**
21
+ * serializes the value parameter according to the input schema and creates a message
22
+ * with the serialized data.
23
+ *
24
+ * @param value - The value to serialize.
25
+ * @param schema - The Json schema to use.
26
+ * @returns A new message with the serialized value. The structure of message is
27
+ * constrolled by the message factory option.
28
+ * @throws {@link Error}
29
+ * Thrown if the schema can not be parsed or the value does not match the schema.
30
+ */
31
+ serialize(value: unknown, schema: string): Promise<MessageT>;
32
+ /**
33
+ * Deserializes the payload of the message using the schema ID in the content type
34
+ * field if no schema was provided.
35
+ *
36
+ * @param message - The message with the payload to be deserialized.
37
+ * @returns The deserialized value.
38
+ * @throws {@link Error}
39
+ * Thrown if the deserialization failed, e.g. because reader and writer schemas are incompatible.
40
+ */
41
+ deserialize<T>(message: MessageT, options?: DeserializeOptions): Promise<T>;
42
+ private getSchemaById;
43
+ private getSchemaByDefinition;
44
+ private cache;
45
+ }
46
+ //# sourceMappingURL=jsonSchemaSerializer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsonSchemaSerializer.d.ts","sourceRoot":"","sources":["../../src/jsonSchemaSerializer.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,kBAAkB,EAClB,2BAA2B,EAE3B,cAAc,EACf,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAqB,cAAc,EAAE,MAAM,wBAAwB,CAAC;AA0ChF;;;GAGG;AACH,qBAAa,oBAAoB,CAAC,QAAQ,GAAG,cAAc;IACzD;;;;;OAKG;gBACS,MAAM,EAAE,cAAc,EAAE,OAAO,CAAC,EAAE,2BAA2B,CAAC,QAAQ,CAAC;IAMnF,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAS;IACtC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAiB;IAC1C,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC,CAA2B;IAC3D,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAA8C;IAClF,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA8C;IAExE;;;;;;;;;;OAUG;IACG,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;IAuBlE;;;;;;;;OAQG;IACG,WAAW,CAAC,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,CAAC,EAAE,kBAAkB,GAAG,OAAO,CAAC,CAAC,CAAC;YAkBnE,aAAa;YAkBb,qBAAqB;IAmCnC,OAAO,CAAC,KAAK;CASd"}
@@ -0,0 +1,177 @@
1
+ "use strict";
2
+ // Copyright (c) Microsoft Corporation.
3
+ // Licensed under the MIT License.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.JsonSchemaSerializer = void 0;
6
+ const schema_registry_1 = require("@azure/schema-registry");
7
+ const utility_js_1 = require("./utility.js");
8
+ const errors_js_1 = require("./errors.js");
9
+ const lru_cache_1 = require("lru-cache");
10
+ const logger_js_1 = require("./logger.js");
11
+ const jsonMimeType = "application/json";
12
+ const encoder = new TextEncoder();
13
+ const decoder = new TextDecoder();
14
+ function getSchemaObject(schema) {
15
+ return (0, errors_js_1.wrapError)(() => JSON.parse(schema), `Parsing Json schema failed:\n\n\t${schema}\n\nSee 'cause' for more details.`);
16
+ }
17
+ const cacheOptions = {
18
+ max: 128,
19
+ /**
20
+ * This is needed in order to specify `sizeCalculation` but we do not intend
21
+ * to limit the size just yet.
22
+ */
23
+ maxSize: Number.MAX_VALUE,
24
+ sizeCalculation: (_value, key) => {
25
+ return key.length;
26
+ },
27
+ };
28
+ /**
29
+ * Json serializer that obtains schemas from a schema registry and does not
30
+ * pack schemas into its payloads.
31
+ */
32
+ class JsonSchemaSerializer {
33
+ /**
34
+ * Creates a new serializer.
35
+ *
36
+ * @param client - Schema Registry where schemas are registered and obtained.
37
+ * Usually this is a SchemaRegistryClient instance.
38
+ */
39
+ constructor(client, options) {
40
+ this.cacheIdByDefinition = new lru_cache_1.LRUCache(cacheOptions);
41
+ this.cacheById = new lru_cache_1.LRUCache(cacheOptions);
42
+ this.registry = client;
43
+ this.schemaGroup = options === null || options === void 0 ? void 0 : options.groupName;
44
+ this.messageAdapter = options === null || options === void 0 ? void 0 : options.messageAdapter;
45
+ }
46
+ /**
47
+ * serializes the value parameter according to the input schema and creates a message
48
+ * with the serialized data.
49
+ *
50
+ * @param value - The value to serialize.
51
+ * @param schema - The Json schema to use.
52
+ * @returns A new message with the serialized value. The structure of message is
53
+ * constrolled by the message factory option.
54
+ * @throws {@link Error}
55
+ * Thrown if the schema can not be parsed or the value does not match the schema.
56
+ */
57
+ async serialize(value, schema) {
58
+ const entry = await this.getSchemaByDefinition(schema);
59
+ const data = (0, errors_js_1.wrapError)(() => encoder.encode(JSON.stringify(value)), `Json serialization failed. See 'cause' for more details. Schema ID: ${entry.id}`);
60
+ const contentType = `${jsonMimeType}+${entry.id}`;
61
+ return this.messageAdapter
62
+ ? this.messageAdapter.produce({
63
+ contentType,
64
+ data,
65
+ })
66
+ : /**
67
+ * If no message consumer was provided, then a MessageContent will be
68
+ * returned. This should work because the MessageT type parameter defaults
69
+ * to MessageContent.
70
+ */
71
+ {
72
+ data,
73
+ contentType,
74
+ };
75
+ }
76
+ /**
77
+ * Deserializes the payload of the message using the schema ID in the content type
78
+ * field if no schema was provided.
79
+ *
80
+ * @param message - The message with the payload to be deserialized.
81
+ * @returns The deserialized value.
82
+ * @throws {@link Error}
83
+ * Thrown if the deserialization failed, e.g. because reader and writer schemas are incompatible.
84
+ */
85
+ async deserialize(message, options) {
86
+ const { data, contentType } = convertMessage(message, this.messageAdapter);
87
+ const schemaId = getSchemaId(contentType);
88
+ const schema = await this.getSchemaById(schemaId);
89
+ const returnedMessage = (0, errors_js_1.wrapError)(() => JSON.parse(decoder.decode(data)), `Json deserialization failed with schema ID (${schemaId}). See 'cause' for more details.`);
90
+ const validate = options === null || options === void 0 ? void 0 : options.validateCallback;
91
+ if (validate) {
92
+ (0, errors_js_1.wrapError)(() => validate(returnedMessage, schema), `Json validation failed. See 'cause' for more details. Schema ID: ${schemaId}`);
93
+ }
94
+ return returnedMessage;
95
+ }
96
+ async getSchemaById(schemaId) {
97
+ const cached = this.cacheById.get(schemaId);
98
+ if (cached) {
99
+ return cached;
100
+ }
101
+ const schemaResponse = await this.registry.getSchema(schemaId);
102
+ if (!schemaResponse) {
103
+ throw new Error(`Schema with ID '${schemaId}' not found.`);
104
+ }
105
+ if (!schemaResponse.properties.format.match(/^json$/i)) {
106
+ throw new Error(`Schema with ID '${schemaResponse.properties.id}' has format '${schemaResponse.properties.format}', not 'json'.`);
107
+ }
108
+ return this.cache(schemaResponse.definition, schemaId).schema;
109
+ }
110
+ async getSchemaByDefinition(definition) {
111
+ const schemaId = this.cacheIdByDefinition.get(definition);
112
+ if (schemaId) {
113
+ return { id: schemaId, schema: definition };
114
+ }
115
+ if (!this.schemaGroup) {
116
+ throw new Error("Schema group must have been specified in the constructor options when the client was created in order to serialize.");
117
+ }
118
+ const schemaObj = getSchemaObject(definition);
119
+ const description = {
120
+ groupName: this.schemaGroup,
121
+ name: getSchemaName(schemaObj),
122
+ format: schema_registry_1.KnownSchemaFormats.Json,
123
+ definition,
124
+ };
125
+ let id;
126
+ try {
127
+ id = (await this.registry.getSchemaProperties(description)).id;
128
+ }
129
+ catch (e) {
130
+ if (e.statusCode === 404) {
131
+ throw (0, errors_js_1.errorWithCause)(`Schema '${description.name}' not found in registry group '${description.groupName}', or not found to have matching definition.`, e);
132
+ }
133
+ else {
134
+ throw e;
135
+ }
136
+ }
137
+ return this.cache(definition, id);
138
+ }
139
+ cache(schema, id) {
140
+ const entry = { schema, id };
141
+ this.cacheIdByDefinition.set(schema, id);
142
+ this.cacheById.set(id, schema);
143
+ logger_js_1.logger.verbose(`Cache entry added or updated. Total number of entries: ${this.cacheIdByDefinition.size}; Total schema length ${this.cacheIdByDefinition.calculatedSize}`);
144
+ return entry;
145
+ }
146
+ }
147
+ exports.JsonSchemaSerializer = JsonSchemaSerializer;
148
+ function getSchemaId(contentType) {
149
+ const contentTypeParts = contentType.split("+");
150
+ if (contentTypeParts.length !== 2) {
151
+ throw new Error("Content type was not in the expected format of MIME type + schema ID");
152
+ }
153
+ if (contentTypeParts[0] !== jsonMimeType) {
154
+ throw new Error(`Received content of type ${contentTypeParts[0]} but an json serializer may only be used on content that is of '${jsonMimeType}' type`);
155
+ }
156
+ return contentTypeParts[1];
157
+ }
158
+ function convertMessage(message, adapter) {
159
+ const messageConsumer = adapter === null || adapter === void 0 ? void 0 : adapter.consume;
160
+ if (messageConsumer) {
161
+ return messageConsumer(message);
162
+ }
163
+ else if ((0, utility_js_1.isMessageContent)(message)) {
164
+ return message;
165
+ }
166
+ else {
167
+ throw new Error(`Expected either a message adapter to be provided to the serializer or the input message to have data and contentType fields`);
168
+ }
169
+ }
170
+ function getSchemaName(schema) {
171
+ const id = schema.$id || schema.id;
172
+ if (!id) {
173
+ throw new Error("Schema must have an ID.");
174
+ }
175
+ return id;
176
+ }
177
+ //# sourceMappingURL=jsonSchemaSerializer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsonSchemaSerializer.js","sourceRoot":"","sources":["../../src/jsonSchemaSerializer.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;;AASlC,4DAA4D;AAC5D,6CAAgD;AAChD,2CAAwD;AACxD,yCAAqC;AAErC,2CAAqC;AAErC,MAAM,YAAY,GAAG,kBAAkB,CAAC;AACxC,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAClC,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAalC,SAAS,eAAe,CAAC,MAAc;IACrC,OAAO,IAAA,qBAAS,EACd,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EACxB,oCAAoC,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AC,EAAE,CAAC;QACrC,OAAO,OAAO,CAAC;IACjB,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,KAAK,CACb,6HAA6H,CAC9H,CAAC;IACJ,CAAC;AACH,CAAC;AAED,SAAS,aAAa,CAAC,MAAoB;IACzC,MAAM,EAAE,GAAG,MAAM,CAAC,GAAG,IAAI,MAAM,CAAC,EAAE,CAAC;IACnC,IAAI,CAAC,EAAE,EAAE,CAAC;QACR,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAC7C,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type {\n DeserializeOptions,\n JsonSchemaSerializerOptions,\n MessageAdapter,\n MessageContent,\n} from \"./models.js\";\nimport type { SchemaDescription, SchemaRegistry } from \"@azure/schema-registry\";\nimport { KnownSchemaFormats } from \"@azure/schema-registry\";\nimport { isMessageContent } from \"./utility.js\";\nimport { errorWithCause, wrapError } from \"./errors.js\";\nimport { LRUCache } from \"lru-cache\";\nimport LRUCacheOptions = LRUCache.Options;\nimport { logger } from \"./logger.js\";\n\nconst jsonMimeType = \"application/json\";\nconst encoder = new TextEncoder();\nconst decoder = new TextDecoder();\n\ninterface CacheEntry {\n /** Schema ID */\n id: string;\n /** Schema string */\n schema: string;\n}\ninterface SchemaObject {\n id?: string;\n $id?: string;\n $schema?: string;\n}\nfunction getSchemaObject(schema: string): SchemaObject {\n return wrapError(\n () => JSON.parse(schema),\n `Parsing Json schema failed:\\n\\n\\t${schema}\\n\\nSee 'cause' for more details.`,\n );\n}\n\nconst cacheOptions: LRUCacheOptions<string, any, unknown> = {\n max: 128,\n /**\n * This is needed in order to specify `sizeCalculation` but we do not intend\n * to limit the size just yet.\n */\n maxSize: Number.MAX_VALUE,\n sizeCalculation: (_value: any, key: string) => {\n return key.length;\n },\n};\n\n/**\n * Json serializer that obtains schemas from a schema registry and does not\n * pack schemas into its payloads.\n */\nexport class JsonSchemaSerializer<MessageT = MessageContent> {\n /**\n * Creates a new serializer.\n *\n * @param client - Schema Registry where schemas are registered and obtained.\n * Usually this is a SchemaRegistryClient instance.\n */\n constructor(client: SchemaRegistry, options?: JsonSchemaSerializerOptions<MessageT>) {\n this.registry = client;\n this.schemaGroup = options?.groupName;\n this.messageAdapter = options?.messageAdapter;\n }\n\n private readonly schemaGroup?: string;\n private readonly registry: SchemaRegistry;\n private readonly messageAdapter?: MessageAdapter<MessageT>;\n private readonly cacheIdByDefinition = new LRUCache<string, string>(cacheOptions);\n private readonly cacheById = new LRUCache<string, string>(cacheOptions);\n\n /**\n * serializes the value parameter according to the input schema and creates a message\n * with the serialized data.\n *\n * @param value - The value to serialize.\n * @param schema - The Json schema to use.\n * @returns A new message with the serialized value. The structure of message is\n * constrolled by the message factory option.\n * @throws {@link Error}\n * Thrown if the schema can not be parsed or the value does not match the schema.\n */\n async serialize(value: unknown, schema: string): Promise<MessageT> {\n const entry = await this.getSchemaByDefinition(schema);\n const data = wrapError(\n () => encoder.encode(JSON.stringify(value)),\n `Json serialization failed. See 'cause' for more details. Schema ID: ${entry.id}`,\n );\n const contentType = `${jsonMimeType}+${entry.id}`;\n return this.messageAdapter\n ? this.messageAdapter.produce({\n contentType,\n data,\n })\n : /**\n * If no message consumer was provided, then a MessageContent will be\n * returned. This should work because the MessageT type parameter defaults\n * to MessageContent.\n */\n ({\n data,\n contentType,\n } as MessageContent as unknown as MessageT);\n }\n\n /**\n * Deserializes the payload of the message using the schema ID in the content type\n * field if no schema was provided.\n *\n * @param message - The message with the payload to be deserialized.\n * @returns The deserialized value.\n * @throws {@link Error}\n * Thrown if the deserialization failed, e.g. because reader and writer schemas are incompatible.\n */\n async deserialize<T>(message: MessageT, options?: DeserializeOptions): Promise<T> {\n const { data, contentType } = convertMessage(message, this.messageAdapter);\n const schemaId = getSchemaId(contentType);\n const schema = await this.getSchemaById(schemaId);\n const returnedMessage = wrapError(\n () => JSON.parse(decoder.decode(data)),\n `Json deserialization failed with schema ID (${schemaId}). See 'cause' for more details.`,\n );\n const validate = options?.validateCallback;\n if (validate) {\n wrapError(\n () => validate(returnedMessage, schema),\n `Json validation failed. See 'cause' for more details. Schema ID: ${schemaId}`,\n );\n }\n return returnedMessage as T;\n }\n\n private async getSchemaById(schemaId: string): Promise<string> {\n const cached = this.cacheById.get(schemaId);\n if (cached) {\n return cached;\n }\n const schemaResponse = await this.registry.getSchema(schemaId);\n if (!schemaResponse) {\n throw new Error(`Schema with ID '${schemaId}' not found.`);\n }\n\n if (!schemaResponse.properties.format.match(/^json$/i)) {\n throw new Error(\n `Schema with ID '${schemaResponse.properties.id}' has format '${schemaResponse.properties.format}', not 'json'.`,\n );\n }\n return this.cache(schemaResponse.definition, schemaId).schema;\n }\n\n private async getSchemaByDefinition(definition: string): Promise<CacheEntry> {\n const schemaId = this.cacheIdByDefinition.get(definition);\n if (schemaId) {\n return { id: schemaId, schema: definition };\n }\n if (!this.schemaGroup) {\n throw new Error(\n \"Schema group must have been specified in the constructor options when the client was created in order to serialize.\",\n );\n }\n const schemaObj = getSchemaObject(definition);\n const description: SchemaDescription = {\n groupName: this.schemaGroup,\n name: getSchemaName(schemaObj),\n format: KnownSchemaFormats.Json,\n definition,\n };\n let id: string;\n\n try {\n id = (await this.registry.getSchemaProperties(description)).id;\n } catch (e) {\n if ((e as any).statusCode === 404) {\n throw errorWithCause(\n `Schema '${description.name}' not found in registry group '${description.groupName}', or not found to have matching definition.`,\n e as Error,\n );\n } else {\n throw e;\n }\n }\n\n return this.cache(definition, id);\n }\n\n private cache(schema: string, id: string): CacheEntry {\n const entry = { schema, id };\n this.cacheIdByDefinition.set(schema, id);\n this.cacheById.set(id, schema);\n logger.verbose(\n `Cache entry added or updated. Total number of entries: ${this.cacheIdByDefinition.size}; Total schema length ${this.cacheIdByDefinition.calculatedSize}`,\n );\n return entry;\n }\n}\n\nfunction getSchemaId(contentType: string): string {\n const contentTypeParts = contentType.split(\"+\");\n if (contentTypeParts.length !== 2) {\n throw new Error(\"Content type was not in the expected format of MIME type + schema ID\");\n }\n if (contentTypeParts[0] !== jsonMimeType) {\n throw new Error(\n `Received content of type ${contentTypeParts[0]} but an json serializer may only be used on content that is of '${jsonMimeType}' type`,\n );\n }\n return contentTypeParts[1];\n}\n\nfunction convertMessage<MessageT>(\n message: MessageT,\n adapter?: MessageAdapter<MessageT>,\n): MessageContent {\n const messageConsumer = adapter?.consume;\n if (messageConsumer) {\n return messageConsumer(message);\n } else if (isMessageContent(message)) {\n return message;\n } else {\n throw new Error(\n `Expected either a message adapter to be provided to the serializer or the input message to have data and contentType fields`,\n );\n }\n}\n\nfunction getSchemaName(schema: SchemaObject): string {\n const id = schema.$id || schema.id;\n if (!id) {\n throw new Error(\"Schema must have an ID.\");\n }\n return id;\n}\n"]}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * The \@azure/logger configuration for the schema-registry-json package.
3
+ */
4
+ export declare const logger: import("@azure/logger").AzureLogger;
5
+ //# sourceMappingURL=logger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/logger.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,eAAO,MAAM,MAAM,qCAA6C,CAAC"}
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ // Copyright (c) Microsoft Corporation.
3
+ // Licensed under the MIT License.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.logger = void 0;
6
+ const logger_1 = require("@azure/logger");
7
+ /**
8
+ * The \@azure/logger configuration for the schema-registry-json package.
9
+ */
10
+ exports.logger = (0, logger_1.createClientLogger)("schema-registry-json");
11
+ //# sourceMappingURL=logger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.js","sourceRoot":"","sources":["../../src/logger.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;;AAElC,0CAAmD;AAEnD;;GAEG;AACU,QAAA,MAAM,GAAG,IAAA,2BAAkB,EAAC,sBAAsB,CAAC,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport { createClientLogger } from \"@azure/logger\";\n\n/**\n * The \\@azure/logger configuration for the schema-registry-json package.\n */\nexport const logger = createClientLogger(\"schema-registry-json\");\n"]}
@@ -0,0 +1,51 @@
1
+ /**
2
+ * A message that contains binary data and a content type.
3
+ */
4
+ export interface MessageContent {
5
+ /**
6
+ * The message's binary data
7
+ */
8
+ data: Uint8Array;
9
+ /**
10
+ * The message's content type
11
+ */
12
+ contentType: string;
13
+ }
14
+ /**
15
+ * MessageAdapter is an interface that converts to/from a concrete message type
16
+ * to a MessageContent
17
+ */
18
+ export interface MessageAdapter<MessageT> {
19
+ /**
20
+ * defines how to create a message from a payload and a content type
21
+ */
22
+ produce: (messageContent: MessageContent) => MessageT;
23
+ /**
24
+ * defines how to access the payload and the content type of a message
25
+ */
26
+ consume: (message: MessageT) => MessageContent;
27
+ }
28
+ /**
29
+ * Options for Schema
30
+ */
31
+ export interface JsonSchemaSerializerOptions<MessageT> {
32
+ /**
33
+ * The group name to be used when registering/looking up a schema. Must be specified
34
+ * if `serialize` will be called.
35
+ */
36
+ groupName?: string;
37
+ /**
38
+ * Message Adapter enables the serializer to produce and consume custom messages.
39
+ */
40
+ messageAdapter?: MessageAdapter<MessageT>;
41
+ }
42
+ /**
43
+ * The options to the deserialize method.
44
+ */
45
+ export interface DeserializeOptions {
46
+ /**
47
+ * Validate the value against the schema. Raise an error if the validation is not successful.
48
+ */
49
+ validateCallback?: (value: unknown, schema: string) => void;
50
+ }
51
+ //# sourceMappingURL=models.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"models.d.ts","sourceRoot":"","sources":["../../src/models.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,IAAI,EAAE,UAAU,CAAC;IACjB;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;;GAGG;AACH,MAAM,WAAW,cAAc,CAAC,QAAQ;IACtC;;OAEG;IACH,OAAO,EAAE,CAAC,cAAc,EAAE,cAAc,KAAK,QAAQ,CAAC;IACtD;;OAEG;IACH,OAAO,EAAE,CAAC,OAAO,EAAE,QAAQ,KAAK,cAAc,CAAC;CAChD;AAED;;GAEG;AACH,MAAM,WAAW,2BAA2B,CAAC,QAAQ;IACnD;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;OAEG;IACH,cAAc,CAAC,EAAE,cAAc,CAAC,QAAQ,CAAC,CAAC;CAC3C;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC;;OAEG;IACH,gBAAgB,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;CAC7D"}