« index

Coverage for /Users/yunong/workspace/node-restify/lib/index.js : 96%

183 lines | 177 run | 6 missing | 3 partial | 23 blocks | 20 blocks run | 3 blocks missing

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

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

  // Copyright 2012 Mark Cavage, Inc.  All rights reserved.
  //
  // Restify supports both a client and server API, and in the essence of not
  // loading the kitchen sink on clients, the exports here is chunked up into
  // client and server; note clients will have to opt in by setting the env
  // var "RESTIFY_CLIENT_ONLY", but if you're in that boat, it's not hard to do,
  // and enables much faster load times
  //
  
  var shallowCopy = require('./utils').shallowCopy;
  
  
  function createClient(options) {
      if (typeof (options) === 'string') {
          options = {
              url: options
          };
      }
  
      var assert = require('assert-plus');
      var bunyan = require('./bunyan_helper');
      var clients = require('./clients');
  
      assert.object(options, 'options');
  
      var client;
      var opts = shallowCopy(options);
      opts.agent = options.agent;
      opts.name = opts.name || 'restify';
      opts.type = opts.type || 'application/octet-stream';
      opts.log = opts.log || bunyan.createLogger(opts.name);
  
      switch (opts.type) {
          case 'json':
              client = new clients.JsonClient(opts);
              break;
  
          case 'string':
              client = new clients.StringClient(opts);
              break;
  
          case 'http':
          default:
              client = new clients.HttpClient(opts);
              break;
      }
  
      return (client);
  }
  
  
  function createJsonClient(options) {
      if (typeof (options) === 'string') {
          options = {
              url: options
          };
      }
  
      options = options ? shallowCopy(options) : {};
      options.type = 'json';
      return (createClient(options));
  }
  
  
  function createStringClient(options) {
      if (typeof (options) === 'string') {
          options = {
              url: options
          };
      }
  
      options = options ? shallowCopy(options) : {};
      options.type = 'string';
      return (createClient(options));
  }
  
  
  function createHttpClient(options) {
      if (typeof (options) === 'string') {
          options = {
              url: options
          };
      }
  
      options = options ? shallowCopy(options) : {};
      options.type = 'http';
      return (createClient(options));
  }
  
  
  function createServer(options) {
      var bunyan = require('./bunyan_helper');
      var InternalError = require('./errors').InternalError;
      var Router = require('./router');
      var Server = require('./server');
  
      var opts = shallowCopy(options || {});
      var server;
  
      opts.name = opts.name || 'restify';
      opts.log = opts.log || bunyan.createLogger(opts.name);
      opts.router = opts.router || new Router(opts);
  
      server = new Server(opts);
      server.on('uncaughtException', function (req, res, route, e) {
          if (this.listeners('uncaughtException').length > 1 ||
              res.headersSent) {
              return (false);
          }
  
          res.send(new InternalError(e, e.message || 'unexpected error'));
          return (true);
      });
  
      return (server);
  }
  
  
  /**
   * Returns a string representation of a URL pattern , with its
   * parameters filled in by the passed hash.
   *
   * If a key is not found in the hash for a param, it is left alone.
   *
   * @param {Object} a hash of parameter names to values for substitution.
   */
  function realizeUrl(pattern, params) {
      var p = pattern.replace(/\/:([^/]+)/g, function (match, k) {
          return (params.hasOwnProperty(k) ? '/' + params[k] : match);
      });
  
  
      return (require('./utils').sanitizePath(p));
  }
  
  
  ///--- Exports
  
  module.exports = {
      // Client API
      createClient: createClient,
      createJsonClient: createJsonClient,
      createJSONClient: createJsonClient,
      createStringClient: createStringClient,
      createHttpClient: createHttpClient,
      get HttpClient() {
          return (require('./clients').HttpClient);
      },
      get JsonClient() {
          return (require('./clients').JsonClient);
      },
      get StringClient() {
          return (require('./clients').StringClient);
      },
  
      // Miscellaneous API
      get bunyan() {
          return (require('./bunyan_helper'));
      },
  
      errors: {}
  
  };
  
  var errors = require('./errors');
  Object.keys(errors).forEach(function (k) {
      module.exports.errors[k] = errors[k];
      module.exports[k] = errors[k];
  });
  
  if (!process.env.RESTIFY_CLIENT_ONLY) {
  
      module.exports.createServer = createServer;
      module.exports.httpDate = require('./http_date');
      module.exports.realizeUrl = realizeUrl;
      module.exports.formatters = require('./formatters');
      module.exports.plugins = {};
      var plugins = require('./plugins');
      Object.keys(plugins).forEach(function (k) {
          module.exports.plugins[k] = plugins[k];
          module.exports[k] = plugins[k];
      });
  }
« index | cover.io