@sasjs/api 0.49.0 → 0.50.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/README.md +11 -0
  2. package/build/browser.js +407 -47
  3. package/build/cjs/appstream/identity.js +2 -31
  4. package/build/cjs/uuid.js +30 -0
  5. package/build/cjs/viya/appSurface.js +241 -0
  6. package/build/cjs/viya/compute.js +71 -20
  7. package/build/cjs/viya/declared.js +96 -0
  8. package/build/cjs/viya/dispatch.js +10 -0
  9. package/build/cjs/viya/identity.js +191 -0
  10. package/build/cjs/viya/overlay.js +10 -0
  11. package/build/cjs/viya/seed.js +34 -8
  12. package/build/cjs/viya/service.js +3 -1
  13. package/build/cjs/viya/sessions.js +24 -10
  14. package/build/esm/appstream/identity.d.ts +1 -9
  15. package/build/esm/appstream/identity.d.ts.map +1 -1
  16. package/build/esm/appstream/identity.js +1 -30
  17. package/build/esm/identity/types.d.ts +2 -0
  18. package/build/esm/identity/types.d.ts.map +1 -1
  19. package/build/esm/uuid.d.ts +6 -0
  20. package/build/esm/uuid.d.ts.map +1 -0
  21. package/build/esm/uuid.js +26 -0
  22. package/build/esm/viya/appSurface.d.ts +51 -0
  23. package/build/esm/viya/appSurface.d.ts.map +1 -0
  24. package/build/esm/viya/appSurface.js +238 -0
  25. package/build/esm/viya/compute.d.ts.map +1 -1
  26. package/build/esm/viya/compute.js +73 -22
  27. package/build/esm/viya/declared.d.ts.map +1 -1
  28. package/build/esm/viya/declared.js +96 -0
  29. package/build/esm/viya/dispatch.d.ts +3 -0
  30. package/build/esm/viya/dispatch.d.ts.map +1 -1
  31. package/build/esm/viya/dispatch.js +11 -1
  32. package/build/esm/viya/identity.d.ts +34 -0
  33. package/build/esm/viya/identity.d.ts.map +1 -0
  34. package/build/esm/viya/identity.js +187 -0
  35. package/build/esm/viya/overlay.d.ts.map +1 -1
  36. package/build/esm/viya/overlay.js +10 -0
  37. package/build/esm/viya/seed.d.ts +29 -7
  38. package/build/esm/viya/seed.d.ts.map +1 -1
  39. package/build/esm/viya/seed.js +32 -7
  40. package/build/esm/viya/service.d.ts.map +1 -1
  41. package/build/esm/viya/service.js +3 -1
  42. package/build/esm/viya/sessions.d.ts +24 -6
  43. package/build/esm/viya/sessions.d.ts.map +1 -1
  44. package/build/esm/viya/sessions.js +24 -10
  45. package/package.json +3 -1
package/README.md CHANGED
@@ -26,6 +26,17 @@ Where the document is wrong, it is corrected in `sasjs/server` and re-vendored h
26
26
  `createAdminService` carries the three Admin reports over the host's `AdminReports`, owning the diagnostics shape, the log-name rule and the `lines` bounds; `createDirectoryService` imports a directory over a five-method `DirectoryStore`, creating what is missing and leaving what exists; `createHomeService` stamps the interface's shell with a token derived from its bundle, so a cached page cannot pin an old build.
27
27
  They are `async`, take a `DriveStore`, and import nothing - so the same code runs under Express and inside a service worker.
28
28
 
29
+ **The emulated Viya estate's scope** is two measurements, not one.
30
+ `src/viya/surface.ts` classifies every path `@sasjs/adapter` was measured to build (`spec/viya/adapter-paths.json`, re-derived by `npm run check:scope` from the pinned adapter).
31
+ `src/viya/appSurface.ts` does the same for an application's own calls to the platform (`spec/viya/app-paths.json`, re-derived by `npm run check:app-scope -- <checkout>`), because an application can speak Viya REST directly where the adapter has no method: Data Controller's group, user and deploy screens do, and a scope measured from the adapter alone left those paths answering the emulator's 404.
32
+ Both lists are held against their inventory in both directions by `spec/viyaAppScope.spec.ts` and `spec/viyaTable.spec.ts`, and an in-scope path with no operation answering it fails the app spec - which is the failure the adapter-only scope could not see.
33
+ `check:app-scope` is not a CI step: it needs a checkout of the application, so it runs where that checkout exists and states so when it is not given one.
34
+ The operations an application's calls add are declared rather than captured (`src/viya/declared.ts`), since no recording pass driven by the adapter can observe them.
35
+
36
+ **Viya compute ownership.** A multi-user host must authenticate its caller and pass the resolved `subject` to `dispatchViya` on every compute session request. Set `subject.ownerId` to the immutable account ID from the host's identity store, never from request data. `subject.userId` still controls the username on the wire. Hosts that omit `ownerId` use `userId` as before, in a separate namespace; switching an existing host to stable IDs requires recreating any live username-scoped sessions. Ownership spans client IDs, and another owner gets the same 404 as for a missing session. Ownerless records are refused. A token without a resolved subject is refused; no subject and no token selects a shared anonymous browser mock, not an authentication boundary. Session stores are keyed by the full normalised `serverUrl`, not the 32-bit seed.
37
+
38
+ New emulated session IDs are opaque UUID v4 strings (Web Crypto is required). Job IDs still derive from their session ID and therefore change with it; clients should use the returned IDs or links rather than parse their format. This does not change externally issued Viya IDs or the emulator's seeded folder IDs and generated logs.
39
+
29
40
  **The app stream** (`createAppStreamService`, `createAppStreamWebService`, `splitAppBundle`) is what a deployed folder becomes when it is an app.
30
41
  An app is one folder, the web assets inside it (`<appLoc>/services/<streamWebFolder>`) and a UUID assigned the first time it is published; the readable name is a label with no uniqueness rule, because nothing is looked up by name - the URL is `/app/<id>/`, so two apps may be called the same thing without colliding.
31
42
  Publishing a folder that is already published is the same app published again: it keeps its id, which is what makes a redeploy keep its URL.
package/build/browser.js CHANGED
@@ -1,4 +1,4 @@
1
- /* @sasjs/api 0.49.0 - the SASjs Server API, for a service worker. https://github.com/sasjs/api */
1
+ /* @sasjs/api 0.50.0 - the SASjs Server API, for a service worker. https://github.com/sasjs/api */
2
2
  "use strict";
3
3
  var SASjsApi = (() => {
4
4
  var __defProp = Object.defineProperty;
@@ -128,7 +128,7 @@ var SASjsApi = (() => {
128
128
  isRouteGranted: () => isRouteGranted,
129
129
  logLines: () => logLines,
130
130
  measureDeployTree: () => measureDeployTree,
131
- newAppId: () => newAppId,
131
+ newAppId: () => newUuidV4,
132
132
  normaliseAppLoc: () => normaliseAppLoc,
133
133
  normaliseAuthConfig: () => normaliseAuthConfig,
134
134
  normaliseDiagnostics: () => normaliseDiagnostics,
@@ -522,18 +522,8 @@ var SASjsApi = (() => {
522
522
  };
523
523
  };
524
524
 
525
- // src/appstream/identity.ts
526
- var normaliseAppLoc = (appLoc) => {
527
- if (typeof appLoc !== "string" || appLoc === "")
528
- throw new Error(
529
- `App location must be a non-empty string, got ${JSON.stringify(appLoc)}.`
530
- );
531
- const normalised = normaliseDrivePath(appLoc);
532
- if (normalised === null)
533
- throw new ApiError(400, "appLoc cannot be outside drive.");
534
- return normalised;
535
- };
536
- var newAppId = () => {
525
+ // src/uuid.ts
526
+ var newUuidV4 = () => {
537
527
  const webCrypto = globalThis.crypto;
538
528
  if (typeof webCrypto?.randomUUID === "function") return webCrypto.randomUUID();
539
529
  if (typeof webCrypto?.getRandomValues !== "function")
@@ -551,6 +541,18 @@ var SASjsApi = (() => {
551
541
  hex.slice(20)
552
542
  ].join("-");
553
543
  };
544
+
545
+ // src/appstream/identity.ts
546
+ var normaliseAppLoc = (appLoc) => {
547
+ if (typeof appLoc !== "string" || appLoc === "")
548
+ throw new Error(
549
+ `App location must be a non-empty string, got ${JSON.stringify(appLoc)}.`
550
+ );
551
+ const normalised = normaliseDrivePath(appLoc);
552
+ if (normalised === null)
553
+ throw new ApiError(400, "appLoc cannot be outside drive.");
554
+ return normalised;
555
+ };
554
556
  var APP_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
555
557
  var isAppId = (value) => typeof value === "string" && APP_ID.test(value);
556
558
 
@@ -750,7 +752,7 @@ var SASjsApi = (() => {
750
752
  );
751
753
  }
752
754
  const entry = {
753
- id: existing ? existing.id : newAppId(),
755
+ id: existing ? existing.id : newUuidV4(),
754
756
  // the label is the one field a redeploy inherits: everything else is
755
757
  // what this publish states
756
758
  name,
@@ -5418,6 +5420,102 @@ var SASjsApi = (() => {
5418
5420
  // there is no JSON shape to record
5419
5421
  shape: void 0,
5420
5422
  declared: "The adapter fetches this only when a run expects a webout (`expectWebout`), and the recording pass submits a `%put` and reads the log instead. Without it an application cannot receive the data its service returns, so the emulator answers it from the job it just ran."
5423
+ },
5424
+ // The application's own identity reads. No capture describes these at all: the
5425
+ // recorder drives the client the adapter is, and Data Controller's group, user
5426
+ // and deploy screens call the identity service from the browser instead. That
5427
+ // is why they are declared rather than recorded, and why the scope they belong
5428
+ // to is stated in `src/viya/appSurface.ts`.
5429
+ {
5430
+ id: "GET /identities/groups",
5431
+ method: "GET",
5432
+ path: "/identities/groups",
5433
+ steps: [],
5434
+ statuses: [200],
5435
+ queries: ["sortBy", "limit", "start"],
5436
+ request: { queries: ["sortBy", "limit", "start"], body: void 0 },
5437
+ // a collection of groups, whose own shape Viya publishes rather than an
5438
+ // estate recording supplying it
5439
+ shape: void 0,
5440
+ declared: "The application fetches the group list twice on its deploy screen - for the admin-group dropdown and, filtered, for the caller own groups - and aborts the wizard when that read fails. The adapter never calls it, so no recording pass reaches it."
5441
+ },
5442
+ {
5443
+ id: "GET /identities/groups/{}/members",
5444
+ method: "GET",
5445
+ path: "/identities/groups/{}/members",
5446
+ steps: [],
5447
+ statuses: [200, 404],
5448
+ queries: ["limit"],
5449
+ request: { queries: ["limit"], body: void 0 },
5450
+ shape: void 0,
5451
+ declared: "The group screen lists the accounts that hold a group, following the members link the group list advertises. The adapter has no identity route beyond the caller, so a capture driven by it cannot reach this one."
5452
+ },
5453
+ {
5454
+ id: "GET /identities/users",
5455
+ method: "GET",
5456
+ path: "/identities/users",
5457
+ steps: [],
5458
+ statuses: [200],
5459
+ queries: ["limit", "start"],
5460
+ request: { queries: ["limit", "start"], body: void 0 },
5461
+ shape: void 0,
5462
+ declared: "The user screen lists the estate accounts. The adapter does not offer an account list and never requests one, so the recording pass has no way to observe it."
5463
+ },
5464
+ {
5465
+ id: "GET /identities/users/{}",
5466
+ method: "GET",
5467
+ path: "/identities/users/{}",
5468
+ steps: [],
5469
+ statuses: [200, 404],
5470
+ queries: [],
5471
+ request: { queries: [], body: void 0 },
5472
+ shape: void 0,
5473
+ declared: "The user screen reads one account from the id the list returned, for the profile above the memberships. The adapter asks only about the caller, which is a different operation with its own row."
5474
+ },
5475
+ {
5476
+ id: "GET /identities/users/@currentUser/memberships",
5477
+ method: "GET",
5478
+ path: "/identities/users/@currentUser/memberships",
5479
+ steps: [],
5480
+ statuses: [200],
5481
+ queries: ["limit", "start"],
5482
+ request: { queries: ["limit", "start"], body: void 0 },
5483
+ shape: void 0,
5484
+ declared: "The deploy screen filters the admin-group list to the groups the caller belongs to, and shows every group when this read fails. The adapter reads the caller itself but never the caller memberships."
5485
+ },
5486
+ {
5487
+ id: "GET /identities/users/{}/memberships",
5488
+ method: "GET",
5489
+ path: "/identities/users/{}/memberships",
5490
+ steps: [],
5491
+ statuses: [200, 401],
5492
+ queries: ["limit", "start"],
5493
+ request: { queries: ["limit", "start"], body: void 0 },
5494
+ shape: void 0,
5495
+ declared: "The user screen lists the groups any account holds, addressed by the id the list returned. It is the same answer as the caller own memberships for one id, and the adapter asks for neither."
5496
+ },
5497
+ {
5498
+ id: "GET /identities/users/{}/avatar/content",
5499
+ method: "GET",
5500
+ path: "/identities/users/{}/avatar/content",
5501
+ steps: [],
5502
+ statuses: [404],
5503
+ queries: [],
5504
+ request: { queries: [], body: void 0 },
5505
+ // the body is an image when an estate holds one: there is no JSON shape
5506
+ shape: void 0,
5507
+ declared: "The header renders the caller avatar from this URL, and an estate whose identity carries no avatar answers 404 so the client falls back to its own image. The adapter has no avatar read."
5508
+ },
5509
+ {
5510
+ id: "GET /compute/contexts/{}",
5511
+ method: "GET",
5512
+ path: "/compute/contexts/{}",
5513
+ steps: [],
5514
+ statuses: [200, 404],
5515
+ queries: [],
5516
+ request: { queries: [], body: void 0 },
5517
+ shape: void 0,
5518
+ declared: "The deploy screen reads each context in full, because the list view carries neither the batch identity nor the reuse setting it filters on. The capture recorded the list only, so the read it follows with is stated here."
5421
5519
  }
5422
5520
  ];
5423
5521
  var VIYA_OPERATIONS = [...VIYA_TABLE, ...DECLARED];
@@ -5434,9 +5532,19 @@ var SASjsApi = (() => {
5434
5532
  "GET /folders/folders/@item": { implemented: true },
5435
5533
  "GET /folders/folders/@myRecycleBin": { implemented: true },
5436
5534
  "GET /identities/users/@currentUser": { implemented: true },
5535
+ // answered here: the application's own identity reads, declared rather than
5536
+ // captured - see src/viya/appSurface.ts for the scope they belong to
5537
+ "GET /identities/groups": { implemented: true },
5538
+ "GET /identities/groups/{}/members": { implemented: true },
5539
+ "GET /identities/users": { implemented: true },
5540
+ "GET /identities/users/{}": { implemented: true },
5541
+ "GET /identities/users/@currentUser/memberships": { implemented: true },
5542
+ "GET /identities/users/{}/memberships": { implemented: true },
5543
+ "GET /identities/users/{}/avatar/content": { implemented: true },
5437
5544
  "GET /types/types": { implemented: true },
5438
5545
  // answered here: compute
5439
5546
  "GET /compute/contexts": { implemented: true },
5547
+ "GET /compute/contexts/{}": { implemented: true },
5440
5548
  "POST /compute/contexts/{}/sessions": { implemented: true },
5441
5549
  "DELETE /compute/sessions/{}": { implemented: true },
5442
5550
  "POST /compute/sessions/{}/jobs": { implemented: true },
@@ -5541,6 +5649,10 @@ var SASjsApi = (() => {
5541
5649
  const address = normaliseServerUrl(settings.serverUrl);
5542
5650
  return hash(`url:${address || DEFAULT_SEED}`);
5543
5651
  };
5652
+ var estateKeyOf = (settings = {}) => {
5653
+ const address = normaliseServerUrl(settings.serverUrl);
5654
+ return address || DEFAULT_SEED;
5655
+ };
5544
5656
  var generator = (seed) => {
5545
5657
  let state = seed >>> 0;
5546
5658
  return () => {
@@ -5576,12 +5688,14 @@ var SASjsApi = (() => {
5576
5688
  constructor(seed) {
5577
5689
  this.seed = seed;
5578
5690
  this.sessions = /* @__PURE__ */ new Map();
5579
- this.created = 0;
5580
5691
  }
5581
- /** A new session: deterministic id, no jobs. */
5582
- create() {
5692
+ /** A new session: opaque UUID v4, no jobs. */
5693
+ create(owner) {
5694
+ const id = newUuidV4();
5695
+ if (this.sessions.has(id)) throw new Error("Duplicate compute session ID.");
5583
5696
  const session = {
5584
- id: `emulated-${this.seed.toString(16)}-ses${String(this.created++).padStart(4, "0")}`,
5697
+ id,
5698
+ owner,
5585
5699
  createdAt: (/* @__PURE__ */ new Date()).toISOString(),
5586
5700
  jobs: /* @__PURE__ */ new Map(),
5587
5701
  submitted: 0
@@ -5619,11 +5733,11 @@ var SASjsApi = (() => {
5619
5733
  }
5620
5734
  };
5621
5735
  var estates = /* @__PURE__ */ new Map();
5622
- var estateFor = (seed) => {
5623
- const existing = estates.get(seed);
5736
+ var estateFor = (key, seed) => {
5737
+ const existing = estates.get(key);
5624
5738
  if (existing) return existing;
5625
5739
  const estate = new EmulatedEstate(seed);
5626
- estates.set(seed, estate);
5740
+ estates.set(key, estate);
5627
5741
  return estate;
5628
5742
  };
5629
5743
 
@@ -5659,10 +5773,10 @@ var SASjsApi = (() => {
5659
5773
  var DEFAULT_CONTEXT_NAME = "SAS Job Execution compute context";
5660
5774
  var EMULATED_CONTEXT_ID = idFor("emulated-context", "context");
5661
5775
  var relLinks = (type, rels) => rels.map(([rel, href]) => ({ href, uri: href, rel, method: "GET", type }));
5662
- var linksFor = (uri, type, collection = false, itemType = false) => [
5776
+ var linksFor = (uri, type, collection2 = false, itemType = false) => [
5663
5777
  // the collection link comes first, because that is the order the capture
5664
5778
  // recorded and the item shape assertion walks the first element
5665
- ...collection ? [
5779
+ ...collection2 ? [
5666
5780
  {
5667
5781
  href: `${uri}/members`,
5668
5782
  uri: `${uri}/members`,
@@ -5683,7 +5797,7 @@ var SASjsApi = (() => {
5683
5797
  ...itemType ? { itemType: type } : {}
5684
5798
  }
5685
5799
  ];
5686
- var estateOf = (services) => estateFor(seedOf(services.viya));
5800
+ var estateOf = (services) => estateFor(estateKeyOf(services.viya), seedOf(services.viya));
5687
5801
  var contextNameFrom = (filter) => {
5688
5802
  if (!filter) return DEFAULT_CONTEXT_NAME;
5689
5803
  const match = /name\s*,\s*'([^']*)'|name\s*,\s*"([^"]*)"/i.exec(filter);
@@ -5699,6 +5813,21 @@ var SASjsApi = (() => {
5699
5813
  "application/vnd.sas.compute.context"
5700
5814
  )
5701
5815
  });
5816
+ var contextDetailsBody = (caller) => ({
5817
+ ...contextBody(DEFAULT_CONTEXT_NAME, caller),
5818
+ description: "The compute context an emulated estate answers for",
5819
+ creationTimeStamp: EMULATED_NOW,
5820
+ modifiedTimeStamp: EMULATED_NOW,
5821
+ modifiedBy: caller,
5822
+ launchType: "service",
5823
+ launchContext: { contextId: EMULATED_CONTEXT_ID },
5824
+ environment: { autoExecLines: [] },
5825
+ attributes: {
5826
+ reuseServerProcesses: "false",
5827
+ allowXCMD: "false",
5828
+ serverMinAvailable: "1"
5829
+ }
5830
+ });
5702
5831
  var sessionBody = (services, session, autoExecLines, caller) => {
5703
5832
  const { id } = session;
5704
5833
  const uri = `/compute/sessions/${id}`;
@@ -5760,14 +5889,25 @@ var SASjsApi = (() => {
5760
5889
  ])
5761
5890
  };
5762
5891
  };
5763
- var sessionOf = (services, id) => {
5892
+ var ownerOf = (subject) => {
5893
+ if (subject === void 0) return { kind: "anonymous" };
5894
+ if (!subject || typeof subject.userId !== "string" || !subject.userId.trim())
5895
+ throw new ApiError(401, "A valid caller is required.");
5896
+ if (subject.ownerId === void 0)
5897
+ return { kind: "user", key: subject.userId };
5898
+ if (typeof subject.ownerId !== "string" || !subject.ownerId.trim())
5899
+ throw new ApiError(401, "A valid caller is required.");
5900
+ return { kind: "account", key: subject.ownerId };
5901
+ };
5902
+ var sessionOf = (services, id, subject) => {
5903
+ const owner = ownerOf(subject);
5764
5904
  const session = estateOf(services).session(id);
5765
- if (!session)
5905
+ if (!session || !session.owner || session.owner.kind !== owner.kind || session.owner.key !== owner.key)
5766
5906
  throw new ApiError(404, `Session with ID '${id}' was not found.`);
5767
5907
  return session;
5768
5908
  };
5769
- var jobOf = (services, sessionId, jobId) => {
5770
- const job = sessionOf(services, sessionId).jobs.get(jobId);
5909
+ var jobOf = (services, sessionId, jobId, subject) => {
5910
+ const job = sessionOf(services, sessionId, subject).jobs.get(jobId);
5771
5911
  if (!job) throw new ApiError(404, `Job with ID '${jobId}' was not found.`);
5772
5912
  return job;
5773
5913
  };
@@ -5798,6 +5938,14 @@ var SASjsApi = (() => {
5798
5938
  }
5799
5939
  };
5800
5940
  },
5941
+ "GET /compute/contexts/{}": async ({ request, params }) => {
5942
+ if (params.id !== EMULATED_CONTEXT_ID)
5943
+ throw new ApiError(
5944
+ 404,
5945
+ `No compute context matches the id ${params.id}. The emulated estate answers one context, and this is not its id.`
5946
+ );
5947
+ return { status: 200, body: contextDetailsBody(callerId(request.subject)) };
5948
+ },
5801
5949
  "POST /compute/contexts/{}/sessions": async ({
5802
5950
  services,
5803
5951
  params,
@@ -5806,7 +5954,7 @@ var SASjsApi = (() => {
5806
5954
  if (params.id !== EMULATED_CONTEXT_ID)
5807
5955
  throw new ApiError(404, `Context with ID '${params.id}' was not found.`);
5808
5956
  const body = request.body ?? {};
5809
- const session = estateOf(services).create();
5957
+ const session = estateOf(services).create(ownerOf(request.subject));
5810
5958
  return {
5811
5959
  status: 201,
5812
5960
  body: sessionBody(
@@ -5817,17 +5965,17 @@ var SASjsApi = (() => {
5817
5965
  )
5818
5966
  };
5819
5967
  },
5820
- "GET /compute/sessions/{}/state": async ({ services, params }) => {
5821
- sessionOf(services, params.id);
5968
+ "GET /compute/sessions/{}/state": async ({ services, params, request }) => {
5969
+ sessionOf(services, params.id, request.subject);
5822
5970
  return { status: 200, body: "idle" };
5823
5971
  },
5824
- "DELETE /compute/sessions/{}": async ({ services, params }) => {
5825
- sessionOf(services, params.id);
5972
+ "DELETE /compute/sessions/{}": async ({ services, params, request }) => {
5973
+ sessionOf(services, params.id, request.subject);
5826
5974
  estateOf(services).remove(params.id);
5827
5975
  return { status: 204, body: void 0 };
5828
5976
  },
5829
5977
  "POST /compute/sessions/{}/jobs": async ({ services, params, request }) => {
5830
- const session = sessionOf(services, params.id);
5978
+ const session = sessionOf(services, params.id, request.subject);
5831
5979
  const body = request.body ?? {};
5832
5980
  const job = estateOf(services).submit(session, body);
5833
5981
  const execute = services.viya?.execute;
@@ -5857,9 +6005,10 @@ var SASjsApi = (() => {
5857
6005
  },
5858
6006
  "GET /compute/sessions/{}/filerefs/_webout/content": async ({
5859
6007
  services,
5860
- params
6008
+ params,
6009
+ request
5861
6010
  }) => {
5862
- const session = sessionOf(services, params.id);
6011
+ const session = sessionOf(services, params.id, request.subject);
5863
6012
  const last = [...session.jobs.values()].pop();
5864
6013
  if (!last)
5865
6014
  throw new ApiError(
@@ -5877,15 +6026,22 @@ var SASjsApi = (() => {
5877
6026
  headers: { "content-type": "text/plain" }
5878
6027
  };
5879
6028
  },
5880
- "GET /compute/sessions/{}/jobs/{}": async ({ services, params }) => {
6029
+ "GET /compute/sessions/{}/jobs/{}": async ({ services, params, request }) => {
5881
6030
  const [sessionId, jobId] = segmentsOf(params);
5882
- return { status: 200, body: jobBody(jobOf(services, sessionId, jobId)) };
6031
+ return {
6032
+ status: 200,
6033
+ body: jobBody(jobOf(services, sessionId, jobId, request.subject))
6034
+ };
5883
6035
  },
5884
- "GET /compute/sessions/{}/jobs/{}/state": async ({ services, params }) => {
6036
+ "GET /compute/sessions/{}/jobs/{}/state": async ({
6037
+ services,
6038
+ params,
6039
+ request
6040
+ }) => {
5885
6041
  const [sessionId, jobId] = segmentsOf(params);
5886
6042
  return {
5887
6043
  status: 200,
5888
- body: jobOf(services, sessionId, jobId).state
6044
+ body: jobOf(services, sessionId, jobId, request.subject).state
5889
6045
  };
5890
6046
  },
5891
6047
  "GET /compute/sessions/{}/jobs/{}/log/content": async ({
@@ -5894,7 +6050,7 @@ var SASjsApi = (() => {
5894
6050
  request
5895
6051
  }) => {
5896
6052
  const [sessionId, jobId] = segmentsOf(params);
5897
- const job = jobOf(services, sessionId, jobId);
6053
+ const job = jobOf(services, sessionId, jobId, request.subject);
5898
6054
  const start = Number(request.query?.start ?? 0) || 0;
5899
6055
  const limit = Number(request.query?.limit ?? 0) || 0;
5900
6056
  const lines = job.log.slice(start, limit ? start + limit : void 0);
@@ -6092,8 +6248,209 @@ var SASjsApi = (() => {
6092
6248
  }
6093
6249
  };
6094
6250
 
6251
+ // src/viya/identity.ts
6252
+ var requireRecords = (services) => {
6253
+ if (!services.records)
6254
+ throw new ApiError(
6255
+ 501,
6256
+ "The emulated identity answers from the records service, which this host does not supply."
6257
+ );
6258
+ return services.records;
6259
+ };
6260
+ var callerOf = async (services, request) => request.subject ?? await services.identity?.authenticate(request.token);
6261
+ var userIdFrom = (segment, caller) => {
6262
+ if (segment.toLowerCase() !== "@currentuser") return segment;
6263
+ const id = caller?.userId;
6264
+ if (!id) throw new ApiError(401, "No caller is signed in.");
6265
+ return id;
6266
+ };
6267
+ var collection = (name, items, limit, uri, type) => {
6268
+ const page = limit > 0 ? items.slice(0, limit) : items;
6269
+ return {
6270
+ accept: "application/json",
6271
+ name,
6272
+ start: 0,
6273
+ // the value asked for is echoed, as the folder answers echo it: a caller
6274
+ // that asked for five and is told zero cannot tell what happened
6275
+ limit: limit || page.length,
6276
+ count: page.length,
6277
+ version: 2,
6278
+ items: page,
6279
+ links: [
6280
+ {
6281
+ href: uri,
6282
+ uri,
6283
+ rel: "self",
6284
+ method: "GET",
6285
+ type: "application/vnd.sas.collection",
6286
+ itemType: type
6287
+ }
6288
+ ]
6289
+ };
6290
+ };
6291
+ var identityLinks = (uri, type) => [
6292
+ { href: uri, uri, rel: "self", method: "GET", type }
6293
+ ];
6294
+ var GROUP_TYPE = "application/vnd.sas.identity.group";
6295
+ var USER_TYPE = "application/vnd.sas.identity.user";
6296
+ var groupBody = (group, memberCount) => ({
6297
+ id: group.uid,
6298
+ name: group.name,
6299
+ description: group.description,
6300
+ providerId: "sas",
6301
+ state: "active",
6302
+ memberCount,
6303
+ creationTimeStamp: EMULATED_NOW,
6304
+ modifiedTimeStamp: EMULATED_NOW,
6305
+ version: 1,
6306
+ links: identityLinks(`/identities/groups/${group.uid}`, GROUP_TYPE)
6307
+ });
6308
+ var userBody = (user3) => ({
6309
+ id: user3.uid,
6310
+ // Viya's `name` is the identifier a person signs in with; the display name is
6311
+ // a separate field, and this is the one a member list shows
6312
+ name: user3.username,
6313
+ displayName: user3.displayName,
6314
+ providerId: "sas",
6315
+ state: "active",
6316
+ version: 1,
6317
+ links: identityLinks(`/identities/users/${user3.uid}`, USER_TYPE)
6318
+ });
6319
+ var membershipsOf = async (records, caller, uid) => {
6320
+ const users = await records.listUsers(caller);
6321
+ const user3 = users.find((item) => item.uid === uid) ?? users.find((item) => item.username === uid);
6322
+ const resolvedUid = user3?.uid ?? uid;
6323
+ const groups = await records.listGroups(caller);
6324
+ const found = [];
6325
+ for (const group of groups) {
6326
+ const details = await records.getGroup(caller, { uid: group.uid });
6327
+ if (details.users.some((member) => member.uid === resolvedUid))
6328
+ found.push(groupBody(group, details.users.length));
6329
+ }
6330
+ return found;
6331
+ };
6332
+ var IDENTITY_HANDLERS = {
6333
+ "GET /identities/groups": async ({ services, request }) => {
6334
+ const records = requireRecords(services);
6335
+ const caller = await callerOf(services, request);
6336
+ const limit = Number(request.query?.limit ?? 0) || 0;
6337
+ const sortBy = request.query?.sortBy;
6338
+ const groups = await records.listGroups(caller);
6339
+ const items = [];
6340
+ for (const group of groups) {
6341
+ const details = await records.getGroup(caller, { uid: group.uid });
6342
+ items.push(groupBody(group, details.users.length));
6343
+ }
6344
+ const ordered = sortBy && sortBy.toLowerCase() === "name" ? [...items].sort((left, right) => left.name.localeCompare(right.name)) : items;
6345
+ return {
6346
+ status: 200,
6347
+ body: collection(
6348
+ "groups",
6349
+ ordered,
6350
+ limit,
6351
+ "/identities/groups",
6352
+ GROUP_TYPE
6353
+ )
6354
+ };
6355
+ },
6356
+ "GET /identities/groups/{}/members": async ({
6357
+ services,
6358
+ request,
6359
+ params
6360
+ }) => {
6361
+ const records = requireRecords(services);
6362
+ const caller = await callerOf(services, request);
6363
+ const limit = Number(request.query?.limit ?? 0) || 0;
6364
+ const details = await records.getGroup(caller, { uid: params.id });
6365
+ const items = details.users.map((user3) => userBody(user3));
6366
+ return {
6367
+ status: 200,
6368
+ body: collection(
6369
+ "members",
6370
+ items,
6371
+ limit,
6372
+ `/identities/groups/${details.uid}/members`,
6373
+ USER_TYPE
6374
+ )
6375
+ };
6376
+ },
6377
+ "GET /identities/users": async ({ services, request }) => {
6378
+ const records = requireRecords(services);
6379
+ const caller = await callerOf(services, request);
6380
+ const limit = Number(request.query?.limit ?? 0) || 0;
6381
+ const users = await records.listUsers(caller);
6382
+ return {
6383
+ status: 200,
6384
+ body: collection(
6385
+ "users",
6386
+ users.map((user3) => userBody(user3)),
6387
+ limit,
6388
+ "/identities/users",
6389
+ USER_TYPE
6390
+ )
6391
+ };
6392
+ },
6393
+ "GET /identities/users/{}": async ({ services, request, params }) => {
6394
+ const records = requireRecords(services);
6395
+ const caller = await callerOf(services, request);
6396
+ const uid = userIdFrom(params.id, caller);
6397
+ const user3 = await records.getUser(caller, { uid });
6398
+ return { status: 200, body: userBody(user3) };
6399
+ },
6400
+ "GET /identities/users/@currentUser/memberships": async ({
6401
+ services,
6402
+ request
6403
+ }) => {
6404
+ const records = requireRecords(services);
6405
+ const caller = await callerOf(services, request);
6406
+ const limit = Number(request.query?.limit ?? 0) || 0;
6407
+ const uid = userIdFrom("@currentUser", caller);
6408
+ const items = await membershipsOf(records, caller, uid);
6409
+ return {
6410
+ status: 200,
6411
+ body: collection(
6412
+ "memberships",
6413
+ items,
6414
+ limit,
6415
+ "/identities/users/@currentUser/memberships",
6416
+ GROUP_TYPE
6417
+ )
6418
+ };
6419
+ },
6420
+ "GET /identities/users/{}/memberships": async ({
6421
+ services,
6422
+ request,
6423
+ params
6424
+ }) => {
6425
+ const records = requireRecords(services);
6426
+ const caller = await callerOf(services, request);
6427
+ const limit = Number(request.query?.limit ?? 0) || 0;
6428
+ const uid = userIdFrom(params.id, caller);
6429
+ const items = await membershipsOf(records, caller, uid);
6430
+ return {
6431
+ status: 200,
6432
+ body: collection(
6433
+ "memberships",
6434
+ items,
6435
+ limit,
6436
+ `/identities/users/${params.id}/memberships`,
6437
+ GROUP_TYPE
6438
+ )
6439
+ };
6440
+ },
6441
+ "GET /identities/users/{}/avatar/content": async ({
6442
+ services,
6443
+ request,
6444
+ params
6445
+ }) => {
6446
+ const caller = await callerOf(services, request);
6447
+ const uid = userIdFrom(params.id, caller);
6448
+ throw new ApiError(404, `No avatar is recorded for the user ${uid}.`);
6449
+ }
6450
+ };
6451
+
6095
6452
  // src/viya/service.ts
6096
- var linksFor2 = (uri, collection = false) => {
6453
+ var linksFor2 = (uri, collection2 = false) => {
6097
6454
  const self = {
6098
6455
  href: uri,
6099
6456
  uri,
@@ -6109,7 +6466,7 @@ var SASjsApi = (() => {
6109
6466
  type: "application/vnd.sas.collection",
6110
6467
  itemType: "folder"
6111
6468
  };
6112
- return collection ? [members, self] : [self, members];
6469
+ return collection2 ? [members, self] : [self, members];
6113
6470
  };
6114
6471
  var folderBody = async (drive, path, caller) => {
6115
6472
  const listing = await drive.list(path);
@@ -6393,7 +6750,8 @@ var SASjsApi = (() => {
6393
6750
  };
6394
6751
  },
6395
6752
  ...COMPUTE_HANDLERS,
6396
- ...FILE_HANDLERS
6753
+ ...FILE_HANDLERS,
6754
+ ...IDENTITY_HANDLERS
6397
6755
  };
6398
6756
 
6399
6757
  // src/viya/dispatch.ts
@@ -6473,6 +6831,8 @@ var SASjsApi = (() => {
6473
6831
  }
6474
6832
  };
6475
6833
  try {
6834
+ if (request.subject === void 0 && request.token !== void 0 && (found.operation.id === "POST /compute/contexts/{}/sessions" || found.operation.path.startsWith("/compute/sessions/")))
6835
+ throw new ApiError(401, "A valid caller is required.");
6476
6836
  const answer = await handler({
6477
6837
  services,
6478
6838
  request,
@@ -12081,7 +12441,7 @@ var SASjsApi = (() => {
12081
12441
  };
12082
12442
  var createRuntimeService = (directory, runner, options = {}) => {
12083
12443
  const runTimes = options.runTimes || DOCUMENTED_RUN_TIMES;
12084
- const callerOf = async (caller) => {
12444
+ const callerOf2 = async (caller) => {
12085
12445
  if (!caller) throw new ApiError(401, "Unauthorized");
12086
12446
  const reader = await directory.userById(caller.userId);
12087
12447
  if (!reader || !reader.isActive) throw new ApiError(401, "Unauthorized");
@@ -12138,7 +12498,7 @@ var SASjsApi = (() => {
12138
12498
  return minutes;
12139
12499
  };
12140
12500
  const prepare = async (caller, request) => {
12141
- const reader = await callerOf(caller);
12501
+ const reader = await callerOf2(caller);
12142
12502
  const program = programOf2(request);
12143
12503
  const vars = { ...request.vars || {} };
12144
12504
  const debug = "code" in program ? CODE_DEBUG : parseDebugValue(vars._debug);