@socketsecurity/sdk 1.10.1 → 1.11.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/CHANGELOG.md +19 -0
- package/README.md +153 -311
- package/dist/constants.js +23 -117
- package/dist/file-upload.js +118 -127
- package/dist/http-client.d.ts +11 -8
- package/dist/http-client.js +281 -223
- package/dist/index.js +43 -48
- package/dist/package.json.js +212 -0
- package/dist/quota-utils.d.ts +13 -6
- package/dist/quota-utils.js +128 -105
- package/dist/socket-sdk-class.d.ts +1 -1
- package/dist/socket-sdk-class.js +1488 -1407
- package/dist/testing.d.ts +453 -0
- package/dist/testing.js +387 -0
- package/dist/types.d.ts +26 -0
- package/dist/user-agent.js +11 -7
- package/dist/utils.d.ts +2 -1
- package/dist/utils.js +66 -60
- package/package.json +27 -6
- package/dist/promise-queue.js +0 -91
- package/dist/types.js +0 -3
package/dist/index.js
CHANGED
|
@@ -1,52 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var constants = require('./constants.js');
|
|
4
|
+
var utils = require('./utils.js');
|
|
5
|
+
var fileUpload = require('./file-upload.js');
|
|
6
|
+
var httpClient = require('./http-client.js');
|
|
7
|
+
var quotaUtils = require('./quota-utils.js');
|
|
8
|
+
var socketSdkClass = require('./socket-sdk-class.js');
|
|
9
|
+
var userAgent = require('./user-agent.js');
|
|
10
|
+
|
|
4
11
|
/**
|
|
5
12
|
* @fileoverview Main entry point for the Socket SDK.
|
|
6
13
|
* Provides the SocketSdk class and utility functions for Socket security analysis API interactions.
|
|
7
14
|
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
Object.defineProperty(exports, "getMethodsByPermissions", { enumerable: true, get: function () { return quota_utils_1.getMethodsByPermissions; } });
|
|
42
|
-
Object.defineProperty(exports, "getMethodsByQuotaCost", { enumerable: true, get: function () { return quota_utils_1.getMethodsByQuotaCost; } });
|
|
43
|
-
Object.defineProperty(exports, "getQuotaCost", { enumerable: true, get: function () { return quota_utils_1.getQuotaCost; } });
|
|
44
|
-
Object.defineProperty(exports, "getQuotaUsageSummary", { enumerable: true, get: function () { return quota_utils_1.getQuotaUsageSummary; } });
|
|
45
|
-
Object.defineProperty(exports, "getRequiredPermissions", { enumerable: true, get: function () { return quota_utils_1.getRequiredPermissions; } });
|
|
46
|
-
Object.defineProperty(exports, "hasQuotaForMethods", { enumerable: true, get: function () { return quota_utils_1.hasQuotaForMethods; } });
|
|
47
|
-
// Re-export the main SocketSdk class
|
|
48
|
-
const socket_sdk_class_1 = require("./socket-sdk-class");
|
|
49
|
-
Object.defineProperty(exports, "SocketSdk", { enumerable: true, get: function () { return socket_sdk_class_1.SocketSdk; } });
|
|
50
|
-
// Re-export functions from modules
|
|
51
|
-
const user_agent_1 = require("./user-agent");
|
|
52
|
-
Object.defineProperty(exports, "createUserAgentFromPkgJson", { enumerable: true, get: function () { return user_agent_1.createUserAgentFromPkgJson; } });
|
|
15
|
+
|
|
16
|
+
exports.DEFAULT_USER_AGENT = constants.DEFAULT_USER_AGENT;
|
|
17
|
+
exports.httpAgentNames = constants.httpAgentNames;
|
|
18
|
+
exports.publicPolicy = constants.publicPolicy;
|
|
19
|
+
exports.normalizeBaseUrl = utils.normalizeBaseUrl;
|
|
20
|
+
exports.promiseWithResolvers = utils.promiseWithResolvers;
|
|
21
|
+
exports.queryToSearchParams = utils.queryToSearchParams;
|
|
22
|
+
exports.resolveAbsPaths = utils.resolveAbsPaths;
|
|
23
|
+
exports.resolveBasePath = utils.resolveBasePath;
|
|
24
|
+
exports.createRequestBodyForFilepaths = fileUpload.createRequestBodyForFilepaths;
|
|
25
|
+
exports.createRequestBodyForJson = fileUpload.createRequestBodyForJson;
|
|
26
|
+
exports.createUploadRequest = fileUpload.createUploadRequest;
|
|
27
|
+
exports.ResponseError = httpClient.ResponseError;
|
|
28
|
+
exports.createDeleteRequest = httpClient.createDeleteRequest;
|
|
29
|
+
exports.createGetRequest = httpClient.createGetRequest;
|
|
30
|
+
exports.createRequestWithJson = httpClient.createRequestWithJson;
|
|
31
|
+
exports.getErrorResponseBody = httpClient.getErrorResponseBody;
|
|
32
|
+
exports.getHttpModule = httpClient.getHttpModule;
|
|
33
|
+
exports.getResponse = httpClient.getResponse;
|
|
34
|
+
exports.getResponseJson = httpClient.getResponseJson;
|
|
35
|
+
exports.isResponseOk = httpClient.isResponseOk;
|
|
36
|
+
exports.reshapeArtifactForPublicPolicy = httpClient.reshapeArtifactForPublicPolicy;
|
|
37
|
+
exports.calculateTotalQuotaCost = quotaUtils.calculateTotalQuotaCost;
|
|
38
|
+
exports.getAllMethodRequirements = quotaUtils.getAllMethodRequirements;
|
|
39
|
+
exports.getMethodRequirements = quotaUtils.getMethodRequirements;
|
|
40
|
+
exports.getMethodsByPermissions = quotaUtils.getMethodsByPermissions;
|
|
41
|
+
exports.getMethodsByQuotaCost = quotaUtils.getMethodsByQuotaCost;
|
|
42
|
+
exports.getQuotaCost = quotaUtils.getQuotaCost;
|
|
43
|
+
exports.getQuotaUsageSummary = quotaUtils.getQuotaUsageSummary;
|
|
44
|
+
exports.getRequiredPermissions = quotaUtils.getRequiredPermissions;
|
|
45
|
+
exports.hasQuotaForMethods = quotaUtils.hasQuotaForMethods;
|
|
46
|
+
exports.SocketSdk = socketSdkClass.SocketSdk;
|
|
47
|
+
exports.createUserAgentFromPkgJson = userAgent.createUserAgentFromPkgJson;
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var name = "@socketsecurity/sdk";
|
|
6
|
+
var version = "1.11.1";
|
|
7
|
+
var license = "MIT";
|
|
8
|
+
var description = "SDK for the Socket API client";
|
|
9
|
+
var author = {
|
|
10
|
+
name: "Socket Inc",
|
|
11
|
+
email: "eng@socket.dev",
|
|
12
|
+
url: "https://socket.dev"
|
|
13
|
+
};
|
|
14
|
+
var homepage = "https://github.com/SocketDev/socket-sdk-js";
|
|
15
|
+
var repository = {
|
|
16
|
+
type: "git",
|
|
17
|
+
url: "git://github.com/SocketDev/socket-sdk-js.git"
|
|
18
|
+
};
|
|
19
|
+
var type = "commonjs";
|
|
20
|
+
var main = "./dist/index.js";
|
|
21
|
+
var types = "./dist/index.d.ts";
|
|
22
|
+
var exports$1 = {
|
|
23
|
+
".": {
|
|
24
|
+
types: "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./testing": {
|
|
28
|
+
types: "./dist/testing.d.ts",
|
|
29
|
+
"default": "./dist/testing.js"
|
|
30
|
+
},
|
|
31
|
+
"./types/api-helpers": "./types/api-helpers.d.ts",
|
|
32
|
+
"./types/api-helpers.d.ts": "./types/api-helpers.d.ts",
|
|
33
|
+
"./types/api": "./types/api.d.ts",
|
|
34
|
+
"./types/api.d.ts": "./types/api.d.ts",
|
|
35
|
+
"./package.json": "./package.json"
|
|
36
|
+
};
|
|
37
|
+
var scripts = {
|
|
38
|
+
build: "pnpm run build:dist",
|
|
39
|
+
"build:dist": "pnpm run build:dist:src && pnpm run build:dist:types",
|
|
40
|
+
"build:dist:src": "pnpm run clean:dist && rollup -c .config/rollup.dist.config.mjs",
|
|
41
|
+
"build:dist:types": "pnpm run clean:dist:types && tsgo --project tsconfig.dts.json",
|
|
42
|
+
check: "run-p -c --aggregate-output check:*",
|
|
43
|
+
"check:lint": "eslint --config .config/eslint.config.mjs --report-unused-disable-directives .",
|
|
44
|
+
"check:lint:fix": "pnpm run check:lint -- --fix",
|
|
45
|
+
"check:tsc": "tsgo --noEmit -p .config/tsconfig.check.json",
|
|
46
|
+
"lint-ci": "pnpm run check:lint",
|
|
47
|
+
coverage: "run-s coverage:*",
|
|
48
|
+
"coverage:test": "run-s pretest:unit test:unit:coverage",
|
|
49
|
+
"coverage:type": "type-coverage",
|
|
50
|
+
"coverage:type:verbose": "type-coverage --detail",
|
|
51
|
+
"coverage:percent": "node scripts/get-coverage-percentage.mjs",
|
|
52
|
+
clean: "run-s -c clean:*",
|
|
53
|
+
"clean:cache": "del-cli '**/.cache'",
|
|
54
|
+
"clean:coverage": "del-cli '.type-coverage' 'coverage'",
|
|
55
|
+
"clean:dist": "del-cli 'dist' '**/*.tsbuildinfo' '.config/*.tsbuildinfo'",
|
|
56
|
+
"clean:dist:types": "del-cli 'dist/types'",
|
|
57
|
+
"clean:declarations": "del-cli '*.d.ts' '!api*.d.ts'",
|
|
58
|
+
"clean:node_modules": "del-cli '**/node_modules'",
|
|
59
|
+
fix: "run-s lint:fix",
|
|
60
|
+
"generate-sdk": "run-s generate-sdk:*",
|
|
61
|
+
"generate-sdk:01-prettify": "node scripts/prettify-base-json.mjs",
|
|
62
|
+
"generate-sdk:02-generate": "node scripts/generate-types.mjs > types/api.d.ts",
|
|
63
|
+
"generate-sdk:03-clean-api": "pnpm run fix && pnpm run fix",
|
|
64
|
+
"knip:dependencies": "knip --dependencies",
|
|
65
|
+
"knip:exports": "knip --include exports,duplicates",
|
|
66
|
+
lint: "oxlint -c=.config/.oxlintrc.json --ignore-path=.config/.oxlintignore --tsconfig=.config/tsconfig.json .",
|
|
67
|
+
"lint:fix": "run-s -c lint:fix:*",
|
|
68
|
+
"lint:fix:oxlint": "oxlint -c=.config/.oxlintrc.json --ignore-path=.config/.oxlintignore --tsconfig=.config/tsconfig.json --quiet --fix . | dev-null",
|
|
69
|
+
"lint:fix:biome": "biome format --log-level=none --fix . | dev-null",
|
|
70
|
+
"lint:fix:eslint": "eslint --config .config/eslint.config.mjs --report-unused-disable-directives --fix . | dev-null",
|
|
71
|
+
"lint-staged": "lint-staged",
|
|
72
|
+
precommit: "lint-staged",
|
|
73
|
+
prepare: "husky",
|
|
74
|
+
prepublishOnly: "echo 'ERROR: Use GitHub Actions workflow for publishing' && exit 1",
|
|
75
|
+
"pretest:unit": "pnpm run build",
|
|
76
|
+
test: "run-s check test:*",
|
|
77
|
+
"test:run": "node scripts/test.mjs",
|
|
78
|
+
"test:unit": "dotenvx -q run -f .env.test -- vitest --run",
|
|
79
|
+
"test:unit:update": "dotenvx -q run -f .env.test -- vitest --run --update",
|
|
80
|
+
"test:unit:coverage": "dotenvx -q run -f .env.test -- vitest run --coverage",
|
|
81
|
+
"test-ci": "dotenvx -q run -f .env.test -- vitest --run",
|
|
82
|
+
"type-ci": "pnpm run check:tsc",
|
|
83
|
+
"test-pre-commit": "dotenvx -q run -f .env.precommit -- pnpm test",
|
|
84
|
+
update: "run-p --aggregate-output update:*",
|
|
85
|
+
"update:deps": "node scripts/taze.mjs",
|
|
86
|
+
"update:socket": "pnpm -r update '@socketsecurity/*' --latest"
|
|
87
|
+
};
|
|
88
|
+
var dependencies = {
|
|
89
|
+
"@socketsecurity/registry": "1.5.1"
|
|
90
|
+
};
|
|
91
|
+
var devDependencies = {
|
|
92
|
+
"@babel/core": "7.28.4",
|
|
93
|
+
"@babel/plugin-transform-runtime": "7.28.3",
|
|
94
|
+
"@babel/preset-typescript": "7.27.1",
|
|
95
|
+
"@babel/runtime": "7.28.4",
|
|
96
|
+
"@biomejs/biome": "2.2.4",
|
|
97
|
+
"@dotenvx/dotenvx": "1.49.0",
|
|
98
|
+
"@eslint/compat": "1.3.2",
|
|
99
|
+
"@eslint/js": "9.35.0",
|
|
100
|
+
"@rollup/plugin-babel": "6.0.4",
|
|
101
|
+
"@rollup/plugin-commonjs": "28.0.6",
|
|
102
|
+
"@rollup/plugin-json": "6.1.0",
|
|
103
|
+
"@rollup/plugin-node-resolve": "16.0.1",
|
|
104
|
+
"@types/node": "24.6.2",
|
|
105
|
+
"@typescript-eslint/eslint-plugin": "8.44.1",
|
|
106
|
+
"@typescript-eslint/parser": "8.44.1",
|
|
107
|
+
"@typescript/native-preview": "7.0.0-dev.20250926.1",
|
|
108
|
+
"@vitest/coverage-v8": "3.2.4",
|
|
109
|
+
"del-cli": "6.0.0",
|
|
110
|
+
"dev-null-cli": "2.0.0",
|
|
111
|
+
eslint: "9.35.0",
|
|
112
|
+
"eslint-import-resolver-typescript": "4.4.4",
|
|
113
|
+
"eslint-plugin-import-x": "4.16.1",
|
|
114
|
+
"eslint-plugin-jsdoc": "57.0.8",
|
|
115
|
+
"eslint-plugin-n": "17.23.1",
|
|
116
|
+
"eslint-plugin-sort-destructure-keys": "2.0.0",
|
|
117
|
+
"eslint-plugin-unicorn": "56.0.1",
|
|
118
|
+
"fast-glob": "3.3.3",
|
|
119
|
+
globals: "16.4.0",
|
|
120
|
+
"http2-wrapper": "2.2.1",
|
|
121
|
+
husky: "9.1.7",
|
|
122
|
+
knip: "5.63.1",
|
|
123
|
+
"lint-staged": "16.1.6",
|
|
124
|
+
nock: "14.0.10",
|
|
125
|
+
"npm-run-all2": "8.0.4",
|
|
126
|
+
"openapi-typescript": "6.7.6",
|
|
127
|
+
oxlint: "1.15.0",
|
|
128
|
+
rollup: "4.50.1",
|
|
129
|
+
taze: "19.6.0",
|
|
130
|
+
trash: "10.0.0",
|
|
131
|
+
"type-coverage": "2.29.7",
|
|
132
|
+
"typescript-eslint": "8.44.1",
|
|
133
|
+
vitest: "3.2.4",
|
|
134
|
+
"yargs-parser": "22.0.0",
|
|
135
|
+
yoctocolors: "2.1.2"
|
|
136
|
+
};
|
|
137
|
+
var pnpm = {
|
|
138
|
+
overrides: {
|
|
139
|
+
vite: "7.1.5"
|
|
140
|
+
},
|
|
141
|
+
ignoredBuiltDependencies: [
|
|
142
|
+
"esbuild",
|
|
143
|
+
"unrs-resolver"
|
|
144
|
+
]
|
|
145
|
+
};
|
|
146
|
+
var engines = {
|
|
147
|
+
node: ">=18",
|
|
148
|
+
pnpm: ">=10.16.0"
|
|
149
|
+
};
|
|
150
|
+
var files = [
|
|
151
|
+
"dist/**",
|
|
152
|
+
"types/**",
|
|
153
|
+
"CHANGELOG.md",
|
|
154
|
+
"requirements.json"
|
|
155
|
+
];
|
|
156
|
+
var typeCoverage = {
|
|
157
|
+
cache: true,
|
|
158
|
+
atLeast: 99.6,
|
|
159
|
+
ignoreAsAssertion: true,
|
|
160
|
+
ignoreCatch: true,
|
|
161
|
+
ignoreEmptyType: true,
|
|
162
|
+
"ignore-non-null-assertion": true,
|
|
163
|
+
"ignore-type-assertion": true,
|
|
164
|
+
"ignore-files": "test/*",
|
|
165
|
+
strict: true
|
|
166
|
+
};
|
|
167
|
+
var rootPkgJson = {
|
|
168
|
+
name: name,
|
|
169
|
+
version: version,
|
|
170
|
+
license: license,
|
|
171
|
+
description: description,
|
|
172
|
+
author: author,
|
|
173
|
+
homepage: homepage,
|
|
174
|
+
repository: repository,
|
|
175
|
+
type: type,
|
|
176
|
+
main: main,
|
|
177
|
+
types: types,
|
|
178
|
+
exports: exports$1,
|
|
179
|
+
scripts: scripts,
|
|
180
|
+
dependencies: dependencies,
|
|
181
|
+
devDependencies: devDependencies,
|
|
182
|
+
pnpm: pnpm,
|
|
183
|
+
engines: engines,
|
|
184
|
+
files: files,
|
|
185
|
+
"lint-staged": {
|
|
186
|
+
"*.{cjs,js,json,md,mjs,mts,ts}": [
|
|
187
|
+
"pnpm run lint:fix:oxlint",
|
|
188
|
+
"pnpm run lint:fix:biome -- --no-errors-on-unmatched --files-ignore-unknown=true --colors=off"
|
|
189
|
+
]
|
|
190
|
+
},
|
|
191
|
+
typeCoverage: typeCoverage
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
exports.author = author;
|
|
195
|
+
exports.default = rootPkgJson;
|
|
196
|
+
exports.dependencies = dependencies;
|
|
197
|
+
exports.description = description;
|
|
198
|
+
exports.devDependencies = devDependencies;
|
|
199
|
+
exports.engines = engines;
|
|
200
|
+
exports.exports = exports$1;
|
|
201
|
+
exports.files = files;
|
|
202
|
+
exports.homepage = homepage;
|
|
203
|
+
exports.license = license;
|
|
204
|
+
exports.main = main;
|
|
205
|
+
exports.name = name;
|
|
206
|
+
exports.pnpm = pnpm;
|
|
207
|
+
exports.repository = repository;
|
|
208
|
+
exports.scripts = scripts;
|
|
209
|
+
exports.type = type;
|
|
210
|
+
exports.typeCoverage = typeCoverage;
|
|
211
|
+
exports.types = types;
|
|
212
|
+
exports.version = version;
|
package/dist/quota-utils.d.ts
CHANGED
|
@@ -11,38 +11,45 @@ export declare function calculateTotalQuotaCost(methodNames: Array<SocketSdkOper
|
|
|
11
11
|
/**
|
|
12
12
|
* Get all available SDK methods with their requirements.
|
|
13
13
|
* Returns complete mapping of methods to quota and permissions.
|
|
14
|
+
* Creates a fresh deep copy each time to prevent external mutations.
|
|
14
15
|
*/
|
|
15
16
|
export declare function getAllMethodRequirements(): Record<string, ApiRequirement>;
|
|
16
17
|
/**
|
|
17
18
|
* Get complete requirement information for a SDK method.
|
|
18
19
|
* Returns both quota cost and required permissions.
|
|
20
|
+
* Memoized to avoid repeated lookups for the same method.
|
|
19
21
|
*/
|
|
20
|
-
export declare
|
|
22
|
+
export declare const getMethodRequirements: (methodName: string) => ApiRequirement;
|
|
21
23
|
/**
|
|
22
24
|
* Get all methods that require specific permissions.
|
|
23
25
|
* Returns methods that need any of the specified permissions.
|
|
26
|
+
* Memoized since the same permission queries are often repeated.
|
|
24
27
|
*/
|
|
25
|
-
export declare
|
|
28
|
+
export declare const getMethodsByPermissions: (permissions: string[]) => string[];
|
|
26
29
|
/**
|
|
27
30
|
* Get all methods that consume a specific quota amount.
|
|
28
31
|
* Useful for finding high-cost or free operations.
|
|
32
|
+
* Memoized to cache results for commonly queried quota costs.
|
|
29
33
|
*/
|
|
30
|
-
export declare
|
|
34
|
+
export declare const getMethodsByQuotaCost: (quotaCost: number) => string[];
|
|
31
35
|
/**
|
|
32
36
|
* Get quota cost for a specific SDK method.
|
|
33
37
|
* Returns the number of quota units consumed by the method.
|
|
38
|
+
* Memoized since quota costs are frequently queried.
|
|
34
39
|
*/
|
|
35
|
-
export declare
|
|
40
|
+
export declare const getQuotaCost: (methodName: string) => number;
|
|
36
41
|
/**
|
|
37
42
|
* Get quota usage summary grouped by cost levels.
|
|
38
43
|
* Returns methods categorized by their quota consumption.
|
|
44
|
+
* Memoized since the summary structure is immutable after load.
|
|
39
45
|
*/
|
|
40
|
-
export declare
|
|
46
|
+
export declare const getQuotaUsageSummary: () => Record<string, string[]>;
|
|
41
47
|
/**
|
|
42
48
|
* Get required permissions for a specific SDK method.
|
|
43
49
|
* Returns array of permission strings needed to call the method.
|
|
50
|
+
* Memoized to cache permission lookups per method.
|
|
44
51
|
*/
|
|
45
|
-
export declare
|
|
52
|
+
export declare const getRequiredPermissions: (methodName: string) => string[];
|
|
46
53
|
/**
|
|
47
54
|
* Check if user has sufficient quota for method calls.
|
|
48
55
|
* Returns true if available quota covers the total cost.
|
package/dist/quota-utils.js
CHANGED
|
@@ -1,152 +1,175 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
exports.getMethodsByQuotaCost = getMethodsByQuotaCost;
|
|
8
|
-
exports.getQuotaCost = getQuotaCost;
|
|
9
|
-
exports.getQuotaUsageSummary = getQuotaUsageSummary;
|
|
10
|
-
exports.getRequiredPermissions = getRequiredPermissions;
|
|
11
|
-
exports.hasQuotaForMethods = hasQuotaForMethods;
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var node_fs = require('node:fs');
|
|
4
|
+
var path = require('node:path');
|
|
5
|
+
var memoization = require('@socketsecurity/registry/lib/memoization');
|
|
6
|
+
|
|
12
7
|
/** @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
8
|
/**
|
|
17
9
|
* Load requirements.json data with caching.
|
|
18
10
|
* Internal function for lazy loading quota requirements.
|
|
11
|
+
* Uses once() memoization to ensure file is only read once.
|
|
19
12
|
*/
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
}
|
|
13
|
+
const loadRequirements = memoization.once(() => {
|
|
14
|
+
try {
|
|
15
|
+
// Resolve path relative to this module file location.
|
|
16
|
+
// When compiled, __dirname will point to dist/ directory.
|
|
17
|
+
// In source, __dirname points to src/ directory.
|
|
18
|
+
// requirements.json is always in the parent directory of dist/ or src/.
|
|
19
|
+
const requirementsPath = path.join(__dirname, '..', 'requirements.json');
|
|
20
|
+
const data = node_fs.readFileSync(requirementsPath, 'utf8');
|
|
21
|
+
return JSON.parse(data);
|
|
22
|
+
} catch (e) {
|
|
23
|
+
throw new Error('Failed to load "requirements.json"', {
|
|
24
|
+
cause: e
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
|
|
38
29
|
/**
|
|
39
30
|
* Calculate total quota cost for multiple SDK method calls.
|
|
40
31
|
* Returns sum of quota units for all specified methods.
|
|
41
32
|
*/
|
|
42
33
|
function calculateTotalQuotaCost(methodNames) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
34
|
+
return methodNames.reduce((total, methodName) => {
|
|
35
|
+
return total + getQuotaCost(methodName);
|
|
36
|
+
}, 0);
|
|
46
37
|
}
|
|
38
|
+
|
|
47
39
|
/**
|
|
48
40
|
* Get all available SDK methods with their requirements.
|
|
49
41
|
* Returns complete mapping of methods to quota and permissions.
|
|
42
|
+
* Creates a fresh deep copy each time to prevent external mutations.
|
|
50
43
|
*/
|
|
51
44
|
function getAllMethodRequirements() {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
45
|
+
const reqs = loadRequirements();
|
|
46
|
+
const result = {};
|
|
47
|
+
Object.entries(reqs.api).forEach(([methodName, requirement]) => {
|
|
48
|
+
result[methodName] = {
|
|
49
|
+
permissions: [...requirement.permissions],
|
|
50
|
+
quota: requirement.quota
|
|
51
|
+
};
|
|
52
|
+
});
|
|
53
|
+
return result;
|
|
61
54
|
}
|
|
55
|
+
|
|
62
56
|
/**
|
|
63
57
|
* Get complete requirement information for a SDK method.
|
|
64
58
|
* Returns both quota cost and required permissions.
|
|
59
|
+
* Memoized to avoid repeated lookups for the same method.
|
|
65
60
|
*/
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
61
|
+
const getMethodRequirements = memoization.memoize(methodName => {
|
|
62
|
+
const reqs = loadRequirements();
|
|
63
|
+
const requirement = reqs.api[methodName];
|
|
64
|
+
if (!requirement) {
|
|
65
|
+
throw new Error(`Unknown SDK method: "${methodName}"`);
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
permissions: [...requirement.permissions],
|
|
69
|
+
quota: requirement.quota
|
|
70
|
+
};
|
|
71
|
+
}, {
|
|
72
|
+
name: 'getMethodRequirements'
|
|
73
|
+
});
|
|
74
|
+
|
|
77
75
|
/**
|
|
78
76
|
* Get all methods that require specific permissions.
|
|
79
77
|
* Returns methods that need any of the specified permissions.
|
|
78
|
+
* Memoized since the same permission queries are often repeated.
|
|
80
79
|
*/
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
80
|
+
const getMethodsByPermissions = memoization.memoize(permissions => {
|
|
81
|
+
const reqs = loadRequirements();
|
|
82
|
+
return Object.entries(reqs.api).filter(([, requirement]) => {
|
|
83
|
+
return permissions.some(permission => requirement.permissions.includes(permission));
|
|
84
|
+
}).map(([methodName]) => methodName).sort();
|
|
85
|
+
}, {
|
|
86
|
+
name: 'getMethodsByPermissions'
|
|
87
|
+
});
|
|
88
|
+
|
|
90
89
|
/**
|
|
91
90
|
* Get all methods that consume a specific quota amount.
|
|
92
91
|
* Useful for finding high-cost or free operations.
|
|
92
|
+
* Memoized to cache results for commonly queried quota costs.
|
|
93
93
|
*/
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
94
|
+
const getMethodsByQuotaCost = memoization.memoize(quotaCost => {
|
|
95
|
+
const reqs = loadRequirements();
|
|
96
|
+
return Object.entries(reqs.api).filter(([, requirement]) => requirement.quota === quotaCost).map(([methodName]) => methodName).sort();
|
|
97
|
+
}, {
|
|
98
|
+
name: 'getMethodsByQuotaCost'
|
|
99
|
+
});
|
|
100
|
+
|
|
101
101
|
/**
|
|
102
102
|
* Get quota cost for a specific SDK method.
|
|
103
103
|
* Returns the number of quota units consumed by the method.
|
|
104
|
+
* Memoized since quota costs are frequently queried.
|
|
104
105
|
*/
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
}
|
|
106
|
+
const getQuotaCost = memoization.memoize(methodName => {
|
|
107
|
+
const reqs = loadRequirements();
|
|
108
|
+
const requirement = reqs.api[methodName];
|
|
109
|
+
if (!requirement) {
|
|
110
|
+
throw new Error(`Unknown SDK method: "${methodName}"`);
|
|
111
|
+
}
|
|
112
|
+
return requirement.quota;
|
|
113
|
+
}, {
|
|
114
|
+
name: 'getQuotaCost'
|
|
115
|
+
});
|
|
116
|
+
|
|
113
117
|
/**
|
|
114
118
|
* Get quota usage summary grouped by cost levels.
|
|
115
119
|
* Returns methods categorized by their quota consumption.
|
|
120
|
+
* Memoized since the summary structure is immutable after load.
|
|
116
121
|
*/
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
122
|
+
const getQuotaUsageSummary = memoization.memoize(() => {
|
|
123
|
+
const reqs = loadRequirements();
|
|
124
|
+
const summary = {};
|
|
125
|
+
Object.entries(reqs.api).forEach(([methodName, requirement]) => {
|
|
126
|
+
const costKey = `${requirement.quota} units`;
|
|
127
|
+
if (!summary[costKey]) {
|
|
128
|
+
summary[costKey] = [];
|
|
129
|
+
}
|
|
130
|
+
summary[costKey].push(methodName);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// Sort methods within each cost level
|
|
134
|
+
Object.keys(summary).forEach(costKey => {
|
|
135
|
+
summary[costKey]?.sort();
|
|
136
|
+
});
|
|
137
|
+
return summary;
|
|
138
|
+
}, {
|
|
139
|
+
name: 'getQuotaUsageSummary'
|
|
140
|
+
});
|
|
141
|
+
|
|
133
142
|
/**
|
|
134
143
|
* Get required permissions for a specific SDK method.
|
|
135
144
|
* Returns array of permission strings needed to call the method.
|
|
145
|
+
* Memoized to cache permission lookups per method.
|
|
136
146
|
*/
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
147
|
+
const getRequiredPermissions = memoization.memoize(methodName => {
|
|
148
|
+
const reqs = loadRequirements();
|
|
149
|
+
const requirement = reqs.api[methodName];
|
|
150
|
+
if (!requirement) {
|
|
151
|
+
throw new Error(`Unknown SDK method: "${methodName}"`);
|
|
152
|
+
}
|
|
153
|
+
return [...requirement.permissions];
|
|
154
|
+
}, {
|
|
155
|
+
name: 'getRequiredPermissions'
|
|
156
|
+
});
|
|
157
|
+
|
|
145
158
|
/**
|
|
146
159
|
* Check if user has sufficient quota for method calls.
|
|
147
160
|
* Returns true if available quota covers the total cost.
|
|
148
161
|
*/
|
|
149
162
|
function hasQuotaForMethods(availableQuota, methodNames) {
|
|
150
|
-
|
|
151
|
-
|
|
163
|
+
const totalCost = calculateTotalQuotaCost(methodNames);
|
|
164
|
+
return availableQuota >= totalCost;
|
|
152
165
|
}
|
|
166
|
+
|
|
167
|
+
exports.calculateTotalQuotaCost = calculateTotalQuotaCost;
|
|
168
|
+
exports.getAllMethodRequirements = getAllMethodRequirements;
|
|
169
|
+
exports.getMethodRequirements = getMethodRequirements;
|
|
170
|
+
exports.getMethodsByPermissions = getMethodsByPermissions;
|
|
171
|
+
exports.getMethodsByQuotaCost = getMethodsByQuotaCost;
|
|
172
|
+
exports.getQuotaCost = getQuotaCost;
|
|
173
|
+
exports.getQuotaUsageSummary = getQuotaUsageSummary;
|
|
174
|
+
exports.getRequiredPermissions = getRequiredPermissions;
|
|
175
|
+
exports.hasQuotaForMethods = hasQuotaForMethods;
|