alchemy 2.0.0-beta.45 → 2.0.0-beta.46
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/lib/AlchemyContext.d.ts +0 -1
- package/lib/AlchemyContext.d.ts.map +1 -1
- package/lib/AlchemyContext.js.map +1 -1
- package/lib/Cli/commands/cloudflare.d.ts.map +1 -1
- package/lib/Cli/commands/cloudflare.js +5 -1
- package/lib/Cli/commands/cloudflare.js.map +1 -1
- package/lib/Cloudflare/Providers.d.ts.map +1 -1
- package/lib/Cloudflare/Providers.js +5 -1
- package/lib/Cloudflare/Providers.js.map +1 -1
- package/lib/Cloudflare/Queue/QueueConsumer.d.ts.map +1 -1
- package/lib/Cloudflare/Queue/QueueConsumer.js +1 -2
- package/lib/Cloudflare/Queue/QueueConsumer.js.map +1 -1
- package/lib/Cloudflare/StateStore/Api.d.ts +1 -1
- package/lib/Cloudflare/StateStore/Api.js +1 -1
- package/lib/Cloudflare/StateStore/State.d.ts +11 -65
- package/lib/Cloudflare/StateStore/State.d.ts.map +1 -1
- package/lib/Cloudflare/StateStore/State.js +307 -366
- package/lib/Cloudflare/StateStore/State.js.map +1 -1
- package/lib/Cloudflare/StateStore/Store.d.ts.map +1 -1
- package/lib/Cloudflare/StateStore/Store.js +16 -29
- package/lib/Cloudflare/StateStore/Store.js.map +1 -1
- package/lib/Cloudflare/Vectorize/VectorizeIndex.d.ts +114 -0
- package/lib/Cloudflare/Vectorize/VectorizeIndex.d.ts.map +1 -0
- package/lib/Cloudflare/Vectorize/VectorizeIndex.js +157 -0
- package/lib/Cloudflare/Vectorize/VectorizeIndex.js.map +1 -0
- package/lib/Cloudflare/Vectorize/VectorizeIndexBinding.d.ts +38 -0
- package/lib/Cloudflare/Vectorize/VectorizeIndexBinding.d.ts.map +1 -0
- package/lib/Cloudflare/Vectorize/VectorizeIndexBinding.js +44 -0
- package/lib/Cloudflare/Vectorize/VectorizeIndexBinding.js.map +1 -0
- package/lib/Cloudflare/Vectorize/VectorizeMetadataIndex.d.ts +72 -0
- package/lib/Cloudflare/Vectorize/VectorizeMetadataIndex.d.ts.map +1 -0
- package/lib/Cloudflare/Vectorize/VectorizeMetadataIndex.js +131 -0
- package/lib/Cloudflare/Vectorize/VectorizeMetadataIndex.js.map +1 -0
- package/lib/Cloudflare/Vectorize/index.d.ts +4 -0
- package/lib/Cloudflare/Vectorize/index.d.ts.map +1 -0
- package/lib/Cloudflare/Vectorize/index.js +4 -0
- package/lib/Cloudflare/Vectorize/index.js.map +1 -0
- package/lib/Cloudflare/Workers/WorkerAsyncBindings.d.ts.map +1 -1
- package/lib/Cloudflare/Workers/WorkerAsyncBindings.js +8 -0
- package/lib/Cloudflare/Workers/WorkerAsyncBindings.js.map +1 -1
- package/lib/Cloudflare/Workers/WorkerBinding.d.ts +2 -1
- package/lib/Cloudflare/Workers/WorkerBinding.d.ts.map +1 -1
- package/lib/Cloudflare/Workers/WorkerBinding.js.map +1 -1
- package/lib/Cloudflare/index.d.ts +1 -0
- package/lib/Cloudflare/index.d.ts.map +1 -1
- package/lib/Cloudflare/index.js +1 -0
- package/lib/Cloudflare/index.js.map +1 -1
- package/lib/State/HttpStateStore.d.ts +4 -0
- package/lib/State/HttpStateStore.d.ts.map +1 -1
- package/lib/State/HttpStateStore.js +13 -0
- package/lib/State/HttpStateStore.js.map +1 -1
- package/lib/Util/poll.d.ts +30 -0
- package/lib/Util/poll.d.ts.map +1 -0
- package/lib/Util/poll.js +26 -0
- package/lib/Util/poll.js.map +1 -0
- package/lib/tsconfig.test.tsbuildinfo +1 -1
- package/package.json +7 -7
- package/src/AlchemyContext.ts +0 -1
- package/src/Cli/commands/cloudflare.ts +5 -3
- package/src/Cloudflare/Providers.ts +7 -0
- package/src/Cloudflare/Queue/QueueConsumer.ts +0 -2
- package/src/Cloudflare/StateStore/Api.ts +1 -1
- package/src/Cloudflare/StateStore/State.ts +445 -507
- package/src/Cloudflare/StateStore/Store.ts +22 -35
- package/src/Cloudflare/Vectorize/VectorizeIndex.ts +261 -0
- package/src/Cloudflare/Vectorize/VectorizeIndexBinding.ts +103 -0
- package/src/Cloudflare/Vectorize/VectorizeMetadataIndex.ts +208 -0
- package/src/Cloudflare/Vectorize/index.ts +3 -0
- package/src/Cloudflare/Workers/WorkerAsyncBindings.ts +7 -0
- package/src/Cloudflare/Workers/WorkerBinding.ts +2 -0
- package/src/Cloudflare/index.ts +1 -0
- package/src/State/HttpStateStore.ts +28 -0
- package/src/Util/poll.ts +47 -0
|
@@ -8,354 +8,212 @@ import * as HttpClient from "effect/unstable/http/HttpClient";
|
|
|
8
8
|
import * as HttpClientRequest from "effect/unstable/http/HttpClientRequest";
|
|
9
9
|
import * as HttpApiClient from "effect/unstable/httpapi/HttpApiClient";
|
|
10
10
|
import crypto from "node:crypto";
|
|
11
|
-
import * as Output from "../../Output.js";
|
|
12
11
|
import * as Config from "effect/Config";
|
|
12
|
+
import { isHttpClientError } from "effect/unstable/http/HttpClientError";
|
|
13
13
|
import { adopt } from "../../AdoptPolicy.js";
|
|
14
14
|
import { AuthError } from "../../Auth/AuthProvider.js";
|
|
15
15
|
import { CredentialsStore, CredentialsStoreLive, } from "../../Auth/Credentials.js";
|
|
16
16
|
import { ALCHEMY_PROFILE, ProfileLive } from "../../Auth/Profile.js";
|
|
17
17
|
import * as Cloudflare from "../../Cloudflare/Providers.js";
|
|
18
18
|
import { deploy } from "../../Deploy.js";
|
|
19
|
+
import * as Output from "../../Output.js";
|
|
19
20
|
import * as Alchemy from "../../Stack.js";
|
|
20
21
|
import { StateApi } from "../../State/HttpStateApi.js";
|
|
21
|
-
import { makeHttpStateStore, } from "../../State/HttpStateStore.js";
|
|
22
|
-
import * as CloudflareEnvironment from "../CloudflareEnvironment.js";
|
|
23
|
-
import * as Credentials from "../Credentials.js";
|
|
24
|
-
import { AlchemyContext } from "../../AlchemyContext.js";
|
|
22
|
+
import { checkHttpStateStoreAuth, makeHttpStateStore, } from "../../State/HttpStateStore.js";
|
|
25
23
|
import { makeLocalState } from "../../State/LocalState.js";
|
|
26
24
|
import { State } from "../../State/State.js";
|
|
27
25
|
import { recordStateStoreInit, recordStateStoreOp, } from "../../Telemetry/Metrics.js";
|
|
28
26
|
import * as Clank from "../../Util/Clank.js";
|
|
29
27
|
import * as Access from "../Access.js";
|
|
30
28
|
import { CloudflareAuth } from "../Auth/AuthProvider.js";
|
|
29
|
+
import * as CloudflareEnvironment from "../CloudflareEnvironment.js";
|
|
30
|
+
import * as Credentials from "../Credentials.js";
|
|
31
31
|
import { EdgeSessionError, createEdgeSession } from "../EdgeSession.js";
|
|
32
32
|
import Api, { STATE_STORE_SCRIPT_NAME, STATE_STORE_VERSION } from "./Api.js";
|
|
33
33
|
import { AuthToken, AuthTokenSecretName, TokenValue } from "./Token.js";
|
|
34
|
+
const CI = Config.boolean("CI").pipe(Config.withDefault(false));
|
|
34
35
|
/** Filename used for stored credentials under the profile directory. */
|
|
35
36
|
const CREDENTIALS_FILE = "cloudflare-state-store";
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
* count distinct state-store deployments without leaking the raw
|
|
40
|
-
* accountId. Mirrors the `alchemy.git.origin_hash` pattern in
|
|
41
|
-
* `Telemetry/Attributes.ts`.
|
|
42
|
-
*/
|
|
43
|
-
const hashAccountId = (accountId) => Effect.sync(() => crypto.createHash("sha256").update(accountId).digest("hex"));
|
|
44
|
-
/**
|
|
45
|
-
* Best-effort Cloudflare-account-hash annotation on the current span.
|
|
46
|
-
* Resolves the accountId from {@link CloudflareEnvironment} and
|
|
47
|
-
* attaches `alchemy.cloudflare.account_hash` to whichever span is
|
|
48
|
-
* active. Silently no-ops if the environment isn't resolvable so
|
|
49
|
-
* State-store layer construction still succeeds in degraded paths.
|
|
50
|
-
*
|
|
51
|
-
* `noTrack` controls whether the hash is attached:
|
|
52
|
-
* - `true` — never annotate (caller-level opt-out).
|
|
53
|
-
* - `false` — always annotate, regardless of env.
|
|
54
|
-
* - `undefined` — fall back to the `NO_TRACK` env var; default off.
|
|
55
|
-
*/
|
|
56
|
-
const annotateAccountHash = (noTrack) => Effect.gen(function* () {
|
|
57
|
-
if (noTrack === true)
|
|
58
|
-
return;
|
|
59
|
-
if (noTrack === undefined) {
|
|
60
|
-
const fromEnv = yield* Config.boolean("NO_TRACK").pipe(Config.withDefault(false));
|
|
61
|
-
if (fromEnv)
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
const env = yield* Effect.serviceOption(CloudflareEnvironment.CloudflareEnvironment);
|
|
65
|
-
if (env._tag !== "Some")
|
|
66
|
-
return;
|
|
67
|
-
const hash = yield* hashAccountId(env.value.accountId);
|
|
68
|
-
yield* Effect.annotateCurrentSpan("alchemy.cloudflare.account_hash", hash);
|
|
69
|
-
}).pipe(Effect.catch(() => Effect.void));
|
|
70
|
-
/**
|
|
71
|
-
* Manually bootstrap (or repair) the Cloudflare-hosted HTTP State
|
|
72
|
-
* Store. Used by `alchemy bootstrap cloudflare`.
|
|
73
|
-
*
|
|
74
|
-
* The flow mirrors what {@link state} does on first use, but is
|
|
75
|
-
* exposed as a standalone effect so users can trigger it (or recover
|
|
76
|
-
* from a partially-failed previous deploy) explicitly:
|
|
77
|
-
*
|
|
78
|
-
* 1. Resume any in-progress local `CloudflareStateStore` stack.
|
|
79
|
-
* 2. If the worker already exists in Cloudflare and `force` is not
|
|
80
|
-
* set, adopt it: read the auth token via an edge-preview probe
|
|
81
|
-
* and persist credentials.
|
|
82
|
-
* 3. Otherwise run the full deploy with adoption enabled, sync local
|
|
83
|
-
* state into the deployed store, persist credentials, and delete
|
|
84
|
-
* the local stack.
|
|
85
|
-
*/
|
|
86
|
-
export const bootstrap = (options = {}) => Effect.gen(function* () {
|
|
87
|
-
const isCI = yield* Config.boolean("CI").pipe(Config.withDefault(false));
|
|
37
|
+
export const state = () => Layer.effect(State, Effect.gen(function* () {
|
|
38
|
+
const isCI = yield* CI;
|
|
39
|
+
const scriptName = STATE_STORE_SCRIPT_NAME;
|
|
88
40
|
const profileName = yield* ALCHEMY_PROFILE;
|
|
89
|
-
const
|
|
90
|
-
const
|
|
91
|
-
yield*
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
"alchemy.state_store.ci": isCI,
|
|
96
|
-
});
|
|
97
|
-
yield* annotateAccountHash();
|
|
98
|
-
const localState = yield* makeLocalState();
|
|
99
|
-
const hasLocalStack = yield* Effect.map(localState.listStages("CloudflareStateStore"), (stages) => stages.includes(scriptName));
|
|
100
|
-
if (hasLocalStack) {
|
|
101
|
-
yield* Clank.info(`Resuming Cloudflare State Store '${scriptName}' deployment...`);
|
|
102
|
-
yield* finishBootstrap({
|
|
41
|
+
const localStage = `${profileName}_${scriptName}`;
|
|
42
|
+
const credStore = yield* CredentialsStore;
|
|
43
|
+
if (yield* hasLocalStack(localStage)) {
|
|
44
|
+
// if there's still a local stack, then we need to finish the bootstrap
|
|
45
|
+
// TODO(sam): what if the local stack was
|
|
46
|
+
return yield* deployWithLocalState({
|
|
103
47
|
scriptName,
|
|
104
48
|
profileName,
|
|
105
|
-
localState,
|
|
106
49
|
isCI,
|
|
107
|
-
force,
|
|
50
|
+
force: false,
|
|
108
51
|
});
|
|
109
|
-
yield* Clank.success(`Cloudflare State Store '${scriptName}' is ready.`);
|
|
110
|
-
return;
|
|
111
52
|
}
|
|
112
|
-
const {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
53
|
+
const ensureLatest = ({ url, authToken, }) => Effect.gen(function* () {
|
|
54
|
+
const { matches, expected, observed } = yield* checkStateStoreVersion(url);
|
|
55
|
+
if (observed === undefined) {
|
|
56
|
+
const shouldDeploy = yield* Clank.confirm({
|
|
57
|
+
message: `Cloudflare State Store '${scriptName}' is not available. Do you want to deploy it?`,
|
|
58
|
+
});
|
|
59
|
+
if (shouldDeploy) {
|
|
60
|
+
return yield* bootstrap({
|
|
61
|
+
workerName: scriptName,
|
|
62
|
+
profile: profileName,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
return yield* Effect.die(new Clank.PromptCancelled());
|
|
67
|
+
}
|
|
123
68
|
}
|
|
124
|
-
yield*
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
69
|
+
const httpState = yield* ensureAccess({ url, authToken });
|
|
70
|
+
if (matches) {
|
|
71
|
+
return httpState;
|
|
72
|
+
}
|
|
73
|
+
else if (isCI) {
|
|
74
|
+
return yield* Effect.die(new AuthError({
|
|
75
|
+
message: `Cloudflare State store not found. Run 'alchemy bootstrap cloudflare --profile <your-ci-profile>' to deploy it first.`,
|
|
76
|
+
}));
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
const shouldDeploy = yield* Clank.confirm({
|
|
80
|
+
message: `Cloudflare State Store '${scriptName}' is out of date ` +
|
|
81
|
+
`(expected v${expected}, observed v${observed ?? "unknown"})`,
|
|
82
|
+
});
|
|
83
|
+
if (shouldDeploy) {
|
|
84
|
+
const stateStoreOptions = yield* deployStateStore({
|
|
85
|
+
stage: scriptName,
|
|
86
|
+
state: httpState,
|
|
87
|
+
force: false,
|
|
88
|
+
});
|
|
89
|
+
return yield* makeCloudflareStateStore(stateStoreOptions);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
return yield* Effect.die(new Clank.PromptCancelled());
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
const ensureAccess = (credentials) => Effect.gen(function* () {
|
|
97
|
+
const isAuth = yield* checkHttpStateStoreAuth(credentials);
|
|
98
|
+
if (!isAuth) {
|
|
99
|
+
// our token is wrong, force a refresh
|
|
100
|
+
yield* Clank.info(`Cloudflare State store authentication failed, refreshing credentials...`);
|
|
101
|
+
const credentials = yield* loginWithCloudflare(profileName, true);
|
|
102
|
+
if (!(yield* checkHttpStateStoreAuth(credentials))) {
|
|
103
|
+
return yield* Effect.die(new AuthError({
|
|
104
|
+
message: `Cloudflare State store authentication failed, after refreshing credentials.`,
|
|
105
|
+
}));
|
|
106
|
+
}
|
|
107
|
+
return yield* makeCloudflareStateStore(credentials);
|
|
108
|
+
}
|
|
109
|
+
return yield* makeCloudflareStateStore(credentials);
|
|
110
|
+
});
|
|
111
|
+
const credentials = yield* credStore.read(profileName, CREDENTIALS_FILE);
|
|
112
|
+
if (credentials) {
|
|
113
|
+
return yield* ensureLatest(credentials);
|
|
134
114
|
}
|
|
115
|
+
const workerExists = yield* isStateStoreAvailable(scriptName);
|
|
135
116
|
if (workerExists) {
|
|
136
|
-
yield*
|
|
117
|
+
return yield* ensureLatest(yield* loginWithCloudflare(profileName, false));
|
|
118
|
+
}
|
|
119
|
+
else if (isCI) {
|
|
120
|
+
// TODO(sam): do we want to support bootstrapping the state store from CI?
|
|
121
|
+
// for now - just die here
|
|
122
|
+
return yield* Effect.die(new AuthError({
|
|
123
|
+
message: `Cloudflare State store not found. Run 'alchemy bootstrap cloudflare --profile <your-ci-profile>' to deploy it first.`,
|
|
124
|
+
}));
|
|
137
125
|
}
|
|
138
126
|
else {
|
|
139
|
-
yield* Clank.
|
|
127
|
+
return yield* Clank.confirm({
|
|
128
|
+
message: "Cloudflare State Store not found. Do you want to deploy it?",
|
|
129
|
+
}).pipe(Effect.flatMap((shouldDeploy) => shouldDeploy
|
|
130
|
+
? bootstrap()
|
|
131
|
+
: Effect.die(new Clank.PromptCancelled())));
|
|
140
132
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
yield* Clank.success(`Cloudflare State Store '${scriptName}' is ready.`);
|
|
149
|
-
}).pipe(Effect.withSpan("state_store.bootstrap", {
|
|
150
|
-
attributes: {
|
|
151
|
-
"alchemy.state_store.op": "bootstrap",
|
|
152
|
-
"alchemy.state_store.script_name": options.workerName ?? STATE_STORE_SCRIPT_NAME,
|
|
153
|
-
},
|
|
154
|
-
}));
|
|
155
|
-
export const state = () => Layer.effect(State, Effect.gen(function* () {
|
|
156
|
-
const isCI = yield* Config.boolean("CI").pipe(Config.withDefault(false));
|
|
157
|
-
const alchemyContext = yield* AlchemyContext;
|
|
158
|
-
const profileName = yield* ALCHEMY_PROFILE;
|
|
159
|
-
const store = yield* CredentialsStore;
|
|
160
|
-
const credentials = yield* store.read(profileName, CREDENTIALS_FILE);
|
|
161
|
-
const scriptName = STATE_STORE_SCRIPT_NAME;
|
|
133
|
+
}).pipe(recordStateStoreInit, Effect.orDie)).pipe(Layer.provideMerge(Credentials.fromAuthProvider()), Layer.provideMerge(CloudflareEnvironment.fromProfile()), Layer.provideMerge(CloudflareAuth), Layer.provideMerge(Access.AccessLive), Layer.provideMerge(ProfileLive), Layer.provideMerge(CredentialsStoreLive), Layer.orDie);
|
|
134
|
+
export const bootstrap = (options = {}) => Effect.gen(function* () {
|
|
135
|
+
const isCI = yield* CI;
|
|
136
|
+
const profileName = options.profile ?? (yield* ALCHEMY_PROFILE);
|
|
137
|
+
const scriptName = options.workerName ?? STATE_STORE_SCRIPT_NAME;
|
|
138
|
+
const force = options.force ?? false;
|
|
139
|
+
const localStage = `${profileName}_${scriptName}`;
|
|
162
140
|
yield* Effect.annotateCurrentSpan({
|
|
163
141
|
"alchemy.state_store.script_name": scriptName,
|
|
164
142
|
"alchemy.state_store.profile": profileName,
|
|
143
|
+
"alchemy.state_store.force": force,
|
|
165
144
|
"alchemy.state_store.ci": isCI,
|
|
166
|
-
"alchemy.state_store.force": false,
|
|
167
145
|
});
|
|
168
|
-
yield* annotateAccountHash(
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
// 1. the worker has been deployed and the credentials persisted
|
|
172
|
-
// 2. the local CloudflareStateStore stack has been synced into
|
|
173
|
-
// the HTTP store and removed from disk
|
|
174
|
-
//
|
|
175
|
-
// If a previous run failed anywhere between deploying the worker
|
|
176
|
-
// and deleting the local stack (e.g. the worker got created but
|
|
177
|
-
// sync/credential-write/delete never ran) we will detect the
|
|
178
|
-
// leftover local stack here and resume the bootstrap. This loop
|
|
179
|
-
// re-runs deploy (idempotent over already-applied resources) and
|
|
180
|
-
// finishes the sync/credential/cleanup steps until both
|
|
181
|
-
// invariants are satisfied.
|
|
182
|
-
const localState = yield* makeLocalState();
|
|
183
|
-
const hasLocalStack = yield* Effect.map(localState.listStages("CloudflareStateStore"), (stages) => stages.includes(scriptName));
|
|
184
|
-
if (hasLocalStack) {
|
|
146
|
+
yield* annotateAccountHash();
|
|
147
|
+
if (yield* hasLocalStack(localStage)) {
|
|
148
|
+
// if there's a local stack still, we can assume we did not finish hoisting it, so finish that
|
|
185
149
|
yield* Clank.info(`Resuming Cloudflare State Store '${scriptName}' deployment...`);
|
|
186
|
-
|
|
150
|
+
// resume deployment
|
|
151
|
+
return yield* deployWithLocalState({
|
|
187
152
|
scriptName,
|
|
188
153
|
profileName,
|
|
189
|
-
localState,
|
|
190
154
|
isCI,
|
|
191
|
-
force
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
if (credentials) {
|
|
195
|
-
// We have local credentials. Before trusting them, verify
|
|
196
|
-
// the deployed worker is on the version this CLI was built
|
|
197
|
-
// against. If it isn't, `redeployIfStale` runs the idempotent
|
|
198
|
-
// bootstrap flow and short-circuits with the freshly-deployed
|
|
199
|
-
// store.
|
|
200
|
-
const fresh = yield* redeployIfStale({
|
|
201
|
-
scriptName,
|
|
202
|
-
profileName,
|
|
203
|
-
localState,
|
|
204
|
-
isCI,
|
|
205
|
-
url: credentials.url,
|
|
206
|
-
force: false,
|
|
207
|
-
});
|
|
208
|
-
if (fresh)
|
|
209
|
-
return fresh;
|
|
210
|
-
const httpState = yield* makeCloudflareStateStore(credentials);
|
|
211
|
-
if (alchemyContext.updateStateStore) {
|
|
212
|
-
yield* deployStateStore(scriptName, httpState);
|
|
213
|
-
}
|
|
214
|
-
return httpState;
|
|
155
|
+
force,
|
|
156
|
+
}).pipe(Effect.tap(() => Clank.success(`Cloudflare State Store '${scriptName}' is ready.`)));
|
|
215
157
|
}
|
|
216
|
-
|
|
217
|
-
const { accountId } = yield* CloudflareEnvironment.CloudflareEnvironment;
|
|
218
|
-
const workerExists = yield* workers
|
|
219
|
-
.getScriptSetting({
|
|
220
|
-
accountId,
|
|
221
|
-
scriptName,
|
|
222
|
-
})
|
|
223
|
-
.pipe(Effect.map((setting) => setting !== undefined), Effect.catchTag("WorkerNotFound", () => Effect.succeed(false)));
|
|
158
|
+
const workerExists = yield* isStateStoreAvailable(scriptName);
|
|
224
159
|
if (workerExists) {
|
|
225
|
-
//
|
|
226
|
-
|
|
160
|
+
// this is a regular update, let's check if it needs an update and refresh credentials
|
|
161
|
+
if (!force) {
|
|
162
|
+
yield* Clank.info(`Worker '${scriptName}' already exists; adopting and refreshing credentials. ` +
|
|
163
|
+
`Use --force to redeploy.`);
|
|
164
|
+
}
|
|
165
|
+
const credentials = yield* loginWithCloudflare(profileName,
|
|
166
|
+
// force refresh during
|
|
167
|
+
true);
|
|
168
|
+
const { url, authToken } = credentials;
|
|
227
169
|
if (!isCI) {
|
|
170
|
+
// we don't write credentials in CI because the file system is ephemeral
|
|
171
|
+
const store = yield* CredentialsStore;
|
|
228
172
|
yield* store.write(profileName, CREDENTIALS_FILE, credentials);
|
|
229
173
|
}
|
|
230
|
-
const
|
|
174
|
+
const { matches, expected, observed } = yield* checkStateStoreVersion(url);
|
|
175
|
+
const httpState = yield* makeCloudflareStateStore({ url, authToken });
|
|
176
|
+
if (!matches || force) {
|
|
177
|
+
if (matches && force) {
|
|
178
|
+
yield* Clank.info(`Cloudflare State Store '${scriptName}' is up to date; force redeploying...`);
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
yield* Clank.info(`Cloudflare State Store '${scriptName}' is out of date ` +
|
|
182
|
+
`(expected v${expected}, observed v${observed ?? "unknown"}); redeploying...`);
|
|
183
|
+
}
|
|
184
|
+
return yield* makeCloudflareStateStore(yield* deployStateStore({
|
|
185
|
+
stage: scriptName,
|
|
186
|
+
state: httpState,
|
|
187
|
+
force,
|
|
188
|
+
}));
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
return httpState;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
// fresh deploy - deploy from local for the first time
|
|
196
|
+
yield* Clank.info(`Deploying Cloudflare State Store '${scriptName}'...`);
|
|
197
|
+
return yield* deployWithLocalState({
|
|
231
198
|
scriptName,
|
|
232
199
|
profileName,
|
|
233
|
-
localState,
|
|
234
200
|
isCI,
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
});
|
|
238
|
-
if (fresh)
|
|
239
|
-
return fresh;
|
|
240
|
-
const httpState = yield* makeCloudflareStateStore(credentials);
|
|
241
|
-
if (alchemyContext.updateStateStore) {
|
|
242
|
-
yield* deployStateStore(scriptName, httpState);
|
|
243
|
-
}
|
|
244
|
-
return httpState;
|
|
245
|
-
}
|
|
246
|
-
if (isCI) {
|
|
247
|
-
// TODO(sam): do we want to support bootstrapping the state store from CI?
|
|
248
|
-
// for now - just die here
|
|
249
|
-
return yield* Effect.die(new AuthError({
|
|
250
|
-
message: `State store not found for script ${scriptName}. Deploy the state store first.`,
|
|
251
|
-
}));
|
|
252
|
-
}
|
|
253
|
-
const shouldDeploy = yield* Clank.confirm({
|
|
254
|
-
message: "Cloudflare State Store not found. Do you want to deploy it?",
|
|
255
|
-
});
|
|
256
|
-
if (!shouldDeploy) {
|
|
257
|
-
return yield* Effect.die(new Clank.PromptCancelled());
|
|
201
|
+
force,
|
|
202
|
+
}).pipe(Effect.tap(() => Clank.success(`Cloudflare State Store '${scriptName}' is ready.`)));
|
|
258
203
|
}
|
|
259
|
-
|
|
260
|
-
scriptName,
|
|
261
|
-
profileName,
|
|
262
|
-
localState,
|
|
263
|
-
isCI,
|
|
264
|
-
force: false,
|
|
265
|
-
});
|
|
266
|
-
}).pipe(recordStateStoreInit, Effect.orDie)).pipe(Layer.provideMerge(Credentials.fromAuthProvider()), Layer.provideMerge(CloudflareEnvironment.fromProfile()), Layer.provideMerge(CloudflareAuth), Layer.provideMerge(Access.AccessLive), Layer.provideMerge(ProfileLive), Layer.provideMerge(CredentialsStoreLive), Layer.orDie);
|
|
267
|
-
const makeCloudflareStateStore = Effect.fnUntraced(function* ({ url, authToken, }) {
|
|
268
|
-
const access = yield* Access.Access;
|
|
269
|
-
const accessHeaders = yield* access.getAccessHeaders(new URL(url).host);
|
|
270
|
-
return yield* makeHttpStateStore({
|
|
271
|
-
url,
|
|
272
|
-
authToken,
|
|
273
|
-
transformClient: HttpClientRequest.setHeaders(accessHeaders),
|
|
274
|
-
id: "cloudflare-http",
|
|
275
|
-
});
|
|
276
|
-
});
|
|
277
|
-
/**
|
|
278
|
-
* Non-destructively copy every resource in the
|
|
279
|
-
* `CloudflareStateStore/<scriptName>` stack from `source` into
|
|
280
|
-
* `destination`, leaving every other stack in `destination` untouched.
|
|
281
|
-
*
|
|
282
|
-
* This intentionally does not delete anything from `destination`: at
|
|
283
|
-
* bootstrap time the destination is the user's live remote state
|
|
284
|
-
* store, and removing entries that happen to be missing locally would
|
|
285
|
-
* be catastrophic.
|
|
286
|
-
*/
|
|
287
|
-
const hoistBootstrapStack = Effect.fnUntraced(function* (source, destination, scriptName) {
|
|
288
|
-
const stack = "CloudflareStateStore";
|
|
289
|
-
const stage = scriptName;
|
|
290
|
-
const fqns = yield* source.list({ stack, stage });
|
|
291
|
-
yield* Effect.annotateCurrentSpan({
|
|
292
|
-
"alchemy.state_store.stack": stack,
|
|
293
|
-
"alchemy.state_store.stage": stage,
|
|
294
|
-
"alchemy.state_store.resources.count": fqns.length,
|
|
295
|
-
});
|
|
296
|
-
yield* Effect.forEach(fqns, Effect.fnUntraced(function* (fqn) {
|
|
297
|
-
const value = yield* source.get({ stack, stage, fqn });
|
|
298
|
-
if (value) {
|
|
299
|
-
yield* destination.set({ stack, stage, fqn, value });
|
|
300
|
-
}
|
|
301
|
-
}), { concurrency: "unbounded" });
|
|
302
|
-
}, Effect.withSpan("state_store.hoist_bootstrap_stack"));
|
|
303
|
-
/**
|
|
304
|
-
* Finish (or resume) the bootstrap of the Cloudflare State Store using
|
|
305
|
-
* the provided local state as the source of truth. This is idempotent
|
|
306
|
-
* and safe to re-run: any resources already applied during a previous
|
|
307
|
-
* partial run will be reconciled, the local stack will be synced into
|
|
308
|
-
* the deployed HTTP store, credentials persisted, and finally the
|
|
309
|
-
* local stack removed - which is what marks the bootstrap as complete.
|
|
310
|
-
*/
|
|
311
|
-
const finishBootstrap = ({ scriptName, profileName, localState, isCI, force, }) => Effect.gen(function* () {
|
|
312
|
-
const { authToken } = yield* deployStateStore(scriptName, localState, force);
|
|
313
|
-
// Don't trust the `authToken` returned by `deploy(...)`: when
|
|
314
|
-
// adoption kicks in (the Secrets Store secret already existed),
|
|
315
|
-
// the locally-generated `Random` value won't match the value
|
|
316
|
-
// actually persisted in Cloudflare, and any HTTP call to the
|
|
317
|
-
// worker would 401. Re-read the live token from the deployed
|
|
318
|
-
// worker via the same edge-preview probe `loginWithCloudflare`
|
|
319
|
-
// uses, so the credentials we persist always reflect what is
|
|
320
|
-
// actually deployed. `loginWithCloudflare` also persists the
|
|
321
|
-
// credentials file (skipping the write in CI), so we don't need
|
|
322
|
-
// to do that explicitly here.
|
|
323
|
-
const { url } = yield* loginWithCloudflare();
|
|
324
|
-
const httpState = yield* makeCloudflareStateStore({ url, authToken });
|
|
325
|
-
// `profileName` is intentionally unused here — `loginWithCloudflare`
|
|
326
|
-
// resolves it itself. Reference it to keep the surrounding API
|
|
327
|
-
// explicit and to avoid an unused-parameter lint.
|
|
328
|
-
void profileName;
|
|
329
|
-
void isCI;
|
|
330
|
-
// Copy the freshly-deployed CloudflareStateStore stack into the
|
|
331
|
-
// remote store. We deliberately do NOT call `syncState` here:
|
|
332
|
-
// syncState is a *mirror* that deletes everything in the
|
|
333
|
-
// destination that is not in the source, which would wipe every
|
|
334
|
-
// user stack in the remote store on any subsequent bootstrap
|
|
335
|
-
// (resume / --force / partial-failure recovery), since by that
|
|
336
|
-
// point local state only contains the bootstrap stack itself.
|
|
337
|
-
yield* hoistBootstrapStack(localState, httpState, scriptName);
|
|
338
|
-
yield* localState.deleteStack({
|
|
339
|
-
stack: "CloudflareStateStore",
|
|
340
|
-
stage: scriptName,
|
|
341
|
-
});
|
|
342
|
-
return httpState;
|
|
343
|
-
}).pipe(Effect.withSpan("state_store.finish_bootstrap", {
|
|
204
|
+
}).pipe(Effect.withSpan("state_store.bootstrap", {
|
|
344
205
|
attributes: {
|
|
345
|
-
"alchemy.state_store.op": "
|
|
346
|
-
"alchemy.state_store.script_name":
|
|
347
|
-
"alchemy.state_store.profile": profileName,
|
|
348
|
-
"alchemy.state_store.ci": isCI,
|
|
206
|
+
"alchemy.state_store.op": "bootstrap",
|
|
207
|
+
"alchemy.state_store.script_name": options.workerName ?? STATE_STORE_SCRIPT_NAME,
|
|
349
208
|
},
|
|
350
209
|
}));
|
|
351
|
-
const deployStateStore = (
|
|
210
|
+
const deployStateStore = ({ stage, state, force, }) => Effect.gen(function* () {
|
|
352
211
|
yield* annotateAccountHash();
|
|
353
|
-
const localState = state ?? (yield* makeLocalState());
|
|
354
212
|
// deploy it with local state (which we will then hoist into the Cloudflare state store)
|
|
355
|
-
const stateLayer = Layer.succeed(State,
|
|
213
|
+
const stateLayer = Layer.succeed(State, state);
|
|
356
214
|
const { url, authToken } = yield* deploy({
|
|
357
215
|
// use the script name as the stage name (so the user can have multiple state stores)
|
|
358
|
-
stage
|
|
216
|
+
stage,
|
|
359
217
|
force,
|
|
360
218
|
stack: Alchemy.Stack("CloudflareStateStore", {
|
|
361
219
|
providers: Cloudflare.providers(),
|
|
@@ -392,13 +250,92 @@ const deployStateStore = (scriptName, state, force) => Effect.gen(function* () {
|
|
|
392
250
|
// observe stale data or trip the staleness check and recurse into
|
|
393
251
|
// another redeploy.
|
|
394
252
|
yield* waitForStateStoreVersion(url);
|
|
395
|
-
return { url, authToken
|
|
253
|
+
return { url, authToken };
|
|
396
254
|
}).pipe(Effect.withSpan("state_store.deploy", {
|
|
397
255
|
attributes: {
|
|
398
|
-
"alchemy.state_store.script_name": scriptName,
|
|
399
256
|
"alchemy.state_store.op": "deploy",
|
|
400
257
|
},
|
|
401
258
|
}), recordStateStoreOp("deploy"));
|
|
259
|
+
const deployWithLocalState = ({ scriptName, isCI, force, profileName, }) => Effect.gen(function* () {
|
|
260
|
+
const localState = yield* makeLocalState();
|
|
261
|
+
const localStage = `${profileName}_${scriptName}`;
|
|
262
|
+
const remoteStage = scriptName;
|
|
263
|
+
const { authToken } = yield* deployStateStore({
|
|
264
|
+
stage: localStage,
|
|
265
|
+
state: localState,
|
|
266
|
+
force,
|
|
267
|
+
});
|
|
268
|
+
const { url } = yield* loginWithCloudflare(profileName, force);
|
|
269
|
+
const httpState = yield* makeCloudflareStateStore({ url, authToken });
|
|
270
|
+
yield* hoistBootstrapStack({
|
|
271
|
+
source: {
|
|
272
|
+
state: localState,
|
|
273
|
+
stage: localStage,
|
|
274
|
+
},
|
|
275
|
+
destination: {
|
|
276
|
+
state: httpState,
|
|
277
|
+
stage: remoteStage,
|
|
278
|
+
},
|
|
279
|
+
});
|
|
280
|
+
yield* localState.deleteStack({
|
|
281
|
+
stack: "CloudflareStateStore",
|
|
282
|
+
stage: localStage,
|
|
283
|
+
});
|
|
284
|
+
return httpState;
|
|
285
|
+
}).pipe(Effect.withSpan("state_store.finish_bootstrap", {
|
|
286
|
+
attributes: {
|
|
287
|
+
"alchemy.state_store.op": "finish_bootstrap",
|
|
288
|
+
"alchemy.state_store.ci": isCI,
|
|
289
|
+
},
|
|
290
|
+
}));
|
|
291
|
+
// check if there's a local stack that wasn't properly hoisted
|
|
292
|
+
const hasLocalStack = (stage) => Effect.gen(function* () {
|
|
293
|
+
const localState = yield* makeLocalState();
|
|
294
|
+
return yield* Effect.map(localState.listStages("CloudflareStateStore"),
|
|
295
|
+
// key off the profile name to avoid conflicts with other profiles
|
|
296
|
+
(stages) => stages.includes(stage));
|
|
297
|
+
});
|
|
298
|
+
/**
|
|
299
|
+
* Non-destructively copy every resource in the
|
|
300
|
+
* `CloudflareStateStore/<scriptName>` stack from `source` into
|
|
301
|
+
* `destination`, leaving every other stack in `destination` untouched.
|
|
302
|
+
*
|
|
303
|
+
* This intentionally does not delete anything from `destination`: at
|
|
304
|
+
* bootstrap time the destination is the user's live remote state
|
|
305
|
+
* store, and removing entries that happen to be missing locally would
|
|
306
|
+
* be catastrophic.
|
|
307
|
+
*/
|
|
308
|
+
const hoistBootstrapStack = Effect.fnUntraced(function* ({ source, destination, }) {
|
|
309
|
+
const stack = "CloudflareStateStore";
|
|
310
|
+
const fqns = yield* source.state.list({ stack, stage: source.stage });
|
|
311
|
+
yield* Effect.annotateCurrentSpan({
|
|
312
|
+
"alchemy.state_store.stack": stack,
|
|
313
|
+
"alchemy.state_store.stage": source.stage,
|
|
314
|
+
"alchemy.state_store.resources.count": fqns.length,
|
|
315
|
+
});
|
|
316
|
+
yield* Effect.forEach(fqns, Effect.fnUntraced(function* (fqn) {
|
|
317
|
+
const value = yield* source.state.get({
|
|
318
|
+
stack,
|
|
319
|
+
stage: source.stage,
|
|
320
|
+
fqn,
|
|
321
|
+
});
|
|
322
|
+
if (value) {
|
|
323
|
+
yield* destination.state
|
|
324
|
+
.set({
|
|
325
|
+
stack,
|
|
326
|
+
stage: destination.stage,
|
|
327
|
+
fqn,
|
|
328
|
+
value,
|
|
329
|
+
})
|
|
330
|
+
.pipe(Effect.retry({
|
|
331
|
+
while: (error) => isHttpClientError(error.cause) &&
|
|
332
|
+
// worker can 404 for a bit on first deploy, retry these
|
|
333
|
+
error.cause.response?.status === 404,
|
|
334
|
+
schedule: Schedule.fixed(200),
|
|
335
|
+
}));
|
|
336
|
+
}
|
|
337
|
+
}), { concurrency: "unbounded" });
|
|
338
|
+
}, Effect.withSpan("state_store.hoist_bootstrap_stack"));
|
|
402
339
|
/**
|
|
403
340
|
* Log in to a Cloudflare-deployed HTTP state-store.
|
|
404
341
|
*
|
|
@@ -415,10 +352,17 @@ const deployStateStore = (scriptName, state, force) => Effect.gen(function* () {
|
|
|
415
352
|
* `CloudflareEnvironment`, `Credentials`, `HttpClient`, and
|
|
416
353
|
* `FileSystem`.
|
|
417
354
|
*/
|
|
418
|
-
export const loginWithCloudflare = () => Effect.gen(function* () {
|
|
419
|
-
const
|
|
420
|
-
const
|
|
355
|
+
export const loginWithCloudflare = (profileName, force) => Effect.gen(function* () {
|
|
356
|
+
const credStore = yield* CredentialsStore;
|
|
357
|
+
const isCI = yield* CI;
|
|
421
358
|
const { accountId } = yield* CloudflareEnvironment.CloudflareEnvironment;
|
|
359
|
+
if (!force) {
|
|
360
|
+
// try and read from the cached credentials first if not forcing (force will always refresh)
|
|
361
|
+
const credentials = yield* credStore.read(profileName, CREDENTIALS_FILE);
|
|
362
|
+
if (credentials) {
|
|
363
|
+
return credentials;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
422
366
|
// 1. Locate the single Secrets Store on the account.
|
|
423
367
|
const stores = yield* SecretsStore.listStores({ accountId });
|
|
424
368
|
const store = stores.result[0];
|
|
@@ -427,15 +371,7 @@ export const loginWithCloudflare = () => Effect.gen(function* () {
|
|
|
427
371
|
message: "No Secrets Store found on this account. Deploy the state store first.",
|
|
428
372
|
}));
|
|
429
373
|
}
|
|
430
|
-
// 2. Fetch the auth-token
|
|
431
|
-
// We piggy-back on the already-deployed state-store script so the
|
|
432
|
-
// `cf-workers-preview-token` header has a real workers.dev route to
|
|
433
|
-
// swap onto. Uploading the probe under a brand-new script name fails
|
|
434
|
-
// on accounts where that script has never been deployed (or where
|
|
435
|
-
// workers.dev preview URLs are off by default — the post-2024
|
|
436
|
-
// Cloudflare default for new accounts) because the host doesn't
|
|
437
|
-
// resolve and Cloudflare's edge serves a 400 HTML error page
|
|
438
|
-
// instead of routing to the preview.
|
|
374
|
+
// 2. Fetch the auth-token from Secrets Store with a temporary edge-preview worker
|
|
439
375
|
const authToken = yield* readSecretViaEdge(STATE_STORE_SCRIPT_NAME, store.id, AuthTokenSecretName).pipe(Effect.retry({
|
|
440
376
|
while: isWorkersPreviewConfigurationError,
|
|
441
377
|
schedule: Schedule.exponential(200).pipe(Schedule.both(Schedule.recurs(15))),
|
|
@@ -446,7 +382,6 @@ export const loginWithCloudflare = () => Effect.gen(function* () {
|
|
|
446
382
|
if (!isCI) {
|
|
447
383
|
// 4. Persist credentials. The profile entry is managed by
|
|
448
384
|
// `loadOrConfigure` when this is invoked through `configure`.
|
|
449
|
-
const credStore = yield* CredentialsStore;
|
|
450
385
|
yield* credStore
|
|
451
386
|
.write(profileName, CREDENTIALS_FILE, {
|
|
452
387
|
url,
|
|
@@ -472,69 +407,21 @@ export const loginWithCloudflare = () => Effect.gen(function* () {
|
|
|
472
407
|
"alchemy.state_store.script_name": STATE_STORE_SCRIPT_NAME,
|
|
473
408
|
},
|
|
474
409
|
}));
|
|
475
|
-
const
|
|
476
|
-
|
|
477
|
-
const
|
|
478
|
-
yield*
|
|
410
|
+
const isStateStoreAvailable = (scriptName = "alchemy-state-store") => Effect.gen(function* () {
|
|
411
|
+
// otherwise, the remote one might exist
|
|
412
|
+
const { accountId } = yield* CloudflareEnvironment.CloudflareEnvironment;
|
|
413
|
+
return yield* workers.getScriptSetting({ accountId, scriptName }).pipe(Effect.map((setting) => setting !== undefined), Effect.catchTag("WorkerNotFound", () => Effect.succeed(false)));
|
|
414
|
+
});
|
|
415
|
+
const makeCloudflareStateStore = Effect.fnUntraced(function* ({ url, authToken, }) {
|
|
416
|
+
const access = yield* Access.Access;
|
|
417
|
+
const accessHeaders = yield* access.getAccessHeaders(new URL(url).host);
|
|
418
|
+
return yield* makeHttpStateStore({
|
|
479
419
|
url,
|
|
480
420
|
authToken,
|
|
421
|
+
transformClient: HttpClientRequest.setHeaders(accessHeaders),
|
|
422
|
+
id: "cloudflare-http",
|
|
481
423
|
});
|
|
482
424
|
});
|
|
483
|
-
const isWorkersPreviewConfigurationError = (error) => error instanceof EdgeSessionError &&
|
|
484
|
-
(error.message.includes("Invalid Workers Preview configuration") ||
|
|
485
|
-
error.message.includes("Error 1031"));
|
|
486
|
-
/**
|
|
487
|
-
* Run {@link finishBootstrap} iff the worker at `url` fails the
|
|
488
|
-
* version probe; otherwise resolve to `undefined`. Centralises the
|
|
489
|
-
* "is the deployed worker still compatible?" guard used by every
|
|
490
|
-
* code path that already has a presumably-valid worker URL.
|
|
491
|
-
*/
|
|
492
|
-
const redeployIfStale = ({ url, scriptName, profileName, localState, isCI, force, }) => Effect.gen(function* () {
|
|
493
|
-
const { matches, expected, observed } = yield* checkStateStoreVersion(url);
|
|
494
|
-
if (matches)
|
|
495
|
-
return undefined;
|
|
496
|
-
yield* Clank.info(`Cloudflare State Store '${scriptName}' is out of date ` +
|
|
497
|
-
`(expected v${expected}, observed v${observed ?? "unknown"}); redeploying...`);
|
|
498
|
-
return yield* finishBootstrap({
|
|
499
|
-
scriptName,
|
|
500
|
-
profileName,
|
|
501
|
-
localState,
|
|
502
|
-
isCI,
|
|
503
|
-
force,
|
|
504
|
-
});
|
|
505
|
-
}).pipe(Effect.withSpan("state_store.redeploy_if_stale", {
|
|
506
|
-
attributes: {
|
|
507
|
-
"alchemy.state_store.op": "redeploy_if_stale",
|
|
508
|
-
"alchemy.state_store.script_name": scriptName,
|
|
509
|
-
"alchemy.state_store.url": url,
|
|
510
|
-
},
|
|
511
|
-
}));
|
|
512
|
-
/**
|
|
513
|
-
* Probe the deployed worker's `/version` endpoint and decide whether
|
|
514
|
-
* it satisfies the current {@link STATE_STORE_VERSION} contract.
|
|
515
|
-
*
|
|
516
|
-
* Reuses the same typed {@link StateApi} client the rest of the store
|
|
517
|
-
* speaks, just without the bearer-token transform — the `/version`
|
|
518
|
-
* route is intentionally unauthenticated so we can probe it before
|
|
519
|
-
* trusting any locally-cached credentials.
|
|
520
|
-
*
|
|
521
|
-
* Returns `true` only on a successful response carrying the matching
|
|
522
|
-
* version. Any failure (404 from a pre-`/version` deploy, transport
|
|
523
|
-
* error, schema mismatch, version mismatch) collapses to `false`,
|
|
524
|
-
* since the caller's response in every case is the same: fall
|
|
525
|
-
* through to the idempotent bootstrap flow.
|
|
526
|
-
*/
|
|
527
|
-
/**
|
|
528
|
-
* Block until the worker at `url` reports the {@link STATE_STORE_VERSION}
|
|
529
|
-
* this CLI was built against, with bounded exponential retry. Used
|
|
530
|
-
* post-deploy to wait out Cloudflare's edge cache so any subsequent
|
|
531
|
-
* read or write goes to the new worker.
|
|
532
|
-
*
|
|
533
|
-
* Failures (transport errors, 404 from a pre-`/version` build, schema
|
|
534
|
-
* mismatch, version mismatch) all collapse to "not ready yet" and are
|
|
535
|
-
* retried; once the budget is exhausted we surface a hard failure
|
|
536
|
-
* since continuing would talk to the wrong worker.
|
|
537
|
-
*/
|
|
538
425
|
class StateStoreVersionNotReady extends Error {
|
|
539
426
|
expected;
|
|
540
427
|
observed;
|
|
@@ -565,13 +452,22 @@ const waitForStateStoreVersion = (url) => Effect.gen(function* () {
|
|
|
565
452
|
}));
|
|
566
453
|
const checkStateStoreVersion = (url) => Effect.gen(function* () {
|
|
567
454
|
const client = yield* HttpApiClient.make(StateApi, { baseUrl: url });
|
|
455
|
+
const isAvailable = yield* Effect.cached(isStateStoreAvailable(STATE_STORE_SCRIPT_NAME));
|
|
568
456
|
// The /version route may 404 transiently after a fresh deploy
|
|
569
457
|
// while Cloudflare propagates the new script to the edge, and may
|
|
570
458
|
// also surface transport-level blips on cold workers.dev hosts.
|
|
571
459
|
// Retry the probe itself for ~10s before giving up — only after
|
|
572
460
|
// exhausting that budget do we collapse to `undefined` and let
|
|
573
461
|
// the caller treat it as a version mismatch.
|
|
574
|
-
const result = yield* client.version.getVersion().pipe(Effect.
|
|
462
|
+
const result = yield* client.version.getVersion().pipe(Effect.catchTag("HttpClientError", (e) =>
|
|
463
|
+
// if we get a 404 here, it means we assumed the worker shoudl exist, but it does not
|
|
464
|
+
// we should do a check to see if it does
|
|
465
|
+
e.response?.status === 404
|
|
466
|
+
? isAvailable.pipe(Effect.flatMap((isAvailable) =>
|
|
467
|
+
// if the worker is available, then we should assume it was recently created and retry by propagating the error
|
|
468
|
+
// otherwise, return undefined (we don't know the version, there is no worker)
|
|
469
|
+
isAvailable ? Effect.fail(e) : Effect.succeed(undefined)))
|
|
470
|
+
: Effect.fail(e)), Effect.retry({
|
|
575
471
|
schedule: Schedule.spaced("250 millis").pipe(Schedule.both(Schedule.recurs(40))),
|
|
576
472
|
}), Effect.catch(() => Effect.succeed(undefined)));
|
|
577
473
|
const matches = result?.version === STATE_STORE_VERSION;
|
|
@@ -654,4 +550,49 @@ const readSecretViaEdge = (scriptName, storeId, secretName) => Effect.gen(functi
|
|
|
654
550
|
"alchemy.state_store.secret_name": secretName,
|
|
655
551
|
},
|
|
656
552
|
}));
|
|
553
|
+
const writeCredentials = (url, authToken) => Effect.gen(function* () {
|
|
554
|
+
const profileName = yield* ALCHEMY_PROFILE;
|
|
555
|
+
const credStore = yield* CredentialsStore;
|
|
556
|
+
yield* credStore.write(profileName, CREDENTIALS_FILE, {
|
|
557
|
+
url,
|
|
558
|
+
authToken,
|
|
559
|
+
});
|
|
560
|
+
});
|
|
561
|
+
const isWorkersPreviewConfigurationError = (error) => error instanceof EdgeSessionError &&
|
|
562
|
+
(error.message.includes("Invalid Workers Preview configuration") ||
|
|
563
|
+
error.message.includes("Error 1031"));
|
|
564
|
+
/**
|
|
565
|
+
* SHA-256 hex digest of the Cloudflare account ID. Used as a stable
|
|
566
|
+
* pseudonymous identifier on telemetry spans so the dashboard can
|
|
567
|
+
* count distinct state-store deployments without leaking the raw
|
|
568
|
+
* accountId. Mirrors the `alchemy.git.origin_hash` pattern in
|
|
569
|
+
* `Telemetry/Attributes.ts`.
|
|
570
|
+
*/
|
|
571
|
+
const hashAccountId = (accountId) => Effect.sync(() => crypto.createHash("sha256").update(accountId).digest("hex"));
|
|
572
|
+
/**
|
|
573
|
+
* Best-effort Cloudflare-account-hash annotation on the current span.
|
|
574
|
+
* Resolves the accountId from {@link CloudflareEnvironment} and
|
|
575
|
+
* attaches `alchemy.cloudflare.account_hash` to whichever span is
|
|
576
|
+
* active. Silently no-ops if the environment isn't resolvable so
|
|
577
|
+
* State-store layer construction still succeeds in degraded paths.
|
|
578
|
+
*
|
|
579
|
+
* `noTrack` controls whether the hash is attached:
|
|
580
|
+
* - `true` — never annotate (caller-level opt-out).
|
|
581
|
+
* - `false` — always annotate, regardless of env.
|
|
582
|
+
* - `undefined` — fall back to the `NO_TRACK` env var; default off.
|
|
583
|
+
*/
|
|
584
|
+
const annotateAccountHash = (noTrack) => Effect.gen(function* () {
|
|
585
|
+
if (noTrack === true)
|
|
586
|
+
return;
|
|
587
|
+
if (noTrack === undefined) {
|
|
588
|
+
const fromEnv = yield* Config.boolean("NO_TRACK").pipe(Config.withDefault(false));
|
|
589
|
+
if (fromEnv)
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
const env = yield* Effect.serviceOption(CloudflareEnvironment.CloudflareEnvironment);
|
|
593
|
+
if (env._tag !== "Some")
|
|
594
|
+
return;
|
|
595
|
+
const hash = yield* hashAccountId(env.value.accountId);
|
|
596
|
+
yield* Effect.annotateCurrentSpan("alchemy.cloudflare.account_hash", hash);
|
|
597
|
+
}).pipe(Effect.catch(() => Effect.void));
|
|
657
598
|
//# sourceMappingURL=State.js.map
|