@niscorp/vex 0.2.0 → 0.2.2

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/index.cjs CHANGED
@@ -1,7 +1,6 @@
1
1
  'use strict';
2
2
 
3
3
  var zod = require('zod');
4
- var crypto = require('crypto');
5
4
  var strata = require('@niscorp/strata');
6
5
  var prism = require('@niscorp/prism');
7
6
  var postgres = require('@niscorp/strata/postgres');
@@ -1302,6 +1301,155 @@ var createMemoryCache = () => {
1302
1301
  };
1303
1302
  return { get, set, delete: del, clear, keys, entries };
1304
1303
  };
1304
+
1305
+ // src/utils/sha256.ts
1306
+ var ROUND_CONSTANTS = new Uint32Array([
1307
+ 1116352408,
1308
+ 1899447441,
1309
+ 3049323471,
1310
+ 3921009573,
1311
+ 961987163,
1312
+ 1508970993,
1313
+ 2453635748,
1314
+ 2870763221,
1315
+ 3624381080,
1316
+ 310598401,
1317
+ 607225278,
1318
+ 1426881987,
1319
+ 1925078388,
1320
+ 2162078206,
1321
+ 2614888103,
1322
+ 3248222580,
1323
+ 3835390401,
1324
+ 4022224774,
1325
+ 264347078,
1326
+ 604807628,
1327
+ 770255983,
1328
+ 1249150122,
1329
+ 1555081692,
1330
+ 1996064986,
1331
+ 2554220882,
1332
+ 2821834349,
1333
+ 2952996808,
1334
+ 3210313671,
1335
+ 3336571891,
1336
+ 3584528711,
1337
+ 113926993,
1338
+ 338241895,
1339
+ 666307205,
1340
+ 773529912,
1341
+ 1294757372,
1342
+ 1396182291,
1343
+ 1695183700,
1344
+ 1986661051,
1345
+ 2177026350,
1346
+ 2456956037,
1347
+ 2730485921,
1348
+ 2820302411,
1349
+ 3259730800,
1350
+ 3345764771,
1351
+ 3516065817,
1352
+ 3600352804,
1353
+ 4094571909,
1354
+ 275423344,
1355
+ 430227734,
1356
+ 506948616,
1357
+ 659060556,
1358
+ 883997877,
1359
+ 958139571,
1360
+ 1322822218,
1361
+ 1537002063,
1362
+ 1747873779,
1363
+ 1955562222,
1364
+ 2024104815,
1365
+ 2227730452,
1366
+ 2361852424,
1367
+ 2428436474,
1368
+ 2756734187,
1369
+ 3204031479,
1370
+ 3329325298
1371
+ ]);
1372
+ var BLOCK_BYTES = 64;
1373
+ var rotateRight = (word, bits) => word >>> bits | word << 32 - bits;
1374
+ var wordHex = (word) => (word >>> 0).toString(16).padStart(8, "0");
1375
+ var sha256Hex = (text) => {
1376
+ const bytes = new TextEncoder().encode(text);
1377
+ const padded = new Uint8Array(Math.ceil((bytes.length + 9) / BLOCK_BYTES) * BLOCK_BYTES);
1378
+ padded.set(bytes);
1379
+ padded[bytes.length] = 128;
1380
+ const view = new DataView(padded.buffer);
1381
+ const bitLength = bytes.length * 8;
1382
+ view.setUint32(padded.length - 8, Math.floor(bitLength / 4294967296));
1383
+ view.setUint32(padded.length - 4, bitLength >>> 0);
1384
+ let h0 = 1779033703;
1385
+ let h1 = 3144134277;
1386
+ let h2 = 1013904242;
1387
+ let h3 = 2773480762;
1388
+ let h4 = 1359893119;
1389
+ let h5 = 2600822924;
1390
+ let h6 = 528734635;
1391
+ let h7 = 1541459225;
1392
+ const schedule = new Uint32Array(64);
1393
+ for (let offset = 0; offset < padded.length; offset += BLOCK_BYTES) {
1394
+ for (let i = 0; i < 16; i += 1) schedule[i] = view.getUint32(offset + i * 4);
1395
+ for (let i = 16; i < 64; i += 1) {
1396
+ const w15 = schedule[i - 15] ?? 0;
1397
+ const w2 = schedule[i - 2] ?? 0;
1398
+ const s0 = rotateRight(w15, 7) ^ rotateRight(w15, 18) ^ w15 >>> 3;
1399
+ const s1 = rotateRight(w2, 17) ^ rotateRight(w2, 19) ^ w2 >>> 10;
1400
+ schedule[i] = (schedule[i - 16] ?? 0) + s0 + (schedule[i - 7] ?? 0) + s1;
1401
+ }
1402
+ let a = h0;
1403
+ let b = h1;
1404
+ let c = h2;
1405
+ let d = h3;
1406
+ let e = h4;
1407
+ let f = h5;
1408
+ let g = h6;
1409
+ let h = h7;
1410
+ for (let i = 0; i < 64; i += 1) {
1411
+ const sum1 = rotateRight(e, 6) ^ rotateRight(e, 11) ^ rotateRight(e, 25);
1412
+ const choice = e & f ^ ~e & g;
1413
+ const temp1 = h + sum1 + choice + (ROUND_CONSTANTS[i] ?? 0) + (schedule[i] ?? 0) | 0;
1414
+ const sum0 = rotateRight(a, 2) ^ rotateRight(a, 13) ^ rotateRight(a, 22);
1415
+ const majority = a & b ^ a & c ^ b & c;
1416
+ const temp2 = sum0 + majority | 0;
1417
+ h = g;
1418
+ g = f;
1419
+ f = e;
1420
+ e = d + temp1 | 0;
1421
+ d = c;
1422
+ c = b;
1423
+ b = a;
1424
+ a = temp1 + temp2 | 0;
1425
+ }
1426
+ h0 = h0 + a | 0;
1427
+ h1 = h1 + b | 0;
1428
+ h2 = h2 + c | 0;
1429
+ h3 = h3 + d | 0;
1430
+ h4 = h4 + e | 0;
1431
+ h5 = h5 + f | 0;
1432
+ h6 = h6 + g | 0;
1433
+ h7 = h7 + h | 0;
1434
+ }
1435
+ return wordHex(h0) + wordHex(h1) + wordHex(h2) + wordHex(h3) + wordHex(h4) + wordHex(h5) + wordHex(h6) + wordHex(h7);
1436
+ };
1437
+
1438
+ // src/utils/canonical.ts
1439
+ var canonical = (value) => {
1440
+ if (Array.isArray(value)) return value.map(canonical);
1441
+ if (value !== null && typeof value === "object" && !(value instanceof Date)) {
1442
+ const sorted = {};
1443
+ for (const [key, inner] of Object.entries(value).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)) sorted[key] = canonical(inner);
1444
+ return sorted;
1445
+ }
1446
+ return value;
1447
+ };
1448
+ var canonicalText = (value) => JSON.stringify(canonical(value)) ?? "undefined";
1449
+ var hashText = (text) => sha256Hex(text);
1450
+ var canonicalHash = (value) => hashText(canonicalText(value));
1451
+
1452
+ // src/cache/hash.ts
1305
1453
  var isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
1306
1454
  var normalizeShape = (shape) => {
1307
1455
  if (shape === null) return "null";
@@ -1323,25 +1471,25 @@ var normalizeShape = (shape) => {
1323
1471
  if (typeof shape === "boolean") return "boolean";
1324
1472
  return "unknown";
1325
1473
  };
1326
- var mintFingerprint = () => `fp_${crypto.createHash("sha256").update(`${Date.now()}:${Math.random()}`).digest("hex").slice(0, 16)}`;
1474
+ var mintFingerprint = () => `fp_${hashText(`${Date.now()}:${Math.random()}`).slice(0, 16)}`;
1327
1475
  var computeRequestHash = (request) => {
1328
1476
  const identity = {
1329
1477
  intent: request.intent ?? null,
1330
1478
  shape: normalizeShape(request.shape),
1331
1479
  contextKeys: Object.keys(request.context ?? {}).sort()
1332
1480
  };
1333
- return crypto.createHash("sha256").update(JSON.stringify(identity)).digest("hex");
1481
+ return hashText(JSON.stringify(identity));
1334
1482
  };
1335
- var canonical = (value) => {
1336
- if (Array.isArray(value)) return value.map(canonical);
1483
+ var canonical2 = (value) => {
1484
+ if (Array.isArray(value)) return value.map(canonical2);
1337
1485
  if (isRecord(value)) {
1338
1486
  const out = {};
1339
- for (const key of Object.keys(value).sort()) out[key] = canonical(value[key]);
1487
+ for (const key of Object.keys(value).sort()) out[key] = canonical2(value[key]);
1340
1488
  return out;
1341
1489
  }
1342
1490
  return value;
1343
1491
  };
1344
- var computePolicyKey = (policy) => policy === void 0 ? "none" : crypto.createHash("sha256").update(JSON.stringify(canonical(policy))).digest("hex").slice(0, 16);
1492
+ var computePolicyKey = (policy) => policy === void 0 ? "none" : hashText(JSON.stringify(canonical2(policy))).slice(0, 16);
1345
1493
  var computeSchemaFingerprint = (schema) => {
1346
1494
  const byName = (a, b) => a.name.localeCompare(b.name);
1347
1495
  const stable = {
@@ -1371,7 +1519,7 @@ var computeSchemaFingerprint = (schema) => {
1371
1519
  }))
1372
1520
  }))
1373
1521
  };
1374
- return crypto.createHash("sha256").update(JSON.stringify(stable)).digest("hex");
1522
+ return hashText(JSON.stringify(stable));
1375
1523
  };
1376
1524
 
1377
1525
  // src/cache/util.ts
@@ -1403,18 +1551,6 @@ var sweepCache = async (cache, options) => {
1403
1551
  }
1404
1552
  return evicted;
1405
1553
  };
1406
- var canonical2 = (value) => {
1407
- if (Array.isArray(value)) return value.map(canonical2);
1408
- if (value !== null && typeof value === "object" && !(value instanceof Date)) {
1409
- const sorted = {};
1410
- for (const [key, inner] of Object.entries(value).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)) sorted[key] = canonical2(inner);
1411
- return sorted;
1412
- }
1413
- return value;
1414
- };
1415
- var canonicalText = (value) => JSON.stringify(canonical2(value)) ?? "undefined";
1416
- var hashText = (text) => crypto.createHash("sha256").update(text).digest("hex");
1417
- var canonicalHash = (value) => hashText(canonicalText(value));
1418
1554
 
1419
1555
  // src/engine/live.ts
1420
1556
  var DEFAULT_TTL_MS = 6e4;
@@ -2767,9 +2903,8 @@ var createPostgresAdapter = (config) => {
2767
2903
  return enforcement;
2768
2904
  };
2769
2905
  const execute = async (query, params) => {
2770
- const { transaction } = pool;
2771
2906
  const limit = perStatementMs;
2772
- const result = limit === void 0 || transaction === void 0 ? await pool.query(query.sql, params) : await transaction(async (tx) => {
2907
+ const result = limit === void 0 || pool.transaction === void 0 ? await pool.query(query.sql, params) : await pool.transaction(async (tx) => {
2773
2908
  await tx.query(`SET LOCAL statement_timeout = ${limit}`);
2774
2909
  return tx.query(query.sql, params);
2775
2910
  });