@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
@@ -15,7 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.DEFAULT_IDENTITY = exports.MemoryIdentityStore = exports.createDirectoryService = exports.normaliseLogFiles = exports.normaliseDiagnostics = exports.logLines = exports.createAdminService = exports.checkLogFileName = exports.MAX_LOG_LINES = exports.DEFAULT_LOG_LINES = exports.normaliseAuthConfig = exports.createAuthConfigService = exports.normaliseInfo = exports.createInfoService = exports.BROWSER_INFO = exports.withBundleVersion = exports.createHomeService = exports.NO_WEB_BUILD = exports.BUNDLE_NAME = exports.HOME_CORPUS = exports.DIRECTORY_CORPUS = exports.AUTH_CONFIG_CORPUS = exports.ADMIN_CORPUS = exports.INFO_CORPUS = exports.CODE_CORPUS = exports.sanitiseDisplayName = exports.createRecordService = exports.createIdentityService = exports.HANDLED_ROUTES = exports.dispatch = exports.TAG_CAPABILITIES = exports.OVERLAY = exports.findRoute = exports.routeById = exports.ROUTES = exports.MemoryDriveStore = exports.measureDeployTree = exports.isDeployTree = exports.deployTree = exports.decodeBase64 = exports.checkDeployTree = exports.DEPLOY_SUCCESS = exports.DEPLOY_PAYLOAD_BYTES = exports.DEPLOY_LIMITS = exports.createDriveService = exports.isRoot = exports.basename = exports.dirname = exports.hasBlockedExtension = exports.normaliseDrivePath = void 0;
18
- exports.servicesFor = exports.runCase = exports.PROVIDER_MANAGED = exports.ADMIN_SIGNED_IN = exports.RECORD_CORPUS = exports.unpublishAppStream = exports.MemoryAppStreamRegistry = exports.RUNTIME_CORPUS = exports.MemorySessionRunner = exports.createRuntimeService = exports.DOCUMENTED_RUN_TIMES = exports.CODE_DEBUG = exports.MemorySessionHost = exports.DEFAULT_SESSIONS = exports.createSessionService = exports.SESSION_CORPUS = exports.DEPLOY_CORPUS = exports.IDENTITY_CORPUS = exports.DRIVE_CORPUS = exports.parseIdentity = exports.IDENTITY_FORMAT = void 0;
18
+ exports.normaliseAppLoc = exports.newAppId = exports.isAppId = exports.escapeHtml = exports.createAppStreamWebService = exports.contentTypeFor = exports.cacheControlFor = exports.NO_SUCH_APP = exports.DEFAULT_APP_PATH = exports.createDriveAppStreamStore = exports.APP_STREAM_INDEX = exports.splitAppBundle = exports.publishRequestFor = exports.parseAppManifest = exports.APP_MANIFEST = exports.MemoryAppStreamStore = exports.defaultAppName = exports.createAppStreamService = exports.appSummary = exports.servicesFor = exports.runCase = exports.PROVIDER_MANAGED = exports.ADMIN_SIGNED_IN = exports.RECORD_CORPUS = exports.RUNTIME_CORPUS = exports.MemorySessionRunner = exports.createRuntimeService = exports.DOCUMENTED_RUN_TIMES = exports.CODE_DEBUG = exports.MemorySessionHost = exports.DEFAULT_SESSIONS = exports.createSessionService = exports.SESSION_CORPUS = exports.DEPLOY_CORPUS = exports.IDENTITY_CORPUS = exports.DRIVE_CORPUS = exports.parseIdentity = exports.IDENTITY_FORMAT = void 0;
19
19
  /**
20
20
  * `@sasjs/api` - the SASjs Server API described once.
21
21
  *
@@ -129,9 +129,6 @@ var memory_runner_js_1 = require("./runtime/memory-runner.js");
129
129
  Object.defineProperty(exports, "MemorySessionRunner", { enumerable: true, get: function () { return memory_runner_js_1.MemorySessionRunner; } });
130
130
  var runtime_js_1 = require("./corpus/runtime.js");
131
131
  Object.defineProperty(exports, "RUNTIME_CORPUS", { enumerable: true, get: function () { return runtime_js_1.RUNTIME_CORPUS; } });
132
- var registry_js_1 = require("./drive/registry.js");
133
- Object.defineProperty(exports, "MemoryAppStreamRegistry", { enumerable: true, get: function () { return registry_js_1.MemoryAppStreamRegistry; } });
134
- Object.defineProperty(exports, "unpublishAppStream", { enumerable: true, get: function () { return registry_js_1.unpublishAppStream; } });
135
132
  var records_js_2 = require("./corpus/records.js");
136
133
  Object.defineProperty(exports, "RECORD_CORPUS", { enumerable: true, get: function () { return records_js_2.RECORD_CORPUS; } });
137
134
  Object.defineProperty(exports, "ADMIN_SIGNED_IN", { enumerable: true, get: function () { return records_js_2.ADMIN_SIGNED_IN; } });
@@ -139,3 +136,27 @@ Object.defineProperty(exports, "PROVIDER_MANAGED", { enumerable: true, get: func
139
136
  var runner_js_1 = require("./corpus/runner.js");
140
137
  Object.defineProperty(exports, "runCase", { enumerable: true, get: function () { return runner_js_1.runCase; } });
141
138
  Object.defineProperty(exports, "servicesFor", { enumerable: true, get: function () { return runner_js_1.servicesFor; } });
139
+ var service_js_10 = require("./appstream/service.js");
140
+ Object.defineProperty(exports, "appSummary", { enumerable: true, get: function () { return service_js_10.appSummary; } });
141
+ Object.defineProperty(exports, "createAppStreamService", { enumerable: true, get: function () { return service_js_10.createAppStreamService; } });
142
+ Object.defineProperty(exports, "defaultAppName", { enumerable: true, get: function () { return service_js_10.defaultAppName; } });
143
+ Object.defineProperty(exports, "MemoryAppStreamStore", { enumerable: true, get: function () { return service_js_10.MemoryAppStreamStore; } });
144
+ var bundle_js_1 = require("./appstream/bundle.js");
145
+ Object.defineProperty(exports, "APP_MANIFEST", { enumerable: true, get: function () { return bundle_js_1.APP_MANIFEST; } });
146
+ Object.defineProperty(exports, "parseAppManifest", { enumerable: true, get: function () { return bundle_js_1.parseAppManifest; } });
147
+ Object.defineProperty(exports, "publishRequestFor", { enumerable: true, get: function () { return bundle_js_1.publishRequestFor; } });
148
+ Object.defineProperty(exports, "splitAppBundle", { enumerable: true, get: function () { return bundle_js_1.splitAppBundle; } });
149
+ var store_js_1 = require("./appstream/store.js");
150
+ Object.defineProperty(exports, "APP_STREAM_INDEX", { enumerable: true, get: function () { return store_js_1.APP_STREAM_INDEX; } });
151
+ Object.defineProperty(exports, "createDriveAppStreamStore", { enumerable: true, get: function () { return store_js_1.createDriveAppStreamStore; } });
152
+ var web_js_1 = require("./appstream/web.js");
153
+ Object.defineProperty(exports, "DEFAULT_APP_PATH", { enumerable: true, get: function () { return web_js_1.DEFAULT_APP_PATH; } });
154
+ Object.defineProperty(exports, "NO_SUCH_APP", { enumerable: true, get: function () { return web_js_1.NO_SUCH_APP; } });
155
+ Object.defineProperty(exports, "cacheControlFor", { enumerable: true, get: function () { return web_js_1.cacheControlFor; } });
156
+ Object.defineProperty(exports, "contentTypeFor", { enumerable: true, get: function () { return web_js_1.contentTypeFor; } });
157
+ Object.defineProperty(exports, "createAppStreamWebService", { enumerable: true, get: function () { return web_js_1.createAppStreamWebService; } });
158
+ Object.defineProperty(exports, "escapeHtml", { enumerable: true, get: function () { return web_js_1.escapeHtml; } });
159
+ var identity_js_2 = require("./appstream/identity.js");
160
+ Object.defineProperty(exports, "isAppId", { enumerable: true, get: function () { return identity_js_2.isAppId; } });
161
+ Object.defineProperty(exports, "newAppId", { enumerable: true, get: function () { return identity_js_2.newAppId; } });
162
+ Object.defineProperty(exports, "normaliseAppLoc", { enumerable: true, get: function () { return identity_js_2.normaliseAppLoc; } });
@@ -508,13 +508,7 @@ exports.GENERATED_ROUTES = [
508
508
  summary: 'Remove an app stream entry',
509
509
  params: [
510
510
  {
511
- name: 'appLoc',
512
- in: 'body',
513
- required: true,
514
- type: 'string'
515
- },
516
- {
517
- name: 'streamServiceName',
511
+ name: 'id',
518
512
  in: 'body',
519
513
  required: true,
520
514
  type: 'string'
@@ -532,7 +526,7 @@ exports.GENERATED_ROUTES = [
532
526
  },
533
527
  {
534
528
  status: 404,
535
- description: 'No app is published at that folder'
529
+ description: 'No app is published with that id'
536
530
  }
537
531
  ]
538
532
  },
@@ -251,8 +251,8 @@ exports.OVERLAY = {
251
251
  }
252
252
  },
253
253
  'POST /SASjsApi/drive/deploy/upload': { implemented: true },
254
- // `404` is a pair - the registry name and the appLoc it was published from -
255
- // that matches no entry, and the drive's content is deliberately left alone.
254
+ // `404` is an id the index does not hold - the id is the app, and its label and
255
+ // folder are not handles - and the drive's content is deliberately left alone.
256
256
  'DELETE /SASjsApi/drive/deploy': { implemented: true },
257
257
  // Group - members and the groups themselves
258
258
  'GET /SASjsApi/group': { implemented: true },
@@ -0,0 +1,46 @@
1
+ import type { DeployTree } from '../drive/deploy.js';
2
+ import type { PublishAppRequest } from './service.js';
3
+ /** The file, at the root of an app bundle, that says the bundle is an app. */
4
+ export declare const APP_MANIFEST = "sasjs.app.json";
5
+ export interface AppManifest {
6
+ /** The label the app is listed under. Defaults to the last segment of the folder. */
7
+ name?: string;
8
+ /** The folder under `<appLoc>/services` holding the compiled web assets. */
9
+ streamWebFolder: string;
10
+ /** An asset inside the app, used as its tile image. */
11
+ logo?: string;
12
+ /** Origins allowed to frame the app, as CSP `frame-ancestors` sources. */
13
+ frameAncestors?: string | string[];
14
+ }
15
+ export interface AppBundle {
16
+ /** The tree to deploy: the manifest, when there is one, is not in it. */
17
+ tree: DeployTree;
18
+ manifest?: AppManifest;
19
+ }
20
+ /** A manifest, checked field by field, naming the field that is wrong. */
21
+ export declare const parseAppManifest: (value: unknown) => AppManifest;
22
+ /**
23
+ * The deploy tree split from its manifest.
24
+ *
25
+ * A tree that is not a tree is passed through untouched: the deploy is the thing
26
+ * that refuses a malformed tree, with the message its own cases pin, and saying
27
+ * it here as well would be two wordings of one refusal.
28
+ */
29
+ export declare const splitAppBundle: (tree: unknown) => AppBundle;
30
+ /** What a deploy request says about the app it publishes. */
31
+ export interface DeployPublish {
32
+ appLoc: string;
33
+ streamWebFolder?: string;
34
+ streamServiceName?: string;
35
+ streamLogo?: string;
36
+ }
37
+ /**
38
+ * The publish a deploy asks for, or `undefined` when it publishes nothing.
39
+ *
40
+ * The request wins over the bundle where both speak - a caller naming a folder or
41
+ * a label is correcting the archive, not repeating it - and the bundle fills what
42
+ * the request leaves out, so a deploy that carries a manifest needs neither
43
+ * `streamWebFolder` nor `streamServiceName`.
44
+ */
45
+ export declare const publishRequestFor: (deploy: DeployPublish, manifest?: AppManifest) => PublishAppRequest | undefined;
46
+ //# sourceMappingURL=bundle.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bundle.d.ts","sourceRoot":"","sources":["../../../src/appstream/bundle.ts"],"names":[],"mappings":"AA2BA,OAAO,KAAK,EAAgB,UAAU,EAAE,MAAM,oBAAoB,CAAA;AAClE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAErD,8EAA8E;AAC9E,eAAO,MAAM,YAAY,mBAAmB,CAAA;AAE5C,MAAM,WAAW,WAAW;IAC1B,qFAAqF;IACrF,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,4EAA4E;IAC5E,eAAe,EAAE,MAAM,CAAA;IACvB,uDAAuD;IACvD,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,0EAA0E;IAC1E,cAAc,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;CACnC;AAED,MAAM,WAAW,SAAS;IACxB,yEAAyE;IACzE,IAAI,EAAE,UAAU,CAAA;IAChB,QAAQ,CAAC,EAAE,WAAW,CAAA;CACvB;AAiBD,0EAA0E;AAC1E,eAAO,MAAM,gBAAgB,GAAI,OAAO,OAAO,KAAG,WAyCjD,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,GAAI,MAAM,OAAO,KAAG,SAoC9C,CAAA;AAED,6DAA6D;AAC7D,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,MAAM,CAAA;IACd,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,iBAAiB,GAC5B,QAAQ,aAAa,EACrB,WAAW,WAAW,KACrB,iBAAiB,GAAG,SAiBtB,CAAA"}
@@ -0,0 +1,118 @@
1
+ /**
2
+ * An app as one bundle: the deployment and the statement of what it publishes.
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:
10
+ *
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/
16
+ *
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.
22
+ *
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.
26
+ */
27
+ import { ApiError } from '../types.js';
28
+ /** The file, at the root of an app bundle, that says the bundle is an app. */
29
+ export const APP_MANIFEST = 'sasjs.app.json';
30
+ const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
31
+ const text = (value, field) => {
32
+ if (value === undefined)
33
+ return undefined;
34
+ if (typeof value !== 'string' || value.trim() === '')
35
+ throw new ApiError(400, `"${field}" must be a non-empty string in ${APP_MANIFEST}.`);
36
+ return value.trim();
37
+ };
38
+ /** A manifest, checked field by field, naming the field that is wrong. */
39
+ export const parseAppManifest = (value) => {
40
+ if (!isObject(value))
41
+ 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
+ const name = text(value.name, 'name');
46
+ const logo = text(value.logo, 'logo');
47
+ const frameAncestors = value.frameAncestors;
48
+ if (frameAncestors !== undefined &&
49
+ (typeof frameAncestors !== 'string' || frameAncestors === '') &&
50
+ !(Array.isArray(frameAncestors) &&
51
+ frameAncestors.length > 0 &&
52
+ frameAncestors.every((source) => typeof source === 'string' && source !== '')))
53
+ throw new ApiError(400, `"frameAncestors" in ${APP_MANIFEST} must be a string or a non-empty list of strings.`);
54
+ return {
55
+ ...(name === undefined ? {} : { name }),
56
+ streamWebFolder,
57
+ ...(logo === undefined ? {} : { logo }),
58
+ ...(frameAncestors === undefined
59
+ ? {}
60
+ : { frameAncestors: frameAncestors })
61
+ };
62
+ };
63
+ /**
64
+ * The deploy tree split from its manifest.
65
+ *
66
+ * A tree that is not a tree is passed through untouched: the deploy is the thing
67
+ * that refuses a malformed tree, with the message its own cases pin, and saying
68
+ * it here as well would be two wordings of one refusal.
69
+ */
70
+ export const splitAppBundle = (tree) => {
71
+ if (!isObject(tree) || !Array.isArray(tree.members))
72
+ return { tree: tree };
73
+ const members = tree.members;
74
+ 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}`);
86
+ }
87
+ return {
88
+ tree: {
89
+ ...tree,
90
+ members: members.filter((_, index) => index !== at)
91
+ },
92
+ manifest: parseAppManifest(parsed)
93
+ };
94
+ };
95
+ /**
96
+ * The publish a deploy asks for, or `undefined` when it publishes nothing.
97
+ *
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`.
102
+ */
103
+ export const publishRequestFor = (deploy, manifest) => {
104
+ const streamWebFolder = deploy.streamWebFolder ?? manifest?.streamWebFolder;
105
+ if (streamWebFolder === undefined)
106
+ return undefined;
107
+ const name = deploy.streamServiceName ?? manifest?.name;
108
+ const logo = deploy.streamLogo ?? manifest?.logo;
109
+ return {
110
+ appLoc: deploy.appLoc,
111
+ streamWebFolder,
112
+ ...(name === undefined ? {} : { name }),
113
+ ...(logo === undefined ? {} : { logo }),
114
+ ...(manifest?.frameAncestors === undefined
115
+ ? {}
116
+ : { frameAncestors: manifest.frameAncestors })
117
+ };
118
+ };
@@ -0,0 +1,27 @@
1
+ /**
2
+ * An app's id is a UUID assigned once, when the app is first published.
3
+ *
4
+ * Not a hash of anything. Hashing a location was tried and removed: it made the id
5
+ * a function of the path, which forced rules on what a path may look like (lowercase
6
+ * only, no spaces) to keep the hash unambiguous, and it needed a hash implementation
7
+ * that worked identically in Node and in the browser worker the mock drive runs in.
8
+ * A UUID needs none of that: it is generated once, stored, and looked up by `appLoc`
9
+ * when the same folder is published again, so republishing restores the id while the
10
+ * path stays free to be whatever the drive allows.
11
+ *
12
+ * The readable NAME is the label - what a person sees and what a URL shows. The id
13
+ * is the key: what the registry, the routes and the permission rules point at.
14
+ */
15
+ export declare const normaliseAppLoc: (appLoc: string) => string;
16
+ /**
17
+ * A new id: a version 4 UUID.
18
+ *
19
+ * `crypto.randomUUID` exists in Node and in a browser worker on https or localhost,
20
+ * which is where the mock drive runs. The fallback builds the same thing from
21
+ * `getRandomValues` rather than from `Math.random`, so a weaker environment does not
22
+ * quietly produce a weaker id.
23
+ */
24
+ export declare const newAppId: () => string;
25
+ /** Whether a string is a well-formed id: a version 4 UUID. */
26
+ export declare const isAppId: (value: unknown) => value is string;
27
+ //# sourceMappingURL=identity.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identity.d.ts","sourceRoot":"","sources":["../../../src/appstream/identity.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,eAAe,GAAI,QAAQ,MAAM,KAAG,MAgBhD,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,QAAQ,QAAO,MAkC3B,CAAA;AAOD,8DAA8D;AAC9D,eAAO,MAAM,OAAO,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,MACD,CAAA"}
@@ -0,0 +1,63 @@
1
+ import { normaliseDrivePath } from '../drive/paths.js';
2
+ import { ApiError } from '../types.js';
3
+ /**
4
+ * An app's id is a UUID assigned once, when the app is first published.
5
+ *
6
+ * Not a hash of anything. Hashing a location was tried and removed: it made the id
7
+ * a function of the path, which forced rules on what a path may look like (lowercase
8
+ * only, no spaces) to keep the hash unambiguous, and it needed a hash implementation
9
+ * that worked identically in Node and in the browser worker the mock drive runs in.
10
+ * A UUID needs none of that: it is generated once, stored, and looked up by `appLoc`
11
+ * when the same folder is published again, so republishing restores the id while the
12
+ * path stays free to be whatever the drive allows.
13
+ *
14
+ * The readable NAME is the label - what a person sees and what a URL shows. The id
15
+ * is the key: what the registry, the routes and the permission rules point at.
16
+ */
17
+ export const normaliseAppLoc = (appLoc) => {
18
+ // An absent location is not the root: coercing it to one would register every
19
+ // caller that forgot to pass an appLoc against the root app's entry.
20
+ if (typeof appLoc !== 'string' || appLoc === '')
21
+ throw new Error(`App location must be a non-empty string, got ${JSON.stringify(appLoc)}.`);
22
+ const normalised = normaliseDrivePath(appLoc);
23
+ // one refusal for one rule: the deploy route refuses an escaping location with
24
+ // this wording and this status, and a host calling in here gets the same
25
+ if (normalised === null)
26
+ throw new ApiError(400, 'appLoc cannot be outside drive.');
27
+ return normalised;
28
+ };
29
+ /**
30
+ * A new id: a version 4 UUID.
31
+ *
32
+ * `crypto.randomUUID` exists in Node and in a browser worker on https or localhost,
33
+ * which is where the mock drive runs. The fallback builds the same thing from
34
+ * `getRandomValues` rather than from `Math.random`, so a weaker environment does not
35
+ * quietly produce a weaker id.
36
+ */
37
+ export const newAppId = () => {
38
+ const webCrypto = globalThis.crypto;
39
+ if (typeof webCrypto?.randomUUID === 'function')
40
+ return webCrypto.randomUUID();
41
+ if (typeof webCrypto?.getRandomValues !== 'function')
42
+ throw new Error('No cryptographic source of randomness is available.');
43
+ const bytes = new Uint8Array(16);
44
+ webCrypto.getRandomValues(bytes);
45
+ // version 4, variant 10xx
46
+ bytes[6] = (bytes[6] & 0x0f) | 0x40;
47
+ bytes[8] = (bytes[8] & 0x3f) | 0x80;
48
+ const hex = Array.from(bytes)
49
+ .map((byte) => byte.toString(16).padStart(2, '0'))
50
+ .join('');
51
+ return [
52
+ hex.slice(0, 8),
53
+ hex.slice(8, 12),
54
+ hex.slice(12, 16),
55
+ hex.slice(16, 20),
56
+ hex.slice(20)
57
+ ].join('-');
58
+ };
59
+ // no `i` flag: `newAppId` mints lower case and every lookup compares exactly, so
60
+ // a guard that accepts upper case would vouch for an id no lookup can find
61
+ const APP_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
62
+ /** Whether a string is a well-formed id: a version 4 UUID. */
63
+ export const isAppId = (value) => typeof value === 'string' && APP_ID.test(value);
@@ -0,0 +1,96 @@
1
+ export interface AppStreamEntry {
2
+ /** The identity: a version 4 UUID, minted once and kept for the life of the app. */
3
+ id: string;
4
+ /** The label a person reads. Not unique, not a key, not in any URL. */
5
+ 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;
10
+ /** An asset inside the app, served from the app's own URL. */
11
+ logo?: string;
12
+ /** Origins allowed to frame the app, as CSP `frame-ancestors` sources. */
13
+ frameAncestors?: string[];
14
+ /** When this publish happened, ISO 8601. */
15
+ publishedAt: string;
16
+ }
17
+ /**
18
+ * The published apps, as a host stores them.
19
+ *
20
+ * A whole-list read and write because the index is small and a host that has a
21
+ * file, a table or a browser store should not have to implement a query language
22
+ * to hold it. Mutations are serialised by the service, so a store only has to be
23
+ * atomic for a single call.
24
+ */
25
+ export interface AppStreamStore {
26
+ read(): Promise<AppStreamEntry[]>;
27
+ write(entries: AppStreamEntry[]): Promise<void>;
28
+ }
29
+ export interface PublishAppRequest {
30
+ appLoc: string;
31
+ streamWebFolder: string;
32
+ /** The label. Defaults to the last segment of `appLoc`. */
33
+ name?: string;
34
+ logo?: string;
35
+ frameAncestors?: string | string[];
36
+ /** The publish time, for a caller that has a clock of its own (or a test). */
37
+ publishedAt?: string;
38
+ }
39
+ export interface AppStreamService {
40
+ /** Every published app this caller may see, ordered by name. */
41
+ list(allow?: (entry: AppStreamEntry) => boolean): Promise<AppStreamEntry[]>;
42
+ /** One app by id, or the `404` a client that mistyped an id expects. */
43
+ get(id: string): Promise<AppStreamEntry>;
44
+ /** The app published at a folder, if any - the redeploy lookup. */
45
+ forAppLoc(appLoc: string): Promise<AppStreamEntry | undefined>;
46
+ /**
47
+ * Publishes a folder, minting an id the first time and keeping it afterwards.
48
+ *
49
+ * The entry IS the publish: a field the request does not state is absent from
50
+ * the entry rather than inherited from the previous publish, so republishing
51
+ * from a bundle that dropped its logo drops the logo. The label is the one
52
+ * exception - it carries over unless this publish names one - because a redeploy
53
+ * that says nothing about the name is not asking for a rename.
54
+ */
55
+ publish(request: PublishAppRequest): Promise<AppStreamEntry>;
56
+ /** Removes the app the id names, and returns what was removed. */
57
+ unpublish(id: string): Promise<AppStreamEntry>;
58
+ }
59
+ /** What a publish answers: enough for a client to print the app's URL. */
60
+ export declare const appSummary: (entry: AppStreamEntry) => {
61
+ id: string;
62
+ name: string;
63
+ url: string;
64
+ };
65
+ /** The label an app gets when the publish does not name one. */
66
+ export declare const defaultAppName: (appLoc: string) => string;
67
+ /** A publish request with every field in the shape the index stores. */
68
+ export interface CheckedPublish extends Omit<PublishAppRequest, 'frameAncestors'> {
69
+ frameAncestors?: string[];
70
+ }
71
+ /**
72
+ * A publish request, checked and normalised, with no default applied.
73
+ *
74
+ * Separate from `publish` so a host can refuse a publish BEFORE it writes the
75
+ * deployment: the drive and the index are two writes, and a request the index
76
+ * would refuse must not leave the files behind. `name` is left absent when the
77
+ * caller did not state one, because a redeploy inherits the label it had.
78
+ */
79
+ export declare const checkPublishRequest: (request: PublishAppRequest) => CheckedPublish;
80
+ /**
81
+ * The index, over a store.
82
+ *
83
+ * Every mutation reads the whole list, changes it and writes it back, so two
84
+ * publishes that overlap would discard each other's entry - the loser's change
85
+ * vanishing silently, which is the worst way for an index to be wrong. One queue
86
+ * removes the interleaving without a lock.
87
+ */
88
+ export declare const createAppStreamService: (store: AppStreamStore) => AppStreamService;
89
+ /** An index with no storage behind it: what a browser host and the corpus start from. */
90
+ export declare class MemoryAppStreamStore implements AppStreamStore {
91
+ private entries;
92
+ constructor(entries?: AppStreamEntry[]);
93
+ read(): Promise<AppStreamEntry[]>;
94
+ write(entries: AppStreamEntry[]): Promise<void>;
95
+ }
96
+ //# sourceMappingURL=service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../../src/appstream/service.ts"],"names":[],"mappings":"AA0BA,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,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,GAAG,SAAS,CAAC,CAAA;IAC9D;;;;;;;;OAQG;IACH,OAAO,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,cAAc,CAAC,CAAA;IAC5D,kEAAkE;IAClE,SAAS,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,CAAA;CAC/C;AAED,0EAA0E;AAC1E,eAAO,MAAM,UAAU,GACrB,OAAO,cAAc,KACpB;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAIxC,CAAA;AAaF,gEAAgE;AAChE,eAAO,MAAM,cAAc,GAAI,QAAQ,MAAM,KAAG,MAI/C,CAAA;AA0BD,wEAAwE;AACxE,MAAM,WAAW,cAAe,SAAQ,IAAI,CAC1C,iBAAiB,EACjB,gBAAgB,CACjB;IACC,cAAc,CAAC,EAAE,MAAM,EAAE,CAAA;CAC1B;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,mBAAmB,GAC9B,SAAS,iBAAiB,KACzB,cAuBF,CAAA;AAqCD;;;;;;;GAOG;AACH,eAAO,MAAM,sBAAsB,GACjC,OAAO,cAAc,KACpB,gBAgHF,CAAA;AAED,yFAAyF;AACzF,qBAAa,oBAAqB,YAAW,cAAc;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"}