@intuned/runtime-dev 1.0.6-cli-auth.0.0.11-test → 1.0.6-cli-auth.0.0.12-test

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.
@@ -2,14 +2,11 @@
2
2
  "use strict";
3
3
 
4
4
  var _commander = require("commander");
5
- var fs = _interopRequireWildcard(require("fs-extra"));
6
5
  var _dotenv = _interopRequireDefault(require("dotenv"));
7
- var _path = _interopRequireDefault(require("path"));
8
6
  var _utils = require("./utils");
9
7
  var _chalk = _interopRequireDefault(require("chalk"));
8
+ var _utils2 = require("../run-api-cli/utils");
10
9
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
- function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
12
- function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
13
10
  _dotenv.default.config({
14
11
  path: `.env`
15
12
  });
@@ -23,16 +20,7 @@ _commander.program.description("Create an auth session").option("-i, --input <in
23
20
  if (!createApiExists) {
24
21
  throw new Error("Auth session creation API not implemented, please create it in the auth sessions specified directory");
25
22
  }
26
- let authSessionInput = {};
27
- if (options.input) {
28
- const inputFilePath = _path.default.resolve(process.cwd(), options.input);
29
- if (!(await fs.exists(inputFilePath))) {
30
- throw new Error(`Auth session input file not found at ${inputFilePath}`);
31
- }
32
- const authSessionInputFile = options.input;
33
- const authSessionInputPath = _path.default.join(process.cwd(), authSessionInputFile);
34
- authSessionInput = await fs.readJSON(authSessionInputPath);
35
- }
23
+ const authSessionInput = (await (0, _utils2.loadParameters)(options.input)) ?? {};
36
24
  const session = await (0, _utils.runCreateApi)(authSessionInput);
37
25
  if (!session) {
38
26
  console.error(_chalk.default.red("Failed to create auth session."));
@@ -1,19 +1,20 @@
1
1
  import { StorageState } from "../../common/contextStorageStateHelpers";
2
+ import { AuthSessionMetadata } from "../../common/cli/types";
2
3
  export declare function isAuthEnabled(): Promise<boolean>;
3
4
  export declare function ensureAuthApi(operation: "create" | "check"): Promise<boolean>;
4
5
  export declare function runCreateApi(authSessionInput: Record<string, any>): Promise<StorageState>;
5
6
  export declare function runCheckApi(authSessionPath: string): Promise<boolean>;
6
7
  export declare function runCreateApiViaCLI(authSessionInput: Record<string, any>): Promise<StorageState>;
7
8
  export declare function runCheckApiViaCLI(authSessionPath: string): Promise<boolean>;
8
- export declare function storeAuthSessionInstance(authSessionInstance: StorageState, customName?: string, authSessionInput?: Record<string, any>): Promise<string>;
9
+ export declare function storeAuthSessionInstance(authSessionInstance: StorageState, customName?: string, authSessionInput?: Record<string, any>, shouldOverride?: boolean): Promise<string>;
9
10
  export declare function retrieveAuthSessionInstance(authSessionId: string, pathsOnly?: boolean): Promise<{
10
11
  authSessionInstanceStoragePath: string;
11
12
  authSessionInstanceMetadataPath: string;
12
13
  authSessionInstance?: undefined;
13
14
  metadata?: undefined;
14
15
  } | {
15
- authSessionInstance: any;
16
- metadata: any;
16
+ authSessionInstance: StorageState;
17
+ metadata: AuthSessionMetadata;
17
18
  authSessionInstanceStoragePath?: undefined;
18
19
  authSessionInstanceMetadataPath?: undefined;
19
20
  }>;
@@ -21,6 +21,7 @@ var _runApi = require("../../common/runApi");
21
21
  var _tsNodeImport = require("../common/tsNodeImport");
22
22
  var _promptly = require("promptly");
23
23
  var _utils = require("../../common/cli/utils");
24
+ var _types = require("../../common/cli/types");
24
25
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
25
26
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
26
27
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
@@ -61,7 +62,10 @@ async function runCheckApi(authSessionPath) {
61
62
  }, () => runCheckApiViaCLI(authSessionPath));
62
63
  return checkResult;
63
64
  } catch (error) {
64
- throw new Error(`Error running the create API: ${error.message}`);
65
+ if (error.message === _types.CHECK_FAILED_ERROR_MESSAGE) {
66
+ return false;
67
+ }
68
+ throw new Error(`Error running the check API: ${error.message}`);
65
69
  }
66
70
  }
67
71
  async function runCreateApiViaCLI(authSessionInput) {
@@ -142,30 +146,39 @@ async function runCheckApiViaCLI(authSessionPath) {
142
146
  }
143
147
  return result;
144
148
  }
145
- async function storeAuthSessionInstance(authSessionInstance, customName, authSessionInput) {
149
+ async function storeAuthSessionInstance(authSessionInstance, customName, authSessionInput, shouldOverride = false) {
146
150
  try {
147
151
  const authSessionsDirectoryPath = _path.default.join(process.cwd(), _constants.AUTH_SESSIONS_INSTANCES_FOLDER_NAME);
148
152
  await fs.ensureDir(authSessionsDirectoryPath);
149
153
  const authSessionInstanceId = customName ?? `auth-session-${Date.now()}`;
150
154
  const authSessionInstancePath = _path.default.join(authSessionsDirectoryPath, authSessionInstanceId);
155
+ const authSessionExists = await fs.pathExists(authSessionInstancePath);
151
156
  await fs.ensureDir(authSessionInstancePath);
152
157
  const authSessionInstanceStoragePath = _path.default.join(authSessionInstancePath, `auth-session.json`);
153
158
  await fs.writeJSON(authSessionInstanceStoragePath, authSessionInstance, {
154
159
  spaces: 2
155
160
  });
156
161
  const projectAuthConfig = await (0, _utils.getSettingIntunedJSON)("authSessions");
162
+ let existingMetadata = {};
163
+ const authSessionInstanceMetadataPath = _path.default.join(authSessionInstancePath, `metadata.json`);
164
+ if (authSessionExists && shouldOverride) {
165
+ try {
166
+ existingMetadata = await fs.readJSON(authSessionInstanceMetadataPath);
167
+ } catch (readError) {
168
+ existingMetadata = {};
169
+ }
170
+ }
157
171
  const authSessionMetadata = {
158
- createdAt: new Date().toISOString(),
172
+ createdAt: existingMetadata.createdAt || new Date().toISOString(),
159
173
  updatedAt: new Date().toISOString(),
160
- authSessionInput: authSessionInput ?? {},
174
+ authSessionInput: authSessionInput || existingMetadata.authSessionInput || {},
161
175
  authSessionId: authSessionInstanceId,
162
- authSessionType: projectAuthConfig.type ?? "API",
176
+ authSessionType: projectAuthConfig.type ?? existingMetadata.authSessionType ?? "API",
163
177
  ...(projectAuthConfig.type === "MANUAL" && {
164
178
  recorderStartUrl: projectAuthConfig.startUrl,
165
179
  recorderEndUrl: projectAuthConfig.endUrl
166
180
  })
167
181
  };
168
- const authSessionInstanceMetadataPath = _path.default.join(authSessionInstancePath, `metadata.json`);
169
182
  await fs.writeJSON(authSessionInstanceMetadataPath, authSessionMetadata, {
170
183
  spaces: 2
171
184
  });
@@ -8,6 +8,7 @@ var _dotenv = _interopRequireDefault(require("dotenv"));
8
8
  var _utils = require("./utils");
9
9
  var _enums = require("../../runtime/enums");
10
10
  var _asyncLocalStorage = require("../../common/asyncLocalStorage");
11
+ var _utils2 = require("../cli-auth-sessions/utils");
11
12
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
12
13
  _dotenv.default.config({
13
14
  path: `.env`
@@ -17,6 +18,13 @@ _commander.program.name("intuned-run").description("Run an Intuned API with para
17
18
  if (!apiName) {
18
19
  throw new Error("API name is required, please provide it");
19
20
  }
21
+ const _isAuthEnabled = await (0, _utils2.isAuthEnabled)();
22
+ if (!_isAuthEnabled && options.authSession) {
23
+ throw new Error("Auth session is not enabled, enable it in Intuned.json to be able to use it");
24
+ }
25
+ if (_isAuthEnabled && !options.authSession) {
26
+ throw new Error("Auth session is enabled but no auth session provided, please provide it");
27
+ }
20
28
  const inputData = await (0, _utils.loadParameters)(options.parametersFile);
21
29
  const runId = (0, _nanoid.nanoid)();
22
30
  const {
@@ -27,7 +35,7 @@ _commander.program.name("intuned-run").description("Run an Intuned API with para
27
35
  extendedPayloads: [],
28
36
  runId
29
37
  }, () => (0, _utils.runApiViaCLI)(apiName, inputData, {
30
- authSessionPath: options.authSession
38
+ authSession: options.authSession
31
39
  }));
32
40
  if (!result) {
33
41
  console.log(_chalk.default.yellow("No result returned from the API"));
@@ -1,10 +1,8 @@
1
1
  import { Payload } from "../../runtime/export";
2
- import { ApiAuthSessionBehavior } from "../../common/cli/types";
3
2
  export declare function loadParameters(parametersFile: string): Promise<object | null>;
4
3
  export declare function writeResultToFile(runId: string, result: any, payloadToAppend?: Payload[]): Promise<void>;
5
4
  export declare function runApiViaCLI(apiName: string, inputData: object | null | undefined, options?: {
6
- authSessionPath?: string;
7
- authSessionsMode?: ApiAuthSessionBehavior;
5
+ authSession?: string;
8
6
  }): Promise<{
9
7
  result: any;
10
8
  payloadToAppend: Payload[] | undefined;
@@ -12,9 +12,7 @@ var _Logger = require("../../common/Logger");
12
12
  var _chalk = _interopRequireDefault(require("chalk"));
13
13
  var _runApi = require("../../common/runApi");
14
14
  var _tsNodeImport = require("../common/tsNodeImport");
15
- var _utils = require("../../common/cli/utils");
16
- var _types = require("../../common/cli/types");
17
- var _utils2 = require("../cli-auth-sessions/utils");
15
+ var _utils = require("../cli-auth-sessions/utils");
18
16
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
19
17
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
20
18
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
@@ -64,32 +62,45 @@ async function writeResultToFile(runId, result, payloadToAppend) {
64
62
  }
65
63
  async function runApiViaCLI(apiName, inputData, options) {
66
64
  let authSessionPathToUse = null;
67
- const authSessionsMode = (options === null || options === void 0 ? void 0 : options.authSessionsMode) ?? "PERFORM_CHECK_AND_REFRESH";
68
- if (options !== null && options !== void 0 && options.authSessionPath) {
69
- const authSessions = await (0, _utils.getSettingIntunedJSON)("authSessions");
70
- if (authSessions.enabled) {
71
- if (!options.authSessionPath) {
72
- throw new Error("Auth session is enabled but no auth session provided");
73
- }
74
- authSessionPathToUse = options.authSessionPath;
75
- } else {
76
- if (options.authSessionPath) {
77
- throw new Error("Auth session is not enabled but auth session provided. To use auth session please enable it in Intuned.json");
78
- }
79
- }
65
+ if (options !== null && options !== void 0 && options.authSession) {
66
+ const {
67
+ authSessionInstanceStoragePath
68
+ } = await (0, _utils.retrieveAuthSessionInstance)(options === null || options === void 0 ? void 0 : options.authSession, true);
69
+ authSessionPathToUse = authSessionInstanceStoragePath;
80
70
  }
81
- if (authSessionsMode === _types.ApiAuthSessionBehavior.PERFORM_CHECK_AND_REFRESH && authSessionPathToUse) {
82
- const checkApiExists = await (0, _utils2.ensureAuthApi)("check");
71
+ if (authSessionPathToUse) {
72
+ const checkApiExists = await (0, _utils.ensureAuthApi)("check");
83
73
  if (!checkApiExists) {
84
74
  throw new Error("Auth session check API not implemented, please create it in the auth sessions specified directory");
85
75
  }
86
- const checkResult = await (0, _utils2.runCheckApiViaCLI)(authSessionPathToUse);
76
+ const checkResult = await (0, _utils.runCheckApiViaCLI)(authSessionPathToUse);
87
77
  if (!checkResult) {
88
78
  console.log(_chalk.default.yellow("Auth session check failed, trying to refresh it..."));
89
- const createApiExists = await (0, _utils2.ensureAuthApi)("create");
79
+ const createApiExists = await (0, _utils.ensureAuthApi)("create");
90
80
  if (!createApiExists) {
91
81
  throw new Error("Auth session creation API not implemented, please create it in the auth sessions specified directory");
92
82
  }
83
+ const {
84
+ metadata
85
+ } = await (0, _utils.retrieveAuthSessionInstance)(authSessionPathToUse, true);
86
+ if ((metadata === null || metadata === void 0 ? void 0 : metadata.authSessionType) === "MANUAL") {
87
+ throw new Error("Expired Auth session is recorder-based, please provide a new one or refresh it manually");
88
+ }
89
+ const authSessionInput = (metadata === null || metadata === void 0 ? void 0 : metadata.authSessionInput) ?? {};
90
+ try {
91
+ const refresehAuthSessionInstance = await (0, _utils.runCreateApiViaCLI)(authSessionInput);
92
+ if (!refresehAuthSessionInstance) {
93
+ throw new Error("Failed to refresh auth session");
94
+ }
95
+ await (0, _utils.storeAuthSessionInstance)(refresehAuthSessionInstance, options === null || options === void 0 ? void 0 : options.authSession, authSessionInput, true);
96
+ const checkResult = await (0, _utils.runCheckApiViaCLI)(authSessionPathToUse);
97
+ if (!checkResult) {
98
+ throw new Error("Failed to refresh auth session");
99
+ }
100
+ console.log(_chalk.default.green("✓ Auth session refreshed successfully on this run"));
101
+ } catch (error) {
102
+ throw new Error(`Failed to refresh auth session: ${error.message}`);
103
+ }
93
104
  }
94
105
  }
95
106
  const runApiResult = await (0, _runApi.runApi)({
@@ -54,3 +54,14 @@ export declare enum ApiAuthSessionBehavior {
54
54
  PERFORM_CHECK_AND_REFRESH = "PERFORM_CHECK_AND_REFRESH",
55
55
  SKIP_CHECK_AND_REFRESH = "SKIP_CHECK_AND_REFRESH"
56
56
  }
57
+ export declare const CHECK_FAILED_ERROR_MESSAGE = "Auth session check failed";
58
+ export type AuthSessionType = "API" | "MANUAL";
59
+ export type AuthSessionMetadata = {
60
+ createdAt: string;
61
+ updatedAt: string;
62
+ authSessionInput: Record<string, any>;
63
+ authSessionId: string;
64
+ authSessionType: AuthSessionType;
65
+ recorderStartUrl?: string;
66
+ recorderEndUrl?: string;
67
+ };
@@ -3,10 +3,11 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.templateIds = exports.ApiAuthSessionBehavior = void 0;
6
+ exports.templateIds = exports.CHECK_FAILED_ERROR_MESSAGE = exports.ApiAuthSessionBehavior = void 0;
7
7
  const templateIds = exports.templateIds = ["default", "empty", "linkedin-recorder", "api-auth-sessions", "nested-scheduling", "ai-extractors", "npm-auth-sessions"];
8
8
  let ApiAuthSessionBehavior = exports.ApiAuthSessionBehavior = function (ApiAuthSessionBehavior) {
9
9
  ApiAuthSessionBehavior["PERFORM_CHECK_AND_REFRESH"] = "PERFORM_CHECK_AND_REFRESH";
10
10
  ApiAuthSessionBehavior["SKIP_CHECK_AND_REFRESH"] = "SKIP_CHECK_AND_REFRESH";
11
11
  return ApiAuthSessionBehavior;
12
- }({});
12
+ }({});
13
+ const CHECK_FAILED_ERROR_MESSAGE = exports.CHECK_FAILED_ERROR_MESSAGE = "Auth session check failed";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intuned/runtime-dev",
3
- "version": "1.0.6-cli-auth.0.0.11-test",
3
+ "version": "1.0.6-cli-auth.0.0.12-test",
4
4
  "description": "Intuned runtime",
5
5
  "exports": {
6
6
  ".": "./dist/index.js",