@tinycloud/sdk-services 2.2.0-beta.10 → 2.2.0-beta.12
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/dist/index.cjs +116 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +115 -0
- package/dist/index.js.map +1 -1
- package/dist/kv/index.cjs +116 -0
- package/dist/kv/index.cjs.map +1 -1
- package/dist/kv/index.d.cts +99 -1
- package/dist/kv/index.d.ts +99 -1
- package/dist/kv/index.js +115 -0
- package/dist/kv/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.cts
CHANGED
|
@@ -2,7 +2,7 @@ import { I as IServiceContext, a as InvokeFunction, b as InvokeAnyFunction, F as
|
|
|
2
2
|
export { E as ErrorCode, g as ErrorCodes, h as EventHandler, i as FetchRequestInit, j as FetchResponse, k as InvocationFact, l as InvocationFacts, m as InvokeAnyEntry, n as ServiceErrorEvent, o as ServiceHeaders, p as ServiceRequestEvent, q as ServiceResponseEvent, r as ServiceRetryEvent, T as TelemetryEvents, s as defaultRetryPolicy, t as err, u as ok, v as serviceError } from './BaseService-BiS6HRwE.cjs';
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
import { IKVService } from './kv/index.cjs';
|
|
5
|
-
export { IPrefixedKVService, KVAction, KVActionType, KVDeleteOptions, KVGetOptions, KVHeadOptions, KVListOptions, KVListResponse, KVPutOptions, KVResponse, KVResponseHeaders, KVService, KVServiceConfig, PrefixedKVService } from './kv/index.cjs';
|
|
5
|
+
export { DEFAULT_SIGNED_READ_URL_EXPIRY_MS, IPrefixedKVService, KVAction, KVActionType, KVCreateSignedReadUrlOptions, KVDeleteOptions, KVGetOptions, KVHeadOptions, KVListOptions, KVListResponse, KVPutOptions, KVResponse, KVResponseHeaders, KVService, KVServiceConfig, KVSignedReadUrlResponse, PrefixedKVService } from './kv/index.cjs';
|
|
6
6
|
export { BatchOptions, BatchResponse, DatabaseHandle, ExecuteOptions, ExecuteResponse, IDatabaseHandle, ISQLService, QueryOptions, QueryResponse, SQLAction, SQLActionType, SQLService, SQLServiceConfig, SqlStatement, SqlValue } from './sql/index.cjs';
|
|
7
7
|
|
|
8
8
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { I as IServiceContext, a as InvokeFunction, b as InvokeAnyFunction, F as
|
|
|
2
2
|
export { E as ErrorCode, g as ErrorCodes, h as EventHandler, i as FetchRequestInit, j as FetchResponse, k as InvocationFact, l as InvocationFacts, m as InvokeAnyEntry, n as ServiceErrorEvent, o as ServiceHeaders, p as ServiceRequestEvent, q as ServiceResponseEvent, r as ServiceRetryEvent, T as TelemetryEvents, s as defaultRetryPolicy, t as err, u as ok, v as serviceError } from './BaseService-BiS6HRwE.js';
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
import { IKVService } from './kv/index.js';
|
|
5
|
-
export { IPrefixedKVService, KVAction, KVActionType, KVDeleteOptions, KVGetOptions, KVHeadOptions, KVListOptions, KVListResponse, KVPutOptions, KVResponse, KVResponseHeaders, KVService, KVServiceConfig, PrefixedKVService } from './kv/index.js';
|
|
5
|
+
export { DEFAULT_SIGNED_READ_URL_EXPIRY_MS, IPrefixedKVService, KVAction, KVActionType, KVCreateSignedReadUrlOptions, KVDeleteOptions, KVGetOptions, KVHeadOptions, KVListOptions, KVListResponse, KVPutOptions, KVResponse, KVResponseHeaders, KVService, KVServiceConfig, KVSignedReadUrlResponse, PrefixedKVService } from './kv/index.js';
|
|
6
6
|
export { BatchOptions, BatchResponse, DatabaseHandle, ExecuteOptions, ExecuteResponse, IDatabaseHandle, ISQLService, QueryOptions, QueryResponse, SQLAction, SQLActionType, SQLService, SQLServiceConfig, SqlStatement, SqlValue } from './sql/index.js';
|
|
7
7
|
|
|
8
8
|
/**
|
package/dist/index.js
CHANGED
|
@@ -818,6 +818,13 @@ var PrefixedKVService = class _PrefixedKVService {
|
|
|
818
818
|
const fullKey = this.getFullKey(key);
|
|
819
819
|
return this._kv.head(fullKey, { ...options, prefix: "" });
|
|
820
820
|
}
|
|
821
|
+
/**
|
|
822
|
+
* Create a short-lived signed URL for reading a KV object.
|
|
823
|
+
*/
|
|
824
|
+
async createSignedReadUrl(key, options) {
|
|
825
|
+
const fullKey = this.getFullKey(key);
|
|
826
|
+
return this._kv.createSignedReadUrl(fullKey, { ...options, prefix: "" });
|
|
827
|
+
}
|
|
821
828
|
/**
|
|
822
829
|
* Create a nested prefix-scoped view.
|
|
823
830
|
*/
|
|
@@ -829,6 +836,7 @@ var PrefixedKVService = class _PrefixedKVService {
|
|
|
829
836
|
};
|
|
830
837
|
|
|
831
838
|
// src/kv/types.ts
|
|
839
|
+
var DEFAULT_SIGNED_READ_URL_EXPIRY_MS = 5 * 60 * 1e3;
|
|
832
840
|
var KVAction = {
|
|
833
841
|
GET: "tinycloud.kv/get",
|
|
834
842
|
PUT: "tinycloud.kv/put",
|
|
@@ -913,6 +921,15 @@ var KVService = class extends BaseService {
|
|
|
913
921
|
get host() {
|
|
914
922
|
return this.context.hosts[0];
|
|
915
923
|
}
|
|
924
|
+
withJsonContentType(headers) {
|
|
925
|
+
if (Array.isArray(headers)) {
|
|
926
|
+
return [...headers, ["content-type", "application/json"]];
|
|
927
|
+
}
|
|
928
|
+
return {
|
|
929
|
+
...headers,
|
|
930
|
+
"content-type": "application/json"
|
|
931
|
+
};
|
|
932
|
+
}
|
|
916
933
|
/**
|
|
917
934
|
* Execute an invoke operation.
|
|
918
935
|
*
|
|
@@ -982,6 +999,48 @@ var KVService = class extends BaseService {
|
|
|
982
999
|
return text;
|
|
983
1000
|
}
|
|
984
1001
|
}
|
|
1002
|
+
async createSignedReadUrlError(response, key) {
|
|
1003
|
+
let errorText = response.statusText;
|
|
1004
|
+
try {
|
|
1005
|
+
const text = await response.text();
|
|
1006
|
+
if (text) {
|
|
1007
|
+
errorText = text;
|
|
1008
|
+
}
|
|
1009
|
+
} catch {
|
|
1010
|
+
}
|
|
1011
|
+
if (response.status === 401 || response.status === 403) {
|
|
1012
|
+
const { resource, action } = parseAuthError(errorText);
|
|
1013
|
+
return err(authUnauthorizedError("kv", errorText, {
|
|
1014
|
+
status: response.status,
|
|
1015
|
+
...action && { requiredAction: action },
|
|
1016
|
+
...resource && { resource }
|
|
1017
|
+
}));
|
|
1018
|
+
}
|
|
1019
|
+
const code = response.status === 400 ? ErrorCodes.INVALID_INPUT : ErrorCodes.NETWORK_ERROR;
|
|
1020
|
+
return err(
|
|
1021
|
+
serviceError(
|
|
1022
|
+
code,
|
|
1023
|
+
`Failed to create signed read URL for key "${key}": ${response.status} - ${errorText}`,
|
|
1024
|
+
"kv",
|
|
1025
|
+
{ meta: { status: response.status, statusText: response.statusText } }
|
|
1026
|
+
)
|
|
1027
|
+
);
|
|
1028
|
+
}
|
|
1029
|
+
normalizeSignedReadUrlResponse(data) {
|
|
1030
|
+
if (!data || typeof data !== "object") {
|
|
1031
|
+
return void 0;
|
|
1032
|
+
}
|
|
1033
|
+
const response = data;
|
|
1034
|
+
if (typeof response.url !== "string" || typeof response.ticketId !== "string" || typeof response.expiresAt !== "string") {
|
|
1035
|
+
return void 0;
|
|
1036
|
+
}
|
|
1037
|
+
return {
|
|
1038
|
+
url: new URL(response.url, this.host).toString(),
|
|
1039
|
+
relativeUrl: response.url,
|
|
1040
|
+
ticketId: response.ticketId,
|
|
1041
|
+
expiresAt: response.expiresAt
|
|
1042
|
+
};
|
|
1043
|
+
}
|
|
985
1044
|
/**
|
|
986
1045
|
* Get a value by key.
|
|
987
1046
|
*/
|
|
@@ -1254,6 +1313,61 @@ var KVService = class extends BaseService {
|
|
|
1254
1313
|
}
|
|
1255
1314
|
});
|
|
1256
1315
|
}
|
|
1316
|
+
/**
|
|
1317
|
+
* Create a short-lived signed URL for reading a KV object.
|
|
1318
|
+
*/
|
|
1319
|
+
async createSignedReadUrl(key, options) {
|
|
1320
|
+
return this.withTelemetry("createSignedReadUrl", key, async () => {
|
|
1321
|
+
if (!this.requireAuth()) {
|
|
1322
|
+
return err(authRequiredError("kv"));
|
|
1323
|
+
}
|
|
1324
|
+
const path = this.getFullPath(key, options?.prefix);
|
|
1325
|
+
const session = this.context.session;
|
|
1326
|
+
const headers = this.context.invoke(
|
|
1327
|
+
session,
|
|
1328
|
+
"kv",
|
|
1329
|
+
path,
|
|
1330
|
+
KVAction.GET
|
|
1331
|
+
);
|
|
1332
|
+
const body = {
|
|
1333
|
+
space: session.spaceId,
|
|
1334
|
+
path,
|
|
1335
|
+
ttl_seconds: options?.expiresInSeconds ?? Math.ceil(DEFAULT_SIGNED_READ_URL_EXPIRY_MS / 1e3)
|
|
1336
|
+
};
|
|
1337
|
+
if (options?.contentHash !== void 0) {
|
|
1338
|
+
body.content_hash = options.contentHash;
|
|
1339
|
+
}
|
|
1340
|
+
if (options?.etag !== void 0) {
|
|
1341
|
+
body.etag = options.etag;
|
|
1342
|
+
}
|
|
1343
|
+
try {
|
|
1344
|
+
const response = await this.context.fetch(`${this.host}/signed/kv`, {
|
|
1345
|
+
method: "POST",
|
|
1346
|
+
headers: this.withJsonContentType(headers),
|
|
1347
|
+
body: JSON.stringify(body),
|
|
1348
|
+
signal: this.combineSignals(options?.signal)
|
|
1349
|
+
});
|
|
1350
|
+
if (!response.ok) {
|
|
1351
|
+
return this.createSignedReadUrlError(response, key);
|
|
1352
|
+
}
|
|
1353
|
+
const signedUrl = this.normalizeSignedReadUrlResponse(
|
|
1354
|
+
await response.json()
|
|
1355
|
+
);
|
|
1356
|
+
if (!signedUrl) {
|
|
1357
|
+
return err(
|
|
1358
|
+
serviceError(
|
|
1359
|
+
ErrorCodes.NETWORK_ERROR,
|
|
1360
|
+
"Signed read URL response did not include url, ticketId, and expiresAt",
|
|
1361
|
+
"kv"
|
|
1362
|
+
)
|
|
1363
|
+
);
|
|
1364
|
+
}
|
|
1365
|
+
return ok(signedUrl);
|
|
1366
|
+
} catch (error) {
|
|
1367
|
+
return err(wrapError("kv", error));
|
|
1368
|
+
}
|
|
1369
|
+
});
|
|
1370
|
+
}
|
|
1257
1371
|
/**
|
|
1258
1372
|
* Create a prefix-scoped view of this KV service.
|
|
1259
1373
|
*
|
|
@@ -3885,6 +3999,7 @@ var SecretsService = class {
|
|
|
3885
3999
|
};
|
|
3886
4000
|
export {
|
|
3887
4001
|
BaseService,
|
|
4002
|
+
DEFAULT_SIGNED_READ_URL_EXPIRY_MS,
|
|
3888
4003
|
DataVaultService,
|
|
3889
4004
|
DatabaseHandle,
|
|
3890
4005
|
DuckDbAction,
|