(files: Files)
| 1118 | * parallel for throughput. |
| 1119 | */ |
| 1120 | export async function calculateBundleSize(files: Files): Promise<number> { |
| 1121 | let knownSize = 0; |
| 1122 | const statPromises: Promise<number>[] = []; |
| 1123 | |
| 1124 | for (const filePath of Object.keys(files)) { |
| 1125 | const file = files[filePath]; |
| 1126 | if ('fsPath' in file && file.fsPath) { |
| 1127 | const fsRef = file as FileFsRef; |
| 1128 | if (typeof fsRef.size === 'number') { |
| 1129 | // Size already known (populated from RECORD or prior stat). |
| 1130 | knownSize += fsRef.size; |
| 1131 | } else { |
| 1132 | statPromises.push( |
| 1133 | fs.promises |
| 1134 | .stat(fsRef.fsPath) |
| 1135 | .then(stats => stats.size) |
| 1136 | .catch(err => { |
| 1137 | console.warn( |
| 1138 | `Warning: Failed to stat file ${fsRef.fsPath}, size will not be included in bundle calculation: ${err}` |
| 1139 | ); |
| 1140 | return 0; |
| 1141 | }) |
| 1142 | ); |
| 1143 | } |
| 1144 | } else if ('data' in file) { |
| 1145 | // FileBlob with data |
| 1146 | const data = (file as { data: string | Buffer }).data; |
| 1147 | knownSize += |
| 1148 | typeof data === 'string' ? Buffer.byteLength(data) : data.length; |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | const statSizes = await Promise.all(statPromises); |
| 1153 | let totalSize = knownSize; |
| 1154 | for (const s of statSizes) { |
| 1155 | totalSize += s; |
| 1156 | } |
| 1157 | return totalSize; |
| 1158 | } |
| 1159 | |
| 1160 | /** |
| 1161 | * Largest-first knapsack packing algorithm. |
no test coverage detected