forked from leancloud/javascript-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
567 lines (515 loc) · 16.7 KB
/
Copy pathutils.js
File metadata and controls
567 lines (515 loc) · 16.7 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/**
* 每位工程师都有保持代码优雅的义务
* Each engineer has a duty to keep the code elegant
**/
const _ = require('underscore');
const request = require('./request');
// Helper function to check null or undefined.
const isNullOrUndefined = (x) => _.isNull(x) || _.isUndefined(x);
const ensureArray = target => {
if (_.isArray(target)) {
return target;
}
if (target === undefined || target === null) {
return [];
}
return [target];
};
const init = (AV) => {
// 挂载一些配置
const AVConfig = AV._config;
_.extend(AVConfig, {
// 服务器节点地区,默认中国大陆
region: 'cn',
// 服务器的 URL,默认初始化时被设置为大陆节点地址
APIServerURL: AVConfig.APIServerURL || '',
// 当前是否为 nodejs 环境
isNode: false,
// 禁用 currentUser,通常用于多用户环境
disableCurrentUser: false,
// Internal config can modifie the UserAgent
userAgent: null,
// set production environment or test environment
// 1: production environment, 0: test environment, null: default environment
applicationProduction: null,
});
/**
* Contains all AV API classes and functions.
* @name AV
* @namespace
*
* Contains all AV API classes and functions.
*/
// Check whether we are running in Node.js.
if (typeof(process) !== 'undefined' && process.versions && process.versions.node) {
AVConfig.isNode = true;
}
// Helpers
// -------
// Shared empty constructor function to aid in prototype-chain creation.
var EmptyConstructor = function() {};
// Helper function to correctly set up the prototype chain, for subclasses.
// Similar to `goog.inherits`, but uses a hash of prototype properties and
// class properties to be extended.
var inherits = function(parent, protoProps, staticProps) {
var child;
// The constructor function for the new subclass is either defined by you
// (the "constructor" property in your `extend` definition), or defaulted
// by us to simply call the parent's constructor.
if (protoProps && protoProps.hasOwnProperty('constructor')) {
child = protoProps.constructor;
} else {
/** @ignore */
child = function(){ parent.apply(this, arguments); };
}
// Inherit class (static) properties from parent.
_.extend(child, parent);
// Set the prototype chain to inherit from `parent`, without calling
// `parent`'s constructor function.
EmptyConstructor.prototype = parent.prototype;
child.prototype = new EmptyConstructor();
// Add prototype properties (instance properties) to the subclass,
// if supplied.
if (protoProps) {
_.extend(child.prototype, protoProps);
}
// Add static properties to the constructor function, if supplied.
if (staticProps) {
_.extend(child, staticProps);
}
// Correctly set child's `prototype.constructor`.
child.prototype.constructor = child;
// Set a convenience property in case the parent's prototype is
// needed later.
child.__super__ = parent.prototype;
return child;
};
/**
* Call this method first to set up authentication tokens for AV.
* This method is for AV's own private use.
* @param {String} applicationId Your AV Application ID.
* @param {String} applicationKey Your AV Application Key
*/
const initialize = (appId, appKey, masterKey) => {
if (AV.applicationId && appId !== AV.applicationId && appKey !== AV.applicationKey && masterKey !== AV.masterKey) {
console.warn('LeanCloud SDK is already initialized, please do not reinitialize it.');
}
AV.applicationId = appId;
AV.applicationKey = appKey;
AV.masterKey = masterKey;
AV._useMasterKey = false;
};
/**
* Call this method first to set up your authentication tokens for AV.
* You can get your app keys from the LeanCloud dashboard on http://leancloud.cn .
* @function AV.init
* @param args initialize options.
* @param args.appId application id
* @param args.appKey application key
* @param args.masterKey application master key
*/
AV.init = (...args) => {
const masterKeyWarn = () => {
console.warn('MasterKey should not be used in the browser. ' +
'The permissions of MasterKey can be across all the server permissions,' +
' including the setting of ACL .');
};
switch (args.length) {
case 1:
const options = args[0];
if (typeof options === 'object') {
if (!AVConfig.isNode && options.masterKey) {
masterKeyWarn();
}
initialize(options.appId, options.appKey, options.masterKey);
request.setServerUrlByRegion(options.region);
AVConfig.disableCurrentUser = options.disableCurrentUser;
} else {
throw new Error('AV.init(): Parameter is not correct.');
}
break;
// 兼容旧版本的初始化方法
case 2:
case 3:
console.warn('Please use AV.init() to replace AV.initialize(), ' +
'AV.init() need an Object param, like { appId: \'YOUR_APP_ID\', appKey: \'YOUR_APP_KEY\' } . ' +
'Docs: https://leancloud.cn/docs/sdk_setup-js.html');
if (!AVConfig.isNode && args.length === 3) {
masterKeyWarn();
}
initialize(...args);
request.setServerUrlByRegion('cn');
break;
}
};
// If we're running in node.js, allow using the master key.
if (AVConfig.isNode) {
AV.Cloud = AV.Cloud || {};
/**
* Switches the LeanCloud SDK to using the Master key. The Master key grants
* priveleged access to the data in LeanCloud and can be used to bypass ACLs and
* other restrictions that are applied to the client SDKs.
* <p><strong><em>Available in Cloud Code and Node.js only.</em></strong>
* </p>
*/
AV.Cloud.useMasterKey = function() {
AV._useMasterKey = true;
};
}
// 兼容老版本的初始化方法
AV.initialize = AV.init;
/**
* Call this method to set production environment variable.
* @function AV.setProduction
* @param {Boolean} production True is production environment,and
* it's true by default.
*/
AV.setProduction = (production) => {
if (!isNullOrUndefined(production)) {
AVConfig.applicationProduction = production ? 1 : 0;
} else {
// change to default value
AVConfig.applicationProduction = null;
}
};
/**
* @deprecated Please use AV.init(), you can set the region of server .
**/
// TODO: 后续不再暴露此接口
AV.useAVCloudCN = function(){
request.setServerUrlByRegion('cn');
console.warn('Do not use AV.useAVCloudCN. Please use AV.init(), you can set the region of server.');
};
/**
* @deprecated Please use AV.init(), you can set the region of server .
**/
// TODO: 后续不再暴露此接口
AV.useAVCloudUS = function(){
request.setServerUrlByRegion('us');
console.warn('Do not use AV.useAVCloudUS. Please use AV.init(), you can set the region of server.');
};
/**
* Returns prefix for localStorage keys used by this instance of AV.
* @param {String} path The relative suffix to append to it.
* null or undefined is treated as the empty string.
* @return {String} The full key name.
*/
AV._getAVPath = function(path) {
if (!AV.applicationId) {
throw "You need to call AV.initialize before using AV.";
}
if (!path) {
path = "";
}
if (!_.isString(path)) {
throw "Tried to get a localStorage path that wasn't a String.";
}
if (path[0] === "/") {
path = path.substring(1);
}
return "AV/" + AV.applicationId + "/" + path;
};
/**
* Returns the unique string for this app on this machine.
* Gets reset when localStorage is cleared.
*/
AV._installationId = null;
AV._getInstallationId = function() {
// See if it's cached in RAM.
if (AV._installationId) {
return AV.Promise.as(AV._installationId);
}
// Try to get it from localStorage.
var path = AV._getAVPath("installationId");
return AV.localStorage.getItemAsync(path).then(function(_installationId){
AV._installationId = _installationId;
if (!AV._installationId) {
// It wasn't in localStorage, so create a new one.
var hexOctet = function() {
return Math.floor((1+Math.random())*0x10000).toString(16).substring(1);
};
AV._installationId = (
hexOctet() + hexOctet() + "-" +
hexOctet() + "-" +
hexOctet() + "-" +
hexOctet() + "-" +
hexOctet() + hexOctet() + hexOctet());
return AV.localStorage.setItemAsync(path, AV._installationId);
}
else {
return _installationId;
}
});
};
AV._parseDate = function(iso8601) {
var regexp = new RegExp(
"^([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2})" + "T" +
"([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})" +
"(.([0-9]+))?" + "Z$");
var match = regexp.exec(iso8601);
if (!match) {
return null;
}
var year = match[1] || 0;
var month = (match[2] || 1) - 1;
var day = match[3] || 0;
var hour = match[4] || 0;
var minute = match[5] || 0;
var second = match[6] || 0;
var milli = match[8] || 0;
return new Date(Date.UTC(year, month, day, hour, minute, second, milli));
};
// TODO: Next version remove
AV._ajax = (...args) => {
console.warn('AV._ajax is deprecated, and will be removed in next release.');
request.ajax(...args);
};
// TODO: Next version remove
AV._request = (...args) => {
console.warn('AV._request is deprecated, and will be removed in next release.');
request.request(...args);
};
// A self-propagating extend function.
AV._extend = function(protoProps, classProps) {
var child = inherits(this, protoProps, classProps);
child.extend = this.extend;
return child;
};
// Helper function to get a value from a Backbone object as a property
// or as a function.
AV._getValue = function(object, prop) {
if (!(object && object[prop])) {
return null;
}
return _.isFunction(object[prop]) ? object[prop]() : object[prop];
};
/**
* Converts a value in a AV Object into the appropriate representation.
* This is the JS equivalent of Java's AV.maybeReferenceAndEncode(Object)
* if seenObjects is falsey. Otherwise any AV.Objects not in
* seenObjects will be fully embedded rather than encoded
* as a pointer. This array will be used to prevent going into an infinite
* loop because we have circular references. If <seenObjects>
* is set, then none of the AV Objects that are serialized can be dirty.
*/
AV._encode = function(value, seenObjects, disallowObjects) {
if (value instanceof AV.Object) {
if (disallowObjects) {
throw "AV.Objects not allowed here";
}
if (!seenObjects || _.include(seenObjects, value) || !value._hasData) {
return value._toPointer();
}
if (!value.dirty()) {
seenObjects = seenObjects.concat(value);
return AV._encode(value._toFullJSON(seenObjects),
seenObjects,
disallowObjects);
}
throw "Tried to save an object with a pointer to a new, unsaved object.";
}
if (value instanceof AV.ACL) {
return value.toJSON();
}
if (_.isDate(value)) {
return { "__type": "Date", "iso": value.toJSON() };
}
if (value instanceof AV.GeoPoint) {
return value.toJSON();
}
if (_.isArray(value)) {
return _.map(value, function(x) {
return AV._encode(x, seenObjects, disallowObjects);
});
}
if (_.isRegExp(value)) {
return value.source;
}
if (value instanceof AV.Relation) {
return value.toJSON();
}
if (value instanceof AV.Op) {
return value.toJSON();
}
if (value instanceof AV.File) {
if (!value.url() && !value.id) {
throw "Tried to save an object containing an unsaved file.";
}
return {
__type: "File",
id: value.id,
name: value.name(),
url: value.url()
};
}
if (_.isObject(value)) {
var output = {};
AV._objectEach(value, function(v, k) {
output[k] = AV._encode(v, seenObjects, disallowObjects);
});
return output;
}
return value;
};
/**
* The inverse function of AV._encode.
* TODO: make decode not mutate value.
*/
AV._decode = function(key, value) {
if (!_.isObject(value)) {
return value;
}
if (_.isArray(value)) {
AV._arrayEach(value, function(v, k) {
value[k] = AV._decode(k, v);
});
return value;
}
if (value instanceof AV.Object) {
return value;
}
if (value instanceof AV.File) {
return value;
}
if (value instanceof AV.Op) {
return value;
}
if (value.__op) {
return AV.Op._decode(value);
}
var className;
if (value.__type === "Pointer") {
className = value.className;
var pointer = AV.Object._create(className);
if(Object.keys(value).length > 3) {
delete value.__type;
delete value.className;
pointer._finishFetch(value, true);
}else{
pointer._finishFetch({ objectId: value.objectId }, false);
}
return pointer;
}
if (value.__type === "Object") {
// It's an Object included in a query result.
className = value.className;
delete value.__type;
delete value.className;
var object = AV.Object._create(className);
object._finishFetch(value, true);
return object;
}
if (value.__type === "Date") {
return AV._parseDate(value.iso);
}
if (value.__type === "GeoPoint") {
return new AV.GeoPoint({
latitude: value.latitude,
longitude: value.longitude
});
}
if (key === "ACL") {
if (value instanceof AV.ACL) {
return value;
}
return new AV.ACL(value);
}
if (value.__type === "Relation") {
var relation = new AV.Relation(null, key);
relation.targetClassName = value.className;
return relation;
}
if (value.__type === 'File') {
var file = new AV.File(value.name);
file.attributes.metaData = value.metaData || {};
file.attributes.url = value.url;
file.id = value.objectId;
return file;
}
AV._objectEach(value, function(v, k) {
value[k] = AV._decode(k, v);
});
return value;
};
AV._encodeObjectOrArray = function(value) {
var encodeAVObject = function(object) {
if (object && object._toFullJSON){
object = object._toFullJSON([]);
}
return _.mapObject(object, function(value) {
return AV._encode(value, []);
});
};
if (_.isArray(value)) {
return value.map(function(object) {
return encodeAVObject(object);
});
} else {
return encodeAVObject(value);
}
};
AV._arrayEach = _.each;
/**
* Does a deep traversal of every item in object, calling func on every one.
* @param {Object} object The object or array to traverse deeply.
* @param {Function} func The function to call for every item. It will
* be passed the item as an argument. If it returns a truthy value, that
* value will replace the item in its parent container.
* @returns {} the result of calling func on the top-level object itself.
*/
AV._traverse = function(object, func, seen) {
if (object instanceof AV.Object) {
seen = seen || [];
if (_.indexOf(seen, object) >= 0) {
// We've already visited this object in this call.
return;
}
seen.push(object);
AV._traverse(object.attributes, func, seen);
return func(object);
}
if (object instanceof AV.Relation || object instanceof AV.File) {
// Nothing needs to be done, but we don't want to recurse into the
// object's parent infinitely, so we catch this case.
return func(object);
}
if (_.isArray(object)) {
_.each(object, function(child, index) {
var newChild = AV._traverse(child, func, seen);
if (newChild) {
object[index] = newChild;
}
});
return func(object);
}
if (_.isObject(object)) {
AV._each(object, function(child, key) {
var newChild = AV._traverse(child, func, seen);
if (newChild) {
object[key] = newChild;
}
});
return func(object);
}
return func(object);
};
/**
* This is like _.each, except:
* * it doesn't work for so-called array-like objects,
* * it does work for dictionaries with a "length" attribute.
*/
AV._objectEach = AV._each = function(obj, callback) {
if (_.isObject(obj)) {
_.each(_.keys(obj), function(key) {
callback(obj[key], key);
});
} else {
_.each(obj, callback);
}
};
};
module.exports = {
init,
isNullOrUndefined,
ensureArray,
};