Skip to content
File

Blob: src/pyodide/internal/metadata.ts

typescript72 lines
1// Copyright (c) 2026 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5import { default as MetadataReader } from 'pyodide-internal:runtime-generated/metadata';
6import { default as ArtifactBundler } from 'pyodide-internal:artifacts';
7 
8export const IS_WORKERD = MetadataReader.isWorkerd();
9export const IS_TRACING = MetadataReader.isTracing();
10export const SHOULD_ABORT_ISOLATE_ON_FATAL_ERROR =
11 MetadataReader.shouldAbortIsolateOnFatalError();
12 
13// Snapshots
14export const SHOULD_SNAPSHOT_TO_DISK = MetadataReader.shouldSnapshotToDisk();
15export const IS_CREATING_BASELINE_SNAPSHOT =
16 MetadataReader.isCreatingBaselineSnapshot();
17export const IS_EW_VALIDATING = ArtifactBundler.isEwValidating();
18export const IS_DYNAMIC_WORKER = ArtifactBundler.isDynamicWorker();
19export const IS_CREATING_SNAPSHOT = IS_EW_VALIDATING || SHOULD_SNAPSHOT_TO_DISK;
20 
21// There are two validations that we perform. The first one runs without a _bundled_ memory snapshot
22// (but may use a baseline snapshot, though this is stored in the ArtfactBundler). The second one
23// runs with a _bundled_ memory snapshot, which is expected to be a dedicated snapshot. When this
24// bool is true, we verify that the bundled memory snapshot we receive is a dedicated snapshot.
25export const IS_SECOND_VALIDATION_PHASE =
26 MetadataReader.hasMemorySnapshot() && IS_EW_VALIDATING;
27export const MEMORY_SNAPSHOT_READER = MetadataReader.hasMemorySnapshot()
28 ? MetadataReader
29 : ArtifactBundler.hasMemorySnapshot()
30 ? ArtifactBundler
31 : undefined;
32 
33// Packages
34export const PACKAGES_VERSION = MetadataReader.getPackagesVersion();
35export const USING_OLDEST_PACKAGES_VERSION = PACKAGES_VERSION === '20240829.4';
36// The package lock is embedded in the binary. See `getPyodideLock` and `packageLocks`.
37export const LOCKFILE = JSON.parse(
38 MetadataReader.getPackagesLock()
39) as PackageLock;
40 
41export const REQUIREMENTS = MetadataReader.getRequirements();
42export const TRANSITIVE_REQUIREMENTS =
43 MetadataReader.getTransitiveRequirements();
44 
45// Entrypoints
46export const MAIN_MODULE_NAME = MetadataReader.getMainModule();
47 
48export type CompatibilityFlags = MetadataReader.CompatibilityFlags;
49export const COMPATIBILITY_FLAGS: MetadataReader.CompatibilityFlags = {
50 // Compat flags returned from getCompatibilityFlags is immutable,
51 // but in Pyodide 0.26, we modify the JS object that is exposed to the Python through
52 // registerJsModule so we create a new object here by copying the values.
53 ...MetadataReader.getCompatibilityFlags(),
54};
55export const WORKFLOWS_ENABLED: boolean =
56 !!COMPATIBILITY_FLAGS.python_workflows;
57const NO_GLOBAL_HANDLERS: boolean =
58 !!COMPATIBILITY_FLAGS.python_no_global_handlers;
59const FORCE_NEW_VENDOR_PATH: boolean =
60 !!COMPATIBILITY_FLAGS.python_workers_force_new_vendor_path;
61export const IS_DEDICATED_SNAPSHOT_ENABLED: boolean =
62 !!COMPATIBILITY_FLAGS.python_dedicated_snapshot;
63export const EXTERNAL_SDK = !!COMPATIBILITY_FLAGS.enable_python_external_sdk;
64 
65export const LEGACY_GLOBAL_HANDLERS = !NO_GLOBAL_HANDLERS;
66export const LEGACY_VENDOR_PATH = !FORCE_NEW_VENDOR_PATH;
67export const CHECK_RNG_STATE = !!COMPATIBILITY_FLAGS.python_check_rng_state;
68export const PROCESS_PTH_FILES = !!COMPATIBILITY_FLAGS.python_process_pth_files;
69 
70export const setCpuLimitNearlyExceededCallback =
71 MetadataReader.setCpuLimitNearlyExceededCallback.bind(MetadataReader);