@arcote.tech/arc-adapter-db-sqlite 0.7.26 → 0.7.28

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.
Files changed (2) hide show
  1. package/dist/index.js +224 -74
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1782,9 +1782,21 @@ class LocalEventPublisher {
1782
1782
  views = [];
1783
1783
  syncCallback;
1784
1784
  subscribers = new Map;
1785
+ pending = new Set;
1785
1786
  constructor(dataStorage) {
1786
1787
  this.dataStorage = dataStorage;
1787
1788
  }
1789
+ track(work) {
1790
+ const wrapped = Promise.resolve(work).catch(() => {}).finally(() => {
1791
+ this.pending.delete(wrapped);
1792
+ });
1793
+ this.pending.add(wrapped);
1794
+ }
1795
+ async whenIdle() {
1796
+ while (this.pending.size > 0) {
1797
+ await Promise.all([...this.pending]);
1798
+ }
1799
+ }
1788
1800
  onPublish(callback) {
1789
1801
  this.syncCallback = callback;
1790
1802
  }
@@ -1820,9 +1832,12 @@ class LocalEventPublisher {
1820
1832
  store: EVENT_TABLES.events,
1821
1833
  changes: [{ type: "set", data: storedEvent }]
1822
1834
  });
1823
- if (event.payload && typeof event.payload === "object") {
1835
+ const tagFields = event.definition?.tagFields ?? [];
1836
+ if (tagFields.length > 0 && event.payload && typeof event.payload === "object") {
1837
+ const payload = event.payload;
1824
1838
  const tagChanges = [];
1825
- for (const [key, value] of Object.entries(event.payload)) {
1839
+ for (const key of tagFields) {
1840
+ const value = payload[key];
1826
1841
  if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
1827
1842
  tagChanges.push({
1828
1843
  type: "set",
@@ -1980,6 +1995,47 @@ class LocalEventPublisher {
1980
1995
  return allViewChanges;
1981
1996
  }
1982
1997
  }
1998
+ function murmurHash(key, seed = 0) {
1999
+ let remainder, bytes, h1, h1b, c1, c2, k1, i;
2000
+ remainder = key.length & 3;
2001
+ bytes = key.length - remainder;
2002
+ h1 = seed;
2003
+ c1 = 3432918353;
2004
+ c2 = 461845907;
2005
+ i = 0;
2006
+ while (i < bytes) {
2007
+ k1 = key.charCodeAt(i) & 255 | (key.charCodeAt(++i) & 255) << 8 | (key.charCodeAt(++i) & 255) << 16 | (key.charCodeAt(++i) & 255) << 24;
2008
+ ++i;
2009
+ k1 = (k1 & 65535) * c1 + (((k1 >>> 16) * c1 & 65535) << 16) & 4294967295;
2010
+ k1 = k1 << 15 | k1 >>> 17;
2011
+ k1 = (k1 & 65535) * c2 + (((k1 >>> 16) * c2 & 65535) << 16) & 4294967295;
2012
+ h1 ^= k1;
2013
+ h1 = h1 << 13 | h1 >>> 19;
2014
+ h1b = (h1 & 65535) * 5 + (((h1 >>> 16) * 5 & 65535) << 16) & 4294967295;
2015
+ h1 = (h1b & 65535) + 27492 + (((h1b >>> 16) + 58964 & 65535) << 16);
2016
+ }
2017
+ k1 = 0;
2018
+ if (remainder >= 3) {
2019
+ k1 ^= (key.charCodeAt(i + 2) & 255) << 16;
2020
+ }
2021
+ if (remainder >= 2) {
2022
+ k1 ^= (key.charCodeAt(i + 1) & 255) << 8;
2023
+ }
2024
+ if (remainder >= 1) {
2025
+ k1 ^= key.charCodeAt(i) & 255;
2026
+ k1 = (k1 & 65535) * c1 + (((k1 >>> 16) * c1 & 65535) << 16) & 4294967295;
2027
+ k1 = k1 << 15 | k1 >>> 17;
2028
+ k1 = (k1 & 65535) * c2 + (((k1 >>> 16) * c2 & 65535) << 16) & 4294967295;
2029
+ h1 ^= k1;
2030
+ }
2031
+ h1 ^= key.length;
2032
+ h1 ^= h1 >>> 16;
2033
+ h1 = (h1 & 65535) * 2246822507 + (((h1 >>> 16) * 2246822507 & 65535) << 16) & 4294967295;
2034
+ h1 ^= h1 >>> 13;
2035
+ h1 = (h1 & 65535) * 3266489909 + (((h1 >>> 16) * 3266489909 & 65535) << 16) & 4294967295;
2036
+ h1 ^= h1 >>> 16;
2037
+ return h1 >>> 0;
2038
+ }
1983
2039
  class ArcOptional {
1984
2040
  parent;
1985
2041
  constructor(parent) {
@@ -2005,6 +2061,11 @@ class ArcOptional {
2005
2061
  return false;
2006
2062
  return this.parent.validate(value);
2007
2063
  }
2064
+ coerce(value) {
2065
+ if (value == null)
2066
+ return;
2067
+ return this.parent.coerce(value);
2068
+ }
2008
2069
  toJsonSchema() {
2009
2070
  const parentSchema = this.parent.toJsonSchema?.() ?? {};
2010
2071
  return {
@@ -2051,6 +2112,9 @@ class ArcBranded {
2051
2112
  validate(value) {
2052
2113
  return this.parent.validate(value);
2053
2114
  }
2115
+ coerce(value) {
2116
+ return this.parent.coerce(value);
2117
+ }
2054
2118
  toJsonSchema() {
2055
2119
  return this.parent.toJsonSchema?.() ?? {};
2056
2120
  }
@@ -2071,7 +2135,14 @@ class ArcDefault {
2071
2135
  this.defaultValueOrCallback = defaultValueOrCallback;
2072
2136
  }
2073
2137
  validate(value) {
2074
- throw new Error("Method not implemented.");
2138
+ if (value === undefined || value === null)
2139
+ return false;
2140
+ return this.parent.validate(value);
2141
+ }
2142
+ coerce(value) {
2143
+ if (value === undefined || value === null)
2144
+ return;
2145
+ return this.parent.coerce(value);
2075
2146
  }
2076
2147
  parse(value) {
2077
2148
  if (value)
@@ -2155,6 +2226,9 @@ class ArcAbstract {
2155
2226
  }
2156
2227
  return false;
2157
2228
  }
2229
+ coerce(value) {
2230
+ return this.validate(value) ? undefined : value;
2231
+ }
2158
2232
  pipeValidation(name, validator) {
2159
2233
  const newInstance = this.clone();
2160
2234
  newInstance.validations = [...this.validations, { name, validator }];
@@ -2292,6 +2366,17 @@ class ArcArray extends ArcAbstract {
2292
2366
  return { msg: "array is empty" };
2293
2367
  });
2294
2368
  }
2369
+ coerce(value) {
2370
+ if (!Array.isArray(value))
2371
+ return;
2372
+ const out = [];
2373
+ for (const item of value) {
2374
+ const coerced = this.parent.coerce(item);
2375
+ if (coerced !== undefined)
2376
+ out.push(coerced);
2377
+ }
2378
+ return this.validate(out) ? undefined : out;
2379
+ }
2295
2380
  parse(value) {
2296
2381
  return value.map((v) => this.parent.parse(v));
2297
2382
  }
@@ -2363,6 +2448,19 @@ class ArcObject extends ArcAbstract {
2363
2448
  ]);
2364
2449
  this.rawShape = rawShape;
2365
2450
  }
2451
+ coerce(value) {
2452
+ if (value == null || typeof value !== "object" || Array.isArray(value)) {
2453
+ return;
2454
+ }
2455
+ const src = value;
2456
+ const out = {};
2457
+ for (const [key, element] of Object.entries(this.rawShape)) {
2458
+ const coerced = element.coerce(src[key]);
2459
+ if (coerced !== undefined)
2460
+ out[key] = coerced;
2461
+ }
2462
+ return this.validate(out) ? undefined : out;
2463
+ }
2366
2464
  parse(value) {
2367
2465
  return Object.entries(this.rawShape).reduce((acc, [key, element]) => {
2368
2466
  acc[key] = element.parse(value[key]);
@@ -2597,6 +2695,51 @@ class ScopedDataStorage extends DataStorage {
2597
2695
  return this.#inner.getReadWriteTransaction();
2598
2696
  }
2599
2697
  }
2698
+ class ArcAuthenticationError extends Error {
2699
+ arcCode = "AUTHENTICATION";
2700
+ constructor(message = "Authentication required") {
2701
+ super(message);
2702
+ this.name = "ArcAuthenticationError";
2703
+ }
2704
+ }
2705
+
2706
+ class ArcAuthorizationError extends Error {
2707
+ arcCode = "AUTHORIZATION";
2708
+ constructor(message = "Forbidden") {
2709
+ super(message);
2710
+ this.name = "ArcAuthorizationError";
2711
+ }
2712
+ }
2713
+ function collectFieldPaths(errors, prefix = "") {
2714
+ if (!errors || typeof errors !== "object")
2715
+ return [];
2716
+ const schema = errors.schema;
2717
+ if (!schema || typeof schema !== "object")
2718
+ return [];
2719
+ const out = [];
2720
+ for (const [field, fieldError] of Object.entries(schema)) {
2721
+ if (!fieldError)
2722
+ continue;
2723
+ const path = prefix ? `${prefix}.${field}` : field;
2724
+ const nested = collectFieldPaths(fieldError, path);
2725
+ if (nested.length > 0)
2726
+ out.push(...nested);
2727
+ else
2728
+ out.push(path);
2729
+ }
2730
+ return out;
2731
+ }
2732
+
2733
+ class ArcValidationError extends Error {
2734
+ arcCode = "VALIDATION";
2735
+ fields;
2736
+ constructor(errors, message = "Invalid parameters") {
2737
+ super(message);
2738
+ this.name = "ArcValidationError";
2739
+ const paths = collectFieldPaths(errors);
2740
+ this.fields = paths.length > 0 ? paths : errors && typeof errors === "object" ? Object.keys(errors) : [];
2741
+ }
2742
+ }
2600
2743
  class ArcPrimitive extends ArcAbstract {
2601
2744
  serialize(value) {
2602
2745
  return value;
@@ -2769,6 +2912,8 @@ class ArcFragmentBase {
2769
2912
  class ArcContextElement extends ArcFragmentBase {
2770
2913
  name;
2771
2914
  types = ["context-element"];
2915
+ __contextId;
2916
+ __contextName;
2772
2917
  get id() {
2773
2918
  return this.name;
2774
2919
  }
@@ -2959,7 +3104,8 @@ class ArcEvent extends ArcContextElement {
2959
3104
  payload,
2960
3105
  id: this.eventId.generate(),
2961
3106
  createdAt: new Date,
2962
- authContext
3107
+ authContext,
3108
+ definition: this
2963
3109
  };
2964
3110
  await adapters.eventPublisher.publish(event);
2965
3111
  }
@@ -3012,8 +3158,8 @@ class ArcEvent extends ArcContextElement {
3012
3158
  const tagsSchema = new ArcObject({
3013
3159
  _id: new ArcString().primaryKey(),
3014
3160
  eventId: new ArcString().index(),
3015
- tagKey: new ArcString().index(),
3016
- tagValue: new ArcString().index()
3161
+ tagKey: new ArcString,
3162
+ tagValue: new ArcString
3017
3163
  });
3018
3164
  const syncStatusSchema = new ArcObject({
3019
3165
  _id: new ArcString().primaryKey(),
@@ -3108,6 +3254,27 @@ class ArcFunction {
3108
3254
  get results() {
3109
3255
  return this.data.results;
3110
3256
  }
3257
+ validateParams(params) {
3258
+ const schema = this.data.params;
3259
+ if (!schema || typeof schema.validate !== "function")
3260
+ return;
3261
+ const errors = schema.validate(params ?? {});
3262
+ if (errors)
3263
+ throw new ArcValidationError(errors);
3264
+ }
3265
+ async authorize(adapters) {
3266
+ if (!this.hasProtections)
3267
+ return;
3268
+ const decoded = adapters.authAdapter?.getDecoded?.();
3269
+ if (!decoded) {
3270
+ throw new ArcAuthenticationError;
3271
+ }
3272
+ const instances = this.data.protections.filter((p) => p.token?.name === decoded.tokenName).map((p) => p.token.create(decoded.params, adapters));
3273
+ const ok = await this.verifyProtections(instances);
3274
+ if (!ok) {
3275
+ throw new ArcAuthorizationError;
3276
+ }
3277
+ }
3111
3278
  async verifyProtections(tokens) {
3112
3279
  if (!this.data.protections || this.data.protections.length === 0) {
3113
3280
  return true;
@@ -3268,6 +3435,8 @@ class ArcCommand extends ArcContextElement {
3268
3435
  if (!this.data.handler) {
3269
3436
  throw new Error(`Command "${this.data.name}" has no handler`);
3270
3437
  }
3438
+ this.#fn.validateParams(params);
3439
+ await this.#fn.authorize(adapters);
3271
3440
  const context2 = this.buildCommandContext(adapters);
3272
3441
  return await this.data.handler(context2, params);
3273
3442
  }
@@ -3378,9 +3547,10 @@ class ArcListener extends ArcContextElement {
3378
3547
  }
3379
3548
  }
3380
3549
  if (this.data.isAsync) {
3381
- Promise.resolve(this.data.handler(context2, event2)).catch((error) => {
3550
+ const work = Promise.resolve(this.data.handler(context2, event2)).catch((error) => {
3382
3551
  console.error(`Async listener "${this.data.name}" error:`, error);
3383
3552
  });
3553
+ adapters.eventPublisher?.track?.(work);
3384
3554
  } else {
3385
3555
  await this.data.handler(context2, event2);
3386
3556
  }
@@ -3587,48 +3757,6 @@ function deepMerge(target, source) {
3587
3757
  function isPlainObject(item) {
3588
3758
  return item && typeof item === "object" && !Array.isArray(item) && !(item instanceof Date) && Object.prototype.toString.call(item) === "[object Object]";
3589
3759
  }
3590
- function murmurHash(key, seed = 0) {
3591
- let remainder, bytes, h1, h1b, c1, c2, k1, i;
3592
- remainder = key.length & 3;
3593
- bytes = key.length - remainder;
3594
- h1 = seed;
3595
- c1 = 3432918353;
3596
- c2 = 461845907;
3597
- i = 0;
3598
- while (i < bytes) {
3599
- k1 = key.charCodeAt(i) & 255 | (key.charCodeAt(++i) & 255) << 8 | (key.charCodeAt(++i) & 255) << 16 | (key.charCodeAt(++i) & 255) << 24;
3600
- ++i;
3601
- k1 = (k1 & 65535) * c1 + (((k1 >>> 16) * c1 & 65535) << 16) & 4294967295;
3602
- k1 = k1 << 15 | k1 >>> 17;
3603
- k1 = (k1 & 65535) * c2 + (((k1 >>> 16) * c2 & 65535) << 16) & 4294967295;
3604
- h1 ^= k1;
3605
- h1 = h1 << 13 | h1 >>> 19;
3606
- h1b = (h1 & 65535) * 5 + (((h1 >>> 16) * 5 & 65535) << 16) & 4294967295;
3607
- h1 = (h1b & 65535) + 27492 + (((h1b >>> 16) + 58964 & 65535) << 16);
3608
- }
3609
- k1 = 0;
3610
- if (remainder >= 3) {
3611
- k1 ^= (key.charCodeAt(i + 2) & 255) << 16;
3612
- }
3613
- if (remainder >= 2) {
3614
- k1 ^= (key.charCodeAt(i + 1) & 255) << 8;
3615
- }
3616
- if (remainder >= 1) {
3617
- k1 ^= key.charCodeAt(i) & 255;
3618
- k1 = (k1 & 65535) * c1 + (((k1 >>> 16) * c1 & 65535) << 16) & 4294967295;
3619
- k1 = k1 << 15 | k1 >>> 17;
3620
- k1 = (k1 & 65535) * c2 + (((k1 >>> 16) * c2 & 65535) << 16) & 4294967295;
3621
- h1 ^= k1;
3622
- }
3623
- h1 ^= key.length;
3624
- h1 ^= h1 >>> 16;
3625
- h1 = (h1 & 65535) * 2246822507 + (((h1 >>> 16) * 2246822507 & 65535) << 16) & 4294967295;
3626
- h1 ^= h1 >>> 13;
3627
- h1 = (h1 & 65535) * 3266489909 + (((h1 >>> 16) * 3266489909 & 65535) << 16) & 4294967295;
3628
- h1 ^= h1 >>> 16;
3629
- return h1 >>> 0;
3630
- }
3631
-
3632
3760
  class ForkedStoreState extends StoreState {
3633
3761
  master;
3634
3762
  changedItems = new Map;
@@ -3972,16 +4100,57 @@ function resolveQueryChange(currentResult, event3, options) {
3972
4100
  }
3973
4101
  const shouldBeInResult = checkItemMatchesWhere(event3.item, options.where);
3974
4102
  if (isInCurrentResult && shouldBeInResult) {
3975
- const newResult = currentResult.toSpliced(index, 1, event3.item);
3976
- return applyOrderByAndLimit(newResult, options);
4103
+ if (!options.orderBy) {
4104
+ return applyLimit(currentResult.toSpliced(index, 1, event3.item), options);
4105
+ }
4106
+ const cmp = compareByOrderBy(options.orderBy);
4107
+ if (cmp(currentResult[index], event3.item) === 0) {
4108
+ return applyLimit(currentResult.toSpliced(index, 1, event3.item), options);
4109
+ }
4110
+ const without = currentResult.toSpliced(index, 1);
4111
+ const at = upperBound(without, event3.item, cmp);
4112
+ return applyLimit(without.toSpliced(at, 0, event3.item), options);
3977
4113
  } else if (isInCurrentResult && !shouldBeInResult) {
3978
4114
  return currentResult.toSpliced(index, 1);
3979
4115
  } else if (!isInCurrentResult && shouldBeInResult) {
3980
- const newResult = [...currentResult, event3.item];
3981
- return applyOrderByAndLimit(newResult, options);
4116
+ if (!options.orderBy) {
4117
+ return applyLimit([...currentResult, event3.item], options);
4118
+ }
4119
+ const cmp = compareByOrderBy(options.orderBy);
4120
+ const at = upperBound(currentResult, event3.item, cmp);
4121
+ return applyLimit(currentResult.toSpliced(at, 0, event3.item), options);
3982
4122
  }
3983
4123
  return false;
3984
4124
  }
4125
+ function compareByOrderBy(orderBy) {
4126
+ const entries = Object.entries(orderBy);
4127
+ return (a, b) => {
4128
+ for (const [key, direction] of entries) {
4129
+ const aVal = a[key];
4130
+ const bVal = b[key];
4131
+ if (aVal < bVal)
4132
+ return direction === "asc" ? -1 : 1;
4133
+ if (aVal > bVal)
4134
+ return direction === "asc" ? 1 : -1;
4135
+ }
4136
+ return 0;
4137
+ };
4138
+ }
4139
+ function upperBound(arr, item, cmp) {
4140
+ let lo = 0;
4141
+ let hi = arr.length;
4142
+ while (lo < hi) {
4143
+ const mid = lo + hi >> 1;
4144
+ if (cmp(arr[mid], item) <= 0)
4145
+ lo = mid + 1;
4146
+ else
4147
+ hi = mid;
4148
+ }
4149
+ return lo;
4150
+ }
4151
+ function applyLimit(result, options) {
4152
+ return options.limit !== undefined && result.length > options.limit ? result.slice(0, options.limit) : result;
4153
+ }
3985
4154
  function checkItemMatchesWhere(item, where) {
3986
4155
  if (!where) {
3987
4156
  return true;
@@ -4017,27 +4186,6 @@ function checkItemMatchesWhere(item, where) {
4017
4186
  });
4018
4187
  });
4019
4188
  }
4020
- function applyOrderByAndLimit(result, options) {
4021
- let sorted = result;
4022
- if (options.orderBy) {
4023
- sorted = [...result].sort((a, b) => {
4024
- for (const [key, direction] of Object.entries(options.orderBy)) {
4025
- const aVal = a[key];
4026
- const bVal = b[key];
4027
- if (aVal < bVal)
4028
- return direction === "asc" ? -1 : 1;
4029
- if (aVal > bVal)
4030
- return direction === "asc" ? 1 : -1;
4031
- }
4032
- return 0;
4033
- });
4034
- }
4035
- if (options.limit !== undefined) {
4036
- sorted = sorted.slice(0, options.limit);
4037
- }
4038
- return sorted;
4039
- }
4040
-
4041
4189
  class ObservableDataStorage {
4042
4190
  source;
4043
4191
  onChange;
@@ -4685,6 +4833,8 @@ class SecuredDataStorage {
4685
4833
  return this.dataStorage.fork();
4686
4834
  }
4687
4835
  }
4836
+ var _te = new TextEncoder;
4837
+ var _td = new TextDecoder;
4688
4838
  class TokenCache {
4689
4839
  instanceCache = new Map;
4690
4840
  permissionCache = new Map;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcote.tech/arc-adapter-db-sqlite",
3
- "version": "0.7.26",
3
+ "version": "0.7.28",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -20,7 +20,7 @@
20
20
  "test": "bun test"
21
21
  },
22
22
  "peerDependencies": {
23
- "@arcote.tech/arc": "^0.7.26"
23
+ "@arcote.tech/arc": "^0.7.28"
24
24
  },
25
25
  "devDependencies": {
26
26
  "typescript": "^5.0.0"