Repository navigation
Expand file tree
/
Copy pathlib.js
More file actions
326 lines (300 loc) 路 10.3 KB
/
Copy pathlib.js
File metadata and controls
326 lines (300 loc) 路 10.3 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import { check, Match } from 'meteor/check';
/**
* @const {Set<string>} UNSAFE_KEYS - Keys skipped when walking untrusted objects
*/
const UNSAFE_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
const helpers = {
sanitize(str = '', max = 28, replacement = '-') {
return str.replace(/([^a-z0-9\-\_]+)/gi, replacement).substring(0, max);
},
isUndefined(obj) {
return obj === void 0;
},
isObject(obj) {
if (obj === null || this.isArray(obj) || this.isFunction(obj)) {
return false;
}
return obj === Object(obj);
},
isNumber(obj) {
return Object.prototype.toString.call(obj) === '[object Number]';
},
isDate(obj) {
return Object.prototype.toString.call(obj) === '[object Date]';
},
isString(obj) {
return Object.prototype.toString.call(obj) === '[object String]';
},
isArray(obj) {
return Array.isArray(obj);
},
isBoolean(obj) {
return obj === true || obj === false || Object.prototype.toString.call(obj) === '[object Boolean]';
},
isFunction(obj) {
if (this.isUndefined(obj)) {
return false;
}
const type = Object.prototype.toString.call(obj);
return type === '[object Function]' || type === '[object AsyncFunction]';
},
isEmpty(obj) {
if (this.isDate(obj)) {
return false;
}
if (this.isObject(obj)) {
return !Object.keys(obj).length;
}
if (this.isArray(obj) || this.isString(obj)) {
return !obj.length;
}
return false;
},
clone(obj) {
if (!this.isObject(obj)) {
return obj;
}
return this.isArray(obj) ? [...obj] : { ...obj };
},
cloneDeep(obj) {
if (this.isArray(obj)) {
return obj.map((item) => this.cloneDeep(item));
}
if (this.isDate(obj)) {
return new Date(obj.getTime());
}
if (ArrayBuffer.isView(obj)) {
return obj.slice();
}
if (!this.isObject(obj)) {
return obj;
}
const proto = Object.getPrototypeOf(obj);
if (proto !== Object.prototype && proto !== null) {
// Class instances (ObjectID, Binary, etc.) are kept by reference
return obj;
}
const copy = {};
for (const key of Object.keys(obj)) {
if (UNSAFE_KEYS.has(key)) {
continue;
}
copy[key] = this.cloneDeep(obj[key]);
}
return copy;
},
has(_obj, path) {
let obj = _obj;
if (!this.isObject(obj)) {
return false;
}
if (!this.isArray(path)) {
return this.isObject(obj) && Object.prototype.hasOwnProperty.call(obj, path);
}
const length = path.length;
for (let i = 0; i < length; i++) {
if (!Object.prototype.hasOwnProperty.call(obj, path[i])) {
return false;
}
obj = obj[path[i]];
}
return !!length;
},
omit(obj, ...keys) {
const clear = Object.assign({}, obj);
for (let i = keys.length - 1; i >= 0; i--) {
delete clear[keys[i]];
}
return clear;
},
now: Date.now,
throttle(func, wait, options = {}) {
let previous = 0;
let timeout = null;
let result;
const that = this;
let self;
let args;
const later = () => {
previous = options.leading === false ? 0 : that.now();
timeout = null;
result = func.apply(self, args);
if (!timeout) {
self = args = null;
}
};
const throttled = function () {
const now = that.now();
if (!previous && options.leading === false) previous = now;
const remaining = wait - (now - previous);
self = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(self, args);
if (!timeout) {
self = args = null;
}
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
throttled.cancel = () => {
clearTimeout(timeout);
previous = 0;
timeout = self = args = null;
};
return throttled;
}
};
/**
* @const {function} fixJSONParse - Fix issue with Date parse
* @summary Revive `=--JSON-DATE--=` strings into `Date` objects in place. Walks own keys only and skips `__proto__`, `constructor`, and `prototype`
*/
const fixJSONParse = function(obj) {
for (const key of Object.keys(obj)) {
if (UNSAFE_KEYS.has(key)) {
continue;
}
if (helpers.isString(obj[key]) && obj[key].includes('=--JSON-DATE--=')) {
obj[key] = obj[key].replace('=--JSON-DATE--=', '');
obj[key] = new Date(parseInt(obj[key]));
} else if (helpers.isObject(obj[key])) {
obj[key] = fixJSONParse(obj[key]);
} else if (helpers.isArray(obj[key])) {
let v;
for (let i = 0; i < obj[key].length; i++) {
v = obj[key][i];
if (helpers.isObject(v)) {
obj[key][i] = fixJSONParse(v);
} else if (helpers.isString(v) && v.includes('=--JSON-DATE--=')) {
v = v.replace('=--JSON-DATE--=', '');
obj[key][i] = new Date(parseInt(v));
}
}
}
}
return obj;
};
/**
* @const {function} fixJSONStringify - Fix issue with Date stringify
* @summary Replace `Date` objects with `=--JSON-DATE--=` strings in place. Walks own keys only and skips `__proto__`, `constructor`, and `prototype`
*/
const fixJSONStringify = function(obj) {
for (const key of Object.keys(obj)) {
if (UNSAFE_KEYS.has(key)) {
continue;
}
if (helpers.isDate(obj[key])) {
obj[key] = `=--JSON-DATE--=${+obj[key]}`;
} else if (helpers.isObject(obj[key])) {
obj[key] = fixJSONStringify(obj[key]);
} else if (helpers.isArray(obj[key])) {
let v;
for (let i = 0; i < obj[key].length; i++) {
v = obj[key][i];
if (helpers.isObject(v)) {
obj[key][i] = fixJSONStringify(v);
} else if (helpers.isDate(v)) {
obj[key][i] = `=--JSON-DATE--=${+v}`;
}
}
}
}
return obj;
};
/**
* @const {RegExp} LOCAL_ROUTE_RE - A local absolute path, not a protocol-relative or absolute URL
*/
const LOCAL_ROUTE_RE = /^\/[^/]/;
/**
* @const {RegExp} UNSAFE_ROUTE_RE - Parts not allowed in a stored route: `..`, `//`, `@`, `:`, `\`, `?`, `#`, encoded `.`, `/`, and `\` (`%2e`, `%2f`, `%5c`, any case), whitespace, and control characters
*/
// eslint-disable-next-line no-control-regex
const UNSAFE_ROUTE_RE = /\.\.|\/\/|%2e|%2f|%5c|[@:\\?#\s\x00-\x1f\x7f]/i;
/**
* @const {RegExp} PLAIN_NAME_RE - A plain collection name: letters, digits, `_`, `-`, and `.`
*/
const PLAIN_NAME_RE = /^[A-Za-z0-9_.-]+$/;
/**
* @private
* @summary Returns `true` for a stored `_downloadRoute` that is a safe local path
* @param {*} route - Value to check
* @returns {boolean}
*/
const isSafeRoute = (route) => helpers.isString(route) && LOCAL_ROUTE_RE.test(route) && !UNSAFE_ROUTE_RE.test(route);
/**
* @private
* @summary Returns `true` for a stored `_collectionName` that is a plain name
* @param {*} name - Value to check
* @returns {boolean}
*/
const isPlainName = (name) => helpers.isString(name) && PLAIN_NAME_RE.test(name) && !name.includes('..');
/**
* @locus Anywhere
* @private
* @name formatFileURL
* @param {Partial<FileObj>|FileCursor} fileRef - File reference object
* @param {string} [version] - [Optional] Version of file you would like build URL for
* @param {string} [uriBase] - [Optional] URI base, see - https://github.com/veliovgroup/Meteor-Files/issues/626
* @param {FilesCollection} [collection] - [Optional] Collection of the file. Its `downloadRoute` and `collectionName` replace the document's `_downloadRoute` and `_collectionName` when those are not safe
* @summary Returns formatted URL for file. Uses the document's `_downloadRoute` when it is a safe local path (`/...` without `..`, `//`, `@`, `:`, `\`, `?`, `#`, `%2e`, `%2f`, `%5c`, whitespace, or control characters) and `_collectionName` when it is a plain name, otherwise the collection's values. Returns an empty string when neither is available or the file has no `_id`. `_id`, `version`, the extension, and the collection name are URI-encoded
* @returns {string} Downloadable link
*/
// eslint-disable-next-line camelcase, no-undef
const formatFileURL = (_fileRef, version = 'original', _uriBase = (__meteor_runtime_config__ || {}).ROOT_URL, collection) => {
// Unwrap FileCursor
const fileRef = (helpers.isObject(_fileRef) && helpers.isObject(_fileRef._fileRef)) ? _fileRef._fileRef : _fileRef;
// eslint-disable-next-line new-cap
check(fileRef, Match.Where((obj) => helpers.isObject(obj)));
check(version, String);
let uriBase = _uriBase;
if (!helpers.isString(uriBase)) {
// eslint-disable-next-line camelcase, no-undef
uriBase = (__meteor_runtime_config__ || {}).ROOT_URL || '/';
}
// Stored values are set by the server (client-supplied ones are stripped since 3.1), keep them when safe, as in 3.0.x
const hasCollection = helpers.isObject(collection);
let route;
if (isSafeRoute(fileRef._downloadRoute)) {
route = fileRef._downloadRoute;
} else if (hasCollection && helpers.isString(collection.downloadRoute)) {
// Trusted: collection's own configuration
route = collection.downloadRoute;
} else {
return '';
}
let collectionName;
if (isPlainName(fileRef._collectionName)) {
collectionName = fileRef._collectionName;
} else if (hasCollection && helpers.isString(collection.collectionName)) {
collectionName = collection.collectionName;
} else if (fileRef.public !== true) {
return '';
}
// No `_id`, for example the file object of a rejected upload: there is no file to link to
if (fileRef._id === undefined || fileRef._id === null || fileRef._id === '') {
return '';
}
const _root = uriBase.replace(/\/+$/, '');
const vRef = (fileRef.versions && fileRef.versions[version]) || fileRef || {};
let ext;
if (helpers.isString(vRef.extension)) {
ext = `.${encodeURIComponent(vRef.extension.replace(/^\./, ''))}`;
} else {
ext = '';
}
const _id = encodeURIComponent(`${fileRef._id}`);
const _version = encodeURIComponent(version);
if (fileRef.public === true) {
return _root + (version === 'original' ? `${route}/${_id}${ext}` : `${route}/${_version}-${_id}${ext}`);
}
collectionName = encodeURIComponent(`${collectionName}`);
return `${_root}${route}/${collectionName}/${_id}/${_version}/${_id}${ext}`;
};
export { fixJSONParse, fixJSONStringify, formatFileURL, helpers };