@sasjs/api 0.39.0 → 0.40.1

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 (38) hide show
  1. package/build/browser.js +513 -234
  2. package/build/cjs/appstream/bundle.js +125 -54
  3. package/build/cjs/appstream/service.js +140 -65
  4. package/build/cjs/appstream/store.js +12 -5
  5. package/build/cjs/appstream/web.js +17 -4
  6. package/build/cjs/corpus/app.js +37 -0
  7. package/build/cjs/corpus/deploy.js +113 -0
  8. package/build/cjs/corpus/runner.js +1 -1
  9. package/build/cjs/host/dispatch.js +59 -11
  10. package/build/cjs/index.js +8 -2
  11. package/build/cjs/routes/generated.js +13 -1
  12. package/build/esm/appstream/bundle.d.ts +56 -12
  13. package/build/esm/appstream/bundle.d.ts.map +1 -1
  14. package/build/esm/appstream/bundle.js +124 -53
  15. package/build/esm/appstream/service.d.ts +51 -11
  16. package/build/esm/appstream/service.d.ts.map +1 -1
  17. package/build/esm/appstream/service.js +137 -64
  18. package/build/esm/appstream/store.d.ts.map +1 -1
  19. package/build/esm/appstream/store.js +12 -5
  20. package/build/esm/appstream/web.d.ts +11 -1
  21. package/build/esm/appstream/web.d.ts.map +1 -1
  22. package/build/esm/appstream/web.js +15 -3
  23. package/build/esm/corpus/app.d.ts.map +1 -1
  24. package/build/esm/corpus/app.js +37 -0
  25. package/build/esm/corpus/deploy.d.ts.map +1 -1
  26. package/build/esm/corpus/deploy.js +113 -0
  27. package/build/esm/corpus/drive.d.ts +9 -0
  28. package/build/esm/corpus/drive.d.ts.map +1 -1
  29. package/build/esm/corpus/runner.d.ts.map +1 -1
  30. package/build/esm/corpus/runner.js +1 -1
  31. package/build/esm/host/dispatch.d.ts.map +1 -1
  32. package/build/esm/host/dispatch.js +60 -12
  33. package/build/esm/index.d.ts +3 -3
  34. package/build/esm/index.d.ts.map +1 -1
  35. package/build/esm/index.js +3 -3
  36. package/build/esm/routes/generated.d.ts.map +1 -1
  37. package/build/esm/routes/generated.js +13 -1
  38. package/package.json +1 -1
@@ -1,32 +1,36 @@
1
1
  /**
2
2
  * An app as one bundle: the deployment and the statement of what it publishes.
3
3
  *
4
- * A deploy writes a tree to a folder, and a tree of an app already holds
5
- * everything the app is - its services under `services/`, the compiled web assets
6
- * under `services/<streamWebFolder>`, and whatever jobs and data files it was
7
- * built with. What a tree cannot say is that the folder IS an app: which of its
8
- * folders holds the web build, what it should be called, and whether it may be
9
- * framed. That statement is a small file at the root of the archive:
4
+ * Two shapes travel through here, and the difference is where the frontend sits.
10
5
  *
11
- * sasjs.app.json
12
- * services/
13
- * web/ the compiled app: index.html, main.<hash>.js, assets/
14
- * build.sas the services the app calls
15
- * jobs/
6
+ * **A package** is what a current `sasjs build` emits for a server: a frontend, an
7
+ * optional backend, and a manifest saying which app the frontend is.
16
8
  *
17
- * So one zip deploys the app and the folders under it in a single request, and
18
- * the app is indexed as part of that deploy rather than by a second call that can
19
- * fail on its own. The manifest is consumed here: it is metadata, not an asset,
20
- * so it is stripped from the tree before the tree is written and never appears in
21
- * the app's served files.
9
+ * sasjs.app.json {"name": "macrodash", "appLoc": "/Public/app/macrodash"}
10
+ * web/ the compiled app: index.html, assets/
11
+ * backend/ optional: the services the app calls, written to appLoc
22
12
  *
23
- * The manifest is optional. A deploy that names `streamWebFolder` in its payload
24
- * publishes without one, which is what an existing client does; a bundle that
25
- * carries a manifest needs no arguments beyond the folder it deploys to.
13
+ * The halves are named, not inferred from a path: `web/` becomes `apps/<name>` and
14
+ * `backend/` becomes `appLoc`, and neither is guessed at from a member's name.
15
+ * One zip therefore deploys a backend and publishes a frontend in a single
16
+ * request, and the frontend can be republished alone by leaving `backend/` out.
17
+ *
18
+ * **A legacy bundle** is the shape before that: one tree deployed to `appLoc`,
19
+ * with the web assets carried at `services/<streamWebFolder>`. It is what every
20
+ * released CLI emits, and it keeps working unchanged - the manifest is optional
21
+ * there because the deploy payload states the stream fields instead.
22
+ *
23
+ * The manifest is metadata, not an asset: it is taken out of the tree before the
24
+ * tree is written and never appears in the app's served files.
26
25
  */
27
26
  import { ApiError } from '../types.js';
27
+ import { decodeBase64 } from '../drive/deploy.js';
28
28
  /** The file, at the root of an app bundle, that says the bundle is an app. */
29
29
  export const APP_MANIFEST = 'sasjs.app.json';
30
+ /** The folder in a package holding the compiled frontend. */
31
+ export const FRONTEND_FOLDER = 'web';
32
+ /** The folder in a package holding the services the frontend calls. */
33
+ export const BACKEND_FOLDER = 'backend';
30
34
  const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
31
35
  const text = (value, field) => {
32
36
  if (value === undefined)
@@ -35,15 +39,24 @@ const text = (value, field) => {
35
39
  throw new ApiError(400, `"${field}" must be a non-empty string in ${APP_MANIFEST}.`);
36
40
  return value.trim();
37
41
  };
38
- /** A manifest, checked field by field, naming the field that is wrong. */
42
+ /**
43
+ * A manifest, checked field by field, naming the field that is wrong.
44
+ *
45
+ * Nothing is required: a package states its name, a legacy bundle states its
46
+ * stream folder, and a deploy that states neither in the manifest states them in
47
+ * its payload. What the manifest states has to be usable, which is what is checked
48
+ * here; whether the request as a whole can publish is `checkPublishRequest`'s
49
+ * question, asked once against the request the two have been composed into.
50
+ */
39
51
  export const parseAppManifest = (value) => {
40
52
  if (!isObject(value))
41
53
  throw new ApiError(400, `${APP_MANIFEST} must be an object.`);
42
- const streamWebFolder = text(value.streamWebFolder, 'streamWebFolder');
43
- if (streamWebFolder === undefined)
44
- throw new ApiError(400, `${APP_MANIFEST} must state "streamWebFolder": the folder under services/ holding the compiled app.`);
45
54
  const name = text(value.name, 'name');
55
+ const appLoc = text(value.appLoc, 'appLoc');
56
+ const streamWebFolder = text(value.streamWebFolder, 'streamWebFolder');
46
57
  const logo = text(value.logo, 'logo');
58
+ if (streamWebFolder !== undefined && /[\\/]/.test(streamWebFolder))
59
+ throw new ApiError(400, `"streamWebFolder" in ${APP_MANIFEST} must be a single path segment.`);
47
60
  const frameAncestors = value.frameAncestors;
48
61
  if (frameAncestors !== undefined &&
49
62
  (typeof frameAncestors !== 'string' || frameAncestors === '') &&
@@ -53,66 +66,124 @@ export const parseAppManifest = (value) => {
53
66
  throw new ApiError(400, `"frameAncestors" in ${APP_MANIFEST} must be a string or a non-empty list of strings.`);
54
67
  return {
55
68
  ...(name === undefined ? {} : { name }),
56
- streamWebFolder,
69
+ ...(appLoc === undefined ? {} : { appLoc }),
70
+ ...(streamWebFolder === undefined ? {} : { streamWebFolder }),
57
71
  ...(logo === undefined ? {} : { logo }),
58
72
  ...(frameAncestors === undefined
59
73
  ? {}
60
74
  : { frameAncestors: frameAncestors })
61
75
  };
62
76
  };
77
+ /** A member that is a folder, by name. */
78
+ const folderNamed = (members, name) => members.find((member) => isObject(member) && member.type === 'folder' && member.name === name);
79
+ /** A folder's own members, as a tree of its contents. */
80
+ const contentsOf = (member) => {
81
+ const members = member?.members;
82
+ return Array.isArray(members)
83
+ ? { members: members }
84
+ : undefined;
85
+ };
63
86
  /**
64
- * The deploy tree split from its manifest.
87
+ * The deploy tree split from its manifest, and from the halves a package names.
65
88
  *
66
89
  * A tree that is not a tree is passed through untouched: the deploy is the thing
67
90
  * that refuses a malformed tree, with the message its own cases pin, and saying
68
91
  * it here as well would be two wordings of one refusal.
92
+ *
93
+ * A bundle is a package when it holds a `web/` folder or a manifest at its root.
94
+ * The frontend is then that folder's contents - which is why a compiled app is
95
+ * zipped as the folder's contents rather than the folder itself - and a `backend/`
96
+ * folder beside it is the tree to write to `appLoc`.
69
97
  */
70
98
  export const splitAppBundle = (tree) => {
71
99
  if (!isObject(tree) || !Array.isArray(tree.members))
72
100
  return { tree: tree };
73
101
  const members = tree.members;
74
102
  const at = members.findIndex((member) => isObject(member) && member.type === 'file' && member.name === APP_MANIFEST);
75
- if (at === -1)
76
- return { tree: tree };
77
- const member = members[at];
78
- if (typeof member.code !== 'string')
79
- throw new ApiError(400, `${APP_MANIFEST} carries no text.`);
80
- let parsed;
81
- try {
82
- parsed = JSON.parse(member.code);
83
- }
84
- catch (error) {
85
- throw new ApiError(400, `${APP_MANIFEST} is not valid JSON: ${error.message}`);
103
+ const withoutManifest = at === -1 ? members : members.filter((_, index) => index !== at);
104
+ let manifest;
105
+ if (at !== -1) {
106
+ const member = members[at];
107
+ if (typeof member.code !== 'string')
108
+ throw new ApiError(400, `${APP_MANIFEST} carries no text.`);
109
+ // a manifest read out of a zip is bytes: an archive records no text/binary
110
+ // distinction, so the tree builder marks every member `base64` and the
111
+ // manifest has to be decoded before it can be parsed
112
+ const source = member.encoding === 'base64'
113
+ ? new TextDecoder().decode(decodeBase64(member.code))
114
+ : member.code;
115
+ let parsed;
116
+ try {
117
+ parsed = JSON.parse(source);
118
+ }
119
+ catch (error) {
120
+ throw new ApiError(400, `${APP_MANIFEST} is not valid JSON: ${error.message}`);
121
+ }
122
+ manifest = parseAppManifest(parsed);
86
123
  }
124
+ const rest = { ...tree, members: withoutManifest };
125
+ const web = folderNamed(withoutManifest, FRONTEND_FOLDER);
126
+ // nothing says this is an app: pass the tree through untouched, so a caller that
127
+ // keeps its own copy sees no change and the deploy is the thing that judges it.
128
+ // A manifest is the only other way out of this branch, so there is none here.
129
+ if (web === undefined && manifest === undefined)
130
+ return { tree: tree };
131
+ // `streamWebFolder` names the older shape, where the web assets sit inside the
132
+ // backend's folder and one tree is deployed to `appLoc`. It decides on its own:
133
+ // a folder called `web` is a folder a deployment may simply contain, and reading
134
+ // it as a package would deploy half a tree and publish the other half.
135
+ if (manifest?.streamWebFolder !== undefined)
136
+ return { tree: rest, manifest };
137
+ // a `web/` folder is the frontend; without one, a manifest still says the tree
138
+ // is an app, and the whole tree is then the app's own files
139
+ const backend = contentsOf(folderNamed(withoutManifest, BACKEND_FOLDER));
87
140
  return {
88
- tree: {
89
- ...tree,
90
- members: members.filter((_, index) => index !== at)
141
+ tree: rest,
142
+ frontend: contentsOf(web) ?? {
143
+ members: withoutManifest.filter((member) => member !== folderNamed(withoutManifest, BACKEND_FOLDER))
91
144
  },
92
- manifest: parseAppManifest(parsed)
145
+ ...(backend === undefined ? {} : { backend }),
146
+ manifest: manifest
93
147
  };
94
148
  };
95
149
  /**
96
150
  * The publish a deploy asks for, or `undefined` when it publishes nothing.
97
151
  *
98
- * The request wins over the bundle where both speak - a caller naming a folder or
99
- * a label is correcting the archive, not repeating it - and the bundle fills what
100
- * the request leaves out, so a deploy that carries a manifest needs neither
101
- * `streamWebFolder` nor `streamServiceName`.
152
+ * The request wins over the bundle where both speak - a caller naming a folder, a
153
+ * name or a label is correcting the archive, not repeating it - and the bundle
154
+ * fills what the request leaves out.
155
+ *
156
+ * A bundle is published as a **legacy** app when either half states a
157
+ * `streamWebFolder`: that field is what puts the web assets inside the backend's
158
+ * folder, and a request that keeps it is asking for the shape it describes. A
159
+ * package publishes as a **frontend** instead, which needs only a name - the
160
+ * folder follows from it.
161
+ *
162
+ * `packaged` says the bundle carries a `web/` folder, so a request that names
163
+ * neither a stream folder nor a name is an app with nothing to call it: the
164
+ * request comes back without a name and `checkPublishRequest` refuses it by name,
165
+ * rather than the deploy writing the frontend nowhere.
102
166
  */
103
- export const publishRequestFor = (deploy, manifest) => {
167
+ export const publishRequestFor = (deploy, manifest, options = {}) => {
104
168
  const streamWebFolder = deploy.streamWebFolder ?? manifest?.streamWebFolder;
105
- if (streamWebFolder === undefined)
106
- return undefined;
107
- const name = deploy.streamServiceName ?? manifest?.name;
169
+ const appLoc = deploy.appLoc ?? manifest?.appLoc;
170
+ const name = deploy.name ?? deploy.streamServiceName ?? manifest?.name;
108
171
  const logo = deploy.streamLogo ?? manifest?.logo;
172
+ const frameAncestors = manifest?.frameAncestors;
173
+ if (streamWebFolder !== undefined)
174
+ return {
175
+ ...(appLoc === undefined ? {} : { appLoc }),
176
+ streamWebFolder,
177
+ ...(name === undefined ? {} : { name }),
178
+ ...(logo === undefined ? {} : { logo }),
179
+ ...(frameAncestors === undefined ? {} : { frameAncestors })
180
+ };
181
+ if (name === undefined && !options.packaged)
182
+ return undefined;
109
183
  return {
110
- appLoc: deploy.appLoc,
111
- streamWebFolder,
112
184
  ...(name === undefined ? {} : { name }),
185
+ ...(appLoc === undefined ? {} : { appLoc }),
113
186
  ...(logo === undefined ? {} : { logo }),
114
- ...(manifest?.frameAncestors === undefined
115
- ? {}
116
- : { frameAncestors: manifest.frameAncestors })
187
+ ...(frameAncestors === undefined ? {} : { frameAncestors })
117
188
  };
118
189
  };
@@ -1,12 +1,26 @@
1
+ /**
2
+ * Where a frontend lives in the drive: one reserved folder, so an app's files are
3
+ * found by its name alone and nothing has to be recorded about the location.
4
+ */
5
+ export declare const APP_FOLDERS_ROOT = "apps";
6
+ /** The drive folder holding an app's frontend. */
7
+ export declare const frontendFolderOf: (name: string) => string;
1
8
  export interface AppStreamEntry {
2
9
  /** The identity: a version 4 UUID, minted once and kept for the life of the app. */
3
10
  id: string;
4
- /** The label a person reads. Not unique, not a key, not in any URL. */
11
+ /** The key: the app's name, its folder under `apps/`, and its address. */
5
12
  name: string;
6
- /** The folder the app was deployed to - the one app one folder rule. */
7
- appLoc: string;
8
- /** The folder holding the web assets, relative to `appLoc/services`. */
9
- streamWebFolder: string;
13
+ /**
14
+ * The backend this frontend calls, when it has one. Metadata: several apps may
15
+ * name the same folder, and a frontend-only app names none.
16
+ */
17
+ appLoc?: string;
18
+ /**
19
+ * The legacy shape, and only the legacy shape: the web assets live at
20
+ * `<appLoc>/services/<streamWebFolder>`. An entry without it is served from
21
+ * `apps/<name>`.
22
+ */
23
+ streamWebFolder?: string;
10
24
  /** An asset inside the app, served from the app's own URL. */
11
25
  logo?: string;
12
26
  /** Origins allowed to frame the app, as CSP `frame-ancestors` sources. */
@@ -27,10 +41,19 @@ export interface AppStreamStore {
27
41
  write(entries: AppStreamEntry[]): Promise<void>;
28
42
  }
29
43
  export interface PublishAppRequest {
30
- appLoc: string;
31
- streamWebFolder: string;
32
- /** The label. Defaults to the last segment of `appLoc`. */
44
+ /**
45
+ * The app's name: the key, the folder under `apps/` and the address. Required
46
+ * of a frontend publish, optional for a legacy one (which derives it from the
47
+ * folder).
48
+ */
33
49
  name?: string;
50
+ /** The backend this frontend calls. Optional: a frontend-only app names none. */
51
+ appLoc?: string;
52
+ /**
53
+ * The legacy publish only: the folder under `<appLoc>/services` holding the web
54
+ * assets. Stating it keeps the entry in the legacy shape.
55
+ */
56
+ streamWebFolder?: string;
34
57
  logo?: string;
35
58
  frameAncestors?: string | string[];
36
59
  /** The publish time, for a caller that has a clock of its own (or a test). */
@@ -77,16 +100,33 @@ export interface CheckedPublish extends Omit<PublishAppRequest, 'frameAncestors'
77
100
  * folder.
78
101
  */
79
102
  export declare const hasStreamFolder: (tree: unknown, streamWebFolder: string) => boolean;
103
+ /**
104
+ * Whether a deploy tree is a frontend: something to open.
105
+ *
106
+ * A frontend is served from its folder root, so the root has to hold the page a
107
+ * browser opens. Without it the publish would advertise an app that answers `404`
108
+ * to every request - a tile that opens nothing, which is worse than a refusal that
109
+ * names what is missing.
110
+ */
111
+ export declare const hasFrontendRoot: (tree: unknown) => boolean;
80
112
  /**
81
113
  * A publish request, checked and normalised, with no default applied.
82
114
  *
83
115
  * Separate from `publish` so a host can refuse a publish BEFORE it writes the
84
116
  * deployment: the drive and the index are two writes, and a request the index
85
117
  * would refuse must not leave the files behind. `name` is left absent when the
86
- * caller did not state one, because a redeploy inherits the label it had.
118
+ * caller did not state one, because a legacy redeploy inherits the label it had.
119
+ *
120
+ * Two shapes, told apart by `streamWebFolder`:
121
+ *
122
+ * - **legacy** - `streamWebFolder` and `appLoc` stated: the web assets are at
123
+ * `<appLoc>/services/<streamWebFolder>`, which is where the tree has to carry
124
+ * them. This is what an older CLI deploys, and it stays exactly as it was.
125
+ * - **frontend** - no `streamWebFolder`: the app is `apps/<name>`, so the name is
126
+ * required and is validated as the folder it now is. `appLoc` is optional
127
+ * metadata naming the backend.
87
128
  *
88
- * The tree is optional so that a caller holding only the request can still check
89
- * it; when it is stated, the folder the app would be served from has to be in it.
129
+ * The tree is optional so a caller holding only the request can still check it.
90
130
  */
91
131
  export declare const checkPublishRequest: (request: PublishAppRequest, tree?: unknown, options?: {
92
132
  folderFromManifest?: boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../../src/appstream/service.ts"],"names":[],"mappings":"AA4BA,MAAM,WAAW,cAAc;IAC7B,oFAAoF;IACpF,EAAE,EAAE,MAAM,CAAA;IACV,uEAAuE;IACvE,IAAI,EAAE,MAAM,CAAA;IACZ,wEAAwE;IACxE,MAAM,EAAE,MAAM,CAAA;IACd,wEAAwE;IACxE,eAAe,EAAE,MAAM,CAAA;IACvB,8DAA8D;IAC9D,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,0EAA0E;IAC1E,cAAc,CAAC,EAAE,MAAM,EAAE,CAAA;IACzB,4CAA4C;IAC5C,WAAW,EAAE,MAAM,CAAA;CACpB;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,cAAc;IAC7B,IAAI,IAAI,OAAO,CAAC,cAAc,EAAE,CAAC,CAAA;IACjC,KAAK,CAAC,OAAO,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CAChD;AAED,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,MAAM,CAAA;IACd,eAAe,EAAE,MAAM,CAAA;IACvB,2DAA2D;IAC3D,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,cAAc,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;IAClC,8EAA8E;IAC9E,WAAW,CAAC,EAAE,MAAM,CAAA;CACrB;AAED,MAAM,WAAW,gBAAgB;IAC/B,gEAAgE;IAChE,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,cAAc,KAAK,OAAO,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC,CAAA;IAC3E,wEAAwE;IACxE,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,CAAA;IACxC,mEAAmE;IACnE,SAAS,CAA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Ac;IACzD,OAAO,CAAC,OAAO,CAAkB;gBAErB,OAAO,GAAE,cAAc,EAAO;IAIpC,IAAI,IAAI,OAAO,CAAC,cAAc,EAAE,CAAC;IAIjC,KAAK,CAAC,OAAO,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;CAGtD"}
@@ -1,30 +1,44 @@
1
1
  /**
2
2
  * The app index: what has been published, keyed by a UUID.
3
3
  *
4
- * An app is one deployed folder (`appLoc`), the web assets inside it
5
- * (`appLoc/services/<streamWebFolder>`) and a UUID assigned when it is first
6
- * published. The id is the key the routes, the listing and the permission rules
7
- * point at, and it is what a redeploy keeps. The name is a label a person reads
8
- * AND - when exactly one app holds it - an address: `/app/<name>` opens the app,
9
- * so a name the caller states is validated as a URL segment and has to be free.
10
- * A label derived from the folder carries no such rule, because it was not
11
- * chosen: two folders may share a leaf name and both stay openable by id.
4
+ * An app is a **frontend**: a named folder under `apps/` in the drive, and a UUID
5
+ * assigned when it is first published. The id is the key the routes, the listing
6
+ * and the permission rules point at, and it is what a redeploy keeps. The name is
7
+ * the app's address as well as its label - `/app/<name>` opens it and
8
+ * `apps/<name>` holds it - so a name is validated as a URL segment and has to be
9
+ * free.
10
+ *
11
+ * A backend is metadata. An app may name the `appLoc` whose services its frontend
12
+ * calls, and several apps may name the same one: a frontend is what an app is,
13
+ * and where its services live is a fact about the app rather than its identity.
14
+ * The backend's files are provisioned by a publish that carries them, which is a
15
+ * separate, optional half of a bundle.
16
+ *
17
+ * One legacy shape survives, and only so that a deployment made by an older CLI
18
+ * keeps serving: an entry carrying `streamWebFolder` keeps its web assets where
19
+ * that shape put them, `<appLoc>/services/<streamWebFolder>`, and is still keyed
20
+ * by its folder. Republishing it from a current bundle moves it to `apps/<name>`.
12
21
  *
13
22
  * The service is the decisions, and a host supplies the storage. That split is
14
23
  * the point of this package: sasjs/server keeps the index in a JSON file beside
15
24
  * its drive, the browser build keeps it in the drive it already has, and both
16
25
  * answer the same listing because both call this file.
17
26
  *
18
- * One app per folder. Publishing a folder that is already published is the same
19
- * app published again: it keeps its id (so its URL survives a redeploy) and takes
20
- * the values the new publish states - except the label, which carries over when
21
- * the new publish does not name one, so a redeploy that has forgotten what the
22
- * app is called does not rename it to its folder. Nothing is derived from the
23
- * path, so a capital, a space or a `..` in a folder name is the drive's business
24
- * rather than an identity question.
27
+ * Publishing an app that is already published is the same app published again: it
28
+ * keeps its id (so its URL survives a redeploy) and takes the values the new
29
+ * publish states - except the label, which carries over when the new publish does
30
+ * not name one, so a redeploy that has forgotten what the app is called does not
31
+ * rename it.
25
32
  */
26
33
  import { ApiError } from '../types.js';
27
34
  import { newAppId, normaliseAppLoc } from './identity.js';
35
+ /**
36
+ * Where a frontend lives in the drive: one reserved folder, so an app's files are
37
+ * found by its name alone and nothing has to be recorded about the location.
38
+ */
39
+ export const APP_FOLDERS_ROOT = 'apps';
40
+ /** The drive folder holding an app's frontend. */
41
+ export const frontendFolderOf = (name) => `/${APP_FOLDERS_ROOT}/${name}`;
28
42
  /** What a publish answers: enough for a client to print the app's URL. */
29
43
  export const appSummary = (entry) => ({
30
44
  id: entry.id,
@@ -89,50 +103,92 @@ export const hasStreamFolder = (tree, streamWebFolder) => {
89
103
  return (Array.isArray(children) &&
90
104
  children.some((member) => isFolder(member, streamWebFolder)));
91
105
  };
106
+ /**
107
+ * Whether a deploy tree is a frontend: something to open.
108
+ *
109
+ * A frontend is served from its folder root, so the root has to hold the page a
110
+ * browser opens. Without it the publish would advertise an app that answers `404`
111
+ * to every request - a tile that opens nothing, which is worse than a refusal that
112
+ * names what is missing.
113
+ */
114
+ export const hasFrontendRoot = (tree) => {
115
+ const members = tree?.members;
116
+ if (!Array.isArray(members))
117
+ return false;
118
+ return members.some((member) => {
119
+ const entry = member;
120
+ return entry?.type === 'file' && entry?.name === 'index.html';
121
+ });
122
+ };
92
123
  /**
93
124
  * A publish request, checked and normalised, with no default applied.
94
125
  *
95
126
  * Separate from `publish` so a host can refuse a publish BEFORE it writes the
96
127
  * deployment: the drive and the index are two writes, and a request the index
97
128
  * would refuse must not leave the files behind. `name` is left absent when the
98
- * caller did not state one, because a redeploy inherits the label it had.
129
+ * caller did not state one, because a legacy redeploy inherits the label it had.
130
+ *
131
+ * Two shapes, told apart by `streamWebFolder`:
132
+ *
133
+ * - **legacy** - `streamWebFolder` and `appLoc` stated: the web assets are at
134
+ * `<appLoc>/services/<streamWebFolder>`, which is where the tree has to carry
135
+ * them. This is what an older CLI deploys, and it stays exactly as it was.
136
+ * - **frontend** - no `streamWebFolder`: the app is `apps/<name>`, so the name is
137
+ * required and is validated as the folder it now is. `appLoc` is optional
138
+ * metadata naming the backend.
99
139
  *
100
- * The tree is optional so that a caller holding only the request can still check
101
- * it; when it is stated, the folder the app would be served from has to be in it.
140
+ * The tree is optional so a caller holding only the request can still check it.
102
141
  */
103
142
  export const checkPublishRequest = (request, tree, options = {}) => {
104
- const appLoc = appLocOf(request.appLoc);
105
- // the drive root is not a folder an app can live in: a deploy to `/` writes
106
- // `//index.html`, which nothing can read, so it is refused rather than indexed
107
- if (appLoc === '/')
108
- throw new ApiError(400, 'An app cannot be published at the drive root.');
109
143
  const logo = logoOf(request.logo);
110
144
  const frameAncestors = frameAncestorsOf(request.frameAncestors);
111
- const streamWebFolder = webFolderOf(request.streamWebFolder);
112
- // the tree has to carry the folder the app is served from, or the publish
113
- // advertises an app whose every request is a `404`. The advice names the source
114
- // of the folder: a caller whose bundle stated it cannot omit the field, and the
115
- // bundle is the thing to correct.
116
- if (tree !== undefined && !hasStreamFolder(tree, streamWebFolder))
117
- throw new 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
118
- ? 'correct the folder sasjs.app.json names.'
119
- : 'omit streamWebFolder.'}`);
145
+ const timing = request.publishedAt === undefined
146
+ ? {}
147
+ : { publishedAt: request.publishedAt };
148
+ if (request.streamWebFolder !== undefined) {
149
+ if (request.appLoc === undefined)
150
+ throw new 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.');
151
+ const appLoc = appLocOf(request.appLoc);
152
+ // the drive root is not a folder an app can live in: a deploy to `/` writes
153
+ // `//index.html`, which nothing can read, so it is refused rather than indexed
154
+ if (appLoc === '/')
155
+ throw new ApiError(400, 'An app cannot be published at the drive root.');
156
+ const streamWebFolder = webFolderOf(request.streamWebFolder);
157
+ // the tree has to carry the folder the app is served from, or the publish
158
+ // advertises an app whose every request is a `404`. The advice names the source
159
+ // of the folder: a caller whose bundle stated it cannot omit the field, and the
160
+ // bundle is the thing to correct.
161
+ if (tree !== undefined && !hasStreamFolder(tree, streamWebFolder))
162
+ throw new 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
163
+ ? 'correct the folder sasjs.app.json names.'
164
+ : 'omit streamWebFolder.'}`);
165
+ return {
166
+ appLoc,
167
+ streamWebFolder,
168
+ ...(request.name === undefined ? {} : { name: nameOf(request.name) }),
169
+ ...(logo === undefined ? {} : { logo }),
170
+ ...(frameAncestors === undefined ? {} : { frameAncestors }),
171
+ ...timing
172
+ };
173
+ }
174
+ // a frontend publish: the name is the folder, so there is no sensible default
175
+ if (request.name === undefined)
176
+ throw new ApiError(400, '"name" is required: an app is published at apps/<name>, so the publish has to say what it is called.');
177
+ const name = nameOf(request.name);
178
+ const appLoc = request.appLoc === undefined ? undefined : appLocOf(request.appLoc);
179
+ if (appLoc === '/')
180
+ throw new ApiError(400, 'An app cannot name the drive root as its backend.');
181
+ if (tree !== undefined && !hasFrontendRoot(tree))
182
+ throw new 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.`);
120
183
  return {
121
- appLoc,
122
- streamWebFolder,
123
- ...(request.name === undefined
124
- ? {}
125
- : { name: nameOf(request.name, appLoc) }),
184
+ name,
185
+ ...(appLoc === undefined ? {} : { appLoc }),
126
186
  ...(logo === undefined ? {} : { logo }),
127
187
  ...(frameAncestors === undefined ? {} : { frameAncestors }),
128
- ...(request.publishedAt === undefined
129
- ? {}
130
- : { publishedAt: request.publishedAt })
188
+ ...timing
131
189
  };
132
190
  };
133
- const nameOf = (value, appLoc) => {
134
- if (value === undefined)
135
- return defaultAppName(appLoc);
191
+ const nameOf = (value) => {
136
192
  if (typeof value !== 'string' || value.trim() === '')
137
193
  throw new ApiError(400, '"name" must be a non-empty string');
138
194
  return urlSafeName(value.trim());
@@ -218,48 +274,65 @@ export const createAppStreamService = (store) => {
218
274
  catch (_) {
219
275
  return undefined;
220
276
  }
221
- const entry = (await store.read()).find((candidate) => sameAppLoc(candidate.appLoc, normalised));
277
+ // legacy entries only: a frontend app names a backend it CALLS, which is not
278
+ // a claim to have been published there
279
+ const entry = (await store.read()).find((candidate) => candidate.streamWebFolder !== undefined &&
280
+ candidate.appLoc !== undefined &&
281
+ sameAppLoc(candidate.appLoc, normalised));
222
282
  return entry ? { ...entry } : undefined;
223
283
  },
224
284
  publish(request) {
225
285
  return mutate(async () => {
226
286
  const checked = checkPublishRequest(request);
227
- const appLoc = checked.appLoc;
228
- const streamWebFolder = checked.streamWebFolder;
229
287
  const entries = await store.read();
230
- // the folder is the app, so a redeploy is the same app published again:
231
- // it keeps the id its URL depends on, and takes the values this publish
232
- // states rather than the ones the previous publish left behind
233
- const existing = entries.find((entry) => sameAppLoc(entry.appLoc, appLoc));
288
+ const legacy = checked.streamWebFolder !== undefined;
289
+ // A legacy publish is keyed by the folder it deployed to; a frontend
290
+ // publish by its name, which IS its folder. Either way a redeploy finds the
291
+ // entry it published before and keeps its id, which is what its URL
292
+ // depends on. The name is compared exactly: a drive that ignores case would
293
+ // hold one folder for two spellings, so a case-only difference is a clash
294
+ // rather than a redeploy.
295
+ const sameTarget = (candidate) => legacy
296
+ ? candidate.streamWebFolder !== undefined &&
297
+ candidate.appLoc !== undefined &&
298
+ sameAppLoc(candidate.appLoc, checked.appLoc)
299
+ : candidate.streamWebFolder === undefined &&
300
+ candidate.name === checked.name;
301
+ const existing = entries.find(sameTarget);
234
302
  const logo = checked.logo;
235
303
  const frameAncestors = checked.frameAncestors;
236
- // A name this publish STATES is an address, so it is validated as a URL
237
- // segment and has to be free. A name it inherits from the last publish,
238
- // or one derived from the folder, is a label only: it was not chosen
239
- // here, it may predate this rule, and two folders may share a leaf name.
240
- const statedName = checked.name;
241
- const name = statedName !== undefined
242
- ? nameOf(statedName, appLoc)
243
- : (existing?.name ?? nameOf(undefined, appLoc));
244
- if (statedName !== undefined) {
245
- const clash = entries.find((candidate) => !sameAppLoc(candidate.appLoc, appLoc) &&
304
+ // A legacy label is a label: it was not chosen here, it may predate this
305
+ // rule, and two folders may share a leaf name. A frontend name is the
306
+ // folder and the address, so it has to be free.
307
+ const name = legacy
308
+ ? (checked.name ??
309
+ existing?.name ??
310
+ defaultAppName(checked.appLoc))
311
+ : checked.name;
312
+ // A legacy label is only validated when this publish STATES one; a name it
313
+ // inherits, or one derived from the folder, predates the rule. A frontend
314
+ // name is the folder and the address, so it is always the caller's.
315
+ if (!legacy || checked.name !== undefined) {
316
+ const clash = entries.find((candidate) => candidate !== existing &&
246
317
  candidate.name.toLowerCase() === name.toLowerCase());
247
318
  if (clash)
248
- throw new 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.`);
319
+ throw new ApiError(409, `The name "${name}" is already the address of the app at ${clash.appLoc ?? 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.`);
249
320
  }
250
321
  const entry = {
251
322
  id: existing ? existing.id : newAppId(),
252
323
  // the label is the one field a redeploy inherits: everything else is
253
324
  // what this publish states
254
325
  name,
255
- appLoc,
256
- streamWebFolder,
326
+ ...(checked.appLoc === undefined ? {} : { appLoc: checked.appLoc }),
327
+ ...(checked.streamWebFolder === undefined
328
+ ? {}
329
+ : { streamWebFolder: checked.streamWebFolder }),
257
330
  ...(logo === undefined ? {} : { logo }),
258
331
  ...(frameAncestors === undefined ? {} : { frameAncestors }),
259
332
  publishedAt: checked.publishedAt ?? new Date().toISOString()
260
333
  };
261
334
  await store.write([
262
- ...entries.filter((candidate) => !sameAppLoc(candidate.appLoc, appLoc)),
335
+ ...entries.filter((candidate) => !sameTarget(candidate)),
263
336
  entry
264
337
  ]);
265
338
  return { ...entry };
@@ -1 +1 @@
1
- {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../../src/appstream/store.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AACvD,OAAO,KAAK,EAAkB,cAAc,EAAE,MAAM,cAAc,CAAA;AAElE,mFAAmF;AACnF,eAAO,MAAM,gBAAgB,sBAAsB,CAAA;AAgBnD,eAAO,MAAM,yBAAyB,GACpC,OAAO,YAAY,EACnB,WAAU,MAAyB,KAClC,cAsDD,CAAA"}
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../../src/appstream/store.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AACvD,OAAO,KAAK,EAAkB,cAAc,EAAE,MAAM,cAAc,CAAA;AAElE,mFAAmF;AACnF,eAAO,MAAM,gBAAgB,sBAAsB,CAAA;AA4BnD,eAAO,MAAM,yBAAyB,GACpC,OAAO,YAAY,EACnB,WAAU,MAAyB,KAClC,cAsDD,CAAA"}