-
Notifications
You must be signed in to change notification settings - Fork 0
/
SupCore.js
14612 lines (13751 loc) · 482 KB
/
SupCore.js
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.SupCore=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
(function (process){
/// <reference path="../typings/tsd.d.ts" />
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
/* tslint:disable:no-unused-variable */
var Data = require("./Data");
exports.Data = Data;
/* tslint:enable:no-unused-variable */
__export(require("./systems"));
function log(message) {
var date = new Date();
var text = date.toLocaleDateString() + " " + date.toLocaleTimeString() + " - " + message;
console.log(text);
if (process != null && process.send != null)
process.send(text);
return;
}
exports.log = log;
}).call(this,require('_process'))
},{"./Data":18,"./systems":19,"_process":25}],2:[function(require,module,exports){
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var SupData = require("./index");
var fs = require("fs");
var path = require("path");
var Assets = (function (_super) {
__extends(Assets, _super);
function Assets(server) {
_super.call(this);
this.server = server;
}
Assets.prototype.acquire = function (id, owner, callback) {
if (this.server.data.entries.byId[id] == null || this.server.data.entries.byId[id].type == null) {
callback(new Error("Invalid asset id: " + id), null);
return;
}
_super.prototype.acquire.call(this, id, owner, callback);
};
Assets.prototype._load = function (id) {
var _this = this;
var entry = this.server.data.entries.byId[id];
var assetClass = this.server.system.data.assetClasses[entry.type];
if (assetClass == null)
throw new Error("No data plugin for asset type \"" + entry.type + "\"");
var asset = new assetClass(id, null, this.server);
// NOTE: The way assets are laid out on disk was changed in Superpowers 0.11
var oldDirPath = path.join(this.server.projectPath, "assets/" + id);
fs.stat(oldDirPath, function (err, stats) {
var dirPath = path.join(_this.server.projectPath, "assets/" + _this.server.data.entries.getStoragePathFromId(id));
if (stats == null)
asset.load(dirPath);
else {
fs.rename(oldDirPath, dirPath, function (err) {
if (err != null)
throw err;
asset.load(dirPath);
});
}
});
return asset;
};
return Assets;
})(SupData.Base.Dictionary);
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = Assets;
},{"./index":18,"fs":20,"path":24}],3:[function(require,module,exports){
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var ListById_1 = require("./Base/ListById");
var Badges = (function (_super) {
__extends(Badges, _super);
function Badges(pub) {
_super.call(this, pub, Badges.schema);
}
Badges.schema = {
id: { type: "string" },
type: { type: "string" },
data: { type: "any" }
};
return Badges;
})(ListById_1.default);
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = Badges;
},{"./Base/ListById":7}],4:[function(require,module,exports){
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Hash_1 = require("./Hash");
var path = require("path");
var fs = require("fs");
var mkdirp = require("mkdirp");
var Asset = (function (_super) {
__extends(Asset, _super);
function Asset(id, pub, schema, server) {
_super.call(this, pub, schema);
this.id = id;
this.server = server;
this.setMaxListeners(Infinity);
if (this.server == null)
this.setup();
}
Asset.prototype.init = function (options, callback) { this.setup(); callback(); };
Asset.prototype.setup = function () { };
Asset.prototype.restore = function () { };
Asset.prototype.destroy = function (callback) { callback(); };
Asset.prototype.load = function (assetPath) {
var _this = this;
fs.readFile(path.join(assetPath, "asset.json"), { encoding: "utf8" }, function (err, json) {
if (err != null)
throw err;
var pub = JSON.parse(json);
_this._onLoaded(assetPath, pub);
});
};
Asset.prototype._onLoaded = function (assetPath, pub) {
var _this = this;
this.migrate(assetPath, pub, function (hasMigrated) {
if (hasMigrated) {
_this.pub = pub;
_this.save(assetPath, function (err) {
_this.setup();
_this.emit("load");
});
}
else {
_this.pub = pub;
_this.setup();
_this.emit("load");
}
});
};
Asset.prototype.unload = function () { this.removeAllListeners(); };
Asset.prototype.migrate = function (assetPath, pub, callback) { callback(false); };
;
Asset.prototype.client_load = function () { };
Asset.prototype.client_unload = function () { };
Asset.prototype.save = function (assetPath, callback) {
var json = JSON.stringify(this.pub, null, 2);
fs.writeFile(path.join(assetPath, "asset.json"), json, { encoding: "utf8" }, callback);
};
Asset.prototype.publish = function (buildPath, callback) {
var _this = this;
var folderPath = buildPath + "/assets/" + this.server.data.entries.getStoragePathFromId(this.id);
mkdirp(folderPath, function (err) { _this.save(folderPath, callback); });
};
Asset.prototype.server_setProperty = function (client, path, value, callback) {
this.setProperty(path, value, function (err, actualValue) {
if (err != null) {
callback(err);
return;
}
callback(null, path, actualValue);
});
};
return Asset;
})(Hash_1.default);
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = Asset;
},{"./Hash":6,"fs":20,"mkdirp":23,"path":24}],5:[function(require,module,exports){
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var events_1 = require("events");
var Dictionary = (function (_super) {
__extends(Dictionary, _super);
function Dictionary(unloadDelaySeconds) {
if (unloadDelaySeconds === void 0) { unloadDelaySeconds = 60; }
_super.call(this);
this.unloadTimeoutsById = {};
this.byId = {};
this.refCountById = {};
this.unloadDelaySeconds = unloadDelaySeconds;
}
Dictionary.prototype.acquire = function (id, owner, callback) {
var _this = this;
if (this.refCountById[id] == null)
this.refCountById[id] = 0;
this.refCountById[id]++;
// console.log(`Acquiring ${id}: ${this.refCountById[id]} refs`);
// Cancel pending unload timeout if any
var timeout = this.unloadTimeoutsById[id];
if (timeout != null) {
// console.log(`Cancelling unload timeout for ${id}`);
clearTimeout(timeout);
delete this.unloadTimeoutsById[id];
}
var item = this.byId[id];
if (item == null) {
try {
item = this._load(id);
}
catch (e) {
callback(e, null);
return;
}
this.byId[id] = item;
item.on("load", function () {
// Bail if entry was evicted from the cache
if (_this.byId[id] == null)
return;
_this.emit("itemLoad", id, item);
});
}
if (item.pub != null)
callback(null, item);
else
item.on("load", function () {
// Bail if entry was evicted from the cache
if (_this.byId[id] == null)
return;
callback(null, item);
return;
});
};
Dictionary.prototype.release = function (id, owner, options) {
var _this = this;
if (this.refCountById[id] == null) {
// This might happen if .releaseAll(id) was called elsewhere since we called acquire
// Just log and ignore
console.log("Can't release " + id + ", ref count is null");
return;
}
this.refCountById[id]--;
// console.log(`Releasing ${id}: ${this.refCountById[id]} refs left`);
if (this.refCountById[id] === 0) {
delete this.refCountById[id];
// Schedule unloading the asset after a while
if (options != null && options.skipUnloadDelay)
this._unload(id);
else
this.unloadTimeoutsById[id] = setTimeout(function () { _this._unload(id); }, this.unloadDelaySeconds * 1000);
}
};
Dictionary.prototype._load = function (id) { throw new Error("This method must be overridden by derived classes"); };
Dictionary.prototype._unload = function (id) {
// console.log(`Unloading ${id}`);
this.byId[id].unload();
delete this.byId[id];
delete this.unloadTimeoutsById[id];
};
Dictionary.prototype.releaseAll = function (id) {
// Cancel pending unload timeout if any
var timeout = this.unloadTimeoutsById[id];
if (timeout != null) {
clearTimeout(timeout);
delete this.unloadTimeoutsById[id];
}
delete this.refCountById[id];
delete this.byId[id];
};
return Dictionary;
})(events_1.EventEmitter);
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = Dictionary;
},{"events":21}],6:[function(require,module,exports){
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var base = require("./index");
var events_1 = require("events");
var Hash = (function (_super) {
__extends(Hash, _super);
function Hash(pub, schema) {
_super.call(this);
this.pub = pub;
this.schema = schema;
}
Hash.prototype.setProperty = function (path, value, callback) {
var parts = path.split(".");
var rule = this.schema[parts[0]];
for (var _i = 0, _a = parts.slice(1); _i < _a.length; _i++) {
var part = _a[_i];
rule = rule.properties[part];
if (rule.type === "any")
break;
}
if (rule == null) {
callback("Invalid key: " + path);
return;
}
if (rule.type !== "any") {
var violation = base.getRuleViolation(value, rule);
if (violation != null) {
callback("Invalid value for " + path + ": " + base.formatRuleViolation(violation));
return;
}
}
var obj = this.pub;
for (var _b = 0, _c = parts.slice(0, parts.length - 1); _b < _c.length; _b++) {
var part = _c[_b];
obj = obj[part];
}
obj[parts[parts.length - 1]] = value;
callback(null, value);
this.emit("change");
};
Hash.prototype.client_setProperty = function (path, value) {
var parts = path.split(".");
var obj = this.pub;
for (var _i = 0, _a = parts.slice(0, parts.length - 1); _i < _a.length; _i++) {
var part = _a[_i];
obj = obj[part];
}
obj[parts[parts.length - 1]] = value;
};
return Hash;
})(events_1.EventEmitter);
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = Hash;
},{"./index":10,"events":21}],7:[function(require,module,exports){
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var base = require("./index");
var events_1 = require("events");
var ListById = (function (_super) {
__extends(ListById, _super);
function ListById(pub, schema, generateNextId) {
var _this = this;
_super.call(this);
this.pub = pub;
this.schema = schema;
this.nextId = 0;
this.byId = {};
this.generateNextId = generateNextId;
var maxItemId = -1;
for (var _i = 0, _a = this.pub; _i < _a.length; _i++) {
var item = _a[_i];
// NOTE: Legacy stuff from Superpowers 0.4
if (typeof item.id === "number")
item.id = item.id.toString();
this.byId[item.id] = item;
maxItemId = Math.max(maxItemId, item.id);
}
if (this.generateNextId == null) {
this.generateNextId = function () {
var id = _this.nextId.toString();
_this.nextId++;
return id;
};
this.nextId = maxItemId + 1;
}
}
ListById.prototype.add = function (item, index, callback) {
if (item.id != null && this.schema["id"] == null) {
callback("Found unexpected id key");
return;
}
var missingKeys = Object.keys(this.schema);
for (var key in item) {
var value = item[key];
var rule = this.schema[key];
if (rule == null) {
if (key === "id" && value == null)
continue;
callback("Invalid key: " + key);
return;
}
var violation = base.getRuleViolation(value, rule, true);
if (violation != null) {
callback("Invalid value for " + key + ": " + base.formatRuleViolation(violation));
return;
}
missingKeys.splice(missingKeys.indexOf(key), 1);
}
if (missingKeys.length > 0) {
callback("Missing key: " + missingKeys[0]);
return;
}
if (item.id == null)
item.id = this.generateNextId();
this.byId[item.id] = item;
// Fix index if it's out of bounds
if (index == null || index < 0 || index >= this.pub.length)
index = this.pub.length;
this.pub.splice(index, 0, item);
callback(null, index);
this.emit("change");
};
ListById.prototype.client_add = function (item, index) {
this.byId[item.id] = item;
this.pub.splice(index, 0, item);
};
ListById.prototype.move = function (id, index, callback) {
var item = this.byId[id];
if (item == null) {
callback("Invalid item id: " + id);
return;
}
if (index == null || index < 0 || index >= this.pub.length)
index = this.pub.length;
var oldIndex = this.pub.indexOf(item);
this.pub.splice(oldIndex, 1);
var actualIndex = index;
if (oldIndex < actualIndex)
actualIndex--;
this.pub.splice(actualIndex, 0, item);
callback(null, index);
this.emit("change");
};
ListById.prototype.client_move = function (id, newIndex) {
var item = this.byId[id];
var oldIndex = this.pub.indexOf(item);
this.pub.splice(oldIndex, 1);
if (oldIndex < newIndex)
newIndex--;
this.pub.splice(newIndex, 0, item);
};
ListById.prototype.remove = function (id, callback) {
var item = this.byId[id];
if (item == null) {
callback("Invalid item id: " + id);
return;
}
var index = this.pub.indexOf(item);
this.pub.splice(index, 1);
delete this.byId[id];
callback(null, index);
this.emit("change");
};
ListById.prototype.client_remove = function (id) {
var item = this.byId[id];
this.pub.splice(this.pub.indexOf(item), 1);
delete this.byId[id];
};
// clear: ->
ListById.prototype.setProperty = function (id, path, value, callback) {
var item = this.byId[id];
if (item == null) {
callback("Invalid item id: " + id);
return;
}
var parts = path.split(".");
var rule = this.schema[parts[0]];
for (var _i = 0, _a = parts.slice(1); _i < _a.length; _i++) {
var part = _a[_i];
rule = rule.properties[part];
if (rule.type === "any")
break;
}
if (rule == null) {
callback("Invalid key: " + path);
return;
}
if (rule.type !== "any") {
var violation = base.getRuleViolation(value, rule);
if (violation != null) {
callback("Invalid value for " + path + ": " + base.formatRuleViolation(violation));
return;
}
}
for (var _b = 0, _c = parts.slice(0, parts.length - 1); _b < _c.length; _b++) {
var part = _c[_b];
item = item[part];
}
item[parts[parts.length - 1]] = value;
callback(null, value);
this.emit("change");
};
ListById.prototype.client_setProperty = function (id, path, value) {
var parts = path.split(".");
var item = this.byId[id];
for (var _i = 0, _a = parts.slice(0, parts.length - 1); _i < _a.length; _i++) {
var part = _a[_i];
item = item[part];
}
item[parts[parts.length - 1]] = value;
};
return ListById;
})(events_1.EventEmitter);
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = ListById;
},{"./index":10,"events":21}],8:[function(require,module,exports){
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Hash_1 = require("./Hash");
var path = require("path");
var fs = require("fs");
var Resource = (function (_super) {
__extends(Resource, _super);
function Resource(id, pub, schema, server) {
_super.call(this, pub, schema);
this.id = id;
this.server = server;
if (server == null)
this.setup();
}
Resource.prototype.init = function (callback) { this.setup(); callback(); };
Resource.prototype.setup = function () { };
Resource.prototype.restore = function () { };
Resource.prototype.load = function (resourcePath) {
var _this = this;
fs.readFile(path.join(resourcePath, "resource.json"), { encoding: "utf8" }, function (err, json) {
if (err != null) {
if (err.code === "ENOENT") {
_this.init(function () { _this._onLoaded(resourcePath, _this.pub, true); });
return;
}
throw err;
}
var pub = JSON.parse(json);
_this._onLoaded(resourcePath, pub, false);
});
};
Resource.prototype._onLoaded = function (resourcePath, pub, justCreated) {
var _this = this;
if (justCreated) {
this.pub = pub;
fs.mkdir(path.join(resourcePath), function (err) {
_this.save(resourcePath, function (err) {
_this.setup();
_this.emit("load");
});
});
return;
}
this.migrate(resourcePath, pub, function (hasMigrated) {
if (hasMigrated) {
_this.pub = pub;
fs.mkdir(path.join(resourcePath), function (err) {
_this.save(resourcePath, function (err) {
_this.setup();
_this.emit("load");
});
});
}
else {
_this.pub = pub;
_this.setup();
_this.emit("load");
}
});
};
Resource.prototype.unload = function () { this.removeAllListeners(); };
Resource.prototype.migrate = function (resourcePath, pub, callback) { callback(false); };
;
Resource.prototype.save = function (resourcePath, callback) {
var json = JSON.stringify(this.pub, null, 2);
fs.writeFile(path.join(resourcePath, "resource.json"), json, { encoding: "utf8" }, callback);
};
Resource.prototype.publish = function (buildPath, callback) {
var _this = this;
var folderPath = buildPath + "/resources/" + this.id;
fs.mkdir(folderPath, function (err) { _this.save(folderPath, callback); });
};
Resource.prototype.server_setProperty = function (client, path, value, callback) {
this.setProperty(path, value, function (err, actualValue) {
if (err != null) {
callback(err);
return;
}
callback(null, path, actualValue);
});
};
return Resource;
})(Hash_1.default);
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = Resource;
},{"./Hash":6,"fs":20,"path":24}],9:[function(require,module,exports){
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var base = require("./index");
var events_1 = require("events");
var TreeById = (function (_super) {
__extends(TreeById, _super);
function TreeById(pub, schema, nextId) {
var _this = this;
_super.call(this);
this.schema = schema;
this.pub = pub;
this.nextId = nextId;
this.byId = {};
this.parentNodesById = {};
var maxNodeId = -1;
this.walk(function (node, parentNode) {
// NOTE: Legacy stuff from Superpowers 0.4
if (typeof node.id === "number")
node.id = node.id.toString();
maxNodeId = Math.max(maxNodeId, parseInt(node.id, 10));
_this.byId[node.id] = node;
_this.parentNodesById[node.id] = parentNode;
});
if (this.nextId == null)
this.nextId = maxNodeId + 1;
}
TreeById.prototype.walk = function (callback) {
for (var _i = 0, _a = this.pub; _i < _a.length; _i++) {
var node = _a[_i];
this.walkNode(node, null, callback);
}
};
TreeById.prototype.walkNode = function (node, parentNode, callback) {
callback(node, parentNode);
if (node.children != null) {
for (var _i = 0, _a = node.children; _i < _a.length; _i++) {
var child = _a[_i];
this.walkNode(child, node, callback);
}
}
};
TreeById.prototype.getPathFromId = function (id) {
var name = this.byId[id].name;
var parent = this.parentNodesById[id];
while (true) {
if (parent == null)
break;
name = parent.name + "/" + name;
parent = this.parentNodesById[parent.id];
}
return name;
};
TreeById.prototype.add = function (node, parentId, index, callback) {
if (node.id != null && this.schema["id"] == null) {
callback("Found unexpected id key");
return;
}
var siblings = this.pub;
if (parentId != null)
siblings = (this.byId[parentId] != null) ? this.byId[parentId].children : null;
if (siblings == null) {
callback("Invalid parent id: " + parentId);
return;
}
var missingKeys = Object.keys(this.schema);
for (var key in node) {
var value = node[key];
var rule = this.schema[key];
if (rule == null) {
if (key === "id" && value == null)
continue;
callback("Invalid key: " + key);
return;
}
var violation = base.getRuleViolation(value, rule, true);
if (violation != null) {
callback("Invalid value for " + key + ": " + base.formatRuleViolation(violation));
return;
}
missingKeys.splice(missingKeys.indexOf(key), 1);
}
if (missingKeys.length > 0) {
callback("Missing key: " + missingKeys[0]);
return;
}
if (node.id == null) {
node.id = this.nextId.toString();
this.nextId++;
}
this.byId[node.id] = node;
this.parentNodesById[node.id] = this.byId[parentId];
// Fix index if it's out of bounds
if (index == null || index < 0 || index > siblings.length)
index = siblings.length;
siblings.splice(index, 0, node);
callback(null, index);
this.emit("change");
};
TreeById.prototype.client_add = function (node, parentId, index) {
var siblings = this.pub;
if (parentId != null)
siblings = (this.byId[parentId] != null) ? this.byId[parentId].children : null;
siblings.splice(index, 0, node);
this.byId[node.id] = node;
this.parentNodesById[node.id] = this.byId[parentId];
};
TreeById.prototype.move = function (id, parentId, index, callback) {
var node = this.byId[id];
if (node == null) {
callback("Invalid node id: " + id);
return;
}
var parentNode = null;
if (parentId != null) {
parentNode = this.byId[parentId];
if (parentNode == null || parentNode.children == null) {
callback("Invalid parent node id: " + parentId);
return;
}
}
// Adjust insertion index if needed
var siblings = (parentNode != null) ? parentNode.children : this.pub;
if (index == null || index < 0 || index > siblings.length)
index = siblings.length;
var oldSiblings = (this.parentNodesById[id] != null) ? this.parentNodesById[id].children : this.pub;
var oldIndex = oldSiblings.indexOf(node);
oldSiblings.splice(oldIndex, 1);
var actualIndex = index;
if (siblings === oldSiblings && oldIndex < actualIndex)
actualIndex--;
siblings.splice(actualIndex, 0, node);
this.parentNodesById[id] = parentNode;
callback(null, index);
this.emit("change");
};
TreeById.prototype.client_move = function (id, parentId, index) {
var node = this.byId[id];
var parentNode = (parentId != null) ? this.byId[parentId] : null;
var siblings = (parentNode != null) ? this.byId[parentId].children : this.pub;
var oldSiblings = (this.parentNodesById[id] != null) ? this.parentNodesById[id].children : this.pub;
var oldIndex = oldSiblings.indexOf(node);
oldSiblings.splice(oldIndex, 1);
var actualIndex = index;
if (siblings === oldSiblings && oldIndex < actualIndex)
actualIndex--;
siblings.splice(actualIndex, 0, node);
this.parentNodesById[id] = parentNode;
};
TreeById.prototype.remove = function (id, callback) {
var _this = this;
var node = this.byId[id];
if (node == null) {
callback("Invalid node id: " + id);
return;
}
var siblings = (this.parentNodesById[id] != null) ? this.parentNodesById[id].children : this.pub;
siblings.splice(siblings.indexOf(node), 1);
this.walkNode(node, null, function (node, parentNode) {
delete _this.parentNodesById[node.id];
delete _this.byId[node.id];
});
callback(null);
this.emit("change");
};
TreeById.prototype.client_remove = function (id) {
var _this = this;
var node = this.byId[id];
var siblings = (this.parentNodesById[id] != null) ? this.parentNodesById[id].children : this.pub;
siblings.splice(siblings.indexOf(node), 1);
this.walkNode(node, null, function (node, parentNode) {
delete _this.parentNodesById[node.id];
delete _this.byId[node.id];
});
};
// clear() {}
TreeById.prototype.setProperty = function (id, path, value, callback) {
var node = this.byId[id];
if (node == null) {
callback("Invalid node id: " + id);
return;
}
var parts = path.split(".");
var rule = this.schema[parts[0]];
for (var _i = 0, _a = parts.slice(1); _i < _a.length; _i++) {
var part = _a[_i];
rule = rule.properties[part];
if (rule.type === "any")
break;
}
if (rule == null) {
callback("Invalid key: " + path);
return;
}
if (rule.type !== "any") {
var violation = base.getRuleViolation(value, rule);
if (violation != null) {
callback("Invalid value for " + path + ": " + base.formatRuleViolation(violation));
return;
}
}
for (var _b = 0, _c = parts.slice(0, parts.length - 1); _b < _c.length; _b++) {
var part = _c[_b];
node = node[part];
}
node[parts[parts.length - 1]] = value;
callback(null, value);
this.emit("change");
};
TreeById.prototype.client_setProperty = function (id, path, value) {
var parts = path.split(".");
var node = this.byId[id];
for (var _i = 0, _a = parts.slice(0, parts.length - 1); _i < _a.length; _i++) {
var part = _a[_i];
node = node[part];
}
node[parts[parts.length - 1]] = value;
};
return TreeById;
})(events_1.EventEmitter);
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = TreeById;
},{"./index":10,"events":21}],10:[function(require,module,exports){
/* tslint:disable:no-unused-variable */
var Hash_1 = require("./Hash");
exports.Hash = Hash_1.default;
var ListById_1 = require("./ListById");
exports.ListById = ListById_1.default;
var TreeById_1 = require("./TreeById");
exports.TreeById = TreeById_1.default;
var Dictionary_1 = require("./Dictionary");
exports.Dictionary = Dictionary_1.default;
var Asset_1 = require("./Asset");
exports.Asset = Asset_1.default;
var Resource_1 = require("./Resource");
exports.Resource = Resource_1.default;
/* tslint:enable:no-unused-variable */
function getRuleViolation(value, rule, create) {
if (create === void 0) { create = false; }
if (!create && !rule.mutable)
return { message: "Immutable" };
var optional = rule.type[rule.type.length - 1] === "?";
if (optional && value == null)
return null;
var ruleType;
if (optional)
ruleType = rule.type.slice(0, rule.type.length - 1);
else
ruleType = rule.type;
switch (ruleType) {
case "boolean":
{
if (typeof value !== "boolean")
return { message: "Expected boolean" };
}
break;
case "number":
case "integer":
{
if (typeof value !== "number")
return { message: "Expected " + ruleType };
if (ruleType === "integer" && (value % 1) !== 0)
return { message: "Expected integer" };
if (rule.min != null && value < rule.min)
return { message: "Value (" + value + ") is less than minimum value (" + rule.min + ")" };
if (rule.minExcluded != null && value <= rule.minExcluded)
return { message: "Value (" + value + ") is less than minimum value (" + rule.min + ")" };
if (rule.max != null && value > rule.max)
return { message: "Value (" + value + ") is greater than maximum value (" + rule.max + ")" };
if (rule.maxExcluded != null && value >= rule.maxExcluded)
return { message: "Value (" + value + ") is greater than maximum value (" + rule.max + ")" };
}
break;
case "string":
{
if (typeof value !== "string")
return { message: "Expected string" };
if (rule.length != null && value.length !== rule.length)
return { message: "String should have length of " + rule.length + ", got " + value.length };
if (rule.minLength != null && value.length < rule.minLength)
return { message: "String length (" + value.length + ") is less than minimum length (" + rule.minLength + ")" };
if (rule.maxLength != null && value.length > rule.maxLength)
return { message: "String length (" + value.length + ") is greater than maximum length (" + rule.maxLength + ")" };
}
break;
case "enum":
{
if (typeof value !== "string")
return { message: "Expected string for enum" };
var items = rule.items;
if (items.indexOf(value) === -1)
return { message: "Invalid enum value: " + value };
}
break;
case "hash":
{
if (value == null || typeof value !== "object")
return { message: "Expected hash" };
var ruleProperties = (rule.properties != null) ? rule.properties : {};
var missingKeys = Object.keys(ruleProperties);
for (var key in value) {
var propertyValue = value[key];
var propertyRule = ruleProperties[key];
if (propertyRule == null) {
if (rule.values == null)
return { message: "Unexpected hash key: " + key, path: key };
if (rule.keys != null) {
if (rule.keys.length != null && key.length !== rule.keys.length)
return { message: "Key should have length of " + rule.keys.length + ", got " + key.length, path: key };
if (rule.keys.minLength != null && key.length < rule.keys.minLength)
return { message: "Key length (" + key.length + ") is less than minimum length (" + rule.keys.minLength + ")", path: key };
if (rule.keys.maxLength != null && key.length > rule.keys.maxLength)
return { message: "Key length (" + key.length + ") is greater than maximum length (" + rule.keys.maxLength + ")", path: key };
}
var violation = getRuleViolation(propertyValue, rule.values, true);
if (violation != null) {
var violationPath = (violation.path != null) ? key + "." + violation.path : key;
return { message: violation.message, path: violationPath };
}
}
else {
var violation = getRuleViolation(propertyValue, propertyRule, true);
if (violation != null) {
var violationPath = (violation.path != null) ? key + "." + violation.path : key;
return { message: violation.message, path: violationPath };
}
missingKeys.splice(missingKeys.indexOf(key), 1);
}
}
// Ignore optional keys
var actualMissingKeys = [];
for (var _i = 0; _i < missingKeys.length; _i++) {
var missingKey = missingKeys[_i];
var missingKeyRuleType = ruleProperties[missingKey].type;
if (missingKeyRuleType[missingKeyRuleType.length - 1] !== "?")
actualMissingKeys.push(missingKey);
}
if (actualMissingKeys.length > 0)
return { message: "Missing hash keys: " + actualMissingKeys.join(", ") };
}
break;
case "array":
{
if (!Array.isArray(value))
return { message: "Expected array" };
if (rule.length != null && value.length !== rule.length)
return { message: "Array should have length of " + rule.length + ", got " + value.length };
if (rule.minLength != null && value.length < rule.minLength)
return { message: "Array length (" + value.length + ") is less than minimum length (" + rule.minLength + ")" };
if (rule.maxLength != null && value.length > rule.maxLength)
return { message: "Array length (" + value.length + ") is greater than maximum length (" + rule.maxLength + ")" };
if (rule.items != null) {
for (var index = 0; index < value.length; index++) {
var item = value[index];
var violation = getRuleViolation(item, rule.items, true);
if (violation != null) {
var violationPath = (violation.path != null) ? "[" + index + "]." + violation.path : "[" + index + "]";
return { message: violation.message, path: violationPath };
}
}
}
}
break;
case "any":
{
}
break;
default: {
console.warn("getRuleViolation - Unhandled rule type: " + ruleType);
}
}
return null;
}
exports.getRuleViolation = getRuleViolation;
function formatRuleViolation(violation) {
if (violation == null)
return "No error";
var text = violation.message;
if (violation.path != null)
text += " at " + violation.path;
return text;
}
exports.formatRuleViolation = formatRuleViolation;
},{"./Asset":4,"./Dictionary":5,"./Hash":6,"./ListById":7,"./Resource":8,"./TreeById":9}],11:[function(require,module,exports){
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var SupData = require("./index");
var Entries = (function (_super) {
__extends(Entries, _super);
function Entries(pub, server) {
var _this = this;
_super.call(this, pub, Entries.schema);
this.server = server;
this.badgesByEntryId = {};
this.dependenciesByAssetId = {};
this.walk(function (node, parentNode) {
if (node.type == null)
return;
if (node.badges == null)
node.badges = [];
_this.badgesByEntryId[node.id] = new SupData.Badges(node.badges);
if (node.dependentAssetIds == null)
node.dependentAssetIds = [];
});
}
Entries.prototype.add = function (node, parentId, index, callback) {
var _this = this;