saaslayer 0.4.2 → 0.5.0

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 (62) hide show
  1. package/bin/saaslayer.js +23 -11
  2. package/package.json +7 -7
  3. package/src/cdn-aws.js +67 -0
  4. package/src/cdn-publication.js +146 -0
  5. package/src/cdn-publisher.js +166 -0
  6. package/src/commands/cdn.js +66 -0
  7. package/src/commands/deploy.js +4 -3
  8. package/src/commands/lifecycle.js +20 -2
  9. package/src/commands/operations.js +8 -3
  10. package/src/commands/stacks.js +2 -0
  11. package/src/commands/users-management.js +2 -2
  12. package/src/deployments.js +5 -2
  13. package/src/module-catalog.js +15 -5
  14. package/src/sms/scope.js +4 -2
  15. package/src/source-branch.js +36 -0
  16. package/src/users-import/ids.js +1 -1
  17. package/templates/saaslayer-foundation.yml +215 -281
  18. package/vendor/config/login-domain.js +213 -0
  19. package/vendor/discovery/index.d.mts +109 -0
  20. package/vendor/discovery/index.mjs +409 -0
  21. package/vendor/ids/index.d.ts +21 -0
  22. package/vendor/ids/index.js +237 -0
  23. package/vendor/migrations/backups.js +118 -0
  24. package/vendor/migrations/child.js +78 -0
  25. package/vendor/migrations/credentials.js +103 -0
  26. package/vendor/migrations/errors.js +30 -0
  27. package/vendor/migrations/exec-child.js +78 -0
  28. package/vendor/migrations/gate.js +51 -0
  29. package/vendor/migrations/index.js +30 -0
  30. package/vendor/migrations/inventory.js +92 -0
  31. package/vendor/migrations/log.js +14 -0
  32. package/vendor/migrations/release.js +45 -0
  33. package/vendor/migrations/report.js +129 -0
  34. package/vendor/migrations/rules.js +104 -0
  35. package/vendor/migrations/run.js +311 -0
  36. package/vendor/migrations/session-policy.js +37 -0
  37. package/vendor/migrations/step-guard.js +82 -0
  38. package/vendor/operations/activity.d.ts +21 -0
  39. package/vendor/operations/activity.js +126 -0
  40. package/vendor/operations/index.d.ts +72 -0
  41. package/vendor/operations/index.js +510 -0
  42. package/vendor/permissions-engine-core/account-status.js +270 -0
  43. package/vendor/permissions-engine-core/delegation.js +174 -0
  44. package/vendor/permissions-engine-core/direct-mode-contract.js +237 -0
  45. package/vendor/permissions-engine-core/sso-claims.js +206 -0
  46. package/vendor/user-management/cognito/emails/token-registry.js +48 -0
  47. package/vendor/user-management/email/validate.js +207 -0
  48. package/vendor/user-management/extensibility/hook-contract.js +720 -0
  49. package/vendor/user-management/extensibility/safe-webhook.js +85 -0
  50. package/vendor/user-management/identity/admission.js +215 -0
  51. package/vendor/user-management/identity/attr.js +27 -0
  52. package/vendor/user-management/identity/attribute-registry.js +115 -0
  53. package/vendor/user-management/identity/email.js +25 -0
  54. package/vendor/user-management/identity/ensure-suid.js +74 -0
  55. package/vendor/user-management/identity/pointers.js +192 -0
  56. package/vendor/user-management/identity/stitch-provider.js +275 -0
  57. package/vendor/user-management/management/apply-request.js +227 -0
  58. package/vendor/user-management/management/core-profile.js +110 -0
  59. package/vendor/user-management/management/index.js +23 -0
  60. package/vendor/user-management/management/purposes.js +94 -0
  61. package/vendor/user-management/management/registry.js +569 -0
  62. package/vendor/user-management/store/table.js +332 -0
package/bin/saaslayer.js CHANGED
@@ -30,6 +30,14 @@ function parseArgs(argv) {
30
30
  const HELP = `saaslayer — SaaSLayer onboarding CLI
31
31
 
32
32
  Usage:
33
+ saaslayer cdn publish <dir> --package NAME --version SEMVER [--deployment ID | --project P --env E --region R]
34
+ [--service cdn-service@INSTANCE] [--profile P] [--dry-run] [--json]
35
+ Publish immutable versioned assets with your own AWS credentials. Requires a live authorized control plane
36
+ and a connected BYOA account with foundation >=8.5.0; use saaslayer foundation upgrade when needed.
37
+ Identical retries verify the original bytes; changed content needs a new version. --dry-run reserves nothing.
38
+ saaslayer cdn withdraw --package NAME --version SEMVER --reason TEXT [same deployment/service selectors] [--json]
39
+ Audited whole-version withdrawal. Waits for the product operation; retained assets and history stay reserved.
40
+
33
41
  saaslayer init --control-plane-account <12-digit> --external-id <id> --control-plane-id <id> [--stack-name NAME]
34
42
  [--region REGION] [--stack-name-prefix sl-] [--org-id ID] [--template PATH] [--dry-run]
35
43
  Create the SaaSLayer foundation stack (3 IAM roles + permissions boundary) in the caller's AWS account, with one
@@ -308,28 +316,29 @@ Deploy & lifecycle (requires login + a connected AWS account):
308
316
  old one once no running operation carries it. rollback (before retire) goes back to the current identifier; after
309
317
  a switch it waits for the operations already using the new one. Each step is signed with your AWS credentials (a
310
318
  connection authorizer of the account) and is safe to rerun. status shows the rotation; identifiers are never shown.
311
- saaslayer sms setup --env E [--region R] [--simulator] [--help-message <text>] [--yes] [--profile P]
319
+ saaslayer sms setup --env E [--region R] [--target ACCT] [--simulator] [--help-message <text>] [--yes] [--profile P]
312
320
  Install the SMS sending number for phone login on the region foundation of the env's account and region (from
313
- the project's deployment there, else its connected account), with your own AWS credentials for that account
314
- (cloud-prem; hosted accounts are not supported yet). Shows the cost (~$2/month) and asks unless --yes. Prints the
321
+ the project's deployment there, else its connected account; --target names it when the project has several),
322
+ with your own AWS credentials for that account (cloud-prem; hosted accounts are not supported yet). Shows the cost
323
+ (~$2/month) and asks unless --yes. Prints the
315
324
  number's ARN and the exact settings command. It cannot send until its registration is approved (AWS review: up to
316
325
  15 business days). --simulator (dev only, refused when the account has staging or prod deployments) installs an
317
326
  AWS simulator number instead: it reaches simulator destinations only, and needs no registration.
318
327
  --help-message sets the number's automatic reply to HELP (one SMS, at most 160 characters, naming a contact and
319
328
  STOP; carriers check it). Run again with a new --help-message to change only the reply.
320
- saaslayer sms register --env <env> [--region R] [--file registration.json] [--profile P]
329
+ saaslayer sms register --env <env> [--region R] [--target ACCT] [--file registration.json] [--profile P]
321
330
  Register the account's toll-free sending number with AWS (cloud-prem, with YOUR AWS credentials for the env's
322
331
  account): asks for (or reads from --file) the company, website, public privacy-policy and terms URLs, contact,
323
332
  opt-in description + PNG screenshot and message samples (naming the brand, with HELP and STOP), checks them,
324
333
  then creates the registration, fills it, associates the number and submits. AWS review: up to 15 business days.
325
334
  Re-running resumes the same registration; after AWS asks for updates it shows the reasons, re-asks only the
326
335
  flagged fields and resubmits. A submitted, reviewing or complete registration is left as it is.
327
- saaslayer sms status --env <env> [--region R] [--json] [--profile P]
336
+ saaslayer sms status --env <env> [--region R] [--target ACCT] [--json] [--profile P]
328
337
  The sending number, registration state, sandbox or production, spend limit and what is still missing (as the
329
338
  deploy's sms.* readiness codes), with the console links and values for the production-access and spend-limit
330
339
  requests (AWS has no API for either). An unfinished sms release is moved forward from the stack's actual state
331
340
  and shown with the command that resumes it.
332
- saaslayer sms release --env E [--region R] [--yes] [--profile P]
341
+ saaslayer sms release --env E [--region R] [--target ACCT] [--yes] [--profile P]
333
342
  Retire the account-and-region SMS number: takes the control plane's SMS lock (deploys that set the number are
334
343
  refused meanwhile), checks that no deployment in any project still uses it, asks, removes the number (billing
335
344
  stops), then deletes its registration. Every step is recorded; re-run it from any machine to resume (sms status
@@ -375,7 +384,7 @@ Deploy & lifecycle (requires login + a connected AWS account):
375
384
  command prints \`scope: org=… project=… env=… (from: …)\` on stderr first (--json omits it); an empty result
376
385
  lists the projects you can see that do have deployments.
377
386
  saaslayer service add <service> [--region R] [--param MODULE.Setting=VALUE] [--settings FILE]
378
- saaslayer service remove <service> --data-custody delete|retain [--region R] [--reason TEXT] [--yes]
387
+ saaslayer service remove <service> --data-custody delete|retain [--region R] [--reason TEXT] [--acknowledge-cdn-outage] [--yes]
379
388
  Add or remove a single service on an existing deployment. A protected env (staging, prod) removal needs --reason
380
389
  and the service name typed to confirm (--yes confirms it).
381
390
  saaslayer update --version X.Y.Z | --channel stable [--env E] [--region R] [--reason TEXT] [--force]
@@ -384,9 +393,10 @@ Deploy & lifecycle (requires login + a connected AWS account):
384
393
  lists them. Protected envs (staging, prod) need --reason; --force updates past the drift gate (audited with the
385
394
  reason). --commit-sha SHA still works until the control plane refuses it.
386
395
  saaslayer update --retry [--env E] [--region R] [--reason TEXT] [--no-wait]
387
- Retry the deployment at its deployed release with its stored settings (nothing is rewritten): resumes an accepted
388
- sign-in method that failed or is not ready yet (\`saaslayer settings get\` shows why and what to fix first). An
389
- accepted method is never turned off. Nothing to resume answers "nothing to retry". Protected envs need --reason.
396
+ Retry the deployment at its deployed release with its stored settings (nothing is rewritten): re-runs a failed or
397
+ blocked update and resumes an accepted sign-in method that failed or is not ready yet (\`saaslayer settings get\`
398
+ shows why and what to fix first). An accepted method is never turned off. Nothing to resume answers "nothing to
399
+ retry"; a failed create (CREATE_FAILED) is destroyed and deployed again instead. Protected envs need --reason.
390
400
  saaslayer migrations approve --env E [--region R] --run <runId> --reason TEXT [--no-wait]
391
401
  Approve the migrations an update left awaiting approval (a protected env's destructive contract migrations, e.g. a
392
402
  rename's retire step): \`saaslayer update\` prints the run id. The control plane records you and the reason.
@@ -430,7 +440,7 @@ Deploy & lifecycle (requires login + a connected AWS account):
430
440
  checked against the release: modules outside or behind the bundle, bundle modules it lacks, the
431
441
  permission-doc format it writes, and its deployed values against environments.<env>.settings.
432
442
  Every settings command prints its scope and the deployment id on stderr (--json omits them).
433
- saaslayer destroy --data-custody delete|retain [--region R] [--reason TEXT] [--yes]
443
+ saaslayer destroy --data-custody delete|retain [--region R] [--reason TEXT] [--acknowledge-cdn-outage] [--yes]
434
444
  saaslayer destroy --purge-retained --reason TEXT [--region R] [--yes]
435
445
  Tear down the project's deployment (typed-name confirmation; --yes confirms it). delete removes its data
436
446
  (irreversible); retain keeps its data and leaves the deployment RETIRED: retained resources remain your
@@ -700,11 +710,13 @@ function cmdInit({ flags }) {
700
710
 
701
711
  async function main() {
702
712
  const argv = process.argv.slice(2)
713
+ if (argv.length === 1 && (argv[0] === '--version' || argv[0] === '-v')) { console.log(require('../package.json').version); return }
703
714
  const { positional, flags } = parseArgs(argv)
704
715
  if (flags.help || positional.length === 0) { console.log(HELP); return }
705
716
  const [cmd, sub] = positional
706
717
  try {
707
718
  if (cmd === 'init') return cmdInit({ flags })
719
+ if (cmd === 'cdn') { await require('../src/commands/cdn').cdnCommand(sub, positional.slice(2), flags); return }
708
720
  if (cmd === 'permissions' && sub === 'inspect') { await require('../src/commands/permissions-inspect').permissionsInspect(positional.slice(2), flags); return }
709
721
  if (cmd === 'permissions' && sub === 'register') { await require('../src/commands/catalog').permissionsRegister(positional.slice(2), flags); return }
710
722
  if (cmd === 'permissions' && sub === 'model' && positional[2] === 'apply') { await require('../src/commands/catalog').catalogApply(positional.slice(3), flags); return }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "saaslayer",
3
- "version": "0.4.2",
4
- "description": "SaaSLayer CLI — onboard an account (init) and register a service's permissions manifest.",
3
+ "version": "0.5.0",
4
+ "description": "SaaSLayer CLI \u2014 onboard an account (init) and register a service's permissions manifest.",
5
5
  "type": "commonjs",
6
6
  "bin": {
7
7
  "saaslayer": "bin/saaslayer.js"
@@ -41,18 +41,18 @@
41
41
  "access": "public"
42
42
  },
43
43
  "scripts": {
44
- "test": "node scripts/sync-template.js --check && node scripts/sync-vendor.js --check && node bin/saaslayer.js --help >/dev/null && node test/credentials.test.js && node test/multi-account.test.js && node test/aws.test.js && node test/api-client-refresh.test.js && node test/update.test.js && node test/versions.test.js && node test/migrations-approve.test.js && node test/service-add.test.js && node test/service-remove.test.js && node test/destroy.test.js && node test/settings.test.js && node test/login-methods.test.js && node test/settings-login-domain.test.js && node test/login-domain.test.js && node test/env-compare.test.js && node test/release-manifest.test.js && node test/operations.test.js && node test/projects.test.js && node test/activity.test.js && node test/integration-credentials.test.js && node test/users-management.test.js && node test/deploy.test.js && node test/module-catalog.test.js && node test/stacks.test.js && node test/catalog.test.js && node test/catalog-registry.test.js && node test/permissions-inspect.test.js && node test/permissions-commands.test.js && node test/iam.test.js && node test/machine-trust.test.js && node test/access-admins.test.js && node test/support-staff.test.js && node test/permission-orgs.test.js && node test/first-admin.test.js && node test/social.test.js && node test/hooks.test.js && node test/verifier-keys.test.js && node test/sso.test.js && node test/service-principals.test.js && node test/package-files.test.js && node test/users-export.test.js && node test/users-import.test.js && node test/users-import-checkpoint.test.js && node test/users-import-profiles.test.js && node test/users.test.js && node test/users-api.test.js && node test/migrate.test.js && node test/foundation-template.test.js && node test/trust-entries.test.js && node test/foundation-home.test.js && node test/foundation.test.js && node test/foundation-change-set.test.js && node test/foundation-trust-write.test.js && node test/connection-proof.test.js && node test/aws-connect.test.js && node test/bootstrap-policy.test.js && node test/aws-rotate-trust.test.js && node test/sms-setup.test.js && node test/sms-release.test.js && node test/scope.test.js && node test/discovery.test.js && node test/credential-profiles.test.js && node test/login-challenges.test.js && node test/login-prompt.test.js && node test/control-plane-url.test.js && node test/signup.test.js && node test/org-members.test.js && node test/sms-register.test.js && node test/sms-status.test.js && echo ok && node test/id-token-auth.test.js && node test/trust-policies.test.js && node test/api-keys.test.js && node test/bindings.test.js && node test/binding-handoff.test.js && node test/env-export.test.js && node test/audit-erase.test.js",
44
+ "test": "node scripts/sync-template.js --check && node scripts/sync-vendor.js --check && node bin/saaslayer.js --help >/dev/null && node test/credentials.test.js && node test/multi-account.test.js && node test/aws.test.js && node test/api-client-refresh.test.js && node test/update.test.js && node test/versions.test.js && node test/migrations-approve.test.js && node test/service-add.test.js && node test/service-remove.test.js && node test/destroy.test.js && node test/settings.test.js && node test/login-methods.test.js && node test/settings-login-domain.test.js && node test/login-domain.test.js && node test/env-compare.test.js && node test/release-manifest.test.js && node test/operations.test.js && node test/projects.test.js && node test/activity.test.js && node test/integration-credentials.test.js && node test/users-management.test.js && node test/deploy.test.js && node test/module-catalog.test.js && node test/stacks.test.js && node test/catalog.test.js && node test/catalog-registry.test.js && node test/permissions-inspect.test.js && node test/permissions-commands.test.js && node test/iam.test.js && node test/machine-trust.test.js && node test/access-admins.test.js && node test/support-staff.test.js && node test/permission-orgs.test.js && node test/first-admin.test.js && node test/social.test.js && node test/hooks.test.js && node test/verifier-keys.test.js && node test/sso.test.js && node test/service-principals.test.js && node test/package-files.test.js && node test/pack-release.test.js && node test/vendor-user-management.test.js && node test/users-export.test.js && node test/users-import.test.js && node test/users-import-checkpoint.test.js && node test/users-import-profiles.test.js && node test/users.test.js && node test/users-api.test.js && node test/migrate.test.js && node test/foundation-template.test.js && node test/trust-entries.test.js && node test/foundation-home.test.js && node test/foundation.test.js && node test/foundation-change-set.test.js && node test/foundation-trust-write.test.js && node test/connection-proof.test.js && node test/aws-connect.test.js && node test/bootstrap-policy.test.js && node test/aws-rotate-trust.test.js && node test/sms-setup.test.js && node test/sms-release.test.js && node test/scope.test.js && node test/discovery.test.js && node test/credential-profiles.test.js && node test/login-challenges.test.js && node test/login-prompt.test.js && node test/control-plane-url.test.js && node test/signup.test.js && node test/org-members.test.js && node test/sms-register.test.js && node test/sms-status.test.js && echo ok && node test/id-token-auth.test.js && node test/trust-policies.test.js && node test/api-keys.test.js && node test/bindings.test.js && node test/binding-handoff.test.js && node test/env-export.test.js && node test/audit-erase.test.js && node --test test/cdn-publication.test.js test/cdn-publisher.test.js test/cdn-aws.test.js test/cdn-command.test.js",
45
45
  "sync-template": "node scripts/sync-template.js",
46
46
  "sync-template:check": "node scripts/sync-template.js --check",
47
47
  "prepublishOnly": "node scripts/sync-template.js",
48
- "release:patch": "npm version patch && npm publish",
49
- "release:minor": "npm version minor && npm publish",
50
- "release:major": "npm version major && npm publish",
51
48
  "test:ddb": "node test/migrate.ddb.test.js",
52
49
  "prepack": "node scripts/sync-vendor.js",
53
- "sync-vendor": "node scripts/sync-vendor.js"
50
+ "sync-vendor": "node scripts/sync-vendor.js",
51
+ "pack:release": "node scripts/pack-release.js"
54
52
  },
55
53
  "dependencies": {
54
+ "@aws-sdk/client-s3": "3.1085.0",
55
+ "@aws-sdk/client-cognito-identity-provider": "^3.1053.0",
56
56
  "@aws-sdk/client-dynamodb": "^3.700.0",
57
57
  "@aws-sdk/client-pinpoint-sms-voice-v2": "^3.1147.0",
58
58
  "@aws-sdk/client-sts": "^3.1058.0",
package/src/cdn-aws.js ADDED
@@ -0,0 +1,67 @@
1
+ 'use strict'
2
+ const fs = require('node:fs/promises')
3
+ const { createReadStream, constants } = require('node:fs')
4
+ const { createHash } = require('node:crypto')
5
+ const { finished } = require('node:stream/promises')
6
+ const os = require('node:os')
7
+ const path = require('node:path')
8
+ const { relativePath } = require('./cdn-publication')
9
+ const { awsCredentials } = require('./iam-session')
10
+
11
+ /** Seal one validated file on disk before upload, so a large file need not fit in memory. */
12
+ async function snapshotCdnAsset(publication, name) {
13
+ relativePath(name)
14
+ const expected = publication.descriptor.files[name], source = publication.sources?.[name]
15
+ if (!expected || !source) throw new Error('cdn publish: asset is not in the captured inventory')
16
+ const stat = await fs.lstat(source)
17
+ if (!stat.isFile() || stat.isSymbolicLink()) throw new Error('cdn publish: source changed before upload')
18
+ const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'saaslayer-cdn-asset-')), file = path.join(dir, 'asset')
19
+ let body
20
+ const dispose = async () => {
21
+ if (body) { const closed = finished(body).catch(() => {}); body.destroy(); await closed }
22
+ await fs.rm(dir, { recursive: true, force: true })
23
+ }
24
+ try {
25
+ await fs.copyFile(source, file, constants.COPYFILE_FICLONE)
26
+ await fs.chmod(file, 0o400)
27
+ const hash = createHash('sha384'); let size = 0
28
+ for await (const chunk of createReadStream(file)) { size += chunk.length; hash.update(chunk) }
29
+ const integrity = `sha384-${hash.digest('base64')}`
30
+ if (size !== expected.size || integrity !== expected.sri) throw new Error(`cdn publish: source changed before upload: ${name}`)
31
+ body = createReadStream(file)
32
+ return { body, size, sri: integrity, dispose }
33
+ } catch (error) { await dispose(); throw error }
34
+ }
35
+
36
+ /** Standard SDK transports, with the same selected credentials for STS and every bucket request. */
37
+ function createCdnAws({ profile, credentials = () => awsCredentials({ profile }), s3For, stsFor } = {}) {
38
+ const s3 = require('@aws-sdk/client-s3'), sts = require('@aws-sdk/client-sts')
39
+ const clients = new Map()
40
+ const client = target => {
41
+ if (!clients.has(target.region)) clients.set(target.region, s3For ? s3For(target) : new s3.S3Client({ region: target.region, credentials, maxAttempts: 3 }))
42
+ return clients.get(target.region)
43
+ }
44
+ return {
45
+ async awsAccountId(target) {
46
+ const client = stsFor ? stsFor(target) : new sts.STSClient({ region: target.region, credentials, maxAttempts: 3 })
47
+ const result = await client.send(new sts.GetCallerIdentityCommand({}))
48
+ if (!result.Account) throw new Error('cdn publish: STS did not return an AWS account')
49
+ return result.Account
50
+ },
51
+ s3: {
52
+ async get(target, key) {
53
+ let object
54
+ try { object = await client(target).send(new s3.GetObjectCommand({ Bucket: target.bucketName, Key: key, ExpectedBucketOwner: target.accountId })) }
55
+ catch (error) { if (error.name === 'NoSuchKey') return null; throw error }
56
+ return { body: object.Body, contentType: object.ContentType, cacheControl: object.CacheControl, contentEncoding: object.ContentEncoding }
57
+ },
58
+ async put(target, key, body, metadata, options) {
59
+ if (options.ifNoneMatch !== '*') throw new Error('cdn publish: conditional creation is required')
60
+ await client(target).send(new s3.PutObjectCommand({ Bucket: target.bucketName, Key: key, Body: body,
61
+ ExpectedBucketOwner: target.accountId, IfNoneMatch: '*', ContentLength: metadata.size ?? body.length,
62
+ ContentType: metadata.contentType, CacheControl: metadata.cacheControl }))
63
+ },
64
+ },
65
+ }
66
+ }
67
+ module.exports = { snapshotCdnAsset, createCdnAws }
@@ -0,0 +1,146 @@
1
+ 'use strict'
2
+ const fs = require('node:fs/promises')
3
+ const { createReadStream } = require('node:fs')
4
+ const { createHash } = require('node:crypto')
5
+ const path = require('node:path')
6
+
7
+ const CACHE_CONTROL = 'public, max-age=31536000, immutable'
8
+ const TYPES = { '.js': 'text/javascript; charset=utf-8', '.mjs': 'text/javascript; charset=utf-8',
9
+ '.css': 'text/css; charset=utf-8', '.json': 'application/json', '.html': 'text/html; charset=utf-8',
10
+ '.svg': 'image/svg+xml', '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg',
11
+ '.webp': 'image/webp', '.gif': 'image/gif', '.ico': 'image/x-icon', '.woff': 'font/woff',
12
+ '.woff2': 'font/woff2', '.wasm': 'application/wasm', '.txt': 'text/plain; charset=utf-8', '.map': 'application/json' }
13
+ const own = (o, k) => Object.prototype.hasOwnProperty.call(o, k)
14
+ function fail(message) { throw new Error(`cdn publish: ${message}`) }
15
+ function identity(packageName, version) {
16
+ if (typeof packageName !== 'string' || !/^[a-z0-9][a-z0-9._-]*$/.test(packageName)) fail('package must be one lowercase path segment')
17
+ // Semver prerelease identifiers: numeric identifiers have no leading zeroes.
18
+ if (typeof version !== 'string' || !/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?$/.test(version)) fail('version must be an exact semver, without a v prefix or build metadata')
19
+ return { package: packageName, version }
20
+ }
21
+ function relativePath(name) {
22
+ if (typeof name !== 'string' || !name || name !== name.normalize('NFC') || Buffer.from(name, 'utf8').toString('utf8') !== name || /[\\\x00-\x1f\x7f?#%]/.test(name)) fail('invalid asset path')
23
+ const parts = name.split('/')
24
+ if (parts.some(p => !p || p === '.' || p === '..' || p.startsWith('.') || p === '_publications')) fail('reserved or nonrelative asset path')
25
+ if (name === 'manifest.json' || name === 'health.json') fail('reserved asset path')
26
+ return name
27
+ }
28
+ function sri(bytes) { return `sha384-${createHash('sha384').update(bytes).digest('base64')}` }
29
+ function metadata(name, bytes) {
30
+ return { size: bytes.length, sri: sri(bytes), contentType: TYPES[path.extname(name).toLowerCase()] || 'application/octet-stream', cacheControl: CACHE_CONTROL }
31
+ }
32
+ function exactKeys(object, keys, label) {
33
+ if (!object || typeof object !== 'object' || Array.isArray(object) ||
34
+ Object.keys(object).length !== keys.length || !keys.every(k => own(object, k))) fail(`invalid ${label}`)
35
+ }
36
+ function digestFields(value, actual, label) {
37
+ exactKeys(value, ['size', 'sri'], label)
38
+ if (!Number.isSafeInteger(value.size) || value.size < 0 || value.size !== actual.size || value.sri !== actual.sri) fail(`${label} does not match original bytes`)
39
+ }
40
+ function manifest(bytes, packageName, version, files) {
41
+ let value
42
+ try { value = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(bytes)) } catch { fail('manifest.json must be valid UTF-8 JSON') }
43
+ if (!value || typeof value !== 'object' || Array.isArray(value) || value.version !== version) fail('manifest version mismatch')
44
+ if (own(value, 'files')) {
45
+ exactKeys(value, ['package', 'version', 'files'], 'generic manifest')
46
+ if (value.package !== packageName) fail('manifest package mismatch')
47
+ exactKeys(value.files, Object.keys(files), 'manifest file inventory')
48
+ for (const [name, actual] of Object.entries(files)) {
49
+ relativePath(name); digestFields(value.files[name], actual, `manifest entry ${name}`)
50
+ }
51
+ } else {
52
+ if (packageName !== 'embed') fail('legacy manifests are only supported for package embed')
53
+ const hasSso = own(value, 'ssoPages')
54
+ exactKeys(value, ['version', 'size', 'sri', ...(hasSso ? ['ssoPages'] : [])], 'legacy embed manifest')
55
+ exactKeys(files, ['widgets.js', ...(hasSso ? ['sso-pages.js'] : [])], 'legacy embed inventory')
56
+ digestFields({ size: value.size, sri: value.sri }, files['widgets.js'], 'widgets.js')
57
+ if (hasSso) digestFields(value.ssoPages, files['sso-pages.js'], 'sso-pages.js')
58
+ }
59
+ return value
60
+ }
61
+ function canonical(value) {
62
+ if (Array.isArray(value)) return `[${value.map(canonical).join(',')}]`
63
+ if (value && typeof value === 'object') return `{${Object.keys(value).sort().map(k => `${JSON.stringify(k)}:${canonical(value[k])}`).join(',')}}`
64
+ return JSON.stringify(value)
65
+ }
66
+ function buildPublication(packageName, version, entries, manifestBytes) {
67
+ identity(packageName, version)
68
+ if (!Array.isArray(entries) || entries.length === 0) fail('release has no assets')
69
+ const files = Object.create(null)
70
+ for (const entry of entries) {
71
+ const name = relativePath(entry.name)
72
+ if (own(files, name)) fail(`duplicate asset ${name}`)
73
+ if (!Buffer.isBuffer(entry.bytes)) fail(`asset ${name} must contain original bytes`)
74
+ files[name] = metadata(name, entry.bytes)
75
+ }
76
+ return finish(packageName, version, files, manifestBytes)
77
+ }
78
+ function finish(packageName, version, files, supplied) {
79
+ // Both the public manifest and private reservation must also fit S3's UTF-8 key limit.
80
+ const prefix = `${packageName}/${version}/`
81
+ for (const key of [prefix + 'manifest.json', `_publications/${prefix}publication.json`, ...Object.keys(files).map(name => prefix + name)]) {
82
+ if (Buffer.byteLength(key, 'utf8') > 1024) fail('publication object key exceeds 1024 UTF-8 bytes')
83
+ }
84
+ const sorted = Object.fromEntries(Object.keys(files).sort().map(k => [k, files[k]]))
85
+ let manifestBytes
86
+ if (supplied !== undefined) {
87
+ manifestBytes = Buffer.from(supplied)
88
+ manifest(manifestBytes, packageName, version, sorted)
89
+ } else {
90
+ const publicFiles = Object.fromEntries(Object.entries(sorted).map(([name, f]) => [name, { size: f.size, sri: f.sri }]))
91
+ manifestBytes = Buffer.from(canonical({ package: packageName, version, files: publicFiles }) + '\n')
92
+ }
93
+ const descriptor = { package: packageName, version, files: sorted, manifest: { size: manifestBytes.length, sri: sri(manifestBytes) } }
94
+ const descriptorBytes = Buffer.from(canonical(descriptor) + '\n')
95
+ if (descriptorBytes.length > 256 * 1024 || manifestBytes.length > 1024 * 1024) fail('publication metadata exceeds the descriptor (256 KiB) or manifest (1 MiB) limit')
96
+ return { descriptor, descriptorBytes, descriptorSha256: createHash('sha256').update(descriptorBytes).digest('hex'), manifestBytes }
97
+ }
98
+
99
+ // Reject symlinks rather than resolving through a tree whose links may change while publishing.
100
+ // Hash streaming so a large release does not have to fit into memory. The upload reader
101
+ // rechecks each captured buffer against the inventory before sending it to S3.
102
+ async function inventoryDirectory(dir, packageName, version) {
103
+ identity(packageName, version)
104
+ const root = path.resolve(dir)
105
+ if (!(await fs.lstat(root)).isDirectory()) fail('input must be a real directory')
106
+ const files = Object.create(null), sources = Object.create(null)
107
+ let supplied
108
+ async function walk(directory, prefix = '') {
109
+ const entries = await fs.readdir(directory, { withFileTypes: true })
110
+ entries.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0)
111
+ for (const entry of entries) {
112
+ const name = prefix + entry.name, source = path.join(directory, entry.name)
113
+ const before = await fs.lstat(source, { bigint: true })
114
+ if (before.isSymbolicLink()) fail(`symlinks are not supported: ${name}`)
115
+ if (before.isDirectory()) { relativePath(name); await walk(source, name + '/'); continue }
116
+ if (!before.isFile()) fail(`not a regular file: ${name}`)
117
+ if (name === 'manifest.json') {
118
+ supplied = await fs.readFile(source)
119
+ } else {
120
+ relativePath(name)
121
+ const hash = createHash('sha384'); let size = 0
122
+ for await (const bytes of createReadStream(source)) { hash.update(bytes); size += bytes.length }
123
+ if (!Number.isSafeInteger(size)) fail(`asset too large: ${name}`)
124
+ files[name] = { size, sri: `sha384-${hash.digest('base64')}`, contentType: TYPES[path.extname(name).toLowerCase()] || 'application/octet-stream', cacheControl: CACHE_CONTROL }
125
+ sources[name] = source
126
+ }
127
+ const after = await fs.lstat(source, { bigint: true })
128
+ if (before.ino !== after.ino || before.dev !== after.dev || before.size !== after.size || before.mtimeNs !== after.mtimeNs || before.ctimeNs !== after.ctimeNs) fail(`input changed while reading: ${name}`)
129
+ }
130
+ }
131
+ await walk(root)
132
+ if (!Object.keys(files).length) fail('release has no assets')
133
+ return { ...finish(packageName, version, files, supplied), sources }
134
+ }
135
+ async function readAsset(publication, name) {
136
+ relativePath(name)
137
+ if (!own(publication.sources, name) || !own(publication.descriptor.files, name)) fail('asset is not in publication inventory')
138
+ const source = publication.sources[name]
139
+ const stat = await fs.lstat(source)
140
+ if (!stat.isFile() || stat.isSymbolicLink()) fail(`input changed before upload: ${name}`)
141
+ const bytes = await fs.readFile(source)
142
+ const expected = publication.descriptor.files[name]
143
+ if (bytes.length !== expected.size || sri(bytes) !== expected.sri) fail(`input changed before upload: ${name}`)
144
+ return bytes
145
+ }
146
+ module.exports = { CACHE_CONTROL, identity, relativePath, sri, canonical, buildPublication, inventoryDirectory, readAsset }
@@ -0,0 +1,166 @@
1
+ 'use strict'
2
+ const { CACHE_CONTROL, canonical, identity, relativePath, sri, readAsset: readInventoriedAsset } = require('./cdn-publication')
3
+ const { createHash } = require('node:crypto')
4
+
5
+ const conflict = message => Object.assign(new Error(`cdn publish: ${message}`), { code: 'CDN_VERSION_CONFLICT' })
6
+ function destination(value) {
7
+ if (!value || !/^\d{12}$/.test(value.accountId) || !/^[a-z]{2}-[a-z]+-\d+$/.test(value.region) ||
8
+ !/^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$/.test(value.bucketName) || !/^[A-Z0-9]+$/.test(value.distributionId) ||
9
+ typeof value.deploymentId !== 'string' || !value.deploymentId || !/^cdn-service(?:@[a-z][a-z0-9]{0,11})?$/.test(value.service)) throw new Error('cdn publish: invalid authenticated destination')
10
+ const url = new URL(value.cdnBaseUrl)
11
+ if (url.protocol !== 'https:' || url.username || url.password || url.port || url.search || url.hash || url.pathname !== '/' ||
12
+ url.hostname.split('.').length < 2 || !url.hostname.split('.').every(l => /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/.test(l))) throw new Error('cdn publish: invalid authenticated CDN URL')
13
+ return { ...value, cdnBaseUrl: url.origin }
14
+ }
15
+ const sameTarget = (a, b) => ['deploymentId', 'service', 'accountId', 'region', 'bucketName', 'distributionId', 'cdnBaseUrl'].every(k => a[k] === b[k])
16
+ async function bodyFingerprint(body, maximum) {
17
+ const hash = createHash('sha384'); let size = 0
18
+ if (Buffer.isBuffer(body)) return { size: body.length, sri: sri(body) }
19
+ if (!body || typeof body[Symbol.asyncIterator] !== 'function') throw conflict('invalid object body')
20
+ for await (const chunk of body) {
21
+ size += chunk.length
22
+ if (size > maximum) throw conflict('object exceeds its reserved size')
23
+ hash.update(chunk)
24
+ }
25
+ return { size, sri: `sha384-${hash.digest('base64')}` }
26
+ }
27
+ async function original(object, bytes, contentType, cacheControl, key) {
28
+ return originalAsset(object, { size: bytes.length, sri: sri(bytes), contentType, cacheControl }, key)
29
+ }
30
+ async function originalAsset(object, expected, key) {
31
+ if (!object || object.contentType !== expected.contentType || object.cacheControl !== expected.cacheControl || object.contentEncoding) {
32
+ object?.body?.destroy?.(); await object?.body?.cancel?.()
33
+ throw conflict(`existing asset differs in bytes or metadata: ${key}`)
34
+ }
35
+ const actual = await bodyFingerprint(object.body, expected.size)
36
+ if (actual.size !== expected.size || actual.sri !== expected.sri) throw conflict(`existing asset differs in bytes or metadata: ${key}`)
37
+ }
38
+ const publicUrl = (target, key) => `${target.cdnBaseUrl}/${key.split('/').map(encodeURIComponent).join('/')}`
39
+
40
+ /** Bounded, redirect-free public verification; transient negative-cache responses may be retried. */
41
+ async function verifyPublic({ url, expected, fetch: request = globalThis.fetch, sleep = ms => new Promise(r => setTimeout(r, ms)), attempts = 5,
42
+ headerTimeoutMs = 15_000, idleTimeoutMs = 30_000 }) {
43
+ if (!Number.isInteger(attempts) || attempts < 1 || attempts > 10 || !Number.isSafeInteger(expected.size) || expected.size < 0 || expected.size > 5 * 1024 ** 3) throw new Error('Invalid CDN verification bound')
44
+ let last
45
+ for (let attempt = 0; attempt < attempts; attempt++) {
46
+ const controller = new AbortController()
47
+ let headerTimer, transferTimer, idleTimer, response
48
+ const arm = ms => { const timer = setTimeout(() => controller.abort(), ms); timer.unref?.(); return timer }
49
+ const progress = () => { clearTimeout(idleTimer); idleTimer = arm(idleTimeoutMs) }
50
+ try {
51
+ headerTimer = arm(headerTimeoutMs)
52
+ response = await request(url, { redirect: 'manual', signal: controller.signal })
53
+ clearTimeout(headerTimer)
54
+ if (response.status === 200) {
55
+ if (response.headers.get('content-type') !== expected.contentType || response.headers.get('cache-control') !== expected.cacheControl) throw conflict(`public object metadata differs: ${url}`)
56
+ // The header deadline must not truncate a progressing large body. Bound total time by size and also
57
+ // refuse a stalled transfer; hash chunks directly rather than buffering an object in memory.
58
+ transferTimer = arm(Math.max(60_000, 60_000 + Math.ceil(expected.size / (256 * 1024)) * 1000))
59
+ progress()
60
+ const hash = createHash('sha384'); let size = 0
61
+ for await (const chunk of response.body || []) {
62
+ progress(); size += chunk.length
63
+ if (size > expected.size) throw conflict(`public object is larger than its descriptor: ${url}`)
64
+ hash.update(chunk)
65
+ }
66
+ if (size !== expected.size || `sha384-${hash.digest('base64')}` !== expected.sri) throw conflict(`public bytes differ: ${url}`)
67
+ return
68
+ }
69
+ if (![404, 429, 500, 502, 503, 504].includes(response.status)) throw Object.assign(new Error(`cdn publish: public verification returned HTTP ${response.status}: ${url}`), { permanent: true })
70
+ last = new Error(`cdn publish: public verification returned HTTP ${response.status}: ${url}`)
71
+ } catch (error) {
72
+ if (error.code === 'CDN_VERSION_CONFLICT' || error.permanent) throw error
73
+ last = error
74
+ } finally {
75
+ clearTimeout(headerTimer); clearTimeout(transferTimer); clearTimeout(idleTimer)
76
+ controller.abort()
77
+ try { await response?.body?.cancel?.() } catch {}
78
+ }
79
+ if (attempt + 1 < attempts) await sleep((attempt + 1) * 1000)
80
+ }
81
+ throw last || new Error(`cdn publish: public verification failed: ${url}`)
82
+ }
83
+
84
+ /**
85
+ * Actual publication protocol. Product discovery/admission/completion and authenticated S3 reads/conditional
86
+ * puts are transports supplied by the command. No cached-target fallback, delete, overwrite or lease stealing.
87
+ * S3 transport: get(target,key) => null ONLY for a missing key; put(target,key,body,metadata,{ifNoneMatch:'*'}).
88
+ */
89
+ async function publishCdn({ publication, discover, awsAccountId, admit, complete, s3, dryRun = false,
90
+ readAsset = readInventoriedAsset, verify = verifyPublic, sleep = ms => new Promise(r => setTimeout(r, ms)), completionAttempts = 5400, onPending = () => {} }) {
91
+ const { descriptor, descriptorBytes, descriptorSha256, manifestBytes } = publication
92
+ identity(descriptor.package, descriptor.version)
93
+ if (!Buffer.isBuffer(descriptorBytes) || !descriptorBytes.equals(Buffer.from(canonical(descriptor) + '\n')) ||
94
+ createHash('sha256').update(descriptorBytes).digest('hex') !== descriptorSha256 || !Buffer.isBuffer(manifestBytes) ||
95
+ sri(manifestBytes) !== descriptor.manifest.sri || manifestBytes.length !== descriptor.manifest.size) throw conflict('publication inventory changed')
96
+ const files = Object.entries(descriptor.files)
97
+ if (!files.length) throw conflict('empty publication inventory')
98
+ for (const [name, entry] of files) {
99
+ relativePath(name)
100
+ if (!Number.isSafeInteger(entry.size) || entry.size < 0 || entry.size > 5 * 1024 ** 3 || entry.cacheControl !== CACHE_CONTROL ||
101
+ typeof entry.contentType !== 'string' || !/^sha384-[A-Za-z0-9+/]{64}$/.test(entry.sri)) throw conflict('invalid asset descriptor or object exceeds single-PUT limit')
102
+ }
103
+ const target = destination(await discover()) // Always authenticated and live, including completed retries.
104
+ if (await awsAccountId(target) !== target.accountId) throw new Error('cdn publish: AWS credentials do not match the deployment account; select the correct --profile')
105
+ if (dryRun) return { target, publication, dryRun: true }
106
+ const request = { service: target.service, package: descriptor.package, version: descriptor.version, descriptor }
107
+ const admission = destination(await admit(target, request))
108
+ if (!sameTarget(target, admission) || !['reserved', 'published'].includes(admission.status)) throw new Error('cdn publish: destination changed during admission; resolve it again')
109
+ const prefix = `${descriptor.package}/${descriptor.version}/`, descriptorKey = `_publications/${prefix}publication.json`, manifestKey = prefix + 'manifest.json'
110
+ const descriptorMeta = { contentType: 'application/json', cacheControl: 'no-store' }, manifestMeta = { contentType: 'application/json', cacheControl: CACHE_CONTROL }
111
+ const savedDescriptor = await s3.get(target, descriptorKey)
112
+ if (savedDescriptor) await original(savedDescriptor, descriptorBytes, descriptorMeta.contentType, descriptorMeta.cacheControl, descriptorKey)
113
+ const savedManifest = await s3.get(target, manifestKey)
114
+ if (savedManifest) await original(savedManifest, manifestBytes, manifestMeta.contentType, manifestMeta.cacheControl, manifestKey)
115
+ if ((savedManifest && !savedDescriptor) || (admission.status === 'published' && (!savedManifest || !savedDescriptor))) throw conflict('committed release is missing its original descriptor or manifest')
116
+ const missing = []
117
+ for (const [name, expected] of files) {
118
+ const key = prefix + name, saved = await s3.get(target, key)
119
+ if (saved) await originalAsset(saved, expected, key)
120
+ else if (savedManifest || admission.status === 'published') throw conflict(`committed release is missing ${name}`)
121
+ else missing.push([name, expected])
122
+ }
123
+ const put = async (key, bytes, metadata, expected = { size: bytes.length, sri: sri(bytes) }) => {
124
+ try { await s3.put(target, key, bytes, metadata, { ifNoneMatch: '*' }) }
125
+ catch (error) {
126
+ // A 412, transport error or lost successful response grants no overwrite permission.
127
+ const saved = await s3.get(target, key)
128
+ if (!saved) throw error
129
+ await originalAsset(saved, { ...expected, contentType: metadata.contentType, cacheControl: metadata.cacheControl }, key)
130
+ }
131
+ }
132
+ if (!savedDescriptor) await put(descriptorKey, descriptorBytes, descriptorMeta)
133
+ for (const [name, expected] of missing) {
134
+ const prepared = await readAsset(publication, name)
135
+ try {
136
+ if (Buffer.isBuffer(prepared)) {
137
+ if (prepared.length !== expected.size || sri(prepared) !== expected.sri) throw conflict(`local input changed: ${name}`)
138
+ await put(prefix + name, prepared, expected, expected)
139
+ } else {
140
+ if (prepared.size !== expected.size || prepared.sri !== expected.sri || !prepared.body) throw conflict(`local input changed: ${name}`)
141
+ await put(prefix + name, prepared.body, expected, expected)
142
+ }
143
+ } finally { if (!Buffer.isBuffer(prepared)) await prepared?.dispose?.() }
144
+ }
145
+ let committed = !!savedManifest
146
+ try {
147
+ for (const [name, expected] of files) await verify({ url: publicUrl(target, prefix + name), expected })
148
+ if (!savedManifest) { committed = 'unknown'; await put(manifestKey, manifestBytes, manifestMeta); committed = true }
149
+ await verify({ url: publicUrl(target, manifestKey), expected: { ...descriptor.manifest, ...manifestMeta } })
150
+ if (!Number.isInteger(completionAttempts) || completionAttempts < 1 || completionAttempts > 5400) throw new Error('Invalid CDN completion retry bound')
151
+ let published = false
152
+ const deadline = Date.now() + 3 * 60 * 60 * 1000
153
+ for (let attempt = 0; attempt < completionAttempts && Date.now() < deadline; attempt++) {
154
+ const result = destination(await complete(target, request)) // Every poll rechecks live active/withdrawal authority.
155
+ if (!sameTarget(target, result) || !['reserved', 'published'].includes(result.status)) throw new Error('cdn publish: product did not confirm this publication')
156
+ if (result.status === 'published') { published = true; break }
157
+ if (attempt === 0) onPending()
158
+ if (attempt + 1 < completionAttempts) await sleep(2000)
159
+ }
160
+ if (!published) throw new Error('cdn publish: verification is still running; rerun this exact command to resume')
161
+ } catch (error) { error.committed = committed; throw error }
162
+ const snippets = files.filter(([name]) => /\.(?:m?js)$/.test(name)).map(([name, expected]) =>
163
+ `<script src="${publicUrl(target, prefix + name)}" integrity="${expected.sri}" crossorigin="anonymous"></script>`)
164
+ return { target, existing: !!savedManifest, status: 'published', manifestUrl: publicUrl(target, manifestKey), snippets }
165
+ }
166
+ module.exports = { publishCdn, verifyPublic, publicUrl }
@@ -0,0 +1,66 @@
1
+ 'use strict'
2
+ const { randomUUID } = require('node:crypto')
3
+ const { apiCall } = require('../api-client')
4
+ const { resolveDeployment } = require('../deployments')
5
+ const { inventoryDirectory, identity } = require('../cdn-publication')
6
+ const { publishCdn } = require('../cdn-publisher')
7
+ const { createCdnAws, snapshotCdnAsset } = require('../cdn-aws')
8
+ const { followOperation } = require('./operations')
9
+ const attributes = value => value?.data?.attributes || value
10
+
11
+ async function cdnCommand(action, args, flags, deps = {}) {
12
+ const accepted = new Set(['package', 'version', 'deployment', 'project', 'env', 'region', 'org', 'service', 'profile', 'config-url', 'json', 'dry-run', 'reason', 'as'])
13
+ for (const name of Object.keys(flags)) if (!accepted.has(name)) throw new Error(`cdn: unsupported flag --${name}`)
14
+ if (!['publish', 'withdraw'].includes(action)) throw new Error('usage: saaslayer cdn publish <dir> --package NAME --version VERSION | cdn withdraw --package NAME --version VERSION --reason TEXT')
15
+ identity(flags.package, flags.version)
16
+ const service = flags.service === undefined ? 'cdn-service' : flags.service
17
+ if (typeof service !== 'string' || !/^cdn-service(?:@[a-z][a-z0-9]{0,11})?$/.test(service)) throw new Error('--service must be cdn-service or cdn-service@<instance>')
18
+ if (flags['dry-run'] !== undefined && flags['dry-run'] !== true) throw new Error('--dry-run is a presence flag')
19
+ if (action === 'publish' ? args.length !== 1 : args.length !== 0) throw new Error(action === 'publish' ? 'cdn publish requires exactly one asset directory' : 'cdn withdraw takes no directory')
20
+ if (action === 'withdraw' && (typeof flags.reason !== 'string' || !flags.reason.trim())) throw new Error('cdn withdraw requires --reason')
21
+ const call = deps.apiCall || apiCall, print = deps.print || console.log
22
+ const resolve = deps.resolveDeployment || resolveDeployment
23
+ const deploymentId = typeof flags.deployment === 'string' && flags.deployment.startsWith('dep_') ? flags.deployment : (await resolve(flags)).deploymentId
24
+ if (!deploymentId) throw new Error('cdn: select one active deployment with --deployment or --project/--env/--region')
25
+ const route = `/api/deployments/${encodeURIComponent(deploymentId)}/cdn/`
26
+ if (action === 'withdraw') {
27
+ const body = { service, package: flags.package, version: flags.version, reason: flags.reason.trim() }
28
+ const result = attributes(await call('POST', route + 'withdrawals' + (flags['dry-run'] ? '?dryRun=true' : ''), body, flags))
29
+ if (flags['dry-run']) {
30
+ print(flags.json ? JSON.stringify(result, null, 2) : `Would withdraw ${flags.package}@${flags.version} from ${service} on ${deploymentId}`)
31
+ return result
32
+ }
33
+ const operationId = result.operationId || result.operation?.id || result.runId
34
+ if (!operationId) throw new Error('cdn withdraw: product returned no operation to follow')
35
+ return (deps.followOperation || followOperation)({ deploymentId, operationId, flags })
36
+ }
37
+ const publication = await (deps.inventoryDirectory || inventoryDirectory)(args[0], flags.package, flags.version)
38
+ const request = { service, package: flags.package, version: flags.version, descriptor: publication.descriptor }
39
+ const aws = deps.aws || createCdnAws({ profile: flags.profile })
40
+ const completionKey = randomUUID() // One command attempt; a rerun can resume an interrupted verification.
41
+ let result
42
+ try {
43
+ result = await publishCdn({ publication, dryRun: !!flags['dry-run'], awsAccountId: aws.awsAccountId, s3: aws.s3,
44
+ discover: async () => {
45
+ const target = attributes(await call('POST', route + 'publications?dryRun=true', request, flags))
46
+ if (target.deploymentId !== deploymentId || target.service !== service || (flags.region && target.region !== flags.region)) throw new Error('cdn publish: live destination does not match the selected deployment, service or region')
47
+ return target
48
+ },
49
+ admit: async (_target, body) => attributes(await call('POST', route + 'publications', body, flags)),
50
+ complete: async (_target, body) => attributes(await call('POST', route + 'publications/complete', body, flags, { headers: { 'idempotency-key': completionKey } })),
51
+ readAsset: deps.readAsset || snapshotCdnAsset,
52
+ onPending: deps.onPending || (() => console.error('Verifying uploaded CDN assets; waiting for completion…')),
53
+ ...(deps.verify ? { verify: deps.verify } : {}),
54
+ })
55
+ } catch (error) {
56
+ if (error.committed) error.message += '\nThe manifest is committed or its outcome is unknown; rerun this exact command to verify it. Do not change the version contents.'
57
+ throw error
58
+ }
59
+ const output = { ...result.target, package: flags.package, version: flags.version, descriptor: publication.descriptor,
60
+ ...(result.status ? { status: result.status } : {}), ...(result.dryRun ? { dryRun: true } : {}) }
61
+ if (flags.json) print(JSON.stringify(output, null, 2))
62
+ else if (result.dryRun) print(`Would publish ${flags.package}@${flags.version} to ${result.target.cdnBaseUrl} (${result.target.accountId}, ${service}); no reservation or upload made`)
63
+ else print([`${result.existing ? 'Verified existing' : 'Published'} ${flags.package}@${flags.version}`, result.manifestUrl, ...result.snippets].join('\n'))
64
+ return output
65
+ }
66
+ module.exports = { cdnCommand }