@samebase/alchemy-convex 0.34.0 → 0.36.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/README.md CHANGED
@@ -168,14 +168,15 @@ values explicitly: `Convex.providers(Convex.fromValues({ accessToken, deployKey
168
168
 
169
169
  `state: Convex.state()` keeps the Alchemy state of every stack and stage of a Convex team in one
170
170
  project of that team, `alchemy-state-store`, the same name as the Cloudflare state store of Alchemy,
171
- so a repository holds one secret and a stack can create and destroy its own project.
171
+ so a repository holds one secret and a stack can create and destroy its own project. The store holds
172
+ Alchemy's state interface and nothing else, so `Cloudflare.state()` can take its place in a stack.
172
173
  [docs/design/convex-state.md](docs/design/convex-state.md) records the decisions and the
173
174
  measurements.
174
175
 
175
176
  ```ts
176
177
  export default Alchemy.Stack(
177
178
  process.env["GITHUB_REPOSITORY"] ?? "my-org/my-app", // the stack name: the repository
178
- { providers: Convex.providers(), state: Convex.state() },
179
+ { providers: Convex.providers(), state: Convex.withRunRecord(Convex.state()) },
179
180
  Effect.gen(function* () {
180
181
  /* ... */
181
182
  }),
@@ -234,10 +235,12 @@ To rotate the key, from a directory that selects the project `alchemy-state-stor
234
235
 
235
236
  ### What the output records about a run
236
237
 
237
- Alchemy state holds no time, commit, branch, or run. `Convex.state()` adds them to the stage output:
238
- the document that `setOutput` stores is the stack's output plus the key `samebase.run`. On GitHub
239
- Actions the record comes from the run's variables and the event payload; on a development machine
240
- from git, which needs a branch and an `origin` remote. It holds no secret.
238
+ Alchemy state holds no time, commit, branch, or run. `Convex.withRunRecord(state)` adds them to the
239
+ stage output of any state layer, `Convex.state()` or `Cloudflare.state()`: the document that
240
+ `setOutput` stores is the stack's output plus the key `samebase.run`. Samebase shows a stage only
241
+ when its output has this record. On GitHub Actions the record comes from the run's variables and the
242
+ event payload; on a development machine from git, which needs a branch and an `origin` remote. It
243
+ holds no secret.
241
244
 
242
245
  ```json
243
246
  {
@@ -261,9 +264,9 @@ from git, which needs a branch and an `origin` remote. It holds no secret.
261
264
  `source` is `checkout`. A value that the stack itself returns under `samebase.run` stays. A checkout
262
265
  that gives no record, such as a tarball without git or a detached HEAD, writes its outputs without
263
266
  one, and the log says why. Alchemy writes the output once after a full successful deploy, so a
264
- failed deploy leaves the previous record, and a deploy of selected resources writes none. A reader
265
- gets every stage of a stack, with its resources and output, from the store function
266
- `state:readStack({ stack })` in one call.
267
+ failed deploy leaves the previous record, a deploy of selected resources writes none, and a stage
268
+ has no record until its first deploy is complete. A reader gets every stage of a stack, with its
269
+ resources and output, from the store function `state:readStack({ stack })` in one call.
267
270
 
268
271
  ## Resources
269
272
 
@@ -331,14 +334,15 @@ gets every stage of a stack, with its resources and output, from the store funct
331
334
  - Each deploy reads the saved client on the hub with its own token. A client that is gone gets a
332
335
  new one, and a client on another `redirectUri`, of another `deploymentType`, or with other
333
336
  `connectors` too. A replaced production client stays valid on the hub until its administrator
334
- deletes it.
337
+ deletes it. A saved row whose attributes this version cannot read stops the run with a fixed
338
+ message, which holds no value of the row.
335
339
  - A new client logs the link to the terms of the shared sign-in service. A refusal logs its
336
340
  reason, such as a host that does not serve the file, a block, a token of another app, a
337
341
  connector the hub does not offer or allow, or the hourly limit it reached.
338
- - No failure stops a deploy or a destroy. A failed renewal keeps the saved values. Without saved
339
- values, `status` is `off`, and `issuer`, `clientId`, and `clientSecret` are the `no-hub` values
340
- of a checkout without a hub. A production or preview run without a GitHub OIDC token, such as on
341
- a development machine, does the same, and logs no warning.
342
+ - No failure of the hub stops a deploy or a destroy. A failed renewal keeps the saved values.
343
+ Without saved values, `status` is `off`, and `issuer`, `clientId`, and `clientSecret` are the
344
+ `no-hub` values of a checkout without a hub. A production or preview run without a GitHub OIDC
345
+ token, such as on a development machine, does the same, and logs no warning.
342
346
  - `required: true` makes the run fail unless the hub confirms the client in this run: a refusal, a
343
347
  hub that does not answer a registration or a renewal, or a suspended client fails it. Set it
344
348
  where sign-in is the product, so a deploy never ships guest sign-in, or a client that the hub
@@ -1,4 +1,4 @@
1
- import { n as newKeyId, t as newKey } from "./sealedSecret-Bf8AeIp_.js";
1
+ import { n as newKeyId, t as newKey } from "./sealedSecret-B7HlvIg2.js";
2
2
  import { createRequire } from "node:module";
3
3
  import { Stack } from "alchemy/Stack";
4
4
  import * as Effect from "effect/Effect";
@@ -1536,10 +1536,49 @@ const isPlainObject = (value) => {
1536
1536
  * key: a string, an array, null, or an object that is not plain, such as a
1537
1537
  * Date. A value that the stack itself put under the key stays.
1538
1538
  */
1539
- const withRunRecord = (output, record) => isPlainObject(output) ? "samebase.run" in output ? output : {
1539
+ const outputWithRunRecord = (output, record) => isPlainObject(output) ? "samebase.run" in output ? output : {
1540
1540
  ...output,
1541
1541
  [RUN_RECORD_KEY]: record
1542
1542
  } : void 0;
1543
+ /**
1544
+ * The state of `inner`, with the record of this run in each stage output that it writes, under
1545
+ * {@link RUN_RECORD_KEY}: `state: withRunRecord(Convex.state())`, or the same over another state
1546
+ * layer, such as the Cloudflare state store of Alchemy. Every other call goes to `inner` as it is.
1547
+ *
1548
+ * The record is derived once, at the first use of the state, and `recordedAt` is the time of the
1549
+ * output write. A checkout that gives no record, such as a tarball without git, a checkout without
1550
+ * `origin`, or a detached HEAD, still reads, plans, and destroys: its outputs are written without
1551
+ * the record, and the log says why. The engine writes the output only after a full successful
1552
+ * apply, so the record names the last full successful deploy of the stage.
1553
+ */
1554
+ const withRunRecord = (inner) => Layer.effect(State, Effect.gen(function* () {
1555
+ const open = yield* State;
1556
+ return yield* Effect.cached(Effect.gen(function* () {
1557
+ const service = yield* open;
1558
+ const run = yield* runRecord({
1559
+ cwd: process.cwd(),
1560
+ env: process.env
1561
+ }).pipe(Effect.catch((error) => Effect.logWarning(`The stage outputs of this run carry no run record. ${error.message}`).pipe(Effect.as(void 0))));
1562
+ const documentOf = (request) => Effect.gen(function* () {
1563
+ if (run === void 0) return request.value;
1564
+ const document = outputWithRunRecord(request.value, {
1565
+ ...run,
1566
+ recordedAt: yield* now
1567
+ });
1568
+ if (document !== void 0) return document;
1569
+ yield* Effect.logWarning(`The output of ${request.stack}/${request.stage} is not a plain object, so it carries no run record.`);
1570
+ return request.value;
1571
+ });
1572
+ return {
1573
+ ...service,
1574
+ setOutput: (request) => documentOf(request).pipe(Effect.flatMap((value) => service.setOutput({
1575
+ stack: request.stack,
1576
+ stage: request.stage,
1577
+ value
1578
+ })))
1579
+ };
1580
+ }));
1581
+ })).pipe(Layer.provide(inner));
1543
1582
  //#endregion
1544
1583
  //#region src/store/convex/sealing.ts
1545
1584
  /** The variable of the store deployment that holds the key `keyId`. */
@@ -1605,18 +1644,8 @@ const makeFunctionApi = (url, key) => ({ call: (path, args) => post(`${url}/api/
1605
1644
  /** The value of a reply, or the Convex message, which names the function and the cause, such as a missing key action. */
1606
1645
  const valueOf = (reply) => reply.status === "success" ? Effect.succeed(reply.value) : Effect.fail(failure(reply.errorMessage.trim()));
1607
1646
  /** The functions of the store with Alchemy's encoding: the JSON text of encodeState. */
1608
- const remoteOf = (api, record) => {
1647
+ const remoteOf = (api) => {
1609
1648
  const run = (name, args) => api.call(`state:${name}`, args).pipe(Effect.flatMap(valueOf));
1610
- const write = (name, args) => Effect.gen(function* () {
1611
- const currentRun = record === void 0 ? void 0 : {
1612
- ...record,
1613
- recordedAt: new Date(yield* Clock.currentTimeMillis).toISOString()
1614
- };
1615
- return yield* run(name, {
1616
- ...args,
1617
- run: currentRun
1618
- });
1619
- });
1620
1649
  const strings = (name, args) => run(name, args).pipe(Effect.flatMap(decode(Schema.Array(Schema.String), name)));
1621
1650
  const optional = (name, args) => run(name, args).pipe(Effect.flatMap(decode(Schema.NullOr(Schema.String), name)));
1622
1651
  const encode = (value) => JSON.stringify(encodeState(value));
@@ -1640,13 +1669,13 @@ const remoteOf = (api, record) => {
1640
1669
  stack: r.stack,
1641
1670
  stage: r.stage
1642
1671
  }).pipe(Effect.map((text) => text === null ? void 0 : JSON.parse(text, reviveState))),
1643
- set: (r) => write("set", {
1672
+ set: (r) => run("set", {
1644
1673
  stack: r.stack,
1645
1674
  stage: r.stage,
1646
1675
  fqn: r.fqn,
1647
1676
  value: encode(r.value)
1648
1677
  }),
1649
- delete: (r) => write("remove", {
1678
+ delete: (r) => run("remove", {
1650
1679
  stack: r.stack,
1651
1680
  stage: r.stage,
1652
1681
  fqn: r.fqn
@@ -1655,7 +1684,7 @@ const remoteOf = (api, record) => {
1655
1684
  stack: r.stack,
1656
1685
  stage: r.stage
1657
1686
  }),
1658
- setOutput: (r) => write("setOutput", {
1687
+ setOutput: (r) => run("setOutput", {
1659
1688
  stack: r.stack,
1660
1689
  stage: r.stage,
1661
1690
  value: encode(r.value)
@@ -1679,7 +1708,7 @@ const storeDirectory = () => join(dirname(fileURLToPath(import.meta.url)), "..",
1679
1708
  */
1680
1709
  const pushStore = (options, deployment) => Effect.gen(function* () {
1681
1710
  const cli = yield* installedCli(join(options.store, ".."));
1682
- yield* Effect.logInfo(`Convex state: pushing the store version 6 to ${deployment.name}`);
1711
+ yield* Effect.logInfo(`Convex state: pushing the store version 7 to ${deployment.name}`);
1683
1712
  yield* withPushKey(options.api, deployment.name, PUSH_KEY_ACTIONS, (key) => push(options.spawner, key, {
1684
1713
  deployment: deployment.name,
1685
1714
  url: deployment.deploymentUrl,
@@ -1762,32 +1791,17 @@ const makeConvexState = (options) => Effect.gen(function* () {
1762
1791
  return next.call(path, args);
1763
1792
  })))) };
1764
1793
  let version = yield* storeVersion(functions);
1765
- if (version === void 0 || version < 6) {
1794
+ if (version === void 0 || version < 7) {
1766
1795
  if (version === void 0 && deployment.lastDeployTime) return yield* failure(`project ${STORE_PROJECT_NAME} (${project.id}) of team ${teamId} has code that is not the state store, and Convex.state does not push over it. Rename that project in the Convex dashboard.`);
1767
1796
  version = yield* storeVersion(functions);
1768
- if (version === void 0 || version < 6) {
1797
+ if (version === void 0 || version < 7) {
1769
1798
  yield* pushStore(options, deployment);
1770
1799
  version = yield* storeVersion(functions);
1771
1800
  }
1772
1801
  }
1773
- if (version !== 6) return yield* failure(`${deployment.name} answers store version ${version ?? "none"}, and this package needs store version 6.`);
1802
+ if (version !== 7) return yield* failure(`${deployment.name} answers store version ${version ?? "none"}, and this package needs store version 7.`);
1774
1803
  yield* ensureSealed(options, deployment, functions);
1775
- const remote = remoteOf(functions, options.run);
1776
- /** The stack's output plus the record, when the run has one and the output has a place for it. */
1777
- const documentOf = (request) => Effect.gen(function* () {
1778
- const { run } = options;
1779
- if (run === void 0) return request.value;
1780
- const recordedAt = new Date(yield* Clock.currentTimeMillis).toISOString();
1781
- const document = withRunRecord(request.value, {
1782
- ...run,
1783
- recordedAt
1784
- });
1785
- if (document === void 0) {
1786
- yield* Effect.logWarning(`Convex state: the output of ${request.stack}/${request.stage} is not a plain object, so it carries no run record.`);
1787
- return request.value;
1788
- }
1789
- return document;
1790
- });
1804
+ const remote = remoteOf(functions);
1791
1805
  return {
1792
1806
  id: "convex",
1793
1807
  getVersion: () => Effect.succeed(STATE_STORE_VERSION),
@@ -1800,15 +1814,7 @@ const makeConvexState = (options) => Effect.gen(function* () {
1800
1814
  set: (request) => remote.set(request).pipe(Effect.as(request.value)),
1801
1815
  delete: (request) => remote.delete(request).pipe(Effect.asVoid),
1802
1816
  deleteStack: (request) => remote.deleteStack(request).pipe(Effect.asVoid),
1803
- setOutput: (request) => Effect.gen(function* () {
1804
- const value = yield* documentOf(request);
1805
- yield* remote.setOutput({
1806
- stack: request.stack,
1807
- stage: request.stage,
1808
- value
1809
- });
1810
- return value;
1811
- })
1817
+ setOutput: (request) => remote.setOutput(request).pipe(Effect.as(request.value))
1812
1818
  };
1813
1819
  });
1814
1820
  /**
@@ -1824,20 +1830,15 @@ const state = () => Layer.effect(State, Effect.gen(function* () {
1824
1830
  const context = yield* Effect.context();
1825
1831
  return yield* Effect.cached(Effect.gen(function* () {
1826
1832
  const api = yield* ManagementApi.pipe(Effect.provide(ManagementApiLive().pipe(Layer.provide(fromConfig()))));
1827
- const run = yield* runRecord({
1828
- cwd: process.cwd(),
1829
- env: process.env
1830
- }).pipe(Effect.catch((error) => Effect.logWarning(`Convex state: the stage outputs of this run carry no record. ${error.message}`).pipe(Effect.as(void 0))));
1831
1833
  return yield* makeConvexState({
1832
1834
  api,
1833
1835
  spawner,
1834
1836
  scope,
1835
- store: storeDirectory(),
1836
- run
1837
+ store: storeDirectory()
1837
1838
  });
1838
1839
  }).pipe(Effect.provideContext(context), Effect.orDie));
1839
1840
  }));
1840
1841
  //#endregion
1841
1842
  export { ManagementApi as A, ConvexCliNotFound as C, parseDeployOutput as D, VariablesOwnedBySomeoneElse as E, CredentialsError as F, DEPLOY_KEY_ENV as I, fromConfig as L, readLocalDeployment as M, ACCESS_TOKEN_ENV as N, push as O, Credentials as P, fromValues as R, ConvexAppNotFound as S, PushTargetMismatch as T, DeploymentIdentityChange as _, runRecord as a, ProductionDeploymentNotFound as b, CreatedProjectNotFound as c, ProjectNotFound as d, ProjectProvider as f, Deployment as g, AmbiguousDeployment as h, RunRecordError as i, ManagementApiLive as j, ConvexApiError as k, Project as l, AbsoluteRootDir as m, state as n, withRunRecord as o, ProjectTeamChange as p, RUN_RECORD_KEY as r, AmbiguousProject as s, STORE_PROJECT_NAME as t, ProjectDeploymentChange as u, DeploymentProvider as v, EnvironmentNeedsDeployKey as w, CodePushFailed as x, DeploymentResource as y };
1842
1843
 
1843
- //# sourceMappingURL=State-CuktZaOh.js.map
1844
+ //# sourceMappingURL=State-DVC61yrQ.js.map