@barekey/sdk 0.1.0
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/LICENSE +28 -0
- package/README.md +21 -0
- package/dist/client.d.ts +41 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +302 -0
- package/dist/errors.d.ts +461 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +343 -0
- package/dist/handle.d.ts +20 -0
- package/dist/handle.d.ts.map +1 -0
- package/dist/handle.js +35 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/internal/cache.d.ts +13 -0
- package/dist/internal/cache.d.ts.map +1 -0
- package/dist/internal/cache.js +24 -0
- package/dist/internal/evaluate.d.ts +7 -0
- package/dist/internal/evaluate.d.ts.map +1 -0
- package/dist/internal/evaluate.js +176 -0
- package/dist/internal/http.d.ts +19 -0
- package/dist/internal/http.d.ts.map +1 -0
- package/dist/internal/http.js +92 -0
- package/dist/internal/node-runtime.d.ts +19 -0
- package/dist/internal/node-runtime.d.ts.map +1 -0
- package/dist/internal/node-runtime.js +422 -0
- package/dist/internal/requirements.d.ts +3 -0
- package/dist/internal/requirements.d.ts.map +1 -0
- package/dist/internal/requirements.js +40 -0
- package/dist/internal/runtime.d.ts +15 -0
- package/dist/internal/runtime.d.ts.map +1 -0
- package/dist/internal/runtime.js +135 -0
- package/dist/internal/ttl.d.ts +4 -0
- package/dist/internal/ttl.d.ts.map +1 -0
- package/dist/internal/ttl.js +30 -0
- package/dist/internal/typegen.d.ts +25 -0
- package/dist/internal/typegen.d.ts.map +1 -0
- package/dist/internal/typegen.js +75 -0
- package/dist/types.d.ts +130 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/generated.d.ts +16 -0
- package/index.d.ts +2 -0
- package/package.json +42 -0
- package/src/client.ts +422 -0
- package/src/errors.ts +420 -0
- package/src/handle.ts +67 -0
- package/src/index.ts +60 -0
- package/src/internal/cache.ts +33 -0
- package/src/internal/evaluate.ts +232 -0
- package/src/internal/http.ts +134 -0
- package/src/internal/node-runtime.ts +581 -0
- package/src/internal/requirements.ts +57 -0
- package/src/internal/runtime.ts +199 -0
- package/src/internal/ttl.ts +41 -0
- package/src/internal/typegen.ts +124 -0
- package/src/types.ts +189 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/internal/http.ts"],"names":[],"mappings":"AAUA,MAAM,MAAM,oBAAoB,GAAG;IACjC,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAClC,cAAc,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAClC,CAAC;AAUF,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAExD;AAqFD,wBAAsB,QAAQ,CAAC,SAAS,EAAE,KAAK,EAAE;IAC/C,OAAO,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;IACjC,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,CAAC,EAAE,oBAAoB,CAAC;CAC7B,GAAG,OAAO,CAAC,SAAS,CAAC,CAKrB;AAED,wBAAsB,OAAO,CAAC,SAAS,EAAE,KAAK,EAAE;IAC9C,OAAO,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;IACjC,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,oBAAoB,CAAC;CAC7B,GAAG,OAAO,CAAC,SAAS,CAAC,CAKrB"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { NetworkError, UnauthorizedError, createBarekeyErrorFromCode } from "../errors.js";
|
|
2
|
+
async function parseJsonResponse(response) {
|
|
3
|
+
try {
|
|
4
|
+
return await response.json();
|
|
5
|
+
}
|
|
6
|
+
catch {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export function normalizeBaseUrl(baseUrl) {
|
|
11
|
+
return baseUrl.replace(/\/$/, "");
|
|
12
|
+
}
|
|
13
|
+
async function requestJson(input) {
|
|
14
|
+
const makeRequest = async (accessToken) => input.fetchFn(`${normalizeBaseUrl(input.baseUrl)}${input.path}`, {
|
|
15
|
+
method: input.method,
|
|
16
|
+
headers: {
|
|
17
|
+
...(input.method === "POST"
|
|
18
|
+
? {
|
|
19
|
+
"content-type": "application/json",
|
|
20
|
+
}
|
|
21
|
+
: {}),
|
|
22
|
+
...(accessToken
|
|
23
|
+
? {
|
|
24
|
+
authorization: `Bearer ${accessToken}`,
|
|
25
|
+
}
|
|
26
|
+
: {}),
|
|
27
|
+
},
|
|
28
|
+
...(input.method === "POST"
|
|
29
|
+
? {
|
|
30
|
+
body: JSON.stringify(input.payload),
|
|
31
|
+
}
|
|
32
|
+
: {}),
|
|
33
|
+
});
|
|
34
|
+
const accessToken = input.auth ? await input.auth.getAccessToken() : undefined;
|
|
35
|
+
let response;
|
|
36
|
+
try {
|
|
37
|
+
response = await makeRequest(accessToken);
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
throw new NetworkError({
|
|
41
|
+
message: error instanceof Error ? error.message : "A Barekey network request failed.",
|
|
42
|
+
cause: error,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
if (response.status === 401 && input.auth?.onUnauthorized) {
|
|
46
|
+
await input.auth.onUnauthorized();
|
|
47
|
+
const retryAccessToken = await input.auth.getAccessToken();
|
|
48
|
+
try {
|
|
49
|
+
response = await makeRequest(retryAccessToken);
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
throw new NetworkError({
|
|
53
|
+
message: error instanceof Error ? error.message : "A Barekey network request failed.",
|
|
54
|
+
cause: error,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const parsed = (await parseJsonResponse(response));
|
|
59
|
+
if (!response.ok) {
|
|
60
|
+
const parsedError = parsed;
|
|
61
|
+
const code = parsedError?.error?.code ?? (response.status === 401 ? "UNAUTHORIZED" : "UNKNOWN_ERROR");
|
|
62
|
+
const message = parsedError?.error?.message ??
|
|
63
|
+
(response.status === 401
|
|
64
|
+
? "The provided Barekey credentials were rejected."
|
|
65
|
+
: `Barekey request failed with status ${response.status}.`);
|
|
66
|
+
if (response.status === 401 && code === "UNAUTHORIZED" && !parsedError?.error?.message) {
|
|
67
|
+
throw new UnauthorizedError({
|
|
68
|
+
requestId: parsedError?.error?.requestId ?? null,
|
|
69
|
+
status: response.status,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
throw createBarekeyErrorFromCode({
|
|
73
|
+
code,
|
|
74
|
+
message,
|
|
75
|
+
requestId: parsedError?.error?.requestId ?? null,
|
|
76
|
+
status: response.status,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
return parsed;
|
|
80
|
+
}
|
|
81
|
+
export async function postJson(input) {
|
|
82
|
+
return await requestJson({
|
|
83
|
+
...input,
|
|
84
|
+
method: "POST",
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
export async function getJson(input) {
|
|
88
|
+
return await requestJson({
|
|
89
|
+
...input,
|
|
90
|
+
method: "GET",
|
|
91
|
+
});
|
|
92
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare function isFilesystemAvailable(): Promise<boolean>;
|
|
2
|
+
export declare function loadBarekeyJsonConfig(): Promise<{
|
|
3
|
+
path: string;
|
|
4
|
+
json: Record<string, unknown>;
|
|
5
|
+
} | null>;
|
|
6
|
+
export declare function loadCliSessionAuthResolver(fetchFn: typeof globalThis.fetch): Promise<{
|
|
7
|
+
baseUrl: string;
|
|
8
|
+
getAccessToken(): Promise<string>;
|
|
9
|
+
onUnauthorized(): Promise<void>;
|
|
10
|
+
} | null>;
|
|
11
|
+
type InstalledSdkTypegenTarget = {
|
|
12
|
+
packageRoot: string;
|
|
13
|
+
generatedTypesPath: string;
|
|
14
|
+
};
|
|
15
|
+
export declare function resolveInstalledSdkTypegenTarget(): Promise<InstalledSdkTypegenTarget | null>;
|
|
16
|
+
export declare function readTextFile(filePath: string): Promise<string | null>;
|
|
17
|
+
export declare function writeTextFileAtomic(filePath: string, value: string): Promise<void>;
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=node-runtime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node-runtime.d.ts","sourceRoot":"","sources":["../../src/internal/node-runtime.ts"],"names":[],"mappings":"AA4VA,wBAAsB,qBAAqB,IAAI,OAAO,CAAC,OAAO,CAAC,CAE9D;AAED,wBAAsB,qBAAqB,IAAI,OAAO,CAAC;IACrD,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAC/B,GAAG,IAAI,CAAC,CAwCR;AAED,wBAAsB,0BAA0B,CAAC,OAAO,EAAE,OAAO,UAAU,CAAC,KAAK,GAAG,OAAO,CAAC;IAC1F,OAAO,EAAE,MAAM,CAAC;IAChB,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAClC,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACjC,GAAG,IAAI,CAAC,CAoER;AAOD,KAAK,yBAAyB,GAAG;IAC/B,WAAW,EAAE,MAAM,CAAC;IACpB,kBAAkB,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF,wBAAsB,gCAAgC,IAAI,OAAO,CAAC,yBAAyB,GAAG,IAAI,CAAC,CAsDlG;AAED,wBAAsB,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAkB3E;AAED,wBAAsB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAuBxF"}
|
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
import { InvalidConfigurationProvidedError, InvalidCredentialsProvidedError, NoCredentialsProvidedError, SdkModuleNotFoundError, TypegenReadFailedError, TypegenUnsupportedSdkError, TypegenWriteFailedError, } from "../errors.js";
|
|
2
|
+
import { postJson } from "./http.js";
|
|
3
|
+
const CLI_SERVICE_NAME = "barekey-cli";
|
|
4
|
+
function isNodeRuntime() {
|
|
5
|
+
return (typeof process !== "undefined" &&
|
|
6
|
+
typeof process.versions === "object" &&
|
|
7
|
+
process.versions !== null &&
|
|
8
|
+
typeof process.versions.node === "string");
|
|
9
|
+
}
|
|
10
|
+
async function loadNodeRuntime() {
|
|
11
|
+
if (!isNodeRuntime()) {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
try {
|
|
15
|
+
const [childProcess, fs, moduleApi, os, path] = await Promise.all([
|
|
16
|
+
import("node:child_process"),
|
|
17
|
+
import("node:fs/promises"),
|
|
18
|
+
import("node:module"),
|
|
19
|
+
import("node:os"),
|
|
20
|
+
import("node:path"),
|
|
21
|
+
]);
|
|
22
|
+
return {
|
|
23
|
+
childProcess,
|
|
24
|
+
fs,
|
|
25
|
+
moduleApi,
|
|
26
|
+
os,
|
|
27
|
+
path,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
async function runCommand(runtime, command, args, input) {
|
|
35
|
+
return await new Promise((resolve) => {
|
|
36
|
+
const child = runtime.childProcess.spawn(command, args, {
|
|
37
|
+
stdio: "pipe",
|
|
38
|
+
});
|
|
39
|
+
let stdout = "";
|
|
40
|
+
let stderr = "";
|
|
41
|
+
child.stdout.on("data", (chunk) => {
|
|
42
|
+
stdout += chunk.toString("utf8");
|
|
43
|
+
});
|
|
44
|
+
child.stderr.on("data", (chunk) => {
|
|
45
|
+
stderr += chunk.toString("utf8");
|
|
46
|
+
});
|
|
47
|
+
child.stdin.on("error", () => {
|
|
48
|
+
// Ignore EPIPE if the child exits before consuming stdin.
|
|
49
|
+
});
|
|
50
|
+
child.on("error", () => {
|
|
51
|
+
resolve({
|
|
52
|
+
stdout: "",
|
|
53
|
+
stderr: "",
|
|
54
|
+
code: 127,
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
child.on("close", (code) => {
|
|
58
|
+
resolve({
|
|
59
|
+
stdout,
|
|
60
|
+
stderr,
|
|
61
|
+
code: code ?? 1,
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
if (input !== undefined) {
|
|
65
|
+
child.stdin.write(input);
|
|
66
|
+
}
|
|
67
|
+
child.stdin.end();
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
async function readJsonFile(runtime, filePath) {
|
|
71
|
+
try {
|
|
72
|
+
const value = await runtime.fs.readFile(filePath, "utf8");
|
|
73
|
+
return JSON.parse(value);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function isCliConfig(value) {
|
|
80
|
+
return (typeof value === "object" &&
|
|
81
|
+
value !== null &&
|
|
82
|
+
typeof value.baseUrl === "string" &&
|
|
83
|
+
typeof value.activeAccountId === "string");
|
|
84
|
+
}
|
|
85
|
+
function isCliCredentials(value) {
|
|
86
|
+
if (typeof value !== "object" || value === null) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
const candidate = value;
|
|
90
|
+
return (typeof candidate.accessToken === "string" &&
|
|
91
|
+
typeof candidate.refreshToken === "string" &&
|
|
92
|
+
typeof candidate.accessTokenExpiresAtMs === "number" &&
|
|
93
|
+
typeof candidate.refreshTokenExpiresAtMs === "number" &&
|
|
94
|
+
typeof candidate.clerkUserId === "string" &&
|
|
95
|
+
typeof candidate.orgId === "string" &&
|
|
96
|
+
typeof candidate.orgSlug === "string");
|
|
97
|
+
}
|
|
98
|
+
async function getFromKeychain(runtime, account) {
|
|
99
|
+
if (process.platform === "darwin") {
|
|
100
|
+
const result = await runCommand(runtime, "security", [
|
|
101
|
+
"find-generic-password",
|
|
102
|
+
"-s",
|
|
103
|
+
CLI_SERVICE_NAME,
|
|
104
|
+
"-a",
|
|
105
|
+
account,
|
|
106
|
+
"-w",
|
|
107
|
+
]);
|
|
108
|
+
if (result.code !== 0) {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
const value = result.stdout.trim();
|
|
112
|
+
return value.length > 0 ? value : null;
|
|
113
|
+
}
|
|
114
|
+
if (process.platform === "linux") {
|
|
115
|
+
const result = await runCommand(runtime, "secret-tool", [
|
|
116
|
+
"lookup",
|
|
117
|
+
"service",
|
|
118
|
+
CLI_SERVICE_NAME,
|
|
119
|
+
"account",
|
|
120
|
+
account,
|
|
121
|
+
]);
|
|
122
|
+
if (result.code !== 0) {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
const value = result.stdout.trim();
|
|
126
|
+
return value.length > 0 ? value : null;
|
|
127
|
+
}
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
async function setInKeychain(runtime, account, value) {
|
|
131
|
+
if (process.platform === "darwin") {
|
|
132
|
+
const result = await runCommand(runtime, "security", [
|
|
133
|
+
"add-generic-password",
|
|
134
|
+
"-U",
|
|
135
|
+
"-s",
|
|
136
|
+
CLI_SERVICE_NAME,
|
|
137
|
+
"-a",
|
|
138
|
+
account,
|
|
139
|
+
"-w",
|
|
140
|
+
value,
|
|
141
|
+
]);
|
|
142
|
+
return result.code === 0;
|
|
143
|
+
}
|
|
144
|
+
if (process.platform === "linux") {
|
|
145
|
+
const result = await runCommand(runtime, "secret-tool", ["store", `--label=${CLI_SERVICE_NAME}`, "service", CLI_SERVICE_NAME, "account", account], value);
|
|
146
|
+
return result.code === 0;
|
|
147
|
+
}
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
function getConfigPaths(runtime) {
|
|
151
|
+
const configDir = runtime.path.join(runtime.os.homedir(), ".config", "barekey");
|
|
152
|
+
return {
|
|
153
|
+
configPath: runtime.path.join(configDir, "config.json"),
|
|
154
|
+
credentialsDir: runtime.path.join(configDir, "credentials"),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function credentialsPathForAccount(runtime, credentialsDir, accountId) {
|
|
158
|
+
return runtime.path.join(credentialsDir, `${encodeURIComponent(accountId)}.json`);
|
|
159
|
+
}
|
|
160
|
+
async function saveCliCredentials(runtime, accountId, credentials) {
|
|
161
|
+
const serialized = JSON.stringify(credentials);
|
|
162
|
+
if (await setInKeychain(runtime, accountId, serialized)) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const { credentialsDir } = getConfigPaths(runtime);
|
|
166
|
+
await runtime.fs.mkdir(credentialsDir, {
|
|
167
|
+
recursive: true,
|
|
168
|
+
mode: 0o700,
|
|
169
|
+
});
|
|
170
|
+
await runtime.fs.writeFile(credentialsPathForAccount(runtime, credentialsDir, accountId), serialized, {
|
|
171
|
+
encoding: "utf8",
|
|
172
|
+
mode: 0o600,
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
async function readCliSessionWithSource(runtime) {
|
|
176
|
+
const { configPath, credentialsDir } = getConfigPaths(runtime);
|
|
177
|
+
const rawConfig = await readJsonFile(runtime, configPath);
|
|
178
|
+
if (rawConfig === null) {
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
if (!isCliConfig(rawConfig)) {
|
|
182
|
+
throw new InvalidCredentialsProvidedError({
|
|
183
|
+
message: "Stored Barekey CLI config is malformed.",
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
const fromKeychain = await getFromKeychain(runtime, rawConfig.activeAccountId);
|
|
187
|
+
if (fromKeychain !== null) {
|
|
188
|
+
try {
|
|
189
|
+
const parsed = JSON.parse(fromKeychain);
|
|
190
|
+
if (!isCliCredentials(parsed)) {
|
|
191
|
+
throw new InvalidCredentialsProvidedError({
|
|
192
|
+
message: "Stored Barekey CLI credentials are malformed.",
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
return {
|
|
196
|
+
baseUrl: rawConfig.baseUrl,
|
|
197
|
+
activeAccountId: rawConfig.activeAccountId,
|
|
198
|
+
credentials: parsed,
|
|
199
|
+
source: "keychain",
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
if (error instanceof InvalidCredentialsProvidedError) {
|
|
204
|
+
throw error;
|
|
205
|
+
}
|
|
206
|
+
throw new InvalidCredentialsProvidedError({
|
|
207
|
+
message: "Stored Barekey CLI credentials are malformed.",
|
|
208
|
+
cause: error,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
const fromFile = await readJsonFile(runtime, credentialsPathForAccount(runtime, credentialsDir, rawConfig.activeAccountId));
|
|
213
|
+
if (fromFile === null) {
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
if (!isCliCredentials(fromFile)) {
|
|
217
|
+
throw new InvalidCredentialsProvidedError({
|
|
218
|
+
message: "Stored Barekey CLI credentials are malformed.",
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
return {
|
|
222
|
+
baseUrl: rawConfig.baseUrl,
|
|
223
|
+
activeAccountId: rawConfig.activeAccountId,
|
|
224
|
+
credentials: fromFile,
|
|
225
|
+
source: "file",
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
export async function isFilesystemAvailable() {
|
|
229
|
+
return (await loadNodeRuntime()) !== null;
|
|
230
|
+
}
|
|
231
|
+
export async function loadBarekeyJsonConfig() {
|
|
232
|
+
const runtime = await loadNodeRuntime();
|
|
233
|
+
if (runtime === null) {
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
let current = runtime.path.resolve(process.cwd());
|
|
237
|
+
while (true) {
|
|
238
|
+
const candidate = runtime.path.join(current, "barekey.json");
|
|
239
|
+
try {
|
|
240
|
+
const raw = await runtime.fs.readFile(candidate, "utf8");
|
|
241
|
+
try {
|
|
242
|
+
return {
|
|
243
|
+
path: candidate,
|
|
244
|
+
json: JSON.parse(raw),
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
catch (error) {
|
|
248
|
+
throw new InvalidConfigurationProvidedError({
|
|
249
|
+
message: `The barekey.json file at ${candidate} is not valid JSON.`,
|
|
250
|
+
cause: error,
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
catch (error) {
|
|
255
|
+
if (error instanceof InvalidConfigurationProvidedError) {
|
|
256
|
+
throw error;
|
|
257
|
+
}
|
|
258
|
+
const nodeError = error;
|
|
259
|
+
if (nodeError?.code && nodeError.code !== "ENOENT") {
|
|
260
|
+
throw new InvalidConfigurationProvidedError({
|
|
261
|
+
message: `The barekey.json file at ${candidate} could not be read.`,
|
|
262
|
+
cause: error,
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
const parent = runtime.path.dirname(current);
|
|
266
|
+
if (parent === current) {
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
current = parent;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
export async function loadCliSessionAuthResolver(fetchFn) {
|
|
274
|
+
const runtime = await loadNodeRuntime();
|
|
275
|
+
if (runtime === null) {
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
let cachedSession = await readCliSessionWithSource(runtime);
|
|
279
|
+
if (cachedSession === null) {
|
|
280
|
+
return null;
|
|
281
|
+
}
|
|
282
|
+
let forceRefresh = false;
|
|
283
|
+
const refreshCredentials = async () => {
|
|
284
|
+
const currentSession = cachedSession;
|
|
285
|
+
if (currentSession === null) {
|
|
286
|
+
throw new NoCredentialsProvidedError();
|
|
287
|
+
}
|
|
288
|
+
if (!forceRefresh && currentSession.credentials.accessTokenExpiresAtMs > Date.now() + 10_000) {
|
|
289
|
+
return currentSession.credentials.accessToken;
|
|
290
|
+
}
|
|
291
|
+
const refreshed = await postJson({
|
|
292
|
+
fetchFn,
|
|
293
|
+
baseUrl: currentSession.baseUrl,
|
|
294
|
+
path: "/v1/cli/token/refresh",
|
|
295
|
+
payload: {
|
|
296
|
+
refreshToken: currentSession.credentials.refreshToken,
|
|
297
|
+
},
|
|
298
|
+
});
|
|
299
|
+
const nextCredentials = {
|
|
300
|
+
accessToken: refreshed.accessToken,
|
|
301
|
+
refreshToken: refreshed.refreshToken,
|
|
302
|
+
accessTokenExpiresAtMs: refreshed.accessTokenExpiresAtMs,
|
|
303
|
+
refreshTokenExpiresAtMs: refreshed.refreshTokenExpiresAtMs,
|
|
304
|
+
clerkUserId: refreshed.clerkUserId,
|
|
305
|
+
orgId: refreshed.orgId,
|
|
306
|
+
orgSlug: refreshed.orgSlug,
|
|
307
|
+
};
|
|
308
|
+
await saveCliCredentials(runtime, currentSession.activeAccountId, nextCredentials);
|
|
309
|
+
cachedSession = {
|
|
310
|
+
...currentSession,
|
|
311
|
+
credentials: nextCredentials,
|
|
312
|
+
};
|
|
313
|
+
forceRefresh = false;
|
|
314
|
+
return nextCredentials.accessToken;
|
|
315
|
+
};
|
|
316
|
+
return {
|
|
317
|
+
baseUrl: cachedSession.baseUrl,
|
|
318
|
+
async getAccessToken() {
|
|
319
|
+
return await refreshCredentials();
|
|
320
|
+
},
|
|
321
|
+
async onUnauthorized() {
|
|
322
|
+
forceRefresh = true;
|
|
323
|
+
},
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
function isMissingModuleError(error) {
|
|
327
|
+
const nodeError = error;
|
|
328
|
+
return nodeError?.code === "MODULE_NOT_FOUND" || nodeError?.code === "ERR_MODULE_NOT_FOUND";
|
|
329
|
+
}
|
|
330
|
+
export async function resolveInstalledSdkTypegenTarget() {
|
|
331
|
+
const runtime = await loadNodeRuntime();
|
|
332
|
+
if (runtime === null) {
|
|
333
|
+
return null;
|
|
334
|
+
}
|
|
335
|
+
const require = runtime.moduleApi.createRequire(runtime.path.join(process.cwd(), "__barekey_typegen__.cjs"));
|
|
336
|
+
let resolvedEntrypoint;
|
|
337
|
+
try {
|
|
338
|
+
resolvedEntrypoint = require.resolve("@barekey/sdk/package.json");
|
|
339
|
+
}
|
|
340
|
+
catch (error) {
|
|
341
|
+
if (isMissingModuleError(error)) {
|
|
342
|
+
throw new SdkModuleNotFoundError();
|
|
343
|
+
}
|
|
344
|
+
throw new SdkModuleNotFoundError({
|
|
345
|
+
message: "The installed @barekey/sdk module could not be resolved from the current project.",
|
|
346
|
+
cause: error,
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
let current = runtime.path.dirname(await runtime.fs.realpath(resolvedEntrypoint));
|
|
350
|
+
while (true) {
|
|
351
|
+
const candidatePackageJson = runtime.path.join(current, "package.json");
|
|
352
|
+
const rawPackageJson = await readJsonFile(runtime, candidatePackageJson);
|
|
353
|
+
if (rawPackageJson !== null && rawPackageJson.name === "@barekey/sdk") {
|
|
354
|
+
const generatedTypesPath = runtime.path.join(current, "generated.d.ts");
|
|
355
|
+
try {
|
|
356
|
+
await runtime.fs.access(generatedTypesPath);
|
|
357
|
+
}
|
|
358
|
+
catch (error) {
|
|
359
|
+
throw new TypegenUnsupportedSdkError({
|
|
360
|
+
message: `The installed @barekey/sdk module at ${current} does not include generated.d.ts.`,
|
|
361
|
+
cause: error,
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
return {
|
|
365
|
+
packageRoot: current,
|
|
366
|
+
generatedTypesPath,
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
const parent = runtime.path.dirname(current);
|
|
370
|
+
if (parent === current) {
|
|
371
|
+
break;
|
|
372
|
+
}
|
|
373
|
+
current = parent;
|
|
374
|
+
}
|
|
375
|
+
throw new TypegenUnsupportedSdkError({
|
|
376
|
+
message: "The installed @barekey/sdk module could not be mapped to a supported package root.",
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
export async function readTextFile(filePath) {
|
|
380
|
+
const runtime = await loadNodeRuntime();
|
|
381
|
+
if (runtime === null) {
|
|
382
|
+
return null;
|
|
383
|
+
}
|
|
384
|
+
try {
|
|
385
|
+
return await runtime.fs.readFile(filePath, "utf8");
|
|
386
|
+
}
|
|
387
|
+
catch (error) {
|
|
388
|
+
const nodeError = error;
|
|
389
|
+
if (nodeError?.code === "ENOENT") {
|
|
390
|
+
return null;
|
|
391
|
+
}
|
|
392
|
+
throw new TypegenReadFailedError({
|
|
393
|
+
message: `Barekey could not read ${filePath} while updating generated SDK types.`,
|
|
394
|
+
cause: error,
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
export async function writeTextFileAtomic(filePath, value) {
|
|
399
|
+
const runtime = await loadNodeRuntime();
|
|
400
|
+
if (runtime === null) {
|
|
401
|
+
throw new TypegenWriteFailedError({
|
|
402
|
+
message: "Barekey could not update generated SDK types because filesystem access is unavailable.",
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
const temporaryPath = `${filePath}.${process.pid}.${Date.now()}.tmp`;
|
|
406
|
+
try {
|
|
407
|
+
await runtime.fs.writeFile(temporaryPath, value, "utf8");
|
|
408
|
+
await runtime.fs.rename(temporaryPath, filePath);
|
|
409
|
+
}
|
|
410
|
+
catch (error) {
|
|
411
|
+
try {
|
|
412
|
+
await runtime.fs.unlink(temporaryPath);
|
|
413
|
+
}
|
|
414
|
+
catch {
|
|
415
|
+
// Best effort cleanup only.
|
|
416
|
+
}
|
|
417
|
+
throw new TypegenWriteFailedError({
|
|
418
|
+
message: `Barekey could not write ${filePath}.`,
|
|
419
|
+
cause: error,
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"requirements.d.ts","sourceRoot":"","sources":["../../src/internal/requirements.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAGV,uBAAuB,EACxB,MAAM,aAAa,CAAC;AAiBrB,wBAAsB,oBAAoB,CACxC,YAAY,EAAE,uBAAuB,EACrC,KAAK,EAAE,OAAO,GACb,OAAO,CAAC,IAAI,CAAC,CA+Bf"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { InvalidConfigurationProvidedError, RequirementsValidationFailedError } from "../errors.js";
|
|
2
|
+
function isStandardSchemaResult(value) {
|
|
3
|
+
if (typeof value !== "object" || value === null) {
|
|
4
|
+
return false;
|
|
5
|
+
}
|
|
6
|
+
return "value" in value || "issues" in value;
|
|
7
|
+
}
|
|
8
|
+
function formatPathSegment(segment) {
|
|
9
|
+
if (typeof segment === "object" && segment !== null && "key" in segment) {
|
|
10
|
+
return String(segment.key);
|
|
11
|
+
}
|
|
12
|
+
return String(segment);
|
|
13
|
+
}
|
|
14
|
+
export async function validateRequirements(requirements, value) {
|
|
15
|
+
const standard = requirements["~standard"];
|
|
16
|
+
if (standard === undefined ||
|
|
17
|
+
typeof standard !== "object" ||
|
|
18
|
+
standard === null ||
|
|
19
|
+
typeof standard.validate !== "function") {
|
|
20
|
+
throw new InvalidConfigurationProvidedError({
|
|
21
|
+
message: "requirements must implement Standard Schema v1.",
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
const result = await standard.validate(value);
|
|
25
|
+
if (!isStandardSchemaResult(result)) {
|
|
26
|
+
throw new InvalidConfigurationProvidedError({
|
|
27
|
+
message: "requirements returned an invalid Standard Schema result.",
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
if ("issues" in result && Array.isArray(result.issues) && result.issues.length > 0) {
|
|
31
|
+
const firstIssue = result.issues[0];
|
|
32
|
+
const issuePath = firstIssue?.path && firstIssue.path.length > 0
|
|
33
|
+
? ` at ${firstIssue.path.map(formatPathSegment).join(".")}`
|
|
34
|
+
: "";
|
|
35
|
+
const issueMessage = firstIssue?.message ?? "Validation failed.";
|
|
36
|
+
throw new RequirementsValidationFailedError({
|
|
37
|
+
message: `Barekey requirements validation failed${issuePath}: ${issueMessage}`,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { BarekeyClientOptions, BarekeyStandardSchemaV1 } from "../types.js";
|
|
2
|
+
import { type InternalAuthResolver } from "./http.js";
|
|
3
|
+
type BarekeyResolvedScope = {
|
|
4
|
+
organization: string;
|
|
5
|
+
project: string;
|
|
6
|
+
environment: string;
|
|
7
|
+
};
|
|
8
|
+
export type BarekeyRuntimeContext = BarekeyResolvedScope & {
|
|
9
|
+
baseUrl: string;
|
|
10
|
+
auth: InternalAuthResolver;
|
|
11
|
+
requirements?: BarekeyStandardSchemaV1;
|
|
12
|
+
};
|
|
13
|
+
export declare function resolveRuntimeContext(options: BarekeyClientOptions, fetchFn: typeof globalThis.fetch): Promise<BarekeyRuntimeContext>;
|
|
14
|
+
export {};
|
|
15
|
+
//# sourceMappingURL=runtime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../src/internal/runtime.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,oBAAoB,EAAqB,uBAAuB,EAAE,MAAM,aAAa,CAAC;AACpG,OAAO,EAAE,KAAK,oBAAoB,EAAoB,MAAM,WAAW,CAAC;AASxE,KAAK,oBAAoB,GAAG;IAC1B,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG,oBAAoB,GAAG;IACzD,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,oBAAoB,CAAC;IAC3B,YAAY,CAAC,EAAE,uBAAuB,CAAC;CACxC,CAAC;AA8JF,wBAAsB,qBAAqB,CACzC,OAAO,EAAE,oBAAoB,EAC7B,OAAO,EAAE,OAAO,UAAU,CAAC,KAAK,GAC/B,OAAO,CAAC,qBAAqB,CAAC,CAQhC"}
|