@ultrst-armada/kloks 1.26.1 → 1.26.3
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/package.json +12 -3
- package/packages/backend/dist/chunk-3S2NSIFA.js +80 -0
- package/packages/backend/dist/{chunk-JRNH5KPV.js → chunk-EDKCP5CQ.js} +2 -2
- package/packages/backend/dist/{chunk-3W4TN6XB.js → chunk-FU6TYLI2.js} +5 -5
- package/packages/backend/dist/chunk-KPZQUA7R.js +406 -0
- package/packages/backend/dist/{chunk-IB5SLBHT.js → chunk-U4UI6KK4.js} +1 -1
- package/packages/backend/dist/{chunk-QXIMZLGS.js → chunk-WAQDOIGQ.js} +1 -1
- package/packages/backend/dist/development/index.js +5 -5
- package/packages/backend/dist/index.js +19 -17
- package/packages/backend/dist/krnl-ai-services.js +2 -2
- package/packages/backend/dist/krnl-services.js +4 -4
- package/packages/backend/dist/services/index.js +3 -3
- package/packages/backend/dist/storage/GoogleDriveAdapter.d.ts +23 -1
- package/packages/backend/dist/storage/GoogleDriveOAuth.d.ts +20 -2
- package/packages/backend/dist/storage/GoogleDriveStorageService.d.ts +7 -1
- package/packages/backend/dist/storage/GoogleDriveWorkspaceService.d.ts +69 -0
- package/packages/backend/dist/storage/GoogleDriveWorkspaceService.test.d.ts +1 -0
- package/packages/backend/dist/storage/StorageAdapterFactory.d.ts +6 -1
- package/packages/backend/dist/storage/googleDriveSdk.d.ts +7 -0
- package/packages/backend/dist/storage/index.js +5 -3
- package/packages/backend/dist/storage/types.d.ts +13 -0
- package/packages/backend/dist/storage-workspace.d.ts +2 -0
- package/packages/backend/dist/storage-workspace.js +339 -0
- package/packages/backend/dist/workspace/index.js +2 -2
- package/packages/backend/package.json +6 -1
- package/packages/state-graph/dist/browser.d.ts +28 -0
- package/packages/state-graph/dist/browser.js +302 -0
- package/packages/state-graph/dist/chunk-2CQQGFMF.js +7339 -0
- package/packages/state-graph/dist/index.js +283 -7260
- package/packages/state-graph/dist/runtime-entrypoint.test.d.ts +1 -0
- package/packages/state-graph/dist/store/CommandApi.d.ts +4 -4
- package/packages/state-graph/package.json +2 -1
- package/packages/backend/dist/chunk-52M4L74T.js +0 -247
- package/packages/backend/dist/chunk-GZD5A54P.js +0 -67
- package/packages/backend/dist/{chunk-M45KQRPR.js → chunk-GWCLYVJT.js} +3 -3
- /package/packages/state-graph/dist/{dist-es-OECEAUXH.js → dist-es-QZ6GKMM7.js} +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultrst-armada/kloks",
|
|
3
|
-
"version": "1.26.
|
|
3
|
+
"version": "1.26.3",
|
|
4
4
|
"private": false,
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public",
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
"default": "./packages/functions/dist/index.js"
|
|
37
37
|
},
|
|
38
38
|
"./state-graph": {
|
|
39
|
+
"browser": "./packages/state-graph/dist/browser.js",
|
|
39
40
|
"types": "./packages/state-graph/dist/index.d.ts",
|
|
40
41
|
"import": "./packages/state-graph/dist/index.js",
|
|
41
42
|
"default": "./packages/state-graph/dist/index.js"
|
|
@@ -214,6 +215,11 @@
|
|
|
214
215
|
"types": "./packages/audio-midi/dist/services/index.d.ts",
|
|
215
216
|
"import": "./packages/audio-midi/dist/services/index.js",
|
|
216
217
|
"default": "./packages/audio-midi/dist/services/index.js"
|
|
218
|
+
},
|
|
219
|
+
"./backend/storage-workspace": {
|
|
220
|
+
"types": "./packages/backend/dist/storage-workspace.d.ts",
|
|
221
|
+
"import": "./packages/backend/dist/storage-workspace.js",
|
|
222
|
+
"default": "./packages/backend/dist/storage-workspace.js"
|
|
217
223
|
}
|
|
218
224
|
},
|
|
219
225
|
"scripts": {
|
|
@@ -223,10 +229,13 @@
|
|
|
223
229
|
"test": "vitest run && pnpm run verify:npm-trusted-publishing",
|
|
224
230
|
"typecheck": "pnpm -r run typecheck",
|
|
225
231
|
"clean": "pnpm -r run clean",
|
|
226
|
-
"verify:npm-trusted-publishing": "node scripts/verify-npm-trusted-publishing.mjs"
|
|
232
|
+
"verify:npm-trusted-publishing": "node scripts/verify-npm-trusted-publishing.mjs",
|
|
233
|
+
"boundary": "node scripts/architecture-boundary.mjs",
|
|
234
|
+
"boundary:clean-room": "node scripts/clean-room-boundary.mjs",
|
|
235
|
+
"boundary:relative": "node scripts/relative-package-boundary.mjs"
|
|
227
236
|
},
|
|
228
237
|
"dependencies": {
|
|
229
|
-
"@dinilmusick/gutts": "^
|
|
238
|
+
"@dinilmusick/gutts": "^2.0.2",
|
|
230
239
|
"@webcontainer/api": "^1.6.4",
|
|
231
240
|
"ws": "^8.18.0",
|
|
232
241
|
"zod": "^3.24.1",
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import {
|
|
2
|
+
google
|
|
3
|
+
} from "./chunk-KPZQUA7R.js";
|
|
4
|
+
|
|
5
|
+
// src/storage/GoogleDriveOAuth.ts
|
|
6
|
+
var GOOGLE_DRIVE_STORAGE_SCOPE = "https://www.googleapis.com/auth/drive";
|
|
7
|
+
var GOOGLE_DRIVE_FILE_SCOPE = "https://www.googleapis.com/auth/drive.file";
|
|
8
|
+
function createOAuthClient(config) {
|
|
9
|
+
if (!config.clientId?.trim()) throw new Error("Google OAuth client ID is required.");
|
|
10
|
+
if (!config.redirectUri?.trim()) throw new Error("Google OAuth redirect URI is required.");
|
|
11
|
+
return new google.auth.OAuth2(config.clientId, config.clientSecret, config.redirectUri);
|
|
12
|
+
}
|
|
13
|
+
function createGoogleDriveAuthorizationUrl(config, state, codeChallenge) {
|
|
14
|
+
if (!state?.trim()) throw new Error("A non-empty OAuth state value is required.");
|
|
15
|
+
if (codeChallenge !== void 0 && !codeChallenge.trim()) {
|
|
16
|
+
throw new Error("PKCE code challenge cannot be empty.");
|
|
17
|
+
}
|
|
18
|
+
const client = createOAuthClient(config);
|
|
19
|
+
return client.generateAuthUrl({
|
|
20
|
+
access_type: "offline",
|
|
21
|
+
prompt: "consent",
|
|
22
|
+
include_granted_scopes: true,
|
|
23
|
+
response_type: "code",
|
|
24
|
+
scope: config.scopes?.length ? config.scopes : [GOOGLE_DRIVE_STORAGE_SCOPE],
|
|
25
|
+
state,
|
|
26
|
+
...codeChallenge ? { code_challenge: codeChallenge, code_challenge_method: "S256" } : {}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
async function exchangeGoogleDriveCode(config, code, codeVerifier, existingRefreshToken) {
|
|
30
|
+
if (!code?.trim()) throw new Error("Google OAuth authorization code is required.");
|
|
31
|
+
const client = createOAuthClient(config);
|
|
32
|
+
try {
|
|
33
|
+
const { tokens } = await client.getToken({
|
|
34
|
+
code,
|
|
35
|
+
...codeVerifier ? { codeVerifier } : {}
|
|
36
|
+
});
|
|
37
|
+
if (!tokens.access_token) {
|
|
38
|
+
throw new Error("Google OAuth token exchange returned no access token.");
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
accessToken: tokens.access_token,
|
|
42
|
+
...tokens.refresh_token || existingRefreshToken ? { refreshToken: tokens.refresh_token || existingRefreshToken } : {},
|
|
43
|
+
...tokens.expiry_date ? { expiresAt: tokens.expiry_date } : {},
|
|
44
|
+
...tokens.token_type ? { tokenType: tokens.token_type } : {}
|
|
45
|
+
};
|
|
46
|
+
} catch (error) {
|
|
47
|
+
if (error instanceof Error && error.message === "Google OAuth token exchange returned no access token.") {
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
throw new Error("Google OAuth authorization-code exchange failed.");
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async function refreshGoogleDriveAccessToken(clientId, refreshToken, clientSecret, redirectUri = "http://localhost") {
|
|
54
|
+
if (!clientId?.trim() || !refreshToken?.trim()) {
|
|
55
|
+
throw new Error("Google Drive refresh credentials are incomplete.");
|
|
56
|
+
}
|
|
57
|
+
const client = new google.auth.OAuth2(clientId, clientSecret, redirectUri);
|
|
58
|
+
client.setCredentials({ refresh_token: refreshToken });
|
|
59
|
+
try {
|
|
60
|
+
const { token } = await client.getAccessToken();
|
|
61
|
+
const credentials = client.credentials;
|
|
62
|
+
if (!token) throw new Error("Google OAuth refresh returned no access token.");
|
|
63
|
+
return {
|
|
64
|
+
accessToken: token,
|
|
65
|
+
refreshToken: credentials.refresh_token || refreshToken,
|
|
66
|
+
...credentials.expiry_date ? { expiresAt: credentials.expiry_date } : {},
|
|
67
|
+
...credentials.token_type ? { tokenType: credentials.token_type } : {}
|
|
68
|
+
};
|
|
69
|
+
} catch {
|
|
70
|
+
throw new Error("Google Drive access-token refresh failed. Reauthorize the Google account if the refresh credential is invalid or revoked.");
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export {
|
|
75
|
+
GOOGLE_DRIVE_STORAGE_SCOPE,
|
|
76
|
+
GOOGLE_DRIVE_FILE_SCOPE,
|
|
77
|
+
createGoogleDriveAuthorizationUrl,
|
|
78
|
+
exchangeGoogleDriveCode,
|
|
79
|
+
refreshGoogleDriveAccessToken
|
|
80
|
+
};
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
workspaceGraphService
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-WAQDOIGQ.js";
|
|
4
4
|
import {
|
|
5
5
|
getSrklAccounts,
|
|
6
6
|
srklListBackblazeBuckets
|
|
7
7
|
} from "./chunk-RIOYKF3X.js";
|
|
8
8
|
import {
|
|
9
9
|
StorageAdapterFactory
|
|
10
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-FU6TYLI2.js";
|
|
11
11
|
import {
|
|
12
12
|
b2ListBuckets
|
|
13
13
|
} from "./chunk-4QEMALXF.js";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
BaseStorageAdapter,
|
|
3
3
|
GoogleDriveAdapter
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-KPZQUA7R.js";
|
|
5
5
|
|
|
6
6
|
// src/storage/LocalFSAdapter.ts
|
|
7
7
|
import { promises as fs } from "fs";
|
|
@@ -527,8 +527,8 @@ var BackblazeB2Adapter = class extends BaseStorageAdapter {
|
|
|
527
527
|
|
|
528
528
|
// src/storage/GoogleDriveStorageService.ts
|
|
529
529
|
var GoogleDriveStorageService = class extends GoogleDriveAdapter {
|
|
530
|
-
constructor(config) {
|
|
531
|
-
super({ ...config, provider: "google_drive" });
|
|
530
|
+
constructor(config, runtimeAuth = {}) {
|
|
531
|
+
super({ ...config, provider: "google_drive" }, runtimeAuth);
|
|
532
532
|
}
|
|
533
533
|
};
|
|
534
534
|
|
|
@@ -609,7 +609,7 @@ var SupabaseStorageAdapter = class extends BaseStorageAdapter {
|
|
|
609
609
|
|
|
610
610
|
// src/storage/StorageAdapterFactory.ts
|
|
611
611
|
var StorageAdapterFactory = class {
|
|
612
|
-
static create(config) {
|
|
612
|
+
static create(config, runtimeOptions = {}) {
|
|
613
613
|
switch (config.provider) {
|
|
614
614
|
case "local_fs":
|
|
615
615
|
return new LocalFSAdapter(config);
|
|
@@ -619,7 +619,7 @@ var StorageAdapterFactory = class {
|
|
|
619
619
|
case "supabase":
|
|
620
620
|
return new SupabaseStorageAdapter(config);
|
|
621
621
|
case "google_drive":
|
|
622
|
-
return new GoogleDriveStorageService(config);
|
|
622
|
+
return new GoogleDriveStorageService(config, runtimeOptions.googleDrive);
|
|
623
623
|
case "s3":
|
|
624
624
|
case "s3_compatible":
|
|
625
625
|
return new BucketStorageService(config);
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
// src/storage/BaseStorageAdapter.ts
|
|
2
|
+
import { SkipoBase } from "@ultrst-armada/kloks/functions";
|
|
3
|
+
var BaseStorageAdapter = class extends SkipoBase {
|
|
4
|
+
config;
|
|
5
|
+
constructor(config) {
|
|
6
|
+
super({ name: "BaseStorageAdapter" });
|
|
7
|
+
this.config = config;
|
|
8
|
+
}
|
|
9
|
+
async deleteFolder(pathStr) {
|
|
10
|
+
const items = await this.list(pathStr);
|
|
11
|
+
let count = 0;
|
|
12
|
+
for (const item of items) {
|
|
13
|
+
await this.delete(item.path);
|
|
14
|
+
count++;
|
|
15
|
+
}
|
|
16
|
+
return { success: true, deletedCount: count };
|
|
17
|
+
}
|
|
18
|
+
async renameFolder(oldPath, newPath) {
|
|
19
|
+
const items = await this.list(oldPath);
|
|
20
|
+
let count = 0;
|
|
21
|
+
for (const item of items) {
|
|
22
|
+
const rel = item.path.startsWith(oldPath) ? item.path.slice(oldPath.length).replace(/^\/+/, "") : item.path;
|
|
23
|
+
const target = `${newPath.replace(/\/+$/, "")}/${rel}`;
|
|
24
|
+
const data = await this.read(item.path);
|
|
25
|
+
await this.write(target, data);
|
|
26
|
+
await this.delete(item.path);
|
|
27
|
+
count++;
|
|
28
|
+
}
|
|
29
|
+
return { success: true, movedCount: count };
|
|
30
|
+
}
|
|
31
|
+
async ensureDirectory(pathStr) {
|
|
32
|
+
await this.mkdir(pathStr);
|
|
33
|
+
}
|
|
34
|
+
async testConnection() {
|
|
35
|
+
const start = performance.now();
|
|
36
|
+
try {
|
|
37
|
+
await this.list("");
|
|
38
|
+
return { ok: true, latencyMs: Math.round(performance.now() - start) };
|
|
39
|
+
} catch (e) {
|
|
40
|
+
return { ok: false, latencyMs: Math.round(performance.now() - start), error: e.message };
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
normalizePath(p) {
|
|
44
|
+
return p.replace(/\\/g, "/").replace(/^\/+/, "");
|
|
45
|
+
}
|
|
46
|
+
getExtension(p) {
|
|
47
|
+
const parts = p.split(".");
|
|
48
|
+
return parts.length > 1 ? parts[parts.length - 1] : "";
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// src/storage/googleDriveSdk.ts
|
|
53
|
+
import { google } from "@dinilmusick/gutts/storage";
|
|
54
|
+
|
|
55
|
+
// src/storage/GoogleDriveAdapter.ts
|
|
56
|
+
import { Readable } from "stream";
|
|
57
|
+
var FOLDER_MIME_TYPE = "application/vnd.google-apps.folder";
|
|
58
|
+
function toTokenSet(credentials, fallback) {
|
|
59
|
+
const accessToken = credentials.access_token || fallback?.accessToken;
|
|
60
|
+
if (!accessToken) return void 0;
|
|
61
|
+
return {
|
|
62
|
+
accessToken,
|
|
63
|
+
...credentials.refresh_token || fallback?.refreshToken ? { refreshToken: credentials.refresh_token || fallback?.refreshToken } : {},
|
|
64
|
+
...credentials.expiry_date || fallback?.expiresAt ? { expiresAt: credentials.expiry_date || fallback?.expiresAt } : {},
|
|
65
|
+
...credentials.token_type || fallback?.tokenType ? { tokenType: credentials.token_type || fallback?.tokenType } : {}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
var GoogleDriveAdapter = class extends BaseStorageAdapter {
|
|
69
|
+
client;
|
|
70
|
+
auth;
|
|
71
|
+
authPromise;
|
|
72
|
+
pendingCredentialPersistence;
|
|
73
|
+
credentialPersistenceError;
|
|
74
|
+
runtimeAuth;
|
|
75
|
+
constructor(config, runtimeAuth = {}) {
|
|
76
|
+
super({ ...config, provider: "google_drive" });
|
|
77
|
+
this.runtimeAuth = runtimeAuth;
|
|
78
|
+
}
|
|
79
|
+
async getAuth() {
|
|
80
|
+
if (this.auth) return this.auth;
|
|
81
|
+
if (this.authPromise) return this.authPromise;
|
|
82
|
+
this.authPromise = (async () => {
|
|
83
|
+
const client = new google.auth.OAuth2(
|
|
84
|
+
this.config.googleClientId,
|
|
85
|
+
this.runtimeAuth.clientSecret,
|
|
86
|
+
this.config.googleRedirectUri
|
|
87
|
+
);
|
|
88
|
+
const supplied = this.runtimeAuth.getCredentials ? await this.runtimeAuth.getCredentials() : void 0;
|
|
89
|
+
const credentials = supplied ?? {
|
|
90
|
+
accessToken: this.config.googleAccessToken,
|
|
91
|
+
refreshToken: this.config.googleRefreshToken,
|
|
92
|
+
expiresAt: this.config.googleAccessTokenExpiresAt
|
|
93
|
+
};
|
|
94
|
+
if (!credentials.accessToken && !credentials.refreshToken) {
|
|
95
|
+
throw new Error("Google Drive OAuth credentials are incomplete. Reauthorize the Google account.");
|
|
96
|
+
}
|
|
97
|
+
client.setCredentials({
|
|
98
|
+
...credentials.accessToken ? { access_token: credentials.accessToken } : {},
|
|
99
|
+
...credentials.refreshToken ? { refresh_token: credentials.refreshToken } : {},
|
|
100
|
+
...credentials.expiresAt ? { expiry_date: credentials.expiresAt } : {},
|
|
101
|
+
...credentials.tokenType ? { token_type: credentials.tokenType } : {}
|
|
102
|
+
});
|
|
103
|
+
client.on("tokens", (tokens) => {
|
|
104
|
+
const next = toTokenSet({ ...client.credentials, ...tokens }, credentials);
|
|
105
|
+
if (!next) return;
|
|
106
|
+
this.config.googleAccessToken = next.accessToken;
|
|
107
|
+
if (next.refreshToken) this.config.googleRefreshToken = next.refreshToken;
|
|
108
|
+
if (next.expiresAt) this.config.googleAccessTokenExpiresAt = next.expiresAt;
|
|
109
|
+
if (!this.runtimeAuth.persistCredentials) return;
|
|
110
|
+
this.pendingCredentialPersistence = Promise.resolve(this.runtimeAuth.persistCredentials(next)).then(
|
|
111
|
+
() => void 0,
|
|
112
|
+
() => {
|
|
113
|
+
this.credentialPersistenceError = new Error("Google Drive credentials could not be persisted securely.");
|
|
114
|
+
}
|
|
115
|
+
);
|
|
116
|
+
});
|
|
117
|
+
this.auth = client;
|
|
118
|
+
return client;
|
|
119
|
+
})();
|
|
120
|
+
try {
|
|
121
|
+
return await this.authPromise;
|
|
122
|
+
} catch (error) {
|
|
123
|
+
this.authPromise = void 0;
|
|
124
|
+
throw error;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
async getClient() {
|
|
128
|
+
if (this.client) {
|
|
129
|
+
if (this.pendingCredentialPersistence) await this.pendingCredentialPersistence;
|
|
130
|
+
if (this.credentialPersistenceError) throw this.credentialPersistenceError;
|
|
131
|
+
return this.client;
|
|
132
|
+
}
|
|
133
|
+
const auth = await this.getAuth();
|
|
134
|
+
const token = await auth.getAccessToken();
|
|
135
|
+
if (this.pendingCredentialPersistence) await this.pendingCredentialPersistence;
|
|
136
|
+
if (this.credentialPersistenceError) throw this.credentialPersistenceError;
|
|
137
|
+
if (!token.token) {
|
|
138
|
+
throw new Error("Google Drive authorization returned no access token. Reauthorize the Google account.");
|
|
139
|
+
}
|
|
140
|
+
this.config.googleAccessToken = token.token;
|
|
141
|
+
if (auth.credentials.expiry_date) {
|
|
142
|
+
this.config.googleAccessTokenExpiresAt = auth.credentials.expiry_date;
|
|
143
|
+
}
|
|
144
|
+
if (auth.credentials.refresh_token) {
|
|
145
|
+
this.config.googleRefreshToken = auth.credentials.refresh_token;
|
|
146
|
+
}
|
|
147
|
+
this.client = google.drive({ version: "v3", auth });
|
|
148
|
+
return this.client;
|
|
149
|
+
}
|
|
150
|
+
async findChild(parentId, name) {
|
|
151
|
+
const drive = await this.getClient();
|
|
152
|
+
const escapedName = name.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
|
|
153
|
+
const response = await drive.files.list({
|
|
154
|
+
q: `'${parentId}' in parents and name = '${escapedName}' and trashed = false`,
|
|
155
|
+
fields: "files(id,name,size,modifiedTime,mimeType,parents)",
|
|
156
|
+
pageSize: 10,
|
|
157
|
+
supportsAllDrives: true,
|
|
158
|
+
includeItemsFromAllDrives: true
|
|
159
|
+
});
|
|
160
|
+
return response.data.files?.[0] || null;
|
|
161
|
+
}
|
|
162
|
+
async resolveParent(relativeDir, create = false) {
|
|
163
|
+
let parent = this.config.googleRootFolderId || "root";
|
|
164
|
+
for (const part of this.normalizePath(relativeDir).split("/").filter(Boolean)) {
|
|
165
|
+
let child = await this.findChild(parent, part);
|
|
166
|
+
if (!child && create) {
|
|
167
|
+
const drive = await this.getClient();
|
|
168
|
+
const response = await drive.files.create({
|
|
169
|
+
requestBody: {
|
|
170
|
+
name: part,
|
|
171
|
+
mimeType: FOLDER_MIME_TYPE,
|
|
172
|
+
parents: [parent]
|
|
173
|
+
},
|
|
174
|
+
fields: "id,name,mimeType,parents,modifiedTime",
|
|
175
|
+
supportsAllDrives: true
|
|
176
|
+
});
|
|
177
|
+
child = response.data;
|
|
178
|
+
}
|
|
179
|
+
if (!child?.id || child.mimeType !== FOLDER_MIME_TYPE) {
|
|
180
|
+
throw new Error(`Google Drive directory not found: ${part}`);
|
|
181
|
+
}
|
|
182
|
+
parent = child.id;
|
|
183
|
+
}
|
|
184
|
+
return parent;
|
|
185
|
+
}
|
|
186
|
+
split(pathStr) {
|
|
187
|
+
const key = this.normalizePath(pathStr);
|
|
188
|
+
const i = key.lastIndexOf("/");
|
|
189
|
+
return i < 0 ? { dir: "", name: key } : { dir: key.slice(0, i), name: key.slice(i + 1) };
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Return only non-secret connection metadata for persistence alongside the
|
|
193
|
+
* authenticated application user's storage connection record.
|
|
194
|
+
*/
|
|
195
|
+
getConnectionMetadata() {
|
|
196
|
+
return {
|
|
197
|
+
provider: "google_drive",
|
|
198
|
+
...this.config.googleClientId ? { googleClientId: this.config.googleClientId } : {},
|
|
199
|
+
...this.config.googleRedirectUri ? { googleRedirectUri: this.config.googleRedirectUri } : {},
|
|
200
|
+
...this.config.googleAccountId ? { googleAccountId: this.config.googleAccountId } : {},
|
|
201
|
+
...this.config.googleRootFolderId ? { googleRootFolderId: this.config.googleRootFolderId } : {}
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
/** Browse only folders directly below the supplied parent (or selected storage root). */
|
|
205
|
+
async listFolders(parentFolderId = this.config.googleRootFolderId || "root") {
|
|
206
|
+
const drive = await this.getClient();
|
|
207
|
+
const folders = [];
|
|
208
|
+
let pageToken;
|
|
209
|
+
do {
|
|
210
|
+
const response = await drive.files.list({
|
|
211
|
+
q: `'${parentFolderId}' in parents and mimeType = '${FOLDER_MIME_TYPE}' and trashed = false`,
|
|
212
|
+
fields: "nextPageToken,files(id,name,mimeType,parents,modifiedTime)",
|
|
213
|
+
pageSize: 1e3,
|
|
214
|
+
supportsAllDrives: true,
|
|
215
|
+
includeItemsFromAllDrives: true,
|
|
216
|
+
...pageToken ? { pageToken } : {}
|
|
217
|
+
});
|
|
218
|
+
for (const folder of response.data.files || []) {
|
|
219
|
+
if (!folder.id || !folder.name) continue;
|
|
220
|
+
folders.push({
|
|
221
|
+
id: folder.id,
|
|
222
|
+
name: folder.name,
|
|
223
|
+
mimeType: FOLDER_MIME_TYPE,
|
|
224
|
+
parentIds: folder.parents || [],
|
|
225
|
+
modifiedAt: folder.modifiedTime ? Date.parse(folder.modifiedTime) : 0
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
pageToken = response.data.nextPageToken || void 0;
|
|
229
|
+
} while (pageToken);
|
|
230
|
+
return folders;
|
|
231
|
+
}
|
|
232
|
+
/** Resolve and validate a provider-native folder ID before it is saved as a root. */
|
|
233
|
+
async resolveFolder(folderId) {
|
|
234
|
+
if (!folderId?.trim()) throw new Error("A Google Drive folder ID is required.");
|
|
235
|
+
const drive = await this.getClient();
|
|
236
|
+
const response = await drive.files.get({
|
|
237
|
+
fileId: folderId,
|
|
238
|
+
fields: "id,name,mimeType,parents,modifiedTime",
|
|
239
|
+
supportsAllDrives: true
|
|
240
|
+
});
|
|
241
|
+
const folder = response.data;
|
|
242
|
+
if (!folder.id || !folder.name || folder.mimeType !== FOLDER_MIME_TYPE) {
|
|
243
|
+
throw new Error("The selected Google Drive item is not a folder.");
|
|
244
|
+
}
|
|
245
|
+
return {
|
|
246
|
+
id: folder.id,
|
|
247
|
+
name: folder.name,
|
|
248
|
+
mimeType: FOLDER_MIME_TYPE,
|
|
249
|
+
parentIds: folder.parents || [],
|
|
250
|
+
modifiedAt: folder.modifiedTime ? Date.parse(folder.modifiedTime) : 0
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
/** Select a validated folder as this connection's storage/workspace root. */
|
|
254
|
+
async selectRootFolder(folderId) {
|
|
255
|
+
const folder = await this.resolveFolder(folderId);
|
|
256
|
+
this.config.googleRootFolderId = folder.id;
|
|
257
|
+
return folder;
|
|
258
|
+
}
|
|
259
|
+
async read(pathStr) {
|
|
260
|
+
const { dir, name } = this.split(pathStr);
|
|
261
|
+
const parent = await this.resolveParent(dir);
|
|
262
|
+
const file = await this.findChild(parent, name);
|
|
263
|
+
if (!file?.id) throw new Error(`Google Drive file not found: ${pathStr}`);
|
|
264
|
+
if (file.mimeType === FOLDER_MIME_TYPE) throw new Error(`Google Drive path is a folder, not a file: ${pathStr}`);
|
|
265
|
+
const drive = await this.getClient();
|
|
266
|
+
const response = await drive.files.get(
|
|
267
|
+
{ fileId: file.id, alt: "media", supportsAllDrives: true },
|
|
268
|
+
{ responseType: "arraybuffer" }
|
|
269
|
+
);
|
|
270
|
+
return new Uint8Array(response.data);
|
|
271
|
+
}
|
|
272
|
+
async write(pathStr, data, contentType = "application/octet-stream") {
|
|
273
|
+
const { dir, name } = this.split(pathStr);
|
|
274
|
+
if (!name) throw new Error("A Google Drive file name is required.");
|
|
275
|
+
const parent = await this.resolveParent(dir, true);
|
|
276
|
+
const existing = await this.findChild(parent, name);
|
|
277
|
+
const bytes = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
278
|
+
const drive = await this.getClient();
|
|
279
|
+
if (existing?.id) {
|
|
280
|
+
if (existing.mimeType === FOLDER_MIME_TYPE) throw new Error(`Google Drive path is a folder: ${pathStr}`);
|
|
281
|
+
await drive.files.update({
|
|
282
|
+
fileId: existing.id,
|
|
283
|
+
requestBody: { name, mimeType: contentType },
|
|
284
|
+
media: { mimeType: contentType, body: Readable.from(bytes) },
|
|
285
|
+
fields: "id",
|
|
286
|
+
supportsAllDrives: true
|
|
287
|
+
});
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
await drive.files.create({
|
|
291
|
+
requestBody: { name, mimeType: contentType, parents: [parent] },
|
|
292
|
+
media: { mimeType: contentType, body: Readable.from(bytes) },
|
|
293
|
+
fields: "id",
|
|
294
|
+
supportsAllDrives: true
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
async delete(pathStr) {
|
|
298
|
+
const { dir, name } = this.split(pathStr);
|
|
299
|
+
const parent = await this.resolveParent(dir);
|
|
300
|
+
const file = await this.findChild(parent, name);
|
|
301
|
+
if (!file?.id) return;
|
|
302
|
+
const drive = await this.getClient();
|
|
303
|
+
try {
|
|
304
|
+
await drive.files.delete({ fileId: file.id, supportsAllDrives: true });
|
|
305
|
+
} catch (error) {
|
|
306
|
+
if (error?.response?.status !== 404 && error?.code !== 404) throw error;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
async exists(pathStr) {
|
|
310
|
+
try {
|
|
311
|
+
await this.stat(pathStr);
|
|
312
|
+
return true;
|
|
313
|
+
} catch (error) {
|
|
314
|
+
if (error?.response?.status === 404 || error?.code === 404) return false;
|
|
315
|
+
throw error;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
async list(pathStr = "") {
|
|
319
|
+
const parent = await this.resolveParent(pathStr);
|
|
320
|
+
const drive = await this.getClient();
|
|
321
|
+
const results = [];
|
|
322
|
+
let pageToken;
|
|
323
|
+
do {
|
|
324
|
+
const response = await drive.files.list({
|
|
325
|
+
q: `'${parent}' in parents and trashed = false`,
|
|
326
|
+
fields: "nextPageToken,files(id,name,size,modifiedTime,mimeType)",
|
|
327
|
+
pageSize: 1e3,
|
|
328
|
+
supportsAllDrives: true,
|
|
329
|
+
includeItemsFromAllDrives: true,
|
|
330
|
+
...pageToken ? { pageToken } : {}
|
|
331
|
+
});
|
|
332
|
+
for (const file of response.data.files || []) {
|
|
333
|
+
if (!file.name) continue;
|
|
334
|
+
results.push({
|
|
335
|
+
path: this.normalizePath(pathStr ? `${pathStr}/${file.name}` : file.name),
|
|
336
|
+
size: Number(file.size || 0),
|
|
337
|
+
modifiedAt: file.modifiedTime ? Date.parse(file.modifiedTime) : 0,
|
|
338
|
+
...file.id ? { id: file.id } : {},
|
|
339
|
+
...file.mimeType ? { mimeType: file.mimeType } : {},
|
|
340
|
+
isFolder: file.mimeType === FOLDER_MIME_TYPE
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
pageToken = response.data.nextPageToken || void 0;
|
|
344
|
+
} while (pageToken);
|
|
345
|
+
return results;
|
|
346
|
+
}
|
|
347
|
+
async stat(pathStr) {
|
|
348
|
+
const { dir, name } = this.split(pathStr);
|
|
349
|
+
const parent = await this.resolveParent(dir);
|
|
350
|
+
const file = await this.findChild(parent, name);
|
|
351
|
+
if (!file?.id) throw new Error(`Google Drive file not found: ${pathStr}`);
|
|
352
|
+
return {
|
|
353
|
+
path: pathStr,
|
|
354
|
+
size: Number(file.size || 0),
|
|
355
|
+
modifiedAt: file.modifiedTime ? Date.parse(file.modifiedTime) : 0,
|
|
356
|
+
id: file.id,
|
|
357
|
+
...file.mimeType ? { mimeType: file.mimeType } : {},
|
|
358
|
+
isFolder: file.mimeType === FOLDER_MIME_TYPE
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
async mkdir(pathStr) {
|
|
362
|
+
await this.resolveParent(pathStr, true);
|
|
363
|
+
}
|
|
364
|
+
/** Resolve the stable Google account identity for association with the app user's server-side connection record. */
|
|
365
|
+
async getAccountIdentity() {
|
|
366
|
+
const drive = await this.getClient();
|
|
367
|
+
const response = await drive.about.get({ fields: "user(permissionId,emailAddress)" });
|
|
368
|
+
const accountId = response.data.user?.permissionId || response.data.user?.emailAddress;
|
|
369
|
+
if (!accountId) throw new Error("Google Drive did not return an account identity.");
|
|
370
|
+
if (this.config.googleAccountId && this.config.googleAccountId !== accountId) {
|
|
371
|
+
throw new Error("Google Drive account identity does not match the saved storage connection.");
|
|
372
|
+
}
|
|
373
|
+
this.config.googleAccountId = accountId;
|
|
374
|
+
return {
|
|
375
|
+
accountId,
|
|
376
|
+
...response.data.user?.emailAddress ? { email: response.data.user.emailAddress } : {}
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
async testConnection() {
|
|
380
|
+
const start = performance.now();
|
|
381
|
+
try {
|
|
382
|
+
const identity = await this.getAccountIdentity();
|
|
383
|
+
return {
|
|
384
|
+
ok: true,
|
|
385
|
+
latencyMs: Math.round(performance.now() - start),
|
|
386
|
+
details: {
|
|
387
|
+
accountId: identity.accountId,
|
|
388
|
+
...identity.email ? { email: identity.email } : {},
|
|
389
|
+
rootFolderId: this.config.googleRootFolderId || "root"
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
} catch (error) {
|
|
393
|
+
return {
|
|
394
|
+
ok: false,
|
|
395
|
+
latencyMs: Math.round(performance.now() - start),
|
|
396
|
+
error: error?.message || "Google Drive authorization failed."
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
export {
|
|
403
|
+
BaseStorageAdapter,
|
|
404
|
+
google,
|
|
405
|
+
GoogleDriveAdapter
|
|
406
|
+
};
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PlaywrightInspectionRuntime,
|
|
3
|
+
PlaywrightInspectionServiceDefinition,
|
|
4
|
+
PlaywrightInspectionServiceFacade
|
|
5
|
+
} from "../chunk-3VWBZEMG.js";
|
|
1
6
|
import "../chunk-YEGS4HTA.js";
|
|
2
7
|
import {
|
|
3
8
|
WebContainerServiceDefinition,
|
|
@@ -9,11 +14,6 @@ import {
|
|
|
9
14
|
parseDevelopmentDiagnostics,
|
|
10
15
|
resolveWebContainerCommand
|
|
11
16
|
} from "../chunk-CA5HVC4X.js";
|
|
12
|
-
import {
|
|
13
|
-
PlaywrightInspectionRuntime,
|
|
14
|
-
PlaywrightInspectionServiceDefinition,
|
|
15
|
-
PlaywrightInspectionServiceFacade
|
|
16
|
-
} from "../chunk-3VWBZEMG.js";
|
|
17
17
|
import {
|
|
18
18
|
createService,
|
|
19
19
|
createServiceDefinition
|