@valbuild/shared 0.148.6 → 0.149.7

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.
Files changed (70) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/client/dist/valbuild-shared-client.cjs.dev.js +2 -1
  3. package/client/dist/valbuild-shared-client.cjs.prod.js +2 -1
  4. package/client/dist/valbuild-shared-client.esm.js +2 -1
  5. package/dist/acquireTypes-50960f2c.esm.js +1801 -0
  6. package/dist/acquireTypes-de10005d.cjs.dev.js +1815 -0
  7. package/dist/acquireTypes-e9467a2a.cjs.prod.js +1815 -0
  8. package/dist/createForOfIteratorHelper-3db5d737.esm.js +121 -0
  9. package/dist/createForOfIteratorHelper-90e6a8b6.cjs.prod.js +129 -0
  10. package/dist/createForOfIteratorHelper-d86bb352.cjs.dev.js +129 -0
  11. package/dist/declarations/src/internal/ApiRoutes.d.ts +3 -3
  12. package/dist/declarations/src/internal/index.d.ts +1 -0
  13. package/dist/declarations/src/internal/selfCheck.d.ts +51 -0
  14. package/dist/declarations/src/publish/buildProposal.d.ts +106 -0
  15. package/dist/declarations/src/publish/builder.d.ts +22 -0
  16. package/dist/declarations/src/publish/callJson.d.ts +21 -0
  17. package/dist/declarations/src/publish/index.d.ts +23 -0
  18. package/dist/declarations/src/publish/jobClient.d.ts +96 -0
  19. package/dist/declarations/src/publish/loadedLayer.d.ts +50 -0
  20. package/dist/declarations/src/publish/proposalProject.d.ts +2 -0
  21. package/dist/declarations/src/publish/publishClient.d.ts +95 -0
  22. package/dist/declarations/src/publish/runStudioDeploy.d.ts +244 -0
  23. package/dist/declarations/src/publish/runStudioJob.d.ts +102 -0
  24. package/dist/declarations/src/publish/runStudioPublish.d.ts +80 -0
  25. package/dist/declarations/src/typecheck/acquireTypes.d.ts +109 -0
  26. package/dist/declarations/src/typecheck/adeptSources.d.ts +9 -0
  27. package/dist/declarations/src/typecheck/index.d.ts +20 -0
  28. package/dist/declarations/src/typecheck/linkedFileSystem.d.ts +53 -0
  29. package/dist/declarations/src/typecheck/lockedVersions.d.ts +14 -0
  30. package/dist/declarations/src/typecheck/pnpmLayout.d.ts +95 -0
  31. package/dist/declarations/src/typecheck/pnpmLockfile.d.ts +57 -0
  32. package/dist/declarations/src/typecheck/proposalTypecheck.d.ts +76 -0
  33. package/dist/declarations/src/typecheck/testing/index.d.ts +6 -0
  34. package/dist/declarations/src/typecheck/testing/nodeModulesSources.d.ts +5 -0
  35. package/dist/declarations/src/typecheck/typecheckProject.d.ts +89 -0
  36. package/dist/declarations/src/typecheck/typescriptCompiler.d.ts +52 -0
  37. package/dist/{getSitemapTree-40d76bf8.cjs.prod.js → getSitemapTree-0fa0569c.cjs.dev.js} +5 -131
  38. package/dist/{getSitemapTree-cd006a36.esm.js → getSitemapTree-5afbc3ac.esm.js} +2 -121
  39. package/dist/{getSitemapTree-a7f2cc6d.cjs.dev.js → getSitemapTree-667bcb3c.cjs.prod.js} +5 -131
  40. package/dist/proposalProject-28550146.cjs.prod.js +18 -0
  41. package/dist/proposalProject-a3d3ad76.esm.js +16 -0
  42. package/dist/proposalProject-cdc53711.cjs.dev.js +18 -0
  43. package/dist/protocol-1c13de0d.cjs.prod.js +433 -0
  44. package/dist/protocol-3b9ef786.esm.js +413 -0
  45. package/dist/protocol-d8a6efda.cjs.dev.js +433 -0
  46. package/dist/slicedToArray-62e428df.cjs.dev.js +309 -0
  47. package/dist/slicedToArray-6cd80315.esm.js +299 -0
  48. package/dist/slicedToArray-af50e684.cjs.prod.js +309 -0
  49. package/internal/dist/valbuild-shared-internal.cjs.dev.js +419 -888
  50. package/internal/dist/valbuild-shared-internal.cjs.prod.js +419 -888
  51. package/internal/dist/valbuild-shared-internal.esm.js +236 -707
  52. package/package.json +28 -5
  53. package/publish/dist/valbuild-shared-publish.cjs.d.ts +2 -0
  54. package/publish/dist/valbuild-shared-publish.cjs.dev.js +2466 -0
  55. package/publish/dist/valbuild-shared-publish.cjs.js +7 -0
  56. package/publish/dist/valbuild-shared-publish.cjs.prod.js +2466 -0
  57. package/publish/dist/valbuild-shared-publish.esm.js +2428 -0
  58. package/publish/package.json +4 -0
  59. package/typecheck/dist/valbuild-shared-typecheck.cjs.d.ts +2 -0
  60. package/typecheck/dist/valbuild-shared-typecheck.cjs.dev.js +1173 -0
  61. package/typecheck/dist/valbuild-shared-typecheck.cjs.js +7 -0
  62. package/typecheck/dist/valbuild-shared-typecheck.cjs.prod.js +1173 -0
  63. package/typecheck/dist/valbuild-shared-typecheck.esm.js +1152 -0
  64. package/typecheck/package.json +4 -0
  65. package/typecheck/testing/dist/valbuild-shared-typecheck-testing.cjs.d.ts +2 -0
  66. package/typecheck/testing/dist/valbuild-shared-typecheck-testing.cjs.dev.js +202 -0
  67. package/typecheck/testing/dist/valbuild-shared-typecheck-testing.cjs.js +7 -0
  68. package/typecheck/testing/dist/valbuild-shared-typecheck-testing.cjs.prod.js +202 -0
  69. package/typecheck/testing/dist/valbuild-shared-typecheck-testing.esm.js +177 -0
  70. package/typecheck/testing/package.json +4 -0
@@ -0,0 +1,2466 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var slicedToArray = require('../../dist/slicedToArray-af50e684.cjs.prod.js');
6
+ var createForOfIteratorHelper = require('../../dist/createForOfIteratorHelper-90e6a8b6.cjs.prod.js');
7
+ var protocol = require('../../dist/protocol-1c13de0d.cjs.prod.js');
8
+ var zod = require('zod');
9
+ var proposalProject = require('../../dist/proposalProject-28550146.cjs.prod.js');
10
+ require('zod-validation-error');
11
+
12
+ /*
13
+ * The one JSON call the Studio makes through its own origin's publish proxy,
14
+ * and the errors it throws. Apart from `publishClient.ts` (which re-exports
15
+ * all three) so that a worker can make the same call without importing the
16
+ * publish client's parsers, and `@valbuild/shared` with them.
17
+ */
18
+
19
+ /**
20
+ * Who hears of a refusal's body before it is thrown: the Studio's shell, which
21
+ * says a refusal of the project's plan with the next step (`planRefusal.ts` in
22
+ * `@valbuild/ui`). One listener; setting it again replaces it.
23
+ */
24
+ var refusalListener = null;
25
+ function onPublishRefusal(listener) {
26
+ refusalListener = listener;
27
+ }
28
+
29
+ /** The publish API refused, and said why. */
30
+ var StudioPublishError = /*#__PURE__*/function (_Error) {
31
+ function StudioPublishError(statusCode, message, body) {
32
+ var _this;
33
+ slicedToArray._classCallCheck(this, StudioPublishError);
34
+ _this = slicedToArray._callSuper(this, StudioPublishError, [message]);
35
+ createForOfIteratorHelper._defineProperty(_this, "statusCode", void 0);
36
+ createForOfIteratorHelper._defineProperty(_this, "body", void 0);
37
+ _this.name = "StudioPublishError";
38
+ _this.statusCode = statusCode;
39
+ _this.body = body;
40
+ return _this;
41
+ }
42
+ slicedToArray._inherits(StudioPublishError, _Error);
43
+ return slicedToArray._createClass(StudioPublishError);
44
+ }(/*#__PURE__*/slicedToArray._wrapNativeSuper(Error));
45
+
46
+ /**
47
+ * Was this a failure that asking again could get past?
48
+ *
49
+ * No answer at all (the request never got through) and the answers that mean
50
+ * "not now" -- a timeout, too many requests, a server or gateway error. Every
51
+ * other status is content answering, and the same request gets the same
52
+ * answer.
53
+ */
54
+ function isTransientPublishError(error) {
55
+ if (!(error instanceof StudioPublishError)) return true;
56
+ return error.statusCode >= 500 || error.statusCode === 408 || error.statusCode === 429;
57
+ }
58
+
59
+ /**
60
+ * One JSON call through this origin, with the session cookie: its body, or a
61
+ * `StudioPublishError` carrying the answer's own message.
62
+ */
63
+ function callJson(_x, _x2, _x3, _x4) {
64
+ return _callJson.apply(this, arguments);
65
+ }
66
+ function _callJson() {
67
+ _callJson = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee(fetchImpl, url, method, body) {
68
+ var res, text, parsed, _refusalListener, message;
69
+ return slicedToArray._regenerator().w(function (_context) {
70
+ while (1) switch (_context.n) {
71
+ case 0:
72
+ _context.n = 1;
73
+ return fetchImpl(url, createForOfIteratorHelper._objectSpread2({
74
+ method: method,
75
+ // The session cookie is the whole credential this end has.
76
+ credentials: "same-origin"
77
+ }, body === undefined ? {} : {
78
+ headers: {
79
+ "Content-Type": "application/json"
80
+ },
81
+ body: JSON.stringify(body)
82
+ }));
83
+ case 1:
84
+ res = _context.v;
85
+ _context.n = 2;
86
+ return res.text();
87
+ case 2:
88
+ text = _context.v;
89
+ try {
90
+ parsed = text === "" ? undefined : JSON.parse(text);
91
+ } catch (_unused) {
92
+ parsed = undefined;
93
+ }
94
+ if (res.ok) {
95
+ _context.n = 3;
96
+ break;
97
+ }
98
+ // A publish the plan does not allow (a trial's 3 publishes, a trial that
99
+ // ended): the shell says it, with the next step. See `planRefusal`.
100
+ (_refusalListener = refusalListener) === null || _refusalListener === void 0 || _refusalListener(parsed);
101
+ message = createForOfIteratorHelper._typeof(parsed) === "object" && parsed !== null && "message" in parsed && typeof parsed.message === "string" ? parsed.message : "The publish API answered ".concat(res.status, ".");
102
+ throw new StudioPublishError(res.status, message, parsed);
103
+ case 3:
104
+ return _context.a(2, parsed);
105
+ }
106
+ }, _callee);
107
+ }));
108
+ return _callJson.apply(this, arguments);
109
+ }
110
+
111
+ /** An artifact the live build has and this publish re-declares by hash. */
112
+
113
+ /** See {@link StudioPublishClient.publicFiles}. */
114
+
115
+ /** Object storage refused the bytes, or could not be reached. */
116
+ var StudioUploadError = /*#__PURE__*/function (_Error) {
117
+ function StudioUploadError(statusCode, key, message) {
118
+ var _this;
119
+ slicedToArray._classCallCheck(this, StudioUploadError);
120
+ _this = slicedToArray._callSuper(this, StudioUploadError, [message]);
121
+ createForOfIteratorHelper._defineProperty(_this, "statusCode", void 0);
122
+ createForOfIteratorHelper._defineProperty(_this, "key", void 0);
123
+ _this.name = "StudioUploadError";
124
+ _this.statusCode = statusCode;
125
+ _this.key = key;
126
+ return _this;
127
+ }
128
+ slicedToArray._inherits(StudioUploadError, _Error);
129
+ return slicedToArray._createClass(StudioUploadError);
130
+ }(/*#__PURE__*/slicedToArray._wrapNativeSuper(Error));
131
+ /**
132
+ * A slot's permission to write has run out.
133
+ *
134
+ * Its own shape because it is not a failure: the answer is to declare again for
135
+ * fresh slots, and a publish that treated it as one would give up an upload
136
+ * short of the finish.
137
+ */
138
+
139
+ /*
140
+ * One request for both halves of the answer. A client lives for one deploy,
141
+ * so this is the source as that deploy read it -- two requests could read
142
+ * two different builds' files.
143
+ */
144
+
145
+ function isExpiredSlot(error) {
146
+ return error instanceof StudioUploadError && error.statusCode === 403;
147
+ }
148
+
149
+ /**
150
+ * Where content's publish API is reached: through Val's proxy on this origin
151
+ * (`api`, the Studio), or directly (`publishApi`, `<content>/v1`: home's
152
+ * upgrade page, whose `fetchImpl` adds the project token).
153
+ */
154
+
155
+ function publishApiOf(at) {
156
+ return "publishApi" in at ? at.publishApi.replace(/\/+$/, "") : "".concat(at.api.replace(/\/+$/, ""), "/publish-api");
157
+ }
158
+ function createStudioPublishClient(options) {
159
+ var _options$fetchImpl;
160
+ var fetchImpl = (_options$fetchImpl = options.fetchImpl) !== null && _options$fetchImpl !== void 0 ? _options$fetchImpl : fetch;
161
+ var proxy = publishApiOf(options);
162
+ var call = function call(path, method, body) {
163
+ return callJson(fetchImpl, "".concat(proxy).concat(path), method, body);
164
+ };
165
+ var source = null;
166
+ var readSource = function readSource() {
167
+ return source !== null && source !== void 0 ? source : source = call("/project-source", "GET").then(protocol.parseProjectSource);
168
+ };
169
+ return {
170
+ buildTarget: function () {
171
+ var _buildTarget = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee() {
172
+ var _t;
173
+ return slicedToArray._regenerator().w(function (_context) {
174
+ while (1) switch (_context.n) {
175
+ case 0:
176
+ _t = protocol.parseBuildTarget;
177
+ _context.n = 1;
178
+ return call("/build-target", "GET");
179
+ case 1:
180
+ return _context.a(2, _t(_context.v));
181
+ }
182
+ }, _callee);
183
+ }));
184
+ function buildTarget() {
185
+ return _buildTarget.apply(this, arguments);
186
+ }
187
+ return buildTarget;
188
+ }(),
189
+ projectSource: function () {
190
+ var _projectSource = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee2() {
191
+ return slicedToArray._regenerator().w(function (_context2) {
192
+ while (1) switch (_context2.n) {
193
+ case 0:
194
+ _context2.n = 1;
195
+ return readSource();
196
+ case 1:
197
+ return _context2.a(2, _context2.v.files);
198
+ }
199
+ }, _callee2);
200
+ }));
201
+ function projectSource() {
202
+ return _projectSource.apply(this, arguments);
203
+ }
204
+ return projectSource;
205
+ }(),
206
+ publicFiles: function () {
207
+ var _publicFiles = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee3() {
208
+ var _yield$readSource, publicFiles, publicPaths;
209
+ return slicedToArray._regenerator().w(function (_context3) {
210
+ while (1) switch (_context3.n) {
211
+ case 0:
212
+ _context3.n = 1;
213
+ return readSource();
214
+ case 1:
215
+ _yield$readSource = _context3.v;
216
+ publicFiles = _yield$readSource.publicFiles;
217
+ publicPaths = _yield$readSource.publicPaths;
218
+ if (!publicFiles) {
219
+ _context3.n = 2;
220
+ break;
221
+ }
222
+ return _context3.a(2, {
223
+ carried: publicFiles
224
+ });
225
+ case 2:
226
+ if (!publicPaths) {
227
+ _context3.n = 3;
228
+ break;
229
+ }
230
+ return _context3.a(2, {
231
+ paths: publicPaths
232
+ });
233
+ case 3:
234
+ return _context3.a(2, null);
235
+ }
236
+ }, _callee3);
237
+ }));
238
+ function publicFiles() {
239
+ return _publicFiles.apply(this, arguments);
240
+ }
241
+ return publicFiles;
242
+ }(),
243
+ declare: function () {
244
+ var _declare = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee4(body) {
245
+ var _t2;
246
+ return slicedToArray._regenerator().w(function (_context4) {
247
+ while (1) switch (_context4.n) {
248
+ case 0:
249
+ _t2 = protocol.parseDeclare;
250
+ _context4.n = 1;
251
+ return call("/publish", "POST", body);
252
+ case 1:
253
+ return _context4.a(2, _t2(_context4.v));
254
+ }
255
+ }, _callee4);
256
+ }));
257
+ function declare(_x) {
258
+ return _declare.apply(this, arguments);
259
+ }
260
+ return declare;
261
+ }(),
262
+ confirmArtifacts: function () {
263
+ var _confirmArtifacts = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee5(publishId) {
264
+ var _t3;
265
+ return slicedToArray._regenerator().w(function (_context5) {
266
+ while (1) switch (_context5.n) {
267
+ case 0:
268
+ _t3 = protocol.parseArtifacts;
269
+ _context5.n = 1;
270
+ return call(publishStep(publishId, "artifacts"), "POST");
271
+ case 1:
272
+ return _context5.a(2, _t3(_context5.v));
273
+ }
274
+ }, _callee5);
275
+ }));
276
+ function confirmArtifacts(_x2) {
277
+ return _confirmArtifacts.apply(this, arguments);
278
+ }
279
+ return confirmArtifacts;
280
+ }(),
281
+ verify: function () {
282
+ var _verify = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee6(publishId) {
283
+ var _t4;
284
+ return slicedToArray._regenerator().w(function (_context6) {
285
+ while (1) switch (_context6.n) {
286
+ case 0:
287
+ _t4 = protocol.parseVerify;
288
+ _context6.n = 1;
289
+ return call(publishStep(publishId, "verify"), "POST");
290
+ case 1:
291
+ return _context6.a(2, _t4(_context6.v));
292
+ }
293
+ }, _callee6);
294
+ }));
295
+ function verify(_x3) {
296
+ return _verify.apply(this, arguments);
297
+ }
298
+ return verify;
299
+ }(),
300
+ promote: function () {
301
+ var _promote = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee7(publishId) {
302
+ var _t5;
303
+ return slicedToArray._regenerator().w(function (_context7) {
304
+ while (1) switch (_context7.n) {
305
+ case 0:
306
+ _t5 = protocol.parsePromote;
307
+ _context7.n = 1;
308
+ return call(publishStep(publishId, "promote"), "POST");
309
+ case 1:
310
+ return _context7.a(2, _t5(_context7.v));
311
+ }
312
+ }, _callee7);
313
+ }));
314
+ function promote(_x4) {
315
+ return _promote.apply(this, arguments);
316
+ }
317
+ return promote;
318
+ }(),
319
+ status: function () {
320
+ var _status = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee8(publishId) {
321
+ var _t6;
322
+ return slicedToArray._regenerator().w(function (_context8) {
323
+ while (1) switch (_context8.n) {
324
+ case 0:
325
+ _t6 = protocol.parseStatus;
326
+ _context8.n = 1;
327
+ return call(publishStep(publishId), "GET");
328
+ case 1:
329
+ return _context8.a(2, _t6(_context8.v));
330
+ }
331
+ }, _callee8);
332
+ }));
333
+ function status(_x5) {
334
+ return _status.apply(this, arguments);
335
+ }
336
+ return status;
337
+ }(),
338
+ upload: function () {
339
+ var _upload = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee9(slot, body) {
340
+ var res, _t7;
341
+ return slicedToArray._regenerator().w(function (_context9) {
342
+ while (1) switch (_context9.p = _context9.n) {
343
+ case 0:
344
+ _context9.p = 0;
345
+ _context9.n = 1;
346
+ return fetchImpl(slot.url, {
347
+ method: slot.method,
348
+ headers: slot.headers,
349
+ body: body,
350
+ /*
351
+ * A presigned URL authorises itself. Sending this origin's cookies to
352
+ * a storage host would leak a session to somewhere that has no use
353
+ * for it and no reason to be trusted with it.
354
+ */
355
+ credentials: "omit"
356
+ });
357
+ case 1:
358
+ res = _context9.v;
359
+ _context9.n = 3;
360
+ break;
361
+ case 2:
362
+ _context9.p = 2;
363
+ _t7 = _context9.v;
364
+ throw new StudioUploadError(0, slot.key, "Could not reach storage for '".concat(slot.key, "': ").concat(_t7 instanceof Error ? _t7.message : String(_t7)));
365
+ case 3:
366
+ if (res.ok) {
367
+ _context9.n = 4;
368
+ break;
369
+ }
370
+ throw new StudioUploadError(res.status, slot.key, "Storage refused '".concat(slot.key, "' with ").concat(res.status, "."));
371
+ case 4:
372
+ return _context9.a(2);
373
+ }
374
+ }, _callee9, null, [[0, 2]]);
375
+ }));
376
+ function upload(_x6, _x7) {
377
+ return _upload.apply(this, arguments);
378
+ }
379
+ return upload;
380
+ }()
381
+ };
382
+ }
383
+ var publishStep = function publishStep(publishId, step) {
384
+ return "/publish/".concat(encodeURIComponent(publishId)).concat(step ? "/".concat(step) : "");
385
+ };
386
+
387
+ /** Which step the publish is on, for the one line an editor is shown. */
388
+
389
+ /**
390
+ * How many times to declare again.
391
+ *
392
+ * Declaring mints upload slots, and a slot expires. Three rounds covers an
393
+ * upload that ran past the hour and a storage write that content did not see;
394
+ * a fourth would be the same failure a third time.
395
+ */
396
+ var DECLARE_ROUNDS = 3;
397
+ function runStudioPublish(_x) {
398
+ return _runStudioPublish.apply(this, arguments);
399
+ }
400
+
401
+ /** The problems a refusal carries, or none when its body names none. */
402
+ function _runStudioPublish() {
403
+ _runStudioPublish = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee(args) {
404
+ var client, artifacts, onPhase, bodyOf, publishId, confirmed, round, declared, slots, expired, _iterator, _step, _step$value, index, slot, body, confirmation, verified, promoted, _t, _t2, _t3;
405
+ return slicedToArray._regenerator().w(function (_context) {
406
+ while (1) switch (_context.p = _context.n) {
407
+ case 0:
408
+ client = args.client, artifacts = args.artifacts, onPhase = args.onPhase;
409
+ bodyOf = new Map(artifacts.map(function (a) {
410
+ return [a.key, a.body];
411
+ }));
412
+ publishId = null;
413
+ _context.p = 1;
414
+ confirmed = false;
415
+ round = 1;
416
+ case 2:
417
+ if (!(round <= DECLARE_ROUNDS && !confirmed)) {
418
+ _context.n = 23;
419
+ break;
420
+ }
421
+ onPhase({
422
+ kind: "declaring"
423
+ });
424
+ _context.n = 3;
425
+ return client.declare(args.declare);
426
+ case 3:
427
+ declared = _context.v;
428
+ publishId = declared.publishId;
429
+ if (!(declared.state === "live")) {
430
+ _context.n = 5;
431
+ break;
432
+ }
433
+ if (!(args.until === "confirmed")) {
434
+ _context.n = 4;
435
+ break;
436
+ }
437
+ return _context.a(2, {
438
+ status: "uploaded",
439
+ publishId: declared.publishId
440
+ });
441
+ case 4:
442
+ return _context.a(2, {
443
+ status: "already-live",
444
+ publishId: declared.publishId,
445
+ url: declared.project.siteUrl
446
+ });
447
+ case 5:
448
+ slots = declared.uploads;
449
+ expired = false;
450
+ _iterator = createForOfIteratorHelper._createForOfIteratorHelper(slots.entries());
451
+ _context.p = 6;
452
+ _iterator.s();
453
+ case 7:
454
+ if ((_step = _iterator.n()).done) {
455
+ _context.n = 13;
456
+ break;
457
+ }
458
+ _step$value = slicedToArray._slicedToArray(_step.value, 2), index = _step$value[0], slot = _step$value[1];
459
+ onPhase({
460
+ kind: "uploading",
461
+ done: index,
462
+ total: slots.length
463
+ });
464
+ body = bodyOf.get(slot.key);
465
+ if (!(body === undefined)) {
466
+ _context.n = 8;
467
+ break;
468
+ }
469
+ return _context.a(2, {
470
+ status: "failed",
471
+ publishId: publishId,
472
+ message: "Content asked for an artifact this build did not produce: '".concat(slot.key, "'."),
473
+ problems: []
474
+ });
475
+ case 8:
476
+ _context.p = 8;
477
+ _context.n = 9;
478
+ return client.upload(slot, body);
479
+ case 9:
480
+ _context.n = 12;
481
+ break;
482
+ case 10:
483
+ _context.p = 10;
484
+ _t = _context.v;
485
+ if (!isExpiredSlot(_t)) {
486
+ _context.n = 11;
487
+ break;
488
+ }
489
+ // Not a failure: the hour on the slots ran out while they were
490
+ // being used. Declaring again mints fresh ones.
491
+ expired = true;
492
+ return _context.a(3, 13);
493
+ case 11:
494
+ throw _t;
495
+ case 12:
496
+ _context.n = 7;
497
+ break;
498
+ case 13:
499
+ _context.n = 15;
500
+ break;
501
+ case 14:
502
+ _context.p = 14;
503
+ _t2 = _context.v;
504
+ _iterator.e(_t2);
505
+ case 15:
506
+ _context.p = 15;
507
+ _iterator.f();
508
+ return _context.f(15);
509
+ case 16:
510
+ if (!expired) {
511
+ _context.n = 17;
512
+ break;
513
+ }
514
+ return _context.a(3, 22);
515
+ case 17:
516
+ onPhase({
517
+ kind: "uploading",
518
+ done: slots.length,
519
+ total: slots.length
520
+ });
521
+ onPhase({
522
+ kind: "confirming"
523
+ });
524
+ _context.n = 18;
525
+ return client.confirmArtifacts(declared.publishId);
526
+ case 18:
527
+ confirmation = _context.v;
528
+ if (!(confirmation.state === "awaiting-artifacts")) {
529
+ _context.n = 20;
530
+ break;
531
+ }
532
+ if (!(round === DECLARE_ROUNDS)) {
533
+ _context.n = 19;
534
+ break;
535
+ }
536
+ return _context.a(2, {
537
+ status: "failed",
538
+ publishId: publishId,
539
+ message: "Some artifacts did not arrive as declared.",
540
+ problems: confirmation.problems
541
+ });
542
+ case 19:
543
+ return _context.a(3, 22);
544
+ case 20:
545
+ if (!(confirmation.state !== "ready")) {
546
+ _context.n = 21;
547
+ break;
548
+ }
549
+ return _context.a(2, {
550
+ status: "failed",
551
+ publishId: publishId,
552
+ message: "Content left this publish in \"".concat(confirmation.state, "\" after confirming the artifacts."),
553
+ problems: confirmation.problems
554
+ });
555
+ case 21:
556
+ confirmed = true;
557
+ case 22:
558
+ round++;
559
+ _context.n = 2;
560
+ break;
561
+ case 23:
562
+ if (!(!confirmed || publishId === null)) {
563
+ _context.n = 24;
564
+ break;
565
+ }
566
+ return _context.a(2, {
567
+ status: "failed",
568
+ publishId: publishId,
569
+ message: "The artifacts were not confirmed after ".concat(DECLARE_ROUNDS, " attempts."),
570
+ problems: []
571
+ });
572
+ case 24:
573
+ if (!(args.until === "confirmed")) {
574
+ _context.n = 25;
575
+ break;
576
+ }
577
+ return _context.a(2, {
578
+ status: "uploaded",
579
+ publishId: publishId
580
+ });
581
+ case 25:
582
+ onPhase({
583
+ kind: "verifying"
584
+ });
585
+ _context.n = 26;
586
+ return client.verify(publishId);
587
+ case 26:
588
+ verified = _context.v;
589
+ if (verified.ok) {
590
+ _context.n = 27;
591
+ break;
592
+ }
593
+ return _context.a(2, {
594
+ status: "failed",
595
+ publishId: publishId,
596
+ message: "The build was uploaded but did not render, so nothing was published.",
597
+ problems: verified.problems
598
+ });
599
+ case 27:
600
+ onPhase({
601
+ kind: "promoting"
602
+ });
603
+ _context.n = 28;
604
+ return client.promote(publishId);
605
+ case 28:
606
+ promoted = _context.v;
607
+ if (!(promoted.state !== "live")) {
608
+ _context.n = 29;
609
+ break;
610
+ }
611
+ return _context.a(2, {
612
+ status: "failed",
613
+ publishId: publishId,
614
+ message: "The site was not moved to this build: it is \"".concat(promoted.state, "\"."),
615
+ problems: []
616
+ });
617
+ case 29:
618
+ return _context.a(2, {
619
+ status: "live",
620
+ publishId: publishId,
621
+ url: promoted.url
622
+ });
623
+ case 30:
624
+ _context.p = 30;
625
+ _t3 = _context.v;
626
+ return _context.a(2, {
627
+ status: "failed",
628
+ publishId: publishId,
629
+ message: _t3 instanceof StudioPublishError || _t3 instanceof Error ? _t3.message : String(_t3),
630
+ /*
631
+ * Content refuses with `{ statusCode, message, details }`, and the
632
+ * publish routes put EVERY problem in `details`. Without them the editor
633
+ * reads "This publish cannot be declared" and nothing about why.
634
+ */
635
+ problems: _t3 instanceof StudioPublishError ? problemsOf(_t3.body) : []
636
+ });
637
+ }
638
+ }, _callee, null, [[8, 10], [6, 14, 15, 16], [1, 30]]);
639
+ }));
640
+ return _runStudioPublish.apply(this, arguments);
641
+ }
642
+ var problemsOf = function problemsOf(body) {
643
+ return createForOfIteratorHelper._typeof(body) === "object" && body !== null && "details" in body ? protocol.parseProblems(body.details) : [];
644
+ };
645
+
646
+ /**
647
+ * Has the site moved onto other dependencies since this page loaded?
648
+ *
649
+ * A managed project's Studio is served by the site's own live build, so the
650
+ * Studio on screen -- its builder, its Val -- is the version that build was
651
+ * made with. When the site is updated (Settings → Updates, in this tab or
652
+ * another), every Studio opened before that is the OLD version, and a publish
653
+ * from one would build the site with the old builder, or, if it read its build
654
+ * target before the update went live, put the old dependencies back.
655
+ *
656
+ * So the page remembers which dependency layer the live build had when it
657
+ * loaded, and a publish compares that with the live build's now. They differ
658
+ * only when somebody moved the site's dependencies in between; the answer is
659
+ * to reload, which loses nothing -- unpublished changes are the server's.
660
+ *
661
+ * Read from `/__api/head`, which the platform answers on every site without a
662
+ * credential and which names the layer (`projectVendorRev`). Anything that
663
+ * cannot answer -- not on the platform, the loader unreachable, an older
664
+ * loader -- reads as "cannot tell", and "cannot tell" never blocks a publish.
665
+ */
666
+
667
+ /**
668
+ * The layer, or `undefined` for "cannot tell". `null` is never READ -- a head
669
+ * that names no layer is "cannot tell" too, see `readLiveLayer` -- but is what
670
+ * a build target says for a project with none, which is why it is comparable.
671
+ */
672
+
673
+ function readLiveLayer() {
674
+ return _readLiveLayer.apply(this, arguments);
675
+ }
676
+
677
+ /**
678
+ * Stale only on a positive answer from both readings. A page that could not
679
+ * tell what it loaded on, or cannot tell now, publishes as before.
680
+ */
681
+ function _readLiveLayer() {
682
+ _readLiveLayer = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee() {
683
+ var fetchImpl,
684
+ res,
685
+ body,
686
+ _args = arguments;
687
+ return slicedToArray._regenerator().w(function (_context) {
688
+ while (1) switch (_context.p = _context.n) {
689
+ case 0:
690
+ fetchImpl = _args.length > 0 && _args[0] !== undefined ? _args[0] : fetch;
691
+ _context.p = 1;
692
+ _context.n = 2;
693
+ return fetchImpl("/__api/head", {
694
+ credentials: "same-origin",
695
+ cache: "no-store"
696
+ });
697
+ case 2:
698
+ res = _context.v;
699
+ if (res.ok) {
700
+ _context.n = 3;
701
+ break;
702
+ }
703
+ return _context.a(2, undefined);
704
+ case 3:
705
+ _context.n = 4;
706
+ return res.json()["catch"](function () {
707
+ return undefined;
708
+ });
709
+ case 4:
710
+ body = _context.v;
711
+ if (!(createForOfIteratorHelper._typeof(body) !== "object" || body === null || !("hash" in body))) {
712
+ _context.n = 5;
713
+ break;
714
+ }
715
+ return _context.a(2, undefined);
716
+ case 5:
717
+ return _context.a(2, "projectVendorRev" in body && typeof body.projectVendorRev === "string" ? body.projectVendorRev : undefined);
718
+ case 6:
719
+ _context.p = 6;
720
+ _context.v;
721
+ return _context.a(2, undefined);
722
+ }
723
+ }, _callee, null, [[1, 6]]);
724
+ }));
725
+ return _readLiveLayer.apply(this, arguments);
726
+ }
727
+ function layerMoved(loaded, now) {
728
+ if (loaded === undefined || now === undefined) return false;
729
+ return loaded !== now;
730
+ }
731
+ var loaded = null;
732
+
733
+ /**
734
+ * Take the reading this page is compared against. Once per page: the first
735
+ * call wins, because a later one would record the layer an update just moved
736
+ * to and make a stale page look current.
737
+ */
738
+ function rememberLoadedLayer() {
739
+ var fetchImpl = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : fetch;
740
+ loaded !== null && loaded !== void 0 ? loaded : loaded = readLiveLayer(fetchImpl);
741
+ }
742
+
743
+ /** What {@link rememberLoadedLayer} read, or `undefined` if it never ran. */
744
+ function loadedLayer() {
745
+ return loaded !== null && loaded !== void 0 ? loaded : Promise.resolve(undefined);
746
+ }
747
+
748
+ /** Has the live build's layer moved since this page loaded? */
749
+ function siteMovedSinceLoad() {
750
+ return _siteMovedSinceLoad.apply(this, arguments);
751
+ }
752
+
753
+ /** The code a deploy refuses with when this page is out of date. */
754
+ function _siteMovedSinceLoad() {
755
+ _siteMovedSinceLoad = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee2() {
756
+ var fetchImpl,
757
+ before,
758
+ _args2 = arguments,
759
+ _t2,
760
+ _t3;
761
+ return slicedToArray._regenerator().w(function (_context2) {
762
+ while (1) switch (_context2.n) {
763
+ case 0:
764
+ fetchImpl = _args2.length > 0 && _args2[0] !== undefined ? _args2[0] : fetch;
765
+ _context2.n = 1;
766
+ return loadedLayer();
767
+ case 1:
768
+ before = _context2.v;
769
+ if (!(before === undefined)) {
770
+ _context2.n = 2;
771
+ break;
772
+ }
773
+ return _context2.a(2, false);
774
+ case 2:
775
+ _t2 = layerMoved;
776
+ _t3 = before;
777
+ _context2.n = 3;
778
+ return readLiveLayer(fetchImpl);
779
+ case 3:
780
+ return _context2.a(2, _t2(_t3, _context2.v));
781
+ }
782
+ }, _callee2);
783
+ }));
784
+ return _siteMovedSinceLoad.apply(this, arguments);
785
+ }
786
+ var STUDIO_OUT_OF_DATE = "STUDIO_OUT_OF_DATE";
787
+ var STUDIO_OUT_OF_DATE_MESSAGE = "Your site was updated after this Studio was opened. Reload to publish. " + "Your changes are kept.";
788
+
789
+ /*
790
+ * Whether this page has been told it is out of date. Module state rather than
791
+ * React state, because it is found in two places that share no component -- a
792
+ * publish's pre-check and the deploy under it -- and shown in a third.
793
+ */
794
+ /**
795
+ * How many times a publish has been refused for it. A count rather than a
796
+ * flag, so a dialog dismissed once is shown again by the next refused press.
797
+ */
798
+ var refusals = 0;
799
+ var listeners = new Set();
800
+ function markStudioOutOfDate() {
801
+ refusals++;
802
+ var _iterator = createForOfIteratorHelper._createForOfIteratorHelper(listeners),
803
+ _step;
804
+ try {
805
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
806
+ var listener = _step.value;
807
+ listener();
808
+ }
809
+ } catch (err) {
810
+ _iterator.e(err);
811
+ } finally {
812
+ _iterator.f();
813
+ }
814
+ }
815
+ function studioOutOfDateRefusals() {
816
+ return refusals;
817
+ }
818
+ function subscribeStudioOutOfDate(listener) {
819
+ listeners.add(listener);
820
+ return function () {
821
+ return listeners["delete"](listener);
822
+ };
823
+ }
824
+
825
+ /** For tests: forget everything this module has learnt. */
826
+ function resetLoadedLayerForTests() {
827
+ loaded = null;
828
+ refusals = 0;
829
+ listeners.clear();
830
+ }
831
+
832
+ /**
833
+ * Where a project's route files live, if it has any.
834
+ *
835
+ * Spelled here rather than imported as `ROUTES_DIR` so this module keeps its
836
+ * one runtime reach into the builder package behind `loadBuilder` — the
837
+ * constant is four characters and the guard that says so is worth more. See
838
+ * `onlyBuilderRoot.test.ts`.
839
+ */
840
+ var ROUTES_PREFIX = "src/routes/";
841
+
842
+ /**
843
+ * Where a project's served-as-is files live, and the artifact prefix they are
844
+ * published under -- the same four letters either way, which is the contract
845
+ * (`public/favicon.ico` is requested as `/favicon.ico`). Spelled here for the
846
+ * reason `ROUTES_PREFIX` is.
847
+ */
848
+ var PUBLIC_PREFIX = "public/";
849
+
850
+ /**
851
+ * The binary files a commit wrote, as `/save` answered them.
852
+ *
853
+ * `unread` is files the server could not read back before committing. The
854
+ * build refuses over them: the site would link a file it does not serve.
855
+ */
856
+
857
+ function runStudioDeploy(_x) {
858
+ return _runStudioDeploy.apply(this, arguments);
859
+ }
860
+
861
+ /**
862
+ * The stored source with a commit's files laid over it.
863
+ *
864
+ * A new record rather than an edit of the one read, which the caller may still
865
+ * hold. Val names module files with a leading `/`; the stored record does
866
+ * not, so the slash is dropped -- two spellings of one path would build the
867
+ * old file and publish the new one beside it.
868
+ */
869
+ function _runStudioDeploy() {
870
+ _runStudioDeploy = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee2(options) {
871
+ var _options$committedFil3, _git$commit, _git, _git$branch, _git2;
872
+ var client, onPhase, failed, publishByName, builder, target, source, carried, refetched, _options$target, _options$committedBin, _options$committedBin2, _options$committedFil, _yield$Promise$all, _yield$Promise$all2, readTarget, readSource, readPublic, unread, _options$committedFil2, build, git, wiredSource, _ref8, _options$branch, _yield$builder$bakedG, _options$committedBin3, _options$committedBin4, branch, wired, fileBased, generateRouteTree, binaries, extraInputs, input, name, held, live, artifacts, _live, declared, published, _t, _t2, _t3, _t4, _t5, _t6, _t7, _t8, _t9, _t0, _t1, _t10, _t11, _t12, _t13, _t14, _t15, _t16, _t17, _t18, _t19, _t20, _t21;
873
+ return slicedToArray._regenerator().w(function (_context2) {
874
+ while (1) switch (_context2.p = _context2.n) {
875
+ case 0:
876
+ client = options.client, onPhase = options.onPhase;
877
+ failed = function failed(message) {
878
+ return {
879
+ status: "failed",
880
+ message: message,
881
+ problems: []
882
+ };
883
+ };
884
+ /*
885
+ * The build content already holds under this name, published as any build
886
+ * is -- or `null` when content holds none, or cannot be asked by name (one
887
+ * from before the question existed refuses an empty declaration), and the
888
+ * build has to be made.
889
+ *
890
+ * `null` only for a refusal of the DECLARE: once content has taken the
891
+ * build, what happens to it is that publish's outcome, a failure included.
892
+ * Building it again would not change it.
893
+ */
894
+ publishByName = /*#__PURE__*/function () {
895
+ var _ref7 = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee(name, at) {
896
+ var _at$git$commit, _at$git, _at$git$branch, _at$git2;
897
+ var published;
898
+ return slicedToArray._regenerator().w(function (_context) {
899
+ while (1) switch (_context.n) {
900
+ case 0:
901
+ _context.n = 1;
902
+ return runStudioPublish(createForOfIteratorHelper._objectSpread2({
903
+ client: client,
904
+ artifacts: [],
905
+ declare: {
906
+ buildHash: name,
907
+ commit: (_at$git$commit = (_at$git = at.git) === null || _at$git === void 0 ? void 0 : _at$git.commit) !== null && _at$git$commit !== void 0 ? _at$git$commit : null,
908
+ branch: (_at$git$branch = (_at$git2 = at.git) === null || _at$git2 === void 0 ? void 0 : _at$git2.branch) !== null && _at$git$branch !== void 0 ? _at$git$branch : null,
909
+ layerRev: at.layerRev,
910
+ // The build's, which content has: this tab built nothing to ask.
911
+ linksOwnCss: null,
912
+ artifacts: [],
913
+ byReference: true
914
+ },
915
+ onPhase: onPhase
916
+ }, options.until !== undefined ? {
917
+ until: options.until
918
+ } : {}));
919
+ case 1:
920
+ published = _context.v;
921
+ if (!(published.status === "failed" && published.publishId === null)) {
922
+ _context.n = 2;
923
+ break;
924
+ }
925
+ return _context.a(2, null);
926
+ case 2:
927
+ return _context.a(2, asDeployResult(published));
928
+ }
929
+ }, _callee);
930
+ }));
931
+ return function publishByName(_x5, _x6) {
932
+ return _ref7.apply(this, arguments);
933
+ };
934
+ }();
935
+ _context2.p = 1;
936
+ onPhase({
937
+ kind: "getting-ready"
938
+ });
939
+ _context2.n = 2;
940
+ return options.loadBuilder();
941
+ case 2:
942
+ builder = _context2.v;
943
+ _context2.n = 4;
944
+ break;
945
+ case 3:
946
+ _context2.p = 3;
947
+ _t = _context2.v;
948
+ return _context2.a(2, failed(messageOf$2(_t)));
949
+ case 4:
950
+ _context2.p = 4;
951
+ onPhase({
952
+ kind: "reading"
953
+ });
954
+ /*
955
+ * Together, because they are one answer with two halves and neither is
956
+ * useful alone -- and because the round trip is the cost, not the work.
957
+ */
958
+ _context2.n = 5;
959
+ return Promise.all([(_options$target = options.target) !== null && _options$target !== void 0 ? _options$target : client.buildTarget(), client.projectSource(), client.publicFiles()]);
960
+ case 5:
961
+ _yield$Promise$all = _context2.v;
962
+ _yield$Promise$all2 = slicedToArray._slicedToArray(_yield$Promise$all, 3);
963
+ readTarget = _yield$Promise$all2[0];
964
+ readSource = _yield$Promise$all2[1];
965
+ readPublic = _yield$Promise$all2[2];
966
+ if (!(readSource === null)) {
967
+ _context2.n = 6;
968
+ break;
969
+ }
970
+ return _context2.a(2, failed("This project has no published source to build from. Publish it once " + "from a checkout before editing it here."));
971
+ case 6:
972
+ if (!(readTarget.project.rev === null)) {
973
+ _context2.n = 7;
974
+ break;
975
+ }
976
+ return _context2.a(2, failed("This project has no dependency layer yet, and one cannot be built " + "from the browser. Publish it once from a checkout, which builds it."));
977
+ case 7:
978
+ if (!(options.target === undefined && layerMoved(options.loadedLayer, readTarget.project.rev))) {
979
+ _context2.n = 8;
980
+ break;
981
+ }
982
+ return _context2.a(2, {
983
+ status: "failed",
984
+ message: STUDIO_OUT_OF_DATE_MESSAGE,
985
+ problems: [{
986
+ code: STUDIO_OUT_OF_DATE,
987
+ message: STUDIO_OUT_OF_DATE_MESSAGE
988
+ }]
989
+ });
990
+ case 8:
991
+ if (!(readPublic === null || "paths" in readPublic && options.fetchPublicFile === undefined)) {
992
+ _context2.n = 9;
993
+ break;
994
+ }
995
+ return _context2.a(2, failed("The Studio cannot tell which images and other public files this " + "site serves, so publishing would remove them. Publish it once from " + "a checkout with `val publish`, and the Studio can publish it from " + "then on."));
996
+ case 9:
997
+ unread = (_options$committedBin = (_options$committedBin2 = options.committedBinaryFiles) === null || _options$committedBin2 === void 0 ? void 0 : _options$committedBin2.unread) !== null && _options$committedBin !== void 0 ? _options$committedBin : [];
998
+ if (!(unread.length > 0)) {
999
+ _context2.n = 10;
1000
+ break;
1001
+ }
1002
+ return _context2.a(2, failed("Your changes are saved, but ".concat(unread.join(", "), " could not be read ") + "back to build with, and the site would link a file it does not " + "serve. Try publishing again."));
1003
+ case 10:
1004
+ target = readTarget;
1005
+ source = withCommittedFiles(readSource, (_options$committedFil = options.committedFiles) !== null && _options$committedFil !== void 0 ? _options$committedFil : null);
1006
+ if (!("carried" in readPublic)) {
1007
+ _context2.n = 11;
1008
+ break;
1009
+ }
1010
+ carried = readPublic.carried;
1011
+ refetched = {};
1012
+ _context2.n = 13;
1013
+ break;
1014
+ case 11:
1015
+ /*
1016
+ * Content has no record of the live build -- a project cloned from a
1017
+ * template seed -- but the loader knows what it serves, and this page is
1018
+ * ON the site. So the files come from the site and go into the build;
1019
+ * content holds their hashes after this publish and the next one
1020
+ * carries them without fetching.
1021
+ */
1022
+ carried = [];
1023
+ _context2.n = 12;
1024
+ return refetchPublicFiles(readPublic.paths, (_options$committedFil2 = options.committedFiles) !== null && _options$committedFil2 !== void 0 ? _options$committedFil2 : null, options.fetchPublicFile);
1025
+ case 12:
1026
+ refetched = _context2.v;
1027
+ case 13:
1028
+ _context2.n = 15;
1029
+ break;
1030
+ case 14:
1031
+ _context2.p = 14;
1032
+ _t2 = _context2.v;
1033
+ return _context2.a(2, failed(messageOf$2(_t2)));
1034
+ case 15:
1035
+ _context2.p = 15;
1036
+ onPhase({
1037
+ kind: "building"
1038
+ });
1039
+ /*
1040
+ * The branch the last build was wired at, which is the only place one can
1041
+ * be read from here -- `/stat` does not carry one and neither does
1042
+ * `/save`. `null` when the record names none, which is a project that has
1043
+ * only ever published commitlessly; content then answers from the
1044
+ * project's own chain, where the answer always came from for a project
1045
+ * whose content service is its store of record.
1046
+ *
1047
+ * The pair goes in BEFORE the build, and into the record published back as
1048
+ * source, so the next publish reads it back and the two never disagree
1049
+ * about which commit this build is wired at.
1050
+ */
1051
+ if (!((_options$branch = options.branch) !== null && _options$branch !== void 0)) {
1052
+ _context2.n = 16;
1053
+ break;
1054
+ }
1055
+ _t4 = _options$branch;
1056
+ _context2.n = 21;
1057
+ break;
1058
+ case 16:
1059
+ _context2.n = 17;
1060
+ return builder.bakedGit(source);
1061
+ case 17:
1062
+ _t6 = _yield$builder$bakedG = _context2.v;
1063
+ _t5 = _t6 === null;
1064
+ if (_t5) {
1065
+ _context2.n = 18;
1066
+ break;
1067
+ }
1068
+ _t5 = _yield$builder$bakedG === void 0;
1069
+ case 18:
1070
+ if (!_t5) {
1071
+ _context2.n = 19;
1072
+ break;
1073
+ }
1074
+ _t7 = void 0;
1075
+ _context2.n = 20;
1076
+ break;
1077
+ case 19:
1078
+ _t7 = _yield$builder$bakedG.branch;
1079
+ case 20:
1080
+ _t4 = _t7;
1081
+ case 21:
1082
+ _t8 = _ref8 = _t4;
1083
+ _t3 = _t8 !== null;
1084
+ if (!_t3) {
1085
+ _context2.n = 22;
1086
+ break;
1087
+ }
1088
+ _t3 = _ref8 !== void 0;
1089
+ case 22:
1090
+ if (!_t3) {
1091
+ _context2.n = 23;
1092
+ break;
1093
+ }
1094
+ _t9 = _ref8;
1095
+ _context2.n = 24;
1096
+ break;
1097
+ case 23:
1098
+ _t9 = null;
1099
+ case 24:
1100
+ branch = _t9;
1101
+ git = options.commit === null || branch === null ? null : {
1102
+ commit: options.commit,
1103
+ branch: branch
1104
+ };
1105
+ _context2.n = 25;
1106
+ return builder.rebakeGit(source, git, options.job);
1107
+ case 25:
1108
+ wired = _context2.v;
1109
+ wiredSource = wired;
1110
+ fileBased = Object.keys(wired).some(function (path) {
1111
+ return path.startsWith(ROUTES_PREFIX);
1112
+ });
1113
+ /*
1114
+ * Read out before the branch, so the guard below and the call are the same
1115
+ * narrowing rather than two that agree by inspection.
1116
+ */
1117
+ generateRouteTree = options.generateRouteTree;
1118
+ if (!(fileBased && generateRouteTree === undefined)) {
1119
+ _context2.n = 26;
1120
+ break;
1121
+ }
1122
+ return _context2.a(2, failed("This project's pages are files under src/routes, and building those " + "needs a route generator this page was not given. Publish it from a " + "checkout, or mount the Studio in a deployment that supplies one."));
1123
+ case 26:
1124
+ binaries = splitBinaryFiles((_options$committedBin3 = (_options$committedBin4 = options.committedBinaryFiles) === null || _options$committedBin4 === void 0 ? void 0 : _options$committedBin4.files) !== null && _options$committedBin3 !== void 0 ? _options$committedBin3 : {});
1125
+ /*
1126
+ * What this build ships that it is not built from, so the build's name
1127
+ * (`buildInputId`) changes with it: the live build's public files carried
1128
+ * over by sha, and -- below -- the live stylesheet in place of an empty one.
1129
+ */
1130
+ extraInputs = Object.fromEntries(carried.map(function (_ref9) {
1131
+ var key = _ref9.key,
1132
+ sha256 = _ref9.sha256;
1133
+ return ["carried:".concat(key), sha256];
1134
+ }));
1135
+ if (!(fileBased && generateRouteTree !== undefined)) {
1136
+ _context2.n = 28;
1137
+ break;
1138
+ }
1139
+ _context2.n = 27;
1140
+ return generateRouteTree(wired);
1141
+ case 27:
1142
+ _t0 = _context2.v;
1143
+ _context2.n = 29;
1144
+ break;
1145
+ case 28:
1146
+ _t0 = wired;
1147
+ case 29:
1148
+ _t1 = _t0;
1149
+ _t10 = createForOfIteratorHelper._objectSpread2(createForOfIteratorHelper._objectSpread2({}, refetched), binaries.publicFiles);
1150
+ _t11 = binaries.assets;
1151
+ _t12 = wired;
1152
+ _t13 = target;
1153
+ _t14 = extraInputs;
1154
+ input = {
1155
+ files: _t1,
1156
+ publicFiles: _t10,
1157
+ assets: _t11,
1158
+ projectSource: _t12,
1159
+ target: _t13,
1160
+ extraInputs: _t14
1161
+ };
1162
+ _context2.n = 30;
1163
+ return builder.buildInputId(input)["catch"](function () {
1164
+ return null;
1165
+ });
1166
+ case 30:
1167
+ name = _context2.v;
1168
+ if (!(name !== null)) {
1169
+ _context2.n = 33;
1170
+ break;
1171
+ }
1172
+ _context2.n = 31;
1173
+ return publishByName(name, {
1174
+ git: git,
1175
+ layerRev: target.project.rev
1176
+ });
1177
+ case 31:
1178
+ held = _context2.v;
1179
+ if (!(held !== null)) {
1180
+ _context2.n = 32;
1181
+ break;
1182
+ }
1183
+ return _context2.a(2, held);
1184
+ case 32:
1185
+ onPhase({
1186
+ kind: "building"
1187
+ });
1188
+ case 33:
1189
+ _context2.n = 34;
1190
+ return builder.buildUserApp(input);
1191
+ case 34:
1192
+ build = _context2.v;
1193
+ if (!(build.cssCode === "" && options.liveStylesheet)) {
1194
+ _context2.n = 37;
1195
+ break;
1196
+ }
1197
+ _context2.n = 35;
1198
+ return options.liveStylesheet();
1199
+ case 35:
1200
+ live = _context2.v;
1201
+ if (!(live !== "")) {
1202
+ _context2.n = 37;
1203
+ break;
1204
+ }
1205
+ _t15 = createForOfIteratorHelper._objectSpread2;
1206
+ _t16 = createForOfIteratorHelper._objectSpread2({}, build);
1207
+ _t17 = {};
1208
+ _t18 = live;
1209
+ _context2.n = 36;
1210
+ return builder.buildInputId(createForOfIteratorHelper._objectSpread2(createForOfIteratorHelper._objectSpread2({}, input), {}, {
1211
+ extraInputs: createForOfIteratorHelper._objectSpread2(createForOfIteratorHelper._objectSpread2({}, extraInputs), {}, {
1212
+ liveStylesheet: live
1213
+ })
1214
+ }));
1215
+ case 36:
1216
+ _t19 = _context2.v;
1217
+ build = _t15(_t16, _t17, {
1218
+ cssCode: _t18,
1219
+ hash: _t19
1220
+ });
1221
+ case 37:
1222
+ _context2.n = 39;
1223
+ break;
1224
+ case 38:
1225
+ _context2.p = 38;
1226
+ _t20 = _context2.v;
1227
+ return _context2.a(2, failed(messageOf$2(_t20)));
1228
+ case 39:
1229
+ _context2.p = 39;
1230
+ _context2.n = 40;
1231
+ return builder.publishArtifacts(build, {
1232
+ projectSource: wiredSource
1233
+ });
1234
+ case 40:
1235
+ artifacts = _context2.v;
1236
+ _context2.n = 42;
1237
+ break;
1238
+ case 41:
1239
+ _context2.p = 41;
1240
+ _t21 = _context2.v;
1241
+ return _context2.a(2, failed(messageOf$2(_t21)));
1242
+ case 42:
1243
+ if (!(options.target === undefined && options.liveLayer !== undefined)) {
1244
+ _context2.n = 44;
1245
+ break;
1246
+ }
1247
+ _context2.n = 43;
1248
+ return options.liveLayer()["catch"](function () {
1249
+ return undefined;
1250
+ });
1251
+ case 43:
1252
+ _live = _context2.v;
1253
+ if (!layerMoved(options.loadedLayer, _live)) {
1254
+ _context2.n = 44;
1255
+ break;
1256
+ }
1257
+ return _context2.a(2, {
1258
+ status: "failed",
1259
+ message: STUDIO_OUT_OF_DATE_MESSAGE,
1260
+ problems: [{
1261
+ code: STUDIO_OUT_OF_DATE,
1262
+ message: STUDIO_OUT_OF_DATE_MESSAGE
1263
+ }]
1264
+ });
1265
+ case 44:
1266
+ declared = [].concat(slicedToArray._toConsumableArray(artifacts.map(function (_ref0) {
1267
+ var key = _ref0.key,
1268
+ sha256 = _ref0.sha256,
1269
+ bytes = _ref0.bytes;
1270
+ return {
1271
+ key: key,
1272
+ sha256: sha256,
1273
+ bytes: bytes
1274
+ };
1275
+ })), slicedToArray._toConsumableArray(carriedInto(artifacts, carried, (_options$committedFil3 = options.committedFiles) !== null && _options$committedFil3 !== void 0 ? _options$committedFil3 : null)));
1276
+ _context2.n = 45;
1277
+ return runStudioPublish(createForOfIteratorHelper._objectSpread2({
1278
+ client: client,
1279
+ artifacts: artifacts,
1280
+ declare: {
1281
+ buildHash: build.hash,
1282
+ /*
1283
+ * What went into the build, not what the caller asked for. A declare
1284
+ * naming a commit the bundle was not wired at labels a build as
1285
+ * something it is not.
1286
+ */
1287
+ commit: (_git$commit = (_git = git) === null || _git === void 0 ? void 0 : _git.commit) !== null && _git$commit !== void 0 ? _git$commit : null,
1288
+ branch: (_git$branch = (_git2 = git) === null || _git2 === void 0 ? void 0 : _git2.branch) !== null && _git$branch !== void 0 ? _git$branch : null,
1289
+ layerRev: target.project.rev,
1290
+ // The base layer this build was compiled against, so the platform can
1291
+ // refuse it if it has been deployed since.
1292
+ baseRev: build.vendorRev,
1293
+ linksOwnCss: build.linksOwnCss,
1294
+ artifacts: declared
1295
+ },
1296
+ onPhase: onPhase
1297
+ }, options.until !== undefined ? {
1298
+ until: options.until
1299
+ } : {}));
1300
+ case 45:
1301
+ published = _context2.v;
1302
+ return _context2.a(2, asDeployResult(published));
1303
+ }
1304
+ }, _callee2, null, [[39, 41], [15, 38], [4, 14], [1, 3]]);
1305
+ }));
1306
+ return _runStudioDeploy.apply(this, arguments);
1307
+ }
1308
+ function withCommittedFiles(source, committed) {
1309
+ if (committed === null) return source;
1310
+ var out = createForOfIteratorHelper._objectSpread2({}, source);
1311
+ for (var _i = 0, _Object$entries = Object.entries(committed); _i < _Object$entries.length; _i++) {
1312
+ var _Object$entries$_i = slicedToArray._slicedToArray(_Object$entries[_i], 2),
1313
+ path = _Object$entries$_i[0],
1314
+ content = _Object$entries$_i[1];
1315
+ var key = path.replace(/^\/+/, "");
1316
+ if (content === null) delete out[key];else out[key] = content;
1317
+ }
1318
+ return out;
1319
+ }
1320
+
1321
+ /**
1322
+ * A commit's binary files, as the build takes them: `public/...` served as is,
1323
+ * everything else an asset an import can reach. Keys lose the leading `/`
1324
+ * Val's paths carry, as in {@link withCommittedFiles}.
1325
+ */
1326
+ function splitBinaryFiles(files) {
1327
+ var publicFiles = {};
1328
+ var assets = {};
1329
+ for (var _i2 = 0, _Object$entries2 = Object.entries(files); _i2 < _Object$entries2.length; _i2++) {
1330
+ var _Object$entries2$_i = slicedToArray._slicedToArray(_Object$entries2[_i2], 2),
1331
+ path = _Object$entries2$_i[0],
1332
+ base64 = _Object$entries2$_i[1];
1333
+ var key = path.replace(/^\/+/, "");
1334
+ if (key.startsWith(PUBLIC_PREFIX)) publicFiles[key] = base64;else assets[key] = base64;
1335
+ }
1336
+ return {
1337
+ publicFiles: publicFiles,
1338
+ assets: assets
1339
+ };
1340
+ }
1341
+
1342
+ /**
1343
+ * The live site's public files, fetched to be built with, as `public/<path>`.
1344
+ *
1345
+ * All of them but the ones the commit deleted -- a replaced one is overridden
1346
+ * by the commit's own bytes where the two are merged. A file that cannot be
1347
+ * fetched fails the publish by name: leaving it out is the hole this exists
1348
+ * to prevent.
1349
+ */
1350
+ function refetchPublicFiles(_x2, _x3, _x4) {
1351
+ return _refetchPublicFiles.apply(this, arguments);
1352
+ }
1353
+ /**
1354
+ * The live build's public files this build keeps, re-declared by hash.
1355
+ *
1356
+ * All of them, except the ones this build produced itself (a file the commit
1357
+ * replaced) and the ones the commit deleted. Content holds the bytes already,
1358
+ * so this uploads nothing -- and if it has lost one, it asks for it, and
1359
+ * `runStudioPublish` refuses rather than publishing a hole.
1360
+ */
1361
+ function _refetchPublicFiles() {
1362
+ _refetchPublicFiles = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee3(paths, committed, fetchPublicFile) {
1363
+ var deleted, out, _iterator, _step, path, key, _t22, _t23;
1364
+ return slicedToArray._regenerator().w(function (_context3) {
1365
+ while (1) switch (_context3.p = _context3.n) {
1366
+ case 0:
1367
+ deleted = new Set(Object.entries(committed !== null && committed !== void 0 ? committed : {}).filter(function (_ref1) {
1368
+ var _ref10 = slicedToArray._slicedToArray(_ref1, 2),
1369
+ content = _ref10[1];
1370
+ return content === null;
1371
+ }).map(function (_ref11) {
1372
+ var _ref12 = slicedToArray._slicedToArray(_ref11, 1),
1373
+ path = _ref12[0];
1374
+ return path.replace(/^\/+/, "");
1375
+ }));
1376
+ out = {};
1377
+ _iterator = createForOfIteratorHelper._createForOfIteratorHelper(paths);
1378
+ _context3.p = 1;
1379
+ _iterator.s();
1380
+ case 2:
1381
+ if ((_step = _iterator.n()).done) {
1382
+ _context3.n = 8;
1383
+ break;
1384
+ }
1385
+ path = _step.value;
1386
+ key = PUBLIC_PREFIX + path.replace(/^\/+/, "");
1387
+ if (!deleted.has(key)) {
1388
+ _context3.n = 3;
1389
+ break;
1390
+ }
1391
+ return _context3.a(3, 7);
1392
+ case 3:
1393
+ if (!(fetchPublicFile === undefined)) {
1394
+ _context3.n = 4;
1395
+ break;
1396
+ }
1397
+ throw new Error("No way to read /".concat(path, " from the site to build with."));
1398
+ case 4:
1399
+ _context3.p = 4;
1400
+ _context3.n = 5;
1401
+ return fetchPublicFile(path.replace(/^\/+/, ""));
1402
+ case 5:
1403
+ out[key] = _context3.v;
1404
+ _context3.n = 7;
1405
+ break;
1406
+ case 6:
1407
+ _context3.p = 6;
1408
+ _t22 = _context3.v;
1409
+ throw new Error("The site's /".concat(path, " could not be read to build with, and leaving it ") + "out would remove it: ".concat(messageOf$2(_t22)), {
1410
+ cause: _t22
1411
+ });
1412
+ case 7:
1413
+ _context3.n = 2;
1414
+ break;
1415
+ case 8:
1416
+ _context3.n = 10;
1417
+ break;
1418
+ case 9:
1419
+ _context3.p = 9;
1420
+ _t23 = _context3.v;
1421
+ _iterator.e(_t23);
1422
+ case 10:
1423
+ _context3.p = 10;
1424
+ _iterator.f();
1425
+ return _context3.f(10);
1426
+ case 11:
1427
+ return _context3.a(2, out);
1428
+ }
1429
+ }, _callee3, null, [[4, 6], [1, 9, 10, 11]]);
1430
+ }));
1431
+ return _refetchPublicFiles.apply(this, arguments);
1432
+ }
1433
+ function carriedInto(built, carried, committed) {
1434
+ var produced = new Set(built.map(function (_ref) {
1435
+ var key = _ref.key;
1436
+ return key;
1437
+ }));
1438
+ var deleted = new Set(Object.entries(committed !== null && committed !== void 0 ? committed : {}).filter(function (_ref2) {
1439
+ var _ref3 = slicedToArray._slicedToArray(_ref2, 2),
1440
+ content = _ref3[1];
1441
+ return content === null;
1442
+ }).map(function (_ref4) {
1443
+ var _ref5 = slicedToArray._slicedToArray(_ref4, 1),
1444
+ path = _ref5[0];
1445
+ return path.replace(/^\/+/, "");
1446
+ }));
1447
+ return carried.filter(function (_ref6) {
1448
+ var key = _ref6.key;
1449
+ return key.startsWith(PUBLIC_PREFIX) && !produced.has(key) && !deleted.has(key);
1450
+ });
1451
+ }
1452
+ var asDeployResult = function asDeployResult(published) {
1453
+ switch (published.status) {
1454
+ case "live":
1455
+ return {
1456
+ status: "live",
1457
+ url: published.url
1458
+ };
1459
+ case "already-live":
1460
+ return {
1461
+ status: "already-live",
1462
+ url: published.url
1463
+ };
1464
+ case "uploaded":
1465
+ return {
1466
+ status: "uploaded",
1467
+ publishId: published.publishId
1468
+ };
1469
+ case "failed":
1470
+ return {
1471
+ status: "failed",
1472
+ message: published.message,
1473
+ problems: published.problems
1474
+ };
1475
+ }
1476
+ };
1477
+
1478
+ /**
1479
+ * A thrown thing, as a sentence.
1480
+ *
1481
+ * `String(error)` on an Error gives `Error: ...`, which reads as a stack trace
1482
+ * leaking into the page. The message alone is what was written for a person.
1483
+ */
1484
+ var messageOf$2 = function messageOf(error) {
1485
+ return error instanceof Error ? error.message : String(error);
1486
+ };
1487
+
1488
+ var _excluded = ["buildable"];
1489
+
1490
+ /**
1491
+ * Publishing as a queued job, as a Studio tab speaks it.
1492
+ *
1493
+ * valbuild/home's docs/app-mode.md, "Publishing is a queued job", is the
1494
+ * design: a press is a REQUEST that returns at once; the tab that pressed runs
1495
+ * the job it is given -- prepare, build, upload -- and content runs verify and
1496
+ * the seal. Everything goes through this deployment: the job routes through
1497
+ * the publish proxy, and the prepare through this server's own route, which is
1498
+ * the one place a job's sources can be computed.
1499
+ */
1500
+
1501
+ /** What the tab builds a job from: `/api/val/publish-job-prepare`'s answer. */
1502
+
1503
+ function createStudioJobClient(options) {
1504
+ var _options$fetchImpl;
1505
+ var fetchImpl = (_options$fetchImpl = options.fetchImpl) !== null && _options$fetchImpl !== void 0 ? _options$fetchImpl : fetch;
1506
+ var publishApi = publishApiOf(options);
1507
+ var proposalsApi = "proposalsApi" in options ? options.proposalsApi.replace(/\/+$/, "") : "".concat(options.api.replace(/\/+$/, ""), "/proposals-api");
1508
+ var proxy = function proxy(path, method, body) {
1509
+ return callJson(fetchImpl, "".concat(publishApi).concat(path), method, body);
1510
+ };
1511
+ var job = function job(jobId, step) {
1512
+ return "/publish-jobs/".concat(encodeURIComponent(jobId), "/").concat(step);
1513
+ };
1514
+ return {
1515
+ press: function () {
1516
+ var _press = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee(requestId, tab) {
1517
+ var _t;
1518
+ return slicedToArray._regenerator().w(function (_context) {
1519
+ while (1) switch (_context.n) {
1520
+ case 0:
1521
+ _t = protocol.parsePress;
1522
+ _context.n = 1;
1523
+ return proxy("/publish-requests", "POST", {
1524
+ requestId: requestId,
1525
+ tab: tab
1526
+ });
1527
+ case 1:
1528
+ return _context.a(2, _t(_context.v, "POST /v1/publish-requests"));
1529
+ }
1530
+ }, _callee);
1531
+ }));
1532
+ function press(_x, _x2) {
1533
+ return _press.apply(this, arguments);
1534
+ }
1535
+ return press;
1536
+ }(),
1537
+ pressMerge: function () {
1538
+ var _pressMerge = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee2(proposal, requestId, tab, anyway) {
1539
+ var _t2;
1540
+ return slicedToArray._regenerator().w(function (_context2) {
1541
+ while (1) switch (_context2.n) {
1542
+ case 0:
1543
+ _t2 = protocol.parsePress;
1544
+ _context2.n = 1;
1545
+ return callJson(fetchImpl, "".concat(proposalsApi, "/").concat(encodeURIComponent(proposal), "/merge"), "POST", createForOfIteratorHelper._objectSpread2({
1546
+ requestId: requestId,
1547
+ tab: tab
1548
+ }, anyway !== undefined && anyway.length > 0 ? {
1549
+ anyway: anyway
1550
+ } : {}));
1551
+ case 1:
1552
+ return _context2.a(2, _t2(_context2.v, "POST /v1/proposals/{name}/merge"));
1553
+ }
1554
+ }, _callee2);
1555
+ }));
1556
+ function pressMerge(_x3, _x4, _x5, _x6) {
1557
+ return _pressMerge.apply(this, arguments);
1558
+ }
1559
+ return pressMerge;
1560
+ }(),
1561
+ tryAgain: function () {
1562
+ var _tryAgain = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee3(requestId, tab) {
1563
+ var _t3;
1564
+ return slicedToArray._regenerator().w(function (_context3) {
1565
+ while (1) switch (_context3.n) {
1566
+ case 0:
1567
+ _t3 = protocol.parsePress;
1568
+ _context3.n = 1;
1569
+ return proxy("/publish-requests/try-again", "POST", {
1570
+ requestId: requestId,
1571
+ tab: tab
1572
+ });
1573
+ case 1:
1574
+ return _context3.a(2, _t3(_context3.v, "POST /v1/publish-requests/try-again"));
1575
+ }
1576
+ }, _callee3);
1577
+ }));
1578
+ function tryAgain(_x7, _x8) {
1579
+ return _tryAgain.apply(this, arguments);
1580
+ }
1581
+ return tryAgain;
1582
+ }(),
1583
+ requestStatus: function () {
1584
+ var _requestStatus = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee4(requestId) {
1585
+ var _t4;
1586
+ return slicedToArray._regenerator().w(function (_context4) {
1587
+ while (1) switch (_context4.n) {
1588
+ case 0:
1589
+ _t4 = protocol.parseRequestStatus;
1590
+ _context4.n = 1;
1591
+ return proxy("/publish-requests/".concat(encodeURIComponent(requestId)), "GET");
1592
+ case 1:
1593
+ return _context4.a(2, _t4(_context4.v).request);
1594
+ }
1595
+ }, _callee4);
1596
+ }));
1597
+ function requestStatus(_x9) {
1598
+ return _requestStatus.apply(this, arguments);
1599
+ }
1600
+ return requestStatus;
1601
+ }(),
1602
+ next: function () {
1603
+ var _next = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee5(tab) {
1604
+ var _t5;
1605
+ return slicedToArray._regenerator().w(function (_context5) {
1606
+ while (1) switch (_context5.n) {
1607
+ case 0:
1608
+ _t5 = protocol.parseJob;
1609
+ _context5.n = 1;
1610
+ return proxy("/publish-jobs/next", "POST", {
1611
+ tab: tab
1612
+ });
1613
+ case 1:
1614
+ return _context5.a(2, _t5(_context5.v, "POST /v1/publish-jobs/next").job);
1615
+ }
1616
+ }, _callee5);
1617
+ }));
1618
+ function next(_x0) {
1619
+ return _next.apply(this, arguments);
1620
+ }
1621
+ return next;
1622
+ }(),
1623
+ prepare: function () {
1624
+ var _prepare = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee6(tabJob, tab) {
1625
+ var merged, answer, _t6;
1626
+ return slicedToArray._regenerator().w(function (_context6) {
1627
+ while (1) switch (_context6.n) {
1628
+ case 0:
1629
+ if (!tabJob.patches.some(isMergeChange)) {
1630
+ _context6.n = 2;
1631
+ break;
1632
+ }
1633
+ _t6 = MergePrepareAnswer;
1634
+ _context6.n = 1;
1635
+ return proxy(job(tabJob.id, "merge-prepare"), "POST", {
1636
+ tab: tab
1637
+ });
1638
+ case 1:
1639
+ merged = _t6.parse.call(_t6, _context6.v);
1640
+ return _context6.a(2, createForOfIteratorHelper._objectSpread2({
1641
+ job: merged.job,
1642
+ sourceFiles: merged.sourceFiles,
1643
+ binaryFiles: {},
1644
+ binaryFilesUnread: [],
1645
+ branch: null,
1646
+ buildable: true
1647
+ }, merged.target !== undefined ? {
1648
+ target: protocol.parseBuildTarget(merged.target)
1649
+ } : {}));
1650
+ case 2:
1651
+ if ("api" in options) {
1652
+ _context6.n = 3;
1653
+ break;
1654
+ }
1655
+ throw new Error("Only a merge's job can be run here: a site change's job needs the Studio.");
1656
+ case 3:
1657
+ _context6.n = 4;
1658
+ return callJson(fetchImpl, "".concat(options.api.replace(/\/+$/, ""), "/publish-job-prepare"), "POST", {
1659
+ jobId: tabJob.id,
1660
+ tab: tab,
1661
+ patchIds: tabJob.patches
1662
+ });
1663
+ case 4:
1664
+ answer = _context6.v;
1665
+ return _context6.a(2, parsePreparedJob(answer));
1666
+ }
1667
+ }, _callee6);
1668
+ }));
1669
+ function prepare(_x1, _x10) {
1670
+ return _prepare.apply(this, arguments);
1671
+ }
1672
+ return prepare;
1673
+ }(),
1674
+ step: function () {
1675
+ var _step = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee7(jobId, body) {
1676
+ var _t7;
1677
+ return slicedToArray._regenerator().w(function (_context7) {
1678
+ while (1) switch (_context7.n) {
1679
+ case 0:
1680
+ _t7 = protocol.parseJob;
1681
+ _context7.n = 1;
1682
+ return proxy(job(jobId, "steps"), "POST", body);
1683
+ case 1:
1684
+ return _context7.a(2, _t7(_context7.v, "POST /v1/publish-jobs/{id}/steps").job);
1685
+ }
1686
+ }, _callee7);
1687
+ }));
1688
+ function step(_x11, _x12) {
1689
+ return _step.apply(this, arguments);
1690
+ }
1691
+ return step;
1692
+ }(),
1693
+ renew: function () {
1694
+ var _renew = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee8(jobId, tab) {
1695
+ var _t8;
1696
+ return slicedToArray._regenerator().w(function (_context8) {
1697
+ while (1) switch (_context8.n) {
1698
+ case 0:
1699
+ _t8 = protocol.parseRenew;
1700
+ _context8.n = 1;
1701
+ return proxy(job(jobId, "renew"), "POST", {
1702
+ tab: tab
1703
+ });
1704
+ case 1:
1705
+ return _context8.a(2, _t8(_context8.v).renewed);
1706
+ }
1707
+ }, _callee8);
1708
+ }));
1709
+ function renew(_x13, _x14) {
1710
+ return _renew.apply(this, arguments);
1711
+ }
1712
+ return renew;
1713
+ }(),
1714
+ cancel: function () {
1715
+ var _cancel = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee9(jobId) {
1716
+ var _t9;
1717
+ return slicedToArray._regenerator().w(function (_context9) {
1718
+ while (1) switch (_context9.n) {
1719
+ case 0:
1720
+ _t9 = protocol.parseCancel;
1721
+ _context9.n = 1;
1722
+ return proxy(job(jobId, "cancel"), "POST", {});
1723
+ case 1:
1724
+ return _context9.a(2, _t9(_context9.v).cancelled);
1725
+ }
1726
+ }, _callee9);
1727
+ }));
1728
+ function cancel(_x15) {
1729
+ return _cancel.apply(this, arguments);
1730
+ }
1731
+ return cancel;
1732
+ }(),
1733
+ discard: function () {
1734
+ var _discard = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee0(jobId) {
1735
+ var _t0;
1736
+ return slicedToArray._regenerator().w(function (_context0) {
1737
+ while (1) switch (_context0.n) {
1738
+ case 0:
1739
+ _t0 = protocol.parseDiscard;
1740
+ _context0.n = 1;
1741
+ return proxy(job(jobId, "discard"), "POST", {});
1742
+ case 1:
1743
+ return _context0.a(2, _t0(_context0.v).stillHeld);
1744
+ }
1745
+ }, _callee0);
1746
+ }));
1747
+ function discard(_x16) {
1748
+ return _discard.apply(this, arguments);
1749
+ }
1750
+ return discard;
1751
+ }(),
1752
+ newestCiRun: function () {
1753
+ var _newestCiRun = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee1() {
1754
+ var _t1;
1755
+ return slicedToArray._regenerator().w(function (_context1) {
1756
+ while (1) switch (_context1.n) {
1757
+ case 0:
1758
+ _t1 = protocol.parseNewestCiRun;
1759
+ _context1.n = 1;
1760
+ return proxy("/ci-runs/newest", "GET");
1761
+ case 1:
1762
+ return _context1.a(2, _t1(_context1.v).run);
1763
+ }
1764
+ }, _callee1);
1765
+ }));
1766
+ function newestCiRun() {
1767
+ return _newestCiRun.apply(this, arguments);
1768
+ }
1769
+ return newestCiRun;
1770
+ }()
1771
+ };
1772
+ }
1773
+
1774
+ /** A job's change that merges a proposal: `merge:<name>` (content's `mergeChange`). */
1775
+ function isMergeChange(change) {
1776
+ return change.startsWith("merge:");
1777
+ }
1778
+
1779
+ /** `/publish-jobs/:id/merge-prepare`'s answer: the merge's files, from content. */
1780
+ var MergePrepareAnswer = zod.z.object({
1781
+ job: zod.z.object({
1782
+ id: zod.z.string(),
1783
+ step: zod.z["enum"](["prepare", "build", "upload"]).nullable(),
1784
+ base: zod.z.string().nullable(),
1785
+ patches: zod.z.array(zod.z.string()),
1786
+ /*
1787
+ * A merge that changes only content goes live on the live build, and
1788
+ * this tab builds nothing (valbuild/home `docs/hosted-publishing.md`,
1789
+ * step 8). Dropped by an object that did not name it, and the tab then
1790
+ * read a job nobody builds as one it had lost.
1791
+ */
1792
+ contentOnly: zod.z.literal(true).optional()
1793
+ }).nullable(),
1794
+ sourceFiles: zod.z.record(zod.z.string(), zod.z.string().nullable()),
1795
+ /*
1796
+ * The proposal's own build target, when it changed the dependencies. Read
1797
+ * by `parseBuildTarget`, the one reading of a build target there is.
1798
+ */
1799
+ target: zod.z.unknown().optional()
1800
+ });
1801
+
1802
+ /** `/api/val/publish-job-prepare`'s answer, checked: it crossed a network. */
1803
+ var preparedJob = zod.z.object({
1804
+ sourceFiles: zod.z.record(zod.z.string(), zod.z.string().nullable()),
1805
+ binaryFiles: zod.z.record(zod.z.string(), zod.z.string()),
1806
+ binaryFilesUnread: zod.z.array(zod.z.string()),
1807
+ branch: zod.z.string().nullable(),
1808
+ buildable: zod.z["boolean"]().optional()
1809
+ });
1810
+ function parsePreparedJob(body) {
1811
+ var job = protocol.parseJob(body, "POST /api/val/publish-job-prepare").job;
1812
+ var parsed = preparedJob.safeParse(body);
1813
+ if (!parsed.success) throw new Error("The server's publish prepare answered with something this Studio cannot read.");
1814
+ var _parsed$data = parsed.data,
1815
+ buildable = _parsed$data.buildable,
1816
+ rest = protocol._objectWithoutProperties(_parsed$data, _excluded);
1817
+ return createForOfIteratorHelper._objectSpread2(createForOfIteratorHelper._objectSpread2({
1818
+ job: job
1819
+ }, rest), {}, {
1820
+ buildable: buildable !== null && buildable !== void 0 ? buildable : true
1821
+ });
1822
+ }
1823
+
1824
+ /**
1825
+ * A publish job, as the tab that holds it runs it: prepare, build, upload --
1826
+ * and then it is content's (valbuild/home, docs/app-mode.md, "Publishing is a
1827
+ * queued job"). Content runs verify and the seal, and "Live" arrives on the
1828
+ * websocket, so a tab that closes after its upload costs nothing.
1829
+ *
1830
+ * The tab holds the job by a lease, renewed on a timer while it works. A
1831
+ * renewal answered `false` means the job is no longer this tab's -- its lease
1832
+ * lapsed and its requests went back to the queue, or it was cancelled -- and
1833
+ * the tab stops: another tab, or this one's next job, will build it.
1834
+ *
1835
+ * Every step is reported by name, so a report retried after a lost answer
1836
+ * does nothing twice, and a failure is reported as the step's: content counts
1837
+ * the attempts and, after the last, fails the job with Try again.
1838
+ */
1839
+
1840
+ /** How often the tab renews its lease. Content's job lease is 30 s. */
1841
+ var RENEW_EVERY_MS = 10000;
1842
+ function runStudioJob(_x) {
1843
+ return _runStudioJob.apply(this, arguments);
1844
+ }
1845
+ function _runStudioJob() {
1846
+ _runStudioJob = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee(options) {
1847
+ var _options$renewEveryMs;
1848
+ var client, job, tab, onPhase, lost, renewing, lostResult, _options$canBuild, _options$canBuild2, _options$canBuild3, _options$canBuild4, prepared, message, handed, deployed, _message, built, uploaded, _t;
1849
+ return slicedToArray._regenerator().w(function (_context) {
1850
+ while (1) switch (_context.p = _context.n) {
1851
+ case 0:
1852
+ client = options.client, job = options.job, tab = options.tab, onPhase = options.onPhase;
1853
+ lost = false;
1854
+ renewing = setInterval(function () {
1855
+ client.renew(job.id, tab).then(function (renewed) {
1856
+ if (!renewed) lost = true;
1857
+ },
1858
+ // A renewal that did not get through is not a lost job: the lease is
1859
+ // 30 s and the next one may.
1860
+ function () {});
1861
+ }, (_options$renewEveryMs = options.renewEveryMs) !== null && _options$renewEveryMs !== void 0 ? _options$renewEveryMs : RENEW_EVERY_MS);
1862
+ lostResult = {
1863
+ status: "lost",
1864
+ jobId: job.id
1865
+ };
1866
+ _context.p = 1;
1867
+ onPhase({
1868
+ kind: "preparing"
1869
+ });
1870
+ _context.p = 2;
1871
+ _context.n = 3;
1872
+ return client.prepare(job, tab);
1873
+ case 3:
1874
+ prepared = _context.v;
1875
+ _context.n = 6;
1876
+ break;
1877
+ case 4:
1878
+ _context.p = 4;
1879
+ _t = _context.v;
1880
+ /*
1881
+ * 502 is content answering the prepare with a failure, which content
1882
+ * has already counted as an attempt at the step. Anything else never
1883
+ * reached it, so the tab reports it.
1884
+ */
1885
+ message = messageOf$1(_t);
1886
+ if (_t instanceof StudioPublishError && _t.statusCode === 502) {
1887
+ _context.n = 5;
1888
+ break;
1889
+ }
1890
+ _context.n = 5;
1891
+ return client.step(job.id, {
1892
+ tab: tab,
1893
+ step: "prepare",
1894
+ ok: false,
1895
+ message: message
1896
+ })["catch"](function () {
1897
+ return null;
1898
+ });
1899
+ case 5:
1900
+ return _context.a(2, {
1901
+ status: "failed",
1902
+ jobId: job.id,
1903
+ message: message
1904
+ });
1905
+ case 6:
1906
+ if (!(lost || prepared.job === null)) {
1907
+ _context.n = 7;
1908
+ break;
1909
+ }
1910
+ return _context.a(2, lostResult);
1911
+ case 7:
1912
+ if (!(prepared.job.step === null)) {
1913
+ _context.n = 8;
1914
+ break;
1915
+ }
1916
+ return _context.a(2, createForOfIteratorHelper._objectSpread2({
1917
+ status: "handed-off",
1918
+ jobId: job.id,
1919
+ built: false
1920
+ }, prepared.job.contentOnly ? {
1921
+ contentOnly: true
1922
+ } : {}));
1923
+ case 8:
1924
+ if (!(prepared.job.step !== "build")) {
1925
+ _context.n = 9;
1926
+ break;
1927
+ }
1928
+ return _context.a(2, lostResult);
1929
+ case 9:
1930
+ if (!(options.buildsElsewhere && !((_options$canBuild = (_options$canBuild2 = options.canBuild) === null || _options$canBuild2 === void 0 ? void 0 : _options$canBuild2.call(options)) !== null && _options$canBuild !== void 0 ? _options$canBuild : true))) {
1931
+ _context.n = 10;
1932
+ break;
1933
+ }
1934
+ return _context.a(2, {
1935
+ status: "needs-builder",
1936
+ jobId: job.id
1937
+ });
1938
+ case 10:
1939
+ if (!(!prepared.buildable || !((_options$canBuild3 = (_options$canBuild4 = options.canBuild) === null || _options$canBuild4 === void 0 ? void 0 : _options$canBuild4.call(options)) !== null && _options$canBuild3 !== void 0 ? _options$canBuild3 : true))) {
1940
+ _context.n = 13;
1941
+ break;
1942
+ }
1943
+ onPhase({
1944
+ kind: "handing-off"
1945
+ });
1946
+ _context.n = 11;
1947
+ return client.step(job.id, {
1948
+ tab: tab,
1949
+ step: "build",
1950
+ ok: true,
1951
+ noBuild: true
1952
+ });
1953
+ case 11:
1954
+ handed = _context.v;
1955
+ if (!(lost || handed === null || handed.step !== null)) {
1956
+ _context.n = 12;
1957
+ break;
1958
+ }
1959
+ return _context.a(2, lostResult);
1960
+ case 12:
1961
+ return _context.a(2, {
1962
+ status: "handed-off",
1963
+ jobId: job.id,
1964
+ built: false
1965
+ });
1966
+ case 13:
1967
+ _context.n = 14;
1968
+ return options.deploy(prepared, function (phase) {
1969
+ return onPhase({
1970
+ kind: "deploying",
1971
+ phase: phase
1972
+ });
1973
+ });
1974
+ case 14:
1975
+ deployed = _context.v;
1976
+ if (!lost) {
1977
+ _context.n = 15;
1978
+ break;
1979
+ }
1980
+ return _context.a(2, lostResult);
1981
+ case 15:
1982
+ if (!(deployed.status !== "uploaded")) {
1983
+ _context.n = 17;
1984
+ break;
1985
+ }
1986
+ _message = deployed.status === "failed" ? deployed.message : "The build went further than a job's build should.";
1987
+ /*
1988
+ * With the reason: if the build fails for good, content's failure is
1989
+ * what every Studio shows, and without it that said "build failed 3
1990
+ * times" and nothing else.
1991
+ */
1992
+ _context.n = 16;
1993
+ return client.step(job.id, {
1994
+ tab: tab,
1995
+ step: "build",
1996
+ ok: false,
1997
+ message: _message
1998
+ })["catch"](function () {
1999
+ return null;
2000
+ });
2001
+ case 16:
2002
+ return _context.a(2, {
2003
+ status: "failed",
2004
+ jobId: job.id,
2005
+ message: _message
2006
+ });
2007
+ case 17:
2008
+ onPhase({
2009
+ kind: "handing-off"
2010
+ });
2011
+ _context.n = 18;
2012
+ return client.step(job.id, {
2013
+ tab: tab,
2014
+ step: "build",
2015
+ ok: true,
2016
+ build: deployed.publishId
2017
+ });
2018
+ case 18:
2019
+ built = _context.v;
2020
+ if (!(built === null || built.step !== "upload")) {
2021
+ _context.n = 19;
2022
+ break;
2023
+ }
2024
+ return _context.a(2, lostResult);
2025
+ case 19:
2026
+ _context.n = 20;
2027
+ return client.step(job.id, {
2028
+ tab: tab,
2029
+ step: "upload",
2030
+ ok: true
2031
+ });
2032
+ case 20:
2033
+ uploaded = _context.v;
2034
+ if (!(lost || uploaded === null || uploaded.step !== null)) {
2035
+ _context.n = 21;
2036
+ break;
2037
+ }
2038
+ return _context.a(2, lostResult);
2039
+ case 21:
2040
+ return _context.a(2, {
2041
+ status: "handed-off",
2042
+ jobId: job.id,
2043
+ built: true
2044
+ });
2045
+ case 22:
2046
+ _context.p = 22;
2047
+ clearInterval(renewing);
2048
+ return _context.f(22);
2049
+ case 23:
2050
+ return _context.a(2);
2051
+ }
2052
+ }, _callee, null, [[2, 4], [1,, 22, 23]]);
2053
+ }));
2054
+ return _runStudioJob.apply(this, arguments);
2055
+ }
2056
+ var messageOf$1 = function messageOf(error) {
2057
+ return error instanceof Error ? error.message : String(error);
2058
+ };
2059
+
2060
+ /**
2061
+ * How many times one job is built in a row. Content counts the attempts at a
2062
+ * step and fails the job after its last (`MAX_STEP_ATTEMPTS`, 3), so this is
2063
+ * only a backstop for failures content never heard about.
2064
+ */
2065
+ var MAX_RUNS = 5;
2066
+
2067
+ /**
2068
+ * Run a job until this tab's part of it is over: handed off, lost, or failed
2069
+ * for good.
2070
+ *
2071
+ * A failed step is counted by content, which keeps the job at that step for
2072
+ * the tab to run again -- until its last attempt, when it fails the job.
2073
+ * Whether it is still this tab's is the renewal's answer, so a failure is
2074
+ * followed by one, and a `true` runs it again.
2075
+ */
2076
+ function runJobToEnd(_x2) {
2077
+ return _runJobToEnd.apply(this, arguments);
2078
+ }
2079
+ function _runJobToEnd() {
2080
+ _runJobToEnd = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee2(options) {
2081
+ var result, runs, again;
2082
+ return slicedToArray._regenerator().w(function (_context2) {
2083
+ while (1) switch (_context2.n) {
2084
+ case 0:
2085
+ result = {
2086
+ status: "lost",
2087
+ jobId: options.job.id
2088
+ };
2089
+ runs = 0;
2090
+ case 1:
2091
+ if (!(runs < MAX_RUNS)) {
2092
+ _context2.n = 6;
2093
+ break;
2094
+ }
2095
+ _context2.n = 2;
2096
+ return options.run()["catch"](function (error) {
2097
+ return {
2098
+ status: "failed",
2099
+ jobId: options.job.id,
2100
+ message: messageOf$1(error)
2101
+ };
2102
+ });
2103
+ case 2:
2104
+ result = _context2.v;
2105
+ if (!(result.status !== "failed")) {
2106
+ _context2.n = 3;
2107
+ break;
2108
+ }
2109
+ return _context2.a(2, result);
2110
+ case 3:
2111
+ _context2.n = 4;
2112
+ return options.client.renew(options.job.id, options.tab)["catch"](function () {
2113
+ return false;
2114
+ });
2115
+ case 4:
2116
+ again = _context2.v;
2117
+ if (again) {
2118
+ _context2.n = 5;
2119
+ break;
2120
+ }
2121
+ return _context2.a(2, result);
2122
+ case 5:
2123
+ runs++;
2124
+ _context2.n = 1;
2125
+ break;
2126
+ case 6:
2127
+ return _context2.a(2, result);
2128
+ }
2129
+ }, _callee2);
2130
+ }));
2131
+ return _runJobToEnd.apply(this, arguments);
2132
+ }
2133
+
2134
+ /*
2135
+ * Building a proposal's staged files and serving the build at its address:
2136
+ * valbuild/home `docs/hosted-publishing.md`, step 7. The Studio builds one
2137
+ * from its proposals bar, and home's upgrade page builds an upgrade's; each
2138
+ * reaches content its own way, so the client is the caller's.
2139
+ */
2140
+
2141
+ /** A proposal as content answers it, as far as a build reads it. */
2142
+
2143
+ /** The calls a build of a proposal makes. An error carries content's `status`. */
2144
+
2145
+ /** The HTTP status an error carries, if it is an answer at all. */
2146
+ function statusOf(error) {
2147
+ return createForOfIteratorHelper._typeof(error) === "object" && error !== null && "status" in error && typeof error.status === "number" ? error.status : null;
2148
+ }
2149
+ function messageOf(error) {
2150
+ return error instanceof Error ? error.message : String(error);
2151
+ }
2152
+
2153
+ /** What a build of the proposal came to. */
2154
+
2155
+ /**
2156
+ * Where a build of the proposal is, around the deploy: the deploy's own steps
2157
+ * are the Studio's deploy state (`useStudioDeployState`), and these are the
2158
+ * parts before and after it.
2159
+ *
2160
+ * - `reading`: the proposal's files and the target to build them on.
2161
+ * - `deploying`: the deploy is running; its state says which step.
2162
+ * - `serving`: content is putting the build up at the proposal's address.
2163
+ * - `waiting`: that request did not come back -- a gateway gave up on it --
2164
+ * and content is asked until it says the proposal is built.
2165
+ */
2166
+
2167
+ /**
2168
+ * Files that never go into a build: lockfiles, at any depth. The same list as
2169
+ * content's `NEVER_SHIPPED` (valbuild/home `content/src/val/dependencyFiles.ts`).
2170
+ *
2171
+ * A merge builds from the site's source with the proposal's files over it,
2172
+ * without these (`shippedFiles` there). The proposal's own build has to be
2173
+ * made of the same files, or the two are named differently: a merge then
2174
+ * never finds the proposal's build held, and builds the whole site again --
2175
+ * which is every upgrade, since an upgrade stages a `pnpm-lock.yaml`.
2176
+ */
2177
+ var NEVER_SHIPPED = new Set(["pnpm-lock.yaml", "pnpm-workspace.yaml", "package-lock.json", "npm-shrinkwrap.json", "yarn.lock", "bun.lock", "bun.lockb"]);
2178
+ function withoutLockfiles(files) {
2179
+ return Object.fromEntries(Object.entries(files).filter(function (_ref) {
2180
+ var _ref2 = slicedToArray._slicedToArray(_ref, 1),
2181
+ path = _ref2[0];
2182
+ return !NEVER_SHIPPED.has(path.slice(path.lastIndexOf("/") + 1));
2183
+ }));
2184
+ }
2185
+
2186
+ /**
2187
+ * Whether serving got no answer from content rather than a refusal: the
2188
+ * connection failed, or a gateway stopped waiting -- Cloudflare's 52x (524 is
2189
+ * "no answer in 100 seconds"), and the 502-504 of anything else in between.
2190
+ * Content goes on with the request either way, so the build may well be
2191
+ * served.
2192
+ */
2193
+ function gatewayGaveUp(error) {
2194
+ // No answer at all: `fetch` rejects with a TypeError when the connection
2195
+ // fails or is cut. Anything else is a failure with words of its own.
2196
+ if (error instanceof TypeError) return true;
2197
+ var status = statusOf(error);
2198
+ if (status === null) return false;
2199
+ return status === 502 || status === 503 || status === 504 || status >= 520 && status <= 527;
2200
+ }
2201
+
2202
+ /** How long, and how often, content is asked whether it served the build. */
2203
+ var SERVE_WAIT_MS = 4 * 60000;
2204
+ var SERVE_POLL_MS = 5000;
2205
+
2206
+ /**
2207
+ * Build the proposal's staged files and serve the build at its address.
2208
+ *
2209
+ * The files and what they are for (`inputs`) come in one answer, so the build
2210
+ * names exactly what it was made of; content refuses it if they were staged
2211
+ * again meanwhile. The tab builds as for a publish and stops once the build is
2212
+ * uploaded -- nothing goes live -- and content hands it to the platform.
2213
+ *
2214
+ * Serving can take longer than a gateway waits for an answer, and content
2215
+ * finishes it all the same. So a gateway that gives up is not a failure: the
2216
+ * proposal is read until it says it is built from these inputs, and served
2217
+ * once more only if it never does.
2218
+ */
2219
+ function buildProposal(_x) {
2220
+ return _buildProposal.apply(this, arguments);
2221
+ }
2222
+ function _buildProposal() {
2223
+ _buildProposal = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee3(deps) {
2224
+ var _deps$now, _deps$sleep, _deps$onStep;
2225
+ var now, sleep, step, read, _read, files, staged, inputs, target, asked, built, serve, servedNow, builtOf, since, served, _t2, _t3, _t4, _t5, _t6, _t7;
2226
+ return slicedToArray._regenerator().w(function (_context3) {
2227
+ while (1) switch (_context3.p = _context3.n) {
2228
+ case 0:
2229
+ now = (_deps$now = deps.now) !== null && _deps$now !== void 0 ? _deps$now : Date.now;
2230
+ sleep = (_deps$sleep = deps.sleep) !== null && _deps$sleep !== void 0 ? _deps$sleep : function (ms) {
2231
+ return new Promise(function (resolve) {
2232
+ return setTimeout(resolve, ms);
2233
+ });
2234
+ };
2235
+ step = (_deps$onStep = deps.onStep) !== null && _deps$onStep !== void 0 ? _deps$onStep : function () {};
2236
+ if (deps.canBuildHere()) {
2237
+ _context3.n = 1;
2238
+ break;
2239
+ }
2240
+ return _context3.a(2, {
2241
+ status: "failed",
2242
+ message: "This browser cannot build in this tab. Open the proposal in Chrome, Edge or Firefox and build it there"
2243
+ });
2244
+ case 1:
2245
+ step({
2246
+ kind: "reading"
2247
+ });
2248
+ _context3.p = 2;
2249
+ _context3.n = 3;
2250
+ return deps.client.files(deps.name);
2251
+ case 3:
2252
+ read = _context3.v;
2253
+ _context3.n = 5;
2254
+ break;
2255
+ case 4:
2256
+ _context3.p = 4;
2257
+ _t2 = _context3.v;
2258
+ return _context3.a(2, {
2259
+ status: "failed",
2260
+ message: messageOf(_t2)
2261
+ });
2262
+ case 5:
2263
+ _read = read, files = _read.files, staged = _read.staged;
2264
+ if (!(staged.inputs === null)) {
2265
+ _context3.n = 6;
2266
+ break;
2267
+ }
2268
+ return _context3.a(2, {
2269
+ status: "nothing-staged"
2270
+ });
2271
+ case 6:
2272
+ if (staged.buildRequired) {
2273
+ _context3.n = 7;
2274
+ break;
2275
+ }
2276
+ return _context3.a(2, {
2277
+ status: "already-built",
2278
+ hash: null
2279
+ });
2280
+ case 7:
2281
+ inputs = staged.inputs;
2282
+ _context3.p = 8;
2283
+ _context3.n = 9;
2284
+ return deps.client.buildTarget(deps.name);
2285
+ case 9:
2286
+ asked = _context3.v;
2287
+ target = asked.layer === "own" ? asked.target : undefined;
2288
+ _context3.n = 11;
2289
+ break;
2290
+ case 10:
2291
+ _context3.p = 10;
2292
+ _t3 = _context3.v;
2293
+ if (!(statusOf(_t3) !== 404)) {
2294
+ _context3.n = 11;
2295
+ break;
2296
+ }
2297
+ return _context3.a(2, {
2298
+ status: "failed",
2299
+ message: messageOf(_t3)
2300
+ });
2301
+ case 11:
2302
+ step({
2303
+ kind: "deploying"
2304
+ });
2305
+ _context3.n = 12;
2306
+ return deps.deploy(withoutLockfiles(files), target);
2307
+ case 12:
2308
+ built = _context3.v;
2309
+ if (!(built.status !== "uploaded")) {
2310
+ _context3.n = 13;
2311
+ break;
2312
+ }
2313
+ return _context3.a(2, {
2314
+ status: "failed",
2315
+ message: built.status === "failed" ? built.message : "The build went live instead of stopping once uploaded."
2316
+ });
2317
+ case 13:
2318
+ serve = /*#__PURE__*/function () {
2319
+ var _ref3 = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee() {
2320
+ return slicedToArray._regenerator().w(function (_context) {
2321
+ while (1) switch (_context.n) {
2322
+ case 0:
2323
+ step({
2324
+ kind: "serving",
2325
+ since: now()
2326
+ });
2327
+ return _context.a(2, deps.client.serveBuild(deps.name, {
2328
+ publishId: built.publishId,
2329
+ inputs: inputs
2330
+ }));
2331
+ }
2332
+ }, _callee);
2333
+ }));
2334
+ return function serve() {
2335
+ return _ref3.apply(this, arguments);
2336
+ };
2337
+ }();
2338
+ servedNow = /*#__PURE__*/function () {
2339
+ var _ref4 = slicedToArray._asyncToGenerator(/*#__PURE__*/slicedToArray._regenerator().m(function _callee2() {
2340
+ var _proposal$staged, _proposal$built, proposal;
2341
+ return slicedToArray._regenerator().w(function (_context2) {
2342
+ while (1) switch (_context2.p = _context2.n) {
2343
+ case 0:
2344
+ _context2.p = 0;
2345
+ _context2.n = 1;
2346
+ return deps.client.get(deps.name);
2347
+ case 1:
2348
+ proposal = _context2.v;
2349
+ return _context2.a(2, ((_proposal$staged = proposal.staged) === null || _proposal$staged === void 0 ? void 0 : _proposal$staged.inputs) === inputs && proposal.staged.buildRequired === false && (_proposal$built = proposal.built) !== null && _proposal$built !== void 0 && _proposal$built.hash ? proposal : null);
2350
+ case 2:
2351
+ _context2.p = 2;
2352
+ _context2.v;
2353
+ return _context2.a(2, null);
2354
+ }
2355
+ }, _callee2, null, [[0, 2]]);
2356
+ }));
2357
+ return function servedNow() {
2358
+ return _ref4.apply(this, arguments);
2359
+ };
2360
+ }();
2361
+ builtOf = function builtOf(proposal) {
2362
+ var _proposal$built$hash, _proposal$built2;
2363
+ return {
2364
+ status: "built",
2365
+ hash: (_proposal$built$hash = (_proposal$built2 = proposal.built) === null || _proposal$built2 === void 0 ? void 0 : _proposal$built2.hash) !== null && _proposal$built$hash !== void 0 ? _proposal$built$hash : null,
2366
+ proposal: proposal
2367
+ };
2368
+ };
2369
+ _context3.p = 14;
2370
+ _t4 = builtOf;
2371
+ _context3.n = 15;
2372
+ return serve();
2373
+ case 15:
2374
+ return _context3.a(2, _t4(_context3.v));
2375
+ case 16:
2376
+ _context3.p = 16;
2377
+ _t5 = _context3.v;
2378
+ if (gatewayGaveUp(_t5)) {
2379
+ _context3.n = 17;
2380
+ break;
2381
+ }
2382
+ return _context3.a(2, {
2383
+ status: "failed",
2384
+ message: messageOf(_t5)
2385
+ });
2386
+ case 17:
2387
+ since = now();
2388
+ step({
2389
+ kind: "waiting",
2390
+ since: since
2391
+ });
2392
+ case 18:
2393
+ if (!(now() - since < SERVE_WAIT_MS)) {
2394
+ _context3.n = 22;
2395
+ break;
2396
+ }
2397
+ _context3.n = 19;
2398
+ return sleep(SERVE_POLL_MS);
2399
+ case 19:
2400
+ _context3.n = 20;
2401
+ return servedNow();
2402
+ case 20:
2403
+ served = _context3.v;
2404
+ if (!(served !== null)) {
2405
+ _context3.n = 21;
2406
+ break;
2407
+ }
2408
+ return _context3.a(2, builtOf(served));
2409
+ case 21:
2410
+ _context3.n = 18;
2411
+ break;
2412
+ case 22:
2413
+ _context3.p = 22;
2414
+ _t6 = builtOf;
2415
+ _context3.n = 23;
2416
+ return serve();
2417
+ case 23:
2418
+ return _context3.a(2, _t6(_context3.v));
2419
+ case 24:
2420
+ _context3.p = 24;
2421
+ _t7 = _context3.v;
2422
+ return _context3.a(2, {
2423
+ status: "failed",
2424
+ message: messageOf(_t7)
2425
+ });
2426
+ }
2427
+ }, _callee3, null, [[22, 24], [14, 16], [8, 10], [2, 4]]);
2428
+ }));
2429
+ return _buildProposal.apply(this, arguments);
2430
+ }
2431
+
2432
+ exports.proposalProject = proposalProject.proposalProject;
2433
+ exports.MergePrepareAnswer = MergePrepareAnswer;
2434
+ exports.RENEW_EVERY_MS = RENEW_EVERY_MS;
2435
+ exports.SERVE_POLL_MS = SERVE_POLL_MS;
2436
+ exports.SERVE_WAIT_MS = SERVE_WAIT_MS;
2437
+ exports.STUDIO_OUT_OF_DATE = STUDIO_OUT_OF_DATE;
2438
+ exports.STUDIO_OUT_OF_DATE_MESSAGE = STUDIO_OUT_OF_DATE_MESSAGE;
2439
+ exports.StudioPublishError = StudioPublishError;
2440
+ exports.StudioUploadError = StudioUploadError;
2441
+ exports.buildProposal = buildProposal;
2442
+ exports.callJson = callJson;
2443
+ exports.carriedInto = carriedInto;
2444
+ exports.createStudioJobClient = createStudioJobClient;
2445
+ exports.createStudioPublishClient = createStudioPublishClient;
2446
+ exports.isExpiredSlot = isExpiredSlot;
2447
+ exports.isMergeChange = isMergeChange;
2448
+ exports.isTransientPublishError = isTransientPublishError;
2449
+ exports.layerMoved = layerMoved;
2450
+ exports.loadedLayer = loadedLayer;
2451
+ exports.markStudioOutOfDate = markStudioOutOfDate;
2452
+ exports.onPublishRefusal = onPublishRefusal;
2453
+ exports.publishApiOf = publishApiOf;
2454
+ exports.readLiveLayer = readLiveLayer;
2455
+ exports.rememberLoadedLayer = rememberLoadedLayer;
2456
+ exports.resetLoadedLayerForTests = resetLoadedLayerForTests;
2457
+ exports.runJobToEnd = runJobToEnd;
2458
+ exports.runStudioDeploy = runStudioDeploy;
2459
+ exports.runStudioJob = runStudioJob;
2460
+ exports.runStudioPublish = runStudioPublish;
2461
+ exports.siteMovedSinceLoad = siteMovedSinceLoad;
2462
+ exports.splitBinaryFiles = splitBinaryFiles;
2463
+ exports.studioOutOfDateRefusals = studioOutOfDateRefusals;
2464
+ exports.subscribeStudioOutOfDate = subscribeStudioOutOfDate;
2465
+ exports.withCommittedFiles = withCommittedFiles;
2466
+ exports.withoutLockfiles = withoutLockfiles;