@reventlessdev/reventless-seed 1.0.0-alpha.6 → 1.0.0-alpha.8
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 +15 -0
- package/lib/bs/.compiler.log +2 -2
- package/lib/bs/ReventlessSeed.cmi +0 -0
- package/lib/bs/ReventlessSeed.cmt +0 -0
- package/lib/bs/ReventlessSeed.mlmap +1 -1
- package/lib/bs/compiler-info.json +1 -1
- 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 -5
- 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 +4 -0
- package/lib/bs/src/Seed_Client.res.mjs +5 -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 +13 -49
- package/lib/bs/src/Seed_Connect.res.mjs +5 -26
- package/lib/bs/src/Seed_Prompt-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_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 +39 -100
- package/lib/bs/src/Seed_Upload.res.mjs +61 -93
- package/lib/bs/tests/Seed_RandomTest-ReventlessSeed.cmi +0 -0
- package/lib/bs/tests/Seed_RandomTest-ReventlessSeed.cmj +0 -0
- package/lib/bs/tests/Seed_RandomTest-ReventlessSeed.cmt +0 -0
- package/lib/bs/tests/Seed_RandomTest.ast +0 -0
- package/lib/bs/tests/Seed_RandomTest.res +160 -0
- package/lib/bs/tests/Seed_RandomTest.res.mjs +208 -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 -1
- 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 -5
- 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 +4 -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 +13 -49
- package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_RandomTest-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_RandomTest-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_RandomTest-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_RandomTest.ast +0 -0
- package/lib/ocaml/Seed_RandomTest.res +160 -0
- package/lib/ocaml/Seed_Runner-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_Runner-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 +39 -100
- package/package.json +2 -2
- package/src/Seed.res +4 -5
- package/src/Seed_Client.res +4 -0
- package/src/Seed_Client.res.mjs +5 -0
- package/src/Seed_Connect.res +13 -49
- package/src/Seed_Connect.res.mjs +5 -26
- package/src/Seed_Upload.res +39 -100
- package/src/Seed_Upload.res.mjs +61 -93
- package/tests/Seed_RandomTest.res +160 -0
- package/tests/Seed_RandomTest.res.mjs +208 -0
- 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 +0 -100
- package/lib/bs/tests/UploadEndpointResolutionTest.res.mjs +0 -59
- 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 +0 -100
- package/tests/UploadEndpointResolutionTest.res +0 -100
- package/tests/UploadEndpointResolutionTest.res.mjs +0 -59
package/src/Seed_Connect.res.mjs
CHANGED
|
@@ -6,12 +6,9 @@ import * as Seed_Client$ReventlessSeed from "./Seed_Client.res.mjs";
|
|
|
6
6
|
import * as Seed_Prompt$ReventlessSeed from "./Seed_Prompt.res.mjs";
|
|
7
7
|
import * as Seed_Upload$ReventlessSeed from "./Seed_Upload.res.mjs";
|
|
8
8
|
|
|
9
|
-
async function make(label, endpoint,
|
|
10
|
-
let uploadEndpoints = uploadEndpointsOpt !== undefined ? uploadEndpointsOpt : ({});
|
|
9
|
+
async function make(label, endpoint, login, localDefaultsOpt) {
|
|
11
10
|
let localDefaults = localDefaultsOpt !== undefined ? localDefaultsOpt : false;
|
|
12
|
-
let
|
|
13
|
-
let uploadEndpoint$1 = skip ? "" : uploadEndpoint;
|
|
14
|
-
let uploadEndpoints$1 = skip ? ({}) : uploadEndpoints;
|
|
11
|
+
let uploadsSkipped = Seed_Upload$ReventlessSeed.uploadsSkipped();
|
|
15
12
|
let match = await Seed_Prompt$ReventlessSeed.credentials(localDefaults);
|
|
16
13
|
let token = await login(match[0], match[1]);
|
|
17
14
|
let client = Seed_Client$ReventlessSeed.make({
|
|
@@ -20,27 +17,11 @@ async function make(label, endpoint, uploadEndpoint, uploadEndpointsOpt, login,
|
|
|
20
17
|
Seed_Client$ReventlessSeed.useToken(client, token);
|
|
21
18
|
return {
|
|
22
19
|
client: client,
|
|
23
|
-
|
|
24
|
-
uploadEndpoints: uploadEndpoints$1,
|
|
20
|
+
uploadsSkipped: uploadsSkipped,
|
|
25
21
|
label: label
|
|
26
22
|
};
|
|
27
23
|
}
|
|
28
24
|
|
|
29
|
-
function uploadEndpointFor(c, store) {
|
|
30
|
-
let endpoint = Seed_Upload$ReventlessSeed.endpointFor(store, c.uploadEndpoint, c.uploadEndpoints);
|
|
31
|
-
if (endpoint !== undefined) {
|
|
32
|
-
return {
|
|
33
|
-
TAG: "Ok",
|
|
34
|
-
_0: endpoint
|
|
35
|
-
};
|
|
36
|
-
} else {
|
|
37
|
-
return {
|
|
38
|
-
TAG: "Error",
|
|
39
|
-
_0: Seed_Upload$ReventlessSeed.unresolvedReason(store, c.uploadEndpoints)
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
25
|
function viaLoginEndpoint(loginEndpoint) {
|
|
45
26
|
return async (username, password) => {
|
|
46
27
|
let client = Seed_Client$ReventlessSeed.make({
|
|
@@ -66,16 +47,14 @@ function envOr(key, fallback) {
|
|
|
66
47
|
return Stdlib_Option.getOr(Seed_Prompt$ReventlessSeed.envValue(key), fallback);
|
|
67
48
|
}
|
|
68
49
|
|
|
69
|
-
function local(graphql,
|
|
50
|
+
function local(graphql, login, param) {
|
|
70
51
|
let endpoint = Stdlib_Option.getOr(graphql, Stdlib_Option.getOr(Seed_Prompt$ReventlessSeed.envValue("REVENTLESS_GRAPHQL_ENDPOINT"), "http://localhost:4000/graphql"));
|
|
71
|
-
let uploadEndpoint = Stdlib_Option.getOr(upload, Stdlib_Option.getOr(Seed_Prompt$ReventlessSeed.envValue("REVENTLESS_UPLOAD_ENDPOINT"), "http://localhost:4000/__inmemory/upload"));
|
|
72
52
|
let loginEndpoint = Stdlib_Option.getOr(login, Stdlib_Option.getOr(Seed_Prompt$ReventlessSeed.envValue("REVENTLESS_LOGIN_ENDPOINT"), "http://localhost:4000/__inmemory/login"));
|
|
73
|
-
return () => make("local", endpoint,
|
|
53
|
+
return () => make("local", endpoint, viaLoginEndpoint(loginEndpoint), true);
|
|
74
54
|
}
|
|
75
55
|
|
|
76
56
|
export {
|
|
77
57
|
make,
|
|
78
|
-
uploadEndpointFor,
|
|
79
58
|
viaLoginEndpoint,
|
|
80
59
|
envOr,
|
|
81
60
|
local,
|
package/src/Seed_Upload.res
CHANGED
|
@@ -1,32 +1,26 @@
|
|
|
1
|
-
// Seed-time asset upload against the framework's
|
|
1
|
+
// Seed-time asset upload against the framework's upload contract (route B).
|
|
2
2
|
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
// stores and the UI renders.
|
|
3
|
+
// Minting is a GraphQL mutation on the domain API — `Upload_Presign(store, fileName,
|
|
4
|
+
// contentType)` — authenticated by the same bearer the seed already uses for commands
|
|
5
|
+
// (no separate endpoint, no anonymous surface). `uploadAsset` issues it through the
|
|
6
|
+
// shared `Seed_Client`, then PUTs the bytes to the returned `uploadUrl` and resolves
|
|
7
|
+
// `Ok(storageRef)` — the same `/{prefix}/{key}` ref a command stores and the UI renders.
|
|
8
8
|
//
|
|
9
|
-
// The seed runs under Node, so
|
|
10
|
-
// absolute URL against the
|
|
11
|
-
//
|
|
12
|
-
//
|
|
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.
|
|
9
|
+
// The seed runs under Node, so a relative `uploadUrl` (the local dev server returns a
|
|
10
|
+
// same-origin `/{prefix}/{key}`) is resolved to an absolute URL against the client's
|
|
11
|
+
// endpoint origin before the PUT; AWS returns an absolute presigned S3 URL (used as-is).
|
|
12
|
+
// The PUT itself is unauthenticated — presigned on AWS, open on the local route.
|
|
18
13
|
|
|
19
14
|
type fetchInit = {method: string, headers: dict<string>, body: string}
|
|
20
15
|
type response
|
|
21
16
|
|
|
22
17
|
@val external fetch: (string, fetchInit) => promise<response> = "fetch"
|
|
23
|
-
@send external responseJson: response => promise<JSON.t> = "json"
|
|
24
18
|
@send external responseText: response => promise<string> = "text"
|
|
25
19
|
@get external responseOk: response => bool = "ok"
|
|
26
20
|
@get external responseStatus: response => int = "status"
|
|
27
21
|
|
|
28
|
-
// Resolve a possibly-relative URL against a base (WHATWG URL semantics: an
|
|
29
|
-
//
|
|
22
|
+
// Resolve a possibly-relative URL against a base (WHATWG URL semantics: an absolute
|
|
23
|
+
// `input` ignores `base`, a relative one is resolved against it).
|
|
30
24
|
type url
|
|
31
25
|
@new external makeUrl: (string, string) => url = "URL"
|
|
32
26
|
@get external urlHref: url => string = "href"
|
|
@@ -43,105 +37,50 @@ let asString = (json: JSON.t): option<string> =>
|
|
|
43
37
|
| _ => None
|
|
44
38
|
}
|
|
45
39
|
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
}
|
|
40
|
+
// Whether this run should skip uploads entirely. `SEED_SKIP_UPLOADS` seeds domain data
|
|
41
|
+
// fast, or skips a broken/absent upload path without editing the data set.
|
|
42
|
+
let uploadsSkipped = (): bool => Seed_Prompt.envValue("SEED_SKIP_UPLOADS")->Option.isSome
|
|
99
43
|
|
|
100
|
-
// A `fetch` that reports an unreachable endpoint as `Error` instead of throwing
|
|
101
|
-
// so the two-step upload can thread failures without exceptions.
|
|
44
|
+
// A `fetch` that reports an unreachable endpoint as `Error` instead of throwing.
|
|
102
45
|
let tryFetch = async (url: string, init: fetchInit): result<response, string> =>
|
|
103
46
|
try Ok(await fetch(url, init)) catch {
|
|
104
47
|
| _ => Error(`cannot reach ${url}`)
|
|
105
48
|
}
|
|
106
49
|
|
|
107
50
|
/**
|
|
108
|
-
* Uploads `bytes` under `fileName`/`contentType`
|
|
51
|
+
* Uploads `bytes` under `fileName`/`contentType` into `store` (the qualified
|
|
52
|
+
* `{plugin}.{store}` the asset's field declares, e.g. `"Catalog.productImages"`) and
|
|
109
53
|
* resolves `Ok(storageRef)` (the stored `/{prefix}/{key}` ref), or `Error(msg)`
|
|
110
|
-
* describing the step that failed.
|
|
111
|
-
*
|
|
112
|
-
* the PUT itself is unauthenticated — presigned on AWS, open locally).
|
|
54
|
+
* describing the step that failed. Mints through the domain API's `Upload_Presign`
|
|
55
|
+
* mutation on `client`, then PUTs the bytes to the returned `uploadUrl`.
|
|
113
56
|
*/
|
|
114
57
|
let uploadAsset = async (
|
|
115
|
-
~
|
|
58
|
+
~client: Seed_Client.t,
|
|
59
|
+
~store: string,
|
|
116
60
|
~bytes: string,
|
|
117
61
|
~fileName: string,
|
|
118
62
|
~contentType: string,
|
|
119
|
-
~authToken: option<string>=?,
|
|
120
63
|
): result<string, string> => {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
let
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
)
|
|
64
|
+
// GraphQL string literals: JSON-quote the argument values (JSON string escaping is a
|
|
65
|
+
// superset-safe subset of GraphQL's for these ASCII-ish inputs).
|
|
66
|
+
let q = s => JSON.stringify(JSON.Encode.string(s))
|
|
67
|
+
let query = `mutation { r: Upload_Presign(store: ${q(store)}, fileName: ${q(fileName)}, contentType: ${q(contentType)}) { uploadUrl storageRef } }`
|
|
68
|
+
|
|
69
|
+
let presign = try Ok(await Seed_Client.gql(client, ~query, ~label="Upload_Presign")) catch {
|
|
70
|
+
| Seed_Types.Failed(m) => Error(m)
|
|
71
|
+
| _ => Error("Upload_Presign: unexpected error")
|
|
72
|
+
}
|
|
131
73
|
|
|
132
|
-
switch
|
|
133
|
-
| Error(m) => Error(`presign: ${m}
|
|
134
|
-
| Ok(
|
|
135
|
-
let
|
|
136
|
-
Error(`presign failed with HTTP ${(presignRes->responseStatus)->Int.toString}: ${detail}`)
|
|
137
|
-
| Ok(presignRes) =>
|
|
138
|
-
let json = await presignRes->responseJson
|
|
74
|
+
switch presign {
|
|
75
|
+
| Error(m) => Error(`presign: ${m}`)
|
|
76
|
+
| Ok(data) =>
|
|
77
|
+
let r = data->field("r")->Option.getOr(JSON.Encode.null)
|
|
139
78
|
switch (
|
|
140
|
-
|
|
141
|
-
|
|
79
|
+
r->field("uploadUrl")->Option.flatMap(asString),
|
|
80
|
+
r->field("storageRef")->Option.flatMap(asString),
|
|
142
81
|
) {
|
|
143
82
|
| (Some(uploadUrl), Some(storageRef)) =>
|
|
144
|
-
let putUrl = urlHref(makeUrl(uploadUrl,
|
|
83
|
+
let putUrl = urlHref(makeUrl(uploadUrl, Seed_Client.endpoint(client)))
|
|
145
84
|
switch await tryFetch(
|
|
146
85
|
putUrl,
|
|
147
86
|
{method: "PUT", headers: Dict.fromArray([("content-type", contentType)]), body: bytes},
|
|
@@ -152,7 +91,7 @@ let uploadAsset = async (
|
|
|
152
91
|
let detail = await putRes->responseText
|
|
153
92
|
Error(`upload PUT failed with HTTP ${(putRes->responseStatus)->Int.toString}: ${detail}`)
|
|
154
93
|
}
|
|
155
|
-
| _ => Error("
|
|
94
|
+
| _ => Error("Upload_Presign response missing uploadUrl/storageRef")
|
|
156
95
|
}
|
|
157
96
|
}
|
|
158
97
|
}
|
package/src/Seed_Upload.res.mjs
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
2
|
|
|
3
3
|
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
4
|
+
import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
|
|
5
|
+
import * as Seed_Types$ReventlessSeed from "./Seed_Types.res.mjs";
|
|
6
|
+
import * as Seed_Client$ReventlessSeed from "./Seed_Client.res.mjs";
|
|
4
7
|
import * as Seed_Prompt$ReventlessSeed from "./Seed_Prompt.res.mjs";
|
|
5
8
|
|
|
6
9
|
function field(json, key) {
|
|
@@ -15,29 +18,8 @@ function asString(json) {
|
|
|
15
18
|
}
|
|
16
19
|
}
|
|
17
20
|
|
|
18
|
-
function
|
|
19
|
-
|
|
20
|
-
if (store !== undefined) {
|
|
21
|
-
return Stdlib_Option.orElse(uploadEndpoints[store], legacy);
|
|
22
|
-
} else {
|
|
23
|
-
return legacy;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function unresolvedReason(store, uploadEndpoints) {
|
|
28
|
-
let available = Object.keys(uploadEndpoints);
|
|
29
|
-
let match = Seed_Prompt$ReventlessSeed.envValue("SEED_SKIP_UPLOADS");
|
|
30
|
-
if (match !== undefined) {
|
|
31
|
-
return "SEED_SKIP_UPLOADS is set";
|
|
32
|
-
} else if (available.length !== 0) {
|
|
33
|
-
if (store !== undefined) {
|
|
34
|
-
return `this deployment publishes upload endpoints for ` + available.join(", ") + `, none for "` + store + `"`;
|
|
35
|
-
} else {
|
|
36
|
-
return `this deployment publishes only per-store upload endpoints (` + available.join(", ") + `) and this upload names no store`;
|
|
37
|
-
}
|
|
38
|
-
} else {
|
|
39
|
-
return "this deployment publishes no upload endpoint";
|
|
40
|
-
}
|
|
21
|
+
function uploadsSkipped() {
|
|
22
|
+
return Stdlib_Option.isSome(Seed_Prompt$ReventlessSeed.envValue("SEED_SKIP_UPLOADS"));
|
|
41
23
|
}
|
|
42
24
|
|
|
43
25
|
async function tryFetch(url, init) {
|
|
@@ -54,92 +36,78 @@ async function tryFetch(url, init) {
|
|
|
54
36
|
}
|
|
55
37
|
}
|
|
56
38
|
|
|
57
|
-
async function uploadAsset(
|
|
58
|
-
let
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
method: "POST",
|
|
77
|
-
headers: presignHeaders,
|
|
78
|
-
body: presignBody
|
|
79
|
-
});
|
|
80
|
-
if (m.TAG !== "Ok") {
|
|
39
|
+
async function uploadAsset(client, store, bytes, fileName, contentType) {
|
|
40
|
+
let query = `mutation { r: Upload_Presign(store: ` + JSON.stringify(store) + `, fileName: ` + JSON.stringify(fileName) + `, contentType: ` + JSON.stringify(contentType) + `) { uploadUrl storageRef } }`;
|
|
41
|
+
let presign;
|
|
42
|
+
try {
|
|
43
|
+
presign = {
|
|
44
|
+
TAG: "Ok",
|
|
45
|
+
_0: await Seed_Client$ReventlessSeed.gql(client, query, "Upload_Presign")
|
|
46
|
+
};
|
|
47
|
+
} catch (raw_m) {
|
|
48
|
+
let m = Primitive_exceptions.internalToException(raw_m);
|
|
49
|
+
presign = m.RE_EXN_ID === Seed_Types$ReventlessSeed.Failed ? ({
|
|
50
|
+
TAG: "Error",
|
|
51
|
+
_0: m._1
|
|
52
|
+
}) : ({
|
|
53
|
+
TAG: "Error",
|
|
54
|
+
_0: "Upload_Presign: unexpected error"
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
if (presign.TAG !== "Ok") {
|
|
81
58
|
return {
|
|
82
59
|
TAG: "Error",
|
|
83
|
-
_0: `presign: ` +
|
|
60
|
+
_0: `presign: ` + presign._0
|
|
84
61
|
};
|
|
85
62
|
}
|
|
86
|
-
let
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
});
|
|
112
|
-
if (m$1.TAG !== "Ok") {
|
|
113
|
-
return {
|
|
114
|
-
TAG: "Error",
|
|
115
|
-
_0: `upload PUT: ` + m$1._0
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
let putRes = m$1._0;
|
|
119
|
-
if (putRes.ok) {
|
|
120
|
-
return {
|
|
121
|
-
TAG: "Ok",
|
|
122
|
-
_0: match$1
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
let detail = await putRes.text();
|
|
63
|
+
let r = Stdlib_Option.getOr(field(presign._0, "r"), null);
|
|
64
|
+
let match = Stdlib_Option.flatMap(field(r, "uploadUrl"), asString);
|
|
65
|
+
let match$1 = Stdlib_Option.flatMap(field(r, "storageRef"), asString);
|
|
66
|
+
if (match === undefined) {
|
|
67
|
+
return {
|
|
68
|
+
TAG: "Error",
|
|
69
|
+
_0: "Upload_Presign response missing uploadUrl/storageRef"
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
if (match$1 === undefined) {
|
|
73
|
+
return {
|
|
74
|
+
TAG: "Error",
|
|
75
|
+
_0: "Upload_Presign response missing uploadUrl/storageRef"
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
let putUrl = new URL(match, Seed_Client$ReventlessSeed.endpoint(client)).href;
|
|
79
|
+
let m$1 = await tryFetch(putUrl, {
|
|
80
|
+
method: "PUT",
|
|
81
|
+
headers: Object.fromEntries([[
|
|
82
|
+
"content-type",
|
|
83
|
+
contentType
|
|
84
|
+
]]),
|
|
85
|
+
body: bytes
|
|
86
|
+
});
|
|
87
|
+
if (m$1.TAG !== "Ok") {
|
|
126
88
|
return {
|
|
127
89
|
TAG: "Error",
|
|
128
|
-
_0: `upload PUT
|
|
90
|
+
_0: `upload PUT: ` + m$1._0
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
let putRes = m$1._0;
|
|
94
|
+
if (putRes.ok) {
|
|
95
|
+
return {
|
|
96
|
+
TAG: "Ok",
|
|
97
|
+
_0: match$1
|
|
129
98
|
};
|
|
130
99
|
}
|
|
131
|
-
let detail
|
|
100
|
+
let detail = await putRes.text();
|
|
132
101
|
return {
|
|
133
102
|
TAG: "Error",
|
|
134
|
-
_0: `
|
|
103
|
+
_0: `upload PUT failed with HTTP ` + putRes.status.toString() + `: ` + detail
|
|
135
104
|
};
|
|
136
105
|
}
|
|
137
106
|
|
|
138
107
|
export {
|
|
139
108
|
field,
|
|
140
109
|
asString,
|
|
141
|
-
|
|
142
|
-
unresolvedReason,
|
|
110
|
+
uploadsSkipped,
|
|
143
111
|
tryFetch,
|
|
144
112
|
uploadAsset,
|
|
145
113
|
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
open JestGlobals
|
|
2
|
+
|
|
3
|
+
// `Seed_Random` exists so that two seed runs against a fresh store produce the
|
|
4
|
+
// same data — that is what makes a seeded store usable as a screenshot baseline
|
|
5
|
+
// or a comparison target. Its correctness is therefore not "looks random" but
|
|
6
|
+
// "repeats exactly, and stays inside the bounds it advertises".
|
|
7
|
+
//
|
|
8
|
+
// The suite is written around that: every case below fails for a generator that
|
|
9
|
+
// is subtly non-reproducible or that walks out of its range, and none of them
|
|
10
|
+
// would notice a change of algorithm that kept both properties.
|
|
11
|
+
|
|
12
|
+
describe("Seed_Random:", () => {
|
|
13
|
+
let take = (~seed, ~n) => {
|
|
14
|
+
let r = Seed_Random.make(~seed)
|
|
15
|
+
Array.make(~length=n, 0.0)->Array.map(_ => Seed_Random.float(r))
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe("reproducibility — the reason this module exists:", () => {
|
|
19
|
+
testSync("the same seed replays the same sequence", () =>
|
|
20
|
+
expect(take(~seed=42, ~n=25))->toEqual(take(~seed=42, ~n=25))
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
// Without this, a generator that returned a constant would satisfy the
|
|
24
|
+
// case above and nothing else in the suite would catch it.
|
|
25
|
+
testSync("different seeds diverge", () =>
|
|
26
|
+
expect(take(~seed=42, ~n=25) == take(~seed=43, ~n=25))->toBe(false)
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
testSync("a fresh generator does not continue the previous one", () => {
|
|
30
|
+
let r = Seed_Random.make(~seed=7)
|
|
31
|
+
let first = Seed_Random.float(r)
|
|
32
|
+
let _ = Seed_Random.float(r)
|
|
33
|
+
expect(Seed_Random.float(Seed_Random.make(~seed=7)))->toEqual(first)
|
|
34
|
+
})
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
describe("float stays a unit interval:", () => {
|
|
38
|
+
// mulberry32's last step is a signed 32-bit value folded back to unsigned.
|
|
39
|
+
// Get that fold wrong and roughly half the draws come out negative, which
|
|
40
|
+
// every consumer below (int, pick, sampleWeighted) silently mis-scales.
|
|
41
|
+
testSync("every draw is in [0, 1)", () =>
|
|
42
|
+
expect(take(~seed=99, ~n=500)->Array.every(f => f >= 0.0 && f < 1.0))->toBe(true)
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
testSync("and the draws are not all the same value", () => {
|
|
46
|
+
let draws = take(~seed=99, ~n=100)
|
|
47
|
+
expect(draws->Array.some(f => f != draws->Array.getUnsafe(0)))->toBe(true)
|
|
48
|
+
})
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
describe("int is inclusive on both ends, as documented:", () => {
|
|
52
|
+
let draws = (~min, ~max, ~n) => {
|
|
53
|
+
let r = Seed_Random.make(~seed=5)
|
|
54
|
+
Array.make(~length=n, 0)->Array.map(_ => Seed_Random.int(r, ~min, ~max))
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
testSync("never escapes the range", () =>
|
|
58
|
+
expect(draws(~min=3, ~max=7, ~n=500)->Array.every(i => i >= 3 && i <= 7))->toBe(true)
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
// The documented contract is "inclusive on both ends"; an off-by-one in the
|
|
62
|
+
// `max - min + 1` scaling shows up as a bound that is never drawn.
|
|
63
|
+
testSync("reaches both bounds", () => {
|
|
64
|
+
let d = draws(~min=3, ~max=7, ~n=500)
|
|
65
|
+
expect((d->Array.includes(3), d->Array.includes(7)))->toEqual((true, true))
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
testSync("a single-value range is that value", () =>
|
|
69
|
+
expect(draws(~min=4, ~max=4, ~n=10)->Array.every(i => i == 4))->toBe(true)
|
|
70
|
+
)
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
describe("pick:", () => {
|
|
74
|
+
testSync("returns None for an empty array", () =>
|
|
75
|
+
expect(Seed_Random.make(~seed=1)->Seed_Random.pick([]))->toEqual(None)
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
testSync("always lands on a member", () => {
|
|
79
|
+
let r = Seed_Random.make(~seed=2)
|
|
80
|
+
let xs = ["a", "b", "c", "d"]
|
|
81
|
+
let hits = Array.make(~length=200, "")->Array.map(_ =>
|
|
82
|
+
r->Seed_Random.pickOr(~fallback="MISS", xs)
|
|
83
|
+
)
|
|
84
|
+
expect(hits->Array.every(h => xs->Array.includes(h)))->toBe(true)
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
testSync("pickOr falls back only when there is nothing to pick", () =>
|
|
88
|
+
expect(Seed_Random.make(~seed=3)->Seed_Random.pickOr(~fallback="fallback", []))->toBe(
|
|
89
|
+
"fallback",
|
|
90
|
+
)
|
|
91
|
+
)
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
describe("sampleWeighted is without replacement:", () => {
|
|
95
|
+
let equal = xs => xs->Array.map(x => (x, 1.0))
|
|
96
|
+
|
|
97
|
+
// The documented shuffle use: equal weights, count = length. If sampling
|
|
98
|
+
// ever replaced instead of removing, duplicates would appear here.
|
|
99
|
+
testSync("count = length yields a permutation, not a multiset", () => {
|
|
100
|
+
let xs = ["a", "b", "c", "d", "e"]
|
|
101
|
+
let out = Seed_Random.make(~seed=11)->Seed_Random.sampleWeighted(equal(xs), ~count=5)
|
|
102
|
+
let sorted = out->Array.toSorted(String.compare)
|
|
103
|
+
expect((out->Array.length, sorted))->toEqual((5, ["a", "b", "c", "d", "e"]))
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
testSync("count < length returns exactly count distinct items", () => {
|
|
107
|
+
let xs = ["a", "b", "c", "d", "e"]
|
|
108
|
+
let out = Seed_Random.make(~seed=12)->Seed_Random.sampleWeighted(equal(xs), ~count=3)
|
|
109
|
+
let distinct = Set.fromArray(out)->Set.size
|
|
110
|
+
expect((out->Array.length, distinct))->toEqual((3, 3))
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
testSync("count > length clamps to the pool", () =>
|
|
114
|
+
expect(
|
|
115
|
+
Seed_Random.make(~seed=13)
|
|
116
|
+
->Seed_Random.sampleWeighted(equal(["a", "b"]), ~count=99)
|
|
117
|
+
->Array.length,
|
|
118
|
+
)->toBe(2)
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
testSync("a zero-weight entry is never chosen while others remain", () => {
|
|
122
|
+
let out =
|
|
123
|
+
Seed_Random.make(~seed=14)->Seed_Random.sampleWeighted(
|
|
124
|
+
[("never", 0.0), ("a", 1.0), ("b", 1.0)],
|
|
125
|
+
~count=2,
|
|
126
|
+
)
|
|
127
|
+
expect(out->Array.includes("never"))->toBe(false)
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
testSync("is reproducible for a given seed", () => {
|
|
131
|
+
let run = () =>
|
|
132
|
+
Seed_Random.make(~seed=15)->Seed_Random.sampleWeighted(
|
|
133
|
+
equal(["a", "b", "c", "d", "e", "f"]),
|
|
134
|
+
~count=4,
|
|
135
|
+
)
|
|
136
|
+
expect(run())->toEqual(run())
|
|
137
|
+
})
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
describe("zipfWeights skews toward the head:", () => {
|
|
141
|
+
let weights = Seed_Random.zipfWeights(["a", "b", "c", "d", "e"], ~exponent=1.1)
|
|
142
|
+
|
|
143
|
+
testSync("keeps every item, in order", () =>
|
|
144
|
+
expect(weights->Array.map(((item, _)) => item))->toEqual(["a", "b", "c", "d", "e"])
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
testSync("weight decreases monotonically with rank", () => {
|
|
148
|
+
let ws = weights->Array.map(((_, w)) => w)
|
|
149
|
+
let decreasing =
|
|
150
|
+
ws->Array.everyWithIndex((w, i) => i == 0 || w < ws->Array.getUnsafe(i - 1))
|
|
151
|
+
expect(decreasing)->toBe(true)
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
testSync("the leader is clear — first outweighs last several times over", () => {
|
|
155
|
+
let first = weights->Array.get(0)->Option.mapOr(0.0, ((_, w)) => w)
|
|
156
|
+
let last = weights->Array.get(4)->Option.mapOr(0.0, ((_, w)) => w)
|
|
157
|
+
expect(first > last *. 3.0)->toBe(true)
|
|
158
|
+
})
|
|
159
|
+
})
|
|
160
|
+
})
|