@socketsecurity/sdk 1.8.1 → 1.8.2
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/CHANGELOG.md +6 -0
- package/dist/constants.d.ts +5 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +128 -0
- package/dist/constants.js.map +1 -0
- package/dist/file-upload.d.ts +22 -0
- package/dist/file-upload.d.ts.map +1 -0
- package/dist/file-upload.js +152 -0
- package/dist/file-upload.js.map +1 -0
- package/dist/http-client.d.ts +79 -0
- package/dist/http-client.d.ts.map +1 -0
- package/dist/http-client.js +262 -0
- package/dist/http-client.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +53 -0
- package/dist/index.js.map +1 -0
- package/dist/quota-utils.d.ts +52 -0
- package/dist/quota-utils.d.ts.map +1 -0
- package/dist/quota-utils.js +150 -0
- package/dist/quota-utils.js.map +1 -0
- package/dist/socket-sdk-class.d.ts +415 -0
- package/dist/socket-sdk-class.d.ts.map +1 -0
- package/dist/socket-sdk-class.js +1336 -0
- package/dist/socket-sdk-class.js.map +1 -0
- package/dist/types.d.ts +151 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +4 -0
- package/dist/types.js.map +1 -0
- package/dist/user-agent.d.ts +14 -0
- package/dist/user-agent.d.ts.map +1 -0
- package/dist/user-agent.js +18 -0
- package/dist/user-agent.js.map +1 -0
- package/dist/utils.d.ts +28 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +94 -0
- package/dist/utils.js.map +1 -0
- package/package.json +3 -3
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ResponseError = void 0;
|
|
7
|
+
exports.createDeleteRequest = createDeleteRequest;
|
|
8
|
+
exports.createGetRequest = createGetRequest;
|
|
9
|
+
exports.createRequestWithJson = createRequestWithJson;
|
|
10
|
+
exports.getErrorResponseBody = getErrorResponseBody;
|
|
11
|
+
exports.getHttpModule = getHttpModule;
|
|
12
|
+
exports.getResponse = getResponse;
|
|
13
|
+
exports.getResponseJson = getResponseJson;
|
|
14
|
+
exports.isResponseOk = isResponseOk;
|
|
15
|
+
exports.reshapeArtifactForPublicPolicy = reshapeArtifactForPublicPolicy;
|
|
16
|
+
/**
|
|
17
|
+
* @fileoverview HTTP client utilities for Socket API communication.
|
|
18
|
+
* Provides low-level HTTP request handling with proper error management and response parsing.
|
|
19
|
+
*/
|
|
20
|
+
const node_http_1 = __importDefault(require("node:http"));
|
|
21
|
+
const node_https_1 = __importDefault(require("node:https"));
|
|
22
|
+
const debug_1 = require("@socketsecurity/registry/lib/debug");
|
|
23
|
+
const json_1 = require("@socketsecurity/registry/lib/json");
|
|
24
|
+
/**
|
|
25
|
+
* HTTP response error for Socket API requests.
|
|
26
|
+
* Extends Error with response details for debugging failed API calls.
|
|
27
|
+
*/
|
|
28
|
+
class ResponseError extends Error {
|
|
29
|
+
response;
|
|
30
|
+
/**
|
|
31
|
+
* Create a new ResponseError from an HTTP response.
|
|
32
|
+
* Automatically formats error message with status code and message.
|
|
33
|
+
*/
|
|
34
|
+
constructor(response, message = '') {
|
|
35
|
+
/* c8 ignore next 2 - statusCode and statusMessage may be undefined in edge cases */
|
|
36
|
+
const statusCode = response.statusCode ?? 'unknown';
|
|
37
|
+
const statusMessage = response.statusMessage ?? 'No status message';
|
|
38
|
+
super(
|
|
39
|
+
/* c8 ignore next - fallback empty message if not provided */
|
|
40
|
+
`Socket API ${message || 'Request failed'} (${statusCode}): ${statusMessage}`);
|
|
41
|
+
this.name = 'ResponseError';
|
|
42
|
+
this.response = response;
|
|
43
|
+
Error.captureStackTrace(this, ResponseError);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.ResponseError = ResponseError;
|
|
47
|
+
/**
|
|
48
|
+
* Create and execute an HTTP DELETE request.
|
|
49
|
+
* Returns the response stream for further processing.
|
|
50
|
+
*
|
|
51
|
+
* @throws {Error} When network or timeout errors occur
|
|
52
|
+
*/
|
|
53
|
+
async function createDeleteRequest(baseUrl, urlPath, options) {
|
|
54
|
+
const req = getHttpModule(baseUrl)
|
|
55
|
+
.request(`${baseUrl}${urlPath}`, {
|
|
56
|
+
method: 'DELETE',
|
|
57
|
+
...options,
|
|
58
|
+
})
|
|
59
|
+
.end();
|
|
60
|
+
return await getResponse(req);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Create and execute an HTTP GET request.
|
|
64
|
+
* Returns the response stream for further processing.
|
|
65
|
+
*
|
|
66
|
+
* @throws {Error} When network or timeout errors occur
|
|
67
|
+
*/
|
|
68
|
+
async function createGetRequest(baseUrl, urlPath, options) {
|
|
69
|
+
const req = getHttpModule(baseUrl)
|
|
70
|
+
.request(`${baseUrl}${urlPath}`, {
|
|
71
|
+
method: 'GET',
|
|
72
|
+
...options,
|
|
73
|
+
})
|
|
74
|
+
.end();
|
|
75
|
+
return await getResponse(req);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Create and execute an HTTP request with JSON payload.
|
|
79
|
+
* Automatically sets appropriate content headers and serializes the body.
|
|
80
|
+
*
|
|
81
|
+
* @throws {Error} When network or timeout errors occur
|
|
82
|
+
*/
|
|
83
|
+
async function createRequestWithJson(method, baseUrl, urlPath, json, options) {
|
|
84
|
+
const body = JSON.stringify(json);
|
|
85
|
+
const req = getHttpModule(baseUrl).request(`${baseUrl}${urlPath}`, {
|
|
86
|
+
method,
|
|
87
|
+
...options,
|
|
88
|
+
headers: {
|
|
89
|
+
...options.headers,
|
|
90
|
+
'Content-Length': Buffer.byteLength(body, 'utf8'),
|
|
91
|
+
'Content-Type': 'application/json',
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
req.write(body);
|
|
95
|
+
req.end();
|
|
96
|
+
return await getResponse(req);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Read the response body from an HTTP error response.
|
|
100
|
+
* Accumulates all chunks into a complete string for error handling.
|
|
101
|
+
*
|
|
102
|
+
* @throws {Error} When stream errors occur during reading
|
|
103
|
+
*/
|
|
104
|
+
async function getErrorResponseBody(response) {
|
|
105
|
+
return await new Promise((resolve, reject) => {
|
|
106
|
+
let body = '';
|
|
107
|
+
response.setEncoding('utf8');
|
|
108
|
+
response.on('data', (chunk) => (body += chunk));
|
|
109
|
+
response.on('end', () => resolve(body));
|
|
110
|
+
/* c8 ignore next - Extremely rare network or stream error during error response reading. */
|
|
111
|
+
response.on('error', e => reject(e));
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Get the appropriate HTTP module based on URL protocol.
|
|
116
|
+
* Returns http module for http: URLs, https module for https: URLs.
|
|
117
|
+
*/
|
|
118
|
+
function getHttpModule(url) {
|
|
119
|
+
return url.startsWith('https:') ? node_https_1.default : node_http_1.default;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Wait for and return the HTTP response from a request.
|
|
123
|
+
* Handles timeout and error conditions during request processing.
|
|
124
|
+
*
|
|
125
|
+
* @throws {Error} When request times out or network errors occur
|
|
126
|
+
*/
|
|
127
|
+
async function getResponse(req) {
|
|
128
|
+
return await new Promise((resolve, reject) => {
|
|
129
|
+
let timedOut = false;
|
|
130
|
+
req.on('response', (response) => {
|
|
131
|
+
/* c8 ignore next 3 - Race condition where response arrives after timeout. */
|
|
132
|
+
if (timedOut) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
resolve(response);
|
|
136
|
+
});
|
|
137
|
+
req.on('timeout', () => {
|
|
138
|
+
timedOut = true;
|
|
139
|
+
req.destroy();
|
|
140
|
+
reject(new Error('Request timed out'));
|
|
141
|
+
});
|
|
142
|
+
/* c8 ignore start - Network error handling during request, difficult to test reliably. */
|
|
143
|
+
req.on('error', e => {
|
|
144
|
+
if (!timedOut) {
|
|
145
|
+
reject(e);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
/* c8 ignore stop */
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Parse HTTP response body as JSON.
|
|
153
|
+
* Validates response status and handles empty responses gracefully.
|
|
154
|
+
*
|
|
155
|
+
* @throws {ResponseError} When response has non-2xx status code
|
|
156
|
+
* @throws {SyntaxError} When response body contains invalid JSON
|
|
157
|
+
*/
|
|
158
|
+
async function getResponseJson(response, method) {
|
|
159
|
+
if (!isResponseOk(response)) {
|
|
160
|
+
throw new ResponseError(response, method ? `${method} Request failed` : undefined);
|
|
161
|
+
}
|
|
162
|
+
const responseBody = await getErrorResponseBody(response);
|
|
163
|
+
// Handle truly empty responses (not whitespace) as valid empty objects.
|
|
164
|
+
if (responseBody === '') {
|
|
165
|
+
(0, debug_1.debugLog)('API response: empty response treated as {}');
|
|
166
|
+
return {};
|
|
167
|
+
}
|
|
168
|
+
try {
|
|
169
|
+
const responseJson = (0, json_1.jsonParse)(responseBody);
|
|
170
|
+
(0, debug_1.debugLog)('API response:', responseJson);
|
|
171
|
+
return responseJson;
|
|
172
|
+
}
|
|
173
|
+
catch (e) {
|
|
174
|
+
if (e instanceof SyntaxError) {
|
|
175
|
+
// Attach the original response text for better error reporting.
|
|
176
|
+
const enhancedError = new Error(`Socket API - Invalid JSON response:\n${responseBody}\n→ ${e.message}`);
|
|
177
|
+
enhancedError.name = 'SyntaxError';
|
|
178
|
+
enhancedError.originalResponse = responseBody;
|
|
179
|
+
Object.setPrototypeOf(enhancedError, SyntaxError.prototype);
|
|
180
|
+
throw enhancedError;
|
|
181
|
+
}
|
|
182
|
+
/* c8 ignore start - Error instanceof check and unknown error handling for JSON parsing edge cases. */
|
|
183
|
+
if (e instanceof Error) {
|
|
184
|
+
throw e;
|
|
185
|
+
}
|
|
186
|
+
// Handle non-Error objects thrown by JSON parsing.
|
|
187
|
+
const unknownError = new Error('Unknown error');
|
|
188
|
+
unknownError.name = 'SyntaxError';
|
|
189
|
+
unknownError.originalResponse = responseBody;
|
|
190
|
+
Object.setPrototypeOf(unknownError, SyntaxError.prototype);
|
|
191
|
+
throw unknownError;
|
|
192
|
+
/* c8 ignore stop */
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Check if HTTP response has a successful status code (2xx range).
|
|
197
|
+
* Returns true for status codes between 200-299, false otherwise.
|
|
198
|
+
*/
|
|
199
|
+
function isResponseOk(response) {
|
|
200
|
+
const { statusCode } = response;
|
|
201
|
+
/* c8 ignore next - Defensive fallback for edge cases where statusCode might be undefined. */
|
|
202
|
+
return statusCode ? statusCode >= 200 && statusCode < 300 : false;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Transform artifact data based on authentication status.
|
|
206
|
+
* Filters and compacts response data for public/free-tier users.
|
|
207
|
+
*/
|
|
208
|
+
function reshapeArtifactForPublicPolicy(data, isAuthenticated, actions) {
|
|
209
|
+
/* c8 ignore start - Public policy artifact reshaping for unauthenticated users, difficult to test edge cases. */
|
|
210
|
+
// If user is not authenticated, provide a different response structure
|
|
211
|
+
// optimized for the public free-tier experience.
|
|
212
|
+
if (!isAuthenticated) {
|
|
213
|
+
// Parse actions parameter for alert filtering.
|
|
214
|
+
const allowedActions = actions ? actions.split(',') : undefined;
|
|
215
|
+
const reshapeArtifact = (artifact) => ({
|
|
216
|
+
name: artifact.name,
|
|
217
|
+
version: artifact.version,
|
|
218
|
+
size: artifact.size,
|
|
219
|
+
author: artifact.author,
|
|
220
|
+
type: artifact.type,
|
|
221
|
+
supplyChainRisk: artifact.supplyChainRisk,
|
|
222
|
+
scorecards: artifact.scorecards,
|
|
223
|
+
topLevelAncestors: artifact.topLevelAncestors,
|
|
224
|
+
// Compact the alerts array to reduce response size for non-authenticated
|
|
225
|
+
// requests.
|
|
226
|
+
alerts: artifact.alerts
|
|
227
|
+
?.filter((alert) => {
|
|
228
|
+
// Filter by severity (remove low severity alerts).
|
|
229
|
+
if (alert.severity === 'low') {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
// Filter by actions if specified.
|
|
233
|
+
if (allowedActions &&
|
|
234
|
+
alert.action &&
|
|
235
|
+
!allowedActions.includes(alert.action)) {
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
return true;
|
|
239
|
+
})
|
|
240
|
+
.map((alert) => ({
|
|
241
|
+
type: alert.type,
|
|
242
|
+
severity: alert.severity,
|
|
243
|
+
key: alert.key,
|
|
244
|
+
})),
|
|
245
|
+
});
|
|
246
|
+
// Handle both single artifacts and objects with artifacts arrays.
|
|
247
|
+
if (data['artifacts']) {
|
|
248
|
+
// Object with artifacts array.
|
|
249
|
+
return {
|
|
250
|
+
...data,
|
|
251
|
+
artifacts: data['artifacts']?.map(reshapeArtifact),
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
else if (data['alerts']) {
|
|
255
|
+
// Single artifact with alerts.
|
|
256
|
+
return reshapeArtifact(data);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return data;
|
|
260
|
+
/* c8 ignore stop */
|
|
261
|
+
}
|
|
262
|
+
//# sourceMappingURL=http-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http-client.js","sourceRoot":"","sources":["../src/http-client.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA;;;GAGG;AACH,0DAA4B;AAC5B,4DAA8B;AAE9B,8DAA6D;AAC7D,4DAA6D;AAU7D;;;GAGG;AACH,mBAA2B,SAAQ,KAAK;IACtC,QAAQ,CAAiB;IAEzB;;;OAGG;IACH,YAAY,QAAyB,EAAE,OAAO,GAAW,EAAE,EAAE;QAC3D,oFAAoF;QACpF,MAAM,UAAU,GAAG,QAAQ,CAAC,UAAU,IAAI,SAAS,CAAA;QACnD,MAAM,aAAa,GAAG,QAAQ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CAAC;aAAM,IAAI,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC1B,+BAA+B;YAC/B,OAAO,eAAe,CACpB,IAA2C,CAC5B,CAAA;QACnB,CAAC;IACH,CAAC;IACD,OAAO,IAAI,CAAA;IACX,oBAAoB;AADT,CAEZ"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Main entry point for the Socket SDK.
|
|
3
|
+
* Provides the SocketSdk class and utility functions for Socket security analysis API interactions.
|
|
4
|
+
*/
|
|
5
|
+
import { DEFAULT_USER_AGENT, httpAgentNames, publicPolicy } from './constants';
|
|
6
|
+
import { normalizeBaseUrl, promiseWithResolvers, queryToSearchParams, resolveAbsPaths, resolveBasePath } from './utils';
|
|
7
|
+
export type * from './types';
|
|
8
|
+
export { createUserAgentFromPkgJson } from './user-agent';
|
|
9
|
+
export { createDeleteRequest, createGetRequest, createRequestWithJson, getErrorResponseBody, getHttpModule, getResponse, getResponseJson, isResponseOk, reshapeArtifactForPublicPolicy, ResponseError, } from './http-client';
|
|
10
|
+
export { createRequestBodyForFilepaths, createRequestBodyForJson, createUploadRequest, } from './file-upload';
|
|
11
|
+
export { SocketSdk } from './socket-sdk-class';
|
|
12
|
+
export { calculateTotalQuotaCost, getAllMethodRequirements, getMethodRequirements, getMethodsByPermissions, getMethodsByQuotaCost, getQuotaCost, getQuotaUsageSummary, getRequiredPermissions, hasQuotaForMethods, } from './quota-utils';
|
|
13
|
+
export { normalizeBaseUrl, promiseWithResolvers, queryToSearchParams, resolveAbsPaths, resolveBasePath, };
|
|
14
|
+
export { DEFAULT_USER_AGENT, httpAgentNames, publicPolicy };
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,kBAAkB,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC9E,OAAO,EACL,gBAAgB,EAChB,oBAAoB,EACpB,mBAAmB,EACnB,eAAe,EACf,eAAe,EAChB,MAAM,SAAS,CAAA;AAGhB,mBAAmB,SAAS,CAAA;AAG5B,OAAO,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAA;AAGzD,OAAO,EACL,mBAAmB,EACnB,gBAAgB,EAChB,qBAAqB,EACrB,oBAAoB,EACpB,aAAa,EACb,WAAW,EACX,eAAe,EACf,YAAY,EACZ,8BAA8B,EAC9B,aAAa,GACd,MAAM,eAAe,CAAA;AAGtB,OAAO,EACL,6BAA6B,EAC7B,wBAAwB,EACxB,mBAAmB,GACpB,MAAM,eAAe,CAAA;AAGtB,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAG9C,OAAO,EACL,uBAAuB,EACvB,wBAAwB,EACxB,qBAAqB,EACrB,uBAAuB,EACvB,qBAAqB,EACrB,YAAY,EACZ,oBAAoB,EACpB,sBAAsB,EACtB,kBAAkB,GACnB,MAAM,eAAe,CAAA;AAGtB,OAAO,EACL,gBAAgB,EAChB,oBAAoB,EACpB,mBAAmB,EACnB,eAAe,EACf,eAAe,GAChB,CAAA;AAGD,OAAO,EAAE,kBAAkB,EAAE,cAAc,EAAE,YAAY,EAAE,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.publicPolicy = exports.httpAgentNames = exports.DEFAULT_USER_AGENT = exports.resolveBasePath = exports.resolveAbsPaths = exports.queryToSearchParams = exports.promiseWithResolvers = exports.normalizeBaseUrl = exports.hasQuotaForMethods = exports.getRequiredPermissions = exports.getQuotaUsageSummary = exports.getQuotaCost = exports.getMethodsByQuotaCost = exports.getMethodsByPermissions = exports.getMethodRequirements = exports.getAllMethodRequirements = exports.calculateTotalQuotaCost = exports.SocketSdk = exports.createUploadRequest = exports.createRequestBodyForJson = exports.createRequestBodyForFilepaths = exports.ResponseError = exports.reshapeArtifactForPublicPolicy = exports.isResponseOk = exports.getResponseJson = exports.getResponse = exports.getHttpModule = exports.getErrorResponseBody = exports.createRequestWithJson = exports.createGetRequest = exports.createDeleteRequest = exports.createUserAgentFromPkgJson = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @fileoverview Main entry point for the Socket SDK.
|
|
6
|
+
* Provides the SocketSdk class and utility functions for Socket security analysis API interactions.
|
|
7
|
+
*/
|
|
8
|
+
// Import from our modules
|
|
9
|
+
const constants_1 = require("./constants");
|
|
10
|
+
Object.defineProperty(exports, "DEFAULT_USER_AGENT", { enumerable: true, get: function () { return constants_1.DEFAULT_USER_AGENT; } });
|
|
11
|
+
Object.defineProperty(exports, "httpAgentNames", { enumerable: true, get: function () { return constants_1.httpAgentNames; } });
|
|
12
|
+
Object.defineProperty(exports, "publicPolicy", { enumerable: true, get: function () { return constants_1.publicPolicy; } });
|
|
13
|
+
const utils_1 = require("./utils");
|
|
14
|
+
Object.defineProperty(exports, "normalizeBaseUrl", { enumerable: true, get: function () { return utils_1.normalizeBaseUrl; } });
|
|
15
|
+
Object.defineProperty(exports, "promiseWithResolvers", { enumerable: true, get: function () { return utils_1.promiseWithResolvers; } });
|
|
16
|
+
Object.defineProperty(exports, "queryToSearchParams", { enumerable: true, get: function () { return utils_1.queryToSearchParams; } });
|
|
17
|
+
Object.defineProperty(exports, "resolveAbsPaths", { enumerable: true, get: function () { return utils_1.resolveAbsPaths; } });
|
|
18
|
+
Object.defineProperty(exports, "resolveBasePath", { enumerable: true, get: function () { return utils_1.resolveBasePath; } });
|
|
19
|
+
// Re-export functions from modules
|
|
20
|
+
const user_agent_1 = require("./user-agent");
|
|
21
|
+
Object.defineProperty(exports, "createUserAgentFromPkgJson", { enumerable: true, get: function () { return user_agent_1.createUserAgentFromPkgJson; } });
|
|
22
|
+
// Re-export HTTP client functions
|
|
23
|
+
const http_client_1 = require("./http-client");
|
|
24
|
+
Object.defineProperty(exports, "createDeleteRequest", { enumerable: true, get: function () { return http_client_1.createDeleteRequest; } });
|
|
25
|
+
Object.defineProperty(exports, "createGetRequest", { enumerable: true, get: function () { return http_client_1.createGetRequest; } });
|
|
26
|
+
Object.defineProperty(exports, "createRequestWithJson", { enumerable: true, get: function () { return http_client_1.createRequestWithJson; } });
|
|
27
|
+
Object.defineProperty(exports, "getErrorResponseBody", { enumerable: true, get: function () { return http_client_1.getErrorResponseBody; } });
|
|
28
|
+
Object.defineProperty(exports, "getHttpModule", { enumerable: true, get: function () { return http_client_1.getHttpModule; } });
|
|
29
|
+
Object.defineProperty(exports, "getResponse", { enumerable: true, get: function () { return http_client_1.getResponse; } });
|
|
30
|
+
Object.defineProperty(exports, "getResponseJson", { enumerable: true, get: function () { return http_client_1.getResponseJson; } });
|
|
31
|
+
Object.defineProperty(exports, "isResponseOk", { enumerable: true, get: function () { return http_client_1.isResponseOk; } });
|
|
32
|
+
Object.defineProperty(exports, "reshapeArtifactForPublicPolicy", { enumerable: true, get: function () { return http_client_1.reshapeArtifactForPublicPolicy; } });
|
|
33
|
+
Object.defineProperty(exports, "ResponseError", { enumerable: true, get: function () { return http_client_1.ResponseError; } });
|
|
34
|
+
// Re-export file upload functions
|
|
35
|
+
const file_upload_1 = require("./file-upload");
|
|
36
|
+
Object.defineProperty(exports, "createRequestBodyForFilepaths", { enumerable: true, get: function () { return file_upload_1.createRequestBodyForFilepaths; } });
|
|
37
|
+
Object.defineProperty(exports, "createRequestBodyForJson", { enumerable: true, get: function () { return file_upload_1.createRequestBodyForJson; } });
|
|
38
|
+
Object.defineProperty(exports, "createUploadRequest", { enumerable: true, get: function () { return file_upload_1.createUploadRequest; } });
|
|
39
|
+
// Re-export the main SocketSdk class
|
|
40
|
+
const socket_sdk_class_1 = require("./socket-sdk-class");
|
|
41
|
+
Object.defineProperty(exports, "SocketSdk", { enumerable: true, get: function () { return socket_sdk_class_1.SocketSdk; } });
|
|
42
|
+
// Re-export quota utility functions
|
|
43
|
+
const quota_utils_1 = require("./quota-utils");
|
|
44
|
+
Object.defineProperty(exports, "calculateTotalQuotaCost", { enumerable: true, get: function () { return quota_utils_1.calculateTotalQuotaCost; } });
|
|
45
|
+
Object.defineProperty(exports, "getAllMethodRequirements", { enumerable: true, get: function () { return quota_utils_1.getAllMethodRequirements; } });
|
|
46
|
+
Object.defineProperty(exports, "getMethodRequirements", { enumerable: true, get: function () { return quota_utils_1.getMethodRequirements; } });
|
|
47
|
+
Object.defineProperty(exports, "getMethodsByPermissions", { enumerable: true, get: function () { return quota_utils_1.getMethodsByPermissions; } });
|
|
48
|
+
Object.defineProperty(exports, "getMethodsByQuotaCost", { enumerable: true, get: function () { return quota_utils_1.getMethodsByQuotaCost; } });
|
|
49
|
+
Object.defineProperty(exports, "getQuotaCost", { enumerable: true, get: function () { return quota_utils_1.getQuotaCost; } });
|
|
50
|
+
Object.defineProperty(exports, "getQuotaUsageSummary", { enumerable: true, get: function () { return quota_utils_1.getQuotaUsageSummary; } });
|
|
51
|
+
Object.defineProperty(exports, "getRequiredPermissions", { enumerable: true, get: function () { return quota_utils_1.getRequiredPermissions; } });
|
|
52
|
+
Object.defineProperty(exports, "hasQuotaForMethods", { enumerable: true, get: function () { return quota_utils_1.hasQuotaForMethods; } });
|
|
53
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA;;;GAGG;AAEH,0BAA0B;AAC1B,2CAA8E;mGAArE,8BAAkB;+FAAE,0BAAc;6FAAE,wBAAY;AACzD,mCAMgB;iGALd,wBAAgB;qGAChB,4BAAoB;oGACpB,2BAAmB;gGACnB,uBAAe;gGACf,uBAAe;AAMjB,mCAAmC;AACnC,6CAAyD;AAAhD,wHAAA,0BAA0B,OAAA;AAEnC,kCAAkC;AAClC,+CAWsB;AAVpB,kHAAA,mBAAmB,OAAA;AACnB,+GAAA,gBAAgB,OAAA;AAChB,oHAAA,qBAAqB,OAAA;AACrB,mHAAA,oBAAoB,OAAA;AACpB,4GAAA,aAAa,OAAA;AACb,0GAAA,WAAW,OAAA;AACX,8GAAA,eAAe,OAAA;AACf,2GAAA,YAAY,OAAA;AACZ,6HAAA,8BAA8B,OAAA;AAC9B,4GAAA,aAAa,OAAA;AAGf,kCAAkC;AAClC,+CAIsB;AAHpB,4HAAA,6BAA6B,OAAA;AAC7B,uHAAA,wBAAwB,OAAA;AACxB,kHAAA,mBAAmB,OAAA;AAGrB,qCAAqC;AACrC,yDAA8C;AAArC,6GAAA,SAAS,OAAA;AAElB,oCAAoC;AACpC,+CAUsB;AATpB,sHAAA,uBAAuB,OAAA;AACvB,uHAAA,wBAAwB,OAAA;AACxB,oHAAA,qBAAqB,OAAA;AACrB,sHAAA,uBAAuB,OAAA;AACvB,oHAAA,qBAAqB,OAAA;AACrB,2GAAA,YAAY,OAAA;AACZ,mHAAA,oBAAoB,OAAA;AACpB,qHAAA,sBAAsB,OAAA;AACtB,iHAAA,kBAAkB,OAAA"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { SocketSdkOperations } from './types';
|
|
2
|
+
interface ApiRequirement {
|
|
3
|
+
quota: number;
|
|
4
|
+
permissions: string[];
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Get quota cost for a specific SDK method.
|
|
8
|
+
* Returns the number of quota units consumed by the method.
|
|
9
|
+
*/
|
|
10
|
+
export declare function getQuotaCost(methodName: SocketSdkOperations | string): number;
|
|
11
|
+
/**
|
|
12
|
+
* Get required permissions for a specific SDK method.
|
|
13
|
+
* Returns array of permission strings needed to call the method.
|
|
14
|
+
*/
|
|
15
|
+
export declare function getRequiredPermissions(methodName: SocketSdkOperations | string): string[];
|
|
16
|
+
/**
|
|
17
|
+
* Get complete requirement information for a SDK method.
|
|
18
|
+
* Returns both quota cost and required permissions.
|
|
19
|
+
*/
|
|
20
|
+
export declare function getMethodRequirements(methodName: SocketSdkOperations | string): ApiRequirement;
|
|
21
|
+
/**
|
|
22
|
+
* Calculate total quota cost for multiple SDK method calls.
|
|
23
|
+
* Returns sum of quota units for all specified methods.
|
|
24
|
+
*/
|
|
25
|
+
export declare function calculateTotalQuotaCost(methodNames: Array<SocketSdkOperations | string>): number;
|
|
26
|
+
/**
|
|
27
|
+
* Get all methods that consume a specific quota amount.
|
|
28
|
+
* Useful for finding high-cost or free operations.
|
|
29
|
+
*/
|
|
30
|
+
export declare function getMethodsByQuotaCost(quotaCost: number): string[];
|
|
31
|
+
/**
|
|
32
|
+
* Get all methods that require specific permissions.
|
|
33
|
+
* Returns methods that need any of the specified permissions.
|
|
34
|
+
*/
|
|
35
|
+
export declare function getMethodsByPermissions(permissions: string[]): string[];
|
|
36
|
+
/**
|
|
37
|
+
* Check if user has sufficient quota for method calls.
|
|
38
|
+
* Returns true if available quota covers the total cost.
|
|
39
|
+
*/
|
|
40
|
+
export declare function hasQuotaForMethods(availableQuota: number, methodNames: Array<SocketSdkOperations | string>): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Get quota usage summary grouped by cost levels.
|
|
43
|
+
* Returns methods categorized by their quota consumption.
|
|
44
|
+
*/
|
|
45
|
+
export declare function getQuotaUsageSummary(): Record<string, string[]>;
|
|
46
|
+
/**
|
|
47
|
+
* Get all available SDK methods with their requirements.
|
|
48
|
+
* Returns complete mapping of methods to quota and permissions.
|
|
49
|
+
*/
|
|
50
|
+
export declare function getAllMethodRequirements(): Record<string, ApiRequirement>;
|
|
51
|
+
export {};
|
|
52
|
+
//# sourceMappingURL=quota-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"quota-utils.d.ts","sourceRoot":"","sources":["../src/quota-utils.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAA;AAElD,UAAU,cAAc;IACtB,KAAK,EAAE,MAAM,CAAA;IACb,WAAW,EAAE,MAAM,EAAE,CAAA;CACtB;AA4BD;;;GAGG;AACH,wBAAgB,YAAY,CAAC,UAAU,EAAE,mBAAmB,GAAG,MAAM,GAAG,MAAM,CAS7E;AAED;;;GAGG;AACH,wBAAgB,sBAAsB,CACpC,UAAU,EAAE,mBAAmB,GAAG,MAAM,GACvC,MAAM,EAAE,CASV;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CACnC,UAAU,EAAE,mBAAmB,GAAG,MAAM,GACvC,cAAc,CAYhB;AAED;;;GAGG;AACH,wBAAgB,uBAAuB,CACrC,WAAW,EAAE,KAAK,CAAC,mBAAmB,GAAG,MAAM,CAAC,GAC/C,MAAM,CAIR;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,CAOjE;AAED;;;GAGG;AACH,wBAAgB,uBAAuB,CAAC,WAAW,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAWvE;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAChC,cAAc,EAAE,MAAM,EACtB,WAAW,EAAE,KAAK,CAAC,mBAAmB,GAAG,MAAM,CAAC,GAC/C,OAAO,CAGT;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAoB/D;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,IAAI,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAYzE"}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getQuotaCost = getQuotaCost;
|
|
4
|
+
exports.getRequiredPermissions = getRequiredPermissions;
|
|
5
|
+
exports.getMethodRequirements = getMethodRequirements;
|
|
6
|
+
exports.calculateTotalQuotaCost = calculateTotalQuotaCost;
|
|
7
|
+
exports.getMethodsByQuotaCost = getMethodsByQuotaCost;
|
|
8
|
+
exports.getMethodsByPermissions = getMethodsByPermissions;
|
|
9
|
+
exports.hasQuotaForMethods = hasQuotaForMethods;
|
|
10
|
+
exports.getQuotaUsageSummary = getQuotaUsageSummary;
|
|
11
|
+
exports.getAllMethodRequirements = getAllMethodRequirements;
|
|
12
|
+
/** @fileoverview Quota utility functions for Socket SDK method cost lookup. */
|
|
13
|
+
const node_fs_1 = require("node:fs");
|
|
14
|
+
const node_path_1 = require("node:path");
|
|
15
|
+
let requirements = null;
|
|
16
|
+
/**
|
|
17
|
+
* Load requirements.json data with caching.
|
|
18
|
+
* Internal function for lazy loading quota requirements.
|
|
19
|
+
*/
|
|
20
|
+
function loadRequirements() {
|
|
21
|
+
if (requirements) {
|
|
22
|
+
return requirements;
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
// Resolve path relative to current working directory
|
|
26
|
+
const requirementsPath = (0, node_path_1.join)(process.cwd(), 'requirements.json');
|
|
27
|
+
const data = (0, node_fs_1.readFileSync)(requirementsPath, 'utf8');
|
|
28
|
+
requirements = JSON.parse(data);
|
|
29
|
+
return requirements;
|
|
30
|
+
}
|
|
31
|
+
catch (e) {
|
|
32
|
+
throw new Error('Failed to load requirements.json', { cause: e });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Get quota cost for a specific SDK method.
|
|
37
|
+
* Returns the number of quota units consumed by the method.
|
|
38
|
+
*/
|
|
39
|
+
function getQuotaCost(methodName) {
|
|
40
|
+
const reqs = loadRequirements();
|
|
41
|
+
const requirement = reqs.api[methodName];
|
|
42
|
+
if (!requirement) {
|
|
43
|
+
throw new Error(`Unknown SDK method: ${methodName}`);
|
|
44
|
+
}
|
|
45
|
+
return requirement.quota;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Get required permissions for a specific SDK method.
|
|
49
|
+
* Returns array of permission strings needed to call the method.
|
|
50
|
+
*/
|
|
51
|
+
function getRequiredPermissions(methodName) {
|
|
52
|
+
const reqs = loadRequirements();
|
|
53
|
+
const requirement = reqs.api[methodName];
|
|
54
|
+
if (!requirement) {
|
|
55
|
+
throw new Error(`Unknown SDK method: ${methodName}`);
|
|
56
|
+
}
|
|
57
|
+
return [...requirement.permissions];
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Get complete requirement information for a SDK method.
|
|
61
|
+
* Returns both quota cost and required permissions.
|
|
62
|
+
*/
|
|
63
|
+
function getMethodRequirements(methodName) {
|
|
64
|
+
const reqs = loadRequirements();
|
|
65
|
+
const requirement = reqs.api[methodName];
|
|
66
|
+
if (!requirement) {
|
|
67
|
+
throw new Error(`Unknown SDK method: ${methodName}`);
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
quota: requirement.quota,
|
|
71
|
+
permissions: [...requirement.permissions],
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Calculate total quota cost for multiple SDK method calls.
|
|
76
|
+
* Returns sum of quota units for all specified methods.
|
|
77
|
+
*/
|
|
78
|
+
function calculateTotalQuotaCost(methodNames) {
|
|
79
|
+
return methodNames.reduce((total, methodName) => {
|
|
80
|
+
return total + getQuotaCost(methodName);
|
|
81
|
+
}, 0);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Get all methods that consume a specific quota amount.
|
|
85
|
+
* Useful for finding high-cost or free operations.
|
|
86
|
+
*/
|
|
87
|
+
function getMethodsByQuotaCost(quotaCost) {
|
|
88
|
+
const reqs = loadRequirements();
|
|
89
|
+
return Object.entries(reqs.api)
|
|
90
|
+
.filter(([, requirement]) => requirement.quota === quotaCost)
|
|
91
|
+
.map(([methodName]) => methodName)
|
|
92
|
+
.sort();
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Get all methods that require specific permissions.
|
|
96
|
+
* Returns methods that need any of the specified permissions.
|
|
97
|
+
*/
|
|
98
|
+
function getMethodsByPermissions(permissions) {
|
|
99
|
+
const reqs = loadRequirements();
|
|
100
|
+
return Object.entries(reqs.api)
|
|
101
|
+
.filter(([, requirement]) => {
|
|
102
|
+
return permissions.some(permission => requirement.permissions.includes(permission));
|
|
103
|
+
})
|
|
104
|
+
.map(([methodName]) => methodName)
|
|
105
|
+
.sort();
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Check if user has sufficient quota for method calls.
|
|
109
|
+
* Returns true if available quota covers the total cost.
|
|
110
|
+
*/
|
|
111
|
+
function hasQuotaForMethods(availableQuota, methodNames) {
|
|
112
|
+
const totalCost = calculateTotalQuotaCost(methodNames);
|
|
113
|
+
return availableQuota >= totalCost;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Get quota usage summary grouped by cost levels.
|
|
117
|
+
* Returns methods categorized by their quota consumption.
|
|
118
|
+
*/
|
|
119
|
+
function getQuotaUsageSummary() {
|
|
120
|
+
const reqs = loadRequirements();
|
|
121
|
+
const summary = {};
|
|
122
|
+
Object.entries(reqs.api).forEach(([methodName, requirement]) => {
|
|
123
|
+
const costKey = `${requirement.quota} units`;
|
|
124
|
+
if (!summary[costKey]) {
|
|
125
|
+
summary[costKey] = [];
|
|
126
|
+
}
|
|
127
|
+
summary[costKey].push(methodName);
|
|
128
|
+
});
|
|
129
|
+
// Sort methods within each cost level
|
|
130
|
+
Object.keys(summary).forEach(costKey => {
|
|
131
|
+
summary[costKey]?.sort();
|
|
132
|
+
});
|
|
133
|
+
return summary;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Get all available SDK methods with their requirements.
|
|
137
|
+
* Returns complete mapping of methods to quota and permissions.
|
|
138
|
+
*/
|
|
139
|
+
function getAllMethodRequirements() {
|
|
140
|
+
const reqs = loadRequirements();
|
|
141
|
+
const result = {};
|
|
142
|
+
Object.entries(reqs.api).forEach(([methodName, requirement]) => {
|
|
143
|
+
result[methodName] = {
|
|
144
|
+
quota: requirement.quota,
|
|
145
|
+
permissions: [...requirement.permissions],
|
|
146
|
+
};
|
|
147
|
+
});
|
|
148
|
+
return result;
|
|
149
|
+
}
|
|
150
|
+
//# sourceMappingURL=quota-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"quota-utils.js","sourceRoot":"","sources":["../src/quota-utils.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,+EAA+E;AAC/E,qCAAsC;AACtC,yCAAgC;AAahC,IAAI,YAAY,GAAwB,IAAI,CAAA;AAE5C;;;GAGG;AACH,SAAS,gBAAgB,GAAiB;IACxC,IAAI,YAAY,EAAE,CAAC;QACjB,OAAO,YAAY,CAAA;IACrB,CAAC;IAED,IAAI,CAAC;QACH,qDAAqD;QACrD,MAAM,gBAAgB,GAAG,IAAA,gBAAI,EAAC,OAAO,CAAC,GAAG,EAAE,EAAE,mBAAmB,CAAC,CAAA;QACjE,MAAM,IAAI,GAAG,IAAA,sBAAY,EAAC,gBAAgB,EAAE,MAAM,CAAC,CAAA;QACnD,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAiB,CAAA;QAC/C,OAAO,YAAY,CAAA;IACrB,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,MAAM,IAAI,KAAK,CAAC,kCAAkC,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAA;IACnE,CAAC;AAAA,CACF;AAED;;;GAGG;AACH,sBAA6B,UAAwC,EAAU;IAC7E,MAAM,IAAI,GAAG,gBAAgB,EAAE,CAAA;IAC/B,MAAM,WAAW,GAAG,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,CAAA;IAExC,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,MAAM,IAAI,KAAK,CAAC,uBAAuB,UAAU,EAAE,CAAC,CAAA;IACtD,CAAC;IAED,OAAO,WAAW,CAAC,KAAK,CAAA;AAAA,CACzB;AAED;;;GAGG;AACH,gCACE,UAAwC,EAC9B;IACV,MAAM,IAAI,GAAG,gBAAgB,EAAE,CAAA;IAC/B,MAAM,WAAW,GAAG,IAAI,CAAC,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