@sasjs/api 0.33.0 → 0.35.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/browser.js +1962 -50
- package/build/cjs/corpus/viya/operations.js +647 -0
- package/build/cjs/index.js +60 -40
- package/build/cjs/viya/dispatch.js +141 -0
- package/build/cjs/viya/overlay.js +104 -0
- package/build/cjs/viya/service.js +317 -0
- package/build/cjs/viya/surface.js +1 -49
- package/build/cjs/viya/table.js +805 -0
- package/build/cjs/viya/unrecorded.js +58 -0
- package/build/cjs/viya/version.js +43 -0
- package/build/esm/corpus/viya/operations.d.ts +28 -0
- package/build/esm/corpus/viya/operations.d.ts.map +1 -0
- package/build/esm/corpus/viya/operations.js +644 -0
- package/build/esm/index.d.ts +12 -0
- package/build/esm/index.d.ts.map +1 -1
- package/build/esm/index.js +7 -0
- package/build/esm/viya/dispatch.d.ts +35 -0
- package/build/esm/viya/dispatch.d.ts.map +1 -0
- package/build/esm/viya/dispatch.js +136 -0
- package/build/esm/viya/overlay.d.ts +25 -0
- package/build/esm/viya/overlay.d.ts.map +1 -0
- package/build/esm/viya/overlay.js +100 -0
- package/build/esm/viya/service.d.ts +44 -0
- package/build/esm/viya/service.d.ts.map +1 -0
- package/build/esm/viya/service.js +313 -0
- package/build/esm/viya/surface.d.ts.map +1 -1
- package/build/esm/viya/surface.js +1 -49
- package/build/esm/viya/table.d.ts +39 -0
- package/build/esm/viya/table.d.ts.map +1 -0
- package/build/esm/viya/table.js +802 -0
- package/build/esm/viya/unrecorded.d.ts +24 -0
- package/build/esm/viya/unrecorded.d.ts.map +1 -0
- package/build/esm/viya/unrecorded.js +55 -0
- package/build/esm/viya/version.d.ts +37 -0
- package/build/esm/viya/version.d.ts.map +1 -0
- package/build/esm/viya/version.js +39 -0
- package/package.json +3 -1
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one entry point for a Viya-shaped request.
|
|
3
|
+
*
|
|
4
|
+
* A host owns its transport - express on one side, a service worker on the other
|
|
5
|
+
* - and this is where a Viya path becomes an answer. The table supplies the
|
|
6
|
+
* operations, the overlay supplies the decision, the handlers supply the body,
|
|
7
|
+
* and every answer leaves through one place so the emulated header cannot be
|
|
8
|
+
* forgotten on the paths that are easiest to forget it: a refusal and a 404.
|
|
9
|
+
*
|
|
10
|
+
* A request for a path the table does not have is a 404 that says so, and it
|
|
11
|
+
* carries the header like everything else - an emulated 404 must not be read as
|
|
12
|
+
* evidence about Viya.
|
|
13
|
+
*/
|
|
14
|
+
import type { ApiRequest, ApiResponse, Services } from '../host/dispatch.js';
|
|
15
|
+
import { type ViyaOperation } from './table.js';
|
|
16
|
+
/** An answer that states which contract produced it. */
|
|
17
|
+
export interface ViyaResponse extends ApiResponse {
|
|
18
|
+
/** Always carries `x-sasjs-emulated`, including on a 404 and a 501. */
|
|
19
|
+
headers: Record<string, string>;
|
|
20
|
+
}
|
|
21
|
+
export interface ViyaMatch {
|
|
22
|
+
operation: ViyaOperation;
|
|
23
|
+
/** The values the `{}` segments carried, in order. */
|
|
24
|
+
segments: string[];
|
|
25
|
+
}
|
|
26
|
+
/** The operation a request names, ignoring case in the static segments. */
|
|
27
|
+
export declare const findViyaOperation: (method: string, path: string) => ViyaMatch | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* Answer a Viya request.
|
|
30
|
+
*
|
|
31
|
+
* `request` is the same shape the SASjs dispatch takes, so a host that already
|
|
32
|
+
* resolves a caller - a session cookie - passes `subject` rather than a token.
|
|
33
|
+
*/
|
|
34
|
+
export declare const dispatchViya: (services: Services, method: string, path: string, request?: ApiRequest) => Promise<ViyaResponse>;
|
|
35
|
+
//# sourceMappingURL=dispatch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dispatch.d.ts","sourceRoot":"","sources":["../../../src/viya/dispatch.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAA;AAE5E,OAAO,EAAc,KAAK,aAAa,EAAE,MAAM,YAAY,CAAA;AAK3D,wDAAwD;AACxD,MAAM,WAAW,YAAa,SAAQ,WAAW;IAC/C,uEAAuE;IACvE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAChC;AAED,MAAM,WAAW,SAAS;IACxB,SAAS,EAAE,aAAa,CAAA;IACxB,sDAAsD;IACtD,QAAQ,EAAE,MAAM,EAAE,CAAA;CACnB;AA4BD,2EAA2E;AAC3E,eAAO,MAAM,iBAAiB,GAC5B,QAAQ,MAAM,EACd,MAAM,MAAM,KACX,SAAS,GAAG,SA0Bd,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,YAAY,GACvB,UAAU,QAAQ,EAClB,QAAQ,MAAM,EACd,MAAM,MAAM,EACZ,UAAS,UAAe,KACvB,OAAO,CAAC,YAAY,CAyFtB,CAAA"}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { isApiError } from '../types.js';
|
|
2
|
+
import { VIYA_TABLE } from './table.js';
|
|
3
|
+
import { VIYA_OVERLAY } from './overlay.js';
|
|
4
|
+
import { VIYA_HANDLERS } from './service.js';
|
|
5
|
+
import { emulatedHeaders } from './version.js';
|
|
6
|
+
const escape = (segment) => segment.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
7
|
+
const compiled = VIYA_TABLE.filter((operation) => operation.path.includes('{}')).map((operation) => ({
|
|
8
|
+
operation,
|
|
9
|
+
pattern: new RegExp('^' +
|
|
10
|
+
operation.path
|
|
11
|
+
.split('/')
|
|
12
|
+
.map((segment) => (segment === '{}' ? '([^/]+)' : escape(segment)))
|
|
13
|
+
.join('/') +
|
|
14
|
+
'$',
|
|
15
|
+
// the static segments ignore case, as the hosts do
|
|
16
|
+
'i')
|
|
17
|
+
}));
|
|
18
|
+
const exact = new Map();
|
|
19
|
+
for (const operation of VIYA_TABLE)
|
|
20
|
+
if (!operation.path.includes('{}'))
|
|
21
|
+
// keyed in lower case, because the hosts are: express's `case sensitive
|
|
22
|
+
// routing` is off and the emulated estate has to match what they serve
|
|
23
|
+
exact.set(`${operation.method} ${operation.path}`.toLowerCase(), operation);
|
|
24
|
+
/** The operation a request names, ignoring case in the static segments. */
|
|
25
|
+
export const findViyaOperation = (method, path) => {
|
|
26
|
+
const verb = method.toUpperCase();
|
|
27
|
+
const clean = path.split('?')[0].replace(/\/+$/, '') || '/';
|
|
28
|
+
const staticHit = exact.get(`${verb} ${clean}`.toLowerCase());
|
|
29
|
+
if (staticHit)
|
|
30
|
+
return { operation: staticHit, segments: [] };
|
|
31
|
+
for (const candidate of compiled) {
|
|
32
|
+
if (candidate.operation.method !== verb)
|
|
33
|
+
continue;
|
|
34
|
+
const match = candidate.pattern.exec(clean);
|
|
35
|
+
if (match)
|
|
36
|
+
return {
|
|
37
|
+
operation: candidate.operation,
|
|
38
|
+
segments: match.slice(1).map((segment) => {
|
|
39
|
+
try {
|
|
40
|
+
return decodeURIComponent(segment);
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return segment;
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
return undefined;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Answer a Viya request.
|
|
52
|
+
*
|
|
53
|
+
* `request` is the same shape the SASjs dispatch takes, so a host that already
|
|
54
|
+
* resolves a caller - a session cookie - passes `subject` rather than a token.
|
|
55
|
+
*/
|
|
56
|
+
export const dispatchViya = async (services, method, path, request = {}) => {
|
|
57
|
+
const headers = emulatedHeaders();
|
|
58
|
+
const found = findViyaOperation(method, path);
|
|
59
|
+
if (!found)
|
|
60
|
+
return {
|
|
61
|
+
status: 404,
|
|
62
|
+
headers,
|
|
63
|
+
body: {
|
|
64
|
+
version: 2,
|
|
65
|
+
httpStatusCode: 404,
|
|
66
|
+
errorCode: 404,
|
|
67
|
+
message: `No operation is emulated at ${method.toUpperCase()} ${path}. The emulated estate answers the client's call surface and nothing else.`
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const entry = VIYA_OVERLAY[found.operation.id];
|
|
71
|
+
if (!entry)
|
|
72
|
+
// the overlay refuses to import unclassified rows, so reaching here is a
|
|
73
|
+
// change that bypassed that check
|
|
74
|
+
return {
|
|
75
|
+
status: 501,
|
|
76
|
+
headers,
|
|
77
|
+
body: { message: `${found.operation.id} has no decision in the overlay.` }
|
|
78
|
+
};
|
|
79
|
+
if (!entry.implemented)
|
|
80
|
+
return {
|
|
81
|
+
status: 501,
|
|
82
|
+
headers,
|
|
83
|
+
body: {
|
|
84
|
+
message: `${found.operation.id} is not emulated. ${entry.reason ?? ''}`.trim()
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
const handler = VIYA_HANDLERS[found.operation.id];
|
|
88
|
+
if (!handler)
|
|
89
|
+
return {
|
|
90
|
+
status: 501,
|
|
91
|
+
headers,
|
|
92
|
+
body: {
|
|
93
|
+
message: `${found.operation.id} is marked implemented, but no handler answers it.`
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
try {
|
|
97
|
+
const answer = await handler({
|
|
98
|
+
services,
|
|
99
|
+
request,
|
|
100
|
+
params: {
|
|
101
|
+
id: found.segments[0],
|
|
102
|
+
segments: found.segments
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
return {
|
|
106
|
+
...answer,
|
|
107
|
+
// the emulated headers are spread LAST: they are the invariant this module
|
|
108
|
+
// exists to guarantee, so a handler cannot overwrite the stamp even by
|
|
109
|
+
// returning headers of its own
|
|
110
|
+
headers: { ...answer.headers, ...headers }
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
if (isApiError(error))
|
|
115
|
+
return {
|
|
116
|
+
status: error.status,
|
|
117
|
+
headers,
|
|
118
|
+
body: error.body ?? {
|
|
119
|
+
version: 2,
|
|
120
|
+
httpStatusCode: error.status,
|
|
121
|
+
errorCode: error.status,
|
|
122
|
+
message: error.message
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
return {
|
|
126
|
+
status: 500,
|
|
127
|
+
headers,
|
|
128
|
+
body: {
|
|
129
|
+
version: 2,
|
|
130
|
+
httpStatusCode: 500,
|
|
131
|
+
errorCode: 500,
|
|
132
|
+
message: String(error?.message ?? error)
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface ViyaOverlayEntry {
|
|
2
|
+
implemented: boolean;
|
|
3
|
+
/** Present when declined: what a caller is told, and why. */
|
|
4
|
+
reason?: string;
|
|
5
|
+
}
|
|
6
|
+
export declare const VIYA_OVERLAY: Record<string, ViyaOverlayEntry>;
|
|
7
|
+
/**
|
|
8
|
+
* Refuse an overlay and a table that do not describe the same operations.
|
|
9
|
+
*
|
|
10
|
+
* Both directions, because each covers a different mistake:
|
|
11
|
+
*
|
|
12
|
+
* - an entry the table does not have is a claim about a call no client makes,
|
|
13
|
+
* which is what a stale entry becomes when an operation is retired;
|
|
14
|
+
* - a row with no entry is a capture that landed without a decision. One loop
|
|
15
|
+
* is not enough: a re-capture that adds an operation would import cleanly and
|
|
16
|
+
* answer 501 at run time, and `dispatch.ts` says the overlay refuses exactly
|
|
17
|
+
* that.
|
|
18
|
+
*
|
|
19
|
+
* Called at import, and exported so the refusals themselves can be tested: a
|
|
20
|
+
* check that only runs on import is a check nothing can pin.
|
|
21
|
+
*/
|
|
22
|
+
export declare const assertOverlayEntries: (overlay: Record<string, ViyaOverlayEntry>, operations?: Array<{
|
|
23
|
+
id: string;
|
|
24
|
+
}>) => void;
|
|
25
|
+
//# sourceMappingURL=overlay.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"overlay.d.ts","sourceRoot":"","sources":["../../../src/viya/overlay.ts"],"names":[],"mappings":"AAeA,MAAM,WAAW,gBAAgB;IAC/B,WAAW,EAAE,OAAO,CAAA;IACpB,6DAA6D;IAC7D,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAsBD,eAAO,MAAM,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,CA8CzD,CAAA;AAED;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,oBAAoB,GAC/B,SAAS,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,EACzC,aAAY,KAAK,CAAC;IAAE,EAAE,EAAE,MAAM,CAAA;CAAE,CAAc,KAC7C,IAcF,CAAA"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What this package answers of the Viya table, and what it declines.
|
|
3
|
+
*
|
|
4
|
+
* `src/viya/table.ts` states what a capture observed. This file states the
|
|
5
|
+
* decision for each of those operations: a handler that answers it, or a reason
|
|
6
|
+
* that travels into the 501 body. Every row needs an entry, and importing this
|
|
7
|
+
* file throws if one is missing - the failure mode the route table already
|
|
8
|
+
* refuses, because a row with no decision reads exactly like a row that is about
|
|
9
|
+
* to be done.
|
|
10
|
+
*
|
|
11
|
+
* A declined row is not a TODO. The reason names what is missing, so a caller
|
|
12
|
+
* that reaches it learns why rather than that something is unimplemented.
|
|
13
|
+
*/
|
|
14
|
+
import { VIYA_TABLE } from './table.js';
|
|
15
|
+
/**
|
|
16
|
+
* The reason the session and file families carry.
|
|
17
|
+
*
|
|
18
|
+
* They need the session port and the file content routes, which this slice does
|
|
19
|
+
* not mount. Saying it once is better than saying it thirteen times in slightly
|
|
20
|
+
* different words.
|
|
21
|
+
*/
|
|
22
|
+
const NEEDS_RUNTIME = 'The emulated estate answers folders, identities and types today. This operation needs the session or file port, which the host mounting the emulator has not supplied.';
|
|
23
|
+
/**
|
|
24
|
+
* The token endpoint is the host's.
|
|
25
|
+
*
|
|
26
|
+
* A host that mounts the emulator owns the token port - it is the thing that
|
|
27
|
+
* issues what a client presents - so the emulator declines rather than inventing
|
|
28
|
+
* a second answer to the same question.
|
|
29
|
+
*/
|
|
30
|
+
const NEEDS_AUTH = 'The token endpoint is answered by the host that mounts the emulator, which owns the token port.';
|
|
31
|
+
export const VIYA_OVERLAY = {
|
|
32
|
+
// answered here
|
|
33
|
+
'POST /folders/folders': { implemented: true },
|
|
34
|
+
'GET /folders/folders/{}': { implemented: true },
|
|
35
|
+
'PATCH /folders/folders/{}': { implemented: true },
|
|
36
|
+
'GET /folders/folders/{}/members': { implemented: true },
|
|
37
|
+
'GET /folders/folders/@item': { implemented: true },
|
|
38
|
+
'GET /folders/folders/@myRecycleBin': { implemented: true },
|
|
39
|
+
'GET /identities/users/@currentUser': { implemented: true },
|
|
40
|
+
'GET /types/types': { implemented: true },
|
|
41
|
+
// declined: the session and file ports
|
|
42
|
+
'GET /compute/contexts': { implemented: false, reason: NEEDS_RUNTIME },
|
|
43
|
+
'POST /compute/contexts/{}/sessions': {
|
|
44
|
+
implemented: false,
|
|
45
|
+
reason: NEEDS_RUNTIME
|
|
46
|
+
},
|
|
47
|
+
'DELETE /compute/sessions/{}': { implemented: false, reason: NEEDS_RUNTIME },
|
|
48
|
+
'POST /compute/sessions/{}/jobs': {
|
|
49
|
+
implemented: false,
|
|
50
|
+
reason: NEEDS_RUNTIME
|
|
51
|
+
},
|
|
52
|
+
'GET /compute/sessions/{}/jobs/{}': {
|
|
53
|
+
implemented: false,
|
|
54
|
+
reason: NEEDS_RUNTIME
|
|
55
|
+
},
|
|
56
|
+
'GET /compute/sessions/{}/jobs/{}/log/content': {
|
|
57
|
+
implemented: false,
|
|
58
|
+
reason: NEEDS_RUNTIME
|
|
59
|
+
},
|
|
60
|
+
'GET /compute/sessions/{}/jobs/{}/state': {
|
|
61
|
+
implemented: false,
|
|
62
|
+
reason: NEEDS_RUNTIME
|
|
63
|
+
},
|
|
64
|
+
'GET /compute/sessions/{}/state': {
|
|
65
|
+
implemented: false,
|
|
66
|
+
reason: NEEDS_RUNTIME
|
|
67
|
+
},
|
|
68
|
+
'POST /files/files': { implemented: false, reason: NEEDS_RUNTIME },
|
|
69
|
+
'GET /files/files/{}': { implemented: false, reason: NEEDS_RUNTIME },
|
|
70
|
+
'PATCH /files/files/{}': { implemented: false, reason: NEEDS_RUNTIME },
|
|
71
|
+
'GET /files/files/{}/content': { implemented: false, reason: NEEDS_RUNTIME },
|
|
72
|
+
'PUT /files/files/{}/content': { implemented: false, reason: NEEDS_RUNTIME },
|
|
73
|
+
// declined: the host owns the token port
|
|
74
|
+
'POST /SASLogon/oauth/token': { implemented: false, reason: NEEDS_AUTH }
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Refuse an overlay and a table that do not describe the same operations.
|
|
78
|
+
*
|
|
79
|
+
* Both directions, because each covers a different mistake:
|
|
80
|
+
*
|
|
81
|
+
* - an entry the table does not have is a claim about a call no client makes,
|
|
82
|
+
* which is what a stale entry becomes when an operation is retired;
|
|
83
|
+
* - a row with no entry is a capture that landed without a decision. One loop
|
|
84
|
+
* is not enough: a re-capture that adds an operation would import cleanly and
|
|
85
|
+
* answer 501 at run time, and `dispatch.ts` says the overlay refuses exactly
|
|
86
|
+
* that.
|
|
87
|
+
*
|
|
88
|
+
* Called at import, and exported so the refusals themselves can be tested: a
|
|
89
|
+
* check that only runs on import is a check nothing can pin.
|
|
90
|
+
*/
|
|
91
|
+
export const assertOverlayEntries = (overlay, operations = VIYA_TABLE) => {
|
|
92
|
+
const known = new Set(operations.map((operation) => operation.id));
|
|
93
|
+
for (const id of Object.keys(overlay))
|
|
94
|
+
if (!known.has(id))
|
|
95
|
+
throw new Error(`src/viya/overlay.ts names an operation the table does not have: ${id}. The table is generated from a capture, so an entry for an operation nobody observed is a claim about a call no client makes.`);
|
|
96
|
+
for (const operation of operations)
|
|
97
|
+
if (!(operation.id in overlay))
|
|
98
|
+
throw new Error(`src/viya/overlay.ts has no decision for ${operation.id}. Every captured operation states whether this package implements it and, when it does not, the reason it does not: add \`{ implemented: false, reason: '...' }\` until a handler exists.`);
|
|
99
|
+
};
|
|
100
|
+
assertOverlayEntries(VIYA_OVERLAY);
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Viya-shaped answers, built from the services the package already has.
|
|
3
|
+
*
|
|
4
|
+
* Each handler calls a port (`DriveService`, `IdentityService`) and returns the
|
|
5
|
+
* shape the capture recorded for that operation - the shape is in
|
|
6
|
+
* `src/viya/table.ts`, and the spec holds the two together. Nothing here
|
|
7
|
+
* re-implements a rule the SASjs API side already decides: a folder that does not
|
|
8
|
+
* exist is the drive's 404, and a path that climbs out of the root is the drive's
|
|
9
|
+
* refusal.
|
|
10
|
+
*
|
|
11
|
+
* Identifiers are derived from the path rather than minted, so the same folder is
|
|
12
|
+
* the same id on every host and every run. That is the seed rule applied to the
|
|
13
|
+
* one thing the emulator cannot invent per request: a client stores a folder id
|
|
14
|
+
* and asks for it later.
|
|
15
|
+
*/
|
|
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
|
+
/** The handlers, keyed by the operation id the table states. */
|
|
34
|
+
export declare const VIYA_HANDLERS: Record<string, (context: {
|
|
35
|
+
services: Services;
|
|
36
|
+
request: {
|
|
37
|
+
query?: Record<string, string | undefined>;
|
|
38
|
+
body?: unknown;
|
|
39
|
+
token?: string;
|
|
40
|
+
subject?: unknown;
|
|
41
|
+
};
|
|
42
|
+
params: Record<string, string>;
|
|
43
|
+
}) => Promise<ApiResponse>>;
|
|
44
|
+
//# sourceMappingURL=service.d.ts.map
|
|
@@ -0,0 +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"}
|
|
@@ -0,0 +1,313 @@
|
|
|
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
|
+
};
|
|
36
|
+
const linksFor = (uri, collection = false) => {
|
|
37
|
+
const self = {
|
|
38
|
+
href: uri,
|
|
39
|
+
uri,
|
|
40
|
+
rel: 'self',
|
|
41
|
+
method: 'GET',
|
|
42
|
+
type: 'application/vnd.sas.folder'
|
|
43
|
+
};
|
|
44
|
+
const members = {
|
|
45
|
+
href: `${uri}/members`,
|
|
46
|
+
uri: `${uri}/members`,
|
|
47
|
+
rel: 'members',
|
|
48
|
+
method: 'GET',
|
|
49
|
+
type: 'application/vnd.sas.collection',
|
|
50
|
+
itemType: 'folder'
|
|
51
|
+
};
|
|
52
|
+
// a collection answer leads with the collection link, which carries the
|
|
53
|
+
// itemType its capture recorded
|
|
54
|
+
return collection ? [members, self] : [self, members];
|
|
55
|
+
};
|
|
56
|
+
const folderBody = async (drive, path) => {
|
|
57
|
+
const listing = await drive.list(path);
|
|
58
|
+
const id = idFor(path);
|
|
59
|
+
const name = path.split('/').filter(Boolean).pop() ?? '';
|
|
60
|
+
const parent = path.split('/').slice(0, -1).join('/') || '/';
|
|
61
|
+
return {
|
|
62
|
+
id,
|
|
63
|
+
name,
|
|
64
|
+
description: '',
|
|
65
|
+
type: 'folder',
|
|
66
|
+
version: 1,
|
|
67
|
+
createdBy: EMULATED_USER.id,
|
|
68
|
+
modifiedBy: EMULATED_USER.id,
|
|
69
|
+
creationTimeStamp: EMULATED_NOW,
|
|
70
|
+
modifiedTimeStamp: EMULATED_NOW,
|
|
71
|
+
memberCount: listing.files.length + listing.folders.length,
|
|
72
|
+
parentFolderUri: `/folders/folders/${idFor(parent)}`,
|
|
73
|
+
links: linksFor(`/folders/folders/${id}`)
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
const recycleBin = () => {
|
|
77
|
+
const path = '@myRecycleBin';
|
|
78
|
+
const id = idFor(path);
|
|
79
|
+
return {
|
|
80
|
+
id,
|
|
81
|
+
name: 'Recycle Bin',
|
|
82
|
+
description: 'Recycle Bin',
|
|
83
|
+
type: 'trashFolder',
|
|
84
|
+
version: 1,
|
|
85
|
+
createdBy: EMULATED_USER.id,
|
|
86
|
+
modifiedBy: EMULATED_USER.id,
|
|
87
|
+
creationTimeStamp: EMULATED_NOW,
|
|
88
|
+
modifiedTimeStamp: EMULATED_NOW,
|
|
89
|
+
memberCount: 0,
|
|
90
|
+
parentFolderUri: '',
|
|
91
|
+
properties: { allowMove: 'false' },
|
|
92
|
+
links: linksFor(`/folders/folders/${id}`)
|
|
93
|
+
};
|
|
94
|
+
};
|
|
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
|
+
const pathFromQuery = (request) => {
|
|
101
|
+
const path = request.query?.path;
|
|
102
|
+
if (!path)
|
|
103
|
+
throw new ApiError(400, 'The path parameter is required.');
|
|
104
|
+
return path;
|
|
105
|
+
};
|
|
106
|
+
/** The recycle-bin alias the emulator answers, which is also a valid parent. */
|
|
107
|
+
const RECYCLE_BIN_ALIAS = '/folders/folders/@myRecycleBin';
|
|
108
|
+
const isRecycleBin = (uri) => !!uri &&
|
|
109
|
+
(uri === RECYCLE_BIN_ALIAS || uri.endsWith(`/${idFor('@myRecycleBin')}`));
|
|
110
|
+
/**
|
|
111
|
+
* The path a folder id stands for, found by walking the drive.
|
|
112
|
+
*
|
|
113
|
+
* An id is derived from a path, so the drive is the index: a map would be a
|
|
114
|
+
* second source of truth that goes stale the moment a folder is renamed or
|
|
115
|
+
* moved - and would answer 404 for every id after a restart, which is the
|
|
116
|
+
* opposite of "a client stores an id and asks for it later".
|
|
117
|
+
*/
|
|
118
|
+
const pathForId = async (drive, id) => {
|
|
119
|
+
const walk = (node) => {
|
|
120
|
+
if (node.isFolder && idFor(node.relativePath) === id)
|
|
121
|
+
return node.relativePath;
|
|
122
|
+
for (const child of node.children) {
|
|
123
|
+
const hit = walk(child);
|
|
124
|
+
if (hit)
|
|
125
|
+
return hit;
|
|
126
|
+
}
|
|
127
|
+
return undefined;
|
|
128
|
+
};
|
|
129
|
+
const { tree } = await drive.tree('/');
|
|
130
|
+
const path = walk(tree);
|
|
131
|
+
if (!path)
|
|
132
|
+
throw new ApiError(404, `No folder matches the id ${id}. Ids are derived from paths, so an id that names no folder is one no folder ever had.`);
|
|
133
|
+
return path;
|
|
134
|
+
};
|
|
135
|
+
/** The handlers, keyed by the operation id the table states. */
|
|
136
|
+
export const VIYA_HANDLERS = {
|
|
137
|
+
'GET /identities/users/@currentUser': async ({ services, request }) => {
|
|
138
|
+
const subject = request.subject ??
|
|
139
|
+
(await services.identity?.authenticate(request.token));
|
|
140
|
+
const id = subject?.userId ?? EMULATED_USER.id;
|
|
141
|
+
return {
|
|
142
|
+
status: 200,
|
|
143
|
+
body: {
|
|
144
|
+
id,
|
|
145
|
+
name: subject?.userId ?? EMULATED_USER.name,
|
|
146
|
+
providerId: 'sas',
|
|
147
|
+
state: 'active',
|
|
148
|
+
version: 1,
|
|
149
|
+
addresses: [],
|
|
150
|
+
emailAddresses: [],
|
|
151
|
+
links: [
|
|
152
|
+
{
|
|
153
|
+
href: `/identities/users/${id}`,
|
|
154
|
+
uri: `/identities/users/${id}`,
|
|
155
|
+
rel: 'self',
|
|
156
|
+
method: 'GET',
|
|
157
|
+
type: 'application/vnd.sas.identity.user'
|
|
158
|
+
}
|
|
159
|
+
]
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
},
|
|
163
|
+
'GET /types/types': async () => ({
|
|
164
|
+
status: 200,
|
|
165
|
+
body: {
|
|
166
|
+
count: 1,
|
|
167
|
+
items: [
|
|
168
|
+
{
|
|
169
|
+
name: 'file',
|
|
170
|
+
type: 'typeDef',
|
|
171
|
+
version: 1,
|
|
172
|
+
links: [{ href: '/types/types/file', rel: 'self', method: 'GET' }]
|
|
173
|
+
}
|
|
174
|
+
]
|
|
175
|
+
}
|
|
176
|
+
}),
|
|
177
|
+
'GET /folders/folders/@item': async ({ services, request }) => ({
|
|
178
|
+
status: 200,
|
|
179
|
+
body: await folderBody(requireDrive(services), pathFromQuery(request))
|
|
180
|
+
}),
|
|
181
|
+
'GET /folders/folders/@myRecycleBin': async () => ({
|
|
182
|
+
status: 200,
|
|
183
|
+
body: recycleBin()
|
|
184
|
+
}),
|
|
185
|
+
'GET /folders/folders/{}': async ({ services, params }) => {
|
|
186
|
+
// the bin is addressed by id too, and it is not in the drive's tree
|
|
187
|
+
if (params.id === idFor('@myRecycleBin'))
|
|
188
|
+
return { status: 200, body: recycleBin() };
|
|
189
|
+
const drive = requireDrive(services);
|
|
190
|
+
return {
|
|
191
|
+
status: 200,
|
|
192
|
+
body: await folderBody(drive, await pathForId(drive, params.id))
|
|
193
|
+
};
|
|
194
|
+
},
|
|
195
|
+
'GET /folders/folders/{}/members': async ({ services, params, request }) => {
|
|
196
|
+
const drive = requireDrive(services);
|
|
197
|
+
const limit = Number(request.query?.limit ?? 0) || 0;
|
|
198
|
+
// the bin is not a folder in the drive, and its own body advertises this
|
|
199
|
+
// link - so a client following the answer it just received must not 404
|
|
200
|
+
if (params.id === idFor('@myRecycleBin'))
|
|
201
|
+
return {
|
|
202
|
+
status: 200,
|
|
203
|
+
body: {
|
|
204
|
+
accept: 'application/json',
|
|
205
|
+
name: 'members',
|
|
206
|
+
start: 0,
|
|
207
|
+
// the query value is echoed as the sibling branch echoes it: a caller
|
|
208
|
+
// that asked for five and is told zero cannot tell what happened
|
|
209
|
+
limit,
|
|
210
|
+
count: 0,
|
|
211
|
+
version: 2,
|
|
212
|
+
items: [],
|
|
213
|
+
links: linksFor(`/folders/folders/${idFor('@myRecycleBin')}`, true)
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
const path = await pathForId(drive, params.id);
|
|
217
|
+
const listing = await drive.list(path);
|
|
218
|
+
const items = [
|
|
219
|
+
...listing.folders.map((name) => ({
|
|
220
|
+
id: idFor(`${path}/${name}`.replace(/\/{2,}/g, '/')),
|
|
221
|
+
name,
|
|
222
|
+
type: 'folder',
|
|
223
|
+
version: 1,
|
|
224
|
+
createdBy: EMULATED_USER.id,
|
|
225
|
+
modifiedBy: EMULATED_USER.id,
|
|
226
|
+
creationTimeStamp: EMULATED_NOW,
|
|
227
|
+
modifiedTimeStamp: EMULATED_NOW,
|
|
228
|
+
parentFolderUri: `/folders/folders/${idFor(path)}`,
|
|
229
|
+
uri: `${path}/${name}`.replace(/\/{2,}/g, '/'),
|
|
230
|
+
links: linksFor(`/folders/folders/${idFor(`${path}/${name}`.replace(/\/{2,}/g, '/'))}`)
|
|
231
|
+
})),
|
|
232
|
+
...listing.files.map((name) => ({
|
|
233
|
+
id: idFor(`${path}/${name}`.replace(/\/{2,}/g, '/'), 'file'),
|
|
234
|
+
name,
|
|
235
|
+
type: 'file',
|
|
236
|
+
version: 1,
|
|
237
|
+
contentType: 'text/plain',
|
|
238
|
+
createdBy: EMULATED_USER.id,
|
|
239
|
+
modifiedBy: EMULATED_USER.id,
|
|
240
|
+
creationTimeStamp: EMULATED_NOW,
|
|
241
|
+
modifiedTimeStamp: EMULATED_NOW,
|
|
242
|
+
parentFolderUri: `/folders/folders/${idFor(path)}`,
|
|
243
|
+
added: EMULATED_NOW,
|
|
244
|
+
uri: `${path}/${name}`.replace(/\/{2,}/g, '/'),
|
|
245
|
+
links: linksFor(`/files/files/${idFor(`${path}/${name}`.replace(/\/{2,}/g, '/'), 'file')}`)
|
|
246
|
+
}))
|
|
247
|
+
];
|
|
248
|
+
const page = limit > 0 ? items.slice(0, limit) : items;
|
|
249
|
+
// the envelope is what the capture recorded, including the paging fields a
|
|
250
|
+
// client reads to decide whether to ask for more
|
|
251
|
+
return {
|
|
252
|
+
status: 200,
|
|
253
|
+
body: {
|
|
254
|
+
accept: 'application/json',
|
|
255
|
+
name: 'members',
|
|
256
|
+
start: 0,
|
|
257
|
+
limit: limit || page.length,
|
|
258
|
+
count: page.length,
|
|
259
|
+
version: 2,
|
|
260
|
+
items: page,
|
|
261
|
+
links: linksFor(`/folders/folders/${idFor(path)}`, true)
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
},
|
|
265
|
+
'POST /folders/folders': async ({ services, request }) => {
|
|
266
|
+
const drive = requireDrive(services);
|
|
267
|
+
const body = (request.body ?? {});
|
|
268
|
+
if (!body.name)
|
|
269
|
+
throw new ApiError(400, 'A folder name is required.');
|
|
270
|
+
// the capture records the parent as a QUERY parameter, with the name in the
|
|
271
|
+
// body: a handler that read the body alone put every folder at the root
|
|
272
|
+
const parentUri = request.query?.parentFolderUri ?? body.parentFolderUri;
|
|
273
|
+
const parent = parentUri
|
|
274
|
+
? await pathForId(drive, String(parentUri).split('/').pop())
|
|
275
|
+
: '/';
|
|
276
|
+
const path = `${parent}/${body.name}`.replace(/\/{2,}/g, '/');
|
|
277
|
+
await drive.mkdir(path);
|
|
278
|
+
return { status: 201, body: await folderBody(drive, path) };
|
|
279
|
+
},
|
|
280
|
+
'PATCH /folders/folders/{}': async ({ services, request, params }) => {
|
|
281
|
+
const drive = requireDrive(services);
|
|
282
|
+
if (params.id === idFor('@myRecycleBin'))
|
|
283
|
+
throw new ApiError(400, 'The recycle bin cannot be moved or renamed.');
|
|
284
|
+
const path = await pathForId(drive, params.id);
|
|
285
|
+
const body = (request.body ?? {});
|
|
286
|
+
// the adapter removes a folder by moving it to the recycle bin, which is the
|
|
287
|
+
// shape the capture recorded for this verb. Both spellings arrive: the bin's
|
|
288
|
+
// id, and the alias the emulator itself answers with
|
|
289
|
+
if (isRecycleBin(body.parentFolderUri)) {
|
|
290
|
+
const removed = await folderBody(drive, path);
|
|
291
|
+
await drive.rmdir(path);
|
|
292
|
+
// the answer describes the folder that was moved, not the drive root -
|
|
293
|
+
// a client reads the id and parent from it
|
|
294
|
+
return {
|
|
295
|
+
status: 200,
|
|
296
|
+
body: { ...removed, parentFolderUri: String(body.parentFolderUri) }
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
// a move and a rename can arrive together, and an empty parent means the
|
|
300
|
+
// root - the same value means the root on create, so reading it as "no
|
|
301
|
+
// change" here was a second meaning for one string
|
|
302
|
+
const name = body.name ?? path.split('/').pop();
|
|
303
|
+
const parent = body.parentFolderUri === undefined
|
|
304
|
+
? path.split('/').slice(0, -1).join('/') || '/'
|
|
305
|
+
: body.parentFolderUri === ''
|
|
306
|
+
? '/'
|
|
307
|
+
: await pathForId(drive, body.parentFolderUri.split('/').pop());
|
|
308
|
+
const target = `${parent}/${name}`.replace(/\/{2,}/g, '/');
|
|
309
|
+
if (target !== path)
|
|
310
|
+
await drive.rename(path, target);
|
|
311
|
+
return { status: 200, body: await folderBody(drive, target) };
|
|
312
|
+
}
|
|
313
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"surface.d.ts","sourceRoot":"","sources":["../../../src/viya/surface.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,MAAM,MAAM,aAAa,GACrB,MAAM,GACN,WAAW,GACX,WAAW,GACX,cAAc,GACd,YAAY,GACZ,cAAc,GACd,eAAe,GACf,WAAW,CAAA;AAEf,MAAM,WAAW,YAAY;IAC3B,2DAA2D;IAC3D,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,EAAE,aAAa,CAAA;IACrB,QAAQ,EAAE,IAAI,GAAG,UAAU,CAAA;IAC3B,wEAAwE;IACxE,MAAM,EAAE,MAAM,CAAA;CACf;AAED,8DAA8D;AAC9D,eAAO,MAAM,uBAAuB,UAAU,CAAA;AAE9C,eAAO,MAAM,OAAO,EAAE,YAAY,
|
|
1
|
+
{"version":3,"file":"surface.d.ts","sourceRoot":"","sources":["../../../src/viya/surface.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,MAAM,MAAM,aAAa,GACrB,MAAM,GACN,WAAW,GACX,WAAW,GACX,cAAc,GACd,YAAY,GACZ,cAAc,GACd,eAAe,GACf,WAAW,CAAA;AAEf,MAAM,WAAW,YAAY;IAC3B,2DAA2D;IAC3D,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,EAAE,aAAa,CAAA;IACrB,QAAQ,EAAE,IAAI,GAAG,UAAU,CAAA;IAC3B,wEAAwE;IACxE,MAAM,EAAE,MAAM,CAAA;CACf;AAED,8DAA8D;AAC9D,eAAO,MAAM,uBAAuB,UAAU,CAAA;AAE9C,eAAO,MAAM,OAAO,EAAE,YAAY,EA0WjC,CAAA"}
|