@monosmith/plugin-sdk 0.0.0-stage → 0.2.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/README.md +90 -2
- package/build-dir/build-dir.algorithm.d.ts +16 -0
- package/build-dir/build-dir.algorithm.js +26 -0
- package/build-dir/index.d.ts +1 -0
- package/build-dir/index.js +1 -0
- package/ci/ci-contribution.algorithm.d.ts +28 -0
- package/ci/ci-contribution.algorithm.js +19 -0
- package/ci/index.d.ts +2 -0
- package/ci/index.js +1 -0
- package/codec/encode-by-schema.algorithm.d.ts +12 -0
- package/codec/encode-by-schema.algorithm.js +105 -0
- package/codec/encode-error.error.d.ts +4 -0
- package/codec/encode-error.error.js +7 -0
- package/codec/encode-json-value.algorithm.d.ts +13 -0
- package/codec/encode-json-value.algorithm.js +51 -0
- package/codec/index.d.ts +6 -0
- package/codec/index.js +5 -0
- package/codec/message-parse.error.d.ts +4 -0
- package/codec/message-parse.error.js +7 -0
- package/codec/method-codec.algorithm.d.ts +8 -0
- package/codec/method-codec.algorithm.js +31 -0
- package/codec/normalize-by-schema.algorithm.d.ts +14 -0
- package/codec/normalize-by-schema.algorithm.js +103 -0
- package/codec/params-error.error.d.ts +4 -0
- package/codec/params-error.error.js +7 -0
- package/codec/schema-lookup.algorithm.d.ts +13 -0
- package/codec/schema-lookup.algorithm.js +56 -0
- package/codec/wire-message.mapper.d.ts +27 -0
- package/codec/wire-message.mapper.js +62 -0
- package/concurrency/concurrency.policy.d.ts +21 -0
- package/concurrency/concurrency.policy.js +30 -0
- package/concurrency/index.d.ts +1 -0
- package/concurrency/index.js +1 -0
- package/conformance/check-generation.use-case.d.ts +12 -0
- package/conformance/check-generation.use-case.js +91 -0
- package/conformance/check-plugin.use-case.d.ts +25 -0
- package/conformance/check-plugin.use-case.js +81 -0
- package/conformance/check-scheduling.use-case.d.ts +11 -0
- package/conformance/check-scheduling.use-case.js +98 -0
- package/conformance/conformance-report.model.d.ts +22 -0
- package/conformance/conformance-report.model.js +23 -0
- package/conformance/find-manifests.algorithm.d.ts +7 -0
- package/conformance/find-manifests.algorithm.js +34 -0
- package/conformance/go-list.mapper.d.ts +4 -0
- package/conformance/go-list.mapper.js +8 -0
- package/conformance/index.d.ts +5 -0
- package/conformance/index.js +3 -0
- package/framing/frame-message.algorithm.d.ts +5 -0
- package/framing/frame-message.algorithm.js +8 -0
- package/framing/frame-reader.store.d.ts +15 -0
- package/framing/frame-reader.store.js +59 -0
- package/framing/framing.error.d.ts +4 -0
- package/framing/framing.error.js +7 -0
- package/framing/index.d.ts +3 -0
- package/framing/index.js +3 -0
- package/host/call-interrupted.error.d.ts +16 -0
- package/host/call-interrupted.error.js +20 -0
- package/host/connect-plugin.use-case.d.ts +26 -0
- package/host/connect-plugin.use-case.js +44 -0
- package/host/host-options.contract.d.ts +34 -0
- package/host/host-options.contract.js +4 -0
- package/host/index.d.ts +9 -0
- package/host/index.js +5 -0
- package/host/plugin-client.client.d.ts +61 -0
- package/host/plugin-client.client.js +297 -0
- package/host/start-plugin.use-case.d.ts +10 -0
- package/host/start-plugin.use-case.js +57 -0
- package/index.d.ts +16 -0
- package/index.js +21 -0
- package/jsondoc/indent-raw-json.algorithm.d.ts +8 -0
- package/jsondoc/indent-raw-json.algorithm.js +60 -0
- package/jsondoc/index.d.ts +2 -0
- package/jsondoc/index.js +2 -0
- package/jsondoc/json-document.error.d.ts +4 -0
- package/jsondoc/json-document.error.js +7 -0
- package/jsondoc/json-document.model.d.ts +38 -0
- package/jsondoc/json-document.model.js +113 -0
- package/jsondoc/scan-json-object.algorithm.d.ts +17 -0
- package/jsondoc/scan-json-object.algorithm.js +132 -0
- package/package.json +34 -4
- package/plugin-refs/index.d.ts +1 -0
- package/plugin-refs/index.js +1 -0
- package/plugin-refs/plugin-ref.policy.d.ts +9 -0
- package/plugin-refs/plugin-ref.policy.js +22 -0
- package/plugin-roles/index.d.ts +1 -0
- package/plugin-roles/index.js +1 -0
- package/plugin-roles/plugin-role.policy.d.ts +16 -0
- package/plugin-roles/plugin-role.policy.js +30 -0
- package/projects/audit-settings.policy.d.ts +5 -0
- package/projects/audit-settings.policy.js +8 -0
- package/projects/index.d.ts +6 -0
- package/projects/index.js +6 -0
- package/projects/manifest-match.algorithm.d.ts +7 -0
- package/projects/manifest-match.algorithm.js +38 -0
- package/projects/project-kind.policy.d.ts +9 -0
- package/projects/project-kind.policy.js +28 -0
- package/projects/publishes.policy.d.ts +7 -0
- package/projects/publishes.policy.js +8 -0
- package/projects/set-toolchain.algorithm.d.ts +7 -0
- package/projects/set-toolchain.algorithm.js +13 -0
- package/projects/stack-plugins.algorithm.d.ts +3 -0
- package/projects/stack-plugins.algorithm.js +4 -0
- package/protocol/index.d.ts +4 -0
- package/protocol/index.js +3 -0
- package/protocol/protocol-schema.contract.d.ts +3 -0
- package/protocol/protocol-schema.contract.js +1976 -0
- package/protocol/protocol-types.contract.d.ts +1057 -0
- package/protocol/protocol-types.contract.js +43 -0
- package/protocol/rpc-error.error.d.ts +9 -0
- package/protocol/rpc-error.error.js +13 -0
- package/protocol/schema-document.contract.d.ts +58 -0
- package/protocol/schema-document.contract.js +1 -0
- package/scaffold/index.d.ts +3 -0
- package/scaffold/index.js +2 -0
- package/scaffold/scaffold-data.mapper.d.ts +15 -0
- package/scaffold/scaffold-data.mapper.js +24 -0
- package/scaffold/scaffold-project.use-case.d.ts +20 -0
- package/scaffold/scaffold-project.use-case.js +13 -0
- package/serve/capabilities-of.algorithm.d.ts +14 -0
- package/serve/capabilities-of.algorithm.js +31 -0
- package/serve/claim-stdout.use-case.d.ts +10 -0
- package/serve/claim-stdout.use-case.js +14 -0
- package/serve/dispatch-request.use-case.d.ts +15 -0
- package/serve/dispatch-request.use-case.js +57 -0
- package/serve/index.d.ts +4 -0
- package/serve/index.js +2 -0
- package/serve/initialize-plugin.use-case.d.ts +9 -0
- package/serve/initialize-plugin.use-case.js +16 -0
- package/serve/plugin-members.config.d.ts +16 -0
- package/serve/plugin-members.config.js +25 -0
- package/serve/plugin.contract.d.ts +80 -0
- package/serve/plugin.contract.js +1 -0
- package/serve/protocol-streams.contract.d.ts +13 -0
- package/serve/protocol-streams.contract.js +1 -0
- package/serve/request-outcome.contract.d.ts +9 -0
- package/serve/request-outcome.contract.js +1 -0
- package/serve/request-pool.store.d.ts +49 -0
- package/serve/request-pool.store.js +110 -0
- package/serve/serve.use-case.d.ts +31 -0
- package/serve/serve.use-case.js +180 -0
- package/tasks/contribute-tasks.algorithm.d.ts +15 -0
- package/tasks/contribute-tasks.algorithm.js +22 -0
- package/tasks/index.d.ts +2 -0
- package/tasks/index.js +2 -0
- package/tasks/task.algorithm.d.ts +18 -0
- package/tasks/task.algorithm.js +23 -0
- package/templating/file-exists.error.d.ts +6 -0
- package/templating/file-exists.error.js +10 -0
- package/templating/index.d.ts +4 -0
- package/templating/index.js +4 -0
- package/templating/parse-template.algorithm.d.ts +11 -0
- package/templating/parse-template.algorithm.js +197 -0
- package/templating/render-template.algorithm.d.ts +7 -0
- package/templating/render-template.algorithm.js +87 -0
- package/templating/template-functions.algorithm.d.ts +10 -0
- package/templating/template-functions.algorithm.js +104 -0
- package/templating/template-syntax.contract.d.ts +33 -0
- package/templating/template-syntax.contract.js +1 -0
- package/templating/template.error.d.ts +4 -0
- package/templating/template.error.js +7 -0
- package/templating/tokenize-action.algorithm.d.ts +20 -0
- package/templating/tokenize-action.algorithm.js +57 -0
- package/templating/write-templates.use-case.d.ts +11 -0
- package/templating/write-templates.use-case.js +44 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/** The requests waiting for a worker are limited to this many; the host never has more in flight than the plugin declared, so more means a host that breaks the protocol. */
|
|
2
|
+
export const MAX_QUEUED = 1024;
|
|
3
|
+
/**
|
|
4
|
+
* The state of the serve loop: the requests in flight, the ones waiting for a
|
|
5
|
+
* worker, and a bounded number of workers running them. Requests start in the
|
|
6
|
+
* order they arrive and may finish in any order. The Go SDK's request pool,
|
|
7
|
+
* with the same rules.
|
|
8
|
+
*/
|
|
9
|
+
export class RequestPool {
|
|
10
|
+
#limit = 1;
|
|
11
|
+
#running = 0;
|
|
12
|
+
#queue = [];
|
|
13
|
+
#inflight = new Map();
|
|
14
|
+
#idle = [];
|
|
15
|
+
#run;
|
|
16
|
+
#failure;
|
|
17
|
+
/** `run` answers one request; if it throws, the first error is kept in {@link failure} and the pool goes on. */
|
|
18
|
+
constructor(run) {
|
|
19
|
+
this.#run = run;
|
|
20
|
+
}
|
|
21
|
+
#start(first) {
|
|
22
|
+
this.#running++;
|
|
23
|
+
void this.#work(first);
|
|
24
|
+
}
|
|
25
|
+
async #work(first) {
|
|
26
|
+
for (let pooled = first; pooled !== undefined; pooled = this.#next()) {
|
|
27
|
+
try {
|
|
28
|
+
await this.#run(pooled);
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
this.#failure ??= error;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
#next() {
|
|
36
|
+
if (this.#queue.length > 0 && this.#running <= this.#limit)
|
|
37
|
+
return this.#queue.shift();
|
|
38
|
+
this.#running--;
|
|
39
|
+
if (this.#running === 0)
|
|
40
|
+
for (const resolve of this.#idle.splice(0))
|
|
41
|
+
resolve();
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
/** The first error a run threw, if any. */
|
|
45
|
+
get failure() {
|
|
46
|
+
return this.#failure;
|
|
47
|
+
}
|
|
48
|
+
/** How many workers may run at once. Raising it starts waiting requests; lowering it lets running ones finish. */
|
|
49
|
+
setLimit(limit) {
|
|
50
|
+
this.#limit = Math.max(1, limit);
|
|
51
|
+
while (this.#running < this.#limit && this.#queue.length > 0)
|
|
52
|
+
this.#start(this.#queue.shift());
|
|
53
|
+
}
|
|
54
|
+
/** Takes a request: it runs at once on a free worker, or waits its turn. */
|
|
55
|
+
submit(request) {
|
|
56
|
+
if (this.#inflight.has(request.id))
|
|
57
|
+
return 'duplicate';
|
|
58
|
+
if (this.#running >= this.#limit && this.#queue.length >= MAX_QUEUED)
|
|
59
|
+
return 'full';
|
|
60
|
+
const pooled = { request, controller: new AbortController(), cancelled: false };
|
|
61
|
+
this.#inflight.set(request.id, pooled);
|
|
62
|
+
if (this.#running >= this.#limit)
|
|
63
|
+
this.#queue.push(pooled);
|
|
64
|
+
else
|
|
65
|
+
this.#start(pooled);
|
|
66
|
+
return 'admitted';
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Forgets an answered request; call it before the answer is written, so a
|
|
70
|
+
* host that reuses the id right away is not refused as a duplicate.
|
|
71
|
+
*/
|
|
72
|
+
release(pooled) {
|
|
73
|
+
this.#inflight.delete(pooled.request.id);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Stops the request with an id. One that had not started is taken out of
|
|
77
|
+
* the queue and returned, because no worker will ever answer it. An unknown
|
|
78
|
+
* id is ignored.
|
|
79
|
+
*/
|
|
80
|
+
cancel(id) {
|
|
81
|
+
const pooled = this.#inflight.get(id);
|
|
82
|
+
if (pooled === undefined)
|
|
83
|
+
return undefined;
|
|
84
|
+
pooled.cancelled = true;
|
|
85
|
+
pooled.controller.abort();
|
|
86
|
+
const at = this.#queue.indexOf(pooled);
|
|
87
|
+
if (at === -1)
|
|
88
|
+
return undefined;
|
|
89
|
+
this.#queue.splice(at, 1);
|
|
90
|
+
this.#inflight.delete(id);
|
|
91
|
+
return pooled;
|
|
92
|
+
}
|
|
93
|
+
/** Stops every request. The ones that had not started are returned, to be answered. */
|
|
94
|
+
cancelAll() {
|
|
95
|
+
for (const pooled of this.#inflight.values()) {
|
|
96
|
+
pooled.cancelled = true;
|
|
97
|
+
pooled.controller.abort();
|
|
98
|
+
}
|
|
99
|
+
const waiting = this.#queue.splice(0);
|
|
100
|
+
for (const pooled of waiting)
|
|
101
|
+
this.#inflight.delete(pooled.request.id);
|
|
102
|
+
return waiting;
|
|
103
|
+
}
|
|
104
|
+
/** Resolves once no worker is running. */
|
|
105
|
+
idle() {
|
|
106
|
+
if (this.#running === 0)
|
|
107
|
+
return Promise.resolve();
|
|
108
|
+
return new Promise((resolve) => { this.#idle.push(resolve); });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { SmithPlugin } from './plugin.contract.ts';
|
|
2
|
+
import type { ServeStreams } from './protocol-streams.contract.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Runs a plugin on the process's standard input and output, and exits when the
|
|
5
|
+
* host sends `shutdown` or closes the stream. Use it as the whole body of the
|
|
6
|
+
* plugin's entry point:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* await serve({ describe: () => ({ ... }), discover: (params) => ({ ... }) })
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* Standard output carries only the protocol's frames: whatever else the plugin
|
|
13
|
+
* or a library writes there goes to standard error instead. A failure of the
|
|
14
|
+
* stream exits with status 1 after the reason is printed.
|
|
15
|
+
*/
|
|
16
|
+
export declare function serve(plugin: SmithPlugin): Promise<never>;
|
|
17
|
+
/**
|
|
18
|
+
* Answers requests from `input` on `output` until the host sends `shutdown`
|
|
19
|
+
* or the input ends, then resolves. This is the loop {@link serve} runs, on
|
|
20
|
+
* streams you choose: a test serves a plugin in-process over pipes with it.
|
|
21
|
+
*
|
|
22
|
+
* Semantics of the Go SDK's `serve.Serve`: requests are answered by a bounded
|
|
23
|
+
* pool of as many at a time as the plugin declared in `concurrency` (one, in
|
|
24
|
+
* order, when it said nothing), and the answers may be written in any order;
|
|
25
|
+
* a request before `initialize` is refused (InvalidRequest); a notification
|
|
26
|
+
* (no `id`) gets no answer; an unreadable frame body is answered with
|
|
27
|
+
* ParseError and the loop goes on; an unreadable header ends it with an error.
|
|
28
|
+
* A `cancellable` plugin has the signal of a request aborted when the host
|
|
29
|
+
* sends `cancel` for it, and at shutdown or the end of the input.
|
|
30
|
+
*/
|
|
31
|
+
export declare function serveStreams(plugin: SmithPlugin, streams: ServeStreams): Promise<void>;
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { MessageParseError, ParamsError, decodeParams, parseRequest, writeFailure, writeResult } from '../codec/index.js';
|
|
2
|
+
import { workerLimit } from '../concurrency/index.js';
|
|
3
|
+
import { FrameReader, frameMessage } from '../framing/index.js';
|
|
4
|
+
import { ERROR_CODES } from '../protocol/index.js';
|
|
5
|
+
import { halfImplemented } from './capabilities-of.algorithm.js';
|
|
6
|
+
import { claimStdout } from './claim-stdout.use-case.js';
|
|
7
|
+
import { dispatchRequest } from './dispatch-request.use-case.js';
|
|
8
|
+
import { RequestPool } from './request-pool.store.js';
|
|
9
|
+
/**
|
|
10
|
+
* Runs a plugin on the process's standard input and output, and exits when the
|
|
11
|
+
* host sends `shutdown` or closes the stream. Use it as the whole body of the
|
|
12
|
+
* plugin's entry point:
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* await serve({ describe: () => ({ ... }), discover: (params) => ({ ... }) })
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* Standard output carries only the protocol's frames: whatever else the plugin
|
|
19
|
+
* or a library writes there goes to standard error instead. A failure of the
|
|
20
|
+
* stream exits with status 1 after the reason is printed.
|
|
21
|
+
*/
|
|
22
|
+
export async function serve(plugin) {
|
|
23
|
+
const output = claimStdout();
|
|
24
|
+
try {
|
|
25
|
+
await serveStreams(plugin, { input: process.stdin, output });
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
29
|
+
// serve is the plugin's main, like the Go SDK's serve.Main: it ends the process, whatever handles the plugin left open.
|
|
30
|
+
// eslint-disable-next-line unicorn/no-process-exit
|
|
31
|
+
process.exit(1);
|
|
32
|
+
}
|
|
33
|
+
// eslint-disable-next-line unicorn/no-process-exit
|
|
34
|
+
process.exit(0);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Answers requests from `input` on `output` until the host sends `shutdown`
|
|
38
|
+
* or the input ends, then resolves. This is the loop {@link serve} runs, on
|
|
39
|
+
* streams you choose: a test serves a plugin in-process over pipes with it.
|
|
40
|
+
*
|
|
41
|
+
* Semantics of the Go SDK's `serve.Serve`: requests are answered by a bounded
|
|
42
|
+
* pool of as many at a time as the plugin declared in `concurrency` (one, in
|
|
43
|
+
* order, when it said nothing), and the answers may be written in any order;
|
|
44
|
+
* a request before `initialize` is refused (InvalidRequest); a notification
|
|
45
|
+
* (no `id`) gets no answer; an unreadable frame body is answered with
|
|
46
|
+
* ParseError and the loop goes on; an unreadable header ends it with an error.
|
|
47
|
+
* A `cancellable` plugin has the signal of a request aborted when the host
|
|
48
|
+
* sends `cancel` for it, and at shutdown or the end of the input.
|
|
49
|
+
*/
|
|
50
|
+
export async function serveStreams(plugin, streams) {
|
|
51
|
+
const log = streams.log ?? ((message) => process.stderr.write(`${message}\n`));
|
|
52
|
+
for (const member of halfImplemented(plugin)) {
|
|
53
|
+
log(`plugin: ${member} needs its pair (install with uninstall, declared with rewrite) to be advertised; it will not be called`);
|
|
54
|
+
}
|
|
55
|
+
const reader = new FrameReader();
|
|
56
|
+
const pool = new RequestPool(async (pooled) => { await run(session, pooled); });
|
|
57
|
+
const session = { initialized: false, plugin, output: streams.output, pool, cancellable: plugin.cancellable === true };
|
|
58
|
+
try {
|
|
59
|
+
for await (const chunk of streams.input) {
|
|
60
|
+
for (const body of reader.push(chunk)) {
|
|
61
|
+
if (pool.failure !== undefined)
|
|
62
|
+
throw pool.failure;
|
|
63
|
+
const shutdown = await receive(session, body);
|
|
64
|
+
if (shutdown)
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
finally {
|
|
70
|
+
await drain(session);
|
|
71
|
+
}
|
|
72
|
+
if (pool.failure !== undefined)
|
|
73
|
+
throw pool.failure;
|
|
74
|
+
}
|
|
75
|
+
/** Handles one frame. Returns whether it was `shutdown`, after which the loop ends. */
|
|
76
|
+
async function receive(session, body) {
|
|
77
|
+
let request;
|
|
78
|
+
try {
|
|
79
|
+
request = parseRequest(body);
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
if (!(error instanceof MessageParseError))
|
|
83
|
+
throw error;
|
|
84
|
+
await send(session.output, writeFailure(null, ERROR_CODES.ParseError, error.message));
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
if (request.id === null) {
|
|
88
|
+
await notified(session, request);
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
const id = request.id;
|
|
92
|
+
if (!session.initialized && request.method !== 'initialize') {
|
|
93
|
+
await send(session.output, writeFailure(id, ERROR_CODES.InvalidRequest, 'initialize must come first'));
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
if (request.method === 'initialize') {
|
|
97
|
+
const outcome = await dispatchRequest(session.plugin, request);
|
|
98
|
+
if (outcome.ok) {
|
|
99
|
+
session.initialized = true;
|
|
100
|
+
session.pool.setLimit(workerLimit(JSON.parse(outcome.resultJson).concurrency));
|
|
101
|
+
}
|
|
102
|
+
await send(session.output, outcome.ok ? writeResult(id, outcome.resultJson) : writeFailure(id, outcome.code, outcome.message));
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
if (request.method === 'shutdown') {
|
|
106
|
+
// Everything read before shutdown, started or still waiting, is answered with its result first, as in the Go SDK; only a cancel message stops a request.
|
|
107
|
+
await session.pool.idle();
|
|
108
|
+
await send(session.output, writeResult(id, '{}'));
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
switch (session.pool.submit({ ...request, id })) {
|
|
112
|
+
case 'duplicate': {
|
|
113
|
+
await send(session.output, writeFailure(id, ERROR_CODES.InvalidRequest, `request id ${id} is already in flight`));
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
case 'full': {
|
|
117
|
+
await send(session.output, writeFailure(id, ERROR_CODES.InvalidRequest, 'too many requests in flight: the host must not send more than the plugin\'s declared concurrency'));
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
case 'admitted': {
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Handles a message without an `id`. `cancel` is the only one the protocol
|
|
128
|
+
* defines for a plugin; every other is ignored, and so is a `cancel` to a
|
|
129
|
+
* plugin that is not `cancellable`, or one that names no request.
|
|
130
|
+
*/
|
|
131
|
+
async function notified(session, request) {
|
|
132
|
+
if (request.method !== 'cancel' || !session.cancellable)
|
|
133
|
+
return;
|
|
134
|
+
let params;
|
|
135
|
+
try {
|
|
136
|
+
params = decodeParams('cancel', request.params);
|
|
137
|
+
}
|
|
138
|
+
catch (error) {
|
|
139
|
+
if (error instanceof ParamsError)
|
|
140
|
+
return;
|
|
141
|
+
throw error;
|
|
142
|
+
}
|
|
143
|
+
const waiting = session.pool.cancel(params.id);
|
|
144
|
+
if (waiting !== undefined)
|
|
145
|
+
await answerCancelled(session.output, waiting);
|
|
146
|
+
}
|
|
147
|
+
/** A worker's job: handle one request and answer it. */
|
|
148
|
+
async function run(session, pooled) {
|
|
149
|
+
const outcome = await dispatchRequest(session.plugin, pooled.request, pooled.controller.signal);
|
|
150
|
+
const answer = !outcome.ok && outcome.code === ERROR_CODES.PluginFailure && pooled.cancelled
|
|
151
|
+
? { ok: false, code: ERROR_CODES.RequestCancelled, message: `request cancelled: ${outcome.message}` }
|
|
152
|
+
: outcome;
|
|
153
|
+
session.pool.release(pooled);
|
|
154
|
+
await send(session.output, answer.ok ? writeResult(pooled.request.id, answer.resultJson) : writeFailure(pooled.request.id, answer.code, answer.message));
|
|
155
|
+
}
|
|
156
|
+
function answerCancelled(output, pooled) {
|
|
157
|
+
return send(output, writeFailure(pooled.request.id, ERROR_CODES.RequestCancelled, 'request cancelled before it started'));
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Ends the work in flight when the loop returns (the input ended, the host is gone): a
|
|
161
|
+
* cancellable plugin is asked to stop and what had not started is refused;
|
|
162
|
+
* any other finishes what it is doing.
|
|
163
|
+
*/
|
|
164
|
+
async function drain(session) {
|
|
165
|
+
if (session.cancellable) {
|
|
166
|
+
for (const waiting of session.pool.cancelAll())
|
|
167
|
+
await answerCancelled(session.output, waiting);
|
|
168
|
+
}
|
|
169
|
+
await session.pool.idle();
|
|
170
|
+
}
|
|
171
|
+
function send(output, body) {
|
|
172
|
+
return new Promise((resolve, reject) => {
|
|
173
|
+
output.write(frameMessage(body), (error) => {
|
|
174
|
+
if (error === null || error === undefined)
|
|
175
|
+
resolve();
|
|
176
|
+
else
|
|
177
|
+
reject(error);
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ContributeResult, Contribution, Project } from '../protocol/index.ts';
|
|
2
|
+
/**
|
|
3
|
+
* The answer to `contribute` when every project gets the same kind of
|
|
4
|
+
* addition: calls `contribution` for each project and keys the result by
|
|
5
|
+
* project name. Return `undefined` for a project the plugin has nothing to add
|
|
6
|
+
* to; it is left out.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* contribute: ({ projects }) => contributeTo(projects, () => ({
|
|
10
|
+
* tasks: { 'layout-check': task(['node', checker, 'layout-check'], { cache: true }) },
|
|
11
|
+
* dependsOn: { lint: ['layout-check'] },
|
|
12
|
+
* }))
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare function contributeTo(projects: readonly Project[], contribution: (project: Project) => Contribution | undefined): ContributeResult;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The answer to `contribute` when every project gets the same kind of
|
|
3
|
+
* addition: calls `contribution` for each project and keys the result by
|
|
4
|
+
* project name. Return `undefined` for a project the plugin has nothing to add
|
|
5
|
+
* to; it is left out.
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* contribute: ({ projects }) => contributeTo(projects, () => ({
|
|
9
|
+
* tasks: { 'layout-check': task(['node', checker, 'layout-check'], { cache: true }) },
|
|
10
|
+
* dependsOn: { lint: ['layout-check'] },
|
|
11
|
+
* }))
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export function contributeTo(projects, contribution) {
|
|
15
|
+
const result = {};
|
|
16
|
+
for (const project of projects) {
|
|
17
|
+
const added = contribution(project);
|
|
18
|
+
if (added !== undefined)
|
|
19
|
+
result[project.name] = added;
|
|
20
|
+
}
|
|
21
|
+
return { projects: result };
|
|
22
|
+
}
|
package/tasks/index.d.ts
ADDED
package/tasks/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Task } from '../protocol/index.ts';
|
|
2
|
+
/**
|
|
3
|
+
* A task the host can run, hash and cache. The command is an argv array and is
|
|
4
|
+
* never passed through a shell. Other members are the options.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* task(['node', 'check.js'], { inputs: ['widget.json'], cache: true, description: 'check the manifest' })
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
export declare function task(command: string[], options?: Omit<Task, 'command'>): Task;
|
|
11
|
+
/**
|
|
12
|
+
* The `build` task of a project: cached, built into the project's directory
|
|
13
|
+
* under `dist/` (which is also its `outputs`, restored on a cache hit), and run
|
|
14
|
+
* after the build of every project this one imports (`^build`). `outputDir` is
|
|
15
|
+
* the directory the command is told to write into, `../../dist/libs/core` for
|
|
16
|
+
* `libs/core`, to pass in its argv.
|
|
17
|
+
*/
|
|
18
|
+
export declare function buildTask(root: string, command: (outputDir: string) => string[], options?: Omit<Task, 'command'>): Task;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { buildDirFromProject } from '../build-dir/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* A task the host can run, hash and cache. The command is an argv array and is
|
|
4
|
+
* never passed through a shell. Other members are the options.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* task(['node', 'check.js'], { inputs: ['widget.json'], cache: true, description: 'check the manifest' })
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
export function task(command, options = {}) {
|
|
11
|
+
return { command, ...options };
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The `build` task of a project: cached, built into the project's directory
|
|
15
|
+
* under `dist/` (which is also its `outputs`, restored on a cache hit), and run
|
|
16
|
+
* after the build of every project this one imports (`^build`). `outputDir` is
|
|
17
|
+
* the directory the command is told to write into, `../../dist/libs/core` for
|
|
18
|
+
* `libs/core`, to pass in its argv.
|
|
19
|
+
*/
|
|
20
|
+
export function buildTask(root, command, options = {}) {
|
|
21
|
+
const outputDir = buildDirFromProject(root);
|
|
22
|
+
return { command: command(outputDir), outputs: [outputDir], dependsOn: ['^build'], cache: true, ...options };
|
|
23
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** A target file already exists: templates are written for a new project and never overwrite what is there. */
|
|
2
|
+
export declare class FileExistsError extends Error {
|
|
3
|
+
/** The existing file, relative to the root. */
|
|
4
|
+
readonly path: string;
|
|
5
|
+
constructor(path: string);
|
|
6
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** A target file already exists: templates are written for a new project and never overwrite what is there. */
|
|
2
|
+
export class FileExistsError extends Error {
|
|
3
|
+
/** The existing file, relative to the root. */
|
|
4
|
+
path;
|
|
5
|
+
constructor(path) {
|
|
6
|
+
super(`${path}: file already exists`);
|
|
7
|
+
this.name = 'FileExistsError';
|
|
8
|
+
this.path = path;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { TemplateNode } from './template-syntax.contract.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Parses a template with `[[ ]]` delimiters. The language is the subset of Go's
|
|
4
|
+
* `text/template` that project templates use: `[[.Field]]` and `[[.A.B]]`,
|
|
5
|
+
* `[[if]]`, `[[else if]]`, `[[else]]`, `[[with]]`, `[[range]]` and `[[end]]`, the
|
|
6
|
+
* functions `eq ne lt le gt ge not and or len index print printf`, string,
|
|
7
|
+
* number and boolean literals, parentheses, pipes, comments, and the `-`
|
|
8
|
+
* trim markers (`[[- x -]]`). Variables, `define`, `template` and `block`
|
|
9
|
+
* are not supported and are reported as such.
|
|
10
|
+
*/
|
|
11
|
+
export declare function parseTemplate(text: string, name: string): TemplateNode[];
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { tokenizeAction } from './tokenize-action.algorithm.js';
|
|
2
|
+
import { TemplateError } from './template.error.js';
|
|
3
|
+
const OPEN = '[[';
|
|
4
|
+
const CLOSE = ']]';
|
|
5
|
+
const UNSUPPORTED = new Set(['define', 'template', 'block', 'break', 'continue']);
|
|
6
|
+
/**
|
|
7
|
+
* Parses a template with `[[ ]]` delimiters. The language is the subset of Go's
|
|
8
|
+
* `text/template` that project templates use: `[[.Field]]` and `[[.A.B]]`,
|
|
9
|
+
* `[[if]]`, `[[else if]]`, `[[else]]`, `[[with]]`, `[[range]]` and `[[end]]`, the
|
|
10
|
+
* functions `eq ne lt le gt ge not and or len index print printf`, string,
|
|
11
|
+
* number and boolean literals, parentheses, pipes, comments, and the `-`
|
|
12
|
+
* trim markers (`[[- x -]]`). Variables, `define`, `template` and `block`
|
|
13
|
+
* are not supported and are reported as such.
|
|
14
|
+
*/
|
|
15
|
+
export function parseTemplate(text, name) {
|
|
16
|
+
const fail = (message) => {
|
|
17
|
+
throw new TemplateError(name, message);
|
|
18
|
+
};
|
|
19
|
+
const root = [];
|
|
20
|
+
const stack = [];
|
|
21
|
+
const target = () => {
|
|
22
|
+
const top = stack.at(-1);
|
|
23
|
+
if (top === undefined)
|
|
24
|
+
return root;
|
|
25
|
+
return top.inElse ? top.otherwise : top.body;
|
|
26
|
+
};
|
|
27
|
+
for (const piece of splitPieces(text, fail)) {
|
|
28
|
+
if ('literal' in piece) {
|
|
29
|
+
if (piece.literal !== '')
|
|
30
|
+
target().push({ type: 'text', text: piece.literal });
|
|
31
|
+
}
|
|
32
|
+
else if (piece.action.startsWith('/*')) {
|
|
33
|
+
if (!piece.action.endsWith('*/'))
|
|
34
|
+
fail('unclosed comment');
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
applyAction(piece.action, stack, target, fail);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const open = stack.at(-1);
|
|
41
|
+
if (open !== undefined)
|
|
42
|
+
fail(`unclosed ${open.type}: missing [[end]]`);
|
|
43
|
+
return root;
|
|
44
|
+
}
|
|
45
|
+
/** The literal text and the actions of a template, in order, with the `-` trim markers applied. */
|
|
46
|
+
function splitPieces(text, fail) {
|
|
47
|
+
const pieces = [];
|
|
48
|
+
let position = 0;
|
|
49
|
+
let trimNext = false;
|
|
50
|
+
while (position < text.length) {
|
|
51
|
+
const open = text.indexOf(OPEN, position);
|
|
52
|
+
let literal = text.slice(position, open === -1 ? text.length : open);
|
|
53
|
+
if (trimNext)
|
|
54
|
+
literal = literal.trimStart();
|
|
55
|
+
trimNext = false;
|
|
56
|
+
if (open === -1) {
|
|
57
|
+
pieces.push({ literal });
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
const close = findClose(text, open + OPEN.length);
|
|
61
|
+
if (close === -1)
|
|
62
|
+
fail(`unclosed action at offset ${open}`);
|
|
63
|
+
let action = text.slice(open + OPEN.length, close);
|
|
64
|
+
if (/^-\s/.test(action)) {
|
|
65
|
+
literal = literal.trimEnd();
|
|
66
|
+
action = action.slice(1);
|
|
67
|
+
}
|
|
68
|
+
if (/\s-$/.test(action)) {
|
|
69
|
+
trimNext = true;
|
|
70
|
+
action = action.slice(0, -1);
|
|
71
|
+
}
|
|
72
|
+
pieces.push({ literal }, { action: action.trim() });
|
|
73
|
+
position = close + CLOSE.length;
|
|
74
|
+
}
|
|
75
|
+
return pieces;
|
|
76
|
+
}
|
|
77
|
+
/** The offset of the `]]` that ends the action, skipping string literals. */
|
|
78
|
+
function findClose(text, from) {
|
|
79
|
+
for (let i = from; i < text.length; i++) {
|
|
80
|
+
const c = text[i];
|
|
81
|
+
if (c === '"') {
|
|
82
|
+
i++;
|
|
83
|
+
while (i < text.length && text[i] !== '"')
|
|
84
|
+
i += text[i] === '\\' ? 2 : 1;
|
|
85
|
+
}
|
|
86
|
+
else if (c === '`') {
|
|
87
|
+
i = text.indexOf('`', i + 1);
|
|
88
|
+
if (i === -1)
|
|
89
|
+
return -1;
|
|
90
|
+
}
|
|
91
|
+
else if (text.startsWith(CLOSE, i)) {
|
|
92
|
+
return i;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return -1;
|
|
96
|
+
}
|
|
97
|
+
function applyAction(action, stack, target, fail) {
|
|
98
|
+
const tokens = tokenizeAction(action, fail);
|
|
99
|
+
const first = tokens[0];
|
|
100
|
+
const keyword = first?.t === 'word' ? first.value : '';
|
|
101
|
+
switch (keyword) {
|
|
102
|
+
case 'if':
|
|
103
|
+
case 'with':
|
|
104
|
+
case 'range': {
|
|
105
|
+
stack.push({ type: keyword, pipeline: parsePipeline(tokens.slice(1), fail), body: [], otherwise: [], inElse: false, chained: false });
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
case 'else': {
|
|
109
|
+
startElse(tokens, stack, fail);
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
case 'end': {
|
|
113
|
+
if (tokens.length > 1)
|
|
114
|
+
fail('unexpected tokens after end');
|
|
115
|
+
closeFrames(stack, target, fail);
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
default: {
|
|
119
|
+
if (UNSUPPORTED.has(keyword)) {
|
|
120
|
+
fail(`"${keyword}" is not supported by the SDK's templates (the supported subset: fields, if, else, with, range, end, and the functions eq ne lt le gt ge not and or len index print printf)`);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
if (tokens.length === 0)
|
|
124
|
+
fail('empty action');
|
|
125
|
+
target().push({ type: 'action', pipeline: parsePipeline(tokens, fail) });
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
function startElse(tokens, stack, fail) {
|
|
131
|
+
const top = stack.at(-1) ?? fail('unexpected else');
|
|
132
|
+
if (top.inElse)
|
|
133
|
+
fail('multiple else in one block');
|
|
134
|
+
top.inElse = true;
|
|
135
|
+
const next = tokens[1];
|
|
136
|
+
if (next?.t === 'word' && next.value === 'if') {
|
|
137
|
+
stack.push({ type: 'if', pipeline: parsePipeline(tokens.slice(2), fail), body: [], otherwise: [], inElse: false, chained: true });
|
|
138
|
+
}
|
|
139
|
+
else if (tokens.length > 1) {
|
|
140
|
+
fail('unexpected tokens after else');
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/** Ends the innermost block, and the `else if` blocks chained to it. */
|
|
144
|
+
function closeFrames(stack, target, fail) {
|
|
145
|
+
let frame;
|
|
146
|
+
do {
|
|
147
|
+
frame = stack.pop() ?? fail('unexpected end');
|
|
148
|
+
target().push({ type: frame.type, pipeline: frame.pipeline, body: frame.body, otherwise: frame.otherwise });
|
|
149
|
+
} while (frame.chained);
|
|
150
|
+
}
|
|
151
|
+
function parsePipeline(tokens, fail) {
|
|
152
|
+
const [pipeline, used] = pipelineFrom(tokens, 0, fail);
|
|
153
|
+
if (used < tokens.length)
|
|
154
|
+
fail('unexpected ")" in action');
|
|
155
|
+
return pipeline;
|
|
156
|
+
}
|
|
157
|
+
function pipelineFrom(tokens, start, fail) {
|
|
158
|
+
const pipeline = [];
|
|
159
|
+
let command = [];
|
|
160
|
+
let i = start;
|
|
161
|
+
for (; i < tokens.length; i++) {
|
|
162
|
+
const token = tokens[i];
|
|
163
|
+
if (token.t === 'close')
|
|
164
|
+
break;
|
|
165
|
+
if (token.t === 'pipe') {
|
|
166
|
+
if (command.length === 0)
|
|
167
|
+
fail('missing value before |');
|
|
168
|
+
pipeline.push(command);
|
|
169
|
+
command = [];
|
|
170
|
+
}
|
|
171
|
+
else if (token.t === 'open') {
|
|
172
|
+
const [inner, next] = pipelineFrom(tokens, i + 1, fail);
|
|
173
|
+
if (tokens[next]?.t !== 'close')
|
|
174
|
+
fail('unclosed (');
|
|
175
|
+
command.push({ kind: 'pipeline', pipeline: inner });
|
|
176
|
+
i = next;
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
command.push(operandOf(token));
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (command.length === 0)
|
|
183
|
+
fail(pipeline.length > 0 ? 'missing command after |' : 'missing value for the action');
|
|
184
|
+
pipeline.push(command);
|
|
185
|
+
return [pipeline, i];
|
|
186
|
+
}
|
|
187
|
+
function operandOf(token) {
|
|
188
|
+
if (token.t === 'field')
|
|
189
|
+
return { kind: 'field', root: token.root, path: token.path };
|
|
190
|
+
if (token.t === 'string' || token.t === 'number')
|
|
191
|
+
return { kind: token.t, value: token.value };
|
|
192
|
+
if (token.t !== 'word')
|
|
193
|
+
throw new Error('unreachable');
|
|
194
|
+
if (token.value === 'true' || token.value === 'false')
|
|
195
|
+
return { kind: 'bool', value: token.value === 'true' };
|
|
196
|
+
return token.value === 'nil' ? { kind: 'nil' } : { kind: 'name', name: token.value };
|
|
197
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renders a `[[ ]]` template with the data, the way the Go SDK's `templating`
|
|
3
|
+
* does with `text/template` and `missingkey=error`: a field the data does not
|
|
4
|
+
* have is an error, not an empty string. See {@link parseTemplate} for the
|
|
5
|
+
* language. `name` is only used in error messages.
|
|
6
|
+
*/
|
|
7
|
+
export declare function renderTemplate(text: string, data: unknown, name?: string): string;
|