Download examples/visualizer_code/source/tests/scale4d_sandbox_harness.py from facebook/SCALE4D: direct link, hf CLI and curl.
- Browser
- Download file 3.31 kB
-
https://huggingface.co/facebook/SCALE4D/resolve/main/examples/visualizer_code/source/tests/scale4d_sandbox_harness.py
- Command line
-
hf download hf://facebook/SCALE4D/examples/visualizer_code/source/tests/scale4d_sandbox_harness.py
-
curl -L -o scale4d_sandbox_harness.py https://huggingface.co/facebook/SCALE4D/resolve/main/examples/visualizer_code/source/tests/scale4d_sandbox_harness.py
3.31 kB
| """Generate a browser regression fixture with an opaque origin and no network.""" | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| def make_harness(viewer: str, *, label: str, verify: str = "") -> str: | |
| policy = ( | |
| "default-src 'none'; connect-src 'none'; media-src data:; " | |
| "img-src data:; style-src 'unsafe-inline'; script-src 'unsafe-inline'" | |
| ) | |
| viewer = viewer.replace( | |
| "<head>", | |
| f'<head><meta http-equiv="Content-Security-Policy" content="{policy}">', | |
| 1, | |
| ) | |
| observe = r""" | |
| window.__SANDBOX_FETCH_CALLS__ = 0; | |
| const reportFailure = setInterval(() => { | |
| const overlay = document.getElementById("error-overlay"); | |
| if (overlay && !overlay.hidden) { | |
| clearInterval(reportFailure); | |
| parent.postMessage({sandboxResult: true, passed: false, | |
| error: document.getElementById("error-message").textContent, | |
| frameUrl: location.href, origin: window.origin}, "*"); | |
| } | |
| }, 100); | |
| """ | |
| if verify: | |
| observe += "\nwindow.fetch = () => { window.__SANDBOX_FETCH_CALLS__++; throw new TypeError('Network disabled by regression test'); };\n" | |
| viewer = viewer.replace("<head>", "<head><script>" + observe + "</script>", 1) | |
| if verify: | |
| verification = ( | |
| "Promise.resolve(" | |
| + verify.strip() | |
| + ").then(result => { clearInterval(reportFailure); " | |
| + "parent.postMessage({sandboxResult:true, ...result, " | |
| + "fetchCalls:window.__SANDBOX_FETCH_CALLS__, frameUrl:location.href, " | |
| + "origin:window.origin, videoSource:document.getElementById('source-video').src.slice(0,30)}, '*'); " | |
| + "}).catch(error => parent.postMessage({sandboxResult:true,passed:false,error:String(error)}, '*'));" | |
| ) | |
| viewer = viewer.replace( | |
| "</body>", "<script>" + verification + "</script></body>", 1 | |
| ) | |
| source = json.dumps(viewer).replace("<", "\\u003c") | |
| return ( | |
| '<!doctype html><html><head><meta charset="utf-8"></head><body ' | |
| 'style="margin:0"><iframe id="viewer" sandbox="allow-scripts" ' | |
| 'allow="autoplay" style="width:100%;height:100vh;border:0"></iframe><script>' | |
| f"window.__SANDBOX_LABEL__={json.dumps(label)};" | |
| "window.__SANDBOX_RESULT__=null;" | |
| "addEventListener('message', event => {" | |
| "if(event.source===document.getElementById('viewer').contentWindow " | |
| "&& event.data?.sandboxResult) window.__SANDBOX_RESULT__=event.data;});" | |
| f"document.getElementById('viewer').srcdoc={source};" | |
| "</script></body></html>" | |
| ) | |
| def main() -> None: | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--viewer", type=Path, required=True) | |
| parser.add_argument("--output", type=Path, required=True) | |
| parser.add_argument("--label", required=True) | |
| parser.add_argument("--verify", type=Path) | |
| args = parser.parse_args() | |
| args.output.parent.mkdir(parents=True, exist_ok=True) | |
| with args.output.open("x") as handle: | |
| handle.write( | |
| make_harness( | |
| args.viewer.read_text(), | |
| label=args.label, | |
| verify=args.verify.read_text() if args.verify else "", | |
| ) | |
| ) | |
| if __name__ == "__main__": | |
| main() | |