@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,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The app index: what has been published, keyed by a UUID.
|
|
3
|
+
*
|
|
4
|
+
* An app is one deployed folder (`appLoc`), the web assets inside it
|
|
5
|
+
* (`appLoc/services/<streamWebFolder>`) and a UUID assigned when it is first
|
|
6
|
+
* published. The id is the key the routes, the listing and the permission rules
|
|
7
|
+
* point at; the name is a label a person reads, and carries no uniqueness rule at
|
|
8
|
+
* all - two apps may be called the same thing without colliding, because nothing
|
|
9
|
+
* is looked up by name.
|
|
10
|
+
*
|
|
11
|
+
* The service is the decisions, and a host supplies the storage. That split is
|
|
12
|
+
* the point of this package: sasjs/server keeps the index in a JSON file beside
|
|
13
|
+
* its drive, the browser build keeps it in the drive it already has, and both
|
|
14
|
+
* answer the same listing because both call this file.
|
|
15
|
+
*
|
|
16
|
+
* One app per folder. Publishing a folder that is already published is the same
|
|
17
|
+
* app published again: it keeps its id (so its URL survives a redeploy) and takes
|
|
18
|
+
* the values the new publish states - except the label, which carries over when
|
|
19
|
+
* the new publish does not name one, so a redeploy that has forgotten what the
|
|
20
|
+
* app is called does not rename it to its folder. Nothing is derived from the
|
|
21
|
+
* path, so a capital, a space or a `..` in a folder name is the drive's business
|
|
22
|
+
* rather than an identity question.
|
|
23
|
+
*/
|
|
24
|
+
import { ApiError } from '../types.js';
|
|
25
|
+
import { newAppId, normaliseAppLoc } from './identity.js';
|
|
26
|
+
/** What a publish answers: enough for a client to print the app's URL. */
|
|
27
|
+
export const appSummary = (entry) => ({
|
|
28
|
+
id: entry.id,
|
|
29
|
+
name: entry.name,
|
|
30
|
+
url: `/app/${entry.id}/`
|
|
31
|
+
});
|
|
32
|
+
/** A folder holding web assets: one segment, so it cannot climb out of the app. */
|
|
33
|
+
const webFolderOf = (value) => {
|
|
34
|
+
if (typeof value !== 'string' || value === '')
|
|
35
|
+
throw new ApiError(400, '"streamWebFolder" is required');
|
|
36
|
+
if (value === '.' || value === '..' || /[\\/]/.test(value))
|
|
37
|
+
throw new ApiError(400, 'streamWebFolder must be a single path segment.');
|
|
38
|
+
return value;
|
|
39
|
+
};
|
|
40
|
+
/** The label an app gets when the publish does not name one. */
|
|
41
|
+
export const defaultAppName = (appLoc) => {
|
|
42
|
+
const segment = appLoc.split('/').filter(Boolean).pop();
|
|
43
|
+
return segment ? segment : 'app';
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Whether two spellings name one folder.
|
|
47
|
+
*
|
|
48
|
+
* A location that cannot be a folder matches nothing rather than throwing: an
|
|
49
|
+
* index written by hand, or by an older shape, holds one bad entry at most, and
|
|
50
|
+
* that entry must not stop every publish and every redeploy lookup on the host.
|
|
51
|
+
*/
|
|
52
|
+
const sameAppLoc = (left, right) => {
|
|
53
|
+
try {
|
|
54
|
+
return normaliseAppLoc(left) === normaliseAppLoc(right);
|
|
55
|
+
}
|
|
56
|
+
catch (_) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
/** The same folder spellings `normaliseAppLoc` collapses, as a `400` rather than a throw. */
|
|
61
|
+
const appLocOf = (value) => {
|
|
62
|
+
try {
|
|
63
|
+
return normaliseAppLoc(value);
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
throw new ApiError(400, error.message);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* A publish request, checked and normalised, with no default applied.
|
|
71
|
+
*
|
|
72
|
+
* Separate from `publish` so a host can refuse a publish BEFORE it writes the
|
|
73
|
+
* deployment: the drive and the index are two writes, and a request the index
|
|
74
|
+
* would refuse must not leave the files behind. `name` is left absent when the
|
|
75
|
+
* caller did not state one, because a redeploy inherits the label it had.
|
|
76
|
+
*/
|
|
77
|
+
export const checkPublishRequest = (request) => {
|
|
78
|
+
const appLoc = appLocOf(request.appLoc);
|
|
79
|
+
// the drive root is not a folder an app can live in: a deploy to `/` writes
|
|
80
|
+
// `//index.html`, which nothing can read, so it is refused rather than indexed
|
|
81
|
+
if (appLoc === '/')
|
|
82
|
+
throw new ApiError(400, 'An app cannot be published at the drive root.');
|
|
83
|
+
const logo = logoOf(request.logo);
|
|
84
|
+
const frameAncestors = frameAncestorsOf(request.frameAncestors);
|
|
85
|
+
return {
|
|
86
|
+
appLoc,
|
|
87
|
+
streamWebFolder: webFolderOf(request.streamWebFolder),
|
|
88
|
+
...(request.name === undefined
|
|
89
|
+
? {}
|
|
90
|
+
: { name: nameOf(request.name, appLoc) }),
|
|
91
|
+
...(logo === undefined ? {} : { logo }),
|
|
92
|
+
...(frameAncestors === undefined ? {} : { frameAncestors }),
|
|
93
|
+
...(request.publishedAt === undefined
|
|
94
|
+
? {}
|
|
95
|
+
: { publishedAt: request.publishedAt })
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
const nameOf = (value, appLoc) => {
|
|
99
|
+
if (value === undefined)
|
|
100
|
+
return defaultAppName(appLoc);
|
|
101
|
+
if (typeof value !== 'string' || value.trim() === '')
|
|
102
|
+
throw new ApiError(400, '"name" must be a non-empty string');
|
|
103
|
+
return value.trim();
|
|
104
|
+
};
|
|
105
|
+
const logoOf = (value) => {
|
|
106
|
+
if (value === undefined)
|
|
107
|
+
return undefined;
|
|
108
|
+
if (typeof value !== 'string' || value === '')
|
|
109
|
+
throw new ApiError(400, '"logo" must be a non-empty string');
|
|
110
|
+
return value;
|
|
111
|
+
};
|
|
112
|
+
const frameAncestorsOf = (value) => {
|
|
113
|
+
if (value === undefined)
|
|
114
|
+
return undefined;
|
|
115
|
+
const list = Array.isArray(value) ? value : [value];
|
|
116
|
+
if (list.length === 0 ||
|
|
117
|
+
list.some((source) => typeof source !== 'string' || source === ''))
|
|
118
|
+
throw new ApiError(400, '"frameAncestors" must be a string or a non-empty list of strings');
|
|
119
|
+
return [...list];
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* The index, over a store.
|
|
123
|
+
*
|
|
124
|
+
* Every mutation reads the whole list, changes it and writes it back, so two
|
|
125
|
+
* publishes that overlap would discard each other's entry - the loser's change
|
|
126
|
+
* vanishing silently, which is the worst way for an index to be wrong. One queue
|
|
127
|
+
* removes the interleaving without a lock.
|
|
128
|
+
*/
|
|
129
|
+
export const createAppStreamService = (store) => {
|
|
130
|
+
let queue = Promise.resolve();
|
|
131
|
+
const mutate = (work) => {
|
|
132
|
+
// `then(work, work)`: a failed mutation must not poison the queue
|
|
133
|
+
const run = queue.then(work, work);
|
|
134
|
+
queue = run.catch(() => undefined);
|
|
135
|
+
return run;
|
|
136
|
+
};
|
|
137
|
+
const readable = (allow) => store.read().then((entries) => entries
|
|
138
|
+
.filter((entry) => (allow ? allow(entry) : true))
|
|
139
|
+
.map((entry) => ({ ...entry }))
|
|
140
|
+
.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0)));
|
|
141
|
+
return {
|
|
142
|
+
list: readable,
|
|
143
|
+
async get(id) {
|
|
144
|
+
const entry = (await store.read()).find((candidate) => candidate.id === id);
|
|
145
|
+
if (!entry)
|
|
146
|
+
throw new ApiError(404, 'No app is published with that id.');
|
|
147
|
+
return { ...entry };
|
|
148
|
+
},
|
|
149
|
+
async forAppLoc(appLoc) {
|
|
150
|
+
// spelled inconsistently by a caller is still one folder; a location that
|
|
151
|
+
// cannot name a folder simply has no app
|
|
152
|
+
let normalised;
|
|
153
|
+
try {
|
|
154
|
+
normalised = normaliseAppLoc(appLoc);
|
|
155
|
+
}
|
|
156
|
+
catch (_) {
|
|
157
|
+
return undefined;
|
|
158
|
+
}
|
|
159
|
+
const entry = (await store.read()).find((candidate) => sameAppLoc(candidate.appLoc, normalised));
|
|
160
|
+
return entry ? { ...entry } : undefined;
|
|
161
|
+
},
|
|
162
|
+
publish(request) {
|
|
163
|
+
return mutate(async () => {
|
|
164
|
+
const checked = checkPublishRequest(request);
|
|
165
|
+
const appLoc = checked.appLoc;
|
|
166
|
+
const streamWebFolder = checked.streamWebFolder;
|
|
167
|
+
const entries = await store.read();
|
|
168
|
+
// the folder is the app, so a redeploy is the same app published again:
|
|
169
|
+
// it keeps the id its URL depends on, and takes the values this publish
|
|
170
|
+
// states rather than the ones the previous publish left behind
|
|
171
|
+
const existing = entries.find((entry) => sameAppLoc(entry.appLoc, appLoc));
|
|
172
|
+
const logo = checked.logo;
|
|
173
|
+
const frameAncestors = checked.frameAncestors;
|
|
174
|
+
const entry = {
|
|
175
|
+
id: existing ? existing.id : newAppId(),
|
|
176
|
+
// the label is the one field a redeploy inherits: everything else is
|
|
177
|
+
// what this publish states
|
|
178
|
+
name: nameOf(checked.name ?? existing?.name, appLoc),
|
|
179
|
+
appLoc,
|
|
180
|
+
streamWebFolder,
|
|
181
|
+
...(logo === undefined ? {} : { logo }),
|
|
182
|
+
...(frameAncestors === undefined ? {} : { frameAncestors }),
|
|
183
|
+
publishedAt: checked.publishedAt ?? new Date().toISOString()
|
|
184
|
+
};
|
|
185
|
+
await store.write([
|
|
186
|
+
...entries.filter((candidate) => !sameAppLoc(candidate.appLoc, appLoc)),
|
|
187
|
+
entry
|
|
188
|
+
]);
|
|
189
|
+
return { ...entry };
|
|
190
|
+
});
|
|
191
|
+
},
|
|
192
|
+
unpublish(id) {
|
|
193
|
+
return mutate(async () => {
|
|
194
|
+
const entries = await store.read();
|
|
195
|
+
const index = entries.findIndex((entry) => entry.id === id);
|
|
196
|
+
// the id is the app, and the only handle: a label is shared by design and
|
|
197
|
+
// a folder is where the app was published rather than what it is
|
|
198
|
+
if (index === -1)
|
|
199
|
+
throw new ApiError(404, 'No app is published with that id.');
|
|
200
|
+
const [removed] = entries.splice(index, 1);
|
|
201
|
+
await store.write(entries);
|
|
202
|
+
return { ...removed };
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
};
|
|
207
|
+
/** An index with no storage behind it: what a browser host and the corpus start from. */
|
|
208
|
+
export class MemoryAppStreamStore {
|
|
209
|
+
constructor(entries = []) {
|
|
210
|
+
this.entries = entries.map((entry) => ({ ...entry }));
|
|
211
|
+
}
|
|
212
|
+
async read() {
|
|
213
|
+
return this.entries.map((entry) => ({ ...entry }));
|
|
214
|
+
}
|
|
215
|
+
async write(entries) {
|
|
216
|
+
this.entries = entries.map((entry) => ({ ...entry }));
|
|
217
|
+
}
|
|
218
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { DriveService } from '../drive/service.js';
|
|
2
|
+
import type { AppStreamStore } from './service.js';
|
|
3
|
+
/** Where the index lives when a host does not choose: hidden, and not an app's. */
|
|
4
|
+
export declare const APP_STREAM_INDEX = "/.sasjs/apps.json";
|
|
5
|
+
export declare const createDriveAppStreamStore: (drive: DriveService, filePath?: string) => AppStreamStore;
|
|
6
|
+
//# sourceMappingURL=store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../../src/appstream/store.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AACvD,OAAO,KAAK,EAAkB,cAAc,EAAE,MAAM,cAAc,CAAA;AAElE,mFAAmF;AACnF,eAAO,MAAM,gBAAgB,sBAAsB,CAAA;AAgBnD,eAAO,MAAM,yBAAyB,GACpC,OAAO,YAAY,EACnB,WAAU,MAAyB,KAClC,cAsDD,CAAA"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The index kept as a file on the drive.
|
|
3
|
+
*
|
|
4
|
+
* A host that has a drive and no other place to put the index can use this: the
|
|
5
|
+
* browser build does, where the drive is the only durable thing there is, and the
|
|
6
|
+
* file rides along with the persistence the drive already has.
|
|
7
|
+
*
|
|
8
|
+
* An absent file is an empty index - the state a host starts in. A file that is
|
|
9
|
+
* not an index is an error rather than an empty index: a deploy that silently
|
|
10
|
+
* unregistered every app because the file had a trailing comma is the failure
|
|
11
|
+
* this refuses to hide.
|
|
12
|
+
*/
|
|
13
|
+
import { ApiError } from '../types.js';
|
|
14
|
+
/** Where the index lives when a host does not choose: hidden, and not an app's. */
|
|
15
|
+
export const APP_STREAM_INDEX = '/.sasjs/apps.json';
|
|
16
|
+
const isEntry = (value) => {
|
|
17
|
+
if (typeof value !== 'object' || value === null)
|
|
18
|
+
return false;
|
|
19
|
+
const entry = value;
|
|
20
|
+
return (typeof entry.id === 'string' &&
|
|
21
|
+
typeof entry.name === 'string' &&
|
|
22
|
+
typeof entry.appLoc === 'string' &&
|
|
23
|
+
typeof entry.streamWebFolder === 'string' &&
|
|
24
|
+
typeof entry.publishedAt === 'string');
|
|
25
|
+
};
|
|
26
|
+
export const createDriveAppStreamStore = (drive, filePath = APP_STREAM_INDEX) => ({
|
|
27
|
+
async read() {
|
|
28
|
+
let contents;
|
|
29
|
+
try {
|
|
30
|
+
contents = (await drive.read(filePath)).contents;
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
// nothing published yet is not an error, it is the empty case
|
|
34
|
+
if (error instanceof ApiError && error.status === 404)
|
|
35
|
+
return [];
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
if (typeof contents !== 'string')
|
|
39
|
+
throw new ApiError(500, `The app index at ${filePath} is not text, so no app can be read from it.`);
|
|
40
|
+
let parsed;
|
|
41
|
+
try {
|
|
42
|
+
parsed = JSON.parse(contents);
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
throw new ApiError(500, `The app index at ${filePath} is not valid JSON, so no app is registered: ${error.message}`);
|
|
46
|
+
}
|
|
47
|
+
if (!Array.isArray(parsed) || !parsed.every(isEntry))
|
|
48
|
+
throw new ApiError(500, `The app index at ${filePath} is not a list of app entries, so no app is registered.`);
|
|
49
|
+
return parsed;
|
|
50
|
+
},
|
|
51
|
+
async write(entries) {
|
|
52
|
+
const contents = JSON.stringify(entries, null, 2);
|
|
53
|
+
try {
|
|
54
|
+
await drive.update(filePath, contents);
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
// the first publish creates the index; every one after it replaces it
|
|
58
|
+
if (error instanceof ApiError && error.status === 404) {
|
|
59
|
+
await drive.save(filePath, contents);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
});
|
|
@@ -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,IAAI,EAAE,MAAM,GAAG,IAAI,CAAA;IACnB,mCAAmC;IACnC,GAAG,EAAE,MAAM,CAAA;IACX,WAAW,EAAE,MAAM,GAAG,IAAI,CAAA;CAC3B;AAED;;;GAGG;AACH,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,MAAM,CAAA;IACd,IAAI,CAAC,EAAE,MAAM,GAAG,UAAU,CAAA;IAC1B,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAChC,sCAAsC;IACtC,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAED,uEAAuE;AACvE,MAAM,MAAM,SAAS,GAAG,CAAC,KAAK,EAAE,cAAc,KAAK,OAAO,CAAA;AAE1D,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,MAAM,CAAA;IACd,uEAAuE;IACvE,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,CAAC,EAAE,SAAS,CAAA;CAClB;AAED,MAAM,WAAW,mBAAmB;IAClC,4BAA4B;IAC5B,IAAI,CAAC,KAAK,CAAC,EAAE,SAAS,GAAG,OAAO,CAAC;QAAE,IAAI,EAAE,YAAY,EAAE,CAAA;KAAE,CAAC,CAAA;IAC1D,mBAAmB;IACnB,OAAO,CAAC,KAAK,CAAC,EAAE,SAAS,GAAG,OAAO,CAAC,aAAa,CAAC,CAAA;IAClD,+DAA+D;IAC/D,OAAO,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,aAAa,CAAC,CAAA;CAC3D;AAED,MAAM,WAAW,mBAAmB;IAClC,uDAAuD;IACvD,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,4CAA4C;IAC5C,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAUD;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,GAAI,OAAO,MAAM,KAAG,MACmB,CAAA;AA2B9D,qEAAqE;AACrE,eAAO,MAAM,cAAc,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
|
+
'&': '&',
|
|
28
|
+
'<': '<',
|
|
29
|
+
'>': '>',
|
|
30
|
+
'"': '"',
|
|
31
|
+
"'": '''
|
|
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
|
|
9
|
-
* remove
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* name-keyed
|
|
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,
|
|
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"}
|