|
1 | 1 | import type * as types from "./types" |
2 | 2 | import * as protocol from "./stdio_protocol" |
| 3 | +import { JSON_parse } from "./uint8array_json_parser" |
3 | 4 |
|
4 | 5 | declare const ESBUILD_VERSION: string |
5 | 6 |
|
@@ -952,7 +953,7 @@ function buildOrContextImpl( |
952 | 953 | const originalErrors = result.errors.slice() |
953 | 954 | const originalWarnings = result.warnings.slice() |
954 | 955 | if (response!.outputFiles) result.outputFiles = response!.outputFiles.map(convertOutputFiles) |
955 | | - if (response!.metafile) result.metafile = JSON.parse(response!.metafile) |
| 956 | + if (response!.metafile) result.metafile = parseJSON(response!.metafile) |
956 | 957 | if (response!.mangleCache) result.mangleCache = response!.mangleCache |
957 | 958 | if (response!.writeToStdout !== void 0) console.log(protocol.decodeUTF8(response!.writeToStdout).replace(/\n$/, '')) |
958 | 959 | runOnEndCallbacks(result, (onEndErrors, onEndWarnings) => { |
@@ -1858,3 +1859,20 @@ function jsRegExpToGoRegExp(regexp: RegExp): string { |
1858 | 1859 | if (regexp.flags) result = `(?${regexp.flags})${result}` |
1859 | 1860 | return result |
1860 | 1861 | } |
| 1862 | + |
| 1863 | +function parseJSON(bytes: Uint8Array): any { |
| 1864 | + let text: string |
| 1865 | + try { |
| 1866 | + // This may fail in V8 with the error "Cannot create a string longer than |
| 1867 | + // 0x1fffffe8 characters". Other JS engines may have similar limitations. |
| 1868 | + text = protocol.decodeUTF8(bytes) |
| 1869 | + } catch { |
| 1870 | + // In that case, we attempt to parse the JSON ourselves directly from the |
| 1871 | + // Uint8Array. This bypasses the string length limit as we no longer need |
| 1872 | + // to construct a string that's the length of the input. However, doing |
| 1873 | + // this is likely significantly slower (perhaps around ~4x slower?), so we |
| 1874 | + // only do it if we have to. |
| 1875 | + return JSON_parse(bytes) |
| 1876 | + } |
| 1877 | + return JSON.parse(text) |
| 1878 | +} |
0 commit comments