同步已上线的智能分析执行协议、来源查询、图表报告与管理页面配套代码。 补齐数据库迁移、运行依赖和回归测试,保留原有指标计算口径。 统一午夜黑及其他主题的控件细节、中文和业务数字字体。 排除本地业务数据、凭据、预览、发布与验收产物。 验证:生产构建通过,320项前端回归通过;本轮仅同步Git,不再次部署。
156 lines
5.4 KiB
TypeScript
156 lines
5.4 KiB
TypeScript
const PNG_SIGNATURE = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]);
|
|
const MAX_REPORT_PIXELS = 100_000_000;
|
|
const MAX_REPORT_WIDTH = 16_384;
|
|
const IDAT_CHUNK_SIZE = 64 * 1024;
|
|
|
|
export type RgbaStrip = {
|
|
y: number;
|
|
height: number;
|
|
data: Uint8Array | Uint8ClampedArray;
|
|
};
|
|
|
|
const CRC_TABLE = (() => {
|
|
const table = new Uint32Array(256);
|
|
for (let index = 0; index < table.length; index += 1) {
|
|
let value = index;
|
|
for (let bit = 0; bit < 8; bit += 1) value = value & 1 ? 0xedb88320 ^ (value >>> 1) : value >>> 1;
|
|
table[index] = value >>> 0;
|
|
}
|
|
return table;
|
|
})();
|
|
|
|
function writeUint32(target: Uint8Array, offset: number, value: number) {
|
|
target[offset] = (value >>> 24) & 0xff;
|
|
target[offset + 1] = (value >>> 16) & 0xff;
|
|
target[offset + 2] = (value >>> 8) & 0xff;
|
|
target[offset + 3] = value & 0xff;
|
|
}
|
|
|
|
function crc32(bytes: Uint8Array) {
|
|
let value = 0xffffffff;
|
|
for (const byte of bytes) value = CRC_TABLE[(value ^ byte) & 0xff] ^ (value >>> 8);
|
|
return (value ^ 0xffffffff) >>> 0;
|
|
}
|
|
|
|
function pngChunk(type: string, data: Uint8Array) {
|
|
const bytes = new Uint8Array(12 + data.length);
|
|
writeUint32(bytes, 0, data.length);
|
|
bytes.set(new TextEncoder().encode(type), 4);
|
|
bytes.set(data, 8);
|
|
writeUint32(bytes, 8 + data.length, crc32(bytes.subarray(4, 8 + data.length)));
|
|
return bytes;
|
|
}
|
|
|
|
function pngHeader(width: number, height: number) {
|
|
const header = new Uint8Array(13);
|
|
writeUint32(header, 0, width);
|
|
writeUint32(header, 4, height);
|
|
header[8] = 8; // bit depth
|
|
header[9] = 6; // truecolour with alpha
|
|
return header;
|
|
}
|
|
|
|
function filteredRows(strip: RgbaStrip, width: number) {
|
|
const rowBytes = width * 4;
|
|
const filteredRowBytes = rowBytes + 1;
|
|
const output = new Uint8Array(filteredRowBytes * strip.height);
|
|
for (let row = 0; row < strip.height; row += 1) {
|
|
const outputStart = row * filteredRowBytes;
|
|
output[outputStart] = 0; // PNG filter None
|
|
output.set(strip.data.subarray(row * rowBytes, (row + 1) * rowBytes), outputStart + 1);
|
|
}
|
|
return output;
|
|
}
|
|
|
|
function validateDimensions(width: number, height: number) {
|
|
if (!Number.isSafeInteger(width) || !Number.isSafeInteger(height) || width <= 0 || height <= 0) {
|
|
throw new Error('报告尺寸无效,无法生成长图。');
|
|
}
|
|
if (width > MAX_REPORT_WIDTH || width * height > MAX_REPORT_PIXELS) {
|
|
throw new Error('报告过长,超过单张清晰长图的安全尺寸上限。请缩小报告范围后重试。');
|
|
}
|
|
}
|
|
|
|
function cancelledError() {
|
|
return new Error('已取消长图准备。');
|
|
}
|
|
|
|
/**
|
|
* Encode ordered RGBA strips into one PNG without allocating a full-height
|
|
* canvas or uncompressed image buffer. The compressed IDAT stream is framed
|
|
* into ordinary PNG chunks as it arrives.
|
|
*/
|
|
export async function encodeRgbaStripsToPng(
|
|
width: number,
|
|
height: number,
|
|
strips: AsyncIterable<RgbaStrip>,
|
|
cancelled: () => boolean = () => false,
|
|
): Promise<Blob> {
|
|
validateDimensions(width, height);
|
|
const checkCancelled = () => { if (cancelled()) throw cancelledError(); };
|
|
checkCancelled();
|
|
if (typeof CompressionStream === 'undefined') {
|
|
throw new Error('当前浏览器不支持分段长图编码,请更新浏览器后重试。');
|
|
}
|
|
|
|
const stream = new CompressionStream('deflate');
|
|
const writer = stream.writable.getWriter();
|
|
const reader = stream.readable.getReader();
|
|
const parts: BlobPart[] = [PNG_SIGNATURE, pngChunk('IHDR', pngHeader(width, height))];
|
|
let pendingIdat = new Uint8Array(IDAT_CHUNK_SIZE);
|
|
let pendingLength = 0;
|
|
const flushIdat = () => {
|
|
if (pendingLength === 0) return;
|
|
parts.push(pngChunk('IDAT', pendingIdat.slice(0, pendingLength)));
|
|
pendingIdat = new Uint8Array(IDAT_CHUNK_SIZE);
|
|
pendingLength = 0;
|
|
};
|
|
const consumeCompressed = (async () => {
|
|
while (true) {
|
|
const { done, value } = await reader.read();
|
|
if (done) break;
|
|
let offset = 0;
|
|
while (offset < value.length) {
|
|
const copied = Math.min(IDAT_CHUNK_SIZE - pendingLength, value.length - offset);
|
|
pendingIdat.set(value.subarray(offset, offset + copied), pendingLength);
|
|
pendingLength += copied;
|
|
offset += copied;
|
|
if (pendingLength === IDAT_CHUNK_SIZE) flushIdat();
|
|
}
|
|
}
|
|
})();
|
|
|
|
try {
|
|
let expectedY = 0;
|
|
for await (const strip of strips) {
|
|
checkCancelled();
|
|
if (!Number.isSafeInteger(strip.y) || !Number.isSafeInteger(strip.height)
|
|
|| strip.height <= 0 || strip.y !== expectedY
|
|
|| strip.data.byteLength !== width * strip.height * 4) {
|
|
throw new Error('报告长图条带数据不连续或尺寸无效。');
|
|
}
|
|
if (strip.y + strip.height > height) throw new Error('报告长图条带超出图像范围。');
|
|
await writer.write(filteredRows(strip, width));
|
|
expectedY += strip.height;
|
|
}
|
|
checkCancelled();
|
|
if (expectedY !== height) throw new Error('报告长图内容未完整栅格化。');
|
|
await writer.close();
|
|
await consumeCompressed;
|
|
flushIdat();
|
|
parts.push(pngChunk('IEND', new Uint8Array()));
|
|
checkCancelled();
|
|
return new Blob(parts, { type: 'image/png' });
|
|
} catch (error) {
|
|
try { await writer.abort(error); } catch { /* stream may already be closed */ }
|
|
try { await reader.cancel(error); } catch { /* stream may already be closed */ }
|
|
try { await consumeCompressed; } catch { /* preserve the original error */ }
|
|
throw error;
|
|
} finally {
|
|
writer.releaseLock();
|
|
reader.releaseLock();
|
|
}
|
|
}
|
|
|
|
export const reportLongImagePixelLimit = MAX_REPORT_PIXELS;
|