@arcote.tech/arc-adapter-db-sqlite 0.7.25 → 0.7.27

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 +110 -44
  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
  }
@@ -1980,6 +1992,47 @@ class LocalEventPublisher {
1980
1992
  return allViewChanges;
1981
1993
  }
1982
1994
  }
1995
+ function murmurHash(key, seed = 0) {
1996
+ let remainder, bytes, h1, h1b, c1, c2, k1, i;
1997
+ remainder = key.length & 3;
1998
+ bytes = key.length - remainder;
1999
+ h1 = seed;
2000
+ c1 = 3432918353;
2001
+ c2 = 461845907;
2002
+ i = 0;
2003
+ while (i < bytes) {
2004
+ k1 = key.charCodeAt(i) & 255 | (key.charCodeAt(++i) & 255) << 8 | (key.charCodeAt(++i) & 255) << 16 | (key.charCodeAt(++i) & 255) << 24;
2005
+ ++i;
2006
+ k1 = (k1 & 65535) * c1 + (((k1 >>> 16) * c1 & 65535) << 16) & 4294967295;
2007
+ k1 = k1 << 15 | k1 >>> 17;
2008
+ k1 = (k1 & 65535) * c2 + (((k1 >>> 16) * c2 & 65535) << 16) & 4294967295;
2009
+ h1 ^= k1;
2010
+ h1 = h1 << 13 | h1 >>> 19;
2011
+ h1b = (h1 & 65535) * 5 + (((h1 >>> 16) * 5 & 65535) << 16) & 4294967295;
2012
+ h1 = (h1b & 65535) + 27492 + (((h1b >>> 16) + 58964 & 65535) << 16);
2013
+ }
2014
+ k1 = 0;
2015
+ if (remainder >= 3) {
2016
+ k1 ^= (key.charCodeAt(i + 2) & 255) << 16;
2017
+ }
2018
+ if (remainder >= 2) {
2019
+ k1 ^= (key.charCodeAt(i + 1) & 255) << 8;
2020
+ }
2021
+ if (remainder >= 1) {
2022
+ k1 ^= key.charCodeAt(i) & 255;
2023
+ k1 = (k1 & 65535) * c1 + (((k1 >>> 16) * c1 & 65535) << 16) & 4294967295;
2024
+ k1 = k1 << 15 | k1 >>> 17;
2025
+ k1 = (k1 & 65535) * c2 + (((k1 >>> 16) * c2 & 65535) << 16) & 4294967295;
2026
+ h1 ^= k1;
2027
+ }
2028
+ h1 ^= key.length;
2029
+ h1 ^= h1 >>> 16;
2030
+ h1 = (h1 & 65535) * 2246822507 + (((h1 >>> 16) * 2246822507 & 65535) << 16) & 4294967295;
2031
+ h1 ^= h1 >>> 13;
2032
+ h1 = (h1 & 65535) * 3266489909 + (((h1 >>> 16) * 3266489909 & 65535) << 16) & 4294967295;
2033
+ h1 ^= h1 >>> 16;
2034
+ return h1 >>> 0;
2035
+ }
1983
2036
  class ArcOptional {
1984
2037
  parent;
1985
2038
  constructor(parent) {
@@ -2071,7 +2124,9 @@ class ArcDefault {
2071
2124
  this.defaultValueOrCallback = defaultValueOrCallback;
2072
2125
  }
2073
2126
  validate(value) {
2074
- throw new Error("Method not implemented.");
2127
+ if (value === undefined || value === null)
2128
+ return false;
2129
+ return this.parent.validate(value);
2075
2130
  }
2076
2131
  parse(value) {
2077
2132
  if (value)
@@ -2597,6 +2652,31 @@ class ScopedDataStorage extends DataStorage {
2597
2652
  return this.#inner.getReadWriteTransaction();
2598
2653
  }
2599
2654
  }
2655
+ class ArcAuthenticationError extends Error {
2656
+ arcCode = "AUTHENTICATION";
2657
+ constructor(message = "Authentication required") {
2658
+ super(message);
2659
+ this.name = "ArcAuthenticationError";
2660
+ }
2661
+ }
2662
+
2663
+ class ArcAuthorizationError extends Error {
2664
+ arcCode = "AUTHORIZATION";
2665
+ constructor(message = "Forbidden") {
2666
+ super(message);
2667
+ this.name = "ArcAuthorizationError";
2668
+ }
2669
+ }
2670
+
2671
+ class ArcValidationError extends Error {
2672
+ arcCode = "VALIDATION";
2673
+ fields;
2674
+ constructor(errors, message = "Invalid parameters") {
2675
+ super(message);
2676
+ this.name = "ArcValidationError";
2677
+ this.fields = errors && typeof errors === "object" ? Object.keys(errors) : [];
2678
+ }
2679
+ }
2600
2680
  class ArcPrimitive extends ArcAbstract {
2601
2681
  serialize(value) {
2602
2682
  return value;
@@ -2769,6 +2849,8 @@ class ArcFragmentBase {
2769
2849
  class ArcContextElement extends ArcFragmentBase {
2770
2850
  name;
2771
2851
  types = ["context-element"];
2852
+ __contextId;
2853
+ __contextName;
2772
2854
  get id() {
2773
2855
  return this.name;
2774
2856
  }
@@ -3108,6 +3190,27 @@ class ArcFunction {
3108
3190
  get results() {
3109
3191
  return this.data.results;
3110
3192
  }
3193
+ validateParams(params) {
3194
+ const schema = this.data.params;
3195
+ if (!schema || typeof schema.validate !== "function")
3196
+ return;
3197
+ const errors = schema.validate(params ?? {});
3198
+ if (errors)
3199
+ throw new ArcValidationError(errors);
3200
+ }
3201
+ async authorize(adapters) {
3202
+ if (!this.hasProtections)
3203
+ return;
3204
+ const decoded = adapters.authAdapter?.getDecoded?.();
3205
+ if (!decoded) {
3206
+ throw new ArcAuthenticationError;
3207
+ }
3208
+ const instances = this.data.protections.filter((p) => p.token?.name === decoded.tokenName).map((p) => p.token.create(decoded.params, adapters));
3209
+ const ok = await this.verifyProtections(instances);
3210
+ if (!ok) {
3211
+ throw new ArcAuthorizationError;
3212
+ }
3213
+ }
3111
3214
  async verifyProtections(tokens) {
3112
3215
  if (!this.data.protections || this.data.protections.length === 0) {
3113
3216
  return true;
@@ -3268,6 +3371,8 @@ class ArcCommand extends ArcContextElement {
3268
3371
  if (!this.data.handler) {
3269
3372
  throw new Error(`Command "${this.data.name}" has no handler`);
3270
3373
  }
3374
+ this.#fn.validateParams(params);
3375
+ await this.#fn.authorize(adapters);
3271
3376
  const context2 = this.buildCommandContext(adapters);
3272
3377
  return await this.data.handler(context2, params);
3273
3378
  }
@@ -3378,9 +3483,10 @@ class ArcListener extends ArcContextElement {
3378
3483
  }
3379
3484
  }
3380
3485
  if (this.data.isAsync) {
3381
- Promise.resolve(this.data.handler(context2, event2)).catch((error) => {
3486
+ const work = Promise.resolve(this.data.handler(context2, event2)).catch((error) => {
3382
3487
  console.error(`Async listener "${this.data.name}" error:`, error);
3383
3488
  });
3489
+ adapters.eventPublisher?.track?.(work);
3384
3490
  } else {
3385
3491
  await this.data.handler(context2, event2);
3386
3492
  }
@@ -3587,48 +3693,6 @@ function deepMerge(target, source) {
3587
3693
  function isPlainObject(item) {
3588
3694
  return item && typeof item === "object" && !Array.isArray(item) && !(item instanceof Date) && Object.prototype.toString.call(item) === "[object Object]";
3589
3695
  }
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
3696
  class ForkedStoreState extends StoreState {
3633
3697
  master;
3634
3698
  changedItems = new Map;
@@ -4685,6 +4749,8 @@ class SecuredDataStorage {
4685
4749
  return this.dataStorage.fork();
4686
4750
  }
4687
4751
  }
4752
+ var _te = new TextEncoder;
4753
+ var _td = new TextDecoder;
4688
4754
  class TokenCache {
4689
4755
  instanceCache = new Map;
4690
4756
  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.25",
3
+ "version": "0.7.27",
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.25"
23
+ "@arcote.tech/arc": "^0.7.27"
24
24
  },
25
25
  "devDependencies": {
26
26
  "typescript": "^5.0.0"