@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.
@@ -0,0 +1,60 @@
1
+ /**
2
+ * What the emulated estate derives its generated data from.
3
+ *
4
+ * The emulator cannot execute SAS, so any answer that a real engine would produce
5
+ * - a job log, a listing, a webout - is generated. Generated data is only useful
6
+ * if it is the same data twice: a screenshot taken after a build must still be
7
+ * true after the next one, and a fixture committed today must match the answer
8
+ * tomorrow.
9
+ *
10
+ * The seed is therefore the identity of the estate, and the identity of an estate
11
+ * is the address clients already use to reach it. A target that points at
12
+ * `http://localhost:5000` and a target that points at `http://127.0.0.1:5000` are
13
+ * the same estate spelled two ways, so the address is normalised before it is
14
+ * hashed - and `seed` exists for the two cases where the address is the wrong
15
+ * identity: the estate reached by a different hostname (a container name, a LAN
16
+ * address), and the port that moves between runs (CI picks an ephemeral one).
17
+ *
18
+ * Absent both, every emulated estate shares one seed. That is the only honest
19
+ * default: an emulator with no address has not told us which estate it is.
20
+ */
21
+ /** The seed every emulated estate falls back to. */
22
+ export declare const DEFAULT_SEED = "emulated";
23
+ export interface ViyaSeedSettings {
24
+ /** The address this estate answers at. */
25
+ serverUrl?: string;
26
+ /** Pins the generated data when the address is not the identity. */
27
+ seed?: string | number;
28
+ }
29
+ /**
30
+ * The address reduced to what identifies it.
31
+ *
32
+ * Lowercased host, no default port, no trailing slash, no path: `https://Viya.Example.com:443/SASDrive/`
33
+ * and `https://viya.example.com` are one estate, and a path is where a client
34
+ * lands rather than which server it is. A non-default port stays, because two
35
+ * emulators on one machine are two estates.
36
+ */
37
+ export declare const normaliseServerUrl: (url: string | undefined) => string;
38
+ /** A 32-bit FNV-1a hash, so one string becomes one number. */
39
+ export declare const hash: (input: string) => number;
40
+ /**
41
+ * The number that makes this estate's generated data reproducible.
42
+ *
43
+ * `seed` wins over the address when both are present: it is the override a host
44
+ * reaches for when the address disagrees with the estate, not a second opinion
45
+ * the address gets to overrule.
46
+ */
47
+ export declare const seedOf: (settings?: ViyaSeedSettings) => number;
48
+ /**
49
+ * A uniform generator over one seed (mulberry32).
50
+ *
51
+ * Every draw advances the same 32-bit state, so a sequence is a function of the
52
+ * seed alone - which is the whole point: the same estate answers the same way,
53
+ * and two estates answer differently.
54
+ */
55
+ export declare const generator: (seed: number) => (() => number);
56
+ /** An integer in `[0, bound)`, drawn from the generator. */
57
+ export declare const drawInt: (next: () => number, bound: number) => number;
58
+ /** A line from a list, drawn from the generator. */
59
+ export declare const drawFrom: <T>(next: () => number, values: T[]) => T;
60
+ //# sourceMappingURL=seed.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"seed.d.ts","sourceRoot":"","sources":["../../../src/viya/seed.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,oDAAoD;AACpD,eAAO,MAAM,YAAY,aAAa,CAAA;AAEtC,MAAM,WAAW,gBAAgB;IAC/B,0CAA0C;IAC1C,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,oEAAoE;IACpE,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;CACvB;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,kBAAkB,GAAI,KAAK,MAAM,GAAG,SAAS,KAAG,MA+C5D,CAAA;AAED,8DAA8D;AAC9D,eAAO,MAAM,IAAI,GAAI,OAAO,MAAM,KAAG,MASpC,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,MAAM,GAAI,WAAU,gBAAqB,KAAG,MAOxD,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,SAAS,GAAI,MAAM,MAAM,KAAG,CAAC,MAAM,MAAM,CAarD,CAAA;AAED,4DAA4D;AAC5D,eAAO,MAAM,OAAO,GAAI,MAAM,MAAM,MAAM,EAAE,OAAO,MAAM,KAAG,MAChC,CAAA;AAE5B,oDAAoD;AACpD,eAAO,MAAM,QAAQ,GAAI,CAAC,EAAE,MAAM,MAAM,MAAM,EAAE,QAAQ,CAAC,EAAE,KAAG,CACxB,CAAA"}
@@ -0,0 +1,111 @@
1
+ /**
2
+ * What the emulated estate derives its generated data from.
3
+ *
4
+ * The emulator cannot execute SAS, so any answer that a real engine would produce
5
+ * - a job log, a listing, a webout - is generated. Generated data is only useful
6
+ * if it is the same data twice: a screenshot taken after a build must still be
7
+ * true after the next one, and a fixture committed today must match the answer
8
+ * tomorrow.
9
+ *
10
+ * The seed is therefore the identity of the estate, and the identity of an estate
11
+ * is the address clients already use to reach it. A target that points at
12
+ * `http://localhost:5000` and a target that points at `http://127.0.0.1:5000` are
13
+ * the same estate spelled two ways, so the address is normalised before it is
14
+ * hashed - and `seed` exists for the two cases where the address is the wrong
15
+ * identity: the estate reached by a different hostname (a container name, a LAN
16
+ * address), and the port that moves between runs (CI picks an ephemeral one).
17
+ *
18
+ * Absent both, every emulated estate shares one seed. That is the only honest
19
+ * default: an emulator with no address has not told us which estate it is.
20
+ */
21
+ /** The seed every emulated estate falls back to. */
22
+ export const DEFAULT_SEED = 'emulated';
23
+ /**
24
+ * The address reduced to what identifies it.
25
+ *
26
+ * Lowercased host, no default port, no trailing slash, no path: `https://Viya.Example.com:443/SASDrive/`
27
+ * and `https://viya.example.com` are one estate, and a path is where a client
28
+ * lands rather than which server it is. A non-default port stays, because two
29
+ * emulators on one machine are two estates.
30
+ */
31
+ export const normaliseServerUrl = (url) => {
32
+ if (!url)
33
+ return '';
34
+ const trimmed = url.trim();
35
+ if (!trimmed)
36
+ return '';
37
+ // parsed by hand rather than with `new URL`, because the browser build has no
38
+ // URL global and this module is part of it
39
+ const match = /^(?:([a-z][a-z0-9+.-]*):\/\/)?([^/?#]+)/i.exec(trimmed);
40
+ if (!match)
41
+ return trimmed.replace(/\/+$/, '').toLowerCase();
42
+ const scheme = (match[1] ?? 'http').toLowerCase();
43
+ let authority = match[2].toLowerCase();
44
+ const credentials = authority.lastIndexOf('@');
45
+ if (credentials >= 0)
46
+ authority = authority.slice(credentials + 1);
47
+ let host = authority;
48
+ let port = '';
49
+ if (authority.startsWith('[')) {
50
+ const close = authority.indexOf(']');
51
+ if (close >= 0) {
52
+ host = authority.slice(0, close + 1);
53
+ if (authority[close + 1] === ':')
54
+ port = authority.slice(close + 2);
55
+ }
56
+ }
57
+ else {
58
+ const colon = authority.lastIndexOf(':');
59
+ if (colon >= 0) {
60
+ host = authority.slice(0, colon);
61
+ port = authority.slice(colon + 1);
62
+ }
63
+ }
64
+ const isDefault = (scheme === 'http' && port === '80') ||
65
+ (scheme === 'https' && port === '443');
66
+ // a non-default port stays: two emulators on one machine are two estates
67
+ return `${scheme}://${host}${port && !isDefault ? `:${port}` : ''}`;
68
+ };
69
+ /** A 32-bit FNV-1a hash, so one string becomes one number. */
70
+ export const hash = (input) => {
71
+ let value = 2166136261;
72
+ for (let index = 0; index < input.length; index++) {
73
+ value ^= input.charCodeAt(index);
74
+ value = Math.imul(value, 16777619) >>> 0;
75
+ }
76
+ return value >>> 0;
77
+ };
78
+ /**
79
+ * The number that makes this estate's generated data reproducible.
80
+ *
81
+ * `seed` wins over the address when both are present: it is the override a host
82
+ * reaches for when the address disagrees with the estate, not a second opinion
83
+ * the address gets to overrule.
84
+ */
85
+ export const seedOf = (settings = {}) => {
86
+ if (settings.seed !== undefined && settings.seed !== '')
87
+ return hash(`seed:${settings.seed}`);
88
+ const address = normaliseServerUrl(settings.serverUrl);
89
+ return hash(`url:${address || DEFAULT_SEED}`);
90
+ };
91
+ /**
92
+ * A uniform generator over one seed (mulberry32).
93
+ *
94
+ * Every draw advances the same 32-bit state, so a sequence is a function of the
95
+ * seed alone - which is the whole point: the same estate answers the same way,
96
+ * and two estates answer differently.
97
+ */
98
+ export const generator = (seed) => {
99
+ let state = seed >>> 0;
100
+ return () => {
101
+ state = (state + 0x6d2b79f5) >>> 0;
102
+ let value = state;
103
+ value = Math.imul(value ^ (value >>> 15), value | 1);
104
+ value ^= value + Math.imul(value ^ (value >>> 7), value | 61);
105
+ return ((value ^ (value >>> 14)) >>> 0) / 4294967296;
106
+ };
107
+ };
108
+ /** An integer in `[0, bound)`, drawn from the generator. */
109
+ export const drawInt = (next, bound) => Math.floor(next() * bound);
110
+ /** A line from a list, drawn from the generator. */
111
+ export const drawFrom = (next, values) => values[drawInt(next, values.length)];
@@ -14,22 +14,6 @@
14
14
  * and asks for it later.
15
15
  */
16
16
  import type { ApiResponse, Services } from '../host/dispatch.js';
17
- /** The emulated clock: a fixed instant, so a body is reproducible. */
18
- export declare const EMULATED_NOW = "2026-01-01T00:00:00.000Z";
19
- /** The caller the emulated estate reports when a host does not name one. */
20
- export declare const EMULATED_USER: {
21
- id: string;
22
- name: string;
23
- };
24
- /**
25
- * A uuid shape from a string, deterministically.
26
- *
27
- * Two 32-bit FNV-1a passes give enough of a hash to fill a uuid's fields, and the
28
- * version/variant nibbles are set so the result is a well-formed v4-shaped id.
29
- * Collisions are possible in principle and irrelevant here: the id is a handle
30
- * the emulator hands back, not a uniqueness guarantee.
31
- */
32
- export declare const idFor: (path: string, kind?: string) => string;
33
17
  /** The handlers, keyed by the operation id the table states. */
34
18
  export declare const VIYA_HANDLERS: Record<string, (context: {
35
19
  services: Services;
@@ -1 +1 @@
1
- {"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../../src/viya/service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAA;AAIhE,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;AAsID,gEAAgE;AAChE,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,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,CA4N3B,CAAA"}
1
+ {"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../../src/viya/service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAA;AAmIhE,gEAAgE;AAChE,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,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,CAgO3B,CAAA"}
@@ -1,38 +1,8 @@
1
1
  import { ApiError } from '../types.js';
2
- /** The emulated clock: a fixed instant, so a body is reproducible. */
3
- export const EMULATED_NOW = '2026-01-01T00:00:00.000Z';
4
- /** The caller the emulated estate reports when a host does not name one. */
5
- export const EMULATED_USER = { id: 'emulated-user', name: 'Emulated User' };
6
- /**
7
- * A uuid shape from a string, deterministically.
8
- *
9
- * Two 32-bit FNV-1a passes give enough of a hash to fill a uuid's fields, and the
10
- * version/variant nibbles are set so the result is a well-formed v4-shaped id.
11
- * Collisions are possible in principle and irrelevant here: the id is a handle
12
- * the emulator hands back, not a uniqueness guarantee.
13
- */
14
- export const idFor = (path, kind = 'folder') => {
15
- const hash = (input, seed) => {
16
- let value = seed;
17
- for (let index = 0; index < input.length; index++) {
18
- value ^= input.charCodeAt(index);
19
- value = Math.imul(value, 16777619) >>> 0;
20
- }
21
- return value.toString(16).padStart(8, '0');
22
- };
23
- const raw = hash(`${kind}:${path}`, 2166136261) +
24
- hash(`${kind}:${path}`, 2246822519) +
25
- hash(`${kind}:${path}`, 3266489917) +
26
- hash(`${kind}:${path}`, 668265263);
27
- const hex = raw.slice(0, 32);
28
- return [
29
- hex.slice(0, 8),
30
- hex.slice(8, 12),
31
- `4${hex.slice(13, 16)}`,
32
- `a${hex.slice(17, 20)}`,
33
- hex.slice(20, 32)
34
- ].join('-');
35
- };
2
+ import { requireDrive } from './ports.js';
3
+ import { COMPUTE_HANDLERS } from './compute.js';
4
+ import { FILE_HANDLERS } from './files.js';
5
+ import { idFor, normalisePath, EMULATED_NOW, EMULATED_USER } from './ids.js';
36
6
  const linksFor = (uri, collection = false) => {
37
7
  const self = {
38
8
  href: uri,
@@ -92,16 +62,11 @@ const recycleBin = () => {
92
62
  links: linksFor(`/folders/folders/${id}`)
93
63
  };
94
64
  };
95
- const requireDrive = (services) => {
96
- if (!services.drive)
97
- throw new ApiError(501, 'This host has no drive, so it cannot answer a folder operation.');
98
- return services.drive;
99
- };
100
65
  const pathFromQuery = (request) => {
101
66
  const path = request.query?.path;
102
67
  if (!path)
103
68
  throw new ApiError(400, 'The path parameter is required.');
104
- return path;
69
+ return normalisePath(path);
105
70
  };
106
71
  /** The recycle-bin alias the emulator answers, which is also a valid parent. */
107
72
  const RECYCLE_BIN_ALIAS = '/folders/folders/@myRecycleBin';
@@ -273,7 +238,7 @@ export const VIYA_HANDLERS = {
273
238
  const parent = parentUri
274
239
  ? await pathForId(drive, String(parentUri).split('/').pop())
275
240
  : '/';
276
- const path = `${parent}/${body.name}`.replace(/\/{2,}/g, '/');
241
+ const path = normalisePath(`${parent}/${body.name}`);
277
242
  await drive.mkdir(path);
278
243
  return { status: 201, body: await folderBody(drive, path) };
279
244
  },
@@ -305,9 +270,11 @@ export const VIYA_HANDLERS = {
305
270
  : body.parentFolderUri === ''
306
271
  ? '/'
307
272
  : await pathForId(drive, body.parentFolderUri.split('/').pop());
308
- const target = `${parent}/${name}`.replace(/\/{2,}/g, '/');
273
+ const target = normalisePath(`${parent}/${name}`);
309
274
  if (target !== path)
310
275
  await drive.rename(path, target);
311
276
  return { status: 200, body: await folderBody(drive, target) };
312
- }
277
+ },
278
+ ...COMPUTE_HANDLERS,
279
+ ...FILE_HANDLERS
313
280
  };
@@ -0,0 +1,63 @@
1
+ /** One submitted job, and what the emulator answered for it. */
2
+ export interface EmulatedJob {
3
+ id: string;
4
+ name: string;
5
+ /** The lines the emulated log returns, in order. */
6
+ log: string[];
7
+ /** The state every job is in: the emulator does not fail on purpose. */
8
+ state: 'completed';
9
+ creationTimeStamp: string;
10
+ completedTimeStamp: string;
11
+ sessionId: string;
12
+ }
13
+ export interface EmulatedSession {
14
+ id: string;
15
+ /** When it was created, on the real clock: a client expires sessions by age. */
16
+ createdAt: string;
17
+ jobs: Map<string, EmulatedJob>;
18
+ /** How many jobs this session has accepted, so ids are stable per session. */
19
+ submitted: number;
20
+ }
21
+ /** What a submitted job answers before it is run - the same for every request. */
22
+ export interface SubmittedJob {
23
+ name?: string;
24
+ description?: string;
25
+ /**
26
+ * The program.
27
+ *
28
+ * A string or a list of lines: `@sasjs/adapter` posts an array of lines, and
29
+ * the capture recorded a string because the recorder posted one. Both are the
30
+ * same program, so both are accepted.
31
+ */
32
+ code?: string | string[];
33
+ }
34
+ /**
35
+ * The generated log for one program.
36
+ *
37
+ * Deterministic in (seed, code): the same program on the same estate answers the
38
+ * same lines, so a test can assert a log and a screenshot stays true. The numbers
39
+ * are drawn rather than fixed so that two estates do not look identical, and the
40
+ * generator is re-seeded from the code for each job so that one job's log does not
41
+ * depend on how many jobs ran before it.
42
+ */
43
+ export declare const logFor: (seed: number, program: string | string[]) => string[];
44
+ /**
45
+ * The sessions one estate holds.
46
+ *
47
+ * Keyed by seed, so two emulated estates in one process - a test that runs two
48
+ * targets - do not share a session namespace.
49
+ */
50
+ export declare class EmulatedEstate {
51
+ private readonly seed;
52
+ private readonly sessions;
53
+ private created;
54
+ constructor(seed: number);
55
+ /** A new session: deterministic id, no jobs. */
56
+ create(): EmulatedSession;
57
+ session(id: string): EmulatedSession | undefined;
58
+ remove(id: string): boolean;
59
+ /** A job accepted into a session, with the log the emulator generated for it. */
60
+ submit(session: EmulatedSession, job: SubmittedJob): EmulatedJob;
61
+ }
62
+ export declare const estateFor: (seed: number) => EmulatedEstate;
63
+ //# sourceMappingURL=sessions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sessions.d.ts","sourceRoot":"","sources":["../../../src/viya/sessions.ts"],"names":[],"mappings":"AAiBA,gEAAgE;AAChE,MAAM,WAAW,WAAW;IAC1B,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,MAAM,CAAA;IACZ,oDAAoD;IACpD,GAAG,EAAE,MAAM,EAAE,CAAA;IACb,wEAAwE;IACxE,KAAK,EAAE,WAAW,CAAA;IAClB,iBAAiB,EAAE,MAAM,CAAA;IACzB,kBAAkB,EAAE,MAAM,CAAA;IAC1B,SAAS,EAAE,MAAM,CAAA;CAClB;AAED,MAAM,WAAW,eAAe;IAC9B,EAAE,EAAE,MAAM,CAAA;IACV,gFAAgF;IAChF,SAAS,EAAE,MAAM,CAAA;IACjB,IAAI,EAAE,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC,CAAA;IAC9B,8EAA8E;IAC9E,SAAS,EAAE,MAAM,CAAA;CAClB;AAED,kFAAkF;AAClF,MAAM,WAAW,YAAY;IAC3B,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB;;;;;;OAMG;IACH,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;CACzB;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,MAAM,GAAI,MAAM,MAAM,EAAE,SAAS,MAAM,GAAG,MAAM,EAAE,KAAG,MAAM,EAuBvE,CAAA;AAED;;;;;GAKG;AACH,qBAAa,cAAc;IAKb,OAAO,CAAC,QAAQ,CAAC,IAAI;IAJjC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAqC;IAE9D,OAAO,CAAC,OAAO,CAAI;gBAEU,IAAI,EAAE,MAAM;IAEzC,gDAAgD;IAChD,MAAM,IAAI,eAAe;IAazB,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,eAAe,GAAG,SAAS;IAIhD,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO;IAI3B,iFAAiF;IACjF,MAAM,CAAC,OAAO,EAAE,eAAe,EAAE,GAAG,EAAE,YAAY,GAAG,WAAW;CAuBjE;AAKD,eAAO,MAAM,SAAS,GAAI,MAAM,MAAM,KAAG,cAUxC,CAAA"}
@@ -0,0 +1,108 @@
1
+ /**
2
+ * The emulated compute estate: sessions, jobs and the logs generated for them.
3
+ *
4
+ * A session is a handle a client keeps and asks about later, so it lives in the
5
+ * process that issued it - the way a real compute server's does - and it survives
6
+ * as long as the emulator does. Ids are a function of the seed and a counter
7
+ * rather than a random uuid: a session id that changes between runs would break
8
+ * every fixture that names one, which is the same rule as a folder id.
9
+ *
10
+ * Nothing here executes SAS. The engine is not present, and a log that claimed to
11
+ * be output would be a lie the first time somebody read it. The log echoes what
12
+ * was submitted and states, in the log itself, that it is generated - so a client
13
+ * that parses for NOTEs still gets a well-formed log, and a person reading it is
14
+ * not misled about what ran.
15
+ */
16
+ import { drawFrom, drawInt, generator, hash } from './seed.js';
17
+ /**
18
+ * The generated log for one program.
19
+ *
20
+ * Deterministic in (seed, code): the same program on the same estate answers the
21
+ * same lines, so a test can assert a log and a screenshot stays true. The numbers
22
+ * are drawn rather than fixed so that two estates do not look identical, and the
23
+ * generator is re-seeded from the code for each job so that one job's log does not
24
+ * depend on how many jobs ran before it.
25
+ */
26
+ export const logFor = (seed, program) => {
27
+ const code = Array.isArray(program) ? program.join('\n') : program;
28
+ // seeded from the program's TEXT, not its length: two programs of the same
29
+ // length are two programs, and a fixture that names the numbers would be
30
+ // right for both of them otherwise
31
+ const next = generator(seed ^ hash(`code:${code}`));
32
+ const rows = drawInt(next, 900) + 100;
33
+ const seconds = (drawInt(next, 90) + 10) / 100;
34
+ const lines = code
35
+ .split('\n')
36
+ .map((line, index) => `${String(index + 1).padStart(3, ' ')} ${line}`);
37
+ return [
38
+ 'NOTE: Emulated compute engine - this log is generated, not produced by SAS.',
39
+ 'NOTE: No library was touched: the emulated engine has none.',
40
+ ...lines,
41
+ `NOTE: The data set WORK.EMULATED has ${rows} observations and 3 variables.`,
42
+ `NOTE: DATA statement used (Total process time):`,
43
+ `NOTE: real time ${seconds.toFixed(2)} seconds`,
44
+ `NOTE: user cpu time ${(seconds / 2).toFixed(2)} seconds`,
45
+ `NOTE: ${drawFrom(next, ['The emulated run completed.', 'Emulation complete - no SAS was executed.'])}`
46
+ ];
47
+ };
48
+ /**
49
+ * The sessions one estate holds.
50
+ *
51
+ * Keyed by seed, so two emulated estates in one process - a test that runs two
52
+ * targets - do not share a session namespace.
53
+ */
54
+ export class EmulatedEstate {
55
+ constructor(seed) {
56
+ this.seed = seed;
57
+ this.sessions = new Map();
58
+ this.created = 0;
59
+ }
60
+ /** A new session: deterministic id, no jobs. */
61
+ create() {
62
+ const session = {
63
+ id: `emulated-${this.seed.toString(16)}-ses${String(this.created++).padStart(4, '0')}`,
64
+ createdAt: new Date().toISOString(),
65
+ jobs: new Map(),
66
+ submitted: 0
67
+ };
68
+ this.sessions.set(session.id, session);
69
+ return session;
70
+ }
71
+ session(id) {
72
+ return this.sessions.get(id);
73
+ }
74
+ remove(id) {
75
+ return this.sessions.delete(id);
76
+ }
77
+ /** A job accepted into a session, with the log the emulator generated for it. */
78
+ submit(session, job) {
79
+ const id = `${session.id}-job${String(session.submitted++).padStart(4, '0')}`;
80
+ const code = job.code ?? '';
81
+ const submitted = {
82
+ id,
83
+ name: job.name ?? id,
84
+ // the log is a function of the seed and the program alone, so the same
85
+ // program answers the same lines whatever session it was submitted in
86
+ log: logFor(this.seed, code),
87
+ state: 'completed',
88
+ // the real clock, unlike a folder's: a client computes a session's age
89
+ // from this stamp and expires it, so a fixed instant in the past is a
90
+ // session that is born expired
91
+ creationTimeStamp: new Date().toISOString(),
92
+ completedTimeStamp: new Date().toISOString(),
93
+ sessionId: session.id
94
+ };
95
+ session.jobs.set(id, submitted);
96
+ return submitted;
97
+ }
98
+ }
99
+ /** One estate per seed, for the life of the process. */
100
+ const estates = new Map();
101
+ export const estateFor = (seed) => {
102
+ const existing = estates.get(seed);
103
+ if (existing)
104
+ return existing;
105
+ const estate = new EmulatedEstate(seed);
106
+ estates.set(seed, estate);
107
+ return estate;
108
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sasjs/api",
3
- "version": "0.35.0",
3
+ "version": "0.36.0",
4
4
  "description": "The SASjs Server API, described once - a generated route table, the services behind it, and the conformance corpus both hosts run",
5
5
  "license": "MIT",
6
6
  "author": "SASjs",
@@ -48,7 +48,8 @@
48
48
  "check:viya": "node scripts/viya-generate.mjs --check",
49
49
  "check:builds": "node scripts/check-builds.mjs",
50
50
  "audit:gate": "node scripts/audit.mjs",
51
- "check:appstream": "node scripts/appstream-browser-check.cjs"
51
+ "check:appstream": "node scripts/appstream-browser-check.cjs",
52
+ "conformance": "node tools/viya-conformance/conformance.mjs"
52
53
  },
53
54
  "devDependencies": {
54
55
  "@semantic-release/commit-analyzer": "13.0.1",