@luckylillia-dev/plugin-aiimgdetector 0.0.0-placeholder → 1.0.4
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.
- package/README.md +2 -2
- package/assets/demo.png +0 -0
- package/assets/icon.svg +15 -0
- package/assets/synthid-login-check.jpg +0 -0
- package/docs/details.md +45 -0
- package/docs/publishing.md +66 -0
- package/docs/synthid.md +80 -0
- package/lib/image.js +90 -0
- package/lib/index.js +207 -0
- package/lib/provenance.js +21 -0
- package/lib/synthid-auth.js +100 -0
- package/lib/synthid-browser.js +451 -0
- package/lib/synthid-http-runtime/chromium-css-property-catalog.mjs +7 -0
- package/lib/synthid-http-runtime/complete-protocol.mjs +88 -0
- package/lib/synthid-http-runtime/entry.d.mts +41 -0
- package/lib/synthid-http-runtime/entry.mjs +6 -0
- package/lib/synthid-http-runtime/event-listener-diagnostics.mjs +141 -0
- package/lib/synthid-http-runtime/independent-request-reference.mjs +49 -0
- package/lib/synthid-http-runtime/jsdom-event-clock-compat.mjs +76 -0
- package/lib/synthid-http-runtime/lifecycle-client.mjs +165 -0
- package/lib/synthid-http-runtime/lifecycle-web-session.mjs +114 -0
- package/lib/synthid-http-runtime/lifecycle-worker.mjs +363 -0
- package/lib/synthid-http-runtime/node-platform-failure-diagnostics.mjs +108 -0
- package/lib/synthid-http-runtime/realm-activation-diagnostic.js +31 -0
- package/lib/synthid-http-runtime/realm-focus-diagnostic.js +9 -0
- package/lib/synthid-http-runtime/realm-init.js +34 -0
- package/lib/synthid-http-runtime/realm-pointer-diagnostic.js +49 -0
- package/lib/synthid-http-runtime/runtime-anchor-href-translate.mjs +242 -0
- package/lib/synthid-http-runtime/runtime-child-web-compat.mjs +103 -0
- package/lib/synthid-http-runtime/runtime-chrome-csi.mjs +216 -0
- package/lib/synthid-http-runtime/runtime-clock-precision-control.mjs +51 -0
- package/lib/synthid-http-runtime/runtime-css-properties-compat.mjs +165 -0
- package/lib/synthid-http-runtime/runtime-detached-frame-compat.mjs +214 -0
- package/lib/synthid-http-runtime/runtime-javascript-bindings.mjs +69 -0
- package/lib/synthid-http-runtime/runtime-navigator-compat.mjs +99 -0
- package/lib/synthid-http-runtime/runtime-permissions-binding.mjs +64 -0
- package/lib/synthid-http-runtime/runtime-permissions-control.mjs +143 -0
- package/lib/synthid-http-runtime/runtime-web-compat.mjs +639 -0
- package/lib/synthid-http-runtime/runtime-webidl-callable-control.mjs +151 -0
- package/lib/synthid-http-runtime/runtime-webidl-realm-compat.mjs +132 -0
- package/lib/synthid-http-runtime/runtime-window-state-compat.mjs +128 -0
- package/lib/synthid-http-runtime/scripting-parser.mjs +13 -0
- package/lib/synthid-http-runtime/web-session.mjs +64 -0
- package/lib/synthid-http-runtime/webidl-brand-errors-preload.mjs +49 -0
- package/lib/synthid-http-runtime/webidl-callable-source-catalog.mjs +4491 -0
- package/lib/synthid-http-runtime/webidl-realm-catalog.mjs +124 -0
- package/lib/synthid-http.js +314 -0
- package/lib/synthid-manual.js +365 -0
- package/lib/synthid-page.js +101 -0
- package/lib/synthid-result.js +189 -0
- package/lib/synthid.js +540 -0
- package/lib/vision.js +41 -0
- package/package.json +77 -4
- package/src/image.ts +74 -0
- package/src/index.ts +203 -0
- package/src/provenance.ts +40 -0
- package/src/synthid-auth.ts +83 -0
- package/src/synthid-browser.ts +426 -0
- package/src/synthid-http-runtime/chromium-css-property-catalog.mjs +7 -0
- package/src/synthid-http-runtime/complete-protocol.mjs +88 -0
- package/src/synthid-http-runtime/entry.d.mts +41 -0
- package/src/synthid-http-runtime/entry.mjs +6 -0
- package/src/synthid-http-runtime/event-listener-diagnostics.mjs +141 -0
- package/src/synthid-http-runtime/independent-request-reference.mjs +49 -0
- package/src/synthid-http-runtime/jsdom-event-clock-compat.mjs +76 -0
- package/src/synthid-http-runtime/lifecycle-client.mjs +165 -0
- package/src/synthid-http-runtime/lifecycle-web-session.mjs +114 -0
- package/src/synthid-http-runtime/lifecycle-worker.mjs +363 -0
- package/src/synthid-http-runtime/node-platform-failure-diagnostics.mjs +108 -0
- package/src/synthid-http-runtime/realm-activation-diagnostic.js +31 -0
- package/src/synthid-http-runtime/realm-focus-diagnostic.js +9 -0
- package/src/synthid-http-runtime/realm-init.js +34 -0
- package/src/synthid-http-runtime/realm-pointer-diagnostic.js +49 -0
- package/src/synthid-http-runtime/runtime-anchor-href-translate.mjs +242 -0
- package/src/synthid-http-runtime/runtime-child-web-compat.mjs +103 -0
- package/src/synthid-http-runtime/runtime-chrome-csi.mjs +216 -0
- package/src/synthid-http-runtime/runtime-clock-precision-control.mjs +51 -0
- package/src/synthid-http-runtime/runtime-css-properties-compat.mjs +165 -0
- package/src/synthid-http-runtime/runtime-detached-frame-compat.mjs +214 -0
- package/src/synthid-http-runtime/runtime-javascript-bindings.mjs +69 -0
- package/src/synthid-http-runtime/runtime-navigator-compat.mjs +99 -0
- package/src/synthid-http-runtime/runtime-permissions-binding.mjs +64 -0
- package/src/synthid-http-runtime/runtime-permissions-control.mjs +143 -0
- package/src/synthid-http-runtime/runtime-web-compat.mjs +639 -0
- package/src/synthid-http-runtime/runtime-webidl-callable-control.mjs +151 -0
- package/src/synthid-http-runtime/runtime-webidl-realm-compat.mjs +132 -0
- package/src/synthid-http-runtime/runtime-window-state-compat.mjs +128 -0
- package/src/synthid-http-runtime/scripting-parser.mjs +13 -0
- package/src/synthid-http-runtime/web-session.mjs +64 -0
- package/src/synthid-http-runtime/webidl-brand-errors-preload.mjs +49 -0
- package/src/synthid-http-runtime/webidl-callable-source-catalog.mjs +4491 -0
- package/src/synthid-http-runtime/webidl-realm-catalog.mjs +124 -0
- package/src/synthid-http.ts +290 -0
- package/src/synthid-manual.ts +388 -0
- package/src/synthid-page.ts +103 -0
- package/src/synthid-result.ts +177 -0
- package/src/synthid.ts +514 -0
- package/src/vision.ts +57 -0
- package/webui/index.js +279 -0
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
// Optional Chrome hrefTranslate DOMString reflection for isolated JSDOM 26.1.0 realms.
|
|
2
|
+
import { types as nodeTypes } from 'node:util';
|
|
3
|
+
import WindowModule from 'jsdom/lib/jsdom/browser/Window.js';
|
|
4
|
+
import anchorGenerated from 'jsdom/lib/jsdom/living/generated/HTMLAnchorElement.js';
|
|
5
|
+
import webIdlUtils from 'jsdom/lib/jsdom/living/generated/utils.js';
|
|
6
|
+
import elementImplementation from 'jsdom/lib/jsdom/living/nodes/Element-impl.js';
|
|
7
|
+
import customElements from 'jsdom/lib/jsdom/living/helpers/custom-elements.js';
|
|
8
|
+
import jsdomPackage from 'jsdom/package.json' with { type: 'json' };
|
|
9
|
+
|
|
10
|
+
const own = Object.getOwnPropertyDescriptor, define = Object.defineProperty;
|
|
11
|
+
const getPrototype = Object.getPrototypeOf, apply = Reflect.apply;
|
|
12
|
+
const createObject = Object.create;
|
|
13
|
+
const hasOwn = Object.prototype.hasOwnProperty, freeze = Object.freeze;
|
|
14
|
+
const remove = Reflect.deleteProperty, isProxy = nodeTypes.isProxy;
|
|
15
|
+
const anchorIs = anchorGenerated.is, implForWrapper = webIdlUtils.implForWrapper;
|
|
16
|
+
const implSymbol = webIdlUtils.implSymbol, wrapperSymbol = webIdlUtils.wrapperSymbol;
|
|
17
|
+
const getAttribute = own(elementImplementation.implementation.prototype, 'getAttributeNS').value;
|
|
18
|
+
const setAttribute = own(elementImplementation.implementation.prototype, 'setAttributeNS').value;
|
|
19
|
+
const preReactions = customElements.ceReactionsPreSteps, postReactions = customElements.ceReactionsPostSteps;
|
|
20
|
+
const MAX_REALMS = 32, LIMIT = 1000000;
|
|
21
|
+
const activeRealms = new WeakSet();
|
|
22
|
+
let activeChildHook = false;
|
|
23
|
+
const isObject = value => value !== null && (typeof value === 'object' || typeof value === 'function');
|
|
24
|
+
const data = (object, key) => {
|
|
25
|
+
const descriptor = own(object, key);
|
|
26
|
+
return descriptor && apply(hasOwn, descriptor, ['value']) ? descriptor.value : undefined;
|
|
27
|
+
};
|
|
28
|
+
const sameAccessor = (a, b) => !!a && !!b && a.get === b.get && a.set === b.set &&
|
|
29
|
+
a.enumerable === b.enumerable && a.configurable === b.configurable && !apply(hasOwn, a, ['value']);
|
|
30
|
+
const sameFactory = (a, b) => !!a && !!b && a.value === b.value &&
|
|
31
|
+
a.writable === b.writable && a.enumerable === b.enumerable && a.configurable === b.configurable;
|
|
32
|
+
const errorCodes = new Set(['INVALID_OPTIONS', 'UNSUPPORTED_JSDOM', 'UNSUPPORTED_REALM',
|
|
33
|
+
'ALREADY_INSTALLED', 'CHILD_HOOK_ALREADY_INSTALLED', 'INSTALL_FAILED']);
|
|
34
|
+
export class RuntimeAnchorHrefTranslateError extends Error {
|
|
35
|
+
constructor(code) {
|
|
36
|
+
const safe = errorCodes.has(code) ? code : 'INSTALL_FAILED';
|
|
37
|
+
super('Runtime anchor hrefTranslate failed: ' + safe);
|
|
38
|
+
this.name = 'RuntimeAnchorHrefTranslateError'; this.code = safe;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Reject proxies and accessor-backed/borrowed impl symbols before generated.is
|
|
43
|
+
// or implForWrapper can read a page-controlled property. Cross-realm genuine
|
|
44
|
+
// wrappers keep the same implementation brand and reverse-wrapper identity.
|
|
45
|
+
function brandedImplementation(receiver) {
|
|
46
|
+
if (!isObject(receiver) || isProxy(receiver)) return undefined;
|
|
47
|
+
const impl = data(receiver, implSymbol);
|
|
48
|
+
if (!isObject(impl) || isProxy(impl) || data(impl, wrapperSymbol) !== receiver) return undefined;
|
|
49
|
+
let depth = 0;
|
|
50
|
+
for (let cursor = impl; cursor !== null; cursor = getPrototype(cursor)) {
|
|
51
|
+
if (isProxy(cursor) || depth++ >= 64) return undefined;
|
|
52
|
+
}
|
|
53
|
+
const carrier = createObject(null);
|
|
54
|
+
define(carrier, implSymbol, { value: impl });
|
|
55
|
+
if (!anchorIs(carrier)) return undefined;
|
|
56
|
+
return implForWrapper(carrier) === impl ? impl : undefined;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const realmSource = [
|
|
60
|
+
'"use strict";',
|
|
61
|
+
'const prefix = "Failed to set the \'hrefTranslate\' property on \'HTMLAnchorElement\': ";',
|
|
62
|
+
'const objectLike = value => value !== null && (typeof value === "object" || typeof value === "function");',
|
|
63
|
+
// ToPrimitive uses the specified string hint and original receiver. Only
|
|
64
|
+
// conversion-defined property reads/calls occur; user exceptions are kept.
|
|
65
|
+
'function domString(value) {',
|
|
66
|
+
' let primitive = value;',
|
|
67
|
+
' if (objectLike(value)) {',
|
|
68
|
+
' const exotic = value[toPrimitive];',
|
|
69
|
+
' if (exotic !== undefined && exotic !== null) {',
|
|
70
|
+
' if (typeof exotic !== "function") {',
|
|
71
|
+
' try { RealmString({ [toPrimitive]: exotic }); }',
|
|
72
|
+
' catch (error) { throw new RealmTypeError(prefix + error.message); }',
|
|
73
|
+
' }',
|
|
74
|
+
' primitive = apply(exotic, value, ["string"]);',
|
|
75
|
+
' if (objectLike(primitive)) throw new RealmTypeError(prefix + "Cannot convert object to primitive value");',
|
|
76
|
+
' } else {',
|
|
77
|
+
' let method = value.toString;',
|
|
78
|
+
' if (typeof method === "function") primitive = apply(method, value, []);',
|
|
79
|
+
' if (objectLike(primitive)) {',
|
|
80
|
+
' method = value.valueOf;',
|
|
81
|
+
' if (typeof method === "function") primitive = apply(method, value, []);',
|
|
82
|
+
' }',
|
|
83
|
+
' if (objectLike(primitive)) throw new RealmTypeError(prefix + "Cannot convert object to primitive value");',
|
|
84
|
+
' }',
|
|
85
|
+
' }',
|
|
86
|
+
' if (typeof primitive === "symbol") throw new RealmTypeError(prefix + "Cannot convert a Symbol value to a string");',
|
|
87
|
+
' return RealmString(primitive);',
|
|
88
|
+
'}',
|
|
89
|
+
'const holder = {',
|
|
90
|
+
' get hrefTranslate() {',
|
|
91
|
+
' bridge.call(false);',
|
|
92
|
+
' try {',
|
|
93
|
+
' const impl = bridge.brand(this);',
|
|
94
|
+
' if (!impl) { bridge.illegal(); throw new RealmTypeError("Illegal invocation"); }',
|
|
95
|
+
' const result = bridge.read(impl); bridge.returned(); return result;',
|
|
96
|
+
' } catch (error) { bridge.threw(); throw error; }',
|
|
97
|
+
' },',
|
|
98
|
+
' set hrefTranslate(value) {',
|
|
99
|
+
' bridge.call(true);',
|
|
100
|
+
' try {',
|
|
101
|
+
' const impl = bridge.brand(this);',
|
|
102
|
+
' if (!impl) { bridge.illegal(); throw new RealmTypeError("Illegal invocation"); }',
|
|
103
|
+
' bridge.write(impl, domString(value)); bridge.returned();',
|
|
104
|
+
' } catch (error) { bridge.threw(); throw error; }',
|
|
105
|
+
' }',
|
|
106
|
+
'};',
|
|
107
|
+
'return { realm: globalThis, holder, wrap: target => new RealmProxy(target, { __proto__: null }) };',
|
|
108
|
+
].join('\n');
|
|
109
|
+
|
|
110
|
+
/** Install before page code/observers; dispose before earlier child factory hooks. */
|
|
111
|
+
export function installRuntimeAnchorHrefTranslate(window, options = {}) {
|
|
112
|
+
if (!isObject(window) || isProxy(window) || !options || typeof options !== 'object' || isProxy(options))
|
|
113
|
+
throw new RuntimeAnchorHrefTranslateError('INVALID_OPTIONS');
|
|
114
|
+
const option = own(options, 'children');
|
|
115
|
+
if (option && !apply(hasOwn, option, ['value'])) throw new RuntimeAnchorHrefTranslateError('INVALID_OPTIONS');
|
|
116
|
+
const children = option?.value === undefined ? false : option.value;
|
|
117
|
+
if (typeof children !== 'boolean') throw new RuntimeAnchorHrefTranslateError('INVALID_OPTIONS');
|
|
118
|
+
if (jsdomPackage.version !== '26.1.0') throw new RuntimeAnchorHrefTranslateError('UNSUPPORTED_JSDOM');
|
|
119
|
+
if (activeRealms.has(window)) throw new RuntimeAnchorHrefTranslateError('ALREADY_INSTALLED');
|
|
120
|
+
if (children && activeChildHook) throw new RuntimeAnchorHrefTranslateError('CHILD_HOOK_ALREADY_INSTALLED');
|
|
121
|
+
const previousFactory = children ? own(WindowModule, 'createWindow') : undefined;
|
|
122
|
+
if (children && (!previousFactory?.writable || typeof previousFactory.value !== 'function'))
|
|
123
|
+
throw new RuntimeAnchorHrefTranslateError('UNSUPPORTED_JSDOM');
|
|
124
|
+
const records = [];
|
|
125
|
+
let disposed = false, installedFactory;
|
|
126
|
+
const stats = { __proto__: null,
|
|
127
|
+
installedRealms: 0, installedBindings: 0, existingBindingsPreserved: 0,
|
|
128
|
+
observedChildren: 0, skippedFactories: 0, unsupportedChildren: 0, childFailures: 0,
|
|
129
|
+
realmLimitReached: false, calls: 0, getterCalls: 0, setterCalls: 0,
|
|
130
|
+
returned: 0, threw: 0, illegalReceivers: 0,
|
|
131
|
+
removedBindings: 0, cleanupConflicts: 0, cleanupFailures: 0,
|
|
132
|
+
childFactoryRestored: false, childFactoryRestoreConflict: false,
|
|
133
|
+
};
|
|
134
|
+
const increment = key => { if (stats[key] < LIMIT) stats[key]++; };
|
|
135
|
+
const counted = key => { if (!disposed) increment(key); };
|
|
136
|
+
const snapshot = () => ({ schemaVersion: 1, diagnosticOnly: true,
|
|
137
|
+
attribute: 'hreftranslate', conversion: 'DOMString', sourceRenderingModified: false,
|
|
138
|
+
callableRepresentation: 'empty-handler-realm-proxy', childrenEnabled: children,
|
|
139
|
+
maximumRealms: MAX_REALMS, counterLimit: LIMIT, ...stats, disposed });
|
|
140
|
+
function installRealm(realm) {
|
|
141
|
+
if (records.length >= MAX_REALMS) { stats.realmLimitReached = true; return; }
|
|
142
|
+
if (!isObject(realm) || isProxy(realm)) throw new RuntimeAnchorHrefTranslateError('UNSUPPORTED_REALM');
|
|
143
|
+
if (activeRealms.has(realm)) throw new RuntimeAnchorHrefTranslateError('ALREADY_INSTALLED');
|
|
144
|
+
const RealmFunction = data(realm, 'Function'), registry = data(realm, webIdlUtils.ctorRegistrySymbol);
|
|
145
|
+
if (typeof RealmFunction !== 'function' || RealmFunction === Function || !isObject(registry) ||
|
|
146
|
+
isProxy(registry) || getPrototype(registry) !== null) throw new RuntimeAnchorHrefTranslateError('UNSUPPORTED_REALM');
|
|
147
|
+
const Anchor = data(registry, 'HTMLAnchorElement');
|
|
148
|
+
const prototype = typeof Anchor === 'function' ? data(Anchor, 'prototype') : undefined;
|
|
149
|
+
if (!isObject(prototype) || isProxy(prototype)) throw new RuntimeAnchorHrefTranslateError('UNSUPPORTED_REALM');
|
|
150
|
+
let existing = false;
|
|
151
|
+
for (let cursor = prototype; cursor !== null; cursor = getPrototype(cursor)) {
|
|
152
|
+
if (isProxy(cursor)) throw new RuntimeAnchorHrefTranslateError('UNSUPPORTED_REALM');
|
|
153
|
+
if (own(cursor, 'hrefTranslate')) { existing = true; break; }
|
|
154
|
+
}
|
|
155
|
+
const record = { realm, prototype, installed: undefined };
|
|
156
|
+
if (!existing) {
|
|
157
|
+
const RealmTypeError = data(realm, 'TypeError'), RealmString = data(realm, 'String');
|
|
158
|
+
const RealmProxy = data(realm, 'Proxy'), RealmSymbol = data(realm, 'Symbol');
|
|
159
|
+
const RealmReflect = data(realm, 'Reflect');
|
|
160
|
+
const realmApply = isObject(RealmReflect) && !isProxy(RealmReflect) ? data(RealmReflect, 'apply') : undefined;
|
|
161
|
+
if (![RealmTypeError, RealmString, RealmProxy, RealmSymbol].every(value => typeof value === 'function'))
|
|
162
|
+
throw new RuntimeAnchorHrefTranslateError('UNSUPPORTED_REALM');
|
|
163
|
+
const toPrimitive = data(RealmSymbol, 'toPrimitive');
|
|
164
|
+
if (typeof toPrimitive !== 'symbol' || typeof realmApply !== 'function') throw new RuntimeAnchorHrefTranslateError('UNSUPPORTED_REALM');
|
|
165
|
+
const bridge = freeze({
|
|
166
|
+
brand: brandedImplementation,
|
|
167
|
+
call(setter) { counted('calls'); counted(setter ? 'setterCalls' : 'getterCalls'); },
|
|
168
|
+
returned() { counted('returned'); }, threw() { counted('threw'); }, illegal() { counted('illegalReceivers'); },
|
|
169
|
+
read(impl) {
|
|
170
|
+
preReactions(realm);
|
|
171
|
+
try { const value = apply(getAttribute, impl, [null, 'hreftranslate']); return value === null ? '' : value; }
|
|
172
|
+
finally { postReactions(realm); }
|
|
173
|
+
},
|
|
174
|
+
write(impl, value) {
|
|
175
|
+
preReactions(realm);
|
|
176
|
+
try { apply(setAttribute, impl, [null, 'hreftranslate', value]); }
|
|
177
|
+
finally { postReactions(realm); }
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
const create = RealmFunction('bridge', 'RealmTypeError', 'RealmString', 'RealmProxy', 'toPrimitive', 'apply', realmSource);
|
|
181
|
+
const created = create(bridge, RealmTypeError, RealmString, RealmProxy, toPrimitive, realmApply);
|
|
182
|
+
if (created.realm !== realm) throw new RuntimeAnchorHrefTranslateError('UNSUPPORTED_REALM');
|
|
183
|
+
const authored = own(created.holder, 'hrefTranslate');
|
|
184
|
+
record.installed = { get: created.wrap(authored.get), set: created.wrap(authored.set), enumerable: true, configurable: true };
|
|
185
|
+
define(prototype, 'hrefTranslate', record.installed);
|
|
186
|
+
}
|
|
187
|
+
records[records.length] = record; activeRealms.add(realm); increment('installedRealms');
|
|
188
|
+
increment(existing ? 'existingBindingsPreserved' : 'installedBindings');
|
|
189
|
+
}
|
|
190
|
+
function dispose() {
|
|
191
|
+
if (disposed) return snapshot();
|
|
192
|
+
disposed = true;
|
|
193
|
+
if (installedFactory) {
|
|
194
|
+
try {
|
|
195
|
+
if (!sameFactory(own(WindowModule, 'createWindow'), installedFactory)) stats.childFactoryRestoreConflict = true;
|
|
196
|
+
else { define(WindowModule, 'createWindow', previousFactory); stats.childFactoryRestored = true; }
|
|
197
|
+
} catch { stats.childFactoryRestoreConflict = true; }
|
|
198
|
+
activeChildHook = false;
|
|
199
|
+
}
|
|
200
|
+
for (let index = records.length - 1; index >= 0; index--) {
|
|
201
|
+
const record = records[index];
|
|
202
|
+
try {
|
|
203
|
+
if (record.installed) {
|
|
204
|
+
const current = own(record.prototype, 'hrefTranslate');
|
|
205
|
+
if (current && !sameAccessor(current, record.installed)) increment('cleanupConflicts');
|
|
206
|
+
else if (current && remove(record.prototype, 'hrefTranslate')) increment('removedBindings');
|
|
207
|
+
else if (current) increment('cleanupFailures');
|
|
208
|
+
}
|
|
209
|
+
} catch { increment('cleanupFailures'); }
|
|
210
|
+
activeRealms.delete(record.realm);
|
|
211
|
+
}
|
|
212
|
+
records.length = 0;
|
|
213
|
+
return snapshot();
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
installRealm(window);
|
|
217
|
+
if (children) {
|
|
218
|
+
const original = previousFactory.value;
|
|
219
|
+
const wrapper = function createWindowWithAnchorHrefTranslate(factoryOptions) {
|
|
220
|
+
const child = apply(original, this, arguments);
|
|
221
|
+
if (disposed) return child;
|
|
222
|
+
if (!isObject(factoryOptions) || isProxy(factoryOptions) || !own(factoryOptions, 'parentOrigin')) {
|
|
223
|
+
increment('skippedFactories'); return child;
|
|
224
|
+
}
|
|
225
|
+
increment('observedChildren');
|
|
226
|
+
if (data(factoryOptions, 'runScripts') !== 'outside-only') { increment('unsupportedChildren'); return child; }
|
|
227
|
+
if (records.length >= MAX_REALMS) { stats.realmLimitReached = true; return child; }
|
|
228
|
+
try {
|
|
229
|
+
const realm = data(child, '_globalProxy');
|
|
230
|
+
if (!realm) increment('unsupportedChildren'); else installRealm(realm);
|
|
231
|
+
} catch { increment('childFailures'); }
|
|
232
|
+
return child;
|
|
233
|
+
};
|
|
234
|
+
installedFactory = { ...previousFactory, value: wrapper };
|
|
235
|
+
define(WindowModule, 'createWindow', installedFactory); activeChildHook = true;
|
|
236
|
+
}
|
|
237
|
+
return freeze({ snapshot, dispose });
|
|
238
|
+
} catch (error) {
|
|
239
|
+
dispose();
|
|
240
|
+
throw error instanceof RuntimeAnchorHrefTranslateError ? error : new RuntimeAnchorHrefTranslateError('INSTALL_FAILED');
|
|
241
|
+
}
|
|
242
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import WindowModule from 'jsdom/lib/jsdom/browser/Window.js';
|
|
2
|
+
import jsdomPackage from 'jsdom/package.json' with { type: 'json' };
|
|
3
|
+
import { installRuntimeWebCompat, normalizeRuntimeNetworkSample } from './runtime-web-compat.mjs';
|
|
4
|
+
|
|
5
|
+
const MAX_CHILDREN = 32;
|
|
6
|
+
let active = false;
|
|
7
|
+
const data = (object, key) => {
|
|
8
|
+
const descriptor = Object.getOwnPropertyDescriptor(object, key);
|
|
9
|
+
if (descriptor && !Object.hasOwn(descriptor, 'value')) throw TypeError('CHILD_COMPAT_DATA_OPTIONS_REQUIRED');
|
|
10
|
+
return descriptor?.value;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/** Dedicated JSDOM26 worker hook, installed after the top window is created.
|
|
14
|
+
* HTMLFrameElement calls this shared module export dynamically; JSDOM's public
|
|
15
|
+
* top-level factory retains its original destructured function. This host hook
|
|
16
|
+
* is never assigned to a DOM object or passed as a page callback. */
|
|
17
|
+
export function installRuntimeChildWebCompat(options = {}) {
|
|
18
|
+
if (active) throw Error('CHILD_COMPAT_ALREADY_INSTALLED');
|
|
19
|
+
const descriptor = Object.getOwnPropertyDescriptor(WindowModule, 'createWindow');
|
|
20
|
+
if (jsdomPackage.version !== '26.1.0' || !descriptor?.writable || typeof descriptor.value !== 'function') {
|
|
21
|
+
throw Error('CHILD_COMPAT_UNSUPPORTED_JSDOM');
|
|
22
|
+
}
|
|
23
|
+
if (!options || typeof options !== 'object') throw TypeError('CHILD_COMPAT_INVALID_OPTIONS');
|
|
24
|
+
const childOptions = {};
|
|
25
|
+
for (const key of ['physicalMemoryBytes', 'deviceMemoryMaxGiB', 'platform', 'virtualScreen', 'virtualMouse', 'clockPrecision100us', 'userActivationWebIdlCompatibility']) {
|
|
26
|
+
const value = data(options, key);
|
|
27
|
+
if (value !== undefined) childOptions[key] = value;
|
|
28
|
+
}
|
|
29
|
+
const networkSample = normalizeRuntimeNetworkSample(data(options, 'networkSample'));
|
|
30
|
+
if (networkSample) childOptions.networkSample = Object.freeze(networkSample);
|
|
31
|
+
if (childOptions.physicalMemoryBytes !== undefined && (!Number.isSafeInteger(childOptions.physicalMemoryBytes) || childOptions.physicalMemoryBytes <= 0)
|
|
32
|
+
|| childOptions.deviceMemoryMaxGiB !== undefined && ![8, 32].includes(childOptions.deviceMemoryMaxGiB)
|
|
33
|
+
|| childOptions.platform !== undefined && !['Win32', 'MacIntel', 'Linux x86_64', 'Linux aarch64'].includes(childOptions.platform)
|
|
34
|
+
|| childOptions.virtualScreen !== undefined && typeof childOptions.virtualScreen !== 'boolean'
|
|
35
|
+
|| childOptions.virtualMouse !== undefined && typeof childOptions.virtualMouse !== 'boolean'
|
|
36
|
+
|| childOptions.clockPrecision100us !== undefined && typeof childOptions.clockPrecision100us !== 'boolean'
|
|
37
|
+
|| childOptions.userActivationWebIdlCompatibility !== undefined && typeof childOptions.userActivationWebIdlCompatibility !== 'boolean') {
|
|
38
|
+
throw TypeError('CHILD_COMPAT_INVALID_OPTIONS');
|
|
39
|
+
}
|
|
40
|
+
Object.freeze(childOptions);
|
|
41
|
+
const original = descriptor.value;
|
|
42
|
+
const controllers = [];
|
|
43
|
+
const stats = { observedChildren: 0, installationAttempts: 0, installedChildren: 0, skippedFactories: 0,
|
|
44
|
+
unsupportedRealms: 0, installationFailures: 0, disposedChildren: 0,
|
|
45
|
+
disposalFailures: 0, limitReached: false, restoreConflict: false, disposed: false };
|
|
46
|
+
const count = key => { stats[key] = Math.min(1000000, stats[key] + 1); };
|
|
47
|
+
function createWindowWithCompatibility(factoryOptions) {
|
|
48
|
+
const child = Reflect.apply(original, this, arguments);
|
|
49
|
+
if (stats.disposed) return child;
|
|
50
|
+
// parentOrigin is supplied by HTMLFrameElement, not the top-level API.
|
|
51
|
+
if (!factoryOptions || !Object.hasOwn(factoryOptions, 'parentOrigin')) {
|
|
52
|
+
count('skippedFactories'); return child;
|
|
53
|
+
}
|
|
54
|
+
count('observedChildren');
|
|
55
|
+
if (factoryOptions.runScripts !== 'outside-only') { count('unsupportedRealms'); return child; }
|
|
56
|
+
if (stats.installationAttempts >= MAX_CHILDREN) { stats.limitReached = true; return child; }
|
|
57
|
+
count('installationAttempts');
|
|
58
|
+
try {
|
|
59
|
+
// createWindow returns the backing object; _globalProxy is the real vm
|
|
60
|
+
// global returned by frame.contentWindow and used by realm evaluation.
|
|
61
|
+
const proxy = Object.getOwnPropertyDescriptor(child, '_globalProxy')?.value;
|
|
62
|
+
if (!proxy || proxy.Function === Function || proxy.eval === eval) { count('unsupportedRealms'); return child; }
|
|
63
|
+
// Child clocks retain their own creation origin. Parent HTTP navigation
|
|
64
|
+
// records and origins are intentionally not passed to an unrelated realm.
|
|
65
|
+
controllers.push(installRuntimeWebCompat(proxy, childOptions));
|
|
66
|
+
count('installedChildren');
|
|
67
|
+
} catch {
|
|
68
|
+
// Do not leak a new host Error object into page appendChild/navigation.
|
|
69
|
+
// A counted failure makes the compatibility attempt explicitly incomplete.
|
|
70
|
+
count('installationFailures');
|
|
71
|
+
}
|
|
72
|
+
return child;
|
|
73
|
+
}
|
|
74
|
+
Object.defineProperty(WindowModule, 'createWindow', { ...descriptor, value: createWindowWithCompatibility });
|
|
75
|
+
active = true;
|
|
76
|
+
return {
|
|
77
|
+
snapshot() {
|
|
78
|
+
let userActivationReads;
|
|
79
|
+
for (const controller of controllers) {
|
|
80
|
+
const reads = controller.snapshot().userActivationReads;
|
|
81
|
+
if (!reads) continue;
|
|
82
|
+
userActivationReads ??= Object.fromEntries(Object.keys(reads).map(kind => [kind, {}]));
|
|
83
|
+
for (const [kind, counters] of Object.entries(reads)) for (const [name, value] of Object.entries(counters)) {
|
|
84
|
+
userActivationReads[kind][name] = Math.min(1000000, (userActivationReads[kind][name] ?? 0) + value);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return { ...stats, ...(userActivationReads ? { userActivationReads } : {}) };
|
|
88
|
+
},
|
|
89
|
+
dispose() {
|
|
90
|
+
if (stats.disposed) return;
|
|
91
|
+
stats.disposed = true;
|
|
92
|
+
const current = Object.getOwnPropertyDescriptor(WindowModule, 'createWindow');
|
|
93
|
+
if (current?.value === createWindowWithCompatibility) Object.defineProperty(WindowModule, 'createWindow', descriptor);
|
|
94
|
+
else stats.restoreConflict = true;
|
|
95
|
+
for (const controller of controllers.reverse()) {
|
|
96
|
+
try { controller.dispose(); count('disposedChildren'); }
|
|
97
|
+
catch { count('disposalFailures'); }
|
|
98
|
+
}
|
|
99
|
+
controllers.length = 0;
|
|
100
|
+
active = false;
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// Narrow diagnostic Chrome CSI capability for pinned isolated JSDOM realms.
|
|
2
|
+
// Native CSI source: Chromium 157.0.8103.38 chrome/renderer/loadtimes_bindings.cc.
|
|
3
|
+
// These are this Node HTTP/DOM lifecycle's own clocks, not captured browser data.
|
|
4
|
+
import { types as nodeTypes } from 'node:util';
|
|
5
|
+
import WindowModule from 'jsdom/lib/jsdom/browser/Window.js';
|
|
6
|
+
import DocumentModule from 'jsdom/lib/jsdom/living/nodes/Document-impl.js';
|
|
7
|
+
import EventModule from 'jsdom/lib/jsdom/living/events/Event-impl.js';
|
|
8
|
+
import PerformanceModule from 'jsdom/lib/jsdom/living/hr-time/Performance-impl.js';
|
|
9
|
+
import utils from 'jsdom/lib/jsdom/living/generated/utils.js';
|
|
10
|
+
import jsdomPackage from 'jsdom/package.json' with { type: 'json' };
|
|
11
|
+
import { uncoarsenedPerformanceNow } from './runtime-clock-precision-control.mjs';
|
|
12
|
+
|
|
13
|
+
const get = Object.getOwnPropertyDescriptor, define = Object.defineProperty, apply = Reflect.apply, remove = Reflect.deleteProperty;
|
|
14
|
+
const object = value => value !== null && (typeof value === 'object' || typeof value === 'function');
|
|
15
|
+
const proxy = nodeTypes.isProxy, own = Object.hasOwn, prototypeOf = Object.getPrototypeOf;
|
|
16
|
+
const data = (value, key) => { if (!object(value) || proxy(value)) return undefined; const d = get(value, key); return d && own(d, 'value') ? d.value : undefined; };
|
|
17
|
+
const same = (left, right) => !!left && !!right && ['value', 'get', 'set', 'writable', 'enumerable', 'configurable'].every(
|
|
18
|
+
key => own(left, key) === own(right, key) && Object.is(left[key], right[key]));
|
|
19
|
+
const MAX_REALMS = 32, LIMIT = 1000000, activeRealms = new WeakSet(); let activeChildHook = false;
|
|
20
|
+
const transitions = new Map([['link', 0], ['form-submission', 0], ['form-resubmission', 0], ['back-forward', 6], ['restore', 6], ['reload', 16], ['other', 15]]);
|
|
21
|
+
const errorCodes = new Set(['INVALID_OPTIONS', 'UNSUPPORTED_JSDOM', 'UNSUPPORTED_REALM', 'ALREADY_INSTALLED', 'CHILD_HOOK_ALREADY_INSTALLED', 'INSTALL_FAILED']);
|
|
22
|
+
export class RuntimeChromeCsiError extends Error {
|
|
23
|
+
constructor(code) { const safe = errorCodes.has(code) ? code : 'INSTALL_FAILED'; super('Runtime Chrome CSI failed: ' + safe); this.name = 'RuntimeChromeCsiError'; this.code = safe; }
|
|
24
|
+
}
|
|
25
|
+
function inheritedData(target, key) {
|
|
26
|
+
for (let current = target, depth = 0; current && depth < 64; current = prototypeOf(current), depth++) {
|
|
27
|
+
if (proxy(current)) return undefined;
|
|
28
|
+
const d = get(current, key); if (d) return own(d, 'value') ? d : undefined;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function hasBinding(target, key) {
|
|
32
|
+
for (let current = target, depth = 0; current && depth < 64; current = prototypeOf(current), depth++) {
|
|
33
|
+
if (proxy(current)) throw new RuntimeChromeCsiError('UNSUPPORTED_REALM');
|
|
34
|
+
if (get(current, key)) return true;
|
|
35
|
+
}
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
function implementation(wrapper, expected) {
|
|
39
|
+
const impl = data(wrapper, utils.implSymbol);
|
|
40
|
+
return object(impl) && !proxy(impl) && Object.prototype.isPrototypeOf.call(expected, impl) ? impl : undefined;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Trusted source creates every public function, prototype and result in the
|
|
44
|
+
// owning realm. Its private bridges accept no input and return numbers only.
|
|
45
|
+
function createInRealm(readOrigin, readElapsed, transition) {
|
|
46
|
+
'use strict';
|
|
47
|
+
const trunc = Math.trunc, finite = Number.isFinite, RealmTypeError = TypeError;
|
|
48
|
+
const stats = { calls: 0, domContentLoadedCompletions: 0, clockFailures: 0 };
|
|
49
|
+
let disposed = false, domContentLoadedEnd = 0;
|
|
50
|
+
const increment = key => { if (!disposed && stats[key] < 1000000) stats[key]++; };
|
|
51
|
+
const sample = () => {
|
|
52
|
+
const origin = readOrigin(), elapsed = readElapsed();
|
|
53
|
+
if (!finite(origin) || !finite(elapsed) || origin <= 0 || elapsed < 0) {
|
|
54
|
+
increment('clockFailures'); throw new RealmTypeError('CSI_CLOCK_UNAVAILABLE');
|
|
55
|
+
}
|
|
56
|
+
return { origin, elapsed };
|
|
57
|
+
};
|
|
58
|
+
// A comma expression prevents inferred name "csi"; strict ordinary functions
|
|
59
|
+
// match the native constructibility, prototype and own caller/arguments shape.
|
|
60
|
+
const callable = (0, function () {
|
|
61
|
+
increment('calls'); const { origin, elapsed } = sample();
|
|
62
|
+
const startE = trunc(origin);
|
|
63
|
+
// Chromium subtracts the legacy integer navigation origin. The actual
|
|
64
|
+
// JSDOM clock is relative to the full origin, including its fraction.
|
|
65
|
+
const elapsedSinceLegacyStart = elapsed + (origin - startE);
|
|
66
|
+
return { startE, onloadT: domContentLoadedEnd, pageT: trunc(elapsedSinceLegacyStart * 1000) / 1000, tran: transition };
|
|
67
|
+
});
|
|
68
|
+
const chrome = { csi: callable };
|
|
69
|
+
return {
|
|
70
|
+
realm: globalThis, chrome,
|
|
71
|
+
completeDomContentLoaded() {
|
|
72
|
+
if (disposed || domContentLoadedEnd !== 0) return;
|
|
73
|
+
const { origin, elapsed } = sample(); domContentLoadedEnd = trunc(origin + elapsed); increment('domContentLoadedCompletions');
|
|
74
|
+
},
|
|
75
|
+
snapshot() { return { ...stats, domContentLoadedCaptured: domContentLoadedEnd !== 0 }; },
|
|
76
|
+
dispose() { disposed = true; },
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Prefer calling inside JSDOM beforeParse. Later performance clock rebasing is
|
|
81
|
+
* observed dynamically. Dispose in reverse order with later createWindow hooks.
|
|
82
|
+
* onloadT remains zero if a past DOMContentLoaded completion was not observed. */
|
|
83
|
+
export function installRuntimeChromeCsi(window, options = {}) {
|
|
84
|
+
if (!object(window) || proxy(window) || !object(options) || proxy(options) || Array.isArray(options)) throw new RuntimeChromeCsiError('INVALID_OPTIONS');
|
|
85
|
+
const option = key => { const d = get(options, key); if (d && !own(d, 'value')) throw new RuntimeChromeCsiError('INVALID_OPTIONS'); return d?.value; };
|
|
86
|
+
const children = option('children') ?? false, transition = option('transition') ?? 'other';
|
|
87
|
+
if (typeof children !== 'boolean' || !transitions.has(transition)) throw new RuntimeChromeCsiError('INVALID_OPTIONS');
|
|
88
|
+
if (jsdomPackage.version !== '26.1.0' || typeof uncoarsenedPerformanceNow !== 'function') throw new RuntimeChromeCsiError('UNSUPPORTED_JSDOM');
|
|
89
|
+
if (activeRealms.has(window)) throw new RuntimeChromeCsiError('ALREADY_INSTALLED');
|
|
90
|
+
if (children && activeChildHook) throw new RuntimeChromeCsiError('CHILD_HOOK_ALREADY_INSTALLED');
|
|
91
|
+
const previousFactory = children ? get(WindowModule, 'createWindow') : undefined;
|
|
92
|
+
if (children && (!previousFactory?.writable || typeof previousFactory.value !== 'function')) throw new RuntimeChromeCsiError('UNSUPPORTED_JSDOM');
|
|
93
|
+
const records = []; let disposed = false, installedFactory;
|
|
94
|
+
const stats = { installedRealms: 0, installedBindings: 0, preservedBindings: 0, observedChildren: 0, skippedFactories: 0,
|
|
95
|
+
unsupportedChildren: 0, childFailures: 0, realmLimitReached: false, dispatchHooksInstalled: 0, completedDispatches: 0,
|
|
96
|
+
lateDocuments: 0, dispatchCaptureFailures: 0, clockBridgeFailures: 0, removedBindings: 0, restoredDispatchHooks: 0,
|
|
97
|
+
cleanupConflicts: 0, cleanupFailures: 0, childFactoryRestored: false, childFactoryRestoreConflict: false };
|
|
98
|
+
const increment = key => { stats[key] = Math.min(LIMIT, stats[key] + 1); };
|
|
99
|
+
const totals = { calls: 0, domContentLoadedCompletions: 0, clockFailures: 0, domContentLoadedCapturedRealms: 0 };
|
|
100
|
+
function addStats(target, value) {
|
|
101
|
+
for (const key of ['calls', 'domContentLoadedCompletions', 'clockFailures']) {
|
|
102
|
+
const number = data(value, key); if (!Number.isInteger(number) || number < 0 || number > LIMIT) throw Error('INVALID_STATS');
|
|
103
|
+
target[key] = Math.min(LIMIT, target[key] + number);
|
|
104
|
+
}
|
|
105
|
+
const captured = data(value, 'domContentLoadedCaptured'); if (typeof captured !== 'boolean') throw Error('INVALID_STATS');
|
|
106
|
+
if (captured) target.domContentLoadedCapturedRealms = Math.min(LIMIT, target.domContentLoadedCapturedRealms + 1);
|
|
107
|
+
}
|
|
108
|
+
function snapshot() {
|
|
109
|
+
const current = { ...totals };
|
|
110
|
+
if (!disposed) for (const record of records) if (record.controller) addStats(current, record.controller.snapshot());
|
|
111
|
+
return { schemaVersion: 1, diagnosticOnly: true, runtimeChromeCsi: true, childrenEnabled: children, maximumRealms: MAX_REALMS,
|
|
112
|
+
timingSource: 'node-http-jsdom-monotonic-projection', nativeWallClockModeled: false, microsecondTruncation: true,
|
|
113
|
+
domContentLoadedEndSource: 'trusted-jsdom-dispatch-return', nativeSourceForged: false, sourceRenderingModified: false,
|
|
114
|
+
callableRepresentation: 'realm-anonymous-strict-function', nativeResultSetterSemanticsModeled: false,
|
|
115
|
+
hostCallbacksExposed: false, unrelatedChromeApisAdded: false, ...stats, ...current, disposed };
|
|
116
|
+
}
|
|
117
|
+
function installRealm(realm, isChild = false) {
|
|
118
|
+
if (records.length >= MAX_REALMS) { stats.realmLimitReached = true; return; }
|
|
119
|
+
if (!object(realm) || proxy(realm)) throw new RuntimeChromeCsiError('UNSUPPORTED_REALM');
|
|
120
|
+
if (activeRealms.has(realm)) throw new RuntimeChromeCsiError('ALREADY_INSTALLED');
|
|
121
|
+
const backing = data(realm, '_globalObject'), realmFunction = data(realm, 'Function');
|
|
122
|
+
const document = implementation(data(backing, '_document'), DocumentModule.implementation.prototype);
|
|
123
|
+
// The performance wrapper is the existing JSDOM builtin, read once at install.
|
|
124
|
+
const performance = realm.performance, perf = implementation(performance, PerformanceModule.implementation.prototype);
|
|
125
|
+
if (!backing || data(backing, '_globalProxy') !== realm || typeof realmFunction !== 'function' || realmFunction === Function
|
|
126
|
+
|| !document || !perf || data(perf, '_globalObject') !== backing) throw new RuntimeChromeCsiError('UNSUPPORTED_REALM');
|
|
127
|
+
const record = { realm, document, controller: undefined, installed: undefined, previousDispatch: undefined, installedDispatch: undefined };
|
|
128
|
+
records.push(record); activeRealms.add(realm); increment('installedRealms');
|
|
129
|
+
if (hasBinding(realm, 'chrome')) { increment('preservedBindings'); return; }
|
|
130
|
+
const finiteNonnegative = value => typeof value === 'number' && Number.isFinite(value) && value >= 0 && value < Number.MAX_SAFE_INTEGER;
|
|
131
|
+
const readOrigin = () => { const value = data(perf, 'timeOrigin'); if (finiteNonnegative(value) && value > 0) return value; if (!disposed) increment('clockBridgeFailures'); return NaN; };
|
|
132
|
+
const readElapsed = () => {
|
|
133
|
+
try {
|
|
134
|
+
// Refuse accessor-backed timing internals before calling the original
|
|
135
|
+
// JSDOM method, which otherwise reads _nowAtTimeOrigin directly.
|
|
136
|
+
const clockOffset = data(perf, '_nowAtTimeOrigin');
|
|
137
|
+
if (typeof clockOffset !== 'number' || !Number.isFinite(clockOffset) || Math.abs(clockOffset) >= Number.MAX_SAFE_INTEGER) throw Error('INVALID_CLOCK');
|
|
138
|
+
const value = apply(uncoarsenedPerformanceNow, perf, []);
|
|
139
|
+
if (!finiteNonnegative(value)) throw Error('INVALID_CLOCK'); return value;
|
|
140
|
+
} catch { if (!disposed) increment('clockBridgeFailures'); return NaN; }
|
|
141
|
+
};
|
|
142
|
+
const create = realmFunction('readOrigin', 'readElapsed', 'transition', 'return (' + createInRealm.toString() + ')(readOrigin,readElapsed,transition)');
|
|
143
|
+
const controller = apply(create, undefined, [readOrigin, readElapsed, transitions.get(isChild ? 'other' : transition)]);
|
|
144
|
+
if (controller.realm !== realm) throw new RuntimeChromeCsiError('UNSUPPORTED_REALM');
|
|
145
|
+
record.controller = controller;
|
|
146
|
+
record.installed = { value: controller.chrome, writable: true, enumerable: true, configurable: true };
|
|
147
|
+
define(realm, 'chrome', record.installed); increment('installedBindings');
|
|
148
|
+
const dispatch = inheritedData(document, '_dispatch');
|
|
149
|
+
if (!dispatch || typeof dispatch.value !== 'function') throw new RuntimeChromeCsiError('UNSUPPORTED_REALM');
|
|
150
|
+
record.previousDispatch = get(document, '_dispatch');
|
|
151
|
+
if (record.previousDispatch && !record.previousDispatch.configurable && !record.previousDispatch.writable) throw new RuntimeChromeCsiError('UNSUPPORTED_REALM');
|
|
152
|
+
if (data(document, '_currentDocumentReadiness') !== 'loading') increment('lateDocuments');
|
|
153
|
+
const original = dispatch.value;
|
|
154
|
+
const dispatchWrapper = function dispatchWithChromeCsiTiming(eventImpl) {
|
|
155
|
+
const result = apply(original, this, arguments);
|
|
156
|
+
if (!disposed && this === document && object(eventImpl) && !proxy(eventImpl)
|
|
157
|
+
&& Object.prototype.isPrototypeOf.call(EventModule.implementation.prototype, eventImpl)
|
|
158
|
+
&& data(eventImpl, 'type') === 'DOMContentLoaded' && data(eventImpl, 'isTrusted') === true) {
|
|
159
|
+
increment('completedDispatches');
|
|
160
|
+
try { controller.completeDomContentLoaded(); } catch { increment('dispatchCaptureFailures'); }
|
|
161
|
+
}
|
|
162
|
+
return result;
|
|
163
|
+
};
|
|
164
|
+
record.installedDispatch = record.previousDispatch ? { ...record.previousDispatch, value: dispatchWrapper }
|
|
165
|
+
: { value: dispatchWrapper, writable: true, enumerable: false, configurable: true };
|
|
166
|
+
define(document, '_dispatch', record.installedDispatch); increment('dispatchHooksInstalled');
|
|
167
|
+
}
|
|
168
|
+
function restore(target, key, previous, installed, counter) {
|
|
169
|
+
if (!installed) return;
|
|
170
|
+
try {
|
|
171
|
+
if (!same(get(target, key), installed)) { increment('cleanupConflicts'); return; }
|
|
172
|
+
if (previous) define(target, key, previous); else if (!remove(target, key)) throw Error('RESTORE_FAILED');
|
|
173
|
+
increment(counter);
|
|
174
|
+
} catch { increment('cleanupFailures'); }
|
|
175
|
+
}
|
|
176
|
+
function dispose() {
|
|
177
|
+
if (disposed) return snapshot();
|
|
178
|
+
for (const record of records) if (record.controller) { try { addStats(totals, record.controller.snapshot()); } catch { increment('cleanupFailures'); } }
|
|
179
|
+
disposed = true;
|
|
180
|
+
if (installedFactory) {
|
|
181
|
+
try {
|
|
182
|
+
if (same(get(WindowModule, 'createWindow'), installedFactory)) { define(WindowModule, 'createWindow', previousFactory); stats.childFactoryRestored = true; }
|
|
183
|
+
else stats.childFactoryRestoreConflict = true;
|
|
184
|
+
} catch { stats.childFactoryRestoreConflict = true; }
|
|
185
|
+
activeChildHook = false;
|
|
186
|
+
}
|
|
187
|
+
for (let index = records.length - 1; index >= 0; index--) {
|
|
188
|
+
const record = records[index];
|
|
189
|
+
restore(record.document, '_dispatch', record.previousDispatch, record.installedDispatch, 'restoredDispatchHooks');
|
|
190
|
+
restore(record.realm, 'chrome', undefined, record.installed, 'removedBindings');
|
|
191
|
+
try { record.controller?.dispose(); } catch { increment('cleanupFailures'); }
|
|
192
|
+
activeRealms.delete(record.realm);
|
|
193
|
+
}
|
|
194
|
+
records.length = 0; return snapshot();
|
|
195
|
+
}
|
|
196
|
+
try {
|
|
197
|
+
installRealm(window);
|
|
198
|
+
if (children) {
|
|
199
|
+
const original = previousFactory.value;
|
|
200
|
+
const wrapper = function createWindowWithChromeCsi(factoryOptions) {
|
|
201
|
+
const child = apply(original, this, arguments); if (disposed) return child;
|
|
202
|
+
if (!object(factoryOptions) || proxy(factoryOptions) || !get(factoryOptions, 'parentOrigin')) { increment('skippedFactories'); return child; }
|
|
203
|
+
increment('observedChildren');
|
|
204
|
+
if (data(factoryOptions, 'runScripts') !== 'outside-only') { increment('unsupportedChildren'); return child; }
|
|
205
|
+
if (records.length >= MAX_REALMS) { stats.realmLimitReached = true; return child; }
|
|
206
|
+
try { const realm = data(child, '_globalProxy'); if (!realm) increment('unsupportedChildren'); else installRealm(realm, true); }
|
|
207
|
+
catch { increment('childFailures'); } return child;
|
|
208
|
+
};
|
|
209
|
+
installedFactory = { ...previousFactory, value: wrapper };
|
|
210
|
+
define(WindowModule, 'createWindow', installedFactory); activeChildHook = true;
|
|
211
|
+
}
|
|
212
|
+
return Object.freeze({ snapshot, dispose });
|
|
213
|
+
} catch (error) {
|
|
214
|
+
dispose(); throw error instanceof RuntimeChromeCsiError ? error : new RuntimeChromeCsiError('INSTALL_FAILED');
|
|
215
|
+
}
|
|
216
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Fixed100us precision experiment for one dedicated JSDOM26 worker process.
|
|
2
|
+
// Never installs on host Date/performance or on a realm's generated wrapper.
|
|
3
|
+
import PerformanceModule from 'jsdom/lib/jsdom/living/hr-time/Performance-impl.js';
|
|
4
|
+
import jsdomPackage from 'jsdom/package.json' with {type:'json'};
|
|
5
|
+
const prototype=PerformanceModule.implementation.prototype;
|
|
6
|
+
const originalDescriptor=Object.getOwnPropertyDescriptor(prototype,'now');
|
|
7
|
+
export const uncoarsenedPerformanceNow=originalDescriptor?.value;
|
|
8
|
+
let active=false;
|
|
9
|
+
|
|
10
|
+
// Self-contained so web compatibility can create its own realm-local copy.
|
|
11
|
+
export function quantizeCompletedClock100us(value){
|
|
12
|
+
if(typeof value!=='number'||!Number.isFinite(value)||value<0||value>Number.MAX_SAFE_INTEGER/10)throw Error('CLOCK_PRECISION_INVALID_VALUE');
|
|
13
|
+
return Math.floor(value*10)/10;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function installRuntimeClockPrecisionControl(){
|
|
17
|
+
const previous=Object.getOwnPropertyDescriptor(prototype,'now');
|
|
18
|
+
if(active)throw Error('CLOCK_PRECISION_ALREADY_INSTALLED');
|
|
19
|
+
if(jsdomPackage.version!=='26.1.0'||typeof uncoarsenedPerformanceNow!=='function'||previous?.value!==uncoarsenedPerformanceNow
|
|
20
|
+
||!previous.writable||!previous.configurable)throw Error('CLOCK_PRECISION_UNSUPPORTED_JSDOM');
|
|
21
|
+
const apply=Reflect.apply,get=Object.getOwnPropertyDescriptor,define=Object.defineProperty,own=Object.hasOwn,is=Object.is,min=Math.min;
|
|
22
|
+
const stats={diagnosticOnly:true,fixed100us:true,algorithm:'floor-relative',absoluteMonotonicClamping:false,epochTimeOriginCoarsened:false,quantumMicroseconds:100,hostIntrinsicsModified:false,publicWrapperIdentityChanged:false,
|
|
23
|
+
installed:false,calls:0,quantizedCalls:0,originalThrows:0,invalidResults:0,disposed:false,restored:false,restoreConflict:false,restoreFailures:0};
|
|
24
|
+
const count=key=>{stats[key]=min(1000000,stats[key]+1);};
|
|
25
|
+
const wrapper=function now(){
|
|
26
|
+
if(stats.disposed)return apply(uncoarsenedPerformanceNow,this,arguments);
|
|
27
|
+
count('calls');let value;
|
|
28
|
+
try{value=apply(uncoarsenedPerformanceNow,this,arguments);}catch(error){count('originalThrows');throw error;}
|
|
29
|
+
let rounded;
|
|
30
|
+
try{rounded=quantizeCompletedClock100us(value);}catch(error){count('invalidResults');throw error;}
|
|
31
|
+
count('quantizedCalls');return rounded;
|
|
32
|
+
};
|
|
33
|
+
const installed={...previous,value:wrapper};
|
|
34
|
+
const sameDescriptor=(left,right)=>{
|
|
35
|
+
if(!left||!right)return false;
|
|
36
|
+
for(const key of ['value','get','set','writable','enumerable','configurable'])if(own(left,key)!==own(right,key)||!is(left[key],right[key]))return false;
|
|
37
|
+
return true;
|
|
38
|
+
};
|
|
39
|
+
const snapshot=()=>({...stats});
|
|
40
|
+
const dispose=()=>{
|
|
41
|
+
if(stats.disposed)return snapshot();stats.disposed=true;
|
|
42
|
+
try{if(sameDescriptor(get(prototype,'now'),installed)){define(prototype,'now',previous);stats.restored=true;}else stats.restoreConflict=true;}
|
|
43
|
+
catch{count('restoreFailures');}
|
|
44
|
+
active=false;return snapshot();
|
|
45
|
+
};
|
|
46
|
+
try{
|
|
47
|
+
define(prototype,'now',installed);stats.installed=true;active=true;
|
|
48
|
+
}
|
|
49
|
+
catch(error){dispose();throw error;}
|
|
50
|
+
return{snapshot,dispose};
|
|
51
|
+
}
|