@jacebenson/jsn 5.5.1-dev.166.1 → 5.5.1-dev.168.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/package.json +1 -1
- package/src/app.js +11 -1
- package/src/cli.js +3 -3
- package/src/commands/auth.js +6 -4
- package/src/commands/flows.js +6 -0
- package/src/commands/setup.js +16 -4
- package/src/flow-inspection.js +26 -2
- package/src/sdk.js +28 -1
- package/src/setup-preferences.js +3 -3
- package/src/welcome.js +9 -24
package/package.json
CHANGED
package/src/app.js
CHANGED
|
@@ -95,11 +95,21 @@ export class App {
|
|
|
95
95
|
return this.session?.instance || '';
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
async printContextHeader(
|
|
98
|
+
async printContextHeader(argv = {}) {
|
|
99
99
|
if (!this.getEffectiveInstance() || !this.sdk) return;
|
|
100
100
|
if (process.env.JSN_NO_HEADER) return;
|
|
101
101
|
if (this.output.getFormat() === FormatJSON || this.output.getFormat() === FormatQuiet) return;
|
|
102
102
|
|
|
103
|
+
// The banner is a terminal affordance, never data. `--get`/`--select`
|
|
104
|
+
// prints an extracted JSON value while the format still reports pretty, so
|
|
105
|
+
// a banner on stderr merged with `2>&1` landed in front of the JSON and
|
|
106
|
+
// broke `jsn ... 2>&1 | jq` / `json.load(sys.stdin)` at character 0. Skip
|
|
107
|
+
// it whenever an extraction was requested or stderr is not a terminal
|
|
108
|
+
// (nobody reads it there, and it costs five HTTP requests per command:
|
|
109
|
+
// user, application preference, update-set preference, scope, update set).
|
|
110
|
+
if (argv.get !== undefined && argv.get !== null && argv.get !== false) return;
|
|
111
|
+
if (process.stderr.isTTY !== true) return;
|
|
112
|
+
|
|
103
113
|
let userDisplayName = 'Unknown';
|
|
104
114
|
let userSysID = '';
|
|
105
115
|
|
package/src/cli.js
CHANGED
|
@@ -601,12 +601,12 @@ export function buildCLI() {
|
|
|
601
601
|
}
|
|
602
602
|
process.exit(0);
|
|
603
603
|
}
|
|
604
|
-
// First-run orientation
|
|
604
|
+
// First-run orientation replaces the catalog only for a fresh human terminal.
|
|
605
605
|
// maybeShowWelcome decides for itself: human terminal, no machine
|
|
606
606
|
// format, no configured profile, and not shown before.
|
|
607
607
|
const config = loadConfig(formatArgv);
|
|
608
|
-
maybeShowWelcome({ argv: formatArgv, requestedFormat, cfg: config });
|
|
609
|
-
process.stdout.write(renderHelp(config, visibleCommandRegistry(cliInstance.commandRegistry(), config, formatArgv)) + '\n');
|
|
608
|
+
const welcome = maybeShowWelcome({ argv: formatArgv, requestedFormat, cfg: config });
|
|
609
|
+
if (!welcome) process.stdout.write(renderHelp(config, visibleCommandRegistry(cliInstance.commandRegistry(), config, formatArgv)) + '\n');
|
|
610
610
|
process.exit(0);
|
|
611
611
|
}
|
|
612
612
|
// Every other failure here (unknown command, missing required argument,
|
package/src/commands/auth.js
CHANGED
|
@@ -322,19 +322,21 @@ export async function loginWizard(app, argv = {}) {
|
|
|
322
322
|
const request = { ...argv, profile: undefined, instance, name: name.trim() };
|
|
323
323
|
if (method === 'browser') request.browser = true;
|
|
324
324
|
if (method === 'oauth') request.oauth = true;
|
|
325
|
+
const settings = await chooseConnectionPreferences(app.config, profileSettingValues(argv), { confirm });
|
|
325
326
|
if (method === 'basic') {
|
|
326
327
|
request.basic = true;
|
|
327
328
|
const username = await input({ message: 'Username:', validate: value => value.trim() ? true : 'A username is required' });
|
|
328
329
|
request._basicCredentials = { username: username.trim(), password: await password({ message: 'Password:' }) };
|
|
329
330
|
}
|
|
330
|
-
const settings = await chooseConnectionPreferences(app.config, profileSettingValues(argv), { confirm });
|
|
331
331
|
request['read-only'] = settings.read_only;
|
|
332
332
|
request['skip-confirmations'] = settings.skip_confirmations;
|
|
333
333
|
request['include-counts'] = settings.include_counts;
|
|
334
|
-
request.makeDefault = argv.makeDefault ??
|
|
335
|
-
message: 'Use this profile by default?', default: !app.config.defaultProfile
|
|
336
|
-
});
|
|
334
|
+
request.makeDefault = argv.makeDefault ?? (Object.keys(app.config.profiles || {}).length === 0 && !app.config.defaultProfile
|
|
335
|
+
? true : await confirm({ message: 'Use this profile by default?', default: !app.config.defaultProfile }));
|
|
337
336
|
const result = await loginProfile(request, app);
|
|
337
|
+
if (result.authenticated) {
|
|
338
|
+
process.stdout.write(`Connection preset: ${settings.read_only ? 'read-only' : 'writes allowed'} · deletes ${settings.skip_confirmations ? 'without confirmation' : 'ask first'} · totals ${settings.include_counts ? 'on' : 'off'}\nChange with: jsn auth modify ${result.profile} --read-only\n`);
|
|
339
|
+
}
|
|
338
340
|
return { instanceURL: instance, profileName: result.profile, loggedIn: result.authenticated };
|
|
339
341
|
}
|
|
340
342
|
|
package/src/commands/flows.js
CHANGED
|
@@ -249,6 +249,11 @@ export function flowsCmd(wrap) {
|
|
|
249
249
|
type: 'number',
|
|
250
250
|
default: 2,
|
|
251
251
|
describe: 'Expand nested subflows to this depth (1 = subflow names only, 2 = one level, 3+ = deeper)',
|
|
252
|
+
})
|
|
253
|
+
.option('raw-payload', {
|
|
254
|
+
type: 'boolean',
|
|
255
|
+
default: false,
|
|
256
|
+
describe: 'Include the raw ProcessFlow document (large; it duplicates the extracted steps)',
|
|
252
257
|
}),
|
|
253
258
|
handler: wrap(async (argv, app) => {
|
|
254
259
|
const identifier = await pickFlow(argv, app);
|
|
@@ -258,6 +263,7 @@ export function flowsCmd(wrap) {
|
|
|
258
263
|
identifier,
|
|
259
264
|
instanceURL: app.getEffectiveInstance(),
|
|
260
265
|
depth: argv.depth,
|
|
266
|
+
includePayload: argv.rawPayload === true,
|
|
261
267
|
});
|
|
262
268
|
app.ok(inspection, {
|
|
263
269
|
summary: `Flow: ${inspection.flow.name}`,
|
package/src/commands/setup.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { configureGlobalDefaults, configureDocumentation } from '../setup-preferences.js';
|
|
1
|
+
import { configureGlobalDefaults, configureDocumentation, connectionPreferences } from '../setup-preferences.js';
|
|
2
|
+
import { saveConfig } from '../config.js';
|
|
2
3
|
import { select } from '../prompts.js';
|
|
3
4
|
import { loginWizard, loginProfile, showAuthStatus, switchProfile, modifyProfile, logoutProfile, removeConnection } from './auth.js';
|
|
4
5
|
import { errUsageHint } from '../errors.js';
|
|
@@ -37,6 +38,7 @@ const setupHelp = () => [
|
|
|
37
38
|
'Use --no-read-only, --no-confirm-deletes, --no-include-counts or --no-make-default for the opposite value.',
|
|
38
39
|
'Use --json for structured output. Explicit flags avoid implicit pickers; ambiguous targets return an error.',
|
|
39
40
|
'Discover all options with jsn auth <command> --help.',
|
|
41
|
+
'Configure settings directly: jsn setup --configure-defaults or jsn setup --configure-docs.',
|
|
40
42
|
].join('\n');
|
|
41
43
|
|
|
42
44
|
/** The setup hub prompt; q and Escape resolve with no selected action. */
|
|
@@ -119,21 +121,31 @@ export function setupCmd(wrap) {
|
|
|
119
121
|
return {
|
|
120
122
|
command: 'setup',
|
|
121
123
|
describe: 'Set up JSN defaults and manage ServiceNow connections (interactive)',
|
|
122
|
-
builder: y => y.option('docs-source', { type: 'string', choices: ['snapshot', 'github', 'skip'], describe: 'Install optional documentation directly without the authentication wizard' })
|
|
124
|
+
builder: y => y.option('docs-source', { type: 'string', choices: ['snapshot', 'github', 'skip'], describe: 'Install optional documentation directly without the authentication wizard' })
|
|
125
|
+
.option('configure-defaults', { type: 'boolean', describe: 'Configure defaults for future connections without the hub' })
|
|
126
|
+
.option('configure-docs', { type: 'boolean', describe: 'Configure documentation without the hub' }).epilogue(setupHelp()),
|
|
123
127
|
handler: wrap(async (argv, app) => {
|
|
124
128
|
if (argv['docs-source']) {
|
|
125
129
|
const { installDocumentation } = await import('./docs/install.js');
|
|
126
130
|
app.ok(await installDocumentation(argv['docs-source']));
|
|
127
131
|
return;
|
|
128
132
|
}
|
|
133
|
+
if (argv['configure-defaults'] || argv['configure-docs']) {
|
|
134
|
+
if (argv['configure-defaults']) await configureGlobalDefaults(app.config, { documentation: false });
|
|
135
|
+
if (argv['configure-docs']) await configureDocumentation(app.config);
|
|
136
|
+
app.ok({ settings: true }, { summary: 'Settings saved; existing connections unchanged' });
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
129
139
|
const { canUsePicker } = await import('../helpers.js');
|
|
130
140
|
if (!app.isInteractive() || !canUsePicker(app, argv)) {
|
|
131
141
|
throw errUsageHint('"jsn setup" is interactive. Use explicit auth commands instead.', setupHelp());
|
|
132
142
|
}
|
|
133
143
|
const profileNames = Object.keys(app.config.profiles || {});
|
|
134
144
|
const firstSetup = !app.config.connectionDefaults;
|
|
135
|
-
if (firstSetup)
|
|
136
|
-
|
|
145
|
+
if (firstSetup) {
|
|
146
|
+
app.config.connectionDefaults = connectionPreferences(app.config);
|
|
147
|
+
await saveConfig(app.config);
|
|
148
|
+
}
|
|
137
149
|
if (profileNames.length === 0 && firstSetup) {
|
|
138
150
|
const result = await loginWizard(app, argv);
|
|
139
151
|
await showAuthStatus({ ...argv, profile: result.profileName }, app);
|
package/src/flow-inspection.js
CHANGED
|
@@ -4,7 +4,7 @@ import { getStringField } from './helpers.js';
|
|
|
4
4
|
* Inspect a flow through the narrow remote adapter and render its deep view.
|
|
5
5
|
* The adapter only needs inspectFlow and inspectCustomAction.
|
|
6
6
|
*/
|
|
7
|
-
export async function inspectFlow({ adapter, identifier, instanceURL = '', depth = 2 }) {
|
|
7
|
+
export async function inspectFlow({ adapter, identifier, instanceURL = '', depth = 2, includePayload = false }) {
|
|
8
8
|
if (!adapter || typeof adapter.inspectFlow !== 'function') {
|
|
9
9
|
throw new TypeError('flow inspection adapter must provide inspectFlow');
|
|
10
10
|
}
|
|
@@ -19,7 +19,31 @@ export async function inspectFlow({ adapter, identifier, instanceURL = '', depth
|
|
|
19
19
|
catalogLookupCache: new Map(),
|
|
20
20
|
};
|
|
21
21
|
const formatted = await formatFlowInspection(inspection, ctx);
|
|
22
|
-
|
|
22
|
+
const shaped = includePayload ? inspection : omitRawPayload(inspection);
|
|
23
|
+
const result = { ...shaped };
|
|
24
|
+
// Same convention as `catalogitems show`: the rendered view is a styled-mode
|
|
25
|
+
// side channel, so it must not appear in the JSON envelope. Attached
|
|
26
|
+
// enumerably it shipped the whole ASCII tree (4.9KB on one flow) next to the
|
|
27
|
+
// fields it renders, on top of an already oversized payload.
|
|
28
|
+
Object.defineProperty(result, '_formatted', { value: formatted, enumerable: false, configurable: true });
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The raw ProcessFlow document is both the bulk of a `flows show` response and
|
|
34
|
+
* a duplicate of it: actionInstances, flowLogicInstances and subFlowInstances
|
|
35
|
+
* are copied out of the payload onto the inspection, so every step serializes
|
|
36
|
+
* twice. Measured on a 10-step flow: 536KB of payload inside 1.06MB of data,
|
|
37
|
+
* with all three arrays byte-identical at both levels. The styled view is
|
|
38
|
+
* rendered from the payload before this runs, and `--raw-payload` keeps it for
|
|
39
|
+
* the rare caller that needs the provider document itself.
|
|
40
|
+
*/
|
|
41
|
+
function omitRawPayload(inspection) {
|
|
42
|
+
if (!inspection || typeof inspection !== 'object' || Array.isArray(inspection)) return inspection;
|
|
43
|
+
if (!Object.prototype.hasOwnProperty.call(inspection, 'payload')) return inspection;
|
|
44
|
+
const copy = { ...inspection };
|
|
45
|
+
delete copy.payload;
|
|
46
|
+
return copy;
|
|
23
47
|
}
|
|
24
48
|
|
|
25
49
|
function normalizeInspection(value) {
|
package/src/sdk.js
CHANGED
|
@@ -36,6 +36,26 @@ function serializeCookies(cookieJar) {
|
|
|
36
36
|
return [...cookieJar].map(([name, value]) => `${name}=${value}`).join('; ');
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
+
// REST resources that authenticate from the request's own headers. Every CLI
|
|
40
|
+
// invocation is a fresh Node process, so `_warmSession()` always pays 2-3
|
|
41
|
+
// sequential requests to mint UI cookies these paths never read — measured on a
|
|
42
|
+
// live instance, `jsn records list` spent 4 round-trips on one list. Anything
|
|
43
|
+
// else (script processors, GraphQL, the concourse picker, xmlhttp.do, update-set
|
|
44
|
+
// export/import, binary attachment uploads) keeps the session bootstrap it
|
|
45
|
+
// needs, and JSN_ALWAYS_WARM_SESSION=1 restores it for every path.
|
|
46
|
+
const STATELESS_REST_PREFIXES = ['/api/now/table/', '/api/now/stats/', '/api/now/v1/batch'];
|
|
47
|
+
|
|
48
|
+
export function needsUiSession(url, { stateful = false, forceSession = false } = {}) {
|
|
49
|
+
if (stateful || forceSession) return true;
|
|
50
|
+
let pathname;
|
|
51
|
+
try {
|
|
52
|
+
pathname = new URL(url).pathname;
|
|
53
|
+
} catch {
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
return !STATELESS_REST_PREFIXES.some(prefix => pathname.startsWith(prefix));
|
|
57
|
+
}
|
|
58
|
+
|
|
39
59
|
// Build the sys.scripts.do POST body for background script execution.
|
|
40
60
|
// Extracted for testability (issue #177): maps script-mode options to the
|
|
41
61
|
// form fields on ServiceNow's Script Background form. Defaults preserve the
|
|
@@ -178,7 +198,14 @@ export class SDKClient {
|
|
|
178
198
|
try {
|
|
179
199
|
const req = new Request(endpoint, { ...opts, signal: controller.signal });
|
|
180
200
|
if (prepare) prepare(req);
|
|
181
|
-
|
|
201
|
+
// Bootstrap the UI session only for the surfaces that read it. Table,
|
|
202
|
+
// stats and batch requests authenticate from their headers, so warming
|
|
203
|
+
// there was latency and an extra failure mode for nothing.
|
|
204
|
+
const skipSession = skipOAuthSession || !needsUiSession(endpoint, {
|
|
205
|
+
stateful,
|
|
206
|
+
forceSession: process.env.JSN_ALWAYS_WARM_SESSION === '1',
|
|
207
|
+
});
|
|
208
|
+
await this._setAuth(req, { skipOAuthSession: skipSession });
|
|
182
209
|
const response = await fetch(req);
|
|
183
210
|
if (!skipOAuthSession && this.recovery) {
|
|
184
211
|
// A 401 is definitive; only then is the body sniffed for a login
|
package/src/setup-preferences.js
CHANGED
|
@@ -26,9 +26,9 @@ export async function askConnectionPreferences(defaults, prompts = { confirm })
|
|
|
26
26
|
|
|
27
27
|
export async function chooseConnectionPreferences(config, flags = {}, prompts = { confirm }) {
|
|
28
28
|
const defaults = connectionPreferences(config, {}, flags);
|
|
29
|
-
const summary =
|
|
30
|
-
if (await prompts.confirm({ message:
|
|
31
|
-
return askConnectionPreferences(defaults, prompts);
|
|
29
|
+
const summary = `${defaults.read_only ? 'read-only' : 'writes allowed'} · delete confirmations ${defaults.skip_confirmations ? 'off' : 'on'} · result totals ${defaults.include_counts ? 'on' : 'off'}`;
|
|
30
|
+
if (await prompts.confirm({ message: `Accept defaults? ${summary}`, default: true })) return defaults;
|
|
31
|
+
return connectionPreferences(config, await askConnectionPreferences(defaults, prompts), flags);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
export async function configureDocumentation(config, prompts = { input, confirm }) {
|
package/src/welcome.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
// First-run welcome (issue #387).
|
|
2
2
|
//
|
|
3
|
-
// A brand-new install
|
|
4
|
-
//
|
|
5
|
-
// above that help, exactly once, only when a human is reading it.
|
|
3
|
+
// A brand-new install gets a compact front door instead of the command catalog,
|
|
4
|
+
// exactly once, only when a human is reading it.
|
|
6
5
|
//
|
|
7
6
|
// Invariants (see docs/DECISIONS.md):
|
|
8
7
|
// - at most once: a marker file in the XDG config dir records that the note
|
|
@@ -10,7 +9,7 @@
|
|
|
10
9
|
// both print it;
|
|
11
10
|
// - human terminal only: no --json/--quiet/--csv/--markdown/--get, no piped
|
|
12
11
|
// output, no agent/CI environments;
|
|
13
|
-
// -
|
|
12
|
+
// - explicit help stays comprehensive; the first-run front door stands alone;
|
|
14
13
|
// - never an instance or auth operation: the note only points at local and
|
|
15
14
|
// interactive commands, so it cannot block setup or require credentials;
|
|
16
15
|
// - nothing on this path reaches the network, prompts, or reads an instance;
|
|
@@ -39,21 +38,10 @@ const MIN_WIDTH = 24;
|
|
|
39
38
|
|
|
40
39
|
const HUMAN_FORMATS = new Set(['', 'auto', 'styled']);
|
|
41
40
|
|
|
42
|
-
/**
|
|
43
|
-
* What a first run actually needs next. Every command here exists, is visible
|
|
44
|
-
* to a profile-less install, and works without an instance:
|
|
45
|
-
* - `jsn setup` opens the interactive add-connection wizard (and defaults);
|
|
46
|
-
* - `jsn auth login <instance>` adds one connection without the wizard;
|
|
47
|
-
* - `jsn --help` is the grouped command list this note sits above;
|
|
48
|
-
* - `jsn docs search` queries ServiceNow documentation, not an instance;
|
|
49
|
-
* - `jsn skill install` writes the bundled agent skill from local files.
|
|
50
|
-
*/
|
|
41
|
+
/** One obvious next action, plus secondary discovery; no command catalog. */
|
|
51
42
|
const STEPS = [
|
|
52
|
-
{ command: 'jsn setup', detail: '
|
|
53
|
-
{ command: 'jsn auth login <instance>', detail: 'one connection, scriptable, no wizard' },
|
|
43
|
+
{ command: 'jsn setup', detail: 'connect to ServiceNow' },
|
|
54
44
|
{ command: 'jsn --help', detail: 'every command and flag' },
|
|
55
|
-
{ command: 'jsn docs search <query>', detail: 'search ServiceNow documentation' },
|
|
56
|
-
{ command: 'jsn skill install', detail: 'install the jsn skill for your AI agent' },
|
|
57
45
|
];
|
|
58
46
|
|
|
59
47
|
/**
|
|
@@ -145,8 +133,8 @@ function renderSteps(width) {
|
|
|
145
133
|
}
|
|
146
134
|
|
|
147
135
|
/**
|
|
148
|
-
* The orientation text. Kept short: it
|
|
149
|
-
*
|
|
136
|
+
* The orientation text. Kept short: it stands alone on the first screen
|
|
137
|
+
* instead of the command catalog, and stays inside 12 lines at 80
|
|
150
138
|
* columns.
|
|
151
139
|
*
|
|
152
140
|
* Everything shown is derived from local state: the version comes from the
|
|
@@ -177,11 +165,8 @@ export function welcomeLines({
|
|
|
177
165
|
...wrapText(title, width),
|
|
178
166
|
...wrapText(configLabel, width),
|
|
179
167
|
'',
|
|
180
|
-
'
|
|
168
|
+
' Get started',
|
|
181
169
|
...renderSteps(width),
|
|
182
|
-
'',
|
|
183
|
-
...wrapText('Nothing above needs an instance. jsn asks for credentials only when needed.', width),
|
|
184
|
-
...wrapText('Prints once. Hide it with JSN_NO_WELCOME=1. Update with jsn upgrade.', width),
|
|
185
170
|
];
|
|
186
171
|
}
|
|
187
172
|
|
|
@@ -304,7 +289,7 @@ export function markWelcomeSeen(configDir = globalConfigDir()) {
|
|
|
304
289
|
}
|
|
305
290
|
|
|
306
291
|
/**
|
|
307
|
-
* Show the welcome
|
|
292
|
+
* Show the standalone welcome instead of the catalog when this is a first
|
|
308
293
|
* run, then record it. Returns the text that was shown, or '' when nothing was.
|
|
309
294
|
*
|
|
310
295
|
* The marker is claimed before anything is printed, so an interrupted run
|