@fastly/compute-js-static-publish 8.0.0-beta.7 → 8.0.0-beta.9
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 +18 -4
- package/README.short.md +18 -4
- package/build/cli/cli/commands/manage/clean.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/clean.js +15 -21
- package/build/cli/cli/commands/manage/collections/delete.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/collections/delete.js +11 -20
- package/build/cli/cli/commands/manage/collections/index.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/collections/index.js +5 -12
- package/build/cli/cli/commands/manage/collections/list.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/collections/list.js +10 -19
- package/build/cli/cli/commands/manage/collections/promote.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/collections/promote.js +10 -19
- package/build/cli/cli/commands/manage/collections/update-expiration.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/collections/update-expiration.js +10 -19
- package/build/cli/cli/commands/manage/index.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/index.js +5 -12
- package/build/cli/cli/commands/manage/publish-content.d.ts.map +1 -1
- package/build/cli/cli/commands/manage/publish-content.js +93 -80
- package/build/cli/cli/commands/scaffold/index.d.ts.map +1 -1
- package/build/cli/cli/commands/scaffold/index.js +31 -29
- package/build/cli/cli/storage/kv-store-local-provider.d.ts +3 -5
- package/build/cli/cli/storage/kv-store-local-provider.d.ts.map +1 -1
- package/build/cli/cli/storage/kv-store-local-provider.js +3 -8
- package/build/cli/cli/storage/kv-store-provider.d.ts +4 -5
- package/build/cli/cli/storage/kv-store-provider.d.ts.map +1 -1
- package/build/cli/cli/storage/kv-store-provider.js +38 -66
- package/build/cli/cli/storage/s3-storage-provider.d.ts +3 -5
- package/build/cli/cli/storage/s3-storage-provider.d.ts.map +1 -1
- package/build/cli/cli/storage/s3-storage-provider.js +35 -46
- package/build/cli/cli/storage/storage-provider.d.ts +7 -8
- package/build/cli/cli/storage/storage-provider.d.ts.map +1 -1
- package/build/cli/cli/util/content-types.d.ts.map +1 -1
- package/build/cli/cli/util/content-types.js +4 -1
- package/build/cli/cli/util/kv-store-items.d.ts +12 -0
- package/build/cli/cli/util/kv-store-items.d.ts.map +1 -1
- package/build/cli/cli/util/kv-store-items.js +49 -0
- package/build/cli/cli/util/kv-store.d.ts +6 -0
- package/build/cli/cli/util/kv-store.d.ts.map +1 -1
- package/build/cli/cli/util/kv-store.js +39 -2
- package/build/cli/cli/util/retryable.d.ts +2 -1
- package/build/cli/cli/util/retryable.d.ts.map +1 -1
- package/build/cli/cli/util/retryable.js +34 -1
- package/build/cli/cli/util/s3-credentials.d.ts +11 -0
- package/build/cli/cli/util/s3-credentials.d.ts.map +1 -0
- package/build/cli/cli/util/s3-credentials.js +38 -0
- package/build/cli/cli/util/s3.d.ts +1 -1
- package/build/cli/cli/util/s3.d.ts.map +1 -1
- package/build/cli/cli/util/s3.js +1 -1
- package/build/cli/models/assets/index.js +1 -1
- package/build/cli/models/metadata/index.js +1 -1
- package/build/server/models/assets/index.js +1 -1
- package/build/server/models/metadata/index.js +1 -1
- package/build/server/server/index.d.ts +1 -1
- package/build/server/server/index.d.ts.map +1 -1
- package/build/server/server/index.js +1 -1
- package/build/server/server/storage/s3-storage-provider.d.ts +7 -7
- package/build/server/server/storage/s3-storage-provider.d.ts.map +1 -1
- package/build/server/server/storage/s3-storage-provider.js +28 -28
- package/package.json +2 -3
- package/build/cli/cli/util/aws-credentials.d.ts +0 -12
- package/build/cli/cli/util/aws-credentials.d.ts.map +0 -1
- package/build/cli/cli/util/aws-credentials.js +0 -67
|
@@ -3,14 +3,13 @@
|
|
|
3
3
|
* Licensed under the MIT license. See LICENSE file for details.
|
|
4
4
|
*/
|
|
5
5
|
import fs from 'node:fs';
|
|
6
|
-
import { decodeAssetVariantMetadata, } from '../../models/assets/index.js';
|
|
7
6
|
import { isKvStoreConfigRc, } from '../../models/config/static-publish-rc.js';
|
|
8
7
|
import { getKvStoreConfigFromRc, } from '../../models/config/kv-store-config.js';
|
|
9
8
|
import { FetchError, loadApiToken, } from '../util/api-token.js';
|
|
10
9
|
import { rootRelative, } from '../util/files.js';
|
|
11
|
-
import { getKvStoreEntry, getKVStoreKeys, kvStoreDeleteEntry, kvStoreSubmitEntry,
|
|
12
|
-
import { applyKVStoreEntriesChunks, KV_STORE_CHUNK_SIZE, } from '../util/kv-store-items.js';
|
|
13
|
-
import { concurrentParallel, } from '../util/retryable.js';
|
|
10
|
+
import { getKvStoreEntry, getKVStoreKeys, kvStoreDeleteEntry, kvStoreSubmitEntry, kvStoreSubmitBatch, } from '../util/kv-store.js';
|
|
11
|
+
import { applyKVStoreEntriesChunks, packNdjsonBatches, ndjsonLineForBatchEntry, entriesToUpload, KV_STORE_CHUNK_SIZE, } from '../util/kv-store-items.js';
|
|
12
|
+
import { concurrentParallel, makeRetryable, } from '../util/retryable.js';
|
|
14
13
|
export const buildStoreProvider = (config, context) => {
|
|
15
14
|
if (isKvStoreConfigRc(config) && !context.localMode) {
|
|
16
15
|
console.log(` Working on the Fastly KV Store...`);
|
|
@@ -55,20 +54,43 @@ export class KvStoreProvider {
|
|
|
55
54
|
async deleteStorageEntry(key) {
|
|
56
55
|
await kvStoreDeleteEntry(this.fastlyApiContext, this.kvStoreName, key);
|
|
57
56
|
}
|
|
58
|
-
async applyBatch(batch) {
|
|
57
|
+
async applyBatch(batch, options = {}) {
|
|
58
|
+
const { existingKeys } = options;
|
|
59
59
|
console.log(`🍪 Chunking large files...`);
|
|
60
60
|
await applyKVStoreEntriesChunks(batch.storageProviderBatchEntries, KV_STORE_CHUNK_SIZE);
|
|
61
61
|
console.log(`✅ Large files have been chunked.`);
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
62
|
+
let toWrite = batch.storageProviderBatchEntries;
|
|
63
|
+
if (existingKeys != null) {
|
|
64
|
+
toWrite = entriesToUpload(toWrite, existingKeys);
|
|
65
|
+
console.log(` | ${batch.storageProviderBatchEntries.length - toWrite.length} chunk(s) already present in the KV Store.`);
|
|
66
|
+
}
|
|
67
|
+
console.log(`📤 Uploading ${toWrite.length} entries to KV Store.`);
|
|
68
|
+
await this.uploadEntries(toWrite);
|
|
69
|
+
console.log(`✅ Uploaded entries to KV Store.`);
|
|
70
|
+
}
|
|
71
|
+
async uploadEntries(entries) {
|
|
72
|
+
const { batches, oversized } = packNdjsonBatches(entries);
|
|
73
|
+
await this.doConcurrentParallel(oversized, async ({ filePath, metadataJson }, key) => {
|
|
65
74
|
const fileBytes = fs.readFileSync(filePath);
|
|
66
75
|
await kvStoreSubmitEntry(this.fastlyApiContext, this.kvStoreName, key, fileBytes, metadataJson != null ? JSON.stringify(metadataJson) : undefined);
|
|
67
|
-
console.log(` 🌐 Submitted asset "${rootRelative(filePath)}" to KV Store with key "${key}".`);
|
|
68
|
-
});
|
|
69
|
-
|
|
76
|
+
console.log(` 🌐 Submitted large asset "${rootRelative(filePath)}" to KV Store with key "${key}".`);
|
|
77
|
+
}, 12, true);
|
|
78
|
+
await this.doConcurrentParallel(batches, async (batch, key) => {
|
|
79
|
+
const lines = batch.entries.map(ndjsonLineForBatchEntry);
|
|
80
|
+
const failures = await kvStoreSubmitBatch(this.fastlyApiContext, this.kvStoreName, lines);
|
|
81
|
+
if (failures.length > 0) {
|
|
82
|
+
// Keep only the entries that failed. The retry submits only these entries.
|
|
83
|
+
const failedKeys = new Set(failures.map(f => f.key));
|
|
84
|
+
const submittedCount = batch.entries.length;
|
|
85
|
+
batch.entries = batch.entries.filter(entry => failedKeys.has(entry.key));
|
|
86
|
+
const shown = failures.slice(0, 5).map(f => `${f.key} (${f.code ?? 'unknown'}: ${f.reason ?? 'unknown'})`).join(', ');
|
|
87
|
+
const more = failures.length > 5 ? `, and ${failures.length - 5} more` : '';
|
|
88
|
+
throw makeRetryable(new Error(`${failures.length} of ${submittedCount} entries failed: ${shown}${more}`));
|
|
89
|
+
}
|
|
90
|
+
console.log(` 🌐 Submitted ${batch.entries.length} entries to KV Store ("${key}").`);
|
|
91
|
+
}, 12, true);
|
|
70
92
|
}
|
|
71
|
-
async doConcurrentParallel(objects, fn, maxConcurrent = 12) {
|
|
93
|
+
async doConcurrentParallel(objects, fn, maxConcurrent = 12, throwOnError = false) {
|
|
72
94
|
await concurrentParallel(objects, fn, (err) => {
|
|
73
95
|
if (err instanceof FetchError) {
|
|
74
96
|
return `HTTP ${err.status}`;
|
|
@@ -76,65 +98,15 @@ export class KvStoreProvider {
|
|
|
76
98
|
else if (err instanceof TypeError) {
|
|
77
99
|
return 'transport';
|
|
78
100
|
}
|
|
101
|
+
else if (err instanceof Error) {
|
|
102
|
+
return err.message;
|
|
103
|
+
}
|
|
79
104
|
return null;
|
|
80
|
-
}, maxConcurrent);
|
|
105
|
+
}, maxConcurrent, throwOnError);
|
|
81
106
|
}
|
|
82
107
|
calculateNumChunks(size) {
|
|
83
108
|
return Math.ceil(size / KV_STORE_CHUNK_SIZE);
|
|
84
109
|
}
|
|
85
|
-
async getExistingAssetVariant(variantKey) {
|
|
86
|
-
let kvStoreItemMetadata = null;
|
|
87
|
-
const items = [{
|
|
88
|
-
key: variantKey,
|
|
89
|
-
}];
|
|
90
|
-
await this.doConcurrentParallel(items, async (_, variantKey) => {
|
|
91
|
-
// fastlyApiContext is non-null if useKvStore is true
|
|
92
|
-
const kvStoreEntryInfo = await getKvStoreEntryInfo(this.fastlyApiContext, this.kvStoreName, variantKey);
|
|
93
|
-
if (!kvStoreEntryInfo) {
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
let itemMetadata;
|
|
97
|
-
if (kvStoreEntryInfo.metadata != null) {
|
|
98
|
-
try {
|
|
99
|
-
itemMetadata = JSON.parse(kvStoreEntryInfo.metadata);
|
|
100
|
-
}
|
|
101
|
-
catch {
|
|
102
|
-
// if the metadata does not parse successfully as JSON,
|
|
103
|
-
// treat it as though it didn't exist.
|
|
104
|
-
}
|
|
105
|
-
itemMetadata = decodeAssetVariantMetadata(itemMetadata);
|
|
106
|
-
}
|
|
107
|
-
if (itemMetadata != null) {
|
|
108
|
-
let exists = false;
|
|
109
|
-
if (itemMetadata.size <= KV_STORE_CHUNK_SIZE) {
|
|
110
|
-
// For an item equal to or smaller than the chunk size, if it exists
|
|
111
|
-
// and its metadata asserts no chunk count, then we assume it exists.
|
|
112
|
-
if (itemMetadata.numChunks === undefined) {
|
|
113
|
-
exists = true;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
// For chunked objects, if the first chunk exists, and its metadata asserts
|
|
118
|
-
// the same number of chunks based on size, then we assume it exists (for now).
|
|
119
|
-
// In the future we might actually check for the existence and sizes of
|
|
120
|
-
// every chunk in the KV Store.
|
|
121
|
-
const expectedNumChunks = Math.ceil(itemMetadata.size / KV_STORE_CHUNK_SIZE);
|
|
122
|
-
if (itemMetadata.numChunks === expectedNumChunks) {
|
|
123
|
-
exists = true;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
if (exists) {
|
|
127
|
-
kvStoreItemMetadata = {
|
|
128
|
-
contentEncoding: itemMetadata.contentEncoding,
|
|
129
|
-
size: itemMetadata.size,
|
|
130
|
-
hash: itemMetadata.hash,
|
|
131
|
-
numChunks: itemMetadata.numChunks,
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
return kvStoreItemMetadata;
|
|
137
|
-
}
|
|
138
110
|
async purgeSurrogateKey(_surrogateKey) {
|
|
139
111
|
}
|
|
140
112
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { S3Client } from '@aws-sdk/client-s3';
|
|
2
2
|
import { type Command, type HttpHandlerOptions } from '@aws-sdk/types';
|
|
3
|
-
import { type
|
|
4
|
-
import { type StorageEntry, type StorageProvider, type StorageProviderBuilder, type StorageProviderBatch } from './storage-provider.js';
|
|
3
|
+
import { type StorageEntry, type StorageProvider, type StorageProviderBuilder, type StorageProviderBatch, type ApplyBatchOptions } from './storage-provider.js';
|
|
5
4
|
type CommandOutput<C> = C extends Command<any, any, any, infer O, any> ? O : never;
|
|
6
5
|
export declare const buildStoreProvider: StorageProviderBuilder;
|
|
7
6
|
export declare class S3StorageProvider implements StorageProvider {
|
|
@@ -21,12 +20,11 @@ export declare class S3StorageProvider implements StorageProvider {
|
|
|
21
20
|
getStorageEntryInfo(key: string): Promise<StorageEntry | null>;
|
|
22
21
|
submitStorageEntry(key: string, _filePath: string, data: ReadableStream<Uint8Array> | Uint8Array | string | null | undefined, metadata?: Record<string, string>): Promise<void>;
|
|
23
22
|
deleteStorageEntry(key: string): Promise<void>;
|
|
24
|
-
applyBatch(batch: StorageProviderBatch): Promise<void>;
|
|
23
|
+
applyBatch(batch: StorageProviderBatch, options?: ApplyBatchOptions): Promise<void>;
|
|
25
24
|
doConcurrentParallel<TObject extends {
|
|
26
25
|
key: string;
|
|
27
|
-
}>(objects: TObject[], fn: (obj: TObject, key: string, index: number) => Promise<void>, maxConcurrent?: number): Promise<void>;
|
|
26
|
+
}>(objects: TObject[], fn: (obj: TObject, key: string, index: number) => Promise<void>, maxConcurrent?: number, throwOnError?: boolean): Promise<void>;
|
|
28
27
|
calculateNumChunks(_size: number): number;
|
|
29
|
-
getExistingAssetVariant(variantKey: string): Promise<AssetVariantMetadata | null>;
|
|
30
28
|
purgeSurrogateKey(surrogateKey: string): Promise<void>;
|
|
31
29
|
}
|
|
32
30
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"s3-storage-provider.d.ts","sourceRoot":"","sources":["../../../../src/cli/storage/s3-storage-provider.ts"],"names":[],"mappings":"AAOA,OAAO,
|
|
1
|
+
{"version":3,"file":"s3-storage-provider.d.ts","sourceRoot":"","sources":["../../../../src/cli/storage/s3-storage-provider.ts"],"names":[],"mappings":"AAOA,OAAO,EAUL,QAAQ,EAIT,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,KAAK,OAAO,EACZ,KAAK,kBAAkB,EACxB,MAAM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from '../../models/assets/index.js';
|
|
7
|
+
import { DeleteObjectCommand, GetObjectCommand, HeadObjectCommand, PutObjectCommand, S3Client, S3ServiceException, paginateListObjectsV2, } from '@aws-sdk/client-s3';
|
|
9
8
|
import { isS3StorageConfigRc, } from '../../models/config/static-publish-rc.js';
|
|
10
9
|
import { getS3StorageConfigFromRc, } from '../../models/config/s3-storage-config.js';
|
|
11
|
-
import {
|
|
10
|
+
import { entriesToUpload } from '../util/kv-store-items.js';
|
|
11
|
+
import { loadS3Credentials, } from '../util/s3-credentials.js';
|
|
12
12
|
import { concurrentParallel, makeRetryable, } from '../util/retryable.js';
|
|
13
13
|
import { rootRelative, } from '../util/files.js';
|
|
14
14
|
import { loadApiToken, } from '../util/api-token.js';
|
|
@@ -26,15 +26,14 @@ export const buildStoreProvider = async (config, context) => {
|
|
|
26
26
|
console.log(` Region : ${region}`);
|
|
27
27
|
console.log(` Bucket : ${bucket}`);
|
|
28
28
|
console.log(` Endpoint: ${endpoint ?? 'default'}`);
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
awsSecretAccessKey: context.awsSecretAccessKey,
|
|
29
|
+
const s3CredentialsResult = await loadS3Credentials({
|
|
30
|
+
s3AccessKeyId: context.s3AccessKeyId,
|
|
31
|
+
s3SecretAccessKey: context.s3SecretAccessKey,
|
|
33
32
|
});
|
|
34
|
-
if (
|
|
35
|
-
throw new Error("❌ S3 Credentials not provided.\nProvide an
|
|
33
|
+
if (s3CredentialsResult == null) {
|
|
34
|
+
throw new Error("❌ S3 Credentials not provided.\nProvide an access key ID and secret access key that have write access to the S3 or compatible storage.\nRefer to the README file and --help for additional information.");
|
|
36
35
|
}
|
|
37
|
-
console.log(`✔️ S3 Credentials: ${
|
|
36
|
+
console.log(`✔️ S3 Credentials: ${s3CredentialsResult.s3AccessKeyId.slice(0, 4)}${'*'.repeat(s3CredentialsResult.s3AccessKeyId.length - 4)} from '${s3CredentialsResult.source}'`);
|
|
38
37
|
const fastlyTomlPath = path.resolve(context.computeAppDir, 'fastly.toml');
|
|
39
38
|
const serviceId = readServiceId(fastlyTomlPath);
|
|
40
39
|
let apiToken = undefined;
|
|
@@ -50,7 +49,7 @@ export const buildStoreProvider = async (config, context) => {
|
|
|
50
49
|
console.log(`✔️ Fastly API Token: ${apiTokenResult.apiToken.slice(0, 4)}${'*'.repeat(apiTokenResult.apiToken.length - 4)} from '${apiTokenResult.source}'`);
|
|
51
50
|
apiToken = apiTokenResult.apiToken;
|
|
52
51
|
}
|
|
53
|
-
return new S3StorageProvider(serviceId, apiToken, region,
|
|
52
|
+
return new S3StorageProvider(serviceId, apiToken, region, s3CredentialsResult.s3AccessKeyId, s3CredentialsResult.s3SecretAccessKey, bucket, endpoint);
|
|
54
53
|
};
|
|
55
54
|
export class S3StorageProvider {
|
|
56
55
|
constructor(fastlyServiceId, fastlyApiToken, s3Region, accessKeyId, secretAccessKey, s3Bucket, s3Endpoint) {
|
|
@@ -90,20 +89,26 @@ export class S3StorageProvider {
|
|
|
90
89
|
}
|
|
91
90
|
}
|
|
92
91
|
async getStorageKeys(prefix) {
|
|
92
|
+
const paginationConfig = {
|
|
93
|
+
client: this.getS3Client(),
|
|
94
|
+
};
|
|
93
95
|
const input = {
|
|
94
96
|
Bucket: this.s3Bucket,
|
|
95
|
-
MaxKeys: 4096,
|
|
96
97
|
Prefix: prefix,
|
|
97
|
-
ContinuationToken: undefined, // pagination
|
|
98
98
|
};
|
|
99
|
-
const
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
99
|
+
const paginator = paginateListObjectsV2(paginationConfig, input);
|
|
100
|
+
const objectKeys = [];
|
|
101
|
+
for await (const { Contents } of paginator) {
|
|
102
|
+
if (Contents == null) {
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
for (const obj of Contents) {
|
|
106
|
+
if (obj.Key != null) {
|
|
107
|
+
objectKeys.push(obj.Key);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
103
110
|
}
|
|
104
|
-
return
|
|
105
|
-
.map(c => c.Key)
|
|
106
|
-
.filter(c => c != null);
|
|
111
|
+
return objectKeys.length > 0 ? objectKeys : null;
|
|
107
112
|
}
|
|
108
113
|
async getStorageEntry(key) {
|
|
109
114
|
const input = {
|
|
@@ -173,18 +178,23 @@ export class S3StorageProvider {
|
|
|
173
178
|
const command = new DeleteObjectCommand(input);
|
|
174
179
|
await this.sendS3ClientCommand(command);
|
|
175
180
|
}
|
|
176
|
-
async applyBatch(batch) {
|
|
181
|
+
async applyBatch(batch, options = {}) {
|
|
182
|
+
const { existingKeys } = options;
|
|
183
|
+
let toWrite = batch.storageProviderBatchEntries;
|
|
184
|
+
if (existingKeys != null) {
|
|
185
|
+
toWrite = entriesToUpload(toWrite, existingKeys);
|
|
186
|
+
}
|
|
177
187
|
console.log(`📤 Uploading entries to S3 storage.`);
|
|
178
188
|
// fastlyApiContext is non-null if useKvStore is true
|
|
179
|
-
await this.doConcurrentParallel(
|
|
189
|
+
await this.doConcurrentParallel(toWrite, async ({ filePath, metadataJson }, key) => {
|
|
180
190
|
// const fileStream = fs.createReadStream(filePath);
|
|
181
191
|
const fileData = fs.readFileSync(filePath);
|
|
182
192
|
await this.submitStorageEntry(key, filePath, fileData, metadataJson);
|
|
183
193
|
console.log(` 🌐 Submitted asset "${rootRelative(filePath)}" to S3 storage with key "${key}".`);
|
|
184
|
-
});
|
|
194
|
+
}, 12, true);
|
|
185
195
|
console.log(`✅ Uploaded entries to S3 storage.`);
|
|
186
196
|
}
|
|
187
|
-
async doConcurrentParallel(objects, fn, maxConcurrent = 12) {
|
|
197
|
+
async doConcurrentParallel(objects, fn, maxConcurrent = 12, throwOnError = false) {
|
|
188
198
|
await concurrentParallel(objects, fn, (err) => {
|
|
189
199
|
if (err instanceof S3ServiceException) {
|
|
190
200
|
return `S3 error [${err.name}] - ${err.message}`;
|
|
@@ -193,32 +203,11 @@ export class S3StorageProvider {
|
|
|
193
203
|
return 'transport';
|
|
194
204
|
}
|
|
195
205
|
return null;
|
|
196
|
-
}, maxConcurrent);
|
|
206
|
+
}, maxConcurrent, throwOnError);
|
|
197
207
|
}
|
|
198
208
|
calculateNumChunks(_size) {
|
|
199
209
|
return 1;
|
|
200
210
|
}
|
|
201
|
-
async getExistingAssetVariant(variantKey) {
|
|
202
|
-
let assetVariantMetadata = null;
|
|
203
|
-
await this.doConcurrentParallel([{ key: variantKey }], async (_, variantKey) => {
|
|
204
|
-
const entryInfo = await this.getStorageEntryInfo(variantKey);
|
|
205
|
-
if (entryInfo == null) {
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
const metadata = decodeAssetVariantMetadata(entryInfo.metadata);
|
|
209
|
-
if (metadata != null) {
|
|
210
|
-
if (metadata.numChunks !== undefined) {
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
assetVariantMetadata = {
|
|
214
|
-
contentEncoding: metadata.contentEncoding,
|
|
215
|
-
size: metadata.size,
|
|
216
|
-
hash: metadata.hash,
|
|
217
|
-
};
|
|
218
|
-
}
|
|
219
|
-
});
|
|
220
|
-
return assetVariantMetadata;
|
|
221
|
-
}
|
|
222
211
|
async purgeSurrogateKey(surrogateKey) {
|
|
223
212
|
if (this.fastlyServiceId == null) {
|
|
224
213
|
console.log('Fastly Service ID not set, skipping purge...');
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { type AssetVariantMetadata } from '../../models/assets/index.js';
|
|
2
1
|
import { type StaticPublishRc } from '../../models/config/static-publish-rc.js';
|
|
3
2
|
export interface StorageEntry {
|
|
4
3
|
data?: ReadableStream<Uint8Array>;
|
|
@@ -11,21 +10,22 @@ export interface StorageProvider {
|
|
|
11
10
|
getStorageEntry(key: string): Promise<StorageEntry | null>;
|
|
12
11
|
submitStorageEntry(key: string, filePath: string, data: ReadableStream<Uint8Array> | Uint8Array | string | null | undefined, metadata?: Record<string, string>): Promise<void>;
|
|
13
12
|
deleteStorageEntry(key: string): Promise<void>;
|
|
14
|
-
applyBatch(batch: StorageProviderBatch): Promise<void>;
|
|
13
|
+
applyBatch(batch: StorageProviderBatch, options?: ApplyBatchOptions): Promise<void>;
|
|
15
14
|
doConcurrentParallel<TObject extends {
|
|
16
15
|
key: string;
|
|
17
|
-
}>(objects: TObject[], fn: (obj: TObject, key: string, index: number) => Promise<void>, maxConcurrent?: number): Promise<void>;
|
|
16
|
+
}>(objects: TObject[], fn: (obj: TObject, key: string, index: number) => Promise<void>, maxConcurrent?: number, throwOnError?: boolean): Promise<void>;
|
|
18
17
|
calculateNumChunks(size: number): number;
|
|
19
|
-
getExistingAssetVariant(variantKey: string): Promise<AssetVariantMetadata | null>;
|
|
20
18
|
purgeSurrogateKey(surrogateKey: string): Promise<void>;
|
|
21
19
|
}
|
|
22
20
|
export type StorageProviderBatchEntry = {
|
|
23
|
-
write: boolean;
|
|
24
21
|
size: number;
|
|
25
22
|
key: string;
|
|
26
23
|
filePath: string;
|
|
27
24
|
metadataJson?: Record<string, string>;
|
|
28
25
|
};
|
|
26
|
+
export type ApplyBatchOptions = {
|
|
27
|
+
existingKeys?: Set<string>;
|
|
28
|
+
};
|
|
29
29
|
export declare class StorageProviderBatch {
|
|
30
30
|
constructor();
|
|
31
31
|
storageProviderBatchEntries: StorageProviderBatchEntry[];
|
|
@@ -35,9 +35,8 @@ export type StorageProviderBuilderContext = {
|
|
|
35
35
|
computeAppDir: string;
|
|
36
36
|
localMode?: boolean;
|
|
37
37
|
fastlyApiToken?: string;
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
awsSecretAccessKey?: string;
|
|
38
|
+
s3AccessKeyId?: string;
|
|
39
|
+
s3SecretAccessKey?: string;
|
|
41
40
|
};
|
|
42
41
|
export type StorageProviderBuilder = (config: StaticPublishRc, context: StorageProviderBuilderContext) => (Promise<StorageProvider | null> | StorageProvider | null);
|
|
43
42
|
export declare function registerStorageProviderBuilder(builder: StorageProviderBuilder): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage-provider.d.ts","sourceRoot":"","sources":["../../../../src/cli/storage/storage-provider.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,KAAK,
|
|
1
|
+
{"version":3,"file":"storage-provider.d.ts","sourceRoot":"","sources":["../../../../src/cli/storage/storage-provider.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,KAAK,eAAe,EACrB,MAAM,0CAA0C,CAAC;AAElD,MAAM,WAAW,YAAY;IAC3B,IAAI,CAAC,EAAE,cAAc,CAAC,UAAU,CAAC,CAAC;IAClC,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAClC,gBAAgB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC3C;AAED,MAAM,WAAW,eAAe;IAC9B,cAAc,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,CAAC;IAC1D,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC,CAAC;IAC/D,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC,CAAC;IAC3D,kBAAkB,CAChB,GAAG,EAAE,MAAM,EACX,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,cAAc,CAAC,UAAU,CAAC,GAAG,UAAU,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,EACzE,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAChC,OAAO,CAAC,IAAI,CAAC,CAAC;IACjB,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC/C,UAAU,CAAC,KAAK,EAAE,oBAAoB,EAAE,OAAO,CAAC,EAAE,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACpF,oBAAoB,CAAC,OAAO,SAAS;QAAE,GAAG,EAAE,MAAM,CAAA;KA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sBAAsB,QAE7E;AAED,wBAAsB,sCAAsC,CAAC,MAAM,EAAE,eAAe,EAAE,OAAO,EAAE,6BAA6B,4BAS3H"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"content-types.d.ts","sourceRoot":"","sources":["../../../../src/cli/util/content-types.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,qBAAqB,EAC3B,MAAM,+CAA+C,CAAC;
|
|
1
|
+
{"version":3,"file":"content-types.d.ts","sourceRoot":"","sources":["../../../../src/cli/util/content-types.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,qBAAqB,EAC3B,MAAM,+CAA+C,CAAC;AAwCvD,wBAAgB,iBAAiB,CAAC,YAAY,EAAE,cAAc,EAAE,oBAmD/D;AAED,wBAAgB,mBAAmB,CAAC,YAAY,EAAE,cAAc,EAAE,GAAG,IAAI,GAAG,SAAS,EAAE,QAAQ,EAAE,MAAM,GAAG,qBAAqB,GAAG,IAAI,CAkBrI"}
|
|
@@ -18,8 +18,10 @@ const defaultContentTypes = [
|
|
|
18
18
|
{ test: /\.gif$/, contentType: 'image/gif', text: false },
|
|
19
19
|
{ test: /\.jp(e)?g$/, contentType: 'image/jpeg', text: false },
|
|
20
20
|
{ test: /\.ico$/, contentType: 'image/vnd.microsoft.icon', text: false },
|
|
21
|
-
{ test: /\.tif(f)?$/, contentType: 'image/
|
|
21
|
+
{ test: /\.tif(f)?$/, contentType: 'image/tiff', text: false },
|
|
22
22
|
{ test: /\.webp$/, contentType: 'image/webp', text: false },
|
|
23
|
+
{ test: /\.avif$/, contentType: 'image/avif', text: false },
|
|
24
|
+
{ test: /\.jxl$/, contentType: 'image/jxl', text: false },
|
|
23
25
|
{ test: /\.aac$/, contentType: 'audio/aac', text: false },
|
|
24
26
|
{ test: /\.mp3$/, contentType: 'audio/mpeg', text: false },
|
|
25
27
|
{ test: /\.avi$/, contentType: 'video/x-msvideo', text: false },
|
|
@@ -29,6 +31,7 @@ const defaultContentTypes = [
|
|
|
29
31
|
{ test: /\.pdf$/, contentType: 'application/pdf', text: false },
|
|
30
32
|
{ test: /\.tar$/, contentType: 'application/x-tar', text: false, precompressAsset: true, },
|
|
31
33
|
{ test: /\.zip$/, contentType: 'application/zip', text: false },
|
|
34
|
+
{ test: /\.wasm$/, contentType: 'application/wasm', text: false },
|
|
32
35
|
{ test: /\.eot$/, contentType: 'application/vnd.ms-fontobject', text: false },
|
|
33
36
|
{ test: /\.otf$/, contentType: 'font/otf', text: false },
|
|
34
37
|
{ test: /\.ttf$/, contentType: 'font/ttf', text: false },
|
|
@@ -2,4 +2,16 @@ import { StorageProviderBatchEntry } from '../storage/storage-provider.js';
|
|
|
2
2
|
export declare const KV_STORE_CHUNK_SIZE: number;
|
|
3
3
|
export declare function shouldRecreateChunks(chunksDir: string, numChunks: number, item: StorageProviderBatchEntry, chunkSize: number): boolean;
|
|
4
4
|
export declare function applyKVStoreEntriesChunks(kvStoreItemDescriptions: StorageProviderBatchEntry[], chunkSize: number): Promise<void>;
|
|
5
|
+
export declare const KV_BATCH_MAX_ITEMS = 256;
|
|
6
|
+
export declare const KV_BATCH_MAX_BYTES: number;
|
|
7
|
+
export type NdjsonBatch = {
|
|
8
|
+
key: string;
|
|
9
|
+
entries: StorageProviderBatchEntry[];
|
|
10
|
+
};
|
|
11
|
+
export declare function packNdjsonBatches(entries: StorageProviderBatchEntry[], maxItems?: number, maxBytes?: number): {
|
|
12
|
+
batches: NdjsonBatch[];
|
|
13
|
+
oversized: StorageProviderBatchEntry[];
|
|
14
|
+
};
|
|
15
|
+
export declare function ndjsonLineForBatchEntry(entry: StorageProviderBatchEntry): string;
|
|
16
|
+
export declare function entriesToUpload(entries: StorageProviderBatchEntry[], existingKeys: Set<string>): StorageProviderBatchEntry[];
|
|
5
17
|
//# sourceMappingURL=kv-store-items.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"kv-store-items.d.ts","sourceRoot":"","sources":["../../../../src/cli/util/kv-store-items.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC;AAI3E,eAAO,MAAM,mBAAmB,QAAqB,CAAC;AAEtD,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,yBAAyB,EAAE,SAAS,EAAE,MAAM,WAkC5H;AAED,wBAAsB,yBAAyB,CAAC,uBAAuB,EAAE,yBAAyB,EAAE,EAAE,SAAS,EAAE,MAAM,iBAoGtH"}
|
|
1
|
+
{"version":3,"file":"kv-store-items.d.ts","sourceRoot":"","sources":["../../../../src/cli/util/kv-store-items.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC;AAI3E,eAAO,MAAM,mBAAmB,QAAqB,CAAC;AAEtD,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,yBAAyB,EAAE,SAAS,EAAE,MAAM,WAkC5H;AAED,wBAAsB,yBAAyB,CAAC,uBAAuB,EAAE,yBAAyB,EAAE,EAAE,SAAS,EAAE,MAAM,iBAoGtH;AAED,eAAO,MAAM,kBAAkB,MAAM,CAAC;AACtC,eAAO,MAAM,kBAAkB,QAAoB,CAAC;AAEpD,MAAM,MAAM,WAAW,GAAG;IACxB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,yBAAyB,EAAE,CAAC;CACtC,CAAC;AAEF,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,yBAAyB,EAAE,EACpC,QAAQ,GAAE,MAA2B,EACrC,QAAQ,GAAE,MAA2B,GACpC;IAAE,OAAO,EAAE,WAAW,EAAE,CAAC;IAAC,SAAS,EAAE,yBAAyB,EAAE,CAAA;CAAE,CAuCpE;AAED,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,yBAAyB,GAAG,MAAM,CAOhF;AAED,wBAAgB,eAAe,CAC7B,OAAO,EAAE,yBAAyB,EAAE,EACpC,YAAY,EAAE,GAAG,CAAC,MAAM,CAAC,GACxB,yBAAyB,EAAE,CAE7B"}
|
|
@@ -114,3 +114,52 @@ export async function applyKVStoreEntriesChunks(kvStoreItemDescriptions, chunkSi
|
|
|
114
114
|
}
|
|
115
115
|
}
|
|
116
116
|
}
|
|
117
|
+
export const KV_BATCH_MAX_ITEMS = 256;
|
|
118
|
+
export const KV_BATCH_MAX_BYTES = 8 * 1_024 * 1_024;
|
|
119
|
+
export function packNdjsonBatches(entries, maxItems = KV_BATCH_MAX_ITEMS, maxBytes = KV_BATCH_MAX_BYTES) {
|
|
120
|
+
const lineBytesFor = (entry) => {
|
|
121
|
+
const envelope = JSON.stringify({
|
|
122
|
+
key: entry.key,
|
|
123
|
+
value: '',
|
|
124
|
+
...(entry.metadataJson != null ? { metadata: JSON.stringify(entry.metadataJson) } : {}),
|
|
125
|
+
});
|
|
126
|
+
return envelope.length + Math.ceil(entry.size / 3) * 4;
|
|
127
|
+
};
|
|
128
|
+
const oversized = [];
|
|
129
|
+
const groups = [];
|
|
130
|
+
let current = [];
|
|
131
|
+
let currentBytes = 0;
|
|
132
|
+
for (const entry of entries) {
|
|
133
|
+
const lineBytes = lineBytesFor(entry);
|
|
134
|
+
if (lineBytes > maxBytes) {
|
|
135
|
+
oversized.push(entry);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
if (current.length > 0 && (current.length >= maxItems || currentBytes + lineBytes > maxBytes)) {
|
|
139
|
+
groups.push(current);
|
|
140
|
+
current = [];
|
|
141
|
+
currentBytes = 0;
|
|
142
|
+
}
|
|
143
|
+
current.push(entry);
|
|
144
|
+
currentBytes += lineBytes;
|
|
145
|
+
}
|
|
146
|
+
if (current.length > 0) {
|
|
147
|
+
groups.push(current);
|
|
148
|
+
}
|
|
149
|
+
const batches = groups.map((groupEntries, i) => ({
|
|
150
|
+
key: `batch-${i}`,
|
|
151
|
+
entries: groupEntries,
|
|
152
|
+
}));
|
|
153
|
+
return { batches, oversized };
|
|
154
|
+
}
|
|
155
|
+
export function ndjsonLineForBatchEntry(entry) {
|
|
156
|
+
const value = fs.readFileSync(entry.filePath).toString('base64');
|
|
157
|
+
return JSON.stringify({
|
|
158
|
+
key: entry.key,
|
|
159
|
+
value,
|
|
160
|
+
...(entry.metadataJson != null ? { metadata: JSON.stringify(entry.metadataJson) } : {}),
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
export function entriesToUpload(entries, existingKeys) {
|
|
164
|
+
return entries.filter((entry) => !existingKeys.has(entry.key));
|
|
165
|
+
}
|
|
@@ -17,5 +17,11 @@ export declare function getKvStoreEntryInfo(fastlyApiContext: FastlyApiContext,
|
|
|
17
17
|
export declare function getKvStoreEntry(fastlyApiContext: FastlyApiContext, kvStoreName: string, key: string, metadataOnly?: boolean): Promise<KvStoreEntryInfo | null>;
|
|
18
18
|
export declare function kvStoreSubmitEntry(fastlyApiContext: FastlyApiContext, kvStoreName: string, key: string, data: ReadableStream<Uint8Array> | Uint8Array | string, metadata: string | undefined): Promise<void>;
|
|
19
19
|
export declare function kvStoreDeleteEntry(fastlyApiContext: FastlyApiContext, kvStoreName: string, key: string): Promise<void>;
|
|
20
|
+
export type KvStoreBatchError = {
|
|
21
|
+
key: string;
|
|
22
|
+
code?: string;
|
|
23
|
+
reason?: string;
|
|
24
|
+
};
|
|
25
|
+
export declare function kvStoreSubmitBatch(fastlyApiContext: FastlyApiContext, kvStoreName: string, ndjsonLines: string[]): Promise<KvStoreBatchError[]>;
|
|
20
26
|
export {};
|
|
21
27
|
//# sourceMappingURL=kv-store.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"kv-store.d.ts","sourceRoot":"","sources":["../../../../src/cli/util/kv-store.ts"],"names":[],"mappings":"AAKA,OAAO,EAAiB,KAAK,gBAAgB,EAAc,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version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AC,UAAU,CAAC,GAAG,UAAU,GAAG,MAAM,EACtD,QAAQ,EAAE,MAAM,GAAG,SAAS,iBAoB7B;AAED,wBAAsB,kBAAkB,CAAC,gBAAgB,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,iBAa5G;AAED,MAAM,MAAM,iBAAiB,GAAG;IAC9B,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAKF,wBAAsB,kBAAkB,CACtC,gBAAgB,EAAE,gBAAgB,EAClC,WAAW,EAAE,MAAM,EACnB,WAAW,EAAE,MAAM,EAAE,GACpB,OAAO,CAAC,iBAAiB,EAAE,CAAC,CAwC9B"}
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* Licensed under the MIT license. See LICENSE file for details.
|
|
4
4
|
*/
|
|
5
5
|
import { callFastlyApi, FetchError } from './api-token.js';
|
|
6
|
+
import { makeRetryable } from './retryable.js';
|
|
6
7
|
const cache = new WeakMap();
|
|
7
8
|
export async function getKVStoreIdForNameMap(fastlyApiContext) {
|
|
8
9
|
const cacheEntry = cache.get(fastlyApiContext);
|
|
@@ -59,9 +60,12 @@ export async function getKVStoreInfos(fastlyApiContext) {
|
|
|
59
60
|
return kvStoreInfos;
|
|
60
61
|
}
|
|
61
62
|
export const _getKVStoreKeys = createArrayGetter()((kvStoreId, prefix) => {
|
|
62
|
-
|
|
63
|
+
// Read from the primary data source. An eventual read can show a key
|
|
64
|
+
// that was deleted a short time ago. Then publish-content does not upload
|
|
65
|
+
// an item that the new index needs.
|
|
66
|
+
let endpoint = `/resources/stores/kv/${encodeURIComponent(kvStoreId)}/keys?consistency=strong`;
|
|
63
67
|
if (prefix != null) {
|
|
64
|
-
endpoint += '
|
|
68
|
+
endpoint += '&prefix=' + encodeURIComponent(prefix);
|
|
65
69
|
}
|
|
66
70
|
return endpoint;
|
|
67
71
|
});
|
|
@@ -126,3 +130,36 @@ export async function kvStoreDeleteEntry(fastlyApiContext, kvStoreName, key) {
|
|
|
126
130
|
method: 'DELETE',
|
|
127
131
|
});
|
|
128
132
|
}
|
|
133
|
+
// Returns the items that the KV Store reports as failed. The caller can
|
|
134
|
+
// submit only these items again. Throws if the response does not identify
|
|
135
|
+
// the failed items.
|
|
136
|
+
export async function kvStoreSubmitBatch(fastlyApiContext, kvStoreName, ndjsonLines) {
|
|
137
|
+
const kvStoreId = await getKVStoreIdForName(fastlyApiContext, kvStoreName);
|
|
138
|
+
if (kvStoreId == null) {
|
|
139
|
+
throw new Error(`KV Store '${kvStoreName}' not found.`);
|
|
140
|
+
}
|
|
141
|
+
const response = await callFastlyApi(fastlyApiContext, `/resources/stores/kv/${encodeURIComponent(kvStoreId)}/batch`, `Submitting batch of ${ndjsonLines.length} item(s) to KV Store`, null, {
|
|
142
|
+
method: 'PUT',
|
|
143
|
+
headers: { 'Content-Type': 'application/x-ndjson' },
|
|
144
|
+
body: ndjsonLines.join('\n'),
|
|
145
|
+
});
|
|
146
|
+
// A 207 response has the body
|
|
147
|
+
// {"title":"some inserts failed","errors":[{"key":"...","code":"...","reason":"..."}]}
|
|
148
|
+
// A 200 response has the body {"title":"success","errors":[]}.
|
|
149
|
+
// Read the body in all cases, so that the connection can be used again.
|
|
150
|
+
const detail = await response.text().catch(() => '');
|
|
151
|
+
let errors;
|
|
152
|
+
try {
|
|
153
|
+
errors = JSON.parse(detail)?.errors;
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
errors = undefined;
|
|
157
|
+
}
|
|
158
|
+
if (Array.isArray(errors) && errors.every(e => typeof e?.key === 'string')) {
|
|
159
|
+
return errors.map(e => ({ key: e.key, code: e.code, reason: e.reason }));
|
|
160
|
+
}
|
|
161
|
+
if (response.status === 207) {
|
|
162
|
+
throw makeRetryable(new FetchError(`Batch upload partially failed (207): ${detail.slice(0, 300)}`, 207));
|
|
163
|
+
}
|
|
164
|
+
return [];
|
|
165
|
+
}
|
|
@@ -10,6 +10,7 @@ type RetryWithBackoffOptions = {
|
|
|
10
10
|
export declare function attemptWithRetries<TResult>(fn: () => Promise<TResult>, options?: RetryWithBackoffOptions): Promise<TResult>;
|
|
11
11
|
export declare function concurrentParallel<TObject extends {
|
|
12
12
|
key: string;
|
|
13
|
-
}>(objects: TObject[], fn: (obj: TObject, key: string, index: number) => Promise<void>, buildStatusMessage: (err: unknown) => (string | null), maxConcurrent?: number): Promise<void>;
|
|
13
|
+
}>(objects: TObject[], fn: (obj: TObject, key: string, index: number) => Promise<void>, buildStatusMessage: (err: unknown) => (string | null), maxConcurrent?: number, throwOnError?: boolean): Promise<void>;
|
|
14
|
+
export declare function concurrentMap<TItem, TResult>(items: TItem[], fn: (item: TItem, index: number) => Promise<TResult>, maxConcurrent?: number): Promise<TResult[]>;
|
|
14
15
|
export {};
|
|
15
16
|
//# sourceMappingURL=retryable.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retryable.d.ts","sourceRoot":"","sources":["../../../../src/cli/util/retryable.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"retryable.d.ts","sourceRoot":"","sources":["../../../../src/cli/util/retryable.ts"],"names":[],"mappings":"AAUA,wBAAgB,aAAa,CAAC,KAAK,EAAE,KAAK,SAGzC;AACD,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,OAK9C;AAED,KAAK,uBAAuB,GAAG;IAC7B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACtC,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACtC,OAAO,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CAClE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