@beam-network/sdk 0.2.4 → 0.2.5
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/client.js +11 -0
- package/dist/provider-signing.d.ts +1 -0
- package/dist/provider-signing.js +38 -1
- package/package.json +1 -1
package/dist/client.js
CHANGED
|
@@ -92,6 +92,10 @@ export class BeamClient {
|
|
|
92
92
|
preparedDestinations.forEach((preparedDestination, index) => {
|
|
93
93
|
destinationsById.set(preparedDestination.destination_id, input.destinations[index]);
|
|
94
94
|
});
|
|
95
|
+
const sourcesById = new Map();
|
|
96
|
+
preparedSources.forEach((preparedSource, index) => {
|
|
97
|
+
sourcesById.set(preparedSource.source_id, input.sources[index]);
|
|
98
|
+
});
|
|
95
99
|
const routes = [];
|
|
96
100
|
const multipartUploads = new Map();
|
|
97
101
|
try {
|
|
@@ -101,6 +105,10 @@ export class BeamClient {
|
|
|
101
105
|
if (!destination) {
|
|
102
106
|
throw new Error(`BeamCore returned unknown destination_id: ${target.destination_id}`);
|
|
103
107
|
}
|
|
108
|
+
const source = sourcesById.get(chunk.source_id);
|
|
109
|
+
if (!source) {
|
|
110
|
+
throw new Error(`BeamCore returned unknown source_id: ${chunk.source_id}`);
|
|
111
|
+
}
|
|
104
112
|
const metadata = target.metadata ?? {};
|
|
105
113
|
const finalObjectKey = typeof metadata.final_object_key === "string"
|
|
106
114
|
? metadata.final_object_key
|
|
@@ -142,6 +150,7 @@ export class BeamClient {
|
|
|
142
150
|
routes.push(await this.signProviderRoute({
|
|
143
151
|
chunk,
|
|
144
152
|
target,
|
|
153
|
+
source,
|
|
145
154
|
destination,
|
|
146
155
|
expiresIn,
|
|
147
156
|
upload,
|
|
@@ -181,6 +190,7 @@ export class BeamClient {
|
|
|
181
190
|
return signDestinationRoute({
|
|
182
191
|
chunk: input.chunk,
|
|
183
192
|
target: input.target,
|
|
193
|
+
source: input.source,
|
|
184
194
|
destination: input.destination,
|
|
185
195
|
expiresIn: input.expiresIn,
|
|
186
196
|
transferId: input.transferId,
|
|
@@ -206,6 +216,7 @@ export class BeamClient {
|
|
|
206
216
|
return signDestinationRoute({
|
|
207
217
|
chunk: input.chunk,
|
|
208
218
|
target: input.target,
|
|
219
|
+
source: input.source,
|
|
209
220
|
destination: input.destination,
|
|
210
221
|
expiresIn: input.expiresIn,
|
|
211
222
|
uploadId: input.upload.uploadId,
|
|
@@ -23,6 +23,7 @@ export declare function abortMultipartUpload(destination: ProviderDestinationCon
|
|
|
23
23
|
export declare function signDestinationRoute(input: {
|
|
24
24
|
chunk: ChunkSigningPlanItem;
|
|
25
25
|
target: ChunkDestinationSigningTarget;
|
|
26
|
+
source?: ProviderSourceConfig;
|
|
26
27
|
destination: ProviderDestinationConfig;
|
|
27
28
|
expiresIn: number;
|
|
28
29
|
uploadId?: string;
|
package/dist/provider-signing.js
CHANGED
|
@@ -154,15 +154,23 @@ export async function signDestinationRoute(input) {
|
|
|
154
154
|
useStagingPut: input.commitMethod === "upload_part_copy",
|
|
155
155
|
fetchImpl: input.fetchImpl
|
|
156
156
|
});
|
|
157
|
+
const sourceRoute = await signSourceRoute({
|
|
158
|
+
source: input.source,
|
|
159
|
+
fallbackUrl: input.chunk.source_url,
|
|
160
|
+
chunk: input.chunk,
|
|
161
|
+
expiresIn: input.expiresIn,
|
|
162
|
+
fetchImpl: input.fetchImpl
|
|
163
|
+
});
|
|
157
164
|
return {
|
|
158
165
|
source_id: input.chunk.source_id,
|
|
159
166
|
destination_id: input.target.destination_id,
|
|
160
167
|
chunk_index: input.chunk.chunk_index,
|
|
161
|
-
source_url:
|
|
168
|
+
source_url: sourceRoute.url,
|
|
162
169
|
dest_url: destUrl,
|
|
163
170
|
source_offset: input.chunk.source_offset,
|
|
164
171
|
chunk_size: input.chunk.chunk_size,
|
|
165
172
|
expires_at: expiresAtIso(input.expiresIn),
|
|
173
|
+
headers: sourceRoute.headers,
|
|
166
174
|
metadata: {
|
|
167
175
|
...(input.target.metadata ?? {}),
|
|
168
176
|
...(input.transferId ? { transfer_id: input.transferId } : {}),
|
|
@@ -182,6 +190,35 @@ export async function signDestinationRoute(input) {
|
|
|
182
190
|
}
|
|
183
191
|
};
|
|
184
192
|
}
|
|
193
|
+
function rangeHeaderForChunk(chunk) {
|
|
194
|
+
const start = Math.max(0, Math.floor(chunk.source_offset));
|
|
195
|
+
const size = Math.max(1, Math.floor(chunk.chunk_size));
|
|
196
|
+
return `bytes=${start}-${start + size - 1}`;
|
|
197
|
+
}
|
|
198
|
+
async function signSourceRoute(input) {
|
|
199
|
+
const range = rangeHeaderForChunk(input.chunk);
|
|
200
|
+
if (!input.source) {
|
|
201
|
+
return { url: input.fallbackUrl, headers: { Range: range } };
|
|
202
|
+
}
|
|
203
|
+
if (isS3CompatibleProvider(input.source)) {
|
|
204
|
+
const client = createS3CompatibleClient(input.source);
|
|
205
|
+
return {
|
|
206
|
+
url: await getSignedUrl(client, new GetObjectCommand({
|
|
207
|
+
Bucket: input.source.bucket,
|
|
208
|
+
Key: input.source.key,
|
|
209
|
+
Range: range
|
|
210
|
+
}), { expiresIn: input.expiresIn }),
|
|
211
|
+
headers: { Range: range }
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
if (isHippiusProvider(input.source)) {
|
|
215
|
+
return {
|
|
216
|
+
url: await hippiusPresign(input.fetchImpl ?? globalThis.fetch, input.source.base_url ?? "https://api.hippius.com", input.source.api_token, input.source.bucket, input.source.key, "get", input.expiresIn),
|
|
217
|
+
headers: { Range: range }
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
return unsupportedProviderConfig(input.source);
|
|
221
|
+
}
|
|
185
222
|
async function signDestinationUrl(input) {
|
|
186
223
|
if (isS3CompatibleProvider(input.destination)) {
|
|
187
224
|
const client = createS3CompatibleClient(input.destination);
|