@printy3d/sdk 0.0.0-stage → 0.4.0
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/LICENSE +201 -0
- package/README.md +85 -2
- package/dist/cad-documents.d.ts +169 -0
- package/dist/cad-documents.d.ts.map +1 -0
- package/dist/cad-documents.js +17 -0
- package/dist/cad-documents.js.map +1 -0
- package/dist/client.d.ts +116 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +165 -0
- package/dist/client.js.map +1 -0
- package/dist/errors.d.ts +125 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +202 -0
- package/dist/errors.js.map +1 -0
- package/dist/http.d.ts +70 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +291 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/resources.d.ts +222 -0
- package/dist/resources.d.ts.map +1 -0
- package/dist/resources.js +629 -0
- package/dist/resources.js.map +1 -0
- package/dist/runtime.d.ts +30 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +88 -0
- package/dist/runtime.js.map +1 -0
- package/dist/sources.d.ts +13 -0
- package/dist/sources.d.ts.map +1 -0
- package/dist/sources.js +82 -0
- package/dist/sources.js.map +1 -0
- package/dist/types.d.ts +776 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +18 -0
- package/dist/types.js.map +1 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +2 -0
- package/dist/version.js.map +1 -0
- package/dist/webhooks.d.ts +24 -0
- package/dist/webhooks.d.ts.map +1 -0
- package/dist/webhooks.js +95 -0
- package/dist/webhooks.js.map +1 -0
- package/package.json +48 -4
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime helpers that differ between Node and browsers. Node built-ins are loaded lazily
|
|
3
|
+
* with a computed specifier so a browser bundle never pulls them in; only the path-based
|
|
4
|
+
* helpers (upload from a path, download to a file) need them.
|
|
5
|
+
*/
|
|
6
|
+
import type * as NodeCrypto from "node:crypto";
|
|
7
|
+
import type * as NodeFs from "node:fs";
|
|
8
|
+
import type * as NodeFsPromises from "node:fs/promises";
|
|
9
|
+
import type * as NodePath from "node:path";
|
|
10
|
+
/** Whether this is Node (or a Node-compatible runtime). */
|
|
11
|
+
export declare const isNode: boolean;
|
|
12
|
+
export declare const nodeFs: () => Promise<typeof NodeFs>;
|
|
13
|
+
export declare const nodeFsPromises: () => Promise<typeof NodeFsPromises>;
|
|
14
|
+
export declare const nodePath: () => Promise<typeof NodePath>;
|
|
15
|
+
export declare const nodeCrypto: () => Promise<typeof NodeCrypto>;
|
|
16
|
+
/** An environment variable, or undefined outside Node. */
|
|
17
|
+
export declare function env(name: string): string | undefined;
|
|
18
|
+
/** Lower-case hex SHA-256 of in-memory bytes (WebCrypto, else node:crypto). */
|
|
19
|
+
export declare function sha256Hex(data: Uint8Array): Promise<string>;
|
|
20
|
+
/** An incremental SHA-256 for streamed downloads. */
|
|
21
|
+
export interface Hasher {
|
|
22
|
+
update(chunk: Uint8Array): void;
|
|
23
|
+
digest(): Promise<string>;
|
|
24
|
+
}
|
|
25
|
+
export declare function createHasher(): Promise<Hasher>;
|
|
26
|
+
export declare function concat(chunks: Uint8Array[]): Uint8Array;
|
|
27
|
+
export declare function sleep(ms: number): Promise<void>;
|
|
28
|
+
/** A random RFC 4122 version 4 UUID (WebCrypto, else node:crypto). */
|
|
29
|
+
export declare function uuid4(): Promise<string>;
|
|
30
|
+
//# sourceMappingURL=runtime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,KAAK,UAAU,MAAM,aAAa,CAAC;AAC/C,OAAO,KAAK,KAAK,MAAM,MAAM,SAAS,CAAC;AACvC,OAAO,KAAK,KAAK,cAAc,MAAM,kBAAkB,CAAC;AACxD,OAAO,KAAK,KAAK,QAAQ,MAAM,WAAW,CAAC;AAI3C,2DAA2D;AAC3D,eAAO,MAAM,MAAM,EAAE,OAE6D,CAAC;AAYnF,eAAO,MAAM,MAAM,QAAO,OAAO,CAAC,OAAO,MAAM,CAAoB,CAAC;AACpE,eAAO,MAAM,cAAc,QAAO,OAAO,CAAC,OAAO,cAAc,CAA6B,CAAC;AAC7F,eAAO,MAAM,QAAQ,QAAO,OAAO,CAAC,OAAO,QAAQ,CAAsB,CAAC;AAC1E,eAAO,MAAM,UAAU,QAAO,OAAO,CAAC,OAAO,UAAU,CAAwB,CAAC;AAEhF,0DAA0D;AAC1D,wBAAgB,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAIpD;AAMD,+EAA+E;AAC/E,wBAAsB,SAAS,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,CAQjE;AAED,qDAAqD;AACrD,MAAM,WAAW,MAAM;IACrB,MAAM,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,CAAC;IAChC,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;CAC3B;AAED,wBAAsB,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC,CAepD;AAED,wBAAgB,MAAM,CAAC,MAAM,EAAE,UAAU,EAAE,GAAG,UAAU,CAUvD;AAED,wBAAgB,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE/C;AAED,sEAAsE;AACtE,wBAAsB,KAAK,IAAI,OAAO,CAAC,MAAM,CAAC,CAY7C"}
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime helpers that differ between Node and browsers. Node built-ins are loaded lazily
|
|
3
|
+
* with a computed specifier so a browser bundle never pulls them in; only the path-based
|
|
4
|
+
* helpers (upload from a path, download to a file) need them.
|
|
5
|
+
*/
|
|
6
|
+
import { Printy3DError } from "./errors.js";
|
|
7
|
+
/** Whether this is Node (or a Node-compatible runtime). */
|
|
8
|
+
export const isNode = typeof process !== "undefined" &&
|
|
9
|
+
typeof process.versions?.node === "string";
|
|
10
|
+
async function load(name) {
|
|
11
|
+
if (!isNode) {
|
|
12
|
+
throw new Printy3DError(`This helper needs ${name}, which only Node provides; pass bytes or a Blob instead`);
|
|
13
|
+
}
|
|
14
|
+
const specifier = name;
|
|
15
|
+
return (await import(/* @vite-ignore */ /* webpackIgnore: true */ specifier));
|
|
16
|
+
}
|
|
17
|
+
export const nodeFs = () => load("node:fs");
|
|
18
|
+
export const nodeFsPromises = () => load("node:fs/promises");
|
|
19
|
+
export const nodePath = () => load("node:path");
|
|
20
|
+
export const nodeCrypto = () => load("node:crypto");
|
|
21
|
+
/** An environment variable, or undefined outside Node. */
|
|
22
|
+
export function env(name) {
|
|
23
|
+
if (typeof process === "undefined" || !process.env)
|
|
24
|
+
return undefined;
|
|
25
|
+
const value = process.env[name];
|
|
26
|
+
return value === undefined || value === "" ? undefined : value;
|
|
27
|
+
}
|
|
28
|
+
function hex(bytes) {
|
|
29
|
+
return Array.from(new Uint8Array(bytes), (b) => b.toString(16).padStart(2, "0")).join("");
|
|
30
|
+
}
|
|
31
|
+
/** Lower-case hex SHA-256 of in-memory bytes (WebCrypto, else node:crypto). */
|
|
32
|
+
export async function sha256Hex(data) {
|
|
33
|
+
const subtle = globalThis.crypto?.subtle;
|
|
34
|
+
if (subtle) {
|
|
35
|
+
const view = new Uint8Array(data); // a copy backed by a plain ArrayBuffer
|
|
36
|
+
return hex(await subtle.digest("SHA-256", view));
|
|
37
|
+
}
|
|
38
|
+
const crypto = await nodeCrypto();
|
|
39
|
+
return crypto.createHash("sha256").update(data).digest("hex");
|
|
40
|
+
}
|
|
41
|
+
export async function createHasher() {
|
|
42
|
+
if (isNode) {
|
|
43
|
+
const crypto = await nodeCrypto();
|
|
44
|
+
const hash = crypto.createHash("sha256");
|
|
45
|
+
return {
|
|
46
|
+
update: (chunk) => void hash.update(chunk),
|
|
47
|
+
digest: async () => hash.digest("hex"),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
// Browsers have no incremental WebCrypto digest: buffer, then hash once.
|
|
51
|
+
const chunks = [];
|
|
52
|
+
return {
|
|
53
|
+
update: (chunk) => void chunks.push(chunk),
|
|
54
|
+
digest: async () => sha256Hex(concat(chunks)),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
export function concat(chunks) {
|
|
58
|
+
let size = 0;
|
|
59
|
+
for (const chunk of chunks)
|
|
60
|
+
size += chunk.byteLength;
|
|
61
|
+
const out = new Uint8Array(size);
|
|
62
|
+
let offset = 0;
|
|
63
|
+
for (const chunk of chunks) {
|
|
64
|
+
out.set(chunk, offset);
|
|
65
|
+
offset += chunk.byteLength;
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
export function sleep(ms) {
|
|
70
|
+
return new Promise((resolve) => setTimeout(resolve, Math.max(0, ms)));
|
|
71
|
+
}
|
|
72
|
+
/** A random RFC 4122 version 4 UUID (WebCrypto, else node:crypto). */
|
|
73
|
+
export async function uuid4() {
|
|
74
|
+
const web = globalThis.crypto;
|
|
75
|
+
if (web && typeof web.randomUUID === "function")
|
|
76
|
+
return web.randomUUID();
|
|
77
|
+
if (isNode)
|
|
78
|
+
return (await nodeCrypto()).randomUUID();
|
|
79
|
+
if (!web || typeof web.getRandomValues !== "function") {
|
|
80
|
+
throw new Printy3DError("No secure random source to generate an Idempotency-Key");
|
|
81
|
+
}
|
|
82
|
+
const bytes = web.getRandomValues(new Uint8Array(16));
|
|
83
|
+
bytes[6] = ((bytes[6] ?? 0) & 0x0f) | 0x40;
|
|
84
|
+
bytes[8] = ((bytes[8] ?? 0) & 0x3f) | 0x80;
|
|
85
|
+
const h = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
86
|
+
return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}`;
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=runtime.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.js","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAOH,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,2DAA2D;AAC3D,MAAM,CAAC,MAAM,MAAM,GACjB,OAAO,OAAO,KAAK,WAAW;IAC9B,OAAQ,OAA4C,CAAC,QAAQ,EAAE,IAAI,KAAK,QAAQ,CAAC;AAEnF,KAAK,UAAU,IAAI,CAAI,IAAY;IACjC,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,MAAM,IAAI,aAAa,CACrB,qBAAqB,IAAI,0DAA0D,CACpF,CAAC;IACJ,CAAC;IACD,MAAM,SAAS,GAAG,IAAI,CAAC;IACvB,OAAO,CAAC,MAAM,MAAM,CAAC,kBAAkB,CAAC,yBAAyB,CAAC,SAAS,CAAC,CAAM,CAAC;AACrF,CAAC;AAED,MAAM,CAAC,MAAM,MAAM,GAAG,GAA2B,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;AACpE,MAAM,CAAC,MAAM,cAAc,GAAG,GAAmC,EAAE,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAC;AAC7F,MAAM,CAAC,MAAM,QAAQ,GAAG,GAA6B,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;AAC1E,MAAM,CAAC,MAAM,UAAU,GAAG,GAA+B,EAAE,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;AAEhF,0DAA0D;AAC1D,MAAM,UAAU,GAAG,CAAC,IAAY;IAC9B,IAAI,OAAO,OAAO,KAAK,WAAW,IAAI,CAAC,OAAO,CAAC,GAAG;QAAE,OAAO,SAA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E,IAAI,MAAM;QAAE,OAAO,CAAC,MAAM,UAAU,EAAE,CAAC,CAAC,UAAU,EAAE,CAAC;IACrD,IAAI,CAAC,GAAG,IAAI,OAAO,GAAG,CAAC,eAAe,KAAK,UAAU,EAAE,CAAC;QACtD,MAAM,IAAI,aAAa,CAAC,wDAAwD,CAAC,CAAC;IACpF,CAAC;IACD,MAAM,KAAK,GAAG,GAAG,CAAC,eAAe,CAAC,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC,CAAC;IACtD,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;IAC3C,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;IAC3C,MAAM,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC7E,OAAO,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE,CAAC;AACnG,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Byte sources for uploads: a file path (Node), bytes, or a Blob / File (anywhere). */
|
|
2
|
+
/** What `upload()` accepts: a local path (Node only), bytes, or a Blob / File. */
|
|
3
|
+
export type UploadSource = string | Uint8Array | ArrayBuffer | Blob;
|
|
4
|
+
export interface ByteSource {
|
|
5
|
+
/** The file name when the source carries one (path basename, `File.name`). */
|
|
6
|
+
readonly name: string | undefined;
|
|
7
|
+
readonly size: number;
|
|
8
|
+
read(offset: number, length: number): Promise<Uint8Array>;
|
|
9
|
+
sha256(): Promise<string>;
|
|
10
|
+
close(): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
export declare function openSource(source: UploadSource): Promise<ByteSource>;
|
|
13
|
+
//# sourceMappingURL=sources.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sources.d.ts","sourceRoot":"","sources":["../src/sources.ts"],"names":[],"mappings":"AAAA,wFAAwF;AAKxF,kFAAkF;AAClF,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,UAAU,GAAG,WAAW,GAAG,IAAI,CAAC;AAEpE,MAAM,WAAW,UAAU;IACzB,8EAA8E;IAC9E,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC;IAClC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IAC1D,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAC1B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAED,wBAAsB,UAAU,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,UAAU,CAAC,CAM1E"}
|
package/dist/sources.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/** Byte sources for uploads: a file path (Node), bytes, or a Blob / File (anywhere). */
|
|
2
|
+
import { Printy3DError } from "./errors.js";
|
|
3
|
+
import { createHasher, nodeFs, nodeFsPromises, nodePath, sha256Hex } from "./runtime.js";
|
|
4
|
+
export async function openSource(source) {
|
|
5
|
+
if (typeof source === "string")
|
|
6
|
+
return openPath(source);
|
|
7
|
+
if (source instanceof Uint8Array)
|
|
8
|
+
return bytesSource(source, undefined);
|
|
9
|
+
if (source instanceof ArrayBuffer)
|
|
10
|
+
return bytesSource(new Uint8Array(source), undefined);
|
|
11
|
+
if (typeof Blob !== "undefined" && source instanceof Blob)
|
|
12
|
+
return blobSource(source);
|
|
13
|
+
throw new Printy3DError("Unsupported upload source: pass a path, bytes or a Blob");
|
|
14
|
+
}
|
|
15
|
+
function bytesSource(data, name) {
|
|
16
|
+
return {
|
|
17
|
+
name,
|
|
18
|
+
size: data.byteLength,
|
|
19
|
+
read: async (offset, length) => data.slice(offset, offset + length),
|
|
20
|
+
sha256: () => sha256Hex(data),
|
|
21
|
+
close: async () => undefined,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function blobSource(blob) {
|
|
25
|
+
const named = blob.name;
|
|
26
|
+
const name = typeof named === "string" && named ? named : undefined;
|
|
27
|
+
return {
|
|
28
|
+
name,
|
|
29
|
+
size: blob.size,
|
|
30
|
+
read: async (offset, length) => new Uint8Array(await blob.slice(offset, offset + length).arrayBuffer()),
|
|
31
|
+
sha256: async () => {
|
|
32
|
+
const hasher = await createHasher();
|
|
33
|
+
const reader = blob.stream().getReader();
|
|
34
|
+
for (;;) {
|
|
35
|
+
const next = await reader.read();
|
|
36
|
+
if (next.done)
|
|
37
|
+
break;
|
|
38
|
+
hasher.update(next.value);
|
|
39
|
+
}
|
|
40
|
+
return hasher.digest();
|
|
41
|
+
},
|
|
42
|
+
close: async () => undefined,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
async function openPath(path) {
|
|
46
|
+
const [fsp, fs, pathMod] = await Promise.all([nodeFsPromises(), nodeFs(), nodePath()]);
|
|
47
|
+
const handle = await fsp.open(path, "r");
|
|
48
|
+
try {
|
|
49
|
+
const { size } = await handle.stat();
|
|
50
|
+
return {
|
|
51
|
+
name: pathMod.basename(path),
|
|
52
|
+
size,
|
|
53
|
+
read: async (offset, length) => {
|
|
54
|
+
const buffer = new Uint8Array(length);
|
|
55
|
+
let filled = 0;
|
|
56
|
+
while (filled < length) {
|
|
57
|
+
const { bytesRead } = await handle.read(buffer, filled, length - filled, offset + filled);
|
|
58
|
+
if (bytesRead === 0)
|
|
59
|
+
break;
|
|
60
|
+
filled += bytesRead;
|
|
61
|
+
}
|
|
62
|
+
return filled === length ? buffer : buffer.slice(0, filled);
|
|
63
|
+
},
|
|
64
|
+
sha256: async () => {
|
|
65
|
+
const hasher = await createHasher();
|
|
66
|
+
await new Promise((resolve, reject) => {
|
|
67
|
+
const stream = fs.createReadStream(path, { highWaterMark: 1024 * 1024 });
|
|
68
|
+
stream.on("data", (chunk) => hasher.update(chunk));
|
|
69
|
+
stream.on("end", () => resolve());
|
|
70
|
+
stream.on("error", reject);
|
|
71
|
+
});
|
|
72
|
+
return hasher.digest();
|
|
73
|
+
},
|
|
74
|
+
close: () => handle.close(),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
await handle.close();
|
|
79
|
+
throw error;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=sources.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sources.js","sourceRoot":"","sources":["../src/sources.ts"],"names":[],"mappings":"AAAA,wFAAwF;AAExF,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,cAAc,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAczF,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,MAAoB;IACnD,IAAI,OAAO,MAAM,KAAK,QAAQ;QAAE,OAAO,QAAQ,CAAC,MAAM,CAAC,CAAC;IACxD,IAAI,MAAM,YAAY,UAAU;QAAE,OAAO,WAAW,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IACxE,IAAI,MAAM,YAAY,WAAW;QAAE,OAAO,WAAW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