camoucore 0.3.0 → 0.6.1

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