@pylonsync/functions 0.22.32 → 0.22.34

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/runtime.d.ts CHANGED
@@ -14,7 +14,7 @@
14
14
  * each ctx.db / ctx.scheduler / ctx.runMutation call), so the map never
15
15
  * needs to queue multiple RPCs per call_id.
16
16
  */
17
- import type { DbReader, DbWriter, Llm, Rooms, Workflows, Audit } from "./types";
17
+ import type { DbReader, DbWriter, Llm, Rooms, Workflows, Audit, ApiKeys } from "./types";
18
18
  export declare function buildDbReader(callId: string, ssrRead?: boolean): DbReader;
19
19
  export declare function buildSsrFnCaller(callId: string): (name: string, args?: Record<string, unknown>) => Promise<unknown>;
20
20
  export declare function buildDbWriter(callId: string): DbWriter;
@@ -48,4 +48,6 @@ export declare function buildRooms(callId: string): Rooms;
48
48
  */
49
49
  /** Build `ctx.audit` — round-trips `audit_op` frames to the host. */
50
50
  export declare function buildAudit(callId: string): Audit;
51
+ /** Build `ctx.apiKeys` — round-trips `api_key_op` frames to the host. */
52
+ export declare function buildApiKeys(callId: string): ApiKeys;
51
53
  export declare function buildWorkflows(callId: string): Workflows;
package/dist/types.d.ts CHANGED
@@ -628,6 +628,46 @@ export interface AuditLog {
628
628
  id: string;
629
629
  }>;
630
630
  }
631
+ /**
632
+ * `ctx.apiKeys`: API keys for the app's own users, minted from server
633
+ * code. For a service user the app runs (for example one per AI agent),
634
+ * where the HTTP route `POST /api/auth/api-keys` does not fit because it
635
+ * mints a key for the signed-in caller only.
636
+ *
637
+ * Actions only. The action must be `internal: true` or have called
638
+ * `ctx.auth.elevate({ admin: true, reason })` after checking its own
639
+ * caller; anything else is refused with `API_KEYS_FORBIDDEN`. Not in
640
+ * mutations: a key is not part of the mutation's transaction.
641
+ * Every call is in the server log with the function name and user id.
642
+ */
643
+ export interface ApiKeys {
644
+ /**
645
+ * Mint a key for `userId`. The plaintext `key` is returned once and is
646
+ * never readable again; store it where the key's holder can use it.
647
+ * `expiresAt` is unix seconds. `name` is a label (at most 100
648
+ * characters).
649
+ */
650
+ create(options: {
651
+ userId: string;
652
+ name?: string;
653
+ expiresAt?: number;
654
+ }): Promise<{
655
+ key: string;
656
+ id: string;
657
+ prefix: string;
658
+ expiresAt: number | null;
659
+ }>;
660
+ /**
661
+ * Revoke key `keyId` of `userId`. `revoked` is false when no key of
662
+ * that user has that id.
663
+ */
664
+ revoke(options: {
665
+ userId: string;
666
+ keyId: string;
667
+ }): Promise<{
668
+ revoked: boolean;
669
+ }>;
670
+ }
631
671
  /**
632
672
  * `ctx.audit` on actions: {@link AuditLog.log} plus reads. Mutations get
633
673
  * only `log`: in a Postgres mutation the transaction holds a connection,
@@ -1371,6 +1411,8 @@ export interface ActionCtx<R extends AuthRequirement = "optional"> {
1371
1411
  workflows: Workflows;
1372
1412
  /** Application audit log — see {@link Audit}. */
1373
1413
  audit: Audit;
1414
+ /** API keys for the app's own users, from server code — see {@link ApiKeys}. */
1415
+ apiKeys: ApiKeys;
1374
1416
  /** Run an `agent()` as a named user from server code — see {@link Agents}. */
1375
1417
  agents: Agents;
1376
1418
  /** Attach / register custom domains for your app's OWN end-customers
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pylonsync/functions",
3
- "version": "0.22.32",
3
+ "version": "0.22.34",
4
4
  "description": "TypeScript function runtime for pylon — defines server-side queries, mutations, and actions.",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
package/src/runtime.ts CHANGED
@@ -39,6 +39,7 @@ import type {
39
39
  WorkflowRunSummary,
40
40
  Audit,
41
41
  AuditEntry,
42
+ ApiKeys,
42
43
  Connections,
43
44
  Domains,
44
45
  TenantDomainResult,
@@ -1102,6 +1103,37 @@ export function buildAudit(callId: string): Audit {
1102
1103
  };
1103
1104
  }
1104
1105
 
1106
+ /** Build `ctx.apiKeys` — round-trips `api_key_op` frames to the host. */
1107
+ export function buildApiKeys(callId: string): ApiKeys {
1108
+ return {
1109
+ async create(options) {
1110
+ // A value Rust cannot read as u64 would be dropped by the host and
1111
+ // leave the call waiting; NaN would turn into "never expires".
1112
+ const exp = options.expiresAt;
1113
+ if (exp !== undefined && !(Number.isSafeInteger(exp) && exp >= 0)) {
1114
+ const err = new Error("ctx.apiKeys.create expiresAt must be a whole number of unix seconds, 0 or more");
1115
+ (err as { code?: string }).code = "INVALID_ARGS";
1116
+ throw err;
1117
+ }
1118
+ return (await rpc(callId, {
1119
+ type: "api_key_op",
1120
+ op: "create",
1121
+ user_id: options.userId,
1122
+ name: options.name ?? null,
1123
+ expires_at: options.expiresAt ?? null,
1124
+ })) as { key: string; id: string; prefix: string; expiresAt: number | null };
1125
+ },
1126
+ async revoke(options) {
1127
+ return (await rpc(callId, {
1128
+ type: "api_key_op",
1129
+ op: "revoke",
1130
+ user_id: options.userId,
1131
+ key_id: options.keyId,
1132
+ })) as { revoked: boolean };
1133
+ },
1134
+ };
1135
+ }
1136
+
1105
1137
  export function buildWorkflows(callId: string): Workflows {
1106
1138
  return {
1107
1139
  async start(name: string, input?: unknown, opts?: { key?: string }) {
@@ -1356,6 +1388,7 @@ function buildActionCtx(
1356
1388
  connections,
1357
1389
  workflows: buildWorkflows(callId),
1358
1390
  audit: buildAudit(callId),
1391
+ apiKeys: buildApiKeys(callId),
1359
1392
  agents: {
1360
1393
  async run(name, options) {
1361
1394
  if (!options || typeof options.as?.userId !== "string") {
@@ -804,3 +804,14 @@ describe("nearestBoundaryComponent across route groups", () => {
804
804
  ).toBe("app/(marketing)/not-found");
805
805
  });
806
806
  });
807
+
808
+ describe("client router popstate", () => {
809
+ test("a hash-only popstate does not re-render the route", () => {
810
+ // Clicking a same-page anchor fires popstate. Treating it as a navigation
811
+ // re-rendered the page and scrolled back to the top.
812
+ const src = fs.readFileSync(path.join(import.meta.dir, "ssr-client-bundler.ts"), "utf8");
813
+ const popstate = src.slice(src.indexOf('window.addEventListener("popstate"'));
814
+ expect(popstate).toContain("if (next === renderedPath) return;");
815
+ expect(src.match(/renderedPath = location\.pathname \+ location\.search;/g)?.length).toBeGreaterThanOrEqual(3);
816
+ });
817
+ });
@@ -830,6 +830,12 @@ function renderPendingPlaceholder(pathname, manifest, myEpoch) {
830
830
  return true;
831
831
  }
832
832
 
833
+ // The path and query the page last rendered. A popstate that keeps them and
834
+ // only changes the hash is a same-page jump (an anchor link, a back press
835
+ // between anchors), not a navigation: re-rendering for it would reset the
836
+ // scroll to the top.
837
+ let renderedPath = "";
838
+
833
839
  async function navigate(href, opts) {
834
840
  const push = !opts || opts.push !== false;
835
841
  const url = new URL(href, location.href);
@@ -891,6 +897,7 @@ async function navigate(href, opts) {
891
897
  history.pushState({ component: matched.component }, "", target);
892
898
  }
893
899
  window.dispatchEvent(new Event("pylon:navigation"));
900
+ renderedPath = location.pathname + location.search;
894
901
  window.scrollTo(0, 0);
895
902
  didOptimistic = true;
896
903
  }
@@ -915,6 +922,7 @@ async function navigate(href, opts) {
915
922
  loadManifest().then((manifest) => {
916
923
  if (navState.settled || myEpoch !== navEpoch) return;
917
924
  if (renderPendingPlaceholder(url.pathname, manifest, myEpoch)) {
925
+ renderedPath = location.pathname + location.search;
918
926
  window.scrollTo(0, 0);
919
927
  }
920
928
  });
@@ -1013,6 +1021,7 @@ async function navigate(href, opts) {
1013
1021
  // re-read location after a Link click or a router.push(). popstate already
1014
1022
  // covers back/forward, but pushState/replaceState fire no event.
1015
1023
  window.dispatchEvent(new Event("pylon:navigation"));
1024
+ renderedPath = location.pathname + location.search;
1016
1025
  // After a successful nav, scroll to top (Next.js default).
1017
1026
  window.scrollTo(0, 0);
1018
1027
  }
@@ -1042,8 +1051,11 @@ function installNavHandlers() {
1042
1051
  const seed = seeds ? seeds.get(link) : undefined;
1043
1052
  navigate(href, seed != null ? { seed } : undefined);
1044
1053
  });
1054
+ renderedPath = location.pathname + location.search;
1045
1055
  window.addEventListener("popstate", () => {
1046
- navigate(location.pathname + location.search, { push: false });
1056
+ const next = location.pathname + location.search;
1057
+ if (next === renderedPath) return;
1058
+ navigate(next, { push: false });
1047
1059
  });
1048
1060
  // Progressive-enhancement form submit (#276): intercept <form data-pylon-form>
1049
1061
  // so the page doesn't full-reload. fetch the same route.ts endpoint, follow
package/src/types.ts CHANGED
@@ -705,6 +705,37 @@ export interface AuditLog {
705
705
  }): Promise<{ id: string }>;
706
706
  }
707
707
 
708
+ /**
709
+ * `ctx.apiKeys`: API keys for the app's own users, minted from server
710
+ * code. For a service user the app runs (for example one per AI agent),
711
+ * where the HTTP route `POST /api/auth/api-keys` does not fit because it
712
+ * mints a key for the signed-in caller only.
713
+ *
714
+ * Actions only. The action must be `internal: true` or have called
715
+ * `ctx.auth.elevate({ admin: true, reason })` after checking its own
716
+ * caller; anything else is refused with `API_KEYS_FORBIDDEN`. Not in
717
+ * mutations: a key is not part of the mutation's transaction.
718
+ * Every call is in the server log with the function name and user id.
719
+ */
720
+ export interface ApiKeys {
721
+ /**
722
+ * Mint a key for `userId`. The plaintext `key` is returned once and is
723
+ * never readable again; store it where the key's holder can use it.
724
+ * `expiresAt` is unix seconds. `name` is a label (at most 100
725
+ * characters).
726
+ */
727
+ create(options: {
728
+ userId: string;
729
+ name?: string;
730
+ expiresAt?: number;
731
+ }): Promise<{ key: string; id: string; prefix: string; expiresAt: number | null }>;
732
+ /**
733
+ * Revoke key `keyId` of `userId`. `revoked` is false when no key of
734
+ * that user has that id.
735
+ */
736
+ revoke(options: { userId: string; keyId: string }): Promise<{ revoked: boolean }>;
737
+ }
738
+
708
739
  /**
709
740
  * `ctx.audit` on actions: {@link AuditLog.log} plus reads. Mutations get
710
741
  * only `log`: in a Postgres mutation the transaction holds a connection,
@@ -1500,6 +1531,8 @@ export interface ActionCtx<R extends AuthRequirement = "optional"> {
1500
1531
  workflows: Workflows;
1501
1532
  /** Application audit log — see {@link Audit}. */
1502
1533
  audit: Audit;
1534
+ /** API keys for the app's own users, from server code — see {@link ApiKeys}. */
1535
+ apiKeys: ApiKeys;
1503
1536
  /** Run an `agent()` as a named user from server code — see {@link Agents}. */
1504
1537
  agents: Agents;
1505
1538
  /** Attach / register custom domains for your app's OWN end-customers