@burdenoff/be-sdk 2026.1021.5 → 2026.1021.6
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/README.md +42 -0
- package/dist/cache/index.js +2 -2
- package/dist/chunk-4KK6YJ46.js +41 -0
- package/dist/chunk-4KK6YJ46.js.map +1 -0
- package/dist/chunk-AE4PRE4N.js +731 -0
- package/dist/chunk-AE4PRE4N.js.map +1 -0
- package/dist/{chunk-YCSIHUYP.js → chunk-HDUS6UBO.js} +3 -3
- package/dist/{chunk-YCSIHUYP.js.map → chunk-HDUS6UBO.js.map} +1 -1
- package/dist/{chunk-HIO3DNKK.js → chunk-KCGJCY5U.js} +2 -2
- package/dist/{chunk-HIO3DNKK.js.map → chunk-KCGJCY5U.js.map} +1 -1
- package/dist/{chunk-AT52BRVL.js → chunk-OXGIVASV.js} +3 -3
- package/dist/{chunk-AT52BRVL.js.map → chunk-OXGIVASV.js.map} +1 -1
- package/dist/{chunk-V7QNQN7Y.js → chunk-PPFJTSHA.js} +3 -3
- package/dist/{chunk-V7QNQN7Y.js.map → chunk-PPFJTSHA.js.map} +1 -1
- package/dist/{chunk-4UABBBB4.js → chunk-QZG27OR7.js} +5 -4
- package/dist/chunk-QZG27OR7.js.map +1 -0
- package/dist/{chunk-VBUUEOHC.js → chunk-UCNTCCV6.js} +4 -39
- package/dist/chunk-UCNTCCV6.js.map +1 -0
- package/dist/{chunk-XUXSX5UX.js → chunk-X3MNBBOL.js} +3 -3
- package/dist/{chunk-XUXSX5UX.js.map → chunk-X3MNBBOL.js.map} +1 -1
- package/dist/{chunk-CJS6KURB.js → chunk-XJKVW34Q.js} +3 -3
- package/dist/{chunk-CJS6KURB.js.map → chunk-XJKVW34Q.js.map} +1 -1
- package/dist/{client-X4PGGWPO.js → client-WSFUJHLI.js} +4 -3
- package/dist/{client-X4PGGWPO.js.map → client-WSFUJHLI.js.map} +1 -1
- package/dist/export/index.js +5 -4
- package/dist/files/index.d.ts +3 -0
- package/dist/files/index.js +5 -0
- package/dist/files/index.js.map +1 -0
- package/dist/gateway/index.js +4 -3
- package/dist/gateway/security/index.js +1 -1
- package/dist/graph/index.js +1 -1
- package/dist/index-BjtgcJxt.d.ts +208 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +24 -22
- package/dist/index.js.map +1 -1
- package/dist/init/index.js +5 -4
- package/dist/server/index.js +1 -1
- package/dist/tags/index.js +1 -1
- package/dist/tracker/index.js +2 -2
- package/package.json +3 -3
- package/dist/chunk-4UABBBB4.js.map +0 -1
- package/dist/chunk-VBUUEOHC.js.map +0 -1
|
@@ -0,0 +1,731 @@
|
|
|
1
|
+
import { GatewayError } from './chunk-4KK6YJ46.js';
|
|
2
|
+
import { __export, __name } from './chunk-XE4OXN2W.js';
|
|
3
|
+
import { createHash } from 'crypto';
|
|
4
|
+
|
|
5
|
+
// src/files/index.ts
|
|
6
|
+
var files_exports = {};
|
|
7
|
+
__export(files_exports, {
|
|
8
|
+
FILES_CLIENT_GRAPHQL_CODES: () => FILES_CLIENT_GRAPHQL_CODES,
|
|
9
|
+
FILE_ACTOR_TYPES: () => FILE_ACTOR_TYPES,
|
|
10
|
+
FILE_REFERENCE_KIND: () => FILE_REFERENCE_KIND,
|
|
11
|
+
FILE_VISIBILITIES: () => FILE_VISIBILITIES,
|
|
12
|
+
FilesClient: () => FilesClient,
|
|
13
|
+
FilesClientError: () => FilesClientError,
|
|
14
|
+
MAX_DOWNLOAD_URL_TTL_SECONDS: () => MAX_DOWNLOAD_URL_TTL_SECONDS,
|
|
15
|
+
MIN_DOWNLOAD_URL_TTL_SECONDS: () => MIN_DOWNLOAD_URL_TTL_SECONDS,
|
|
16
|
+
clampDownloadUrlTtl: () => clampDownloadUrlTtl,
|
|
17
|
+
classifyFilesErrors: () => classifyFilesErrors,
|
|
18
|
+
fileExtensionOf: () => fileExtensionOf,
|
|
19
|
+
isFileReference: () => isFileReference,
|
|
20
|
+
isFilesClientError: () => isFilesClientError,
|
|
21
|
+
mapFileMetadata: () => mapFileMetadata,
|
|
22
|
+
toFileReference: () => toFileReference,
|
|
23
|
+
toFilesClientError: () => toFilesClientError
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// src/files/types.ts
|
|
27
|
+
var FILE_ACTOR_TYPES = [
|
|
28
|
+
"user",
|
|
29
|
+
"app",
|
|
30
|
+
"service",
|
|
31
|
+
"super_admin"
|
|
32
|
+
];
|
|
33
|
+
var FILE_VISIBILITIES = [
|
|
34
|
+
"PRIVATE",
|
|
35
|
+
"PUBLIC",
|
|
36
|
+
"TEAM",
|
|
37
|
+
"WORKSPACE"
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
// src/files/errors.ts
|
|
41
|
+
var FILES_CLIENT_GRAPHQL_CODES = {
|
|
42
|
+
INVALID_INPUT: "BAD_USER_INPUT",
|
|
43
|
+
MISSING_CONTEXT: "UNAUTHENTICATED",
|
|
44
|
+
// Never FORBIDDEN: a file the actor cannot see must be indistinguishable from one that does not exist.
|
|
45
|
+
NOT_FOUND: "NOT_FOUND",
|
|
46
|
+
QUOTA_EXCEEDED: "QUOTA_EXCEEDED",
|
|
47
|
+
TOO_LARGE: "BAD_USER_INPUT",
|
|
48
|
+
STORAGE_ERROR: "INTERNAL_SERVER_ERROR",
|
|
49
|
+
UPSTREAM_ERROR: "INTERNAL_SERVER_ERROR",
|
|
50
|
+
CONFIGURATION_ERROR: "INTERNAL_SERVER_ERROR"
|
|
51
|
+
};
|
|
52
|
+
var FilesClientError = class extends Error {
|
|
53
|
+
static {
|
|
54
|
+
__name(this, "FilesClientError");
|
|
55
|
+
}
|
|
56
|
+
code;
|
|
57
|
+
details;
|
|
58
|
+
constructor(code, message, details = {}) {
|
|
59
|
+
super(message);
|
|
60
|
+
this.name = "FilesClientError";
|
|
61
|
+
this.code = code;
|
|
62
|
+
this.details = details;
|
|
63
|
+
}
|
|
64
|
+
/** The GraphQL `extensions.code` this error maps to. */
|
|
65
|
+
get graphqlCode() {
|
|
66
|
+
return FILES_CLIENT_GRAPHQL_CODES[this.code];
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
function isFilesClientError(error) {
|
|
70
|
+
return error instanceof FilesClientError;
|
|
71
|
+
}
|
|
72
|
+
__name(isFilesClientError, "isFilesClientError");
|
|
73
|
+
var NOT_FOUND_UPSTREAM_CODES = /* @__PURE__ */ new Set([
|
|
74
|
+
"NOT_FOUND",
|
|
75
|
+
"FORBIDDEN"
|
|
76
|
+
]);
|
|
77
|
+
var QUOTA_UPSTREAM_CODES = /* @__PURE__ */ new Set([
|
|
78
|
+
"QUOTA_EXCEEDED",
|
|
79
|
+
"QUOTA_EXHAUSTED",
|
|
80
|
+
"USAGE_LIMIT_EXCEEDED"
|
|
81
|
+
]);
|
|
82
|
+
var INVALID_UPSTREAM_CODES = /* @__PURE__ */ new Set([
|
|
83
|
+
"BAD_USER_INPUT",
|
|
84
|
+
"EXPIRED"
|
|
85
|
+
]);
|
|
86
|
+
function upstreamCode(error) {
|
|
87
|
+
const code = error.extensions?.["code"];
|
|
88
|
+
return typeof code === "string" ? code : null;
|
|
89
|
+
}
|
|
90
|
+
__name(upstreamCode, "upstreamCode");
|
|
91
|
+
function classifyFilesErrors(errors) {
|
|
92
|
+
const codes = errors.map(upstreamCode).filter((code) => code !== null);
|
|
93
|
+
if (codes.some((code) => QUOTA_UPSTREAM_CODES.has(code))) return "QUOTA_EXCEEDED";
|
|
94
|
+
if (codes.some((code) => NOT_FOUND_UPSTREAM_CODES.has(code))) return "NOT_FOUND";
|
|
95
|
+
if (codes.some((code) => INVALID_UPSTREAM_CODES.has(code))) return "INVALID_INPUT";
|
|
96
|
+
return "UPSTREAM_ERROR";
|
|
97
|
+
}
|
|
98
|
+
__name(classifyFilesErrors, "classifyFilesErrors");
|
|
99
|
+
function toFilesClientError(error, operation) {
|
|
100
|
+
if (error instanceof FilesClientError) return error;
|
|
101
|
+
if (error instanceof GatewayError) {
|
|
102
|
+
const graphqlErrors = error.graphqlErrors ?? [];
|
|
103
|
+
const code = graphqlErrors.length > 0 ? classifyFilesErrors(graphqlErrors) : "UPSTREAM_ERROR";
|
|
104
|
+
return new FilesClientError(code, `wspace-files-svc ${operation} failed: ${error.message}`, {
|
|
105
|
+
operation,
|
|
106
|
+
gatewayErrorType: error.type,
|
|
107
|
+
statusCode: error.statusCode,
|
|
108
|
+
errors: graphqlErrors.map((graphqlError) => ({
|
|
109
|
+
message: graphqlError.message,
|
|
110
|
+
code: upstreamCode(graphqlError)
|
|
111
|
+
}))
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
115
|
+
return new FilesClientError("UPSTREAM_ERROR", `wspace-files-svc ${operation} failed: ${message}`, {
|
|
116
|
+
operation
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
__name(toFilesClientError, "toFilesClientError");
|
|
120
|
+
var MIB = 1024 * 1024;
|
|
121
|
+
var MAX_DOWNLOAD_URL_TTL_SECONDS = 604800;
|
|
122
|
+
var MIN_DOWNLOAD_URL_TTL_SECONDS = 60;
|
|
123
|
+
var DEFAULT_REQUEST_TIMEOUT_MS = 15e3;
|
|
124
|
+
var DEFAULT_TRANSFER_TIMEOUT_MS = 12e4;
|
|
125
|
+
var DEFAULT_MAX_UPLOAD_BYTES = 100 * MIB;
|
|
126
|
+
var DEFAULT_MAX_DOWNLOAD_BYTES = 25 * MIB;
|
|
127
|
+
var DEFAULT_DOWNLOAD_URL_TTL_SECONDS = 3600;
|
|
128
|
+
var INTERNAL_DOWNLOAD_URL_TTL_SECONDS = 300;
|
|
129
|
+
var PART_URL_BATCH_SIZE = 20;
|
|
130
|
+
var FILE_FIELDS = "id name mimeType extension size status visibility workspaceId folderId uploadedById description sha256Hash createdAt updatedAt canRead canDelete";
|
|
131
|
+
var INITIATE_UPLOAD = `mutation FilesClientInitiateUpload($input: InitiateUploadInput!) {
|
|
132
|
+
initiateUpload(input: $input) {
|
|
133
|
+
uploadUrl
|
|
134
|
+
session { id uploadType partSize totalParts fileSize }
|
|
135
|
+
}
|
|
136
|
+
}`;
|
|
137
|
+
var PART_URLS = `query FilesClientPartUploadUrls($sessionId: ID!, $partNumbers: [Int!]!) {
|
|
138
|
+
getBatchPartUploadUrls(sessionId: $sessionId, partNumbers: $partNumbers) { partNumber url }
|
|
139
|
+
}`;
|
|
140
|
+
var REGISTER_PART = `mutation FilesClientRegisterUploadedPart($input: UploadPartInput!) {
|
|
141
|
+
registerUploadedPart(input: $input) { session { id uploadedParts } }
|
|
142
|
+
}`;
|
|
143
|
+
var COMPLETE_UPLOAD = `mutation FilesClientCompleteUpload($input: CompleteUploadInput!) {
|
|
144
|
+
completeUpload(input: $input) { fileId file { id name mimeType size workspaceId sha256Hash } }
|
|
145
|
+
}`;
|
|
146
|
+
var ABORT_UPLOAD = `mutation FilesClientAbortUpload($sessionId: ID!) { abortUpload(sessionId: $sessionId) }`;
|
|
147
|
+
var GET_FILE = `query FilesClientGetFile($id: ID!) { getFile(id: $id) { ${FILE_FIELDS} } }`;
|
|
148
|
+
var GET_DOWNLOAD_URL = `query FilesClientGetFileDownloadUrl($id: ID!, $expiresIn: Int) {
|
|
149
|
+
getFileDownloadUrl(id: $id, expiresIn: $expiresIn)
|
|
150
|
+
}`;
|
|
151
|
+
var RESOLVE_FOR_DOWNLOAD = `query FilesClientResolveForDownload($id: ID!, $expiresIn: Int) {
|
|
152
|
+
getFile(id: $id) { ${FILE_FIELDS} }
|
|
153
|
+
getFileDownloadUrl(id: $id, expiresIn: $expiresIn)
|
|
154
|
+
}`;
|
|
155
|
+
var DELETE_FILE = `mutation FilesClientDeleteFile($id: ID!) { deleteFile(id: $id) }`;
|
|
156
|
+
var PERMANENTLY_DELETE_FILE = `mutation FilesClientPermanentlyDeleteFile($id: ID!) { permanentlyDeleteFile(id: $id) }`;
|
|
157
|
+
function requireNonEmpty(value, field) {
|
|
158
|
+
const trimmed = typeof value === "string" ? value.trim() : "";
|
|
159
|
+
if (!trimmed) {
|
|
160
|
+
throw new FilesClientError("INVALID_INPUT", `${field} is required`, {
|
|
161
|
+
field
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
return trimmed;
|
|
165
|
+
}
|
|
166
|
+
__name(requireNonEmpty, "requireNonEmpty");
|
|
167
|
+
function requirePositiveInteger(value, field) {
|
|
168
|
+
if (!Number.isSafeInteger(value) || value <= 0) {
|
|
169
|
+
throw new FilesClientError("CONFIGURATION_ERROR", `${field} must be a positive integer`, {
|
|
170
|
+
field,
|
|
171
|
+
value
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
return value;
|
|
175
|
+
}
|
|
176
|
+
__name(requirePositiveInteger, "requirePositiveInteger");
|
|
177
|
+
function toNumber(value, field) {
|
|
178
|
+
const parsed = typeof value === "number" ? value : Number(value);
|
|
179
|
+
if (!Number.isSafeInteger(parsed) || parsed < 0) {
|
|
180
|
+
throw new FilesClientError("UPSTREAM_ERROR", `wspace-files-svc returned an invalid ${field}`, {
|
|
181
|
+
field,
|
|
182
|
+
value
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
return parsed;
|
|
186
|
+
}
|
|
187
|
+
__name(toNumber, "toNumber");
|
|
188
|
+
function toDate(value) {
|
|
189
|
+
if (!value) return null;
|
|
190
|
+
const date = new Date(value);
|
|
191
|
+
return Number.isNaN(date.getTime()) ? null : date;
|
|
192
|
+
}
|
|
193
|
+
__name(toDate, "toDate");
|
|
194
|
+
function toBytes(data) {
|
|
195
|
+
if (data instanceof ArrayBuffer) return new Uint8Array(data);
|
|
196
|
+
const { buffer } = data;
|
|
197
|
+
if (buffer instanceof ArrayBuffer) return new Uint8Array(buffer, data.byteOffset, data.byteLength);
|
|
198
|
+
return new Uint8Array(data);
|
|
199
|
+
}
|
|
200
|
+
__name(toBytes, "toBytes");
|
|
201
|
+
function fileExtensionOf(fileName) {
|
|
202
|
+
const base = fileName.trim().split(/[\\/]/).pop() ?? "";
|
|
203
|
+
const dot = base.lastIndexOf(".");
|
|
204
|
+
if (dot <= 0 || dot === base.length - 1) return "bin";
|
|
205
|
+
const extension = base.slice(dot + 1).toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
206
|
+
return extension || "bin";
|
|
207
|
+
}
|
|
208
|
+
__name(fileExtensionOf, "fileExtensionOf");
|
|
209
|
+
function clampDownloadUrlTtl(seconds, fallback) {
|
|
210
|
+
const value = seconds === null || seconds === void 0 || !Number.isFinite(seconds) ? fallback : seconds;
|
|
211
|
+
return Math.min(Math.max(Math.trunc(value), MIN_DOWNLOAD_URL_TTL_SECONDS), MAX_DOWNLOAD_URL_TTL_SECONDS);
|
|
212
|
+
}
|
|
213
|
+
__name(clampDownloadUrlTtl, "clampDownloadUrlTtl");
|
|
214
|
+
function isHttpUrl(value) {
|
|
215
|
+
if (typeof value !== "string" || value.length === 0) return false;
|
|
216
|
+
try {
|
|
217
|
+
const { protocol } = new URL(value);
|
|
218
|
+
return protocol === "https:" || protocol === "http:";
|
|
219
|
+
} catch {
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
__name(isHttpUrl, "isHttpUrl");
|
|
224
|
+
function mapFileMetadata(raw) {
|
|
225
|
+
return {
|
|
226
|
+
id: raw.id,
|
|
227
|
+
name: raw.name,
|
|
228
|
+
mimeType: raw.mimeType,
|
|
229
|
+
extension: raw.extension,
|
|
230
|
+
size: toNumber(raw.size, "size"),
|
|
231
|
+
status: raw.status,
|
|
232
|
+
visibility: raw.visibility,
|
|
233
|
+
workspaceId: raw.workspaceId,
|
|
234
|
+
folderId: raw.folderId ?? null,
|
|
235
|
+
uploadedById: raw.uploadedById,
|
|
236
|
+
description: raw.description ?? null,
|
|
237
|
+
sha256Hash: raw.sha256Hash ?? null,
|
|
238
|
+
createdAt: toDate(raw.createdAt),
|
|
239
|
+
updatedAt: toDate(raw.updatedAt),
|
|
240
|
+
canRead: raw.canRead ?? null,
|
|
241
|
+
canDelete: raw.canDelete ?? null
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
__name(mapFileMetadata, "mapFileMetadata");
|
|
245
|
+
var FilesClient = class {
|
|
246
|
+
static {
|
|
247
|
+
__name(this, "FilesClient");
|
|
248
|
+
}
|
|
249
|
+
clientOption;
|
|
250
|
+
serviceName;
|
|
251
|
+
fetchImpl;
|
|
252
|
+
requestTimeoutMs;
|
|
253
|
+
transferTimeoutMs;
|
|
254
|
+
maxUploadBytes;
|
|
255
|
+
maxDownloadBytes;
|
|
256
|
+
defaultDownloadUrlTtlSeconds;
|
|
257
|
+
clientPromise = null;
|
|
258
|
+
constructor(options = {}) {
|
|
259
|
+
this.clientOption = options.client;
|
|
260
|
+
this.serviceName = options.serviceName;
|
|
261
|
+
this.fetchImpl = options.fetch ?? ((url, init) => fetch(url, init));
|
|
262
|
+
this.requestTimeoutMs = requirePositiveInteger(options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS, "requestTimeoutMs");
|
|
263
|
+
this.transferTimeoutMs = requirePositiveInteger(options.transferTimeoutMs ?? DEFAULT_TRANSFER_TIMEOUT_MS, "transferTimeoutMs");
|
|
264
|
+
this.maxUploadBytes = requirePositiveInteger(options.maxUploadBytes ?? DEFAULT_MAX_UPLOAD_BYTES, "maxUploadBytes");
|
|
265
|
+
this.maxDownloadBytes = requirePositiveInteger(options.maxDownloadBytes ?? DEFAULT_MAX_DOWNLOAD_BYTES, "maxDownloadBytes");
|
|
266
|
+
const ttl = options.defaultDownloadUrlTtlSeconds ?? DEFAULT_DOWNLOAD_URL_TTL_SECONDS;
|
|
267
|
+
if (!Number.isSafeInteger(ttl) || ttl < MIN_DOWNLOAD_URL_TTL_SECONDS || ttl > MAX_DOWNLOAD_URL_TTL_SECONDS) {
|
|
268
|
+
throw new FilesClientError("CONFIGURATION_ERROR", `defaultDownloadUrlTtlSeconds must be between ${MIN_DOWNLOAD_URL_TTL_SECONDS} and ${MAX_DOWNLOAD_URL_TTL_SECONDS}`, {
|
|
269
|
+
value: ttl
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
this.defaultDownloadUrlTtlSeconds = ttl;
|
|
273
|
+
}
|
|
274
|
+
// ── Upload ────────────────────────────────────────────────────────────
|
|
275
|
+
/**
|
|
276
|
+
* Store bytes in the actor's workspace file library and return the File's id.
|
|
277
|
+
* Throws {@link FilesClientError}; on any failure after the session was opened
|
|
278
|
+
* the session is aborted so no half-uploaded object is left behind.
|
|
279
|
+
*/
|
|
280
|
+
async uploadFile(input, actor) {
|
|
281
|
+
const context = this.requireActor(actor);
|
|
282
|
+
const fileName = requireNonEmpty(input.fileName, "fileName");
|
|
283
|
+
const mimeType = requireNonEmpty(input.mimeType, "mimeType");
|
|
284
|
+
const bytes = toBytes(input.data);
|
|
285
|
+
if (bytes.byteLength === 0) {
|
|
286
|
+
throw new FilesClientError("INVALID_INPUT", "data must not be empty", {
|
|
287
|
+
field: "data"
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
if (bytes.byteLength > this.maxUploadBytes) {
|
|
291
|
+
throw new FilesClientError("TOO_LARGE", `File is larger than ${this.maxUploadBytes} bytes`, {
|
|
292
|
+
size: bytes.byteLength,
|
|
293
|
+
maxBytes: this.maxUploadBytes
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
if (input.visibility !== void 0 && !FILE_VISIBILITIES.includes(input.visibility)) {
|
|
297
|
+
throw new FilesClientError("INVALID_INPUT", `Unknown file visibility "${String(input.visibility)}"`, {
|
|
298
|
+
field: "visibility"
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
const extension = input.extension?.trim() ? input.extension.trim().replace(/^\./, "").toLowerCase() : fileExtensionOf(fileName);
|
|
302
|
+
const fileSize = String(bytes.byteLength);
|
|
303
|
+
const sha256Hash = input.declareSha256 === false ? null : createHash("sha256").update(bytes).digest("hex");
|
|
304
|
+
const folderId = input.folderId?.trim() || void 0;
|
|
305
|
+
const initiated = await this.request("initiateUpload", INITIATE_UPLOAD, {
|
|
306
|
+
input: {
|
|
307
|
+
fileName,
|
|
308
|
+
fileSize,
|
|
309
|
+
mimeType,
|
|
310
|
+
extension,
|
|
311
|
+
...folderId ? {
|
|
312
|
+
folderId
|
|
313
|
+
} : {},
|
|
314
|
+
...input.visibility ? {
|
|
315
|
+
visibility: input.visibility
|
|
316
|
+
} : {},
|
|
317
|
+
...sha256Hash ? {
|
|
318
|
+
sha256Hash
|
|
319
|
+
} : {}
|
|
320
|
+
}
|
|
321
|
+
}, context);
|
|
322
|
+
const rawSession = initiated.initiateUpload?.session;
|
|
323
|
+
if (!rawSession?.id) {
|
|
324
|
+
throw new FilesClientError("UPSTREAM_ERROR", "wspace-files-svc initiateUpload returned no session");
|
|
325
|
+
}
|
|
326
|
+
const session = {
|
|
327
|
+
id: rawSession.id,
|
|
328
|
+
uploadType: rawSession.uploadType,
|
|
329
|
+
partSize: toNumber(rawSession.partSize, "partSize"),
|
|
330
|
+
totalParts: toNumber(rawSession.totalParts, "totalParts")
|
|
331
|
+
};
|
|
332
|
+
try {
|
|
333
|
+
if (session.uploadType === "MULTIPART") {
|
|
334
|
+
await this.uploadParts(session, bytes, context);
|
|
335
|
+
} else {
|
|
336
|
+
await this.putDirect(initiated.initiateUpload?.uploadUrl, bytes, mimeType);
|
|
337
|
+
}
|
|
338
|
+
const completed = await this.request("completeUpload", COMPLETE_UPLOAD, {
|
|
339
|
+
input: {
|
|
340
|
+
sessionId: session.id,
|
|
341
|
+
fileSize,
|
|
342
|
+
...folderId ? {
|
|
343
|
+
folderId
|
|
344
|
+
} : {},
|
|
345
|
+
...input.description ? {
|
|
346
|
+
description: input.description
|
|
347
|
+
} : {},
|
|
348
|
+
...sha256Hash ? {
|
|
349
|
+
sha256Hash
|
|
350
|
+
} : {}
|
|
351
|
+
}
|
|
352
|
+
}, context);
|
|
353
|
+
const file = completed.completeUpload?.file;
|
|
354
|
+
const fileId = completed.completeUpload?.fileId ?? file?.id;
|
|
355
|
+
if (!fileId) {
|
|
356
|
+
throw new FilesClientError("UPSTREAM_ERROR", "wspace-files-svc completeUpload returned no fileId", {
|
|
357
|
+
sessionId: session.id
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
return {
|
|
361
|
+
fileId,
|
|
362
|
+
name: file?.name ?? fileName,
|
|
363
|
+
mimeType: file?.mimeType ?? mimeType,
|
|
364
|
+
size: file ? toNumber(file.size, "size") : bytes.byteLength,
|
|
365
|
+
workspaceId: file?.workspaceId ?? context.workspaceId,
|
|
366
|
+
sha256Hash: file?.sha256Hash ?? sha256Hash
|
|
367
|
+
};
|
|
368
|
+
} catch (error) {
|
|
369
|
+
await this.abortQuietly(session.id, context);
|
|
370
|
+
throw toFilesClientError(error, "uploadFile");
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
// ── Read ──────────────────────────────────────────────────────────────
|
|
374
|
+
/** File metadata, or `null` when it does not exist or the actor cannot see it. */
|
|
375
|
+
async getFile(fileId, actor) {
|
|
376
|
+
const id = requireNonEmpty(fileId, "fileId");
|
|
377
|
+
try {
|
|
378
|
+
const data = await this.request("getFile", GET_FILE, {
|
|
379
|
+
id
|
|
380
|
+
}, actor);
|
|
381
|
+
return data.getFile ? mapFileMetadata(data.getFile) : null;
|
|
382
|
+
} catch (error) {
|
|
383
|
+
if (error instanceof FilesClientError && error.code === "NOT_FOUND") return null;
|
|
384
|
+
throw error;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Prove that an opaque file id the caller supplied names a File in the actor's
|
|
389
|
+
* workspace that they may read, before the service stores or acts on it.
|
|
390
|
+
* Throws `NOT_FOUND` (never a permission error) otherwise.
|
|
391
|
+
*/
|
|
392
|
+
async requireFile(fileId, actor, options = {}) {
|
|
393
|
+
const context = this.requireActor(actor);
|
|
394
|
+
const file = await this.getFile(fileId, context);
|
|
395
|
+
this.assertUsable(file, fileId, context, options);
|
|
396
|
+
return file;
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* A presigned download URL for the file, minted for the actor. Mint it when the
|
|
400
|
+
* user opens the file; never persist it. The lifetime is clamped to
|
|
401
|
+
* [60, 604800] seconds.
|
|
402
|
+
*/
|
|
403
|
+
async getDownloadUrl(fileId, actor, options = {}) {
|
|
404
|
+
const id = requireNonEmpty(fileId, "fileId");
|
|
405
|
+
const expiresIn = clampDownloadUrlTtl(options.expiresInSeconds, this.defaultDownloadUrlTtlSeconds);
|
|
406
|
+
const data = await this.request("getFileDownloadUrl", GET_DOWNLOAD_URL, {
|
|
407
|
+
id,
|
|
408
|
+
expiresIn
|
|
409
|
+
}, actor);
|
|
410
|
+
if (!isHttpUrl(data.getFileDownloadUrl)) {
|
|
411
|
+
throw new FilesClientError("UPSTREAM_ERROR", "wspace-files-svc returned no usable download URL", {
|
|
412
|
+
fileId: id
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
return data.getFileDownloadUrl;
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Fetch the file's bytes for server-side processing (vision models, parsers,
|
|
419
|
+
* workflow nodes). Checks the file is active, readable by the actor and within
|
|
420
|
+
* `maxBytes` before downloading, and verifies the downloaded size.
|
|
421
|
+
*/
|
|
422
|
+
async downloadFile(fileId, actor, options = {}) {
|
|
423
|
+
const context = this.requireActor(actor);
|
|
424
|
+
const id = requireNonEmpty(fileId, "fileId");
|
|
425
|
+
const maxBytes = options.maxBytes ?? this.maxDownloadBytes;
|
|
426
|
+
if (!Number.isSafeInteger(maxBytes) || maxBytes <= 0) {
|
|
427
|
+
throw new FilesClientError("INVALID_INPUT", "maxBytes must be a positive integer", {
|
|
428
|
+
field: "maxBytes"
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
const data = await this.request("downloadFile", RESOLVE_FOR_DOWNLOAD, {
|
|
432
|
+
id,
|
|
433
|
+
expiresIn: INTERNAL_DOWNLOAD_URL_TTL_SECONDS
|
|
434
|
+
}, context);
|
|
435
|
+
const file = data.getFile ? mapFileMetadata(data.getFile) : null;
|
|
436
|
+
this.assertUsable(file, id, context, {});
|
|
437
|
+
if (file.size > maxBytes) {
|
|
438
|
+
throw new FilesClientError("TOO_LARGE", `File is larger than ${maxBytes} bytes`, {
|
|
439
|
+
fileId: id,
|
|
440
|
+
size: file.size,
|
|
441
|
+
maxBytes
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
const url = data.getFileDownloadUrl;
|
|
445
|
+
if (!isHttpUrl(url)) {
|
|
446
|
+
throw new FilesClientError("UPSTREAM_ERROR", "wspace-files-svc returned no usable download URL", {
|
|
447
|
+
fileId: id
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
const response = await this.transfer(url, {
|
|
451
|
+
method: "GET"
|
|
452
|
+
}, "download");
|
|
453
|
+
const bytes = await this.readBounded(response, maxBytes, id);
|
|
454
|
+
if (bytes.byteLength !== file.size) {
|
|
455
|
+
throw new FilesClientError("STORAGE_ERROR", "Downloaded size does not match the stored file size", {
|
|
456
|
+
fileId: id,
|
|
457
|
+
expected: file.size,
|
|
458
|
+
actual: bytes.byteLength
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
return {
|
|
462
|
+
bytes,
|
|
463
|
+
mimeType: file.mimeType,
|
|
464
|
+
name: file.name,
|
|
465
|
+
size: bytes.byteLength
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
/** {@link downloadFile} as base64, for payloads that must stay JSON (model inputs). */
|
|
469
|
+
async downloadFileAsBase64(fileId, actor, options = {}) {
|
|
470
|
+
const file = await this.downloadFile(fileId, actor, options);
|
|
471
|
+
return {
|
|
472
|
+
base64: Buffer.from(file.bytes).toString("base64"),
|
|
473
|
+
mimeType: file.mimeType,
|
|
474
|
+
name: file.name,
|
|
475
|
+
size: file.size
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
// ── Delete ────────────────────────────────────────────────────────────
|
|
479
|
+
/** Move the file to trash (or hard-delete with `permanent`). `true` when files-svc deleted it. */
|
|
480
|
+
async deleteFile(fileId, actor, options = {}) {
|
|
481
|
+
const id = requireNonEmpty(fileId, "fileId");
|
|
482
|
+
if (options.permanent) {
|
|
483
|
+
const data2 = await this.request("permanentlyDeleteFile", PERMANENTLY_DELETE_FILE, {
|
|
484
|
+
id
|
|
485
|
+
}, actor);
|
|
486
|
+
return data2.permanentlyDeleteFile === true;
|
|
487
|
+
}
|
|
488
|
+
const data = await this.request("deleteFile", DELETE_FILE, {
|
|
489
|
+
id
|
|
490
|
+
}, actor);
|
|
491
|
+
return data.deleteFile === true;
|
|
492
|
+
}
|
|
493
|
+
// ── Upload legs ───────────────────────────────────────────────────────
|
|
494
|
+
async putDirect(uploadUrl, bytes, mimeType) {
|
|
495
|
+
if (!isHttpUrl(uploadUrl)) {
|
|
496
|
+
throw new FilesClientError("UPSTREAM_ERROR", "wspace-files-svc initiateUpload returned no upload URL");
|
|
497
|
+
}
|
|
498
|
+
await this.transfer(uploadUrl, {
|
|
499
|
+
method: "PUT",
|
|
500
|
+
body: bytes,
|
|
501
|
+
headers: {
|
|
502
|
+
"Content-Type": mimeType
|
|
503
|
+
}
|
|
504
|
+
}, "upload");
|
|
505
|
+
}
|
|
506
|
+
async uploadParts(session, bytes, actor) {
|
|
507
|
+
if (session.partSize <= 0 || session.totalParts !== Math.ceil(bytes.byteLength / session.partSize)) {
|
|
508
|
+
throw new FilesClientError("UPSTREAM_ERROR", "wspace-files-svc returned an inconsistent multipart plan", {
|
|
509
|
+
sessionId: session.id,
|
|
510
|
+
partSize: session.partSize,
|
|
511
|
+
totalParts: session.totalParts,
|
|
512
|
+
size: bytes.byteLength
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
for (let first = 1; first <= session.totalParts; first += PART_URL_BATCH_SIZE) {
|
|
516
|
+
const partNumbers = [];
|
|
517
|
+
for (let part = first; part < first + PART_URL_BATCH_SIZE && part <= session.totalParts; part += 1) {
|
|
518
|
+
partNumbers.push(part);
|
|
519
|
+
}
|
|
520
|
+
const data = await this.request("getBatchPartUploadUrls", PART_URLS, {
|
|
521
|
+
sessionId: session.id,
|
|
522
|
+
partNumbers
|
|
523
|
+
}, actor);
|
|
524
|
+
const urls = new Map((data.getBatchPartUploadUrls ?? []).map((entry) => [
|
|
525
|
+
entry.partNumber,
|
|
526
|
+
entry.url
|
|
527
|
+
]));
|
|
528
|
+
for (const partNumber of partNumbers) {
|
|
529
|
+
const url = urls.get(partNumber);
|
|
530
|
+
if (!isHttpUrl(url)) {
|
|
531
|
+
throw new FilesClientError("UPSTREAM_ERROR", `wspace-files-svc returned no URL for part ${partNumber}`, {
|
|
532
|
+
sessionId: session.id,
|
|
533
|
+
partNumber
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
const start = (partNumber - 1) * session.partSize;
|
|
537
|
+
const body = bytes.subarray(start, Math.min(start + session.partSize, bytes.byteLength));
|
|
538
|
+
const response = await this.transfer(url, {
|
|
539
|
+
method: "PUT",
|
|
540
|
+
body
|
|
541
|
+
}, "upload");
|
|
542
|
+
const etag = response.headers.get("etag");
|
|
543
|
+
if (!etag) {
|
|
544
|
+
throw new FilesClientError("STORAGE_ERROR", `Object storage returned no ETag for part ${partNumber}`, {
|
|
545
|
+
sessionId: session.id,
|
|
546
|
+
partNumber
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
await this.request("registerUploadedPart", REGISTER_PART, {
|
|
550
|
+
input: {
|
|
551
|
+
sessionId: session.id,
|
|
552
|
+
partNumber,
|
|
553
|
+
etag
|
|
554
|
+
}
|
|
555
|
+
}, actor);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
async abortQuietly(sessionId, actor) {
|
|
560
|
+
try {
|
|
561
|
+
await this.request("abortUpload", ABORT_UPLOAD, {
|
|
562
|
+
sessionId
|
|
563
|
+
}, actor);
|
|
564
|
+
} catch {
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
// ── Transport ─────────────────────────────────────────────────────────
|
|
568
|
+
assertUsable(file, fileId, actor, options) {
|
|
569
|
+
const requireActive = options.requireActive ?? true;
|
|
570
|
+
const requireReadable = options.requireReadable ?? true;
|
|
571
|
+
if (!file || file.id !== fileId.trim() || file.workspaceId !== actor.workspaceId || requireActive && file.status !== "ACTIVE" || requireReadable && file.canRead !== true) {
|
|
572
|
+
throw new FilesClientError("NOT_FOUND", "File not found", {
|
|
573
|
+
fileId
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
async transfer(url, init, leg) {
|
|
578
|
+
let response;
|
|
579
|
+
try {
|
|
580
|
+
response = await this.fetchImpl(url, {
|
|
581
|
+
...init,
|
|
582
|
+
signal: AbortSignal.timeout(this.transferTimeoutMs)
|
|
583
|
+
});
|
|
584
|
+
} catch (error) {
|
|
585
|
+
throw new FilesClientError("STORAGE_ERROR", `Object storage ${leg} failed`, {
|
|
586
|
+
leg,
|
|
587
|
+
cause: error instanceof Error ? error.message : String(error)
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
if (!response.ok) {
|
|
591
|
+
throw new FilesClientError("STORAGE_ERROR", `Object storage ${leg} failed with HTTP ${response.status}`, {
|
|
592
|
+
leg,
|
|
593
|
+
status: response.status
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
return response;
|
|
597
|
+
}
|
|
598
|
+
async readBounded(response, maxBytes, fileId) {
|
|
599
|
+
const tooLarge = /* @__PURE__ */ __name(() => new FilesClientError("TOO_LARGE", `File is larger than ${maxBytes} bytes`, {
|
|
600
|
+
fileId,
|
|
601
|
+
maxBytes
|
|
602
|
+
}), "tooLarge");
|
|
603
|
+
const declared = Number(response.headers.get("content-length") ?? Number.NaN);
|
|
604
|
+
if (Number.isFinite(declared) && declared > maxBytes) throw tooLarge();
|
|
605
|
+
if (!response.body) {
|
|
606
|
+
const buffer = new Uint8Array(await response.arrayBuffer());
|
|
607
|
+
if (buffer.byteLength > maxBytes) throw tooLarge();
|
|
608
|
+
return buffer;
|
|
609
|
+
}
|
|
610
|
+
const reader = response.body.getReader();
|
|
611
|
+
const chunks = [];
|
|
612
|
+
let total = 0;
|
|
613
|
+
for (; ; ) {
|
|
614
|
+
const { done, value } = await reader.read();
|
|
615
|
+
if (done) break;
|
|
616
|
+
total += value.byteLength;
|
|
617
|
+
if (total > maxBytes) {
|
|
618
|
+
await reader.cancel();
|
|
619
|
+
throw tooLarge();
|
|
620
|
+
}
|
|
621
|
+
chunks.push(value);
|
|
622
|
+
}
|
|
623
|
+
const bytes = new Uint8Array(total);
|
|
624
|
+
let offset = 0;
|
|
625
|
+
for (const chunk of chunks) {
|
|
626
|
+
bytes.set(chunk, offset);
|
|
627
|
+
offset += chunk.byteLength;
|
|
628
|
+
}
|
|
629
|
+
return bytes;
|
|
630
|
+
}
|
|
631
|
+
requireActor(actor) {
|
|
632
|
+
if (!actor.actorId?.trim() || !actor.workspaceId?.trim()) {
|
|
633
|
+
throw new FilesClientError("MISSING_CONTEXT", "File operations need an authenticated actor and a workspace");
|
|
634
|
+
}
|
|
635
|
+
if (actor.actorType !== void 0 && !FILE_ACTOR_TYPES.includes(actor.actorType)) {
|
|
636
|
+
throw new FilesClientError("MISSING_CONTEXT", `Unknown actor type "${String(actor.actorType)}"`);
|
|
637
|
+
}
|
|
638
|
+
return actor;
|
|
639
|
+
}
|
|
640
|
+
getClient() {
|
|
641
|
+
if (!this.clientPromise) {
|
|
642
|
+
const option = this.clientOption;
|
|
643
|
+
this.clientPromise = (async () => {
|
|
644
|
+
try {
|
|
645
|
+
if (typeof option === "function") return await option();
|
|
646
|
+
if (option) return option;
|
|
647
|
+
const gateway = await import('./client-WSFUJHLI.js');
|
|
648
|
+
if (this.serviceName) {
|
|
649
|
+
return new gateway.GatewayClient({
|
|
650
|
+
layer: "wspace",
|
|
651
|
+
serviceName: this.serviceName,
|
|
652
|
+
serviceIdentity: this.serviceName
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
return gateway.createGatewayClientFromEnv();
|
|
656
|
+
} catch (error) {
|
|
657
|
+
throw new FilesClientError("CONFIGURATION_ERROR", "Files gateway client is not configured", {
|
|
658
|
+
cause: error instanceof Error ? error.message : String(error)
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
})();
|
|
662
|
+
this.clientPromise.catch(() => {
|
|
663
|
+
this.clientPromise = null;
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
return this.clientPromise;
|
|
667
|
+
}
|
|
668
|
+
async request(operation, query, variables, actor) {
|
|
669
|
+
const { actorId, workspaceId, actorType } = this.requireActor(actor);
|
|
670
|
+
const client = await this.getClient();
|
|
671
|
+
let response;
|
|
672
|
+
try {
|
|
673
|
+
response = await client.internalQuery({
|
|
674
|
+
query,
|
|
675
|
+
variables,
|
|
676
|
+
timeout: this.requestTimeoutMs,
|
|
677
|
+
headers: {
|
|
678
|
+
"x-actor-id": actorId,
|
|
679
|
+
"x-actor-type": actorType ?? "user",
|
|
680
|
+
"x-workspace-id": workspaceId
|
|
681
|
+
}
|
|
682
|
+
});
|
|
683
|
+
} catch (error) {
|
|
684
|
+
throw toFilesClientError(error, operation);
|
|
685
|
+
}
|
|
686
|
+
if (response.errors?.length) {
|
|
687
|
+
throw new FilesClientError(classifyFilesErrors(response.errors), `wspace-files-svc ${operation} failed`, {
|
|
688
|
+
operation,
|
|
689
|
+
errors: response.errors.map((error) => ({
|
|
690
|
+
message: error.message,
|
|
691
|
+
code: error.extensions?.["code"] ?? null
|
|
692
|
+
}))
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
if (!response.data) {
|
|
696
|
+
throw new FilesClientError("UPSTREAM_ERROR", `wspace-files-svc ${operation} returned no data`, {
|
|
697
|
+
operation
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
return response.data;
|
|
701
|
+
}
|
|
702
|
+
};
|
|
703
|
+
|
|
704
|
+
// src/files/reference.ts
|
|
705
|
+
var FILE_REFERENCE_KIND = "burdenoff.file";
|
|
706
|
+
function toFileReference(file) {
|
|
707
|
+
return {
|
|
708
|
+
kind: FILE_REFERENCE_KIND,
|
|
709
|
+
fileId: "fileId" in file ? file.fileId : file.id,
|
|
710
|
+
name: file.name,
|
|
711
|
+
mimeType: file.mimeType,
|
|
712
|
+
size: file.size,
|
|
713
|
+
sha256Hash: file.sha256Hash
|
|
714
|
+
};
|
|
715
|
+
}
|
|
716
|
+
__name(toFileReference, "toFileReference");
|
|
717
|
+
function isRecord(value) {
|
|
718
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
719
|
+
}
|
|
720
|
+
__name(isRecord, "isRecord");
|
|
721
|
+
function isFileReference(value) {
|
|
722
|
+
if (!isRecord(value)) return false;
|
|
723
|
+
const size = value["size"];
|
|
724
|
+
const sha256Hash = value["sha256Hash"];
|
|
725
|
+
return value["kind"] === FILE_REFERENCE_KIND && typeof value["fileId"] === "string" && value["fileId"].length > 0 && typeof value["name"] === "string" && typeof value["mimeType"] === "string" && typeof size === "number" && Number.isSafeInteger(size) && size >= 0 && (sha256Hash === null || sha256Hash === void 0 || typeof sha256Hash === "string");
|
|
726
|
+
}
|
|
727
|
+
__name(isFileReference, "isFileReference");
|
|
728
|
+
|
|
729
|
+
export { FILES_CLIENT_GRAPHQL_CODES, FILE_ACTOR_TYPES, FILE_REFERENCE_KIND, FILE_VISIBILITIES, FilesClient, FilesClientError, MAX_DOWNLOAD_URL_TTL_SECONDS, MIN_DOWNLOAD_URL_TTL_SECONDS, clampDownloadUrlTtl, classifyFilesErrors, fileExtensionOf, files_exports, isFileReference, isFilesClientError, mapFileMetadata, toFileReference, toFilesClientError };
|
|
730
|
+
//# sourceMappingURL=chunk-AE4PRE4N.js.map
|
|
731
|
+
//# sourceMappingURL=chunk-AE4PRE4N.js.map
|