@databricks/zerobus-ingest-sdk 1.0.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Options for loading a descriptor from a FileDescriptorSet.
3
+ */
4
+ export interface LoadDescriptorOptions {
5
+ /**
6
+ * Path to the descriptor file generated by protoc.
7
+ * Example: 'schemas/my_schema_descriptor.pb'
8
+ */
9
+ descriptorPath: string;
10
+
11
+ /**
12
+ * Name of the proto file within the FileDescriptorSet.
13
+ * This should match the filename in your .proto file.
14
+ * Example: 'schemas/air_quality.proto' or 'air_quality.proto'
15
+ */
16
+ protoFileName: string;
17
+
18
+ /**
19
+ * Name of the message type to extract.
20
+ * Example: 'AirQuality'
21
+ */
22
+ messageName: string;
23
+ }
24
+
25
+ /**
26
+ * Loads a specific message's DescriptorProto from a FileDescriptorSet.
27
+ *
28
+ * @param options - Configuration specifying which message to extract
29
+ * @returns Base64-encoded DescriptorProto string for the specified message
30
+ * @throws Error if the descriptor file, proto file, or message cannot be found
31
+ */
32
+ export function loadDescriptorProto(options: LoadDescriptorOptions): string;
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * Utility functions for working with Protocol Buffer descriptors.
5
+ *
6
+ * These utilities help extract message descriptors from FileDescriptorSets
7
+ * generated by protoc, providing flexibility in choosing which message to use.
8
+ */
9
+
10
+ const fs = require("fs");
11
+ const descriptor = require("protobufjs/ext/descriptor");
12
+
13
+ /**
14
+ * Loads a specific message's DescriptorProto from a FileDescriptorSet.
15
+ *
16
+ * @param {object} options
17
+ * @param {string} options.descriptorPath Path to the descriptor file generated by protoc.
18
+ * @param {string} options.protoFileName Name of the proto file within the FileDescriptorSet.
19
+ * @param {string} options.messageName Name of the message type to extract.
20
+ * @returns {string} Base64-encoded DescriptorProto string for the specified message.
21
+ */
22
+ function loadDescriptorProto(options) {
23
+ const { descriptorPath, protoFileName, messageName } = options;
24
+
25
+ const descriptorBytes = fs.readFileSync(descriptorPath);
26
+ const fileDescriptorSet = descriptor.FileDescriptorSet.decode(descriptorBytes);
27
+ const fileDescriptor = fileDescriptorSet.file.find((file) =>
28
+ file.name === protoFileName ||
29
+ file.name.endsWith("/" + protoFileName)
30
+ );
31
+
32
+ if (!fileDescriptor) {
33
+ throw new Error(
34
+ `Proto file '${protoFileName}' not found in descriptor. Available files: ${
35
+ fileDescriptorSet.file.map((file) => file.name).join(", ")
36
+ }`
37
+ );
38
+ }
39
+
40
+ const messageType = fileDescriptor.messageType?.find((message) => message.name === messageName);
41
+
42
+ if (!messageType) {
43
+ const availableMessages = fileDescriptor.messageType?.map((message) => message.name).join(", ") || "none";
44
+ throw new Error(
45
+ `Message '${messageName}' not found in ${protoFileName}. Available messages: ${availableMessages}`
46
+ );
47
+ }
48
+
49
+ const descriptorProtoBytes = descriptor.DescriptorProto.encode(messageType).finish();
50
+ return Buffer.from(descriptorProtoBytes).toString("base64");
51
+ }
52
+
53
+ module.exports = {
54
+ loadDescriptorProto,
55
+ };
@@ -49,7 +49,7 @@ export interface LoadDescriptorOptions {
49
49
  *
50
50
  * @example
51
51
  * ```typescript
52
- * import { loadDescriptorProto } from '@databricks/zerobus-sdk/utils/descriptor';
52
+ * import { loadDescriptorProto } from '@databricks/zerobus-ingest-sdk/utils/descriptor.js';
53
53
  *
54
54
  * const descriptorBase64 = loadDescriptorProto({
55
55
  * descriptorPath: 'schemas/air_quality_descriptor.pb',
@@ -74,7 +74,7 @@ export function loadDescriptorProto(options: LoadDescriptorOptions): string {
74
74
 
75
75
  // Find the file descriptor matching the proto file name
76
76
  const fileDescriptor = fileDescriptorSet.file.find((f: any) =>
77
- f.name === protoFileName || f.name.endsWith('/' + protoFileName) || f.name.endsWith(protoFileName)
77
+ f.name === protoFileName || f.name.endsWith('/' + protoFileName)
78
78
  );
79
79
 
80
80
  if (!fileDescriptor) {