@fastly/compute-js-static-publish 7.0.0-beta.1 → 7.0.0-beta.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 (68) hide show
  1. package/build/cli/commands/manage/clean.d.ts +2 -0
  2. package/build/cli/commands/manage/clean.d.ts.map +1 -0
  3. package/build/cli/commands/manage/clean.js +201 -0
  4. package/build/cli/commands/manage/collections/delete.d.ts +2 -0
  5. package/build/cli/commands/manage/collections/delete.d.ts.map +1 -0
  6. package/build/cli/commands/{collections → manage/collections}/delete.js +56 -25
  7. package/build/cli/commands/manage/collections/index.d.ts.map +1 -0
  8. package/build/cli/commands/{collections → manage/collections}/index.js +27 -22
  9. package/build/cli/commands/manage/collections/list.d.ts +2 -0
  10. package/build/cli/commands/manage/collections/list.d.ts.map +1 -0
  11. package/build/cli/commands/manage/collections/list.js +133 -0
  12. package/build/cli/commands/manage/collections/promote.d.ts +2 -0
  13. package/build/cli/commands/manage/collections/promote.d.ts.map +1 -0
  14. package/build/cli/commands/manage/collections/promote.js +153 -0
  15. package/build/cli/commands/manage/collections/update-expiration.d.ts +2 -0
  16. package/build/cli/commands/manage/collections/update-expiration.d.ts.map +1 -0
  17. package/build/cli/commands/manage/collections/update-expiration.js +129 -0
  18. package/build/cli/commands/manage/index.d.ts.map +1 -0
  19. package/build/cli/commands/{index.js → manage/index.js} +26 -29
  20. package/build/cli/commands/manage/publish-content.d.ts +2 -0
  21. package/build/cli/commands/manage/publish-content.d.ts.map +1 -0
  22. package/build/cli/commands/{publish-content.js → manage/publish-content.js} +142 -56
  23. package/build/cli/commands/{init-app.d.ts → scaffold/index.d.ts} +2 -2
  24. package/build/cli/commands/scaffold/index.d.ts.map +1 -0
  25. package/build/cli/commands/{init-app.js → scaffold/index.js} +70 -22
  26. package/build/cli/fastly-api/api-token.d.ts.map +1 -1
  27. package/build/cli/fastly-api/api-token.js +7 -1
  28. package/build/cli/fastly-api/kv-store.d.ts +2 -2
  29. package/build/cli/fastly-api/kv-store.d.ts.map +1 -1
  30. package/build/cli/fastly-api/kv-store.js +2 -2
  31. package/build/cli/index.js +11 -3
  32. package/build/cli/util/args.d.ts +16 -2
  33. package/build/cli/util/args.d.ts.map +1 -1
  34. package/build/cli/util/args.js +32 -6
  35. package/build/cli/util/fastly-toml.d.ts +2 -0
  36. package/build/cli/util/fastly-toml.d.ts.map +1 -0
  37. package/build/cli/util/fastly-toml.js +15 -0
  38. package/build/cli/util/kv-store-items.d.ts +3 -2
  39. package/build/cli/util/kv-store-items.d.ts.map +1 -1
  40. package/build/cli/util/kv-store-items.js +6 -12
  41. package/build/cli/util/node.d.ts +2 -0
  42. package/build/cli/util/node.d.ts.map +1 -0
  43. package/build/cli/util/node.js +10 -0
  44. package/build/models/server/index.d.ts +5 -0
  45. package/build/models/server/index.d.ts.map +1 -0
  46. package/build/models/server/index.js +1 -0
  47. package/build/models/time/index.d.ts +7 -0
  48. package/build/models/time/index.d.ts.map +1 -0
  49. package/build/models/time/index.js +51 -0
  50. package/build/server/publisher-server/index.d.ts +2 -0
  51. package/build/server/publisher-server/index.d.ts.map +1 -1
  52. package/build/server/publisher-server/index.js +34 -3
  53. package/package.json +3 -2
  54. package/build/cli/commands/clean.d.ts +0 -2
  55. package/build/cli/commands/clean.d.ts.map +0 -1
  56. package/build/cli/commands/clean.js +0 -121
  57. package/build/cli/commands/collections/delete.d.ts +0 -2
  58. package/build/cli/commands/collections/delete.d.ts.map +0 -1
  59. package/build/cli/commands/collections/index.d.ts.map +0 -1
  60. package/build/cli/commands/collections/list.d.ts +0 -2
  61. package/build/cli/commands/collections/list.d.ts.map +0 -1
  62. package/build/cli/commands/collections/list.js +0 -62
  63. package/build/cli/commands/index.d.ts.map +0 -1
  64. package/build/cli/commands/init-app.d.ts.map +0 -1
  65. package/build/cli/commands/publish-content.d.ts +0 -2
  66. package/build/cli/commands/publish-content.d.ts.map +0 -1
  67. /package/build/cli/commands/{collections → manage/collections}/index.d.ts +0 -0
  68. /package/build/cli/commands/{index.d.ts → manage/index.d.ts} +0 -0
@@ -0,0 +1,2 @@
1
+ export declare function action(actionArgs: string[]): Promise<void>;
2
+ //# sourceMappingURL=clean.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clean.d.ts","sourceRoot":"","sources":["../../../../src/cli/commands/manage/clean.ts"],"names":[],"mappings":"AAsCA,wBAAsB,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,iBAuNhD"}
@@ -0,0 +1,201 @@
1
+ /*
2
+ * Copyright Fastly, Inc.
3
+ * Licensed under the MIT license. See LICENSE file for details.
4
+ */
5
+ import { isExpired } from '../../../models/time/index.js';
6
+ import { LoadConfigError, loadStaticPublisherRcFile } from '../../util/config.js';
7
+ import { getKvStoreEntry, getKVStoreKeys, kvStoreDeleteEntry } from '../../fastly-api/kv-store.js';
8
+ import { loadApiToken } from '../../fastly-api/api-token.js';
9
+ import { parseCommandLine } from '../../util/args.js';
10
+ import { doKvStoreItemsOperation } from "../../util/kv-store-items.js";
11
+ function help() {
12
+ console.log(`\
13
+
14
+ Usage:
15
+ npx @fastly/compute-js-static-publish clean [options]
16
+
17
+ Description:
18
+ Cleans up expired or unreferenced items in the Fastly KV Store.
19
+ This includes expired collection indexes and orphaned content assets.
20
+
21
+ Options:
22
+ --delete-expired-collections If set, expired collection index files will be deleted.
23
+ --dry-run Show what would be deleted without performing any deletions.
24
+
25
+ --fastly-api-token <token> Fastly API token used for KV Store access. If not provided,
26
+ the tool will try:
27
+ 1. FASTLY_API_TOKEN environment variable
28
+ 2. fastly profile token (via CLI)
29
+ -h, --help Show help for this command.
30
+ `);
31
+ }
32
+ export async function action(actionArgs) {
33
+ const optionDefinitions = [
34
+ { name: 'verbose', type: Boolean },
35
+ { name: 'delete-expired-collections', type: Boolean },
36
+ { name: 'dry-run', type: Boolean },
37
+ { name: 'fastly-api-token', type: String, },
38
+ ];
39
+ const parsed = parseCommandLine(actionArgs, optionDefinitions);
40
+ if (parsed.needHelp) {
41
+ if (parsed.error != null) {
42
+ console.error(String(parsed.error));
43
+ console.error();
44
+ process.exitCode = 1;
45
+ }
46
+ help();
47
+ return;
48
+ }
49
+ const { verbose, ['delete-expired-collections']: deleteExpiredCollections, ['dry-run']: dryRun, ['fastly-api-token']: fastlyApiToken, } = parsed.commandLineOptions;
50
+ const apiTokenResult = loadApiToken({ commandLine: fastlyApiToken });
51
+ if (apiTokenResult == null) {
52
+ console.error("❌ Fastly API Token not provided.");
53
+ console.error("Set the FASTLY_API_TOKEN environment variable to an API token that has write access to the KV Store.");
54
+ process.exitCode = 1;
55
+ return;
56
+ }
57
+ const fastlyApiContext = { apiToken: apiTokenResult.apiToken };
58
+ console.log(`✔️ Fastly API Token: ${fastlyApiContext.apiToken.slice(0, 4)}${'*'.repeat(fastlyApiContext.apiToken.length - 4)} from '${apiTokenResult.source}'`);
59
+ // #### load config
60
+ let staticPublisherRc;
61
+ try {
62
+ staticPublisherRc = await loadStaticPublisherRcFile();
63
+ }
64
+ catch (err) {
65
+ console.error("❌ Can't load static-publish.rc.js");
66
+ console.error("Run this from a compute-js-static-publish compute-js directory.");
67
+ if (err instanceof LoadConfigError) {
68
+ for (const error of err.errors) {
69
+ console.error(error);
70
+ }
71
+ }
72
+ process.exitCode = 1;
73
+ return;
74
+ }
75
+ const publishId = staticPublisherRc.publishId;
76
+ console.log(` | Publish ID: ${publishId}`);
77
+ const kvStoreName = staticPublisherRc.kvStoreName;
78
+ console.log(` | Using KV Store: ${kvStoreName}`);
79
+ const defaultCollectionName = staticPublisherRc.defaultCollectionName;
80
+ console.log(` | Default Collection Name: ${defaultCollectionName}`);
81
+ // ### KVStore Keys to delete
82
+ const kvKeysToDelete = new Set();
83
+ // ### List all indexes ###
84
+ const indexesPrefix = publishId + '_index_';
85
+ const indexKeys = await getKVStoreKeys(fastlyApiContext, kvStoreName, indexesPrefix);
86
+ if (indexKeys == null) {
87
+ throw new Error(`Can't query indexes in KV Store`);
88
+ }
89
+ // ### Go through the index files, make lists
90
+ const foundCollections = indexKeys.map(x => x.slice(indexesPrefix.length));
91
+ // All collection names that we are keeping
92
+ const liveCollections = new Set();
93
+ // All asset keys (hashes) that we are keeping
94
+ const assetsIdsInUse = new Set();
95
+ console.log('Processing collections:');
96
+ await doKvStoreItemsOperation(foundCollections.map(collection => ({
97
+ key: indexesPrefix + collection,
98
+ collection,
99
+ })), async ({ collection }, indexKey) => {
100
+ console.log(`Collection: ${collection}`);
101
+ // TODO deal with when the index file is > 20MB
102
+ const kvAssetsIndexResponse = await getKvStoreEntry(fastlyApiContext, kvStoreName, indexKey);
103
+ if (!kvAssetsIndexResponse) {
104
+ throw new Error(`Can't load KV Store entry ${indexesPrefix + collection}`);
105
+ }
106
+ let indexMetadata = {};
107
+ if (kvAssetsIndexResponse.metadata != null) {
108
+ try {
109
+ indexMetadata = JSON.parse(kvAssetsIndexResponse.metadata);
110
+ }
111
+ catch {
112
+ }
113
+ }
114
+ let isLive = true;
115
+ if (indexMetadata.expirationTime == null) {
116
+ console.log(' Collection has no expiration time set');
117
+ }
118
+ else {
119
+ console.log(' ⏰ Expiration: ' + new Date(indexMetadata.expirationTime * 1000));
120
+ if (collection === defaultCollectionName) {
121
+ console.log(` ✅ Expiration time not enforced for default collection.`);
122
+ }
123
+ else if (isExpired(indexMetadata.expirationTime)) {
124
+ if (!deleteExpiredCollections) {
125
+ console.log(` ⚠️ Use --delete-expired-collections to delete expired collections.`);
126
+ }
127
+ else {
128
+ console.log(` 🗑️ Marking expired collection for deletion.`);
129
+ kvKeysToDelete.add(indexKey);
130
+ isLive = false;
131
+ }
132
+ }
133
+ }
134
+ if (!isLive) {
135
+ return;
136
+ }
137
+ liveCollections.add(collection);
138
+ const kvAssetsIndex = (await kvAssetsIndexResponse.response.json());
139
+ for (const [_assetKey, assetEntry] of Object.entries(kvAssetsIndex)) {
140
+ if (assetEntry.key.startsWith('sha256:')) {
141
+ assetsIdsInUse.add(`sha256_${assetEntry.key.slice('sha256:'.length)}`);
142
+ }
143
+ }
144
+ });
145
+ console.log('');
146
+ // ### List all settings ###
147
+ console.log('Enumerating settings:');
148
+ const settingsPrefix = publishId + '_settings_';
149
+ const settingsKeys = await getKVStoreKeys(fastlyApiContext, kvStoreName, settingsPrefix);
150
+ if (settingsKeys == null) {
151
+ throw new Error(`Can't query settings in KV Store`);
152
+ }
153
+ // If a settings object is found that doesn't match a live collection name then
154
+ // mark it for deletion
155
+ const foundSettingsCollections = settingsKeys.map(key => ({ key, name: key.slice(settingsPrefix.length) }));
156
+ for (const foundSettings of foundSettingsCollections) {
157
+ console.log(`Settings: ${foundSettings.name}`);
158
+ if (!liveCollections.has(foundSettings.name)) {
159
+ console.log(` 🗑️ Does not match a live index, marking for deletion.`);
160
+ kvKeysToDelete.add(foundSettings.key);
161
+ }
162
+ }
163
+ console.log('');
164
+ // ### Obtain the assets in the KV Store and find the ones that are not in use
165
+ console.log('Enumerating assets:');
166
+ const assetPrefix = publishId + '_files_';
167
+ const assetKeys = await getKVStoreKeys(fastlyApiContext, kvStoreName, assetPrefix);
168
+ if (assetKeys == null) {
169
+ throw new Error(`Can't query assets in KV Store`);
170
+ }
171
+ for (const assetKey of assetKeys) {
172
+ let assetId = assetKey.slice(assetPrefix.length);
173
+ if (assetId.startsWith('sha256_')) {
174
+ assetId = assetId.slice(0, 'sha256_'.length + 64);
175
+ }
176
+ else {
177
+ // If we don't know what the prefix is, we ignore it
178
+ continue;
179
+ }
180
+ if (assetsIdsInUse.has(assetId)) {
181
+ console.log(` ${assetId}: in use`);
182
+ }
183
+ else {
184
+ kvKeysToDelete.add(assetKey);
185
+ console.log(` ${assetId}: not in use - ✅ marking for deletion`);
186
+ }
187
+ }
188
+ console.log('');
189
+ // ### Delete items that have been flagged
190
+ const items = [...kvKeysToDelete].map(key => ({ key }));
191
+ await doKvStoreItemsOperation(items, async (_, key) => {
192
+ if (dryRun) {
193
+ console.log(`[DRY RUN] Deleting item: ${key}`);
194
+ }
195
+ else {
196
+ console.log(`Deleting item from KV Store: ${key}`);
197
+ await kvStoreDeleteEntry(fastlyApiContext, kvStoreName, key);
198
+ }
199
+ });
200
+ console.log('✅ Completed.');
201
+ }
@@ -0,0 +1,2 @@
1
+ export declare function action(actionArgs: string[]): Promise<void>;
2
+ //# sourceMappingURL=delete.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"delete.d.ts","sourceRoot":"","sources":["../../../../../src/cli/commands/manage/collections/delete.ts"],"names":[],"mappings":"AAmCA,wBAAsB,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,iBAkHhD"}
@@ -2,25 +2,50 @@
2
2
  * Copyright Fastly, Inc.
3
3
  * Licensed under the MIT license. See LICENSE file for details.
4
4
  */
5
- import commandLineArgs from 'command-line-args';
6
- import { LoadConfigError, loadStaticPublisherRcFile } from '../../util/config.js';
7
- import { getKVStoreKeys, kvStoreDeleteFile } from '../../fastly-api/kv-store.js';
8
- import { loadApiToken } from '../../fastly-api/api-token.js';
9
- export async function action(argv) {
5
+ import { LoadConfigError, loadStaticPublisherRcFile } from '../../../util/config.js';
6
+ import { getKVStoreKeys, kvStoreDeleteEntry } from '../../../fastly-api/kv-store.js';
7
+ import { loadApiToken } from '../../../fastly-api/api-token.js';
8
+ import { parseCommandLine } from "../../../util/args.js";
9
+ import { doKvStoreItemsOperation } from "../../../util/kv-store-items.js";
10
+ function help() {
11
+ console.log(`\
12
+
13
+ Usage:
14
+ npx @fastly/compute-js-static-publish collections delete \\
15
+ --collection-name <name> \\
16
+ [options]
17
+
18
+ Description:
19
+ Deletes a collection index from the KV Store. The content files will remain but will no longer be referenced.
20
+
21
+ Options:
22
+ --collection-name <name> (Required) The name of the collection to delete
23
+
24
+ --fastly-api-token <token> Fastly API token used for KV Store access. If not provided,
25
+ the tool will try:
26
+ 1. FASTLY_API_TOKEN environment variable
27
+ 2. fastly profile token (via CLI)
28
+ -h, --help Show help for this command.
29
+ `);
30
+ }
31
+ export async function action(actionArgs) {
10
32
  const optionDefinitions = [
11
33
  { name: 'verbose', type: Boolean },
34
+ { name: 'collection-name', type: String },
12
35
  { name: 'fastly-api-token', type: String, },
13
- { name: 'collection-name', type: String, multiple: true }
14
36
  ];
15
- const commandLineValues = commandLineArgs(optionDefinitions, { argv });
16
- const { verbose, ['fastly-api-token']: fastlyApiToken, ['collection-name']: collectionNameValue, } = commandLineValues;
17
- const collectionNamesAsArray = (Array.isArray(collectionNameValue) ? collectionNameValue : [collectionNameValue])
18
- .filter(x => typeof x === 'string')
19
- .map(x => x.split(','))
20
- .flat()
21
- .map(x => x.trim())
22
- .filter(Boolean);
23
- if (collectionNamesAsArray.length === 0) {
37
+ const parsed = parseCommandLine(actionArgs, optionDefinitions);
38
+ if (parsed.needHelp) {
39
+ if (parsed.error != null) {
40
+ console.error(String(parsed.error));
41
+ console.error();
42
+ process.exitCode = 1;
43
+ }
44
+ help();
45
+ return;
46
+ }
47
+ const { verbose, ['fastly-api-token']: fastlyApiToken, ['collection-name']: collectionNameValue, } = parsed.commandLineOptions;
48
+ if (collectionNameValue == null) {
24
49
  console.error("❌ Required argument '--collection-name' not specified.");
25
50
  process.exitCode = 1;
26
51
  return;
@@ -51,13 +76,18 @@ export async function action(argv) {
51
76
  return;
52
77
  }
53
78
  const publishId = staticPublisherRc.publishId;
54
- console.log(`✔️ Publish ID: ${publishId}`);
79
+ console.log(` | Publish ID: ${publishId}`);
55
80
  const kvStoreName = staticPublisherRc.kvStoreName;
56
- console.log(`✔️ Using KV Store: ${kvStoreName}`);
81
+ console.log(` | Using KV Store: ${kvStoreName}`);
57
82
  const defaultCollectionName = staticPublisherRc.defaultCollectionName;
58
- console.log(`✔️ Default Collection Name: ${defaultCollectionName}`);
59
- console.log(`✔️ Collections to delete: ${collectionNamesAsArray.map(x => `'${x}'`).join(', ')}`);
60
- const collectionNames = new Set(collectionNamesAsArray);
83
+ console.log(` | Default Collection Name: ${defaultCollectionName}`);
84
+ const collectionName = collectionNameValue;
85
+ if (collectionName === defaultCollectionName) {
86
+ console.error(`❌ Cannot delete default collection: ${collectionName}`);
87
+ process.exitCode = 1;
88
+ return;
89
+ }
90
+ console.log(`✔️ Collection to delete: ${collectionName}`);
61
91
  const kvKeysToDelete = new Set();
62
92
  // ### List all indexes ###
63
93
  const indexesPrefix = publishId + '_index_';
@@ -73,16 +103,17 @@ export async function action(argv) {
73
103
  else {
74
104
  console.log(`Found collections: ${foundCollections.map(x => `'${x.name}'`).join(', ')}`);
75
105
  for (const collection of foundCollections) {
76
- if (collectionNames.has(collection.name)) {
106
+ if (collection.name === collectionName) {
77
107
  console.log(`Flagging collection '${collection.name}' for deletion: ${collection.key}`);
78
108
  kvKeysToDelete.add(collection.key);
79
109
  }
80
110
  }
81
111
  }
82
112
  // ### Delete items that have been flagged
83
- for (const key of kvKeysToDelete) {
84
- console.log("Deleting key from KV Store: " + key);
85
- await kvStoreDeleteFile(fastlyApiContext, kvStoreName, key);
86
- }
113
+ const items = [...kvKeysToDelete].map(key => ({ key }));
114
+ await doKvStoreItemsOperation(items, async (_, key) => {
115
+ console.log(`Deleting key from KV Store: ${key}`);
116
+ await kvStoreDeleteEntry(fastlyApiContext, kvStoreName, key);
117
+ });
87
118
  console.log("✅ Completed.");
88
119
  }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/cli/commands/manage/collections/index.ts"],"names":[],"mappings":"AAuCA,wBAAsB,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,iBA0BhD"}
@@ -2,28 +2,11 @@
2
2
  * Copyright Fastly, Inc.
3
3
  * Licensed under the MIT license. See LICENSE file for details.
4
4
  */
5
- import { getCommandAndArgs } from '../../util/args.js';
6
- import * as collectionsDelete from './delete.js';
7
- import * as collectionsList from './list.js';
8
- export async function action(actionArgs) {
9
- const modes = {
10
- 'delete': collectionsDelete,
11
- 'list': collectionsList,
12
- };
13
- const commandAndArgs = getCommandAndArgs(actionArgs, Object.keys(modes));
14
- if (commandAndArgs.needHelp) {
15
- if (commandAndArgs.command != null) {
16
- console.error(`Unknown subcommand: ${commandAndArgs.command}`);
17
- console.error(`Specify one of: ${Object.keys(modes).join(', ')}`);
18
- console.error();
19
- process.exitCode = 1;
20
- }
21
- help();
22
- return;
23
- }
24
- const { command, argv, } = commandAndArgs;
25
- await modes[command].action(argv);
26
- }
5
+ import { getCommandAndArgs } from '../../../util/args.js';
6
+ import * as deleteCommand from './delete.js';
7
+ import * as listCommand from './list.js';
8
+ import * as promoteCommand from './promote.js';
9
+ import * as updateExpirationCommand from './update-expiration.js';
27
10
  function help() {
28
11
  console.log(`\
29
12
 
@@ -36,6 +19,7 @@ Description:
36
19
  Available Subcommands:
37
20
  list List all published collections
38
21
  delete Delete a specific collection index
22
+ promote Copies an existing collection (content + config) to a new collection name
39
23
  update-expiration Modify expiration time for an existing collection
40
24
 
41
25
  Global Options:
@@ -50,3 +34,24 @@ Examples:
50
34
  npx @fastly/compute-js-static-publish collections delete --collection-name=preview-42
51
35
  `);
52
36
  }
37
+ export async function action(actionArgs) {
38
+ const modes = {
39
+ 'delete': deleteCommand,
40
+ 'list': listCommand,
41
+ 'promote': promoteCommand,
42
+ 'update-expiration': updateExpirationCommand,
43
+ };
44
+ const commandAndArgs = getCommandAndArgs(actionArgs, modes);
45
+ if (commandAndArgs.needHelp) {
46
+ if (commandAndArgs.command != null) {
47
+ console.error(`Unknown subcommand: ${commandAndArgs.command}`);
48
+ console.error(`Specify one of: ${Object.keys(modes).join(', ')}`);
49
+ console.error();
50
+ process.exitCode = 1;
51
+ }
52
+ help();
53
+ return;
54
+ }
55
+ const { command, argv, } = commandAndArgs;
56
+ await modes[command].action(argv);
57
+ }
@@ -0,0 +1,2 @@
1
+ export declare function action(actionArgs: string[]): Promise<void>;
2
+ //# sourceMappingURL=list.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"list.d.ts","sourceRoot":"","sources":["../../../../../src/cli/commands/manage/collections/list.ts"],"names":[],"mappings":"AAgCA,wBAAsB,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,iBA2HhD"}
@@ -0,0 +1,133 @@
1
+ /*
2
+ * Copyright Fastly, Inc.
3
+ * Licensed under the MIT license. See LICENSE file for details.
4
+ */
5
+ import { LoadConfigError, loadStaticPublisherRcFile } from '../../../util/config.js';
6
+ import { getKvStoreEntry, getKVStoreKeys } from '../../../fastly-api/kv-store.js';
7
+ import { loadApiToken } from '../../../fastly-api/api-token.js';
8
+ import { parseCommandLine } from '../../../util/args.js';
9
+ import { isExpired } from "../../../../models/time/index.js";
10
+ function help() {
11
+ console.log(`\
12
+
13
+ Usage:
14
+ npx @fastly/compute-js-static-publish collections list [options]
15
+
16
+ Description:
17
+ Lists all collections currently published in the KV Store.
18
+
19
+ Options:
20
+ --fastly-api-token <token> Fastly API token used for KV Store access. If not provided,
21
+ the tool will try:
22
+ 1. FASTLY_API_TOKEN environment variable
23
+ 2. fastly profile token (via CLI)
24
+ -h, --help Show help for this command.
25
+ `);
26
+ }
27
+ export async function action(actionArgs) {
28
+ const optionDefinitions = [
29
+ { name: 'verbose', type: Boolean },
30
+ { name: 'fastly-api-token', type: String, },
31
+ ];
32
+ const parsed = parseCommandLine(actionArgs, optionDefinitions);
33
+ if (parsed.needHelp) {
34
+ if (parsed.error != null) {
35
+ console.error(String(parsed.error));
36
+ console.error();
37
+ process.exitCode = 1;
38
+ }
39
+ help();
40
+ return;
41
+ }
42
+ const { verbose, ['fastly-api-token']: fastlyApiToken, } = parsed.commandLineOptions;
43
+ const apiTokenResult = loadApiToken({ commandLine: fastlyApiToken });
44
+ if (apiTokenResult == null) {
45
+ console.error("❌ Fastly API Token not provided.");
46
+ console.error("Set the FASTLY_API_TOKEN environment variable to an API token that has write access to the KV Store.");
47
+ process.exitCode = 1;
48
+ return;
49
+ }
50
+ const fastlyApiContext = { apiToken: apiTokenResult.apiToken };
51
+ console.log(`✔️ Fastly API Token: ${fastlyApiContext.apiToken.slice(0, 4)}${'*'.repeat(fastlyApiContext.apiToken.length - 4)} from '${apiTokenResult.source}'`);
52
+ // #### load config
53
+ let staticPublisherRc;
54
+ try {
55
+ staticPublisherRc = await loadStaticPublisherRcFile();
56
+ }
57
+ catch (err) {
58
+ console.error("❌ Can't load static-publish.rc.js");
59
+ console.error("Run this from a compute-js-static-publish compute-js directory.");
60
+ if (err instanceof LoadConfigError) {
61
+ for (const error of err.errors) {
62
+ console.error(error);
63
+ }
64
+ }
65
+ process.exitCode = 1;
66
+ return;
67
+ }
68
+ const publishId = staticPublisherRc.publishId;
69
+ console.log(` | Publish ID: ${publishId}`);
70
+ const kvStoreName = staticPublisherRc.kvStoreName;
71
+ console.log(` | Using KV Store: ${kvStoreName}`);
72
+ const defaultCollectionName = staticPublisherRc.defaultCollectionName;
73
+ console.log(` | Default Collection Name: ${defaultCollectionName}`);
74
+ // ### List all indexes ###
75
+ const indexesPrefix = publishId + '_index_';
76
+ const indexKeys = await getKVStoreKeys(fastlyApiContext, kvStoreName, indexesPrefix);
77
+ if (indexKeys == null) {
78
+ throw new Error(`❌ Can't query indexes in KV Store`);
79
+ }
80
+ // ### Found collections ###
81
+ const foundCollections = indexKeys.map(x => x.slice(indexesPrefix.length));
82
+ if (foundCollections.length === 0) {
83
+ console.log('No collections found.');
84
+ }
85
+ else {
86
+ console.log(`Found collections:`);
87
+ for (const collection of foundCollections) {
88
+ if (collection === defaultCollectionName) {
89
+ console.log(` ${collection} *DEFAULT*`);
90
+ }
91
+ else {
92
+ console.log(` ${collection}`);
93
+ }
94
+ const indexKey = indexesPrefix + collection;
95
+ const kvAssetsIndexResponse = await getKvStoreEntry(fastlyApiContext, kvStoreName, indexKey);
96
+ if (!kvAssetsIndexResponse) {
97
+ throw new Error(`❌ Can't load KV Store entry ${indexesPrefix + collection}`);
98
+ }
99
+ let indexMetadata;
100
+ if (kvAssetsIndexResponse.metadata != null) {
101
+ try {
102
+ indexMetadata = JSON.parse(kvAssetsIndexResponse.metadata);
103
+ }
104
+ catch {
105
+ }
106
+ }
107
+ if (indexMetadata == null) {
108
+ console.log(` No metadata found.`);
109
+ continue;
110
+ }
111
+ if (indexMetadata.publishedTime == null) {
112
+ console.log(' Published : unknown');
113
+ }
114
+ else {
115
+ console.log(` Published : ${new Date(indexMetadata.publishedTime * 1000)}`);
116
+ }
117
+ if (indexMetadata.expirationTime == null) {
118
+ console.log(' Expiration : not set');
119
+ }
120
+ else {
121
+ console.log(` Expiration : ${new Date(indexMetadata.expirationTime * 1000)}`);
122
+ if (collection === defaultCollectionName) {
123
+ console.log(` ✅ Expiration time not enforced for default collection.`);
124
+ }
125
+ else if (isExpired(indexMetadata.expirationTime)) {
126
+ console.log(` ⚠️ EXPIRED - Use 'clean --delete-expired-collections' to`);
127
+ console.log(` remove expired collections.`);
128
+ }
129
+ }
130
+ }
131
+ }
132
+ console.log("✅ Completed.");
133
+ }
@@ -0,0 +1,2 @@
1
+ export declare function action(actionArgs: string[]): Promise<void>;
2
+ //# sourceMappingURL=promote.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"promote.d.ts","sourceRoot":"","sources":["../../../../../src/cli/commands/manage/collections/promote.ts"],"names":[],"mappings":"AA2CA,wBAAsB,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,iBA+IhD"}