UNPKG

jose

Version:

JWA, JWS, JWE, JWT, JWK, JWKS for Node.js, Browser, Cloudflare Workers, Deno, Bun, and other Web-interoperable runtimes

41 lines (40 loc) • 1.6 kB
import { JOSENotSupported, JWEInvalid } from "../util/errors.js"; import { concat } from "./buffer_utils.js"; function validateZip(joseHeader, protectedHeader) { if (joseHeader.zip !== void 0 && joseHeader.zip !== "DEF") throw new JOSENotSupported('Unsupported JWE "zip" (Compression Algorithm) Header Parameter value.'); if (joseHeader.zip !== void 0 && !protectedHeader?.zip) throw new JWEInvalid('JWE "zip" (Compression Algorithm) Header Parameter MUST be in a protected header.'); } function supported(name) { if (typeof globalThis[name] > "u") throw new JOSENotSupported(`JWE "zip" (Compression Algorithm) Header Parameter requires the ${name} API.`); } async function transform(stream, input, maxLength = 1 / 0) { const writer = stream.writable.getWriter(); writer.write(input).catch(() => { }), writer.close().catch(() => { }); const chunks = []; let length = 0; const reader = stream.readable.getReader(); for (; ; ) { const { value, done } = await reader.read(); if (done) break; if (chunks.push(value), length += value.byteLength, maxLength !== 1 / 0 && length > maxLength) throw new JWEInvalid("Decompressed plaintext exceeded the configured limit"); } return concat(...chunks); } async function compress(input) { return supported("CompressionStream"), transform(new CompressionStream("deflate-raw"), input); } async function decompress(input, maxLength) { return supported("DecompressionStream"), transform(new DecompressionStream("deflate-raw"), input, maxLength); } export { compress, decompress, validateZip };