@sasjs/api 0.23.1 → 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 (55) hide show
  1. package/README.md +10 -0
  2. package/build/browser.js +695 -101
  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 +112 -29
  9. package/build/cjs/corpus/runner.js +30 -18
  10. package/build/cjs/drive/deploy.js +20 -4
  11. package/build/cjs/drive/service.js +2 -2
  12. package/build/cjs/host/dispatch.js +38 -9
  13. package/build/cjs/index.js +25 -4
  14. package/build/cjs/routes/generated.js +2 -8
  15. package/build/cjs/routes/overlay.js +2 -2
  16. package/build/esm/appstream/bundle.d.ts +46 -0
  17. package/build/esm/appstream/bundle.d.ts.map +1 -0
  18. package/build/esm/appstream/bundle.js +118 -0
  19. package/build/esm/appstream/identity.d.ts +27 -0
  20. package/build/esm/appstream/identity.d.ts.map +1 -0
  21. package/build/esm/appstream/identity.js +63 -0
  22. package/build/esm/appstream/service.d.ts +96 -0
  23. package/build/esm/appstream/service.d.ts.map +1 -0
  24. package/build/esm/appstream/service.js +218 -0
  25. package/build/esm/appstream/store.d.ts +6 -0
  26. package/build/esm/appstream/store.d.ts.map +1 -0
  27. package/build/esm/appstream/store.js +65 -0
  28. package/build/esm/appstream/web.d.ts +74 -0
  29. package/build/esm/appstream/web.d.ts.map +1 -0
  30. package/build/esm/appstream/web.js +241 -0
  31. package/build/esm/corpus/deploy.d.ts +5 -5
  32. package/build/esm/corpus/deploy.d.ts.map +1 -1
  33. package/build/esm/corpus/deploy.js +112 -29
  34. package/build/esm/corpus/runner.d.ts.map +1 -1
  35. package/build/esm/corpus/runner.js +14 -2
  36. package/build/esm/drive/deploy.d.ts +41 -2
  37. package/build/esm/drive/deploy.d.ts.map +1 -1
  38. package/build/esm/drive/deploy.js +20 -4
  39. package/build/esm/drive/service.d.ts +2 -1
  40. package/build/esm/drive/service.d.ts.map +1 -1
  41. package/build/esm/drive/service.js +2 -2
  42. package/build/esm/host/dispatch.d.ts +5 -4
  43. package/build/esm/host/dispatch.d.ts.map +1 -1
  44. package/build/esm/host/dispatch.js +38 -9
  45. package/build/esm/index.d.ts +9 -3
  46. package/build/esm/index.d.ts.map +1 -1
  47. package/build/esm/index.js +5 -1
  48. package/build/esm/routes/generated.d.ts.map +1 -1
  49. package/build/esm/routes/generated.js +2 -8
  50. package/build/esm/routes/overlay.js +2 -2
  51. package/package.json +5 -4
  52. package/build/cjs/drive/registry.js +0 -38
  53. package/build/esm/drive/registry.d.ts +0 -52
  54. package/build/esm/drive/registry.d.ts.map +0 -1
  55. package/build/esm/drive/registry.js +0 -33
@@ -0,0 +1,248 @@
1
+ "use strict";
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;
4
+ /**
5
+ * The app stream as a web surface: the listing, the landing page, the apps, and
6
+ * the removal of one - decided once for every host.
7
+ *
8
+ * This is not part of the REST API document, because it is not a JSON API: a
9
+ * browser opens these URLs, and an app's own files are served from them
10
+ * (`GET /app/<id>/main.js`). What the hosts must agree on is the part that was
11
+ * written twice before: what the listing says, what a missing app answers, how an
12
+ * asset is found inside a deployment, and what is cacheable. So it lives here,
13
+ * and a host mounts it:
14
+ *
15
+ * - sasjs/server mounts it behind its own authentication and permission rules,
16
+ * passing `allow` so a caller sees only the apps it holds a grant on;
17
+ * - the browser build answers it from the drive it already has, so a mock site
18
+ * publishes, lists and serves its apps exactly as the server does.
19
+ *
20
+ * The base path is `/app`. An app is addressed by its id - `/app/<id>/` - which is
21
+ * the whole reason the id is a UUID and not a label: two apps may share a name,
22
+ * and neither the URL nor the lookup has to care.
23
+ */
24
+ const types_js_1 = require("../types.js");
25
+ /** Where the surface lives unless a host mounts it elsewhere. */
26
+ exports.DEFAULT_APP_PATH = '/app';
27
+ /** The words a caller sees when there is nothing at the address it asked for. */
28
+ exports.NO_SUCH_APP = "There's no App Stream available here.";
29
+ const ESCAPES = {
30
+ '&': '&amp;',
31
+ '<': '&lt;',
32
+ '>': '&gt;',
33
+ '"': '&quot;',
34
+ "'": '&#39;'
35
+ };
36
+ /**
37
+ * Text made safe for an attribute or a text node.
38
+ *
39
+ * An app's name and folder are its author's strings, not ours: the previous
40
+ * implementation interpolated them straight into markup, so a folder named
41
+ * `"><script>` was a landing page that ran it.
42
+ */
43
+ const escapeHtml = (value) => value.replace(/[&<>"']/g, (character) => ESCAPES[character]);
44
+ exports.escapeHtml = escapeHtml;
45
+ const CONTENT_TYPES = {
46
+ html: 'text/html; charset=utf-8',
47
+ htm: 'text/html; charset=utf-8',
48
+ js: 'text/javascript; charset=utf-8',
49
+ mjs: 'text/javascript; charset=utf-8',
50
+ css: 'text/css; charset=utf-8',
51
+ json: 'application/json; charset=utf-8',
52
+ map: 'application/json; charset=utf-8',
53
+ txt: 'text/plain; charset=utf-8',
54
+ svg: 'image/svg+xml',
55
+ png: 'image/png',
56
+ jpg: 'image/jpeg',
57
+ jpeg: 'image/jpeg',
58
+ gif: 'image/gif',
59
+ webp: 'image/webp',
60
+ ico: 'image/x-icon',
61
+ woff: 'font/woff',
62
+ woff2: 'font/woff2',
63
+ ttf: 'font/ttf',
64
+ otf: 'font/otf',
65
+ wasm: 'application/wasm',
66
+ pdf: 'application/pdf',
67
+ zip: 'application/zip'
68
+ };
69
+ /** The type of a file, by extension; bytes when nothing names it. */
70
+ const contentTypeFor = (filePath) => {
71
+ const extension = filePath.slice(filePath.lastIndexOf('.') + 1).toLowerCase();
72
+ return CONTENT_TYPES[extension] ?? 'application/octet-stream';
73
+ };
74
+ exports.contentTypeFor = contentTypeFor;
75
+ /**
76
+ * Caching for an app's own build output.
77
+ *
78
+ * A bundle names its files after their contents (`main.<hash>.js`), so a hashed
79
+ * name is immutable - a deployment ships a new name, never a new body. Everything
80
+ * else, `index.html` above all, has to be revalidated, or a browser keeps opening
81
+ * the previous deployment's shell against the new bundle.
82
+ */
83
+ const cacheControlFor = (filePath) => /\.[0-9a-f]{8,}\.[A-Za-z0-9]+$/.test(filePath)
84
+ ? 'public, max-age=31536000, immutable'
85
+ : 'public, max-age=0, must-revalidate';
86
+ exports.cacheControlFor = cacheControlFor;
87
+ const html = (status, markup) => ({
88
+ status,
89
+ body: markup,
90
+ contentType: 'text/html; charset=utf-8'
91
+ });
92
+ const json = (status, body) => ({
93
+ status,
94
+ body: JSON.stringify(body),
95
+ contentType: 'application/json; charset=utf-8'
96
+ });
97
+ // our own words, not a caller's: no escaping, and a browser shows the same
98
+ // sentence the server has always answered with
99
+ const noSuchApp = () => html(404, `<!doctype html><html><body>${exports.NO_SUCH_APP}</body></html>`);
100
+ const methodNotAllowed = (allowed) => ({
101
+ status: 405,
102
+ body: JSON.stringify({ status: 'failure', message: `${allowed} only.` }),
103
+ contentType: 'application/json; charset=utf-8',
104
+ headers: { allow: allowed }
105
+ });
106
+ /** A path segment as the caller spelled it, or `undefined` when it cannot be read. */
107
+ const decoded = (segment) => {
108
+ try {
109
+ return decodeURIComponent(segment);
110
+ }
111
+ catch (_) {
112
+ return undefined;
113
+ }
114
+ };
115
+ const createAppStreamWebService = (appStream, drive, options = {}) => {
116
+ const base = (options.basePath ?? exports.DEFAULT_APP_PATH).replace(/\/+$/, '');
117
+ const title = options.title ?? 'SASjs App Stream';
118
+ const urlOf = (entry) => `${base}/${entry.id}/`;
119
+ const rowOf = (entry) => ({
120
+ id: entry.id,
121
+ name: entry.name,
122
+ appLoc: entry.appLoc,
123
+ logo: entry.logo === undefined ? null : `${urlOf(entry)}${entry.logo}`,
124
+ url: urlOf(entry),
125
+ publishedAt: entry.publishedAt ?? null
126
+ });
127
+ const apps = async (allow) => {
128
+ const entries = await appStream.list(allow);
129
+ return { apps: entries.map(rowOf) };
130
+ };
131
+ const landing = async (allow) => {
132
+ const { apps: rows } = await apps(allow);
133
+ const tiles = rows.length
134
+ ? `<ul class="apps">${rows
135
+ .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>`)
137
+ .join('')}</ul>`
138
+ : '<p class="empty">No apps have been published yet.</p>';
139
+ return html(200, [
140
+ '<!doctype html><html lang="en"><head><meta charset="utf-8">',
141
+ '<meta name="viewport" content="width=device-width,initial-scale=1">',
142
+ `<title>${(0, exports.escapeHtml)(title)}</title>`,
143
+ '<style>',
144
+ 'body{font-family:system-ui,sans-serif;margin:2rem;color:#1f2430}',
145
+ 'h1{font-size:1.4rem}',
146
+ 'ul.apps{list-style:none;padding:0;display:grid;gap:.75rem;max-width:40rem}',
147
+ 'ul.apps li{display:flex;flex-direction:column;gap:.15rem;padding:.75rem 1rem;',
148
+ 'border:1px solid #d8dee9;border-radius:.5rem}',
149
+ 'ul.apps a{font-weight:600;text-decoration:none;color:#0b5cad}',
150
+ '.loc{font-size:.8rem;color:#5b6472;font-family:ui-monospace,monospace}',
151
+ '.empty{color:#5b6472}',
152
+ '</style></head><body>',
153
+ `<h1>${(0, exports.escapeHtml)(title)}</h1>`,
154
+ tiles,
155
+ '</body></html>'
156
+ ].join(''));
157
+ };
158
+ /**
159
+ * The app a URL segment names: its id, or - as a courtesy - its label when one
160
+ * app alone holds that label. An ambiguous label resolves to nothing, because
161
+ * guessing which of two same-named apps a URL meant is worse than a 404.
162
+ */
163
+ const resolve = async (segment, allow) => {
164
+ const entries = await appStream.list(allow);
165
+ const byId = entries.find((entry) => entry.id === segment);
166
+ if (byId)
167
+ return byId;
168
+ const byName = entries.filter((entry) => entry.name === segment);
169
+ return byName.length === 1 ? byName[0] : undefined;
170
+ };
171
+ const asset = async (entry, segments) => {
172
+ const parts = segments.map(decoded);
173
+ if (parts.some((part) => part === undefined))
174
+ return noSuchApp();
175
+ const relative = parts.join('/') || 'index.html';
176
+ try {
177
+ const file = await drive.read(`${entry.appLoc}/services/${entry.streamWebFolder}/${relative}`);
178
+ return {
179
+ status: 200,
180
+ body: file.contents,
181
+ contentType: (0, exports.contentTypeFor)(relative),
182
+ headers: { 'cache-control': (0, exports.cacheControlFor)(relative) }
183
+ };
184
+ }
185
+ catch (error) {
186
+ // a missing file, a path that climbs out of the folder and a folder named
187
+ // where a file belongs are one answer to a browser: nothing is here. A
188
+ // fault is not: reporting a drive that went away as a wrong URL would send
189
+ // the caller looking for a typo that is not there.
190
+ if (error instanceof types_js_1.ApiError &&
191
+ (error.status === 404 || error.status === 400))
192
+ return noSuchApp();
193
+ throw error;
194
+ }
195
+ };
196
+ return {
197
+ apps,
198
+ landing,
199
+ async request({ method, path, allow }) {
200
+ const clean = (path.split(/[?#]/)[0] || '/').replace(/\/{2,}/g, '/');
201
+ const relative = base === ''
202
+ ? clean
203
+ : clean === base
204
+ ? '/'
205
+ : clean.startsWith(`${base}/`)
206
+ ? clean.slice(base.length)
207
+ : null;
208
+ // a host mounted this at a path it does not own: not an app, not a 404
209
+ // about apps either, so it is the same "nothing here" a wrong name earns
210
+ if (relative === null)
211
+ return noSuchApp();
212
+ if (relative === '' || relative === '/' || relative === '/index.html') {
213
+ if (method !== 'GET')
214
+ return methodNotAllowed('GET');
215
+ return landing(allow);
216
+ }
217
+ if (relative === '/apps.json') {
218
+ if (method !== 'GET')
219
+ return methodNotAllowed('GET');
220
+ return json(200, await apps(allow));
221
+ }
222
+ const segments = relative.split('/').filter(Boolean);
223
+ const key = decoded(segments[0]);
224
+ if (key === undefined)
225
+ return noSuchApp();
226
+ if (method === 'DELETE') {
227
+ const entry = await resolve(key, allow);
228
+ // the id is what a removal names; a label is accepted only because it
229
+ // resolves to exactly one app
230
+ if (!entry)
231
+ return noSuchApp();
232
+ const removed = await appStream.unpublish(entry.id);
233
+ return json(200, { status: 'success', app: rowOf(removed) });
234
+ }
235
+ if (method !== 'GET')
236
+ return methodNotAllowed('GET, DELETE');
237
+ const entry = await resolve(key, allow);
238
+ if (!entry)
239
+ return noSuchApp();
240
+ // `/app/<id>` is a folder, and a folder's files resolve under a trailing
241
+ // slash: a relative URL in the page would otherwise resolve one level up
242
+ if (segments.length === 1 && !relative.endsWith('/'))
243
+ return { status: 301, location: urlOf(entry) };
244
+ return asset(entry, segments.slice(1));
245
+ }
246
+ };
247
+ };
248
+ exports.createAppStreamWebService = createAppStreamWebService;
@@ -12,46 +12,35 @@ exports.DEPLOY_CORPUS = [
12
12
  {
13
13
  id: 'deploy-unpublish',
14
14
  route: 'DELETE /SASjsApi/drive/deploy',
15
- description: 'removes a published app stream and answers the status',
15
+ description: 'removes the app the id names, and says what went',
16
16
  request: {
17
17
  subject: SUBJECT,
18
- body: { streamServiceName: 'myApp', appLoc: '/Public/myApp' }
18
+ body: { id: '6f1c1c1e-6a1e-4c4a-9d1a-1b2c3d4e5f60' }
19
19
  },
20
- expect: { status: 200, body: { status: 'success' } }
21
- },
22
- {
23
- id: 'deploy-unpublish-other-name',
24
- route: 'DELETE /SASjsApi/drive/deploy',
25
- description: 'removes the app by its folder when the name has been renamed',
26
- request: {
27
- subject: SUBJECT,
28
- body: { streamServiceName: 'renamed-app', appLoc: '/Public/myApp' }
29
- },
30
- expect: { status: 200, body: { status: 'success' } }
31
- },
32
- {
33
- id: 'deploy-unpublish-no-stream-name',
34
- route: 'DELETE /SASjsApi/drive/deploy',
35
- description: 'refuses a body that does not say which stream to remove',
36
- request: { subject: SUBJECT, body: { appLoc: '/Public/myApp' } },
37
- expect: { status: 400, error: '"streamServiceName" is required' }
20
+ expect: {
21
+ status: 200,
22
+ partial: {
23
+ status: 'success',
24
+ app: { id: '6f1c1c1e-6a1e-4c4a-9d1a-1b2c3d4e5f60', name: 'myApp' }
25
+ }
26
+ }
38
27
  },
39
28
  {
40
- id: 'deploy-unpublish-no-app-loc',
29
+ id: 'deploy-unpublish-no-id',
41
30
  route: 'DELETE /SASjsApi/drive/deploy',
42
- description: 'refuses a body that does not say which folder to remove',
43
- request: { subject: SUBJECT, body: { streamServiceName: 'myApp' } },
44
- expect: { status: 400, error: '"appLoc" is required' }
31
+ description: 'refuses a body that does not say which app to remove',
32
+ request: { subject: SUBJECT, body: {} },
33
+ expect: { status: 400, error: '"id" is required' }
45
34
  },
46
35
  {
47
- id: 'deploy-unpublish-unknown-folder',
36
+ id: 'deploy-unpublish-unknown-id',
48
37
  route: 'DELETE /SASjsApi/drive/deploy',
49
- description: 'answers 404 for a folder nothing is published at',
38
+ description: 'answers 404 for an id the index does not hold',
50
39
  request: {
51
40
  subject: SUBJECT,
52
- body: { streamServiceName: 'myApp', appLoc: '/Public/not-an-app' }
41
+ body: { id: '11111111-1111-4111-8111-111111111111' }
53
42
  },
54
- expect: { status: 404, error: 'No app is published at that folder.' }
43
+ expect: { status: 404, error: 'No app is published with that id.' }
55
44
  },
56
45
  // ------------------------------------------- the uploaded-archive route
57
46
  // The archive is expanded by the host, so what arrives here is the same tree
@@ -66,7 +55,35 @@ exports.DEPLOY_CORPUS = [
66
55
  appLoc: '/Public/apps/uploaded',
67
56
  fileTree: {
68
57
  members: [
69
- { name: 'index.html', type: 'file', code: 'PGgxPkhlbGxvPC9oMT4=' }
58
+ {
59
+ name: 'index.html',
60
+ type: 'file',
61
+ code: 'PGgxPkhlbGxvPC9oMT4=',
62
+ encoding: 'base64'
63
+ }
64
+ ]
65
+ }
66
+ }
67
+ },
68
+ expect: {
69
+ status: 200,
70
+ body: {
71
+ status: 'success',
72
+ message: 'Files deployed successfully to @sasjs/server.'
73
+ }
74
+ }
75
+ },
76
+ {
77
+ id: 'upload-text-file',
78
+ route: 'POST /SASjsApi/drive/deploy/upload',
79
+ description: 'writes a file member as its own text when no encoding is stated',
80
+ request: {
81
+ subject: SUBJECT,
82
+ body: {
83
+ appLoc: '/Public/apps/text',
84
+ fileTree: {
85
+ members: [
86
+ { name: 'index.html', type: 'file', code: '<h1>Hello</h1>' }
70
87
  ]
71
88
  }
72
89
  }
@@ -116,6 +133,72 @@ exports.DEPLOY_CORPUS = [
116
133
  error: 'The file tree exceeds the deploy limit: 10001 entries (maximum 10000).'
117
134
  }
118
135
  },
136
+ // ------------------------------------------- a bundle that is an app
137
+ // One deploy writes the tree and indexes the app, from the manifest at the
138
+ // root of the archive. The id is minted, so the case pins what can be pinned:
139
+ // the app exists, under the label the manifest states.
140
+ {
141
+ id: 'deploy-publishes-an-app',
142
+ route: 'POST /SASjsApi/drive/deploy',
143
+ description: 'indexes a bundle as an app from the manifest it carries, in one request',
144
+ request: {
145
+ subject: SUBJECT,
146
+ body: {
147
+ appLoc: '/Public/apps/mario',
148
+ fileTree: {
149
+ members: [
150
+ {
151
+ name: 'sasjs.app.json',
152
+ type: 'file',
153
+ code: JSON.stringify({
154
+ name: 'Mario',
155
+ streamWebFolder: 'web',
156
+ logo: 'logo.png'
157
+ })
158
+ },
159
+ {
160
+ name: 'services',
161
+ type: 'folder',
162
+ members: [
163
+ {
164
+ name: 'web',
165
+ type: 'folder',
166
+ members: [
167
+ { name: 'index.html', type: 'file', code: '<h1>Mario</h1>' }
168
+ ]
169
+ },
170
+ { name: 'run', type: 'service', code: '%put hi;' }
171
+ ]
172
+ }
173
+ ]
174
+ }
175
+ }
176
+ },
177
+ expect: {
178
+ status: 200,
179
+ partial: {
180
+ status: 'success',
181
+ app: { name: 'Mario' }
182
+ }
183
+ }
184
+ },
185
+ {
186
+ id: 'deploy-refuses-a-bad-manifest',
187
+ route: 'POST /SASjsApi/drive/deploy',
188
+ description: 'refuses a bundle whose manifest is not JSON',
189
+ request: {
190
+ subject: SUBJECT,
191
+ body: {
192
+ appLoc: '/Public/apps/bad-manifest',
193
+ fileTree: {
194
+ members: [{ name: 'sasjs.app.json', type: 'file', code: '{not json' }]
195
+ }
196
+ }
197
+ },
198
+ expect: {
199
+ status: 400
200
+ }
201
+ },
119
202
  {
120
203
  id: 'upload-refused-by-store',
121
204
  route: 'POST /SASjsApi/drive/deploy/upload',
@@ -4,24 +4,36 @@ exports.runCase = exports.servicesFor = void 0;
4
4
  const dispatch_js_1 = require("../host/dispatch.js");
5
5
  const service_js_1 = require("../drive/service.js");
6
6
  const memory_store_js_1 = require("../drive/memory-store.js");
7
- const registry_js_1 = require("../drive/registry.js");
8
- const service_js_2 = require("../identity/service.js");
7
+ const service_js_2 = require("../appstream/service.js");
8
+ const service_js_3 = require("../identity/service.js");
9
9
  const records_js_1 = require("../identity/records.js");
10
- const service_js_3 = require("../session/service.js");
11
- const service_js_4 = require("../runtime/service.js");
10
+ const service_js_4 = require("../session/service.js");
11
+ const service_js_5 = require("../runtime/service.js");
12
12
  const memory_runner_js_1 = require("../runtime/memory-runner.js");
13
- const service_js_5 = require("../info/service.js");
14
- const service_js_6 = require("../authconfig/service.js");
15
- const service_js_7 = require("../admin/service.js");
16
- const service_js_8 = require("../directory/service.js");
13
+ const service_js_6 = require("../info/service.js");
14
+ const service_js_7 = require("../authconfig/service.js");
15
+ const service_js_8 = require("../admin/service.js");
16
+ const service_js_9 = require("../directory/service.js");
17
17
  const authconfig_js_1 = require("./authconfig.js");
18
18
  const admin_js_1 = require("./admin.js");
19
19
  const directory_js_1 = require("./directory.js");
20
- const service_js_9 = require("../home/service.js");
20
+ const service_js_10 = require("../home/service.js");
21
21
  const home_js_1 = require("./home.js");
22
22
  const memory_store_js_2 = require("../identity/memory-store.js");
23
23
  const fixture_js_1 = require("../identity/fixture.js");
24
24
  const types_js_1 = require("../types.js");
25
+ /**
26
+ * The app every case starts from: one published app, at the folder the unpublish
27
+ * cases name, so a removal has something to remove. Its id is stated rather than
28
+ * minted, because a case that pins "the id that is there" cannot invent one.
29
+ */
30
+ const CORPUS_APP = {
31
+ id: '6f1c1c1e-6a1e-4c4a-9d1a-1b2c3d4e5f60',
32
+ name: 'myApp',
33
+ appLoc: '/Public/myApp',
34
+ streamWebFolder: 'web',
35
+ publishedAt: '2026-01-01T00:00:00.000Z'
36
+ };
25
37
  /**
26
38
  * Services over fresh stores, seeded with the case's files and identity.
27
39
  *
@@ -46,22 +58,22 @@ const servicesFor = (testCase) => {
46
58
  drive: (0, service_js_1.createDriveService)(testCase.unwritable
47
59
  ? new RefusingDriveStore(testCase.seed || {})
48
60
  : new memory_store_js_1.MemoryDriveStore(testCase.seed || {})),
49
- info: (0, service_js_5.createInfoService)(service_js_5.BROWSER_INFO),
50
- home: (0, service_js_9.createHomeService)(async () => home_js_1.CORPUS_WEB_PAGE),
51
- authConfig: (0, service_js_6.createAuthConfigService)(authconfig_js_1.CORPUS_AUTH_CONFIG),
52
- admin: (0, service_js_7.createAdminService)({
61
+ info: (0, service_js_6.createInfoService)(service_js_6.BROWSER_INFO),
62
+ home: (0, service_js_10.createHomeService)(async () => home_js_1.CORPUS_WEB_PAGE),
63
+ authConfig: (0, service_js_7.createAuthConfigService)(authconfig_js_1.CORPUS_AUTH_CONFIG),
64
+ admin: (0, service_js_8.createAdminService)({
53
65
  diagnostics: async () => admin_js_1.CORPUS_DIAGNOSTICS,
54
66
  logs: async () => [admin_js_1.CORPUS_LOG],
55
67
  logTail: async (fileName) => fileName === admin_js_1.CORPUS_LOG.name ? admin_js_1.CORPUS_LOG_TAIL : undefined
56
68
  }),
57
- directory: (0, service_js_8.createDirectoryService)(identity, directory_js_1.CORPUS_DIRECTORY),
58
- identity: (0, service_js_2.createIdentityService)(identity),
69
+ directory: (0, service_js_9.createDirectoryService)(identity, directory_js_1.CORPUS_DIRECTORY),
70
+ identity: (0, service_js_3.createIdentityService)(identity),
59
71
  records: (0, records_js_1.createRecordService)(identity),
60
- registry: new registry_js_1.MemoryAppStreamRegistry(),
61
- session: (0, service_js_3.createSessionService)(identity, runner),
72
+ appStream: (0, service_js_2.createAppStreamService)(new service_js_2.MemoryAppStreamStore([CORPUS_APP])),
73
+ session: (0, service_js_4.createSessionService)(identity, runner),
62
74
  // no `runTimes` option: the default is the five the document names, so a
63
75
  // case is never refused for a reason that belongs to one host's install
64
- runtime: (0, service_js_4.createRuntimeService)(identity, runner)
76
+ runtime: (0, service_js_5.createRuntimeService)(identity, runner)
65
77
  };
66
78
  };
67
79
  exports.servicesFor = servicesFor;
@@ -91,7 +91,9 @@ const utf8Length = (value) => {
91
91
  /** The name a member is written under - a service gains the extension. */
92
92
  const memberName = (member) => member.type === 'service' ? `${member.name}.sas` : member.name;
93
93
  /** The bytes a member writes. */
94
- const memberBytes = (member) => member.type === 'file' ? base64Length(member.code) : utf8Length(member.code);
94
+ const memberBytes = (member) => member.type === 'file' && member.encoding === 'base64'
95
+ ? base64Length(member.code)
96
+ : utf8Length(member.code);
95
97
  const MEGABYTE = 1024 * 1024;
96
98
  /**
97
99
  * A MEASURED size, rounded UP to one decimal in binary megabytes.
@@ -249,8 +251,13 @@ const isDeployMember = (value) => {
249
251
  return false;
250
252
  if (member.type === 'folder')
251
253
  return Array.isArray(member.members) && member.members.every(isDeployMember);
252
- if (member.type === 'service' || member.type === 'file')
254
+ if (member.type === 'service')
253
255
  return typeof member.code === 'string';
256
+ if (member.type === 'file')
257
+ return (typeof member.code === 'string' &&
258
+ (member.encoding === undefined ||
259
+ member.encoding === 'utf8' ||
260
+ member.encoding === 'base64'));
254
261
  return false;
255
262
  };
256
263
  /**
@@ -264,7 +271,8 @@ const isDeployMember = (value) => {
264
271
  * A write that fails part-way answers the deploy routes' `500`; the tree before
265
272
  * the failure is already on the drive, which the message does not hide.
266
273
  */
267
- const deployTree = async (store, appLoc, tree, limits = exports.DEPLOY_LIMITS) => {
274
+ const deployTree = async (store, appLoc, tree, options = {}) => {
275
+ const limits = options.limits ?? exports.DEPLOY_LIMITS;
268
276
  if (!(0, exports.isDeployTree)(tree))
269
277
  throw new types_js_1.ApiError(400, 'Provided not supported data format.', {
270
278
  status: 'failure',
@@ -285,6 +293,12 @@ const deployTree = async (store, appLoc, tree, limits = exports.DEPLOY_LIMITS) =
285
293
  message: refusal.message
286
294
  });
287
295
  try {
296
+ if (options.replace && (await store.exists(root)))
297
+ await store.remove(root);
298
+ // The app's own folder is created before the first member is written: a tree
299
+ // whose root holds only files has no folder member to create it, and a store
300
+ // that writes through to a filesystem refuses a missing parent.
301
+ await store.mkdirp(root);
288
302
  await writeMembers(store, tree.members, root);
289
303
  }
290
304
  catch {
@@ -304,6 +318,8 @@ const writeMembers = async (store, members, parentPath) => {
304
318
  await writeMembers(store, member.members, path);
305
319
  continue;
306
320
  }
307
- await store.write(path, member.type === 'file' ? (0, exports.decodeBase64)(member.code) : member.code);
321
+ await store.write(path, member.type === 'file' && member.encoding === 'base64'
322
+ ? (0, exports.decodeBase64)(member.code)
323
+ : member.code);
308
324
  }
309
325
  };
@@ -153,8 +153,8 @@ const createDriveService = (store) => {
153
153
  throw new types_js_1.ApiError(400, 'Not a Folder.');
154
154
  return { status: 'success', tree: await treeOf(path) };
155
155
  },
156
- async deploy(appLoc, tree) {
157
- await (0, deploy_js_1.deployTree)(store, appLoc, tree);
156
+ async deploy(appLoc, tree, options) {
157
+ await (0, deploy_js_1.deployTree)(store, appLoc, tree, options);
158
158
  return { status: 'success' };
159
159
  }
160
160
  };
@@ -2,7 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.HANDLED_ROUTES = exports.noHandler = exports.dispatch = void 0;
4
4
  const index_js_1 = require("../routes/index.js");
5
- const registry_js_1 = require("../drive/registry.js");
5
+ const service_js_1 = require("../appstream/service.js");
6
+ const bundle_js_1 = require("../appstream/bundle.js");
6
7
  const types_js_1 = require("../types.js");
7
8
  const deploy_js_1 = require("../drive/deploy.js");
8
9
  /** The defaults sasjs/server's Client schema applies to an omitted lifetime. */
@@ -23,6 +24,8 @@ const RUN_ROUTES = [
23
24
  'POST /SASjsApi/code/execute',
24
25
  'POST /SASjsApi/code/trigger'
25
26
  ];
27
+ /** A payload value, when the caller actually stated one. */
28
+ const given = (value) => typeof value === 'string' && value !== '' ? value : undefined;
26
29
  /**
27
30
  * The parameters the table marks required, checked before the handler runs.
28
31
  *
@@ -278,18 +281,44 @@ const dispatch = async (services, routeId, request = {}) => {
278
281
  // expansion, which is why the two share this path rather than reading bytes.
279
282
  case 'POST /SASjsApi/drive/deploy/upload':
280
283
  case 'POST /SASjsApi/drive/deploy': {
281
- // `streamWebFolder` is the host's: publishing an app stream is not a drive
282
- // operation, and a host that has no app stream ignores it.
283
- await drive.deploy(String(body.appLoc ?? ''), body.fileTree);
284
- return { status: 200, body: deploy_js_1.DEPLOY_SUCCESS };
284
+ // A bundle is one archive: the deployment and - when it carries one - the
285
+ // statement that this folder is an app. The manifest is metadata, so it is
286
+ // taken out of the tree before anything is written.
287
+ const bundle = (0, bundle_js_1.splitAppBundle)(body.fileTree);
288
+ const appLoc = String(body.appLoc ?? '');
289
+ const requested = services.appStream
290
+ ? (0, bundle_js_1.publishRequestFor)({
291
+ appLoc,
292
+ streamWebFolder: given(body.streamWebFolder),
293
+ streamServiceName: given(body.streamServiceName),
294
+ streamLogo: given(body.streamLogo)
295
+ }, bundle.manifest)
296
+ : undefined;
297
+ // the index's own checks run BEFORE the drive is written: a tree the index
298
+ // would refuse must not leave its files behind, which is what happened when
299
+ // the deploy came first - a request answered `400` had already deployed
300
+ const publish = requested === undefined ? undefined : (0, service_js_1.checkPublishRequest)(requested);
301
+ await drive.deploy(appLoc, bundle.tree);
302
+ // a deploy of something that is not an app writes files and says so; an app
303
+ // is indexed in the same request, so the two cannot disagree
304
+ if (publish === undefined || !services.appStream)
305
+ return { status: 200, body: deploy_js_1.DEPLOY_SUCCESS };
306
+ const entry = await services.appStream.publish(publish);
307
+ return {
308
+ status: 200,
309
+ body: { ...deploy_js_1.DEPLOY_SUCCESS, app: (0, service_js_1.appSummary)(entry) }
310
+ };
285
311
  }
286
- case 'DELETE /SASjsApi/drive/deploy':
287
- if (!services.registry)
288
- throw new types_js_1.ApiError(501, `This host has no app stream registry, so it cannot answer ${routeId}.`);
312
+ case 'DELETE /SASjsApi/drive/deploy': {
313
+ if (!services.appStream)
314
+ throw new types_js_1.ApiError(501, `This host has no app stream, so it cannot answer ${routeId}.`);
315
+ // the id is the app: a label is not unique and a folder is not a handle
316
+ const removed = await services.appStream.unpublish(String(body.id ?? ''));
289
317
  return {
290
318
  status: 200,
291
- body: await (0, registry_js_1.unpublishAppStream)(services.registry, body)
319
+ body: { status: 'success', app: (0, service_js_1.appSummary)(removed) }
292
320
  };
321
+ }
293
322
  case 'GET /SASjsApi/drive/filetree':
294
323
  return { status: 200, body: await drive.tree(query._folderPath) };
295
324
  // ------------------------------------------------------------- run times