@sasjs/api 0.16.0 → 0.17.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/README.md +3 -2
  2. package/build/browser.js +668 -87
  3. package/build/cjs/corpus/runner.js +19 -2
  4. package/build/cjs/corpus/runtime.js +238 -0
  5. package/build/cjs/corpus/session.js +1 -1
  6. package/build/cjs/host/dispatch.js +115 -1
  7. package/build/cjs/index.js +9 -1
  8. package/build/cjs/routes/generated.js +40 -0
  9. package/build/cjs/routes/overlay.js +18 -6
  10. package/build/cjs/runtime/memory-runner.js +59 -0
  11. package/build/cjs/runtime/service.js +303 -0
  12. package/build/cjs/session/memory-host.js +22 -5
  13. package/build/esm/corpus/drive.d.ts +13 -0
  14. package/build/esm/corpus/drive.d.ts.map +1 -1
  15. package/build/esm/corpus/runner.d.ts.map +1 -1
  16. package/build/esm/corpus/runner.js +19 -2
  17. package/build/esm/corpus/runtime.d.ts +17 -0
  18. package/build/esm/corpus/runtime.d.ts.map +1 -0
  19. package/build/esm/corpus/runtime.js +235 -0
  20. package/build/esm/corpus/session.js +1 -1
  21. package/build/esm/host/dispatch.d.ts +15 -0
  22. package/build/esm/host/dispatch.d.ts.map +1 -1
  23. package/build/esm/host/dispatch.js +115 -1
  24. package/build/esm/index.d.ts +6 -1
  25. package/build/esm/index.d.ts.map +1 -1
  26. package/build/esm/index.js +3 -0
  27. package/build/esm/routes/generated.d.ts.map +1 -1
  28. package/build/esm/routes/generated.js +40 -0
  29. package/build/esm/routes/overlay.d.ts.map +1 -1
  30. package/build/esm/routes/overlay.js +18 -6
  31. package/build/esm/runtime/memory-runner.d.ts +38 -0
  32. package/build/esm/runtime/memory-runner.d.ts.map +1 -0
  33. package/build/esm/runtime/memory-runner.js +55 -0
  34. package/build/esm/runtime/service.d.ts +185 -0
  35. package/build/esm/runtime/service.d.ts.map +1 -0
  36. package/build/esm/runtime/service.js +298 -0
  37. package/build/esm/session/memory-host.d.ts +13 -2
  38. package/build/esm/session/memory-host.d.ts.map +1 -1
  39. package/build/esm/session/memory-host.js +22 -5
  40. package/build/esm/session/service.d.ts +12 -1
  41. package/build/esm/session/service.d.ts.map +1 -1
  42. package/package.json +1 -1
@@ -8,7 +8,8 @@ const registry_js_1 = require("../drive/registry.js");
8
8
  const service_js_2 = require("../identity/service.js");
9
9
  const records_js_1 = require("../identity/records.js");
10
10
  const service_js_3 = require("../session/service.js");
11
- const memory_host_js_1 = require("../session/memory-host.js");
11
+ const service_js_4 = require("../runtime/service.js");
12
+ const memory_runner_js_1 = require("../runtime/memory-runner.js");
12
13
  const memory_store_js_2 = require("../identity/memory-store.js");
13
14
  const fixture_js_1 = require("../identity/fixture.js");
14
15
  const types_js_1 = require("../types.js");
@@ -24,6 +25,14 @@ const servicesFor = (testCase) => {
24
25
  // has to be one the flow can sign in. Two stores built from the same fixture
25
26
  // would let a case assert across two independent states and still pass.
26
27
  const identity = new memory_store_js_2.MemoryIdentityStore(testCase.identity || fixture_js_1.DEFAULT_IDENTITY);
28
+ // one runner, shared by the runtime routes and the session routes: a session a
29
+ // trigger creates has to be one the session routes can read
30
+ const runner = new memory_runner_js_1.MemorySessionRunner({
31
+ fail: testCase.failingRun,
32
+ outputs: testCase.runOutput
33
+ ? { [programOf(testCase)]: testCase.runOutput }
34
+ : undefined
35
+ });
27
36
  return {
28
37
  drive: (0, service_js_1.createDriveService)(testCase.unwritable
29
38
  ? new RefusingDriveStore(testCase.seed || {})
@@ -31,10 +40,18 @@ const servicesFor = (testCase) => {
31
40
  identity: (0, service_js_2.createIdentityService)(identity),
32
41
  records: (0, records_js_1.createRecordService)(identity),
33
42
  registry: new registry_js_1.MemoryAppStreamRegistry(),
34
- session: (0, service_js_3.createSessionService)(identity, new memory_host_js_1.MemorySessionHost())
43
+ session: (0, service_js_3.createSessionService)(identity, runner),
44
+ // no `runTimes` option: the default is the five the document names, so a
45
+ // case is never refused for a reason that belongs to one host's install
46
+ runtime: (0, service_js_4.createRuntimeService)(identity, runner)
35
47
  };
36
48
  };
37
49
  exports.servicesFor = servicesFor;
50
+ /** The program a case runs, keyed the way the runner is keyed by it. */
51
+ const programOf = (testCase) => String(testCase.request.query?._program ??
52
+ testCase.request.body?._program ??
53
+ testCase.request.body?.code ??
54
+ '');
38
55
  /**
39
56
  * A drive that reads but refuses every write.
40
57
  *
@@ -0,0 +1,238 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RUNTIME_CORPUS = void 0;
4
+ const ADMIN = { userId: 'user-admin', clientId: 'web_app' };
5
+ const PROGRAM = '/Public/apps/demo/run.sas';
6
+ const CODE = 'data _null_;\n put "hello";\nrun;';
7
+ const UNAUTHORIZED = 'Unauthorized';
8
+ const FAILED = 'Job execution failed.';
9
+ /** Bytes without an encoding API, as the drive corpus has them. */
10
+ const bytes = (...values) => Uint8Array.from(values);
11
+ exports.RUNTIME_CORPUS = [
12
+ // ------------------------------------------------- GET /stp/execute
13
+ {
14
+ id: 'stp-execute-get-answers-output',
15
+ route: 'GET /SASjsApi/stp/execute',
16
+ description: 'answers the output the program produced',
17
+ request: { subject: ADMIN, query: { _program: PROGRAM } },
18
+ runOutput: { contents: 'hello from the program' },
19
+ expect: { status: 200, contents: 'hello from the program' }
20
+ },
21
+ {
22
+ id: 'stp-execute-get-answers-bytes',
23
+ route: 'GET /SASjsApi/stp/execute',
24
+ description: 'answers an output that is bytes as bytes',
25
+ request: { subject: ADMIN, query: { _program: PROGRAM } },
26
+ runOutput: { contents: bytes(1, 2, 3) },
27
+ expect: { status: 200, contents: bytes(1, 2, 3) }
28
+ },
29
+ {
30
+ id: 'stp-execute-get-missing-program',
31
+ route: 'GET /SASjsApi/stp/execute',
32
+ description: 'refuses a request that names no program',
33
+ request: { subject: ADMIN, query: {} },
34
+ expect: { status: 400, error: '"_program" is required' }
35
+ },
36
+ {
37
+ id: 'stp-execute-get-failed-run',
38
+ route: 'GET /SASjsApi/stp/execute',
39
+ description: 'answers 400 when the program could not be run',
40
+ request: { subject: ADMIN, query: { _program: PROGRAM } },
41
+ failingRun: true,
42
+ expect: { status: 400, error: FAILED }
43
+ },
44
+ {
45
+ id: 'stp-execute-get-unauthenticated',
46
+ route: 'GET /SASjsApi/stp/execute',
47
+ description: 'refuses a caller with no token',
48
+ request: { query: { _program: PROGRAM } },
49
+ expect: { status: 401, error: UNAUTHORIZED }
50
+ },
51
+ // ------------------------------------------------ POST /stp/execute
52
+ {
53
+ id: 'stp-execute-post-body-program',
54
+ route: 'POST /SASjsApi/stp/execute',
55
+ description: 'runs the program the body names',
56
+ request: { subject: ADMIN, body: { _program: PROGRAM } },
57
+ runOutput: { contents: 'hello from the body' },
58
+ expect: { status: 200, contents: 'hello from the body' }
59
+ },
60
+ {
61
+ id: 'stp-execute-post-query-program',
62
+ route: 'POST /SASjsApi/stp/execute',
63
+ description: 'runs the program the query names, with no body',
64
+ request: { subject: ADMIN, query: { _program: PROGRAM }, body: {} },
65
+ runOutput: { contents: 'hello from the query' },
66
+ expect: { status: 200, contents: 'hello from the query' }
67
+ },
68
+ {
69
+ id: 'stp-execute-post-no-program',
70
+ route: 'POST /SASjsApi/stp/execute',
71
+ description: 'refuses a request with no program in the query or the body',
72
+ request: { subject: ADMIN, body: {} },
73
+ expect: { status: 400, error: '"_program" is required' }
74
+ },
75
+ {
76
+ id: 'stp-execute-post-failed-run',
77
+ route: 'POST /SASjsApi/stp/execute',
78
+ description: 'answers 400 when the program could not be run',
79
+ request: { subject: ADMIN, body: { _program: PROGRAM } },
80
+ failingRun: true,
81
+ expect: { status: 400, error: FAILED }
82
+ },
83
+ {
84
+ id: 'stp-execute-post-unauthenticated',
85
+ route: 'POST /SASjsApi/stp/execute',
86
+ description: 'refuses a caller with no token, before it reads the payload',
87
+ request: { body: {} },
88
+ expect: { status: 401, error: UNAUTHORIZED }
89
+ },
90
+ // ------------------------------------------------ POST /stp/trigger
91
+ {
92
+ id: 'stp-trigger-answers-session',
93
+ route: 'POST /SASjsApi/stp/trigger',
94
+ description: 'answers the session a caller polls, and no pid from a mock',
95
+ request: { subject: ADMIN, query: { _program: PROGRAM } },
96
+ expect: { status: 200, keys: ['sessionId'] }
97
+ },
98
+ {
99
+ id: 'stp-trigger-retention',
100
+ route: 'POST /SASjsApi/stp/trigger',
101
+ description: 'accepts a retention in minutes, as the query carries it',
102
+ request: {
103
+ subject: ADMIN,
104
+ query: { _program: PROGRAM, expiresAfterMins: '15' }
105
+ },
106
+ expect: { status: 200, keys: ['sessionId'] }
107
+ },
108
+ {
109
+ id: 'stp-trigger-retention-not-positive',
110
+ route: 'POST /SASjsApi/stp/trigger',
111
+ description: 'refuses a retention that is not a positive number of minutes',
112
+ request: {
113
+ subject: ADMIN,
114
+ query: { _program: PROGRAM, expiresAfterMins: '0' }
115
+ },
116
+ expect: { status: 400, error: '"expiresAfterMins" must be greater than 0' }
117
+ },
118
+ {
119
+ id: 'stp-trigger-missing-program',
120
+ route: 'POST /SASjsApi/stp/trigger',
121
+ description: 'refuses a request that names no program',
122
+ request: { subject: ADMIN, query: {} },
123
+ expect: { status: 400, error: '"_program" is required' }
124
+ },
125
+ {
126
+ id: 'stp-trigger-failed-run',
127
+ route: 'POST /SASjsApi/stp/trigger',
128
+ description: 'answers 400 when the program could not be started',
129
+ request: { subject: ADMIN, query: { _program: PROGRAM } },
130
+ failingRun: true,
131
+ expect: { status: 400, error: FAILED }
132
+ },
133
+ {
134
+ id: 'stp-trigger-unauthenticated',
135
+ route: 'POST /SASjsApi/stp/trigger',
136
+ description: 'refuses a caller with no token',
137
+ request: { query: { _program: PROGRAM } },
138
+ expect: { status: 401, error: UNAUTHORIZED }
139
+ },
140
+ // ----------------------------------------------- POST /code/execute
141
+ {
142
+ id: 'code-execute-answers-output',
143
+ route: 'POST /SASjsApi/code/execute',
144
+ description: 'runs the code in the body and answers its output',
145
+ request: { subject: ADMIN, body: { code: CODE, runTime: 'sas' } },
146
+ runOutput: { contents: 'NOTE: mock run' },
147
+ expect: { status: 200, contents: 'NOTE: mock run' }
148
+ },
149
+ {
150
+ // The refusal's *message* is not pinned here: it names the run times the
151
+ // host holds, which is deployment data - `r` is a valid request where R is
152
+ // configured and a 400 where it is not - so a corpus that stated a list
153
+ // would be asserting one host's install. `spec/runtime.spec.ts` pins the
154
+ // wording against an explicit list; this states the status every host sends
155
+ // for a name no host holds.
156
+ id: 'code-execute-unknown-runtime',
157
+ route: 'POST /SASjsApi/code/execute',
158
+ description: 'refuses a run time the document does not name',
159
+ request: { subject: ADMIN, body: { code: CODE, runTime: 'cobol' } },
160
+ expect: { status: 400 }
161
+ },
162
+ {
163
+ id: 'code-execute-missing-code',
164
+ route: 'POST /SASjsApi/code/execute',
165
+ description: 'refuses a payload with no code',
166
+ request: { subject: ADMIN, body: { runTime: 'sas' } },
167
+ expect: { status: 400, error: '"code" is required' }
168
+ },
169
+ {
170
+ id: 'code-execute-empty-code',
171
+ route: 'POST /SASjsApi/code/execute',
172
+ description: 'refuses code that is present but empty',
173
+ request: { subject: ADMIN, body: { code: '', runTime: 'sas' } },
174
+ expect: { status: 400, error: '"code" is not allowed to be empty' }
175
+ },
176
+ {
177
+ id: 'code-execute-non-string-code',
178
+ route: 'POST /SASjsApi/code/execute',
179
+ description: 'refuses code that is not a string rather than running its text',
180
+ request: { subject: ADMIN, body: { code: 123, runTime: 'sas' } },
181
+ expect: { status: 400, error: '"code" must be a string' }
182
+ },
183
+ {
184
+ id: 'code-execute-failed-run',
185
+ route: 'POST /SASjsApi/code/execute',
186
+ description: 'answers 400 when the code could not be run',
187
+ request: { subject: ADMIN, body: { code: CODE, runTime: 'sas' } },
188
+ failingRun: true,
189
+ expect: { status: 400, error: FAILED }
190
+ },
191
+ {
192
+ id: 'code-execute-unauthenticated',
193
+ route: 'POST /SASjsApi/code/execute',
194
+ description: 'refuses a caller with no token, before it reads the payload',
195
+ request: { body: {} },
196
+ expect: { status: 401, error: UNAUTHORIZED }
197
+ },
198
+ // ----------------------------------------------- POST /code/trigger
199
+ {
200
+ id: 'code-trigger-answers-session',
201
+ route: 'POST /SASjsApi/code/trigger',
202
+ description: 'answers the session a caller polls, and no pid from a mock',
203
+ request: { subject: ADMIN, body: { code: CODE, runTime: 'sas' } },
204
+ expect: { status: 200, keys: ['sessionId'] }
205
+ },
206
+ {
207
+ id: 'code-trigger-missing-runtime',
208
+ route: 'POST /SASjsApi/code/trigger',
209
+ description: 'refuses a payload with no run time, which the document requires',
210
+ request: { subject: ADMIN, body: { code: CODE } },
211
+ expect: { status: 400, error: '"runTime" is required' }
212
+ },
213
+ {
214
+ id: 'code-trigger-retention-not-positive',
215
+ route: 'POST /SASjsApi/code/trigger',
216
+ description: 'refuses a retention that is not a positive number of minutes',
217
+ request: {
218
+ subject: ADMIN,
219
+ body: { code: CODE, runTime: 'sas', expiresAfterMins: 0 }
220
+ },
221
+ expect: { status: 400, error: '"expiresAfterMins" must be greater than 0' }
222
+ },
223
+ {
224
+ id: 'code-trigger-failed-run',
225
+ route: 'POST /SASjsApi/code/trigger',
226
+ description: 'answers 400 when the code could not be started',
227
+ request: { subject: ADMIN, body: { code: CODE, runTime: 'sas' } },
228
+ failingRun: true,
229
+ expect: { status: 400, error: FAILED }
230
+ },
231
+ {
232
+ id: 'code-trigger-unauthenticated',
233
+ route: 'POST /SASjsApi/code/trigger',
234
+ description: 'refuses a caller with no token',
235
+ request: { body: { code: CODE, runTime: 'sas' } },
236
+ expect: { status: 401, error: UNAUTHORIZED }
237
+ }
238
+ ];
@@ -20,7 +20,7 @@ exports.SESSION_CORPUS = [
20
20
  pid: 4242,
21
21
  creationTimeStamp: '2026-11-07T09:00:00.000Z',
22
22
  deathTimeStamp: '2026-11-07T09:00:04.000Z',
23
- expiresAfterMins: 60
23
+ expiresAfterMins: { mins: 60, used: false }
24
24
  }
25
25
  }
26
26
  },
@@ -7,6 +7,22 @@ const types_js_1 = require("../types.js");
7
7
  const deploy_js_1 = require("../drive/deploy.js");
8
8
  /** The defaults sasjs/server's Client schema applies to an omitted lifetime. */
9
9
  const SECONDS_IN_A_DAY = 86400;
10
+ /**
11
+ * The routes that perform work, and so find their caller before they read the
12
+ * payload.
13
+ *
14
+ * sasjs/server mounts `authenticateAccessToken` ahead of the router that owns
15
+ * these, so an anonymous request with an invalid program is a `401` there and
16
+ * would be a `400` here if the schema ran first. The same ordering is observable
17
+ * on the record routes, which is why both are resolved in one place.
18
+ */
19
+ const RUN_ROUTES = [
20
+ 'GET /SASjsApi/stp/execute',
21
+ 'POST /SASjsApi/stp/execute',
22
+ 'POST /SASjsApi/stp/trigger',
23
+ 'POST /SASjsApi/code/execute',
24
+ 'POST /SASjsApi/code/trigger'
25
+ ];
10
26
  /**
11
27
  * The parameters the table marks required, checked before the handler runs.
12
28
  *
@@ -57,7 +73,11 @@ const dispatch = async (services, routeId, request = {}) => {
57
73
  // flow has no caller by definition, and the drive has no identity at all.
58
74
  const identityScoped = ['User', 'Group', 'Permission', 'Client'].includes(route.tag) ||
59
75
  routeId === 'PATCH /SASjsApi/auth/updatePassword';
60
- if (identityScoped && records) {
76
+ // A host that answers the run routes must have a runner; without one it is
77
+ // told which capability it is missing rather than sent round a login loop, so
78
+ // the gate is tested on the service that serves the route.
79
+ const runtimeScoped = RUN_ROUTES.includes(routeId);
80
+ if ((identityScoped && records) || (runtimeScoped && services.runtime)) {
61
81
  const subject = request.subject ??
62
82
  (identity ? await identity.authenticate(request.token) : undefined);
63
83
  if (!subject) {
@@ -81,6 +101,39 @@ const dispatch = async (services, routeId, request = {}) => {
81
101
  throw new types_js_1.ApiError(501, `This host has no session service, so it cannot answer ${routeId}.`);
82
102
  return services.session;
83
103
  };
104
+ /** The runtime service, or the 501 a host without a runner answers. */
105
+ const runtimeRequired = () => {
106
+ if (!services.runtime)
107
+ throw new types_js_1.ApiError(501, `This host has no runtime service, so it cannot answer ${routeId}.`);
108
+ return services.runtime;
109
+ };
110
+ /**
111
+ * The files a multipart request carried, by field name.
112
+ *
113
+ * A host with one file passes `file`; one with several passes `files`, because
114
+ * the field names are the caller's and a program reads them as session
115
+ * variables.
116
+ */
117
+ const uploaded = () => {
118
+ const files = request.files || (request.file ? [request.file] : []);
119
+ if (!files.length)
120
+ return undefined;
121
+ return files.reduce((byName, file) => {
122
+ byName[file.name] = file.contents;
123
+ return byName;
124
+ }, {});
125
+ };
126
+ /**
127
+ * A program's output travels as the response body, bytes or not.
128
+ *
129
+ * The same shape `GET /session/{sessionId}/output` answers with, and for the
130
+ * same reason: the body is the artefact, not a value the API composed, so it
131
+ * is handed on as it came rather than wrapped in a JSON string.
132
+ */
133
+ const artifact = (output) => ({
134
+ status: 200,
135
+ contents: output.contents
136
+ });
84
137
  const required = () => {
85
138
  if (!records)
86
139
  throw new types_js_1.ApiError(501, `This host has no record service, so it cannot answer ${routeId}.`);
@@ -190,6 +243,62 @@ const dispatch = async (services, routeId, request = {}) => {
190
243
  };
191
244
  case 'GET /SASjsApi/drive/filetree':
192
245
  return { status: 200, body: await drive.tree(query._folderPath) };
246
+ // ------------------------------------------------------------- run times
247
+ //
248
+ // A stored program is named by a drive path and a code buffer travels in the
249
+ // body; either way the package decides everything about the request and the
250
+ // host's runner does the work.
251
+ case 'GET /SASjsApi/stp/execute': {
252
+ const output = await runtimeRequired().execute(await caller(), {
253
+ source: 'program',
254
+ program: query._program,
255
+ vars: { ...query },
256
+ files: uploaded()
257
+ });
258
+ return artifact(output);
259
+ }
260
+ case 'POST /SASjsApi/stp/execute': {
261
+ // the query wins over the body, which is the order the server resolves
262
+ // them in: an empty query value is absent in every sense that matters
263
+ const output = await runtimeRequired().execute(await caller(), {
264
+ source: 'program',
265
+ program: query._program || body._program,
266
+ vars: { ...query, ...body },
267
+ files: uploaded()
268
+ });
269
+ return artifact(output);
270
+ }
271
+ case 'POST /SASjsApi/stp/trigger':
272
+ return {
273
+ status: 200,
274
+ body: await runtimeRequired().trigger(await caller(), {
275
+ source: 'program',
276
+ program: query._program,
277
+ vars: { ...query },
278
+ expiresAfterMins: query.expiresAfterMins
279
+ })
280
+ };
281
+ case 'POST /SASjsApi/code/execute': {
282
+ const output = await runtimeRequired().execute(await caller(), {
283
+ source: 'code',
284
+ code: body.code,
285
+ runTime: body.runTime,
286
+ vars: { ...query, ...body },
287
+ files: uploaded()
288
+ });
289
+ return artifact(output);
290
+ }
291
+ case 'POST /SASjsApi/code/trigger':
292
+ return {
293
+ status: 200,
294
+ body: await runtimeRequired().trigger(await caller(), {
295
+ source: 'code',
296
+ code: body.code,
297
+ runTime: body.runTime,
298
+ vars: { ...query, ...body },
299
+ expiresAfterMins: body.expiresAfterMins
300
+ })
301
+ };
193
302
  // ------------------------------------------------------------- sessions
194
303
  case 'GET /SASjsApi/session/{sessionId}': {
195
304
  return {
@@ -403,6 +512,11 @@ exports.dispatch = dispatch;
403
512
  exports.HANDLED_ROUTES = [
404
513
  'POST /SASjsApi/drive/deploy/upload',
405
514
  'DELETE /SASjsApi/drive/deploy',
515
+ 'GET /SASjsApi/stp/execute',
516
+ 'POST /SASjsApi/stp/execute',
517
+ 'POST /SASjsApi/stp/trigger',
518
+ 'POST /SASjsApi/code/execute',
519
+ 'POST /SASjsApi/code/trigger',
406
520
  'GET /SASjsApi/session/{sessionId}',
407
521
  'GET /SASjsApi/session/{sessionId}/log',
408
522
  'GET /SASjsApi/session/{sessionId}/output',
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.servicesFor = exports.runCase = exports.PROVIDER_MANAGED = exports.ADMIN_SIGNED_IN = exports.RECORD_CORPUS = exports.unpublishAppStream = exports.MemoryAppStreamRegistry = exports.MemorySessionHost = exports.DEFAULT_SESSIONS = exports.createSessionService = exports.SESSION_CORPUS = exports.DEPLOY_CORPUS = exports.IDENTITY_CORPUS = exports.DRIVE_CORPUS = exports.parseIdentity = exports.IDENTITY_FORMAT = exports.DEFAULT_IDENTITY = exports.MemoryIdentityStore = exports.CODE_CORPUS = exports.sanitiseDisplayName = exports.createRecordService = exports.createIdentityService = exports.HANDLED_ROUTES = exports.dispatch = exports.TAG_CAPABILITIES = exports.OVERLAY = exports.findRoute = exports.routeById = exports.ROUTES = exports.MemoryDriveStore = exports.measureDeployTree = exports.isDeployTree = exports.deployTree = exports.decodeBase64 = exports.checkDeployTree = exports.DEPLOY_SUCCESS = exports.DEPLOY_PAYLOAD_BYTES = exports.DEPLOY_LIMITS = exports.createDriveService = exports.isRoot = exports.basename = exports.dirname = exports.hasBlockedExtension = exports.normaliseDrivePath = void 0;
17
+ exports.servicesFor = exports.runCase = exports.PROVIDER_MANAGED = exports.ADMIN_SIGNED_IN = exports.RECORD_CORPUS = exports.unpublishAppStream = exports.MemoryAppStreamRegistry = exports.RUNTIME_CORPUS = exports.MemorySessionRunner = exports.createRuntimeService = exports.DOCUMENTED_RUN_TIMES = exports.CODE_DEBUG = exports.MemorySessionHost = exports.DEFAULT_SESSIONS = exports.createSessionService = exports.SESSION_CORPUS = exports.DEPLOY_CORPUS = exports.IDENTITY_CORPUS = exports.DRIVE_CORPUS = exports.parseIdentity = exports.IDENTITY_FORMAT = exports.DEFAULT_IDENTITY = exports.MemoryIdentityStore = exports.CODE_CORPUS = exports.sanitiseDisplayName = exports.createRecordService = exports.createIdentityService = exports.HANDLED_ROUTES = exports.dispatch = exports.TAG_CAPABILITIES = exports.OVERLAY = exports.findRoute = exports.routeById = exports.ROUTES = exports.MemoryDriveStore = exports.measureDeployTree = exports.isDeployTree = exports.deployTree = exports.decodeBase64 = exports.checkDeployTree = exports.DEPLOY_SUCCESS = exports.DEPLOY_PAYLOAD_BYTES = exports.DEPLOY_LIMITS = exports.createDriveService = exports.isRoot = exports.basename = exports.dirname = exports.hasBlockedExtension = exports.normaliseDrivePath = void 0;
18
18
  /**
19
19
  * `@sasjs/api` - the SASjs Server API described once.
20
20
  *
@@ -88,6 +88,14 @@ Object.defineProperty(exports, "createSessionService", { enumerable: true, get:
88
88
  var memory_host_js_1 = require("./session/memory-host.js");
89
89
  Object.defineProperty(exports, "DEFAULT_SESSIONS", { enumerable: true, get: function () { return memory_host_js_1.DEFAULT_SESSIONS; } });
90
90
  Object.defineProperty(exports, "MemorySessionHost", { enumerable: true, get: function () { return memory_host_js_1.MemorySessionHost; } });
91
+ var service_js_4 = require("./runtime/service.js");
92
+ Object.defineProperty(exports, "CODE_DEBUG", { enumerable: true, get: function () { return service_js_4.CODE_DEBUG; } });
93
+ Object.defineProperty(exports, "DOCUMENTED_RUN_TIMES", { enumerable: true, get: function () { return service_js_4.DOCUMENTED_RUN_TIMES; } });
94
+ Object.defineProperty(exports, "createRuntimeService", { enumerable: true, get: function () { return service_js_4.createRuntimeService; } });
95
+ var memory_runner_js_1 = require("./runtime/memory-runner.js");
96
+ Object.defineProperty(exports, "MemorySessionRunner", { enumerable: true, get: function () { return memory_runner_js_1.MemorySessionRunner; } });
97
+ var runtime_js_1 = require("./corpus/runtime.js");
98
+ Object.defineProperty(exports, "RUNTIME_CORPUS", { enumerable: true, get: function () { return runtime_js_1.RUNTIME_CORPUS; } });
91
99
  var registry_js_1 = require("./drive/registry.js");
92
100
  Object.defineProperty(exports, "MemoryAppStreamRegistry", { enumerable: true, get: function () { return registry_js_1.MemoryAppStreamRegistry; } });
93
101
  Object.defineProperty(exports, "unpublishAppStream", { enumerable: true, get: function () { return registry_js_1.unpublishAppStream; } });
@@ -319,6 +319,14 @@ exports.GENERATED_ROUTES = [
319
319
  {
320
320
  status: 200,
321
321
  description: 'Ok'
322
+ },
323
+ {
324
+ status: 400,
325
+ description: 'Invalid request: the payload is invalid, or the program could not be run'
326
+ },
327
+ {
328
+ status: 401,
329
+ description: 'Unauthorized'
322
330
  }
323
331
  ]
324
332
  },
@@ -425,6 +433,14 @@ exports.GENERATED_ROUTES = [
425
433
  {
426
434
  status: 200,
427
435
  description: 'Ok'
436
+ },
437
+ {
438
+ status: 400,
439
+ description: 'Invalid request: the payload is invalid, or the program could not be run'
440
+ },
441
+ {
442
+ status: 401,
443
+ description: 'Unauthorized'
428
444
  }
429
445
  ]
430
446
  },
@@ -1431,6 +1447,14 @@ exports.GENERATED_ROUTES = [
1431
1447
  {
1432
1448
  status: 200,
1433
1449
  description: 'Ok'
1450
+ },
1451
+ {
1452
+ status: 400,
1453
+ description: 'Invalid request: the query parameters are invalid, or the program could not be run'
1454
+ },
1455
+ {
1456
+ status: 401,
1457
+ description: 'Unauthorized'
1434
1458
  }
1435
1459
  ]
1436
1460
  },
@@ -1459,6 +1483,14 @@ exports.GENERATED_ROUTES = [
1459
1483
  {
1460
1484
  status: 200,
1461
1485
  description: 'Ok'
1486
+ },
1487
+ {
1488
+ status: 400,
1489
+ description: 'Invalid request: the payload is invalid, or the program could not be run'
1490
+ },
1491
+ {
1492
+ status: 401,
1493
+ description: 'Unauthorized'
1462
1494
  }
1463
1495
  ]
1464
1496
  },
@@ -1493,6 +1525,14 @@ exports.GENERATED_ROUTES = [
1493
1525
  {
1494
1526
  status: 200,
1495
1527
  description: 'Ok'
1528
+ },
1529
+ {
1530
+ status: 400,
1531
+ description: 'Invalid request: the payload is invalid, or the program could not be run'
1532
+ },
1533
+ {
1534
+ status: 401,
1535
+ description: 'Unauthorized'
1496
1536
  }
1497
1537
  ]
1498
1538
  },
@@ -26,7 +26,6 @@ exports.TAG_CAPABILITIES = {
26
26
  * thing thirty times.
27
27
  */
28
28
  const DIRECTORY = 'Synchronising with an external auth provider needs a directory port, which this package does not have: a host decides which directory it holds accounts in, and answers this itself.';
29
- const RUNTIME = 'Starting a program needs a runtime port, which this package does not have: a host decides which languages it holds, and answers this itself until then.';
30
29
  const HOST_CONFIG = "The body is the host's own configuration - ports, run times and providers - which a mock states for itself and this package cannot know.";
31
30
  const HOST_OWNED = "The host owns this report: it describes the host's own process and files, which a browser build has no equivalent of.";
32
31
  const WEB_BUNDLE = 'The host serves its own web bundle; there is no API decision here for this package to answer.';
@@ -90,8 +89,18 @@ exports.OVERLAY = {
90
89
  'GET /SASjsApi/client': { implemented: true },
91
90
  'POST /SASjsApi/client': { implemented: true },
92
91
  // Code - the editor's three calls, and the runtime one
93
- 'POST /SASjsApi/code/execute': { implemented: false, reason: RUNTIME },
94
- 'POST /SASjsApi/code/trigger': { implemented: false, reason: RUNTIME },
92
+ //
93
+ // The runtime routes are carried over a `SessionRunner` port: the document's
94
+ // `RunTimeType` is validated here, the id, the record and the refusals are
95
+ // decided here, and performing the work is the host's. The response body is
96
+ // the program's own output - the log included when `_debug` asks for it - so
97
+ // there is no body to annotate.
98
+ 'POST /SASjsApi/code/execute': { implemented: true },
99
+ 'POST /SASjsApi/code/trigger': {
100
+ implemented: true,
101
+ // `pid` is the host's, and a run time without a local process omits it
102
+ responses: { 200: { sessionId: '' } }
103
+ },
95
104
  'POST /SASjsApi/code/lint': {
96
105
  implemented: true,
97
106
  capabilities: ['identity']
@@ -204,9 +213,12 @@ exports.OVERLAY = {
204
213
  'GET /SASjsApi/session/{sessionId}/output': { implemented: true },
205
214
  'GET /SASjsApi/session/{sessionId}/state': { implemented: true },
206
215
  // STP - a stored program, on the runtime port
207
- 'GET /SASjsApi/stp/execute': { implemented: false, reason: RUNTIME },
208
- 'POST /SASjsApi/stp/execute': { implemented: false, reason: RUNTIME },
209
- 'POST /SASjsApi/stp/trigger': { implemented: false, reason: RUNTIME },
216
+ 'GET /SASjsApi/stp/execute': { implemented: true },
217
+ 'POST /SASjsApi/stp/execute': { implemented: true },
218
+ 'POST /SASjsApi/stp/trigger': {
219
+ implemented: true,
220
+ responses: { 200: { sessionId: '' } }
221
+ },
210
222
  // User - the accounts the identity store already holds for sign-in
211
223
  // Reading accounts is open to any authenticated caller, which is what
212
224
  // sasjs/server's routes do (`authenticateAccessToken` only): the document once
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MemorySessionRunner = void 0;
4
+ /**
5
+ * A runner that needs no process, and the sessions it creates.
6
+ *
7
+ * The corpus and a browser build have the same problem: nothing to hand a
8
+ * program to. A memory runner answers it the way a mock should - it holds the
9
+ * record, the state, the log and the output of a session, and it finishes
10
+ * synchronously - which is why "the same API" can be checked without a SAS
11
+ * binary on the box. The one thing it cannot supply is a `pid`, and every
12
+ * response that would carry one carries it as optional.
13
+ *
14
+ * It is also a `SessionHost`, because a session a trigger created has to be one
15
+ * the session routes can read: two maps behind two ports would let a trigger
16
+ * write a record nothing can poll, which is the class of mistake the package
17
+ * exists to end.
18
+ */
19
+ const memory_host_js_1 = require("../session/memory-host.js");
20
+ class MemorySessionRunner extends memory_host_js_1.MemorySessionHost {
21
+ constructor(options = {}) {
22
+ super();
23
+ this.options = options;
24
+ }
25
+ async run(request) {
26
+ if (this.options.fail)
27
+ throw new Error('the mock run failed');
28
+ const key = 'code' in request.program ? request.program.code : request.program.path;
29
+ const output = this.options.outputs?.[key] || { contents: '' };
30
+ const pid = this.options.pid;
31
+ // a session is recorded before the work is reported done, because the id in
32
+ // the trigger's response is what a client polls with
33
+ if (request.session) {
34
+ const { sessionId, owner, expiresAfterMins } = request.session;
35
+ const started = new Date().toISOString();
36
+ const summary = {
37
+ sessionId,
38
+ // the work is finished by the time this returns: a memory runner has
39
+ // nothing to wait for, and the session routes report what it did
40
+ state: 'completed',
41
+ creationTimeStamp: started,
42
+ deathTimeStamp: started
43
+ };
44
+ if (pid !== undefined)
45
+ summary.pid = pid;
46
+ if (expiresAfterMins !== undefined)
47
+ // the record answers `{mins, used}`, not the number the caller sent: an
48
+ // interactive client reads this straight off the summary route
49
+ summary.expiresAfterMins = { mins: expiresAfterMins, used: false };
50
+ this.sessions[sessionId] = { owner, summary };
51
+ this.states[sessionId] =
52
+ pid === undefined ? { state: 'completed' } : { state: 'completed', pid };
53
+ this.logs[sessionId] = '';
54
+ this.outputs[sessionId] = output;
55
+ }
56
+ return pid === undefined ? { output } : { output, pid };
57
+ }
58
+ }
59
+ exports.MemorySessionRunner = MemorySessionRunner;