@arcote.tech/arc-adapter-db-sqlite-wasm 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
@@ -1779,9 +1779,21 @@ class LocalEventPublisher {
1779
1779
  views = [];
1780
1780
  syncCallback;
1781
1781
  subscribers = new Map;
1782
+ pending = new Set;
1782
1783
  constructor(dataStorage) {
1783
1784
  this.dataStorage = dataStorage;
1784
1785
  }
1786
+ track(work) {
1787
+ const wrapped = Promise.resolve(work).catch(() => {}).finally(() => {
1788
+ this.pending.delete(wrapped);
1789
+ });
1790
+ this.pending.add(wrapped);
1791
+ }
1792
+ async whenIdle() {
1793
+ while (this.pending.size > 0) {
1794
+ await Promise.all([...this.pending]);
1795
+ }
1796
+ }
1785
1797
  onPublish(callback) {
1786
1798
  this.syncCallback = callback;
1787
1799
  }
@@ -1977,6 +1989,47 @@ class LocalEventPublisher {
1977
1989
  return allViewChanges;
1978
1990
  }
1979
1991
  }
1992
+ function murmurHash(key, seed = 0) {
1993
+ let remainder, bytes, h1, h1b, c1, c2, k1, i;
1994
+ remainder = key.length & 3;
1995
+ bytes = key.length - remainder;
1996
+ h1 = seed;
1997
+ c1 = 3432918353;
1998
+ c2 = 461845907;
1999
+ i = 0;
2000
+ while (i < bytes) {
2001
+ k1 = key.charCodeAt(i) & 255 | (key.charCodeAt(++i) & 255) << 8 | (key.charCodeAt(++i) & 255) << 16 | (key.charCodeAt(++i) & 255) << 24;
2002
+ ++i;
2003
+ k1 = (k1 & 65535) * c1 + (((k1 >>> 16) * c1 & 65535) << 16) & 4294967295;
2004
+ k1 = k1 << 15 | k1 >>> 17;
2005
+ k1 = (k1 & 65535) * c2 + (((k1 >>> 16) * c2 & 65535) << 16) & 4294967295;
2006
+ h1 ^= k1;
2007
+ h1 = h1 << 13 | h1 >>> 19;
2008
+ h1b = (h1 & 65535) * 5 + (((h1 >>> 16) * 5 & 65535) << 16) & 4294967295;
2009
+ h1 = (h1b & 65535) + 27492 + (((h1b >>> 16) + 58964 & 65535) << 16);
2010
+ }
2011
+ k1 = 0;
2012
+ if (remainder >= 3) {
2013
+ k1 ^= (key.charCodeAt(i + 2) & 255) << 16;
2014
+ }
2015
+ if (remainder >= 2) {
2016
+ k1 ^= (key.charCodeAt(i + 1) & 255) << 8;
2017
+ }
2018
+ if (remainder >= 1) {
2019
+ k1 ^= key.charCodeAt(i) & 255;
2020
+ k1 = (k1 & 65535) * c1 + (((k1 >>> 16) * c1 & 65535) << 16) & 4294967295;
2021
+ k1 = k1 << 15 | k1 >>> 17;
2022
+ k1 = (k1 & 65535) * c2 + (((k1 >>> 16) * c2 & 65535) << 16) & 4294967295;
2023
+ h1 ^= k1;
2024
+ }
2025
+ h1 ^= key.length;
2026
+ h1 ^= h1 >>> 16;
2027
+ h1 = (h1 & 65535) * 2246822507 + (((h1 >>> 16) * 2246822507 & 65535) << 16) & 4294967295;
2028
+ h1 ^= h1 >>> 13;
2029
+ h1 = (h1 & 65535) * 3266489909 + (((h1 >>> 16) * 3266489909 & 65535) << 16) & 4294967295;
2030
+ h1 ^= h1 >>> 16;
2031
+ return h1 >>> 0;
2032
+ }
1980
2033
  class ArcOptional {
1981
2034
  parent;
1982
2035
  constructor(parent) {
@@ -2068,7 +2121,9 @@ class ArcDefault {
2068
2121
  this.defaultValueOrCallback = defaultValueOrCallback;
2069
2122
  }
2070
2123
  validate(value) {
2071
- throw new Error("Method not implemented.");
2124
+ if (value === undefined || value === null)
2125
+ return false;
2126
+ return this.parent.validate(value);
2072
2127
  }
2073
2128
  parse(value) {
2074
2129
  if (value)
@@ -2594,6 +2649,31 @@ class ScopedDataStorage extends DataStorage {
2594
2649
  return this.#inner.getReadWriteTransaction();
2595
2650
  }
2596
2651
  }
2652
+ class ArcAuthenticationError extends Error {
2653
+ arcCode = "AUTHENTICATION";
2654
+ constructor(message = "Authentication required") {
2655
+ super(message);
2656
+ this.name = "ArcAuthenticationError";
2657
+ }
2658
+ }
2659
+
2660
+ class ArcAuthorizationError extends Error {
2661
+ arcCode = "AUTHORIZATION";
2662
+ constructor(message = "Forbidden") {
2663
+ super(message);
2664
+ this.name = "ArcAuthorizationError";
2665
+ }
2666
+ }
2667
+
2668
+ class ArcValidationError extends Error {
2669
+ arcCode = "VALIDATION";
2670
+ fields;
2671
+ constructor(errors, message = "Invalid parameters") {
2672
+ super(message);
2673
+ this.name = "ArcValidationError";
2674
+ this.fields = errors && typeof errors === "object" ? Object.keys(errors) : [];
2675
+ }
2676
+ }
2597
2677
  class ArcPrimitive extends ArcAbstract {
2598
2678
  serialize(value) {
2599
2679
  return value;
@@ -2766,6 +2846,8 @@ class ArcFragmentBase {
2766
2846
  class ArcContextElement extends ArcFragmentBase {
2767
2847
  name;
2768
2848
  types = ["context-element"];
2849
+ __contextId;
2850
+ __contextName;
2769
2851
  get id() {
2770
2852
  return this.name;
2771
2853
  }
@@ -3105,6 +3187,27 @@ class ArcFunction {
3105
3187
  get results() {
3106
3188
  return this.data.results;
3107
3189
  }
3190
+ validateParams(params) {
3191
+ const schema = this.data.params;
3192
+ if (!schema || typeof schema.validate !== "function")
3193
+ return;
3194
+ const errors = schema.validate(params ?? {});
3195
+ if (errors)
3196
+ throw new ArcValidationError(errors);
3197
+ }
3198
+ async authorize(adapters) {
3199
+ if (!this.hasProtections)
3200
+ return;
3201
+ const decoded = adapters.authAdapter?.getDecoded?.();
3202
+ if (!decoded) {
3203
+ throw new ArcAuthenticationError;
3204
+ }
3205
+ const instances = this.data.protections.filter((p) => p.token?.name === decoded.tokenName).map((p) => p.token.create(decoded.params, adapters));
3206
+ const ok = await this.verifyProtections(instances);
3207
+ if (!ok) {
3208
+ throw new ArcAuthorizationError;
3209
+ }
3210
+ }
3108
3211
  async verifyProtections(tokens) {
3109
3212
  if (!this.data.protections || this.data.protections.length === 0) {
3110
3213
  return true;
@@ -3265,6 +3368,8 @@ class ArcCommand extends ArcContextElement {
3265
3368
  if (!this.data.handler) {
3266
3369
  throw new Error(`Command "${this.data.name}" has no handler`);
3267
3370
  }
3371
+ this.#fn.validateParams(params);
3372
+ await this.#fn.authorize(adapters);
3268
3373
  const context2 = this.buildCommandContext(adapters);
3269
3374
  return await this.data.handler(context2, params);
3270
3375
  }
@@ -3375,9 +3480,10 @@ class ArcListener extends ArcContextElement {
3375
3480
  }
3376
3481
  }
3377
3482
  if (this.data.isAsync) {
3378
- Promise.resolve(this.data.handler(context2, event2)).catch((error) => {
3483
+ const work = Promise.resolve(this.data.handler(context2, event2)).catch((error) => {
3379
3484
  console.error(`Async listener "${this.data.name}" error:`, error);
3380
3485
  });
3486
+ adapters.eventPublisher?.track?.(work);
3381
3487
  } else {
3382
3488
  await this.data.handler(context2, event2);
3383
3489
  }
@@ -3584,48 +3690,6 @@ function deepMerge(target, source) {
3584
3690
  function isPlainObject(item) {
3585
3691
  return item && typeof item === "object" && !Array.isArray(item) && !(item instanceof Date) && Object.prototype.toString.call(item) === "[object Object]";
3586
3692
  }
3587
- function murmurHash(key, seed = 0) {
3588
- let remainder, bytes, h1, h1b, c1, c2, k1, i;
3589
- remainder = key.length & 3;
3590
- bytes = key.length - remainder;
3591
- h1 = seed;
3592
- c1 = 3432918353;
3593
- c2 = 461845907;
3594
- i = 0;
3595
- while (i < bytes) {
3596
- k1 = key.charCodeAt(i) & 255 | (key.charCodeAt(++i) & 255) << 8 | (key.charCodeAt(++i) & 255) << 16 | (key.charCodeAt(++i) & 255) << 24;
3597
- ++i;
3598
- k1 = (k1 & 65535) * c1 + (((k1 >>> 16) * c1 & 65535) << 16) & 4294967295;
3599
- k1 = k1 << 15 | k1 >>> 17;
3600
- k1 = (k1 & 65535) * c2 + (((k1 >>> 16) * c2 & 65535) << 16) & 4294967295;
3601
- h1 ^= k1;
3602
- h1 = h1 << 13 | h1 >>> 19;
3603
- h1b = (h1 & 65535) * 5 + (((h1 >>> 16) * 5 & 65535) << 16) & 4294967295;
3604
- h1 = (h1b & 65535) + 27492 + (((h1b >>> 16) + 58964 & 65535) << 16);
3605
- }
3606
- k1 = 0;
3607
- if (remainder >= 3) {
3608
- k1 ^= (key.charCodeAt(i + 2) & 255) << 16;
3609
- }
3610
- if (remainder >= 2) {
3611
- k1 ^= (key.charCodeAt(i + 1) & 255) << 8;
3612
- }
3613
- if (remainder >= 1) {
3614
- k1 ^= key.charCodeAt(i) & 255;
3615
- k1 = (k1 & 65535) * c1 + (((k1 >>> 16) * c1 & 65535) << 16) & 4294967295;
3616
- k1 = k1 << 15 | k1 >>> 17;
3617
- k1 = (k1 & 65535) * c2 + (((k1 >>> 16) * c2 & 65535) << 16) & 4294967295;
3618
- h1 ^= k1;
3619
- }
3620
- h1 ^= key.length;
3621
- h1 ^= h1 >>> 16;
3622
- h1 = (h1 & 65535) * 2246822507 + (((h1 >>> 16) * 2246822507 & 65535) << 16) & 4294967295;
3623
- h1 ^= h1 >>> 13;
3624
- h1 = (h1 & 65535) * 3266489909 + (((h1 >>> 16) * 3266489909 & 65535) << 16) & 4294967295;
3625
- h1 ^= h1 >>> 16;
3626
- return h1 >>> 0;
3627
- }
3628
-
3629
3693
  class ForkedStoreState extends StoreState {
3630
3694
  master;
3631
3695
  changedItems = new Map;
@@ -4682,6 +4746,8 @@ class SecuredDataStorage {
4682
4746
  return this.dataStorage.fork();
4683
4747
  }
4684
4748
  }
4749
+ var _te = new TextEncoder;
4750
+ var _td = new TextDecoder;
4685
4751
  class TokenCache {
4686
4752
  instanceCache = new Map;
4687
4753
  permissionCache = new Map;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcote.tech/arc-adapter-db-sqlite-wasm",
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",
@@ -39,7 +39,7 @@
39
39
  "dev": "bun build ./src/index.ts ./src/worker.ts --outdir ./dist --target browser --format esm --watch"
40
40
  },
41
41
  "peerDependencies": {
42
- "@arcote.tech/arc": "^0.7.25",
42
+ "@arcote.tech/arc": "^0.7.27",
43
43
  "@sqlite.org/sqlite-wasm": "^3.46.0-build1"
44
44
  },
45
45
  "devDependencies": {