-
-
Notifications
You must be signed in to change notification settings - Fork 60
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
Showing
9 changed files
with
160 additions
and
43 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,41 @@ | ||
const grpc = require('@grpc/grpc-js') | ||
|
||
/** | ||
* Utility helper function to create <code>Metadata</code> object from plain Javascript object | ||
* This strictly just calls <code>Metadata.add</code> with the key / value map of objects. | ||
* If the value is a <code>Buffer</code> it's passed as is. | ||
* If the value is a <code>Sting</code> it's passed as is. | ||
* Else if the value defined and not a string we simply call <code>toString()</code>. | ||
* Note that <code>Metadata</code> only accept string or buffer values. | ||
* @param {Object} metadata Plain javascript object to tranform into <code>Metadata</code> | ||
* If an instance of <code>Metadata</code> is passed in it is simply returned | ||
* @return {Metadata} An instance of <code>Metadata</code>, or `undefined` if input is not an object | ||
*/ | ||
module.exports = function create (metadata) { | ||
if (typeof metadata !== 'object') { | ||
return | ||
} | ||
|
||
if (metadata instanceof grpc.Metadata) { | ||
return metadata | ||
} | ||
|
||
const meta = new grpc.Metadata() | ||
|
||
for (const k in metadata) { | ||
const v = metadata[k] | ||
if (Buffer.isBuffer(v)) { | ||
if (!k.endsWith('-bin')) { | ||
throw new Error(`Metadata key "${k}" should end with "-bin" since it has a non-string value.`) | ||
} | ||
meta.add(k, v) | ||
} else if (v !== null && typeof v !== 'undefined') { | ||
const toAdd = typeof v === 'string' ? v : v.toString() | ||
if (toAdd) { | ||
meta.add(k, toAdd) | ||
} | ||
} | ||
} | ||
|
||
return meta | ||
} |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,26 @@ | ||
const Benchmark = require('benchmark') | ||
const metaCreate = require('../lib/metadata') | ||
const Mali = require('../lib/app') | ||
const path = require('path') | ||
|
||
const suite = new Benchmark.Suite() | ||
|
||
const largeMetadata = { | ||
hello: 'world', | ||
maxSafe: Number.MAX_SAFE_INTEGER | ||
} | ||
|
||
for (let i = 0; i < 5000; i++) { | ||
largeMetadata[`key-${i}`] = `value-${i}` | ||
} | ||
|
||
suite | ||
.add('metadata.create', () => { | ||
metaCreate(largeMetadata) | ||
}) | ||
.add('mali.addService', () => { | ||
const app = new Mali() | ||
app.addService(path.join(__dirname, './protos/helloworld.proto'), 'Greeter') | ||
}) | ||
.on('cycle', (event) => console.log(event.target.toString())) | ||
.run({ async: true }) |
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,31 @@ | ||
const test = require('ava') | ||
const grpc = require('@grpc/grpc-js') | ||
const create = require('../lib/metadata') | ||
|
||
test('should return undefined if not-object', (t) => { | ||
t.is(create('hello'), undefined) | ||
t.is(create(undefined), undefined) | ||
}) | ||
|
||
test('should return grpc.Metadata', (t) => { | ||
const meta = new grpc.Metadata() | ||
meta.set('hello', 'world') | ||
|
||
const returned = create(meta) | ||
t.truthy(returned instanceof grpc.Metadata) | ||
t.deepEqual(meta.get('hello'), ['world']) | ||
}) | ||
|
||
test('should handle buffers', (t) => { | ||
const meta = create({ 'john-bin': Buffer.from('snow'), 'non-existing': null, john: [] }) | ||
t.deepEqual(meta.get('john-bin'), [Buffer.from('snow')]) | ||
t.deepEqual(meta.get('non-existing'), []) | ||
t.deepEqual(meta.get('john'), []) | ||
|
||
try { | ||
create({ john: Buffer.from('snow') }) | ||
t.fail() | ||
} catch (error) { | ||
t.is(error.message, 'Metadata key "john" should end with "-bin" since it has a non-string value.') | ||
} | ||
}) |