src_metadata.js.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: src/metadata.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: src/metadata.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/**
  20. * @license
  21. * Copyright 2015 gRPC authors.
  22. *
  23. * Licensed under the Apache License, Version 2.0 (the "License");
  24. * you may not use this file except in compliance with the License.
  25. * You may obtain a copy of the License at
  26. *
  27. * http://www.apache.org/licenses/LICENSE-2.0
  28. *
  29. * Unless required by applicable law or agreed to in writing, software
  30. * distributed under the License is distributed on an "AS IS" BASIS,
  31. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  32. * See the License for the specific language governing permissions and
  33. * limitations under the License.
  34. *
  35. */
  36. 'use strict';
  37. var _ = require('lodash');
  38. var grpc = require('./grpc_extension');
  39. /**
  40. * Class for storing metadata. Keys are normalized to lowercase ASCII.
  41. * @memberof grpc
  42. * @constructor
  43. * @example
  44. * var metadata = new metadata_module.Metadata();
  45. * metadata.set('key1', 'value1');
  46. * metadata.add('key1', 'value2');
  47. * metadata.get('key1') // returns ['value1', 'value2']
  48. */
  49. function Metadata() {
  50. this._internal_repr = {};
  51. }
  52. function normalizeKey(key) {
  53. key = key.toLowerCase();
  54. if (grpc.metadataKeyIsLegal(key)) {
  55. return key;
  56. } else {
  57. throw new Error('Metadata key"' + key + '" contains illegal characters');
  58. }
  59. }
  60. function validate(key, value) {
  61. if (grpc.metadataKeyIsBinary(key)) {
  62. if (!(value instanceof Buffer)) {
  63. throw new Error('keys that end with \'-bin\' must have Buffer values');
  64. }
  65. } else {
  66. if (!_.isString(value)) {
  67. throw new Error(
  68. 'keys that don\'t end with \'-bin\' must have String values');
  69. }
  70. if (!grpc.metadataNonbinValueIsLegal(value)) {
  71. throw new Error('Metadata string value "' + value +
  72. '" contains illegal characters');
  73. }
  74. }
  75. }
  76. /**
  77. * Sets the given value for the given key, replacing any other values associated
  78. * with that key. Normalizes the key.
  79. * @param {String} key The key to set
  80. * @param {String|Buffer} value The value to set. Must be a buffer if and only
  81. * if the normalized key ends with '-bin'
  82. */
  83. Metadata.prototype.set = function(key, value) {
  84. key = normalizeKey(key);
  85. validate(key, value);
  86. this._internal_repr[key] = [value];
  87. };
  88. /**
  89. * Adds the given value for the given key. Normalizes the key.
  90. * @param {String} key The key to add to.
  91. * @param {String|Buffer} value The value to add. Must be a buffer if and only
  92. * if the normalized key ends with '-bin'
  93. */
  94. Metadata.prototype.add = function(key, value) {
  95. key = normalizeKey(key);
  96. validate(key, value);
  97. if (!this._internal_repr[key]) {
  98. this._internal_repr[key] = [];
  99. }
  100. this._internal_repr[key].push(value);
  101. };
  102. /**
  103. * Remove the given key and any associated values. Normalizes the key.
  104. * @param {String} key The key to remove
  105. */
  106. Metadata.prototype.remove = function(key) {
  107. key = normalizeKey(key);
  108. if (Object.prototype.hasOwnProperty.call(this._internal_repr, key)) {
  109. delete this._internal_repr[key];
  110. }
  111. };
  112. /**
  113. * Gets a list of all values associated with the key. Normalizes the key.
  114. * @param {String} key The key to get
  115. * @return {Array.&lt;String|Buffer>} The values associated with that key
  116. */
  117. Metadata.prototype.get = function(key) {
  118. key = normalizeKey(key);
  119. if (Object.prototype.hasOwnProperty.call(this._internal_repr, key)) {
  120. return this._internal_repr[key];
  121. } else {
  122. return [];
  123. }
  124. };
  125. /**
  126. * Get a map of each key to a single associated value. This reflects the most
  127. * common way that people will want to see metadata.
  128. * @return {Object.&lt;String,String|Buffer>} A key/value mapping of the metadata
  129. */
  130. Metadata.prototype.getMap = function() {
  131. var result = {};
  132. _.forOwn(this._internal_repr, function(values, key) {
  133. if(values.length > 0) {
  134. result[key] = values[0];
  135. }
  136. });
  137. return result;
  138. };
  139. /**
  140. * Clone the metadata object.
  141. * @return {Metadata} The new cloned object
  142. */
  143. Metadata.prototype.clone = function() {
  144. var copy = new Metadata();
  145. _.forOwn(this._internal_repr, function(value, key) {
  146. copy._internal_repr[key] = _.clone(value);
  147. });
  148. return copy;
  149. };
  150. /**
  151. * Gets the metadata in the format used by interal code. Intended for internal
  152. * use only. API stability is not guaranteed.
  153. * @private
  154. * @return {Object.&lt;String, Array.&lt;String|Buffer>>} The metadata
  155. */
  156. Metadata.prototype._getCoreRepresentation = function() {
  157. return this._internal_repr;
  158. };
  159. /**
  160. * Creates a Metadata object from a metadata map in the internal format.
  161. * Intended for internal use only. API stability is not guaranteed.
  162. * @private
  163. * @param {Object.&lt;String, Array.&lt;String|Buffer>>} The metadata
  164. * @return {Metadata} The new Metadata object
  165. */
  166. Metadata._fromCoreRepresentation = function(metadata) {
  167. var newMetadata = new Metadata();
  168. if (metadata) {
  169. _.forOwn(metadata, function(value, key) {
  170. newMetadata._internal_repr[key] = _.clone(value);
  171. });
  172. }
  173. return newMetadata;
  174. };
  175. module.exports = Metadata;
  176. </code></pre>
  177. </article>
  178. </section>
  179. </div>
  180. <nav>
  181. <h2><a href="index.html">Home</a></h2><h3>Externals</h3><ul><li><a href="external-Duplex.html">Duplex</a></li><li><a href="external-EventEmitter.html">EventEmitter</a></li><li><a href="external-GoogleCredential.html">GoogleCredential</a></li><li><a href="external-Readable.html">Readable</a></li><li><a href="external-Writable.html">Writable</a></li></ul><h3>Classes</h3><ul><li><a href="grpc.Client.html">Client</a></li><li><a href="grpc.credentials-CallCredentials.html">CallCredentials</a></li><li><a href="grpc.credentials-ChannelCredentials.html">ChannelCredentials</a></li><li><a href="grpc.Metadata.html">Metadata</a></li><li><a href="grpc.Server.html">Server</a></li><li><a href="grpc.ServerCredentials.html">ServerCredentials</a></li><li><a href="grpc-ClientDuplexStream.html">ClientDuplexStream</a></li><li><a href="grpc-ClientReadableStream.html">ClientReadableStream</a></li><li><a href="grpc-ClientUnaryCall.html">ClientUnaryCall</a></li><li><a href="grpc-ClientWritableStream.html">ClientWritableStream</a></li><li><a href="grpc-ServerDuplexStream.html">ServerDuplexStream</a></li><li><a href="grpc-ServerReadableStream.html">ServerReadableStream</a></li><li><a href="grpc-ServerUnaryCall.html">ServerUnaryCall</a></li><li><a href="grpc-ServerWritableStream.html">ServerWritableStream</a></li></ul><h3>Events</h3><ul><li><a href="grpc-ClientDuplexStream.html#event:metadata">metadata</a></li><li><a href="grpc-ClientDuplexStream.html#event:status">status</a></li><li><a href="grpc-ClientReadableStream.html#event:metadata">metadata</a></li><li><a href="grpc-ClientReadableStream.html#event:status">status</a></li><li><a href="grpc-ClientUnaryCall.html#event:metadata">metadata</a></li><li><a href="grpc-ClientUnaryCall.html#event:status">status</a></li><li><a href="grpc-ClientWritableStream.html#event:metadata">metadata</a></li><li><a href="grpc-ClientWritableStream.html#event:status">status</a></li><li><a href="grpc-ServerDuplexStream.html#~event:cancelled">cancelled</a></li><li><a href="grpc-ServerReadableStream.html#~event:cancelled">cancelled</a></li><li><a href="grpc-ServerUnaryCall.html#~event:cancelled">cancelled</a></li><li><a href="grpc-ServerWritableStream.html#~event:cancelled">cancelled</a></li></ul><h3>Namespaces</h3><ul><li><a href="grpc.html">grpc</a></li><li><a href="grpc.credentials.html">credentials</a></li></ul>
  182. </nav>
  183. <br class="clear">
  184. <footer>
  185. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.3</a> on Fri Sep 08 2017 11:10:31 GMT-0700 (PDT)
  186. </footer>
  187. <script> prettyPrint(); </script>
  188. <script src="scripts/linenumber.js"> </script>
  189. </body>
  190. </html>