/
index.js
73 lines (65 loc) · 2.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const grpc = require('@grpc/grpc-js')
const forOwn = require('lodash.forown')
/**
* Simple Node.js helper utility for creating gRPC metadata
*
* @example <caption>Installation</caption>
* npm install grpc-create-metadata
*
* @example <caption>Usage</caption>
* const grpc = require('grpc')
* const create = require('grpc-create-metadata')
* const meta = create({
* name: 'Bob',
* age: 20,
* active: true
* });
* console.log(meta instanceof grpc.Metadata) // true
* console.dir(meta.getMap()) // { foo: 'bar', age: '12', prop: 'true' }
* @module grpc-create-metadata
*/
/**
* 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
* @param {Object} options options
* @param {Boolean} options.addEmpty whether to add empty strings. Default: <code>false</code>
* @return {Metadata} An instance of <code>Metadata</code>, or `undefined` if input is not an object
*/
module.exports = function create (metadata, options) {
if (!isObject(metadata)) {
return
}
if (metadata instanceof grpc.Metadata) {
return metadata
}
const meta = new grpc.Metadata()
forOwn(metadata, (v, k) => {
if (Buffer.isBuffer(v)) {
meta.add(k, v)
} else if (v !== null && !isUndefined(v)) {
const toAdd = isString(v) ? v : v.toString()
if (toAdd || (options && options.addEmpty)) {
meta.add(k, toAdd)
}
}
})
return meta
}
function isObject (value) {
const type = typeof value
return value !== null && type === 'object'
}
function isString (value) {
const type = typeof value
return type === 'string'
}
function isUndefined (value) {
const type = typeof value
return value !== null && type === 'undefined'
}