1 | 'use strict';
|
2 | const _ = require('lodash'),
|
3 | nodeUrl = require('url'),
|
4 | replace = require('string-replace-async'),
|
5 | h = require('highland'),
|
6 | b64 = require('base-64'),
|
7 | types = require('./types');
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function add(dispatch, prefix) {
|
16 | const stringDispatch = JSON.stringify(dispatch);
|
17 |
|
18 | let urlPrefix = prefix;
|
19 |
|
20 | if (_.includes(prefix, 'http')) {
|
21 |
|
22 | prefix = urlToUri(prefix);
|
23 | }
|
24 |
|
25 | return h(replace(stringDispatch, /"\/_?(components|uris|pages|lists|users|layouts)(\/[\w-\/]*)/g, (match, type, name) => {
|
26 | if (type === 'uris') {
|
27 | return Promise.resolve(`"${prefix}/_${type}/${b64.encode(prefix + name)}`);
|
28 | } else {
|
29 | return Promise.resolve(`"${prefix}/_${type}${name}`);
|
30 | }
|
31 | }).then((prefixedString) => replace(prefixedString, /"customUrl":"(.*)"/g, (match, uri) => Promise.resolve(`"customUrl":"${urlPrefix}${uri}"`)))).map(JSON.parse);
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | function remove(dispatch, prefix) {
|
41 | const stringDispatch = JSON.stringify(dispatch);
|
42 |
|
43 | let urlPrefix = prefix;
|
44 |
|
45 | if (_.includes(prefix, 'http')) {
|
46 |
|
47 | prefix = urlToUri(prefix);
|
48 | }
|
49 |
|
50 | return h(replace(stringDispatch, new RegExp(`"${prefix}\/_?(components|uris|pages|lists|users|layouts)/(.+?)"`, 'g'), (match, type, end) => {
|
51 | if (type === 'uris') {
|
52 | return Promise.resolve(`"/_${type}${b64.decode(end).replace(prefix, '')}"`);
|
53 | } else {
|
54 | return Promise.resolve(`"/_${type}/${end}"`);
|
55 | }
|
56 | }).then((unprefixedString) => replace(unprefixedString, /"customUrl":"(.*)"/g, (match, prefixedURI) => Promise.resolve(`"customUrl":"${prefixedURI.replace(urlPrefix, '')}"`)))).map(JSON.parse);
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | function getFromUrl(url) {
|
66 | let type = _.find(types, (t) => _.includes(url, t));
|
67 |
|
68 | if (type) {
|
69 | return url.slice(0, url.indexOf(type));
|
70 | } else {
|
71 | throw new Error(`Unable to find site prefix for ${url}`);
|
72 | }
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | function uriToUrl(prefix, uri) {
|
82 | let type = _.find(types, (t) => _.includes(uri, t)),
|
83 | parts = uri.split(type),
|
84 | path = parts[1];
|
85 |
|
86 | return `${prefix}${type}${path}`;
|
87 | }
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | function urlToUri(url) {
|
96 | const parts = nodeUrl.parse(url);
|
97 |
|
98 | let path;
|
99 |
|
100 | if (parts.pathname === '/') {
|
101 | path = '';
|
102 | } else if (_.includes(parts.pathname, '.')) {
|
103 | path = parts.pathname.slice(0, parts.pathname.indexOf('.'));
|
104 | } else {
|
105 | path = parts.pathname;
|
106 | }
|
107 |
|
108 | return parts.hostname + path;
|
109 | }
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | function getExt(url) {
|
117 | const parts = nodeUrl.parse(url);
|
118 |
|
119 | if (_.includes(parts.pathname, '.')) {
|
120 | return parts.pathname.slice(parts.pathname.indexOf('.'));
|
121 | } else {
|
122 | return null;
|
123 | }
|
124 | }
|
125 |
|
126 | module.exports.add = add;
|
127 | module.exports.remove = remove;
|
128 | module.exports.getFromUrl = getFromUrl;
|
129 | module.exports.uriToUrl = uriToUrl;
|
130 | module.exports.urlToUri = urlToUri;
|
131 | module.exports.getExt = getExt;
|