@sasjs/api 0.38.0 → 0.40.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.
@@ -1,35 +1,39 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.publishRequestFor = exports.splitAppBundle = exports.parseAppManifest = exports.APP_MANIFEST = void 0;
3
+ exports.publishRequestFor = exports.splitAppBundle = exports.parseAppManifest = exports.BACKEND_FOLDER = exports.FRONTEND_FOLDER = exports.APP_MANIFEST = void 0;
4
4
  /**
5
5
  * An app as one bundle: the deployment and the statement of what it publishes.
6
6
  *
7
- * A deploy writes a tree to a folder, and a tree of an app already holds
8
- * everything the app is - its services under `services/`, the compiled web assets
9
- * under `services/<streamWebFolder>`, and whatever jobs and data files it was
10
- * built with. What a tree cannot say is that the folder IS an app: which of its
11
- * folders holds the web build, what it should be called, and whether it may be
12
- * framed. That statement is a small file at the root of the archive:
7
+ * Two shapes travel through here, and the difference is where the frontend sits.
13
8
  *
14
- * sasjs.app.json
15
- * services/
16
- * web/ the compiled app: index.html, main.<hash>.js, assets/
17
- * build.sas the services the app calls
18
- * jobs/
9
+ * **A package** is what a current `sasjs build` emits for a server: a frontend, an
10
+ * optional backend, and a manifest saying which app the frontend is.
19
11
  *
20
- * So one zip deploys the app and the folders under it in a single request, and
21
- * the app is indexed as part of that deploy rather than by a second call that can
22
- * fail on its own. The manifest is consumed here: it is metadata, not an asset,
23
- * so it is stripped from the tree before the tree is written and never appears in
24
- * the app's served files.
12
+ * sasjs.app.json {"name": "macrodash", "appLoc": "/Public/app/macrodash"}
13
+ * web/ the compiled app: index.html, assets/
14
+ * backend/ optional: the services the app calls, written to appLoc
25
15
  *
26
- * The manifest is optional. A deploy that names `streamWebFolder` in its payload
27
- * publishes without one, which is what an existing client does; a bundle that
28
- * carries a manifest needs no arguments beyond the folder it deploys to.
16
+ * The halves are named, not inferred from a path: `web/` becomes `apps/<name>` and
17
+ * `backend/` becomes `appLoc`, and neither is guessed at from a member's name.
18
+ * One zip therefore deploys a backend and publishes a frontend in a single
19
+ * request, and the frontend can be republished alone by leaving `backend/` out.
20
+ *
21
+ * **A legacy bundle** is the shape before that: one tree deployed to `appLoc`,
22
+ * with the web assets carried at `services/<streamWebFolder>`. It is what every
23
+ * released CLI emits, and it keeps working unchanged - the manifest is optional
24
+ * there because the deploy payload states the stream fields instead.
25
+ *
26
+ * The manifest is metadata, not an asset: it is taken out of the tree before the
27
+ * tree is written and never appears in the app's served files.
29
28
  */
30
29
  const types_js_1 = require("../types.js");
30
+ const deploy_js_1 = require("../drive/deploy.js");
31
31
  /** The file, at the root of an app bundle, that says the bundle is an app. */
32
32
  exports.APP_MANIFEST = 'sasjs.app.json';
33
+ /** The folder in a package holding the compiled frontend. */
34
+ exports.FRONTEND_FOLDER = 'web';
35
+ /** The folder in a package holding the services the frontend calls. */
36
+ exports.BACKEND_FOLDER = 'backend';
33
37
  const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
34
38
  const text = (value, field) => {
35
39
  if (value === undefined)
@@ -38,15 +42,24 @@ const text = (value, field) => {
38
42
  throw new types_js_1.ApiError(400, `"${field}" must be a non-empty string in ${exports.APP_MANIFEST}.`);
39
43
  return value.trim();
40
44
  };
41
- /** A manifest, checked field by field, naming the field that is wrong. */
45
+ /**
46
+ * A manifest, checked field by field, naming the field that is wrong.
47
+ *
48
+ * Nothing is required: a package states its name, a legacy bundle states its
49
+ * stream folder, and a deploy that states neither in the manifest states them in
50
+ * its payload. What the manifest states has to be usable, which is what is checked
51
+ * here; whether the request as a whole can publish is `checkPublishRequest`'s
52
+ * question, asked once against the request the two have been composed into.
53
+ */
42
54
  const parseAppManifest = (value) => {
43
55
  if (!isObject(value))
44
56
  throw new types_js_1.ApiError(400, `${exports.APP_MANIFEST} must be an object.`);
45
- const streamWebFolder = text(value.streamWebFolder, 'streamWebFolder');
46
- if (streamWebFolder === undefined)
47
- throw new types_js_1.ApiError(400, `${exports.APP_MANIFEST} must state "streamWebFolder": the folder under services/ holding the compiled app.`);
48
57
  const name = text(value.name, 'name');
58
+ const appLoc = text(value.appLoc, 'appLoc');
59
+ const streamWebFolder = text(value.streamWebFolder, 'streamWebFolder');
49
60
  const logo = text(value.logo, 'logo');
61
+ if (streamWebFolder !== undefined && /[\\/]/.test(streamWebFolder))
62
+ throw new types_js_1.ApiError(400, `"streamWebFolder" in ${exports.APP_MANIFEST} must be a single path segment.`);
50
63
  const frameAncestors = value.frameAncestors;
51
64
  if (frameAncestors !== undefined &&
52
65
  (typeof frameAncestors !== 'string' || frameAncestors === '') &&
@@ -56,7 +69,8 @@ const parseAppManifest = (value) => {
56
69
  throw new types_js_1.ApiError(400, `"frameAncestors" in ${exports.APP_MANIFEST} must be a string or a non-empty list of strings.`);
57
70
  return {
58
71
  ...(name === undefined ? {} : { name }),
59
- streamWebFolder,
72
+ ...(appLoc === undefined ? {} : { appLoc }),
73
+ ...(streamWebFolder === undefined ? {} : { streamWebFolder }),
60
74
  ...(logo === undefined ? {} : { logo }),
61
75
  ...(frameAncestors === undefined
62
76
  ? {}
@@ -64,61 +78,112 @@ const parseAppManifest = (value) => {
64
78
  };
65
79
  };
66
80
  exports.parseAppManifest = parseAppManifest;
81
+ /** A member that is a folder, by name. */
82
+ const folderNamed = (members, name) => members.find((member) => isObject(member) && member.type === 'folder' && member.name === name);
83
+ /** A folder's own members, as a tree of its contents. */
84
+ const contentsOf = (member) => {
85
+ const members = member?.members;
86
+ return Array.isArray(members)
87
+ ? { members: members }
88
+ : undefined;
89
+ };
67
90
  /**
68
- * The deploy tree split from its manifest.
91
+ * The deploy tree split from its manifest, and from the halves a package names.
69
92
  *
70
93
  * A tree that is not a tree is passed through untouched: the deploy is the thing
71
94
  * that refuses a malformed tree, with the message its own cases pin, and saying
72
95
  * it here as well would be two wordings of one refusal.
96
+ *
97
+ * A bundle is a package when it holds a `web/` folder or a manifest at its root.
98
+ * The frontend is then that folder's contents - which is why a compiled app is
99
+ * zipped as the folder's contents rather than the folder itself - and a `backend/`
100
+ * folder beside it is the tree to write to `appLoc`.
73
101
  */
74
102
  const splitAppBundle = (tree) => {
75
103
  if (!isObject(tree) || !Array.isArray(tree.members))
76
104
  return { tree: tree };
77
105
  const members = tree.members;
78
106
  const at = members.findIndex((member) => isObject(member) && member.type === 'file' && member.name === exports.APP_MANIFEST);
79
- if (at === -1)
80
- return { tree: tree };
81
- const member = members[at];
82
- if (typeof member.code !== 'string')
83
- throw new types_js_1.ApiError(400, `${exports.APP_MANIFEST} carries no text.`);
84
- let parsed;
85
- try {
86
- parsed = JSON.parse(member.code);
87
- }
88
- catch (error) {
89
- throw new types_js_1.ApiError(400, `${exports.APP_MANIFEST} is not valid JSON: ${error.message}`);
107
+ const withoutManifest = at === -1 ? members : members.filter((_, index) => index !== at);
108
+ let manifest;
109
+ if (at !== -1) {
110
+ const member = members[at];
111
+ if (typeof member.code !== 'string')
112
+ throw new types_js_1.ApiError(400, `${exports.APP_MANIFEST} carries no text.`);
113
+ // a manifest read out of a zip is bytes: an archive records no text/binary
114
+ // distinction, so the tree builder marks every member `base64` and the
115
+ // manifest has to be decoded before it can be parsed
116
+ const source = member.encoding === 'base64'
117
+ ? new TextDecoder().decode((0, deploy_js_1.decodeBase64)(member.code))
118
+ : member.code;
119
+ let parsed;
120
+ try {
121
+ parsed = JSON.parse(source);
122
+ }
123
+ catch (error) {
124
+ throw new types_js_1.ApiError(400, `${exports.APP_MANIFEST} is not valid JSON: ${error.message}`);
125
+ }
126
+ manifest = (0, exports.parseAppManifest)(parsed);
90
127
  }
128
+ const rest = { ...tree, members: withoutManifest };
129
+ const web = folderNamed(withoutManifest, exports.FRONTEND_FOLDER);
130
+ // nothing says this is an app: pass the tree through untouched, so a caller that
131
+ // keeps its own copy sees no change and the deploy is the thing that judges it.
132
+ // A manifest is the only other way out of this branch, so there is none here.
133
+ if (web === undefined && manifest === undefined)
134
+ return { tree: tree };
135
+ // a `web/` folder is the frontend; without one, a manifest still says the tree
136
+ // is an app, and the whole tree is then the app's own files
137
+ const backend = contentsOf(folderNamed(withoutManifest, exports.BACKEND_FOLDER));
91
138
  return {
92
- tree: {
93
- ...tree,
94
- members: members.filter((_, index) => index !== at)
139
+ tree: rest,
140
+ frontend: contentsOf(web) ?? {
141
+ members: withoutManifest.filter((member) => member !== folderNamed(withoutManifest, exports.BACKEND_FOLDER))
95
142
  },
96
- manifest: (0, exports.parseAppManifest)(parsed)
143
+ ...(backend === undefined ? {} : { backend }),
144
+ manifest: manifest
97
145
  };
98
146
  };
99
147
  exports.splitAppBundle = splitAppBundle;
100
148
  /**
101
149
  * The publish a deploy asks for, or `undefined` when it publishes nothing.
102
150
  *
103
- * The request wins over the bundle where both speak - a caller naming a folder or
104
- * a label is correcting the archive, not repeating it - and the bundle fills what
105
- * the request leaves out, so a deploy that carries a manifest needs neither
106
- * `streamWebFolder` nor `streamServiceName`.
151
+ * The request wins over the bundle where both speak - a caller naming a folder, a
152
+ * name or a label is correcting the archive, not repeating it - and the bundle
153
+ * fills what the request leaves out.
154
+ *
155
+ * A bundle is published as a **legacy** app when either half states a
156
+ * `streamWebFolder`: that field is what puts the web assets inside the backend's
157
+ * folder, and a request that keeps it is asking for the shape it describes. A
158
+ * package publishes as a **frontend** instead, which needs only a name - the
159
+ * folder follows from it.
160
+ *
161
+ * `packaged` says the bundle carries a `web/` folder, so a request that names
162
+ * neither a stream folder nor a name is an app with nothing to call it: the
163
+ * request comes back without a name and `checkPublishRequest` refuses it by name,
164
+ * rather than the deploy writing the frontend nowhere.
107
165
  */
108
- const publishRequestFor = (deploy, manifest) => {
166
+ const publishRequestFor = (deploy, manifest, options = {}) => {
109
167
  const streamWebFolder = deploy.streamWebFolder ?? manifest?.streamWebFolder;
110
- if (streamWebFolder === undefined)
111
- return undefined;
112
- const name = deploy.streamServiceName ?? manifest?.name;
168
+ const appLoc = deploy.appLoc ?? manifest?.appLoc;
169
+ const name = deploy.name ?? deploy.streamServiceName ?? manifest?.name;
113
170
  const logo = deploy.streamLogo ?? manifest?.logo;
171
+ const frameAncestors = manifest?.frameAncestors;
172
+ if (streamWebFolder !== undefined)
173
+ return {
174
+ ...(appLoc === undefined ? {} : { appLoc }),
175
+ streamWebFolder,
176
+ ...(name === undefined ? {} : { name }),
177
+ ...(logo === undefined ? {} : { logo }),
178
+ ...(frameAncestors === undefined ? {} : { frameAncestors })
179
+ };
180
+ if (name === undefined && !options.packaged)
181
+ return undefined;
114
182
  return {
115
- appLoc: deploy.appLoc,
116
- streamWebFolder,
117
183
  ...(name === undefined ? {} : { name }),
184
+ ...(appLoc === undefined ? {} : { appLoc }),
118
185
  ...(logo === undefined ? {} : { logo }),
119
- ...(manifest?.frameAncestors === undefined
120
- ? {}
121
- : { frameAncestors: manifest.frameAncestors })
186
+ ...(frameAncestors === undefined ? {} : { frameAncestors })
122
187
  };
123
188
  };
124
189
  exports.publishRequestFor = publishRequestFor;
@@ -1,33 +1,48 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MemoryAppStreamStore = exports.createAppStreamService = exports.checkPublishRequest = exports.hasStreamFolder = exports.defaultAppName = exports.appSummary = void 0;
3
+ exports.MemoryAppStreamStore = exports.createAppStreamService = exports.checkPublishRequest = exports.hasFrontendRoot = exports.hasStreamFolder = exports.defaultAppName = exports.appSummary = exports.frontendFolderOf = exports.APP_FOLDERS_ROOT = void 0;
4
4
  /**
5
5
  * The app index: what has been published, keyed by a UUID.
6
6
  *
7
- * An app is one deployed folder (`appLoc`), the web assets inside it
8
- * (`appLoc/services/<streamWebFolder>`) and a UUID assigned when it is first
9
- * published. The id is the key the routes, the listing and the permission rules
10
- * point at, and it is what a redeploy keeps. The name is a label a person reads
11
- * AND - when exactly one app holds it - an address: `/app/<name>` opens the app,
12
- * so a name the caller states is validated as a URL segment and has to be free.
13
- * A label derived from the folder carries no such rule, because it was not
14
- * chosen: two folders may share a leaf name and both stay openable by id.
7
+ * An app is a **frontend**: a named folder under `apps/` in the drive, and a UUID
8
+ * assigned when it is first published. The id is the key the routes, the listing
9
+ * and the permission rules point at, and it is what a redeploy keeps. The name is
10
+ * the app's address as well as its label - `/app/<name>` opens it and
11
+ * `apps/<name>` holds it - so a name is validated as a URL segment and has to be
12
+ * free.
13
+ *
14
+ * A backend is metadata. An app may name the `appLoc` whose services its frontend
15
+ * calls, and several apps may name the same one: a frontend is what an app is,
16
+ * and where its services live is a fact about the app rather than its identity.
17
+ * The backend's files are provisioned by a publish that carries them, which is a
18
+ * separate, optional half of a bundle.
19
+ *
20
+ * One legacy shape survives, and only so that a deployment made by an older CLI
21
+ * keeps serving: an entry carrying `streamWebFolder` keeps its web assets where
22
+ * that shape put them, `<appLoc>/services/<streamWebFolder>`, and is still keyed
23
+ * by its folder. Republishing it from a current bundle moves it to `apps/<name>`.
15
24
  *
16
25
  * The service is the decisions, and a host supplies the storage. That split is
17
26
  * the point of this package: sasjs/server keeps the index in a JSON file beside
18
27
  * its drive, the browser build keeps it in the drive it already has, and both
19
28
  * answer the same listing because both call this file.
20
29
  *
21
- * One app per folder. Publishing a folder that is already published is the same
22
- * app published again: it keeps its id (so its URL survives a redeploy) and takes
23
- * the values the new publish states - except the label, which carries over when
24
- * the new publish does not name one, so a redeploy that has forgotten what the
25
- * app is called does not rename it to its folder. Nothing is derived from the
26
- * path, so a capital, a space or a `..` in a folder name is the drive's business
27
- * rather than an identity question.
30
+ * Publishing an app that is already published is the same app published again: it
31
+ * keeps its id (so its URL survives a redeploy) and takes the values the new
32
+ * publish states - except the label, which carries over when the new publish does
33
+ * not name one, so a redeploy that has forgotten what the app is called does not
34
+ * rename it.
28
35
  */
29
36
  const types_js_1 = require("../types.js");
30
37
  const identity_js_1 = require("./identity.js");
38
+ /**
39
+ * Where a frontend lives in the drive: one reserved folder, so an app's files are
40
+ * found by its name alone and nothing has to be recorded about the location.
41
+ */
42
+ exports.APP_FOLDERS_ROOT = 'apps';
43
+ /** The drive folder holding an app's frontend. */
44
+ const frontendFolderOf = (name) => `/${exports.APP_FOLDERS_ROOT}/${name}`;
45
+ exports.frontendFolderOf = frontendFolderOf;
31
46
  /** What a publish answers: enough for a client to print the app's URL. */
32
47
  const appSummary = (entry) => ({
33
48
  id: entry.id,
@@ -95,51 +110,94 @@ const hasStreamFolder = (tree, streamWebFolder) => {
95
110
  children.some((member) => isFolder(member, streamWebFolder)));
96
111
  };
97
112
  exports.hasStreamFolder = hasStreamFolder;
113
+ /**
114
+ * Whether a deploy tree is a frontend: something to open.
115
+ *
116
+ * A frontend is served from its folder root, so the root has to hold the page a
117
+ * browser opens. Without it the publish would advertise an app that answers `404`
118
+ * to every request - a tile that opens nothing, which is worse than a refusal that
119
+ * names what is missing.
120
+ */
121
+ const hasFrontendRoot = (tree) => {
122
+ const members = tree?.members;
123
+ if (!Array.isArray(members))
124
+ return false;
125
+ return members.some((member) => {
126
+ const entry = member;
127
+ return entry?.type === 'file' && entry?.name === 'index.html';
128
+ });
129
+ };
130
+ exports.hasFrontendRoot = hasFrontendRoot;
98
131
  /**
99
132
  * A publish request, checked and normalised, with no default applied.
100
133
  *
101
134
  * Separate from `publish` so a host can refuse a publish BEFORE it writes the
102
135
  * deployment: the drive and the index are two writes, and a request the index
103
136
  * would refuse must not leave the files behind. `name` is left absent when the
104
- * caller did not state one, because a redeploy inherits the label it had.
137
+ * caller did not state one, because a legacy redeploy inherits the label it had.
138
+ *
139
+ * Two shapes, told apart by `streamWebFolder`:
140
+ *
141
+ * - **legacy** - `streamWebFolder` and `appLoc` stated: the web assets are at
142
+ * `<appLoc>/services/<streamWebFolder>`, which is where the tree has to carry
143
+ * them. This is what an older CLI deploys, and it stays exactly as it was.
144
+ * - **frontend** - no `streamWebFolder`: the app is `apps/<name>`, so the name is
145
+ * required and is validated as the folder it now is. `appLoc` is optional
146
+ * metadata naming the backend.
105
147
  *
106
- * The tree is optional so that a caller holding only the request can still check
107
- * it; when it is stated, the folder the app would be served from has to be in it.
148
+ * The tree is optional so a caller holding only the request can still check it.
108
149
  */
109
150
  const checkPublishRequest = (request, tree, options = {}) => {
110
- const appLoc = appLocOf(request.appLoc);
111
- // the drive root is not a folder an app can live in: a deploy to `/` writes
112
- // `//index.html`, which nothing can read, so it is refused rather than indexed
113
- if (appLoc === '/')
114
- throw new types_js_1.ApiError(400, 'An app cannot be published at the drive root.');
115
151
  const logo = logoOf(request.logo);
116
152
  const frameAncestors = frameAncestorsOf(request.frameAncestors);
117
- const streamWebFolder = webFolderOf(request.streamWebFolder);
118
- // the tree has to carry the folder the app is served from, or the publish
119
- // advertises an app whose every request is a `404`. The advice names the source
120
- // of the folder: a caller whose bundle stated it cannot omit the field, and the
121
- // bundle is the thing to correct.
122
- if (tree !== undefined && !(0, exports.hasStreamFolder)(tree, streamWebFolder))
123
- 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
124
- ? 'correct the folder sasjs.app.json names.'
125
- : 'omit streamWebFolder.'}`);
153
+ const timing = request.publishedAt === undefined
154
+ ? {}
155
+ : { publishedAt: request.publishedAt };
156
+ if (request.streamWebFolder !== undefined) {
157
+ if (request.appLoc === undefined)
158
+ throw new types_js_1.ApiError(400, '"appLoc" is required when "streamWebFolder" is stated: the entry keeps its web assets at <appLoc>/services/<streamWebFolder>, so both halves are needed to say where they are.');
159
+ const appLoc = appLocOf(request.appLoc);
160
+ // the drive root is not a folder an app can live in: a deploy to `/` writes
161
+ // `//index.html`, which nothing can read, so it is refused rather than indexed
162
+ if (appLoc === '/')
163
+ throw new types_js_1.ApiError(400, 'An app cannot be published at the drive root.');
164
+ const streamWebFolder = webFolderOf(request.streamWebFolder);
165
+ // the tree has to carry the folder the app is served from, or the publish
166
+ // advertises an app whose every request is a `404`. The advice names the source
167
+ // of the folder: a caller whose bundle stated it cannot omit the field, and the
168
+ // bundle is the thing to correct.
169
+ if (tree !== undefined && !(0, exports.hasStreamFolder)(tree, streamWebFolder))
170
+ 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
171
+ ? 'correct the folder sasjs.app.json names.'
172
+ : 'omit streamWebFolder.'}`);
173
+ return {
174
+ appLoc,
175
+ streamWebFolder,
176
+ ...(request.name === undefined ? {} : { name: nameOf(request.name) }),
177
+ ...(logo === undefined ? {} : { logo }),
178
+ ...(frameAncestors === undefined ? {} : { frameAncestors }),
179
+ ...timing
180
+ };
181
+ }
182
+ // a frontend publish: the name is the folder, so there is no sensible default
183
+ if (request.name === undefined)
184
+ throw new types_js_1.ApiError(400, '"name" is required: an app is published at apps/<name>, so the publish has to say what it is called.');
185
+ const name = nameOf(request.name);
186
+ const appLoc = request.appLoc === undefined ? undefined : appLocOf(request.appLoc);
187
+ if (appLoc === '/')
188
+ throw new types_js_1.ApiError(400, 'An app cannot name the drive root as its backend.');
189
+ if (tree !== undefined && !(0, exports.hasFrontendRoot)(tree))
190
+ throw new types_js_1.ApiError(400, `The deployed tree has no "index.html" at its root, so nothing would be published at apps/${name}. Publish the compiled web folder itself, not the folder that holds it.`);
126
191
  return {
127
- appLoc,
128
- streamWebFolder,
129
- ...(request.name === undefined
130
- ? {}
131
- : { name: nameOf(request.name, appLoc) }),
192
+ name,
193
+ ...(appLoc === undefined ? {} : { appLoc }),
132
194
  ...(logo === undefined ? {} : { logo }),
133
195
  ...(frameAncestors === undefined ? {} : { frameAncestors }),
134
- ...(request.publishedAt === undefined
135
- ? {}
136
- : { publishedAt: request.publishedAt })
196
+ ...timing
137
197
  };
138
198
  };
139
199
  exports.checkPublishRequest = checkPublishRequest;
140
- const nameOf = (value, appLoc) => {
141
- if (value === undefined)
142
- return (0, exports.defaultAppName)(appLoc);
200
+ const nameOf = (value) => {
143
201
  if (typeof value !== 'string' || value.trim() === '')
144
202
  throw new types_js_1.ApiError(400, '"name" must be a non-empty string');
145
203
  return urlSafeName(value.trim());
@@ -225,48 +283,65 @@ const createAppStreamService = (store) => {
225
283
  catch (_) {
226
284
  return undefined;
227
285
  }
228
- const entry = (await store.read()).find((candidate) => sameAppLoc(candidate.appLoc, normalised));
286
+ // legacy entries only: a frontend app names a backend it CALLS, which is not
287
+ // a claim to have been published there
288
+ const entry = (await store.read()).find((candidate) => candidate.streamWebFolder !== undefined &&
289
+ candidate.appLoc !== undefined &&
290
+ sameAppLoc(candidate.appLoc, normalised));
229
291
  return entry ? { ...entry } : undefined;
230
292
  },
231
293
  publish(request) {
232
294
  return mutate(async () => {
233
295
  const checked = (0, exports.checkPublishRequest)(request);
234
- const appLoc = checked.appLoc;
235
- const streamWebFolder = checked.streamWebFolder;
236
296
  const entries = await store.read();
237
- // the folder is the app, so a redeploy is the same app published again:
238
- // it keeps the id its URL depends on, and takes the values this publish
239
- // states rather than the ones the previous publish left behind
240
- const existing = entries.find((entry) => sameAppLoc(entry.appLoc, appLoc));
297
+ const legacy = checked.streamWebFolder !== undefined;
298
+ // A legacy publish is keyed by the folder it deployed to; a frontend
299
+ // publish by its name, which IS its folder. Either way a redeploy finds the
300
+ // entry it published before and keeps its id, which is what its URL
301
+ // depends on. The name is compared exactly: a drive that ignores case would
302
+ // hold one folder for two spellings, so a case-only difference is a clash
303
+ // rather than a redeploy.
304
+ const sameTarget = (candidate) => legacy
305
+ ? candidate.streamWebFolder !== undefined &&
306
+ candidate.appLoc !== undefined &&
307
+ sameAppLoc(candidate.appLoc, checked.appLoc)
308
+ : candidate.streamWebFolder === undefined &&
309
+ candidate.name === checked.name;
310
+ const existing = entries.find(sameTarget);
241
311
  const logo = checked.logo;
242
312
  const frameAncestors = checked.frameAncestors;
243
- // A name this publish STATES is an address, so it is validated as a URL
244
- // segment and has to be free. A name it inherits from the last publish,
245
- // or one derived from the folder, is a label only: it was not chosen
246
- // here, it may predate this rule, and two folders may share a leaf name.
247
- const statedName = checked.name;
248
- const name = statedName !== undefined
249
- ? nameOf(statedName, appLoc)
250
- : (existing?.name ?? nameOf(undefined, appLoc));
251
- if (statedName !== undefined) {
252
- const clash = entries.find((candidate) => !sameAppLoc(candidate.appLoc, appLoc) &&
313
+ // A legacy label is a label: it was not chosen here, it may predate this
314
+ // rule, and two folders may share a leaf name. A frontend name is the
315
+ // folder and the address, so it has to be free.
316
+ const name = legacy
317
+ ? (checked.name ??
318
+ existing?.name ??
319
+ (0, exports.defaultAppName)(checked.appLoc))
320
+ : checked.name;
321
+ // A legacy label is only validated when this publish STATES one; a name it
322
+ // inherits, or one derived from the folder, predates the rule. A frontend
323
+ // name is the folder and the address, so it is always the caller's.
324
+ if (!legacy || checked.name !== undefined) {
325
+ const clash = entries.find((candidate) => candidate !== existing &&
253
326
  candidate.name.toLowerCase() === name.toLowerCase());
254
327
  if (clash)
255
- throw new types_js_1.ApiError(409, `The name "${name}" is already the address of the app published at ${clash.appLoc}. A stated name is how the app opens (/app/${name}), and two apps holding one name would answer an ambiguous lookup with nothing, so publish this one under a different name.`);
328
+ throw new types_js_1.ApiError(409, `The name "${name}" is already the address of the app at ${clash.appLoc ?? (0, exports.frontendFolderOf)(clash.name)}. An app's name is how it opens (/app/${name}), and two apps holding one name would answer an ambiguous lookup with nothing, so publish this one under a different name.`);
256
329
  }
257
330
  const entry = {
258
331
  id: existing ? existing.id : (0, identity_js_1.newAppId)(),
259
332
  // the label is the one field a redeploy inherits: everything else is
260
333
  // what this publish states
261
334
  name,
262
- appLoc,
263
- streamWebFolder,
335
+ ...(checked.appLoc === undefined ? {} : { appLoc: checked.appLoc }),
336
+ ...(checked.streamWebFolder === undefined
337
+ ? {}
338
+ : { streamWebFolder: checked.streamWebFolder }),
264
339
  ...(logo === undefined ? {} : { logo }),
265
340
  ...(frameAncestors === undefined ? {} : { frameAncestors }),
266
341
  publishedAt: checked.publishedAt ?? new Date().toISOString()
267
342
  };
268
343
  await store.write([
269
- ...entries.filter((candidate) => !sameAppLoc(candidate.appLoc, appLoc)),
344
+ ...entries.filter((candidate) => !sameTarget(candidate)),
270
345
  entry
271
346
  ]);
272
347
  return { ...entry };
@@ -20,11 +20,18 @@ const isEntry = (value) => {
20
20
  if (typeof value !== 'object' || value === null)
21
21
  return false;
22
22
  const entry = value;
23
- return (typeof entry.id === 'string' &&
24
- typeof entry.name === 'string' &&
25
- typeof entry.appLoc === 'string' &&
26
- typeof entry.streamWebFolder === 'string' &&
27
- typeof entry.publishedAt === 'string');
23
+ if (typeof entry.id !== 'string' ||
24
+ typeof entry.name !== 'string' ||
25
+ typeof entry.publishedAt !== 'string')
26
+ return false;
27
+ if (entry.appLoc !== undefined && typeof entry.appLoc !== 'string')
28
+ return false;
29
+ if (entry.streamWebFolder !== undefined &&
30
+ typeof entry.streamWebFolder !== 'string')
31
+ return false;
32
+ // the legacy shape states where the assets are, so it needs both halves: a
33
+ // `streamWebFolder` with no `appLoc` names no folder at all
34
+ return !(entry.streamWebFolder !== undefined && entry.appLoc === undefined);
28
35
  };
29
36
  const createDriveAppStreamStore = (drive, filePath = exports.APP_STREAM_INDEX) => ({
30
37
  async read() {
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createAppStreamWebService = exports.cacheControlFor = exports.contentTypeFor = exports.escapeHtml = exports.NO_SUCH_APP = exports.DEFAULT_APP_PATH = void 0;
3
+ exports.createAppStreamWebService = exports.appRootOf = exports.cacheControlFor = exports.contentTypeFor = exports.escapeHtml = exports.NO_SUCH_APP = exports.DEFAULT_APP_PATH = void 0;
4
4
  /**
5
5
  * The app stream as a web surface: the listing, the landing page, the apps, and
6
6
  * the removal of one - decided once for every host.
@@ -22,6 +22,7 @@ exports.createAppStreamWebService = exports.cacheControlFor = exports.contentTyp
22
22
  * and neither the URL nor the lookup has to care.
23
23
  */
24
24
  const types_js_1 = require("../types.js");
25
+ const service_js_1 = require("./service.js");
25
26
  /** Where the surface lives unless a host mounts it elsewhere. */
26
27
  exports.DEFAULT_APP_PATH = '/app';
27
28
  /** The words a caller sees when there is nothing at the address it asked for. */
@@ -112,6 +113,18 @@ const decoded = (segment) => {
112
113
  return undefined;
113
114
  }
114
115
  };
116
+ /**
117
+ * Where an entry's files are read from.
118
+ *
119
+ * A frontend lives in one folder named after the app - `apps/<name>` - so the
120
+ * entry records nothing about it, and moving a frontend is a delete and a publish.
121
+ * A legacy entry states its own location, because that is where its deployment put
122
+ * it: `<appLoc>/services/<streamWebFolder>`.
123
+ */
124
+ const appRootOf = (entry) => entry.streamWebFolder !== undefined && entry.appLoc !== undefined
125
+ ? `${entry.appLoc}/services/${entry.streamWebFolder}`
126
+ : (0, service_js_1.frontendFolderOf)(entry.name);
127
+ exports.appRootOf = appRootOf;
115
128
  const createAppStreamWebService = (appStream, drive, options = {}) => {
116
129
  const base = (options.basePath ?? exports.DEFAULT_APP_PATH).replace(/\/+$/, '');
117
130
  const title = options.title ?? 'SASjs App Stream';
@@ -119,7 +132,7 @@ const createAppStreamWebService = (appStream, drive, options = {}) => {
119
132
  const rowOf = (entry) => ({
120
133
  id: entry.id,
121
134
  name: entry.name,
122
- appLoc: entry.appLoc,
135
+ appLoc: entry.appLoc ?? null,
123
136
  logo: entry.logo === undefined ? null : `${urlOf(entry)}${entry.logo}`,
124
137
  url: urlOf(entry),
125
138
  publishedAt: entry.publishedAt ?? null
@@ -133,7 +146,7 @@ const createAppStreamWebService = (appStream, drive, options = {}) => {
133
146
  const tiles = rows.length
134
147
  ? `<ul class="apps">${rows
135
148
  .map((row) => `<li><a href="${(0, exports.escapeHtml)(row.url)}">${(0, exports.escapeHtml)(row.name)}</a>` +
136
- `<span class="loc">${(0, exports.escapeHtml)(row.appLoc)}</span></li>`)
149
+ `<span class="loc">${(0, exports.escapeHtml)(row.appLoc ?? (0, service_js_1.frontendFolderOf)(row.name))}</span></li>`)
137
150
  .join('')}</ul>`
138
151
  : '<p class="empty">No apps have been published yet.</p>';
139
152
  return html(200, [
@@ -178,7 +191,7 @@ const createAppStreamWebService = (appStream, drive, options = {}) => {
178
191
  return noSuchApp();
179
192
  const relative = parts.join('/') || 'index.html';
180
193
  try {
181
- const file = await drive.read(`${entry.appLoc}/services/${entry.streamWebFolder}/${relative}`);
194
+ const file = await drive.read(`${(0, exports.appRootOf)(entry)}/${relative}`);
182
195
  return {
183
196
  status: 200,
184
197
  body: file.contents,