@arcote.tech/arc-adapter-db-postgres 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 +116 -50
  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;
@@ -5249,7 +5315,7 @@ kebab.column.to = fromKebab;
5249
5315
  // ../../../../node_modules/.bun/postgres@3.4.9/node_modules/postgres/src/connection.js
5250
5316
  import net from "net";
5251
5317
  import tls from "tls";
5252
- import crypto from "crypto";
5318
+ import crypto2 from "crypto";
5253
5319
  import Stream from "stream";
5254
5320
  import { performance } from "perf_hooks";
5255
5321
 
@@ -5830,14 +5896,14 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
5830
5896
  write(bytes_default().p().str(payload).z(1).end());
5831
5897
  }
5832
5898
  async function SASL() {
5833
- nonce = (await crypto.randomBytes(18)).toString("base64");
5899
+ nonce = (await crypto2.randomBytes(18)).toString("base64");
5834
5900
  bytes_default().p().str("SCRAM-SHA-256" + bytes_default.N);
5835
5901
  const i = bytes_default.i;
5836
5902
  write(bytes_default.inc(4).str("n,,n=*,r=" + nonce).i32(bytes_default.i - i - 4, i).end());
5837
5903
  }
5838
5904
  async function SASLContinue(x) {
5839
5905
  const res = x.toString("utf8", 9).split(",").reduce((acc, x2) => (acc[x2[0]] = x2.slice(2), acc), {});
5840
- const saltedPassword = await crypto.pbkdf2Sync(await Pass(), Buffer.from(res.s, "base64"), parseInt(res.i), 32, "sha256");
5906
+ const saltedPassword = await crypto2.pbkdf2Sync(await Pass(), Buffer.from(res.s, "base64"), parseInt(res.i), 32, "sha256");
5841
5907
  const clientKey = await hmac(saltedPassword, "Client Key");
5842
5908
  const auth = "n=*,r=" + nonce + "," + "r=" + res.r + ",s=" + res.s + ",i=" + res.i + ",c=biws,r=" + res.r;
5843
5909
  serverSignature = (await hmac(await hmac(saltedPassword, "Server Key"), auth)).toString("base64");
@@ -6058,13 +6124,13 @@ function parseError(x) {
6058
6124
  return error;
6059
6125
  }
6060
6126
  function md5(x) {
6061
- return crypto.createHash("md5").update(x).digest("hex");
6127
+ return crypto2.createHash("md5").update(x).digest("hex");
6062
6128
  }
6063
6129
  function hmac(key, x) {
6064
- return crypto.createHmac("sha256", key).update(x).digest();
6130
+ return crypto2.createHmac("sha256", key).update(x).digest();
6065
6131
  }
6066
6132
  function sha256(x) {
6067
- return crypto.createHash("sha256").update(x).digest();
6133
+ return crypto2.createHash("sha256").update(x).digest();
6068
6134
  }
6069
6135
  function xor(a, b2) {
6070
6136
  const length = Math.max(a.length, b2.length);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcote.tech/arc-adapter-db-postgres",
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",
@@ -23,7 +23,7 @@
23
23
  "postgres": "^3.4.4"
24
24
  },
25
25
  "peerDependencies": {
26
- "@arcote.tech/arc": "^0.7.25"
26
+ "@arcote.tech/arc": "^0.7.27"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@types/pg": "^8.11.0",