@fastly/compute-js-static-publish 8.0.2 → 8.0.3-rc.2

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.
Files changed (48) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/DOCUMENTATION.md +28 -23
  3. package/MIGRATING.md +1 -1
  4. package/README.md +3 -3
  5. package/README.short.md +3 -3
  6. package/build/cli/cli/commands/manage/clean.d.ts.map +1 -1
  7. package/build/cli/cli/commands/manage/clean.js +27 -0
  8. package/build/cli/cli/commands/manage/publish-content.d.ts.map +1 -1
  9. package/build/cli/cli/commands/manage/publish-content.js +38 -1
  10. package/build/cli/cli/storage/kv-store-local-provider.d.ts +2 -2
  11. package/build/cli/cli/storage/kv-store-local-provider.d.ts.map +1 -1
  12. package/build/cli/cli/storage/kv-store-local-provider.js +2 -14
  13. package/build/cli/cli/storage/kv-store-provider.d.ts +2 -2
  14. package/build/cli/cli/storage/kv-store-provider.d.ts.map +1 -1
  15. package/build/cli/cli/storage/kv-store-provider.js +2 -1
  16. package/build/cli/cli/storage/s3-storage-provider.d.ts +2 -2
  17. package/build/cli/cli/storage/s3-storage-provider.d.ts.map +1 -1
  18. package/build/cli/cli/storage/s3-storage-provider.js +2 -1
  19. package/build/cli/cli/storage/storage-provider.d.ts +3 -1
  20. package/build/cli/cli/storage/storage-provider.d.ts.map +1 -1
  21. package/build/cli/cli/storage/storage-provider.js +16 -0
  22. package/build/cli/cli/util/kv-store.d.ts +1 -1
  23. package/build/cli/cli/util/kv-store.d.ts.map +1 -1
  24. package/build/cli/models/index-parts/index.d.ts +20 -0
  25. package/build/cli/models/index-parts/index.d.ts.map +1 -0
  26. package/build/cli/models/index-parts/index.js +76 -0
  27. package/build/cli/models/server/index.d.ts +2 -0
  28. package/build/cli/models/server/index.d.ts.map +1 -1
  29. package/build/cli/models/server/index.js +7 -1
  30. package/build/server/models/index-parts/index.d.ts +20 -0
  31. package/build/server/models/index-parts/index.d.ts.map +1 -0
  32. package/build/server/models/index-parts/index.js +76 -0
  33. package/build/server/models/server/index.d.ts +2 -0
  34. package/build/server/models/server/index.d.ts.map +1 -1
  35. package/build/server/models/server/index.js +7 -1
  36. package/build/server/server/index.d.ts +1 -1
  37. package/build/server/server/index.d.ts.map +1 -1
  38. package/build/server/server/publisher-server/index.d.ts +5 -0
  39. package/build/server/server/publisher-server/index.d.ts.map +1 -1
  40. package/build/server/server/publisher-server/index.js +95 -21
  41. package/build/server/server/storage/kv-store-provider.d.ts.map +1 -1
  42. package/build/server/server/storage/kv-store-provider.js +1 -0
  43. package/build/server/server/storage/s3-storage-provider.d.ts +2 -2
  44. package/build/server/server/storage/s3-storage-provider.d.ts.map +1 -1
  45. package/build/server/server/storage/s3-storage-provider.js +24 -3
  46. package/build/server/server/storage/storage-provider.d.ts +12 -1
  47. package/build/server/server/storage/storage-provider.d.ts.map +1 -1
  48. package/package.json +1 -1
@@ -5,7 +5,8 @@
5
5
  /// <reference types="@fastly/js-compute" />
6
6
  import { CoreCache } from 'fastly:cache';
7
7
  import { decodeAssetVariantMetadata, } from '../../models/assets/index.js';
8
- import { decodeIndexMetadata, getUnsupportedIndexFeatures } from '../../models/server/index.js';
8
+ import { decodeIndexMetadata, getUnsupportedIndexFeatures, } from '../../models/server/index.js';
9
+ import { getIndexPartKey, getIndexPartNumber, } from '../../models/index-parts/index.js';
9
10
  import { isExpired } from '../../models/time/index.js';
10
11
  import { loadStorageProviderFromStaticPublishRc, } from '../storage/storage-provider.js';
11
12
  import { checkIfModifiedSince, getIfModifiedSinceHeader } from './serve-preconditions/if-modified-since.js';
@@ -32,8 +33,11 @@ export function buildHeadersSubset(responseHeaders, keys) {
32
33
  // * Content-Location, Date, ETag, and Vary
33
34
  // * Cache-Control and Expires
34
35
  const headersToPreserveForUnmodified = ['Content-Location', 'ETag', 'Vary', 'Cache-Control', 'Expires'];
36
+ // The time, in seconds, that a storage provider with a cache keeps a file (one year).
37
+ const FILE_CACHE_TTL = 31536000;
35
38
  export class PublisherServer {
36
39
  constructor(publishId, storageProvider, defaultCollectionName) {
40
+ this.indexPartsCache = new Map();
37
41
  // When true, serveCached() does not store the response of the current request.
38
42
  this.doNotStoreResponse = false;
39
43
  this.publishId = publishId;
@@ -56,6 +60,8 @@ export class PublisherServer {
56
60
  this.activeCollectionName = collectionName ?? this.defaultCollectionName;
57
61
  this.settingsCached = undefined;
58
62
  this.assetEntryMapCache = undefined;
63
+ this.indexCache = undefined;
64
+ this.indexPartsCache.clear();
59
65
  }
60
66
  setCollectionNameHeader(collectionHeader) {
61
67
  this.collectionNameHeader = collectionHeader;
@@ -81,6 +87,8 @@ export class PublisherServer {
81
87
  beginRequest(request) {
82
88
  this.settingsCached = undefined;
83
89
  this.assetEntryMapCache = undefined;
90
+ this.indexCache = undefined;
91
+ this.indexPartsCache.clear();
84
92
  this.doNotStoreResponse = false;
85
93
  const header = this.serverTimingRequestHeader;
86
94
  this.serverTiming = header != null && request.headers.has(header) ? new ServerTiming() : null;
@@ -125,27 +133,31 @@ export class PublisherServer {
125
133
  })
126
134
  .filter(x => Boolean(x));
127
135
  }
128
- async getAssetEntryMap() {
129
- if (this.assetEntryMapCache !== undefined) {
130
- return this.assetEntryMapCache;
136
+ // Reads the index entry of the active collection, but not its body. Returns
137
+ // null if there is no usable index: the index is not found, it requires a
138
+ // feature that this version does not support, or the collection expired.
139
+ async getIndex() {
140
+ if (this.indexCache !== undefined) {
141
+ return this.indexCache;
131
142
  }
132
143
  const indexFileKey = `${this.publishId}_index_${this.activeCollectionName}`;
133
144
  const indexFile = await this.timed('index', () => this.storageProvider.getEntry(indexFileKey, [`${this.publishId}-${this.activeCollectionName}`, 'index']));
134
145
  if (indexFile == null) {
135
146
  console.error(`Index File not found at ${indexFileKey}.`);
136
147
  console.error(`You may need to publish your application.`);
137
- this.assetEntryMapCache = null;
148
+ this.indexCache = null;
138
149
  return null;
139
150
  }
140
- let metadata;
151
+ let metadata = null;
141
152
  const metadataText = indexFile.metadataText();
142
153
  if (metadataText != null) {
154
+ let metadataObj;
143
155
  try {
144
- metadata = JSON.parse(metadataText);
156
+ metadataObj = JSON.parse(metadataText);
145
157
  }
146
158
  catch {
147
159
  }
148
- metadata = decodeIndexMetadata(metadata);
160
+ metadata = decodeIndexMetadata(metadataObj);
149
161
  }
150
162
  // Do not serve a collection that needs a feature that this version does not
151
163
  // support. This check applies to all collections, also the default collection.
@@ -153,7 +165,7 @@ export class PublisherServer {
153
165
  if (unsupportedFeatures.length > 0) {
154
166
  console.error(`Collection at ${indexFileKey} requires features that this version of the server does not support: ${unsupportedFeatures.join(', ')}.`);
155
167
  console.error(`Build and deploy the application with a newer version of @fastly/compute-js-static-publish.`);
156
- this.assetEntryMapCache = null;
168
+ this.indexCache = null;
157
169
  return null;
158
170
  }
159
171
  let collectionIsExpired = false;
@@ -162,26 +174,86 @@ export class PublisherServer {
162
174
  }
163
175
  if (collectionIsExpired) {
164
176
  console.error(`Requested collection expired at ${indexFileKey}.`);
177
+ this.indexCache = null;
178
+ return null;
179
+ }
180
+ this.indexCache = { key: indexFileKey, entry: indexFile, metadata };
181
+ return this.indexCache;
182
+ }
183
+ // Returns the whole index of the active collection. This reads and parses
184
+ // the full index. To find one asset, getMatchingAsset() is faster, because
185
+ // it reads only one index part if the index has parts.
186
+ async getAssetEntryMap() {
187
+ if (this.assetEntryMapCache !== undefined) {
188
+ return this.assetEntryMapCache;
189
+ }
190
+ const index = await this.getIndex();
191
+ if (index == null) {
165
192
  this.assetEntryMapCache = null;
166
193
  return null;
167
194
  }
168
195
  // Read and parse in two steps, so that Server-Timing can report each one.
169
- const indexText = await this.timed('index-body', () => indexFile.text());
196
+ const indexText = await this.timed('index-body', () => index.entry.text());
170
197
  this.assetEntryMapCache = await this.timed('index-parse', async () => JSON.parse(indexText));
171
198
  return this.assetEntryMapCache;
172
199
  }
200
+ // Reads one index part. Returns null if the part cannot be read, so that the
201
+ // caller uses the full index.
202
+ async getIndexPart(indexParts, partNumber) {
203
+ const cached = this.indexPartsCache.get(partNumber);
204
+ if (cached !== undefined) {
205
+ return cached;
206
+ }
207
+ const partKey = getIndexPartKey(this.publishId, indexParts, partNumber);
208
+ let part = null;
209
+ try {
210
+ part = await this.timed('index-part', async () => {
211
+ // The key of a part has the hash of the full index, so a cached copy is
212
+ // never out of date. It has the same cache TTL and tag as a file.
213
+ const entry = await this.storageProvider.getEntry(partKey, [`${this.publishId}-files`], { ttl: FILE_CACHE_TTL });
214
+ return entry != null ? (await entry.json()) : null;
215
+ });
216
+ if (part == null) {
217
+ console.warn(`Index part not found at ${partKey}. Using the full index.`);
218
+ }
219
+ }
220
+ catch (err) {
221
+ console.warn(`Could not read the index part at ${partKey}. Using the full index.`, err);
222
+ }
223
+ this.indexPartsCache.set(partNumber, part);
224
+ return part;
225
+ }
226
+ // Finds the entry of one asset key in the index of the active collection.
227
+ // If the index has parts, this reads only the part that has the key.
228
+ async findAssetEntry(assetKey) {
229
+ if (this.assetEntryMapCache != null) {
230
+ return this.assetEntryMapCache[assetKey] ?? null;
231
+ }
232
+ const index = await this.getIndex();
233
+ if (index == null) {
234
+ return null;
235
+ }
236
+ const indexParts = index.metadata?.indexParts;
237
+ if (indexParts != null) {
238
+ const part = await this.getIndexPart(indexParts, getIndexPartNumber(assetKey, indexParts.count));
239
+ if (part != null) {
240
+ return part[assetKey] ?? null;
241
+ }
242
+ }
243
+ const assetEntryMap = await this.getAssetEntryMap();
244
+ return assetEntryMap?.[assetKey] ?? null;
245
+ }
173
246
  async getMatchingAsset(assetKey, applyAuto = false) {
174
247
  const serverConfig = await this.getServerConfig();
175
248
  if (serverConfig == null) {
176
249
  return null;
177
250
  }
178
- const assetEntryMap = await this.getAssetEntryMap();
179
- if (assetEntryMap == null) {
251
+ if (await this.getIndex() == null) {
180
252
  return null;
181
253
  }
182
254
  if (!assetKey.endsWith('/')) {
183
255
  // A path that does not end in a slash can match an asset directly
184
- const asset = assetEntryMap[assetKey];
256
+ const asset = await this.findAssetEntry(assetKey);
185
257
  if (asset != null) {
186
258
  return asset;
187
259
  }
@@ -190,7 +262,7 @@ export class PublisherServer {
190
262
  // looks for an asset that has the specified suffix (usually extension, such as .html)
191
263
  for (const extEntry of serverConfig.autoExt) {
192
264
  let assetKeyWithExt = assetKey + extEntry;
193
- const asset = assetEntryMap[assetKeyWithExt];
265
+ const asset = await this.findAssetEntry(assetKeyWithExt);
194
266
  if (asset != null) {
195
267
  return asset;
196
268
  }
@@ -210,7 +282,7 @@ export class PublisherServer {
210
282
  assetNameAsDir = assetNameAsDir + '/';
211
283
  for (const indexEntry of serverConfig.autoIndex) {
212
284
  let assetKeyIndex = assetNameAsDir + indexEntry;
213
- const asset = assetEntryMap[assetKeyIndex];
285
+ const asset = await this.findAssetEntry(assetKeyIndex);
214
286
  if (asset != null) {
215
287
  return asset;
216
288
  }
@@ -305,7 +377,10 @@ export class PublisherServer {
305
377
  const baseHash = entry.key.slice(7);
306
378
  const baseKey = `${this.publishId}_files_sha256_${baseHash}`;
307
379
  const variantKey = variant != null ? `${baseKey}_${variant}` : baseKey;
308
- const storageEntry = await this.timed('asset', () => this.storageProvider.getEntry(variantKey));
380
+ // The key of a file has the hash of its content, so a cached copy is never
381
+ // out of date. A publish does not purge the tag; purge it to remove the
382
+ // cached copies of all files, for example after a bad upload.
383
+ const storageEntry = await this.timed('asset', () => this.storageProvider.getEntry(variantKey, [`${this.publishId}-files`], { ttl: FILE_CACHE_TTL }));
309
384
  if (storageEntry == null) {
310
385
  return null;
311
386
  }
@@ -560,7 +635,7 @@ export class PublisherServer {
560
635
  // that expired, gets a 404. It can be published at any time, so the
561
636
  // response cache does not store this response.
562
637
  if (serverConfig == null ||
563
- (this.activeCollectionName !== this.defaultCollectionName && await this.getAssetEntryMap() == null)) {
638
+ (this.activeCollectionName !== this.defaultCollectionName && await this.getIndex() == null)) {
564
639
  this.doNotStoreResponse = true;
565
640
  return new Response(`Collection '${this.activeCollectionName}' is not available. It is not published, or it expired.`, {
566
641
  status: 404,
@@ -579,14 +654,13 @@ export class PublisherServer {
579
654
  }
580
655
  // fallback HTML responses, like SPA and "not found" pages
581
656
  if (requestAcceptsTextHtml(request)) {
582
- const assetEntryMap = await this.getAssetEntryMap();
583
- if (assetEntryMap == null) {
657
+ if (await this.getIndex() == null) {
584
658
  return null;
585
659
  }
586
660
  // These are raw asset paths, not relative to public path
587
661
  const { spaFile } = serverConfig;
588
662
  if (spaFile != null) {
589
- const asset = assetEntryMap[spaFile];
663
+ const asset = await this.findAssetEntry(spaFile);
590
664
  if (asset != null) {
591
665
  return this.serveAsset(request, asset, {
592
666
  cache: 'never',
@@ -595,7 +669,7 @@ export class PublisherServer {
595
669
  }
596
670
  const { notFoundPageFile } = serverConfig;
597
671
  if (notFoundPageFile != null) {
598
- const asset = assetEntryMap[notFoundPageFile];
672
+ const asset = await this.findAssetEntry(notFoundPageFile);
599
673
  if (asset != null) {
600
674
  return this.serveAsset(request, asset, {
601
675
  status: 404,
@@ -1 +1 @@
1
- {"version":3,"file":"kv-store-provider.d.ts","sourceRoot":"","sources":["../../../../src/server/storage/kv-store-provider.ts"],"names":[],"mappings":"AAUA,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,sBAAsB,EAC5B,MAAM,uBAAuB,CAAC;AAK/B,eAAO,MAAM,kBAAkB,EAAE,sBAMhC,CAAC;AAEF,qBAAa,eAAgB,YAAW,eAAe;gBACzC,WAAW,EAAE,MAAM;IAI/B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAEvB,QAAQ,CAAC,GAAG,EAAE,MAAM;CAI3B"}
1
+ {"version":3,"file":"kv-store-provider.d.ts","sourceRoot":"","sources":["../../../../src/server/storage/kv-store-provider.ts"],"names":[],"mappings":"AAUA,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,sBAAsB,EAC5B,MAAM,uBAAuB,CAAC;AAK/B,eAAO,MAAM,kBAAkB,EAAE,sBAMhC,CAAC;AAEF,qBAAa,eAAgB,YAAW,eAAe;gBACzC,WAAW,EAAE,MAAM;IAI/B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAGvB,QAAQ,CAAC,GAAG,EAAE,MAAM;CAI3B"}
@@ -17,6 +17,7 @@ export class KvStoreProvider {
17
17
  constructor(kvStoreName) {
18
18
  this.kvStoreName = kvStoreName;
19
19
  }
20
+ // The KV Store has its own cache, so the tags and the TTL are not used.
20
21
  async getEntry(key) {
21
22
  const kvStore = new KVStore(this.kvStoreName);
22
23
  return await getKVStoreEntry(kvStore, key);
@@ -1,4 +1,4 @@
1
- import { type StorageEntry, type StorageProvider, StorageProviderBuilder } from './storage-provider.js';
1
+ import { type GetEntryOptions, type StorageEntry, type StorageProvider, StorageProviderBuilder } from './storage-provider.js';
2
2
  export type S3Credentials = {
3
3
  accessKeyId: string;
4
4
  secretAccessKey: string;
@@ -27,6 +27,6 @@ export declare class S3StorageProvider implements StorageProvider {
27
27
  private readonly s3FastlyBackendName?;
28
28
  objectUrl(key: string): URL;
29
29
  fetchObject(key: string, requestInit: RequestInit): Promise<Response>;
30
- getEntry(key: string, tags?: string[]): Promise<StorageEntry | null>;
30
+ getEntry(key: string, tags?: string[], options?: GetEntryOptions): Promise<StorageEntry | null>;
31
31
  }
32
32
  //# sourceMappingURL=s3-storage-provider.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"s3-storage-provider.d.ts","sourceRoot":"","sources":["../../../../src/server/storage/s3-storage-provider.ts"],"names":[],"mappings":"AAiBA,OAAO,EAEL,KAAK,YAAY,EAEjB,KAAK,eAAe,EACpB,sBAAsB,EACvB,MAAM,uBAAuB,CAAC;AAE/B,MAAM,MAAM,aAAa,GAAG;IAC1B,WAAW,EAAE,MAAM,CAAC;IACpB,eAAe,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG,MAAM,CAAC,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC;AAElF,eAAO,MAAM,kBAAkB,EAAE,sBAahC,CAAC;AAMF;;;;GAIG;AACH,wBAAgB,8BA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:cache-override';
6
+ import { purgeSurrogateKey } from 'fastly:compute';
7
+ import { env } from 'fastly:env';
6
8
  import { SecretStore } from 'fastly:secret-store';
7
9
  import { Sha256 } from '@aws-crypto/sha256-js';
8
10
  import { SignatureV4 } from '@smithy/signature-v4';
@@ -68,6 +70,9 @@ let _s3CredentialsBuilder = buildS3CredentialsFromSecretStore;
68
70
  export function setS3CredentialsBuilder(s3CredentialsBuilder) {
69
71
  _s3CredentialsBuilder = s3CredentialsBuilder;
70
72
  }
73
+ // The time, in seconds, that the readthrough cache keeps an object when
74
+ // getEntry() does not give a TTL (one hour).
75
+ const DEFAULT_CACHE_TTL = 3600;
71
76
  // Attempts for a request that fails with a retryable status or a network error.
72
77
  const MAX_ATTEMPTS = 5;
73
78
  const RETRY_BASE_DELAY_MS = 100;
@@ -153,16 +158,32 @@ export class S3StorageProvider {
153
158
  await new Promise(resolve => setTimeout(resolve, delay));
154
159
  }
155
160
  }
156
- async getEntry(key, tags) {
161
+ async getEntry(key, tags, options) {
162
+ const ttl = options?.ttl ?? DEFAULT_CACHE_TTL;
163
+ // A read with a TTL longer than DEFAULT_CACHE_TTL also gets a surrogate key
164
+ // for this object in this POP. If the object does not exist, the server
165
+ // purges this key, so that the cache does not keep the 404 for the long
166
+ // TTL. The key has the POP name, so the purge removes only the copy in
167
+ // this POP.
168
+ const popSurrogateKey = ttl > DEFAULT_CACHE_TTL ? `pop-${env('FASTLY_POP')}-${key}` : null;
169
+ const surrogateKeys = [...(tags ?? []), ...(popSurrogateKey != null ? [popSurrogateKey] : [])];
157
170
  const response = await this.fetchObject(key, {
158
171
  backend: this.s3FastlyBackendName ?? "s3_storage",
159
172
  cacheOverride: new CacheOverride({
160
- ttl: 3600,
161
- surrogateKey: (tags ?? []).join(' ') || undefined,
173
+ ttl,
174
+ surrogateKey: surrogateKeys.join(' ') || undefined,
162
175
  }),
163
176
  });
164
177
  if (response.status === 404) {
165
178
  console.log("Object does not exist");
179
+ if (popSurrogateKey != null) {
180
+ try {
181
+ purgeSurrogateKey(popSurrogateKey);
182
+ }
183
+ catch (err) {
184
+ console.warn(`Could not purge the 404 of ${key} from the cache.`, err);
185
+ }
186
+ }
166
187
  return null;
167
188
  }
168
189
  if (!response.ok) {
@@ -1,6 +1,17 @@
1
1
  import { type StaticPublishRc } from '../../models/config/static-publish-rc.js';
2
+ export type GetEntryOptions = {
3
+ /**
4
+ * How long, in seconds, a provider that caches what it reads (such as the
5
+ * S3 provider) keeps the entry. A provider that does not cache ignores it.
6
+ */
7
+ ttl?: number;
8
+ };
2
9
  export interface StorageProvider {
3
- getEntry(key: string, tags?: string[]): Promise<StorageEntry | null>;
10
+ /**
11
+ * Reads an entry. `tags` are the surrogate keys of the cached copy, if the
12
+ * provider caches what it reads.
13
+ */
14
+ getEntry(key: string, tags?: string[], options?: GetEntryOptions): Promise<StorageEntry | null>;
4
15
  }
5
16
  export type StorageProviderBuilder = (config: StaticPublishRc) => (StorageProvider | null);
6
17
  export interface StorageEntry {
@@ -1 +1 @@
1
- {"version":3,"file":"storage-provider.d.ts","sourceRoot":"","sources":["../../../../src/server/storage/storage-provider.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,KAAK,eAAe,EACrB,MAAM,0CAA0C,CAAC;AAElD,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC,CAAC;CACtE;AAED,MAAM,MAAM,sBAAsB,GAAG,CAAC,MAAM,EAAE,eAAe,KAAK,CAAC,eAAe,GAAG,IAAI,CAAC,CAAC;AAE3F,MAAM,WAAW,YAAY;IAC3B;;OAEG;IACH,IAAI,IAAI,IAAI,cAAc,CAAC;IAE3B;;OAEG;IACH,IAAI,QAAQ,IAAI,OAAO,CAAC;IAExB;;;OAGG;IACH,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAExB;;OAEG;IACH,IAAI,IAAI,OAAO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3B;;OAEG;IACH,IAAI,IAAI,IAAI,cAAc,CAAC;IAE3B;;OAEG;IACH,IAAI,QAAQ,IAAI,OAAO,CAAC;IAExB;;;OAGG;IACH,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAExB;;OAEG;IACH,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAExB;;OAEG;IACH,WAAW,IAAI,OAAO,CAAC,WAAW,CAAC,CAAC;IAEpC;;OAEG;IACH,QAAQ,IAAI,WAAW,GAAG,IAAI,CAAC;IAE/B;;;OAGG;IACH,YAAY,IAAI,MAAM,GAAG,IAAI,CAAC;CAC/B;AAKD,qBAAa,gBAAiB,SAAQ,QAAS,YAAW,YAAY;gBACxD,IAAI,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM;IAKhD,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAS;IAE3C,IAAI,IAAI,4CAEP;IAED,QAAQ,IAAI,WAAW,GAAG,IAAI;IAG9B,YAAY,IAAI,MAAM,GAAG,IAAI;CAG9B;AAED,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,cAAc,CAAC,UAAU,CAAC,EAAE,GAAG,cAAc,CAAC,UAAU,CAAC,CAuCvG;AAGD,wBAAgB,8BAA8B,CAAC,OAAO,EAAE,sBAAsB,QAE7E;AAED,wBAAgB,sCAAsC,CAAC,MAAM,EAAE,eAAe,mBAS7E"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@fastly/compute-js-static-publish",
3
3
  "type": "module",
4
- "version": "8.0.2",
4
+ "version": "8.0.3-rc.2",
5
5
  "description": "Static Publisher for Fastly Compute JavaScript",
6
6
  "main": "build/server/server/index.js",
7
7
  "exports": {