@reventlessdev/reventless-seed 1.0.0-alpha.3 → 1.0.0-alpha.5
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/CHANGELOG.md +14 -0
- package/lib/bs/.compiler.log +2 -2
- package/lib/bs/.sourcedirs.json +1 -0
- package/lib/bs/ReventlessSeed.cmi +0 -0
- package/lib/bs/ReventlessSeed.cmt +0 -0
- package/lib/bs/ReventlessSeed.mlmap +1 -0
- package/lib/bs/compiler-info.json +2 -2
- package/lib/bs/src/Seed-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed.ast +0 -0
- package/lib/bs/src/Seed.res +4 -2
- package/lib/bs/src/Seed_Client-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed_Client-ReventlessSeed.cmj +0 -0
- package/lib/bs/src/Seed_Client-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Client.ast +0 -0
- package/lib/bs/src/Seed_Client.res +36 -0
- package/lib/bs/src/Seed_Client.res.mjs +23 -0
- package/lib/bs/src/Seed_Connect-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed_Connect-ReventlessSeed.cmj +0 -0
- package/lib/bs/src/Seed_Connect-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Connect.ast +0 -0
- package/lib/bs/src/Seed_Connect.res +49 -11
- package/lib/bs/src/Seed_Connect.res.mjs +24 -3
- package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Random-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Runner-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed_Runner-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Types-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Upload-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed_Upload-ReventlessSeed.cmj +0 -0
- package/lib/bs/src/Seed_Upload-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Upload.ast +0 -0
- package/lib/bs/src/Seed_Upload.res +59 -0
- package/lib/bs/src/Seed_Upload.res.mjs +29 -1
- package/lib/bs/tests/UploadEndpointResolutionTest-ReventlessSeed.cmi +0 -0
- package/lib/bs/tests/UploadEndpointResolutionTest-ReventlessSeed.cmj +0 -0
- package/lib/bs/tests/UploadEndpointResolutionTest-ReventlessSeed.cmt +0 -0
- package/lib/bs/tests/UploadEndpointResolutionTest.ast +0 -0
- package/lib/bs/tests/UploadEndpointResolutionTest.res +101 -0
- package/lib/bs/tests/UploadEndpointResolutionTest.res.mjs +59 -0
- package/lib/ocaml/.compiler.log +2 -2
- package/lib/ocaml/ReventlessSeed.cmi +0 -0
- package/lib/ocaml/ReventlessSeed.cmt +0 -0
- package/lib/ocaml/ReventlessSeed.mlmap +1 -0
- package/lib/ocaml/Seed-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed.ast +0 -0
- package/lib/ocaml/Seed.res +4 -2
- package/lib/ocaml/Seed_Client-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_Client-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_Client-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Client.ast +0 -0
- package/lib/ocaml/Seed_Client.res +36 -0
- package/lib/ocaml/Seed_Connect-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_Connect-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_Connect-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Connect.ast +0 -0
- package/lib/ocaml/Seed_Connect.res +49 -11
- package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Random-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Runner-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_Runner-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Types-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Upload-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_Upload-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_Upload-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Upload.ast +0 -0
- package/lib/ocaml/Seed_Upload.res +59 -0
- package/lib/ocaml/UploadEndpointResolutionTest-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/UploadEndpointResolutionTest-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/UploadEndpointResolutionTest-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/UploadEndpointResolutionTest.ast +0 -0
- package/lib/ocaml/UploadEndpointResolutionTest.res +101 -0
- package/package.json +3 -2
- package/rescript.json +8 -1
- package/src/Seed.res +4 -2
- package/src/Seed_Client.res +36 -0
- package/src/Seed_Client.res.mjs +23 -0
- package/src/Seed_Connect.res +49 -11
- package/src/Seed_Connect.res.mjs +24 -3
- package/src/Seed_Upload.res +59 -0
- package/src/Seed_Upload.res.mjs +29 -1
- package/tests/UploadEndpointResolutionTest.res +101 -0
- package/tests/UploadEndpointResolutionTest.res.mjs +59 -0
package/lib/ocaml/.compiler.log
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
#Start(
|
|
2
|
-
#Done(
|
|
1
|
+
#Start(1785495045271)
|
|
2
|
+
#Done(1785495045300)
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/lib/ocaml/Seed.ast
CHANGED
|
Binary file
|
package/lib/ocaml/Seed.res
CHANGED
|
@@ -14,11 +14,13 @@ module Connect = Seed_Connect
|
|
|
14
14
|
|
|
15
15
|
exception Failed = Seed_Types.Failed
|
|
16
16
|
|
|
17
|
-
// A live target a data set seeds against: an authenticated client, the
|
|
18
|
-
//
|
|
17
|
+
// A live target a data set seeds against: an authenticated client, the
|
|
18
|
+
// deployment's upload endpoints (the legacy single service and the per-store
|
|
19
|
+
// map, either of which may be empty), and a label.
|
|
19
20
|
type connection = Seed_Connect.connection = {
|
|
20
21
|
client: Seed_Client.t,
|
|
21
22
|
uploadEndpoint: string,
|
|
23
|
+
uploadEndpoints: dict<string>,
|
|
22
24
|
label: string,
|
|
23
25
|
}
|
|
24
26
|
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -216,6 +216,42 @@ let queryAllNodes = async (t: t, ~field as fieldName: string, ~selection: string
|
|
|
216
216
|
let countNodes = async (t: t, ~field: string): int =>
|
|
217
217
|
(await queryAllNodes(t, ~field, ~selection="id"))->Array.length
|
|
218
218
|
|
|
219
|
+
/**
|
|
220
|
+
* Polls a connection field until the collected nodes satisfy `satisfied`, or the
|
|
221
|
+
* timeout elapses. Read models are eventually consistent: a view queried the
|
|
222
|
+
* instant after the writes that feed it can still read empty or partial (a
|
|
223
|
+
* DynamoDB connection scan is eventually consistent by default, and the write and
|
|
224
|
+
* read-back can land on different replicas), so a cross-check that asserts on a
|
|
225
|
+
* single immediate read races the projection. Same rationale as `waitForIds`,
|
|
226
|
+
* for the "observe the whole connection" case rather than a fixed id set.
|
|
227
|
+
*
|
|
228
|
+
* On timeout, throws `Failed(onTimeout(finalNodes))` so the caller can report
|
|
229
|
+
* what the view actually settled on — distinguishing a still-empty view from a
|
|
230
|
+
* genuine count mismatch — instead of a bare assertion.
|
|
231
|
+
*/
|
|
232
|
+
let queryAllNodesUntil = async (
|
|
233
|
+
t: t,
|
|
234
|
+
~field: string,
|
|
235
|
+
~selection: string,
|
|
236
|
+
~satisfied: array<JSON.t> => bool,
|
|
237
|
+
~onTimeout: array<JSON.t> => string,
|
|
238
|
+
~timeoutMs: int=60000,
|
|
239
|
+
): array<JSON.t> => {
|
|
240
|
+
let deadline = nowMs() +. Int.toFloat(timeoutMs)
|
|
241
|
+
let result = ref(None)
|
|
242
|
+
while result.contents == None {
|
|
243
|
+
let nodes = await queryAllNodes(t, ~field, ~selection)
|
|
244
|
+
if satisfied(nodes) {
|
|
245
|
+
result := Some(nodes)
|
|
246
|
+
} else if nowMs() > deadline {
|
|
247
|
+
throw(Failed(onTimeout(nodes)))
|
|
248
|
+
} else {
|
|
249
|
+
await sleep(500)
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
result.contents->Option.getOr([])
|
|
253
|
+
}
|
|
254
|
+
|
|
219
255
|
/**
|
|
220
256
|
* Polls a `<View>ByIds` field until every id is present.
|
|
221
257
|
*
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -1,15 +1,25 @@
|
|
|
1
1
|
// Turns endpoints plus a login function into a ready `connection`.
|
|
2
2
|
//
|
|
3
3
|
// A connection is what a data set seeds against: an authenticated GraphQL
|
|
4
|
-
// client, the upload
|
|
5
|
-
// a human label for the target. `make` is provider-agnostic — it prompts for
|
|
4
|
+
// client, the deployment's upload endpoints (empty when it serves no uploads),
|
|
5
|
+
// and a human label for the target. `make` is provider-agnostic — it prompts for
|
|
6
6
|
// credentials, calls the supplied `login` to obtain a bearer, and wires it onto
|
|
7
7
|
// the client via `useToken`. Where the bearer comes from (a local `/login`
|
|
8
8
|
// round-trip, a Cognito id token) is the caller's concern.
|
|
9
9
|
|
|
10
10
|
open Seed_Types
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
// `uploadEndpoint` is the legacy single presign service (and the local dev
|
|
13
|
+
// server's one upload route); `uploadEndpoints` maps the qualified
|
|
14
|
+
// `{plugin}.{store}` of each declared store to its own presign endpoint. Both,
|
|
15
|
+
// because a deployment may publish either or both — see `Seed_Upload.endpointFor`
|
|
16
|
+
// for the resolution between them.
|
|
17
|
+
type connection = {
|
|
18
|
+
client: Seed_Client.t,
|
|
19
|
+
uploadEndpoint: string,
|
|
20
|
+
uploadEndpoints: dict<string>,
|
|
21
|
+
label: string,
|
|
22
|
+
}
|
|
13
23
|
|
|
14
24
|
/**
|
|
15
25
|
* Prompts for credentials, calls `login` to mint a bearer, and returns a
|
|
@@ -20,23 +30,47 @@ let make = async (
|
|
|
20
30
|
~label: string,
|
|
21
31
|
~endpoint: string,
|
|
22
32
|
~uploadEndpoint: string,
|
|
33
|
+
~uploadEndpoints: dict<string>=Dict.make(),
|
|
23
34
|
~login: (~username: string, ~password: string) => promise<string>,
|
|
24
35
|
~localDefaults: bool=false,
|
|
25
36
|
): connection => {
|
|
26
|
-
// `SEED_SKIP_UPLOADS` forces the connection to carry no upload endpoint
|
|
27
|
-
// data set's upload phase no-ops the same way it does when a
|
|
28
|
-
// none. Use it to seed domain data fast, or to skip a
|
|
29
|
-
// without editing the data set. It is the single
|
|
30
|
-
// `REVENTLESS_UPLOAD_ENDPOINT` env reads as "unset" (falls back
|
|
31
|
-
// so this is the reliable way to disable uploads
|
|
32
|
-
|
|
37
|
+
// `SEED_SKIP_UPLOADS` forces the connection to carry no upload endpoint at
|
|
38
|
+
// all, so a data set's upload phase no-ops the same way it does when a
|
|
39
|
+
// deployment serves none. Use it to seed domain data fast, or to skip a
|
|
40
|
+
// broken/absent upload path without editing the data set. It is the single
|
|
41
|
+
// knob: an empty `REVENTLESS_UPLOAD_ENDPOINT` env reads as "unset" (falls back
|
|
42
|
+
// to discovery), so this is the reliable way to disable uploads — which means
|
|
43
|
+
// it has to clear the per-store map too, or a declared store keeps uploading
|
|
44
|
+
// through it.
|
|
45
|
+
let skip = Seed_Prompt.envValue("SEED_SKIP_UPLOADS")->Option.isSome
|
|
46
|
+
let uploadEndpoint = skip ? "" : uploadEndpoint
|
|
47
|
+
let uploadEndpoints = skip ? Dict.make() : uploadEndpoints
|
|
33
48
|
let (username, password) = await Seed_Prompt.credentials(~localDefaults)
|
|
34
49
|
let token = await login(~username, ~password)
|
|
35
50
|
let client = Seed_Client.make(~config={endpoint: endpoint})
|
|
36
51
|
client->Seed_Client.useToken(token)
|
|
37
|
-
{client, uploadEndpoint, label}
|
|
52
|
+
{client, uploadEndpoint, uploadEndpoints, label}
|
|
38
53
|
}
|
|
39
54
|
|
|
55
|
+
/**
|
|
56
|
+
* The endpoint an asset destined for `store` uploads through, or the reason
|
|
57
|
+
* there is none — the message to report when the upload phase skips.
|
|
58
|
+
*
|
|
59
|
+
* `store` is the qualified `{plugin}.{store}` the asset's field declares, e.g.
|
|
60
|
+
* `"Catalog.productImages"`. Resolution is `Seed_Upload.endpointFor`; naming a
|
|
61
|
+
* store the deployment does not serve falls back to the legacy single service
|
|
62
|
+
* rather than to another store's endpoint.
|
|
63
|
+
*/
|
|
64
|
+
let uploadEndpointFor = (c: connection, ~store: string): result<string, string> =>
|
|
65
|
+
switch Seed_Upload.endpointFor(
|
|
66
|
+
~store,
|
|
67
|
+
~uploadEndpoint=c.uploadEndpoint,
|
|
68
|
+
~uploadEndpoints=c.uploadEndpoints,
|
|
69
|
+
) {
|
|
70
|
+
| Some(endpoint) => Ok(endpoint)
|
|
71
|
+
| None => Error(Seed_Upload.unresolvedReason(~store, ~uploadEndpoints=c.uploadEndpoints))
|
|
72
|
+
}
|
|
73
|
+
|
|
40
74
|
/**
|
|
41
75
|
* A login function backed by an HTTP login endpoint (the local dev
|
|
42
76
|
* `/__inmemory/login` shape: POST `{username, password}` → `{token}`). Reuses
|
|
@@ -63,6 +97,10 @@ let envOr = (key: string, fallback: string): string =>
|
|
|
63
97
|
* A ready `connect` thunk for the local dev platform, with the localhost
|
|
64
98
|
* defaults baked in and overridable per endpoint (arg, then env var, then
|
|
65
99
|
* default). Empty input at the credential prompt falls back to `admin`/`admin`.
|
|
100
|
+
*
|
|
101
|
+
* No per-store map: the local dev server serves every store through its one
|
|
102
|
+
* upload route, so a data set naming a store resolves to that route by the
|
|
103
|
+
* "declares a store with no matching endpoint" row.
|
|
66
104
|
*/
|
|
67
105
|
let local = (~graphql=?, ~upload=?, ~login=?, ()): (unit => promise<connection>) => {
|
|
68
106
|
let endpoint =
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -10,6 +10,11 @@
|
|
|
10
10
|
// absolute URL against the endpoint's origin: AWS returns an absolute presigned
|
|
11
11
|
// S3 URL (unchanged), while the local route returns a same-origin relative
|
|
12
12
|
// `/{prefix}/{key}` that Node's `fetch` cannot PUT to until it is made absolute.
|
|
13
|
+
//
|
|
14
|
+
// `uploadAsset` is the transport and takes a resolved endpoint. *Which* endpoint
|
|
15
|
+
// an asset uses — a deployment now publishes one per declared store — is
|
|
16
|
+
// `endpointFor` below, kept separate so the decision is testable without a
|
|
17
|
+
// running presign service.
|
|
13
18
|
|
|
14
19
|
type fetchInit = {method: string, headers: dict<string>, body: string}
|
|
15
20
|
type response
|
|
@@ -38,6 +43,60 @@ let asString = (json: JSON.t): option<string> =>
|
|
|
38
43
|
| _ => None
|
|
39
44
|
}
|
|
40
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Which endpoint an asset destined for `store` uploads through:
|
|
48
|
+
*
|
|
49
|
+
* declares a store with a matching endpoint → that store's endpoint
|
|
50
|
+
* declares a store with no matching endpoint → the legacy single service
|
|
51
|
+
* declares no store → the legacy single service
|
|
52
|
+
* neither available → `None`
|
|
53
|
+
*
|
|
54
|
+
* `store` is the qualified `{plugin}.{store}` the platform keys its per-store
|
|
55
|
+
* presign endpoints by. The two middle rows are what keep a deployment predating
|
|
56
|
+
* per-store endpoints — and the local dev server, which serves one upload route
|
|
57
|
+
* — working unchanged.
|
|
58
|
+
*
|
|
59
|
+
* These are the same four rows the AutoUI renderer resolves an upload field by.
|
|
60
|
+
* Resolving one declaration by two different rules is how an asset lands in
|
|
61
|
+
* another plugin's bucket with a 2xx and a plausible-looking ref, so a missing
|
|
62
|
+
* key falls back to the legacy service rather than to some other store.
|
|
63
|
+
*/
|
|
64
|
+
let endpointFor = (
|
|
65
|
+
~store: option<string>=?,
|
|
66
|
+
~uploadEndpoint: string,
|
|
67
|
+
~uploadEndpoints: dict<string>,
|
|
68
|
+
): option<string> => {
|
|
69
|
+
let legacy = uploadEndpoint == "" ? None : Some(uploadEndpoint)
|
|
70
|
+
switch store {
|
|
71
|
+
| Some(s) => uploadEndpoints->Dict.get(s)->Option.orElse(legacy)
|
|
72
|
+
| None => legacy
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Why `endpointFor` resolved nothing — the message a data set reports when its
|
|
78
|
+
* upload phase skips.
|
|
79
|
+
*
|
|
80
|
+
* "no upload endpoint" states a fact about the *deployment*; when the deployment
|
|
81
|
+
* publishes endpoints the caller could not match, the fact is about the *client*
|
|
82
|
+
* and that phrasing sends a reader to the bucket, the presign service and the
|
|
83
|
+
* IAM policy, all of which are correct. So the three cases are distinguished and
|
|
84
|
+
* the unmatched one names both the store it wanted and the keys it saw.
|
|
85
|
+
*/
|
|
86
|
+
let unresolvedReason = (~store: option<string>=?, ~uploadEndpoints: dict<string>): string => {
|
|
87
|
+
let available = uploadEndpoints->Dict.keysToArray
|
|
88
|
+
switch (Seed_Prompt.envValue("SEED_SKIP_UPLOADS"), store, available) {
|
|
89
|
+
| (Some(_), _, _) => "SEED_SKIP_UPLOADS is set"
|
|
90
|
+
| (None, _, []) => "this deployment publishes no upload endpoint"
|
|
91
|
+
| (None, Some(s), keys) =>
|
|
92
|
+
`this deployment publishes upload endpoints for ${keys->Array.join(", ")}, none for "${s}"`
|
|
93
|
+
| (None, None, keys) =>
|
|
94
|
+
`this deployment publishes only per-store upload endpoints (${keys->Array.join(
|
|
95
|
+
", ",
|
|
96
|
+
)}) and this upload names no store`
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
41
100
|
// A `fetch` that reports an unreachable endpoint as `Error` instead of throwing,
|
|
42
101
|
// so the two-step upload can thread failures without exceptions.
|
|
43
102
|
let tryFetch = async (url: string, init: fetchInit): result<response, string> =>
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// Which endpoint an asset uploads through once a deployment publishes one per
|
|
2
|
+
// declared store, and what a data set is told when none resolves.
|
|
3
|
+
//
|
|
4
|
+
// The four rows are the same ones the AutoUI renderer resolves an upload field
|
|
5
|
+
// by. They are pinned here because the two middle ones are what keep a
|
|
6
|
+
// deployment predating per-store endpoints — and the local dev server, which
|
|
7
|
+
// serves a single upload route — working unchanged, and because the row that
|
|
8
|
+
// falls back to the legacy service must never fall back to *another store*: that
|
|
9
|
+
// uploads into a different plugin's bucket with a 2xx and a plausible ref.
|
|
10
|
+
|
|
11
|
+
open JestGlobals
|
|
12
|
+
|
|
13
|
+
@val @scope("process") external processEnv: dict<string> = "env"
|
|
14
|
+
|
|
15
|
+
// `unresolvedReason` reads the knob, so the ambient environment has to be quiet.
|
|
16
|
+
let clearSkip = () => processEnv->Dict.set("SEED_SKIP_UPLOADS", "")
|
|
17
|
+
|
|
18
|
+
let perStore = Dict.fromArray([
|
|
19
|
+
("Catalog.productImages", "https://catalog.example/"),
|
|
20
|
+
("Ordering.invoices", "https://ordering.example/"),
|
|
21
|
+
])
|
|
22
|
+
|
|
23
|
+
describe("Seed.Upload.endpointFor", () => {
|
|
24
|
+
testSync("a declared store with a matching endpoint uses that store's endpoint", () =>
|
|
25
|
+
expect(
|
|
26
|
+
Seed.Upload.endpointFor(
|
|
27
|
+
~store="Catalog.productImages",
|
|
28
|
+
~uploadEndpoint="https://legacy.example/",
|
|
29
|
+
~uploadEndpoints=perStore,
|
|
30
|
+
),
|
|
31
|
+
)->toEqual(Some("https://catalog.example/"))
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
// Not "https://ordering.example/" — a wrong guess here is a silent
|
|
35
|
+
// wrong-bucket write.
|
|
36
|
+
testSync("a declared store with no matching endpoint falls back to the legacy service", () =>
|
|
37
|
+
expect(
|
|
38
|
+
Seed.Upload.endpointFor(
|
|
39
|
+
~store="Catalog.brochures",
|
|
40
|
+
~uploadEndpoint="https://legacy.example/",
|
|
41
|
+
~uploadEndpoints=perStore,
|
|
42
|
+
),
|
|
43
|
+
)->toEqual(Some("https://legacy.example/"))
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
testSync("naming no store uses the legacy service", () =>
|
|
47
|
+
expect(
|
|
48
|
+
Seed.Upload.endpointFor(~uploadEndpoint="https://legacy.example/", ~uploadEndpoints=perStore),
|
|
49
|
+
)->toEqual(Some("https://legacy.example/"))
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
testSync("neither available resolves nothing", () =>
|
|
53
|
+
expect(
|
|
54
|
+
Seed.Upload.endpointFor(
|
|
55
|
+
~store="Catalog.brochures",
|
|
56
|
+
~uploadEndpoint="",
|
|
57
|
+
~uploadEndpoints=Dict.make(),
|
|
58
|
+
),
|
|
59
|
+
)->toEqual(None)
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
// The local dev server's shape: one upload route, no per-store map, and a data
|
|
63
|
+
// set that names its store all the same.
|
|
64
|
+
testSync("a single-route deployment serves a store-naming data set", () =>
|
|
65
|
+
expect(
|
|
66
|
+
Seed.Upload.endpointFor(
|
|
67
|
+
~store="Catalog.productImages",
|
|
68
|
+
~uploadEndpoint="http://localhost:4000/__inmemory/upload",
|
|
69
|
+
~uploadEndpoints=Dict.make(),
|
|
70
|
+
),
|
|
71
|
+
)->toEqual(Some("http://localhost:4000/__inmemory/upload"))
|
|
72
|
+
)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
// "no upload endpoint" states a fact about the deployment. When the deployment
|
|
76
|
+
// publishes endpoints the caller could not match, the fact is about the client,
|
|
77
|
+
// and that phrasing sends a reader to the bucket, the presign service and the
|
|
78
|
+
// IAM policy — all of which are correct.
|
|
79
|
+
describe("Seed.Upload.unresolvedReason", () => {
|
|
80
|
+
beforeEach(clearSkip)
|
|
81
|
+
|
|
82
|
+
testSync("no endpoints at all reads as a fact about the deployment", () =>
|
|
83
|
+
expect(
|
|
84
|
+
Seed.Upload.unresolvedReason(~store="Catalog.productImages", ~uploadEndpoints=Dict.make()),
|
|
85
|
+
)->toBe("this deployment publishes no upload endpoint")
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
testSync("an unmatched store names the store and the keys that were available", () => {
|
|
89
|
+
let msg = Seed.Upload.unresolvedReason(~store="Catalog.brochures", ~uploadEndpoints=perStore)
|
|
90
|
+
expect(msg)->toContain("Catalog.brochures")
|
|
91
|
+
expect(msg)->toContain("Catalog.productImages")
|
|
92
|
+
expect(msg)->toContain("Ordering.invoices")
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
testSync("the knob outranks both, so a skipped run is never read as a broken one", () => {
|
|
96
|
+
processEnv->Dict.set("SEED_SKIP_UPLOADS", "1")
|
|
97
|
+
expect(
|
|
98
|
+
Seed.Upload.unresolvedReason(~store="Catalog.productImages", ~uploadEndpoints=perStore),
|
|
99
|
+
)->toBe("SEED_SKIP_UPLOADS is set")
|
|
100
|
+
})
|
|
101
|
+
})
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reventlessdev/reventless-seed",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.5",
|
|
4
4
|
"description": "Reusable harness for seeding a Reventless platform through its public GraphQL command API",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"devDependencies": {
|
|
7
|
-
"rescript": "12.3.0"
|
|
7
|
+
"rescript": "12.3.0",
|
|
8
|
+
"@reventlessdev/rescript-jest": "1.0.0-alpha.9"
|
|
8
9
|
},
|
|
9
10
|
"peerDependencies": {
|
|
10
11
|
"rescript": "12.3.0"
|
package/rescript.json
CHANGED
|
@@ -12,9 +12,16 @@
|
|
|
12
12
|
{
|
|
13
13
|
"dir": "src",
|
|
14
14
|
"subdirs": true
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"dir": "tests",
|
|
18
|
+
"subdirs": true,
|
|
19
|
+
"type": "dev"
|
|
15
20
|
}
|
|
16
21
|
],
|
|
17
|
-
"dependencies": [
|
|
22
|
+
"dependencies": [
|
|
23
|
+
"@reventlessdev/rescript-jest"
|
|
24
|
+
],
|
|
18
25
|
"compiler-flags": [],
|
|
19
26
|
"suffix": ".res.mjs"
|
|
20
27
|
}
|
package/src/Seed.res
CHANGED
|
@@ -14,11 +14,13 @@ module Connect = Seed_Connect
|
|
|
14
14
|
|
|
15
15
|
exception Failed = Seed_Types.Failed
|
|
16
16
|
|
|
17
|
-
// A live target a data set seeds against: an authenticated client, the
|
|
18
|
-
//
|
|
17
|
+
// A live target a data set seeds against: an authenticated client, the
|
|
18
|
+
// deployment's upload endpoints (the legacy single service and the per-store
|
|
19
|
+
// map, either of which may be empty), and a label.
|
|
19
20
|
type connection = Seed_Connect.connection = {
|
|
20
21
|
client: Seed_Client.t,
|
|
21
22
|
uploadEndpoint: string,
|
|
23
|
+
uploadEndpoints: dict<string>,
|
|
22
24
|
label: string,
|
|
23
25
|
}
|
|
24
26
|
|
package/src/Seed_Client.res
CHANGED
|
@@ -216,6 +216,42 @@ let queryAllNodes = async (t: t, ~field as fieldName: string, ~selection: string
|
|
|
216
216
|
let countNodes = async (t: t, ~field: string): int =>
|
|
217
217
|
(await queryAllNodes(t, ~field, ~selection="id"))->Array.length
|
|
218
218
|
|
|
219
|
+
/**
|
|
220
|
+
* Polls a connection field until the collected nodes satisfy `satisfied`, or the
|
|
221
|
+
* timeout elapses. Read models are eventually consistent: a view queried the
|
|
222
|
+
* instant after the writes that feed it can still read empty or partial (a
|
|
223
|
+
* DynamoDB connection scan is eventually consistent by default, and the write and
|
|
224
|
+
* read-back can land on different replicas), so a cross-check that asserts on a
|
|
225
|
+
* single immediate read races the projection. Same rationale as `waitForIds`,
|
|
226
|
+
* for the "observe the whole connection" case rather than a fixed id set.
|
|
227
|
+
*
|
|
228
|
+
* On timeout, throws `Failed(onTimeout(finalNodes))` so the caller can report
|
|
229
|
+
* what the view actually settled on — distinguishing a still-empty view from a
|
|
230
|
+
* genuine count mismatch — instead of a bare assertion.
|
|
231
|
+
*/
|
|
232
|
+
let queryAllNodesUntil = async (
|
|
233
|
+
t: t,
|
|
234
|
+
~field: string,
|
|
235
|
+
~selection: string,
|
|
236
|
+
~satisfied: array<JSON.t> => bool,
|
|
237
|
+
~onTimeout: array<JSON.t> => string,
|
|
238
|
+
~timeoutMs: int=60000,
|
|
239
|
+
): array<JSON.t> => {
|
|
240
|
+
let deadline = nowMs() +. Int.toFloat(timeoutMs)
|
|
241
|
+
let result = ref(None)
|
|
242
|
+
while result.contents == None {
|
|
243
|
+
let nodes = await queryAllNodes(t, ~field, ~selection)
|
|
244
|
+
if satisfied(nodes) {
|
|
245
|
+
result := Some(nodes)
|
|
246
|
+
} else if nowMs() > deadline {
|
|
247
|
+
throw(Failed(onTimeout(nodes)))
|
|
248
|
+
} else {
|
|
249
|
+
await sleep(500)
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
result.contents->Option.getOr([])
|
|
253
|
+
}
|
|
254
|
+
|
|
219
255
|
/**
|
|
220
256
|
* Polls a `<View>ByIds` field until every id is present.
|
|
221
257
|
*
|
package/src/Seed_Client.res.mjs
CHANGED
|
@@ -208,6 +208,28 @@ async function countNodes(t, field) {
|
|
|
208
208
|
return (await queryAllNodes(t, field, "id")).length;
|
|
209
209
|
}
|
|
210
210
|
|
|
211
|
+
async function queryAllNodesUntil(t, field, selection, satisfied, onTimeout, timeoutMsOpt) {
|
|
212
|
+
let timeoutMs = timeoutMsOpt !== undefined ? timeoutMsOpt : 60000;
|
|
213
|
+
let deadline = Date.now() + timeoutMs;
|
|
214
|
+
let result;
|
|
215
|
+
while (result === undefined) {
|
|
216
|
+
let nodes = await queryAllNodes(t, field, selection);
|
|
217
|
+
if (satisfied(nodes)) {
|
|
218
|
+
result = nodes;
|
|
219
|
+
} else {
|
|
220
|
+
if (Date.now() > deadline) {
|
|
221
|
+
throw {
|
|
222
|
+
RE_EXN_ID: Seed_Types$ReventlessSeed.Failed,
|
|
223
|
+
_1: onTimeout(nodes),
|
|
224
|
+
Error: new Error()
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
await sleep(500);
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
return Stdlib_Option.getOr(result, []);
|
|
231
|
+
}
|
|
232
|
+
|
|
211
233
|
async function waitForIds(t, fieldName, ids, timeoutMsOpt) {
|
|
212
234
|
let timeoutMs = timeoutMsOpt !== undefined ? timeoutMsOpt : 60000;
|
|
213
235
|
let deadline = Date.now() + timeoutMs;
|
|
@@ -255,6 +277,7 @@ export {
|
|
|
255
277
|
sendAll,
|
|
256
278
|
queryAllNodes,
|
|
257
279
|
countNodes,
|
|
280
|
+
queryAllNodesUntil,
|
|
258
281
|
waitForIds,
|
|
259
282
|
}
|
|
260
283
|
/* No side effect */
|
package/src/Seed_Connect.res
CHANGED
|
@@ -1,15 +1,25 @@
|
|
|
1
1
|
// Turns endpoints plus a login function into a ready `connection`.
|
|
2
2
|
//
|
|
3
3
|
// A connection is what a data set seeds against: an authenticated GraphQL
|
|
4
|
-
// client, the upload
|
|
5
|
-
// a human label for the target. `make` is provider-agnostic — it prompts for
|
|
4
|
+
// client, the deployment's upload endpoints (empty when it serves no uploads),
|
|
5
|
+
// and a human label for the target. `make` is provider-agnostic — it prompts for
|
|
6
6
|
// credentials, calls the supplied `login` to obtain a bearer, and wires it onto
|
|
7
7
|
// the client via `useToken`. Where the bearer comes from (a local `/login`
|
|
8
8
|
// round-trip, a Cognito id token) is the caller's concern.
|
|
9
9
|
|
|
10
10
|
open Seed_Types
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
// `uploadEndpoint` is the legacy single presign service (and the local dev
|
|
13
|
+
// server's one upload route); `uploadEndpoints` maps the qualified
|
|
14
|
+
// `{plugin}.{store}` of each declared store to its own presign endpoint. Both,
|
|
15
|
+
// because a deployment may publish either or both — see `Seed_Upload.endpointFor`
|
|
16
|
+
// for the resolution between them.
|
|
17
|
+
type connection = {
|
|
18
|
+
client: Seed_Client.t,
|
|
19
|
+
uploadEndpoint: string,
|
|
20
|
+
uploadEndpoints: dict<string>,
|
|
21
|
+
label: string,
|
|
22
|
+
}
|
|
13
23
|
|
|
14
24
|
/**
|
|
15
25
|
* Prompts for credentials, calls `login` to mint a bearer, and returns a
|
|
@@ -20,23 +30,47 @@ let make = async (
|
|
|
20
30
|
~label: string,
|
|
21
31
|
~endpoint: string,
|
|
22
32
|
~uploadEndpoint: string,
|
|
33
|
+
~uploadEndpoints: dict<string>=Dict.make(),
|
|
23
34
|
~login: (~username: string, ~password: string) => promise<string>,
|
|
24
35
|
~localDefaults: bool=false,
|
|
25
36
|
): connection => {
|
|
26
|
-
// `SEED_SKIP_UPLOADS` forces the connection to carry no upload endpoint
|
|
27
|
-
// data set's upload phase no-ops the same way it does when a
|
|
28
|
-
// none. Use it to seed domain data fast, or to skip a
|
|
29
|
-
// without editing the data set. It is the single
|
|
30
|
-
// `REVENTLESS_UPLOAD_ENDPOINT` env reads as "unset" (falls back
|
|
31
|
-
// so this is the reliable way to disable uploads
|
|
32
|
-
|
|
37
|
+
// `SEED_SKIP_UPLOADS` forces the connection to carry no upload endpoint at
|
|
38
|
+
// all, so a data set's upload phase no-ops the same way it does when a
|
|
39
|
+
// deployment serves none. Use it to seed domain data fast, or to skip a
|
|
40
|
+
// broken/absent upload path without editing the data set. It is the single
|
|
41
|
+
// knob: an empty `REVENTLESS_UPLOAD_ENDPOINT` env reads as "unset" (falls back
|
|
42
|
+
// to discovery), so this is the reliable way to disable uploads — which means
|
|
43
|
+
// it has to clear the per-store map too, or a declared store keeps uploading
|
|
44
|
+
// through it.
|
|
45
|
+
let skip = Seed_Prompt.envValue("SEED_SKIP_UPLOADS")->Option.isSome
|
|
46
|
+
let uploadEndpoint = skip ? "" : uploadEndpoint
|
|
47
|
+
let uploadEndpoints = skip ? Dict.make() : uploadEndpoints
|
|
33
48
|
let (username, password) = await Seed_Prompt.credentials(~localDefaults)
|
|
34
49
|
let token = await login(~username, ~password)
|
|
35
50
|
let client = Seed_Client.make(~config={endpoint: endpoint})
|
|
36
51
|
client->Seed_Client.useToken(token)
|
|
37
|
-
{client, uploadEndpoint, label}
|
|
52
|
+
{client, uploadEndpoint, uploadEndpoints, label}
|
|
38
53
|
}
|
|
39
54
|
|
|
55
|
+
/**
|
|
56
|
+
* The endpoint an asset destined for `store` uploads through, or the reason
|
|
57
|
+
* there is none — the message to report when the upload phase skips.
|
|
58
|
+
*
|
|
59
|
+
* `store` is the qualified `{plugin}.{store}` the asset's field declares, e.g.
|
|
60
|
+
* `"Catalog.productImages"`. Resolution is `Seed_Upload.endpointFor`; naming a
|
|
61
|
+
* store the deployment does not serve falls back to the legacy single service
|
|
62
|
+
* rather than to another store's endpoint.
|
|
63
|
+
*/
|
|
64
|
+
let uploadEndpointFor = (c: connection, ~store: string): result<string, string> =>
|
|
65
|
+
switch Seed_Upload.endpointFor(
|
|
66
|
+
~store,
|
|
67
|
+
~uploadEndpoint=c.uploadEndpoint,
|
|
68
|
+
~uploadEndpoints=c.uploadEndpoints,
|
|
69
|
+
) {
|
|
70
|
+
| Some(endpoint) => Ok(endpoint)
|
|
71
|
+
| None => Error(Seed_Upload.unresolvedReason(~store, ~uploadEndpoints=c.uploadEndpoints))
|
|
72
|
+
}
|
|
73
|
+
|
|
40
74
|
/**
|
|
41
75
|
* A login function backed by an HTTP login endpoint (the local dev
|
|
42
76
|
* `/__inmemory/login` shape: POST `{username, password}` → `{token}`). Reuses
|
|
@@ -63,6 +97,10 @@ let envOr = (key: string, fallback: string): string =>
|
|
|
63
97
|
* A ready `connect` thunk for the local dev platform, with the localhost
|
|
64
98
|
* defaults baked in and overridable per endpoint (arg, then env var, then
|
|
65
99
|
* default). Empty input at the credential prompt falls back to `admin`/`admin`.
|
|
100
|
+
*
|
|
101
|
+
* No per-store map: the local dev server serves every store through its one
|
|
102
|
+
* upload route, so a data set naming a store resolves to that route by the
|
|
103
|
+
* "declares a store with no matching endpoint" row.
|
|
66
104
|
*/
|
|
67
105
|
let local = (~graphql=?, ~upload=?, ~login=?, ()): (unit => promise<connection>) => {
|
|
68
106
|
let endpoint =
|