publication-payload.ts 1.2 KB

123456789101112131415161718192021222324252627
  1. /** Publication payload policy shared by static manifests and packed tarballs. */
  2. /** Normalize a package manifest path or npm tarball member to its payload-relative path. */
  3. function payloadPath(file: string): string {
  4. const normalized = file.replaceAll('\\', '/').replace(/^\.\/+/, '').replace(/\/+$/, '')
  5. return normalized.startsWith('package/') ? normalized.slice('package/'.length) : normalized
  6. }
  7. /** Whether a package payload path exposes source or declaration-map intermediates. */
  8. export function isForbiddenPublicationFile(file: string): boolean {
  9. const normalized = payloadPath(file)
  10. return normalized === 'src'
  11. || normalized.startsWith('src/')
  12. || normalized.endsWith('.d.ts.map')
  13. }
  14. /** Reject source and declaration-map members in a packed npm tarball. */
  15. export function validateTarballPayload(files: readonly string[], context: string): void {
  16. for (const file of files) {
  17. if (!isForbiddenPublicationFile(file)) continue
  18. const normalized = payloadPath(file)
  19. if (normalized === 'src' || normalized.startsWith('src/')) {
  20. throw new Error(`${context} publishes source file ${file}`)
  21. }
  22. throw new Error(`${context} publishes declaration map ${file}`)
  23. }
  24. }