@arcote.tech/arc-host 0.1.8 → 0.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/host.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +104 -3196
- package/dist/index.js.map +89 -0
- package/dist/postgresAdapter.d.ts +3 -0
- package/dist/postgresAdapter.d.ts.map +1 -0
- package/host.ts +21 -22
- package/index.ts +2 -0
- package/package.json +7 -4
- package/postgresAdapter.ts +50 -0
package/dist/index.js
CHANGED
|
@@ -554,8 +554,8 @@ var require_sign_stream = __commonJS((exports, module) => {
|
|
|
554
554
|
var Stream = __require("node:stream");
|
|
555
555
|
var toString = require_tostring();
|
|
556
556
|
var util = __require("node:util");
|
|
557
|
-
function base64url(
|
|
558
|
-
return Buffer2.from(
|
|
557
|
+
function base64url(string, encoding) {
|
|
558
|
+
return Buffer2.from(string, encoding).toString("base64").replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
|
|
559
559
|
}
|
|
560
560
|
function jwsSecuredInput(header, payload, encoding) {
|
|
561
561
|
encoding = encoding || "utf8";
|
|
@@ -623,11 +623,11 @@ var require_verify_stream = __commonJS((exports, module) => {
|
|
|
623
623
|
var toString = require_tostring();
|
|
624
624
|
var util = __require("node:util");
|
|
625
625
|
var JWS_REGEX = /^[a-zA-Z0-9\-_]+?\.[a-zA-Z0-9\-_]+?\.([a-zA-Z0-9\-_]+)?$/;
|
|
626
|
-
function
|
|
626
|
+
function isObject(thing) {
|
|
627
627
|
return Object.prototype.toString.call(thing) === "[object Object]";
|
|
628
628
|
}
|
|
629
629
|
function safeJsonParse(thing) {
|
|
630
|
-
if (
|
|
630
|
+
if (isObject(thing))
|
|
631
631
|
return thing;
|
|
632
632
|
try {
|
|
633
633
|
return JSON.parse(thing);
|
|
@@ -650,8 +650,8 @@ var require_verify_stream = __commonJS((exports, module) => {
|
|
|
650
650
|
var payload = jwsSig.split(".")[1];
|
|
651
651
|
return Buffer2.from(payload, "base64").toString(encoding);
|
|
652
652
|
}
|
|
653
|
-
function isValidJws(
|
|
654
|
-
return JWS_REGEX.test(
|
|
653
|
+
function isValidJws(string) {
|
|
654
|
+
return JWS_REGEX.test(string) && !!headerFromJWS(string);
|
|
655
655
|
}
|
|
656
656
|
function jwsVerify(jwsSig, algorithm, secretOrKey) {
|
|
657
657
|
if (!algorithm) {
|
|
@@ -1155,14 +1155,14 @@ var require_semver = __commonJS((exports, module) => {
|
|
|
1155
1155
|
if (!m[4]) {
|
|
1156
1156
|
this.prerelease = [];
|
|
1157
1157
|
} else {
|
|
1158
|
-
this.prerelease = m[4].split(".").map((
|
|
1159
|
-
if (/^[0-9]+$/.test(
|
|
1160
|
-
const num = +
|
|
1158
|
+
this.prerelease = m[4].split(".").map((id) => {
|
|
1159
|
+
if (/^[0-9]+$/.test(id)) {
|
|
1160
|
+
const num = +id;
|
|
1161
1161
|
if (num >= 0 && num < MAX_SAFE_INTEGER) {
|
|
1162
1162
|
return num;
|
|
1163
1163
|
}
|
|
1164
1164
|
}
|
|
1165
|
-
return
|
|
1165
|
+
return id;
|
|
1166
1166
|
});
|
|
1167
1167
|
}
|
|
1168
1168
|
this.build = m[5] ? m[5].split(".") : [];
|
|
@@ -1883,7 +1883,7 @@ var require_range = __commonJS((exports, module) => {
|
|
|
1883
1883
|
debug("stars", comp);
|
|
1884
1884
|
return comp;
|
|
1885
1885
|
};
|
|
1886
|
-
var isX = (
|
|
1886
|
+
var isX = (id) => !id || id.toLowerCase() === "x" || id === "*";
|
|
1887
1887
|
var replaceTildes = (comp, options) => {
|
|
1888
1888
|
return comp.trim().split(/\s+/).map((c) => replaceTilde(c, options)).join(" ");
|
|
1889
1889
|
};
|
|
@@ -2049,20 +2049,20 @@ var require_range = __commonJS((exports, module) => {
|
|
|
2049
2049
|
}
|
|
2050
2050
|
return `${from} ${to}`.trim();
|
|
2051
2051
|
};
|
|
2052
|
-
var testSet = (
|
|
2053
|
-
for (let i = 0;i <
|
|
2054
|
-
if (!
|
|
2052
|
+
var testSet = (set, version, options) => {
|
|
2053
|
+
for (let i = 0;i < set.length; i++) {
|
|
2054
|
+
if (!set[i].test(version)) {
|
|
2055
2055
|
return false;
|
|
2056
2056
|
}
|
|
2057
2057
|
}
|
|
2058
2058
|
if (version.prerelease.length && !options.includePrerelease) {
|
|
2059
|
-
for (let i = 0;i <
|
|
2060
|
-
debug(
|
|
2061
|
-
if (
|
|
2059
|
+
for (let i = 0;i < set.length; i++) {
|
|
2060
|
+
debug(set[i].semver);
|
|
2061
|
+
if (set[i].semver === Comparator.ANY) {
|
|
2062
2062
|
continue;
|
|
2063
2063
|
}
|
|
2064
|
-
if (
|
|
2065
|
-
const allowed =
|
|
2064
|
+
if (set[i].semver.prerelease.length > 0) {
|
|
2065
|
+
const allowed = set[i].semver;
|
|
2066
2066
|
if (allowed.major === version.major && allowed.minor === version.minor && allowed.patch === version.patch) {
|
|
2067
2067
|
return true;
|
|
2068
2068
|
}
|
|
@@ -2421,7 +2421,7 @@ var require_simplify = __commonJS((exports, module) => {
|
|
|
2421
2421
|
var satisfies = require_satisfies();
|
|
2422
2422
|
var compare = require_compare();
|
|
2423
2423
|
module.exports = (versions, range, options) => {
|
|
2424
|
-
const
|
|
2424
|
+
const set = [];
|
|
2425
2425
|
let first = null;
|
|
2426
2426
|
let prev = null;
|
|
2427
2427
|
const v = versions.sort((a, b) => compare(a, b, options));
|
|
@@ -2434,17 +2434,17 @@ var require_simplify = __commonJS((exports, module) => {
|
|
|
2434
2434
|
}
|
|
2435
2435
|
} else {
|
|
2436
2436
|
if (prev) {
|
|
2437
|
-
|
|
2437
|
+
set.push([first, prev]);
|
|
2438
2438
|
}
|
|
2439
2439
|
prev = null;
|
|
2440
2440
|
first = null;
|
|
2441
2441
|
}
|
|
2442
2442
|
}
|
|
2443
2443
|
if (first) {
|
|
2444
|
-
|
|
2444
|
+
set.push([first, null]);
|
|
2445
2445
|
}
|
|
2446
2446
|
const ranges = [];
|
|
2447
|
-
for (const [min, max] of
|
|
2447
|
+
for (const [min, max] of set) {
|
|
2448
2448
|
if (min === max) {
|
|
2449
2449
|
ranges.push(min);
|
|
2450
2450
|
} else if (!max && min === v[0]) {
|
|
@@ -3121,13 +3121,13 @@ var require_lodash = __commonJS((exports, module) => {
|
|
|
3121
3121
|
return isObjectLike(value) && isArrayLike(value);
|
|
3122
3122
|
}
|
|
3123
3123
|
function isFunction(value) {
|
|
3124
|
-
var tag =
|
|
3124
|
+
var tag = isObject(value) ? objectToString.call(value) : "";
|
|
3125
3125
|
return tag == funcTag || tag == genTag;
|
|
3126
3126
|
}
|
|
3127
3127
|
function isLength(value) {
|
|
3128
3128
|
return typeof value == "number" && value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
|
|
3129
3129
|
}
|
|
3130
|
-
function
|
|
3130
|
+
function isObject(value) {
|
|
3131
3131
|
var type = typeof value;
|
|
3132
3132
|
return !!value && (type == "object" || type == "function");
|
|
3133
3133
|
}
|
|
@@ -3162,9 +3162,9 @@ var require_lodash = __commonJS((exports, module) => {
|
|
|
3162
3162
|
if (isSymbol(value)) {
|
|
3163
3163
|
return NAN;
|
|
3164
3164
|
}
|
|
3165
|
-
if (
|
|
3165
|
+
if (isObject(value)) {
|
|
3166
3166
|
var other = typeof value.valueOf == "function" ? value.valueOf() : value;
|
|
3167
|
-
value =
|
|
3167
|
+
value = isObject(other) ? other + "" : other;
|
|
3168
3168
|
}
|
|
3169
3169
|
if (typeof value != "string") {
|
|
3170
3170
|
return value === 0 ? value : +value;
|
|
@@ -3212,7 +3212,7 @@ var require_lodash3 = __commonJS((exports, module) => {
|
|
|
3212
3212
|
function isInteger(value) {
|
|
3213
3213
|
return typeof value == "number" && value == toInteger(value);
|
|
3214
3214
|
}
|
|
3215
|
-
function
|
|
3215
|
+
function isObject(value) {
|
|
3216
3216
|
var type = typeof value;
|
|
3217
3217
|
return !!value && (type == "object" || type == "function");
|
|
3218
3218
|
}
|
|
@@ -3244,9 +3244,9 @@ var require_lodash3 = __commonJS((exports, module) => {
|
|
|
3244
3244
|
if (isSymbol(value)) {
|
|
3245
3245
|
return NAN;
|
|
3246
3246
|
}
|
|
3247
|
-
if (
|
|
3247
|
+
if (isObject(value)) {
|
|
3248
3248
|
var other = typeof value.valueOf == "function" ? value.valueOf() : value;
|
|
3249
|
-
value =
|
|
3249
|
+
value = isObject(other) ? other + "" : other;
|
|
3250
3250
|
}
|
|
3251
3251
|
if (typeof value != "string") {
|
|
3252
3252
|
return value === 0 ? value : +value;
|
|
@@ -3361,7 +3361,7 @@ var require_lodash7 = __commonJS((exports, module) => {
|
|
|
3361
3361
|
function once(func) {
|
|
3362
3362
|
return before(2, func);
|
|
3363
3363
|
}
|
|
3364
|
-
function
|
|
3364
|
+
function isObject(value) {
|
|
3365
3365
|
var type = typeof value;
|
|
3366
3366
|
return !!value && (type == "object" || type == "function");
|
|
3367
3367
|
}
|
|
@@ -3393,9 +3393,9 @@ var require_lodash7 = __commonJS((exports, module) => {
|
|
|
3393
3393
|
if (isSymbol(value)) {
|
|
3394
3394
|
return NAN;
|
|
3395
3395
|
}
|
|
3396
|
-
if (
|
|
3396
|
+
if (isObject(value)) {
|
|
3397
3397
|
var other = typeof value.valueOf == "function" ? value.valueOf() : value;
|
|
3398
|
-
value =
|
|
3398
|
+
value = isObject(other) ? other + "" : other;
|
|
3399
3399
|
}
|
|
3400
3400
|
if (typeof value != "string") {
|
|
3401
3401
|
return value === 0 ? value : +value;
|
|
@@ -3641,3154 +3641,13 @@ var require_jsonwebtoken = __commonJS((exports, module) => {
|
|
|
3641
3641
|
};
|
|
3642
3642
|
});
|
|
3643
3643
|
|
|
3644
|
-
//
|
|
3645
|
-
|
|
3646
|
-
|
|
3647
|
-
|
|
3648
|
-
|
|
3649
|
-
|
|
3650
|
-
|
|
3651
|
-
var RAW_RETURN_SYMBOL = Symbol("__MUTATIVE_RAW_RETURN_SYMBOL__");
|
|
3652
|
-
var iteratorSymbol = Symbol.iterator;
|
|
3653
|
-
var dataTypes = {
|
|
3654
|
-
mutable: "mutable",
|
|
3655
|
-
immutable: "immutable"
|
|
3656
|
-
};
|
|
3657
|
-
var internal = {};
|
|
3658
|
-
function latest(proxyDraft) {
|
|
3659
|
-
var _a;
|
|
3660
|
-
return (_a = proxyDraft.copy) !== null && _a !== undefined ? _a : proxyDraft.original;
|
|
3661
|
-
}
|
|
3662
|
-
function isDraft(target) {
|
|
3663
|
-
return !!getProxyDraft(target);
|
|
3664
|
-
}
|
|
3665
|
-
function getProxyDraft(value) {
|
|
3666
|
-
if (typeof value !== "object")
|
|
3667
|
-
return null;
|
|
3668
|
-
return value === null || value === undefined ? undefined : value[PROXY_DRAFT];
|
|
3669
|
-
}
|
|
3670
|
-
function getValue(value) {
|
|
3671
|
-
var _a;
|
|
3672
|
-
const proxyDraft = getProxyDraft(value);
|
|
3673
|
-
return proxyDraft ? (_a = proxyDraft.copy) !== null && _a !== undefined ? _a : proxyDraft.original : value;
|
|
3674
|
-
}
|
|
3675
|
-
function isDraftable(value, options) {
|
|
3676
|
-
if (!value || typeof value !== "object")
|
|
3677
|
-
return false;
|
|
3678
|
-
let markResult;
|
|
3679
|
-
return Object.getPrototypeOf(value) === Object.prototype || Array.isArray(value) || value instanceof Map || value instanceof Set || !!(options === null || options === undefined ? undefined : options.mark) && ((markResult = options.mark(value, dataTypes)) === dataTypes.immutable || typeof markResult === "function");
|
|
3680
|
-
}
|
|
3681
|
-
function getPath(target, path = []) {
|
|
3682
|
-
if (Object.hasOwnProperty.call(target, "key")) {
|
|
3683
|
-
const proxyDraft = getProxyDraft(get(target.parent.copy, target.key));
|
|
3684
|
-
if (proxyDraft !== null && (proxyDraft === null || proxyDraft === undefined ? undefined : proxyDraft.original) !== target.original) {
|
|
3685
|
-
return null;
|
|
3686
|
-
}
|
|
3687
|
-
path.push(target.parent.type === 3 ? Array.from(target.parent.setMap.keys()).indexOf(target.key) : target.key);
|
|
3688
|
-
}
|
|
3689
|
-
if (target.parent) {
|
|
3690
|
-
return getPath(target.parent, path);
|
|
3691
|
-
}
|
|
3692
|
-
return path.reverse();
|
|
3693
|
-
}
|
|
3694
|
-
function getType(target) {
|
|
3695
|
-
if (Array.isArray(target))
|
|
3696
|
-
return 1;
|
|
3697
|
-
if (target instanceof Map)
|
|
3698
|
-
return 2;
|
|
3699
|
-
if (target instanceof Set)
|
|
3700
|
-
return 3;
|
|
3701
|
-
return 0;
|
|
3702
|
-
}
|
|
3703
|
-
function get(target, key) {
|
|
3704
|
-
return getType(target) === 2 ? target.get(key) : target[key];
|
|
3705
|
-
}
|
|
3706
|
-
function set(target, key, value) {
|
|
3707
|
-
const type = getType(target);
|
|
3708
|
-
if (type === 2) {
|
|
3709
|
-
target.set(key, value);
|
|
3710
|
-
} else {
|
|
3711
|
-
target[key] = value;
|
|
3712
|
-
}
|
|
3713
|
-
}
|
|
3714
|
-
function peek(target, key) {
|
|
3715
|
-
const state = getProxyDraft(target);
|
|
3716
|
-
const source = state ? latest(state) : target;
|
|
3717
|
-
return source[key];
|
|
3718
|
-
}
|
|
3719
|
-
function isEqual(x, y) {
|
|
3720
|
-
if (x === y) {
|
|
3721
|
-
return x !== 0 || 1 / x === 1 / y;
|
|
3722
|
-
} else {
|
|
3723
|
-
return x !== x && y !== y;
|
|
3724
|
-
}
|
|
3725
|
-
}
|
|
3726
|
-
function revokeProxy(proxyDraft) {
|
|
3727
|
-
if (!proxyDraft)
|
|
3728
|
-
return;
|
|
3729
|
-
while (proxyDraft.finalities.revoke.length > 0) {
|
|
3730
|
-
const revoke = proxyDraft.finalities.revoke.pop();
|
|
3731
|
-
revoke();
|
|
3732
|
-
}
|
|
3733
|
-
}
|
|
3734
|
-
function escapePath(path, pathAsArray) {
|
|
3735
|
-
return pathAsArray ? path : [""].concat(path).map((_item) => {
|
|
3736
|
-
const item = `${_item}`;
|
|
3737
|
-
if (item.indexOf("/") === -1 && item.indexOf("~") === -1)
|
|
3738
|
-
return item;
|
|
3739
|
-
return item.replace(/~/g, "~0").replace(/\//g, "~1");
|
|
3740
|
-
}).join("/");
|
|
3741
|
-
}
|
|
3742
|
-
function unescapePath(path) {
|
|
3743
|
-
if (Array.isArray(path))
|
|
3744
|
-
return path;
|
|
3745
|
-
return path.split("/").map((_item) => _item.replace(/~1/g, "/").replace(/~0/g, "~")).slice(1);
|
|
3746
|
-
}
|
|
3747
|
-
function strictCopy(target) {
|
|
3748
|
-
const copy = Object.create(Object.getPrototypeOf(target));
|
|
3749
|
-
Reflect.ownKeys(target).forEach((key) => {
|
|
3750
|
-
let desc = Reflect.getOwnPropertyDescriptor(target, key);
|
|
3751
|
-
if (desc.enumerable && desc.configurable && desc.writable) {
|
|
3752
|
-
copy[key] = target[key];
|
|
3753
|
-
return;
|
|
3754
|
-
}
|
|
3755
|
-
if (!desc.writable) {
|
|
3756
|
-
desc.writable = true;
|
|
3757
|
-
desc.configurable = true;
|
|
3758
|
-
}
|
|
3759
|
-
if (desc.get || desc.set)
|
|
3760
|
-
desc = {
|
|
3761
|
-
configurable: true,
|
|
3762
|
-
writable: true,
|
|
3763
|
-
enumerable: desc.enumerable,
|
|
3764
|
-
value: target[key]
|
|
3765
|
-
};
|
|
3766
|
-
Reflect.defineProperty(copy, key, desc);
|
|
3767
|
-
});
|
|
3768
|
-
return copy;
|
|
3769
|
-
}
|
|
3770
|
-
var propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
3771
|
-
function shallowCopy(original, options) {
|
|
3772
|
-
let markResult;
|
|
3773
|
-
if (Array.isArray(original)) {
|
|
3774
|
-
return Array.prototype.concat.call(original);
|
|
3775
|
-
} else if (original instanceof Set) {
|
|
3776
|
-
return new Set(original.values());
|
|
3777
|
-
} else if (original instanceof Map) {
|
|
3778
|
-
return new Map(original);
|
|
3779
|
-
} else if ((options === null || options === undefined ? undefined : options.mark) && (markResult = options.mark(original, dataTypes), markResult !== undefined) && markResult !== dataTypes.mutable) {
|
|
3780
|
-
if (markResult === dataTypes.immutable) {
|
|
3781
|
-
return strictCopy(original);
|
|
3782
|
-
} else if (typeof markResult === "function") {
|
|
3783
|
-
if (options.enablePatches || options.enableAutoFreeze) {
|
|
3784
|
-
throw new Error(`You can't use mark and patches or auto freeze together.`);
|
|
3785
|
-
}
|
|
3786
|
-
return markResult();
|
|
3787
|
-
}
|
|
3788
|
-
throw new Error(`Unsupported mark result: ${markResult}`);
|
|
3789
|
-
} else if (typeof original === "object" && Object.getPrototypeOf(original) === Object.prototype) {
|
|
3790
|
-
const copy = {};
|
|
3791
|
-
Object.keys(original).forEach((key) => {
|
|
3792
|
-
copy[key] = original[key];
|
|
3793
|
-
});
|
|
3794
|
-
Object.getOwnPropertySymbols(original).forEach((key) => {
|
|
3795
|
-
if (propIsEnum.call(original, key)) {
|
|
3796
|
-
copy[key] = original[key];
|
|
3797
|
-
}
|
|
3798
|
-
});
|
|
3799
|
-
return copy;
|
|
3800
|
-
} else {
|
|
3801
|
-
throw new Error(`Please check mark() to ensure that it is a stable marker draftable function.`);
|
|
3802
|
-
}
|
|
3803
|
-
}
|
|
3804
|
-
function ensureShallowCopy(target) {
|
|
3805
|
-
if (target.copy)
|
|
3806
|
-
return;
|
|
3807
|
-
target.copy = shallowCopy(target.original, target.options);
|
|
3808
|
-
}
|
|
3809
|
-
function deepClone(target) {
|
|
3810
|
-
if (!isDraftable(target))
|
|
3811
|
-
return getValue(target);
|
|
3812
|
-
if (Array.isArray(target))
|
|
3813
|
-
return target.map(deepClone);
|
|
3814
|
-
if (target instanceof Map)
|
|
3815
|
-
return new Map(Array.from(target.entries()).map(([k, v]) => [k, deepClone(v)]));
|
|
3816
|
-
if (target instanceof Set)
|
|
3817
|
-
return new Set(Array.from(target).map(deepClone));
|
|
3818
|
-
const copy = Object.create(Object.getPrototypeOf(target));
|
|
3819
|
-
for (const key in target)
|
|
3820
|
-
copy[key] = deepClone(target[key]);
|
|
3821
|
-
return copy;
|
|
3822
|
-
}
|
|
3823
|
-
function cloneIfNeeded(target) {
|
|
3824
|
-
return isDraft(target) ? deepClone(target) : target;
|
|
3825
|
-
}
|
|
3826
|
-
function markChanged(proxyDraft) {
|
|
3827
|
-
var _a;
|
|
3828
|
-
proxyDraft.assignedMap = (_a = proxyDraft.assignedMap) !== null && _a !== undefined ? _a : new Map;
|
|
3829
|
-
if (!proxyDraft.operated) {
|
|
3830
|
-
proxyDraft.operated = true;
|
|
3831
|
-
if (proxyDraft.parent) {
|
|
3832
|
-
markChanged(proxyDraft.parent);
|
|
3833
|
-
}
|
|
3834
|
-
}
|
|
3835
|
-
}
|
|
3836
|
-
function throwFrozenError() {
|
|
3837
|
-
throw new Error("Cannot modify frozen object");
|
|
3838
|
-
}
|
|
3839
|
-
function deepFreeze(target, subKey, updatedValues, stack, keys) {
|
|
3840
|
-
{
|
|
3841
|
-
updatedValues = updatedValues !== null && updatedValues !== undefined ? updatedValues : new WeakMap;
|
|
3842
|
-
stack = stack !== null && stack !== undefined ? stack : [];
|
|
3843
|
-
keys = keys !== null && keys !== undefined ? keys : [];
|
|
3844
|
-
const value = updatedValues.has(target) ? updatedValues.get(target) : target;
|
|
3845
|
-
if (stack.length > 0) {
|
|
3846
|
-
const index = stack.indexOf(value);
|
|
3847
|
-
if (value && typeof value === "object" && index !== -1) {
|
|
3848
|
-
if (stack[0] === value) {
|
|
3849
|
-
throw new Error(`Forbids circular reference`);
|
|
3850
|
-
}
|
|
3851
|
-
throw new Error(`Forbids circular reference: ~/${keys.slice(0, index).map((key, index2) => {
|
|
3852
|
-
if (typeof key === "symbol")
|
|
3853
|
-
return `[${key.toString()}]`;
|
|
3854
|
-
const parent = stack[index2];
|
|
3855
|
-
if (typeof key === "object" && (parent instanceof Map || parent instanceof Set))
|
|
3856
|
-
return Array.from(parent.keys()).indexOf(key);
|
|
3857
|
-
return key;
|
|
3858
|
-
}).join("/")}`);
|
|
3859
|
-
}
|
|
3860
|
-
stack.push(value);
|
|
3861
|
-
keys.push(subKey);
|
|
3862
|
-
} else {
|
|
3863
|
-
stack.push(value);
|
|
3864
|
-
}
|
|
3865
|
-
}
|
|
3866
|
-
if (Object.isFrozen(target) || isDraft(target)) {
|
|
3867
|
-
{
|
|
3868
|
-
stack.pop();
|
|
3869
|
-
keys.pop();
|
|
3870
|
-
}
|
|
3871
|
-
return;
|
|
3872
|
-
}
|
|
3873
|
-
const type = getType(target);
|
|
3874
|
-
switch (type) {
|
|
3875
|
-
case 2:
|
|
3876
|
-
for (const [key, value] of target) {
|
|
3877
|
-
deepFreeze(key, key, updatedValues, stack, keys);
|
|
3878
|
-
deepFreeze(value, key, updatedValues, stack, keys);
|
|
3879
|
-
}
|
|
3880
|
-
target.set = target.clear = target.delete = throwFrozenError;
|
|
3881
|
-
break;
|
|
3882
|
-
case 3:
|
|
3883
|
-
for (const value of target) {
|
|
3884
|
-
deepFreeze(value, value, updatedValues, stack, keys);
|
|
3885
|
-
}
|
|
3886
|
-
target.add = target.clear = target.delete = throwFrozenError;
|
|
3887
|
-
break;
|
|
3888
|
-
case 1:
|
|
3889
|
-
Object.freeze(target);
|
|
3890
|
-
let index = 0;
|
|
3891
|
-
for (const value of target) {
|
|
3892
|
-
deepFreeze(value, index, updatedValues, stack, keys);
|
|
3893
|
-
index += 1;
|
|
3894
|
-
}
|
|
3895
|
-
break;
|
|
3896
|
-
default:
|
|
3897
|
-
Object.freeze(target);
|
|
3898
|
-
Object.keys(target).forEach((name) => {
|
|
3899
|
-
const value = target[name];
|
|
3900
|
-
deepFreeze(value, name, updatedValues, stack, keys);
|
|
3901
|
-
});
|
|
3902
|
-
}
|
|
3903
|
-
{
|
|
3904
|
-
stack.pop();
|
|
3905
|
-
keys.pop();
|
|
3906
|
-
}
|
|
3907
|
-
}
|
|
3908
|
-
function has(target, key) {
|
|
3909
|
-
return target instanceof Map ? target.has(key) : Object.prototype.hasOwnProperty.call(target, key);
|
|
3910
|
-
}
|
|
3911
|
-
function getDescriptor(target, key) {
|
|
3912
|
-
if (key in target) {
|
|
3913
|
-
let prototype = Reflect.getPrototypeOf(target);
|
|
3914
|
-
while (prototype) {
|
|
3915
|
-
const descriptor = Reflect.getOwnPropertyDescriptor(prototype, key);
|
|
3916
|
-
if (descriptor)
|
|
3917
|
-
return descriptor;
|
|
3918
|
-
prototype = Reflect.getPrototypeOf(prototype);
|
|
3919
|
-
}
|
|
3920
|
-
}
|
|
3921
|
-
return;
|
|
3922
|
-
}
|
|
3923
|
-
function forEach(target, iter) {
|
|
3924
|
-
const type = getType(target);
|
|
3925
|
-
if (type === 0) {
|
|
3926
|
-
Reflect.ownKeys(target).forEach((key) => {
|
|
3927
|
-
iter(key, target[key], target);
|
|
3928
|
-
});
|
|
3929
|
-
} else if (type === 1) {
|
|
3930
|
-
let index = 0;
|
|
3931
|
-
for (const entry of target) {
|
|
3932
|
-
iter(index, entry, target);
|
|
3933
|
-
index += 1;
|
|
3934
|
-
}
|
|
3935
|
-
} else {
|
|
3936
|
-
target.forEach((entry, index) => iter(index, entry, target));
|
|
3937
|
-
}
|
|
3938
|
-
}
|
|
3939
|
-
function handleValue(target, handledSet, options) {
|
|
3940
|
-
if (isDraft(target) || !isDraftable(target, options) || handledSet.has(target) || Object.isFrozen(target))
|
|
3941
|
-
return;
|
|
3942
|
-
const isSet = target instanceof Set;
|
|
3943
|
-
const setMap = isSet ? new Map : undefined;
|
|
3944
|
-
handledSet.add(target);
|
|
3945
|
-
forEach(target, (key, value) => {
|
|
3946
|
-
var _a;
|
|
3947
|
-
if (isDraft(value)) {
|
|
3948
|
-
const proxyDraft = getProxyDraft(value);
|
|
3949
|
-
ensureShallowCopy(proxyDraft);
|
|
3950
|
-
const updatedValue = ((_a = proxyDraft.assignedMap) === null || _a === undefined ? undefined : _a.size) || proxyDraft.operated ? proxyDraft.copy : proxyDraft.original;
|
|
3951
|
-
set(isSet ? setMap : target, key, updatedValue);
|
|
3952
|
-
} else {
|
|
3953
|
-
handleValue(value, handledSet, options);
|
|
3954
|
-
}
|
|
3955
|
-
});
|
|
3956
|
-
if (setMap) {
|
|
3957
|
-
const set2 = target;
|
|
3958
|
-
const values = Array.from(set2);
|
|
3959
|
-
set2.clear();
|
|
3960
|
-
values.forEach((value) => {
|
|
3961
|
-
set2.add(setMap.has(value) ? setMap.get(value) : value);
|
|
3962
|
-
});
|
|
3963
|
-
}
|
|
3964
|
-
}
|
|
3965
|
-
function finalizeAssigned(proxyDraft, key) {
|
|
3966
|
-
const copy = proxyDraft.type === 3 ? proxyDraft.setMap : proxyDraft.copy;
|
|
3967
|
-
if (proxyDraft.finalities.revoke.length > 1 && proxyDraft.assignedMap.get(key) && copy) {
|
|
3968
|
-
handleValue(get(copy, key), proxyDraft.finalities.handledSet, proxyDraft.options);
|
|
3969
|
-
}
|
|
3970
|
-
}
|
|
3971
|
-
function finalizeSetValue(target) {
|
|
3972
|
-
if (target.type === 3 && target.copy) {
|
|
3973
|
-
target.copy.clear();
|
|
3974
|
-
target.setMap.forEach((value) => {
|
|
3975
|
-
target.copy.add(getValue(value));
|
|
3976
|
-
});
|
|
3977
|
-
}
|
|
3978
|
-
}
|
|
3979
|
-
function finalizePatches(target, generatePatches, patches, inversePatches) {
|
|
3980
|
-
const shouldFinalize = target.operated && target.assignedMap && target.assignedMap.size > 0 && !target.finalized;
|
|
3981
|
-
if (shouldFinalize) {
|
|
3982
|
-
if (patches && inversePatches) {
|
|
3983
|
-
const basePath = getPath(target);
|
|
3984
|
-
if (basePath) {
|
|
3985
|
-
generatePatches(target, basePath, patches, inversePatches);
|
|
3986
|
-
}
|
|
3987
|
-
}
|
|
3988
|
-
target.finalized = true;
|
|
3989
|
-
}
|
|
3990
|
-
}
|
|
3991
|
-
function markFinalization(target, key, value, generatePatches) {
|
|
3992
|
-
const proxyDraft = getProxyDraft(value);
|
|
3993
|
-
if (proxyDraft) {
|
|
3994
|
-
if (!proxyDraft.callbacks) {
|
|
3995
|
-
proxyDraft.callbacks = [];
|
|
3996
|
-
}
|
|
3997
|
-
proxyDraft.callbacks.push((patches, inversePatches) => {
|
|
3998
|
-
var _a;
|
|
3999
|
-
const copy = target.type === 3 ? target.setMap : target.copy;
|
|
4000
|
-
if (isEqual(get(copy, key), value)) {
|
|
4001
|
-
let updatedValue = proxyDraft.original;
|
|
4002
|
-
if (proxyDraft.copy) {
|
|
4003
|
-
updatedValue = proxyDraft.copy;
|
|
4004
|
-
}
|
|
4005
|
-
finalizeSetValue(target);
|
|
4006
|
-
finalizePatches(target, generatePatches, patches, inversePatches);
|
|
4007
|
-
if (target.options.enableAutoFreeze) {
|
|
4008
|
-
target.options.updatedValues = (_a = target.options.updatedValues) !== null && _a !== undefined ? _a : new WeakMap;
|
|
4009
|
-
target.options.updatedValues.set(updatedValue, proxyDraft.original);
|
|
4010
|
-
}
|
|
4011
|
-
set(copy, key, updatedValue);
|
|
4012
|
-
}
|
|
4013
|
-
});
|
|
4014
|
-
if (target.options.enableAutoFreeze) {
|
|
4015
|
-
if (proxyDraft.finalities !== target.finalities) {
|
|
4016
|
-
target.options.enableAutoFreeze = false;
|
|
4017
|
-
}
|
|
4018
|
-
}
|
|
4019
|
-
}
|
|
4020
|
-
if (isDraftable(value, target.options)) {
|
|
4021
|
-
target.finalities.draft.push(() => {
|
|
4022
|
-
const copy = target.type === 3 ? target.setMap : target.copy;
|
|
4023
|
-
if (isEqual(get(copy, key), value)) {
|
|
4024
|
-
finalizeAssigned(target, key);
|
|
4025
|
-
}
|
|
4026
|
-
});
|
|
4027
|
-
}
|
|
4028
|
-
}
|
|
4029
|
-
function generateArrayPatches(proxyState, basePath, patches, inversePatches, pathAsArray) {
|
|
4030
|
-
let { original, assignedMap, options } = proxyState;
|
|
4031
|
-
let copy = proxyState.copy;
|
|
4032
|
-
if (copy.length < original.length) {
|
|
4033
|
-
[original, copy] = [copy, original];
|
|
4034
|
-
[patches, inversePatches] = [inversePatches, patches];
|
|
4035
|
-
}
|
|
4036
|
-
for (let index = 0;index < original.length; index += 1) {
|
|
4037
|
-
if (assignedMap.get(index.toString()) && copy[index] !== original[index]) {
|
|
4038
|
-
const _path = basePath.concat([index]);
|
|
4039
|
-
const path = escapePath(_path, pathAsArray);
|
|
4040
|
-
patches.push({
|
|
4041
|
-
op: Operation.Replace,
|
|
4042
|
-
path,
|
|
4043
|
-
value: cloneIfNeeded(copy[index])
|
|
4044
|
-
});
|
|
4045
|
-
inversePatches.push({
|
|
4046
|
-
op: Operation.Replace,
|
|
4047
|
-
path,
|
|
4048
|
-
value: cloneIfNeeded(original[index])
|
|
4049
|
-
});
|
|
4050
|
-
}
|
|
4051
|
-
}
|
|
4052
|
-
for (let index = original.length;index < copy.length; index += 1) {
|
|
4053
|
-
const _path = basePath.concat([index]);
|
|
4054
|
-
const path = escapePath(_path, pathAsArray);
|
|
4055
|
-
patches.push({
|
|
4056
|
-
op: Operation.Add,
|
|
4057
|
-
path,
|
|
4058
|
-
value: cloneIfNeeded(copy[index])
|
|
4059
|
-
});
|
|
4060
|
-
}
|
|
4061
|
-
if (original.length < copy.length) {
|
|
4062
|
-
const { arrayLengthAssignment = true } = options.enablePatches;
|
|
4063
|
-
if (arrayLengthAssignment) {
|
|
4064
|
-
const _path = basePath.concat(["length"]);
|
|
4065
|
-
const path = escapePath(_path, pathAsArray);
|
|
4066
|
-
inversePatches.push({
|
|
4067
|
-
op: Operation.Replace,
|
|
4068
|
-
path,
|
|
4069
|
-
value: original.length
|
|
4070
|
-
});
|
|
4071
|
-
} else {
|
|
4072
|
-
for (let index = copy.length;original.length < index; index -= 1) {
|
|
4073
|
-
const _path = basePath.concat([index - 1]);
|
|
4074
|
-
const path = escapePath(_path, pathAsArray);
|
|
4075
|
-
inversePatches.push({
|
|
4076
|
-
op: Operation.Remove,
|
|
4077
|
-
path
|
|
4078
|
-
});
|
|
4079
|
-
}
|
|
4080
|
-
}
|
|
4081
|
-
}
|
|
4082
|
-
}
|
|
4083
|
-
function generatePatchesFromAssigned({ original, copy, assignedMap }, basePath, patches, inversePatches, pathAsArray) {
|
|
4084
|
-
assignedMap.forEach((assignedValue, key) => {
|
|
4085
|
-
const originalValue = get(original, key);
|
|
4086
|
-
const value = cloneIfNeeded(get(copy, key));
|
|
4087
|
-
const op = !assignedValue ? Operation.Remove : has(original, key) ? Operation.Replace : Operation.Add;
|
|
4088
|
-
if (isEqual(originalValue, value) && op === Operation.Replace)
|
|
4089
|
-
return;
|
|
4090
|
-
const _path = basePath.concat(key);
|
|
4091
|
-
const path = escapePath(_path, pathAsArray);
|
|
4092
|
-
patches.push(op === Operation.Remove ? { op, path } : { op, path, value });
|
|
4093
|
-
inversePatches.push(op === Operation.Add ? { op: Operation.Remove, path } : op === Operation.Remove ? { op: Operation.Add, path, value: originalValue } : { op: Operation.Replace, path, value: originalValue });
|
|
4094
|
-
});
|
|
4095
|
-
}
|
|
4096
|
-
function generateSetPatches({ original, copy }, basePath, patches, inversePatches, pathAsArray) {
|
|
4097
|
-
let index = 0;
|
|
4098
|
-
original.forEach((value) => {
|
|
4099
|
-
if (!copy.has(value)) {
|
|
4100
|
-
const _path = basePath.concat([index]);
|
|
4101
|
-
const path = escapePath(_path, pathAsArray);
|
|
4102
|
-
patches.push({
|
|
4103
|
-
op: Operation.Remove,
|
|
4104
|
-
path,
|
|
4105
|
-
value
|
|
4106
|
-
});
|
|
4107
|
-
inversePatches.unshift({
|
|
4108
|
-
op: Operation.Add,
|
|
4109
|
-
path,
|
|
4110
|
-
value
|
|
4111
|
-
});
|
|
4112
|
-
}
|
|
4113
|
-
index += 1;
|
|
4114
|
-
});
|
|
4115
|
-
index = 0;
|
|
4116
|
-
copy.forEach((value) => {
|
|
4117
|
-
if (!original.has(value)) {
|
|
4118
|
-
const _path = basePath.concat([index]);
|
|
4119
|
-
const path = escapePath(_path, pathAsArray);
|
|
4120
|
-
patches.push({
|
|
4121
|
-
op: Operation.Add,
|
|
4122
|
-
path,
|
|
4123
|
-
value
|
|
4124
|
-
});
|
|
4125
|
-
inversePatches.unshift({
|
|
4126
|
-
op: Operation.Remove,
|
|
4127
|
-
path,
|
|
4128
|
-
value
|
|
4129
|
-
});
|
|
4130
|
-
}
|
|
4131
|
-
index += 1;
|
|
4132
|
-
});
|
|
4133
|
-
}
|
|
4134
|
-
function generatePatches(proxyState, basePath, patches, inversePatches) {
|
|
4135
|
-
const { pathAsArray = true } = proxyState.options.enablePatches;
|
|
4136
|
-
switch (proxyState.type) {
|
|
4137
|
-
case 0:
|
|
4138
|
-
case 2:
|
|
4139
|
-
return generatePatchesFromAssigned(proxyState, basePath, patches, inversePatches, pathAsArray);
|
|
4140
|
-
case 1:
|
|
4141
|
-
return generateArrayPatches(proxyState, basePath, patches, inversePatches, pathAsArray);
|
|
4142
|
-
case 3:
|
|
4143
|
-
return generateSetPatches(proxyState, basePath, patches, inversePatches, pathAsArray);
|
|
4144
|
-
}
|
|
4145
|
-
}
|
|
4146
|
-
var readable = false;
|
|
4147
|
-
var checkReadable = (value, options, ignoreCheckDraftable = false) => {
|
|
4148
|
-
if (typeof value === "object" && value !== null && (!isDraftable(value, options) || ignoreCheckDraftable) && !readable) {
|
|
4149
|
-
throw new Error(`Strict mode: Mutable data cannot be accessed directly, please use 'unsafe(callback)' wrap.`);
|
|
4150
|
-
}
|
|
4151
|
-
};
|
|
4152
|
-
var mapHandler = {
|
|
4153
|
-
get size() {
|
|
4154
|
-
const current = latest(getProxyDraft(this));
|
|
4155
|
-
return current.size;
|
|
4156
|
-
},
|
|
4157
|
-
has(key) {
|
|
4158
|
-
return latest(getProxyDraft(this)).has(key);
|
|
4159
|
-
},
|
|
4160
|
-
set(key, value) {
|
|
4161
|
-
const target = getProxyDraft(this);
|
|
4162
|
-
const source = latest(target);
|
|
4163
|
-
if (!source.has(key) || !isEqual(source.get(key), value)) {
|
|
4164
|
-
ensureShallowCopy(target);
|
|
4165
|
-
markChanged(target);
|
|
4166
|
-
target.assignedMap.set(key, true);
|
|
4167
|
-
target.copy.set(key, value);
|
|
4168
|
-
markFinalization(target, key, value, generatePatches);
|
|
4169
|
-
}
|
|
4170
|
-
return this;
|
|
4171
|
-
},
|
|
4172
|
-
delete(key) {
|
|
4173
|
-
if (!this.has(key)) {
|
|
4174
|
-
return false;
|
|
4175
|
-
}
|
|
4176
|
-
const target = getProxyDraft(this);
|
|
4177
|
-
ensureShallowCopy(target);
|
|
4178
|
-
markChanged(target);
|
|
4179
|
-
if (target.original.has(key)) {
|
|
4180
|
-
target.assignedMap.set(key, false);
|
|
4181
|
-
} else {
|
|
4182
|
-
target.assignedMap.delete(key);
|
|
4183
|
-
}
|
|
4184
|
-
target.copy.delete(key);
|
|
4185
|
-
return true;
|
|
4186
|
-
},
|
|
4187
|
-
clear() {
|
|
4188
|
-
const target = getProxyDraft(this);
|
|
4189
|
-
if (!this.size)
|
|
4190
|
-
return;
|
|
4191
|
-
ensureShallowCopy(target);
|
|
4192
|
-
markChanged(target);
|
|
4193
|
-
target.assignedMap = new Map;
|
|
4194
|
-
for (const [key] of target.original) {
|
|
4195
|
-
target.assignedMap.set(key, false);
|
|
4196
|
-
}
|
|
4197
|
-
target.copy.clear();
|
|
4198
|
-
},
|
|
4199
|
-
forEach(callback, thisArg) {
|
|
4200
|
-
const target = getProxyDraft(this);
|
|
4201
|
-
latest(target).forEach((_value, _key) => {
|
|
4202
|
-
callback.call(thisArg, this.get(_key), _key, this);
|
|
4203
|
-
});
|
|
4204
|
-
},
|
|
4205
|
-
get(key) {
|
|
4206
|
-
var _a, _b;
|
|
4207
|
-
const target = getProxyDraft(this);
|
|
4208
|
-
const value = latest(target).get(key);
|
|
4209
|
-
const mutable = ((_b = (_a = target.options).mark) === null || _b === undefined ? undefined : _b.call(_a, value, dataTypes)) === dataTypes.mutable;
|
|
4210
|
-
if (target.options.strict) {
|
|
4211
|
-
checkReadable(value, target.options, mutable);
|
|
4212
|
-
}
|
|
4213
|
-
if (mutable) {
|
|
4214
|
-
return value;
|
|
4215
|
-
}
|
|
4216
|
-
if (target.finalized || !isDraftable(value, target.options)) {
|
|
4217
|
-
return value;
|
|
4218
|
-
}
|
|
4219
|
-
if (value !== target.original.get(key)) {
|
|
4220
|
-
return value;
|
|
4221
|
-
}
|
|
4222
|
-
const draft = internal.createDraft({
|
|
4223
|
-
original: value,
|
|
4224
|
-
parentDraft: target,
|
|
4225
|
-
key,
|
|
4226
|
-
finalities: target.finalities,
|
|
4227
|
-
options: target.options
|
|
4228
|
-
});
|
|
4229
|
-
ensureShallowCopy(target);
|
|
4230
|
-
target.copy.set(key, draft);
|
|
4231
|
-
return draft;
|
|
4232
|
-
},
|
|
4233
|
-
keys() {
|
|
4234
|
-
return latest(getProxyDraft(this)).keys();
|
|
4235
|
-
},
|
|
4236
|
-
values() {
|
|
4237
|
-
const iterator = this.keys();
|
|
4238
|
-
return {
|
|
4239
|
-
[iteratorSymbol]: () => this.values(),
|
|
4240
|
-
next: () => {
|
|
4241
|
-
const result = iterator.next();
|
|
4242
|
-
if (result.done)
|
|
4243
|
-
return result;
|
|
4244
|
-
const value = this.get(result.value);
|
|
4245
|
-
return {
|
|
4246
|
-
done: false,
|
|
4247
|
-
value
|
|
4248
|
-
};
|
|
4249
|
-
}
|
|
4250
|
-
};
|
|
4251
|
-
},
|
|
4252
|
-
entries() {
|
|
4253
|
-
const iterator = this.keys();
|
|
4254
|
-
return {
|
|
4255
|
-
[iteratorSymbol]: () => this.entries(),
|
|
4256
|
-
next: () => {
|
|
4257
|
-
const result = iterator.next();
|
|
4258
|
-
if (result.done)
|
|
4259
|
-
return result;
|
|
4260
|
-
const value = this.get(result.value);
|
|
4261
|
-
return {
|
|
4262
|
-
done: false,
|
|
4263
|
-
value: [result.value, value]
|
|
4264
|
-
};
|
|
4265
|
-
}
|
|
4266
|
-
};
|
|
4267
|
-
},
|
|
4268
|
-
[iteratorSymbol]() {
|
|
4269
|
-
return this.entries();
|
|
4270
|
-
}
|
|
4271
|
-
};
|
|
4272
|
-
var mapHandlerKeys = Reflect.ownKeys(mapHandler);
|
|
4273
|
-
var getNextIterator = (target, iterator, { isValuesIterator }) => () => {
|
|
4274
|
-
var _a, _b;
|
|
4275
|
-
const result = iterator.next();
|
|
4276
|
-
if (result.done)
|
|
4277
|
-
return result;
|
|
4278
|
-
const key = result.value;
|
|
4279
|
-
let value = target.setMap.get(key);
|
|
4280
|
-
const currentDraft = getProxyDraft(value);
|
|
4281
|
-
const mutable = ((_b = (_a = target.options).mark) === null || _b === undefined ? undefined : _b.call(_a, value, dataTypes)) === dataTypes.mutable;
|
|
4282
|
-
if (target.options.strict) {
|
|
4283
|
-
checkReadable(key, target.options, mutable);
|
|
4284
|
-
}
|
|
4285
|
-
if (!mutable && !currentDraft && isDraftable(key, target.options) && !target.finalized && target.original.has(key)) {
|
|
4286
|
-
const proxy = internal.createDraft({
|
|
4287
|
-
original: key,
|
|
4288
|
-
parentDraft: target,
|
|
4289
|
-
key,
|
|
4290
|
-
finalities: target.finalities,
|
|
4291
|
-
options: target.options
|
|
4292
|
-
});
|
|
4293
|
-
target.setMap.set(key, proxy);
|
|
4294
|
-
value = proxy;
|
|
4295
|
-
} else if (currentDraft) {
|
|
4296
|
-
value = currentDraft.proxy;
|
|
4297
|
-
}
|
|
4298
|
-
return {
|
|
4299
|
-
done: false,
|
|
4300
|
-
value: isValuesIterator ? value : [value, value]
|
|
4301
|
-
};
|
|
4302
|
-
};
|
|
4303
|
-
var setHandler = {
|
|
4304
|
-
get size() {
|
|
4305
|
-
const target = getProxyDraft(this);
|
|
4306
|
-
return target.setMap.size;
|
|
4307
|
-
},
|
|
4308
|
-
has(value) {
|
|
4309
|
-
const target = getProxyDraft(this);
|
|
4310
|
-
if (target.setMap.has(value))
|
|
4311
|
-
return true;
|
|
4312
|
-
ensureShallowCopy(target);
|
|
4313
|
-
const valueProxyDraft = getProxyDraft(value);
|
|
4314
|
-
if (valueProxyDraft && target.setMap.has(valueProxyDraft.original))
|
|
4315
|
-
return true;
|
|
4316
|
-
return false;
|
|
4317
|
-
},
|
|
4318
|
-
add(value) {
|
|
4319
|
-
const target = getProxyDraft(this);
|
|
4320
|
-
if (!this.has(value)) {
|
|
4321
|
-
ensureShallowCopy(target);
|
|
4322
|
-
markChanged(target);
|
|
4323
|
-
target.assignedMap.set(value, true);
|
|
4324
|
-
target.setMap.set(value, value);
|
|
4325
|
-
markFinalization(target, value, value, generatePatches);
|
|
4326
|
-
}
|
|
4327
|
-
return this;
|
|
4328
|
-
},
|
|
4329
|
-
delete(value) {
|
|
4330
|
-
if (!this.has(value)) {
|
|
4331
|
-
return false;
|
|
4332
|
-
}
|
|
4333
|
-
const target = getProxyDraft(this);
|
|
4334
|
-
ensureShallowCopy(target);
|
|
4335
|
-
markChanged(target);
|
|
4336
|
-
const valueProxyDraft = getProxyDraft(value);
|
|
4337
|
-
if (valueProxyDraft && target.setMap.has(valueProxyDraft.original)) {
|
|
4338
|
-
target.assignedMap.set(valueProxyDraft.original, false);
|
|
4339
|
-
return target.setMap.delete(valueProxyDraft.original);
|
|
4340
|
-
}
|
|
4341
|
-
if (!valueProxyDraft && target.setMap.has(value)) {
|
|
4342
|
-
target.assignedMap.set(value, false);
|
|
4343
|
-
} else {
|
|
4344
|
-
target.assignedMap.delete(value);
|
|
4345
|
-
}
|
|
4346
|
-
return target.setMap.delete(value);
|
|
4347
|
-
},
|
|
4348
|
-
clear() {
|
|
4349
|
-
if (!this.size)
|
|
4350
|
-
return;
|
|
4351
|
-
const target = getProxyDraft(this);
|
|
4352
|
-
ensureShallowCopy(target);
|
|
4353
|
-
markChanged(target);
|
|
4354
|
-
for (const value of target.original) {
|
|
4355
|
-
target.assignedMap.set(value, false);
|
|
4356
|
-
}
|
|
4357
|
-
target.setMap.clear();
|
|
4358
|
-
},
|
|
4359
|
-
values() {
|
|
4360
|
-
const target = getProxyDraft(this);
|
|
4361
|
-
ensureShallowCopy(target);
|
|
4362
|
-
const iterator = target.setMap.keys();
|
|
4363
|
-
return {
|
|
4364
|
-
[Symbol.iterator]: () => this.values(),
|
|
4365
|
-
next: getNextIterator(target, iterator, { isValuesIterator: true })
|
|
4366
|
-
};
|
|
4367
|
-
},
|
|
4368
|
-
entries() {
|
|
4369
|
-
const target = getProxyDraft(this);
|
|
4370
|
-
ensureShallowCopy(target);
|
|
4371
|
-
const iterator = target.setMap.keys();
|
|
4372
|
-
return {
|
|
4373
|
-
[Symbol.iterator]: () => this.entries(),
|
|
4374
|
-
next: getNextIterator(target, iterator, {
|
|
4375
|
-
isValuesIterator: false
|
|
4376
|
-
})
|
|
4377
|
-
};
|
|
4378
|
-
},
|
|
4379
|
-
keys() {
|
|
4380
|
-
return this.values();
|
|
4381
|
-
},
|
|
4382
|
-
[iteratorSymbol]() {
|
|
4383
|
-
return this.values();
|
|
4384
|
-
},
|
|
4385
|
-
forEach(callback, thisArg) {
|
|
4386
|
-
const iterator = this.values();
|
|
4387
|
-
let result = iterator.next();
|
|
4388
|
-
while (!result.done) {
|
|
4389
|
-
callback.call(thisArg, result.value, result.value, this);
|
|
4390
|
-
result = iterator.next();
|
|
4391
|
-
}
|
|
4392
|
-
}
|
|
4393
|
-
};
|
|
4394
|
-
var setHandlerKeys = Reflect.ownKeys(setHandler);
|
|
4395
|
-
var draftsCache = new WeakSet;
|
|
4396
|
-
var proxyHandler = {
|
|
4397
|
-
get(target, key, receiver) {
|
|
4398
|
-
var _a, _b;
|
|
4399
|
-
const copy = (_a = target.copy) === null || _a === undefined ? undefined : _a[key];
|
|
4400
|
-
if (copy && draftsCache.has(copy)) {
|
|
4401
|
-
return copy;
|
|
4402
|
-
}
|
|
4403
|
-
if (key === PROXY_DRAFT)
|
|
4404
|
-
return target;
|
|
4405
|
-
let markResult;
|
|
4406
|
-
if (target.options.mark) {
|
|
4407
|
-
const value2 = key === "size" && (target.original instanceof Map || target.original instanceof Set) ? Reflect.get(target.original, key) : Reflect.get(target.original, key, receiver);
|
|
4408
|
-
markResult = target.options.mark(value2, dataTypes);
|
|
4409
|
-
if (markResult === dataTypes.mutable) {
|
|
4410
|
-
if (target.options.strict) {
|
|
4411
|
-
checkReadable(value2, target.options, true);
|
|
4412
|
-
}
|
|
4413
|
-
return value2;
|
|
4414
|
-
}
|
|
4415
|
-
}
|
|
4416
|
-
const source = latest(target);
|
|
4417
|
-
if (source instanceof Map && mapHandlerKeys.includes(key)) {
|
|
4418
|
-
if (key === "size") {
|
|
4419
|
-
return Object.getOwnPropertyDescriptor(mapHandler, "size").get.call(target.proxy);
|
|
4420
|
-
}
|
|
4421
|
-
const handle = mapHandler[key];
|
|
4422
|
-
if (handle) {
|
|
4423
|
-
return handle.bind(target.proxy);
|
|
4424
|
-
}
|
|
4425
|
-
}
|
|
4426
|
-
if (source instanceof Set && setHandlerKeys.includes(key)) {
|
|
4427
|
-
if (key === "size") {
|
|
4428
|
-
return Object.getOwnPropertyDescriptor(setHandler, "size").get.call(target.proxy);
|
|
4429
|
-
}
|
|
4430
|
-
const handle = setHandler[key];
|
|
4431
|
-
if (handle) {
|
|
4432
|
-
return handle.bind(target.proxy);
|
|
4433
|
-
}
|
|
4434
|
-
}
|
|
4435
|
-
if (!has(source, key)) {
|
|
4436
|
-
const desc = getDescriptor(source, key);
|
|
4437
|
-
return desc ? `value` in desc ? desc.value : (_b = desc.get) === null || _b === undefined ? undefined : _b.call(target.proxy) : undefined;
|
|
4438
|
-
}
|
|
4439
|
-
const value = source[key];
|
|
4440
|
-
if (target.options.strict) {
|
|
4441
|
-
checkReadable(value, target.options);
|
|
4442
|
-
}
|
|
4443
|
-
if (target.finalized || !isDraftable(value, target.options)) {
|
|
4444
|
-
return value;
|
|
4445
|
-
}
|
|
4446
|
-
if (value === peek(target.original, key)) {
|
|
4447
|
-
ensureShallowCopy(target);
|
|
4448
|
-
target.copy[key] = createDraft({
|
|
4449
|
-
original: target.original[key],
|
|
4450
|
-
parentDraft: target,
|
|
4451
|
-
key: target.type === 1 ? Number(key) : key,
|
|
4452
|
-
finalities: target.finalities,
|
|
4453
|
-
options: target.options
|
|
4454
|
-
});
|
|
4455
|
-
if (typeof markResult === "function") {
|
|
4456
|
-
const subProxyDraft = getProxyDraft(target.copy[key]);
|
|
4457
|
-
ensureShallowCopy(subProxyDraft);
|
|
4458
|
-
markChanged(subProxyDraft);
|
|
4459
|
-
return subProxyDraft.copy;
|
|
4460
|
-
}
|
|
4461
|
-
return target.copy[key];
|
|
4462
|
-
}
|
|
4463
|
-
return value;
|
|
4464
|
-
},
|
|
4465
|
-
set(target, key, value) {
|
|
4466
|
-
var _a;
|
|
4467
|
-
if (target.type === 3 || target.type === 2) {
|
|
4468
|
-
throw new Error(`Map/Set draft does not support any property assignment.`);
|
|
4469
|
-
}
|
|
4470
|
-
let _key;
|
|
4471
|
-
if (target.type === 1 && key !== "length" && !(Number.isInteger(_key = Number(key)) && _key >= 0 && (key === 0 || _key === 0 || String(_key) === String(key)))) {
|
|
4472
|
-
throw new Error(`Only supports setting array indices and the 'length' property.`);
|
|
4473
|
-
}
|
|
4474
|
-
const desc = getDescriptor(latest(target), key);
|
|
4475
|
-
if (desc === null || desc === undefined ? undefined : desc.set) {
|
|
4476
|
-
desc.set.call(target.proxy, value);
|
|
4477
|
-
return true;
|
|
4478
|
-
}
|
|
4479
|
-
const current = peek(latest(target), key);
|
|
4480
|
-
const currentProxyDraft = getProxyDraft(current);
|
|
4481
|
-
if (currentProxyDraft && isEqual(currentProxyDraft.original, value)) {
|
|
4482
|
-
target.copy[key] = value;
|
|
4483
|
-
target.assignedMap = (_a = target.assignedMap) !== null && _a !== undefined ? _a : new Map;
|
|
4484
|
-
target.assignedMap.set(key, false);
|
|
4485
|
-
return true;
|
|
4486
|
-
}
|
|
4487
|
-
if (isEqual(value, current) && (value !== undefined || has(target.original, key)))
|
|
4488
|
-
return true;
|
|
4489
|
-
ensureShallowCopy(target);
|
|
4490
|
-
markChanged(target);
|
|
4491
|
-
if (has(target.original, key) && isEqual(value, target.original[key])) {
|
|
4492
|
-
target.assignedMap.delete(key);
|
|
4493
|
-
} else {
|
|
4494
|
-
target.assignedMap.set(key, true);
|
|
4495
|
-
}
|
|
4496
|
-
target.copy[key] = value;
|
|
4497
|
-
markFinalization(target, key, value, generatePatches);
|
|
4498
|
-
return true;
|
|
4499
|
-
},
|
|
4500
|
-
has(target, key) {
|
|
4501
|
-
return key in latest(target);
|
|
4502
|
-
},
|
|
4503
|
-
ownKeys(target) {
|
|
4504
|
-
return Reflect.ownKeys(latest(target));
|
|
4505
|
-
},
|
|
4506
|
-
getOwnPropertyDescriptor(target, key) {
|
|
4507
|
-
const source = latest(target);
|
|
4508
|
-
const descriptor = Reflect.getOwnPropertyDescriptor(source, key);
|
|
4509
|
-
if (!descriptor)
|
|
4510
|
-
return descriptor;
|
|
4511
|
-
return {
|
|
4512
|
-
writable: true,
|
|
4513
|
-
configurable: target.type !== 1 || key !== "length",
|
|
4514
|
-
enumerable: descriptor.enumerable,
|
|
4515
|
-
value: source[key]
|
|
4516
|
-
};
|
|
4517
|
-
},
|
|
4518
|
-
getPrototypeOf(target) {
|
|
4519
|
-
return Reflect.getPrototypeOf(target.original);
|
|
4520
|
-
},
|
|
4521
|
-
setPrototypeOf() {
|
|
4522
|
-
throw new Error(`Cannot call 'setPrototypeOf()' on drafts`);
|
|
4523
|
-
},
|
|
4524
|
-
defineProperty() {
|
|
4525
|
-
throw new Error(`Cannot call 'defineProperty()' on drafts`);
|
|
4526
|
-
},
|
|
4527
|
-
deleteProperty(target, key) {
|
|
4528
|
-
var _a;
|
|
4529
|
-
if (target.type === 1) {
|
|
4530
|
-
return proxyHandler.set.call(this, target, key, undefined, target.proxy);
|
|
4531
|
-
}
|
|
4532
|
-
if (peek(target.original, key) !== undefined || key in target.original) {
|
|
4533
|
-
ensureShallowCopy(target);
|
|
4534
|
-
markChanged(target);
|
|
4535
|
-
target.assignedMap.set(key, false);
|
|
4536
|
-
} else {
|
|
4537
|
-
target.assignedMap = (_a = target.assignedMap) !== null && _a !== undefined ? _a : new Map;
|
|
4538
|
-
target.assignedMap.delete(key);
|
|
4539
|
-
}
|
|
4540
|
-
if (target.copy)
|
|
4541
|
-
delete target.copy[key];
|
|
4542
|
-
return true;
|
|
4543
|
-
}
|
|
4544
|
-
};
|
|
4545
|
-
function createDraft(createDraftOptions) {
|
|
4546
|
-
const { original, parentDraft, key, finalities, options } = createDraftOptions;
|
|
4547
|
-
const type = getType(original);
|
|
4548
|
-
const proxyDraft = {
|
|
4549
|
-
type,
|
|
4550
|
-
finalized: false,
|
|
4551
|
-
parent: parentDraft,
|
|
4552
|
-
original,
|
|
4553
|
-
copy: null,
|
|
4554
|
-
proxy: null,
|
|
4555
|
-
finalities,
|
|
4556
|
-
options,
|
|
4557
|
-
setMap: type === 3 ? new Map(original.entries()) : undefined
|
|
4558
|
-
};
|
|
4559
|
-
if (key || "key" in createDraftOptions) {
|
|
4560
|
-
proxyDraft.key = key;
|
|
4561
|
-
}
|
|
4562
|
-
const { proxy, revoke } = Proxy.revocable(type === 1 ? Object.assign([], proxyDraft) : proxyDraft, proxyHandler);
|
|
4563
|
-
finalities.revoke.push(revoke);
|
|
4564
|
-
draftsCache.add(proxy);
|
|
4565
|
-
proxyDraft.proxy = proxy;
|
|
4566
|
-
if (parentDraft) {
|
|
4567
|
-
const target = parentDraft;
|
|
4568
|
-
target.finalities.draft.push((patches, inversePatches) => {
|
|
4569
|
-
var _a, _b;
|
|
4570
|
-
const oldProxyDraft = getProxyDraft(proxy);
|
|
4571
|
-
let copy = target.type === 3 ? target.setMap : target.copy;
|
|
4572
|
-
const draft = get(copy, key);
|
|
4573
|
-
const proxyDraft2 = getProxyDraft(draft);
|
|
4574
|
-
if (proxyDraft2) {
|
|
4575
|
-
let updatedValue = proxyDraft2.original;
|
|
4576
|
-
if (proxyDraft2.operated) {
|
|
4577
|
-
updatedValue = getValue(draft);
|
|
4578
|
-
}
|
|
4579
|
-
finalizeSetValue(proxyDraft2);
|
|
4580
|
-
finalizePatches(proxyDraft2, generatePatches, patches, inversePatches);
|
|
4581
|
-
if (target.options.enableAutoFreeze) {
|
|
4582
|
-
target.options.updatedValues = (_a = target.options.updatedValues) !== null && _a !== undefined ? _a : new WeakMap;
|
|
4583
|
-
target.options.updatedValues.set(updatedValue, proxyDraft2.original);
|
|
4584
|
-
}
|
|
4585
|
-
set(copy, key, updatedValue);
|
|
4586
|
-
}
|
|
4587
|
-
(_b = oldProxyDraft.callbacks) === null || _b === undefined || _b.forEach((callback) => {
|
|
4588
|
-
callback(patches, inversePatches);
|
|
4589
|
-
});
|
|
4590
|
-
});
|
|
4591
|
-
} else {
|
|
4592
|
-
const target = getProxyDraft(proxy);
|
|
4593
|
-
target.finalities.draft.push((patches, inversePatches) => {
|
|
4594
|
-
finalizeSetValue(target);
|
|
4595
|
-
finalizePatches(target, generatePatches, patches, inversePatches);
|
|
4596
|
-
});
|
|
4597
|
-
}
|
|
4598
|
-
return proxy;
|
|
4599
|
-
}
|
|
4600
|
-
internal.createDraft = createDraft;
|
|
4601
|
-
function finalizeDraft(result, returnedValue, patches, inversePatches, enableAutoFreeze) {
|
|
4602
|
-
var _a;
|
|
4603
|
-
const proxyDraft = getProxyDraft(result);
|
|
4604
|
-
const original = (_a = proxyDraft === null || proxyDraft === undefined ? undefined : proxyDraft.original) !== null && _a !== undefined ? _a : result;
|
|
4605
|
-
const hasReturnedValue = !!returnedValue.length;
|
|
4606
|
-
if (proxyDraft === null || proxyDraft === undefined ? undefined : proxyDraft.operated) {
|
|
4607
|
-
while (proxyDraft.finalities.draft.length > 0) {
|
|
4608
|
-
const finalize = proxyDraft.finalities.draft.pop();
|
|
4609
|
-
finalize(patches, inversePatches);
|
|
4610
|
-
}
|
|
4611
|
-
}
|
|
4612
|
-
const state = hasReturnedValue ? returnedValue[0] : proxyDraft ? proxyDraft.operated ? proxyDraft.copy : proxyDraft.original : result;
|
|
4613
|
-
if (proxyDraft)
|
|
4614
|
-
revokeProxy(proxyDraft);
|
|
4615
|
-
if (enableAutoFreeze) {
|
|
4616
|
-
deepFreeze(state, state, proxyDraft === null || proxyDraft === undefined ? undefined : proxyDraft.options.updatedValues);
|
|
4617
|
-
}
|
|
4618
|
-
return [
|
|
4619
|
-
state,
|
|
4620
|
-
patches && hasReturnedValue ? [{ op: Operation.Replace, path: [], value: returnedValue[0] }] : patches,
|
|
4621
|
-
inversePatches && hasReturnedValue ? [{ op: Operation.Replace, path: [], value: original }] : inversePatches
|
|
4622
|
-
];
|
|
4623
|
-
}
|
|
4624
|
-
function draftify(baseState, options) {
|
|
4625
|
-
var _a;
|
|
4626
|
-
const finalities = {
|
|
4627
|
-
draft: [],
|
|
4628
|
-
revoke: [],
|
|
4629
|
-
handledSet: new WeakSet
|
|
4630
|
-
};
|
|
4631
|
-
let patches;
|
|
4632
|
-
let inversePatches;
|
|
4633
|
-
if (options.enablePatches) {
|
|
4634
|
-
patches = [];
|
|
4635
|
-
inversePatches = [];
|
|
4636
|
-
}
|
|
4637
|
-
const isMutable = ((_a = options.mark) === null || _a === undefined ? undefined : _a.call(options, baseState, dataTypes)) === dataTypes.mutable || !isDraftable(baseState, options);
|
|
4638
|
-
const draft = isMutable ? baseState : createDraft({
|
|
4639
|
-
original: baseState,
|
|
4640
|
-
parentDraft: null,
|
|
4641
|
-
finalities,
|
|
4642
|
-
options
|
|
4643
|
-
});
|
|
4644
|
-
return [
|
|
4645
|
-
draft,
|
|
4646
|
-
(returnedValue = []) => {
|
|
4647
|
-
const [finalizedState, finalizedPatches, finalizedInversePatches] = finalizeDraft(draft, returnedValue, patches, inversePatches, options.enableAutoFreeze);
|
|
4648
|
-
return options.enablePatches ? [finalizedState, finalizedPatches, finalizedInversePatches] : finalizedState;
|
|
4649
|
-
}
|
|
4650
|
-
];
|
|
4651
|
-
}
|
|
4652
|
-
function handleReturnValue(options) {
|
|
4653
|
-
const { rootDraft, value, useRawReturn = false, isRoot = true } = options;
|
|
4654
|
-
forEach(value, (key, item, source) => {
|
|
4655
|
-
const proxyDraft = getProxyDraft(item);
|
|
4656
|
-
if (proxyDraft && rootDraft && proxyDraft.finalities === rootDraft.finalities) {
|
|
4657
|
-
options.isContainDraft = true;
|
|
4658
|
-
const currentValue = proxyDraft.original;
|
|
4659
|
-
if (source instanceof Set) {
|
|
4660
|
-
const arr = Array.from(source);
|
|
4661
|
-
source.clear();
|
|
4662
|
-
arr.forEach((_item) => source.add(key === _item ? currentValue : _item));
|
|
4663
|
-
} else {
|
|
4664
|
-
set(source, key, currentValue);
|
|
4665
|
-
}
|
|
4666
|
-
} else if (typeof item === "object" && item !== null) {
|
|
4667
|
-
options.value = item;
|
|
4668
|
-
options.isRoot = false;
|
|
4669
|
-
handleReturnValue(options);
|
|
4670
|
-
}
|
|
4671
|
-
});
|
|
4672
|
-
if (isRoot) {
|
|
4673
|
-
if (!options.isContainDraft)
|
|
4674
|
-
console.warn(`The return value does not contain any draft, please use 'rawReturn()' to wrap the return value to improve performance.`);
|
|
4675
|
-
if (useRawReturn) {
|
|
4676
|
-
console.warn(`The return value contains drafts, please don't use 'rawReturn()' to wrap the return value.`);
|
|
4677
|
-
}
|
|
4678
|
-
}
|
|
4679
|
-
}
|
|
4680
|
-
function getCurrent(target) {
|
|
4681
|
-
const proxyDraft = getProxyDraft(target);
|
|
4682
|
-
if (!isDraftable(target, proxyDraft === null || proxyDraft === undefined ? undefined : proxyDraft.options))
|
|
4683
|
-
return target;
|
|
4684
|
-
const type = getType(target);
|
|
4685
|
-
if (proxyDraft && !proxyDraft.operated)
|
|
4686
|
-
return proxyDraft.original;
|
|
4687
|
-
let currentValue;
|
|
4688
|
-
function ensureShallowCopy2() {
|
|
4689
|
-
currentValue = type === 2 ? new Map(target) : type === 3 ? Array.from(proxyDraft.setMap.values()) : shallowCopy(target, proxyDraft === null || proxyDraft === undefined ? undefined : proxyDraft.options);
|
|
4690
|
-
}
|
|
4691
|
-
if (proxyDraft) {
|
|
4692
|
-
proxyDraft.finalized = true;
|
|
4693
|
-
try {
|
|
4694
|
-
ensureShallowCopy2();
|
|
4695
|
-
} finally {
|
|
4696
|
-
proxyDraft.finalized = false;
|
|
4697
|
-
}
|
|
4698
|
-
} else {
|
|
4699
|
-
currentValue = target;
|
|
4700
|
-
}
|
|
4701
|
-
forEach(currentValue, (key, value) => {
|
|
4702
|
-
if (proxyDraft && isEqual(get(proxyDraft.original, key), value))
|
|
4703
|
-
return;
|
|
4704
|
-
const newValue = getCurrent(value);
|
|
4705
|
-
if (newValue !== value) {
|
|
4706
|
-
if (currentValue === target)
|
|
4707
|
-
ensureShallowCopy2();
|
|
4708
|
-
set(currentValue, key, newValue);
|
|
4709
|
-
}
|
|
4710
|
-
});
|
|
4711
|
-
return type === 3 ? new Set(currentValue) : currentValue;
|
|
4712
|
-
}
|
|
4713
|
-
function current(target) {
|
|
4714
|
-
if (!isDraft(target)) {
|
|
4715
|
-
throw new Error(`current() is only used for Draft, parameter: ${target}`);
|
|
4716
|
-
}
|
|
4717
|
-
return getCurrent(target);
|
|
4718
|
-
}
|
|
4719
|
-
var makeCreator = (arg) => {
|
|
4720
|
-
if (arg !== undefined && Object.prototype.toString.call(arg) !== "[object Object]") {
|
|
4721
|
-
throw new Error(`Invalid options: ${String(arg)}, 'options' should be an object.`);
|
|
4722
|
-
}
|
|
4723
|
-
return function create(arg0, arg1, arg2) {
|
|
4724
|
-
var _a, _b, _c;
|
|
4725
|
-
if (typeof arg0 === "function" && typeof arg1 !== "function") {
|
|
4726
|
-
return function(base2, ...args) {
|
|
4727
|
-
return create(base2, (draft2) => arg0.call(this, draft2, ...args), arg1);
|
|
4728
|
-
};
|
|
4729
|
-
}
|
|
4730
|
-
const base = arg0;
|
|
4731
|
-
const mutate = arg1;
|
|
4732
|
-
let options = arg2;
|
|
4733
|
-
if (typeof arg1 !== "function") {
|
|
4734
|
-
options = arg1;
|
|
4735
|
-
}
|
|
4736
|
-
if (options !== undefined && Object.prototype.toString.call(options) !== "[object Object]") {
|
|
4737
|
-
throw new Error(`Invalid options: ${options}, 'options' should be an object.`);
|
|
4738
|
-
}
|
|
4739
|
-
options = Object.assign(Object.assign({}, arg), options);
|
|
4740
|
-
const state = isDraft(base) ? current(base) : base;
|
|
4741
|
-
const mark = Array.isArray(options.mark) ? (value, types) => {
|
|
4742
|
-
for (const mark2 of options.mark) {
|
|
4743
|
-
if (typeof mark2 !== "function") {
|
|
4744
|
-
throw new Error(`Invalid mark: ${mark2}, 'mark' should be a function.`);
|
|
4745
|
-
}
|
|
4746
|
-
const result2 = mark2(value, types);
|
|
4747
|
-
if (result2) {
|
|
4748
|
-
return result2;
|
|
4749
|
-
}
|
|
4750
|
-
}
|
|
4751
|
-
return;
|
|
4752
|
-
} : options.mark;
|
|
4753
|
-
const enablePatches = (_a = options.enablePatches) !== null && _a !== undefined ? _a : false;
|
|
4754
|
-
const strict = (_b = options.strict) !== null && _b !== undefined ? _b : false;
|
|
4755
|
-
const enableAutoFreeze = (_c = options.enableAutoFreeze) !== null && _c !== undefined ? _c : false;
|
|
4756
|
-
const _options = {
|
|
4757
|
-
enableAutoFreeze,
|
|
4758
|
-
mark,
|
|
4759
|
-
strict,
|
|
4760
|
-
enablePatches
|
|
4761
|
-
};
|
|
4762
|
-
if (!isDraftable(state, _options) && typeof state === "object" && state !== null) {
|
|
4763
|
-
throw new Error(`Invalid base state: create() only supports plain objects, arrays, Set, Map or using mark() to mark the state as immutable.`);
|
|
4764
|
-
}
|
|
4765
|
-
const [draft, finalize] = draftify(state, _options);
|
|
4766
|
-
if (typeof arg1 !== "function") {
|
|
4767
|
-
if (!isDraftable(state, _options)) {
|
|
4768
|
-
throw new Error(`Invalid base state: create() only supports plain objects, arrays, Set, Map or using mark() to mark the state as immutable.`);
|
|
4769
|
-
}
|
|
4770
|
-
return [draft, finalize];
|
|
4771
|
-
}
|
|
4772
|
-
let result;
|
|
4773
|
-
try {
|
|
4774
|
-
result = mutate(draft);
|
|
4775
|
-
} catch (error) {
|
|
4776
|
-
revokeProxy(getProxyDraft(draft));
|
|
4777
|
-
throw error;
|
|
4778
|
-
}
|
|
4779
|
-
const returnValue = (value) => {
|
|
4780
|
-
const proxyDraft = getProxyDraft(draft);
|
|
4781
|
-
if (!isDraft(value)) {
|
|
4782
|
-
if (value !== undefined && !isEqual(value, draft) && (proxyDraft === null || proxyDraft === undefined ? undefined : proxyDraft.operated)) {
|
|
4783
|
-
throw new Error(`Either the value is returned as a new non-draft value, or only the draft is modified without returning any value.`);
|
|
4784
|
-
}
|
|
4785
|
-
const rawReturnValue = value === null || value === undefined ? undefined : value[RAW_RETURN_SYMBOL];
|
|
4786
|
-
if (rawReturnValue) {
|
|
4787
|
-
const _value = rawReturnValue[0];
|
|
4788
|
-
if (_options.strict && typeof value === "object" && value !== null) {
|
|
4789
|
-
handleReturnValue({
|
|
4790
|
-
rootDraft: proxyDraft,
|
|
4791
|
-
value,
|
|
4792
|
-
useRawReturn: true
|
|
4793
|
-
});
|
|
4794
|
-
}
|
|
4795
|
-
return finalize([_value]);
|
|
4796
|
-
}
|
|
4797
|
-
if (value !== undefined) {
|
|
4798
|
-
if (typeof value === "object" && value !== null) {
|
|
4799
|
-
handleReturnValue({ rootDraft: proxyDraft, value });
|
|
4800
|
-
}
|
|
4801
|
-
return finalize([value]);
|
|
4802
|
-
}
|
|
4803
|
-
}
|
|
4804
|
-
if (value === draft || value === undefined) {
|
|
4805
|
-
return finalize([]);
|
|
4806
|
-
}
|
|
4807
|
-
const returnedProxyDraft = getProxyDraft(value);
|
|
4808
|
-
if (_options === returnedProxyDraft.options) {
|
|
4809
|
-
if (returnedProxyDraft.operated) {
|
|
4810
|
-
throw new Error(`Cannot return a modified child draft.`);
|
|
4811
|
-
}
|
|
4812
|
-
return finalize([current(value)]);
|
|
4813
|
-
}
|
|
4814
|
-
return finalize([value]);
|
|
4815
|
-
};
|
|
4816
|
-
if (result instanceof Promise) {
|
|
4817
|
-
return result.then(returnValue, (error) => {
|
|
4818
|
-
revokeProxy(getProxyDraft(draft));
|
|
4819
|
-
throw error;
|
|
4820
|
-
});
|
|
4821
|
-
}
|
|
4822
|
-
return returnValue(result);
|
|
4823
|
-
};
|
|
4824
|
-
};
|
|
4825
|
-
var create = makeCreator();
|
|
4826
|
-
function apply(state, patches, applyOptions) {
|
|
4827
|
-
let i;
|
|
4828
|
-
for (i = patches.length - 1;i >= 0; i -= 1) {
|
|
4829
|
-
const { value, op, path } = patches[i];
|
|
4830
|
-
if (!path.length && op === Operation.Replace || path === "" && op === Operation.Add) {
|
|
4831
|
-
state = value;
|
|
4832
|
-
break;
|
|
4833
|
-
}
|
|
4834
|
-
}
|
|
4835
|
-
if (i > -1) {
|
|
4836
|
-
patches = patches.slice(i + 1);
|
|
4837
|
-
}
|
|
4838
|
-
const mutate = (draft) => {
|
|
4839
|
-
patches.forEach((patch) => {
|
|
4840
|
-
const { path: _path, op } = patch;
|
|
4841
|
-
const path = unescapePath(_path);
|
|
4842
|
-
let base = draft;
|
|
4843
|
-
for (let index = 0;index < path.length - 1; index += 1) {
|
|
4844
|
-
const parentType = getType(base);
|
|
4845
|
-
let key2 = path[index];
|
|
4846
|
-
if (typeof key2 !== "string" && typeof key2 !== "number") {
|
|
4847
|
-
key2 = String(key2);
|
|
4848
|
-
}
|
|
4849
|
-
if ((parentType === 0 || parentType === 1) && (key2 === "__proto__" || key2 === "constructor") || typeof base === "function" && key2 === "prototype") {
|
|
4850
|
-
throw new Error(`Patching reserved attributes like __proto__ and constructor is not allowed.`);
|
|
4851
|
-
}
|
|
4852
|
-
base = get(getType(base) === 3 ? Array.from(base) : base, key2);
|
|
4853
|
-
if (typeof base !== "object") {
|
|
4854
|
-
throw new Error(`Cannot apply patch at '${path.join("/")}'.`);
|
|
4855
|
-
}
|
|
4856
|
-
}
|
|
4857
|
-
const type = getType(base);
|
|
4858
|
-
const value = deepClone(patch.value);
|
|
4859
|
-
const key = path[path.length - 1];
|
|
4860
|
-
switch (op) {
|
|
4861
|
-
case Operation.Replace:
|
|
4862
|
-
switch (type) {
|
|
4863
|
-
case 2:
|
|
4864
|
-
return base.set(key, value);
|
|
4865
|
-
case 3:
|
|
4866
|
-
throw new Error(`Cannot apply replace patch to set.`);
|
|
4867
|
-
default:
|
|
4868
|
-
return base[key] = value;
|
|
4869
|
-
}
|
|
4870
|
-
case Operation.Add:
|
|
4871
|
-
switch (type) {
|
|
4872
|
-
case 1:
|
|
4873
|
-
return key === "-" ? base.push(value) : base.splice(key, 0, value);
|
|
4874
|
-
case 2:
|
|
4875
|
-
return base.set(key, value);
|
|
4876
|
-
case 3:
|
|
4877
|
-
return base.add(value);
|
|
4878
|
-
default:
|
|
4879
|
-
return base[key] = value;
|
|
4880
|
-
}
|
|
4881
|
-
case Operation.Remove:
|
|
4882
|
-
switch (type) {
|
|
4883
|
-
case 1:
|
|
4884
|
-
return base.splice(key, 1);
|
|
4885
|
-
case 2:
|
|
4886
|
-
return base.delete(key);
|
|
4887
|
-
case 3:
|
|
4888
|
-
return base.delete(patch.value);
|
|
4889
|
-
default:
|
|
4890
|
-
return delete base[key];
|
|
4891
|
-
}
|
|
4892
|
-
default:
|
|
4893
|
-
throw new Error(`Unsupported patch operation: ${op}.`);
|
|
4894
|
-
}
|
|
4895
|
-
});
|
|
4896
|
-
};
|
|
4897
|
-
if (isDraft(state)) {
|
|
4898
|
-
if (applyOptions !== undefined) {
|
|
4899
|
-
throw new Error(`Cannot apply patches with options to a draft.`);
|
|
4900
|
-
}
|
|
4901
|
-
mutate(state);
|
|
4902
|
-
return state;
|
|
4903
|
-
}
|
|
4904
|
-
return create(state, mutate, Object.assign(Object.assign({}, applyOptions), { enablePatches: false }));
|
|
4905
|
-
}
|
|
4906
|
-
var constructorString = Object.prototype.constructor.toString();
|
|
4907
|
-
|
|
4908
|
-
// ../core/dist/index.js
|
|
4909
|
-
class ArcContextElement {
|
|
4910
|
-
$event;
|
|
4911
|
-
name;
|
|
4912
|
-
queryBuilder;
|
|
4913
|
-
commandContext;
|
|
4914
|
-
commandClient;
|
|
4915
|
-
observer;
|
|
4916
|
-
}
|
|
4917
|
-
|
|
4918
|
-
class ArcContextElementWithStore extends ArcContextElement {
|
|
4919
|
-
}
|
|
4920
|
-
|
|
4921
|
-
class ArcOptional {
|
|
4922
|
-
parent;
|
|
4923
|
-
constructor(parent) {
|
|
4924
|
-
this.parent = parent;
|
|
4925
|
-
}
|
|
4926
|
-
parse(value) {
|
|
4927
|
-
if (!value)
|
|
4928
|
-
return null;
|
|
4929
|
-
return this.parent.parse(value);
|
|
4930
|
-
}
|
|
4931
|
-
serialize(value) {
|
|
4932
|
-
if (value)
|
|
4933
|
-
return this.parent.serialize(value);
|
|
4934
|
-
return null;
|
|
4935
|
-
}
|
|
4936
|
-
deserialize(value) {
|
|
4937
|
-
if (!value)
|
|
4938
|
-
return null;
|
|
4939
|
-
return this.parent.deserialize(value);
|
|
4940
|
-
}
|
|
4941
|
-
validate(value) {
|
|
4942
|
-
if (!value)
|
|
4943
|
-
return false;
|
|
4944
|
-
return this.parent.validate(value);
|
|
4945
|
-
}
|
|
4946
|
-
toJsonSchema() {
|
|
4947
|
-
const parentSchema = this.parent.toJsonSchema?.() ?? {};
|
|
4948
|
-
return {
|
|
4949
|
-
anyOf: [parentSchema, { type: "null" }]
|
|
4950
|
-
};
|
|
4951
|
-
}
|
|
4952
|
-
}
|
|
4953
|
-
|
|
4954
|
-
class ArcBranded {
|
|
4955
|
-
parent;
|
|
4956
|
-
brand;
|
|
4957
|
-
constructor(parent, brand) {
|
|
4958
|
-
this.parent = parent;
|
|
4959
|
-
this.brand = brand;
|
|
4960
|
-
}
|
|
4961
|
-
serialize(value) {
|
|
4962
|
-
return this.parent.serialize(value);
|
|
4963
|
-
}
|
|
4964
|
-
deserialize(value) {
|
|
4965
|
-
return this.parent.deserialize(value);
|
|
4966
|
-
}
|
|
4967
|
-
parse(value) {
|
|
4968
|
-
return this.parent.parse(value);
|
|
4969
|
-
}
|
|
4970
|
-
optional() {
|
|
4971
|
-
return new ArcOptional(this);
|
|
4972
|
-
}
|
|
4973
|
-
validate(value) {
|
|
4974
|
-
return this.parent.validate(value);
|
|
4975
|
-
}
|
|
4976
|
-
toJsonSchema() {
|
|
4977
|
-
return this.parent.toJsonSchema?.() ?? {};
|
|
4978
|
-
}
|
|
4979
|
-
}
|
|
4980
|
-
|
|
4981
|
-
class ArcDefault {
|
|
4982
|
-
parent;
|
|
4983
|
-
defaultValueOrCallback;
|
|
4984
|
-
constructor(parent, defaultValueOrCallback) {
|
|
4985
|
-
this.parent = parent;
|
|
4986
|
-
this.defaultValueOrCallback = defaultValueOrCallback;
|
|
4987
|
-
}
|
|
4988
|
-
validate(value) {
|
|
4989
|
-
throw new Error("Method not implemented.");
|
|
4990
|
-
}
|
|
4991
|
-
parse(value) {
|
|
4992
|
-
if (value)
|
|
4993
|
-
return this.parent.parse(value);
|
|
4994
|
-
if (typeof this.defaultValueOrCallback === "function") {
|
|
4995
|
-
return this.defaultValueOrCallback();
|
|
4996
|
-
} else
|
|
4997
|
-
return this.defaultValueOrCallback;
|
|
4998
|
-
}
|
|
4999
|
-
serialize(value) {
|
|
5000
|
-
return this.parent.serialize(value);
|
|
5001
|
-
}
|
|
5002
|
-
deserialize(value) {
|
|
5003
|
-
if (value)
|
|
5004
|
-
return this.parent.deserialize(value);
|
|
5005
|
-
if (typeof this.defaultValueOrCallback === "function") {
|
|
5006
|
-
return this.defaultValueOrCallback();
|
|
5007
|
-
} else
|
|
5008
|
-
return this.defaultValueOrCallback;
|
|
5009
|
-
}
|
|
5010
|
-
toJsonSchema() {
|
|
5011
|
-
const schema = this.parent.toJsonSchema?.() ?? {};
|
|
5012
|
-
const defaultValue = typeof this.defaultValueOrCallback === "function" ? this.defaultValueOrCallback() : this.defaultValueOrCallback;
|
|
5013
|
-
return {
|
|
5014
|
-
...schema,
|
|
5015
|
-
default: defaultValue
|
|
5016
|
-
};
|
|
5017
|
-
}
|
|
5018
|
-
}
|
|
5019
|
-
|
|
5020
|
-
class ArcAbstract {
|
|
5021
|
-
validations;
|
|
5022
|
-
constructor(validations = []) {
|
|
5023
|
-
this.validations = validations;
|
|
5024
|
-
}
|
|
5025
|
-
default(defaultValueOrCallback) {
|
|
5026
|
-
return new ArcDefault(this, defaultValueOrCallback);
|
|
5027
|
-
}
|
|
5028
|
-
optional() {
|
|
5029
|
-
return new ArcOptional(this);
|
|
5030
|
-
}
|
|
5031
|
-
branded(name) {
|
|
5032
|
-
return new ArcBranded(this, name);
|
|
5033
|
-
}
|
|
5034
|
-
clone() {
|
|
5035
|
-
const Constructor = this.constructor;
|
|
5036
|
-
const newInstance = Object.assign(new Constructor, this);
|
|
5037
|
-
return newInstance;
|
|
5038
|
-
}
|
|
5039
|
-
validate(value) {
|
|
5040
|
-
const errors = this.validations.reduce((acc, { name, validator }) => {
|
|
5041
|
-
try {
|
|
5042
|
-
acc[name] = validator(value);
|
|
5043
|
-
} catch (error) {
|
|
5044
|
-
acc[name] = error;
|
|
5045
|
-
}
|
|
5046
|
-
return acc;
|
|
5047
|
-
}, {});
|
|
5048
|
-
if (Object.values(errors).some((result) => !!result)) {
|
|
5049
|
-
return errors;
|
|
5050
|
-
}
|
|
5051
|
-
return false;
|
|
5052
|
-
}
|
|
5053
|
-
pipeValidation(name, validator) {
|
|
5054
|
-
const newInstance = this.clone();
|
|
5055
|
-
newInstance.validations = [...this.validations, { name, validator }];
|
|
5056
|
-
return newInstance;
|
|
5057
|
-
}
|
|
5058
|
-
toJsonSchema() {
|
|
5059
|
-
return {};
|
|
5060
|
-
}
|
|
5061
|
-
getValidations() {
|
|
5062
|
-
return this.validations;
|
|
5063
|
-
}
|
|
5064
|
-
}
|
|
5065
|
-
|
|
5066
|
-
class ArcPrimitive extends ArcAbstract {
|
|
5067
|
-
serialize(value) {
|
|
5068
|
-
return value;
|
|
5069
|
-
}
|
|
5070
|
-
parse(value) {
|
|
5071
|
-
return value;
|
|
5072
|
-
}
|
|
5073
|
-
deserialize(value) {
|
|
5074
|
-
return value;
|
|
5075
|
-
}
|
|
5076
|
-
}
|
|
5077
|
-
function primitiveTypeComparatorStrategyFactory(type) {
|
|
5078
|
-
return (value) => typeof value === type;
|
|
5079
|
-
}
|
|
5080
|
-
function typeValidatorBuilder(typeName, comparatorStrategy) {
|
|
5081
|
-
const comparator = comparatorStrategy || primitiveTypeComparatorStrategyFactory(typeName);
|
|
5082
|
-
return {
|
|
5083
|
-
name: "type",
|
|
5084
|
-
validator: (value) => {
|
|
5085
|
-
const valueType = typeof value;
|
|
5086
|
-
if (!comparator(value))
|
|
5087
|
-
return { current: valueType, expected: typeName };
|
|
5088
|
-
return false;
|
|
5089
|
-
}
|
|
5090
|
-
};
|
|
5091
|
-
}
|
|
5092
|
-
var stringValidator = typeValidatorBuilder("string");
|
|
5093
|
-
|
|
5094
|
-
class ArcString extends ArcPrimitive {
|
|
5095
|
-
constructor() {
|
|
5096
|
-
super([stringValidator]);
|
|
5097
|
-
}
|
|
5098
|
-
minLength(min) {
|
|
5099
|
-
return this.validation("minLength", (value) => {
|
|
5100
|
-
if (value.length < min)
|
|
5101
|
-
return {
|
|
5102
|
-
currentLength: value.length,
|
|
5103
|
-
minLength: min
|
|
5104
|
-
};
|
|
5105
|
-
});
|
|
5106
|
-
}
|
|
5107
|
-
maxLength(max) {
|
|
5108
|
-
return this.validation("maxLength", (value) => {
|
|
5109
|
-
if (value.length > max)
|
|
5110
|
-
return {
|
|
5111
|
-
currentLength: value.length,
|
|
5112
|
-
maxLength: max
|
|
5113
|
-
};
|
|
5114
|
-
});
|
|
5115
|
-
}
|
|
5116
|
-
length(number) {
|
|
5117
|
-
return this.validation("length", (value) => {
|
|
5118
|
-
if (value.length !== number) {
|
|
5119
|
-
return {
|
|
5120
|
-
currentLength: value.length,
|
|
5121
|
-
length: number
|
|
5122
|
-
};
|
|
5123
|
-
}
|
|
5124
|
-
});
|
|
5125
|
-
}
|
|
5126
|
-
includes(str) {
|
|
5127
|
-
return this.validation("includes", (value) => {
|
|
5128
|
-
if (!value.includes(str)) {
|
|
5129
|
-
return {
|
|
5130
|
-
currentValue: value,
|
|
5131
|
-
includes: str
|
|
5132
|
-
};
|
|
5133
|
-
}
|
|
5134
|
-
});
|
|
5135
|
-
}
|
|
5136
|
-
startsWith(str) {
|
|
5137
|
-
return this.validation("startsWith", (value) => {
|
|
5138
|
-
if (!value.startsWith(str)) {
|
|
5139
|
-
return {
|
|
5140
|
-
currentValue: value,
|
|
5141
|
-
startsWith: str
|
|
5142
|
-
};
|
|
5143
|
-
}
|
|
5144
|
-
});
|
|
5145
|
-
}
|
|
5146
|
-
endsWith(str) {
|
|
5147
|
-
return this.validation("endsWith", (value) => {
|
|
5148
|
-
if (!value.endsWith(str)) {
|
|
5149
|
-
return {
|
|
5150
|
-
currentValue: value,
|
|
5151
|
-
endsWith: str
|
|
5152
|
-
};
|
|
5153
|
-
}
|
|
5154
|
-
});
|
|
5155
|
-
}
|
|
5156
|
-
regex(regex) {
|
|
5157
|
-
return this.validation("regex", (value) => {
|
|
5158
|
-
if (!regex.test(value)) {
|
|
5159
|
-
return {
|
|
5160
|
-
currentValue: value,
|
|
5161
|
-
regex
|
|
5162
|
-
};
|
|
5163
|
-
}
|
|
5164
|
-
});
|
|
5165
|
-
}
|
|
5166
|
-
email() {
|
|
5167
|
-
const regex = /^(?:(?:[a-zA-Z0-9!#$%&'*+/=?^_`{|}~.-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~.-]+)*)|(?:"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f])*"))@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$|(?:\[(?:\d{1,3}\.){3}\d{1,3}\])$/;
|
|
5168
|
-
return this.validation("email", (value) => {
|
|
5169
|
-
if (!regex.test(value)) {
|
|
5170
|
-
return {
|
|
5171
|
-
currentEmail: value
|
|
5172
|
-
};
|
|
5173
|
-
}
|
|
5174
|
-
});
|
|
5175
|
-
}
|
|
5176
|
-
toJsonSchema() {
|
|
5177
|
-
return { type: "string" };
|
|
5178
|
-
}
|
|
5179
|
-
url() {
|
|
5180
|
-
const regex = /^(https?):\/\/(?![-0-9])(?!www\.[0-9])(?:[a-zA-Z0-9-]+(?::[a-zA-Z0-9-]+)?@)?(?:localhost|\b(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b|[a-zA-Z0-9-]+\.)?[a-zA-Z0-9-]+\.(?:[a-zA-Z]{2,}|[a-zA-Z]{2}\.[a-zA-Z]{2})(?::\d{1,5})?(?!\/\/)(?:\/[a-zA-Z0-9-._~:?#\[\]@!$&'()*+,;=]*)*(?!\/{2})(?:;[a-zA-Z0-9-._~:?#\[\]@!$&'()*+,;=]*)*(\?(?!=)[a-zA-Z0-9&=;]*)?(\#[a-zA-Z0-9-]*)?$|^(https?):\/\/(localhost|\b(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)(?::\d{1,5})?(?!\/\/)(?:\/[a-zA-Z0-9-._~:?#\[\]@!$&'()*+,;=]*)*(?!\/{2})(?:;[a-zA-Z0-9-._~:?#\[\]@!$&'()*+,;=]*)*(\?(?!=)[a-zA-Z0-9&=;]*)?(\#[a-zA-Z0-9-]*)?$/;
|
|
5181
|
-
return this.validation("url", (value) => {
|
|
5182
|
-
if (!regex.test(value)) {
|
|
5183
|
-
return {
|
|
5184
|
-
currentUrl: value
|
|
5185
|
-
};
|
|
5186
|
-
}
|
|
5187
|
-
});
|
|
5188
|
-
}
|
|
5189
|
-
ip() {
|
|
5190
|
-
const IPv4regex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
|
|
5191
|
-
const IPv6regex = /^(?:([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|:(:[0-9a-fA-F]{1,4}){1,7}|([0-9a-fA-F]{1,4}:){1,6}:([0-9a-fA-F]{1,4}:){1,6}[0-9a-fA-F]{1,4})$/;
|
|
5192
|
-
return this.validation("ip", (value) => {
|
|
5193
|
-
if (!(IPv4regex.test(value) || IPv6regex.test(value))) {
|
|
5194
|
-
return {
|
|
5195
|
-
currentIP: value
|
|
5196
|
-
};
|
|
5197
|
-
}
|
|
5198
|
-
});
|
|
5199
|
-
}
|
|
5200
|
-
validation(name, validator) {
|
|
5201
|
-
const instance = this.pipeValidation(name, validator);
|
|
5202
|
-
return instance;
|
|
5203
|
-
}
|
|
5204
|
-
}
|
|
5205
|
-
function string() {
|
|
5206
|
-
return new ArcString;
|
|
5207
|
-
}
|
|
5208
|
-
|
|
5209
|
-
class ArcCustomId extends ArcBranded {
|
|
5210
|
-
createFn;
|
|
5211
|
-
constructor(name, createFn) {
|
|
5212
|
-
super(string(), name);
|
|
5213
|
-
this.createFn = createFn;
|
|
5214
|
-
}
|
|
5215
|
-
get(...args) {
|
|
5216
|
-
return this.createFn(...args);
|
|
5217
|
-
}
|
|
5218
|
-
}
|
|
5219
|
-
|
|
5220
|
-
class ArcId extends ArcBranded {
|
|
5221
|
-
generateFn;
|
|
5222
|
-
constructor(name, generateFn) {
|
|
5223
|
-
super(string(), name);
|
|
5224
|
-
this.generateFn = generateFn;
|
|
5225
|
-
}
|
|
5226
|
-
generate() {
|
|
5227
|
-
if (this.generateFn) {
|
|
5228
|
-
return this.generateFn();
|
|
5229
|
-
}
|
|
5230
|
-
var timestamp = (new Date().getTime() / 1000 | 0).toString(16);
|
|
5231
|
-
return timestamp + "xxxxxxxxxxxxxxxx".replace(/[x]/g, function() {
|
|
5232
|
-
return (Math.random() * 16 | 0).toString(16);
|
|
5233
|
-
}).toLowerCase();
|
|
5234
|
-
}
|
|
5235
|
-
}
|
|
5236
|
-
function id(name, generateFn) {
|
|
5237
|
-
return new ArcId(name, generateFn);
|
|
5238
|
-
}
|
|
5239
|
-
var objectValidator = typeValidatorBuilder("object");
|
|
5240
|
-
|
|
5241
|
-
class ArcObject extends ArcAbstract {
|
|
5242
|
-
rawShape;
|
|
5243
|
-
constructor(rawShape) {
|
|
5244
|
-
super([
|
|
5245
|
-
objectValidator,
|
|
5246
|
-
{
|
|
5247
|
-
name: "schema",
|
|
5248
|
-
validator: (value) => {
|
|
5249
|
-
const errors = Object.entries(this.rawShape).reduce((acc, [key, element]) => {
|
|
5250
|
-
if (!element.validate) {
|
|
5251
|
-
return acc;
|
|
5252
|
-
}
|
|
5253
|
-
acc[key] = element.validate(value[key]);
|
|
5254
|
-
return acc;
|
|
5255
|
-
}, {});
|
|
5256
|
-
if (Object.values(errors).some((result) => !!result)) {
|
|
5257
|
-
return errors;
|
|
5258
|
-
}
|
|
5259
|
-
return false;
|
|
5260
|
-
}
|
|
5261
|
-
}
|
|
5262
|
-
]);
|
|
5263
|
-
this.rawShape = rawShape;
|
|
5264
|
-
}
|
|
5265
|
-
parse(value) {
|
|
5266
|
-
return Object.entries(this.rawShape).reduce((acc, [key, element]) => {
|
|
5267
|
-
acc[key] = element.parse(value[key]);
|
|
5268
|
-
return acc;
|
|
5269
|
-
}, {});
|
|
5270
|
-
}
|
|
5271
|
-
serialize(value) {
|
|
5272
|
-
return Object.entries(value).reduce((acc, [key, value2]) => {
|
|
5273
|
-
if (!this.rawShape[key]) {
|
|
5274
|
-
acc[key] = value2;
|
|
5275
|
-
} else {
|
|
5276
|
-
acc[key] = this.rawShape[key].serialize(value2);
|
|
5277
|
-
}
|
|
5278
|
-
return acc;
|
|
5279
|
-
}, {});
|
|
5280
|
-
}
|
|
5281
|
-
deserialize(value) {
|
|
5282
|
-
return Object.fromEntries(Object.entries(this.rawShape).map(([key, element]) => [
|
|
5283
|
-
key,
|
|
5284
|
-
element.deserialize(value[key])
|
|
5285
|
-
]));
|
|
5286
|
-
}
|
|
5287
|
-
deserializePath(path, value) {
|
|
5288
|
-
if (path.length === 0) {
|
|
5289
|
-
return this.deserialize(value);
|
|
5290
|
-
}
|
|
5291
|
-
const [key, ...restPath] = path;
|
|
5292
|
-
const element = this.rawShape[key];
|
|
5293
|
-
if (!element) {
|
|
5294
|
-
console.warn(`No element found for key: ${key}`);
|
|
5295
|
-
return value;
|
|
5296
|
-
}
|
|
5297
|
-
if (element instanceof ArcObject || element instanceof ArcArray) {
|
|
5298
|
-
return element.deserializePath(restPath, value);
|
|
5299
|
-
}
|
|
5300
|
-
return element.deserialize(value);
|
|
5301
|
-
}
|
|
5302
|
-
parsePartial(value) {
|
|
5303
|
-
return Object.entries(value).reduce((acc, [key, value2]) => {
|
|
5304
|
-
acc[key] = this.rawShape[key].parse(value2);
|
|
5305
|
-
return acc;
|
|
5306
|
-
}, {});
|
|
5307
|
-
}
|
|
5308
|
-
deserializePartial(value) {
|
|
5309
|
-
return Object.entries(value).reduce((acc, [key, value2]) => {
|
|
5310
|
-
acc[key] = this.rawShape[key].deserialize(value2);
|
|
5311
|
-
return acc;
|
|
5312
|
-
}, {});
|
|
5313
|
-
}
|
|
5314
|
-
serializePartial(value) {
|
|
5315
|
-
return Object.entries(value).reduce((acc, [key, value2]) => {
|
|
5316
|
-
acc[key] = this.rawShape[key].serialize(value2);
|
|
5317
|
-
return acc;
|
|
5318
|
-
}, {});
|
|
5319
|
-
}
|
|
5320
|
-
validatePartial(value) {
|
|
5321
|
-
const errors = Object.entries(value).reduce((acc, [key, val]) => {
|
|
5322
|
-
if (!this.rawShape[key]) {
|
|
5323
|
-
return acc;
|
|
5324
|
-
}
|
|
5325
|
-
const result = this.rawShape[key].validate(val);
|
|
5326
|
-
if (result) {
|
|
5327
|
-
acc[key] = result;
|
|
5328
|
-
}
|
|
5329
|
-
return acc;
|
|
5330
|
-
}, {});
|
|
5331
|
-
if (Object.keys(errors).length > 0) {
|
|
5332
|
-
return { schema: errors };
|
|
5333
|
-
}
|
|
5334
|
-
return false;
|
|
5335
|
-
}
|
|
5336
|
-
pick(...keys) {
|
|
5337
|
-
return new ArcObject(Object.fromEntries(keys.map((key) => [key, this.rawShape[key]])));
|
|
5338
|
-
}
|
|
5339
|
-
omit(...keys) {
|
|
5340
|
-
return new ArcObject(Object.fromEntries(Object.entries(this.rawShape).filter(([key]) => !keys.includes(key))));
|
|
5341
|
-
}
|
|
5342
|
-
entries() {
|
|
5343
|
-
return Object.entries(this.rawShape);
|
|
5344
|
-
}
|
|
5345
|
-
toJsonSchema() {
|
|
5346
|
-
const properties = {};
|
|
5347
|
-
const required = [];
|
|
5348
|
-
for (const [key, element] of Object.entries(this.rawShape)) {
|
|
5349
|
-
properties[key] = element.toJsonSchema?.() ?? {};
|
|
5350
|
-
if (!(element instanceof ArcOptional)) {
|
|
5351
|
-
required.push(key);
|
|
5352
|
-
}
|
|
5353
|
-
}
|
|
5354
|
-
const schema = {
|
|
5355
|
-
type: "object",
|
|
5356
|
-
properties
|
|
5357
|
-
};
|
|
5358
|
-
if (required.length)
|
|
5359
|
-
schema.required = required;
|
|
5360
|
-
return schema;
|
|
5361
|
-
}
|
|
5362
|
-
}
|
|
5363
|
-
var arrayValidator = typeValidatorBuilder("array", Array.isArray);
|
|
5364
|
-
|
|
5365
|
-
class ArcArray extends ArcAbstract {
|
|
5366
|
-
parent;
|
|
5367
|
-
constructor(parent) {
|
|
5368
|
-
super([
|
|
5369
|
-
arrayValidator,
|
|
5370
|
-
{
|
|
5371
|
-
name: "schema",
|
|
5372
|
-
validator: (values) => {
|
|
5373
|
-
const errors = values.reduce((acc, value, index) => {
|
|
5374
|
-
const error = parent.validate(value);
|
|
5375
|
-
if (error)
|
|
5376
|
-
acc[index] = error;
|
|
5377
|
-
return acc;
|
|
5378
|
-
}, {});
|
|
5379
|
-
if (Object.values(errors).some((result) => !!result)) {
|
|
5380
|
-
return errors;
|
|
5381
|
-
}
|
|
5382
|
-
return false;
|
|
5383
|
-
}
|
|
5384
|
-
}
|
|
5385
|
-
]);
|
|
5386
|
-
this.parent = parent;
|
|
5387
|
-
}
|
|
5388
|
-
minLength(min) {
|
|
5389
|
-
return this.validation("minLength", (value) => {
|
|
5390
|
-
if (value.length < min)
|
|
5391
|
-
return {
|
|
5392
|
-
currentLength: value.length,
|
|
5393
|
-
minLength: min
|
|
5394
|
-
};
|
|
5395
|
-
});
|
|
5396
|
-
}
|
|
5397
|
-
maxLength(max) {
|
|
5398
|
-
return this.validation("maxLength", (value) => {
|
|
5399
|
-
if (value.length > max)
|
|
5400
|
-
return {
|
|
5401
|
-
currentLength: value.length,
|
|
5402
|
-
maxLength: max
|
|
5403
|
-
};
|
|
5404
|
-
});
|
|
5405
|
-
}
|
|
5406
|
-
length(number) {
|
|
5407
|
-
return this.validation("length", (value) => {
|
|
5408
|
-
if (value.length !== number) {
|
|
5409
|
-
return {
|
|
5410
|
-
currentLength: value.length,
|
|
5411
|
-
length: number
|
|
5412
|
-
};
|
|
5413
|
-
}
|
|
5414
|
-
});
|
|
5415
|
-
}
|
|
5416
|
-
nonEmpty() {
|
|
5417
|
-
return this.validation("nonEmpty", (value) => {
|
|
5418
|
-
if (value.length === 0)
|
|
5419
|
-
return { msg: "array is empty" };
|
|
5420
|
-
});
|
|
5421
|
-
}
|
|
5422
|
-
parse(value) {
|
|
5423
|
-
return value.map((v) => this.parent.parse(v));
|
|
5424
|
-
}
|
|
5425
|
-
serialize(value) {
|
|
5426
|
-
return value.map((v) => this.parent.serialize(v));
|
|
5427
|
-
}
|
|
5428
|
-
deserialize(value) {
|
|
5429
|
-
if (!Array.isArray(value))
|
|
5430
|
-
return [];
|
|
5431
|
-
return value.map((v) => this.parent.deserialize(v));
|
|
5432
|
-
}
|
|
5433
|
-
deserializePath(path, value) {
|
|
5434
|
-
if (path.length === 0) {
|
|
5435
|
-
return this.deserialize(value);
|
|
5436
|
-
}
|
|
5437
|
-
if (this.parent instanceof ArcObject || this.parent instanceof ArcArray) {
|
|
5438
|
-
return this.parent.deserializePath(path, value);
|
|
5439
|
-
}
|
|
5440
|
-
return this.parent.deserialize(value);
|
|
5441
|
-
}
|
|
5442
|
-
validation(name, validator) {
|
|
5443
|
-
const instance = this.pipeValidation(name, validator);
|
|
5444
|
-
return instance;
|
|
5445
|
-
}
|
|
5446
|
-
toJsonSchema() {
|
|
5447
|
-
return {
|
|
5448
|
-
type: "array",
|
|
5449
|
-
items: this.parent.toJsonSchema?.() ?? {}
|
|
5450
|
-
};
|
|
5451
|
-
}
|
|
5452
|
-
}
|
|
5453
|
-
class ArcBoolean extends ArcPrimitive {
|
|
5454
|
-
hasToBeTrue() {
|
|
5455
|
-
return this.validation("hasToBeTrue", (value) => {
|
|
5456
|
-
if (!value)
|
|
5457
|
-
return {
|
|
5458
|
-
current: value
|
|
5459
|
-
};
|
|
5460
|
-
});
|
|
5461
|
-
}
|
|
5462
|
-
validation(name, validator) {
|
|
5463
|
-
const instance = this.pipeValidation(name, validator);
|
|
5464
|
-
return instance;
|
|
5465
|
-
}
|
|
5466
|
-
toJsonSchema() {
|
|
5467
|
-
return { type: "boolean" };
|
|
5468
|
-
}
|
|
5469
|
-
}
|
|
5470
|
-
var dateValidator = typeValidatorBuilder("Date", (value) => value instanceof Date);
|
|
5471
|
-
|
|
5472
|
-
class ArcDate extends ArcAbstract {
|
|
5473
|
-
constructor() {
|
|
5474
|
-
super([dateValidator]);
|
|
5475
|
-
}
|
|
5476
|
-
parse(value) {
|
|
5477
|
-
return new Date(value);
|
|
5478
|
-
}
|
|
5479
|
-
serialize(value) {
|
|
5480
|
-
if (value instanceof Date) {
|
|
5481
|
-
return value.getTime();
|
|
5482
|
-
}
|
|
5483
|
-
return value;
|
|
5484
|
-
}
|
|
5485
|
-
deserialize(value) {
|
|
5486
|
-
return new Date(value);
|
|
5487
|
-
}
|
|
5488
|
-
after(after) {
|
|
5489
|
-
return this.validation("after", (value) => {
|
|
5490
|
-
if (value <= after)
|
|
5491
|
-
return { current: value, after };
|
|
5492
|
-
});
|
|
5493
|
-
}
|
|
5494
|
-
before(before) {
|
|
5495
|
-
return this.validation("before", (value) => {
|
|
5496
|
-
if (value >= before)
|
|
5497
|
-
return { current: value, before };
|
|
5498
|
-
});
|
|
5499
|
-
}
|
|
5500
|
-
validation(name, validator) {
|
|
5501
|
-
const instance = this.pipeValidation(name, validator);
|
|
5502
|
-
return instance;
|
|
5503
|
-
}
|
|
5504
|
-
toJsonSchema() {
|
|
5505
|
-
return {
|
|
5506
|
-
type: "string",
|
|
5507
|
-
format: "date-time"
|
|
5508
|
-
};
|
|
5509
|
-
}
|
|
5510
|
-
}
|
|
5511
|
-
var numberValidator = typeValidatorBuilder("number");
|
|
5512
|
-
|
|
5513
|
-
class ArcNumber extends ArcPrimitive {
|
|
5514
|
-
constructor() {
|
|
5515
|
-
super([numberValidator]);
|
|
5516
|
-
}
|
|
5517
|
-
min(min) {
|
|
5518
|
-
return this.validation("min", (value) => {
|
|
5519
|
-
if (value < min)
|
|
5520
|
-
return { current: value, min };
|
|
5521
|
-
});
|
|
5522
|
-
}
|
|
5523
|
-
max(max) {
|
|
5524
|
-
return this.validation("max", (value) => {
|
|
5525
|
-
if (value > max)
|
|
5526
|
-
return { current: value, max };
|
|
5527
|
-
});
|
|
5528
|
-
}
|
|
5529
|
-
validation(name, validator) {
|
|
5530
|
-
const instance = this.pipeValidation(name, validator);
|
|
5531
|
-
return instance;
|
|
5532
|
-
}
|
|
5533
|
-
toJsonSchema() {
|
|
5534
|
-
return { type: "number" };
|
|
5535
|
-
}
|
|
5536
|
-
}
|
|
5537
|
-
function getSQLiteType(element2) {
|
|
5538
|
-
if (element2 instanceof ArcDate) {
|
|
5539
|
-
return "TEXT";
|
|
5540
|
-
}
|
|
5541
|
-
if (element2 instanceof ArcArray || element2 instanceof ArcObject) {
|
|
5542
|
-
return "TEXT";
|
|
5543
|
-
}
|
|
5544
|
-
if (element2 instanceof ArcBoolean) {
|
|
5545
|
-
return "INTEGER";
|
|
5546
|
-
}
|
|
5547
|
-
if (element2 instanceof ArcNumber) {
|
|
5548
|
-
return "NUMBER";
|
|
5549
|
-
}
|
|
5550
|
-
return "TEXT";
|
|
5551
|
-
}
|
|
5552
|
-
function arcObjectToStoreSchema(tableName, schema) {
|
|
5553
|
-
const columns = schema.entries().map(([name, element2]) => ({
|
|
5554
|
-
name,
|
|
5555
|
-
type: getSQLiteType(element2),
|
|
5556
|
-
isOptional: element2 instanceof ArcOptional
|
|
5557
|
-
}));
|
|
5558
|
-
columns.unshift({
|
|
5559
|
-
name: "_id",
|
|
5560
|
-
type: "TEXT",
|
|
5561
|
-
isOptional: false
|
|
5562
|
-
}, {
|
|
5563
|
-
name: "deleted",
|
|
5564
|
-
type: "INTEGER",
|
|
5565
|
-
isOptional: false,
|
|
5566
|
-
default: 0
|
|
5567
|
-
}, {
|
|
5568
|
-
name: "lastUpdate",
|
|
5569
|
-
type: "TEXT",
|
|
5570
|
-
isOptional: false
|
|
5571
|
-
});
|
|
5572
|
-
return {
|
|
5573
|
-
tables: [
|
|
5574
|
-
{
|
|
5575
|
-
name: tableName,
|
|
5576
|
-
columns,
|
|
5577
|
-
primaryKey: "_id"
|
|
5578
|
-
}
|
|
5579
|
-
]
|
|
5580
|
-
};
|
|
5581
|
-
}
|
|
5582
|
-
|
|
5583
|
-
class ArcQuery {
|
|
5584
|
-
lastResult;
|
|
5585
|
-
listeners = new Set;
|
|
5586
|
-
subscribe(listener) {
|
|
5587
|
-
this.listeners.add(listener);
|
|
5588
|
-
}
|
|
5589
|
-
unsubscribe(listener) {
|
|
5590
|
-
this.listeners.delete(listener);
|
|
5591
|
-
}
|
|
5592
|
-
nextResult(result) {
|
|
5593
|
-
this.lastResult = result;
|
|
5594
|
-
this.listeners.forEach((listener) => listener(result));
|
|
5595
|
-
}
|
|
5596
|
-
}
|
|
5597
|
-
|
|
5598
|
-
class ArcSerializableQuery extends ArcQuery {
|
|
5599
|
-
params;
|
|
5600
|
-
static key;
|
|
5601
|
-
constructor(params) {
|
|
5602
|
-
super();
|
|
5603
|
-
this.params = params;
|
|
5604
|
-
}
|
|
5605
|
-
serialize() {
|
|
5606
|
-
return {
|
|
5607
|
-
key: this.constructor.key,
|
|
5608
|
-
params: this.params
|
|
5609
|
-
};
|
|
5610
|
-
}
|
|
5611
|
-
}
|
|
5612
|
-
|
|
5613
|
-
class ArcCollectionQuery extends ArcSerializableQuery {
|
|
5614
|
-
collection;
|
|
5615
|
-
bindedChangeHandler = this.changeHandler.bind(this);
|
|
5616
|
-
store;
|
|
5617
|
-
constructor(collection, params) {
|
|
5618
|
-
super(params);
|
|
5619
|
-
this.collection = collection;
|
|
5620
|
-
}
|
|
5621
|
-
async run(dataStorage) {
|
|
5622
|
-
const store = dataStorage.getStore(this.collection.name);
|
|
5623
|
-
this.store = store;
|
|
5624
|
-
const result = await this.fetch(store);
|
|
5625
|
-
this.lastResult = result;
|
|
5626
|
-
return result;
|
|
5627
|
-
}
|
|
5628
|
-
changeHandler(changes) {
|
|
5629
|
-
let resultChanged = false;
|
|
5630
|
-
for (const change of changes) {
|
|
5631
|
-
const response = this.onChange(change);
|
|
5632
|
-
if (response !== false) {
|
|
5633
|
-
this.lastResult = response;
|
|
5634
|
-
resultChanged = true;
|
|
5635
|
-
}
|
|
5636
|
-
}
|
|
5637
|
-
if (resultChanged)
|
|
5638
|
-
this.nextResult(this.lastResult);
|
|
5639
|
-
}
|
|
5640
|
-
}
|
|
5641
|
-
|
|
5642
|
-
class QueryCollectionResult {
|
|
5643
|
-
result;
|
|
5644
|
-
constructor(result) {
|
|
5645
|
-
this.result = result;
|
|
5646
|
-
}
|
|
5647
|
-
get(id3) {
|
|
5648
|
-
return id3 ? this.result.find((r) => r._id === id3) : undefined;
|
|
5649
|
-
}
|
|
5650
|
-
map(callbackfn) {
|
|
5651
|
-
return this.result.map(callbackfn);
|
|
5652
|
-
}
|
|
5653
|
-
toArray() {
|
|
5654
|
-
return this.result;
|
|
5655
|
-
}
|
|
5656
|
-
}
|
|
5657
|
-
|
|
5658
|
-
class ArcFindQuery extends ArcCollectionQuery {
|
|
5659
|
-
params;
|
|
5660
|
-
constructor(collection, params) {
|
|
5661
|
-
super(collection, params);
|
|
5662
|
-
this.params = params;
|
|
5663
|
-
}
|
|
5664
|
-
async fetch(store) {
|
|
5665
|
-
const results = await store.find(this.params, this.bindedChangeHandler);
|
|
5666
|
-
return this.createResult(results);
|
|
5667
|
-
}
|
|
5668
|
-
checkItem(item) {
|
|
5669
|
-
if (!this.params.where)
|
|
5670
|
-
return true;
|
|
5671
|
-
return Object.entries(this.params.where).every(([key, condition]) => {
|
|
5672
|
-
const value = item[key];
|
|
5673
|
-
if (typeof condition !== "object" || condition === null) {
|
|
5674
|
-
return value === condition;
|
|
5675
|
-
}
|
|
5676
|
-
return Object.entries(condition).every(([operator, operatorValue]) => {
|
|
5677
|
-
switch (operator) {
|
|
5678
|
-
case "$eq":
|
|
5679
|
-
return value === operatorValue;
|
|
5680
|
-
case "$ne":
|
|
5681
|
-
return value !== operatorValue;
|
|
5682
|
-
case "$gt":
|
|
5683
|
-
return typeof value === "number" && typeof operatorValue === "number" && value > operatorValue;
|
|
5684
|
-
case "$gte":
|
|
5685
|
-
return typeof value === "number" && typeof operatorValue === "number" && value >= operatorValue;
|
|
5686
|
-
case "$lt":
|
|
5687
|
-
return typeof value === "number" && typeof operatorValue === "number" && value < operatorValue;
|
|
5688
|
-
case "$lte":
|
|
5689
|
-
return typeof value === "number" && typeof operatorValue === "number" && value <= operatorValue;
|
|
5690
|
-
case "$in":
|
|
5691
|
-
return Array.isArray(operatorValue) && operatorValue.includes(value);
|
|
5692
|
-
case "$nin":
|
|
5693
|
-
return Array.isArray(operatorValue) && !operatorValue.includes(value);
|
|
5694
|
-
case "$exists":
|
|
5695
|
-
return typeof operatorValue === "boolean" ? operatorValue ? value !== undefined : value === undefined : true;
|
|
5696
|
-
default:
|
|
5697
|
-
return true;
|
|
5698
|
-
}
|
|
5699
|
-
});
|
|
5700
|
-
});
|
|
5701
|
-
}
|
|
5702
|
-
onChange(change) {
|
|
5703
|
-
const lastResult = this.lastResult;
|
|
5704
|
-
const lastResultAsArray = lastResult || [];
|
|
5705
|
-
const index = lastResultAsArray.findIndex((e) => e._id === (change.type === "delete" ? change.id : change.id));
|
|
5706
|
-
const isInLastResult = index !== -1;
|
|
5707
|
-
const shouldBeInTheResult = change.type !== "delete" && this.checkItem(change.item);
|
|
5708
|
-
if (isInLastResult && shouldBeInTheResult)
|
|
5709
|
-
return this.createResult(lastResultAsArray.toSpliced(index, 1, change.item));
|
|
5710
|
-
else if (isInLastResult && (change.type === "delete" || !shouldBeInTheResult))
|
|
5711
|
-
return this.createResult(lastResultAsArray.toSpliced(index, 1));
|
|
5712
|
-
else if (!isInLastResult && shouldBeInTheResult)
|
|
5713
|
-
return this.createResult(lastResultAsArray.concat(change.item));
|
|
5714
|
-
return false;
|
|
5715
|
-
}
|
|
5716
|
-
createResult(result) {
|
|
5717
|
-
return new QueryCollectionResult(result);
|
|
5718
|
-
}
|
|
5719
|
-
}
|
|
5720
|
-
|
|
5721
|
-
class ArcFindQueryBuilder {
|
|
5722
|
-
collection;
|
|
5723
|
-
queryContext;
|
|
5724
|
-
options;
|
|
5725
|
-
constructor(collection, queryContext, options) {
|
|
5726
|
-
this.collection = collection;
|
|
5727
|
-
this.queryContext = queryContext;
|
|
5728
|
-
this.options = options;
|
|
5729
|
-
}
|
|
5730
|
-
toQuery() {
|
|
5731
|
-
return this.queryContext.cacheQuery(ArcFindQuery, [
|
|
5732
|
-
this.collection,
|
|
5733
|
-
this.options
|
|
5734
|
-
]);
|
|
5735
|
-
}
|
|
5736
|
-
run() {
|
|
5737
|
-
return this.queryContext.runQuery(this.toQuery());
|
|
5738
|
-
}
|
|
5739
|
-
}
|
|
5740
|
-
|
|
5741
|
-
class ArcCollection extends ArcContextElementWithStore {
|
|
5742
|
-
name;
|
|
5743
|
-
id;
|
|
5744
|
-
schema;
|
|
5745
|
-
options;
|
|
5746
|
-
_restrictions;
|
|
5747
|
-
constructor(name, id3, schema, options) {
|
|
5748
|
-
super();
|
|
5749
|
-
this.name = name;
|
|
5750
|
-
this.id = id3;
|
|
5751
|
-
this.schema = schema;
|
|
5752
|
-
this.options = options;
|
|
5753
|
-
}
|
|
5754
|
-
storeSchema() {
|
|
5755
|
-
return arcObjectToStoreSchema(this.name, this.schema);
|
|
5756
|
-
}
|
|
5757
|
-
restrictions(authContext) {
|
|
5758
|
-
if (this._restrictions) {
|
|
5759
|
-
return this._restrictions(authContext);
|
|
5760
|
-
}
|
|
5761
|
-
return {
|
|
5762
|
-
read: false,
|
|
5763
|
-
write: false,
|
|
5764
|
-
modify: false,
|
|
5765
|
-
delete: false
|
|
5766
|
-
};
|
|
5767
|
-
}
|
|
5768
|
-
auth(restrictionsFn) {
|
|
5769
|
-
const collection = new ArcCollection(this.name, this.id, this.schema, this.options);
|
|
5770
|
-
collection._restrictions = restrictionsFn;
|
|
5771
|
-
return collection;
|
|
5772
|
-
}
|
|
5773
|
-
serialize(data) {
|
|
5774
|
-
return {
|
|
5775
|
-
_id: this.id.serialize(data._id),
|
|
5776
|
-
...this.schema.serialize(data)
|
|
5777
|
-
};
|
|
5778
|
-
}
|
|
5779
|
-
deserialize(data) {
|
|
5780
|
-
return {
|
|
5781
|
-
_id: this.id.deserialize(data._id),
|
|
5782
|
-
...this.schema.deserialize(data)
|
|
5783
|
-
};
|
|
5784
|
-
}
|
|
5785
|
-
queryBuilder = (context) => {
|
|
5786
|
-
return {
|
|
5787
|
-
find: (options) => new ArcFindQueryBuilder(this, context, options || {})
|
|
5788
|
-
};
|
|
5789
|
-
};
|
|
5790
|
-
commandContext = (dataStorage, publishEvent) => {
|
|
5791
|
-
const store = dataStorage.getStore(this.name);
|
|
5792
|
-
return {
|
|
5793
|
-
add: async (data) => {
|
|
5794
|
-
if (this.id instanceof ArcCustomId)
|
|
5795
|
-
throw new Error("Collection with custom id not support 'add' method");
|
|
5796
|
-
const id3 = this.id.generate();
|
|
5797
|
-
const parsed = this.schema.parse(data);
|
|
5798
|
-
const body = {
|
|
5799
|
-
_id: id3,
|
|
5800
|
-
lastUpdate: new Date().toISOString(),
|
|
5801
|
-
...parsed
|
|
5802
|
-
};
|
|
5803
|
-
await store.set(body);
|
|
5804
|
-
await publishEvent({
|
|
5805
|
-
type: "set",
|
|
5806
|
-
to: body
|
|
5807
|
-
});
|
|
5808
|
-
return { id: id3 };
|
|
5809
|
-
},
|
|
5810
|
-
remove: async (id3) => {
|
|
5811
|
-
await store.remove(id3);
|
|
5812
|
-
return { success: true };
|
|
5813
|
-
},
|
|
5814
|
-
set: async (id3, data) => {
|
|
5815
|
-
const parsed = this.schema.parse(data);
|
|
5816
|
-
const body = {
|
|
5817
|
-
_id: id3,
|
|
5818
|
-
...parsed
|
|
5819
|
-
};
|
|
5820
|
-
await store.set(body);
|
|
5821
|
-
await publishEvent({
|
|
5822
|
-
type: "set",
|
|
5823
|
-
to: body
|
|
5824
|
-
});
|
|
5825
|
-
return { success: true };
|
|
5826
|
-
},
|
|
5827
|
-
find: async (options) => {
|
|
5828
|
-
return store.find(options);
|
|
5829
|
-
},
|
|
5830
|
-
findOne: async (where) => {
|
|
5831
|
-
const result = await store.find({
|
|
5832
|
-
where,
|
|
5833
|
-
limit: 1
|
|
5834
|
-
});
|
|
5835
|
-
return result[0];
|
|
5836
|
-
},
|
|
5837
|
-
modify: async (id3, data) => {
|
|
5838
|
-
const deserialized = this.schema.serializePartial(data);
|
|
5839
|
-
const { from, to } = await store.modify(id3, deserialized);
|
|
5840
|
-
await publishEvent({
|
|
5841
|
-
type: "modify",
|
|
5842
|
-
changes: deserialized,
|
|
5843
|
-
from,
|
|
5844
|
-
to
|
|
5845
|
-
});
|
|
5846
|
-
},
|
|
5847
|
-
edit: async (id3, editCallback) => {
|
|
5848
|
-
const { from, to } = await store.mutate(id3, editCallback);
|
|
5849
|
-
await publishEvent({
|
|
5850
|
-
type: "mutate",
|
|
5851
|
-
from,
|
|
5852
|
-
to
|
|
5853
|
-
});
|
|
5854
|
-
}
|
|
5855
|
-
};
|
|
5856
|
-
};
|
|
5857
|
-
}
|
|
5858
|
-
var eventValidator = typeValidatorBuilder("object");
|
|
5859
|
-
var eventId = id("event");
|
|
5860
|
-
class QueryBuilderContext {
|
|
5861
|
-
queryCache;
|
|
5862
|
-
dataStorage;
|
|
5863
|
-
usedQueries = new Set;
|
|
5864
|
-
constructor(queryCache, dataStorage) {
|
|
5865
|
-
this.queryCache = queryCache;
|
|
5866
|
-
this.dataStorage = dataStorage;
|
|
5867
|
-
}
|
|
5868
|
-
cacheQuery(QueryConstructor, args) {
|
|
5869
|
-
return new QueryConstructor(...args);
|
|
5870
|
-
}
|
|
5871
|
-
runQuery(query) {
|
|
5872
|
-
this.usedQueries.add(query);
|
|
5873
|
-
return query.run(this.dataStorage);
|
|
5874
|
-
}
|
|
5875
|
-
getUsedQueries() {
|
|
5876
|
-
const queries = Array.from(this.usedQueries);
|
|
5877
|
-
this.usedQueries.clear();
|
|
5878
|
-
return queries;
|
|
5879
|
-
}
|
|
5880
|
-
}
|
|
5881
|
-
function murmurHash(key, seed = 0) {
|
|
5882
|
-
var remainder, bytes, h1, h1b, c1, c1b, c2, c2b, k1, i;
|
|
5883
|
-
remainder = key.length & 3;
|
|
5884
|
-
bytes = key.length - remainder;
|
|
5885
|
-
h1 = seed;
|
|
5886
|
-
c1 = 3432918353;
|
|
5887
|
-
c2 = 461845907;
|
|
5888
|
-
i = 0;
|
|
5889
|
-
while (i < bytes) {
|
|
5890
|
-
k1 = key.charCodeAt(i) & 255 | (key.charCodeAt(++i) & 255) << 8 | (key.charCodeAt(++i) & 255) << 16 | (key.charCodeAt(++i) & 255) << 24;
|
|
5891
|
-
++i;
|
|
5892
|
-
k1 = (k1 & 65535) * c1 + (((k1 >>> 16) * c1 & 65535) << 16) & 4294967295;
|
|
5893
|
-
k1 = k1 << 15 | k1 >>> 17;
|
|
5894
|
-
k1 = (k1 & 65535) * c2 + (((k1 >>> 16) * c2 & 65535) << 16) & 4294967295;
|
|
5895
|
-
h1 ^= k1;
|
|
5896
|
-
h1 = h1 << 13 | h1 >>> 19;
|
|
5897
|
-
h1b = (h1 & 65535) * 5 + (((h1 >>> 16) * 5 & 65535) << 16) & 4294967295;
|
|
5898
|
-
h1 = (h1b & 65535) + 27492 + (((h1b >>> 16) + 58964 & 65535) << 16);
|
|
5899
|
-
}
|
|
5900
|
-
k1 = 0;
|
|
5901
|
-
switch (remainder) {
|
|
5902
|
-
case 3:
|
|
5903
|
-
k1 ^= (key.charCodeAt(i + 2) & 255) << 16;
|
|
5904
|
-
case 2:
|
|
5905
|
-
k1 ^= (key.charCodeAt(i + 1) & 255) << 8;
|
|
5906
|
-
case 1:
|
|
5907
|
-
k1 ^= key.charCodeAt(i) & 255;
|
|
5908
|
-
k1 = (k1 & 65535) * c1 + (((k1 >>> 16) * c1 & 65535) << 16) & 4294967295;
|
|
5909
|
-
k1 = k1 << 15 | k1 >>> 17;
|
|
5910
|
-
k1 = (k1 & 65535) * c2 + (((k1 >>> 16) * c2 & 65535) << 16) & 4294967295;
|
|
5911
|
-
h1 ^= k1;
|
|
5912
|
-
}
|
|
5913
|
-
h1 ^= key.length;
|
|
5914
|
-
h1 ^= h1 >>> 16;
|
|
5915
|
-
h1 = (h1 & 65535) * 2246822507 + (((h1 >>> 16) * 2246822507 & 65535) << 16) & 4294967295;
|
|
5916
|
-
h1 ^= h1 >>> 13;
|
|
5917
|
-
h1 = (h1 & 65535) * 3266489909 + (((h1 >>> 16) * 3266489909 & 65535) << 16) & 4294967295;
|
|
5918
|
-
h1 ^= h1 >>> 16;
|
|
5919
|
-
return h1 >>> 0;
|
|
5920
|
-
}
|
|
5921
|
-
|
|
5922
|
-
class QueryCache {
|
|
5923
|
-
cache = new Map;
|
|
5924
|
-
getIndexHash(value) {
|
|
5925
|
-
if (typeof value === "number" || typeof value === "string" || typeof value === "function" || value === null || value === undefined) {
|
|
5926
|
-
return value;
|
|
5927
|
-
}
|
|
5928
|
-
return murmurHash(value);
|
|
5929
|
-
}
|
|
5930
|
-
set(index, query) {
|
|
5931
|
-
let current2 = this.cache;
|
|
5932
|
-
for (let i = 0;i < index.length - 1; i++) {
|
|
5933
|
-
const hashedIndex = this.getIndexHash(index[i]);
|
|
5934
|
-
if (!current2.has(hashedIndex)) {
|
|
5935
|
-
current2.set(hashedIndex, new Map);
|
|
5936
|
-
}
|
|
5937
|
-
current2 = current2.get(hashedIndex);
|
|
5938
|
-
}
|
|
5939
|
-
const lastHashedIndex = this.getIndexHash(index[index.length - 1]);
|
|
5940
|
-
current2.set(lastHashedIndex, query);
|
|
5941
|
-
}
|
|
5942
|
-
get(index) {
|
|
5943
|
-
let current2 = this.cache;
|
|
5944
|
-
for (let i = 0;i < index.length - 1; i++) {
|
|
5945
|
-
const hashedIndex = this.getIndexHash(index[i]);
|
|
5946
|
-
if (!current2.has(hashedIndex)) {
|
|
5947
|
-
return;
|
|
5948
|
-
}
|
|
5949
|
-
current2 = current2.get(hashedIndex);
|
|
5950
|
-
}
|
|
5951
|
-
const lastHashedIndex = this.getIndexHash(index[index.length - 1]);
|
|
5952
|
-
return current2.get(lastHashedIndex);
|
|
5953
|
-
}
|
|
5954
|
-
debug() {
|
|
5955
|
-
return this.cache;
|
|
5956
|
-
}
|
|
5957
|
-
}
|
|
5958
|
-
|
|
5959
|
-
class ReactiveQuery extends ArcQuery {
|
|
5960
|
-
queryContext;
|
|
5961
|
-
queryBuilder;
|
|
5962
|
-
fn;
|
|
5963
|
-
queries = [];
|
|
5964
|
-
constructor(queryContext, queryBuilder, fn) {
|
|
5965
|
-
super();
|
|
5966
|
-
this.queryContext = queryContext;
|
|
5967
|
-
this.queryBuilder = queryBuilder;
|
|
5968
|
-
this.fn = fn;
|
|
5969
|
-
}
|
|
5970
|
-
run() {
|
|
5971
|
-
const promise = new Promise(async (resolve) => {
|
|
5972
|
-
const value = await this.runQuery();
|
|
5973
|
-
resolve(value);
|
|
5974
|
-
});
|
|
5975
|
-
return promise;
|
|
5976
|
-
}
|
|
5977
|
-
onChangeHandler = this.onChange.bind(this);
|
|
5978
|
-
async onChange() {
|
|
5979
|
-
const value = await this.runQuery();
|
|
5980
|
-
await this.nextResult(value);
|
|
5981
|
-
}
|
|
5982
|
-
async runQuery() {
|
|
5983
|
-
const value = await this.fn(this.queryBuilder);
|
|
5984
|
-
const queries = this.queryContext.getUsedQueries();
|
|
5985
|
-
this.queries.forEach((query) => {
|
|
5986
|
-
if (!queries.includes(query)) {
|
|
5987
|
-
query.unsubscribe(this.onChangeHandler);
|
|
5988
|
-
}
|
|
5989
|
-
});
|
|
5990
|
-
queries.forEach((query) => {
|
|
5991
|
-
if (!this.queries.includes(query)) {
|
|
5992
|
-
query.subscribe(this.onChangeHandler);
|
|
5993
|
-
}
|
|
5994
|
-
});
|
|
5995
|
-
this.queries = queries;
|
|
5996
|
-
return value;
|
|
5997
|
-
}
|
|
5998
|
-
}
|
|
5999
|
-
class DataStorage {
|
|
6000
|
-
async commitChanges(changes) {
|
|
6001
|
-
await Promise.all(changes.map(({ store, changes: changes2 }) => this.getStore(store).applyChanges(changes2)));
|
|
6002
|
-
}
|
|
6003
|
-
}
|
|
6004
|
-
function deepMerge(target, source) {
|
|
6005
|
-
const output = { ...target };
|
|
6006
|
-
for (const key in source) {
|
|
6007
|
-
if (source[key] === undefined) {
|
|
6008
|
-
output[key] = undefined;
|
|
6009
|
-
continue;
|
|
6010
|
-
}
|
|
6011
|
-
if (isObject(source[key]) && isObject(target[key])) {
|
|
6012
|
-
output[key] = deepMerge(target[key], source[key]);
|
|
6013
|
-
} else {
|
|
6014
|
-
output[key] = source[key];
|
|
6015
|
-
}
|
|
6016
|
-
}
|
|
6017
|
-
return output;
|
|
6018
|
-
}
|
|
6019
|
-
function isObject(item) {
|
|
6020
|
-
return item && typeof item === "object" && !Array.isArray(item);
|
|
6021
|
-
}
|
|
6022
|
-
|
|
6023
|
-
class StoreState {
|
|
6024
|
-
storeName;
|
|
6025
|
-
dataStorage;
|
|
6026
|
-
deserialize;
|
|
6027
|
-
listeners = new Map;
|
|
6028
|
-
constructor(storeName, dataStorage, deserialize) {
|
|
6029
|
-
this.storeName = storeName;
|
|
6030
|
-
this.dataStorage = dataStorage;
|
|
6031
|
-
this.deserialize = deserialize;
|
|
6032
|
-
}
|
|
6033
|
-
fork() {
|
|
6034
|
-
return new ForkedStoreState(this.storeName, this.dataStorage, this);
|
|
6035
|
-
}
|
|
6036
|
-
async set(item) {
|
|
6037
|
-
const change = {
|
|
6038
|
-
type: "set",
|
|
6039
|
-
data: item
|
|
6040
|
-
};
|
|
6041
|
-
await this.applyChanges([change]);
|
|
6042
|
-
}
|
|
6043
|
-
async remove(id3) {
|
|
6044
|
-
const change = {
|
|
6045
|
-
type: "delete",
|
|
6046
|
-
id: id3
|
|
6047
|
-
};
|
|
6048
|
-
await this.applyChanges([change]);
|
|
6049
|
-
}
|
|
6050
|
-
async modify(id3, data) {
|
|
6051
|
-
const change = {
|
|
6052
|
-
type: "modify",
|
|
6053
|
-
id: id3,
|
|
6054
|
-
data
|
|
6055
|
-
};
|
|
6056
|
-
const { from, to } = await this.applyChange(change);
|
|
6057
|
-
return { from, to };
|
|
6058
|
-
}
|
|
6059
|
-
async mutate(id3, editCallback) {
|
|
6060
|
-
const result = await this.find({ where: { _id: id3 } });
|
|
6061
|
-
const object3 = result[0];
|
|
6062
|
-
const [draft, finalize] = create(object3 || {}, { enablePatches: true });
|
|
6063
|
-
await editCallback(draft);
|
|
6064
|
-
const [_, patches] = finalize();
|
|
6065
|
-
const change = {
|
|
6066
|
-
type: "mutate",
|
|
6067
|
-
id: id3,
|
|
6068
|
-
patches
|
|
6069
|
-
};
|
|
6070
|
-
const { from, to } = await this.applyChange(change);
|
|
6071
|
-
return { from, to };
|
|
6072
|
-
}
|
|
6073
|
-
applySerializedChanges(changes) {
|
|
6074
|
-
return Promise.all(changes.map((change) => this.applyChange(change)));
|
|
6075
|
-
}
|
|
6076
|
-
unsubscribe(listener) {
|
|
6077
|
-
this.listeners.delete(listener);
|
|
6078
|
-
}
|
|
6079
|
-
notifyListeners(events) {
|
|
6080
|
-
for (const listener of this.listeners.values()) {
|
|
6081
|
-
listener.callback(events);
|
|
6082
|
-
}
|
|
6083
|
-
}
|
|
6084
|
-
}
|
|
6085
|
-
|
|
6086
|
-
class ForkedStoreState extends StoreState {
|
|
6087
|
-
master;
|
|
6088
|
-
changedItems = new Map;
|
|
6089
|
-
changes = [];
|
|
6090
|
-
constructor(storeName, dataStorage, master) {
|
|
6091
|
-
super(storeName, dataStorage, master.deserialize);
|
|
6092
|
-
this.master = master;
|
|
6093
|
-
}
|
|
6094
|
-
async applyChangeAndReturnEvent(change) {
|
|
6095
|
-
this.changes.push(change);
|
|
6096
|
-
if (change.type === "set") {
|
|
6097
|
-
const item = this.deserialize ? this.deserialize(change.data) : change.data;
|
|
6098
|
-
this.changedItems.set(change.data._id, item);
|
|
6099
|
-
return {
|
|
6100
|
-
from: null,
|
|
6101
|
-
to: item,
|
|
6102
|
-
event: {
|
|
6103
|
-
type: "set",
|
|
6104
|
-
item: change.data,
|
|
6105
|
-
id: change.data._id
|
|
6106
|
-
}
|
|
6107
|
-
};
|
|
6108
|
-
}
|
|
6109
|
-
if (change.type === "delete") {
|
|
6110
|
-
const from = await this.find({ where: { _id: change.id } }).then((results) => results[0] || null);
|
|
6111
|
-
this.changedItems.set(change.id, null);
|
|
6112
|
-
return {
|
|
6113
|
-
from,
|
|
6114
|
-
to: null,
|
|
6115
|
-
event: {
|
|
6116
|
-
type: "delete",
|
|
6117
|
-
item: null,
|
|
6118
|
-
id: change.id
|
|
6119
|
-
}
|
|
6120
|
-
};
|
|
6121
|
-
}
|
|
6122
|
-
if (change.type === "modify") {
|
|
6123
|
-
const existing = await this.find({
|
|
6124
|
-
where: { _id: change.id }
|
|
6125
|
-
}).then((results) => results[0]);
|
|
6126
|
-
const updated = existing ? deepMerge(existing, change.data) : { _id: change.id, ...change.data };
|
|
6127
|
-
const item = this.deserialize ? this.deserialize(updated) : updated;
|
|
6128
|
-
this.changedItems.set(change.id, item);
|
|
6129
|
-
return {
|
|
6130
|
-
from: existing || null,
|
|
6131
|
-
to: item,
|
|
6132
|
-
event: {
|
|
6133
|
-
type: "set",
|
|
6134
|
-
item,
|
|
6135
|
-
id: change.id
|
|
6136
|
-
}
|
|
6137
|
-
};
|
|
6138
|
-
}
|
|
6139
|
-
if (change.type === "mutate") {
|
|
6140
|
-
const existing = await this.find({
|
|
6141
|
-
where: { _id: change.id }
|
|
6142
|
-
}).then((results) => results[0]);
|
|
6143
|
-
const updated = apply(existing || {}, change.patches);
|
|
6144
|
-
const item = this.deserialize ? this.deserialize(updated) : updated;
|
|
6145
|
-
this.changedItems.set(change.id, item);
|
|
6146
|
-
return {
|
|
6147
|
-
from: existing || null,
|
|
6148
|
-
to: item,
|
|
6149
|
-
event: {
|
|
6150
|
-
type: "set",
|
|
6151
|
-
item,
|
|
6152
|
-
id: change.id
|
|
6153
|
-
}
|
|
6154
|
-
};
|
|
6155
|
-
}
|
|
6156
|
-
throw new Error("Unknown change type");
|
|
6157
|
-
}
|
|
6158
|
-
async applyChange(change) {
|
|
6159
|
-
const { event: event3, from, to } = await this.applyChangeAndReturnEvent(change);
|
|
6160
|
-
this.notifyListeners([event3]);
|
|
6161
|
-
return { from, to };
|
|
6162
|
-
}
|
|
6163
|
-
async applyChanges(changes) {
|
|
6164
|
-
const events = [];
|
|
6165
|
-
for (const change of changes) {
|
|
6166
|
-
const { event: event3 } = await this.applyChangeAndReturnEvent(change);
|
|
6167
|
-
if (event3)
|
|
6168
|
-
events.push(event3);
|
|
6169
|
-
}
|
|
6170
|
-
if (events.length > 0) {
|
|
6171
|
-
this.notifyListeners(events);
|
|
6172
|
-
}
|
|
6173
|
-
}
|
|
6174
|
-
async find(options, listener) {
|
|
6175
|
-
if (listener) {
|
|
6176
|
-
this.listeners.set(listener, { callback: listener });
|
|
6177
|
-
}
|
|
6178
|
-
const parentResults = await this.master.find(options);
|
|
6179
|
-
const results = new Map;
|
|
6180
|
-
parentResults.forEach((item) => results.set(item._id, item));
|
|
6181
|
-
for (const [id3, changedItem] of this.changedItems) {
|
|
6182
|
-
if (changedItem === null) {
|
|
6183
|
-
results.delete(id3);
|
|
6184
|
-
continue;
|
|
6185
|
-
}
|
|
6186
|
-
const matches = !options.where || Object.entries(options.where).every(([key, value]) => changedItem[key] === value);
|
|
6187
|
-
if (matches) {
|
|
6188
|
-
results.set(id3, changedItem);
|
|
6189
|
-
} else {
|
|
6190
|
-
results.delete(id3);
|
|
6191
|
-
}
|
|
6192
|
-
}
|
|
6193
|
-
return Array.from(results.values());
|
|
6194
|
-
}
|
|
6195
|
-
}
|
|
6196
|
-
|
|
6197
|
-
class ForkedDataStorage extends DataStorage {
|
|
6198
|
-
master;
|
|
6199
|
-
stores = new Map;
|
|
6200
|
-
constructor(master) {
|
|
6201
|
-
super();
|
|
6202
|
-
this.master = master;
|
|
6203
|
-
}
|
|
6204
|
-
getReadTransaction() {
|
|
6205
|
-
return this.master.getReadTransaction();
|
|
6206
|
-
}
|
|
6207
|
-
getReadWriteTransaction() {
|
|
6208
|
-
return this.master.getReadWriteTransaction();
|
|
6209
|
-
}
|
|
6210
|
-
getStore(storeName) {
|
|
6211
|
-
if (this.stores.has(storeName))
|
|
6212
|
-
return this.stores.get(storeName);
|
|
6213
|
-
const masterStorage = this.master.getStore(storeName);
|
|
6214
|
-
const storage = new ForkedStoreState(storeName, this, masterStorage);
|
|
6215
|
-
this.stores.set(storeName, storage);
|
|
6216
|
-
return storage;
|
|
6217
|
-
}
|
|
6218
|
-
fork() {
|
|
6219
|
-
return new ForkedDataStorage(this);
|
|
6220
|
-
}
|
|
6221
|
-
async merge() {
|
|
6222
|
-
const changes = Array.from(this.stores.values()).filter((store) => store.changes.length > 0).map((store) => ({
|
|
6223
|
-
store: store.storeName,
|
|
6224
|
-
changes: store.changes
|
|
6225
|
-
}));
|
|
6226
|
-
await this.master.commitChanges(changes);
|
|
6227
|
-
}
|
|
6228
|
-
}
|
|
6229
|
-
|
|
6230
|
-
class MasterStoreState extends StoreState {
|
|
6231
|
-
constructor(storeName, dataStorage, deserialize) {
|
|
6232
|
-
super(storeName, dataStorage, deserialize);
|
|
6233
|
-
}
|
|
6234
|
-
async applyChangeAndReturnEvent(transaction, change) {
|
|
6235
|
-
if (change.type === "set") {
|
|
6236
|
-
await transaction.set(this.storeName, change.data);
|
|
6237
|
-
const item = this.deserialize ? this.deserialize(change.data) : change.data;
|
|
6238
|
-
return {
|
|
6239
|
-
from: null,
|
|
6240
|
-
to: item,
|
|
6241
|
-
event: {
|
|
6242
|
-
type: "set",
|
|
6243
|
-
item: change.data,
|
|
6244
|
-
id: change.data._id
|
|
6245
|
-
}
|
|
6246
|
-
};
|
|
6247
|
-
}
|
|
6248
|
-
if (change.type === "delete") {
|
|
6249
|
-
await transaction.remove(this.storeName, change.id);
|
|
6250
|
-
return {
|
|
6251
|
-
from: null,
|
|
6252
|
-
to: null,
|
|
6253
|
-
event: {
|
|
6254
|
-
type: "delete",
|
|
6255
|
-
item: null,
|
|
6256
|
-
id: change.id
|
|
6257
|
-
}
|
|
6258
|
-
};
|
|
6259
|
-
}
|
|
6260
|
-
if (change.type === "modify") {
|
|
6261
|
-
const existing = await transaction.find(this.storeName, { where: { _id: change.id } }).then((results) => results[0]);
|
|
6262
|
-
const updated = existing ? deepMerge(existing, change.data) : { _id: change.id, ...change.data };
|
|
6263
|
-
await transaction.set(this.storeName, updated);
|
|
6264
|
-
const item = this.deserialize ? this.deserialize(updated) : updated;
|
|
6265
|
-
return {
|
|
6266
|
-
from: null,
|
|
6267
|
-
to: item,
|
|
6268
|
-
event: {
|
|
6269
|
-
type: "set",
|
|
6270
|
-
item,
|
|
6271
|
-
id: change.id
|
|
6272
|
-
}
|
|
6273
|
-
};
|
|
6274
|
-
}
|
|
6275
|
-
if (change.type === "mutate") {
|
|
6276
|
-
const existing = await transaction.find(this.storeName, { where: { _id: change.id } }).then((results) => results[0]);
|
|
6277
|
-
const updated = apply(existing || {}, change.patches);
|
|
6278
|
-
await transaction.set(this.storeName, updated);
|
|
6279
|
-
const item = this.deserialize ? this.deserialize(updated) : updated;
|
|
6280
|
-
return {
|
|
6281
|
-
from: null,
|
|
6282
|
-
to: item,
|
|
6283
|
-
event: {
|
|
6284
|
-
type: "set",
|
|
6285
|
-
item,
|
|
6286
|
-
id: change.id
|
|
6287
|
-
}
|
|
6288
|
-
};
|
|
6289
|
-
}
|
|
6290
|
-
throw new Error("Unknown change type");
|
|
6291
|
-
}
|
|
6292
|
-
async applyChange(change) {
|
|
6293
|
-
const transaction = await this.dataStorage.getReadWriteTransaction();
|
|
6294
|
-
const { event: event3, from, to } = await this.applyChangeAndReturnEvent(transaction, change);
|
|
6295
|
-
await transaction.commit();
|
|
6296
|
-
this.notifyListeners([event3]);
|
|
6297
|
-
return { from, to };
|
|
6298
|
-
}
|
|
6299
|
-
async applyChanges(changes) {
|
|
6300
|
-
const transaction = await this.dataStorage.getReadWriteTransaction();
|
|
6301
|
-
const events = [];
|
|
6302
|
-
for (const change of changes) {
|
|
6303
|
-
const { event: event3 } = await this.applyChangeAndReturnEvent(transaction, change);
|
|
6304
|
-
if (event3)
|
|
6305
|
-
events.push(event3);
|
|
6306
|
-
}
|
|
6307
|
-
await transaction.commit();
|
|
6308
|
-
if (events.length > 0) {
|
|
6309
|
-
this.notifyListeners(events);
|
|
6310
|
-
}
|
|
6311
|
-
}
|
|
6312
|
-
async find(options, listener) {
|
|
6313
|
-
if (listener) {
|
|
6314
|
-
this.listeners.set(listener, { callback: listener });
|
|
6315
|
-
}
|
|
6316
|
-
const transaction = await this.dataStorage.getReadTransaction();
|
|
6317
|
-
const results = await transaction.find(this.storeName, options);
|
|
6318
|
-
return results.map((item) => this.deserialize ? this.deserialize(item) : item);
|
|
6319
|
-
}
|
|
6320
|
-
}
|
|
6321
|
-
|
|
6322
|
-
class MasterDataStorage extends DataStorage {
|
|
6323
|
-
dbAdapter;
|
|
6324
|
-
arcContext;
|
|
6325
|
-
stores = new Map;
|
|
6326
|
-
rtcAdapter;
|
|
6327
|
-
constructor(dbAdapter, rtcAdapterFactory, arcContext) {
|
|
6328
|
-
super();
|
|
6329
|
-
this.dbAdapter = dbAdapter;
|
|
6330
|
-
this.arcContext = arcContext;
|
|
6331
|
-
this.rtcAdapter = rtcAdapterFactory(this);
|
|
6332
|
-
}
|
|
6333
|
-
async getReadTransaction() {
|
|
6334
|
-
return (await this.dbAdapter).readTransaction();
|
|
6335
|
-
}
|
|
6336
|
-
async getReadWriteTransaction() {
|
|
6337
|
-
return (await this.dbAdapter).readWriteTransaction();
|
|
6338
|
-
}
|
|
6339
|
-
getStore(storeName) {
|
|
6340
|
-
if (!this.stores.has(storeName)) {
|
|
6341
|
-
this.stores.set(storeName, new MasterStoreState(storeName, this));
|
|
6342
|
-
}
|
|
6343
|
-
return this.stores.get(storeName);
|
|
6344
|
-
}
|
|
6345
|
-
async applyChanges(changes) {
|
|
6346
|
-
return Promise.all(changes.map(({ store, changes: changes2 }) => this.getStore(store).applyChanges(changes2)));
|
|
6347
|
-
}
|
|
6348
|
-
applySerializedChanges(changes) {
|
|
6349
|
-
return Promise.all(changes.map(({ store, changes: changes2 }) => this.getStore(store).applySerializedChanges(changes2)));
|
|
6350
|
-
}
|
|
6351
|
-
async commitChanges(changes) {
|
|
6352
|
-
await Promise.all([
|
|
6353
|
-
this.applyChanges(changes),
|
|
6354
|
-
this.rtcAdapter.commitChanges(changes)
|
|
6355
|
-
]);
|
|
6356
|
-
}
|
|
6357
|
-
fork() {
|
|
6358
|
-
return new ForkedDataStorage(this);
|
|
6359
|
-
}
|
|
6360
|
-
async sync(progressCallback) {
|
|
6361
|
-
await this.rtcAdapter.sync(progressCallback);
|
|
6362
|
-
}
|
|
6363
|
-
}
|
|
6364
|
-
|
|
6365
|
-
class SQLiteReadTransaction {
|
|
6366
|
-
db;
|
|
6367
|
-
tables;
|
|
6368
|
-
constructor(db, tables) {
|
|
6369
|
-
this.db = db;
|
|
6370
|
-
this.tables = tables;
|
|
6371
|
-
}
|
|
6372
|
-
deserializeValue(value, column) {
|
|
6373
|
-
if (value === null || value === undefined)
|
|
6374
|
-
return null;
|
|
6375
|
-
switch (column.type.toLowerCase()) {
|
|
6376
|
-
case "json":
|
|
6377
|
-
case "text": {
|
|
6378
|
-
try {
|
|
6379
|
-
const parsed = JSON.parse(value);
|
|
6380
|
-
if (typeof parsed === "object" || Array.isArray(parsed)) {
|
|
6381
|
-
return parsed;
|
|
6382
|
-
}
|
|
6383
|
-
return value;
|
|
6384
|
-
} catch {
|
|
6385
|
-
return value;
|
|
6386
|
-
}
|
|
6387
|
-
}
|
|
6388
|
-
case "datetime":
|
|
6389
|
-
case "timestamp":
|
|
6390
|
-
return new Date(value);
|
|
6391
|
-
default:
|
|
6392
|
-
return value;
|
|
6393
|
-
}
|
|
6394
|
-
}
|
|
6395
|
-
deserializeRow(row, table) {
|
|
6396
|
-
const result = {};
|
|
6397
|
-
for (const column of table.columns) {
|
|
6398
|
-
const value = row[column.name];
|
|
6399
|
-
result[column.name] = this.deserializeValue(value, column);
|
|
6400
|
-
}
|
|
6401
|
-
return result;
|
|
6402
|
-
}
|
|
6403
|
-
getId(store, id3) {
|
|
6404
|
-
return id3;
|
|
6405
|
-
}
|
|
6406
|
-
buildWhereClause(where) {
|
|
6407
|
-
if (!where) {
|
|
6408
|
-
return { sql: "deleted != 1", params: [] };
|
|
6409
|
-
}
|
|
6410
|
-
const conditions = ["deleted != 1"];
|
|
6411
|
-
const params = [];
|
|
6412
|
-
Object.entries(where).forEach(([key, value]) => {
|
|
6413
|
-
if (typeof value === "object" && value !== null) {
|
|
6414
|
-
Object.entries(value).forEach(([operator, operand]) => {
|
|
6415
|
-
switch (operator) {
|
|
6416
|
-
case "$eq":
|
|
6417
|
-
case "$ne":
|
|
6418
|
-
case "$gt":
|
|
6419
|
-
case "$gte":
|
|
6420
|
-
case "$lt":
|
|
6421
|
-
case "$lte":
|
|
6422
|
-
conditions.push(`${key} ${this.getOperatorSymbol(operator)} ?`);
|
|
6423
|
-
params.push(operand);
|
|
6424
|
-
break;
|
|
6425
|
-
case "$in":
|
|
6426
|
-
case "$nin":
|
|
6427
|
-
if (Array.isArray(operand)) {
|
|
6428
|
-
conditions.push(`${key} ${operator === "$in" ? "IN" : "NOT IN"} (${operand.map(() => "?").join(", ")})`);
|
|
6429
|
-
params.push(...operand);
|
|
6430
|
-
}
|
|
6431
|
-
break;
|
|
6432
|
-
case "$exists":
|
|
6433
|
-
if (typeof operand === "boolean") {
|
|
6434
|
-
conditions.push(operand ? `${key} IS NOT NULL` : `${key} IS NULL`);
|
|
6435
|
-
}
|
|
6436
|
-
break;
|
|
6437
|
-
}
|
|
6438
|
-
});
|
|
6439
|
-
} else {
|
|
6440
|
-
conditions.push(`${key} = ?`);
|
|
6441
|
-
params.push(value);
|
|
6442
|
-
}
|
|
6443
|
-
});
|
|
6444
|
-
return {
|
|
6445
|
-
sql: conditions.join(" AND "),
|
|
6446
|
-
params
|
|
6447
|
-
};
|
|
6448
|
-
}
|
|
6449
|
-
getOperatorSymbol(operator) {
|
|
6450
|
-
const operators = {
|
|
6451
|
-
$eq: "=",
|
|
6452
|
-
$ne: "!=",
|
|
6453
|
-
$gt: ">",
|
|
6454
|
-
$gte: ">=",
|
|
6455
|
-
$lt: "<",
|
|
6456
|
-
$lte: "<="
|
|
6457
|
-
};
|
|
6458
|
-
return operators[operator] || "=";
|
|
6459
|
-
}
|
|
6460
|
-
buildOrderByClause(orderBy) {
|
|
6461
|
-
if (!orderBy)
|
|
6462
|
-
return "";
|
|
6463
|
-
const orderClauses = Object.entries(orderBy).map(([key, direction]) => `${key} ${direction.toUpperCase()}`).join(", ");
|
|
6464
|
-
return orderClauses ? `ORDER BY ${orderClauses}` : "";
|
|
6465
|
-
}
|
|
6466
|
-
async find(store, options) {
|
|
6467
|
-
const { where, limit, offset, orderBy } = options || {};
|
|
6468
|
-
const whereClause = this.buildWhereClause(where);
|
|
6469
|
-
const orderByClause = this.buildOrderByClause(orderBy);
|
|
6470
|
-
const table = this.tables.get(store);
|
|
6471
|
-
if (!table) {
|
|
6472
|
-
throw new Error(`Store ${store} not found`);
|
|
6473
|
-
}
|
|
6474
|
-
const query2 = `
|
|
6475
|
-
SELECT *
|
|
6476
|
-
FROM ${store}
|
|
6477
|
-
WHERE ${whereClause.sql}
|
|
6478
|
-
${orderByClause}
|
|
6479
|
-
${limit ? `LIMIT ${limit}` : ""}
|
|
6480
|
-
${offset ? `OFFSET ${offset}` : ""}
|
|
6481
|
-
`;
|
|
6482
|
-
const rows = await this.db.exec(query2, whereClause.params);
|
|
6483
|
-
return rows.map((row) => this.deserializeRow(row, table));
|
|
6484
|
-
}
|
|
6485
|
-
}
|
|
6486
|
-
|
|
6487
|
-
class SQLiteReadWriteTransaction extends SQLiteReadTransaction {
|
|
6488
|
-
async remove(store, id3) {
|
|
6489
|
-
const table = this.tables.get(store);
|
|
6490
|
-
if (!table) {
|
|
6491
|
-
throw new Error(`Store ${store} not found`);
|
|
6492
|
-
}
|
|
6493
|
-
const query2 = `UPDATE ${store} SET deleted = 1, lastUpdate = ? WHERE ${table.primaryKey} = ?`;
|
|
6494
|
-
await this.db.exec(query2, [new Date().toISOString(), id3]);
|
|
6495
|
-
}
|
|
6496
|
-
async set(store, item) {
|
|
6497
|
-
const table = this.tables.get(store);
|
|
6498
|
-
if (!table) {
|
|
6499
|
-
throw new Error(`Store ${store} not found`);
|
|
6500
|
-
}
|
|
6501
|
-
const now = new Date().toISOString();
|
|
6502
|
-
const columnNames = table.columns.map((col) => col.name);
|
|
6503
|
-
const values = table.columns.map((column) => {
|
|
6504
|
-
let value = item[column.name];
|
|
6505
|
-
if (value === undefined && column.default !== undefined) {
|
|
6506
|
-
value = column.default;
|
|
6507
|
-
}
|
|
6508
|
-
return this.serializeValue(value, column);
|
|
6509
|
-
});
|
|
6510
|
-
const placeholders = columnNames.map(() => "?").join(", ");
|
|
6511
|
-
const sql = `
|
|
6512
|
-
INSERT OR REPLACE INTO ${table.name}
|
|
6513
|
-
(${columnNames.map((c) => `"${c}"`).join(", ")})
|
|
6514
|
-
VALUES (${placeholders})
|
|
6515
|
-
`;
|
|
6516
|
-
await this.db.exec(sql, values);
|
|
6517
|
-
}
|
|
6518
|
-
async commit() {
|
|
6519
|
-
return Promise.resolve();
|
|
6520
|
-
}
|
|
6521
|
-
serializeValue(value, column) {
|
|
6522
|
-
if (value === null || value === undefined)
|
|
6523
|
-
return null;
|
|
6524
|
-
switch (column.type.toLowerCase()) {
|
|
6525
|
-
case "json":
|
|
6526
|
-
return JSON.stringify(value);
|
|
6527
|
-
default:
|
|
6528
|
-
if (value instanceof Date) {
|
|
6529
|
-
return value.toISOString();
|
|
6530
|
-
}
|
|
6531
|
-
if (Array.isArray(value) || typeof value === "object") {
|
|
6532
|
-
return JSON.stringify(value);
|
|
6533
|
-
}
|
|
6534
|
-
return value;
|
|
6535
|
-
}
|
|
6536
|
-
}
|
|
6537
|
-
}
|
|
6538
|
-
|
|
6539
|
-
class SQLiteAdapter {
|
|
6540
|
-
db;
|
|
6541
|
-
context;
|
|
6542
|
-
tables = new Map;
|
|
6543
|
-
constructor(db, context3) {
|
|
6544
|
-
this.db = db;
|
|
6545
|
-
this.context = context3;
|
|
6546
|
-
this.context.elements.forEach((element3) => {
|
|
6547
|
-
if ("storeSchema" in element3 && typeof element3.storeSchema === "function") {
|
|
6548
|
-
element3.storeSchema().tables.forEach((table) => {
|
|
6549
|
-
this.tables.set(table.name, table);
|
|
6550
|
-
});
|
|
6551
|
-
}
|
|
6552
|
-
});
|
|
6553
|
-
}
|
|
6554
|
-
async initialize() {
|
|
6555
|
-
const stores = new Set;
|
|
6556
|
-
for (const element3 of this.context.elements) {
|
|
6557
|
-
if ("storeSchema" in element3 && typeof element3.storeSchema === "function") {
|
|
6558
|
-
const schema = element3.storeSchema();
|
|
6559
|
-
stores.add(schema);
|
|
6560
|
-
}
|
|
6561
|
-
}
|
|
6562
|
-
for (const schema of stores) {
|
|
6563
|
-
for (const table of schema.tables) {
|
|
6564
|
-
await this.createTableIfNotExists(table);
|
|
6565
|
-
}
|
|
6566
|
-
}
|
|
6567
|
-
}
|
|
6568
|
-
async createTableIfNotExists(table) {
|
|
6569
|
-
const columns = table.columns.map((column) => {
|
|
6570
|
-
const constraints = [];
|
|
6571
|
-
if (!column.isOptional) {
|
|
6572
|
-
constraints.push("NOT NULL");
|
|
6573
|
-
}
|
|
6574
|
-
if (column.default !== undefined) {
|
|
6575
|
-
constraints.push(`DEFAULT ${JSON.stringify(column.default)}`);
|
|
6576
|
-
}
|
|
6577
|
-
if (column.name === table.primaryKey) {
|
|
6578
|
-
constraints.push("PRIMARY KEY");
|
|
6579
|
-
}
|
|
6580
|
-
return `"${column.name}" ${column.type} ${constraints.join(" ")}`.trim();
|
|
6581
|
-
});
|
|
6582
|
-
const createTableSQL = `
|
|
6583
|
-
CREATE TABLE IF NOT EXISTS ${table.name} (
|
|
6584
|
-
${columns.join(`,
|
|
6585
|
-
`)}
|
|
6586
|
-
)
|
|
6587
|
-
`;
|
|
6588
|
-
await this.db.exec(createTableSQL);
|
|
6589
|
-
}
|
|
6590
|
-
readWriteTransaction(stores) {
|
|
6591
|
-
return new SQLiteReadWriteTransaction(this.db, this.tables);
|
|
6592
|
-
}
|
|
6593
|
-
readTransaction(stores) {
|
|
6594
|
-
return new SQLiteReadTransaction(this.db, this.tables);
|
|
6595
|
-
}
|
|
6596
|
-
}
|
|
6597
|
-
var createSQLiteAdapterFactory = (db) => {
|
|
6598
|
-
return async (context3) => {
|
|
6599
|
-
const adapter = new SQLiteAdapter(db, context3);
|
|
6600
|
-
await adapter.initialize();
|
|
6601
|
-
return adapter;
|
|
6602
|
-
};
|
|
6603
|
-
};
|
|
6604
|
-
class EventPublisher {
|
|
6605
|
-
context;
|
|
6606
|
-
dataStorage;
|
|
6607
|
-
authContext;
|
|
6608
|
-
asyncEvents = [];
|
|
6609
|
-
constructor(context3, dataStorage, authContext) {
|
|
6610
|
-
this.context = context3;
|
|
6611
|
-
this.dataStorage = dataStorage;
|
|
6612
|
-
this.authContext = authContext;
|
|
6613
|
-
}
|
|
6614
|
-
async publishEvent(event3, commandDataStorage) {
|
|
6615
|
-
const recursivePublishEvent = async (childEvent) => {
|
|
6616
|
-
await this.publishEvent(childEvent, commandDataStorage);
|
|
6617
|
-
};
|
|
6618
|
-
const syncListeners = this.context.getSyncListeners(event3.type, this.authContext);
|
|
6619
|
-
for (const listener3 of syncListeners) {
|
|
6620
|
-
try {
|
|
6621
|
-
await listener3(event3, commandDataStorage, recursivePublishEvent);
|
|
6622
|
-
} catch (error) {
|
|
6623
|
-
console.error("Sync listener failed:", error);
|
|
6624
|
-
throw error;
|
|
6625
|
-
}
|
|
6626
|
-
}
|
|
6627
|
-
const asyncListeners = this.context.getAsyncListeners(event3.type, this.authContext);
|
|
6628
|
-
if (asyncListeners.length > 0) {
|
|
6629
|
-
this.asyncEvents.push({
|
|
6630
|
-
event: event3,
|
|
6631
|
-
listeners: asyncListeners,
|
|
6632
|
-
authContext: this.authContext
|
|
6633
|
-
});
|
|
6634
|
-
}
|
|
6635
|
-
}
|
|
6636
|
-
async runAsyncListeners() {
|
|
6637
|
-
const allAsyncTasks = this.asyncEvents.flatMap(({ event: event3, listeners, authContext }) => listeners.map(async (listener3) => {
|
|
6638
|
-
const listenerFork = this.dataStorage.fork();
|
|
6639
|
-
const asyncPublishEvent = async (childEvent) => {
|
|
6640
|
-
const childPublisher = new EventPublisher(this.context, listenerFork, authContext);
|
|
6641
|
-
await childPublisher.publishEvent(childEvent, listenerFork);
|
|
6642
|
-
await childPublisher.runAsyncListeners();
|
|
6643
|
-
};
|
|
6644
|
-
try {
|
|
6645
|
-
await listener3(event3, listenerFork, asyncPublishEvent);
|
|
6646
|
-
await listenerFork.merge();
|
|
6647
|
-
} catch (error) {
|
|
6648
|
-
console.error("Async listener failed:", error);
|
|
6649
|
-
}
|
|
6650
|
-
}));
|
|
6651
|
-
await Promise.all(allAsyncTasks);
|
|
6652
|
-
this.asyncEvents = [];
|
|
6653
|
-
}
|
|
6654
|
-
}
|
|
6655
|
-
|
|
6656
|
-
class ModelBase {
|
|
6657
|
-
token = null;
|
|
6658
|
-
setAuthToken(token) {
|
|
6659
|
-
this.token = token;
|
|
6660
|
-
}
|
|
6661
|
-
}
|
|
6662
|
-
|
|
6663
|
-
class Model extends ModelBase {
|
|
6664
|
-
context;
|
|
6665
|
-
dataStorage;
|
|
6666
|
-
catchErrorCallback;
|
|
6667
|
-
queryCache = new QueryCache;
|
|
6668
|
-
constructor(context3, dataStorage, catchErrorCallback) {
|
|
6669
|
-
super();
|
|
6670
|
-
this.context = context3;
|
|
6671
|
-
this.dataStorage = dataStorage;
|
|
6672
|
-
this.catchErrorCallback = catchErrorCallback;
|
|
6673
|
-
}
|
|
6674
|
-
async query(queryBuilderFn, authContext) {
|
|
6675
|
-
const queryContext = new QueryBuilderContext(this.queryCache, this.dataStorage);
|
|
6676
|
-
const queryBuilder = this.context.queryBuilder(queryContext, authContext);
|
|
6677
|
-
const query2 = queryBuilderFn(queryBuilder).toQuery(queryContext);
|
|
6678
|
-
return query2.run(this.dataStorage);
|
|
6679
|
-
}
|
|
6680
|
-
subscribe(queryBuilderFn, callback, authContext) {
|
|
6681
|
-
const queryContext = new QueryBuilderContext(this.queryCache, this.dataStorage);
|
|
6682
|
-
const queryBuilder = this.context.queryBuilder(queryContext, authContext);
|
|
6683
|
-
const query2 = queryBuilderFn(queryBuilder).toQuery(queryContext);
|
|
6684
|
-
query2.subscribe(callback);
|
|
6685
|
-
const runPromise = query2.run(this.dataStorage);
|
|
6686
|
-
runPromise.then((result) => {
|
|
6687
|
-
callback(result);
|
|
6688
|
-
});
|
|
6689
|
-
return {
|
|
6690
|
-
unsubscribe: () => {
|
|
6691
|
-
query2.unsubscribe(callback);
|
|
6692
|
-
},
|
|
6693
|
-
result: runPromise
|
|
6694
|
-
};
|
|
6695
|
-
}
|
|
6696
|
-
commands(authContext) {
|
|
6697
|
-
return new Proxy({}, {
|
|
6698
|
-
get: (_, name) => {
|
|
6699
|
-
const element3 = this.context.elements.find((element4) => element4.name === name);
|
|
6700
|
-
if (!element3) {
|
|
6701
|
-
throw new Error(`Element "${String(name)}" not found`);
|
|
6702
|
-
}
|
|
6703
|
-
if (!element3.commandClient) {
|
|
6704
|
-
throw new Error(`Element "${String(name)}" does not have a command client`);
|
|
6705
|
-
}
|
|
6706
|
-
return async (arg) => {
|
|
6707
|
-
const forkedDataStorage = this.dataStorage.fork();
|
|
6708
|
-
const eventPublisher = new EventPublisher(this.context, this.dataStorage, authContext);
|
|
6709
|
-
const publishEvent = async (event3) => {
|
|
6710
|
-
await eventPublisher.publishEvent(event3, forkedDataStorage);
|
|
6711
|
-
};
|
|
6712
|
-
const commandContext = this.context.commandContext(forkedDataStorage, publishEvent, authContext);
|
|
6713
|
-
try {
|
|
6714
|
-
const result = await element3.commandClient(commandContext, authContext)(arg);
|
|
6715
|
-
await forkedDataStorage.merge();
|
|
6716
|
-
eventPublisher.runAsyncListeners();
|
|
6717
|
-
return result;
|
|
6718
|
-
} catch (error) {
|
|
6719
|
-
this.catchErrorCallback(error);
|
|
6720
|
-
return error;
|
|
6721
|
-
}
|
|
6722
|
-
};
|
|
6723
|
-
}
|
|
6724
|
-
});
|
|
6725
|
-
}
|
|
6726
|
-
routes(authContext) {
|
|
6727
|
-
return new Proxy({}, {
|
|
6728
|
-
get: (_, name) => {
|
|
6729
|
-
const element3 = this.context.elements.find((element4) => element4.name === name);
|
|
6730
|
-
if (!element3) {
|
|
6731
|
-
throw new Error(`Route element "${String(name)}" not found`);
|
|
6732
|
-
}
|
|
6733
|
-
if (typeof element3.getHandler !== "function") {
|
|
6734
|
-
throw new Error(`Element "${String(name)}" does not have route handlers`);
|
|
6735
|
-
}
|
|
6736
|
-
return async (method, req, routeParams, url) => {
|
|
6737
|
-
const handler = element3.getHandler(method);
|
|
6738
|
-
if (!handler) {
|
|
6739
|
-
throw new Error(`Method ${method} not allowed for route ${String(name)}`);
|
|
6740
|
-
}
|
|
6741
|
-
const forkedDataStorage = this.dataStorage.fork();
|
|
6742
|
-
const eventPublisher = new EventPublisher(this.context, this.dataStorage, authContext);
|
|
6743
|
-
const publishEvent = async (event3) => {
|
|
6744
|
-
await eventPublisher.publishEvent(event3, forkedDataStorage);
|
|
6745
|
-
};
|
|
6746
|
-
const commandContext = this.context.commandContext(forkedDataStorage, publishEvent, authContext);
|
|
6747
|
-
try {
|
|
6748
|
-
const result = await handler(commandContext, req, routeParams, url);
|
|
6749
|
-
await forkedDataStorage.merge();
|
|
6750
|
-
eventPublisher.runAsyncListeners();
|
|
6751
|
-
return result;
|
|
6752
|
-
} catch (error) {
|
|
6753
|
-
this.catchErrorCallback(error);
|
|
6754
|
-
throw error;
|
|
6755
|
-
}
|
|
6756
|
-
};
|
|
6757
|
-
}
|
|
6758
|
-
});
|
|
6759
|
-
}
|
|
6760
|
-
get $debug() {
|
|
6761
|
-
return {};
|
|
6762
|
-
}
|
|
6763
|
-
}
|
|
6764
|
-
class ArcViewQuery extends ArcSerializableQuery {
|
|
6765
|
-
view;
|
|
6766
|
-
bindedChangeHandler = this.changeHandler.bind(this);
|
|
6767
|
-
store;
|
|
6768
|
-
constructor(view, params) {
|
|
6769
|
-
super(params);
|
|
6770
|
-
this.view = view;
|
|
6771
|
-
}
|
|
6772
|
-
async run(dataStorage) {
|
|
6773
|
-
const store = dataStorage.getStore(this.view.name);
|
|
6774
|
-
this.store = store;
|
|
6775
|
-
const result = await this.fetch(store);
|
|
6776
|
-
this.lastResult = result;
|
|
6777
|
-
return result;
|
|
6778
|
-
}
|
|
6779
|
-
changeHandler(changes) {
|
|
6780
|
-
let resultChanged = false;
|
|
6781
|
-
for (const change of changes) {
|
|
6782
|
-
const response = this.onChange(change);
|
|
6783
|
-
if (response !== false) {
|
|
6784
|
-
this.lastResult = response;
|
|
6785
|
-
resultChanged = true;
|
|
6786
|
-
}
|
|
6787
|
-
}
|
|
6788
|
-
if (resultChanged)
|
|
6789
|
-
this.nextResult(this.lastResult);
|
|
6790
|
-
}
|
|
6791
|
-
}
|
|
3644
|
+
// host.ts
|
|
3645
|
+
import {
|
|
3646
|
+
MasterDataStorage,
|
|
3647
|
+
Model,
|
|
3648
|
+
QueryBuilderContext,
|
|
3649
|
+
QueryCache
|
|
3650
|
+
} from "@arcote.tech/arc";
|
|
6792
3651
|
|
|
6793
3652
|
// auth/jwt.ts
|
|
6794
3653
|
var import_jsonwebtoken = __toESM(require_jsonwebtoken(), 1);
|
|
@@ -6860,35 +3719,35 @@ class RTCHost {
|
|
|
6860
3719
|
}
|
|
6861
3720
|
setNestedProperty(obj, path, value) {
|
|
6862
3721
|
const keys = path.split(/[\[\]]+/).filter((key) => key !== "");
|
|
6863
|
-
let
|
|
3722
|
+
let current = obj;
|
|
6864
3723
|
for (let i = 0;i < keys.length - 1; i++) {
|
|
6865
3724
|
const key = keys[i];
|
|
6866
3725
|
const nextKey = keys[i + 1];
|
|
6867
3726
|
if (!isNaN(Number(nextKey))) {
|
|
6868
|
-
if (!
|
|
6869
|
-
|
|
3727
|
+
if (!current[key]) {
|
|
3728
|
+
current[key] = [];
|
|
6870
3729
|
}
|
|
6871
|
-
if (!
|
|
6872
|
-
|
|
3730
|
+
if (!current[key][Number(nextKey)]) {
|
|
3731
|
+
current[key][Number(nextKey)] = {};
|
|
6873
3732
|
}
|
|
6874
|
-
|
|
3733
|
+
current = current[key][Number(nextKey)];
|
|
6875
3734
|
i++;
|
|
6876
3735
|
} else {
|
|
6877
|
-
if (!
|
|
6878
|
-
|
|
3736
|
+
if (!current[key]) {
|
|
3737
|
+
current[key] = {};
|
|
6879
3738
|
}
|
|
6880
|
-
|
|
3739
|
+
current = current[key];
|
|
6881
3740
|
}
|
|
6882
3741
|
}
|
|
6883
3742
|
const finalKey = keys[keys.length - 1];
|
|
6884
3743
|
if (!isNaN(Number(finalKey))) {
|
|
6885
3744
|
const parentKey = keys[keys.length - 2];
|
|
6886
|
-
if (!
|
|
6887
|
-
|
|
3745
|
+
if (!current[parentKey]) {
|
|
3746
|
+
current[parentKey] = [];
|
|
6888
3747
|
}
|
|
6889
|
-
|
|
3748
|
+
current[parentKey][Number(finalKey)] = value;
|
|
6890
3749
|
} else {
|
|
6891
|
-
|
|
3750
|
+
current[finalKey] = value;
|
|
6892
3751
|
}
|
|
6893
3752
|
}
|
|
6894
3753
|
async handleCommand(req) {
|
|
@@ -7063,7 +3922,8 @@ class RTCHost {
|
|
|
7063
3922
|
perMessageDeflate: true,
|
|
7064
3923
|
backpressureLimit: 16 * 1024 * 1024
|
|
7065
3924
|
},
|
|
7066
|
-
port: 5005
|
|
3925
|
+
port: 5005,
|
|
3926
|
+
idleTimeout: 30
|
|
7067
3927
|
});
|
|
7068
3928
|
}
|
|
7069
3929
|
isPublicEndpoint(pathname) {
|
|
@@ -7091,7 +3951,6 @@ class RTCHost {
|
|
|
7091
3951
|
}
|
|
7092
3952
|
} : {};
|
|
7093
3953
|
const syncResults = [];
|
|
7094
|
-
for (const collection of this.context.elements.filter((element) => element instanceof ArcCollection).concat([{ name: "state" }])) {}
|
|
7095
3954
|
return new Response(JSON.stringify({
|
|
7096
3955
|
results: syncResults,
|
|
7097
3956
|
syncDate: syncDate.toISOString()
|
|
@@ -7124,6 +3983,9 @@ var rtcHostFactory = (context, dbAdapter) => () => {
|
|
|
7124
3983
|
};
|
|
7125
3984
|
|
|
7126
3985
|
// sqliteAdapter.ts
|
|
3986
|
+
import {
|
|
3987
|
+
createSQLiteAdapterFactory
|
|
3988
|
+
} from "@arcote.tech/arc";
|
|
7127
3989
|
import { Database } from "bun:sqlite";
|
|
7128
3990
|
|
|
7129
3991
|
class BunSQLiteDatabase {
|
|
@@ -7152,11 +4014,57 @@ var sqliteAdapterFactory = (dbName) => {
|
|
|
7152
4014
|
};
|
|
7153
4015
|
};
|
|
7154
4016
|
|
|
4017
|
+
// postgresAdapter.ts
|
|
4018
|
+
import {
|
|
4019
|
+
createPostgreSQLAdapterFactory
|
|
4020
|
+
} from "@arcote.tech/arc";
|
|
4021
|
+
var {SQL } = globalThis.Bun;
|
|
4022
|
+
|
|
4023
|
+
class BunPostgreSQLDatabase {
|
|
4024
|
+
connectionString;
|
|
4025
|
+
sql;
|
|
4026
|
+
constructor(connectionString) {
|
|
4027
|
+
this.connectionString = connectionString;
|
|
4028
|
+
this.sql = new SQL(this.connectionString);
|
|
4029
|
+
}
|
|
4030
|
+
async exec(query, params) {
|
|
4031
|
+
try {
|
|
4032
|
+
const result = await this.sql.unsafe(query, params);
|
|
4033
|
+
return result;
|
|
4034
|
+
} catch (error) {
|
|
4035
|
+
console.error("PostgreSQL error:", error, query, params);
|
|
4036
|
+
throw error;
|
|
4037
|
+
}
|
|
4038
|
+
}
|
|
4039
|
+
async execBatch(queries) {
|
|
4040
|
+
try {
|
|
4041
|
+
return await this.sql.transaction(async (tx) => {
|
|
4042
|
+
for (const query of queries) {
|
|
4043
|
+
await tx.unsafe(query.sql, query.params);
|
|
4044
|
+
}
|
|
4045
|
+
});
|
|
4046
|
+
} catch (error) {
|
|
4047
|
+
console.error("PostgreSQL batch transaction error:", error);
|
|
4048
|
+
throw error;
|
|
4049
|
+
}
|
|
4050
|
+
}
|
|
4051
|
+
}
|
|
4052
|
+
var postgreSQLAdapterFactory = (connectionString) => {
|
|
4053
|
+
return async (context) => {
|
|
4054
|
+
const pgDb = new BunPostgreSQLDatabase(connectionString);
|
|
4055
|
+
return createPostgreSQLAdapterFactory(pgDb)(context);
|
|
4056
|
+
};
|
|
4057
|
+
};
|
|
4058
|
+
|
|
7155
4059
|
// index.ts
|
|
7156
4060
|
function hostLiveModel(context, options) {
|
|
7157
4061
|
const dbAdapterFactory = sqliteAdapterFactory(options.db);
|
|
7158
4062
|
rtcHostFactory(context, dbAdapterFactory(context))();
|
|
7159
4063
|
}
|
|
7160
4064
|
export {
|
|
4065
|
+
postgreSQLAdapterFactory,
|
|
7161
4066
|
hostLiveModel
|
|
7162
4067
|
};
|
|
4068
|
+
|
|
4069
|
+
//# debugId=3170B378AC8CEA7664756E2164756E21
|
|
4070
|
+
//# sourceMappingURL=index.js.map
|