@sasjs/api 0.35.0 → 0.36.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/build/browser.js +546 -56
- package/build/cjs/viya/compute.js +223 -0
- package/build/cjs/viya/files.js +199 -0
- package/build/cjs/viya/ids.js +60 -0
- package/build/cjs/viya/overlay.js +15 -40
- package/build/cjs/viya/ports.js +5 -0
- package/build/cjs/viya/seed.js +120 -0
- package/build/cjs/viya/service.js +51 -85
- package/build/cjs/viya/sessions.js +114 -0
- package/build/esm/host/dispatch.d.ts +10 -0
- package/build/esm/host/dispatch.d.ts.map +1 -1
- package/build/esm/viya/compute.d.ts +39 -0
- package/build/esm/viya/compute.d.ts.map +1 -0
- package/build/esm/viya/compute.js +219 -0
- package/build/esm/viya/files.d.ts +33 -0
- package/build/esm/viya/files.d.ts.map +1 -0
- package/build/esm/viya/files.js +196 -0
- package/build/esm/viya/ids.d.ts +35 -0
- package/build/esm/viya/ids.d.ts.map +1 -0
- package/build/esm/viya/ids.js +55 -0
- package/build/esm/viya/overlay.d.ts.map +1 -1
- package/build/esm/viya/overlay.js +15 -40
- package/build/esm/viya/ports.d.ts +12 -0
- package/build/esm/viya/ports.d.ts.map +1 -0
- package/build/esm/viya/ports.js +1 -0
- package/build/esm/viya/seed.d.ts +60 -0
- package/build/esm/viya/seed.d.ts.map +1 -0
- package/build/esm/viya/seed.js +111 -0
- package/build/esm/viya/service.d.ts +0 -16
- package/build/esm/viya/service.d.ts.map +1 -1
- package/build/esm/viya/service.js +10 -43
- package/build/esm/viya/sessions.d.ts +63 -0
- package/build/esm/viya/sessions.d.ts.map +1 -0
- package/build/esm/viya/sessions.js +108 -0
- package/package.json +3 -2
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { ApiError } from '../types.js';
|
|
2
|
+
import { normaliseServerUrl } from './seed.js';
|
|
3
|
+
import { seedOf } from './seed.js';
|
|
4
|
+
import { estateFor } from './sessions.js';
|
|
5
|
+
import { EMULATED_USER, idFor } from './ids.js';
|
|
6
|
+
/** The name the emulator answers when a client asks for no particular context. */
|
|
7
|
+
export const DEFAULT_CONTEXT_NAME = 'Emulated Compute Context';
|
|
8
|
+
/** The one context id this estate mints, whatever name it is asked for. */
|
|
9
|
+
export const EMULATED_CONTEXT_ID = idFor('emulated-context', 'context');
|
|
10
|
+
/**
|
|
11
|
+
* The links a client follows, by rel.
|
|
12
|
+
*
|
|
13
|
+
* The rels are not decoration: the adapter reads `state` to poll a session and a
|
|
14
|
+
* job, `log` to fetch the log, `up` to reach the session from a job. A body that
|
|
15
|
+
* carried only `self` answered 201 and then failed the client, which is how the
|
|
16
|
+
* conformance harness found this.
|
|
17
|
+
*/
|
|
18
|
+
const relLinks = (type, rels) => rels.map(([rel, href]) => ({ href, uri: href, rel, method: 'GET', type }));
|
|
19
|
+
const linksFor = (uri, type, collection = false, itemType = false) => [
|
|
20
|
+
// the collection link comes first, because that is the order the capture
|
|
21
|
+
// recorded and the item shape assertion walks the first element
|
|
22
|
+
...(collection
|
|
23
|
+
? [
|
|
24
|
+
{
|
|
25
|
+
href: `${uri}/members`,
|
|
26
|
+
uri: `${uri}/members`,
|
|
27
|
+
rel: 'members',
|
|
28
|
+
method: 'GET',
|
|
29
|
+
type,
|
|
30
|
+
itemType: type
|
|
31
|
+
}
|
|
32
|
+
]
|
|
33
|
+
: []),
|
|
34
|
+
{
|
|
35
|
+
href: uri,
|
|
36
|
+
uri,
|
|
37
|
+
rel: 'self',
|
|
38
|
+
method: 'GET',
|
|
39
|
+
type,
|
|
40
|
+
// the log's capture records itemType on its first link, so it is stamped
|
|
41
|
+
// there rather than invented as a members link the estate does not have
|
|
42
|
+
...(itemType ? { itemType: type } : {})
|
|
43
|
+
}
|
|
44
|
+
];
|
|
45
|
+
const estateOf = (services) => estateFor(seedOf(services.viya));
|
|
46
|
+
/**
|
|
47
|
+
* The context name a client asked for.
|
|
48
|
+
*
|
|
49
|
+
* The adapter filters by name (`filter=eq(name,'X')`), so the answer is the one
|
|
50
|
+
* emulated context under the requested name. A second estate is not something an
|
|
51
|
+
* emulator can have, and an empty list would fail a client that has nothing wrong
|
|
52
|
+
* with it.
|
|
53
|
+
*/
|
|
54
|
+
export const contextNameFrom = (filter) => {
|
|
55
|
+
if (!filter)
|
|
56
|
+
return DEFAULT_CONTEXT_NAME;
|
|
57
|
+
const match = /name\s*,\s*'([^']*)'|name\s*,\s*"([^"]*)"/i.exec(filter);
|
|
58
|
+
return match?.[1] ?? match?.[2] ?? DEFAULT_CONTEXT_NAME;
|
|
59
|
+
};
|
|
60
|
+
const contextBody = (name) => ({
|
|
61
|
+
id: EMULATED_CONTEXT_ID,
|
|
62
|
+
name,
|
|
63
|
+
createdBy: EMULATED_USER.id,
|
|
64
|
+
version: 2,
|
|
65
|
+
links: linksFor(`/compute/contexts/${EMULATED_CONTEXT_ID}`, 'application/vnd.sas.compute.context')
|
|
66
|
+
});
|
|
67
|
+
const sessionBody = (services, session, autoExecLines) => {
|
|
68
|
+
const { id } = session;
|
|
69
|
+
const uri = `/compute/sessions/${id}`;
|
|
70
|
+
return {
|
|
71
|
+
id,
|
|
72
|
+
applicationName: 'emulated',
|
|
73
|
+
owner: EMULATED_USER.id,
|
|
74
|
+
serverId: idFor(normaliseServerUrl(services.viya?.serverUrl) || 'emulated', 'server'),
|
|
75
|
+
state: 'idle',
|
|
76
|
+
attributes: {
|
|
77
|
+
homeDirectory: '/tmp/emulated',
|
|
78
|
+
sessionInactiveTimeout: 3600
|
|
79
|
+
},
|
|
80
|
+
environment: { autoExecLines },
|
|
81
|
+
// the real clock: the adapter computes a session's age from this stamp and
|
|
82
|
+
// expires it against sessionInactiveTimeout
|
|
83
|
+
creationTimeStamp: session.createdAt,
|
|
84
|
+
serviceAPIVersion: 1,
|
|
85
|
+
sessionConditionCode: 0,
|
|
86
|
+
stateElapsedTime: 0,
|
|
87
|
+
version: 2,
|
|
88
|
+
links: relLinks('application/vnd.sas.compute.session', [
|
|
89
|
+
['self', uri],
|
|
90
|
+
['state', `${uri}/state`],
|
|
91
|
+
['log', `${uri}/log`],
|
|
92
|
+
['jobs', `${uri}/jobs`]
|
|
93
|
+
])
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
const jobBody = (job) => {
|
|
97
|
+
const uri = `/compute/sessions/${job.sessionId}/jobs/${job.id}`;
|
|
98
|
+
return {
|
|
99
|
+
id: job.id,
|
|
100
|
+
name: job.name,
|
|
101
|
+
sessionId: job.sessionId,
|
|
102
|
+
state: job.state,
|
|
103
|
+
creationTimeStamp: job.creationTimeStamp,
|
|
104
|
+
completedTimeStamp: job.completedTimeStamp,
|
|
105
|
+
jobConditionCode: 0,
|
|
106
|
+
stateElapsedTime: 1,
|
|
107
|
+
listingStatistics: {
|
|
108
|
+
lineCount: job.log.length,
|
|
109
|
+
modifiedTimeStamp: job.completedTimeStamp
|
|
110
|
+
},
|
|
111
|
+
logStatistics: {
|
|
112
|
+
lineCount: job.log.length,
|
|
113
|
+
modifiedTimeStamp: job.completedTimeStamp
|
|
114
|
+
},
|
|
115
|
+
statistics: { systemCpuTime: 0.01, userCpuTime: 0.02 },
|
|
116
|
+
version: 2,
|
|
117
|
+
links: relLinks('application/vnd.sas.compute.job', [
|
|
118
|
+
['self', uri],
|
|
119
|
+
['state', `${uri}/state`],
|
|
120
|
+
['log', `${uri}/log`],
|
|
121
|
+
['up', `/compute/sessions/${job.sessionId}`]
|
|
122
|
+
])
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
/** The session the request names, or the 404 a client that lost it expects. */
|
|
126
|
+
const sessionOf = (services, id) => {
|
|
127
|
+
const session = estateOf(services).session(id);
|
|
128
|
+
if (!session)
|
|
129
|
+
throw new ApiError(404, `Session with ID '${id}' was not found.`);
|
|
130
|
+
return session;
|
|
131
|
+
};
|
|
132
|
+
const jobOf = (services, sessionId, jobId) => {
|
|
133
|
+
const job = sessionOf(services, sessionId).jobs.get(jobId);
|
|
134
|
+
if (!job)
|
|
135
|
+
throw new ApiError(404, `Job with ID '${jobId}' was not found.`);
|
|
136
|
+
return job;
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* The `{}` values a request carried, in path order.
|
|
140
|
+
*
|
|
141
|
+
* `dispatchViya` sets `segments` on every request it hands a handler, so there is
|
|
142
|
+
* no absent case to answer - a fallback here would be a branch nothing reaches.
|
|
143
|
+
*/
|
|
144
|
+
const segmentsOf = (params) => params.segments;
|
|
145
|
+
export const COMPUTE_HANDLERS = {
|
|
146
|
+
'GET /compute/contexts': async ({ request }) => {
|
|
147
|
+
const limit = Number(request.query?.limit ?? 0) || 0;
|
|
148
|
+
return {
|
|
149
|
+
status: 200,
|
|
150
|
+
body: {
|
|
151
|
+
accept: 'application/json',
|
|
152
|
+
name: 'contexts',
|
|
153
|
+
start: 0,
|
|
154
|
+
limit,
|
|
155
|
+
count: 1,
|
|
156
|
+
version: 2,
|
|
157
|
+
items: [contextBody(contextNameFrom(request.query?.filter))],
|
|
158
|
+
links: linksFor('/compute/contexts', 'application/vnd.sas.compute.context', true)
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
},
|
|
162
|
+
'POST /compute/contexts/{}/sessions': async ({ services, params, request }) => {
|
|
163
|
+
if (params.id !== EMULATED_CONTEXT_ID)
|
|
164
|
+
throw new ApiError(404, `Context with ID '${params.id}' was not found.`);
|
|
165
|
+
const body = (request.body ?? {});
|
|
166
|
+
const session = estateOf(services).create();
|
|
167
|
+
return {
|
|
168
|
+
status: 201,
|
|
169
|
+
body: sessionBody(services, session, body.environment?.autoExecLines ?? [])
|
|
170
|
+
};
|
|
171
|
+
},
|
|
172
|
+
'GET /compute/sessions/{}/state': async ({ services, params }) => {
|
|
173
|
+
sessionOf(services, params.id);
|
|
174
|
+
// a bare enum, not an object: the adapter calls `.trim()` on the answer, and
|
|
175
|
+
// the capture recorded no shape here because there is no JSON body
|
|
176
|
+
return { status: 200, body: 'idle' };
|
|
177
|
+
},
|
|
178
|
+
'DELETE /compute/sessions/{}': async ({ services, params }) => {
|
|
179
|
+
sessionOf(services, params.id);
|
|
180
|
+
estateOf(services).remove(params.id);
|
|
181
|
+
return { status: 204, body: undefined };
|
|
182
|
+
},
|
|
183
|
+
'POST /compute/sessions/{}/jobs': async ({ services, params, request }) => {
|
|
184
|
+
const session = sessionOf(services, params.id);
|
|
185
|
+
const job = estateOf(services).submit(session, (request.body ?? {}));
|
|
186
|
+
return { status: 201, body: jobBody(job) };
|
|
187
|
+
},
|
|
188
|
+
'GET /compute/sessions/{}/jobs/{}': async ({ services, params }) => {
|
|
189
|
+
const [sessionId, jobId] = segmentsOf(params);
|
|
190
|
+
return { status: 200, body: jobBody(jobOf(services, sessionId, jobId)) };
|
|
191
|
+
},
|
|
192
|
+
'GET /compute/sessions/{}/jobs/{}/state': async ({ services, params }) => {
|
|
193
|
+
const [sessionId, jobId] = segmentsOf(params);
|
|
194
|
+
return {
|
|
195
|
+
status: 200,
|
|
196
|
+
body: jobOf(services, sessionId, jobId).state
|
|
197
|
+
};
|
|
198
|
+
},
|
|
199
|
+
'GET /compute/sessions/{}/jobs/{}/log/content': async ({ services, params, request }) => {
|
|
200
|
+
const [sessionId, jobId] = segmentsOf(params);
|
|
201
|
+
const job = jobOf(services, sessionId, jobId);
|
|
202
|
+
const start = Number(request.query?.start ?? 0) || 0;
|
|
203
|
+
const limit = Number(request.query?.limit ?? 0) || 0;
|
|
204
|
+
const lines = job.log.slice(start, limit ? start + limit : undefined);
|
|
205
|
+
return {
|
|
206
|
+
status: 200,
|
|
207
|
+
body: {
|
|
208
|
+
accept: 'application/json',
|
|
209
|
+
name: 'content',
|
|
210
|
+
start,
|
|
211
|
+
limit: limit || lines.length,
|
|
212
|
+
count: job.log.length,
|
|
213
|
+
version: 2,
|
|
214
|
+
items: lines.map((line) => ({ line, type: 'NOTE', version: 2 })),
|
|
215
|
+
links: linksFor(`/compute/sessions/${job.sessionId}/jobs/${job.id}/log`, 'application/vnd.sas.compute.job.log', false, true)
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The file family: upload, read back, rename, read content, replace content.
|
|
3
|
+
*
|
|
4
|
+
* Five operations, all answered by the drive the package already has, so nothing
|
|
5
|
+
* here stores bytes of its own: an uploaded file is a file in the drive, and
|
|
6
|
+
* reading it back is the drive's read. That is also what makes an upload visible
|
|
7
|
+
* to the folder family - the same estate holds both views.
|
|
8
|
+
*
|
|
9
|
+
* A file id is derived from its path, like a folder id: a client stores the id an
|
|
10
|
+
* upload answered and asks for it later, so the id cannot move between runs.
|
|
11
|
+
*/
|
|
12
|
+
import type { ApiResponse, Services } from '../host/dispatch.js';
|
|
13
|
+
/** What an upload answers about itself, whichever operation asked. */
|
|
14
|
+
export interface EmulatedFile {
|
|
15
|
+
name: string;
|
|
16
|
+
path: string;
|
|
17
|
+
size: number;
|
|
18
|
+
}
|
|
19
|
+
export declare const FILE_HANDLERS: Record<string, (context: {
|
|
20
|
+
services: Services;
|
|
21
|
+
request: {
|
|
22
|
+
query?: Record<string, string | undefined>;
|
|
23
|
+
body?: unknown;
|
|
24
|
+
file?: {
|
|
25
|
+
name: string;
|
|
26
|
+
contents: string | Uint8Array;
|
|
27
|
+
};
|
|
28
|
+
token?: string;
|
|
29
|
+
subject?: unknown;
|
|
30
|
+
};
|
|
31
|
+
params: Record<string, string>;
|
|
32
|
+
}) => Promise<ApiResponse>>;
|
|
33
|
+
//# sourceMappingURL=files.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"files.d.ts","sourceRoot":"","sources":["../../../src/viya/files.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAA;AAOhE,sEAAsE;AACtE,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;CACb;AA4KD,eAAO,MAAM,aAAa,EAAE,MAAM,CAChC,MAAM,EACN,CAAC,OAAO,EAAE;IACR,QAAQ,EAAE,QAAQ,CAAA;IAClB,OAAO,EAAE;QACP,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;QAC1C,IAAI,CAAC,EAAE,OAAO,CAAA;QACd,IAAI,CAAC,EAAE;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,QAAQ,EAAE,MAAM,GAAG,UAAU,CAAA;SAAE,CAAA;QACtD,KAAK,CAAC,EAAE,MAAM,CAAA;QACd,OAAO,CAAC,EAAE,OAAO,CAAA;KAClB,CAAA;IACD,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAC/B,KAAK,OAAO,CAAC,WAAW,CAAC,CA6E3B,CAAA"}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { ApiError } from '../types.js';
|
|
2
|
+
import { requireDrive } from './ports.js';
|
|
3
|
+
import { EMULATED_NOW, EMULATED_USER, idFor } from './ids.js';
|
|
4
|
+
const contentTypeOf = (name) => {
|
|
5
|
+
if (/\.sas$/i.test(name))
|
|
6
|
+
return 'text/plain';
|
|
7
|
+
if (/\.(json)$/i.test(name))
|
|
8
|
+
return 'application/json';
|
|
9
|
+
if (/\.(csv|txt|log)$/i.test(name))
|
|
10
|
+
return 'text/plain';
|
|
11
|
+
if (/\.(html?)$/i.test(name))
|
|
12
|
+
return 'text/html';
|
|
13
|
+
if (/\.(png)$/i.test(name))
|
|
14
|
+
return 'image/png';
|
|
15
|
+
if (/\.(jpe?g)$/i.test(name))
|
|
16
|
+
return 'image/jpeg';
|
|
17
|
+
return 'application/octet-stream';
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* What the estate records about a file.
|
|
21
|
+
*
|
|
22
|
+
* `properties` carries the five keys the capture recorded, with the values a
|
|
23
|
+
* client reads rather than placeholders: the three `content.*` flags say what can
|
|
24
|
+
* be done with the file, and `typeRegistrant` says who registered the type.
|
|
25
|
+
*/
|
|
26
|
+
const fileBody = (file) => {
|
|
27
|
+
const id = idFor(file.path, 'file');
|
|
28
|
+
const uri = `/files/files/${id}`;
|
|
29
|
+
const contentType = contentTypeOf(file.name);
|
|
30
|
+
return {
|
|
31
|
+
id,
|
|
32
|
+
name: file.name,
|
|
33
|
+
contentType,
|
|
34
|
+
contentDisposition: `attachment; filename="${file.name}"`,
|
|
35
|
+
encoding: 'UTF-8',
|
|
36
|
+
size: file.size,
|
|
37
|
+
fileStatus: 'complete',
|
|
38
|
+
fileVersion: 1,
|
|
39
|
+
version: 2,
|
|
40
|
+
typeDefName: 'file',
|
|
41
|
+
createdBy: EMULATED_USER.id,
|
|
42
|
+
modifiedBy: EMULATED_USER.id,
|
|
43
|
+
creationTimeStamp: EMULATED_NOW,
|
|
44
|
+
modifiedTimeStamp: EMULATED_NOW,
|
|
45
|
+
quarantine: false,
|
|
46
|
+
searchable: true,
|
|
47
|
+
virusDetected: false,
|
|
48
|
+
urlDetected: false,
|
|
49
|
+
properties: {
|
|
50
|
+
'content.download': 'true',
|
|
51
|
+
'content.openInBrowser': 'true',
|
|
52
|
+
'content.preview': 'false',
|
|
53
|
+
sasIconKey: 'file',
|
|
54
|
+
typeRegistrant: EMULATED_USER.id
|
|
55
|
+
},
|
|
56
|
+
links: [
|
|
57
|
+
{
|
|
58
|
+
href: uri,
|
|
59
|
+
uri,
|
|
60
|
+
rel: 'self',
|
|
61
|
+
method: 'GET',
|
|
62
|
+
type: 'application/vnd.sas.file'
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
href: `${uri}/content`,
|
|
66
|
+
uri: `${uri}/content`,
|
|
67
|
+
rel: 'content',
|
|
68
|
+
method: 'GET',
|
|
69
|
+
type: 'application/vnd.sas.file.content'
|
|
70
|
+
}
|
|
71
|
+
]
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* The bytes in a string, counted rather than measured.
|
|
76
|
+
*
|
|
77
|
+
* `Buffer.byteLength` is not available in the browser build, and the number this
|
|
78
|
+
* returns is the size a client sees, so it has to be right for non-ASCII too.
|
|
79
|
+
*/
|
|
80
|
+
const utf8Length = (text) => {
|
|
81
|
+
let bytes = 0;
|
|
82
|
+
for (const character of text) {
|
|
83
|
+
const point = character.codePointAt(0);
|
|
84
|
+
bytes += point < 0x80 ? 1 : point < 0x800 ? 2 : point < 0x10000 ? 3 : 4;
|
|
85
|
+
}
|
|
86
|
+
return bytes;
|
|
87
|
+
};
|
|
88
|
+
const sizeOf = (contents) => typeof contents === 'string' ? utf8Length(contents) : contents.byteLength;
|
|
89
|
+
/** Every file in the drive, as `path` and name. */
|
|
90
|
+
const filesIn = async (drive) => {
|
|
91
|
+
const walk = (node, found) => {
|
|
92
|
+
if (!node.isFolder)
|
|
93
|
+
found.push({ name: node.name, path: node.relativePath, size: 0 });
|
|
94
|
+
for (const child of node.children)
|
|
95
|
+
walk(child, found);
|
|
96
|
+
return found;
|
|
97
|
+
};
|
|
98
|
+
return walk((await drive.tree('/')).tree, []);
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* The path a file id names, or the 404 a client with a stale id expects.
|
|
102
|
+
*
|
|
103
|
+
* Resolved against the drive rather than remembered in a map: a rename changes the
|
|
104
|
+
* path and therefore the id, and a map would keep answering for the old one.
|
|
105
|
+
*/
|
|
106
|
+
const pathForFileId = async (drive, id) => {
|
|
107
|
+
const file = (await filesIn(drive)).find((candidate) => idFor(candidate.path, 'file') === id);
|
|
108
|
+
if (!file)
|
|
109
|
+
throw new ApiError(404, `File with ID '${id}' was not found.`);
|
|
110
|
+
return file.path;
|
|
111
|
+
};
|
|
112
|
+
const bodyOf = async (drive, path) => {
|
|
113
|
+
const read = await drive.read(path);
|
|
114
|
+
return {
|
|
115
|
+
name: path.split('/').pop(),
|
|
116
|
+
path,
|
|
117
|
+
size: sizeOf(read.contents)
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
/** The parent folder a request names, or the root it means by its absence. */
|
|
121
|
+
const parentOf = async (drive, uri) => {
|
|
122
|
+
if (!uri)
|
|
123
|
+
return '/';
|
|
124
|
+
const id = uri.split('/').pop();
|
|
125
|
+
const tree = await drive.tree('/');
|
|
126
|
+
const walk = (node) => {
|
|
127
|
+
if (node.isFolder && idFor(node.relativePath, 'folder') === id)
|
|
128
|
+
return node.relativePath;
|
|
129
|
+
for (const child of node.children) {
|
|
130
|
+
const hit = walk(child);
|
|
131
|
+
if (hit !== undefined)
|
|
132
|
+
return hit;
|
|
133
|
+
}
|
|
134
|
+
return undefined;
|
|
135
|
+
};
|
|
136
|
+
const found = walk(tree.tree);
|
|
137
|
+
if (found === undefined)
|
|
138
|
+
throw new ApiError(404, `Folder with ID '${id}' was not found.`);
|
|
139
|
+
return found;
|
|
140
|
+
};
|
|
141
|
+
export const FILE_HANDLERS = {
|
|
142
|
+
'POST /files/files': async ({ services, request }) => {
|
|
143
|
+
const drive = requireDrive(services);
|
|
144
|
+
const upload = request.file;
|
|
145
|
+
if (!upload?.name)
|
|
146
|
+
throw new ApiError(400, 'A file named `file` is required.');
|
|
147
|
+
const parent = await parentOf(drive, request.query?.parentFolderUri);
|
|
148
|
+
const path = `${parent}/${upload.name}`.replace(/\/{2,}/g, '/');
|
|
149
|
+
await drive.save(path, upload.contents);
|
|
150
|
+
return { status: 201, body: fileBody(await bodyOf(drive, path)) };
|
|
151
|
+
},
|
|
152
|
+
'GET /files/files/{}': async ({ services, params }) => {
|
|
153
|
+
const drive = requireDrive(services);
|
|
154
|
+
const path = await pathForFileId(drive, params.id);
|
|
155
|
+
return { status: 200, body: fileBody(await bodyOf(drive, path)) };
|
|
156
|
+
},
|
|
157
|
+
'PATCH /files/files/{}': async ({ services, params, request }) => {
|
|
158
|
+
const drive = requireDrive(services);
|
|
159
|
+
const path = await pathForFileId(drive, params.id);
|
|
160
|
+
const body = (request.body ?? {});
|
|
161
|
+
const name = body.name ?? path.split('/').pop();
|
|
162
|
+
const target = `${path.split('/').slice(0, -1).join('/')}/${name}`.replace(/\/{2,}/g, '/');
|
|
163
|
+
if (target !== path)
|
|
164
|
+
await drive.rename(path, target);
|
|
165
|
+
return { status: 200, body: fileBody(await bodyOf(drive, target)) };
|
|
166
|
+
},
|
|
167
|
+
'GET /files/files/{}/content': async ({ services, params }) => {
|
|
168
|
+
const drive = requireDrive(services);
|
|
169
|
+
const path = await pathForFileId(drive, params.id);
|
|
170
|
+
const read = await drive.read(path);
|
|
171
|
+
// `contents`, not `body`: ApiResponse reserves that field for a raw body,
|
|
172
|
+
// e.g. the driver route that answers with the file itself. A host that maps
|
|
173
|
+
// `body` JSON-encodes the string and declares text/html over a quoted value.
|
|
174
|
+
return {
|
|
175
|
+
status: 200,
|
|
176
|
+
contents: read.contents,
|
|
177
|
+
headers: {
|
|
178
|
+
'content-type': contentTypeOf(path.split('/').pop())
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
},
|
|
182
|
+
'PUT /files/files/{}/content': async ({ services, params, request }) => {
|
|
183
|
+
const drive = requireDrive(services);
|
|
184
|
+
const path = await pathForFileId(drive, params.id);
|
|
185
|
+
// a host may hand the bytes over as the multipart part or as the body
|
|
186
|
+
const body = request.body;
|
|
187
|
+
const contents = request.file?.contents ??
|
|
188
|
+
(typeof body === 'string'
|
|
189
|
+
? body
|
|
190
|
+
: body?.contents);
|
|
191
|
+
if (contents === undefined || contents === null)
|
|
192
|
+
throw new ApiError(400, 'File content is required.');
|
|
193
|
+
await drive.update(path, contents);
|
|
194
|
+
return { status: 200, body: fileBody(await bodyOf(drive, path)) };
|
|
195
|
+
}
|
|
196
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The values that identify the emulated estate, in one place.
|
|
3
|
+
*
|
|
4
|
+
* They live apart from the handlers because two handler modules need them: the
|
|
5
|
+
* folder family derives a folder id, the compute family derives a context and a
|
|
6
|
+
* server id, and a module that imports the other's handler map to reach them
|
|
7
|
+
* would be a cycle.
|
|
8
|
+
*/
|
|
9
|
+
/** The emulated clock: a fixed instant, so a body is reproducible. */
|
|
10
|
+
export declare const EMULATED_NOW = "2026-01-01T00:00:00.000Z";
|
|
11
|
+
/** The caller the emulated estate reports when a host does not name one. */
|
|
12
|
+
export declare const EMULATED_USER: {
|
|
13
|
+
id: string;
|
|
14
|
+
name: string;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* A uuid shape from a string, deterministically.
|
|
18
|
+
*
|
|
19
|
+
* Two 32-bit FNV-1a passes give enough of a hash to fill a uuid's fields, and the
|
|
20
|
+
* version/variant nibbles are set so the result is a well-formed v4-shaped id.
|
|
21
|
+
* Collisions are possible in principle and irrelevant here: the id is a handle
|
|
22
|
+
* the emulator hands back, not a uniqueness guarantee.
|
|
23
|
+
*/
|
|
24
|
+
export declare const idFor: (path: string, kind?: string) => string;
|
|
25
|
+
/**
|
|
26
|
+
* One folder path, however it was spelled.
|
|
27
|
+
*
|
|
28
|
+
* The adapter joins a root of '/' to a path of '/app' and sends '//app', which is
|
|
29
|
+
* a different string and - because an id is derived from the path - a different
|
|
30
|
+
* id. A client that then asks for that id is asking about a folder the drive does
|
|
31
|
+
* not have, so every path is collapsed to one spelling before anything derives
|
|
32
|
+
* from it.
|
|
33
|
+
*/
|
|
34
|
+
export declare const normalisePath: (path: string) => string;
|
|
35
|
+
//# sourceMappingURL=ids.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ids.d.ts","sourceRoot":"","sources":["../../../src/viya/ids.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,sEAAsE;AACtE,eAAO,MAAM,YAAY,6BAA6B,CAAA;AAEtD,4EAA4E;AAC5E,eAAO,MAAM,aAAa;;;CAAiD,CAAA;AAE3E;;;;;;;GAOG;AACH,eAAO,MAAM,KAAK,GAAI,MAAM,MAAM,EAAE,aAAe,KAAG,MAyBrD,CAAA;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,aAAa,GAAI,MAAM,MAAM,KAAG,MAI5C,CAAA"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The values that identify the emulated estate, in one place.
|
|
3
|
+
*
|
|
4
|
+
* They live apart from the handlers because two handler modules need them: the
|
|
5
|
+
* folder family derives a folder id, the compute family derives a context and a
|
|
6
|
+
* server id, and a module that imports the other's handler map to reach them
|
|
7
|
+
* would be a cycle.
|
|
8
|
+
*/
|
|
9
|
+
/** The emulated clock: a fixed instant, so a body is reproducible. */
|
|
10
|
+
export const EMULATED_NOW = '2026-01-01T00:00:00.000Z';
|
|
11
|
+
/** The caller the emulated estate reports when a host does not name one. */
|
|
12
|
+
export const EMULATED_USER = { id: 'emulated-user', name: 'Emulated User' };
|
|
13
|
+
/**
|
|
14
|
+
* A uuid shape from a string, deterministically.
|
|
15
|
+
*
|
|
16
|
+
* Two 32-bit FNV-1a passes give enough of a hash to fill a uuid's fields, and the
|
|
17
|
+
* version/variant nibbles are set so the result is a well-formed v4-shaped id.
|
|
18
|
+
* Collisions are possible in principle and irrelevant here: the id is a handle
|
|
19
|
+
* the emulator hands back, not a uniqueness guarantee.
|
|
20
|
+
*/
|
|
21
|
+
export const idFor = (path, kind = 'folder') => {
|
|
22
|
+
const hash = (input, seed) => {
|
|
23
|
+
let value = seed;
|
|
24
|
+
for (let index = 0; index < input.length; index++) {
|
|
25
|
+
value ^= input.charCodeAt(index);
|
|
26
|
+
value = Math.imul(value, 16777619) >>> 0;
|
|
27
|
+
}
|
|
28
|
+
return value.toString(16).padStart(8, '0');
|
|
29
|
+
};
|
|
30
|
+
const raw = hash(`${kind}:${path}`, 2166136261) +
|
|
31
|
+
hash(`${kind}:${path}`, 2246822519) +
|
|
32
|
+
hash(`${kind}:${path}`, 3266489917) +
|
|
33
|
+
hash(`${kind}:${path}`, 668265263);
|
|
34
|
+
const hex = raw.slice(0, 32);
|
|
35
|
+
return [
|
|
36
|
+
hex.slice(0, 8),
|
|
37
|
+
hex.slice(8, 12),
|
|
38
|
+
`4${hex.slice(13, 16)}`,
|
|
39
|
+
`a${hex.slice(17, 20)}`,
|
|
40
|
+
hex.slice(20, 32)
|
|
41
|
+
].join('-');
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* One folder path, however it was spelled.
|
|
45
|
+
*
|
|
46
|
+
* The adapter joins a root of '/' to a path of '/app' and sends '//app', which is
|
|
47
|
+
* a different string and - because an id is derived from the path - a different
|
|
48
|
+
* id. A client that then asks for that id is asking about a folder the drive does
|
|
49
|
+
* not have, so every path is collapsed to one spelling before anything derives
|
|
50
|
+
* from it.
|
|
51
|
+
*/
|
|
52
|
+
export const normalisePath = (path) => {
|
|
53
|
+
const collapsed = `/${path}`.replace(/\/{2,}/g, '/');
|
|
54
|
+
return collapsed.length > 1 ? collapsed.replace(/\/+$/, '') : collapsed;
|
|
55
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"overlay.d.ts","sourceRoot":"","sources":["../../../src/viya/overlay.ts"],"names":[],"mappings":"AAeA,MAAM,WAAW,gBAAgB;IAC/B,WAAW,EAAE,OAAO,CAAA;IACpB,6DAA6D;IAC7D,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;
|
|
1
|
+
{"version":3,"file":"overlay.d.ts","sourceRoot":"","sources":["../../../src/viya/overlay.ts"],"names":[],"mappings":"AAeA,MAAM,WAAW,gBAAgB;IAC/B,WAAW,EAAE,OAAO,CAAA;IACpB,6DAA6D;IAC7D,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAYD,eAAO,MAAM,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,CA8BzD,CAAA;AAED;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,oBAAoB,GAC/B,SAAS,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,EACzC,aAAY,KAAK,CAAC;IAAE,EAAE,EAAE,MAAM,CAAA;CAAE,CAAc,KAC7C,IAcF,CAAA"}
|
|
@@ -12,14 +12,6 @@
|
|
|
12
12
|
* that reaches it learns why rather than that something is unimplemented.
|
|
13
13
|
*/
|
|
14
14
|
import { VIYA_TABLE } from './table.js';
|
|
15
|
-
/**
|
|
16
|
-
* The reason the session and file families carry.
|
|
17
|
-
*
|
|
18
|
-
* They need the session port and the file content routes, which this slice does
|
|
19
|
-
* not mount. Saying it once is better than saying it thirteen times in slightly
|
|
20
|
-
* different words.
|
|
21
|
-
*/
|
|
22
|
-
const NEEDS_RUNTIME = 'The emulated estate answers folders, identities and types today. This operation needs the session or file port, which the host mounting the emulator has not supplied.';
|
|
23
15
|
/**
|
|
24
16
|
* The token endpoint is the host's.
|
|
25
17
|
*
|
|
@@ -38,38 +30,21 @@ export const VIYA_OVERLAY = {
|
|
|
38
30
|
'GET /folders/folders/@myRecycleBin': { implemented: true },
|
|
39
31
|
'GET /identities/users/@currentUser': { implemented: true },
|
|
40
32
|
'GET /types/types': { implemented: true },
|
|
41
|
-
//
|
|
42
|
-
'GET /compute/contexts': { implemented:
|
|
43
|
-
'POST /compute/contexts/{}/sessions': {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
},
|
|
47
|
-
'
|
|
48
|
-
'
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
},
|
|
52
|
-
'GET /
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
},
|
|
56
|
-
'GET /compute/sessions/{}/jobs/{}/log/content': {
|
|
57
|
-
implemented: false,
|
|
58
|
-
reason: NEEDS_RUNTIME
|
|
59
|
-
},
|
|
60
|
-
'GET /compute/sessions/{}/jobs/{}/state': {
|
|
61
|
-
implemented: false,
|
|
62
|
-
reason: NEEDS_RUNTIME
|
|
63
|
-
},
|
|
64
|
-
'GET /compute/sessions/{}/state': {
|
|
65
|
-
implemented: false,
|
|
66
|
-
reason: NEEDS_RUNTIME
|
|
67
|
-
},
|
|
68
|
-
'POST /files/files': { implemented: false, reason: NEEDS_RUNTIME },
|
|
69
|
-
'GET /files/files/{}': { implemented: false, reason: NEEDS_RUNTIME },
|
|
70
|
-
'PATCH /files/files/{}': { implemented: false, reason: NEEDS_RUNTIME },
|
|
71
|
-
'GET /files/files/{}/content': { implemented: false, reason: NEEDS_RUNTIME },
|
|
72
|
-
'PUT /files/files/{}/content': { implemented: false, reason: NEEDS_RUNTIME },
|
|
33
|
+
// answered here: compute
|
|
34
|
+
'GET /compute/contexts': { implemented: true },
|
|
35
|
+
'POST /compute/contexts/{}/sessions': { implemented: true },
|
|
36
|
+
'DELETE /compute/sessions/{}': { implemented: true },
|
|
37
|
+
'POST /compute/sessions/{}/jobs': { implemented: true },
|
|
38
|
+
'GET /compute/sessions/{}/jobs/{}': { implemented: true },
|
|
39
|
+
'GET /compute/sessions/{}/jobs/{}/log/content': { implemented: true },
|
|
40
|
+
'GET /compute/sessions/{}/jobs/{}/state': { implemented: true },
|
|
41
|
+
'GET /compute/sessions/{}/state': { implemented: true },
|
|
42
|
+
// answered here: files
|
|
43
|
+
'POST /files/files': { implemented: true },
|
|
44
|
+
'GET /files/files/{}': { implemented: true },
|
|
45
|
+
'PATCH /files/files/{}': { implemented: true },
|
|
46
|
+
'GET /files/files/{}/content': { implemented: true },
|
|
47
|
+
'PUT /files/files/{}/content': { implemented: true },
|
|
73
48
|
// declined: the host owns the token port
|
|
74
49
|
'POST /SASLogon/oauth/token': { implemented: false, reason: NEEDS_AUTH }
|
|
75
50
|
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ports the emulated handlers read, in one place.
|
|
3
|
+
*
|
|
4
|
+
* `drive` is required on `Services`, so there is no absent case to answer: a host
|
|
5
|
+
* that mounts the emulator has one, and a `501` for a missing drive would be a
|
|
6
|
+
* branch nothing can reach. It lives here rather than in a handler module because
|
|
7
|
+
* two of them read it, and importing one handler map from the other is a cycle.
|
|
8
|
+
*/
|
|
9
|
+
import type { Services } from '../host/dispatch.js';
|
|
10
|
+
import type { DriveService } from '../drive/service.js';
|
|
11
|
+
export declare const requireDrive: (services: Services) => DriveService;
|
|
12
|
+
//# sourceMappingURL=ports.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ports.d.ts","sourceRoot":"","sources":["../../../src/viya/ports.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAA;AACnD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AAEvD,eAAO,MAAM,YAAY,GAAI,UAAU,QAAQ,KAAG,YAA8B,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const requireDrive = (services) => services.drive;
|