-
-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9b811e8
commit d3628bf
Showing
3 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Root } from 'protobufjs/light'; | ||
|
||
type Data = Record<string, unknown>; | ||
|
||
export const loadProtobuf = async ( | ||
messages: Root, | ||
packageName: string, | ||
packageLoader: () => Data | Promise<Data>, | ||
) => { | ||
// check if messages already exists | ||
try { | ||
const pkg = messages.lookup(packageName); | ||
if (pkg) { | ||
return; | ||
} | ||
} catch {} | ||
|
||
// get current MessageType enum | ||
let enumType; | ||
try { | ||
enumType = messages.lookupEnum('MessageType'); | ||
} catch {} | ||
|
||
// load definitions | ||
const packageMessages = await packageLoader(); | ||
const pkg = messages.define(packageName, packageMessages); | ||
// get package MessageType enum | ||
let packageEnumType; | ||
try { | ||
packageEnumType = pkg.lookupEnum('MessageType'); | ||
} catch {} | ||
|
||
// merge both enums | ||
if (enumType && packageEnumType) { | ||
Object.keys(packageEnumType.values).forEach(key => { | ||
enumType.add(key, packageEnumType.values[key]); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import * as ProtoBuf from 'protobufjs/light'; | ||
|
||
import { loadProtobuf } from '../src/load'; | ||
|
||
describe('protocol-thp/loadProtobuf', () => { | ||
const createProtobufRoot = () => { | ||
return ProtoBuf.Root.fromJSON({ | ||
nested: { | ||
MessageType: { | ||
values: { | ||
Initialize: 0, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}; | ||
|
||
it('merge MessageType enum', async () => { | ||
const root = createProtobufRoot(); | ||
await loadProtobuf(root, 'bitcoin', () => { | ||
return Promise.resolve({ | ||
MessageType: { | ||
values: { | ||
GetAddress: 29, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
const keys = root.lookupEnum('MessageType')?.values; | ||
expect(keys).toEqual({ | ||
Initialize: 0, | ||
GetAddress: 29, | ||
}); | ||
}); | ||
|
||
it('throw on merge MessageType enum', async () => { | ||
await expect( | ||
loadProtobuf(createProtobufRoot(), 'bitcoin', () => { | ||
return Promise.resolve({ | ||
MessageType: { | ||
values: { | ||
GetAddress: 0, | ||
}, | ||
}, | ||
}); | ||
}), | ||
).rejects.toThrow('duplicate id 0'); | ||
|
||
await expect( | ||
loadProtobuf(createProtobufRoot(), 'bitcoin', () => { | ||
return Promise.resolve({ | ||
MessageType: { | ||
values: { | ||
Initialize: 1, | ||
}, | ||
}, | ||
}); | ||
}), | ||
).rejects.toThrow('duplicate name'); | ||
}); | ||
|
||
it('create MessageType enum', async () => { | ||
const root = createProtobufRoot(); | ||
root.remove(root.lookupEnum('MessageType')); | ||
|
||
await loadProtobuf(root, 'bitcoin', () => { | ||
return Promise.resolve({ | ||
MessageType: { | ||
values: { | ||
GetAddress: 29, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
const keys = root.lookupEnum('MessageType')?.values; | ||
expect(keys).toEqual({ | ||
GetAddress: 29, | ||
}); | ||
}); | ||
|
||
it('already loaded', async () => { | ||
const root = createProtobufRoot(); | ||
root.define('bitcoin', { | ||
MessageType: { | ||
values: { | ||
GetAddress: 29, | ||
}, | ||
}, | ||
}); | ||
|
||
const spy = jest.fn(); | ||
await loadProtobuf(root, 'bitcoin', spy); | ||
|
||
expect(spy).toHaveBeenCalledTimes(0); | ||
}); | ||
}); |