camoucore 0.2.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. package/LICENSE +4 -3
  2. package/dist/camoufox/__main__.d.ts +10 -0
  3. package/dist/camoufox/__main__.js +1175 -0
  4. package/dist/camoufox/__version__.d.ts +31 -0
  5. package/dist/camoufox/__version__.js +32 -0
  6. package/dist/camoufox/_humanize_playwright.d.ts +54 -0
  7. package/dist/camoufox/_humanize_playwright.js +429 -0
  8. package/dist/camoufox/addons.d.ts +21 -0
  9. package/dist/camoufox/addons.js +56 -0
  10. package/dist/camoufox/async_api.d.ts +9 -0
  11. package/dist/camoufox/async_api.js +9 -0
  12. package/dist/camoufox/browser-pin.d.ts +46 -0
  13. package/dist/camoufox/browser-pin.js +96 -0
  14. package/dist/camoufox/cli.d.ts +7 -0
  15. package/dist/camoufox/cli.js +20 -0
  16. package/dist/camoufox/coherence.d.ts +141 -0
  17. package/dist/camoufox/coherence.js +708 -0
  18. package/dist/camoufox/data-files/audio-output-latency.json +15 -0
  19. package/dist/camoufox/data-files/browser-pin.json +7 -0
  20. package/dist/camoufox/data-files/display-profiles.json +6 -0
  21. package/dist/camoufox/data-files/essential-fonts.json +5 -0
  22. package/dist/camoufox/data-files/fingerprint-presets-v150.json +14544 -0
  23. package/dist/camoufox/data-files/fingerprint-presets.json +5192 -0
  24. package/dist/camoufox/data-files/font-bases.json +1888 -0
  25. package/dist/camoufox/data-files/font-groups.json +716 -0
  26. package/dist/camoufox/data-files/font-prefs.json +1046 -0
  27. package/dist/camoufox/data-files/fonts.json +240 -0
  28. package/dist/camoufox/data-files/fpgen.yml +44 -0
  29. package/dist/camoufox/data-files/launcher-constants.json +136 -0
  30. package/dist/camoufox/data-files/media-devices.json +180 -0
  31. package/dist/camoufox/data-files/repos.yml +40 -0
  32. package/dist/camoufox/data-files/voice-manifests.json +1864 -0
  33. package/dist/camoufox/data-files/voice-uris.json +188 -0
  34. package/dist/camoufox/data-files/warnings.yml +146 -0
  35. package/dist/camoufox/display_profile.d.ts +21 -0
  36. package/dist/camoufox/display_profile.js +129 -0
  37. package/dist/camoufox/driver_defaults.d.ts +27 -0
  38. package/dist/camoufox/driver_defaults.js +119 -0
  39. package/dist/camoufox/exceptions.d.ts +68 -0
  40. package/dist/camoufox/exceptions.js +122 -0
  41. package/dist/camoufox/fingerprints.d.ts +434 -0
  42. package/dist/camoufox/fingerprints.js +1936 -0
  43. package/dist/camoufox/gpudisplay.d.ts +158 -0
  44. package/dist/camoufox/gpudisplay.js +695 -0
  45. package/dist/camoufox/host_rendering.d.ts +38 -0
  46. package/dist/camoufox/host_rendering.js +180 -0
  47. package/dist/camoufox/humanize.d.ts +89 -0
  48. package/dist/camoufox/humanize.js +143 -0
  49. package/dist/camoufox/index.d.ts +21 -0
  50. package/dist/camoufox/index.js +21 -0
  51. package/dist/camoufox/ip.d.ts +45 -0
  52. package/dist/camoufox/ip.js +114 -0
  53. package/dist/camoufox/multiversion.d.ts +131 -0
  54. package/dist/camoufox/multiversion.js +490 -0
  55. package/dist/camoufox/node-floor.d.ts +5 -0
  56. package/dist/camoufox/node-floor.js +17 -0
  57. package/dist/camoufox/paths.d.ts +45 -0
  58. package/dist/camoufox/paths.js +62 -0
  59. package/dist/camoufox/pkgman.d.ts +361 -0
  60. package/dist/camoufox/pkgman.js +1066 -0
  61. package/dist/camoufox/product.d.ts +8 -0
  62. package/dist/camoufox/product.js +8 -0
  63. package/dist/camoufox/pycompat.d.ts +97 -0
  64. package/dist/camoufox/pycompat.js +458 -0
  65. package/dist/camoufox/server.d.ts +36 -0
  66. package/dist/camoufox/server.js +82 -0
  67. package/dist/camoufox/sync_api.d.ts +77 -0
  68. package/dist/camoufox/sync_api.js +207 -0
  69. package/dist/camoufox/utils.d.ts +638 -0
  70. package/dist/camoufox/utils.js +2507 -0
  71. package/dist/camoufox/warnings.d.ts +16 -0
  72. package/dist/camoufox/warnings.js +65 -0
  73. package/dist/camoufox/webgl.d.ts +52 -0
  74. package/dist/camoufox/webgl.js +264 -0
  75. package/dist/camouwolf/__main__.d.ts +10 -0
  76. package/dist/camouwolf/__main__.js +1180 -0
  77. package/dist/camouwolf/__version__.d.ts +31 -0
  78. package/dist/camouwolf/__version__.js +32 -0
  79. package/dist/camouwolf/_humanize_playwright.d.ts +53 -0
  80. package/dist/camouwolf/_humanize_playwright.js +427 -0
  81. package/dist/camouwolf/addons.d.ts +42 -0
  82. package/dist/camouwolf/addons.js +113 -0
  83. package/dist/camouwolf/async_api.d.ts +9 -0
  84. package/dist/camouwolf/async_api.js +9 -0
  85. package/dist/camouwolf/browser-pin.d.ts +46 -0
  86. package/dist/camouwolf/browser-pin.js +96 -0
  87. package/dist/camouwolf/cli.d.ts +7 -0
  88. package/dist/camouwolf/cli.js +20 -0
  89. package/dist/camouwolf/clienthints.d.ts +61 -0
  90. package/dist/camouwolf/clienthints.js +176 -0
  91. package/dist/camouwolf/coherence.d.ts +152 -0
  92. package/dist/camouwolf/coherence.js +554 -0
  93. package/dist/camouwolf/data-files/browser-pin.json +7 -0
  94. package/dist/camouwolf/data-files/camouwolf.defaults.json +118 -0
  95. package/dist/camouwolf/data-files/data/bundle-fonts.json +1983 -0
  96. package/dist/camouwolf/data-files/data/chrome-languages.json +517 -0
  97. package/dist/camouwolf/data-files/data/properties.json +321 -0
  98. package/dist/camouwolf/data-files/data/system-colors.json +35 -0
  99. package/dist/camouwolf/data-files/data/ubo.json +6 -0
  100. package/dist/camouwolf/data-files/fpgen.yml +41 -0
  101. package/dist/camouwolf/data-files/launcher-constants.json +26 -0
  102. package/dist/camouwolf/data-files/repos.yml +23 -0
  103. package/dist/camouwolf/data-files/warnings.yml +96 -0
  104. package/dist/camouwolf/driver_defaults.d.ts +25 -0
  105. package/dist/camouwolf/driver_defaults.js +114 -0
  106. package/dist/camouwolf/exceptions.d.ts +95 -0
  107. package/dist/camouwolf/exceptions.js +150 -0
  108. package/dist/camouwolf/fingerprints.d.ts +227 -0
  109. package/dist/camouwolf/fingerprints.js +753 -0
  110. package/dist/camouwolf/gpudisplay.d.ts +171 -0
  111. package/dist/camouwolf/gpudisplay.js +711 -0
  112. package/dist/camouwolf/host_rendering.d.ts +18 -0
  113. package/dist/camouwolf/host_rendering.js +114 -0
  114. package/dist/camouwolf/humanize.d.ts +84 -0
  115. package/dist/camouwolf/humanize.js +128 -0
  116. package/dist/camouwolf/index.d.ts +22 -0
  117. package/dist/camouwolf/index.js +21 -0
  118. package/dist/camouwolf/multiversion.d.ts +131 -0
  119. package/dist/camouwolf/multiversion.js +491 -0
  120. package/dist/camouwolf/node-floor.d.ts +5 -0
  121. package/dist/camouwolf/node-floor.js +17 -0
  122. package/dist/camouwolf/paths.d.ts +34 -0
  123. package/dist/camouwolf/paths.js +58 -0
  124. package/dist/camouwolf/pkgman.d.ts +331 -0
  125. package/dist/camouwolf/pkgman.js +972 -0
  126. package/dist/camouwolf/product.d.ts +3 -0
  127. package/dist/camouwolf/product.js +3 -0
  128. package/dist/camouwolf/pycompat.d.ts +92 -0
  129. package/dist/camouwolf/pycompat.js +461 -0
  130. package/dist/camouwolf/server.d.ts +36 -0
  131. package/dist/camouwolf/server.js +81 -0
  132. package/dist/camouwolf/switches.d.ts +77 -0
  133. package/dist/camouwolf/switches.js +222 -0
  134. package/dist/camouwolf/sync_api.d.ts +84 -0
  135. package/dist/camouwolf/sync_api.js +173 -0
  136. package/dist/camouwolf/utils.d.ts +644 -0
  137. package/dist/camouwolf/utils.js +2057 -0
  138. package/dist/camouwolf/warnings.d.ts +14 -0
  139. package/dist/camouwolf/warnings.js +59 -0
  140. package/dist/camouwolf/webgl.d.ts +41 -0
  141. package/dist/camouwolf/webgl.js +198 -0
  142. package/dist/data-files/antibot-rules.json +146 -0
  143. package/dist/data.d.ts +1 -1
  144. package/dist/data.js +4 -3
  145. package/dist/download.js +1 -1
  146. package/dist/exceptions.d.ts +1 -1
  147. package/dist/exceptions.js +1 -1
  148. package/dist/geolocation.js +1 -1
  149. package/dist/host.d.ts +2 -0
  150. package/dist/host.js +17 -1
  151. package/dist/ip.js +1 -1
  152. package/dist/locales.js +1 -1
  153. package/dist/mcp/cli.d.ts +15 -0
  154. package/dist/mcp/cli.js +108 -0
  155. package/dist/mcp/index.d.ts +8 -0
  156. package/dist/mcp/index.js +8 -0
  157. package/dist/mcp/main.d.ts +2 -0
  158. package/dist/mcp/main.js +8 -0
  159. package/dist/mcp/secrets.d.ts +50 -0
  160. package/dist/mcp/secrets.js +193 -0
  161. package/dist/mcp/server.d.ts +48 -0
  162. package/dist/mcp/server.js +653 -0
  163. package/dist/mcp/tree.d.ts +10 -0
  164. package/dist/mcp/tree.js +36 -0
  165. package/dist/passthrough.d.ts +16 -0
  166. package/dist/passthrough.js +50 -0
  167. package/dist/product.js +1 -1
  168. package/dist/settings.d.ts +12 -0
  169. package/dist/settings.js +95 -0
  170. package/dist/telemetry.d.ts +127 -0
  171. package/dist/telemetry.js +426 -0
  172. package/dist/virtdisplay.d.ts +1 -1
  173. package/dist/virtdisplay.js +1 -1
  174. package/dist/warnings.js +1 -1
  175. package/package.json +57 -13
@@ -0,0 +1,3 @@
1
+ /** Camouwolf as camoucore knows it: the name, cache directory and engine. */
2
+ import { Product } from "../index.js";
3
+ export declare const PRODUCT: Product;
@@ -0,0 +1,3 @@
1
+ /** Camouwolf as camoucore knows it: the name, cache directory and engine. */
2
+ import { CHROMIUM, Product } from "../index.js";
3
+ export const PRODUCT = new Product("camouwolf", "Camouwolf", CHROMIUM);
@@ -0,0 +1,92 @@
1
+ /**
2
+ * The handful of CPython / orjson behaviours the identity layer has to
3
+ * reproduce bit-for-bit so that the same inputs yield the same identity in the
4
+ * Python and TypeScript launchers: orjson's JSON bytes (hashed by
5
+ * identity_salt), str() of a value (hashed by identity_seed), zlib.crc32,
6
+ * code-point string ordering, and the float summation builtin sum() uses.
7
+ *
8
+ * JavaScript has one number type where Python has two, so `1.0` and `1` are
9
+ * the same value here. Where the difference reaches a hash, wrap the float in
10
+ * {@link PyFloat}, or read the JSON with {@link parsePyJson}, which keeps the
11
+ * distinction (and keeps integers above 2**53 exact as bigint).
12
+ */
13
+ import { KeyError, ValueError } from "./exceptions.js";
14
+ export { KeyError, NotImplementedError, ValueError } from "./exceptions.js";
15
+ /** A Python float whose value happens to be integral (1.0, 3.4e38...). */
16
+ export declare class PyFloat {
17
+ readonly value: number;
18
+ constructor(value: number);
19
+ valueOf(): number;
20
+ toJSON(): number;
21
+ toString(): string;
22
+ }
23
+ /** Number of a value that may be a PyFloat. */
24
+ export declare function num(value: unknown): number;
25
+ /** Whether `value` is a Python int (bool counts, as in Python). */
26
+ /** Python's round(x) to an integer: halves go to the even neighbour. */
27
+ export declare function pyRound(x: number): number;
28
+ export declare function isPyInt(value: unknown): boolean;
29
+ /**
30
+ * JSON.parse that keeps what Python's json.loads would: `1.0` stays a float
31
+ * (a {@link PyFloat} when integral) and an integer beyond 2**53 stays exact
32
+ * (a bigint).
33
+ */
34
+ export declare function parsePyJson(text: string): any;
35
+ /** A float exactly as orjson serializes it. */
36
+ export declare function formatOrjsonFloat(x: number): string;
37
+ /** repr(float) / str(float) in CPython. */
38
+ export declare function formatPyFloatRepr(x: number): string;
39
+ /** Compare two strings by code point, as Python (and UTF-8 bytes) order them. */
40
+ export declare function comparePyStr(a: string, b: string): number;
41
+ /** type(value).__name__ for a JSON-ish value. */
42
+ export declare function pyTypeName(value: unknown): string;
43
+ /** isinstance(value, dict) for a JSON-like value. */
44
+ export declare function isPlainObject(value: unknown): value is Record<string, any>;
45
+ /** Python truthiness of a JSON-like value. */
46
+ export declare function pyTruthy(value: unknown): boolean;
47
+ /** repr() of a str. */
48
+ export declare function pyStrRepr(s: string): string;
49
+ /** repr() of a JSON-like value. */
50
+ export declare function pyRepr(value: unknown): string;
51
+ /** str() of a JSON-like value. */
52
+ export declare function pyStr(value: unknown): string;
53
+ /**
54
+ * orjson.dumps(value, option=OPT_SORT_KEYS | OPT_NON_STR_KEYS, default=str),
55
+ * as a string (its UTF-8 encoding is orjson's bytes).
56
+ *
57
+ * Plain numbers that are safe integers serialize as Python ints, every other
58
+ * number as a float; use {@link PyFloat} for an integral float and bigint for
59
+ * an integer beyond 2**53. An object with a `toPyDict()` method (the TS twin
60
+ * of a dataclass) serializes as that dict.
61
+ */
62
+ export declare function orjsonDumps(value: unknown, sortKeys?: boolean): string;
63
+ /**
64
+ * sum() over floats, as CPython 3.12+ computes it (Neumaier-compensated).
65
+ * CPython 3.10/3.11 fold left without compensation; the two differ only in
66
+ * the last bit, and only for sums that lose precision.
67
+ */
68
+ export declare function pySumFloats(values: Iterable<number>): number;
69
+ /**
70
+ * sum() over a mix of ints and floats, as CPython 3.14 computes it: ints add
71
+ * exactly until the first float, which is added plainly; after that every item
72
+ * is Neumaier-compensated. (3.12/3.13 add those later ints without
73
+ * compensation and 3.10/3.11 compensate nothing, so a sum that loses
74
+ * precision can differ in its last bit there.) A safe
75
+ * integer number counts as an int here, anything else (or a PyFloat) as a
76
+ * float. Returns a number, or a bigint when every item was an int that only
77
+ * a bigint holds exactly.
78
+ */
79
+ export declare function pySum(values: Iterable<unknown>): number | bigint;
80
+ /** zlib.crc32. */
81
+ export declare function crc32(data: Uint8Array | string): number;
82
+ /** Python's OSError, for failures Node does not raise as a system error. */
83
+ export declare class OSError extends Error {
84
+ name: string;
85
+ }
86
+ declare const PY_ERROR_TESTS: {
87
+ OSError: (e: unknown) => boolean;
88
+ ValueError: (e: unknown) => e is SyntaxError | ValueError;
89
+ KeyError: (e: unknown) => e is KeyError;
90
+ };
91
+ /** Whether `except (<kinds>)` in the Python twin would catch `error`. */
92
+ export declare function isPyError(error: unknown, ...kinds: Array<keyof typeof PY_ERROR_TESTS>): boolean;
@@ -0,0 +1,461 @@
1
+ /**
2
+ * The handful of CPython / orjson behaviours the identity layer has to
3
+ * reproduce bit-for-bit so that the same inputs yield the same identity in the
4
+ * Python and TypeScript launchers: orjson's JSON bytes (hashed by
5
+ * identity_salt), str() of a value (hashed by identity_seed), zlib.crc32,
6
+ * code-point string ordering, and the float summation builtin sum() uses.
7
+ *
8
+ * JavaScript has one number type where Python has two, so `1.0` and `1` are
9
+ * the same value here. Where the difference reaches a hash, wrap the float in
10
+ * {@link PyFloat}, or read the JSON with {@link parsePyJson}, which keeps the
11
+ * distinction (and keeps integers above 2**53 exact as bigint).
12
+ */
13
+ import { FileNotFoundError, KeyError, ValueError } from "./exceptions.js";
14
+ export { KeyError, NotImplementedError, ValueError } from "./exceptions.js";
15
+ /** A Python float whose value happens to be integral (1.0, 3.4e38...). */
16
+ export class PyFloat {
17
+ value;
18
+ constructor(value) {
19
+ this.value = value;
20
+ }
21
+ valueOf() {
22
+ return this.value;
23
+ }
24
+ toJSON() {
25
+ return this.value;
26
+ }
27
+ toString() {
28
+ return formatPyFloatRepr(this.value);
29
+ }
30
+ }
31
+ /** Number of a value that may be a PyFloat. */
32
+ export function num(value) {
33
+ return value instanceof PyFloat ? value.value : value;
34
+ }
35
+ /** Whether `value` is a Python int (bool counts, as in Python). */
36
+ /** Python's round(x) to an integer: halves go to the even neighbour. */
37
+ export function pyRound(x) {
38
+ const floor = Math.floor(x);
39
+ const diff = x - floor;
40
+ if (diff < 0.5)
41
+ return floor;
42
+ if (diff > 0.5)
43
+ return floor + 1;
44
+ return floor % 2 === 0 ? floor : floor + 1;
45
+ }
46
+ export function isPyInt(value) {
47
+ return (typeof value === "bigint" ||
48
+ typeof value === "boolean" ||
49
+ (typeof value === "number" && Number.isSafeInteger(value)));
50
+ }
51
+ /**
52
+ * JSON.parse that keeps what Python's json.loads would: `1.0` stays a float
53
+ * (a {@link PyFloat} when integral) and an integer beyond 2**53 stays exact
54
+ * (a bigint).
55
+ */
56
+ export function parsePyJson(text) {
57
+ return JSON.parse(text, function (_key, value, context) {
58
+ if (typeof value !== "number" || !context?.source)
59
+ return value;
60
+ const src = context.source;
61
+ const isFloat = /[.eE]/.test(src);
62
+ if (!isFloat) {
63
+ return Number.isSafeInteger(value) ? value : BigInt(src);
64
+ }
65
+ return Number.isSafeInteger(value) ? new PyFloat(value) : value;
66
+ });
67
+ }
68
+ /** Shortest round-trip digits and decimal exponent of a finite, non-zero x. */
69
+ function shortestDigits(x) {
70
+ const [mantissa, exponent] = Math.abs(x).toExponential().split("e");
71
+ return { digits: mantissa.replace(".", ""), exp: Number(exponent) };
72
+ }
73
+ function fixedNotation(digits, exp) {
74
+ if (exp >= 0) {
75
+ if (digits.length <= exp + 1) {
76
+ return `${digits}${"0".repeat(exp + 1 - digits.length)}.0`;
77
+ }
78
+ return `${digits.slice(0, exp + 1)}.${digits.slice(exp + 1)}`;
79
+ }
80
+ return `0.${"0".repeat(-exp - 1)}${digits}`;
81
+ }
82
+ function sciMantissa(digits) {
83
+ return digits.length === 1 ? digits : `${digits[0]}.${digits.slice(1)}`;
84
+ }
85
+ /** A float exactly as orjson serializes it. */
86
+ export function formatOrjsonFloat(x) {
87
+ if (!Number.isFinite(x))
88
+ return "null";
89
+ if (x === 0)
90
+ return Object.is(x, -0) ? "-0.0" : "0.0";
91
+ const sign = x < 0 ? "-" : "";
92
+ const { digits, exp } = shortestDigits(x);
93
+ if (exp >= -5 && exp < 16)
94
+ return sign + fixedNotation(digits, exp);
95
+ return `${sign}${sciMantissa(digits)}e${exp < 0 ? "-" : "+"}${Math.abs(exp)}`;
96
+ }
97
+ /** repr(float) / str(float) in CPython. */
98
+ export function formatPyFloatRepr(x) {
99
+ if (Number.isNaN(x))
100
+ return "nan";
101
+ if (!Number.isFinite(x))
102
+ return x > 0 ? "inf" : "-inf";
103
+ if (x === 0)
104
+ return Object.is(x, -0) ? "-0.0" : "0.0";
105
+ const sign = x < 0 ? "-" : "";
106
+ const { digits, exp } = shortestDigits(x);
107
+ if (exp >= -4 && exp < 16)
108
+ return sign + fixedNotation(digits, exp);
109
+ const e = Math.abs(exp).toString().padStart(2, "0");
110
+ return `${sign}${sciMantissa(digits)}e${exp < 0 ? "-" : "+"}${e}`;
111
+ }
112
+ /** Compare two strings by code point, as Python (and UTF-8 bytes) order them. */
113
+ export function comparePyStr(a, b) {
114
+ if (a === b)
115
+ return 0;
116
+ const n = Math.min(a.length, b.length);
117
+ for (let i = 0; i < n; i++) {
118
+ const ca = a.charCodeAt(i);
119
+ const cb = b.charCodeAt(i);
120
+ if (ca === cb)
121
+ continue;
122
+ // Surrogates (0xD800-0xDFFF) encode code points above every other
123
+ // BMP unit, so only the surrogate-vs-high-BMP case reorders.
124
+ const sa = ca >= 0xd800 && ca <= 0xdfff;
125
+ const sb = cb >= 0xd800 && cb <= 0xdfff;
126
+ if (sa !== sb) {
127
+ if (sa)
128
+ return cb >= 0xe000 ? 1 : ca - cb;
129
+ return ca >= 0xe000 ? -1 : ca - cb;
130
+ }
131
+ return ca - cb;
132
+ }
133
+ return a.length - b.length;
134
+ }
135
+ /** type(value).__name__ for a JSON-ish value. */
136
+ export function pyTypeName(value) {
137
+ if (value === null || value === undefined)
138
+ return "NoneType";
139
+ if (typeof value === "boolean")
140
+ return "bool";
141
+ if (value instanceof PyFloat)
142
+ return "float";
143
+ if (typeof value === "number")
144
+ return Number.isInteger(value) ? "int" : "float";
145
+ if (typeof value === "bigint")
146
+ return "int";
147
+ if (typeof value === "string")
148
+ return "str";
149
+ if (Array.isArray(value))
150
+ return "list";
151
+ return "dict";
152
+ }
153
+ /** isinstance(value, dict) for a JSON-like value. */
154
+ export function isPlainObject(value) {
155
+ if (value === null || typeof value !== "object")
156
+ return false;
157
+ const proto = Object.getPrototypeOf(value);
158
+ return proto === Object.prototype || proto === null;
159
+ }
160
+ /** Python truthiness of a JSON-like value. */
161
+ export function pyTruthy(value) {
162
+ if (value === null || value === undefined)
163
+ return false;
164
+ if (value instanceof PyFloat)
165
+ return value.value !== 0;
166
+ if (typeof value === "number")
167
+ return value !== 0;
168
+ if (typeof value === "bigint")
169
+ return value !== 0n;
170
+ if (typeof value === "string")
171
+ return value.length > 0;
172
+ if (typeof value === "boolean")
173
+ return value;
174
+ if (Array.isArray(value))
175
+ return value.length > 0;
176
+ if (value instanceof Map || value instanceof Set)
177
+ return value.size > 0;
178
+ if (typeof value === "object")
179
+ return Object.keys(value).length > 0;
180
+ return true;
181
+ }
182
+ const NON_PRINTABLE = /[\p{C}\p{Z}]/u;
183
+ /** repr() of a str. */
184
+ export function pyStrRepr(s) {
185
+ const quote = s.includes("'") && !s.includes('"') ? '"' : "'";
186
+ let out = quote;
187
+ for (const ch of s) {
188
+ const cp = ch.codePointAt(0);
189
+ if (ch === quote || ch === "\\")
190
+ out += `\\${ch}`;
191
+ else if (ch === "\t")
192
+ out += "\\t";
193
+ else if (ch === "\n")
194
+ out += "\\n";
195
+ else if (ch === "\r")
196
+ out += "\\r";
197
+ else if (cp < 0x20 || cp === 0x7f)
198
+ out += `\\x${cp.toString(16).padStart(2, "0")}`;
199
+ else if (ch !== " " && NON_PRINTABLE.test(ch)) {
200
+ if (cp <= 0xff)
201
+ out += `\\x${cp.toString(16).padStart(2, "0")}`;
202
+ else if (cp <= 0xffff)
203
+ out += `\\u${cp.toString(16).padStart(4, "0")}`;
204
+ else
205
+ out += `\\U${cp.toString(16).padStart(8, "0")}`;
206
+ }
207
+ else
208
+ out += ch;
209
+ }
210
+ return out + quote;
211
+ }
212
+ function pyNumberStr(value) {
213
+ if (Number.isSafeInteger(value))
214
+ return String(value);
215
+ return formatPyFloatRepr(value);
216
+ }
217
+ /** repr() of a JSON-like value. */
218
+ export function pyRepr(value) {
219
+ if (typeof value === "string")
220
+ return pyStrRepr(value);
221
+ if (Array.isArray(value))
222
+ return `[${value.map(pyRepr).join(", ")}]`;
223
+ if (value instanceof PyFloat)
224
+ return formatPyFloatRepr(value.value);
225
+ if (value !== null && typeof value === "object") {
226
+ const entries = value instanceof Map ? [...value.entries()] : Object.entries(value);
227
+ return `{${entries.map(([k, v]) => `${pyRepr(k)}: ${pyRepr(v)}`).join(", ")}}`;
228
+ }
229
+ return pyStr(value);
230
+ }
231
+ /** str() of a JSON-like value. */
232
+ export function pyStr(value) {
233
+ if (value === null || value === undefined)
234
+ return "None";
235
+ if (value === true)
236
+ return "True";
237
+ if (value === false)
238
+ return "False";
239
+ if (typeof value === "string")
240
+ return value;
241
+ if (typeof value === "bigint")
242
+ return value.toString();
243
+ if (typeof value === "number")
244
+ return pyNumberStr(value);
245
+ if (value instanceof PyFloat)
246
+ return formatPyFloatRepr(value.value);
247
+ return pyRepr(value);
248
+ }
249
+ function orjsonString(s) {
250
+ let out = '"';
251
+ for (let i = 0; i < s.length; i++) {
252
+ const c = s.charCodeAt(i);
253
+ if (c === 0x22)
254
+ out += '\\"';
255
+ else if (c === 0x5c)
256
+ out += "\\\\";
257
+ else if (c < 0x20) {
258
+ switch (c) {
259
+ case 0x08:
260
+ out += "\\b";
261
+ break;
262
+ case 0x09:
263
+ out += "\\t";
264
+ break;
265
+ case 0x0a:
266
+ out += "\\n";
267
+ break;
268
+ case 0x0c:
269
+ out += "\\f";
270
+ break;
271
+ case 0x0d:
272
+ out += "\\r";
273
+ break;
274
+ default:
275
+ out += `\\u${c.toString(16).padStart(4, "0")}`;
276
+ }
277
+ }
278
+ else
279
+ out += s[i];
280
+ }
281
+ return `${out}"`;
282
+ }
283
+ function orjsonKey(key) {
284
+ if (typeof key === "string")
285
+ return key;
286
+ if (key === null || key === undefined)
287
+ return "null";
288
+ if (typeof key === "boolean")
289
+ return key ? "true" : "false";
290
+ if (typeof key === "bigint")
291
+ return key.toString();
292
+ if (key instanceof PyFloat)
293
+ return formatOrjsonFloat(key.value);
294
+ if (typeof key === "number") {
295
+ return Number.isSafeInteger(key) ? String(key) : formatOrjsonFloat(key);
296
+ }
297
+ return pyStr(key);
298
+ }
299
+ /**
300
+ * orjson.dumps(value, option=OPT_SORT_KEYS | OPT_NON_STR_KEYS, default=str),
301
+ * as a string (its UTF-8 encoding is orjson's bytes).
302
+ *
303
+ * Plain numbers that are safe integers serialize as Python ints, every other
304
+ * number as a float; use {@link PyFloat} for an integral float and bigint for
305
+ * an integer beyond 2**53. An object with a `toPyDict()` method (the TS twin
306
+ * of a dataclass) serializes as that dict.
307
+ */
308
+ export function orjsonDumps(value, sortKeys = true) {
309
+ if (value === null || value === undefined)
310
+ return "null";
311
+ if (value === true)
312
+ return "true";
313
+ if (value === false)
314
+ return "false";
315
+ if (typeof value === "string")
316
+ return orjsonString(value);
317
+ if (typeof value === "bigint")
318
+ return value.toString();
319
+ if (typeof value === "number") {
320
+ return Number.isSafeInteger(value)
321
+ ? String(value)
322
+ : formatOrjsonFloat(value);
323
+ }
324
+ if (value instanceof PyFloat)
325
+ return formatOrjsonFloat(value.value);
326
+ if (Array.isArray(value)) {
327
+ return `[${value.map((v) => orjsonDumps(v, sortKeys)).join(",")}]`;
328
+ }
329
+ if (typeof value === "object") {
330
+ const withDict = value;
331
+ if (typeof withDict.toPyDict === "function") {
332
+ return orjsonDumps(withDict.toPyDict(), sortKeys);
333
+ }
334
+ if (value instanceof Set) {
335
+ return orjsonString(pyStr([...value]));
336
+ }
337
+ let entries;
338
+ if (value instanceof Map) {
339
+ entries = [...value.entries()].map(([k, v]) => [orjsonKey(k), v]);
340
+ }
341
+ else if (Object.getPrototypeOf(value) === Object.prototype ||
342
+ Object.getPrototypeOf(value) === null) {
343
+ entries = Object.entries(value);
344
+ }
345
+ else {
346
+ // default=str
347
+ return orjsonString(String(value));
348
+ }
349
+ if (sortKeys)
350
+ entries.sort((a, b) => comparePyStr(a[0], b[0]));
351
+ return `{${entries.map(([k, v]) => `${orjsonString(k)}:${orjsonDumps(v, sortKeys)}`).join(",")}}`;
352
+ }
353
+ return orjsonString(String(value));
354
+ }
355
+ /**
356
+ * sum() over floats, as CPython 3.12+ computes it (Neumaier-compensated).
357
+ * CPython 3.10/3.11 fold left without compensation; the two differ only in
358
+ * the last bit, and only for sums that lose precision.
359
+ */
360
+ export function pySumFloats(values) {
361
+ let started = false;
362
+ let f = 0;
363
+ let c = 0;
364
+ for (const x of values) {
365
+ if (!started) {
366
+ // int 0 start + first float: a plain add
367
+ f = 0 + x;
368
+ started = true;
369
+ continue;
370
+ }
371
+ const t = f + x;
372
+ if (Math.abs(f) >= Math.abs(x))
373
+ c += f - t + x;
374
+ else
375
+ c += x - t + f;
376
+ f = t;
377
+ }
378
+ if (c !== 0 && Number.isFinite(c))
379
+ f += c;
380
+ return f;
381
+ }
382
+ /**
383
+ * sum() over a mix of ints and floats, as CPython 3.14 computes it: ints add
384
+ * exactly until the first float, which is added plainly; after that every item
385
+ * is Neumaier-compensated. (3.12/3.13 add those later ints without
386
+ * compensation and 3.10/3.11 compensate nothing, so a sum that loses
387
+ * precision can differ in its last bit there.) A safe
388
+ * integer number counts as an int here, anything else (or a PyFloat) as a
389
+ * float. Returns a number, or a bigint when every item was an int that only
390
+ * a bigint holds exactly.
391
+ */
392
+ export function pySum(values) {
393
+ let i = 0n;
394
+ let f = 0;
395
+ let c = 0;
396
+ for (const v of values) {
397
+ const isInt = typeof v === "bigint" ||
398
+ typeof v === "boolean" ||
399
+ (typeof v === "number" && Number.isSafeInteger(v));
400
+ if (i !== null) {
401
+ if (isInt) {
402
+ i += typeof v === "bigint" ? v : BigInt(Number(v));
403
+ continue;
404
+ }
405
+ f = Number(i) + num(v);
406
+ i = null;
407
+ continue;
408
+ }
409
+ const x = isInt ? Number(v) : num(v);
410
+ const t = f + x;
411
+ if (Math.abs(f) >= Math.abs(x))
412
+ c += f - t + x;
413
+ else
414
+ c += x - t + f;
415
+ f = t;
416
+ }
417
+ if (i !== null)
418
+ return Number.isSafeInteger(Number(i)) ? Number(i) : i;
419
+ if (c !== 0 && Number.isFinite(c))
420
+ f += c;
421
+ return f;
422
+ }
423
+ let CRC_TABLE = null;
424
+ /** zlib.crc32. */
425
+ export function crc32(data) {
426
+ if (!CRC_TABLE) {
427
+ CRC_TABLE = new Uint32Array(256);
428
+ for (let n = 0; n < 256; n++) {
429
+ let c = n;
430
+ for (let k = 0; k < 8; k++)
431
+ c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
432
+ CRC_TABLE[n] = c >>> 0;
433
+ }
434
+ }
435
+ const bytes = typeof data === "string" ? new TextEncoder().encode(data) : data;
436
+ let crc = 0xffffffff;
437
+ for (let i = 0; i < bytes.length; i++) {
438
+ crc = CRC_TABLE[(crc ^ bytes[i]) & 0xff] ^ (crc >>> 8);
439
+ }
440
+ return (crc ^ 0xffffffff) >>> 0;
441
+ }
442
+ /** Python's OSError, for failures Node does not raise as a system error. */
443
+ export class OSError extends Error {
444
+ name = "OSError";
445
+ }
446
+ const PY_ERROR_TESTS = {
447
+ // Node's system errors (fs, net) carry a string code and a numeric errno;
448
+ // FileNotFoundError is an OSError in Python.
449
+ OSError: (e) => e instanceof OSError ||
450
+ e instanceof FileNotFoundError ||
451
+ (e instanceof Error &&
452
+ typeof e.code === "string" &&
453
+ typeof e.errno === "number"),
454
+ // JSON.parse's SyntaxError is json.JSONDecodeError, a ValueError.
455
+ ValueError: (e) => e instanceof ValueError || e instanceof SyntaxError,
456
+ KeyError: (e) => e instanceof KeyError,
457
+ };
458
+ /** Whether `except (<kinds>)` in the Python twin would catch `error`. */
459
+ export function isPyError(error, ...kinds) {
460
+ return kinds.some((kind) => PY_ERROR_TESTS[kind](error));
461
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Playwright server mode.
3
+ *
4
+ * TypeScript twin of python/camoucore/src/camoucore/camouwolf/server.py. Python has to shell out to
5
+ * the Node runtime bundled with its Playwright driver (and hand it a base64
6
+ * config frame over stdin, via launchServer.js) because there is no Python
7
+ * binding for BrowserServer. Here we already are that runtime, so this calls
8
+ * playwright-core's launchServer() directly with the same options Python
9
+ * would send: launchOptions() with every top-level key camelCased.
10
+ */
11
+ import { type BrowserServer } from "playwright-core";
12
+ import { type GpuDisplayOptions, type Headless, type LaunchOptions } from "./utils.js";
13
+ export interface LaunchServerOptions extends Omit<LaunchOptions, "headless">, GpuDisplayOptions {
14
+ /** Port to listen on. Defaults to a random free port. */
15
+ port?: number;
16
+ /** Path of the websocket endpoint. Defaults to a random path. */
17
+ ws_path?: string;
18
+ /** Whether to run the browser headless. `"virtual"` spawns an Xvfb display,
19
+ * `"virtual-gpu"` a headless Xorg server bound to one GPU. */
20
+ headless?: Headless;
21
+ }
22
+ /**
23
+ * Convert a dictionary's keys to camelCase (server.to_camel_case_dict). Keys
24
+ * without an underscore are already JS names and are kept as they are.
25
+ */
26
+ export declare function toCamelCaseDict(data: Record<string, any>): Record<string, any>;
27
+ /**
28
+ * Launch a Playwright server. Takes the same options as `Camouwolf()`.
29
+ *
30
+ * Note: persistent contexts are not servable. Playwright's `launchServer`
31
+ * routes through `BrowserType.launch()`, and its `PlaywrightServer` only
32
+ * accepts a pre-launched Browser -- there is no way to expose a persistent
33
+ * BrowserContext over a websocket endpoint. Reject those options up front
34
+ * rather than accepting them and silently launching a throwaway profile.
35
+ */
36
+ export declare function launchServer({ headless, ...options }?: LaunchServerOptions): Promise<BrowserServer>;
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Playwright server mode.
3
+ *
4
+ * TypeScript twin of python/camoucore/src/camoucore/camouwolf/server.py. Python has to shell out to
5
+ * the Node runtime bundled with its Playwright driver (and hand it a base64
6
+ * config frame over stdin, via launchServer.js) because there is no Python
7
+ * binding for BrowserServer. Here we already are that runtime, so this calls
8
+ * playwright-core's launchServer() directly with the same options Python
9
+ * would send: launchOptions() with every top-level key camelCased.
10
+ */
11
+ import { chromium } from "playwright-core";
12
+ import { withUnpinnedLaunch } from "../cpu_affinity.js";
13
+ import { ValueError } from "./exceptions.js";
14
+ import { GpuDisplay, proveGpu } from "./gpudisplay.js";
15
+ import { drivenChannels } from "./humanize.js";
16
+ import { camelCase } from "./sync_api.js";
17
+ import { launchOptions, startVirtualDisplay, } from "./utils.js";
18
+ /**
19
+ * Convert a dictionary's keys to camelCase (server.to_camel_case_dict). Keys
20
+ * without an underscore are already JS names and are kept as they are.
21
+ */
22
+ export function toCamelCaseDict(data) {
23
+ const out = {};
24
+ for (const [key, value] of Object.entries(data)) {
25
+ out[key.includes("_") ? camelCase(key) : key] = value;
26
+ }
27
+ return out;
28
+ }
29
+ /**
30
+ * Launch a Playwright server. Takes the same options as `Camouwolf()`.
31
+ *
32
+ * Note: persistent contexts are not servable. Playwright's `launchServer`
33
+ * routes through `BrowserType.launch()`, and its `PlaywrightServer` only
34
+ * accepts a pre-launched Browser -- there is no way to expose a persistent
35
+ * BrowserContext over a websocket endpoint. Reject those options up front
36
+ * rather than accepting them and silently launching a throwaway profile.
37
+ */
38
+ export async function launchServer({ headless, ...options } = {}) {
39
+ for (const unsupported of ["persistent_context", "user_data_dir"]) {
40
+ if (options[unsupported]) {
41
+ throw new ValueError(`launch_server() does not support '${unsupported}': Playwright cannot ` +
42
+ "serve a persistent context over a websocket endpoint. Use " +
43
+ "Camouwolf(persistent_context=True, ...) in-process instead.");
44
+ }
45
+ delete options[unsupported];
46
+ }
47
+ if (drivenChannels(options.humanize).length)
48
+ throw new ValueError("launch_server() cannot run humanize engines: they run in the process that drives " +
49
+ "the page. Serve without humanize, and on the client call " +
50
+ "attach(browser, humanize, os) on the connected browser.");
51
+ const [virtualDisplay, headlessBool] = await startVirtualDisplay(headless, options.debug, options);
52
+ try {
53
+ const config = await launchOptions({ ...options, headless: headlessBool });
54
+ // The server's browser is spawned from this process too, so it must not
55
+ // start inside another launch's CPU pin.
56
+ const server = await withUnpinnedLaunch(() => chromium.launchServer(toCamelCaseDict(config)));
57
+ if (virtualDisplay) {
58
+ // BrowserServer has no "disconnected" event; "close" fires on shutdown.
59
+ const display = virtualDisplay;
60
+ server.on("close", () => display.kill());
61
+ }
62
+ if (virtualDisplay instanceof GpuDisplay) {
63
+ const browser = await chromium.connect(server.wsEndpoint());
64
+ try {
65
+ await proveGpu(browser, virtualDisplay);
66
+ }
67
+ catch (error) {
68
+ await server.close();
69
+ throw error;
70
+ }
71
+ finally {
72
+ await browser.close();
73
+ }
74
+ }
75
+ return server;
76
+ }
77
+ catch (error) {
78
+ virtualDisplay?.kill();
79
+ throw error;
80
+ }
81
+ }