@dsh-workbench/adapter-runtime 1.3.2 → 1.4.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/channel/opencli-plugin.json +2 -2
- package/channel/package.json +2 -2
- package/dist/browser/browser-find.js +6 -5
- package/dist/browser/browser-read.js +4 -3
- package/dist/cli/index.d.ts +3 -0
- package/dist/cli/index.js +3 -0
- package/dist/cli/json-file.d.ts +2 -0
- package/dist/cli/json-file.js +17 -0
- package/dist/cli/opencli-report.d.ts +19 -0
- package/dist/cli/opencli-report.js +85 -0
- package/dist/cli/opencli-runtime.d.ts +1 -0
- package/dist/cli/opencli-runtime.js +10 -7
- package/dist/cli/opencli-session.d.ts +4 -0
- package/dist/cli/opencli-session.js +24 -0
- package/dist/cli/opencli-window.d.ts +7 -1
- package/dist/cli/opencli-window.js +10 -2
- package/dist/cli/opencli-wire.d.ts +13 -0
- package/dist/cli/opencli-wire.js +206 -0
- package/dist/cli/opencli.js +18 -12
- package/dist/cli/plugin-peers.d.ts +5 -0
- package/dist/cli/plugin-peers.js +45 -0
- package/dist/data/channel-plugin.d.ts +2 -0
- package/dist/data/channel-plugin.js +10 -3
- package/dist/data/channel.js +2 -2
- package/dist/data/index.js +32 -14
- package/dist/data/session-pool.d.ts +11 -0
- package/dist/data/session-pool.js +60 -0
- package/dist/data/session-registry.d.ts +9 -0
- package/dist/data/session-registry.js +66 -0
- package/dist/data/sessions.d.ts +13 -0
- package/dist/data/sessions.js +69 -0
- package/dist/data/types.d.ts +5 -0
- package/dist/data/types.js +29 -1
- package/dist/foundation/paths.d.ts +2 -0
- package/dist/foundation/paths.js +9 -1
- package/dist/libs/links.d.ts +3 -0
- package/dist/libs/links.js +24 -4
- package/package.json +3 -2
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { existsSync, readdirSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { checkVersionRange } from '@dsh-workbench/shared/host';
|
|
4
|
+
import { OPENCLI_NPM_PACKAGE, vendorPluginsDir } from '../foundation/paths.js';
|
|
5
|
+
import { jsonAt as jsonOf } from './json-file.js';
|
|
6
|
+
function packageDirs(root) {
|
|
7
|
+
if (!existsSync(root))
|
|
8
|
+
return [];
|
|
9
|
+
const found = [];
|
|
10
|
+
for (const entry of readdirSync(root, { withFileTypes: true })) {
|
|
11
|
+
if (!entry.isDirectory())
|
|
12
|
+
continue;
|
|
13
|
+
if (!entry.name.startsWith('@')) {
|
|
14
|
+
found.push(join(root, entry.name));
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
const scoped = join(root, entry.name);
|
|
18
|
+
for (const inner of readdirSync(scoped, { withFileTypes: true })) {
|
|
19
|
+
if (inner.isDirectory())
|
|
20
|
+
found.push(join(scoped, inner.name));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return found;
|
|
24
|
+
}
|
|
25
|
+
export function pluginPeerProblems(pin) {
|
|
26
|
+
const found = [];
|
|
27
|
+
for (const dir of packageDirs(vendorPluginsDir())) {
|
|
28
|
+
const manifest = jsonOf(join(dir, 'package.json'));
|
|
29
|
+
const name = manifest?.['name'];
|
|
30
|
+
if (typeof name !== 'string' || name.trim() === '')
|
|
31
|
+
continue;
|
|
32
|
+
const peers = manifest?.['peerDependencies'];
|
|
33
|
+
const declared = typeof peers === 'object' && peers !== null
|
|
34
|
+
? peers[OPENCLI_NPM_PACKAGE]
|
|
35
|
+
: undefined;
|
|
36
|
+
const range = typeof declared === 'string' ? declared.trim() : '';
|
|
37
|
+
if (range === '')
|
|
38
|
+
continue;
|
|
39
|
+
const verdict = checkVersionRange(range, pin);
|
|
40
|
+
if (verdict === 'satisfied')
|
|
41
|
+
continue;
|
|
42
|
+
found.push({ name: name.trim(), range });
|
|
43
|
+
}
|
|
44
|
+
return found.sort((a, b) => a.name.localeCompare(b.name));
|
|
45
|
+
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { PluginPlanStep } from '@dsh-workbench/shared/host';
|
|
2
2
|
export declare const CHANNEL_PLUGIN_NAME = "opencli-plugin-workbench";
|
|
3
3
|
export declare const CHANNEL_PLUGIN_SITE = "workbench";
|
|
4
|
+
export declare function channelPluginDir(): string;
|
|
5
|
+
export declare function legacyChannelPluginDir(): string;
|
|
4
6
|
export interface ChannelPluginPlan {
|
|
5
7
|
readonly dest: string;
|
|
6
8
|
readonly version: string;
|
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import { existsSync, readFileSync } from 'node:fs';
|
|
2
2
|
import { join } from 'node:path';
|
|
3
3
|
import { planLineOf, quoteShellArg } from '@dsh-workbench/shared/host';
|
|
4
|
-
import { isolatedHome, toolchainOpencliDir, vendorPeerLink, vendorPluginsDir } from '../foundation/paths.js';
|
|
4
|
+
import { isolatedHome, toolchainOpencliDir, vendorPeerLink, vendorPrefix, vendorPluginsDir } from '../foundation/paths.js';
|
|
5
5
|
export const CHANNEL_PLUGIN_NAME = 'opencli-plugin-workbench';
|
|
6
6
|
export const CHANNEL_PLUGIN_SITE = 'workbench';
|
|
7
|
+
export function channelPluginDir() {
|
|
8
|
+
return join(vendorPrefix(), 'plugins', CHANNEL_PLUGIN_NAME);
|
|
9
|
+
}
|
|
10
|
+
export function legacyChannelPluginDir() {
|
|
11
|
+
return join(vendorPluginsDir(), CHANNEL_PLUGIN_NAME);
|
|
12
|
+
}
|
|
7
13
|
function textOf(value) {
|
|
8
14
|
return typeof value === 'string' ? value.trim() : '';
|
|
9
15
|
}
|
|
@@ -24,10 +30,11 @@ export function channelPluginPlan(options) {
|
|
|
24
30
|
if (version === undefined)
|
|
25
31
|
return undefined;
|
|
26
32
|
const command = options.command ?? 'opencli';
|
|
27
|
-
const dest =
|
|
33
|
+
const dest = channelPluginDir();
|
|
28
34
|
const peer = vendorPeerLink();
|
|
29
35
|
const steps = [
|
|
30
36
|
{ kind: 'copy', from: options.channelDir, to: dest },
|
|
37
|
+
{ kind: 'remove', path: legacyChannelPluginDir() },
|
|
31
38
|
{ kind: 'remove', path: peer.dir },
|
|
32
39
|
{ kind: 'mkdir', path: peer.parent },
|
|
33
40
|
{ kind: 'link', target: toolchainOpencliDir(), path: peer.dir },
|
|
@@ -41,7 +48,7 @@ export function channelPluginPlan(options) {
|
|
|
41
48
|
return { dest, version, steps, line: planLineOf(steps) };
|
|
42
49
|
}
|
|
43
50
|
export function channelInstalledVersion() {
|
|
44
|
-
return channelVersionOf(
|
|
51
|
+
return channelVersionOf(channelPluginDir());
|
|
45
52
|
}
|
|
46
53
|
export function channelSiteInstalled() {
|
|
47
54
|
return existsSync(join(isolatedHome(), '.opencli', 'plugins', CHANNEL_PLUGIN_SITE));
|
package/dist/data/channel.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { fileURLToPath } from 'node:url';
|
|
2
|
-
import { DEFAULT_RECIPE_MAX_BYTES, PlanStepError, productWrite, quoteShellArg, runPlan, } from '@dsh-workbench/shared/host';
|
|
2
|
+
import { CHANNEL_API_VERSION, DEFAULT_RECIPE_MAX_BYTES, PlanStepError, productWrite, quoteShellArg, runPlan, } from '@dsh-workbench/shared/host';
|
|
3
3
|
import { channelInstalledVersion, channelPluginPlan, channelSiteInstalled, channelVersionOf, } from './channel-plugin.js';
|
|
4
4
|
export const CHANNEL_DIR = fileURLToPath(new URL('../../channel/', import.meta.url));
|
|
5
5
|
function authOf(recipe) {
|
|
@@ -73,7 +73,7 @@ export function channelCallOf(recipe, overrides, command) {
|
|
|
73
73
|
'workbench',
|
|
74
74
|
write ? 'send' : 'fetch',
|
|
75
75
|
'--api',
|
|
76
|
-
|
|
76
|
+
String(CHANNEL_API_VERSION),
|
|
77
77
|
'--url',
|
|
78
78
|
channelUrlOf(recipe, overrides),
|
|
79
79
|
'--jar',
|
package/dist/data/index.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import { callsProblemOf, noWrite, now, planProblemOf, planResultsOf, recipeProblemOf, scriptOf, scriptOfMany, scriptOfPlan, allSame, } from '@dsh-workbench/shared/host';
|
|
1
|
+
import { BROWSER_READ_SESSIONS, BROWSER_SESSION_IDLE_MS, callsProblemOf, noWrite, now, planProblemOf, planResultsOf, recipeProblemOf, scriptOf, scriptOfMany, scriptOfPlan, allSame, } from '@dsh-workbench/shared/host';
|
|
2
|
+
import { opencliResultOf } from '../cli/opencli-wire.js';
|
|
2
3
|
import { browserRead } from '../browser/index.js';
|
|
3
4
|
import { attachOf } from './attachment.js';
|
|
4
5
|
import { readHttp } from './http.js';
|
|
5
6
|
import { createCommandRead } from './command-read.js';
|
|
6
7
|
import { createSessionQueue } from './queue.js';
|
|
8
|
+
import { createSessionKeeper } from './sessions.js';
|
|
7
9
|
import { submitPlanOf } from './submit-plan.js';
|
|
8
|
-
import { sessionOf } from './types.js';
|
|
10
|
+
import { readSessionOf, sessionOf, writeSessionOf } from './types.js';
|
|
9
11
|
import { allFailure, asRecord, browserFailure, failure, pageFailure, pickOf, } from './result.js';
|
|
10
12
|
import { channelCallOf, channelCommandLine, ensureChannel, } from './channel.js';
|
|
11
13
|
export * from './channel-plugin.js';
|
|
@@ -22,6 +24,15 @@ export function createDataReader(options = {}) {
|
|
|
22
24
|
const command = options.command ?? 'opencli';
|
|
23
25
|
const queue = createSessionQueue();
|
|
24
26
|
const queueWaitMs = options.queueWaitMs ?? 30_000;
|
|
27
|
+
const sessions = createSessionKeeper({
|
|
28
|
+
seam: options.seam,
|
|
29
|
+
command,
|
|
30
|
+
max: options.readSessions ?? BROWSER_READ_SESSIONS,
|
|
31
|
+
...(options.sessionRegistryDir === undefined
|
|
32
|
+
? {}
|
|
33
|
+
: { registryDir: options.sessionRegistryDir }),
|
|
34
|
+
idleMs: options.sessionIdleMs ?? BROWSER_SESSION_IDLE_MS,
|
|
35
|
+
});
|
|
25
36
|
const readCommand = createCommandRead({ seam: options.seam, command });
|
|
26
37
|
function browserCall(input) {
|
|
27
38
|
return {
|
|
@@ -60,11 +71,16 @@ export function createDataReader(options = {}) {
|
|
|
60
71
|
const argv = scriptArgProblemOf(input.script);
|
|
61
72
|
if (argv !== undefined)
|
|
62
73
|
return { kind: 'failed', result: failure('invalid-request', argv) };
|
|
63
|
-
const
|
|
64
|
-
|
|
74
|
+
const write = input.purpose === 'write';
|
|
75
|
+
const session = input.finalSession ??
|
|
76
|
+
(write ? writeSessionOf(input.session, input.url) : readSessionOf(input.session, input.url));
|
|
77
|
+
return serialGuard(session, input.signal, () => browserRead(browserCall({ ...input, session })), write);
|
|
65
78
|
}
|
|
66
|
-
|
|
79
|
+
const inFlight = new Map();
|
|
80
|
+
async function serialGuard(session, signal, work, write = false) {
|
|
81
|
+
inFlight.set(session, (inFlight.get(session) ?? 0) + 1);
|
|
67
82
|
const started = now();
|
|
83
|
+
sessions.begin(session);
|
|
68
84
|
const outcome = await queue.acquire(session, signal, queueWaitMs);
|
|
69
85
|
try {
|
|
70
86
|
options.onQueue?.({ session, waitedMs: now() - started, outcome });
|
|
@@ -85,6 +101,7 @@ export function createDataReader(options = {}) {
|
|
|
85
101
|
}
|
|
86
102
|
finally {
|
|
87
103
|
queue.release(session);
|
|
104
|
+
await sessions.end(session, write);
|
|
88
105
|
}
|
|
89
106
|
}
|
|
90
107
|
async function readBrowser(request, callOptions) {
|
|
@@ -126,13 +143,9 @@ export function createDataReader(options = {}) {
|
|
|
126
143
|
return failure('timeout', `通道读超时(${call.timeoutMs}ms)`);
|
|
127
144
|
if (result.exitCode !== 0)
|
|
128
145
|
return undefined;
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
parsed = JSON.parse(result.stdout);
|
|
132
|
-
}
|
|
133
|
-
catch {
|
|
146
|
+
const parsed = opencliResultOf(result.stdout);
|
|
147
|
+
if (parsed === undefined)
|
|
134
148
|
return undefined;
|
|
135
|
-
}
|
|
136
149
|
const box = asRecord(parsed);
|
|
137
150
|
if (box === undefined)
|
|
138
151
|
return undefined;
|
|
@@ -185,7 +198,10 @@ export function createDataReader(options = {}) {
|
|
|
185
198
|
catch (error) {
|
|
186
199
|
return failure('invalid-request', error instanceof Error ? error.message : String(error));
|
|
187
200
|
}
|
|
188
|
-
const got = await guardedBrowser(
|
|
201
|
+
const got = await guardedBrowser({
|
|
202
|
+
...recipeBrowserInput(recipe, script, seam, callOptions),
|
|
203
|
+
...(mode === 'submit' ? { purpose: 'write' } : {}),
|
|
204
|
+
});
|
|
189
205
|
const unwrapped = unwrapBrowser(got, recipe.loginUrl);
|
|
190
206
|
if (!unwrapped.ok)
|
|
191
207
|
return unwrapped;
|
|
@@ -336,17 +352,19 @@ export function createDataReader(options = {}) {
|
|
|
336
352
|
if (options.seam === undefined) {
|
|
337
353
|
return allSame(plan, { code: 'invalid-request', message: '没有执行缝,写不了' });
|
|
338
354
|
}
|
|
355
|
+
const session = writeSessionOf(plan.session, plan.origin);
|
|
339
356
|
const run = async (script) => {
|
|
340
357
|
const got = await guardedBrowser({
|
|
341
358
|
seam: options.seam,
|
|
342
359
|
script,
|
|
343
360
|
url: plan.origin,
|
|
344
361
|
...(plan.loginUrl === undefined ? {} : { loginUrl: plan.loginUrl }),
|
|
345
|
-
...(plan.session === undefined ? {} : { session: plan.session }),
|
|
346
362
|
open: submitOptions.open ?? false,
|
|
347
363
|
...(plan.timeoutMs === undefined ? {} : { timeoutMs: plan.timeoutMs }),
|
|
348
364
|
...(submitOptions.signal === undefined ? {} : { signal: submitOptions.signal }),
|
|
349
365
|
retry: false,
|
|
366
|
+
purpose: 'write',
|
|
367
|
+
finalSession: session,
|
|
350
368
|
});
|
|
351
369
|
if (got.kind === 'failed')
|
|
352
370
|
return got.result;
|
|
@@ -354,7 +372,7 @@ export function createDataReader(options = {}) {
|
|
|
354
372
|
return browserFailure(got.value, plan.loginUrl);
|
|
355
373
|
return { ok: true, value: got.value.value };
|
|
356
374
|
};
|
|
357
|
-
return submitPlanOf(plan, submitOptions, run, options.saveBytes);
|
|
375
|
+
return sessions.hold(session, () => submitPlanOf(plan, submitOptions, run, options.saveBytes));
|
|
358
376
|
},
|
|
359
377
|
async submit(recipe, overrides, callOptions = {}) {
|
|
360
378
|
return (await readRecipe(recipe, overrides, 'submit', callOptions));
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface SessionPool {
|
|
2
|
+
touch(session: string): void;
|
|
3
|
+
hold(session: string): void;
|
|
4
|
+
idle(session: string): string | undefined;
|
|
5
|
+
drop(session: string): void;
|
|
6
|
+
expire(idleMs: number): readonly string[];
|
|
7
|
+
}
|
|
8
|
+
export declare function createSessionPool(input: {
|
|
9
|
+
readonly max: number;
|
|
10
|
+
readonly close: (session: string) => void;
|
|
11
|
+
}): SessionPool;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { now } from '@dsh-workbench/shared/host';
|
|
2
|
+
export function createSessionPool(input) {
|
|
3
|
+
const seen = new Map();
|
|
4
|
+
const busy = new Set();
|
|
5
|
+
let clock = 0;
|
|
6
|
+
function mark(session) {
|
|
7
|
+
clock += 1;
|
|
8
|
+
seen.set(session, { seq: clock, at: now() });
|
|
9
|
+
}
|
|
10
|
+
function sweep(keep) {
|
|
11
|
+
if (seen.size <= input.max)
|
|
12
|
+
return;
|
|
13
|
+
const idle = [...seen.entries()]
|
|
14
|
+
.filter(([session]) => !busy.has(session) && session !== keep)
|
|
15
|
+
.sort((a, b) => a[1].seq - b[1].seq);
|
|
16
|
+
for (const [session] of idle) {
|
|
17
|
+
if (seen.size <= input.max)
|
|
18
|
+
break;
|
|
19
|
+
seen.delete(session);
|
|
20
|
+
input.close(session);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
touch(session) {
|
|
25
|
+
mark(session);
|
|
26
|
+
sweep(session);
|
|
27
|
+
},
|
|
28
|
+
hold(session) {
|
|
29
|
+
busy.add(session);
|
|
30
|
+
mark(session);
|
|
31
|
+
sweep(session);
|
|
32
|
+
},
|
|
33
|
+
drop(session) {
|
|
34
|
+
busy.delete(session);
|
|
35
|
+
seen.delete(session);
|
|
36
|
+
},
|
|
37
|
+
expire(idleMs) {
|
|
38
|
+
if (idleMs <= 0)
|
|
39
|
+
return [];
|
|
40
|
+
const at = now();
|
|
41
|
+
const found = [];
|
|
42
|
+
for (const [session, entry] of [...seen.entries()]) {
|
|
43
|
+
if (busy.has(session))
|
|
44
|
+
continue;
|
|
45
|
+
if (at - entry.at < idleMs)
|
|
46
|
+
continue;
|
|
47
|
+
seen.delete(session);
|
|
48
|
+
found.push(session);
|
|
49
|
+
}
|
|
50
|
+
return found;
|
|
51
|
+
},
|
|
52
|
+
idle(session) {
|
|
53
|
+
busy.delete(session);
|
|
54
|
+
mark(session);
|
|
55
|
+
const before = seen.size;
|
|
56
|
+
sweep(session);
|
|
57
|
+
return before === seen.size ? undefined : session;
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface SessionRegistry {
|
|
2
|
+
claim(session: string): void;
|
|
3
|
+
stale(): readonly string[];
|
|
4
|
+
release(): void;
|
|
5
|
+
}
|
|
6
|
+
export declare function createSessionRegistry(input: {
|
|
7
|
+
readonly dir: string;
|
|
8
|
+
readonly processId?: number | undefined;
|
|
9
|
+
}): SessionRegistry;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { ensureDir, pidAlive, removePath, writeTextFile } from '@dsh-workbench/shared/host';
|
|
4
|
+
function readFileAt(file) {
|
|
5
|
+
try {
|
|
6
|
+
const parsed = JSON.parse(readFileSync(file, 'utf8'));
|
|
7
|
+
if (typeof parsed !== 'object' || parsed === null)
|
|
8
|
+
return undefined;
|
|
9
|
+
const pid = parsed['pid'];
|
|
10
|
+
const sessions = parsed['sessions'];
|
|
11
|
+
if (typeof pid !== 'number' || !Number.isInteger(pid) || pid <= 0)
|
|
12
|
+
return undefined;
|
|
13
|
+
const list = Array.isArray(sessions) ? sessions.filter((one) => typeof one === 'string') : [];
|
|
14
|
+
return { pid, sessions: list };
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export function createSessionRegistry(input) {
|
|
21
|
+
const processId = input.processId ?? process.pid;
|
|
22
|
+
const own = join(input.dir, `${String(processId)}.json`);
|
|
23
|
+
const claimed = new Set();
|
|
24
|
+
function write() {
|
|
25
|
+
try {
|
|
26
|
+
ensureDir(input.dir);
|
|
27
|
+
writeTextFile(own, `${JSON.stringify({ pid: processId, sessions: [...claimed] })}\n`);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
claim(session) {
|
|
35
|
+
if (claimed.has(session))
|
|
36
|
+
return;
|
|
37
|
+
claimed.add(session);
|
|
38
|
+
write();
|
|
39
|
+
},
|
|
40
|
+
stale() {
|
|
41
|
+
if (!existsSync(input.dir))
|
|
42
|
+
return [];
|
|
43
|
+
const found = [];
|
|
44
|
+
for (const entry of readdirSync(input.dir)) {
|
|
45
|
+
if (!entry.endsWith('.json'))
|
|
46
|
+
continue;
|
|
47
|
+
const file = join(input.dir, entry);
|
|
48
|
+
const parsed = readFileAt(file);
|
|
49
|
+
if (parsed === undefined) {
|
|
50
|
+
removePath(file);
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (parsed.pid === processId)
|
|
54
|
+
continue;
|
|
55
|
+
if (pidAlive(parsed.pid))
|
|
56
|
+
continue;
|
|
57
|
+
found.push(...parsed.sessions);
|
|
58
|
+
removePath(file);
|
|
59
|
+
}
|
|
60
|
+
return [...new Set(found)];
|
|
61
|
+
},
|
|
62
|
+
release() {
|
|
63
|
+
removePath(own);
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type Seam } from '@dsh-workbench/shared/host';
|
|
2
|
+
export interface SessionKeeper {
|
|
3
|
+
begin(session: string): void;
|
|
4
|
+
end(session: string, write: boolean): Promise<void>;
|
|
5
|
+
hold<T>(session: string, work: () => Promise<T>): Promise<T>;
|
|
6
|
+
}
|
|
7
|
+
export declare function createSessionKeeper(input: {
|
|
8
|
+
readonly seam?: Seam | undefined;
|
|
9
|
+
readonly command: string;
|
|
10
|
+
readonly max: number;
|
|
11
|
+
readonly registryDir?: string | undefined;
|
|
12
|
+
readonly idleMs?: number | undefined;
|
|
13
|
+
}): SessionKeeper;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { productWrite } from '@dsh-workbench/shared/host';
|
|
2
|
+
import { createSessionPool } from './session-pool.js';
|
|
3
|
+
import { createSessionRegistry } from './session-registry.js';
|
|
4
|
+
export function createSessionKeeper(input) {
|
|
5
|
+
const inFlight = new Map();
|
|
6
|
+
const opened = new Set();
|
|
7
|
+
async function close(session) {
|
|
8
|
+
if (input.seam === undefined)
|
|
9
|
+
return;
|
|
10
|
+
await input.seam
|
|
11
|
+
.run({
|
|
12
|
+
argv: [input.command, 'browser', '--session', session, 'close'],
|
|
13
|
+
intent: productWrite(),
|
|
14
|
+
timeoutMs: 15_000,
|
|
15
|
+
bestEffort: true,
|
|
16
|
+
})
|
|
17
|
+
.catch(() => undefined);
|
|
18
|
+
}
|
|
19
|
+
const pool = createSessionPool({ max: input.max, close: (session) => void close(session) });
|
|
20
|
+
const registry = input.registryDir === undefined ? undefined : createSessionRegistry({ dir: input.registryDir });
|
|
21
|
+
async function leave(session, write) {
|
|
22
|
+
const left = (inFlight.get(session) ?? 1) - 1;
|
|
23
|
+
if (left > 0) {
|
|
24
|
+
inFlight.set(session, left);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
inFlight.delete(session);
|
|
28
|
+
if (!write)
|
|
29
|
+
return;
|
|
30
|
+
if (!opened.delete(session))
|
|
31
|
+
return;
|
|
32
|
+
pool.drop(session);
|
|
33
|
+
await close(session);
|
|
34
|
+
}
|
|
35
|
+
void (async () => {
|
|
36
|
+
for (const session of registry?.stale() ?? [])
|
|
37
|
+
await close(session);
|
|
38
|
+
})();
|
|
39
|
+
if (input.idleMs !== undefined && input.idleMs > 0) {
|
|
40
|
+
const timer = setInterval(() => {
|
|
41
|
+
void (async () => {
|
|
42
|
+
for (const session of pool.expire(input.idleMs ?? 0))
|
|
43
|
+
await close(session);
|
|
44
|
+
})();
|
|
45
|
+
}, Math.min(input.idleMs, 60_000));
|
|
46
|
+
timer.unref?.();
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
begin(session) {
|
|
50
|
+
inFlight.set(session, (inFlight.get(session) ?? 0) + 1);
|
|
51
|
+
registry?.claim(session);
|
|
52
|
+
opened.add(session);
|
|
53
|
+
pool.hold(session);
|
|
54
|
+
},
|
|
55
|
+
async end(session, write) {
|
|
56
|
+
pool.idle(session);
|
|
57
|
+
await leave(session, write);
|
|
58
|
+
},
|
|
59
|
+
async hold(session, work) {
|
|
60
|
+
inFlight.set(session, (inFlight.get(session) ?? 0) + 1);
|
|
61
|
+
try {
|
|
62
|
+
return await work();
|
|
63
|
+
}
|
|
64
|
+
finally {
|
|
65
|
+
await leave(session, true);
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
package/dist/data/types.d.ts
CHANGED
|
@@ -13,7 +13,12 @@ export interface DataReaderOptions {
|
|
|
13
13
|
readonly channel?: boolean | undefined;
|
|
14
14
|
readonly channelInstalled?: (() => boolean) | undefined;
|
|
15
15
|
readonly queueWaitMs?: number | undefined;
|
|
16
|
+
readonly readSessions?: number | undefined;
|
|
17
|
+
readonly sessionRegistryDir?: string | undefined;
|
|
18
|
+
readonly sessionIdleMs?: number | undefined;
|
|
16
19
|
readonly onQueue?: ((event: QueueWaitEvent) => void) | undefined;
|
|
17
20
|
readonly saveBytes?: SaveBytes | undefined;
|
|
18
21
|
}
|
|
19
22
|
export declare function sessionOf(value: string | undefined): string;
|
|
23
|
+
export declare function readSessionOf(value: string | undefined, url: string | undefined): string;
|
|
24
|
+
export declare function writeSessionOf(value: string | undefined, url: string | undefined): string;
|
package/dist/data/types.js
CHANGED
|
@@ -1,4 +1,32 @@
|
|
|
1
|
-
import { BROWSER_READ_SESSION } from '@dsh-workbench/shared/host';
|
|
1
|
+
import { BROWSER_READ_SESSION, BROWSER_WRITE_SESSION } from '@dsh-workbench/shared/host';
|
|
2
2
|
export function sessionOf(value) {
|
|
3
3
|
return value === undefined || value === '' ? BROWSER_READ_SESSION : value;
|
|
4
4
|
}
|
|
5
|
+
const HOST_RE = /^[a-z0-9-]+$/;
|
|
6
|
+
function hostKeyOf(url) {
|
|
7
|
+
if (url === undefined || url.trim() === '')
|
|
8
|
+
return undefined;
|
|
9
|
+
let host;
|
|
10
|
+
try {
|
|
11
|
+
host = new URL(url).host.toLowerCase();
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
if (host === '')
|
|
17
|
+
return undefined;
|
|
18
|
+
const key = host.replaceAll(/[^a-z0-9]+/g, '-').replaceAll(/^-+|-+$/g, '');
|
|
19
|
+
return HOST_RE.test(key) ? key : undefined;
|
|
20
|
+
}
|
|
21
|
+
export function readSessionOf(value, url) {
|
|
22
|
+
if (value !== undefined && value !== '')
|
|
23
|
+
return value;
|
|
24
|
+
const host = hostKeyOf(url);
|
|
25
|
+
return host === undefined ? BROWSER_READ_SESSION : `${BROWSER_READ_SESSION}-${host}`;
|
|
26
|
+
}
|
|
27
|
+
export function writeSessionOf(value, url) {
|
|
28
|
+
if (value !== undefined && value !== '')
|
|
29
|
+
return `${value}-write`;
|
|
30
|
+
const host = hostKeyOf(url);
|
|
31
|
+
return host === undefined ? BROWSER_WRITE_SESSION : `${BROWSER_WRITE_SESSION}-${host}`;
|
|
32
|
+
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { OPENCLI_NPM_PACKAGE } from '@dsh-workbench/shared/host';
|
|
2
2
|
export declare const OPENCLI_RUNTIME_ID = "opencli";
|
|
3
3
|
export declare const OPENCLI_MANIFEST_FILE = "opencli-plugin.json";
|
|
4
|
+
export declare const OPENCLI_RUNTIME_FIELD = "opencli";
|
|
4
5
|
export { OPENCLI_NPM_PACKAGE };
|
|
5
6
|
export declare function isolatedRoot(): string;
|
|
6
7
|
export declare function isolatedHome(): string;
|
|
8
|
+
export declare function sessionStateDir(): string;
|
|
7
9
|
export declare function vendorPrefix(): string;
|
|
8
10
|
export declare function vendorPluginsDir(): string;
|
|
9
11
|
export declare function libsPrefix(): string;
|
package/dist/foundation/paths.js
CHANGED
|
@@ -3,6 +3,7 @@ import { join } from 'node:path';
|
|
|
3
3
|
import { OPENCLI_NPM_PACKAGE, runtimeLayoutOf } from '@dsh-workbench/shared/host';
|
|
4
4
|
export const OPENCLI_RUNTIME_ID = 'opencli';
|
|
5
5
|
export const OPENCLI_MANIFEST_FILE = 'opencli-plugin.json';
|
|
6
|
+
export const OPENCLI_RUNTIME_FIELD = 'opencli';
|
|
6
7
|
export { OPENCLI_NPM_PACKAGE };
|
|
7
8
|
const layout = () => runtimeLayoutOf(OPENCLI_RUNTIME_ID);
|
|
8
9
|
export function isolatedRoot() {
|
|
@@ -11,6 +12,9 @@ export function isolatedRoot() {
|
|
|
11
12
|
export function isolatedHome() {
|
|
12
13
|
return layout().home;
|
|
13
14
|
}
|
|
15
|
+
export function sessionStateDir() {
|
|
16
|
+
return join(layout().root, 'state', 'sessions');
|
|
17
|
+
}
|
|
14
18
|
export function vendorPrefix() {
|
|
15
19
|
return layout().vendorPrefix;
|
|
16
20
|
}
|
|
@@ -62,11 +66,15 @@ function lineStoreDir(text) {
|
|
|
62
66
|
const value = unquote(line.slice('storeDir:'.length).trim());
|
|
63
67
|
return value === '' ? undefined : value;
|
|
64
68
|
}
|
|
69
|
+
function storeRootOf(value) {
|
|
70
|
+
return value.replace(/[\\/]v\d+$/u, '');
|
|
71
|
+
}
|
|
65
72
|
export function storeDirFor(projectDir, read = readTextFile) {
|
|
66
73
|
const text = read(join(projectDir, 'node_modules', '.modules.yaml'));
|
|
67
74
|
if (text === undefined)
|
|
68
75
|
return pnpmStoreDir();
|
|
69
|
-
|
|
76
|
+
const recorded = jsonStoreDir(text) ?? lineStoreDir(text);
|
|
77
|
+
return recorded === undefined ? pnpmStoreDir() : storeRootOf(recorded);
|
|
70
78
|
}
|
|
71
79
|
export function toolchainOpencliDir() {
|
|
72
80
|
return join(isolatedRoot(), 'toolchain', 'node_modules', ...OPENCLI_NPM_PACKAGE.split('/'));
|
package/dist/libs/links.d.ts
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
export declare function ensureLibsBinOnPath(env?: NodeJS.ProcessEnv): boolean;
|
|
2
|
+
export declare function libsOpencliPeerLink(): string;
|
|
3
|
+
export declare function detachLibsOpencliPeer(): boolean;
|
|
2
4
|
export declare function ensureLibsOpencliPeer(): boolean;
|
|
5
|
+
export declare function ensureVendorOpencliPeer(): boolean;
|
package/dist/libs/links.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { existsSync, mkdirSync, realpathSync } from 'node:fs';
|
|
2
2
|
import { delimiter, dirname, join } from 'node:path';
|
|
3
|
-
import { linkDir } from '@dsh-workbench/shared/host';
|
|
4
|
-
import { OPENCLI_NPM_PACKAGE, libsBinDir, libsPrefix, toolchainOpencliDir } from '../foundation/paths.js';
|
|
3
|
+
import { linkDir, removePath } from '@dsh-workbench/shared/host';
|
|
4
|
+
import { OPENCLI_NPM_PACKAGE, libsBinDir, libsPrefix, toolchainOpencliDir, vendorPluginsDir, } from '../foundation/paths.js';
|
|
5
5
|
export function ensureLibsBinOnPath(env = process.env) {
|
|
6
6
|
const dir = libsBinDir();
|
|
7
7
|
const parts = (env.PATH ?? '').split(delimiter).filter((one) => one !== '');
|
|
@@ -18,8 +18,22 @@ function pointsAt(link, target) {
|
|
|
18
18
|
return false;
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
|
-
export function
|
|
22
|
-
|
|
21
|
+
export function libsOpencliPeerLink() {
|
|
22
|
+
return join(libsPrefix(), 'node_modules', ...OPENCLI_NPM_PACKAGE.split('/'));
|
|
23
|
+
}
|
|
24
|
+
export function detachLibsOpencliPeer() {
|
|
25
|
+
const link = libsOpencliPeerLink();
|
|
26
|
+
if (!existsSync(link))
|
|
27
|
+
return false;
|
|
28
|
+
try {
|
|
29
|
+
removePath(link);
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function linkRuntimePeer(link) {
|
|
23
37
|
const target = toolchainOpencliDir();
|
|
24
38
|
if (!existsSync(target))
|
|
25
39
|
return false;
|
|
@@ -34,3 +48,9 @@ export function ensureLibsOpencliPeer() {
|
|
|
34
48
|
return false;
|
|
35
49
|
}
|
|
36
50
|
}
|
|
51
|
+
export function ensureLibsOpencliPeer() {
|
|
52
|
+
return linkRuntimePeer(libsOpencliPeerLink());
|
|
53
|
+
}
|
|
54
|
+
export function ensureVendorOpencliPeer() {
|
|
55
|
+
return linkRuntimePeer(join(vendorPluginsDir(), ...OPENCLI_NPM_PACKAGE.split('/')));
|
|
56
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dsh-workbench/adapter-runtime",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0-beta.1",
|
|
4
4
|
"description": "Opencli, browser and data runtime adapters.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
"bin"
|
|
18
18
|
],
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"
|
|
20
|
+
"execa": "^10.2.0",
|
|
21
|
+
"@dsh-workbench/shared": "1.4.0-beta.1"
|
|
21
22
|
},
|
|
22
23
|
"devDependencies": {
|
|
23
24
|
"@types/node": "^24.19.0",
|