@componentonce/compiler-esbuild 0.1.0-beta.0 → 0.1.0-beta.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.
package/README.md CHANGED
@@ -1,31 +1,119 @@
1
1
  # @componentonce/compiler-esbuild
2
2
 
3
- Optional trusted compiler/evaluator tooling for ComponentOnce.
3
+ Optional trusted compiler, packager, and evaluator tooling for ComponentOnce.
4
4
 
5
- This package is not part of the core runtime ABI and owns no storage or transport.
5
+ This package is not part of the core runtime ABI. It owns no storage or transport.
6
6
 
7
- ## Generic modules
7
+ ## CLI: source file to self-describing package
8
8
 
9
- compileTrustedModule compiles JavaScript/TypeScript into an immutable CommonJS artifact. It adds no runtime externals implicitly. Any imported host module must be declared explicitly through externalModules and injected when instantiateTrustedBundle executes the trusted artifact.
9
+ Install the renderer adapter you use plus this compiler, then:
10
10
 
11
- This is suitable for ordinary DOM/HTML modules and other non-React implementations.
11
+ ```sh
12
+ componentonce build ./src/amount-card.tsx \
13
+ --renderer react \
14
+ --out ./dist/amount-card.componentonce.json
15
+ ```
12
16
 
13
- ## React convenience
17
+ The default renderer is `react`, so this is also valid:
14
18
 
15
- compileTrustedReactModule wraps the generic compiler with automatic JSX transformation and allows the React module specifiers as host externals:
19
+ ```sh
20
+ componentonce build ./src/amount-card.tsx
21
+ ```
16
22
 
17
- - react
18
- - react/jsx-runtime
19
- - react/jsx-dev-runtime
23
+ Useful options:
20
24
 
21
- React is still never bundled. The host injects its own React singleton at instantiation. Additional host SDK imports use additionalExternalModules.
25
+ ```text
26
+ --renderer <id> Renderer id recorded in the package
27
+ -o, --out <file> Output JSON package
28
+ --export <name> Definition export name (default: definition)
29
+ --external <name> Additional host module kept external and loaded for trusted build discovery
30
+ ```
22
31
 
23
- ## Artifact and trust model
32
+ React builds automatically externalize `react`, the JSX runtimes, and `@componentonce/react`. DOM builds automatically externalize `@componentonce/dom`. Relative imports are bundled from the entry file directory; undeclared package imports still fail rather than being silently pulled from the compiler process.
24
33
 
25
- Artifacts contain deterministic bundle text, byte length, SHA-256 hash/integrity, normalized diagnostics, exact referenced externals, and esbuild metafile data.
34
+ The source module normally exports one definition:
26
35
 
27
- instantiateTrustedBundle deliberately uses new Function with a narrow injected require map. It is for trusted internal code and is not a security sandbox.
36
+ ```tsx
37
+ import {
38
+ createReactRequirement,
39
+ defineReactComponent,
40
+ } from "@componentonce/react";
28
41
 
29
- Storage is host-owned: compiled text/bytes can live in a database-backed file object, local disk, object storage, or any other adapter.
42
+ export const definition = defineReactComponent<Props, HostContext, Payload>({
43
+ manifest: {
44
+ id: "example/amount-card",
45
+ version: "1.0.0",
46
+ requirements: [createReactRequirement("19.3.0")],
47
+ },
48
+ component({ props, context, payload }) {
49
+ return <strong>{props.label}: {context.formatAmount(payload.amount)}</strong>;
50
+ },
51
+ });
52
+ ```
53
+
54
+ The high-level builder evaluates trusted source once during build to discover the exported manifest. The resulting package stores that manifest next to the bundle, so catalogs can inspect identity, version, and requirements without executing the component.
55
+
56
+ ## Programmatic packaging
57
+
58
+ ```ts
59
+ import * as React from "react";
60
+ import * as jsxRuntime from "react/jsx-runtime";
61
+ import * as ComponentOnceReact from "@componentonce/react";
62
+ import {
63
+ buildTrustedReactPackage,
64
+ serializeTrustedComponentPackage,
65
+ } from "@componentonce/compiler-esbuild";
66
+
67
+ const componentPackage = await buildTrustedReactPackage({
68
+ source,
69
+ sourceFileName: "amount-card.tsx",
70
+ resolveDir: sourceDirectory,
71
+ externals: {
72
+ react: React,
73
+ "react/jsx-runtime": jsxRuntime,
74
+ "@componentonce/react": ComponentOnceReact,
75
+ },
76
+ });
77
+
78
+ await store.put(
79
+ componentPackage.manifest.id,
80
+ serializeTrustedComponentPackage(componentPackage),
81
+ );
82
+ ```
83
+
84
+ At runtime:
85
+
86
+ ```ts
87
+ import {
88
+ instantiateTrustedComponentPackage,
89
+ parseTrustedComponentPackage,
90
+ } from "@componentonce/compiler-esbuild";
91
+
92
+ const componentPackage = parseTrustedComponentPackage(await store.get(...));
93
+
94
+ console.log(componentPackage.manifest); // no component execution
95
+
96
+ const definition = instantiateTrustedComponentPackage(componentPackage, {
97
+ externals: {
98
+ react: React,
99
+ "react/jsx-runtime": jsxRuntime,
100
+ "@componentonce/react": ComponentOnceReact,
101
+ },
102
+ });
103
+ ```
104
+
105
+ Instantiation verifies that the definition inside the executable bundle still has the same manifest as the package envelope.
106
+
107
+ ## Low-level compilation
108
+
109
+ `compileTrustedModule` compiles JavaScript/TypeScript into an immutable CommonJS artifact. Caller-declared package imports remain external. Relative imports can be bundled when `resolveDir` is supplied.
110
+
111
+ `compileTrustedReactModule` adds automatic JSX transformation and React/JSX host externals while still never bundling React.
112
+
113
+ Artifacts contain deterministic bundle text, byte length, SHA-256 hash/integrity, normalized diagnostics, exact external imports, and esbuild metafile data.
114
+
115
+ ## Trust model
116
+
117
+ `instantiateTrustedBundle` deliberately uses `new Function` with a narrow injected require map. It is for trusted internal code and is not a security sandbox.
30
118
 
31
119
  esbuild transpiles TypeScript syntax but does not perform semantic type checking; hosts that need that guarantee should run their TypeScript checker separately.
package/dist/cli.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+ /** Run the ComponentOnce command line interface with ordinary process arguments. */
3
+ export declare function runComponentOnceCli(args: readonly string[]): Promise<void>;
4
+ //# sourceMappingURL=cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AA2BA,oFAAoF;AACpF,wBAAsB,mBAAmB,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAwDhF"}
package/dist/cli.js ADDED
@@ -0,0 +1,171 @@
1
+ #!/usr/bin/env node
2
+ import { mkdir, readFile, writeFile } from "node:fs/promises";
3
+ import { createRequire } from "node:module";
4
+ import { basename, dirname, extname, resolve } from "node:path";
5
+ import { pathToFileURL } from "node:url";
6
+ import { buildTrustedDefinitionPackage, buildTrustedReactPackage, serializeTrustedComponentPackage, } from "./package.js";
7
+ const REACT_HOST_EXTERNALS = [
8
+ "react",
9
+ "react/jsx-runtime",
10
+ "react/jsx-dev-runtime",
11
+ "@componentonce/react",
12
+ ];
13
+ const DOM_HOST_EXTERNALS = ["@componentonce/dom"];
14
+ /** Run the ComponentOnce command line interface with ordinary process arguments. */
15
+ export async function runComponentOnceCli(args) {
16
+ if (args.length === 0 || args.includes("--help") || args.includes("-h")) {
17
+ process.stdout.write(usage());
18
+ return;
19
+ }
20
+ if (args[0] !== "build") {
21
+ throw new Error("Unknown command \"" + String(args[0]) + "\".\n\n" + usage());
22
+ }
23
+ const options = parseBuildArgs(args.slice(1));
24
+ const entryPath = resolve(options.entry);
25
+ const source = await readFile(entryPath, "utf8");
26
+ const sourceDir = dirname(entryPath);
27
+ const externalNames = uniqueStrings([
28
+ ...(options.renderer === "react" ? REACT_HOST_EXTERNALS : []),
29
+ ...(options.renderer === "dom" ? DOM_HOST_EXTERNALS : []),
30
+ ...options.externalModules,
31
+ ]);
32
+ const externals = await loadExternals(externalNames, sourceDir);
33
+ const componentPackage = options.renderer === "react"
34
+ ? await buildTrustedReactPackage({
35
+ source,
36
+ sourceFileName: basename(entryPath),
37
+ resolveDir: sourceDir,
38
+ externals,
39
+ ...(options.definitionExport === undefined
40
+ ? {}
41
+ : { definitionExport: options.definitionExport }),
42
+ })
43
+ : await buildTrustedDefinitionPackage({
44
+ source,
45
+ sourceFileName: basename(entryPath),
46
+ resolveDir: sourceDir,
47
+ renderer: options.renderer,
48
+ externals,
49
+ ...(options.definitionExport === undefined
50
+ ? {}
51
+ : { definitionExport: options.definitionExport }),
52
+ });
53
+ const outPath = resolve(options.out);
54
+ await mkdir(dirname(outPath), { recursive: true });
55
+ await writeFile(outPath, serializeTrustedComponentPackage(componentPackage) + "\n", "utf8");
56
+ process.stdout.write("Built " +
57
+ componentPackage.manifest.id +
58
+ "@" +
59
+ componentPackage.manifest.version +
60
+ " (" +
61
+ componentPackage.renderer +
62
+ ") -> " +
63
+ outPath +
64
+ "\n");
65
+ }
66
+ function parseBuildArgs(args) {
67
+ const entry = args[0];
68
+ if (entry === undefined || entry.startsWith("-")) {
69
+ throw new Error("componentonce build requires an entry source file.\n\n" + usage());
70
+ }
71
+ let renderer = "react";
72
+ let out;
73
+ let definitionExport;
74
+ const externalModules = [];
75
+ for (let index = 1; index < args.length; index += 1) {
76
+ const arg = args[index];
77
+ switch (arg) {
78
+ case "--renderer": {
79
+ renderer = requireOptionValue(args, ++index, "--renderer");
80
+ break;
81
+ }
82
+ case "--out":
83
+ case "-o": {
84
+ out = requireOptionValue(args, ++index, String(arg));
85
+ break;
86
+ }
87
+ case "--export": {
88
+ definitionExport = requireOptionValue(args, ++index, "--export");
89
+ break;
90
+ }
91
+ case "--external": {
92
+ externalModules.push(requireOptionValue(args, ++index, "--external"));
93
+ break;
94
+ }
95
+ default:
96
+ throw new Error("Unknown build option \"" + String(arg) + "\".\n\n" + usage());
97
+ }
98
+ }
99
+ return {
100
+ entry,
101
+ renderer,
102
+ out: out ?? defaultOutputPath(entry),
103
+ ...(definitionExport === undefined ? {} : { definitionExport }),
104
+ externalModules,
105
+ };
106
+ }
107
+ function requireOptionValue(args, index, option) {
108
+ const value = args[index];
109
+ if (value === undefined || value.startsWith("-")) {
110
+ throw new Error(option + " requires a value.");
111
+ }
112
+ return value;
113
+ }
114
+ function defaultOutputPath(entry) {
115
+ const extension = extname(entry);
116
+ const base = extension === "" ? entry : entry.slice(0, -extension.length);
117
+ return base + ".componentonce.json";
118
+ }
119
+ async function loadExternals(specifiers, sourceDir) {
120
+ const resolver = createRequire(pathToFileURL(resolve(sourceDir, "__componentonce_resolve__.mjs")));
121
+ const externals = {};
122
+ for (const specifier of specifiers) {
123
+ externals[specifier] = await loadExternal(specifier, resolver);
124
+ }
125
+ return externals;
126
+ }
127
+ async function loadExternal(specifier, resolver) {
128
+ try {
129
+ return resolver(specifier);
130
+ }
131
+ catch (error) {
132
+ if (!canRetryAsEsm(error))
133
+ throw error;
134
+ return import(specifier);
135
+ }
136
+ }
137
+ function canRetryAsEsm(error) {
138
+ if (typeof error !== "object" || error === null || !("code" in error))
139
+ return false;
140
+ const code = error.code;
141
+ return code === "ERR_REQUIRE_ESM" || code === "ERR_PACKAGE_PATH_NOT_EXPORTED";
142
+ }
143
+ function uniqueStrings(values) {
144
+ return [...new Set(values)];
145
+ }
146
+ function usage() {
147
+ return [
148
+ "ComponentOnce trusted component builder",
149
+ "",
150
+ "Usage:",
151
+ " componentonce build <entry> [options]",
152
+ "",
153
+ "Options:",
154
+ " --renderer <id> Renderer id recorded in the package (default: react)",
155
+ " -o, --out <file> Output JSON package (default: <entry>.componentonce.json)",
156
+ " --export <name> Definition export name (default: definition)",
157
+ " --external <name> Host module to keep external and load for trusted build-time discovery",
158
+ " -h, --help Show this help",
159
+ "",
160
+ "React builds automatically externalize react, JSX runtimes, and @componentonce/react.",
161
+ "DOM builds automatically externalize @componentonce/dom.",
162
+ "Relative imports are bundled from the entry file directory.",
163
+ "",
164
+ ].join("\n");
165
+ }
166
+ runComponentOnceCli(process.argv.slice(2)).catch((error) => {
167
+ const message = error instanceof Error ? error.message : String(error);
168
+ process.stderr.write("componentonce: " + message + "\n");
169
+ process.exitCode = 1;
170
+ });
171
+ //# sourceMappingURL=cli.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC9D,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EACL,6BAA6B,EAC7B,wBAAwB,EACxB,gCAAgC,GACjC,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,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACvE,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,iBAAiB,GAAG,OAAO,GAAG,IAAI,CAAC,CAAC;IACzD,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC;AACvB,CAAC,CAAC,CAAC"}
@@ -0,0 +1,141 @@
1
+ import { type Metafile } from "esbuild";
2
+ /** Stable identifier for the trusted CommonJS bundle contract emitted by this package. */
3
+ export declare const COMPONENTONCE_TRUSTED_BUNDLE_FORMAT: "componentonce.trusted-cjs.v1";
4
+ /** Source syntaxes accepted by the trusted compiler. */
5
+ export type ComponentOnceSourceLoader = "js" | "jsx" | "ts" | "tsx";
6
+ /** JSX transform mode forwarded to esbuild by the generic compiler. */
7
+ export type ComponentOnceJsxMode = "transform" | "preserve" | "automatic";
8
+ /** Input for compiling one trusted JavaScript or TypeScript module. */
9
+ export interface ComponentOnceCompileInput {
10
+ /** TypeScript, TSX, JavaScript, or JSX module source. */
11
+ readonly source: string;
12
+ /** Stable diagnostic/source-map name. */
13
+ readonly sourceFileName?: string;
14
+ /** Explicit source syntax, otherwise inferred from sourceFileName. */
15
+ readonly loader?: ComponentOnceSourceLoader;
16
+ /** Directory used to resolve and bundle relative imports from the in-memory entry source. */
17
+ readonly resolveDir?: string;
18
+ /** Exact import specifiers the host will inject during instantiation. */
19
+ readonly externalModules?: readonly string[];
20
+ /** JSX transform mode; generic compilation defaults to transform. */
21
+ readonly jsx?: ComponentOnceJsxMode;
22
+ }
23
+ /** React convenience compiler input; React externals are supplied by the wrapper. */
24
+ export interface ComponentOnceReactCompileInput {
25
+ readonly source: string;
26
+ readonly sourceFileName?: string;
27
+ readonly loader?: ComponentOnceSourceLoader;
28
+ /** Directory used to resolve and bundle relative imports from the in-memory entry source. */
29
+ readonly resolveDir?: string;
30
+ /** Additional non-React host externals. */
31
+ readonly additionalExternalModules?: readonly string[];
32
+ }
33
+ /** Source location attached to a normalized compiler diagnostic. */
34
+ export interface ComponentOnceDiagnosticLocation {
35
+ /** Source file name supplied to the compiler. */
36
+ readonly file: string;
37
+ /** One-based source line. */
38
+ readonly line: number;
39
+ /** Zero-based source column. */
40
+ readonly column: number;
41
+ /** Length of the highlighted source range. */
42
+ readonly length: number;
43
+ /** Full source line associated with the diagnostic. */
44
+ readonly lineText: string;
45
+ }
46
+ /** Secondary explanatory note attached to a compiler diagnostic. */
47
+ export interface ComponentOnceDiagnosticNote {
48
+ /** Human-readable note. */
49
+ readonly text: string;
50
+ /** Source location when esbuild can identify one. */
51
+ readonly location?: ComponentOnceDiagnosticLocation;
52
+ }
53
+ /** Storage-neutral compiler warning or error. */
54
+ export interface ComponentOnceDiagnostic {
55
+ /** Diagnostic severity. */
56
+ readonly kind: "error" | "warning";
57
+ /** Human-readable diagnostic text. */
58
+ readonly text: string;
59
+ /** Primary source location when available. */
60
+ readonly location?: ComponentOnceDiagnosticLocation;
61
+ /** Supporting notes supplied by esbuild. */
62
+ readonly notes: readonly ComponentOnceDiagnosticNote[];
63
+ }
64
+ /** Read-only esbuild metadata describing the emitted bundle and its external imports. */
65
+ export type ComponentOnceBundleMetafile = Readonly<Metafile>;
66
+ /** Immutable, storage-neutral output of the trusted module compiler. */
67
+ export interface ComponentOnceTrustedBundleArtifact {
68
+ /** Bundle contract understood by `instantiateTrustedBundle`. */
69
+ readonly format: typeof COMPONENTONCE_TRUSTED_BUNDLE_FORMAT;
70
+ /** Executable CommonJS bundle text. */
71
+ readonly code: string;
72
+ /** UTF-8 byte length of `code`. */
73
+ readonly byteLength: number;
74
+ /** Lowercase hexadecimal SHA-256 digest of `code`. */
75
+ readonly sha256: string;
76
+ /** Subresource-integrity-style SHA-256 value for `code`. */
77
+ readonly integrity: string;
78
+ /** Exact external module specifiers referenced by the emitted bundle. */
79
+ readonly externalModules: readonly string[];
80
+ /** Normalized non-fatal compiler diagnostics. */
81
+ readonly diagnostics: readonly ComponentOnceDiagnostic[];
82
+ /** esbuild metadata for dependency and output inspection. */
83
+ readonly metafile: ComponentOnceBundleMetafile;
84
+ }
85
+ /** Compilation failure with stable, normalized esbuild diagnostics. */
86
+ export declare class ComponentOnceCompileError extends Error {
87
+ /** Compiler errors that caused the build to fail. */
88
+ readonly diagnostics: readonly ComponentOnceDiagnostic[];
89
+ constructor(diagnostics: readonly ComponentOnceDiagnostic[]);
90
+ }
91
+ /** Exact host-owned module values exposed to a trusted bundle's local `require`. */
92
+ export type ComponentOnceHostExternals = Readonly<Record<string, unknown>>;
93
+ /** JavaScript text, UTF-8 bytes, or an in-memory artifact accepted by the trusted evaluator. */
94
+ export type ComponentOnceTrustedBundleSource = string | Uint8Array | ComponentOnceTrustedBundleArtifact;
95
+ /** Options controlling explicit external injection and optional integrity verification. */
96
+ export interface InstantiateTrustedBundleOptions {
97
+ /** Exact external values, including the host's React and JSX runtime instances. */
98
+ readonly externals: ComponentOnceHostExternals;
99
+ /** Expected integrity for separately loaded text/bytes; artifacts verify their own value by default. */
100
+ readonly expectedIntegrity?: string;
101
+ /** Debugger-only source name appended to the evaluated bundle. */
102
+ readonly sourceName?: string;
103
+ }
104
+ /** Deterministic error raised when a bundle requests an external the host did not inject. */
105
+ export declare class ComponentOnceMissingExternalError extends Error {
106
+ /** Exact module specifier requested by the bundle. */
107
+ readonly specifier: string;
108
+ /** Sorted external specifiers supplied by the host. */
109
+ readonly availableExternals: readonly string[];
110
+ constructor(specifier: string, availableExternals: readonly string[]);
111
+ }
112
+ /** Error raised when loaded bundle content does not match its expected SHA-256 integrity. */
113
+ export declare class ComponentOnceIntegrityError extends Error {
114
+ /** Expected subresource-integrity-style SHA-256 value. */
115
+ readonly expectedIntegrity: string;
116
+ /** Actual subresource-integrity-style SHA-256 value. */
117
+ readonly actualIntegrity: string;
118
+ constructor(expectedIntegrity: string, actualIntegrity: string);
119
+ }
120
+ /**
121
+ * Compile one trusted module into a deterministic CommonJS artifact.
122
+ *
123
+ * Only caller-declared imports remain external. This generic path does not add React or any other
124
+ * runtime implicitly; every external must be injected explicitly by the host at instantiation.
125
+ */
126
+ export declare function compileTrustedModule(input: ComponentOnceCompileInput): Promise<ComponentOnceTrustedBundleArtifact>;
127
+ /** Compile a trusted React module while keeping the host React singleton external. */
128
+ export declare function compileTrustedReactModule(input: ComponentOnceReactCompileInput): Promise<ComponentOnceTrustedBundleArtifact>;
129
+ /** Calculate a subresource-integrity-style SHA-256 value for bundle text or UTF-8 bytes. */
130
+ export declare function calculateTrustedBundleIntegrity(source: string | Uint8Array): string;
131
+ /** Throw when bundle text or bytes do not match an expected SHA-256 integrity value. */
132
+ export declare function assertTrustedBundleIntegrity(source: string | Uint8Array, expectedIntegrity: string): void;
133
+ /**
134
+ * Execute a trusted bundle with a deliberately narrow CommonJS environment.
135
+ *
136
+ * This API uses `new Function` and is only for trusted internal code. It is not a sandbox: evaluated
137
+ * code retains access to JavaScript globals. The only available `require` values are provided by
138
+ * `options.externals`, so React is always the exact instance selected by the host.
139
+ */
140
+ export declare function instantiateTrustedBundle<TExports = Record<string, unknown>>(source: ComponentOnceTrustedBundleSource, options: InstantiateTrustedBundleOptions): TExports;
141
+ //# sourceMappingURL=compiler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compiler.d.ts","sourceRoot":"","sources":["../src/compiler.ts"],"names":[],"mappings":"AAEA,OAAO,EAKL,KAAK,QAAQ,EAEd,MAAM,SAAS,CAAC;AAEjB,0FAA0F;AAC1F,eAAO,MAAM,mCAAmC,EAAG,8BAAuC,CAAC;AAO3F,wDAAwD;AACxD,MAAM,MAAM,yBAAyB,GAAG,IAAI,GAAG,KAAK,GAAG,IAAI,GAAG,KAAK,CAAC;AAEpE,uEAAuE;AACvE,MAAM,MAAM,oBAAoB,GAAG,WAAW,GAAG,UAAU,GAAG,WAAW,CAAC;AAE1E,uEAAuE;AACvE,MAAM,WAAW,yBAAyB;IACxC,yDAAyD;IACzD,QAAQ,CAAC,MAAM,EAAE,MAAM,CA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