@rpcbase/server 0.531.0 → 0.532.0

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.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { n as __commonJSMin, r as __toESM, t as sendEmail } from "./email-Dzauaq11.js";
2
- import { a as normalizeRtsQueryOptions, c as runRtsQuery, i as isRtsRequestAuthorized, l as getDerivedKey, r as buildRtsAbilityFromRequest, s as resolveRtsRequestTenantId } from "./queryExecutor-DYVlCvns.js";
2
+ import { a as normalizeRtsQueryOptions, c as runRtsCount, i as isRtsRequestAuthorized, l as runRtsQuery, r as buildRtsAbilityFromRequest, s as resolveRtsRequestTenantId, u as getDerivedKey } from "./queryExecutor-JadZcQSQ.js";
3
3
  import session from "express-session";
4
4
  import { RedisStore } from "connect-redis";
5
5
  import MongoStore from "connect-mongo";
@@ -4611,9 +4611,11 @@ var createRtsSsrCollector = (req, opts) => {
4611
4611
  const maxDocsPerQuery = typeof opts?.maxDocsPerQuery === "number" && opts.maxDocsPerQuery > 0 ? Math.floor(opts.maxDocsPerQuery) : DEFAULT_MAX_DOCS_PER_QUERY;
4612
4612
  const maxSerializedBytes = typeof opts?.maxSerializedBytes === "number" && opts.maxSerializedBytes > 0 ? Math.floor(opts.maxSerializedBytes) : DEFAULT_MAX_SERIALIZED_BYTES;
4613
4613
  const registrations = /* @__PURE__ */ new Map();
4614
+ const countRegistrations = /* @__PURE__ */ new Map();
4614
4615
  const resolved = /* @__PURE__ */ new Map();
4615
4616
  const resolvedPageInfo = /* @__PURE__ */ new Map();
4616
4617
  const resolvedTotalCount = /* @__PURE__ */ new Map();
4618
+ const resolvedCounts = /* @__PURE__ */ new Map();
4617
4619
  const runtime = {
4618
4620
  registerQuery(query) {
4619
4621
  const modelName = typeof query.modelName === "string" ? query.modelName.trim() : "";
@@ -4621,7 +4623,7 @@ var createRtsSsrCollector = (req, opts) => {
4621
4623
  if (!modelName || !queryKey) return;
4622
4624
  const key = makeRegistrationKey(modelName, queryKey);
4623
4625
  if (registrations.has(key)) return;
4624
- if (registrations.size >= maxQueries) return;
4626
+ if (registrations.size + countRegistrations.size >= maxQueries) return;
4625
4627
  registrations.set(key, {
4626
4628
  modelName,
4627
4629
  queryKey,
@@ -4629,6 +4631,20 @@ var createRtsSsrCollector = (req, opts) => {
4629
4631
  options: query.options ?? {}
4630
4632
  });
4631
4633
  },
4634
+ registerCount(query) {
4635
+ const modelName = typeof query.modelName === "string" ? query.modelName.trim() : "";
4636
+ const queryKey = typeof query.queryKey === "string" ? query.queryKey.trim() : "";
4637
+ if (!modelName || !queryKey) return;
4638
+ const key = makeRegistrationKey(modelName, queryKey);
4639
+ if (countRegistrations.has(key)) return;
4640
+ if (registrations.size + countRegistrations.size >= maxQueries) return;
4641
+ countRegistrations.set(key, {
4642
+ modelName,
4643
+ queryKey,
4644
+ query: query.query ?? {},
4645
+ options: query.options ?? {}
4646
+ });
4647
+ },
4632
4648
  getQueryData(modelName, queryKey) {
4633
4649
  return resolved.get(makeRegistrationKey(modelName, queryKey));
4634
4650
  },
@@ -4637,15 +4653,19 @@ var createRtsSsrCollector = (req, opts) => {
4637
4653
  },
4638
4654
  getQueryTotalCount(modelName, queryKey) {
4639
4655
  return resolvedTotalCount.get(makeRegistrationKey(modelName, queryKey));
4656
+ },
4657
+ getCount(modelName, queryKey) {
4658
+ return resolvedCounts.get(makeRegistrationKey(modelName, queryKey));
4640
4659
  }
4641
4660
  };
4642
4661
  const resolve = async () => {
4643
- if (!registrations.size) return null;
4662
+ if (!registrations.size && !countRegistrations.size) return null;
4644
4663
  const tenantId = resolveRtsRequestTenantId(req);
4645
4664
  if (!tenantId) return null;
4646
4665
  if (hasSessionUser(req) && !isRtsRequestAuthorized(req, tenantId)) return null;
4647
4666
  const { ability, userId } = await buildRtsAbilityFromRequest(req, tenantId);
4648
4667
  const queryEntries = [];
4668
+ const countEntries = [];
4649
4669
  for (const registration of registrations.values()) {
4650
4670
  const options = normalizeRtsQueryOptions(registration.options);
4651
4671
  try {
@@ -4668,12 +4688,28 @@ var createRtsSsrCollector = (req, opts) => {
4668
4688
  continue;
4669
4689
  }
4670
4690
  }
4671
- if (!queryEntries.length) return null;
4691
+ for (const registration of countRegistrations.values()) try {
4692
+ const count = await runRtsCount({
4693
+ tenantId,
4694
+ ability,
4695
+ modelName: registration.modelName,
4696
+ query: registration.query
4697
+ });
4698
+ countEntries.push({
4699
+ modelName: registration.modelName,
4700
+ queryKey: registration.queryKey,
4701
+ count
4702
+ });
4703
+ } catch {
4704
+ continue;
4705
+ }
4706
+ if (!queryEntries.length && !countEntries.length) return null;
4672
4707
  const payload = {
4673
4708
  v: 1,
4674
4709
  tenantId,
4675
4710
  uid: userId,
4676
- queries: []
4711
+ queries: [],
4712
+ counts: []
4677
4713
  };
4678
4714
  for (const entry of queryEntries) {
4679
4715
  payload.queries.push(entry);
@@ -4682,19 +4718,28 @@ var createRtsSsrCollector = (req, opts) => {
4682
4718
  break;
4683
4719
  }
4684
4720
  }
4721
+ for (const entry of countEntries) {
4722
+ payload.counts.push(entry);
4723
+ if (Buffer.byteLength(JSON.stringify(payload), "utf8") > maxSerializedBytes) {
4724
+ payload.counts.pop();
4725
+ break;
4726
+ }
4727
+ }
4685
4728
  const serializedPayload = toSerializedHydrationPayload(payload);
4686
- if (!serializedPayload?.queries.length) return null;
4729
+ if (!serializedPayload || !serializedPayload.queries.length && !serializedPayload.counts.length) return null;
4687
4730
  resolved.clear();
4688
4731
  resolvedPageInfo.clear();
4689
4732
  resolvedTotalCount.clear();
4733
+ resolvedCounts.clear();
4690
4734
  for (const entry of serializedPayload.queries) {
4691
4735
  resolved.set(makeRegistrationKey(entry.modelName, entry.queryKey), entry.data);
4692
4736
  resolvedPageInfo.set(makeRegistrationKey(entry.modelName, entry.queryKey), entry.pageInfo);
4693
4737
  resolvedTotalCount.set(makeRegistrationKey(entry.modelName, entry.queryKey), entry.totalCount);
4694
4738
  }
4739
+ for (const entry of serializedPayload.counts) resolvedCounts.set(makeRegistrationKey(entry.modelName, entry.queryKey), entry.count);
4695
4740
  return serializedPayload;
4696
4741
  };
4697
- const hasRegistrations = () => registrations.size > 0;
4742
+ const hasRegistrations = () => registrations.size > 0 || countRegistrations.size > 0;
4698
4743
  return {
4699
4744
  runtime,
4700
4745
  hasRegistrations,