@chargebee/chargebee-apps-shared 0.0.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.
Files changed (48) hide show
  1. package/LICENSE +24 -0
  2. package/README.md +73 -0
  3. package/SECURITY.md +8 -0
  4. package/dist/config/environment.d.ts +78 -0
  5. package/dist/config/environment.js +150 -0
  6. package/dist/config/path-resolver.d.ts +117 -0
  7. package/dist/config/path-resolver.js +247 -0
  8. package/dist/dependency/dependency-manager.d.ts +66 -0
  9. package/dist/dependency/dependency-manager.js +257 -0
  10. package/dist/errors.d.ts +8 -0
  11. package/dist/errors.js +17 -0
  12. package/dist/index.d.ts +19 -0
  13. package/dist/index.js +36 -0
  14. package/dist/libs/archive-service.d.ts +21 -0
  15. package/dist/libs/archive-service.js +145 -0
  16. package/dist/libs/file-management-service.d.ts +59 -0
  17. package/dist/libs/file-management-service.js +189 -0
  18. package/dist/libs/iparams-service.d.ts +69 -0
  19. package/dist/libs/iparams-service.js +136 -0
  20. package/dist/libs/template-service.d.ts +57 -0
  21. package/dist/libs/template-service.js +152 -0
  22. package/dist/logger/cb-logger.d.ts +56 -0
  23. package/dist/logger/cb-logger.js +13 -0
  24. package/dist/logger/internal-logger.d.ts +81 -0
  25. package/dist/logger/internal-logger.js +146 -0
  26. package/dist/node/file-system.d.ts +66 -0
  27. package/dist/node/file-system.js +76 -0
  28. package/dist/node/process.d.ts +30 -0
  29. package/dist/node/process.js +25 -0
  30. package/dist/sandbox/sandbox-context.d.ts +37 -0
  31. package/dist/sandbox/sandbox-context.js +48 -0
  32. package/dist/sandbox/sandbox-wrapper.d.ts +75 -0
  33. package/dist/sandbox/sandbox-wrapper.js +2 -0
  34. package/dist/types/common.d.ts +135 -0
  35. package/dist/types/common.js +20 -0
  36. package/dist/validator/iparam-constants.d.ts +49 -0
  37. package/dist/validator/iparam-constants.js +52 -0
  38. package/dist/validator/iparam-defns-validator.d.ts +46 -0
  39. package/dist/validator/iparam-defns-validator.js +291 -0
  40. package/dist/validator/iparams-inputs-validator.d.ts +62 -0
  41. package/dist/validator/iparams-inputs-validator.js +289 -0
  42. package/dist/validator/manifest-validator.d.ts +48 -0
  43. package/dist/validator/manifest-validator.js +138 -0
  44. package/dist/validator/package-validator.d.ts +27 -0
  45. package/dist/validator/package-validator.js +43 -0
  46. package/dist/validator/validator-utils.d.ts +13 -0
  47. package/dist/validator/validator-utils.js +26 -0
  48. package/package.json +45 -0
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sandboxContext = exports.SandboxContext = void 0;
4
+ const async_hooks_1 = require("async_hooks");
5
+ /**
6
+ * SandboxContext manages the execution context for sandboxed code execution
7
+ * using AsyncLocalStorage to maintain correlation between event and site information
8
+ */
9
+ class SandboxContext {
10
+ constructor() {
11
+ this.asyncLocalStorage = new async_hooks_1.AsyncLocalStorage();
12
+ }
13
+ /**
14
+ * Creates a new execution context with event and site information
15
+ * @param {Object} contextData - The context data containing event and site information
16
+ * @param {string} contextData.eventId - The unique identifier for the event
17
+ * @param {string} contextData.eventType - The type of the event (e.g., 'customer_created')
18
+ * @param {string} contextData.site - The site/tenant identifier in Chargebee
19
+ * @param {Function} callback - The function to execute within this context
20
+ * @returns {Promise<any>} The result of the callback execution
21
+ */
22
+ async run(contextData, callback) {
23
+ const context = {
24
+ eventId: contextData.eventId || 'unknown',
25
+ eventType: contextData.eventType || 'unknown',
26
+ startTime: new Date().toISOString(),
27
+ };
28
+ return this.asyncLocalStorage.run(context, () => callback());
29
+ }
30
+ /**
31
+ * Gets the current execution context
32
+ * @returns {Object|null} The current context or null if not in an execution context
33
+ */
34
+ getCurrentContext() {
35
+ return this.asyncLocalStorage.getStore();
36
+ }
37
+ /**
38
+ * Checks if there is an active execution context
39
+ * @returns {boolean} True if there is an active context, false otherwise
40
+ */
41
+ hasContext() {
42
+ return this.asyncLocalStorage.getStore() !== undefined;
43
+ }
44
+ }
45
+ exports.SandboxContext = SandboxContext;
46
+ // Create a singleton instance
47
+ exports.sandboxContext = new SandboxContext();
48
+ exports.default = exports.sandboxContext;
@@ -0,0 +1,75 @@
1
+ import { ICBLogger } from '../logger/cb-logger';
2
+ import { HandlerPayload } from '../types/common';
3
+ /**
4
+ * Result of sandbox execution
5
+ */
6
+ export interface ExecutionResult {
7
+ /** Whether the execution was successful */
8
+ success: boolean;
9
+ /** HTTP status code (200 for success, error code for failures) */
10
+ statusCode?: number;
11
+ /** Error message if execution failed */
12
+ error?: string;
13
+ /** Stack trace if execution failed */
14
+ stack?: string | undefined;
15
+ }
16
+ /**
17
+ * Sandbox environment configuration
18
+ */
19
+ export interface SandboxEnvironment {
20
+ /** Controlled console object */
21
+ console: any;
22
+ /** Read-only payload (event + iparams) passed to handler(payload) */
23
+ payload: HandlerPayload;
24
+ /** Safe async primitives */
25
+ setTimeout: typeof setTimeout;
26
+ clearTimeout: typeof clearTimeout;
27
+ setImmediate: typeof setImmediate;
28
+ clearImmediate: typeof clearImmediate;
29
+ /** CommonJS emulation */
30
+ module: {
31
+ exports: any;
32
+ };
33
+ exports: any;
34
+ /** Controlled module access */
35
+ require: (moduleName: string) => any;
36
+ /** Limited process access */
37
+ process: {
38
+ cwd: () => string;
39
+ env: Record<string, string>;
40
+ nextTick: typeof process.nextTick;
41
+ };
42
+ /** Prevented escape hatches */
43
+ global?: undefined;
44
+ Buffer?: undefined;
45
+ __dirname?: undefined;
46
+ __filename?: undefined;
47
+ }
48
+ /**
49
+ * Interface for sandbox wrapper implementations
50
+ * This defines the contract that both public and private sandbox implementations must follow
51
+ */
52
+ export interface ISandboxWrapper {
53
+ /**
54
+ * Creates a safe require function that uses isolated node_modules
55
+ * @param projectPath - Path to the user project
56
+ * @returns A safe require function
57
+ */
58
+ createSafeRequire(projectPath: string): (moduleName: string) => any;
59
+ /**
60
+ * Creates a secure sandbox environment for code execution
61
+ * @param projectPath - Path to the user project
62
+ * @param payload - Handler payload (event + iparams) to expose to user code as handler(payload)
63
+ * @param sessionLogger - Logger instance for this server session
64
+ * @returns The sandbox environment object; handlers are invoked with payload
65
+ */
66
+ createSandbox(projectPath: string, payload: HandlerPayload, sessionLogger: ICBLogger): SandboxEnvironment;
67
+ /**
68
+ * Executes user-provided code in a secure sandbox environment
69
+ * @param userCodePath - Path to the directory containing user code
70
+ * @param payload - Handler payload (event + iparams) to pass to the user handler
71
+ * @param sessionLogger - The logger instance for this server session
72
+ * @returns {Promise<ExecutionResult>} Execution result with success status and data/error
73
+ */
74
+ execute(userCodePath: string, payload: HandlerPayload, sessionLogger: ICBLogger): Promise<ExecutionResult>;
75
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,135 @@
1
+ /**
2
+ * EventRecord is the type for the event data that is passed to the handler function.
3
+ */
4
+ export interface EventRecord {
5
+ /** API version */
6
+ api_version: string;
7
+ /** Event content containing the actual data */
8
+ content: Record<string, any>;
9
+ /** Type of the event (e.g., 'customer_created', 'subscription_created') */
10
+ event_type: string;
11
+ /** Unique identifier for the event */
12
+ id: string;
13
+ /** Object type */
14
+ object: string;
15
+ /** Timestamp when the event occurred */
16
+ occurred_at: number;
17
+ /** Source of the event */
18
+ source: string;
19
+ /** Webhook status */
20
+ webhook_status: string;
21
+ /** Array of webhook configurations */
22
+ webhooks: Array<{
23
+ id: string;
24
+ object: string;
25
+ webhook_status: string;
26
+ }>;
27
+ }
28
+ export interface AllowedModule {
29
+ name: string;
30
+ version?: string;
31
+ description?: string;
32
+ type: 'internal' | 'public';
33
+ }
34
+ export interface Manifest {
35
+ events: Record<string, {
36
+ handler: string;
37
+ }>;
38
+ dependencies?: Record<string, string>;
39
+ }
40
+ /**
41
+ * Create options for create command
42
+ */
43
+ export interface CreateOptions {
44
+ template: string;
45
+ }
46
+ /**
47
+ * Run options for run command
48
+ */
49
+ export interface RunOptions {
50
+ port: string;
51
+ }
52
+ /**
53
+ * Package options for package command
54
+ */
55
+ export interface PackageOptions {
56
+ name: string;
57
+ }
58
+ /**
59
+ * Map of iparam `type` identifiers to placeholder values.
60
+ * Keys match valid `type` values in `iparams.json`; each value’s TypeScript type is the allowed
61
+ * JSON shape for that type in definition defaults and `iparams.local.json`.
62
+ */
63
+ export declare const IPARAM_TYPE_VALUE_MAP: {
64
+ NUMBER: number;
65
+ TEXT: string;
66
+ DROPDOWN: string;
67
+ MULTISELECT_DROPDOWN: string[];
68
+ DATE: string;
69
+ URL: string;
70
+ BOOLEAN: boolean;
71
+ SECRET: string;
72
+ };
73
+ /** Parameter type (keys of {@link IPARAM_TYPE_VALUE_MAP}) */
74
+ export type IparamType = keyof typeof IPARAM_TYPE_VALUE_MAP;
75
+ /** Per-type value type; derived from {@link IPARAM_TYPE_VALUE_MAP} */
76
+ export type IparamValueByType = {
77
+ [K in IparamType]: (typeof IPARAM_TYPE_VALUE_MAP)[K];
78
+ };
79
+ /** Valid `type` strings for runtime checks. */
80
+ export declare const VALID_IPARAM_TYPES: IparamType[];
81
+ /** Union of every value an iparam can hold. */
82
+ export type IparamValue = IparamValueByType[IparamType];
83
+ /** Single parameter entry inside a section in `iparams.json`. */
84
+ export interface IparamDefn {
85
+ name: string;
86
+ display_name: string;
87
+ description: string;
88
+ type: IparamType;
89
+ required?: boolean;
90
+ default?: IparamValue;
91
+ options?: string[];
92
+ }
93
+ /** Group of related parameters in `iparams.json`. */
94
+ export interface IparamSection {
95
+ name: string;
96
+ display_name: string;
97
+ description: string;
98
+ parameters: IparamDefn[];
99
+ }
100
+ /** Parameter values for a single section in `iparams.local.json`. */
101
+ export interface SectionObject {
102
+ [key: string]: IparamValue;
103
+ }
104
+ /**
105
+ * Root schema for `iparams.json`.
106
+ * An empty object means no installation parameters are defined.
107
+ * Parameter names are unique within a section and may repeat across sections.
108
+ */
109
+ export interface IparamDefns {
110
+ installation_parameters?: {
111
+ sections: IparamSection[];
112
+ };
113
+ }
114
+ /**
115
+ * Root values for `iparams.local.json`, keyed by section name.
116
+ * Access in handlers: `payload.iparams.<section_name>.<param_name>`.
117
+ */
118
+ export interface IparamInputs {
119
+ [sectionName: string]: SectionObject;
120
+ }
121
+ /** SNS payload version */
122
+ export type PayloadVersion = 'v1' | 'v2';
123
+ /**
124
+ * Single argument passed to event handlers.
125
+ * Handlers receive payload with event data and installation parameters.
126
+ */
127
+ export interface HandlerPayload {
128
+ /** The webhook event record */
129
+ event: EventRecord;
130
+ /**
131
+ * Installation parameter values.
132
+ * Present for templates/apps that define iparams; omitted for templates without iparams.
133
+ */
134
+ iparams?: IparamInputs;
135
+ }
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VALID_IPARAM_TYPES = exports.IPARAM_TYPE_VALUE_MAP = void 0;
4
+ /**
5
+ * Map of iparam `type` identifiers to placeholder values.
6
+ * Keys match valid `type` values in `iparams.json`; each value’s TypeScript type is the allowed
7
+ * JSON shape for that type in definition defaults and `iparams.local.json`.
8
+ */
9
+ exports.IPARAM_TYPE_VALUE_MAP = {
10
+ NUMBER: 0,
11
+ TEXT: '',
12
+ DROPDOWN: '',
13
+ MULTISELECT_DROPDOWN: [],
14
+ DATE: '',
15
+ URL: '',
16
+ BOOLEAN: false,
17
+ SECRET: ''
18
+ };
19
+ /** Valid `type` strings for runtime checks. */
20
+ exports.VALID_IPARAM_TYPES = Object.keys(exports.IPARAM_TYPE_VALUE_MAP);
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Allowed top-level keys in `iparams.json`.
3
+ */
4
+ export declare const IPARAMS_INSTALLATION_PARAMETERS_LABEL = "installation_parameters";
5
+ export declare const IPARAMS_ROOT_ALLOWED_KEYS: readonly ["installation_parameters"];
6
+ /**
7
+ * File name for iparam definitions in an app directory.
8
+ */
9
+ export declare const IPARAMS_JSON_FILENAME = "iparams.json";
10
+ /**
11
+ * Label for the `sections` array in validation error messages.
12
+ */
13
+ export declare const IPARAMS_SECTIONS_LABEL = "sections";
14
+ /**
15
+ * Allowed keys under `installation_parameters` in `iparams.json`.
16
+ */
17
+ export declare const IPARAMS_INSTALLATION_PARAMETERS_ALLOWED_KEYS: readonly ["sections"];
18
+ /**
19
+ * Maximum number of parameter definitions allowed in a single `iparams.json` array.
20
+ */
21
+ export declare const IPARAMS_MAX_COUNT = 20;
22
+ /**
23
+ * Central limits for iparam definition fields (`iparams.json`).
24
+ */
25
+ export declare const IPARAM_DEFN_MAX_LENGTHS: {
26
+ readonly NAME: 50;
27
+ readonly DISPLAY_NAME: 50;
28
+ readonly DESCRIPTION: 250;
29
+ };
30
+ /**
31
+ * Limits for DROPDOWN and MULTISELECT_DROPDOWN `options` arrays in definitions.
32
+ */
33
+ export declare const OPTIONS_LIMITS: {
34
+ readonly MAX_COUNT: 20;
35
+ readonly MAX_OPTION_LENGTH: 250;
36
+ };
37
+ /**
38
+ * Max lengths for iparam input values when validating `iparams.local.json` / runtime inputs.
39
+ */
40
+ export declare const IPARAM_VALUE_MAX_LENGTHS: {
41
+ readonly TEXT: 250;
42
+ readonly SECRET: 250;
43
+ readonly URL: 250;
44
+ readonly DATE: 10;
45
+ };
46
+ /**
47
+ * Allowed {@link URL#protocol} values for iparam type `URL`.
48
+ */
49
+ export declare const IPARAM_URL_ALLOWED_PROTOCOLS: readonly ["http:", "https:"];
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IPARAM_URL_ALLOWED_PROTOCOLS = exports.IPARAM_VALUE_MAX_LENGTHS = exports.OPTIONS_LIMITS = exports.IPARAM_DEFN_MAX_LENGTHS = exports.IPARAMS_MAX_COUNT = exports.IPARAMS_INSTALLATION_PARAMETERS_ALLOWED_KEYS = exports.IPARAMS_SECTIONS_LABEL = exports.IPARAMS_JSON_FILENAME = exports.IPARAMS_ROOT_ALLOWED_KEYS = exports.IPARAMS_INSTALLATION_PARAMETERS_LABEL = void 0;
4
+ /**
5
+ * Allowed top-level keys in `iparams.json`.
6
+ */
7
+ exports.IPARAMS_INSTALLATION_PARAMETERS_LABEL = 'installation_parameters';
8
+ exports.IPARAMS_ROOT_ALLOWED_KEYS = [exports.IPARAMS_INSTALLATION_PARAMETERS_LABEL];
9
+ /**
10
+ * File name for iparam definitions in an app directory.
11
+ */
12
+ exports.IPARAMS_JSON_FILENAME = 'iparams.json';
13
+ /**
14
+ * Label for the `sections` array in validation error messages.
15
+ */
16
+ exports.IPARAMS_SECTIONS_LABEL = 'sections';
17
+ /**
18
+ * Allowed keys under `installation_parameters` in `iparams.json`.
19
+ */
20
+ exports.IPARAMS_INSTALLATION_PARAMETERS_ALLOWED_KEYS = [exports.IPARAMS_SECTIONS_LABEL];
21
+ /**
22
+ * Maximum number of parameter definitions allowed in a single `iparams.json` array.
23
+ */
24
+ exports.IPARAMS_MAX_COUNT = 20;
25
+ /**
26
+ * Central limits for iparam definition fields (`iparams.json`).
27
+ */
28
+ exports.IPARAM_DEFN_MAX_LENGTHS = {
29
+ NAME: 50,
30
+ DISPLAY_NAME: 50,
31
+ DESCRIPTION: 250
32
+ };
33
+ /**
34
+ * Limits for DROPDOWN and MULTISELECT_DROPDOWN `options` arrays in definitions.
35
+ */
36
+ exports.OPTIONS_LIMITS = {
37
+ MAX_COUNT: 20,
38
+ MAX_OPTION_LENGTH: 250
39
+ };
40
+ /**
41
+ * Max lengths for iparam input values when validating `iparams.local.json` / runtime inputs.
42
+ */
43
+ exports.IPARAM_VALUE_MAX_LENGTHS = {
44
+ TEXT: 250,
45
+ SECRET: 250,
46
+ URL: 250,
47
+ DATE: 10
48
+ };
49
+ /**
50
+ * Allowed {@link URL#protocol} values for iparam type `URL`.
51
+ */
52
+ exports.IPARAM_URL_ALLOWED_PROTOCOLS = ['http:', 'https:'];
@@ -0,0 +1,46 @@
1
+ import { CBFileSystem } from '../node/file-system';
2
+ import { IparamDefns } from '../types/common';
3
+ /**
4
+ * Validator for iparamDefns
5
+ * Validates the sectioned iparams.json schema
6
+ */
7
+ export declare class IparamDefnsValidator {
8
+ private fileSystem;
9
+ private inputsValidator;
10
+ constructor(fileSystem: CBFileSystem);
11
+ /**
12
+ * Validates and loads the iparamDefns from a file
13
+ * @param {string} path - Path to the iparams.json file
14
+ * @returns {IparamDefns} The validated iparamDefns
15
+ * @throws {Error} If the iparamDefns is invalid or cannot be read
16
+ */
17
+ validateAndGetIparamDefnsFile(path: string): IparamDefns;
18
+ /**
19
+ * Validates an iparamDefns object
20
+ * @param {IparamDefns} iparamDefns - The iparamDefns object to validate
21
+ * @throws {Error} If the iparamDefns is invalid
22
+ */
23
+ validate(iparamDefns: IparamDefns): void;
24
+ /**
25
+ * Validates a single section definition
26
+ * @param {any} section - The section definition to validate
27
+ * @param {number} index - The index of the section in the array (for error messages)
28
+ * @throws {Error} If the section is invalid
29
+ */
30
+ private validateSection;
31
+ /**
32
+ * Validates a single parameter definition
33
+ * @param {any} param - The parameter definition to validate
34
+ * @param {string} sectionName - Parent section name for error messages
35
+ * @param {number} index - The index of the parameter in the section (for error messages)
36
+ * @throws {Error} If the parameter is invalid
37
+ */
38
+ private validateParameter;
39
+ private requireNonEmptyStringField;
40
+ private rejectUnknownKeys;
41
+ private validateSectionName;
42
+ private validateParamName;
43
+ private validateType;
44
+ private validateOptions;
45
+ private validateDefaultValue;
46
+ }