{
  "version": 3,
  "sources": ["detect.js"],
  "sourcesContent": ["// Copyright 2026 will Farrell, and datastream contributors.\n// SPDX-License-Identifier: MIT\nimport { createPassThroughStream } from \"@datastream/core\";\nimport { analyse } from \"chardet\";\n\nconst charsetKeys = [\n\t\"UTF-8\",\n\t\"UTF-16BE\",\n\t\"UTF-16LE\",\n\t\"UTF-32BE\",\n\t\"UTF-32LE\",\n\t\"Shift_JIS\",\n\t\"ISO-2022-JP\",\n\t\"ISO-2022-CN\",\n\t\"ISO-2022-KR\",\n\t\"GB18030\",\n\t\"EUC-JP\",\n\t\"EUC-KR\",\n\t\"Big5\",\n\t\"ISO-8859-1\",\n\t\"ISO-8859-2\",\n\t\"ISO-8859-5\",\n\t\"ISO-8859-6\",\n\t\"ISO-8859-7\",\n\t\"ISO-8859-8\",\n\t\"windows-1251\",\n\t\"windows-1256\",\n\t\"windows-1252\",\n\t\"windows-1254\",\n\t\"windows-1250\",\n\t\"KOI8-R\",\n\t\"ISO-8859-9\",\n];\n\n// Cap the detection sample so we never buffer an unbounded amount of the\n// stream. chardet analyses a representative prefix; 64KB is plenty.\nconst MAX_DETECTION_SAMPLE = 64 * 1024;\n\n// chardet reports pure-ASCII input as \"ASCII\" (often at confidence 100). ASCII\n// is a strict subset of UTF-8, so fold an ASCII match into the UTF-8 bucket\n// rather than discarding the highest-confidence result and reporting a\n// spurious ISO-8859-1 winner.\nconst normaliseMatchName = (name) => (name === \"ASCII\" ? \"UTF-8\" : name);\n\n// Concatenate the sampled Uint8Array chunks without relying on the node-only\n// Buffer global, so the shared detect source runs in the browser too. String\n// chunks are encoded as UTF-8 bytes via TextEncoder for the same reason.\nconst concatBytes = (chunks, totalLength) => {\n\tconst out = new Uint8Array(totalLength);\n\tlet offset = 0;\n\tfor (const chunk of chunks) {\n\t\tout.set(chunk, offset);\n\t\toffset += chunk.length;\n\t}\n\treturn out;\n};\n\nexport const charsetDetectStream = ({ resultKey } = {}, streamOptions = {}) => {\n\t// Accumulate a bounded byte sample and run chardet once on the whole sample\n\t// in result(). Running analyse() per-chunk and averaging corrupts results at\n\t// multibyte chunk boundaries (a split sequence mis-detects each fragment).\n\tconst sample = [];\n\tlet sampleLength = 0;\n\tconst encoder = new TextEncoder();\n\tconst passThrough = (chunk) => {\n\t\tconst bytes = typeof chunk === \"string\" ? encoder.encode(chunk) : chunk;\n\t\t// Keep only the bytes that still fit under the cap. subarray clamps the\n\t\t// end index to the array length, so this both passes short chunks through\n\t\t// whole and truncates the one chunk that crosses MAX_DETECTION_SAMPLE;\n\t\t// once the cap is reached remaining is 0 and the slice is empty, so no\n\t\t// further bytes are ever sampled. This single bound makes the cap the\n\t\t// sole gate (no redundant length guard that a slice would mask).\n\t\tconst remaining = MAX_DETECTION_SAMPLE - sampleLength;\n\t\tconst slice = bytes.subarray(0, remaining);\n\t\tsample.push(slice);\n\t\tsampleLength += slice.length;\n\t};\n\tconst stream = createPassThroughStream(passThrough, streamOptions);\n\tstream.result = () => {\n\t\t// No bytes seen: signal \"nothing to detect\" rather than a phantom guess so\n\t\t// callers can distinguish empty input from a low-confidence real result.\n\t\tif (!sampleLength) {\n\t\t\treturn {\n\t\t\t\tkey: resultKey ?? \"charset\",\n\t\t\t\tvalue: { charset: undefined, confidence: 0 },\n\t\t\t};\n\t\t}\n\t\tconst charsets = Object.fromEntries(charsetKeys.map((k) => [k, undefined]));\n\t\tconst matches = analyse(concatBytes(sample, sampleLength));\n\t\tfor (const match of matches) {\n\t\t\tconst name = normaliseMatchName(match.name);\n\t\t\tif (name in charsets) {\n\t\t\t\tcharsets[name] = Math.max(charsets[name] ?? 0, match.confidence);\n\t\t\t}\n\t\t}\n\t\tconst values = Object.entries(charsets)\n\t\t\t.map(([charset, confidence]) => ({\n\t\t\t\tcharset,\n\t\t\t\tconfidence: confidence ?? 0,\n\t\t\t}))\n\t\t\t.sort((a, b) => b.confidence - a.confidence);\n\t\treturn { key: resultKey ?? \"charset\", value: values[0] };\n\t};\n\treturn stream;\n};\n\nexport const getSupportedEncoding = (charset) => {\n\tif (charset === \"ISO-8859-8-I\") charset = \"ISO-8859-8\";\n\treturn charset;\n};\n\nexport default charsetDetectStream;\n"],
  "mappings": "AAEA,SAAS,+BAA+B;AACxC,SAAS,eAAe;AAExB,MAAM,cAAc;AAAA,EACnB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD;AAIA,MAAM,uBAAuB,KAAK;AAMlC,MAAM,qBAAqB,CAAC,SAAU,SAAS,UAAU,UAAU;AAKnE,MAAM,cAAc,CAAC,QAAQ,gBAAgB;AAC5C,QAAM,MAAM,IAAI,WAAW,WAAW;AACtC,MAAI,SAAS;AACb,aAAW,SAAS,QAAQ;AAC3B,QAAI,IAAI,OAAO,MAAM;AACrB,cAAU,MAAM;AAAA,EACjB;AACA,SAAO;AACR;AAEO,MAAM,sBAAsB,CAAC,EAAE,UAAU,IAAI,CAAC,GAAG,gBAAgB,CAAC,MAAM;AAI9E,QAAM,SAAS,CAAC;AAChB,MAAI,eAAe;AACnB,QAAM,UAAU,IAAI,YAAY;AAChC,QAAM,cAAc,CAAC,UAAU;AAC9B,UAAM,QAAQ,OAAO,UAAU,WAAW,QAAQ,OAAO,KAAK,IAAI;AAOlE,UAAM,YAAY,uBAAuB;AACzC,UAAM,QAAQ,MAAM,SAAS,GAAG,SAAS;AACzC,WAAO,KAAK,KAAK;AACjB,oBAAgB,MAAM;AAAA,EACvB;AACA,QAAM,SAAS,wBAAwB,aAAa,aAAa;AACjE,SAAO,SAAS,MAAM;AAGrB,QAAI,CAAC,cAAc;AAClB,aAAO;AAAA,QACN,KAAK,aAAa;AAAA,QAClB,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