UNPKG

1.71 kBMarkdownView Raw
1Creates a thru stream (that should be applied to the output of the backend).
2
3 ec = encodeURIComponent
4 dc = decodeURIComponent
5
6In the parameter we need:
7- .url — base url (used to build URIs provided to external users)
8- .target — a function that takes and id and returns a set of request options (esp. `.url` which must be a `new URL` object)
9- .secret
10- .hash (defaults to `sha256`)
11- .timeout (defaults to one hour)
12
13 attachments = (our_proxy) ->
14 target = our_proxy.target
15
16 our_proxy.target = (pathname) ->
17 return null unless $ = pathname.match ///
18 ^ / ([^/]+) / (.+) $
19 ///
20 target? pathname, dc($[1]), $[2]
21
22 {handler,download_uri,upload_uri} = proxy our_proxy
23
24 thru = (stream) ->
25 stream.map (msg) ->
26 rev = msg.getIn ['value','rev']
27 attachments = msg.getIn ['doc','_attachments']
28 return msg unless rev? and attachments?
29
30 id = msg.get 'id'
31 ec_id = ec id
32 msg.withMutations (msg) ->
33 attachments.forEach (rec,name) ->
34 path = "/#{ec_id}/#{ec name}"
35 if rec.has 'stub'
36 msg = msg.setIn ['doc','_attachments',name,'download_uri'], download_uri path, rev
37 msg = msg.setIn ['doc','_attachments',name,'upload_uri'], upload_uri path, rev
38 return
39 msg
40
41 {handler,thru}
42
43The `handler` can be used either as a `createServer` request-listener, or as middleware (using Express conventions).
44The `thru` can be used as a most.js `.thru` processor on (e.g.) the output of the backe
45
46 module.exports = attachments
47 proxy = require './util/attachments-proxy'
48 {URL} = require 'url'