@sasjs/api 0.27.0 → 0.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/browser.js CHANGED
@@ -1,4 +1,4 @@
1
- /* @sasjs/api 0.27.0 - the SASjs Server API, for a service worker. https://github.com/sasjs/api */
1
+ /* @sasjs/api 0.28.0 - the SASjs Server API, for a service worker. https://github.com/sasjs/api */
2
2
  "use strict";
3
3
  var SASjsApi = (() => {
4
4
  var __defProp = Object.defineProperty;
@@ -1126,7 +1126,7 @@ var SASjsApi = (() => {
1126
1126
  method: "DELETE",
1127
1127
  path: "/SASjsApi/drive/deploy",
1128
1128
  tag: "Drive",
1129
- summary: "Remove an app stream entry",
1129
+ summary: "Remove an app",
1130
1130
  params: [
1131
1131
  {
1132
1132
  name: "id",
@@ -1170,6 +1170,24 @@ var SASjsApi = (() => {
1170
1170
  required: false,
1171
1171
  type: "string"
1172
1172
  },
1173
+ {
1174
+ name: "streamServiceName",
1175
+ in: "body",
1176
+ required: false,
1177
+ type: "string"
1178
+ },
1179
+ {
1180
+ name: "streamLogo",
1181
+ in: "body",
1182
+ required: false,
1183
+ type: "string"
1184
+ },
1185
+ {
1186
+ name: "replace",
1187
+ in: "body",
1188
+ required: false,
1189
+ type: "boolean"
1190
+ },
1173
1191
  {
1174
1192
  name: "fileTree",
1175
1193
  in: "body",
@@ -1204,6 +1222,36 @@ var SASjsApi = (() => {
1204
1222
  tag: "Drive",
1205
1223
  summary: "Deploy an uploaded file to SASjs Drive",
1206
1224
  params: [
1225
+ {
1226
+ name: "appLoc",
1227
+ in: "query",
1228
+ required: false,
1229
+ type: "string"
1230
+ },
1231
+ {
1232
+ name: "streamWebFolder",
1233
+ in: "query",
1234
+ required: false,
1235
+ type: "string"
1236
+ },
1237
+ {
1238
+ name: "streamServiceName",
1239
+ in: "query",
1240
+ required: false,
1241
+ type: "string"
1242
+ },
1243
+ {
1244
+ name: "streamLogo",
1245
+ in: "query",
1246
+ required: false,
1247
+ type: "string"
1248
+ },
1249
+ {
1250
+ name: "replace",
1251
+ in: "query",
1252
+ required: false,
1253
+ type: "boolean"
1254
+ },
1207
1255
  {
1208
1256
  name: "file",
1209
1257
  in: "formData",
@@ -3147,15 +3195,32 @@ var SASjsApi = (() => {
3147
3195
  throw new ApiError(400, error.message);
3148
3196
  }
3149
3197
  };
3150
- var checkPublishRequest = (request) => {
3198
+ var hasStreamFolder = (tree, streamWebFolder) => {
3199
+ const members = tree?.members;
3200
+ if (!Array.isArray(members)) return false;
3201
+ const isFolder = (member, name) => {
3202
+ const entry = member;
3203
+ return entry?.type === "folder" && entry?.name === name;
3204
+ };
3205
+ const services = members.find((member) => isFolder(member, "services"));
3206
+ const children = services?.members;
3207
+ return Array.isArray(children) && children.some((member) => isFolder(member, streamWebFolder));
3208
+ };
3209
+ var checkPublishRequest = (request, tree, options = {}) => {
3151
3210
  const appLoc = appLocOf(request.appLoc);
3152
3211
  if (appLoc === "/")
3153
3212
  throw new ApiError(400, "An app cannot be published at the drive root.");
3154
3213
  const logo = logoOf(request.logo);
3155
3214
  const frameAncestors = frameAncestorsOf(request.frameAncestors);
3215
+ const streamWebFolder = webFolderOf(request.streamWebFolder);
3216
+ if (tree !== void 0 && !hasStreamFolder(tree, streamWebFolder))
3217
+ throw new ApiError(
3218
+ 400,
3219
+ `The deployed tree has no "services/${streamWebFolder}" folder, so nothing would be published at ${appLoc}. Deploy the web assets under services/${streamWebFolder}, or ${options.folderFromManifest ? "correct the folder sasjs.app.json names." : "omit streamWebFolder."}`
3220
+ );
3156
3221
  return {
3157
3222
  appLoc,
3158
- streamWebFolder: webFolderOf(request.streamWebFolder),
3223
+ streamWebFolder,
3159
3224
  ...request.name === void 0 ? {} : { name: nameOf(request.name, appLoc) },
3160
3225
  ...logo === void 0 ? {} : { logo },
3161
3226
  ...frameAncestors === void 0 ? {} : { frameAncestors },
@@ -3361,6 +3426,14 @@ var SASjsApi = (() => {
3361
3426
  "POST /SASjsApi/code/trigger"
3362
3427
  ];
3363
3428
  var given = (value) => typeof value === "string" && value !== "" ? value : void 0;
3429
+ var replaceOf = (value) => {
3430
+ if (value === void 0 || value === "") return void 0;
3431
+ if (typeof value === "boolean") return value;
3432
+ const text2 = String(value).trim().toLowerCase();
3433
+ if (text2 === "true") return true;
3434
+ if (text2 === "false") return false;
3435
+ throw new ApiError(400, '"replace" must be true or false.');
3436
+ };
3364
3437
  var validate = (route, request) => {
3365
3438
  for (const param of route.params) {
3366
3439
  if (!param.required || param.in === "formData" || param.in === "header")
@@ -3602,18 +3675,22 @@ var SASjsApi = (() => {
3602
3675
  case "POST /SASjsApi/drive/deploy/upload":
3603
3676
  case "POST /SASjsApi/drive/deploy": {
3604
3677
  const bundle = splitAppBundle(body.fileTree);
3605
- const appLoc = String(body.appLoc ?? "");
3678
+ const isUpload = routeId === "POST /SASjsApi/drive/deploy/upload";
3679
+ const setting = (name) => isUpload ? given(query[name]) ?? body?.[name] : body?.[name];
3680
+ const appLoc = String(setting("appLoc") ?? "");
3606
3681
  const requested = services.appStream ? publishRequestFor(
3607
3682
  {
3608
3683
  appLoc,
3609
- streamWebFolder: given(body.streamWebFolder),
3610
- streamServiceName: given(body.streamServiceName),
3611
- streamLogo: given(body.streamLogo)
3684
+ streamWebFolder: given(setting("streamWebFolder")),
3685
+ streamServiceName: given(setting("streamServiceName")),
3686
+ streamLogo: given(setting("streamLogo"))
3612
3687
  },
3613
3688
  bundle.manifest
3614
3689
  ) : void 0;
3615
- const publish = requested === void 0 ? void 0 : checkPublishRequest(requested);
3616
- await drive.deploy(appLoc, bundle.tree);
3690
+ const folderFromManifest = requested?.streamWebFolder !== void 0 && given(setting("streamWebFolder")) === void 0 && bundle.manifest?.streamWebFolder !== void 0;
3691
+ const publish = requested === void 0 ? void 0 : checkPublishRequest(requested, bundle.tree, { folderFromManifest });
3692
+ const replace = replaceOf(setting("replace")) ?? isUpload;
3693
+ await drive.deploy(appLoc, bundle.tree, { replace });
3617
3694
  if (publish === void 0 || !services.appStream)
3618
3695
  return { status: 200, body: DEPLOY_SUCCESS };
3619
3696
  const entry = await services.appStream.publish(publish);
@@ -6482,15 +6559,18 @@ var SASjsApi = (() => {
6482
6559
  },
6483
6560
  // ------------------------------------------- the uploaded-archive route
6484
6561
  // The archive is expanded by the host, so what arrives here is the same tree
6485
- // `POST /deploy` takes - which is why every answer below matches its twin.
6562
+ // `POST /deploy` takes - which is why every answer below matches its twin. The
6563
+ // folder and the stream are stated in the QUERY, where the document puts them on
6564
+ // this route: the body is the archive. A host that receives an archive builds it
6565
+ // from the tree below; the tree is what the archive has to expand to.
6486
6566
  {
6487
6567
  id: "upload-tree",
6488
6568
  route: "POST /SASjsApi/drive/deploy/upload",
6489
6569
  description: "writes the tree an uploaded archive expanded to",
6490
6570
  request: {
6491
6571
  subject: SUBJECT,
6572
+ query: { appLoc: "/Public/apps/uploaded" },
6492
6573
  body: {
6493
- appLoc: "/Public/apps/uploaded",
6494
6574
  fileTree: {
6495
6575
  members: [
6496
6576
  {
@@ -6511,14 +6591,70 @@ var SASjsApi = (() => {
6511
6591
  }
6512
6592
  }
6513
6593
  },
6594
+ {
6595
+ id: "upload-query-fields",
6596
+ route: "POST /SASjsApi/drive/deploy/upload",
6597
+ description: "takes the folder and the stream from the query, where the document states them",
6598
+ request: {
6599
+ subject: SUBJECT,
6600
+ query: { appLoc: "/Public/apps/from-query", streamWebFolder: "web" },
6601
+ body: {
6602
+ fileTree: {
6603
+ members: [
6604
+ {
6605
+ name: "services",
6606
+ type: "folder",
6607
+ members: [
6608
+ {
6609
+ name: "web",
6610
+ type: "folder",
6611
+ members: [
6612
+ { name: "index.html", type: "file", code: "<h1>Hello</h1>" }
6613
+ ]
6614
+ }
6615
+ ]
6616
+ }
6617
+ ]
6618
+ }
6619
+ }
6620
+ },
6621
+ expect: {
6622
+ status: 200,
6623
+ partial: {
6624
+ status: "success",
6625
+ app: { name: "from-query" }
6626
+ }
6627
+ }
6628
+ },
6629
+ {
6630
+ id: "deploy-refuses-a-tree-without-its-web-folder",
6631
+ route: "POST /SASjsApi/drive/deploy",
6632
+ description: "refuses a stream the tree does not carry, rather than indexing an app that would answer 404",
6633
+ request: {
6634
+ subject: SUBJECT,
6635
+ body: {
6636
+ appLoc: "/Public/apps/no-web",
6637
+ streamWebFolder: "web",
6638
+ fileTree: {
6639
+ members: [
6640
+ { name: "index.html", type: "file", code: "<h1>Hello</h1>" }
6641
+ ]
6642
+ }
6643
+ }
6644
+ },
6645
+ expect: {
6646
+ status: 400,
6647
+ error: 'The deployed tree has no "services/web" folder, so nothing would be published at /Public/apps/no-web. Deploy the web assets under services/web, or omit streamWebFolder.'
6648
+ }
6649
+ },
6514
6650
  {
6515
6651
  id: "upload-text-file",
6516
6652
  route: "POST /SASjsApi/drive/deploy/upload",
6517
6653
  description: "writes a file member as its own text when no encoding is stated",
6518
6654
  request: {
6519
6655
  subject: SUBJECT,
6656
+ query: { appLoc: "/Public/apps/text" },
6520
6657
  body: {
6521
- appLoc: "/Public/apps/text",
6522
6658
  fileTree: {
6523
6659
  members: [
6524
6660
  { name: "index.html", type: "file", code: "<h1>Hello</h1>" }
@@ -6538,9 +6674,12 @@ var SASjsApi = (() => {
6538
6674
  id: "upload-refused-by-shape",
6539
6675
  route: "POST /SASjsApi/drive/deploy/upload",
6540
6676
  description: "refuses an expansion that is not a tree",
6677
+ // no archive expands to this: only a host handed an expansion can send it
6678
+ transport: "expansion",
6541
6679
  request: {
6542
6680
  subject: SUBJECT,
6543
- body: { appLoc: "/Public/apps/bad", fileTree: { members: 1 } }
6681
+ query: { appLoc: "/Public/apps/bad" },
6682
+ body: { fileTree: { members: 1 } }
6544
6683
  },
6545
6684
  expect: {
6546
6685
  status: 400,
@@ -6557,8 +6696,8 @@ var SASjsApi = (() => {
6557
6696
  description: "applies the deploy cap to the expanded tree, not the bytes",
6558
6697
  request: {
6559
6698
  subject: SUBJECT,
6699
+ query: { appLoc: "/Public/apps/many" },
6560
6700
  body: {
6561
- appLoc: "/Public/apps/many",
6562
6701
  fileTree: { members: manyMembers2(10001) }
6563
6702
  }
6564
6703
  },
@@ -6644,8 +6783,8 @@ var SASjsApi = (() => {
6644
6783
  unwritable: true,
6645
6784
  request: {
6646
6785
  subject: SUBJECT,
6786
+ query: { appLoc: "/Public/apps/demo" },
6647
6787
  body: {
6648
- appLoc: "/Public/apps/demo",
6649
6788
  fileTree: {
6650
6789
  members: [{ name: "run", type: "service", code: "%put hi;" }]
6651
6790
  }
@@ -6656,6 +6795,66 @@ var SASjsApi = (() => {
6656
6795
  body: { status: "failure", message: "Deployment failed!" },
6657
6796
  error: "Deployment failed!"
6658
6797
  }
6798
+ },
6799
+ {
6800
+ id: "upload-replaces-the-folder",
6801
+ route: "POST /SASjsApi/drive/deploy/upload",
6802
+ description: "an archive upload is the folder's whole contents, so a file it drops is gone",
6803
+ seed: {
6804
+ "/Public/apps/replaced/services/web/old.html": "<h1>old</h1>"
6805
+ },
6806
+ request: {
6807
+ subject: SUBJECT,
6808
+ query: { appLoc: "/Public/apps/replaced", streamWebFolder: "web" },
6809
+ body: {
6810
+ fileTree: {
6811
+ members: [
6812
+ {
6813
+ name: "services",
6814
+ type: "folder",
6815
+ members: [
6816
+ {
6817
+ name: "web",
6818
+ type: "folder",
6819
+ members: [
6820
+ { name: "index.html", type: "file", code: "<h1>new</h1>" }
6821
+ ]
6822
+ }
6823
+ ]
6824
+ }
6825
+ ]
6826
+ }
6827
+ }
6828
+ },
6829
+ expect: {
6830
+ status: 200,
6831
+ partial: { status: "success", app: { name: "replaced" } }
6832
+ },
6833
+ expectDrive: {
6834
+ present: ["/Public/apps/replaced/services/web/index.html"],
6835
+ missing: ["/Public/apps/replaced/services/web/old.html"]
6836
+ }
6837
+ },
6838
+ {
6839
+ id: "deploy-keeps-what-is-there",
6840
+ route: "POST /SASjsApi/drive/deploy",
6841
+ description: "the JSON route writes over what is there unless it is told to replace",
6842
+ seed: {
6843
+ "/Public/apps/kept/old.html": "<h1>old</h1>"
6844
+ },
6845
+ request: {
6846
+ subject: SUBJECT,
6847
+ body: {
6848
+ appLoc: "/Public/apps/kept",
6849
+ fileTree: {
6850
+ members: [{ name: "a.html", type: "file", code: "<h1>a</h1>" }]
6851
+ }
6852
+ }
6853
+ },
6854
+ expect: { status: 200 },
6855
+ expectDrive: {
6856
+ present: ["/Public/apps/kept/a.html", "/Public/apps/kept/old.html"]
6857
+ }
6659
6858
  }
6660
6859
  ];
6661
6860
 
@@ -8641,9 +8840,31 @@ var SASjsApi = (() => {
8641
8840
  throw new Error("the store refused the write");
8642
8841
  }
8643
8842
  };
8843
+ var checkDrive = async (testCase, services) => {
8844
+ if (!testCase.expectDrive || !services.drive) return;
8845
+ const holds = async (path) => {
8846
+ try {
8847
+ await services.drive.read(path);
8848
+ return true;
8849
+ } catch (e) {
8850
+ return false;
8851
+ }
8852
+ };
8853
+ for (const path of testCase.expectDrive.present || [])
8854
+ if (!await holds(path))
8855
+ throw new Error(
8856
+ `${testCase.id}: expected a file at ${path}, and the drive has none`
8857
+ );
8858
+ for (const path of testCase.expectDrive.missing || [])
8859
+ if (await holds(path))
8860
+ throw new Error(
8861
+ `${testCase.id}: expected no file at ${path}, and the drive still holds one`
8862
+ );
8863
+ };
8644
8864
  var runCase = async (testCase, services = servicesFor(testCase)) => {
8645
8865
  try {
8646
8866
  const response = await dispatch(services, testCase.route, testCase.request);
8867
+ await checkDrive(testCase, services);
8647
8868
  return {
8648
8869
  status: response.status,
8649
8870
  body: response.body,
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MemoryAppStreamStore = exports.createAppStreamService = exports.checkPublishRequest = exports.defaultAppName = exports.appSummary = void 0;
3
+ exports.MemoryAppStreamStore = exports.createAppStreamService = exports.checkPublishRequest = exports.hasStreamFolder = exports.defaultAppName = exports.appSummary = void 0;
4
4
  /**
5
5
  * The app index: what has been published, keyed by a UUID.
6
6
  *
@@ -71,6 +71,28 @@ const appLocOf = (value) => {
71
71
  throw new types_js_1.ApiError(400, error.message);
72
72
  }
73
73
  };
74
+ /**
75
+ * Whether a deploy tree carries the folder an app is served from.
76
+ *
77
+ * An app is served out of `<appLoc>/services/<streamWebFolder>`, so a tree
78
+ * without that folder would be indexed and then answer `404` to every request -
79
+ * a tile that opens nothing, which is worse than a refusal that names the missing
80
+ * folder.
81
+ */
82
+ const hasStreamFolder = (tree, streamWebFolder) => {
83
+ const members = tree?.members;
84
+ if (!Array.isArray(members))
85
+ return false;
86
+ const isFolder = (member, name) => {
87
+ const entry = member;
88
+ return entry?.type === 'folder' && entry?.name === name;
89
+ };
90
+ const services = members.find((member) => isFolder(member, 'services'));
91
+ const children = services?.members;
92
+ return (Array.isArray(children) &&
93
+ children.some((member) => isFolder(member, streamWebFolder)));
94
+ };
95
+ exports.hasStreamFolder = hasStreamFolder;
74
96
  /**
75
97
  * A publish request, checked and normalised, with no default applied.
76
98
  *
@@ -78,8 +100,11 @@ const appLocOf = (value) => {
78
100
  * deployment: the drive and the index are two writes, and a request the index
79
101
  * would refuse must not leave the files behind. `name` is left absent when the
80
102
  * caller did not state one, because a redeploy inherits the label it had.
103
+ *
104
+ * The tree is optional so that a caller holding only the request can still check
105
+ * it; when it is stated, the folder the app would be served from has to be in it.
81
106
  */
82
- const checkPublishRequest = (request) => {
107
+ const checkPublishRequest = (request, tree, options = {}) => {
83
108
  const appLoc = appLocOf(request.appLoc);
84
109
  // the drive root is not a folder an app can live in: a deploy to `/` writes
85
110
  // `//index.html`, which nothing can read, so it is refused rather than indexed
@@ -87,9 +112,18 @@ const checkPublishRequest = (request) => {
87
112
  throw new types_js_1.ApiError(400, 'An app cannot be published at the drive root.');
88
113
  const logo = logoOf(request.logo);
89
114
  const frameAncestors = frameAncestorsOf(request.frameAncestors);
115
+ const streamWebFolder = webFolderOf(request.streamWebFolder);
116
+ // the tree has to carry the folder the app is served from, or the publish
117
+ // advertises an app whose every request is a `404`. The advice names the source
118
+ // of the folder: a caller whose bundle stated it cannot omit the field, and the
119
+ // bundle is the thing to correct.
120
+ if (tree !== undefined && !(0, exports.hasStreamFolder)(tree, streamWebFolder))
121
+ throw new types_js_1.ApiError(400, `The deployed tree has no "services/${streamWebFolder}" folder, so nothing would be published at ${appLoc}. Deploy the web assets under services/${streamWebFolder}, or ${options.folderFromManifest
122
+ ? 'correct the folder sasjs.app.json names.'
123
+ : 'omit streamWebFolder.'}`);
90
124
  return {
91
125
  appLoc,
92
- streamWebFolder: webFolderOf(request.streamWebFolder),
126
+ streamWebFolder,
93
127
  ...(request.name === undefined
94
128
  ? {}
95
129
  : { name: nameOf(request.name, appLoc) }),
@@ -44,15 +44,18 @@ exports.DEPLOY_CORPUS = [
44
44
  },
45
45
  // ------------------------------------------- the uploaded-archive route
46
46
  // The archive is expanded by the host, so what arrives here is the same tree
47
- // `POST /deploy` takes - which is why every answer below matches its twin.
47
+ // `POST /deploy` takes - which is why every answer below matches its twin. The
48
+ // folder and the stream are stated in the QUERY, where the document puts them on
49
+ // this route: the body is the archive. A host that receives an archive builds it
50
+ // from the tree below; the tree is what the archive has to expand to.
48
51
  {
49
52
  id: 'upload-tree',
50
53
  route: 'POST /SASjsApi/drive/deploy/upload',
51
54
  description: 'writes the tree an uploaded archive expanded to',
52
55
  request: {
53
56
  subject: SUBJECT,
57
+ query: { appLoc: '/Public/apps/uploaded' },
54
58
  body: {
55
- appLoc: '/Public/apps/uploaded',
56
59
  fileTree: {
57
60
  members: [
58
61
  {
@@ -73,14 +76,70 @@ exports.DEPLOY_CORPUS = [
73
76
  }
74
77
  }
75
78
  },
79
+ {
80
+ id: 'upload-query-fields',
81
+ route: 'POST /SASjsApi/drive/deploy/upload',
82
+ description: 'takes the folder and the stream from the query, where the document states them',
83
+ request: {
84
+ subject: SUBJECT,
85
+ query: { appLoc: '/Public/apps/from-query', streamWebFolder: 'web' },
86
+ body: {
87
+ fileTree: {
88
+ members: [
89
+ {
90
+ name: 'services',
91
+ type: 'folder',
92
+ members: [
93
+ {
94
+ name: 'web',
95
+ type: 'folder',
96
+ members: [
97
+ { name: 'index.html', type: 'file', code: '<h1>Hello</h1>' }
98
+ ]
99
+ }
100
+ ]
101
+ }
102
+ ]
103
+ }
104
+ }
105
+ },
106
+ expect: {
107
+ status: 200,
108
+ partial: {
109
+ status: 'success',
110
+ app: { name: 'from-query' }
111
+ }
112
+ }
113
+ },
114
+ {
115
+ id: 'deploy-refuses-a-tree-without-its-web-folder',
116
+ route: 'POST /SASjsApi/drive/deploy',
117
+ description: 'refuses a stream the tree does not carry, rather than indexing an app that would answer 404',
118
+ request: {
119
+ subject: SUBJECT,
120
+ body: {
121
+ appLoc: '/Public/apps/no-web',
122
+ streamWebFolder: 'web',
123
+ fileTree: {
124
+ members: [
125
+ { name: 'index.html', type: 'file', code: '<h1>Hello</h1>' }
126
+ ]
127
+ }
128
+ }
129
+ },
130
+ expect: {
131
+ status: 400,
132
+ error: 'The deployed tree has no "services/web" folder, so nothing would be published at /Public/apps/no-web. Deploy the web assets under services/web, or omit streamWebFolder.'
133
+ }
134
+ },
76
135
  {
77
136
  id: 'upload-text-file',
78
137
  route: 'POST /SASjsApi/drive/deploy/upload',
79
138
  description: 'writes a file member as its own text when no encoding is stated',
80
139
  request: {
81
140
  subject: SUBJECT,
141
+ query: { appLoc: '/Public/apps/text' },
82
142
  body: {
83
- appLoc: '/Public/apps/text',
84
143
  fileTree: {
85
144
  members: [
86
145
  { name: 'index.html', type: 'file', code: '<h1>Hello</h1>' }
@@ -100,9 +159,12 @@ exports.DEPLOY_CORPUS = [
100
159
  id: 'upload-refused-by-shape',
101
160
  route: 'POST /SASjsApi/drive/deploy/upload',
102
161
  description: 'refuses an expansion that is not a tree',
162
+ // no archive expands to this: only a host handed an expansion can send it
163
+ transport: 'expansion',
103
164
  request: {
104
165
  subject: SUBJECT,
105
- body: { appLoc: '/Public/apps/bad', fileTree: { members: 1 } }
166
+ query: { appLoc: '/Public/apps/bad' },
167
+ body: { fileTree: { members: 1 } }
106
168
  },
107
169
  expect: {
108
170
  status: 400,
@@ -119,8 +181,8 @@ exports.DEPLOY_CORPUS = [
119
181
  description: 'applies the deploy cap to the expanded tree, not the bytes',
120
182
  request: {
121
183
  subject: SUBJECT,
184
+ query: { appLoc: '/Public/apps/many' },
122
185
  body: {
123
- appLoc: '/Public/apps/many',
124
186
  fileTree: { members: manyMembers(10001) }
125
187
  }
126
188
  },
@@ -206,8 +268,8 @@ exports.DEPLOY_CORPUS = [
206
268
  unwritable: true,
207
269
  request: {
208
270
  subject: SUBJECT,
271
+ query: { appLoc: '/Public/apps/demo' },
209
272
  body: {
210
- appLoc: '/Public/apps/demo',
211
273
  fileTree: {
212
274
  members: [{ name: 'run', type: 'service', code: '%put hi;' }]
213
275
  }
@@ -218,5 +280,65 @@ exports.DEPLOY_CORPUS = [
218
280
  body: { status: 'failure', message: 'Deployment failed!' },
219
281
  error: 'Deployment failed!'
220
282
  }
283
+ },
284
+ {
285
+ id: 'upload-replaces-the-folder',
286
+ route: 'POST /SASjsApi/drive/deploy/upload',
287
+ description: "an archive upload is the folder's whole contents, so a file it drops is gone",
288
+ seed: {
289
+ '/Public/apps/replaced/services/web/old.html': '<h1>old</h1>'
290
+ },
291
+ request: {
292
+ subject: SUBJECT,
293
+ query: { appLoc: '/Public/apps/replaced', streamWebFolder: 'web' },
294
+ body: {
295
+ fileTree: {
296
+ members: [
297
+ {
298
+ name: 'services',
299
+ type: 'folder',
300
+ members: [
301
+ {
302
+ name: 'web',
303
+ type: 'folder',
304
+ members: [
305
+ { name: 'index.html', type: 'file', code: '<h1>new</h1>' }
306
+ ]
307
+ }
308
+ ]
309
+ }
310
+ ]
311
+ }
312
+ }
313
+ },
314
+ expect: {
315
+ status: 200,
316
+ partial: { status: 'success', app: { name: 'replaced' } }
317
+ },
318
+ expectDrive: {
319
+ present: ['/Public/apps/replaced/services/web/index.html'],
320
+ missing: ['/Public/apps/replaced/services/web/old.html']
321
+ }
322
+ },
323
+ {
324
+ id: 'deploy-keeps-what-is-there',
325
+ route: 'POST /SASjsApi/drive/deploy',
326
+ description: 'the JSON route writes over what is there unless it is told to replace',
327
+ seed: {
328
+ '/Public/apps/kept/old.html': '<h1>old</h1>'
329
+ },
330
+ request: {
331
+ subject: SUBJECT,
332
+ body: {
333
+ appLoc: '/Public/apps/kept',
334
+ fileTree: {
335
+ members: [{ name: 'a.html', type: 'file', code: '<h1>a</h1>' }]
336
+ }
337
+ }
338
+ },
339
+ expect: { status: 200 },
340
+ expectDrive: {
341
+ present: ['/Public/apps/kept/a.html', '/Public/apps/kept/old.html']
342
+ }
221
343
  }
222
344
  ];
@@ -96,6 +96,32 @@ class RefusingDriveStore extends memory_store_js_1.MemoryDriveStore {
96
96
  throw new Error('the store refused the write');
97
97
  }
98
98
  }
99
+ /**
100
+ * The drive state a case says it leaves, checked where the case states it.
101
+ *
102
+ * Reported as a thrown error naming the case rather than as a status mismatch: a
103
+ * host that dropped the deploy option answers `200` and fails here, which is the
104
+ * only place the difference between "replaced" and "kept" can be seen.
105
+ */
106
+ const checkDrive = async (testCase, services) => {
107
+ if (!testCase.expectDrive || !services.drive)
108
+ return;
109
+ const holds = async (path) => {
110
+ try {
111
+ await services.drive.read(path);
112
+ return true;
113
+ }
114
+ catch (e) {
115
+ return false;
116
+ }
117
+ };
118
+ for (const path of testCase.expectDrive.present || [])
119
+ if (!(await holds(path)))
120
+ throw new Error(`${testCase.id}: expected a file at ${path}, and the drive has none`);
121
+ for (const path of testCase.expectDrive.missing || [])
122
+ if (await holds(path))
123
+ throw new Error(`${testCase.id}: expected no file at ${path}, and the drive still holds one`);
124
+ };
99
125
  /**
100
126
  * Runs a corpus case against the drive the case seeds itself, or against
101
127
  * services a caller supplies - that is how a host (or a failing store) is put
@@ -104,6 +130,7 @@ class RefusingDriveStore extends memory_store_js_1.MemoryDriveStore {
104
130
  const runCase = async (testCase, services = (0, exports.servicesFor)(testCase)) => {
105
131
  try {
106
132
  const response = await (0, dispatch_js_1.dispatch)(services, testCase.route, testCase.request);
133
+ await checkDrive(testCase, services);
107
134
  return {
108
135
  status: response.status,
109
136
  body: response.body,