@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.
- package/README.md +10 -0
- package/build/browser.js +695 -101
- package/build/cjs/appstream/bundle.js +124 -0
- package/build/cjs/appstream/identity.js +69 -0
- package/build/cjs/appstream/service.js +226 -0
- package/build/cjs/appstream/store.js +69 -0
- package/build/cjs/appstream/web.js +248 -0
- package/build/cjs/corpus/deploy.js +112 -29
- package/build/cjs/corpus/runner.js +30 -18
- package/build/cjs/drive/deploy.js +20 -4
- package/build/cjs/drive/service.js +2 -2
- package/build/cjs/host/dispatch.js +38 -9
- package/build/cjs/index.js +25 -4
- package/build/cjs/routes/generated.js +2 -8
- package/build/cjs/routes/overlay.js +2 -2
- package/build/esm/appstream/bundle.d.ts +46 -0
- package/build/esm/appstream/bundle.d.ts.map +1 -0
- package/build/esm/appstream/bundle.js +118 -0
- package/build/esm/appstream/identity.d.ts +27 -0
- package/build/esm/appstream/identity.d.ts.map +1 -0
- package/build/esm/appstream/identity.js +63 -0
- package/build/esm/appstream/service.d.ts +96 -0
- package/build/esm/appstream/service.d.ts.map +1 -0
- package/build/esm/appstream/service.js +218 -0
- package/build/esm/appstream/store.d.ts +6 -0
- package/build/esm/appstream/store.d.ts.map +1 -0
- package/build/esm/appstream/store.js +65 -0
- package/build/esm/appstream/web.d.ts +74 -0
- package/build/esm/appstream/web.d.ts.map +1 -0
- package/build/esm/appstream/web.js +241 -0
- package/build/esm/corpus/deploy.d.ts +5 -5
- package/build/esm/corpus/deploy.d.ts.map +1 -1
- package/build/esm/corpus/deploy.js +112 -29
- package/build/esm/corpus/runner.d.ts.map +1 -1
- package/build/esm/corpus/runner.js +14 -2
- package/build/esm/drive/deploy.d.ts +41 -2
- package/build/esm/drive/deploy.d.ts.map +1 -1
- package/build/esm/drive/deploy.js +20 -4
- package/build/esm/drive/service.d.ts +2 -1
- package/build/esm/drive/service.d.ts.map +1 -1
- package/build/esm/drive/service.js +2 -2
- package/build/esm/host/dispatch.d.ts +5 -4
- package/build/esm/host/dispatch.d.ts.map +1 -1
- package/build/esm/host/dispatch.js +38 -9
- package/build/esm/index.d.ts +9 -3
- package/build/esm/index.d.ts.map +1 -1
- package/build/esm/index.js +5 -1
- package/build/esm/routes/generated.d.ts.map +1 -1
- package/build/esm/routes/generated.js +2 -8
- package/build/esm/routes/overlay.js +2 -2
- package/package.json +5 -4
- package/build/cjs/drive/registry.js +0 -38
- package/build/esm/drive/registry.d.ts +0 -52
- package/build/esm/drive/registry.d.ts.map +0 -1
- package/build/esm/drive/registry.js +0 -33
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.publishRequestFor = exports.splitAppBundle = exports.parseAppManifest = exports.APP_MANIFEST = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* An app as one bundle: the deployment and the statement of what it publishes.
|
|
6
|
+
*
|
|
7
|
+
* A deploy writes a tree to a folder, and a tree of an app already holds
|
|
8
|
+
* everything the app is - its services under `services/`, the compiled web assets
|
|
9
|
+
* under `services/<streamWebFolder>`, and whatever jobs and data files it was
|
|
10
|
+
* built with. What a tree cannot say is that the folder IS an app: which of its
|
|
11
|
+
* folders holds the web build, what it should be called, and whether it may be
|
|
12
|
+
* framed. That statement is a small file at the root of the archive:
|
|
13
|
+
*
|
|
14
|
+
* sasjs.app.json
|
|
15
|
+
* services/
|
|
16
|
+
* web/ the compiled app: index.html, main.<hash>.js, assets/
|
|
17
|
+
* build.sas the services the app calls
|
|
18
|
+
* jobs/
|
|
19
|
+
*
|
|
20
|
+
* So one zip deploys the app and the folders under it in a single request, and
|
|
21
|
+
* the app is indexed as part of that deploy rather than by a second call that can
|
|
22
|
+
* fail on its own. The manifest is consumed here: it is metadata, not an asset,
|
|
23
|
+
* so it is stripped from the tree before the tree is written and never appears in
|
|
24
|
+
* the app's served files.
|
|
25
|
+
*
|
|
26
|
+
* The manifest is optional. A deploy that names `streamWebFolder` in its payload
|
|
27
|
+
* publishes without one, which is what an existing client does; a bundle that
|
|
28
|
+
* carries a manifest needs no arguments beyond the folder it deploys to.
|
|
29
|
+
*/
|
|
30
|
+
const types_js_1 = require("../types.js");
|
|
31
|
+
/** The file, at the root of an app bundle, that says the bundle is an app. */
|
|
32
|
+
exports.APP_MANIFEST = 'sasjs.app.json';
|
|
33
|
+
const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
34
|
+
const text = (value, field) => {
|
|
35
|
+
if (value === undefined)
|
|
36
|
+
return undefined;
|
|
37
|
+
if (typeof value !== 'string' || value.trim() === '')
|
|
38
|
+
throw new types_js_1.ApiError(400, `"${field}" must be a non-empty string in ${exports.APP_MANIFEST}.`);
|
|
39
|
+
return value.trim();
|
|
40
|
+
};
|
|
41
|
+
/** A manifest, checked field by field, naming the field that is wrong. */
|
|
42
|
+
const parseAppManifest = (value) => {
|
|
43
|
+
if (!isObject(value))
|
|
44
|
+
throw new types_js_1.ApiError(400, `${exports.APP_MANIFEST} must be an object.`);
|
|
45
|
+
const streamWebFolder = text(value.streamWebFolder, 'streamWebFolder');
|
|
46
|
+
if (streamWebFolder === undefined)
|
|
47
|
+
throw new types_js_1.ApiError(400, `${exports.APP_MANIFEST} must state "streamWebFolder": the folder under services/ holding the compiled app.`);
|
|
48
|
+
const name = text(value.name, 'name');
|
|
49
|
+
const logo = text(value.logo, 'logo');
|
|
50
|
+
const frameAncestors = value.frameAncestors;
|
|
51
|
+
if (frameAncestors !== undefined &&
|
|
52
|
+
(typeof frameAncestors !== 'string' || frameAncestors === '') &&
|
|
53
|
+
!(Array.isArray(frameAncestors) &&
|
|
54
|
+
frameAncestors.length > 0 &&
|
|
55
|
+
frameAncestors.every((source) => typeof source === 'string' && source !== '')))
|
|
56
|
+
throw new types_js_1.ApiError(400, `"frameAncestors" in ${exports.APP_MANIFEST} must be a string or a non-empty list of strings.`);
|
|
57
|
+
return {
|
|
58
|
+
...(name === undefined ? {} : { name }),
|
|
59
|
+
streamWebFolder,
|
|
60
|
+
...(logo === undefined ? {} : { logo }),
|
|
61
|
+
...(frameAncestors === undefined
|
|
62
|
+
? {}
|
|
63
|
+
: { frameAncestors: frameAncestors })
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
exports.parseAppManifest = parseAppManifest;
|
|
67
|
+
/**
|
|
68
|
+
* The deploy tree split from its manifest.
|
|
69
|
+
*
|
|
70
|
+
* A tree that is not a tree is passed through untouched: the deploy is the thing
|
|
71
|
+
* that refuses a malformed tree, with the message its own cases pin, and saying
|
|
72
|
+
* it here as well would be two wordings of one refusal.
|
|
73
|
+
*/
|
|
74
|
+
const splitAppBundle = (tree) => {
|
|
75
|
+
if (!isObject(tree) || !Array.isArray(tree.members))
|
|
76
|
+
return { tree: tree };
|
|
77
|
+
const members = tree.members;
|
|
78
|
+
const at = members.findIndex((member) => isObject(member) && member.type === 'file' && member.name === exports.APP_MANIFEST);
|
|
79
|
+
if (at === -1)
|
|
80
|
+
return { tree: tree };
|
|
81
|
+
const member = members[at];
|
|
82
|
+
if (typeof member.code !== 'string')
|
|
83
|
+
throw new types_js_1.ApiError(400, `${exports.APP_MANIFEST} carries no text.`);
|
|
84
|
+
let parsed;
|
|
85
|
+
try {
|
|
86
|
+
parsed = JSON.parse(member.code);
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
throw new types_js_1.ApiError(400, `${exports.APP_MANIFEST} is not valid JSON: ${error.message}`);
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
tree: {
|
|
93
|
+
...tree,
|
|
94
|
+
members: members.filter((_, index) => index !== at)
|
|
95
|
+
},
|
|
96
|
+
manifest: (0, exports.parseAppManifest)(parsed)
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
exports.splitAppBundle = splitAppBundle;
|
|
100
|
+
/**
|
|
101
|
+
* The publish a deploy asks for, or `undefined` when it publishes nothing.
|
|
102
|
+
*
|
|
103
|
+
* The request wins over the bundle where both speak - a caller naming a folder or
|
|
104
|
+
* a label is correcting the archive, not repeating it - and the bundle fills what
|
|
105
|
+
* the request leaves out, so a deploy that carries a manifest needs neither
|
|
106
|
+
* `streamWebFolder` nor `streamServiceName`.
|
|
107
|
+
*/
|
|
108
|
+
const publishRequestFor = (deploy, manifest) => {
|
|
109
|
+
const streamWebFolder = deploy.streamWebFolder ?? manifest?.streamWebFolder;
|
|
110
|
+
if (streamWebFolder === undefined)
|
|
111
|
+
return undefined;
|
|
112
|
+
const name = deploy.streamServiceName ?? manifest?.name;
|
|
113
|
+
const logo = deploy.streamLogo ?? manifest?.logo;
|
|
114
|
+
return {
|
|
115
|
+
appLoc: deploy.appLoc,
|
|
116
|
+
streamWebFolder,
|
|
117
|
+
...(name === undefined ? {} : { name }),
|
|
118
|
+
...(logo === undefined ? {} : { logo }),
|
|
119
|
+
...(manifest?.frameAncestors === undefined
|
|
120
|
+
? {}
|
|
121
|
+
: { frameAncestors: manifest.frameAncestors })
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
exports.publishRequestFor = publishRequestFor;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isAppId = exports.newAppId = exports.normaliseAppLoc = void 0;
|
|
4
|
+
const paths_js_1 = require("../drive/paths.js");
|
|
5
|
+
const types_js_1 = require("../types.js");
|
|
6
|
+
/**
|
|
7
|
+
* An app's id is a UUID assigned once, when the app is first published.
|
|
8
|
+
*
|
|
9
|
+
* Not a hash of anything. Hashing a location was tried and removed: it made the id
|
|
10
|
+
* a function of the path, which forced rules on what a path may look like (lowercase
|
|
11
|
+
* only, no spaces) to keep the hash unambiguous, and it needed a hash implementation
|
|
12
|
+
* that worked identically in Node and in the browser worker the mock drive runs in.
|
|
13
|
+
* A UUID needs none of that: it is generated once, stored, and looked up by `appLoc`
|
|
14
|
+
* when the same folder is published again, so republishing restores the id while the
|
|
15
|
+
* path stays free to be whatever the drive allows.
|
|
16
|
+
*
|
|
17
|
+
* The readable NAME is the label - what a person sees and what a URL shows. The id
|
|
18
|
+
* is the key: what the registry, the routes and the permission rules point at.
|
|
19
|
+
*/
|
|
20
|
+
const normaliseAppLoc = (appLoc) => {
|
|
21
|
+
// An absent location is not the root: coercing it to one would register every
|
|
22
|
+
// caller that forgot to pass an appLoc against the root app's entry.
|
|
23
|
+
if (typeof appLoc !== 'string' || appLoc === '')
|
|
24
|
+
throw new Error(`App location must be a non-empty string, got ${JSON.stringify(appLoc)}.`);
|
|
25
|
+
const normalised = (0, paths_js_1.normaliseDrivePath)(appLoc);
|
|
26
|
+
// one refusal for one rule: the deploy route refuses an escaping location with
|
|
27
|
+
// this wording and this status, and a host calling in here gets the same
|
|
28
|
+
if (normalised === null)
|
|
29
|
+
throw new types_js_1.ApiError(400, 'appLoc cannot be outside drive.');
|
|
30
|
+
return normalised;
|
|
31
|
+
};
|
|
32
|
+
exports.normaliseAppLoc = normaliseAppLoc;
|
|
33
|
+
/**
|
|
34
|
+
* A new id: a version 4 UUID.
|
|
35
|
+
*
|
|
36
|
+
* `crypto.randomUUID` exists in Node and in a browser worker on https or localhost,
|
|
37
|
+
* which is where the mock drive runs. The fallback builds the same thing from
|
|
38
|
+
* `getRandomValues` rather than from `Math.random`, so a weaker environment does not
|
|
39
|
+
* quietly produce a weaker id.
|
|
40
|
+
*/
|
|
41
|
+
const newAppId = () => {
|
|
42
|
+
const webCrypto = globalThis.crypto;
|
|
43
|
+
if (typeof webCrypto?.randomUUID === 'function')
|
|
44
|
+
return webCrypto.randomUUID();
|
|
45
|
+
if (typeof webCrypto?.getRandomValues !== 'function')
|
|
46
|
+
throw new Error('No cryptographic source of randomness is available.');
|
|
47
|
+
const bytes = new Uint8Array(16);
|
|
48
|
+
webCrypto.getRandomValues(bytes);
|
|
49
|
+
// version 4, variant 10xx
|
|
50
|
+
bytes[6] = (bytes[6] & 0x0f) | 0x40;
|
|
51
|
+
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
52
|
+
const hex = Array.from(bytes)
|
|
53
|
+
.map((byte) => byte.toString(16).padStart(2, '0'))
|
|
54
|
+
.join('');
|
|
55
|
+
return [
|
|
56
|
+
hex.slice(0, 8),
|
|
57
|
+
hex.slice(8, 12),
|
|
58
|
+
hex.slice(12, 16),
|
|
59
|
+
hex.slice(16, 20),
|
|
60
|
+
hex.slice(20)
|
|
61
|
+
].join('-');
|
|
62
|
+
};
|
|
63
|
+
exports.newAppId = newAppId;
|
|
64
|
+
// no `i` flag: `newAppId` mints lower case and every lookup compares exactly, so
|
|
65
|
+
// a guard that accepts upper case would vouch for an id no lookup can find
|
|
66
|
+
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}$/;
|
|
67
|
+
/** Whether a string is a well-formed id: a version 4 UUID. */
|
|
68
|
+
const isAppId = (value) => typeof value === 'string' && APP_ID.test(value);
|
|
69
|
+
exports.isAppId = isAppId;
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MemoryAppStreamStore = exports.createAppStreamService = exports.checkPublishRequest = exports.defaultAppName = exports.appSummary = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* The app index: what has been published, keyed by a UUID.
|
|
6
|
+
*
|
|
7
|
+
* An app is one deployed folder (`appLoc`), the web assets inside it
|
|
8
|
+
* (`appLoc/services/<streamWebFolder>`) and a UUID assigned when it is first
|
|
9
|
+
* published. The id is the key the routes, the listing and the permission rules
|
|
10
|
+
* point at; the name is a label a person reads, and carries no uniqueness rule at
|
|
11
|
+
* all - two apps may be called the same thing without colliding, because nothing
|
|
12
|
+
* is looked up by name.
|
|
13
|
+
*
|
|
14
|
+
* The service is the decisions, and a host supplies the storage. That split is
|
|
15
|
+
* the point of this package: sasjs/server keeps the index in a JSON file beside
|
|
16
|
+
* its drive, the browser build keeps it in the drive it already has, and both
|
|
17
|
+
* answer the same listing because both call this file.
|
|
18
|
+
*
|
|
19
|
+
* One app per folder. Publishing a folder that is already published is the same
|
|
20
|
+
* app published again: it keeps its id (so its URL survives a redeploy) and takes
|
|
21
|
+
* the values the new publish states - except the label, which carries over when
|
|
22
|
+
* the new publish does not name one, so a redeploy that has forgotten what the
|
|
23
|
+
* app is called does not rename it to its folder. Nothing is derived from the
|
|
24
|
+
* path, so a capital, a space or a `..` in a folder name is the drive's business
|
|
25
|
+
* rather than an identity question.
|
|
26
|
+
*/
|
|
27
|
+
const types_js_1 = require("../types.js");
|
|
28
|
+
const identity_js_1 = require("./identity.js");
|
|
29
|
+
/** What a publish answers: enough for a client to print the app's URL. */
|
|
30
|
+
const appSummary = (entry) => ({
|
|
31
|
+
id: entry.id,
|
|
32
|
+
name: entry.name,
|
|
33
|
+
url: `/app/${entry.id}/`
|
|
34
|
+
});
|
|
35
|
+
exports.appSummary = appSummary;
|
|
36
|
+
/** A folder holding web assets: one segment, so it cannot climb out of the app. */
|
|
37
|
+
const webFolderOf = (value) => {
|
|
38
|
+
if (typeof value !== 'string' || value === '')
|
|
39
|
+
throw new types_js_1.ApiError(400, '"streamWebFolder" is required');
|
|
40
|
+
if (value === '.' || value === '..' || /[\\/]/.test(value))
|
|
41
|
+
throw new types_js_1.ApiError(400, 'streamWebFolder must be a single path segment.');
|
|
42
|
+
return value;
|
|
43
|
+
};
|
|
44
|
+
/** The label an app gets when the publish does not name one. */
|
|
45
|
+
const defaultAppName = (appLoc) => {
|
|
46
|
+
const segment = appLoc.split('/').filter(Boolean).pop();
|
|
47
|
+
return segment ? segment : 'app';
|
|
48
|
+
};
|
|
49
|
+
exports.defaultAppName = defaultAppName;
|
|
50
|
+
/**
|
|
51
|
+
* Whether two spellings name one folder.
|
|
52
|
+
*
|
|
53
|
+
* A location that cannot be a folder matches nothing rather than throwing: an
|
|
54
|
+
* index written by hand, or by an older shape, holds one bad entry at most, and
|
|
55
|
+
* that entry must not stop every publish and every redeploy lookup on the host.
|
|
56
|
+
*/
|
|
57
|
+
const sameAppLoc = (left, right) => {
|
|
58
|
+
try {
|
|
59
|
+
return (0, identity_js_1.normaliseAppLoc)(left) === (0, identity_js_1.normaliseAppLoc)(right);
|
|
60
|
+
}
|
|
61
|
+
catch (_) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
/** The same folder spellings `normaliseAppLoc` collapses, as a `400` rather than a throw. */
|
|
66
|
+
const appLocOf = (value) => {
|
|
67
|
+
try {
|
|
68
|
+
return (0, identity_js_1.normaliseAppLoc)(value);
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
throw new types_js_1.ApiError(400, error.message);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* A publish request, checked and normalised, with no default applied.
|
|
76
|
+
*
|
|
77
|
+
* Separate from `publish` so a host can refuse a publish BEFORE it writes the
|
|
78
|
+
* deployment: the drive and the index are two writes, and a request the index
|
|
79
|
+
* would refuse must not leave the files behind. `name` is left absent when the
|
|
80
|
+
* caller did not state one, because a redeploy inherits the label it had.
|
|
81
|
+
*/
|
|
82
|
+
const checkPublishRequest = (request) => {
|
|
83
|
+
const appLoc = appLocOf(request.appLoc);
|
|
84
|
+
// the drive root is not a folder an app can live in: a deploy to `/` writes
|
|
85
|
+
// `//index.html`, which nothing can read, so it is refused rather than indexed
|
|
86
|
+
if (appLoc === '/')
|
|
87
|
+
throw new types_js_1.ApiError(400, 'An app cannot be published at the drive root.');
|
|
88
|
+
const logo = logoOf(request.logo);
|
|
89
|
+
const frameAncestors = frameAncestorsOf(request.frameAncestors);
|
|
90
|
+
return {
|
|
91
|
+
appLoc,
|
|
92
|
+
streamWebFolder: webFolderOf(request.streamWebFolder),
|
|
93
|
+
...(request.name === undefined
|
|
94
|
+
? {}
|
|
95
|
+
: { name: nameOf(request.name, appLoc) }),
|
|
96
|
+
...(logo === undefined ? {} : { logo }),
|
|
97
|
+
...(frameAncestors === undefined ? {} : { frameAncestors }),
|
|
98
|
+
...(request.publishedAt === undefined
|
|
99
|
+
? {}
|
|
100
|
+
: { publishedAt: request.publishedAt })
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
exports.checkPublishRequest = checkPublishRequest;
|
|
104
|
+
const nameOf = (value, appLoc) => {
|
|
105
|
+
if (value === undefined)
|
|
106
|
+
return (0, exports.defaultAppName)(appLoc);
|
|
107
|
+
if (typeof value !== 'string' || value.trim() === '')
|
|
108
|
+
throw new types_js_1.ApiError(400, '"name" must be a non-empty string');
|
|
109
|
+
return value.trim();
|
|
110
|
+
};
|
|
111
|
+
const logoOf = (value) => {
|
|
112
|
+
if (value === undefined)
|
|
113
|
+
return undefined;
|
|
114
|
+
if (typeof value !== 'string' || value === '')
|
|
115
|
+
throw new types_js_1.ApiError(400, '"logo" must be a non-empty string');
|
|
116
|
+
return value;
|
|
117
|
+
};
|
|
118
|
+
const frameAncestorsOf = (value) => {
|
|
119
|
+
if (value === undefined)
|
|
120
|
+
return undefined;
|
|
121
|
+
const list = Array.isArray(value) ? value : [value];
|
|
122
|
+
if (list.length === 0 ||
|
|
123
|
+
list.some((source) => typeof source !== 'string' || source === ''))
|
|
124
|
+
throw new types_js_1.ApiError(400, '"frameAncestors" must be a string or a non-empty list of strings');
|
|
125
|
+
return [...list];
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* The index, over a store.
|
|
129
|
+
*
|
|
130
|
+
* Every mutation reads the whole list, changes it and writes it back, so two
|
|
131
|
+
* publishes that overlap would discard each other's entry - the loser's change
|
|
132
|
+
* vanishing silently, which is the worst way for an index to be wrong. One queue
|
|
133
|
+
* removes the interleaving without a lock.
|
|
134
|
+
*/
|
|
135
|
+
const createAppStreamService = (store) => {
|
|
136
|
+
let queue = Promise.resolve();
|
|
137
|
+
const mutate = (work) => {
|
|
138
|
+
// `then(work, work)`: a failed mutation must not poison the queue
|
|
139
|
+
const run = queue.then(work, work);
|
|
140
|
+
queue = run.catch(() => undefined);
|
|
141
|
+
return run;
|
|
142
|
+
};
|
|
143
|
+
const readable = (allow) => store.read().then((entries) => entries
|
|
144
|
+
.filter((entry) => (allow ? allow(entry) : true))
|
|
145
|
+
.map((entry) => ({ ...entry }))
|
|
146
|
+
.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0)));
|
|
147
|
+
return {
|
|
148
|
+
list: readable,
|
|
149
|
+
async get(id) {
|
|
150
|
+
const entry = (await store.read()).find((candidate) => candidate.id === id);
|
|
151
|
+
if (!entry)
|
|
152
|
+
throw new types_js_1.ApiError(404, 'No app is published with that id.');
|
|
153
|
+
return { ...entry };
|
|
154
|
+
},
|
|
155
|
+
async forAppLoc(appLoc) {
|
|
156
|
+
// spelled inconsistently by a caller is still one folder; a location that
|
|
157
|
+
// cannot name a folder simply has no app
|
|
158
|
+
let normalised;
|
|
159
|
+
try {
|
|
160
|
+
normalised = (0, identity_js_1.normaliseAppLoc)(appLoc);
|
|
161
|
+
}
|
|
162
|
+
catch (_) {
|
|
163
|
+
return undefined;
|
|
164
|
+
}
|
|
165
|
+
const entry = (await store.read()).find((candidate) => sameAppLoc(candidate.appLoc, normalised));
|
|
166
|
+
return entry ? { ...entry } : undefined;
|
|
167
|
+
},
|
|
168
|
+
publish(request) {
|
|
169
|
+
return mutate(async () => {
|
|
170
|
+
const checked = (0, exports.checkPublishRequest)(request);
|
|
171
|
+
const appLoc = checked.appLoc;
|
|
172
|
+
const streamWebFolder = checked.streamWebFolder;
|
|
173
|
+
const entries = await store.read();
|
|
174
|
+
// the folder is the app, so a redeploy is the same app published again:
|
|
175
|
+
// it keeps the id its URL depends on, and takes the values this publish
|
|
176
|
+
// states rather than the ones the previous publish left behind
|
|
177
|
+
const existing = entries.find((entry) => sameAppLoc(entry.appLoc, appLoc));
|
|
178
|
+
const logo = checked.logo;
|
|
179
|
+
const frameAncestors = checked.frameAncestors;
|
|
180
|
+
const entry = {
|
|
181
|
+
id: existing ? existing.id : (0, identity_js_1.newAppId)(),
|
|
182
|
+
// the label is the one field a redeploy inherits: everything else is
|
|
183
|
+
// what this publish states
|
|
184
|
+
name: nameOf(checked.name ?? existing?.name, appLoc),
|
|
185
|
+
appLoc,
|
|
186
|
+
streamWebFolder,
|
|
187
|
+
...(logo === undefined ? {} : { logo }),
|
|
188
|
+
...(frameAncestors === undefined ? {} : { frameAncestors }),
|
|
189
|
+
publishedAt: checked.publishedAt ?? new Date().toISOString()
|
|
190
|
+
};
|
|
191
|
+
await store.write([
|
|
192
|
+
...entries.filter((candidate) => !sameAppLoc(candidate.appLoc, appLoc)),
|
|
193
|
+
entry
|
|
194
|
+
]);
|
|
195
|
+
return { ...entry };
|
|
196
|
+
});
|
|
197
|
+
},
|
|
198
|
+
unpublish(id) {
|
|
199
|
+
return mutate(async () => {
|
|
200
|
+
const entries = await store.read();
|
|
201
|
+
const index = entries.findIndex((entry) => entry.id === id);
|
|
202
|
+
// the id is the app, and the only handle: a label is shared by design and
|
|
203
|
+
// a folder is where the app was published rather than what it is
|
|
204
|
+
if (index === -1)
|
|
205
|
+
throw new types_js_1.ApiError(404, 'No app is published with that id.');
|
|
206
|
+
const [removed] = entries.splice(index, 1);
|
|
207
|
+
await store.write(entries);
|
|
208
|
+
return { ...removed };
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
};
|
|
213
|
+
exports.createAppStreamService = createAppStreamService;
|
|
214
|
+
/** An index with no storage behind it: what a browser host and the corpus start from. */
|
|
215
|
+
class MemoryAppStreamStore {
|
|
216
|
+
constructor(entries = []) {
|
|
217
|
+
this.entries = entries.map((entry) => ({ ...entry }));
|
|
218
|
+
}
|
|
219
|
+
async read() {
|
|
220
|
+
return this.entries.map((entry) => ({ ...entry }));
|
|
221
|
+
}
|
|
222
|
+
async write(entries) {
|
|
223
|
+
this.entries = entries.map((entry) => ({ ...entry }));
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
exports.MemoryAppStreamStore = MemoryAppStreamStore;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createDriveAppStreamStore = exports.APP_STREAM_INDEX = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* The index kept as a file on the drive.
|
|
6
|
+
*
|
|
7
|
+
* A host that has a drive and no other place to put the index can use this: the
|
|
8
|
+
* browser build does, where the drive is the only durable thing there is, and the
|
|
9
|
+
* file rides along with the persistence the drive already has.
|
|
10
|
+
*
|
|
11
|
+
* An absent file is an empty index - the state a host starts in. A file that is
|
|
12
|
+
* not an index is an error rather than an empty index: a deploy that silently
|
|
13
|
+
* unregistered every app because the file had a trailing comma is the failure
|
|
14
|
+
* this refuses to hide.
|
|
15
|
+
*/
|
|
16
|
+
const types_js_1 = require("../types.js");
|
|
17
|
+
/** Where the index lives when a host does not choose: hidden, and not an app's. */
|
|
18
|
+
exports.APP_STREAM_INDEX = '/.sasjs/apps.json';
|
|
19
|
+
const isEntry = (value) => {
|
|
20
|
+
if (typeof value !== 'object' || value === null)
|
|
21
|
+
return false;
|
|
22
|
+
const entry = value;
|
|
23
|
+
return (typeof entry.id === 'string' &&
|
|
24
|
+
typeof entry.name === 'string' &&
|
|
25
|
+
typeof entry.appLoc === 'string' &&
|
|
26
|
+
typeof entry.streamWebFolder === 'string' &&
|
|
27
|
+
typeof entry.publishedAt === 'string');
|
|
28
|
+
};
|
|
29
|
+
const createDriveAppStreamStore = (drive, filePath = exports.APP_STREAM_INDEX) => ({
|
|
30
|
+
async read() {
|
|
31
|
+
let contents;
|
|
32
|
+
try {
|
|
33
|
+
contents = (await drive.read(filePath)).contents;
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
// nothing published yet is not an error, it is the empty case
|
|
37
|
+
if (error instanceof types_js_1.ApiError && error.status === 404)
|
|
38
|
+
return [];
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
if (typeof contents !== 'string')
|
|
42
|
+
throw new types_js_1.ApiError(500, `The app index at ${filePath} is not text, so no app can be read from it.`);
|
|
43
|
+
let parsed;
|
|
44
|
+
try {
|
|
45
|
+
parsed = JSON.parse(contents);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
throw new types_js_1.ApiError(500, `The app index at ${filePath} is not valid JSON, so no app is registered: ${error.message}`);
|
|
49
|
+
}
|
|
50
|
+
if (!Array.isArray(parsed) || !parsed.every(isEntry))
|
|
51
|
+
throw new types_js_1.ApiError(500, `The app index at ${filePath} is not a list of app entries, so no app is registered.`);
|
|
52
|
+
return parsed;
|
|
53
|
+
},
|
|
54
|
+
async write(entries) {
|
|
55
|
+
const contents = JSON.stringify(entries, null, 2);
|
|
56
|
+
try {
|
|
57
|
+
await drive.update(filePath, contents);
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
// the first publish creates the index; every one after it replaces it
|
|
61
|
+
if (error instanceof types_js_1.ApiError && error.status === 404) {
|
|
62
|
+
await drive.save(filePath, contents);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
exports.createDriveAppStreamStore = createDriveAppStreamStore;
|