@@ -30,6 +30,17 @@ declare module 'protobufjs' {
30
30
descriptor . IDescriptorProto ;
31
31
}
32
32
33
+ interface RootConstructor {
34
+ new ( options ?: Options ) : Root ;
35
+ fromDescriptor (
36
+ descriptorSet :
37
+ | descriptor . IFileDescriptorSet
38
+ | Protobuf . Reader
39
+ | Uint8Array
40
+ ) : Root ;
41
+ fromJSON ( json : Protobuf . INamespace , root ?: Root ) : Root ;
42
+ }
43
+
33
44
interface Root {
34
45
toDescriptor (
35
46
protoVersion : string
@@ -98,6 +109,9 @@ export type Options = Protobuf.IParseOptions &
98
109
includeDirs ?: string [ ] ;
99
110
} ;
100
111
112
+ type DecodedDescriptorSet = Protobuf . Message < descriptor . IFileDescriptorSet > &
113
+ descriptor . IFileDescriptorSet ;
114
+
101
115
const descriptorOptions : Protobuf . IConversionOptions = {
102
116
longs : String ,
103
117
enums : String ,
@@ -307,6 +321,19 @@ function addIncludePathResolver(root: Protobuf.Root, includePaths: string[]) {
307
321
} ;
308
322
}
309
323
324
+ function createPackageDefinitionFromDescriptorSet (
325
+ decodedDescriptorSet : DecodedDescriptorSet ,
326
+ options ?: Options
327
+ ) {
328
+ options = options || { } ;
329
+
330
+ const root = ( Protobuf . Root as Protobuf . RootConstructor ) . fromDescriptor (
331
+ decodedDescriptorSet
332
+ ) ;
333
+ root . resolveAll ( ) ;
334
+ return createPackageDefinition ( root , options ) ;
335
+ }
336
+
310
337
/**
311
338
* Load a .proto file with the specified options.
312
339
* @param filename One or multiple file paths to load. Can be an absolute path
@@ -368,6 +395,34 @@ export function loadSync(
368
395
return createPackageDefinition ( root , options ! ) ;
369
396
}
370
397
398
+ export function loadFileDescriptorSetFromBuffer (
399
+ descriptorSet : Buffer ,
400
+ options ?: Options
401
+ ) : PackageDefinition {
402
+ const decodedDescriptorSet = descriptor . FileDescriptorSet . decode (
403
+ descriptorSet
404
+ ) as DecodedDescriptorSet ;
405
+
406
+ return createPackageDefinitionFromDescriptorSet (
407
+ decodedDescriptorSet ,
408
+ options
409
+ ) ;
410
+ }
411
+
412
+ export function loadFileDescriptorSetFromObject (
413
+ descriptorSet : Parameters < typeof descriptor . FileDescriptorSet . fromObject > [ 0 ] ,
414
+ options ?: Options
415
+ ) : PackageDefinition {
416
+ const decodedDescriptorSet = descriptor . FileDescriptorSet . fromObject (
417
+ descriptorSet
418
+ ) as DecodedDescriptorSet ;
419
+
420
+ return createPackageDefinitionFromDescriptorSet (
421
+ decodedDescriptorSet ,
422
+ options
423
+ ) ;
424
+ }
425
+
371
426
// Load Google's well-known proto files that aren't exposed by Protobuf.js.
372
427
373
428
// Protobuf.js exposes: any, duration, empty, field_mask, struct, timestamp,
0 commit comments