(header: Header, alloc: (size: number) => Uint8Array)
| 976 | // Writes the header blocks of an entry, with a PAX header first when needed, into |
| 977 | // zeroed memory from `alloc`. Throws before allocating when the header is invalid. |
| 978 | const encodeHeader = (header: Header, alloc: (size: number) => Uint8Array): void => { |
| 979 | const path = checkNoNul("path", header.path) |
| 980 | if (path === "") { |
| 981 | throw invalidHeader("path must not be empty") |
| 982 | } |
| 983 | // GNU tar and node-tar read such an entry as a directory |
| 984 | if (header.type === "File" && path.endsWith("/")) { |
| 985 | throw invalidHeader(`File entry ${JSON.stringify(path)} must not end with "/"`) |
| 986 | } |
| 987 | const typeflag = typeflagOf(header) |
| 988 | if (typeflag.length !== 1 || typeflag.charCodeAt(0) >= 0x80 || "xgLKS".includes(typeflag)) { |
| 989 | throw invalidHeader(`invalid typeflag ${JSON.stringify(typeflag)}`) |
| 990 | } |
| 991 | const size = checkNumber("size", header.size, Number.MAX_SAFE_INTEGER) |
| 992 | if (size > 0 && isHeaderOnly(header.type)) { |
| 993 | throw invalidHeader(`${header.type} entry ${JSON.stringify(path)} must have size 0`) |
| 994 | } |
| 995 | const mode = checkNumber("mode", header.mode, Number.MAX_SAFE_INTEGER) & PERMISSION_BITS |
| 996 | const uid = checkNumber("uid", header.uid, Number.MAX_SAFE_INTEGER) |
| 997 | const gid = checkNumber("gid", header.gid, Number.MAX_SAFE_INTEGER) |
| 998 | const devMajor = checkNumber("devMajor", header.devMajor, MAX_OCTAL_7) |
| 999 | const devMinor = checkNumber("devMinor", header.devMinor, MAX_OCTAL_7) |
| 1000 | const millis = header.mtime.getTime() |
| 1001 | if (Number.isNaN(millis)) { |
| 1002 | throw invalidHeader("mtime must be a valid date") |
| 1003 | } |
| 1004 | const mtime = Math.floor(millis / 1000) |
| 1005 | |
| 1006 | // Records of the PAX header, created when one is needed. Their keys are |
| 1007 | // unique, as custom records cannot use the keys of header fields. |
| 1008 | let pax: Array<readonly [key: string, value: string]> | undefined |
| 1009 | for (const key of Object.keys(header.pax)) { |
| 1010 | if (key.startsWith("GNU.sparse.")) { |
| 1011 | throw invalidHeader(`PAX record ${JSON.stringify(key)} is not supported`) |
| 1012 | } |
| 1013 | if (paxHeaderKeys.has(key)) { |
| 1014 | throw invalidHeader(`PAX record ${JSON.stringify(key)} must be set through its header field`) |
| 1015 | } |
| 1016 | if (key === "" || key.includes("=") || key.includes("\0")) { |
| 1017 | throw invalidHeader(`invalid PAX record ${JSON.stringify(key)}`) |
| 1018 | } |
| 1019 | pax ??= [] |
| 1020 | pax.push([key, header.pax[key]]) |
| 1021 | } |
| 1022 | |
| 1023 | // PAX holds overflow values; USTAR keeps truncated ASCII strings or zero. |
| 1024 | const split = splitPath(path) |
| 1025 | const linkTarget = checkNoNul("linkTarget", header.linkTarget ?? "") |
| 1026 | const uname = checkNoNul("uname", header.uname) |
| 1027 | const gname = checkNoNul("gname", header.gname) |
| 1028 | const pathFits = split !== undefined || fits(path, 100) |
| 1029 | const linkTargetFits = fits(linkTarget, 100) |
| 1030 | const unameFits = fits(uname, 32) |
| 1031 | const gnameFits = fits(gname, 32) |
| 1032 | if (!pathFits) (pax ??= []).push(["path", path]) |
| 1033 | if (!linkTargetFits) (pax ??= []).push(["linkpath", linkTarget]) |
| 1034 | if (!unameFits) (pax ??= []).push(["uname", uname]) |
| 1035 | if (!gnameFits) (pax ??= []).push(["gname", gname]) |
no test coverage detected