@jacebenson/jsn 5.5.1-dev.166.1 → 5.5.1-dev.167.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jacebenson/jsn",
3
- "version": "5.5.1-dev.166.1",
3
+ "version": "5.5.1-dev.167.1",
4
4
  "description": "A command-line interface for ServiceNow that follows the Unix philosophy: simple, composable, and scriptable.",
5
5
  "type": "module",
6
6
  "bin": {
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(_argv = {}) {
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
 
@@ -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}`,
@@ -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
- return { ...inspection, _formatted: formatted };
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
- await this._setAuth(req, { skipOAuthSession });
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