UNPKG

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