@sasjs/api 0.24.0 → 0.25.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.
Files changed (47) hide show
  1. package/README.md +10 -0
  2. package/build/browser.js +655 -93
  3. package/build/cjs/appstream/bundle.js +124 -0
  4. package/build/cjs/appstream/identity.js +69 -0
  5. package/build/cjs/appstream/service.js +226 -0
  6. package/build/cjs/appstream/store.js +69 -0
  7. package/build/cjs/appstream/web.js +248 -0
  8. package/build/cjs/corpus/deploy.js +83 -28
  9. package/build/cjs/corpus/runner.js +30 -18
  10. package/build/cjs/host/dispatch.js +38 -9
  11. package/build/cjs/index.js +25 -4
  12. package/build/cjs/routes/generated.js +2 -8
  13. package/build/cjs/routes/overlay.js +2 -2
  14. package/build/esm/appstream/bundle.d.ts +46 -0
  15. package/build/esm/appstream/bundle.d.ts.map +1 -0
  16. package/build/esm/appstream/bundle.js +118 -0
  17. package/build/esm/appstream/identity.d.ts +27 -0
  18. package/build/esm/appstream/identity.d.ts.map +1 -0
  19. package/build/esm/appstream/identity.js +63 -0
  20. package/build/esm/appstream/service.d.ts +96 -0
  21. package/build/esm/appstream/service.d.ts.map +1 -0
  22. package/build/esm/appstream/service.js +218 -0
  23. package/build/esm/appstream/store.d.ts +6 -0
  24. package/build/esm/appstream/store.d.ts.map +1 -0
  25. package/build/esm/appstream/store.js +65 -0
  26. package/build/esm/appstream/web.d.ts +74 -0
  27. package/build/esm/appstream/web.d.ts.map +1 -0
  28. package/build/esm/appstream/web.js +241 -0
  29. package/build/esm/corpus/deploy.d.ts +5 -5
  30. package/build/esm/corpus/deploy.d.ts.map +1 -1
  31. package/build/esm/corpus/deploy.js +83 -28
  32. package/build/esm/corpus/runner.d.ts.map +1 -1
  33. package/build/esm/corpus/runner.js +14 -2
  34. package/build/esm/host/dispatch.d.ts +5 -4
  35. package/build/esm/host/dispatch.d.ts.map +1 -1
  36. package/build/esm/host/dispatch.js +38 -9
  37. package/build/esm/index.d.ts +8 -2
  38. package/build/esm/index.d.ts.map +1 -1
  39. package/build/esm/index.js +5 -1
  40. package/build/esm/routes/generated.d.ts.map +1 -1
  41. package/build/esm/routes/generated.js +2 -8
  42. package/build/esm/routes/overlay.js +2 -2
  43. package/package.json +5 -4
  44. package/build/cjs/drive/registry.js +0 -38
  45. package/build/esm/drive/registry.d.ts +0 -52
  46. package/build/esm/drive/registry.d.ts.map +0 -1
  47. package/build/esm/drive/registry.js +0 -33
@@ -0,0 +1,74 @@
1
+ import type { DriveService } from '../drive/service.js';
2
+ import type { AppStreamEntry, AppStreamService } from './service.js';
3
+ /** Where the surface lives unless a host mounts it elsewhere. */
4
+ export declare const DEFAULT_APP_PATH = "/app";
5
+ /** The words a caller sees when there is nothing at the address it asked for. */
6
+ export declare const NO_SUCH_APP = "There's no App Stream available here.";
7
+ /** One row of the listing: what the interface needs to draw a tile. */
8
+ export interface AppStreamApp {
9
+ id: string;
10
+ name: string;
11
+ appLoc: string;
12
+ /** An asset inside the app, addressed from the app's own URL. */
13
+ logo: string | null;
14
+ /** The app's URL, `/app/<id>/`. */
15
+ url: string;
16
+ publishedAt: string | null;
17
+ }
18
+ /**
19
+ * What the surface answers, in terms a host can render in its own transport: a
20
+ * `Response` in a worker, `res.status().send()` in express.
21
+ */
22
+ export interface AppStreamPage {
23
+ status: number;
24
+ body?: string | Uint8Array;
25
+ contentType?: string;
26
+ headers?: Record<string, string>;
27
+ /** Where a `301` sends the caller. */
28
+ location?: string;
29
+ }
30
+ /** The caller's own rule about what it may see; a host supplies it. */
31
+ export type AppFilter = (entry: AppStreamEntry) => boolean;
32
+ export interface AppStreamRequest {
33
+ method: string;
34
+ /** The request's path, base path included - `/app/<id>/index.html`. */
35
+ path: string;
36
+ allow?: AppFilter;
37
+ }
38
+ export interface AppStreamWebService {
39
+ /** `GET /app/apps.json`. */
40
+ apps(allow?: AppFilter): Promise<{
41
+ apps: AppStreamApp[];
42
+ }>;
43
+ /** `GET /app/`. */
44
+ landing(allow?: AppFilter): Promise<AppStreamPage>;
45
+ /** Everything else the surface answers, by path and method. */
46
+ request(request: AppStreamRequest): Promise<AppStreamPage>;
47
+ }
48
+ export interface AppStreamWebOptions {
49
+ /** Where the surface is mounted. `/app` by default. */
50
+ basePath?: string;
51
+ /** The heading the landing page carries. */
52
+ title?: string;
53
+ }
54
+ /**
55
+ * Text made safe for an attribute or a text node.
56
+ *
57
+ * An app's name and folder are its author's strings, not ours: the previous
58
+ * implementation interpolated them straight into markup, so a folder named
59
+ * `"><script>` was a landing page that ran it.
60
+ */
61
+ export declare const escapeHtml: (value: string) => string;
62
+ /** The type of a file, by extension; bytes when nothing names it. */
63
+ export declare const contentTypeFor: (filePath: string) => string;
64
+ /**
65
+ * Caching for an app's own build output.
66
+ *
67
+ * A bundle names its files after their contents (`main.<hash>.js`), so a hashed
68
+ * name is immutable - a deployment ships a new name, never a new body. Everything
69
+ * else, `index.html` above all, has to be revalidated, or a browser keeps opening
70
+ * the previous deployment's shell against the new bundle.
71
+ */
72
+ export declare const cacheControlFor: (filePath: string) => string;
73
+ export declare const createAppStreamWebService: (appStream: AppStreamService, drive: DriveService, options?: AppStreamWebOptions) => AppStreamWebService;
74
+ //# sourceMappingURL=web.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web.d.ts","sourceRoot":"","sources":["../../../src/appstream/web.ts"],"names":[],"mappings":"AAqBA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AACvD,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AAEpE,iEAAiE;AACjE,eAAO,MAAM,gBAAgB,SAAS,CAAA;AAEtC,iFAAiF;AACjF,eAAO,MAAM,WAAW,0CAA0C,CAAA;AAElE,uEAAuE;AACvE,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,EAAE,MAAM,CAAA;IACd,iEAAiE;IACjE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,GAAI,UAAU,MAAM,KAAG,MAIjD,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,GAAI,UAAU,MAAM,KAAG,MAGT,CAAA;AAmC1C,eAAO,MAAM,yBAAyB,GACpC,WAAW,gBAAgB,EAC3B,OAAO,YAAY,EACnB,UAAS,mBAAwB,KAChC,mBAgLF,CAAA"}
@@ -0,0 +1,241 @@
1
+ /**
2
+ * The app stream as a web surface: the listing, the landing page, the apps, and
3
+ * the removal of one - decided once for every host.
4
+ *
5
+ * This is not part of the REST API document, because it is not a JSON API: a
6
+ * browser opens these URLs, and an app's own files are served from them
7
+ * (`GET /app/<id>/main.js`). What the hosts must agree on is the part that was
8
+ * written twice before: what the listing says, what a missing app answers, how an
9
+ * asset is found inside a deployment, and what is cacheable. So it lives here,
10
+ * and a host mounts it:
11
+ *
12
+ * - sasjs/server mounts it behind its own authentication and permission rules,
13
+ * passing `allow` so a caller sees only the apps it holds a grant on;
14
+ * - the browser build answers it from the drive it already has, so a mock site
15
+ * publishes, lists and serves its apps exactly as the server does.
16
+ *
17
+ * The base path is `/app`. An app is addressed by its id - `/app/<id>/` - which is
18
+ * the whole reason the id is a UUID and not a label: two apps may share a name,
19
+ * and neither the URL nor the lookup has to care.
20
+ */
21
+ import { ApiError } from '../types.js';
22
+ /** Where the surface lives unless a host mounts it elsewhere. */
23
+ export const DEFAULT_APP_PATH = '/app';
24
+ /** The words a caller sees when there is nothing at the address it asked for. */
25
+ export const NO_SUCH_APP = "There's no App Stream available here.";
26
+ const ESCAPES = {
27
+ '&': '&amp;',
28
+ '<': '&lt;',
29
+ '>': '&gt;',
30
+ '"': '&quot;',
31
+ "'": '&#39;'
32
+ };
33
+ /**
34
+ * Text made safe for an attribute or a text node.
35
+ *
36
+ * An app's name and folder are its author's strings, not ours: the previous
37
+ * implementation interpolated them straight into markup, so a folder named
38
+ * `"><script>` was a landing page that ran it.
39
+ */
40
+ export const escapeHtml = (value) => value.replace(/[&<>"']/g, (character) => ESCAPES[character]);
41
+ const CONTENT_TYPES = {
42
+ html: 'text/html; charset=utf-8',
43
+ htm: 'text/html; charset=utf-8',
44
+ js: 'text/javascript; charset=utf-8',
45
+ mjs: 'text/javascript; charset=utf-8',
46
+ css: 'text/css; charset=utf-8',
47
+ json: 'application/json; charset=utf-8',
48
+ map: 'application/json; charset=utf-8',
49
+ txt: 'text/plain; charset=utf-8',
50
+ svg: 'image/svg+xml',
51
+ png: 'image/png',
52
+ jpg: 'image/jpeg',
53
+ jpeg: 'image/jpeg',
54
+ gif: 'image/gif',
55
+ webp: 'image/webp',
56
+ ico: 'image/x-icon',
57
+ woff: 'font/woff',
58
+ woff2: 'font/woff2',
59
+ ttf: 'font/ttf',
60
+ otf: 'font/otf',
61
+ wasm: 'application/wasm',
62
+ pdf: 'application/pdf',
63
+ zip: 'application/zip'
64
+ };
65
+ /** The type of a file, by extension; bytes when nothing names it. */
66
+ export const contentTypeFor = (filePath) => {
67
+ const extension = filePath.slice(filePath.lastIndexOf('.') + 1).toLowerCase();
68
+ return CONTENT_TYPES[extension] ?? 'application/octet-stream';
69
+ };
70
+ /**
71
+ * Caching for an app's own build output.
72
+ *
73
+ * A bundle names its files after their contents (`main.<hash>.js`), so a hashed
74
+ * name is immutable - a deployment ships a new name, never a new body. Everything
75
+ * else, `index.html` above all, has to be revalidated, or a browser keeps opening
76
+ * the previous deployment's shell against the new bundle.
77
+ */
78
+ export const cacheControlFor = (filePath) => /\.[0-9a-f]{8,}\.[A-Za-z0-9]+$/.test(filePath)
79
+ ? 'public, max-age=31536000, immutable'
80
+ : 'public, max-age=0, must-revalidate';
81
+ const html = (status, markup) => ({
82
+ status,
83
+ body: markup,
84
+ contentType: 'text/html; charset=utf-8'
85
+ });
86
+ const json = (status, body) => ({
87
+ status,
88
+ body: JSON.stringify(body),
89
+ contentType: 'application/json; charset=utf-8'
90
+ });
91
+ // our own words, not a caller's: no escaping, and a browser shows the same
92
+ // sentence the server has always answered with
93
+ const noSuchApp = () => html(404, `<!doctype html><html><body>${NO_SUCH_APP}</body></html>`);
94
+ const methodNotAllowed = (allowed) => ({
95
+ status: 405,
96
+ body: JSON.stringify({ status: 'failure', message: `${allowed} only.` }),
97
+ contentType: 'application/json; charset=utf-8',
98
+ headers: { allow: allowed }
99
+ });
100
+ /** A path segment as the caller spelled it, or `undefined` when it cannot be read. */
101
+ const decoded = (segment) => {
102
+ try {
103
+ return decodeURIComponent(segment);
104
+ }
105
+ catch (_) {
106
+ return undefined;
107
+ }
108
+ };
109
+ export const createAppStreamWebService = (appStream, drive, options = {}) => {
110
+ const base = (options.basePath ?? DEFAULT_APP_PATH).replace(/\/+$/, '');
111
+ const title = options.title ?? 'SASjs App Stream';
112
+ const urlOf = (entry) => `${base}/${entry.id}/`;
113
+ const rowOf = (entry) => ({
114
+ id: entry.id,
115
+ name: entry.name,
116
+ appLoc: entry.appLoc,
117
+ logo: entry.logo === undefined ? null : `${urlOf(entry)}${entry.logo}`,
118
+ url: urlOf(entry),
119
+ publishedAt: entry.publishedAt ?? null
120
+ });
121
+ const apps = async (allow) => {
122
+ const entries = await appStream.list(allow);
123
+ return { apps: entries.map(rowOf) };
124
+ };
125
+ const landing = async (allow) => {
126
+ const { apps: rows } = await apps(allow);
127
+ const tiles = rows.length
128
+ ? `<ul class="apps">${rows
129
+ .map((row) => `<li><a href="${escapeHtml(row.url)}">${escapeHtml(row.name)}</a>` +
130
+ `<span class="loc">${escapeHtml(row.appLoc)}</span></li>`)
131
+ .join('')}</ul>`
132
+ : '<p class="empty">No apps have been published yet.</p>';
133
+ return html(200, [
134
+ '<!doctype html><html lang="en"><head><meta charset="utf-8">',
135
+ '<meta name="viewport" content="width=device-width,initial-scale=1">',
136
+ `<title>${escapeHtml(title)}</title>`,
137
+ '<style>',
138
+ 'body{font-family:system-ui,sans-serif;margin:2rem;color:#1f2430}',
139
+ 'h1{font-size:1.4rem}',
140
+ 'ul.apps{list-style:none;padding:0;display:grid;gap:.75rem;max-width:40rem}',
141
+ 'ul.apps li{display:flex;flex-direction:column;gap:.15rem;padding:.75rem 1rem;',
142
+ 'border:1px solid #d8dee9;border-radius:.5rem}',
143
+ 'ul.apps a{font-weight:600;text-decoration:none;color:#0b5cad}',
144
+ '.loc{font-size:.8rem;color:#5b6472;font-family:ui-monospace,monospace}',
145
+ '.empty{color:#5b6472}',
146
+ '</style></head><body>',
147
+ `<h1>${escapeHtml(title)}</h1>`,
148
+ tiles,
149
+ '</body></html>'
150
+ ].join(''));
151
+ };
152
+ /**
153
+ * The app a URL segment names: its id, or - as a courtesy - its label when one
154
+ * app alone holds that label. An ambiguous label resolves to nothing, because
155
+ * guessing which of two same-named apps a URL meant is worse than a 404.
156
+ */
157
+ const resolve = async (segment, allow) => {
158
+ const entries = await appStream.list(allow);
159
+ const byId = entries.find((entry) => entry.id === segment);
160
+ if (byId)
161
+ return byId;
162
+ const byName = entries.filter((entry) => entry.name === segment);
163
+ return byName.length === 1 ? byName[0] : undefined;
164
+ };
165
+ const asset = async (entry, segments) => {
166
+ const parts = segments.map(decoded);
167
+ if (parts.some((part) => part === undefined))
168
+ return noSuchApp();
169
+ const relative = parts.join('/') || 'index.html';
170
+ try {
171
+ const file = await drive.read(`${entry.appLoc}/services/${entry.streamWebFolder}/${relative}`);
172
+ return {
173
+ status: 200,
174
+ body: file.contents,
175
+ contentType: contentTypeFor(relative),
176
+ headers: { 'cache-control': cacheControlFor(relative) }
177
+ };
178
+ }
179
+ catch (error) {
180
+ // a missing file, a path that climbs out of the folder and a folder named
181
+ // where a file belongs are one answer to a browser: nothing is here. A
182
+ // fault is not: reporting a drive that went away as a wrong URL would send
183
+ // the caller looking for a typo that is not there.
184
+ if (error instanceof ApiError &&
185
+ (error.status === 404 || error.status === 400))
186
+ return noSuchApp();
187
+ throw error;
188
+ }
189
+ };
190
+ return {
191
+ apps,
192
+ landing,
193
+ async request({ method, path, allow }) {
194
+ const clean = (path.split(/[?#]/)[0] || '/').replace(/\/{2,}/g, '/');
195
+ const relative = base === ''
196
+ ? clean
197
+ : clean === base
198
+ ? '/'
199
+ : clean.startsWith(`${base}/`)
200
+ ? clean.slice(base.length)
201
+ : null;
202
+ // a host mounted this at a path it does not own: not an app, not a 404
203
+ // about apps either, so it is the same "nothing here" a wrong name earns
204
+ if (relative === null)
205
+ return noSuchApp();
206
+ if (relative === '' || relative === '/' || relative === '/index.html') {
207
+ if (method !== 'GET')
208
+ return methodNotAllowed('GET');
209
+ return landing(allow);
210
+ }
211
+ if (relative === '/apps.json') {
212
+ if (method !== 'GET')
213
+ return methodNotAllowed('GET');
214
+ return json(200, await apps(allow));
215
+ }
216
+ const segments = relative.split('/').filter(Boolean);
217
+ const key = decoded(segments[0]);
218
+ if (key === undefined)
219
+ return noSuchApp();
220
+ if (method === 'DELETE') {
221
+ const entry = await resolve(key, allow);
222
+ // the id is what a removal names; a label is accepted only because it
223
+ // resolves to exactly one app
224
+ if (!entry)
225
+ return noSuchApp();
226
+ const removed = await appStream.unpublish(entry.id);
227
+ return json(200, { status: 'success', app: rowOf(removed) });
228
+ }
229
+ if (method !== 'GET')
230
+ return methodNotAllowed('GET, DELETE');
231
+ const entry = await resolve(key, allow);
232
+ if (!entry)
233
+ return noSuchApp();
234
+ // `/app/<id>` is a folder, and a folder's files resolve under a trailing
235
+ // slash: a relative URL in the page would otherwise resolve one level up
236
+ if (segments.length === 1 && !relative.endsWith('/'))
237
+ return { status: 301, location: urlOf(entry) };
238
+ return asset(entry, segments.slice(1));
239
+ }
240
+ };
241
+ };
@@ -5,11 +5,11 @@
5
5
  * `404` - and each has a case here, which is what
6
6
  * `spec/corpus-parity.spec.ts` enforces in both directions.
7
7
  *
8
- * **The folder is the identity.** An app is one thing to deploy and one thing to
9
- * remove; its name is a label on it. The document requires both body values, so a
10
- * caller must supply both, but only the folder decides what is deleted - and the
11
- * case below with a stale name and the right folder pins exactly that, because a
12
- * name-keyed lookup would answer 404 there and leave the app in place.
8
+ * **The id is the identity.** An app is one thing to deploy and one thing to
9
+ * remove, and the id a publish returns is the handle: a label is shared by design
10
+ * and a folder is where the app was published rather than what it is. The case
11
+ * below removes an app whose label has since changed, by id alone, which a
12
+ * name-keyed or folder-keyed lookup could not do.
13
13
  */
14
14
  import type { CorpusCase } from './drive.js';
15
15
  export declare const DEPLOY_CORPUS: CorpusCase[];
@@ -1 +1 @@
1
- {"version":3,"file":"deploy.d.ts","sourceRoot":"","sources":["../../../src/corpus/deploy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAW5C,eAAO,MAAM,aAAa,EAAE,UAAU,EAgKrC,CAAA"}
1
+ {"version":3,"file":"deploy.d.ts","sourceRoot":"","sources":["../../../src/corpus/deploy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAW5C,eAAO,MAAM,aAAa,EAAE,UAAU,EAyNrC,CAAA"}
@@ -9,46 +9,35 @@ export const DEPLOY_CORPUS = [
9
9
  {
10
10
  id: 'deploy-unpublish',
11
11
  route: 'DELETE /SASjsApi/drive/deploy',
12
- description: 'removes a published app stream and answers the status',
12
+ description: 'removes the app the id names, and says what went',
13
13
  request: {
14
14
  subject: SUBJECT,
15
- body: { streamServiceName: 'myApp', appLoc: '/Public/myApp' }
15
+ body: { id: '6f1c1c1e-6a1e-4c4a-9d1a-1b2c3d4e5f60' }
16
16
  },
17
- expect: { status: 200, body: { status: 'success' } }
18
- },
19
- {
20
- id: 'deploy-unpublish-other-name',
21
- route: 'DELETE /SASjsApi/drive/deploy',
22
- description: 'removes the app by its folder when the name has been renamed',
23
- request: {
24
- subject: SUBJECT,
25
- body: { streamServiceName: 'renamed-app', appLoc: '/Public/myApp' }
26
- },
27
- expect: { status: 200, body: { status: 'success' } }
28
- },
29
- {
30
- id: 'deploy-unpublish-no-stream-name',
31
- route: 'DELETE /SASjsApi/drive/deploy',
32
- description: 'refuses a body that does not say which stream to remove',
33
- request: { subject: SUBJECT, body: { appLoc: '/Public/myApp' } },
34
- expect: { status: 400, error: '"streamServiceName" is required' }
17
+ expect: {
18
+ status: 200,
19
+ partial: {
20
+ status: 'success',
21
+ app: { id: '6f1c1c1e-6a1e-4c4a-9d1a-1b2c3d4e5f60', name: 'myApp' }
22
+ }
23
+ }
35
24
  },
36
25
  {
37
- id: 'deploy-unpublish-no-app-loc',
26
+ id: 'deploy-unpublish-no-id',
38
27
  route: 'DELETE /SASjsApi/drive/deploy',
39
- description: 'refuses a body that does not say which folder to remove',
40
- request: { subject: SUBJECT, body: { streamServiceName: 'myApp' } },
41
- expect: { status: 400, error: '"appLoc" is required' }
28
+ description: 'refuses a body that does not say which app to remove',
29
+ request: { subject: SUBJECT, body: {} },
30
+ expect: { status: 400, error: '"id" is required' }
42
31
  },
43
32
  {
44
- id: 'deploy-unpublish-unknown-folder',
33
+ id: 'deploy-unpublish-unknown-id',
45
34
  route: 'DELETE /SASjsApi/drive/deploy',
46
- description: 'answers 404 for a folder nothing is published at',
35
+ description: 'answers 404 for an id the index does not hold',
47
36
  request: {
48
37
  subject: SUBJECT,
49
- body: { streamServiceName: 'myApp', appLoc: '/Public/not-an-app' }
38
+ body: { id: '11111111-1111-4111-8111-111111111111' }
50
39
  },
51
- expect: { status: 404, error: 'No app is published at that folder.' }
40
+ expect: { status: 404, error: 'No app is published with that id.' }
52
41
  },
53
42
  // ------------------------------------------- the uploaded-archive route
54
43
  // The archive is expanded by the host, so what arrives here is the same tree
@@ -141,6 +130,72 @@ export const DEPLOY_CORPUS = [
141
130
  error: 'The file tree exceeds the deploy limit: 10001 entries (maximum 10000).'
142
131
  }
143
132
  },
133
+ // ------------------------------------------- a bundle that is an app
134
+ // One deploy writes the tree and indexes the app, from the manifest at the
135
+ // root of the archive. The id is minted, so the case pins what can be pinned:
136
+ // the app exists, under the label the manifest states.
137
+ {
138
+ id: 'deploy-publishes-an-app',
139
+ route: 'POST /SASjsApi/drive/deploy',
140
+ description: 'indexes a bundle as an app from the manifest it carries, in one request',
141
+ request: {
142
+ subject: SUBJECT,
143
+ body: {
144
+ appLoc: '/Public/apps/mario',
145
+ fileTree: {
146
+ members: [
147
+ {
148
+ name: 'sasjs.app.json',
149
+ type: 'file',
150
+ code: JSON.stringify({
151
+ name: 'Mario',
152
+ streamWebFolder: 'web',
153
+ logo: 'logo.png'
154
+ })
155
+ },
156
+ {
157
+ name: 'services',
158
+ type: 'folder',
159
+ members: [
160
+ {
161
+ name: 'web',
162
+ type: 'folder',
163
+ members: [
164
+ { name: 'index.html', type: 'file', code: '<h1>Mario</h1>' }
165
+ ]
166
+ },
167
+ { name: 'run', type: 'service', code: '%put hi;' }
168
+ ]
169
+ }
170
+ ]
171
+ }
172
+ }
173
+ },
174
+ expect: {
175
+ status: 200,
176
+ partial: {
177
+ status: 'success',
178
+ app: { name: 'Mario' }
179
+ }
180
+ }
181
+ },
182
+ {
183
+ id: 'deploy-refuses-a-bad-manifest',
184
+ route: 'POST /SASjsApi/drive/deploy',
185
+ description: 'refuses a bundle whose manifest is not JSON',
186
+ request: {
187
+ subject: SUBJECT,
188
+ body: {
189
+ appLoc: '/Public/apps/bad-manifest',
190
+ fileTree: {
191
+ members: [{ name: 'sasjs.app.json', type: 'file', code: '{not json' }]
192
+ }
193
+ }
194
+ },
195
+ expect: {
196
+ status: 400
197
+ }
198
+ },
144
199
  {
145
200
  id: 'upload-refused-by-store',
146
201
  route: 'POST /SASjsApi/drive/deploy/upload',
@@ -1 +1 @@
1
- {"version":3,"file":"runner.d.ts","sourceRoot":"","sources":["../../../src/corpus/runner.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAE5C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAA;AAsBnD,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,MAAM,CAAA;IACd,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,QAAQ,CAAC,EAAE,MAAM,GAAG,UAAU,CAAA;IAC9B,wDAAwD;IACxD,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAED;;;;;GAKG;AACH,eAAO,MAAM,WAAW,GAAI,UAAU,UAAU,KAAG,QA0ClD,CAAA;AA2BD;;;;GAIG;AACH,eAAO,MAAM,OAAO,GAClB,UAAU,UAAU,EACpB,WAAU,QAAgC,KACzC,OAAO,CAAC,UAAU,CAepB,CAAA"}
1
+ {"version":3,"file":"runner.d.ts","sourceRoot":"","sources":["../../../src/corpus/runner.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAE5C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAA;AAsCnD,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,MAAM,CAAA;IACd,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,QAAQ,CAAC,EAAE,MAAM,GAAG,UAAU,CAAA;IAC9B,wDAAwD;IACxD,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAED;;;;;GAKG;AACH,eAAO,MAAM,WAAW,GAAI,UAAU,UAAU,KAAG,QA0ClD,CAAA;AA2BD;;;;GAIG;AACH,eAAO,MAAM,OAAO,GAClB,UAAU,UAAU,EACpB,WAAU,QAAgC,KACzC,OAAO,CAAC,UAAU,CAepB,CAAA"}
@@ -1,7 +1,7 @@
1
1
  import { dispatch } from '../host/dispatch.js';
2
2
  import { createDriveService } from '../drive/service.js';
3
3
  import { MemoryDriveStore } from '../drive/memory-store.js';
4
- import { MemoryAppStreamRegistry } from '../drive/registry.js';
4
+ import { MemoryAppStreamStore, createAppStreamService } from '../appstream/service.js';
5
5
  import { createIdentityService } from '../identity/service.js';
6
6
  import { createRecordService } from '../identity/records.js';
7
7
  import { createSessionService } from '../session/service.js';
@@ -19,6 +19,18 @@ import { CORPUS_WEB_PAGE } from './home.js';
19
19
  import { MemoryIdentityStore } from '../identity/memory-store.js';
20
20
  import { DEFAULT_IDENTITY } from '../identity/fixture.js';
21
21
  import { isApiError } from '../types.js';
22
+ /**
23
+ * The app every case starts from: one published app, at the folder the unpublish
24
+ * cases name, so a removal has something to remove. Its id is stated rather than
25
+ * minted, because a case that pins "the id that is there" cannot invent one.
26
+ */
27
+ const CORPUS_APP = {
28
+ id: '6f1c1c1e-6a1e-4c4a-9d1a-1b2c3d4e5f60',
29
+ name: 'myApp',
30
+ appLoc: '/Public/myApp',
31
+ streamWebFolder: 'web',
32
+ publishedAt: '2026-01-01T00:00:00.000Z'
33
+ };
22
34
  /**
23
35
  * Services over fresh stores, seeded with the case's files and identity.
24
36
  *
@@ -54,7 +66,7 @@ export const servicesFor = (testCase) => {
54
66
  directory: createDirectoryService(identity, CORPUS_DIRECTORY),
55
67
  identity: createIdentityService(identity),
56
68
  records: createRecordService(identity),
57
- registry: new MemoryAppStreamRegistry(),
69
+ appStream: createAppStreamService(new MemoryAppStreamStore([CORPUS_APP])),
58
70
  session: createSessionService(identity, runner),
59
71
  // no `runTimes` option: the default is the five the document names, so a
60
72
  // case is never refused for a reason that belongs to one host's install
@@ -3,7 +3,7 @@ import type { IdentityService } from '../identity/service.js';
3
3
  import type { RecordService } from '../identity/records.js';
4
4
  import type { SessionService } from '../session/service.js';
5
5
  import type { RuntimeService } from '../runtime/service.js';
6
- import type { AppStreamRegistry } from '../drive/registry.js';
6
+ import type { AppStreamService } from '../appstream/service.js';
7
7
  import type { CodeService } from '../code/service.js';
8
8
  import type { InfoService } from '../info/service.js';
9
9
  import type { AuthConfigService } from '../authconfig/service.js';
@@ -100,10 +100,11 @@ export interface Services {
100
100
  */
101
101
  runtime?: RuntimeService;
102
102
  /**
103
- * The app streams a host has published. Absent, `DELETE /drive/deploy`
104
- * answers 501 - the package cannot know a host's stream registry.
103
+ * The apps a host has published, and their index. Absent, a deploy publishes
104
+ * nothing and `DELETE /drive/deploy` answers 501 - the package can decide what
105
+ * an app is, but only a host can store one.
105
106
  */
106
- registry?: AppStreamRegistry;
107
+ appStream?: AppStreamService;
107
108
  }
108
109
  export declare const dispatch: (services: Services, routeId: string, request?: ApiRequest) => Promise<ApiResponse>;
109
110
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"dispatch.d.ts","sourceRoot":"","sources":["../../../src/host/dispatch.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AACvD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAA;AAC7D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAA;AAC3D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAC3D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAC3D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAA;AAE7D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAA;AACrD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAA;AACrD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAA;AACjE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AACvD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAA;AAC/D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAA;AACrD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAA;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAuBtC,MAAM,WAAW,UAAU;IACzB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;IAC1C,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC9B;;;;OAIG;IACH,MAAM,CAA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IACH,OAAO,CAAC,EAAE,cAAc,CAAA;IACxB;;;;OAIG;IACH,SAAS,CAAC,EAAE,gBAAgB,CAAA;CAC7B;AA+CD,eAAO,MAAM,QAAQ,GACnB,UAAU,QAAQ,EAClB,SAAS,MAAM,EACf,UAAS,UAAe,KACvB,OAAO,CAAC,WAAW,CAirBrB,CAAA;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,SAAS,GAAI,SAAS,MAAM,KAAG,QAO3C,CAAA;AAED,0DAA0D;AAC1D,eAAO,MAAM,cAAc,EAAE,MAAM,EA8DlC,CAAA"}
@@ -1,5 +1,6 @@
1
1
  import { routeById } from '../routes/index.js';
2
- import { unpublishAppStream } from '../drive/registry.js';
2
+ import { appSummary, checkPublishRequest } from '../appstream/service.js';
3
+ import { publishRequestFor, splitAppBundle } from '../appstream/bundle.js';
3
4
  import { ApiError } from '../types.js';
4
5
  import { DEPLOY_SUCCESS } from '../drive/deploy.js';
5
6
  /** The defaults sasjs/server's Client schema applies to an omitted lifetime. */
@@ -20,6 +21,8 @@ const RUN_ROUTES = [
20
21
  'POST /SASjsApi/code/execute',
21
22
  'POST /SASjsApi/code/trigger'
22
23
  ];
24
+ /** A payload value, when the caller actually stated one. */
25
+ const given = (value) => typeof value === 'string' && value !== '' ? value : undefined;
23
26
  /**
24
27
  * The parameters the table marks required, checked before the handler runs.
25
28
  *
@@ -275,18 +278,44 @@ export const dispatch = async (services, routeId, request = {}) => {
275
278
  // expansion, which is why the two share this path rather than reading bytes.
276
279
  case 'POST /SASjsApi/drive/deploy/upload':
277
280
  case 'POST /SASjsApi/drive/deploy': {
278
- // `streamWebFolder` is the host's: publishing an app stream is not a drive
279
- // operation, and a host that has no app stream ignores it.
280
- await drive.deploy(String(body.appLoc ?? ''), body.fileTree);
281
- return { status: 200, body: DEPLOY_SUCCESS };
281
+ // A bundle is one archive: the deployment and - when it carries one - the
282
+ // statement that this folder is an app. The manifest is metadata, so it is
283
+ // taken out of the tree before anything is written.
284
+ const bundle = splitAppBundle(body.fileTree);
285
+ const appLoc = String(body.appLoc ?? '');
286
+ const requested = services.appStream
287
+ ? publishRequestFor({
288
+ appLoc,
289
+ streamWebFolder: given(body.streamWebFolder),
290
+ streamServiceName: given(body.streamServiceName),
291
+ streamLogo: given(body.streamLogo)
292
+ }, bundle.manifest)
293
+ : undefined;
294
+ // the index's own checks run BEFORE the drive is written: a tree the index
295
+ // would refuse must not leave its files behind, which is what happened when
296
+ // the deploy came first - a request answered `400` had already deployed
297
+ const publish = requested === undefined ? undefined : checkPublishRequest(requested);
298
+ await drive.deploy(appLoc, bundle.tree);
299
+ // a deploy of something that is not an app writes files and says so; an app
300
+ // is indexed in the same request, so the two cannot disagree
301
+ if (publish === undefined || !services.appStream)
302
+ return { status: 200, body: DEPLOY_SUCCESS };
303
+ const entry = await services.appStream.publish(publish);
304
+ return {
305
+ status: 200,
306
+ body: { ...DEPLOY_SUCCESS, app: appSummary(entry) }
307
+ };
282
308
  }
283
- case 'DELETE /SASjsApi/drive/deploy':
284
- if (!services.registry)
285
- throw new ApiError(501, `This host has no app stream registry, so it cannot answer ${routeId}.`);
309
+ case 'DELETE /SASjsApi/drive/deploy': {
310
+ if (!services.appStream)
311
+ throw new ApiError(501, `This host has no app stream, so it cannot answer ${routeId}.`);
312
+ // the id is the app: a label is not unique and a folder is not a handle
313
+ const removed = await services.appStream.unpublish(String(body.id ?? ''));
286
314
  return {
287
315
  status: 200,
288
- body: await unpublishAppStream(services.registry, body)
316
+ body: { status: 'success', app: appSummary(removed) }
289
317
  };
318
+ }
290
319
  case 'GET /SASjsApi/drive/filetree':
291
320
  return { status: 200, body: await drive.tree(query._folderPath) };
292
321
  // ------------------------------------------------------------- run times