vulcan 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (260) hide show
  1. data/lib/vulcan/cli.rb +20 -10
  2. data/lib/vulcan/version.rb +1 -1
  3. data/server/Procfile +1 -1
  4. data/server/bin/web +10 -0
  5. data/server/lib/cloudant.coffee +20 -0
  6. data/server/lib/logger.coffee +17 -0
  7. data/server/lib/on.coffee +10 -0
  8. data/server/lib/spawner.coffee +65 -0
  9. data/server/node_modules/coffee-script/CNAME +1 -0
  10. data/server/node_modules/coffee-script/LICENSE +22 -0
  11. data/server/node_modules/coffee-script/README +51 -0
  12. data/server/node_modules/coffee-script/Rakefile +78 -0
  13. data/server/node_modules/coffee-script/bin/cake +7 -0
  14. data/server/node_modules/coffee-script/bin/coffee +7 -0
  15. data/server/node_modules/coffee-script/extras/jsl.conf +44 -0
  16. data/server/node_modules/coffee-script/lib/coffee-script/browser.js +92 -0
  17. data/server/node_modules/coffee-script/lib/coffee-script/cake.js +111 -0
  18. data/server/node_modules/coffee-script/lib/coffee-script/coffee-script.js +167 -0
  19. data/server/node_modules/coffee-script/lib/coffee-script/command.js +500 -0
  20. data/server/node_modules/coffee-script/lib/coffee-script/grammar.js +606 -0
  21. data/server/node_modules/coffee-script/lib/coffee-script/helpers.js +77 -0
  22. data/server/node_modules/coffee-script/lib/coffee-script/index.js +11 -0
  23. data/server/node_modules/coffee-script/lib/coffee-script/lexer.js +788 -0
  24. data/server/node_modules/coffee-script/lib/coffee-script/nodes.js +2986 -0
  25. data/server/node_modules/coffee-script/lib/coffee-script/optparse.js +138 -0
  26. data/server/node_modules/coffee-script/lib/coffee-script/parser.js +683 -0
  27. data/server/node_modules/coffee-script/lib/coffee-script/repl.js +261 -0
  28. data/server/node_modules/coffee-script/lib/coffee-script/rewriter.js +349 -0
  29. data/server/node_modules/coffee-script/lib/coffee-script/scope.js +146 -0
  30. data/server/node_modules/coffee-script/package.json +55 -0
  31. data/server/node_modules/connect-form/History.md +0 -6
  32. data/server/node_modules/connect-form/lib/connect-form.js +2 -4
  33. data/server/node_modules/connect-form/node_modules/formidable/Readme.md +64 -36
  34. data/server/node_modules/connect-form/node_modules/formidable/lib/incoming_form.js +5 -1
  35. data/server/node_modules/connect-form/node_modules/formidable/package.json +20 -6
  36. data/server/node_modules/connect-form/node_modules/formidable/test/common.js +5 -6
  37. data/server/node_modules/connect-form/node_modules/formidable/test/fixture/file/funkyfilename.txt +1 -0
  38. data/server/node_modules/connect-form/node_modules/formidable/test/fixture/js/special-chars-in-filename.js +1 -1
  39. data/server/node_modules/connect-form/node_modules/formidable/test/{slow → integration}/test-fixtures.js +38 -33
  40. data/server/node_modules/connect-form/node_modules/formidable/test/legacy/simple/test-incoming-form.js +11 -0
  41. data/server/node_modules/connect-form/node_modules/formidable/test/run.js +1 -6
  42. data/server/node_modules/connect-form/node_modules/formidable/test/unit/test-incoming-form.js +63 -0
  43. data/server/node_modules/connect-form/package.json +27 -5
  44. data/server/node_modules/cradle/README.md +10 -10
  45. data/server/node_modules/cradle/lib/cradle.js +117 -523
  46. data/server/node_modules/cradle/lib/cradle/database/attachments.js +120 -0
  47. data/server/node_modules/cradle/lib/cradle/database/changes.js +56 -0
  48. data/server/node_modules/cradle/lib/cradle/database/documents.js +215 -0
  49. data/server/node_modules/cradle/lib/cradle/database/index.js +65 -0
  50. data/server/node_modules/cradle/lib/cradle/database/views.js +125 -0
  51. data/server/node_modules/cradle/node_modules/follow/LICENSE +202 -0
  52. data/server/node_modules/cradle/node_modules/follow/README.md +164 -0
  53. data/server/node_modules/cradle/node_modules/follow/Rakefile +54 -0
  54. data/server/node_modules/cradle/node_modules/follow/api.js +35 -0
  55. data/server/node_modules/cradle/node_modules/follow/browser/eventemitter2.js +453 -0
  56. data/server/node_modules/cradle/node_modules/follow/browser/export.js +78 -0
  57. data/server/node_modules/cradle/node_modules/follow/browser/index.html +14 -0
  58. data/server/node_modules/cradle/node_modules/follow/browser/jquery-1.6.1.min.js +18 -0
  59. data/server/node_modules/cradle/node_modules/follow/browser/log4js.js +46 -0
  60. data/server/node_modules/cradle/node_modules/follow/browser/main.js +92 -0
  61. data/server/node_modules/cradle/node_modules/follow/browser/querystring.js +28 -0
  62. data/server/node_modules/cradle/node_modules/follow/browser/request.jquery.js +237 -0
  63. data/server/node_modules/cradle/node_modules/follow/browser/require.js +33 -0
  64. data/server/node_modules/cradle/node_modules/follow/browser/util.js +28 -0
  65. data/server/node_modules/cradle/node_modules/follow/cli.js +101 -0
  66. data/server/node_modules/cradle/node_modules/follow/lib/feed.js +556 -0
  67. data/server/node_modules/cradle/node_modules/follow/lib/index.js +66 -0
  68. data/server/node_modules/cradle/node_modules/follow/lib/stream.js +305 -0
  69. data/server/node_modules/cradle/node_modules/follow/node_modules/request/LICENSE +55 -0
  70. data/server/node_modules/cradle/node_modules/follow/node_modules/request/README.md +285 -0
  71. data/server/node_modules/cradle/node_modules/follow/node_modules/request/main.js +618 -0
  72. data/server/node_modules/cradle/node_modules/follow/node_modules/request/mimetypes.js +146 -0
  73. data/server/node_modules/cradle/node_modules/follow/node_modules/request/oauth.js +34 -0
  74. data/server/node_modules/cradle/node_modules/follow/node_modules/request/package.json +42 -0
  75. data/server/node_modules/cradle/node_modules/follow/node_modules/request/tests/googledoodle.png +0 -0
  76. data/server/node_modules/cradle/node_modules/follow/node_modules/request/tests/run.sh +6 -0
  77. data/server/node_modules/cradle/node_modules/follow/node_modules/request/tests/server.js +57 -0
  78. data/server/node_modules/cradle/node_modules/follow/node_modules/request/tests/test-body.js +90 -0
  79. data/server/node_modules/cradle/node_modules/follow/node_modules/request/tests/test-cookie.js +29 -0
  80. data/server/node_modules/cradle/node_modules/follow/node_modules/request/tests/test-cookiejar.js +90 -0
  81. data/server/node_modules/cradle/node_modules/follow/node_modules/request/tests/test-errors.js +30 -0
  82. data/server/node_modules/cradle/node_modules/follow/node_modules/request/tests/test-oauth.js +109 -0
  83. data/server/node_modules/cradle/node_modules/follow/node_modules/request/tests/test-pipes.js +167 -0
  84. data/server/node_modules/cradle/node_modules/follow/node_modules/request/tests/test-proxy.js +39 -0
  85. data/server/node_modules/cradle/node_modules/follow/node_modules/request/tests/test-timeout.js +87 -0
  86. data/server/node_modules/cradle/node_modules/follow/node_modules/request/uuid.js +19 -0
  87. data/server/node_modules/cradle/node_modules/follow/node_modules/request/vendor/cookie/index.js +55 -0
  88. data/server/node_modules/cradle/node_modules/follow/node_modules/request/vendor/cookie/jar.js +72 -0
  89. data/server/node_modules/cradle/node_modules/follow/package.json +45 -0
  90. data/server/node_modules/cradle/node_modules/follow/test/couch.js +153 -0
  91. data/server/node_modules/cradle/node_modules/follow/test/follow.js +136 -0
  92. data/server/node_modules/cradle/node_modules/follow/test/issues.js +178 -0
  93. data/server/node_modules/cradle/node_modules/follow/test/issues/10.js +24 -0
  94. data/server/node_modules/cradle/node_modules/follow/test/stream.js +493 -0
  95. data/server/node_modules/cradle/node_modules/request/LICENSE +55 -0
  96. data/server/node_modules/cradle/node_modules/request/README.md +287 -0
  97. data/server/node_modules/cradle/node_modules/request/forever.js +103 -0
  98. data/server/node_modules/cradle/node_modules/request/main.js +913 -0
  99. data/server/node_modules/cradle/node_modules/request/mimetypes.js +152 -0
  100. data/server/node_modules/cradle/node_modules/request/oauth.js +34 -0
  101. data/server/node_modules/cradle/node_modules/request/package.json +42 -0
  102. data/server/node_modules/cradle/node_modules/request/tests/googledoodle.png +0 -0
  103. data/server/node_modules/cradle/node_modules/request/tests/run.js +38 -0
  104. data/server/node_modules/cradle/node_modules/request/tests/server.js +82 -0
  105. data/server/node_modules/cradle/node_modules/request/tests/squid.conf +77 -0
  106. data/server/node_modules/cradle/node_modules/request/tests/ssl/ca/ca.cnf +20 -0
  107. data/server/node_modules/cradle/node_modules/request/tests/ssl/ca/ca.crl +0 -0
  108. data/server/node_modules/cradle/node_modules/request/tests/ssl/ca/ca.crt +17 -0
  109. data/server/node_modules/cradle/node_modules/request/tests/ssl/ca/ca.csr +13 -0
  110. data/server/node_modules/cradle/node_modules/request/tests/ssl/ca/ca.key +18 -0
  111. data/server/node_modules/cradle/node_modules/request/tests/ssl/ca/ca.srl +1 -0
  112. data/server/node_modules/cradle/node_modules/request/tests/ssl/ca/server.cnf +19 -0
  113. data/server/node_modules/cradle/node_modules/request/tests/ssl/ca/server.crt +16 -0
  114. data/server/node_modules/cradle/node_modules/request/tests/ssl/ca/server.csr +11 -0
  115. data/server/node_modules/cradle/node_modules/request/tests/ssl/ca/server.js +28 -0
  116. data/server/node_modules/cradle/node_modules/request/tests/ssl/ca/server.key +9 -0
  117. data/server/node_modules/cradle/node_modules/request/tests/ssl/npm-ca.crt +16 -0
  118. data/server/node_modules/cradle/node_modules/request/tests/ssl/test.crt +15 -0
  119. data/server/node_modules/cradle/node_modules/request/tests/ssl/test.key +15 -0
  120. data/server/node_modules/cradle/node_modules/request/tests/test-body.js +95 -0
  121. data/server/node_modules/cradle/node_modules/request/tests/test-cookie.js +29 -0
  122. data/server/node_modules/cradle/node_modules/request/tests/test-cookiejar.js +90 -0
  123. data/server/node_modules/cradle/node_modules/request/tests/test-defaults.js +68 -0
  124. data/server/node_modules/cradle/node_modules/request/tests/test-errors.js +37 -0
  125. data/server/node_modules/cradle/node_modules/request/tests/test-headers.js +52 -0
  126. data/server/node_modules/cradle/node_modules/request/tests/test-httpModule.js +94 -0
  127. data/server/node_modules/cradle/node_modules/request/tests/test-https-strict.js +97 -0
  128. data/server/node_modules/cradle/node_modules/request/tests/test-https.js +86 -0
  129. data/server/node_modules/cradle/node_modules/request/tests/test-oauth.js +117 -0
  130. data/server/node_modules/cradle/node_modules/request/tests/test-params.js +92 -0
  131. data/server/node_modules/cradle/node_modules/request/tests/test-pipes.js +202 -0
  132. data/server/node_modules/cradle/node_modules/request/tests/test-proxy.js +39 -0
  133. data/server/node_modules/cradle/node_modules/request/tests/test-qs.js +28 -0
  134. data/server/node_modules/cradle/node_modules/request/tests/test-redirect.js +154 -0
  135. data/server/node_modules/cradle/node_modules/request/tests/test-timeout.js +87 -0
  136. data/server/node_modules/cradle/node_modules/request/tests/test-toJSON.js +14 -0
  137. data/server/node_modules/cradle/node_modules/request/tests/test-tunnel.js +61 -0
  138. data/server/node_modules/cradle/node_modules/request/tunnel.js +229 -0
  139. data/server/node_modules/cradle/node_modules/request/uuid.js +19 -0
  140. data/server/node_modules/cradle/node_modules/request/vendor/cookie/index.js +65 -0
  141. data/server/node_modules/cradle/node_modules/request/vendor/cookie/jar.js +72 -0
  142. data/server/node_modules/cradle/node_modules/vargs/package.json +33 -10
  143. data/server/node_modules/cradle/package.json +50 -12
  144. data/server/node_modules/cradle/test/cache-test.js +1 -4
  145. data/server/node_modules/cradle/test/connection-test.js +179 -0
  146. data/server/node_modules/cradle/test/database-attachment-test.js +344 -0
  147. data/server/node_modules/cradle/test/database-cache-test.js +132 -0
  148. data/server/node_modules/cradle/test/database-test.js +219 -0
  149. data/server/node_modules/cradle/test/database-view-test.js +141 -0
  150. data/server/node_modules/cradle/test/fixtures/databases.json +28 -1
  151. data/server/node_modules/cradle/test/helpers/seed.js +14 -5
  152. data/server/node_modules/cradle/test/response-test.js +1 -1
  153. data/server/node_modules/express/History.md +16 -0
  154. data/server/node_modules/express/bin/express +7 -6
  155. data/server/node_modules/express/lib-cov/application.js +510 -0
  156. data/server/node_modules/express/lib-cov/express.js +65 -0
  157. data/server/node_modules/express/lib-cov/middleware.js +54 -0
  158. data/server/node_modules/express/lib-cov/request.js +225 -0
  159. data/server/node_modules/express/lib-cov/response.js +611 -0
  160. data/server/node_modules/express/lib-cov/router/collection.js +40 -0
  161. data/server/node_modules/express/lib-cov/router/index.js +515 -0
  162. data/server/node_modules/express/lib-cov/router/methods.js +9 -0
  163. data/server/node_modules/express/lib-cov/router/route.js +68 -0
  164. data/server/node_modules/express/lib-cov/utils.js +151 -0
  165. data/server/node_modules/express/lib-cov/view.js +81 -0
  166. data/server/node_modules/express/lib/express.js +1 -1
  167. data/server/node_modules/express/lib/http.js +1 -2
  168. data/server/node_modules/express/lib/request.js +2 -2
  169. data/server/node_modules/express/lib/router/methods.js +10 -1
  170. data/server/node_modules/express/node_modules/connect/lib/connect.js +1 -1
  171. data/server/node_modules/express/node_modules/connect/lib/http.js +3 -2
  172. data/server/node_modules/express/node_modules/connect/lib/middleware/limit.js +0 -2
  173. data/server/node_modules/express/node_modules/connect/lib/middleware/session.js +1 -2
  174. data/server/node_modules/express/node_modules/connect/node_modules/formidable/Readme.md +42 -25
  175. data/server/node_modules/express/node_modules/connect/node_modules/formidable/lib/incoming_form.js +1 -0
  176. data/server/node_modules/express/node_modules/connect/node_modules/formidable/package.json +14 -3
  177. data/server/node_modules/express/node_modules/connect/node_modules/formidable/test/legacy/simple/test-incoming-form.js +11 -0
  178. data/server/node_modules/express/node_modules/connect/package.json +35 -7
  179. data/server/node_modules/express/node_modules/mime/package.json +30 -8
  180. data/server/node_modules/express/node_modules/mkdirp/README.markdown +35 -2
  181. data/server/node_modules/express/node_modules/mkdirp/examples/pow.js +1 -1
  182. data/server/node_modules/express/node_modules/mkdirp/index.js +72 -13
  183. data/server/node_modules/express/node_modules/mkdirp/package.json +39 -21
  184. data/server/node_modules/express/node_modules/mkdirp/test/chmod.js +38 -0
  185. data/server/node_modules/express/node_modules/mkdirp/test/clobber.js +37 -0
  186. data/server/node_modules/express/node_modules/mkdirp/test/perm.js +32 -0
  187. data/server/node_modules/express/node_modules/mkdirp/test/perm_sync.js +39 -0
  188. data/server/node_modules/express/node_modules/mkdirp/test/sync.js +27 -0
  189. data/server/node_modules/express/node_modules/mkdirp/test/umask.js +28 -0
  190. data/server/node_modules/express/node_modules/mkdirp/test/umask_sync.js +27 -0
  191. data/server/node_modules/express/node_modules/qs/History.md +10 -0
  192. data/server/node_modules/express/node_modules/qs/Readme.md +9 -2
  193. data/server/node_modules/express/node_modules/qs/examples.js +3 -0
  194. data/server/node_modules/express/node_modules/qs/lib/querystring.js +8 -6
  195. data/server/node_modules/express/node_modules/qs/package.json +26 -8
  196. data/server/node_modules/express/node_modules/qs/test/parse.js +13 -1
  197. data/server/node_modules/express/node_modules/qs/test/stringify.js +45 -37
  198. data/server/node_modules/express/package.json +55 -16
  199. data/server/node_modules/express/test.js +41 -0
  200. data/server/node_modules/knox/package.json +26 -4
  201. data/server/node_modules/node-uuid/package.json +40 -11
  202. data/server/node_modules/node-uuid/test/test.js +1 -1
  203. data/server/node_modules/nodemon/README.md +120 -0
  204. data/server/node_modules/nodemon/nodemon.js +518 -0
  205. data/server/node_modules/nodemon/nodemonignore.example +11 -0
  206. data/server/node_modules/nodemon/package.json +49 -0
  207. data/server/node_modules/restler/README.md +144 -94
  208. data/server/node_modules/restler/lib/multipartform.js +2 -0
  209. data/server/node_modules/restler/lib/restler.js +218 -61
  210. data/server/node_modules/restler/package.json +35 -8
  211. data/server/node_modules/restler/test/all.js +6 -1
  212. data/server/node_modules/restler/test/restler.js +624 -118
  213. data/server/package.json +14 -10
  214. data/server/web.coffee +64 -0
  215. data/server/web.js +15 -3
  216. metadata +170 -57
  217. data/server/index.js +0 -14
  218. data/server/node_modules/connect-form/LICENSE +0 -22
  219. data/server/node_modules/connect-form/index.js +0 -100
  220. data/server/node_modules/connect-form/node_modules/formidable/test/fast/test-incoming-form.js +0 -45
  221. data/server/node_modules/connect-form/node_modules/formidable/test/fixture/http/no-filename/generic.http +0 -13
  222. data/server/node_modules/connect-form/node_modules/formidable/test/fixture/http/special-chars-in-filename/osx-chrome-13.http +0 -26
  223. data/server/node_modules/connect-form/node_modules/formidable/test/fixture/http/special-chars-in-filename/osx-firefox-3.6.http +0 -24
  224. data/server/node_modules/connect-form/node_modules/formidable/test/fixture/http/special-chars-in-filename/osx-safari-5.http +0 -23
  225. data/server/node_modules/connect-form/node_modules/formidable/test/fixture/http/special-chars-in-filename/xp-chrome-12.http +0 -24
  226. data/server/node_modules/connect-form/node_modules/formidable/test/fixture/http/special-chars-in-filename/xp-ie-7.http +0 -22
  227. data/server/node_modules/connect-form/node_modules/formidable/test/fixture/http/special-chars-in-filename/xp-ie-8.http +0 -22
  228. data/server/node_modules/connect-form/node_modules/formidable/test/fixture/http/special-chars-in-filename/xp-safari-5.http +0 -22
  229. data/server/node_modules/connect-form/node_modules/formidable/test/fixture/multi_video.upload +0 -0
  230. data/server/node_modules/cradle/Makefile +0 -10
  231. data/server/node_modules/cradle/test/cradle-test.js +0 -650
  232. data/server/node_modules/express/node_modules/connect/node_modules/formidable/test/fixture/http/no-filename/generic.http +0 -13
  233. data/server/node_modules/express/node_modules/connect/node_modules/formidable/test/fixture/http/special-chars-in-filename/osx-chrome-13.http +0 -26
  234. data/server/node_modules/express/node_modules/connect/node_modules/formidable/test/fixture/http/special-chars-in-filename/osx-firefox-3.6.http +0 -24
  235. data/server/node_modules/express/node_modules/connect/node_modules/formidable/test/fixture/http/special-chars-in-filename/osx-safari-5.http +0 -23
  236. data/server/node_modules/express/node_modules/connect/node_modules/formidable/test/fixture/http/special-chars-in-filename/xp-chrome-12.http +0 -24
  237. data/server/node_modules/express/node_modules/connect/node_modules/formidable/test/fixture/http/special-chars-in-filename/xp-ie-7.http +0 -22
  238. data/server/node_modules/express/node_modules/connect/node_modules/formidable/test/fixture/http/special-chars-in-filename/xp-ie-8.http +0 -22
  239. data/server/node_modules/express/node_modules/connect/node_modules/formidable/test/fixture/http/special-chars-in-filename/xp-safari-5.http +0 -22
  240. data/server/node_modules/express/node_modules/connect/node_modules/formidable/test/fixture/multi_video.upload +0 -0
  241. data/server/node_modules/express/node_modules/connect/test.js +0 -52
  242. data/server/node_modules/express/testing/foo/app.js +0 -35
  243. data/server/node_modules/express/testing/foo/package.json +0 -9
  244. data/server/node_modules/express/testing/foo/public/stylesheets/style.css +0 -8
  245. data/server/node_modules/express/testing/foo/routes/index.js +0 -10
  246. data/server/node_modules/express/testing/foo/views/index.jade +0 -2
  247. data/server/node_modules/express/testing/foo/views/layout.jade +0 -6
  248. data/server/node_modules/express/testing/index.js +0 -43
  249. data/server/node_modules/express/testing/public/test.txt +0 -2971
  250. data/server/node_modules/express/testing/views/page.html +0 -1
  251. data/server/node_modules/express/testing/views/page.jade +0 -3
  252. data/server/node_modules/express/testing/views/test.md +0 -1
  253. data/server/node_modules/express/testing/views/user/index.jade +0 -1
  254. data/server/node_modules/express/testing/views/user/list.jade +0 -1
  255. data/server/node_modules/knox/lib/knox/mime/index.js +0 -308
  256. data/server/node_modules/knox/lib/knox/mime/test.js +0 -59
  257. data/server/node_modules/node-uuid/test/benchmark-native +0 -0
  258. data/server/node_modules/on/index.js +0 -13
  259. data/server/node_modules/restler/test/test_helper.js +0 -163
  260. data/server/node_modules/spawner/index.js +0 -106
@@ -0,0 +1,120 @@
1
+ var querystring = require('querystring'),
2
+ Args = require('vargs').Constructor,
3
+ cradle = require('../../cradle'),
4
+ Database = require('./index').Database;
5
+
6
+ Database.prototype.getAttachment = function (id, attachmentName, callback) {
7
+ //
8
+ // TODO: Update cache?
9
+ //
10
+
11
+ return this.connection.rawRequest({
12
+ method: 'GET',
13
+ path: '/' + [this.name, querystring.escape(id), attachmentName].join('/')
14
+ }, callback);
15
+ };
16
+
17
+ Database.prototype.removeAttachment = function (doc, attachmentName, callback) {
18
+ var params,
19
+ rev,
20
+ id;
21
+
22
+ if (typeof doc === 'string') {
23
+ id = doc;
24
+ } else {
25
+ id = doc.id || doc._id;
26
+ rev = doc.rev || doc._rev;
27
+ }
28
+
29
+ if (!id) {
30
+ error = new(TypeError)("first argument must be a document id");
31
+ if (!callback) { throw error }
32
+ return callback(error);
33
+ }
34
+
35
+ if (!rev && this.cache.has(id)) {
36
+ rev = this.cache.get(id)._rev;
37
+ } else if (rev) {
38
+ rev = rev.replace(/\"/g, '');
39
+ }
40
+
41
+ this.query({
42
+ method: 'DELETE',
43
+ path: [querystring.escape(id), attachmentName].join('/'),
44
+ query: { rev: rev }
45
+ }, callback);
46
+ };
47
+
48
+ Database.prototype.saveAttachment = function (doc, attachment, callback) {
49
+ var attachmentName,
50
+ options = {},
51
+ self = this,
52
+ params,
53
+ error,
54
+ rev,
55
+ id;
56
+
57
+ if (typeof doc === 'string') {
58
+ id = doc;
59
+ } else {
60
+ id = doc.id || doc._id;
61
+ rev = doc.rev || doc._rev;
62
+ }
63
+
64
+ if (!id) {
65
+ error = new(TypeError)("Missing document id.");
66
+ if (!callback) { throw error }
67
+ return callback(error);
68
+ }
69
+
70
+ attachmentName = typeof attachment !== 'string'
71
+ ? attachment.name
72
+ : attachment;
73
+
74
+ if (!rev && this.cache.has(id)) {
75
+ params = { rev: this.cache.get(id)._rev };
76
+ } else if (rev) {
77
+ params = { rev: rev.replace(/\"/g, '') };
78
+ }
79
+
80
+ options.method = 'PUT';
81
+ options.path = '/' + [this.name, querystring.escape(id), attachmentName].join('/');
82
+ options.headers = {
83
+ 'Content-Type': attachment['content-type']
84
+ || attachment['contentType']
85
+ || attachment['Content-Type']
86
+ || 'text/plain'
87
+ };
88
+
89
+ if (attachment.body) {
90
+ options.body = attachment.body;
91
+ }
92
+
93
+ if (params) {
94
+ options.path += ('?' + querystring.stringify(params));
95
+ }
96
+
97
+ return this.connection.rawRequest(options, function (err, res, body) {
98
+ var result = JSON.parse(body);
99
+ result.headers = res.headers;
100
+ result.headers.status = res.statusCode;
101
+
102
+ if (result.headers.status == 201) {
103
+ if (self.cache.has(id)) {
104
+ cached = self.cache.store[id].document;
105
+ cached._rev = result.rev;
106
+ cached._attachments = cached._attachments || {};
107
+ cached._attachments[attachmentName] = { stub: true };
108
+ }
109
+
110
+ return callback(null, result);
111
+ }
112
+
113
+ callback(result);
114
+ });
115
+ };
116
+
117
+ //
118
+ // Alias `saveAttachment` to `addAttachment`
119
+ //
120
+ Database.prototype.addAttachment = Database.prototype.saveAttachment;
@@ -0,0 +1,56 @@
1
+ var events = require('events'),
2
+ querystring = require('querystring'),
3
+ Args = require('vargs').Constructor,
4
+ follow = require('follow'),
5
+ cradle = require('../../cradle'),
6
+ Database = require('./index').Database;
7
+
8
+ Database.prototype.changes = function (options, callback) {
9
+ if (typeof(options) === 'function') {
10
+ callback = options;
11
+ options = {};
12
+ }
13
+
14
+ options = options || {};
15
+
16
+ if (callback) {
17
+ return this.query({
18
+ method: 'GET',
19
+ path: '_changes',
20
+ query: options
21
+ }, callback);
22
+ }
23
+
24
+ var response = new events.EventEmitter(),
25
+ responded = false,
26
+ auth = '',
27
+ feed;
28
+
29
+ if (!options.db) {
30
+ if (this.connection.auth && this.connection.auth.username
31
+ && this.connection.auth.password) {
32
+ auth = this.connection.auth.username + ':' + this.connection.auth.password + '@';
33
+ }
34
+
35
+ options.db = 'http://' + auth + this.connection.host + ':' + this.connection.port + '/' + this.name;
36
+ }
37
+
38
+ feed = new follow.Feed(options);
39
+ feed.on('change', function () {
40
+ //
41
+ // Remark: Support the legacy `data` events.
42
+ //
43
+ if (!responded) {
44
+ responded = true;
45
+ feed.emit('response', response);
46
+ }
47
+
48
+ response.emit.apply(response, ['data'].concat(Array.prototype.slice.call(arguments)));
49
+ });
50
+
51
+ if (options.follow !== false) {
52
+ feed.follow();
53
+ }
54
+
55
+ return feed;
56
+ };
@@ -0,0 +1,215 @@
1
+ var querystring = require('querystring'),
2
+ Args = require('vargs').Constructor,
3
+ cradle = require('../../cradle'),
4
+ Database = require('./index').Database;
5
+
6
+ //
7
+ // Perform a HEAD request
8
+ //
9
+ Database.prototype.head = function (id, callback) {
10
+ this.query({
11
+ method: 'HEAD',
12
+ path: cradle.escape(id)
13
+ }, callback);
14
+ };
15
+
16
+ // Fetch either a single document from the database, or cache,
17
+ // or multiple documents from the database.
18
+ // If it's a single doc from the db, attempt to save it to the cache.
19
+ Database.prototype.get = function (id, rev) {
20
+ var args = new (Args)(arguments),
21
+ options = null,
22
+ that = this;
23
+
24
+ if (Array.isArray(id)) { // Bulk GET
25
+ this.query({
26
+ method: 'POST',
27
+ path: '/_all_docs',
28
+ query: { include_docs: true },
29
+ body: { keys: id },
30
+ }, function (err, res) {
31
+ args.callback(err, res)
32
+ });
33
+ } else {
34
+ if (rev && args.length === 2) {
35
+ if (typeof(rev) === 'string') { options = { rev: rev } }
36
+ else if (typeof(rev) === 'object') { options = rev }
37
+ } else if (this.cache.has(id)) {
38
+ return args.callback(null, this.cache.get(id));
39
+ }
40
+ this.query({
41
+ path: cradle.escape(id),
42
+ query: options
43
+ }, function (err, res) {
44
+ if (! err) that.cache.save(res.id, res.json);
45
+ args.callback(err, res);
46
+ });
47
+ }
48
+ };
49
+
50
+ //
51
+ // PUT a document, and write through cache
52
+ //
53
+ Database.prototype.put = function (id, doc, callback) {
54
+ var cache = this.cache;
55
+ if (typeof(id) !== 'string') { throw new(TypeError)("id must be a string") }
56
+ this.query({
57
+ method: 'PUT',
58
+ path: cradle.escape(id),
59
+ body: doc
60
+ }, function (e, res) {
61
+ if (! e) { cache.save(id, cradle.merge({}, doc, { _id: id, _rev: res.rev })) }
62
+ callback && callback(e, res);
63
+ });
64
+ };
65
+
66
+ //
67
+ // POST a document, and write through cache
68
+ //
69
+ Database.prototype.post = function (doc, callback) {
70
+ var cache = this.cache;
71
+ this.query({
72
+ method: 'POST',
73
+ path: '/',
74
+ body: doc
75
+ }, function (e, res) {
76
+ if (! e) { cache.save(res.id, cradle.merge({}, doc, { _id: res.id, _rev: res.rev })) }
77
+ callback && callback(e, res);
78
+ });
79
+ };
80
+
81
+ Database.prototype.save = function (/* [id], [rev], doc | [doc, ...] */) {
82
+ var args = new(Args)(arguments),
83
+ array = args.all.slice(0), doc, id, rev;
84
+
85
+ if (Array.isArray(args.first)) {
86
+ doc = args.first;
87
+ } else {
88
+ doc = array.pop(),
89
+ id = array.shift(),
90
+ rev = array.shift();
91
+ }
92
+ this._save(id, rev, doc, args.callback);
93
+ };
94
+
95
+ Database.prototype._save = function (id, rev, doc, callback) {
96
+ var options = this.connection.options;
97
+ var document = {}, that = this;
98
+
99
+ // Bulk Insert
100
+ if (Array.isArray(doc)) {
101
+ document.docs = doc;
102
+ if (options.allOrNothing) { document.all_or_nothing = true }
103
+ this.query({
104
+ method: 'POST',
105
+ path: '/_bulk_docs',
106
+ body: document
107
+ }, callback);
108
+ } else {
109
+ if (!id && doc._id) {
110
+ id = doc._id;
111
+ }
112
+
113
+ // PUT a single document, with an id (Create or Update)
114
+ if (id) {
115
+ // Design document
116
+ if (/^_design\/(\w|%|\-)+$/.test(id) && !('views' in doc)) {
117
+ document.language = "javascript";
118
+ document.views = doc;
119
+ } else {
120
+ document = doc;
121
+ }
122
+ // Try to set the '_rev' attribute of the document.
123
+ // If it wasn't passed, attempt to retrieve it from the cache.
124
+ rev && (document._rev = rev);
125
+
126
+ if (document._rev) {
127
+ this.put(id, document, callback);
128
+ } else if (this.cache.has(id)) {
129
+ document._rev = this.cache.get(id)._rev;
130
+ this.put(id, document, callback);
131
+ } else {
132
+ // Attempt to create a new document. If it fails,
133
+ // because an existing document with that _id exists (409),
134
+ // perform a HEAD, to get the _rev, and try to re-save.
135
+ this.put(id, document, function (e, res) {
136
+ if (e && e.headers && e.headers.status === 409) { // Conflict
137
+ that.head(id, function (e, headers, res) {
138
+ if (res === 404 || !headers['etag']) {
139
+ return callback({ reason: 'not_found' });
140
+ }
141
+
142
+ document._rev = headers['etag'].slice(1, -1);
143
+ that.put(id, document, callback);
144
+ });
145
+ } else { callback(e, res) }
146
+ });
147
+ }
148
+ // POST a single document, without an id (Create)
149
+ } else {
150
+ this.post(doc, callback);
151
+ }
152
+ }
153
+ };
154
+
155
+ Database.prototype.merge = function (/* [id], doc */) {
156
+ var args = Array.prototype.slice.call(arguments),
157
+ callback = args.pop(),
158
+ doc = args.pop(),
159
+ id = args.pop() || doc._id;
160
+
161
+ this._merge(id, doc, callback);
162
+ };
163
+
164
+ Database.prototype._merge = function (id, doc, callback) {
165
+ var that = this;
166
+ this.get(id, function (e, res) {
167
+ if (e) { return callback(e) }
168
+ doc = cradle.merge({}, res.json || res, doc);
169
+ that.save(id, res._rev, doc, callback);
170
+ });
171
+ };
172
+
173
+ Database.prototype.insert = function () {
174
+ throw new Error("`insert` is deprecated, use `save` instead");
175
+ };
176
+
177
+ Database.prototype.update = function (path, id, options) {
178
+ var args = new(Args)(arguments);
179
+ path = path.split('/');
180
+
181
+ if (id) {
182
+ return this.query({
183
+ method: 'PUT',
184
+ path: ['_design', path[0], '_update', path[1], id].map(querystring.escape).join('/'),
185
+ query: options
186
+ }, args.callback);
187
+ }
188
+
189
+ return this.query({
190
+ method: 'POST',
191
+ path: ['_design', path[0], '_update', path[1]].map(querystring.escape).join('/'),
192
+ query: options,
193
+ }, args.callback);
194
+ };
195
+
196
+ // Delete a document
197
+ // if the _rev wasn't supplied, we attempt to retrieve it from the
198
+ // cache. If the deletion was successful, we purge the cache.
199
+ Database.prototype.remove = function (id, rev) {
200
+ var that = this, doc, args = new(Args)(arguments);
201
+
202
+ if (typeof(rev) !== 'string') {
203
+ if (doc = this.cache.get(id)) { rev = doc._rev }
204
+ else { throw new(Error)("rev needs to be supplied") }
205
+ }
206
+
207
+ this.query({
208
+ method: 'DELETE',
209
+ path: cradle.escape(id),
210
+ query: { rev: rev }
211
+ }, function (err, res) {
212
+ if (! err) { that.cache.purge(id) }
213
+ args.callback(err, res);
214
+ });
215
+ };
@@ -0,0 +1,65 @@
1
+ var querystring = require('querystring'),
2
+ Args = require('vargs').Constructor,
3
+ cradle = require('../../cradle');
4
+
5
+ var Database = exports.Database = function (name, connection) {
6
+ this.connection = connection;
7
+ this.name = encodeURIComponent(name);
8
+ this.cache = new (cradle.Cache)(connection.options);
9
+ };
10
+
11
+ // A wrapper around `Connection.request`,
12
+ // which prepends the database name.
13
+ Database.prototype.query = function (options, callback) {
14
+ options.path = [this.name, options.path].filter(Boolean).join('/');
15
+ this.connection.request(options, callback);
16
+ };
17
+
18
+ Database.prototype.exists = function (callback) {
19
+ this.query({ method: 'HEAD' }, function (err, res, status) {
20
+ if (err) {
21
+ callback(err);
22
+ } else {
23
+ if (status === 404) {
24
+ callback(null, false);
25
+ } else {
26
+ callback(null, true);
27
+ }
28
+ }
29
+ });
30
+ };
31
+
32
+ Database.prototype.replicate = function (target, options, callback) {
33
+ if (typeof(options) === 'function') { callback = options, options = {} }
34
+ this.connection.replicate(cradle.merge({ source: name, target: target }, options), callback);
35
+ };
36
+
37
+ Database.prototype.info = function (callback) {
38
+ this.query({ method: 'GET' }, callback);
39
+ };
40
+
41
+ Database.prototype.create = function (callback) {
42
+ this.query({ method: 'PUT' }, callback);
43
+ };
44
+
45
+ // Destroys a database with 'DELETE'
46
+ // we raise an exception if arguments were supplied,
47
+ // as we don't want users to confuse this function with `remove`.
48
+ Database.prototype.destroy = function (callback) {
49
+ if (arguments.length > 1) {
50
+ throw new(Error)("destroy() doesn't take any additional arguments");
51
+ }
52
+
53
+ this.query({
54
+ method: 'DELETE',
55
+ path: '/',
56
+ }, callback);
57
+ };
58
+
59
+ //
60
+ // Extend the Database prototype with Couch features
61
+ //
62
+ require('./attachments');
63
+ require('./changes');
64
+ require('./documents');
65
+ require('./views');