diff --git a/Math/LineIntersections.ts b/Math/LineIntersections.ts new file mode 100644 index 0000000..fb14c28 --- /dev/null +++ b/Math/LineIntersections.ts @@ -0,0 +1,113 @@ +/** + * @author Peter Kelley + * @author pgkelley4@gmail.com + */ + +import type { Vector } from "./Vector"; + +/** + * See if two line segments intersect. This uses the + * vector cross product approach described below: + * http://stackoverflow.com/a/565282/786339 + * + * @param {Object} p point object with x and y coordinates + * representing the start of the 1st line. + * @param {Object} p2 point object with x and y coordinates + * representing the end of the 1st line. + * @param {Object} q point object with x and y coordinates + * representing the start of the 2nd line. + * @param {Object} q2 point object with x and y coordinates + * representing the end of the 2nd line. + */ + +export interface Point { + x: number; + y: number; +} + +export function getLineSegmentsIntersection(p1: Vector, p2: Vector, p3: Vector, p4: Vector): Point | null { + const d = (p2.x - p1.x) * (p4.y - p3.y) - (p2.y - p1.y) * (p4.x - p3.x); + + if (d === 0) { + // Lines are parallel or collinear + return null; // For now, we don't handle collinear intersections + } + + const t = ((p3.x - p1.x) * (p4.y - p3.y) - (p3.y - p1.y) * (p4.x - p3.x)) / d; + const u = -((p1.x - p3.x) * (p2.y - p1.y) - (p1.y - p3.y) * (p2.x - p1.x)) / d; + + if (t >= 0 && t <= 1 && u >= 0 && u <= 1) { + return { + x: p1.x + t * (p2.x - p1.x), + y: p1.y + t * (p2.y - p1.y), + }; + } + + return null; +} + +/** + * Calculate the cross product of the two points. + * + * @param {Object} point1 point object with x and y coordinates + * @param {Object} point2 point object with x and y coordinates + * + * @return the cross product result as a float + */ +function crossProduct(point1: Point, point2: Point): number { + return point1.x * point2.y - point1.y * point2.x; +} + +/** + * Subtract the second point from the first. + * + * @param {Object} point1 point object with x and y coordinates + * @param {Object} point2 point object with x and y coordinates + * + * @return the subtraction result as a point object + */ +function subtractPoints(point1: Point, point2: Point): Point { + return { + x: point1.x - point2.x, + y: point1.y - point2.y + } +} + +/** + * See if the points are equal. + * + * @param {Object} point1 point object with x and y coordinates + * @param {Object} point2 point object with x and y coordinates + * + * @return if the points are equal + */ +function equalPoints(point1: Point, point2: Point): boolean { + return (point1.x == point2.x) && (point1.y == point2.y) +} + +/** + * See if all arguments are equal. + * + * @param {...} args arguments that will be compared by '=='. + * + * @return if all arguments are equal + */ +function allEqual(...args: any[]): boolean { + var firstValue = args[0], + i; + for (i = 1; i < args.length; i += 1) { + if (args[i] != firstValue) { + return false; + } + } + return true; +} + + + +/** Modifications by @sojs-coder + * + * + * - Type annotation + * + */ \ No newline at end of file diff --git a/Math/PolygonUnion.ts b/Math/PolygonUnion.ts new file mode 100644 index 0000000..e09eebc --- /dev/null +++ b/Math/PolygonUnion.ts @@ -0,0 +1,64 @@ +import * as polygonClipping from 'polygon-clipping'; +import { Vector } from './Vector'; + +/** + * Unions an array of polygons (each a Vector[]) and returns an array of resulting polygons (Vector[][]). + * Always returns Vector[][], even for empty or single input. + */ +export function unionPolygons(polygons: Vector[][]): Vector[][] { + if (polygons.length === 0) return []; + if (polygons.length === 1) return [polygons[0]]; + try { + const convertedPolygons: polygonClipping.Polygon[] = polygons.map(polygon => { + const coords = polygon.map(vertex => vertex.toArray()); + if (coords.length > 0) { + const first = coords[0]; + const last = coords[coords.length - 1]; + if (first[0] !== last[0] || first[1] !== last[1]) { + coords.push([first[0], first[1]]); + } + } + return [coords]; + }); + let result: polygonClipping.MultiPolygon = [convertedPolygons[0]]; + for (let i = 1; i < convertedPolygons.length; i++) { + result = polygonClipping.union(result, [convertedPolygons[i]]); + if (!result || result.length === 0) { + console.warn('Union operation resulted in empty geometry at step', i); + return []; + } + } + if (!result || result.length === 0) { + return []; + } + // Convert all resulting polygons to Vector[][] + return result.map(polygon => { + const coords = polygon[0]; + let vectors = coords.map((coord: number[]) => new Vector(coord[0], coord[1])); + if (vectors.length > 1) { + const first = vectors[0]; + const last = vectors[vectors.length - 1]; + if (first.x === last.x && first.y === last.y) { + vectors = vectors.slice(0, -1); + } + } + return vectors; + }); + } catch (error) { + console.error('Error during polygon union:', error); + // Fallback: return all input polygons + return polygons; + } +} + +// Helper function to calculate polygon area from coordinate array +function calculatePolygonArea(coords: number[][]): number { + if (coords.length < 3) return 0; + let area = 0; + for (let i = 0; i < coords.length; i++) { + const j = (i + 1) % coords.length; + area += coords[i][0] * coords[j][1]; + area -= coords[j][0] * coords[i][1]; + } + return Math.abs(area) / 2; +} \ No newline at end of file diff --git a/Math/SpatialGrid.ts b/Math/SpatialGrid.ts new file mode 100644 index 0000000..b9ca141 --- /dev/null +++ b/Math/SpatialGrid.ts @@ -0,0 +1,64 @@ + +import type { Collider } from '../Parts/Children/Collider'; +import type { Vector } from './Vector'; + +export class SpatialGrid { + private cells: Map; + private cellSize: number; + + constructor(cellSize: number) { + this.cells = new Map(); + this.cellSize = cellSize; + } + + private getKey(x: number, y: number): string { + return `${Math.floor(x / this.cellSize)}_${Math.floor(y / this.cellSize)}`; + } + + clear() { + this.cells.clear(); + } + + insert(collider: Collider) { + const start = collider.realWorldStart; + const end = collider.realWorldEnd; + + const startX = Math.floor(start.x / this.cellSize); + const startY = Math.floor(start.y / this.cellSize); + const endX = Math.floor(end.x / this.cellSize); + const endY = Math.floor(end.y / this.cellSize); + + for (let x = startX; x <= endX; x++) { + for (let y = startY; y <= endY; y++) { + const key = `${x}_${y}`; + if (!this.cells.has(key)) { + this.cells.set(key, []); + } + this.cells.get(key)!.push(collider); + } + } + } + + query(collider: Collider): Collider[] { + const candidates = new Set(); + const start = collider.realWorldStart; + const end = collider.realWorldEnd; + + const startX = Math.floor(start.x / this.cellSize); + const startY = Math.floor(start.y / this.cellSize); + const endX = Math.floor(end.x / this.cellSize); + const endY = Math.floor(end.y / this.cellSize); + + for (let x = startX; x <= endX; x++) { + for (let y = startY; y <= endY; y++) { + const key = `${x}_${y}`; + if (this.cells.has(key)) { + for (const other of this.cells.get(key)!) { + candidates.add(other); + } + } + } + } + return Array.from(candidates); + } +} diff --git a/Math/Vector.ts b/Math/Vector.ts index 9b6623e..e1d591c 100644 --- a/Math/Vector.ts +++ b/Math/Vector.ts @@ -1,3 +1,5 @@ +import type { Point } from "./LineIntersections"; + export class Vector { x: number; y: number; @@ -54,7 +56,9 @@ export class Vector { } normalize(): Vector { const len = this.length(); - if (len === 0) throw new Error("Cannot normalize zero-length vector"); + if (len === 0) { + return new Vector(0, 0); + } return new Vector(this.x / len, this.y / len); } dot(other: Vector): number { @@ -80,7 +84,11 @@ export class Vector { this.y += other.y; return this; } - static From(scalar: number): Vector { - return new Vector(scalar, scalar); + static From(scalar: number | Point): Vector { + if (typeof scalar === "number") { + return new Vector(scalar, scalar); + } else { + return new Vector(scalar.x, scalar.y); + } } } \ No newline at end of file diff --git a/Parts/CharacterMovement.ts b/Parts/CharacterMovement.ts index 6da86ba..f3c61cd 100644 --- a/Parts/CharacterMovement.ts +++ b/Parts/CharacterMovement.ts @@ -16,7 +16,7 @@ export class CharacterMovement extends Part { this.type = "CharacterMovement"; } - act(_delta: number): void { + act(delta: number): void { if (!this.input) { if (!this.warned.has("MissingInput")) this.top?.warn(`CharacterMovement <${this.name}> (${this.id}) is missing an input property. Please create an input on the scene and pass it.`) ? this.warned.add("MissingInput") : null; return; @@ -26,7 +26,7 @@ export class CharacterMovement extends Part { if (!transform) { return; } - + const speed = this.speed * delta; const keys = this.input.downkeys; let dx = 0; let dy = 0; @@ -66,7 +66,7 @@ export class CharacterMovement extends Part { dy *= Math.SQRT1_2; } if (dx !== 0 || dy !== 0) { - transform.move(new Vector(dx * this.speed, dy * this.speed)); + transform.move(new Vector(dx * speed, dy * speed)); } } } \ No newline at end of file diff --git a/Parts/Children/BoxCollider.ts b/Parts/Children/BoxCollider.ts index b5c275d..d374d0e 100644 --- a/Parts/Children/BoxCollider.ts +++ b/Parts/Children/BoxCollider.ts @@ -3,7 +3,9 @@ import { Game } from "../Game"; import { Part } from "../Part"; import { Collider } from "./Collider"; import { Transform } from "./Transform"; +import { MultiPolygonCollider } from "./MultiPolygonCollider"; import { PolygonCollider } from "./PolygonCollider"; +import type { Polygon } from "martinez-polygon-clipping"; export class BoxCollider extends Collider { start: Vector; @@ -16,8 +18,8 @@ export class BoxCollider extends Collider { private lastRotation: number = NaN; private lastScale: Vector = new Vector(NaN, NaN); - constructor({ width, height, tag }: { width: number; height: number; tag?: string }) { - super({ tag }); + constructor({ width, height, tag = "" }: { width: number; height: number; tag?: string }) { + super({ tag, allowMerge: tag !== '' }); this.name = "BoxCollider"; this.width = width; this.height = height; @@ -36,6 +38,10 @@ export class BoxCollider extends Collider { return this.rotatedCorners; } + getGeometry(): Polygon { + return [this.worldVertices.map(v => v.toArray())]; + } + updateCollider(transform: Transform) { const cos = Math.cos(transform.rotation); const sin = Math.sin(transform.rotation); @@ -78,7 +84,7 @@ export class BoxCollider extends Collider { narrowPhaseCheck(other: Collider): boolean { if (other instanceof BoxCollider) { return this.checkBoxVsBox(this, other); - } else if (other instanceof PolygonCollider) { + } else if (other instanceof PolygonCollider || other instanceof MultiPolygonCollider) { return other.narrowPhaseCheck(this); } @@ -96,6 +102,19 @@ export class BoxCollider extends Collider { return true; } + override _updateVerticesAfterMerge(polygons: Vector[][][]): void { + const newCollider = new MultiPolygonCollider({ polygons, tag: this.tag }); + + newCollider.active = this.active; + newCollider.allowMerge = this.allowMerge; + + const parent = this.parent; + if (parent) { + parent.removeChild(this); + parent.addChild(newCollider); + } + } + act(delta: number) { super.act(delta); } diff --git a/Parts/Children/Collider.bak.txt b/Parts/Children/Collider.bak.txt new file mode 100644 index 0000000..f8f4dde --- /dev/null +++ b/Parts/Children/Collider.bak.txt @@ -0,0 +1,456 @@ +import { getLineSegmentsIntersection } from "../../Math/LineIntersections"; +import { Vector } from "../../Math/Vector"; +import { vecEq, pointInPoly } from "../../helpers"; +import type { Camera } from "../Camera"; +import { Game } from "../Game"; +import { Part } from "../Part"; +import type { Transform } from "./Transform"; + +export abstract class Collider extends Part { + colliding: boolean = false; + collidingWith: Set = new Set(); + tag: string = ""; + radius: number; + realWorldStart: Vector; + realWorldEnd: Vector; + vertices: Vector[]; + active: boolean = true; + allowMerge: boolean; + randomTestingColors: [number, number, number]; + constructor({ tag, allowMerge }: { tag?: string, allowMerge?: boolean }) { + super({ name: "Collider" }); + this.type = "Collider"; + this.base = "Collider"; + this.tag = tag || ""; + this.radius = 0; + this.realWorldStart = new Vector(0, 0); + this.allowMerge = allowMerge !== undefined ? allowMerge : true; + this.realWorldEnd = new Vector(0, 0); + this.randomTestingColors = [ + Math.random() * 255, + Math.random() * 255, + Math.random() * 255 + ]; + this.vertices = []; + } + + setTag(tag: string) { + this.tag = tag; + } + + abstract override clone(memo?: Map): this; + + onMount(parent: Part) { + super.onMount(parent); + const transform = this.sibling("Transform"); + if (!transform) { + this.top?.warn( + `Collider <${this.name}> (${this.id}) does not have Transform sibling. Please ensure you add a Transform component.` + ); + return; + } + this.updateCollider(transform); + } + + abstract get worldVertices(): Vector[]; + + abstract narrowPhaseCheck(other: Collider): boolean; + + abstract updateCollider(transform: Transform): void; + + abstract drawDebug(ctx: CanvasRenderingContext2D): void; + + abstract _updateVerticesAfterMerge(vertices: Vector[]): void; + + private isMergedAndConcave: boolean = false; + private convexParts: Vector[][] = []; + + private isConvex(vertices: Vector[]): boolean { + if (vertices.length < 4) return true; + let sign = 0; + for (let i = 0; i < vertices.length; i++) { + const p1 = vertices[i]; + const p2 = vertices[(i + 1) % vertices.length]; + const p3 = vertices[(i + 2) % vertices.length]; + const crossProduct = (p2.x - p1.x) * (p3.y - p2.y) - (p2.y - p1.y) * (p3.x - p2.x); + if (Math.abs(crossProduct) > 1e-9) { + if (sign === 0) { + sign = Math.sign(crossProduct); + } else if (Math.sign(crossProduct) !== sign) { + return false; + } + } + } + return true; + } + + private triangulate(vertices: Vector[]): Vector[][] { + const triangles: Vector[][] = []; + const polygon = [...vertices]; + + while (polygon.length > 3) { + let earFound = false; + for (let i = 0; i < polygon.length; i++) { + const p1 = polygon[(i + polygon.length - 1) % polygon.length]; + const p2 = polygon[i]; + const p3 = polygon[(i + 1) % polygon.length]; + + const crossProduct = (p2.x - p1.x) * (p3.y - p2.y) - (p2.y - p1.y) * (p3.x - p2.x); + + if (crossProduct < 0) continue; + + const triangle = [p1, p2, p3]; + let isEar = true; + + for (let j = 0; j < polygon.length; j++) { + if (j === i || j === (i + 1) % polygon.length || j === (i + polygon.length - 1) % polygon.length) continue; + if (pointInPoly(polygon[j], triangle)) { + isEar = false; + break; + } + } + + if (isEar) { + triangles.push(triangle); + polygon.splice(i, 1); + earFound = true; + break; + } + } + if (!earFound) { + break; + } + } + if (polygon.length === 3) { + triangles.push(polygon); + } + return triangles; + } + + onRegister(attribute: string, value: any) { + super.onRegister(attribute, value); + if (attribute === "layer") { + value.flats.colliders.push(this); + } + } + + + evaluateMerging() { + const layer = this.registrations["layer"]; + if (!layer) return; + + const fellowColliders = layer.flats.colliders.filter((c: Collider) => c.tag == this.tag && c.id !== this.id && c.allowMerge && c.active); + if (fellowColliders.length == 0) return; + + for (const fellow of fellowColliders) { + if (this.id < fellow.id && this.checkCollision(fellow, true)) { + this.mergeWith(fellow); + } + } + } + onStart() { + if (this.allowMerge) this.evaluateMerging(); + } + + mergeWith(other: Collider) { + if (this.tag !== other.tag || other.tag == "" || this.tag == "") return; + + const thisTransform = this.sibling("Transform"); + if (!thisTransform) { + this.top?.warn(`Collider <${this.name}> has no Transform sibling, cannot merge.`); + return; + } + + const poly1 = this.worldVertices; + const poly2 = other.worldVertices; + + // --- Conservative Merge Check --- + let sharedEdges = 0; + const tolerance = 1e-5; + + for (let i = 0; i < poly1.length; i++) { + const p1_v1 = poly1[i]; + const p1_v2 = poly1[(i + 1) % poly1.length]; + for (let j = 0; j < poly2.length; j++) { + const p2_v1 = poly2[j]; + const p2_v2 = poly2[(j + 1) % poly2.length]; + if (p1_v1.distance(p2_v2) < tolerance && p1_v2.distance(p2_v1) < tolerance) { + sharedEdges++; + } + } + } + + if (sharedEdges === 0) return; // Not adjacent, abort merge + + for(const p of poly1) { + if(pointInPoly(p, poly2) && !poly2.some(p2 => p.distance(p2) < tolerance)) return; + } + for(const p of poly2) { + if(pointInPoly(p, poly1) && !poly1.some(p1 => p.distance(p1) < tolerance)) return; + } + + // --- Edge Cancellation Merge --- + const getEdges = (vertices: Vector[]): Vector[][] => { + const edges: Vector[][] = []; + for (let i = 0; i < vertices.length; i++) { + edges.push([vertices[i], vertices[(i + 1) % vertices.length]]); + } + return edges; + }; + + const allEdges = [...getEdges(poly1), ...getEdges(poly2)]; + const edgeMap = new Map(); + const vecToKey = (v: Vector) => `${Math.round(v.x / tolerance)},${Math.round(v.y / tolerance)}`; + + for (const edge of allEdges) { + const key = `${vecToKey(edge[0])}->${vecToKey(edge[1])}`; + edgeMap.set(key, (edgeMap.get(key) || 0) + 1); + } + + const finalEdges: Vector[][] = []; + for (const [key, count] of edgeMap.entries()) { + const [p1Key, p2Key] = key.split('->'); + const revKey = `${p2Key}->${p1Key}`; + const revCount = edgeMap.get(revKey) || 0; + if (count > revCount) { + const edge = allEdges.find(e => vecToKey(e[0]) === p1Key && vecToKey(e[1]) === p2Key); + if (edge) { + for (let i = 0; i < count - revCount; i++) { + finalEdges.push(edge); + } + } + } + } + + if (finalEdges.length === 0) { + other.inactivate(); + return; + } + + const stitchMap = new Map(); + for (const edge of finalEdges) { + stitchMap.set(vecToKey(edge[0]), edge[1]); + } + + const hull: Vector[] = []; + let current = finalEdges[0][0]; + let startKey = vecToKey(current); + + for (let i = 0; i < finalEdges.length; i++) { + hull.push(current); + const currentKey = vecToKey(current); + current = stitchMap.get(currentKey)!; + if (vecToKey(current) === startKey) break; + } + + if (hull.length < 3) { + other.inactivate(); + return; + } + + const newLocalVertices = hull.map(v => v.subtract(thisTransform.worldPosition)); + this._updateVerticesAfterMerge(newLocalVertices); + + if (!this.isConvex(newLocalVertices)) { + this.isMergedAndConcave = true; + this.convexParts = this.triangulate(newLocalVertices); + } + + other.inactivate(); + } + + inactivate() { + this.active = false; + } + activate() { + this.active = true; + } + act(delta: number): void { + super.act(delta); + if (!this.active) return; + if (!this.registrations?.layer) { + throw new Error(`Collider <${this.name}> (${this.id}) is not registered to a layer. Collisions will not be checked.`); + } + + const transform = this.sibling("Transform"); + if (!transform) return; + + this.updateCollider(transform); + this.colliding = false; + this.collidingWith.clear(); + + const layer = this.registrations.layer as Part; + const colliders = layer.flats.colliders as Collider[]; + for (const other of colliders) { + if (other === this) continue; + if (this.checkCollision(other)) { + this.colliding = true; + this.collidingWith.add(other); + } + } + + this.hoverbug = `${this.colliding ? "🟥" : "🟩"} - ${Array.from(this.collidingWith).map(o => o.name).join(", ")} objects`; + + // Debugging manually + + const fill = this.active; + + const ctx = this.top instanceof Game ? this.top.context : null; + if (ctx) { + ctx.beginPath(); + ctx.strokeStyle = `rgb(${this.randomTestingColors[0]}, ${this.randomTestingColors[1]}, ${this.randomTestingColors[2]})`; + ctx.fillStyle = fill ? `rgba(${this.randomTestingColors[0]}, ${this.randomTestingColors[1]}, ${this.randomTestingColors[2]}, 0.5)` : "transparent"; + + ctx.moveTo(this.worldVertices[0].x, this.worldVertices[0].y); + for (const vertex of this.worldVertices) { + ctx.lineTo(vertex.x, vertex.y); + } + ctx.closePath(); + ctx.stroke(); + ctx.fill(); + } + this.evaluateMerging(); + + if (this.top instanceof Game && this.top.devmode) { + const ctx = this.top.context; + if (ctx) { + this.drawDebug(ctx); + } + } + } + + checkCollision(other: Collider, ignoreTags: boolean = false): boolean { + const thisTransform = this.sibling("Transform"); + const otherTransform = other.sibling("Transform"); + + if (!thisTransform || !otherTransform) { + return false; // Cannot check collision without transforms + } + + this.updateCollider(thisTransform); + other.updateCollider(otherTransform); + + if (!this.active || !other.active) return false; + + if (!ignoreTags && other.tag === this.tag && this.tag !== "") return false; + + const checkParts = (collider: Collider, parts: Vector[][], transform: Transform, otherCollider: Collider) => { + for (const part of parts) { + const worldPart = part.map(p => p.add(transform.worldPosition)); + if (otherCollider.isMergedAndConcave) { + const otherTransform = otherCollider.sibling("Transform")!; + for (const otherPart of otherCollider.convexParts) { + const otherWorldPart = otherPart.map(p => p.add(otherTransform.worldPosition)); + if (this.checkVerticesAgainstVertices(worldPart, otherWorldPart)) return true; + } + } else { + if (this.checkVerticesAgainstVertices(worldPart, otherCollider.worldVertices)) return true; + } + } + return false; + } + + if (this.isMergedAndConcave) { + return checkParts(this, this.convexParts, thisTransform, other); + } + + if (other.isMergedAndConcave) { + return checkParts(other, other.convexParts, otherTransform, this); + } + + if ( + this.realWorldEnd.x < other.realWorldStart.x || + this.realWorldStart.x > other.realWorldEnd.x || + this.realWorldEnd.y < other.realWorldStart.y || + this.realWorldStart.y > other.realWorldEnd.y + ) { + return false; + } + + return this.narrowPhaseCheck(other); + } + + isVisible(camera: Camera): boolean { + if (!this.top) { + throw new Error("Collider cannot calculate visibility without a 'top' (Game instance)."); + } + + const transform = this.sibling("Transform"); + if (!transform) { + return false; + } + this.updateCollider(transform); + + const { offset, scale } = camera.getViewMatrix(); + const cameraPos = offset.multiply(-1); + + const screenWidth = this.top.width; + const screenHeight = this.top.height; + + const viewWidth = screenWidth / scale.x; + const viewHeight = screenHeight / scale.y; + + const cameraVertices = [ + new Vector(cameraPos.x - viewWidth / 2, cameraPos.y - viewHeight / 2), + new Vector(cameraPos.x + viewWidth / 2, cameraPos.y - viewHeight / 2), + new Vector(cameraPos.x + viewWidth / 2, cameraPos.y + viewHeight / 2), + new Vector(cameraPos.x - viewWidth / 2, cameraPos.y + viewHeight / 2) + ]; + + return this.checkVerticesAgainstVertices(this.worldVertices, cameraVertices); + } + + protected checkVerticesAgainstVertices(vertices1: Vector[], vertices2: Vector[]): boolean { + const axes1 = this.getAxes(vertices1); + for (const axis of axes1) { + const projection1 = this.project(vertices1, axis); + const projection2 = this.project(vertices2, axis); + + if (!this.overlap(projection1, projection2)) { + return false; + } + } + + const axes2 = this.getAxes(vertices2); + for (const axis of axes2) { + const projection1 = this.project(vertices1, axis); + const projection2 = this.project(vertices2, axis); + + if (!this.overlap(projection1, projection2)) { + return false; + } + } + + return true; + } + + protected getAxes(vertices: Vector[]): Vector[] { + const axes: Vector[] = []; + for (let i = 0; i < vertices.length; i++) { + const p1 = vertices[i]; + const p2 = vertices[i === vertices.length - 1 ? 0 : i + 1]; + const edge = p2.subtract(p1); + const normal = new Vector(-edge.y, edge.x).normalize(); + axes.push(normal); + } + return axes; + } + + protected project(vertices: Vector[], axis: Vector): { min: number, max: number } { + let min = axis.dot(vertices[0]); + let max = min; + for (let i = 1; i < vertices.length; i++) { + const p = axis.dot(vertices[i]); + if (p < min) { + min = p; + } else if (p > max) { + max = p; + } + } + return { min, max }; + } + + protected overlap(proj1: { min: number, max: number }, proj2: { min: number, max: number }): boolean { + return proj1.max >= proj2.min && proj2.max >= proj1.min; + } +} \ No newline at end of file diff --git a/Parts/Children/Collider.ts b/Parts/Children/Collider.ts index 0737822..19d8d06 100644 --- a/Parts/Children/Collider.ts +++ b/Parts/Children/Collider.ts @@ -2,8 +2,69 @@ import { Vector } from "../../Math/Vector"; import type { Camera } from "../Camera"; import { Game } from "../Game"; import { Part } from "../Part"; +import type { Layer } from "../Layer"; +import { MultiPolygonCollider } from "./MultiPolygonCollider"; import type { Transform } from "./Transform"; +import * as martinez from 'martinez-polygon-clipping'; +/** if (this.tag !== other.tag || other.tag == "" || this.tag == "") return; + + const thisTransform = this.sibling("Transform"); + if (!thisTransform) { + this.top?.warn(`Collider <${this.name}> has no Transform sibling, cannot merge.`); + return; + } + + // 1. Combine vertices from both colliders + const allVertices = [...this.worldVertices, ...other.worldVertices]; + + // 2. Compute the convex hull of the combined vertices using the Monotone Chain algorithm. + // This is a robust way to find the union of two convex polygons. + allVertices.sort((a, b) => { + return a.x < b.x || (a.x == b.x && a.y < b.y) ? -1 : 1; + }); + const cross = (o: Vector, a: Vector, b: Vector) => { + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); + }; + + const lower: Vector[] = []; + for (const p of allVertices) { + while (lower.length >= 2 && cross(lower[lower.length - 2], lower[lower.length - 1], p) <= 0) { + lower.pop(); + } + lower.push(p); + } + + const upper: Vector[] = []; + for (let i = allVertices.length - 1; i >= 0; i--) { + const p = allVertices[i]; + while (upper.length >= 2 && cross(upper[upper.length - 2], upper[upper.length - 1], p) <= 0) { + upper.pop(); + } + upper.push(p); + } + + upper.pop(); + lower.pop(); + + const hull = lower.concat(upper); + + if (hull.length < 3) { + // The merged shape is not a valid polygon, so we inactivate the other collider + // and leave this one as is. This might happen in degenerate cases. + return; + } + + // 3. Update the collider with the new shape + const newLocalVertices = hull.map(v => v.subtract(thisTransform.worldPosition)); + this._updateVerticesAfterMerge(newLocalVertices); + other.inactivate(); */ + +/** From martinez-polygon-clipping */ +export type Position = number[] +export type Polygon = Position[] +export type MultiPolygon = Position[][] +export type Geometry = Polygon | MultiPolygon export abstract class Collider extends Part { colliding: boolean = false; collidingWith: Set = new Set(); @@ -12,15 +73,23 @@ export abstract class Collider extends Part { realWorldStart: Vector; realWorldEnd: Vector; vertices: Vector[]; - - constructor({ tag }: { tag?: string }) { + active: boolean = true; + allowMerge: boolean; + randomTestingColors: [number, number, number]; + constructor({ tag, allowMerge }: { tag?: string, allowMerge?: boolean }) { super({ name: "Collider" }); this.type = "Collider"; this.base = "Collider"; this.tag = tag || ""; this.radius = 0; this.realWorldStart = new Vector(0, 0); + this.allowMerge = allowMerge !== undefined ? allowMerge : true; this.realWorldEnd = new Vector(0, 0); + this.randomTestingColors = [ + Math.random() * 255, + Math.random() * 255, + Math.random() * 255 + ]; this.vertices = []; } @@ -50,6 +119,8 @@ export abstract class Collider extends Part { abstract drawDebug(ctx: CanvasRenderingContext2D): void; + abstract _updateVerticesAfterMerge(polygons: Vector[][]): void; + onRegister(attribute: string, value: any) { super.onRegister(attribute, value); if (attribute === "layer") { @@ -57,8 +128,79 @@ export abstract class Collider extends Part { } } + + evaluateMerging() { + const layer = this.registrations["layer"] as import("../Layer").Layer; + if (!layer) return; + + const candidates = layer.spatialGrid.query(this); + const fellowColliders: Collider[] = candidates.filter((c: Collider) => c.tag == this.tag && c.id !== this.id && c.allowMerge && c.active); + + if (fellowColliders.length == 0) return; + + for (const fellow of fellowColliders) { + if (!fellow.sibling("Transform")?.initialized) continue; + + // To avoid race conditions where both colliders try to merge with each other, + // we establish a rule: the collider with the smaller ID merges the one with the larger ID. + // This creates a deterministic merge order. + if (this.id < fellow.id && this.checkCollision(fellow, true)) { + this.mergeWith(fellow); + } + } + } + + + abstract getGeometry(): Polygon | MultiPolygon; + + mergeWith(other: Collider) { + if (this.tag !== other.tag || other.tag == "" || this.tag == "") return; + + const thisTransform = this.sibling("Transform"); + if (!thisTransform) { + this.top?.warn(`Collider <${this.name}> has no Transform sibling, cannot merge.`); + return; + } + + const allPolygons: Polygon[] = []; // Polygon is Position[][] + + const g1 = this.getGeometry(); + if (this.type === 'MultiPolygonCollider') { // Using this.type to avoid instanceof and circular dependencies + allPolygons.push(...(g1 as MultiPolygon)); + } else { + allPolygons.push(g1 as Polygon); + } + + const g2 = other.getGeometry(); + if (other.type === 'MultiPolygonCollider') { // Using other.type to avoid instanceof and circular dependencies + allPolygons.push(...(g2 as MultiPolygon)); + } else { + allPolygons.push(g2 as Polygon); + } + + if (allPolygons.length === 0) return; + + const localPolygons: Vector[][] = allPolygons.map(polygon => { + return polygon.map(([x, y]) => thisTransform.worldToLocal(new Vector(x, y))); + }); + + this._updateVerticesAfterMerge(localPolygons); + other.inactivate(); + } + onStart() { + // if (this.allowMerge) { + // this.evaluateMerging(); + // } + } + inactivate() { + this.active = false; + } + activate() { + this.active = true; + } act(delta: number): void { super.act(delta); + if (!this.active) return; if (!this.registrations?.layer) { throw new Error(`Collider <${this.name}> (${this.id}) is not registered to a layer. Collisions will not be checked.`); } @@ -67,13 +209,14 @@ export abstract class Collider extends Part { if (!transform) return; this.updateCollider(transform); - this.colliding = false; this.collidingWith.clear(); - const layer = this.registrations.layer as Part; - const colliders = layer.flats.colliders as Collider[]; - for (const other of colliders) { + + const layer = this.registrations.layer as Layer; + const candidates = layer.spatialGrid.query(this); + + for (const other of candidates) { if (other === this) continue; if (this.checkCollision(other)) { this.colliding = true; @@ -83,6 +226,25 @@ export abstract class Collider extends Part { this.hoverbug = `${this.colliding ? "🟥" : "🟩"} - ${Array.from(this.collidingWith).map(o => o.name).join(", ")} objects`; + // Debugging manually + + const fill = this.active; + + const ctx = this.top instanceof Game ? this.top.context : null; + if (ctx) { + ctx.beginPath(); + ctx.strokeStyle = `rgb(${this.randomTestingColors[0]}, ${this.randomTestingColors[1]}, ${this.randomTestingColors[2]})`; + ctx.fillStyle = fill ? `rgba(${this.randomTestingColors[0]}, ${this.randomTestingColors[1]}, ${this.randomTestingColors[2]}, 0.5)` : "transparent"; + + ctx.moveTo(this.worldVertices[0].x, this.worldVertices[0].y); + for (const vertex of this.worldVertices) { + ctx.lineTo(vertex.x, vertex.y); + } + ctx.closePath(); + ctx.stroke(); + ctx.fill(); + } + if (this.top instanceof Game && this.top.devmode) { const ctx = this.top.context; if (ctx) { @@ -91,8 +253,18 @@ export abstract class Collider extends Part { } } - checkCollision(other: Collider): boolean { - if (other.tag === this.tag && this.tag !== "") return false; + checkCollision(other: Collider, ignoreTags: boolean = false): boolean { + const thisTransform = this.sibling("Transform"); + const otherTransform = other.sibling("Transform"); + + if (!thisTransform || !otherTransform) { + return false; // Cannot check collision without transforms + } + + this.updateCollider(thisTransform); + other.updateCollider(otherTransform); + + if (!ignoreTags && other.tag === this.tag && this.tag !== "") return false; if ( this.realWorldEnd.x < other.realWorldStart.x || @@ -138,11 +310,17 @@ export abstract class Collider extends Part { protected checkVerticesAgainstVertices(vertices1: Vector[], vertices2: Vector[]): boolean { const axes1 = this.getAxes(vertices1); - const axes2 = this.getAxes(vertices2); + for (const axis of axes1) { + const projection1 = this.project(vertices1, axis); + const projection2 = this.project(vertices2, axis); - const axes = axes1.concat(axes2); + if (!this.overlap(projection1, projection2)) { + return false; + } + } - for (const axis of axes) { + const axes2 = this.getAxes(vertices2); + for (const axis of axes2) { const projection1 = this.project(vertices1, axis); const projection2 = this.project(vertices2, axis); @@ -183,4 +361,21 @@ export abstract class Collider extends Part { protected overlap(proj1: { min: number, max: number }, proj2: { min: number, max: number }): boolean { return proj1.max >= proj2.min && proj2.max >= proj1.min; } -} + + protected _checkPolygonVsPolygon(vertices1: Vector[], vertices2: Vector[]): boolean { + const axes1 = this.getAxes(vertices1); + const axes2 = this.getAxes(vertices2); + + const axes = axes1.concat(axes2); + + for (const axis of axes) { + const projection1 = this.project(vertices1, axis); + const projection2 = this.project(vertices2, axis); + + if (!this.overlap(projection1, projection2)) { + return false; + } + } + return true; + } +} \ No newline at end of file diff --git a/Parts/Children/ColorRender.ts b/Parts/Children/ColorRender.ts index 18bfeae..b10a68a 100644 --- a/Parts/Children/ColorRender.ts +++ b/Parts/Children/ColorRender.ts @@ -67,9 +67,6 @@ export class ColorRender extends Renderer { if (!this.top) { throw new Error(`ColorRender <${this.parent?.name}.${this.name}> is not attached to a top-level parent. Ensure it is added to a Game instance or Scene before rendering.`); } - if (!(this.top instanceof Game)) { - throw new Error(`ColorRender <${this.parent?.name}.${this.name}> is not attached to a Game instance. Ensure it is added to a Game, Scene, or Layer with a game ancestor.`); - } const transform = this.sibling("Transform"); if (!transform) { this.top?.warn(`ColorRender <${this.parent?.name}.${this.name}> does not have a Transform sibling. Skipping rendering.`); diff --git a/Parts/Children/MultiPolygonCollider.ts b/Parts/Children/MultiPolygonCollider.ts new file mode 100644 index 0000000..459d691 --- /dev/null +++ b/Parts/Children/MultiPolygonCollider.ts @@ -0,0 +1,171 @@ +import { Vector } from "../../Math/Vector"; +import { Collider, type MultiPolygon } from "./Collider"; +import type { Transform } from "./Transform"; +import { unionPolygons } from "../../Math/PolygonUnion"; + + +export class MultiPolygonCollider extends Collider { + polygons: Vector[][]; + _worldPolygons: Vector[][] = []; + unioned: Vector[][] = []; + constructor({ polygons, tag = "" }: { polygons: Vector[][], tag?: string }) { + super({ tag, allowMerge: tag !== '' }); + this.name = "MultiPolygonCollider"; + this.polygons = polygons; + this.type = "MultiPolygonCollider"; + let maxDist = 0; + const allVertices = polygons.flat(); + for (let i = 0; i < allVertices.length; i++) { + for (let j = i + 1; j < allVertices.length; j++) { + const dist = allVertices[i].distance(allVertices[j]); + if (dist > maxDist) { + maxDist = dist; + } + } + } + this.radius = maxDist; + } + + getGeometry(): MultiPolygon { + return this._worldPolygons.map(polygon => { + return polygon.map(v => v.toArray()); + }); + } + + get worldVertices(): Vector[] { + const allVertices = this._worldPolygons.flat(); + allVertices.sort((a, b) => { + return a.x < b.x || (a.x == b.x && a.y < b.y) ? -1 : 1; + }); + + const cross = (o: Vector, a: Vector, b: Vector) => { + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); + }; + + const lower: Vector[] = []; + for (const p of allVertices) { + while (lower.length >= 2 && cross(lower[lower.length - 2], lower[lower.length - 1], p) <= 0) { + lower.pop(); + } + lower.push(p); + } + + const upper: Vector[] = []; + for (let i = allVertices.length - 1; i >= 0; i--) { + const p = allVertices[i]; + while (upper.length >= 2 && cross(upper[upper.length - 2], upper[upper.length - 1], p) <= 0) { + upper.pop(); + } + upper.push(p); + } + + upper.pop(); + lower.pop(); + + return lower.concat(upper); + } + + act(delta: number) { + super.act(delta); + } + + override _updateVerticesAfterMerge(polygons: Vector[][]): void { + // console.log("Before merging: polygons.length", polygons.length); + // const unionResult: Vector[][] = unionPolygons(polygons); + // if (unionResult.length > 0) { + // this.polygons = unionResult; + // } else { + // // Fallback: keep original polygons if union fails + // console.warn("Union failed, keeping original polygons"); + // this.polygons = polygons; + // } + this.polygons = polygons; + } + + updateCollider(transform: Transform) { + const position = transform.worldPosition; + const rotation = transform.rotation; + const scale = transform.scale; + + this._worldPolygons = this.polygons.map(polygon => { + return polygon.map(vertex => { + let scaledVertex = vertex.multiply(scale); + + if (rotation !== 0) { + const cos = Math.cos(rotation); + const sin = Math.sin(rotation); + scaledVertex = new Vector( + scaledVertex.x * cos - scaledVertex.y * sin, + scaledVertex.x * sin + scaledVertex.y * cos + ); + } + return position.add(scaledVertex); + }); + }); + + const allWorldVertices = this._worldPolygons.flat(); + const xs = allWorldVertices.map(v => v.x); + const ys = allWorldVertices.map(v => v.y); + this.realWorldStart.set(Math.min(...xs), Math.min(...ys)); + this.realWorldEnd.set(Math.max(...xs), Math.max(...ys)); + } + + narrowPhaseCheck(other: Collider): boolean { + if (other instanceof MultiPolygonCollider) { + for (const p1 of this._worldPolygons) { + for (const p2 of other._worldPolygons) { + if (this._checkPolygonVsPolygon(p1, p2)) { + return true; + } + } + } + return false; + } + + for (const polygon of this._worldPolygons) { + if (this._checkPolygonVsPolygon(polygon, other.worldVertices)) { + return true; + } + } + + return false; + } + + drawDebug(ctx: CanvasRenderingContext2D): void { + ctx.save(); + ctx.strokeStyle = this.colliding ? "rgba(255, 0, 100, 0.8)" : "rgba(0, 255, 100, 0.8)"; + ctx.lineWidth = 1; + + for (const polygon of this._worldPolygons) { + ctx.beginPath(); + ctx.moveTo(polygon[0].x, polygon[0].y); + for (let i = 1; i < polygon.length; i++) { + ctx.lineTo(polygon[i].x, polygon[i].y); + } + ctx.closePath(); + ctx.stroke(); + } + ctx.restore(); + } + + override clone(memo = new Map()): this { + if (memo.has(this)) { + return memo.get(this); + } + + const clonedMultiPolygonCollider = new MultiPolygonCollider({ + polygons: this.polygons.map(p => p.map(v => v.clone())), + tag: this.tag + }); + memo.set(this, clonedMultiPolygonCollider); + + this._cloneProperties(clonedMultiPolygonCollider, memo); + + clonedMultiPolygonCollider.colliding = false; + clonedMultiPolygonCollider.base = this.base; + clonedMultiPolygonCollider.type = this.type; + clonedMultiPolygonCollider.collidingWith = new Set(); + + return clonedMultiPolygonCollider as this; + } +} diff --git a/Parts/Children/PolygonCollider.ts b/Parts/Children/PolygonCollider.ts index fa0e6d7..08dab5d 100644 --- a/Parts/Children/PolygonCollider.ts +++ b/Parts/Children/PolygonCollider.ts @@ -3,14 +3,15 @@ import { Part } from "../Part"; import { Collider } from "./Collider"; import type { Transform } from "./Transform"; import { Game } from "../Game"; +import { MultiPolygonCollider } from "./MultiPolygonCollider"; import { BoxCollider } from "./BoxCollider"; - +import type { Polygon } from "martinez-polygon-clipping"; export class PolygonCollider extends Collider { localVertices: Vector[]; _worldVertices: Vector[] = []; - constructor({ vertices, tag }: { vertices: Vector[], tag?: string }) { - super({ tag: tag }); + constructor({ vertices, tag = "" }: { vertices: Vector[], tag?: string }) { + super({ tag, allowMerge: tag !== '' }); this.name = "PolygonCollider"; this.localVertices = vertices; this.vertices = vertices; @@ -32,10 +33,27 @@ export class PolygonCollider extends Collider { return this._worldVertices; } + getGeometry(): Polygon { + return [this.worldVertices.map(v => v.toArray())]; + } + act(delta: number) { super.act(delta); } + override _updateVerticesAfterMerge(polygons: Vector[][]): void { + const newCollider = new MultiPolygonCollider({ polygons, tag: this.tag }); + + newCollider.active = this.active; + newCollider.allowMerge = this.allowMerge; + + const parent = this.parent; + if (parent) { + parent.removeChild(this); + parent.addChild(newCollider); + } + } + updateCollider(transform: Transform) { const position = transform.worldPosition; const rotation = transform.rotation; @@ -66,26 +84,15 @@ export class PolygonCollider extends Collider { return this.checkPolygonVsBox(this, other); } else if (other instanceof PolygonCollider) { return this.checkPolygonVsPolygon(this, other); + } else if (other instanceof MultiPolygonCollider) { + return other.narrowPhaseCheck(this); } this.top?.warn("Collision checks are only supported between BoxColliders and PolygonColliders."); return false; } private checkPolygonVsPolygon(poly1: PolygonCollider, poly2: PolygonCollider): boolean { - const axes1 = this.getAxes(poly1.worldVertices); - const axes2 = this.getAxes(poly2.worldVertices); - - const axes = axes1.concat(axes2); - - for (const axis of axes) { - const projection1 = this.project(poly1.worldVertices, axis); - const projection2 = this.project(poly2.worldVertices, axis); - - if (!this.overlap(projection1, projection2)) { - return false; - } - } - return true; + return this._checkPolygonVsPolygon(poly1.worldVertices, poly2.worldVertices); } private checkPolygonVsBox(poly: PolygonCollider, box: BoxCollider): boolean { diff --git a/Parts/Children/Transform.ts b/Parts/Children/Transform.ts index d021593..91b48cb 100644 --- a/Parts/Children/Transform.ts +++ b/Parts/Children/Transform.ts @@ -1,12 +1,12 @@ -import { version } from "bun"; -import { Vector } from "../../Math/Vector"; import { Part } from "../Part"; +import { Vector } from "../../Math/Vector"; export class Transform extends Part { position: Vector; worldPosition: Vector; // Will be updated when mounted to a parent rotation: number; // radians scale: Vector; + initialized: boolean; constructor({ position, rotation, scale }: { position?: Vector, rotation?: number, scale?: Vector } = {}) { super({ name: "Transform" }); @@ -17,19 +17,12 @@ export class Transform extends Part { this.scale = scale || new Vector(1, 1); // Default scale this.debugEmoji = "📐"; // Emoji for debugging Transform this.type = "Transform"; + this.initialized = false; } onMount(parent: Part) { super.onMount(parent); - // Transform's world position is determined from it's own local position and the parent's world position. - // If we reference the parent's world position, we will get this Transform itself - // Therefore, we need to use the grandparent's world position, if it exists - const grandparentTransform = parent.sibling("Transform"); - if (grandparentTransform) { - this.worldPosition.set(this.position.add(grandparentTransform.worldPosition)); - } else { - this.worldPosition.set(this.position); // If no grandparent, worldPosition is same as local position - } + this.updateWorldPosition(); // Inherit superficial dimensions from parent if available if (parent.superficialWidth && parent.superficialHeight) { this.superficialWidth = parent.superficialWidth; @@ -53,17 +46,53 @@ export class Transform extends Part { this.rotation = rotation % (2 * Math.PI); // Normalize rotation to [0, 2π) this.updateWorldPosition(); } + worldToLocal(position: Vector): Vector { + // 1. Translate back to origin + const translated = position.subtract(this.worldPosition); + + // 2. Rotate back by the inverse of the transform's rotation + const cos = Math.cos(-this.rotation); + const sin = Math.sin(-this.rotation); + const rotated = new Vector( + translated.x * cos - translated.y * sin, + translated.x * sin + translated.y * cos + ); + + // 3. Scale back by the inverse of the transform's scale + const scaled = new Vector( + rotated.x / this.scale.x, + rotated.y / this.scale.y + ); + + return scaled; + } + preFrame(): void { + super.preFrame(); + this.updateWorldPosition(); + } updateWorldPosition() { - // Update world position based on parent's world position - const parentTransform = this.parent?.sibling("Transform"); + const parentTransform = this.parent?.parent?.child("Transform"); if (parentTransform) { - this.worldPosition.set(this.position.add(parentTransform.worldPosition)); + // 1. Scale the local position by the parent's scale + const scaledPosition = this.position.multiply(parentTransform.scale); + + // 2. Rotate the scaled position by the parent's rotation + const cos = Math.cos(parentTransform.rotation); + const sin = Math.sin(parentTransform.rotation); + const rotatedPosition = new Vector( + scaledPosition.x * cos - scaledPosition.y * sin, + scaledPosition.x * sin + scaledPosition.y * cos + ); + + // 3. Add the parent's world position + this.worldPosition.set(rotatedPosition.add(parentTransform.worldPosition)); } else { - this.worldPosition.set(this.position); // If no parent, worldPosition is same as local position + this.worldPosition.set(this.position); } + + this.initialized = true; // We have at least one tick with an actual world position instead of a default 0,0 } act(_delta: number) { - this.updateWorldPosition(); this.hoverbug = `${this.position.toString()} | ${this.worldPosition.toString()} | ${(this.rotation / Math.PI).toFixed(2)}pi | ${this.scale.toString()}`; } -} \ No newline at end of file +} diff --git a/Parts/Game.ts b/Parts/Game.ts index 8f32ee7..d5e43af 100644 --- a/Parts/Game.ts +++ b/Parts/Game.ts @@ -1,4 +1,3 @@ -import { getDebugInfo } from "../helpers"; import { Part } from "./Part"; import { Scene } from "./Scene"; import { SoundManager } from "./SoundManager"; @@ -9,10 +8,7 @@ export class Game extends Part { childrenArray: Scene[]; devmode: boolean; context: CanvasRenderingContext2D; - showtoolTips: boolean = false; hovering?: Part; - tooltipLocked?: boolean; - lastMousePosition: { x: number, y: number } = { x: 0, y: 0 }; scaleFactor: number = 1; canvasOffset: { x: number, y: number } = { x: 0, y: 0 }; messageHook?: (type: "warn" | "error" | "debug", ...args: any[]) => void; @@ -30,10 +26,9 @@ export class Game extends Part { private _animationFrameId?: number; private _lastUpdateTime: number = 0; - constructor({ name, canvas, devmode = false, width, height, disableAntiAliasing = false, showtoolTips = false, showFrameStats = "BASIC" }: { name: string, canvas: HTMLCanvasElement | string, devmode?: boolean, width: number, height: number, disableAntiAliasing?: boolean, showtoolTips?: boolean, showFrameStats?: "BASIC" | "EXTENDED" | "ADVANCED" }) { - super(); - this.name = name; - this.showtoolTips = showtoolTips; + constructor({ name, canvas, devmode = false, width, height, disableAntiAliasing = false, showtoolTips = false, showFrameStats = "BASIC" }: { name: string, canvas: HTMLCanvasElement | string, devmode?: boolean, width: number, height: number, disableAntiAliasing?: boolean, showtoolTips?: boolean, showFrameStats?: "BASIC" | "EXTENDED" | "ADVANCED" | "PERFORMANCE_HUD"}) { + super({ name }); + this.type = "Game"; this.childrenArray = []; this.showFrameStats = showFrameStats; this.canvas = typeof canvas === "string" ? document.getElementById(canvas) as HTMLCanvasElement : canvas; @@ -43,30 +38,7 @@ export class Game extends Part { this.context.imageSmoothingEnabled = !disableAntiAliasing; this.debugEmoji = "🎮"; - this.tooltipLocked = false; this.top = this; - if (this.devmode) { - let tooltip = document.getElementById("debug-tooltip"); - if (!tooltip) { - tooltip = this.createDebugTooltip(); - } - document.addEventListener("mousemove", (event) => { - const rect = this.canvas.getBoundingClientRect(); - const clientX = event.clientX - rect.left; - const clientY = event.clientY - rect.top; - this.lastMousePosition = { - x: (clientX / this.scaleFactor) - (this.canvasOffset.x / this.scaleFactor), - y: (clientY / this.scaleFactor) - (this.canvasOffset.y / this.scaleFactor) - }; - }); - document.addEventListener("click", (event) => { - if (tooltip && !this.tooltipLocked) { - this.tooltipLocked = true; - } else if (tooltip) { - this.tooltipLocked = false; - } - }); - } } clone(memo = new Map()): this { @@ -84,7 +56,6 @@ export class Game extends Part { width: this.width, height: this.height, disableAntiAliasing: !this.context.imageSmoothingEnabled, // Infer from original context - showtoolTips: this.showtoolTips }); memo.set(this, clonedGame); @@ -97,8 +68,6 @@ export class Game extends Part { clonedGame.currentScene = undefined; // Will be set by start() or setScene() // clonedGame.childrenArray is handled by _cloneProperties clonedGame.hovering = undefined; // Reset hovering part - clonedGame.tooltipLocked = undefined; // Reset tooltip lock - clonedGame.lastMousePosition = { x: 0, y: 0 }; // Reset mouse position clonedGame.scaleFactor = 1; // Reset scale factor clonedGame.canvasOffset = { x: 0, y: 0 }; // Reset canvas offset clonedGame.messageHook = undefined; // Clear message hook @@ -135,20 +104,6 @@ export class Game extends Part { return this._height; } - createDebugTooltip() { - const tooltip = document.createElement("div"); - tooltip.id = "debug-tooltip"; - tooltip.style.position = "absolute"; - tooltip.style.backgroundColor = "rgba(0, 0, 0, 0.7)"; - tooltip.style.color = "white"; - tooltip.style.padding = "5px"; - tooltip.style.display = 'none'; - tooltip.style.borderRadius = "5px"; - tooltip.style.pointerEvents = "none"; - tooltip.style.zIndex = "1000"; - document.body.appendChild(tooltip); - return tooltip; - } addChild(scene: Scene) { this.currentScene = this.currentScene || scene; @@ -230,21 +185,26 @@ export class Game extends Part { } } - getColliderCount(): number { + getColliderCount(activeOnly: boolean = false): number { const layers = this.currentScene?.childrenArray || []; let c = 0; for (const layer of layers) { - const colliders = layer.flats.colliders.length; - c += colliders; + if (!activeOnly) { + const colliders = layer.flats.colliders.length; + c += colliders; + } else { + const colliders = layer.flats.colliders.filter(col => col.active).length; + c += colliders; + } } return c; - + } private renderFrameStats() { if (!this.showFrameStats) return; - + const FADE_BACKGROUND = 0.5; const avgDelta = this.frameBuffer.reduce((a, b) => a + b, 0) / this.frameBuffer.length; const avgFPS = 1000 / avgDelta; const sorted = [...this.frameBuffer].sort((a, b) => a - b); @@ -252,57 +212,103 @@ export class Game extends Part { const p99 = sorted[Math.floor(sorted.length * 0.99)]; const minFrameTime = sorted[0]; const maxFrameTime = sorted[sorted.length - 1]; - this.context.fillStyle = "white"; - this.context.font = "12px Arial"; - let y = 20; + // Prepare stats lines + let lines: string[] = []; const levels = ["BASIC", "EXTENDED", "ADVANCED", "PERFORMANCE_HUD"]; const levelIndex = levels.indexOf(this.showFrameStats); - if (levelIndex >= 0) { - this.context.fillText(`FPS: ${avgFPS.toFixed(2)}`, 10, y); y += 20; - } - if (levelIndex >= 1) { - this.context.fillText(`Frame Time: ${avgDelta.toFixed(2)} ms`, 10, y); y += 20; - } + if (levelIndex >= 0) lines.push(`FPS: ${avgFPS.toFixed(2)}`); + if (levelIndex >= 1) lines.push(`Frame Time: ${avgDelta.toFixed(2)} ms`); if (levelIndex >= 2) { - this.context.fillText(`Min: ${minFrameTime.toFixed(2)} (${this._minFrameTime.toFixed(2)} AT) ms`, 10, y); y += 20; - this.context.fillText(`Max: ${maxFrameTime.toFixed(2)} (${this._maxFrameTime.toFixed(2)} AT) ms`, 10, y); y += 20; + lines.push(`Min: ${minFrameTime.toFixed(2)} (${this._minFrameTime.toFixed(2)} AT) ms`); + lines.push(`Max: ${maxFrameTime.toFixed(2)} (${this._maxFrameTime.toFixed(2)} AT) ms`); } if (levelIndex >= 3) { - // --- PERFORMANCE HUD --- - this.context.fillText(`p95 Frame: ${p95.toFixed(2)} ms`, 10, y); y += 20; - this.context.fillText(`p99 Frame: ${p99.toFixed(2)} ms`, 10, y); y += 20; + lines.push(`p95 Frame: ${p95.toFixed(2)} ms`); + lines.push(`p99 Frame: ${p99.toFixed(2)} ms`); const droppedPct = (this._droppedFrames / (this.frameBuffer.length || 1)) * 100; - this.context.fillText(`Dropped Frames: ${droppedPct.toFixed(1)}%`, 10, y); y += 20; - - // Memory usage (if available) + lines.push(`Dropped Frames: ${droppedPct.toFixed(1)}%`); const perfMem = (performance as any).memory; if (perfMem) { const usedMB = (perfMem.usedJSHeapSize / 1048576).toFixed(1); const totalMB = (perfMem.totalJSHeapSize / 1048576).toFixed(1); - this.context.fillText(`Heap: ${usedMB} MB / ${totalMB} MB`, 10, y); y += 20; + lines.push(`Heap: ${usedMB} MB / ${totalMB} MB`); } - - // Scene stats if (this.currentScene) { - this.context.fillText(`Colliders: ${this.getColliderCount()}`, 10, y); y += 20; - // (Optionally count draw calls if you track them) + lines.push(`Colliders: ${this.getColliderCount()}`); + lines.push(`Active colliders: ${this.getColliderCount(true)}`); } + } - // Small frame time chart + // Calculate box size + const fontSize = 12; + const lineHeight = 20; + const padding = 8; + this.context.font = `${fontSize}px Arial`; + let maxWidth = 0; + for (const line of lines) { + const width = this.context.measureText(line).width; + if (width > maxWidth) maxWidth = width; + } + let boxHeight = lines.length * lineHeight + padding * 2; + let boxWidth = maxWidth + padding * 2; + let boxX = 6; + let boxY = 6; + + // Draw background box + this.context.globalAlpha = FADE_BACKGROUND; + this.context.fillStyle = "#000"; + this.context.fillRect(boxX, boxY, boxWidth, boxHeight); + this.context.globalAlpha = 1.0; + + // Draw text + this.context.fillStyle = "white"; + let y = boxY + padding + fontSize; + for (const line of lines) { + this.context.fillText(line, boxX + padding, y); + y += lineHeight; + } + + + // Draw chart if PERFORMANCE_HUD + if (levelIndex >= 3) { const chartWidth = 200; const chartHeight = 80; - const chartX = 10; - const chartY = y + 10; - const maxFrameTime = Math.max(...this.frameBuffer); + const chartX = boxX + padding; + const chartY = boxY + boxHeight + 10; + + // Find min and max for scaling, but zoom out a bit to reduce minor oscillations + const minFrameTimeChart = Math.min(...this.frameBuffer); + const maxFrameTimeChart = Math.max(...this.frameBuffer); + + // Add a margin to "zoom out" the chart + const margin = Math.max(2, (maxFrameTimeChart - minFrameTimeChart) * 0.2); + const chartMin = Math.max(0, minFrameTimeChart - margin); + const chartMax = maxFrameTimeChart + margin; + + // Add a small epsilon to avoid division by zero + const range = Math.max(1, chartMax - chartMin); + + // Draw background + this.context.globalAlpha = FADE_BACKGROUND; + this.context.fillStyle = "#000"; + this.context.fillRect(chartX - padding, chartY - padding, chartWidth + padding * 2, chartHeight + padding * 2); + this.context.globalAlpha = 1; + + + // Draw frame time line this.context.strokeStyle = "white"; this.context.beginPath(); - this.context.moveTo(chartX, chartY + chartHeight); this.frameBuffer.forEach((frameTime, index) => { - const x = chartX + (index / this.maxFrameBufferLength) * chartWidth; - const yVal = chartY + chartHeight - (frameTime / maxFrameTime) * chartHeight; - this.context.lineTo(x, yVal); + const x = chartX + (index / (this.maxFrameBufferLength - 1)) * chartWidth; + // Scale so that chartMin is at bottom, chartMax is at top + const yVal = chartY + chartHeight - ((frameTime - chartMin) / range) * chartHeight; + if (index === 0) { + this.context.moveTo(x, yVal); + } else { + this.context.lineTo(x, yVal); + } }); this.context.stroke(); } @@ -403,25 +409,4 @@ export class Game extends Part { return false; } } - updateDebugToolTip() { - const tooltip = document.getElementById("debug-tooltip"); - if (!tooltip) { - this.warn("Debug tooltip not found. Ensure it is created in devmode."); - return; - } - if (this.hovering) { - if (tooltip && this.showtoolTips) { - try { - tooltip.style.left = `${(this.lastMousePosition!.x * this.scaleFactor) + this.canvasOffset.x + 10}px`; - tooltip.style.top = `${(this.lastMousePosition!.y * this.scaleFactor) + this.canvasOffset.y + 10}px`; - tooltip.style.display = "block"; - tooltip.innerHTML = getDebugInfo(this.hovering, 0); - } catch (err) { - throw new Error(`Error updating debug tooltip: ${err}`); - } - } - } else { - tooltip.style.display = "none"; - } - } } \ No newline at end of file diff --git a/Parts/GameObject.ts b/Parts/GameObject.ts index ecb3e91..2db94e3 100644 --- a/Parts/GameObject.ts +++ b/Parts/GameObject.ts @@ -3,9 +3,9 @@ import { Part } from "./Part"; export class GameObject extends Part { layer?: Layer; - constructor({ name, render }: { name: string, render?: boolean }) { - super({ name, render }); - this.name = name; + constructor({ name, render = true }: { name: string, render?: boolean }) { + super({ name, render: !!render }); + this.type = "GameObject"; this.debugEmoji = "🕹️"; // Default emoji for debugging the game object } } diff --git a/Parts/GravityCharacterMovement.ts b/Parts/GravityCharacterMovement.ts index b7d66aa..eb6015e 100644 --- a/Parts/GravityCharacterMovement.ts +++ b/Parts/GravityCharacterMovement.ts @@ -14,20 +14,26 @@ export class GravityCharacterMovement extends Part { velocity: Vector; jumpForce: number; facing: Vector; + waterFraction: number; + landFraction: number; constructor({ speed = 5, movementType = 'WASD', input, gravityScale, maxSpeed, - jumpForce + jumpForce, + waterFraction, + landFraction }: { speed?: number, movementType?: 'WASD' | 'ArrowKeys' | 'BOTH', input?: Input, gravityScale: Vector, maxSpeed: number, - jumpForce: number + jumpForce: number, + waterFraction?: number, + landFraction?: number; }) { super({ name: 'GravityCharacterMovement' }); this.speed = speed; @@ -39,6 +45,8 @@ export class GravityCharacterMovement extends Part { this.velocity = new Vector(0, 0); this.jumpForce = jumpForce; this.facing = new Vector(1, 1); + this.waterFraction = waterFraction || 0.5; + this.landFraction = landFraction || 0.9; } private getStandingGround(): Collider | null { @@ -92,9 +100,9 @@ export class GravityCharacterMovement extends Part { const onGround = !!groundCollider; const inWater = this.isInWater(); - const speedMultiplier = inWater ? 0.5 : 1.0; - const gravityMultiplier = inWater ? 0.5 : 1.0; - const jumpForceMultiplier = inWater ? 0.8 : 1.0; + const speedMultiplier = inWater ? this.waterFraction : (onGround ? this.landFraction : 1.0); + const gravityMultiplier = inWater ? this.gravityScale.y : 1.0; + const jumpForceMultiplier = inWater ? this.jumpForce * this.waterFraction : this.jumpForce; // --- Horizontal Movement --- let dx = 0; diff --git a/Parts/Layer.ts b/Parts/Layer.ts index 9c09644..07f7267 100644 --- a/Parts/Layer.ts +++ b/Parts/Layer.ts @@ -1,37 +1,54 @@ import { GameObject } from "./GameObject"; import { generateUID } from "../helpers"; import { Part } from "./Part"; -import type { Game } from "./Game"; +import { SpatialGrid } from "../Math/SpatialGrid"; +import type { Collider } from "./Children/Collider"; export class Layer extends Part { - constructor({ name }: { name: string }) { - super(); - this.name = name; + spatialGrid: SpatialGrid; + + constructor({ name, spatialGridDefinition }: { name: string, spatialGridDefinition?: number }) { + super({ name }); + this.type = "Layer"; this.id = generateUID(); this.debugEmoji = "🗂️"; // Default emoji for debugging the layer + this.spatialGrid = new SpatialGrid(spatialGridDefinition || 100); } addChild(part: Part) { part.setAll("layer", this); super.addChild(part); } + addChildren(...parts: Part[]) { parts.forEach((part) => this.addChild(part)); } + removeChild(part: Part) { part.onUnregister("layer", this); super.removeChild(part); } + act(delta: number) { if (!this.ready) { return; } + + this.spatialGrid.clear(); + const colliders = this.flats.colliders as Collider[]; + for (const collider of colliders) { + if (collider.active) { + this.spatialGrid.insert(collider); + } + } + this.ties.forEach(tie => { if (tie.target && tie.target.hasOwnProperty(tie.targetAttribute)) { const value = this.attr(tie.localAttribute); tie.target.attr(tie.targetAttribute, value); } }); + this.childrenArray.forEach(child => { child.act(delta); }); diff --git a/Parts/PhysicsEngine.ts b/Parts/PhysicsEngine.ts index ab57eaf..36df1da 100644 --- a/Parts/PhysicsEngine.ts +++ b/Parts/PhysicsEngine.ts @@ -5,14 +5,19 @@ import { Vector } from "../Math/Vector"; export class PhysicsEngine extends Part { engine: Engine; world: World; + gravity: { x: number, y: number }; + scale: number; - constructor({ gravity }: { gravity?: { x: number, y: number, scale: number } }) { + constructor({ gravity, scale }: { gravity?: Vector, scale: number }) { super({ name: 'PhysicsEngine' }); + this.gravity = gravity?.toObject() || new Vector(0, 1).toObject(); + this.scale = scale || 0.001; + this.engine = Engine.create({ - gravity: gravity || { - x: 0, - y: 1, // Default gravity pointing downwards - scale: 0.001 // Scale for the gravity vector + gravity: { + x: this.gravity.x, + y: this.gravity.y, + scale: this.scale } }); this.world = this.engine.world; @@ -26,7 +31,8 @@ export class PhysicsEngine extends Part { } const clonedEngine = new PhysicsEngine({ - gravity: this.engine.gravity // Pass original gravity settings to constructor + gravity: new Vector(this.gravity.x, this.gravity.y), + scale: this.scale }); memo.set(this, clonedEngine); diff --git a/Parts/image.png b/Parts/image.png new file mode 100644 index 0000000..43ccc37 Binary files /dev/null and b/Parts/image.png differ diff --git a/debug/desired.png b/debug/desired.png new file mode 100644 index 0000000..9bf3196 Binary files /dev/null and b/debug/desired.png differ diff --git a/debug/diagonal.png b/debug/diagonal.png new file mode 100644 index 0000000..de72fd0 Binary files /dev/null and b/debug/diagonal.png differ diff --git a/debug/extrusion.png b/debug/extrusion.png new file mode 100644 index 0000000..d203526 Binary files /dev/null and b/debug/extrusion.png differ diff --git a/debug/failedmerge.png b/debug/failedmerge.png new file mode 100644 index 0000000..8793332 Binary files /dev/null and b/debug/failedmerge.png differ diff --git a/debug/firstpass.png b/debug/firstpass.png new file mode 100644 index 0000000..ec97e9c Binary files /dev/null and b/debug/firstpass.png differ diff --git a/debug/gpc.c b/debug/gpc.c new file mode 100644 index 0000000..90544bc --- /dev/null +++ b/debug/gpc.c @@ -0,0 +1,2510 @@ +/* +=========================================================================== + +Project: Generic Polygon Clipper + + A new algorithm for calculating the difference, intersection, + exclusive-or or union of arbitrary polygon sets. + +File: gpc.c +Author: Alan Murta (email: gpc@cs.man.ac.uk) +Version: 2.32 +Date: 17th December 2004 + +Copyright: (C) Advanced Interfaces Group, + University of Manchester. + + This software is free for non-commercial use. It may be copied, + modified, and redistributed provided that this copyright notice + is preserved on all copies. The intellectual property rights of + the algorithms used reside with the University of Manchester + Advanced Interfaces Group. + + You may not use this software, in whole or in part, in support + of any commercial product without the express consent of the + author. + + There is no warranty or other guarantee of fitness of this + software for any purpose. It is provided solely "as is". + +=========================================================================== +*/ + + +/* +=========================================================================== + Includes +=========================================================================== +*/ + +#include "gpc.h" +#include +#include +#include + + +/* +=========================================================================== + Constants +=========================================================================== +*/ + +#ifndef TRUE +#define FALSE 0 +#define TRUE 1 +#endif + +#define LEFT 0 +#define RIGHT 1 + +#define ABOVE 0 +#define BELOW 1 + +#define CLIP 0 +#define SUBJ 1 + +#define INVERT_TRISTRIPS FALSE + + +/* +=========================================================================== + Macros +=========================================================================== +*/ + +#define EQ(a, b) (fabs((a) - (b)) <= GPC_EPSILON) + +#define PREV_INDEX(i, n) ((i - 1 + n) % n) +#define NEXT_INDEX(i, n) ((i + 1 ) % n) + +#define OPTIMAL(v, i, n) ((v[PREV_INDEX(i, n)].y != v[i].y) || \ + (v[NEXT_INDEX(i, n)].y != v[i].y)) + +#define FWD_MIN(v, i, n) ((v[PREV_INDEX(i, n)].vertex.y >= v[i].vertex.y) \ + && (v[NEXT_INDEX(i, n)].vertex.y > v[i].vertex.y)) + +#define NOT_FMAX(v, i, n) (v[NEXT_INDEX(i, n)].vertex.y > v[i].vertex.y) + +#define REV_MIN(v, i, n) ((v[PREV_INDEX(i, n)].vertex.y > v[i].vertex.y) \ + && (v[NEXT_INDEX(i, n)].vertex.y >= v[i].vertex.y)) + +#define NOT_RMAX(v, i, n) (v[PREV_INDEX(i, n)].vertex.y > v[i].vertex.y) + +#define VERTEX(e,p,s,x,y) {add_vertex(&((e)->outp[(p)]->v[(s)]), x, y); \ + (e)->outp[(p)]->active++;} + +#define P_EDGE(d,e,p,i,j) {(d)= (e); \ + do {(d)= (d)->prev;} while (!(d)->outp[(p)]); \ + (i)= (d)->bot.x + (d)->dx * ((j)-(d)->bot.y);} + +#define N_EDGE(d,e,p,i,j) {(d)= (e); \ + do {(d)= (d)->next;} while (!(d)->outp[(p)]); \ + (i)= (d)->bot.x + (d)->dx * ((j)-(d)->bot.y);} + +#define MALLOC(p, b, s, t) {if ((b) > 0) { \ + p= (t*)malloc(b); if (!(p)) { \ + fprintf(stderr, "gpc malloc failure: %s\n", s); \ + exit(0);}} else p= NULL;} + +#define FREE(p) {if (p) {free(p); (p)= NULL;}} + + +/* +=========================================================================== + Private Data Types +=========================================================================== +*/ + +typedef enum /* Edge intersection classes */ +{ + NUL, /* Empty non-intersection */ + EMX, /* External maximum */ + ELI, /* External left intermediate */ + TED, /* Top edge */ + ERI, /* External right intermediate */ + RED, /* Right edge */ + IMM, /* Internal maximum and minimum */ + IMN, /* Internal minimum */ + EMN, /* External minimum */ + EMM, /* External maximum and minimum */ + LED, /* Left edge */ + ILI, /* Internal left intermediate */ + BED, /* Bottom edge */ + IRI, /* Internal right intermediate */ + IMX, /* Internal maximum */ + FUL /* Full non-intersection */ +} vertex_type; + +typedef enum /* Horizontal edge states */ +{ + NH, /* No horizontal edge */ + BH, /* Bottom horizontal edge */ + TH /* Top horizontal edge */ +} h_state; + +typedef enum /* Edge bundle state */ +{ + UNBUNDLED, /* Isolated edge not within a bundle */ + BUNDLE_HEAD, /* Bundle head node */ + BUNDLE_TAIL /* Passive bundle tail node */ +} bundle_state; + +typedef struct v_shape /* Internal vertex list datatype */ +{ + double x; /* X coordinate component */ + double y; /* Y coordinate component */ + struct v_shape *next; /* Pointer to next vertex in list */ +} vertex_node; + +typedef struct p_shape /* Internal contour / tristrip type */ +{ + int active; /* Active flag / vertex count */ + int hole; /* Hole / external contour flag */ + vertex_node *v[2]; /* Left and right vertex list ptrs */ + struct p_shape *next; /* Pointer to next polygon contour */ + struct p_shape *proxy; /* Pointer to actual structure used */ +} polygon_node; + +typedef struct edge_shape +{ + gpc_vertex vertex; /* Piggy-backed contour vertex data */ + gpc_vertex bot; /* Edge lower (x, y) coordinate */ + gpc_vertex top; /* Edge upper (x, y) coordinate */ + double xb; /* Scanbeam bottom x coordinate */ + double xt; /* Scanbeam top x coordinate */ + double dx; /* Change in x for a unit y increase */ + int type; /* Clip / subject edge flag */ + int bundle[2][2]; /* Bundle edge flags */ + int bside[2]; /* Bundle left / right indicators */ + bundle_state bstate[2]; /* Edge bundle state */ + polygon_node *outp[2]; /* Output polygon / tristrip pointer */ + struct edge_shape *prev; /* Previous edge in the AET */ + struct edge_shape *next; /* Next edge in the AET */ + struct edge_shape *pred; /* Edge connected at the lower end */ + struct edge_shape *succ; /* Edge connected at the upper end */ + struct edge_shape *next_bound; /* Pointer to next bound in LMT */ +} edge_node; + +typedef struct lmt_shape /* Local minima table */ +{ + double y; /* Y coordinate at local minimum */ + edge_node *first_bound; /* Pointer to bound list */ + struct lmt_shape *next; /* Pointer to next local minimum */ +} lmt_node; + +typedef struct sbt_t_shape /* Scanbeam tree */ +{ + double y; /* Scanbeam node y value */ + struct sbt_t_shape *less; /* Pointer to nodes with lower y */ + struct sbt_t_shape *more; /* Pointer to nodes with higher y */ +} sb_tree; + +typedef struct it_shape /* Intersection table */ +{ + edge_node *ie[2]; /* Intersecting edge (bundle) pair */ + gpc_vertex point; /* Point of intersection */ + struct it_shape *next; /* The next intersection table node */ +} it_node; + +typedef struct st_shape /* Sorted edge table */ +{ + edge_node *edge; /* Pointer to AET edge */ + double xb; /* Scanbeam bottom x coordinate */ + double xt; /* Scanbeam top x coordinate */ + double dx; /* Change in x for a unit y increase */ + struct st_shape *prev; /* Previous edge in sorted list */ +} st_node; + +typedef struct bbox_shape /* Contour axis-aligned bounding box */ +{ + double xmin; /* Minimum x coordinate */ + double ymin; /* Minimum y coordinate */ + double xmax; /* Maximum x coordinate */ + double ymax; /* Maximum y coordinate */ +} bbox; + + +/* +=========================================================================== + Global Data +=========================================================================== +*/ + +/* Horizontal edge state transitions within scanbeam boundary */ +const h_state next_h_state[3][6]= +{ + /* ABOVE BELOW CROSS */ + /* L R L R L R */ + /* NH */ {BH, TH, TH, BH, NH, NH}, + /* BH */ {NH, NH, NH, NH, TH, TH}, + /* TH */ {NH, NH, NH, NH, BH, BH} +}; + + +/* +=========================================================================== + Private Functions +=========================================================================== +*/ + +static void reset_it(it_node **it) +{ + it_node *itn; + + while (*it) + { + itn= (*it)->next; + FREE(*it); + *it= itn; + } +} + + +static void reset_lmt(lmt_node **lmt) +{ + lmt_node *lmtn; + + while (*lmt) + { + lmtn= (*lmt)->next; + FREE(*lmt); + *lmt= lmtn; + } +} + + +static void insert_bound(edge_node **b, edge_node *e) +{ + edge_node *existing_bound; + + if (!*b) + { + /* Link node e to the tail of the list */ + *b= e; + } + else + { + /* Do primary sort on the x field */ + if (e[0].bot.x < (*b)[0].bot.x) + { + /* Insert a new node mid-list */ + existing_bound= *b; + *b= e; + (*b)->next_bound= existing_bound; + } + else + { + if (e[0].bot.x == (*b)[0].bot.x) + { + /* Do secondary sort on the dx field */ + if (e[0].dx < (*b)[0].dx) + { + /* Insert a new node mid-list */ + existing_bound= *b; + *b= e; + (*b)->next_bound= existing_bound; + } + else + { + /* Head further down the list */ + insert_bound(&((*b)->next_bound), e); + } + } + else + { + /* Head further down the list */ + insert_bound(&((*b)->next_bound), e); + } + } + } +} + + +static edge_node **bound_list(lmt_node **lmt, double y) +{ + lmt_node *existing_node; + + if (!*lmt) + { + /* Add node onto the tail end of the LMT */ + MALLOC(*lmt, sizeof(lmt_node), "LMT insertion", lmt_node); + (*lmt)->y= y; + (*lmt)->first_bound= NULL; + (*lmt)->next= NULL; + return &((*lmt)->first_bound); + } + else + if (y < (*lmt)->y) + { + /* Insert a new LMT node before the current node */ + existing_node= *lmt; + MALLOC(*lmt, sizeof(lmt_node), "LMT insertion", lmt_node); + (*lmt)->y= y; + (*lmt)->first_bound= NULL; + (*lmt)->next= existing_node; + return &((*lmt)->first_bound); + } + else + if (y > (*lmt)->y) + /* Head further up the LMT */ + return bound_list(&((*lmt)->next), y); + else + /* Use this existing LMT node */ + return &((*lmt)->first_bound); +} + + +static void add_to_sbtree(int *entries, sb_tree **sbtree, double y) +{ + if (!*sbtree) + { + /* Add a new tree node here */ + MALLOC(*sbtree, sizeof(sb_tree), "scanbeam tree insertion", sb_tree); + (*sbtree)->y= y; + (*sbtree)->less= NULL; + (*sbtree)->more= NULL; + (*entries)++; + } + else + { + if ((*sbtree)->y > y) + { + /* Head into the 'less' sub-tree */ + add_to_sbtree(entries, &((*sbtree)->less), y); + } + else + { + if ((*sbtree)->y < y) + { + /* Head into the 'more' sub-tree */ + add_to_sbtree(entries, &((*sbtree)->more), y); + } + } + } +} + + +static void build_sbt(int *entries, double *sbt, sb_tree *sbtree) +{ + if (sbtree->less) + build_sbt(entries, sbt, sbtree->less); + sbt[*entries]= sbtree->y; + (*entries)++; + if (sbtree->more) + build_sbt(entries, sbt, sbtree->more); +} + + +static void free_sbtree(sb_tree **sbtree) +{ + if (*sbtree) + { + free_sbtree(&((*sbtree)->less)); + free_sbtree(&((*sbtree)->more)); + FREE(*sbtree); + } +} + + +static int count_optimal_vertices(gpc_vertex_list c) +{ + int result= 0, i; + + /* Ignore non-contributing contours */ + if (c.num_vertices > 0) + { + for (i= 0; i < c.num_vertices; i++) + /* Ignore superfluous vertices embedded in horizontal edges */ + if (OPTIMAL(c.vertex, i, c.num_vertices)) + result++; + } + return result; +} + + +static edge_node *build_lmt(lmt_node **lmt, sb_tree **sbtree, + int *sbt_entries, gpc_polygon *p, int type, + gpc_op op) +{ + int c, i, min, max, num_edges, v, num_vertices; + int total_vertices= 0, e_index=0; + edge_node *e, *edge_table; + + for (c= 0; c < p->num_contours; c++) + total_vertices+= count_optimal_vertices(p->contour[c]); + + /* Create the entire input polygon edge table in one go */ + MALLOC(edge_table, total_vertices * sizeof(edge_node), + "edge table creation", edge_node); + + for (c= 0; c < p->num_contours; c++) + { + if (p->contour[c].num_vertices < 0) + { + /* Ignore the non-contributing contour and repair the vertex count */ + p->contour[c].num_vertices= -p->contour[c].num_vertices; + } + else + { + /* Perform contour optimisation */ + num_vertices= 0; + for (i= 0; i < p->contour[c].num_vertices; i++) + if (OPTIMAL(p->contour[c].vertex, i, p->contour[c].num_vertices)) + { + edge_table[num_vertices].vertex.x= p->contour[c].vertex[i].x; + edge_table[num_vertices].vertex.y= p->contour[c].vertex[i].y; + + /* Record vertex in the scanbeam table */ + add_to_sbtree(sbt_entries, sbtree, + edge_table[num_vertices].vertex.y); + + num_vertices++; + } + + /* Do the contour forward pass */ + for (min= 0; min < num_vertices; min++) + { + /* If a forward local minimum... */ + if (FWD_MIN(edge_table, min, num_vertices)) + { + /* Search for the next local maximum... */ + num_edges= 1; + max= NEXT_INDEX(min, num_vertices); + while (NOT_FMAX(edge_table, max, num_vertices)) + { + num_edges++; + max= NEXT_INDEX(max, num_vertices); + } + + /* Build the next edge list */ + e= &edge_table[e_index]; + e_index+= num_edges; + v= min; + e[0].bstate[BELOW]= UNBUNDLED; + e[0].bundle[BELOW][CLIP]= FALSE; + e[0].bundle[BELOW][SUBJ]= FALSE; + for (i= 0; i < num_edges; i++) + { + e[i].xb= edge_table[v].vertex.x; + e[i].bot.x= edge_table[v].vertex.x; + e[i].bot.y= edge_table[v].vertex.y; + + v= NEXT_INDEX(v, num_vertices); + + e[i].top.x= edge_table[v].vertex.x; + e[i].top.y= edge_table[v].vertex.y; + e[i].dx= (edge_table[v].vertex.x - e[i].bot.x) / + (e[i].top.y - e[i].bot.y); + e[i].type= type; + e[i].outp[ABOVE]= NULL; + e[i].outp[BELOW]= NULL; + e[i].next= NULL; + e[i].prev= NULL; + e[i].succ= ((num_edges > 1) && (i < (num_edges - 1))) ? + &(e[i + 1]) : NULL; + e[i].pred= ((num_edges > 1) && (i > 0)) ? &(e[i - 1]) : NULL; + e[i].next_bound= NULL; + e[i].bside[CLIP]= (op == GPC_DIFF) ? RIGHT : LEFT; + e[i].bside[SUBJ]= LEFT; + } + insert_bound(bound_list(lmt, edge_table[min].vertex.y), e); + } + } + + /* Do the contour reverse pass */ + for (min= 0; min < num_vertices; min++) + { + /* If a reverse local minimum... */ + if (REV_MIN(edge_table, min, num_vertices)) + { + /* Search for the previous local maximum... */ + num_edges= 1; + max= PREV_INDEX(min, num_vertices); + while (NOT_RMAX(edge_table, max, num_vertices)) + { + num_edges++; + max= PREV_INDEX(max, num_vertices); + } + + /* Build the previous edge list */ + e= &edge_table[e_index]; + e_index+= num_edges; + v= min; + e[0].bstate[BELOW]= UNBUNDLED; + e[0].bundle[BELOW][CLIP]= FALSE; + e[0].bundle[BELOW][SUBJ]= FALSE; + for (i= 0; i < num_edges; i++) + { + e[i].xb= edge_table[v].vertex.x; + e[i].bot.x= edge_table[v].vertex.x; + e[i].bot.y= edge_table[v].vertex.y; + + v= PREV_INDEX(v, num_vertices); + + e[i].top.x= edge_table[v].vertex.x; + e[i].top.y= edge_table[v].vertex.y; + e[i].dx= (edge_table[v].vertex.x - e[i].bot.x) / + (e[i].top.y - e[i].bot.y); + e[i].type= type; + e[i].outp[ABOVE]= NULL; + e[i].outp[BELOW]= NULL; + e[i].next= NULL; + e[i].prev= NULL; + e[i].succ= ((num_edges > 1) && (i < (num_edges - 1))) ? + &(e[i + 1]) : NULL; + e[i].pred= ((num_edges > 1) && (i > 0)) ? &(e[i - 1]) : NULL; + e[i].next_bound= NULL; + e[i].bside[CLIP]= (op == GPC_DIFF) ? RIGHT : LEFT; + e[i].bside[SUBJ]= LEFT; + } + insert_bound(bound_list(lmt, edge_table[min].vertex.y), e); + } + } + } + } + return edge_table; +} + + +static void add_edge_to_aet(edge_node **aet, edge_node *edge, edge_node *prev) +{ + if (!*aet) + { + /* Append edge onto the tail end of the AET */ + *aet= edge; + edge->prev= prev; + edge->next= NULL; + } + else + { + /* Do primary sort on the xb field */ + if (edge->xb < (*aet)->xb) + { + /* Insert edge here (before the AET edge) */ + edge->prev= prev; + edge->next= *aet; + (*aet)->prev= edge; + *aet= edge; + } + else + { + if (edge->xb == (*aet)->xb) + { + /* Do secondary sort on the dx field */ + if (edge->dx < (*aet)->dx) + { + /* Insert edge here (before the AET edge) */ + edge->prev= prev; + edge->next= *aet; + (*aet)->prev= edge; + *aet= edge; + } + else + { + /* Head further into the AET */ + add_edge_to_aet(&((*aet)->next), edge, *aet); + } + } + else + { + /* Head further into the AET */ + add_edge_to_aet(&((*aet)->next), edge, *aet); + } + } + } +} + + +static void add_intersection(it_node **it, edge_node *edge0, edge_node *edge1, + double x, double y) +{ + it_node *existing_node; + + if (!*it) + { + /* Append a new node to the tail of the list */ + MALLOC(*it, sizeof(it_node), "IT insertion", it_node); + (*it)->ie[0]= edge0; + (*it)->ie[1]= edge1; + (*it)->point.x= x; + (*it)->point.y= y; + (*it)->next= NULL; + } + else + { + if ((*it)->point.y > y) + { + /* Insert a new node mid-list */ + existing_node= *it; + MALLOC(*it, sizeof(it_node), "IT insertion", it_node); + (*it)->ie[0]= edge0; + (*it)->ie[1]= edge1; + (*it)->point.x= x; + (*it)->point.y= y; + (*it)->next= existing_node; + } + else + /* Head further down the list */ + add_intersection(&((*it)->next), edge0, edge1, x, y); + } +} + + +static void add_st_edge(st_node **st, it_node **it, edge_node *edge, + double dy) +{ + st_node *existing_node; + double den, r, x, y; + + if (!*st) + { + /* Append edge onto the tail end of the ST */ + MALLOC(*st, sizeof(st_node), "ST insertion", st_node); + (*st)->edge= edge; + (*st)->xb= edge->xb; + (*st)->xt= edge->xt; + (*st)->dx= edge->dx; + (*st)->prev= NULL; + } + else + { + den= ((*st)->xt - (*st)->xb) - (edge->xt - edge->xb); + + /* If new edge and ST edge don't cross */ + if ((edge->xt >= (*st)->xt) || (edge->dx == (*st)->dx) || + (fabs(den) <= DBL_EPSILON)) + { + /* No intersection - insert edge here (before the ST edge) */ + existing_node= *st; + MALLOC(*st, sizeof(st_node), "ST insertion", st_node); + (*st)->edge= edge; + (*st)->xb= edge->xb; + (*st)->xt= edge->xt; + (*st)->dx= edge->dx; + (*st)->prev= existing_node; + } + else + { + /* Compute intersection between new edge and ST edge */ + r= (edge->xb - (*st)->xb) / den; + x= (*st)->xb + r * ((*st)->xt - (*st)->xb); + y= r * dy; + + /* Insert the edge pointers and the intersection point in the IT */ + add_intersection(it, (*st)->edge, edge, x, y); + + /* Head further into the ST */ + add_st_edge(&((*st)->prev), it, edge, dy); + } + } +} + + +static void build_intersection_table(it_node **it, edge_node *aet, double dy) +{ + st_node *st, *stp; + edge_node *edge; + + /* Build intersection table for the current scanbeam */ + reset_it(it); + st= NULL; + + /* Process each AET edge */ + for (edge= aet; edge; edge= edge->next) + { + if ((edge->bstate[ABOVE] == BUNDLE_HEAD) || + edge->bundle[ABOVE][CLIP] || edge->bundle[ABOVE][SUBJ]) + add_st_edge(&st, it, edge, dy); + } + + /* Free the sorted edge table */ + while (st) + { + stp= st->prev; + FREE(st); + st= stp; + } +} + + +static void swap_intersecting_edge_bundles(edge_node **aet, it_node *intersect) +{ + edge_node *e0 = intersect->ie[0]; + edge_node *e1 = intersect->ie[1]; + edge_node *e0t = e0; + edge_node *e1t = e1; + edge_node *e0n = e0->next; + edge_node *e1n = e1->next; + + // Find the node before the e0 bundle + edge_node *e0p = e0->prev; + if (e0->bstate[ABOVE] == BUNDLE_HEAD) + { + do + { + e0t = e0p; + e0p = e0p->prev; + } + while (e0p && (e0p->bstate[ABOVE] == BUNDLE_TAIL)); + } + + // Find the node before the e1 bundle + edge_node *e1p = e1->prev; + if (e1->bstate[ABOVE] == BUNDLE_HEAD) + { + do + { + e1t = e1p; + e1p = e1p->prev; + } + while (e1p && (e1p->bstate[ABOVE] == BUNDLE_TAIL)); + } + + // Swap the e0p and e1p links + if (e0p) + { + if (e1p) + { + if (e0p != e1) + { + e0p->next = e1t; + e1t->prev = e0p; + } + if (e1p != e0) + { + e1p->next = e0t; + e0t->prev = e1p; + } + } + else + { + if (e0p != e1) + { + e0p->next = e1t; + e1t->prev = e0p; + } + *aet = e0t; + e0t->prev = NULL; + } + } + else + { + if (e1p != e0) + { + e1p->next = e0t; + e0t->prev = e1p; + } + *aet = e1t; + e1t->prev = NULL; + } + + // Re-link after e0 + if (e0p != e1) + { + e0->next = e1n; + if (e1n) + { + e1n->prev = e0; + } + } + else + { + e0->next = e1t; + e1t->prev = e0; + } + + // Re-link after e1 + if (e1p != e0) + { + e1->next = e0n; + if (e0n) + { + e0n->prev = e1; + } + } + else + { + e1->next = e0t; + e0t->prev = e1; + } +} + + +static int count_contours(polygon_node *polygon) +{ + int nc, nv; + vertex_node *v, *nextv; + + for (nc= 0; polygon; polygon= polygon->next) + if (polygon->active) + { + /* Count the vertices in the current contour */ + nv= 0; + for (v= polygon->proxy->v[LEFT]; v; v= v->next) + nv++; + + /* Record valid vertex counts in the active field */ + if (nv > 2) + { + polygon->active= nv; + nc++; + } + else + { + /* Invalid contour: just free the heap */ + for (v= polygon->proxy->v[LEFT]; v; v= nextv) + { + nextv= v->next; + FREE(v); + } + polygon->active= 0; + } + } + return nc; +} + + +static void add_left(polygon_node *p, double x, double y) +{ + vertex_node *nv; + + /* Create a new vertex node and set its fields */ + MALLOC(nv, sizeof(vertex_node), "vertex node creation", vertex_node); + nv->x= x; + nv->y= y; + + /* Add vertex nv to the left end of the polygon's vertex list */ + nv->next= p->proxy->v[LEFT]; + + /* Update proxy->[LEFT] to point to nv */ + p->proxy->v[LEFT]= nv; +} + + +static void merge_left(polygon_node *p, polygon_node *q, polygon_node *list) +{ + polygon_node *target; + + /* Label contour as a hole */ + q->proxy->hole= TRUE; + + if (p->proxy != q->proxy) + { + /* Assign p's vertex list to the left end of q's list */ + p->proxy->v[RIGHT]->next= q->proxy->v[LEFT]; + q->proxy->v[LEFT]= p->proxy->v[LEFT]; + + /* Redirect any p->proxy references to q->proxy */ + + for (target= p->proxy; list; list= list->next) + { + if (list->proxy == target) + { + list->active= FALSE; + list->proxy= q->proxy; + } + } + } +} + + +static void add_right(polygon_node *p, double x, double y) +{ + vertex_node *nv; + + /* Create a new vertex node and set its fields */ + MALLOC(nv, sizeof(vertex_node), "vertex node creation", vertex_node); + nv->x= x; + nv->y= y; + nv->next= NULL; + + /* Add vertex nv to the right end of the polygon's vertex list */ + p->proxy->v[RIGHT]->next= nv; + + /* Update proxy->v[RIGHT] to point to nv */ + p->proxy->v[RIGHT]= nv; +} + + +static void merge_right(polygon_node *p, polygon_node *q, polygon_node *list) +{ + polygon_node *target; + + /* Label contour as external */ + q->proxy->hole= FALSE; + + if (p->proxy != q->proxy) + { + /* Assign p's vertex list to the right end of q's list */ + q->proxy->v[RIGHT]->next= p->proxy->v[LEFT]; + q->proxy->v[RIGHT]= p->proxy->v[RIGHT]; + + /* Redirect any p->proxy references to q->proxy */ + for (target= p->proxy; list; list= list->next) + { + if (list->proxy == target) + { + list->active= FALSE; + list->proxy= q->proxy; + } + } + } +} + + +static void add_local_min(polygon_node **p, edge_node *edge, + double x, double y) +{ + polygon_node *existing_min; + vertex_node *nv; + + existing_min= *p; + + MALLOC(*p, sizeof(polygon_node), "polygon node creation", polygon_node); + + /* Create a new vertex node and set its fields */ + MALLOC(nv, sizeof(vertex_node), "vertex node creation", vertex_node); + nv->x= x; + nv->y= y; + nv->next= NULL; + + /* Initialise proxy to point to p itself */ + (*p)->proxy= (*p); + (*p)->active= TRUE; + (*p)->next= existing_min; + + /* Make v[LEFT] and v[RIGHT] point to new vertex nv */ + (*p)->v[LEFT]= nv; + (*p)->v[RIGHT]= nv; + + /* Assign polygon p to the edge */ + edge->outp[ABOVE]= *p; +} + + +static int count_tristrips(polygon_node *tn) +{ + int total; + + for (total= 0; tn; tn= tn->next) + if (tn->active > 2) + total++; + return total; +} + + +static void add_vertex(vertex_node **t, double x, double y) +{ + if (!(*t)) + { + MALLOC(*t, sizeof(vertex_node), "tristrip vertex creation", vertex_node); + (*t)->x= x; + (*t)->y= y; + (*t)->next= NULL; + } + else + /* Head further down the list */ + add_vertex(&((*t)->next), x, y); +} + + +static void new_tristrip(polygon_node **tn, edge_node *edge, + double x, double y) +{ + if (!(*tn)) + { + MALLOC(*tn, sizeof(polygon_node), "tristrip node creation", polygon_node); + (*tn)->next= NULL; + (*tn)->v[LEFT]= NULL; + (*tn)->v[RIGHT]= NULL; + (*tn)->active= 1; + add_vertex(&((*tn)->v[LEFT]), x, y); + edge->outp[ABOVE]= *tn; + } + else + /* Head further down the list */ + new_tristrip(&((*tn)->next), edge, x, y); +} + + +static bbox *create_contour_bboxes(gpc_polygon *p) +{ + bbox *box; + int c, v; + + MALLOC(box, p->num_contours * sizeof(bbox), "Bounding box creation", bbox); + + /* Construct contour bounding boxes */ + for (c= 0; c < p->num_contours; c++) + { + /* Initialise bounding box extent */ + box[c].xmin= DBL_MAX; + box[c].ymin= DBL_MAX; + box[c].xmax= -DBL_MAX; + box[c].ymax= -DBL_MAX; + + for (v= 0; v < p->contour[c].num_vertices; v++) + { + /* Adjust bounding box */ + if (p->contour[c].vertex[v].x < box[c].xmin) + box[c].xmin= p->contour[c].vertex[v].x; + if (p->contour[c].vertex[v].y < box[c].ymin) + box[c].ymin= p->contour[c].vertex[v].y; + if (p->contour[c].vertex[v].x > box[c].xmax) + box[c].xmax= p->contour[c].vertex[v].x; + if (p->contour[c].vertex[v].y > box[c].ymax) + box[c].ymax= p->contour[c].vertex[v].y; + } + } + return box; +} + + +static void minimax_test(gpc_polygon *subj, gpc_polygon *clip, gpc_op op) +{ + bbox *s_bbox, *c_bbox; + int s, c, *o_table, overlap; + + s_bbox= create_contour_bboxes(subj); + c_bbox= create_contour_bboxes(clip); + + MALLOC(o_table, subj->num_contours * clip->num_contours * sizeof(int), + "overlap table creation", int); + + /* Check all subject contour bounding boxes against clip boxes */ + for (s= 0; s < subj->num_contours; s++) + for (c= 0; c < clip->num_contours; c++) + o_table[c * subj->num_contours + s]= + (!((s_bbox[s].xmax < c_bbox[c].xmin) || + (s_bbox[s].xmin > c_bbox[c].xmax))) && + (!((s_bbox[s].ymax < c_bbox[c].ymin) || + (s_bbox[s].ymin > c_bbox[c].ymax))); + + /* For each clip contour, search for any subject contour overlaps */ + for (c= 0; c < clip->num_contours; c++) + { + overlap= 0; + for (s= 0; (!overlap) && (s < subj->num_contours); s++) + overlap= o_table[c * subj->num_contours + s]; + + if (!overlap) + /* Flag non contributing status by negating vertex count */ + clip->contour[c].num_vertices = -clip->contour[c].num_vertices; + } + + if (op == GPC_INT) + { + /* For each subject contour, search for any clip contour overlaps */ + for (s= 0; s < subj->num_contours; s++) + { + overlap= 0; + for (c= 0; (!overlap) && (c < clip->num_contours); c++) + overlap= o_table[c * subj->num_contours + s]; + + if (!overlap) + /* Flag non contributing status by negating vertex count */ + subj->contour[s].num_vertices = -subj->contour[s].num_vertices; + } + } + + FREE(s_bbox); + FREE(c_bbox); + FREE(o_table); +} + + +/* +=========================================================================== + Public Functions +=========================================================================== +*/ + +void gpc_free_polygon(gpc_polygon *p) +{ + int c; + + for (c= 0; c < p->num_contours; c++) + FREE(p->contour[c].vertex); + FREE(p->hole); + FREE(p->contour); + p->num_contours= 0; +} + + +void gpc_read_polygon(FILE *fp, int read_hole_flags, gpc_polygon *p) +{ + int c, v; + + fscanf_s(fp, "%d", &(p->num_contours)); + MALLOC(p->hole, p->num_contours * sizeof(int), + "hole flag array creation", int); + MALLOC(p->contour, p->num_contours + * sizeof(gpc_vertex_list), "contour creation", gpc_vertex_list); + for (c= 0; c < p->num_contours; c++) + { + fscanf_s(fp, "%d", &(p->contour[c].num_vertices)); + + if (read_hole_flags) + fscanf_s(fp, "%d", &(p->hole[c])); + else + p->hole[c]= FALSE; /* Assume all contours to be external */ + + MALLOC(p->contour[c].vertex, p->contour[c].num_vertices + * sizeof(gpc_vertex), "vertex creation", gpc_vertex); + for (v= 0; v < p->contour[c].num_vertices; v++) + fscanf_s(fp, "%lf %lf", &(p->contour[c].vertex[v].x), + &(p->contour[c].vertex[v].y)); + } +} + + +void gpc_write_polygon(FILE *fp, int write_hole_flags, gpc_polygon *p) +{ + int c, v; + + fprintf(fp, "%d\n", p->num_contours); + for (c= 0; c < p->num_contours; c++) + { + fprintf(fp, "%d\n", p->contour[c].num_vertices); + + if (write_hole_flags) + fprintf(fp, "%d\n", p->hole[c]); + + for (v= 0; v < p->contour[c].num_vertices; v++) + fprintf(fp, "% .*lf % .*lf\n", + DBL_DIG, p->contour[c].vertex[v].x, + DBL_DIG, p->contour[c].vertex[v].y); + } +} + + +void gpc_add_contour(gpc_polygon *p, gpc_vertex_list *new_contour, int hole) +{ + int *extended_hole, c, v; + gpc_vertex_list *extended_contour; + + /* Create an extended hole array */ + MALLOC(extended_hole, (p->num_contours + 1) + * sizeof(int), "contour hole addition", int); + + /* Create an extended contour array */ + MALLOC(extended_contour, (p->num_contours + 1) + * sizeof(gpc_vertex_list), "contour addition", gpc_vertex_list); + + /* Copy the old contour and hole data into the extended arrays */ + for (c= 0; c < p->num_contours; c++) + { + extended_hole[c]= p->hole[c]; + extended_contour[c]= p->contour[c]; + } + + /* Copy the new contour and hole onto the end of the extended arrays */ + c= p->num_contours; + extended_hole[c]= hole; + extended_contour[c].num_vertices= new_contour->num_vertices; + MALLOC(extended_contour[c].vertex, new_contour->num_vertices + * sizeof(gpc_vertex), "contour addition", gpc_vertex); + for (v= 0; v < new_contour->num_vertices; v++) + extended_contour[c].vertex[v]= new_contour->vertex[v]; + + /* Dispose of the old contour */ + FREE(p->contour); + FREE(p->hole); + + /* Update the polygon information */ + p->num_contours++; + p->hole= extended_hole; + p->contour= extended_contour; +} + + +void gpc_polygon_clip(gpc_op op, gpc_polygon *subj, gpc_polygon *clip, + gpc_polygon *result) +{ + sb_tree *sbtree= NULL; + it_node *it= NULL, *intersect; + edge_node *edge, *prev_edge, *next_edge, *succ_edge, *e0, *e1; + edge_node *aet= NULL, *c_heap= NULL, *s_heap= NULL; + lmt_node *lmt= NULL, *local_min; + polygon_node *out_poly= NULL, *p, *q, *poly, *npoly, *cf= NULL; + vertex_node *vtx, *nv; + h_state horiz[2]; + int in[2], exists[2], parity[2]= {LEFT, LEFT}; + int c, v, contributing, scanbeam= 0, sbt_entries= 0; + int vclass, bl, br, tl, tr; + double *sbt= NULL, xb, px, yb, yt, dy, ix, iy; + + /* Test for trivial NULL result cases */ + if (((subj->num_contours == 0) && (clip->num_contours == 0)) + || ((subj->num_contours == 0) && ((op == GPC_INT) || (op == GPC_DIFF))) + || ((clip->num_contours == 0) && (op == GPC_INT))) + { + result->num_contours= 0; + result->hole= NULL; + result->contour= NULL; + return; + } + + /* Identify potentialy contributing contours */ + if (((op == GPC_INT) || (op == GPC_DIFF)) + && (subj->num_contours > 0) && (clip->num_contours > 0)) + minimax_test(subj, clip, op); + + /* Build LMT */ + if (subj->num_contours > 0) + s_heap= build_lmt(&lmt, &sbtree, &sbt_entries, subj, SUBJ, op); + if (clip->num_contours > 0) + c_heap= build_lmt(&lmt, &sbtree, &sbt_entries, clip, CLIP, op); + + /* Return a NULL result if no contours contribute */ + if (lmt == NULL) + { + result->num_contours= 0; + result->hole= NULL; + result->contour= NULL; + reset_lmt(&lmt); + FREE(s_heap); + FREE(c_heap); + return; + } + + /* Build scanbeam table from scanbeam tree */ + MALLOC(sbt, sbt_entries * sizeof(double), "sbt creation", double); + build_sbt(&scanbeam, sbt, sbtree); + scanbeam= 0; + free_sbtree(&sbtree); + + /* Allow pointer re-use without causing memory leak */ + if (subj == result) + gpc_free_polygon(subj); + if (clip == result) + gpc_free_polygon(clip); + + /* Invert clip polygon for difference operation */ + if (op == GPC_DIFF) + parity[CLIP]= RIGHT; + + local_min= lmt; + + /* Process each scanbeam */ + while (scanbeam < sbt_entries) + { + /* Set yb and yt to the bottom and top of the scanbeam */ + yb= sbt[scanbeam++]; + if (scanbeam < sbt_entries) + { + yt= sbt[scanbeam]; + dy= yt - yb; + } + + /* === SCANBEAM BOUNDARY PROCESSING ================================ */ + + /* If LMT node corresponding to yb exists */ + if (local_min) + { + if (local_min->y == yb) + { + /* Add edges starting at this local minimum to the AET */ + for (edge= local_min->first_bound; edge; edge= edge->next_bound) + add_edge_to_aet(&aet, edge, NULL); + + local_min= local_min->next; + } + } + + /* Set dummy previous x value */ + px= -DBL_MAX; + + /* Create bundles within AET */ + e0= aet; + e1= aet; + + /* Set up bundle fields of first edge */ + aet->bundle[ABOVE][ aet->type]= (aet->top.y != yb); + aet->bundle[ABOVE][!aet->type]= FALSE; + aet->bstate[ABOVE]= UNBUNDLED; + + for (next_edge= aet->next; next_edge; next_edge= next_edge->next) + { + /* Set up bundle fields of next edge */ + next_edge->bundle[ABOVE][ next_edge->type]= (next_edge->top.y != yb); + next_edge->bundle[ABOVE][!next_edge->type]= FALSE; + next_edge->bstate[ABOVE]= UNBUNDLED; + + /* Bundle edges above the scanbeam boundary if they coincide */ + if (next_edge->bundle[ABOVE][next_edge->type]) + { + if (EQ(e0->xb, next_edge->xb) && EQ(e0->dx, next_edge->dx) + && (e0->top.y != yb)) + { + next_edge->bundle[ABOVE][ next_edge->type]^= + e0->bundle[ABOVE][ next_edge->type]; + next_edge->bundle[ABOVE][!next_edge->type]= + e0->bundle[ABOVE][!next_edge->type]; + next_edge->bstate[ABOVE]= BUNDLE_HEAD; + e0->bundle[ABOVE][CLIP]= FALSE; + e0->bundle[ABOVE][SUBJ]= FALSE; + e0->bstate[ABOVE]= BUNDLE_TAIL; + } + e0= next_edge; + } + } + + horiz[CLIP]= NH; + horiz[SUBJ]= NH; + + /* Process each edge at this scanbeam boundary */ + for (edge= aet; edge; edge= edge->next) + { + exists[CLIP]= edge->bundle[ABOVE][CLIP] + + (edge->bundle[BELOW][CLIP] << 1); + exists[SUBJ]= edge->bundle[ABOVE][SUBJ] + + (edge->bundle[BELOW][SUBJ] << 1); + + if (exists[CLIP] || exists[SUBJ]) + { + /* Set bundle side */ + edge->bside[CLIP]= parity[CLIP]; + edge->bside[SUBJ]= parity[SUBJ]; + + /* Determine contributing status and quadrant occupancies */ + switch (op) + { + case GPC_DIFF: + case GPC_INT: + contributing= (exists[CLIP] && (parity[SUBJ] || horiz[SUBJ])) + || (exists[SUBJ] && (parity[CLIP] || horiz[CLIP])) + || (exists[CLIP] && exists[SUBJ] + && (parity[CLIP] == parity[SUBJ])); + br= (parity[CLIP]) + && (parity[SUBJ]); + bl= (parity[CLIP] ^ edge->bundle[ABOVE][CLIP]) + && (parity[SUBJ] ^ edge->bundle[ABOVE][SUBJ]); + tr= (parity[CLIP] ^ (horiz[CLIP]!=NH)) + && (parity[SUBJ] ^ (horiz[SUBJ]!=NH)); + tl= (parity[CLIP] ^ (horiz[CLIP]!=NH) ^ edge->bundle[BELOW][CLIP]) + && (parity[SUBJ] ^ (horiz[SUBJ]!=NH) ^ edge->bundle[BELOW][SUBJ]); + break; + case GPC_XOR: + contributing= exists[CLIP] || exists[SUBJ]; + br= (parity[CLIP]) + ^ (parity[SUBJ]); + bl= (parity[CLIP] ^ edge->bundle[ABOVE][CLIP]) + ^ (parity[SUBJ] ^ edge->bundle[ABOVE][SUBJ]); + tr= (parity[CLIP] ^ (horiz[CLIP]!=NH)) + ^ (parity[SUBJ] ^ (horiz[SUBJ]!=NH)); + tl= (parity[CLIP] ^ (horiz[CLIP]!=NH) ^ edge->bundle[BELOW][CLIP]) + ^ (parity[SUBJ] ^ (horiz[SUBJ]!=NH) ^ edge->bundle[BELOW][SUBJ]); + break; + case GPC_UNION: + contributing= (exists[CLIP] && (!parity[SUBJ] || horiz[SUBJ])) + || (exists[SUBJ] && (!parity[CLIP] || horiz[CLIP])) + || (exists[CLIP] && exists[SUBJ] + && (parity[CLIP] == parity[SUBJ])); + br= (parity[CLIP]) + || (parity[SUBJ]); + bl= (parity[CLIP] ^ edge->bundle[ABOVE][CLIP]) + || (parity[SUBJ] ^ edge->bundle[ABOVE][SUBJ]); + tr= (parity[CLIP] ^ (horiz[CLIP]!=NH)) + || (parity[SUBJ] ^ (horiz[SUBJ]!=NH)); + tl= (parity[CLIP] ^ (horiz[CLIP]!=NH) ^ edge->bundle[BELOW][CLIP]) + || (parity[SUBJ] ^ (horiz[SUBJ]!=NH) ^ edge->bundle[BELOW][SUBJ]); + break; + } + + /* Update parity */ + parity[CLIP]^= edge->bundle[ABOVE][CLIP]; + parity[SUBJ]^= edge->bundle[ABOVE][SUBJ]; + + /* Update horizontal state */ + if (exists[CLIP]) + horiz[CLIP]= + next_h_state[horiz[CLIP]] + [((exists[CLIP] - 1) << 1) + parity[CLIP]]; + if (exists[SUBJ]) + horiz[SUBJ]= + next_h_state[horiz[SUBJ]] + [((exists[SUBJ] - 1) << 1) + parity[SUBJ]]; + + vclass= tr + (tl << 1) + (br << 2) + (bl << 3); + + if (contributing) + { + xb= edge->xb; + + switch (vclass) + { + case EMN: + case IMN: + add_local_min(&out_poly, edge, xb, yb); + px= xb; + cf= edge->outp[ABOVE]; + break; + case ERI: + if (xb != px) + { + add_right(cf, xb, yb); + px= xb; + } + edge->outp[ABOVE]= cf; + cf= NULL; + break; + case ELI: + add_left(edge->outp[BELOW], xb, yb); + px= xb; + cf= edge->outp[BELOW]; + break; + case EMX: + if (xb != px) + { + add_left(cf, xb, yb); + px= xb; + } + merge_right(cf, edge->outp[BELOW], out_poly); + cf= NULL; + break; + case ILI: + if (xb != px) + { + add_left(cf, xb, yb); + px= xb; + } + edge->outp[ABOVE]= cf; + cf= NULL; + break; + case IRI: + add_right(edge->outp[BELOW], xb, yb); + px= xb; + cf= edge->outp[BELOW]; + edge->outp[BELOW]= NULL; + break; + case IMX: + if (xb != px) + { + add_right(cf, xb, yb); + px= xb; + } + merge_left(cf, edge->outp[BELOW], out_poly); + cf= NULL; + edge->outp[BELOW]= NULL; + break; + case IMM: + if (xb != px) + { + add_right(cf, xb, yb); + px= xb; + } + merge_left(cf, edge->outp[BELOW], out_poly); + edge->outp[BELOW]= NULL; + add_local_min(&out_poly, edge, xb, yb); + cf= edge->outp[ABOVE]; + break; + case EMM: + if (xb != px) + { + add_left(cf, xb, yb); + px= xb; + } + merge_right(cf, edge->outp[BELOW], out_poly); + edge->outp[BELOW]= NULL; + add_local_min(&out_poly, edge, xb, yb); + cf= edge->outp[ABOVE]; + break; + case LED: + if (edge->bot.y == yb) + add_left(edge->outp[BELOW], xb, yb); + edge->outp[ABOVE]= edge->outp[BELOW]; + px= xb; + break; + case RED: + if (edge->bot.y == yb) + add_right(edge->outp[BELOW], xb, yb); + edge->outp[ABOVE]= edge->outp[BELOW]; + px= xb; + break; + default: + break; + } /* End of switch */ + } /* End of contributing conditional */ + } /* End of edge exists conditional */ + } /* End of AET loop */ + + /* Delete terminating edges from the AET, otherwise compute xt */ + for (edge= aet; edge; edge= edge->next) + { + if (edge->top.y == yb) + { + prev_edge= edge->prev; + next_edge= edge->next; + if (prev_edge) + prev_edge->next= next_edge; + else + aet= next_edge; + if (next_edge) + next_edge->prev= prev_edge; + + /* Copy bundle head state to the adjacent tail edge if required */ + if ((edge->bstate[BELOW] == BUNDLE_HEAD) && prev_edge) + { + if (prev_edge->bstate[BELOW] == BUNDLE_TAIL) + { + prev_edge->outp[BELOW]= edge->outp[BELOW]; + prev_edge->bstate[BELOW]= UNBUNDLED; + if (prev_edge->prev) + if (prev_edge->prev->bstate[BELOW] == BUNDLE_TAIL) + prev_edge->bstate[BELOW]= BUNDLE_HEAD; + } + } + } + else + { + if (edge->top.y == yt) + edge->xt= edge->top.x; + else + edge->xt= edge->bot.x + edge->dx * (yt - edge->bot.y); + } + } + + if (scanbeam < sbt_entries) + { + /* === SCANBEAM INTERIOR PROCESSING ============================== */ + + build_intersection_table(&it, aet, dy); + + /* Process each node in the intersection table */ + for (intersect= it; intersect; intersect= intersect->next) + { + e0= intersect->ie[0]; + e1= intersect->ie[1]; + + /* Only generate output for contributing intersections */ + if ((e0->bundle[ABOVE][CLIP] || e0->bundle[ABOVE][SUBJ]) + && (e1->bundle[ABOVE][CLIP] || e1->bundle[ABOVE][SUBJ])) + { + p= e0->outp[ABOVE]; + q= e1->outp[ABOVE]; + ix= intersect->point.x; + iy= intersect->point.y + yb; + + in[CLIP]= ( e0->bundle[ABOVE][CLIP] && !e0->bside[CLIP]) + || ( e1->bundle[ABOVE][CLIP] && e1->bside[CLIP]) + || (!e0->bundle[ABOVE][CLIP] && !e1->bundle[ABOVE][CLIP] + && e0->bside[CLIP] && e1->bside[CLIP]); + in[SUBJ]= ( e0->bundle[ABOVE][SUBJ] && !e0->bside[SUBJ]) + || ( e1->bundle[ABOVE][SUBJ] && e1->bside[SUBJ]) + || (!e0->bundle[ABOVE][SUBJ] && !e1->bundle[ABOVE][SUBJ] + && e0->bside[SUBJ] && e1->bside[SUBJ]); + + /* Determine quadrant occupancies */ + switch (op) + { + case GPC_DIFF: + case GPC_INT: + tr= (in[CLIP]) + && (in[SUBJ]); + tl= (in[CLIP] ^ e1->bundle[ABOVE][CLIP]) + && (in[SUBJ] ^ e1->bundle[ABOVE][SUBJ]); + br= (in[CLIP] ^ e0->bundle[ABOVE][CLIP]) + && (in[SUBJ] ^ e0->bundle[ABOVE][SUBJ]); + bl= (in[CLIP] ^ e1->bundle[ABOVE][CLIP] ^ e0->bundle[ABOVE][CLIP]) + && (in[SUBJ] ^ e1->bundle[ABOVE][SUBJ] ^ e0->bundle[ABOVE][SUBJ]); + break; + case GPC_XOR: + tr= (in[CLIP]) + ^ (in[SUBJ]); + tl= (in[CLIP] ^ e1->bundle[ABOVE][CLIP]) + ^ (in[SUBJ] ^ e1->bundle[ABOVE][SUBJ]); + br= (in[CLIP] ^ e0->bundle[ABOVE][CLIP]) + ^ (in[SUBJ] ^ e0->bundle[ABOVE][SUBJ]); + bl= (in[CLIP] ^ e1->bundle[ABOVE][CLIP] ^ e0->bundle[ABOVE][CLIP]) + ^ (in[SUBJ] ^ e1->bundle[ABOVE][SUBJ] ^ e0->bundle[ABOVE][SUBJ]); + break; + case GPC_UNION: + tr= (in[CLIP]) + || (in[SUBJ]); + tl= (in[CLIP] ^ e1->bundle[ABOVE][CLIP]) + || (in[SUBJ] ^ e1->bundle[ABOVE][SUBJ]); + br= (in[CLIP] ^ e0->bundle[ABOVE][CLIP]) + || (in[SUBJ] ^ e0->bundle[ABOVE][SUBJ]); + bl= (in[CLIP] ^ e1->bundle[ABOVE][CLIP] ^ e0->bundle[ABOVE][CLIP]) + || (in[SUBJ] ^ e1->bundle[ABOVE][SUBJ] ^ e0->bundle[ABOVE][SUBJ]); + break; + } + + vclass= tr + (tl << 1) + (br << 2) + (bl << 3); + + switch (vclass) + { + case EMN: + add_local_min(&out_poly, e0, ix, iy); + e1->outp[ABOVE]= e0->outp[ABOVE]; + break; + case ERI: + if (p) + { + add_right(p, ix, iy); + e1->outp[ABOVE]= p; + e0->outp[ABOVE]= NULL; + } + break; + case ELI: + if (q) + { + add_left(q, ix, iy); + e0->outp[ABOVE]= q; + e1->outp[ABOVE]= NULL; + } + break; + case EMX: + if (p && q) + { + add_left(p, ix, iy); + merge_right(p, q, out_poly); + e0->outp[ABOVE]= NULL; + e1->outp[ABOVE]= NULL; + } + break; + case IMN: + add_local_min(&out_poly, e0, ix, iy); + e1->outp[ABOVE]= e0->outp[ABOVE]; + break; + case ILI: + if (p) + { + add_left(p, ix, iy); + e1->outp[ABOVE]= p; + e0->outp[ABOVE]= NULL; + } + break; + case IRI: + if (q) + { + add_right(q, ix, iy); + e0->outp[ABOVE]= q; + e1->outp[ABOVE]= NULL; + } + break; + case IMX: + if (p && q) + { + add_right(p, ix, iy); + merge_left(p, q, out_poly); + e0->outp[ABOVE]= NULL; + e1->outp[ABOVE]= NULL; + } + break; + case IMM: + if (p && q) + { + add_right(p, ix, iy); + merge_left(p, q, out_poly); + add_local_min(&out_poly, e0, ix, iy); + e1->outp[ABOVE]= e0->outp[ABOVE]; + } + break; + case EMM: + if (p && q) + { + add_left(p, ix, iy); + merge_right(p, q, out_poly); + add_local_min(&out_poly, e0, ix, iy); + e1->outp[ABOVE]= e0->outp[ABOVE]; + } + break; + default: + break; + } /* End of switch */ + } /* End of contributing intersection conditional */ + + /* Swap bundle sides in response to edge crossing */ + if (e0->bundle[ABOVE][CLIP]) + e1->bside[CLIP]= !e1->bside[CLIP]; + if (e1->bundle[ABOVE][CLIP]) + e0->bside[CLIP]= !e0->bside[CLIP]; + if (e0->bundle[ABOVE][SUBJ]) + e1->bside[SUBJ]= !e1->bside[SUBJ]; + if (e1->bundle[ABOVE][SUBJ]) + e0->bside[SUBJ]= !e0->bside[SUBJ]; + + /* Swap the edge bundles in the aet */ + swap_intersecting_edge_bundles(&aet, intersect); + + } /* End of IT loop*/ + + /* Prepare for next scanbeam */ + for (edge= aet; edge; edge= next_edge) + { + next_edge= edge->next; + succ_edge= edge->succ; + + if ((edge->top.y == yt) && succ_edge) + { + /* Replace AET edge by its successor */ + succ_edge->outp[BELOW]= edge->outp[ABOVE]; + succ_edge->bstate[BELOW]= edge->bstate[ABOVE]; + succ_edge->bundle[BELOW][CLIP]= edge->bundle[ABOVE][CLIP]; + succ_edge->bundle[BELOW][SUBJ]= edge->bundle[ABOVE][SUBJ]; + prev_edge= edge->prev; + if (prev_edge) + prev_edge->next= succ_edge; + else + aet= succ_edge; + if (next_edge) + next_edge->prev= succ_edge; + succ_edge->prev= prev_edge; + succ_edge->next= next_edge; + } + else + { + /* Update this edge */ + edge->outp[BELOW]= edge->outp[ABOVE]; + edge->bstate[BELOW]= edge->bstate[ABOVE]; + edge->bundle[BELOW][CLIP]= edge->bundle[ABOVE][CLIP]; + edge->bundle[BELOW][SUBJ]= edge->bundle[ABOVE][SUBJ]; + edge->xb= edge->xt; + } + edge->outp[ABOVE]= NULL; + } + } + } /* === END OF SCANBEAM PROCESSING ================================== */ + + /* Generate result polygon from out_poly */ + result->contour= NULL; + result->hole= NULL; + result->num_contours= count_contours(out_poly); + if (result->num_contours > 0) + { + MALLOC(result->hole, result->num_contours + * sizeof(int), "hole flag table creation", int); + MALLOC(result->contour, result->num_contours + * sizeof(gpc_vertex_list), "contour creation", gpc_vertex_list); + + c= 0; + for (poly= out_poly; poly; poly= npoly) + { + npoly= poly->next; + if (poly->active) + { + result->hole[c]= poly->proxy->hole; + result->contour[c].num_vertices= poly->active; + MALLOC(result->contour[c].vertex, + result->contour[c].num_vertices * sizeof(gpc_vertex), + "vertex creation", gpc_vertex); + + v= result->contour[c].num_vertices - 1; + for (vtx= poly->proxy->v[LEFT]; vtx; vtx= nv) + { + nv= vtx->next; + result->contour[c].vertex[v].x= vtx->x; + result->contour[c].vertex[v].y= vtx->y; + FREE(vtx); + v--; + } + c++; + } + FREE(poly); + } + } + else + { + for (poly= out_poly; poly; poly= npoly) + { + npoly= poly->next; + FREE(poly); + } + } + + /* Tidy up */ + reset_it(&it); + reset_lmt(&lmt); + FREE(c_heap); + FREE(s_heap); + FREE(sbt); +} + + +void gpc_free_tristrip(gpc_tristrip *t) +{ + int s; + + for (s= 0; s < t->num_strips; s++) + FREE(t->strip[s].vertex); + FREE(t->strip); + t->num_strips= 0; +} + + +void gpc_polygon_to_tristrip(gpc_polygon *s, gpc_tristrip *t) +{ + gpc_polygon c; + + c.num_contours= 0; + c.hole= NULL; + c.contour= NULL; + gpc_tristrip_clip(GPC_DIFF, s, &c, t); +} + + +void gpc_tristrip_clip(gpc_op op, gpc_polygon *subj, gpc_polygon *clip, + gpc_tristrip *result) +{ + sb_tree *sbtree= NULL; + it_node *it= NULL, *intersect; + edge_node *edge, *prev_edge, *next_edge, *succ_edge, *e0, *e1; + edge_node *aet= NULL, *c_heap= NULL, *s_heap= NULL, *cf; + lmt_node *lmt= NULL, *local_min; + polygon_node *tlist= NULL, *tn, *tnn, *p, *q; + vertex_node *lt, *ltn, *rt, *rtn; + h_state horiz[2]; + vertex_type cft; + int in[2], exists[2], parity[2]= {LEFT, LEFT}; + int s, v, contributing, scanbeam= 0, sbt_entries= 0; + int vclass, bl, br, tl, tr; + double *sbt= NULL, xb, px, nx, yb, yt, dy, ix, iy; + + /* Test for trivial NULL result cases */ + if (((subj->num_contours == 0) && (clip->num_contours == 0)) + || ((subj->num_contours == 0) && ((op == GPC_INT) || (op == GPC_DIFF))) + || ((clip->num_contours == 0) && (op == GPC_INT))) + { + result->num_strips= 0; + result->strip= NULL; + return; + } + + /* Identify potentialy contributing contours */ + if (((op == GPC_INT) || (op == GPC_DIFF)) + && (subj->num_contours > 0) && (clip->num_contours > 0)) + minimax_test(subj, clip, op); + + /* Build LMT */ + if (subj->num_contours > 0) + s_heap= build_lmt(&lmt, &sbtree, &sbt_entries, subj, SUBJ, op); + if (clip->num_contours > 0) + c_heap= build_lmt(&lmt, &sbtree, &sbt_entries, clip, CLIP, op); + + /* Return a NULL result if no contours contribute */ + if (lmt == NULL) + { + result->num_strips= 0; + result->strip= NULL; + reset_lmt(&lmt); + FREE(s_heap); + FREE(c_heap); + return; + } + + /* Build scanbeam table from scanbeam tree */ + MALLOC(sbt, sbt_entries * sizeof(double), "sbt creation", double); + build_sbt(&scanbeam, sbt, sbtree); + scanbeam= 0; + free_sbtree(&sbtree); + + /* Invert clip polygon for difference operation */ + if (op == GPC_DIFF) + parity[CLIP]= RIGHT; + + local_min= lmt; + + /* Process each scanbeam */ + while (scanbeam < sbt_entries) + { + /* Set yb and yt to the bottom and top of the scanbeam */ + yb= sbt[scanbeam++]; + if (scanbeam < sbt_entries) + { + yt= sbt[scanbeam]; + dy= yt - yb; + } + + /* === SCANBEAM BOUNDARY PROCESSING ================================ */ + + /* If LMT node corresponding to yb exists */ + if (local_min) + { + if (local_min->y == yb) + { + /* Add edges starting at this local minimum to the AET */ + for (edge= local_min->first_bound; edge; edge= edge->next_bound) + add_edge_to_aet(&aet, edge, NULL); + + local_min= local_min->next; + } + } + + /* Set dummy previous x value */ + px= -DBL_MAX; + + /* Create bundles within AET */ + e0= aet; + e1= aet; + + /* Set up bundle fields of first edge */ + aet->bundle[ABOVE][ aet->type]= (aet->top.y != yb); + aet->bundle[ABOVE][!aet->type]= FALSE; + aet->bstate[ABOVE]= UNBUNDLED; + + for (next_edge= aet->next; next_edge; next_edge= next_edge->next) + { + /* Set up bundle fields of next edge */ + next_edge->bundle[ABOVE][ next_edge->type]= (next_edge->top.y != yb); + next_edge->bundle[ABOVE][!next_edge->type]= FALSE; + next_edge->bstate[ABOVE]= UNBUNDLED; + + /* Bundle edges above the scanbeam boundary if they coincide */ + if (next_edge->bundle[ABOVE][next_edge->type]) + { + if (EQ(e0->xb, next_edge->xb) && EQ(e0->dx, next_edge->dx) + && (e0->top.y != yb)) + { + next_edge->bundle[ABOVE][ next_edge->type]^= + e0->bundle[ABOVE][ next_edge->type]; + next_edge->bundle[ABOVE][!next_edge->type]= + e0->bundle[ABOVE][!next_edge->type]; + next_edge->bstate[ABOVE]= BUNDLE_HEAD; + e0->bundle[ABOVE][CLIP]= FALSE; + e0->bundle[ABOVE][SUBJ]= FALSE; + e0->bstate[ABOVE]= BUNDLE_TAIL; + } + e0= next_edge; + } + } + + horiz[CLIP]= NH; + horiz[SUBJ]= NH; + + /* Process each edge at this scanbeam boundary */ + for (edge= aet; edge; edge= edge->next) + { + exists[CLIP]= edge->bundle[ABOVE][CLIP] + + (edge->bundle[BELOW][CLIP] << 1); + exists[SUBJ]= edge->bundle[ABOVE][SUBJ] + + (edge->bundle[BELOW][SUBJ] << 1); + + if (exists[CLIP] || exists[SUBJ]) + { + /* Set bundle side */ + edge->bside[CLIP]= parity[CLIP]; + edge->bside[SUBJ]= parity[SUBJ]; + + /* Determine contributing status and quadrant occupancies */ + switch (op) + { + case GPC_DIFF: + case GPC_INT: + contributing= (exists[CLIP] && (parity[SUBJ] || horiz[SUBJ])) + || (exists[SUBJ] && (parity[CLIP] || horiz[CLIP])) + || (exists[CLIP] && exists[SUBJ] + && (parity[CLIP] == parity[SUBJ])); + br= (parity[CLIP]) + && (parity[SUBJ]); + bl= (parity[CLIP] ^ edge->bundle[ABOVE][CLIP]) + && (parity[SUBJ] ^ edge->bundle[ABOVE][SUBJ]); + tr= (parity[CLIP] ^ (horiz[CLIP]!=NH)) + && (parity[SUBJ] ^ (horiz[SUBJ]!=NH)); + tl= (parity[CLIP] ^ (horiz[CLIP]!=NH) ^ edge->bundle[BELOW][CLIP]) + && (parity[SUBJ] ^ (horiz[SUBJ]!=NH) ^ edge->bundle[BELOW][SUBJ]); + break; + case GPC_XOR: + contributing= exists[CLIP] || exists[SUBJ]; + br= (parity[CLIP]) + ^ (parity[SUBJ]); + bl= (parity[CLIP] ^ edge->bundle[ABOVE][CLIP]) + ^ (parity[SUBJ] ^ edge->bundle[ABOVE][SUBJ]); + tr= (parity[CLIP] ^ (horiz[CLIP]!=NH)) + ^ (parity[SUBJ] ^ (horiz[SUBJ]!=NH)); + tl= (parity[CLIP] ^ (horiz[CLIP]!=NH) ^ edge->bundle[BELOW][CLIP]) + ^ (parity[SUBJ] ^ (horiz[SUBJ]!=NH) ^ edge->bundle[BELOW][SUBJ]); + break; + case GPC_UNION: + contributing= (exists[CLIP] && (!parity[SUBJ] || horiz[SUBJ])) + || (exists[SUBJ] && (!parity[CLIP] || horiz[CLIP])) + || (exists[CLIP] && exists[SUBJ] + && (parity[CLIP] == parity[SUBJ])); + br= (parity[CLIP]) + || (parity[SUBJ]); + bl= (parity[CLIP] ^ edge->bundle[ABOVE][CLIP]) + || (parity[SUBJ] ^ edge->bundle[ABOVE][SUBJ]); + tr= (parity[CLIP] ^ (horiz[CLIP]!=NH)) + || (parity[SUBJ] ^ (horiz[SUBJ]!=NH)); + tl= (parity[CLIP] ^ (horiz[CLIP]!=NH) ^ edge->bundle[BELOW][CLIP]) + || (parity[SUBJ] ^ (horiz[SUBJ]!=NH) ^ edge->bundle[BELOW][SUBJ]); + break; + } + + /* Update parity */ + parity[CLIP]^= edge->bundle[ABOVE][CLIP]; + parity[SUBJ]^= edge->bundle[ABOVE][SUBJ]; + + /* Update horizontal state */ + if (exists[CLIP]) + horiz[CLIP]= + next_h_state[horiz[CLIP]] + [((exists[CLIP] - 1) << 1) + parity[CLIP]]; + if (exists[SUBJ]) + horiz[SUBJ]= + next_h_state[horiz[SUBJ]] + [((exists[SUBJ] - 1) << 1) + parity[SUBJ]]; + + vclass= tr + (tl << 1) + (br << 2) + (bl << 3); + + if (contributing) + { + xb= edge->xb; + + switch (vclass) + { + case EMN: + new_tristrip(&tlist, edge, xb, yb); + cf= edge; + break; + case ERI: + edge->outp[ABOVE]= cf->outp[ABOVE]; + if (xb != cf->xb) + VERTEX(edge, ABOVE, RIGHT, xb, yb); + cf= NULL; + break; + case ELI: + VERTEX(edge, BELOW, LEFT, xb, yb); + edge->outp[ABOVE]= NULL; + cf= edge; + break; + case EMX: + if (xb != cf->xb) + VERTEX(edge, BELOW, RIGHT, xb, yb); + edge->outp[ABOVE]= NULL; + cf= NULL; + break; + case IMN: + if (cft == LED) + { + if (cf->bot.y != yb) + VERTEX(cf, BELOW, LEFT, cf->xb, yb); + new_tristrip(&tlist, cf, cf->xb, yb); + } + edge->outp[ABOVE]= cf->outp[ABOVE]; + VERTEX(edge, ABOVE, RIGHT, xb, yb); + break; + case ILI: + new_tristrip(&tlist, edge, xb, yb); + cf= edge; + cft= ILI; + break; + case IRI: + if (cft == LED) + { + if (cf->bot.y != yb) + VERTEX(cf, BELOW, LEFT, cf->xb, yb); + new_tristrip(&tlist, cf, cf->xb, yb); + } + VERTEX(edge, BELOW, RIGHT, xb, yb); + edge->outp[ABOVE]= NULL; + break; + case IMX: + VERTEX(edge, BELOW, LEFT, xb, yb); + edge->outp[ABOVE]= NULL; + cft= IMX; + break; + case IMM: + VERTEX(edge, BELOW, LEFT, xb, yb); + edge->outp[ABOVE]= cf->outp[ABOVE]; + if (xb != cf->xb) + VERTEX(cf, ABOVE, RIGHT, xb, yb); + cf= edge; + break; + case EMM: + VERTEX(edge, BELOW, RIGHT, xb, yb); + edge->outp[ABOVE]= NULL; + new_tristrip(&tlist, edge, xb, yb); + cf= edge; + break; + case LED: + if (edge->bot.y == yb) + VERTEX(edge, BELOW, LEFT, xb, yb); + edge->outp[ABOVE]= edge->outp[BELOW]; + cf= edge; + cft= LED; + break; + case RED: + edge->outp[ABOVE]= cf->outp[ABOVE]; + if (cft == LED) + { + if (cf->bot.y == yb) + { + VERTEX(edge, BELOW, RIGHT, xb, yb); + } + else + { + if (edge->bot.y == yb) + { + VERTEX(cf, BELOW, LEFT, cf->xb, yb); + VERTEX(edge, BELOW, RIGHT, xb, yb); + } + } + } + else + { + VERTEX(edge, BELOW, RIGHT, xb, yb); + VERTEX(edge, ABOVE, RIGHT, xb, yb); + } + cf= NULL; + break; + default: + break; + } /* End of switch */ + } /* End of contributing conditional */ + } /* End of edge exists conditional */ + } /* End of AET loop */ + + /* Delete terminating edges from the AET, otherwise compute xt */ + for (edge= aet; edge; edge= edge->next) + { + if (edge->top.y == yb) + { + prev_edge= edge->prev; + next_edge= edge->next; + if (prev_edge) + prev_edge->next= next_edge; + else + aet= next_edge; + if (next_edge) + next_edge->prev= prev_edge; + + /* Copy bundle head state to the adjacent tail edge if required */ + if ((edge->bstate[BELOW] == BUNDLE_HEAD) && prev_edge) + { + if (prev_edge->bstate[BELOW] == BUNDLE_TAIL) + { + prev_edge->outp[BELOW]= edge->outp[BELOW]; + prev_edge->bstate[BELOW]= UNBUNDLED; + if (prev_edge->prev) + if (prev_edge->prev->bstate[BELOW] == BUNDLE_TAIL) + prev_edge->bstate[BELOW]= BUNDLE_HEAD; + } + } + } + else + { + if (edge->top.y == yt) + edge->xt= edge->top.x; + else + edge->xt= edge->bot.x + edge->dx * (yt - edge->bot.y); + } + } + + if (scanbeam < sbt_entries) + { + /* === SCANBEAM INTERIOR PROCESSING ============================== */ + + build_intersection_table(&it, aet, dy); + + /* Process each node in the intersection table */ + for (intersect= it; intersect; intersect= intersect->next) + { + e0= intersect->ie[0]; + e1= intersect->ie[1]; + + /* Only generate output for contributing intersections */ + if ((e0->bundle[ABOVE][CLIP] || e0->bundle[ABOVE][SUBJ]) + && (e1->bundle[ABOVE][CLIP] || e1->bundle[ABOVE][SUBJ])) + { + p= e0->outp[ABOVE]; + q= e1->outp[ABOVE]; + ix= intersect->point.x; + iy= intersect->point.y + yb; + + in[CLIP]= ( e0->bundle[ABOVE][CLIP] && !e0->bside[CLIP]) + || ( e1->bundle[ABOVE][CLIP] && e1->bside[CLIP]) + || (!e0->bundle[ABOVE][CLIP] && !e1->bundle[ABOVE][CLIP] + && e0->bside[CLIP] && e1->bside[CLIP]); + in[SUBJ]= ( e0->bundle[ABOVE][SUBJ] && !e0->bside[SUBJ]) + || ( e1->bundle[ABOVE][SUBJ] && e1->bside[SUBJ]) + || (!e0->bundle[ABOVE][SUBJ] && !e1->bundle[ABOVE][SUBJ] + && e0->bside[SUBJ] && e1->bside[SUBJ]); + + /* Determine quadrant occupancies */ + switch (op) + { + case GPC_DIFF: + case GPC_INT: + tr= (in[CLIP]) + && (in[SUBJ]); + tl= (in[CLIP] ^ e1->bundle[ABOVE][CLIP]) + && (in[SUBJ] ^ e1->bundle[ABOVE][SUBJ]); + br= (in[CLIP] ^ e0->bundle[ABOVE][CLIP]) + && (in[SUBJ] ^ e0->bundle[ABOVE][SUBJ]); + bl= (in[CLIP] ^ e1->bundle[ABOVE][CLIP] ^ e0->bundle[ABOVE][CLIP]) + && (in[SUBJ] ^ e1->bundle[ABOVE][SUBJ] ^ e0->bundle[ABOVE][SUBJ]); + break; + case GPC_XOR: + tr= (in[CLIP]) + ^ (in[SUBJ]); + tl= (in[CLIP] ^ e1->bundle[ABOVE][CLIP]) + ^ (in[SUBJ] ^ e1->bundle[ABOVE][SUBJ]); + br= (in[CLIP] ^ e0->bundle[ABOVE][CLIP]) + ^ (in[SUBJ] ^ e0->bundle[ABOVE][SUBJ]); + bl= (in[CLIP] ^ e1->bundle[ABOVE][CLIP] ^ e0->bundle[ABOVE][CLIP]) + ^ (in[SUBJ] ^ e1->bundle[ABOVE][SUBJ] ^ e0->bundle[ABOVE][SUBJ]); + break; + case GPC_UNION: + tr= (in[CLIP]) + || (in[SUBJ]); + tl= (in[CLIP] ^ e1->bundle[ABOVE][CLIP]) + || (in[SUBJ] ^ e1->bundle[ABOVE][SUBJ]); + br= (in[CLIP] ^ e0->bundle[ABOVE][CLIP]) + || (in[SUBJ] ^ e0->bundle[ABOVE][SUBJ]); + bl= (in[CLIP] ^ e1->bundle[ABOVE][CLIP] ^ e0->bundle[ABOVE][CLIP]) + || (in[SUBJ] ^ e1->bundle[ABOVE][SUBJ] ^ e0->bundle[ABOVE][SUBJ]); + break; + } + + vclass= tr + (tl << 1) + (br << 2) + (bl << 3); + + switch (vclass) + { + case EMN: + new_tristrip(&tlist, e1, ix, iy); + e0->outp[ABOVE]= e1->outp[ABOVE]; + break; + case ERI: + if (p) + { + P_EDGE(prev_edge, e0, ABOVE, px, iy); + VERTEX(prev_edge, ABOVE, LEFT, px, iy); + VERTEX(e0, ABOVE, RIGHT, ix, iy); + e1->outp[ABOVE]= e0->outp[ABOVE]; + e0->outp[ABOVE]= NULL; + } + break; + case ELI: + if (q) + { + N_EDGE(next_edge, e1, ABOVE, nx, iy); + VERTEX(e1, ABOVE, LEFT, ix, iy); + VERTEX(next_edge, ABOVE, RIGHT, nx, iy); + e0->outp[ABOVE]= e1->outp[ABOVE]; + e1->outp[ABOVE]= NULL; + } + break; + case EMX: + if (p && q) + { + VERTEX(e0, ABOVE, LEFT, ix, iy); + e0->outp[ABOVE]= NULL; + e1->outp[ABOVE]= NULL; + } + break; + case IMN: + P_EDGE(prev_edge, e0, ABOVE, px, iy); + VERTEX(prev_edge, ABOVE, LEFT, px, iy); + N_EDGE(next_edge, e1, ABOVE, nx, iy); + VERTEX(next_edge, ABOVE, RIGHT, nx, iy); + new_tristrip(&tlist, prev_edge, px, iy); + e1->outp[ABOVE]= prev_edge->outp[ABOVE]; + VERTEX(e1, ABOVE, RIGHT, ix, iy); + new_tristrip(&tlist, e0, ix, iy); + next_edge->outp[ABOVE]= e0->outp[ABOVE]; + VERTEX(next_edge, ABOVE, RIGHT, nx, iy); + break; + case ILI: + if (p) + { + VERTEX(e0, ABOVE, LEFT, ix, iy); + N_EDGE(next_edge, e1, ABOVE, nx, iy); + VERTEX(next_edge, ABOVE, RIGHT, nx, iy); + e1->outp[ABOVE]= e0->outp[ABOVE]; + e0->outp[ABOVE]= NULL; + } + break; + case IRI: + if (q) + { + VERTEX(e1, ABOVE, RIGHT, ix, iy); + P_EDGE(prev_edge, e0, ABOVE, px, iy); + VERTEX(prev_edge, ABOVE, LEFT, px, iy); + e0->outp[ABOVE]= e1->outp[ABOVE]; + e1->outp[ABOVE]= NULL; + } + break; + case IMX: + if (p && q) + { + VERTEX(e0, ABOVE, RIGHT, ix, iy); + VERTEX(e1, ABOVE, LEFT, ix, iy); + e0->outp[ABOVE]= NULL; + e1->outp[ABOVE]= NULL; + P_EDGE(prev_edge, e0, ABOVE, px, iy); + VERTEX(prev_edge, ABOVE, LEFT, px, iy); + new_tristrip(&tlist, prev_edge, px, iy); + N_EDGE(next_edge, e1, ABOVE, nx, iy); + VERTEX(next_edge, ABOVE, RIGHT, nx, iy); + next_edge->outp[ABOVE]= prev_edge->outp[ABOVE]; + VERTEX(next_edge, ABOVE, RIGHT, nx, iy); + } + break; + case IMM: + if (p && q) + { + VERTEX(e0, ABOVE, RIGHT, ix, iy); + VERTEX(e1, ABOVE, LEFT, ix, iy); + P_EDGE(prev_edge, e0, ABOVE, px, iy); + VERTEX(prev_edge, ABOVE, LEFT, px, iy); + new_tristrip(&tlist, prev_edge, px, iy); + N_EDGE(next_edge, e1, ABOVE, nx, iy); + VERTEX(next_edge, ABOVE, RIGHT, nx, iy); + e1->outp[ABOVE]= prev_edge->outp[ABOVE]; + VERTEX(e1, ABOVE, RIGHT, ix, iy); + new_tristrip(&tlist, e0, ix, iy); + next_edge->outp[ABOVE]= e0->outp[ABOVE]; + VERTEX(next_edge, ABOVE, RIGHT, nx, iy); + } + break; + case EMM: + if (p && q) + { + VERTEX(e0, ABOVE, LEFT, ix, iy); + new_tristrip(&tlist, e1, ix, iy); + e0->outp[ABOVE]= e1->outp[ABOVE]; + } + break; + default: + break; + } /* End of switch */ + } /* End of contributing intersection conditional */ + + /* Swap bundle sides in response to edge crossing */ + if (e0->bundle[ABOVE][CLIP]) + e1->bside[CLIP]= !e1->bside[CLIP]; + if (e1->bundle[ABOVE][CLIP]) + e0->bside[CLIP]= !e0->bside[CLIP]; + if (e0->bundle[ABOVE][SUBJ]) + e1->bside[SUBJ]= !e1->bside[SUBJ]; + if (e1->bundle[ABOVE][SUBJ]) + e0->bside[SUBJ]= !e0->bside[SUBJ]; + + /* Swap the edge bundles in the aet */ + swap_intersecting_edge_bundles(&aet, intersect); + + } /* End of IT loop*/ + + /* Prepare for next scanbeam */ + for (edge= aet; edge; edge= next_edge) + { + next_edge= edge->next; + succ_edge= edge->succ; + + if ((edge->top.y == yt) && succ_edge) + { + /* Replace AET edge by its successor */ + succ_edge->outp[BELOW]= edge->outp[ABOVE]; + succ_edge->bstate[BELOW]= edge->bstate[ABOVE]; + succ_edge->bundle[BELOW][CLIP]= edge->bundle[ABOVE][CLIP]; + succ_edge->bundle[BELOW][SUBJ]= edge->bundle[ABOVE][SUBJ]; + prev_edge= edge->prev; + if (prev_edge) + prev_edge->next= succ_edge; + else + aet= succ_edge; + if (next_edge) + next_edge->prev= succ_edge; + succ_edge->prev= prev_edge; + succ_edge->next= next_edge; + } + else + { + /* Update this edge */ + edge->outp[BELOW]= edge->outp[ABOVE]; + edge->bstate[BELOW]= edge->bstate[ABOVE]; + edge->bundle[BELOW][CLIP]= edge->bundle[ABOVE][CLIP]; + edge->bundle[BELOW][SUBJ]= edge->bundle[ABOVE][SUBJ]; + edge->xb= edge->xt; + } + edge->outp[ABOVE]= NULL; + } + } + } /* === END OF SCANBEAM PROCESSING ================================== */ + + /* Generate result tristrip from tlist */ + result->strip= NULL; + result->num_strips= count_tristrips(tlist); + if (result->num_strips > 0) + { + MALLOC(result->strip, result->num_strips * sizeof(gpc_vertex_list), + "tristrip list creation", gpc_vertex_list); + + s= 0; + for (tn= tlist; tn; tn= tnn) + { + tnn= tn->next; + + if (tn->active > 2) + { + /* Valid tristrip: copy the vertices and free the heap */ + result->strip[s].num_vertices= tn->active; + MALLOC(result->strip[s].vertex, tn->active * sizeof(gpc_vertex), + "tristrip creation", gpc_vertex); + v= 0; + if (INVERT_TRISTRIPS) + { + lt= tn->v[RIGHT]; + rt= tn->v[LEFT]; + } + else + { + lt= tn->v[LEFT]; + rt= tn->v[RIGHT]; + } + while (lt || rt) + { + if (lt) + { + ltn= lt->next; + result->strip[s].vertex[v].x= lt->x; + result->strip[s].vertex[v].y= lt->y; + v++; + FREE(lt); + lt= ltn; + } + if (rt) + { + rtn= rt->next; + result->strip[s].vertex[v].x= rt->x; + result->strip[s].vertex[v].y= rt->y; + v++; + FREE(rt); + rt= rtn; + } + } + s++; + } + else + { + /* Invalid tristrip: just free the heap */ + for (lt= tn->v[LEFT]; lt; lt= ltn) + { + ltn= lt->next; + FREE(lt); + } + for (rt= tn->v[RIGHT]; rt; rt=rtn) + { + rtn= rt->next; + FREE(rt); + } + } + FREE(tn); + } + } + + /* Tidy up */ + reset_it(&it); + reset_lmt(&lmt); + FREE(c_heap); + FREE(s_heap); + FREE(sbt); +} + +/* +=========================================================================== + End of file: gpc.c +=========================================================================== +*/ \ No newline at end of file diff --git a/debug/image.png b/debug/image.png deleted file mode 100644 index 98f725a..0000000 Binary files a/debug/image.png and /dev/null differ diff --git a/debug/image1.png b/debug/image1.png deleted file mode 100644 index 47589a8..0000000 Binary files a/debug/image1.png and /dev/null differ diff --git a/debug/output.png b/debug/output.png new file mode 100644 index 0000000..28e40d9 Binary files /dev/null and b/debug/output.png differ diff --git a/debug/output2.png b/debug/output2.png new file mode 100644 index 0000000..35cca02 Binary files /dev/null and b/debug/output2.png differ diff --git a/debug/secondpass.png b/debug/secondpass.png new file mode 100644 index 0000000..e105c83 Binary files /dev/null and b/debug/secondpass.png differ diff --git a/docs/_sidebar.md b/docs/_sidebar.md index b4f0db1..4719a5a 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -25,8 +25,10 @@ - [Collider](api/Collider.md) - [BoxCollider](api/BoxCollider.md) - [PolygonCollider](api/PolygonCollider.md) + - [MultiPolygonCollider](api/MultiPolygonCollider.md) - **Utility Components** - [CharacterMovement](api/CharacterMovement.md) + - [GravityCharacterMovement](api/GravityCharacterMovement.md) - [Rotator](api/Rotator.md) - [Scaler](api/Scaler.md) - [Projectile](api/Projectile.md) @@ -46,6 +48,8 @@ - **Math** - [Vector](api/Vector.md) - [Engine Internals](engine-internals.md) +- **Tools** + - [Sprite Builder](spriteBuilder.md) - **Tutorials** - [Setting up the Physics Engine](tutorials/physics-engine.md) - [Integrating Physics with PhysicsBody](tutorials/physics-body.md) diff --git a/docs/api-reference.md b/docs/api-reference.md index 1264ded..f1b1168 100644 --- a/docs/api-reference.md +++ b/docs/api-reference.md @@ -33,6 +33,7 @@ This section provides a detailed reference for the core classes and components i - [Collider](./api/Collider.md) (Base class) - [BoxCollider](./api/BoxCollider.md) - [PolygonCollider](./api/PolygonCollider.md) +- [MultiPolygonCollider](./api/MultiPolygonCollider.md) - [AreaTrigger](./api/AreaTrigger.md) @@ -41,6 +42,7 @@ This section provides a detailed reference for the core classes and components i - [CameraShake](./api/CameraShake.md) - [CharacterMovement](./api/CharacterMovement.md) +- [GravityCharacterMovement](./api/GravityCharacterMovement.md) - [Follow](./api/Follow.md) - [Health](./api/Health.md) - [HealthBar](./api/HealthBar.md) diff --git a/docs/api/AnimatedSprite.md b/docs/api/AnimatedSprite.md index 39f7030..ac78a0e 100644 --- a/docs/api/AnimatedSprite.md +++ b/docs/api/AnimatedSprite.md @@ -6,7 +6,148 @@ The `AnimatedSprite` component renders an animated sprite from a spritesheet. It ## Constructor -`new AnimatedSprite({ spritesheet, spritesheetImage, width, height, startingAnimation, disableAntiAliasing, onAnimationComplete, webEngine })` +# AnimatedSprite + +**Extends:** [Renderer](./Renderer.md) + +The `AnimatedSprite` component renders an animated sprite from a spritesheet. It requires a JSON file (typically generated by a tool like TexturePacker) that defines the frames and animations. + +## Constructor + +`new AnimatedSprite({ spritesheet, spritesheetImage, width, height, startingAnimation, disableAntiAliasing, onAnimationComplete, webEngine, loop, bounce })` + +- `spritesheet: string` + The path to the JSON file that defines the spritesheet data. + +- `spritesheetImage?: string` + The path to the image file for the spritesheet. + +- `width: number` + The width of the sprite. + +- `height: number` + The height of the sprite. + +- `startingAnimation?: string` + The name of the animation to play when the sprite is created. (Or "default" to preserve spritesheet data's starting animation but force the `bounce` and `loop` properties) + +- `disableAntiAliasing?: boolean` + If `true`, disables anti-aliasing for this sprite. Defaults to `false`. + +- `onAnimationComplete?: (animationName: string, sprite: AnimatedSprite) => void` + A callback function that is called when an animation completes. + +- `webEngine?: boolean` + This property is used internally by the engine and should not be modified. + +- `loop?: boolean` + Override spritesheet data and loop immediately. + +- `bounce?: boolean` + Override spritesheet data and bounce immediately. + +## Properties + +- `spritesheet: string` + The path to the JSON data for the spritesheet. + +- `spritesheetData?: SpriteSheetData` + The parsed spritesheet data. + +- `loadedSheet?: HTMLImageElement` + The image element for the spritesheet. + +- `frames: Record` + A dictionary of all the frames for each animation. + +- `currentFrameIndex: number` + The index of the current frame in the current animation. + +- `width: number` & `height: number` + The dimensions of the sprite. + +- `bouncing: boolean` + A flag to indicate if the sprite animation is in reverse (bouncing). + +- `currentAnimation: string` + The name of the animation that is currently playing. + +- `disableAntiAliasing: boolean` + If `true`, disables anti-aliasing for this sprite. + +- `onAnimationComplete?: (animationName: string, sprite: AnimatedSprite) => void` + A callback function that is called when an animation completes. + +- `lastFrameTime` + Timestamp of the last frame update (as performance.now(), not Date.now()) + +## Methods + +- `setAnimation(animationName: string, { loop, bounce }: { loop?: boolean, bounce?: boolean } = {})` + Changes the current animation. + +## Spritesheet Format + +The engine expects a specific JSON format for the spritesheet data. See the `convertTexturePackerToSpriteSheetData` helper function and the `SpriteSheetData` type in the source code for details. + +## Examples + +### Creating an Animated Sprite + +```javascript +import { GameObject } from './Parts/GameObject'; +import { Transform } from './Parts/Children/Transform'; +import { AnimatedSprite } from './Parts/Children/AnimatedSprite'; +import { Vector } from './Math/Vector'; + +const monster = new GameObject({ name: 'Monster' }); + +monster.addChildren( + new Transform({ position: new Vector(400, 300) }), + new AnimatedSprite({ + spritesheet: './assets/monster.json', // Path to the JSON data + spritesheetImage: './assets/monster.png', // Path to the image + width: 32, + height: 32, + startingAnimation: 'walk' + }) +); + +myLayer.addChild(monster); +``` + +### Controlling Animations from another Part + +This example shows a script that changes the monster's animation based on its state. + +```javascript +import { Part } from './Parts/Part'; +import { AnimatedSprite } from './Parts/Children/AnimatedSprite'; + +class MonsterAI extends Part { + constructor() { + super(); + this.name = 'MonsterAI'; + this.isAngry = false; + } + + act() { + const animator = this.sibling('AnimatedSprite'); + if (!animator) return; + + // If the monster is angry and not already playing the attack animation... + if (this.isAngry && animator.currentAnimation !== 'attack') { + animator.setAnimation('attack'); + } else if (!this.isAngry && animator.currentAnimation !== 'walk') { + animator.setAnimation('walk'); + } + } +} + +// Add the AI to the monster GameObject +monster.addChild(new MonsterAI()); +``` + - `spritesheet: string` The path to the JSON file that defines the spritesheet data. diff --git a/docs/api/BoxCollider.md b/docs/api/BoxCollider.md index 1525cd7..dd9d9a4 100644 --- a/docs/api/BoxCollider.md +++ b/docs/api/BoxCollider.md @@ -6,7 +6,7 @@ A `BoxCollider` is a rectangular-shaped collider. It's the most common and compu ## Constructor -`new BoxCollider({ width, height })` +`new BoxCollider({ width, height, tag })` - `width: number` The width of the collision box. @@ -14,6 +14,9 @@ A `BoxCollider` is a rectangular-shaped collider. It's the most common and compu - `height: number` The height of the collision box. +- `tag?: string` + An optional tag for the collider. Defaults to `""`. + ## Properties - `start: Vector` diff --git a/docs/api/Button.md b/docs/api/Button.md index cae5c59..fff1810 100644 --- a/docs/api/Button.md +++ b/docs/api/Button.md @@ -6,7 +6,7 @@ The `Button` component creates a clickable UI button with different visual state ## Constructor -`new Button({ label, onClick, styles, clickSound, hoverSound, activeSound })` +`new Button({ label, onClick, styles, clickSound, hoverSound, activeSound, width, height, backgroundColor, color, font, borderRadius, borderWidth, borderColor, hoverBackground, hoverColor, activeBackground, activeColor })` - `label: string` The text to display on the button. @@ -26,6 +26,9 @@ The `Button` component creates a clickable UI button with different visual state - `activeSound?: Sound` A `Sound` to play when the button is pressed. +- `width?: number`, `height?: number`, `backgroundColor?: string`, `color?: string`, `font?: string`, `borderRadius?: number`, `borderWidth?: number`, `borderColor?: string`, `hoverBackground?: string`, `hoverColor?: string`, `activeBackground?: string`, `activeColor?: string` + Optional individual style properties that can be provided instead of a `styles` object. + ## Properties - `styles?: ButtonStyles` @@ -89,4 +92,4 @@ startButton.addChildren( ); myMenuScene.addChild(startButton); -``` \ No newline at end of file +``` diff --git a/docs/api/Follow.md b/docs/api/Follow.md index 90473b1..cea7080 100644 --- a/docs/api/Follow.md +++ b/docs/api/Follow.md @@ -6,7 +6,10 @@ The `Follow` component is used to make a `GameObject` follow another `Part` in t ## Constructor -`new Follow({ target, offset, interpolationSpeed })` +`new Follow({ name, target, offset, interpolationSpeed })` + +- `name?: string` + The name of the follow component. - `target: Transform` The `Transform` component of the `Part` to follow. diff --git a/docs/api/Game.md b/docs/api/Game.md index 20be34f..7f4e22f 100644 --- a/docs/api/Game.md +++ b/docs/api/Game.md @@ -6,7 +6,7 @@ The `Game` class is the heart of your application. It's the top-level container ## Constructor -`new Game({ name, canvas, devmode, width, height, disableAntiAliasing, showtoolTips })` +`new Game({ name, canvas, devmode, width, height, disableAntiAliasing, showtoolTips, showFrameStats })` - `name: string` The name of the game. @@ -29,6 +29,9 @@ The `Game` class is the heart of your application. It's the top-level container - `showtoolTips?: boolean` When set to `true`, shows tooltips in devmode. Defaults to `false`. +- `showFrameStats?: "BASIC" | "EXTENDED" | "ADVANCED" | "PERFORMANCE_HUD"` + Shows frame rate statistics. Defaults to `"BASIC"`. + ## Properties - `canvas: HTMLCanvasElement` @@ -129,4 +132,4 @@ myGame.addChild(myFirstScene); myGame.start(myFirstScene); // or by name: // myGame.start('Level 1'); -``` \ No newline at end of file +``` diff --git a/docs/api/GameObject.md b/docs/api/GameObject.md index c4c9df6..de28751 100644 --- a/docs/api/GameObject.md +++ b/docs/api/GameObject.md @@ -36,6 +36,48 @@ const myObject = new GameObject({ name: 'EmptyObject' }); This is a more typical use case. We create a `GameObject` and then add `Part`s to it to define its properties and behavior. +```javascript +# GameObject + +**Extends:** [Part](./Part.md) + +The `GameObject` is a fundamental building block. It serves as a container for other `Part`s (components) to create a single, cohesive entity in your game. Think of a `Player`, an `Enemy`, or a `Bullet`—these are all typically `GameObject`s. + +By itself, a `GameObject` doesn't do much. Its power comes from the components you add to it. + +## Constructor + +`new GameObject({ name, render })` + +- `name: string` + The name of the GameObject. + +- `render?: boolean` + Whether this GameObject should be rendered. If false, no child Parts will be ran. Defaults to `true`. + +## Properties + +A `GameObject` inherits all properties from `Part`, such as `name`, `parent`, and `children`. + +- `layer?: Layer` + A reference to the `Layer` this `GameObject` belongs to. This is automatically set when you add the `GameObject` to a `Layer`. + +## Examples + +### Creating a Simple GameObject + +This creates an empty `GameObject`. It exists in the scene but has no appearance or behavior yet. + +```javascript +import { GameObject } from './Parts/GameObject'; + +const myObject = new GameObject({ name: 'EmptyObject' }); +``` + +### Creating a Player GameObject with Components + +This is a more typical use case. We create a `GameObject` and then add `Part`s to it to define its properties and behavior. + ```javascript import { GameObject } from './Parts/GameObject'; import { Transform } from './Parts/Children/Transform'; @@ -54,4 +96,6 @@ player.addChildren( // Now, you would add the 'player' to a Layer in your Scene myLayer.addChild(player); +``` + ``` \ No newline at end of file diff --git a/docs/api/GravityCharacterMovement.md b/docs/api/GravityCharacterMovement.md new file mode 100644 index 0000000..ce64b15 --- /dev/null +++ b/docs/api/GravityCharacterMovement.md @@ -0,0 +1,78 @@ +# GravityCharacterMovement + +**Extends:** [Part](./Part.md) + +The `GravityCharacterMovement` component provides a more advanced character controller that includes gravity, jumping, and interactions with different ground types like water. + +## Constructor + +`new GravityCharacterMovement({ speed, movementType, input, gravityScale, maxSpeed, jumpForce, waterFraction, landFraction })` + +- `speed?: number` + The movement speed of the character. Defaults to `5`. + +- `movementType?: 'WASD' | 'ArrowKeys' | 'BOTH'` + The control scheme for movement. Defaults to `'WASD'`. + +- `input?: Input` + The `Input` component to use for movement. + +- `gravityScale: Vector` + The gravity vector applied to the character. + +- `maxSpeed: number` + The maximum speed of the character. + +- `jumpForce: number` + The force applied to the character when jumping. + +- `waterFraction?: number` + The fraction of normal speed and jump force when in water (0-1). Defaults to `0.5`. + +- `landFraction?: number` + The fraction of normal speed and jump force when on land (0-1). Defaults to `1.0`. + +## Properties + +- `speed: number` +- `movementType: 'WASD' | 'ArrowKeys' | 'BOTH'` +- `input: Input | undefined` +- `gravityScale: Vector` +- `maxSpeed: number` +- `velocity: Vector` +- `jumpForce: number` +- `facing: Vector` +- `waterFraction: number` +- `landFraction: number` + +## How it Works + +This component applies gravity to the character and allows for jumping. It also checks for collisions with objects tagged as 'ground' or 'water' to modify movement behavior. + +## Example + +```javascript +import { GameObject } from './Parts/GameObject'; +import { Transform } from './Parts/Children/Transform'; +import { GravityCharacterMovement } from './Parts/GravityCharacterMovement'; +import { Vector } from './Math/Vector'; +import { Input } from './Parts/Input'; + +const input = new Input({}); +myScene.addChild(input); + +const player = new GameObject({ name: 'Player' }); +player.addChildren( + new Transform({ position: new Vector(400, 300) }), + new GravityCharacterMovement({ + speed: 5, + movementType: 'BOTH', + input: input, + gravityScale: new Vector(0, 0.5), + maxSpeed: 10, + jumpForce: 10 + }) +); + +myScene.addChild(player); +``` diff --git a/docs/api/Layer.md b/docs/api/Layer.md index 0707850..745499a 100644 --- a/docs/api/Layer.md +++ b/docs/api/Layer.md @@ -8,11 +8,14 @@ For example, you would typically have a `Background` layer, a `Gameplay` layer, ## Constructor -`new Layer({ name })` +`new Layer({ name, spatialGridDefinition })` - `name: string` The name of the layer. +- `spatialGridDefinition?: number` + The cell size for the spatial grid used for broad-phase collision detection. Defaults to `100`. + ## Properties A `Layer` inherits all properties from `Part`. It does not add any of its own specific properties. diff --git a/docs/api/MultiPolygonCollider.md b/docs/api/MultiPolygonCollider.md new file mode 100644 index 0000000..299e11c --- /dev/null +++ b/docs/api/MultiPolygonCollider.md @@ -0,0 +1,57 @@ +# MultiPolygonCollider + +**Extends:** [Collider](./Collider.md) + +A `MultiPolygonCollider` is a collider that can be composed of multiple polygons. This is useful for creating complex shapes that are not possible with a single polygon, or for representing objects that have holes. + +## Constructor + +`new MultiPolygonCollider({ polygons, tag })` + +- `polygons: Vector[][]` + An array of polygons, where each polygon is an array of `Vector`s. + +- `tag?: string` + An optional tag for the collider. Defaults to `""`. + +## Properties + +- `polygons: Vector[][]` + An array of polygons, where each polygon is an array of `Vector`s. + +- `_worldPolygons: Vector[][]` + The polygons in world coordinates. + +## Examples + +### Creating a MultiPolygonCollider + +```javascript +import { GameObject } from './Parts/GameObject'; +import { Transform } from './Parts/Children/Transform'; +import { MultiPolygonCollider } from './Parts/Children/MultiPolygonCollider'; + +const myObject = new GameObject({ name: 'MyObject' }); + +myObject.addChildren( + new Transform({ /* ... */ }), + new MultiPolygonCollider({ + polygons: [ + [ + new Vector(-50, -50), + new Vector(50, -50), + new Vector(50, 50), + new Vector(-50, 50) + ], + [ + new Vector(-25, -25), + new Vector(25, -25), + new Vector(25, 25), + new Vector(-25, 25) + ] + ] + }) +); + +myGameplayLayer.addChild(myObject); +``` diff --git a/docs/api/Part.md b/docs/api/Part.md index e905506..64603c5 100644 --- a/docs/api/Part.md +++ b/docs/api/Part.md @@ -154,6 +154,204 @@ The `Part` class is the fundamental building block of the entire Forge engine. E Returns a list of parts that are of type in args, as a string. `part.siblinOf("Camera", "Input", "Renderer", "BoxCollider")` would return a list with every Camera, Input, Renderer, TextRender, ColorRender, AnimatedSprite, SpriteRender, Button, and BoxCollider that is a sibling of the part. +## Examples + +### Creating a Custom Part + +This is the foundation for all custom game logic. + +```javascript +import { Part } from './Parts/Part'; +import { Transform } from './Parts/Children/Transform'; + +class Health extends Part { + constructor() { + super(); + this.name = 'Health'; + this.currentHealth = 100; + } + + takeDamage(amount) { + this.currentHealth -= amount; + console.log(`Ouch! Health is now ${this.currentHealth}`); + if (this.currentHealth <= 0) { + // We can access the parent GameObject and tell it to do something + this.parent?.destroy(); // Assuming a destroy method exists + } + } + + act() { + // Maybe regenerate health over time + // this.currentHealth += 0.1; + } +} +``` + +### Using a Part from another Part (Sibling Communication) + +Here, a `PlayerController` part accesses its `Transform` sibling to move and its `Health` sibling to take damage. + +```javascript +import { Part } from './Parts/Part'; +import { Transform } from './Parts/Children/Transform'; +import { Health } from './Health'; // Your custom class + +# Part + +**Extends:** None (Base Class) + +The `Part` class is the fundamental building block of the entire Forge engine. Every other class, from `Game` and `Scene` to `Transform` and `SpriteRender`, inherits from `Part`. It provides the core functionality for creating a hierarchical scene graph, handling parent-child relationships, and executing per-frame logic. + +## Constructor + +`new Part({ name })` + +- `name?: string` + A human-readable name for the part. Defaults to `"New Object"`. + +## Key Concepts + +- **Hierarchy**: Parts can be nested within each other to create complex objects. A `GameObject` might have a `Transform`, a `SpriteRender`, and a `BoxCollider` as its children. +- **Lifecycle Methods**: `Part` provides methods like `onMount` and `act` that are automatically called by the engine at the appropriate times. +- **Siblings**: A `Part` can easily access its siblings (other `Part`s that share the same parent) using the `sibling()` method. This is essential for components to communicate with each other (e.g., a movement script accessing the `Transform`). + +## Properties + +- `id: string` + A unique identifier automatically generated for each `Part` instance. + +- `name: string` + A human-readable name for the part, used for debugging and for accessing children by name. + +- `childrenArray: Part[]` + An array containing all direct children, preserving the order in which they were added. This is used for iterating through children during the update loop. + +- `parent?: Part` + A reference to the `Part` that this part is a child of. It's `undefined` for the top-level `Game` part. + +- `top?: Game` + A reference to the top-level `Game` instance. This is automatically propagated down the hierarchy, giving any `Part` access to the global game state, like the rendering `context`. + +- `ready: boolean` + A boolean indicating whether the part is ready for use. + +- `registrations: { [key: string]: any }` + For storing information set by parents or other parts. + +- `flats: { colliders: Collider[] }` + Used for storing flat lists of certain types of children, like colliders. + +- `_layoutWidth: number` + Used for layout calculations in debugTreeRender. + +- `context?: CanvasRenderingContext2D` + Optional context property for rendering. Derived from the top-level parent, not usually defined unless top-level parent is a canvas element. + +- `debugEmoji?: string` + Optional emoji for debugging purposes. + +- `hoverbug?: string` + Tooltip for debug info, works with debugTreeRender. + +- `_superficialWidth: number` + General width of object. + +- `_superficialHeight: number` + General height of object. + +- `ties: Set<{ target: Part; localAttribute: string; targetAttribute: string; }>` + Ties to other parts, allowing for dynamic attribute linking. + +- `type: string` + The type of the part. + +- `base: string` + The base class of the part. For example, `BoxCollider` and `PolygonCollider` are both of base `Collider`. `Button` and `AnimatedSprite` are both of base `Renderer`. Most parts are of base `Part` + +## Methods + +- `tie(target: Part, property: string, localAttribute: string)` + Ties a local attribute of this part to a property of a target part. + +- `onclick(event: MouseEvent, clicked: Part)` + Optional click handler for the part. + +- `onhover()` + Optional hover handler for the part. + +- `onunhover()` + Optional unhover handler for the part. + +- `onmousedown(event: MouseEvent)` + Optional mousedown handler for the part. + +- `onmouseup(event: MouseEvent)` + Optional mouseup handler for the part. + +- `sibling(name: string): T | undefined` + Retrieves a sibling `Part` by its name. + +- `setSuperficialDimensions(width: number, height: number)` + Sets the superficial dimensions of the part. + +- `onMount(parent: Part)` + Called when the part is mounted to a parent. + +- `onRegister(attribute: string, value: any)` + Called when an attribute is registered. + +- `onUnregister(attribute: string, value: any)` + Called when an attribute is unregistered. + +- `addChild(child: Part)` + Adds another `Part` as a child of this one. + +- `addChildren(...children: Part[])` + Adds multiple `Part`s as children of this one. + +- `setTop(top: Game)` + Sets the top-level `Game` instance for this part and its children. + +- `attr(attribute: string, value?: T): T | undefined` + Gets or sets an attribute of the part. + +- `act(delta: number)` + The main update method. It's called on every `Part` in the scene graph on every frame of the game loop. You override this method in your custom classes to implement your game logic. + +- `setAll(attribute: string, value: any)` + Sets an attribute and propagates it to all children. + +- `calculateLayout(spacing?: { x: number, y: number })` + Calculates the layout of the part and its children for debugging purposes. + +- `removeChild(child: Part)` + Removes a specific child `Part`. + +- `debugTreeRender(x: number, y: number, spacing?: { x: number, y: number })` + Renders the debug tree for this part and its children. + +- `child(name: string): T | undefined` + Retrieves a child `Part` by its name or type. + +- `onStart()` + Called before act() is called, when the scene is first started through `Game.start()` + +- `frameEnd(delta: number)` + Called when the frame has ended. This is good if you want your Part to have final say in the scene. Eg: apply a filter some other form of post effects. + +- `isVisible(camera: Camera): boolean` + Returns true or false if the object is visible in the camera. Needs to be called on a collider or implemented in a custom class, otherwise it will not work. This is good if a Part is computationally intensive, you can call a sibling collider's isVisible method and skip an intensive rending process if it is not visible (eg: lighting). You most likely want to override this method. + +- `getPart(arg: (string | new (...args: any[]) => T)): T | undefined` + Searches through children parts to find a part of that type. Example: `part.getPart(Collider)`. It can also be used with strings, and can reference both base types and main types. Example: `part.getPart("Collider")` - this would return the first collider child, which could be a BoxCollider, PolygonCollider, or Collider instance. + +- `getChildPartRecursive(arg: (string | new (...args: any[]) => T), found: T[] = []): T[]` + This goes through the child node graph and identifies any child of type T. (Same implementation as getPart- either string or type). For example `part.getChildPartRecursive("Collider")` would get every BoxCollider, PolygonCollider, and Collider that is a child of `part`. + +- `siblingOf(...args: string[]): T | undefined` + Returns the first sibling Part that is of a type included in `args`. `part.siblingOf("Camera", "Input", "Renderer", "BoxCollider")` would return the first Camera, Input, Renderer, TextRender, ColorRender, AnimatedSprite, SpriteRender, Button, or BoxCollider that is a sibling of the part. + + ## Examples ### Creating a Custom Part @@ -227,4 +425,5 @@ player.addChildren( new Health(), new PlayerController() ); +``` ``` \ No newline at end of file diff --git a/docs/api/PolygonCollider.md b/docs/api/PolygonCollider.md index c80d006..a337669 100644 --- a/docs/api/PolygonCollider.md +++ b/docs/api/PolygonCollider.md @@ -48,6 +48,64 @@ const triangleVertices = [ new Vector(15, 20) // Bottom-right point ]; +triangleShip.addChildren( + new Transform({ + position: new Vector(100, 100), + rotation: Math.PI // Flipped upside down + }), + # PolygonCollider + +**Extends:** [Collider](./Collider.md) + +A `PolygonCollider` provides collision detection for objects with custom, complex shapes. It is defined by a list of vertices that form a convex polygon. + +This collider is more computationally expensive than a `BoxCollider` but is essential for non-rectangular shapes, especially ones that can rotate. + +## Constructor + +`new PolygonCollider({ vertices, tag })` + +- `vertices: Vector[]` + An array of `Vector`s that define the shape of the polygon. The vertices should be defined in local space, relative to the `Transform`'s position (which acts as the center of the polygon). + +- `tag?: string` + An optional tag for the collider. Defaults to `""`. + +## Properties + +- `localVertices: Vector[]` + An array of `Vector`s that define the shape of the polygon in local space. + +- `worldVertices: Vector[]` + An array of `Vector`s that define the shape of the polygon in world space. + +- `realWorldStart: Vector` + The top-left corner of the axis-aligned bounding box (AABB) of the polygon in world coordinates. + +- `realWorldEnd: Vector` + The bottom-right corner of the axis-aligned bounding box (AABB) of the polygon in world coordinates. + +## Examples + +### Creating a Triangular Collider + +This example creates a `GameObject` shaped like a triangle. + +```javascript +import { GameObject } from './Parts/GameObject'; +import { Transform } from './Parts/Children/Transform'; +import { PolygonCollider } from './Parts/Children/PolygonCollider'; +import { Vector } from './Math/Vector'; + +const triangleShip = new GameObject({ name: 'TriangleShip' }); + +// The vertices define the shape around the origin (0,0) +const triangleVertices = [ + new Vector(0, -20), // Top point + new Vector(-15, 20), // Bottom-left point + new Vector(15, 20) // Bottom-right point +]; + triangleShip.addChildren( new Transform({ position: new Vector(100, 100), @@ -59,5 +117,11 @@ triangleShip.addChildren( // You would also add a custom renderer that draws a matching triangle ); +myGameplayLayer.addChild(triangleShip); +``` + + // You would also add a custom renderer that draws a matching triangle +); + myGameplayLayer.addChild(triangleShip); ``` \ No newline at end of file diff --git a/docs/api/Projectile.md b/docs/api/Projectile.md index ef0272e..da09568 100644 --- a/docs/api/Projectile.md +++ b/docs/api/Projectile.md @@ -46,6 +46,68 @@ import { BoxCollider } from './Parts/Children/BoxCollider'; import { Projectile } from './Parts/Projectile'; import { Vector } from './Math/Vector'; +// Function to create a bullet GameObject +function createBullet(position: Vector, direction: Vector) { + const bullet = new GameObject({ name: 'Bullet' }); + bullet.addChildren( + new Transform({ position: position }), + new SpriteRender({ + imageSource: './assets/bullet.png', + width: 10, + height: 5 + }), + new BoxCollider({ width: 10, height: 5 }), + # Projectile + +**Extends:** [Part](./Part.md) + +The `Projectile` component is designed for objects that move in a straight line and can inflict damage upon collision. It's ideal for bullets, arrows, or any other one-shot moving attack. + +## Constructor + +`new Projectile({ name, speed, direction, damage })` + +- `name?: string` + The name of the projectile. + +- `speed?: number` + The speed at which the projectile travels per frame. Defaults to `10`. + +- `direction: Vector` + A `Vector` representing the normalized direction of the projectile's movement. This vector should typically have a magnitude of 1 (e.g., `new Vector(1, 0)` for right, `new Vector(0, -1)` for up). + +- `damage?: number` + The amount of damage the projectile will inflict on a `GameObject` that has a `Health` component upon collision. Defaults to `10`. + +## Properties + +- `speed: number` + The speed at which the projectile travels per frame. + +- `direction: Vector` + A `Vector` representing the normalized direction of the projectile's movement. + +- `damage: number` + The amount of damage the projectile will inflict on a `GameObject` that has a `Health` component upon collision. + +## How it Works + +The `Projectile` component requires a `Transform` sibling to control its position and a `Collider` sibling to detect collisions. When a collision occurs with another `GameObject` that has a `Health` component, the `Projectile` will call `takeDamage` on that `Health` component and then remove itself from the game. + +## Examples + +### Creating a Simple Bullet + +This example shows how to create a bullet that flies to the right and damages enemies. + +```javascript +import { GameObject } from './Parts/GameObject'; +import { Transform } from './Parts/Children/Transform'; +import { SpriteRender } from './Parts/Children/SpriteRender'; +import { BoxCollider } from './Parts/Children/BoxCollider'; +import { Projectile } from './Parts/Projectile'; +import { Vector } from './Math/Vector'; + // Function to create a bullet GameObject function createBullet(position: Vector, direction: Vector) { const bullet = new GameObject({ name: 'Bullet' }); @@ -66,6 +128,14 @@ function createBullet(position: Vector, direction: Vector) { return bullet; } +// Example usage (e.g., in a PlayerController when firing) +// myScene.addChild(createBullet(playerTransform.position, new Vector(1, 0))); +``` + + ); + return bullet; +} + // Example usage (e.g., in a PlayerController when firing) // myScene.addChild(createBullet(playerTransform.position, new Vector(1, 0))); ``` \ No newline at end of file diff --git a/docs/api/Rotator.md b/docs/api/Rotator.md index dcebbae..a200cd8 100644 --- a/docs/api/Rotator.md +++ b/docs/api/Rotator.md @@ -26,6 +26,38 @@ The `Rotator` component accesses the `Transform` sibling of its parent `GameObje This example shows how to make a coin sprite continuously spin. +```javascript +# Rotator + +**Extends:** [Part](./Part.md) + +The `Rotator` component makes a `GameObject` continuously rotate around its origin. It's useful for visual effects, spinning collectibles, or indicating active elements. + +## Constructor + +`new Rotator({ name, rotationSpeed })` + +- `name?: string` + The name of the rotator component. + +- `rotationSpeed?: number` + The speed of rotation in radians per frame. A positive value rotates clockwise, and a negative value rotates counter-clockwise. Defaults to `0.05`. + +## Properties + +- `rotationSpeed: number` + The speed of rotation in radians per frame. + +## How it Works + +The `Rotator` component accesses the `Transform` sibling of its parent `GameObject` and continuously increments its `rotation` property by the `rotationSpeed` each frame. + +## Examples + +### Making a Coin Spin + +This example shows how to make a coin sprite continuously spin. + ```javascript import { GameObject } from './Parts/GameObject'; import { Transform } from './Parts/Children/Transform'; @@ -46,4 +78,6 @@ coin.addChildren( ); myLayer.addChild(coin); +``` + ``` \ No newline at end of file diff --git a/docs/api/Scaler.md b/docs/api/Scaler.md index 74f709d..a060666 100644 --- a/docs/api/Scaler.md +++ b/docs/api/Scaler.md @@ -50,6 +50,69 @@ import { Vector } from './Math/Vector'; const heart = new GameObject({ name: 'PulsatingHeart' }); +heart.addChildren( + new Transform({ position: new Vector(400, 300) }), + new SpriteRender({ + imageSource: './assets/heart.png', + width: 50, + height: 50 + }), + new Scaler({ + # Scaler + +**Extends:** [Part](./Part.md) + +The `Scaler` component makes a `GameObject` continuously scale up and down between a minimum and maximum size. It's useful for pulsating effects, breathing animations, or indicating interactive elements. + +## Constructor + +`new Scaler({ name, scaleSpeed, minScale, maxScale })` + +- `name?: string` + The name of the scaler component. + +- `scaleSpeed?: Vector` + The rate at which the object scales per frame. This is a `Vector` to allow for independent scaling speeds on the X and Y axes. Defaults to `new Vector(0.01, 0.01)`. + +- `minScale?: Vector` + The minimum scale the object will shrink to. Defaults to `new Vector(0.5, 0.5)`. + +- `maxScale?: Vector` + The maximum scale the object will grow to. Defaults to `new Vector(1.5, 1.5)`. + +## Properties + +- `scaleSpeed: Vector` + The rate at which the object scales per frame. + +- `minScale: Vector` + The minimum scale the object will shrink to. + +- `maxScale: Vector` + The maximum scale the object will grow to. + +- `scalingUp: boolean` + A boolean indicating whether the object is currently scaling up or down. + +## How it Works + +The `Scaler` component accesses the `Transform` sibling of its parent `GameObject` and continuously adjusts its `scale` property each frame. When the object reaches its `maxScale`, it reverses direction and scales down to `minScale`, and vice-versa. + +## Examples + +### Creating a Pulsating Heart + +This example shows how to make a heart sprite pulsate in size. + +```javascript +import { GameObject } from './Parts/GameObject'; +import { Transform } from './Parts/Children/Transform'; +import { SpriteRender } from './Parts/Children/SpriteRender'; +import { Scaler } from './Parts/Scaler'; +import { Vector } from './Math/Vector'; + +const heart = new GameObject({ name: 'PulsatingHeart' }); + heart.addChildren( new Transform({ position: new Vector(400, 300) }), new SpriteRender({ @@ -64,5 +127,11 @@ heart.addChildren( }) ); +myLayer.addChild(heart); +``` + + }) +); + myLayer.addChild(heart); ``` \ No newline at end of file diff --git a/docs/api/Scene.md b/docs/api/Scene.md index 9d18027..5e55a4a 100644 --- a/docs/api/Scene.md +++ b/docs/api/Scene.md @@ -8,14 +8,11 @@ Only one `Scene` can be active at a time, and the `Game` object is responsible f ## Constructor -`new Scene({ name, backgroundColor })` +`new Scene({ name })` - `name: string` The name of the scene. -- `backgroundColor?: string` - The background color of the scene. Defaults to `"#000"` (black). - ## Properties A `Scene` inherits all properties from `Part`. diff --git a/docs/api/SpriteRender.md b/docs/api/SpriteRender.md index 18ebd4b..d90b197 100644 --- a/docs/api/SpriteRender.md +++ b/docs/api/SpriteRender.md @@ -6,7 +6,7 @@ The `SpriteRender` component is used to draw a static (non-animated) image to th ## Constructor -`new SpriteRender({ imageSource, width, height })` +`new SpriteRender({ imageSource, width, height, disableAntiAliasing, facing })` - `imageSource: string` The file path to the image you want to display. @@ -17,6 +17,12 @@ The `SpriteRender` component is used to draw a static (non-animated) image to th - `height: number` The height to draw the image. +- `disableAntiAliasing?: boolean` + If `true`, disables anti-aliasing for this sprite. Defaults to `false`. + +- `facing?: Vector` + The direction the renderer is facing. Defaults to `new Vector(1, 1)`. + ## Properties - `imageSource: string | null` diff --git a/docs/spriteBuilder.md b/docs/spriteBuilder.md new file mode 100644 index 0000000..94bd841 --- /dev/null +++ b/docs/spriteBuilder.md @@ -0,0 +1,41 @@ +# Sprite Builder + +The Sprite Builder is a tool within the Forge Engine editor that allows you to create and edit spritesheets and their animations. + +## Overview + +The Sprite Builder provides a canvas for drawing individual frames, tools for creating animations from those frames, and an export functionality to generate a spritesheet image and a compatible JSON data file for use with the `AnimatedSprite` component. + +## Layout + +- **Top Panel**: Contains global actions and settings. + - `Create Frame`: Adds a new, empty frame to the frames list. + - `Export Spritesheet`: Exports the frames and animations as a PNG image and a JSON file. + - `Overlay Frame`: Allows you to see a semi-transparent version of another frame while drawing on the current one, which is useful for animation consistency. + - `Frame Width`/`Frame Height`: Sets the dimensions for new frames. + - `Light Background`: Toggles the canvas background color between dark and light. + +- **Drawing Tools**: A panel with tools for drawing on the canvas. This panel appears when you are editing a frame. + - `Pencil`, `Line`, `Circle`, `Polygon`, `Image`, `Cursor`, `Select`: Various tools for drawing and manipulating shapes. + - `Color Picker`: Selects the drawing color. + - `Thickness`: Sets the line thickness. + - `Anti-aliasing`: Toggles anti-aliasing for smoother lines. + - `Save Drawing`/`Cancel`: Saves or discards the changes to the current frame. + +- **Center Panel**: The main canvas where you draw and edit individual frames. + +- **Right Panel**: Manages animations. + - `Animations List`: Shows all created animations. + - `Add Animation`: Creates a new animation sequence. + +- **Bottom Gutter**: Manages all the frames in your spritesheet. + - `Frames List`: Displays all created frames. You can select, reorder, and delete frames from here. + +## Workflow + +1. **Create Frames**: Click `Create Frame` to add as many frames as you need for your animations. Set the desired `Frame Width` and `Frame Height` before creating them. +2. **Draw Frames**: Click on a frame in the `Frames List` to open it in the `Center Panel`. Use the `Drawing Tools` to create your sprite art. +3. **Create Animations**: Click `Add Animation` in the `Right Panel`. Give the animation a name (e.g., "walk", "idle"). +4. **Build Animations**: Drag frames from the `Frames List` at the bottom and drop them into an animation in the `Animations List` on the right to build your animation sequence. +5. **Preview Animations**: Click the preview button on an animation to see it play in a modal window. +6. **Export**: Click `Export Spritesheet`. This will generate a single PNG image containing all your frames and a JSON file that describes the locations of those frames and the animation sequences you created. These two files can then be used with the `AnimatedSprite` component in your game. diff --git a/engine/bundle.js b/engine/bundle.js index 0cc2568..8ca3d31 100644 --- a/engine/bundle.js +++ b/engine/bundle.js @@ -6944,8 +6944,7 @@ Defaulting to 2020, but this will stop working in the future.`); this.context.push(statementParens ? types.p_stat : types.p_expr); this.exprAllowed = true; }; - types$1.incDec.updateContext = function() { - }; + types$1.incDec.updateContext = function() {}; types$1._function.updateContext = types$1._class.updateContext = function(prevType) { if (prevType.beforeExpr && prevType !== types$1._else && !(prevType === types$1.semi && this.curContext() !== types.p_stat) && !(prevType === types$1._return && lineBreak.test(this.input.slice(this.lastTokEnd, this.start))) && !((prevType === types$1.colon || prevType === types$1.braceL) && this.curContext() === types.b_stat)) { this.context.push(types.f_expr); @@ -8451,8 +8450,7 @@ Defaulting to 2020, but this will stop working in the future.`); } }; pp$1.regexp_alternative = function(state) { - while (state.pos < state.source.length && this.regexp_eatTerm(state)) { - } + while (state.pos < state.source.length && this.regexp_eatTerm(state)) {} }; pp$1.regexp_eatTerm = function(state) { if (this.regexp_eatAssertion(state)) { @@ -9765,8 +9763,7 @@ Defaulting to 2020, but this will stop working in the future.`); var value = null; try { value = new RegExp(pattern, flags); - } catch (e) { - } + } catch (e) {} return this.finishToken(types$1.regexp, { pattern, flags, value }); }; pp.readInt = function(radix, len, maybeLegacyOctalNumericLiteral) { @@ -10352,6 +10349,24 @@ function isPointInObject(mouseX, mouseY, child) { } return false; } +function vecEq(a, b) { + return a.x === b.x && a.y === b.y; +} +function pointInPoly(point, poly) { + let inside = false; + for (let i = 0, j = poly.length - 1;i < poly.length; j = i++) { + const xi = poly[i].x, yi = poly[i].y; + const xj = poly[j].x, yj = poly[j].y; + const onSegment = (point.y - yi) * (xj - xi) === (point.x - xi) * (yj - yi) && (Math.min(xi, xj) <= point.x && point.x <= Math.max(xi, xj)) && (Math.min(yi, yj) <= point.y && point.y <= Math.max(yi, yj)); + if (onSegment) { + return true; + } + const intersect = yi > point.y !== yj > point.y && point.x < (xj - xi) * (point.y - yi) / (yj - yi) + xi; + if (intersect) + inside = !inside; + } + return inside; +} // Parts/Part.ts class Part { @@ -10458,8 +10473,7 @@ class Part { onMount(parent) { this.parent = parent; } - onRegister(attribute, value) { - } + onRegister(attribute, value) {} onUnregister(attribute, value, debug) { if (debug) console.log(debug, value.name); @@ -10482,8 +10496,7 @@ class Part { break; } } - onUnmount() { - } + onUnmount() {} onStart() { this.childrenArray.forEach((child) => { if (typeof child.onStart === "function") { @@ -10839,8 +10852,7 @@ class SoundManagerController { static instance; sounds = []; isGameRunning = false; - constructor() { - } + constructor() {} static getInstance() { if (!SoundManagerController.instance) { SoundManagerController.instance = new SoundManagerController; @@ -10889,10 +10901,7 @@ class Game extends Part { childrenArray; devmode; context; - showtoolTips = false; hovering; - tooltipLocked; - lastMousePosition = { x: 0, y: 0 }; scaleFactor = 1; canvasOffset = { x: 0, y: 0 }; messageHook; @@ -10909,9 +10918,8 @@ class Game extends Part { _animationFrameId; _lastUpdateTime = 0; constructor({ name, canvas, devmode = false, width, height, disableAntiAliasing = false, showtoolTips = false, showFrameStats = "BASIC" }) { - super(); - this.name = name; - this.showtoolTips = showtoolTips; + super({ name }); + this.type = "Game"; this.childrenArray = []; this.showFrameStats = showFrameStats; this.canvas = typeof canvas === "string" ? document.getElementById(canvas) : canvas; @@ -10920,30 +10928,7 @@ class Game extends Part { this.changeCanvasSize(width, height); this.context.imageSmoothingEnabled = !disableAntiAliasing; this.debugEmoji = "\uD83C\uDFAE"; - this.tooltipLocked = false; this.top = this; - if (this.devmode) { - let tooltip = document.getElementById("debug-tooltip"); - if (!tooltip) { - tooltip = this.createDebugTooltip(); - } - document.addEventListener("mousemove", (event) => { - const rect = this.canvas.getBoundingClientRect(); - const clientX = event.clientX - rect.left; - const clientY = event.clientY - rect.top; - this.lastMousePosition = { - x: clientX / this.scaleFactor - this.canvasOffset.x / this.scaleFactor, - y: clientY / this.scaleFactor - this.canvasOffset.y / this.scaleFactor - }; - }); - document.addEventListener("click", (event) => { - if (tooltip && !this.tooltipLocked) { - this.tooltipLocked = true; - } else if (tooltip) { - this.tooltipLocked = false; - } - }); - } } clone(memo = new Map) { if (memo.has(this)) { @@ -10955,8 +10940,7 @@ class Game extends Part { devmode: this.devmode, width: this.width, height: this.height, - disableAntiAliasing: !this.context.imageSmoothingEnabled, - showtoolTips: this.showtoolTips + disableAntiAliasing: !this.context.imageSmoothingEnabled }); memo.set(this, clonedGame); this._cloneProperties(clonedGame, memo); @@ -10964,8 +10948,6 @@ class Game extends Part { clonedGame.context = undefined; clonedGame.currentScene = undefined; clonedGame.hovering = undefined; - clonedGame.tooltipLocked = undefined; - clonedGame.lastMousePosition = { x: 0, y: 0 }; clonedGame.scaleFactor = 1; clonedGame.canvasOffset = { x: 0, y: 0 }; clonedGame.messageHook = undefined; @@ -10995,20 +10977,6 @@ class Game extends Part { get height() { return this._height; } - createDebugTooltip() { - const tooltip = document.createElement("div"); - tooltip.id = "debug-tooltip"; - tooltip.style.position = "absolute"; - tooltip.style.backgroundColor = "rgba(0, 0, 0, 0.7)"; - tooltip.style.color = "white"; - tooltip.style.padding = "5px"; - tooltip.style.display = "none"; - tooltip.style.borderRadius = "5px"; - tooltip.style.pointerEvents = "none"; - tooltip.style.zIndex = "1000"; - document.body.appendChild(tooltip); - return tooltip; - } addChild(scene) { this.currentScene = this.currentScene || scene; scene.setTop(this); @@ -11076,18 +11044,24 @@ class Game extends Part { this._animationFrameId = window.requestAnimationFrame(this.loop.bind(this)); } } - getColliderCount() { + getColliderCount(activeOnly = false) { const layers = this.currentScene?.childrenArray || []; let c = 0; for (const layer of layers) { - const colliders = layer.flats.colliders.length; - c += colliders; + if (!activeOnly) { + const colliders = layer.flats.colliders.length; + c += colliders; + } else { + const colliders = layer.flats.colliders.filter((col) => col.active).length; + c += colliders; + } } return c; } renderFrameStats() { if (!this.showFrameStats) return; + const FADE_BACKGROUND = 0.5; const avgDelta = this.frameBuffer.reduce((a, b) => a + b, 0) / this.frameBuffer.length; const avgFPS = 1000 / avgDelta; const sorted = [...this.frameBuffer].sort((a, b) => a - b); @@ -11095,56 +11069,82 @@ class Game extends Part { const p99 = sorted[Math.floor(sorted.length * 0.99)]; const minFrameTime = sorted[0]; const maxFrameTime = sorted[sorted.length - 1]; - this.context.fillStyle = "white"; - this.context.font = "12px Arial"; - let y = 20; + let lines = []; const levels = ["BASIC", "EXTENDED", "ADVANCED", "PERFORMANCE_HUD"]; const levelIndex = levels.indexOf(this.showFrameStats); - if (levelIndex >= 0) { - this.context.fillText(`FPS: ${avgFPS.toFixed(2)}`, 10, y); - y += 20; - } - if (levelIndex >= 1) { - this.context.fillText(`Frame Time: ${avgDelta.toFixed(2)} ms`, 10, y); - y += 20; - } + if (levelIndex >= 0) + lines.push(`FPS: ${avgFPS.toFixed(2)}`); + if (levelIndex >= 1) + lines.push(`Frame Time: ${avgDelta.toFixed(2)} ms`); if (levelIndex >= 2) { - this.context.fillText(`Min: ${minFrameTime.toFixed(2)} (${this._minFrameTime.toFixed(2)} AT) ms`, 10, y); - y += 20; - this.context.fillText(`Max: ${maxFrameTime.toFixed(2)} (${this._maxFrameTime.toFixed(2)} AT) ms`, 10, y); - y += 20; + lines.push(`Min: ${minFrameTime.toFixed(2)} (${this._minFrameTime.toFixed(2)} AT) ms`); + lines.push(`Max: ${maxFrameTime.toFixed(2)} (${this._maxFrameTime.toFixed(2)} AT) ms`); } if (levelIndex >= 3) { - this.context.fillText(`p95 Frame: ${p95.toFixed(2)} ms`, 10, y); - y += 20; - this.context.fillText(`p99 Frame: ${p99.toFixed(2)} ms`, 10, y); - y += 20; + lines.push(`p95 Frame: ${p95.toFixed(2)} ms`); + lines.push(`p99 Frame: ${p99.toFixed(2)} ms`); const droppedPct = this._droppedFrames / (this.frameBuffer.length || 1) * 100; - this.context.fillText(`Dropped Frames: ${droppedPct.toFixed(1)}%`, 10, y); - y += 20; + lines.push(`Dropped Frames: ${droppedPct.toFixed(1)}%`); const perfMem = performance.memory; if (perfMem) { const usedMB = (perfMem.usedJSHeapSize / 1048576).toFixed(1); const totalMB = (perfMem.totalJSHeapSize / 1048576).toFixed(1); - this.context.fillText(`Heap: ${usedMB} MB / ${totalMB} MB`, 10, y); - y += 20; + lines.push(`Heap: ${usedMB} MB / ${totalMB} MB`); } if (this.currentScene) { - this.context.fillText(`Colliders: ${this.getColliderCount()}`, 10, y); - y += 20; - } + lines.push(`Colliders: ${this.getColliderCount()}`); + lines.push(`Active colliders: ${this.getColliderCount(true)}`); + } + } + const fontSize = 12; + const lineHeight = 20; + const padding = 8; + this.context.font = `${fontSize}px Arial`; + let maxWidth = 0; + for (const line of lines) { + const width = this.context.measureText(line).width; + if (width > maxWidth) + maxWidth = width; + } + let boxHeight = lines.length * lineHeight + padding * 2; + let boxWidth = maxWidth + padding * 2; + let boxX = 6; + let boxY = 6; + this.context.globalAlpha = FADE_BACKGROUND; + this.context.fillStyle = "#000"; + this.context.fillRect(boxX, boxY, boxWidth, boxHeight); + this.context.globalAlpha = 1; + this.context.fillStyle = "white"; + let y = boxY + padding + fontSize; + for (const line of lines) { + this.context.fillText(line, boxX + padding, y); + y += lineHeight; + } + if (levelIndex >= 3) { const chartWidth = 200; const chartHeight = 80; - const chartX = 10; - const chartY = y + 10; - const maxFrameTime2 = Math.max(...this.frameBuffer); + const chartX = boxX + padding; + const chartY = boxY + boxHeight + 10; + const minFrameTimeChart = Math.min(...this.frameBuffer); + const maxFrameTimeChart = Math.max(...this.frameBuffer); + const margin = Math.max(2, (maxFrameTimeChart - minFrameTimeChart) * 0.2); + const chartMin = Math.max(0, minFrameTimeChart - margin); + const chartMax = maxFrameTimeChart + margin; + const range = Math.max(1, chartMax - chartMin); + this.context.globalAlpha = FADE_BACKGROUND; + this.context.fillStyle = "#000"; + this.context.fillRect(chartX - padding, chartY - padding, chartWidth + padding * 2, chartHeight + padding * 2); + this.context.globalAlpha = 1; this.context.strokeStyle = "white"; this.context.beginPath(); - this.context.moveTo(chartX, chartY + chartHeight); this.frameBuffer.forEach((frameTime, index) => { - const x = chartX + index / this.maxFrameBufferLength * chartWidth; - const yVal = chartY + chartHeight - frameTime / maxFrameTime2 * chartHeight; - this.context.lineTo(x, yVal); + const x = chartX + index / (this.maxFrameBufferLength - 1) * chartWidth; + const yVal = chartY + chartHeight - (frameTime - chartMin) / range * chartHeight; + if (index === 0) { + this.context.moveTo(x, yVal); + } else { + this.context.lineTo(x, yVal); + } }); this.context.stroke(); } @@ -11237,35 +11237,69 @@ class Game extends Part { return false; } } - updateDebugToolTip() { - const tooltip = document.getElementById("debug-tooltip"); - if (!tooltip) { - this.warn("Debug tooltip not found. Ensure it is created in devmode."); - return; - } - if (this.hovering) { - if (tooltip && this.showtoolTips) { - try { - tooltip.style.left = `${this.lastMousePosition.x * this.scaleFactor + this.canvasOffset.x + 10}px`; - tooltip.style.top = `${this.lastMousePosition.y * this.scaleFactor + this.canvasOffset.y + 10}px`; - tooltip.style.display = "block"; - tooltip.innerHTML = getDebugInfo(this.hovering, 0); - } catch (err) { - throw new Error(`Error updating debug tooltip: ${err}`); +} +// Math/SpatialGrid.ts +class SpatialGrid { + cells; + cellSize; + constructor(cellSize) { + this.cells = new Map; + this.cellSize = cellSize; + } + getKey(x, y) { + return `${Math.floor(x / this.cellSize)}_${Math.floor(y / this.cellSize)}`; + } + clear() { + this.cells.clear(); + } + insert(collider) { + const start = collider.realWorldStart; + const end = collider.realWorldEnd; + const startX = Math.floor(start.x / this.cellSize); + const startY = Math.floor(start.y / this.cellSize); + const endX = Math.floor(end.x / this.cellSize); + const endY = Math.floor(end.y / this.cellSize); + for (let x = startX;x <= endX; x++) { + for (let y = startY;y <= endY; y++) { + const key = `${x}_${y}`; + if (!this.cells.has(key)) { + this.cells.set(key, []); + } + this.cells.get(key).push(collider); + } + } + } + query(collider) { + const candidates = new Set; + const start = collider.realWorldStart; + const end = collider.realWorldEnd; + const startX = Math.floor(start.x / this.cellSize); + const startY = Math.floor(start.y / this.cellSize); + const endX = Math.floor(end.x / this.cellSize); + const endY = Math.floor(end.y / this.cellSize); + for (let x = startX;x <= endX; x++) { + for (let y = startY;y <= endY; y++) { + const key = `${x}_${y}`; + if (this.cells.has(key)) { + for (const other of this.cells.get(key)) { + candidates.add(other); + } } } - } else { - tooltip.style.display = "none"; } + return Array.from(candidates); } } + // Parts/Layer.ts class Layer extends Part { - constructor({ name }) { - super(); - this.name = name; + spatialGrid; + constructor({ name, spatialGridDefinition }) { + super({ name }); + this.type = "Layer"; this.id = generateUID(); this.debugEmoji = "\uD83D\uDDC2️"; + this.spatialGrid = new SpatialGrid(spatialGridDefinition || 100); } addChild(part) { part.setAll("layer", this); @@ -11282,6 +11316,13 @@ class Layer extends Part { if (!this.ready) { return; } + this.spatialGrid.clear(); + const colliders = this.flats.colliders; + for (const collider of colliders) { + if (collider.active) { + this.spatialGrid.insert(collider); + } + } this.ties.forEach((tie) => { if (tie.target && tie.target.hasOwnProperty(tie.targetAttribute)) { const value = this.attr(tie.localAttribute); @@ -11296,9 +11337,9 @@ class Layer extends Part { // Parts/GameObject.ts class GameObject extends Part { layer; - constructor({ name, render }) { - super({ name, render }); - this.name = name; + constructor({ name, render = true }) { + super({ name, render: !!render }); + this.type = "GameObject"; this.debugEmoji = "\uD83D\uDD79️"; } } @@ -11361,8 +11402,9 @@ class Vector { } normalize() { const len = this.length(); - if (len === 0) - throw new Error("Cannot normalize zero-length vector"); + if (len === 0) { + return new Vector(0, 0); + } return new Vector(this.x / len, this.y / len); } dot(other) { @@ -11389,7 +11431,11 @@ class Vector { return this; } static From(scalar) { - return new Vector(scalar, scalar); + if (typeof scalar === "number") { + return new Vector(scalar, scalar); + } else { + return new Vector(scalar.x, scalar.y); + } } } @@ -11469,14 +11515,10 @@ class Input extends Part { return memo.get(this); } const clonedInput = new Input({ - key: this.key || (() => { - }), - keyup: this.keyup || (() => { - }), - mousemove: this.mousemove || (() => { - }), - click: this.click || (() => { - }) + key: this.key || (() => {}), + keyup: this.keyup || (() => {}), + mousemove: this.mousemove || (() => {}), + click: this.click || (() => {}) }); memo.set(this, clonedInput); this._cloneProperties(clonedInput, memo); @@ -11999,14 +12041,23 @@ class Collider extends Part { realWorldStart; realWorldEnd; vertices; - constructor({ tag }) { + active = true; + allowMerge; + randomTestingColors; + constructor({ tag, allowMerge }) { super({ name: "Collider" }); this.type = "Collider"; this.base = "Collider"; this.tag = tag || ""; this.radius = 0; this.realWorldStart = new Vector(0, 0); + this.allowMerge = allowMerge !== undefined ? allowMerge : true; this.realWorldEnd = new Vector(0, 0); + this.randomTestingColors = [ + Math.random() * 255, + Math.random() * 255, + Math.random() * 255 + ]; this.vertices = []; } setTag(tag) { @@ -12027,8 +12078,62 @@ class Collider extends Part { value.flats.colliders.push(this); } } + evaluateMerging() { + const layer = this.registrations["layer"]; + if (!layer) + return; + const candidates = layer.spatialGrid.query(this); + const fellowColliders = candidates.filter((c) => c.tag == this.tag && c.id !== this.id && c.allowMerge && c.active); + if (fellowColliders.length == 0) + return; + for (const fellow of fellowColliders) { + if (!fellow.sibling("Transform")?.initialized) + continue; + if (this.id < fellow.id && this.checkCollision(fellow, true)) { + this.mergeWith(fellow); + } + } + } + mergeWith(other) { + if (this.tag !== other.tag || other.tag == "" || this.tag == "") + return; + const thisTransform = this.sibling("Transform"); + if (!thisTransform) { + this.top?.warn(`Collider <${this.name}> has no Transform sibling, cannot merge.`); + return; + } + const allPolygons = []; + const g1 = this.getGeometry(); + if (this.type === "MultiPolygonCollider") { + allPolygons.push(...g1); + } else { + allPolygons.push(g1); + } + const g2 = other.getGeometry(); + if (other.type === "MultiPolygonCollider") { + allPolygons.push(...g2); + } else { + allPolygons.push(g2); + } + if (allPolygons.length === 0) + return; + const localPolygons = allPolygons.map((polygon) => { + return polygon.map(([x, y]) => thisTransform.worldToLocal(new Vector(x, y))); + }); + this._updateVerticesAfterMerge(localPolygons); + other.inactivate(); + } + onStart() {} + inactivate() { + this.active = false; + } + activate() { + this.active = true; + } act(delta) { super.act(delta); + if (!this.active) + return; if (!this.registrations?.layer) { throw new Error(`Collider <${this.name}> (${this.id}) is not registered to a layer. Collisions will not be checked.`); } @@ -12039,8 +12144,8 @@ class Collider extends Part { this.colliding = false; this.collidingWith.clear(); const layer = this.registrations.layer; - const colliders = layer.flats.colliders; - for (const other of colliders) { + const candidates = layer.spatialGrid.query(this); + for (const other of candidates) { if (other === this) continue; if (this.checkCollision(other)) { @@ -12049,15 +12154,36 @@ class Collider extends Part { } } this.hoverbug = `${this.colliding ? "\uD83D\uDFE5" : "\uD83D\uDFE9"} - ${Array.from(this.collidingWith).map((o) => o.name).join(", ")} objects`; + const fill = this.active; + const ctx = this.top instanceof Game ? this.top.context : null; + if (ctx) { + ctx.beginPath(); + ctx.strokeStyle = `rgb(${this.randomTestingColors[0]}, ${this.randomTestingColors[1]}, ${this.randomTestingColors[2]})`; + ctx.fillStyle = fill ? `rgba(${this.randomTestingColors[0]}, ${this.randomTestingColors[1]}, ${this.randomTestingColors[2]}, 0.5)` : "transparent"; + ctx.moveTo(this.worldVertices[0].x, this.worldVertices[0].y); + for (const vertex of this.worldVertices) { + ctx.lineTo(vertex.x, vertex.y); + } + ctx.closePath(); + ctx.stroke(); + ctx.fill(); + } if (this.top instanceof Game && this.top.devmode) { - const ctx = this.top.context; - if (ctx) { - this.drawDebug(ctx); + const ctx2 = this.top.context; + if (ctx2) { + this.drawDebug(ctx2); } } } - checkCollision(other) { - if (other.tag === this.tag && this.tag !== "") + checkCollision(other, ignoreTags = false) { + const thisTransform = this.sibling("Transform"); + const otherTransform = other.sibling("Transform"); + if (!thisTransform || !otherTransform) { + return false; + } + this.updateCollider(thisTransform); + other.updateCollider(otherTransform); + if (!ignoreTags && other.tag === this.tag && this.tag !== "") return false; if (this.realWorldEnd.x < other.realWorldStart.x || this.realWorldStart.x > other.realWorldEnd.x || this.realWorldEnd.y < other.realWorldStart.y || this.realWorldStart.y > other.realWorldEnd.y) { return false; @@ -12089,9 +12215,15 @@ class Collider extends Part { } checkVerticesAgainstVertices(vertices1, vertices2) { const axes1 = this.getAxes(vertices1); + for (const axis of axes1) { + const projection1 = this.project(vertices1, axis); + const projection2 = this.project(vertices2, axis); + if (!this.overlap(projection1, projection2)) { + return false; + } + } const axes2 = this.getAxes(vertices2); - const axes = axes1.concat(axes2); - for (const axis of axes) { + for (const axis of axes2) { const projection1 = this.project(vertices1, axis); const projection2 = this.project(vertices2, axis); if (!this.overlap(projection1, projection2)) { @@ -12127,14 +12259,159 @@ class Collider extends Part { overlap(proj1, proj2) { return proj1.max >= proj2.min && proj2.max >= proj1.min; } + _checkPolygonVsPolygon(vertices1, vertices2) { + const axes1 = this.getAxes(vertices1); + const axes2 = this.getAxes(vertices2); + const axes = axes1.concat(axes2); + for (const axis of axes) { + const projection1 = this.project(vertices1, axis); + const projection2 = this.project(vertices2, axis); + if (!this.overlap(projection1, projection2)) { + return false; + } + } + return true; + } +} + +// Parts/Children/MultiPolygonCollider.ts +class MultiPolygonCollider extends Collider { + polygons; + _worldPolygons = []; + unioned = []; + constructor({ polygons, tag = "" }) { + super({ tag, allowMerge: tag !== "" }); + this.name = "MultiPolygonCollider"; + this.polygons = polygons; + this.type = "MultiPolygonCollider"; + let maxDist = 0; + const allVertices = polygons.flat(); + for (let i = 0;i < allVertices.length; i++) { + for (let j = i + 1;j < allVertices.length; j++) { + const dist = allVertices[i].distance(allVertices[j]); + if (dist > maxDist) { + maxDist = dist; + } + } + } + this.radius = maxDist; + } + getGeometry() { + return this._worldPolygons.map((polygon) => { + return polygon.map((v) => v.toArray()); + }); + } + get worldVertices() { + const allVertices = this._worldPolygons.flat(); + allVertices.sort((a, b) => { + return a.x < b.x || a.x == b.x && a.y < b.y ? -1 : 1; + }); + const cross = (o, a, b) => { + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); + }; + const lower = []; + for (const p of allVertices) { + while (lower.length >= 2 && cross(lower[lower.length - 2], lower[lower.length - 1], p) <= 0) { + lower.pop(); + } + lower.push(p); + } + const upper = []; + for (let i = allVertices.length - 1;i >= 0; i--) { + const p = allVertices[i]; + while (upper.length >= 2 && cross(upper[upper.length - 2], upper[upper.length - 1], p) <= 0) { + upper.pop(); + } + upper.push(p); + } + upper.pop(); + lower.pop(); + return lower.concat(upper); + } + act(delta) { + super.act(delta); + } + _updateVerticesAfterMerge(polygons) { + this.polygons = polygons; + } + updateCollider(transform) { + const position = transform.worldPosition; + const rotation = transform.rotation; + const scale = transform.scale; + this._worldPolygons = this.polygons.map((polygon) => { + return polygon.map((vertex) => { + let scaledVertex = vertex.multiply(scale); + if (rotation !== 0) { + const cos = Math.cos(rotation); + const sin = Math.sin(rotation); + scaledVertex = new Vector(scaledVertex.x * cos - scaledVertex.y * sin, scaledVertex.x * sin + scaledVertex.y * cos); + } + return position.add(scaledVertex); + }); + }); + const allWorldVertices = this._worldPolygons.flat(); + const xs = allWorldVertices.map((v) => v.x); + const ys = allWorldVertices.map((v) => v.y); + this.realWorldStart.set(Math.min(...xs), Math.min(...ys)); + this.realWorldEnd.set(Math.max(...xs), Math.max(...ys)); + } + narrowPhaseCheck(other) { + if (other instanceof MultiPolygonCollider) { + for (const p1 of this._worldPolygons) { + for (const p2 of other._worldPolygons) { + if (this._checkPolygonVsPolygon(p1, p2)) { + return true; + } + } + } + return false; + } + for (const polygon of this._worldPolygons) { + if (this._checkPolygonVsPolygon(polygon, other.worldVertices)) { + return true; + } + } + return false; + } + drawDebug(ctx) { + ctx.save(); + ctx.strokeStyle = this.colliding ? "rgba(255, 0, 100, 0.8)" : "rgba(0, 255, 100, 0.8)"; + ctx.lineWidth = 1; + for (const polygon of this._worldPolygons) { + ctx.beginPath(); + ctx.moveTo(polygon[0].x, polygon[0].y); + for (let i = 1;i < polygon.length; i++) { + ctx.lineTo(polygon[i].x, polygon[i].y); + } + ctx.closePath(); + ctx.stroke(); + } + ctx.restore(); + } + clone(memo = new Map) { + if (memo.has(this)) { + return memo.get(this); + } + const clonedMultiPolygonCollider = new MultiPolygonCollider({ + polygons: this.polygons.map((p) => p.map((v) => v.clone())), + tag: this.tag + }); + memo.set(this, clonedMultiPolygonCollider); + this._cloneProperties(clonedMultiPolygonCollider, memo); + clonedMultiPolygonCollider.colliding = false; + clonedMultiPolygonCollider.base = this.base; + clonedMultiPolygonCollider.type = this.type; + clonedMultiPolygonCollider.collidingWith = new Set; + return clonedMultiPolygonCollider; + } } // Parts/Children/PolygonCollider.ts class PolygonCollider extends Collider { localVertices; _worldVertices = []; - constructor({ vertices, tag }) { - super({ tag }); + constructor({ vertices, tag = "" }) { + super({ tag, allowMerge: tag !== "" }); this.name = "PolygonCollider"; this.localVertices = vertices; this.vertices = vertices; @@ -12153,9 +12430,22 @@ class PolygonCollider extends Collider { get worldVertices() { return this._worldVertices; } + getGeometry() { + return [this.worldVertices.map((v) => v.toArray())]; + } act(delta) { super.act(delta); } + _updateVerticesAfterMerge(polygons) { + const newCollider = new MultiPolygonCollider({ polygons, tag: this.tag }); + newCollider.active = this.active; + newCollider.allowMerge = this.allowMerge; + const parent = this.parent; + if (parent) { + parent.removeChild(this); + parent.addChild(newCollider); + } + } updateCollider(transform) { const position = transform.worldPosition; const rotation = transform.rotation; @@ -12179,22 +12469,14 @@ class PolygonCollider extends Collider { return this.checkPolygonVsBox(this, other); } else if (other instanceof PolygonCollider) { return this.checkPolygonVsPolygon(this, other); + } else if (other instanceof MultiPolygonCollider) { + return other.narrowPhaseCheck(this); } this.top?.warn("Collision checks are only supported between BoxColliders and PolygonColliders."); return false; } checkPolygonVsPolygon(poly1, poly2) { - const axes1 = this.getAxes(poly1.worldVertices); - const axes2 = this.getAxes(poly2.worldVertices); - const axes = axes1.concat(axes2); - for (const axis of axes) { - const projection1 = this.project(poly1.worldVertices, axis); - const projection2 = this.project(poly2.worldVertices, axis); - if (!this.overlap(projection1, projection2)) { - return false; - } - } - return true; + return this._checkPolygonVsPolygon(poly1.worldVertices, poly2.worldVertices); } checkPolygonVsBox(poly, box) { const boxVertices = box.worldVertices; @@ -12251,8 +12533,8 @@ class BoxCollider extends Collider { cachedAxes = []; lastRotation = NaN; lastScale = new Vector(NaN, NaN); - constructor({ width, height, tag }) { - super({ tag }); + constructor({ width, height, tag = "" }) { + super({ tag, allowMerge: tag !== "" }); this.name = "BoxCollider"; this.width = width; this.height = height; @@ -12268,6 +12550,9 @@ class BoxCollider extends Collider { get worldVertices() { return this.rotatedCorners; } + getGeometry() { + return [this.worldVertices.map((v) => v.toArray())]; + } updateCollider(transform) { const cos = Math.cos(transform.rotation); const sin = Math.sin(transform.rotation); @@ -12306,7 +12591,7 @@ class BoxCollider extends Collider { narrowPhaseCheck(other) { if (other instanceof BoxCollider) { return this.checkBoxVsBox(this, other); - } else if (other instanceof PolygonCollider) { + } else if (other instanceof PolygonCollider || other instanceof MultiPolygonCollider) { return other.narrowPhaseCheck(this); } this.top?.warn(`Collision with unsupported collider type: ${other.type}`); @@ -12322,6 +12607,16 @@ class BoxCollider extends Collider { } return true; } + _updateVerticesAfterMerge(polygons) { + const newCollider = new MultiPolygonCollider({ polygons, tag: this.tag }); + newCollider.active = this.active; + newCollider.allowMerge = this.allowMerge; + const parent = this.parent; + if (parent) { + parent.removeChild(this); + parent.addChild(newCollider); + } + } act(delta) { super.act(delta); } @@ -12510,14 +12805,10 @@ class Button extends Renderer { if (scene) { if (!scene.child("Input")) { const input = new Input({ - key: () => { - }, - keyup: () => { - }, - mousemove: () => { - }, - click: () => { - } + key: () => {}, + keyup: () => {}, + mousemove: () => {}, + click: () => {} }); scene.addChild(input); } @@ -12644,9 +12935,6 @@ class ColorRender extends Renderer { if (!this.top) { throw new Error(`ColorRender <${this.parent?.name}.${this.name}> is not attached to a top-level parent. Ensure it is added to a Game instance or Scene before rendering.`); } - if (!(this.top instanceof Game)) { - throw new Error(`ColorRender <${this.parent?.name}.${this.name}> is not attached to a Game instance. Ensure it is added to a Game, Scene, or Layer with a game ancestor.`); - } const transform = this.sibling("Transform"); if (!transform) { this.top?.warn(`ColorRender <${this.parent?.name}.${this.name}> does not have a Transform sibling. Skipping rendering.`); @@ -12822,6 +13110,7 @@ class Transform extends Part { worldPosition; rotation; scale; + initialized; constructor({ position, rotation, scale } = {}) { super({ name: "Transform" }); this.position = position || Vector.From(0); @@ -12831,15 +13120,11 @@ class Transform extends Part { this.scale = scale || new Vector(1, 1); this.debugEmoji = "\uD83D\uDCD0"; this.type = "Transform"; + this.initialized = false; } onMount(parent) { super.onMount(parent); - const grandparentTransform = parent.sibling("Transform"); - if (grandparentTransform) { - this.worldPosition.set(this.position.add(grandparentTransform.worldPosition)); - } else { - this.worldPosition.set(this.position); - } + this.updateWorldPosition(); if (parent.superficialWidth && parent.superficialHeight) { this.superficialWidth = parent.superficialWidth; this.superficialHeight = parent.superficialHeight; @@ -12862,16 +13147,32 @@ class Transform extends Part { this.rotation = rotation % (2 * Math.PI); this.updateWorldPosition(); } + worldToLocal(position) { + const translated = position.subtract(this.worldPosition); + const cos = Math.cos(-this.rotation); + const sin = Math.sin(-this.rotation); + const rotated = new Vector(translated.x * cos - translated.y * sin, translated.x * sin + translated.y * cos); + const scaled = new Vector(rotated.x / this.scale.x, rotated.y / this.scale.y); + return scaled; + } + preFrame() { + super.preFrame(); + this.updateWorldPosition(); + } updateWorldPosition() { - const parentTransform = this.parent?.sibling("Transform"); + const parentTransform = this.parent?.parent?.child("Transform"); if (parentTransform) { - this.worldPosition.set(this.position.add(parentTransform.worldPosition)); + const scaledPosition = this.position.multiply(parentTransform.scale); + const cos = Math.cos(parentTransform.rotation); + const sin = Math.sin(parentTransform.rotation); + const rotatedPosition = new Vector(scaledPosition.x * cos - scaledPosition.y * sin, scaledPosition.x * sin + scaledPosition.y * cos); + this.worldPosition.set(rotatedPosition.add(parentTransform.worldPosition)); } else { this.worldPosition.set(this.position); } + this.initialized = true; } act(_delta) { - this.updateWorldPosition(); this.hoverbug = `${this.position.toString()} | ${this.worldPosition.toString()} | ${(this.rotation / Math.PI).toFixed(2)}pi | ${this.scale.toString()}`; } } @@ -13056,8 +13357,7 @@ class Health extends Part { currentHealth; onDeath; isDead = false; - constructor({ maxHealth = 100, onDeath = () => { - } }) { + constructor({ maxHealth = 100, onDeath = () => {} }) { super({ name: "Health" }); this.maxHealth = maxHealth; this.currentHealth = maxHealth; @@ -13270,7 +13570,7 @@ class CharacterMovement extends Part { this.input = input; this.type = "CharacterMovement"; } - act(_delta) { + act(delta) { if (!this.input) { if (!this.warned.has("MissingInput")) this.top?.warn(`CharacterMovement <${this.name}> (${this.id}) is missing an input property. Please create an input on the scene and pass it.`) && this.warned.add("MissingInput"); @@ -13280,6 +13580,7 @@ class CharacterMovement extends Part { if (!transform) { return; } + const speed = this.speed * delta; const keys = this.input.downkeys; let dx = 0; let dy = 0; @@ -13316,7 +13617,7 @@ class CharacterMovement extends Part { dy *= Math.SQRT1_2; } if (dx !== 0 || dy !== 0) { - transform.move(new Vector(dx * this.speed, dy * this.speed)); + transform.move(new Vector(dx * speed, dy * speed)); } } } @@ -13379,17 +13680,20 @@ class ParallaxLayer extends Layer { } // Parts/PhysicsEngine.ts var import_matter_js = __toESM(require_matter(), 1); - class PhysicsEngine extends Part { engine; world; - constructor({ gravity }) { + gravity; + scale; + constructor({ gravity, scale }) { super({ name: "PhysicsEngine" }); + this.gravity = gravity?.toObject() || new Vector(0, 1).toObject(); + this.scale = scale || 0.001; this.engine = import_matter_js.Engine.create({ - gravity: gravity || { - x: 0, - y: 1, - scale: 0.001 + gravity: { + x: this.gravity.x, + y: this.gravity.y, + scale: this.scale } }); this.world = this.engine.world; @@ -13401,7 +13705,8 @@ class PhysicsEngine extends Part { return memo.get(this); } const clonedEngine = new PhysicsEngine({ - gravity: this.engine.gravity + gravity: new Vector(this.gravity.x, this.gravity.y), + scale: this.scale }); memo.set(this, clonedEngine); this._cloneProperties(clonedEngine, memo); @@ -13986,13 +14291,17 @@ class GravityCharacterMovement extends Part { velocity; jumpForce; facing; + waterFraction; + landFraction; constructor({ speed = 5, movementType = "WASD", input, gravityScale, maxSpeed, - jumpForce + jumpForce, + waterFraction, + landFraction }) { super({ name: "GravityCharacterMovement" }); this.speed = speed; @@ -14004,6 +14313,8 @@ class GravityCharacterMovement extends Part { this.velocity = new Vector(0, 0); this.jumpForce = jumpForce; this.facing = new Vector(1, 1); + this.waterFraction = waterFraction || 0.5; + this.landFraction = landFraction || 0.9; } getStandingGround() { const myCollider = this.siblingOf("Collider", "BoxCollider", "PolygonCollider"); @@ -14048,9 +14359,9 @@ class GravityCharacterMovement extends Part { const groundCollider = this.getStandingGround(); const onGround = !!groundCollider; const inWater = this.isInWater(); - const speedMultiplier = inWater ? 0.5 : 1; - const gravityMultiplier = inWater ? 0.5 : 1; - const jumpForceMultiplier = inWater ? 0.8 : 1; + const speedMultiplier = inWater ? this.waterFraction : onGround ? this.landFraction : 1; + const gravityMultiplier = inWater ? this.gravityScale.y : 1; + const jumpForceMultiplier = inWater ? this.jumpForce * this.waterFraction : this.jumpForce; let dx = 0; if (this.movementType === "WASD" || this.movementType === "BOTH") { if (keys.has("a")) { @@ -14165,8 +14476,7 @@ function defaults(args, defs, croak) { } return ret; } -function noop() { -} +function noop() {} function return_false() { return false; } @@ -17435,8 +17745,7 @@ var AST_Node = DEFNODE("Node", "start end", function AST_Node2(props) { walk: function(visitor) { return this._walk(visitor); }, - _children_backwards: () => { - } + _children_backwards: () => {} }, null); var AST_Statement = DEFNODE("Statement", null, function AST_Statement2(props) { if (props) { @@ -19929,8 +20238,7 @@ var AST_Undefined = DEFNODE("Undefined", null, function AST_Undefined2(props) { this.flags = 0; }, { $documentation: "The `undefined` value", - value: function() { - }() + value: function() {}() }, AST_Atom); var AST_Hole = DEFNODE("Hole", null, function AST_Hole2(props) { if (props) { @@ -19940,8 +20248,7 @@ var AST_Hole = DEFNODE("Hole", null, function AST_Hole2(props) { this.flags = 0; }, { $documentation: "A hole in an array", - value: function() { - }() + value: function() {}() }, AST_Atom); var AST_Infinity = DEFNODE("Infinity", null, function AST_Infinity2(props) { if (props) { @@ -22355,8 +22662,7 @@ function OutputStream(options) { } } options.source_map.add(mapping.token.file, mapping.line, mapping.col, mapping.token.line, mapping.token.col, is_basic_identifier_string(name) ? name : undefined); - } catch (ex) { - } + } catch (ex) {} }); mappings = []; } : noop; @@ -26640,8 +26946,7 @@ def_eval(AST_TemplateString, function() { }); def_eval(AST_Function, function(compressor) { if (compressor.option("unsafe")) { - var fn = function() { - }; + var fn = function() {}; fn.node = this; fn.toString = () => this.print_to_string(); return fn; @@ -26958,8 +27263,7 @@ def_eval(AST_Call, function(compressor, depth) { } try { return val[key].apply(val, args); - } catch (ex) { - } + } catch (ex) {} } return this; }); @@ -33685,8 +33989,7 @@ function addSegmentInternal(skipable, map, genLine, genColumn, source, sourceLin } return insert2(line, index, name ? [genColumn, sourcesIndex, sourceLine, sourceColumn, namesIndex] : [genColumn, sourcesIndex, sourceLine, sourceColumn]); } -function assert(_val) { -} +function assert(_val) {} function getIndex(arr, index) { for (let i = arr.length;i <= index; i++) { arr[i] = []; @@ -33798,8 +34101,7 @@ var SourceMapConsumer = class _SourceMapConsumer { eachMapping(callback, context) { eachMapping(this._map, context ? callback.bind(context) : callback); } - destroy() { - } + destroy() {} }; var SourceMapGenerator = class _SourceMapGenerator { constructor(opts) { @@ -42949,8 +43251,7 @@ function find_builtins(reserved) { var objects = {}; var global_ref = typeof global === "object" ? global : self; new_globals.forEach(function(new_global) { - objects[new_global] = global_ref[new_global] || function() { - }; + objects[new_global] = global_ref[new_global] || function() {}; }); [ "null", @@ -43068,8 +43369,7 @@ function mangle_private_properties(ast, options) { function find_annotated_props(ast) { var annotated_props = new Set; walk(ast, (node) => { - if (node instanceof AST_ClassPrivateProperty || node instanceof AST_PrivateMethod || node instanceof AST_PrivateGetter || node instanceof AST_PrivateSetter || node instanceof AST_DotHash) { - } else if (node instanceof AST_ObjectKeyVal) { + if (node instanceof AST_ClassPrivateProperty || node instanceof AST_PrivateMethod || node instanceof AST_PrivateGetter || node instanceof AST_PrivateSetter || node instanceof AST_DotHash) {} else if (node instanceof AST_ObjectKeyVal) { if (typeof node.key == "string" && has_annotation(node, _MANGLEPROP)) { annotated_props.add(node.key); } @@ -43128,8 +43428,7 @@ function mangle_properties(ast, options, annotated_props = find_annotated_props( cache.forEach((mangled_name) => unmangleable.add(mangled_name)); var keep_quoted = !!options.keep_quoted; ast.walk(new TreeWalker(function(node) { - if (node instanceof AST_ClassPrivateProperty || node instanceof AST_PrivateMethod || node instanceof AST_PrivateGetter || node instanceof AST_PrivateSetter || node instanceof AST_DotHash) { - } else if (node instanceof AST_ObjectKeyVal) { + if (node instanceof AST_ClassPrivateProperty || node instanceof AST_PrivateMethod || node instanceof AST_PrivateGetter || node instanceof AST_PrivateSetter || node instanceof AST_DotHash) {} else if (node instanceof AST_ObjectKeyVal) { if (typeof node.key == "string" && (!keep_quoted || !node.quote)) { add(node.key); } @@ -43162,8 +43461,7 @@ function mangle_properties(ast, options, annotated_props = find_annotated_props( } })); return ast.transform(new TreeTransformer(function(node) { - if (node instanceof AST_ClassPrivateProperty || node instanceof AST_PrivateMethod || node instanceof AST_PrivateGetter || node instanceof AST_PrivateSetter || node instanceof AST_DotHash) { - } else if (node instanceof AST_ObjectKeyVal) { + if (node instanceof AST_ClassPrivateProperty || node instanceof AST_PrivateMethod || node instanceof AST_PrivateGetter || node instanceof AST_PrivateSetter || node instanceof AST_DotHash) {} else if (node instanceof AST_ObjectKeyVal) { if (typeof node.key == "string" && (!keep_quoted || !node.quote)) { node.key = mangle(node.key); } @@ -43473,8 +43771,7 @@ function* minify_sync_or_async(files, options, _fs_module) { } if (timings) timings.rename = Date.now(); - if (0) { - } + if (0) {} if (timings) timings.compress = Date.now(); if (options.compress) { @@ -43938,8 +44235,7 @@ async function run_cli({ program, packageJson, fs, path }) { var dir = path.dirname(glob); try { var entries = fs.readdirSync(dir); - } catch (ex) { - } + } catch (ex) {} if (entries) { var pattern = "^" + path.basename(glob).replace(/[.+^$[\]\\(){}]/g, "\\$&").replace(/\*/g, "[^/\\\\]*").replace(/\?/g, "[^/\\\\]") + "$"; var mod = process.platform === "win32" ? "i" : ""; @@ -44094,7 +44390,9 @@ async function infer_options(options) { } } export { + vecEq, resetCamera, + pointInPoly, minify_sync, minify, isPointInPolygon, diff --git a/engine/editor.js b/engine/editor.js index 63add75..547820e 100644 --- a/engine/editor.js +++ b/engine/editor.js @@ -98,15 +98,16 @@ var require_PostgrestBuilder = __commonJS((exports) => { class PostgrestBuilder { constructor(builder) { + var _a, _b; this.shouldThrowOnError = false; this.method = builder.method; this.url = builder.url; - this.headers = builder.headers; + this.headers = new Headers(builder.headers); this.schema = builder.schema; this.body = builder.body; - this.shouldThrowOnError = builder.shouldThrowOnError; + this.shouldThrowOnError = (_a = builder.shouldThrowOnError) !== null && _a !== undefined ? _a : false; this.signal = builder.signal; - this.isMaybeSingle = builder.isMaybeSingle; + this.isMaybeSingle = (_b = builder.isMaybeSingle) !== null && _b !== undefined ? _b : false; if (builder.fetch) { this.fetch = builder.fetch; } else if (typeof fetch === "undefined") { @@ -120,19 +121,18 @@ var require_PostgrestBuilder = __commonJS((exports) => { return this; } setHeader(name, value) { - this.headers = Object.assign({}, this.headers); - this.headers[name] = value; + this.headers = new Headers(this.headers); + this.headers.set(name, value); return this; } then(onfulfilled, onrejected) { - if (this.schema === undefined) { - } else if (["GET", "HEAD"].includes(this.method)) { - this.headers["Accept-Profile"] = this.schema; + if (this.schema === undefined) {} else if (["GET", "HEAD"].includes(this.method)) { + this.headers.set("Accept-Profile", this.schema); } else { - this.headers["Content-Profile"] = this.schema; + this.headers.set("Content-Profile", this.schema); } if (this.method !== "GET" && this.method !== "HEAD") { - this.headers["Content-Type"] = "application/json"; + this.headers.set("Content-Type", "application/json"); } const _fetch = this.fetch; let res = _fetch(this.url.toString(), { @@ -141,7 +141,7 @@ var require_PostgrestBuilder = __commonJS((exports) => { body: JSON.stringify(this.body), signal: this.signal }).then(async (res2) => { - var _a, _b, _c; + var _a, _b, _c, _d; let error = null; let data = null; let count = null; @@ -150,17 +150,16 @@ var require_PostgrestBuilder = __commonJS((exports) => { if (res2.ok) { if (this.method !== "HEAD") { const body = await res2.text(); - if (body === "") { - } else if (this.headers["Accept"] === "text/csv") { + if (body === "") {} else if (this.headers.get("Accept") === "text/csv") { data = body; - } else if (this.headers["Accept"] && this.headers["Accept"].includes("application/vnd.pgrst.plan+text")) { + } else if (this.headers.get("Accept") && ((_a = this.headers.get("Accept")) === null || _a === undefined ? undefined : _a.includes("application/vnd.pgrst.plan+text"))) { data = body; } else { data = JSON.parse(body); } } - const countHeader = (_a = this.headers["Prefer"]) === null || _a === undefined ? undefined : _a.match(/count=(exact|planned|estimated)/); - const contentRange = (_b = res2.headers.get("content-range")) === null || _b === undefined ? undefined : _b.split("/"); + const countHeader = (_b = this.headers.get("Prefer")) === null || _b === undefined ? undefined : _b.match(/count=(exact|planned|estimated)/); + const contentRange = (_c = res2.headers.get("content-range")) === null || _c === undefined ? undefined : _c.split("/"); if (countHeader && contentRange && contentRange.length > 1) { count = parseInt(contentRange[1]); } @@ -192,7 +191,7 @@ var require_PostgrestBuilder = __commonJS((exports) => { status = 200; statusText = "OK"; } - } catch (_d) { + } catch (_e) { if (res2.status === 404 && body === "") { status = 204; statusText = "No Content"; @@ -202,7 +201,7 @@ var require_PostgrestBuilder = __commonJS((exports) => { }; } } - if (error && this.isMaybeSingle && ((_c = error === null || error === undefined ? undefined : error.details) === null || _c === undefined ? undefined : _c.includes("0 rows"))) { + if (error && this.isMaybeSingle && ((_d = error === null || error === undefined ? undefined : error.details) === null || _d === undefined ? undefined : _d.includes("0 rows"))) { error = null; status = 200; statusText = "OK"; @@ -270,10 +269,7 @@ var require_PostgrestTransformBuilder = __commonJS((exports) => { return c; }).join(""); this.url.searchParams.set("select", cleanedColumns); - if (this.headers["Prefer"]) { - this.headers["Prefer"] += ","; - } - this.headers["Prefer"] += "return=representation"; + this.headers.append("Prefer", "return=representation"); return this; } order(column, { ascending = true, nullsFirst, foreignTable, referencedTable = foreignTable } = {}) { @@ -299,24 +295,24 @@ var require_PostgrestTransformBuilder = __commonJS((exports) => { return this; } single() { - this.headers["Accept"] = "application/vnd.pgrst.object+json"; + this.headers.set("Accept", "application/vnd.pgrst.object+json"); return this; } maybeSingle() { if (this.method === "GET") { - this.headers["Accept"] = "application/json"; + this.headers.set("Accept", "application/json"); } else { - this.headers["Accept"] = "application/vnd.pgrst.object+json"; + this.headers.set("Accept", "application/vnd.pgrst.object+json"); } this.isMaybeSingle = true; return this; } csv() { - this.headers["Accept"] = "text/csv"; + this.headers.set("Accept", "text/csv"); return this; } geojson() { - this.headers["Accept"] = "application/geo+json"; + this.headers.set("Accept", "application/geo+json"); return this; } explain({ analyze = false, verbose = false, settings = false, buffers = false, wal = false, format = "text" } = {}) { @@ -328,25 +324,26 @@ var require_PostgrestTransformBuilder = __commonJS((exports) => { buffers ? "buffers" : null, wal ? "wal" : null ].filter(Boolean).join("|"); - const forMediatype = (_a = this.headers["Accept"]) !== null && _a !== undefined ? _a : "application/json"; - this.headers["Accept"] = `application/vnd.pgrst.plan+${format}; for="${forMediatype}"; options=${options};`; - if (format === "json") + const forMediatype = (_a = this.headers.get("Accept")) !== null && _a !== undefined ? _a : "application/json"; + this.headers.set("Accept", `application/vnd.pgrst.plan+${format}; for="${forMediatype}"; options=${options};`); + if (format === "json") { return this; - else + } else { return this; + } } rollback() { - var _a; - if (((_a = this.headers["Prefer"]) !== null && _a !== undefined ? _a : "").trim().length > 0) { - this.headers["Prefer"] += ",tx=rollback"; - } else { - this.headers["Prefer"] = "tx=rollback"; - } + this.headers.append("Prefer", "tx=rollback"); return this; } returns() { return this; } + maxAffected(value) { + this.headers.append("Prefer", "handling=strict"); + this.headers.append("Prefer", `max-affected=${value}`); + return this; + } } exports.default = PostgrestTransformBuilder; }); @@ -517,7 +514,7 @@ var require_PostgrestQueryBuilder = __commonJS((exports) => { class PostgrestQueryBuilder { constructor(url, { headers = {}, schema, fetch: fetch3 }) { this.url = url; - this.headers = headers; + this.headers = new Headers(headers); this.schema = schema; this.fetch = fetch3; } @@ -535,30 +532,25 @@ var require_PostgrestQueryBuilder = __commonJS((exports) => { }).join(""); this.url.searchParams.set("select", cleanedColumns); if (count) { - this.headers["Prefer"] = `count=${count}`; + this.headers.append("Prefer", `count=${count}`); } return new PostgrestFilterBuilder_1.default({ method, url: this.url, headers: this.headers, schema: this.schema, - fetch: this.fetch, - allowEmpty: false + fetch: this.fetch }); } insert(values, { count, defaultToNull = true } = {}) { + var _a; const method = "POST"; - const prefersHeaders = []; - if (this.headers["Prefer"]) { - prefersHeaders.push(this.headers["Prefer"]); - } if (count) { - prefersHeaders.push(`count=${count}`); + this.headers.append("Prefer", `count=${count}`); } if (!defaultToNull) { - prefersHeaders.push("missing=default"); + this.headers.append("Prefer", `missing=default`); } - this.headers["Prefer"] = prefersHeaders.join(","); if (Array.isArray(values)) { const columns = values.reduce((acc, x) => acc.concat(Object.keys(x)), []); if (columns.length > 0) { @@ -572,25 +564,21 @@ var require_PostgrestQueryBuilder = __commonJS((exports) => { headers: this.headers, schema: this.schema, body: values, - fetch: this.fetch, - allowEmpty: false + fetch: (_a = this.fetch) !== null && _a !== undefined ? _a : fetch }); } upsert(values, { onConflict, ignoreDuplicates = false, count, defaultToNull = true } = {}) { + var _a; const method = "POST"; - const prefersHeaders = [`resolution=${ignoreDuplicates ? "ignore" : "merge"}-duplicates`]; + this.headers.append("Prefer", `resolution=${ignoreDuplicates ? "ignore" : "merge"}-duplicates`); if (onConflict !== undefined) this.url.searchParams.set("on_conflict", onConflict); - if (this.headers["Prefer"]) { - prefersHeaders.push(this.headers["Prefer"]); - } if (count) { - prefersHeaders.push(`count=${count}`); + this.headers.append("Prefer", `count=${count}`); } if (!defaultToNull) { - prefersHeaders.push("missing=default"); + this.headers.append("Prefer", "missing=default"); } - this.headers["Prefer"] = prefersHeaders.join(","); if (Array.isArray(values)) { const columns = values.reduce((acc, x) => acc.concat(Object.keys(x)), []); if (columns.length > 0) { @@ -604,68 +592,42 @@ var require_PostgrestQueryBuilder = __commonJS((exports) => { headers: this.headers, schema: this.schema, body: values, - fetch: this.fetch, - allowEmpty: false + fetch: (_a = this.fetch) !== null && _a !== undefined ? _a : fetch }); } update(values, { count } = {}) { + var _a; const method = "PATCH"; - const prefersHeaders = []; - if (this.headers["Prefer"]) { - prefersHeaders.push(this.headers["Prefer"]); - } if (count) { - prefersHeaders.push(`count=${count}`); + this.headers.append("Prefer", `count=${count}`); } - this.headers["Prefer"] = prefersHeaders.join(","); return new PostgrestFilterBuilder_1.default({ method, url: this.url, headers: this.headers, schema: this.schema, body: values, - fetch: this.fetch, - allowEmpty: false + fetch: (_a = this.fetch) !== null && _a !== undefined ? _a : fetch }); } delete({ count } = {}) { + var _a; const method = "DELETE"; - const prefersHeaders = []; if (count) { - prefersHeaders.push(`count=${count}`); - } - if (this.headers["Prefer"]) { - prefersHeaders.unshift(this.headers["Prefer"]); + this.headers.append("Prefer", `count=${count}`); } - this.headers["Prefer"] = prefersHeaders.join(","); return new PostgrestFilterBuilder_1.default({ method, url: this.url, headers: this.headers, schema: this.schema, - fetch: this.fetch, - allowEmpty: false + fetch: (_a = this.fetch) !== null && _a !== undefined ? _a : fetch }); } } exports.default = PostgrestQueryBuilder; }); -// node_modules/@supabase/postgrest-js/dist/cjs/version.js -var require_version = __commonJS((exports) => { - Object.defineProperty(exports, "__esModule", { value: true }); - exports.version = undefined; - exports.version = "0.0.0-automated"; -}); - -// node_modules/@supabase/postgrest-js/dist/cjs/constants.js -var require_constants = __commonJS((exports) => { - Object.defineProperty(exports, "__esModule", { value: true }); - exports.DEFAULT_HEADERS = undefined; - var version_1 = require_version(); - exports.DEFAULT_HEADERS = { "X-Client-Info": `postgrest-js/${version_1.version}` }; -}); - // node_modules/@supabase/postgrest-js/dist/cjs/PostgrestClient.js var require_PostgrestClient = __commonJS((exports) => { var __importDefault = exports && exports.__importDefault || function(mod) { @@ -674,19 +636,18 @@ var require_PostgrestClient = __commonJS((exports) => { Object.defineProperty(exports, "__esModule", { value: true }); var PostgrestQueryBuilder_1 = __importDefault(require_PostgrestQueryBuilder()); var PostgrestFilterBuilder_1 = __importDefault(require_PostgrestFilterBuilder()); - var constants_1 = require_constants(); class PostgrestClient { constructor(url, { headers = {}, schema, fetch: fetch3 } = {}) { this.url = url; - this.headers = Object.assign(Object.assign({}, constants_1.DEFAULT_HEADERS), headers); + this.headers = new Headers(headers); this.schemaName = schema; this.fetch = fetch3; } from(relation) { const url = new URL(`${this.url}/${relation}`); return new PostgrestQueryBuilder_1.default(url, { - headers: Object.assign({}, this.headers), + headers: new Headers(this.headers), schema: this.schemaName, fetch: this.fetch }); @@ -699,6 +660,7 @@ var require_PostgrestClient = __commonJS((exports) => { }); } rpc(fn, args = {}, { head = false, get = false, count } = {}) { + var _a; let method; const url = new URL(`${this.url}/rpc/${fn}`); let body; @@ -711,9 +673,9 @@ var require_PostgrestClient = __commonJS((exports) => { method = "POST"; body = args; } - const headers = Object.assign({}, this.headers); + const headers = new Headers(this.headers); if (count) { - headers["Prefer"] = `count=${count}`; + headers.set("Prefer", `count=${count}`); } return new PostgrestFilterBuilder_1.default({ method, @@ -721,8 +683,7 @@ var require_PostgrestClient = __commonJS((exports) => { headers, schema: this.schemaName, body, - fetch: this.fetch, - allowEmpty: false + fetch: (_a = this.fetch) !== null && _a !== undefined ? _a : fetch }); } } @@ -3938,8 +3899,7 @@ var require_showdown = __commonJS((exports, module) => { for (i = 0;i < rawCells.length; ++i) { var row = []; for (var ii = 0;ii < headers.length; ++ii) { - if (showdown.helper.isUndefined(rawCells[i][ii])) { - } + if (showdown.helper.isUndefined(rawCells[i][ii])) {} row.push(parseCells(rawCells[i][ii], styles[ii])); } cells.push(row); @@ -4391,7 +4351,9 @@ var require_showdown = __commonJS((exports, module) => { // engine/bundle.js var exports_bundle = {}; __export(exports_bundle, { + vecEq: () => vecEq, resetCamera: () => resetCamera, + pointInPoly: () => pointInPoly, minify_sync: () => minify_sync, minify: () => minify, isPointInPolygon: () => isPointInPolygon, @@ -4597,6 +4559,24 @@ function isPointInObject(mouseX, mouseY, child) { } return false; } +function vecEq(a, b) { + return a.x === b.x && a.y === b.y; +} +function pointInPoly(point, poly) { + let inside = false; + for (let i = 0, j = poly.length - 1;i < poly.length; j = i++) { + const xi = poly[i].x, yi = poly[i].y; + const xj = poly[j].x, yj = poly[j].y; + const onSegment = (point.y - yi) * (xj - xi) === (point.x - xi) * (yj - yi) && (Math.min(xi, xj) <= point.x && point.x <= Math.max(xi, xj)) && (Math.min(yi, yj) <= point.y && point.y <= Math.max(yi, yj)); + if (onSegment) { + return true; + } + const intersect = yi > point.y !== yj > point.y && point.x < (xj - xi) * (point.y - yi) / (yj - yi) + xi; + if (intersect) + inside = !inside; + } + return inside; +} class Part { id; @@ -4702,8 +4682,7 @@ class Part { onMount(parent) { this.parent = parent; } - onRegister(attribute, value) { - } + onRegister(attribute, value) {} onUnregister(attribute, value, debug) { if (debug) console.log(debug, value.name); @@ -4726,8 +4705,7 @@ class Part { break; } } - onUnmount() { - } + onUnmount() {} onStart() { this.childrenArray.forEach((child) => { if (typeof child.onStart === "function") { @@ -5026,8 +5004,7 @@ class SoundManagerController { static instance; sounds = []; isGameRunning = false; - constructor() { - } + constructor() {} static getInstance() { if (!SoundManagerController.instance) { SoundManagerController.instance = new SoundManagerController; @@ -5068,6 +5045,58 @@ class SoundManagerController { } } +class SpatialGrid { + cells; + cellSize; + constructor(cellSize) { + this.cells = new Map; + this.cellSize = cellSize; + } + getKey(x, y) { + return `${Math.floor(x / this.cellSize)}_${Math.floor(y / this.cellSize)}`; + } + clear() { + this.cells.clear(); + } + insert(collider) { + const start = collider.realWorldStart; + const end = collider.realWorldEnd; + const startX = Math.floor(start.x / this.cellSize); + const startY = Math.floor(start.y / this.cellSize); + const endX = Math.floor(end.x / this.cellSize); + const endY = Math.floor(end.y / this.cellSize); + for (let x = startX;x <= endX; x++) { + for (let y = startY;y <= endY; y++) { + const key = `${x}_${y}`; + if (!this.cells.has(key)) { + this.cells.set(key, []); + } + this.cells.get(key).push(collider); + } + } + } + query(collider) { + const candidates = new Set; + const start = collider.realWorldStart; + const end = collider.realWorldEnd; + const startX = Math.floor(start.x / this.cellSize); + const startY = Math.floor(start.y / this.cellSize); + const endX = Math.floor(end.x / this.cellSize); + const endY = Math.floor(end.y / this.cellSize); + for (let x = startX;x <= endX; x++) { + for (let y = startY;y <= endY; y++) { + const key = `${x}_${y}`; + if (this.cells.has(key)) { + for (const other of this.cells.get(key)) { + candidates.add(other); + } + } + } + } + return Array.from(candidates); + } +} + class Vector { x; y; @@ -5126,8 +5155,9 @@ class Vector { } normalize() { const len = this.length(); - if (len === 0) - throw new Error("Cannot normalize zero-length vector"); + if (len === 0) { + return new Vector(0, 0); + } return new Vector(this.x / len, this.y / len); } dot(other) { @@ -5154,7 +5184,11 @@ class Vector { return this; } static From(scalar) { - return new Vector(scalar, scalar); + if (typeof scalar === "number") { + return new Vector(scalar, scalar); + } else { + return new Vector(scalar.x, scalar.y); + } } } function characters(str) { @@ -5191,8 +5225,7 @@ function defaults(args, defs, croak) { } return ret; } -function noop3() { -} +function noop3() {} function return_false() { return false; } @@ -8690,8 +8723,7 @@ function OutputStream(options) { } } options.source_map.add(mapping.token.file, mapping.line, mapping.col, mapping.token.line, mapping.token.col, is_basic_identifier_string(name) ? name : undefined); - } catch (ex) { - } + } catch (ex) {} }); mappings = []; } : noop3; @@ -12209,8 +12241,7 @@ function addSegmentInternal(skipable, map, genLine, genColumn, source, sourceLin } return insert2(line, index2, name ? [genColumn, sourcesIndex, sourceLine, sourceColumn, namesIndex] : [genColumn, sourcesIndex, sourceLine, sourceColumn]); } -function assert(_val) { -} +function assert(_val) {} function getIndex(arr, index2) { for (let i = arr.length;i <= index2; i++) { arr[i] = []; @@ -12361,8 +12392,7 @@ function find_builtins(reserved) { var objects = {}; var global_ref = typeof global === "object" ? global : self; new_globals.forEach(function(new_global) { - objects[new_global] = global_ref[new_global] || function() { - }; + objects[new_global] = global_ref[new_global] || function() {}; }); [ "null", @@ -12480,8 +12510,7 @@ function mangle_private_properties(ast, options) { function find_annotated_props(ast) { var annotated_props = new Set; walk(ast, (node) => { - if (node instanceof AST_ClassPrivateProperty || node instanceof AST_PrivateMethod || node instanceof AST_PrivateGetter || node instanceof AST_PrivateSetter || node instanceof AST_DotHash) { - } else if (node instanceof AST_ObjectKeyVal) { + if (node instanceof AST_ClassPrivateProperty || node instanceof AST_PrivateMethod || node instanceof AST_PrivateGetter || node instanceof AST_PrivateSetter || node instanceof AST_DotHash) {} else if (node instanceof AST_ObjectKeyVal) { if (typeof node.key == "string" && has_annotation(node, _MANGLEPROP)) { annotated_props.add(node.key); } @@ -12540,8 +12569,7 @@ function mangle_properties(ast, options, annotated_props = find_annotated_props( cache.forEach((mangled_name) => unmangleable.add(mangled_name)); var keep_quoted = !!options.keep_quoted; ast.walk(new TreeWalker(function(node) { - if (node instanceof AST_ClassPrivateProperty || node instanceof AST_PrivateMethod || node instanceof AST_PrivateGetter || node instanceof AST_PrivateSetter || node instanceof AST_DotHash) { - } else if (node instanceof AST_ObjectKeyVal) { + if (node instanceof AST_ClassPrivateProperty || node instanceof AST_PrivateMethod || node instanceof AST_PrivateGetter || node instanceof AST_PrivateSetter || node instanceof AST_DotHash) {} else if (node instanceof AST_ObjectKeyVal) { if (typeof node.key == "string" && (!keep_quoted || !node.quote)) { add(node.key); } @@ -12574,8 +12602,7 @@ function mangle_properties(ast, options, annotated_props = find_annotated_props( } })); return ast.transform(new TreeTransformer(function(node) { - if (node instanceof AST_ClassPrivateProperty || node instanceof AST_PrivateMethod || node instanceof AST_PrivateGetter || node instanceof AST_PrivateSetter || node instanceof AST_DotHash) { - } else if (node instanceof AST_ObjectKeyVal) { + if (node instanceof AST_ClassPrivateProperty || node instanceof AST_PrivateMethod || node instanceof AST_PrivateGetter || node instanceof AST_PrivateSetter || node instanceof AST_DotHash) {} else if (node instanceof AST_ObjectKeyVal) { if (typeof node.key == "string" && (!keep_quoted || !node.quote)) { node.key = mangle(node.key); } @@ -12881,8 +12908,7 @@ function* minify_sync_or_async(files, options, _fs_module) { } if (timings) timings.rename = Date.now(); - if (0) { - } + if (0) {} if (timings) timings.compress = Date.now(); if (options.compress) { @@ -13345,8 +13371,7 @@ async function run_cli({ program, packageJson, fs, path }) { var dir = path.dirname(glob); try { var entries = fs.readdirSync(dir); - } catch (ex) { - } + } catch (ex) {} if (entries) { var pattern = "^" + path.basename(glob).replace(/[.+^$[\]\\(){}]/g, "\\$&").replace(/\*/g, "[^/\\\\]*").replace(/\?/g, "[^/\\\\]") + "$"; var mod = process.platform === "win32" ? "i" : ""; @@ -13508,7 +13533,7 @@ var __create2, __getProtoOf2, __defProp2, __getOwnPropNames2, __hasOwnProp2, __t enumerable: true }); return to; -}, __commonJS2 = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports), require_matter, require_resolve_uri_umd, require_acorn, Scene, SoundManager, Game, Layer, GameObject, Camera, Input, Renderer, AnimatedSprite, Collider, PolygonCollider, BoxCollider, Button, ColorRender, SpriteRender, TextRender, Transform, Sound, Health, Timer2, Spawner, Follow, CharacterMovement, ParallaxLayer, import_matter_js, PhysicsEngine, Rotator, Scaler, Projectile, AreaTrigger, Particle, ParticleEmitter, WaypointFollower, CameraShake, HealthBar, import_matter_js2, PhysicsBody, GravityCharacterMovement, DefaultsError, MAP, lineTerminatorEscape, re_safe_regexp, regexp_is_safe = (source) => re_safe_regexp.test(source), all_flags = "dgimsuyv", LATEST_RAW = "", TEMPLATE_RAWS, KEYWORDS = "break case catch class const continue debugger default delete do else export extends finally for function if in instanceof let new return switch throw try typeof var void while with", KEYWORDS_ATOM = "false null true", RESERVED_WORDS, ALL_RESERVED_WORDS, KEYWORDS_BEFORE_EXPRESSION = "return new delete throw else case yield await", OPERATOR_CHARS, RE_HEX_NUMBER, RE_OCT_NUMBER, RE_ES6_OCT_NUMBER, RE_BIN_NUMBER, RE_DEC_NUMBER, RE_BIG_INT, OPERATORS, WHITESPACE_CHARS, NEWLINE_CHARS, PUNC_AFTER_EXPRESSION, PUNC_BEFORE_EXPRESSION, PUNC_CHARS, UNICODE, BASIC_IDENT, JS_Parse_Error, EX_EOF, UNARY_PREFIX, UNARY_POSTFIX, ASSIGNMENT, LOGICAL_ASSIGNMENT, PRECEDENCE, ATOMIC_START_TOKEN, has_tok_flag = (tok, flag) => Boolean(tok.flags & flag), set_tok_flag = (tok, flag, truth) => { +}, __commonJS2 = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports), require_matter, require_resolve_uri_umd, require_acorn, Scene, SoundManager, Game, Layer, GameObject, Camera, Input, Renderer, AnimatedSprite, Collider, MultiPolygonCollider, PolygonCollider, BoxCollider, Button, ColorRender, SpriteRender, TextRender, Transform, Sound, Health, Timer2, Spawner, Follow, CharacterMovement, ParallaxLayer, import_matter_js, PhysicsEngine, Rotator, Scaler, Projectile, AreaTrigger, Particle, ParticleEmitter, WaypointFollower, CameraShake, HealthBar, import_matter_js2, PhysicsBody, GravityCharacterMovement, DefaultsError, MAP, lineTerminatorEscape, re_safe_regexp, regexp_is_safe = (source) => re_safe_regexp.test(source), all_flags = "dgimsuyv", LATEST_RAW = "", TEMPLATE_RAWS, KEYWORDS = "break case catch class const continue debugger default delete do else export extends finally for function if in instanceof let new return switch throw try typeof var void while with", KEYWORDS_ATOM = "false null true", RESERVED_WORDS, ALL_RESERVED_WORDS, KEYWORDS_BEFORE_EXPRESSION = "return new delete throw else case yield await", OPERATOR_CHARS, RE_HEX_NUMBER, RE_OCT_NUMBER, RE_ES6_OCT_NUMBER, RE_BIN_NUMBER, RE_DEC_NUMBER, RE_BIG_INT, OPERATORS, WHITESPACE_CHARS, NEWLINE_CHARS, PUNC_AFTER_EXPRESSION, PUNC_BEFORE_EXPRESSION, PUNC_CHARS, UNICODE, BASIC_IDENT, JS_Parse_Error, EX_EOF, UNARY_PREFIX, UNARY_POSTFIX, ASSIGNMENT, LOGICAL_ASSIGNMENT, PRECEDENCE, ATOMIC_START_TOKEN, has_tok_flag = (tok, flag) => Boolean(tok.flags & flag), set_tok_flag = (tok, flag, truth) => { if (truth) { tok.flags |= flag; } else { @@ -13704,8 +13729,7 @@ var __create2, __getProtoOf2, __defProp2, __getOwnPropNames2, __hasOwnProp2, __t eachMapping(callback, context) { eachMapping(this._map, context ? callback.bind(context) : callback); } - destroy() { - } + destroy() {} }, SourceMapGenerator = class _SourceMapGenerator { constructor(opts) { this._map = opts instanceof GenMapping ? opts : new GenMapping(opts); @@ -20658,8 +20682,7 @@ Defaulting to 2020, but this will stop working in the future.`); this.context.push(statementParens ? types3.p_stat : types3.p_expr); this.exprAllowed = true; }; - types$1.incDec.updateContext = function() { - }; + types$1.incDec.updateContext = function() {}; types$1._function.updateContext = types$1._class.updateContext = function(prevType) { if (prevType.beforeExpr && prevType !== types$1._else && !(prevType === types$1.semi && this.curContext() !== types3.p_stat) && !(prevType === types$1._return && lineBreak.test(this.input.slice(this.lastTokEnd, this.start))) && !((prevType === types$1.colon || prevType === types$1.braceL) && this.curContext() === types3.b_stat)) { this.context.push(types3.f_expr); @@ -22165,8 +22188,7 @@ Defaulting to 2020, but this will stop working in the future.`); } }; pp$1.regexp_alternative = function(state2) { - while (state2.pos < state2.source.length && this.regexp_eatTerm(state2)) { - } + while (state2.pos < state2.source.length && this.regexp_eatTerm(state2)) {} }; pp$1.regexp_eatTerm = function(state2) { if (this.regexp_eatAssertion(state2)) { @@ -23479,8 +23501,7 @@ Defaulting to 2020, but this will stop working in the future.`); var value = null; try { value = new RegExp(pattern, flags); - } catch (e) { - } + } catch (e) {} return this.finishToken(types$1.regexp, { pattern, flags, value }); }; pp.readInt = function(radix, len, maybeLegacyOctalNumericLiteral) { @@ -23968,10 +23989,7 @@ Defaulting to 2020, but this will stop working in the future.`); childrenArray; devmode; context; - showtoolTips = false; hovering; - tooltipLocked; - lastMousePosition = { x: 0, y: 0 }; scaleFactor = 1; canvasOffset = { x: 0, y: 0 }; messageHook; @@ -23988,9 +24006,8 @@ Defaulting to 2020, but this will stop working in the future.`); _animationFrameId; _lastUpdateTime = 0; constructor({ name, canvas, devmode = false, width, height, disableAntiAliasing = false, showtoolTips = false, showFrameStats = "BASIC" }) { - super(); - this.name = name; - this.showtoolTips = showtoolTips; + super({ name }); + this.type = "Game"; this.childrenArray = []; this.showFrameStats = showFrameStats; this.canvas = typeof canvas === "string" ? document.getElementById(canvas) : canvas; @@ -23999,30 +24016,7 @@ Defaulting to 2020, but this will stop working in the future.`); this.changeCanvasSize(width, height); this.context.imageSmoothingEnabled = !disableAntiAliasing; this.debugEmoji = "\uD83C\uDFAE"; - this.tooltipLocked = false; this.top = this; - if (this.devmode) { - let tooltip2 = document.getElementById("debug-tooltip"); - if (!tooltip2) { - tooltip2 = this.createDebugTooltip(); - } - document.addEventListener("mousemove", (event) => { - const rect = this.canvas.getBoundingClientRect(); - const clientX = event.clientX - rect.left; - const clientY = event.clientY - rect.top; - this.lastMousePosition = { - x: clientX / this.scaleFactor - this.canvasOffset.x / this.scaleFactor, - y: clientY / this.scaleFactor - this.canvasOffset.y / this.scaleFactor - }; - }); - document.addEventListener("click", (event) => { - if (tooltip2 && !this.tooltipLocked) { - this.tooltipLocked = true; - } else if (tooltip2) { - this.tooltipLocked = false; - } - }); - } } clone(memo = new Map) { if (memo.has(this)) { @@ -24034,8 +24028,7 @@ Defaulting to 2020, but this will stop working in the future.`); devmode: this.devmode, width: this.width, height: this.height, - disableAntiAliasing: !this.context.imageSmoothingEnabled, - showtoolTips: this.showtoolTips + disableAntiAliasing: !this.context.imageSmoothingEnabled }); memo.set(this, clonedGame); this._cloneProperties(clonedGame, memo); @@ -24043,8 +24036,6 @@ Defaulting to 2020, but this will stop working in the future.`); clonedGame.context = undefined; clonedGame.currentScene = undefined; clonedGame.hovering = undefined; - clonedGame.tooltipLocked = undefined; - clonedGame.lastMousePosition = { x: 0, y: 0 }; clonedGame.scaleFactor = 1; clonedGame.canvasOffset = { x: 0, y: 0 }; clonedGame.messageHook = undefined; @@ -24074,20 +24065,6 @@ Defaulting to 2020, but this will stop working in the future.`); get height() { return this._height; } - createDebugTooltip() { - const tooltip2 = document.createElement("div"); - tooltip2.id = "debug-tooltip"; - tooltip2.style.position = "absolute"; - tooltip2.style.backgroundColor = "rgba(0, 0, 0, 0.7)"; - tooltip2.style.color = "white"; - tooltip2.style.padding = "5px"; - tooltip2.style.display = "none"; - tooltip2.style.borderRadius = "5px"; - tooltip2.style.pointerEvents = "none"; - tooltip2.style.zIndex = "1000"; - document.body.appendChild(tooltip2); - return tooltip2; - } addChild(scene) { this.currentScene = this.currentScene || scene; scene.setTop(this); @@ -24155,18 +24132,24 @@ Defaulting to 2020, but this will stop working in the future.`); this._animationFrameId = window.requestAnimationFrame(this.loop.bind(this)); } } - getColliderCount() { + getColliderCount(activeOnly = false) { const layers = this.currentScene?.childrenArray || []; let c = 0; for (const layer of layers) { - const colliders = layer.flats.colliders.length; - c += colliders; + if (!activeOnly) { + const colliders = layer.flats.colliders.length; + c += colliders; + } else { + const colliders = layer.flats.colliders.filter((col) => col.active).length; + c += colliders; + } } return c; } renderFrameStats() { if (!this.showFrameStats) return; + const FADE_BACKGROUND = 0.5; const avgDelta = this.frameBuffer.reduce((a, b) => a + b, 0) / this.frameBuffer.length; const avgFPS = 1000 / avgDelta; const sorted = [...this.frameBuffer].sort((a, b) => a - b); @@ -24174,56 +24157,82 @@ Defaulting to 2020, but this will stop working in the future.`); const p99 = sorted[Math.floor(sorted.length * 0.99)]; const minFrameTime = sorted[0]; const maxFrameTime = sorted[sorted.length - 1]; - this.context.fillStyle = "white"; - this.context.font = "12px Arial"; - let y = 20; + let lines = []; const levels = ["BASIC", "EXTENDED", "ADVANCED", "PERFORMANCE_HUD"]; const levelIndex = levels.indexOf(this.showFrameStats); - if (levelIndex >= 0) { - this.context.fillText(`FPS: ${avgFPS.toFixed(2)}`, 10, y); - y += 20; - } - if (levelIndex >= 1) { - this.context.fillText(`Frame Time: ${avgDelta.toFixed(2)} ms`, 10, y); - y += 20; - } + if (levelIndex >= 0) + lines.push(`FPS: ${avgFPS.toFixed(2)}`); + if (levelIndex >= 1) + lines.push(`Frame Time: ${avgDelta.toFixed(2)} ms`); if (levelIndex >= 2) { - this.context.fillText(`Min: ${minFrameTime.toFixed(2)} (${this._minFrameTime.toFixed(2)} AT) ms`, 10, y); - y += 20; - this.context.fillText(`Max: ${maxFrameTime.toFixed(2)} (${this._maxFrameTime.toFixed(2)} AT) ms`, 10, y); - y += 20; + lines.push(`Min: ${minFrameTime.toFixed(2)} (${this._minFrameTime.toFixed(2)} AT) ms`); + lines.push(`Max: ${maxFrameTime.toFixed(2)} (${this._maxFrameTime.toFixed(2)} AT) ms`); } if (levelIndex >= 3) { - this.context.fillText(`p95 Frame: ${p95.toFixed(2)} ms`, 10, y); - y += 20; - this.context.fillText(`p99 Frame: ${p99.toFixed(2)} ms`, 10, y); - y += 20; + lines.push(`p95 Frame: ${p95.toFixed(2)} ms`); + lines.push(`p99 Frame: ${p99.toFixed(2)} ms`); const droppedPct = this._droppedFrames / (this.frameBuffer.length || 1) * 100; - this.context.fillText(`Dropped Frames: ${droppedPct.toFixed(1)}%`, 10, y); - y += 20; + lines.push(`Dropped Frames: ${droppedPct.toFixed(1)}%`); const perfMem = performance.memory; if (perfMem) { const usedMB = (perfMem.usedJSHeapSize / 1048576).toFixed(1); const totalMB = (perfMem.totalJSHeapSize / 1048576).toFixed(1); - this.context.fillText(`Heap: ${usedMB} MB / ${totalMB} MB`, 10, y); - y += 20; + lines.push(`Heap: ${usedMB} MB / ${totalMB} MB`); } if (this.currentScene) { - this.context.fillText(`Colliders: ${this.getColliderCount()}`, 10, y); - y += 20; - } + lines.push(`Colliders: ${this.getColliderCount()}`); + lines.push(`Active colliders: ${this.getColliderCount(true)}`); + } + } + const fontSize = 12; + const lineHeight = 20; + const padding = 8; + this.context.font = `${fontSize}px Arial`; + let maxWidth = 0; + for (const line of lines) { + const width = this.context.measureText(line).width; + if (width > maxWidth) + maxWidth = width; + } + let boxHeight = lines.length * lineHeight + padding * 2; + let boxWidth = maxWidth + padding * 2; + let boxX = 6; + let boxY = 6; + this.context.globalAlpha = FADE_BACKGROUND; + this.context.fillStyle = "#000"; + this.context.fillRect(boxX, boxY, boxWidth, boxHeight); + this.context.globalAlpha = 1; + this.context.fillStyle = "white"; + let y = boxY + padding + fontSize; + for (const line of lines) { + this.context.fillText(line, boxX + padding, y); + y += lineHeight; + } + if (levelIndex >= 3) { const chartWidth = 200; const chartHeight = 80; - const chartX = 10; - const chartY = y + 10; - const maxFrameTime2 = Math.max(...this.frameBuffer); + const chartX = boxX + padding; + const chartY = boxY + boxHeight + 10; + const minFrameTimeChart = Math.min(...this.frameBuffer); + const maxFrameTimeChart = Math.max(...this.frameBuffer); + const margin = Math.max(2, (maxFrameTimeChart - minFrameTimeChart) * 0.2); + const chartMin = Math.max(0, minFrameTimeChart - margin); + const chartMax = maxFrameTimeChart + margin; + const range = Math.max(1, chartMax - chartMin); + this.context.globalAlpha = FADE_BACKGROUND; + this.context.fillStyle = "#000"; + this.context.fillRect(chartX - padding, chartY - padding, chartWidth + padding * 2, chartHeight + padding * 2); + this.context.globalAlpha = 1; this.context.strokeStyle = "white"; this.context.beginPath(); - this.context.moveTo(chartX, chartY + chartHeight); this.frameBuffer.forEach((frameTime, index2) => { - const x = chartX + index2 / this.maxFrameBufferLength * chartWidth; - const yVal = chartY + chartHeight - frameTime / maxFrameTime2 * chartHeight; - this.context.lineTo(x, yVal); + const x = chartX + index2 / (this.maxFrameBufferLength - 1) * chartWidth; + const yVal = chartY + chartHeight - (frameTime - chartMin) / range * chartHeight; + if (index2 === 0) { + this.context.moveTo(x, yVal); + } else { + this.context.lineTo(x, yVal); + } }); this.context.stroke(); } @@ -24316,34 +24325,15 @@ Defaulting to 2020, but this will stop working in the future.`); return false; } } - updateDebugToolTip() { - const tooltip2 = document.getElementById("debug-tooltip"); - if (!tooltip2) { - this.warn("Debug tooltip not found. Ensure it is created in devmode."); - return; - } - if (this.hovering) { - if (tooltip2 && this.showtoolTips) { - try { - tooltip2.style.left = `${this.lastMousePosition.x * this.scaleFactor + this.canvasOffset.x + 10}px`; - tooltip2.style.top = `${this.lastMousePosition.y * this.scaleFactor + this.canvasOffset.y + 10}px`; - tooltip2.style.display = "block"; - tooltip2.innerHTML = getDebugInfo(this.hovering, 0); - } catch (err) { - throw new Error(`Error updating debug tooltip: ${err}`); - } - } - } else { - tooltip2.style.display = "none"; - } - } }; Layer = class Layer extends Part { - constructor({ name }) { - super(); - this.name = name; + spatialGrid; + constructor({ name, spatialGridDefinition }) { + super({ name }); + this.type = "Layer"; this.id = generateUID(); this.debugEmoji = "\uD83D\uDDC2️"; + this.spatialGrid = new SpatialGrid(spatialGridDefinition || 100); } addChild(part) { part.setAll("layer", this); @@ -24360,6 +24350,13 @@ Defaulting to 2020, but this will stop working in the future.`); if (!this.ready) { return; } + this.spatialGrid.clear(); + const colliders = this.flats.colliders; + for (const collider of colliders) { + if (collider.active) { + this.spatialGrid.insert(collider); + } + } this.ties.forEach((tie) => { if (tie.target && tie.target.hasOwnProperty(tie.targetAttribute)) { const value = this.attr(tie.localAttribute); @@ -24373,9 +24370,9 @@ Defaulting to 2020, but this will stop working in the future.`); }; GameObject = class GameObject extends Part { layer; - constructor({ name, render }) { - super({ name, render }); - this.name = name; + constructor({ name, render = true }) { + super({ name, render: !!render }); + this.type = "GameObject"; this.debugEmoji = "\uD83D\uDD79️"; } }; @@ -24453,14 +24450,10 @@ Defaulting to 2020, but this will stop working in the future.`); return memo.get(this); } const clonedInput = new Input({ - key: this.key || (() => { - }), - keyup: this.keyup || (() => { - }), - mousemove: this.mousemove || (() => { - }), - click: this.click || (() => { - }) + key: this.key || (() => {}), + keyup: this.keyup || (() => {}), + mousemove: this.mousemove || (() => {}), + click: this.click || (() => {}) }); memo.set(this, clonedInput); this._cloneProperties(clonedInput, memo); @@ -24979,14 +24972,23 @@ Defaulting to 2020, but this will stop working in the future.`); realWorldStart; realWorldEnd; vertices; - constructor({ tag }) { + active = true; + allowMerge; + randomTestingColors; + constructor({ tag, allowMerge }) { super({ name: "Collider" }); this.type = "Collider"; this.base = "Collider"; this.tag = tag || ""; this.radius = 0; this.realWorldStart = new Vector(0, 0); + this.allowMerge = allowMerge !== undefined ? allowMerge : true; this.realWorldEnd = new Vector(0, 0); + this.randomTestingColors = [ + Math.random() * 255, + Math.random() * 255, + Math.random() * 255 + ]; this.vertices = []; } setTag(tag) { @@ -25007,8 +25009,62 @@ Defaulting to 2020, but this will stop working in the future.`); value.flats.colliders.push(this); } } + evaluateMerging() { + const layer = this.registrations["layer"]; + if (!layer) + return; + const candidates = layer.spatialGrid.query(this); + const fellowColliders = candidates.filter((c) => c.tag == this.tag && c.id !== this.id && c.allowMerge && c.active); + if (fellowColliders.length == 0) + return; + for (const fellow of fellowColliders) { + if (!fellow.sibling("Transform")?.initialized) + continue; + if (this.id < fellow.id && this.checkCollision(fellow, true)) { + this.mergeWith(fellow); + } + } + } + mergeWith(other) { + if (this.tag !== other.tag || other.tag == "" || this.tag == "") + return; + const thisTransform = this.sibling("Transform"); + if (!thisTransform) { + this.top?.warn(`Collider <${this.name}> has no Transform sibling, cannot merge.`); + return; + } + const allPolygons = []; + const g1 = this.getGeometry(); + if (this.type === "MultiPolygonCollider") { + allPolygons.push(...g1); + } else { + allPolygons.push(g1); + } + const g2 = other.getGeometry(); + if (other.type === "MultiPolygonCollider") { + allPolygons.push(...g2); + } else { + allPolygons.push(g2); + } + if (allPolygons.length === 0) + return; + const localPolygons = allPolygons.map((polygon) => { + return polygon.map(([x, y]) => thisTransform.worldToLocal(new Vector(x, y))); + }); + this._updateVerticesAfterMerge(localPolygons); + other.inactivate(); + } + onStart() {} + inactivate() { + this.active = false; + } + activate() { + this.active = true; + } act(delta) { super.act(delta); + if (!this.active) + return; if (!this.registrations?.layer) { throw new Error(`Collider <${this.name}> (${this.id}) is not registered to a layer. Collisions will not be checked.`); } @@ -25019,8 +25075,8 @@ Defaulting to 2020, but this will stop working in the future.`); this.colliding = false; this.collidingWith.clear(); const layer = this.registrations.layer; - const colliders = layer.flats.colliders; - for (const other of colliders) { + const candidates = layer.spatialGrid.query(this); + for (const other of candidates) { if (other === this) continue; if (this.checkCollision(other)) { @@ -25029,15 +25085,36 @@ Defaulting to 2020, but this will stop working in the future.`); } } this.hoverbug = `${this.colliding ? "\uD83D\uDFE5" : "\uD83D\uDFE9"} - ${Array.from(this.collidingWith).map((o) => o.name).join(", ")} objects`; + const fill = this.active; + const ctx = this.top instanceof Game ? this.top.context : null; + if (ctx) { + ctx.beginPath(); + ctx.strokeStyle = `rgb(${this.randomTestingColors[0]}, ${this.randomTestingColors[1]}, ${this.randomTestingColors[2]})`; + ctx.fillStyle = fill ? `rgba(${this.randomTestingColors[0]}, ${this.randomTestingColors[1]}, ${this.randomTestingColors[2]}, 0.5)` : "transparent"; + ctx.moveTo(this.worldVertices[0].x, this.worldVertices[0].y); + for (const vertex of this.worldVertices) { + ctx.lineTo(vertex.x, vertex.y); + } + ctx.closePath(); + ctx.stroke(); + ctx.fill(); + } if (this.top instanceof Game && this.top.devmode) { - const ctx = this.top.context; - if (ctx) { - this.drawDebug(ctx); + const ctx2 = this.top.context; + if (ctx2) { + this.drawDebug(ctx2); } } } - checkCollision(other) { - if (other.tag === this.tag && this.tag !== "") + checkCollision(other, ignoreTags = false) { + const thisTransform = this.sibling("Transform"); + const otherTransform = other.sibling("Transform"); + if (!thisTransform || !otherTransform) { + return false; + } + this.updateCollider(thisTransform); + other.updateCollider(otherTransform); + if (!ignoreTags && other.tag === this.tag && this.tag !== "") return false; if (this.realWorldEnd.x < other.realWorldStart.x || this.realWorldStart.x > other.realWorldEnd.x || this.realWorldEnd.y < other.realWorldStart.y || this.realWorldStart.y > other.realWorldEnd.y) { return false; @@ -25069,9 +25146,15 @@ Defaulting to 2020, but this will stop working in the future.`); } checkVerticesAgainstVertices(vertices1, vertices2) { const axes1 = this.getAxes(vertices1); + for (const axis of axes1) { + const projection1 = this.project(vertices1, axis); + const projection2 = this.project(vertices2, axis); + if (!this.overlap(projection1, projection2)) { + return false; + } + } const axes2 = this.getAxes(vertices2); - const axes = axes1.concat(axes2); - for (const axis of axes) { + for (const axis of axes2) { const projection1 = this.project(vertices1, axis); const projection2 = this.project(vertices2, axis); if (!this.overlap(projection1, projection2)) { @@ -25107,12 +25190,155 @@ Defaulting to 2020, but this will stop working in the future.`); overlap(proj1, proj2) { return proj1.max >= proj2.min && proj2.max >= proj1.min; } + _checkPolygonVsPolygon(vertices1, vertices2) { + const axes1 = this.getAxes(vertices1); + const axes2 = this.getAxes(vertices2); + const axes = axes1.concat(axes2); + for (const axis of axes) { + const projection1 = this.project(vertices1, axis); + const projection2 = this.project(vertices2, axis); + if (!this.overlap(projection1, projection2)) { + return false; + } + } + return true; + } + }; + MultiPolygonCollider = class MultiPolygonCollider extends Collider { + polygons; + _worldPolygons = []; + unioned = []; + constructor({ polygons, tag = "" }) { + super({ tag, allowMerge: tag !== "" }); + this.name = "MultiPolygonCollider"; + this.polygons = polygons; + this.type = "MultiPolygonCollider"; + let maxDist = 0; + const allVertices = polygons.flat(); + for (let i = 0;i < allVertices.length; i++) { + for (let j = i + 1;j < allVertices.length; j++) { + const dist = allVertices[i].distance(allVertices[j]); + if (dist > maxDist) { + maxDist = dist; + } + } + } + this.radius = maxDist; + } + getGeometry() { + return this._worldPolygons.map((polygon) => { + return polygon.map((v) => v.toArray()); + }); + } + get worldVertices() { + const allVertices = this._worldPolygons.flat(); + allVertices.sort((a, b) => { + return a.x < b.x || a.x == b.x && a.y < b.y ? -1 : 1; + }); + const cross = (o, a, b) => { + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); + }; + const lower = []; + for (const p of allVertices) { + while (lower.length >= 2 && cross(lower[lower.length - 2], lower[lower.length - 1], p) <= 0) { + lower.pop(); + } + lower.push(p); + } + const upper = []; + for (let i = allVertices.length - 1;i >= 0; i--) { + const p = allVertices[i]; + while (upper.length >= 2 && cross(upper[upper.length - 2], upper[upper.length - 1], p) <= 0) { + upper.pop(); + } + upper.push(p); + } + upper.pop(); + lower.pop(); + return lower.concat(upper); + } + act(delta) { + super.act(delta); + } + _updateVerticesAfterMerge(polygons) { + this.polygons = polygons; + } + updateCollider(transform) { + const position = transform.worldPosition; + const rotation = transform.rotation; + const scale = transform.scale; + this._worldPolygons = this.polygons.map((polygon) => { + return polygon.map((vertex) => { + let scaledVertex = vertex.multiply(scale); + if (rotation !== 0) { + const cos = Math.cos(rotation); + const sin = Math.sin(rotation); + scaledVertex = new Vector(scaledVertex.x * cos - scaledVertex.y * sin, scaledVertex.x * sin + scaledVertex.y * cos); + } + return position.add(scaledVertex); + }); + }); + const allWorldVertices = this._worldPolygons.flat(); + const xs = allWorldVertices.map((v) => v.x); + const ys = allWorldVertices.map((v) => v.y); + this.realWorldStart.set(Math.min(...xs), Math.min(...ys)); + this.realWorldEnd.set(Math.max(...xs), Math.max(...ys)); + } + narrowPhaseCheck(other) { + if (other instanceof MultiPolygonCollider) { + for (const p1 of this._worldPolygons) { + for (const p2 of other._worldPolygons) { + if (this._checkPolygonVsPolygon(p1, p2)) { + return true; + } + } + } + return false; + } + for (const polygon of this._worldPolygons) { + if (this._checkPolygonVsPolygon(polygon, other.worldVertices)) { + return true; + } + } + return false; + } + drawDebug(ctx) { + ctx.save(); + ctx.strokeStyle = this.colliding ? "rgba(255, 0, 100, 0.8)" : "rgba(0, 255, 100, 0.8)"; + ctx.lineWidth = 1; + for (const polygon of this._worldPolygons) { + ctx.beginPath(); + ctx.moveTo(polygon[0].x, polygon[0].y); + for (let i = 1;i < polygon.length; i++) { + ctx.lineTo(polygon[i].x, polygon[i].y); + } + ctx.closePath(); + ctx.stroke(); + } + ctx.restore(); + } + clone(memo = new Map) { + if (memo.has(this)) { + return memo.get(this); + } + const clonedMultiPolygonCollider = new MultiPolygonCollider({ + polygons: this.polygons.map((p) => p.map((v) => v.clone())), + tag: this.tag + }); + memo.set(this, clonedMultiPolygonCollider); + this._cloneProperties(clonedMultiPolygonCollider, memo); + clonedMultiPolygonCollider.colliding = false; + clonedMultiPolygonCollider.base = this.base; + clonedMultiPolygonCollider.type = this.type; + clonedMultiPolygonCollider.collidingWith = new Set; + return clonedMultiPolygonCollider; + } }; PolygonCollider = class PolygonCollider extends Collider { localVertices; _worldVertices = []; - constructor({ vertices, tag }) { - super({ tag }); + constructor({ vertices, tag = "" }) { + super({ tag, allowMerge: tag !== "" }); this.name = "PolygonCollider"; this.localVertices = vertices; this.vertices = vertices; @@ -25131,9 +25357,22 @@ Defaulting to 2020, but this will stop working in the future.`); get worldVertices() { return this._worldVertices; } + getGeometry() { + return [this.worldVertices.map((v) => v.toArray())]; + } act(delta) { super.act(delta); } + _updateVerticesAfterMerge(polygons) { + const newCollider = new MultiPolygonCollider({ polygons, tag: this.tag }); + newCollider.active = this.active; + newCollider.allowMerge = this.allowMerge; + const parent = this.parent; + if (parent) { + parent.removeChild(this); + parent.addChild(newCollider); + } + } updateCollider(transform) { const position = transform.worldPosition; const rotation = transform.rotation; @@ -25157,22 +25396,14 @@ Defaulting to 2020, but this will stop working in the future.`); return this.checkPolygonVsBox(this, other); } else if (other instanceof PolygonCollider) { return this.checkPolygonVsPolygon(this, other); + } else if (other instanceof MultiPolygonCollider) { + return other.narrowPhaseCheck(this); } this.top?.warn("Collision checks are only supported between BoxColliders and PolygonColliders."); return false; } checkPolygonVsPolygon(poly1, poly2) { - const axes1 = this.getAxes(poly1.worldVertices); - const axes2 = this.getAxes(poly2.worldVertices); - const axes = axes1.concat(axes2); - for (const axis of axes) { - const projection1 = this.project(poly1.worldVertices, axis); - const projection2 = this.project(poly2.worldVertices, axis); - if (!this.overlap(projection1, projection2)) { - return false; - } - } - return true; + return this._checkPolygonVsPolygon(poly1.worldVertices, poly2.worldVertices); } checkPolygonVsBox(poly, box) { const boxVertices = box.worldVertices; @@ -25227,8 +25458,8 @@ Defaulting to 2020, but this will stop working in the future.`); cachedAxes = []; lastRotation = NaN; lastScale = new Vector(NaN, NaN); - constructor({ width, height, tag }) { - super({ tag }); + constructor({ width, height, tag = "" }) { + super({ tag, allowMerge: tag !== "" }); this.name = "BoxCollider"; this.width = width; this.height = height; @@ -25244,6 +25475,9 @@ Defaulting to 2020, but this will stop working in the future.`); get worldVertices() { return this.rotatedCorners; } + getGeometry() { + return [this.worldVertices.map((v) => v.toArray())]; + } updateCollider(transform) { const cos = Math.cos(transform.rotation); const sin = Math.sin(transform.rotation); @@ -25282,7 +25516,7 @@ Defaulting to 2020, but this will stop working in the future.`); narrowPhaseCheck(other) { if (other instanceof BoxCollider) { return this.checkBoxVsBox(this, other); - } else if (other instanceof PolygonCollider) { + } else if (other instanceof PolygonCollider || other instanceof MultiPolygonCollider) { return other.narrowPhaseCheck(this); } this.top?.warn(`Collision with unsupported collider type: ${other.type}`); @@ -25298,6 +25532,16 @@ Defaulting to 2020, but this will stop working in the future.`); } return true; } + _updateVerticesAfterMerge(polygons) { + const newCollider = new MultiPolygonCollider({ polygons, tag: this.tag }); + newCollider.active = this.active; + newCollider.allowMerge = this.allowMerge; + const parent = this.parent; + if (parent) { + parent.removeChild(this); + parent.addChild(newCollider); + } + } act(delta) { super.act(delta); } @@ -25485,14 +25729,10 @@ Defaulting to 2020, but this will stop working in the future.`); if (scene) { if (!scene.child("Input")) { const input = new Input({ - key: () => { - }, - keyup: () => { - }, - mousemove: () => { - }, - click: () => { - } + key: () => {}, + keyup: () => {}, + mousemove: () => {}, + click: () => {} }); scene.addChild(input); } @@ -25618,9 +25858,6 @@ Defaulting to 2020, but this will stop working in the future.`); if (!this.top) { throw new Error(`ColorRender <${this.parent?.name}.${this.name}> is not attached to a top-level parent. Ensure it is added to a Game instance or Scene before rendering.`); } - if (!(this.top instanceof Game)) { - throw new Error(`ColorRender <${this.parent?.name}.${this.name}> is not attached to a Game instance. Ensure it is added to a Game, Scene, or Layer with a game ancestor.`); - } const transform = this.sibling("Transform"); if (!transform) { this.top?.warn(`ColorRender <${this.parent?.name}.${this.name}> does not have a Transform sibling. Skipping rendering.`); @@ -25793,6 +26030,7 @@ Defaulting to 2020, but this will stop working in the future.`); worldPosition; rotation; scale; + initialized; constructor({ position, rotation, scale } = {}) { super({ name: "Transform" }); this.position = position || Vector.From(0); @@ -25802,15 +26040,11 @@ Defaulting to 2020, but this will stop working in the future.`); this.scale = scale || new Vector(1, 1); this.debugEmoji = "\uD83D\uDCD0"; this.type = "Transform"; + this.initialized = false; } onMount(parent) { super.onMount(parent); - const grandparentTransform = parent.sibling("Transform"); - if (grandparentTransform) { - this.worldPosition.set(this.position.add(grandparentTransform.worldPosition)); - } else { - this.worldPosition.set(this.position); - } + this.updateWorldPosition(); if (parent.superficialWidth && parent.superficialHeight) { this.superficialWidth = parent.superficialWidth; this.superficialHeight = parent.superficialHeight; @@ -25833,16 +26067,32 @@ Defaulting to 2020, but this will stop working in the future.`); this.rotation = rotation % (2 * Math.PI); this.updateWorldPosition(); } + worldToLocal(position) { + const translated = position.subtract(this.worldPosition); + const cos = Math.cos(-this.rotation); + const sin = Math.sin(-this.rotation); + const rotated = new Vector(translated.x * cos - translated.y * sin, translated.x * sin + translated.y * cos); + const scaled = new Vector(rotated.x / this.scale.x, rotated.y / this.scale.y); + return scaled; + } + preFrame() { + super.preFrame(); + this.updateWorldPosition(); + } updateWorldPosition() { - const parentTransform = this.parent?.sibling("Transform"); + const parentTransform = this.parent?.parent?.child("Transform"); if (parentTransform) { - this.worldPosition.set(this.position.add(parentTransform.worldPosition)); + const scaledPosition = this.position.multiply(parentTransform.scale); + const cos = Math.cos(parentTransform.rotation); + const sin = Math.sin(parentTransform.rotation); + const rotatedPosition = new Vector(scaledPosition.x * cos - scaledPosition.y * sin, scaledPosition.x * sin + scaledPosition.y * cos); + this.worldPosition.set(rotatedPosition.add(parentTransform.worldPosition)); } else { this.worldPosition.set(this.position); } + this.initialized = true; } act(_delta) { - this.updateWorldPosition(); this.hoverbug = `${this.position.toString()} | ${this.worldPosition.toString()} | ${(this.rotation / Math.PI).toFixed(2)}pi | ${this.scale.toString()}`; } }; @@ -26025,8 +26275,7 @@ Defaulting to 2020, but this will stop working in the future.`); currentHealth; onDeath; isDead = false; - constructor({ maxHealth = 100, onDeath = () => { - } }) { + constructor({ maxHealth = 100, onDeath = () => {} }) { super({ name: "Health" }); this.maxHealth = maxHealth; this.currentHealth = maxHealth; @@ -26235,7 +26484,7 @@ Defaulting to 2020, but this will stop working in the future.`); this.input = input; this.type = "CharacterMovement"; } - act(_delta) { + act(delta) { if (!this.input) { if (!this.warned.has("MissingInput")) this.top?.warn(`CharacterMovement <${this.name}> (${this.id}) is missing an input property. Please create an input on the scene and pass it.`) && this.warned.add("MissingInput"); @@ -26245,6 +26494,7 @@ Defaulting to 2020, but this will stop working in the future.`); if (!transform) { return; } + const speed = this.speed * delta; const keys = this.input.downkeys; let dx = 0; let dy = 0; @@ -26281,7 +26531,7 @@ Defaulting to 2020, but this will stop working in the future.`); dy *= Math.SQRT1_2; } if (dx !== 0 || dy !== 0) { - transform.move(new Vector(dx * this.speed, dy * this.speed)); + transform.move(new Vector(dx * speed, dy * speed)); } } }; @@ -26345,13 +26595,17 @@ Defaulting to 2020, but this will stop working in the future.`); PhysicsEngine = class PhysicsEngine extends Part { engine; world; - constructor({ gravity }) { + gravity; + scale; + constructor({ gravity, scale }) { super({ name: "PhysicsEngine" }); + this.gravity = gravity?.toObject() || new Vector(0, 1).toObject(); + this.scale = scale || 0.001; this.engine = import_matter_js.Engine.create({ - gravity: gravity || { - x: 0, - y: 1, - scale: 0.001 + gravity: { + x: this.gravity.x, + y: this.gravity.y, + scale: this.scale } }); this.world = this.engine.world; @@ -26363,7 +26617,8 @@ Defaulting to 2020, but this will stop working in the future.`); return memo.get(this); } const clonedEngine = new PhysicsEngine({ - gravity: this.engine.gravity + gravity: new Vector(this.gravity.x, this.gravity.y), + scale: this.scale }); memo.set(this, clonedEngine); this._cloneProperties(clonedEngine, memo); @@ -26936,13 +27191,17 @@ Defaulting to 2020, but this will stop working in the future.`); velocity; jumpForce; facing; + waterFraction; + landFraction; constructor({ speed = 5, movementType = "WASD", input, gravityScale, maxSpeed, - jumpForce + jumpForce, + waterFraction, + landFraction }) { super({ name: "GravityCharacterMovement" }); this.speed = speed; @@ -26954,6 +27213,8 @@ Defaulting to 2020, but this will stop working in the future.`); this.velocity = new Vector(0, 0); this.jumpForce = jumpForce; this.facing = new Vector(1, 1); + this.waterFraction = waterFraction || 0.5; + this.landFraction = landFraction || 0.9; } getStandingGround() { const myCollider = this.siblingOf("Collider", "BoxCollider", "PolygonCollider"); @@ -26998,9 +27259,9 @@ Defaulting to 2020, but this will stop working in the future.`); const groundCollider = this.getStandingGround(); const onGround = !!groundCollider; const inWater = this.isInWater(); - const speedMultiplier = inWater ? 0.5 : 1; - const gravityMultiplier = inWater ? 0.5 : 1; - const jumpForceMultiplier = inWater ? 0.8 : 1; + const speedMultiplier = inWater ? this.waterFraction : onGround ? this.landFraction : 1; + const gravityMultiplier = inWater ? this.gravityScale.y : 1; + const jumpForceMultiplier = inWater ? this.jumpForce * this.waterFraction : this.jumpForce; let dx = 0; if (this.movementType === "WASD" || this.movementType === "BOTH") { if (keys.has("a")) { @@ -27308,8 +27569,7 @@ Defaulting to 2020, but this will stop working in the future.`); walk: function(visitor) { return this._walk(visitor); }, - _children_backwards: () => { - } + _children_backwards: () => {} }, null); AST_Statement = DEFNODE("Statement", null, function AST_Statement2(props) { if (props) { @@ -29789,8 +30049,7 @@ Defaulting to 2020, but this will stop working in the future.`); this.flags = 0; }, { $documentation: "The `undefined` value", - value: function() { - }() + value: function() {}() }, AST_Atom); AST_Hole = DEFNODE("Hole", null, function AST_Hole2(props) { if (props) { @@ -29800,8 +30059,7 @@ Defaulting to 2020, but this will stop working in the future.`); this.flags = 0; }, { $documentation: "A hole in an array", - value: function() { - }() + value: function() {}() }, AST_Atom); AST_Infinity = DEFNODE("Infinity", null, function AST_Infinity2(props) { if (props) { @@ -35080,8 +35338,7 @@ Defaulting to 2020, but this will stop working in the future.`); }); def_eval(AST_Function, function(compressor) { if (compressor.option("unsafe")) { - var fn = function() { - }; + var fn = function() {}; fn.node = this; fn.toString = () => this.print_to_string(); return fn; @@ -35397,8 +35654,7 @@ Defaulting to 2020, but this will stop working in the future.`); } try { return val[key].apply(val, args); - } catch (ex) { - } + } catch (ex) {} } return this; }); @@ -49624,6 +49880,7 @@ var nodeDefinitions = { width: { type: "number", default: 800, description: "Width of the game canvas in pixels." }, height: { type: "number", default: 600, description: "Height of the game canvas in pixels." }, devmode: { type: "boolean", default: true, description: "Enable developer mode." }, + showtoolTips: { type: "boolean", default: false, description: "Show tooltips in devmode." }, disableAntiAliasing: { type: "boolean", default: false, description: "Disable anti-aliasing for every object." }, starterScene: { type: "Part", subType: "Scene", description: "The scene that will be loaded when the game starts." }, showFrameStats: { type: "enum", default: "BASIC", options: ["BASIC", "EXTENDED", "ADVANCED", "PERFORMANCE_HUD"], description: "Show frame statistics." } @@ -49638,7 +49895,8 @@ var nodeDefinitions = { }, Layer: { properties: { - name: { type: "text", default: "NewLayer", description: "The name of the layer." } + name: { type: "text", default: "NewLayer", description: "The name of the layer." }, + spatialGridDefinition: { type: "number", default: 100, description: "Cell size for the spatial grid used for broad-phase collision detection." } }, children: ["GameObject"] }, @@ -49647,7 +49905,7 @@ var nodeDefinitions = { name: { type: "text", default: "NewGameObject", description: "The name of the game object." }, render: { type: "boolean", default: true, description: "Whether this GameObject should be rendered. If false, no child Parts will be ran." } }, - children: ["Transform", "BoxCollider", "PolygonCollider", "ColorRender", "SpriteRender", "AnimatedSprite", "TextRender", "Button", "Sound", "Health", "Timer", "Spawner", "Follow", "CharacterMovement", "PhysicsEngine", "Rotator", "Scaler", "Projectile", "AreaTrigger", "ParticleEmitter", "WaypointFollower", "CameraShake", "HealthBar", "PhysicsBody", "GravityCharacterMovement"] + children: ["Transform", "BoxCollider", "PolygonCollider", "MultiPolygonCollider", "ColorRender", "SpriteRender", "AnimatedSprite", "TextRender", "Button", "Sound", "Health", "Timer", "Spawner", "Follow", "CharacterMovement", "PhysicsEngine", "Rotator", "Scaler", "Projectile", "AreaTrigger", "ParticleEmitter", "WaypointFollower", "CameraShake", "HealthBar", "PhysicsBody", "GravityCharacterMovement"] }, Camera: { properties: { @@ -49690,12 +49948,21 @@ var nodeDefinitions = { }, singular: true }, + MultiPolygonCollider: { + properties: { + name: { type: "text", default: "MultiPolygonCollider", description: "The name of the multi-polygon collider." }, + tag: { type: "text", default: "", description: "The tag of the collider." }, + polygons: { type: "list", subType: "list", tertiaryType: "Vector", default: [], description: "List of polygons (each a list of vectors)." } + }, + singular: true + }, ColorRender: { properties: { name: { type: "text", default: "ColorRender", description: "The name of the color renderer." }, width: { type: "number", default: 50, description: "Width of the color renderer." }, height: { type: "number", default: 50, description: "Height of the color renderer." }, - color: { type: "color", default: "#FF0000", description: "Color of the renderer." } + color: { type: "color", default: "#FF0000", description: "Color of the renderer." }, + vertices: { type: "list", subType: "Vector", default: [], description: "Vertices of a polygon to render. If not provided, a rectangle is rendered." } }, singular: true }, @@ -49720,7 +49987,9 @@ var nodeDefinitions = { startingAnimation: { type: "text", default: "", description: "Starting animation name." }, disableAntiAliasing: { type: "boolean", default: false, description: "Disable anti-aliasing for this sprite." }, facing: { type: "Vector", default: "new Vector(1, 1)", description: "Direction to face. Use -1 to flip." }, - webEngine: { type: "boolean", default: true, description: "Set to true if this is running in a web engine context.", dontShow: true } + webEngine: { type: "boolean", default: true, description: "Set to true if this is running in a web engine context.", dontShow: true }, + loop: { type: "boolean", default: true, description: "Override spritesheet data and loop immediately." }, + bounce: { type: "boolean", default: false, description: "Override spritesheet data and bounce immediately." } }, singular: true }, @@ -49818,7 +50087,8 @@ var nodeDefinitions = { PhysicsEngine: { properties: { name: { type: "text", default: "PhysicsEngine", description: "The name of the physics engine." }, - gravity: { type: "Vector", default: "new Vector(0, 1)", description: "Gravity vector applied to all physics bodies." } + gravity: { type: "Vector", default: "new Vector(0, 1)", description: "Gravity vector applied to all physics bodies." }, + scale: { type: "number", default: 0.001, description: "Scale for the physics engine." } }, singular: true }, @@ -49911,7 +50181,9 @@ var nodeDefinitions = { input: { type: "Part", subType: "Input", description: "The input component for the scene, used to track player input." }, gravityScale: { type: "Vector", default: "new Vector(0, 0.5)", description: "Gravity scale applied to the character." }, maxSpeed: { type: "number", default: 10, description: "Maximum speed of the character." }, - jumpForce: { type: "number", default: 10, description: "Jump force applied to the character." } + jumpForce: { type: "number", default: 10, description: "Jump force applied to the character." }, + waterFraction: { type: "number", default: 0.5, description: "Fraction of normal speed and jump force when in water (0-1)." }, + landFraction: { type: "number", default: 1, description: "Fraction of normal speed and jump force when on land (0-1)." } }, singular: true } @@ -50147,30 +50419,96 @@ var { PostgrestError } = import_cjs.default; -// node_modules/isows/_esm/utils.js -function getNativeWebSocket() { - if (typeof WebSocket !== "undefined") - return WebSocket; - if (typeof global.WebSocket !== "undefined") - return global.WebSocket; - if (typeof window.WebSocket !== "undefined") - return window.WebSocket; - if (typeof self.WebSocket !== "undefined") - return self.WebSocket; - throw new Error("`WebSocket` is not supported in this environment"); -} +// node_modules/@supabase/realtime-js/dist/module/lib/websocket-factory.js +class WebSocketFactory { + static detectEnvironment() { + var _a; + if (typeof WebSocket !== "undefined") { + return { type: "native", constructor: WebSocket }; + } + if (typeof globalThis !== "undefined" && typeof globalThis.WebSocket !== "undefined") { + return { type: "native", constructor: globalThis.WebSocket }; + } + if (typeof global !== "undefined" && typeof global.WebSocket !== "undefined") { + return { type: "native", constructor: global.WebSocket }; + } + if (typeof globalThis !== "undefined" && typeof globalThis.WebSocketPair !== "undefined" && typeof globalThis.WebSocket === "undefined") { + return { + type: "cloudflare", + error: "Cloudflare Workers detected. WebSocket clients are not supported in Cloudflare Workers.", + workaround: "Use Cloudflare Workers WebSocket API for server-side WebSocket handling, or deploy to a different runtime." + }; + } + if (typeof globalThis !== "undefined" && globalThis.EdgeRuntime || typeof navigator !== "undefined" && ((_a = navigator.userAgent) === null || _a === undefined ? undefined : _a.includes("Vercel-Edge"))) { + return { + type: "unsupported", + error: "Edge runtime detected (Vercel Edge/Netlify Edge). WebSockets are not supported in edge functions.", + workaround: "Use serverless functions or a different deployment target for WebSocket functionality." + }; + } + if (typeof process !== "undefined" && process.versions && process.versions.node) { + const nodeVersion = parseInt(process.versions.node.split(".")[0]); + if (nodeVersion >= 22) { + if (typeof globalThis.WebSocket !== "undefined") { + return { type: "native", constructor: globalThis.WebSocket }; + } + return { + type: "unsupported", + error: `Node.js ${nodeVersion} detected but native WebSocket not found.`, + workaround: "Provide a WebSocket implementation via the transport option." + }; + } + return { + type: "unsupported", + error: `Node.js ${nodeVersion} detected without native WebSocket support.`, + workaround: `For Node.js < 22, install "ws" package and provide it via the transport option: +` + `import ws from "ws" +` + "new RealtimeClient(url, { transport: ws })" + }; + } + return { + type: "unsupported", + error: "Unknown JavaScript runtime without WebSocket support.", + workaround: "Ensure you're running in a supported environment (browser, Node.js, Deno) or provide a custom WebSocket implementation." + }; + } + static getWebSocketConstructor() { + const env = this.detectEnvironment(); + if (env.constructor) { + return env.constructor; + } + let errorMessage = env.error || "WebSocket not supported in this environment."; + if (env.workaround) { + errorMessage += ` -// node_modules/isows/_esm/native.js -var WebSocket2 = getNativeWebSocket(); +Suggested solution: ${env.workaround}`; + } + throw new Error(errorMessage); + } + static createWebSocket(url, protocols) { + const WS = this.getWebSocketConstructor(); + return new WS(url, protocols); + } + static isWebSocketSupported() { + try { + const env = this.detectEnvironment(); + return env.type === "native" || env.type === "ws"; + } catch (_a) { + return false; + } + } +} +var websocket_factory_default = WebSocketFactory; // node_modules/@supabase/realtime-js/dist/module/lib/version.js -var version = "2.11.15"; +var version = "2.15.4"; // node_modules/@supabase/realtime-js/dist/module/lib/constants.js var DEFAULT_VERSION = `realtime-js/${version}`; var VSN = "1.0.0"; var DEFAULT_TIMEOUT = 1e4; var WS_CLOSE_NORMAL = 1000; +var MAX_PUSH_BUFFER_SIZE = 100; var SOCKET_STATES; (function(SOCKET_STATES2) { SOCKET_STATES2[SOCKET_STATES2["connecting"] = 0] = "connecting"; @@ -50252,6 +50590,7 @@ class Timer { reset() { this.tries = 0; clearTimeout(this.timer); + this.timer = undefined; } scheduleTimeout() { clearTimeout(this.timer); @@ -50408,7 +50747,7 @@ var httpEndpointURL = (socketUrl) => { let url = socketUrl; url = url.replace(/^ws/i, "http"); url = url.replace(/(\/socket\/websocket|\/socket|\/websocket)\/?$/i, ""); - return url.replace(/\/+$/, ""); + return url.replace(/\/+$/, "") + "/api/broadcast"; }; // node_modules/@supabase/realtime-js/dist/module/lib/push.js @@ -50516,13 +50855,11 @@ class RealtimePresence { this.state = {}; this.pendingDiffs = []; this.joinRef = null; + this.enabled = false; this.caller = { - onJoin: () => { - }, - onLeave: () => { - }, - onSync: () => { - } + onJoin: () => {}, + onLeave: () => {}, + onSync: () => {} }; const events = (opts === null || opts === undefined ? undefined : opts.events) || { state: "presence_state", @@ -50602,12 +50939,10 @@ class RealtimePresence { leaves: this.transformState(diff.leaves) }; if (!onJoin) { - onJoin = () => { - }; + onJoin = () => {}; } if (!onLeave) { - onLeave = () => { - }; + onLeave = () => {}; } this.map(joins, (key, newPresences) => { var _a; @@ -50704,7 +51039,7 @@ class RealtimeChannel { this.subTopic = topic.replace(/^realtime:/i, ""); this.params.config = Object.assign({ broadcast: { ack: false, self: false }, - presence: { key: "" }, + presence: { key: "", enabled: false }, private: false }, params.config); this.timeout = this.socket.timeout; @@ -50738,39 +51073,49 @@ class RealtimeChannel { this.state = CHANNEL_STATES.errored; this.rejoinTimer.scheduleTimeout(); }); + this.joinPush.receive("error", (reason) => { + if (this._isLeaving() || this._isClosed()) { + return; + } + this.socket.log("channel", `error ${this.topic}`, reason); + this.state = CHANNEL_STATES.errored; + this.rejoinTimer.scheduleTimeout(); + }); this._on(CHANNEL_EVENTS.reply, {}, (payload, ref) => { this._trigger(this._replyEventName(ref), payload); }); this.presence = new RealtimePresence(this); - this.broadcastEndpointURL = httpEndpointURL(this.socket.endPoint) + "/api/broadcast"; + this.broadcastEndpointURL = httpEndpointURL(this.socket.endPoint); this.private = this.params.config.private || false; } subscribe(callback, timeout = this.timeout) { - var _a, _b; + var _a, _b, _c; if (!this.socket.isConnected()) { this.socket.connect(); } if (this.state == CHANNEL_STATES.closed) { const { config: { broadcast, presence, private: isPrivate } } = this.params; - this._onError((e) => callback === null || callback === undefined ? undefined : callback(REALTIME_SUBSCRIBE_STATES.CHANNEL_ERROR, e)); - this._onClose(() => callback === null || callback === undefined ? undefined : callback(REALTIME_SUBSCRIBE_STATES.CLOSED)); + const postgres_changes = (_b = (_a = this.bindings.postgres_changes) === null || _a === undefined ? undefined : _a.map((r) => r.filter)) !== null && _b !== undefined ? _b : []; + const presence_enabled = !!this.bindings[REALTIME_LISTEN_TYPES.PRESENCE] && this.bindings[REALTIME_LISTEN_TYPES.PRESENCE].length > 0 || ((_c = this.params.config.presence) === null || _c === undefined ? undefined : _c.enabled) === true; const accessTokenPayload = {}; const config = { broadcast, - presence, - postgres_changes: (_b = (_a = this.bindings.postgres_changes) === null || _a === undefined ? undefined : _a.map((r) => r.filter)) !== null && _b !== undefined ? _b : [], + presence: Object.assign(Object.assign({}, presence), { enabled: presence_enabled }), + postgres_changes, private: isPrivate }; if (this.socket.accessTokenValue) { accessTokenPayload.access_token = this.socket.accessTokenValue; } + this._onError((e) => callback === null || callback === undefined ? undefined : callback(REALTIME_SUBSCRIBE_STATES.CHANNEL_ERROR, e)); + this._onClose(() => callback === null || callback === undefined ? undefined : callback(REALTIME_SUBSCRIBE_STATES.CLOSED)); this.updateJoinPayload(Object.assign({ config }, accessTokenPayload)); this.joinedOnce = true; this._rejoin(timeout); - this.joinPush.receive("ok", async ({ postgres_changes }) => { + this.joinPush.receive("ok", async ({ postgres_changes: postgres_changes2 }) => { var _a2; this.socket.setAuth(); - if (postgres_changes === undefined) { + if (postgres_changes2 === undefined) { callback === null || callback === undefined || callback(REALTIME_SUBSCRIBE_STATES.SUBSCRIBED); return; } else { @@ -50780,7 +51125,7 @@ class RealtimeChannel { for (let i = 0;i < bindingsLen; i++) { const clientPostgresBinding = clientPostgresBindings[i]; const { filter: { event, schema, table, filter } } = clientPostgresBinding; - const serverPostgresFilter = postgres_changes && postgres_changes[i]; + const serverPostgresFilter = postgres_changes2 && postgres_changes2[i]; if (serverPostgresFilter && serverPostgresFilter.event === event && serverPostgresFilter.schema === schema && serverPostgresFilter.table === table && serverPostgresFilter.filter === filter) { newPostgresBindings.push(Object.assign(Object.assign({}, clientPostgresBinding), { id: serverPostgresFilter.id })); } else { @@ -50822,6 +51167,10 @@ class RealtimeChannel { }, opts); } on(type, filter, callback) { + if (this.state === CHANNEL_STATES.joined && type === REALTIME_LISTEN_TYPES.PRESENCE) { + this.socket.log("channel", `resubscribe to ${this.topic} due to change in presence callbacks on joined channel`); + this.unsubscribe().then(() => this.subscribe()); + } return this._on(type, filter, callback); } async send(args, opts = {}) { @@ -50903,8 +51252,11 @@ class RealtimeChannel { } teardown() { this.pushBuffer.forEach((push) => push.destroy()); - this.rejoinTimer && clearTimeout(this.rejoinTimer.timer); + this.pushBuffer = []; + this.rejoinTimer.reset(); this.joinPush.destroy(); + this.state = CHANNEL_STATES.closed; + this.bindings = {}; } async _fetchWithTimeout(url, options, timeout) { const controller = new AbortController; @@ -50921,11 +51273,21 @@ class RealtimeChannel { if (this._canPush()) { pushEvent.send(); } else { - pushEvent.startTimeout(); - this.pushBuffer.push(pushEvent); + this._addToPushBuffer(pushEvent); } return pushEvent; } + _addToPushBuffer(pushEvent) { + pushEvent.startTimeout(); + this.pushBuffer.push(pushEvent); + if (this.pushBuffer.length > MAX_PUSH_BUFFER_SIZE) { + const removedPush = this.pushBuffer.shift(); + if (removedPush) { + removedPush.destroy(); + this.socket.log("channel", `discarded push due to buffer overflow: ${removedPush.event}`, removedPush.payload); + } + } + } _onMessage(_event, payload, _ref) { return payload; } @@ -51017,10 +51379,12 @@ class RealtimeChannel { } _off(type, filter) { const typeLower = type.toLocaleLowerCase(); - this.bindings[typeLower] = this.bindings[typeLower].filter((bind) => { - var _a; - return !(((_a = bind.type) === null || _a === undefined ? undefined : _a.toLocaleLowerCase()) === typeLower && RealtimeChannel.isEqual(bind.filter, filter)); - }); + if (this.bindings[typeLower]) { + this.bindings[typeLower] = this.bindings[typeLower].filter((bind) => { + var _a; + return !(((_a = bind.type) === null || _a === undefined ? undefined : _a.toLocaleLowerCase()) === typeLower && RealtimeChannel.isEqual(bind.filter, filter)); + }); + } return this; } static isEqual(obj1, obj2) { @@ -51073,8 +51437,14 @@ class RealtimeChannel { } // node_modules/@supabase/realtime-js/dist/module/RealtimeClient.js -var noop2 = () => { +var noop2 = () => {}; +var CONNECTION_TIMEOUTS = { + HEARTBEAT_INTERVAL: 25000, + RECONNECT_DELAY: 10, + HEARTBEAT_TIMEOUT_FALLBACK: 100 }; +var RECONNECT_INTERVALS = [1000, 2000, 5000, 1e4]; +var DEFAULT_RECONNECT_FALLBACK = 1e4; var WORKER_SCRIPT = ` addEventListener("message", (e) => { if (e.data.event === "start") { @@ -51093,11 +51463,13 @@ class RealtimeClient { this.headers = {}; this.params = {}; this.timeout = DEFAULT_TIMEOUT; - this.heartbeatIntervalMs = 25000; + this.transport = null; + this.heartbeatIntervalMs = CONNECTION_TIMEOUTS.HEARTBEAT_INTERVAL; this.heartbeatTimer = undefined; this.pendingHeartbeatRef = null; this.heartbeatCallback = noop2; this.ref = 0; + this.reconnectTimer = null; this.logger = noop2; this.conn = null; this.sendBuffer = []; @@ -51109,91 +51481,91 @@ class RealtimeClient { message: [] }; this.accessToken = null; + this._connectionState = "disconnected"; + this._wasManualDisconnect = false; + this._authPromise = null; this._resolveFetch = (customFetch) => { let _fetch; if (customFetch) { _fetch = customFetch; } else if (typeof fetch === "undefined") { - _fetch = (...args) => Promise.resolve().then(() => (init_browser(), exports_browser)).then(({ default: fetch3 }) => fetch3(...args)); + _fetch = (...args) => Promise.resolve().then(() => (init_browser(), exports_browser)).then(({ default: fetch3 }) => fetch3(...args)).catch((error) => { + throw new Error(`Failed to load @supabase/node-fetch: ${error.message}. This is required for HTTP requests in Node.js environments without native fetch.`); + }); } else { _fetch = fetch; } return (...args) => _fetch(...args); }; + if (!((_a = options === null || options === undefined ? undefined : options.params) === null || _a === undefined ? undefined : _a.apikey)) { + throw new Error("API key is required to connect to Realtime"); + } + this.apiKey = options.params.apikey; this.endPoint = `${endPoint}/${TRANSPORTS.websocket}`; this.httpEndpoint = httpEndpointURL(endPoint); - if (options === null || options === undefined ? undefined : options.transport) { - this.transport = options.transport; - } else { - this.transport = null; - } - if (options === null || options === undefined ? undefined : options.params) - this.params = options.params; - if (options === null || options === undefined ? undefined : options.timeout) - this.timeout = options.timeout; - if (options === null || options === undefined ? undefined : options.logger) - this.logger = options.logger; - if ((options === null || options === undefined ? undefined : options.logLevel) || (options === null || options === undefined ? undefined : options.log_level)) { - this.logLevel = options.logLevel || options.log_level; - this.params = Object.assign(Object.assign({}, this.params), { log_level: this.logLevel }); - } - if (options === null || options === undefined ? undefined : options.heartbeatIntervalMs) - this.heartbeatIntervalMs = options.heartbeatIntervalMs; - const accessTokenValue = (_a = options === null || options === undefined ? undefined : options.params) === null || _a === undefined ? undefined : _a.apikey; - if (accessTokenValue) { - this.accessTokenValue = accessTokenValue; - this.apiKey = accessTokenValue; - } - this.reconnectAfterMs = (options === null || options === undefined ? undefined : options.reconnectAfterMs) ? options.reconnectAfterMs : (tries) => { - return [1000, 2000, 5000, 1e4][tries - 1] || 1e4; - }; - this.encode = (options === null || options === undefined ? undefined : options.encode) ? options.encode : (payload, callback) => { - return callback(JSON.stringify(payload)); - }; - this.decode = (options === null || options === undefined ? undefined : options.decode) ? options.decode : this.serializer.decode.bind(this.serializer); - this.reconnectTimer = new Timer(async () => { - this.disconnect(); - this.connect(); - }, this.reconnectAfterMs); + this._initializeOptions(options); + this._setupReconnectionTimer(); this.fetch = this._resolveFetch(options === null || options === undefined ? undefined : options.fetch); - if (options === null || options === undefined ? undefined : options.worker) { - if (typeof window !== "undefined" && !window.Worker) { - throw new Error("Web Worker is not supported"); - } - this.worker = (options === null || options === undefined ? undefined : options.worker) || false; - this.workerUrl = options === null || options === undefined ? undefined : options.workerUrl; - } - this.accessToken = (options === null || options === undefined ? undefined : options.accessToken) || null; } connect() { - if (this.conn) { + if (this.isConnecting() || this.isDisconnecting() || this.conn !== null && this.isConnected()) { return; } - if (!this.transport) { - this.transport = WebSocket2; - } - if (!this.transport) { - throw new Error("No transport provided"); + this._setConnectionState("connecting"); + this._setAuthSafely("connect"); + if (this.transport) { + this.conn = new this.transport(this.endpointURL()); + } else { + try { + this.conn = websocket_factory_default.createWebSocket(this.endpointURL()); + } catch (error) { + this._setConnectionState("disconnected"); + const errorMessage = error.message; + if (errorMessage.includes("Node.js")) { + throw new Error(`${errorMessage} + +To use Realtime in Node.js, you need to provide a WebSocket implementation: + +Option 1: Use Node.js 22+ which has native WebSocket support +Option 2: Install and provide the "ws" package: + + npm install ws + + import ws from "ws" + const client = new RealtimeClient(url, { + ...options, + transport: ws + })`); + } + throw new Error(`WebSocket not available: ${errorMessage}`); + } } - this.conn = new this.transport(this.endpointURL()); - this.setupConnection(); + this._setupConnectionHandlers(); } endpointURL() { return this._appendParams(this.endPoint, Object.assign({}, this.params, { vsn: VSN })); } disconnect(code2, reason) { + if (this.isDisconnecting()) { + return; + } + this._setConnectionState("disconnecting", true); if (this.conn) { - this.conn.onclose = function() { + const fallbackTimer = setTimeout(() => { + this._setConnectionState("disconnected"); + }, 100); + this.conn.onclose = () => { + clearTimeout(fallbackTimer); + this._setConnectionState("disconnected"); }; if (code2) { this.conn.close(code2, reason !== null && reason !== undefined ? reason : ""); } else { this.conn.close(); } - this.conn = null; - this.heartbeatTimer && clearInterval(this.heartbeatTimer); - this.reconnectTimer.reset(); - this.channels.forEach((channel) => channel.teardown()); + this._teardownConnection(); + } else { + this._setConnectionState("disconnected"); } } getChannels() { @@ -51230,6 +51602,12 @@ class RealtimeClient { isConnected() { return this.connectionState() === CONNECTION_STATE.Open; } + isConnecting() { + return this._connectionState === "connecting"; + } + isDisconnecting() { + return this._connectionState === "disconnecting"; + } channel(topic, params = { config: {} }) { const realtimeTopic = `realtime:${topic}`; const exists = this.getChannels().find((c) => c.topic === realtimeTopic); @@ -51257,34 +51635,39 @@ class RealtimeClient { } } async setAuth(token = null) { - let tokenToSend = token || this.accessToken && await this.accessToken() || this.accessTokenValue; - if (this.accessTokenValue != tokenToSend) { - this.accessTokenValue = tokenToSend; - this.channels.forEach((channel) => { - const payload = { - access_token: tokenToSend, - version: DEFAULT_VERSION - }; - tokenToSend && channel.updateJoinPayload(payload); - if (channel.joinedOnce && channel._isJoined()) { - channel._push(CHANNEL_EVENTS.access_token, { - access_token: tokenToSend - }); - } - }); + this._authPromise = this._performAuth(token); + try { + await this._authPromise; + } finally { + this._authPromise = null; } } async sendHeartbeat() { var _a; if (!this.isConnected()) { - this.heartbeatCallback("disconnected"); + try { + this.heartbeatCallback("disconnected"); + } catch (e) { + this.log("error", "error in heartbeat callback", e); + } return; } if (this.pendingHeartbeatRef) { this.pendingHeartbeatRef = null; this.log("transport", "heartbeat timeout. Attempting to re-establish connection"); - this.heartbeatCallback("timeout"); - (_a = this.conn) === null || _a === undefined || _a.close(WS_CLOSE_NORMAL, "hearbeat timeout"); + try { + this.heartbeatCallback("timeout"); + } catch (e) { + this.log("error", "error in heartbeat callback", e); + } + this._wasManualDisconnect = false; + (_a = this.conn) === null || _a === undefined || _a.close(WS_CLOSE_NORMAL, "heartbeat timeout"); + setTimeout(() => { + var _a2; + if (!this.isConnected()) { + (_a2 = this.reconnectTimer) === null || _a2 === undefined || _a2.scheduleTimeout(); + } + }, CONNECTION_TIMEOUTS.HEARTBEAT_TIMEOUT_FALLBACK); return; } this.pendingHeartbeatRef = this._makeRef(); @@ -51294,8 +51677,12 @@ class RealtimeClient { payload: {}, ref: this.pendingHeartbeatRef }); - this.heartbeatCallback("sent"); - await this.setAuth(); + try { + this.heartbeatCallback("sent"); + } catch (e) { + this.log("error", "error in heartbeat callback", e); + } + this._setAuthSafely("heartbeat"); } onHeartbeat(callback) { this.heartbeatCallback = callback; @@ -51325,33 +51712,66 @@ class RealtimeClient { _remove(channel) { this.channels = this.channels.filter((c) => c.topic !== channel.topic); } - setupConnection() { - if (this.conn) { - this.conn.binaryType = "arraybuffer"; - this.conn.onopen = () => this._onConnOpen(); - this.conn.onerror = (error) => this._onConnError(error); - this.conn.onmessage = (event) => this._onConnMessage(event); - this.conn.onclose = (event) => this._onConnClose(event); - } - } _onConnMessage(rawMessage) { this.decode(rawMessage.data, (msg) => { - let { topic, event, payload, ref } = msg; - if (topic === "phoenix" && event === "phx_reply") { - this.heartbeatCallback(msg.payload.status == "ok" ? "ok" : "error"); + if (msg.topic === "phoenix" && msg.event === "phx_reply") { + try { + this.heartbeatCallback(msg.payload.status === "ok" ? "ok" : "error"); + } catch (e) { + this.log("error", "error in heartbeat callback", e); + } } - if (ref && ref === this.pendingHeartbeatRef) { + if (msg.ref && msg.ref === this.pendingHeartbeatRef) { this.pendingHeartbeatRef = null; } - this.log("receive", `${payload.status || ""} ${topic} ${event} ${ref && "(" + ref + ")" || ""}`, payload); - Array.from(this.channels).filter((channel) => channel._isMember(topic)).forEach((channel) => channel._trigger(event, payload, ref)); - this.stateChangeCallbacks.message.forEach((callback) => callback(msg)); + const { topic, event, payload, ref } = msg; + const refString = ref ? `(${ref})` : ""; + const status = payload.status || ""; + this.log("receive", `${status} ${topic} ${event} ${refString}`.trim(), payload); + this.channels.filter((channel) => channel._isMember(topic)).forEach((channel) => channel._trigger(event, payload, ref)); + this._triggerStateCallbacks("message", msg); }); } + _clearTimer(timer) { + var _a; + if (timer === "heartbeat" && this.heartbeatTimer) { + clearInterval(this.heartbeatTimer); + this.heartbeatTimer = undefined; + } else if (timer === "reconnect") { + (_a = this.reconnectTimer) === null || _a === undefined || _a.reset(); + } + } + _clearAllTimers() { + this._clearTimer("heartbeat"); + this._clearTimer("reconnect"); + } + _setupConnectionHandlers() { + if (!this.conn) + return; + if ("binaryType" in this.conn) { + this.conn.binaryType = "arraybuffer"; + } + this.conn.onopen = () => this._onConnOpen(); + this.conn.onerror = (error) => this._onConnError(error); + this.conn.onmessage = (event) => this._onConnMessage(event); + this.conn.onclose = (event) => this._onConnClose(event); + } + _teardownConnection() { + if (this.conn) { + this.conn.onopen = null; + this.conn.onerror = null; + this.conn.onmessage = null; + this.conn.onclose = null; + this.conn = null; + } + this._clearAllTimers(); + this.channels.forEach((channel) => channel.teardown()); + } _onConnOpen() { + this._setConnectionState("connected"); this.log("transport", `connected to ${this.endpointURL()}`); this.flushSendBuffer(); - this.reconnectTimer.reset(); + this._clearTimer("reconnect"); if (!this.worker) { this._startHeartbeat(); } else { @@ -51359,7 +51779,7 @@ class RealtimeClient { this._startWorkerHeartbeat(); } } - this.stateChangeCallbacks.open.forEach((callback) => callback()); + this._triggerStateCallbacks("open"); } _startHeartbeat() { this.heartbeatTimer && clearInterval(this.heartbeatTimer); @@ -51388,16 +51808,21 @@ class RealtimeClient { }); } _onConnClose(event) { + var _a; + this._setConnectionState("disconnected"); this.log("transport", "close", event); this._triggerChanError(); - this.heartbeatTimer && clearInterval(this.heartbeatTimer); - this.reconnectTimer.scheduleTimeout(); - this.stateChangeCallbacks.close.forEach((callback) => callback(event)); + this._clearTimer("heartbeat"); + if (!this._wasManualDisconnect) { + (_a = this.reconnectTimer) === null || _a === undefined || _a.scheduleTimeout(); + } + this._triggerStateCallbacks("close", event); } _onConnError(error) { + this._setConnectionState("disconnected"); this.log("transport", `${error}`); this._triggerChanError(); - this.stateChangeCallbacks.error.forEach((callback) => callback(error)); + this._triggerStateCallbacks("error", error); } _triggerChanError() { this.channels.forEach((channel) => channel._trigger(CHANNEL_EVENTS.error)); @@ -51420,6 +51845,102 @@ class RealtimeClient { } return result_url; } + _setConnectionState(state2, manual = false) { + this._connectionState = state2; + if (state2 === "connecting") { + this._wasManualDisconnect = false; + } else if (state2 === "disconnecting") { + this._wasManualDisconnect = manual; + } + } + async _performAuth(token = null) { + let tokenToSend; + if (token) { + tokenToSend = token; + } else if (this.accessToken) { + tokenToSend = await this.accessToken(); + } else { + tokenToSend = this.accessTokenValue; + } + if (this.accessTokenValue != tokenToSend) { + this.accessTokenValue = tokenToSend; + this.channels.forEach((channel) => { + const payload = { + access_token: tokenToSend, + version: DEFAULT_VERSION + }; + tokenToSend && channel.updateJoinPayload(payload); + if (channel.joinedOnce && channel._isJoined()) { + channel._push(CHANNEL_EVENTS.access_token, { + access_token: tokenToSend + }); + } + }); + } + } + async _waitForAuthIfNeeded() { + if (this._authPromise) { + await this._authPromise; + } + } + _setAuthSafely(context = "general") { + this.setAuth().catch((e) => { + this.log("error", `error setting auth in ${context}`, e); + }); + } + _triggerStateCallbacks(event, data) { + try { + this.stateChangeCallbacks[event].forEach((callback) => { + try { + callback(data); + } catch (e) { + this.log("error", `error in ${event} callback`, e); + } + }); + } catch (e) { + this.log("error", `error triggering ${event} callbacks`, e); + } + } + _setupReconnectionTimer() { + this.reconnectTimer = new Timer(async () => { + setTimeout(async () => { + await this._waitForAuthIfNeeded(); + if (!this.isConnected()) { + this.connect(); + } + }, CONNECTION_TIMEOUTS.RECONNECT_DELAY); + }, this.reconnectAfterMs); + } + _initializeOptions(options) { + var _a, _b, _c, _d, _e, _f, _g, _h, _j; + this.transport = (_a = options === null || options === undefined ? undefined : options.transport) !== null && _a !== undefined ? _a : null; + this.timeout = (_b = options === null || options === undefined ? undefined : options.timeout) !== null && _b !== undefined ? _b : DEFAULT_TIMEOUT; + this.heartbeatIntervalMs = (_c = options === null || options === undefined ? undefined : options.heartbeatIntervalMs) !== null && _c !== undefined ? _c : CONNECTION_TIMEOUTS.HEARTBEAT_INTERVAL; + this.worker = (_d = options === null || options === undefined ? undefined : options.worker) !== null && _d !== undefined ? _d : false; + this.accessToken = (_e = options === null || options === undefined ? undefined : options.accessToken) !== null && _e !== undefined ? _e : null; + this.heartbeatCallback = (_f = options === null || options === undefined ? undefined : options.heartbeatCallback) !== null && _f !== undefined ? _f : noop2; + if (options === null || options === undefined ? undefined : options.params) + this.params = options.params; + if (options === null || options === undefined ? undefined : options.logger) + this.logger = options.logger; + if ((options === null || options === undefined ? undefined : options.logLevel) || (options === null || options === undefined ? undefined : options.log_level)) { + this.logLevel = options.logLevel || options.log_level; + this.params = Object.assign(Object.assign({}, this.params), { log_level: this.logLevel }); + } + this.reconnectAfterMs = (_g = options === null || options === undefined ? undefined : options.reconnectAfterMs) !== null && _g !== undefined ? _g : (tries) => { + return RECONNECT_INTERVALS[tries - 1] || DEFAULT_RECONNECT_FALLBACK; + }; + this.encode = (_h = options === null || options === undefined ? undefined : options.encode) !== null && _h !== undefined ? _h : (payload, callback) => { + return callback(JSON.stringify(payload)); + }; + this.decode = (_j = options === null || options === undefined ? undefined : options.decode) !== null && _j !== undefined ? _j : this.serializer.decode.bind(this.serializer); + if (this.worker) { + if (typeof window !== "undefined" && !window.Worker) { + throw new Error("Web Worker is not supported"); + } + this.workerUrl = options === null || options === undefined ? undefined : options.workerUrl; + } + } } // node_modules/@supabase/storage-js/dist/module/lib/errors.js @@ -51579,6 +52100,9 @@ var _getRequestParams = (method, options, parameters, body) => { } else { params.body = body; } + if (options === null || options === undefined ? undefined : options.duplex) { + params.duplex = options.duplex; + } return Object.assign(Object.assign({}, params), parameters); }; function _handleRequest(fetcher, method, url, options, parameters, body) { @@ -51958,6 +52482,20 @@ class StorageFileApi { } }); } + listV2(options, parameters) { + return __awaiter4(this, undefined, undefined, function* () { + try { + const body = Object.assign({}, options); + const data = yield post(this.fetch, `${this.url}/object/list-v2/${this.bucketId}`, body, { headers: this.headers }, parameters); + return { data, error: null }; + } catch (error) { + if (isStorageError(error)) { + return { data: null, error }; + } + throw error; + } + }); + } encodeMetadata(metadata) { return JSON.stringify(metadata); } @@ -51995,7 +52533,7 @@ class StorageFileApi { } // node_modules/@supabase/storage-js/dist/module/lib/version.js -var version2 = "2.10.4"; +var version2 = "2.11.0"; // node_modules/@supabase/storage-js/dist/module/lib/constants.js var DEFAULT_HEADERS = { "X-Client-Info": `storage-js/${version2}` }; @@ -52147,7 +52685,7 @@ class StorageClient extends StorageBucketApi { } } // node_modules/@supabase/supabase-js/dist/module/lib/version.js -var version3 = "2.53.0"; +var version3 = "2.56.1"; // node_modules/@supabase/supabase-js/dist/module/lib/constants.js var JS_ENV = ""; @@ -52644,8 +53182,7 @@ function parseParametersFromURL(href) { hashSearchParams.forEach((value, key) => { result[key] = value; }); - } catch (e) { - } + } catch (e) {} } url.searchParams.forEach((value, key) => { result[key] = value; @@ -54030,8 +54567,7 @@ class GoTrueClient { this.pendingInLock.push((async () => { try { await result; - } catch (e) { - } + } catch (e) {} })()); return result; } @@ -54043,8 +54579,7 @@ class GoTrueClient { this.pendingInLock.push((async () => { try { await result; - } catch (e) { - } + } catch (e) {} })()); await result; while (this.pendingInLock.length) { @@ -54735,8 +55270,7 @@ class GoTrueClient { await setItemAsync(this.userStorage, this.storageKey + "-user", { user: sessionToProcess.user }); - } else if (userIsProxy) { - } + } else if (userIsProxy) {} const mainSessionData = Object.assign({}, sessionToProcess); delete mainSessionData.user; const clonedMainSessionData = deepClone(mainSessionData); @@ -55260,7 +55794,7 @@ class SupabaseClient { return yield this.accessToken(); } const { data } = yield this.auth.getSession(); - return (_b = (_a = data.session) === null || _a === undefined ? undefined : _a.access_token) !== null && _b !== undefined ? _b : null; + return (_b = (_a = data.session) === null || _a === undefined ? undefined : _a.access_token) !== null && _b !== undefined ? _b : this.supabaseKey; }); } _initSupabaseAuthClient({ autoRefreshToken, persistSession, detectSessionInUrl, storage, storageKey, flowType, lock, debug }, headers, fetch3) { @@ -55308,10 +55842,17 @@ var createClient = (supabaseUrl, supabaseKey, options) => { return new SupabaseClient(supabaseUrl, supabaseKey, options); }; function shouldShowDeprecationWarning() { - if (typeof window !== "undefined" || typeof process === "undefined" || process.version === undefined || process.version === null) { + if (typeof window !== "undefined") { + return false; + } + if (typeof process === "undefined") { + return false; + } + const processVersion = process["version"]; + if (processVersion === undefined || processVersion === null) { return false; } - const versionMatch = process.version.match(/^v(\d+)\./); + const versionMatch = processVersion.match(/^v(\d+)\./); if (!versionMatch) { return false; } diff --git a/engine/editor/definitions.ts b/engine/editor/definitions.ts index 84b0a70..b0c790f 100644 --- a/engine/editor/definitions.ts +++ b/engine/editor/definitions.ts @@ -8,6 +8,7 @@ export const nodeDefinitions: Record = { width: { type: "number", default: 800, description: "Width of the game canvas in pixels." }, height: { type: "number", default: 600, description: "Height of the game canvas in pixels." }, devmode: { type: "boolean", default: true, description: "Enable developer mode." }, + showtoolTips: { type: "boolean", default: false, description: "Show tooltips in devmode." }, disableAntiAliasing: { type: "boolean", default: false, description: "Disable anti-aliasing for every object." }, starterScene: { type: "Part", subType: "Scene", description: "The scene that will be loaded when the game starts." }, showFrameStats: { type: "enum", default: "BASIC", options: ["BASIC", "EXTENDED", "ADVANCED", "PERFORMANCE_HUD"], description: "Show frame statistics." } @@ -22,7 +23,8 @@ export const nodeDefinitions: Record = { }, "Layer": { properties: { - name: { type: "text", default: "NewLayer", description: "The name of the layer." } + name: { type: "text", default: "NewLayer", description: "The name of the layer." }, + spatialGridDefinition: { type: "number", default: 100, description: "Cell size for the spatial grid used for broad-phase collision detection." } }, children: ["GameObject"] }, @@ -31,7 +33,7 @@ export const nodeDefinitions: Record = { name: { type: "text", default: "NewGameObject", description: "The name of the game object." }, render: { type: "boolean", default: true, description: "Whether this GameObject should be rendered. If false, no child Parts will be ran." } }, - children: ["Transform", "BoxCollider", "PolygonCollider", "ColorRender", "SpriteRender", "AnimatedSprite", "TextRender", "Button", "Sound", "Health", "Timer", "Spawner", "Follow", "CharacterMovement", "PhysicsEngine", "Rotator", "Scaler", "Projectile", "AreaTrigger", "ParticleEmitter", "WaypointFollower", "CameraShake", "HealthBar", "PhysicsBody", "GravityCharacterMovement"] + children: ["Transform", "BoxCollider", "PolygonCollider", "MultiPolygonCollider", "ColorRender", "SpriteRender", "AnimatedSprite", "TextRender", "Button", "Sound", "Health", "Timer", "Spawner", "Follow", "CharacterMovement", "PhysicsEngine", "Rotator", "Scaler", "Projectile", "AreaTrigger", "ParticleEmitter", "WaypointFollower", "CameraShake", "HealthBar", "PhysicsBody", "GravityCharacterMovement"] }, "Camera": { properties: { @@ -74,12 +76,21 @@ export const nodeDefinitions: Record = { }, singular: true }, + "MultiPolygonCollider": { + properties: { + name: { type: "text", default: "MultiPolygonCollider", description: "The name of the multi-polygon collider." }, + tag: { type: "text", default: "", description: "The tag of the collider." }, + polygons: { type: "list", subType: "list", tertiaryType: "Vector", default: [], description: "List of polygons (each a list of vectors)." } + }, + singular: true + }, "ColorRender": { properties: { name: { type: "text", default: "ColorRender", description: "The name of the color renderer." }, width: { type: "number", default: 50, description: "Width of the color renderer." }, height: { type: "number", default: 50, description: "Height of the color renderer." }, - color: { type: "color", default: "#FF0000", description: "Color of the renderer." } + color: { type: "color", default: "#FF0000", description: "Color of the renderer." }, + vertices: { type: "list", subType: "Vector", default: [], description: "Vertices of a polygon to render. If not provided, a rectangle is rendered." } }, singular: true }, @@ -106,6 +117,8 @@ export const nodeDefinitions: Record = { disableAntiAliasing: { type: "boolean", default: false, description: "Disable anti-aliasing for this sprite." }, facing: { type: "Vector", default: "new Vector(1, 1)", description: "Direction to face. Use -1 to flip." }, webEngine: { type: "boolean", default: true, description: "Set to true if this is running in a web engine context.", dontShow: true }, + loop: { type: "boolean", default: true, description: "Override spritesheet data and loop immediately." }, + bounce: { type: "boolean", default: false, description: "Override spritesheet data and bounce immediately." } }, singular: true }, @@ -204,6 +217,7 @@ export const nodeDefinitions: Record = { properties: { name: { type: "text", default: "PhysicsEngine", description: "The name of the physics engine." }, gravity: { type: "Vector", default: "new Vector(0, 1)", description: "Gravity vector applied to all physics bodies." }, + scale: { type: "number", default: 0.001, description: "Scale for the physics engine." } }, singular: true }, @@ -296,7 +310,9 @@ export const nodeDefinitions: Record = { input: { type: "Part", subType: "Input", description: "The input component for the scene, used to track player input." }, gravityScale: { type: "Vector", default: "new Vector(0, 0.5)", description: "Gravity scale applied to the character." }, maxSpeed: { type: "number", default: 10, description: "Maximum speed of the character." }, - jumpForce: { type: "number", default: 10, description: "Jump force applied to the character." } + jumpForce: { type: "number", default: 10, description: "Jump force applied to the character." }, + waterFraction: { type: "number", default: 0.5, description: "Fraction of normal speed and jump force when in water (0-1)." }, + landFraction: { type: "number", default: 1.0, description: "Fraction of normal speed and jump force when on land (0-1)." } }, singular: true } diff --git a/engine/spriteEditor.js b/engine/spriteEditor.js index 7efd206..f9c86cf 100644 --- a/engine/spriteEditor.js +++ b/engine/spriteEditor.js @@ -41,2381 +41,3367 @@ var __export = (target, all) => { }; var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res); -// node:stream -var exports_stream = {}; -__export(exports_stream, { - default: () => Ih +// node:buffer +var exports_buffer = {}; +__export(exports_buffer, { + transcode: () => transcode, + resolveObjectURL: () => resolveObjectURL, + kStringMaxLength: () => kStringMaxLength, + kMaxLength: () => kMaxLength, + isUtf8: () => isUtf8, + isAscii: () => isAscii, + default: () => buffer_default, + constants: () => constants, + btoa: () => btoa, + atob: () => atob, + INSPECT_MAX_BYTES: () => INSPECT_MAX_BYTES, + File: () => File, + Buffer: () => Buffer2, + Blob: () => Blob2 }); -var pu, _t, yu, bu, wu, gu, kn = (e, t) => () => (e && (t = e(e = 0)), t), S = (e, t) => () => (t || e((t = { exports: {} }).exports, t), t.exports), or = (e, t) => { - for (var r in t) - _t(e, r, { get: t[r], enumerable: true }); -}, gt = (e, t, r, n) => { - if (t && typeof t == "object" || typeof t == "function") - for (let i of bu(t)) - !gu.call(e, i) && i !== r && _t(e, i, { get: () => t[i], enumerable: !(n = yu(t, i)) || n.enumerable }); - return e; -}, pe = (e, t, r) => (gt(e, t, "default"), r && gt(r, t, "default")), rt = (e, t, r) => (r = e != null ? pu(wu(e)) : {}, gt(t || !e || !e.__esModule ? _t(r, "default", { value: e, enumerable: true }) : r, e)), ye = (e) => gt(_t({}, "__esModule", { value: true }), e), Pn, qn, le, I, wr, O, We, it, q, He, Br, H, aa, we, Z, ae, Be, Dt, ot, So, lt, Bo, Fo, Mo, vt, Co, Gr, at, zt, Bl, re, hn, yn, Zt, In, ru, Nn, Fn, Cn, wt, Ih; -var init_stream = __esm(() => { - pu = Object.create; - _t = Object.defineProperty; - yu = Object.getOwnPropertyDescriptor; - bu = Object.getOwnPropertyNames; - wu = Object.getPrototypeOf; - gu = Object.prototype.hasOwnProperty; - Pn = S((Et) => { - Et.byteLength = Eu; - Et.toByteArray = mu; - Et.fromByteArray = xu; - var K = [], $ = [], _u = typeof Uint8Array < "u" ? Uint8Array : Array, lr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - for (me = 0, Dn = lr.length;me < Dn; ++me) - K[me] = lr[me], $[lr.charCodeAt(me)] = me; - var me, Dn; - $[45] = 62; - $[95] = 63; - function On(e) { - var t = e.length; - if (t % 4 > 0) - throw new Error("Invalid string. Length must be a multiple of 4"); - var r = e.indexOf("="); - r === -1 && (r = t); - var n = r === t ? 0 : 4 - r % 4; - return [r, n]; - } - function Eu(e) { - var t = On(e), r = t[0], n = t[1]; - return (r + n) * 3 / 4 - n; - } - function Su(e, t, r) { - return (t + r) * 3 / 4 - r; - } - function mu(e) { - var t, r = On(e), n = r[0], i = r[1], o = new _u(Su(e, n, i)), l = 0, u = i > 0 ? n - 4 : n, a; - for (a = 0;a < u; a += 4) - t = $[e.charCodeAt(a)] << 18 | $[e.charCodeAt(a + 1)] << 12 | $[e.charCodeAt(a + 2)] << 6 | $[e.charCodeAt(a + 3)], o[l++] = t >> 16 & 255, o[l++] = t >> 8 & 255, o[l++] = t & 255; - return i === 2 && (t = $[e.charCodeAt(a)] << 2 | $[e.charCodeAt(a + 1)] >> 4, o[l++] = t & 255), i === 1 && (t = $[e.charCodeAt(a)] << 10 | $[e.charCodeAt(a + 1)] << 4 | $[e.charCodeAt(a + 2)] >> 2, o[l++] = t >> 8 & 255, o[l++] = t & 255), o; - } - function Ru(e) { - return K[e >> 18 & 63] + K[e >> 12 & 63] + K[e >> 6 & 63] + K[e & 63]; - } - function Au(e, t, r) { - for (var n, i = [], o = t;o < r; o += 3) - n = (e[o] << 16 & 16711680) + (e[o + 1] << 8 & 65280) + (e[o + 2] & 255), i.push(Ru(n)); - return i.join(""); - } - function xu(e) { - for (var t, r = e.length, n = r % 3, i = [], o = 16383, l = 0, u = r - n;l < u; l += o) - i.push(Au(e, l, l + o > u ? u : l + o)); - return n === 1 ? (t = e[r - 1], i.push(K[t >> 2] + K[t << 4 & 63] + "==")) : n === 2 && (t = (e[r - 2] << 8) + e[r - 1], i.push(K[t >> 10] + K[t >> 4 & 63] + K[t << 2 & 63] + "=")), i.join(""); +function getLens(b64) { + var len2 = b64.length; + if (len2 % 4 > 0) + throw new Error("Invalid string. Length must be a multiple of 4"); + var validLen = b64.indexOf("="); + if (validLen === -1) + validLen = len2; + var placeHoldersLen = validLen === len2 ? 0 : 4 - validLen % 4; + return [validLen, placeHoldersLen]; +} +function _byteLength(validLen, placeHoldersLen) { + return (validLen + placeHoldersLen) * 3 / 4 - placeHoldersLen; +} +function toByteArray(b64) { + var tmp, lens = getLens(b64), validLen = lens[0], placeHoldersLen = lens[1], arr = new Uint8Array(_byteLength(validLen, placeHoldersLen)), curByte = 0, len2 = placeHoldersLen > 0 ? validLen - 4 : validLen, i2; + for (i2 = 0;i2 < len2; i2 += 4) + tmp = revLookup[b64.charCodeAt(i2)] << 18 | revLookup[b64.charCodeAt(i2 + 1)] << 12 | revLookup[b64.charCodeAt(i2 + 2)] << 6 | revLookup[b64.charCodeAt(i2 + 3)], arr[curByte++] = tmp >> 16 & 255, arr[curByte++] = tmp >> 8 & 255, arr[curByte++] = tmp & 255; + if (placeHoldersLen === 2) + tmp = revLookup[b64.charCodeAt(i2)] << 2 | revLookup[b64.charCodeAt(i2 + 1)] >> 4, arr[curByte++] = tmp & 255; + if (placeHoldersLen === 1) + tmp = revLookup[b64.charCodeAt(i2)] << 10 | revLookup[b64.charCodeAt(i2 + 1)] << 4 | revLookup[b64.charCodeAt(i2 + 2)] >> 2, arr[curByte++] = tmp >> 8 & 255, arr[curByte++] = tmp & 255; + return arr; +} +function tripletToBase64(num) { + return lookup[num >> 18 & 63] + lookup[num >> 12 & 63] + lookup[num >> 6 & 63] + lookup[num & 63]; +} +function encodeChunk(uint8, start, end) { + var tmp, output = []; + for (var i2 = start;i2 < end; i2 += 3) + tmp = (uint8[i2] << 16 & 16711680) + (uint8[i2 + 1] << 8 & 65280) + (uint8[i2 + 2] & 255), output.push(tripletToBase64(tmp)); + return output.join(""); +} +function fromByteArray(uint8) { + var tmp, len2 = uint8.length, extraBytes = len2 % 3, parts = [], maxChunkLength = 16383; + for (var i2 = 0, len22 = len2 - extraBytes;i2 < len22; i2 += maxChunkLength) + parts.push(encodeChunk(uint8, i2, i2 + maxChunkLength > len22 ? len22 : i2 + maxChunkLength)); + if (extraBytes === 1) + tmp = uint8[len2 - 1], parts.push(lookup[tmp >> 2] + lookup[tmp << 4 & 63] + "=="); + else if (extraBytes === 2) + tmp = (uint8[len2 - 2] << 8) + uint8[len2 - 1], parts.push(lookup[tmp >> 10] + lookup[tmp >> 4 & 63] + lookup[tmp << 2 & 63] + "="); + return parts.join(""); +} +function read(buffer, offset, isLE, mLen, nBytes) { + var e, m, eLen = nBytes * 8 - mLen - 1, eMax = (1 << eLen) - 1, eBias = eMax >> 1, nBits = -7, i2 = isLE ? nBytes - 1 : 0, d = isLE ? -1 : 1, s = buffer[offset + i2]; + i2 += d, e = s & (1 << -nBits) - 1, s >>= -nBits, nBits += eLen; + for (;nBits > 0; e = e * 256 + buffer[offset + i2], i2 += d, nBits -= 8) + ; + m = e & (1 << -nBits) - 1, e >>= -nBits, nBits += mLen; + for (;nBits > 0; m = m * 256 + buffer[offset + i2], i2 += d, nBits -= 8) + ; + if (e === 0) + e = 1 - eBias; + else if (e === eMax) + return m ? NaN : (s ? -1 : 1) * (1 / 0); + else + m = m + Math.pow(2, mLen), e = e - eBias; + return (s ? -1 : 1) * m * Math.pow(2, e - mLen); +} +function write(buffer, value, offset, isLE, mLen, nBytes) { + var e, m, c, eLen = nBytes * 8 - mLen - 1, eMax = (1 << eLen) - 1, eBias = eMax >> 1, rt = mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0, i2 = isLE ? 0 : nBytes - 1, d = isLE ? 1 : -1, s = value < 0 || value === 0 && 1 / value < 0 ? 1 : 0; + if (value = Math.abs(value), isNaN(value) || value === 1 / 0) + m = isNaN(value) ? 1 : 0, e = eMax; + else { + if (e = Math.floor(Math.log(value) / Math.LN2), value * (c = Math.pow(2, -e)) < 1) + e--, c *= 2; + if (e + eBias >= 1) + value += rt / c; + else + value += rt * Math.pow(2, 1 - eBias); + if (value * c >= 2) + e++, c /= 2; + if (e + eBias >= eMax) + m = 0, e = eMax; + else if (e + eBias >= 1) + m = (value * c - 1) * Math.pow(2, mLen), e = e + eBias; + else + m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen), e = 0; + } + for (;mLen >= 8; buffer[offset + i2] = m & 255, i2 += d, m /= 256, mLen -= 8) + ; + e = e << mLen | m, eLen += mLen; + for (;eLen > 0; buffer[offset + i2] = e & 255, i2 += d, e /= 256, eLen -= 8) + ; + buffer[offset + i2 - d] |= s * 128; +} +function createBuffer(length) { + if (length > kMaxLength) + throw new RangeError('The value "' + length + '" is invalid for option "size"'); + let buf = new Uint8Array(length); + return Object.setPrototypeOf(buf, Buffer2.prototype), buf; +} +function E(sym, getMessage, Base) { + return class NodeError extends Base { + constructor() { + super(); + Object.defineProperty(this, "message", { value: getMessage.apply(this, arguments), writable: true, configurable: true }), this.name = `${this.name} [${sym}]`, this.stack, delete this.name; } - }); - qn = S((ur) => { - ur.read = function(e, t, r, n, i) { - var o, l, u = i * 8 - n - 1, a = (1 << u) - 1, s = a >> 1, c = -7, d = r ? i - 1 : 0, y = r ? -1 : 1, h = e[t + d]; - for (d += y, o = h & (1 << -c) - 1, h >>= -c, c += u;c > 0; o = o * 256 + e[t + d], d += y, c -= 8) - ; - for (l = o & (1 << -c) - 1, o >>= -c, c += n;c > 0; l = l * 256 + e[t + d], d += y, c -= 8) - ; - if (o === 0) - o = 1 - s; - else { - if (o === a) - return l ? NaN : (h ? -1 : 1) * (1 / 0); - l = l + Math.pow(2, n), o = o - s; - } - return (h ? -1 : 1) * l * Math.pow(2, o - n); - }; - ur.write = function(e, t, r, n, i, o) { - var l, u, a, s = o * 8 - i - 1, c = (1 << s) - 1, d = c >> 1, y = i === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0, h = n ? 0 : o - 1, _ = n ? 1 : -1, p = t < 0 || t === 0 && 1 / t < 0 ? 1 : 0; - for (t = Math.abs(t), isNaN(t) || t === 1 / 0 ? (u = isNaN(t) ? 1 : 0, l = c) : (l = Math.floor(Math.log(t) / Math.LN2), t * (a = Math.pow(2, -l)) < 1 && (l--, a *= 2), l + d >= 1 ? t += y / a : t += y * Math.pow(2, 1 - d), t * a >= 2 && (l++, a /= 2), l + d >= c ? (u = 0, l = c) : l + d >= 1 ? (u = (t * a - 1) * Math.pow(2, i), l = l + d) : (u = t * Math.pow(2, d - 1) * Math.pow(2, i), l = 0));i >= 8; e[r + h] = u & 255, h += _, u /= 256, i -= 8) - ; - for (l = l << i | u, s += i;s > 0; e[r + h] = l & 255, h += _, l /= 256, s -= 8) - ; - e[r + h - _] |= p * 128; - }; - }); - le = S((Ue) => { - var sr = Pn(), qe = qn(), vn = typeof Symbol == "function" && typeof Symbol.for == "function" ? Symbol.for("nodejs.util.inspect.custom") : null; - Ue.Buffer = f; - Ue.SlowBuffer = Fu; - Ue.INSPECT_MAX_BYTES = 50; - var St = 2147483647; - Ue.kMaxLength = St; - f.TYPED_ARRAY_SUPPORT = Iu(); - !f.TYPED_ARRAY_SUPPORT && typeof console < "u" && typeof console.error == "function" && console.error("This browser lacks typed array (Uint8Array) support which is required by `buffer` v5.x. Use `buffer` v4.x if you require old browser support."); - function Iu() { - try { - let e = new Uint8Array(1), t = { foo: function() { - return 42; - } }; - return Object.setPrototypeOf(t, Uint8Array.prototype), Object.setPrototypeOf(e, t), e.foo() === 42; - } catch { - return false; - } + get code() { + return sym; } - Object.defineProperty(f.prototype, "parent", { enumerable: true, get: function() { - if (!!f.isBuffer(this)) - return this.buffer; - } }); - Object.defineProperty(f.prototype, "offset", { enumerable: true, get: function() { - if (!!f.isBuffer(this)) - return this.byteOffset; - } }); - function oe(e) { - if (e > St) - throw new RangeError('The value "' + e + '" is invalid for option "size"'); - let t = new Uint8Array(e); - return Object.setPrototypeOf(t, f.prototype), t; - } - function f(e, t, r) { - if (typeof e == "number") { - if (typeof t == "string") - throw new TypeError('The "string" argument must be of type string. Received type number'); - return dr(e); - } - return jn(e, t, r); - } - f.poolSize = 8192; - function jn(e, t, r) { - if (typeof e == "string") - return Bu(e, t); - if (ArrayBuffer.isView(e)) - return Lu(e); - if (e == null) - throw new TypeError("The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type " + typeof e); - if (z(e, ArrayBuffer) || e && z(e.buffer, ArrayBuffer) || typeof SharedArrayBuffer < "u" && (z(e, SharedArrayBuffer) || e && z(e.buffer, SharedArrayBuffer))) - return fr(e, t, r); - if (typeof e == "number") - throw new TypeError('The "value" argument must not be of type number. Received type number'); - let n = e.valueOf && e.valueOf(); - if (n != null && n !== e) - return f.from(n, t, r); - let i = Nu(e); - if (i) - return i; - if (typeof Symbol < "u" && Symbol.toPrimitive != null && typeof e[Symbol.toPrimitive] == "function") - return f.from(e[Symbol.toPrimitive]("string"), t, r); - throw new TypeError("The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type " + typeof e); - } - f.from = function(e, t, r) { - return jn(e, t, r); - }; - Object.setPrototypeOf(f.prototype, Uint8Array.prototype); - Object.setPrototypeOf(f, Uint8Array); - function Hn(e) { - if (typeof e != "number") - throw new TypeError('"size" argument must be of type number'); - if (e < 0) - throw new RangeError('The value "' + e + '" is invalid for option "size"'); - } - function Tu(e, t, r) { - return Hn(e), e <= 0 ? oe(e) : t !== undefined ? typeof r == "string" ? oe(e).fill(t, r) : oe(e).fill(t) : oe(e); - } - f.alloc = function(e, t, r) { - return Tu(e, t, r); - }; - function dr(e) { - return Hn(e), oe(e < 0 ? 0 : hr(e) | 0); + set code(value) { + Object.defineProperty(this, "code", { configurable: true, enumerable: true, value, writable: true }); } - f.allocUnsafe = function(e) { - return dr(e); - }; - f.allocUnsafeSlow = function(e) { - return dr(e); - }; - function Bu(e, t) { - if ((typeof t != "string" || t === "") && (t = "utf8"), !f.isEncoding(t)) - throw new TypeError("Unknown encoding: " + t); - let r = Gn(e, t) | 0, n = oe(r), i = n.write(e, t); - return i !== r && (n = n.slice(0, i)), n; - } - function ar(e) { - let t = e.length < 0 ? 0 : hr(e.length) | 0, r = oe(t); - for (let n = 0;n < t; n += 1) - r[n] = e[n] & 255; - return r; + toString() { + return `${this.name} [${sym}]: ${this.message}`; } - function Lu(e) { - if (z(e, Uint8Array)) { - let t = new Uint8Array(e); - return fr(t.buffer, t.byteOffset, t.byteLength); - } - return ar(e); - } - function fr(e, t, r) { - if (t < 0 || e.byteLength < t) - throw new RangeError('"offset" is outside of buffer bounds'); - if (e.byteLength < t + (r || 0)) - throw new RangeError('"length" is outside of buffer bounds'); - let n; - return t === undefined && r === undefined ? n = new Uint8Array(e) : r === undefined ? n = new Uint8Array(e, t) : n = new Uint8Array(e, t, r), Object.setPrototypeOf(n, f.prototype), n; - } - function Nu(e) { - if (f.isBuffer(e)) { - let t = hr(e.length) | 0, r = oe(t); - return r.length === 0 || e.copy(r, 0, 0, t), r; - } - if (e.length !== undefined) - return typeof e.length != "number" || yr(e.length) ? oe(0) : ar(e); - if (e.type === "Buffer" && Array.isArray(e.data)) - return ar(e.data); - } - function hr(e) { - if (e >= St) - throw new RangeError("Attempt to allocate Buffer larger than maximum size: 0x" + St.toString(16) + " bytes"); - return e | 0; - } - function Fu(e) { - return +e != e && (e = 0), f.alloc(+e); - } - f.isBuffer = function(t) { - return t != null && t._isBuffer === true && t !== f.prototype; - }; - f.compare = function(t, r) { - if (z(t, Uint8Array) && (t = f.from(t, t.offset, t.byteLength)), z(r, Uint8Array) && (r = f.from(r, r.offset, r.byteLength)), !f.isBuffer(t) || !f.isBuffer(r)) - throw new TypeError('The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array'); - if (t === r) - return 0; - let n = t.length, i = r.length; - for (let o = 0, l = Math.min(n, i);o < l; ++o) - if (t[o] !== r[o]) { - n = t[o], i = r[o]; + }; +} +function Buffer2(arg, encodingOrOffset, length) { + if (typeof arg === "number") { + if (typeof encodingOrOffset === "string") + throw new TypeError('The "string" argument must be of type string. Received type number'); + return allocUnsafe(arg); + } + return from(arg, encodingOrOffset, length); +} +function from(value, encodingOrOffset, length) { + if (typeof value === "string") + return fromString(value, encodingOrOffset); + if (ArrayBuffer.isView(value)) + return fromArrayView(value); + if (value == null) + throw new TypeError("The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type " + typeof value); + if (isInstance(value, ArrayBuffer) || value && isInstance(value.buffer, ArrayBuffer)) + return fromArrayBuffer(value, encodingOrOffset, length); + if (typeof SharedArrayBuffer !== "undefined" && (isInstance(value, SharedArrayBuffer) || value && isInstance(value.buffer, SharedArrayBuffer))) + return fromArrayBuffer(value, encodingOrOffset, length); + if (typeof value === "number") + throw new TypeError('The "value" argument must not be of type number. Received type number'); + let valueOf = value.valueOf && value.valueOf(); + if (valueOf != null && valueOf !== value) + return Buffer2.from(valueOf, encodingOrOffset, length); + let b = fromObject(value); + if (b) + return b; + if (typeof Symbol !== "undefined" && Symbol.toPrimitive != null && typeof value[Symbol.toPrimitive] === "function") + return Buffer2.from(value[Symbol.toPrimitive]("string"), encodingOrOffset, length); + throw new TypeError("The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type " + typeof value); +} +function assertSize(size) { + if (typeof size !== "number") + throw new TypeError('"size" argument must be of type number'); + else if (size < 0) + throw new RangeError('The value "' + size + '" is invalid for option "size"'); +} +function alloc(size, fill, encoding) { + if (assertSize(size), size <= 0) + return createBuffer(size); + if (fill !== undefined) + return typeof encoding === "string" ? createBuffer(size).fill(fill, encoding) : createBuffer(size).fill(fill); + return createBuffer(size); +} +function allocUnsafe(size) { + return assertSize(size), createBuffer(size < 0 ? 0 : checked(size) | 0); +} +function fromString(string, encoding) { + if (typeof encoding !== "string" || encoding === "") + encoding = "utf8"; + if (!Buffer2.isEncoding(encoding)) + throw new TypeError("Unknown encoding: " + encoding); + let length = byteLength(string, encoding) | 0, buf = createBuffer(length), actual = buf.write(string, encoding); + if (actual !== length) + buf = buf.slice(0, actual); + return buf; +} +function fromArrayLike(array) { + let length = array.length < 0 ? 0 : checked(array.length) | 0, buf = createBuffer(length); + for (let i2 = 0;i2 < length; i2 += 1) + buf[i2] = array[i2] & 255; + return buf; +} +function fromArrayView(arrayView) { + if (isInstance(arrayView, Uint8Array)) { + let copy = new Uint8Array(arrayView); + return fromArrayBuffer(copy.buffer, copy.byteOffset, copy.byteLength); + } + return fromArrayLike(arrayView); +} +function fromArrayBuffer(array, byteOffset, length) { + if (byteOffset < 0 || array.byteLength < byteOffset) + throw new RangeError('"offset" is outside of buffer bounds'); + if (array.byteLength < byteOffset + (length || 0)) + throw new RangeError('"length" is outside of buffer bounds'); + let buf; + if (byteOffset === undefined && length === undefined) + buf = new Uint8Array(array); + else if (length === undefined) + buf = new Uint8Array(array, byteOffset); + else + buf = new Uint8Array(array, byteOffset, length); + return Object.setPrototypeOf(buf, Buffer2.prototype), buf; +} +function fromObject(obj) { + if (Buffer2.isBuffer(obj)) { + let len2 = checked(obj.length) | 0, buf = createBuffer(len2); + if (buf.length === 0) + return buf; + return obj.copy(buf, 0, 0, len2), buf; + } + if (obj.length !== undefined) { + if (typeof obj.length !== "number" || Number.isNaN(obj.length)) + return createBuffer(0); + return fromArrayLike(obj); + } + if (obj.type === "Buffer" && Array.isArray(obj.data)) + return fromArrayLike(obj.data); +} +function checked(length) { + if (length >= kMaxLength) + throw new RangeError("Attempt to allocate Buffer larger than maximum size: 0x" + kMaxLength.toString(16) + " bytes"); + return length | 0; +} +function byteLength(string, encoding) { + if (Buffer2.isBuffer(string)) + return string.length; + if (ArrayBuffer.isView(string) || isInstance(string, ArrayBuffer)) + return string.byteLength; + if (typeof string !== "string") + throw new TypeError('The "string" argument must be one of type string, Buffer, or ArrayBuffer. Received type ' + typeof string); + let len2 = string.length, mustMatch = arguments.length > 2 && arguments[2] === true; + if (!mustMatch && len2 === 0) + return 0; + let loweredCase = false; + for (;; ) + switch (encoding) { + case "ascii": + case "latin1": + case "binary": + return len2; + case "utf8": + case "utf-8": + return utf8ToBytes(string).length; + case "ucs2": + case "ucs-2": + case "utf16le": + case "utf-16le": + return len2 * 2; + case "hex": + return len2 >>> 1; + case "base64": + return base64ToBytes(string).length; + default: + if (loweredCase) + return mustMatch ? -1 : utf8ToBytes(string).length; + encoding = ("" + encoding).toLowerCase(), loweredCase = true; + } +} +function slowToString(encoding, start, end) { + let loweredCase = false; + if (start === undefined || start < 0) + start = 0; + if (start > this.length) + return ""; + if (end === undefined || end > this.length) + end = this.length; + if (end <= 0) + return ""; + if (end >>>= 0, start >>>= 0, end <= start) + return ""; + if (!encoding) + encoding = "utf8"; + while (true) + switch (encoding) { + case "hex": + return hexSlice(this, start, end); + case "utf8": + case "utf-8": + return utf8Slice(this, start, end); + case "ascii": + return asciiSlice(this, start, end); + case "latin1": + case "binary": + return latin1Slice(this, start, end); + case "base64": + return base64Slice(this, start, end); + case "ucs2": + case "ucs-2": + case "utf16le": + case "utf-16le": + return utf16leSlice(this, start, end); + default: + if (loweredCase) + throw new TypeError("Unknown encoding: " + encoding); + encoding = (encoding + "").toLowerCase(), loweredCase = true; + } +} +function swap(b, n, m) { + let i2 = b[n]; + b[n] = b[m], b[m] = i2; +} +function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) { + if (buffer.length === 0) + return -1; + if (typeof byteOffset === "string") + encoding = byteOffset, byteOffset = 0; + else if (byteOffset > 2147483647) + byteOffset = 2147483647; + else if (byteOffset < -2147483648) + byteOffset = -2147483648; + if (byteOffset = +byteOffset, Number.isNaN(byteOffset)) + byteOffset = dir ? 0 : buffer.length - 1; + if (byteOffset < 0) + byteOffset = buffer.length + byteOffset; + if (byteOffset >= buffer.length) + if (dir) + return -1; + else + byteOffset = buffer.length - 1; + else if (byteOffset < 0) + if (dir) + byteOffset = 0; + else + return -1; + if (typeof val === "string") + val = Buffer2.from(val, encoding); + if (Buffer2.isBuffer(val)) { + if (val.length === 0) + return -1; + return arrayIndexOf(buffer, val, byteOffset, encoding, dir); + } else if (typeof val === "number") { + if (val = val & 255, typeof Uint8Array.prototype.indexOf === "function") + if (dir) + return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset); + else + return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset); + return arrayIndexOf(buffer, [val], byteOffset, encoding, dir); + } + throw new TypeError("val must be string, number or Buffer"); +} +function arrayIndexOf(arr, val, byteOffset, encoding, dir) { + let indexSize = 1, arrLength = arr.length, valLength = val.length; + if (encoding !== undefined) { + if (encoding = String(encoding).toLowerCase(), encoding === "ucs2" || encoding === "ucs-2" || encoding === "utf16le" || encoding === "utf-16le") { + if (arr.length < 2 || val.length < 2) + return -1; + indexSize = 2, arrLength /= 2, valLength /= 2, byteOffset /= 2; + } + } + function read2(buf, i3) { + if (indexSize === 1) + return buf[i3]; + else + return buf.readUInt16BE(i3 * indexSize); + } + let i2; + if (dir) { + let foundIndex = -1; + for (i2 = byteOffset;i2 < arrLength; i2++) + if (read2(arr, i2) === read2(val, foundIndex === -1 ? 0 : i2 - foundIndex)) { + if (foundIndex === -1) + foundIndex = i2; + if (i2 - foundIndex + 1 === valLength) + return foundIndex * indexSize; + } else { + if (foundIndex !== -1) + i2 -= i2 - foundIndex; + foundIndex = -1; + } + } else { + if (byteOffset + valLength > arrLength) + byteOffset = arrLength - valLength; + for (i2 = byteOffset;i2 >= 0; i2--) { + let found = true; + for (let j = 0;j < valLength; j++) + if (read2(arr, i2 + j) !== read2(val, j)) { + found = false; break; } - return n < i ? -1 : i < n ? 1 : 0; - }; - f.isEncoding = function(t) { - switch (String(t).toLowerCase()) { + if (found) + return i2; + } + } + return -1; +} +function hexWrite(buf, string, offset, length) { + offset = Number(offset) || 0; + let remaining = buf.length - offset; + if (!length) + length = remaining; + else if (length = Number(length), length > remaining) + length = remaining; + let strLen = string.length; + if (length > strLen / 2) + length = strLen / 2; + let i2; + for (i2 = 0;i2 < length; ++i2) { + let parsed = parseInt(string.substr(i2 * 2, 2), 16); + if (Number.isNaN(parsed)) + return i2; + buf[offset + i2] = parsed; + } + return i2; +} +function utf8Write(buf, string, offset, length) { + return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length); +} +function asciiWrite(buf, string, offset, length) { + return blitBuffer(asciiToBytes(string), buf, offset, length); +} +function base64Write(buf, string, offset, length) { + return blitBuffer(base64ToBytes(string), buf, offset, length); +} +function ucs2Write(buf, string, offset, length) { + return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length); +} +function base64Slice(buf, start, end) { + if (start === 0 && end === buf.length) + return fromByteArray(buf); + else + return fromByteArray(buf.slice(start, end)); +} +function utf8Slice(buf, start, end) { + end = Math.min(buf.length, end); + let res = [], i2 = start; + while (i2 < end) { + let firstByte = buf[i2], codePoint = null, bytesPerSequence = firstByte > 239 ? 4 : firstByte > 223 ? 3 : firstByte > 191 ? 2 : 1; + if (i2 + bytesPerSequence <= end) { + let secondByte, thirdByte, fourthByte, tempCodePoint; + switch (bytesPerSequence) { + case 1: + if (firstByte < 128) + codePoint = firstByte; + break; + case 2: + if (secondByte = buf[i2 + 1], (secondByte & 192) === 128) { + if (tempCodePoint = (firstByte & 31) << 6 | secondByte & 63, tempCodePoint > 127) + codePoint = tempCodePoint; + } + break; + case 3: + if (secondByte = buf[i2 + 1], thirdByte = buf[i2 + 2], (secondByte & 192) === 128 && (thirdByte & 192) === 128) { + if (tempCodePoint = (firstByte & 15) << 12 | (secondByte & 63) << 6 | thirdByte & 63, tempCodePoint > 2047 && (tempCodePoint < 55296 || tempCodePoint > 57343)) + codePoint = tempCodePoint; + } + break; + case 4: + if (secondByte = buf[i2 + 1], thirdByte = buf[i2 + 2], fourthByte = buf[i2 + 3], (secondByte & 192) === 128 && (thirdByte & 192) === 128 && (fourthByte & 192) === 128) { + if (tempCodePoint = (firstByte & 15) << 18 | (secondByte & 63) << 12 | (thirdByte & 63) << 6 | fourthByte & 63, tempCodePoint > 65535 && tempCodePoint < 1114112) + codePoint = tempCodePoint; + } + } + } + if (codePoint === null) + codePoint = 65533, bytesPerSequence = 1; + else if (codePoint > 65535) + codePoint -= 65536, res.push(codePoint >>> 10 & 1023 | 55296), codePoint = 56320 | codePoint & 1023; + res.push(codePoint), i2 += bytesPerSequence; + } + return decodeCodePointsArray(res); +} +function decodeCodePointsArray(codePoints) { + let len2 = codePoints.length; + if (len2 <= MAX_ARGUMENTS_LENGTH) + return String.fromCharCode.apply(String, codePoints); + let res = "", i2 = 0; + while (i2 < len2) + res += String.fromCharCode.apply(String, codePoints.slice(i2, i2 += MAX_ARGUMENTS_LENGTH)); + return res; +} +function asciiSlice(buf, start, end) { + let ret = ""; + end = Math.min(buf.length, end); + for (let i2 = start;i2 < end; ++i2) + ret += String.fromCharCode(buf[i2] & 127); + return ret; +} +function latin1Slice(buf, start, end) { + let ret = ""; + end = Math.min(buf.length, end); + for (let i2 = start;i2 < end; ++i2) + ret += String.fromCharCode(buf[i2]); + return ret; +} +function hexSlice(buf, start, end) { + let len2 = buf.length; + if (!start || start < 0) + start = 0; + if (!end || end < 0 || end > len2) + end = len2; + let out = ""; + for (let i2 = start;i2 < end; ++i2) + out += hexSliceLookupTable[buf[i2]]; + return out; +} +function utf16leSlice(buf, start, end) { + let bytes = buf.slice(start, end), res = ""; + for (let i2 = 0;i2 < bytes.length - 1; i2 += 2) + res += String.fromCharCode(bytes[i2] + bytes[i2 + 1] * 256); + return res; +} +function checkOffset(offset, ext, length) { + if (offset % 1 !== 0 || offset < 0) + throw new RangeError("offset is not uint"); + if (offset + ext > length) + throw new RangeError("Trying to access beyond buffer length"); +} +function checkInt(buf, value, offset, ext, max, min) { + if (!Buffer2.isBuffer(buf)) + throw new TypeError('"buffer" argument must be a Buffer instance'); + if (value > max || value < min) + throw new RangeError('"value" argument is out of bounds'); + if (offset + ext > buf.length) + throw new RangeError("Index out of range"); +} +function wrtBigUInt64LE(buf, value, offset, min, max) { + checkIntBI(value, min, max, buf, offset, 7); + let lo = Number(value & BigInt(4294967295)); + buf[offset++] = lo, lo = lo >> 8, buf[offset++] = lo, lo = lo >> 8, buf[offset++] = lo, lo = lo >> 8, buf[offset++] = lo; + let hi = Number(value >> BigInt(32) & BigInt(4294967295)); + return buf[offset++] = hi, hi = hi >> 8, buf[offset++] = hi, hi = hi >> 8, buf[offset++] = hi, hi = hi >> 8, buf[offset++] = hi, offset; +} +function wrtBigUInt64BE(buf, value, offset, min, max) { + checkIntBI(value, min, max, buf, offset, 7); + let lo = Number(value & BigInt(4294967295)); + buf[offset + 7] = lo, lo = lo >> 8, buf[offset + 6] = lo, lo = lo >> 8, buf[offset + 5] = lo, lo = lo >> 8, buf[offset + 4] = lo; + let hi = Number(value >> BigInt(32) & BigInt(4294967295)); + return buf[offset + 3] = hi, hi = hi >> 8, buf[offset + 2] = hi, hi = hi >> 8, buf[offset + 1] = hi, hi = hi >> 8, buf[offset] = hi, offset + 8; +} +function checkIEEE754(buf, value, offset, ext, max, min) { + if (offset + ext > buf.length) + throw new RangeError("Index out of range"); + if (offset < 0) + throw new RangeError("Index out of range"); +} +function writeFloat(buf, value, offset, littleEndian, noAssert) { + if (value = +value, offset = offset >>> 0, !noAssert) + checkIEEE754(buf, value, offset, 4, 340282346638528860000000000000000000000, -340282346638528860000000000000000000000); + return write(buf, value, offset, littleEndian, 23, 4), offset + 4; +} +function writeDouble(buf, value, offset, littleEndian, noAssert) { + if (value = +value, offset = offset >>> 0, !noAssert) + checkIEEE754(buf, value, offset, 8, 179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, -179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000); + return write(buf, value, offset, littleEndian, 52, 8), offset + 8; +} +function addNumericalSeparator(val) { + let res = "", i2 = val.length, start = val[0] === "-" ? 1 : 0; + for (;i2 >= start + 4; i2 -= 3) + res = `_${val.slice(i2 - 3, i2)}${res}`; + return `${val.slice(0, i2)}${res}`; +} +function checkBounds(buf, offset, byteLength2) { + if (validateNumber(offset, "offset"), buf[offset] === undefined || buf[offset + byteLength2] === undefined) + boundsError(offset, buf.length - (byteLength2 + 1)); +} +function checkIntBI(value, min, max, buf, offset, byteLength2) { + if (value > max || value < min) { + let n = typeof min === "bigint" ? "n" : "", range; + if (byteLength2 > 3) + if (min === 0 || min === BigInt(0)) + range = `>= 0${n} and < 2${n} ** ${(byteLength2 + 1) * 8}${n}`; + else + range = `>= -(2${n} ** ${(byteLength2 + 1) * 8 - 1}${n}) and < 2 ** ${(byteLength2 + 1) * 8 - 1}${n}`; + else + range = `>= ${min}${n} and <= ${max}${n}`; + throw new ERR_OUT_OF_RANGE("value", range, value); + } + checkBounds(buf, offset, byteLength2); +} +function validateNumber(value, name) { + if (typeof value !== "number") + throw new ERR_INVALID_ARG_TYPE(name, "number", value); +} +function boundsError(value, length, type) { + if (Math.floor(value) !== value) + throw validateNumber(value, type), new ERR_OUT_OF_RANGE(type || "offset", "an integer", value); + if (length < 0) + throw new ERR_BUFFER_OUT_OF_BOUNDS; + throw new ERR_OUT_OF_RANGE(type || "offset", `>= ${type ? 1 : 0} and <= ${length}`, value); +} +function base64clean(str) { + if (str = str.split("=")[0], str = str.trim().replace(INVALID_BASE64_RE, ""), str.length < 2) + return ""; + while (str.length % 4 !== 0) + str = str + "="; + return str; +} +function utf8ToBytes(string, units) { + units = units || 1 / 0; + let codePoint, length = string.length, leadSurrogate = null, bytes = []; + for (let i2 = 0;i2 < length; ++i2) { + if (codePoint = string.charCodeAt(i2), codePoint > 55295 && codePoint < 57344) { + if (!leadSurrogate) { + if (codePoint > 56319) { + if ((units -= 3) > -1) + bytes.push(239, 191, 189); + continue; + } else if (i2 + 1 === length) { + if ((units -= 3) > -1) + bytes.push(239, 191, 189); + continue; + } + leadSurrogate = codePoint; + continue; + } + if (codePoint < 56320) { + if ((units -= 3) > -1) + bytes.push(239, 191, 189); + leadSurrogate = codePoint; + continue; + } + codePoint = (leadSurrogate - 55296 << 10 | codePoint - 56320) + 65536; + } else if (leadSurrogate) { + if ((units -= 3) > -1) + bytes.push(239, 191, 189); + } + if (leadSurrogate = null, codePoint < 128) { + if ((units -= 1) < 0) + break; + bytes.push(codePoint); + } else if (codePoint < 2048) { + if ((units -= 2) < 0) + break; + bytes.push(codePoint >> 6 | 192, codePoint & 63 | 128); + } else if (codePoint < 65536) { + if ((units -= 3) < 0) + break; + bytes.push(codePoint >> 12 | 224, codePoint >> 6 & 63 | 128, codePoint & 63 | 128); + } else if (codePoint < 1114112) { + if ((units -= 4) < 0) + break; + bytes.push(codePoint >> 18 | 240, codePoint >> 12 & 63 | 128, codePoint >> 6 & 63 | 128, codePoint & 63 | 128); + } else + throw new Error("Invalid code point"); + } + return bytes; +} +function asciiToBytes(str) { + let byteArray = []; + for (let i2 = 0;i2 < str.length; ++i2) + byteArray.push(str.charCodeAt(i2) & 255); + return byteArray; +} +function utf16leToBytes(str, units) { + let c, hi, lo, byteArray = []; + for (let i2 = 0;i2 < str.length; ++i2) { + if ((units -= 2) < 0) + break; + c = str.charCodeAt(i2), hi = c >> 8, lo = c % 256, byteArray.push(lo), byteArray.push(hi); + } + return byteArray; +} +function base64ToBytes(str) { + return toByteArray(base64clean(str)); +} +function blitBuffer(src, dst, offset, length) { + let i2; + for (i2 = 0;i2 < length; ++i2) { + if (i2 + offset >= dst.length || i2 >= src.length) + break; + dst[i2 + offset] = src[i2]; + } + return i2; +} +function isInstance(obj, type) { + return obj instanceof type || obj != null && obj.constructor != null && obj.constructor.name != null && obj.constructor.name === type.name; +} +function defineBigIntMethod(fn) { + return typeof BigInt === "undefined" ? BufferBigIntNotDefined : fn; +} +function BufferBigIntNotDefined() { + throw new Error("BigInt not supported"); +} +function notimpl(name) { + return () => { + throw new Error(name + " is not implemented for node:buffer browser polyfill"); + }; +} +var lookup, revLookup, code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", i, len, customInspectSymbol, INSPECT_MAX_BYTES = 50, kMaxLength = 2147483647, kStringMaxLength = 536870888, btoa, atob, File, Blob2, constants, ERR_BUFFER_OUT_OF_BOUNDS, ERR_INVALID_ARG_TYPE, ERR_OUT_OF_RANGE, MAX_ARGUMENTS_LENGTH = 4096, INVALID_BASE64_RE, hexSliceLookupTable, resolveObjectURL, isUtf8, isAscii = (str) => { + for (let char of str) + if (char.charCodeAt(0) > 127) + return false; + return true; +}, transcode, buffer_default; +var init_buffer = __esm(() => { + lookup = []; + revLookup = []; + for (i = 0, len = code.length;i < len; ++i) + lookup[i] = code[i], revLookup[code.charCodeAt(i)] = i; + revLookup[45] = 62; + revLookup[95] = 63; + customInspectSymbol = typeof Symbol === "function" && typeof Symbol.for === "function" ? Symbol.for("nodejs.util.inspect.custom") : null; + btoa = globalThis.btoa; + atob = globalThis.atob; + File = globalThis.File; + Blob2 = globalThis.Blob; + constants = { MAX_LENGTH: kMaxLength, MAX_STRING_LENGTH: kStringMaxLength }; + ERR_BUFFER_OUT_OF_BOUNDS = E("ERR_BUFFER_OUT_OF_BOUNDS", function(name) { + if (name) + return `${name} is outside of buffer bounds`; + return "Attempt to access memory outside buffer bounds"; + }, RangeError); + ERR_INVALID_ARG_TYPE = E("ERR_INVALID_ARG_TYPE", function(name, actual) { + return `The "${name}" argument must be of type number. Received type ${typeof actual}`; + }, TypeError); + ERR_OUT_OF_RANGE = E("ERR_OUT_OF_RANGE", function(str, range, input) { + let msg = `The value of "${str}" is out of range.`, received = input; + if (Number.isInteger(input) && Math.abs(input) > 4294967296) + received = addNumericalSeparator(String(input)); + else if (typeof input === "bigint") { + if (received = String(input), input > BigInt(2) ** BigInt(32) || input < -(BigInt(2) ** BigInt(32))) + received = addNumericalSeparator(received); + received += "n"; + } + return msg += ` It must be ${range}. Received ${received}`, msg; + }, RangeError); + Object.defineProperty(Buffer2.prototype, "parent", { enumerable: true, get: function() { + if (!Buffer2.isBuffer(this)) + return; + return this.buffer; + } }); + Object.defineProperty(Buffer2.prototype, "offset", { enumerable: true, get: function() { + if (!Buffer2.isBuffer(this)) + return; + return this.byteOffset; + } }); + Buffer2.poolSize = 8192; + Buffer2.from = function(value, encodingOrOffset, length) { + return from(value, encodingOrOffset, length); + }; + Object.setPrototypeOf(Buffer2.prototype, Uint8Array.prototype); + Object.setPrototypeOf(Buffer2, Uint8Array); + Buffer2.alloc = function(size, fill, encoding) { + return alloc(size, fill, encoding); + }; + Buffer2.allocUnsafe = function(size) { + return allocUnsafe(size); + }; + Buffer2.allocUnsafeSlow = function(size) { + return allocUnsafe(size); + }; + Buffer2.isBuffer = function isBuffer(b) { + return b != null && b._isBuffer === true && b !== Buffer2.prototype; + }; + Buffer2.compare = function compare(a, b) { + if (isInstance(a, Uint8Array)) + a = Buffer2.from(a, a.offset, a.byteLength); + if (isInstance(b, Uint8Array)) + b = Buffer2.from(b, b.offset, b.byteLength); + if (!Buffer2.isBuffer(a) || !Buffer2.isBuffer(b)) + throw new TypeError('The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array'); + if (a === b) + return 0; + let x = a.length, y = b.length; + for (let i2 = 0, len2 = Math.min(x, y);i2 < len2; ++i2) + if (a[i2] !== b[i2]) { + x = a[i2], y = b[i2]; + break; + } + if (x < y) + return -1; + if (y < x) + return 1; + return 0; + }; + Buffer2.isEncoding = function isEncoding(encoding) { + switch (String(encoding).toLowerCase()) { + case "hex": + case "utf8": + case "utf-8": + case "ascii": + case "latin1": + case "binary": + case "base64": + case "ucs2": + case "ucs-2": + case "utf16le": + case "utf-16le": + return true; + default: + return false; + } + }; + Buffer2.concat = function concat(list, length) { + if (!Array.isArray(list)) + throw new TypeError('"list" argument must be an Array of Buffers'); + if (list.length === 0) + return Buffer2.alloc(0); + let i2; + if (length === undefined) { + length = 0; + for (i2 = 0;i2 < list.length; ++i2) + length += list[i2].length; + } + let buffer = Buffer2.allocUnsafe(length), pos = 0; + for (i2 = 0;i2 < list.length; ++i2) { + let buf = list[i2]; + if (isInstance(buf, Uint8Array)) + if (pos + buf.length > buffer.length) { + if (!Buffer2.isBuffer(buf)) + buf = Buffer2.from(buf); + buf.copy(buffer, pos); + } else + Uint8Array.prototype.set.call(buffer, buf, pos); + else if (!Buffer2.isBuffer(buf)) + throw new TypeError('"list" argument must be an Array of Buffers'); + else + buf.copy(buffer, pos); + pos += buf.length; + } + return buffer; + }; + Buffer2.byteLength = byteLength; + Buffer2.prototype._isBuffer = true; + Buffer2.prototype.swap16 = function swap16() { + let len2 = this.length; + if (len2 % 2 !== 0) + throw new RangeError("Buffer size must be a multiple of 16-bits"); + for (let i2 = 0;i2 < len2; i2 += 2) + swap(this, i2, i2 + 1); + return this; + }; + Buffer2.prototype.swap32 = function swap32() { + let len2 = this.length; + if (len2 % 4 !== 0) + throw new RangeError("Buffer size must be a multiple of 32-bits"); + for (let i2 = 0;i2 < len2; i2 += 4) + swap(this, i2, i2 + 3), swap(this, i2 + 1, i2 + 2); + return this; + }; + Buffer2.prototype.swap64 = function swap64() { + let len2 = this.length; + if (len2 % 8 !== 0) + throw new RangeError("Buffer size must be a multiple of 64-bits"); + for (let i2 = 0;i2 < len2; i2 += 8) + swap(this, i2, i2 + 7), swap(this, i2 + 1, i2 + 6), swap(this, i2 + 2, i2 + 5), swap(this, i2 + 3, i2 + 4); + return this; + }; + Buffer2.prototype.toString = function toString() { + let length = this.length; + if (length === 0) + return ""; + if (arguments.length === 0) + return utf8Slice(this, 0, length); + return slowToString.apply(this, arguments); + }; + Buffer2.prototype.toLocaleString = Buffer2.prototype.toString; + Buffer2.prototype.equals = function equals(b) { + if (!Buffer2.isBuffer(b)) + throw new TypeError("Argument must be a Buffer"); + if (this === b) + return true; + return Buffer2.compare(this, b) === 0; + }; + Buffer2.prototype.inspect = function inspect() { + let str = "", max = INSPECT_MAX_BYTES; + if (str = this.toString("hex", 0, max).replace(/(.{2})/g, "$1 ").trim(), this.length > max) + str += " ... "; + return ""; + }; + if (customInspectSymbol) + Buffer2.prototype[customInspectSymbol] = Buffer2.prototype.inspect; + Buffer2.prototype.compare = function compare2(target, start, end, thisStart, thisEnd) { + if (isInstance(target, Uint8Array)) + target = Buffer2.from(target, target.offset, target.byteLength); + if (!Buffer2.isBuffer(target)) + throw new TypeError('The "target" argument must be one of type Buffer or Uint8Array. Received type ' + typeof target); + if (start === undefined) + start = 0; + if (end === undefined) + end = target ? target.length : 0; + if (thisStart === undefined) + thisStart = 0; + if (thisEnd === undefined) + thisEnd = this.length; + if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) + throw new RangeError("out of range index"); + if (thisStart >= thisEnd && start >= end) + return 0; + if (thisStart >= thisEnd) + return -1; + if (start >= end) + return 1; + if (start >>>= 0, end >>>= 0, thisStart >>>= 0, thisEnd >>>= 0, this === target) + return 0; + let x = thisEnd - thisStart, y = end - start, len2 = Math.min(x, y), thisCopy = this.slice(thisStart, thisEnd), targetCopy = target.slice(start, end); + for (let i2 = 0;i2 < len2; ++i2) + if (thisCopy[i2] !== targetCopy[i2]) { + x = thisCopy[i2], y = targetCopy[i2]; + break; + } + if (x < y) + return -1; + if (y < x) + return 1; + return 0; + }; + Buffer2.prototype.includes = function includes(val, byteOffset, encoding) { + return this.indexOf(val, byteOffset, encoding) !== -1; + }; + Buffer2.prototype.indexOf = function indexOf(val, byteOffset, encoding) { + return bidirectionalIndexOf(this, val, byteOffset, encoding, true); + }; + Buffer2.prototype.lastIndexOf = function lastIndexOf(val, byteOffset, encoding) { + return bidirectionalIndexOf(this, val, byteOffset, encoding, false); + }; + Buffer2.prototype.write = function write2(string, offset, length, encoding) { + if (offset === undefined) + encoding = "utf8", length = this.length, offset = 0; + else if (length === undefined && typeof offset === "string") + encoding = offset, length = this.length, offset = 0; + else if (isFinite(offset)) + if (offset = offset >>> 0, isFinite(length)) { + if (length = length >>> 0, encoding === undefined) + encoding = "utf8"; + } else + encoding = length, length = undefined; + else + throw new Error("Buffer.write(string, encoding, offset[, length]) is no longer supported"); + let remaining = this.length - offset; + if (length === undefined || length > remaining) + length = remaining; + if (string.length > 0 && (length < 0 || offset < 0) || offset > this.length) + throw new RangeError("Attempt to write outside buffer bounds"); + if (!encoding) + encoding = "utf8"; + let loweredCase = false; + for (;; ) + switch (encoding) { case "hex": + return hexWrite(this, string, offset, length); case "utf8": case "utf-8": + return utf8Write(this, string, offset, length); case "ascii": case "latin1": case "binary": + return asciiWrite(this, string, offset, length); case "base64": + return base64Write(this, string, offset, length); case "ucs2": case "ucs-2": case "utf16le": case "utf-16le": - return true; + return ucs2Write(this, string, offset, length); default: - return false; - } - }; - f.concat = function(t, r) { - if (!Array.isArray(t)) - throw new TypeError('"list" argument must be an Array of Buffers'); - if (t.length === 0) - return f.alloc(0); - let n; - if (r === undefined) - for (r = 0, n = 0;n < t.length; ++n) - r += t[n].length; - let i = f.allocUnsafe(r), o = 0; - for (n = 0;n < t.length; ++n) { - let l = t[n]; - if (z(l, Uint8Array)) - o + l.length > i.length ? (f.isBuffer(l) || (l = f.from(l)), l.copy(i, o)) : Uint8Array.prototype.set.call(i, l, o); - else if (f.isBuffer(l)) - l.copy(i, o); - else - throw new TypeError('"list" argument must be an Array of Buffers'); - o += l.length; - } - return i; - }; - function Gn(e, t) { - if (f.isBuffer(e)) - return e.length; - if (ArrayBuffer.isView(e) || z(e, ArrayBuffer)) - return e.byteLength; - if (typeof e != "string") - throw new TypeError('The "string" argument must be one of type string, Buffer, or ArrayBuffer. Received type ' + typeof e); - let r = e.length, n = arguments.length > 2 && arguments[2] === true; - if (!n && r === 0) - return 0; - let i = false; - for (;; ) - switch (t) { - case "ascii": - case "latin1": - case "binary": - return r; - case "utf8": - case "utf-8": - return cr(e).length; - case "ucs2": - case "ucs-2": - case "utf16le": - case "utf-16le": - return r * 2; - case "hex": - return r >>> 1; - case "base64": - return ei(e).length; - default: - if (i) - return n ? -1 : cr(e).length; - t = ("" + t).toLowerCase(), i = true; - } - } - f.byteLength = Gn; - function Mu(e, t, r) { - let n = false; - if ((t === undefined || t < 0) && (t = 0), t > this.length || ((r === undefined || r > this.length) && (r = this.length), r <= 0) || (r >>>= 0, t >>>= 0, r <= t)) - return ""; - for (e || (e = "utf8");; ) - switch (e) { - case "hex": - return $u(this, t, r); - case "utf8": - case "utf-8": - return Yn(this, t, r); - case "ascii": - return Uu(this, t, r); - case "latin1": - case "binary": - return Wu(this, t, r); - case "base64": - return qu(this, t, r); - case "ucs2": - case "ucs-2": - case "utf16le": - case "utf-16le": - return ju(this, t, r); - default: - if (n) - throw new TypeError("Unknown encoding: " + e); - e = (e + "").toLowerCase(), n = true; - } - } - f.prototype._isBuffer = true; - function Re(e, t, r) { - let n = e[t]; - e[t] = e[r], e[r] = n; - } - f.prototype.swap16 = function() { - let t = this.length; - if (t % 2 !== 0) - throw new RangeError("Buffer size must be a multiple of 16-bits"); - for (let r = 0;r < t; r += 2) - Re(this, r, r + 1); - return this; - }; - f.prototype.swap32 = function() { - let t = this.length; - if (t % 4 !== 0) - throw new RangeError("Buffer size must be a multiple of 32-bits"); - for (let r = 0;r < t; r += 4) - Re(this, r, r + 3), Re(this, r + 1, r + 2); - return this; - }; - f.prototype.swap64 = function() { - let t = this.length; - if (t % 8 !== 0) - throw new RangeError("Buffer size must be a multiple of 64-bits"); - for (let r = 0;r < t; r += 8) - Re(this, r, r + 7), Re(this, r + 1, r + 6), Re(this, r + 2, r + 5), Re(this, r + 3, r + 4); - return this; - }; - f.prototype.toString = function() { - let t = this.length; - return t === 0 ? "" : arguments.length === 0 ? Yn(this, 0, t) : Mu.apply(this, arguments); - }; - f.prototype.toLocaleString = f.prototype.toString; - f.prototype.equals = function(t) { - if (!f.isBuffer(t)) - throw new TypeError("Argument must be a Buffer"); - return this === t ? true : f.compare(this, t) === 0; - }; - f.prototype.inspect = function() { - let t = "", r = Ue.INSPECT_MAX_BYTES; - return t = this.toString("hex", 0, r).replace(/(.{2})/g, "$1 ").trim(), this.length > r && (t += " ... "), ""; - }; - vn && (f.prototype[vn] = f.prototype.inspect); - f.prototype.compare = function(t, r, n, i, o) { - if (z(t, Uint8Array) && (t = f.from(t, t.offset, t.byteLength)), !f.isBuffer(t)) - throw new TypeError('The "target" argument must be one of type Buffer or Uint8Array. Received type ' + typeof t); - if (r === undefined && (r = 0), n === undefined && (n = t ? t.length : 0), i === undefined && (i = 0), o === undefined && (o = this.length), r < 0 || n > t.length || i < 0 || o > this.length) - throw new RangeError("out of range index"); - if (i >= o && r >= n) - return 0; - if (i >= o) - return -1; - if (r >= n) - return 1; - if (r >>>= 0, n >>>= 0, i >>>= 0, o >>>= 0, this === t) - return 0; - let l = o - i, u = n - r, a = Math.min(l, u), s = this.slice(i, o), c = t.slice(r, n); - for (let d = 0;d < a; ++d) - if (s[d] !== c[d]) { - l = s[d], u = c[d]; - break; - } - return l < u ? -1 : u < l ? 1 : 0; - }; - function Vn(e, t, r, n, i) { - if (e.length === 0) - return -1; - if (typeof r == "string" ? (n = r, r = 0) : r > 2147483647 ? r = 2147483647 : r < -2147483648 && (r = -2147483648), r = +r, yr(r) && (r = i ? 0 : e.length - 1), r < 0 && (r = e.length + r), r >= e.length) { - if (i) - return -1; - r = e.length - 1; - } else if (r < 0) - if (i) - r = 0; - else - return -1; - if (typeof t == "string" && (t = f.from(t, n)), f.isBuffer(t)) - return t.length === 0 ? -1 : Un(e, t, r, n, i); - if (typeof t == "number") - return t = t & 255, typeof Uint8Array.prototype.indexOf == "function" ? i ? Uint8Array.prototype.indexOf.call(e, t, r) : Uint8Array.prototype.lastIndexOf.call(e, t, r) : Un(e, [t], r, n, i); - throw new TypeError("val must be string, number or Buffer"); - } - function Un(e, t, r, n, i) { - let o = 1, l = e.length, u = t.length; - if (n !== undefined && (n = String(n).toLowerCase(), n === "ucs2" || n === "ucs-2" || n === "utf16le" || n === "utf-16le")) { - if (e.length < 2 || t.length < 2) - return -1; - o = 2, l /= 2, u /= 2, r /= 2; - } - function a(c, d) { - return o === 1 ? c[d] : c.readUInt16BE(d * o); - } - let s; - if (i) { - let c = -1; - for (s = r;s < l; s++) - if (a(e, s) === a(t, c === -1 ? 0 : s - c)) { - if (c === -1 && (c = s), s - c + 1 === u) - return c * o; - } else - c !== -1 && (s -= s - c), c = -1; - } else - for (r + u > l && (r = l - u), s = r;s >= 0; s--) { - let c = true; - for (let d = 0;d < u; d++) - if (a(e, s + d) !== a(t, d)) { - c = false; - break; - } - if (c) - return s; - } - return -1; - } - f.prototype.includes = function(t, r, n) { - return this.indexOf(t, r, n) !== -1; - }; - f.prototype.indexOf = function(t, r, n) { - return Vn(this, t, r, n, true); - }; - f.prototype.lastIndexOf = function(t, r, n) { - return Vn(this, t, r, n, false); - }; - function Cu(e, t, r, n) { - r = Number(r) || 0; - let i = e.length - r; - n ? (n = Number(n), n > i && (n = i)) : n = i; - let o = t.length; - n > o / 2 && (n = o / 2); - let l; - for (l = 0;l < n; ++l) { - let u = parseInt(t.substr(l * 2, 2), 16); - if (yr(u)) - return l; - e[r + l] = u; - } - return l; - } - function ku(e, t, r, n) { - return mt(cr(t, e.length - r), e, r, n); - } - function Du(e, t, r, n) { - return mt(Yu(t), e, r, n); - } - function Ou(e, t, r, n) { - return mt(ei(t), e, r, n); - } - function Pu(e, t, r, n) { - return mt(Ku(t, e.length - r), e, r, n); - } - f.prototype.write = function(t, r, n, i) { - if (r === undefined) - i = "utf8", n = this.length, r = 0; - else if (n === undefined && typeof r == "string") - i = r, n = this.length, r = 0; - else if (isFinite(r)) - r = r >>> 0, isFinite(n) ? (n = n >>> 0, i === undefined && (i = "utf8")) : (i = n, n = undefined); - else - throw new Error("Buffer.write(string, encoding, offset[, length]) is no longer supported"); - let o = this.length - r; - if ((n === undefined || n > o) && (n = o), t.length > 0 && (n < 0 || r < 0) || r > this.length) - throw new RangeError("Attempt to write outside buffer bounds"); - i || (i = "utf8"); - let l = false; - for (;; ) - switch (i) { - case "hex": - return Cu(this, t, r, n); - case "utf8": - case "utf-8": - return ku(this, t, r, n); - case "ascii": - case "latin1": - case "binary": - return Du(this, t, r, n); - case "base64": - return Ou(this, t, r, n); - case "ucs2": - case "ucs-2": - case "utf16le": - case "utf-16le": - return Pu(this, t, r, n); - default: - if (l) - throw new TypeError("Unknown encoding: " + i); - i = ("" + i).toLowerCase(), l = true; - } - }; - f.prototype.toJSON = function() { - return { type: "Buffer", data: Array.prototype.slice.call(this._arr || this, 0) }; - }; - function qu(e, t, r) { - return t === 0 && r === e.length ? sr.fromByteArray(e) : sr.fromByteArray(e.slice(t, r)); - } - function Yn(e, t, r) { - r = Math.min(e.length, r); - let n = [], i = t; - for (;i < r; ) { - let o = e[i], l = null, u = o > 239 ? 4 : o > 223 ? 3 : o > 191 ? 2 : 1; - if (i + u <= r) { - let a, s, c, d; - switch (u) { - case 1: - o < 128 && (l = o); - break; - case 2: - a = e[i + 1], (a & 192) === 128 && (d = (o & 31) << 6 | a & 63, d > 127 && (l = d)); - break; - case 3: - a = e[i + 1], s = e[i + 2], (a & 192) === 128 && (s & 192) === 128 && (d = (o & 15) << 12 | (a & 63) << 6 | s & 63, d > 2047 && (d < 55296 || d > 57343) && (l = d)); - break; - case 4: - a = e[i + 1], s = e[i + 2], c = e[i + 3], (a & 192) === 128 && (s & 192) === 128 && (c & 192) === 128 && (d = (o & 15) << 18 | (a & 63) << 12 | (s & 63) << 6 | c & 63, d > 65535 && d < 1114112 && (l = d)); - } - } - l === null ? (l = 65533, u = 1) : l > 65535 && (l -= 65536, n.push(l >>> 10 & 1023 | 55296), l = 56320 | l & 1023), n.push(l), i += u; - } - return vu(n); - } - var Wn = 4096; - function vu(e) { - let t = e.length; - if (t <= Wn) - return String.fromCharCode.apply(String, e); - let r = "", n = 0; - for (;n < t; ) - r += String.fromCharCode.apply(String, e.slice(n, n += Wn)); - return r; - } - function Uu(e, t, r) { - let n = ""; - r = Math.min(e.length, r); - for (let i = t;i < r; ++i) - n += String.fromCharCode(e[i] & 127); - return n; - } - function Wu(e, t, r) { - let n = ""; - r = Math.min(e.length, r); - for (let i = t;i < r; ++i) - n += String.fromCharCode(e[i]); - return n; - } - function $u(e, t, r) { - let n = e.length; - (!t || t < 0) && (t = 0), (!r || r < 0 || r > n) && (r = n); - let i = ""; - for (let o = t;o < r; ++o) - i += zu[e[o]]; - return i; - } - function ju(e, t, r) { - let n = e.slice(t, r), i = ""; - for (let o = 0;o < n.length - 1; o += 2) - i += String.fromCharCode(n[o] + n[o + 1] * 256); - return i; - } - f.prototype.slice = function(t, r) { - let n = this.length; - t = ~~t, r = r === undefined ? n : ~~r, t < 0 ? (t += n, t < 0 && (t = 0)) : t > n && (t = n), r < 0 ? (r += n, r < 0 && (r = 0)) : r > n && (r = n), r < t && (r = t); - let i = this.subarray(t, r); - return Object.setPrototypeOf(i, f.prototype), i; - }; - function k(e, t, r) { - if (e % 1 !== 0 || e < 0) - throw new RangeError("offset is not uint"); - if (e + t > r) - throw new RangeError("Trying to access beyond buffer length"); - } - f.prototype.readUintLE = f.prototype.readUIntLE = function(t, r, n) { - t = t >>> 0, r = r >>> 0, n || k(t, r, this.length); - let i = this[t], o = 1, l = 0; - for (;++l < r && (o *= 256); ) - i += this[t + l] * o; - return i; - }; - f.prototype.readUintBE = f.prototype.readUIntBE = function(t, r, n) { - t = t >>> 0, r = r >>> 0, n || k(t, r, this.length); - let i = this[t + --r], o = 1; - for (;r > 0 && (o *= 256); ) - i += this[t + --r] * o; - return i; - }; - f.prototype.readUint8 = f.prototype.readUInt8 = function(t, r) { - return t = t >>> 0, r || k(t, 1, this.length), this[t]; - }; - f.prototype.readUint16LE = f.prototype.readUInt16LE = function(t, r) { - return t = t >>> 0, r || k(t, 2, this.length), this[t] | this[t + 1] << 8; - }; - f.prototype.readUint16BE = f.prototype.readUInt16BE = function(t, r) { - return t = t >>> 0, r || k(t, 2, this.length), this[t] << 8 | this[t + 1]; - }; - f.prototype.readUint32LE = f.prototype.readUInt32LE = function(t, r) { - return t = t >>> 0, r || k(t, 4, this.length), (this[t] | this[t + 1] << 8 | this[t + 2] << 16) + this[t + 3] * 16777216; - }; - f.prototype.readUint32BE = f.prototype.readUInt32BE = function(t, r) { - return t = t >>> 0, r || k(t, 4, this.length), this[t] * 16777216 + (this[t + 1] << 16 | this[t + 2] << 8 | this[t + 3]); - }; - f.prototype.readBigUInt64LE = be(function(t) { - t = t >>> 0, ve(t, "offset"); - let r = this[t], n = this[t + 7]; - (r === undefined || n === undefined) && nt(t, this.length - 8); - let i = r + this[++t] * 2 ** 8 + this[++t] * 2 ** 16 + this[++t] * 2 ** 24, o = this[++t] + this[++t] * 2 ** 8 + this[++t] * 2 ** 16 + n * 2 ** 24; - return BigInt(i) + (BigInt(o) << BigInt(32)); - }); - f.prototype.readBigUInt64BE = be(function(t) { - t = t >>> 0, ve(t, "offset"); - let r = this[t], n = this[t + 7]; - (r === undefined || n === undefined) && nt(t, this.length - 8); - let i = r * 2 ** 24 + this[++t] * 2 ** 16 + this[++t] * 2 ** 8 + this[++t], o = this[++t] * 2 ** 24 + this[++t] * 2 ** 16 + this[++t] * 2 ** 8 + n; - return (BigInt(i) << BigInt(32)) + BigInt(o); - }); - f.prototype.readIntLE = function(t, r, n) { - t = t >>> 0, r = r >>> 0, n || k(t, r, this.length); - let i = this[t], o = 1, l = 0; - for (;++l < r && (o *= 256); ) - i += this[t + l] * o; - return o *= 128, i >= o && (i -= Math.pow(2, 8 * r)), i; - }; - f.prototype.readIntBE = function(t, r, n) { - t = t >>> 0, r = r >>> 0, n || k(t, r, this.length); - let i = r, o = 1, l = this[t + --i]; - for (;i > 0 && (o *= 256); ) - l += this[t + --i] * o; - return o *= 128, l >= o && (l -= Math.pow(2, 8 * r)), l; - }; - f.prototype.readInt8 = function(t, r) { - return t = t >>> 0, r || k(t, 1, this.length), this[t] & 128 ? (255 - this[t] + 1) * -1 : this[t]; - }; - f.prototype.readInt16LE = function(t, r) { - t = t >>> 0, r || k(t, 2, this.length); - let n = this[t] | this[t + 1] << 8; - return n & 32768 ? n | 4294901760 : n; - }; - f.prototype.readInt16BE = function(t, r) { - t = t >>> 0, r || k(t, 2, this.length); - let n = this[t + 1] | this[t] << 8; - return n & 32768 ? n | 4294901760 : n; - }; - f.prototype.readInt32LE = function(t, r) { - return t = t >>> 0, r || k(t, 4, this.length), this[t] | this[t + 1] << 8 | this[t + 2] << 16 | this[t + 3] << 24; - }; - f.prototype.readInt32BE = function(t, r) { - return t = t >>> 0, r || k(t, 4, this.length), this[t] << 24 | this[t + 1] << 16 | this[t + 2] << 8 | this[t + 3]; - }; - f.prototype.readBigInt64LE = be(function(t) { - t = t >>> 0, ve(t, "offset"); - let r = this[t], n = this[t + 7]; - (r === undefined || n === undefined) && nt(t, this.length - 8); - let i = this[t + 4] + this[t + 5] * 2 ** 8 + this[t + 6] * 2 ** 16 + (n << 24); - return (BigInt(i) << BigInt(32)) + BigInt(r + this[++t] * 2 ** 8 + this[++t] * 2 ** 16 + this[++t] * 2 ** 24); - }); - f.prototype.readBigInt64BE = be(function(t) { - t = t >>> 0, ve(t, "offset"); - let r = this[t], n = this[t + 7]; - (r === undefined || n === undefined) && nt(t, this.length - 8); - let i = (r << 24) + this[++t] * 2 ** 16 + this[++t] * 2 ** 8 + this[++t]; - return (BigInt(i) << BigInt(32)) + BigInt(this[++t] * 2 ** 24 + this[++t] * 2 ** 16 + this[++t] * 2 ** 8 + n); - }); - f.prototype.readFloatLE = function(t, r) { - return t = t >>> 0, r || k(t, 4, this.length), qe.read(this, t, true, 23, 4); - }; - f.prototype.readFloatBE = function(t, r) { - return t = t >>> 0, r || k(t, 4, this.length), qe.read(this, t, false, 23, 4); - }; - f.prototype.readDoubleLE = function(t, r) { - return t = t >>> 0, r || k(t, 8, this.length), qe.read(this, t, true, 52, 8); - }; - f.prototype.readDoubleBE = function(t, r) { - return t = t >>> 0, r || k(t, 8, this.length), qe.read(this, t, false, 52, 8); - }; - function U(e, t, r, n, i, o) { - if (!f.isBuffer(e)) - throw new TypeError('"buffer" argument must be a Buffer instance'); - if (t > i || t < o) - throw new RangeError('"value" argument is out of bounds'); - if (r + n > e.length) - throw new RangeError("Index out of range"); - } - f.prototype.writeUintLE = f.prototype.writeUIntLE = function(t, r, n, i) { - if (t = +t, r = r >>> 0, n = n >>> 0, !i) { - let u = Math.pow(2, 8 * n) - 1; - U(this, t, r, n, u, 0); - } - let o = 1, l = 0; - for (this[r] = t & 255;++l < n && (o *= 256); ) - this[r + l] = t / o & 255; - return r + n; - }; - f.prototype.writeUintBE = f.prototype.writeUIntBE = function(t, r, n, i) { - if (t = +t, r = r >>> 0, n = n >>> 0, !i) { - let u = Math.pow(2, 8 * n) - 1; - U(this, t, r, n, u, 0); - } - let o = n - 1, l = 1; - for (this[r + o] = t & 255;--o >= 0 && (l *= 256); ) - this[r + o] = t / l & 255; - return r + n; - }; - f.prototype.writeUint8 = f.prototype.writeUInt8 = function(t, r, n) { - return t = +t, r = r >>> 0, n || U(this, t, r, 1, 255, 0), this[r] = t & 255, r + 1; - }; - f.prototype.writeUint16LE = f.prototype.writeUInt16LE = function(t, r, n) { - return t = +t, r = r >>> 0, n || U(this, t, r, 2, 65535, 0), this[r] = t & 255, this[r + 1] = t >>> 8, r + 2; - }; - f.prototype.writeUint16BE = f.prototype.writeUInt16BE = function(t, r, n) { - return t = +t, r = r >>> 0, n || U(this, t, r, 2, 65535, 0), this[r] = t >>> 8, this[r + 1] = t & 255, r + 2; - }; - f.prototype.writeUint32LE = f.prototype.writeUInt32LE = function(t, r, n) { - return t = +t, r = r >>> 0, n || U(this, t, r, 4, 4294967295, 0), this[r + 3] = t >>> 24, this[r + 2] = t >>> 16, this[r + 1] = t >>> 8, this[r] = t & 255, r + 4; - }; - f.prototype.writeUint32BE = f.prototype.writeUInt32BE = function(t, r, n) { - return t = +t, r = r >>> 0, n || U(this, t, r, 4, 4294967295, 0), this[r] = t >>> 24, this[r + 1] = t >>> 16, this[r + 2] = t >>> 8, this[r + 3] = t & 255, r + 4; - }; - function Kn(e, t, r, n, i) { - Zn(t, n, i, e, r, 7); - let o = Number(t & BigInt(4294967295)); - e[r++] = o, o = o >> 8, e[r++] = o, o = o >> 8, e[r++] = o, o = o >> 8, e[r++] = o; - let l = Number(t >> BigInt(32) & BigInt(4294967295)); - return e[r++] = l, l = l >> 8, e[r++] = l, l = l >> 8, e[r++] = l, l = l >> 8, e[r++] = l, r; - } - function zn(e, t, r, n, i) { - Zn(t, n, i, e, r, 7); - let o = Number(t & BigInt(4294967295)); - e[r + 7] = o, o = o >> 8, e[r + 6] = o, o = o >> 8, e[r + 5] = o, o = o >> 8, e[r + 4] = o; - let l = Number(t >> BigInt(32) & BigInt(4294967295)); - return e[r + 3] = l, l = l >> 8, e[r + 2] = l, l = l >> 8, e[r + 1] = l, l = l >> 8, e[r] = l, r + 8; - } - f.prototype.writeBigUInt64LE = be(function(t, r = 0) { - return Kn(this, t, r, BigInt(0), BigInt("0xffffffffffffffff")); - }); - f.prototype.writeBigUInt64BE = be(function(t, r = 0) { - return zn(this, t, r, BigInt(0), BigInt("0xffffffffffffffff")); - }); - f.prototype.writeIntLE = function(t, r, n, i) { - if (t = +t, r = r >>> 0, !i) { - let a = Math.pow(2, 8 * n - 1); - U(this, t, r, n, a - 1, -a); - } - let o = 0, l = 1, u = 0; - for (this[r] = t & 255;++o < n && (l *= 256); ) - t < 0 && u === 0 && this[r + o - 1] !== 0 && (u = 1), this[r + o] = (t / l >> 0) - u & 255; - return r + n; - }; - f.prototype.writeIntBE = function(t, r, n, i) { - if (t = +t, r = r >>> 0, !i) { - let a = Math.pow(2, 8 * n - 1); - U(this, t, r, n, a - 1, -a); - } - let o = n - 1, l = 1, u = 0; - for (this[r + o] = t & 255;--o >= 0 && (l *= 256); ) - t < 0 && u === 0 && this[r + o + 1] !== 0 && (u = 1), this[r + o] = (t / l >> 0) - u & 255; - return r + n; - }; - f.prototype.writeInt8 = function(t, r, n) { - return t = +t, r = r >>> 0, n || U(this, t, r, 1, 127, -128), t < 0 && (t = 255 + t + 1), this[r] = t & 255, r + 1; - }; - f.prototype.writeInt16LE = function(t, r, n) { - return t = +t, r = r >>> 0, n || U(this, t, r, 2, 32767, -32768), this[r] = t & 255, this[r + 1] = t >>> 8, r + 2; - }; - f.prototype.writeInt16BE = function(t, r, n) { - return t = +t, r = r >>> 0, n || U(this, t, r, 2, 32767, -32768), this[r] = t >>> 8, this[r + 1] = t & 255, r + 2; - }; - f.prototype.writeInt32LE = function(t, r, n) { - return t = +t, r = r >>> 0, n || U(this, t, r, 4, 2147483647, -2147483648), this[r] = t & 255, this[r + 1] = t >>> 8, this[r + 2] = t >>> 16, this[r + 3] = t >>> 24, r + 4; - }; - f.prototype.writeInt32BE = function(t, r, n) { - return t = +t, r = r >>> 0, n || U(this, t, r, 4, 2147483647, -2147483648), t < 0 && (t = 4294967295 + t + 1), this[r] = t >>> 24, this[r + 1] = t >>> 16, this[r + 2] = t >>> 8, this[r + 3] = t & 255, r + 4; - }; - f.prototype.writeBigInt64LE = be(function(t, r = 0) { - return Kn(this, t, r, -BigInt("0x8000000000000000"), BigInt("0x7fffffffffffffff")); - }); - f.prototype.writeBigInt64BE = be(function(t, r = 0) { - return zn(this, t, r, -BigInt("0x8000000000000000"), BigInt("0x7fffffffffffffff")); - }); - function Xn(e, t, r, n, i, o) { - if (r + n > e.length) - throw new RangeError("Index out of range"); - if (r < 0) - throw new RangeError("Index out of range"); - } - function Jn(e, t, r, n, i) { - return t = +t, r = r >>> 0, i || Xn(e, t, r, 4, 340282346638528860000000000000000000000, -340282346638528860000000000000000000000), qe.write(e, t, r, n, 23, 4), r + 4; - } - f.prototype.writeFloatLE = function(t, r, n) { - return Jn(this, t, r, true, n); - }; - f.prototype.writeFloatBE = function(t, r, n) { - return Jn(this, t, r, false, n); - }; - function Qn(e, t, r, n, i) { - return t = +t, r = r >>> 0, i || Xn(e, t, r, 8, 179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, -179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000), qe.write(e, t, r, n, 52, 8), r + 8; - } - f.prototype.writeDoubleLE = function(t, r, n) { - return Qn(this, t, r, true, n); - }; - f.prototype.writeDoubleBE = function(t, r, n) { - return Qn(this, t, r, false, n); - }; - f.prototype.copy = function(t, r, n, i) { - if (!f.isBuffer(t)) - throw new TypeError("argument should be a Buffer"); - if (n || (n = 0), !i && i !== 0 && (i = this.length), r >= t.length && (r = t.length), r || (r = 0), i > 0 && i < n && (i = n), i === n || t.length === 0 || this.length === 0) - return 0; - if (r < 0) - throw new RangeError("targetStart out of bounds"); - if (n < 0 || n >= this.length) - throw new RangeError("Index out of range"); - if (i < 0) - throw new RangeError("sourceEnd out of bounds"); - i > this.length && (i = this.length), t.length - r < i - n && (i = t.length - r + n); - let o = i - n; - return this === t && typeof Uint8Array.prototype.copyWithin == "function" ? this.copyWithin(r, n, i) : Uint8Array.prototype.set.call(t, this.subarray(n, i), r), o; - }; - f.prototype.fill = function(t, r, n, i) { - if (typeof t == "string") { - if (typeof r == "string" ? (i = r, r = 0, n = this.length) : typeof n == "string" && (i = n, n = this.length), i !== undefined && typeof i != "string") - throw new TypeError("encoding must be a string"); - if (typeof i == "string" && !f.isEncoding(i)) - throw new TypeError("Unknown encoding: " + i); - if (t.length === 1) { - let l = t.charCodeAt(0); - (i === "utf8" && l < 128 || i === "latin1") && (t = l); - } - } else - typeof t == "number" ? t = t & 255 : typeof t == "boolean" && (t = Number(t)); - if (r < 0 || this.length < r || this.length < n) - throw new RangeError("Out of range index"); - if (n <= r) - return this; - r = r >>> 0, n = n === undefined ? this.length : n >>> 0, t || (t = 0); - let o; - if (typeof t == "number") - for (o = r;o < n; ++o) - this[o] = t; - else { - let l = f.isBuffer(t) ? t : f.from(t, i), u = l.length; - if (u === 0) - throw new TypeError('The value "' + t + '" is invalid for argument "value"'); - for (o = 0;o < n - r; ++o) - this[o + r] = l[o % u]; + if (loweredCase) + throw new TypeError("Unknown encoding: " + encoding); + encoding = ("" + encoding).toLowerCase(), loweredCase = true; } + }; + Buffer2.prototype.toJSON = function toJSON() { + return { type: "Buffer", data: Array.prototype.slice.call(this._arr || this, 0) }; + }; + Buffer2.prototype.slice = function slice(start, end) { + let len2 = this.length; + if (start = ~~start, end = end === undefined ? len2 : ~~end, start < 0) { + if (start += len2, start < 0) + start = 0; + } else if (start > len2) + start = len2; + if (end < 0) { + if (end += len2, end < 0) + end = 0; + } else if (end > len2) + end = len2; + if (end < start) + end = start; + let newBuf = this.subarray(start, end); + return Object.setPrototypeOf(newBuf, Buffer2.prototype), newBuf; + }; + Buffer2.prototype.readUintLE = Buffer2.prototype.readUIntLE = function readUIntLE(offset, byteLength2, noAssert) { + if (offset = offset >>> 0, byteLength2 = byteLength2 >>> 0, !noAssert) + checkOffset(offset, byteLength2, this.length); + let val = this[offset], mul = 1, i2 = 0; + while (++i2 < byteLength2 && (mul *= 256)) + val += this[offset + i2] * mul; + return val; + }; + Buffer2.prototype.readUintBE = Buffer2.prototype.readUIntBE = function readUIntBE(offset, byteLength2, noAssert) { + if (offset = offset >>> 0, byteLength2 = byteLength2 >>> 0, !noAssert) + checkOffset(offset, byteLength2, this.length); + let val = this[offset + --byteLength2], mul = 1; + while (byteLength2 > 0 && (mul *= 256)) + val += this[offset + --byteLength2] * mul; + return val; + }; + Buffer2.prototype.readUint8 = Buffer2.prototype.readUInt8 = function readUInt8(offset, noAssert) { + if (offset = offset >>> 0, !noAssert) + checkOffset(offset, 1, this.length); + return this[offset]; + }; + Buffer2.prototype.readUint16LE = Buffer2.prototype.readUInt16LE = function readUInt16LE(offset, noAssert) { + if (offset = offset >>> 0, !noAssert) + checkOffset(offset, 2, this.length); + return this[offset] | this[offset + 1] << 8; + }; + Buffer2.prototype.readUint16BE = Buffer2.prototype.readUInt16BE = function readUInt16BE(offset, noAssert) { + if (offset = offset >>> 0, !noAssert) + checkOffset(offset, 2, this.length); + return this[offset] << 8 | this[offset + 1]; + }; + Buffer2.prototype.readUint32LE = Buffer2.prototype.readUInt32LE = function readUInt32LE(offset, noAssert) { + if (offset = offset >>> 0, !noAssert) + checkOffset(offset, 4, this.length); + return (this[offset] | this[offset + 1] << 8 | this[offset + 2] << 16) + this[offset + 3] * 16777216; + }; + Buffer2.prototype.readUint32BE = Buffer2.prototype.readUInt32BE = function readUInt32BE(offset, noAssert) { + if (offset = offset >>> 0, !noAssert) + checkOffset(offset, 4, this.length); + return this[offset] * 16777216 + (this[offset + 1] << 16 | this[offset + 2] << 8 | this[offset + 3]); + }; + Buffer2.prototype.readBigUInt64LE = defineBigIntMethod(function readBigUInt64LE(offset) { + offset = offset >>> 0, validateNumber(offset, "offset"); + let first = this[offset], last = this[offset + 7]; + if (first === undefined || last === undefined) + boundsError(offset, this.length - 8); + let lo = first + this[++offset] * 256 + this[++offset] * 65536 + this[++offset] * 16777216, hi = this[++offset] + this[++offset] * 256 + this[++offset] * 65536 + last * 16777216; + return BigInt(lo) + (BigInt(hi) << BigInt(32)); + }); + Buffer2.prototype.readBigUInt64BE = defineBigIntMethod(function readBigUInt64BE(offset) { + offset = offset >>> 0, validateNumber(offset, "offset"); + let first = this[offset], last = this[offset + 7]; + if (first === undefined || last === undefined) + boundsError(offset, this.length - 8); + let hi = first * 16777216 + this[++offset] * 65536 + this[++offset] * 256 + this[++offset], lo = this[++offset] * 16777216 + this[++offset] * 65536 + this[++offset] * 256 + last; + return (BigInt(hi) << BigInt(32)) + BigInt(lo); + }); + Buffer2.prototype.readIntLE = function readIntLE(offset, byteLength2, noAssert) { + if (offset = offset >>> 0, byteLength2 = byteLength2 >>> 0, !noAssert) + checkOffset(offset, byteLength2, this.length); + let val = this[offset], mul = 1, i2 = 0; + while (++i2 < byteLength2 && (mul *= 256)) + val += this[offset + i2] * mul; + if (mul *= 128, val >= mul) + val -= Math.pow(2, 8 * byteLength2); + return val; + }; + Buffer2.prototype.readIntBE = function readIntBE(offset, byteLength2, noAssert) { + if (offset = offset >>> 0, byteLength2 = byteLength2 >>> 0, !noAssert) + checkOffset(offset, byteLength2, this.length); + let i2 = byteLength2, mul = 1, val = this[offset + --i2]; + while (i2 > 0 && (mul *= 256)) + val += this[offset + --i2] * mul; + if (mul *= 128, val >= mul) + val -= Math.pow(2, 8 * byteLength2); + return val; + }; + Buffer2.prototype.readInt8 = function readInt8(offset, noAssert) { + if (offset = offset >>> 0, !noAssert) + checkOffset(offset, 1, this.length); + if (!(this[offset] & 128)) + return this[offset]; + return (255 - this[offset] + 1) * -1; + }; + Buffer2.prototype.readInt16LE = function readInt16LE(offset, noAssert) { + if (offset = offset >>> 0, !noAssert) + checkOffset(offset, 2, this.length); + let val = this[offset] | this[offset + 1] << 8; + return val & 32768 ? val | 4294901760 : val; + }; + Buffer2.prototype.readInt16BE = function readInt16BE(offset, noAssert) { + if (offset = offset >>> 0, !noAssert) + checkOffset(offset, 2, this.length); + let val = this[offset + 1] | this[offset] << 8; + return val & 32768 ? val | 4294901760 : val; + }; + Buffer2.prototype.readInt32LE = function readInt32LE(offset, noAssert) { + if (offset = offset >>> 0, !noAssert) + checkOffset(offset, 4, this.length); + return this[offset] | this[offset + 1] << 8 | this[offset + 2] << 16 | this[offset + 3] << 24; + }; + Buffer2.prototype.readInt32BE = function readInt32BE(offset, noAssert) { + if (offset = offset >>> 0, !noAssert) + checkOffset(offset, 4, this.length); + return this[offset] << 24 | this[offset + 1] << 16 | this[offset + 2] << 8 | this[offset + 3]; + }; + Buffer2.prototype.readBigInt64LE = defineBigIntMethod(function readBigInt64LE(offset) { + offset = offset >>> 0, validateNumber(offset, "offset"); + let first = this[offset], last = this[offset + 7]; + if (first === undefined || last === undefined) + boundsError(offset, this.length - 8); + let val = this[offset + 4] + this[offset + 5] * 256 + this[offset + 6] * 65536 + (last << 24); + return (BigInt(val) << BigInt(32)) + BigInt(first + this[++offset] * 256 + this[++offset] * 65536 + this[++offset] * 16777216); + }); + Buffer2.prototype.readBigInt64BE = defineBigIntMethod(function readBigInt64BE(offset) { + offset = offset >>> 0, validateNumber(offset, "offset"); + let first = this[offset], last = this[offset + 7]; + if (first === undefined || last === undefined) + boundsError(offset, this.length - 8); + let val = (first << 24) + this[++offset] * 65536 + this[++offset] * 256 + this[++offset]; + return (BigInt(val) << BigInt(32)) + BigInt(this[++offset] * 16777216 + this[++offset] * 65536 + this[++offset] * 256 + last); + }); + Buffer2.prototype.readFloatLE = function readFloatLE(offset, noAssert) { + if (offset = offset >>> 0, !noAssert) + checkOffset(offset, 4, this.length); + return read(this, offset, true, 23, 4); + }; + Buffer2.prototype.readFloatBE = function readFloatBE(offset, noAssert) { + if (offset = offset >>> 0, !noAssert) + checkOffset(offset, 4, this.length); + return read(this, offset, false, 23, 4); + }; + Buffer2.prototype.readDoubleLE = function readDoubleLE(offset, noAssert) { + if (offset = offset >>> 0, !noAssert) + checkOffset(offset, 8, this.length); + return read(this, offset, true, 52, 8); + }; + Buffer2.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) { + if (offset = offset >>> 0, !noAssert) + checkOffset(offset, 8, this.length); + return read(this, offset, false, 52, 8); + }; + Buffer2.prototype.writeUintLE = Buffer2.prototype.writeUIntLE = function writeUIntLE(value, offset, byteLength2, noAssert) { + if (value = +value, offset = offset >>> 0, byteLength2 = byteLength2 >>> 0, !noAssert) { + let maxBytes = Math.pow(2, 8 * byteLength2) - 1; + checkInt(this, value, offset, byteLength2, maxBytes, 0); + } + let mul = 1, i2 = 0; + this[offset] = value & 255; + while (++i2 < byteLength2 && (mul *= 256)) + this[offset + i2] = value / mul & 255; + return offset + byteLength2; + }; + Buffer2.prototype.writeUintBE = Buffer2.prototype.writeUIntBE = function writeUIntBE(value, offset, byteLength2, noAssert) { + if (value = +value, offset = offset >>> 0, byteLength2 = byteLength2 >>> 0, !noAssert) { + let maxBytes = Math.pow(2, 8 * byteLength2) - 1; + checkInt(this, value, offset, byteLength2, maxBytes, 0); + } + let i2 = byteLength2 - 1, mul = 1; + this[offset + i2] = value & 255; + while (--i2 >= 0 && (mul *= 256)) + this[offset + i2] = value / mul & 255; + return offset + byteLength2; + }; + Buffer2.prototype.writeUint8 = Buffer2.prototype.writeUInt8 = function writeUInt8(value, offset, noAssert) { + if (value = +value, offset = offset >>> 0, !noAssert) + checkInt(this, value, offset, 1, 255, 0); + return this[offset] = value & 255, offset + 1; + }; + Buffer2.prototype.writeUint16LE = Buffer2.prototype.writeUInt16LE = function writeUInt16LE(value, offset, noAssert) { + if (value = +value, offset = offset >>> 0, !noAssert) + checkInt(this, value, offset, 2, 65535, 0); + return this[offset] = value & 255, this[offset + 1] = value >>> 8, offset + 2; + }; + Buffer2.prototype.writeUint16BE = Buffer2.prototype.writeUInt16BE = function writeUInt16BE(value, offset, noAssert) { + if (value = +value, offset = offset >>> 0, !noAssert) + checkInt(this, value, offset, 2, 65535, 0); + return this[offset] = value >>> 8, this[offset + 1] = value & 255, offset + 2; + }; + Buffer2.prototype.writeUint32LE = Buffer2.prototype.writeUInt32LE = function writeUInt32LE(value, offset, noAssert) { + if (value = +value, offset = offset >>> 0, !noAssert) + checkInt(this, value, offset, 4, 4294967295, 0); + return this[offset + 3] = value >>> 24, this[offset + 2] = value >>> 16, this[offset + 1] = value >>> 8, this[offset] = value & 255, offset + 4; + }; + Buffer2.prototype.writeUint32BE = Buffer2.prototype.writeUInt32BE = function writeUInt32BE(value, offset, noAssert) { + if (value = +value, offset = offset >>> 0, !noAssert) + checkInt(this, value, offset, 4, 4294967295, 0); + return this[offset] = value >>> 24, this[offset + 1] = value >>> 16, this[offset + 2] = value >>> 8, this[offset + 3] = value & 255, offset + 4; + }; + Buffer2.prototype.writeBigUInt64LE = defineBigIntMethod(function writeBigUInt64LE(value, offset = 0) { + return wrtBigUInt64LE(this, value, offset, BigInt(0), BigInt("0xffffffffffffffff")); + }); + Buffer2.prototype.writeBigUInt64BE = defineBigIntMethod(function writeBigUInt64BE(value, offset = 0) { + return wrtBigUInt64BE(this, value, offset, BigInt(0), BigInt("0xffffffffffffffff")); + }); + Buffer2.prototype.writeIntLE = function writeIntLE(value, offset, byteLength2, noAssert) { + if (value = +value, offset = offset >>> 0, !noAssert) { + let limit = Math.pow(2, 8 * byteLength2 - 1); + checkInt(this, value, offset, byteLength2, limit - 1, -limit); + } + let i2 = 0, mul = 1, sub = 0; + this[offset] = value & 255; + while (++i2 < byteLength2 && (mul *= 256)) { + if (value < 0 && sub === 0 && this[offset + i2 - 1] !== 0) + sub = 1; + this[offset + i2] = (value / mul >> 0) - sub & 255; + } + return offset + byteLength2; + }; + Buffer2.prototype.writeIntBE = function writeIntBE(value, offset, byteLength2, noAssert) { + if (value = +value, offset = offset >>> 0, !noAssert) { + let limit = Math.pow(2, 8 * byteLength2 - 1); + checkInt(this, value, offset, byteLength2, limit - 1, -limit); + } + let i2 = byteLength2 - 1, mul = 1, sub = 0; + this[offset + i2] = value & 255; + while (--i2 >= 0 && (mul *= 256)) { + if (value < 0 && sub === 0 && this[offset + i2 + 1] !== 0) + sub = 1; + this[offset + i2] = (value / mul >> 0) - sub & 255; + } + return offset + byteLength2; + }; + Buffer2.prototype.writeInt8 = function writeInt8(value, offset, noAssert) { + if (value = +value, offset = offset >>> 0, !noAssert) + checkInt(this, value, offset, 1, 127, -128); + if (value < 0) + value = 255 + value + 1; + return this[offset] = value & 255, offset + 1; + }; + Buffer2.prototype.writeInt16LE = function writeInt16LE(value, offset, noAssert) { + if (value = +value, offset = offset >>> 0, !noAssert) + checkInt(this, value, offset, 2, 32767, -32768); + return this[offset] = value & 255, this[offset + 1] = value >>> 8, offset + 2; + }; + Buffer2.prototype.writeInt16BE = function writeInt16BE(value, offset, noAssert) { + if (value = +value, offset = offset >>> 0, !noAssert) + checkInt(this, value, offset, 2, 32767, -32768); + return this[offset] = value >>> 8, this[offset + 1] = value & 255, offset + 2; + }; + Buffer2.prototype.writeInt32LE = function writeInt32LE(value, offset, noAssert) { + if (value = +value, offset = offset >>> 0, !noAssert) + checkInt(this, value, offset, 4, 2147483647, -2147483648); + return this[offset] = value & 255, this[offset + 1] = value >>> 8, this[offset + 2] = value >>> 16, this[offset + 3] = value >>> 24, offset + 4; + }; + Buffer2.prototype.writeInt32BE = function writeInt32BE(value, offset, noAssert) { + if (value = +value, offset = offset >>> 0, !noAssert) + checkInt(this, value, offset, 4, 2147483647, -2147483648); + if (value < 0) + value = 4294967295 + value + 1; + return this[offset] = value >>> 24, this[offset + 1] = value >>> 16, this[offset + 2] = value >>> 8, this[offset + 3] = value & 255, offset + 4; + }; + Buffer2.prototype.writeBigInt64LE = defineBigIntMethod(function writeBigInt64LE(value, offset = 0) { + return wrtBigUInt64LE(this, value, offset, -BigInt("0x8000000000000000"), BigInt("0x7fffffffffffffff")); + }); + Buffer2.prototype.writeBigInt64BE = defineBigIntMethod(function writeBigInt64BE(value, offset = 0) { + return wrtBigUInt64BE(this, value, offset, -BigInt("0x8000000000000000"), BigInt("0x7fffffffffffffff")); + }); + Buffer2.prototype.writeFloatLE = function writeFloatLE(value, offset, noAssert) { + return writeFloat(this, value, offset, true, noAssert); + }; + Buffer2.prototype.writeFloatBE = function writeFloatBE(value, offset, noAssert) { + return writeFloat(this, value, offset, false, noAssert); + }; + Buffer2.prototype.writeDoubleLE = function writeDoubleLE(value, offset, noAssert) { + return writeDouble(this, value, offset, true, noAssert); + }; + Buffer2.prototype.writeDoubleBE = function writeDoubleBE(value, offset, noAssert) { + return writeDouble(this, value, offset, false, noAssert); + }; + Buffer2.prototype.copy = function copy(target, targetStart, start, end) { + if (!Buffer2.isBuffer(target)) + throw new TypeError("argument should be a Buffer"); + if (!start) + start = 0; + if (!end && end !== 0) + end = this.length; + if (targetStart >= target.length) + targetStart = target.length; + if (!targetStart) + targetStart = 0; + if (end > 0 && end < start) + end = start; + if (end === start) + return 0; + if (target.length === 0 || this.length === 0) + return 0; + if (targetStart < 0) + throw new RangeError("targetStart out of bounds"); + if (start < 0 || start >= this.length) + throw new RangeError("Index out of range"); + if (end < 0) + throw new RangeError("sourceEnd out of bounds"); + if (end > this.length) + end = this.length; + if (target.length - targetStart < end - start) + end = target.length - targetStart + start; + let len2 = end - start; + if (this === target && typeof Uint8Array.prototype.copyWithin === "function") + this.copyWithin(targetStart, start, end); + else + Uint8Array.prototype.set.call(target, this.subarray(start, end), targetStart); + return len2; + }; + Buffer2.prototype.fill = function fill(val, start, end, encoding) { + if (typeof val === "string") { + if (typeof start === "string") + encoding = start, start = 0, end = this.length; + else if (typeof end === "string") + encoding = end, end = this.length; + if (encoding !== undefined && typeof encoding !== "string") + throw new TypeError("encoding must be a string"); + if (typeof encoding === "string" && !Buffer2.isEncoding(encoding)) + throw new TypeError("Unknown encoding: " + encoding); + if (val.length === 1) { + let code2 = val.charCodeAt(0); + if (encoding === "utf8" && code2 < 128 || encoding === "latin1") + val = code2; + } + } else if (typeof val === "number") + val = val & 255; + else if (typeof val === "boolean") + val = Number(val); + if (start < 0 || this.length < start || this.length < end) + throw new RangeError("Out of range index"); + if (end <= start) return this; - }; - var Pe = {}; - function pr(e, t, r) { - Pe[e] = class extends r { - constructor() { - super(), Object.defineProperty(this, "message", { value: t.apply(this, arguments), writable: true, configurable: true }), this.name = `${this.name} [${e}]`, this.stack, delete this.name; - } - get code() { - return e; - } - set code(i) { - Object.defineProperty(this, "code", { configurable: true, enumerable: true, value: i, writable: true }); - } - toString() { - return `${this.name} [${e}]: ${this.message}`; - } - }; + if (start = start >>> 0, end = end === undefined ? this.length : end >>> 0, !val) + val = 0; + let i2; + if (typeof val === "number") + for (i2 = start;i2 < end; ++i2) + this[i2] = val; + else { + let bytes = Buffer2.isBuffer(val) ? val : Buffer2.from(val, encoding), len2 = bytes.length; + if (len2 === 0) + throw new TypeError('The value "' + val + '" is invalid for argument "value"'); + for (i2 = 0;i2 < end - start; ++i2) + this[i2 + start] = bytes[i2 % len2]; } - pr("ERR_BUFFER_OUT_OF_BOUNDS", function(e) { - return e ? `${e} is outside of buffer bounds` : "Attempt to access memory outside buffer bounds"; - }, RangeError); - pr("ERR_INVALID_ARG_TYPE", function(e, t) { - return `The "${e}" argument must be of type number. Received type ${typeof t}`; - }, TypeError); - pr("ERR_OUT_OF_RANGE", function(e, t, r) { - let n = `The value of "${e}" is out of range.`, i = r; - return Number.isInteger(r) && Math.abs(r) > 2 ** 32 ? i = $n(String(r)) : typeof r == "bigint" && (i = String(r), (r > BigInt(2) ** BigInt(32) || r < -(BigInt(2) ** BigInt(32))) && (i = $n(i)), i += "n"), n += ` It must be ${t}. Received ${i}`, n; - }, RangeError); - function $n(e) { - let t = "", r = e.length, n = e[0] === "-" ? 1 : 0; - for (;r >= n + 4; r -= 3) - t = `_${e.slice(r - 3, r)}${t}`; - return `${e.slice(0, r)}${t}`; - } - function Hu(e, t, r) { - ve(t, "offset"), (e[t] === undefined || e[t + r] === undefined) && nt(t, e.length - (r + 1)); - } - function Zn(e, t, r, n, i, o) { - if (e > r || e < t) { - let l = typeof t == "bigint" ? "n" : "", u; - throw o > 3 ? t === 0 || t === BigInt(0) ? u = `>= 0${l} and < 2${l} ** ${(o + 1) * 8}${l}` : u = `>= -(2${l} ** ${(o + 1) * 8 - 1}${l}) and < 2 ** ${(o + 1) * 8 - 1}${l}` : u = `>= ${t}${l} and <= ${r}${l}`, new Pe.ERR_OUT_OF_RANGE("value", u, e); - } - Hu(n, i, o); - } - function ve(e, t) { - if (typeof e != "number") - throw new Pe.ERR_INVALID_ARG_TYPE(t, "number", e); - } - function nt(e, t, r) { - throw Math.floor(e) !== e ? (ve(e, r), new Pe.ERR_OUT_OF_RANGE(r || "offset", "an integer", e)) : t < 0 ? new Pe.ERR_BUFFER_OUT_OF_BOUNDS : new Pe.ERR_OUT_OF_RANGE(r || "offset", `>= ${r ? 1 : 0} and <= ${t}`, e); - } - var Gu = /[^+/0-9A-Za-z-_]/g; - function Vu(e) { - if (e = e.split("=")[0], e = e.trim().replace(Gu, ""), e.length < 2) - return ""; - for (;e.length % 4 !== 0; ) - e = e + "="; - return e; - } - function cr(e, t) { - t = t || 1 / 0; - let r, n = e.length, i = null, o = []; - for (let l = 0;l < n; ++l) { - if (r = e.charCodeAt(l), r > 55295 && r < 57344) { - if (!i) { - if (r > 56319) { - (t -= 3) > -1 && o.push(239, 191, 189); - continue; - } else if (l + 1 === n) { - (t -= 3) > -1 && o.push(239, 191, 189); - continue; - } - i = r; - continue; - } - if (r < 56320) { - (t -= 3) > -1 && o.push(239, 191, 189), i = r; - continue; - } - r = (i - 55296 << 10 | r - 56320) + 65536; - } else - i && (t -= 3) > -1 && o.push(239, 191, 189); - if (i = null, r < 128) { - if ((t -= 1) < 0) - break; - o.push(r); - } else if (r < 2048) { - if ((t -= 2) < 0) - break; - o.push(r >> 6 | 192, r & 63 | 128); - } else if (r < 65536) { - if ((t -= 3) < 0) - break; - o.push(r >> 12 | 224, r >> 6 & 63 | 128, r & 63 | 128); - } else if (r < 1114112) { - if ((t -= 4) < 0) - break; - o.push(r >> 18 | 240, r >> 12 & 63 | 128, r >> 6 & 63 | 128, r & 63 | 128); - } else - throw new Error("Invalid code point"); - } - return o; - } - function Yu(e) { - let t = []; - for (let r = 0;r < e.length; ++r) - t.push(e.charCodeAt(r) & 255); - return t; + return this; + }; + INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g; + hexSliceLookupTable = function() { + let table = new Array(256); + for (let i2 = 0;i2 < 16; ++i2) { + let i16 = i2 * 16; + for (let j = 0;j < 16; ++j) + table[i16 + j] = "0123456789abcdef"[i2] + "0123456789abcdef"[j]; } - function Ku(e, t) { - let r, n, i, o = []; - for (let l = 0;l < e.length && !((t -= 2) < 0); ++l) - r = e.charCodeAt(l), n = r >> 8, i = r % 256, o.push(i), o.push(n); - return o; + return table; + }(); + resolveObjectURL = notimpl("resolveObjectURL"); + isUtf8 = notimpl("isUtf8"); + transcode = notimpl("transcode"); + buffer_default = Buffer2; +}); + +// node:events +var exports_events = {}; +__export(exports_events, { + setMaxListeners: () => setMaxListeners2, + once: () => once2, + listenerCount: () => listenerCount2, + init: () => EventEmitter, + getMaxListeners: () => getMaxListeners2, + getEventListeners: () => getEventListeners, + default: () => events_default, + captureRejectionSymbol: () => captureRejectionSymbol, + addAbortListener: () => addAbortListener, + EventEmitter: () => EventEmitter +}); +function emitError(emitter, args) { + var { _events: events } = emitter; + if (args[0] ??= new Error("Unhandled error."), !events) + throw args[0]; + var errorMonitor = events[kErrorMonitor]; + if (errorMonitor) + for (var handler of ArrayPrototypeSlice.call(errorMonitor)) + handler.apply(emitter, args); + var handlers = events.error; + if (!handlers) + throw args[0]; + for (var handler of ArrayPrototypeSlice.call(handlers)) + handler.apply(emitter, args); + return true; +} +function addCatch(emitter, promise, type, args) { + promise.then(undefined, function(err) { + queueMicrotask(() => emitUnhandledRejectionOrErr(emitter, err, type, args)); + }); +} +function emitUnhandledRejectionOrErr(emitter, err, type, args) { + if (typeof emitter[kRejection] === "function") + emitter[kRejection](err, type, ...args); + else + try { + emitter[kCapture] = false, emitter.emit("error", err); + } finally { + emitter[kCapture] = true; } - function ei(e) { - return sr.toByteArray(Vu(e)); +} +function overflowWarning(emitter, type, handlers) { + handlers.warned = true; + let warn = new Error(`Possible EventEmitter memory leak detected. ${handlers.length} ${String(type)} listeners added to [${emitter.constructor.name}]. Use emitter.setMaxListeners() to increase limit`); + warn.name = "MaxListenersExceededWarning", warn.emitter = emitter, warn.type = type, warn.count = handlers.length, console.warn(warn); +} +function onceWrapper(type, listener, ...args) { + this.removeListener(type, listener), listener.apply(this, args); +} +function once2(emitter, type, options) { + var signal = options?.signal; + if (validateAbortSignal(signal, "options.signal"), signal?.aborted) + throw new AbortError(undefined, { cause: signal?.reason }); + let { resolve, reject, promise } = $newPromiseCapability(Promise), errorListener = (err) => { + if (emitter.removeListener(type, resolver), signal != null) + eventTargetAgnosticRemoveListener(signal, "abort", abortListener); + reject(err); + }, resolver = (...args) => { + if (typeof emitter.removeListener === "function") + emitter.removeListener("error", errorListener); + if (signal != null) + eventTargetAgnosticRemoveListener(signal, "abort", abortListener); + resolve(args); + }; + if (eventTargetAgnosticAddListener(emitter, type, resolver, { once: true }), type !== "error" && typeof emitter.once === "function") + emitter.once("error", errorListener); + function abortListener() { + eventTargetAgnosticRemoveListener(emitter, type, resolver), eventTargetAgnosticRemoveListener(emitter, "error", errorListener), reject(new AbortError(undefined, { cause: signal?.reason })); + } + if (signal != null) + eventTargetAgnosticAddListener(signal, "abort", abortListener, { once: true }); + return promise; +} +function getEventListeners(emitter, type) { + return emitter.listeners(type); +} +function setMaxListeners2(n, ...eventTargets) { + validateNumber2(n, "setMaxListeners", 0); + var length; + if (eventTargets && (length = eventTargets.length)) + for (let i2 = 0;i2 < length; i2++) + eventTargets[i2].setMaxListeners(n); + else + defaultMaxListeners = n; +} +function listenerCount2(emitter, type) { + return emitter.listenerCount(type); +} +function eventTargetAgnosticRemoveListener(emitter, name, listener, flags) { + if (typeof emitter.removeListener === "function") + emitter.removeListener(name, listener); + else + emitter.removeEventListener(name, listener, flags); +} +function eventTargetAgnosticAddListener(emitter, name, listener, flags) { + if (typeof emitter.on === "function") + if (flags.once) + emitter.once(name, listener); + else + emitter.on(name, listener); + else + emitter.addEventListener(name, listener, flags); +} +function ERR_INVALID_ARG_TYPE2(name, type, value) { + let err = new TypeError(`The "${name}" argument must be of type ${type}. Received ${value}`); + return err.code = "ERR_INVALID_ARG_TYPE", err; +} +function ERR_OUT_OF_RANGE2(name, range, value) { + let err = new RangeError(`The "${name}" argument is out of range. It must be ${range}. Received ${value}`); + return err.code = "ERR_OUT_OF_RANGE", err; +} +function validateAbortSignal(signal, name) { + if (signal !== undefined && (signal === null || typeof signal !== "object" || !("aborted" in signal))) + throw ERR_INVALID_ARG_TYPE2(name, "AbortSignal", signal); +} +function validateNumber2(value, name, min, max) { + if (typeof value !== "number") + throw ERR_INVALID_ARG_TYPE2(name, "number", value); + if (min != null && value < min || max != null && value > max || (min != null || max != null) && Number.isNaN(value)) + throw ERR_OUT_OF_RANGE2(name, `${min != null ? `>= ${min}` : ""}${min != null && max != null ? " && " : ""}${max != null ? `<= ${max}` : ""}`, value); +} +function checkListener(listener) { + if (typeof listener !== "function") + throw new TypeError("The listener must be a function"); +} +function validateBoolean(value, name) { + if (typeof value !== "boolean") + throw ERR_INVALID_ARG_TYPE2(name, "boolean", value); +} +function getMaxListeners2(emitterOrTarget) { + return emitterOrTarget?._maxListeners ?? defaultMaxListeners; +} +function addAbortListener(signal, listener) { + if (signal === undefined) + throw ERR_INVALID_ARG_TYPE2("signal", "AbortSignal", signal); + if (validateAbortSignal(signal, "signal"), typeof listener !== "function") + throw ERR_INVALID_ARG_TYPE2("listener", "function", listener); + let removeEventListener; + if (signal.aborted) + queueMicrotask(() => listener()); + else + signal.addEventListener("abort", listener, { __proto__: null, once: true }), removeEventListener = () => { + signal.removeEventListener("abort", listener); + }; + return { __proto__: null, [Symbol.dispose]() { + removeEventListener?.(); + } }; +} +var SymbolFor, kCapture, kErrorMonitor, kMaxEventTargetListeners, kMaxEventTargetListenersWarned, kRejection, captureRejectionSymbol, ArrayPrototypeSlice, defaultMaxListeners = 10, EventEmitter = function EventEmitter2(opts) { + if (this._events === undefined || this._events === this.__proto__._events) + this._events = { __proto__: null }, this._eventsCount = 0; + if (this._maxListeners ??= undefined, this[kCapture] = opts?.captureRejections ? Boolean(opts?.captureRejections) : EventEmitterPrototype[kCapture]) + this.emit = emitWithRejectionCapture; +}, EventEmitterPrototype, emitWithoutRejectionCapture = function emit(type, ...args) { + if (type === "error") + return emitError(this, args); + var { _events: events } = this; + if (events === undefined) + return false; + var handlers = events[type]; + if (handlers === undefined) + return false; + let maybeClonedHandlers = handlers.length > 1 ? handlers.slice() : handlers; + for (let i2 = 0, { length } = maybeClonedHandlers;i2 < length; i2++) { + let handler = maybeClonedHandlers[i2]; + switch (args.length) { + case 0: + handler.call(this); + break; + case 1: + handler.call(this, args[0]); + break; + case 2: + handler.call(this, args[0], args[1]); + break; + case 3: + handler.call(this, args[0], args[1], args[2]); + break; + default: + handler.apply(this, args); + break; } - function mt(e, t, r, n) { - let i; - for (i = 0;i < n && !(i + r >= t.length || i >= e.length); ++i) - t[i + r] = e[i]; - return i; + } + return true; +}, emitWithRejectionCapture = function emit2(type, ...args) { + if (type === "error") + return emitError(this, args); + var { _events: events } = this; + if (events === undefined) + return false; + var handlers = events[type]; + if (handlers === undefined) + return false; + let maybeClonedHandlers = handlers.length > 1 ? handlers.slice() : handlers; + for (let i2 = 0, { length } = maybeClonedHandlers;i2 < length; i2++) { + let handler = maybeClonedHandlers[i2], result; + switch (args.length) { + case 0: + result = handler.call(this); + break; + case 1: + result = handler.call(this, args[0]); + break; + case 2: + result = handler.call(this, args[0], args[1]); + break; + case 3: + result = handler.call(this, args[0], args[1], args[2]); + break; + default: + result = handler.apply(this, args); + break; } - function z(e, t) { - return e instanceof t || e != null && e.constructor != null && e.constructor.name != null && e.constructor.name === t.name; + if (result !== undefined && typeof result?.then === "function" && result.then === Promise.prototype.then) + addCatch(this, result, type, args); + } + return true; +}, AbortError, events_default; +var init_events = __esm(() => { + SymbolFor = Symbol.for; + kCapture = Symbol("kCapture"); + kErrorMonitor = SymbolFor("events.errorMonitor"); + kMaxEventTargetListeners = Symbol("events.maxEventTargetListeners"); + kMaxEventTargetListenersWarned = Symbol("events.maxEventTargetListenersWarned"); + kRejection = SymbolFor("nodejs.rejection"); + captureRejectionSymbol = SymbolFor("nodejs.rejection"); + ArrayPrototypeSlice = Array.prototype.slice; + EventEmitterPrototype = EventEmitter.prototype = {}; + EventEmitterPrototype._events = undefined; + EventEmitterPrototype._eventsCount = 0; + EventEmitterPrototype._maxListeners = undefined; + EventEmitterPrototype.setMaxListeners = function setMaxListeners(n) { + return validateNumber2(n, "setMaxListeners", 0), this._maxListeners = n, this; + }; + EventEmitterPrototype.constructor = EventEmitter; + EventEmitterPrototype.getMaxListeners = function getMaxListeners() { + return this?._maxListeners ?? defaultMaxListeners; + }; + EventEmitterPrototype.emit = emitWithoutRejectionCapture; + EventEmitterPrototype.addListener = function addListener(type, fn) { + checkListener(fn); + var events = this._events; + if (!events) + events = this._events = { __proto__: null }, this._eventsCount = 0; + else if (events.newListener) + this.emit("newListener", type, fn.listener ?? fn); + var handlers = events[type]; + if (!handlers) + events[type] = [fn], this._eventsCount++; + else { + handlers.push(fn); + var m = this._maxListeners ?? defaultMaxListeners; + if (m > 0 && handlers.length > m && !handlers.warned) + overflowWarning(this, type, handlers); } - function yr(e) { - return e !== e; + return this; + }; + EventEmitterPrototype.on = EventEmitterPrototype.addListener; + EventEmitterPrototype.prependListener = function prependListener(type, fn) { + checkListener(fn); + var events = this._events; + if (!events) + events = this._events = { __proto__: null }, this._eventsCount = 0; + else if (events.newListener) + this.emit("newListener", type, fn.listener ?? fn); + var handlers = events[type]; + if (!handlers) + events[type] = [fn], this._eventsCount++; + else { + handlers.unshift(fn); + var m = this._maxListeners ?? defaultMaxListeners; + if (m > 0 && handlers.length > m && !handlers.warned) + overflowWarning(this, type, handlers); } - var zu = function() { - let e = "0123456789abcdef", t = new Array(256); - for (let r = 0;r < 16; ++r) { - let n = r * 16; - for (let i = 0;i < 16; ++i) - t[n + i] = e[r] + e[i]; + return this; + }; + EventEmitterPrototype.once = function once(type, fn) { + checkListener(fn); + let bound = onceWrapper.bind(this, type, fn); + return bound.listener = fn, this.addListener(type, bound), this; + }; + EventEmitterPrototype.prependOnceListener = function prependOnceListener(type, fn) { + checkListener(fn); + let bound = onceWrapper.bind(this, type, fn); + return bound.listener = fn, this.prependListener(type, bound), this; + }; + EventEmitterPrototype.removeListener = function removeListener(type, fn) { + checkListener(fn); + var { _events: events } = this; + if (!events) + return this; + var handlers = events[type]; + if (!handlers) + return this; + var length = handlers.length; + let position = -1; + for (let i2 = length - 1;i2 >= 0; i2--) + if (handlers[i2] === fn || handlers[i2].listener === fn) { + position = i2; + break; } - return t; - }(); - function be(e) { - return typeof BigInt > "u" ? Xu : e; - } - function Xu() { - throw new Error("BigInt not supported"); + if (position < 0) + return this; + if (position === 0) + handlers.shift(); + else + handlers.splice(position, 1); + if (handlers.length === 0) + delete events[type], this._eventsCount--; + return this; + }; + EventEmitterPrototype.off = EventEmitterPrototype.removeListener; + EventEmitterPrototype.removeAllListeners = function removeAllListeners(type) { + var { _events: events } = this; + if (type && events) { + if (events[type]) + delete events[type], this._eventsCount--; + } else + this._events = { __proto__: null }; + return this; + }; + EventEmitterPrototype.listeners = function listeners(type) { + var { _events: events } = this; + if (!events) + return []; + var handlers = events[type]; + if (!handlers) + return []; + return handlers.map((x) => x.listener ?? x); + }; + EventEmitterPrototype.rawListeners = function rawListeners(type) { + var { _events } = this; + if (!_events) + return []; + var handlers = _events[type]; + if (!handlers) + return []; + return handlers.slice(); + }; + EventEmitterPrototype.listenerCount = function listenerCount(type) { + var { _events: events } = this; + if (!events) + return 0; + return events[type]?.length ?? 0; + }; + EventEmitterPrototype.eventNames = function eventNames() { + return this._eventsCount > 0 ? Reflect.ownKeys(this._events) : []; + }; + EventEmitterPrototype[kCapture] = false; + AbortError = class AbortError extends Error { + constructor(message = "The operation was aborted", options = undefined) { + if (options !== undefined && typeof options !== "object") + throw ERR_INVALID_ARG_TYPE2("options", "Object", options); + super(message, options); + this.code = "ABORT_ERR", this.name = "AbortError"; } - }); - I = S((Mh, ti) => { - var br = class extends Error { - constructor(t) { - if (!Array.isArray(t)) - throw new TypeError(`Expected input to be an Array, got ${typeof t}`); - let r = ""; - for (let n = 0;n < t.length; n++) - r += ` ${t[n].stack} + }; + Object.defineProperties(EventEmitter, { captureRejections: { get() { + return EventEmitterPrototype[kCapture]; + }, set(value) { + validateBoolean(value, "EventEmitter.captureRejections"), EventEmitterPrototype[kCapture] = value; + }, enumerable: true }, defaultMaxListeners: { enumerable: true, get: () => { + return defaultMaxListeners; + }, set: (arg) => { + validateNumber2(arg, "defaultMaxListeners", 0), defaultMaxListeners = arg; + } }, kMaxEventTargetListeners: { value: kMaxEventTargetListeners, enumerable: false, configurable: false, writable: false }, kMaxEventTargetListenersWarned: { value: kMaxEventTargetListenersWarned, enumerable: false, configurable: false, writable: false } }); + Object.assign(EventEmitter, { once: once2, getEventListeners, getMaxListeners: getMaxListeners2, setMaxListeners: setMaxListeners2, EventEmitter, usingDomains: false, captureRejectionSymbol, errorMonitor: kErrorMonitor, addAbortListener, init: EventEmitter, listenerCount: listenerCount2 }); + events_default = EventEmitter; +}); + +// node:stream +var require_stream = __commonJS((exports, module) => { + var __commonJS2 = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports); + var require_primordials = __commonJS2((exports2, module2) => { + + class AggregateError extends Error { + constructor(errors) { + if (!Array.isArray(errors)) + throw new TypeError(`Expected input to be an Array, got ${typeof errors}`); + let message = ""; + for (let i2 = 0;i2 < errors.length; i2++) + message += ` ${errors[i2].stack} `; - super(r), this.name = "AggregateError", this.errors = t; - } - }; - ti.exports = { AggregateError: br, ArrayIsArray(e) { - return Array.isArray(e); - }, ArrayPrototypeIncludes(e, t) { - return e.includes(t); - }, ArrayPrototypeIndexOf(e, t) { - return e.indexOf(t); - }, ArrayPrototypeJoin(e, t) { - return e.join(t); - }, ArrayPrototypeMap(e, t) { - return e.map(t); - }, ArrayPrototypePop(e, t) { - return e.pop(t); - }, ArrayPrototypePush(e, t) { - return e.push(t); - }, ArrayPrototypeSlice(e, t, r) { - return e.slice(t, r); - }, Error, FunctionPrototypeCall(e, t, ...r) { - return e.call(t, ...r); - }, FunctionPrototypeSymbolHasInstance(e, t) { - return Function.prototype[Symbol.hasInstance].call(e, t); - }, MathFloor: Math.floor, Number, NumberIsInteger: Number.isInteger, NumberIsNaN: Number.isNaN, NumberMAX_SAFE_INTEGER: Number.MAX_SAFE_INTEGER, NumberMIN_SAFE_INTEGER: Number.MIN_SAFE_INTEGER, NumberParseInt: Number.parseInt, ObjectDefineProperties(e, t) { - return Object.defineProperties(e, t); - }, ObjectDefineProperty(e, t, r) { - return Object.defineProperty(e, t, r); - }, ObjectGetOwnPropertyDescriptor(e, t) { - return Object.getOwnPropertyDescriptor(e, t); - }, ObjectKeys(e) { - return Object.keys(e); - }, ObjectSetPrototypeOf(e, t) { - return Object.setPrototypeOf(e, t); - }, Promise, PromisePrototypeCatch(e, t) { - return e.catch(t); - }, PromisePrototypeThen(e, t, r) { - return e.then(t, r); - }, PromiseReject(e) { - return Promise.reject(e); - }, PromiseResolve(e) { - return Promise.resolve(e); - }, ReflectApply: Reflect.apply, RegExpPrototypeTest(e, t) { - return e.test(t); - }, SafeSet: Set, String, StringPrototypeSlice(e, t, r) { - return e.slice(t, r); - }, StringPrototypeToLowerCase(e) { - return e.toLowerCase(); - }, StringPrototypeToUpperCase(e) { - return e.toUpperCase(); - }, StringPrototypeTrim(e) { - return e.trim(); - }, Symbol, SymbolFor: Symbol.for, SymbolAsyncIterator: Symbol.asyncIterator, SymbolHasInstance: Symbol.hasInstance, SymbolIterator: Symbol.iterator, SymbolDispose: Symbol.dispose || Symbol("Symbol.dispose"), SymbolAsyncDispose: Symbol.asyncDispose || Symbol("Symbol.asyncDispose"), TypedArrayPrototypeSet(e, t, r) { - return e.set(t, r); + super(message); + this.name = "AggregateError", this.errors = errors; + } + } + module2.exports = { AggregateError, ArrayIsArray(self2) { + return Array.isArray(self2); + }, ArrayPrototypeIncludes(self2, el) { + return self2.includes(el); + }, ArrayPrototypeIndexOf(self2, el) { + return self2.indexOf(el); + }, ArrayPrototypeJoin(self2, sep) { + return self2.join(sep); + }, ArrayPrototypeMap(self2, fn) { + return self2.map(fn); + }, ArrayPrototypePop(self2, el) { + return self2.pop(el); + }, ArrayPrototypePush(self2, el) { + return self2.push(el); + }, ArrayPrototypeSlice(self2, start, end) { + return self2.slice(start, end); + }, Error, FunctionPrototypeCall(fn, thisArgs, ...args) { + return fn.call(thisArgs, ...args); + }, FunctionPrototypeSymbolHasInstance(self2, instance) { + return Function.prototype[Symbol.hasInstance].call(self2, instance); + }, MathFloor: Math.floor, Number, NumberIsInteger: Number.isInteger, NumberIsNaN: Number.isNaN, NumberMAX_SAFE_INTEGER: Number.MAX_SAFE_INTEGER, NumberMIN_SAFE_INTEGER: Number.MIN_SAFE_INTEGER, NumberParseInt: Number.parseInt, ObjectDefineProperties(self2, props) { + return Object.defineProperties(self2, props); + }, ObjectDefineProperty(self2, name, prop) { + return Object.defineProperty(self2, name, prop); + }, ObjectGetOwnPropertyDescriptor(self2, name) { + return Object.getOwnPropertyDescriptor(self2, name); + }, ObjectKeys(obj) { + return Object.keys(obj); + }, ObjectSetPrototypeOf(target, proto) { + return Object.setPrototypeOf(target, proto); + }, Promise, PromisePrototypeCatch(self2, fn) { + return self2.catch(fn); + }, PromisePrototypeThen(self2, thenFn, catchFn) { + return self2.then(thenFn, catchFn); + }, PromiseReject(err) { + return Promise.reject(err); + }, PromiseResolve(val) { + return Promise.resolve(val); + }, ReflectApply: Reflect.apply, RegExpPrototypeTest(self2, value) { + return self2.test(value); + }, SafeSet: Set, String, StringPrototypeSlice(self2, start, end) { + return self2.slice(start, end); + }, StringPrototypeToLowerCase(self2) { + return self2.toLowerCase(); + }, StringPrototypeToUpperCase(self2) { + return self2.toUpperCase(); + }, StringPrototypeTrim(self2) { + return self2.trim(); + }, Symbol, SymbolFor: Symbol.for, SymbolAsyncIterator: Symbol.asyncIterator, SymbolHasInstance: Symbol.hasInstance, SymbolIterator: Symbol.iterator, SymbolDispose: Symbol.dispose || Symbol("Symbol.dispose"), SymbolAsyncDispose: Symbol.asyncDispose || Symbol("Symbol.asyncDispose"), TypedArrayPrototypeSet(self2, buf, len2) { + return self2.set(buf, len2); }, Boolean, Uint8Array }; }); - wr = S((Ch, ri) => { - ri.exports = { format(e, ...t) { - return e.replace(/%([sdifj])/g, function(...[r, n]) { - let i = t.shift(); - return n === "f" ? i.toFixed(6) : n === "j" ? JSON.stringify(i) : n === "s" && typeof i == "object" ? `${i.constructor !== Object ? i.constructor.name : ""} {}`.trim() : i.toString(); + var require_inspect = __commonJS2((exports2, module2) => { + module2.exports = { format(format, ...args) { + return format.replace(/%([sdifj])/g, function(...[_unused, type]) { + let replacement = args.shift(); + if (type === "f") + return replacement.toFixed(6); + else if (type === "j") + return JSON.stringify(replacement); + else if (type === "s" && typeof replacement === "object") + return `${replacement.constructor !== Object ? replacement.constructor.name : ""} {}`.trim(); + else + return replacement.toString(); }); - }, inspect(e) { - switch (typeof e) { + }, inspect(value) { + switch (typeof value) { case "string": - if (e.includes("'")) - if (e.includes('"')) { - if (!e.includes("`") && !e.includes("${")) - return `\`${e}\``; - } else - return `"${e}"`; - return `'${e}'`; + if (value.includes("'")) { + if (!value.includes('"')) + return `"${value}"`; + else if (!value.includes("`") && !value.includes("${")) + return `\`${value}\``; + } + return `'${value}'`; case "number": - return isNaN(e) ? "NaN" : Object.is(e, -0) ? String(e) : e; + if (isNaN(value)) + return "NaN"; + else if (Object.is(value, -0)) + return String(value); + return value; case "bigint": - return `${String(e)}n`; + return `${String(value)}n`; case "boolean": case "undefined": - return String(e); + return String(value); case "object": return "{}"; } } }; }); - O = S((kh, oi) => { - var { format: Ju, inspect: Rt } = wr(), { AggregateError: Qu } = I(), Zu = globalThis.AggregateError || Qu, es = Symbol("kIsNodeError"), ts = ["string", "function", "number", "object", "Function", "Object", "boolean", "bigint", "symbol"], rs = /^([A-Z][a-z0-9]*)+$/, ns = "__node_internal_", At = {}; - function Ae(e, t) { - if (!e) - throw new At.ERR_INTERNAL_ASSERTION(t); - } - function ni(e) { - let t = "", r = e.length, n = e[0] === "-" ? 1 : 0; - for (;r >= n + 4; r -= 3) - t = `_${e.slice(r - 3, r)}${t}`; - return `${e.slice(0, r)}${t}`; - } - function is(e, t, r) { - if (typeof t == "function") - return Ae(t.length <= r.length, `Code: ${e}; The provided arguments length (${r.length}) does not match the required ones (${t.length}).`), t(...r); - let n = (t.match(/%[dfijoOs]/g) || []).length; - return Ae(n === r.length, `Code: ${e}; The provided arguments length (${r.length}) does not match the required ones (${n}).`), r.length === 0 ? t : Ju(t, ...r); - } - function D(e, t, r) { - r || (r = Error); + var require_errors = __commonJS2((exports2, module2) => { + var { format, inspect: inspect2 } = require_inspect(), { AggregateError: CustomAggregateError } = require_primordials(), AggregateError = globalThis.AggregateError || CustomAggregateError, kIsNodeError = Symbol("kIsNodeError"), kTypes = ["string", "function", "number", "object", "Function", "Object", "boolean", "bigint", "symbol"], classRegExp = /^([A-Z][a-z0-9]*)+$/, codes = {}; + function assert(value, message) { + if (!value) + throw new codes.ERR_INTERNAL_ASSERTION(message); + } + function addNumericalSeparator2(val) { + let res = "", i2 = val.length, start = val[0] === "-" ? 1 : 0; + for (;i2 >= start + 4; i2 -= 3) + res = `_${val.slice(i2 - 3, i2)}${res}`; + return `${val.slice(0, i2)}${res}`; + } + function getMessage(key, msg, args) { + if (typeof msg === "function") + return assert(msg.length <= args.length, `Code: ${key}; The provided arguments length (${args.length}) does not match the required ones (${msg.length}).`), msg(...args); + let expectedLength = (msg.match(/%[dfijoOs]/g) || []).length; + if (assert(expectedLength === args.length, `Code: ${key}; The provided arguments length (${args.length}) does not match the required ones (${expectedLength}).`), args.length === 0) + return msg; + return format(msg, ...args); + } + function E2(code2, message, Base) { + if (!Base) + Base = Error; - class n extends r { - constructor(...o) { - super(is(e, t, o)); + class NodeError extends Base { + constructor(...args) { + super(getMessage(code2, message, args)); } toString() { - return `${this.name} [${e}]: ${this.message}`; + return `${this.name} [${code2}]: ${this.message}`; } } - Object.defineProperties(n.prototype, { name: { value: r.name, writable: true, enumerable: false, configurable: true }, toString: { value() { - return `${this.name} [${e}]: ${this.message}`; - }, writable: true, enumerable: false, configurable: true } }), n.prototype.code = e, n.prototype[es] = true, At[e] = n; + Object.defineProperties(NodeError.prototype, { name: { value: Base.name, writable: true, enumerable: false, configurable: true }, toString: { value() { + return `${this.name} [${code2}]: ${this.message}`; + }, writable: true, enumerable: false, configurable: true } }), NodeError.prototype.code = code2, NodeError.prototype[kIsNodeError] = true, codes[code2] = NodeError; } - function ii(e) { - let t = ns + e.name; - return Object.defineProperty(e, "name", { value: t }), e; + function hideStackFrames(fn) { + let hidden = "__node_internal_" + fn.name; + return Object.defineProperty(fn, "name", { value: hidden }), fn; } - function os(e, t) { - if (e && t && e !== t) { - if (Array.isArray(t.errors)) - return t.errors.push(e), t; - let r = new Zu([t, e], t.message); - return r.code = t.code, r; + function aggregateTwoErrors(innerError, outerError) { + if (innerError && outerError && innerError !== outerError) { + if (Array.isArray(outerError.errors)) + return outerError.errors.push(innerError), outerError; + let err = new AggregateError([outerError, innerError], outerError.message); + return err.code = outerError.code, err; } - return e || t; + return innerError || outerError; } - var gr = class extends Error { - constructor(t = "The operation was aborted", r = undefined) { - if (r !== undefined && typeof r != "object") - throw new At.ERR_INVALID_ARG_TYPE("options", "Object", r); - super(t, r), this.code = "ABORT_ERR", this.name = "AbortError"; - } - }; - D("ERR_ASSERTION", "%s", Error); - D("ERR_INVALID_ARG_TYPE", (e, t, r) => { - Ae(typeof e == "string", "'name' must be a string"), Array.isArray(t) || (t = [t]); - let n = "The "; - e.endsWith(" argument") ? n += `${e} ` : n += `"${e}" ${e.includes(".") ? "property" : "argument"} `, n += "must be "; - let i = [], o = [], l = []; - for (let a of t) - Ae(typeof a == "string", "All expected entries have to be of type string"), ts.includes(a) ? i.push(a.toLowerCase()) : rs.test(a) ? o.push(a) : (Ae(a !== "object", 'The value "object" should be written as "Object"'), l.push(a)); - if (o.length > 0) { - let a = i.indexOf("object"); - a !== -1 && (i.splice(i, a, 1), o.push("Object")); - } - if (i.length > 0) { - switch (i.length) { + + class AbortError2 extends Error { + constructor(message = "The operation was aborted", options = undefined) { + if (options !== undefined && typeof options !== "object") + throw new codes.ERR_INVALID_ARG_TYPE("options", "Object", options); + super(message, options); + this.code = "ABORT_ERR", this.name = "AbortError"; + } + } + E2("ERR_ASSERTION", "%s", Error); + E2("ERR_INVALID_ARG_TYPE", (name, expected, actual) => { + if (assert(typeof name === "string", "'name' must be a string"), !Array.isArray(expected)) + expected = [expected]; + let msg = "The "; + if (name.endsWith(" argument")) + msg += `${name} `; + else + msg += `"${name}" ${name.includes(".") ? "property" : "argument"} `; + msg += "must be "; + let types = [], instances = [], other = []; + for (let value of expected) + if (assert(typeof value === "string", "All expected entries have to be of type string"), kTypes.includes(value)) + types.push(value.toLowerCase()); + else if (classRegExp.test(value)) + instances.push(value); + else + assert(value !== "object", 'The value "object" should be written as "Object"'), other.push(value); + if (instances.length > 0) { + let pos = types.indexOf("object"); + if (pos !== -1) + types.splice(types, pos, 1), instances.push("Object"); + } + if (types.length > 0) { + switch (types.length) { case 1: - n += `of type ${i[0]}`; + msg += `of type ${types[0]}`; break; case 2: - n += `one of type ${i[0]} or ${i[1]}`; + msg += `one of type ${types[0]} or ${types[1]}`; break; default: { - let a = i.pop(); - n += `one of type ${i.join(", ")}, or ${a}`; + let last = types.pop(); + msg += `one of type ${types.join(", ")}, or ${last}`; } } - (o.length > 0 || l.length > 0) && (n += " or "); + if (instances.length > 0 || other.length > 0) + msg += " or "; } - if (o.length > 0) { - switch (o.length) { + if (instances.length > 0) { + switch (instances.length) { case 1: - n += `an instance of ${o[0]}`; + msg += `an instance of ${instances[0]}`; break; case 2: - n += `an instance of ${o[0]} or ${o[1]}`; + msg += `an instance of ${instances[0]} or ${instances[1]}`; break; default: { - let a = o.pop(); - n += `an instance of ${o.join(", ")}, or ${a}`; + let last = instances.pop(); + msg += `an instance of ${instances.join(", ")}, or ${last}`; } } - l.length > 0 && (n += " or "); + if (other.length > 0) + msg += " or "; } - switch (l.length) { + switch (other.length) { case 0: break; case 1: - l[0].toLowerCase() !== l[0] && (n += "an "), n += `${l[0]}`; + if (other[0].toLowerCase() !== other[0]) + msg += "an "; + msg += `${other[0]}`; break; case 2: - n += `one of ${l[0]} or ${l[1]}`; + msg += `one of ${other[0]} or ${other[1]}`; break; default: { - let a = l.pop(); - n += `one of ${l.join(", ")}, or ${a}`; + let last = other.pop(); + msg += `one of ${other.join(", ")}, or ${last}`; + } + } + if (actual == null) + msg += `. Received ${actual}`; + else if (typeof actual === "function" && actual.name) + msg += `. Received function ${actual.name}`; + else if (typeof actual === "object") { + var _actual$constructor; + if ((_actual$constructor = actual.constructor) !== null && _actual$constructor !== undefined && _actual$constructor.name) + msg += `. Received an instance of ${actual.constructor.name}`; + else { + let inspected = inspect2(actual, { depth: -1 }); + msg += `. Received ${inspected}`; } - } - if (r == null) - n += `. Received ${r}`; - else if (typeof r == "function" && r.name) - n += `. Received function ${r.name}`; - else if (typeof r == "object") { - var u; - (u = r.constructor) !== null && u !== undefined && u.name ? n += `. Received an instance of ${r.constructor.name}` : n += `. Received ${Rt(r, { depth: -1 })}`; } else { - let a = Rt(r, { colors: false }); - a.length > 25 && (a = `${a.slice(0, 25)}...`), n += `. Received type ${typeof r} (${a})`; + let inspected = inspect2(actual, { colors: false }); + if (inspected.length > 25) + inspected = `${inspected.slice(0, 25)}...`; + msg += `. Received type ${typeof actual} (${inspected})`; } - return n; + return msg; }, TypeError); - D("ERR_INVALID_ARG_VALUE", (e, t, r = "is invalid") => { - let n = Rt(t); - return n.length > 128 && (n = n.slice(0, 128) + "..."), `The ${e.includes(".") ? "property" : "argument"} '${e}' ${r}. Received ${n}`; + E2("ERR_INVALID_ARG_VALUE", (name, value, reason = "is invalid") => { + let inspected = inspect2(value); + if (inspected.length > 128) + inspected = inspected.slice(0, 128) + "..."; + return `The ${name.includes(".") ? "property" : "argument"} '${name}' ${reason}. Received ${inspected}`; }, TypeError); - D("ERR_INVALID_RETURN_VALUE", (e, t, r) => { - var n; - let i = r != null && (n = r.constructor) !== null && n !== undefined && n.name ? `instance of ${r.constructor.name}` : `type ${typeof r}`; - return `Expected ${e} to be returned from the "${t}" function but got ${i}.`; + E2("ERR_INVALID_RETURN_VALUE", (input, name, value) => { + var _value$constructor; + let type = value !== null && value !== undefined && (_value$constructor = value.constructor) !== null && _value$constructor !== undefined && _value$constructor.name ? `instance of ${value.constructor.name}` : `type ${typeof value}`; + return `Expected ${input} to be returned from the "${name}" function but got ${type}.`; }, TypeError); - D("ERR_MISSING_ARGS", (...e) => { - Ae(e.length > 0, "At least one arg needs to be specified"); - let t, r = e.length; - switch (e = (Array.isArray(e) ? e : [e]).map((n) => `"${n}"`).join(" or "), r) { + E2("ERR_MISSING_ARGS", (...args) => { + assert(args.length > 0, "At least one arg needs to be specified"); + let msg, len2 = args.length; + switch (args = (Array.isArray(args) ? args : [args]).map((a) => `"${a}"`).join(" or "), len2) { case 1: - t += `The ${e[0]} argument`; + msg += `The ${args[0]} argument`; break; case 2: - t += `The ${e[0]} and ${e[1]} arguments`; + msg += `The ${args[0]} and ${args[1]} arguments`; break; default: { - let n = e.pop(); - t += `The ${e.join(", ")}, and ${n} arguments`; + let last = args.pop(); + msg += `The ${args.join(", ")}, and ${last} arguments`; } break; } - return `${t} must be specified`; + return `${msg} must be specified`; }, TypeError); - D("ERR_OUT_OF_RANGE", (e, t, r) => { - Ae(t, 'Missing "range" argument'); - let n; - if (Number.isInteger(r) && Math.abs(r) > 2 ** 32) - n = ni(String(r)); - else if (typeof r == "bigint") { - n = String(r); - let i = BigInt(2) ** BigInt(32); - (r > i || r < -i) && (n = ni(n)), n += "n"; + E2("ERR_OUT_OF_RANGE", (str, range, input) => { + assert(range, 'Missing "range" argument'); + let received; + if (Number.isInteger(input) && Math.abs(input) > 4294967296) + received = addNumericalSeparator2(String(input)); + else if (typeof input === "bigint") { + received = String(input); + let limit = BigInt(2) ** BigInt(32); + if (input > limit || input < -limit) + received = addNumericalSeparator2(received); + received += "n"; } else - n = Rt(r); - return `The value of "${e}" is out of range. It must be ${t}. Received ${n}`; + received = inspect2(input); + return `The value of "${str}" is out of range. It must be ${range}. Received ${received}`; }, RangeError); - D("ERR_MULTIPLE_CALLBACK", "Callback called multiple times", Error); - D("ERR_METHOD_NOT_IMPLEMENTED", "The %s method is not implemented", Error); - D("ERR_STREAM_ALREADY_FINISHED", "Cannot call %s after a stream was finished", Error); - D("ERR_STREAM_CANNOT_PIPE", "Cannot pipe, not readable", Error); - D("ERR_STREAM_DESTROYED", "Cannot call %s after a stream was destroyed", Error); - D("ERR_STREAM_NULL_VALUES", "May not write null values to stream", TypeError); - D("ERR_STREAM_PREMATURE_CLOSE", "Premature close", Error); - D("ERR_STREAM_PUSH_AFTER_EOF", "stream.push() after EOF", Error); - D("ERR_STREAM_UNSHIFT_AFTER_END_EVENT", "stream.unshift() after end event", Error); - D("ERR_STREAM_WRITE_AFTER_END", "write after end", Error); - D("ERR_UNKNOWN_ENCODING", "Unknown encoding: %s", TypeError); - oi.exports = { AbortError: gr, aggregateTwoErrors: ii(os), hideStackFrames: ii, codes: At }; - }); - We = S((Dh, xt) => { - var { AbortController: li, AbortSignal: ls } = typeof self < "u" ? self : typeof window < "u" ? window : undefined; - xt.exports = li; - xt.exports.AbortSignal = ls; - xt.exports.default = li; + E2("ERR_MULTIPLE_CALLBACK", "Callback called multiple times", Error); + E2("ERR_METHOD_NOT_IMPLEMENTED", "The %s method is not implemented", Error); + E2("ERR_STREAM_ALREADY_FINISHED", "Cannot call %s after a stream was finished", Error); + E2("ERR_STREAM_CANNOT_PIPE", "Cannot pipe, not readable", Error); + E2("ERR_STREAM_DESTROYED", "Cannot call %s after a stream was destroyed", Error); + E2("ERR_STREAM_NULL_VALUES", "May not write null values to stream", TypeError); + E2("ERR_STREAM_PREMATURE_CLOSE", "Premature close", Error); + E2("ERR_STREAM_PUSH_AFTER_EOF", "stream.push() after EOF", Error); + E2("ERR_STREAM_UNSHIFT_AFTER_END_EVENT", "stream.unshift() after end event", Error); + E2("ERR_STREAM_WRITE_AFTER_END", "write after end", Error); + E2("ERR_UNKNOWN_ENCODING", "Unknown encoding: %s", TypeError); + module2.exports = { AbortError: AbortError2, aggregateTwoErrors: hideStackFrames(aggregateTwoErrors), hideStackFrames, codes }; }); - it = S((Oh, _r) => { - var $e = typeof Reflect == "object" ? Reflect : null, ui = $e && typeof $e.apply == "function" ? $e.apply : function(t, r, n) { - return Function.prototype.apply.call(t, r, n); - }, It; - $e && typeof $e.ownKeys == "function" ? It = $e.ownKeys : Object.getOwnPropertySymbols ? It = function(t) { - return Object.getOwnPropertyNames(t).concat(Object.getOwnPropertySymbols(t)); - } : It = function(t) { - return Object.getOwnPropertyNames(t); - }; - function us(e) { - console && console.warn && console.warn(e); - } - var ai = Number.isNaN || function(t) { - return t !== t; - }; - function m() { - m.init.call(this); - } - _r.exports = m; - _r.exports.once = cs; - m.EventEmitter = m; - m.prototype._events = undefined; - m.prototype._eventsCount = 0; - m.prototype._maxListeners = undefined; - var si = 10; - function Tt(e) { - if (typeof e != "function") - throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof e); - } - Object.defineProperty(m, "defaultMaxListeners", { enumerable: true, get: function() { - return si; - }, set: function(e) { - if (typeof e != "number" || e < 0 || ai(e)) - throw new RangeError('The value of "defaultMaxListeners" is out of range. It must be a non-negative number. Received ' + e + "."); - si = e; - } }); - m.init = function() { - (this._events === undefined || this._events === Object.getPrototypeOf(this)._events) && (this._events = Object.create(null), this._eventsCount = 0), this._maxListeners = this._maxListeners || undefined; - }; - m.prototype.setMaxListeners = function(t) { - if (typeof t != "number" || t < 0 || ai(t)) - throw new RangeError('The value of "n" is out of range. It must be a non-negative number. Received ' + t + "."); - return this._maxListeners = t, this; - }; - function fi(e) { - return e._maxListeners === undefined ? m.defaultMaxListeners : e._maxListeners; - } - m.prototype.getMaxListeners = function() { - return fi(this); - }; - m.prototype.emit = function(t) { - for (var r = [], n = 1;n < arguments.length; n++) - r.push(arguments[n]); - var i = t === "error", o = this._events; - if (o !== undefined) - i = i && o.error === undefined; - else if (!i) - return false; - if (i) { - var l; - if (r.length > 0 && (l = r[0]), l instanceof Error) - throw l; - var u = new Error("Unhandled error." + (l ? " (" + l.message + ")" : "")); - throw u.context = l, u; - } - var a = o[t]; - if (a === undefined) - return false; - if (typeof a == "function") - ui(a, this, r); - else - for (var s = a.length, c = yi(a, s), n = 0;n < s; ++n) - ui(c[n], this, r); - return true; - }; - function ci(e, t, r, n) { - var i, o, l; - if (Tt(r), o = e._events, o === undefined ? (o = e._events = Object.create(null), e._eventsCount = 0) : (o.newListener !== undefined && (e.emit("newListener", t, r.listener ? r.listener : r), o = e._events), l = o[t]), l === undefined) - l = o[t] = r, ++e._eventsCount; - else if (typeof l == "function" ? l = o[t] = n ? [r, l] : [l, r] : n ? l.unshift(r) : l.push(r), i = fi(e), i > 0 && l.length > i && !l.warned) { - l.warned = true; - var u = new Error("Possible EventEmitter memory leak detected. " + l.length + " " + String(t) + " listeners added. Use emitter.setMaxListeners() to increase limit"); - u.name = "MaxListenersExceededWarning", u.emitter = e, u.type = t, u.count = l.length, us(u); - } - return e; - } - m.prototype.addListener = function(t, r) { - return ci(this, t, r, false); - }; - m.prototype.on = m.prototype.addListener; - m.prototype.prependListener = function(t, r) { - return ci(this, t, r, true); - }; - function ss() { - if (!this.fired) - return this.target.removeListener(this.type, this.wrapFn), this.fired = true, arguments.length === 0 ? this.listener.call(this.target) : this.listener.apply(this.target, arguments); - } - function di(e, t, r) { - var n = { fired: false, wrapFn: undefined, target: e, type: t, listener: r }, i = ss.bind(n); - return i.listener = r, n.wrapFn = i, i; - } - m.prototype.once = function(t, r) { - return Tt(r), this.on(t, di(this, t, r)), this; - }; - m.prototype.prependOnceListener = function(t, r) { - return Tt(r), this.prependListener(t, di(this, t, r)), this; - }; - m.prototype.removeListener = function(t, r) { - var n, i, o, l, u; - if (Tt(r), i = this._events, i === undefined) - return this; - if (n = i[t], n === undefined) - return this; - if (n === r || n.listener === r) - --this._eventsCount === 0 ? this._events = Object.create(null) : (delete i[t], i.removeListener && this.emit("removeListener", t, n.listener || r)); - else if (typeof n != "function") { - for (o = -1, l = n.length - 1;l >= 0; l--) - if (n[l] === r || n[l].listener === r) { - u = n[l].listener, o = l; - break; - } - if (o < 0) - return this; - o === 0 ? n.shift() : as(n, o), n.length === 1 && (i[t] = n[0]), i.removeListener !== undefined && this.emit("removeListener", t, u || r); + var require_event_target_shim = __commonJS2((exports2, module2) => { + Object.defineProperty(exports2, "__esModule", { value: true }); + var privateData = new WeakMap, wrappers = new WeakMap; + function pd(event) { + let retv = privateData.get(event); + return console.assert(retv != null, "'this' is expected an Event object, but got", event), retv; + } + function setCancelFlag(data) { + if (data.passiveListener != null) { + if (typeof console !== "undefined" && typeof console.error === "function") + console.error("Unable to preventDefault inside passive event listener invocation.", data.passiveListener); + return; } - return this; - }; - m.prototype.off = m.prototype.removeListener; - m.prototype.removeAllListeners = function(t) { - var r, n, i; - if (n = this._events, n === undefined) - return this; - if (n.removeListener === undefined) - return arguments.length === 0 ? (this._events = Object.create(null), this._eventsCount = 0) : n[t] !== undefined && (--this._eventsCount === 0 ? this._events = Object.create(null) : delete n[t]), this; - if (arguments.length === 0) { - var o = Object.keys(n), l; - for (i = 0;i < o.length; ++i) - l = o[i], l !== "removeListener" && this.removeAllListeners(l); - return this.removeAllListeners("removeListener"), this._events = Object.create(null), this._eventsCount = 0, this; - } - if (r = n[t], typeof r == "function") - this.removeListener(t, r); - else if (r !== undefined) - for (i = r.length - 1;i >= 0; i--) - this.removeListener(t, r[i]); - return this; - }; - function hi(e, t, r) { - var n = e._events; - if (n === undefined) + if (!data.event.cancelable) + return; + if (data.canceled = true, typeof data.event.preventDefault === "function") + data.event.preventDefault(); + } + function Event(eventTarget, event) { + privateData.set(this, { eventTarget, event, eventPhase: 2, currentTarget: eventTarget, canceled: false, stopped: false, immediateStopped: false, passiveListener: null, timeStamp: event.timeStamp || Date.now() }), Object.defineProperty(this, "isTrusted", { value: false, enumerable: true }); + let keys = Object.keys(event); + for (let i2 = 0;i2 < keys.length; ++i2) { + let key = keys[i2]; + if (!(key in this)) + Object.defineProperty(this, key, defineRedirectDescriptor(key)); + } + } + Event.prototype = { get type() { + return pd(this).event.type; + }, get target() { + return pd(this).eventTarget; + }, get currentTarget() { + return pd(this).currentTarget; + }, composedPath() { + let currentTarget = pd(this).currentTarget; + if (currentTarget == null) return []; - var i = n[t]; - return i === undefined ? [] : typeof i == "function" ? r ? [i.listener || i] : [i] : r ? fs(i) : yi(i, i.length); - } - m.prototype.listeners = function(t) { - return hi(this, t, true); - }; - m.prototype.rawListeners = function(t) { - return hi(this, t, false); - }; - m.listenerCount = function(e, t) { - return typeof e.listenerCount == "function" ? e.listenerCount(t) : pi.call(e, t); - }; - m.prototype.listenerCount = pi; - function pi(e) { - var t = this._events; - if (t !== undefined) { - var r = t[e]; - if (typeof r == "function") - return 1; - if (r !== undefined) - return r.length; - } + return [currentTarget]; + }, get NONE() { return 0; + }, get CAPTURING_PHASE() { + return 1; + }, get AT_TARGET() { + return 2; + }, get BUBBLING_PHASE() { + return 3; + }, get eventPhase() { + return pd(this).eventPhase; + }, stopPropagation() { + let data = pd(this); + if (data.stopped = true, typeof data.event.stopPropagation === "function") + data.event.stopPropagation(); + }, stopImmediatePropagation() { + let data = pd(this); + if (data.stopped = true, data.immediateStopped = true, typeof data.event.stopImmediatePropagation === "function") + data.event.stopImmediatePropagation(); + }, get bubbles() { + return Boolean(pd(this).event.bubbles); + }, get cancelable() { + return Boolean(pd(this).event.cancelable); + }, preventDefault() { + setCancelFlag(pd(this)); + }, get defaultPrevented() { + return pd(this).canceled; + }, get composed() { + return Boolean(pd(this).event.composed); + }, get timeStamp() { + return pd(this).timeStamp; + }, get srcElement() { + return pd(this).eventTarget; + }, get cancelBubble() { + return pd(this).stopped; + }, set cancelBubble(value) { + if (!value) + return; + let data = pd(this); + if (data.stopped = true, typeof data.event.cancelBubble === "boolean") + data.event.cancelBubble = true; + }, get returnValue() { + return !pd(this).canceled; + }, set returnValue(value) { + if (!value) + setCancelFlag(pd(this)); + }, initEvent() {} }; + Object.defineProperty(Event.prototype, "constructor", { value: Event, configurable: true, writable: true }); + if (typeof window !== "undefined" && typeof window.Event !== "undefined") + Object.setPrototypeOf(Event.prototype, window.Event.prototype), wrappers.set(window.Event.prototype, Event); + function defineRedirectDescriptor(key) { + return { get() { + return pd(this).event[key]; + }, set(value) { + pd(this).event[key] = value; + }, configurable: true, enumerable: true }; + } + function defineCallDescriptor(key) { + return { value() { + let event = pd(this).event; + return event[key].apply(event, arguments); + }, configurable: true, enumerable: true }; + } + function defineWrapper(BaseEvent, proto) { + let keys = Object.keys(proto); + if (keys.length === 0) + return BaseEvent; + function CustomEvent(eventTarget, event) { + BaseEvent.call(this, eventTarget, event); + } + CustomEvent.prototype = Object.create(BaseEvent.prototype, { constructor: { value: CustomEvent, configurable: true, writable: true } }); + for (let i2 = 0;i2 < keys.length; ++i2) { + let key = keys[i2]; + if (!(key in BaseEvent.prototype)) { + let isFunc = typeof Object.getOwnPropertyDescriptor(proto, key).value === "function"; + Object.defineProperty(CustomEvent.prototype, key, isFunc ? defineCallDescriptor(key) : defineRedirectDescriptor(key)); + } + } + return CustomEvent; + } + function getWrapper(proto) { + if (proto == null || proto === Object.prototype) + return Event; + let wrapper = wrappers.get(proto); + if (wrapper == null) + wrapper = defineWrapper(getWrapper(Object.getPrototypeOf(proto)), proto), wrappers.set(proto, wrapper); + return wrapper; + } + function wrapEvent(eventTarget, event) { + return new (getWrapper(Object.getPrototypeOf(event)))(eventTarget, event); + } + function isStopped(event) { + return pd(event).immediateStopped; + } + function setEventPhase(event, eventPhase) { + pd(event).eventPhase = eventPhase; + } + function setCurrentTarget(event, currentTarget) { + pd(event).currentTarget = currentTarget; + } + function setPassiveListener(event, passiveListener) { + pd(event).passiveListener = passiveListener; + } + var listenersMap = new WeakMap, CAPTURE = 1, BUBBLE = 2, ATTRIBUTE = 3; + function isObject(x) { + return x !== null && typeof x === "object"; + } + function getListeners(eventTarget) { + let listeners2 = listenersMap.get(eventTarget); + if (listeners2 == null) + throw new TypeError("'this' is expected an EventTarget object, but got another value."); + return listeners2; + } + function defineEventAttributeDescriptor(eventName) { + return { get() { + let node = getListeners(this).get(eventName); + while (node != null) { + if (node.listenerType === ATTRIBUTE) + return node.listener; + node = node.next; + } + return null; + }, set(listener) { + if (typeof listener !== "function" && !isObject(listener)) + listener = null; + let listeners2 = getListeners(this), prev = null, node = listeners2.get(eventName); + while (node != null) { + if (node.listenerType === ATTRIBUTE) + if (prev !== null) + prev.next = node.next; + else if (node.next !== null) + listeners2.set(eventName, node.next); + else + listeners2.delete(eventName); + else + prev = node; + node = node.next; + } + if (listener !== null) { + let newNode = { listener, listenerType: ATTRIBUTE, passive: false, once: false, next: null }; + if (prev === null) + listeners2.set(eventName, newNode); + else + prev.next = newNode; + } + }, configurable: true, enumerable: true }; } - m.prototype.eventNames = function() { - return this._eventsCount > 0 ? It(this._events) : []; - }; - function yi(e, t) { - for (var r = new Array(t), n = 0;n < t; ++n) - r[n] = e[n]; - return r; + function defineEventAttribute(eventTargetPrototype, eventName) { + Object.defineProperty(eventTargetPrototype, `on${eventName}`, defineEventAttributeDescriptor(eventName)); } - function as(e, t) { - for (;t + 1 < e.length; t++) - e[t] = e[t + 1]; - e.pop(); + function defineCustomEventTarget(eventNames2) { + function CustomEventTarget() { + EventTarget.call(this); + } + CustomEventTarget.prototype = Object.create(EventTarget.prototype, { constructor: { value: CustomEventTarget, configurable: true, writable: true } }); + for (let i2 = 0;i2 < eventNames2.length; ++i2) + defineEventAttribute(CustomEventTarget.prototype, eventNames2[i2]); + return CustomEventTarget; } - function fs(e) { - for (var t = new Array(e.length), r = 0;r < t.length; ++r) - t[r] = e[r].listener || e[r]; - return t; + function EventTarget() { + if (this instanceof EventTarget) { + listenersMap.set(this, new Map); + return; + } + if (arguments.length === 1 && Array.isArray(arguments[0])) + return defineCustomEventTarget(arguments[0]); + if (arguments.length > 0) { + let types = new Array(arguments.length); + for (let i2 = 0;i2 < arguments.length; ++i2) + types[i2] = arguments[i2]; + return defineCustomEventTarget(types); + } + throw new TypeError("Cannot call a class as a function"); } - function cs(e, t) { - return new Promise(function(r, n) { - function i(l) { - e.removeListener(t, o), n(l); - } - function o() { - typeof e.removeListener == "function" && e.removeListener("error", i), r([].slice.call(arguments)); + EventTarget.prototype = { addEventListener(eventName, listener, options) { + if (listener == null) + return; + if (typeof listener !== "function" && !isObject(listener)) + throw new TypeError("'listener' should be a function or an object."); + let listeners2 = getListeners(this), optionsIsObj = isObject(options), listenerType = (optionsIsObj ? Boolean(options.capture) : Boolean(options)) ? CAPTURE : BUBBLE, newNode = { listener, listenerType, passive: optionsIsObj && Boolean(options.passive), once: optionsIsObj && Boolean(options.once), next: null }, node = listeners2.get(eventName); + if (node === undefined) { + listeners2.set(eventName, newNode); + return; + } + let prev = null; + while (node != null) { + if (node.listener === listener && node.listenerType === listenerType) + return; + prev = node, node = node.next; + } + prev.next = newNode; + }, removeEventListener(eventName, listener, options) { + if (listener == null) + return; + let listeners2 = getListeners(this), listenerType = (isObject(options) ? Boolean(options.capture) : Boolean(options)) ? CAPTURE : BUBBLE, prev = null, node = listeners2.get(eventName); + while (node != null) { + if (node.listener === listener && node.listenerType === listenerType) { + if (prev !== null) + prev.next = node.next; + else if (node.next !== null) + listeners2.set(eventName, node.next); + else + listeners2.delete(eventName); + return; } - bi(e, t, o, { once: true }), t !== "error" && ds(e, i, { once: true }); - }); + prev = node, node = node.next; + } + }, dispatchEvent(event) { + if (event == null || typeof event.type !== "string") + throw new TypeError('"event.type" should be a string.'); + let listeners2 = getListeners(this), eventName = event.type, node = listeners2.get(eventName); + if (node == null) + return true; + let wrappedEvent = wrapEvent(this, event), prev = null; + while (node != null) { + if (node.once) + if (prev !== null) + prev.next = node.next; + else if (node.next !== null) + listeners2.set(eventName, node.next); + else + listeners2.delete(eventName); + else + prev = node; + if (setPassiveListener(wrappedEvent, node.passive ? node.listener : null), typeof node.listener === "function") + try { + node.listener.call(this, wrappedEvent); + } catch (err) { + if (typeof console !== "undefined" && typeof console.error === "function") + console.error(err); + } + else if (node.listenerType !== ATTRIBUTE && typeof node.listener.handleEvent === "function") + node.listener.handleEvent(wrappedEvent); + if (isStopped(wrappedEvent)) + break; + node = node.next; + } + return setPassiveListener(wrappedEvent, null), setEventPhase(wrappedEvent, 0), setCurrentTarget(wrappedEvent, null), !wrappedEvent.defaultPrevented; + } }; + Object.defineProperty(EventTarget.prototype, "constructor", { value: EventTarget, configurable: true, writable: true }); + if (typeof window !== "undefined" && typeof window.EventTarget !== "undefined") + Object.setPrototypeOf(EventTarget.prototype, window.EventTarget.prototype); + exports2.defineEventAttribute = defineEventAttribute; + exports2.EventTarget = EventTarget; + exports2.default = EventTarget; + module2.exports = EventTarget; + module2.exports.EventTarget = module2.exports.default = EventTarget; + module2.exports.defineEventAttribute = defineEventAttribute; + }); + var require_abort_controller = __commonJS2((exports2, module2) => { + Object.defineProperty(exports2, "__esModule", { value: true }); + var eventTargetShim = require_event_target_shim(); + + class AbortSignal extends eventTargetShim.EventTarget { + constructor() { + super(); + throw new TypeError("AbortSignal cannot be constructed directly"); + } + get aborted() { + let aborted = abortedFlags.get(this); + if (typeof aborted !== "boolean") + throw new TypeError(`Expected 'this' to be an 'AbortSignal' object, but got ${this === null ? "null" : typeof this}`); + return aborted; + } } - function ds(e, t, r) { - typeof e.on == "function" && bi(e, "error", t, r); + eventTargetShim.defineEventAttribute(AbortSignal.prototype, "abort"); + function createAbortSignal() { + let signal = Object.create(AbortSignal.prototype); + return eventTargetShim.EventTarget.call(signal), abortedFlags.set(signal, false), signal; } - function bi(e, t, r, n) { - if (typeof e.on == "function") - n.once ? e.once(t, r) : e.on(t, r); - else if (typeof e.addEventListener == "function") - e.addEventListener(t, function i(o) { - n.once && e.removeEventListener(t, i), r(o); - }); - else - throw new TypeError('The "emitter" argument must be of type EventEmitter. Received type ' + typeof e); + function abortSignal(signal) { + if (abortedFlags.get(signal) !== false) + return; + abortedFlags.set(signal, true), signal.dispatchEvent({ type: "abort" }); } + var abortedFlags = new WeakMap; + Object.defineProperties(AbortSignal.prototype, { aborted: { enumerable: true } }); + if (typeof Symbol === "function" && typeof Symbol.toStringTag === "symbol") + Object.defineProperty(AbortSignal.prototype, Symbol.toStringTag, { configurable: true, value: "AbortSignal" }); + + class AbortController { + constructor() { + signals.set(this, createAbortSignal()); + } + get signal() { + return getSignal(this); + } + abort() { + abortSignal(getSignal(this)); + } + } + var signals = new WeakMap; + function getSignal(controller) { + let signal = signals.get(controller); + if (signal == null) + throw new TypeError(`Expected 'this' to be an 'AbortController' object, but got ${controller === null ? "null" : typeof controller}`); + return signal; + } + Object.defineProperties(AbortController.prototype, { signal: { enumerable: true }, abort: { enumerable: true } }); + if (typeof Symbol === "function" && typeof Symbol.toStringTag === "symbol") + Object.defineProperty(AbortController.prototype, Symbol.toStringTag, { configurable: true, value: "AbortController" }); + exports2.AbortController = AbortController; + exports2.AbortSignal = AbortSignal; + exports2.default = AbortController; + module2.exports = AbortController; + module2.exports.AbortController = module2.exports.default = AbortController; + module2.exports.AbortSignal = AbortSignal; }); - q = S((Ph, Sr) => { - var hs = le(), { format: ps, inspect: ys } = wr(), { codes: { ERR_INVALID_ARG_TYPE: Er } } = O(), { kResistStopPropagation: bs, AggregateError: ws, SymbolDispose: gs } = I(), _s = globalThis.AbortSignal || We().AbortSignal, Es = globalThis.AbortController || We().AbortController, Ss = Object.getPrototypeOf(async function() { - }).constructor, wi = globalThis.Blob || hs.Blob, ms = typeof wi < "u" ? function(t) { - return t instanceof wi; - } : function(t) { + var require_util = __commonJS2((exports2, module2) => { + var bufferModule = (init_buffer(), __toCommonJS(exports_buffer)), { format, inspect: inspect2 } = require_inspect(), { codes: { ERR_INVALID_ARG_TYPE: ERR_INVALID_ARG_TYPE3 } } = require_errors(), { kResistStopPropagation, AggregateError, SymbolDispose } = require_primordials(), AbortSignal = globalThis.AbortSignal || require_abort_controller().AbortSignal, AbortController = globalThis.AbortController || require_abort_controller().AbortController, AsyncFunction = Object.getPrototypeOf(async function() {}).constructor, Blob3 = globalThis.Blob || bufferModule.Blob, isBlob = typeof Blob3 !== "undefined" ? function isBlob(b) { + return b instanceof Blob3; + } : function isBlob(b) { return false; - }, gi = (e, t) => { - if (e !== undefined && (e === null || typeof e != "object" || !("aborted" in e))) - throw new Er(t, "AbortSignal", e); - }, Rs = (e, t) => { - if (typeof e != "function") - throw new Er(t, "Function", e); - }; - Sr.exports = { AggregateError: ws, kEmptyObject: Object.freeze({}), once(e) { - let t = false; - return function(...r) { - t || (t = true, e.apply(this, r)); + }, validateAbortSignal2 = (signal, name) => { + if (signal !== undefined && (signal === null || typeof signal !== "object" || !("aborted" in signal))) + throw new ERR_INVALID_ARG_TYPE3(name, "AbortSignal", signal); + }, validateFunction = (value, name) => { + if (typeof value !== "function") + throw new ERR_INVALID_ARG_TYPE3(name, "Function", value); + }; + module2.exports = { AggregateError, kEmptyObject: Object.freeze({}), once(callback) { + let called = false; + return function(...args) { + if (called) + return; + called = true, callback.apply(this, args); }; }, createDeferredPromise: function() { - let e, t; - return { promise: new Promise((n, i) => { - e = n, t = i; - }), resolve: e, reject: t }; - }, promisify(e) { - return new Promise((t, r) => { - e((n, ...i) => n ? r(n) : t(...i)); + let resolve, reject; + return { promise: new Promise((res, rej) => { + resolve = res, reject = rej; + }), resolve, reject }; + }, promisify(fn) { + return new Promise((resolve, reject) => { + fn((err, ...args) => { + if (err) + return reject(err); + return resolve(...args); + }); }); }, debuglog() { - return function() { - }; - }, format: ps, inspect: ys, types: { isAsyncFunction(e) { - return e instanceof Ss; - }, isArrayBufferView(e) { - return ArrayBuffer.isView(e); - } }, isBlob: ms, deprecate(e, t) { - return e; - }, addAbortListener: it().addAbortListener || function(t, r) { - if (t === undefined) - throw new Er("signal", "AbortSignal", t); - gi(t, "signal"), Rs(r, "listener"); - let n; - return t.aborted ? queueMicrotask(() => r()) : (t.addEventListener("abort", r, { __proto__: null, once: true, [bs]: true }), n = () => { - t.removeEventListener("abort", r); - }), { __proto__: null, [gs]() { - var i; - (i = n) === null || i === undefined || i(); + return function() {}; + }, format, inspect: inspect2, types: { isAsyncFunction(fn) { + return fn instanceof AsyncFunction; + }, isArrayBufferView(arr) { + return ArrayBuffer.isView(arr); + } }, isBlob, deprecate(fn, message) { + return fn; + }, addAbortListener: (init_events(), __toCommonJS(exports_events)).addAbortListener || function addAbortListener(signal, listener) { + if (signal === undefined) + throw new ERR_INVALID_ARG_TYPE3("signal", "AbortSignal", signal); + validateAbortSignal2(signal, "signal"), validateFunction(listener, "listener"); + let removeEventListener; + if (signal.aborted) + queueMicrotask(() => listener()); + else + signal.addEventListener("abort", listener, { __proto__: null, once: true, [kResistStopPropagation]: true }), removeEventListener = () => { + signal.removeEventListener("abort", listener); + }; + return { __proto__: null, [SymbolDispose]() { + var _removeEventListener; + (_removeEventListener = removeEventListener) === null || _removeEventListener === undefined || _removeEventListener(); } }; - }, AbortSignalAny: _s.any || function(t) { - if (t.length === 1) - return t[0]; - let r = new Es, n = () => r.abort(); - return t.forEach((i) => { - gi(i, "signals"), i.addEventListener("abort", n, { once: true }); - }), r.signal.addEventListener("abort", () => { - t.forEach((i) => i.removeEventListener("abort", n)); - }, { once: true }), r.signal; + }, AbortSignalAny: AbortSignal.any || function AbortSignalAny(signals) { + if (signals.length === 1) + return signals[0]; + let ac = new AbortController, abort = () => ac.abort(); + return signals.forEach((signal) => { + validateAbortSignal2(signal, "signals"), signal.addEventListener("abort", abort, { once: true }); + }), ac.signal.addEventListener("abort", () => { + signals.forEach((signal) => signal.removeEventListener("abort", abort)); + }, { once: true }), ac.signal; } }; - Sr.exports.promisify.custom = Symbol.for("nodejs.util.promisify.custom"); + module2.exports.promisify.custom = Symbol.for("nodejs.util.promisify.custom"); }); - He = S((qh, Bi) => { - var { ArrayIsArray: Rr, ArrayPrototypeIncludes: mi, ArrayPrototypeJoin: Ri, ArrayPrototypeMap: As, NumberIsInteger: Ar, NumberIsNaN: xs, NumberMAX_SAFE_INTEGER: Is, NumberMIN_SAFE_INTEGER: Ts, NumberParseInt: Bs, ObjectPrototypeHasOwnProperty: Ls, RegExpPrototypeExec: Ai, String: Ns, StringPrototypeToUpperCase: Fs, StringPrototypeTrim: Ms } = I(), { hideStackFrames: j, codes: { ERR_SOCKET_BAD_PORT: Cs, ERR_INVALID_ARG_TYPE: P, ERR_INVALID_ARG_VALUE: je, ERR_OUT_OF_RANGE: xe, ERR_UNKNOWN_SIGNAL: _i } } = O(), { normalizeEncoding: ks } = q(), { isAsyncFunction: Ds, isArrayBufferView: Os } = q().types, Ei = {}; - function Ps(e) { - return e === (e | 0); - } - function qs(e) { - return e === e >>> 0; - } - var vs = /^[0-7]+$/, Us = "must be a 32-bit unsigned integer or an octal string"; - function Ws(e, t, r) { - if (typeof e > "u" && (e = r), typeof e == "string") { - if (Ai(vs, e) === null) - throw new je(t, e, Us); - e = Bs(e, 8); - } - return xi(e, t), e; - } - var $s = j((e, t, r = Ts, n = Is) => { - if (typeof e != "number") - throw new P(t, "number", e); - if (!Ar(e)) - throw new xe(t, "an integer", e); - if (e < r || e > n) - throw new xe(t, `>= ${r} && <= ${n}`, e); - }), js = j((e, t, r = -2147483648, n = 2147483647) => { - if (typeof e != "number") - throw new P(t, "number", e); - if (!Ar(e)) - throw new xe(t, "an integer", e); - if (e < r || e > n) - throw new xe(t, `>= ${r} && <= ${n}`, e); - }), xi = j((e, t, r = false) => { - if (typeof e != "number") - throw new P(t, "number", e); - if (!Ar(e)) - throw new xe(t, "an integer", e); - let n = r ? 1 : 0, i = 4294967295; - if (e < n || e > i) - throw new xe(t, `>= ${n} && <= ${i}`, e); + var require_validators = __commonJS2((exports2, module2) => { + var { ArrayIsArray, ArrayPrototypeIncludes, ArrayPrototypeJoin, ArrayPrototypeMap, NumberIsInteger, NumberIsNaN, NumberMAX_SAFE_INTEGER, NumberMIN_SAFE_INTEGER, NumberParseInt, ObjectPrototypeHasOwnProperty, RegExpPrototypeExec, String: String2, StringPrototypeToUpperCase, StringPrototypeTrim } = require_primordials(), { hideStackFrames, codes: { ERR_SOCKET_BAD_PORT, ERR_INVALID_ARG_TYPE: ERR_INVALID_ARG_TYPE3, ERR_INVALID_ARG_VALUE, ERR_OUT_OF_RANGE: ERR_OUT_OF_RANGE3, ERR_UNKNOWN_SIGNAL } } = require_errors(), { normalizeEncoding } = require_util(), { isAsyncFunction, isArrayBufferView } = require_util().types, signals = {}; + function isInt32(value) { + return value === (value | 0); + } + function isUint32(value) { + return value === value >>> 0; + } + var octalReg = /^[0-7]+$/, modeDesc = "must be a 32-bit unsigned integer or an octal string"; + function parseFileMode(value, name, def) { + if (typeof value === "undefined") + value = def; + if (typeof value === "string") { + if (RegExpPrototypeExec(octalReg, value) === null) + throw new ERR_INVALID_ARG_VALUE(name, value, modeDesc); + value = NumberParseInt(value, 8); + } + return validateUint32(value, name), value; + } + var validateInteger = hideStackFrames((value, name, min = NumberMIN_SAFE_INTEGER, max = NumberMAX_SAFE_INTEGER) => { + if (typeof value !== "number") + throw new ERR_INVALID_ARG_TYPE3(name, "number", value); + if (!NumberIsInteger(value)) + throw new ERR_OUT_OF_RANGE3(name, "an integer", value); + if (value < min || value > max) + throw new ERR_OUT_OF_RANGE3(name, `>= ${min} && <= ${max}`, value); + }), validateInt32 = hideStackFrames((value, name, min = -2147483648, max = 2147483647) => { + if (typeof value !== "number") + throw new ERR_INVALID_ARG_TYPE3(name, "number", value); + if (!NumberIsInteger(value)) + throw new ERR_OUT_OF_RANGE3(name, "an integer", value); + if (value < min || value > max) + throw new ERR_OUT_OF_RANGE3(name, `>= ${min} && <= ${max}`, value); + }), validateUint32 = hideStackFrames((value, name, positive = false) => { + if (typeof value !== "number") + throw new ERR_INVALID_ARG_TYPE3(name, "number", value); + if (!NumberIsInteger(value)) + throw new ERR_OUT_OF_RANGE3(name, "an integer", value); + let min = positive ? 1 : 0, max = 4294967295; + if (value < min || value > max) + throw new ERR_OUT_OF_RANGE3(name, `>= ${min} && <= ${max}`, value); }); - function xr(e, t) { - if (typeof e != "string") - throw new P(t, "string", e); + function validateString(value, name) { + if (typeof value !== "string") + throw new ERR_INVALID_ARG_TYPE3(name, "string", value); } - function Hs(e, t, r = undefined, n) { - if (typeof e != "number") - throw new P(t, "number", e); - if (r != null && e < r || n != null && e > n || (r != null || n != null) && xs(e)) - throw new xe(t, `${r != null ? `>= ${r}` : ""}${r != null && n != null ? " && " : ""}${n != null ? `<= ${n}` : ""}`, e); + function validateNumber3(value, name, min = undefined, max) { + if (typeof value !== "number") + throw new ERR_INVALID_ARG_TYPE3(name, "number", value); + if (min != null && value < min || max != null && value > max || (min != null || max != null) && NumberIsNaN(value)) + throw new ERR_OUT_OF_RANGE3(name, `${min != null ? `>= ${min}` : ""}${min != null && max != null ? " && " : ""}${max != null ? `<= ${max}` : ""}`, value); } - var Gs = j((e, t, r) => { - if (!mi(r, e)) { - let n = Ri(As(r, (o) => typeof o == "string" ? `'${o}'` : Ns(o)), ", "), i = "must be one of: " + n; - throw new je(t, e, i); + var validateOneOf = hideStackFrames((value, name, oneOf) => { + if (!ArrayPrototypeIncludes(oneOf, value)) { + let reason = "must be one of: " + ArrayPrototypeJoin(ArrayPrototypeMap(oneOf, (v) => typeof v === "string" ? `'${v}'` : String2(v)), ", "); + throw new ERR_INVALID_ARG_VALUE(name, value, reason); } }); - function Ii(e, t) { - if (typeof e != "boolean") - throw new P(t, "boolean", e); - } - function mr(e, t, r) { - return e == null || !Ls(e, t) ? r : e[t]; - } - var Vs = j((e, t, r = null) => { - let n = mr(r, "allowArray", false), i = mr(r, "allowFunction", false); - if (!mr(r, "nullable", false) && e === null || !n && Rr(e) || typeof e != "object" && (!i || typeof e != "function")) - throw new P(t, "Object", e); - }), Ys = j((e, t) => { - if (e != null && typeof e != "object" && typeof e != "function") - throw new P(t, "a dictionary", e); - }), Bt = j((e, t, r = 0) => { - if (!Rr(e)) - throw new P(t, "Array", e); - if (e.length < r) { - let n = `must be longer than ${r}`; - throw new je(t, e, n); + function validateBoolean2(value, name) { + if (typeof value !== "boolean") + throw new ERR_INVALID_ARG_TYPE3(name, "boolean", value); + } + function getOwnPropertyValueOrDefault(options, key, defaultValue) { + return options == null || !ObjectPrototypeHasOwnProperty(options, key) ? defaultValue : options[key]; + } + var validateObject = hideStackFrames((value, name, options = null) => { + let allowArray = getOwnPropertyValueOrDefault(options, "allowArray", false), allowFunction = getOwnPropertyValueOrDefault(options, "allowFunction", false); + if (!getOwnPropertyValueOrDefault(options, "nullable", false) && value === null || !allowArray && ArrayIsArray(value) || typeof value !== "object" && (!allowFunction || typeof value !== "function")) + throw new ERR_INVALID_ARG_TYPE3(name, "Object", value); + }), validateDictionary = hideStackFrames((value, name) => { + if (value != null && typeof value !== "object" && typeof value !== "function") + throw new ERR_INVALID_ARG_TYPE3(name, "a dictionary", value); + }), validateArray = hideStackFrames((value, name, minLength = 0) => { + if (!ArrayIsArray(value)) + throw new ERR_INVALID_ARG_TYPE3(name, "Array", value); + if (value.length < minLength) { + let reason = `must be longer than ${minLength}`; + throw new ERR_INVALID_ARG_VALUE(name, value, reason); } }); - function Ks(e, t) { - Bt(e, t); - for (let r = 0;r < e.length; r++) - xr(e[r], `${t}[${r}]`); - } - function zs(e, t) { - Bt(e, t); - for (let r = 0;r < e.length; r++) - Ii(e[r], `${t}[${r}]`); - } - function Xs(e, t) { - Bt(e, t); - for (let r = 0;r < e.length; r++) { - let n = e[r], i = `${t}[${r}]`; - if (n == null) - throw new P(i, "AbortSignal", n); - Ti(n, i); - } - } - function Js(e, t = "signal") { - if (xr(e, t), Ei[e] === undefined) - throw Ei[Fs(e)] !== undefined ? new _i(e + " (signals must use all capital letters)") : new _i(e); - } - var Qs = j((e, t = "buffer") => { - if (!Os(e)) - throw new P(t, ["Buffer", "TypedArray", "DataView"], e); + function validateStringArray(value, name) { + validateArray(value, name); + for (let i2 = 0;i2 < value.length; i2++) + validateString(value[i2], `${name}[${i2}]`); + } + function validateBooleanArray(value, name) { + validateArray(value, name); + for (let i2 = 0;i2 < value.length; i2++) + validateBoolean2(value[i2], `${name}[${i2}]`); + } + function validateAbortSignalArray(value, name) { + validateArray(value, name); + for (let i2 = 0;i2 < value.length; i2++) { + let signal = value[i2], indexedName = `${name}[${i2}]`; + if (signal == null) + throw new ERR_INVALID_ARG_TYPE3(indexedName, "AbortSignal", signal); + validateAbortSignal2(signal, indexedName); + } + } + function validateSignalName(signal, name = "signal") { + if (validateString(signal, name), signals[signal] === undefined) { + if (signals[StringPrototypeToUpperCase(signal)] !== undefined) + throw new ERR_UNKNOWN_SIGNAL(signal + " (signals must use all capital letters)"); + throw new ERR_UNKNOWN_SIGNAL(signal); + } + } + var validateBuffer = hideStackFrames((buffer, name = "buffer") => { + if (!isArrayBufferView(buffer)) + throw new ERR_INVALID_ARG_TYPE3(name, ["Buffer", "TypedArray", "DataView"], buffer); }); - function Zs(e, t) { - let r = ks(t), n = e.length; - if (r === "hex" && n % 2 !== 0) - throw new je("encoding", t, `is invalid for data of length ${n}`); - } - function ea(e, t = "Port", r = true) { - if (typeof e != "number" && typeof e != "string" || typeof e == "string" && Ms(e).length === 0 || +e !== +e >>> 0 || e > 65535 || e === 0 && !r) - throw new Cs(t, e, r); - return e | 0; - } - var Ti = j((e, t) => { - if (e !== undefined && (e === null || typeof e != "object" || !("aborted" in e))) - throw new P(t, "AbortSignal", e); - }), ta = j((e, t) => { - if (typeof e != "function") - throw new P(t, "Function", e); - }), ra = j((e, t) => { - if (typeof e != "function" || Ds(e)) - throw new P(t, "Function", e); - }), na = j((e, t) => { - if (e !== undefined) - throw new P(t, "undefined", e); + function validateEncoding(data, encoding) { + let normalizedEncoding = normalizeEncoding(encoding), length = data.length; + if (normalizedEncoding === "hex" && length % 2 !== 0) + throw new ERR_INVALID_ARG_VALUE("encoding", encoding, `is invalid for data of length ${length}`); + } + function validatePort(port, name = "Port", allowZero = true) { + if (typeof port !== "number" && typeof port !== "string" || typeof port === "string" && StringPrototypeTrim(port).length === 0 || +port !== +port >>> 0 || port > 65535 || port === 0 && !allowZero) + throw new ERR_SOCKET_BAD_PORT(name, port, allowZero); + return port | 0; + } + var validateAbortSignal2 = hideStackFrames((signal, name) => { + if (signal !== undefined && (signal === null || typeof signal !== "object" || !("aborted" in signal))) + throw new ERR_INVALID_ARG_TYPE3(name, "AbortSignal", signal); + }), validateFunction = hideStackFrames((value, name) => { + if (typeof value !== "function") + throw new ERR_INVALID_ARG_TYPE3(name, "Function", value); + }), validatePlainFunction = hideStackFrames((value, name) => { + if (typeof value !== "function" || isAsyncFunction(value)) + throw new ERR_INVALID_ARG_TYPE3(name, "Function", value); + }), validateUndefined = hideStackFrames((value, name) => { + if (value !== undefined) + throw new ERR_INVALID_ARG_TYPE3(name, "undefined", value); }); - function ia(e, t, r) { - if (!mi(r, e)) - throw new P(t, `('${Ri(r, "|")}')`, e); - } - var oa = /^(?:<[^>]*>)(?:\s*;\s*[^;"\s]+(?:=(")?[^;"\s]*\1)?)*$/; - function Si(e, t) { - if (typeof e > "u" || !Ai(oa, e)) - throw new je(t, e, 'must be an array or string of format "; rel=preload; as=style"'); - } - function la(e) { - if (typeof e == "string") - return Si(e, "hints"), e; - if (Rr(e)) { - let t = e.length, r = ""; - if (t === 0) - return r; - for (let n = 0;n < t; n++) { - let i = e[n]; - Si(i, "hints"), r += i, n !== t - 1 && (r += ", "); - } - return r; - } - throw new je("hints", e, 'must be an array or string of format "; rel=preload; as=style"'); - } - Bi.exports = { isInt32: Ps, isUint32: qs, parseFileMode: Ws, validateArray: Bt, validateStringArray: Ks, validateBooleanArray: zs, validateAbortSignalArray: Xs, validateBoolean: Ii, validateBuffer: Qs, validateDictionary: Ys, validateEncoding: Zs, validateFunction: ta, validateInt32: js, validateInteger: $s, validateNumber: Hs, validateObject: Vs, validateOneOf: Gs, validatePlainFunction: ra, validatePort: ea, validateSignalName: Js, validateString: xr, validateUint32: xi, validateUndefined: na, validateUnion: ia, validateAbortSignal: Ti, validateLinkHeaderValue: la }; - }); - Br = S((vh, Mi) => { - var B = Mi.exports = {}, X, J; - function Ir() { - throw new Error("setTimeout has not been defined"); - } - function Tr() { - throw new Error("clearTimeout has not been defined"); - } - (function() { - try { - typeof setTimeout == "function" ? X = setTimeout : X = Ir; - } catch { - X = Ir; - } - try { - typeof clearTimeout == "function" ? J = clearTimeout : J = Tr; - } catch { - J = Tr; - } - })(); - function Li(e) { - if (X === setTimeout) - return setTimeout(e, 0); - if ((X === Ir || !X) && setTimeout) - return X = setTimeout, setTimeout(e, 0); - try { - return X(e, 0); - } catch { - try { - return X.call(null, e, 0); - } catch { - return X.call(this, e, 0); - } - } - } - function ua(e) { - if (J === clearTimeout) - return clearTimeout(e); - if ((J === Tr || !J) && clearTimeout) - return J = clearTimeout, clearTimeout(e); - try { - return J(e); - } catch { - try { - return J.call(null, e); - } catch { - return J.call(this, e); - } - } - } - var ue = [], Ge = false, Ie, Lt = -1; - function sa() { - !Ge || !Ie || (Ge = false, Ie.length ? ue = Ie.concat(ue) : Lt = -1, ue.length && Ni()); - } - function Ni() { - if (!Ge) { - var e = Li(sa); - Ge = true; - for (var t = ue.length;t; ) { - for (Ie = ue, ue = [];++Lt < t; ) - Ie && Ie[Lt].run(); - Lt = -1, t = ue.length; - } - Ie = null, Ge = false, ua(e); - } - } - B.nextTick = function(e) { - var t = new Array(arguments.length - 1); - if (arguments.length > 1) - for (var r = 1;r < arguments.length; r++) - t[r - 1] = arguments[r]; - ue.push(new Fi(e, t)), ue.length === 1 && !Ge && Li(Ni); - }; - function Fi(e, t) { - this.fun = e, this.array = t; - } - Fi.prototype.run = function() { - this.fun.apply(null, this.array); - }; - B.title = "browser"; - B.browser = true; - B.env = {}; - B.argv = []; - B.version = ""; - B.versions = {}; - function se() { - } - B.on = se; - B.addListener = se; - B.once = se; - B.off = se; - B.removeListener = se; - B.removeAllListeners = se; - B.emit = se; - B.prependListener = se; - B.prependOnceListener = se; - B.listeners = function(e) { - return []; - }; - B.binding = function(e) { - throw new Error("process.binding is not supported"); - }; - B.cwd = function() { - return "/"; - }; - B.chdir = function(e) { - throw new Error("process.chdir is not supported"); - }; - B.umask = function() { - return 0; - }; + function validateUnion(value, name, union) { + if (!ArrayPrototypeIncludes(union, value)) + throw new ERR_INVALID_ARG_TYPE3(name, `('${ArrayPrototypeJoin(union, "|")}')`, value); + } + var linkValueRegExp = /^(?:<[^>]*>)(?:\s*;\s*[^;"\s]+(?:=(")?[^;"\s]*\1)?)*$/; + function validateLinkHeaderFormat(value, name) { + if (typeof value === "undefined" || !RegExpPrototypeExec(linkValueRegExp, value)) + throw new ERR_INVALID_ARG_VALUE(name, value, 'must be an array or string of format "; rel=preload; as=style"'); + } + function validateLinkHeaderValue(hints) { + if (typeof hints === "string") + return validateLinkHeaderFormat(hints, "hints"), hints; + else if (ArrayIsArray(hints)) { + let hintsLength = hints.length, result = ""; + if (hintsLength === 0) + return result; + for (let i2 = 0;i2 < hintsLength; i2++) { + let link = hints[i2]; + if (validateLinkHeaderFormat(link, "hints"), result += link, i2 !== hintsLength - 1) + result += ", "; + } + return result; + } + throw new ERR_INVALID_ARG_VALUE("hints", hints, 'must be an array or string of format "; rel=preload; as=style"'); + } + module2.exports = { isInt32, isUint32, parseFileMode, validateArray, validateStringArray, validateBooleanArray, validateAbortSignalArray, validateBoolean: validateBoolean2, validateBuffer, validateDictionary, validateEncoding, validateFunction, validateInt32, validateInteger, validateNumber: validateNumber3, validateObject, validateOneOf, validatePlainFunction, validatePort, validateSignalName, validateString, validateUint32, validateUndefined, validateUnion, validateAbortSignal: validateAbortSignal2, validateLinkHeaderValue }; }); - H = {}; - or(H, { default: () => aa }); - we = kn(() => { - pe(H, rt(Br())); - aa = rt(Br()); + var require_process = __commonJS2((exports2, module2) => { + module2.exports = globalThis.process; }); - Z = S((Wh, Yi) => { - var { SymbolAsyncIterator: Ci, SymbolIterator: ki, SymbolFor: Te } = I(), Di = Te("nodejs.stream.destroyed"), Oi = Te("nodejs.stream.errored"), Lr = Te("nodejs.stream.readable"), Nr = Te("nodejs.stream.writable"), Pi = Te("nodejs.stream.disturbed"), fa = Te("nodejs.webstream.isClosedPromise"), ca = Te("nodejs.webstream.controllerErrorFunction"); - function Nt(e, t = false) { - var r; - return !!(e && typeof e.pipe == "function" && typeof e.on == "function" && (!t || typeof e.pause == "function" && typeof e.resume == "function") && (!e._writableState || ((r = e._readableState) === null || r === undefined ? undefined : r.readable) !== false) && (!e._writableState || e._readableState)); - } - function Ft(e) { - var t; - return !!(e && typeof e.write == "function" && typeof e.on == "function" && (!e._readableState || ((t = e._writableState) === null || t === undefined ? undefined : t.writable) !== false)); + var require_utils = __commonJS2((exports2, module2) => { + var { SymbolAsyncIterator, SymbolIterator, SymbolFor: SymbolFor2 } = require_primordials(), kIsDestroyed = SymbolFor2("nodejs.stream.destroyed"), kIsErrored = SymbolFor2("nodejs.stream.errored"), kIsReadable = SymbolFor2("nodejs.stream.readable"), kIsWritable = SymbolFor2("nodejs.stream.writable"), kIsDisturbed = SymbolFor2("nodejs.stream.disturbed"), kIsClosedPromise = SymbolFor2("nodejs.webstream.isClosedPromise"), kControllerErrorFunction = SymbolFor2("nodejs.webstream.controllerErrorFunction"); + function isReadableNodeStream(obj, strict = false) { + var _obj$_readableState; + return !!(obj && typeof obj.pipe === "function" && typeof obj.on === "function" && (!strict || typeof obj.pause === "function" && typeof obj.resume === "function") && (!obj._writableState || ((_obj$_readableState = obj._readableState) === null || _obj$_readableState === undefined ? undefined : _obj$_readableState.readable) !== false) && (!obj._writableState || obj._readableState)); } - function da(e) { - return !!(e && typeof e.pipe == "function" && e._readableState && typeof e.on == "function" && typeof e.write == "function"); + function isWritableNodeStream(obj) { + var _obj$_writableState; + return !!(obj && typeof obj.write === "function" && typeof obj.on === "function" && (!obj._readableState || ((_obj$_writableState = obj._writableState) === null || _obj$_writableState === undefined ? undefined : _obj$_writableState.writable) !== false)); } - function Q(e) { - return e && (e._readableState || e._writableState || typeof e.write == "function" && typeof e.on == "function" || typeof e.pipe == "function" && typeof e.on == "function"); + function isDuplexNodeStream(obj) { + return !!(obj && typeof obj.pipe === "function" && obj._readableState && typeof obj.on === "function" && typeof obj.write === "function"); } - function qi(e) { - return !!(e && !Q(e) && typeof e.pipeThrough == "function" && typeof e.getReader == "function" && typeof e.cancel == "function"); + function isNodeStream(obj) { + return obj && (obj._readableState || obj._writableState || typeof obj.write === "function" && typeof obj.on === "function" || typeof obj.pipe === "function" && typeof obj.on === "function"); } - function vi(e) { - return !!(e && !Q(e) && typeof e.getWriter == "function" && typeof e.abort == "function"); + function isReadableStream(obj) { + return !!(obj && !isNodeStream(obj) && typeof obj.pipeThrough === "function" && typeof obj.getReader === "function" && typeof obj.cancel === "function"); } - function Ui(e) { - return !!(e && !Q(e) && typeof e.readable == "object" && typeof e.writable == "object"); + function isWritableStream(obj) { + return !!(obj && !isNodeStream(obj) && typeof obj.getWriter === "function" && typeof obj.abort === "function"); } - function ha(e) { - return qi(e) || vi(e) || Ui(e); + function isTransformStream(obj) { + return !!(obj && !isNodeStream(obj) && typeof obj.readable === "object" && typeof obj.writable === "object"); } - function pa(e, t) { - return e == null ? false : t === true ? typeof e[Ci] == "function" : t === false ? typeof e[ki] == "function" : typeof e[Ci] == "function" || typeof e[ki] == "function"; + function isWebStream(obj) { + return isReadableStream(obj) || isWritableStream(obj) || isTransformStream(obj); } - function Mt(e) { - if (!Q(e)) + function isIterable(obj, isAsync) { + if (obj == null) + return false; + if (isAsync === true) + return typeof obj[SymbolAsyncIterator] === "function"; + if (isAsync === false) + return typeof obj[SymbolIterator] === "function"; + return typeof obj[SymbolAsyncIterator] === "function" || typeof obj[SymbolIterator] === "function"; + } + function isDestroyed(stream) { + if (!isNodeStream(stream)) return null; - let { _writableState: t, _readableState: r } = e, n = t || r; - return !!(e.destroyed || e[Di] || n != null && n.destroyed); + let { _writableState: wState, _readableState: rState } = stream, state = wState || rState; + return !!(stream.destroyed || stream[kIsDestroyed] || state !== null && state !== undefined && state.destroyed); } - function Wi(e) { - if (!Ft(e)) + function isWritableEnded(stream) { + if (!isWritableNodeStream(stream)) return null; - if (e.writableEnded === true) + if (stream.writableEnded === true) return true; - let t = e._writableState; - return t != null && t.errored ? false : typeof t?.ended != "boolean" ? null : t.ended; + let wState = stream._writableState; + if (wState !== null && wState !== undefined && wState.errored) + return false; + if (typeof (wState === null || wState === undefined ? undefined : wState.ended) !== "boolean") + return null; + return wState.ended; } - function ya(e, t) { - if (!Ft(e)) + function isWritableFinished(stream, strict) { + if (!isWritableNodeStream(stream)) return null; - if (e.writableFinished === true) + if (stream.writableFinished === true) return true; - let r = e._writableState; - return r != null && r.errored ? false : typeof r?.finished != "boolean" ? null : !!(r.finished || t === false && r.ended === true && r.length === 0); + let wState = stream._writableState; + if (wState !== null && wState !== undefined && wState.errored) + return false; + if (typeof (wState === null || wState === undefined ? undefined : wState.finished) !== "boolean") + return null; + return !!(wState.finished || strict === false && wState.ended === true && wState.length === 0); } - function ba(e) { - if (!Nt(e)) + function isReadableEnded(stream) { + if (!isReadableNodeStream(stream)) return null; - if (e.readableEnded === true) + if (stream.readableEnded === true) return true; - let t = e._readableState; - return !t || t.errored ? false : typeof t?.ended != "boolean" ? null : t.ended; + let rState = stream._readableState; + if (!rState || rState.errored) + return false; + if (typeof (rState === null || rState === undefined ? undefined : rState.ended) !== "boolean") + return null; + return rState.ended; } - function $i(e, t) { - if (!Nt(e)) + function isReadableFinished(stream, strict) { + if (!isReadableNodeStream(stream)) + return null; + let rState = stream._readableState; + if (rState !== null && rState !== undefined && rState.errored) + return false; + if (typeof (rState === null || rState === undefined ? undefined : rState.endEmitted) !== "boolean") return null; - let r = e._readableState; - return r != null && r.errored ? false : typeof r?.endEmitted != "boolean" ? null : !!(r.endEmitted || t === false && r.ended === true && r.length === 0); + return !!(rState.endEmitted || strict === false && rState.ended === true && rState.length === 0); } - function ji(e) { - return e && e[Lr] != null ? e[Lr] : typeof e?.readable != "boolean" ? null : Mt(e) ? false : Nt(e) && e.readable && !$i(e); + function isReadable(stream) { + if (stream && stream[kIsReadable] != null) + return stream[kIsReadable]; + if (typeof (stream === null || stream === undefined ? undefined : stream.readable) !== "boolean") + return null; + if (isDestroyed(stream)) + return false; + return isReadableNodeStream(stream) && stream.readable && !isReadableFinished(stream); } - function Hi(e) { - return e && e[Nr] != null ? e[Nr] : typeof e?.writable != "boolean" ? null : Mt(e) ? false : Ft(e) && e.writable && !Wi(e); + function isWritable(stream) { + if (stream && stream[kIsWritable] != null) + return stream[kIsWritable]; + if (typeof (stream === null || stream === undefined ? undefined : stream.writable) !== "boolean") + return null; + if (isDestroyed(stream)) + return false; + return isWritableNodeStream(stream) && stream.writable && !isWritableEnded(stream); } - function wa(e, t) { - return Q(e) ? Mt(e) ? true : !(t?.readable !== false && ji(e) || t?.writable !== false && Hi(e)) : null; + function isFinished(stream, opts) { + if (!isNodeStream(stream)) + return null; + if (isDestroyed(stream)) + return true; + if ((opts === null || opts === undefined ? undefined : opts.readable) !== false && isReadable(stream)) + return false; + if ((opts === null || opts === undefined ? undefined : opts.writable) !== false && isWritable(stream)) + return false; + return true; } - function ga(e) { - var t, r; - return Q(e) ? e.writableErrored ? e.writableErrored : (t = (r = e._writableState) === null || r === undefined ? undefined : r.errored) !== null && t !== undefined ? t : null : null; + function isWritableErrored(stream) { + var _stream$_writableStat, _stream$_writableStat2; + if (!isNodeStream(stream)) + return null; + if (stream.writableErrored) + return stream.writableErrored; + return (_stream$_writableStat = (_stream$_writableStat2 = stream._writableState) === null || _stream$_writableStat2 === undefined ? undefined : _stream$_writableStat2.errored) !== null && _stream$_writableStat !== undefined ? _stream$_writableStat : null; } - function _a(e) { - var t, r; - return Q(e) ? e.readableErrored ? e.readableErrored : (t = (r = e._readableState) === null || r === undefined ? undefined : r.errored) !== null && t !== undefined ? t : null : null; + function isReadableErrored(stream) { + var _stream$_readableStat, _stream$_readableStat2; + if (!isNodeStream(stream)) + return null; + if (stream.readableErrored) + return stream.readableErrored; + return (_stream$_readableStat = (_stream$_readableStat2 = stream._readableState) === null || _stream$_readableStat2 === undefined ? undefined : _stream$_readableStat2.errored) !== null && _stream$_readableStat !== undefined ? _stream$_readableStat : null; } - function Ea(e) { - if (!Q(e)) + function isClosed(stream) { + if (!isNodeStream(stream)) return null; - if (typeof e.closed == "boolean") - return e.closed; - let { _writableState: t, _readableState: r } = e; - return typeof t?.closed == "boolean" || typeof r?.closed == "boolean" ? t?.closed || r?.closed : typeof e._closed == "boolean" && Gi(e) ? e._closed : null; + if (typeof stream.closed === "boolean") + return stream.closed; + let { _writableState: wState, _readableState: rState } = stream; + if (typeof (wState === null || wState === undefined ? undefined : wState.closed) === "boolean" || typeof (rState === null || rState === undefined ? undefined : rState.closed) === "boolean") + return (wState === null || wState === undefined ? undefined : wState.closed) || (rState === null || rState === undefined ? undefined : rState.closed); + if (typeof stream._closed === "boolean" && isOutgoingMessage(stream)) + return stream._closed; + return null; } - function Gi(e) { - return typeof e._closed == "boolean" && typeof e._defaultKeepAlive == "boolean" && typeof e._removedConnection == "boolean" && typeof e._removedContLen == "boolean"; + function isOutgoingMessage(stream) { + return typeof stream._closed === "boolean" && typeof stream._defaultKeepAlive === "boolean" && typeof stream._removedConnection === "boolean" && typeof stream._removedContLen === "boolean"; } - function Vi(e) { - return typeof e._sent100 == "boolean" && Gi(e); + function isServerResponse(stream) { + return typeof stream._sent100 === "boolean" && isOutgoingMessage(stream); } - function Sa(e) { - var t; - return typeof e._consuming == "boolean" && typeof e._dumped == "boolean" && ((t = e.req) === null || t === undefined ? undefined : t.upgradeOrConnect) === undefined; + function isServerRequest(stream) { + var _stream$req; + return typeof stream._consuming === "boolean" && typeof stream._dumped === "boolean" && ((_stream$req = stream.req) === null || _stream$req === undefined ? undefined : _stream$req.upgradeOrConnect) === undefined; } - function ma(e) { - if (!Q(e)) + function willEmitClose(stream) { + if (!isNodeStream(stream)) return null; - let { _writableState: t, _readableState: r } = e, n = t || r; - return !n && Vi(e) || !!(n && n.autoDestroy && n.emitClose && n.closed === false); + let { _writableState: wState, _readableState: rState } = stream, state = wState || rState; + return !state && isServerResponse(stream) || !!(state && state.autoDestroy && state.emitClose && state.closed === false); } - function Ra(e) { - var t; - return !!(e && ((t = e[Pi]) !== null && t !== undefined ? t : e.readableDidRead || e.readableAborted)); + function isDisturbed(stream) { + var _stream$kIsDisturbed; + return !!(stream && ((_stream$kIsDisturbed = stream[kIsDisturbed]) !== null && _stream$kIsDisturbed !== undefined ? _stream$kIsDisturbed : stream.readableDidRead || stream.readableAborted)); } - function Aa(e) { - var t, r, n, i, o, l, u, a, s, c; - return !!(e && ((t = (r = (n = (i = (o = (l = e[Oi]) !== null && l !== undefined ? l : e.readableErrored) !== null && o !== undefined ? o : e.writableErrored) !== null && i !== undefined ? i : (u = e._readableState) === null || u === undefined ? undefined : u.errorEmitted) !== null && n !== undefined ? n : (a = e._writableState) === null || a === undefined ? undefined : a.errorEmitted) !== null && r !== undefined ? r : (s = e._readableState) === null || s === undefined ? undefined : s.errored) !== null && t !== undefined ? t : (c = e._writableState) === null || c === undefined ? undefined : c.errored)); + function isErrored(stream) { + var _ref, _ref2, _ref3, _ref4, _ref5, _stream$kIsErrored, _stream$_readableStat3, _stream$_writableStat3, _stream$_readableStat4, _stream$_writableStat4; + return !!(stream && ((_ref = (_ref2 = (_ref3 = (_ref4 = (_ref5 = (_stream$kIsErrored = stream[kIsErrored]) !== null && _stream$kIsErrored !== undefined ? _stream$kIsErrored : stream.readableErrored) !== null && _ref5 !== undefined ? _ref5 : stream.writableErrored) !== null && _ref4 !== undefined ? _ref4 : (_stream$_readableStat3 = stream._readableState) === null || _stream$_readableStat3 === undefined ? undefined : _stream$_readableStat3.errorEmitted) !== null && _ref3 !== undefined ? _ref3 : (_stream$_writableStat3 = stream._writableState) === null || _stream$_writableStat3 === undefined ? undefined : _stream$_writableStat3.errorEmitted) !== null && _ref2 !== undefined ? _ref2 : (_stream$_readableStat4 = stream._readableState) === null || _stream$_readableStat4 === undefined ? undefined : _stream$_readableStat4.errored) !== null && _ref !== undefined ? _ref : (_stream$_writableStat4 = stream._writableState) === null || _stream$_writableStat4 === undefined ? undefined : _stream$_writableStat4.errored)); } - Yi.exports = { isDestroyed: Mt, kIsDestroyed: Di, isDisturbed: Ra, kIsDisturbed: Pi, isErrored: Aa, kIsErrored: Oi, isReadable: ji, kIsReadable: Lr, kIsClosedPromise: fa, kControllerErrorFunction: ca, kIsWritable: Nr, isClosed: Ea, isDuplexNodeStream: da, isFinished: wa, isIterable: pa, isReadableNodeStream: Nt, isReadableStream: qi, isReadableEnded: ba, isReadableFinished: $i, isReadableErrored: _a, isNodeStream: Q, isWebStream: ha, isWritable: Hi, isWritableNodeStream: Ft, isWritableStream: vi, isWritableEnded: Wi, isWritableFinished: ya, isWritableErrored: ga, isServerRequest: Sa, isServerResponse: Vi, willEmitClose: ma, isTransformStream: Ui }; + module2.exports = { isDestroyed, kIsDestroyed, isDisturbed, kIsDisturbed, isErrored, kIsErrored, isReadable, kIsReadable, kIsClosedPromise, kControllerErrorFunction, kIsWritable, isClosed, isDuplexNodeStream, isFinished, isIterable, isReadableNodeStream, isReadableStream, isReadableEnded, isReadableFinished, isReadableErrored, isNodeStream, isWebStream, isWritable, isWritableNodeStream, isWritableStream, isWritableEnded, isWritableFinished, isWritableErrored, isServerRequest, isServerResponse, willEmitClose, isTransformStream }; }); - ae = S(($h, Dr) => { - var ge = (we(), ye(H)), { AbortError: ro, codes: xa } = O(), { ERR_INVALID_ARG_TYPE: Ia, ERR_STREAM_PREMATURE_CLOSE: Ki } = xa, { kEmptyObject: Mr, once: Cr } = q(), { validateAbortSignal: Ta, validateFunction: Ba, validateObject: La, validateBoolean: Na } = He(), { Promise: Fa, PromisePrototypeThen: Ma, SymbolDispose: no } = I(), { isClosed: Ca, isReadable: zi, isReadableNodeStream: Fr, isReadableStream: ka, isReadableFinished: Xi, isReadableErrored: Ji, isWritable: Qi, isWritableNodeStream: Zi, isWritableStream: Da, isWritableFinished: eo, isWritableErrored: to, isNodeStream: Oa, willEmitClose: Pa, kIsClosedPromise: qa } = Z(), Ve; - function va(e) { - return e.setHeader && typeof e.abort == "function"; - } - var kr = () => { - }; - function io(e, t, r) { - var n, i; - if (arguments.length === 2 ? (r = t, t = Mr) : t == null ? t = Mr : La(t, "options"), Ba(r, "callback"), Ta(t.signal, "options.signal"), r = Cr(r), ka(e) || Da(e)) - return Ua(e, t, r); - if (!Oa(e)) - throw new Ia("stream", ["ReadableStream", "WritableStream", "Stream"], e); - let o = (n = t.readable) !== null && n !== undefined ? n : Fr(e), l = (i = t.writable) !== null && i !== undefined ? i : Zi(e), u = e._writableState, a = e._readableState, s = () => { - e.writable || y(); - }, c = Pa(e) && Fr(e) === o && Zi(e) === l, d = eo(e, false), y = () => { - d = true, e.destroyed && (c = false), !(c && (!e.readable || o)) && (!o || h) && r.call(e); - }, h = Xi(e, false), _ = () => { - h = true, e.destroyed && (c = false), !(c && (!e.writable || l)) && (!l || d) && r.call(e); - }, p = (N) => { - r.call(e, N); - }, R = Ca(e), w = () => { - R = true; - let N = to(e) || Ji(e); - if (N && typeof N != "boolean") - return r.call(e, N); - if (o && !h && Fr(e, true) && !Xi(e, false)) - return r.call(e, new Ki); - if (l && !d && !eo(e, false)) - return r.call(e, new Ki); - r.call(e); - }, x = () => { - R = true; - let N = to(e) || Ji(e); - if (N && typeof N != "boolean") - return r.call(e, N); - r.call(e); - }, F = () => { - e.req.on("finish", y); + var require_end_of_stream = __commonJS2((exports2, module2) => { + var process2 = require_process(), { AbortError: AbortError2, codes } = require_errors(), { ERR_INVALID_ARG_TYPE: ERR_INVALID_ARG_TYPE3, ERR_STREAM_PREMATURE_CLOSE } = codes, { kEmptyObject, once: once3 } = require_util(), { validateAbortSignal: validateAbortSignal2, validateFunction, validateObject, validateBoolean: validateBoolean2 } = require_validators(), { Promise: Promise2, PromisePrototypeThen, SymbolDispose } = require_primordials(), { isClosed, isReadable, isReadableNodeStream, isReadableStream, isReadableFinished, isReadableErrored, isWritable, isWritableNodeStream, isWritableStream, isWritableFinished, isWritableErrored, isNodeStream, willEmitClose: _willEmitClose, kIsClosedPromise } = require_utils(), addAbortListener2; + function isRequest(stream) { + return stream.setHeader && typeof stream.abort === "function"; + } + var nop = () => {}; + function eos(stream, options, callback) { + var _options$readable, _options$writable; + if (arguments.length === 2) + callback = options, options = kEmptyObject; + else if (options == null) + options = kEmptyObject; + else + validateObject(options, "options"); + if (validateFunction(callback, "callback"), validateAbortSignal2(options.signal, "options.signal"), callback = once3(callback), isReadableStream(stream) || isWritableStream(stream)) + return eosWeb(stream, options, callback); + if (!isNodeStream(stream)) + throw new ERR_INVALID_ARG_TYPE3("stream", ["ReadableStream", "WritableStream", "Stream"], stream); + let readable = (_options$readable = options.readable) !== null && _options$readable !== undefined ? _options$readable : isReadableNodeStream(stream), writable = (_options$writable = options.writable) !== null && _options$writable !== undefined ? _options$writable : isWritableNodeStream(stream), wState = stream._writableState, rState = stream._readableState, onlegacyfinish = () => { + if (!stream.writable) + onfinish(); + }, willEmitClose = _willEmitClose(stream) && isReadableNodeStream(stream) === readable && isWritableNodeStream(stream) === writable, writableFinished = isWritableFinished(stream, false), onfinish = () => { + if (writableFinished = true, stream.destroyed) + willEmitClose = false; + if (willEmitClose && (!stream.readable || readable)) + return; + if (!readable || readableFinished) + callback.call(stream); + }, readableFinished = isReadableFinished(stream, false), onend = () => { + if (readableFinished = true, stream.destroyed) + willEmitClose = false; + if (willEmitClose && (!stream.writable || writable)) + return; + if (!writable || writableFinished) + callback.call(stream); + }, onerror = (err) => { + callback.call(stream, err); + }, closed = isClosed(stream), onclose = () => { + closed = true; + let errored = isWritableErrored(stream) || isReadableErrored(stream); + if (errored && typeof errored !== "boolean") + return callback.call(stream, errored); + if (readable && !readableFinished && isReadableNodeStream(stream, true)) { + if (!isReadableFinished(stream, false)) + return callback.call(stream, new ERR_STREAM_PREMATURE_CLOSE); + } + if (writable && !writableFinished) { + if (!isWritableFinished(stream, false)) + return callback.call(stream, new ERR_STREAM_PREMATURE_CLOSE); + } + callback.call(stream); + }, onclosed = () => { + closed = true; + let errored = isWritableErrored(stream) || isReadableErrored(stream); + if (errored && typeof errored !== "boolean") + return callback.call(stream, errored); + callback.call(stream); + }, onrequest = () => { + stream.req.on("finish", onfinish); }; - va(e) ? (e.on("complete", y), c || e.on("abort", w), e.req ? F() : e.on("request", F)) : l && !u && (e.on("end", s), e.on("close", s)), !c && typeof e.aborted == "boolean" && e.on("aborted", w), e.on("end", _), e.on("finish", y), t.error !== false && e.on("error", p), e.on("close", w), R ? ge.nextTick(w) : u != null && u.errorEmitted || a != null && a.errorEmitted ? c || ge.nextTick(x) : (!o && (!c || zi(e)) && (d || Qi(e) === false) || !l && (!c || Qi(e)) && (h || zi(e) === false) || a && e.req && e.aborted) && ge.nextTick(x); - let E = () => { - r = kr, e.removeListener("aborted", w), e.removeListener("complete", y), e.removeListener("abort", w), e.removeListener("request", F), e.req && e.req.removeListener("finish", y), e.removeListener("end", s), e.removeListener("close", s), e.removeListener("finish", y), e.removeListener("end", _), e.removeListener("error", p), e.removeListener("close", w); + if (isRequest(stream)) { + if (stream.on("complete", onfinish), !willEmitClose) + stream.on("abort", onclose); + if (stream.req) + onrequest(); + else + stream.on("request", onrequest); + } else if (writable && !wState) + stream.on("end", onlegacyfinish), stream.on("close", onlegacyfinish); + if (!willEmitClose && typeof stream.aborted === "boolean") + stream.on("aborted", onclose); + if (stream.on("end", onend), stream.on("finish", onfinish), options.error !== false) + stream.on("error", onerror); + if (stream.on("close", onclose), closed) + process2.nextTick(onclose); + else if (wState !== null && wState !== undefined && wState.errorEmitted || rState !== null && rState !== undefined && rState.errorEmitted) { + if (!willEmitClose) + process2.nextTick(onclosed); + } else if (!readable && (!willEmitClose || isReadable(stream)) && (writableFinished || isWritable(stream) === false)) + process2.nextTick(onclosed); + else if (!writable && (!willEmitClose || isWritable(stream)) && (readableFinished || isReadable(stream) === false)) + process2.nextTick(onclosed); + else if (rState && stream.req && stream.aborted) + process2.nextTick(onclosed); + let cleanup = () => { + if (callback = nop, stream.removeListener("aborted", onclose), stream.removeListener("complete", onfinish), stream.removeListener("abort", onclose), stream.removeListener("request", onrequest), stream.req) + stream.req.removeListener("finish", onfinish); + stream.removeListener("end", onlegacyfinish), stream.removeListener("close", onlegacyfinish), stream.removeListener("finish", onfinish), stream.removeListener("end", onend), stream.removeListener("error", onerror), stream.removeListener("close", onclose); }; - if (t.signal && !R) { - let N = () => { - let Se = r; - E(), Se.call(e, new ro(undefined, { cause: t.signal.reason })); + if (options.signal && !closed) { + let abort = () => { + let endCallback = callback; + cleanup(), endCallback.call(stream, new AbortError2(undefined, { cause: options.signal.reason })); }; - if (t.signal.aborted) - ge.nextTick(N); + if (options.signal.aborted) + process2.nextTick(abort); else { - Ve = Ve || q().addAbortListener; - let Se = Ve(t.signal, N), W = r; - r = Cr((...Oe) => { - Se[no](), W.apply(e, Oe); + addAbortListener2 = addAbortListener2 || require_util().addAbortListener; + let disposable = addAbortListener2(options.signal, abort), originalCallback = callback; + callback = once3((...args) => { + disposable[SymbolDispose](), originalCallback.apply(stream, args); }); } } - return E; + return cleanup; } - function Ua(e, t, r) { - let n = false, i = kr; - if (t.signal) - if (i = () => { - n = true, r.call(e, new ro(undefined, { cause: t.signal.reason })); - }, t.signal.aborted) - ge.nextTick(i); + function eosWeb(stream, options, callback) { + let isAborted = false, abort = nop; + if (options.signal) + if (abort = () => { + isAborted = true, callback.call(stream, new AbortError2(undefined, { cause: options.signal.reason })); + }, options.signal.aborted) + process2.nextTick(abort); else { - Ve = Ve || q().addAbortListener; - let l = Ve(t.signal, i), u = r; - r = Cr((...a) => { - l[no](), u.apply(e, a); + addAbortListener2 = addAbortListener2 || require_util().addAbortListener; + let disposable = addAbortListener2(options.signal, abort), originalCallback = callback; + callback = once3((...args) => { + disposable[SymbolDispose](), originalCallback.apply(stream, args); }); } - let o = (...l) => { - n || ge.nextTick(() => r.apply(e, l)); + let resolverFn = (...args) => { + if (!isAborted) + process2.nextTick(() => callback.apply(stream, args)); }; - return Ma(e[qa].promise, o, o), kr; - } - function Wa(e, t) { - var r; - let n = false; - return t === null && (t = Mr), (r = t) !== null && r !== undefined && r.cleanup && (Na(t.cleanup, "cleanup"), n = t.cleanup), new Fa((i, o) => { - let l = io(e, t, (u) => { - n && l(), u ? o(u) : i(); + return PromisePrototypeThen(stream[kIsClosedPromise].promise, resolverFn, resolverFn), nop; + } + function finished(stream, opts) { + var _opts; + let autoCleanup = false; + if (opts === null) + opts = kEmptyObject; + if ((_opts = opts) !== null && _opts !== undefined && _opts.cleanup) + validateBoolean2(opts.cleanup, "cleanup"), autoCleanup = opts.cleanup; + return new Promise2((resolve, reject) => { + let cleanup = eos(stream, opts, (err) => { + if (autoCleanup) + cleanup(); + if (err) + reject(err); + else + resolve(); }); }); } - Dr.exports = io; - Dr.exports.finished = Wa; + module2.exports = eos; + module2.exports.finished = finished; }); - Be = S((jh, ho) => { - var ee = (we(), ye(H)), { aggregateTwoErrors: $a, codes: { ERR_MULTIPLE_CALLBACK: ja }, AbortError: Ha } = O(), { Symbol: uo } = I(), { kIsDestroyed: Ga, isDestroyed: Va, isFinished: Ya, isServerRequest: Ka } = Z(), so = uo("kDestroy"), Or = uo("kConstruct"); - function ao(e, t, r) { - e && (e.stack, t && !t.errored && (t.errored = e), r && !r.errored && (r.errored = e)); - } - function za(e, t) { - let r = this._readableState, n = this._writableState, i = n || r; - return n != null && n.destroyed || r != null && r.destroyed ? (typeof t == "function" && t(), this) : (ao(e, n, r), n && (n.destroyed = true), r && (r.destroyed = true), i.constructed ? oo(this, e, t) : this.once(so, function(o) { - oo(this, $a(o, e), t); - }), this); - } - function oo(e, t, r) { - let n = false; - function i(o) { - if (n) + var require_destroy = __commonJS2((exports2, module2) => { + var process2 = require_process(), { aggregateTwoErrors, codes: { ERR_MULTIPLE_CALLBACK }, AbortError: AbortError2 } = require_errors(), { Symbol: Symbol2 } = require_primordials(), { kIsDestroyed, isDestroyed, isFinished, isServerRequest } = require_utils(), kDestroy = Symbol2("kDestroy"), kConstruct = Symbol2("kConstruct"); + function checkError(err, w, r) { + if (err) { + if (err.stack, w && !w.errored) + w.errored = err; + if (r && !r.errored) + r.errored = err; + } + } + function destroy(err, cb) { + let r = this._readableState, w = this._writableState, s = w || r; + if (w !== null && w !== undefined && w.destroyed || r !== null && r !== undefined && r.destroyed) { + if (typeof cb === "function") + cb(); + return this; + } + if (checkError(err, w, r), w) + w.destroyed = true; + if (r) + r.destroyed = true; + if (!s.constructed) + this.once(kDestroy, function(er) { + _destroy(this, aggregateTwoErrors(er, err), cb); + }); + else + _destroy(this, err, cb); + return this; + } + function _destroy(self2, err, cb) { + let called = false; + function onDestroy(err2) { + if (called) return; - n = true; - let { _readableState: l, _writableState: u } = e; - ao(o, u, l), u && (u.closed = true), l && (l.closed = true), typeof r == "function" && r(o), o ? ee.nextTick(Xa, e, o) : ee.nextTick(fo, e); + called = true; + let { _readableState: r, _writableState: w } = self2; + if (checkError(err2, w, r), w) + w.closed = true; + if (r) + r.closed = true; + if (typeof cb === "function") + cb(err2); + if (err2) + process2.nextTick(emitErrorCloseNT, self2, err2); + else + process2.nextTick(emitCloseNT, self2); } try { - e._destroy(t || null, i); - } catch (o) { - i(o); - } - } - function Xa(e, t) { - Pr(e, t), fo(e); - } - function fo(e) { - let { _readableState: t, _writableState: r } = e; - r && (r.closeEmitted = true), t && (t.closeEmitted = true), (r != null && r.emitClose || t != null && t.emitClose) && e.emit("close"); - } - function Pr(e, t) { - let { _readableState: r, _writableState: n } = e; - n != null && n.errorEmitted || r != null && r.errorEmitted || (n && (n.errorEmitted = true), r && (r.errorEmitted = true), e.emit("error", t)); - } - function Ja() { - let e = this._readableState, t = this._writableState; - e && (e.constructed = true, e.closed = false, e.closeEmitted = false, e.destroyed = false, e.errored = null, e.errorEmitted = false, e.reading = false, e.ended = e.readable === false, e.endEmitted = e.readable === false), t && (t.constructed = true, t.destroyed = false, t.closed = false, t.closeEmitted = false, t.errored = null, t.errorEmitted = false, t.finalCalled = false, t.prefinished = false, t.ended = t.writable === false, t.ending = t.writable === false, t.finished = t.writable === false); - } - function qr(e, t, r) { - let { _readableState: n, _writableState: i } = e; - if (i != null && i.destroyed || n != null && n.destroyed) + self2._destroy(err || null, onDestroy); + } catch (err2) { + onDestroy(err2); + } + } + function emitErrorCloseNT(self2, err) { + emitErrorNT(self2, err), emitCloseNT(self2); + } + function emitCloseNT(self2) { + let { _readableState: r, _writableState: w } = self2; + if (w) + w.closeEmitted = true; + if (r) + r.closeEmitted = true; + if (w !== null && w !== undefined && w.emitClose || r !== null && r !== undefined && r.emitClose) + self2.emit("close"); + } + function emitErrorNT(self2, err) { + let { _readableState: r, _writableState: w } = self2; + if (w !== null && w !== undefined && w.errorEmitted || r !== null && r !== undefined && r.errorEmitted) + return; + if (w) + w.errorEmitted = true; + if (r) + r.errorEmitted = true; + self2.emit("error", err); + } + function undestroy() { + let r = this._readableState, w = this._writableState; + if (r) + r.constructed = true, r.closed = false, r.closeEmitted = false, r.destroyed = false, r.errored = null, r.errorEmitted = false, r.reading = false, r.ended = r.readable === false, r.endEmitted = r.readable === false; + if (w) + w.constructed = true, w.destroyed = false, w.closed = false, w.closeEmitted = false, w.errored = null, w.errorEmitted = false, w.finalCalled = false, w.prefinished = false, w.ended = w.writable === false, w.ending = w.writable === false, w.finished = w.writable === false; + } + function errorOrDestroy(stream, err, sync) { + let { _readableState: r, _writableState: w } = stream; + if (w !== null && w !== undefined && w.destroyed || r !== null && r !== undefined && r.destroyed) return this; - n != null && n.autoDestroy || i != null && i.autoDestroy ? e.destroy(t) : t && (t.stack, i && !i.errored && (i.errored = t), n && !n.errored && (n.errored = t), r ? ee.nextTick(Pr, e, t) : Pr(e, t)); + if (r !== null && r !== undefined && r.autoDestroy || w !== null && w !== undefined && w.autoDestroy) + stream.destroy(err); + else if (err) { + if (err.stack, w && !w.errored) + w.errored = err; + if (r && !r.errored) + r.errored = err; + if (sync) + process2.nextTick(emitErrorNT, stream, err); + else + emitErrorNT(stream, err); + } } - function Qa(e, t) { - if (typeof e._construct != "function") + function construct(stream, cb) { + if (typeof stream._construct !== "function") return; - let { _readableState: r, _writableState: n } = e; - r && (r.constructed = false), n && (n.constructed = false), e.once(Or, t), !(e.listenerCount(Or) > 1) && ee.nextTick(Za, e); - } - function Za(e) { - let t = false; - function r(n) { - if (t) { - qr(e, n ?? new ja); + let { _readableState: r, _writableState: w } = stream; + if (r) + r.constructed = false; + if (w) + w.constructed = false; + if (stream.once(kConstruct, cb), stream.listenerCount(kConstruct) > 1) + return; + process2.nextTick(constructNT, stream); + } + function constructNT(stream) { + let called = false; + function onConstruct(err) { + if (called) { + errorOrDestroy(stream, err !== null && err !== undefined ? err : new ERR_MULTIPLE_CALLBACK); return; } - t = true; - let { _readableState: i, _writableState: o } = e, l = o || i; - i && (i.constructed = true), o && (o.constructed = true), l.destroyed ? e.emit(so, n) : n ? qr(e, n, true) : ee.nextTick(ef, e); + called = true; + let { _readableState: r, _writableState: w } = stream, s = w || r; + if (r) + r.constructed = true; + if (w) + w.constructed = true; + if (s.destroyed) + stream.emit(kDestroy, err); + else if (err) + errorOrDestroy(stream, err, true); + else + process2.nextTick(emitConstructNT, stream); } try { - e._construct((n) => { - ee.nextTick(r, n); + stream._construct((err) => { + process2.nextTick(onConstruct, err); }); - } catch (n) { - ee.nextTick(r, n); + } catch (err) { + process2.nextTick(onConstruct, err); } } - function ef(e) { - e.emit(Or); + function emitConstructNT(stream) { + stream.emit(kConstruct); } - function lo(e) { - return e?.setHeader && typeof e.abort == "function"; + function isRequest(stream) { + return (stream === null || stream === undefined ? undefined : stream.setHeader) && typeof stream.abort === "function"; } - function co(e) { - e.emit("close"); + function emitCloseLegacy(stream) { + stream.emit("close"); } - function tf(e, t) { - e.emit("error", t), ee.nextTick(co, e); + function emitErrorCloseLegacy(stream, err) { + stream.emit("error", err), process2.nextTick(emitCloseLegacy, stream); } - function rf(e, t) { - !e || Va(e) || (!t && !Ya(e) && (t = new Ha), Ka(e) ? (e.socket = null, e.destroy(t)) : lo(e) ? e.abort() : lo(e.req) ? e.req.abort() : typeof e.destroy == "function" ? e.destroy(t) : typeof e.close == "function" ? e.close() : t ? ee.nextTick(tf, e, t) : ee.nextTick(co, e), e.destroyed || (e[Ga] = true)); + function destroyer(stream, err) { + if (!stream || isDestroyed(stream)) + return; + if (!err && !isFinished(stream)) + err = new AbortError2; + if (isServerRequest(stream)) + stream.socket = null, stream.destroy(err); + else if (isRequest(stream)) + stream.abort(); + else if (isRequest(stream.req)) + stream.req.abort(); + else if (typeof stream.destroy === "function") + stream.destroy(err); + else if (typeof stream.close === "function") + stream.close(); + else if (err) + process2.nextTick(emitErrorCloseLegacy, stream, err); + else + process2.nextTick(emitCloseLegacy, stream); + if (!stream.destroyed) + stream[kIsDestroyed] = true; } - ho.exports = { construct: Qa, destroyer: rf, destroy: za, undestroy: Ja, errorOrDestroy: qr }; + module2.exports = { construct, destroyer, destroy, undestroy, errorOrDestroy }; }); - Dt = S((Hh, yo) => { - var { ArrayIsArray: nf, ObjectSetPrototypeOf: po } = I(), { EventEmitter: Ct } = it(); - function kt(e) { - Ct.call(this, e); - } - po(kt.prototype, Ct.prototype); - po(kt, Ct); - kt.prototype.pipe = function(e, t) { - let r = this; - function n(c) { - e.writable && e.write(c) === false && r.pause && r.pause(); - } - r.on("data", n); - function i() { - r.readable && r.resume && r.resume(); - } - e.on("drain", i), !e._isStdio && (!t || t.end !== false) && (r.on("end", l), r.on("close", u)); - let o = false; - function l() { - o || (o = true, e.end()); - } - function u() { - o || (o = true, typeof e.destroy == "function" && e.destroy()); - } - function a(c) { - s(), Ct.listenerCount(this, "error") === 0 && this.emit("error", c); - } - vr(r, "error", a), vr(e, "error", a); - function s() { - r.removeListener("data", n), e.removeListener("drain", i), r.removeListener("end", l), r.removeListener("close", u), r.removeListener("error", a), e.removeListener("error", a), r.removeListener("end", s), r.removeListener("close", s), e.removeListener("close", s); - } - return r.on("end", s), r.on("close", s), e.on("close", s), e.emit("pipe", r), e; - }; - function vr(e, t, r) { - if (typeof e.prependListener == "function") - return e.prependListener(t, r); - !e._events || !e._events[t] ? e.on(t, r) : nf(e._events[t]) ? e._events[t].unshift(r) : e._events[t] = [r, e._events[t]]; + var require_legacy = __commonJS2((exports2, module2) => { + var { ArrayIsArray, ObjectSetPrototypeOf } = require_primordials(), { EventEmitter: EE } = (init_events(), __toCommonJS(exports_events)); + function Stream(opts) { + EE.call(this, opts); + } + ObjectSetPrototypeOf(Stream.prototype, EE.prototype); + ObjectSetPrototypeOf(Stream, EE); + Stream.prototype.pipe = function(dest, options) { + let source = this; + function ondata(chunk) { + if (dest.writable && dest.write(chunk) === false && source.pause) + source.pause(); + } + source.on("data", ondata); + function ondrain() { + if (source.readable && source.resume) + source.resume(); + } + if (dest.on("drain", ondrain), !dest._isStdio && (!options || options.end !== false)) + source.on("end", onend), source.on("close", onclose); + let didOnEnd = false; + function onend() { + if (didOnEnd) + return; + didOnEnd = true, dest.end(); + } + function onclose() { + if (didOnEnd) + return; + if (didOnEnd = true, typeof dest.destroy === "function") + dest.destroy(); + } + function onerror(er) { + if (cleanup(), EE.listenerCount(this, "error") === 0) + this.emit("error", er); + } + prependListener2(source, "error", onerror), prependListener2(dest, "error", onerror); + function cleanup() { + source.removeListener("data", ondata), dest.removeListener("drain", ondrain), source.removeListener("end", onend), source.removeListener("close", onclose), source.removeListener("error", onerror), dest.removeListener("error", onerror), source.removeListener("end", cleanup), source.removeListener("close", cleanup), dest.removeListener("close", cleanup); + } + return source.on("end", cleanup), source.on("close", cleanup), dest.on("close", cleanup), dest.emit("pipe", source), dest; + }; + function prependListener2(emitter, event, fn) { + if (typeof emitter.prependListener === "function") + return emitter.prependListener(event, fn); + if (!emitter._events || !emitter._events[event]) + emitter.on(event, fn); + else if (ArrayIsArray(emitter._events[event])) + emitter._events[event].unshift(fn); + else + emitter._events[event] = [fn, emitter._events[event]]; } - yo.exports = { Stream: kt, prependListener: vr }; + module2.exports = { Stream, prependListener: prependListener2 }; }); - ot = S((Gh, Ot) => { - var { SymbolDispose: of } = I(), { AbortError: bo, codes: lf } = O(), { isNodeStream: wo, isWebStream: uf, kControllerErrorFunction: sf } = Z(), af = ae(), { ERR_INVALID_ARG_TYPE: go } = lf, Ur, ff = (e, t) => { - if (typeof e != "object" || !("aborted" in e)) - throw new go(t, "AbortSignal", e); - }; - Ot.exports.addAbortSignal = function(t, r) { - if (ff(t, "signal"), !wo(r) && !uf(r)) - throw new go("stream", ["ReadableStream", "WritableStream", "Stream"], r); - return Ot.exports.addAbortSignalNoValidate(t, r); - }; - Ot.exports.addAbortSignalNoValidate = function(e, t) { - if (typeof e != "object" || !("aborted" in e)) - return t; - let r = wo(t) ? () => { - t.destroy(new bo(undefined, { cause: e.reason })); + var require_add_abort_signal = __commonJS2((exports2, module2) => { + var { SymbolDispose } = require_primordials(), { AbortError: AbortError2, codes } = require_errors(), { isNodeStream, isWebStream, kControllerErrorFunction } = require_utils(), eos = require_end_of_stream(), { ERR_INVALID_ARG_TYPE: ERR_INVALID_ARG_TYPE3 } = codes, addAbortListener2, validateAbortSignal2 = (signal, name) => { + if (typeof signal !== "object" || !("aborted" in signal)) + throw new ERR_INVALID_ARG_TYPE3(name, "AbortSignal", signal); + }; + module2.exports.addAbortSignal = function addAbortSignal(signal, stream) { + if (validateAbortSignal2(signal, "signal"), !isNodeStream(stream) && !isWebStream(stream)) + throw new ERR_INVALID_ARG_TYPE3("stream", ["ReadableStream", "WritableStream", "Stream"], stream); + return module2.exports.addAbortSignalNoValidate(signal, stream); + }; + module2.exports.addAbortSignalNoValidate = function(signal, stream) { + if (typeof signal !== "object" || !("aborted" in signal)) + return stream; + let onAbort = isNodeStream(stream) ? () => { + stream.destroy(new AbortError2(undefined, { cause: signal.reason })); } : () => { - t[sf](new bo(undefined, { cause: e.reason })); + stream[kControllerErrorFunction](new AbortError2(undefined, { cause: signal.reason })); }; - if (e.aborted) - r(); + if (signal.aborted) + onAbort(); else { - Ur = Ur || q().addAbortListener; - let n = Ur(e, r); - af(t, n[of]); + addAbortListener2 = addAbortListener2 || require_util().addAbortListener; + let disposable = addAbortListener2(signal, onAbort); + eos(stream, disposable[SymbolDispose]); } - return t; + return stream; }; }); - So = S((Yh, Eo) => { - var { StringPrototypeSlice: _o, SymbolIterator: cf, TypedArrayPrototypeSet: Pt, Uint8Array: df } = I(), { Buffer: Wr } = le(), { inspect: hf } = q(); - Eo.exports = class { + var require_buffer_list = __commonJS2((exports2, module2) => { + var { StringPrototypeSlice, SymbolIterator, TypedArrayPrototypeSet, Uint8Array: Uint8Array2 } = require_primordials(), { Buffer: Buffer3 } = (init_buffer(), __toCommonJS(exports_buffer)), { inspect: inspect2 } = require_util(); + module2.exports = class BufferList { constructor() { this.head = null, this.tail = null, this.length = 0; } - push(t) { - let r = { data: t, next: null }; - this.length > 0 ? this.tail.next = r : this.head = r, this.tail = r, ++this.length; + push(v) { + let entry = { data: v, next: null }; + if (this.length > 0) + this.tail.next = entry; + else + this.head = entry; + this.tail = entry, ++this.length; } - unshift(t) { - let r = { data: t, next: this.head }; - this.length === 0 && (this.tail = r), this.head = r, ++this.length; + unshift(v) { + let entry = { data: v, next: this.head }; + if (this.length === 0) + this.tail = entry; + this.head = entry, ++this.length; } shift() { if (this.length === 0) return; - let t = this.head.data; - return this.length === 1 ? this.head = this.tail = null : this.head = this.head.next, --this.length, t; + let ret = this.head.data; + if (this.length === 1) + this.head = this.tail = null; + else + this.head = this.head.next; + return --this.length, ret; } clear() { this.head = this.tail = null, this.length = 0; } - join(t) { + join(s) { if (this.length === 0) return ""; - let r = this.head, n = "" + r.data; - for (;(r = r.next) !== null; ) - n += t + r.data; - return n; + let p = this.head, ret = "" + p.data; + while ((p = p.next) !== null) + ret += s + p.data; + return ret; } - concat(t) { + concat(n) { if (this.length === 0) - return Wr.alloc(0); - let r = Wr.allocUnsafe(t >>> 0), n = this.head, i = 0; - for (;n; ) - Pt(r, n.data, i), i += n.data.length, n = n.next; - return r; - } - consume(t, r) { - let n = this.head.data; - if (t < n.length) { - let i = n.slice(0, t); - return this.head.data = n.slice(t), i; - } - return t === n.length ? this.shift() : r ? this._getString(t) : this._getBuffer(t); + return Buffer3.alloc(0); + let ret = Buffer3.allocUnsafe(n >>> 0), p = this.head, i2 = 0; + while (p) + TypedArrayPrototypeSet(ret, p.data, i2), i2 += p.data.length, p = p.next; + return ret; + } + consume(n, hasStrings) { + let data = this.head.data; + if (n < data.length) { + let slice2 = data.slice(0, n); + return this.head.data = data.slice(n), slice2; + } + if (n === data.length) + return this.shift(); + return hasStrings ? this._getString(n) : this._getBuffer(n); } first() { return this.head.data; } - *[cf]() { - for (let t = this.head;t; t = t.next) - yield t.data; + *[SymbolIterator]() { + for (let p = this.head;p; p = p.next) + yield p.data; } - _getString(t) { - let r = "", n = this.head, i = 0; + _getString(n) { + let ret = "", p = this.head, c = 0; do { - let o = n.data; - if (t > o.length) - r += o, t -= o.length; + let str = p.data; + if (n > str.length) + ret += str, n -= str.length; else { - t === o.length ? (r += o, ++i, n.next ? this.head = n.next : this.head = this.tail = null) : (r += _o(o, 0, t), this.head = n, n.data = _o(o, t)); + if (n === str.length) + if (ret += str, ++c, p.next) + this.head = p.next; + else + this.head = this.tail = null; + else + ret += StringPrototypeSlice(str, 0, n), this.head = p, p.data = StringPrototypeSlice(str, n); break; } - ++i; - } while ((n = n.next) !== null); - return this.length -= i, r; + ++c; + } while ((p = p.next) !== null); + return this.length -= c, ret; } - _getBuffer(t) { - let r = Wr.allocUnsafe(t), n = t, i = this.head, o = 0; + _getBuffer(n) { + let ret = Buffer3.allocUnsafe(n), retLen = n, p = this.head, c = 0; do { - let l = i.data; - if (t > l.length) - Pt(r, l, n - t), t -= l.length; + let buf = p.data; + if (n > buf.length) + TypedArrayPrototypeSet(ret, buf, retLen - n), n -= buf.length; else { - t === l.length ? (Pt(r, l, n - t), ++o, i.next ? this.head = i.next : this.head = this.tail = null) : (Pt(r, new df(l.buffer, l.byteOffset, t), n - t), this.head = i, i.data = l.slice(t)); + if (n === buf.length) + if (TypedArrayPrototypeSet(ret, buf, retLen - n), ++c, p.next) + this.head = p.next; + else + this.head = this.tail = null; + else + TypedArrayPrototypeSet(ret, new Uint8Array2(buf.buffer, buf.byteOffset, n), retLen - n), this.head = p, p.data = buf.slice(n); break; } - ++o; - } while ((i = i.next) !== null); - return this.length -= o, r; + ++c; + } while ((p = p.next) !== null); + return this.length -= c, ret; } - [Symbol.for("nodejs.util.inspect.custom")](t, r) { - return hf(this, { ...r, depth: 0, customInspect: false }); + [Symbol.for("nodejs.util.inspect.custom")](_, options) { + return inspect2(this, { ...options, depth: 0, customInspect: false }); } }; }); - lt = S((Kh, xo) => { - var { MathFloor: pf, NumberIsInteger: yf } = I(), { validateInteger: bf } = He(), { ERR_INVALID_ARG_VALUE: wf } = O().codes, mo = 16 * 1024, Ro = 16; - function gf(e, t, r) { - return e.highWaterMark != null ? e.highWaterMark : t ? e[r] : null; + var require_state = __commonJS2((exports2, module2) => { + var { MathFloor, NumberIsInteger } = require_primordials(), { validateInteger } = require_validators(), { ERR_INVALID_ARG_VALUE } = require_errors().codes, defaultHighWaterMarkBytes = 16384, defaultHighWaterMarkObjectMode = 16; + function highWaterMarkFrom(options, isDuplex, duplexKey) { + return options.highWaterMark != null ? options.highWaterMark : isDuplex ? options[duplexKey] : null; } - function Ao(e) { - return e ? Ro : mo; + function getDefaultHighWaterMark(objectMode) { + return objectMode ? defaultHighWaterMarkObjectMode : defaultHighWaterMarkBytes; } - function _f(e, t) { - bf(t, "value", 0), e ? Ro = t : mo = t; + function setDefaultHighWaterMark(objectMode, value) { + if (validateInteger(value, "value", 0), objectMode) + defaultHighWaterMarkObjectMode = value; + else + defaultHighWaterMarkBytes = value; } - function Ef(e, t, r, n) { - let i = gf(t, n, r); - if (i != null) { - if (!yf(i) || i < 0) { - let o = n ? `options.${r}` : "options.highWaterMark"; - throw new wf(o, i); + function getHighWaterMark(state, options, duplexKey, isDuplex) { + let hwm = highWaterMarkFrom(options, isDuplex, duplexKey); + if (hwm != null) { + if (!NumberIsInteger(hwm) || hwm < 0) { + let name = isDuplex ? `options.${duplexKey}` : "options.highWaterMark"; + throw new ERR_INVALID_ARG_VALUE(name, hwm); } - return pf(i); + return MathFloor(hwm); } - return Ao(e.objectMode); + return getDefaultHighWaterMark(state.objectMode); } - xo.exports = { getHighWaterMark: Ef, getDefaultHighWaterMark: Ao, setDefaultHighWaterMark: _f }; + module2.exports = { getHighWaterMark, getDefaultHighWaterMark, setDefaultHighWaterMark }; }); - Bo = S(($r, To) => { - var qt = le(), te = qt.Buffer; - function Io(e, t) { - for (var r in e) - t[r] = e[r]; - } - te.from && te.alloc && te.allocUnsafe && te.allocUnsafeSlow ? To.exports = qt : (Io(qt, $r), $r.Buffer = Le); - function Le(e, t, r) { - return te(e, t, r); - } - Le.prototype = Object.create(te.prototype); - Io(te, Le); - Le.from = function(e, t, r) { - if (typeof e == "number") + var require_safe_buffer = __commonJS2((exports2, module2) => { + /*! safe-buffer. MIT License. Feross Aboukhadijeh */ + var buffer = (init_buffer(), __toCommonJS(exports_buffer)), Buffer3 = buffer.Buffer; + function copyProps(src, dst) { + for (var key in src) + dst[key] = src[key]; + } + if (Buffer3.from && Buffer3.alloc && Buffer3.allocUnsafe && Buffer3.allocUnsafeSlow) + module2.exports = buffer; + else + copyProps(buffer, exports2), exports2.Buffer = SafeBuffer; + function SafeBuffer(arg, encodingOrOffset, length) { + return Buffer3(arg, encodingOrOffset, length); + } + SafeBuffer.prototype = Object.create(Buffer3.prototype); + copyProps(Buffer3, SafeBuffer); + SafeBuffer.from = function(arg, encodingOrOffset, length) { + if (typeof arg === "number") throw new TypeError("Argument must not be a number"); - return te(e, t, r); + return Buffer3(arg, encodingOrOffset, length); }; - Le.alloc = function(e, t, r) { - if (typeof e != "number") + SafeBuffer.alloc = function(size, fill2, encoding) { + if (typeof size !== "number") throw new TypeError("Argument must be a number"); - var n = te(e); - return t !== undefined ? typeof r == "string" ? n.fill(t, r) : n.fill(t) : n.fill(0), n; + var buf = Buffer3(size); + if (fill2 !== undefined) + if (typeof encoding === "string") + buf.fill(fill2, encoding); + else + buf.fill(fill2); + else + buf.fill(0); + return buf; }; - Le.allocUnsafe = function(e) { - if (typeof e != "number") + SafeBuffer.allocUnsafe = function(size) { + if (typeof size !== "number") throw new TypeError("Argument must be a number"); - return te(e); + return Buffer3(size); }; - Le.allocUnsafeSlow = function(e) { - if (typeof e != "number") + SafeBuffer.allocUnsafeSlow = function(size) { + if (typeof size !== "number") throw new TypeError("Argument must be a number"); - return qt.SlowBuffer(e); + return buffer.SlowBuffer(size); }; }); - Fo = S((No) => { - var Hr = Bo().Buffer, Lo = Hr.isEncoding || function(e) { - switch (e = "" + e, e && e.toLowerCase()) { + var require_string_decoder = __commonJS2((exports2) => { + var Buffer3 = require_safe_buffer().Buffer, isEncoding2 = Buffer3.isEncoding || function(encoding) { + switch (encoding = "" + encoding, encoding && encoding.toLowerCase()) { case "hex": case "utf8": case "utf-8": @@ -2432,11 +3418,12 @@ var init_stream = __esm(() => { return false; } }; - function Sf(e) { - if (!e) + function _normalizeEncoding(enc) { + if (!enc) return "utf8"; - for (var t;; ) - switch (e) { + var retried; + while (true) + switch (enc) { case "utf8": case "utf-8": return "utf8"; @@ -2451,516 +3438,754 @@ var init_stream = __esm(() => { case "base64": case "ascii": case "hex": - return e; + return enc; default: - if (t) + if (retried) return; - e = ("" + e).toLowerCase(), t = true; + enc = ("" + enc).toLowerCase(), retried = true; } } - function mf(e) { - var t = Sf(e); - if (typeof t != "string" && (Hr.isEncoding === Lo || !Lo(e))) - throw new Error("Unknown encoding: " + e); - return t || e; + function normalizeEncoding(enc) { + var nenc = _normalizeEncoding(enc); + if (typeof nenc !== "string" && (Buffer3.isEncoding === isEncoding2 || !isEncoding2(enc))) + throw new Error("Unknown encoding: " + enc); + return nenc || enc; } - No.StringDecoder = ut; - function ut(e) { - this.encoding = mf(e); - var t; + exports2.StringDecoder = StringDecoder; + function StringDecoder(encoding) { + this.encoding = normalizeEncoding(encoding); + var nb; switch (this.encoding) { case "utf16le": - this.text = Bf, this.end = Lf, t = 4; + this.text = utf16Text, this.end = utf16End, nb = 4; break; case "utf8": - this.fillLast = xf, t = 4; + this.fillLast = utf8FillLast, nb = 4; break; case "base64": - this.text = Nf, this.end = Ff, t = 3; + this.text = base64Text, this.end = base64End, nb = 3; break; default: - this.write = Mf, this.end = Cf; + this.write = simpleWrite, this.end = simpleEnd; return; } - this.lastNeed = 0, this.lastTotal = 0, this.lastChar = Hr.allocUnsafe(t); + this.lastNeed = 0, this.lastTotal = 0, this.lastChar = Buffer3.allocUnsafe(nb); } - ut.prototype.write = function(e) { - if (e.length === 0) + StringDecoder.prototype.write = function(buf) { + if (buf.length === 0) return ""; - var t, r; + var r, i2; if (this.lastNeed) { - if (t = this.fillLast(e), t === undefined) + if (r = this.fillLast(buf), r === undefined) return ""; - r = this.lastNeed, this.lastNeed = 0; + i2 = this.lastNeed, this.lastNeed = 0; } else - r = 0; - return r < e.length ? t ? t + this.text(e, r) : this.text(e, r) : t || ""; - }; - ut.prototype.end = Tf; - ut.prototype.text = If; - ut.prototype.fillLast = function(e) { - if (this.lastNeed <= e.length) - return e.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, this.lastNeed), this.lastChar.toString(this.encoding, 0, this.lastTotal); - e.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, e.length), this.lastNeed -= e.length; - }; - function jr(e) { - return e <= 127 ? 0 : e >> 5 === 6 ? 2 : e >> 4 === 14 ? 3 : e >> 3 === 30 ? 4 : e >> 6 === 2 ? -1 : -2; - } - function Rf(e, t, r) { - var n = t.length - 1; - if (n < r) + i2 = 0; + if (i2 < buf.length) + return r ? r + this.text(buf, i2) : this.text(buf, i2); + return r || ""; + }; + StringDecoder.prototype.end = utf8End; + StringDecoder.prototype.text = utf8Text; + StringDecoder.prototype.fillLast = function(buf) { + if (this.lastNeed <= buf.length) + return buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, this.lastNeed), this.lastChar.toString(this.encoding, 0, this.lastTotal); + buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, buf.length), this.lastNeed -= buf.length; + }; + function utf8CheckByte(byte) { + if (byte <= 127) + return 0; + else if (byte >> 5 === 6) + return 2; + else if (byte >> 4 === 14) + return 3; + else if (byte >> 3 === 30) + return 4; + return byte >> 6 === 2 ? -1 : -2; + } + function utf8CheckIncomplete(self2, buf, i2) { + var j = buf.length - 1; + if (j < i2) + return 0; + var nb = utf8CheckByte(buf[j]); + if (nb >= 0) { + if (nb > 0) + self2.lastNeed = nb - 1; + return nb; + } + if (--j < i2 || nb === -2) + return 0; + if (nb = utf8CheckByte(buf[j]), nb >= 0) { + if (nb > 0) + self2.lastNeed = nb - 2; + return nb; + } + if (--j < i2 || nb === -2) return 0; - var i = jr(t[n]); - return i >= 0 ? (i > 0 && (e.lastNeed = i - 1), i) : --n < r || i === -2 ? 0 : (i = jr(t[n]), i >= 0 ? (i > 0 && (e.lastNeed = i - 2), i) : --n < r || i === -2 ? 0 : (i = jr(t[n]), i >= 0 ? (i > 0 && (i === 2 ? i = 0 : e.lastNeed = i - 3), i) : 0)); + if (nb = utf8CheckByte(buf[j]), nb >= 0) { + if (nb > 0) + if (nb === 2) + nb = 0; + else + self2.lastNeed = nb - 3; + return nb; + } + return 0; } - function Af(e, t, r) { - if ((t[0] & 192) !== 128) - return e.lastNeed = 0, "�"; - if (e.lastNeed > 1 && t.length > 1) { - if ((t[1] & 192) !== 128) - return e.lastNeed = 1, "�"; - if (e.lastNeed > 2 && t.length > 2 && (t[2] & 192) !== 128) - return e.lastNeed = 2, "�"; + function utf8CheckExtraBytes(self2, buf, p) { + if ((buf[0] & 192) !== 128) + return self2.lastNeed = 0, "�"; + if (self2.lastNeed > 1 && buf.length > 1) { + if ((buf[1] & 192) !== 128) + return self2.lastNeed = 1, "�"; + if (self2.lastNeed > 2 && buf.length > 2) { + if ((buf[2] & 192) !== 128) + return self2.lastNeed = 2, "�"; + } } } - function xf(e) { - var t = this.lastTotal - this.lastNeed, r = Af(this, e, t); + function utf8FillLast(buf) { + var p = this.lastTotal - this.lastNeed, r = utf8CheckExtraBytes(this, buf, p); if (r !== undefined) return r; - if (this.lastNeed <= e.length) - return e.copy(this.lastChar, t, 0, this.lastNeed), this.lastChar.toString(this.encoding, 0, this.lastTotal); - e.copy(this.lastChar, t, 0, e.length), this.lastNeed -= e.length; + if (this.lastNeed <= buf.length) + return buf.copy(this.lastChar, p, 0, this.lastNeed), this.lastChar.toString(this.encoding, 0, this.lastTotal); + buf.copy(this.lastChar, p, 0, buf.length), this.lastNeed -= buf.length; } - function If(e, t) { - var r = Rf(this, e, t); + function utf8Text(buf, i2) { + var total = utf8CheckIncomplete(this, buf, i2); if (!this.lastNeed) - return e.toString("utf8", t); - this.lastTotal = r; - var n = e.length - (r - this.lastNeed); - return e.copy(this.lastChar, 0, n), e.toString("utf8", t, n); - } - function Tf(e) { - var t = e && e.length ? this.write(e) : ""; - return this.lastNeed ? t + "�" : t; - } - function Bf(e, t) { - if ((e.length - t) % 2 === 0) { - var r = e.toString("utf16le", t); + return buf.toString("utf8", i2); + this.lastTotal = total; + var end = buf.length - (total - this.lastNeed); + return buf.copy(this.lastChar, 0, end), buf.toString("utf8", i2, end); + } + function utf8End(buf) { + var r = buf && buf.length ? this.write(buf) : ""; + if (this.lastNeed) + return r + "�"; + return r; + } + function utf16Text(buf, i2) { + if ((buf.length - i2) % 2 === 0) { + var r = buf.toString("utf16le", i2); if (r) { - var n = r.charCodeAt(r.length - 1); - if (n >= 55296 && n <= 56319) - return this.lastNeed = 2, this.lastTotal = 4, this.lastChar[0] = e[e.length - 2], this.lastChar[1] = e[e.length - 1], r.slice(0, -1); + var c = r.charCodeAt(r.length - 1); + if (c >= 55296 && c <= 56319) + return this.lastNeed = 2, this.lastTotal = 4, this.lastChar[0] = buf[buf.length - 2], this.lastChar[1] = buf[buf.length - 1], r.slice(0, -1); } return r; } - return this.lastNeed = 1, this.lastTotal = 2, this.lastChar[0] = e[e.length - 1], e.toString("utf16le", t, e.length - 1); + return this.lastNeed = 1, this.lastTotal = 2, this.lastChar[0] = buf[buf.length - 1], buf.toString("utf16le", i2, buf.length - 1); } - function Lf(e) { - var t = e && e.length ? this.write(e) : ""; + function utf16End(buf) { + var r = buf && buf.length ? this.write(buf) : ""; if (this.lastNeed) { - var r = this.lastTotal - this.lastNeed; - return t + this.lastChar.toString("utf16le", 0, r); + var end = this.lastTotal - this.lastNeed; + return r + this.lastChar.toString("utf16le", 0, end); } - return t; + return r; } - function Nf(e, t) { - var r = (e.length - t) % 3; - return r === 0 ? e.toString("base64", t) : (this.lastNeed = 3 - r, this.lastTotal = 3, r === 1 ? this.lastChar[0] = e[e.length - 1] : (this.lastChar[0] = e[e.length - 2], this.lastChar[1] = e[e.length - 1]), e.toString("base64", t, e.length - r)); + function base64Text(buf, i2) { + var n = (buf.length - i2) % 3; + if (n === 0) + return buf.toString("base64", i2); + if (this.lastNeed = 3 - n, this.lastTotal = 3, n === 1) + this.lastChar[0] = buf[buf.length - 1]; + else + this.lastChar[0] = buf[buf.length - 2], this.lastChar[1] = buf[buf.length - 1]; + return buf.toString("base64", i2, buf.length - n); } - function Ff(e) { - var t = e && e.length ? this.write(e) : ""; - return this.lastNeed ? t + this.lastChar.toString("base64", 0, 3 - this.lastNeed) : t; + function base64End(buf) { + var r = buf && buf.length ? this.write(buf) : ""; + if (this.lastNeed) + return r + this.lastChar.toString("base64", 0, 3 - this.lastNeed); + return r; } - function Mf(e) { - return e.toString(this.encoding); + function simpleWrite(buf) { + return buf.toString(this.encoding); } - function Cf(e) { - return e && e.length ? this.write(e) : ""; + function simpleEnd(buf) { + return buf && buf.length ? this.write(buf) : ""; } }); - Mo = {}; - or(Mo, { StringDecoder: () => vt.StringDecoder, default: () => vt.StringDecoder }); - Co = kn(() => { - vt = rt(Fo()); - }); - Gr = S((Xh, Po) => { - var ko = (we(), ye(H)), { PromisePrototypeThen: kf, SymbolAsyncIterator: Do, SymbolIterator: Oo } = I(), { Buffer: Df } = le(), { ERR_INVALID_ARG_TYPE: Of, ERR_STREAM_NULL_VALUES: Pf } = O().codes; - function qf(e, t, r) { - let n; - if (typeof t == "string" || t instanceof Df) - return new e({ objectMode: true, ...r, read() { - this.push(t), this.push(null); + var require_from = __commonJS2((exports2, module2) => { + var process2 = require_process(), { PromisePrototypeThen, SymbolAsyncIterator, SymbolIterator } = require_primordials(), { Buffer: Buffer3 } = (init_buffer(), __toCommonJS(exports_buffer)), { ERR_INVALID_ARG_TYPE: ERR_INVALID_ARG_TYPE3, ERR_STREAM_NULL_VALUES } = require_errors().codes; + function from2(Readable, iterable, opts) { + let iterator; + if (typeof iterable === "string" || iterable instanceof Buffer3) + return new Readable({ objectMode: true, ...opts, read() { + this.push(iterable), this.push(null); } }); - let i; - if (t && t[Do]) - i = true, n = t[Do](); - else if (t && t[Oo]) - i = false, n = t[Oo](); + let isAsync; + if (iterable && iterable[SymbolAsyncIterator]) + isAsync = true, iterator = iterable[SymbolAsyncIterator](); + else if (iterable && iterable[SymbolIterator]) + isAsync = false, iterator = iterable[SymbolIterator](); else - throw new Of("iterable", ["Iterable"], t); - let o = new e({ objectMode: true, highWaterMark: 1, ...r }), l = false; - o._read = function() { - l || (l = true, a()); - }, o._destroy = function(s, c) { - kf(u(s), () => ko.nextTick(c, s), (d) => ko.nextTick(c, d || s)); + throw new ERR_INVALID_ARG_TYPE3("iterable", ["Iterable"], iterable); + let readable = new Readable({ objectMode: true, highWaterMark: 1, ...opts }), reading = false; + readable._read = function() { + if (!reading) + reading = true, next(); + }, readable._destroy = function(error, cb) { + PromisePrototypeThen(close(error), () => process2.nextTick(cb, error), (e) => process2.nextTick(cb, e || error)); }; - async function u(s) { - let c = s != null, d = typeof n.throw == "function"; - if (c && d) { - let { value: y, done: h } = await n.throw(s); - if (await y, h) + async function close(error) { + let hadError = error !== undefined && error !== null, hasThrow = typeof iterator.throw === "function"; + if (hadError && hasThrow) { + let { value, done } = await iterator.throw(error); + if (await value, done) return; } - if (typeof n.return == "function") { - let { value: y } = await n.return(); - await y; + if (typeof iterator.return === "function") { + let { value } = await iterator.return(); + await value; } } - async function a() { + async function next() { for (;; ) { try { - let { value: s, done: c } = i ? await n.next() : n.next(); - if (c) - o.push(null); + let { value, done } = isAsync ? await iterator.next() : iterator.next(); + if (done) + readable.push(null); else { - let d = s && typeof s.then == "function" ? await s : s; - if (d === null) - throw l = false, new Pf; - if (o.push(d)) + let res = value && typeof value.then === "function" ? await value : value; + if (res === null) + throw reading = false, new ERR_STREAM_NULL_VALUES; + else if (readable.push(res)) continue; - l = false; + else + reading = false; } - } catch (s) { - o.destroy(s); + } catch (err) { + readable.destroy(err); } break; } } - return o; + return readable; } - Po.exports = qf; + module2.exports = from2; }); - at = S((Jh, tl) => { - var V = (we(), ye(H)), { ArrayPrototypeIndexOf: vf, NumberIsInteger: Uf, NumberIsNaN: Wf, NumberParseInt: $f, ObjectDefineProperties: Zr, ObjectKeys: jf, ObjectSetPrototypeOf: Uo, Promise: Wo, SafeSet: Hf, SymbolAsyncDispose: Gf, SymbolAsyncIterator: Vf, Symbol: Yf } = I(); - tl.exports = b; - b.ReadableState = jt; - var { EventEmitter: Kf } = it(), { Stream: _e, prependListener: zf } = Dt(), { Buffer: Vr } = le(), { addAbortSignal: Xf } = ot(), $o = ae(), g = q().debuglog("stream", (e) => { - g = e; - }), Jf = So(), ze = Be(), { getHighWaterMark: Qf, getDefaultHighWaterMark: Zf } = lt(), { aggregateTwoErrors: qo, codes: { ERR_INVALID_ARG_TYPE: ec, ERR_METHOD_NOT_IMPLEMENTED: tc, ERR_OUT_OF_RANGE: rc, ERR_STREAM_PUSH_AFTER_EOF: nc, ERR_STREAM_UNSHIFT_AFTER_END_EVENT: ic }, AbortError: oc } = O(), { validateObject: lc } = He(), Ne = Yf("kPaused"), { StringDecoder: jo } = (Co(), ye(Mo)), uc = Gr(); - Uo(b.prototype, _e.prototype); - Uo(b, _e); - var Yr = () => { - }, { errorOrDestroy: Ye } = ze, Ke = 1 << 0, sc = 1 << 1, Ho = 1 << 2, st = 1 << 3, Go = 1 << 4, Ut = 1 << 5, Wt = 1 << 6, Vo = 1 << 7, ac = 1 << 8, fc = 1 << 9, cc = 1 << 10, Jr = 1 << 11, Qr = 1 << 12, dc = 1 << 13, hc = 1 << 14, pc = 1 << 15, Yo = 1 << 16, yc = 1 << 17, bc = 1 << 18; - function M(e) { + var require_readable = __commonJS2((exports2, module2) => { + var process2 = require_process(), { ArrayPrototypeIndexOf, NumberIsInteger, NumberIsNaN, NumberParseInt, ObjectDefineProperties, ObjectKeys, ObjectSetPrototypeOf, Promise: Promise2, SafeSet, SymbolAsyncDispose, SymbolAsyncIterator, Symbol: Symbol2 } = require_primordials(); + module2.exports = Readable; + Readable.ReadableState = ReadableState; + var { EventEmitter: EE } = (init_events(), __toCommonJS(exports_events)), { Stream, prependListener: prependListener2 } = require_legacy(), { Buffer: Buffer3 } = (init_buffer(), __toCommonJS(exports_buffer)), { addAbortSignal } = require_add_abort_signal(), eos = require_end_of_stream(), debug = require_util().debuglog("stream", (fn) => { + debug = fn; + }), BufferList = require_buffer_list(), destroyImpl = require_destroy(), { getHighWaterMark, getDefaultHighWaterMark } = require_state(), { aggregateTwoErrors, codes: { ERR_INVALID_ARG_TYPE: ERR_INVALID_ARG_TYPE3, ERR_METHOD_NOT_IMPLEMENTED, ERR_OUT_OF_RANGE: ERR_OUT_OF_RANGE3, ERR_STREAM_PUSH_AFTER_EOF, ERR_STREAM_UNSHIFT_AFTER_END_EVENT }, AbortError: AbortError2 } = require_errors(), { validateObject } = require_validators(), kPaused = Symbol2("kPaused"), { StringDecoder } = require_string_decoder(), from2 = require_from(); + ObjectSetPrototypeOf(Readable.prototype, Stream.prototype); + ObjectSetPrototypeOf(Readable, Stream); + var nop = () => {}, { errorOrDestroy } = destroyImpl, kObjectMode = 1, kEnded = 2, kEndEmitted = 4, kReading = 8, kConstructed = 16, kSync = 32, kNeedReadable = 64, kEmittedReadable = 128, kReadableListening = 256, kResumeScheduled = 512, kErrorEmitted = 1024, kEmitClose = 2048, kAutoDestroy = 4096, kDestroyed = 8192, kClosed = 16384, kCloseEmitted = 32768, kMultiAwaitDrain = 65536, kReadingMore = 131072, kDataEmitted = 262144; + function makeBitMapDescriptor(bit) { return { enumerable: false, get() { - return (this.state & e) !== 0; - }, set(t) { - t ? this.state |= e : this.state &= ~e; + return (this.state & bit) !== 0; + }, set(value) { + if (value) + this.state |= bit; + else + this.state &= ~bit; } }; } - Zr(jt.prototype, { objectMode: M(Ke), ended: M(sc), endEmitted: M(Ho), reading: M(st), constructed: M(Go), sync: M(Ut), needReadable: M(Wt), emittedReadable: M(Vo), readableListening: M(ac), resumeScheduled: M(fc), errorEmitted: M(cc), emitClose: M(Jr), autoDestroy: M(Qr), destroyed: M(dc), closed: M(hc), closeEmitted: M(pc), multiAwaitDrain: M(Yo), readingMore: M(yc), dataEmitted: M(bc) }); - function jt(e, t, r) { - typeof r != "boolean" && (r = t instanceof re()), this.state = Jr | Qr | Go | Ut, e && e.objectMode && (this.state |= Ke), r && e && e.readableObjectMode && (this.state |= Ke), this.highWaterMark = e ? Qf(this, e, "readableHighWaterMark", r) : Zf(false), this.buffer = new Jf, this.length = 0, this.pipes = [], this.flowing = null, this[Ne] = null, e && e.emitClose === false && (this.state &= ~Jr), e && e.autoDestroy === false && (this.state &= ~Qr), this.errored = null, this.defaultEncoding = e && e.defaultEncoding || "utf8", this.awaitDrainWriters = null, this.decoder = null, this.encoding = null, e && e.encoding && (this.decoder = new jo(e.encoding), this.encoding = e.encoding); - } - function b(e) { - if (!(this instanceof b)) - return new b(e); - let t = this instanceof re(); - this._readableState = new jt(e, this, t), e && (typeof e.read == "function" && (this._read = e.read), typeof e.destroy == "function" && (this._destroy = e.destroy), typeof e.construct == "function" && (this._construct = e.construct), e.signal && !t && Xf(e.signal, this)), _e.call(this, e), ze.construct(this, () => { - this._readableState.needReadable && $t(this, this._readableState); + ObjectDefineProperties(ReadableState.prototype, { objectMode: makeBitMapDescriptor(kObjectMode), ended: makeBitMapDescriptor(kEnded), endEmitted: makeBitMapDescriptor(kEndEmitted), reading: makeBitMapDescriptor(kReading), constructed: makeBitMapDescriptor(kConstructed), sync: makeBitMapDescriptor(kSync), needReadable: makeBitMapDescriptor(kNeedReadable), emittedReadable: makeBitMapDescriptor(kEmittedReadable), readableListening: makeBitMapDescriptor(kReadableListening), resumeScheduled: makeBitMapDescriptor(kResumeScheduled), errorEmitted: makeBitMapDescriptor(kErrorEmitted), emitClose: makeBitMapDescriptor(kEmitClose), autoDestroy: makeBitMapDescriptor(kAutoDestroy), destroyed: makeBitMapDescriptor(kDestroyed), closed: makeBitMapDescriptor(kClosed), closeEmitted: makeBitMapDescriptor(kCloseEmitted), multiAwaitDrain: makeBitMapDescriptor(kMultiAwaitDrain), readingMore: makeBitMapDescriptor(kReadingMore), dataEmitted: makeBitMapDescriptor(kDataEmitted) }); + function ReadableState(options, stream, isDuplex) { + if (typeof isDuplex !== "boolean") + isDuplex = stream instanceof require_duplex(); + if (this.state = kEmitClose | kAutoDestroy | kConstructed | kSync, options && options.objectMode) + this.state |= kObjectMode; + if (isDuplex && options && options.readableObjectMode) + this.state |= kObjectMode; + if (this.highWaterMark = options ? getHighWaterMark(this, options, "readableHighWaterMark", isDuplex) : getDefaultHighWaterMark(false), this.buffer = new BufferList, this.length = 0, this.pipes = [], this.flowing = null, this[kPaused] = null, options && options.emitClose === false) + this.state &= ~kEmitClose; + if (options && options.autoDestroy === false) + this.state &= ~kAutoDestroy; + if (this.errored = null, this.defaultEncoding = options && options.defaultEncoding || "utf8", this.awaitDrainWriters = null, this.decoder = null, this.encoding = null, options && options.encoding) + this.decoder = new StringDecoder(options.encoding), this.encoding = options.encoding; + } + function Readable(options) { + if (!(this instanceof Readable)) + return new Readable(options); + let isDuplex = this instanceof require_duplex(); + if (this._readableState = new ReadableState(options, this, isDuplex), options) { + if (typeof options.read === "function") + this._read = options.read; + if (typeof options.destroy === "function") + this._destroy = options.destroy; + if (typeof options.construct === "function") + this._construct = options.construct; + if (options.signal && !isDuplex) + addAbortSignal(options.signal, this); + } + Stream.call(this, options), destroyImpl.construct(this, () => { + if (this._readableState.needReadable) + maybeReadMore(this, this._readableState); }); } - b.prototype.destroy = ze.destroy; - b.prototype._undestroy = ze.undestroy; - b.prototype._destroy = function(e, t) { - t(e); - }; - b.prototype[Kf.captureRejectionSymbol] = function(e) { - this.destroy(e); - }; - b.prototype[Gf] = function() { - let e; - return this.destroyed || (e = this.readableEnded ? null : new oc, this.destroy(e)), new Wo((t, r) => $o(this, (n) => n && n !== e ? r(n) : t(null))); - }; - b.prototype.push = function(e, t) { - return Ko(this, e, t, false); - }; - b.prototype.unshift = function(e, t) { - return Ko(this, e, t, true); - }; - function Ko(e, t, r, n) { - g("readableAddChunk", t); - let i = e._readableState, o; - if ((i.state & Ke) === 0 && (typeof t == "string" ? (r = r || i.defaultEncoding, i.encoding !== r && (n && i.encoding ? t = Vr.from(t, r).toString(i.encoding) : (t = Vr.from(t, r), r = ""))) : t instanceof Vr ? r = "" : _e._isUint8Array(t) ? (t = _e._uint8ArrayToBuffer(t), r = "") : t != null && (o = new ec("chunk", ["string", "Buffer", "Uint8Array"], t))), o) - Ye(e, o); - else if (t === null) - i.state &= ~st, _c(e, i); - else if ((i.state & Ke) !== 0 || t && t.length > 0) - if (n) - if ((i.state & Ho) !== 0) - Ye(e, new ic); - else { - if (i.destroyed || i.errored) - return false; - Kr(e, i, t, true); - } - else if (i.ended) - Ye(e, new nc); - else { - if (i.destroyed || i.errored) + Readable.prototype.destroy = destroyImpl.destroy; + Readable.prototype._undestroy = destroyImpl.undestroy; + Readable.prototype._destroy = function(err, cb) { + cb(err); + }; + Readable.prototype[EE.captureRejectionSymbol] = function(err) { + this.destroy(err); + }; + Readable.prototype[SymbolAsyncDispose] = function() { + let error; + if (!this.destroyed) + error = this.readableEnded ? null : new AbortError2, this.destroy(error); + return new Promise2((resolve, reject) => eos(this, (err) => err && err !== error ? reject(err) : resolve(null))); + }; + Readable.prototype.push = function(chunk, encoding) { + return readableAddChunk(this, chunk, encoding, false); + }; + Readable.prototype.unshift = function(chunk, encoding) { + return readableAddChunk(this, chunk, encoding, true); + }; + function readableAddChunk(stream, chunk, encoding, addToFront) { + debug("readableAddChunk", chunk); + let state = stream._readableState, err; + if ((state.state & kObjectMode) === 0) { + if (typeof chunk === "string") { + if (encoding = encoding || state.defaultEncoding, state.encoding !== encoding) + if (addToFront && state.encoding) + chunk = Buffer3.from(chunk, encoding).toString(state.encoding); + else + chunk = Buffer3.from(chunk, encoding), encoding = ""; + } else if (chunk instanceof Buffer3) + encoding = ""; + else if (Stream._isUint8Array(chunk)) + chunk = Stream._uint8ArrayToBuffer(chunk), encoding = ""; + else if (chunk != null) + err = new ERR_INVALID_ARG_TYPE3("chunk", ["string", "Buffer", "Uint8Array"], chunk); + } + if (err) + errorOrDestroy(stream, err); + else if (chunk === null) + state.state &= ~kReading, onEofChunk(stream, state); + else if ((state.state & kObjectMode) !== 0 || chunk && chunk.length > 0) + if (addToFront) + if ((state.state & kEndEmitted) !== 0) + errorOrDestroy(stream, new ERR_STREAM_UNSHIFT_AFTER_END_EVENT); + else if (state.destroyed || state.errored) return false; - i.state &= ~st, i.decoder && !r ? (t = i.decoder.write(t), i.objectMode || t.length !== 0 ? Kr(e, i, t, false) : $t(e, i)) : Kr(e, i, t, false); - } + else + addChunk(stream, state, chunk, true); + else if (state.ended) + errorOrDestroy(stream, new ERR_STREAM_PUSH_AFTER_EOF); + else if (state.destroyed || state.errored) + return false; + else if (state.state &= ~kReading, state.decoder && !encoding) + if (chunk = state.decoder.write(chunk), state.objectMode || chunk.length !== 0) + addChunk(stream, state, chunk, false); + else + maybeReadMore(stream, state); + else + addChunk(stream, state, chunk, false); + else if (!addToFront) + state.state &= ~kReading, maybeReadMore(stream, state); + return !state.ended && (state.length < state.highWaterMark || state.length === 0); + } + function addChunk(stream, state, chunk, addToFront) { + if (state.flowing && state.length === 0 && !state.sync && stream.listenerCount("data") > 0) { + if ((state.state & kMultiAwaitDrain) !== 0) + state.awaitDrainWriters.clear(); + else + state.awaitDrainWriters = null; + state.dataEmitted = true, stream.emit("data", chunk); + } else { + if (state.length += state.objectMode ? 1 : chunk.length, addToFront) + state.buffer.unshift(chunk); + else + state.buffer.push(chunk); + if ((state.state & kNeedReadable) !== 0) + emitReadable(stream); + } + maybeReadMore(stream, state); + } + Readable.prototype.isPaused = function() { + let state = this._readableState; + return state[kPaused] === true || state.flowing === false; + }; + Readable.prototype.setEncoding = function(enc) { + let decoder = new StringDecoder(enc); + this._readableState.decoder = decoder, this._readableState.encoding = this._readableState.decoder.encoding; + let buffer = this._readableState.buffer, content = ""; + for (let data of buffer) + content += decoder.write(data); + if (buffer.clear(), content !== "") + buffer.push(content); + return this._readableState.length = content.length, this; + }; + var MAX_HWM = 1073741824; + function computeNewHighWaterMark(n) { + if (n > MAX_HWM) + throw new ERR_OUT_OF_RANGE3("size", "<= 1GiB", n); else - n || (i.state &= ~st, $t(e, i)); - return !i.ended && (i.length < i.highWaterMark || i.length === 0); - } - function Kr(e, t, r, n) { - t.flowing && t.length === 0 && !t.sync && e.listenerCount("data") > 0 ? ((t.state & Yo) !== 0 ? t.awaitDrainWriters.clear() : t.awaitDrainWriters = null, t.dataEmitted = true, e.emit("data", r)) : (t.length += t.objectMode ? 1 : r.length, n ? t.buffer.unshift(r) : t.buffer.push(r), (t.state & Wt) !== 0 && Ht(e)), $t(e, t); + n--, n |= n >>> 1, n |= n >>> 2, n |= n >>> 4, n |= n >>> 8, n |= n >>> 16, n++; + return n; } - b.prototype.isPaused = function() { - let e = this._readableState; - return e[Ne] === true || e.flowing === false; - }; - b.prototype.setEncoding = function(e) { - let t = new jo(e); - this._readableState.decoder = t, this._readableState.encoding = this._readableState.decoder.encoding; - let r = this._readableState.buffer, n = ""; - for (let i of r) - n += t.write(i); - return r.clear(), n !== "" && r.push(n), this._readableState.length = n.length, this; - }; - var wc = 1073741824; - function gc(e) { - if (e > wc) - throw new rc("size", "<= 1GiB", e); - return e--, e |= e >>> 1, e |= e >>> 2, e |= e >>> 4, e |= e >>> 8, e |= e >>> 16, e++, e; - } - function vo(e, t) { - return e <= 0 || t.length === 0 && t.ended ? 0 : (t.state & Ke) !== 0 ? 1 : Wf(e) ? t.flowing && t.length ? t.buffer.first().length : t.length : e <= t.length ? e : t.ended ? t.length : 0; - } - b.prototype.read = function(e) { - g("read", e), e === undefined ? e = NaN : Uf(e) || (e = $f(e, 10)); - let t = this._readableState, r = e; - if (e > t.highWaterMark && (t.highWaterMark = gc(e)), e !== 0 && (t.state &= ~Vo), e === 0 && t.needReadable && ((t.highWaterMark !== 0 ? t.length >= t.highWaterMark : t.length > 0) || t.ended)) - return g("read: emitReadable", t.length, t.ended), t.length === 0 && t.ended ? zr(this) : Ht(this), null; - if (e = vo(e, t), e === 0 && t.ended) - return t.length === 0 && zr(this), null; - let n = (t.state & Wt) !== 0; - if (g("need readable", n), (t.length === 0 || t.length - e < t.highWaterMark) && (n = true, g("length less than watermark", n)), t.ended || t.reading || t.destroyed || t.errored || !t.constructed) - n = false, g("reading, ended or constructing", n); - else if (n) { - g("do read"), t.state |= st | Ut, t.length === 0 && (t.state |= Wt); + function howMuchToRead(n, state) { + if (n <= 0 || state.length === 0 && state.ended) + return 0; + if ((state.state & kObjectMode) !== 0) + return 1; + if (NumberIsNaN(n)) { + if (state.flowing && state.length) + return state.buffer.first().length; + return state.length; + } + if (n <= state.length) + return n; + return state.ended ? state.length : 0; + } + Readable.prototype.read = function(n) { + if (debug("read", n), n === undefined) + n = NaN; + else if (!NumberIsInteger(n)) + n = NumberParseInt(n, 10); + let state = this._readableState, nOrig = n; + if (n > state.highWaterMark) + state.highWaterMark = computeNewHighWaterMark(n); + if (n !== 0) + state.state &= ~kEmittedReadable; + if (n === 0 && state.needReadable && ((state.highWaterMark !== 0 ? state.length >= state.highWaterMark : state.length > 0) || state.ended)) { + if (debug("read: emitReadable", state.length, state.ended), state.length === 0 && state.ended) + endReadable(this); + else + emitReadable(this); + return null; + } + if (n = howMuchToRead(n, state), n === 0 && state.ended) { + if (state.length === 0) + endReadable(this); + return null; + } + let doRead = (state.state & kNeedReadable) !== 0; + if (debug("need readable", doRead), state.length === 0 || state.length - n < state.highWaterMark) + doRead = true, debug("length less than watermark", doRead); + if (state.ended || state.reading || state.destroyed || state.errored || !state.constructed) + doRead = false, debug("reading, ended or constructing", doRead); + else if (doRead) { + if (debug("do read"), state.state |= kReading | kSync, state.length === 0) + state.state |= kNeedReadable; try { - this._read(t.highWaterMark); - } catch (o) { - Ye(this, o); + this._read(state.highWaterMark); + } catch (err) { + errorOrDestroy(this, err); } - t.state &= ~Ut, t.reading || (e = vo(r, t)); + if (state.state &= ~kSync, !state.reading) + n = howMuchToRead(nOrig, state); } - let i; - return e > 0 ? i = Zo(e, t) : i = null, i === null ? (t.needReadable = t.length <= t.highWaterMark, e = 0) : (t.length -= e, t.multiAwaitDrain ? t.awaitDrainWriters.clear() : t.awaitDrainWriters = null), t.length === 0 && (t.ended || (t.needReadable = true), r !== e && t.ended && zr(this)), i !== null && !t.errorEmitted && !t.closeEmitted && (t.dataEmitted = true, this.emit("data", i)), i; + let ret; + if (n > 0) + ret = fromList(n, state); + else + ret = null; + if (ret === null) + state.needReadable = state.length <= state.highWaterMark, n = 0; + else if (state.length -= n, state.multiAwaitDrain) + state.awaitDrainWriters.clear(); + else + state.awaitDrainWriters = null; + if (state.length === 0) { + if (!state.ended) + state.needReadable = true; + if (nOrig !== n && state.ended) + endReadable(this); + } + if (ret !== null && !state.errorEmitted && !state.closeEmitted) + state.dataEmitted = true, this.emit("data", ret); + return ret; }; - function _c(e, t) { - if (g("onEofChunk"), !t.ended) { - if (t.decoder) { - let r = t.decoder.end(); - r && r.length && (t.buffer.push(r), t.length += t.objectMode ? 1 : r.length); - } - t.ended = true, t.sync ? Ht(e) : (t.needReadable = false, t.emittedReadable = true, zo(e)); + function onEofChunk(stream, state) { + if (debug("onEofChunk"), state.ended) + return; + if (state.decoder) { + let chunk = state.decoder.end(); + if (chunk && chunk.length) + state.buffer.push(chunk), state.length += state.objectMode ? 1 : chunk.length; } - } - function Ht(e) { - let t = e._readableState; - g("emitReadable", t.needReadable, t.emittedReadable), t.needReadable = false, t.emittedReadable || (g("emitReadable", t.flowing), t.emittedReadable = true, V.nextTick(zo, e)); - } - function zo(e) { - let t = e._readableState; - g("emitReadable_", t.destroyed, t.length, t.ended), !t.destroyed && !t.errored && (t.length || t.ended) && (e.emit("readable"), t.emittedReadable = false), t.needReadable = !t.flowing && !t.ended && t.length <= t.highWaterMark, Jo(e); - } - function $t(e, t) { - !t.readingMore && t.constructed && (t.readingMore = true, V.nextTick(Ec, e, t)); - } - function Ec(e, t) { - for (;!t.reading && !t.ended && (t.length < t.highWaterMark || t.flowing && t.length === 0); ) { - let r = t.length; - if (g("maybeReadMore read 0"), e.read(0), r === t.length) + if (state.ended = true, state.sync) + emitReadable(stream); + else + state.needReadable = false, state.emittedReadable = true, emitReadable_(stream); + } + function emitReadable(stream) { + let state = stream._readableState; + if (debug("emitReadable", state.needReadable, state.emittedReadable), state.needReadable = false, !state.emittedReadable) + debug("emitReadable", state.flowing), state.emittedReadable = true, process2.nextTick(emitReadable_, stream); + } + function emitReadable_(stream) { + let state = stream._readableState; + if (debug("emitReadable_", state.destroyed, state.length, state.ended), !state.destroyed && !state.errored && (state.length || state.ended)) + stream.emit("readable"), state.emittedReadable = false; + state.needReadable = !state.flowing && !state.ended && state.length <= state.highWaterMark, flow(stream); + } + function maybeReadMore(stream, state) { + if (!state.readingMore && state.constructed) + state.readingMore = true, process2.nextTick(maybeReadMore_, stream, state); + } + function maybeReadMore_(stream, state) { + while (!state.reading && !state.ended && (state.length < state.highWaterMark || state.flowing && state.length === 0)) { + let len2 = state.length; + if (debug("maybeReadMore read 0"), stream.read(0), len2 === state.length) break; } - t.readingMore = false; + state.readingMore = false; } - b.prototype._read = function(e) { - throw new tc("_read()"); + Readable.prototype._read = function(n) { + throw new ERR_METHOD_NOT_IMPLEMENTED("_read()"); }; - b.prototype.pipe = function(e, t) { - let r = this, n = this._readableState; - n.pipes.length === 1 && (n.multiAwaitDrain || (n.multiAwaitDrain = true, n.awaitDrainWriters = new Hf(n.awaitDrainWriters ? [n.awaitDrainWriters] : []))), n.pipes.push(e), g("pipe count=%d opts=%j", n.pipes.length, t); - let o = (!t || t.end !== false) && e !== V.stdout && e !== V.stderr ? u : R; - n.endEmitted ? V.nextTick(o) : r.once("end", o), e.on("unpipe", l); - function l(w, x) { - g("onunpipe"), w === r && x && x.hasUnpiped === false && (x.hasUnpiped = true, c()); - } - function u() { - g("onend"), e.end(); - } - let a, s = false; - function c() { - g("cleanup"), e.removeListener("close", _), e.removeListener("finish", p), a && e.removeListener("drain", a), e.removeListener("error", h), e.removeListener("unpipe", l), r.removeListener("end", u), r.removeListener("end", R), r.removeListener("data", y), s = true, a && n.awaitDrainWriters && (!e._writableState || e._writableState.needDrain) && a(); - } - function d() { - s || (n.pipes.length === 1 && n.pipes[0] === e ? (g("false write response, pause", 0), n.awaitDrainWriters = e, n.multiAwaitDrain = false) : n.pipes.length > 1 && n.pipes.includes(e) && (g("false write response, pause", n.awaitDrainWriters.size), n.awaitDrainWriters.add(e)), r.pause()), a || (a = Sc(r, e), e.on("drain", a)); - } - r.on("data", y); - function y(w) { - g("ondata"); - let x = e.write(w); - g("dest.write", x), x === false && d(); - } - function h(w) { - if (g("onerror", w), R(), e.removeListener("error", h), e.listenerCount("error") === 0) { - let x = e._writableState || e._readableState; - x && !x.errorEmitted ? Ye(e, w) : e.emit("error", w); - } - } - zf(e, "error", h); - function _() { - e.removeListener("finish", p), R(); - } - e.once("close", _); - function p() { - g("onfinish"), e.removeListener("close", _), R(); - } - e.once("finish", p); - function R() { - g("unpipe"), r.unpipe(e); + Readable.prototype.pipe = function(dest, pipeOpts) { + let src = this, state = this._readableState; + if (state.pipes.length === 1) { + if (!state.multiAwaitDrain) + state.multiAwaitDrain = true, state.awaitDrainWriters = new SafeSet(state.awaitDrainWriters ? [state.awaitDrainWriters] : []); } - return e.emit("pipe", r), e.writableNeedDrain === true ? d() : n.flowing || (g("pipe resume"), r.resume()), e; - }; - function Sc(e, t) { - return function() { - let n = e._readableState; - n.awaitDrainWriters === t ? (g("pipeOnDrain", 1), n.awaitDrainWriters = null) : n.multiAwaitDrain && (g("pipeOnDrain", n.awaitDrainWriters.size), n.awaitDrainWriters.delete(t)), (!n.awaitDrainWriters || n.awaitDrainWriters.size === 0) && e.listenerCount("data") && e.resume(); + state.pipes.push(dest), debug("pipe count=%d opts=%j", state.pipes.length, pipeOpts); + let endFn = (!pipeOpts || pipeOpts.end !== false) && dest !== process2.stdout && dest !== process2.stderr ? onend : unpipe; + if (state.endEmitted) + process2.nextTick(endFn); + else + src.once("end", endFn); + dest.on("unpipe", onunpipe); + function onunpipe(readable, unpipeInfo) { + if (debug("onunpipe"), readable === src) { + if (unpipeInfo && unpipeInfo.hasUnpiped === false) + unpipeInfo.hasUnpiped = true, cleanup(); + } + } + function onend() { + debug("onend"), dest.end(); + } + let ondrain, cleanedUp = false; + function cleanup() { + if (debug("cleanup"), dest.removeListener("close", onclose), dest.removeListener("finish", onfinish), ondrain) + dest.removeListener("drain", ondrain); + if (dest.removeListener("error", onerror), dest.removeListener("unpipe", onunpipe), src.removeListener("end", onend), src.removeListener("end", unpipe), src.removeListener("data", ondata), cleanedUp = true, ondrain && state.awaitDrainWriters && (!dest._writableState || dest._writableState.needDrain)) + ondrain(); + } + function pause() { + if (!cleanedUp) { + if (state.pipes.length === 1 && state.pipes[0] === dest) + debug("false write response, pause", 0), state.awaitDrainWriters = dest, state.multiAwaitDrain = false; + else if (state.pipes.length > 1 && state.pipes.includes(dest)) + debug("false write response, pause", state.awaitDrainWriters.size), state.awaitDrainWriters.add(dest); + src.pause(); + } + if (!ondrain) + ondrain = pipeOnDrain(src, dest), dest.on("drain", ondrain); + } + src.on("data", ondata); + function ondata(chunk) { + debug("ondata"); + let ret = dest.write(chunk); + if (debug("dest.write", ret), ret === false) + pause(); + } + function onerror(er) { + if (debug("onerror", er), unpipe(), dest.removeListener("error", onerror), dest.listenerCount("error") === 0) { + let s = dest._writableState || dest._readableState; + if (s && !s.errorEmitted) + errorOrDestroy(dest, er); + else + dest.emit("error", er); + } + } + prependListener2(dest, "error", onerror); + function onclose() { + dest.removeListener("finish", onfinish), unpipe(); + } + dest.once("close", onclose); + function onfinish() { + debug("onfinish"), dest.removeListener("close", onclose), unpipe(); + } + dest.once("finish", onfinish); + function unpipe() { + debug("unpipe"), src.unpipe(dest); + } + if (dest.emit("pipe", src), dest.writableNeedDrain === true) + pause(); + else if (!state.flowing) + debug("pipe resume"), src.resume(); + return dest; + }; + function pipeOnDrain(src, dest) { + return function pipeOnDrainFunctionResult() { + let state = src._readableState; + if (state.awaitDrainWriters === dest) + debug("pipeOnDrain", 1), state.awaitDrainWriters = null; + else if (state.multiAwaitDrain) + debug("pipeOnDrain", state.awaitDrainWriters.size), state.awaitDrainWriters.delete(dest); + if ((!state.awaitDrainWriters || state.awaitDrainWriters.size === 0) && src.listenerCount("data")) + src.resume(); }; } - b.prototype.unpipe = function(e) { - let t = this._readableState, r = { hasUnpiped: false }; - if (t.pipes.length === 0) + Readable.prototype.unpipe = function(dest) { + let state = this._readableState, unpipeInfo = { hasUnpiped: false }; + if (state.pipes.length === 0) return this; - if (!e) { - let i = t.pipes; - t.pipes = [], this.pause(); - for (let o = 0;o < i.length; o++) - i[o].emit("unpipe", this, { hasUnpiped: false }); + if (!dest) { + let dests = state.pipes; + state.pipes = [], this.pause(); + for (let i2 = 0;i2 < dests.length; i2++) + dests[i2].emit("unpipe", this, { hasUnpiped: false }); return this; } - let n = vf(t.pipes, e); - return n === -1 ? this : (t.pipes.splice(n, 1), t.pipes.length === 0 && this.pause(), e.emit("unpipe", this, r), this); - }; - b.prototype.on = function(e, t) { - let r = _e.prototype.on.call(this, e, t), n = this._readableState; - return e === "data" ? (n.readableListening = this.listenerCount("readable") > 0, n.flowing !== false && this.resume()) : e === "readable" && !n.endEmitted && !n.readableListening && (n.readableListening = n.needReadable = true, n.flowing = false, n.emittedReadable = false, g("on readable", n.length, n.reading), n.length ? Ht(this) : n.reading || V.nextTick(mc, this)), r; - }; - b.prototype.addListener = b.prototype.on; - b.prototype.removeListener = function(e, t) { - let r = _e.prototype.removeListener.call(this, e, t); - return e === "readable" && V.nextTick(Xo, this), r; - }; - b.prototype.off = b.prototype.removeListener; - b.prototype.removeAllListeners = function(e) { - let t = _e.prototype.removeAllListeners.apply(this, arguments); - return (e === "readable" || e === undefined) && V.nextTick(Xo, this), t; - }; - function Xo(e) { - let t = e._readableState; - t.readableListening = e.listenerCount("readable") > 0, t.resumeScheduled && t[Ne] === false ? t.flowing = true : e.listenerCount("data") > 0 ? e.resume() : t.readableListening || (t.flowing = null); - } - function mc(e) { - g("readable nexttick read 0"), e.read(0); - } - b.prototype.resume = function() { - let e = this._readableState; - return e.flowing || (g("resume"), e.flowing = !e.readableListening, Rc(this, e)), e[Ne] = false, this; - }; - function Rc(e, t) { - t.resumeScheduled || (t.resumeScheduled = true, V.nextTick(Ac, e, t)); - } - function Ac(e, t) { - g("resume", t.reading), t.reading || e.read(0), t.resumeScheduled = false, e.emit("resume"), Jo(e), t.flowing && !t.reading && e.read(0); - } - b.prototype.pause = function() { - return g("call pause flowing=%j", this._readableState.flowing), this._readableState.flowing !== false && (g("pause"), this._readableState.flowing = false, this.emit("pause")), this._readableState[Ne] = true, this; - }; - function Jo(e) { - let t = e._readableState; - for (g("flow", t.flowing);t.flowing && e.read() !== null; ) + let index = ArrayPrototypeIndexOf(state.pipes, dest); + if (index === -1) + return this; + if (state.pipes.splice(index, 1), state.pipes.length === 0) + this.pause(); + return dest.emit("unpipe", this, unpipeInfo), this; + }; + Readable.prototype.on = function(ev, fn) { + let res = Stream.prototype.on.call(this, ev, fn), state = this._readableState; + if (ev === "data") { + if (state.readableListening = this.listenerCount("readable") > 0, state.flowing !== false) + this.resume(); + } else if (ev === "readable") { + if (!state.endEmitted && !state.readableListening) { + if (state.readableListening = state.needReadable = true, state.flowing = false, state.emittedReadable = false, debug("on readable", state.length, state.reading), state.length) + emitReadable(this); + else if (!state.reading) + process2.nextTick(nReadingNextTick, this); + } + } + return res; + }; + Readable.prototype.addListener = Readable.prototype.on; + Readable.prototype.removeListener = function(ev, fn) { + let res = Stream.prototype.removeListener.call(this, ev, fn); + if (ev === "readable") + process2.nextTick(updateReadableListening, this); + return res; + }; + Readable.prototype.off = Readable.prototype.removeListener; + Readable.prototype.removeAllListeners = function(ev) { + let res = Stream.prototype.removeAllListeners.apply(this, arguments); + if (ev === "readable" || ev === undefined) + process2.nextTick(updateReadableListening, this); + return res; + }; + function updateReadableListening(self2) { + let state = self2._readableState; + if (state.readableListening = self2.listenerCount("readable") > 0, state.resumeScheduled && state[kPaused] === false) + state.flowing = true; + else if (self2.listenerCount("data") > 0) + self2.resume(); + else if (!state.readableListening) + state.flowing = null; + } + function nReadingNextTick(self2) { + debug("readable nexttick read 0"), self2.read(0); + } + Readable.prototype.resume = function() { + let state = this._readableState; + if (!state.flowing) + debug("resume"), state.flowing = !state.readableListening, resume(this, state); + return state[kPaused] = false, this; + }; + function resume(stream, state) { + if (!state.resumeScheduled) + state.resumeScheduled = true, process2.nextTick(resume_, stream, state); + } + function resume_(stream, state) { + if (debug("resume", state.reading), !state.reading) + stream.read(0); + if (state.resumeScheduled = false, stream.emit("resume"), flow(stream), state.flowing && !state.reading) + stream.read(0); + } + Readable.prototype.pause = function() { + if (debug("call pause flowing=%j", this._readableState.flowing), this._readableState.flowing !== false) + debug("pause"), this._readableState.flowing = false, this.emit("pause"); + return this._readableState[kPaused] = true, this; + }; + function flow(stream) { + let state = stream._readableState; + debug("flow", state.flowing); + while (state.flowing && stream.read() !== null) ; } - b.prototype.wrap = function(e) { - let t = false; - e.on("data", (n) => { - !this.push(n) && e.pause && (t = true, e.pause()); - }), e.on("end", () => { + Readable.prototype.wrap = function(stream) { + let paused = false; + stream.on("data", (chunk) => { + if (!this.push(chunk) && stream.pause) + paused = true, stream.pause(); + }), stream.on("end", () => { this.push(null); - }), e.on("error", (n) => { - Ye(this, n); - }), e.on("close", () => { + }), stream.on("error", (err) => { + errorOrDestroy(this, err); + }), stream.on("close", () => { this.destroy(); - }), e.on("destroy", () => { + }), stream.on("destroy", () => { this.destroy(); }), this._read = () => { - t && e.resume && (t = false, e.resume()); + if (paused && stream.resume) + paused = false, stream.resume(); }; - let r = jf(e); - for (let n = 1;n < r.length; n++) { - let i = r[n]; - this[i] === undefined && typeof e[i] == "function" && (this[i] = e[i].bind(e)); + let streamKeys = ObjectKeys(stream); + for (let j = 1;j < streamKeys.length; j++) { + let i2 = streamKeys[j]; + if (this[i2] === undefined && typeof stream[i2] === "function") + this[i2] = stream[i2].bind(stream); } return this; }; - b.prototype[Vf] = function() { - return Qo(this); - }; - b.prototype.iterator = function(e) { - return e !== undefined && lc(e, "options"), Qo(this, e); - }; - function Qo(e, t) { - typeof e.read != "function" && (e = b.wrap(e, { objectMode: true })); - let r = xc(e, t); - return r.stream = e, r; - } - async function* xc(e, t) { - let r = Yr; - function n(l) { - this === e ? (r(), r = Yr) : r = l; - } - e.on("readable", n); - let i, o = $o(e, { writable: false }, (l) => { - i = l ? qo(i, l) : null, r(), r = Yr; + Readable.prototype[SymbolAsyncIterator] = function() { + return streamToAsyncIterator(this); + }; + Readable.prototype.iterator = function(options) { + if (options !== undefined) + validateObject(options, "options"); + return streamToAsyncIterator(this, options); + }; + function streamToAsyncIterator(stream, options) { + if (typeof stream.read !== "function") + stream = Readable.wrap(stream, { objectMode: true }); + let iter = createAsyncIterator(stream, options); + return iter.stream = stream, iter; + } + async function* createAsyncIterator(stream, options) { + let callback = nop; + function next(resolve) { + if (this === stream) + callback(), callback = nop; + else + callback = resolve; + } + stream.on("readable", next); + let error, cleanup = eos(stream, { writable: false }, (err) => { + error = err ? aggregateTwoErrors(error, err) : null, callback(), callback = nop; }); try { - for (;; ) { - let l = e.destroyed ? null : e.read(); - if (l !== null) - yield l; - else { - if (i) - throw i; - if (i === null) - return; - await new Wo(n); - } + while (true) { + let chunk = stream.destroyed ? null : stream.read(); + if (chunk !== null) + yield chunk; + else if (error) + throw error; + else if (error === null) + return; + else + await new Promise2(next); } - } catch (l) { - throw i = qo(i, l), i; + } catch (err) { + throw error = aggregateTwoErrors(error, err), error; } finally { - (i || t?.destroyOnReturn !== false) && (i === undefined || e._readableState.autoDestroy) ? ze.destroyer(e, null) : (e.off("readable", n), o()); + if ((error || (options === null || options === undefined ? undefined : options.destroyOnReturn) !== false) && (error === undefined || stream._readableState.autoDestroy)) + destroyImpl.destroyer(stream, null); + else + stream.off("readable", next), cleanup(); } } - Zr(b.prototype, { readable: { __proto__: null, get() { - let e = this._readableState; - return !!e && e.readable !== false && !e.destroyed && !e.errorEmitted && !e.endEmitted; - }, set(e) { - this._readableState && (this._readableState.readable = !!e); + ObjectDefineProperties(Readable.prototype, { readable: { __proto__: null, get() { + let r = this._readableState; + return !!r && r.readable !== false && !r.destroyed && !r.errorEmitted && !r.endEmitted; + }, set(val) { + if (this._readableState) + this._readableState.readable = !!val; } }, readableDidRead: { __proto__: null, enumerable: false, get: function() { return this._readableState.dataEmitted; } }, readableAborted: { __proto__: null, enumerable: false, get: function() { @@ -2971,8 +4196,9 @@ var init_stream = __esm(() => { return this._readableState && this._readableState.buffer; } }, readableFlowing: { __proto__: null, enumerable: false, get: function() { return this._readableState.flowing; - }, set: function(e) { - this._readableState && (this._readableState.flowing = e); + }, set: function(state) { + if (this._readableState) + this._readableState.flowing = state; } }, readableLength: { __proto__: null, enumerable: false, get() { return this._readableState.length; } }, readableObjectMode: { __proto__: null, enumerable: false, get() { @@ -2985,281 +4211,415 @@ var init_stream = __esm(() => { return this._readableState ? this._readableState.closed : false; } }, destroyed: { __proto__: null, enumerable: false, get() { return this._readableState ? this._readableState.destroyed : false; - }, set(e) { - !this._readableState || (this._readableState.destroyed = e); + }, set(value) { + if (!this._readableState) + return; + this._readableState.destroyed = value; } }, readableEnded: { __proto__: null, enumerable: false, get() { return this._readableState ? this._readableState.endEmitted : false; } } }); - Zr(jt.prototype, { pipesCount: { __proto__: null, get() { + ObjectDefineProperties(ReadableState.prototype, { pipesCount: { __proto__: null, get() { return this.pipes.length; } }, paused: { __proto__: null, get() { - return this[Ne] !== false; - }, set(e) { - this[Ne] = !!e; + return this[kPaused] !== false; + }, set(value) { + this[kPaused] = !!value; } } }); - b._fromList = Zo; - function Zo(e, t) { - if (t.length === 0) + Readable._fromList = fromList; + function fromList(n, state) { + if (state.length === 0) return null; - let r; - return t.objectMode ? r = t.buffer.shift() : !e || e >= t.length ? (t.decoder ? r = t.buffer.join("") : t.buffer.length === 1 ? r = t.buffer.first() : r = t.buffer.concat(t.length), t.buffer.clear()) : r = t.buffer.consume(e, t.decoder), r; + let ret; + if (state.objectMode) + ret = state.buffer.shift(); + else if (!n || n >= state.length) { + if (state.decoder) + ret = state.buffer.join(""); + else if (state.buffer.length === 1) + ret = state.buffer.first(); + else + ret = state.buffer.concat(state.length); + state.buffer.clear(); + } else + ret = state.buffer.consume(n, state.decoder); + return ret; } - function zr(e) { - let t = e._readableState; - g("endReadable", t.endEmitted), t.endEmitted || (t.ended = true, V.nextTick(Ic, t, e)); + function endReadable(stream) { + let state = stream._readableState; + if (debug("endReadable", state.endEmitted), !state.endEmitted) + state.ended = true, process2.nextTick(endReadableNT, state, stream); } - function Ic(e, t) { - if (g("endReadableNT", e.endEmitted, e.length), !e.errored && !e.closeEmitted && !e.endEmitted && e.length === 0) { - if (e.endEmitted = true, t.emit("end"), t.writable && t.allowHalfOpen === false) - V.nextTick(Tc, t); - else if (e.autoDestroy) { - let r = t._writableState; - (!r || r.autoDestroy && (r.finished || r.writable === false)) && t.destroy(); + function endReadableNT(state, stream) { + if (debug("endReadableNT", state.endEmitted, state.length), !state.errored && !state.closeEmitted && !state.endEmitted && state.length === 0) { + if (state.endEmitted = true, stream.emit("end"), stream.writable && stream.allowHalfOpen === false) + process2.nextTick(endWritableNT, stream); + else if (state.autoDestroy) { + let wState = stream._writableState; + if (!wState || wState.autoDestroy && (wState.finished || wState.writable === false)) + stream.destroy(); } } } - function Tc(e) { - e.writable && !e.writableEnded && !e.destroyed && e.end(); + function endWritableNT(stream) { + if (stream.writable && !stream.writableEnded && !stream.destroyed) + stream.end(); } - b.from = function(e, t) { - return uc(b, e, t); + Readable.from = function(iterable, opts) { + return from2(Readable, iterable, opts); }; - var Xr; - function el() { - return Xr === undefined && (Xr = {}), Xr; + var webStreamsAdapters; + function lazyWebStreams() { + if (webStreamsAdapters === undefined) + webStreamsAdapters = {}; + return webStreamsAdapters; } - b.fromWeb = function(e, t) { - return el().newStreamReadableFromReadableStream(e, t); + Readable.fromWeb = function(readableStream, options) { + return lazyWebStreams().newStreamReadableFromReadableStream(readableStream, options); }; - b.toWeb = function(e, t) { - return el().newReadableStreamFromStreamReadable(e, t); + Readable.toWeb = function(streamReadable, options) { + return lazyWebStreams().newReadableStreamFromStreamReadable(streamReadable, options); }; - b.wrap = function(e, t) { - var r, n; - return new b({ objectMode: (r = (n = e.readableObjectMode) !== null && n !== undefined ? n : e.objectMode) !== null && r !== undefined ? r : true, ...t, destroy(i, o) { - ze.destroyer(e, i), o(i); - } }).wrap(e); + Readable.wrap = function(src, options) { + var _ref, _src$readableObjectMo; + return new Readable({ objectMode: (_ref = (_src$readableObjectMo = src.readableObjectMode) !== null && _src$readableObjectMo !== undefined ? _src$readableObjectMo : src.objectMode) !== null && _ref !== undefined ? _ref : true, ...options, destroy(err, callback) { + destroyImpl.destroyer(src, err), callback(err); + } }).wrap(src); }; }); - zt = S((Qh, hl) => { - var Fe = (we(), ye(H)), { ArrayPrototypeSlice: il, Error: Bc, FunctionPrototypeSymbolHasInstance: ol, ObjectDefineProperty: ll, ObjectDefineProperties: Lc, ObjectSetPrototypeOf: ul, StringPrototypeToLowerCase: Nc, Symbol: Fc, SymbolHasInstance: Mc } = I(); - hl.exports = T; - T.WritableState = dt; - var { EventEmitter: Cc } = it(), ft = Dt().Stream, { Buffer: Gt } = le(), Kt = Be(), { addAbortSignal: kc } = ot(), { getHighWaterMark: Dc, getDefaultHighWaterMark: Oc } = lt(), { ERR_INVALID_ARG_TYPE: Pc, ERR_METHOD_NOT_IMPLEMENTED: qc, ERR_MULTIPLE_CALLBACK: sl, ERR_STREAM_CANNOT_PIPE: vc, ERR_STREAM_DESTROYED: ct, ERR_STREAM_ALREADY_FINISHED: Uc, ERR_STREAM_NULL_VALUES: Wc, ERR_STREAM_WRITE_AFTER_END: $c, ERR_UNKNOWN_ENCODING: al } = O().codes, { errorOrDestroy: Xe } = Kt; - ul(T.prototype, ft.prototype); - ul(T, ft); - function rn() { - } - var Je = Fc("kOnFinished"); - function dt(e, t, r) { - typeof r != "boolean" && (r = t instanceof re()), this.objectMode = !!(e && e.objectMode), r && (this.objectMode = this.objectMode || !!(e && e.writableObjectMode)), this.highWaterMark = e ? Dc(this, e, "writableHighWaterMark", r) : Oc(false), this.finalCalled = false, this.needDrain = false, this.ending = false, this.ended = false, this.finished = false, this.destroyed = false; - let n = !!(e && e.decodeStrings === false); - this.decodeStrings = !n, this.defaultEncoding = e && e.defaultEncoding || "utf8", this.length = 0, this.writing = false, this.corked = 0, this.sync = true, this.bufferProcessing = false, this.onwrite = Hc.bind(undefined, t), this.writecb = null, this.writelen = 0, this.afterWriteTickInfo = null, Yt(this), this.pendingcb = 0, this.constructed = true, this.prefinished = false, this.errorEmitted = false, this.emitClose = !e || e.emitClose !== false, this.autoDestroy = !e || e.autoDestroy !== false, this.errored = null, this.closed = false, this.closeEmitted = false, this[Je] = []; - } - function Yt(e) { - e.buffered = [], e.bufferedIndex = 0, e.allBuffers = true, e.allNoop = true; - } - dt.prototype.getBuffer = function() { - return il(this.buffered, this.bufferedIndex); - }; - ll(dt.prototype, "bufferedRequestCount", { __proto__: null, get() { + var require_writable = __commonJS2((exports2, module2) => { + var process2 = require_process(), { ArrayPrototypeSlice: ArrayPrototypeSlice2, Error: Error2, FunctionPrototypeSymbolHasInstance, ObjectDefineProperty, ObjectDefineProperties, ObjectSetPrototypeOf, StringPrototypeToLowerCase, Symbol: Symbol2, SymbolHasInstance } = require_primordials(); + module2.exports = Writable; + Writable.WritableState = WritableState; + var { EventEmitter: EE } = (init_events(), __toCommonJS(exports_events)), Stream = require_legacy().Stream, { Buffer: Buffer3 } = (init_buffer(), __toCommonJS(exports_buffer)), destroyImpl = require_destroy(), { addAbortSignal } = require_add_abort_signal(), { getHighWaterMark, getDefaultHighWaterMark } = require_state(), { ERR_INVALID_ARG_TYPE: ERR_INVALID_ARG_TYPE3, ERR_METHOD_NOT_IMPLEMENTED, ERR_MULTIPLE_CALLBACK, ERR_STREAM_CANNOT_PIPE, ERR_STREAM_DESTROYED, ERR_STREAM_ALREADY_FINISHED, ERR_STREAM_NULL_VALUES, ERR_STREAM_WRITE_AFTER_END, ERR_UNKNOWN_ENCODING } = require_errors().codes, { errorOrDestroy } = destroyImpl; + ObjectSetPrototypeOf(Writable.prototype, Stream.prototype); + ObjectSetPrototypeOf(Writable, Stream); + function nop() {} + var kOnFinished = Symbol2("kOnFinished"); + function WritableState(options, stream, isDuplex) { + if (typeof isDuplex !== "boolean") + isDuplex = stream instanceof require_duplex(); + if (this.objectMode = !!(options && options.objectMode), isDuplex) + this.objectMode = this.objectMode || !!(options && options.writableObjectMode); + this.highWaterMark = options ? getHighWaterMark(this, options, "writableHighWaterMark", isDuplex) : getDefaultHighWaterMark(false), this.finalCalled = false, this.needDrain = false, this.ending = false, this.ended = false, this.finished = false, this.destroyed = false; + let noDecode = !!(options && options.decodeStrings === false); + this.decodeStrings = !noDecode, this.defaultEncoding = options && options.defaultEncoding || "utf8", this.length = 0, this.writing = false, this.corked = 0, this.sync = true, this.bufferProcessing = false, this.onwrite = onwrite.bind(undefined, stream), this.writecb = null, this.writelen = 0, this.afterWriteTickInfo = null, resetBuffer(this), this.pendingcb = 0, this.constructed = true, this.prefinished = false, this.errorEmitted = false, this.emitClose = !options || options.emitClose !== false, this.autoDestroy = !options || options.autoDestroy !== false, this.errored = null, this.closed = false, this.closeEmitted = false, this[kOnFinished] = []; + } + function resetBuffer(state) { + state.buffered = [], state.bufferedIndex = 0, state.allBuffers = true, state.allNoop = true; + } + WritableState.prototype.getBuffer = function getBuffer() { + return ArrayPrototypeSlice2(this.buffered, this.bufferedIndex); + }; + ObjectDefineProperty(WritableState.prototype, "bufferedRequestCount", { __proto__: null, get() { return this.buffered.length - this.bufferedIndex; } }); - function T(e) { - let t = this instanceof re(); - if (!t && !ol(T, this)) - return new T(e); - this._writableState = new dt(e, this, t), e && (typeof e.write == "function" && (this._write = e.write), typeof e.writev == "function" && (this._writev = e.writev), typeof e.destroy == "function" && (this._destroy = e.destroy), typeof e.final == "function" && (this._final = e.final), typeof e.construct == "function" && (this._construct = e.construct), e.signal && kc(e.signal, this)), ft.call(this, e), Kt.construct(this, () => { - let r = this._writableState; - r.writing || on(this, r), ln(this, r); + function Writable(options) { + let isDuplex = this instanceof require_duplex(); + if (!isDuplex && !FunctionPrototypeSymbolHasInstance(Writable, this)) + return new Writable(options); + if (this._writableState = new WritableState(options, this, isDuplex), options) { + if (typeof options.write === "function") + this._write = options.write; + if (typeof options.writev === "function") + this._writev = options.writev; + if (typeof options.destroy === "function") + this._destroy = options.destroy; + if (typeof options.final === "function") + this._final = options.final; + if (typeof options.construct === "function") + this._construct = options.construct; + if (options.signal) + addAbortSignal(options.signal, this); + } + Stream.call(this, options), destroyImpl.construct(this, () => { + let state = this._writableState; + if (!state.writing) + clearBuffer(this, state); + finishMaybe(this, state); }); } - ll(T, Mc, { __proto__: null, value: function(e) { - return ol(this, e) ? true : this !== T ? false : e && e._writableState instanceof dt; + ObjectDefineProperty(Writable, SymbolHasInstance, { __proto__: null, value: function(object) { + if (FunctionPrototypeSymbolHasInstance(this, object)) + return true; + if (this !== Writable) + return false; + return object && object._writableState instanceof WritableState; } }); - T.prototype.pipe = function() { - Xe(this, new vc); + Writable.prototype.pipe = function() { + errorOrDestroy(this, new ERR_STREAM_CANNOT_PIPE); }; - function fl(e, t, r, n) { - let i = e._writableState; - if (typeof r == "function") - n = r, r = i.defaultEncoding; + function _write(stream, chunk, encoding, cb) { + let state = stream._writableState; + if (typeof encoding === "function") + cb = encoding, encoding = state.defaultEncoding; else { - if (!r) - r = i.defaultEncoding; - else if (r !== "buffer" && !Gt.isEncoding(r)) - throw new al(r); - typeof n != "function" && (n = rn); - } - if (t === null) - throw new Wc; - if (!i.objectMode) - if (typeof t == "string") - i.decodeStrings !== false && (t = Gt.from(t, r), r = "buffer"); - else if (t instanceof Gt) - r = "buffer"; - else if (ft._isUint8Array(t)) - t = ft._uint8ArrayToBuffer(t), r = "buffer"; + if (!encoding) + encoding = state.defaultEncoding; + else if (encoding !== "buffer" && !Buffer3.isEncoding(encoding)) + throw new ERR_UNKNOWN_ENCODING(encoding); + if (typeof cb !== "function") + cb = nop; + } + if (chunk === null) + throw new ERR_STREAM_NULL_VALUES; + else if (!state.objectMode) + if (typeof chunk === "string") { + if (state.decodeStrings !== false) + chunk = Buffer3.from(chunk, encoding), encoding = "buffer"; + } else if (chunk instanceof Buffer3) + encoding = "buffer"; + else if (Stream._isUint8Array(chunk)) + chunk = Stream._uint8ArrayToBuffer(chunk), encoding = "buffer"; else - throw new Pc("chunk", ["string", "Buffer", "Uint8Array"], t); - let o; - return i.ending ? o = new $c : i.destroyed && (o = new ct("write")), o ? (Fe.nextTick(n, o), Xe(e, o, true), o) : (i.pendingcb++, jc(e, i, t, r, n)); - } - T.prototype.write = function(e, t, r) { - return fl(this, e, t, r) === true; - }; - T.prototype.cork = function() { + throw new ERR_INVALID_ARG_TYPE3("chunk", ["string", "Buffer", "Uint8Array"], chunk); + let err; + if (state.ending) + err = new ERR_STREAM_WRITE_AFTER_END; + else if (state.destroyed) + err = new ERR_STREAM_DESTROYED("write"); + if (err) + return process2.nextTick(cb, err), errorOrDestroy(stream, err, true), err; + return state.pendingcb++, writeOrBuffer(stream, state, chunk, encoding, cb); + } + Writable.prototype.write = function(chunk, encoding, cb) { + return _write(this, chunk, encoding, cb) === true; + }; + Writable.prototype.cork = function() { this._writableState.corked++; }; - T.prototype.uncork = function() { - let e = this._writableState; - e.corked && (e.corked--, e.writing || on(this, e)); - }; - T.prototype.setDefaultEncoding = function(t) { - if (typeof t == "string" && (t = Nc(t)), !Gt.isEncoding(t)) - throw new al(t); - return this._writableState.defaultEncoding = t, this; - }; - function jc(e, t, r, n, i) { - let o = t.objectMode ? 1 : r.length; - t.length += o; - let l = t.length < t.highWaterMark; - return l || (t.needDrain = true), t.writing || t.corked || t.errored || !t.constructed ? (t.buffered.push({ chunk: r, encoding: n, callback: i }), t.allBuffers && n !== "buffer" && (t.allBuffers = false), t.allNoop && i !== rn && (t.allNoop = false)) : (t.writelen = o, t.writecb = i, t.writing = true, t.sync = true, e._write(r, n, t.onwrite), t.sync = false), l && !t.errored && !t.destroyed; - } - function rl(e, t, r, n, i, o, l) { - t.writelen = n, t.writecb = l, t.writing = true, t.sync = true, t.destroyed ? t.onwrite(new ct("write")) : r ? e._writev(i, t.onwrite) : e._write(i, o, t.onwrite), t.sync = false; - } - function nl(e, t, r, n) { - --t.pendingcb, n(r), nn(t), Xe(e, r); - } - function Hc(e, t) { - let r = e._writableState, n = r.sync, i = r.writecb; - if (typeof i != "function") { - Xe(e, new sl); + Writable.prototype.uncork = function() { + let state = this._writableState; + if (state.corked) { + if (state.corked--, !state.writing) + clearBuffer(this, state); + } + }; + Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) { + if (typeof encoding === "string") + encoding = StringPrototypeToLowerCase(encoding); + if (!Buffer3.isEncoding(encoding)) + throw new ERR_UNKNOWN_ENCODING(encoding); + return this._writableState.defaultEncoding = encoding, this; + }; + function writeOrBuffer(stream, state, chunk, encoding, callback) { + let len2 = state.objectMode ? 1 : chunk.length; + state.length += len2; + let ret = state.length < state.highWaterMark; + if (!ret) + state.needDrain = true; + if (state.writing || state.corked || state.errored || !state.constructed) { + if (state.buffered.push({ chunk, encoding, callback }), state.allBuffers && encoding !== "buffer") + state.allBuffers = false; + if (state.allNoop && callback !== nop) + state.allNoop = false; + } else + state.writelen = len2, state.writecb = callback, state.writing = true, state.sync = true, stream._write(chunk, encoding, state.onwrite), state.sync = false; + return ret && !state.errored && !state.destroyed; + } + function doWrite(stream, state, writev, len2, chunk, encoding, cb) { + if (state.writelen = len2, state.writecb = cb, state.writing = true, state.sync = true, state.destroyed) + state.onwrite(new ERR_STREAM_DESTROYED("write")); + else if (writev) + stream._writev(chunk, state.onwrite); + else + stream._write(chunk, encoding, state.onwrite); + state.sync = false; + } + function onwriteError(stream, state, er, cb) { + --state.pendingcb, cb(er), errorBuffer(state), errorOrDestroy(stream, er); + } + function onwrite(stream, er) { + let state = stream._writableState, sync = state.sync, cb = state.writecb; + if (typeof cb !== "function") { + errorOrDestroy(stream, new ERR_MULTIPLE_CALLBACK); return; } - r.writing = false, r.writecb = null, r.length -= r.writelen, r.writelen = 0, t ? (t.stack, r.errored || (r.errored = t), e._readableState && !e._readableState.errored && (e._readableState.errored = t), n ? Fe.nextTick(nl, e, r, t, i) : nl(e, r, t, i)) : (r.buffered.length > r.bufferedIndex && on(e, r), n ? r.afterWriteTickInfo !== null && r.afterWriteTickInfo.cb === i ? r.afterWriteTickInfo.count++ : (r.afterWriteTickInfo = { count: 1, cb: i, stream: e, state: r }, Fe.nextTick(Gc, r.afterWriteTickInfo)) : cl(e, r, 1, i)); + if (state.writing = false, state.writecb = null, state.length -= state.writelen, state.writelen = 0, er) { + if (er.stack, !state.errored) + state.errored = er; + if (stream._readableState && !stream._readableState.errored) + stream._readableState.errored = er; + if (sync) + process2.nextTick(onwriteError, stream, state, er, cb); + else + onwriteError(stream, state, er, cb); + } else { + if (state.buffered.length > state.bufferedIndex) + clearBuffer(stream, state); + if (sync) + if (state.afterWriteTickInfo !== null && state.afterWriteTickInfo.cb === cb) + state.afterWriteTickInfo.count++; + else + state.afterWriteTickInfo = { count: 1, cb, stream, state }, process2.nextTick(afterWriteTick, state.afterWriteTickInfo); + else + afterWrite(stream, state, 1, cb); + } } - function Gc({ stream: e, state: t, count: r, cb: n }) { - return t.afterWriteTickInfo = null, cl(e, t, r, n); + function afterWriteTick({ stream, state, count, cb }) { + return state.afterWriteTickInfo = null, afterWrite(stream, state, count, cb); } - function cl(e, t, r, n) { - for (!t.ending && !e.destroyed && t.length === 0 && t.needDrain && (t.needDrain = false, e.emit("drain"));r-- > 0; ) - t.pendingcb--, n(); - t.destroyed && nn(t), ln(e, t); + function afterWrite(stream, state, count, cb) { + if (!state.ending && !stream.destroyed && state.length === 0 && state.needDrain) + state.needDrain = false, stream.emit("drain"); + while (count-- > 0) + state.pendingcb--, cb(); + if (state.destroyed) + errorBuffer(state); + finishMaybe(stream, state); } - function nn(e) { - if (e.writing) + function errorBuffer(state) { + if (state.writing) return; - for (let i = e.bufferedIndex;i < e.buffered.length; ++i) { - var t; - let { chunk: o, callback: l } = e.buffered[i], u = e.objectMode ? 1 : o.length; - e.length -= u, l((t = e.errored) !== null && t !== undefined ? t : new ct("write")); + for (let n = state.bufferedIndex;n < state.buffered.length; ++n) { + var _state$errored; + let { chunk, callback } = state.buffered[n], len2 = state.objectMode ? 1 : chunk.length; + state.length -= len2, callback((_state$errored = state.errored) !== null && _state$errored !== undefined ? _state$errored : new ERR_STREAM_DESTROYED("write")); } - let r = e[Je].splice(0); - for (let i = 0;i < r.length; i++) { - var n; - r[i]((n = e.errored) !== null && n !== undefined ? n : new ct("end")); + let onfinishCallbacks = state[kOnFinished].splice(0); + for (let i2 = 0;i2 < onfinishCallbacks.length; i2++) { + var _state$errored2; + onfinishCallbacks[i2]((_state$errored2 = state.errored) !== null && _state$errored2 !== undefined ? _state$errored2 : new ERR_STREAM_DESTROYED("end")); } - Yt(e); + resetBuffer(state); } - function on(e, t) { - if (t.corked || t.bufferProcessing || t.destroyed || !t.constructed) + function clearBuffer(stream, state) { + if (state.corked || state.bufferProcessing || state.destroyed || !state.constructed) return; - let { buffered: r, bufferedIndex: n, objectMode: i } = t, o = r.length - n; - if (!o) + let { buffered, bufferedIndex, objectMode } = state, bufferedLength = buffered.length - bufferedIndex; + if (!bufferedLength) return; - let l = n; - if (t.bufferProcessing = true, o > 1 && e._writev) { - t.pendingcb -= o - 1; - let u = t.allNoop ? rn : (s) => { - for (let c = l;c < r.length; ++c) - r[c].callback(s); - }, a = t.allNoop && l === 0 ? r : il(r, l); - a.allBuffers = t.allBuffers, rl(e, t, true, t.length, a, "", u), Yt(t); + let i2 = bufferedIndex; + if (state.bufferProcessing = true, bufferedLength > 1 && stream._writev) { + state.pendingcb -= bufferedLength - 1; + let callback = state.allNoop ? nop : (err) => { + for (let n = i2;n < buffered.length; ++n) + buffered[n].callback(err); + }, chunks = state.allNoop && i2 === 0 ? buffered : ArrayPrototypeSlice2(buffered, i2); + chunks.allBuffers = state.allBuffers, doWrite(stream, state, true, state.length, chunks, "", callback), resetBuffer(state); } else { do { - let { chunk: u, encoding: a, callback: s } = r[l]; - r[l++] = null; - let c = i ? 1 : u.length; - rl(e, t, false, c, u, a, s); - } while (l < r.length && !t.writing); - l === r.length ? Yt(t) : l > 256 ? (r.splice(0, l), t.bufferedIndex = 0) : t.bufferedIndex = l; + let { chunk, encoding, callback } = buffered[i2]; + buffered[i2++] = null; + let len2 = objectMode ? 1 : chunk.length; + doWrite(stream, state, false, len2, chunk, encoding, callback); + } while (i2 < buffered.length && !state.writing); + if (i2 === buffered.length) + resetBuffer(state); + else if (i2 > 256) + buffered.splice(0, i2), state.bufferedIndex = 0; + else + state.bufferedIndex = i2; } - t.bufferProcessing = false; + state.bufferProcessing = false; } - T.prototype._write = function(e, t, r) { + Writable.prototype._write = function(chunk, encoding, cb) { if (this._writev) - this._writev([{ chunk: e, encoding: t }], r); + this._writev([{ chunk, encoding }], cb); else - throw new qc("_write()"); - }; - T.prototype._writev = null; - T.prototype.end = function(e, t, r) { - let n = this._writableState; - typeof e == "function" ? (r = e, e = null, t = null) : typeof t == "function" && (r = t, t = null); - let i; - if (e != null) { - let o = fl(this, e, t); - o instanceof Bc && (i = o); - } - return n.corked && (n.corked = 1, this.uncork()), i || (!n.errored && !n.ending ? (n.ending = true, ln(this, n, true), n.ended = true) : n.finished ? i = new Uc("end") : n.destroyed && (i = new ct("end"))), typeof r == "function" && (i || n.finished ? Fe.nextTick(r, i) : n[Je].push(r)), this; + throw new ERR_METHOD_NOT_IMPLEMENTED("_write()"); + }; + Writable.prototype._writev = null; + Writable.prototype.end = function(chunk, encoding, cb) { + let state = this._writableState; + if (typeof chunk === "function") + cb = chunk, chunk = null, encoding = null; + else if (typeof encoding === "function") + cb = encoding, encoding = null; + let err; + if (chunk !== null && chunk !== undefined) { + let ret = _write(this, chunk, encoding); + if (ret instanceof Error2) + err = ret; + } + if (state.corked) + state.corked = 1, this.uncork(); + if (err) + ; + else if (!state.errored && !state.ending) + state.ending = true, finishMaybe(this, state, true), state.ended = true; + else if (state.finished) + err = new ERR_STREAM_ALREADY_FINISHED("end"); + else if (state.destroyed) + err = new ERR_STREAM_DESTROYED("end"); + if (typeof cb === "function") + if (err || state.finished) + process2.nextTick(cb, err); + else + state[kOnFinished].push(cb); + return this; }; - function Vt(e) { - return e.ending && !e.destroyed && e.constructed && e.length === 0 && !e.errored && e.buffered.length === 0 && !e.finished && !e.writing && !e.errorEmitted && !e.closeEmitted; + function needFinish(state) { + return state.ending && !state.destroyed && state.constructed && state.length === 0 && !state.errored && state.buffered.length === 0 && !state.finished && !state.writing && !state.errorEmitted && !state.closeEmitted; } - function Vc(e, t) { - let r = false; - function n(i) { - if (r) { - Xe(e, i ?? sl()); + function callFinal(stream, state) { + let called = false; + function onFinish(err) { + if (called) { + errorOrDestroy(stream, err !== null && err !== undefined ? err : ERR_MULTIPLE_CALLBACK()); return; } - if (r = true, t.pendingcb--, i) { - let o = t[Je].splice(0); - for (let l = 0;l < o.length; l++) - o[l](i); - Xe(e, i, t.sync); - } else - Vt(t) && (t.prefinished = true, e.emit("prefinish"), t.pendingcb++, Fe.nextTick(tn, e, t)); + if (called = true, state.pendingcb--, err) { + let onfinishCallbacks = state[kOnFinished].splice(0); + for (let i2 = 0;i2 < onfinishCallbacks.length; i2++) + onfinishCallbacks[i2](err); + errorOrDestroy(stream, err, state.sync); + } else if (needFinish(state)) + state.prefinished = true, stream.emit("prefinish"), state.pendingcb++, process2.nextTick(finish, stream, state); } - t.sync = true, t.pendingcb++; + state.sync = true, state.pendingcb++; try { - e._final(n); - } catch (i) { - n(i); + stream._final(onFinish); + } catch (err) { + onFinish(err); } - t.sync = false; + state.sync = false; } - function Yc(e, t) { - !t.prefinished && !t.finalCalled && (typeof e._final == "function" && !t.destroyed ? (t.finalCalled = true, Vc(e, t)) : (t.prefinished = true, e.emit("prefinish"))); - } - function ln(e, t, r) { - Vt(t) && (Yc(e, t), t.pendingcb === 0 && (r ? (t.pendingcb++, Fe.nextTick((n, i) => { - Vt(i) ? tn(n, i) : i.pendingcb--; - }, e, t)) : Vt(t) && (t.pendingcb++, tn(e, t)))); - } - function tn(e, t) { - t.pendingcb--, t.finished = true; - let r = t[Je].splice(0); - for (let n = 0;n < r.length; n++) - r[n](); - if (e.emit("finish"), t.autoDestroy) { - let n = e._readableState; - (!n || n.autoDestroy && (n.endEmitted || n.readable === false)) && e.destroy(); - } - } - Lc(T.prototype, { closed: { __proto__: null, get() { + function prefinish(stream, state) { + if (!state.prefinished && !state.finalCalled) + if (typeof stream._final === "function" && !state.destroyed) + state.finalCalled = true, callFinal(stream, state); + else + state.prefinished = true, stream.emit("prefinish"); + } + function finishMaybe(stream, state, sync) { + if (needFinish(state)) { + if (prefinish(stream, state), state.pendingcb === 0) { + if (sync) + state.pendingcb++, process2.nextTick((stream2, state2) => { + if (needFinish(state2)) + finish(stream2, state2); + else + state2.pendingcb--; + }, stream, state); + else if (needFinish(state)) + state.pendingcb++, finish(stream, state); + } + } + } + function finish(stream, state) { + state.pendingcb--, state.finished = true; + let onfinishCallbacks = state[kOnFinished].splice(0); + for (let i2 = 0;i2 < onfinishCallbacks.length; i2++) + onfinishCallbacks[i2](); + if (stream.emit("finish"), state.autoDestroy) { + let rState = stream._readableState; + if (!rState || rState.autoDestroy && (rState.endEmitted || rState.readable === false)) + stream.destroy(); + } + } + ObjectDefineProperties(Writable.prototype, { closed: { __proto__: null, get() { return this._writableState ? this._writableState.closed : false; } }, destroyed: { __proto__: null, get() { return this._writableState ? this._writableState.destroyed : false; - }, set(e) { - this._writableState && (this._writableState.destroyed = e); + }, set(value) { + if (this._writableState) + this._writableState.destroyed = value; } }, writable: { __proto__: null, get() { - let e = this._writableState; - return !!e && e.writable !== false && !e.destroyed && !e.errored && !e.ending && !e.ended; - }, set(e) { - this._writableState && (this._writableState.writable = !!e); + let w = this._writableState; + return !!w && w.writable !== false && !w.destroyed && !w.errored && !w.ending && !w.ended; + }, set(val) { + if (this._writableState) + this._writableState.writable = !!val; } }, writableFinished: { __proto__: null, get() { return this._writableState ? this._writableState.finished : false; } }, writableObjectMode: { __proto__: null, get() { @@ -3269,8 +4629,10 @@ var init_stream = __esm(() => { } }, writableEnded: { __proto__: null, get() { return this._writableState ? this._writableState.ending : false; } }, writableNeedDrain: { __proto__: null, get() { - let e = this._writableState; - return e ? !e.destroyed && !e.ending && e.needDrain : false; + let wState = this._writableState; + if (!wState) + return false; + return !wState.destroyed && !wState.ending && wState.needDrain; } }, writableHighWaterMark: { __proto__: null, get() { return this._writableState && this._writableState.highWaterMark; } }, writableCorked: { __proto__: null, get() { @@ -3282,551 +4644,675 @@ var init_stream = __esm(() => { } }, writableAborted: { __proto__: null, enumerable: false, get: function() { return !!(this._writableState.writable !== false && (this._writableState.destroyed || this._writableState.errored) && !this._writableState.finished); } } }); - var Kc = Kt.destroy; - T.prototype.destroy = function(e, t) { - let r = this._writableState; - return !r.destroyed && (r.bufferedIndex < r.buffered.length || r[Je].length) && Fe.nextTick(nn, r), Kc.call(this, e, t), this; + var destroy = destroyImpl.destroy; + Writable.prototype.destroy = function(err, cb) { + let state = this._writableState; + if (!state.destroyed && (state.bufferedIndex < state.buffered.length || state[kOnFinished].length)) + process2.nextTick(errorBuffer, state); + return destroy.call(this, err, cb), this; }; - T.prototype._undestroy = Kt.undestroy; - T.prototype._destroy = function(e, t) { - t(e); + Writable.prototype._undestroy = destroyImpl.undestroy; + Writable.prototype._destroy = function(err, cb) { + cb(err); }; - T.prototype[Cc.captureRejectionSymbol] = function(e) { - this.destroy(e); + Writable.prototype[EE.captureRejectionSymbol] = function(err) { + this.destroy(err); }; - var en; - function dl() { - return en === undefined && (en = {}), en; + var webStreamsAdapters; + function lazyWebStreams() { + if (webStreamsAdapters === undefined) + webStreamsAdapters = {}; + return webStreamsAdapters; } - T.fromWeb = function(e, t) { - return dl().newStreamWritableFromWritableStream(e, t); + Writable.fromWeb = function(writableStream, options) { + return lazyWebStreams().newStreamWritableFromWritableStream(writableStream, options); }; - T.toWeb = function(e) { - return dl().newWritableStreamFromStreamWritable(e); + Writable.toWeb = function(streamWritable) { + return lazyWebStreams().newWritableStreamFromStreamWritable(streamWritable); }; }); - Bl = S((Zh, Tl) => { - var un = (we(), ye(H)), zc = le(), { isReadable: Xc, isWritable: Jc, isIterable: pl, isNodeStream: Qc, isReadableNodeStream: yl, isWritableNodeStream: bl, isDuplexNodeStream: Zc, isReadableStream: wl, isWritableStream: gl } = Z(), _l = ae(), { AbortError: xl, codes: { ERR_INVALID_ARG_TYPE: ed, ERR_INVALID_RETURN_VALUE: El } } = O(), { destroyer: Ze } = Be(), td = re(), Il = at(), rd = zt(), { createDeferredPromise: Sl } = q(), ml = Gr(), Rl = globalThis.Blob || zc.Blob, nd = typeof Rl < "u" ? function(t) { - return t instanceof Rl; - } : function(t) { + var require_duplexify = __commonJS2((exports2, module2) => { + var process2 = require_process(), bufferModule = (init_buffer(), __toCommonJS(exports_buffer)), { isReadable, isWritable, isIterable, isNodeStream, isReadableNodeStream, isWritableNodeStream, isDuplexNodeStream, isReadableStream, isWritableStream } = require_utils(), eos = require_end_of_stream(), { AbortError: AbortError2, codes: { ERR_INVALID_ARG_TYPE: ERR_INVALID_ARG_TYPE3, ERR_INVALID_RETURN_VALUE } } = require_errors(), { destroyer } = require_destroy(), Duplex = require_duplex(), Readable = require_readable(), Writable = require_writable(), { createDeferredPromise } = require_util(), from2 = require_from(), Blob3 = globalThis.Blob || bufferModule.Blob, isBlob = typeof Blob3 !== "undefined" ? function isBlob(b) { + return b instanceof Blob3; + } : function isBlob(b) { return false; - }, id = globalThis.AbortController || We().AbortController, { FunctionPrototypeCall: Al } = I(), Ee = class extends td { - constructor(t) { - super(t), t?.readable === false && (this._readableState.readable = false, this._readableState.ended = true, this._readableState.endEmitted = true), t?.writable === false && (this._writableState.writable = false, this._writableState.ending = true, this._writableState.ended = true, this._writableState.finished = true); - } - }; - Tl.exports = function e(t, r) { - if (Zc(t)) - return t; - if (yl(t)) - return Qe({ readable: t }); - if (bl(t)) - return Qe({ writable: t }); - if (Qc(t)) - return Qe({ writable: false, readable: false }); - if (wl(t)) - return Qe({ readable: Il.fromWeb(t) }); - if (gl(t)) - return Qe({ writable: rd.fromWeb(t) }); - if (typeof t == "function") { - let { value: i, write: o, final: l, destroy: u } = od(t); - if (pl(i)) - return ml(Ee, i, { objectMode: true, write: o, final: l, destroy: u }); - let a = i?.then; - if (typeof a == "function") { - let s, c = Al(a, i, (d) => { - if (d != null) - throw new El("nully", "body", d); - }, (d) => { - Ze(s, d); + }, AbortController = globalThis.AbortController || require_abort_controller().AbortController, { FunctionPrototypeCall } = require_primordials(); + + class Duplexify extends Duplex { + constructor(options) { + super(options); + if ((options === null || options === undefined ? undefined : options.readable) === false) + this._readableState.readable = false, this._readableState.ended = true, this._readableState.endEmitted = true; + if ((options === null || options === undefined ? undefined : options.writable) === false) + this._writableState.writable = false, this._writableState.ending = true, this._writableState.ended = true, this._writableState.finished = true; + } + } + module2.exports = function duplexify(body, name) { + if (isDuplexNodeStream(body)) + return body; + if (isReadableNodeStream(body)) + return _duplexify({ readable: body }); + if (isWritableNodeStream(body)) + return _duplexify({ writable: body }); + if (isNodeStream(body)) + return _duplexify({ writable: false, readable: false }); + if (isReadableStream(body)) + return _duplexify({ readable: Readable.fromWeb(body) }); + if (isWritableStream(body)) + return _duplexify({ writable: Writable.fromWeb(body) }); + if (typeof body === "function") { + let { value, write: write3, final, destroy } = fromAsyncGen(body); + if (isIterable(value)) + return from2(Duplexify, value, { objectMode: true, write: write3, final, destroy }); + let then2 = value === null || value === undefined ? undefined : value.then; + if (typeof then2 === "function") { + let d, promise = FunctionPrototypeCall(then2, value, (val) => { + if (val != null) + throw new ERR_INVALID_RETURN_VALUE("nully", "body", val); + }, (err) => { + destroyer(d, err); }); - return s = new Ee({ objectMode: true, readable: false, write: o, final(d) { - l(async () => { + return d = new Duplexify({ objectMode: true, readable: false, write: write3, final(cb) { + final(async () => { try { - await c, un.nextTick(d, null); - } catch (y) { - un.nextTick(d, y); + await promise, process2.nextTick(cb, null); + } catch (err) { + process2.nextTick(cb, err); } }); - }, destroy: u }); - } - throw new El("Iterable, AsyncIterable or AsyncFunction", r, i); - } - if (nd(t)) - return e(t.arrayBuffer()); - if (pl(t)) - return ml(Ee, t, { objectMode: true, writable: false }); - if (wl(t?.readable) && gl(t?.writable)) - return Ee.fromWeb(t); - if (typeof t?.writable == "object" || typeof t?.readable == "object") { - let i = t != null && t.readable ? yl(t?.readable) ? t?.readable : e(t.readable) : undefined, o = t != null && t.writable ? bl(t?.writable) ? t?.writable : e(t.writable) : undefined; - return Qe({ readable: i, writable: o }); - } - let n = t?.then; - if (typeof n == "function") { - let i; - return Al(n, t, (o) => { - o != null && i.push(o), i.push(null); - }, (o) => { - Ze(i, o); - }), i = new Ee({ objectMode: true, writable: false, read() { - } }); - } - throw new ed(r, ["Blob", "ReadableStream", "WritableStream", "Stream", "Iterable", "AsyncIterable", "Function", "{ readable, writable } pair", "Promise"], t); - }; - function od(e) { - let { promise: t, resolve: r } = Sl(), n = new id, i = n.signal; - return { value: e(async function* () { - for (;; ) { - let l = t; - t = null; - let { chunk: u, done: a, cb: s } = await l; - if (un.nextTick(s), a) + }, destroy }); + } + throw new ERR_INVALID_RETURN_VALUE("Iterable, AsyncIterable or AsyncFunction", name, value); + } + if (isBlob(body)) + return duplexify(body.arrayBuffer()); + if (isIterable(body)) + return from2(Duplexify, body, { objectMode: true, writable: false }); + if (isReadableStream(body === null || body === undefined ? undefined : body.readable) && isWritableStream(body === null || body === undefined ? undefined : body.writable)) + return Duplexify.fromWeb(body); + if (typeof (body === null || body === undefined ? undefined : body.writable) === "object" || typeof (body === null || body === undefined ? undefined : body.readable) === "object") { + let readable = body !== null && body !== undefined && body.readable ? isReadableNodeStream(body === null || body === undefined ? undefined : body.readable) ? body === null || body === undefined ? undefined : body.readable : duplexify(body.readable) : undefined, writable = body !== null && body !== undefined && body.writable ? isWritableNodeStream(body === null || body === undefined ? undefined : body.writable) ? body === null || body === undefined ? undefined : body.writable : duplexify(body.writable) : undefined; + return _duplexify({ readable, writable }); + } + let then = body === null || body === undefined ? undefined : body.then; + if (typeof then === "function") { + let d; + return FunctionPrototypeCall(then, body, (val) => { + if (val != null) + d.push(val); + d.push(null); + }, (err) => { + destroyer(d, err); + }), d = new Duplexify({ objectMode: true, writable: false, read() {} }); + } + throw new ERR_INVALID_ARG_TYPE3(name, ["Blob", "ReadableStream", "WritableStream", "Stream", "Iterable", "AsyncIterable", "Function", "{ readable, writable } pair", "Promise"], body); + }; + function fromAsyncGen(fn) { + let { promise, resolve } = createDeferredPromise(), ac = new AbortController, signal = ac.signal; + return { value: fn(async function* () { + while (true) { + let _promise = promise; + promise = null; + let { chunk, done, cb } = await _promise; + if (process2.nextTick(cb), done) return; - if (i.aborted) - throw new xl(undefined, { cause: i.reason }); - ({ promise: t, resolve: r } = Sl()), yield u; - } - }(), { signal: i }), write(l, u, a) { - let s = r; - r = null, s({ chunk: l, done: false, cb: a }); - }, final(l) { - let u = r; - r = null, u({ done: true, cb: l }); - }, destroy(l, u) { - n.abort(), u(l); + if (signal.aborted) + throw new AbortError2(undefined, { cause: signal.reason }); + ({ promise, resolve } = createDeferredPromise()), yield chunk; + } + }(), { signal }), write(chunk, encoding, cb) { + let _resolve = resolve; + resolve = null, _resolve({ chunk, done: false, cb }); + }, final(cb) { + let _resolve = resolve; + resolve = null, _resolve({ done: true, cb }); + }, destroy(err, cb) { + ac.abort(), cb(err); } }; } - function Qe(e) { - let t = e.readable && typeof e.readable.read != "function" ? Il.wrap(e.readable) : e.readable, r = e.writable, n = !!Xc(t), i = !!Jc(r), o, l, u, a, s; - function c(d) { - let y = a; - a = null, y ? y(d) : d && s.destroy(d); - } - return s = new Ee({ readableObjectMode: !!(t != null && t.readableObjectMode), writableObjectMode: !!(r != null && r.writableObjectMode), readable: n, writable: i }), i && (_l(r, (d) => { - i = false, d && Ze(t, d), c(d); - }), s._write = function(d, y, h) { - r.write(d, y) ? h() : o = h; - }, s._final = function(d) { - r.end(), l = d; - }, r.on("drain", function() { - if (o) { - let d = o; - o = null, d(); - } - }), r.on("finish", function() { - if (l) { - let d = l; - l = null, d(); - } - })), n && (_l(t, (d) => { - n = false, d && Ze(t, d), c(d); - }), t.on("readable", function() { - if (u) { - let d = u; - u = null, d(); - } - }), t.on("end", function() { - s.push(null); - }), s._read = function() { - for (;; ) { - let d = t.read(); - if (d === null) { - u = s._read; - return; + function _duplexify(pair) { + let r = pair.readable && typeof pair.readable.read !== "function" ? Readable.wrap(pair.readable) : pair.readable, w = pair.writable, readable = !!isReadable(r), writable = !!isWritable(w), ondrain, onfinish, onreadable, onclose, d; + function onfinished(err) { + let cb = onclose; + if (onclose = null, cb) + cb(err); + else if (err) + d.destroy(err); + } + if (d = new Duplexify({ readableObjectMode: !!(r !== null && r !== undefined && r.readableObjectMode), writableObjectMode: !!(w !== null && w !== undefined && w.writableObjectMode), readable, writable }), writable) + eos(w, (err) => { + if (writable = false, err) + destroyer(r, err); + onfinished(err); + }), d._write = function(chunk, encoding, callback) { + if (w.write(chunk, encoding)) + callback(); + else + ondrain = callback; + }, d._final = function(callback) { + w.end(), onfinish = callback; + }, w.on("drain", function() { + if (ondrain) { + let cb = ondrain; + ondrain = null, cb(); } - if (!s.push(d)) - return; - } - }), s._destroy = function(d, y) { - !d && a !== null && (d = new xl), u = null, o = null, l = null, a === null ? y(d) : (a = y, Ze(r, d), Ze(t, d)); - }, s; + }), w.on("finish", function() { + if (onfinish) { + let cb = onfinish; + onfinish = null, cb(); + } + }); + if (readable) + eos(r, (err) => { + if (readable = false, err) + destroyer(r, err); + onfinished(err); + }), r.on("readable", function() { + if (onreadable) { + let cb = onreadable; + onreadable = null, cb(); + } + }), r.on("end", function() { + d.push(null); + }), d._read = function() { + while (true) { + let buf = r.read(); + if (buf === null) { + onreadable = d._read; + return; + } + if (!d.push(buf)) + return; + } + }; + return d._destroy = function(err, callback) { + if (!err && onclose !== null) + err = new AbortError2; + if (onreadable = null, ondrain = null, onfinish = null, onclose === null) + callback(err); + else + onclose = callback, destroyer(w, err), destroyer(r, err); + }, d; } }); - re = S((ep, Fl) => { - var { ObjectDefineProperties: ld, ObjectGetOwnPropertyDescriptor: fe, ObjectKeys: ud, ObjectSetPrototypeOf: Ll } = I(); - Fl.exports = Y; - var fn = at(), G = zt(); - Ll(Y.prototype, fn.prototype); - Ll(Y, fn); + var require_duplex = __commonJS2((exports2, module2) => { + var { ObjectDefineProperties, ObjectGetOwnPropertyDescriptor, ObjectKeys, ObjectSetPrototypeOf } = require_primordials(); + module2.exports = Duplex; + var Readable = require_readable(), Writable = require_writable(); + ObjectSetPrototypeOf(Duplex.prototype, Readable.prototype); + ObjectSetPrototypeOf(Duplex, Readable); { - let e = ud(G.prototype); - for (let t = 0;t < e.length; t++) { - let r = e[t]; - Y.prototype[r] || (Y.prototype[r] = G.prototype[r]); - } - } - function Y(e) { - if (!(this instanceof Y)) - return new Y(e); - fn.call(this, e), G.call(this, e), e ? (this.allowHalfOpen = e.allowHalfOpen !== false, e.readable === false && (this._readableState.readable = false, this._readableState.ended = true, this._readableState.endEmitted = true), e.writable === false && (this._writableState.writable = false, this._writableState.ending = true, this._writableState.ended = true, this._writableState.finished = true)) : this.allowHalfOpen = true; + let keys = ObjectKeys(Writable.prototype); + for (let i2 = 0;i2 < keys.length; i2++) { + let method = keys[i2]; + if (!Duplex.prototype[method]) + Duplex.prototype[method] = Writable.prototype[method]; + } + } + function Duplex(options) { + if (!(this instanceof Duplex)) + return new Duplex(options); + if (Readable.call(this, options), Writable.call(this, options), options) { + if (this.allowHalfOpen = options.allowHalfOpen !== false, options.readable === false) + this._readableState.readable = false, this._readableState.ended = true, this._readableState.endEmitted = true; + if (options.writable === false) + this._writableState.writable = false, this._writableState.ending = true, this._writableState.ended = true, this._writableState.finished = true; + } else + this.allowHalfOpen = true; } - ld(Y.prototype, { writable: { __proto__: null, ...fe(G.prototype, "writable") }, writableHighWaterMark: { __proto__: null, ...fe(G.prototype, "writableHighWaterMark") }, writableObjectMode: { __proto__: null, ...fe(G.prototype, "writableObjectMode") }, writableBuffer: { __proto__: null, ...fe(G.prototype, "writableBuffer") }, writableLength: { __proto__: null, ...fe(G.prototype, "writableLength") }, writableFinished: { __proto__: null, ...fe(G.prototype, "writableFinished") }, writableCorked: { __proto__: null, ...fe(G.prototype, "writableCorked") }, writableEnded: { __proto__: null, ...fe(G.prototype, "writableEnded") }, writableNeedDrain: { __proto__: null, ...fe(G.prototype, "writableNeedDrain") }, destroyed: { __proto__: null, get() { - return this._readableState === undefined || this._writableState === undefined ? false : this._readableState.destroyed && this._writableState.destroyed; - }, set(e) { - this._readableState && this._writableState && (this._readableState.destroyed = e, this._writableState.destroyed = e); + ObjectDefineProperties(Duplex.prototype, { writable: { __proto__: null, ...ObjectGetOwnPropertyDescriptor(Writable.prototype, "writable") }, writableHighWaterMark: { __proto__: null, ...ObjectGetOwnPropertyDescriptor(Writable.prototype, "writableHighWaterMark") }, writableObjectMode: { __proto__: null, ...ObjectGetOwnPropertyDescriptor(Writable.prototype, "writableObjectMode") }, writableBuffer: { __proto__: null, ...ObjectGetOwnPropertyDescriptor(Writable.prototype, "writableBuffer") }, writableLength: { __proto__: null, ...ObjectGetOwnPropertyDescriptor(Writable.prototype, "writableLength") }, writableFinished: { __proto__: null, ...ObjectGetOwnPropertyDescriptor(Writable.prototype, "writableFinished") }, writableCorked: { __proto__: null, ...ObjectGetOwnPropertyDescriptor(Writable.prototype, "writableCorked") }, writableEnded: { __proto__: null, ...ObjectGetOwnPropertyDescriptor(Writable.prototype, "writableEnded") }, writableNeedDrain: { __proto__: null, ...ObjectGetOwnPropertyDescriptor(Writable.prototype, "writableNeedDrain") }, destroyed: { __proto__: null, get() { + if (this._readableState === undefined || this._writableState === undefined) + return false; + return this._readableState.destroyed && this._writableState.destroyed; + }, set(value) { + if (this._readableState && this._writableState) + this._readableState.destroyed = value, this._writableState.destroyed = value; } } }); - var sn; - function Nl() { - return sn === undefined && (sn = {}), sn; + var webStreamsAdapters; + function lazyWebStreams() { + if (webStreamsAdapters === undefined) + webStreamsAdapters = {}; + return webStreamsAdapters; } - Y.fromWeb = function(e, t) { - return Nl().newStreamDuplexFromReadableWritablePair(e, t); + Duplex.fromWeb = function(pair, options) { + return lazyWebStreams().newStreamDuplexFromReadableWritablePair(pair, options); }; - Y.toWeb = function(e) { - return Nl().newReadableWritablePairFromDuplex(e); + Duplex.toWeb = function(duplex) { + return lazyWebStreams().newReadableWritablePairFromDuplex(duplex); }; - var an; - Y.from = function(e) { - return an || (an = Bl()), an(e, "body"); + var duplexify; + Duplex.from = function(body) { + if (!duplexify) + duplexify = require_duplexify(); + return duplexify(body, "body"); }; }); - hn = S((tp, Cl) => { - var { ObjectSetPrototypeOf: Ml, Symbol: sd } = I(); - Cl.exports = ce; - var { ERR_METHOD_NOT_IMPLEMENTED: ad } = O().codes, dn = re(), { getHighWaterMark: fd } = lt(); - Ml(ce.prototype, dn.prototype); - Ml(ce, dn); - var ht = sd("kCallback"); - function ce(e) { - if (!(this instanceof ce)) - return new ce(e); - let t = e ? fd(this, e, "readableHighWaterMark", true) : null; - t === 0 && (e = { ...e, highWaterMark: null, readableHighWaterMark: t, writableHighWaterMark: e.writableHighWaterMark || 0 }), dn.call(this, e), this._readableState.sync = false, this[ht] = null, e && (typeof e.transform == "function" && (this._transform = e.transform), typeof e.flush == "function" && (this._flush = e.flush)), this.on("prefinish", cd); - } - function cn(e) { - typeof this._flush == "function" && !this.destroyed ? this._flush((t, r) => { - if (t) { - e ? e(t) : this.destroy(t); - return; - } - r != null && this.push(r), this.push(null), e && e(); - }) : (this.push(null), e && e()); - } - function cd() { - this._final !== cn && cn.call(this); - } - ce.prototype._final = cn; - ce.prototype._transform = function(e, t, r) { - throw new ad("_transform()"); - }; - ce.prototype._write = function(e, t, r) { - let n = this._readableState, i = this._writableState, o = n.length; - this._transform(e, t, (l, u) => { - if (l) { - r(l); + var require_transform = __commonJS2((exports2, module2) => { + var { ObjectSetPrototypeOf, Symbol: Symbol2 } = require_primordials(); + module2.exports = Transform; + var { ERR_METHOD_NOT_IMPLEMENTED } = require_errors().codes, Duplex = require_duplex(), { getHighWaterMark } = require_state(); + ObjectSetPrototypeOf(Transform.prototype, Duplex.prototype); + ObjectSetPrototypeOf(Transform, Duplex); + var kCallback = Symbol2("kCallback"); + function Transform(options) { + if (!(this instanceof Transform)) + return new Transform(options); + let readableHighWaterMark = options ? getHighWaterMark(this, options, "readableHighWaterMark", true) : null; + if (readableHighWaterMark === 0) + options = { ...options, highWaterMark: null, readableHighWaterMark, writableHighWaterMark: options.writableHighWaterMark || 0 }; + if (Duplex.call(this, options), this._readableState.sync = false, this[kCallback] = null, options) { + if (typeof options.transform === "function") + this._transform = options.transform; + if (typeof options.flush === "function") + this._flush = options.flush; + } + this.on("prefinish", prefinish); + } + function final(cb) { + if (typeof this._flush === "function" && !this.destroyed) + this._flush((er, data) => { + if (er) { + if (cb) + cb(er); + else + this.destroy(er); + return; + } + if (data != null) + this.push(data); + if (this.push(null), cb) + cb(); + }); + else if (this.push(null), cb) + cb(); + } + function prefinish() { + if (this._final !== final) + final.call(this); + } + Transform.prototype._final = final; + Transform.prototype._transform = function(chunk, encoding, callback) { + throw new ERR_METHOD_NOT_IMPLEMENTED("_transform()"); + }; + Transform.prototype._write = function(chunk, encoding, callback) { + let rState = this._readableState, wState = this._writableState, length = rState.length; + this._transform(chunk, encoding, (err, val) => { + if (err) { + callback(err); return; } - u != null && this.push(u), i.ended || o === n.length || n.length < n.highWaterMark ? r() : this[ht] = r; + if (val != null) + this.push(val); + if (wState.ended || length === rState.length || rState.length < rState.highWaterMark) + callback(); + else + this[kCallback] = callback; }); }; - ce.prototype._read = function() { - if (this[ht]) { - let e = this[ht]; - this[ht] = null, e(); + Transform.prototype._read = function() { + if (this[kCallback]) { + let callback = this[kCallback]; + this[kCallback] = null, callback(); } }; }); - yn = S((rp, Dl) => { - var { ObjectSetPrototypeOf: kl } = I(); - Dl.exports = et; - var pn = hn(); - kl(et.prototype, pn.prototype); - kl(et, pn); - function et(e) { - if (!(this instanceof et)) - return new et(e); - pn.call(this, e); - } - et.prototype._transform = function(e, t, r) { - r(null, e); + var require_passthrough = __commonJS2((exports2, module2) => { + var { ObjectSetPrototypeOf } = require_primordials(); + module2.exports = PassThrough; + var Transform = require_transform(); + ObjectSetPrototypeOf(PassThrough.prototype, Transform.prototype); + ObjectSetPrototypeOf(PassThrough, Transform); + function PassThrough(options) { + if (!(this instanceof PassThrough)) + return new PassThrough(options); + Transform.call(this, options); + } + PassThrough.prototype._transform = function(chunk, encoding, cb) { + cb(null, chunk); }; }); - Zt = S((np, Ul) => { - var pt = (we(), ye(H)), { ArrayIsArray: dd, Promise: hd, SymbolAsyncIterator: pd, SymbolDispose: yd } = I(), Qt = ae(), { once: bd } = q(), wd = Be(), Ol = re(), { aggregateTwoErrors: gd, codes: { ERR_INVALID_ARG_TYPE: An, ERR_INVALID_RETURN_VALUE: bn, ERR_MISSING_ARGS: _d, ERR_STREAM_DESTROYED: Ed, ERR_STREAM_PREMATURE_CLOSE: Sd }, AbortError: md } = O(), { validateFunction: Rd, validateAbortSignal: Ad } = He(), { isIterable: Me, isReadable: wn, isReadableNodeStream: Jt, isNodeStream: Pl, isTransformStream: tt, isWebStream: xd, isReadableStream: gn, isReadableFinished: Id } = Z(), Td = globalThis.AbortController || We().AbortController, _n, En, Sn; - function ql(e, t, r) { - let n = false; - e.on("close", () => { - n = true; + var require_pipeline = __commonJS2((exports2, module2) => { + var process2 = require_process(), { ArrayIsArray, Promise: Promise2, SymbolAsyncIterator, SymbolDispose } = require_primordials(), eos = require_end_of_stream(), { once: once3 } = require_util(), destroyImpl = require_destroy(), Duplex = require_duplex(), { aggregateTwoErrors, codes: { ERR_INVALID_ARG_TYPE: ERR_INVALID_ARG_TYPE3, ERR_INVALID_RETURN_VALUE, ERR_MISSING_ARGS, ERR_STREAM_DESTROYED, ERR_STREAM_PREMATURE_CLOSE }, AbortError: AbortError2 } = require_errors(), { validateFunction, validateAbortSignal: validateAbortSignal2 } = require_validators(), { isIterable, isReadable, isReadableNodeStream, isNodeStream, isTransformStream, isWebStream, isReadableStream, isReadableFinished } = require_utils(), AbortController = globalThis.AbortController || require_abort_controller().AbortController, PassThrough, Readable, addAbortListener2; + function destroyer(stream, reading, writing) { + let finished = false; + stream.on("close", () => { + finished = true; }); - let i = Qt(e, { readable: t, writable: r }, (o) => { - n = !o; + let cleanup = eos(stream, { readable: reading, writable: writing }, (err) => { + finished = !err; }); - return { destroy: (o) => { - n || (n = true, wd.destroyer(e, o || new Ed("pipe"))); - }, cleanup: i }; - } - function Bd(e) { - return Rd(e[e.length - 1], "streams[stream.length - 1]"), e.pop(); - } - function mn(e) { - if (Me(e)) - return e; - if (Jt(e)) - return Ld(e); - throw new An("val", ["Readable", "Iterable", "AsyncIterable"], e); - } - async function* Ld(e) { - En || (En = at()), yield* En.prototype[pd].call(e); - } - async function Xt(e, t, r, { end: n }) { - let i, o = null, l = (s) => { - if (s && (i = s), o) { - let c = o; - o = null, c(); - } - }, u = () => new hd((s, c) => { - i ? c(i) : o = () => { - i ? c(i) : s(); - }; + return { destroy: (err) => { + if (finished) + return; + finished = true, destroyImpl.destroyer(stream, err || new ERR_STREAM_DESTROYED("pipe")); + }, cleanup }; + } + function popCallback(streams) { + return validateFunction(streams[streams.length - 1], "streams[stream.length - 1]"), streams.pop(); + } + function makeAsyncIterable(val) { + if (isIterable(val)) + return val; + else if (isReadableNodeStream(val)) + return fromReadable(val); + throw new ERR_INVALID_ARG_TYPE3("val", ["Readable", "Iterable", "AsyncIterable"], val); + } + async function* fromReadable(val) { + if (!Readable) + Readable = require_readable(); + yield* Readable.prototype[SymbolAsyncIterator].call(val); + } + async function pumpToNode(iterable, writable, finish, { end }) { + let error, onresolve = null, resume = (err) => { + if (err) + error = err; + if (onresolve) { + let callback = onresolve; + onresolve = null, callback(); + } + }, wait = () => new Promise2((resolve, reject) => { + if (error) + reject(error); + else + onresolve = () => { + if (error) + reject(error); + else + resolve(); + }; }); - t.on("drain", l); - let a = Qt(t, { readable: false }, l); + writable.on("drain", resume); + let cleanup = eos(writable, { readable: false }, resume); try { - t.writableNeedDrain && await u(); - for await (let s of e) - t.write(s) || await u(); - n && (t.end(), await u()), r(); - } catch (s) { - r(i !== s ? gd(i, s) : s); + if (writable.writableNeedDrain) + await wait(); + for await (let chunk of iterable) + if (!writable.write(chunk)) + await wait(); + if (end) + writable.end(), await wait(); + finish(); + } catch (err) { + finish(error !== err ? aggregateTwoErrors(error, err) : err); } finally { - a(), t.off("drain", l); + cleanup(), writable.off("drain", resume); } } - async function Rn(e, t, r, { end: n }) { - tt(t) && (t = t.writable); - let i = t.getWriter(); + async function pumpToWeb(readable, writable, finish, { end }) { + if (isTransformStream(writable)) + writable = writable.writable; + let writer = writable.getWriter(); try { - for await (let o of e) - await i.ready, i.write(o).catch(() => { - }); - await i.ready, n && await i.close(), r(); - } catch (o) { + for await (let chunk of readable) + await writer.ready, writer.write(chunk).catch(() => {}); + if (await writer.ready, end) + await writer.close(); + finish(); + } catch (err) { try { - await i.abort(o), r(o); - } catch (l) { - r(l); - } - } - } - function Nd(...e) { - return vl(e, bd(Bd(e))); - } - function vl(e, t, r) { - if (e.length === 1 && dd(e[0]) && (e = e[0]), e.length < 2) - throw new _d("streams"); - let n = new Td, i = n.signal, o = r?.signal, l = []; - Ad(o, "options.signal"); - function u() { - _(new md); - } - Sn = Sn || q().addAbortListener; - let a; - o && (a = Sn(o, u)); - let s, c, d = [], y = 0; - function h(F) { - _(F, --y === 0); - } - function _(F, E) { - var N; - if (F && (!s || s.code === "ERR_STREAM_PREMATURE_CLOSE") && (s = F), !(!s && !E)) { - for (;d.length; ) - d.shift()(s); - (N = a) === null || N === undefined || N[yd](), n.abort(), E && (s || l.forEach((Se) => Se()), pt.nextTick(t, s, c)); - } - } - let p; - for (let F = 0;F < e.length; F++) { - let E = e[F], N = F < e.length - 1, Se = F > 0, W = N || r?.end !== false, Oe = F === e.length - 1; - if (Pl(E)) { - let v = function(ie) { - ie && ie.name !== "AbortError" && ie.code !== "ERR_STREAM_PREMATURE_CLOSE" && h(ie); + await writer.abort(err), finish(err); + } catch (err2) { + finish(err2); + } + } + } + function pipeline(...streams) { + return pipelineImpl(streams, once3(popCallback(streams))); + } + function pipelineImpl(streams, callback, opts) { + if (streams.length === 1 && ArrayIsArray(streams[0])) + streams = streams[0]; + if (streams.length < 2) + throw new ERR_MISSING_ARGS("streams"); + let ac = new AbortController, signal = ac.signal, outerSignal = opts === null || opts === undefined ? undefined : opts.signal, lastStreamCleanup = []; + validateAbortSignal2(outerSignal, "options.signal"); + function abort() { + finishImpl(new AbortError2); + } + addAbortListener2 = addAbortListener2 || require_util().addAbortListener; + let disposable; + if (outerSignal) + disposable = addAbortListener2(outerSignal, abort); + let error, value, destroys = [], finishCount = 0; + function finish(err) { + finishImpl(err, --finishCount === 0); + } + function finishImpl(err, final) { + var _disposable; + if (err && (!error || error.code === "ERR_STREAM_PREMATURE_CLOSE")) + error = err; + if (!error && !final) + return; + while (destroys.length) + destroys.shift()(error); + if ((_disposable = disposable) === null || _disposable === undefined || _disposable[SymbolDispose](), ac.abort(), final) { + if (!error) + lastStreamCleanup.forEach((fn) => fn()); + process2.nextTick(callback, error, value); + } + } + let ret; + for (let i2 = 0;i2 < streams.length; i2++) { + let stream = streams[i2], reading = i2 < streams.length - 1, writing = i2 > 0, end = reading || (opts === null || opts === undefined ? undefined : opts.end) !== false, isLastStream = i2 === streams.length - 1; + if (isNodeStream(stream)) { + let onError2 = function(err) { + if (err && err.name !== "AbortError" && err.code !== "ERR_STREAM_PREMATURE_CLOSE") + finish(err); }; - var x = v; - if (W) { - let { destroy: ie, cleanup: ir } = ql(E, N, Se); - d.push(ie), wn(E) && Oe && l.push(ir); + var onError = onError2; + if (end) { + let { destroy, cleanup } = destroyer(stream, reading, writing); + if (destroys.push(destroy), isReadable(stream) && isLastStream) + lastStreamCleanup.push(cleanup); } - E.on("error", v), wn(E) && Oe && l.push(() => { - E.removeListener("error", v); - }); + if (stream.on("error", onError2), isReadable(stream) && isLastStream) + lastStreamCleanup.push(() => { + stream.removeListener("error", onError2); + }); } - if (F === 0) - if (typeof E == "function") { - if (p = E({ signal: i }), !Me(p)) - throw new bn("Iterable, AsyncIterable or Stream", "source", p); - } else - Me(E) || Jt(E) || tt(E) ? p = E : p = Ol.from(E); - else if (typeof E == "function") { - if (tt(p)) { - var R; - p = mn((R = p) === null || R === undefined ? undefined : R.readable); + if (i2 === 0) + if (typeof stream === "function") { + if (ret = stream({ signal }), !isIterable(ret)) + throw new ERR_INVALID_RETURN_VALUE("Iterable, AsyncIterable or Stream", "source", ret); + } else if (isIterable(stream) || isReadableNodeStream(stream) || isTransformStream(stream)) + ret = stream; + else + ret = Duplex.from(stream); + else if (typeof stream === "function") { + if (isTransformStream(ret)) { + var _ret; + ret = makeAsyncIterable((_ret = ret) === null || _ret === undefined ? undefined : _ret.readable); } else - p = mn(p); - if (p = E(p, { signal: i }), N) { - if (!Me(p, true)) - throw new bn("AsyncIterable", `transform[${F - 1}]`, p); + ret = makeAsyncIterable(ret); + if (ret = stream(ret, { signal }), reading) { + if (!isIterable(ret, true)) + throw new ERR_INVALID_RETURN_VALUE("AsyncIterable", `transform[${i2 - 1}]`, ret); } else { - var w; - _n || (_n = yn()); - let v = new _n({ objectMode: true }), ie = (w = p) === null || w === undefined ? undefined : w.then; - if (typeof ie == "function") - y++, ie.call(p, (he) => { - c = he, he != null && v.write(he), W && v.end(), pt.nextTick(h); - }, (he) => { - v.destroy(he), pt.nextTick(h, he); + var _ret2; + if (!PassThrough) + PassThrough = require_passthrough(); + let pt = new PassThrough({ objectMode: true }), then = (_ret2 = ret) === null || _ret2 === undefined ? undefined : _ret2.then; + if (typeof then === "function") + finishCount++, then.call(ret, (val) => { + if (value = val, val != null) + pt.write(val); + if (end) + pt.end(); + process2.nextTick(finish); + }, (err) => { + pt.destroy(err), process2.nextTick(finish, err); }); - else if (Me(p, true)) - y++, Xt(p, v, h, { end: W }); - else if (gn(p) || tt(p)) { - let he = p.readable || p; - y++, Xt(he, v, h, { end: W }); + else if (isIterable(ret, true)) + finishCount++, pumpToNode(ret, pt, finish, { end }); + else if (isReadableStream(ret) || isTransformStream(ret)) { + let toRead = ret.readable || ret; + finishCount++, pumpToNode(toRead, pt, finish, { end }); } else - throw new bn("AsyncIterable or Promise", "destination", p); - p = v; - let { destroy: ir, cleanup: hu } = ql(p, false, true); - d.push(ir), Oe && l.push(hu); + throw new ERR_INVALID_RETURN_VALUE("AsyncIterable or Promise", "destination", ret); + ret = pt; + let { destroy, cleanup } = destroyer(ret, false, true); + if (destroys.push(destroy), isLastStream) + lastStreamCleanup.push(cleanup); } - } else if (Pl(E)) { - if (Jt(p)) { - y += 2; - let v = Fd(p, E, h, { end: W }); - wn(E) && Oe && l.push(v); - } else if (tt(p) || gn(p)) { - let v = p.readable || p; - y++, Xt(v, E, h, { end: W }); - } else if (Me(p)) - y++, Xt(p, E, h, { end: W }); + } else if (isNodeStream(stream)) { + if (isReadableNodeStream(ret)) { + finishCount += 2; + let cleanup = pipe(ret, stream, finish, { end }); + if (isReadable(stream) && isLastStream) + lastStreamCleanup.push(cleanup); + } else if (isTransformStream(ret) || isReadableStream(ret)) { + let toRead = ret.readable || ret; + finishCount++, pumpToNode(toRead, stream, finish, { end }); + } else if (isIterable(ret)) + finishCount++, pumpToNode(ret, stream, finish, { end }); else - throw new An("val", ["Readable", "Iterable", "AsyncIterable", "ReadableStream", "TransformStream"], p); - p = E; - } else if (xd(E)) { - if (Jt(p)) - y++, Rn(mn(p), E, h, { end: W }); - else if (gn(p) || Me(p)) - y++, Rn(p, E, h, { end: W }); - else if (tt(p)) - y++, Rn(p.readable, E, h, { end: W }); + throw new ERR_INVALID_ARG_TYPE3("val", ["Readable", "Iterable", "AsyncIterable", "ReadableStream", "TransformStream"], ret); + ret = stream; + } else if (isWebStream(stream)) { + if (isReadableNodeStream(ret)) + finishCount++, pumpToWeb(makeAsyncIterable(ret), stream, finish, { end }); + else if (isReadableStream(ret) || isIterable(ret)) + finishCount++, pumpToWeb(ret, stream, finish, { end }); + else if (isTransformStream(ret)) + finishCount++, pumpToWeb(ret.readable, stream, finish, { end }); else - throw new An("val", ["Readable", "Iterable", "AsyncIterable", "ReadableStream", "TransformStream"], p); - p = E; + throw new ERR_INVALID_ARG_TYPE3("val", ["Readable", "Iterable", "AsyncIterable", "ReadableStream", "TransformStream"], ret); + ret = stream; } else - p = Ol.from(E); + ret = Duplex.from(stream); } - return (i != null && i.aborted || o != null && o.aborted) && pt.nextTick(u), p; + if (signal !== null && signal !== undefined && signal.aborted || outerSignal !== null && outerSignal !== undefined && outerSignal.aborted) + process2.nextTick(abort); + return ret; } - function Fd(e, t, r, { end: n }) { - let i = false; - if (t.on("close", () => { - i || r(new Sd); - }), e.pipe(t, { end: false }), n) { - let l = function() { - i = true, t.end(); + function pipe(src, dst, finish, { end }) { + let ended = false; + if (dst.on("close", () => { + if (!ended) + finish(new ERR_STREAM_PREMATURE_CLOSE); + }), src.pipe(dst, { end: false }), end) { + let endFn2 = function() { + ended = true, dst.end(); }; - var o = l; - Id(e) ? pt.nextTick(l) : e.once("end", l); + var endFn = endFn2; + if (isReadableFinished(src)) + process2.nextTick(endFn2); + else + src.once("end", endFn2); } else - r(); - return Qt(e, { readable: true, writable: false }, (l) => { - let u = e._readableState; - l && l.code === "ERR_STREAM_PREMATURE_CLOSE" && u && u.ended && !u.errored && !u.errorEmitted ? e.once("end", r).once("error", r) : r(l); - }), Qt(t, { readable: false, writable: true }, r); + finish(); + return eos(src, { readable: true, writable: false }, (err) => { + let rState = src._readableState; + if (err && err.code === "ERR_STREAM_PREMATURE_CLOSE" && rState && rState.ended && !rState.errored && !rState.errorEmitted) + src.once("end", finish).once("error", finish); + else + finish(err); + }), eos(dst, { readable: false, writable: true }, finish); } - Ul.exports = { pipelineImpl: vl, pipeline: Nd }; + module2.exports = { pipelineImpl, pipeline }; }); - In = S((ip, Vl) => { - var { pipeline: Md } = Zt(), er = re(), { destroyer: Cd } = Be(), { isNodeStream: tr, isReadable: Wl, isWritable: $l, isWebStream: xn, isTransformStream: Ce, isWritableStream: jl, isReadableStream: Hl } = Z(), { AbortError: kd, codes: { ERR_INVALID_ARG_VALUE: Gl, ERR_MISSING_ARGS: Dd } } = O(), Od = ae(); - Vl.exports = function(...t) { - if (t.length === 0) - throw new Dd("streams"); - if (t.length === 1) - return er.from(t[0]); - let r = [...t]; - if (typeof t[0] == "function" && (t[0] = er.from(t[0])), typeof t[t.length - 1] == "function") { - let h = t.length - 1; - t[h] = er.from(t[h]); - } - for (let h = 0;h < t.length; ++h) - if (!(!tr(t[h]) && !xn(t[h]))) { - if (h < t.length - 1 && !(Wl(t[h]) || Hl(t[h]) || Ce(t[h]))) - throw new Gl(`streams[${h}]`, r[h], "must be readable"); - if (h > 0 && !($l(t[h]) || jl(t[h]) || Ce(t[h]))) - throw new Gl(`streams[${h}]`, r[h], "must be writable"); - } - let n, i, o, l, u; - function a(h) { - let _ = l; - l = null, _ ? _(h) : h ? u.destroy(h) : !y && !d && u.destroy(); - } - let s = t[0], c = Md(t, a), d = !!($l(s) || jl(s) || Ce(s)), y = !!(Wl(c) || Hl(c) || Ce(c)); - if (u = new er({ writableObjectMode: !!(s != null && s.writableObjectMode), readableObjectMode: !!(c != null && c.readableObjectMode), writable: d, readable: y }), d) { - if (tr(s)) - u._write = function(_, p, R) { - s.write(_, p) ? R() : n = R; - }, u._final = function(_) { - s.end(), i = _; - }, s.on("drain", function() { - if (n) { - let _ = n; - n = null, _(); + var require_compose = __commonJS2((exports2, module2) => { + var { pipeline } = require_pipeline(), Duplex = require_duplex(), { destroyer } = require_destroy(), { isNodeStream, isReadable, isWritable, isWebStream, isTransformStream, isWritableStream, isReadableStream } = require_utils(), { AbortError: AbortError2, codes: { ERR_INVALID_ARG_VALUE, ERR_MISSING_ARGS } } = require_errors(), eos = require_end_of_stream(); + module2.exports = function compose(...streams) { + if (streams.length === 0) + throw new ERR_MISSING_ARGS("streams"); + if (streams.length === 1) + return Duplex.from(streams[0]); + let orgStreams = [...streams]; + if (typeof streams[0] === "function") + streams[0] = Duplex.from(streams[0]); + if (typeof streams[streams.length - 1] === "function") { + let idx = streams.length - 1; + streams[idx] = Duplex.from(streams[idx]); + } + for (let n = 0;n < streams.length; ++n) { + if (!isNodeStream(streams[n]) && !isWebStream(streams[n])) + continue; + if (n < streams.length - 1 && !(isReadable(streams[n]) || isReadableStream(streams[n]) || isTransformStream(streams[n]))) + throw new ERR_INVALID_ARG_VALUE(`streams[${n}]`, orgStreams[n], "must be readable"); + if (n > 0 && !(isWritable(streams[n]) || isWritableStream(streams[n]) || isTransformStream(streams[n]))) + throw new ERR_INVALID_ARG_VALUE(`streams[${n}]`, orgStreams[n], "must be writable"); + } + let ondrain, onfinish, onreadable, onclose, d; + function onfinished(err) { + let cb = onclose; + if (onclose = null, cb) + cb(err); + else if (err) + d.destroy(err); + else if (!readable && !writable) + d.destroy(); + } + let head = streams[0], tail = pipeline(streams, onfinished), writable = !!(isWritable(head) || isWritableStream(head) || isTransformStream(head)), readable = !!(isReadable(tail) || isReadableStream(tail) || isTransformStream(tail)); + if (d = new Duplex({ writableObjectMode: !!(head !== null && head !== undefined && head.writableObjectMode), readableObjectMode: !!(tail !== null && tail !== undefined && tail.readableObjectMode), writable, readable }), writable) { + if (isNodeStream(head)) + d._write = function(chunk, encoding, callback) { + if (head.write(chunk, encoding)) + callback(); + else + ondrain = callback; + }, d._final = function(callback) { + head.end(), onfinish = callback; + }, head.on("drain", function() { + if (ondrain) { + let cb = ondrain; + ondrain = null, cb(); } }); - else if (xn(s)) { - let p = (Ce(s) ? s.writable : s).getWriter(); - u._write = async function(R, w, x) { + else if (isWebStream(head)) { + let writer = (isTransformStream(head) ? head.writable : head).getWriter(); + d._write = async function(chunk, encoding, callback) { try { - await p.ready, p.write(R).catch(() => { - }), x(); - } catch (F) { - x(F); + await writer.ready, writer.write(chunk).catch(() => {}), callback(); + } catch (err) { + callback(err); } - }, u._final = async function(R) { + }, d._final = async function(callback) { try { - await p.ready, p.close().catch(() => { - }), i = R; - } catch (w) { - R(w); + await writer.ready, writer.close().catch(() => {}), onfinish = callback; + } catch (err) { + callback(err); } }; } - let h = Ce(c) ? c.readable : c; - Od(h, () => { - if (i) { - let _ = i; - i = null, _(); + let toRead = isTransformStream(tail) ? tail.readable : tail; + eos(toRead, () => { + if (onfinish) { + let cb = onfinish; + onfinish = null, cb(); } }); } - if (y) { - if (tr(c)) - c.on("readable", function() { - if (o) { - let h = o; - o = null, h(); + if (readable) { + if (isNodeStream(tail)) + tail.on("readable", function() { + if (onreadable) { + let cb = onreadable; + onreadable = null, cb(); } - }), c.on("end", function() { - u.push(null); - }), u._read = function() { - for (;; ) { - let h = c.read(); - if (h === null) { - o = u._read; + }), tail.on("end", function() { + d.push(null); + }), d._read = function() { + while (true) { + let buf = tail.read(); + if (buf === null) { + onreadable = d._read; return; } - if (!u.push(h)) + if (!d.push(buf)) return; } }; - else if (xn(c)) { - let _ = (Ce(c) ? c.readable : c).getReader(); - u._read = async function() { - for (;; ) + else if (isWebStream(tail)) { + let reader = (isTransformStream(tail) ? tail.readable : tail).getReader(); + d._read = async function() { + while (true) try { - let { value: p, done: R } = await _.read(); - if (!u.push(p)) + let { value, done } = await reader.read(); + if (!d.push(value)) return; - if (R) { - u.push(null); + if (done) { + d.push(null); return; } } catch { @@ -3835,323 +5321,355 @@ var init_stream = __esm(() => { }; } } - return u._destroy = function(h, _) { - !h && l !== null && (h = new kd), o = null, n = null, i = null, l === null ? _(h) : (l = _, tr(c) && Cd(c, h)); - }, u; + return d._destroy = function(err, callback) { + if (!err && onclose !== null) + err = new AbortError2; + if (onreadable = null, ondrain = null, onfinish = null, onclose === null) + callback(err); + else if (onclose = callback, isNodeStream(tail)) + destroyer(tail, err); + }, d; }; }); - ru = S((op, Ln) => { - var Pd = globalThis.AbortController || We().AbortController, { codes: { ERR_INVALID_ARG_VALUE: qd, ERR_INVALID_ARG_TYPE: yt, ERR_MISSING_ARGS: vd, ERR_OUT_OF_RANGE: Ud }, AbortError: ne } = O(), { validateAbortSignal: ke, validateInteger: Yl, validateObject: De } = He(), Wd = I().Symbol("kWeak"), $d = I().Symbol("kResistStopPropagation"), { finished: jd } = ae(), Hd = In(), { addAbortSignalNoValidate: Gd } = ot(), { isWritable: Vd, isNodeStream: Yd } = Z(), { deprecate: Kd } = q(), { ArrayPrototypePush: zd, Boolean: Xd, MathFloor: Kl, Number: Jd, NumberIsNaN: Qd, Promise: zl, PromiseReject: Xl, PromiseResolve: Zd, PromisePrototypeThen: Jl, Symbol: Zl } = I(), rr = Zl("kEmpty"), Ql = Zl("kEof"); - function eh(e, t) { - if (t != null && De(t, "options"), t?.signal != null && ke(t.signal, "options.signal"), Yd(e) && !Vd(e)) - throw new qd("stream", e, "must be writable"); - let r = Hd(this, e); - return t != null && t.signal && Gd(t.signal, r), r; - } - function nr(e, t) { - if (typeof e != "function") - throw new yt("fn", ["Function", "AsyncFunction"], e); - t != null && De(t, "options"), t?.signal != null && ke(t.signal, "options.signal"); - let r = 1; - t?.concurrency != null && (r = Kl(t.concurrency)); - let n = r - 1; - return t?.highWaterMark != null && (n = Kl(t.highWaterMark)), Yl(r, "options.concurrency", 1), Yl(n, "options.highWaterMark", 0), n += r, async function* () { - let o = q().AbortSignalAny([t?.signal].filter(Xd)), l = this, u = [], a = { signal: o }, s, c, d = false, y = 0; - function h() { - d = true, _(); - } - function _() { - y -= 1, p(); - } - function p() { - c && !d && y < r && u.length < n && (c(), c = null); - } - async function R() { + var require_operators = __commonJS2((exports2, module2) => { + var AbortController = globalThis.AbortController || require_abort_controller().AbortController, { codes: { ERR_INVALID_ARG_VALUE, ERR_INVALID_ARG_TYPE: ERR_INVALID_ARG_TYPE3, ERR_MISSING_ARGS, ERR_OUT_OF_RANGE: ERR_OUT_OF_RANGE3 }, AbortError: AbortError2 } = require_errors(), { validateAbortSignal: validateAbortSignal2, validateInteger, validateObject } = require_validators(), kWeakHandler = require_primordials().Symbol("kWeak"), kResistStopPropagation = require_primordials().Symbol("kResistStopPropagation"), { finished } = require_end_of_stream(), staticCompose = require_compose(), { addAbortSignalNoValidate } = require_add_abort_signal(), { isWritable, isNodeStream } = require_utils(), { deprecate } = require_util(), { ArrayPrototypePush, Boolean: Boolean2, MathFloor, Number: Number2, NumberIsNaN, Promise: Promise2, PromiseReject, PromiseResolve, PromisePrototypeThen, Symbol: Symbol2 } = require_primordials(), kEmpty = Symbol2("kEmpty"), kEof = Symbol2("kEof"); + function compose(stream, options) { + if (options != null) + validateObject(options, "options"); + if ((options === null || options === undefined ? undefined : options.signal) != null) + validateAbortSignal2(options.signal, "options.signal"); + if (isNodeStream(stream) && !isWritable(stream)) + throw new ERR_INVALID_ARG_VALUE("stream", stream, "must be writable"); + let composedStream = staticCompose(this, stream); + if (options !== null && options !== undefined && options.signal) + addAbortSignalNoValidate(options.signal, composedStream); + return composedStream; + } + function map(fn, options) { + if (typeof fn !== "function") + throw new ERR_INVALID_ARG_TYPE3("fn", ["Function", "AsyncFunction"], fn); + if (options != null) + validateObject(options, "options"); + if ((options === null || options === undefined ? undefined : options.signal) != null) + validateAbortSignal2(options.signal, "options.signal"); + let concurrency = 1; + if ((options === null || options === undefined ? undefined : options.concurrency) != null) + concurrency = MathFloor(options.concurrency); + let highWaterMark = concurrency - 1; + if ((options === null || options === undefined ? undefined : options.highWaterMark) != null) + highWaterMark = MathFloor(options.highWaterMark); + return validateInteger(concurrency, "options.concurrency", 1), validateInteger(highWaterMark, "options.highWaterMark", 0), highWaterMark += concurrency, async function* map() { + let signal = require_util().AbortSignalAny([options === null || options === undefined ? undefined : options.signal].filter(Boolean2)), stream = this, queue = [], signalOpt = { signal }, next, resume, done = false, cnt = 0; + function onCatch() { + done = true, afterItemProcessed(); + } + function afterItemProcessed() { + cnt -= 1, maybeResume(); + } + function maybeResume() { + if (resume && !done && cnt < concurrency && queue.length < highWaterMark) + resume(), resume = null; + } + async function pump() { try { - for await (let w of l) { - if (d) + for await (let val of stream) { + if (done) return; - if (o.aborted) - throw new ne; + if (signal.aborted) + throw new AbortError2; try { - if (w = e(w, a), w === rr) + if (val = fn(val, signalOpt), val === kEmpty) continue; - w = Zd(w); - } catch (x) { - w = Xl(x); + val = PromiseResolve(val); + } catch (err) { + val = PromiseReject(err); } - y += 1, Jl(w, _, h), u.push(w), s && (s(), s = null), !d && (u.length >= n || y >= r) && await new zl((x) => { - c = x; - }); + if (cnt += 1, PromisePrototypeThen(val, afterItemProcessed, onCatch), queue.push(val), next) + next(), next = null; + if (!done && (queue.length >= highWaterMark || cnt >= concurrency)) + await new Promise2((resolve) => { + resume = resolve; + }); } - u.push(Ql); - } catch (w) { - let x = Xl(w); - Jl(x, _, h), u.push(x); + queue.push(kEof); + } catch (err) { + let val = PromiseReject(err); + PromisePrototypeThen(val, afterItemProcessed, onCatch), queue.push(val); } finally { - d = true, s && (s(), s = null); + if (done = true, next) + next(), next = null; } } - R(); + pump(); try { - for (;; ) { - for (;u.length > 0; ) { - let w = await u[0]; - if (w === Ql) + while (true) { + while (queue.length > 0) { + let val = await queue[0]; + if (val === kEof) return; - if (o.aborted) - throw new ne; - w !== rr && (yield w), u.shift(), p(); + if (signal.aborted) + throw new AbortError2; + if (val !== kEmpty) + yield val; + queue.shift(), maybeResume(); } - await new zl((w) => { - s = w; + await new Promise2((resolve) => { + next = resolve; }); } } finally { - d = true, c && (c(), c = null); + if (done = true, resume) + resume(), resume = null; } }.call(this); } - function th(e = undefined) { - return e != null && De(e, "options"), e?.signal != null && ke(e.signal, "options.signal"), async function* () { - let r = 0; - for await (let i of this) { - var n; - if (e != null && (n = e.signal) !== null && n !== undefined && n.aborted) - throw new ne({ cause: e.signal.reason }); - yield [r++, i]; + function asIndexedPairs(options = undefined) { + if (options != null) + validateObject(options, "options"); + if ((options === null || options === undefined ? undefined : options.signal) != null) + validateAbortSignal2(options.signal, "options.signal"); + return async function* asIndexedPairs() { + let index = 0; + for await (let val of this) { + var _options$signal; + if (options !== null && options !== undefined && (_options$signal = options.signal) !== null && _options$signal !== undefined && _options$signal.aborted) + throw new AbortError2({ cause: options.signal.reason }); + yield [index++, val]; } }.call(this); } - async function eu(e, t = undefined) { - for await (let r of Bn.call(this, e, t)) + async function some(fn, options = undefined) { + for await (let unused of filter.call(this, fn, options)) return true; return false; } - async function rh(e, t = undefined) { - if (typeof e != "function") - throw new yt("fn", ["Function", "AsyncFunction"], e); - return !await eu.call(this, async (...r) => !await e(...r), t); + async function every(fn, options = undefined) { + if (typeof fn !== "function") + throw new ERR_INVALID_ARG_TYPE3("fn", ["Function", "AsyncFunction"], fn); + return !await some.call(this, async (...args) => { + return !await fn(...args); + }, options); } - async function nh(e, t) { - for await (let r of Bn.call(this, e, t)) - return r; + async function find(fn, options) { + for await (let result of filter.call(this, fn, options)) + return result; + return; } - async function ih(e, t) { - if (typeof e != "function") - throw new yt("fn", ["Function", "AsyncFunction"], e); - async function r(n, i) { - return await e(n, i), rr; + async function forEach(fn, options) { + if (typeof fn !== "function") + throw new ERR_INVALID_ARG_TYPE3("fn", ["Function", "AsyncFunction"], fn); + async function forEachFn(value, options2) { + return await fn(value, options2), kEmpty; } - for await (let n of nr.call(this, r, t)) + for await (let unused of map.call(this, forEachFn, options)) ; } - function Bn(e, t) { - if (typeof e != "function") - throw new yt("fn", ["Function", "AsyncFunction"], e); - async function r(n, i) { - return await e(n, i) ? n : rr; + function filter(fn, options) { + if (typeof fn !== "function") + throw new ERR_INVALID_ARG_TYPE3("fn", ["Function", "AsyncFunction"], fn); + async function filterFn(value, options2) { + if (await fn(value, options2)) + return value; + return kEmpty; } - return nr.call(this, r, t); + return map.call(this, filterFn, options); } - var Tn = class extends vd { + + class ReduceAwareErrMissingArgs extends ERR_MISSING_ARGS { constructor() { - super("reduce"), this.message = "Reduce of an empty stream requires an initial value"; - } - }; - async function oh(e, t, r) { - var n; - if (typeof e != "function") - throw new yt("reducer", ["Function", "AsyncFunction"], e); - r != null && De(r, "options"), r?.signal != null && ke(r.signal, "options.signal"); - let i = arguments.length > 1; - if (r != null && (n = r.signal) !== null && n !== undefined && n.aborted) { - let s = new ne(undefined, { cause: r.signal.reason }); - throw this.once("error", () => { - }), await jd(this.destroy(s)), s; - } - let o = new Pd, l = o.signal; - if (r != null && r.signal) { - let s = { once: true, [Wd]: this, [$d]: true }; - r.signal.addEventListener("abort", () => o.abort(), s); - } - let u = false; + super("reduce"); + this.message = "Reduce of an empty stream requires an initial value"; + } + } + async function reduce(reducer, initialValue, options) { + var _options$signal2; + if (typeof reducer !== "function") + throw new ERR_INVALID_ARG_TYPE3("reducer", ["Function", "AsyncFunction"], reducer); + if (options != null) + validateObject(options, "options"); + if ((options === null || options === undefined ? undefined : options.signal) != null) + validateAbortSignal2(options.signal, "options.signal"); + let hasInitialValue = arguments.length > 1; + if (options !== null && options !== undefined && (_options$signal2 = options.signal) !== null && _options$signal2 !== undefined && _options$signal2.aborted) { + let err = new AbortError2(undefined, { cause: options.signal.reason }); + throw this.once("error", () => {}), await finished(this.destroy(err)), err; + } + let ac = new AbortController, signal = ac.signal; + if (options !== null && options !== undefined && options.signal) { + let opts = { once: true, [kWeakHandler]: this, [kResistStopPropagation]: true }; + options.signal.addEventListener("abort", () => ac.abort(), opts); + } + let gotAnyItemFromStream = false; try { - for await (let s of this) { - var a; - if (u = true, r != null && (a = r.signal) !== null && a !== undefined && a.aborted) - throw new ne; - i ? t = await e(t, s, { signal: l }) : (t = s, i = true); - } - if (!u && !i) - throw new Tn; + for await (let value of this) { + var _options$signal3; + if (gotAnyItemFromStream = true, options !== null && options !== undefined && (_options$signal3 = options.signal) !== null && _options$signal3 !== undefined && _options$signal3.aborted) + throw new AbortError2; + if (!hasInitialValue) + initialValue = value, hasInitialValue = true; + else + initialValue = await reducer(initialValue, value, { signal }); + } + if (!gotAnyItemFromStream && !hasInitialValue) + throw new ReduceAwareErrMissingArgs; } finally { - o.abort(); - } - return t; - } - async function lh(e) { - e != null && De(e, "options"), e?.signal != null && ke(e.signal, "options.signal"); - let t = []; - for await (let n of this) { - var r; - if (e != null && (r = e.signal) !== null && r !== undefined && r.aborted) - throw new ne(undefined, { cause: e.signal.reason }); - zd(t, n); - } - return t; - } - function uh(e, t) { - let r = nr.call(this, e, t); - return async function* () { - for await (let i of r) - yield* i; + ac.abort(); + } + return initialValue; + } + async function toArray(options) { + if (options != null) + validateObject(options, "options"); + if ((options === null || options === undefined ? undefined : options.signal) != null) + validateAbortSignal2(options.signal, "options.signal"); + let result = []; + for await (let val of this) { + var _options$signal4; + if (options !== null && options !== undefined && (_options$signal4 = options.signal) !== null && _options$signal4 !== undefined && _options$signal4.aborted) + throw new AbortError2(undefined, { cause: options.signal.reason }); + ArrayPrototypePush(result, val); + } + return result; + } + function flatMap(fn, options) { + let values = map.call(this, fn, options); + return async function* flatMap() { + for await (let val of values) + yield* val; }.call(this); } - function tu(e) { - if (e = Jd(e), Qd(e)) + function toIntegerOrInfinity(number) { + if (number = Number2(number), NumberIsNaN(number)) return 0; - if (e < 0) - throw new Ud("number", ">= 0", e); - return e; - } - function sh(e, t = undefined) { - return t != null && De(t, "options"), t?.signal != null && ke(t.signal, "options.signal"), e = tu(e), async function* () { - var n; - if (t != null && (n = t.signal) !== null && n !== undefined && n.aborted) - throw new ne; - for await (let o of this) { - var i; - if (t != null && (i = t.signal) !== null && i !== undefined && i.aborted) - throw new ne; - e-- <= 0 && (yield o); + if (number < 0) + throw new ERR_OUT_OF_RANGE3("number", ">= 0", number); + return number; + } + function drop(number, options = undefined) { + if (options != null) + validateObject(options, "options"); + if ((options === null || options === undefined ? undefined : options.signal) != null) + validateAbortSignal2(options.signal, "options.signal"); + return number = toIntegerOrInfinity(number), async function* drop() { + var _options$signal5; + if (options !== null && options !== undefined && (_options$signal5 = options.signal) !== null && _options$signal5 !== undefined && _options$signal5.aborted) + throw new AbortError2; + for await (let val of this) { + var _options$signal6; + if (options !== null && options !== undefined && (_options$signal6 = options.signal) !== null && _options$signal6 !== undefined && _options$signal6.aborted) + throw new AbortError2; + if (number-- <= 0) + yield val; } }.call(this); } - function ah(e, t = undefined) { - return t != null && De(t, "options"), t?.signal != null && ke(t.signal, "options.signal"), e = tu(e), async function* () { - var n; - if (t != null && (n = t.signal) !== null && n !== undefined && n.aborted) - throw new ne; - for await (let o of this) { - var i; - if (t != null && (i = t.signal) !== null && i !== undefined && i.aborted) - throw new ne; - if (e-- > 0 && (yield o), e <= 0) + function take(number, options = undefined) { + if (options != null) + validateObject(options, "options"); + if ((options === null || options === undefined ? undefined : options.signal) != null) + validateAbortSignal2(options.signal, "options.signal"); + return number = toIntegerOrInfinity(number), async function* take() { + var _options$signal7; + if (options !== null && options !== undefined && (_options$signal7 = options.signal) !== null && _options$signal7 !== undefined && _options$signal7.aborted) + throw new AbortError2; + for await (let val of this) { + var _options$signal8; + if (options !== null && options !== undefined && (_options$signal8 = options.signal) !== null && _options$signal8 !== undefined && _options$signal8.aborted) + throw new AbortError2; + if (number-- > 0) + yield val; + if (number <= 0) return; } }.call(this); } - Ln.exports.streamReturningOperators = { asIndexedPairs: Kd(th, "readable.asIndexedPairs will be removed in a future version."), drop: sh, filter: Bn, flatMap: uh, map: nr, take: ah, compose: eh }; - Ln.exports.promiseReturningOperators = { every: rh, forEach: ih, reduce: oh, toArray: lh, some: eu, find: nh }; + module2.exports.streamReturningOperators = { asIndexedPairs: deprecate(asIndexedPairs, "readable.asIndexedPairs will be removed in a future version."), drop, filter, flatMap, map, take, compose }; + module2.exports.promiseReturningOperators = { every, forEach, reduce, toArray, some, find }; }); - Nn = S((lp, nu) => { - var { ArrayPrototypePop: fh, Promise: ch } = I(), { isIterable: dh, isNodeStream: hh, isWebStream: ph } = Z(), { pipelineImpl: yh } = Zt(), { finished: bh } = ae(); - Fn(); - function wh(...e) { - return new ch((t, r) => { - let n, i, o = e[e.length - 1]; - if (o && typeof o == "object" && !hh(o) && !dh(o) && !ph(o)) { - let l = fh(e); - n = l.signal, i = l.end; - } - yh(e, (l, u) => { - l ? r(l) : t(u); - }, { signal: n, end: i }); + var require_promises = __commonJS2((exports2, module2) => { + var { ArrayPrototypePop, Promise: Promise2 } = require_primordials(), { isIterable, isNodeStream, isWebStream } = require_utils(), { pipelineImpl: pl } = require_pipeline(), { finished } = require_end_of_stream(); + require_stream2(); + function pipeline(...streams) { + return new Promise2((resolve, reject) => { + let signal, end, lastArg = streams[streams.length - 1]; + if (lastArg && typeof lastArg === "object" && !isNodeStream(lastArg) && !isIterable(lastArg) && !isWebStream(lastArg)) { + let options = ArrayPrototypePop(streams); + signal = options.signal, end = options.end; + } + pl(streams, (err, value) => { + if (err) + reject(err); + else + resolve(value); + }, { signal, end }); }); } - nu.exports = { finished: bh, pipeline: wh }; + module2.exports = { finished, pipeline }; }); - Fn = S((up, du) => { - var { Buffer: gh } = le(), { ObjectDefineProperty: de, ObjectKeys: lu, ReflectApply: uu } = I(), { promisify: { custom: su } } = q(), { streamReturningOperators: iu, promiseReturningOperators: ou } = ru(), { codes: { ERR_ILLEGAL_CONSTRUCTOR: au } } = O(), _h = In(), { setDefaultHighWaterMark: Eh, getDefaultHighWaterMark: Sh } = lt(), { pipeline: fu } = Zt(), { destroyer: mh } = Be(), cu = ae(), Mn = Nn(), bt = Z(), A = du.exports = Dt().Stream; - A.isDestroyed = bt.isDestroyed; - A.isDisturbed = bt.isDisturbed; - A.isErrored = bt.isErrored; - A.isReadable = bt.isReadable; - A.isWritable = bt.isWritable; - A.Readable = at(); - for (let e of lu(iu)) { - let r = function(...n) { + var require_stream2 = __commonJS2((exports2, module2) => { + var { Buffer: Buffer3 } = (init_buffer(), __toCommonJS(exports_buffer)), { ObjectDefineProperty, ObjectKeys, ReflectApply } = require_primordials(), { promisify: { custom: customPromisify } } = require_util(), { streamReturningOperators, promiseReturningOperators } = require_operators(), { codes: { ERR_ILLEGAL_CONSTRUCTOR } } = require_errors(), compose = require_compose(), { setDefaultHighWaterMark, getDefaultHighWaterMark } = require_state(), { pipeline } = require_pipeline(), { destroyer } = require_destroy(), eos = require_end_of_stream(), promises = require_promises(), utils = require_utils(), Stream = module2.exports = require_legacy().Stream; + Stream.isDestroyed = utils.isDestroyed; + Stream.isDisturbed = utils.isDisturbed; + Stream.isErrored = utils.isErrored; + Stream.isReadable = utils.isReadable; + Stream.isWritable = utils.isWritable; + Stream.Readable = require_readable(); + for (let key of ObjectKeys(streamReturningOperators)) { + let fn = function(...args) { if (new.target) - throw au(); - return A.Readable.from(uu(t, this, n)); - }, t = iu[e]; - de(r, "name", { __proto__: null, value: t.name }), de(r, "length", { __proto__: null, value: t.length }), de(A.Readable.prototype, e, { __proto__: null, value: r, enumerable: false, configurable: true, writable: true }); + throw ERR_ILLEGAL_CONSTRUCTOR(); + return Stream.Readable.from(ReflectApply(op, this, args)); + }, op = streamReturningOperators[key]; + ObjectDefineProperty(fn, "name", { __proto__: null, value: op.name }), ObjectDefineProperty(fn, "length", { __proto__: null, value: op.length }), ObjectDefineProperty(Stream.Readable.prototype, key, { __proto__: null, value: fn, enumerable: false, configurable: true, writable: true }); } - for (let e of lu(ou)) { - let r = function(...n) { + for (let key of ObjectKeys(promiseReturningOperators)) { + let fn = function(...args) { if (new.target) - throw au(); - return uu(t, this, n); - }, t = ou[e]; - de(r, "name", { __proto__: null, value: t.name }), de(r, "length", { __proto__: null, value: t.length }), de(A.Readable.prototype, e, { __proto__: null, value: r, enumerable: false, configurable: true, writable: true }); - } - A.Writable = zt(); - A.Duplex = re(); - A.Transform = hn(); - A.PassThrough = yn(); - A.pipeline = fu; - var { addAbortSignal: Rh } = ot(); - A.addAbortSignal = Rh; - A.finished = cu; - A.destroy = mh; - A.compose = _h; - A.setDefaultHighWaterMark = Eh; - A.getDefaultHighWaterMark = Sh; - de(A, "promises", { __proto__: null, configurable: true, enumerable: true, get() { - return Mn; + throw ERR_ILLEGAL_CONSTRUCTOR(); + return ReflectApply(op, this, args); + }, op = promiseReturningOperators[key]; + ObjectDefineProperty(fn, "name", { __proto__: null, value: op.name }), ObjectDefineProperty(fn, "length", { __proto__: null, value: op.length }), ObjectDefineProperty(Stream.Readable.prototype, key, { __proto__: null, value: fn, enumerable: false, configurable: true, writable: true }); + } + Stream.Writable = require_writable(); + Stream.Duplex = require_duplex(); + Stream.Transform = require_transform(); + Stream.PassThrough = require_passthrough(); + Stream.pipeline = pipeline; + var { addAbortSignal } = require_add_abort_signal(); + Stream.addAbortSignal = addAbortSignal; + Stream.finished = eos; + Stream.destroy = destroyer; + Stream.compose = compose; + Stream.setDefaultHighWaterMark = setDefaultHighWaterMark; + Stream.getDefaultHighWaterMark = getDefaultHighWaterMark; + ObjectDefineProperty(Stream, "promises", { __proto__: null, configurable: true, enumerable: true, get() { + return promises; } }); - de(fu, su, { __proto__: null, enumerable: true, get() { - return Mn.pipeline; + ObjectDefineProperty(pipeline, customPromisify, { __proto__: null, enumerable: true, get() { + return promises.pipeline; } }); - de(cu, su, { __proto__: null, enumerable: true, get() { - return Mn.finished; + ObjectDefineProperty(eos, customPromisify, { __proto__: null, enumerable: true, get() { + return promises.finished; } }); - A.Stream = A; - A._isUint8Array = function(t) { - return t instanceof Uint8Array; + Stream.Stream = Stream; + Stream._isUint8Array = function isUint8Array(value) { + return value instanceof Uint8Array; }; - A._uint8ArrayToBuffer = function(t) { - return gh.from(t.buffer, t.byteOffset, t.byteLength); + Stream._uint8ArrayToBuffer = function _uint8ArrayToBuffer(chunk) { + return Buffer3.from(chunk.buffer, chunk.byteOffset, chunk.byteLength); }; }); - Cn = S((sp, L) => { - var C = Fn(), Ah = Nn(), xh = C.Readable.destroy; - L.exports = C.Readable; - L.exports._uint8ArrayToBuffer = C._uint8ArrayToBuffer; - L.exports._isUint8Array = C._isUint8Array; - L.exports.isDisturbed = C.isDisturbed; - L.exports.isErrored = C.isErrored; - L.exports.isReadable = C.isReadable; - L.exports.Readable = C.Readable; - L.exports.Writable = C.Writable; - L.exports.Duplex = C.Duplex; - L.exports.Transform = C.Transform; - L.exports.PassThrough = C.PassThrough; - L.exports.addAbortSignal = C.addAbortSignal; - L.exports.finished = C.finished; - L.exports.destroy = C.destroy; - L.exports.destroy = xh; - L.exports.pipeline = C.pipeline; - L.exports.compose = C.compose; - Object.defineProperty(C, "promises", { configurable: true, enumerable: true, get() { - return Ah; - } }); - L.exports.Stream = C.Stream; - L.exports.default = L.exports; + var require_ours = __commonJS2((exports2, module2) => { + var Stream = require_stream(); + { + let CustomStream = require_stream2(), promises = require_promises(), originalDestroy = CustomStream.Readable.destroy; + module2.exports = CustomStream.Readable, module2.exports._uint8ArrayToBuffer = CustomStream._uint8ArrayToBuffer, module2.exports._isUint8Array = CustomStream._isUint8Array, module2.exports.isDisturbed = CustomStream.isDisturbed, module2.exports.isErrored = CustomStream.isErrored, module2.exports.isReadable = CustomStream.isReadable, module2.exports.Readable = CustomStream.Readable, module2.exports.Writable = CustomStream.Writable, module2.exports.Duplex = CustomStream.Duplex, module2.exports.Transform = CustomStream.Transform, module2.exports.PassThrough = CustomStream.PassThrough, module2.exports.addAbortSignal = CustomStream.addAbortSignal, module2.exports.finished = CustomStream.finished, module2.exports.destroy = CustomStream.destroy, module2.exports.destroy = originalDestroy, module2.exports.pipeline = CustomStream.pipeline, module2.exports.compose = CustomStream.compose, Object.defineProperty(CustomStream, "promises", { configurable: true, enumerable: true, get() { + return promises; + } }), module2.exports.Stream = CustomStream.Stream; + } + module2.exports.default = module2.exports; }); - wt = {}; - or(wt, { default: () => Ih }); - pe(wt, rt(Cn())); - Ih = rt(Cn()); - /*! - * The buffer module from node.js, for the browser. - * - * @author Feross Aboukhadijeh - * @license MIT - */ - /*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh */ - /*! safe-buffer. MIT License. Feross Aboukhadijeh */ + module.exports = require_ours(); }); // node_modules/jszip/lib/support.js @@ -4185,7 +5703,7 @@ var require_support = __commonJS((exports) => { var Builder; var builder; try { - exports.nodestream = !!(init_stream(), __toCommonJS(exports_stream)).Readable; + exports.nodestream = !!require_stream().Readable; } catch (e) { exports.nodestream = false; } @@ -4199,18 +5717,18 @@ var require_base64 = __commonJS((exports) => { exports.encode = function(input) { var output = []; var chr1, chr2, chr3, enc1, enc2, enc3, enc4; - var i = 0, len = input.length, remainingBytes = len; + var i2 = 0, len2 = input.length, remainingBytes = len2; var isArray = utils.getTypeOf(input) !== "string"; - while (i < input.length) { - remainingBytes = len - i; + while (i2 < input.length) { + remainingBytes = len2 - i2; if (!isArray) { - chr1 = input.charCodeAt(i++); - chr2 = i < len ? input.charCodeAt(i++) : 0; - chr3 = i < len ? input.charCodeAt(i++) : 0; + chr1 = input.charCodeAt(i2++); + chr2 = i2 < len2 ? input.charCodeAt(i2++) : 0; + chr3 = i2 < len2 ? input.charCodeAt(i2++) : 0; } else { - chr1 = input[i++]; - chr2 = i < len ? input[i++] : 0; - chr3 = i < len ? input[i++] : 0; + chr1 = input[i2++]; + chr2 = i2 < len2 ? input[i2++] : 0; + chr3 = i2 < len2 ? input[i2++] : 0; } enc1 = chr1 >> 2; enc2 = (chr1 & 3) << 4 | chr2 >> 4; @@ -4223,7 +5741,7 @@ var require_base64 = __commonJS((exports) => { exports.decode = function(input) { var chr1, chr2, chr3; var enc1, enc2, enc3, enc4; - var i = 0, resultIndex = 0; + var i2 = 0, resultIndex = 0; var dataUrlPrefix = "data:"; if (input.substr(0, dataUrlPrefix.length) === dataUrlPrefix) { throw new Error("Invalid base64 input, it looks like a data url."); @@ -4245,11 +5763,11 @@ var require_base64 = __commonJS((exports) => { } else { output = new Array(totalLength | 0); } - while (i < input.length) { - enc1 = _keyStr.indexOf(input.charAt(i++)); - enc2 = _keyStr.indexOf(input.charAt(i++)); - enc3 = _keyStr.indexOf(input.charAt(i++)); - enc4 = _keyStr.indexOf(input.charAt(i++)); + while (i2 < input.length) { + enc1 = _keyStr.indexOf(input.charAt(i2++)); + enc2 = _keyStr.indexOf(input.charAt(i2++)); + enc3 = _keyStr.indexOf(input.charAt(i2++)); + enc4 = _keyStr.indexOf(input.charAt(i2++)); chr1 = enc1 << 2 | enc2 >> 4; chr2 = (enc2 & 15) << 4 | enc3 >> 2; chr3 = (enc3 & 3) << 6 | enc4; @@ -4343,16 +5861,16 @@ var require_browser = __commonJS((exports, module) => { var queue = []; function nextTick() { draining = true; - var i, oldQueue; - var len = queue.length; - while (len) { + var i2, oldQueue; + var len2 = queue.length; + while (len2) { oldQueue = queue; queue = []; - i = -1; - while (++i < len) { - oldQueue[i](); + i2 = -1; + while (++i2 < len2) { + oldQueue[i2](); } - len = queue.length; + len2 = queue.length; } draining = false; } @@ -4367,8 +5885,7 @@ var require_browser = __commonJS((exports, module) => { // node_modules/lie/lib/browser.js var require_browser2 = __commonJS((exports, module) => { var immediate = require_browser(); - function INTERNAL() { - } + function INTERNAL() {} var handlers = {}; var REJECTED = ["REJECTED"]; var FULFILLED = ["FULFILLED"]; @@ -4469,10 +5986,10 @@ var require_browser2 = __commonJS((exports, module) => { } else { self2.state = FULFILLED; self2.outcome = value; - var i = -1; - var len = self2.queue.length; - while (++i < len) { - self2.queue[i].callFulfilled(value); + var i2 = -1; + var len2 = self2.queue.length; + while (++i2 < len2) { + self2.queue[i2].callFulfilled(value); } } return self2; @@ -4480,10 +5997,10 @@ var require_browser2 = __commonJS((exports, module) => { handlers.reject = function(self2, error) { self2.state = REJECTED; self2.outcome = error; - var i = -1; - var len = self2.queue.length; - while (++i < len) { - self2.queue[i].callRejected(error); + var i2 = -1; + var len2 = self2.queue.length; + while (++i2 < len2) { + self2.queue[i2].callRejected(error); } return self2; }; @@ -4548,20 +6065,20 @@ var require_browser2 = __commonJS((exports, module) => { if (Object.prototype.toString.call(iterable) !== "[object Array]") { return this.reject(new TypeError("must be an array")); } - var len = iterable.length; + var len2 = iterable.length; var called = false; - if (!len) { + if (!len2) { return this.resolve([]); } - var values = new Array(len); + var values = new Array(len2); var resolved = 0; - var i = -1; + var i2 = -1; var promise = new this(INTERNAL); - while (++i < len) { - allResolver(iterable[i], i); + while (++i2 < len2) { + allResolver(iterable[i2], i2); } return promise; - function allResolver(value, i2) { + function allResolver(value, i3) { self2.resolve(value).then(resolveFromAll, function(error) { if (!called) { called = true; @@ -4569,8 +6086,8 @@ var require_browser2 = __commonJS((exports, module) => { } }); function resolveFromAll(outValue) { - values[i2] = outValue; - if (++resolved === len && !called) { + values[i3] = outValue; + if (++resolved === len2 && !called) { called = true; handlers.resolve(promise, values); } @@ -4583,15 +6100,15 @@ var require_browser2 = __commonJS((exports, module) => { if (Object.prototype.toString.call(iterable) !== "[object Array]") { return this.reject(new TypeError("must be an array")); } - var len = iterable.length; + var len2 = iterable.length; var called = false; - if (!len) { + if (!len2) { return this.resolve([]); } - var i = -1; + var i2 = -1; var promise = new this(INTERNAL); - while (++i < len) { - resolver(iterable[i]); + while (++i2 < len2) { + resolver(iterable[i2]); } return promise; function resolver(value) { @@ -4639,8 +6156,8 @@ var require_setImmediate = __commonJS((exports) => { callback = new Function("" + callback); } var args = new Array(arguments.length - 1); - for (var i = 0;i < args.length; i++) { - args[i] = arguments[i + 1]; + for (var i2 = 0;i2 < args.length; i2++) { + args[i2] = arguments[i2 + 1]; } var task = { callback, args }; tasksByHandle[nextHandle] = task; @@ -4805,22 +6322,22 @@ var require_utils = __commonJS((exports) => { return input; } function stringToArrayLike(str, array) { - for (var i = 0;i < str.length; ++i) { - array[i] = str.charCodeAt(i) & 255; + for (var i2 = 0;i2 < str.length; ++i2) { + array[i2] = str.charCodeAt(i2) & 255; } return array; } var arrayToStringHelper = { stringifyByChunk: function(array, type, chunk) { - var result = [], k = 0, len = array.length; - if (len <= chunk) { + var result = [], k = 0, len2 = array.length; + if (len2 <= chunk) { return String.fromCharCode.apply(null, array); } - while (k < len) { + while (k < len2) { if (type === "array" || type === "nodebuffer") { - result.push(String.fromCharCode.apply(null, array.slice(k, Math.min(k + chunk, len)))); + result.push(String.fromCharCode.apply(null, array.slice(k, Math.min(k + chunk, len2)))); } else { - result.push(String.fromCharCode.apply(null, array.subarray(k, Math.min(k + chunk, len)))); + result.push(String.fromCharCode.apply(null, array.subarray(k, Math.min(k + chunk, len2)))); } k += chunk; } @@ -4828,8 +6345,8 @@ var require_utils = __commonJS((exports) => { }, stringifyByChar: function(array) { var resultStr = ""; - for (var i = 0;i < array.length; i++) { - resultStr += String.fromCharCode(array[i]); + for (var i2 = 0;i2 < array.length; i2++) { + resultStr += String.fromCharCode(array[i2]); } return resultStr; }, @@ -4870,8 +6387,8 @@ var require_utils = __commonJS((exports) => { } exports.applyFromCharCode = arrayLikeToString; function arrayLikeToArrayLike(arrayFrom, arrayTo) { - for (var i = 0;i < arrayFrom.length; i++) { - arrayTo[i] = arrayFrom[i]; + for (var i2 = 0;i2 < arrayFrom.length; i2++) { + arrayTo[i2] = arrayFrom[i2]; } return arrayTo; } @@ -4998,10 +6515,10 @@ var require_utils = __commonJS((exports) => { exports.MAX_VALUE_16BITS = 65535; exports.MAX_VALUE_32BITS = -1; exports.pretty = function(str) { - var res = "", code, i; - for (i = 0;i < (str || "").length; i++) { - code = str.charCodeAt(i); - res += "\\x" + (code < 16 ? "0" : "") + code.toString(16).toUpperCase(); + var res = "", code2, i2; + for (i2 = 0;i2 < (str || "").length; i2++) { + code2 = str.charCodeAt(i2); + res += "\\x" + (code2 < 16 ? "0" : "") + code2.toString(16).toUpperCase(); } return res; }; @@ -5011,17 +6528,16 @@ var require_utils = __commonJS((exports) => { }); }; exports.inherits = function(ctor, superCtor) { - var Obj = function() { - }; + var Obj = function() {}; Obj.prototype = superCtor.prototype; ctor.prototype = new Obj; }; exports.extend = function() { - var result = {}, i, attr; - for (i = 0;i < arguments.length; i++) { - for (attr in arguments[i]) { - if (Object.prototype.hasOwnProperty.call(arguments[i], attr) && typeof result[attr] === "undefined") { - result[attr] = arguments[i][attr]; + var result = {}, i2, attr; + for (i2 = 0;i2 < arguments.length; i2++) { + for (attr in arguments[i2]) { + if (Object.prototype.hasOwnProperty.call(arguments[i2], attr) && typeof result[attr] === "undefined") { + result[attr] = arguments[i2][attr]; } } } @@ -5127,8 +6643,8 @@ var require_GenericWorker = __commonJS((exports, module) => { }, emit: function(name, arg) { if (this._listeners[name]) { - for (var i = 0;i < this._listeners[name].length; i++) { - this._listeners[name][i].call(this, arg); + for (var i2 = 0;i2 < this._listeners[name].length; i2++) { + this._listeners[name][i2].call(this, arg); } } }, @@ -5179,8 +6695,7 @@ var require_GenericWorker = __commonJS((exports, module) => { } return !withError; }, - flush: function() { - }, + flush: function() {}, processChunk: function(chunk) { this.push(chunk); }, @@ -5225,13 +6740,13 @@ var require_utf8 = __commonJS((exports) => { var nodejsUtils = require_nodejsUtils(); var GenericWorker = require_GenericWorker(); var _utf8len = new Array(256); - for (i = 0;i < 256; i++) { - _utf8len[i] = i >= 252 ? 6 : i >= 248 ? 5 : i >= 240 ? 4 : i >= 224 ? 3 : i >= 192 ? 2 : 1; + for (i2 = 0;i2 < 256; i2++) { + _utf8len[i2] = i2 >= 252 ? 6 : i2 >= 248 ? 5 : i2 >= 240 ? 4 : i2 >= 224 ? 3 : i2 >= 192 ? 2 : 1; } - var i; + var i2; _utf8len[254] = _utf8len[254] = 1; var string2buf = function(str) { - var buf, c, c2, m_pos, i2, str_len = str.length, buf_len = 0; + var buf, c, c2, m_pos, i3, str_len = str.length, buf_len = 0; for (m_pos = 0;m_pos < str_len; m_pos++) { c = str.charCodeAt(m_pos); if ((c & 64512) === 55296 && m_pos + 1 < str_len) { @@ -5248,7 +6763,7 @@ var require_utf8 = __commonJS((exports) => { } else { buf = new Array(buf_len); } - for (i2 = 0, m_pos = 0;i2 < buf_len; m_pos++) { + for (i3 = 0, m_pos = 0;i3 < buf_len; m_pos++) { c = str.charCodeAt(m_pos); if ((c & 64512) === 55296 && m_pos + 1 < str_len) { c2 = str.charCodeAt(m_pos + 1); @@ -5258,19 +6773,19 @@ var require_utf8 = __commonJS((exports) => { } } if (c < 128) { - buf[i2++] = c; + buf[i3++] = c; } else if (c < 2048) { - buf[i2++] = 192 | c >>> 6; - buf[i2++] = 128 | c & 63; + buf[i3++] = 192 | c >>> 6; + buf[i3++] = 128 | c & 63; } else if (c < 65536) { - buf[i2++] = 224 | c >>> 12; - buf[i2++] = 128 | c >>> 6 & 63; - buf[i2++] = 128 | c & 63; + buf[i3++] = 224 | c >>> 12; + buf[i3++] = 128 | c >>> 6 & 63; + buf[i3++] = 128 | c & 63; } else { - buf[i2++] = 240 | c >>> 18; - buf[i2++] = 128 | c >>> 12 & 63; - buf[i2++] = 128 | c >>> 6 & 63; - buf[i2++] = 128 | c & 63; + buf[i3++] = 240 | c >>> 18; + buf[i3++] = 128 | c >>> 12 & 63; + buf[i3++] = 128 | c >>> 6 & 63; + buf[i3++] = 128 | c & 63; } } return buf; @@ -5294,11 +6809,11 @@ var require_utf8 = __commonJS((exports) => { return pos + _utf8len[buf[pos]] > max ? pos : max; }; var buf2string = function(buf) { - var i2, out, c, c_len; - var len = buf.length; - var utf16buf = new Array(len * 2); - for (out = 0, i2 = 0;i2 < len; ) { - c = buf[i2++]; + var i3, out, c, c_len; + var len2 = buf.length; + var utf16buf = new Array(len2 * 2); + for (out = 0, i3 = 0;i3 < len2; ) { + c = buf[i3++]; if (c < 128) { utf16buf[out++] = c; continue; @@ -5306,12 +6821,12 @@ var require_utf8 = __commonJS((exports) => { c_len = _utf8len[c]; if (c_len > 4) { utf16buf[out++] = 65533; - i2 += c_len - 1; + i3 += c_len - 1; continue; } c &= c_len === 2 ? 31 : c_len === 3 ? 15 : 7; - while (c_len > 1 && i2 < len) { - c = c << 6 | buf[i2++] & 63; + while (c_len > 1 && i3 < len2) { + c = c << 6 | buf[i3++] & 63; c_len--; } if (c_len > 1) { @@ -5425,7 +6940,7 @@ var require_ConvertWorker = __commonJS((exports, module) => { // node_modules/jszip/lib/nodejs/NodejsStreamOutputAdapter.js var require_NodejsStreamOutputAdapter = __commonJS((exports, module) => { - var Readable = (init_stream(), __toCommonJS(exports_stream)).Readable; + var Readable = require_stream().Readable; var utils = require_utils(); utils.inherits(NodejsStreamOutputAdapter, Readable); function NodejsStreamOutputAdapter(helper, options, updateCb) { @@ -5463,8 +6978,7 @@ var require_StreamHelper = __commonJS((exports, module) => { if (support.nodestream) { try { NodejsStreamOutputAdapter = require_NodejsStreamOutputAdapter(); - } catch (e) { - } + } catch (e) {} } function transformZipOutput(type, content, mimeType) { switch (type) { @@ -5476,10 +6990,10 @@ var require_StreamHelper = __commonJS((exports, module) => { return utils.transformTo(type, content); } } - function concat(type, dataArray) { - var i, index = 0, res = null, totalLength = 0; - for (i = 0;i < dataArray.length; i++) { - totalLength += dataArray[i].length; + function concat2(type, dataArray) { + var i2, index = 0, res = null, totalLength = 0; + for (i2 = 0;i2 < dataArray.length; i2++) { + totalLength += dataArray[i2].length; } switch (type) { case "string": @@ -5488,9 +7002,9 @@ var require_StreamHelper = __commonJS((exports, module) => { return Array.prototype.concat.apply([], dataArray); case "uint8array": res = new Uint8Array(totalLength); - for (i = 0;i < dataArray.length; i++) { - res.set(dataArray[i], index); - index += dataArray[i].length; + for (i2 = 0;i2 < dataArray.length; i2++) { + res.set(dataArray[i2], index); + index += dataArray[i2].length; } return res; case "nodebuffer": @@ -5513,7 +7027,7 @@ var require_StreamHelper = __commonJS((exports, module) => { reject(err); }).on("end", function() { try { - var result = transformZipOutput(resultType, concat(chunkType, dataArray), mimeType); + var result = transformZipOutput(resultType, concat2(chunkType, dataArray), mimeType); resolve(result); } catch (e) { reject(e); @@ -5697,19 +7211,19 @@ var require_crc32 = __commonJS((exports, module) => { return table; } var crcTable = makeTable(); - function crc32(crc, buf, len, pos) { - var t = crcTable, end = pos + len; + function crc32(crc, buf, len2, pos) { + var t = crcTable, end = pos + len2; crc = crc ^ -1; - for (var i = pos;i < end; i++) { - crc = crc >>> 8 ^ t[(crc ^ buf[i]) & 255]; + for (var i2 = pos;i2 < end; i2++) { + crc = crc >>> 8 ^ t[(crc ^ buf[i2]) & 255]; } return crc ^ -1; } - function crc32str(crc, str, len, pos) { - var t = crcTable, end = pos + len; + function crc32str(crc, str, len2, pos) { + var t = crcTable, end = pos + len2; crc = crc ^ -1; - for (var i = pos;i < end; i++) { - crc = crc >>> 8 ^ t[(crc ^ str.charCodeAt(i)) & 255]; + for (var i2 = pos;i2 < end; i2++) { + crc = crc >>> 8 ^ t[(crc ^ str.charCodeAt(i2)) & 255]; } return crc ^ -1; } @@ -5875,10 +7389,10 @@ var require_zipObject = __commonJS((exports, module) => { var removedFn = function() { throw new Error("This method has been removed in JSZip 3.0, please check the upgrade guide."); }; - for (i = 0;i < removedMethods.length; i++) { - ZipObject.prototype[removedMethods[i]] = removedFn; + for (i2 = 0;i2 < removedMethods.length; i2++) { + ZipObject.prototype[removedMethods[i2]] = removedFn; } - var i; + var i2; module.exports = ZipObject; }); @@ -5917,25 +7431,25 @@ var require_common = __commonJS((exports) => { return buf; }; var fnTyped = { - arraySet: function(dest, src, src_offs, len, dest_offs) { + arraySet: function(dest, src, src_offs, len2, dest_offs) { if (src.subarray && dest.subarray) { - dest.set(src.subarray(src_offs, src_offs + len), dest_offs); + dest.set(src.subarray(src_offs, src_offs + len2), dest_offs); return; } - for (var i = 0;i < len; i++) { - dest[dest_offs + i] = src[src_offs + i]; + for (var i2 = 0;i2 < len2; i2++) { + dest[dest_offs + i2] = src[src_offs + i2]; } }, flattenChunks: function(chunks) { - var i, l, len, pos, chunk, result; - len = 0; - for (i = 0, l = chunks.length;i < l; i++) { - len += chunks[i].length; + var i2, l, len2, pos, chunk, result; + len2 = 0; + for (i2 = 0, l = chunks.length;i2 < l; i2++) { + len2 += chunks[i2].length; } - result = new Uint8Array(len); + result = new Uint8Array(len2); pos = 0; - for (i = 0, l = chunks.length;i < l; i++) { - chunk = chunks[i]; + for (i2 = 0, l = chunks.length;i2 < l; i2++) { + chunk = chunks[i2]; result.set(chunk, pos); pos += chunk.length; } @@ -5943,9 +7457,9 @@ var require_common = __commonJS((exports) => { } }; var fnUntyped = { - arraySet: function(dest, src, src_offs, len, dest_offs) { - for (var i = 0;i < len; i++) { - dest[dest_offs + i] = src[src_offs + i]; + arraySet: function(dest, src, src_offs, len2, dest_offs) { + for (var i2 = 0;i2 < len2; i2++) { + dest[dest_offs + i2] = src[src_offs + i2]; } }, flattenChunks: function(chunks) { @@ -5976,9 +7490,9 @@ var require_trees = __commonJS((exports) => { var Z_TEXT = 1; var Z_UNKNOWN = 2; function zero(buf) { - var len = buf.length; - while (--len >= 0) { - buf[len] = 0; + var len2 = buf.length; + while (--len2 >= 0) { + buf[len2] = 0; } } var STORED_BLOCK = 0; @@ -6053,13 +7567,13 @@ var require_trees = __commonJS((exports) => { function send_code(s, c, tree) { send_bits(s, tree[c * 2], tree[c * 2 + 1]); } - function bi_reverse(code, len) { + function bi_reverse(code2, len2) { var res = 0; do { - res |= code & 1; - code >>>= 1; + res |= code2 & 1; + code2 >>>= 1; res <<= 1; - } while (--len > 0); + } while (--len2 > 0); return res >>> 1; } function bi_flush(s) { @@ -6143,47 +7657,47 @@ var require_trees = __commonJS((exports) => { } function gen_codes(tree, max_code, bl_count) { var next_code = new Array(MAX_BITS + 1); - var code = 0; + var code2 = 0; var bits; var n; for (bits = 1;bits <= MAX_BITS; bits++) { - next_code[bits] = code = code + bl_count[bits - 1] << 1; + next_code[bits] = code2 = code2 + bl_count[bits - 1] << 1; } for (n = 0;n <= max_code; n++) { - var len = tree[n * 2 + 1]; - if (len === 0) { + var len2 = tree[n * 2 + 1]; + if (len2 === 0) { continue; } - tree[n * 2] = bi_reverse(next_code[len]++, len); + tree[n * 2] = bi_reverse(next_code[len2]++, len2); } } function tr_static_init() { var n; var bits; var length; - var code; + var code2; var dist; var bl_count = new Array(MAX_BITS + 1); length = 0; - for (code = 0;code < LENGTH_CODES - 1; code++) { - base_length[code] = length; - for (n = 0;n < 1 << extra_lbits[code]; n++) { - _length_code[length++] = code; + for (code2 = 0;code2 < LENGTH_CODES - 1; code2++) { + base_length[code2] = length; + for (n = 0;n < 1 << extra_lbits[code2]; n++) { + _length_code[length++] = code2; } } - _length_code[length - 1] = code; + _length_code[length - 1] = code2; dist = 0; - for (code = 0;code < 16; code++) { - base_dist[code] = dist; - for (n = 0;n < 1 << extra_dbits[code]; n++) { - _dist_code[dist++] = code; + for (code2 = 0;code2 < 16; code2++) { + base_dist[code2] = dist; + for (n = 0;n < 1 << extra_dbits[code2]; n++) { + _dist_code[dist++] = code2; } } dist >>= 7; - for (;code < D_CODES; code++) { - base_dist[code] = dist << 7; - for (n = 0;n < 1 << extra_dbits[code] - 7; n++) { - _dist_code[256 + dist++] = code; + for (;code2 < D_CODES; code2++) { + base_dist[code2] = dist << 7; + for (n = 0;n < 1 << extra_dbits[code2] - 7; n++) { + _dist_code[256 + dist++] = code2; } } for (bits = 0;bits <= MAX_BITS; bits++) { @@ -6243,14 +7757,14 @@ var require_trees = __commonJS((exports) => { s.bi_buf = 0; s.bi_valid = 0; } - function copy_block(s, buf, len, header) { + function copy_block(s, buf, len2, header) { bi_windup(s); if (header) { - put_short(s, len); - put_short(s, ~len); + put_short(s, len2); + put_short(s, ~len2); } - utils.arraySet(s.pending_buf, s.window, buf, len, s.pending); - s.pending += len; + utils.arraySet(s.pending_buf, s.window, buf, len2, s.pending); + s.pending += len2; } function smaller(tree, n, m, depth) { var _n2 = n * 2; @@ -6277,7 +7791,7 @@ var require_trees = __commonJS((exports) => { var dist; var lc; var lx = 0; - var code; + var code2; var extra; if (s.last_lit !== 0) { do { @@ -6287,19 +7801,19 @@ var require_trees = __commonJS((exports) => { if (dist === 0) { send_code(s, lc, ltree); } else { - code = _length_code[lc]; - send_code(s, code + LITERALS + 1, ltree); - extra = extra_lbits[code]; + code2 = _length_code[lc]; + send_code(s, code2 + LITERALS + 1, ltree); + extra = extra_lbits[code2]; if (extra !== 0) { - lc -= base_length[code]; + lc -= base_length[code2]; send_bits(s, lc, extra); } dist--; - code = d_code(dist); - send_code(s, code, dtree); - extra = extra_dbits[code]; + code2 = d_code(dist); + send_code(s, code2, dtree); + extra = extra_dbits[code2]; if (extra !== 0) { - dist -= base_dist[code]; + dist -= base_dist[code2]; send_bits(s, dist, extra); } } @@ -6570,11 +8084,11 @@ var require_trees = __commonJS((exports) => { // node_modules/pako/lib/zlib/adler32.js var require_adler32 = __commonJS((exports, module) => { - function adler32(adler, buf, len, pos) { + function adler32(adler, buf, len2, pos) { var s1 = adler & 65535 | 0, s2 = adler >>> 16 & 65535 | 0, n = 0; - while (len !== 0) { - n = len > 2000 ? 2000 : len; - len -= n; + while (len2 !== 0) { + n = len2 > 2000 ? 2000 : len2; + len2 -= n; do { s1 = s1 + buf[pos++] | 0; s2 = s2 + s1 | 0; @@ -6601,11 +8115,11 @@ var require_crc322 = __commonJS((exports, module) => { return table; } var crcTable = makeTable(); - function crc32(crc, buf, len, pos) { - var t = crcTable, end = pos + len; + function crc32(crc, buf, len2, pos) { + var t = crcTable, end = pos + len2; crc ^= -1; - for (var i = pos;i < end; i++) { - crc = crc >>> 8 ^ t[(crc ^ buf[i]) & 255]; + for (var i2 = pos;i2 < end; i2++) { + crc = crc >>> 8 ^ t[(crc ^ buf[i2]) & 255]; } return crc ^ -1; } @@ -6686,26 +8200,26 @@ var require_deflate = __commonJS((exports) => { return (f << 1) - (f > 4 ? 9 : 0); } function zero(buf) { - var len = buf.length; - while (--len >= 0) { - buf[len] = 0; + var len2 = buf.length; + while (--len2 >= 0) { + buf[len2] = 0; } } function flush_pending(strm) { var s = strm.state; - var len = s.pending; - if (len > strm.avail_out) { - len = strm.avail_out; + var len2 = s.pending; + if (len2 > strm.avail_out) { + len2 = strm.avail_out; } - if (len === 0) { + if (len2 === 0) { return; } - utils.arraySet(strm.output, s.pending_buf, s.pending_out, len, strm.next_out); - strm.next_out += len; - s.pending_out += len; - strm.total_out += len; - strm.avail_out -= len; - s.pending -= len; + utils.arraySet(strm.output, s.pending_buf, s.pending_out, len2, strm.next_out); + strm.next_out += len2; + s.pending_out += len2; + strm.total_out += len2; + strm.avail_out -= len2; + s.pending -= len2; if (s.pending === 0) { s.pending_out = 0; } @@ -6723,29 +8237,29 @@ var require_deflate = __commonJS((exports) => { s.pending_buf[s.pending++] = b & 255; } function read_buf(strm, buf, start, size) { - var len = strm.avail_in; - if (len > size) { - len = size; + var len2 = strm.avail_in; + if (len2 > size) { + len2 = size; } - if (len === 0) { + if (len2 === 0) { return 0; } - strm.avail_in -= len; - utils.arraySet(buf, strm.input, strm.next_in, len, start); + strm.avail_in -= len2; + utils.arraySet(buf, strm.input, strm.next_in, len2, start); if (strm.state.wrap === 1) { - strm.adler = adler32(strm.adler, buf, len, start); + strm.adler = adler32(strm.adler, buf, len2, start); } else if (strm.state.wrap === 2) { - strm.adler = crc32(strm.adler, buf, len, start); + strm.adler = crc32(strm.adler, buf, len2, start); } - strm.next_in += len; - strm.total_in += len; - return len; + strm.next_in += len2; + strm.total_in += len2; + return len2; } function longest_match(s, cur_match) { var chain_length = s.max_chain_length; var scan = s.strstart; var match; - var len; + var len2; var best_len = s.prev_length; var nice_match = s.nice_match; var limit = s.strstart > s.w_size - MIN_LOOKAHEAD ? s.strstart - (s.w_size - MIN_LOOKAHEAD) : 0; @@ -6768,14 +8282,13 @@ var require_deflate = __commonJS((exports) => { } scan += 2; match++; - do { - } while (_win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && scan < strend); - len = MAX_MATCH - (strend - scan); + do {} while (_win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && scan < strend); + len2 = MAX_MATCH - (strend - scan); scan = strend - MAX_MATCH; - if (len > best_len) { + if (len2 > best_len) { s.match_start = cur_match; - best_len = len; - if (len >= nice_match) { + best_len = len2; + if (len2 >= nice_match) { break; } scan_end1 = _win[scan + best_len - 1]; @@ -7057,8 +8570,7 @@ var require_deflate = __commonJS((exports) => { prev = _win[scan]; if (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan]) { strend = s.strstart + MAX_MATCH; - do { - } while (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && scan < strend); + do {} while (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && scan < strend); s.match_length = MAX_MATCH - (strend - scan); if (s.match_length > s.lookahead) { s.match_length = s.lookahead; @@ -7675,13 +9187,13 @@ var require_strings = __commonJS((exports) => { STR_APPLY_UIA_OK = false; } var _utf8len = new utils.Buf8(256); - for (q2 = 0;q2 < 256; q2++) { - _utf8len[q2] = q2 >= 252 ? 6 : q2 >= 248 ? 5 : q2 >= 240 ? 4 : q2 >= 224 ? 3 : q2 >= 192 ? 2 : 1; + for (q = 0;q < 256; q++) { + _utf8len[q] = q >= 252 ? 6 : q >= 248 ? 5 : q >= 240 ? 4 : q >= 224 ? 3 : q >= 192 ? 2 : 1; } - var q2; + var q; _utf8len[254] = _utf8len[254] = 1; exports.string2buf = function(str) { - var buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0; + var buf, c, c2, m_pos, i2, str_len = str.length, buf_len = 0; for (m_pos = 0;m_pos < str_len; m_pos++) { c = str.charCodeAt(m_pos); if ((c & 64512) === 55296 && m_pos + 1 < str_len) { @@ -7694,7 +9206,7 @@ var require_strings = __commonJS((exports) => { buf_len += c < 128 ? 1 : c < 2048 ? 2 : c < 65536 ? 3 : 4; } buf = new utils.Buf8(buf_len); - for (i = 0, m_pos = 0;i < buf_len; m_pos++) { + for (i2 = 0, m_pos = 0;i2 < buf_len; m_pos++) { c = str.charCodeAt(m_pos); if ((c & 64512) === 55296 && m_pos + 1 < str_len) { c2 = str.charCodeAt(m_pos + 1); @@ -7704,32 +9216,32 @@ var require_strings = __commonJS((exports) => { } } if (c < 128) { - buf[i++] = c; + buf[i2++] = c; } else if (c < 2048) { - buf[i++] = 192 | c >>> 6; - buf[i++] = 128 | c & 63; + buf[i2++] = 192 | c >>> 6; + buf[i2++] = 128 | c & 63; } else if (c < 65536) { - buf[i++] = 224 | c >>> 12; - buf[i++] = 128 | c >>> 6 & 63; - buf[i++] = 128 | c & 63; + buf[i2++] = 224 | c >>> 12; + buf[i2++] = 128 | c >>> 6 & 63; + buf[i2++] = 128 | c & 63; } else { - buf[i++] = 240 | c >>> 18; - buf[i++] = 128 | c >>> 12 & 63; - buf[i++] = 128 | c >>> 6 & 63; - buf[i++] = 128 | c & 63; + buf[i2++] = 240 | c >>> 18; + buf[i2++] = 128 | c >>> 12 & 63; + buf[i2++] = 128 | c >>> 6 & 63; + buf[i2++] = 128 | c & 63; } } return buf; }; - function buf2binstring(buf, len) { - if (len < 65534) { + function buf2binstring(buf, len2) { + if (len2 < 65534) { if (buf.subarray && STR_APPLY_UIA_OK || !buf.subarray && STR_APPLY_OK) { - return String.fromCharCode.apply(null, utils.shrinkBuf(buf, len)); + return String.fromCharCode.apply(null, utils.shrinkBuf(buf, len2)); } } var result = ""; - for (var i = 0;i < len; i++) { - result += String.fromCharCode(buf[i]); + for (var i2 = 0;i2 < len2; i2++) { + result += String.fromCharCode(buf[i2]); } return result; } @@ -7738,17 +9250,17 @@ var require_strings = __commonJS((exports) => { }; exports.binstring2buf = function(str) { var buf = new utils.Buf8(str.length); - for (var i = 0, len = buf.length;i < len; i++) { - buf[i] = str.charCodeAt(i); + for (var i2 = 0, len2 = buf.length;i2 < len2; i2++) { + buf[i2] = str.charCodeAt(i2); } return buf; }; exports.buf2string = function(buf, max) { - var i, out, c, c_len; - var len = max || buf.length; - var utf16buf = new Array(len * 2); - for (out = 0, i = 0;i < len; ) { - c = buf[i++]; + var i2, out, c, c_len; + var len2 = max || buf.length; + var utf16buf = new Array(len2 * 2); + for (out = 0, i2 = 0;i2 < len2; ) { + c = buf[i2++]; if (c < 128) { utf16buf[out++] = c; continue; @@ -7756,12 +9268,12 @@ var require_strings = __commonJS((exports) => { c_len = _utf8len[c]; if (c_len > 4) { utf16buf[out++] = 65533; - i += c_len - 1; + i2 += c_len - 1; continue; } c &= c_len === 2 ? 31 : c_len === 3 ? 15 : 7; - while (c_len > 1 && i < len) { - c = c << 6 | buf[i++] & 63; + while (c_len > 1 && i2 < len2) { + c = c << 6 | buf[i2++] & 63; c_len--; } if (c_len > 1) { @@ -7824,7 +9336,7 @@ var require_deflate2 = __commonJS((exports) => { var strings = require_strings(); var msg = require_messages(); var ZStream = require_zstream(); - var toString = Object.prototype.toString; + var toString2 = Object.prototype.toString; var Z_NO_FLUSH = 0; var Z_FINISH = 4; var Z_OK = 0; @@ -7868,7 +9380,7 @@ var require_deflate2 = __commonJS((exports) => { var dict; if (typeof opt.dictionary === "string") { dict = strings.string2buf(opt.dictionary); - } else if (toString.call(opt.dictionary) === "[object ArrayBuffer]") { + } else if (toString2.call(opt.dictionary) === "[object ArrayBuffer]") { dict = new Uint8Array(opt.dictionary); } else { dict = opt.dictionary; @@ -7890,7 +9402,7 @@ var require_deflate2 = __commonJS((exports) => { _mode = mode === ~~mode ? mode : mode === true ? Z_FINISH : Z_NO_FLUSH; if (typeof data === "string") { strm.input = strings.string2buf(data); - } else if (toString.call(data) === "[object ArrayBuffer]") { + } else if (toString2.call(data) === "[object ArrayBuffer]") { strm.input = new Uint8Array(data); } else { strm.input = data; @@ -7993,9 +9505,9 @@ var require_inffast = __commonJS((exports, module) => { var dmask; var here; var op; - var len; + var len2; var dist; - var from; + var from2; var from_source; var input, output; state = strm.state; @@ -8035,14 +9547,14 @@ var require_inffast = __commonJS((exports, module) => { if (op === 0) { output[_out++] = here & 65535; } else if (op & 16) { - len = here & 65535; + len2 = here & 65535; op &= 15; if (op) { if (bits < op) { hold += input[_in++] << bits; bits += 8; } - len += hold & (1 << op) - 1; + len2 += hold & (1 << op) - 1; hold >>>= op; bits -= op; } @@ -8088,72 +9600,72 @@ var require_inffast = __commonJS((exports, module) => { break top; } } - from = 0; + from2 = 0; from_source = s_window; if (wnext === 0) { - from += wsize - op; - if (op < len) { - len -= op; + from2 += wsize - op; + if (op < len2) { + len2 -= op; do { - output[_out++] = s_window[from++]; + output[_out++] = s_window[from2++]; } while (--op); - from = _out - dist; + from2 = _out - dist; from_source = output; } } else if (wnext < op) { - from += wsize + wnext - op; + from2 += wsize + wnext - op; op -= wnext; - if (op < len) { - len -= op; + if (op < len2) { + len2 -= op; do { - output[_out++] = s_window[from++]; + output[_out++] = s_window[from2++]; } while (--op); - from = 0; - if (wnext < len) { + from2 = 0; + if (wnext < len2) { op = wnext; - len -= op; + len2 -= op; do { - output[_out++] = s_window[from++]; + output[_out++] = s_window[from2++]; } while (--op); - from = _out - dist; + from2 = _out - dist; from_source = output; } } } else { - from += wnext - op; - if (op < len) { - len -= op; + from2 += wnext - op; + if (op < len2) { + len2 -= op; do { - output[_out++] = s_window[from++]; + output[_out++] = s_window[from2++]; } while (--op); - from = _out - dist; + from2 = _out - dist; from_source = output; } } - while (len > 2) { - output[_out++] = from_source[from++]; - output[_out++] = from_source[from++]; - output[_out++] = from_source[from++]; - len -= 3; + while (len2 > 2) { + output[_out++] = from_source[from2++]; + output[_out++] = from_source[from2++]; + output[_out++] = from_source[from2++]; + len2 -= 3; } - if (len) { - output[_out++] = from_source[from++]; - if (len > 1) { - output[_out++] = from_source[from++]; + if (len2) { + output[_out++] = from_source[from2++]; + if (len2 > 1) { + output[_out++] = from_source[from2++]; } } } else { - from = _out - dist; + from2 = _out - dist; do { - output[_out++] = output[from++]; - output[_out++] = output[from++]; - output[_out++] = output[from++]; - len -= 3; - } while (len > 2); - if (len) { - output[_out++] = output[from++]; - if (len > 1) { - output[_out++] = output[from++]; + output[_out++] = output[from2++]; + output[_out++] = output[from2++]; + output[_out++] = output[from2++]; + len2 -= 3; + } while (len2 > 2); + if (len2) { + output[_out++] = output[from2++]; + if (len2 > 1) { + output[_out++] = output[from2++]; } } } @@ -8181,9 +9693,9 @@ var require_inffast = __commonJS((exports, module) => { break; } } while (_in < last && _out < end); - len = bits >> 3; - _in -= len; - bits -= len << 3; + len2 = bits >> 3; + _in -= len2; + bits -= len2 << 3; hold &= (1 << bits) - 1; strm.next_in = _in; strm.next_out = _out; @@ -8340,7 +9852,7 @@ var require_inftrees = __commonJS((exports, module) => { ]; module.exports = function inflate_table(type, lens, lens_index, codes, table, table_index, work, opts) { var bits = opts.bits; - var len = 0; + var len2 = 0; var sym = 0; var min = 0, max = 0; var root = 0; @@ -8350,7 +9862,7 @@ var require_inftrees = __commonJS((exports, module) => { var used = 0; var huff = 0; var incr; - var fill; + var fill2; var low; var mask; var next; @@ -8362,8 +9874,8 @@ var require_inftrees = __commonJS((exports, module) => { var extra = null; var extra_index = 0; var here_bits, here_op, here_val; - for (len = 0;len <= MAXBITS; len++) { - count[len] = 0; + for (len2 = 0;len2 <= MAXBITS; len2++) { + count[len2] = 0; } for (sym = 0;sym < codes; sym++) { count[lens[lens_index + sym]]++; @@ -8392,9 +9904,9 @@ var require_inftrees = __commonJS((exports, module) => { root = min; } left = 1; - for (len = 1;len <= MAXBITS; len++) { + for (len2 = 1;len2 <= MAXBITS; len2++) { left <<= 1; - left -= count[len]; + left -= count[len2]; if (left < 0) { return -1; } @@ -8403,8 +9915,8 @@ var require_inftrees = __commonJS((exports, module) => { return -1; } offs[1] = 0; - for (len = 1;len < MAXBITS; len++) { - offs[len + 1] = offs[len] + count[len]; + for (len2 = 1;len2 < MAXBITS; len2++) { + offs[len2 + 1] = offs[len2] + count[len2]; } for (sym = 0;sym < codes; sym++) { if (lens[lens_index + sym] !== 0) { @@ -8427,7 +9939,7 @@ var require_inftrees = __commonJS((exports, module) => { } huff = 0; sym = 0; - len = min; + len2 = min; next = table_index; curr = root; drop = 0; @@ -8438,7 +9950,7 @@ var require_inftrees = __commonJS((exports, module) => { return 1; } for (;; ) { - here_bits = len - drop; + here_bits = len2 - drop; if (work[sym] < end) { here_op = 0; here_val = work[sym]; @@ -8449,14 +9961,14 @@ var require_inftrees = __commonJS((exports, module) => { here_op = 32 + 64; here_val = 0; } - incr = 1 << len - drop; - fill = 1 << curr; - min = fill; + incr = 1 << len2 - drop; + fill2 = 1 << curr; + min = fill2; do { - fill -= incr; - table[next + (huff >> drop) + fill] = here_bits << 24 | here_op << 16 | here_val | 0; - } while (fill !== 0); - incr = 1 << len - 1; + fill2 -= incr; + table[next + (huff >> drop) + fill2] = here_bits << 24 | here_op << 16 | here_val | 0; + } while (fill2 !== 0); + incr = 1 << len2 - 1; while (huff & incr) { incr >>= 1; } @@ -8467,18 +9979,18 @@ var require_inftrees = __commonJS((exports, module) => { huff = 0; } sym++; - if (--count[len] === 0) { - if (len === max) { + if (--count[len2] === 0) { + if (len2 === max) { break; } - len = lens[lens_index + work[sym]]; + len2 = lens[lens_index + work[sym]]; } - if (len > root && (huff & mask) !== low) { + if (len2 > root && (huff & mask) !== low) { if (drop === 0) { drop = root; } next += min; - curr = len - drop; + curr = len2 - drop; left = 1 << curr; while (curr + drop < max) { left -= count[curr + drop]; @@ -8497,7 +10009,7 @@ var require_inftrees = __commonJS((exports, module) => { } } if (huff !== 0) { - table[next + huff] = len - drop << 24 | 64 << 16 | 0; + table[next + huff] = len2 - drop << 24 | 64 << 16 | 0; } opts.bits = root; return 0; @@ -8561,8 +10073,8 @@ var require_inflate = __commonJS((exports) => { var ENOUGH_DISTS = 592; var MAX_WBITS = 15; var DEF_WBITS = MAX_WBITS; - function zswap32(q2) { - return (q2 >>> 24 & 255) + (q2 >>> 8 & 65280) + ((q2 & 65280) << 8) + ((q2 & 255) << 24); + function zswap32(q) { + return (q >>> 24 & 255) + (q >>> 8 & 65280) + ((q & 65280) << 8) + ((q & 255) << 24); } function InflateState() { this.mode = 0; @@ -8714,7 +10226,7 @@ var require_inflate = __commonJS((exports) => { state.distcode = distfix; state.distbits = 5; } - function updatewindow(strm, src, end, copy) { + function updatewindow(strm, src, end, copy2) { var dist; var state = strm.state; if (state.window === null) { @@ -8723,20 +10235,20 @@ var require_inflate = __commonJS((exports) => { state.whave = 0; state.window = new utils.Buf8(state.wsize); } - if (copy >= state.wsize) { + if (copy2 >= state.wsize) { utils.arraySet(state.window, src, end - state.wsize, state.wsize, 0); state.wnext = 0; state.whave = state.wsize; } else { dist = state.wsize - state.wnext; - if (dist > copy) { - dist = copy; - } - utils.arraySet(state.window, src, end - copy, dist, state.wnext); - copy -= dist; - if (copy) { - utils.arraySet(state.window, src, end - copy, copy, 0); - state.wnext = copy; + if (dist > copy2) { + dist = copy2; + } + utils.arraySet(state.window, src, end - copy2, dist, state.wnext); + copy2 -= dist; + if (copy2) { + utils.arraySet(state.window, src, end - copy2, copy2, 0); + state.wnext = copy2; state.whave = state.wsize; } else { state.wnext += dist; @@ -8759,13 +10271,13 @@ var require_inflate = __commonJS((exports) => { var hold; var bits; var _in, _out; - var copy; - var from; + var copy2; + var from2; var from_source; var here = 0; var here_bits, here_op, here_val; var last_bits, last_op, last_val; - var len; + var len2; var ret; var hbuf = new utils.Buf8(4); var opts; @@ -8831,15 +10343,15 @@ var require_inflate = __commonJS((exports) => { } hold >>>= 4; bits -= 4; - len = (hold & 15) + 8; + len2 = (hold & 15) + 8; if (state.wbits === 0) { - state.wbits = len; - } else if (len > state.wbits) { + state.wbits = len2; + } else if (len2 > state.wbits) { strm.msg = "invalid window size"; state.mode = BAD; break; } - state.dmax = 1 << len; + state.dmax = 1 << len2; strm.adler = state.check = 1; state.mode = hold & 512 ? DICTID : TYPE; hold = 0; @@ -8946,24 +10458,24 @@ var require_inflate = __commonJS((exports) => { state.mode = EXTRA; case EXTRA: if (state.flags & 1024) { - copy = state.length; - if (copy > have) { - copy = have; + copy2 = state.length; + if (copy2 > have) { + copy2 = have; } - if (copy) { + if (copy2) { if (state.head) { - len = state.head.extra_len - state.length; + len2 = state.head.extra_len - state.length; if (!state.head.extra) { state.head.extra = new Array(state.head.extra_len); } - utils.arraySet(state.head.extra, input, next, copy, len); + utils.arraySet(state.head.extra, input, next, copy2, len2); } if (state.flags & 512) { - state.check = crc32(state.check, input, copy, next); + state.check = crc32(state.check, input, copy2, next); } - have -= copy; - next += copy; - state.length -= copy; + have -= copy2; + next += copy2; + state.length -= copy2; } if (state.length) { break inf_leave; @@ -8976,19 +10488,19 @@ var require_inflate = __commonJS((exports) => { if (have === 0) { break inf_leave; } - copy = 0; + copy2 = 0; do { - len = input[next + copy++]; - if (state.head && len && state.length < 65536) { - state.head.name += String.fromCharCode(len); + len2 = input[next + copy2++]; + if (state.head && len2 && state.length < 65536) { + state.head.name += String.fromCharCode(len2); } - } while (len && copy < have); + } while (len2 && copy2 < have); if (state.flags & 512) { - state.check = crc32(state.check, input, copy, next); + state.check = crc32(state.check, input, copy2, next); } - have -= copy; - next += copy; - if (len) { + have -= copy2; + next += copy2; + if (len2) { break inf_leave; } } else if (state.head) { @@ -9001,19 +10513,19 @@ var require_inflate = __commonJS((exports) => { if (have === 0) { break inf_leave; } - copy = 0; + copy2 = 0; do { - len = input[next + copy++]; - if (state.head && len && state.length < 65536) { - state.head.comment += String.fromCharCode(len); + len2 = input[next + copy2++]; + if (state.head && len2 && state.length < 65536) { + state.head.comment += String.fromCharCode(len2); } - } while (len && copy < have); + } while (len2 && copy2 < have); if (state.flags & 512) { - state.check = crc32(state.check, input, copy, next); + state.check = crc32(state.check, input, copy2, next); } - have -= copy; - next += copy; - if (len) { + have -= copy2; + next += copy2; + if (len2) { break inf_leave; } } else if (state.head) { @@ -9141,23 +10653,23 @@ var require_inflate = __commonJS((exports) => { case COPY_: state.mode = COPY; case COPY: - copy = state.length; - if (copy) { - if (copy > have) { - copy = have; + copy2 = state.length; + if (copy2) { + if (copy2 > have) { + copy2 = have; } - if (copy > left) { - copy = left; + if (copy2 > left) { + copy2 = left; } - if (copy === 0) { + if (copy2 === 0) { break inf_leave; } - utils.arraySet(output, input, next, copy, put); - have -= copy; - next += copy; - left -= copy; - put += copy; - state.length -= copy; + utils.arraySet(output, input, next, copy2, put); + have -= copy2; + next += copy2; + left -= copy2; + put += copy2; + state.length -= copy2; break; } state.mode = TYPE; @@ -9255,8 +10767,8 @@ var require_inflate = __commonJS((exports) => { state.mode = BAD; break; } - len = state.lens[state.have - 1]; - copy = 3 + (hold & 3); + len2 = state.lens[state.have - 1]; + copy2 = 3 + (hold & 3); hold >>>= 2; bits -= 2; } else if (here_val === 17) { @@ -9271,8 +10783,8 @@ var require_inflate = __commonJS((exports) => { } hold >>>= here_bits; bits -= here_bits; - len = 0; - copy = 3 + (hold & 7); + len2 = 0; + copy2 = 3 + (hold & 7); hold >>>= 3; bits -= 3; } else { @@ -9287,18 +10799,18 @@ var require_inflate = __commonJS((exports) => { } hold >>>= here_bits; bits -= here_bits; - len = 0; - copy = 11 + (hold & 127); + len2 = 0; + copy2 = 11 + (hold & 127); hold >>>= 7; bits -= 7; } - if (state.have + copy > state.nlen + state.ndist) { + if (state.have + copy2 > state.nlen + state.ndist) { strm.msg = "invalid bit length repeat"; state.mode = BAD; break; } - while (copy--) { - state.lens[state.have++] = len; + while (copy2--) { + state.lens[state.have++] = len2; } } } @@ -9510,39 +11022,39 @@ var require_inflate = __commonJS((exports) => { if (left === 0) { break inf_leave; } - copy = _out - left; - if (state.offset > copy) { - copy = state.offset - copy; - if (copy > state.whave) { + copy2 = _out - left; + if (state.offset > copy2) { + copy2 = state.offset - copy2; + if (copy2 > state.whave) { if (state.sane) { strm.msg = "invalid distance too far back"; state.mode = BAD; break; } } - if (copy > state.wnext) { - copy -= state.wnext; - from = state.wsize - copy; + if (copy2 > state.wnext) { + copy2 -= state.wnext; + from2 = state.wsize - copy2; } else { - from = state.wnext - copy; + from2 = state.wnext - copy2; } - if (copy > state.length) { - copy = state.length; + if (copy2 > state.length) { + copy2 = state.length; } from_source = state.window; } else { from_source = output; - from = put - state.offset; - copy = state.length; + from2 = put - state.offset; + copy2 = state.length; } - if (copy > left) { - copy = left; + if (copy2 > left) { + copy2 = left; } - left -= copy; - state.length -= copy; + left -= copy2; + state.length -= copy2; do { - output[put++] = from_source[from++]; - } while (--copy); + output[put++] = from_source[from2++]; + } while (--copy2); if (state.length === 0) { state.mode = LEN; } @@ -9761,7 +11273,7 @@ var require_inflate2 = __commonJS((exports) => { var msg = require_messages(); var ZStream = require_zstream(); var GZheader = require_gzheader(); - var toString = Object.prototype.toString; + var toString2 = Object.prototype.toString; function Inflate(options) { if (!(this instanceof Inflate)) return new Inflate(options); @@ -9800,7 +11312,7 @@ var require_inflate2 = __commonJS((exports) => { if (opt.dictionary) { if (typeof opt.dictionary === "string") { opt.dictionary = strings.string2buf(opt.dictionary); - } else if (toString.call(opt.dictionary) === "[object ArrayBuffer]") { + } else if (toString2.call(opt.dictionary) === "[object ArrayBuffer]") { opt.dictionary = new Uint8Array(opt.dictionary); } if (opt.raw) { @@ -9824,7 +11336,7 @@ var require_inflate2 = __commonJS((exports) => { _mode = mode === ~~mode ? mode : mode === true ? c.Z_FINISH : c.Z_NO_FLUSH; if (typeof data === "string") { strm.input = strings.binstring2buf(data); - } else if (toString.call(data) === "[object ArrayBuffer]") { + } else if (toString2.call(data) === "[object ArrayBuffer]") { strm.input = new Uint8Array(data); } else { strm.input = data; @@ -9926,9 +11438,9 @@ var require_pako = __commonJS((exports, module) => { var assign = require_common().assign; var deflate = require_deflate2(); var inflate = require_inflate2(); - var constants = require_constants(); + var constants2 = require_constants(); var pako = {}; - assign(pako, deflate, inflate, constants); + assign(pako, deflate, inflate, constants2); module.exports = pako; }); @@ -10020,8 +11532,8 @@ var require_ZipFileWorker = __commonJS((exports, module) => { var crc32 = require_crc32(); var signature = require_signature(); var decToHex = function(dec, bytes) { - var hex = "", i; - for (i = 0;i < bytes; i++) { + var hex = "", i2; + for (i2 = 0;i2 < bytes; i2++) { hex += String.fromCharCode(dec & 255); dec = dec >>> 8; } @@ -10186,9 +11698,9 @@ var require_ZipFileWorker = __commonJS((exports, module) => { }; ZipFileWorker.prototype.flush = function() { var localDirLength = this.bytesWritten; - for (var i = 0;i < this.dirRecords.length; i++) { + for (var i2 = 0;i2 < this.dirRecords.length; i2++) { this.push({ - data: this.dirRecords[i], + data: this.dirRecords[i2], meta: { percent: 100 } }); } @@ -10245,19 +11757,18 @@ var require_ZipFileWorker = __commonJS((exports, module) => { if (!GenericWorker.prototype.error.call(this, e)) { return false; } - for (var i = 0;i < sources.length; i++) { + for (var i2 = 0;i2 < sources.length; i2++) { try { - sources[i].error(e); - } catch (e2) { - } + sources[i2].error(e); + } catch (e2) {} } return true; }; ZipFileWorker.prototype.lock = function() { GenericWorker.prototype.lock.call(this); var sources = this._sources; - for (var i = 0;i < sources.length; i++) { - sources[i].lock(); + for (var i2 = 0;i2 < sources.length; i2++) { + sources[i2].lock(); } }; module.exports = ZipFileWorker; @@ -10515,8 +12026,8 @@ var require_object = __commonJS((exports, module) => { var kids = this.filter(function(relativePath, file2) { return file2.name.slice(0, name.length) === name; }); - for (var i = 0;i < kids.length; i++) { - delete this.files[kids[i].name]; + for (var i2 = 0;i2 < kids.length; i2++) { + delete this.files[kids[i2].name]; } } return this; @@ -10599,13 +12110,12 @@ var require_DataReader = __commonJS((exports, module) => { skip: function(n) { this.setIndex(this.index + n); }, - byteAt: function() { - }, + byteAt: function() {}, readInt: function(size) { - var result = 0, i; + var result = 0, i2; this.checkOffset(size); - for (i = this.index + size - 1;i >= this.index; i--) { - result = (result << 8) + this.byteAt(i); + for (i2 = this.index + size - 1;i2 >= this.index; i2--) { + result = (result << 8) + this.byteAt(i2); } this.index += size; return result; @@ -10613,12 +12123,9 @@ var require_DataReader = __commonJS((exports, module) => { readString: function(size) { return utils.transformTo("string", this.readData(size)); }, - readData: function() { - }, - lastIndexOfSignature: function() { - }, - readAndCheckSignature: function() { - }, + readData: function() {}, + lastIndexOfSignature: function() {}, + readAndCheckSignature: function() {}, readDate: function() { var dostime = this.readInt(4); return new Date(Date.UTC((dostime >> 25 & 127) + 1980, (dostime >> 21 & 15) - 1, dostime >> 16 & 31, dostime >> 11 & 31, dostime >> 5 & 63, (dostime & 31) << 1)); @@ -10633,19 +12140,19 @@ var require_ArrayReader = __commonJS((exports, module) => { var utils = require_utils(); function ArrayReader(data) { DataReader.call(this, data); - for (var i = 0;i < this.data.length; i++) { - data[i] = data[i] & 255; + for (var i2 = 0;i2 < this.data.length; i2++) { + data[i2] = data[i2] & 255; } } utils.inherits(ArrayReader, DataReader); - ArrayReader.prototype.byteAt = function(i) { - return this.data[this.zero + i]; + ArrayReader.prototype.byteAt = function(i2) { + return this.data[this.zero + i2]; }; ArrayReader.prototype.lastIndexOfSignature = function(sig) { var sig0 = sig.charCodeAt(0), sig1 = sig.charCodeAt(1), sig2 = sig.charCodeAt(2), sig3 = sig.charCodeAt(3); - for (var i = this.length - 4;i >= 0; --i) { - if (this.data[i] === sig0 && this.data[i + 1] === sig1 && this.data[i + 2] === sig2 && this.data[i + 3] === sig3) { - return i - this.zero; + for (var i2 = this.length - 4;i2 >= 0; --i2) { + if (this.data[i2] === sig0 && this.data[i2 + 1] === sig1 && this.data[i2 + 2] === sig2 && this.data[i2 + 3] === sig3) { + return i2 - this.zero; } } return -1; @@ -10674,8 +12181,8 @@ var require_StringReader = __commonJS((exports, module) => { DataReader.call(this, data); } utils.inherits(StringReader, DataReader); - StringReader.prototype.byteAt = function(i) { - return this.data.charCodeAt(this.zero + i); + StringReader.prototype.byteAt = function(i2) { + return this.data.charCodeAt(this.zero + i2); }; StringReader.prototype.lastIndexOfSignature = function(sig) { return this.data.lastIndexOf(sig) - this.zero; @@ -11002,9 +12509,9 @@ var require_zipEntries = __commonJS((exports, module) => { } }, readLocalFiles: function() { - var i, file; - for (i = 0;i < this.files.length; i++) { - file = this.files[i]; + var i2, file; + for (i2 = 0;i2 < this.files.length; i2++) { + file = this.files[i2]; this.reader.setIndex(file.localHeaderOffset); this.checkSignature(sig.LOCAL_FILE_HEADER); file.readLocalPart(this.reader); @@ -11025,8 +12532,7 @@ var require_zipEntries = __commonJS((exports, module) => { if (this.centralDirRecords !== this.files.length) { if (this.centralDirRecords !== 0 && this.files.length === 0) { throw new Error("Corrupted zip or bug: expected " + this.centralDirRecords + " records in central dir, got " + this.files.length); - } else { - } + } else {} } }, readEndOfCentral: function() { @@ -11069,8 +12575,7 @@ var require_zipEntries = __commonJS((exports, module) => { } var extraBytes = endOfCentralDirOffset - expectedEndOfCentralDirOffset; if (extraBytes > 0) { - if (this.isSignature(endOfCentralDirOffset, sig.CENTRAL_FILE_HEADER)) { - } else { + if (this.isSignature(endOfCentralDirOffset, sig.CENTRAL_FILE_HEADER)) {} else { this.reader.zero = extraBytes; } } else if (extraBytes < 0) { @@ -11132,16 +12637,16 @@ var require_load = __commonJS((exports, module) => { var promises = [external.Promise.resolve(zipEntries)]; var files = zipEntries.files; if (options.checkCRC32) { - for (var i = 0;i < files.length; i++) { - promises.push(checkEntryCRC32(files[i])); + for (var i2 = 0;i2 < files.length; i2++) { + promises.push(checkEntryCRC32(files[i2])); } } return external.Promise.all(promises); }).then(function addFiles(results) { var zipEntries = results.shift(); var files = zipEntries.files; - for (var i = 0;i < files.length; i++) { - var input = files[i]; + for (var i2 = 0;i2 < files.length; i2++) { + var input = files[i2]; var unsafeName = input.fileNameStr; var safeName = utils.resolve(input.fileNameStr); zip.file(safeName, input.decompressed, { @@ -11180,9 +12685,9 @@ var require_lib = __commonJS((exports, module) => { this.root = ""; this.clone = function() { var newObj = new JSZip; - for (var i in this) { - if (typeof this[i] !== "function") { - newObj[i] = this[i]; + for (var i2 in this) { + if (typeof this[i2] !== "function") { + newObj[i2] = this[i2]; } } return newObj; @@ -11903,8 +13408,7 @@ class SpriteBuilder { this.activeControlPointIndex = -1; if (this.selectedFrame) { if (this.selectedShape) { - if (this.selectedShape.type === "image") { - } else if (this.selectedShape.type === "circle" && this.selectedShape.points && this.selectedShape.radius !== undefined) { + if (this.selectedShape.type === "image") {} else if (this.selectedShape.type === "circle" && this.selectedShape.points && this.selectedShape.radius !== undefined) { const centerX = this.selectedShape.points[0].x; const centerY = this.selectedShape.points[0].y; const controlX = centerX + this.selectedShape.radius; @@ -11914,10 +13418,10 @@ class SpriteBuilder { this.activeControlPointIndex = 0; } } else if (this.selectedShape.type === "polygon" && this.selectedShape.points) { - for (let i = 0;i < this.selectedShape.points.length; i++) { - if (this.isPointInControlPoint(pos, this.selectedShape.points[i])) { + for (let i2 = 0;i2 < this.selectedShape.points.length; i2++) { + if (this.isPointInControlPoint(pos, this.selectedShape.points[i2])) { this.isModifyingShape = true; - this.activeControlPointIndex = i; + this.activeControlPointIndex = i2; break; } } @@ -11925,8 +13429,8 @@ class SpriteBuilder { } if (!this.isModifyingShape) { this.selectedShape = null; - for (let i = this.selectedFrame.shapes.length - 1;i >= 0; i--) { - const shape = this.selectedFrame.shapes[i]; + for (let i2 = this.selectedFrame.shapes.length - 1;i2 >= 0; i2--) { + const shape = this.selectedFrame.shapes[i2]; if (shape && this.isPointInShape(pos, shape)) { this.selectedShape = shape; if (shape.points && shape.points.length > 0) { @@ -12190,8 +13694,8 @@ class SpriteBuilder { case "polygon": this.drawingCtx.beginPath(); this.drawingCtx.moveTo(shape.points[0].x, shape.points[0].y); - for (let i = 1;i < shape.points.length; i++) { - this.drawingCtx.lineTo(shape.points[i].x, shape.points[i].y); + for (let i2 = 1;i2 < shape.points.length; i2++) { + this.drawingCtx.lineTo(shape.points[i2].x, shape.points[i2].y); } if (shape.type === "polygon") { this.drawingCtx.closePath(); @@ -12274,8 +13778,8 @@ class SpriteBuilder { if (shape.points && shape.points.length > 0) { ctx.beginPath(); ctx.moveTo(shape.points[0].x * scale + offsetX, shape.points[0].y * scale + offsetY); - for (let i = 1;i < shape.points.length; i++) { - ctx.lineTo(shape.points[i].x * scale + offsetX, shape.points[i].y * scale + offsetY); + for (let i2 = 1;i2 < shape.points.length; i2++) { + ctx.lineTo(shape.points[i2].x * scale + offsetX, shape.points[i2].y * scale + offsetY); } if (shape.type === "polygon") { ctx.closePath(); diff --git a/helpers.ts b/helpers.ts index 99716f5..c2aca19 100644 --- a/helpers.ts +++ b/helpers.ts @@ -1,3 +1,4 @@ +import type { Point } from "./Math/LineIntersections"; import type { Vector } from "./Math/Vector"; import type { Camera } from "./Parts/Camera"; import { BoxCollider } from "./Parts/Children/BoxCollider"; @@ -205,95 +206,30 @@ export function isPointInObject(mouseX: number, mouseY: number, child: Part): bo return false; } -// require("fs").writeFileSync("monster.json", JSON.stringify(convertTexturePackerToSpriteSheetData({"frames": [ -// { -// "filename": "idle/idle-0", -// "frame": {"x":100,"y":1,"w":17,"h":26}, -// "rotated": false, -// "trimmed": false, -// "spriteSourceSize": {"x":0,"y":0,"w":17,"h":26}, -// "sourceSize": {"w":17,"h":26} -// }, -// { -// "filename": "idle/idle-1", -// "frame": {"x":21,"y":1,"w":17,"h":27}, -// "rotated": false, -// "trimmed": false, -// "spriteSourceSize": {"x":0,"y":0,"w":17,"h":27}, -// "sourceSize": {"w":17,"h":27} -// }, -// { -// "filename": "idle/idle-2", -// "frame": {"x":1,"y":1,"w":18,"h":27}, -// "rotated": false, -// "trimmed": false, -// "spriteSourceSize": {"x":0,"y":0,"w":18,"h":27}, -// "sourceSize": {"w":18,"h":27} -// }, -// { -// "filename": "idle/idle-3", -// "frame": {"x":78,"y":1,"w":20,"h":26}, -// "rotated": false, -// "trimmed": false, -// "spriteSourceSize": {"x":0,"y":0,"w":20,"h":26}, -// "sourceSize": {"w":20,"h":26} -// }, -// { -// "filename": "walk/walk-0", -// "frame": {"x":177,"y":1,"w":17,"h":25}, -// "rotated": false, -// "trimmed": false, -// "spriteSourceSize": {"x":0,"y":0,"w":17,"h":25}, -// "sourceSize": {"w":17,"h":25} -// }, -// { -// "filename": "walk/walk-1", -// "frame": {"x":119,"y":1,"w":17,"h":26}, -// "rotated": false, -// "trimmed": false, -// "spriteSourceSize": {"x":0,"y":0,"w":17,"h":26}, -// "sourceSize": {"w":17,"h":26} -// }, -// { -// "filename": "walk/walk-2", -// "frame": {"x":40,"y":1,"w":17,"h":27}, -// "rotated": false, -// "trimmed": false, -// "spriteSourceSize": {"x":0,"y":0,"w":17,"h":27}, -// "sourceSize": {"w":17,"h":27} -// }, -// { -// "filename": "walk/walk-3", -// "frame": {"x":157,"y":1,"w":18,"h":25}, -// "rotated": false, -// "trimmed": false, -// "spriteSourceSize": {"x":0,"y":0,"w":18,"h":25}, -// "sourceSize": {"w":18,"h":25} -// }, -// { -// "filename": "walk/walk-4", -// "frame": {"x":138,"y":1,"w":17,"h":26}, -// "rotated": false, -// "trimmed": false, -// "spriteSourceSize": {"x":0,"y":0,"w":17,"h":26}, -// "sourceSize": {"w":17,"h":26} -// }, -// { -// "filename": "walk/walk-5", -// "frame": {"x":59,"y":1,"w":17,"h":27}, -// "rotated": false, -// "trimmed": false, -// "spriteSourceSize": {"x":0,"y":0,"w":17,"h":27}, -// "sourceSize": {"w":17,"h":27} -// }], -// "meta": { -// "app": "https://www.codeandweb.com/texturepacker", -// "version": "1.0", -// "image": "sprite_test.png", -// "format": "RGBA8888", -// "size": {"w":195,"h":29}, -// "scale": "1", -// "smartupdate": "$TexturePacker:SmartUpdate:a644d106dc7284a35edd312ffb5969fa:6d80dcba90cb53bd0891529221374b5c:e2c998ad5b48a6da356a9a84831ec759$" -// } -// }) as SpriteSheetData, null, 4)); \ No newline at end of file +export function vecEq(a: Vector, b: Vector): boolean { + return a.x === b.x && a.y === b.y; +} + +export function pointInPoly(point: Point, poly: Point[]): boolean { + let inside = false; + for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) { + const xi = poly[i].x, yi = poly[i].y; + const xj = poly[j].x, yj = poly[j].y; + + // Check if the point is on the segment + const onSegment = (point.y - yi) * (xj - xi) === (point.x - xi) * (yj - yi) && + (Math.min(xi, xj) <= point.x && point.x <= Math.max(xi, xj)) && + (Math.min(yi, yj) <= point.y && point.y <= Math.max(yi, yj)); + + if (onSegment) { + return true; // Point is on the boundary + } + + const intersect = ((yi > point.y) !== (yj > point.y)) + && (point.x < (xj - xi) * (point.y - yi) / (yj - yi) + xi); + if (intersect) inside = !inside; + } + + return inside; +} \ No newline at end of file diff --git a/image.png b/image.png deleted file mode 100644 index e0e2a1c..0000000 Binary files a/image.png and /dev/null differ diff --git a/package-lock.json b/package-lock.json index 7c5fa7f..2611232 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,7 +6,14 @@ "": { "name": "forge", "dependencies": { + "@supabase/supabase-js": "^2.53.0", + "@types/google-one-tap": "^1.2.6", + "@types/node": "^24.1.0", + "@types/showdown": "^2.0.6", + "jszip": "^3.10.1", "matter-js": "^0.20.0", + "rfdc": "^1.4.1", + "showdown": "^2.1.0", "terser": "^5.43.1" }, "devDependencies": { @@ -61,6 +68,101 @@ "node": ">=6" } }, + "node_modules/@supabase/auth-js": { + "version": "2.71.1", + "resolved": "https://registry.npmjs.org/@supabase/auth-js/-/auth-js-2.71.1.tgz", + "integrity": "sha512-mMIQHBRc+SKpZFRB2qtupuzulaUhFYupNyxqDj5Jp/LyPvcWvjaJzZzObv6URtL/O6lPxkanASnotGtNpS3H2Q==", + "license": "MIT", + "dependencies": { + "@supabase/node-fetch": "^2.6.14" + } + }, + "node_modules/@supabase/functions-js": { + "version": "2.4.5", + "resolved": "https://registry.npmjs.org/@supabase/functions-js/-/functions-js-2.4.5.tgz", + "integrity": "sha512-v5GSqb9zbosquTo6gBwIiq7W9eQ7rE5QazsK/ezNiQXdCbY+bH8D9qEaBIkhVvX4ZRW5rP03gEfw5yw9tiq4EQ==", + "license": "MIT", + "dependencies": { + "@supabase/node-fetch": "^2.6.14" + } + }, + "node_modules/@supabase/node-fetch": { + "version": "2.6.15", + "resolved": "https://registry.npmjs.org/@supabase/node-fetch/-/node-fetch-2.6.15.tgz", + "integrity": "sha512-1ibVeYUacxWYi9i0cf5efil6adJ9WRyZBLivgjs+AUpewx1F3xPi7gLgaASI2SmIQxPoCEjAsLAzKPgMJVgOUQ==", + "license": "MIT", + "dependencies": { + "whatwg-url": "^5.0.0" + }, + "engines": { + "node": "4.x || >=6.0.0" + } + }, + "node_modules/@supabase/postgrest-js": { + "version": "1.21.3", + "resolved": "https://registry.npmjs.org/@supabase/postgrest-js/-/postgrest-js-1.21.3.tgz", + "integrity": "sha512-rg3DmmZQKEVCreXq6Am29hMVe1CzemXyIWVYyyua69y6XubfP+DzGfLxME/1uvdgwqdoaPbtjBDpEBhqxq1ZwA==", + "license": "MIT", + "dependencies": { + "@supabase/node-fetch": "^2.6.14" + } + }, + "node_modules/@supabase/realtime-js": { + "version": "2.15.4", + "resolved": "https://registry.npmjs.org/@supabase/realtime-js/-/realtime-js-2.15.4.tgz", + "integrity": "sha512-e/FYIWjvQJHOCNACWehnKvg26zosju3694k0NMUNb+JGLdvHJzEa29ZVVLmawd2kvx4hdbv8mxSqfttRnH3+DA==", + "license": "MIT", + "dependencies": { + "@supabase/node-fetch": "^2.6.13", + "@types/phoenix": "^1.6.6", + "@types/ws": "^8.18.1", + "ws": "^8.18.2" + } + }, + "node_modules/@supabase/realtime-js/node_modules/ws": { + "version": "8.18.3", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.3.tgz", + "integrity": "sha512-PEIGCY5tSlUt50cqyMXfCzX+oOPqN0vuGqWzbcJ2xvnkzkq46oOpz7dQaTDBdfICb4N14+GARUDw2XV2N4tvzg==", + "license": "MIT", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, + "node_modules/@supabase/storage-js": { + "version": "2.11.0", + "resolved": "https://registry.npmjs.org/@supabase/storage-js/-/storage-js-2.11.0.tgz", + "integrity": "sha512-Y+kx/wDgd4oasAgoAq0bsbQojwQ+ejIif8uczZ9qufRHWFLMU5cODT+ApHsSrDufqUcVKt+eyxtOXSkeh2v9ww==", + "license": "MIT", + "dependencies": { + "@supabase/node-fetch": "^2.6.14" + } + }, + "node_modules/@supabase/supabase-js": { + "version": "2.56.1", + "resolved": "https://registry.npmjs.org/@supabase/supabase-js/-/supabase-js-2.56.1.tgz", + "integrity": "sha512-cb/kS0d6G/qbcmUFItkqVrQbxQHWXzfRZuoiSDv/QiU6RbGNTn73XjjvmbBCZ4MMHs+5teihjhpEVluqbXISEg==", + "license": "MIT", + "dependencies": { + "@supabase/auth-js": "2.71.1", + "@supabase/functions-js": "2.4.5", + "@supabase/node-fetch": "2.6.15", + "@supabase/postgrest-js": "1.21.3", + "@supabase/realtime-js": "2.15.4", + "@supabase/storage-js": "^2.10.4" + } + }, "node_modules/@szmarczak/http-timer": { "version": "1.1.2", "dev": true, @@ -82,6 +184,12 @@ "bun-types": "1.2.18" } }, + "node_modules/@types/google-one-tap": { + "version": "1.2.6", + "resolved": "https://registry.npmjs.org/@types/google-one-tap/-/google-one-tap-1.2.6.tgz", + "integrity": "sha512-REmJsXVHvKb/sgI8DF+7IesMbDbcsEokHBqxU01ENZ8d98UPWdRLhUCtxEm9bhNFFg6PJGy7PNFdvovp0hK3jA==", + "license": "MIT" + }, "node_modules/@types/matter-js": { "version": "0.19.8", "resolved": "https://registry.npmjs.org/@types/matter-js/-/matter-js-0.19.8.tgz", @@ -90,13 +198,20 @@ "license": "MIT" }, "node_modules/@types/node": { - "version": "24.0.10", - "dev": true, + "version": "24.3.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-24.3.0.tgz", + "integrity": "sha512-aPTXCrfwnDLj4VvXrm+UUCQjNEvJgNA8s5F1cvwQU+3KNltTOkBm1j30uNLyqqPNe7gE3KFzImYoZEfLhp4Yow==", "license": "MIT", "dependencies": { - "undici-types": "~7.8.0" + "undici-types": "~7.10.0" } }, + "node_modules/@types/phoenix": { + "version": "1.6.6", + "resolved": "https://registry.npmjs.org/@types/phoenix/-/phoenix-1.6.6.tgz", + "integrity": "sha512-PIzZZlEppgrpoT2QgbnDU+MMzuR6BbCjllj0bM70lWoejMeNJAxCchxnv7J3XFkI8MpygtRpzXrIlmWUBclP5A==", + "license": "MIT" + }, "node_modules/@types/react": { "version": "19.1.8", "dev": true, @@ -106,6 +221,21 @@ "csstype": "^3.0.2" } }, + "node_modules/@types/showdown": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/@types/showdown/-/showdown-2.0.6.tgz", + "integrity": "sha512-pTvD/0CIeqe4x23+YJWlX2gArHa8G0J0Oh6GKaVXV7TAeickpkkZiNOgFcFcmLQ5lB/K0qBJL1FtRYltBfbGCQ==", + "license": "MIT" + }, + "node_modules/@types/ws": { + "version": "8.18.1", + "resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.18.1.tgz", + "integrity": "sha512-ThVF6DCVhA8kUGy+aazFQ4kXQ7E1Ty7A3ypFOe0IcJV8O/M511G99AW24irKrW56Wt44yG9+ij8FaqoBGkuBXg==", + "license": "MIT", + "dependencies": { + "@types/node": "*" + } + }, "node_modules/acorn": { "version": "8.15.0", "license": "MIT", @@ -465,6 +595,12 @@ "node": "*" } }, + "node_modules/core-util-is": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", + "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==", + "license": "MIT" + }, "node_modules/cp-file": { "version": "7.0.0", "dev": true, @@ -935,6 +1071,12 @@ "node": ">= 0.8" } }, + "node_modules/immediate": { + "version": "3.0.6", + "resolved": "https://registry.npmjs.org/immediate/-/immediate-3.0.6.tgz", + "integrity": "sha512-XXOFtyqDjNDAQxVfYxuF7g9Il/IbWmmlQg2MYKOH8ExIT1qg6xc4zyS3HaEEATgs1btfzxq15ciUiY7gjSXRGQ==", + "license": "MIT" + }, "node_modules/import-lazy": { "version": "2.1.0", "dev": true, @@ -953,7 +1095,6 @@ }, "node_modules/inherits": { "version": "2.0.4", - "dev": true, "license": "ISC" }, "node_modules/ini": { @@ -1075,6 +1216,12 @@ "dev": true, "license": "MIT" }, + "node_modules/isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==", + "license": "MIT" + }, "node_modules/json-buffer": { "version": "3.0.0", "dev": true, @@ -1088,6 +1235,18 @@ "graceful-fs": "^4.1.6" } }, + "node_modules/jszip": { + "version": "3.10.1", + "resolved": "https://registry.npmjs.org/jszip/-/jszip-3.10.1.tgz", + "integrity": "sha512-xXDvecyTpGLrqFrvkrUSoxxfJI5AH7U8zxxtVclpsUtMCq4JQ290LY8AW5c7Ggnr/Y/oK+bQMbqK2qmtk3pN4g==", + "license": "(MIT OR GPL-3.0-or-later)", + "dependencies": { + "lie": "~3.3.0", + "pako": "~1.0.2", + "readable-stream": "~2.3.6", + "setimmediate": "^1.0.5" + } + }, "node_modules/keyv": { "version": "3.1.0", "dev": true, @@ -1107,6 +1266,15 @@ "node": ">=8" } }, + "node_modules/lie": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/lie/-/lie-3.3.0.tgz", + "integrity": "sha512-UaiMJzeWRlEujzAuw5LokY1L5ecNQYZKfmyZ9L7wDHb/p5etKaxXhohBcrw0EYby+G/NA52vRSN4N39dxHAIwQ==", + "license": "MIT", + "dependencies": { + "immediate": "~3.0.5" + } + }, "node_modules/livereload": { "version": "0.9.3", "dev": true, @@ -1401,6 +1569,12 @@ "node": ">=8" } }, + "node_modules/pako": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/pako/-/pako-1.0.11.tgz", + "integrity": "sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw==", + "license": "(MIT AND Zlib)" + }, "node_modules/parent-require": { "version": "1.0.0", "dev": true, @@ -1451,6 +1625,12 @@ "node": ">=6" } }, + "node_modules/process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==", + "license": "MIT" + }, "node_modules/pump": { "version": "3.0.3", "dev": true, @@ -1493,6 +1673,27 @@ "rc": "cli.js" } }, + "node_modules/readable-stream": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz", + "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==", + "license": "MIT", + "dependencies": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "node_modules/readable-stream/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "license": "MIT" + }, "node_modules/readdirp": { "version": "3.6.0", "dev": true, @@ -1552,6 +1753,12 @@ "lowercase-keys": "^1.0.0" } }, + "node_modules/rfdc": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/rfdc/-/rfdc-1.4.1.tgz", + "integrity": "sha512-q1b3N5QkRUWUl7iyylaaj3kOpIT0N2i9MqIEQXP73GVsN9cw3fdx8X63cEmWhJGi2PPCF23Ijp7ktmd39rawIA==", + "license": "MIT" + }, "node_modules/semver": { "version": "6.3.1", "dev": true, @@ -1645,11 +1852,42 @@ "dev": true, "license": "ISC" }, + "node_modules/setimmediate": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/setimmediate/-/setimmediate-1.0.5.tgz", + "integrity": "sha512-MATJdZp8sLqDl/68LfQmbP8zKPLQNV6BIZoIgrscFDQ+RsvK/BxeDQOgyxKKoh0y/8h3BqVFnCqQ/gd+reiIXA==", + "license": "MIT" + }, "node_modules/setprototypeof": { "version": "1.2.0", "dev": true, "license": "ISC" }, + "node_modules/showdown": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/showdown/-/showdown-2.1.0.tgz", + "integrity": "sha512-/6NVYu4U819R2pUIk79n67SYgJHWCce0a5xTP979WbNp0FL9MN1I1QK662IDU1b6JzKTvmhgI7T7JYIxBi3kMQ==", + "license": "MIT", + "dependencies": { + "commander": "^9.0.0" + }, + "bin": { + "showdown": "bin/showdown.js" + }, + "funding": { + "type": "individual", + "url": "https://www.paypal.me/tiviesantos" + } + }, + "node_modules/showdown/node_modules/commander": { + "version": "9.5.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-9.5.0.tgz", + "integrity": "sha512-KRs7WVDKg86PWiuAqhDrAQnTXZKraVcCc6vFdL14qrZ/DcWwuRo7VoiYXalXO7S5GKpqYiVEwCbgFDfxNHKJBQ==", + "license": "MIT", + "engines": { + "node": "^12.20.0 || >=14" + } + }, "node_modules/signal-exit": { "version": "3.0.7", "dev": true, @@ -1678,6 +1916,21 @@ "node": ">= 0.6" } }, + "node_modules/string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "license": "MIT", + "dependencies": { + "safe-buffer": "~5.1.0" + } + }, + "node_modules/string_decoder/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "license": "MIT" + }, "node_modules/string-width": { "version": "4.2.3", "dev": true, @@ -1796,7 +2049,6 @@ }, "node_modules/tr46": { "version": "0.0.3", - "dev": true, "license": "MIT" }, "node_modules/tweezer.js": { @@ -1833,8 +2085,9 @@ } }, "node_modules/undici-types": { - "version": "7.8.0", - "dev": true, + "version": "7.10.0", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.10.0.tgz", + "integrity": "sha512-t5Fy/nfn+14LuOc2KNYg75vZqClpAiqscVvMygNnlsHBFpSXdJaYtXMcdNLpl/Qvc3P2cB3s6lOV51nqsFq4ag==", "license": "MIT" }, "node_modules/unique-string": { @@ -1962,6 +2215,12 @@ "node": ">=4" } }, + "node_modules/util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", + "license": "MIT" + }, "node_modules/utils-merge": { "version": "1.0.1", "dev": true, @@ -1972,12 +2231,10 @@ }, "node_modules/webidl-conversions": { "version": "3.0.1", - "dev": true, "license": "BSD-2-Clause" }, "node_modules/whatwg-url": { "version": "5.0.0", - "dev": true, "license": "MIT", "dependencies": { "tr46": "~0.0.3", diff --git a/package.json b/package.json index eab2be4..9d73098 100644 --- a/package.json +++ b/package.json @@ -22,12 +22,14 @@ "build:nominengine": "bun build ./engine/engine-core.ts --outfile ./engine/bundle.js --watch" }, "dependencies": { - "@supabase/supabase-js": "^2.53.0", + "@supabase/supabase-js": "^2.56.1", "@types/google-one-tap": "^1.2.6", "@types/node": "^24.1.0", "@types/showdown": "^2.0.6", "jszip": "^3.10.1", + "martinez-polygon-clipping": "^0.7.4", "matter-js": "^0.20.0", + "polygon-clipping": "^0.15.7", "rfdc": "^1.4.1", "showdown": "^2.1.0", "terser": "^5.43.1" diff --git a/project_context.md b/project_context.md index 785d772..020e0f3 100644 --- a/project_context.md +++ b/project_context.md @@ -5,67 +5,14 @@ This project is a game engine, called Forge. The engine follows a "Game Tree" co ## Overview - You are running on a windows machine. Please use powershell commands by defualt -- Runtime is Bun. Build commands are build:engine, build:editor, and build:all. +- Runtime is Bun. Build commands are build:engine, build:editor, and build:all. These is no need to call any of these, watchers are running in the background building on the fly. ## Code Overview - Examine ./docs for documentation of the project - Examine ./Parts for the code on each built-in part - Examine ./engine for the code that runs the web engine. Ignore the .js files (specifically, bundle.js, editor.js, script.bak.js, and defs.bak.js), as the project is built using ts and these are just the build files. +- Examine ./engine/spriteBuilder.html and .ts for the spriteBuilder functionality. ## Current Task -Examine the structure of the spritesheet data (@docs/spritesheet-format.md), and build a page engine/spriteBuilder.html that allows for importing individual assets and creating spritesheets, complete with animation support. Also, allow building sprites through drawing. - -The idea is that the editor is on large canvas, and there is a right panel for selecting current animation. A bottom gutter allows selecting individual frames for the animation. Also, allow saving individual frames of the sprites. - -When exporting, combing the animations and frames and zip them, so that the unzipped file gives spritesheet.png and spritesheetdata.json. - -Please build it using ts, and add another build directive to package.json `build:spriteEditor` and `watch:spriteEditor`, which outputs to spriteEditor.js. Attach this to the html file. - -Use the same style guides as the editor (@engine/styles.css). - -Begin by examining @docs/spritesheet-format.md and the attached format. - -## SpritesheetData interface -```ts -export interface SpriteSheetData { - frames: Array<{ - filename: string; // Name of the frame - frame: { - x: number; - y: number; - w: number; - h: number; - }; - rotated?: boolean; - trimmed?: boolean; - spriteSourceSize?: { - x: number; - y: number; - w: number; - h: number; - }; - sourceSize?: { - w: number; - h: number; - }; - duration?: number; // Optional duration for each frame (in milliseconds) - }>; - meta: { - image: string; // Path to the spritesheet image - size: { - w: number; // Width of the spritesheet - h: number; // Height of the spritesheet - }; - startingAnimation?: string; // Optional property to specify the starting animation - startingFrame?: string; // Optional property to specify the starting frame - animations: { - [animationName: string]: { - frames: string[]; // Array of frame names for this animation - loop?: boolean; // Optional property to indicate if the animation should loop - bounce?: boolean; // Optional property to indicate if the animation should bounce - }; - }; // Optional property to define animations by frame names - } -} -``` \ No newline at end of file +Update documentation and definitions for all parts and functionality. \ No newline at end of file diff --git a/public/test.html b/public/test.html new file mode 100644 index 0000000..7c08b00 --- /dev/null +++ b/public/test.html @@ -0,0 +1,11 @@ + + + + + Document + + + + + + diff --git a/src/TestColliderMerge.ts b/src/TestColliderMerge.ts new file mode 100644 index 0000000..77120c3 --- /dev/null +++ b/src/TestColliderMerge.ts @@ -0,0 +1,175 @@ +import { Collider, BoxCollider, PolygonCollider, Game, Scene, Layer, GameObject, Transform, Vector, Input, CharacterMovement, Part, ColorRender, Camera, Follow } from "../engine/engine-core"; + +const game = new Game({ + name: "My game", + canvas: "can", + width: window.innerWidth, + height: window.innerHeight, + showFrameStats: "PERFORMANCE_HUD", +}); + + +const s1 = new Scene({ + name: "S1", +}); + +const l1 = new Layer({ + name: "L1" +}); + + +s1.addChild(l1); + + +game.addChild(s1); + + + + +// Missing borders +for (let x = 0; x < 50; x++) { + for (let y = 0; y < 50; y++) { + if ((x == 0 || y === 0) && Math.random() > 0.2) continue; + const testObject = new GameObject({ + name: `Test Object (${x + 1}, ${y + 1})` + }); + const wh = Math.floor(Math.random() * 500) + 10; + const testTransform = new Transform({ + position: new Vector(30 + x * wh, 500 + y * wh), + rotation: 0, + scale: Vector.From(1) + }); + // Generate a random convex n-gon (n=3-8) + const n = Math.floor(Math.random() * 6) + 3; // 3 to 8 + const angleStep = (Math.PI * 2) / n; + const radius = wh / 2; + const center = new Vector(0, 0); + // Add a small random offset to each vertex for variety, but keep convex + const vertices: Vector[] = []; + for (let i = 0; i < n; i++) { + const angle = i * angleStep + Math.random() * (angleStep * 0.15); + const r = radius * (0.7 + Math.random() * 0.6); // 0.7r to 1.3r + vertices.push(new Vector( + Math.cos(angle) * r, + Math.sin(angle) * r + )); + } + const polyCollider = new PolygonCollider({ + vertices, + }); + const colorRender = new ColorRender({ + color: `red`, + width: wh, + height: wh, + }); + + testObject.addChildren(testTransform, polyCollider); + l1.addChild(testObject); + } +} + +for (let x = 0; x < 10; x++) { + const testObject = new GameObject({ + name: `Test D- Object (${x + 1})` + }); + + const testTransform = new Transform({ + position: new Vector(300 + x * 25, 100 + x * 25), + rotation: 0, + scale: Vector.From(1) + }); + const colorRender = new ColorRender({ + color: `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${Math.random() * 255})`, + width: 50, + height: 50, + }); + const boxCollider = new BoxCollider({ + width: 50, + height: 50, + tag: "testObjects", + }); + + testObject.addChildren(testTransform, boxCollider, colorRender); + l1.addChild(testObject); +} + +const input = new Input({ + key: () => {}, + keyup: () => {}, + mousemove: () => {}, + click: () => {}, +}); +s1.addChild(input); + +class CollisionColorChanger extends Part { + private collider: Collider | undefined; + private renderer: ColorRender | undefined; + private originalColor: string = 'blue'; + + onMount(part: Part) { + super.onMount(part); + this.collider = this.siblingOf("Collider"); + console.log(this.collider) + this.renderer = this.sibling("ColorRender"); + console.log(this.renderer); + if (this.renderer) { + this.originalColor = this.renderer.color; + } + } + + act(delta: number) { + super.act(delta); + if (this.collider && this.renderer) { + if (this.collider.colliding) { + this.renderer.color = 'red'; + } else { + this.renderer.color = this.originalColor; + } + } + } +} + +const player = new GameObject({ + name: "Player" +}); + +const playerTransform = new Transform({ + position: new Vector(100, 100) +}); + +const playerCollider = new BoxCollider({ + width: 50, + height: 50, +}); + +const playerRenderer = new ColorRender({ + width: 50, + height: 50, + color: "blue" +}); + +const playerMovement = new CharacterMovement({ + speed: 0.5, + input: input +}); + +const camera = new Camera({ name: "Cam" }); +const follow = new Follow({ + target: playerTransform, + interpolationSpeed: 0.01 +}); + +camera.addChild(follow); +const cameraTransform = new Transform({ position: new Vector(0, 0) }); +camera.addChild(cameraTransform); +s1.addChild(camera); + + +const colorChanger = new CollisionColorChanger(); + +player.addChildren(playerTransform, playerCollider, playerRenderer, playerMovement, colorChanger); +l1.addChild(player); + +console.log(game); + +game.start(s1); \ No newline at end of file diff --git a/testDist/TestColliderMerge.js b/testDist/TestColliderMerge.js new file mode 100644 index 0000000..e26d2f2 --- /dev/null +++ b/testDist/TestColliderMerge.js @@ -0,0 +1,26814 @@ +var __create = Object.create; +var __getProtoOf = Object.getPrototypeOf; +var __defProp = Object.defineProperty; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __toESM = (mod, isNodeMode, target) => { + target = mod != null ? __create(__getProtoOf(mod)) : {}; + const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target; + for (let key of __getOwnPropNames(mod)) + if (!__hasOwnProp.call(to, key)) + __defProp(to, key, { + get: () => mod[key], + enumerable: true + }); + return to; +}; +var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports); + +// node_modules/matter-js/build/matter.js +var require_matter = __commonJS((exports, module) => { + /*! + * matter-js 0.20.0 by @liabru + * http://brm.io/matter-js/ + * License MIT + * + * The MIT License (MIT) + * + * Copyright (c) Liam Brummitt and contributors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + (function webpackUniversalModuleDefinition(root, factory) { + if (typeof exports === "object" && typeof module === "object") + module.exports = factory(); + else if (typeof define === "function" && define.amd) + define("Matter", [], factory); + else if (typeof exports === "object") + exports["Matter"] = factory(); + else + root["Matter"] = factory(); + })(exports, function() { + return function(modules) { + var installedModules = {}; + function __webpack_require__(moduleId) { + if (installedModules[moduleId]) { + return installedModules[moduleId].exports; + } + var module2 = installedModules[moduleId] = { + i: moduleId, + l: false, + exports: {} + }; + modules[moduleId].call(module2.exports, module2, module2.exports, __webpack_require__); + module2.l = true; + return module2.exports; + } + __webpack_require__.m = modules; + __webpack_require__.c = installedModules; + __webpack_require__.d = function(exports2, name, getter) { + if (!__webpack_require__.o(exports2, name)) { + Object.defineProperty(exports2, name, { enumerable: true, get: getter }); + } + }; + __webpack_require__.r = function(exports2) { + if (typeof Symbol !== "undefined" && Symbol.toStringTag) { + Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" }); + } + Object.defineProperty(exports2, "__esModule", { value: true }); + }; + __webpack_require__.t = function(value, mode) { + if (mode & 1) + value = __webpack_require__(value); + if (mode & 8) + return value; + if (mode & 4 && typeof value === "object" && value && value.__esModule) + return value; + var ns = Object.create(null); + __webpack_require__.r(ns); + Object.defineProperty(ns, "default", { enumerable: true, value }); + if (mode & 2 && typeof value != "string") + for (var key in value) + __webpack_require__.d(ns, key, function(key2) { + return value[key2]; + }.bind(null, key)); + return ns; + }; + __webpack_require__.n = function(module2) { + var getter = module2 && module2.__esModule ? function getDefault() { + return module2["default"]; + } : function getModuleExports() { + return module2; + }; + __webpack_require__.d(getter, "a", getter); + return getter; + }; + __webpack_require__.o = function(object, property) { + return Object.prototype.hasOwnProperty.call(object, property); + }; + __webpack_require__.p = ""; + return __webpack_require__(__webpack_require__.s = 20); + }([ + function(module2, exports2) { + var Common = {}; + module2.exports = Common; + (function() { + Common._baseDelta = 1000 / 60; + Common._nextId = 0; + Common._seed = 0; + Common._nowStartTime = +new Date; + Common._warnedOnce = {}; + Common._decomp = null; + Common.extend = function(obj, deep) { + var argsStart, args, deepClone; + if (typeof deep === "boolean") { + argsStart = 2; + deepClone = deep; + } else { + argsStart = 1; + deepClone = true; + } + for (var i = argsStart;i < arguments.length; i++) { + var source = arguments[i]; + if (source) { + for (var prop in source) { + if (deepClone && source[prop] && source[prop].constructor === Object) { + if (!obj[prop] || obj[prop].constructor === Object) { + obj[prop] = obj[prop] || {}; + Common.extend(obj[prop], deepClone, source[prop]); + } else { + obj[prop] = source[prop]; + } + } else { + obj[prop] = source[prop]; + } + } + } + } + return obj; + }; + Common.clone = function(obj, deep) { + return Common.extend({}, deep, obj); + }; + Common.keys = function(obj) { + if (Object.keys) + return Object.keys(obj); + var keys = []; + for (var key in obj) + keys.push(key); + return keys; + }; + Common.values = function(obj) { + var values = []; + if (Object.keys) { + var keys = Object.keys(obj); + for (var i = 0;i < keys.length; i++) { + values.push(obj[keys[i]]); + } + return values; + } + for (var key in obj) + values.push(obj[key]); + return values; + }; + Common.get = function(obj, path, begin, end) { + path = path.split(".").slice(begin, end); + for (var i = 0;i < path.length; i += 1) { + obj = obj[path[i]]; + } + return obj; + }; + Common.set = function(obj, path, val, begin, end) { + var parts = path.split(".").slice(begin, end); + Common.get(obj, path, 0, -1)[parts[parts.length - 1]] = val; + return val; + }; + Common.shuffle = function(array) { + for (var i = array.length - 1;i > 0; i--) { + var j = Math.floor(Common.random() * (i + 1)); + var temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } + return array; + }; + Common.choose = function(choices) { + return choices[Math.floor(Common.random() * choices.length)]; + }; + Common.isElement = function(obj) { + if (typeof HTMLElement !== "undefined") { + return obj instanceof HTMLElement; + } + return !!(obj && obj.nodeType && obj.nodeName); + }; + Common.isArray = function(obj) { + return Object.prototype.toString.call(obj) === "[object Array]"; + }; + Common.isFunction = function(obj) { + return typeof obj === "function"; + }; + Common.isPlainObject = function(obj) { + return typeof obj === "object" && obj.constructor === Object; + }; + Common.isString = function(obj) { + return toString.call(obj) === "[object String]"; + }; + Common.clamp = function(value, min, max) { + if (value < min) + return min; + if (value > max) + return max; + return value; + }; + Common.sign = function(value) { + return value < 0 ? -1 : 1; + }; + Common.now = function() { + if (typeof window !== "undefined" && window.performance) { + if (window.performance.now) { + return window.performance.now(); + } else if (window.performance.webkitNow) { + return window.performance.webkitNow(); + } + } + if (Date.now) { + return Date.now(); + } + return new Date - Common._nowStartTime; + }; + Common.random = function(min, max) { + min = typeof min !== "undefined" ? min : 0; + max = typeof max !== "undefined" ? max : 1; + return min + _seededRandom() * (max - min); + }; + var _seededRandom = function() { + Common._seed = (Common._seed * 9301 + 49297) % 233280; + return Common._seed / 233280; + }; + Common.colorToNumber = function(colorString) { + colorString = colorString.replace("#", ""); + if (colorString.length == 3) { + colorString = colorString.charAt(0) + colorString.charAt(0) + colorString.charAt(1) + colorString.charAt(1) + colorString.charAt(2) + colorString.charAt(2); + } + return parseInt(colorString, 16); + }; + Common.logLevel = 1; + Common.log = function() { + if (console && Common.logLevel > 0 && Common.logLevel <= 3) { + console.log.apply(console, ["matter-js:"].concat(Array.prototype.slice.call(arguments))); + } + }; + Common.info = function() { + if (console && Common.logLevel > 0 && Common.logLevel <= 2) { + console.info.apply(console, ["matter-js:"].concat(Array.prototype.slice.call(arguments))); + } + }; + Common.warn = function() { + if (console && Common.logLevel > 0 && Common.logLevel <= 3) { + console.warn.apply(console, ["matter-js:"].concat(Array.prototype.slice.call(arguments))); + } + }; + Common.warnOnce = function() { + var message = Array.prototype.slice.call(arguments).join(" "); + if (!Common._warnedOnce[message]) { + Common.warn(message); + Common._warnedOnce[message] = true; + } + }; + Common.deprecated = function(obj, prop, warning) { + obj[prop] = Common.chain(function() { + Common.warnOnce("\uD83D\uDD05 deprecated \uD83D\uDD05", warning); + }, obj[prop]); + }; + Common.nextId = function() { + return Common._nextId++; + }; + Common.indexOf = function(haystack, needle) { + if (haystack.indexOf) + return haystack.indexOf(needle); + for (var i = 0;i < haystack.length; i++) { + if (haystack[i] === needle) + return i; + } + return -1; + }; + Common.map = function(list, func) { + if (list.map) { + return list.map(func); + } + var mapped = []; + for (var i = 0;i < list.length; i += 1) { + mapped.push(func(list[i])); + } + return mapped; + }; + Common.topologicalSort = function(graph) { + var result = [], visited = [], temp = []; + for (var node in graph) { + if (!visited[node] && !temp[node]) { + Common._topologicalSort(node, visited, temp, graph, result); + } + } + return result; + }; + Common._topologicalSort = function(node, visited, temp, graph, result) { + var neighbors = graph[node] || []; + temp[node] = true; + for (var i = 0;i < neighbors.length; i += 1) { + var neighbor = neighbors[i]; + if (temp[neighbor]) { + continue; + } + if (!visited[neighbor]) { + Common._topologicalSort(neighbor, visited, temp, graph, result); + } + } + temp[node] = false; + visited[node] = true; + result.push(node); + }; + Common.chain = function() { + var funcs = []; + for (var i = 0;i < arguments.length; i += 1) { + var func = arguments[i]; + if (func._chained) { + funcs.push.apply(funcs, func._chained); + } else { + funcs.push(func); + } + } + var chain = function() { + var lastResult, args = new Array(arguments.length); + for (var i2 = 0, l = arguments.length;i2 < l; i2++) { + args[i2] = arguments[i2]; + } + for (i2 = 0;i2 < funcs.length; i2 += 1) { + var result = funcs[i2].apply(lastResult, args); + if (typeof result !== "undefined") { + lastResult = result; + } + } + return lastResult; + }; + chain._chained = funcs; + return chain; + }; + Common.chainPathBefore = function(base, path, func) { + return Common.set(base, path, Common.chain(func, Common.get(base, path))); + }; + Common.chainPathAfter = function(base, path, func) { + return Common.set(base, path, Common.chain(Common.get(base, path), func)); + }; + Common.setDecomp = function(decomp) { + Common._decomp = decomp; + }; + Common.getDecomp = function() { + var decomp = Common._decomp; + try { + if (!decomp && typeof window !== "undefined") { + decomp = window.decomp; + } + if (!decomp && typeof global !== "undefined") { + decomp = global.decomp; + } + } catch (e) { + decomp = null; + } + return decomp; + }; + })(); + }, + function(module2, exports2) { + var Bounds = {}; + module2.exports = Bounds; + (function() { + Bounds.create = function(vertices) { + var bounds = { + min: { x: 0, y: 0 }, + max: { x: 0, y: 0 } + }; + if (vertices) + Bounds.update(bounds, vertices); + return bounds; + }; + Bounds.update = function(bounds, vertices, velocity) { + bounds.min.x = Infinity; + bounds.max.x = -Infinity; + bounds.min.y = Infinity; + bounds.max.y = -Infinity; + for (var i = 0;i < vertices.length; i++) { + var vertex = vertices[i]; + if (vertex.x > bounds.max.x) + bounds.max.x = vertex.x; + if (vertex.x < bounds.min.x) + bounds.min.x = vertex.x; + if (vertex.y > bounds.max.y) + bounds.max.y = vertex.y; + if (vertex.y < bounds.min.y) + bounds.min.y = vertex.y; + } + if (velocity) { + if (velocity.x > 0) { + bounds.max.x += velocity.x; + } else { + bounds.min.x += velocity.x; + } + if (velocity.y > 0) { + bounds.max.y += velocity.y; + } else { + bounds.min.y += velocity.y; + } + } + }; + Bounds.contains = function(bounds, point) { + return point.x >= bounds.min.x && point.x <= bounds.max.x && point.y >= bounds.min.y && point.y <= bounds.max.y; + }; + Bounds.overlaps = function(boundsA, boundsB) { + return boundsA.min.x <= boundsB.max.x && boundsA.max.x >= boundsB.min.x && boundsA.max.y >= boundsB.min.y && boundsA.min.y <= boundsB.max.y; + }; + Bounds.translate = function(bounds, vector) { + bounds.min.x += vector.x; + bounds.max.x += vector.x; + bounds.min.y += vector.y; + bounds.max.y += vector.y; + }; + Bounds.shift = function(bounds, position) { + var deltaX = bounds.max.x - bounds.min.x, deltaY = bounds.max.y - bounds.min.y; + bounds.min.x = position.x; + bounds.max.x = position.x + deltaX; + bounds.min.y = position.y; + bounds.max.y = position.y + deltaY; + }; + })(); + }, + function(module2, exports2) { + var Vector2 = {}; + module2.exports = Vector2; + (function() { + Vector2.create = function(x, y) { + return { x: x || 0, y: y || 0 }; + }; + Vector2.clone = function(vector) { + return { x: vector.x, y: vector.y }; + }; + Vector2.magnitude = function(vector) { + return Math.sqrt(vector.x * vector.x + vector.y * vector.y); + }; + Vector2.magnitudeSquared = function(vector) { + return vector.x * vector.x + vector.y * vector.y; + }; + Vector2.rotate = function(vector, angle, output) { + var cos = Math.cos(angle), sin = Math.sin(angle); + if (!output) + output = {}; + var x = vector.x * cos - vector.y * sin; + output.y = vector.x * sin + vector.y * cos; + output.x = x; + return output; + }; + Vector2.rotateAbout = function(vector, angle, point, output) { + var cos = Math.cos(angle), sin = Math.sin(angle); + if (!output) + output = {}; + var x = point.x + ((vector.x - point.x) * cos - (vector.y - point.y) * sin); + output.y = point.y + ((vector.x - point.x) * sin + (vector.y - point.y) * cos); + output.x = x; + return output; + }; + Vector2.normalise = function(vector) { + var magnitude = Vector2.magnitude(vector); + if (magnitude === 0) + return { x: 0, y: 0 }; + return { x: vector.x / magnitude, y: vector.y / magnitude }; + }; + Vector2.dot = function(vectorA, vectorB) { + return vectorA.x * vectorB.x + vectorA.y * vectorB.y; + }; + Vector2.cross = function(vectorA, vectorB) { + return vectorA.x * vectorB.y - vectorA.y * vectorB.x; + }; + Vector2.cross3 = function(vectorA, vectorB, vectorC) { + return (vectorB.x - vectorA.x) * (vectorC.y - vectorA.y) - (vectorB.y - vectorA.y) * (vectorC.x - vectorA.x); + }; + Vector2.add = function(vectorA, vectorB, output) { + if (!output) + output = {}; + output.x = vectorA.x + vectorB.x; + output.y = vectorA.y + vectorB.y; + return output; + }; + Vector2.sub = function(vectorA, vectorB, output) { + if (!output) + output = {}; + output.x = vectorA.x - vectorB.x; + output.y = vectorA.y - vectorB.y; + return output; + }; + Vector2.mult = function(vector, scalar) { + return { x: vector.x * scalar, y: vector.y * scalar }; + }; + Vector2.div = function(vector, scalar) { + return { x: vector.x / scalar, y: vector.y / scalar }; + }; + Vector2.perp = function(vector, negate) { + negate = negate === true ? -1 : 1; + return { x: negate * -vector.y, y: negate * vector.x }; + }; + Vector2.neg = function(vector) { + return { x: -vector.x, y: -vector.y }; + }; + Vector2.angle = function(vectorA, vectorB) { + return Math.atan2(vectorB.y - vectorA.y, vectorB.x - vectorA.x); + }; + Vector2._temp = [ + Vector2.create(), + Vector2.create(), + Vector2.create(), + Vector2.create(), + Vector2.create(), + Vector2.create() + ]; + })(); + }, + function(module2, exports2, __webpack_require__) { + var Vertices = {}; + module2.exports = Vertices; + var Vector2 = __webpack_require__(2); + var Common = __webpack_require__(0); + (function() { + Vertices.create = function(points, body) { + var vertices = []; + for (var i = 0;i < points.length; i++) { + var point = points[i], vertex = { + x: point.x, + y: point.y, + index: i, + body, + isInternal: false + }; + vertices.push(vertex); + } + return vertices; + }; + Vertices.fromPath = function(path, body) { + var pathPattern = /L?\s*([-\d.e]+)[\s,]*([-\d.e]+)*/ig, points = []; + path.replace(pathPattern, function(match, x, y) { + points.push({ x: parseFloat(x), y: parseFloat(y) }); + }); + return Vertices.create(points, body); + }; + Vertices.centre = function(vertices) { + var area = Vertices.area(vertices, true), centre = { x: 0, y: 0 }, cross, temp, j; + for (var i = 0;i < vertices.length; i++) { + j = (i + 1) % vertices.length; + cross = Vector2.cross(vertices[i], vertices[j]); + temp = Vector2.mult(Vector2.add(vertices[i], vertices[j]), cross); + centre = Vector2.add(centre, temp); + } + return Vector2.div(centre, 6 * area); + }; + Vertices.mean = function(vertices) { + var average = { x: 0, y: 0 }; + for (var i = 0;i < vertices.length; i++) { + average.x += vertices[i].x; + average.y += vertices[i].y; + } + return Vector2.div(average, vertices.length); + }; + Vertices.area = function(vertices, signed) { + var area = 0, j = vertices.length - 1; + for (var i = 0;i < vertices.length; i++) { + area += (vertices[j].x - vertices[i].x) * (vertices[j].y + vertices[i].y); + j = i; + } + if (signed) + return area / 2; + return Math.abs(area) / 2; + }; + Vertices.inertia = function(vertices, mass) { + var numerator = 0, denominator = 0, v = vertices, cross, j; + for (var n = 0;n < v.length; n++) { + j = (n + 1) % v.length; + cross = Math.abs(Vector2.cross(v[j], v[n])); + numerator += cross * (Vector2.dot(v[j], v[j]) + Vector2.dot(v[j], v[n]) + Vector2.dot(v[n], v[n])); + denominator += cross; + } + return mass / 6 * (numerator / denominator); + }; + Vertices.translate = function(vertices, vector, scalar) { + scalar = typeof scalar !== "undefined" ? scalar : 1; + var verticesLength = vertices.length, translateX = vector.x * scalar, translateY = vector.y * scalar, i; + for (i = 0;i < verticesLength; i++) { + vertices[i].x += translateX; + vertices[i].y += translateY; + } + return vertices; + }; + Vertices.rotate = function(vertices, angle, point) { + if (angle === 0) + return; + var cos = Math.cos(angle), sin = Math.sin(angle), pointX = point.x, pointY = point.y, verticesLength = vertices.length, vertex, dx, dy, i; + for (i = 0;i < verticesLength; i++) { + vertex = vertices[i]; + dx = vertex.x - pointX; + dy = vertex.y - pointY; + vertex.x = pointX + (dx * cos - dy * sin); + vertex.y = pointY + (dx * sin + dy * cos); + } + return vertices; + }; + Vertices.contains = function(vertices, point) { + var { x: pointX, y: pointY } = point, verticesLength = vertices.length, vertex = vertices[verticesLength - 1], nextVertex; + for (var i = 0;i < verticesLength; i++) { + nextVertex = vertices[i]; + if ((pointX - vertex.x) * (nextVertex.y - vertex.y) + (pointY - vertex.y) * (vertex.x - nextVertex.x) > 0) { + return false; + } + vertex = nextVertex; + } + return true; + }; + Vertices.scale = function(vertices, scaleX, scaleY, point) { + if (scaleX === 1 && scaleY === 1) + return vertices; + point = point || Vertices.centre(vertices); + var vertex, delta; + for (var i = 0;i < vertices.length; i++) { + vertex = vertices[i]; + delta = Vector2.sub(vertex, point); + vertices[i].x = point.x + delta.x * scaleX; + vertices[i].y = point.y + delta.y * scaleY; + } + return vertices; + }; + Vertices.chamfer = function(vertices, radius, quality, qualityMin, qualityMax) { + if (typeof radius === "number") { + radius = [radius]; + } else { + radius = radius || [8]; + } + quality = typeof quality !== "undefined" ? quality : -1; + qualityMin = qualityMin || 2; + qualityMax = qualityMax || 14; + var newVertices = []; + for (var i = 0;i < vertices.length; i++) { + var prevVertex = vertices[i - 1 >= 0 ? i - 1 : vertices.length - 1], vertex = vertices[i], nextVertex = vertices[(i + 1) % vertices.length], currentRadius = radius[i < radius.length ? i : radius.length - 1]; + if (currentRadius === 0) { + newVertices.push(vertex); + continue; + } + var prevNormal = Vector2.normalise({ + x: vertex.y - prevVertex.y, + y: prevVertex.x - vertex.x + }); + var nextNormal = Vector2.normalise({ + x: nextVertex.y - vertex.y, + y: vertex.x - nextVertex.x + }); + var diagonalRadius = Math.sqrt(2 * Math.pow(currentRadius, 2)), radiusVector = Vector2.mult(Common.clone(prevNormal), currentRadius), midNormal = Vector2.normalise(Vector2.mult(Vector2.add(prevNormal, nextNormal), 0.5)), scaledVertex = Vector2.sub(vertex, Vector2.mult(midNormal, diagonalRadius)); + var precision = quality; + if (quality === -1) { + precision = Math.pow(currentRadius, 0.32) * 1.75; + } + precision = Common.clamp(precision, qualityMin, qualityMax); + if (precision % 2 === 1) + precision += 1; + var alpha = Math.acos(Vector2.dot(prevNormal, nextNormal)), theta = alpha / precision; + for (var j = 0;j < precision; j++) { + newVertices.push(Vector2.add(Vector2.rotate(radiusVector, theta * j), scaledVertex)); + } + } + return newVertices; + }; + Vertices.clockwiseSort = function(vertices) { + var centre = Vertices.mean(vertices); + vertices.sort(function(vertexA, vertexB) { + return Vector2.angle(centre, vertexA) - Vector2.angle(centre, vertexB); + }); + return vertices; + }; + Vertices.isConvex = function(vertices) { + var flag = 0, n = vertices.length, i, j, k, z; + if (n < 3) + return null; + for (i = 0;i < n; i++) { + j = (i + 1) % n; + k = (i + 2) % n; + z = (vertices[j].x - vertices[i].x) * (vertices[k].y - vertices[j].y); + z -= (vertices[j].y - vertices[i].y) * (vertices[k].x - vertices[j].x); + if (z < 0) { + flag |= 1; + } else if (z > 0) { + flag |= 2; + } + if (flag === 3) { + return false; + } + } + if (flag !== 0) { + return true; + } else { + return null; + } + }; + Vertices.hull = function(vertices) { + var upper = [], lower = [], vertex, i; + vertices = vertices.slice(0); + vertices.sort(function(vertexA, vertexB) { + var dx = vertexA.x - vertexB.x; + return dx !== 0 ? dx : vertexA.y - vertexB.y; + }); + for (i = 0;i < vertices.length; i += 1) { + vertex = vertices[i]; + while (lower.length >= 2 && Vector2.cross3(lower[lower.length - 2], lower[lower.length - 1], vertex) <= 0) { + lower.pop(); + } + lower.push(vertex); + } + for (i = vertices.length - 1;i >= 0; i -= 1) { + vertex = vertices[i]; + while (upper.length >= 2 && Vector2.cross3(upper[upper.length - 2], upper[upper.length - 1], vertex) <= 0) { + upper.pop(); + } + upper.push(vertex); + } + upper.pop(); + lower.pop(); + return upper.concat(lower); + }; + })(); + }, + function(module2, exports2, __webpack_require__) { + var Body = {}; + module2.exports = Body; + var Vertices = __webpack_require__(3); + var Vector2 = __webpack_require__(2); + var Sleeping = __webpack_require__(7); + var Common = __webpack_require__(0); + var Bounds = __webpack_require__(1); + var Axes = __webpack_require__(11); + (function() { + Body._timeCorrection = true; + Body._inertiaScale = 4; + Body._nextCollidingGroupId = 1; + Body._nextNonCollidingGroupId = -1; + Body._nextCategory = 1; + Body._baseDelta = 1000 / 60; + Body.create = function(options) { + var defaults = { + id: Common.nextId(), + type: "body", + label: "Body", + parts: [], + plugin: {}, + angle: 0, + vertices: Vertices.fromPath("L 0 0 L 40 0 L 40 40 L 0 40"), + position: { x: 0, y: 0 }, + force: { x: 0, y: 0 }, + torque: 0, + positionImpulse: { x: 0, y: 0 }, + constraintImpulse: { x: 0, y: 0, angle: 0 }, + totalContacts: 0, + speed: 0, + angularSpeed: 0, + velocity: { x: 0, y: 0 }, + angularVelocity: 0, + isSensor: false, + isStatic: false, + isSleeping: false, + motion: 0, + sleepThreshold: 60, + density: 0.001, + restitution: 0, + friction: 0.1, + frictionStatic: 0.5, + frictionAir: 0.01, + collisionFilter: { + category: 1, + mask: 4294967295, + group: 0 + }, + slop: 0.05, + timeScale: 1, + render: { + visible: true, + opacity: 1, + strokeStyle: null, + fillStyle: null, + lineWidth: null, + sprite: { + xScale: 1, + yScale: 1, + xOffset: 0, + yOffset: 0 + } + }, + events: null, + bounds: null, + chamfer: null, + circleRadius: 0, + positionPrev: null, + anglePrev: 0, + parent: null, + axes: null, + area: 0, + mass: 0, + inertia: 0, + deltaTime: 1000 / 60, + _original: null + }; + var body = Common.extend(defaults, options); + _initProperties(body, options); + return body; + }; + Body.nextGroup = function(isNonColliding) { + if (isNonColliding) + return Body._nextNonCollidingGroupId--; + return Body._nextCollidingGroupId++; + }; + Body.nextCategory = function() { + Body._nextCategory = Body._nextCategory << 1; + return Body._nextCategory; + }; + var _initProperties = function(body, options) { + options = options || {}; + Body.set(body, { + bounds: body.bounds || Bounds.create(body.vertices), + positionPrev: body.positionPrev || Vector2.clone(body.position), + anglePrev: body.anglePrev || body.angle, + vertices: body.vertices, + parts: body.parts || [body], + isStatic: body.isStatic, + isSleeping: body.isSleeping, + parent: body.parent || body + }); + Vertices.rotate(body.vertices, body.angle, body.position); + Axes.rotate(body.axes, body.angle); + Bounds.update(body.bounds, body.vertices, body.velocity); + Body.set(body, { + axes: options.axes || body.axes, + area: options.area || body.area, + mass: options.mass || body.mass, + inertia: options.inertia || body.inertia + }); + var defaultFillStyle = body.isStatic ? "#14151f" : Common.choose(["#f19648", "#f5d259", "#f55a3c", "#063e7b", "#ececd1"]), defaultStrokeStyle = body.isStatic ? "#555" : "#ccc", defaultLineWidth = body.isStatic && body.render.fillStyle === null ? 1 : 0; + body.render.fillStyle = body.render.fillStyle || defaultFillStyle; + body.render.strokeStyle = body.render.strokeStyle || defaultStrokeStyle; + body.render.lineWidth = body.render.lineWidth || defaultLineWidth; + body.render.sprite.xOffset += -(body.bounds.min.x - body.position.x) / (body.bounds.max.x - body.bounds.min.x); + body.render.sprite.yOffset += -(body.bounds.min.y - body.position.y) / (body.bounds.max.y - body.bounds.min.y); + }; + Body.set = function(body, settings, value) { + var property; + if (typeof settings === "string") { + property = settings; + settings = {}; + settings[property] = value; + } + for (property in settings) { + if (!Object.prototype.hasOwnProperty.call(settings, property)) + continue; + value = settings[property]; + switch (property) { + case "isStatic": + Body.setStatic(body, value); + break; + case "isSleeping": + Sleeping.set(body, value); + break; + case "mass": + Body.setMass(body, value); + break; + case "density": + Body.setDensity(body, value); + break; + case "inertia": + Body.setInertia(body, value); + break; + case "vertices": + Body.setVertices(body, value); + break; + case "position": + Body.setPosition(body, value); + break; + case "angle": + Body.setAngle(body, value); + break; + case "velocity": + Body.setVelocity(body, value); + break; + case "angularVelocity": + Body.setAngularVelocity(body, value); + break; + case "speed": + Body.setSpeed(body, value); + break; + case "angularSpeed": + Body.setAngularSpeed(body, value); + break; + case "parts": + Body.setParts(body, value); + break; + case "centre": + Body.setCentre(body, value); + break; + default: + body[property] = value; + } + } + }; + Body.setStatic = function(body, isStatic) { + for (var i = 0;i < body.parts.length; i++) { + var part = body.parts[i]; + if (isStatic) { + if (!part.isStatic) { + part._original = { + restitution: part.restitution, + friction: part.friction, + mass: part.mass, + inertia: part.inertia, + density: part.density, + inverseMass: part.inverseMass, + inverseInertia: part.inverseInertia + }; + } + part.restitution = 0; + part.friction = 1; + part.mass = part.inertia = part.density = Infinity; + part.inverseMass = part.inverseInertia = 0; + part.positionPrev.x = part.position.x; + part.positionPrev.y = part.position.y; + part.anglePrev = part.angle; + part.angularVelocity = 0; + part.speed = 0; + part.angularSpeed = 0; + part.motion = 0; + } else if (part._original) { + part.restitution = part._original.restitution; + part.friction = part._original.friction; + part.mass = part._original.mass; + part.inertia = part._original.inertia; + part.density = part._original.density; + part.inverseMass = part._original.inverseMass; + part.inverseInertia = part._original.inverseInertia; + part._original = null; + } + part.isStatic = isStatic; + } + }; + Body.setMass = function(body, mass) { + var moment = body.inertia / (body.mass / 6); + body.inertia = moment * (mass / 6); + body.inverseInertia = 1 / body.inertia; + body.mass = mass; + body.inverseMass = 1 / body.mass; + body.density = body.mass / body.area; + }; + Body.setDensity = function(body, density) { + Body.setMass(body, density * body.area); + body.density = density; + }; + Body.setInertia = function(body, inertia) { + body.inertia = inertia; + body.inverseInertia = 1 / body.inertia; + }; + Body.setVertices = function(body, vertices) { + if (vertices[0].body === body) { + body.vertices = vertices; + } else { + body.vertices = Vertices.create(vertices, body); + } + body.axes = Axes.fromVertices(body.vertices); + body.area = Vertices.area(body.vertices); + Body.setMass(body, body.density * body.area); + var centre = Vertices.centre(body.vertices); + Vertices.translate(body.vertices, centre, -1); + Body.setInertia(body, Body._inertiaScale * Vertices.inertia(body.vertices, body.mass)); + Vertices.translate(body.vertices, body.position); + Bounds.update(body.bounds, body.vertices, body.velocity); + }; + Body.setParts = function(body, parts, autoHull) { + var i; + parts = parts.slice(0); + body.parts.length = 0; + body.parts.push(body); + body.parent = body; + for (i = 0;i < parts.length; i++) { + var part = parts[i]; + if (part !== body) { + part.parent = body; + body.parts.push(part); + } + } + if (body.parts.length === 1) + return; + autoHull = typeof autoHull !== "undefined" ? autoHull : true; + if (autoHull) { + var vertices = []; + for (i = 0;i < parts.length; i++) { + vertices = vertices.concat(parts[i].vertices); + } + Vertices.clockwiseSort(vertices); + var hull = Vertices.hull(vertices), hullCentre = Vertices.centre(hull); + Body.setVertices(body, hull); + Vertices.translate(body.vertices, hullCentre); + } + var total = Body._totalProperties(body); + body.area = total.area; + body.parent = body; + body.position.x = total.centre.x; + body.position.y = total.centre.y; + body.positionPrev.x = total.centre.x; + body.positionPrev.y = total.centre.y; + Body.setMass(body, total.mass); + Body.setInertia(body, total.inertia); + Body.setPosition(body, total.centre); + }; + Body.setCentre = function(body, centre, relative) { + if (!relative) { + body.positionPrev.x = centre.x - (body.position.x - body.positionPrev.x); + body.positionPrev.y = centre.y - (body.position.y - body.positionPrev.y); + body.position.x = centre.x; + body.position.y = centre.y; + } else { + body.positionPrev.x += centre.x; + body.positionPrev.y += centre.y; + body.position.x += centre.x; + body.position.y += centre.y; + } + }; + Body.setPosition = function(body, position, updateVelocity) { + var delta = Vector2.sub(position, body.position); + if (updateVelocity) { + body.positionPrev.x = body.position.x; + body.positionPrev.y = body.position.y; + body.velocity.x = delta.x; + body.velocity.y = delta.y; + body.speed = Vector2.magnitude(delta); + } else { + body.positionPrev.x += delta.x; + body.positionPrev.y += delta.y; + } + for (var i = 0;i < body.parts.length; i++) { + var part = body.parts[i]; + part.position.x += delta.x; + part.position.y += delta.y; + Vertices.translate(part.vertices, delta); + Bounds.update(part.bounds, part.vertices, body.velocity); + } + }; + Body.setAngle = function(body, angle, updateVelocity) { + var delta = angle - body.angle; + if (updateVelocity) { + body.anglePrev = body.angle; + body.angularVelocity = delta; + body.angularSpeed = Math.abs(delta); + } else { + body.anglePrev += delta; + } + for (var i = 0;i < body.parts.length; i++) { + var part = body.parts[i]; + part.angle += delta; + Vertices.rotate(part.vertices, delta, body.position); + Axes.rotate(part.axes, delta); + Bounds.update(part.bounds, part.vertices, body.velocity); + if (i > 0) { + Vector2.rotateAbout(part.position, delta, body.position, part.position); + } + } + }; + Body.setVelocity = function(body, velocity) { + var timeScale = body.deltaTime / Body._baseDelta; + body.positionPrev.x = body.position.x - velocity.x * timeScale; + body.positionPrev.y = body.position.y - velocity.y * timeScale; + body.velocity.x = (body.position.x - body.positionPrev.x) / timeScale; + body.velocity.y = (body.position.y - body.positionPrev.y) / timeScale; + body.speed = Vector2.magnitude(body.velocity); + }; + Body.getVelocity = function(body) { + var timeScale = Body._baseDelta / body.deltaTime; + return { + x: (body.position.x - body.positionPrev.x) * timeScale, + y: (body.position.y - body.positionPrev.y) * timeScale + }; + }; + Body.getSpeed = function(body) { + return Vector2.magnitude(Body.getVelocity(body)); + }; + Body.setSpeed = function(body, speed) { + Body.setVelocity(body, Vector2.mult(Vector2.normalise(Body.getVelocity(body)), speed)); + }; + Body.setAngularVelocity = function(body, velocity) { + var timeScale = body.deltaTime / Body._baseDelta; + body.anglePrev = body.angle - velocity * timeScale; + body.angularVelocity = (body.angle - body.anglePrev) / timeScale; + body.angularSpeed = Math.abs(body.angularVelocity); + }; + Body.getAngularVelocity = function(body) { + return (body.angle - body.anglePrev) * Body._baseDelta / body.deltaTime; + }; + Body.getAngularSpeed = function(body) { + return Math.abs(Body.getAngularVelocity(body)); + }; + Body.setAngularSpeed = function(body, speed) { + Body.setAngularVelocity(body, Common.sign(Body.getAngularVelocity(body)) * speed); + }; + Body.translate = function(body, translation, updateVelocity) { + Body.setPosition(body, Vector2.add(body.position, translation), updateVelocity); + }; + Body.rotate = function(body, rotation, point, updateVelocity) { + if (!point) { + Body.setAngle(body, body.angle + rotation, updateVelocity); + } else { + var cos = Math.cos(rotation), sin = Math.sin(rotation), dx = body.position.x - point.x, dy = body.position.y - point.y; + Body.setPosition(body, { + x: point.x + (dx * cos - dy * sin), + y: point.y + (dx * sin + dy * cos) + }, updateVelocity); + Body.setAngle(body, body.angle + rotation, updateVelocity); + } + }; + Body.scale = function(body, scaleX, scaleY, point) { + var totalArea = 0, totalInertia = 0; + point = point || body.position; + for (var i = 0;i < body.parts.length; i++) { + var part = body.parts[i]; + Vertices.scale(part.vertices, scaleX, scaleY, point); + part.axes = Axes.fromVertices(part.vertices); + part.area = Vertices.area(part.vertices); + Body.setMass(part, body.density * part.area); + Vertices.translate(part.vertices, { x: -part.position.x, y: -part.position.y }); + Body.setInertia(part, Body._inertiaScale * Vertices.inertia(part.vertices, part.mass)); + Vertices.translate(part.vertices, { x: part.position.x, y: part.position.y }); + if (i > 0) { + totalArea += part.area; + totalInertia += part.inertia; + } + part.position.x = point.x + (part.position.x - point.x) * scaleX; + part.position.y = point.y + (part.position.y - point.y) * scaleY; + Bounds.update(part.bounds, part.vertices, body.velocity); + } + if (body.parts.length > 1) { + body.area = totalArea; + if (!body.isStatic) { + Body.setMass(body, body.density * totalArea); + Body.setInertia(body, totalInertia); + } + } + if (body.circleRadius) { + if (scaleX === scaleY) { + body.circleRadius *= scaleX; + } else { + body.circleRadius = null; + } + } + }; + Body.update = function(body, deltaTime) { + deltaTime = (typeof deltaTime !== "undefined" ? deltaTime : 1000 / 60) * body.timeScale; + var deltaTimeSquared = deltaTime * deltaTime, correction = Body._timeCorrection ? deltaTime / (body.deltaTime || deltaTime) : 1; + var frictionAir = 1 - body.frictionAir * (deltaTime / Common._baseDelta), velocityPrevX = (body.position.x - body.positionPrev.x) * correction, velocityPrevY = (body.position.y - body.positionPrev.y) * correction; + body.velocity.x = velocityPrevX * frictionAir + body.force.x / body.mass * deltaTimeSquared; + body.velocity.y = velocityPrevY * frictionAir + body.force.y / body.mass * deltaTimeSquared; + body.positionPrev.x = body.position.x; + body.positionPrev.y = body.position.y; + body.position.x += body.velocity.x; + body.position.y += body.velocity.y; + body.deltaTime = deltaTime; + body.angularVelocity = (body.angle - body.anglePrev) * frictionAir * correction + body.torque / body.inertia * deltaTimeSquared; + body.anglePrev = body.angle; + body.angle += body.angularVelocity; + for (var i = 0;i < body.parts.length; i++) { + var part = body.parts[i]; + Vertices.translate(part.vertices, body.velocity); + if (i > 0) { + part.position.x += body.velocity.x; + part.position.y += body.velocity.y; + } + if (body.angularVelocity !== 0) { + Vertices.rotate(part.vertices, body.angularVelocity, body.position); + Axes.rotate(part.axes, body.angularVelocity); + if (i > 0) { + Vector2.rotateAbout(part.position, body.angularVelocity, body.position, part.position); + } + } + Bounds.update(part.bounds, part.vertices, body.velocity); + } + }; + Body.updateVelocities = function(body) { + var timeScale = Body._baseDelta / body.deltaTime, bodyVelocity = body.velocity; + bodyVelocity.x = (body.position.x - body.positionPrev.x) * timeScale; + bodyVelocity.y = (body.position.y - body.positionPrev.y) * timeScale; + body.speed = Math.sqrt(bodyVelocity.x * bodyVelocity.x + bodyVelocity.y * bodyVelocity.y); + body.angularVelocity = (body.angle - body.anglePrev) * timeScale; + body.angularSpeed = Math.abs(body.angularVelocity); + }; + Body.applyForce = function(body, position, force) { + var offset = { x: position.x - body.position.x, y: position.y - body.position.y }; + body.force.x += force.x; + body.force.y += force.y; + body.torque += offset.x * force.y - offset.y * force.x; + }; + Body._totalProperties = function(body) { + var properties = { + mass: 0, + area: 0, + inertia: 0, + centre: { x: 0, y: 0 } + }; + for (var i = body.parts.length === 1 ? 0 : 1;i < body.parts.length; i++) { + var part = body.parts[i], mass = part.mass !== Infinity ? part.mass : 1; + properties.mass += mass; + properties.area += part.area; + properties.inertia += part.inertia; + properties.centre = Vector2.add(properties.centre, Vector2.mult(part.position, mass)); + } + properties.centre = Vector2.div(properties.centre, properties.mass); + return properties; + }; + })(); + }, + function(module2, exports2, __webpack_require__) { + var Events = {}; + module2.exports = Events; + var Common = __webpack_require__(0); + (function() { + Events.on = function(object, eventNames, callback) { + var names = eventNames.split(" "), name; + for (var i = 0;i < names.length; i++) { + name = names[i]; + object.events = object.events || {}; + object.events[name] = object.events[name] || []; + object.events[name].push(callback); + } + return callback; + }; + Events.off = function(object, eventNames, callback) { + if (!eventNames) { + object.events = {}; + return; + } + if (typeof eventNames === "function") { + callback = eventNames; + eventNames = Common.keys(object.events).join(" "); + } + var names = eventNames.split(" "); + for (var i = 0;i < names.length; i++) { + var callbacks = object.events[names[i]], newCallbacks = []; + if (callback && callbacks) { + for (var j = 0;j < callbacks.length; j++) { + if (callbacks[j] !== callback) + newCallbacks.push(callbacks[j]); + } + } + object.events[names[i]] = newCallbacks; + } + }; + Events.trigger = function(object, eventNames, event) { + var names, name, callbacks, eventClone; + var events = object.events; + if (events && Common.keys(events).length > 0) { + if (!event) + event = {}; + names = eventNames.split(" "); + for (var i = 0;i < names.length; i++) { + name = names[i]; + callbacks = events[name]; + if (callbacks) { + eventClone = Common.clone(event, false); + eventClone.name = name; + eventClone.source = object; + for (var j = 0;j < callbacks.length; j++) { + callbacks[j].apply(object, [eventClone]); + } + } + } + } + }; + })(); + }, + function(module2, exports2, __webpack_require__) { + var Composite = {}; + module2.exports = Composite; + var Events = __webpack_require__(5); + var Common = __webpack_require__(0); + var Bounds = __webpack_require__(1); + var Body = __webpack_require__(4); + (function() { + Composite.create = function(options) { + return Common.extend({ + id: Common.nextId(), + type: "composite", + parent: null, + isModified: false, + bodies: [], + constraints: [], + composites: [], + label: "Composite", + plugin: {}, + cache: { + allBodies: null, + allConstraints: null, + allComposites: null + } + }, options); + }; + Composite.setModified = function(composite, isModified, updateParents, updateChildren) { + composite.isModified = isModified; + if (isModified && composite.cache) { + composite.cache.allBodies = null; + composite.cache.allConstraints = null; + composite.cache.allComposites = null; + } + if (updateParents && composite.parent) { + Composite.setModified(composite.parent, isModified, updateParents, updateChildren); + } + if (updateChildren) { + for (var i = 0;i < composite.composites.length; i++) { + var childComposite = composite.composites[i]; + Composite.setModified(childComposite, isModified, updateParents, updateChildren); + } + } + }; + Composite.add = function(composite, object) { + var objects = [].concat(object); + Events.trigger(composite, "beforeAdd", { object }); + for (var i = 0;i < objects.length; i++) { + var obj = objects[i]; + switch (obj.type) { + case "body": + if (obj.parent !== obj) { + Common.warn("Composite.add: skipped adding a compound body part (you must add its parent instead)"); + break; + } + Composite.addBody(composite, obj); + break; + case "constraint": + Composite.addConstraint(composite, obj); + break; + case "composite": + Composite.addComposite(composite, obj); + break; + case "mouseConstraint": + Composite.addConstraint(composite, obj.constraint); + break; + } + } + Events.trigger(composite, "afterAdd", { object }); + return composite; + }; + Composite.remove = function(composite, object, deep) { + var objects = [].concat(object); + Events.trigger(composite, "beforeRemove", { object }); + for (var i = 0;i < objects.length; i++) { + var obj = objects[i]; + switch (obj.type) { + case "body": + Composite.removeBody(composite, obj, deep); + break; + case "constraint": + Composite.removeConstraint(composite, obj, deep); + break; + case "composite": + Composite.removeComposite(composite, obj, deep); + break; + case "mouseConstraint": + Composite.removeConstraint(composite, obj.constraint); + break; + } + } + Events.trigger(composite, "afterRemove", { object }); + return composite; + }; + Composite.addComposite = function(compositeA, compositeB) { + compositeA.composites.push(compositeB); + compositeB.parent = compositeA; + Composite.setModified(compositeA, true, true, false); + return compositeA; + }; + Composite.removeComposite = function(compositeA, compositeB, deep) { + var position = Common.indexOf(compositeA.composites, compositeB); + if (position !== -1) { + var bodies = Composite.allBodies(compositeB); + Composite.removeCompositeAt(compositeA, position); + for (var i = 0;i < bodies.length; i++) { + bodies[i].sleepCounter = 0; + } + } + if (deep) { + for (var i = 0;i < compositeA.composites.length; i++) { + Composite.removeComposite(compositeA.composites[i], compositeB, true); + } + } + return compositeA; + }; + Composite.removeCompositeAt = function(composite, position) { + composite.composites.splice(position, 1); + Composite.setModified(composite, true, true, false); + return composite; + }; + Composite.addBody = function(composite, body) { + composite.bodies.push(body); + Composite.setModified(composite, true, true, false); + return composite; + }; + Composite.removeBody = function(composite, body, deep) { + var position = Common.indexOf(composite.bodies, body); + if (position !== -1) { + Composite.removeBodyAt(composite, position); + body.sleepCounter = 0; + } + if (deep) { + for (var i = 0;i < composite.composites.length; i++) { + Composite.removeBody(composite.composites[i], body, true); + } + } + return composite; + }; + Composite.removeBodyAt = function(composite, position) { + composite.bodies.splice(position, 1); + Composite.setModified(composite, true, true, false); + return composite; + }; + Composite.addConstraint = function(composite, constraint) { + composite.constraints.push(constraint); + Composite.setModified(composite, true, true, false); + return composite; + }; + Composite.removeConstraint = function(composite, constraint, deep) { + var position = Common.indexOf(composite.constraints, constraint); + if (position !== -1) { + Composite.removeConstraintAt(composite, position); + } + if (deep) { + for (var i = 0;i < composite.composites.length; i++) { + Composite.removeConstraint(composite.composites[i], constraint, true); + } + } + return composite; + }; + Composite.removeConstraintAt = function(composite, position) { + composite.constraints.splice(position, 1); + Composite.setModified(composite, true, true, false); + return composite; + }; + Composite.clear = function(composite, keepStatic, deep) { + if (deep) { + for (var i = 0;i < composite.composites.length; i++) { + Composite.clear(composite.composites[i], keepStatic, true); + } + } + if (keepStatic) { + composite.bodies = composite.bodies.filter(function(body) { + return body.isStatic; + }); + } else { + composite.bodies.length = 0; + } + composite.constraints.length = 0; + composite.composites.length = 0; + Composite.setModified(composite, true, true, false); + return composite; + }; + Composite.allBodies = function(composite) { + if (composite.cache && composite.cache.allBodies) { + return composite.cache.allBodies; + } + var bodies = [].concat(composite.bodies); + for (var i = 0;i < composite.composites.length; i++) + bodies = bodies.concat(Composite.allBodies(composite.composites[i])); + if (composite.cache) { + composite.cache.allBodies = bodies; + } + return bodies; + }; + Composite.allConstraints = function(composite) { + if (composite.cache && composite.cache.allConstraints) { + return composite.cache.allConstraints; + } + var constraints = [].concat(composite.constraints); + for (var i = 0;i < composite.composites.length; i++) + constraints = constraints.concat(Composite.allConstraints(composite.composites[i])); + if (composite.cache) { + composite.cache.allConstraints = constraints; + } + return constraints; + }; + Composite.allComposites = function(composite) { + if (composite.cache && composite.cache.allComposites) { + return composite.cache.allComposites; + } + var composites = [].concat(composite.composites); + for (var i = 0;i < composite.composites.length; i++) + composites = composites.concat(Composite.allComposites(composite.composites[i])); + if (composite.cache) { + composite.cache.allComposites = composites; + } + return composites; + }; + Composite.get = function(composite, id, type) { + var objects, object; + switch (type) { + case "body": + objects = Composite.allBodies(composite); + break; + case "constraint": + objects = Composite.allConstraints(composite); + break; + case "composite": + objects = Composite.allComposites(composite).concat(composite); + break; + } + if (!objects) + return null; + object = objects.filter(function(object2) { + return object2.id.toString() === id.toString(); + }); + return object.length === 0 ? null : object[0]; + }; + Composite.move = function(compositeA, objects, compositeB) { + Composite.remove(compositeA, objects); + Composite.add(compositeB, objects); + return compositeA; + }; + Composite.rebase = function(composite) { + var objects = Composite.allBodies(composite).concat(Composite.allConstraints(composite)).concat(Composite.allComposites(composite)); + for (var i = 0;i < objects.length; i++) { + objects[i].id = Common.nextId(); + } + return composite; + }; + Composite.translate = function(composite, translation, recursive) { + var bodies = recursive ? Composite.allBodies(composite) : composite.bodies; + for (var i = 0;i < bodies.length; i++) { + Body.translate(bodies[i], translation); + } + return composite; + }; + Composite.rotate = function(composite, rotation, point, recursive) { + var cos = Math.cos(rotation), sin = Math.sin(rotation), bodies = recursive ? Composite.allBodies(composite) : composite.bodies; + for (var i = 0;i < bodies.length; i++) { + var body = bodies[i], dx = body.position.x - point.x, dy = body.position.y - point.y; + Body.setPosition(body, { + x: point.x + (dx * cos - dy * sin), + y: point.y + (dx * sin + dy * cos) + }); + Body.rotate(body, rotation); + } + return composite; + }; + Composite.scale = function(composite, scaleX, scaleY, point, recursive) { + var bodies = recursive ? Composite.allBodies(composite) : composite.bodies; + for (var i = 0;i < bodies.length; i++) { + var body = bodies[i], dx = body.position.x - point.x, dy = body.position.y - point.y; + Body.setPosition(body, { + x: point.x + dx * scaleX, + y: point.y + dy * scaleY + }); + Body.scale(body, scaleX, scaleY); + } + return composite; + }; + Composite.bounds = function(composite) { + var bodies = Composite.allBodies(composite), vertices = []; + for (var i = 0;i < bodies.length; i += 1) { + var body = bodies[i]; + vertices.push(body.bounds.min, body.bounds.max); + } + return Bounds.create(vertices); + }; + })(); + }, + function(module2, exports2, __webpack_require__) { + var Sleeping = {}; + module2.exports = Sleeping; + var Body = __webpack_require__(4); + var Events = __webpack_require__(5); + var Common = __webpack_require__(0); + (function() { + Sleeping._motionWakeThreshold = 0.18; + Sleeping._motionSleepThreshold = 0.08; + Sleeping._minBias = 0.9; + Sleeping.update = function(bodies, delta) { + var timeScale = delta / Common._baseDelta, motionSleepThreshold = Sleeping._motionSleepThreshold; + for (var i = 0;i < bodies.length; i++) { + var body = bodies[i], speed = Body.getSpeed(body), angularSpeed = Body.getAngularSpeed(body), motion = speed * speed + angularSpeed * angularSpeed; + if (body.force.x !== 0 || body.force.y !== 0) { + Sleeping.set(body, false); + continue; + } + var minMotion = Math.min(body.motion, motion), maxMotion = Math.max(body.motion, motion); + body.motion = Sleeping._minBias * minMotion + (1 - Sleeping._minBias) * maxMotion; + if (body.sleepThreshold > 0 && body.motion < motionSleepThreshold) { + body.sleepCounter += 1; + if (body.sleepCounter >= body.sleepThreshold / timeScale) { + Sleeping.set(body, true); + } + } else if (body.sleepCounter > 0) { + body.sleepCounter -= 1; + } + } + }; + Sleeping.afterCollisions = function(pairs) { + var motionSleepThreshold = Sleeping._motionSleepThreshold; + for (var i = 0;i < pairs.length; i++) { + var pair = pairs[i]; + if (!pair.isActive) + continue; + var collision = pair.collision, bodyA = collision.bodyA.parent, bodyB = collision.bodyB.parent; + if (bodyA.isSleeping && bodyB.isSleeping || bodyA.isStatic || bodyB.isStatic) + continue; + if (bodyA.isSleeping || bodyB.isSleeping) { + var sleepingBody = bodyA.isSleeping && !bodyA.isStatic ? bodyA : bodyB, movingBody = sleepingBody === bodyA ? bodyB : bodyA; + if (!sleepingBody.isStatic && movingBody.motion > motionSleepThreshold) { + Sleeping.set(sleepingBody, false); + } + } + } + }; + Sleeping.set = function(body, isSleeping) { + var wasSleeping = body.isSleeping; + if (isSleeping) { + body.isSleeping = true; + body.sleepCounter = body.sleepThreshold; + body.positionImpulse.x = 0; + body.positionImpulse.y = 0; + body.positionPrev.x = body.position.x; + body.positionPrev.y = body.position.y; + body.anglePrev = body.angle; + body.speed = 0; + body.angularSpeed = 0; + body.motion = 0; + if (!wasSleeping) { + Events.trigger(body, "sleepStart"); + } + } else { + body.isSleeping = false; + body.sleepCounter = 0; + if (wasSleeping) { + Events.trigger(body, "sleepEnd"); + } + } + }; + })(); + }, + function(module2, exports2, __webpack_require__) { + var Collision = {}; + module2.exports = Collision; + var Vertices = __webpack_require__(3); + var Pair = __webpack_require__(9); + (function() { + var _supports = []; + var _overlapAB = { + overlap: 0, + axis: null + }; + var _overlapBA = { + overlap: 0, + axis: null + }; + Collision.create = function(bodyA, bodyB) { + return { + pair: null, + collided: false, + bodyA, + bodyB, + parentA: bodyA.parent, + parentB: bodyB.parent, + depth: 0, + normal: { x: 0, y: 0 }, + tangent: { x: 0, y: 0 }, + penetration: { x: 0, y: 0 }, + supports: [null, null], + supportCount: 0 + }; + }; + Collision.collides = function(bodyA, bodyB, pairs) { + Collision._overlapAxes(_overlapAB, bodyA.vertices, bodyB.vertices, bodyA.axes); + if (_overlapAB.overlap <= 0) { + return null; + } + Collision._overlapAxes(_overlapBA, bodyB.vertices, bodyA.vertices, bodyB.axes); + if (_overlapBA.overlap <= 0) { + return null; + } + var pair = pairs && pairs.table[Pair.id(bodyA, bodyB)], collision; + if (!pair) { + collision = Collision.create(bodyA, bodyB); + collision.collided = true; + collision.bodyA = bodyA.id < bodyB.id ? bodyA : bodyB; + collision.bodyB = bodyA.id < bodyB.id ? bodyB : bodyA; + collision.parentA = collision.bodyA.parent; + collision.parentB = collision.bodyB.parent; + } else { + collision = pair.collision; + } + bodyA = collision.bodyA; + bodyB = collision.bodyB; + var minOverlap; + if (_overlapAB.overlap < _overlapBA.overlap) { + minOverlap = _overlapAB; + } else { + minOverlap = _overlapBA; + } + var { normal, tangent, penetration, supports } = collision, depth = minOverlap.overlap, minAxis = minOverlap.axis, normalX = minAxis.x, normalY = minAxis.y, deltaX = bodyB.position.x - bodyA.position.x, deltaY = bodyB.position.y - bodyA.position.y; + if (normalX * deltaX + normalY * deltaY >= 0) { + normalX = -normalX; + normalY = -normalY; + } + normal.x = normalX; + normal.y = normalY; + tangent.x = -normalY; + tangent.y = normalX; + penetration.x = normalX * depth; + penetration.y = normalY * depth; + collision.depth = depth; + var supportsB = Collision._findSupports(bodyA, bodyB, normal, 1), supportCount = 0; + if (Vertices.contains(bodyA.vertices, supportsB[0])) { + supports[supportCount++] = supportsB[0]; + } + if (Vertices.contains(bodyA.vertices, supportsB[1])) { + supports[supportCount++] = supportsB[1]; + } + if (supportCount < 2) { + var supportsA = Collision._findSupports(bodyB, bodyA, normal, -1); + if (Vertices.contains(bodyB.vertices, supportsA[0])) { + supports[supportCount++] = supportsA[0]; + } + if (supportCount < 2 && Vertices.contains(bodyB.vertices, supportsA[1])) { + supports[supportCount++] = supportsA[1]; + } + } + if (supportCount === 0) { + supports[supportCount++] = supportsB[0]; + } + collision.supportCount = supportCount; + return collision; + }; + Collision._overlapAxes = function(result, verticesA, verticesB, axes) { + var verticesALength = verticesA.length, verticesBLength = verticesB.length, verticesAX = verticesA[0].x, verticesAY = verticesA[0].y, verticesBX = verticesB[0].x, verticesBY = verticesB[0].y, axesLength = axes.length, overlapMin = Number.MAX_VALUE, overlapAxisNumber = 0, overlap, overlapAB, overlapBA, dot, i, j; + for (i = 0;i < axesLength; i++) { + var axis = axes[i], axisX = axis.x, axisY = axis.y, minA = verticesAX * axisX + verticesAY * axisY, minB = verticesBX * axisX + verticesBY * axisY, maxA = minA, maxB = minB; + for (j = 1;j < verticesALength; j += 1) { + dot = verticesA[j].x * axisX + verticesA[j].y * axisY; + if (dot > maxA) { + maxA = dot; + } else if (dot < minA) { + minA = dot; + } + } + for (j = 1;j < verticesBLength; j += 1) { + dot = verticesB[j].x * axisX + verticesB[j].y * axisY; + if (dot > maxB) { + maxB = dot; + } else if (dot < minB) { + minB = dot; + } + } + overlapAB = maxA - minB; + overlapBA = maxB - minA; + overlap = overlapAB < overlapBA ? overlapAB : overlapBA; + if (overlap < overlapMin) { + overlapMin = overlap; + overlapAxisNumber = i; + if (overlap <= 0) { + break; + } + } + } + result.axis = axes[overlapAxisNumber]; + result.overlap = overlapMin; + }; + Collision._findSupports = function(bodyA, bodyB, normal, direction) { + var vertices = bodyB.vertices, verticesLength = vertices.length, bodyAPositionX = bodyA.position.x, bodyAPositionY = bodyA.position.y, normalX = normal.x * direction, normalY = normal.y * direction, vertexA = vertices[0], vertexB = vertexA, nearestDistance = normalX * (bodyAPositionX - vertexB.x) + normalY * (bodyAPositionY - vertexB.y), vertexC, distance, j; + for (j = 1;j < verticesLength; j += 1) { + vertexB = vertices[j]; + distance = normalX * (bodyAPositionX - vertexB.x) + normalY * (bodyAPositionY - vertexB.y); + if (distance < nearestDistance) { + nearestDistance = distance; + vertexA = vertexB; + } + } + vertexC = vertices[(verticesLength + vertexA.index - 1) % verticesLength]; + nearestDistance = normalX * (bodyAPositionX - vertexC.x) + normalY * (bodyAPositionY - vertexC.y); + vertexB = vertices[(vertexA.index + 1) % verticesLength]; + if (normalX * (bodyAPositionX - vertexB.x) + normalY * (bodyAPositionY - vertexB.y) < nearestDistance) { + _supports[0] = vertexA; + _supports[1] = vertexB; + return _supports; + } + _supports[0] = vertexA; + _supports[1] = vertexC; + return _supports; + }; + })(); + }, + function(module2, exports2, __webpack_require__) { + var Pair = {}; + module2.exports = Pair; + var Contact = __webpack_require__(16); + (function() { + Pair.create = function(collision, timestamp) { + var { bodyA, bodyB } = collision; + var pair = { + id: Pair.id(bodyA, bodyB), + bodyA, + bodyB, + collision, + contacts: [Contact.create(), Contact.create()], + contactCount: 0, + separation: 0, + isActive: true, + isSensor: bodyA.isSensor || bodyB.isSensor, + timeCreated: timestamp, + timeUpdated: timestamp, + inverseMass: 0, + friction: 0, + frictionStatic: 0, + restitution: 0, + slop: 0 + }; + Pair.update(pair, collision, timestamp); + return pair; + }; + Pair.update = function(pair, collision, timestamp) { + var { supports, supportCount } = collision, contacts = pair.contacts, parentA = collision.parentA, parentB = collision.parentB; + pair.isActive = true; + pair.timeUpdated = timestamp; + pair.collision = collision; + pair.separation = collision.depth; + pair.inverseMass = parentA.inverseMass + parentB.inverseMass; + pair.friction = parentA.friction < parentB.friction ? parentA.friction : parentB.friction; + pair.frictionStatic = parentA.frictionStatic > parentB.frictionStatic ? parentA.frictionStatic : parentB.frictionStatic; + pair.restitution = parentA.restitution > parentB.restitution ? parentA.restitution : parentB.restitution; + pair.slop = parentA.slop > parentB.slop ? parentA.slop : parentB.slop; + pair.contactCount = supportCount; + collision.pair = pair; + var supportA = supports[0], contactA = contacts[0], supportB = supports[1], contactB = contacts[1]; + if (contactB.vertex === supportA || contactA.vertex === supportB) { + contacts[1] = contactA; + contacts[0] = contactA = contactB; + contactB = contacts[1]; + } + contactA.vertex = supportA; + contactB.vertex = supportB; + }; + Pair.setActive = function(pair, isActive, timestamp) { + if (isActive) { + pair.isActive = true; + pair.timeUpdated = timestamp; + } else { + pair.isActive = false; + pair.contactCount = 0; + } + }; + Pair.id = function(bodyA, bodyB) { + return bodyA.id < bodyB.id ? bodyA.id.toString(36) + ":" + bodyB.id.toString(36) : bodyB.id.toString(36) + ":" + bodyA.id.toString(36); + }; + })(); + }, + function(module2, exports2, __webpack_require__) { + var Constraint = {}; + module2.exports = Constraint; + var Vertices = __webpack_require__(3); + var Vector2 = __webpack_require__(2); + var Sleeping = __webpack_require__(7); + var Bounds = __webpack_require__(1); + var Axes = __webpack_require__(11); + var Common = __webpack_require__(0); + (function() { + Constraint._warming = 0.4; + Constraint._torqueDampen = 1; + Constraint._minLength = 0.000001; + Constraint.create = function(options) { + var constraint = options; + if (constraint.bodyA && !constraint.pointA) + constraint.pointA = { x: 0, y: 0 }; + if (constraint.bodyB && !constraint.pointB) + constraint.pointB = { x: 0, y: 0 }; + var initialPointA = constraint.bodyA ? Vector2.add(constraint.bodyA.position, constraint.pointA) : constraint.pointA, initialPointB = constraint.bodyB ? Vector2.add(constraint.bodyB.position, constraint.pointB) : constraint.pointB, length = Vector2.magnitude(Vector2.sub(initialPointA, initialPointB)); + constraint.length = typeof constraint.length !== "undefined" ? constraint.length : length; + constraint.id = constraint.id || Common.nextId(); + constraint.label = constraint.label || "Constraint"; + constraint.type = "constraint"; + constraint.stiffness = constraint.stiffness || (constraint.length > 0 ? 1 : 0.7); + constraint.damping = constraint.damping || 0; + constraint.angularStiffness = constraint.angularStiffness || 0; + constraint.angleA = constraint.bodyA ? constraint.bodyA.angle : constraint.angleA; + constraint.angleB = constraint.bodyB ? constraint.bodyB.angle : constraint.angleB; + constraint.plugin = {}; + var render = { + visible: true, + lineWidth: 2, + strokeStyle: "#ffffff", + type: "line", + anchors: true + }; + if (constraint.length === 0 && constraint.stiffness > 0.1) { + render.type = "pin"; + render.anchors = false; + } else if (constraint.stiffness < 0.9) { + render.type = "spring"; + } + constraint.render = Common.extend(render, constraint.render); + return constraint; + }; + Constraint.preSolveAll = function(bodies) { + for (var i = 0;i < bodies.length; i += 1) { + var body = bodies[i], impulse = body.constraintImpulse; + if (body.isStatic || impulse.x === 0 && impulse.y === 0 && impulse.angle === 0) { + continue; + } + body.position.x += impulse.x; + body.position.y += impulse.y; + body.angle += impulse.angle; + } + }; + Constraint.solveAll = function(constraints, delta) { + var timeScale = Common.clamp(delta / Common._baseDelta, 0, 1); + for (var i = 0;i < constraints.length; i += 1) { + var constraint = constraints[i], fixedA = !constraint.bodyA || constraint.bodyA && constraint.bodyA.isStatic, fixedB = !constraint.bodyB || constraint.bodyB && constraint.bodyB.isStatic; + if (fixedA || fixedB) { + Constraint.solve(constraints[i], timeScale); + } + } + for (i = 0;i < constraints.length; i += 1) { + constraint = constraints[i]; + fixedA = !constraint.bodyA || constraint.bodyA && constraint.bodyA.isStatic; + fixedB = !constraint.bodyB || constraint.bodyB && constraint.bodyB.isStatic; + if (!fixedA && !fixedB) { + Constraint.solve(constraints[i], timeScale); + } + } + }; + Constraint.solve = function(constraint, timeScale) { + var { bodyA, bodyB, pointA, pointB } = constraint; + if (!bodyA && !bodyB) + return; + if (bodyA && !bodyA.isStatic) { + Vector2.rotate(pointA, bodyA.angle - constraint.angleA, pointA); + constraint.angleA = bodyA.angle; + } + if (bodyB && !bodyB.isStatic) { + Vector2.rotate(pointB, bodyB.angle - constraint.angleB, pointB); + constraint.angleB = bodyB.angle; + } + var pointAWorld = pointA, pointBWorld = pointB; + if (bodyA) + pointAWorld = Vector2.add(bodyA.position, pointA); + if (bodyB) + pointBWorld = Vector2.add(bodyB.position, pointB); + if (!pointAWorld || !pointBWorld) + return; + var delta = Vector2.sub(pointAWorld, pointBWorld), currentLength = Vector2.magnitude(delta); + if (currentLength < Constraint._minLength) { + currentLength = Constraint._minLength; + } + var difference = (currentLength - constraint.length) / currentLength, isRigid = constraint.stiffness >= 1 || constraint.length === 0, stiffness = isRigid ? constraint.stiffness * timeScale : constraint.stiffness * timeScale * timeScale, damping = constraint.damping * timeScale, force = Vector2.mult(delta, difference * stiffness), massTotal = (bodyA ? bodyA.inverseMass : 0) + (bodyB ? bodyB.inverseMass : 0), inertiaTotal = (bodyA ? bodyA.inverseInertia : 0) + (bodyB ? bodyB.inverseInertia : 0), resistanceTotal = massTotal + inertiaTotal, torque, share, normal, normalVelocity, relativeVelocity; + if (damping > 0) { + var zero = Vector2.create(); + normal = Vector2.div(delta, currentLength); + relativeVelocity = Vector2.sub(bodyB && Vector2.sub(bodyB.position, bodyB.positionPrev) || zero, bodyA && Vector2.sub(bodyA.position, bodyA.positionPrev) || zero); + normalVelocity = Vector2.dot(normal, relativeVelocity); + } + if (bodyA && !bodyA.isStatic) { + share = bodyA.inverseMass / massTotal; + bodyA.constraintImpulse.x -= force.x * share; + bodyA.constraintImpulse.y -= force.y * share; + bodyA.position.x -= force.x * share; + bodyA.position.y -= force.y * share; + if (damping > 0) { + bodyA.positionPrev.x -= damping * normal.x * normalVelocity * share; + bodyA.positionPrev.y -= damping * normal.y * normalVelocity * share; + } + torque = Vector2.cross(pointA, force) / resistanceTotal * Constraint._torqueDampen * bodyA.inverseInertia * (1 - constraint.angularStiffness); + bodyA.constraintImpulse.angle -= torque; + bodyA.angle -= torque; + } + if (bodyB && !bodyB.isStatic) { + share = bodyB.inverseMass / massTotal; + bodyB.constraintImpulse.x += force.x * share; + bodyB.constraintImpulse.y += force.y * share; + bodyB.position.x += force.x * share; + bodyB.position.y += force.y * share; + if (damping > 0) { + bodyB.positionPrev.x += damping * normal.x * normalVelocity * share; + bodyB.positionPrev.y += damping * normal.y * normalVelocity * share; + } + torque = Vector2.cross(pointB, force) / resistanceTotal * Constraint._torqueDampen * bodyB.inverseInertia * (1 - constraint.angularStiffness); + bodyB.constraintImpulse.angle += torque; + bodyB.angle += torque; + } + }; + Constraint.postSolveAll = function(bodies) { + for (var i = 0;i < bodies.length; i++) { + var body = bodies[i], impulse = body.constraintImpulse; + if (body.isStatic || impulse.x === 0 && impulse.y === 0 && impulse.angle === 0) { + continue; + } + Sleeping.set(body, false); + for (var j = 0;j < body.parts.length; j++) { + var part = body.parts[j]; + Vertices.translate(part.vertices, impulse); + if (j > 0) { + part.position.x += impulse.x; + part.position.y += impulse.y; + } + if (impulse.angle !== 0) { + Vertices.rotate(part.vertices, impulse.angle, body.position); + Axes.rotate(part.axes, impulse.angle); + if (j > 0) { + Vector2.rotateAbout(part.position, impulse.angle, body.position, part.position); + } + } + Bounds.update(part.bounds, part.vertices, body.velocity); + } + impulse.angle *= Constraint._warming; + impulse.x *= Constraint._warming; + impulse.y *= Constraint._warming; + } + }; + Constraint.pointAWorld = function(constraint) { + return { + x: (constraint.bodyA ? constraint.bodyA.position.x : 0) + (constraint.pointA ? constraint.pointA.x : 0), + y: (constraint.bodyA ? constraint.bodyA.position.y : 0) + (constraint.pointA ? constraint.pointA.y : 0) + }; + }; + Constraint.pointBWorld = function(constraint) { + return { + x: (constraint.bodyB ? constraint.bodyB.position.x : 0) + (constraint.pointB ? constraint.pointB.x : 0), + y: (constraint.bodyB ? constraint.bodyB.position.y : 0) + (constraint.pointB ? constraint.pointB.y : 0) + }; + }; + Constraint.currentLength = function(constraint) { + var pointAX = (constraint.bodyA ? constraint.bodyA.position.x : 0) + (constraint.pointA ? constraint.pointA.x : 0); + var pointAY = (constraint.bodyA ? constraint.bodyA.position.y : 0) + (constraint.pointA ? constraint.pointA.y : 0); + var pointBX = (constraint.bodyB ? constraint.bodyB.position.x : 0) + (constraint.pointB ? constraint.pointB.x : 0); + var pointBY = (constraint.bodyB ? constraint.bodyB.position.y : 0) + (constraint.pointB ? constraint.pointB.y : 0); + var deltaX = pointAX - pointBX; + var deltaY = pointAY - pointBY; + return Math.sqrt(deltaX * deltaX + deltaY * deltaY); + }; + })(); + }, + function(module2, exports2, __webpack_require__) { + var Axes = {}; + module2.exports = Axes; + var Vector2 = __webpack_require__(2); + var Common = __webpack_require__(0); + (function() { + Axes.fromVertices = function(vertices) { + var axes = {}; + for (var i = 0;i < vertices.length; i++) { + var j = (i + 1) % vertices.length, normal = Vector2.normalise({ + x: vertices[j].y - vertices[i].y, + y: vertices[i].x - vertices[j].x + }), gradient = normal.y === 0 ? Infinity : normal.x / normal.y; + gradient = gradient.toFixed(3).toString(); + axes[gradient] = normal; + } + return Common.values(axes); + }; + Axes.rotate = function(axes, angle) { + if (angle === 0) + return; + var cos = Math.cos(angle), sin = Math.sin(angle); + for (var i = 0;i < axes.length; i++) { + var axis = axes[i], xx; + xx = axis.x * cos - axis.y * sin; + axis.y = axis.x * sin + axis.y * cos; + axis.x = xx; + } + }; + })(); + }, + function(module2, exports2, __webpack_require__) { + var Bodies = {}; + module2.exports = Bodies; + var Vertices = __webpack_require__(3); + var Common = __webpack_require__(0); + var Body = __webpack_require__(4); + var Bounds = __webpack_require__(1); + var Vector2 = __webpack_require__(2); + (function() { + Bodies.rectangle = function(x, y, width, height, options) { + options = options || {}; + var rectangle = { + label: "Rectangle Body", + position: { x, y }, + vertices: Vertices.fromPath("L 0 0 L " + width + " 0 L " + width + " " + height + " L 0 " + height) + }; + if (options.chamfer) { + var chamfer = options.chamfer; + rectangle.vertices = Vertices.chamfer(rectangle.vertices, chamfer.radius, chamfer.quality, chamfer.qualityMin, chamfer.qualityMax); + delete options.chamfer; + } + return Body.create(Common.extend({}, rectangle, options)); + }; + Bodies.trapezoid = function(x, y, width, height, slope, options) { + options = options || {}; + if (slope >= 1) { + Common.warn("Bodies.trapezoid: slope parameter must be < 1."); + } + slope *= 0.5; + var roof = (1 - slope * 2) * width; + var x1 = width * slope, x2 = x1 + roof, x3 = x2 + x1, verticesPath; + if (slope < 0.5) { + verticesPath = "L 0 0 L " + x1 + " " + -height + " L " + x2 + " " + -height + " L " + x3 + " 0"; + } else { + verticesPath = "L 0 0 L " + x2 + " " + -height + " L " + x3 + " 0"; + } + var trapezoid = { + label: "Trapezoid Body", + position: { x, y }, + vertices: Vertices.fromPath(verticesPath) + }; + if (options.chamfer) { + var chamfer = options.chamfer; + trapezoid.vertices = Vertices.chamfer(trapezoid.vertices, chamfer.radius, chamfer.quality, chamfer.qualityMin, chamfer.qualityMax); + delete options.chamfer; + } + return Body.create(Common.extend({}, trapezoid, options)); + }; + Bodies.circle = function(x, y, radius, options, maxSides) { + options = options || {}; + var circle = { + label: "Circle Body", + circleRadius: radius + }; + maxSides = maxSides || 25; + var sides = Math.ceil(Math.max(10, Math.min(maxSides, radius))); + if (sides % 2 === 1) + sides += 1; + return Bodies.polygon(x, y, sides, radius, Common.extend({}, circle, options)); + }; + Bodies.polygon = function(x, y, sides, radius, options) { + options = options || {}; + if (sides < 3) + return Bodies.circle(x, y, radius, options); + var theta = 2 * Math.PI / sides, path = "", offset = theta * 0.5; + for (var i = 0;i < sides; i += 1) { + var angle = offset + i * theta, xx = Math.cos(angle) * radius, yy = Math.sin(angle) * radius; + path += "L " + xx.toFixed(3) + " " + yy.toFixed(3) + " "; + } + var polygon = { + label: "Polygon Body", + position: { x, y }, + vertices: Vertices.fromPath(path) + }; + if (options.chamfer) { + var chamfer = options.chamfer; + polygon.vertices = Vertices.chamfer(polygon.vertices, chamfer.radius, chamfer.quality, chamfer.qualityMin, chamfer.qualityMax); + delete options.chamfer; + } + return Body.create(Common.extend({}, polygon, options)); + }; + Bodies.fromVertices = function(x, y, vertexSets, options, flagInternal, removeCollinear, minimumArea, removeDuplicatePoints) { + var decomp = Common.getDecomp(), canDecomp, body, parts, isConvex, isConcave, vertices, i, j, k, v, z; + canDecomp = Boolean(decomp && decomp.quickDecomp); + options = options || {}; + parts = []; + flagInternal = typeof flagInternal !== "undefined" ? flagInternal : false; + removeCollinear = typeof removeCollinear !== "undefined" ? removeCollinear : 0.01; + minimumArea = typeof minimumArea !== "undefined" ? minimumArea : 10; + removeDuplicatePoints = typeof removeDuplicatePoints !== "undefined" ? removeDuplicatePoints : 0.01; + if (!Common.isArray(vertexSets[0])) { + vertexSets = [vertexSets]; + } + for (v = 0;v < vertexSets.length; v += 1) { + vertices = vertexSets[v]; + isConvex = Vertices.isConvex(vertices); + isConcave = !isConvex; + if (isConcave && !canDecomp) { + Common.warnOnce("Bodies.fromVertices: Install the 'poly-decomp' library and use Common.setDecomp or provide 'decomp' as a global to decompose concave vertices."); + } + if (isConvex || !canDecomp) { + if (isConvex) { + vertices = Vertices.clockwiseSort(vertices); + } else { + vertices = Vertices.hull(vertices); + } + parts.push({ + position: { x, y }, + vertices + }); + } else { + var concave = vertices.map(function(vertex) { + return [vertex.x, vertex.y]; + }); + decomp.makeCCW(concave); + if (removeCollinear !== false) + decomp.removeCollinearPoints(concave, removeCollinear); + if (removeDuplicatePoints !== false && decomp.removeDuplicatePoints) + decomp.removeDuplicatePoints(concave, removeDuplicatePoints); + var decomposed = decomp.quickDecomp(concave); + for (i = 0;i < decomposed.length; i++) { + var chunk = decomposed[i]; + var chunkVertices = chunk.map(function(vertices2) { + return { + x: vertices2[0], + y: vertices2[1] + }; + }); + if (minimumArea > 0 && Vertices.area(chunkVertices) < minimumArea) + continue; + parts.push({ + position: Vertices.centre(chunkVertices), + vertices: chunkVertices + }); + } + } + } + for (i = 0;i < parts.length; i++) { + parts[i] = Body.create(Common.extend(parts[i], options)); + } + if (flagInternal) { + var coincident_max_dist = 5; + for (i = 0;i < parts.length; i++) { + var partA = parts[i]; + for (j = i + 1;j < parts.length; j++) { + var partB = parts[j]; + if (Bounds.overlaps(partA.bounds, partB.bounds)) { + var pav = partA.vertices, pbv = partB.vertices; + for (k = 0;k < partA.vertices.length; k++) { + for (z = 0;z < partB.vertices.length; z++) { + var da = Vector2.magnitudeSquared(Vector2.sub(pav[(k + 1) % pav.length], pbv[z])), db = Vector2.magnitudeSquared(Vector2.sub(pav[k], pbv[(z + 1) % pbv.length])); + if (da < coincident_max_dist && db < coincident_max_dist) { + pav[k].isInternal = true; + pbv[z].isInternal = true; + } + } + } + } + } + } + } + if (parts.length > 1) { + body = Body.create(Common.extend({ parts: parts.slice(0) }, options)); + Body.setPosition(body, { x, y }); + return body; + } else { + return parts[0]; + } + }; + })(); + }, + function(module2, exports2, __webpack_require__) { + var Detector = {}; + module2.exports = Detector; + var Common = __webpack_require__(0); + var Collision = __webpack_require__(8); + (function() { + Detector.create = function(options) { + var defaults = { + bodies: [], + collisions: [], + pairs: null + }; + return Common.extend(defaults, options); + }; + Detector.setBodies = function(detector, bodies) { + detector.bodies = bodies.slice(0); + }; + Detector.clear = function(detector) { + detector.bodies = []; + detector.collisions = []; + }; + Detector.collisions = function(detector) { + var { pairs, bodies } = detector, bodiesLength = bodies.length, canCollide = Detector.canCollide, collides = Collision.collides, collisions = detector.collisions, collisionIndex = 0, i, j; + bodies.sort(Detector._compareBoundsX); + for (i = 0;i < bodiesLength; i++) { + var bodyA = bodies[i], boundsA = bodyA.bounds, boundXMax = bodyA.bounds.max.x, boundYMax = bodyA.bounds.max.y, boundYMin = bodyA.bounds.min.y, bodyAStatic = bodyA.isStatic || bodyA.isSleeping, partsALength = bodyA.parts.length, partsASingle = partsALength === 1; + for (j = i + 1;j < bodiesLength; j++) { + var bodyB = bodies[j], boundsB = bodyB.bounds; + if (boundsB.min.x > boundXMax) { + break; + } + if (boundYMax < boundsB.min.y || boundYMin > boundsB.max.y) { + continue; + } + if (bodyAStatic && (bodyB.isStatic || bodyB.isSleeping)) { + continue; + } + if (!canCollide(bodyA.collisionFilter, bodyB.collisionFilter)) { + continue; + } + var partsBLength = bodyB.parts.length; + if (partsASingle && partsBLength === 1) { + var collision = collides(bodyA, bodyB, pairs); + if (collision) { + collisions[collisionIndex++] = collision; + } + } else { + var partsAStart = partsALength > 1 ? 1 : 0, partsBStart = partsBLength > 1 ? 1 : 0; + for (var k = partsAStart;k < partsALength; k++) { + var partA = bodyA.parts[k], boundsA = partA.bounds; + for (var z = partsBStart;z < partsBLength; z++) { + var partB = bodyB.parts[z], boundsB = partB.bounds; + if (boundsA.min.x > boundsB.max.x || boundsA.max.x < boundsB.min.x || boundsA.max.y < boundsB.min.y || boundsA.min.y > boundsB.max.y) { + continue; + } + var collision = collides(partA, partB, pairs); + if (collision) { + collisions[collisionIndex++] = collision; + } + } + } + } + } + } + if (collisions.length !== collisionIndex) { + collisions.length = collisionIndex; + } + return collisions; + }; + Detector.canCollide = function(filterA, filterB) { + if (filterA.group === filterB.group && filterA.group !== 0) + return filterA.group > 0; + return (filterA.mask & filterB.category) !== 0 && (filterB.mask & filterA.category) !== 0; + }; + Detector._compareBoundsX = function(bodyA, bodyB) { + return bodyA.bounds.min.x - bodyB.bounds.min.x; + }; + })(); + }, + function(module2, exports2, __webpack_require__) { + var Mouse = {}; + module2.exports = Mouse; + var Common = __webpack_require__(0); + (function() { + Mouse.create = function(element) { + var mouse = {}; + if (!element) { + Common.log("Mouse.create: element was undefined, defaulting to document.body", "warn"); + } + mouse.element = element || document.body; + mouse.absolute = { x: 0, y: 0 }; + mouse.position = { x: 0, y: 0 }; + mouse.mousedownPosition = { x: 0, y: 0 }; + mouse.mouseupPosition = { x: 0, y: 0 }; + mouse.offset = { x: 0, y: 0 }; + mouse.scale = { x: 1, y: 1 }; + mouse.wheelDelta = 0; + mouse.button = -1; + mouse.pixelRatio = parseInt(mouse.element.getAttribute("data-pixel-ratio"), 10) || 1; + mouse.sourceEvents = { + mousemove: null, + mousedown: null, + mouseup: null, + mousewheel: null + }; + mouse.mousemove = function(event) { + var position = Mouse._getRelativeMousePosition(event, mouse.element, mouse.pixelRatio), touches = event.changedTouches; + if (touches) { + mouse.button = 0; + event.preventDefault(); + } + mouse.absolute.x = position.x; + mouse.absolute.y = position.y; + mouse.position.x = mouse.absolute.x * mouse.scale.x + mouse.offset.x; + mouse.position.y = mouse.absolute.y * mouse.scale.y + mouse.offset.y; + mouse.sourceEvents.mousemove = event; + }; + mouse.mousedown = function(event) { + var position = Mouse._getRelativeMousePosition(event, mouse.element, mouse.pixelRatio), touches = event.changedTouches; + if (touches) { + mouse.button = 0; + event.preventDefault(); + } else { + mouse.button = event.button; + } + mouse.absolute.x = position.x; + mouse.absolute.y = position.y; + mouse.position.x = mouse.absolute.x * mouse.scale.x + mouse.offset.x; + mouse.position.y = mouse.absolute.y * mouse.scale.y + mouse.offset.y; + mouse.mousedownPosition.x = mouse.position.x; + mouse.mousedownPosition.y = mouse.position.y; + mouse.sourceEvents.mousedown = event; + }; + mouse.mouseup = function(event) { + var position = Mouse._getRelativeMousePosition(event, mouse.element, mouse.pixelRatio), touches = event.changedTouches; + if (touches) { + event.preventDefault(); + } + mouse.button = -1; + mouse.absolute.x = position.x; + mouse.absolute.y = position.y; + mouse.position.x = mouse.absolute.x * mouse.scale.x + mouse.offset.x; + mouse.position.y = mouse.absolute.y * mouse.scale.y + mouse.offset.y; + mouse.mouseupPosition.x = mouse.position.x; + mouse.mouseupPosition.y = mouse.position.y; + mouse.sourceEvents.mouseup = event; + }; + mouse.mousewheel = function(event) { + mouse.wheelDelta = Math.max(-1, Math.min(1, event.wheelDelta || -event.detail)); + event.preventDefault(); + mouse.sourceEvents.mousewheel = event; + }; + Mouse.setElement(mouse, mouse.element); + return mouse; + }; + Mouse.setElement = function(mouse, element) { + mouse.element = element; + element.addEventListener("mousemove", mouse.mousemove, { passive: true }); + element.addEventListener("mousedown", mouse.mousedown, { passive: true }); + element.addEventListener("mouseup", mouse.mouseup, { passive: true }); + element.addEventListener("wheel", mouse.mousewheel, { passive: false }); + element.addEventListener("touchmove", mouse.mousemove, { passive: false }); + element.addEventListener("touchstart", mouse.mousedown, { passive: false }); + element.addEventListener("touchend", mouse.mouseup, { passive: false }); + }; + Mouse.clearSourceEvents = function(mouse) { + mouse.sourceEvents.mousemove = null; + mouse.sourceEvents.mousedown = null; + mouse.sourceEvents.mouseup = null; + mouse.sourceEvents.mousewheel = null; + mouse.wheelDelta = 0; + }; + Mouse.setOffset = function(mouse, offset) { + mouse.offset.x = offset.x; + mouse.offset.y = offset.y; + mouse.position.x = mouse.absolute.x * mouse.scale.x + mouse.offset.x; + mouse.position.y = mouse.absolute.y * mouse.scale.y + mouse.offset.y; + }; + Mouse.setScale = function(mouse, scale) { + mouse.scale.x = scale.x; + mouse.scale.y = scale.y; + mouse.position.x = mouse.absolute.x * mouse.scale.x + mouse.offset.x; + mouse.position.y = mouse.absolute.y * mouse.scale.y + mouse.offset.y; + }; + Mouse._getRelativeMousePosition = function(event, element, pixelRatio) { + var elementBounds = element.getBoundingClientRect(), rootNode = document.documentElement || document.body.parentNode || document.body, scrollX = window.pageXOffset !== undefined ? window.pageXOffset : rootNode.scrollLeft, scrollY = window.pageYOffset !== undefined ? window.pageYOffset : rootNode.scrollTop, touches = event.changedTouches, x, y; + if (touches) { + x = touches[0].pageX - elementBounds.left - scrollX; + y = touches[0].pageY - elementBounds.top - scrollY; + } else { + x = event.pageX - elementBounds.left - scrollX; + y = event.pageY - elementBounds.top - scrollY; + } + return { + x: x / (element.clientWidth / (element.width || element.clientWidth) * pixelRatio), + y: y / (element.clientHeight / (element.height || element.clientHeight) * pixelRatio) + }; + }; + })(); + }, + function(module2, exports2, __webpack_require__) { + var Plugin = {}; + module2.exports = Plugin; + var Common = __webpack_require__(0); + (function() { + Plugin._registry = {}; + Plugin.register = function(plugin) { + if (!Plugin.isPlugin(plugin)) { + Common.warn("Plugin.register:", Plugin.toString(plugin), "does not implement all required fields."); + } + if (plugin.name in Plugin._registry) { + var registered = Plugin._registry[plugin.name], pluginVersion = Plugin.versionParse(plugin.version).number, registeredVersion = Plugin.versionParse(registered.version).number; + if (pluginVersion > registeredVersion) { + Common.warn("Plugin.register:", Plugin.toString(registered), "was upgraded to", Plugin.toString(plugin)); + Plugin._registry[plugin.name] = plugin; + } else if (pluginVersion < registeredVersion) { + Common.warn("Plugin.register:", Plugin.toString(registered), "can not be downgraded to", Plugin.toString(plugin)); + } else if (plugin !== registered) { + Common.warn("Plugin.register:", Plugin.toString(plugin), "is already registered to different plugin object"); + } + } else { + Plugin._registry[plugin.name] = plugin; + } + return plugin; + }; + Plugin.resolve = function(dependency) { + return Plugin._registry[Plugin.dependencyParse(dependency).name]; + }; + Plugin.toString = function(plugin) { + return typeof plugin === "string" ? plugin : (plugin.name || "anonymous") + "@" + (plugin.version || plugin.range || "0.0.0"); + }; + Plugin.isPlugin = function(obj) { + return obj && obj.name && obj.version && obj.install; + }; + Plugin.isUsed = function(module3, name) { + return module3.used.indexOf(name) > -1; + }; + Plugin.isFor = function(plugin, module3) { + var parsed = plugin.for && Plugin.dependencyParse(plugin.for); + return !plugin.for || module3.name === parsed.name && Plugin.versionSatisfies(module3.version, parsed.range); + }; + Plugin.use = function(module3, plugins) { + module3.uses = (module3.uses || []).concat(plugins || []); + if (module3.uses.length === 0) { + Common.warn("Plugin.use:", Plugin.toString(module3), "does not specify any dependencies to install."); + return; + } + var dependencies = Plugin.dependencies(module3), sortedDependencies = Common.topologicalSort(dependencies), status = []; + for (var i = 0;i < sortedDependencies.length; i += 1) { + if (sortedDependencies[i] === module3.name) { + continue; + } + var plugin = Plugin.resolve(sortedDependencies[i]); + if (!plugin) { + status.push("❌ " + sortedDependencies[i]); + continue; + } + if (Plugin.isUsed(module3, plugin.name)) { + continue; + } + if (!Plugin.isFor(plugin, module3)) { + Common.warn("Plugin.use:", Plugin.toString(plugin), "is for", plugin.for, "but installed on", Plugin.toString(module3) + "."); + plugin._warned = true; + } + if (plugin.install) { + plugin.install(module3); + } else { + Common.warn("Plugin.use:", Plugin.toString(plugin), "does not specify an install function."); + plugin._warned = true; + } + if (plugin._warned) { + status.push("\uD83D\uDD36 " + Plugin.toString(plugin)); + delete plugin._warned; + } else { + status.push("✅ " + Plugin.toString(plugin)); + } + module3.used.push(plugin.name); + } + if (status.length > 0) { + Common.info(status.join(" ")); + } + }; + Plugin.dependencies = function(module3, tracked) { + var parsedBase = Plugin.dependencyParse(module3), name = parsedBase.name; + tracked = tracked || {}; + if (name in tracked) { + return; + } + module3 = Plugin.resolve(module3) || module3; + tracked[name] = Common.map(module3.uses || [], function(dependency) { + if (Plugin.isPlugin(dependency)) { + Plugin.register(dependency); + } + var parsed = Plugin.dependencyParse(dependency), resolved = Plugin.resolve(dependency); + if (resolved && !Plugin.versionSatisfies(resolved.version, parsed.range)) { + Common.warn("Plugin.dependencies:", Plugin.toString(resolved), "does not satisfy", Plugin.toString(parsed), "used by", Plugin.toString(parsedBase) + "."); + resolved._warned = true; + module3._warned = true; + } else if (!resolved) { + Common.warn("Plugin.dependencies:", Plugin.toString(dependency), "used by", Plugin.toString(parsedBase), "could not be resolved."); + module3._warned = true; + } + return parsed.name; + }); + for (var i = 0;i < tracked[name].length; i += 1) { + Plugin.dependencies(tracked[name][i], tracked); + } + return tracked; + }; + Plugin.dependencyParse = function(dependency) { + if (Common.isString(dependency)) { + var pattern = /^[\w-]+(@(\*|[\^~]?\d+\.\d+\.\d+(-[0-9A-Za-z-+]+)?))?$/; + if (!pattern.test(dependency)) { + Common.warn("Plugin.dependencyParse:", dependency, "is not a valid dependency string."); + } + return { + name: dependency.split("@")[0], + range: dependency.split("@")[1] || "*" + }; + } + return { + name: dependency.name, + range: dependency.range || dependency.version + }; + }; + Plugin.versionParse = function(range) { + var pattern = /^(\*)|(\^|~|>=|>)?\s*((\d+)\.(\d+)\.(\d+))(-[0-9A-Za-z-+]+)?$/; + if (!pattern.test(range)) { + Common.warn("Plugin.versionParse:", range, "is not a valid version or range."); + } + var parts = pattern.exec(range); + var major = Number(parts[4]); + var minor = Number(parts[5]); + var patch = Number(parts[6]); + return { + isRange: Boolean(parts[1] || parts[2]), + version: parts[3], + range, + operator: parts[1] || parts[2] || "", + major, + minor, + patch, + parts: [major, minor, patch], + prerelease: parts[7], + number: major * 1e8 + minor * 1e4 + patch + }; + }; + Plugin.versionSatisfies = function(version, range) { + range = range || "*"; + var r = Plugin.versionParse(range), v = Plugin.versionParse(version); + if (r.isRange) { + if (r.operator === "*" || version === "*") { + return true; + } + if (r.operator === ">") { + return v.number > r.number; + } + if (r.operator === ">=") { + return v.number >= r.number; + } + if (r.operator === "~") { + return v.major === r.major && v.minor === r.minor && v.patch >= r.patch; + } + if (r.operator === "^") { + if (r.major > 0) { + return v.major === r.major && v.number >= r.number; + } + if (r.minor > 0) { + return v.minor === r.minor && v.patch >= r.patch; + } + return v.patch === r.patch; + } + } + return version === range || version === "*"; + }; + })(); + }, + function(module2, exports2) { + var Contact = {}; + module2.exports = Contact; + (function() { + Contact.create = function(vertex) { + return { + vertex, + normalImpulse: 0, + tangentImpulse: 0 + }; + }; + })(); + }, + function(module2, exports2, __webpack_require__) { + var Engine = {}; + module2.exports = Engine; + var Sleeping = __webpack_require__(7); + var Resolver = __webpack_require__(18); + var Detector = __webpack_require__(13); + var Pairs = __webpack_require__(19); + var Events = __webpack_require__(5); + var Composite = __webpack_require__(6); + var Constraint = __webpack_require__(10); + var Common = __webpack_require__(0); + var Body = __webpack_require__(4); + (function() { + Engine._deltaMax = 1000 / 60; + Engine.create = function(options) { + options = options || {}; + var defaults = { + positionIterations: 6, + velocityIterations: 4, + constraintIterations: 2, + enableSleeping: false, + events: [], + plugin: {}, + gravity: { + x: 0, + y: 1, + scale: 0.001 + }, + timing: { + timestamp: 0, + timeScale: 1, + lastDelta: 0, + lastElapsed: 0, + lastUpdatesPerFrame: 0 + } + }; + var engine = Common.extend(defaults, options); + engine.world = options.world || Composite.create({ label: "World" }); + engine.pairs = options.pairs || Pairs.create(); + engine.detector = options.detector || Detector.create(); + engine.detector.pairs = engine.pairs; + engine.grid = { buckets: [] }; + engine.world.gravity = engine.gravity; + engine.broadphase = engine.grid; + engine.metrics = {}; + return engine; + }; + Engine.update = function(engine, delta) { + var startTime = Common.now(); + var { world, detector, pairs, timing } = engine, timestamp = timing.timestamp, i; + if (delta > Engine._deltaMax) { + Common.warnOnce("Matter.Engine.update: delta argument is recommended to be less than or equal to", Engine._deltaMax.toFixed(3), "ms."); + } + delta = typeof delta !== "undefined" ? delta : Common._baseDelta; + delta *= timing.timeScale; + timing.timestamp += delta; + timing.lastDelta = delta; + var event = { + timestamp: timing.timestamp, + delta + }; + Events.trigger(engine, "beforeUpdate", event); + var allBodies = Composite.allBodies(world), allConstraints = Composite.allConstraints(world); + if (world.isModified) { + Detector.setBodies(detector, allBodies); + Composite.setModified(world, false, false, true); + } + if (engine.enableSleeping) + Sleeping.update(allBodies, delta); + Engine._bodiesApplyGravity(allBodies, engine.gravity); + if (delta > 0) { + Engine._bodiesUpdate(allBodies, delta); + } + Events.trigger(engine, "beforeSolve", event); + Constraint.preSolveAll(allBodies); + for (i = 0;i < engine.constraintIterations; i++) { + Constraint.solveAll(allConstraints, delta); + } + Constraint.postSolveAll(allBodies); + var collisions = Detector.collisions(detector); + Pairs.update(pairs, collisions, timestamp); + if (engine.enableSleeping) + Sleeping.afterCollisions(pairs.list); + if (pairs.collisionStart.length > 0) { + Events.trigger(engine, "collisionStart", { + pairs: pairs.collisionStart, + timestamp: timing.timestamp, + delta + }); + } + var positionDamping = Common.clamp(20 / engine.positionIterations, 0, 1); + Resolver.preSolvePosition(pairs.list); + for (i = 0;i < engine.positionIterations; i++) { + Resolver.solvePosition(pairs.list, delta, positionDamping); + } + Resolver.postSolvePosition(allBodies); + Constraint.preSolveAll(allBodies); + for (i = 0;i < engine.constraintIterations; i++) { + Constraint.solveAll(allConstraints, delta); + } + Constraint.postSolveAll(allBodies); + Resolver.preSolveVelocity(pairs.list); + for (i = 0;i < engine.velocityIterations; i++) { + Resolver.solveVelocity(pairs.list, delta); + } + Engine._bodiesUpdateVelocities(allBodies); + if (pairs.collisionActive.length > 0) { + Events.trigger(engine, "collisionActive", { + pairs: pairs.collisionActive, + timestamp: timing.timestamp, + delta + }); + } + if (pairs.collisionEnd.length > 0) { + Events.trigger(engine, "collisionEnd", { + pairs: pairs.collisionEnd, + timestamp: timing.timestamp, + delta + }); + } + Engine._bodiesClearForces(allBodies); + Events.trigger(engine, "afterUpdate", event); + engine.timing.lastElapsed = Common.now() - startTime; + return engine; + }; + Engine.merge = function(engineA, engineB) { + Common.extend(engineA, engineB); + if (engineB.world) { + engineA.world = engineB.world; + Engine.clear(engineA); + var bodies = Composite.allBodies(engineA.world); + for (var i = 0;i < bodies.length; i++) { + var body = bodies[i]; + Sleeping.set(body, false); + body.id = Common.nextId(); + } + } + }; + Engine.clear = function(engine) { + Pairs.clear(engine.pairs); + Detector.clear(engine.detector); + }; + Engine._bodiesClearForces = function(bodies) { + var bodiesLength = bodies.length; + for (var i = 0;i < bodiesLength; i++) { + var body = bodies[i]; + body.force.x = 0; + body.force.y = 0; + body.torque = 0; + } + }; + Engine._bodiesApplyGravity = function(bodies, gravity) { + var gravityScale = typeof gravity.scale !== "undefined" ? gravity.scale : 0.001, bodiesLength = bodies.length; + if (gravity.x === 0 && gravity.y === 0 || gravityScale === 0) { + return; + } + for (var i = 0;i < bodiesLength; i++) { + var body = bodies[i]; + if (body.isStatic || body.isSleeping) + continue; + body.force.y += body.mass * gravity.y * gravityScale; + body.force.x += body.mass * gravity.x * gravityScale; + } + }; + Engine._bodiesUpdate = function(bodies, delta) { + var bodiesLength = bodies.length; + for (var i = 0;i < bodiesLength; i++) { + var body = bodies[i]; + if (body.isStatic || body.isSleeping) + continue; + Body.update(body, delta); + } + }; + Engine._bodiesUpdateVelocities = function(bodies) { + var bodiesLength = bodies.length; + for (var i = 0;i < bodiesLength; i++) { + Body.updateVelocities(bodies[i]); + } + }; + })(); + }, + function(module2, exports2, __webpack_require__) { + var Resolver = {}; + module2.exports = Resolver; + var Vertices = __webpack_require__(3); + var Common = __webpack_require__(0); + var Bounds = __webpack_require__(1); + (function() { + Resolver._restingThresh = 2; + Resolver._restingThreshTangent = Math.sqrt(6); + Resolver._positionDampen = 0.9; + Resolver._positionWarming = 0.8; + Resolver._frictionNormalMultiplier = 5; + Resolver._frictionMaxStatic = Number.MAX_VALUE; + Resolver.preSolvePosition = function(pairs) { + var i, pair, contactCount, pairsLength = pairs.length; + for (i = 0;i < pairsLength; i++) { + pair = pairs[i]; + if (!pair.isActive) + continue; + contactCount = pair.contactCount; + pair.collision.parentA.totalContacts += contactCount; + pair.collision.parentB.totalContacts += contactCount; + } + }; + Resolver.solvePosition = function(pairs, delta, damping) { + var i, pair, collision, bodyA, bodyB, normal, contactShare, positionImpulse, positionDampen = Resolver._positionDampen * (damping || 1), slopDampen = Common.clamp(delta / Common._baseDelta, 0, 1), pairsLength = pairs.length; + for (i = 0;i < pairsLength; i++) { + pair = pairs[i]; + if (!pair.isActive || pair.isSensor) + continue; + collision = pair.collision; + bodyA = collision.parentA; + bodyB = collision.parentB; + normal = collision.normal; + pair.separation = collision.depth + normal.x * (bodyB.positionImpulse.x - bodyA.positionImpulse.x) + normal.y * (bodyB.positionImpulse.y - bodyA.positionImpulse.y); + } + for (i = 0;i < pairsLength; i++) { + pair = pairs[i]; + if (!pair.isActive || pair.isSensor) + continue; + collision = pair.collision; + bodyA = collision.parentA; + bodyB = collision.parentB; + normal = collision.normal; + positionImpulse = pair.separation - pair.slop * slopDampen; + if (bodyA.isStatic || bodyB.isStatic) + positionImpulse *= 2; + if (!(bodyA.isStatic || bodyA.isSleeping)) { + contactShare = positionDampen / bodyA.totalContacts; + bodyA.positionImpulse.x += normal.x * positionImpulse * contactShare; + bodyA.positionImpulse.y += normal.y * positionImpulse * contactShare; + } + if (!(bodyB.isStatic || bodyB.isSleeping)) { + contactShare = positionDampen / bodyB.totalContacts; + bodyB.positionImpulse.x -= normal.x * positionImpulse * contactShare; + bodyB.positionImpulse.y -= normal.y * positionImpulse * contactShare; + } + } + }; + Resolver.postSolvePosition = function(bodies) { + var positionWarming = Resolver._positionWarming, bodiesLength = bodies.length, verticesTranslate = Vertices.translate, boundsUpdate = Bounds.update; + for (var i = 0;i < bodiesLength; i++) { + var body = bodies[i], positionImpulse = body.positionImpulse, positionImpulseX = positionImpulse.x, positionImpulseY = positionImpulse.y, velocity = body.velocity; + body.totalContacts = 0; + if (positionImpulseX !== 0 || positionImpulseY !== 0) { + for (var j = 0;j < body.parts.length; j++) { + var part = body.parts[j]; + verticesTranslate(part.vertices, positionImpulse); + boundsUpdate(part.bounds, part.vertices, velocity); + part.position.x += positionImpulseX; + part.position.y += positionImpulseY; + } + body.positionPrev.x += positionImpulseX; + body.positionPrev.y += positionImpulseY; + if (positionImpulseX * velocity.x + positionImpulseY * velocity.y < 0) { + positionImpulse.x = 0; + positionImpulse.y = 0; + } else { + positionImpulse.x *= positionWarming; + positionImpulse.y *= positionWarming; + } + } + } + }; + Resolver.preSolveVelocity = function(pairs) { + var pairsLength = pairs.length, i, j; + for (i = 0;i < pairsLength; i++) { + var pair = pairs[i]; + if (!pair.isActive || pair.isSensor) + continue; + var { contacts, contactCount, collision } = pair, bodyA = collision.parentA, bodyB = collision.parentB, normal = collision.normal, tangent = collision.tangent; + for (j = 0;j < contactCount; j++) { + var contact = contacts[j], contactVertex = contact.vertex, normalImpulse = contact.normalImpulse, tangentImpulse = contact.tangentImpulse; + if (normalImpulse !== 0 || tangentImpulse !== 0) { + var impulseX = normal.x * normalImpulse + tangent.x * tangentImpulse, impulseY = normal.y * normalImpulse + tangent.y * tangentImpulse; + if (!(bodyA.isStatic || bodyA.isSleeping)) { + bodyA.positionPrev.x += impulseX * bodyA.inverseMass; + bodyA.positionPrev.y += impulseY * bodyA.inverseMass; + bodyA.anglePrev += bodyA.inverseInertia * ((contactVertex.x - bodyA.position.x) * impulseY - (contactVertex.y - bodyA.position.y) * impulseX); + } + if (!(bodyB.isStatic || bodyB.isSleeping)) { + bodyB.positionPrev.x -= impulseX * bodyB.inverseMass; + bodyB.positionPrev.y -= impulseY * bodyB.inverseMass; + bodyB.anglePrev -= bodyB.inverseInertia * ((contactVertex.x - bodyB.position.x) * impulseY - (contactVertex.y - bodyB.position.y) * impulseX); + } + } + } + } + }; + Resolver.solveVelocity = function(pairs, delta) { + var timeScale = delta / Common._baseDelta, timeScaleSquared = timeScale * timeScale, timeScaleCubed = timeScaleSquared * timeScale, restingThresh = -Resolver._restingThresh * timeScale, restingThreshTangent = Resolver._restingThreshTangent, frictionNormalMultiplier = Resolver._frictionNormalMultiplier * timeScale, frictionMaxStatic = Resolver._frictionMaxStatic, pairsLength = pairs.length, tangentImpulse, maxFriction, i, j; + for (i = 0;i < pairsLength; i++) { + var pair = pairs[i]; + if (!pair.isActive || pair.isSensor) + continue; + var collision = pair.collision, bodyA = collision.parentA, bodyB = collision.parentB, normalX = collision.normal.x, normalY = collision.normal.y, tangentX = collision.tangent.x, tangentY = collision.tangent.y, inverseMassTotal = pair.inverseMass, friction = pair.friction * pair.frictionStatic * frictionNormalMultiplier, contacts = pair.contacts, contactCount = pair.contactCount, contactShare = 1 / contactCount; + var bodyAVelocityX = bodyA.position.x - bodyA.positionPrev.x, bodyAVelocityY = bodyA.position.y - bodyA.positionPrev.y, bodyAAngularVelocity = bodyA.angle - bodyA.anglePrev, bodyBVelocityX = bodyB.position.x - bodyB.positionPrev.x, bodyBVelocityY = bodyB.position.y - bodyB.positionPrev.y, bodyBAngularVelocity = bodyB.angle - bodyB.anglePrev; + for (j = 0;j < contactCount; j++) { + var contact = contacts[j], contactVertex = contact.vertex; + var offsetAX = contactVertex.x - bodyA.position.x, offsetAY = contactVertex.y - bodyA.position.y, offsetBX = contactVertex.x - bodyB.position.x, offsetBY = contactVertex.y - bodyB.position.y; + var velocityPointAX = bodyAVelocityX - offsetAY * bodyAAngularVelocity, velocityPointAY = bodyAVelocityY + offsetAX * bodyAAngularVelocity, velocityPointBX = bodyBVelocityX - offsetBY * bodyBAngularVelocity, velocityPointBY = bodyBVelocityY + offsetBX * bodyBAngularVelocity; + var relativeVelocityX = velocityPointAX - velocityPointBX, relativeVelocityY = velocityPointAY - velocityPointBY; + var normalVelocity = normalX * relativeVelocityX + normalY * relativeVelocityY, tangentVelocity = tangentX * relativeVelocityX + tangentY * relativeVelocityY; + var normalOverlap = pair.separation + normalVelocity; + var normalForce = Math.min(normalOverlap, 1); + normalForce = normalOverlap < 0 ? 0 : normalForce; + var frictionLimit = normalForce * friction; + if (tangentVelocity < -frictionLimit || tangentVelocity > frictionLimit) { + maxFriction = tangentVelocity > 0 ? tangentVelocity : -tangentVelocity; + tangentImpulse = pair.friction * (tangentVelocity > 0 ? 1 : -1) * timeScaleCubed; + if (tangentImpulse < -maxFriction) { + tangentImpulse = -maxFriction; + } else if (tangentImpulse > maxFriction) { + tangentImpulse = maxFriction; + } + } else { + tangentImpulse = tangentVelocity; + maxFriction = frictionMaxStatic; + } + var oAcN = offsetAX * normalY - offsetAY * normalX, oBcN = offsetBX * normalY - offsetBY * normalX, share = contactShare / (inverseMassTotal + bodyA.inverseInertia * oAcN * oAcN + bodyB.inverseInertia * oBcN * oBcN); + var normalImpulse = (1 + pair.restitution) * normalVelocity * share; + tangentImpulse *= share; + if (normalVelocity < restingThresh) { + contact.normalImpulse = 0; + } else { + var contactNormalImpulse = contact.normalImpulse; + contact.normalImpulse += normalImpulse; + if (contact.normalImpulse > 0) + contact.normalImpulse = 0; + normalImpulse = contact.normalImpulse - contactNormalImpulse; + } + if (tangentVelocity < -restingThreshTangent || tangentVelocity > restingThreshTangent) { + contact.tangentImpulse = 0; + } else { + var contactTangentImpulse = contact.tangentImpulse; + contact.tangentImpulse += tangentImpulse; + if (contact.tangentImpulse < -maxFriction) + contact.tangentImpulse = -maxFriction; + if (contact.tangentImpulse > maxFriction) + contact.tangentImpulse = maxFriction; + tangentImpulse = contact.tangentImpulse - contactTangentImpulse; + } + var impulseX = normalX * normalImpulse + tangentX * tangentImpulse, impulseY = normalY * normalImpulse + tangentY * tangentImpulse; + if (!(bodyA.isStatic || bodyA.isSleeping)) { + bodyA.positionPrev.x += impulseX * bodyA.inverseMass; + bodyA.positionPrev.y += impulseY * bodyA.inverseMass; + bodyA.anglePrev += (offsetAX * impulseY - offsetAY * impulseX) * bodyA.inverseInertia; + } + if (!(bodyB.isStatic || bodyB.isSleeping)) { + bodyB.positionPrev.x -= impulseX * bodyB.inverseMass; + bodyB.positionPrev.y -= impulseY * bodyB.inverseMass; + bodyB.anglePrev -= (offsetBX * impulseY - offsetBY * impulseX) * bodyB.inverseInertia; + } + } + } + }; + })(); + }, + function(module2, exports2, __webpack_require__) { + var Pairs = {}; + module2.exports = Pairs; + var Pair = __webpack_require__(9); + var Common = __webpack_require__(0); + (function() { + Pairs.create = function(options) { + return Common.extend({ + table: {}, + list: [], + collisionStart: [], + collisionActive: [], + collisionEnd: [] + }, options); + }; + Pairs.update = function(pairs, collisions, timestamp) { + var { update: pairUpdate, create: pairCreate, setActive: pairSetActive } = Pair, pairsTable = pairs.table, pairsList = pairs.list, pairsListLength = pairsList.length, pairsListIndex = pairsListLength, collisionStart = pairs.collisionStart, collisionEnd = pairs.collisionEnd, collisionActive = pairs.collisionActive, collisionsLength = collisions.length, collisionStartIndex = 0, collisionEndIndex = 0, collisionActiveIndex = 0, collision, pair, i; + for (i = 0;i < collisionsLength; i++) { + collision = collisions[i]; + pair = collision.pair; + if (pair) { + if (pair.isActive) { + collisionActive[collisionActiveIndex++] = pair; + } + pairUpdate(pair, collision, timestamp); + } else { + pair = pairCreate(collision, timestamp); + pairsTable[pair.id] = pair; + collisionStart[collisionStartIndex++] = pair; + pairsList[pairsListIndex++] = pair; + } + } + pairsListIndex = 0; + pairsListLength = pairsList.length; + for (i = 0;i < pairsListLength; i++) { + pair = pairsList[i]; + if (pair.timeUpdated >= timestamp) { + pairsList[pairsListIndex++] = pair; + } else { + pairSetActive(pair, false, timestamp); + if (pair.collision.bodyA.sleepCounter > 0 && pair.collision.bodyB.sleepCounter > 0) { + pairsList[pairsListIndex++] = pair; + } else { + collisionEnd[collisionEndIndex++] = pair; + delete pairsTable[pair.id]; + } + } + } + if (pairsList.length !== pairsListIndex) { + pairsList.length = pairsListIndex; + } + if (collisionStart.length !== collisionStartIndex) { + collisionStart.length = collisionStartIndex; + } + if (collisionEnd.length !== collisionEndIndex) { + collisionEnd.length = collisionEndIndex; + } + if (collisionActive.length !== collisionActiveIndex) { + collisionActive.length = collisionActiveIndex; + } + }; + Pairs.clear = function(pairs) { + pairs.table = {}; + pairs.list.length = 0; + pairs.collisionStart.length = 0; + pairs.collisionActive.length = 0; + pairs.collisionEnd.length = 0; + return pairs; + }; + })(); + }, + function(module2, exports2, __webpack_require__) { + var Matter = module2.exports = __webpack_require__(21); + Matter.Axes = __webpack_require__(11); + Matter.Bodies = __webpack_require__(12); + Matter.Body = __webpack_require__(4); + Matter.Bounds = __webpack_require__(1); + Matter.Collision = __webpack_require__(8); + Matter.Common = __webpack_require__(0); + Matter.Composite = __webpack_require__(6); + Matter.Composites = __webpack_require__(22); + Matter.Constraint = __webpack_require__(10); + Matter.Contact = __webpack_require__(16); + Matter.Detector = __webpack_require__(13); + Matter.Engine = __webpack_require__(17); + Matter.Events = __webpack_require__(5); + Matter.Grid = __webpack_require__(23); + Matter.Mouse = __webpack_require__(14); + Matter.MouseConstraint = __webpack_require__(24); + Matter.Pair = __webpack_require__(9); + Matter.Pairs = __webpack_require__(19); + Matter.Plugin = __webpack_require__(15); + Matter.Query = __webpack_require__(25); + Matter.Render = __webpack_require__(26); + Matter.Resolver = __webpack_require__(18); + Matter.Runner = __webpack_require__(27); + Matter.SAT = __webpack_require__(28); + Matter.Sleeping = __webpack_require__(7); + Matter.Svg = __webpack_require__(29); + Matter.Vector = __webpack_require__(2); + Matter.Vertices = __webpack_require__(3); + Matter.World = __webpack_require__(30); + Matter.Engine.run = Matter.Runner.run; + Matter.Common.deprecated(Matter.Engine, "run", "Engine.run ➤ use Matter.Runner.run(engine) instead"); + }, + function(module2, exports2, __webpack_require__) { + var Matter = {}; + module2.exports = Matter; + var Plugin = __webpack_require__(15); + var Common = __webpack_require__(0); + (function() { + Matter.name = "matter-js"; + Matter.version = "0.20.0"; + Matter.uses = []; + Matter.used = []; + Matter.use = function() { + Plugin.use(Matter, Array.prototype.slice.call(arguments)); + }; + Matter.before = function(path, func) { + path = path.replace(/^Matter./, ""); + return Common.chainPathBefore(Matter, path, func); + }; + Matter.after = function(path, func) { + path = path.replace(/^Matter./, ""); + return Common.chainPathAfter(Matter, path, func); + }; + })(); + }, + function(module2, exports2, __webpack_require__) { + var Composites = {}; + module2.exports = Composites; + var Composite = __webpack_require__(6); + var Constraint = __webpack_require__(10); + var Common = __webpack_require__(0); + var Body = __webpack_require__(4); + var Bodies = __webpack_require__(12); + var deprecated = Common.deprecated; + (function() { + Composites.stack = function(x, y, columns, rows, columnGap, rowGap, callback) { + var stack = Composite.create({ label: "Stack" }), currentX = x, currentY = y, lastBody, i = 0; + for (var row = 0;row < rows; row++) { + var maxHeight = 0; + for (var column = 0;column < columns; column++) { + var body = callback(currentX, currentY, column, row, lastBody, i); + if (body) { + var bodyHeight = body.bounds.max.y - body.bounds.min.y, bodyWidth = body.bounds.max.x - body.bounds.min.x; + if (bodyHeight > maxHeight) + maxHeight = bodyHeight; + Body.translate(body, { x: bodyWidth * 0.5, y: bodyHeight * 0.5 }); + currentX = body.bounds.max.x + columnGap; + Composite.addBody(stack, body); + lastBody = body; + i += 1; + } else { + currentX += columnGap; + } + } + currentY += maxHeight + rowGap; + currentX = x; + } + return stack; + }; + Composites.chain = function(composite, xOffsetA, yOffsetA, xOffsetB, yOffsetB, options) { + var bodies = composite.bodies; + for (var i = 1;i < bodies.length; i++) { + var bodyA = bodies[i - 1], bodyB = bodies[i], bodyAHeight = bodyA.bounds.max.y - bodyA.bounds.min.y, bodyAWidth = bodyA.bounds.max.x - bodyA.bounds.min.x, bodyBHeight = bodyB.bounds.max.y - bodyB.bounds.min.y, bodyBWidth = bodyB.bounds.max.x - bodyB.bounds.min.x; + var defaults = { + bodyA, + pointA: { x: bodyAWidth * xOffsetA, y: bodyAHeight * yOffsetA }, + bodyB, + pointB: { x: bodyBWidth * xOffsetB, y: bodyBHeight * yOffsetB } + }; + var constraint = Common.extend(defaults, options); + Composite.addConstraint(composite, Constraint.create(constraint)); + } + composite.label += " Chain"; + return composite; + }; + Composites.mesh = function(composite, columns, rows, crossBrace, options) { + var bodies = composite.bodies, row, col, bodyA, bodyB, bodyC; + for (row = 0;row < rows; row++) { + for (col = 1;col < columns; col++) { + bodyA = bodies[col - 1 + row * columns]; + bodyB = bodies[col + row * columns]; + Composite.addConstraint(composite, Constraint.create(Common.extend({ bodyA, bodyB }, options))); + } + if (row > 0) { + for (col = 0;col < columns; col++) { + bodyA = bodies[col + (row - 1) * columns]; + bodyB = bodies[col + row * columns]; + Composite.addConstraint(composite, Constraint.create(Common.extend({ bodyA, bodyB }, options))); + if (crossBrace && col > 0) { + bodyC = bodies[col - 1 + (row - 1) * columns]; + Composite.addConstraint(composite, Constraint.create(Common.extend({ bodyA: bodyC, bodyB }, options))); + } + if (crossBrace && col < columns - 1) { + bodyC = bodies[col + 1 + (row - 1) * columns]; + Composite.addConstraint(composite, Constraint.create(Common.extend({ bodyA: bodyC, bodyB }, options))); + } + } + } + } + composite.label += " Mesh"; + return composite; + }; + Composites.pyramid = function(x, y, columns, rows, columnGap, rowGap, callback) { + return Composites.stack(x, y, columns, rows, columnGap, rowGap, function(stackX, stackY, column, row, lastBody, i) { + var actualRows = Math.min(rows, Math.ceil(columns / 2)), lastBodyWidth = lastBody ? lastBody.bounds.max.x - lastBody.bounds.min.x : 0; + if (row > actualRows) + return; + row = actualRows - row; + var start = row, end = columns - 1 - row; + if (column < start || column > end) + return; + if (i === 1) { + Body.translate(lastBody, { x: (column + (columns % 2 === 1 ? 1 : -1)) * lastBodyWidth, y: 0 }); + } + var xOffset = lastBody ? column * lastBodyWidth : 0; + return callback(x + xOffset + column * columnGap, stackY, column, row, lastBody, i); + }); + }; + Composites.newtonsCradle = function(x, y, number, size, length) { + var newtonsCradle = Composite.create({ label: "Newtons Cradle" }); + for (var i = 0;i < number; i++) { + var separation = 1.9, circle = Bodies.circle(x + i * (size * separation), y + length, size, { inertia: Infinity, restitution: 1, friction: 0, frictionAir: 0.0001, slop: 1 }), constraint = Constraint.create({ pointA: { x: x + i * (size * separation), y }, bodyB: circle }); + Composite.addBody(newtonsCradle, circle); + Composite.addConstraint(newtonsCradle, constraint); + } + return newtonsCradle; + }; + deprecated(Composites, "newtonsCradle", "Composites.newtonsCradle ➤ moved to newtonsCradle example"); + Composites.car = function(x, y, width, height, wheelSize) { + var group = Body.nextGroup(true), wheelBase = 20, wheelAOffset = -width * 0.5 + wheelBase, wheelBOffset = width * 0.5 - wheelBase, wheelYOffset = 0; + var car = Composite.create({ label: "Car" }), body = Bodies.rectangle(x, y, width, height, { + collisionFilter: { + group + }, + chamfer: { + radius: height * 0.5 + }, + density: 0.0002 + }); + var wheelA = Bodies.circle(x + wheelAOffset, y + wheelYOffset, wheelSize, { + collisionFilter: { + group + }, + friction: 0.8 + }); + var wheelB = Bodies.circle(x + wheelBOffset, y + wheelYOffset, wheelSize, { + collisionFilter: { + group + }, + friction: 0.8 + }); + var axelA = Constraint.create({ + bodyB: body, + pointB: { x: wheelAOffset, y: wheelYOffset }, + bodyA: wheelA, + stiffness: 1, + length: 0 + }); + var axelB = Constraint.create({ + bodyB: body, + pointB: { x: wheelBOffset, y: wheelYOffset }, + bodyA: wheelB, + stiffness: 1, + length: 0 + }); + Composite.addBody(car, body); + Composite.addBody(car, wheelA); + Composite.addBody(car, wheelB); + Composite.addConstraint(car, axelA); + Composite.addConstraint(car, axelB); + return car; + }; + deprecated(Composites, "car", "Composites.car ➤ moved to car example"); + Composites.softBody = function(x, y, columns, rows, columnGap, rowGap, crossBrace, particleRadius, particleOptions, constraintOptions) { + particleOptions = Common.extend({ inertia: Infinity }, particleOptions); + constraintOptions = Common.extend({ stiffness: 0.2, render: { type: "line", anchors: false } }, constraintOptions); + var softBody = Composites.stack(x, y, columns, rows, columnGap, rowGap, function(stackX, stackY) { + return Bodies.circle(stackX, stackY, particleRadius, particleOptions); + }); + Composites.mesh(softBody, columns, rows, crossBrace, constraintOptions); + softBody.label = "Soft Body"; + return softBody; + }; + deprecated(Composites, "softBody", "Composites.softBody ➤ moved to softBody and cloth examples"); + })(); + }, + function(module2, exports2, __webpack_require__) { + var Grid = {}; + module2.exports = Grid; + var Pair = __webpack_require__(9); + var Common = __webpack_require__(0); + var deprecated = Common.deprecated; + (function() { + Grid.create = function(options) { + var defaults = { + buckets: {}, + pairs: {}, + pairsList: [], + bucketWidth: 48, + bucketHeight: 48 + }; + return Common.extend(defaults, options); + }; + Grid.update = function(grid, bodies, engine, forceUpdate) { + var i, col, row, world = engine.world, buckets = grid.buckets, bucket, bucketId, gridChanged = false; + for (i = 0;i < bodies.length; i++) { + var body = bodies[i]; + if (body.isSleeping && !forceUpdate) + continue; + if (world.bounds && (body.bounds.max.x < world.bounds.min.x || body.bounds.min.x > world.bounds.max.x || body.bounds.max.y < world.bounds.min.y || body.bounds.min.y > world.bounds.max.y)) + continue; + var newRegion = Grid._getRegion(grid, body); + if (!body.region || newRegion.id !== body.region.id || forceUpdate) { + if (!body.region || forceUpdate) + body.region = newRegion; + var union = Grid._regionUnion(newRegion, body.region); + for (col = union.startCol;col <= union.endCol; col++) { + for (row = union.startRow;row <= union.endRow; row++) { + bucketId = Grid._getBucketId(col, row); + bucket = buckets[bucketId]; + var isInsideNewRegion = col >= newRegion.startCol && col <= newRegion.endCol && row >= newRegion.startRow && row <= newRegion.endRow; + var isInsideOldRegion = col >= body.region.startCol && col <= body.region.endCol && row >= body.region.startRow && row <= body.region.endRow; + if (!isInsideNewRegion && isInsideOldRegion) { + if (isInsideOldRegion) { + if (bucket) + Grid._bucketRemoveBody(grid, bucket, body); + } + } + if (body.region === newRegion || isInsideNewRegion && !isInsideOldRegion || forceUpdate) { + if (!bucket) + bucket = Grid._createBucket(buckets, bucketId); + Grid._bucketAddBody(grid, bucket, body); + } + } + } + body.region = newRegion; + gridChanged = true; + } + } + if (gridChanged) + grid.pairsList = Grid._createActivePairsList(grid); + }; + deprecated(Grid, "update", "Grid.update ➤ replaced by Matter.Detector"); + Grid.clear = function(grid) { + grid.buckets = {}; + grid.pairs = {}; + grid.pairsList = []; + }; + deprecated(Grid, "clear", "Grid.clear ➤ replaced by Matter.Detector"); + Grid._regionUnion = function(regionA, regionB) { + var startCol = Math.min(regionA.startCol, regionB.startCol), endCol = Math.max(regionA.endCol, regionB.endCol), startRow = Math.min(regionA.startRow, regionB.startRow), endRow = Math.max(regionA.endRow, regionB.endRow); + return Grid._createRegion(startCol, endCol, startRow, endRow); + }; + Grid._getRegion = function(grid, body) { + var bounds = body.bounds, startCol = Math.floor(bounds.min.x / grid.bucketWidth), endCol = Math.floor(bounds.max.x / grid.bucketWidth), startRow = Math.floor(bounds.min.y / grid.bucketHeight), endRow = Math.floor(bounds.max.y / grid.bucketHeight); + return Grid._createRegion(startCol, endCol, startRow, endRow); + }; + Grid._createRegion = function(startCol, endCol, startRow, endRow) { + return { + id: startCol + "," + endCol + "," + startRow + "," + endRow, + startCol, + endCol, + startRow, + endRow + }; + }; + Grid._getBucketId = function(column, row) { + return "C" + column + "R" + row; + }; + Grid._createBucket = function(buckets, bucketId) { + var bucket = buckets[bucketId] = []; + return bucket; + }; + Grid._bucketAddBody = function(grid, bucket, body) { + var gridPairs = grid.pairs, pairId = Pair.id, bucketLength = bucket.length, i; + for (i = 0;i < bucketLength; i++) { + var bodyB = bucket[i]; + if (body.id === bodyB.id || body.isStatic && bodyB.isStatic) + continue; + var id = pairId(body, bodyB), pair = gridPairs[id]; + if (pair) { + pair[2] += 1; + } else { + gridPairs[id] = [body, bodyB, 1]; + } + } + bucket.push(body); + }; + Grid._bucketRemoveBody = function(grid, bucket, body) { + var gridPairs = grid.pairs, pairId = Pair.id, i; + bucket.splice(Common.indexOf(bucket, body), 1); + var bucketLength = bucket.length; + for (i = 0;i < bucketLength; i++) { + var pair = gridPairs[pairId(body, bucket[i])]; + if (pair) + pair[2] -= 1; + } + }; + Grid._createActivePairsList = function(grid) { + var pair, gridPairs = grid.pairs, pairKeys = Common.keys(gridPairs), pairKeysLength = pairKeys.length, pairs = [], k; + for (k = 0;k < pairKeysLength; k++) { + pair = gridPairs[pairKeys[k]]; + if (pair[2] > 0) { + pairs.push(pair); + } else { + delete gridPairs[pairKeys[k]]; + } + } + return pairs; + }; + })(); + }, + function(module2, exports2, __webpack_require__) { + var MouseConstraint = {}; + module2.exports = MouseConstraint; + var Vertices = __webpack_require__(3); + var Sleeping = __webpack_require__(7); + var Mouse = __webpack_require__(14); + var Events = __webpack_require__(5); + var Detector = __webpack_require__(13); + var Constraint = __webpack_require__(10); + var Composite = __webpack_require__(6); + var Common = __webpack_require__(0); + var Bounds = __webpack_require__(1); + (function() { + MouseConstraint.create = function(engine, options) { + var mouse = (engine ? engine.mouse : null) || (options ? options.mouse : null); + if (!mouse) { + if (engine && engine.render && engine.render.canvas) { + mouse = Mouse.create(engine.render.canvas); + } else if (options && options.element) { + mouse = Mouse.create(options.element); + } else { + mouse = Mouse.create(); + Common.warn("MouseConstraint.create: options.mouse was undefined, options.element was undefined, may not function as expected"); + } + } + var constraint = Constraint.create({ + label: "Mouse Constraint", + pointA: mouse.position, + pointB: { x: 0, y: 0 }, + length: 0.01, + stiffness: 0.1, + angularStiffness: 1, + render: { + strokeStyle: "#90EE90", + lineWidth: 3 + } + }); + var defaults = { + type: "mouseConstraint", + mouse, + element: null, + body: null, + constraint, + collisionFilter: { + category: 1, + mask: 4294967295, + group: 0 + } + }; + var mouseConstraint = Common.extend(defaults, options); + Events.on(engine, "beforeUpdate", function() { + var allBodies = Composite.allBodies(engine.world); + MouseConstraint.update(mouseConstraint, allBodies); + MouseConstraint._triggerEvents(mouseConstraint); + }); + return mouseConstraint; + }; + MouseConstraint.update = function(mouseConstraint, bodies) { + var { mouse, constraint, body } = mouseConstraint; + if (mouse.button === 0) { + if (!constraint.bodyB) { + for (var i = 0;i < bodies.length; i++) { + body = bodies[i]; + if (Bounds.contains(body.bounds, mouse.position) && Detector.canCollide(body.collisionFilter, mouseConstraint.collisionFilter)) { + for (var j = body.parts.length > 1 ? 1 : 0;j < body.parts.length; j++) { + var part = body.parts[j]; + if (Vertices.contains(part.vertices, mouse.position)) { + constraint.pointA = mouse.position; + constraint.bodyB = mouseConstraint.body = body; + constraint.pointB = { x: mouse.position.x - body.position.x, y: mouse.position.y - body.position.y }; + constraint.angleB = body.angle; + Sleeping.set(body, false); + Events.trigger(mouseConstraint, "startdrag", { mouse, body }); + break; + } + } + } + } + } else { + Sleeping.set(constraint.bodyB, false); + constraint.pointA = mouse.position; + } + } else { + constraint.bodyB = mouseConstraint.body = null; + constraint.pointB = null; + if (body) + Events.trigger(mouseConstraint, "enddrag", { mouse, body }); + } + }; + MouseConstraint._triggerEvents = function(mouseConstraint) { + var mouse = mouseConstraint.mouse, mouseEvents = mouse.sourceEvents; + if (mouseEvents.mousemove) + Events.trigger(mouseConstraint, "mousemove", { mouse }); + if (mouseEvents.mousedown) + Events.trigger(mouseConstraint, "mousedown", { mouse }); + if (mouseEvents.mouseup) + Events.trigger(mouseConstraint, "mouseup", { mouse }); + Mouse.clearSourceEvents(mouse); + }; + })(); + }, + function(module2, exports2, __webpack_require__) { + var Query = {}; + module2.exports = Query; + var Vector2 = __webpack_require__(2); + var Collision = __webpack_require__(8); + var Bounds = __webpack_require__(1); + var Bodies = __webpack_require__(12); + var Vertices = __webpack_require__(3); + (function() { + Query.collides = function(body, bodies) { + var collisions = [], bodiesLength = bodies.length, bounds = body.bounds, collides = Collision.collides, overlaps = Bounds.overlaps; + for (var i = 0;i < bodiesLength; i++) { + var bodyA = bodies[i], partsALength = bodyA.parts.length, partsAStart = partsALength === 1 ? 0 : 1; + if (overlaps(bodyA.bounds, bounds)) { + for (var j = partsAStart;j < partsALength; j++) { + var part = bodyA.parts[j]; + if (overlaps(part.bounds, bounds)) { + var collision = collides(part, body); + if (collision) { + collisions.push(collision); + break; + } + } + } + } + } + return collisions; + }; + Query.ray = function(bodies, startPoint, endPoint, rayWidth) { + rayWidth = rayWidth || 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001; + var rayAngle = Vector2.angle(startPoint, endPoint), rayLength = Vector2.magnitude(Vector2.sub(startPoint, endPoint)), rayX = (endPoint.x + startPoint.x) * 0.5, rayY = (endPoint.y + startPoint.y) * 0.5, ray = Bodies.rectangle(rayX, rayY, rayLength, rayWidth, { angle: rayAngle }), collisions = Query.collides(ray, bodies); + for (var i = 0;i < collisions.length; i += 1) { + var collision = collisions[i]; + collision.body = collision.bodyB = collision.bodyA; + } + return collisions; + }; + Query.region = function(bodies, bounds, outside) { + var result = []; + for (var i = 0;i < bodies.length; i++) { + var body = bodies[i], overlaps = Bounds.overlaps(body.bounds, bounds); + if (overlaps && !outside || !overlaps && outside) + result.push(body); + } + return result; + }; + Query.point = function(bodies, point) { + var result = []; + for (var i = 0;i < bodies.length; i++) { + var body = bodies[i]; + if (Bounds.contains(body.bounds, point)) { + for (var j = body.parts.length === 1 ? 0 : 1;j < body.parts.length; j++) { + var part = body.parts[j]; + if (Bounds.contains(part.bounds, point) && Vertices.contains(part.vertices, point)) { + result.push(body); + break; + } + } + } + } + return result; + }; + })(); + }, + function(module2, exports2, __webpack_require__) { + var Render = {}; + module2.exports = Render; + var Body = __webpack_require__(4); + var Common = __webpack_require__(0); + var Composite = __webpack_require__(6); + var Bounds = __webpack_require__(1); + var Events = __webpack_require__(5); + var Vector2 = __webpack_require__(2); + var Mouse = __webpack_require__(14); + (function() { + var _requestAnimationFrame, _cancelAnimationFrame; + if (typeof window !== "undefined") { + _requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.msRequestAnimationFrame || function(callback) { + window.setTimeout(function() { + callback(Common.now()); + }, 1000 / 60); + }; + _cancelAnimationFrame = window.cancelAnimationFrame || window.mozCancelAnimationFrame || window.webkitCancelAnimationFrame || window.msCancelAnimationFrame; + } + Render._goodFps = 30; + Render._goodDelta = 1000 / 60; + Render.create = function(options) { + var defaults = { + engine: null, + element: null, + canvas: null, + mouse: null, + frameRequestId: null, + timing: { + historySize: 60, + delta: 0, + deltaHistory: [], + lastTime: 0, + lastTimestamp: 0, + lastElapsed: 0, + timestampElapsed: 0, + timestampElapsedHistory: [], + engineDeltaHistory: [], + engineElapsedHistory: [], + engineUpdatesHistory: [], + elapsedHistory: [] + }, + options: { + width: 800, + height: 600, + pixelRatio: 1, + background: "#14151f", + wireframeBackground: "#14151f", + wireframeStrokeStyle: "#bbb", + hasBounds: !!options.bounds, + enabled: true, + wireframes: true, + showSleeping: true, + showDebug: false, + showStats: false, + showPerformance: false, + showBounds: false, + showVelocity: false, + showCollisions: false, + showSeparations: false, + showAxes: false, + showPositions: false, + showAngleIndicator: false, + showIds: false, + showVertexNumbers: false, + showConvexHulls: false, + showInternalEdges: false, + showMousePosition: false + } + }; + var render = Common.extend(defaults, options); + if (render.canvas) { + render.canvas.width = render.options.width || render.canvas.width; + render.canvas.height = render.options.height || render.canvas.height; + } + render.mouse = options.mouse; + render.engine = options.engine; + render.canvas = render.canvas || _createCanvas(render.options.width, render.options.height); + render.context = render.canvas.getContext("2d"); + render.textures = {}; + render.bounds = render.bounds || { + min: { + x: 0, + y: 0 + }, + max: { + x: render.canvas.width, + y: render.canvas.height + } + }; + render.controller = Render; + render.options.showBroadphase = false; + if (render.options.pixelRatio !== 1) { + Render.setPixelRatio(render, render.options.pixelRatio); + } + if (Common.isElement(render.element)) { + render.element.appendChild(render.canvas); + } + return render; + }; + Render.run = function(render) { + (function loop(time) { + render.frameRequestId = _requestAnimationFrame(loop); + _updateTiming(render, time); + Render.world(render, time); + render.context.setTransform(render.options.pixelRatio, 0, 0, render.options.pixelRatio, 0, 0); + if (render.options.showStats || render.options.showDebug) { + Render.stats(render, render.context, time); + } + if (render.options.showPerformance || render.options.showDebug) { + Render.performance(render, render.context, time); + } + render.context.setTransform(1, 0, 0, 1, 0, 0); + })(); + }; + Render.stop = function(render) { + _cancelAnimationFrame(render.frameRequestId); + }; + Render.setPixelRatio = function(render, pixelRatio) { + var { options, canvas } = render; + if (pixelRatio === "auto") { + pixelRatio = _getPixelRatio(canvas); + } + options.pixelRatio = pixelRatio; + canvas.setAttribute("data-pixel-ratio", pixelRatio); + canvas.width = options.width * pixelRatio; + canvas.height = options.height * pixelRatio; + canvas.style.width = options.width + "px"; + canvas.style.height = options.height + "px"; + }; + Render.setSize = function(render, width, height) { + render.options.width = width; + render.options.height = height; + render.bounds.max.x = render.bounds.min.x + width; + render.bounds.max.y = render.bounds.min.y + height; + if (render.options.pixelRatio !== 1) { + Render.setPixelRatio(render, render.options.pixelRatio); + } else { + render.canvas.width = width; + render.canvas.height = height; + } + }; + Render.lookAt = function(render, objects, padding, center) { + center = typeof center !== "undefined" ? center : true; + objects = Common.isArray(objects) ? objects : [objects]; + padding = padding || { + x: 0, + y: 0 + }; + var bounds = { + min: { x: Infinity, y: Infinity }, + max: { x: -Infinity, y: -Infinity } + }; + for (var i = 0;i < objects.length; i += 1) { + var object = objects[i], min = object.bounds ? object.bounds.min : object.min || object.position || object, max = object.bounds ? object.bounds.max : object.max || object.position || object; + if (min && max) { + if (min.x < bounds.min.x) + bounds.min.x = min.x; + if (max.x > bounds.max.x) + bounds.max.x = max.x; + if (min.y < bounds.min.y) + bounds.min.y = min.y; + if (max.y > bounds.max.y) + bounds.max.y = max.y; + } + } + var width = bounds.max.x - bounds.min.x + 2 * padding.x, height = bounds.max.y - bounds.min.y + 2 * padding.y, viewHeight = render.canvas.height, viewWidth = render.canvas.width, outerRatio = viewWidth / viewHeight, innerRatio = width / height, scaleX = 1, scaleY = 1; + if (innerRatio > outerRatio) { + scaleY = innerRatio / outerRatio; + } else { + scaleX = outerRatio / innerRatio; + } + render.options.hasBounds = true; + render.bounds.min.x = bounds.min.x; + render.bounds.max.x = bounds.min.x + width * scaleX; + render.bounds.min.y = bounds.min.y; + render.bounds.max.y = bounds.min.y + height * scaleY; + if (center) { + render.bounds.min.x += width * 0.5 - width * scaleX * 0.5; + render.bounds.max.x += width * 0.5 - width * scaleX * 0.5; + render.bounds.min.y += height * 0.5 - height * scaleY * 0.5; + render.bounds.max.y += height * 0.5 - height * scaleY * 0.5; + } + render.bounds.min.x -= padding.x; + render.bounds.max.x -= padding.x; + render.bounds.min.y -= padding.y; + render.bounds.max.y -= padding.y; + if (render.mouse) { + Mouse.setScale(render.mouse, { + x: (render.bounds.max.x - render.bounds.min.x) / render.canvas.width, + y: (render.bounds.max.y - render.bounds.min.y) / render.canvas.height + }); + Mouse.setOffset(render.mouse, render.bounds.min); + } + }; + Render.startViewTransform = function(render) { + var boundsWidth = render.bounds.max.x - render.bounds.min.x, boundsHeight = render.bounds.max.y - render.bounds.min.y, boundsScaleX = boundsWidth / render.options.width, boundsScaleY = boundsHeight / render.options.height; + render.context.setTransform(render.options.pixelRatio / boundsScaleX, 0, 0, render.options.pixelRatio / boundsScaleY, 0, 0); + render.context.translate(-render.bounds.min.x, -render.bounds.min.y); + }; + Render.endViewTransform = function(render) { + render.context.setTransform(render.options.pixelRatio, 0, 0, render.options.pixelRatio, 0, 0); + }; + Render.world = function(render, time) { + var startTime = Common.now(), engine = render.engine, world = engine.world, canvas = render.canvas, context = render.context, options = render.options, timing = render.timing; + var allBodies = Composite.allBodies(world), allConstraints = Composite.allConstraints(world), background = options.wireframes ? options.wireframeBackground : options.background, bodies = [], constraints = [], i; + var event = { + timestamp: engine.timing.timestamp + }; + Events.trigger(render, "beforeRender", event); + if (render.currentBackground !== background) + _applyBackground(render, background); + context.globalCompositeOperation = "source-in"; + context.fillStyle = "transparent"; + context.fillRect(0, 0, canvas.width, canvas.height); + context.globalCompositeOperation = "source-over"; + if (options.hasBounds) { + for (i = 0;i < allBodies.length; i++) { + var body = allBodies[i]; + if (Bounds.overlaps(body.bounds, render.bounds)) + bodies.push(body); + } + for (i = 0;i < allConstraints.length; i++) { + var constraint = allConstraints[i], bodyA = constraint.bodyA, bodyB = constraint.bodyB, pointAWorld = constraint.pointA, pointBWorld = constraint.pointB; + if (bodyA) + pointAWorld = Vector2.add(bodyA.position, constraint.pointA); + if (bodyB) + pointBWorld = Vector2.add(bodyB.position, constraint.pointB); + if (!pointAWorld || !pointBWorld) + continue; + if (Bounds.contains(render.bounds, pointAWorld) || Bounds.contains(render.bounds, pointBWorld)) + constraints.push(constraint); + } + Render.startViewTransform(render); + if (render.mouse) { + Mouse.setScale(render.mouse, { + x: (render.bounds.max.x - render.bounds.min.x) / render.options.width, + y: (render.bounds.max.y - render.bounds.min.y) / render.options.height + }); + Mouse.setOffset(render.mouse, render.bounds.min); + } + } else { + constraints = allConstraints; + bodies = allBodies; + if (render.options.pixelRatio !== 1) { + render.context.setTransform(render.options.pixelRatio, 0, 0, render.options.pixelRatio, 0, 0); + } + } + if (!options.wireframes || engine.enableSleeping && options.showSleeping) { + Render.bodies(render, bodies, context); + } else { + if (options.showConvexHulls) + Render.bodyConvexHulls(render, bodies, context); + Render.bodyWireframes(render, bodies, context); + } + if (options.showBounds) + Render.bodyBounds(render, bodies, context); + if (options.showAxes || options.showAngleIndicator) + Render.bodyAxes(render, bodies, context); + if (options.showPositions) + Render.bodyPositions(render, bodies, context); + if (options.showVelocity) + Render.bodyVelocity(render, bodies, context); + if (options.showIds) + Render.bodyIds(render, bodies, context); + if (options.showSeparations) + Render.separations(render, engine.pairs.list, context); + if (options.showCollisions) + Render.collisions(render, engine.pairs.list, context); + if (options.showVertexNumbers) + Render.vertexNumbers(render, bodies, context); + if (options.showMousePosition) + Render.mousePosition(render, render.mouse, context); + Render.constraints(constraints, context); + if (options.hasBounds) { + Render.endViewTransform(render); + } + Events.trigger(render, "afterRender", event); + timing.lastElapsed = Common.now() - startTime; + }; + Render.stats = function(render, context, time) { + var engine = render.engine, world = engine.world, bodies = Composite.allBodies(world), parts = 0, width = 55, height = 44, x = 0, y = 0; + for (var i = 0;i < bodies.length; i += 1) { + parts += bodies[i].parts.length; + } + var sections = { + Part: parts, + Body: bodies.length, + Cons: Composite.allConstraints(world).length, + Comp: Composite.allComposites(world).length, + Pair: engine.pairs.list.length + }; + context.fillStyle = "#0e0f19"; + context.fillRect(x, y, width * 5.5, height); + context.font = "12px Arial"; + context.textBaseline = "top"; + context.textAlign = "right"; + for (var key in sections) { + var section = sections[key]; + context.fillStyle = "#aaa"; + context.fillText(key, x + width, y + 8); + context.fillStyle = "#eee"; + context.fillText(section, x + width, y + 26); + x += width; + } + }; + Render.performance = function(render, context) { + var { engine, timing } = render, deltaHistory = timing.deltaHistory, elapsedHistory = timing.elapsedHistory, timestampElapsedHistory = timing.timestampElapsedHistory, engineDeltaHistory = timing.engineDeltaHistory, engineUpdatesHistory = timing.engineUpdatesHistory, engineElapsedHistory = timing.engineElapsedHistory, lastEngineUpdatesPerFrame = engine.timing.lastUpdatesPerFrame, lastEngineDelta = engine.timing.lastDelta; + var deltaMean = _mean(deltaHistory), elapsedMean = _mean(elapsedHistory), engineDeltaMean = _mean(engineDeltaHistory), engineUpdatesMean = _mean(engineUpdatesHistory), engineElapsedMean = _mean(engineElapsedHistory), timestampElapsedMean = _mean(timestampElapsedHistory), rateMean = timestampElapsedMean / deltaMean || 0, neededUpdatesPerFrame = Math.round(deltaMean / lastEngineDelta), fps = 1000 / deltaMean || 0; + var graphHeight = 4, gap = 12, width = 60, height = 34, x = 10, y = 69; + context.fillStyle = "#0e0f19"; + context.fillRect(0, 50, gap * 5 + width * 6 + 22, height); + Render.status(context, x, y, width, graphHeight, deltaHistory.length, Math.round(fps) + " fps", fps / Render._goodFps, function(i) { + return deltaHistory[i] / deltaMean - 1; + }); + Render.status(context, x + gap + width, y, width, graphHeight, engineDeltaHistory.length, lastEngineDelta.toFixed(2) + " dt", Render._goodDelta / lastEngineDelta, function(i) { + return engineDeltaHistory[i] / engineDeltaMean - 1; + }); + Render.status(context, x + (gap + width) * 2, y, width, graphHeight, engineUpdatesHistory.length, lastEngineUpdatesPerFrame + " upf", Math.pow(Common.clamp(engineUpdatesMean / neededUpdatesPerFrame || 1, 0, 1), 4), function(i) { + return engineUpdatesHistory[i] / engineUpdatesMean - 1; + }); + Render.status(context, x + (gap + width) * 3, y, width, graphHeight, engineElapsedHistory.length, engineElapsedMean.toFixed(2) + " ut", 1 - lastEngineUpdatesPerFrame * engineElapsedMean / Render._goodFps, function(i) { + return engineElapsedHistory[i] / engineElapsedMean - 1; + }); + Render.status(context, x + (gap + width) * 4, y, width, graphHeight, elapsedHistory.length, elapsedMean.toFixed(2) + " rt", 1 - elapsedMean / Render._goodFps, function(i) { + return elapsedHistory[i] / elapsedMean - 1; + }); + Render.status(context, x + (gap + width) * 5, y, width, graphHeight, timestampElapsedHistory.length, rateMean.toFixed(2) + " x", rateMean * rateMean * rateMean, function(i) { + return (timestampElapsedHistory[i] / deltaHistory[i] / rateMean || 0) - 1; + }); + }; + Render.status = function(context, x, y, width, height, count, label, indicator, plotY) { + context.strokeStyle = "#888"; + context.fillStyle = "#444"; + context.lineWidth = 1; + context.fillRect(x, y + 7, width, 1); + context.beginPath(); + context.moveTo(x, y + 7 - height * Common.clamp(0.4 * plotY(0), -2, 2)); + for (var i = 0;i < width; i += 1) { + context.lineTo(x + i, y + 7 - (i < count ? height * Common.clamp(0.4 * plotY(i), -2, 2) : 0)); + } + context.stroke(); + context.fillStyle = "hsl(" + Common.clamp(25 + 95 * indicator, 0, 120) + ",100%,60%)"; + context.fillRect(x, y - 7, 4, 4); + context.font = "12px Arial"; + context.textBaseline = "middle"; + context.textAlign = "right"; + context.fillStyle = "#eee"; + context.fillText(label, x + width, y - 5); + }; + Render.constraints = function(constraints, context) { + var c = context; + for (var i = 0;i < constraints.length; i++) { + var constraint = constraints[i]; + if (!constraint.render.visible || !constraint.pointA || !constraint.pointB) + continue; + var { bodyA, bodyB } = constraint, start, end; + if (bodyA) { + start = Vector2.add(bodyA.position, constraint.pointA); + } else { + start = constraint.pointA; + } + if (constraint.render.type === "pin") { + c.beginPath(); + c.arc(start.x, start.y, 3, 0, 2 * Math.PI); + c.closePath(); + } else { + if (bodyB) { + end = Vector2.add(bodyB.position, constraint.pointB); + } else { + end = constraint.pointB; + } + c.beginPath(); + c.moveTo(start.x, start.y); + if (constraint.render.type === "spring") { + var delta = Vector2.sub(end, start), normal = Vector2.perp(Vector2.normalise(delta)), coils = Math.ceil(Common.clamp(constraint.length / 5, 12, 20)), offset; + for (var j = 1;j < coils; j += 1) { + offset = j % 2 === 0 ? 1 : -1; + c.lineTo(start.x + delta.x * (j / coils) + normal.x * offset * 4, start.y + delta.y * (j / coils) + normal.y * offset * 4); + } + } + c.lineTo(end.x, end.y); + } + if (constraint.render.lineWidth) { + c.lineWidth = constraint.render.lineWidth; + c.strokeStyle = constraint.render.strokeStyle; + c.stroke(); + } + if (constraint.render.anchors) { + c.fillStyle = constraint.render.strokeStyle; + c.beginPath(); + c.arc(start.x, start.y, 3, 0, 2 * Math.PI); + c.arc(end.x, end.y, 3, 0, 2 * Math.PI); + c.closePath(); + c.fill(); + } + } + }; + Render.bodies = function(render, bodies, context) { + var c = context, engine = render.engine, options = render.options, showInternalEdges = options.showInternalEdges || !options.wireframes, body, part, i, k; + for (i = 0;i < bodies.length; i++) { + body = bodies[i]; + if (!body.render.visible) + continue; + for (k = body.parts.length > 1 ? 1 : 0;k < body.parts.length; k++) { + part = body.parts[k]; + if (!part.render.visible) + continue; + if (options.showSleeping && body.isSleeping) { + c.globalAlpha = 0.5 * part.render.opacity; + } else if (part.render.opacity !== 1) { + c.globalAlpha = part.render.opacity; + } + if (part.render.sprite && part.render.sprite.texture && !options.wireframes) { + var sprite = part.render.sprite, texture = _getTexture(render, sprite.texture); + c.translate(part.position.x, part.position.y); + c.rotate(part.angle); + c.drawImage(texture, texture.width * -sprite.xOffset * sprite.xScale, texture.height * -sprite.yOffset * sprite.yScale, texture.width * sprite.xScale, texture.height * sprite.yScale); + c.rotate(-part.angle); + c.translate(-part.position.x, -part.position.y); + } else { + if (part.circleRadius) { + c.beginPath(); + c.arc(part.position.x, part.position.y, part.circleRadius, 0, 2 * Math.PI); + } else { + c.beginPath(); + c.moveTo(part.vertices[0].x, part.vertices[0].y); + for (var j = 1;j < part.vertices.length; j++) { + if (!part.vertices[j - 1].isInternal || showInternalEdges) { + c.lineTo(part.vertices[j].x, part.vertices[j].y); + } else { + c.moveTo(part.vertices[j].x, part.vertices[j].y); + } + if (part.vertices[j].isInternal && !showInternalEdges) { + c.moveTo(part.vertices[(j + 1) % part.vertices.length].x, part.vertices[(j + 1) % part.vertices.length].y); + } + } + c.lineTo(part.vertices[0].x, part.vertices[0].y); + c.closePath(); + } + if (!options.wireframes) { + c.fillStyle = part.render.fillStyle; + if (part.render.lineWidth) { + c.lineWidth = part.render.lineWidth; + c.strokeStyle = part.render.strokeStyle; + c.stroke(); + } + c.fill(); + } else { + c.lineWidth = 1; + c.strokeStyle = render.options.wireframeStrokeStyle; + c.stroke(); + } + } + c.globalAlpha = 1; + } + } + }; + Render.bodyWireframes = function(render, bodies, context) { + var c = context, showInternalEdges = render.options.showInternalEdges, body, part, i, j, k; + c.beginPath(); + for (i = 0;i < bodies.length; i++) { + body = bodies[i]; + if (!body.render.visible) + continue; + for (k = body.parts.length > 1 ? 1 : 0;k < body.parts.length; k++) { + part = body.parts[k]; + c.moveTo(part.vertices[0].x, part.vertices[0].y); + for (j = 1;j < part.vertices.length; j++) { + if (!part.vertices[j - 1].isInternal || showInternalEdges) { + c.lineTo(part.vertices[j].x, part.vertices[j].y); + } else { + c.moveTo(part.vertices[j].x, part.vertices[j].y); + } + if (part.vertices[j].isInternal && !showInternalEdges) { + c.moveTo(part.vertices[(j + 1) % part.vertices.length].x, part.vertices[(j + 1) % part.vertices.length].y); + } + } + c.lineTo(part.vertices[0].x, part.vertices[0].y); + } + } + c.lineWidth = 1; + c.strokeStyle = render.options.wireframeStrokeStyle; + c.stroke(); + }; + Render.bodyConvexHulls = function(render, bodies, context) { + var c = context, body, part, i, j, k; + c.beginPath(); + for (i = 0;i < bodies.length; i++) { + body = bodies[i]; + if (!body.render.visible || body.parts.length === 1) + continue; + c.moveTo(body.vertices[0].x, body.vertices[0].y); + for (j = 1;j < body.vertices.length; j++) { + c.lineTo(body.vertices[j].x, body.vertices[j].y); + } + c.lineTo(body.vertices[0].x, body.vertices[0].y); + } + c.lineWidth = 1; + c.strokeStyle = "rgba(255,255,255,0.2)"; + c.stroke(); + }; + Render.vertexNumbers = function(render, bodies, context) { + var c = context, i, j, k; + for (i = 0;i < bodies.length; i++) { + var parts = bodies[i].parts; + for (k = parts.length > 1 ? 1 : 0;k < parts.length; k++) { + var part = parts[k]; + for (j = 0;j < part.vertices.length; j++) { + c.fillStyle = "rgba(255,255,255,0.2)"; + c.fillText(i + "_" + j, part.position.x + (part.vertices[j].x - part.position.x) * 0.8, part.position.y + (part.vertices[j].y - part.position.y) * 0.8); + } + } + } + }; + Render.mousePosition = function(render, mouse, context) { + var c = context; + c.fillStyle = "rgba(255,255,255,0.8)"; + c.fillText(mouse.position.x + " " + mouse.position.y, mouse.position.x + 5, mouse.position.y - 5); + }; + Render.bodyBounds = function(render, bodies, context) { + var c = context, engine = render.engine, options = render.options; + c.beginPath(); + for (var i = 0;i < bodies.length; i++) { + var body = bodies[i]; + if (body.render.visible) { + var parts = bodies[i].parts; + for (var j = parts.length > 1 ? 1 : 0;j < parts.length; j++) { + var part = parts[j]; + c.rect(part.bounds.min.x, part.bounds.min.y, part.bounds.max.x - part.bounds.min.x, part.bounds.max.y - part.bounds.min.y); + } + } + } + if (options.wireframes) { + c.strokeStyle = "rgba(255,255,255,0.08)"; + } else { + c.strokeStyle = "rgba(0,0,0,0.1)"; + } + c.lineWidth = 1; + c.stroke(); + }; + Render.bodyAxes = function(render, bodies, context) { + var c = context, engine = render.engine, options = render.options, part, i, j, k; + c.beginPath(); + for (i = 0;i < bodies.length; i++) { + var body = bodies[i], parts = body.parts; + if (!body.render.visible) + continue; + if (options.showAxes) { + for (j = parts.length > 1 ? 1 : 0;j < parts.length; j++) { + part = parts[j]; + for (k = 0;k < part.axes.length; k++) { + var axis = part.axes[k]; + c.moveTo(part.position.x, part.position.y); + c.lineTo(part.position.x + axis.x * 20, part.position.y + axis.y * 20); + } + } + } else { + for (j = parts.length > 1 ? 1 : 0;j < parts.length; j++) { + part = parts[j]; + for (k = 0;k < part.axes.length; k++) { + c.moveTo(part.position.x, part.position.y); + c.lineTo((part.vertices[0].x + part.vertices[part.vertices.length - 1].x) / 2, (part.vertices[0].y + part.vertices[part.vertices.length - 1].y) / 2); + } + } + } + } + if (options.wireframes) { + c.strokeStyle = "indianred"; + c.lineWidth = 1; + } else { + c.strokeStyle = "rgba(255, 255, 255, 0.4)"; + c.globalCompositeOperation = "overlay"; + c.lineWidth = 2; + } + c.stroke(); + c.globalCompositeOperation = "source-over"; + }; + Render.bodyPositions = function(render, bodies, context) { + var c = context, engine = render.engine, options = render.options, body, part, i, k; + c.beginPath(); + for (i = 0;i < bodies.length; i++) { + body = bodies[i]; + if (!body.render.visible) + continue; + for (k = 0;k < body.parts.length; k++) { + part = body.parts[k]; + c.arc(part.position.x, part.position.y, 3, 0, 2 * Math.PI, false); + c.closePath(); + } + } + if (options.wireframes) { + c.fillStyle = "indianred"; + } else { + c.fillStyle = "rgba(0,0,0,0.5)"; + } + c.fill(); + c.beginPath(); + for (i = 0;i < bodies.length; i++) { + body = bodies[i]; + if (body.render.visible) { + c.arc(body.positionPrev.x, body.positionPrev.y, 2, 0, 2 * Math.PI, false); + c.closePath(); + } + } + c.fillStyle = "rgba(255,165,0,0.8)"; + c.fill(); + }; + Render.bodyVelocity = function(render, bodies, context) { + var c = context; + c.beginPath(); + for (var i = 0;i < bodies.length; i++) { + var body = bodies[i]; + if (!body.render.visible) + continue; + var velocity = Body.getVelocity(body); + c.moveTo(body.position.x, body.position.y); + c.lineTo(body.position.x + velocity.x, body.position.y + velocity.y); + } + c.lineWidth = 3; + c.strokeStyle = "cornflowerblue"; + c.stroke(); + }; + Render.bodyIds = function(render, bodies, context) { + var c = context, i, j; + for (i = 0;i < bodies.length; i++) { + if (!bodies[i].render.visible) + continue; + var parts = bodies[i].parts; + for (j = parts.length > 1 ? 1 : 0;j < parts.length; j++) { + var part = parts[j]; + c.font = "12px Arial"; + c.fillStyle = "rgba(255,255,255,0.5)"; + c.fillText(part.id, part.position.x + 10, part.position.y - 10); + } + } + }; + Render.collisions = function(render, pairs, context) { + var c = context, options = render.options, pair, collision, corrected, bodyA, bodyB, i, j; + c.beginPath(); + for (i = 0;i < pairs.length; i++) { + pair = pairs[i]; + if (!pair.isActive) + continue; + collision = pair.collision; + for (j = 0;j < pair.contactCount; j++) { + var contact = pair.contacts[j], vertex = contact.vertex; + c.rect(vertex.x - 1.5, vertex.y - 1.5, 3.5, 3.5); + } + } + if (options.wireframes) { + c.fillStyle = "rgba(255,255,255,0.7)"; + } else { + c.fillStyle = "orange"; + } + c.fill(); + c.beginPath(); + for (i = 0;i < pairs.length; i++) { + pair = pairs[i]; + if (!pair.isActive) + continue; + collision = pair.collision; + if (pair.contactCount > 0) { + var normalPosX = pair.contacts[0].vertex.x, normalPosY = pair.contacts[0].vertex.y; + if (pair.contactCount === 2) { + normalPosX = (pair.contacts[0].vertex.x + pair.contacts[1].vertex.x) / 2; + normalPosY = (pair.contacts[0].vertex.y + pair.contacts[1].vertex.y) / 2; + } + if (collision.bodyB === collision.supports[0].body || collision.bodyA.isStatic === true) { + c.moveTo(normalPosX - collision.normal.x * 8, normalPosY - collision.normal.y * 8); + } else { + c.moveTo(normalPosX + collision.normal.x * 8, normalPosY + collision.normal.y * 8); + } + c.lineTo(normalPosX, normalPosY); + } + } + if (options.wireframes) { + c.strokeStyle = "rgba(255,165,0,0.7)"; + } else { + c.strokeStyle = "orange"; + } + c.lineWidth = 1; + c.stroke(); + }; + Render.separations = function(render, pairs, context) { + var c = context, options = render.options, pair, collision, corrected, bodyA, bodyB, i, j; + c.beginPath(); + for (i = 0;i < pairs.length; i++) { + pair = pairs[i]; + if (!pair.isActive) + continue; + collision = pair.collision; + bodyA = collision.bodyA; + bodyB = collision.bodyB; + var k = 1; + if (!bodyB.isStatic && !bodyA.isStatic) + k = 0.5; + if (bodyB.isStatic) + k = 0; + c.moveTo(bodyB.position.x, bodyB.position.y); + c.lineTo(bodyB.position.x - collision.penetration.x * k, bodyB.position.y - collision.penetration.y * k); + k = 1; + if (!bodyB.isStatic && !bodyA.isStatic) + k = 0.5; + if (bodyA.isStatic) + k = 0; + c.moveTo(bodyA.position.x, bodyA.position.y); + c.lineTo(bodyA.position.x + collision.penetration.x * k, bodyA.position.y + collision.penetration.y * k); + } + if (options.wireframes) { + c.strokeStyle = "rgba(255,165,0,0.5)"; + } else { + c.strokeStyle = "orange"; + } + c.stroke(); + }; + Render.inspector = function(inspector, context) { + var { engine, selected, render } = inspector, options = render.options, bounds; + if (options.hasBounds) { + var boundsWidth = render.bounds.max.x - render.bounds.min.x, boundsHeight = render.bounds.max.y - render.bounds.min.y, boundsScaleX = boundsWidth / render.options.width, boundsScaleY = boundsHeight / render.options.height; + context.scale(1 / boundsScaleX, 1 / boundsScaleY); + context.translate(-render.bounds.min.x, -render.bounds.min.y); + } + for (var i = 0;i < selected.length; i++) { + var item = selected[i].data; + context.translate(0.5, 0.5); + context.lineWidth = 1; + context.strokeStyle = "rgba(255,165,0,0.9)"; + context.setLineDash([1, 2]); + switch (item.type) { + case "body": + bounds = item.bounds; + context.beginPath(); + context.rect(Math.floor(bounds.min.x - 3), Math.floor(bounds.min.y - 3), Math.floor(bounds.max.x - bounds.min.x + 6), Math.floor(bounds.max.y - bounds.min.y + 6)); + context.closePath(); + context.stroke(); + break; + case "constraint": + var point = item.pointA; + if (item.bodyA) + point = item.pointB; + context.beginPath(); + context.arc(point.x, point.y, 10, 0, 2 * Math.PI); + context.closePath(); + context.stroke(); + break; + } + context.setLineDash([]); + context.translate(-0.5, -0.5); + } + if (inspector.selectStart !== null) { + context.translate(0.5, 0.5); + context.lineWidth = 1; + context.strokeStyle = "rgba(255,165,0,0.6)"; + context.fillStyle = "rgba(255,165,0,0.1)"; + bounds = inspector.selectBounds; + context.beginPath(); + context.rect(Math.floor(bounds.min.x), Math.floor(bounds.min.y), Math.floor(bounds.max.x - bounds.min.x), Math.floor(bounds.max.y - bounds.min.y)); + context.closePath(); + context.stroke(); + context.fill(); + context.translate(-0.5, -0.5); + } + if (options.hasBounds) + context.setTransform(1, 0, 0, 1, 0, 0); + }; + var _updateTiming = function(render, time) { + var { engine, timing } = render, historySize = timing.historySize, timestamp = engine.timing.timestamp; + timing.delta = time - timing.lastTime || Render._goodDelta; + timing.lastTime = time; + timing.timestampElapsed = timestamp - timing.lastTimestamp || 0; + timing.lastTimestamp = timestamp; + timing.deltaHistory.unshift(timing.delta); + timing.deltaHistory.length = Math.min(timing.deltaHistory.length, historySize); + timing.engineDeltaHistory.unshift(engine.timing.lastDelta); + timing.engineDeltaHistory.length = Math.min(timing.engineDeltaHistory.length, historySize); + timing.timestampElapsedHistory.unshift(timing.timestampElapsed); + timing.timestampElapsedHistory.length = Math.min(timing.timestampElapsedHistory.length, historySize); + timing.engineUpdatesHistory.unshift(engine.timing.lastUpdatesPerFrame); + timing.engineUpdatesHistory.length = Math.min(timing.engineUpdatesHistory.length, historySize); + timing.engineElapsedHistory.unshift(engine.timing.lastElapsed); + timing.engineElapsedHistory.length = Math.min(timing.engineElapsedHistory.length, historySize); + timing.elapsedHistory.unshift(timing.lastElapsed); + timing.elapsedHistory.length = Math.min(timing.elapsedHistory.length, historySize); + }; + var _mean = function(values) { + var result = 0; + for (var i = 0;i < values.length; i += 1) { + result += values[i]; + } + return result / values.length || 0; + }; + var _createCanvas = function(width, height) { + var canvas = document.createElement("canvas"); + canvas.width = width; + canvas.height = height; + canvas.oncontextmenu = function() { + return false; + }; + canvas.onselectstart = function() { + return false; + }; + return canvas; + }; + var _getPixelRatio = function(canvas) { + var context = canvas.getContext("2d"), devicePixelRatio = window.devicePixelRatio || 1, backingStorePixelRatio = context.webkitBackingStorePixelRatio || context.mozBackingStorePixelRatio || context.msBackingStorePixelRatio || context.oBackingStorePixelRatio || context.backingStorePixelRatio || 1; + return devicePixelRatio / backingStorePixelRatio; + }; + var _getTexture = function(render, imagePath) { + var image = render.textures[imagePath]; + if (image) + return image; + image = render.textures[imagePath] = new Image; + image.src = imagePath; + return image; + }; + var _applyBackground = function(render, background) { + var cssBackground = background; + if (/(jpg|gif|png)$/.test(background)) + cssBackground = "url(" + background + ")"; + render.canvas.style.background = cssBackground; + render.canvas.style.backgroundSize = "contain"; + render.currentBackground = background; + }; + })(); + }, + function(module2, exports2, __webpack_require__) { + var Runner = {}; + module2.exports = Runner; + var Events = __webpack_require__(5); + var Engine = __webpack_require__(17); + var Common = __webpack_require__(0); + (function() { + Runner._maxFrameDelta = 1000 / 15; + Runner._frameDeltaFallback = 1000 / 60; + Runner._timeBufferMargin = 1.5; + Runner._elapsedNextEstimate = 1; + Runner._smoothingLowerBound = 0.1; + Runner._smoothingUpperBound = 0.9; + Runner.create = function(options) { + var defaults = { + delta: 1000 / 60, + frameDelta: null, + frameDeltaSmoothing: true, + frameDeltaSnapping: true, + frameDeltaHistory: [], + frameDeltaHistorySize: 100, + frameRequestId: null, + timeBuffer: 0, + timeLastTick: null, + maxUpdates: null, + maxFrameTime: 1000 / 30, + lastUpdatesDeferred: 0, + enabled: true + }; + var runner = Common.extend(defaults, options); + runner.fps = 0; + return runner; + }; + Runner.run = function(runner, engine) { + runner.timeBuffer = Runner._frameDeltaFallback; + (function onFrame(time) { + runner.frameRequestId = Runner._onNextFrame(runner, onFrame); + if (time && runner.enabled) { + Runner.tick(runner, engine, time); + } + })(); + return runner; + }; + Runner.tick = function(runner, engine, time) { + var tickStartTime = Common.now(), engineDelta = runner.delta, updateCount = 0; + var frameDelta = time - runner.timeLastTick; + if (!frameDelta || !runner.timeLastTick || frameDelta > Math.max(Runner._maxFrameDelta, runner.maxFrameTime)) { + frameDelta = runner.frameDelta || Runner._frameDeltaFallback; + } + if (runner.frameDeltaSmoothing) { + runner.frameDeltaHistory.push(frameDelta); + runner.frameDeltaHistory = runner.frameDeltaHistory.slice(-runner.frameDeltaHistorySize); + var deltaHistorySorted = runner.frameDeltaHistory.slice(0).sort(); + var deltaHistoryWindow = runner.frameDeltaHistory.slice(deltaHistorySorted.length * Runner._smoothingLowerBound, deltaHistorySorted.length * Runner._smoothingUpperBound); + var frameDeltaSmoothed = _mean(deltaHistoryWindow); + frameDelta = frameDeltaSmoothed || frameDelta; + } + if (runner.frameDeltaSnapping) { + frameDelta = 1000 / Math.round(1000 / frameDelta); + } + runner.frameDelta = frameDelta; + runner.timeLastTick = time; + runner.timeBuffer += runner.frameDelta; + runner.timeBuffer = Common.clamp(runner.timeBuffer, 0, runner.frameDelta + engineDelta * Runner._timeBufferMargin); + runner.lastUpdatesDeferred = 0; + var maxUpdates = runner.maxUpdates || Math.ceil(runner.maxFrameTime / engineDelta); + var event = { + timestamp: engine.timing.timestamp + }; + Events.trigger(runner, "beforeTick", event); + Events.trigger(runner, "tick", event); + var updateStartTime = Common.now(); + while (engineDelta > 0 && runner.timeBuffer >= engineDelta * Runner._timeBufferMargin) { + Events.trigger(runner, "beforeUpdate", event); + Engine.update(engine, engineDelta); + Events.trigger(runner, "afterUpdate", event); + runner.timeBuffer -= engineDelta; + updateCount += 1; + var elapsedTimeTotal = Common.now() - tickStartTime, elapsedTimeUpdates = Common.now() - updateStartTime, elapsedNextEstimate = elapsedTimeTotal + Runner._elapsedNextEstimate * elapsedTimeUpdates / updateCount; + if (updateCount >= maxUpdates || elapsedNextEstimate > runner.maxFrameTime) { + runner.lastUpdatesDeferred = Math.round(Math.max(0, runner.timeBuffer / engineDelta - Runner._timeBufferMargin)); + break; + } + } + engine.timing.lastUpdatesPerFrame = updateCount; + Events.trigger(runner, "afterTick", event); + if (runner.frameDeltaHistory.length >= 100) { + if (runner.lastUpdatesDeferred && Math.round(runner.frameDelta / engineDelta) > maxUpdates) { + Common.warnOnce("Matter.Runner: runner reached runner.maxUpdates, see docs."); + } else if (runner.lastUpdatesDeferred) { + Common.warnOnce("Matter.Runner: runner reached runner.maxFrameTime, see docs."); + } + if (typeof runner.isFixed !== "undefined") { + Common.warnOnce("Matter.Runner: runner.isFixed is now redundant, see docs."); + } + if (runner.deltaMin || runner.deltaMax) { + Common.warnOnce("Matter.Runner: runner.deltaMin and runner.deltaMax were removed, see docs."); + } + if (runner.fps !== 0) { + Common.warnOnce("Matter.Runner: runner.fps was replaced by runner.delta, see docs."); + } + } + }; + Runner.stop = function(runner) { + Runner._cancelNextFrame(runner); + }; + Runner._onNextFrame = function(runner, callback) { + if (typeof window !== "undefined" && window.requestAnimationFrame) { + runner.frameRequestId = window.requestAnimationFrame(callback); + } else { + throw new Error("Matter.Runner: missing required global window.requestAnimationFrame."); + } + return runner.frameRequestId; + }; + Runner._cancelNextFrame = function(runner) { + if (typeof window !== "undefined" && window.cancelAnimationFrame) { + window.cancelAnimationFrame(runner.frameRequestId); + } else { + throw new Error("Matter.Runner: missing required global window.cancelAnimationFrame."); + } + }; + var _mean = function(values) { + var result = 0, valuesLength = values.length; + for (var i = 0;i < valuesLength; i += 1) { + result += values[i]; + } + return result / valuesLength || 0; + }; + })(); + }, + function(module2, exports2, __webpack_require__) { + var SAT = {}; + module2.exports = SAT; + var Collision = __webpack_require__(8); + var Common = __webpack_require__(0); + var deprecated = Common.deprecated; + (function() { + SAT.collides = function(bodyA, bodyB) { + return Collision.collides(bodyA, bodyB); + }; + deprecated(SAT, "collides", "SAT.collides ➤ replaced by Collision.collides"); + })(); + }, + function(module2, exports2, __webpack_require__) { + var Svg = {}; + module2.exports = Svg; + var Bounds = __webpack_require__(1); + var Common = __webpack_require__(0); + (function() { + Svg.pathToVertices = function(path, sampleLength) { + if (typeof window !== "undefined" && !("SVGPathSeg" in window)) { + Common.warn("Svg.pathToVertices: SVGPathSeg not defined, a polyfill is required."); + } + var i, il, total, point, segment, segments, segmentsQueue, lastSegment, lastPoint, segmentIndex, points = [], lx, ly, length = 0, x = 0, y = 0; + sampleLength = sampleLength || 15; + var addPoint = function(px, py, pathSegType) { + var isRelative = pathSegType % 2 === 1 && pathSegType > 1; + if (!lastPoint || px != lastPoint.x || py != lastPoint.y) { + if (lastPoint && isRelative) { + lx = lastPoint.x; + ly = lastPoint.y; + } else { + lx = 0; + ly = 0; + } + var point2 = { + x: lx + px, + y: ly + py + }; + if (isRelative || !lastPoint) { + lastPoint = point2; + } + points.push(point2); + x = lx + px; + y = ly + py; + } + }; + var addSegmentPoint = function(segment2) { + var segType = segment2.pathSegTypeAsLetter.toUpperCase(); + if (segType === "Z") + return; + switch (segType) { + case "M": + case "L": + case "T": + case "C": + case "S": + case "Q": + x = segment2.x; + y = segment2.y; + break; + case "H": + x = segment2.x; + break; + case "V": + y = segment2.y; + break; + } + addPoint(x, y, segment2.pathSegType); + }; + Svg._svgPathToAbsolute(path); + total = path.getTotalLength(); + segments = []; + for (i = 0;i < path.pathSegList.numberOfItems; i += 1) + segments.push(path.pathSegList.getItem(i)); + segmentsQueue = segments.concat(); + while (length < total) { + segmentIndex = path.getPathSegAtLength(length); + segment = segments[segmentIndex]; + if (segment != lastSegment) { + while (segmentsQueue.length && segmentsQueue[0] != segment) + addSegmentPoint(segmentsQueue.shift()); + lastSegment = segment; + } + switch (segment.pathSegTypeAsLetter.toUpperCase()) { + case "C": + case "T": + case "S": + case "Q": + case "A": + point = path.getPointAtLength(length); + addPoint(point.x, point.y, 0); + break; + } + length += sampleLength; + } + for (i = 0, il = segmentsQueue.length;i < il; ++i) + addSegmentPoint(segmentsQueue[i]); + return points; + }; + Svg._svgPathToAbsolute = function(path) { + var x0, y0, x1, y1, x2, y2, segs = path.pathSegList, x = 0, y = 0, len = segs.numberOfItems; + for (var i = 0;i < len; ++i) { + var seg = segs.getItem(i), segType = seg.pathSegTypeAsLetter; + if (/[MLHVCSQTA]/.test(segType)) { + if ("x" in seg) + x = seg.x; + if ("y" in seg) + y = seg.y; + } else { + if ("x1" in seg) + x1 = x + seg.x1; + if ("x2" in seg) + x2 = x + seg.x2; + if ("y1" in seg) + y1 = y + seg.y1; + if ("y2" in seg) + y2 = y + seg.y2; + if ("x" in seg) + x += seg.x; + if ("y" in seg) + y += seg.y; + switch (segType) { + case "m": + segs.replaceItem(path.createSVGPathSegMovetoAbs(x, y), i); + break; + case "l": + segs.replaceItem(path.createSVGPathSegLinetoAbs(x, y), i); + break; + case "h": + segs.replaceItem(path.createSVGPathSegLinetoHorizontalAbs(x), i); + break; + case "v": + segs.replaceItem(path.createSVGPathSegLinetoVerticalAbs(y), i); + break; + case "c": + segs.replaceItem(path.createSVGPathSegCurvetoCubicAbs(x, y, x1, y1, x2, y2), i); + break; + case "s": + segs.replaceItem(path.createSVGPathSegCurvetoCubicSmoothAbs(x, y, x2, y2), i); + break; + case "q": + segs.replaceItem(path.createSVGPathSegCurvetoQuadraticAbs(x, y, x1, y1), i); + break; + case "t": + segs.replaceItem(path.createSVGPathSegCurvetoQuadraticSmoothAbs(x, y), i); + break; + case "a": + segs.replaceItem(path.createSVGPathSegArcAbs(x, y, seg.r1, seg.r2, seg.angle, seg.largeArcFlag, seg.sweepFlag), i); + break; + case "z": + case "Z": + x = x0; + y = y0; + break; + } + } + if (segType == "M" || segType == "m") { + x0 = x; + y0 = y; + } + } + }; + })(); + }, + function(module2, exports2, __webpack_require__) { + var World = {}; + module2.exports = World; + var Composite = __webpack_require__(6); + var Common = __webpack_require__(0); + (function() { + World.create = Composite.create; + World.add = Composite.add; + World.remove = Composite.remove; + World.clear = Composite.clear; + World.addComposite = Composite.addComposite; + World.addBody = Composite.addBody; + World.addConstraint = Composite.addConstraint; + })(); + } + ]); + }); +}); + +// node_modules/@jridgewell/resolve-uri/dist/resolve-uri.umd.js +var require_resolve_uri_umd = __commonJS((exports, module) => { + (function(global2, factory) { + typeof exports === "object" && typeof module !== "undefined" ? module.exports = factory() : typeof define === "function" && define.amd ? define(factory) : (global2 = typeof globalThis !== "undefined" ? globalThis : global2 || self, global2.resolveURI = factory()); + })(exports, function() { + const schemeRegex = /^[\w+.-]+:\/\//; + const urlRegex = /^([\w+.-]+:)\/\/([^@/#?]*@)?([^:/#?]*)(:\d+)?(\/[^#?]*)?(\?[^#]*)?(#.*)?/; + const fileRegex = /^file:(?:\/\/((?![a-z]:)[^/#?]*)?)?(\/?[^#?]*)(\?[^#]*)?(#.*)?/i; + function isAbsoluteUrl(input) { + return schemeRegex.test(input); + } + function isSchemeRelativeUrl(input) { + return input.startsWith("//"); + } + function isAbsolutePath(input) { + return input.startsWith("/"); + } + function isFileUrl(input) { + return input.startsWith("file:"); + } + function isRelative(input) { + return /^[.?#]/.test(input); + } + function parseAbsoluteUrl(input) { + const match = urlRegex.exec(input); + return makeUrl(match[1], match[2] || "", match[3], match[4] || "", match[5] || "/", match[6] || "", match[7] || ""); + } + function parseFileUrl(input) { + const match = fileRegex.exec(input); + const path = match[2]; + return makeUrl("file:", "", match[1] || "", "", isAbsolutePath(path) ? path : "/" + path, match[3] || "", match[4] || ""); + } + function makeUrl(scheme, user, host, port, path, query, hash) { + return { + scheme, + user, + host, + port, + path, + query, + hash, + type: 7 + }; + } + function parseUrl(input) { + if (isSchemeRelativeUrl(input)) { + const url2 = parseAbsoluteUrl("http:" + input); + url2.scheme = ""; + url2.type = 6; + return url2; + } + if (isAbsolutePath(input)) { + const url2 = parseAbsoluteUrl("http://foo.com" + input); + url2.scheme = ""; + url2.host = ""; + url2.type = 5; + return url2; + } + if (isFileUrl(input)) + return parseFileUrl(input); + if (isAbsoluteUrl(input)) + return parseAbsoluteUrl(input); + const url = parseAbsoluteUrl("http://foo.com/" + input); + url.scheme = ""; + url.host = ""; + url.type = input ? input.startsWith("?") ? 3 : input.startsWith("#") ? 2 : 4 : 1; + return url; + } + function stripPathFilename(path) { + if (path.endsWith("/..")) + return path; + const index = path.lastIndexOf("/"); + return path.slice(0, index + 1); + } + function mergePaths(url, base) { + normalizePath(base, base.type); + if (url.path === "/") { + url.path = base.path; + } else { + url.path = stripPathFilename(base.path) + url.path; + } + } + function normalizePath(url, type) { + const rel = type <= 4; + const pieces = url.path.split("/"); + let pointer = 1; + let positive = 0; + let addTrailingSlash = false; + for (let i = 1;i < pieces.length; i++) { + const piece = pieces[i]; + if (!piece) { + addTrailingSlash = true; + continue; + } + addTrailingSlash = false; + if (piece === ".") + continue; + if (piece === "..") { + if (positive) { + addTrailingSlash = true; + positive--; + pointer--; + } else if (rel) { + pieces[pointer++] = piece; + } + continue; + } + pieces[pointer++] = piece; + positive++; + } + let path = ""; + for (let i = 1;i < pointer; i++) { + path += "/" + pieces[i]; + } + if (!path || addTrailingSlash && !path.endsWith("/..")) { + path += "/"; + } + url.path = path; + } + function resolve(input, base) { + if (!input && !base) + return ""; + const url = parseUrl(input); + let inputType = url.type; + if (base && inputType !== 7) { + const baseUrl = parseUrl(base); + const baseType = baseUrl.type; + switch (inputType) { + case 1: + url.hash = baseUrl.hash; + case 2: + url.query = baseUrl.query; + case 3: + case 4: + mergePaths(url, baseUrl); + case 5: + url.user = baseUrl.user; + url.host = baseUrl.host; + url.port = baseUrl.port; + case 6: + url.scheme = baseUrl.scheme; + } + if (baseType > inputType) + inputType = baseType; + } + normalizePath(url, inputType); + const queryHash = url.query + url.hash; + switch (inputType) { + case 2: + case 3: + return queryHash; + case 4: { + const path = url.path.slice(1); + if (!path) + return queryHash || "."; + if (isRelative(base || input) && !isRelative(path)) { + return "./" + path + queryHash; + } + return path + queryHash; + } + case 5: + return url.path + queryHash; + default: + return url.scheme + "//" + url.user + url.host + url.port + url.path + queryHash; + } + } + return resolve; + }); +}); + +// helpers.ts +function generateUID() { + return Math.random().toString(36).substr(2, 9); +} +function applyCamera(context, camera) { + const view = camera.getViewMatrix(); + context.save(); + context.translate(context.canvas.width / 2, context.canvas.height / 2); + context.translate(view.offset.x, view.offset.y); + context.scale(view.scale.x, view.scale.y); +} +function resetCamera(context) { + context.restore(); +} +function isPointInPolygon(pointX, pointY, polygonVertices) { + let inside = false; + for (let i = 0, j = polygonVertices.length - 1;i < polygonVertices.length; j = i++) { + const xi = polygonVertices[i].x, yi = polygonVertices[i].y; + const xj = polygonVertices[j].x, yj = polygonVertices[j].y; + const intersect = yi > pointY !== yj > pointY && pointX < (xj - xi) * (pointY - yi) / (yj - yi) + xi; + if (intersect) + inside = !inside; + } + return inside; +} +function isPointInObject(mouseX, mouseY, child) { + const transform = child.child("Transform"); + if (!transform) { + console.warn(`Part <${child.name}> requires a Transform child.`); + return false; + } + const position = transform.worldPosition; + const boxCollider = child.child("BoxCollider"); + const polygonCollider = child.child("PolygonCollider"); + let width, height, centerX, centerY; + if (boxCollider && boxCollider.start && boxCollider.end) { + const scaledStart = boxCollider.start.multiply(transform.scale); + const scaledEnd = boxCollider.end.multiply(transform.scale); + width = scaledEnd.x - scaledStart.x; + height = scaledEnd.y - scaledStart.y; + const offsetX = (scaledStart.x + scaledEnd.x) / 2; + const offsetY = (scaledStart.y + scaledEnd.y) / 2; + centerX = position.x + offsetX; + centerY = position.y + offsetY; + } else if (polygonCollider) { + width = polygonCollider.realWorldEnd.x - polygonCollider.realWorldStart.x; + height = polygonCollider.realWorldEnd.y - polygonCollider.realWorldStart.y; + centerX = (polygonCollider.realWorldStart.x + polygonCollider.realWorldEnd.x) / 2; + centerY = (polygonCollider.realWorldStart.y + polygonCollider.realWorldEnd.y) / 2; + if (transform.rotation !== 0) { + return isPointInPolygon(mouseX, mouseY, polygonCollider.worldVertices); + } + } else if (child.width && child.height) { + width = child.width * transform.scale.x; + height = child.height * transform.scale.y; + centerX = position.x; + centerY = position.y; + } else { + width = (child?.superficialWidth || 50) * transform.scale.x; + height = (child?.superficialHeight || 50) * transform.scale.y; + centerX = position.x; + centerY = position.y; + } + if (child.type == "Button") { + console.log("BUTTON WH", width, height); + } + if (transform.rotation === 0) { + const left = centerX - width / 2; + const top = centerY - height / 2; + return mouseX >= left && mouseX <= left + width && mouseY >= top && mouseY <= top + height; + } else if (boxCollider) { + const halfWidth = width / 2; + const halfHeight = height / 2; + const corners = [ + { x: -halfWidth, y: -halfHeight }, + { x: halfWidth, y: -halfHeight }, + { x: halfWidth, y: halfHeight }, + { x: -halfWidth, y: halfHeight } + ]; + const cos = Math.cos(transform.rotation); + const sin = Math.sin(transform.rotation); + const rotatedCorners = corners.map((corner) => { + const rotatedX = corner.x * cos - corner.y * sin; + const rotatedY = corner.x * sin + corner.y * cos; + return { + x: centerX + rotatedX, + y: centerY + rotatedY + }; + }); + let inside = false; + for (let i = 0, j = rotatedCorners.length - 1;i < rotatedCorners.length; j = i++) { + if (rotatedCorners[i].y > mouseY !== rotatedCorners[j].y > mouseY && mouseX < (rotatedCorners[j].x - rotatedCorners[i].x) * (mouseY - rotatedCorners[i].y) / (rotatedCorners[j].y - rotatedCorners[i].y) + rotatedCorners[i].x) { + inside = !inside; + } + } + return inside; + } + return false; +} + +// Parts/Part.ts +class Part { + id; + name; + childrenArray = []; + parent; + top; + ready = false; + registrations = {}; + flats = { colliders: [] }; + _layoutWidth = 0; + context; + debugEmoji; + hoverbug; + _superficialWidth = 0; + _superficialHeight = 0; + ties = new Set; + type; + base; + warned = new Set; + _childrenByName = {}; + _childrenByType = {}; + render; + constructor({ name, render } = {}) { + this.id = generateUID(); + this.name = name || "New Object"; + this.type = "Part"; + this.childrenArray = []; + this.parent = undefined; + this.top = undefined; + this.ready = true; + this.base = "Part"; + this.render = typeof render !== "undefined" ? render : true; + this.type = this.constructor.name || "Part"; + this.debugEmoji = "\uD83E\uDDE9"; + } + tie(target, targetAttribute, localAttribute) { + if (!target || !targetAttribute) + return; + if (target.hasOwnProperty(targetAttribute)) { + this.ties.add({ + target, + localAttribute, + targetAttribute + }); + } + } + onclick(event, clicked) { + this.childrenArray.forEach((child) => { + if (typeof child.onclick === "function") { + child.onclick(event, clicked); + } + }); + } + onhover() { + this.childrenArray.forEach((child) => { + if (typeof child.onhover === "function") { + child.onhover(); + } + }); + } + onunhover() { + this.childrenArray.forEach((child) => { + if (typeof child.onunhover === "function") { + child.onunhover(); + } + }); + } + onmousedown(event) { + this.childrenArray.forEach((child) => { + if (typeof child.onmousedown === "function") { + child.onmousedown(event); + } + }); + } + onmouseup(event) { + this.childrenArray.forEach((child) => { + if (typeof child.onmouseup === "function") { + child.onmouseup(event); + } + }); + this.onclick(event, this); + } + sibling(name) { + if (!this.parent) { + return; + } + const sibling = this.parent._childrenByName[name]; + if (!sibling) { + return; + } + return sibling; + } + setSuperficialDimensions(width, height) { + this._superficialHeight = height; + this._superficialWidth = width; + this.childrenArray.forEach((child) => { + if (typeof child.setSuperficialDimensions === "function") { + child.setSuperficialDimensions(width, height); + } + }); + } + onMount(parent) { + this.parent = parent; + } + onRegister(attribute, value) {} + onUnregister(attribute, value, debug) { + if (debug) + console.log(debug, value.name); + switch (attribute) { + case "parent": + this.parent = undefined; + if (this.registrations.layer && this.registrations.layer.flats) { + this.registrations.layer.flats.colliders = this.registrations.layer.flats.colliders.filter((c) => c !== this); + } + break; + case "top": + this.top = undefined; + break; + case "layer": + if (this.registrations.layer && this.registrations.layer.flats) { + this.registrations.layer.flats.colliders = this.registrations.layer.flats.colliders.filter((c) => c.id !== this.id); + } + break; + default: + break; + } + } + onUnmount() {} + onStart() { + this.childrenArray.forEach((child) => { + if (typeof child.onStart === "function") { + child.onStart(); + } + }); + } + addChild(child) { + if (this._childrenByName[child.name]) { + this.top?.warn(`Child with name <${child.name}> already exists in <${this.name}>. Skipping addition. (Child has ID <${child.id}>).`); + return; + } + this.childrenArray.push(child); + if (!this._childrenByType[child.type]) { + this._childrenByType[child.type] = []; + } + this._childrenByType[child.type].push(child); + this._childrenByName[child.name] = child; + if (this.top) { + child.setTop(this.top); + } + for (const [k, v] of Object.entries(this.registrations)) { + child.setAll(k, v); + } + child.onMount(this); + } + addChildren(...children) { + children.forEach((child) => this.addChild(child)); + } + setTop(top) { + this.top = top; + if (this.childrenArray.length > 0) { + this.childrenArray.forEach((child) => { + child.setTop(top); + }); + } + } + attr(attribute, value) { + if (!value) { + return this[attribute]; + } + this[attribute] = value; + return value; + } + preFrame() { + this.childrenArray.forEach((child) => { + child.preFrame(); + }); + } + act(delta) { + if (!this.ready) { + return; + } + this.ties.forEach((tie) => { + if (tie.target && tie.target.hasOwnProperty(tie.targetAttribute)) { + const value = this.attr(tie.localAttribute); + tie.target.attr(tie.targetAttribute, value); + } + }); + if (!this.render) + return; + this.childrenArray.forEach((child) => { + child.act(delta); + }); + } + frameEnd(delta) { + this.childrenArray.forEach((child) => { + child.frameEnd(delta); + }); + } + setAll(attribute, value) { + const current = this.registrations[attribute]; + if (current && current !== value) { + this.onUnregister(attribute, current); + } + if (current !== value) { + this.onRegister(attribute, value); + } + this.registrations[attribute] = value; + this.childrenArray.forEach((child) => { + child.setAll(attribute, value); + }); + } + calculateLayout(spacing = { x: 10, y: 20 }) { + if (!this.childrenArray || this.childrenArray.length === 0) { + this._layoutWidth = 100; + return this._layoutWidth; + } + let totalWidth = 0; + this.childrenArray.forEach((child) => { + if (typeof child.calculateLayout === "function") { + totalWidth += child.calculateLayout(spacing); + } + }); + totalWidth += (this.childrenArray.length - 1) * spacing.x; + this._layoutWidth = totalWidth; + return totalWidth; + } + removeChild(child) { + if (this._childrenByName[child.name]) { + child.childrenArray.forEach((gc) => { + child.removeChild(gc); + }); + delete this._childrenByName[child.name]; + this._childrenByType[child.type] = this._childrenByType[child.type]?.filter((c) => c.id != child.id) || []; + const index = this.childrenArray.indexOf(child); + if (index !== -1) { + this.childrenArray.splice(index, 1); + } + child.parent = undefined; + child.top = undefined; + child.ready = false; + child.onUnregister("parent", this); + child.onUnregister("top", this.top); + child.onUnmount(); + } else { + this.top?.warn(`Child with name <${child.name}> not found in <${this.name}>. Cannot remove. (Child has ID <${child.id}>).`); + } + } + destroy() { + if (this.parent) { + this.parent.removeChild(this); + } + } + debugTreeRender(x, y, spacing = { x: 120, y: 80 }) { + const context = this.context || this.top && this.top.context; + if (!context) + return; + const boxWidth = 100; + const boxHeight = 40; + const label = (this.debugEmoji || "") + this.name || this.id || "Node"; + context.fillStyle = "#fff"; + context.strokeStyle = "#000"; + context.lineWidth = 2; + context.fillRect(x - boxWidth / 2, y, boxWidth, boxHeight); + context.strokeRect(x - boxWidth / 2, y, boxWidth, boxHeight); + context.font = "14px sans-serif"; + context.textAlign = "center"; + context.fillStyle = "#000"; + const spriteRender = this; + const animatedSprite = this; + if (spriteRender && typeof spriteRender.image === "object" && spriteRender.image instanceof Image) { + context.drawImage(spriteRender.image, x - 25 / 2, y + 2, 25, 25); + } else if (animatedSprite && animatedSprite.frames && animatedSprite.currentAnimation && animatedSprite.currentFrameIndex !== undefined) { + const currentFrame = animatedSprite.frames[animatedSprite.currentAnimation]?.[animatedSprite.currentFrameIndex]; + if (currentFrame && currentFrame instanceof Image) { + context.drawImage(currentFrame, x - 25 / 2, y + 2, 25, 25); + } else if (this.debugEmoji) { + context.font = "20px sans-serif"; + context.fillText(this.debugEmoji, x, y + 22); + } + } else if (this.debugEmoji) { + context.font = "20px sans-serif"; + context.fillText(this.debugEmoji, x, y + 22); + } + const labelText = this.name || this.id || "Node"; + context.font = "14px sans-serif"; + context.fillText(labelText, x, y + 38); + if (this.childrenArray && this.childrenArray.length > 0) { + const totalWidth = this.childrenArray.reduce((acc, child) => acc + (child._layoutWidth || 100), 0) + spacing.x * (this.childrenArray.length - 1); + let currentX = x - totalWidth / 2; + this.childrenArray.forEach((child) => { + const childWidth = child._layoutWidth || 100; + const childX = currentX + childWidth / 2; + const childY = y + boxHeight + spacing.y; + context.beginPath(); + context.moveTo(x, y + boxHeight); + context.lineTo(childX, childY); + context.stroke(); + if (typeof child.debugTreeRender === "function") { + child.debugTreeRender(childX, childY, spacing); + } + currentX += childWidth + spacing.x; + }); + } + } + child(name) { + if (this.childrenArray.length === 0) { + return; + } + let child = this._childrenByName[name] || (this._childrenByType[name] ? this._childrenByType[name][0] : undefined); + if (!child) { + return; + } + return child; + } + get superficialHeight() { + return this._superficialHeight || 50; + } + get superficialWidth() { + return this._superficialWidth || 100; + } + set superficialHeight(value) { + this._superficialHeight = value; + this.childrenArray.forEach((child) => { + if (typeof child.setSuperficialDimensions === "function") { + child.setSuperficialDimensions(this._superficialWidth, value); + } + }); + } + set superficialWidth(value) { + this._superficialWidth = value; + this.childrenArray.forEach((child) => { + if (typeof child.setSuperficialDimensions === "function") { + child.setSuperficialDimensions(value, this._superficialHeight); + } + }); + } + emptyChildren() { + this.childrenArray = []; + this._childrenByName = {}; + this._childrenByType = {}; + } + _cloneAndAddChildren(clone, memo) { + this.childrenArray.forEach((child) => { + const clonedChild = child.clone(memo); + clone.addChild(clonedChild); + }); + } + _cloneProperties(clone, memo) { + memo.set(this, clone); + clone.parent = undefined; + clone.top = undefined; + clone._childrenByName = {}; + clone._childrenByType = {}; + clone.childrenArray = []; + this._cloneAndAddChildren(clone, memo); + const clonedTies = new Set; + this.ties.forEach((tie) => { + const clonedTarget = tie.target; + clonedTies.add({ + target: clonedTarget, + localAttribute: tie.localAttribute, + targetAttribute: tie.targetAttribute + }); + }); + clone.ties = clonedTies; + const clonedRegistrations = {}; + for (const regKey in this.registrations) { + const regValue = this.registrations[regKey]; + clonedRegistrations[regKey] = regValue; + } + clone.registrations = clonedRegistrations; + const clonedFlats = { colliders: [] }; + if (this.flats.colliders) { + clonedFlats.colliders = this.flats.colliders.map((collider) => { + return collider; + }); + } + clone.flats = clonedFlats; + clone.id = generateUID(); + clone.name = this.name; + clone.type = this.type; + clone.debugEmoji = this.debugEmoji; + clone.hoverbug = this.hoverbug; + clone._layoutWidth = this._layoutWidth; + clone._superficialWidth = this._superficialWidth; + clone._superficialHeight = this._superficialHeight; + clone.base = this.base; + clone.warned = new Set(this.warned); + return clone; + } + clone(memo = new Map) { + if (memo.has(this)) { + return memo.get(this); + } + const clone = new this.constructor({ name: this.name }); + return this._cloneProperties(clone, memo); + } + isVisible(camera) { + return false; + } + getPart(arg) { + if (typeof arg === "string") { + return this.childrenArray.find((child) => child.type === arg || child.base === arg); + } + return this.childrenArray.find((child) => child instanceof arg); + } + getChildPartRecursive(arg, found = []) { + for (const child of this.childrenArray) { + if (typeof arg !== "string" && child instanceof arg) { + found.push(child); + } else if (child.type === arg || child.base === arg) { + found.push(child); + } + child.getChildPartRecursive(arg, found); + } + return found; + } + siblingOf(...args) { + return this.parent?.childrenArray.find((child) => args.includes(child.type) || args.includes(child.base)); + } +} + +// Parts/Scene.ts +class Scene extends Part { + activeCamera = null; + constructor({ name } = { name: "Scene" }) { + super(); + this.name = name; + this.debugEmoji = "\uD83C\uDFDE️"; + } + clone(memo = new Map) { + if (memo.has(this)) { + return memo.get(this); + } + const clonedScene = new Scene({ + name: this.name + }); + memo.set(this, clonedScene); + this._cloneProperties(clonedScene, memo); + if (this.activeCamera && memo.has(this.activeCamera)) { + clonedScene.activeCamera = memo.get(this.activeCamera); + } else { + clonedScene.activeCamera = null; + } + return clonedScene; + } + removeChild(child) { + child.onUnregister("scene", this); + super.removeChild(child); + } + addChild(part) { + part.setAll("scene", this); + super.addChild(part); + } + addChildren(...parts) { + parts.forEach((part) => this.addChild(part)); + } + act(delta) { + if (!this.top) { + throw new Error(`Act called on Scene <${this.name}> without a top-level parent. Ensure this scene is added to a Game instance before calling act().`); + } + if (!this.top || !(this.top instanceof Game)) { + throw new Error("Scene must be attached to a Game instance."); + } + if (!this.top.canvas) { + throw new Error("Game instance must have a canvas element."); + } + if (this.activeCamera && this.top instanceof Game) { + const camera = this.activeCamera; + applyCamera(this.top.context, camera); + } + super.act(delta); + if (this.top instanceof Game && this.activeCamera) { + resetCamera(this.top.context); + } + } +} + +// Parts/SoundManager.ts +class SoundManagerController { + static instance; + sounds = []; + isGameRunning = false; + constructor() {} + static getInstance() { + if (!SoundManagerController.instance) { + SoundManagerController.instance = new SoundManagerController; + } + return SoundManagerController.instance; + } + registerSound(sound) { + if (!this.sounds.includes(sound)) { + this.sounds.push(sound); + } + } + unregisterSound(sound) { + sound.stop(); + this.sounds = this.sounds.filter((s) => s !== sound); + } + pauseGame() { + this.isGameRunning = false; + this.sounds.forEach((sound) => sound.pause()); + } + resumeGame() { + this.isGameRunning = true; + this.sounds.forEach((sound) => sound.resume()); + } + startGame() { + this.isGameRunning = true; + } + unregisterAllSounds() { + this.sounds.forEach((sound) => sound.stop()); + this.sounds = []; + } + stopGame() { + this.isGameRunning = false; + this.sounds.forEach((sound) => sound.stop()); + SoundManager.unregisterAllSounds(); + } + getIsGameRunning() { + return this.isGameRunning; + } +} +var SoundManager = SoundManagerController.getInstance(); + +// Parts/Game.ts +class Game extends Part { + canvas; + currentScene; + childrenArray; + devmode; + context; + hovering; + scaleFactor = 1; + canvasOffset = { x: 0, y: 0 }; + messageHook; + showFrameStats = "BASIC"; + frameBuffer = []; + maxFrameBufferLength = 60 * 5; + _minFrameTime = Number.POSITIVE_INFINITY; + _maxFrameTime = 0; + _droppedFrames = 0; + _isRunning = false; + _width = 800; + _height = 600; + _isPaused = false; + _animationFrameId; + _lastUpdateTime = 0; + constructor({ name, canvas, devmode = false, width, height, disableAntiAliasing = false, showtoolTips = false, showFrameStats = "BASIC" }) { + super({ name }); + this.type = "Game"; + this.childrenArray = []; + this.showFrameStats = showFrameStats; + this.canvas = typeof canvas === "string" ? document.getElementById(canvas) : canvas; + this.context = this.canvas.getContext("2d"); + this.devmode = devmode; + this.changeCanvasSize(width, height); + this.context.imageSmoothingEnabled = !disableAntiAliasing; + this.debugEmoji = "\uD83C\uDFAE"; + this.top = this; + } + clone(memo = new Map) { + if (memo.has(this)) { + return memo.get(this); + } + const clonedGame = new Game({ + name: this.name, + canvas: document.createElement("canvas"), + devmode: this.devmode, + width: this.width, + height: this.height, + disableAntiAliasing: !this.context.imageSmoothingEnabled + }); + memo.set(this, clonedGame); + this._cloneProperties(clonedGame, memo); + clonedGame.canvas = undefined; + clonedGame.context = undefined; + clonedGame.currentScene = undefined; + clonedGame.hovering = undefined; + clonedGame.scaleFactor = 1; + clonedGame.canvasOffset = { x: 0, y: 0 }; + clonedGame.messageHook = undefined; + clonedGame._isRunning = false; + clonedGame._isPaused = false; + clonedGame._animationFrameId = undefined; + clonedGame._lastUpdateTime = 0; + return clonedGame; + } + changeCanvasSize(width, height) { + this.canvas.width = width; + this.canvas.height = height; + this.width = width; + this.height = height; + } + set width(width) { + this._width = width; + this.canvas.width = width; + } + set height(height) { + this._height = height; + this.canvas.height = height; + } + get width() { + return this._width; + } + get height() { + return this._height; + } + addChild(scene) { + this.currentScene = this.currentScene || scene; + scene.setTop(this); + super.addChild(scene); + } + addChildren(...scenes) { + scenes.forEach((scene) => this.addChild(scene)); + } + start(starterScene) { + if (typeof starterScene === "string") { + const scene = this.child(starterScene); + if (scene instanceof Scene) { + this.currentScene = scene; + } else { + this.currentScene = this.childrenArray.find((s) => s.name === starterScene); + if (!this.currentScene) { + throw new Error(`Scene with name "${starterScene}" not found in game <${this.name}> (Does not exist as ID either. Please check your references).`); + } + } + } else if (starterScene instanceof Scene) { + this.currentScene = starterScene; + } else { + this.warn("No valid scene provided to start the game. Using the first scene found. Check console for more details"); + this.currentScene = this.childrenArray[0]; + if (!this.currentScene) { + throw new Error("No scenes available to start the game."); + } + } + this._isRunning = true; + this._isPaused = false; + this._lastUpdateTime = performance.now(); + this.onStart(); + SoundManager.startGame(); + this.loop(); + } + loop() { + if (!this._isRunning) + return; + if (!this._isPaused && this.currentScene) { + const now = performance.now(); + const delta = now - this._lastUpdateTime; + this._lastUpdateTime = now; + this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); + if (this.devmode) { + this.currentScene.calculateLayout(); + this.context.save(); + this.context.setTransform(1, 0, 0, 1, 0, 0); + this.currentScene.debugTreeRender(this.canvas.width / 2, 10, { x: 10, y: 40 }); + this.context.restore(); + } + this.currentScene.preFrame(); + this.currentScene.act(delta); + this.currentScene.frameEnd(delta); + this.frameBuffer.push(delta); + if (this.frameBuffer.length > this.maxFrameBufferLength) { + this.frameBuffer.shift(); + } + this._minFrameTime = Math.min(this._minFrameTime, delta); + this._maxFrameTime = Math.max(this._maxFrameTime, delta); + if (delta > 32) + this._droppedFrames++; + this.renderFrameStats(); + } + if (this._isRunning) { + this._animationFrameId = window.requestAnimationFrame(this.loop.bind(this)); + } + } + getColliderCount(activeOnly = false) { + const layers = this.currentScene?.childrenArray || []; + let c = 0; + for (const layer of layers) { + if (!activeOnly) { + const colliders = layer.flats.colliders.length; + c += colliders; + } else { + const colliders = layer.flats.colliders.filter((col) => col.active).length; + c += colliders; + } + } + return c; + } + renderFrameStats() { + if (!this.showFrameStats) + return; + const FADE_BACKGROUND = 0.5; + const avgDelta = this.frameBuffer.reduce((a, b) => a + b, 0) / this.frameBuffer.length; + const avgFPS = 1000 / avgDelta; + const sorted = [...this.frameBuffer].sort((a, b) => a - b); + const p95 = sorted[Math.floor(sorted.length * 0.95)]; + const p99 = sorted[Math.floor(sorted.length * 0.99)]; + const minFrameTime = sorted[0]; + const maxFrameTime = sorted[sorted.length - 1]; + let lines = []; + const levels = ["BASIC", "EXTENDED", "ADVANCED", "PERFORMANCE_HUD"]; + const levelIndex = levels.indexOf(this.showFrameStats); + if (levelIndex >= 0) + lines.push(`FPS: ${avgFPS.toFixed(2)}`); + if (levelIndex >= 1) + lines.push(`Frame Time: ${avgDelta.toFixed(2)} ms`); + if (levelIndex >= 2) { + lines.push(`Min: ${minFrameTime.toFixed(2)} (${this._minFrameTime.toFixed(2)} AT) ms`); + lines.push(`Max: ${maxFrameTime.toFixed(2)} (${this._maxFrameTime.toFixed(2)} AT) ms`); + } + if (levelIndex >= 3) { + lines.push(`p95 Frame: ${p95.toFixed(2)} ms`); + lines.push(`p99 Frame: ${p99.toFixed(2)} ms`); + const droppedPct = this._droppedFrames / (this.frameBuffer.length || 1) * 100; + lines.push(`Dropped Frames: ${droppedPct.toFixed(1)}%`); + const perfMem = performance.memory; + if (perfMem) { + const usedMB = (perfMem.usedJSHeapSize / 1048576).toFixed(1); + const totalMB = (perfMem.totalJSHeapSize / 1048576).toFixed(1); + lines.push(`Heap: ${usedMB} MB / ${totalMB} MB`); + } + if (this.currentScene) { + lines.push(`Colliders: ${this.getColliderCount()}`); + lines.push(`Active colliders: ${this.getColliderCount(true)}`); + } + } + const fontSize = 12; + const lineHeight = 20; + const padding = 8; + this.context.font = `${fontSize}px Arial`; + let maxWidth = 0; + for (const line of lines) { + const width = this.context.measureText(line).width; + if (width > maxWidth) + maxWidth = width; + } + let boxHeight = lines.length * lineHeight + padding * 2; + let boxWidth = maxWidth + padding * 2; + let boxX = 6; + let boxY = 6; + this.context.globalAlpha = FADE_BACKGROUND; + this.context.fillStyle = "#000"; + this.context.fillRect(boxX, boxY, boxWidth, boxHeight); + this.context.globalAlpha = 1; + this.context.fillStyle = "white"; + let y = boxY + padding + fontSize; + for (const line of lines) { + this.context.fillText(line, boxX + padding, y); + y += lineHeight; + } + if (levelIndex >= 3) { + const chartWidth = 200; + const chartHeight = 80; + const chartX = boxX + padding; + const chartY = boxY + boxHeight + 10; + const minFrameTimeChart = Math.min(...this.frameBuffer); + const maxFrameTimeChart = Math.max(...this.frameBuffer); + const margin = Math.max(2, (maxFrameTimeChart - minFrameTimeChart) * 0.2); + const chartMin = Math.max(0, minFrameTimeChart - margin); + const chartMax = maxFrameTimeChart + margin; + const range = Math.max(1, chartMax - chartMin); + this.context.globalAlpha = FADE_BACKGROUND; + this.context.fillStyle = "#000"; + this.context.fillRect(chartX - padding, chartY - padding, chartWidth + padding * 2, chartHeight + padding * 2); + this.context.globalAlpha = 1; + this.context.strokeStyle = "white"; + this.context.beginPath(); + this.frameBuffer.forEach((frameTime, index) => { + const x = chartX + index / (this.maxFrameBufferLength - 1) * chartWidth; + const yVal = chartY + chartHeight - (frameTime - chartMin) / range * chartHeight; + if (index === 0) { + this.context.moveTo(x, yVal); + } else { + this.context.lineTo(x, yVal); + } + }); + this.context.stroke(); + } + } + pause() { + this._isPaused = true; + this.debug("Game paused"); + SoundManager.pauseGame(); + } + resume() { + this.debug("Game resumed"); + this._isPaused = false; + SoundManager.resumeGame(); + } + stop() { + this._isRunning = false; + this._isPaused = false; + if (this._animationFrameId) { + window.cancelAnimationFrame(this._animationFrameId); + this._animationFrameId = undefined; + } + SoundManager.stopGame(); + this.childrenArray.forEach((scene) => { + scene.destroy(); + }); + } + get isRunning() { + return this._isRunning; + } + get isPaused() { + return this._isPaused; + } + act(purposeful = false) { + if (!this.warned.has("ActUsage") && !purposeful) { + const seen = this.warn(`Act called on Game <${this.name}>. Use start() to begin the game loop. Calling act() directly will run 1 frame of the current scene. This message will appear only once.`); + if (seen) + this.warned.add("ActUsage"); + } + if (this.currentScene) { + this.currentScene.act(0); + } else { + this.warn(`No current scene set in <${this.name}>, and no available scenes to run as the current scene in game <${this.name}>. Please ensure you have added scenes and/or set a current scene before calling act().`); + } + } + setScene(scene) { + if (typeof scene === "string") { + const foundScene = this.childrenArray.find((s) => s.name === scene || s.id === scene); + if (foundScene) { + this.currentScene = foundScene; + } else { + throw new Error(`Scene with name or ID "${scene}" not found in game <${this.name}>. Please ensure the scene exists and is added to the game.`); + } + } else if (scene instanceof Scene) { + this.currentScene = scene; + } else { + console.error("Set unknown scene type- neither string nor Scene instance"); + let json; + try { + json = JSON.stringify(scene); + } catch (error) { + json = ``; + } + this.debug(`Trying to set scene to unknown type- neither string nor Scene instance. Got ${typeof scene} - ${json}`); + } + } + warn(...args) { + if (this.messageHook && typeof this.messageHook === "function") { + this.messageHook("warn", ...args); + return true; + } else { + console.warn(`[${this.name}] - WARN`, ...args); + return false; + } + } + error(...args) { + if (this.messageHook && typeof this.messageHook === "function") { + this.messageHook("error", ...args); + return true; + } else { + console.error(`[${this.name}] - ERROR`, ...args); + return false; + } + } + debug(...args) { + if (this.messageHook && typeof this.messageHook === "function") { + this.messageHook("debug", ...args); + return true; + } else { + console.debug(`[${this.name}]`, ...args); + return false; + } + } +} +// Math/SpatialGrid.ts +class SpatialGrid { + cells; + cellSize; + constructor(cellSize) { + this.cells = new Map; + this.cellSize = cellSize; + } + getKey(x, y) { + return `${Math.floor(x / this.cellSize)}_${Math.floor(y / this.cellSize)}`; + } + clear() { + this.cells.clear(); + } + insert(collider) { + const start = collider.realWorldStart; + const end = collider.realWorldEnd; + const startX = Math.floor(start.x / this.cellSize); + const startY = Math.floor(start.y / this.cellSize); + const endX = Math.floor(end.x / this.cellSize); + const endY = Math.floor(end.y / this.cellSize); + for (let x = startX;x <= endX; x++) { + for (let y = startY;y <= endY; y++) { + const key = `${x}_${y}`; + if (!this.cells.has(key)) { + this.cells.set(key, []); + } + this.cells.get(key).push(collider); + } + } + } + query(collider) { + const candidates = new Set; + const start = collider.realWorldStart; + const end = collider.realWorldEnd; + const startX = Math.floor(start.x / this.cellSize); + const startY = Math.floor(start.y / this.cellSize); + const endX = Math.floor(end.x / this.cellSize); + const endY = Math.floor(end.y / this.cellSize); + for (let x = startX;x <= endX; x++) { + for (let y = startY;y <= endY; y++) { + const key = `${x}_${y}`; + if (this.cells.has(key)) { + for (const other of this.cells.get(key)) { + candidates.add(other); + } + } + } + } + return Array.from(candidates); + } +} + +// Parts/Layer.ts +class Layer extends Part { + spatialGrid; + constructor({ name, spatialGridDefinition }) { + super({ name }); + this.type = "Layer"; + this.id = generateUID(); + this.debugEmoji = "\uD83D\uDDC2️"; + this.spatialGrid = new SpatialGrid(spatialGridDefinition || 100); + } + addChild(part) { + part.setAll("layer", this); + super.addChild(part); + } + addChildren(...parts) { + parts.forEach((part) => this.addChild(part)); + } + removeChild(part) { + part.onUnregister("layer", this); + super.removeChild(part); + } + act(delta) { + if (!this.ready) { + return; + } + this.spatialGrid.clear(); + const colliders = this.flats.colliders; + for (const collider of colliders) { + if (collider.active) { + this.spatialGrid.insert(collider); + } + } + this.ties.forEach((tie) => { + if (tie.target && tie.target.hasOwnProperty(tie.targetAttribute)) { + const value = this.attr(tie.localAttribute); + tie.target.attr(tie.targetAttribute, value); + } + }); + this.childrenArray.forEach((child) => { + child.act(delta); + }); + } +} +// Parts/GameObject.ts +class GameObject extends Part { + layer; + constructor({ name, render = true }) { + super({ name, render: !!render }); + this.type = "GameObject"; + this.debugEmoji = "\uD83D\uDD79️"; + } +} +// Math/Vector.ts +class Vector { + x; + y; + constructor(x, y) { + this.x = x; + this.y = y; + } + distance(other) { + return Math.sqrt((this.x - other.x) ** 2 + (this.y - other.y) ** 2); + } + add(other) { + if (other instanceof Vector) { + return new Vector(this.x + other.x, this.y + other.y); + } + return new Vector(this.x + other, this.y + other); + } + toObject() { + return { x: this.x, y: this.y }; + } + toArray() { + return [this.x, this.y]; + } + subtract(other) { + if (other instanceof Vector) { + return new Vector(this.x - other.x, this.y - other.y); + } + return new Vector(this.x - other, this.y - other); + } + multiply(other) { + if (other instanceof Vector) { + return new Vector(this.x * other.x, this.y * other.y); + } + return new Vector(this.x * other, this.y * other); + } + divide(other) { + if (other instanceof Vector) { + if (other.x === 0 || other.y === 0) + throw new Error("Cannot divide by zero"); + return new Vector(this.x / other.x, this.y / other.y); + } + if (other === 0) + throw new Error("Cannot divide by zero"); + return new Vector(this.x / other, this.y / other); + } + length() { + return Math.sqrt(this.x * this.x + this.y * this.y); + } + magnitude() { + return this.length(); + } + equals(other) { + return this.x === other.x && this.y === other.y; + } + toString() { + return `[${this.x}, ${this.y}]`; + } + normalize() { + const len = this.length(); + if (len === 0) { + return new Vector(0, 0); + } + return new Vector(this.x / len, this.y / len); + } + dot(other) { + return this.x * other.x + this.y * other.y; + } + clone() { + return new Vector(this.x, this.y); + } + set(...args) { + if (args.length === 1 && args[0] instanceof Vector) { + this.x = args[0].x; + this.y = args[0].y; + } else if (args.length === 2) { + this.x = args[0]; + this.y = args[1]; + } else { + throw new Error("Invalid arguments for set method"); + } + return this; + } + addInPlace(other) { + this.x += other.x; + this.y += other.y; + return this; + } + static From(scalar) { + if (typeof scalar === "number") { + return new Vector(scalar, scalar); + } else { + return new Vector(scalar.x, scalar.y); + } + } +} + +// Parts/Camera.ts +class Camera extends Part { + zoom; + constructor({ name, zoom }) { + super({ name }); + this.zoom = Vector.From(1); + this.debugEmoji = "\uD83D\uDCF7"; + this.type = "Camera"; + } + onMount(parent) { + super.onMount(parent); + let currentParent = this.parent; + while (currentParent) { + if (currentParent instanceof Scene) { + currentParent.activeCamera = this; + return; + } + currentParent = currentParent.parent; + } + throw new Error("Camera must be mounted to a Scene (or a child of a Scene) to be registered."); + } + setActive() { + if (this.registrations.layer && this.registrations.layer.parent) { + this.registrations.layer.parent.activeCamera = this; + } else { + throw new Error("Camera must be mounted to a Layer with a parent Scene to be set active."); + } + } + getViewMatrix() { + const transform = this.child("Transform"); + if (!transform) { + if (!this.warned.has("TransformMissing")) { + const seen = this.top?.warn(`Camera <${this.name}> (${this.id}) does not have a Transform component. View matrix will not be calculated.`); + if (seen) + this.warned.add("TransformMissing"); + } + return { offset: Vector.From(0), scale: this.zoom }; + } + return { + offset: transform.worldPosition.multiply(-1), + scale: this.zoom + }; + } + act(delta) { + super.act(delta); + const transform = this.child("Transform"); + if (transform) { + this.zoom = transform.scale; + } else { + if (!this.warned.has("TransformMissing")) { + this.top?.warn(`Camera <${this.name}> (${this.id}) does not have a Transform component. Camera zoom will not be updated.`) && this.warned.add("TransformMissing"); + } + } + } +} +// Parts/Input.ts +class Input extends Part { + key; + keyup; + mousemove; + click; + downkeys = new Set; + currentMousePos = { x: 0, y: 0 }; + lastClickPos = null; + initialized; + mousemoveDef; + clickDef; + mousedownDef; + mouseupDef; + keydownDef; + keyupDef; + clone(memo = new Map) { + if (memo.has(this)) { + return memo.get(this); + } + const clonedInput = new Input({ + key: this.key || (() => {}), + keyup: this.keyup || (() => {}), + mousemove: this.mousemove || (() => {}), + click: this.click || (() => {}) + }); + memo.set(this, clonedInput); + this._cloneProperties(clonedInput, memo); + clonedInput.downkeys = new Set; + clonedInput.currentMousePos = { x: 0, y: 0 }; + clonedInput.lastClickPos = null; + clonedInput.initialized = false; + clonedInput.mousemoveDef = undefined; + clonedInput.clickDef = undefined; + clonedInput.mousedownDef = undefined; + clonedInput.mouseupDef = undefined; + clonedInput.keydownDef = undefined; + clonedInput.keyupDef = undefined; + return clonedInput; + } + constructor({ + key, + keyup, + mousemove, + click + }) { + super({ name: "Input" }); + this.debugEmoji = "⌨️"; + this.key = key; + this.keyup = keyup; + this.mousemove = mousemove; + this.click = click; + this.initialized = false; + this.type = "Input"; + } + initialize(canvas) { + this.mousemoveDef = (event) => { + if (event.target !== canvas) + return; + const game = this.top; + if (!game || !game.currentScene || game.currentScene !== this.parent || !game.currentScene?.activeCamera) { + return; + } + const rect = canvas.getBoundingClientRect(); + const gameCanvas = game.canvas; + const mouseX = (event.clientX - rect.left) * (gameCanvas.width / rect.width); + const mouseY = (event.clientY - rect.top) * (gameCanvas.height / rect.height); + const camera = game.currentScene?.activeCamera; + let finalX = mouseX; + let finalY = mouseY; + if (camera) { + const view = camera.getViewMatrix(); + const transform = camera.child("Transform"); + if (!transform) { + if (!this.warned.has("TransformMissing")) { + this.top?.warn("Camera does not have a Transform child.") && this.warned.add("TransformMissing"); + } + return; + } + finalX = (mouseX - game.canvas.width / 2) / view.scale.x + transform.worldPosition.x; + finalY = (mouseY - game.canvas.height / 2) / view.scale.y + transform.worldPosition.y; + } + this.currentMousePos = { x: finalX, y: finalY }; + }; + this.clickDef = (event) => { + const game = this.top; + if (!game || !game.currentScene || game.currentScene !== this.parent || !game.currentScene?.activeCamera) { + return; + } + const rect = canvas.getBoundingClientRect(); + const gameCanvas = game.canvas; + const mouseX = (event.clientX - rect.left) * (gameCanvas.width / rect.width); + const mouseY = (event.clientY - rect.top) * (gameCanvas.height / rect.height); + const camera = game.currentScene?.activeCamera; + let finalX = mouseX; + let finalY = mouseY; + if (camera) { + const view = camera.getViewMatrix(); + const transform = camera.child("Transform"); + if (!transform) { + this.top?.warn("Camera does not have a Transform child."); + return; + } + finalX = (mouseX - game.canvas.width / 2) / view.scale.x + transform.worldPosition.x; + finalY = (mouseY - game.canvas.height / 2) / view.scale.y + transform.worldPosition.y; + } + this.lastClickPos = { x: finalX, y: finalY }; + }; + this.mousedownDef = (event) => { + const game = this.top; + if (game.hovering) { + game.hovering.onmousedown(event); + } + }; + this.mouseupDef = (event) => { + const game = this.top; + if (game.hovering) { + game.hovering.onmouseup(event); + } + }; + this.keydownDef = (event) => { + this.downkeys.add(event.key); + }; + this.keyupDef = (event) => { + this.downkeys.delete(event.key); + if (typeof this.keyup == "function") { + this.keyup(event); + } + }; + canvas.addEventListener("mousemove", this.mousemoveDef); + canvas.addEventListener("click", this.clickDef); + canvas.addEventListener("mousedown", this.mousedownDef); + canvas.addEventListener("mouseup", this.mouseupDef); + document.addEventListener("keydown", this.keydownDef); + document.addEventListener("keyup", this.keyupDef); + this.initialized = true; + } + destroy() { + super.destroy(); + const canvas = this.top?.canvas; + if (canvas) { + canvas.removeEventListener("mousemove", this.mousemoveDef); + canvas.removeEventListener("click", this.clickDef); + canvas.removeEventListener("mousedown", this.mousedownDef); + canvas.removeEventListener("mouseup", this.mouseupDef); + document.removeEventListener("keydown", this.keydownDef); + document.removeEventListener("keyup", this.keyupDef); + } + } + act(delta) { + super.act(delta); + if (!this.initialized) { + if (!this.top || !(this.top instanceof Game)) { + throw new Error("Input must be attached to a Game instance."); + } + if (!this.top.canvas) { + throw new Error("Game instance must have a canvas element."); + } + this.initialize(this.top.canvas); + } + const game = this.top; + if (!game || !game.currentScene || game.currentScene !== this.parent) { + return; + } + if (this.currentMousePos) { + const childrenFlat = game.currentScene.childrenArray.flatMap((child) => child.childrenArray); + childrenFlat.sort((a, b) => { + const layers = game.currentScene?.childrenArray.filter((l) => l instanceof Layer) || []; + layers.sort((a2, b2) => { + const aIndex = game.currentScene?.childrenArray.indexOf(a2) || 0; + const bIndex = game.currentScene?.childrenArray.indexOf(b2) || 0; + return aIndex - bIndex; + }); + return layers.indexOf(a) - layers.indexOf(b); + }); + const hovered = childrenFlat.find((child) => { + if (child.child("Transform")) { + return isPointInObject(this.currentMousePos.x, this.currentMousePos.y, child); + } + return false; + }); + if (game.hovering && game.hovering !== hovered) { + game.hovering.onunhover(); + game.hovering = undefined; + } + if (hovered && game.hovering !== hovered) { + game.hovering = hovered; + hovered.onhover(); + } + } + if (this.lastClickPos) { + const childrenFlat = game.currentScene.childrenArray.flatMap((child) => child.childrenArray); + childrenFlat.sort((a, b) => { + const layers = game.currentScene?.childrenArray.filter((l) => l instanceof Layer) || []; + layers.sort((a2, b2) => { + const aIndex = game.currentScene?.childrenArray.indexOf(a2) || 0; + const bIndex = game.currentScene?.childrenArray.indexOf(b2) || 0; + return aIndex - bIndex; + }); + return layers.indexOf(a) - layers.indexOf(b); + }); + const clicked = childrenFlat.find((child) => { + if (child.child("Transform")) { + return isPointInObject(this.lastClickPos.x, this.lastClickPos.y, child); + } + return false; + }); + if (clicked) { + if (typeof this.click == "function") { + this.click(new MouseEvent("click"), clicked); + } + } + this.lastClickPos = null; + } + this.downkeys.forEach((key) => { + if (typeof this.key == "function") { + this.key(new KeyboardEvent("keydown", { key })); + } + }); + } +} +// Parts/Children/Renderer.ts +class Renderer extends Part { + width; + height; + facing = new Vector(1, 1); + disableAntiAliasing = false; + constructor({ width, height, disableAntiAliasing }) { + super({ name: "Renderer" }); + this.width = width; + this.height = height; + this.disableAntiAliasing = disableAntiAliasing || false; + this.debugEmoji = "\uD83C\uDFA8"; + this.type = "Renderer"; + this.base = "Rednerer"; + } + face(direction) { + if (direction.x !== -1 && direction.x !== 1 && direction.y !== -1 && direction.y !== 1) { + throw new Error("Direction must be vector with -1 or 1 in x and y axis"); + } + this.facing = direction; + } +} + +// Parts/Children/AnimatedSprite.ts +class AnimatedSprite extends Renderer { + spritesheet; + spritesheetData; + loadedSheet; + frames = {}; + currentFrameIndex = 0; + width; + height; + bouncing = false; + currentAnimation = "default"; + disableAntiAliasing = false; + webEngine = false; + onAnimationComplete; + spritesheetImage; + startLoop; + startBouncing; + lastFrameTime = performance.now(); + constructor({ spritesheet, spritesheetImage, width, height, startingAnimation, disableAntiAliasing = false, onAnimationComplete, webEngine = false, bounce = false, loop = true }) { + super({ width, height }); + this.name = "AnimatedSprite"; + this.debugEmoji = "\uD83C\uDF9E️"; + this.spritesheet = spritesheet; + this.width = width; + this.height = height; + this.ready = false; + this.startLoop = loop; + this.startBouncing = bounce; + this.spritesheetImage = spritesheetImage; + this.currentAnimation = startingAnimation || "default"; + this.disableAntiAliasing = disableAntiAliasing; + this.onAnimationComplete = onAnimationComplete; + this.webEngine = webEngine; + this.type = "AnimatedSprite"; + this.base = "Renderer"; + } + clone(memo = new Map) { + if (memo.has(this)) { + return memo.get(this); + } + const clonedSprite = new AnimatedSprite({ + spritesheet: this.spritesheet, + spritesheetImage: this.spritesheetImage, + width: this.width, + height: this.height, + startingAnimation: this.currentAnimation, + disableAntiAliasing: this.disableAntiAliasing, + onAnimationComplete: this.onAnimationComplete, + webEngine: this.webEngine, + bounce: this.startBouncing, + loop: this.startLoop + }); + memo.set(this, clonedSprite); + this._cloneProperties(clonedSprite, memo); + clonedSprite.ready = false; + clonedSprite.currentFrameIndex = 0; + clonedSprite.loadedSheet = undefined; + clonedSprite.frames = {}; + clonedSprite.spritesheetData = undefined; + clonedSprite.lastFrameTime = performance.now(); + return clonedSprite; + } + destroy() { + super.destroy(); + if (this.loadedSheet) { + this.loadedSheet.src = ""; + this.loadedSheet = undefined; + } + this.frames = {}; + this.spritesheetData = undefined; + this.spritesheet = ""; + } + async onMount(parent) { + super.onMount(parent); + parent.setSuperficialDimensions(this.width, this.height); + let spritesheetData; + if (!this.spritesheet) { + return; + } + if (this.spritesheet.startsWith("data:application/json")) { + spritesheetData = JSON.parse(atob(this.spritesheet.split(",")[1])); + } else { + const response = await fetch(this.spritesheet); + if (!response.ok) { + throw new Error(`Failed to load spritesheet: ${response.statusText}`); + } + spritesheetData = await response.json(); + } + if (!spritesheetData.frames || !Array.isArray(spritesheetData.frames)) { + throw new Error("Invalid spritesheet format: 'frames' array is missing or not an array."); + } + if (!spritesheetData.meta || !spritesheetData.meta.image) { + throw new Error("Invalid spritesheet format: 'meta.image' is missing."); + } + if (!spritesheetData.meta.size || typeof spritesheetData.meta.size.w !== "number" || typeof spritesheetData.meta.size.h !== "number") { + throw new Error("Invalid spritesheet format: 'meta.size' is missing or invalid."); + } + if (!spritesheetData.meta.animations || typeof spritesheetData.meta.animations !== "object") { + throw new Error("Invalid spritesheet format: 'meta.animations' is missing or not an object."); + } + const image = new Image; + if (this.spritesheetImage) { + image.src = this.spritesheetImage; + } else { + if (!this.webEngine) { + const relativeToSpritesheet = this.spritesheet.startsWith("data:") ? "" : this.spritesheet.split("/").slice(0, -1).join("/"); + const path = spritesheetData.meta.image.startsWith("http") ? spritesheetData.meta.image : new URL(relativeToSpritesheet + "/" + spritesheetData.meta.image, window.location.href).href; + image.src = path; + } + } + image.onerror = (err) => { + this.top?.error(`Failed to load spritesheet image <${spritesheetData.meta.image}>:`, err); + console.error("Failed to load spritesheet image", err); + this.ready = false; + }; + this.spritesheetData = spritesheetData; + this.frames = Object.fromEntries(Object.keys(spritesheetData.meta.animations).map((animationName) => [animationName, Array(spritesheetData.meta.animations[animationName].frames.length).fill(null)])); + await new Promise((resolve, reject) => { + image.onload = () => { + this.loadedSheet = image; + resolve(); + }; + image.onerror = (err) => { + this.top?.error(`Failed to load spritesheet image <${spritesheetData.meta.image}>:`, err); + this.ready = false; + reject(err); + }; + }); + const frameLoadPromises = []; + for (const animation of Object.keys(this.frames)) { + this.frames[animation] = new Array(this.frames[animation].length); + for (let i = 0;i < this.frames[animation].length; i++) { + const frameIndex = this.spritesheetData?.frames.findIndex((frame2) => frame2.filename === this.spritesheetData.meta.animations[animation].frames[i]); + if (frameIndex === -1) { + throw new Error(`Frame '${this.spritesheetData.meta.animations[animation].frames[i]}' does not exist in spritesheet for animated sprite <${this.name}> attached to ${this.parent?.name}.`); + } + const frame = this.frame(frameIndex); + if (frame) { + this.frames[animation][i] = frame; + frameLoadPromises.push(new Promise((resolve, reject) => { + frame.onload = () => { + resolve(); + }; + frame.onerror = (err) => { + this.top?.error(`Failed to load frame at index ${i} for animated sprite <${this.name}>:`, err); + reject(err); + }; + })); + } else { + throw new Error(`Failed to create frame at index ${i} for animated sprite <${this.name}> attached to ${this.parent?.name}.`); + } + } + } + await Promise.all(frameLoadPromises); + if (this.currentAnimation === "default" && this.spritesheetData.meta.startingAnimation) { + this.currentAnimation = this.spritesheetData.meta.startingAnimation; + this.setAnimation(this.currentAnimation, { loop: this.startLoop, bounce: this.startBouncing }); + } else if (this.currentAnimation === "default" && Object.keys(this.spritesheetData.meta.animations).length > 0) { + this.currentAnimation = Object.keys(this.spritesheetData.meta.animations)[0]; + this.setAnimation(this.currentAnimation, { loop: this.startLoop, bounce: this.startBouncing }); + } + this.ready = true; + } + frame(index) { + if (!this.loadedSheet || !this.spritesheetData) { + this.top?.warn("AnimatedSprite is not ready or spritesheet data is missing."); + return null; + } + const frameData = this.spritesheetData.frames[index]; + if (!frameData) { + this.top?.warn(`${this.name} attached to ${this.parent?.name} frame at index ${index} was indexed but does not exist in spritesheet`); + return null; + } + const { x, y, w, h } = frameData.frame; + const rotated = frameData.rotated; + const spriteSourceSize = frameData.spriteSourceSize; + const sourceSize = frameData.sourceSize; + const canvas = document.createElement("canvas"); + canvas.width = sourceSize.w; + canvas.height = sourceSize.h; + const ctx = canvas.getContext("2d"); + if (!ctx) { + this.top?.error("Failed to get canvas context."); + return null; + } + ctx.imageSmoothingEnabled = !this.disableAntiAliasing; + ctx.save(); + if (rotated) { + ctx.translate(sourceSize.w / 2, sourceSize.h / 2); + ctx.rotate(90 * Math.PI / 180); + ctx.translate(-sourceSize.h / 2, -sourceSize.w / 2); + ctx.drawImage(this.loadedSheet, x, y, h, w, spriteSourceSize.y, spriteSourceSize.x, spriteSourceSize.h, spriteSourceSize.w); + } else { + ctx.drawImage(this.loadedSheet, x, y, w, h, spriteSourceSize.x, spriteSourceSize.y, spriteSourceSize.w, spriteSourceSize.h); + } + ctx.restore(); + const string = canvas.toDataURL("image/png"); + const img = new Image; + img.src = string; + return img; + } + setAnimation(animationName, { loop, bounce } = {}) { + if (this.spritesheetData && this.spritesheetData.meta.animations[animationName]) { + this.currentAnimation = animationName; + this.currentFrameIndex = 0; + this.bouncing = bounce ?? this.spritesheetData.meta.animations[animationName].bounce ?? false; + if (loop !== undefined) { + this.spritesheetData.meta.animations[this.currentAnimation].loop = loop; + } + } else { + this.top?.warn(`Animation '${animationName}' does not exist in spritesheet for animated sprite <${this.name}> attached to ${this.parent?.name}.`); + } + } + act(deltaTime) { + super.act(deltaTime); + if (!this.ready) { + return; + } + const duration = this.spritesheetData?.frames[this.currentFrameIndex].duration || 100; + const now = performance.now(); + const between = now - this.lastFrameTime; + if (this.ready && this.spritesheetData) { + if (between > duration) { + this.lastFrameTime = now; + if (this.spritesheetData.meta.animations[this.currentAnimation].bounce) { + let direction = this.bouncing ? -1 : 1; + const animFrames = this.spritesheetData.meta.animations[this.currentAnimation].frames.length; + if (this.currentFrameIndex + direction < 0 || this.currentFrameIndex + direction >= animFrames) { + this.bouncing = !this.bouncing; + direction *= -1; + } + this.currentFrameIndex += direction; + if (this.currentFrameIndex < 0) + this.currentFrameIndex = 0; + if (this.currentFrameIndex >= animFrames) + this.currentFrameIndex = animFrames - 1; + } else { + const animFrames = this.spritesheetData.meta.animations[this.currentAnimation].frames.length; + const shouldLoop = this.spritesheetData.meta.animations[this.currentAnimation].loop !== false; + if (shouldLoop) { + const wasAtLastFrame = this.currentFrameIndex === animFrames - 1; + this.currentFrameIndex = (this.currentFrameIndex + 1) % animFrames; + if (wasAtLastFrame && this.onAnimationComplete) { + this.onAnimationComplete(this.currentAnimation, this); + } + } else { + if (this.currentFrameIndex < animFrames - 1) { + this.currentFrameIndex++; + } else if (this.currentFrameIndex === animFrames - 1 && this.onAnimationComplete) { + this.onAnimationComplete(this.currentAnimation, this); + } + } + } + } + const transform = this.sibling("Transform"); + if (!transform) { + if (!this.warned.has("TransformMissing")) { + this.top?.warn(`AnimatedSprite <${this.name}> attached to ${this.parent?.name} does not have a Transform component. Skipping rendering. This will only show once.`) && this.warned.add("TransformMissing"); + } + return; + } + if (!this.top) { + throw new Error(`AnimatedSprite <${this.name}> is not attached to a top-level parent. Ensure it is added to a Game, Scene, or Layer before rendering.`); + } + if (this.top.context) { + this.top.context.imageSmoothingEnabled = !this.disableAntiAliasing; + const position = transform.worldPosition; + const frame = this.frames[this.currentAnimation][this.currentFrameIndex]; + if (frame) { + this.top.context.save(); + this.top.context.translate(position.x, position.y); + this.top.context.rotate(transform.rotation); + this.top.context.imageSmoothingEnabled = !this.disableAntiAliasing; + this.top.context.scale(transform.scale.x * this.facing.x, transform.scale.y * this.facing.y); + this.top.context.drawImage(frame, -this.width / 2, -this.height / 2, this.width, this.height); + this.top.context.restore(); + } else { + this.top?.warn(`Frame (${this.currentAnimation}) index ${this.currentFrameIndex} does not exist for animated sprite <${this.name}> attached to ${this.parent?.name}.`); + } + } else { + throw new Error(`AnimatedSprite <${this.name}> attached to ${this.parent?.name} does not have a context to render to. Ensure it is added to a Game, Scene, or Layer with a game ancestor.`); + } + } + const barHeight = 15; + const barWidth = 6; + const progress = this.lastFrameTime / duration; + this.hoverbug = `${this.ready ? "✅" : "❌"} ${this.spritesheetData?.meta.animations[this.currentAnimation].loop ? "\uD83D\uDD01" : ""}` + `
` + `${this.frames[this.currentAnimation]?.map((frame, i) => { + if (!frame) + return ""; + frame.style.cssText = `display:inline-block; margin-right:2px;width:10px; height:10px; border: 1px solid ${i === this.currentFrameIndex ? "green" : "white"};`; + return frame.outerHTML; + }).join("") || ""}` + `${this.currentAnimation} ${this.bouncing ? "\uD83D\uDD04" : ""}`; + } +} +// Parts/Children/Collider.ts +class Collider extends Part { + colliding = false; + collidingWith = new Set; + tag = ""; + radius; + realWorldStart; + realWorldEnd; + vertices; + active = true; + allowMerge; + randomTestingColors; + constructor({ tag, allowMerge }) { + super({ name: "Collider" }); + this.type = "Collider"; + this.base = "Collider"; + this.tag = tag || ""; + this.radius = 0; + this.realWorldStart = new Vector(0, 0); + this.allowMerge = allowMerge !== undefined ? allowMerge : true; + this.realWorldEnd = new Vector(0, 0); + this.randomTestingColors = [ + Math.random() * 255, + Math.random() * 255, + Math.random() * 255 + ]; + this.vertices = []; + } + setTag(tag) { + this.tag = tag; + } + onMount(parent) { + super.onMount(parent); + const transform = this.sibling("Transform"); + if (!transform) { + this.top?.warn(`Collider <${this.name}> (${this.id}) does not have Transform sibling. Please ensure you add a Transform component.`); + return; + } + this.updateCollider(transform); + } + onRegister(attribute, value) { + super.onRegister(attribute, value); + if (attribute === "layer") { + value.flats.colliders.push(this); + } + } + evaluateMerging() { + const layer = this.registrations["layer"]; + if (!layer) + return; + const candidates = layer.spatialGrid.query(this); + const fellowColliders = candidates.filter((c) => c.tag == this.tag && c.id !== this.id && c.allowMerge && c.active); + if (fellowColliders.length == 0) + return; + for (const fellow of fellowColliders) { + if (!fellow.sibling("Transform")?.initialized) + continue; + if (this.id < fellow.id && this.checkCollision(fellow, true)) { + this.mergeWith(fellow); + } + } + } + mergeWith(other) { + if (this.tag !== other.tag || other.tag == "" || this.tag == "") + return; + const thisTransform = this.sibling("Transform"); + if (!thisTransform) { + this.top?.warn(`Collider <${this.name}> has no Transform sibling, cannot merge.`); + return; + } + const allPolygons = []; + const g1 = this.getGeometry(); + if (this.type === "MultiPolygonCollider") { + allPolygons.push(...g1); + } else { + allPolygons.push(g1); + } + const g2 = other.getGeometry(); + if (other.type === "MultiPolygonCollider") { + allPolygons.push(...g2); + } else { + allPolygons.push(g2); + } + if (allPolygons.length === 0) + return; + const localPolygons = allPolygons.map((polygon) => { + return polygon.map(([x, y]) => thisTransform.worldToLocal(new Vector(x, y))); + }); + this._updateVerticesAfterMerge(localPolygons); + other.inactivate(); + } + onStart() {} + inactivate() { + this.active = false; + } + activate() { + this.active = true; + } + act(delta) { + super.act(delta); + if (!this.active) + return; + if (!this.registrations?.layer) { + throw new Error(`Collider <${this.name}> (${this.id}) is not registered to a layer. Collisions will not be checked.`); + } + const transform = this.sibling("Transform"); + if (!transform) + return; + this.updateCollider(transform); + this.colliding = false; + this.collidingWith.clear(); + const layer = this.registrations.layer; + const candidates = layer.spatialGrid.query(this); + for (const other of candidates) { + if (other === this) + continue; + if (this.checkCollision(other)) { + this.colliding = true; + this.collidingWith.add(other); + } + } + this.hoverbug = `${this.colliding ? "\uD83D\uDFE5" : "\uD83D\uDFE9"} - ${Array.from(this.collidingWith).map((o) => o.name).join(", ")} objects`; + const fill = this.active; + const ctx = this.top instanceof Game ? this.top.context : null; + if (ctx) { + ctx.beginPath(); + ctx.strokeStyle = `rgb(${this.randomTestingColors[0]}, ${this.randomTestingColors[1]}, ${this.randomTestingColors[2]})`; + ctx.fillStyle = fill ? `rgba(${this.randomTestingColors[0]}, ${this.randomTestingColors[1]}, ${this.randomTestingColors[2]}, 0.5)` : "transparent"; + ctx.moveTo(this.worldVertices[0].x, this.worldVertices[0].y); + for (const vertex of this.worldVertices) { + ctx.lineTo(vertex.x, vertex.y); + } + ctx.closePath(); + ctx.stroke(); + ctx.fill(); + } + if (this.top instanceof Game && this.top.devmode) { + const ctx2 = this.top.context; + if (ctx2) { + this.drawDebug(ctx2); + } + } + } + checkCollision(other, ignoreTags = false) { + const thisTransform = this.sibling("Transform"); + const otherTransform = other.sibling("Transform"); + if (!thisTransform || !otherTransform) { + return false; + } + this.updateCollider(thisTransform); + other.updateCollider(otherTransform); + if (!ignoreTags && other.tag === this.tag && this.tag !== "") + return false; + if (this.realWorldEnd.x < other.realWorldStart.x || this.realWorldStart.x > other.realWorldEnd.x || this.realWorldEnd.y < other.realWorldStart.y || this.realWorldStart.y > other.realWorldEnd.y) { + return false; + } + return this.narrowPhaseCheck(other); + } + isVisible(camera) { + if (!this.top) { + throw new Error("Collider cannot calculate visibility without a 'top' (Game instance)."); + } + const transform = this.sibling("Transform"); + if (!transform) { + return false; + } + this.updateCollider(transform); + const { offset, scale } = camera.getViewMatrix(); + const cameraPos = offset.multiply(-1); + const screenWidth = this.top.width; + const screenHeight = this.top.height; + const viewWidth = screenWidth / scale.x; + const viewHeight = screenHeight / scale.y; + const cameraVertices = [ + new Vector(cameraPos.x - viewWidth / 2, cameraPos.y - viewHeight / 2), + new Vector(cameraPos.x + viewWidth / 2, cameraPos.y - viewHeight / 2), + new Vector(cameraPos.x + viewWidth / 2, cameraPos.y + viewHeight / 2), + new Vector(cameraPos.x - viewWidth / 2, cameraPos.y + viewHeight / 2) + ]; + return this.checkVerticesAgainstVertices(this.worldVertices, cameraVertices); + } + checkVerticesAgainstVertices(vertices1, vertices2) { + const axes1 = this.getAxes(vertices1); + for (const axis of axes1) { + const projection1 = this.project(vertices1, axis); + const projection2 = this.project(vertices2, axis); + if (!this.overlap(projection1, projection2)) { + return false; + } + } + const axes2 = this.getAxes(vertices2); + for (const axis of axes2) { + const projection1 = this.project(vertices1, axis); + const projection2 = this.project(vertices2, axis); + if (!this.overlap(projection1, projection2)) { + return false; + } + } + return true; + } + getAxes(vertices) { + const axes = []; + for (let i = 0;i < vertices.length; i++) { + const p1 = vertices[i]; + const p2 = vertices[i === vertices.length - 1 ? 0 : i + 1]; + const edge = p2.subtract(p1); + const normal = new Vector(-edge.y, edge.x).normalize(); + axes.push(normal); + } + return axes; + } + project(vertices, axis) { + let min = axis.dot(vertices[0]); + let max = min; + for (let i = 1;i < vertices.length; i++) { + const p = axis.dot(vertices[i]); + if (p < min) { + min = p; + } else if (p > max) { + max = p; + } + } + return { min, max }; + } + overlap(proj1, proj2) { + return proj1.max >= proj2.min && proj2.max >= proj1.min; + } + _checkPolygonVsPolygon(vertices1, vertices2) { + const axes1 = this.getAxes(vertices1); + const axes2 = this.getAxes(vertices2); + const axes = axes1.concat(axes2); + for (const axis of axes) { + const projection1 = this.project(vertices1, axis); + const projection2 = this.project(vertices2, axis); + if (!this.overlap(projection1, projection2)) { + return false; + } + } + return true; + } +} + +// Parts/Children/MultiPolygonCollider.ts +class MultiPolygonCollider extends Collider { + polygons; + _worldPolygons = []; + unioned = []; + constructor({ polygons, tag = "" }) { + super({ tag, allowMerge: tag !== "" }); + this.name = "MultiPolygonCollider"; + this.polygons = polygons; + this.type = "MultiPolygonCollider"; + let maxDist = 0; + const allVertices = polygons.flat(); + for (let i = 0;i < allVertices.length; i++) { + for (let j = i + 1;j < allVertices.length; j++) { + const dist = allVertices[i].distance(allVertices[j]); + if (dist > maxDist) { + maxDist = dist; + } + } + } + this.radius = maxDist; + } + getGeometry() { + return this._worldPolygons.map((polygon) => { + return polygon.map((v) => v.toArray()); + }); + } + get worldVertices() { + const allVertices = this._worldPolygons.flat(); + allVertices.sort((a, b) => { + return a.x < b.x || a.x == b.x && a.y < b.y ? -1 : 1; + }); + const cross = (o, a, b) => { + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); + }; + const lower = []; + for (const p of allVertices) { + while (lower.length >= 2 && cross(lower[lower.length - 2], lower[lower.length - 1], p) <= 0) { + lower.pop(); + } + lower.push(p); + } + const upper = []; + for (let i = allVertices.length - 1;i >= 0; i--) { + const p = allVertices[i]; + while (upper.length >= 2 && cross(upper[upper.length - 2], upper[upper.length - 1], p) <= 0) { + upper.pop(); + } + upper.push(p); + } + upper.pop(); + lower.pop(); + return lower.concat(upper); + } + act(delta) { + super.act(delta); + } + _updateVerticesAfterMerge(polygons) { + this.polygons = polygons; + } + updateCollider(transform) { + const position = transform.worldPosition; + const rotation = transform.rotation; + const scale = transform.scale; + this._worldPolygons = this.polygons.map((polygon) => { + return polygon.map((vertex) => { + let scaledVertex = vertex.multiply(scale); + if (rotation !== 0) { + const cos = Math.cos(rotation); + const sin = Math.sin(rotation); + scaledVertex = new Vector(scaledVertex.x * cos - scaledVertex.y * sin, scaledVertex.x * sin + scaledVertex.y * cos); + } + return position.add(scaledVertex); + }); + }); + const allWorldVertices = this._worldPolygons.flat(); + const xs = allWorldVertices.map((v) => v.x); + const ys = allWorldVertices.map((v) => v.y); + this.realWorldStart.set(Math.min(...xs), Math.min(...ys)); + this.realWorldEnd.set(Math.max(...xs), Math.max(...ys)); + } + narrowPhaseCheck(other) { + if (other instanceof MultiPolygonCollider) { + for (const p1 of this._worldPolygons) { + for (const p2 of other._worldPolygons) { + if (this._checkPolygonVsPolygon(p1, p2)) { + return true; + } + } + } + return false; + } + for (const polygon of this._worldPolygons) { + if (this._checkPolygonVsPolygon(polygon, other.worldVertices)) { + return true; + } + } + return false; + } + drawDebug(ctx) { + ctx.save(); + ctx.strokeStyle = this.colliding ? "rgba(255, 0, 100, 0.8)" : "rgba(0, 255, 100, 0.8)"; + ctx.lineWidth = 1; + for (const polygon of this._worldPolygons) { + ctx.beginPath(); + ctx.moveTo(polygon[0].x, polygon[0].y); + for (let i = 1;i < polygon.length; i++) { + ctx.lineTo(polygon[i].x, polygon[i].y); + } + ctx.closePath(); + ctx.stroke(); + } + ctx.restore(); + } + clone(memo = new Map) { + if (memo.has(this)) { + return memo.get(this); + } + const clonedMultiPolygonCollider = new MultiPolygonCollider({ + polygons: this.polygons.map((p) => p.map((v) => v.clone())), + tag: this.tag + }); + memo.set(this, clonedMultiPolygonCollider); + this._cloneProperties(clonedMultiPolygonCollider, memo); + clonedMultiPolygonCollider.colliding = false; + clonedMultiPolygonCollider.base = this.base; + clonedMultiPolygonCollider.type = this.type; + clonedMultiPolygonCollider.collidingWith = new Set; + return clonedMultiPolygonCollider; + } +} + +// Parts/Children/PolygonCollider.ts +class PolygonCollider extends Collider { + localVertices; + _worldVertices = []; + constructor({ vertices, tag = "" }) { + super({ tag, allowMerge: tag !== "" }); + this.name = "PolygonCollider"; + this.localVertices = vertices; + this.vertices = vertices; + let maxDist = 0; + for (let i = 0;i < this.localVertices.length; i++) { + for (let j = i + 1;j < this.localVertices.length; j++) { + const dist = this.localVertices[i].distance(this.localVertices[j]); + if (dist > maxDist) { + maxDist = dist; + } + } + } + this.radius = maxDist; + this.type = "PolygonCollider"; + } + get worldVertices() { + return this._worldVertices; + } + getGeometry() { + return [this.worldVertices.map((v) => v.toArray())]; + } + act(delta) { + super.act(delta); + } + _updateVerticesAfterMerge(polygons) { + const newCollider = new MultiPolygonCollider({ polygons, tag: this.tag }); + newCollider.active = this.active; + newCollider.allowMerge = this.allowMerge; + const parent = this.parent; + if (parent) { + parent.removeChild(this); + parent.addChild(newCollider); + } + } + updateCollider(transform) { + const position = transform.worldPosition; + const rotation = transform.rotation; + const scale = transform.scale; + this._worldVertices = this.localVertices.map((vertex) => { + let scaledVertex = vertex.multiply(scale); + if (rotation !== 0) { + const cos = Math.cos(rotation); + const sin = Math.sin(rotation); + scaledVertex = new Vector(scaledVertex.x * cos - scaledVertex.y * sin, scaledVertex.x * sin + scaledVertex.y * cos); + } + return position.add(scaledVertex); + }); + const xs = this._worldVertices.map((v) => v.x); + const ys = this._worldVertices.map((v) => v.y); + this.realWorldStart.set(Math.min(...xs), Math.min(...ys)); + this.realWorldEnd.set(Math.max(...xs), Math.max(...ys)); + } + narrowPhaseCheck(other) { + if (other instanceof BoxCollider) { + return this.checkPolygonVsBox(this, other); + } else if (other instanceof PolygonCollider) { + return this.checkPolygonVsPolygon(this, other); + } else if (other instanceof MultiPolygonCollider) { + return other.narrowPhaseCheck(this); + } + this.top?.warn("Collision checks are only supported between BoxColliders and PolygonColliders."); + return false; + } + checkPolygonVsPolygon(poly1, poly2) { + return this._checkPolygonVsPolygon(poly1.worldVertices, poly2.worldVertices); + } + checkPolygonVsBox(poly, box) { + const boxVertices = box.worldVertices; + const axes1 = this.getAxes(poly.worldVertices); + const axes2 = this.getAxes(boxVertices); + const axes = axes1.concat(axes2); + for (const axis of axes) { + const projection1 = this.project(poly.worldVertices, axis); + const projection2 = this.project(boxVertices, axis); + if (!this.overlap(projection1, projection2)) { + return false; + } + } + return true; + } + drawDebug(ctx) { + ctx.save(); + ctx.strokeStyle = this.colliding ? "rgba(255, 0, 100, 0.8)" : "rgba(0, 255, 100, 0.8)"; + ctx.lineWidth = 1; + ctx.beginPath(); + ctx.moveTo(this._worldVertices[0].x, this._worldVertices[0].y); + for (let i = 1;i < this._worldVertices.length; i++) { + ctx.lineTo(this._worldVertices[i].x, this._worldVertices[i].y); + } + ctx.closePath(); + ctx.stroke(); + ctx.restore(); + } + clone(memo = new Map) { + if (memo.has(this)) { + return memo.get(this); + } + const clonedPolygonCollider = new PolygonCollider({ + vertices: this.localVertices.map((v) => v.clone()), + tag: this.tag + }); + memo.set(this, clonedPolygonCollider); + this._cloneProperties(clonedPolygonCollider, memo); + clonedPolygonCollider.colliding = false; + clonedPolygonCollider.base = this.base; + clonedPolygonCollider.type = this.type; + clonedPolygonCollider.collidingWith = new Set; + return clonedPolygonCollider; + } +} + +// Parts/Children/BoxCollider.ts +class BoxCollider extends Collider { + start; + end; + rotatedCorners = []; + width; + height; + cachedAxes = []; + lastRotation = NaN; + lastScale = new Vector(NaN, NaN); + constructor({ width, height, tag = "" }) { + super({ tag, allowMerge: tag !== "" }); + this.name = "BoxCollider"; + this.width = width; + this.height = height; + this.start = new Vector(-width / 2, -height / 2); + this.end = new Vector(width / 2, height / 2); + this.radius = Math.sqrt((width / 2) ** 2 + (height / 2) ** 2); + this.type = "BoxCollider"; + for (let i = 0;i < 4; i++) { + this.rotatedCorners.push(new Vector(0, 0)); + this.vertices.push(new Vector(0, 0)); + } + } + get worldVertices() { + return this.rotatedCorners; + } + getGeometry() { + return [this.worldVertices.map((v) => v.toArray())]; + } + updateCollider(transform) { + const cos = Math.cos(transform.rotation); + const sin = Math.sin(transform.rotation); + const halfW = this.width * transform.scale.x / 2; + const halfH = this.height * transform.scale.y / 2; + const localCorners = [ + new Vector(-halfW, -halfH), + new Vector(halfW, -halfH), + new Vector(halfW, halfH), + new Vector(-halfW, halfH) + ]; + let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity; + for (let i = 0;i < 4; i++) { + const c = localCorners[i]; + const x = c.x * cos - c.y * sin + transform.worldPosition.x; + const y = c.x * sin + c.y * cos + transform.worldPosition.y; + this.rotatedCorners[i].set(x, y); + this.vertices[i].set(x - transform.worldPosition.x, y - transform.worldPosition.y); + if (x < minX) + minX = x; + if (x > maxX) + maxX = x; + if (y < minY) + minY = y; + if (y > maxY) + maxY = y; + } + this.realWorldStart.set(minX, minY); + this.realWorldEnd.set(maxX, maxY); + if (transform.rotation !== this.lastRotation || !transform.scale.equals(this.lastScale)) { + this.cachedAxes = this.getAxes(this.rotatedCorners); + this.lastRotation = transform.rotation; + this.lastScale = transform.scale.clone(); + } + } + narrowPhaseCheck(other) { + if (other instanceof BoxCollider) { + return this.checkBoxVsBox(this, other); + } else if (other instanceof PolygonCollider || other instanceof MultiPolygonCollider) { + return other.narrowPhaseCheck(this); + } + this.top?.warn(`Collision with unsupported collider type: ${other.type}`); + return false; + } + checkBoxVsBox(box1, box2) { + const axes = box1.cachedAxes.concat(box2.cachedAxes); + for (const axis of axes) { + const proj1 = this.project(box1.rotatedCorners, axis); + const proj2 = this.project(box2.rotatedCorners, axis); + if (!this.overlap(proj1, proj2)) + return false; + } + return true; + } + _updateVerticesAfterMerge(polygons) { + const newCollider = new MultiPolygonCollider({ polygons, tag: this.tag }); + newCollider.active = this.active; + newCollider.allowMerge = this.allowMerge; + const parent = this.parent; + if (parent) { + parent.removeChild(this); + parent.addChild(newCollider); + } + } + act(delta) { + super.act(delta); + } + drawDebug(ctx) { + ctx.save(); + ctx.strokeStyle = this.colliding ? "rgba(255,0,0,0.5)" : "rgba(0,255,0,0.5)"; + ctx.lineWidth = 1; + ctx.beginPath(); + ctx.moveTo(this.rotatedCorners[0].x, this.rotatedCorners[0].y); + for (let i = 1;i < 4; i++) { + ctx.lineTo(this.rotatedCorners[i].x, this.rotatedCorners[i].y); + } + ctx.closePath(); + ctx.stroke(); + ctx.restore(); + } + clone(memo = new Map) { + if (memo.has(this)) + return memo.get(this); + const cloned = new BoxCollider({ + width: this.width, + height: this.height, + tag: this.tag + }); + memo.set(this, cloned); + this._cloneProperties(cloned, memo); + cloned.colliding = false; + cloned.base = this.base; + cloned.type = this.type; + cloned.collidingWith = new Set; + return cloned; + } +} +// Parts/Children/ColorRender.ts +class ColorRender extends Renderer { + color; + vertices; + constructor({ width, height, color, vertices }) { + if (!width || !height) { + if (vertices && vertices.length > 0) { + width = Math.max(...vertices.map((v) => v.x)) - Math.min(...vertices.map((v) => v.x)); + height = Math.max(...vertices.map((v) => v.y)) - Math.min(...vertices.map((v) => v.y)); + } else { + throw new Error("ColorRender requires either width and height or vertices to be defined."); + } + } + super({ width, height }); + this.name = "ColorRender"; + this.color = color; + this.debugEmoji = "\uD83C\uDFA8"; + this.type = "ColorRender"; + this.base = "Renderer"; + this.vertices = vertices || []; + if (this.vertices.length === 0) { + this.vertices = [ + new Vector(-this.width / 2, -this.height / 2), + new Vector(this.width / 2, -this.height / 2), + new Vector(this.width / 2, this.height / 2), + new Vector(-this.width / 2, this.height / 2) + ]; + } + } + onMount(parent) { + super.onMount(parent); + if (!this.sibling("Transform")) { + this.top?.warn(`ColorRender <${this.name}> does not have a Transform sibling. Please ensure you add a Transform component before adding others.`); + return; + } + parent.setSuperficialDimensions(this.width, this.height); + } + clone(memo = new Map) { + if (memo.has(this)) { + return memo.get(this); + } + const clonedColor = new ColorRender({ + width: this.width, + height: this.height, + color: this.color, + vertices: this.vertices.map((v) => v.clone()) + }); + memo.set(this, clonedColor); + this._cloneProperties(clonedColor, memo); + return clonedColor; + } + act(delta) { + super.act(delta); + if (!this.top) { + throw new Error(`ColorRender <${this.parent?.name}.${this.name}> is not attached to a top-level parent. Ensure it is added to a Game instance or Scene before rendering.`); + } + const transform = this.sibling("Transform"); + if (!transform) { + this.top?.warn(`ColorRender <${this.parent?.name}.${this.name}> does not have a Transform sibling. Skipping rendering.`); + return; + } + const position = transform.worldPosition; + const rotation = transform.rotation; + this.top.context.save(); + this.top.context.translate(position.x, position.y); + this.top.context.rotate(rotation); + this.top.context.scale(transform.scale.x * this.facing.x, transform.scale.y * this.facing.y); + this.top.context.fillStyle = this.color; + this.top.context.beginPath(); + if (this.vertices.length > 0) { + this.top.context.moveTo(this.vertices[0].x, this.vertices[0].y); + for (let i = 1;i < this.vertices.length; i++) { + this.top.context.lineTo(this.vertices[i].x, this.vertices[i].y); + } + this.top.context.closePath(); + this.top.context.fill(); + } + this.top.context.restore(); + this.hoverbug = `Color: ${this.color}`; + } +} +// Parts/Children/Transform.ts +class Transform extends Part { + position; + worldPosition; + rotation; + scale; + initialized; + constructor({ position, rotation, scale } = {}) { + super({ name: "Transform" }); + this.position = position || Vector.From(0); + this.worldPosition = Vector.From(0); + this.worldPosition.set(this.position); + this.rotation = rotation || 0; + this.scale = scale || new Vector(1, 1); + this.debugEmoji = "\uD83D\uDCD0"; + this.type = "Transform"; + this.initialized = false; + } + onMount(parent) { + super.onMount(parent); + this.updateWorldPosition(); + if (parent.superficialWidth && parent.superficialHeight) { + this.superficialWidth = parent.superficialWidth; + this.superficialHeight = parent.superficialHeight; + } + } + move(delta) { + this.position.set(this.position.add(delta)); + this.updateWorldPosition(); + } + moveTo(position) { + this.position.set(position); + this.updateWorldPosition(); + } + rotate(angle) { + this.rotation += angle; + this.rotation = this.rotation % (2 * Math.PI); + this.updateWorldPosition(); + } + setRotation(rotation) { + this.rotation = rotation % (2 * Math.PI); + this.updateWorldPosition(); + } + worldToLocal(position) { + const translated = position.subtract(this.worldPosition); + const cos = Math.cos(-this.rotation); + const sin = Math.sin(-this.rotation); + const rotated = new Vector(translated.x * cos - translated.y * sin, translated.x * sin + translated.y * cos); + const scaled = new Vector(rotated.x / this.scale.x, rotated.y / this.scale.y); + return scaled; + } + preFrame() { + super.preFrame(); + this.updateWorldPosition(); + } + updateWorldPosition() { + const parentTransform = this.parent?.parent?.child("Transform"); + if (parentTransform) { + const scaledPosition = this.position.multiply(parentTransform.scale); + const cos = Math.cos(parentTransform.rotation); + const sin = Math.sin(parentTransform.rotation); + const rotatedPosition = new Vector(scaledPosition.x * cos - scaledPosition.y * sin, scaledPosition.x * sin + scaledPosition.y * cos); + this.worldPosition.set(rotatedPosition.add(parentTransform.worldPosition)); + } else { + this.worldPosition.set(this.position); + } + this.initialized = true; + } + act(_delta) { + this.hoverbug = `${this.position.toString()} | ${this.worldPosition.toString()} | ${(this.rotation / Math.PI).toFixed(2)}pi | ${this.scale.toString()}`; + } +} +// Parts/Sound.ts +class Sound extends Part { + audio; + webEngine = false; + start = false; + _isLoaded = false; + _clones = new Set; + _wantToPlay = false; + _paused = false; + _started = false; + _wasMainAudioPlaying = false; + _playingClonesWhenPaused = new Set; + _hasEndedListener = false; + constructor({ name, src, volume = 1, loop = false, webEngine = false, start = false }) { + super({ name }); + this.debugEmoji = "\uD83D\uDD0A"; + this.audio = new Audio(src); + this.audio.volume = volume; + this.audio.loop = loop; + this.start = start; + this.webEngine = webEngine; + this.type = "Sound"; + SoundManager.registerSound(this); + this.audio.addEventListener("canplaythrough", () => { + this._isLoaded = true; + this.ready = true; + if (this.start && !this._started || this._wantToPlay) { + this._started = true; + this.play(); + } + }); + this.audio.addEventListener("error", () => { + this._isLoaded = false; + this.ready = false; + this.top?.error(`Failed to load sound <${this.name}> from src: ${src.substring(0, 30)}...`); + }); + } + clone(memo = new Map) { + if (memo.has(this)) { + return memo.get(this); + } + const clonedSound = new Sound({ + name: this.name, + src: this.audio.src, + volume: this.audio.volume, + loop: this.audio.loop, + webEngine: this.webEngine, + start: this.start + }); + memo.set(this, clonedSound); + this._cloneProperties(clonedSound, memo); + clonedSound._isLoaded = false; + clonedSound._clones = new Set; + clonedSound._wantToPlay = false; + clonedSound._paused = false; + clonedSound._started = false; + clonedSound._wasMainAudioPlaying = false; + clonedSound._playingClonesWhenPaused = new Set; + clonedSound._hasEndedListener = false; + SoundManager.unregisterSound(this); + SoundManager.registerSound(clonedSound); + return clonedSound; + } + play(options = {}) { + if (this.webEngine && !SoundManager.getIsGameRunning()) + return; + const { restart = false, clone = false } = options; + if (!this._isLoaded) { + this._wantToPlay = true; + return; + } + this._paused = false; + this._started = true; + if (clone) { + const cloneAudio = this.audio.cloneNode(true); + cloneAudio.volume = this.audio.volume; + cloneAudio.loop = false; + this._wantToPlay = false; + cloneAudio.play().catch((e) => this.top?.error(`Error playing cloned sound <${this.name}>:`, e)); + this._clones.add(cloneAudio); + const handleCloneEnded = () => { + this._clones.delete(cloneAudio); + cloneAudio.removeEventListener("ended", handleCloneEnded); + if (this._clones.size === 0) { + this._paused = false; + this._started = false; + } + }; + cloneAudio.addEventListener("ended", handleCloneEnded); + } else { + if (restart) { + this.audio.currentTime = 0; + } + this._wantToPlay = false; + this.audio.play().catch((e) => this.top?.error(`Error playing sound <${this.name}>:`, e)); + if (!this._hasEndedListener) { + this._hasEndedListener = true; + this.audio.addEventListener("ended", () => { + this._paused = false; + this._started = false; + this._clones.forEach((clone2) => { + clone2.pause(); + }); + this._clones.clear(); + }); + } + } + } + pause() { + if (!this._paused && this._started) { + this._paused = true; + this._wantToPlay = false; + this._wasMainAudioPlaying = !this.audio.paused; + this._playingClonesWhenPaused.clear(); + this._clones.forEach((clone) => { + if (!clone.paused) { + this._playingClonesWhenPaused.add(clone); + } + }); + this.audio.pause(); + this._clones.forEach((clone) => clone.pause()); + } + } + resume() { + if (this._paused) { + this._paused = false; + if (this._wasMainAudioPlaying) { + this.audio.play().catch((e) => this.top?.error(`Error resuming sound <${this.name}>:`, e)); + } + this._playingClonesWhenPaused.forEach((clone) => { + if (this._clones.has(clone)) { + clone.play().catch((e) => this.top?.error(`Error resuming cloned sound <${this.name}>:`, e)); + } + }); + this._playingClonesWhenPaused.clear(); + this._wasMainAudioPlaying = false; + } + } + stop() { + this._paused = false; + this._started = false; + this._wasMainAudioPlaying = false; + this._playingClonesWhenPaused.clear(); + this.audio.pause(); + this._wantToPlay = false; + this.audio.currentTime = 0; + this._clones.forEach((clone) => clone.pause()); + this._clones.clear(); + } + setVolume(volume) { + const clampedVolume = Math.max(0, Math.min(1, volume)); + if (this.audio.volume !== clampedVolume) { + this.audio.volume = clampedVolume; + this._clones.forEach((clone) => { + clone.volume = clampedVolume; + }); + } + } + setLoop(loop) { + if (this.audio.loop !== loop) { + this.audio.loop = loop; + } + } + act(delta) { + super.act(delta); + this.hoverbug = `${this.audio.paused ? "⏸️" : "▶️"} V:${this.audio.volume.toFixed(2)} L:${this.audio.loop ? "✅" : "❌"}`; + } + destroy() { + this.stop(); + this.audio.src = ""; + this.audio.load(); + SoundManager.unregisterSound(this); + super.destroy(); + } +} +// Parts/Follow.ts +class Follow extends Part { + target = null; + offset; + externalOffset = new Vector(0, 0); + interpolationSpeed; + constructor({ + name, + target, + offset = new Vector(0, 0), + interpolationSpeed = 1 + }) { + super({ name: name || "Follow" }); + this.target = target; + this.offset = offset; + this.interpolationSpeed = interpolationSpeed; + this.debugEmoji = "\uD83C\uDFAF"; + } + clone(memo = new Map) { + if (memo.has(this)) { + return memo.get(this); + } + const clonedFollow = new Follow({ + name: this.name, + target: this.target, + offset: this.offset.clone(), + interpolationSpeed: this.interpolationSpeed + }); + memo.set(this, clonedFollow); + this._cloneProperties(clonedFollow, memo); + if (this.target && memo.has(this.target)) { + clonedFollow.target = memo.get(this.target); + } else { + clonedFollow.target = this.target; + } + clonedFollow.externalOffset = new Vector(0, 0); + return clonedFollow; + } + act(delta) { + super.act(delta); + if (this.target) { + const targetTransform = this.target; + const ownTransform = this.sibling("Transform"); + if (!ownTransform) { + throw new Error(`Follow <${this.name}> requires a Transform component to be mounted to a parent GameObject.`); + } + const totalOffset = this.offset.add(this.externalOffset); + const targetX = targetTransform.position.x + totalOffset.x; + const targetY = targetTransform.position.y + totalOffset.y; + if (this.interpolationSpeed >= 1) { + ownTransform.position.x = targetX; + ownTransform.position.y = targetY; + } else { + const t = this.interpolationSpeed * delta; + ownTransform.position.x += (targetX - ownTransform.position.x) * t; + ownTransform.position.y += (targetY - ownTransform.position.y) * t; + } + } + } +} +// Parts/CharacterMovement.ts +class CharacterMovement extends Part { + speed; + movementType; + input; + constructor({ speed = 5, movementType = "WASD", input }) { + super({ name: "CharacterMovement" }); + this.speed = speed; + this.movementType = movementType; + this.input = input; + this.type = "CharacterMovement"; + } + act(delta) { + if (!this.input) { + if (!this.warned.has("MissingInput")) + this.top?.warn(`CharacterMovement <${this.name}> (${this.id}) is missing an input property. Please create an input on the scene and pass it.`) && this.warned.add("MissingInput"); + return; + } + const transform = this.sibling("Transform"); + if (!transform) { + return; + } + const speed = this.speed * delta; + const keys = this.input.downkeys; + let dx = 0; + let dy = 0; + if (this.movementType === "WASD" || this.movementType === "BOTH") { + if (keys.has("w")) { + dy -= 1; + } + if (keys.has("s")) { + dy += 1; + } + if (keys.has("a")) { + dx -= 1; + } + if (keys.has("d")) { + dx += 1; + } + } + if (this.movementType === "ArrowKeys" || this.movementType === "BOTH") { + if (keys.has("ArrowUp")) { + dy -= 1; + } + if (keys.has("ArrowDown")) { + dy += 1; + } + if (keys.has("ArrowLeft")) { + dx -= 1; + } + if (keys.has("ArrowRight")) { + dx += 1; + } + } + if (dx !== 0 && dy !== 0) { + dx *= Math.SQRT1_2; + dy *= Math.SQRT1_2; + } + if (dx !== 0 || dy !== 0) { + transform.move(new Vector(dx * speed, dy * speed)); + } + } +} +// Parts/ParallaxLayer.ts +class ParallaxLayer extends Layer { + parallaxFactor; + originalPositions = new Map; + initialized = false; + constructor({ name, parallaxFactor = 0.5 }) { + super({ name }); + this.parallaxFactor = parallaxFactor; + } + clone(memo = new Map) { + if (memo.has(this)) { + return memo.get(this); + } + const clonedLayer = new ParallaxLayer({ + name: this.name, + parallaxFactor: this.parallaxFactor + }); + memo.set(this, clonedLayer); + this._cloneProperties(clonedLayer, memo); + clonedLayer.initialized = false; + clonedLayer.originalPositions = new Map; + return clonedLayer; + } + initialize() { + this.childrenArray.forEach((child) => { + const childTransform = child.child("Transform"); + if (childTransform) { + this.originalPositions.set(child.name, new Vector(childTransform.position.x, childTransform.position.y)); + } + }); + } + act(delta) { + if (!this.initialized) { + this.initialize(); + this.initialized = true; + } + const camera = this.top?.currentScene?.activeCamera; + if (camera) { + const cameraTransform = camera.child("Transform"); + if (cameraTransform) { + const cameraPosition = cameraTransform.worldPosition; + this.childrenArray.forEach((child) => { + const childTransform = child.child("Transform"); + if (childTransform) { + const originalPos = this.originalPositions.get(child.name); + if (originalPos) { + const parallaxX = originalPos.x + cameraPosition.x * this.parallaxFactor; + const parallaxY = originalPos.y + cameraPosition.y * this.parallaxFactor; + childTransform.position.set(new Vector(parallaxX, parallaxY)); + } + } + }); + } + } + super.act(delta); + } +} +// Parts/PhysicsEngine.ts +var import_matter_js = __toESM(require_matter(), 1); +// Parts/AreaTrigger.ts +class AreaTrigger extends Part { + onEnter; + onExit; + activeCollisions = new Set; + constructor({ onEnter, onExit }) { + super({ name: "AreaTrigger" }); + this.onEnter = onEnter; + this.onExit = onExit; + this.debugEmoji = "\uD83E\uDDF2"; + this.type = "AreaTrigger"; + } + clone(memo = new Map) { + if (memo.has(this)) { + return memo.get(this); + } + const clonedTrigger = new AreaTrigger({ + onEnter: this.onEnter, + onExit: this.onExit + }); + memo.set(this, clonedTrigger); + this._cloneProperties(clonedTrigger, memo); + clonedTrigger.activeCollisions = new Set; + return clonedTrigger; + } + act(delta) { + super.act(delta); + const collider = this.sibling("Collider"); + if (!collider) { + this.top?.warn(`AreaTrigger <${this.name}> requires a Collider sibling.`); + return; + } + const currentCollisions = new Set; + if (collider.colliding) { + for (const other of collider.collidingWith) { + currentCollisions.add(other); + if (!this.activeCollisions.has(other)) { + if (this.onEnter) { + this.onEnter(other); + } + } + } + } + for (const other of this.activeCollisions) { + if (!currentCollisions.has(other)) { + const exitedCollider = Array.from(collider.collidingWith).find((c) => c.id === other.id); + if (this.onExit) { + this.onExit(exitedCollider || {}); + } + } + } + this.activeCollisions = currentCollisions; + this.hoverbug = `Active: ${this.activeCollisions.size}`; + } +} +// Parts/PhysicsBody.ts +var import_matter_js2 = __toESM(require_matter(), 1); +// node_modules/terser/lib/utils/index.js +function characters(str) { + return str.split(""); +} +function member(name, array) { + return array.includes(name); +} + +class DefaultsError extends Error { + constructor(msg, defs) { + super(); + this.name = "DefaultsError"; + this.message = msg; + this.defs = defs; + } +} +function defaults(args, defs, croak) { + if (args === true) { + args = {}; + } else if (args != null && typeof args === "object") { + args = { ...args }; + } + const ret = args || {}; + if (croak) { + for (const i in ret) + if (HOP(ret, i) && !HOP(defs, i)) { + throw new DefaultsError("`" + i + "` is not a supported option", defs); + } + } + for (const i in defs) + if (HOP(defs, i)) { + if (!args || !HOP(args, i)) { + ret[i] = defs[i]; + } else if (i === "ecma") { + let ecma = args[i] | 0; + if (ecma > 5 && ecma < 2015) + ecma += 2009; + ret[i] = ecma; + } else { + ret[i] = args && HOP(args, i) ? args[i] : defs[i]; + } + } + return ret; +} +function noop() {} +function return_false() { + return false; +} +function return_true() { + return true; +} +function return_this() { + return this; +} +function return_null() { + return null; +} +var MAP = function() { + function MAP2(a, tw, allow_splicing = true) { + const new_a = []; + for (let i = 0;i < a.length; ++i) { + let item = a[i]; + let ret = item.transform(tw, allow_splicing); + if (ret instanceof AST_Node) { + new_a.push(ret); + } else if (ret instanceof Splice) { + new_a.push(...ret.v); + } + } + return new_a; + } + MAP2.splice = function(val) { + return new Splice(val); + }; + MAP2.skip = {}; + function Splice(val) { + this.v = val; + } + return MAP2; +}(); +function make_node(ctor, orig, props) { + if (!props) + props = {}; + if (orig) { + if (!props.start) + props.start = orig.start; + if (!props.end) + props.end = orig.end; + } + return new ctor(props); +} +function push_uniq(array, el) { + if (!array.includes(el)) + array.push(el); +} +function string_template(text, props) { + return text.replace(/{(.+?)}/g, function(str, p) { + return props && props[p]; + }); +} +function remove(array, el) { + for (var i = array.length;--i >= 0; ) { + if (array[i] === el) + array.splice(i, 1); + } +} +function mergeSort(array, cmp) { + if (array.length < 2) + return array.slice(); + function merge(a, b) { + var r = [], ai = 0, bi = 0, i = 0; + while (ai < a.length && bi < b.length) { + cmp(a[ai], b[bi]) <= 0 ? r[i++] = a[ai++] : r[i++] = b[bi++]; + } + if (ai < a.length) + r.push.apply(r, a.slice(ai)); + if (bi < b.length) + r.push.apply(r, b.slice(bi)); + return r; + } + function _ms(a) { + if (a.length <= 1) + return a; + var m = Math.floor(a.length / 2), left = a.slice(0, m), right = a.slice(m); + left = _ms(left); + right = _ms(right); + return merge(left, right); + } + return _ms(array); +} +function makePredicate(words) { + if (!Array.isArray(words)) + words = words.split(" "); + return new Set(words.sort()); +} +function map_add(map, key, value) { + if (map.has(key)) { + map.get(key).push(value); + } else { + map.set(key, [value]); + } +} +function HOP(obj, prop) { + return Object.prototype.hasOwnProperty.call(obj, prop); +} +function keep_name(keep_setting, name) { + return keep_setting === true || keep_setting instanceof RegExp && keep_setting.test(name); +} +var lineTerminatorEscape = { + "\x00": "0", + "\n": "n", + "\r": "r", + "\u2028": "u2028", + "\u2029": "u2029" +}; +function regexp_source_fix(source) { + return source.replace(/[\0\n\r\u2028\u2029]/g, function(match, offset) { + var escaped = source[offset - 1] == "\\" && (source[offset - 2] != "\\" || /(?:^|[^\\])(?:\\{2})*$/.test(source.slice(0, offset - 1))); + return (escaped ? "" : "\\") + lineTerminatorEscape[match]; + }); +} +var re_safe_regexp = /^[\\/|\0\s\w^$.[\]()]*$/; +var regexp_is_safe = (source) => re_safe_regexp.test(source); +var all_flags = "dgimsuyv"; +function sort_regexp_flags(flags) { + const existing_flags = new Set(flags.split("")); + let out = ""; + for (const flag of all_flags) { + if (existing_flags.has(flag)) { + out += flag; + existing_flags.delete(flag); + } + } + if (existing_flags.size) { + existing_flags.forEach((flag) => { + out += flag; + }); + } + return out; +} +function has_annotation(node, annotation) { + return node._annotations & annotation; +} +function set_annotation(node, annotation) { + node._annotations |= annotation; +} + +// node_modules/terser/lib/parse.js +var LATEST_RAW = ""; +var TEMPLATE_RAWS = new Map; +var KEYWORDS = "break case catch class const continue debugger default delete do else export extends finally for function if in instanceof let new return switch throw try typeof var void while with"; +var KEYWORDS_ATOM = "false null true"; +var RESERVED_WORDS = "enum import super this " + KEYWORDS_ATOM + " " + KEYWORDS; +var ALL_RESERVED_WORDS = "implements interface package private protected public static " + RESERVED_WORDS; +var KEYWORDS_BEFORE_EXPRESSION = "return new delete throw else case yield await"; +KEYWORDS = makePredicate(KEYWORDS); +RESERVED_WORDS = makePredicate(RESERVED_WORDS); +KEYWORDS_BEFORE_EXPRESSION = makePredicate(KEYWORDS_BEFORE_EXPRESSION); +KEYWORDS_ATOM = makePredicate(KEYWORDS_ATOM); +ALL_RESERVED_WORDS = makePredicate(ALL_RESERVED_WORDS); +var OPERATOR_CHARS = makePredicate(characters("+-*&%=<>!?|~^")); +var RE_HEX_NUMBER = /^0x[0-9a-f]+$/i; +var RE_OCT_NUMBER = /^0[0-7]+$/; +var RE_ES6_OCT_NUMBER = /^0o[0-7]+$/i; +var RE_BIN_NUMBER = /^0b[01]+$/i; +var RE_DEC_NUMBER = /^\d*\.?\d*(?:e[+-]?\d*(?:\d\.?|\.?\d)\d*)?$/i; +var RE_BIG_INT = /^(0[xob])?[0-9a-f]+n$/i; +var OPERATORS = makePredicate([ + "in", + "instanceof", + "typeof", + "new", + "void", + "delete", + "++", + "--", + "+", + "-", + "!", + "~", + "&", + "|", + "^", + "*", + "**", + "/", + "%", + ">>", + "<<", + ">>>", + "<", + ">", + "<=", + ">=", + "==", + "===", + "!=", + "!==", + "?", + "=", + "+=", + "-=", + "||=", + "&&=", + "??=", + "/=", + "*=", + "**=", + "%=", + ">>=", + "<<=", + ">>>=", + "|=", + "^=", + "&=", + "&&", + "??", + "||" +]); +var WHITESPACE_CHARS = makePredicate(characters(`   +\r \f\v​           \u2028\u2029   \uFEFF`)); +var NEWLINE_CHARS = makePredicate(characters(` +\r\u2028\u2029`)); +var PUNC_AFTER_EXPRESSION = makePredicate(characters(";]),:")); +var PUNC_BEFORE_EXPRESSION = makePredicate(characters("[{(,;:")); +var PUNC_CHARS = makePredicate(characters("[]{}(),;:")); +var UNICODE = { + ID_Start: /[$A-Z_a-z\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376\u0377\u037A-\u037D\u037F\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u048A-\u052F\u0531-\u0556\u0559\u0561-\u0587\u05D0-\u05EA\u05F0-\u05F2\u0620-\u064A\u066E\u066F\u0671-\u06D3\u06D5\u06E5\u06E6\u06EE\u06EF\u06FA-\u06FC\u06FF\u0710\u0712-\u072F\u074D-\u07A5\u07B1\u07CA-\u07EA\u07F4\u07F5\u07FA\u0800-\u0815\u081A\u0824\u0828\u0840-\u0858\u08A0-\u08B4\u0904-\u0939\u093D\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BD\u09CE\u09DC\u09DD\u09DF-\u09E1\u09F0\u09F1\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A59-\u0A5C\u0A5E\u0A72-\u0A74\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABD\u0AD0\u0AE0\u0AE1\u0AF9\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3D\u0B5C\u0B5D\u0B5F-\u0B61\u0B71\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BD0\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C39\u0C3D\u0C58-\u0C5A\u0C60\u0C61\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBD\u0CDE\u0CE0\u0CE1\u0CF1\u0CF2\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D\u0D4E\u0D5F-\u0D61\u0D7A-\u0D7F\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0E01-\u0E30\u0E32\u0E33\u0E40-\u0E46\u0E81\u0E82\u0E84\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\u0EB0\u0EB2\u0EB3\u0EBD\u0EC0-\u0EC4\u0EC6\u0EDC-\u0EDF\u0F00\u0F40-\u0F47\u0F49-\u0F6C\u0F88-\u0F8C\u1000-\u102A\u103F\u1050-\u1055\u105A-\u105D\u1061\u1065\u1066\u106E-\u1070\u1075-\u1081\u108E\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u1380-\u138F\u13A0-\u13F5\u13F8-\u13FD\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16EE-\u16F8\u1700-\u170C\u170E-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D7\u17DC\u1820-\u1877\u1880-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191E\u1950-\u196D\u1970-\u1974\u1980-\u19AB\u19B0-\u19C9\u1A00-\u1A16\u1A20-\u1A54\u1AA7\u1B05-\u1B33\u1B45-\u1B4B\u1B83-\u1BA0\u1BAE\u1BAF\u1BBA-\u1BE5\u1C00-\u1C23\u1C4D-\u1C4F\u1C5A-\u1C7D\u1CE9-\u1CEC\u1CEE-\u1CF1\u1CF5\u1CF6\u1D00-\u1DBF\u1E00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2071\u207F\u2090-\u209C\u2102\u2107\u210A-\u2113\u2115\u2118-\u211D\u2124\u2126\u2128\u212A-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2160-\u2188\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CEE\u2CF2\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D80-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303C\u3041-\u3096\u309B-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312D\u3131-\u318E\u31A0-\u31BA\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FD5\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA61F\uA62A\uA62B\uA640-\uA66E\uA67F-\uA69D\uA6A0-\uA6EF\uA717-\uA71F\uA722-\uA788\uA78B-\uA7AD\uA7B0-\uA7B7\uA7F7-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA840-\uA873\uA882-\uA8B3\uA8F2-\uA8F7\uA8FB\uA8FD\uA90A-\uA925\uA930-\uA946\uA960-\uA97C\uA984-\uA9B2\uA9CF\uA9E0-\uA9E4\uA9E6-\uA9EF\uA9FA-\uA9FE\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA60-\uAA76\uAA7A\uAA7E-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAADD\uAAE0-\uAAEA\uAAF2-\uAAF4\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uAB30-\uAB5A\uAB5C-\uAB65\uAB70-\uABE2\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D\uFB1F-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE70-\uFE74\uFE76-\uFEFC\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]|\uD800[\uDC00-\uDC0B\uDC0D-\uDC26\uDC28-\uDC3A\uDC3C\uDC3D\uDC3F-\uDC4D\uDC50-\uDC5D\uDC80-\uDCFA\uDD40-\uDD74\uDE80-\uDE9C\uDEA0-\uDED0\uDF00-\uDF1F\uDF30-\uDF4A\uDF50-\uDF75\uDF80-\uDF9D\uDFA0-\uDFC3\uDFC8-\uDFCF\uDFD1-\uDFD5]|\uD801[\uDC00-\uDC9D\uDD00-\uDD27\uDD30-\uDD63\uDE00-\uDF36\uDF40-\uDF55\uDF60-\uDF67]|\uD802[\uDC00-\uDC05\uDC08\uDC0A-\uDC35\uDC37\uDC38\uDC3C\uDC3F-\uDC55\uDC60-\uDC76\uDC80-\uDC9E\uDCE0-\uDCF2\uDCF4\uDCF5\uDD00-\uDD15\uDD20-\uDD39\uDD80-\uDDB7\uDDBE\uDDBF\uDE00\uDE10-\uDE13\uDE15-\uDE17\uDE19-\uDE33\uDE60-\uDE7C\uDE80-\uDE9C\uDEC0-\uDEC7\uDEC9-\uDEE4\uDF00-\uDF35\uDF40-\uDF55\uDF60-\uDF72\uDF80-\uDF91]|\uD803[\uDC00-\uDC48\uDC80-\uDCB2\uDCC0-\uDCF2]|\uD804[\uDC03-\uDC37\uDC83-\uDCAF\uDCD0-\uDCE8\uDD03-\uDD26\uDD50-\uDD72\uDD76\uDD83-\uDDB2\uDDC1-\uDDC4\uDDDA\uDDDC\uDE00-\uDE11\uDE13-\uDE2B\uDE80-\uDE86\uDE88\uDE8A-\uDE8D\uDE8F-\uDE9D\uDE9F-\uDEA8\uDEB0-\uDEDE\uDF05-\uDF0C\uDF0F\uDF10\uDF13-\uDF28\uDF2A-\uDF30\uDF32\uDF33\uDF35-\uDF39\uDF3D\uDF50\uDF5D-\uDF61]|\uD805[\uDC80-\uDCAF\uDCC4\uDCC5\uDCC7\uDD80-\uDDAE\uDDD8-\uDDDB\uDE00-\uDE2F\uDE44\uDE80-\uDEAA\uDF00-\uDF19]|\uD806[\uDCA0-\uDCDF\uDCFF\uDEC0-\uDEF8]|\uD808[\uDC00-\uDF99]|\uD809[\uDC00-\uDC6E\uDC80-\uDD43]|[\uD80C\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872][\uDC00-\uDFFF]|\uD80D[\uDC00-\uDC2E]|\uD811[\uDC00-\uDE46]|\uD81A[\uDC00-\uDE38\uDE40-\uDE5E\uDED0-\uDEED\uDF00-\uDF2F\uDF40-\uDF43\uDF63-\uDF77\uDF7D-\uDF8F]|\uD81B[\uDF00-\uDF44\uDF50\uDF93-\uDF9F]|\uD82C[\uDC00\uDC01]|\uD82F[\uDC00-\uDC6A\uDC70-\uDC7C\uDC80-\uDC88\uDC90-\uDC99]|\uD835[\uDC00-\uDC54\uDC56-\uDC9C\uDC9E\uDC9F\uDCA2\uDCA5\uDCA6\uDCA9-\uDCAC\uDCAE-\uDCB9\uDCBB\uDCBD-\uDCC3\uDCC5-\uDD05\uDD07-\uDD0A\uDD0D-\uDD14\uDD16-\uDD1C\uDD1E-\uDD39\uDD3B-\uDD3E\uDD40-\uDD44\uDD46\uDD4A-\uDD50\uDD52-\uDEA5\uDEA8-\uDEC0\uDEC2-\uDEDA\uDEDC-\uDEFA\uDEFC-\uDF14\uDF16-\uDF34\uDF36-\uDF4E\uDF50-\uDF6E\uDF70-\uDF88\uDF8A-\uDFA8\uDFAA-\uDFC2\uDFC4-\uDFCB]|\uD83A[\uDC00-\uDCC4]|\uD83B[\uDE00-\uDE03\uDE05-\uDE1F\uDE21\uDE22\uDE24\uDE27\uDE29-\uDE32\uDE34-\uDE37\uDE39\uDE3B\uDE42\uDE47\uDE49\uDE4B\uDE4D-\uDE4F\uDE51\uDE52\uDE54\uDE57\uDE59\uDE5B\uDE5D\uDE5F\uDE61\uDE62\uDE64\uDE67-\uDE6A\uDE6C-\uDE72\uDE74-\uDE77\uDE79-\uDE7C\uDE7E\uDE80-\uDE89\uDE8B-\uDE9B\uDEA1-\uDEA3\uDEA5-\uDEA9\uDEAB-\uDEBB]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1]|\uD87E[\uDC00-\uDE1D]/, + ID_Continue: /(?:[$0-9A-Z_a-z\xAA\xB5\xB7\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0300-\u0374\u0376\u0377\u037A-\u037D\u037F\u0386-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u0483-\u0487\u048A-\u052F\u0531-\u0556\u0559\u0561-\u0587\u0591-\u05BD\u05BF\u05C1\u05C2\u05C4\u05C5\u05C7\u05D0-\u05EA\u05F0-\u05F2\u0610-\u061A\u0620-\u0669\u066E-\u06D3\u06D5-\u06DC\u06DF-\u06E8\u06EA-\u06FC\u06FF\u0710-\u074A\u074D-\u07B1\u07C0-\u07F5\u07FA\u0800-\u082D\u0840-\u085B\u08A0-\u08B4\u08E3-\u0963\u0966-\u096F\u0971-\u0983\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BC-\u09C4\u09C7\u09C8\u09CB-\u09CE\u09D7\u09DC\u09DD\u09DF-\u09E3\u09E6-\u09F1\u0A01-\u0A03\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A3C\u0A3E-\u0A42\u0A47\u0A48\u0A4B-\u0A4D\u0A51\u0A59-\u0A5C\u0A5E\u0A66-\u0A75\u0A81-\u0A83\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABC-\u0AC5\u0AC7-\u0AC9\u0ACB-\u0ACD\u0AD0\u0AE0-\u0AE3\u0AE6-\u0AEF\u0AF9\u0B01-\u0B03\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3C-\u0B44\u0B47\u0B48\u0B4B-\u0B4D\u0B56\u0B57\u0B5C\u0B5D\u0B5F-\u0B63\u0B66-\u0B6F\u0B71\u0B82\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BBE-\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCD\u0BD0\u0BD7\u0BE6-\u0BEF\u0C00-\u0C03\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C39\u0C3D-\u0C44\u0C46-\u0C48\u0C4A-\u0C4D\u0C55\u0C56\u0C58-\u0C5A\u0C60-\u0C63\u0C66-\u0C6F\u0C81-\u0C83\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBC-\u0CC4\u0CC6-\u0CC8\u0CCA-\u0CCD\u0CD5\u0CD6\u0CDE\u0CE0-\u0CE3\u0CE6-\u0CEF\u0CF1\u0CF2\u0D01-\u0D03\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D-\u0D44\u0D46-\u0D48\u0D4A-\u0D4E\u0D57\u0D5F-\u0D63\u0D66-\u0D6F\u0D7A-\u0D7F\u0D82\u0D83\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0DCA\u0DCF-\u0DD4\u0DD6\u0DD8-\u0DDF\u0DE6-\u0DEF\u0DF2\u0DF3\u0E01-\u0E3A\u0E40-\u0E4E\u0E50-\u0E59\u0E81\u0E82\u0E84\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\u0EB9\u0EBB-\u0EBD\u0EC0-\u0EC4\u0EC6\u0EC8-\u0ECD\u0ED0-\u0ED9\u0EDC-\u0EDF\u0F00\u0F18\u0F19\u0F20-\u0F29\u0F35\u0F37\u0F39\u0F3E-\u0F47\u0F49-\u0F6C\u0F71-\u0F84\u0F86-\u0F97\u0F99-\u0FBC\u0FC6\u1000-\u1049\u1050-\u109D\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u135D-\u135F\u1369-\u1371\u1380-\u138F\u13A0-\u13F5\u13F8-\u13FD\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16EE-\u16F8\u1700-\u170C\u170E-\u1714\u1720-\u1734\u1740-\u1753\u1760-\u176C\u176E-\u1770\u1772\u1773\u1780-\u17D3\u17D7\u17DC\u17DD\u17E0-\u17E9\u180B-\u180D\u1810-\u1819\u1820-\u1877\u1880-\u18AA\u18B0-\u18F5\u1900-\u191E\u1920-\u192B\u1930-\u193B\u1946-\u196D\u1970-\u1974\u1980-\u19AB\u19B0-\u19C9\u19D0-\u19DA\u1A00-\u1A1B\u1A20-\u1A5E\u1A60-\u1A7C\u1A7F-\u1A89\u1A90-\u1A99\u1AA7\u1AB0-\u1ABD\u1B00-\u1B4B\u1B50-\u1B59\u1B6B-\u1B73\u1B80-\u1BF3\u1C00-\u1C37\u1C40-\u1C49\u1C4D-\u1C7D\u1CD0-\u1CD2\u1CD4-\u1CF6\u1CF8\u1CF9\u1D00-\u1DF5\u1DFC-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u200C\u200D\u203F\u2040\u2054\u2071\u207F\u2090-\u209C\u20D0-\u20DC\u20E1\u20E5-\u20F0\u2102\u2107\u210A-\u2113\u2115\u2118-\u211D\u2124\u2126\u2128\u212A-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2160-\u2188\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D7F-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2DE0-\u2DFF\u3005-\u3007\u3021-\u302F\u3031-\u3035\u3038-\u303C\u3041-\u3096\u3099-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312D\u3131-\u318E\u31A0-\u31BA\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FD5\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA62B\uA640-\uA66F\uA674-\uA67D\uA67F-\uA6F1\uA717-\uA71F\uA722-\uA788\uA78B-\uA7AD\uA7B0-\uA7B7\uA7F7-\uA827\uA840-\uA873\uA880-\uA8C4\uA8D0-\uA8D9\uA8E0-\uA8F7\uA8FB\uA8FD\uA900-\uA92D\uA930-\uA953\uA960-\uA97C\uA980-\uA9C0\uA9CF-\uA9D9\uA9E0-\uA9FE\uAA00-\uAA36\uAA40-\uAA4D\uAA50-\uAA59\uAA60-\uAA76\uAA7A-\uAAC2\uAADB-\uAADD\uAAE0-\uAAEF\uAAF2-\uAAF6\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uAB30-\uAB5A\uAB5C-\uAB65\uAB70-\uABEA\uABEC\uABED\uABF0-\uABF9\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE00-\uFE0F\uFE20-\uFE2F\uFE33\uFE34\uFE4D-\uFE4F\uFE70-\uFE74\uFE76-\uFEFC\uFF10-\uFF19\uFF21-\uFF3A\uFF3F\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]|\uD800[\uDC00-\uDC0B\uDC0D-\uDC26\uDC28-\uDC3A\uDC3C\uDC3D\uDC3F-\uDC4D\uDC50-\uDC5D\uDC80-\uDCFA\uDD40-\uDD74\uDDFD\uDE80-\uDE9C\uDEA0-\uDED0\uDEE0\uDF00-\uDF1F\uDF30-\uDF4A\uDF50-\uDF7A\uDF80-\uDF9D\uDFA0-\uDFC3\uDFC8-\uDFCF\uDFD1-\uDFD5]|\uD801[\uDC00-\uDC9D\uDCA0-\uDCA9\uDD00-\uDD27\uDD30-\uDD63\uDE00-\uDF36\uDF40-\uDF55\uDF60-\uDF67]|\uD802[\uDC00-\uDC05\uDC08\uDC0A-\uDC35\uDC37\uDC38\uDC3C\uDC3F-\uDC55\uDC60-\uDC76\uDC80-\uDC9E\uDCE0-\uDCF2\uDCF4\uDCF5\uDD00-\uDD15\uDD20-\uDD39\uDD80-\uDDB7\uDDBE\uDDBF\uDE00-\uDE03\uDE05\uDE06\uDE0C-\uDE13\uDE15-\uDE17\uDE19-\uDE33\uDE38-\uDE3A\uDE3F\uDE60-\uDE7C\uDE80-\uDE9C\uDEC0-\uDEC7\uDEC9-\uDEE6\uDF00-\uDF35\uDF40-\uDF55\uDF60-\uDF72\uDF80-\uDF91]|\uD803[\uDC00-\uDC48\uDC80-\uDCB2\uDCC0-\uDCF2]|\uD804[\uDC00-\uDC46\uDC66-\uDC6F\uDC7F-\uDCBA\uDCD0-\uDCE8\uDCF0-\uDCF9\uDD00-\uDD34\uDD36-\uDD3F\uDD50-\uDD73\uDD76\uDD80-\uDDC4\uDDCA-\uDDCC\uDDD0-\uDDDA\uDDDC\uDE00-\uDE11\uDE13-\uDE37\uDE80-\uDE86\uDE88\uDE8A-\uDE8D\uDE8F-\uDE9D\uDE9F-\uDEA8\uDEB0-\uDEEA\uDEF0-\uDEF9\uDF00-\uDF03\uDF05-\uDF0C\uDF0F\uDF10\uDF13-\uDF28\uDF2A-\uDF30\uDF32\uDF33\uDF35-\uDF39\uDF3C-\uDF44\uDF47\uDF48\uDF4B-\uDF4D\uDF50\uDF57\uDF5D-\uDF63\uDF66-\uDF6C\uDF70-\uDF74]|\uD805[\uDC80-\uDCC5\uDCC7\uDCD0-\uDCD9\uDD80-\uDDB5\uDDB8-\uDDC0\uDDD8-\uDDDD\uDE00-\uDE40\uDE44\uDE50-\uDE59\uDE80-\uDEB7\uDEC0-\uDEC9\uDF00-\uDF19\uDF1D-\uDF2B\uDF30-\uDF39]|\uD806[\uDCA0-\uDCE9\uDCFF\uDEC0-\uDEF8]|\uD808[\uDC00-\uDF99]|\uD809[\uDC00-\uDC6E\uDC80-\uDD43]|[\uD80C\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872][\uDC00-\uDFFF]|\uD80D[\uDC00-\uDC2E]|\uD811[\uDC00-\uDE46]|\uD81A[\uDC00-\uDE38\uDE40-\uDE5E\uDE60-\uDE69\uDED0-\uDEED\uDEF0-\uDEF4\uDF00-\uDF36\uDF40-\uDF43\uDF50-\uDF59\uDF63-\uDF77\uDF7D-\uDF8F]|\uD81B[\uDF00-\uDF44\uDF50-\uDF7E\uDF8F-\uDF9F]|\uD82C[\uDC00\uDC01]|\uD82F[\uDC00-\uDC6A\uDC70-\uDC7C\uDC80-\uDC88\uDC90-\uDC99\uDC9D\uDC9E]|\uD834[\uDD65-\uDD69\uDD6D-\uDD72\uDD7B-\uDD82\uDD85-\uDD8B\uDDAA-\uDDAD\uDE42-\uDE44]|\uD835[\uDC00-\uDC54\uDC56-\uDC9C\uDC9E\uDC9F\uDCA2\uDCA5\uDCA6\uDCA9-\uDCAC\uDCAE-\uDCB9\uDCBB\uDCBD-\uDCC3\uDCC5-\uDD05\uDD07-\uDD0A\uDD0D-\uDD14\uDD16-\uDD1C\uDD1E-\uDD39\uDD3B-\uDD3E\uDD40-\uDD44\uDD46\uDD4A-\uDD50\uDD52-\uDEA5\uDEA8-\uDEC0\uDEC2-\uDEDA\uDEDC-\uDEFA\uDEFC-\uDF14\uDF16-\uDF34\uDF36-\uDF4E\uDF50-\uDF6E\uDF70-\uDF88\uDF8A-\uDFA8\uDFAA-\uDFC2\uDFC4-\uDFCB\uDFCE-\uDFFF]|\uD836[\uDE00-\uDE36\uDE3B-\uDE6C\uDE75\uDE84\uDE9B-\uDE9F\uDEA1-\uDEAF]|\uD83A[\uDC00-\uDCC4\uDCD0-\uDCD6]|\uD83B[\uDE00-\uDE03\uDE05-\uDE1F\uDE21\uDE22\uDE24\uDE27\uDE29-\uDE32\uDE34-\uDE37\uDE39\uDE3B\uDE42\uDE47\uDE49\uDE4B\uDE4D-\uDE4F\uDE51\uDE52\uDE54\uDE57\uDE59\uDE5B\uDE5D\uDE5F\uDE61\uDE62\uDE64\uDE67-\uDE6A\uDE6C-\uDE72\uDE74-\uDE77\uDE79-\uDE7C\uDE7E\uDE80-\uDE89\uDE8B-\uDE9B\uDEA1-\uDEA3\uDEA5-\uDEA9\uDEAB-\uDEBB]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1]|\uD87E[\uDC00-\uDE1D]|\uDB40[\uDD00-\uDDEF])+/ +}; +function get_full_char(str, pos) { + if (is_surrogate_pair_head(str.charCodeAt(pos))) { + if (is_surrogate_pair_tail(str.charCodeAt(pos + 1))) { + return str.charAt(pos) + str.charAt(pos + 1); + } + } else if (is_surrogate_pair_tail(str.charCodeAt(pos))) { + if (is_surrogate_pair_head(str.charCodeAt(pos - 1))) { + return str.charAt(pos - 1) + str.charAt(pos); + } + } + return str.charAt(pos); +} +function get_full_char_code(str, pos) { + if (is_surrogate_pair_head(str.charCodeAt(pos))) { + return 65536 + (str.charCodeAt(pos) - 55296 << 10) + str.charCodeAt(pos + 1) - 56320; + } + return str.charCodeAt(pos); +} +function get_full_char_length(str) { + var surrogates = 0; + for (var i = 0;i < str.length; i++) { + if (is_surrogate_pair_head(str.charCodeAt(i)) && is_surrogate_pair_tail(str.charCodeAt(i + 1))) { + surrogates++; + i++; + } + } + return str.length - surrogates; +} +function from_char_code(code) { + if (code > 65535) { + code -= 65536; + return String.fromCharCode((code >> 10) + 55296) + String.fromCharCode(code % 1024 + 56320); + } + return String.fromCharCode(code); +} +function is_surrogate_pair_head(code) { + return code >= 55296 && code <= 56319; +} +function is_surrogate_pair_tail(code) { + return code >= 56320 && code <= 57343; +} +function is_digit(code) { + return code >= 48 && code <= 57; +} +function is_identifier_start(ch) { + return UNICODE.ID_Start.test(ch); +} +function is_identifier_char(ch) { + return UNICODE.ID_Continue.test(ch); +} +var BASIC_IDENT = /^[a-z_$][a-z0-9_$]*$/i; +function is_basic_identifier_string(str) { + return BASIC_IDENT.test(str); +} +function is_identifier_string(str, allow_surrogates) { + if (BASIC_IDENT.test(str)) { + return true; + } + if (!allow_surrogates && /[\ud800-\udfff]/.test(str)) { + return false; + } + var match = UNICODE.ID_Start.exec(str); + if (!match || match.index !== 0) { + return false; + } + str = str.slice(match[0].length); + if (!str) { + return true; + } + match = UNICODE.ID_Continue.exec(str); + return !!match && match[0].length === str.length; +} +function parse_js_number(num, allow_e = true) { + if (!allow_e && num.includes("e")) { + return NaN; + } + if (RE_HEX_NUMBER.test(num)) { + return parseInt(num.substr(2), 16); + } else if (RE_OCT_NUMBER.test(num)) { + return parseInt(num.substr(1), 8); + } else if (RE_ES6_OCT_NUMBER.test(num)) { + return parseInt(num.substr(2), 8); + } else if (RE_BIN_NUMBER.test(num)) { + return parseInt(num.substr(2), 2); + } else if (RE_DEC_NUMBER.test(num)) { + return parseFloat(num); + } else { + var val = parseFloat(num); + if (val == num) + return val; + } +} + +class JS_Parse_Error extends Error { + constructor(message, filename, line, col, pos) { + super(); + this.name = "SyntaxError"; + this.message = message; + this.filename = filename; + this.line = line; + this.col = col; + this.pos = pos; + } +} +function js_error(message, filename, line, col, pos) { + throw new JS_Parse_Error(message, filename, line, col, pos); +} +function is_token(token, type, val) { + return token.type == type && (val == null || token.value == val); +} +var EX_EOF = {}; +function tokenizer($TEXT, filename, html5_comments, shebang) { + var S = { + text: $TEXT, + filename, + pos: 0, + tokpos: 0, + line: 1, + tokline: 0, + col: 0, + tokcol: 0, + newline_before: false, + regex_allowed: false, + brace_counter: 0, + template_braces: [], + comments_before: [], + directives: {}, + directive_stack: [] + }; + function peek() { + return get_full_char(S.text, S.pos); + } + function is_option_chain_op() { + const must_be_dot = S.text.charCodeAt(S.pos + 1) === 46; + if (!must_be_dot) + return false; + const cannot_be_digit = S.text.charCodeAt(S.pos + 2); + return cannot_be_digit < 48 || cannot_be_digit > 57; + } + function next(signal_eof, in_string) { + var ch = get_full_char(S.text, S.pos++); + if (signal_eof && !ch) + throw EX_EOF; + if (NEWLINE_CHARS.has(ch)) { + S.newline_before = S.newline_before || !in_string; + ++S.line; + S.col = 0; + if (ch == "\r" && peek() == ` +`) { + ++S.pos; + ch = ` +`; + } + } else { + if (ch.length > 1) { + ++S.pos; + ++S.col; + } + ++S.col; + } + return ch; + } + function forward(i) { + while (i--) + next(); + } + function looking_at(str) { + return S.text.substr(S.pos, str.length) == str; + } + function find_eol() { + var text = S.text; + for (var i = S.pos, n = S.text.length;i < n; ++i) { + var ch = text[i]; + if (NEWLINE_CHARS.has(ch)) + return i; + } + return -1; + } + function find(what, signal_eof) { + var pos = S.text.indexOf(what, S.pos); + if (signal_eof && pos == -1) + throw EX_EOF; + return pos; + } + function start_token() { + S.tokline = S.line; + S.tokcol = S.col; + S.tokpos = S.pos; + } + var prev_was_dot = false; + var previous_token = null; + function token(type, value, is_comment) { + S.regex_allowed = type == "operator" && !UNARY_POSTFIX.has(value) || type == "keyword" && KEYWORDS_BEFORE_EXPRESSION.has(value) || type == "punc" && PUNC_BEFORE_EXPRESSION.has(value) || type == "arrow"; + if (type == "punc" && (value == "." || value == "?.")) { + prev_was_dot = true; + } else if (!is_comment) { + prev_was_dot = false; + } + const line = S.tokline; + const col = S.tokcol; + const pos = S.tokpos; + const nlb = S.newline_before; + const file = filename; + let comments_before = []; + let comments_after = []; + if (!is_comment) { + comments_before = S.comments_before; + comments_after = S.comments_before = []; + } + S.newline_before = false; + const tok = new AST_Token(type, value, line, col, pos, nlb, comments_before, comments_after, file); + if (!is_comment) + previous_token = tok; + return tok; + } + function skip_whitespace() { + while (WHITESPACE_CHARS.has(peek())) + next(); + } + function read_while(pred) { + var ret = "", ch, i = 0; + while ((ch = peek()) && pred(ch, i++)) + ret += next(); + return ret; + } + function parse_error(err) { + js_error(err, filename, S.tokline, S.tokcol, S.tokpos); + } + function read_num(prefix) { + var has_e = false, after_e = false, has_x = false, has_dot = prefix == ".", is_big_int = false, numeric_separator = false; + var num = read_while(function(ch, i) { + if (is_big_int) + return false; + var code = ch.charCodeAt(0); + switch (code) { + case 95: + return numeric_separator = true; + case 98: + case 66: + return has_x = true; + case 111: + case 79: + case 120: + case 88: + return has_x ? false : has_x = true; + case 101: + case 69: + return has_x ? true : has_e ? false : has_e = after_e = true; + case 45: + return after_e || i == 0 && !prefix; + case 43: + return after_e; + case (after_e = false, 46): + return !has_dot && !has_x && !has_e ? has_dot = true : false; + case 110: + is_big_int = true; + return true; + } + return code >= 48 && code <= 57 || code >= 97 && code <= 102 || code >= 65 && code <= 70; + }); + if (prefix) + num = prefix + num; + LATEST_RAW = num; + if (RE_OCT_NUMBER.test(num) && next_token.has_directive("use strict")) { + parse_error("Legacy octal literals are not allowed in strict mode"); + } + if (numeric_separator) { + if (num.endsWith("_")) { + parse_error("Numeric separators are not allowed at the end of numeric literals"); + } else if (num.includes("__")) { + parse_error("Only one underscore is allowed as numeric separator"); + } + num = num.replace(/_/g, ""); + } + if (is_big_int) { + const without_n = num.slice(0, -1); + const allow_e = RE_HEX_NUMBER.test(without_n); + const valid2 = parse_js_number(without_n, allow_e); + if (!has_dot && RE_BIG_INT.test(num) && !isNaN(valid2)) + return token("big_int", without_n); + parse_error("Invalid or unexpected token"); + } + var valid = parse_js_number(num); + if (!isNaN(valid)) { + return token("num", valid); + } else { + parse_error("Invalid syntax: " + num); + } + } + function is_octal(ch) { + return ch >= "0" && ch <= "7"; + } + function read_escaped_char(in_string, strict_hex, template_string) { + var ch = next(true, in_string); + switch (ch.charCodeAt(0)) { + case 110: + return ` +`; + case 114: + return "\r"; + case 116: + return "\t"; + case 98: + return "\b"; + case 118: + return "\v"; + case 102: + return "\f"; + case 120: + return String.fromCharCode(hex_bytes(2, strict_hex)); + case 117: + if (peek() == "{") { + next(true); + if (peek() === "}") + parse_error("Expecting hex-character between {}"); + while (peek() == "0") + next(true); + var result, length = find("}", true) - S.pos; + if (length > 6 || (result = hex_bytes(length, strict_hex)) > 1114111) { + parse_error("Unicode reference out of bounds"); + } + next(true); + return from_char_code(result); + } + return String.fromCharCode(hex_bytes(4, strict_hex)); + case 10: + return ""; + case 13: + if (peek() == ` +`) { + next(true, in_string); + return ""; + } + } + if (is_octal(ch)) { + if (template_string && strict_hex) { + const represents_null_character = ch === "0" && !is_octal(peek()); + if (!represents_null_character) { + parse_error("Octal escape sequences are not allowed in template strings"); + } + } + return read_octal_escape_sequence(ch, strict_hex); + } + return ch; + } + function read_octal_escape_sequence(ch, strict_octal) { + var p = peek(); + if (p >= "0" && p <= "7") { + ch += next(true); + if (ch[0] <= "3" && (p = peek()) >= "0" && p <= "7") + ch += next(true); + } + if (ch === "0") + return "\x00"; + if (ch.length > 0 && next_token.has_directive("use strict") && strict_octal) + parse_error("Legacy octal escape sequences are not allowed in strict mode"); + return String.fromCharCode(parseInt(ch, 8)); + } + function hex_bytes(n, strict_hex) { + var num = 0; + for (;n > 0; --n) { + if (!strict_hex && isNaN(parseInt(peek(), 16))) { + return parseInt(num, 16) || ""; + } + var digit = next(true); + if (isNaN(parseInt(digit, 16))) + parse_error("Invalid hex-character pattern in string"); + num += digit; + } + return parseInt(num, 16); + } + var read_string = with_eof_error("Unterminated string constant", function() { + const start_pos = S.pos; + var quote = next(), ret = []; + for (;; ) { + var ch = next(true, true); + if (ch == "\\") + ch = read_escaped_char(true, true); + else if (ch == "\r" || ch == ` +`) + parse_error("Unterminated string constant"); + else if (ch == quote) + break; + ret.push(ch); + } + var tok = token("string", ret.join("")); + LATEST_RAW = S.text.slice(start_pos, S.pos); + tok.quote = quote; + return tok; + }); + var read_template_characters = with_eof_error("Unterminated template", function(begin) { + if (begin) { + S.template_braces.push(S.brace_counter); + } + var content = "", raw = "", ch, tok; + next(true, true); + while ((ch = next(true, true)) != "`") { + if (ch == "\r") { + if (peek() == ` +`) + ++S.pos; + ch = ` +`; + } else if (ch == "$" && peek() == "{") { + next(true, true); + S.brace_counter++; + tok = token(begin ? "template_head" : "template_cont", content); + TEMPLATE_RAWS.set(tok, raw); + tok.template_end = false; + return tok; + } + raw += ch; + if (ch == "\\") { + var tmp = S.pos; + var prev_is_tag = previous_token && (previous_token.type === "name" || previous_token.type === "punc" && (previous_token.value === ")" || previous_token.value === "]")); + ch = read_escaped_char(true, !prev_is_tag, true); + raw += S.text.substr(tmp, S.pos - tmp); + } + content += ch; + } + S.template_braces.pop(); + tok = token(begin ? "template_head" : "template_cont", content); + TEMPLATE_RAWS.set(tok, raw); + tok.template_end = true; + return tok; + }); + function skip_line_comment(type) { + var regex_allowed = S.regex_allowed; + var i = find_eol(), ret; + if (i == -1) { + ret = S.text.substr(S.pos); + S.pos = S.text.length; + } else { + ret = S.text.substring(S.pos, i); + S.pos = i; + } + S.col = S.tokcol + (S.pos - S.tokpos); + S.comments_before.push(token(type, ret, true)); + S.regex_allowed = regex_allowed; + return next_token; + } + var skip_multiline_comment = with_eof_error("Unterminated multiline comment", function() { + var regex_allowed = S.regex_allowed; + var i = find("*/", true); + var text = S.text.substring(S.pos, i).replace(/\r\n|\r|\u2028|\u2029/g, ` +`); + forward(get_full_char_length(text) + 2); + S.comments_before.push(token("comment2", text, true)); + S.newline_before = S.newline_before || text.includes(` +`); + S.regex_allowed = regex_allowed; + return next_token; + }); + var read_name = function() { + let start = S.pos, end = start - 1, ch = "c"; + while ((ch = S.text.charAt(++end)) && (ch >= "a" && ch <= "z" || ch >= "A" && ch <= "Z")) + ; + if (end > start + 1 && ch && ch !== "\\" && !is_identifier_char(ch)) { + S.pos += end - start; + S.col += end - start; + return S.text.slice(start, S.pos); + } + return read_name_hard(); + }; + var read_name_hard = with_eof_error("Unterminated identifier name", function() { + var name = [], ch, escaped = false; + var read_escaped_identifier_char = function() { + escaped = true; + next(); + if (peek() !== "u") { + parse_error("Expecting UnicodeEscapeSequence -- uXXXX or u{XXXX}"); + } + return read_escaped_char(false, true); + }; + if ((ch = peek()) === "\\") { + ch = read_escaped_identifier_char(); + if (!is_identifier_start(ch)) { + parse_error("First identifier char is an invalid identifier char"); + } + } else if (is_identifier_start(ch)) { + next(); + } else { + return ""; + } + name.push(ch); + while ((ch = peek()) != null) { + if ((ch = peek()) === "\\") { + ch = read_escaped_identifier_char(); + if (!is_identifier_char(ch)) { + parse_error("Invalid escaped identifier char"); + } + } else { + if (!is_identifier_char(ch)) { + break; + } + next(); + } + name.push(ch); + } + const name_str = name.join(""); + if (RESERVED_WORDS.has(name_str) && escaped) { + parse_error("Escaped characters are not allowed in keywords"); + } + return name_str; + }); + var read_regexp = with_eof_error("Unterminated regular expression", function(source) { + var prev_backslash = false, ch, in_class = false; + while (ch = next(true)) + if (NEWLINE_CHARS.has(ch)) { + parse_error("Unexpected line terminator"); + } else if (prev_backslash) { + if (/^[\u0000-\u007F]$/.test(ch)) { + source += "\\" + ch; + } else { + source += ch; + } + prev_backslash = false; + } else if (ch == "[") { + in_class = true; + source += ch; + } else if (ch == "]" && in_class) { + in_class = false; + source += ch; + } else if (ch == "/" && !in_class) { + break; + } else if (ch == "\\") { + prev_backslash = true; + } else { + source += ch; + } + const flags = read_name(); + return token("regexp", "/" + source + "/" + flags); + }); + function read_operator(prefix) { + function grow(op) { + if (!peek()) + return op; + var bigger = op + peek(); + if (OPERATORS.has(bigger)) { + next(); + return grow(bigger); + } else { + return op; + } + } + return token("operator", grow(prefix || next())); + } + function handle_slash() { + next(); + switch (peek()) { + case "/": + next(); + return skip_line_comment("comment1"); + case "*": + next(); + return skip_multiline_comment(); + } + return S.regex_allowed ? read_regexp("") : read_operator("/"); + } + function handle_eq_sign() { + next(); + if (peek() === ">") { + next(); + return token("arrow", "=>"); + } else { + return read_operator("="); + } + } + function handle_dot() { + next(); + if (is_digit(peek().charCodeAt(0))) { + return read_num("."); + } + if (peek() === ".") { + next(); + next(); + return token("expand", "..."); + } + return token("punc", "."); + } + function read_word() { + var word = read_name(); + if (prev_was_dot) + return token("name", word); + return KEYWORDS_ATOM.has(word) ? token("atom", word) : !KEYWORDS.has(word) ? token("name", word) : OPERATORS.has(word) ? token("operator", word) : token("keyword", word); + } + function read_private_word() { + next(); + return token("privatename", read_name()); + } + function with_eof_error(eof_error, cont) { + return function(x) { + try { + return cont(x); + } catch (ex) { + if (ex === EX_EOF) + parse_error(eof_error); + else + throw ex; + } + }; + } + function next_token(force_regexp) { + if (force_regexp != null) + return read_regexp(force_regexp); + if (shebang && S.pos == 0 && looking_at("#!")) { + start_token(); + forward(2); + skip_line_comment("comment5"); + } + for (;; ) { + skip_whitespace(); + start_token(); + if (html5_comments) { + if (looking_at("") && S.newline_before) { + forward(3); + skip_line_comment("comment4"); + continue; + } + } + var ch = peek(); + if (!ch) + return token("eof"); + var code = ch.charCodeAt(0); + switch (code) { + case 34: + case 39: + return read_string(); + case 46: + return handle_dot(); + case 47: { + var tok = handle_slash(); + if (tok === next_token) + continue; + return tok; + } + case 61: + return handle_eq_sign(); + case 63: { + if (!is_option_chain_op()) + break; + next(); + next(); + return token("punc", "?."); + } + case 96: + return read_template_characters(true); + case 123: + S.brace_counter++; + break; + case 125: + S.brace_counter--; + if (S.template_braces.length > 0 && S.template_braces[S.template_braces.length - 1] === S.brace_counter) + return read_template_characters(false); + break; + } + if (is_digit(code)) + return read_num(); + if (PUNC_CHARS.has(ch)) + return token("punc", next()); + if (OPERATOR_CHARS.has(ch)) + return read_operator(); + if (code == 92 || is_identifier_start(ch)) + return read_word(); + if (code == 35) + return read_private_word(); + break; + } + parse_error("Unexpected character '" + ch + "'"); + } + next_token.next = next; + next_token.peek = peek; + next_token.context = function(nc) { + if (nc) + S = nc; + return S; + }; + next_token.add_directive = function(directive) { + S.directive_stack[S.directive_stack.length - 1].push(directive); + if (S.directives[directive] === undefined) { + S.directives[directive] = 1; + } else { + S.directives[directive]++; + } + }; + next_token.push_directives_stack = function() { + S.directive_stack.push([]); + }; + next_token.pop_directives_stack = function() { + var directives = S.directive_stack[S.directive_stack.length - 1]; + for (var i = 0;i < directives.length; i++) { + S.directives[directives[i]]--; + } + S.directive_stack.pop(); + }; + next_token.has_directive = function(directive) { + return S.directives[directive] > 0; + }; + return next_token; +} +var UNARY_PREFIX = makePredicate([ + "typeof", + "void", + "delete", + "--", + "++", + "!", + "~", + "-", + "+" +]); +var UNARY_POSTFIX = makePredicate(["--", "++"]); +var ASSIGNMENT = makePredicate(["=", "+=", "-=", "??=", "&&=", "||=", "/=", "*=", "**=", "%=", ">>=", "<<=", ">>>=", "|=", "^=", "&="]); +var LOGICAL_ASSIGNMENT = makePredicate(["??=", "&&=", "||="]); +var PRECEDENCE = function(a, ret) { + for (var i = 0;i < a.length; ++i) { + for (const op of a[i]) { + ret[op] = i + 1; + } + } + return ret; +}([ + ["||"], + ["??"], + ["&&"], + ["|"], + ["^"], + ["&"], + ["==", "===", "!=", "!=="], + ["<", ">", "<=", ">=", "in", "instanceof"], + [">>", "<<", ">>>"], + ["+", "-"], + ["*", "/", "%"], + ["**"] +], {}); +var ATOMIC_START_TOKEN = makePredicate(["atom", "num", "big_int", "string", "regexp", "name"]); +function parse($TEXT, options) { + const outer_comments_before_counts = new WeakMap; + options = defaults(options, { + bare_returns: false, + ecma: null, + expression: false, + filename: null, + html5_comments: true, + module: false, + shebang: true, + strict: false, + toplevel: null + }, true); + var S = { + input: typeof $TEXT == "string" ? tokenizer($TEXT, options.filename, options.html5_comments, options.shebang) : $TEXT, + token: null, + prev: null, + peeked: null, + in_function: 0, + in_async: -1, + in_generator: -1, + in_directives: true, + in_loop: 0, + labels: [] + }; + S.token = next(); + function is(type, value) { + return is_token(S.token, type, value); + } + function peek() { + return S.peeked || (S.peeked = S.input()); + } + function next() { + S.prev = S.token; + if (!S.peeked) + peek(); + S.token = S.peeked; + S.peeked = null; + S.in_directives = S.in_directives && (S.token.type == "string" || is("punc", ";")); + return S.token; + } + function prev() { + return S.prev; + } + function croak(msg, line, col, pos) { + var ctx = S.input.context(); + js_error(msg, ctx.filename, line != null ? line : ctx.tokline, col != null ? col : ctx.tokcol, pos != null ? pos : ctx.tokpos); + } + function token_error(token, msg) { + croak(msg, token.line, token.col); + } + function unexpected(token) { + if (token == null) + token = S.token; + token_error(token, "Unexpected token: " + token.type + " (" + token.value + ")"); + } + function expect_token(type, val) { + if (is(type, val)) { + return next(); + } + token_error(S.token, "Unexpected token " + S.token.type + " «" + S.token.value + "»" + ", expected " + type + " «" + val + "»"); + } + function expect(punc) { + return expect_token("punc", punc); + } + function has_newline_before(token) { + return token.nlb || !token.comments_before.every((comment) => !comment.nlb); + } + function can_insert_semicolon() { + return !options.strict && (is("eof") || is("punc", "}") || has_newline_before(S.token)); + } + function is_in_generator() { + return S.in_generator === S.in_function; + } + function is_in_async() { + return S.in_async === S.in_function; + } + function can_await() { + return S.in_async === S.in_function || S.in_function === 0 && S.input.has_directive("use strict"); + } + function semicolon(optional) { + if (is("punc", ";")) + next(); + else if (!optional && !can_insert_semicolon()) + unexpected(); + } + function parenthesised() { + expect("("); + var exp = expression(true); + expect(")"); + return exp; + } + function embed_tokens(parser) { + return function _embed_tokens_wrapper(...args) { + const start = S.token; + const expr = parser(...args); + expr.start = start; + expr.end = prev(); + return expr; + }; + } + function handle_regexp() { + if (is("operator", "/") || is("operator", "/=")) { + S.peeked = null; + S.token = S.input(S.token.value.substr(1)); + } + } + var statement = embed_tokens(function statement(is_export_default, is_for_body, is_if_body) { + handle_regexp(); + switch (S.token.type) { + case "string": + if (S.in_directives) { + var token = peek(); + if (!LATEST_RAW.includes("\\") && (is_token(token, "punc", ";") || is_token(token, "punc", "}") || has_newline_before(token) || is_token(token, "eof"))) { + S.input.add_directive(S.token.value); + } else { + S.in_directives = false; + } + } + var dir = S.in_directives, stat = simple_statement(); + return dir && stat.body instanceof AST_String ? new AST_Directive(stat.body) : stat; + case "template_head": + case "num": + case "big_int": + case "regexp": + case "operator": + case "atom": + return simple_statement(); + case "name": + case "privatename": + if (is("privatename") && !S.in_class) + croak("Private field must be used in an enclosing class"); + if (S.token.value == "async" && is_token(peek(), "keyword", "function")) { + next(); + next(); + if (is_for_body) { + croak("functions are not allowed as the body of a loop"); + } + return function_(AST_Defun, false, true, is_export_default); + } + if (S.token.value == "import" && !is_token(peek(), "punc", "(") && !is_token(peek(), "punc", ".")) { + next(); + var node = import_statement(); + semicolon(); + return node; + } + return is_token(peek(), "punc", ":") ? labeled_statement() : simple_statement(); + case "punc": + switch (S.token.value) { + case "{": + return new AST_BlockStatement({ + start: S.token, + body: block_(), + end: prev() + }); + case "[": + case "(": + return simple_statement(); + case ";": + S.in_directives = false; + next(); + return new AST_EmptyStatement; + default: + unexpected(); + } + case "keyword": + switch (S.token.value) { + case "break": + next(); + return break_cont(AST_Break); + case "continue": + next(); + return break_cont(AST_Continue); + case "debugger": + next(); + semicolon(); + return new AST_Debugger; + case "do": + next(); + var body = in_loop(statement); + expect_token("keyword", "while"); + var condition = parenthesised(); + semicolon(true); + return new AST_Do({ + body, + condition + }); + case "while": + next(); + return new AST_While({ + condition: parenthesised(), + body: in_loop(function() { + return statement(false, true); + }) + }); + case "for": + next(); + return for_(); + case "class": + next(); + if (is_for_body) { + croak("classes are not allowed as the body of a loop"); + } + if (is_if_body) { + croak("classes are not allowed as the body of an if"); + } + return class_(AST_DefClass, is_export_default); + case "function": + next(); + if (is_for_body) { + croak("functions are not allowed as the body of a loop"); + } + return function_(AST_Defun, false, false, is_export_default); + case "if": + next(); + return if_(); + case "return": + if (S.in_function == 0 && !options.bare_returns) + croak("'return' outside of function"); + next(); + var value = null; + if (is("punc", ";")) { + next(); + } else if (!can_insert_semicolon()) { + value = expression(true); + semicolon(); + } + return new AST_Return({ + value + }); + case "switch": + next(); + return new AST_Switch({ + expression: parenthesised(), + body: in_loop(switch_body_) + }); + case "throw": + next(); + if (has_newline_before(S.token)) + croak("Illegal newline after 'throw'"); + var value = expression(true); + semicolon(); + return new AST_Throw({ + value + }); + case "try": + next(); + return try_(); + case "var": + next(); + var node = var_(); + semicolon(); + return node; + case "let": + next(); + var node = let_(); + semicolon(); + return node; + case "const": + next(); + var node = const_(); + semicolon(); + return node; + case "with": + if (S.input.has_directive("use strict")) { + croak("Strict mode may not include a with statement"); + } + next(); + return new AST_With({ + expression: parenthesised(), + body: statement() + }); + case "export": + if (!is_token(peek(), "punc", "(")) { + next(); + var node = export_statement(); + if (is("punc", ";")) + semicolon(); + return node; + } + } + } + unexpected(); + }); + function labeled_statement() { + var label = as_symbol(AST_Label); + if (label.name === "await" && is_in_async()) { + token_error(S.prev, "await cannot be used as label inside async function"); + } + if (S.labels.some((l) => l.name === label.name)) { + croak("Label " + label.name + " defined twice"); + } + expect(":"); + S.labels.push(label); + var stat = statement(); + S.labels.pop(); + if (!(stat instanceof AST_IterationStatement)) { + label.references.forEach(function(ref) { + if (ref instanceof AST_Continue) { + ref = ref.label.start; + croak("Continue label `" + label.name + "` refers to non-IterationStatement.", ref.line, ref.col, ref.pos); + } + }); + } + return new AST_LabeledStatement({ body: stat, label }); + } + function simple_statement(tmp) { + return new AST_SimpleStatement({ body: (tmp = expression(true), semicolon(), tmp) }); + } + function break_cont(type) { + var label = null, ldef; + if (!can_insert_semicolon()) { + label = as_symbol(AST_LabelRef, true); + } + if (label != null) { + ldef = S.labels.find((l) => l.name === label.name); + if (!ldef) + croak("Undefined label " + label.name); + label.thedef = ldef; + } else if (S.in_loop == 0) + croak(type.TYPE + " not inside a loop or switch"); + semicolon(); + var stat = new type({ label }); + if (ldef) + ldef.references.push(stat); + return stat; + } + function for_() { + var for_await_error = "`for await` invalid in this context"; + var await_tok = S.token; + if (await_tok.type == "name" && await_tok.value == "await") { + if (!can_await()) { + token_error(await_tok, for_await_error); + } + next(); + } else { + await_tok = false; + } + expect("("); + var init = null; + if (!is("punc", ";")) { + init = is("keyword", "var") ? (next(), var_(true)) : is("keyword", "let") ? (next(), let_(true)) : is("keyword", "const") ? (next(), const_(true)) : expression(true, true); + var is_in = is("operator", "in"); + var is_of = is("name", "of"); + if (await_tok && !is_of) { + token_error(await_tok, for_await_error); + } + if (is_in || is_of) { + if (init instanceof AST_Definitions) { + if (init.definitions.length > 1) + token_error(init.start, "Only one variable declaration allowed in for..in loop"); + } else if (!(is_assignable(init) || (init = to_destructuring(init)) instanceof AST_Destructuring)) { + token_error(init.start, "Invalid left-hand side in for..in loop"); + } + next(); + if (is_in) { + return for_in(init); + } else { + return for_of(init, !!await_tok); + } + } + } else if (await_tok) { + token_error(await_tok, for_await_error); + } + return regular_for(init); + } + function regular_for(init) { + expect(";"); + var test = is("punc", ";") ? null : expression(true); + expect(";"); + var step = is("punc", ")") ? null : expression(true); + expect(")"); + return new AST_For({ + init, + condition: test, + step, + body: in_loop(function() { + return statement(false, true); + }) + }); + } + function for_of(init, is_await) { + var lhs = init instanceof AST_Definitions ? init.definitions[0].name : null; + var obj = expression(true); + expect(")"); + return new AST_ForOf({ + await: is_await, + init, + name: lhs, + object: obj, + body: in_loop(function() { + return statement(false, true); + }) + }); + } + function for_in(init) { + var obj = expression(true); + expect(")"); + return new AST_ForIn({ + init, + object: obj, + body: in_loop(function() { + return statement(false, true); + }) + }); + } + var arrow_function = function(start, argnames, is_async) { + if (has_newline_before(S.token)) { + croak("Unexpected newline before arrow (=>)"); + } + expect_token("arrow", "=>"); + var body = _function_body(is("punc", "{"), false, is_async); + return new AST_Arrow({ + start, + end: body.end, + async: is_async, + argnames, + body + }); + }; + var function_ = function(ctor, is_generator, is_async, is_export_default) { + var in_statement = ctor === AST_Defun; + if (is("operator", "*")) { + is_generator = true; + next(); + } + var name = is("name") ? as_symbol(in_statement ? AST_SymbolDefun : AST_SymbolLambda) : null; + if (in_statement && !name) { + if (is_export_default) { + ctor = AST_Function; + } else { + unexpected(); + } + } + if (name && ctor !== AST_Accessor && !(name instanceof AST_SymbolDeclaration)) + unexpected(prev()); + var args = []; + var body = _function_body(true, is_generator, is_async, name, args); + return new ctor({ + start: args.start, + end: body.end, + is_generator, + async: is_async, + name, + argnames: args, + body + }); + }; + + class UsedParametersTracker { + constructor(is_parameter, strict, duplicates_ok = false) { + this.is_parameter = is_parameter; + this.duplicates_ok = duplicates_ok; + this.parameters = new Set; + this.duplicate = null; + this.default_assignment = false; + this.spread = false; + this.strict_mode = !!strict; + } + add_parameter(token) { + if (this.parameters.has(token.value)) { + if (this.duplicate === null) { + this.duplicate = token; + } + this.check_strict(); + } else { + this.parameters.add(token.value); + if (this.is_parameter) { + switch (token.value) { + case "arguments": + case "eval": + case "yield": + if (this.strict_mode) { + token_error(token, "Unexpected " + token.value + " identifier as parameter inside strict mode"); + } + break; + default: + if (RESERVED_WORDS.has(token.value)) { + unexpected(); + } + } + } + } + } + mark_default_assignment(token) { + if (this.default_assignment === false) { + this.default_assignment = token; + } + } + mark_spread(token) { + if (this.spread === false) { + this.spread = token; + } + } + mark_strict_mode() { + this.strict_mode = true; + } + is_strict() { + return this.default_assignment !== false || this.spread !== false || this.strict_mode; + } + check_strict() { + if (this.is_strict() && this.duplicate !== null && !this.duplicates_ok) { + token_error(this.duplicate, "Parameter " + this.duplicate.value + " was used already"); + } + } + } + function parameters(params) { + var used_parameters = new UsedParametersTracker(true, S.input.has_directive("use strict")); + expect("("); + while (!is("punc", ")")) { + var param = parameter(used_parameters); + params.push(param); + if (!is("punc", ")")) { + expect(","); + } + if (param instanceof AST_Expansion) { + break; + } + } + next(); + } + function parameter(used_parameters, symbol_type) { + var param; + var expand = false; + if (used_parameters === undefined) { + used_parameters = new UsedParametersTracker(true, S.input.has_directive("use strict")); + } + if (is("expand", "...")) { + expand = S.token; + used_parameters.mark_spread(S.token); + next(); + } + param = binding_element(used_parameters, symbol_type); + if (is("operator", "=") && expand === false) { + used_parameters.mark_default_assignment(S.token); + next(); + param = new AST_DefaultAssign({ + start: param.start, + left: param, + operator: "=", + right: expression(false), + end: S.token + }); + } + if (expand !== false) { + if (!is("punc", ")")) { + unexpected(); + } + param = new AST_Expansion({ + start: expand, + expression: param, + end: expand + }); + } + used_parameters.check_strict(); + return param; + } + function binding_element(used_parameters, symbol_type) { + var elements = []; + var first = true; + var is_expand = false; + var expand_token; + var first_token = S.token; + if (used_parameters === undefined) { + const strict = S.input.has_directive("use strict"); + const duplicates_ok = symbol_type === AST_SymbolVar; + used_parameters = new UsedParametersTracker(false, strict, duplicates_ok); + } + symbol_type = symbol_type === undefined ? AST_SymbolFunarg : symbol_type; + if (is("punc", "[")) { + next(); + while (!is("punc", "]")) { + if (first) { + first = false; + } else { + expect(","); + } + if (is("expand", "...")) { + is_expand = true; + expand_token = S.token; + used_parameters.mark_spread(S.token); + next(); + } + if (is("punc")) { + switch (S.token.value) { + case ",": + elements.push(new AST_Hole({ + start: S.token, + end: S.token + })); + continue; + case "]": + break; + case "[": + case "{": + elements.push(binding_element(used_parameters, symbol_type)); + break; + default: + unexpected(); + } + } else if (is("name")) { + used_parameters.add_parameter(S.token); + elements.push(as_symbol(symbol_type)); + } else { + croak("Invalid function parameter"); + } + if (is("operator", "=") && is_expand === false) { + used_parameters.mark_default_assignment(S.token); + next(); + elements[elements.length - 1] = new AST_DefaultAssign({ + start: elements[elements.length - 1].start, + left: elements[elements.length - 1], + operator: "=", + right: expression(false), + end: S.token + }); + } + if (is_expand) { + if (!is("punc", "]")) { + croak("Rest element must be last element"); + } + elements[elements.length - 1] = new AST_Expansion({ + start: expand_token, + expression: elements[elements.length - 1], + end: expand_token + }); + } + } + expect("]"); + used_parameters.check_strict(); + return new AST_Destructuring({ + start: first_token, + names: elements, + is_array: true, + end: prev() + }); + } else if (is("punc", "{")) { + next(); + while (!is("punc", "}")) { + if (first) { + first = false; + } else { + expect(","); + } + if (is("expand", "...")) { + is_expand = true; + expand_token = S.token; + used_parameters.mark_spread(S.token); + next(); + } + if (is("name") && (is_token(peek(), "punc") || is_token(peek(), "operator")) && [",", "}", "="].includes(peek().value)) { + used_parameters.add_parameter(S.token); + var start = prev(); + var value = as_symbol(symbol_type); + if (is_expand) { + elements.push(new AST_Expansion({ + start: expand_token, + expression: value, + end: value.end + })); + } else { + elements.push(new AST_ObjectKeyVal({ + start, + key: value.name, + value, + end: value.end + })); + } + } else if (is("punc", "}")) { + continue; + } else { + var property_token = S.token; + var property = as_property_name(); + if (property === null) { + unexpected(prev()); + } else if (prev().type === "name" && !is("punc", ":")) { + elements.push(new AST_ObjectKeyVal({ + start: prev(), + key: property, + value: new symbol_type({ + start: prev(), + name: property, + end: prev() + }), + end: prev() + })); + } else { + expect(":"); + elements.push(new AST_ObjectKeyVal({ + start: property_token, + quote: property_token.quote, + key: property, + value: binding_element(used_parameters, symbol_type), + end: prev() + })); + } + } + if (is_expand) { + if (!is("punc", "}")) { + croak("Rest element must be last element"); + } + } else if (is("operator", "=")) { + used_parameters.mark_default_assignment(S.token); + next(); + elements[elements.length - 1].value = new AST_DefaultAssign({ + start: elements[elements.length - 1].value.start, + left: elements[elements.length - 1].value, + operator: "=", + right: expression(false), + end: S.token + }); + } + } + expect("}"); + used_parameters.check_strict(); + return new AST_Destructuring({ + start: first_token, + names: elements, + is_array: false, + end: prev() + }); + } else if (is("name")) { + used_parameters.add_parameter(S.token); + return as_symbol(symbol_type); + } else { + croak("Invalid function parameter"); + } + } + function params_or_seq_(allow_arrows, maybe_sequence) { + var spread_token; + var invalid_sequence; + var trailing_comma; + var a = []; + expect("("); + while (!is("punc", ")")) { + if (spread_token) + unexpected(spread_token); + if (is("expand", "...")) { + spread_token = S.token; + if (maybe_sequence) + invalid_sequence = S.token; + next(); + a.push(new AST_Expansion({ + start: prev(), + expression: expression(), + end: S.token + })); + } else { + a.push(expression()); + } + if (!is("punc", ")")) { + expect(","); + if (is("punc", ")")) { + trailing_comma = prev(); + if (maybe_sequence) + invalid_sequence = trailing_comma; + } + } + } + expect(")"); + if (allow_arrows && is("arrow", "=>")) { + if (spread_token && trailing_comma) + unexpected(trailing_comma); + } else if (invalid_sequence) { + unexpected(invalid_sequence); + } + return a; + } + function _function_body(block, generator, is_async, name, args) { + var loop = S.in_loop; + var labels = S.labels; + var current_generator = S.in_generator; + var current_async = S.in_async; + ++S.in_function; + if (generator) + S.in_generator = S.in_function; + if (is_async) + S.in_async = S.in_function; + if (args) + parameters(args); + if (block) + S.in_directives = true; + S.in_loop = 0; + S.labels = []; + if (block) { + S.input.push_directives_stack(); + var a = block_(); + if (name) + _verify_symbol(name); + if (args) + args.forEach(_verify_symbol); + S.input.pop_directives_stack(); + } else { + var a = [new AST_Return({ + start: S.token, + value: expression(false), + end: S.token + })]; + } + --S.in_function; + S.in_loop = loop; + S.labels = labels; + S.in_generator = current_generator; + S.in_async = current_async; + return a; + } + function _await_expression() { + if (!can_await()) { + croak("Unexpected await expression outside async function", S.prev.line, S.prev.col, S.prev.pos); + } + return new AST_Await({ + start: prev(), + end: S.token, + expression: maybe_unary(true) + }); + } + function _yield_expression() { + var start = S.token; + var star = false; + var has_expression = true; + if (can_insert_semicolon() || is("punc") && PUNC_AFTER_EXPRESSION.has(S.token.value) || is("template_cont")) { + has_expression = false; + } else if (is("operator", "*")) { + star = true; + next(); + } + return new AST_Yield({ + start, + is_star: star, + expression: has_expression ? expression() : null, + end: prev() + }); + } + function if_() { + var cond = parenthesised(), body = statement(false, false, true), belse = null; + if (is("keyword", "else")) { + next(); + belse = statement(false, false, true); + } + return new AST_If({ + condition: cond, + body, + alternative: belse + }); + } + function block_() { + expect("{"); + var a = []; + while (!is("punc", "}")) { + if (is("eof")) + unexpected(); + a.push(statement()); + } + next(); + return a; + } + function switch_body_() { + expect("{"); + var a = [], cur = null, branch = null, tmp; + while (!is("punc", "}")) { + if (is("eof")) + unexpected(); + if (is("keyword", "case")) { + if (branch) + branch.end = prev(); + cur = []; + branch = new AST_Case({ + start: (tmp = S.token, next(), tmp), + expression: expression(true), + body: cur + }); + a.push(branch); + expect(":"); + } else if (is("keyword", "default")) { + if (branch) + branch.end = prev(); + cur = []; + branch = new AST_Default({ + start: (tmp = S.token, next(), expect(":"), tmp), + body: cur + }); + a.push(branch); + } else { + if (!cur) + unexpected(); + cur.push(statement()); + } + } + if (branch) + branch.end = prev(); + next(); + return a; + } + function try_() { + var body, bcatch = null, bfinally = null; + body = new AST_TryBlock({ + start: S.token, + body: block_(), + end: prev() + }); + if (is("keyword", "catch")) { + var start = S.token; + next(); + if (is("punc", "{")) { + var name = null; + } else { + expect("("); + var name = parameter(undefined, AST_SymbolCatch); + expect(")"); + } + bcatch = new AST_Catch({ + start, + argname: name, + body: block_(), + end: prev() + }); + } + if (is("keyword", "finally")) { + var start = S.token; + next(); + bfinally = new AST_Finally({ + start, + body: block_(), + end: prev() + }); + } + if (!bcatch && !bfinally) + croak("Missing catch/finally blocks"); + return new AST_Try({ + body, + bcatch, + bfinally + }); + } + function vardefs(no_in, kind) { + var var_defs = []; + var def; + for (;; ) { + var sym_type = kind === "var" ? AST_SymbolVar : kind === "const" ? AST_SymbolConst : kind === "let" ? AST_SymbolLet : null; + if (is("punc", "{") || is("punc", "[")) { + def = new AST_VarDef({ + start: S.token, + name: binding_element(undefined, sym_type), + value: is("operator", "=") ? (expect_token("operator", "="), expression(false, no_in)) : null, + end: prev() + }); + } else { + def = new AST_VarDef({ + start: S.token, + name: as_symbol(sym_type), + value: is("operator", "=") ? (next(), expression(false, no_in)) : !no_in && kind === "const" ? croak("Missing initializer in const declaration") : null, + end: prev() + }); + if (def.name.name == "import") + croak("Unexpected token: import"); + } + var_defs.push(def); + if (!is("punc", ",")) + break; + next(); + } + return var_defs; + } + var var_ = function(no_in) { + return new AST_Var({ + start: prev(), + definitions: vardefs(no_in, "var"), + end: prev() + }); + }; + var let_ = function(no_in) { + return new AST_Let({ + start: prev(), + definitions: vardefs(no_in, "let"), + end: prev() + }); + }; + var const_ = function(no_in) { + return new AST_Const({ + start: prev(), + definitions: vardefs(no_in, "const"), + end: prev() + }); + }; + var new_ = function(allow_calls) { + var start = S.token; + expect_token("operator", "new"); + if (is("punc", ".")) { + next(); + expect_token("name", "target"); + return subscripts(new AST_NewTarget({ + start, + end: prev() + }), allow_calls); + } + var newexp = expr_atom(false), args; + if (is("punc", "(")) { + next(); + args = expr_list(")", true); + } else { + args = []; + } + var call = new AST_New({ + start, + expression: newexp, + args, + end: prev() + }); + annotate(call); + return subscripts(call, allow_calls); + }; + function as_atom_node() { + var tok = S.token, ret; + switch (tok.type) { + case "name": + ret = _make_symbol(AST_SymbolRef); + break; + case "num": + ret = new AST_Number({ + start: tok, + end: tok, + value: tok.value, + raw: LATEST_RAW + }); + break; + case "big_int": + ret = new AST_BigInt({ + start: tok, + end: tok, + value: tok.value, + raw: LATEST_RAW + }); + break; + case "string": + ret = new AST_String({ + start: tok, + end: tok, + value: tok.value, + quote: tok.quote + }); + annotate(ret); + break; + case "regexp": + const [_, source, flags] = tok.value.match(/^\/(.*)\/(\w*)$/); + ret = new AST_RegExp({ start: tok, end: tok, value: { source, flags } }); + break; + case "atom": + switch (tok.value) { + case "false": + ret = new AST_False({ start: tok, end: tok }); + break; + case "true": + ret = new AST_True({ start: tok, end: tok }); + break; + case "null": + ret = new AST_Null({ start: tok, end: tok }); + break; + } + break; + } + next(); + return ret; + } + function to_fun_args(ex, default_seen_above) { + var insert_default = function(ex2, default_value) { + if (default_value) { + return new AST_DefaultAssign({ + start: ex2.start, + left: ex2, + operator: "=", + right: default_value, + end: default_value.end + }); + } + return ex2; + }; + if (ex instanceof AST_Object) { + return insert_default(new AST_Destructuring({ + start: ex.start, + end: ex.end, + is_array: false, + names: ex.properties.map((prop) => to_fun_args(prop)) + }), default_seen_above); + } else if (ex instanceof AST_ObjectKeyVal) { + ex.value = to_fun_args(ex.value); + return insert_default(ex, default_seen_above); + } else if (ex instanceof AST_Hole) { + return ex; + } else if (ex instanceof AST_Destructuring) { + ex.names = ex.names.map((name) => to_fun_args(name)); + return insert_default(ex, default_seen_above); + } else if (ex instanceof AST_SymbolRef) { + return insert_default(new AST_SymbolFunarg({ + name: ex.name, + start: ex.start, + end: ex.end + }), default_seen_above); + } else if (ex instanceof AST_Expansion) { + ex.expression = to_fun_args(ex.expression); + return insert_default(ex, default_seen_above); + } else if (ex instanceof AST_Array) { + return insert_default(new AST_Destructuring({ + start: ex.start, + end: ex.end, + is_array: true, + names: ex.elements.map((elm) => to_fun_args(elm)) + }), default_seen_above); + } else if (ex instanceof AST_Assign) { + return insert_default(to_fun_args(ex.left, ex.right), default_seen_above); + } else if (ex instanceof AST_DefaultAssign) { + ex.left = to_fun_args(ex.left); + return ex; + } else { + croak("Invalid function parameter", ex.start.line, ex.start.col); + } + } + var expr_atom = function(allow_calls, allow_arrows) { + if (is("operator", "new")) { + return new_(allow_calls); + } + if (is("name", "import") && is_token(peek(), "punc", ".")) { + return import_meta(allow_calls); + } + var start = S.token; + var peeked; + var async = is("name", "async") && (peeked = peek()).value != "[" && peeked.type != "arrow" && as_atom_node(); + if (is("punc")) { + switch (S.token.value) { + case "(": + if (async && !allow_calls) + break; + var exprs = params_or_seq_(allow_arrows, !async); + if (allow_arrows && is("arrow", "=>")) { + return arrow_function(start, exprs.map((e) => to_fun_args(e)), !!async); + } + var ex = async ? new AST_Call({ + expression: async, + args: exprs + }) : to_expr_or_sequence(start, exprs); + if (ex.start) { + const outer_comments_before = start.comments_before.length; + outer_comments_before_counts.set(start, outer_comments_before); + ex.start.comments_before.unshift(...start.comments_before); + start.comments_before = ex.start.comments_before; + if (outer_comments_before == 0 && start.comments_before.length > 0) { + var comment = start.comments_before[0]; + if (!comment.nlb) { + comment.nlb = start.nlb; + start.nlb = false; + } + } + start.comments_after = ex.start.comments_after; + } + ex.start = start; + var end = prev(); + if (ex.end) { + end.comments_before = ex.end.comments_before; + ex.end.comments_after.push(...end.comments_after); + end.comments_after = ex.end.comments_after; + } + ex.end = end; + if (ex instanceof AST_Call) + annotate(ex); + return subscripts(ex, allow_calls); + case "[": + return subscripts(array_(), allow_calls); + case "{": + return subscripts(object_or_destructuring_(), allow_calls); + } + if (!async) + unexpected(); + } + if (allow_arrows && is("name") && is_token(peek(), "arrow")) { + var param = new AST_SymbolFunarg({ + name: S.token.value, + start, + end: start + }); + next(); + return arrow_function(start, [param], !!async); + } + if (is("keyword", "function")) { + next(); + var func = function_(AST_Function, false, !!async); + func.start = start; + func.end = prev(); + return subscripts(func, allow_calls); + } + if (async) + return subscripts(async, allow_calls); + if (is("keyword", "class")) { + next(); + var cls = class_(AST_ClassExpression); + cls.start = start; + cls.end = prev(); + return subscripts(cls, allow_calls); + } + if (is("template_head")) { + return subscripts(template_string(), allow_calls); + } + if (ATOMIC_START_TOKEN.has(S.token.type)) { + return subscripts(as_atom_node(), allow_calls); + } + unexpected(); + }; + function template_string() { + var segments = [], start = S.token; + segments.push(new AST_TemplateSegment({ + start: S.token, + raw: TEMPLATE_RAWS.get(S.token), + value: S.token.value, + end: S.token + })); + while (!S.token.template_end) { + next(); + handle_regexp(); + segments.push(expression(true)); + segments.push(new AST_TemplateSegment({ + start: S.token, + raw: TEMPLATE_RAWS.get(S.token), + value: S.token.value, + end: S.token + })); + } + next(); + return new AST_TemplateString({ + start, + segments, + end: S.token + }); + } + function expr_list(closing, allow_trailing_comma, allow_empty) { + var first = true, a = []; + while (!is("punc", closing)) { + if (first) + first = false; + else + expect(","); + if (allow_trailing_comma && is("punc", closing)) + break; + if (is("punc", ",") && allow_empty) { + a.push(new AST_Hole({ start: S.token, end: S.token })); + } else if (is("expand", "...")) { + next(); + a.push(new AST_Expansion({ start: prev(), expression: expression(), end: S.token })); + } else { + a.push(expression(false)); + } + } + next(); + return a; + } + var array_ = embed_tokens(function() { + expect("["); + return new AST_Array({ + elements: expr_list("]", !options.strict, true) + }); + }); + var create_accessor = embed_tokens((is_generator, is_async) => { + return function_(AST_Accessor, is_generator, is_async); + }); + var object_or_destructuring_ = embed_tokens(function object_or_destructuring_() { + var start = S.token, first = true, a = []; + expect("{"); + while (!is("punc", "}")) { + if (first) + first = false; + else + expect(","); + if (!options.strict && is("punc", "}")) + break; + start = S.token; + if (start.type == "expand") { + next(); + a.push(new AST_Expansion({ + start, + expression: expression(false), + end: prev() + })); + continue; + } + if (is("privatename")) { + croak("private fields are not allowed in an object"); + } + var name = as_property_name(); + var value; + if (!is("punc", ":")) { + var concise = object_or_class_property(name, start); + if (concise) { + a.push(concise); + continue; + } + value = new AST_SymbolRef({ + start: prev(), + name, + end: prev() + }); + } else if (name === null) { + unexpected(prev()); + } else { + next(); + value = expression(false); + } + if (is("operator", "=")) { + next(); + value = new AST_Assign({ + start, + left: value, + operator: "=", + right: expression(false), + logical: false, + end: prev() + }); + } + const kv = new AST_ObjectKeyVal({ + start, + quote: start.quote, + key: name, + value, + end: prev() + }); + a.push(annotate(kv)); + } + next(); + return new AST_Object({ properties: a }); + }); + function class_(KindOfClass, is_export_default) { + var start, method, class_name, extends_, properties = []; + S.input.push_directives_stack(); + S.input.add_directive("use strict"); + if (S.token.type == "name" && S.token.value != "extends") { + class_name = as_symbol(KindOfClass === AST_DefClass ? AST_SymbolDefClass : AST_SymbolClass); + } + if (KindOfClass === AST_DefClass && !class_name) { + if (is_export_default) { + KindOfClass = AST_ClassExpression; + } else { + unexpected(); + } + } + if (S.token.value == "extends") { + next(); + extends_ = expression(true); + } + expect("{"); + const save_in_class = S.in_class; + S.in_class = true; + while (is("punc", ";")) { + next(); + } + while (!is("punc", "}")) { + start = S.token; + method = object_or_class_property(as_property_name(), start, true); + if (!method) { + unexpected(); + } + properties.push(method); + while (is("punc", ";")) { + next(); + } + } + S.in_class = save_in_class; + S.input.pop_directives_stack(); + next(); + return new KindOfClass({ + start, + name: class_name, + extends: extends_, + properties, + end: prev() + }); + } + function object_or_class_property(name, start, is_class) { + const get_symbol_ast = (name2, SymbolClass) => { + if (typeof name2 === "string") { + return new SymbolClass({ start, name: name2, end: prev() }); + } else if (name2 === null) { + unexpected(); + } + return name2; + }; + const is_not_method_start = () => !is("punc", "(") && !is("punc", ",") && !is("punc", "}") && !is("punc", ";") && !is("operator", "="); + var is_async = false; + var is_static = false; + var is_generator = false; + var is_private = false; + var accessor_type = null; + if (is_class && name === "static" && is_not_method_start()) { + const static_block = class_static_block(); + if (static_block != null) { + return static_block; + } + is_static = true; + name = as_property_name(); + } + if (name === "async" && is_not_method_start()) { + is_async = true; + name = as_property_name(); + } + if (prev().type === "operator" && prev().value === "*") { + is_generator = true; + name = as_property_name(); + } + if ((name === "get" || name === "set") && is_not_method_start()) { + accessor_type = name; + name = as_property_name(); + } + if (prev().type === "privatename") { + is_private = true; + } + const property_token = prev(); + if (accessor_type != null) { + if (!is_private) { + const AccessorClass = accessor_type === "get" ? AST_ObjectGetter : AST_ObjectSetter; + name = get_symbol_ast(name, AST_SymbolMethod); + return annotate(new AccessorClass({ + start, + static: is_static, + key: name, + quote: name instanceof AST_SymbolMethod ? property_token.quote : undefined, + value: create_accessor(), + end: prev() + })); + } else { + const AccessorClass = accessor_type === "get" ? AST_PrivateGetter : AST_PrivateSetter; + return annotate(new AccessorClass({ + start, + static: is_static, + key: get_symbol_ast(name, AST_SymbolMethod), + value: create_accessor(), + end: prev() + })); + } + } + if (is("punc", "(")) { + name = get_symbol_ast(name, AST_SymbolMethod); + const AST_MethodVariant = is_private ? AST_PrivateMethod : AST_ConciseMethod; + var node = new AST_MethodVariant({ + start, + static: is_static, + key: name, + quote: name instanceof AST_SymbolMethod ? property_token.quote : undefined, + value: create_accessor(is_generator, is_async), + end: prev() + }); + return annotate(node); + } + if (is_class) { + const AST_SymbolVariant = is_private ? AST_SymbolPrivateProperty : AST_SymbolClassProperty; + const AST_ClassPropertyVariant = is_private ? AST_ClassPrivateProperty : AST_ClassProperty; + const key = get_symbol_ast(name, AST_SymbolVariant); + const quote = key instanceof AST_SymbolClassProperty ? property_token.quote : undefined; + if (is("operator", "=")) { + next(); + return annotate(new AST_ClassPropertyVariant({ + start, + static: is_static, + quote, + key, + value: expression(false), + end: prev() + })); + } else if (is("name") || is("privatename") || is("punc", "[") || is("operator", "*") || is("punc", ";") || is("punc", "}") || is("string") || is("num") || is("big_int")) { + return annotate(new AST_ClassPropertyVariant({ + start, + static: is_static, + quote, + key, + end: prev() + })); + } + } + } + function class_static_block() { + if (!is("punc", "{")) { + return null; + } + const start = S.token; + const body = []; + next(); + while (!is("punc", "}")) { + body.push(statement()); + } + next(); + return new AST_ClassStaticBlock({ start, body, end: prev() }); + } + function maybe_import_attributes() { + if ((is("keyword", "with") || is("name", "assert")) && !has_newline_before(S.token)) { + next(); + return object_or_destructuring_(); + } + return null; + } + function import_statement() { + var start = prev(); + var imported_name; + var imported_names; + if (is("name")) { + imported_name = as_symbol(AST_SymbolImport); + } + if (is("punc", ",")) { + next(); + } + imported_names = map_names(true); + if (imported_names || imported_name) { + expect_token("name", "from"); + } + var mod_str = S.token; + if (mod_str.type !== "string") { + unexpected(); + } + next(); + const attributes = maybe_import_attributes(); + return new AST_Import({ + start, + imported_name, + imported_names, + module_name: new AST_String({ + start: mod_str, + value: mod_str.value, + quote: mod_str.quote, + end: mod_str + }), + attributes, + end: S.token + }); + } + function import_meta(allow_calls) { + var start = S.token; + expect_token("name", "import"); + expect_token("punc", "."); + expect_token("name", "meta"); + return subscripts(new AST_ImportMeta({ + start, + end: prev() + }), allow_calls); + } + function map_name(is_import) { + function make_symbol(type2, quote) { + return new type2({ + name: as_property_name(), + quote: quote || undefined, + start: prev(), + end: prev() + }); + } + var foreign_type = is_import ? AST_SymbolImportForeign : AST_SymbolExportForeign; + var type = is_import ? AST_SymbolImport : AST_SymbolExport; + var start = S.token; + var foreign_name; + var name; + if (is_import) { + foreign_name = make_symbol(foreign_type, start.quote); + } else { + name = make_symbol(type, start.quote); + } + if (is("name", "as")) { + next(); + if (is_import) { + name = make_symbol(type); + } else { + foreign_name = make_symbol(foreign_type, S.token.quote); + } + } else { + if (is_import) { + name = new type(foreign_name); + } else { + foreign_name = new foreign_type(name); + } + } + return new AST_NameMapping({ + start, + foreign_name, + name, + end: prev() + }); + } + function map_nameAsterisk(is_import, import_or_export_foreign_name) { + var foreign_type = is_import ? AST_SymbolImportForeign : AST_SymbolExportForeign; + var type = is_import ? AST_SymbolImport : AST_SymbolExport; + var start = S.token; + var name, foreign_name; + var end = prev(); + if (is_import) { + name = import_or_export_foreign_name; + } else { + foreign_name = import_or_export_foreign_name; + } + name = name || new type({ + start, + name: "*", + end + }); + foreign_name = foreign_name || new foreign_type({ + start, + name: "*", + end + }); + return new AST_NameMapping({ + start, + foreign_name, + name, + end + }); + } + function map_names(is_import) { + var names; + if (is("punc", "{")) { + next(); + names = []; + while (!is("punc", "}")) { + names.push(map_name(is_import)); + if (is("punc", ",")) { + next(); + } + } + next(); + } else if (is("operator", "*")) { + var name; + next(); + if (is("name", "as")) { + next(); + name = is_import ? as_symbol(AST_SymbolImport) : as_symbol_or_string(AST_SymbolExportForeign); + } + names = [map_nameAsterisk(is_import, name)]; + } + return names; + } + function export_statement() { + var start = S.token; + var is_default; + var exported_names; + if (is("keyword", "default")) { + is_default = true; + next(); + } else if (exported_names = map_names(false)) { + if (is("name", "from")) { + next(); + var mod_str = S.token; + if (mod_str.type !== "string") { + unexpected(); + } + next(); + const attributes = maybe_import_attributes(); + return new AST_Export({ + start, + is_default, + exported_names, + module_name: new AST_String({ + start: mod_str, + value: mod_str.value, + quote: mod_str.quote, + end: mod_str + }), + end: prev(), + attributes + }); + } else { + return new AST_Export({ + start, + is_default, + exported_names, + end: prev() + }); + } + } + var node; + var exported_value; + var exported_definition; + if (is("punc", "{") || is_default && (is("keyword", "class") || is("keyword", "function")) && is_token(peek(), "punc")) { + exported_value = expression(false); + semicolon(); + } else if ((node = statement(is_default)) instanceof AST_Definitions && is_default) { + unexpected(node.start); + } else if (node instanceof AST_Definitions || node instanceof AST_Defun || node instanceof AST_DefClass) { + exported_definition = node; + } else if (node instanceof AST_ClassExpression || node instanceof AST_Function) { + exported_value = node; + } else if (node instanceof AST_SimpleStatement) { + exported_value = node.body; + } else { + unexpected(node.start); + } + return new AST_Export({ + start, + is_default, + exported_value, + exported_definition, + end: prev(), + attributes: null + }); + } + function as_property_name() { + var tmp = S.token; + switch (tmp.type) { + case "punc": + if (tmp.value === "[") { + next(); + var ex = expression(false); + expect("]"); + return ex; + } else + unexpected(tmp); + case "operator": + if (tmp.value === "*") { + next(); + return null; + } + if (!["delete", "in", "instanceof", "new", "typeof", "void"].includes(tmp.value)) { + unexpected(tmp); + } + case "name": + case "privatename": + case "string": + case "keyword": + case "atom": + next(); + return tmp.value; + case "num": + case "big_int": + next(); + return "" + tmp.value; + default: + unexpected(tmp); + } + } + function as_name() { + var tmp = S.token; + if (tmp.type != "name" && tmp.type != "privatename") + unexpected(); + next(); + return tmp.value; + } + function _make_symbol(type) { + var name = S.token.value; + return new (name == "this" ? AST_This : name == "super" ? AST_Super : type)({ + name: String(name), + start: S.token, + end: S.token + }); + } + function _verify_symbol(sym) { + var name = sym.name; + if (is_in_generator() && name == "yield") { + token_error(sym.start, "Yield cannot be used as identifier inside generators"); + } + if (S.input.has_directive("use strict")) { + if (name == "yield") { + token_error(sym.start, "Unexpected yield identifier inside strict mode"); + } + if (sym instanceof AST_SymbolDeclaration && (name == "arguments" || name == "eval")) { + token_error(sym.start, "Unexpected " + name + " in strict mode"); + } + } + } + function as_symbol(type, noerror) { + if (!is("name")) { + if (!noerror) + croak("Name expected"); + return null; + } + var sym = _make_symbol(type); + _verify_symbol(sym); + next(); + return sym; + } + function as_symbol_or_string(type) { + if (!is("name")) { + if (!is("string")) { + croak("Name or string expected"); + } + var tok = S.token; + var ret = new type({ + start: tok, + end: tok, + name: tok.value, + quote: tok.quote + }); + next(); + return ret; + } + var sym = _make_symbol(type); + _verify_symbol(sym); + next(); + return sym; + } + function annotate(node, before_token = node.start) { + var comments = before_token.comments_before; + const comments_outside_parens = outer_comments_before_counts.get(before_token); + var i = comments_outside_parens != null ? comments_outside_parens : comments.length; + while (--i >= 0) { + var comment = comments[i]; + if (/[@#]__/.test(comment.value)) { + if (/[@#]__PURE__/.test(comment.value)) { + set_annotation(node, _PURE); + break; + } + if (/[@#]__INLINE__/.test(comment.value)) { + set_annotation(node, _INLINE); + break; + } + if (/[@#]__NOINLINE__/.test(comment.value)) { + set_annotation(node, _NOINLINE); + break; + } + if (/[@#]__KEY__/.test(comment.value)) { + set_annotation(node, _KEY); + break; + } + if (/[@#]__MANGLE_PROP__/.test(comment.value)) { + set_annotation(node, _MANGLEPROP); + break; + } + } + } + return node; + } + var subscripts = function(expr, allow_calls, is_chain) { + var start = expr.start; + if (is("punc", ".")) { + next(); + if (is("privatename") && !S.in_class) + croak("Private field must be used in an enclosing class"); + const AST_DotVariant = is("privatename") ? AST_DotHash : AST_Dot; + return annotate(subscripts(new AST_DotVariant({ + start, + expression: expr, + optional: false, + property: as_name(), + end: prev() + }), allow_calls, is_chain)); + } + if (is("punc", "[")) { + next(); + var prop = expression(true); + expect("]"); + return annotate(subscripts(new AST_Sub({ + start, + expression: expr, + optional: false, + property: prop, + end: prev() + }), allow_calls, is_chain)); + } + if (allow_calls && is("punc", "(")) { + next(); + var call = new AST_Call({ + start, + expression: expr, + optional: false, + args: call_args(), + end: prev() + }); + annotate(call); + return subscripts(call, true, is_chain); + } + if (is("punc", "?.")) { + next(); + let chain_contents; + if (allow_calls && is("punc", "(")) { + next(); + const call2 = new AST_Call({ + start, + optional: true, + expression: expr, + args: call_args(), + end: prev() + }); + annotate(call2); + chain_contents = subscripts(call2, true, true); + } else if (is("name") || is("privatename")) { + if (is("privatename") && !S.in_class) + croak("Private field must be used in an enclosing class"); + const AST_DotVariant = is("privatename") ? AST_DotHash : AST_Dot; + chain_contents = annotate(subscripts(new AST_DotVariant({ + start, + expression: expr, + optional: true, + property: as_name(), + end: prev() + }), allow_calls, true)); + } else if (is("punc", "[")) { + next(); + const property = expression(true); + expect("]"); + chain_contents = annotate(subscripts(new AST_Sub({ + start, + expression: expr, + optional: true, + property, + end: prev() + }), allow_calls, true)); + } + if (!chain_contents) + unexpected(); + if (chain_contents instanceof AST_Chain) + return chain_contents; + return new AST_Chain({ + start, + expression: chain_contents, + end: prev() + }); + } + if (is("template_head")) { + if (is_chain) { + unexpected(); + } + return subscripts(new AST_PrefixedTemplateString({ + start, + prefix: expr, + template_string: template_string(), + end: prev() + }), allow_calls); + } + return expr; + }; + function call_args() { + var args = []; + while (!is("punc", ")")) { + if (is("expand", "...")) { + next(); + args.push(new AST_Expansion({ + start: prev(), + expression: expression(false), + end: prev() + })); + } else { + args.push(expression(false)); + } + if (!is("punc", ")")) { + expect(","); + } + } + next(); + return args; + } + var maybe_unary = function(allow_calls, allow_arrows) { + var start = S.token; + if (start.type == "name" && start.value == "await" && can_await()) { + next(); + return _await_expression(); + } + if (is("operator") && UNARY_PREFIX.has(start.value)) { + next(); + handle_regexp(); + var ex = make_unary(AST_UnaryPrefix, start, maybe_unary(allow_calls)); + ex.start = start; + ex.end = prev(); + return ex; + } + var val = expr_atom(allow_calls, allow_arrows); + while (is("operator") && UNARY_POSTFIX.has(S.token.value) && !has_newline_before(S.token)) { + if (val instanceof AST_Arrow) + unexpected(); + val = make_unary(AST_UnaryPostfix, S.token, val); + val.start = start; + val.end = S.token; + next(); + } + return val; + }; + function make_unary(ctor, token, expr) { + var op = token.value; + switch (op) { + case "++": + case "--": + if (!is_assignable(expr)) + croak("Invalid use of " + op + " operator", token.line, token.col, token.pos); + break; + case "delete": + if (expr instanceof AST_SymbolRef && S.input.has_directive("use strict")) + croak("Calling delete on expression not allowed in strict mode", expr.start.line, expr.start.col, expr.start.pos); + break; + } + return new ctor({ operator: op, expression: expr }); + } + var expr_op = function(left, min_prec, no_in) { + var op = is("operator") ? S.token.value : null; + if (op == "in" && no_in) + op = null; + if (op == "**" && left instanceof AST_UnaryPrefix && !is_token(left.start, "punc", "(") && left.operator !== "--" && left.operator !== "++") + unexpected(left.start); + var prec = op != null ? PRECEDENCE[op] : null; + if (prec != null && (prec > min_prec || op === "**" && min_prec === prec)) { + next(); + var right = expr_ops(no_in, prec, true); + return expr_op(new AST_Binary({ + start: left.start, + left, + operator: op, + right, + end: right.end + }), min_prec, no_in); + } + return left; + }; + function expr_ops(no_in, min_prec, allow_calls, allow_arrows) { + if (!no_in && min_prec < PRECEDENCE["in"] && is("privatename")) { + if (!S.in_class) { + croak("Private field must be used in an enclosing class"); + } + const start = S.token; + const key = new AST_SymbolPrivateProperty({ + start, + name: start.value, + end: start + }); + next(); + expect_token("operator", "in"); + const private_in = new AST_PrivateIn({ + start, + key, + value: expr_ops(no_in, PRECEDENCE["in"], true), + end: prev() + }); + return expr_op(private_in, 0, no_in); + } else { + return expr_op(maybe_unary(allow_calls, allow_arrows), min_prec, no_in); + } + } + var maybe_conditional = function(no_in) { + var start = S.token; + var expr = expr_ops(no_in, 0, true, true); + if (is("operator", "?")) { + next(); + var yes = expression(false); + expect(":"); + return new AST_Conditional({ + start, + condition: expr, + consequent: yes, + alternative: expression(false, no_in), + end: prev() + }); + } + return expr; + }; + function is_assignable(expr) { + return expr instanceof AST_PropAccess || expr instanceof AST_SymbolRef; + } + function to_destructuring(node) { + if (node instanceof AST_Object) { + node = new AST_Destructuring({ + start: node.start, + names: node.properties.map(to_destructuring), + is_array: false, + end: node.end + }); + } else if (node instanceof AST_Array) { + var names = []; + for (var i = 0;i < node.elements.length; i++) { + if (node.elements[i] instanceof AST_Expansion) { + if (i + 1 !== node.elements.length) { + token_error(node.elements[i].start, "Spread must the be last element in destructuring array"); + } + node.elements[i].expression = to_destructuring(node.elements[i].expression); + } + names.push(to_destructuring(node.elements[i])); + } + node = new AST_Destructuring({ + start: node.start, + names, + is_array: true, + end: node.end + }); + } else if (node instanceof AST_ObjectProperty) { + node.value = to_destructuring(node.value); + } else if (node instanceof AST_Assign) { + node = new AST_DefaultAssign({ + start: node.start, + left: node.left, + operator: "=", + right: node.right, + end: node.end + }); + } + return node; + } + var maybe_assign = function(no_in) { + handle_regexp(); + var start = S.token; + if (start.type == "name" && start.value == "yield") { + if (is_in_generator()) { + next(); + return _yield_expression(); + } else if (S.input.has_directive("use strict")) { + token_error(S.token, "Unexpected yield identifier inside strict mode"); + } + } + var left = maybe_conditional(no_in); + var val = S.token.value; + if (is("operator") && ASSIGNMENT.has(val)) { + if (is_assignable(left) || (left = to_destructuring(left)) instanceof AST_Destructuring) { + next(); + return new AST_Assign({ + start, + left, + operator: val, + right: maybe_assign(no_in), + logical: LOGICAL_ASSIGNMENT.has(val), + end: prev() + }); + } + croak("Invalid assignment"); + } + return left; + }; + var to_expr_or_sequence = function(start, exprs) { + if (exprs.length === 1) { + return exprs[0]; + } else if (exprs.length > 1) { + return new AST_Sequence({ start, expressions: exprs, end: peek() }); + } else { + croak("Invalid parenthesized expression"); + } + }; + var expression = function(commas, no_in) { + var start = S.token; + var exprs = []; + while (true) { + exprs.push(maybe_assign(no_in)); + if (!commas || !is("punc", ",")) + break; + next(); + commas = true; + } + return to_expr_or_sequence(start, exprs); + }; + function in_loop(cont) { + ++S.in_loop; + var ret = cont(); + --S.in_loop; + return ret; + } + if (options.expression) { + return expression(true); + } + return function parse_toplevel() { + var start = S.token; + var body = []; + S.input.push_directives_stack(); + if (options.module) + S.input.add_directive("use strict"); + while (!is("eof")) { + body.push(statement()); + } + S.input.pop_directives_stack(); + var end = prev(); + var toplevel = options.toplevel; + if (toplevel) { + toplevel.body = toplevel.body.concat(body); + toplevel.end = end; + } else { + toplevel = new AST_Toplevel({ start, body, end }); + } + TEMPLATE_RAWS = new Map; + return toplevel; + }(); +} + +// node_modules/terser/lib/ast.js +function DEFNODE(type, props, ctor, methods, base = AST_Node) { + if (!props) + props = []; + else + props = props.split(/\s+/); + var self_props = props; + if (base && base.PROPS) + props = props.concat(base.PROPS); + const proto = base && Object.create(base.prototype); + if (proto) { + ctor.prototype = proto; + ctor.BASE = base; + } + if (base) + base.SUBCLASSES.push(ctor); + ctor.prototype.CTOR = ctor; + ctor.prototype.constructor = ctor; + ctor.PROPS = props || null; + ctor.SELF_PROPS = self_props; + ctor.SUBCLASSES = []; + if (type) { + ctor.prototype.TYPE = ctor.TYPE = type; + } + if (methods) { + for (let i in methods) + if (HOP(methods, i)) { + if (i[0] === "$") { + ctor[i.substr(1)] = methods[i]; + } else { + ctor.prototype[i] = methods[i]; + } + } + } + ctor.DEFMETHOD = function(name, method) { + this.prototype[name] = method; + }; + return ctor; +} +var has_tok_flag = (tok, flag) => Boolean(tok.flags & flag); +var set_tok_flag = (tok, flag, truth) => { + if (truth) { + tok.flags |= flag; + } else { + tok.flags &= ~flag; + } +}; +var TOK_FLAG_NLB = 1; +var TOK_FLAG_QUOTE_SINGLE = 2; +var TOK_FLAG_QUOTE_EXISTS = 4; +var TOK_FLAG_TEMPLATE_END = 8; + +class AST_Token { + constructor(type, value, line, col, pos, nlb, comments_before, comments_after, file) { + this.flags = nlb ? 1 : 0; + this.type = type; + this.value = value; + this.line = line; + this.col = col; + this.pos = pos; + this.comments_before = comments_before; + this.comments_after = comments_after; + this.file = file; + Object.seal(this); + } + [Symbol.for("nodejs.util.inspect.custom")](_depth, options) { + const special = (str) => options.stylize(str, "special"); + const quote = typeof this.value === "string" && this.value.includes("`") ? "'" : "`"; + const value = `${quote}${this.value}${quote}`; + return `${special("[AST_Token")} ${value} at ${this.line}:${this.col}${special("]")}`; + } + get nlb() { + return has_tok_flag(this, TOK_FLAG_NLB); + } + set nlb(new_nlb) { + set_tok_flag(this, TOK_FLAG_NLB, new_nlb); + } + get quote() { + return !has_tok_flag(this, TOK_FLAG_QUOTE_EXISTS) ? "" : has_tok_flag(this, TOK_FLAG_QUOTE_SINGLE) ? "'" : '"'; + } + set quote(quote_type) { + set_tok_flag(this, TOK_FLAG_QUOTE_SINGLE, quote_type === "'"); + set_tok_flag(this, TOK_FLAG_QUOTE_EXISTS, !!quote_type); + } + get template_end() { + return has_tok_flag(this, TOK_FLAG_TEMPLATE_END); + } + set template_end(new_template_end) { + set_tok_flag(this, TOK_FLAG_TEMPLATE_END, new_template_end); + } +} +var AST_Node = DEFNODE("Node", "start end", function AST_Node2(props) { + if (props) { + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + _clone: function(deep) { + if (deep) { + var self2 = this.clone(); + return self2.transform(new TreeTransformer(function(node) { + if (node !== self2) { + return node.clone(true); + } + })); + } + return new this.CTOR(this); + }, + clone: function(deep) { + return this._clone(deep); + }, + $documentation: "Base class of all AST nodes", + $propdoc: { + start: "[AST_Token] The first token of this node", + end: "[AST_Token] The last token of this node" + }, + _walk: function(visitor) { + return visitor._visit(this); + }, + walk: function(visitor) { + return this._walk(visitor); + }, + _children_backwards: () => {} +}, null); +var AST_Statement = DEFNODE("Statement", null, function AST_Statement2(props) { + if (props) { + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Base class of all statements" +}); +var AST_Debugger = DEFNODE("Debugger", null, function AST_Debugger2(props) { + if (props) { + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Represents a debugger statement" +}, AST_Statement); +var AST_Directive = DEFNODE("Directive", "value quote", function AST_Directive2(props) { + if (props) { + this.value = props.value; + this.quote = props.quote; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: 'Represents a directive, like "use strict";', + $propdoc: { + value: "[string] The value of this directive as a plain string (it's not an AST_String!)", + quote: "[string] the original quote character" + } +}, AST_Statement); +var AST_SimpleStatement = DEFNODE("SimpleStatement", "body", function AST_SimpleStatement2(props) { + if (props) { + this.body = props.body; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A statement consisting of an expression, i.e. a = 1 + 2", + $propdoc: { + body: "[AST_Node] an expression node (should not be instanceof AST_Statement)" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + this.body._walk(visitor); + }); + }, + _children_backwards(push) { + push(this.body); + } +}, AST_Statement); +function walk_body(node, visitor) { + const body = node.body; + for (var i = 0, len = body.length;i < len; i++) { + body[i]._walk(visitor); + } +} +function clone_block_scope(deep) { + var clone = this._clone(deep); + if (this.block_scope) { + clone.block_scope = this.block_scope.clone(); + } + return clone; +} +var AST_Block = DEFNODE("Block", "body block_scope", function AST_Block2(props) { + if (props) { + this.body = props.body; + this.block_scope = props.block_scope; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A body of statements (usually braced)", + $propdoc: { + body: "[AST_Statement*] an array of statements", + block_scope: "[AST_Scope] the block scope" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + walk_body(this, visitor); + }); + }, + _children_backwards(push) { + let i = this.body.length; + while (i--) + push(this.body[i]); + }, + clone: clone_block_scope +}, AST_Statement); +var AST_BlockStatement = DEFNODE("BlockStatement", null, function AST_BlockStatement2(props) { + if (props) { + this.body = props.body; + this.block_scope = props.block_scope; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A block statement" +}, AST_Block); +var AST_EmptyStatement = DEFNODE("EmptyStatement", null, function AST_EmptyStatement2(props) { + if (props) { + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "The empty statement (empty block or simply a semicolon)" +}, AST_Statement); +var AST_StatementWithBody = DEFNODE("StatementWithBody", "body", function AST_StatementWithBody2(props) { + if (props) { + this.body = props.body; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Base class for all statements that contain one nested body: `For`, `ForIn`, `Do`, `While`, `With`", + $propdoc: { + body: "[AST_Statement] the body; this should always be present, even if it's an AST_EmptyStatement" + } +}, AST_Statement); +var AST_LabeledStatement = DEFNODE("LabeledStatement", "label", function AST_LabeledStatement2(props) { + if (props) { + this.label = props.label; + this.body = props.body; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Statement with a label", + $propdoc: { + label: "[AST_Label] a label definition" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + this.label._walk(visitor); + this.body._walk(visitor); + }); + }, + _children_backwards(push) { + push(this.body); + push(this.label); + }, + clone: function(deep) { + var node = this._clone(deep); + if (deep) { + var label = node.label; + var def = this.label; + node.walk(new TreeWalker(function(node2) { + if (node2 instanceof AST_LoopControl && node2.label && node2.label.thedef === def) { + node2.label.thedef = label; + label.references.push(node2); + } + })); + } + return node; + } +}, AST_StatementWithBody); +var AST_IterationStatement = DEFNODE("IterationStatement", "block_scope", function AST_IterationStatement2(props) { + if (props) { + this.block_scope = props.block_scope; + this.body = props.body; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Internal class. All loops inherit from it.", + $propdoc: { + block_scope: "[AST_Scope] the block scope for this iteration statement." + }, + clone: clone_block_scope +}, AST_StatementWithBody); +var AST_DWLoop = DEFNODE("DWLoop", "condition", function AST_DWLoop2(props) { + if (props) { + this.condition = props.condition; + this.block_scope = props.block_scope; + this.body = props.body; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Base class for do/while statements", + $propdoc: { + condition: "[AST_Node] the loop condition. Should not be instanceof AST_Statement" + } +}, AST_IterationStatement); +var AST_Do = DEFNODE("Do", null, function AST_Do2(props) { + if (props) { + this.condition = props.condition; + this.block_scope = props.block_scope; + this.body = props.body; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A `do` statement", + _walk: function(visitor) { + return visitor._visit(this, function() { + this.body._walk(visitor); + this.condition._walk(visitor); + }); + }, + _children_backwards(push) { + push(this.condition); + push(this.body); + } +}, AST_DWLoop); +var AST_While = DEFNODE("While", null, function AST_While2(props) { + if (props) { + this.condition = props.condition; + this.block_scope = props.block_scope; + this.body = props.body; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A `while` statement", + _walk: function(visitor) { + return visitor._visit(this, function() { + this.condition._walk(visitor); + this.body._walk(visitor); + }); + }, + _children_backwards(push) { + push(this.body); + push(this.condition); + } +}, AST_DWLoop); +var AST_For = DEFNODE("For", "init condition step", function AST_For2(props) { + if (props) { + this.init = props.init; + this.condition = props.condition; + this.step = props.step; + this.block_scope = props.block_scope; + this.body = props.body; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A `for` statement", + $propdoc: { + init: "[AST_Node?] the `for` initialization code, or null if empty", + condition: "[AST_Node?] the `for` termination clause, or null if empty", + step: "[AST_Node?] the `for` update clause, or null if empty" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + if (this.init) + this.init._walk(visitor); + if (this.condition) + this.condition._walk(visitor); + if (this.step) + this.step._walk(visitor); + this.body._walk(visitor); + }); + }, + _children_backwards(push) { + push(this.body); + if (this.step) + push(this.step); + if (this.condition) + push(this.condition); + if (this.init) + push(this.init); + } +}, AST_IterationStatement); +var AST_ForIn = DEFNODE("ForIn", "init object", function AST_ForIn2(props) { + if (props) { + this.init = props.init; + this.object = props.object; + this.block_scope = props.block_scope; + this.body = props.body; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A `for ... in` statement", + $propdoc: { + init: "[AST_Node] the `for/in` initialization code", + object: "[AST_Node] the object that we're looping through" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + this.init._walk(visitor); + this.object._walk(visitor); + this.body._walk(visitor); + }); + }, + _children_backwards(push) { + push(this.body); + if (this.object) + push(this.object); + if (this.init) + push(this.init); + } +}, AST_IterationStatement); +var AST_ForOf = DEFNODE("ForOf", "await", function AST_ForOf2(props) { + if (props) { + this.await = props.await; + this.init = props.init; + this.object = props.object; + this.block_scope = props.block_scope; + this.body = props.body; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A `for ... of` statement" +}, AST_ForIn); +var AST_With = DEFNODE("With", "expression", function AST_With2(props) { + if (props) { + this.expression = props.expression; + this.body = props.body; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A `with` statement", + $propdoc: { + expression: "[AST_Node] the `with` expression" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + this.expression._walk(visitor); + this.body._walk(visitor); + }); + }, + _children_backwards(push) { + push(this.body); + push(this.expression); + } +}, AST_StatementWithBody); +var AST_Scope = DEFNODE("Scope", "variables uses_with uses_eval parent_scope enclosed cname", function AST_Scope2(props) { + if (props) { + this.variables = props.variables; + this.uses_with = props.uses_with; + this.uses_eval = props.uses_eval; + this.parent_scope = props.parent_scope; + this.enclosed = props.enclosed; + this.cname = props.cname; + this.body = props.body; + this.block_scope = props.block_scope; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Base class for all statements introducing a lexical scope", + $propdoc: { + variables: "[Map/S] a map of name -> SymbolDef for all variables/functions defined in this scope", + uses_with: "[boolean/S] tells whether this scope uses the `with` statement", + uses_eval: "[boolean/S] tells whether this scope contains a direct call to the global `eval`", + parent_scope: "[AST_Scope?/S] link to the parent scope", + enclosed: "[SymbolDef*/S] a list of all symbol definitions that are accessed from this scope or any subscopes", + cname: "[integer/S] current index for mangling variables (used internally by the mangler)" + }, + get_defun_scope: function() { + var self2 = this; + while (self2.is_block_scope()) { + self2 = self2.parent_scope; + } + return self2; + }, + clone: function(deep, toplevel) { + var node = this._clone(deep); + if (deep && this.variables && toplevel && !this._block_scope) { + node.figure_out_scope({}, { + toplevel, + parent_scope: this.parent_scope + }); + } else { + if (this.variables) + node.variables = new Map(this.variables); + if (this.enclosed) + node.enclosed = this.enclosed.slice(); + if (this._block_scope) + node._block_scope = this._block_scope; + } + return node; + }, + pinned: function() { + return this.uses_eval || this.uses_with; + } +}, AST_Block); +var AST_Toplevel = DEFNODE("Toplevel", "globals", function AST_Toplevel2(props) { + if (props) { + this.globals = props.globals; + this.variables = props.variables; + this.uses_with = props.uses_with; + this.uses_eval = props.uses_eval; + this.parent_scope = props.parent_scope; + this.enclosed = props.enclosed; + this.cname = props.cname; + this.body = props.body; + this.block_scope = props.block_scope; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "The toplevel scope", + $propdoc: { + globals: "[Map/S] a map of name -> SymbolDef for all undeclared names" + }, + wrap_commonjs: function(name) { + var body = this.body; + var wrapped_tl = "(function(exports){'$ORIG';})(typeof " + name + "=='undefined'?(" + name + "={}):" + name + ");"; + wrapped_tl = parse(wrapped_tl); + wrapped_tl = wrapped_tl.transform(new TreeTransformer(function(node) { + if (node instanceof AST_Directive && node.value == "$ORIG") { + return MAP.splice(body); + } + })); + return wrapped_tl; + }, + wrap_enclose: function(args_values) { + if (typeof args_values != "string") + args_values = ""; + var index = args_values.indexOf(":"); + if (index < 0) + index = args_values.length; + var body = this.body; + return parse([ + "(function(", + args_values.slice(0, index), + '){"$ORIG"})(', + args_values.slice(index + 1), + ")" + ].join("")).transform(new TreeTransformer(function(node) { + if (node instanceof AST_Directive && node.value == "$ORIG") { + return MAP.splice(body); + } + })); + } +}, AST_Scope); +var AST_Expansion = DEFNODE("Expansion", "expression", function AST_Expansion2(props) { + if (props) { + this.expression = props.expression; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "An expandible argument, such as ...rest, a splat, such as [1,2,...all], or an expansion in a variable declaration, such as var [first, ...rest] = list", + $propdoc: { + expression: "[AST_Node] the thing to be expanded" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + this.expression.walk(visitor); + }); + }, + _children_backwards(push) { + push(this.expression); + } +}); +var AST_Lambda = DEFNODE("Lambda", "name argnames uses_arguments is_generator async", function AST_Lambda2(props) { + if (props) { + this.name = props.name; + this.argnames = props.argnames; + this.uses_arguments = props.uses_arguments; + this.is_generator = props.is_generator; + this.async = props.async; + this.variables = props.variables; + this.uses_with = props.uses_with; + this.uses_eval = props.uses_eval; + this.parent_scope = props.parent_scope; + this.enclosed = props.enclosed; + this.cname = props.cname; + this.body = props.body; + this.block_scope = props.block_scope; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Base class for functions", + $propdoc: { + name: "[AST_SymbolDeclaration?] the name of this function", + argnames: "[AST_SymbolFunarg|AST_Destructuring|AST_Expansion|AST_DefaultAssign*] array of function arguments, destructurings, or expanding arguments", + uses_arguments: "[boolean/S] tells whether this function accesses the arguments array", + is_generator: "[boolean] is this a generator method", + async: "[boolean] is this method async" + }, + args_as_names: function() { + var out = []; + for (var i = 0;i < this.argnames.length; i++) { + if (this.argnames[i] instanceof AST_Destructuring) { + out.push(...this.argnames[i].all_symbols()); + } else { + out.push(this.argnames[i]); + } + } + return out; + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + if (this.name) + this.name._walk(visitor); + var argnames = this.argnames; + for (var i = 0, len = argnames.length;i < len; i++) { + argnames[i]._walk(visitor); + } + walk_body(this, visitor); + }); + }, + _children_backwards(push) { + let i = this.body.length; + while (i--) + push(this.body[i]); + i = this.argnames.length; + while (i--) + push(this.argnames[i]); + if (this.name) + push(this.name); + }, + is_braceless() { + return this.body[0] instanceof AST_Return && this.body[0].value; + }, + length_property() { + let length = 0; + for (const arg of this.argnames) { + if (arg instanceof AST_SymbolFunarg || arg instanceof AST_Destructuring) { + length++; + } + } + return length; + } +}, AST_Scope); +var AST_Accessor = DEFNODE("Accessor", null, function AST_Accessor2(props) { + if (props) { + this.name = props.name; + this.argnames = props.argnames; + this.uses_arguments = props.uses_arguments; + this.is_generator = props.is_generator; + this.async = props.async; + this.variables = props.variables; + this.uses_with = props.uses_with; + this.uses_eval = props.uses_eval; + this.parent_scope = props.parent_scope; + this.enclosed = props.enclosed; + this.cname = props.cname; + this.body = props.body; + this.block_scope = props.block_scope; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A setter/getter function. The `name` property is always null." +}, AST_Lambda); +var AST_Function = DEFNODE("Function", null, function AST_Function2(props) { + if (props) { + this.name = props.name; + this.argnames = props.argnames; + this.uses_arguments = props.uses_arguments; + this.is_generator = props.is_generator; + this.async = props.async; + this.variables = props.variables; + this.uses_with = props.uses_with; + this.uses_eval = props.uses_eval; + this.parent_scope = props.parent_scope; + this.enclosed = props.enclosed; + this.cname = props.cname; + this.body = props.body; + this.block_scope = props.block_scope; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A function expression" +}, AST_Lambda); +var AST_Arrow = DEFNODE("Arrow", null, function AST_Arrow2(props) { + if (props) { + this.name = props.name; + this.argnames = props.argnames; + this.uses_arguments = props.uses_arguments; + this.is_generator = props.is_generator; + this.async = props.async; + this.variables = props.variables; + this.uses_with = props.uses_with; + this.uses_eval = props.uses_eval; + this.parent_scope = props.parent_scope; + this.enclosed = props.enclosed; + this.cname = props.cname; + this.body = props.body; + this.block_scope = props.block_scope; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "An ES6 Arrow function ((a) => b)" +}, AST_Lambda); +var AST_Defun = DEFNODE("Defun", null, function AST_Defun2(props) { + if (props) { + this.name = props.name; + this.argnames = props.argnames; + this.uses_arguments = props.uses_arguments; + this.is_generator = props.is_generator; + this.async = props.async; + this.variables = props.variables; + this.uses_with = props.uses_with; + this.uses_eval = props.uses_eval; + this.parent_scope = props.parent_scope; + this.enclosed = props.enclosed; + this.cname = props.cname; + this.body = props.body; + this.block_scope = props.block_scope; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A function definition" +}, AST_Lambda); +var AST_Destructuring = DEFNODE("Destructuring", "names is_array", function AST_Destructuring2(props) { + if (props) { + this.names = props.names; + this.is_array = props.is_array; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A destructuring of several names. Used in destructuring assignment and with destructuring function argument names", + $propdoc: { + names: "[AST_Node*] Array of properties or elements", + is_array: "[Boolean] Whether the destructuring represents an object or array" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + this.names.forEach(function(name) { + name._walk(visitor); + }); + }); + }, + _children_backwards(push) { + let i = this.names.length; + while (i--) + push(this.names[i]); + }, + all_symbols: function() { + var out = []; + walk(this, (node) => { + if (node instanceof AST_SymbolDeclaration) { + out.push(node); + } + if (node instanceof AST_Lambda) { + return true; + } + }); + return out; + } +}); +var AST_PrefixedTemplateString = DEFNODE("PrefixedTemplateString", "template_string prefix", function AST_PrefixedTemplateString2(props) { + if (props) { + this.template_string = props.template_string; + this.prefix = props.prefix; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A templatestring with a prefix, such as String.raw`foobarbaz`", + $propdoc: { + template_string: "[AST_TemplateString] The template string", + prefix: "[AST_Node] The prefix, which will get called." + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + this.prefix._walk(visitor); + this.template_string._walk(visitor); + }); + }, + _children_backwards(push) { + push(this.template_string); + push(this.prefix); + } +}); +var AST_TemplateString = DEFNODE("TemplateString", "segments", function AST_TemplateString2(props) { + if (props) { + this.segments = props.segments; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A template string literal", + $propdoc: { + segments: "[AST_Node*] One or more segments, starting with AST_TemplateSegment. AST_Node may follow AST_TemplateSegment, but each AST_Node must be followed by AST_TemplateSegment." + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + this.segments.forEach(function(seg) { + seg._walk(visitor); + }); + }); + }, + _children_backwards(push) { + let i = this.segments.length; + while (i--) + push(this.segments[i]); + } +}); +var AST_TemplateSegment = DEFNODE("TemplateSegment", "value raw", function AST_TemplateSegment2(props) { + if (props) { + this.value = props.value; + this.raw = props.raw; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A segment of a template string literal", + $propdoc: { + value: "Content of the segment", + raw: "Raw source of the segment" + } +}); +var AST_Jump = DEFNODE("Jump", null, function AST_Jump2(props) { + if (props) { + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Base class for “jumps” (for now that's `return`, `throw`, `break` and `continue`)" +}, AST_Statement); +var AST_Exit = DEFNODE("Exit", "value", function AST_Exit2(props) { + if (props) { + this.value = props.value; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Base class for “exits” (`return` and `throw`)", + $propdoc: { + value: "[AST_Node?] the value returned or thrown by this statement; could be null for AST_Return" + }, + _walk: function(visitor) { + return visitor._visit(this, this.value && function() { + this.value._walk(visitor); + }); + }, + _children_backwards(push) { + if (this.value) + push(this.value); + } +}, AST_Jump); +var AST_Return = DEFNODE("Return", null, function AST_Return2(props) { + if (props) { + this.value = props.value; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A `return` statement" +}, AST_Exit); +var AST_Throw = DEFNODE("Throw", null, function AST_Throw2(props) { + if (props) { + this.value = props.value; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A `throw` statement" +}, AST_Exit); +var AST_LoopControl = DEFNODE("LoopControl", "label", function AST_LoopControl2(props) { + if (props) { + this.label = props.label; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Base class for loop control statements (`break` and `continue`)", + $propdoc: { + label: "[AST_LabelRef?] the label, or null if none" + }, + _walk: function(visitor) { + return visitor._visit(this, this.label && function() { + this.label._walk(visitor); + }); + }, + _children_backwards(push) { + if (this.label) + push(this.label); + } +}, AST_Jump); +var AST_Break = DEFNODE("Break", null, function AST_Break2(props) { + if (props) { + this.label = props.label; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A `break` statement" +}, AST_LoopControl); +var AST_Continue = DEFNODE("Continue", null, function AST_Continue2(props) { + if (props) { + this.label = props.label; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A `continue` statement" +}, AST_LoopControl); +var AST_Await = DEFNODE("Await", "expression", function AST_Await2(props) { + if (props) { + this.expression = props.expression; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "An `await` statement", + $propdoc: { + expression: "[AST_Node] the mandatory expression being awaited" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + this.expression._walk(visitor); + }); + }, + _children_backwards(push) { + push(this.expression); + } +}); +var AST_Yield = DEFNODE("Yield", "expression is_star", function AST_Yield2(props) { + if (props) { + this.expression = props.expression; + this.is_star = props.is_star; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A `yield` statement", + $propdoc: { + expression: "[AST_Node?] the value returned or thrown by this statement; could be null (representing undefined) but only when is_star is set to false", + is_star: "[Boolean] Whether this is a yield or yield* statement" + }, + _walk: function(visitor) { + return visitor._visit(this, this.expression && function() { + this.expression._walk(visitor); + }); + }, + _children_backwards(push) { + if (this.expression) + push(this.expression); + } +}); +var AST_If = DEFNODE("If", "condition alternative", function AST_If2(props) { + if (props) { + this.condition = props.condition; + this.alternative = props.alternative; + this.body = props.body; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A `if` statement", + $propdoc: { + condition: "[AST_Node] the `if` condition", + alternative: "[AST_Statement?] the `else` part, or null if not present" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + this.condition._walk(visitor); + this.body._walk(visitor); + if (this.alternative) + this.alternative._walk(visitor); + }); + }, + _children_backwards(push) { + if (this.alternative) { + push(this.alternative); + } + push(this.body); + push(this.condition); + } +}, AST_StatementWithBody); +var AST_Switch = DEFNODE("Switch", "expression", function AST_Switch2(props) { + if (props) { + this.expression = props.expression; + this.body = props.body; + this.block_scope = props.block_scope; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A `switch` statement", + $propdoc: { + expression: "[AST_Node] the `switch` “discriminant”" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + this.expression._walk(visitor); + walk_body(this, visitor); + }); + }, + _children_backwards(push) { + let i = this.body.length; + while (i--) + push(this.body[i]); + push(this.expression); + } +}, AST_Block); +var AST_SwitchBranch = DEFNODE("SwitchBranch", null, function AST_SwitchBranch2(props) { + if (props) { + this.body = props.body; + this.block_scope = props.block_scope; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Base class for `switch` branches" +}, AST_Block); +var AST_Default = DEFNODE("Default", null, function AST_Default2(props) { + if (props) { + this.body = props.body; + this.block_scope = props.block_scope; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A `default` switch branch" +}, AST_SwitchBranch); +var AST_Case = DEFNODE("Case", "expression", function AST_Case2(props) { + if (props) { + this.expression = props.expression; + this.body = props.body; + this.block_scope = props.block_scope; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A `case` switch branch", + $propdoc: { + expression: "[AST_Node] the `case` expression" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + this.expression._walk(visitor); + walk_body(this, visitor); + }); + }, + _children_backwards(push) { + let i = this.body.length; + while (i--) + push(this.body[i]); + push(this.expression); + } +}, AST_SwitchBranch); +var AST_Try = DEFNODE("Try", "body bcatch bfinally", function AST_Try2(props) { + if (props) { + this.body = props.body; + this.bcatch = props.bcatch; + this.bfinally = props.bfinally; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A `try` statement", + $propdoc: { + body: "[AST_TryBlock] the try block", + bcatch: "[AST_Catch?] the catch block, or null if not present", + bfinally: "[AST_Finally?] the finally block, or null if not present" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + this.body._walk(visitor); + if (this.bcatch) + this.bcatch._walk(visitor); + if (this.bfinally) + this.bfinally._walk(visitor); + }); + }, + _children_backwards(push) { + if (this.bfinally) + push(this.bfinally); + if (this.bcatch) + push(this.bcatch); + push(this.body); + } +}, AST_Statement); +var AST_TryBlock = DEFNODE("TryBlock", null, function AST_TryBlock2(props) { + if (props) { + this.body = props.body; + this.block_scope = props.block_scope; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "The `try` block of a try statement" +}, AST_Block); +var AST_Catch = DEFNODE("Catch", "argname", function AST_Catch2(props) { + if (props) { + this.argname = props.argname; + this.body = props.body; + this.block_scope = props.block_scope; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A `catch` node; only makes sense as part of a `try` statement", + $propdoc: { + argname: "[AST_SymbolCatch|AST_Destructuring|AST_Expansion|AST_DefaultAssign] symbol for the exception" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + if (this.argname) + this.argname._walk(visitor); + walk_body(this, visitor); + }); + }, + _children_backwards(push) { + let i = this.body.length; + while (i--) + push(this.body[i]); + if (this.argname) + push(this.argname); + } +}, AST_Block); +var AST_Finally = DEFNODE("Finally", null, function AST_Finally2(props) { + if (props) { + this.body = props.body; + this.block_scope = props.block_scope; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A `finally` node; only makes sense as part of a `try` statement" +}, AST_Block); +var AST_Definitions = DEFNODE("Definitions", "definitions", function AST_Definitions2(props) { + if (props) { + this.definitions = props.definitions; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Base class for `var` or `const` nodes (variable declarations/initializations)", + $propdoc: { + definitions: "[AST_VarDef*] array of variable definitions" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + var definitions = this.definitions; + for (var i = 0, len = definitions.length;i < len; i++) { + definitions[i]._walk(visitor); + } + }); + }, + _children_backwards(push) { + let i = this.definitions.length; + while (i--) + push(this.definitions[i]); + } +}, AST_Statement); +var AST_Var = DEFNODE("Var", null, function AST_Var2(props) { + if (props) { + this.definitions = props.definitions; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A `var` statement" +}, AST_Definitions); +var AST_Let = DEFNODE("Let", null, function AST_Let2(props) { + if (props) { + this.definitions = props.definitions; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A `let` statement" +}, AST_Definitions); +var AST_Const = DEFNODE("Const", null, function AST_Const2(props) { + if (props) { + this.definitions = props.definitions; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A `const` statement" +}, AST_Definitions); +var AST_VarDef = DEFNODE("VarDef", "name value", function AST_VarDef2(props) { + if (props) { + this.name = props.name; + this.value = props.value; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A variable declaration; only appears in a AST_Definitions node", + $propdoc: { + name: "[AST_Destructuring|AST_SymbolConst|AST_SymbolLet|AST_SymbolVar] name of the variable", + value: "[AST_Node?] initializer, or null of there's no initializer" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + this.name._walk(visitor); + if (this.value) + this.value._walk(visitor); + }); + }, + _children_backwards(push) { + if (this.value) + push(this.value); + push(this.name); + }, + declarations_as_names() { + if (this.name instanceof AST_SymbolDeclaration) { + return [this]; + } else { + return this.name.all_symbols(); + } + } +}); +var AST_NameMapping = DEFNODE("NameMapping", "foreign_name name", function AST_NameMapping2(props) { + if (props) { + this.foreign_name = props.foreign_name; + this.name = props.name; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "The part of the export/import statement that declare names from a module.", + $propdoc: { + foreign_name: "[AST_SymbolExportForeign|AST_SymbolImportForeign] The name being exported/imported (as specified in the module)", + name: "[AST_SymbolExport|AST_SymbolImport] The name as it is visible to this module." + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + this.foreign_name._walk(visitor); + this.name._walk(visitor); + }); + }, + _children_backwards(push) { + push(this.name); + push(this.foreign_name); + } +}); +var AST_Import = DEFNODE("Import", "imported_name imported_names module_name attributes", function AST_Import2(props) { + if (props) { + this.imported_name = props.imported_name; + this.imported_names = props.imported_names; + this.module_name = props.module_name; + this.attributes = props.attributes; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "An `import` statement", + $propdoc: { + imported_name: "[AST_SymbolImport] The name of the variable holding the module's default export.", + imported_names: "[AST_NameMapping*] The names of non-default imported variables", + module_name: "[AST_String] String literal describing where this module came from", + attributes: "[AST_Object?] The import attributes (with {...})" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + if (this.imported_name) { + this.imported_name._walk(visitor); + } + if (this.imported_names) { + this.imported_names.forEach(function(name_import) { + name_import._walk(visitor); + }); + } + this.module_name._walk(visitor); + }); + }, + _children_backwards(push) { + push(this.module_name); + if (this.imported_names) { + let i = this.imported_names.length; + while (i--) + push(this.imported_names[i]); + } + if (this.imported_name) + push(this.imported_name); + } +}); +var AST_ImportMeta = DEFNODE("ImportMeta", null, function AST_ImportMeta2(props) { + if (props) { + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A reference to import.meta" +}); +var AST_Export = DEFNODE("Export", "exported_definition exported_value is_default exported_names module_name attributes", function AST_Export2(props) { + if (props) { + this.exported_definition = props.exported_definition; + this.exported_value = props.exported_value; + this.is_default = props.is_default; + this.exported_names = props.exported_names; + this.module_name = props.module_name; + this.attributes = props.attributes; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "An `export` statement", + $propdoc: { + exported_definition: "[AST_Defun|AST_Definitions|AST_DefClass?] An exported definition", + exported_value: "[AST_Node?] An exported value", + exported_names: "[AST_NameMapping*?] List of exported names", + module_name: "[AST_String?] Name of the file to load exports from", + is_default: "[Boolean] Whether this is the default exported value of this module", + attributes: "[AST_Object?] The import attributes" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + if (this.exported_definition) { + this.exported_definition._walk(visitor); + } + if (this.exported_value) { + this.exported_value._walk(visitor); + } + if (this.exported_names) { + this.exported_names.forEach(function(name_export) { + name_export._walk(visitor); + }); + } + if (this.module_name) { + this.module_name._walk(visitor); + } + }); + }, + _children_backwards(push) { + if (this.module_name) + push(this.module_name); + if (this.exported_names) { + let i = this.exported_names.length; + while (i--) + push(this.exported_names[i]); + } + if (this.exported_value) + push(this.exported_value); + if (this.exported_definition) + push(this.exported_definition); + } +}, AST_Statement); +var AST_Call = DEFNODE("Call", "expression args optional _annotations", function AST_Call2(props) { + if (props) { + this.expression = props.expression; + this.args = props.args; + this.optional = props.optional; + this._annotations = props._annotations; + this.start = props.start; + this.end = props.end; + this.initialize(); + } + this.flags = 0; +}, { + $documentation: "A function call expression", + $propdoc: { + expression: "[AST_Node] expression to invoke as function", + args: "[AST_Node*] array of arguments", + optional: "[boolean] whether this is an optional call (IE ?.() )", + _annotations: "[number] bitfield containing information about the call" + }, + initialize() { + if (this._annotations == null) + this._annotations = 0; + }, + _walk(visitor) { + return visitor._visit(this, function() { + var args = this.args; + for (var i = 0, len = args.length;i < len; i++) { + args[i]._walk(visitor); + } + this.expression._walk(visitor); + }); + }, + _children_backwards(push) { + let i = this.args.length; + while (i--) + push(this.args[i]); + push(this.expression); + } +}); +var AST_New = DEFNODE("New", null, function AST_New2(props) { + if (props) { + this.expression = props.expression; + this.args = props.args; + this.optional = props.optional; + this._annotations = props._annotations; + this.start = props.start; + this.end = props.end; + this.initialize(); + } + this.flags = 0; +}, { + $documentation: "An object instantiation. Derives from a function call since it has exactly the same properties" +}, AST_Call); +var AST_Sequence = DEFNODE("Sequence", "expressions", function AST_Sequence2(props) { + if (props) { + this.expressions = props.expressions; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A sequence expression (comma-separated expressions)", + $propdoc: { + expressions: "[AST_Node*] array of expressions (at least two)" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + this.expressions.forEach(function(node) { + node._walk(visitor); + }); + }); + }, + _children_backwards(push) { + let i = this.expressions.length; + while (i--) + push(this.expressions[i]); + } +}); +var AST_PropAccess = DEFNODE("PropAccess", "expression property optional", function AST_PropAccess2(props) { + if (props) { + this.expression = props.expression; + this.property = props.property; + this.optional = props.optional; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: 'Base class for property access expressions, i.e. `a.foo` or `a["foo"]`', + $propdoc: { + expression: "[AST_Node] the “container” expression", + property: "[AST_Node|string] the property to access. For AST_Dot & AST_DotHash this is always a plain string, while for AST_Sub it's an arbitrary AST_Node", + optional: "[boolean] whether this is an optional property access (IE ?.)" + } +}); +var AST_Dot = DEFNODE("Dot", "quote", function AST_Dot2(props) { + if (props) { + this.quote = props.quote; + this.expression = props.expression; + this.property = props.property; + this.optional = props.optional; + this._annotations = props._annotations; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A dotted property access expression", + $propdoc: { + quote: "[string] the original quote character when transformed from AST_Sub" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + this.expression._walk(visitor); + }); + }, + _children_backwards(push) { + push(this.expression); + } +}, AST_PropAccess); +var AST_DotHash = DEFNODE("DotHash", "", function AST_DotHash2(props) { + if (props) { + this.expression = props.expression; + this.property = props.property; + this.optional = props.optional; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A dotted property access to a private property", + _walk: function(visitor) { + return visitor._visit(this, function() { + this.expression._walk(visitor); + }); + }, + _children_backwards(push) { + push(this.expression); + } +}, AST_PropAccess); +var AST_Sub = DEFNODE("Sub", null, function AST_Sub2(props) { + if (props) { + this.expression = props.expression; + this.property = props.property; + this.optional = props.optional; + this._annotations = props._annotations; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: 'Index-style property access, i.e. `a["foo"]`', + _walk: function(visitor) { + return visitor._visit(this, function() { + this.expression._walk(visitor); + this.property._walk(visitor); + }); + }, + _children_backwards(push) { + push(this.property); + push(this.expression); + } +}, AST_PropAccess); +var AST_Chain = DEFNODE("Chain", "expression", function AST_Chain2(props) { + if (props) { + this.expression = props.expression; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A chain expression like a?.b?.(c)?.[d]", + $propdoc: { + expression: "[AST_Call|AST_Dot|AST_DotHash|AST_Sub] chain element." + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + this.expression._walk(visitor); + }); + }, + _children_backwards(push) { + push(this.expression); + } +}); +var AST_Unary = DEFNODE("Unary", "operator expression", function AST_Unary2(props) { + if (props) { + this.operator = props.operator; + this.expression = props.expression; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Base class for unary expressions", + $propdoc: { + operator: "[string] the operator", + expression: "[AST_Node] expression that this unary operator applies to" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + this.expression._walk(visitor); + }); + }, + _children_backwards(push) { + push(this.expression); + } +}); +var AST_UnaryPrefix = DEFNODE("UnaryPrefix", null, function AST_UnaryPrefix2(props) { + if (props) { + this.operator = props.operator; + this.expression = props.expression; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Unary prefix expression, i.e. `typeof i` or `++i`" +}, AST_Unary); +var AST_UnaryPostfix = DEFNODE("UnaryPostfix", null, function AST_UnaryPostfix2(props) { + if (props) { + this.operator = props.operator; + this.expression = props.expression; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Unary postfix expression, i.e. `i++`" +}, AST_Unary); +var AST_Binary = DEFNODE("Binary", "operator left right", function AST_Binary2(props) { + if (props) { + this.operator = props.operator; + this.left = props.left; + this.right = props.right; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Binary expression, i.e. `a + b`", + $propdoc: { + left: "[AST_Node] left-hand side expression", + operator: "[string] the operator", + right: "[AST_Node] right-hand side expression" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + this.left._walk(visitor); + this.right._walk(visitor); + }); + }, + _children_backwards(push) { + push(this.right); + push(this.left); + } +}); +var AST_Conditional = DEFNODE("Conditional", "condition consequent alternative", function AST_Conditional2(props) { + if (props) { + this.condition = props.condition; + this.consequent = props.consequent; + this.alternative = props.alternative; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Conditional expression using the ternary operator, i.e. `a ? b : c`", + $propdoc: { + condition: "[AST_Node]", + consequent: "[AST_Node]", + alternative: "[AST_Node]" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + this.condition._walk(visitor); + this.consequent._walk(visitor); + this.alternative._walk(visitor); + }); + }, + _children_backwards(push) { + push(this.alternative); + push(this.consequent); + push(this.condition); + } +}); +var AST_Assign = DEFNODE("Assign", "logical", function AST_Assign2(props) { + if (props) { + this.logical = props.logical; + this.operator = props.operator; + this.left = props.left; + this.right = props.right; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "An assignment expression — `a = b + 5`", + $propdoc: { + logical: "Whether it's a logical assignment" + } +}, AST_Binary); +var AST_DefaultAssign = DEFNODE("DefaultAssign", null, function AST_DefaultAssign2(props) { + if (props) { + this.operator = props.operator; + this.left = props.left; + this.right = props.right; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A default assignment expression like in `(a = 3) => a`" +}, AST_Binary); +var AST_Array = DEFNODE("Array", "elements", function AST_Array2(props) { + if (props) { + this.elements = props.elements; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "An array literal", + $propdoc: { + elements: "[AST_Node*] array of elements" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + var elements = this.elements; + for (var i = 0, len = elements.length;i < len; i++) { + elements[i]._walk(visitor); + } + }); + }, + _children_backwards(push) { + let i = this.elements.length; + while (i--) + push(this.elements[i]); + } +}); +var AST_Object = DEFNODE("Object", "properties", function AST_Object2(props) { + if (props) { + this.properties = props.properties; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "An object literal", + $propdoc: { + properties: "[AST_ObjectProperty*] array of properties" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + var properties = this.properties; + for (var i = 0, len = properties.length;i < len; i++) { + properties[i]._walk(visitor); + } + }); + }, + _children_backwards(push) { + let i = this.properties.length; + while (i--) + push(this.properties[i]); + } +}); +var AST_ObjectProperty = DEFNODE("ObjectProperty", "key value", function AST_ObjectProperty2(props) { + if (props) { + this.key = props.key; + this.value = props.value; + this.start = props.start; + this.end = props.end; + this._annotations = props._annotations; + } + this.flags = 0; +}, { + $documentation: "Base class for literal object properties", + $propdoc: { + key: "[string|AST_Node] property name. For ObjectKeyVal this is a string. For getters, setters and computed property this is an AST_Node.", + value: "[AST_Node] property value. For getters, setters and methods this is an AST_Accessor." + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + if (this.key instanceof AST_Node) + this.key._walk(visitor); + this.value._walk(visitor); + }); + }, + _children_backwards(push) { + push(this.value); + if (this.key instanceof AST_Node) + push(this.key); + } +}); +var AST_ObjectKeyVal = DEFNODE("ObjectKeyVal", "quote", function AST_ObjectKeyVal2(props) { + if (props) { + this.quote = props.quote; + this.key = props.key; + this.value = props.value; + this.start = props.start; + this.end = props.end; + this._annotations = props._annotations; + } + this.flags = 0; +}, { + $documentation: "A key: value object property", + $propdoc: { + quote: "[string] the original quote character" + }, + computed_key() { + return this.key instanceof AST_Node; + } +}, AST_ObjectProperty); +var AST_PrivateSetter = DEFNODE("PrivateSetter", "static", function AST_PrivateSetter2(props) { + if (props) { + this.static = props.static; + this.key = props.key; + this.value = props.value; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $propdoc: { + static: "[boolean] whether this is a static private setter" + }, + $documentation: "A private setter property", + computed_key() { + return false; + } +}, AST_ObjectProperty); +var AST_PrivateGetter = DEFNODE("PrivateGetter", "static", function AST_PrivateGetter2(props) { + if (props) { + this.static = props.static; + this.key = props.key; + this.value = props.value; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $propdoc: { + static: "[boolean] whether this is a static private getter" + }, + $documentation: "A private getter property", + computed_key() { + return false; + } +}, AST_ObjectProperty); +var AST_ObjectSetter = DEFNODE("ObjectSetter", "quote static", function AST_ObjectSetter2(props) { + if (props) { + this.quote = props.quote; + this.static = props.static; + this.key = props.key; + this.value = props.value; + this.start = props.start; + this.end = props.end; + this._annotations = props._annotations; + } + this.flags = 0; +}, { + $propdoc: { + quote: "[string|undefined] the original quote character, if any", + static: "[boolean] whether this is a static setter (classes only)" + }, + $documentation: "An object setter property", + computed_key() { + return !(this.key instanceof AST_SymbolMethod); + } +}, AST_ObjectProperty); +var AST_ObjectGetter = DEFNODE("ObjectGetter", "quote static", function AST_ObjectGetter2(props) { + if (props) { + this.quote = props.quote; + this.static = props.static; + this.key = props.key; + this.value = props.value; + this.start = props.start; + this.end = props.end; + this._annotations = props._annotations; + } + this.flags = 0; +}, { + $propdoc: { + quote: "[string|undefined] the original quote character, if any", + static: "[boolean] whether this is a static getter (classes only)" + }, + $documentation: "An object getter property", + computed_key() { + return !(this.key instanceof AST_SymbolMethod); + } +}, AST_ObjectProperty); +var AST_ConciseMethod = DEFNODE("ConciseMethod", "quote static", function AST_ConciseMethod2(props) { + if (props) { + this.quote = props.quote; + this.static = props.static; + this.key = props.key; + this.value = props.value; + this.start = props.start; + this.end = props.end; + this._annotations = props._annotations; + } + this.flags = 0; +}, { + $propdoc: { + quote: "[string|undefined] the original quote character, if any", + static: "[boolean] is this method static (classes only)" + }, + $documentation: "An ES6 concise method inside an object or class", + computed_key() { + return !(this.key instanceof AST_SymbolMethod); + } +}, AST_ObjectProperty); +var AST_PrivateMethod = DEFNODE("PrivateMethod", "static", function AST_PrivateMethod2(props) { + if (props) { + this.static = props.static; + this.key = props.key; + this.value = props.value; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A private class method inside a class", + $propdoc: { + static: "[boolean] is this a static private method" + }, + computed_key() { + return false; + } +}, AST_ObjectProperty); +var AST_Class = DEFNODE("Class", "name extends properties", function AST_Class2(props) { + if (props) { + this.name = props.name; + this.extends = props.extends; + this.properties = props.properties; + this.variables = props.variables; + this.uses_with = props.uses_with; + this.uses_eval = props.uses_eval; + this.parent_scope = props.parent_scope; + this.enclosed = props.enclosed; + this.cname = props.cname; + this.body = props.body; + this.block_scope = props.block_scope; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $propdoc: { + name: "[AST_SymbolClass|AST_SymbolDefClass?] optional class name.", + extends: "[AST_Node]? optional parent class", + properties: "[AST_ObjectProperty|AST_ClassStaticBlock]* array of properties or static blocks" + }, + $documentation: "An ES6 class", + _walk: function(visitor) { + return visitor._visit(this, function() { + if (this.name) { + this.name._walk(visitor); + } + if (this.extends) { + this.extends._walk(visitor); + } + this.properties.forEach((prop) => prop._walk(visitor)); + }); + }, + _children_backwards(push) { + let i = this.properties.length; + while (i--) + push(this.properties[i]); + if (this.extends) + push(this.extends); + if (this.name) + push(this.name); + }, + visit_nondeferred_class_parts(visitor) { + if (this.extends) { + this.extends._walk(visitor); + } + this.properties.forEach((prop) => { + if (prop instanceof AST_ClassStaticBlock) { + prop._walk(visitor); + return; + } + if (prop.computed_key()) { + visitor.push(prop); + prop.key._walk(visitor); + visitor.pop(); + } + if (prop instanceof AST_ClassPrivateProperty && prop.static && prop.value || prop instanceof AST_ClassProperty && prop.static && prop.value) { + visitor.push(prop); + prop.value._walk(visitor); + visitor.pop(); + } + }); + }, + visit_deferred_class_parts(visitor) { + this.properties.forEach((prop) => { + if (prop instanceof AST_ConciseMethod || prop instanceof AST_PrivateMethod) { + prop.walk(visitor); + } else if (prop instanceof AST_ClassProperty && !prop.static && prop.value || prop instanceof AST_ClassPrivateProperty && !prop.static && prop.value) { + visitor.push(prop); + prop.value._walk(visitor); + visitor.pop(); + } + }); + }, + is_self_referential: function() { + const this_id = this.name && this.name.definition().id; + let found = false; + let class_this = true; + this.visit_nondeferred_class_parts(new TreeWalker((node, descend) => { + if (found) + return true; + if (node instanceof AST_This) + return found = class_this; + if (node instanceof AST_SymbolRef) + return found = node.definition().id === this_id; + if (node instanceof AST_Lambda && !(node instanceof AST_Arrow)) { + const class_this_save = class_this; + class_this = false; + descend(); + class_this = class_this_save; + return true; + } + })); + return found; + } +}, AST_Scope); +var AST_ClassProperty = DEFNODE("ClassProperty", "static quote", function AST_ClassProperty2(props) { + if (props) { + this.static = props.static; + this.quote = props.quote; + this.key = props.key; + this.value = props.value; + this.start = props.start; + this.end = props.end; + this._annotations = props._annotations; + } + this.flags = 0; +}, { + $documentation: "A class property", + $propdoc: { + static: "[boolean] whether this is a static key", + quote: "[string] which quote is being used" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + if (this.key instanceof AST_Node) + this.key._walk(visitor); + if (this.value instanceof AST_Node) + this.value._walk(visitor); + }); + }, + _children_backwards(push) { + if (this.value instanceof AST_Node) + push(this.value); + if (this.key instanceof AST_Node) + push(this.key); + }, + computed_key() { + return !(this.key instanceof AST_SymbolClassProperty); + } +}, AST_ObjectProperty); +var AST_ClassPrivateProperty = DEFNODE("ClassPrivateProperty", "", function AST_ClassPrivateProperty2(props) { + if (props) { + this.static = props.static; + this.key = props.key; + this.value = props.value; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A class property for a private property", + _walk: function(visitor) { + return visitor._visit(this, function() { + if (this.value instanceof AST_Node) + this.value._walk(visitor); + }); + }, + _children_backwards(push) { + if (this.value instanceof AST_Node) + push(this.value); + }, + computed_key() { + return false; + } +}, AST_ObjectProperty); +var AST_PrivateIn = DEFNODE("PrivateIn", "key value", function AST_PrivateIn2(props) { + if (props) { + this.key = props.key; + this.value = props.value; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "An `in` binop when the key is private, eg #x in this", + _walk: function(visitor) { + return visitor._visit(this, function() { + this.key._walk(visitor); + this.value._walk(visitor); + }); + }, + _children_backwards(push) { + push(this.value); + push(this.key); + } +}); +var AST_DefClass = DEFNODE("DefClass", null, function AST_DefClass2(props) { + if (props) { + this.name = props.name; + this.extends = props.extends; + this.properties = props.properties; + this.variables = props.variables; + this.uses_with = props.uses_with; + this.uses_eval = props.uses_eval; + this.parent_scope = props.parent_scope; + this.enclosed = props.enclosed; + this.cname = props.cname; + this.body = props.body; + this.block_scope = props.block_scope; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A class definition" +}, AST_Class); +var AST_ClassStaticBlock = DEFNODE("ClassStaticBlock", "body block_scope", function AST_ClassStaticBlock2(props) { + this.body = props.body; + this.block_scope = props.block_scope; + this.start = props.start; + this.end = props.end; +}, { + $documentation: "A block containing statements to be executed in the context of the class", + $propdoc: { + body: "[AST_Statement*] an array of statements" + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + walk_body(this, visitor); + }); + }, + _children_backwards(push) { + let i = this.body.length; + while (i--) + push(this.body[i]); + }, + clone: clone_block_scope, + computed_key() { + return false; + } +}, AST_Scope); +var AST_ClassExpression = DEFNODE("ClassExpression", null, function AST_ClassExpression2(props) { + if (props) { + this.name = props.name; + this.extends = props.extends; + this.properties = props.properties; + this.variables = props.variables; + this.uses_with = props.uses_with; + this.uses_eval = props.uses_eval; + this.parent_scope = props.parent_scope; + this.enclosed = props.enclosed; + this.cname = props.cname; + this.body = props.body; + this.block_scope = props.block_scope; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A class expression." +}, AST_Class); +var AST_Symbol = DEFNODE("Symbol", "scope name thedef", function AST_Symbol2(props) { + if (props) { + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $propdoc: { + name: "[string] name of this symbol", + scope: "[AST_Scope/S] the current scope (not necessarily the definition scope)", + thedef: "[SymbolDef/S] the definition of this symbol" + }, + $documentation: "Base class for all symbols" +}); +var AST_NewTarget = DEFNODE("NewTarget", null, function AST_NewTarget2(props) { + if (props) { + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A reference to new.target" +}); +var AST_SymbolDeclaration = DEFNODE("SymbolDeclaration", "init", function AST_SymbolDeclaration2(props) { + if (props) { + this.init = props.init; + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A declaration symbol (symbol in var/const, function name or argument, symbol in catch)" +}, AST_Symbol); +var AST_SymbolVar = DEFNODE("SymbolVar", null, function AST_SymbolVar2(props) { + if (props) { + this.init = props.init; + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Symbol defining a variable" +}, AST_SymbolDeclaration); +var AST_SymbolBlockDeclaration = DEFNODE("SymbolBlockDeclaration", null, function AST_SymbolBlockDeclaration2(props) { + if (props) { + this.init = props.init; + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Base class for block-scoped declaration symbols" +}, AST_SymbolDeclaration); +var AST_SymbolConst = DEFNODE("SymbolConst", null, function AST_SymbolConst2(props) { + if (props) { + this.init = props.init; + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A constant declaration" +}, AST_SymbolBlockDeclaration); +var AST_SymbolLet = DEFNODE("SymbolLet", null, function AST_SymbolLet2(props) { + if (props) { + this.init = props.init; + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A block-scoped `let` declaration" +}, AST_SymbolBlockDeclaration); +var AST_SymbolFunarg = DEFNODE("SymbolFunarg", null, function AST_SymbolFunarg2(props) { + if (props) { + this.init = props.init; + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Symbol naming a function argument" +}, AST_SymbolVar); +var AST_SymbolDefun = DEFNODE("SymbolDefun", null, function AST_SymbolDefun2(props) { + if (props) { + this.init = props.init; + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Symbol defining a function" +}, AST_SymbolDeclaration); +var AST_SymbolMethod = DEFNODE("SymbolMethod", null, function AST_SymbolMethod2(props) { + if (props) { + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Symbol in an object defining a method" +}, AST_Symbol); +var AST_SymbolClassProperty = DEFNODE("SymbolClassProperty", null, function AST_SymbolClassProperty2(props) { + if (props) { + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Symbol for a class property" +}, AST_Symbol); +var AST_SymbolLambda = DEFNODE("SymbolLambda", null, function AST_SymbolLambda2(props) { + if (props) { + this.init = props.init; + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Symbol naming a function expression" +}, AST_SymbolDeclaration); +var AST_SymbolDefClass = DEFNODE("SymbolDefClass", null, function AST_SymbolDefClass2(props) { + if (props) { + this.init = props.init; + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Symbol naming a class's name in a class declaration. Lexically scoped to its containing scope, and accessible within the class." +}, AST_SymbolBlockDeclaration); +var AST_SymbolClass = DEFNODE("SymbolClass", null, function AST_SymbolClass2(props) { + if (props) { + this.init = props.init; + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Symbol naming a class's name. Lexically scoped to the class." +}, AST_SymbolDeclaration); +var AST_SymbolCatch = DEFNODE("SymbolCatch", null, function AST_SymbolCatch2(props) { + if (props) { + this.init = props.init; + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Symbol naming the exception in catch" +}, AST_SymbolBlockDeclaration); +var AST_SymbolImport = DEFNODE("SymbolImport", null, function AST_SymbolImport2(props) { + if (props) { + this.init = props.init; + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Symbol referring to an imported name" +}, AST_SymbolBlockDeclaration); +var AST_SymbolImportForeign = DEFNODE("SymbolImportForeign", "quote", function AST_SymbolImportForeign2(props) { + if (props) { + this.quote = props.quote; + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A symbol imported from a module, but it is defined in the other module, and its real name is irrelevant for this module's purposes" +}, AST_Symbol); +var AST_Label = DEFNODE("Label", "references", function AST_Label2(props) { + if (props) { + this.references = props.references; + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + this.initialize(); + } + this.flags = 0; +}, { + $documentation: "Symbol naming a label (declaration)", + $propdoc: { + references: "[AST_LoopControl*] a list of nodes referring to this label" + }, + initialize: function() { + this.references = []; + this.thedef = this; + } +}, AST_Symbol); +var AST_SymbolRef = DEFNODE("SymbolRef", null, function AST_SymbolRef2(props) { + if (props) { + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Reference to some symbol (not definition/declaration)" +}, AST_Symbol); +var AST_SymbolExport = DEFNODE("SymbolExport", "quote", function AST_SymbolExport2(props) { + if (props) { + this.quote = props.quote; + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Symbol referring to a name to export" +}, AST_SymbolRef); +var AST_SymbolExportForeign = DEFNODE("SymbolExportForeign", "quote", function AST_SymbolExportForeign2(props) { + if (props) { + this.quote = props.quote; + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A symbol exported from this module, but it is used in the other module, and its real name is irrelevant for this module's purposes" +}, AST_Symbol); +var AST_LabelRef = DEFNODE("LabelRef", null, function AST_LabelRef2(props) { + if (props) { + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Reference to a label symbol" +}, AST_Symbol); +var AST_SymbolPrivateProperty = DEFNODE("SymbolPrivateProperty", null, function AST_SymbolPrivateProperty2(props) { + if (props) { + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A symbol that refers to a private property" +}, AST_Symbol); +var AST_This = DEFNODE("This", null, function AST_This2(props) { + if (props) { + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "The `this` symbol" +}, AST_Symbol); +var AST_Super = DEFNODE("Super", null, function AST_Super2(props) { + if (props) { + this.scope = props.scope; + this.name = props.name; + this.thedef = props.thedef; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "The `super` symbol" +}, AST_This); +var AST_Constant = DEFNODE("Constant", null, function AST_Constant2(props) { + if (props) { + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Base class for all constants", + getValue: function() { + return this.value; + } +}); +var AST_String = DEFNODE("String", "value quote", function AST_String2(props) { + if (props) { + this.value = props.value; + this.quote = props.quote; + this.start = props.start; + this.end = props.end; + this._annotations = props._annotations; + } + this.flags = 0; +}, { + $documentation: "A string literal", + $propdoc: { + value: "[string] the contents of this string", + quote: "[string] the original quote character" + } +}, AST_Constant); +var AST_Number = DEFNODE("Number", "value raw", function AST_Number2(props) { + if (props) { + this.value = props.value; + this.raw = props.raw; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A number literal", + $propdoc: { + value: "[number] the numeric value", + raw: "[string] numeric value as string" + } +}, AST_Constant); +var AST_BigInt = DEFNODE("BigInt", "value raw", function AST_BigInt2(props) { + if (props) { + this.value = props.value; + this.raw = props.raw; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A big int literal", + $propdoc: { + value: "[string] big int value, represented as a string", + raw: "[string] the original format preserved" + } +}, AST_Constant); +var AST_RegExp = DEFNODE("RegExp", "value", function AST_RegExp2(props) { + if (props) { + this.value = props.value; + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A regexp literal", + $propdoc: { + value: "[RegExp] the actual regexp" + } +}, AST_Constant); +var AST_Atom = DEFNODE("Atom", null, function AST_Atom2(props) { + if (props) { + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Base class for atoms" +}, AST_Constant); +var AST_Null = DEFNODE("Null", null, function AST_Null2(props) { + if (props) { + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "The `null` atom", + value: null +}, AST_Atom); +var AST_NaN = DEFNODE("NaN", null, function AST_NaN2(props) { + if (props) { + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "The impossible value", + value: 0 / 0 +}, AST_Atom); +var AST_Undefined = DEFNODE("Undefined", null, function AST_Undefined2(props) { + if (props) { + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "The `undefined` value", + value: function() {}() +}, AST_Atom); +var AST_Hole = DEFNODE("Hole", null, function AST_Hole2(props) { + if (props) { + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "A hole in an array", + value: function() {}() +}, AST_Atom); +var AST_Infinity = DEFNODE("Infinity", null, function AST_Infinity2(props) { + if (props) { + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "The `Infinity` value", + value: 1 / 0 +}, AST_Atom); +var AST_Boolean = DEFNODE("Boolean", null, function AST_Boolean2(props) { + if (props) { + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "Base class for booleans" +}, AST_Atom); +var AST_False = DEFNODE("False", null, function AST_False2(props) { + if (props) { + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "The `false` atom", + value: false +}, AST_Boolean); +var AST_True = DEFNODE("True", null, function AST_True2(props) { + if (props) { + this.start = props.start; + this.end = props.end; + } + this.flags = 0; +}, { + $documentation: "The `true` atom", + value: true +}, AST_Boolean); +function walk(node, cb, to_visit = [node]) { + const push = to_visit.push.bind(to_visit); + while (to_visit.length) { + const node2 = to_visit.pop(); + const ret = cb(node2, to_visit); + if (ret) { + if (ret === walk_abort) + return true; + continue; + } + node2._children_backwards(push); + } + return false; +} +function walk_parent(node, cb, initial_stack) { + const to_visit = [node]; + const push = to_visit.push.bind(to_visit); + const stack = initial_stack ? initial_stack.slice() : []; + const parent_pop_indices = []; + let current; + const info = { + parent: (n = 0) => { + if (n === -1) { + return current; + } + if (initial_stack && n >= stack.length) { + n -= stack.length; + return initial_stack[initial_stack.length - (n + 1)]; + } + return stack[stack.length - (1 + n)]; + } + }; + while (to_visit.length) { + current = to_visit.pop(); + while (parent_pop_indices.length && to_visit.length == parent_pop_indices[parent_pop_indices.length - 1]) { + stack.pop(); + parent_pop_indices.pop(); + } + const ret = cb(current, info); + if (ret) { + if (ret === walk_abort) + return true; + continue; + } + const visit_length = to_visit.length; + current._children_backwards(push); + if (to_visit.length > visit_length) { + stack.push(current); + parent_pop_indices.push(visit_length - 1); + } + } + return false; +} +var walk_abort = Symbol("abort walk"); + +class TreeWalker { + constructor(callback) { + this.visit = callback; + this.stack = []; + this.directives = Object.create(null); + } + _visit(node, descend) { + this.push(node); + var ret = this.visit(node, descend ? function() { + descend.call(node); + } : noop); + if (!ret && descend) { + descend.call(node); + } + this.pop(); + return ret; + } + parent(n) { + return this.stack[this.stack.length - 2 - (n || 0)]; + } + push(node) { + if (node instanceof AST_Lambda) { + this.directives = Object.create(this.directives); + } else if (node instanceof AST_Directive && !this.directives[node.value]) { + this.directives[node.value] = node; + } else if (node instanceof AST_Class) { + this.directives = Object.create(this.directives); + if (!this.directives["use strict"]) { + this.directives["use strict"] = node; + } + } + this.stack.push(node); + } + pop() { + var node = this.stack.pop(); + if (node instanceof AST_Lambda || node instanceof AST_Class) { + this.directives = Object.getPrototypeOf(this.directives); + } + } + self() { + return this.stack[this.stack.length - 1]; + } + find_parent(type) { + var stack = this.stack; + for (var i = stack.length;--i >= 0; ) { + var x = stack[i]; + if (x instanceof type) + return x; + } + } + is_within_loop() { + let i = this.stack.length - 1; + let child = this.stack[i]; + while (i--) { + const node = this.stack[i]; + if (node instanceof AST_Lambda) + return false; + if (node instanceof AST_IterationStatement && !(node instanceof AST_For && child === node.init) && !((node instanceof AST_ForIn || node instanceof AST_ForOf) && child === node.object)) { + return true; + } + child = node; + } + return false; + } + find_scope() { + var stack = this.stack; + for (var i = stack.length;--i >= 0; ) { + const p = stack[i]; + if (p instanceof AST_Toplevel) + return p; + if (p instanceof AST_Lambda) + return p; + if (p.block_scope) + return p.block_scope; + } + } + has_directive(type) { + var dir = this.directives[type]; + if (dir) + return dir; + var node = this.stack[this.stack.length - 1]; + if (node instanceof AST_Scope && node.body) { + for (var i = 0;i < node.body.length; ++i) { + var st = node.body[i]; + if (!(st instanceof AST_Directive)) + break; + if (st.value == type) + return st; + } + } + } + loopcontrol_target(node) { + var stack = this.stack; + if (node.label) + for (var i = stack.length;--i >= 0; ) { + var x = stack[i]; + if (x instanceof AST_LabeledStatement && x.label.name == node.label.name) + return x.body; + } + else + for (var i = stack.length;--i >= 0; ) { + var x = stack[i]; + if (x instanceof AST_IterationStatement || node instanceof AST_Break && x instanceof AST_Switch) + return x; + } + } +} + +class TreeTransformer extends TreeWalker { + constructor(before, after) { + super(); + this.before = before; + this.after = after; + } +} +var _PURE = 1; +var _INLINE = 2; +var _NOINLINE = 4; +var _KEY = 8; +var _MANGLEPROP = 16; + +// node_modules/terser/lib/transform.js +function def_transform(node, descend) { + node.DEFMETHOD("transform", function(tw, in_list) { + let transformed = undefined; + tw.push(this); + if (tw.before) + transformed = tw.before(this, descend, in_list); + if (transformed === undefined) { + transformed = this; + descend(transformed, tw); + if (tw.after) { + const after_ret = tw.after(transformed, in_list); + if (after_ret !== undefined) + transformed = after_ret; + } + } + tw.pop(); + return transformed; + }); +} +def_transform(AST_Node, noop); +def_transform(AST_LabeledStatement, function(self2, tw) { + self2.label = self2.label.transform(tw); + self2.body = self2.body.transform(tw); +}); +def_transform(AST_SimpleStatement, function(self2, tw) { + self2.body = self2.body.transform(tw); +}); +def_transform(AST_Block, function(self2, tw) { + self2.body = MAP(self2.body, tw); +}); +def_transform(AST_Do, function(self2, tw) { + self2.body = self2.body.transform(tw); + self2.condition = self2.condition.transform(tw); +}); +def_transform(AST_While, function(self2, tw) { + self2.condition = self2.condition.transform(tw); + self2.body = self2.body.transform(tw); +}); +def_transform(AST_For, function(self2, tw) { + if (self2.init) + self2.init = self2.init.transform(tw); + if (self2.condition) + self2.condition = self2.condition.transform(tw); + if (self2.step) + self2.step = self2.step.transform(tw); + self2.body = self2.body.transform(tw); +}); +def_transform(AST_ForIn, function(self2, tw) { + self2.init = self2.init.transform(tw); + self2.object = self2.object.transform(tw); + self2.body = self2.body.transform(tw); +}); +def_transform(AST_With, function(self2, tw) { + self2.expression = self2.expression.transform(tw); + self2.body = self2.body.transform(tw); +}); +def_transform(AST_Exit, function(self2, tw) { + if (self2.value) + self2.value = self2.value.transform(tw); +}); +def_transform(AST_LoopControl, function(self2, tw) { + if (self2.label) + self2.label = self2.label.transform(tw); +}); +def_transform(AST_If, function(self2, tw) { + self2.condition = self2.condition.transform(tw); + self2.body = self2.body.transform(tw); + if (self2.alternative) + self2.alternative = self2.alternative.transform(tw); +}); +def_transform(AST_Switch, function(self2, tw) { + self2.expression = self2.expression.transform(tw); + self2.body = MAP(self2.body, tw); +}); +def_transform(AST_Case, function(self2, tw) { + self2.expression = self2.expression.transform(tw); + self2.body = MAP(self2.body, tw); +}); +def_transform(AST_Try, function(self2, tw) { + self2.body = self2.body.transform(tw); + if (self2.bcatch) + self2.bcatch = self2.bcatch.transform(tw); + if (self2.bfinally) + self2.bfinally = self2.bfinally.transform(tw); +}); +def_transform(AST_Catch, function(self2, tw) { + if (self2.argname) + self2.argname = self2.argname.transform(tw); + self2.body = MAP(self2.body, tw); +}); +def_transform(AST_Definitions, function(self2, tw) { + self2.definitions = MAP(self2.definitions, tw); +}); +def_transform(AST_VarDef, function(self2, tw) { + self2.name = self2.name.transform(tw); + if (self2.value) + self2.value = self2.value.transform(tw); +}); +def_transform(AST_Destructuring, function(self2, tw) { + self2.names = MAP(self2.names, tw); +}); +def_transform(AST_Lambda, function(self2, tw) { + if (self2.name) + self2.name = self2.name.transform(tw); + self2.argnames = MAP(self2.argnames, tw, false); + if (self2.body instanceof AST_Node) { + self2.body = self2.body.transform(tw); + } else { + self2.body = MAP(self2.body, tw); + } +}); +def_transform(AST_Call, function(self2, tw) { + self2.expression = self2.expression.transform(tw); + self2.args = MAP(self2.args, tw, false); +}); +def_transform(AST_Sequence, function(self2, tw) { + const result = MAP(self2.expressions, tw); + self2.expressions = result.length ? result : [new AST_Number({ value: 0 })]; +}); +def_transform(AST_PropAccess, function(self2, tw) { + self2.expression = self2.expression.transform(tw); +}); +def_transform(AST_Sub, function(self2, tw) { + self2.expression = self2.expression.transform(tw); + self2.property = self2.property.transform(tw); +}); +def_transform(AST_Chain, function(self2, tw) { + self2.expression = self2.expression.transform(tw); +}); +def_transform(AST_Yield, function(self2, tw) { + if (self2.expression) + self2.expression = self2.expression.transform(tw); +}); +def_transform(AST_Await, function(self2, tw) { + self2.expression = self2.expression.transform(tw); +}); +def_transform(AST_Unary, function(self2, tw) { + self2.expression = self2.expression.transform(tw); +}); +def_transform(AST_Binary, function(self2, tw) { + self2.left = self2.left.transform(tw); + self2.right = self2.right.transform(tw); +}); +def_transform(AST_PrivateIn, function(self2, tw) { + self2.key = self2.key.transform(tw); + self2.value = self2.value.transform(tw); +}); +def_transform(AST_Conditional, function(self2, tw) { + self2.condition = self2.condition.transform(tw); + self2.consequent = self2.consequent.transform(tw); + self2.alternative = self2.alternative.transform(tw); +}); +def_transform(AST_Array, function(self2, tw) { + self2.elements = MAP(self2.elements, tw); +}); +def_transform(AST_Object, function(self2, tw) { + self2.properties = MAP(self2.properties, tw); +}); +def_transform(AST_ObjectProperty, function(self2, tw) { + if (self2.key instanceof AST_Node) { + self2.key = self2.key.transform(tw); + } + if (self2.value) + self2.value = self2.value.transform(tw); +}); +def_transform(AST_Class, function(self2, tw) { + if (self2.name) + self2.name = self2.name.transform(tw); + if (self2.extends) + self2.extends = self2.extends.transform(tw); + self2.properties = MAP(self2.properties, tw); +}); +def_transform(AST_ClassStaticBlock, function(self2, tw) { + self2.body = MAP(self2.body, tw); +}); +def_transform(AST_Expansion, function(self2, tw) { + self2.expression = self2.expression.transform(tw); +}); +def_transform(AST_NameMapping, function(self2, tw) { + self2.foreign_name = self2.foreign_name.transform(tw); + self2.name = self2.name.transform(tw); +}); +def_transform(AST_Import, function(self2, tw) { + if (self2.imported_name) + self2.imported_name = self2.imported_name.transform(tw); + if (self2.imported_names) + MAP(self2.imported_names, tw); + self2.module_name = self2.module_name.transform(tw); +}); +def_transform(AST_Export, function(self2, tw) { + if (self2.exported_definition) + self2.exported_definition = self2.exported_definition.transform(tw); + if (self2.exported_value) + self2.exported_value = self2.exported_value.transform(tw); + if (self2.exported_names) + MAP(self2.exported_names, tw); + if (self2.module_name) + self2.module_name = self2.module_name.transform(tw); +}); +def_transform(AST_TemplateString, function(self2, tw) { + self2.segments = MAP(self2.segments, tw); +}); +def_transform(AST_PrefixedTemplateString, function(self2, tw) { + self2.prefix = self2.prefix.transform(tw); + self2.template_string = self2.template_string.transform(tw); +}); + +// node_modules/terser/lib/mozilla-ast.js +(function() { + var normalize_directives = function(body) { + for (var i = 0;i < body.length; i++) { + if (body[i] instanceof AST_Statement && body[i].body instanceof AST_String) { + body[i] = new AST_Directive({ + start: body[i].start, + end: body[i].end, + quote: '"', + value: body[i].body.value + }); + } else { + return body; + } + } + return body; + }; + function import_attributes_from_moz(attributes) { + if (attributes && attributes.length > 0) { + return new AST_Object({ + start: my_start_token(attributes), + end: my_end_token(attributes), + properties: attributes.map((attr) => new AST_ObjectKeyVal({ + start: my_start_token(attr), + end: my_end_token(attr), + key: attr.key.name || attr.key.value, + value: from_moz(attr.value) + })) + }); + } + return null; + } + var MOZ_TO_ME = { + Program: function(M) { + return new AST_Toplevel({ + start: my_start_token(M), + end: my_end_token(M), + body: normalize_directives(M.body.map(from_moz)) + }); + }, + ArrayPattern: function(M) { + return new AST_Destructuring({ + start: my_start_token(M), + end: my_end_token(M), + names: M.elements.map(function(elm) { + if (elm === null) { + return new AST_Hole; + } + return from_moz(elm); + }), + is_array: true + }); + }, + ObjectPattern: function(M) { + return new AST_Destructuring({ + start: my_start_token(M), + end: my_end_token(M), + names: M.properties.map(from_moz), + is_array: false + }); + }, + AssignmentPattern: function(M) { + return new AST_DefaultAssign({ + start: my_start_token(M), + end: my_end_token(M), + left: from_moz(M.left), + operator: "=", + right: from_moz(M.right) + }); + }, + SpreadElement: function(M) { + return new AST_Expansion({ + start: my_start_token(M), + end: my_end_token(M), + expression: from_moz(M.argument) + }); + }, + RestElement: function(M) { + return new AST_Expansion({ + start: my_start_token(M), + end: my_end_token(M), + expression: from_moz(M.argument) + }); + }, + TemplateElement: function(M) { + return new AST_TemplateSegment({ + start: my_start_token(M), + end: my_end_token(M), + value: M.value.cooked, + raw: M.value.raw + }); + }, + TemplateLiteral: function(M) { + var segments = []; + for (var i = 0;i < M.quasis.length; i++) { + segments.push(from_moz(M.quasis[i])); + if (M.expressions[i]) { + segments.push(from_moz(M.expressions[i])); + } + } + return new AST_TemplateString({ + start: my_start_token(M), + end: my_end_token(M), + segments + }); + }, + TaggedTemplateExpression: function(M) { + return new AST_PrefixedTemplateString({ + start: my_start_token(M), + end: my_end_token(M), + template_string: from_moz(M.quasi), + prefix: from_moz(M.tag) + }); + }, + FunctionDeclaration: function(M) { + return new AST_Defun({ + start: my_start_token(M), + end: my_end_token(M), + name: M.id && from_moz_symbol(AST_SymbolDefun, M.id), + argnames: M.params.map((M2) => from_moz_pattern(M2, AST_SymbolFunarg)), + is_generator: M.generator, + async: M.async, + body: normalize_directives(from_moz(M.body).body) + }); + }, + FunctionExpression: function(M) { + return from_moz_lambda(M, false); + }, + ArrowFunctionExpression: function(M) { + const body = M.body.type === "BlockStatement" ? from_moz(M.body).body : [make_node(AST_Return, {}, { value: from_moz(M.body) })]; + return new AST_Arrow({ + start: my_start_token(M), + end: my_end_token(M), + argnames: M.params.map((p) => from_moz_pattern(p, AST_SymbolFunarg)), + body, + async: M.async + }); + }, + ExpressionStatement: function(M) { + return new AST_SimpleStatement({ + start: my_start_token(M), + end: my_end_token(M), + body: from_moz(M.expression) + }); + }, + TryStatement: function(M) { + var handlers = M.handlers || [M.handler]; + if (handlers.length > 1 || M.guardedHandlers && M.guardedHandlers.length) { + throw new Error("Multiple catch clauses are not supported."); + } + return new AST_Try({ + start: my_start_token(M), + end: my_end_token(M), + body: new AST_TryBlock(from_moz(M.block)), + bcatch: from_moz(handlers[0]), + bfinally: M.finalizer ? new AST_Finally(from_moz(M.finalizer)) : null + }); + }, + Property: function(M) { + if (M.kind == "init" && !M.method) { + var args = { + start: my_start_token(M.key || M.value), + end: my_end_token(M.value), + key: M.computed ? from_moz(M.key) : M.key.name || String(M.key.value), + quote: from_moz_quote(M.key, M.computed), + static: false, + value: from_moz(M.value) + }; + return new AST_ObjectKeyVal(args); + } else { + var value = from_moz_lambda(M.value, true); + var args = { + start: my_start_token(M.key || M.value), + end: my_end_token(M.value), + key: M.computed ? from_moz(M.key) : from_moz_symbol(AST_SymbolMethod, M.key), + quote: from_moz_quote(M.key, M.computed), + static: false, + value + }; + if (M.kind == "get") + return new AST_ObjectGetter(args); + if (M.kind == "set") + return new AST_ObjectSetter(args); + if (M.method) + return new AST_ConciseMethod(args); + } + }, + MethodDefinition: function(M) { + const is_private = M.key.type === "PrivateIdentifier"; + const key = M.computed ? from_moz(M.key) : new AST_SymbolMethod({ name: M.key.name || String(M.key.value) }); + var args = { + start: my_start_token(M), + end: my_end_token(M), + key, + quote: from_moz_quote(M.key, M.computed), + value: from_moz_lambda(M.value, true), + static: M.static + }; + if (M.kind == "get") { + return new (is_private ? AST_PrivateGetter : AST_ObjectGetter)(args); + } + if (M.kind == "set") { + return new (is_private ? AST_PrivateSetter : AST_ObjectSetter)(args); + } + return new (is_private ? AST_PrivateMethod : AST_ConciseMethod)(args); + }, + FieldDefinition: function(M) { + let key; + if (M.computed) { + key = from_moz(M.key); + } else { + if (M.key.type !== "Identifier") + throw new Error("Non-Identifier key in FieldDefinition"); + key = from_moz(M.key); + } + return new AST_ClassProperty({ + start: my_start_token(M), + end: my_end_token(M), + quote: from_moz_quote(M.key, M.computed), + key, + value: from_moz(M.value), + static: M.static + }); + }, + PropertyDefinition: function(M) { + let key; + if (M.computed) { + key = from_moz(M.key); + } else if (M.key.type === "PrivateIdentifier") { + return new AST_ClassPrivateProperty({ + start: my_start_token(M), + end: my_end_token(M), + key: from_moz(M.key), + value: from_moz(M.value), + static: M.static + }); + } else { + key = from_moz_symbol(AST_SymbolClassProperty, M.key); + } + return new AST_ClassProperty({ + start: my_start_token(M), + end: my_end_token(M), + quote: from_moz_quote(M.key, M.computed), + key, + value: from_moz(M.value), + static: M.static + }); + }, + PrivateIdentifier: function(M) { + return new AST_SymbolPrivateProperty({ + start: my_start_token(M), + end: my_end_token(M), + name: M.name + }); + }, + StaticBlock: function(M) { + return new AST_ClassStaticBlock({ + start: my_start_token(M), + end: my_end_token(M), + body: M.body.map(from_moz) + }); + }, + ArrayExpression: function(M) { + return new AST_Array({ + start: my_start_token(M), + end: my_end_token(M), + elements: M.elements.map(function(elem) { + return elem === null ? new AST_Hole : from_moz(elem); + }) + }); + }, + ObjectExpression: function(M) { + return new AST_Object({ + start: my_start_token(M), + end: my_end_token(M), + properties: M.properties.map(function(prop) { + if (prop.type === "SpreadElement") { + return from_moz(prop); + } + prop.type = "Property"; + return from_moz(prop); + }) + }); + }, + SequenceExpression: function(M) { + return new AST_Sequence({ + start: my_start_token(M), + end: my_end_token(M), + expressions: M.expressions.map(from_moz) + }); + }, + MemberExpression: function(M) { + if (M.property.type === "PrivateIdentifier") { + return new AST_DotHash({ + start: my_start_token(M), + end: my_end_token(M), + property: M.property.name, + expression: from_moz(M.object), + optional: M.optional || false + }); + } + return new (M.computed ? AST_Sub : AST_Dot)({ + start: my_start_token(M), + end: my_end_token(M), + property: M.computed ? from_moz(M.property) : M.property.name, + expression: from_moz(M.object), + optional: M.optional || false + }); + }, + ChainExpression: function(M) { + return new AST_Chain({ + start: my_start_token(M), + end: my_end_token(M), + expression: from_moz(M.expression) + }); + }, + SwitchCase: function(M) { + return new (M.test ? AST_Case : AST_Default)({ + start: my_start_token(M), + end: my_end_token(M), + expression: from_moz(M.test), + body: M.consequent.map(from_moz) + }); + }, + VariableDeclaration: function(M) { + let decl_type; + let sym_type; + if (M.kind === "const") { + decl_type = AST_Const; + sym_type = AST_SymbolConst; + } else if (M.kind === "let") { + decl_type = AST_Let; + sym_type = AST_SymbolLet; + } else { + decl_type = AST_Var; + sym_type = AST_SymbolVar; + } + const definitions = M.declarations.map((M2) => { + return new AST_VarDef({ + start: my_start_token(M2), + end: my_end_token(M2), + name: from_moz_pattern(M2.id, sym_type), + value: from_moz(M2.init) + }); + }); + return new decl_type({ + start: my_start_token(M), + end: my_end_token(M), + definitions + }); + }, + ImportDeclaration: function(M) { + var imported_name = null; + var imported_names = null; + M.specifiers.forEach(function(specifier) { + if (specifier.type === "ImportSpecifier" || specifier.type === "ImportNamespaceSpecifier") { + if (!imported_names) { + imported_names = []; + } + imported_names.push(from_moz(specifier)); + } else if (specifier.type === "ImportDefaultSpecifier") { + imported_name = from_moz(specifier); + } + }); + return new AST_Import({ + start: my_start_token(M), + end: my_end_token(M), + imported_name, + imported_names, + module_name: from_moz(M.source), + attributes: import_attributes_from_moz(M.attributes || M.assertions) + }); + }, + ImportSpecifier: function(M) { + return new AST_NameMapping({ + start: my_start_token(M), + end: my_end_token(M), + foreign_name: from_moz_symbol(AST_SymbolImportForeign, M.imported, M.imported.type === "Literal"), + name: from_moz_symbol(AST_SymbolImport, M.local) + }); + }, + ImportDefaultSpecifier: function(M) { + return from_moz_symbol(AST_SymbolImport, M.local); + }, + ImportNamespaceSpecifier: function(M) { + return new AST_NameMapping({ + start: my_start_token(M), + end: my_end_token(M), + foreign_name: new AST_SymbolImportForeign({ name: "*" }), + name: from_moz_symbol(AST_SymbolImport, M.local) + }); + }, + ImportExpression: function(M) { + const args = [from_moz(M.source)]; + if (M.options) { + args.push(from_moz(M.options)); + } + return new AST_Call({ + start: my_start_token(M), + end: my_end_token(M), + expression: from_moz({ + type: "Identifier", + name: "import" + }), + optional: false, + args + }); + }, + ExportAllDeclaration: function(M) { + var foreign_name = M.exported == null ? new AST_SymbolExportForeign({ name: "*" }) : from_moz_symbol(AST_SymbolExportForeign, M.exported, M.exported.type === "Literal"); + return new AST_Export({ + start: my_start_token(M), + end: my_end_token(M), + exported_names: [ + new AST_NameMapping({ + start: my_start_token(M), + end: my_end_token(M), + name: new AST_SymbolExport({ name: "*" }), + foreign_name + }) + ], + module_name: from_moz(M.source), + attributes: import_attributes_from_moz(M.attributes || M.assertions) + }); + }, + ExportNamedDeclaration: function(M) { + if (M.declaration) { + return new AST_Export({ + start: my_start_token(M), + end: my_end_token(M), + exported_definition: from_moz(M.declaration), + exported_names: null, + module_name: null, + attributes: null + }); + } else { + return new AST_Export({ + start: my_start_token(M), + end: my_end_token(M), + exported_definition: null, + exported_names: M.specifiers && M.specifiers.length ? M.specifiers.map(from_moz) : [], + module_name: from_moz(M.source), + attributes: import_attributes_from_moz(M.attributes || M.assertions) + }); + } + }, + ExportDefaultDeclaration: function(M) { + return new AST_Export({ + start: my_start_token(M), + end: my_end_token(M), + exported_value: from_moz(M.declaration), + is_default: true + }); + }, + ExportSpecifier: function(M) { + return new AST_NameMapping({ + start: my_start_token(M), + end: my_end_token(M), + foreign_name: from_moz_symbol(AST_SymbolExportForeign, M.exported, M.exported.type === "Literal"), + name: from_moz_symbol(AST_SymbolExport, M.local, M.local.type === "Literal") + }); + }, + Literal: function(M) { + var val = M.value, args = { + start: my_start_token(M), + end: my_end_token(M) + }; + var rx = M.regex; + if (rx && rx.pattern) { + args.value = { + source: rx.pattern, + flags: rx.flags + }; + return new AST_RegExp(args); + } else if (rx) { + const rx_source = M.raw || val; + const match = rx_source.match(/^\/(.*)\/(\w*)$/); + if (!match) + throw new Error("Invalid regex source " + rx_source); + const [_, source, flags] = match; + args.value = { source, flags }; + return new AST_RegExp(args); + } + const bi = typeof M.value === "bigint" ? M.value.toString() : M.bigint; + if (typeof bi === "string") { + args.value = bi; + args.raw = M.raw; + return new AST_BigInt(args); + } + if (val === null) + return new AST_Null(args); + switch (typeof val) { + case "string": + args.quote = '"'; + args.value = val; + return new AST_String(args); + case "number": + args.value = val; + args.raw = M.raw || val.toString(); + return new AST_Number(args); + case "boolean": + return new (val ? AST_True : AST_False)(args); + } + }, + MetaProperty: function(M) { + if (M.meta.name === "new" && M.property.name === "target") { + return new AST_NewTarget({ + start: my_start_token(M), + end: my_end_token(M) + }); + } else if (M.meta.name === "import" && M.property.name === "meta") { + return new AST_ImportMeta({ + start: my_start_token(M), + end: my_end_token(M) + }); + } + }, + Identifier: function(M) { + return new AST_SymbolRef({ + start: my_start_token(M), + end: my_end_token(M), + name: M.name + }); + }, + EmptyStatement: function(M) { + return new AST_EmptyStatement({ + start: my_start_token(M), + end: my_end_token(M) + }); + }, + BlockStatement: function(M) { + return new AST_BlockStatement({ + start: my_start_token(M), + end: my_end_token(M), + body: M.body.map(from_moz) + }); + }, + IfStatement: function(M) { + return new AST_If({ + start: my_start_token(M), + end: my_end_token(M), + condition: from_moz(M.test), + body: from_moz(M.consequent), + alternative: from_moz(M.alternate) + }); + }, + LabeledStatement: function(M) { + try { + const label = from_moz_symbol(AST_Label, M.label); + FROM_MOZ_LABELS.push(label); + const stat = new AST_LabeledStatement({ + start: my_start_token(M), + end: my_end_token(M), + label, + body: from_moz(M.body) + }); + return stat; + } finally { + FROM_MOZ_LABELS.pop(); + } + }, + BreakStatement: function(M) { + return new AST_Break({ + start: my_start_token(M), + end: my_end_token(M), + label: from_moz_label_ref(M.label) + }); + }, + ContinueStatement: function(M) { + return new AST_Continue({ + start: my_start_token(M), + end: my_end_token(M), + label: from_moz_label_ref(M.label) + }); + }, + WithStatement: function(M) { + return new AST_With({ + start: my_start_token(M), + end: my_end_token(M), + expression: from_moz(M.object), + body: from_moz(M.body) + }); + }, + SwitchStatement: function(M) { + return new AST_Switch({ + start: my_start_token(M), + end: my_end_token(M), + expression: from_moz(M.discriminant), + body: M.cases.map(from_moz) + }); + }, + ReturnStatement: function(M) { + return new AST_Return({ + start: my_start_token(M), + end: my_end_token(M), + value: from_moz(M.argument) + }); + }, + ThrowStatement: function(M) { + return new AST_Throw({ + start: my_start_token(M), + end: my_end_token(M), + value: from_moz(M.argument) + }); + }, + WhileStatement: function(M) { + return new AST_While({ + start: my_start_token(M), + end: my_end_token(M), + condition: from_moz(M.test), + body: from_moz(M.body) + }); + }, + DoWhileStatement: function(M) { + return new AST_Do({ + start: my_start_token(M), + end: my_end_token(M), + condition: from_moz(M.test), + body: from_moz(M.body) + }); + }, + ForStatement: function(M) { + return new AST_For({ + start: my_start_token(M), + end: my_end_token(M), + init: from_moz(M.init), + condition: from_moz(M.test), + step: from_moz(M.update), + body: from_moz(M.body) + }); + }, + ForInStatement: function(M) { + return new AST_ForIn({ + start: my_start_token(M), + end: my_end_token(M), + init: from_moz(M.left), + object: from_moz(M.right), + body: from_moz(M.body) + }); + }, + ForOfStatement: function(M) { + return new AST_ForOf({ + start: my_start_token(M), + end: my_end_token(M), + init: from_moz(M.left), + object: from_moz(M.right), + body: from_moz(M.body), + await: M.await + }); + }, + AwaitExpression: function(M) { + return new AST_Await({ + start: my_start_token(M), + end: my_end_token(M), + expression: from_moz(M.argument) + }); + }, + YieldExpression: function(M) { + return new AST_Yield({ + start: my_start_token(M), + end: my_end_token(M), + expression: from_moz(M.argument), + is_star: M.delegate + }); + }, + DebuggerStatement: function(M) { + return new AST_Debugger({ + start: my_start_token(M), + end: my_end_token(M) + }); + }, + CatchClause: function(M) { + return new AST_Catch({ + start: my_start_token(M), + end: my_end_token(M), + argname: M.param ? from_moz_pattern(M.param, AST_SymbolCatch) : null, + body: from_moz(M.body).body + }); + }, + ThisExpression: function(M) { + return new AST_This({ + start: my_start_token(M), + name: "this", + end: my_end_token(M) + }); + }, + Super: function(M) { + return new AST_Super({ + start: my_start_token(M), + end: my_end_token(M), + name: "super" + }); + }, + BinaryExpression: function(M) { + if (M.left.type === "PrivateIdentifier") { + return new AST_PrivateIn({ + start: my_start_token(M), + end: my_end_token(M), + key: new AST_SymbolPrivateProperty({ + start: my_start_token(M.left), + end: my_end_token(M.left), + name: M.left.name + }), + value: from_moz(M.right) + }); + } + return new AST_Binary({ + start: my_start_token(M), + end: my_end_token(M), + operator: M.operator, + left: from_moz(M.left), + right: from_moz(M.right) + }); + }, + LogicalExpression: function(M) { + return new AST_Binary({ + start: my_start_token(M), + end: my_end_token(M), + operator: M.operator, + left: from_moz(M.left), + right: from_moz(M.right) + }); + }, + AssignmentExpression: function(M) { + return new AST_Assign({ + start: my_start_token(M), + end: my_end_token(M), + operator: M.operator, + logical: M.operator === "??=" || M.operator === "&&=" || M.operator === "||=", + left: from_moz(M.left), + right: from_moz(M.right) + }); + }, + ConditionalExpression: function(M) { + return new AST_Conditional({ + start: my_start_token(M), + end: my_end_token(M), + condition: from_moz(M.test), + consequent: from_moz(M.consequent), + alternative: from_moz(M.alternate) + }); + }, + NewExpression: function(M) { + return new AST_New({ + start: my_start_token(M), + end: my_end_token(M), + expression: from_moz(M.callee), + args: M.arguments.map(from_moz) + }); + }, + CallExpression: function(M) { + return new AST_Call({ + start: my_start_token(M), + end: my_end_token(M), + expression: from_moz(M.callee), + optional: M.optional, + args: M.arguments.map(from_moz) + }); + } + }; + MOZ_TO_ME.UpdateExpression = MOZ_TO_ME.UnaryExpression = function To_Moz_Unary(M) { + var prefix = "prefix" in M ? M.prefix : M.type == "UnaryExpression" ? true : false; + return new (prefix ? AST_UnaryPrefix : AST_UnaryPostfix)({ + start: my_start_token(M), + end: my_end_token(M), + operator: M.operator, + expression: from_moz(M.argument) + }); + }; + MOZ_TO_ME.ClassDeclaration = MOZ_TO_ME.ClassExpression = function From_Moz_Class(M) { + return new (M.type === "ClassDeclaration" ? AST_DefClass : AST_ClassExpression)({ + start: my_start_token(M), + end: my_end_token(M), + name: M.id && from_moz_symbol(M.type === "ClassDeclaration" ? AST_SymbolDefClass : AST_SymbolClass, M.id), + extends: from_moz(M.superClass), + properties: M.body.body.map(from_moz) + }); + }; + def_to_moz(AST_EmptyStatement, function To_Moz_EmptyStatement() { + return { + type: "EmptyStatement" + }; + }); + def_to_moz(AST_BlockStatement, function To_Moz_BlockStatement(M) { + return { + type: "BlockStatement", + body: M.body.map(to_moz) + }; + }); + def_to_moz(AST_If, function To_Moz_IfStatement(M) { + return { + type: "IfStatement", + test: to_moz(M.condition), + consequent: to_moz(M.body), + alternate: to_moz(M.alternative) + }; + }); + def_to_moz(AST_LabeledStatement, function To_Moz_LabeledStatement(M) { + return { + type: "LabeledStatement", + label: to_moz(M.label), + body: to_moz(M.body) + }; + }); + def_to_moz(AST_Break, function To_Moz_BreakStatement(M) { + return { + type: "BreakStatement", + label: to_moz(M.label) + }; + }); + def_to_moz(AST_Continue, function To_Moz_ContinueStatement(M) { + return { + type: "ContinueStatement", + label: to_moz(M.label) + }; + }); + def_to_moz(AST_With, function To_Moz_WithStatement(M) { + return { + type: "WithStatement", + object: to_moz(M.expression), + body: to_moz(M.body) + }; + }); + def_to_moz(AST_Switch, function To_Moz_SwitchStatement(M) { + return { + type: "SwitchStatement", + discriminant: to_moz(M.expression), + cases: M.body.map(to_moz) + }; + }); + def_to_moz(AST_Return, function To_Moz_ReturnStatement(M) { + return { + type: "ReturnStatement", + argument: to_moz(M.value) + }; + }); + def_to_moz(AST_Throw, function To_Moz_ThrowStatement(M) { + return { + type: "ThrowStatement", + argument: to_moz(M.value) + }; + }); + def_to_moz(AST_While, function To_Moz_WhileStatement(M) { + return { + type: "WhileStatement", + test: to_moz(M.condition), + body: to_moz(M.body) + }; + }); + def_to_moz(AST_Do, function To_Moz_DoWhileStatement(M) { + return { + type: "DoWhileStatement", + test: to_moz(M.condition), + body: to_moz(M.body) + }; + }); + def_to_moz(AST_For, function To_Moz_ForStatement(M) { + return { + type: "ForStatement", + init: to_moz(M.init), + test: to_moz(M.condition), + update: to_moz(M.step), + body: to_moz(M.body) + }; + }); + def_to_moz(AST_ForIn, function To_Moz_ForInStatement(M) { + return { + type: "ForInStatement", + left: to_moz(M.init), + right: to_moz(M.object), + body: to_moz(M.body) + }; + }); + def_to_moz(AST_ForOf, function To_Moz_ForOfStatement(M) { + return { + type: "ForOfStatement", + left: to_moz(M.init), + right: to_moz(M.object), + body: to_moz(M.body), + await: M.await + }; + }); + def_to_moz(AST_Await, function To_Moz_AwaitExpression(M) { + return { + type: "AwaitExpression", + argument: to_moz(M.expression) + }; + }); + def_to_moz(AST_Yield, function To_Moz_YieldExpression(M) { + return { + type: "YieldExpression", + argument: to_moz(M.expression), + delegate: M.is_star + }; + }); + def_to_moz(AST_Debugger, function To_Moz_DebuggerStatement() { + return { + type: "DebuggerStatement" + }; + }); + def_to_moz(AST_VarDef, function To_Moz_VariableDeclarator(M) { + return { + type: "VariableDeclarator", + id: to_moz(M.name), + init: to_moz(M.value) + }; + }); + def_to_moz(AST_This, function To_Moz_ThisExpression() { + return { + type: "ThisExpression" + }; + }); + def_to_moz(AST_Super, function To_Moz_Super() { + return { + type: "Super" + }; + }); + def_to_moz(AST_Conditional, function To_Moz_ConditionalExpression(M) { + return { + type: "ConditionalExpression", + test: to_moz(M.condition), + consequent: to_moz(M.consequent), + alternate: to_moz(M.alternative) + }; + }); + def_to_moz(AST_New, function To_Moz_NewExpression(M) { + return { + type: "NewExpression", + callee: to_moz(M.expression), + arguments: M.args.map(to_moz) + }; + }); + def_to_moz(AST_Call, function To_Moz_CallExpression(M) { + if (M.expression instanceof AST_SymbolRef && M.expression.name === "import") { + const [source, options] = M.args.map(to_moz); + return { + type: "ImportExpression", + source, + options: options || null + }; + } + return { + type: "CallExpression", + callee: to_moz(M.expression), + optional: M.optional, + arguments: M.args.map(to_moz) + }; + }); + def_to_moz(AST_Toplevel, function To_Moz_Program(M) { + return to_moz_scope("Program", M); + }); + def_to_moz(AST_Expansion, function To_Moz_Spread(M) { + return { + type: to_moz_in_destructuring() ? "RestElement" : "SpreadElement", + argument: to_moz(M.expression) + }; + }); + def_to_moz(AST_PrefixedTemplateString, function To_Moz_TaggedTemplateExpression(M) { + return { + type: "TaggedTemplateExpression", + tag: to_moz(M.prefix), + quasi: to_moz(M.template_string) + }; + }); + def_to_moz(AST_TemplateString, function To_Moz_TemplateLiteral(M) { + var quasis = []; + var expressions = []; + for (var i = 0;i < M.segments.length; i++) { + if (i % 2 !== 0) { + expressions.push(to_moz(M.segments[i])); + } else { + quasis.push({ + type: "TemplateElement", + value: { + raw: M.segments[i].raw, + cooked: M.segments[i].value + }, + tail: i === M.segments.length - 1 + }); + } + } + return { + type: "TemplateLiteral", + quasis, + expressions + }; + }); + def_to_moz(AST_Defun, function To_Moz_FunctionDeclaration(M) { + return { + type: "FunctionDeclaration", + id: to_moz(M.name), + params: M.argnames.map(to_moz_pattern), + generator: M.is_generator, + async: M.async, + body: to_moz_scope("BlockStatement", M) + }; + }); + def_to_moz(AST_Function, function To_Moz_FunctionExpression(M) { + return { + type: "FunctionExpression", + id: to_moz(M.name), + params: M.argnames.map(to_moz_pattern), + generator: M.is_generator || false, + async: M.async || false, + body: to_moz_scope("BlockStatement", M) + }; + }); + def_to_moz(AST_Arrow, function To_Moz_ArrowFunctionExpression(M) { + var body = M.body.length === 1 && M.body[0] instanceof AST_Return && M.body[0].value ? to_moz(M.body[0].value) : { + type: "BlockStatement", + body: M.body.map(to_moz) + }; + return { + type: "ArrowFunctionExpression", + params: M.argnames.map(to_moz_pattern), + async: M.async, + body + }; + }); + def_to_moz(AST_Destructuring, function To_Moz_ObjectPattern(M) { + if (M.is_array) { + return { + type: "ArrayPattern", + elements: M.names.map((M2) => M2 instanceof AST_Hole ? null : to_moz_pattern(M2)) + }; + } + return { + type: "ObjectPattern", + properties: M.names.map((M2) => { + if (M2 instanceof AST_ObjectKeyVal) { + var computed = M2.computed_key(); + const [shorthand, key] = to_moz_property_key(M2.key, computed, M2.quote, M2.value); + return { + type: "Property", + computed, + kind: "init", + key, + method: false, + shorthand, + value: to_moz_pattern(M2.value) + }; + } else { + return to_moz_pattern(M2); + } + }) + }; + }); + def_to_moz(AST_DefaultAssign, function To_Moz_AssignmentExpression(M) { + return { + type: "AssignmentPattern", + left: to_moz_pattern(M.left), + right: to_moz(M.right) + }; + }); + def_to_moz(AST_Directive, function To_Moz_Directive(M) { + return { + type: "ExpressionStatement", + expression: { + type: "Literal", + value: M.value, + raw: M.print_to_string() + }, + directive: M.value + }; + }); + def_to_moz(AST_SimpleStatement, function To_Moz_ExpressionStatement(M) { + return { + type: "ExpressionStatement", + expression: to_moz(M.body) + }; + }); + def_to_moz(AST_SwitchBranch, function To_Moz_SwitchCase(M) { + return { + type: "SwitchCase", + test: to_moz(M.expression), + consequent: M.body.map(to_moz) + }; + }); + def_to_moz(AST_Try, function To_Moz_TryStatement(M) { + return { + type: "TryStatement", + block: to_moz_block(M.body), + handler: to_moz(M.bcatch), + guardedHandlers: [], + finalizer: to_moz(M.bfinally) + }; + }); + def_to_moz(AST_Catch, function To_Moz_CatchClause(M) { + return { + type: "CatchClause", + param: M.argname != null ? to_moz_pattern(M.argname) : null, + body: to_moz_block(M) + }; + }); + def_to_moz(AST_Definitions, function To_Moz_VariableDeclaration(M) { + return { + type: "VariableDeclaration", + kind: M instanceof AST_Const ? "const" : M instanceof AST_Let ? "let" : "var", + declarations: M.definitions.map(to_moz) + }; + }); + function import_attributes_to_moz(attribute) { + const import_attributes = []; + if (attribute) { + for (const { key, value } of attribute.properties) { + const key_moz = is_basic_identifier_string(key) ? { type: "Identifier", name: key } : { type: "Literal", value: key, raw: JSON.stringify(key) }; + import_attributes.push({ + type: "ImportAttribute", + key: key_moz, + value: to_moz(value) + }); + } + } + return import_attributes; + } + def_to_moz(AST_Export, function To_Moz_ExportDeclaration(M) { + if (M.exported_names) { + var first_exported = M.exported_names[0]; + if (first_exported && first_exported.name.name === "*" && !first_exported.name.quote) { + var foreign_name = first_exported.foreign_name; + var exported = foreign_name.name === "*" && !foreign_name.quote ? null : to_moz(foreign_name); + return { + type: "ExportAllDeclaration", + source: to_moz(M.module_name), + exported, + attributes: import_attributes_to_moz(M.attributes) + }; + } + return { + type: "ExportNamedDeclaration", + specifiers: M.exported_names.map(function(name_mapping) { + return { + type: "ExportSpecifier", + exported: to_moz(name_mapping.foreign_name), + local: to_moz(name_mapping.name) + }; + }), + declaration: to_moz(M.exported_definition), + source: to_moz(M.module_name), + attributes: import_attributes_to_moz(M.attributes) + }; + } + if (M.is_default) { + return { + type: "ExportDefaultDeclaration", + declaration: to_moz(M.exported_value || M.exported_definition) + }; + } else { + return { + type: "ExportNamedDeclaration", + declaration: to_moz(M.exported_value || M.exported_definition), + specifiers: [], + source: null + }; + } + }); + def_to_moz(AST_Import, function To_Moz_ImportDeclaration(M) { + var specifiers = []; + if (M.imported_name) { + specifiers.push({ + type: "ImportDefaultSpecifier", + local: to_moz(M.imported_name) + }); + } + if (M.imported_names) { + var first_imported_foreign_name = M.imported_names[0].foreign_name; + if (first_imported_foreign_name.name === "*" && !first_imported_foreign_name.quote) { + specifiers.push({ + type: "ImportNamespaceSpecifier", + local: to_moz(M.imported_names[0].name) + }); + } else { + M.imported_names.forEach(function(name_mapping) { + specifiers.push({ + type: "ImportSpecifier", + local: to_moz(name_mapping.name), + imported: to_moz(name_mapping.foreign_name) + }); + }); + } + } + return { + type: "ImportDeclaration", + specifiers, + source: to_moz(M.module_name), + attributes: import_attributes_to_moz(M.attributes) + }; + }); + def_to_moz(AST_ImportMeta, function To_Moz_MetaProperty() { + return { + type: "MetaProperty", + meta: { + type: "Identifier", + name: "import" + }, + property: { + type: "Identifier", + name: "meta" + } + }; + }); + def_to_moz(AST_Sequence, function To_Moz_SequenceExpression(M) { + return { + type: "SequenceExpression", + expressions: M.expressions.map(to_moz) + }; + }); + def_to_moz(AST_DotHash, function To_Moz_PrivateMemberExpression(M) { + return { + type: "MemberExpression", + object: to_moz(M.expression), + computed: false, + property: { + type: "PrivateIdentifier", + name: M.property + }, + optional: M.optional + }; + }); + def_to_moz(AST_PropAccess, function To_Moz_MemberExpression(M) { + var isComputed = M instanceof AST_Sub; + return { + type: "MemberExpression", + object: to_moz(M.expression), + computed: isComputed, + property: isComputed ? to_moz(M.property) : { type: "Identifier", name: M.property }, + optional: M.optional + }; + }); + def_to_moz(AST_Chain, function To_Moz_ChainExpression(M) { + return { + type: "ChainExpression", + expression: to_moz(M.expression) + }; + }); + def_to_moz(AST_Unary, function To_Moz_Unary(M) { + return { + type: M.operator == "++" || M.operator == "--" ? "UpdateExpression" : "UnaryExpression", + operator: M.operator, + prefix: M instanceof AST_UnaryPrefix, + argument: to_moz(M.expression) + }; + }); + def_to_moz(AST_Binary, function To_Moz_BinaryExpression(M) { + if (M.operator == "=" && to_moz_in_destructuring()) { + return { + type: "AssignmentPattern", + left: to_moz(M.left), + right: to_moz(M.right) + }; + } + const type = M.operator == "&&" || M.operator == "||" || M.operator === "??" ? "LogicalExpression" : "BinaryExpression"; + return { + type, + left: to_moz(M.left), + operator: M.operator, + right: to_moz(M.right) + }; + }); + def_to_moz(AST_Assign, function To_Moz_AssignmentExpression(M) { + return { + type: "AssignmentExpression", + operator: M.operator, + left: to_moz(M.left), + right: to_moz(M.right) + }; + }); + def_to_moz(AST_PrivateIn, function To_Moz_BinaryExpression_PrivateIn(M) { + return { + type: "BinaryExpression", + left: { type: "PrivateIdentifier", name: M.key.name }, + operator: "in", + right: to_moz(M.value) + }; + }); + def_to_moz(AST_Array, function To_Moz_ArrayExpression(M) { + return { + type: "ArrayExpression", + elements: M.elements.map(to_moz) + }; + }); + def_to_moz(AST_Object, function To_Moz_ObjectExpression(M) { + return { + type: "ObjectExpression", + properties: M.properties.map(to_moz) + }; + }); + def_to_moz(AST_ObjectProperty, function To_Moz_Property(M, parent) { + var computed = M.computed_key(); + const [shorthand, key] = to_moz_property_key(M.key, computed, M.quote, M.value); + var kind; + if (M instanceof AST_ObjectGetter) { + kind = "get"; + } else if (M instanceof AST_ObjectSetter) { + kind = "set"; + } + if (M instanceof AST_PrivateGetter || M instanceof AST_PrivateSetter) { + const kind2 = M instanceof AST_PrivateGetter ? "get" : "set"; + return { + type: "MethodDefinition", + computed: false, + kind: kind2, + static: M.static, + key: { + type: "PrivateIdentifier", + name: M.key.name + }, + value: to_moz(M.value) + }; + } + if (M instanceof AST_ClassPrivateProperty) { + return { + type: "PropertyDefinition", + key: { + type: "PrivateIdentifier", + name: M.key.name + }, + value: to_moz(M.value), + computed: false, + static: M.static + }; + } + if (M instanceof AST_ClassProperty) { + return { + type: "PropertyDefinition", + key, + value: to_moz(M.value), + computed, + static: M.static + }; + } + if (parent instanceof AST_Class) { + return { + type: "MethodDefinition", + computed, + kind, + static: M.static, + key: to_moz(M.key), + value: to_moz(M.value) + }; + } + return { + type: "Property", + computed, + method: false, + shorthand, + kind, + key, + value: to_moz(M.value) + }; + }); + def_to_moz(AST_ObjectKeyVal, function To_Moz_Property(M) { + var computed = M.computed_key(); + const [shorthand, key] = to_moz_property_key(M.key, computed, M.quote, M.value); + return { + type: "Property", + computed, + shorthand, + method: false, + kind: "init", + key, + value: to_moz(M.value) + }; + }); + def_to_moz(AST_ConciseMethod, function To_Moz_MethodDefinition(M, parent) { + const computed = M.computed_key(); + const [_always_false, key] = to_moz_property_key(M.key, computed, M.quote, M.value); + if (parent instanceof AST_Object) { + return { + type: "Property", + kind: "init", + computed, + method: true, + shorthand: false, + key, + value: to_moz(M.value) + }; + } + return { + type: "MethodDefinition", + kind: !computed && M.key.name === "constructor" ? "constructor" : "method", + computed, + key, + value: to_moz(M.value), + static: M.static + }; + }); + def_to_moz(AST_PrivateMethod, function To_Moz_MethodDefinition(M) { + return { + type: "MethodDefinition", + kind: "method", + key: { type: "PrivateIdentifier", name: M.key.name }, + value: to_moz(M.value), + computed: false, + static: M.static + }; + }); + def_to_moz(AST_Class, function To_Moz_Class(M) { + var type = M instanceof AST_ClassExpression ? "ClassExpression" : "ClassDeclaration"; + return { + type, + superClass: to_moz(M.extends), + id: M.name ? to_moz(M.name) : null, + body: { + type: "ClassBody", + body: M.properties.map(to_moz) + } + }; + }); + def_to_moz(AST_ClassStaticBlock, function To_Moz_StaticBlock(M) { + return { + type: "StaticBlock", + body: M.body.map(to_moz) + }; + }); + def_to_moz(AST_NewTarget, function To_Moz_MetaProperty() { + return { + type: "MetaProperty", + meta: { + type: "Identifier", + name: "new" + }, + property: { + type: "Identifier", + name: "target" + } + }; + }); + def_to_moz(AST_Symbol, function To_Moz_Identifier(M, parent) { + if (M instanceof AST_SymbolMethod && parent.quote || (M instanceof AST_SymbolImportForeign || M instanceof AST_SymbolExportForeign || M instanceof AST_SymbolExport) && M.quote) { + return { + type: "Literal", + value: M.name + }; + } + var def = M.definition(); + return { + type: "Identifier", + name: def ? def.mangled_name || def.name : M.name + }; + }); + def_to_moz(AST_RegExp, function To_Moz_RegExpLiteral(M) { + const pattern = M.value.source; + const flags = M.value.flags; + return { + type: "Literal", + value: null, + raw: M.print_to_string(), + regex: { pattern, flags } + }; + }); + def_to_moz(AST_Constant, function To_Moz_Literal(M) { + var value = M.value; + return { + type: "Literal", + value, + raw: M.raw || M.print_to_string() + }; + }); + def_to_moz(AST_Atom, function To_Moz_Atom(M) { + return { + type: "Identifier", + name: String(M.value) + }; + }); + def_to_moz(AST_BigInt, (M) => ({ + type: "Literal", + value: null, + bigint: typeof BigInt === "function" ? BigInt(M.value).toString() : M.value, + raw: M.raw + })); + AST_Boolean.DEFMETHOD("to_mozilla_ast", AST_Constant.prototype.to_mozilla_ast); + AST_Null.DEFMETHOD("to_mozilla_ast", AST_Constant.prototype.to_mozilla_ast); + AST_Hole.DEFMETHOD("to_mozilla_ast", function To_Moz_ArrayHole() { + return null; + }); + AST_Block.DEFMETHOD("to_mozilla_ast", AST_BlockStatement.prototype.to_mozilla_ast); + AST_Lambda.DEFMETHOD("to_mozilla_ast", AST_Function.prototype.to_mozilla_ast); + function my_start_token(moznode) { + var loc = moznode.loc, start = loc && loc.start; + var range = moznode.range; + return new AST_Token("", "", start && start.line || 0, start && start.column || 0, range ? range[0] : moznode.start, false, [], [], loc && loc.source); + } + function my_end_token(moznode) { + var loc = moznode.loc, end = loc && loc.end; + var range = moznode.range; + return new AST_Token("", "", end && end.line || 0, end && end.column || 0, range ? range[0] : moznode.end, false, [], [], loc && loc.source); + } + var FROM_MOZ_LABELS = null; + function from_moz(node) { + if (node == null) + return null; + return MOZ_TO_ME[node.type](node); + } + function from_moz_quote(moz_key, computed) { + if (!computed && moz_key.type === "Literal" && typeof moz_key.value === "string") { + return '"'; + } else { + return ""; + } + } + function from_moz_symbol(symbol_type, M, has_quote) { + return new symbol_type({ + start: my_start_token(M), + quote: has_quote ? '"' : undefined, + name: M.type === "Identifier" ? M.name : String(M.value), + end: my_end_token(M) + }); + } + function from_moz_lambda(M, is_method) { + return new (is_method ? AST_Accessor : AST_Function)({ + start: my_start_token(M), + end: my_end_token(M), + name: M.id && from_moz_symbol(is_method ? AST_SymbolMethod : AST_SymbolLambda, M.id), + argnames: M.params.map((M2) => from_moz_pattern(M2, AST_SymbolFunarg)), + is_generator: M.generator, + async: M.async, + body: normalize_directives(from_moz(M.body).body) + }); + } + function from_moz_pattern(M, sym_type) { + switch (M.type) { + case "ObjectPattern": + return new AST_Destructuring({ + start: my_start_token(M), + end: my_end_token(M), + names: M.properties.map((p) => from_moz_pattern(p, sym_type)), + is_array: false + }); + case "Property": + var key = M.key; + var args = { + start: my_start_token(key || M.value), + end: my_end_token(M.value), + key: key.type == "Identifier" ? key.name : String(key.value), + quote: !M.computed && key.type === "Literal" && typeof key.value === "string" ? '"' : "", + value: from_moz_pattern(M.value, sym_type) + }; + if (M.computed) { + args.key = from_moz(M.key); + } + return new AST_ObjectKeyVal(args); + case "ArrayPattern": + return new AST_Destructuring({ + start: my_start_token(M), + end: my_end_token(M), + names: M.elements.map(function(elm) { + if (elm === null) { + return new AST_Hole; + } + return from_moz_pattern(elm, sym_type); + }), + is_array: true + }); + case "SpreadElement": + case "RestElement": + return new AST_Expansion({ + start: my_start_token(M), + end: my_end_token(M), + expression: from_moz_pattern(M.argument, sym_type) + }); + case "AssignmentPattern": + return new AST_DefaultAssign({ + start: my_start_token(M), + end: my_end_token(M), + left: from_moz_pattern(M.left, sym_type), + operator: "=", + right: from_moz(M.right) + }); + case "Identifier": + return new sym_type({ + start: my_start_token(M), + end: my_end_token(M), + name: M.name + }); + default: + throw new Error("Invalid node type for destructuring: " + M.type); + } + } + function from_moz_label_ref(m_label) { + if (!m_label) + return null; + const label = from_moz_symbol(AST_LabelRef, m_label); + let i = FROM_MOZ_LABELS.length; + while (i--) { + const label_origin = FROM_MOZ_LABELS[i]; + if (label.name === label_origin.name) { + label.thedef = label_origin; + break; + } + } + return label; + } + AST_Node.from_mozilla_ast = function(node) { + var save_labels = FROM_MOZ_LABELS; + FROM_MOZ_LABELS = []; + var ast = from_moz(node); + FROM_MOZ_LABELS = save_labels; + return ast; + }; + function set_moz_loc(mynode, moznode) { + var start = mynode.start; + var end = mynode.end; + if (!(start && end)) { + return moznode; + } + if (start.pos != null && end.endpos != null) { + moznode.range = [start.pos, end.endpos]; + } + if (start.line) { + moznode.loc = { + start: { line: start.line, column: start.col }, + end: end.endline ? { line: end.endline, column: end.endcol } : null + }; + if (start.file) { + moznode.loc.source = start.file; + } + } + return moznode; + } + function def_to_moz(mytype, handler) { + mytype.DEFMETHOD("to_mozilla_ast", function(parent) { + return set_moz_loc(this, handler(this, parent)); + }); + } + var TO_MOZ_STACK = null; + function to_moz(node) { + if (TO_MOZ_STACK === null) { + TO_MOZ_STACK = []; + } + TO_MOZ_STACK.push(node); + var ast = node != null ? node.to_mozilla_ast(TO_MOZ_STACK[TO_MOZ_STACK.length - 2]) : null; + TO_MOZ_STACK.pop(); + if (TO_MOZ_STACK.length === 0) { + TO_MOZ_STACK = null; + } + return ast; + } + function to_moz_property_key(key, computed = false, quote = false, value = null) { + if (computed) { + return [false, to_moz(key)]; + } + const key_name = typeof key === "string" ? key : key.name; + let moz_key; + if (quote) { + moz_key = { type: "Literal", value: key_name, raw: JSON.stringify(key_name) }; + } else if ("" + +key_name === key_name && +key_name >= 0) { + moz_key = { type: "Literal", value: +key_name, raw: JSON.stringify(+key_name) }; + } else { + moz_key = { type: "Identifier", name: key_name }; + } + const shorthand = moz_key.type === "Identifier" && moz_key.name === key_name && (value instanceof AST_Symbol && value.name === key_name || value instanceof AST_DefaultAssign && value.left.name === key_name); + return [shorthand, moz_key]; + } + function to_moz_pattern(node) { + if (node instanceof AST_Expansion) { + return { + type: "RestElement", + argument: to_moz_pattern(node.expression) + }; + } + if (node instanceof AST_Symbol || node instanceof AST_Destructuring || node instanceof AST_DefaultAssign || node instanceof AST_PropAccess) { + return to_moz(node); + } + throw new Error(node.TYPE); + } + function to_moz_in_destructuring() { + var i = TO_MOZ_STACK.length; + while (i--) { + if (TO_MOZ_STACK[i] instanceof AST_Destructuring) { + return true; + } + } + return false; + } + function to_moz_block(node) { + return { + type: "BlockStatement", + body: node.body.map(to_moz) + }; + } + function to_moz_scope(type, node) { + var body = node.body.map(to_moz); + if (node.body[0] instanceof AST_SimpleStatement && node.body[0].body instanceof AST_String) { + body.unshift(to_moz(new AST_EmptyStatement(node.body[0]))); + } + return { + type, + body + }; + } +})(); + +// node_modules/terser/lib/utils/first_in_statement.js +function first_in_statement(stack) { + let node = stack.parent(-1); + for (let i = 0, p;p = stack.parent(i); i++) { + if (p instanceof AST_Statement && p.body === node) + return true; + if (p instanceof AST_Sequence && p.expressions[0] === node || p.TYPE === "Call" && p.expression === node || p instanceof AST_PrefixedTemplateString && p.prefix === node || p instanceof AST_Dot && p.expression === node || p instanceof AST_Sub && p.expression === node || p instanceof AST_Chain && p.expression === node || p instanceof AST_Conditional && p.condition === node || p instanceof AST_Binary && p.left === node || p instanceof AST_UnaryPostfix && p.expression === node) { + node = p; + } else { + return false; + } + } +} +function left_is_object(node) { + if (node instanceof AST_Object) + return true; + if (node instanceof AST_Sequence) + return left_is_object(node.expressions[0]); + if (node.TYPE === "Call") + return left_is_object(node.expression); + if (node instanceof AST_PrefixedTemplateString) + return left_is_object(node.prefix); + if (node instanceof AST_Dot || node instanceof AST_Sub) + return left_is_object(node.expression); + if (node instanceof AST_Chain) + return left_is_object(node.expression); + if (node instanceof AST_Conditional) + return left_is_object(node.condition); + if (node instanceof AST_Binary) + return left_is_object(node.left); + if (node instanceof AST_UnaryPostfix) + return left_is_object(node.expression); + return false; +} + +// node_modules/terser/lib/output.js +var CODE_LINE_BREAK = 10; +var CODE_SPACE = 32; +var r_annotation = /[@#]__(PURE|INLINE|NOINLINE)__/; +function is_some_comments(comment) { + return (comment.type === "comment2" || comment.type === "comment1") && /@preserve|@copyright|@lic|@cc_on|^\**!/i.test(comment.value); +} +var ROPE_COMMIT_WHEN = 8 * 1000; + +class Rope { + constructor() { + this.committed = ""; + this.current = ""; + } + append(str) { + if (this.current.length > ROPE_COMMIT_WHEN) { + this.committed += this.current + str; + this.current = ""; + } else { + this.current += str; + } + } + insertAt(char, index) { + const { committed, current } = this; + if (index < committed.length) { + this.committed = committed.slice(0, index) + char + committed.slice(index); + } else if (index === committed.length) { + this.committed += char; + } else { + index -= committed.length; + this.committed += current.slice(0, index) + char; + this.current = current.slice(index); + } + } + charAt(index) { + const { committed } = this; + if (index < committed.length) + return committed[index]; + return this.current[index - committed.length]; + } + charCodeAt(index) { + const { committed } = this; + if (index < committed.length) + return committed.charCodeAt(index); + return this.current.charCodeAt(index - committed.length); + } + length() { + return this.committed.length + this.current.length; + } + expectDirective() { + let ch, n = this.length(); + if (n <= 0) + return true; + while ((ch = this.charCodeAt(--n)) && (ch == CODE_SPACE || ch == CODE_LINE_BREAK)) + ; + return !ch || ch === 59 || ch === 123; + } + hasNLB() { + let n = this.length() - 1; + while (n >= 0) { + const code = this.charCodeAt(n--); + if (code === CODE_LINE_BREAK) + return true; + if (code !== CODE_SPACE) + return false; + } + return true; + } + toString() { + return this.committed + this.current; + } +} +function OutputStream(options) { + var readonly = !options; + options = defaults(options, { + ascii_only: false, + beautify: false, + braces: false, + comments: "some", + ecma: 5, + ie8: false, + indent_level: 4, + indent_start: 0, + inline_script: true, + keep_numbers: false, + keep_quoted_props: false, + max_line_len: false, + preamble: null, + preserve_annotations: false, + quote_keys: false, + quote_style: 0, + safari10: false, + semicolons: true, + shebang: true, + shorthand: undefined, + source_map: null, + webkit: false, + width: 80, + wrap_iife: false, + wrap_func_args: false, + _destroy_ast: false + }, true); + if (options.shorthand === undefined) + options.shorthand = options.ecma > 5; + var comment_filter = return_false; + if (options.comments) { + let comments = options.comments; + if (typeof options.comments === "string" && /^\/.*\/[a-zA-Z]*$/.test(options.comments)) { + var regex_pos = options.comments.lastIndexOf("/"); + comments = new RegExp(options.comments.substr(1, regex_pos - 1), options.comments.substr(regex_pos + 1)); + } + if (comments instanceof RegExp) { + comment_filter = function(comment) { + return comment.type != "comment5" && comments.test(comment.value); + }; + } else if (typeof comments === "function") { + comment_filter = function(comment) { + return comment.type != "comment5" && comments(this, comment); + }; + } else if (comments === "some") { + comment_filter = is_some_comments; + } else { + comment_filter = return_true; + } + } + if (options.preserve_annotations) { + let prev_comment_filter = comment_filter; + comment_filter = function(comment) { + return r_annotation.test(comment.value) || prev_comment_filter.apply(this, arguments); + }; + } + var indentation = 0; + var current_col = 0; + var current_line = 1; + var current_pos = 0; + var OUTPUT = new Rope; + let printed_comments = new Set; + var to_utf8 = options.ascii_only ? function(str, identifier = false, regexp = false) { + if (options.ecma >= 2015 && !options.safari10 && !regexp) { + str = str.replace(/[\ud800-\udbff][\udc00-\udfff]/g, function(ch) { + var code = get_full_char_code(ch, 0).toString(16); + return "\\u{" + code + "}"; + }); + } + return str.replace(/[\u0000-\u001f\u007f-\uffff]/g, function(ch) { + var code = ch.charCodeAt(0).toString(16); + if (code.length <= 2 && !identifier) { + while (code.length < 2) + code = "0" + code; + return "\\x" + code; + } else { + while (code.length < 4) + code = "0" + code; + return "\\u" + code; + } + }); + } : function(str) { + return str.replace(/[\ud800-\udbff][\udc00-\udfff]|([\ud800-\udbff]|[\udc00-\udfff])/g, function(match, lone) { + if (lone) { + return "\\u" + lone.charCodeAt(0).toString(16); + } + return match; + }); + }; + function make_string(str, quote) { + var dq = 0, sq = 0; + str = str.replace(/[\\\b\f\n\r\v\t\x22\x27\u2028\u2029\0\ufeff]/g, function(s, i) { + switch (s) { + case '"': + ++dq; + return '"'; + case "'": + ++sq; + return "'"; + case "\\": + return "\\\\"; + case ` +`: + return "\\n"; + case "\r": + return "\\r"; + case "\t": + return "\\t"; + case "\b": + return "\\b"; + case "\f": + return "\\f"; + case "\v": + return options.ie8 ? "\\x0B" : "\\v"; + case "\u2028": + return "\\u2028"; + case "\u2029": + return "\\u2029"; + case "\uFEFF": + return "\\ufeff"; + case "\x00": + return /[0-9]/.test(get_full_char(str, i + 1)) ? "\\x00" : "\\0"; + } + return s; + }); + function quote_single() { + return "'" + str.replace(/\x27/g, "\\'") + "'"; + } + function quote_double() { + return '"' + str.replace(/\x22/g, "\\\"") + '"'; + } + function quote_template() { + return "`" + str.replace(/`/g, "\\`") + "`"; + } + str = to_utf8(str); + if (quote === "`") + return quote_template(); + switch (options.quote_style) { + case 1: + return quote_single(); + case 2: + return quote_double(); + case 3: + return quote == "'" ? quote_single() : quote_double(); + default: + return dq > sq ? quote_single() : quote_double(); + } + } + function encode_string(str, quote) { + var ret = make_string(str, quote); + if (options.inline_script) { + ret = ret.replace(/<\x2f(script)([>\/\t\n\f\r ])/gi, "<\\/$1$2"); + ret = ret.replace(/\x3c!--/g, "\\x3c!--"); + ret = ret.replace(/--\x3e/g, "--\\x3e"); + } + return ret; + } + function make_name(name) { + name = name.toString(); + name = to_utf8(name, true); + return name; + } + function make_indent(back) { + return " ".repeat(options.indent_start + indentation - back * options.indent_level); + } + var has_parens = false; + var might_need_space = false; + var might_need_semicolon = false; + var might_add_newline = 0; + var need_newline_indented = false; + var need_space = false; + var newline_insert = -1; + var last = ""; + var mapping_token, mapping_name, mappings = options.source_map && []; + var do_add_mapping = mappings ? function() { + mappings.forEach(function(mapping) { + try { + let { name, token } = mapping; + if (name !== false) { + if (token.type == "name" || token.type === "privatename") { + name = token.value; + } else if (name instanceof AST_Symbol) { + name = token.type === "string" ? token.value : name.name; + } + } + options.source_map.add(mapping.token.file, mapping.line, mapping.col, mapping.token.line, mapping.token.col, is_basic_identifier_string(name) ? name : undefined); + } catch (ex) {} + }); + mappings = []; + } : noop; + var ensure_line_len = options.max_line_len ? function() { + if (current_col > options.max_line_len) { + if (might_add_newline) { + OUTPUT.insertAt(` +`, might_add_newline); + const len_after_newline = OUTPUT.length() - might_add_newline - 1; + if (mappings) { + var delta = len_after_newline - current_col; + mappings.forEach(function(mapping) { + mapping.line++; + mapping.col += delta; + }); + } + current_line++; + current_pos++; + current_col = len_after_newline; + } + } + if (might_add_newline) { + might_add_newline = 0; + do_add_mapping(); + } + } : noop; + var requireSemicolonChars = makePredicate("( [ + * / - , . `"); + function print(str) { + str = String(str); + var ch = get_full_char(str, 0); + if (need_newline_indented && ch) { + need_newline_indented = false; + if (ch !== ` +`) { + print(` +`); + indent(); + } + } + if (need_space && ch) { + need_space = false; + if (!/[\s;})]/.test(ch)) { + space(); + } + } + newline_insert = -1; + var prev = last.charAt(last.length - 1); + if (might_need_semicolon) { + might_need_semicolon = false; + if (prev === ":" && ch === "}" || (!ch || !";}".includes(ch)) && prev !== ";") { + if (options.semicolons || requireSemicolonChars.has(ch)) { + OUTPUT.append(";"); + current_col++; + current_pos++; + } else { + ensure_line_len(); + if (current_col > 0) { + OUTPUT.append(` +`); + current_pos++; + current_line++; + current_col = 0; + } + if (/^\s+$/.test(str)) { + might_need_semicolon = true; + } + } + if (!options.beautify) + might_need_space = false; + } + } + if (might_need_space) { + if (is_identifier_char(prev) && (is_identifier_char(ch) || ch == "\\") || ch == "/" && ch == prev || (ch == "+" || ch == "-") && ch == last) { + OUTPUT.append(" "); + current_col++; + current_pos++; + } + might_need_space = false; + } + if (mapping_token) { + mappings.push({ + token: mapping_token, + name: mapping_name, + line: current_line, + col: current_col + }); + mapping_token = false; + if (!might_add_newline) + do_add_mapping(); + } + OUTPUT.append(str); + has_parens = str[str.length - 1] == "("; + current_pos += str.length; + var a = str.split(/\r?\n/), n = a.length - 1; + current_line += n; + current_col += a[0].length; + if (n > 0) { + ensure_line_len(); + current_col = a[n].length; + } + last = str; + } + var star = function() { + print("*"); + }; + var space = options.beautify ? function() { + print(" "); + } : function() { + might_need_space = true; + }; + var indent = options.beautify ? function(half) { + if (options.beautify) { + print(make_indent(half ? 0.5 : 0)); + } + } : noop; + var with_indent = options.beautify ? function(col, cont) { + if (col === true) + col = next_indent(); + var save_indentation = indentation; + indentation = col; + var ret = cont(); + indentation = save_indentation; + return ret; + } : function(col, cont) { + return cont(); + }; + var newline = options.beautify ? function() { + if (newline_insert < 0) + return print(` +`); + if (OUTPUT.charAt(newline_insert) != ` +`) { + OUTPUT.insertAt(` +`, newline_insert); + current_pos++; + current_line++; + } + newline_insert++; + } : options.max_line_len ? function() { + ensure_line_len(); + might_add_newline = OUTPUT.length(); + } : noop; + var semicolon = options.beautify ? function() { + print(";"); + } : function() { + might_need_semicolon = true; + }; + function force_semicolon() { + might_need_semicolon = false; + print(";"); + } + function next_indent() { + return indentation + options.indent_level; + } + function with_block(cont) { + var ret; + print("{"); + newline(); + with_indent(next_indent(), function() { + ret = cont(); + }); + indent(); + print("}"); + return ret; + } + function with_parens(cont) { + print("("); + var ret = cont(); + print(")"); + return ret; + } + function with_square(cont) { + print("["); + var ret = cont(); + print("]"); + return ret; + } + function comma() { + print(","); + space(); + } + function colon() { + print(":"); + space(); + } + var add_mapping = mappings ? function(token, name) { + mapping_token = token; + mapping_name = name; + } : noop; + function get() { + if (might_add_newline) { + ensure_line_len(); + } + return OUTPUT.toString(); + } + function filter_comment(comment) { + if (!options.preserve_annotations) { + comment = comment.replace(r_annotation, " "); + } + if (/^\s*$/.test(comment)) { + return ""; + } + return comment.replace(/(<\s*\/\s*)(script)/i, "<\\/$2"); + } + function prepend_comments(node) { + var self2 = this; + var start = node.start; + if (!start) + return; + var printed_comments2 = self2.printed_comments; + const keyword_with_value = node instanceof AST_Exit && node.value || (node instanceof AST_Await || node instanceof AST_Yield) && node.expression; + if (start.comments_before && printed_comments2.has(start.comments_before)) { + if (keyword_with_value) { + start.comments_before = []; + } else { + return; + } + } + var comments = start.comments_before; + if (!comments) { + comments = start.comments_before = []; + } + printed_comments2.add(comments); + if (keyword_with_value) { + var tw = new TreeWalker(function(node2) { + var parent = tw.parent(); + if (parent instanceof AST_Exit || parent instanceof AST_Await || parent instanceof AST_Yield || parent instanceof AST_Binary && parent.left === node2 || parent.TYPE == "Call" && parent.expression === node2 || parent instanceof AST_Conditional && parent.condition === node2 || parent instanceof AST_Dot && parent.expression === node2 || parent instanceof AST_Sequence && parent.expressions[0] === node2 || parent instanceof AST_Sub && parent.expression === node2 || parent instanceof AST_UnaryPostfix) { + if (!node2.start) + return; + var text = node2.start.comments_before; + if (text && !printed_comments2.has(text)) { + printed_comments2.add(text); + comments = comments.concat(text); + } + } else { + return true; + } + }); + tw.push(node); + keyword_with_value.walk(tw); + } + if (current_pos == 0) { + if (comments.length > 0 && options.shebang && comments[0].type === "comment5" && !printed_comments2.has(comments[0])) { + print("#!" + comments.shift().value + ` +`); + indent(); + } + var preamble = options.preamble; + if (preamble) { + print(preamble.replace(/\r\n?|[\n\u2028\u2029]|\s*$/g, ` +`)); + } + } + comments = comments.filter(comment_filter, node).filter((c) => !printed_comments2.has(c)); + if (comments.length == 0) + return; + var last_nlb = OUTPUT.hasNLB(); + comments.forEach(function(c, i) { + printed_comments2.add(c); + if (!last_nlb) { + if (c.nlb) { + print(` +`); + indent(); + last_nlb = true; + } else if (i > 0) { + space(); + } + } + if (/comment[134]/.test(c.type)) { + var value = filter_comment(c.value); + if (value) { + print("//" + value + ` +`); + indent(); + } + last_nlb = true; + } else if (c.type == "comment2") { + var value = filter_comment(c.value); + if (value) { + print("/*" + value + "*/"); + } + last_nlb = false; + } + }); + if (!last_nlb) { + if (start.nlb) { + print(` +`); + indent(); + } else { + space(); + } + } + } + function append_comments(node, tail) { + var self2 = this; + var token = node.end; + if (!token) + return; + var printed_comments2 = self2.printed_comments; + var comments = token[tail ? "comments_before" : "comments_after"]; + if (!comments || printed_comments2.has(comments)) + return; + if (!(node instanceof AST_Statement || comments.every((c) => !/comment[134]/.test(c.type)))) + return; + printed_comments2.add(comments); + var insert = OUTPUT.length(); + comments.filter(comment_filter, node).forEach(function(c, i) { + if (printed_comments2.has(c)) + return; + printed_comments2.add(c); + need_space = false; + if (need_newline_indented) { + print(` +`); + indent(); + need_newline_indented = false; + } else if (c.nlb && (i > 0 || !OUTPUT.hasNLB())) { + print(` +`); + indent(); + } else if (i > 0 || !tail) { + space(); + } + if (/comment[134]/.test(c.type)) { + const value = filter_comment(c.value); + if (value) { + print("//" + value); + } + need_newline_indented = true; + } else if (c.type == "comment2") { + const value = filter_comment(c.value); + if (value) { + print("/*" + value + "*/"); + } + need_space = true; + } + }); + if (OUTPUT.length() > insert) + newline_insert = insert; + } + const gc_scope = options["_destroy_ast"] ? function gc_scope(scope) { + scope.body.length = 0; + scope.argnames.length = 0; + } : noop; + var stack = []; + return { + get, + toString: get, + indent, + in_directive: false, + use_asm: null, + active_scope: null, + indentation: function() { + return indentation; + }, + current_width: function() { + return current_col - indentation; + }, + should_break: function() { + return options.width && this.current_width() >= options.width; + }, + has_parens: function() { + return has_parens; + }, + newline, + print, + star, + space, + comma, + colon, + last: function() { + return last; + }, + semicolon, + force_semicolon, + to_utf8, + print_name: function(name) { + print(make_name(name)); + }, + print_string: function(str, quote, escape_directive) { + var encoded = encode_string(str, quote); + if (escape_directive === true && !encoded.includes("\\")) { + if (!OUTPUT.expectDirective()) { + force_semicolon(); + } + force_semicolon(); + } + print(encoded); + }, + print_template_string_chars: function(str) { + var encoded = encode_string(str, "`").replace(/\${/g, "\\${"); + return print(encoded.substr(1, encoded.length - 2)); + }, + encode_string, + next_indent, + with_indent, + with_block, + with_parens, + with_square, + add_mapping, + option: function(opt) { + return options[opt]; + }, + gc_scope, + printed_comments, + prepend_comments: readonly ? noop : prepend_comments, + append_comments: readonly || comment_filter === return_false ? noop : append_comments, + line: function() { + return current_line; + }, + col: function() { + return current_col; + }, + pos: function() { + return current_pos; + }, + push_node: function(node) { + stack.push(node); + }, + pop_node: function() { + return stack.pop(); + }, + parent: function(n) { + return stack[stack.length - 2 - (n || 0)]; + } + }; +} +(function() { + function DEFPRINT(nodetype, generator) { + nodetype.DEFMETHOD("_codegen", generator); + } + AST_Node.DEFMETHOD("print", function(output, force_parens) { + var self2 = this, generator = self2._codegen; + if (self2 instanceof AST_Scope) { + output.active_scope = self2; + } else if (!output.use_asm && self2 instanceof AST_Directive && self2.value == "use asm") { + output.use_asm = output.active_scope; + } + function doit() { + output.prepend_comments(self2); + self2.add_source_map(output); + generator(self2, output); + output.append_comments(self2); + } + output.push_node(self2); + if (force_parens || self2.needs_parens(output)) { + output.with_parens(doit); + } else { + doit(); + } + output.pop_node(); + if (self2 === output.use_asm) { + output.use_asm = null; + } + }); + AST_Node.DEFMETHOD("_print", AST_Node.prototype.print); + AST_Node.DEFMETHOD("print_to_string", function(options) { + var output = OutputStream(options); + this.print(output); + return output.get(); + }); + function PARENS(nodetype, func) { + if (Array.isArray(nodetype)) { + nodetype.forEach(function(nodetype2) { + PARENS(nodetype2, func); + }); + } else { + nodetype.DEFMETHOD("needs_parens", func); + } + } + PARENS(AST_Node, return_false); + PARENS(AST_Function, function(output) { + if (!output.has_parens() && first_in_statement(output)) { + return true; + } + if (output.option("webkit")) { + var p = output.parent(); + if (p instanceof AST_PropAccess && p.expression === this) { + return true; + } + } + if (output.option("wrap_iife")) { + var p = output.parent(); + if (p instanceof AST_Call && p.expression === this) { + return true; + } + } + if (output.option("wrap_func_args")) { + var p = output.parent(); + if (p instanceof AST_Call && p.args.includes(this)) { + return true; + } + } + return false; + }); + PARENS(AST_Arrow, function(output) { + var p = output.parent(); + if (output.option("wrap_func_args") && p instanceof AST_Call && p.args.includes(this)) { + return true; + } + return p instanceof AST_PropAccess && p.expression === this || p instanceof AST_Conditional && p.condition === this; + }); + PARENS(AST_Object, function(output) { + return !output.has_parens() && first_in_statement(output); + }); + PARENS(AST_ClassExpression, first_in_statement); + PARENS(AST_Unary, function(output) { + var p = output.parent(); + return p instanceof AST_PropAccess && p.expression === this || p instanceof AST_Call && p.expression === this || p instanceof AST_Binary && p.operator === "**" && this instanceof AST_UnaryPrefix && p.left === this && this.operator !== "++" && this.operator !== "--"; + }); + PARENS(AST_Await, function(output) { + var p = output.parent(); + return p instanceof AST_PropAccess && p.expression === this || p instanceof AST_Call && p.expression === this || p instanceof AST_Binary && p.operator === "**" && p.left === this || output.option("safari10") && p instanceof AST_UnaryPrefix; + }); + PARENS(AST_Sequence, function(output) { + var p = output.parent(); + return p instanceof AST_Call || p instanceof AST_Unary || p instanceof AST_Binary || p instanceof AST_VarDef || p instanceof AST_PropAccess || p instanceof AST_Array || p instanceof AST_ObjectProperty || p instanceof AST_Conditional || p instanceof AST_Arrow || p instanceof AST_DefaultAssign || p instanceof AST_Expansion || p instanceof AST_ForOf && this === p.object || p instanceof AST_Yield || p instanceof AST_Export; + }); + PARENS(AST_Binary, function(output) { + var p = output.parent(); + if (p instanceof AST_Call && p.expression === this) + return true; + if (p instanceof AST_Unary) + return true; + if (p instanceof AST_PropAccess && p.expression === this) + return true; + if (p instanceof AST_Binary) { + const parent_op = p.operator; + const op = this.operator; + if (op === "??" && (parent_op === "||" || parent_op === "&&")) { + return true; + } + if (parent_op === "??" && (op === "||" || op === "&&")) { + return true; + } + const pp = PRECEDENCE[parent_op]; + const sp = PRECEDENCE[op]; + if (pp > sp || pp == sp && (this === p.right || parent_op == "**")) { + return true; + } + } + if (p instanceof AST_PrivateIn) { + const op = this.operator; + const pp = PRECEDENCE["in"]; + const sp = PRECEDENCE[op]; + if (pp > sp || pp == sp && this === p.value) { + return true; + } + } + }); + PARENS(AST_PrivateIn, function(output) { + var p = output.parent(); + if (p instanceof AST_Call && p.expression === this) { + return true; + } + if (p instanceof AST_Unary) { + return true; + } + if (p instanceof AST_PropAccess && p.expression === this) { + return true; + } + if (p instanceof AST_Binary) { + const parent_op = p.operator; + const pp = PRECEDENCE[parent_op]; + const sp = PRECEDENCE["in"]; + if (pp > sp || pp == sp && (this === p.right || parent_op == "**")) { + return true; + } + } + if (p instanceof AST_PrivateIn && this === p.value) { + return true; + } + }); + PARENS(AST_Yield, function(output) { + var p = output.parent(); + if (p instanceof AST_Binary && p.operator !== "=") + return true; + if (p instanceof AST_Call && p.expression === this) + return true; + if (p instanceof AST_Conditional && p.condition === this) + return true; + if (p instanceof AST_Unary) + return true; + if (p instanceof AST_PropAccess && p.expression === this) + return true; + }); + PARENS(AST_Chain, function(output) { + var p = output.parent(); + if (!(p instanceof AST_Call || p instanceof AST_PropAccess)) + return false; + return p.expression === this; + }); + PARENS(AST_PropAccess, function(output) { + var p = output.parent(); + if (p instanceof AST_New && p.expression === this) { + return walk(this, (node) => { + if (node instanceof AST_Scope) + return true; + if (node instanceof AST_Call) { + return walk_abort; + } + }); + } + }); + PARENS(AST_Call, function(output) { + var p = output.parent(), p1; + if (p instanceof AST_New && p.expression === this || p instanceof AST_Export && p.is_default && this.expression instanceof AST_Function) + return true; + return this.expression instanceof AST_Function && p instanceof AST_PropAccess && p.expression === this && (p1 = output.parent(1)) instanceof AST_Assign && p1.left === p; + }); + PARENS(AST_New, function(output) { + var p = output.parent(); + if (this.args.length === 0 && (p instanceof AST_PropAccess || p instanceof AST_Call && p.expression === this || p instanceof AST_PrefixedTemplateString && p.prefix === this)) + return true; + }); + PARENS(AST_Number, function(output) { + var p = output.parent(); + if (p instanceof AST_PropAccess && p.expression === this) { + var value = this.getValue(); + if (value < 0 || /^0/.test(make_num(value))) { + return true; + } + } + }); + PARENS(AST_BigInt, function(output) { + var p = output.parent(); + if (p instanceof AST_PropAccess && p.expression === this) { + var value = this.getValue(); + if (value.startsWith("-")) { + return true; + } + } + }); + PARENS([AST_Assign, AST_Conditional], function(output) { + var p = output.parent(); + if (p instanceof AST_Unary) + return true; + if (p instanceof AST_Binary && !(p instanceof AST_Assign)) + return true; + if (p instanceof AST_Call && p.expression === this) + return true; + if (p instanceof AST_Conditional && p.condition === this) + return true; + if (p instanceof AST_PropAccess && p.expression === this) + return true; + if (this instanceof AST_Assign && this.left instanceof AST_Destructuring && this.left.is_array === false) + return true; + }); + DEFPRINT(AST_Directive, function(self2, output) { + output.print_string(self2.value, self2.quote); + output.semicolon(); + }); + DEFPRINT(AST_Expansion, function(self2, output) { + output.print("..."); + self2.expression.print(output); + }); + DEFPRINT(AST_Destructuring, function(self2, output) { + output.print(self2.is_array ? "[" : "{"); + var len = self2.names.length; + self2.names.forEach(function(name, i) { + if (i > 0) + output.comma(); + name.print(output); + if (i == len - 1 && name instanceof AST_Hole) + output.comma(); + }); + output.print(self2.is_array ? "]" : "}"); + }); + DEFPRINT(AST_Debugger, function(self2, output) { + output.print("debugger"); + output.semicolon(); + }); + function display_body(body, is_toplevel, output, allow_directives) { + var last = body.length - 1; + output.in_directive = allow_directives; + body.forEach(function(stmt, i) { + if (output.in_directive === true && !(stmt instanceof AST_Directive || stmt instanceof AST_EmptyStatement || stmt instanceof AST_SimpleStatement && stmt.body instanceof AST_String)) { + output.in_directive = false; + } + if (!(stmt instanceof AST_EmptyStatement)) { + output.indent(); + stmt.print(output); + if (!(i == last && is_toplevel)) { + output.newline(); + if (is_toplevel) + output.newline(); + } + } + if (output.in_directive === true && stmt instanceof AST_SimpleStatement && stmt.body instanceof AST_String) { + output.in_directive = false; + } + }); + output.in_directive = false; + } + AST_StatementWithBody.DEFMETHOD("_do_print_body", function(output) { + print_maybe_braced_body(this.body, output); + }); + DEFPRINT(AST_Statement, function(self2, output) { + self2.body.print(output); + output.semicolon(); + }); + DEFPRINT(AST_Toplevel, function(self2, output) { + display_body(self2.body, true, output, true); + output.print(""); + }); + DEFPRINT(AST_LabeledStatement, function(self2, output) { + self2.label.print(output); + output.colon(); + self2.body.print(output); + }); + DEFPRINT(AST_SimpleStatement, function(self2, output) { + self2.body.print(output); + output.semicolon(); + }); + function print_braced_empty(self2, output) { + output.print("{"); + output.with_indent(output.next_indent(), function() { + output.append_comments(self2, true); + }); + output.add_mapping(self2.end); + output.print("}"); + } + function print_braced(self2, output, allow_directives) { + if (self2.body.length > 0) { + output.with_block(function() { + display_body(self2.body, false, output, allow_directives); + output.add_mapping(self2.end); + }); + } else + print_braced_empty(self2, output); + } + DEFPRINT(AST_BlockStatement, function(self2, output) { + print_braced(self2, output); + }); + DEFPRINT(AST_EmptyStatement, function(self2, output) { + output.semicolon(); + }); + DEFPRINT(AST_Do, function(self2, output) { + output.print("do"); + output.space(); + make_block(self2.body, output); + output.space(); + output.print("while"); + output.space(); + output.with_parens(function() { + self2.condition.print(output); + }); + output.semicolon(); + }); + DEFPRINT(AST_While, function(self2, output) { + output.print("while"); + output.space(); + output.with_parens(function() { + self2.condition.print(output); + }); + output.space(); + self2._do_print_body(output); + }); + DEFPRINT(AST_For, function(self2, output) { + output.print("for"); + output.space(); + output.with_parens(function() { + if (self2.init) { + if (self2.init instanceof AST_Definitions) { + self2.init.print(output); + } else { + parenthesize_for_noin(self2.init, output, true); + } + output.print(";"); + output.space(); + } else { + output.print(";"); + } + if (self2.condition) { + self2.condition.print(output); + output.print(";"); + output.space(); + } else { + output.print(";"); + } + if (self2.step) { + self2.step.print(output); + } + }); + output.space(); + self2._do_print_body(output); + }); + DEFPRINT(AST_ForIn, function(self2, output) { + output.print("for"); + if (self2.await) { + output.space(); + output.print("await"); + } + output.space(); + output.with_parens(function() { + self2.init.print(output); + output.space(); + output.print(self2 instanceof AST_ForOf ? "of" : "in"); + output.space(); + self2.object.print(output); + }); + output.space(); + self2._do_print_body(output); + }); + DEFPRINT(AST_With, function(self2, output) { + output.print("with"); + output.space(); + output.with_parens(function() { + self2.expression.print(output); + }); + output.space(); + self2._do_print_body(output); + }); + AST_Lambda.DEFMETHOD("_do_print", function(output, nokeyword) { + var self2 = this; + if (!nokeyword) { + if (self2.async) { + output.print("async"); + output.space(); + } + output.print("function"); + if (self2.is_generator) { + output.star(); + } + if (self2.name) { + output.space(); + } + } + if (self2.name instanceof AST_Symbol) { + self2.name.print(output); + } else if (nokeyword && self2.name instanceof AST_Node) { + output.with_square(function() { + self2.name.print(output); + }); + } + output.with_parens(function() { + self2.argnames.forEach(function(arg, i) { + if (i) + output.comma(); + arg.print(output); + }); + }); + output.space(); + print_braced(self2, output, true); + }); + DEFPRINT(AST_Lambda, function(self2, output) { + self2._do_print(output); + output.gc_scope(self2); + }); + DEFPRINT(AST_PrefixedTemplateString, function(self2, output) { + var tag = self2.prefix; + var parenthesize_tag = tag instanceof AST_Lambda || tag instanceof AST_Binary || tag instanceof AST_Conditional || tag instanceof AST_Sequence || tag instanceof AST_Unary || tag instanceof AST_Dot && tag.expression instanceof AST_Object; + if (parenthesize_tag) + output.print("("); + self2.prefix.print(output); + if (parenthesize_tag) + output.print(")"); + self2.template_string.print(output); + }); + DEFPRINT(AST_TemplateString, function(self2, output) { + var is_tagged = output.parent() instanceof AST_PrefixedTemplateString; + output.print("`"); + for (var i = 0;i < self2.segments.length; i++) { + if (!(self2.segments[i] instanceof AST_TemplateSegment)) { + output.print("${"); + self2.segments[i].print(output); + output.print("}"); + } else if (is_tagged) { + output.print(self2.segments[i].raw); + } else { + output.print_template_string_chars(self2.segments[i].value); + } + } + output.print("`"); + }); + DEFPRINT(AST_TemplateSegment, function(self2, output) { + output.print_template_string_chars(self2.value); + }); + AST_Arrow.DEFMETHOD("_do_print", function(output) { + var self2 = this; + var parent = output.parent(); + var needs_parens = parent instanceof AST_Binary && !(parent instanceof AST_Assign) && !(parent instanceof AST_DefaultAssign) || parent instanceof AST_Unary || parent instanceof AST_Call && self2 === parent.expression; + if (needs_parens) { + output.print("("); + } + if (self2.async) { + output.print("async"); + output.space(); + } + if (self2.argnames.length === 1 && self2.argnames[0] instanceof AST_Symbol) { + self2.argnames[0].print(output); + } else { + output.with_parens(function() { + self2.argnames.forEach(function(arg, i) { + if (i) + output.comma(); + arg.print(output); + }); + }); + } + output.space(); + output.print("=>"); + output.space(); + const first_statement = self2.body[0]; + if (self2.body.length === 1 && first_statement instanceof AST_Return) { + const returned = first_statement.value; + if (!returned) { + output.print("{}"); + } else if (left_is_object(returned)) { + output.print("("); + returned.print(output); + output.print(")"); + } else { + returned.print(output); + } + } else { + print_braced(self2, output); + } + if (needs_parens) { + output.print(")"); + } + output.gc_scope(self2); + }); + AST_Exit.DEFMETHOD("_do_print", function(output, kind) { + output.print(kind); + if (this.value) { + output.space(); + const comments = this.value.start.comments_before; + if (comments && comments.length && !output.printed_comments.has(comments)) { + output.print("("); + this.value.print(output); + output.print(")"); + } else { + this.value.print(output); + } + } + output.semicolon(); + }); + DEFPRINT(AST_Return, function(self2, output) { + self2._do_print(output, "return"); + }); + DEFPRINT(AST_Throw, function(self2, output) { + self2._do_print(output, "throw"); + }); + DEFPRINT(AST_Yield, function(self2, output) { + var star = self2.is_star ? "*" : ""; + output.print("yield" + star); + if (self2.expression) { + output.space(); + self2.expression.print(output); + } + }); + DEFPRINT(AST_Await, function(self2, output) { + output.print("await"); + output.space(); + var e = self2.expression; + var parens = !(e instanceof AST_Call || e instanceof AST_SymbolRef || e instanceof AST_PropAccess || e instanceof AST_Unary || e instanceof AST_Constant || e instanceof AST_Await || e instanceof AST_Object); + if (parens) + output.print("("); + self2.expression.print(output); + if (parens) + output.print(")"); + }); + AST_LoopControl.DEFMETHOD("_do_print", function(output, kind) { + output.print(kind); + if (this.label) { + output.space(); + this.label.print(output); + } + output.semicolon(); + }); + DEFPRINT(AST_Break, function(self2, output) { + self2._do_print(output, "break"); + }); + DEFPRINT(AST_Continue, function(self2, output) { + self2._do_print(output, "continue"); + }); + function make_then(self2, output) { + var b = self2.body; + if (output.option("braces") || output.option("ie8") && b instanceof AST_Do) + return make_block(b, output); + if (!b) + return output.force_semicolon(); + while (true) { + if (b instanceof AST_If) { + if (!b.alternative) { + make_block(self2.body, output); + return; + } + b = b.alternative; + } else if (b instanceof AST_StatementWithBody) { + b = b.body; + } else + break; + } + print_maybe_braced_body(self2.body, output); + } + DEFPRINT(AST_If, function(self2, output) { + output.print("if"); + output.space(); + output.with_parens(function() { + self2.condition.print(output); + }); + output.space(); + if (self2.alternative) { + make_then(self2, output); + output.space(); + output.print("else"); + output.space(); + if (self2.alternative instanceof AST_If) + self2.alternative.print(output); + else + print_maybe_braced_body(self2.alternative, output); + } else { + self2._do_print_body(output); + } + }); + DEFPRINT(AST_Switch, function(self2, output) { + output.print("switch"); + output.space(); + output.with_parens(function() { + self2.expression.print(output); + }); + output.space(); + var last = self2.body.length - 1; + if (last < 0) + print_braced_empty(self2, output); + else + output.with_block(function() { + self2.body.forEach(function(branch, i) { + output.indent(true); + branch.print(output); + if (i < last && branch.body.length > 0) + output.newline(); + }); + }); + }); + AST_SwitchBranch.DEFMETHOD("_do_print_body", function(output) { + output.newline(); + this.body.forEach(function(stmt) { + output.indent(); + stmt.print(output); + output.newline(); + }); + }); + DEFPRINT(AST_Default, function(self2, output) { + output.print("default:"); + self2._do_print_body(output); + }); + DEFPRINT(AST_Case, function(self2, output) { + output.print("case"); + output.space(); + self2.expression.print(output); + output.print(":"); + self2._do_print_body(output); + }); + DEFPRINT(AST_Try, function(self2, output) { + output.print("try"); + output.space(); + self2.body.print(output); + if (self2.bcatch) { + output.space(); + self2.bcatch.print(output); + } + if (self2.bfinally) { + output.space(); + self2.bfinally.print(output); + } + }); + DEFPRINT(AST_TryBlock, function(self2, output) { + print_braced(self2, output); + }); + DEFPRINT(AST_Catch, function(self2, output) { + output.print("catch"); + if (self2.argname) { + output.space(); + output.with_parens(function() { + self2.argname.print(output); + }); + } + output.space(); + print_braced(self2, output); + }); + DEFPRINT(AST_Finally, function(self2, output) { + output.print("finally"); + output.space(); + print_braced(self2, output); + }); + AST_Definitions.DEFMETHOD("_do_print", function(output, kind) { + output.print(kind); + output.space(); + this.definitions.forEach(function(def, i) { + if (i) + output.comma(); + def.print(output); + }); + var p = output.parent(); + var in_for = p instanceof AST_For || p instanceof AST_ForIn; + var output_semicolon = !in_for || p && p.init !== this; + if (output_semicolon) + output.semicolon(); + }); + DEFPRINT(AST_Let, function(self2, output) { + self2._do_print(output, "let"); + }); + DEFPRINT(AST_Var, function(self2, output) { + self2._do_print(output, "var"); + }); + DEFPRINT(AST_Const, function(self2, output) { + self2._do_print(output, "const"); + }); + DEFPRINT(AST_Import, function(self2, output) { + output.print("import"); + output.space(); + if (self2.imported_name) { + self2.imported_name.print(output); + } + if (self2.imported_name && self2.imported_names) { + output.print(","); + output.space(); + } + if (self2.imported_names) { + if (self2.imported_names.length === 1 && self2.imported_names[0].foreign_name.name === "*" && !self2.imported_names[0].foreign_name.quote) { + self2.imported_names[0].print(output); + } else { + output.print("{"); + self2.imported_names.forEach(function(name_import, i) { + output.space(); + name_import.print(output); + if (i < self2.imported_names.length - 1) { + output.print(","); + } + }); + output.space(); + output.print("}"); + } + } + if (self2.imported_name || self2.imported_names) { + output.space(); + output.print("from"); + output.space(); + } + self2.module_name.print(output); + if (self2.attributes) { + output.print("with"); + self2.attributes.print(output); + } + output.semicolon(); + }); + DEFPRINT(AST_ImportMeta, function(self2, output) { + output.print("import.meta"); + }); + DEFPRINT(AST_NameMapping, function(self2, output) { + var is_import = output.parent() instanceof AST_Import; + var definition = self2.name.definition(); + var foreign_name = self2.foreign_name; + var names_are_different = (definition && definition.mangled_name || self2.name.name) !== foreign_name.name; + if (!names_are_different && foreign_name.name === "*" && !!foreign_name.quote != !!self2.name.quote) { + names_are_different = true; + } + var foreign_name_is_name = !foreign_name.quote; + if (names_are_different) { + if (is_import) { + if (foreign_name_is_name) { + output.print(foreign_name.name); + } else { + output.print_string(foreign_name.name, foreign_name.quote); + } + } else { + if (!self2.name.quote) { + self2.name.print(output); + } else { + output.print_string(self2.name.name, self2.name.quote); + } + } + output.space(); + output.print("as"); + output.space(); + if (is_import) { + self2.name.print(output); + } else { + if (foreign_name_is_name) { + output.print(foreign_name.name); + } else { + output.print_string(foreign_name.name, foreign_name.quote); + } + } + } else { + if (!self2.name.quote) { + self2.name.print(output); + } else { + output.print_string(self2.name.name, self2.name.quote); + } + } + }); + DEFPRINT(AST_Export, function(self2, output) { + output.print("export"); + output.space(); + if (self2.is_default) { + output.print("default"); + output.space(); + } + if (self2.exported_names) { + if (self2.exported_names.length === 1 && self2.exported_names[0].name.name === "*" && !self2.exported_names[0].name.quote) { + self2.exported_names[0].print(output); + } else { + output.print("{"); + self2.exported_names.forEach(function(name_export, i) { + output.space(); + name_export.print(output); + if (i < self2.exported_names.length - 1) { + output.print(","); + } + }); + output.space(); + output.print("}"); + } + } else if (self2.exported_value) { + self2.exported_value.print(output); + } else if (self2.exported_definition) { + self2.exported_definition.print(output); + if (self2.exported_definition instanceof AST_Definitions) + return; + } + if (self2.module_name) { + output.space(); + output.print("from"); + output.space(); + self2.module_name.print(output); + } + if (self2.attributes) { + output.print("with"); + self2.attributes.print(output); + } + if (self2.exported_value && !(self2.exported_value instanceof AST_Defun || self2.exported_value instanceof AST_Function || self2.exported_value instanceof AST_Class) || self2.module_name || self2.exported_names) { + output.semicolon(); + } + }); + function parenthesize_for_noin(node, output, noin) { + var parens = false; + if (noin) { + parens = walk(node, (node2) => { + if (node2 instanceof AST_Scope && !(node2 instanceof AST_Arrow)) { + return true; + } + if (node2 instanceof AST_Binary && node2.operator == "in" || node2 instanceof AST_PrivateIn) { + return walk_abort; + } + }); + } + node.print(output, parens); + } + DEFPRINT(AST_VarDef, function(self2, output) { + self2.name.print(output); + if (self2.value) { + output.space(); + output.print("="); + output.space(); + var p = output.parent(1); + var noin = p instanceof AST_For || p instanceof AST_ForIn; + parenthesize_for_noin(self2.value, output, noin); + } + }); + DEFPRINT(AST_Call, function(self2, output) { + self2.expression.print(output); + if (self2 instanceof AST_New && self2.args.length === 0) + return; + if (self2.expression instanceof AST_Call || self2.expression instanceof AST_Lambda) { + output.add_mapping(self2.start); + } + if (self2.optional) + output.print("?."); + output.with_parens(function() { + self2.args.forEach(function(expr, i) { + if (i) + output.comma(); + expr.print(output); + }); + }); + }); + DEFPRINT(AST_New, function(self2, output) { + output.print("new"); + output.space(); + AST_Call.prototype._codegen(self2, output); + }); + AST_Sequence.DEFMETHOD("_do_print", function(output) { + this.expressions.forEach(function(node, index) { + if (index > 0) { + output.comma(); + if (output.should_break()) { + output.newline(); + output.indent(); + } + } + node.print(output); + }); + }); + DEFPRINT(AST_Sequence, function(self2, output) { + self2._do_print(output); + }); + DEFPRINT(AST_Dot, function(self2, output) { + var expr = self2.expression; + expr.print(output); + var prop = self2.property; + var print_computed = ALL_RESERVED_WORDS.has(prop) ? output.option("ie8") : !is_identifier_string(prop, output.option("ecma") >= 2015 && !output.option("safari10")); + if (self2.optional) + output.print("?."); + if (print_computed) { + output.print("["); + output.add_mapping(self2.end); + output.print_string(prop); + output.print("]"); + } else { + if (expr instanceof AST_Number && expr.getValue() >= 0) { + if (!/[xa-f.)]/i.test(output.last())) { + output.print("."); + } + } + if (!self2.optional) + output.print("."); + output.add_mapping(self2.end); + output.print_name(prop); + } + }); + DEFPRINT(AST_DotHash, function(self2, output) { + var expr = self2.expression; + expr.print(output); + var prop = self2.property; + if (self2.optional) + output.print("?"); + output.print(".#"); + output.add_mapping(self2.end); + output.print_name(prop); + }); + DEFPRINT(AST_Sub, function(self2, output) { + self2.expression.print(output); + if (self2.optional) + output.print("?."); + output.print("["); + self2.property.print(output); + output.print("]"); + }); + DEFPRINT(AST_Chain, function(self2, output) { + self2.expression.print(output); + }); + DEFPRINT(AST_UnaryPrefix, function(self2, output) { + var op = self2.operator; + if (op === "--" && output.last().endsWith("!")) { + output.print(" "); + } + output.print(op); + if (/^[a-z]/i.test(op) || /[+-]$/.test(op) && self2.expression instanceof AST_UnaryPrefix && /^[+-]/.test(self2.expression.operator)) { + output.space(); + } + self2.expression.print(output); + }); + DEFPRINT(AST_UnaryPostfix, function(self2, output) { + self2.expression.print(output); + output.print(self2.operator); + }); + DEFPRINT(AST_Binary, function(self2, output) { + var op = self2.operator; + self2.left.print(output); + if (op[0] == ">" && output.last().endsWith("--")) { + output.print(" "); + } else { + output.space(); + } + output.print(op); + output.space(); + self2.right.print(output); + }); + DEFPRINT(AST_Conditional, function(self2, output) { + self2.condition.print(output); + output.space(); + output.print("?"); + output.space(); + self2.consequent.print(output); + output.space(); + output.colon(); + self2.alternative.print(output); + }); + DEFPRINT(AST_Array, function(self2, output) { + output.with_square(function() { + var a = self2.elements, len = a.length; + if (len > 0) + output.space(); + a.forEach(function(exp, i) { + if (i) + output.comma(); + exp.print(output); + if (i === len - 1 && exp instanceof AST_Hole) + output.comma(); + }); + if (len > 0) + output.space(); + }); + }); + DEFPRINT(AST_Object, function(self2, output) { + if (self2.properties.length > 0) + output.with_block(function() { + self2.properties.forEach(function(prop, i) { + if (i) { + output.print(","); + output.newline(); + } + output.indent(); + prop.print(output); + }); + output.newline(); + }); + else + print_braced_empty(self2, output); + }); + DEFPRINT(AST_Class, function(self2, output) { + output.print("class"); + output.space(); + if (self2.name) { + self2.name.print(output); + output.space(); + } + if (self2.extends) { + var parens = !(self2.extends instanceof AST_SymbolRef) && !(self2.extends instanceof AST_PropAccess) && !(self2.extends instanceof AST_ClassExpression) && !(self2.extends instanceof AST_Function); + output.print("extends"); + if (parens) { + output.print("("); + } else { + output.space(); + } + self2.extends.print(output); + if (parens) { + output.print(")"); + } else { + output.space(); + } + } + if (self2.properties.length > 0) + output.with_block(function() { + self2.properties.forEach(function(prop, i) { + if (i) { + output.newline(); + } + output.indent(); + prop.print(output); + }); + output.newline(); + }); + else + output.print("{}"); + }); + DEFPRINT(AST_NewTarget, function(self2, output) { + output.print("new.target"); + }); + function print_property_name(key, quote, output) { + if (output.option("quote_keys")) { + output.print_string(key); + return false; + } + if ("" + +key == key && key >= 0) { + if (output.option("keep_numbers")) { + output.print(key); + return false; + } + output.print(make_num(key)); + return false; + } + var print_string = ALL_RESERVED_WORDS.has(key) ? output.option("ie8") : output.option("ecma") < 2015 || output.option("safari10") ? !is_basic_identifier_string(key) : !is_identifier_string(key, true); + if (print_string || quote && output.option("keep_quoted_props")) { + output.print_string(key, quote); + return false; + } + output.print_name(key); + return true; + } + DEFPRINT(AST_ObjectKeyVal, function(self2, output) { + function get_name(self3) { + var def = self3.definition(); + return def ? def.mangled_name || def.name : self3.name; + } + const try_shorthand = output.option("shorthand") && !(self2.key instanceof AST_Node); + if (try_shorthand && self2.value instanceof AST_Symbol && get_name(self2.value) === self2.key && !ALL_RESERVED_WORDS.has(self2.key)) { + const was_shorthand = print_property_name(self2.key, self2.quote, output); + if (!was_shorthand) { + output.colon(); + self2.value.print(output); + } + } else if (try_shorthand && self2.value instanceof AST_DefaultAssign && self2.value.left instanceof AST_Symbol && get_name(self2.value.left) === self2.key) { + const was_shorthand = print_property_name(self2.key, self2.quote, output); + if (!was_shorthand) { + output.colon(); + self2.value.left.print(output); + } + output.space(); + output.print("="); + output.space(); + self2.value.right.print(output); + } else { + if (!(self2.key instanceof AST_Node)) { + print_property_name(self2.key, self2.quote, output); + } else { + output.with_square(function() { + self2.key.print(output); + }); + } + output.colon(); + self2.value.print(output); + } + }); + DEFPRINT(AST_ClassPrivateProperty, (self2, output) => { + if (self2.static) { + output.print("static"); + output.space(); + } + output.print("#"); + print_property_name(self2.key.name, undefined, output); + if (self2.value) { + output.print("="); + self2.value.print(output); + } + output.semicolon(); + }); + DEFPRINT(AST_ClassProperty, (self2, output) => { + if (self2.static) { + output.print("static"); + output.space(); + } + if (self2.key instanceof AST_SymbolClassProperty) { + print_property_name(self2.key.name, self2.quote, output); + } else { + output.print("["); + self2.key.print(output); + output.print("]"); + } + if (self2.value) { + output.print("="); + self2.value.print(output); + } + output.semicolon(); + }); + AST_ObjectProperty.DEFMETHOD("_print_getter_setter", function(type, is_private, output) { + var self2 = this; + if (self2.static) { + output.print("static"); + output.space(); + } + if (type) { + output.print(type); + output.space(); + } + if (self2.key instanceof AST_SymbolMethod) { + if (is_private) + output.print("#"); + print_property_name(self2.key.name, self2.quote, output); + self2.key.add_source_map(output); + } else { + output.with_square(function() { + self2.key.print(output); + }); + } + self2.value._do_print(output, true); + }); + DEFPRINT(AST_ObjectSetter, function(self2, output) { + self2._print_getter_setter("set", false, output); + }); + DEFPRINT(AST_ObjectGetter, function(self2, output) { + self2._print_getter_setter("get", false, output); + }); + DEFPRINT(AST_PrivateSetter, function(self2, output) { + self2._print_getter_setter("set", true, output); + }); + DEFPRINT(AST_PrivateGetter, function(self2, output) { + self2._print_getter_setter("get", true, output); + }); + DEFPRINT(AST_ConciseMethod, function(self2, output) { + var type; + if (self2.value.is_generator && self2.value.async) { + type = "async*"; + } else if (self2.value.is_generator) { + type = "*"; + } else if (self2.value.async) { + type = "async"; + } + self2._print_getter_setter(type, false, output); + }); + DEFPRINT(AST_PrivateMethod, function(self2, output) { + var type; + if (self2.value.is_generator && self2.value.async) { + type = "async*"; + } else if (self2.value.is_generator) { + type = "*"; + } else if (self2.value.async) { + type = "async"; + } + self2._print_getter_setter(type, true, output); + }); + DEFPRINT(AST_PrivateIn, function(self2, output) { + self2.key.print(output); + output.space(); + output.print("in"); + output.space(); + self2.value.print(output); + }); + DEFPRINT(AST_SymbolPrivateProperty, function(self2, output) { + output.print("#" + self2.name); + }); + DEFPRINT(AST_ClassStaticBlock, function(self2, output) { + output.print("static"); + output.space(); + print_braced(self2, output); + }); + AST_Symbol.DEFMETHOD("_do_print", function(output) { + var def = this.definition(); + output.print_name(def ? def.mangled_name || def.name : this.name); + }); + DEFPRINT(AST_Symbol, function(self2, output) { + self2._do_print(output); + }); + DEFPRINT(AST_Hole, noop); + DEFPRINT(AST_This, function(self2, output) { + output.print("this"); + }); + DEFPRINT(AST_Super, function(self2, output) { + output.print("super"); + }); + DEFPRINT(AST_Constant, function(self2, output) { + output.print(self2.getValue()); + }); + DEFPRINT(AST_String, function(self2, output) { + output.print_string(self2.getValue(), self2.quote, output.in_directive); + }); + DEFPRINT(AST_Number, function(self2, output) { + if ((output.option("keep_numbers") || output.use_asm) && self2.raw) { + output.print(self2.raw); + } else { + output.print(make_num(self2.getValue())); + } + }); + DEFPRINT(AST_BigInt, function(self2, output) { + if (output.option("keep_numbers") && self2.raw) { + output.print(self2.raw); + } else { + output.print(self2.getValue() + "n"); + } + }); + const r_slash_script = /(<\s*\/\s*script)/i; + const r_starts_with_script = /^\s*script/i; + const slash_script_replace = (_, $1) => $1.replace("/", "\\/"); + DEFPRINT(AST_RegExp, function(self2, output) { + let { source, flags } = self2.getValue(); + source = regexp_source_fix(source); + flags = flags ? sort_regexp_flags(flags) : ""; + source = source.replace(r_slash_script, slash_script_replace); + if (r_starts_with_script.test(source) && output.last().endsWith("<")) { + output.print(" "); + } + output.print(output.to_utf8(`/${source}/${flags}`, false, true)); + const parent = output.parent(); + if (parent instanceof AST_Binary && /^\w/.test(parent.operator) && parent.left === self2) { + output.print(" "); + } + }); + function print_maybe_braced_body(stat, output) { + if (output.option("braces")) { + make_block(stat, output); + } else { + if (!stat || stat instanceof AST_EmptyStatement) + output.force_semicolon(); + else if (stat instanceof AST_Let || stat instanceof AST_Const || stat instanceof AST_Class) + make_block(stat, output); + else + stat.print(output); + } + } + function best_of(a) { + var best = a[0], len = best.length; + for (var i = 1;i < a.length; ++i) { + if (a[i].length < len) { + best = a[i]; + len = best.length; + } + } + return best; + } + function make_num(num) { + var str = num.toString(10).replace(/^0\./, ".").replace("e+", "e"); + var candidates = [str]; + if (Math.floor(num) === num) { + if (num < 0) { + candidates.push("-0x" + (-num).toString(16).toLowerCase()); + } else { + candidates.push("0x" + num.toString(16).toLowerCase()); + } + } + var match, len, digits; + if (match = /^\.0+/.exec(str)) { + len = match[0].length; + digits = str.slice(len); + candidates.push(digits + "e-" + (digits.length + len - 1)); + } else if (match = /0+$/.exec(str)) { + len = match[0].length; + candidates.push(str.slice(0, -len) + "e" + len); + } else if (match = /^(\d)\.(\d+)e(-?\d+)$/.exec(str)) { + candidates.push(match[1] + match[2] + "e" + (match[3] - match[2].length)); + } + return best_of(candidates); + } + function make_block(stmt, output) { + if (!stmt || stmt instanceof AST_EmptyStatement) + output.print("{}"); + else if (stmt instanceof AST_BlockStatement) + stmt.print(output); + else + output.with_block(function() { + output.indent(); + stmt.print(output); + output.newline(); + }); + } + function DEFMAP(nodetype, generator) { + nodetype.forEach(function(nodetype2) { + nodetype2.DEFMETHOD("add_source_map", generator); + }); + } + DEFMAP([ + AST_Node, + AST_LabeledStatement, + AST_Toplevel + ], noop); + DEFMAP([ + AST_Array, + AST_BlockStatement, + AST_Catch, + AST_Class, + AST_Constant, + AST_Debugger, + AST_Definitions, + AST_Directive, + AST_Finally, + AST_Jump, + AST_Lambda, + AST_New, + AST_Object, + AST_StatementWithBody, + AST_Symbol, + AST_Switch, + AST_SwitchBranch, + AST_TemplateString, + AST_TemplateSegment, + AST_Try + ], function(output) { + output.add_mapping(this.start); + }); + DEFMAP([ + AST_ObjectGetter, + AST_ObjectSetter, + AST_PrivateGetter, + AST_PrivateSetter, + AST_ConciseMethod, + AST_PrivateMethod + ], function(output) { + output.add_mapping(this.start, false); + }); + DEFMAP([ + AST_SymbolMethod, + AST_SymbolPrivateProperty + ], function(output) { + const tok_type = this.end && this.end.type; + if (tok_type === "name" || tok_type === "privatename") { + output.add_mapping(this.end, this.name); + } else { + output.add_mapping(this.end); + } + }); + DEFMAP([AST_ObjectProperty], function(output) { + output.add_mapping(this.start, this.key); + }); +})(); + +// node_modules/terser/lib/equivalent-to.js +var shallow_cmp = (node1, node2) => { + return node1 === null && node2 === null || node1.TYPE === node2.TYPE && node1.shallow_cmp(node2); +}; +var equivalent_to = (tree1, tree2) => { + if (!shallow_cmp(tree1, tree2)) + return false; + const walk_1_state = [tree1]; + const walk_2_state = [tree2]; + const walk_1_push = walk_1_state.push.bind(walk_1_state); + const walk_2_push = walk_2_state.push.bind(walk_2_state); + while (walk_1_state.length && walk_2_state.length) { + const node_1 = walk_1_state.pop(); + const node_2 = walk_2_state.pop(); + if (!shallow_cmp(node_1, node_2)) + return false; + node_1._children_backwards(walk_1_push); + node_2._children_backwards(walk_2_push); + if (walk_1_state.length !== walk_2_state.length) { + return false; + } + } + return walk_1_state.length == 0 && walk_2_state.length == 0; +}; +var pass_through = () => true; +AST_Node.prototype.shallow_cmp = function() { + throw new Error("did not find a shallow_cmp function for " + this.constructor.name); +}; +AST_Debugger.prototype.shallow_cmp = pass_through; +AST_Directive.prototype.shallow_cmp = function(other) { + return this.value === other.value; +}; +AST_SimpleStatement.prototype.shallow_cmp = pass_through; +AST_Block.prototype.shallow_cmp = pass_through; +AST_EmptyStatement.prototype.shallow_cmp = pass_through; +AST_LabeledStatement.prototype.shallow_cmp = function(other) { + return this.label.name === other.label.name; +}; +AST_Do.prototype.shallow_cmp = pass_through; +AST_While.prototype.shallow_cmp = pass_through; +AST_For.prototype.shallow_cmp = function(other) { + return (this.init == null ? other.init == null : this.init === other.init) && (this.condition == null ? other.condition == null : this.condition === other.condition) && (this.step == null ? other.step == null : this.step === other.step); +}; +AST_ForIn.prototype.shallow_cmp = pass_through; +AST_ForOf.prototype.shallow_cmp = pass_through; +AST_With.prototype.shallow_cmp = pass_through; +AST_Toplevel.prototype.shallow_cmp = pass_through; +AST_Expansion.prototype.shallow_cmp = pass_through; +AST_Lambda.prototype.shallow_cmp = function(other) { + return this.is_generator === other.is_generator && this.async === other.async; +}; +AST_Destructuring.prototype.shallow_cmp = function(other) { + return this.is_array === other.is_array; +}; +AST_PrefixedTemplateString.prototype.shallow_cmp = pass_through; +AST_TemplateString.prototype.shallow_cmp = pass_through; +AST_TemplateSegment.prototype.shallow_cmp = function(other) { + return this.value === other.value; +}; +AST_Jump.prototype.shallow_cmp = pass_through; +AST_LoopControl.prototype.shallow_cmp = pass_through; +AST_Await.prototype.shallow_cmp = pass_through; +AST_Yield.prototype.shallow_cmp = function(other) { + return this.is_star === other.is_star; +}; +AST_If.prototype.shallow_cmp = function(other) { + return this.alternative == null ? other.alternative == null : this.alternative === other.alternative; +}; +AST_Switch.prototype.shallow_cmp = pass_through; +AST_SwitchBranch.prototype.shallow_cmp = pass_through; +AST_Try.prototype.shallow_cmp = function(other) { + return this.body === other.body && (this.bcatch == null ? other.bcatch == null : this.bcatch === other.bcatch) && (this.bfinally == null ? other.bfinally == null : this.bfinally === other.bfinally); +}; +AST_Catch.prototype.shallow_cmp = function(other) { + return this.argname == null ? other.argname == null : this.argname === other.argname; +}; +AST_Finally.prototype.shallow_cmp = pass_through; +AST_Definitions.prototype.shallow_cmp = pass_through; +AST_VarDef.prototype.shallow_cmp = function(other) { + return this.value == null ? other.value == null : this.value === other.value; +}; +AST_NameMapping.prototype.shallow_cmp = pass_through; +AST_Import.prototype.shallow_cmp = function(other) { + return (this.imported_name == null ? other.imported_name == null : this.imported_name === other.imported_name) && (this.imported_names == null ? other.imported_names == null : this.imported_names === other.imported_names) && (this.attributes == null ? other.attributes == null : this.attributes === other.attributes); +}; +AST_ImportMeta.prototype.shallow_cmp = pass_through; +AST_Export.prototype.shallow_cmp = function(other) { + return (this.exported_definition == null ? other.exported_definition == null : this.exported_definition === other.exported_definition) && (this.exported_value == null ? other.exported_value == null : this.exported_value === other.exported_value) && (this.exported_names == null ? other.exported_names == null : this.exported_names === other.exported_names) && (this.attributes == null ? other.attributes == null : this.attributes === other.attributes) && this.module_name === other.module_name && this.is_default === other.is_default; +}; +AST_Call.prototype.shallow_cmp = pass_through; +AST_Sequence.prototype.shallow_cmp = pass_through; +AST_PropAccess.prototype.shallow_cmp = pass_through; +AST_Chain.prototype.shallow_cmp = pass_through; +AST_Dot.prototype.shallow_cmp = function(other) { + return this.property === other.property; +}; +AST_DotHash.prototype.shallow_cmp = function(other) { + return this.property === other.property; +}; +AST_Unary.prototype.shallow_cmp = function(other) { + return this.operator === other.operator; +}; +AST_Binary.prototype.shallow_cmp = function(other) { + return this.operator === other.operator; +}; +AST_PrivateIn.prototype.shallow_cmp = pass_through; +AST_Conditional.prototype.shallow_cmp = pass_through; +AST_Array.prototype.shallow_cmp = pass_through; +AST_Object.prototype.shallow_cmp = pass_through; +AST_ObjectProperty.prototype.shallow_cmp = pass_through; +AST_ObjectKeyVal.prototype.shallow_cmp = function(other) { + return this.key === other.key && this.quote === other.quote; +}; +AST_ObjectSetter.prototype.shallow_cmp = function(other) { + return this.static === other.static; +}; +AST_ObjectGetter.prototype.shallow_cmp = function(other) { + return this.static === other.static; +}; +AST_ConciseMethod.prototype.shallow_cmp = function(other) { + return this.static === other.static; +}; +AST_PrivateMethod.prototype.shallow_cmp = function(other) { + return this.static === other.static; +}; +AST_Class.prototype.shallow_cmp = function(other) { + return (this.name == null ? other.name == null : this.name === other.name) && (this.extends == null ? other.extends == null : this.extends === other.extends); +}; +AST_ClassProperty.prototype.shallow_cmp = function(other) { + return this.static === other.static && (typeof this.key === "string" ? this.key === other.key : true); +}; +AST_ClassPrivateProperty.prototype.shallow_cmp = function(other) { + return this.static === other.static; +}; +AST_Symbol.prototype.shallow_cmp = function(other) { + return this.name === other.name; +}; +AST_NewTarget.prototype.shallow_cmp = pass_through; +AST_This.prototype.shallow_cmp = pass_through; +AST_Super.prototype.shallow_cmp = pass_through; +AST_String.prototype.shallow_cmp = function(other) { + return this.value === other.value; +}; +AST_Number.prototype.shallow_cmp = function(other) { + return this.value === other.value; +}; +AST_BigInt.prototype.shallow_cmp = function(other) { + return this.value === other.value; +}; +AST_RegExp.prototype.shallow_cmp = function(other) { + return this.value.flags === other.value.flags && this.value.source === other.value.source; +}; +AST_Atom.prototype.shallow_cmp = pass_through; + +// node_modules/terser/lib/scope.js +var MASK_EXPORT_DONT_MANGLE = 1 << 0; +var MASK_EXPORT_WANT_MANGLE = 1 << 1; +var function_defs = null; +var unmangleable_names = null; +var scopes_with_block_defuns = null; + +class SymbolDef { + constructor(scope, orig, init) { + this.name = orig.name; + this.orig = [orig]; + this.init = init; + this.eliminated = 0; + this.assignments = 0; + this.scope = scope; + this.replaced = 0; + this.global = false; + this.export = 0; + this.mangled_name = null; + this.undeclared = false; + this.id = SymbolDef.next_id++; + this.chained = false; + this.direct_access = false; + this.escaped = 0; + this.recursive_refs = 0; + this.references = []; + this.should_replace = undefined; + this.single_use = false; + this.fixed = false; + Object.seal(this); + } + fixed_value() { + if (!this.fixed || this.fixed instanceof AST_Node) + return this.fixed; + return this.fixed(); + } + unmangleable(options) { + if (!options) + options = {}; + if (function_defs && function_defs.has(this.id) && keep_name(options.keep_fnames, this.orig[0].name)) + return true; + return this.global && !options.toplevel || this.export & MASK_EXPORT_DONT_MANGLE || this.undeclared || !options.eval && this.scope.pinned() || (this.orig[0] instanceof AST_SymbolLambda || this.orig[0] instanceof AST_SymbolDefun) && keep_name(options.keep_fnames, this.orig[0].name) || this.orig[0] instanceof AST_SymbolMethod || (this.orig[0] instanceof AST_SymbolClass || this.orig[0] instanceof AST_SymbolDefClass) && keep_name(options.keep_classnames, this.orig[0].name); + } + mangle(options) { + const cache = options.cache && options.cache.props; + if (this.global && cache && cache.has(this.name)) { + this.mangled_name = cache.get(this.name); + } else if (!this.mangled_name && !this.unmangleable(options)) { + var s = this.scope; + var sym = this.orig[0]; + if (options.ie8 && sym instanceof AST_SymbolLambda) + s = s.parent_scope; + const redefinition = redefined_catch_def(this); + this.mangled_name = redefinition ? redefinition.mangled_name || redefinition.name : s.next_mangled(options, this); + if (this.global && cache) { + cache.set(this.name, this.mangled_name); + } + } + } +} +SymbolDef.next_id = 1; +function redefined_catch_def(def) { + if (def.orig[0] instanceof AST_SymbolCatch && def.scope.is_block_scope()) { + return def.scope.get_defun_scope().variables.get(def.name); + } +} +AST_Scope.DEFMETHOD("figure_out_scope", function(options, { parent_scope = undefined, toplevel = this } = {}) { + options = defaults(options, { + cache: null, + ie8: false, + safari10: false, + module: false + }); + if (!(toplevel instanceof AST_Toplevel)) { + throw new Error("Invalid toplevel scope"); + } + var scope = this.parent_scope = parent_scope; + var labels = new Map; + var defun = null; + var in_destructuring = null; + var for_scopes = []; + var tw = new TreeWalker((node, descend) => { + if (node.is_block_scope()) { + const save_scope2 = scope; + node.block_scope = scope = new AST_Scope(node); + scope._block_scope = true; + scope.init_scope_vars(save_scope2); + scope.uses_with = save_scope2.uses_with; + scope.uses_eval = save_scope2.uses_eval; + if (options.safari10) { + if (node instanceof AST_For || node instanceof AST_ForIn || node instanceof AST_ForOf) { + for_scopes.push(scope); + } + } + if (node instanceof AST_Switch) { + const the_block_scope = scope; + scope = save_scope2; + node.expression.walk(tw); + scope = the_block_scope; + for (let i = 0;i < node.body.length; i++) { + node.body[i].walk(tw); + } + } else { + descend(); + } + scope = save_scope2; + return true; + } + if (node instanceof AST_Destructuring) { + const save_destructuring = in_destructuring; + in_destructuring = node; + descend(); + in_destructuring = save_destructuring; + return true; + } + if (node instanceof AST_Scope) { + node.init_scope_vars(scope); + var save_scope = scope; + var save_defun = defun; + var save_labels = labels; + defun = scope = node; + labels = new Map; + descend(); + scope = save_scope; + defun = save_defun; + labels = save_labels; + return true; + } + if (node instanceof AST_LabeledStatement) { + var l = node.label; + if (labels.has(l.name)) { + throw new Error(string_template("Label {name} defined twice", l)); + } + labels.set(l.name, l); + descend(); + labels.delete(l.name); + return true; + } + if (node instanceof AST_With) { + for (var s = scope;s; s = s.parent_scope) + s.uses_with = true; + return; + } + if (node instanceof AST_Symbol) { + node.scope = scope; + } + if (node instanceof AST_Label) { + node.thedef = node; + node.references = []; + } + if (node instanceof AST_SymbolLambda) { + defun.def_function(node, node.name == "arguments" ? undefined : defun); + } else if (node instanceof AST_SymbolDefun) { + const closest_scope = defun.parent_scope; + node.scope = tw.directives["use strict"] ? closest_scope : closest_scope.get_defun_scope(); + mark_export(node.scope.def_function(node, defun), 1); + } else if (node instanceof AST_SymbolClass) { + mark_export(defun.def_variable(node, defun), 1); + } else if (node instanceof AST_SymbolImport) { + scope.def_variable(node); + } else if (node instanceof AST_SymbolDefClass) { + mark_export((node.scope = defun.parent_scope).def_function(node, defun), 1); + } else if (node instanceof AST_SymbolVar || node instanceof AST_SymbolLet || node instanceof AST_SymbolConst || node instanceof AST_SymbolCatch) { + var def; + if (node instanceof AST_SymbolBlockDeclaration) { + def = scope.def_variable(node, null); + } else { + def = defun.def_variable(node, node.TYPE == "SymbolVar" ? null : undefined); + } + if (!def.orig.every((sym2) => { + if (sym2 === node) + return true; + if (node instanceof AST_SymbolBlockDeclaration) { + return sym2 instanceof AST_SymbolLambda; + } + return !(sym2 instanceof AST_SymbolLet || sym2 instanceof AST_SymbolConst); + })) { + js_error(`"${node.name}" is redeclared`, node.start.file, node.start.line, node.start.col, node.start.pos); + } + if (!(node instanceof AST_SymbolFunarg)) + mark_export(def, 2); + if (defun !== scope) { + node.mark_enclosed(); + var def = scope.find_variable(node); + if (node.thedef !== def) { + node.thedef = def; + node.reference(); + } + } + } else if (node instanceof AST_LabelRef) { + var sym = labels.get(node.name); + if (!sym) + throw new Error(string_template("Undefined label {name} [{line},{col}]", { + name: node.name, + line: node.start.line, + col: node.start.col + })); + node.thedef = sym; + } + if (!(scope instanceof AST_Toplevel) && (node instanceof AST_Export || node instanceof AST_Import)) { + js_error(`"${node.TYPE}" statement may only appear at the top level`, node.start.file, node.start.line, node.start.col, node.start.pos); + } + }); + if (options.module) { + tw.directives["use strict"] = true; + } + this.walk(tw); + function mark_export(def, level) { + if (in_destructuring) { + var i = 0; + do { + level++; + } while (tw.parent(i++) !== in_destructuring); + } + var node = tw.parent(level); + if (def.export = node instanceof AST_Export ? MASK_EXPORT_DONT_MANGLE : 0) { + var exported = node.exported_definition; + if ((exported instanceof AST_Defun || exported instanceof AST_DefClass) && node.is_default) { + def.export = MASK_EXPORT_WANT_MANGLE; + } + } + } + const is_toplevel = this instanceof AST_Toplevel; + if (is_toplevel) { + this.globals = new Map; + } + var tw = new TreeWalker((node) => { + if (node instanceof AST_LoopControl && node.label) { + node.label.thedef.references.push(node); + return true; + } + if (node instanceof AST_SymbolRef) { + var name = node.name; + if (name == "eval" && tw.parent() instanceof AST_Call) { + for (var s = node.scope;s && !s.uses_eval; s = s.parent_scope) { + s.uses_eval = true; + } + } + var sym; + if (tw.parent() instanceof AST_NameMapping && tw.parent(1).module_name || !(sym = node.scope.find_variable(name))) { + sym = toplevel.def_global(node); + if (node instanceof AST_SymbolExport) + sym.export = MASK_EXPORT_DONT_MANGLE; + } else if (sym.scope instanceof AST_Lambda && name == "arguments") { + sym.scope.get_defun_scope().uses_arguments = true; + } + node.thedef = sym; + node.reference(); + if (node.scope.is_block_scope() && !(sym.orig[0] instanceof AST_SymbolBlockDeclaration)) { + node.scope = node.scope.get_defun_scope(); + } + return true; + } + var def; + if (node instanceof AST_SymbolCatch && (def = redefined_catch_def(node.definition()))) { + var s = node.scope; + while (s) { + push_uniq(s.enclosed, def); + if (s === def.scope) + break; + s = s.parent_scope; + } + } + }); + this.walk(tw); + if (options.ie8 || options.safari10) { + walk(this, (node) => { + if (node instanceof AST_SymbolCatch) { + var name = node.name; + var refs = node.thedef.references; + var scope2 = node.scope.get_defun_scope(); + var def = scope2.find_variable(name) || toplevel.globals.get(name) || scope2.def_variable(node); + refs.forEach(function(ref) { + ref.thedef = def; + ref.reference(); + }); + node.thedef = def; + node.reference(); + return true; + } + }); + } + if (options.safari10) { + for (const scope2 of for_scopes) { + scope2.parent_scope.variables.forEach(function(def) { + push_uniq(scope2.enclosed, def); + }); + } + } +}); +AST_Toplevel.DEFMETHOD("def_global", function(node) { + var globals = this.globals, name = node.name; + if (globals.has(name)) { + return globals.get(name); + } else { + var g = new SymbolDef(this, node); + g.undeclared = true; + g.global = true; + globals.set(name, g); + return g; + } +}); +AST_Scope.DEFMETHOD("init_scope_vars", function(parent_scope) { + this.variables = new Map; + this.uses_with = false; + this.uses_eval = false; + this.parent_scope = parent_scope; + this.enclosed = []; + this.cname = -1; +}); +AST_Scope.DEFMETHOD("conflicting_def", function(name) { + return this.enclosed.find((def) => def.name === name) || this.variables.has(name) || this.parent_scope && this.parent_scope.conflicting_def(name); +}); +AST_Scope.DEFMETHOD("conflicting_def_shallow", function(name) { + return this.enclosed.find((def) => def.name === name) || this.variables.has(name); +}); +AST_Scope.DEFMETHOD("add_child_scope", function(scope) { + if (scope.parent_scope === this) + return; + scope.parent_scope = this; + if (scope instanceof AST_Arrow && (this instanceof AST_Lambda && !this.uses_arguments)) { + this.uses_arguments = walk(scope, (node) => { + if (node instanceof AST_SymbolRef && node.scope instanceof AST_Lambda && node.name === "arguments") { + return walk_abort; + } + if (node instanceof AST_Lambda && !(node instanceof AST_Arrow)) { + return true; + } + }); + } + this.uses_with = this.uses_with || scope.uses_with; + this.uses_eval = this.uses_eval || scope.uses_eval; + const scope_ancestry = (() => { + const ancestry = []; + let cur = this; + do { + ancestry.push(cur); + } while (cur = cur.parent_scope); + ancestry.reverse(); + return ancestry; + })(); + const new_scope_enclosed_set = new Set(scope.enclosed); + const to_enclose = []; + for (const scope_topdown of scope_ancestry) { + to_enclose.forEach((e) => push_uniq(scope_topdown.enclosed, e)); + for (const def of scope_topdown.variables.values()) { + if (new_scope_enclosed_set.has(def)) { + push_uniq(to_enclose, def); + push_uniq(scope_topdown.enclosed, def); + } + } + } +}); +function find_scopes_visible_from(scopes) { + const found_scopes = new Set; + for (const scope of new Set(scopes)) { + (function bubble_up(scope2) { + if (scope2 == null || found_scopes.has(scope2)) + return; + found_scopes.add(scope2); + bubble_up(scope2.parent_scope); + })(scope); + } + return [...found_scopes]; +} +AST_Scope.DEFMETHOD("create_symbol", function(SymClass, { + source, + tentative_name, + scope, + conflict_scopes = [scope], + init = null +} = {}) { + let symbol_name; + conflict_scopes = find_scopes_visible_from(conflict_scopes); + if (tentative_name) { + tentative_name = symbol_name = tentative_name.replace(/(?:^[^a-z_$]|[^a-z0-9_$])/ig, "_"); + let i = 0; + while (conflict_scopes.find((s) => s.conflicting_def_shallow(symbol_name))) { + symbol_name = tentative_name + "$" + i++; + } + } + if (!symbol_name) { + throw new Error("No symbol name could be generated in create_symbol()"); + } + const symbol = make_node(SymClass, source, { + name: symbol_name, + scope + }); + this.def_variable(symbol, init || null); + symbol.mark_enclosed(); + return symbol; +}); +AST_Node.DEFMETHOD("is_block_scope", return_false); +AST_Class.DEFMETHOD("is_block_scope", return_false); +AST_Lambda.DEFMETHOD("is_block_scope", return_false); +AST_Toplevel.DEFMETHOD("is_block_scope", return_false); +AST_SwitchBranch.DEFMETHOD("is_block_scope", return_false); +AST_Block.DEFMETHOD("is_block_scope", return_true); +AST_Scope.DEFMETHOD("is_block_scope", function() { + return this._block_scope || false; +}); +AST_IterationStatement.DEFMETHOD("is_block_scope", return_true); +AST_Lambda.DEFMETHOD("init_scope_vars", function() { + AST_Scope.prototype.init_scope_vars.apply(this, arguments); + this.uses_arguments = false; + this.def_variable(new AST_SymbolFunarg({ + name: "arguments", + start: this.start, + end: this.end + })); +}); +AST_Arrow.DEFMETHOD("init_scope_vars", function() { + AST_Scope.prototype.init_scope_vars.apply(this, arguments); + this.uses_arguments = false; +}); +AST_Symbol.DEFMETHOD("mark_enclosed", function() { + var def = this.definition(); + var s = this.scope; + while (s) { + push_uniq(s.enclosed, def); + if (s === def.scope) + break; + s = s.parent_scope; + } +}); +AST_Symbol.DEFMETHOD("reference", function() { + this.definition().references.push(this); + this.mark_enclosed(); +}); +AST_Scope.DEFMETHOD("find_variable", function(name) { + if (name instanceof AST_Symbol) + name = name.name; + return this.variables.get(name) || this.parent_scope && this.parent_scope.find_variable(name); +}); +AST_Scope.DEFMETHOD("def_function", function(symbol, init) { + var def = this.def_variable(symbol, init); + if (!def.init || def.init instanceof AST_Defun) + def.init = init; + return def; +}); +AST_Scope.DEFMETHOD("def_variable", function(symbol, init) { + var def = this.variables.get(symbol.name); + if (def) { + def.orig.push(symbol); + if (def.init && (def.scope !== symbol.scope || def.init instanceof AST_Function)) { + def.init = init; + } + } else { + def = new SymbolDef(this, symbol, init); + this.variables.set(symbol.name, def); + def.global = !this.parent_scope; + } + return symbol.thedef = def; +}); +function next_mangled(scope, options) { + let defun_scope; + if (scopes_with_block_defuns && (defun_scope = scope.get_defun_scope()) && scopes_with_block_defuns.has(defun_scope)) { + scope = defun_scope; + } + var ext = scope.enclosed; + var nth_identifier = options.nth_identifier; + out: + while (true) { + var m = nth_identifier.get(++scope.cname); + if (ALL_RESERVED_WORDS.has(m)) + continue; + if (options.reserved.has(m)) + continue; + if (unmangleable_names && unmangleable_names.has(m)) + continue out; + for (let i = ext.length;--i >= 0; ) { + const def = ext[i]; + const name = def.mangled_name || def.unmangleable(options) && def.name; + if (m == name) + continue out; + } + return m; + } +} +AST_Scope.DEFMETHOD("next_mangled", function(options) { + return next_mangled(this, options); +}); +AST_Toplevel.DEFMETHOD("next_mangled", function(options) { + let name; + const mangled_names = this.mangled_names; + do { + name = next_mangled(this, options); + } while (mangled_names.has(name)); + return name; +}); +AST_Function.DEFMETHOD("next_mangled", function(options, def) { + var tricky_def = def.orig[0] instanceof AST_SymbolFunarg && this.name && this.name.definition(); + var tricky_name = tricky_def ? tricky_def.mangled_name || tricky_def.name : null; + while (true) { + var name = next_mangled(this, options); + if (!tricky_name || tricky_name != name) + return name; + } +}); +AST_Symbol.DEFMETHOD("unmangleable", function(options) { + var def = this.definition(); + return !def || def.unmangleable(options); +}); +AST_Label.DEFMETHOD("unmangleable", return_false); +AST_Symbol.DEFMETHOD("unreferenced", function() { + return !this.definition().references.length && !this.scope.pinned(); +}); +AST_Symbol.DEFMETHOD("definition", function() { + return this.thedef; +}); +AST_Symbol.DEFMETHOD("global", function() { + return this.thedef.global; +}); +function format_mangler_options(options) { + options = defaults(options, { + eval: false, + nth_identifier: base54, + ie8: false, + keep_classnames: false, + keep_fnames: false, + module: false, + reserved: [], + toplevel: false + }); + if (options.module) + options.toplevel = true; + if (!Array.isArray(options.reserved) && !(options.reserved instanceof Set)) { + options.reserved = []; + } + options.reserved = new Set(options.reserved); + options.reserved.add("arguments"); + return options; +} +AST_Toplevel.DEFMETHOD("mangle_names", function(options) { + options = format_mangler_options(options); + var nth_identifier = options.nth_identifier; + var lname = -1; + var to_mangle = []; + if (options.keep_fnames) { + function_defs = new Set; + } + const mangled_names = this.mangled_names = new Set; + unmangleable_names = new Set; + if (options.cache) { + this.globals.forEach(collect); + if (options.cache.props) { + options.cache.props.forEach(function(mangled_name) { + mangled_names.add(mangled_name); + }); + } + } + var tw = new TreeWalker(function(node, descend) { + if (node instanceof AST_LabeledStatement) { + var save_nesting = lname; + descend(); + lname = save_nesting; + return true; + } + if (node instanceof AST_Defun && !(tw.parent() instanceof AST_Scope)) { + scopes_with_block_defuns = scopes_with_block_defuns || new Set; + scopes_with_block_defuns.add(node.parent_scope.get_defun_scope()); + } + if (node instanceof AST_Scope) { + node.variables.forEach(collect); + return; + } + if (node.is_block_scope()) { + node.block_scope.variables.forEach(collect); + return; + } + if (function_defs && node instanceof AST_VarDef && node.value instanceof AST_Lambda && !node.value.name && keep_name(options.keep_fnames, node.name.name)) { + function_defs.add(node.name.definition().id); + return; + } + if (node instanceof AST_Label) { + let name; + do { + name = nth_identifier.get(++lname); + } while (ALL_RESERVED_WORDS.has(name)); + node.mangled_name = name; + return true; + } + if (!(options.ie8 || options.safari10) && node instanceof AST_SymbolCatch) { + to_mangle.push(node.definition()); + return; + } + }); + this.walk(tw); + if (options.keep_fnames || options.keep_classnames) { + to_mangle.forEach((def) => { + if (def.name.length < 6 && def.unmangleable(options)) { + unmangleable_names.add(def.name); + } + }); + } + to_mangle.forEach((def) => { + def.mangle(options); + }); + function_defs = null; + unmangleable_names = null; + scopes_with_block_defuns = null; + function collect(symbol) { + if (symbol.export & MASK_EXPORT_DONT_MANGLE) { + unmangleable_names.add(symbol.name); + } else if (!options.reserved.has(symbol.name)) { + to_mangle.push(symbol); + } + } +}); +AST_Toplevel.DEFMETHOD("find_colliding_names", function(options) { + const cache = options.cache && options.cache.props; + const avoid = new Set; + options.reserved.forEach(to_avoid); + this.globals.forEach(add_def); + this.walk(new TreeWalker(function(node) { + if (node instanceof AST_Scope) + node.variables.forEach(add_def); + if (node instanceof AST_SymbolCatch) + add_def(node.definition()); + })); + return avoid; + function to_avoid(name) { + avoid.add(name); + } + function add_def(def) { + var name = def.name; + if (def.global && cache && cache.has(name)) + name = cache.get(name); + else if (!def.unmangleable(options)) + return; + to_avoid(name); + } +}); +AST_Toplevel.DEFMETHOD("expand_names", function(options) { + options = format_mangler_options(options); + var nth_identifier = options.nth_identifier; + if (nth_identifier.reset && nth_identifier.sort) { + nth_identifier.reset(); + nth_identifier.sort(); + } + var avoid = this.find_colliding_names(options); + var cname = 0; + this.globals.forEach(rename); + this.walk(new TreeWalker(function(node) { + if (node instanceof AST_Scope) + node.variables.forEach(rename); + if (node instanceof AST_SymbolCatch) + rename(node.definition()); + })); + function next_name() { + var name; + do { + name = nth_identifier.get(cname++); + } while (avoid.has(name) || ALL_RESERVED_WORDS.has(name)); + return name; + } + function rename(def) { + if (def.global && options.cache) + return; + if (def.unmangleable(options)) + return; + if (options.reserved.has(def.name)) + return; + const redefinition = redefined_catch_def(def); + const name = def.name = redefinition ? redefinition.name : next_name(); + def.orig.forEach(function(sym) { + sym.name = name; + }); + def.references.forEach(function(sym) { + sym.name = name; + }); + } +}); +AST_Node.DEFMETHOD("tail_node", return_this); +AST_Sequence.DEFMETHOD("tail_node", function() { + return this.expressions[this.expressions.length - 1]; +}); +AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options) { + options = format_mangler_options(options); + var nth_identifier = options.nth_identifier; + if (!nth_identifier.reset || !nth_identifier.consider || !nth_identifier.sort) { + return; + } + nth_identifier.reset(); + try { + AST_Node.prototype.print = function(stream, force_parens) { + this._print(stream, force_parens); + if (this instanceof AST_Symbol && !this.unmangleable(options)) { + nth_identifier.consider(this.name, -1); + } else if (options.properties) { + if (this instanceof AST_DotHash) { + nth_identifier.consider("#" + this.property, -1); + } else if (this instanceof AST_Dot) { + nth_identifier.consider(this.property, -1); + } else if (this instanceof AST_Sub) { + skip_string(this.property); + } + } + }; + nth_identifier.consider(this.print_to_string(), 1); + } finally { + AST_Node.prototype.print = AST_Node.prototype._print; + } + nth_identifier.sort(); + function skip_string(node) { + if (node instanceof AST_String) { + nth_identifier.consider(node.value, -1); + } else if (node instanceof AST_Conditional) { + skip_string(node.consequent); + skip_string(node.alternative); + } else if (node instanceof AST_Sequence) { + skip_string(node.tail_node()); + } + } +}); +var base54 = (() => { + const leading = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_".split(""); + const digits = "0123456789".split(""); + let chars; + let frequency; + function reset() { + frequency = new Map; + leading.forEach(function(ch) { + frequency.set(ch, 0); + }); + digits.forEach(function(ch) { + frequency.set(ch, 0); + }); + } + function consider(str, delta) { + for (var i = str.length;--i >= 0; ) { + frequency.set(str[i], frequency.get(str[i]) + delta); + } + } + function compare(a, b) { + return frequency.get(b) - frequency.get(a); + } + function sort() { + chars = mergeSort(leading, compare).concat(mergeSort(digits, compare)); + } + reset(); + sort(); + function base542(num) { + var ret = "", base = 54; + num++; + do { + num--; + ret += chars[num % base]; + num = Math.floor(num / base); + base = 64; + } while (num > 0); + return ret; + } + return { + get: base542, + consider, + reset, + sort + }; +})(); + +// node_modules/terser/lib/size.js +var mangle_options = undefined; +AST_Node.prototype.size = function(compressor, stack) { + mangle_options = compressor && compressor._mangle_options; + let size = 0; + walk_parent(this, (node, info) => { + size += node._size(info); + if (node instanceof AST_Arrow && node.is_braceless()) { + size += node.body[0].value._size(info); + return true; + } + }, stack || compressor && compressor.stack); + mangle_options = undefined; + return size; +}; +AST_Node.prototype._size = () => 0; +AST_Debugger.prototype._size = () => 8; +AST_Directive.prototype._size = function() { + return 2 + this.value.length; +}; +var list_overhead = (array) => array.length && array.length - 1; +AST_Block.prototype._size = function() { + return 2 + list_overhead(this.body); +}; +AST_Toplevel.prototype._size = function() { + return list_overhead(this.body); +}; +AST_EmptyStatement.prototype._size = () => 1; +AST_LabeledStatement.prototype._size = () => 2; +AST_Do.prototype._size = () => 9; +AST_While.prototype._size = () => 7; +AST_For.prototype._size = () => 8; +AST_ForIn.prototype._size = () => 8; +AST_With.prototype._size = () => 6; +AST_Expansion.prototype._size = () => 3; +var lambda_modifiers = (func) => (func.is_generator ? 1 : 0) + (func.async ? 6 : 0); +AST_Accessor.prototype._size = function() { + return lambda_modifiers(this) + 4 + list_overhead(this.argnames) + list_overhead(this.body); +}; +AST_Function.prototype._size = function(info) { + const first = !!first_in_statement(info); + return first * 2 + lambda_modifiers(this) + 12 + list_overhead(this.argnames) + list_overhead(this.body); +}; +AST_Defun.prototype._size = function() { + return lambda_modifiers(this) + 13 + list_overhead(this.argnames) + list_overhead(this.body); +}; +AST_Arrow.prototype._size = function() { + let args_and_arrow = 2 + list_overhead(this.argnames); + if (!(this.argnames.length === 1 && this.argnames[0] instanceof AST_Symbol)) { + args_and_arrow += 2; + } + const body_overhead = this.is_braceless() ? 0 : list_overhead(this.body) + 2; + return lambda_modifiers(this) + args_and_arrow + body_overhead; +}; +AST_Destructuring.prototype._size = () => 2; +AST_TemplateString.prototype._size = function() { + return 2 + Math.floor(this.segments.length / 2) * 3; +}; +AST_TemplateSegment.prototype._size = function() { + return this.value.length; +}; +AST_Return.prototype._size = function() { + return this.value ? 7 : 6; +}; +AST_Throw.prototype._size = () => 6; +AST_Break.prototype._size = function() { + return this.label ? 6 : 5; +}; +AST_Continue.prototype._size = function() { + return this.label ? 9 : 8; +}; +AST_If.prototype._size = () => 4; +AST_Switch.prototype._size = function() { + return 8 + list_overhead(this.body); +}; +AST_Case.prototype._size = function() { + return 5 + list_overhead(this.body); +}; +AST_Default.prototype._size = function() { + return 8 + list_overhead(this.body); +}; +AST_Try.prototype._size = () => 3; +AST_Catch.prototype._size = function() { + let size = 7 + list_overhead(this.body); + if (this.argname) { + size += 2; + } + return size; +}; +AST_Finally.prototype._size = function() { + return 7 + list_overhead(this.body); +}; +AST_Var.prototype._size = function() { + return 4 + list_overhead(this.definitions); +}; +AST_Let.prototype._size = function() { + return 4 + list_overhead(this.definitions); +}; +AST_Const.prototype._size = function() { + return 6 + list_overhead(this.definitions); +}; +AST_VarDef.prototype._size = function() { + return this.value ? 1 : 0; +}; +AST_NameMapping.prototype._size = function() { + return this.name ? 4 : 0; +}; +AST_Import.prototype._size = function() { + let size = 6; + if (this.imported_name) + size += 1; + if (this.imported_name || this.imported_names) + size += 5; + if (this.imported_names) { + size += 2 + list_overhead(this.imported_names); + } + return size; +}; +AST_ImportMeta.prototype._size = () => 11; +AST_Export.prototype._size = function() { + let size = 7 + (this.is_default ? 8 : 0); + if (this.exported_value) { + size += this.exported_value._size(); + } + if (this.exported_names) { + size += 2 + list_overhead(this.exported_names); + } + if (this.module_name) { + size += 5; + } + return size; +}; +AST_Call.prototype._size = function() { + if (this.optional) { + return 4 + list_overhead(this.args); + } + return 2 + list_overhead(this.args); +}; +AST_New.prototype._size = function() { + return 6 + list_overhead(this.args); +}; +AST_Sequence.prototype._size = function() { + return list_overhead(this.expressions); +}; +AST_Dot.prototype._size = function() { + if (this.optional) { + return this.property.length + 2; + } + return this.property.length + 1; +}; +AST_DotHash.prototype._size = function() { + if (this.optional) { + return this.property.length + 3; + } + return this.property.length + 2; +}; +AST_Sub.prototype._size = function() { + return this.optional ? 4 : 2; +}; +AST_Unary.prototype._size = function() { + if (this.operator === "typeof") + return 7; + if (this.operator === "void") + return 5; + return this.operator.length; +}; +AST_Binary.prototype._size = function(info) { + if (this.operator === "in") + return 4; + let size = this.operator.length; + if ((this.operator === "+" || this.operator === "-") && this.right instanceof AST_Unary && this.right.operator === this.operator) { + size += 1; + } + if (this.needs_parens(info)) { + size += 2; + } + return size; +}; +AST_Conditional.prototype._size = () => 3; +AST_Array.prototype._size = function() { + return 2 + list_overhead(this.elements); +}; +AST_Object.prototype._size = function(info) { + let base = 2; + if (first_in_statement(info)) { + base += 2; + } + return base + list_overhead(this.properties); +}; +var key_size = (key) => typeof key === "string" ? key.length : 0; +AST_ObjectKeyVal.prototype._size = function() { + return key_size(this.key) + 1; +}; +var static_size = (is_static) => is_static ? 7 : 0; +AST_ObjectGetter.prototype._size = function() { + return 5 + static_size(this.static) + key_size(this.key); +}; +AST_ObjectSetter.prototype._size = function() { + return 5 + static_size(this.static) + key_size(this.key); +}; +AST_ConciseMethod.prototype._size = function() { + return static_size(this.static) + key_size(this.key); +}; +AST_PrivateMethod.prototype._size = function() { + return AST_ConciseMethod.prototype._size.call(this) + 1; +}; +AST_PrivateGetter.prototype._size = function() { + return AST_ConciseMethod.prototype._size.call(this) + 4; +}; +AST_PrivateSetter.prototype._size = function() { + return AST_ConciseMethod.prototype._size.call(this) + 4; +}; +AST_PrivateIn.prototype._size = function() { + return 5; +}; +AST_Class.prototype._size = function() { + return (this.name ? 8 : 7) + (this.extends ? 8 : 0); +}; +AST_ClassStaticBlock.prototype._size = function() { + return 8 + list_overhead(this.body); +}; +AST_ClassProperty.prototype._size = function() { + return static_size(this.static) + (typeof this.key === "string" ? this.key.length + 2 : 0) + (this.value ? 1 : 0); +}; +AST_ClassPrivateProperty.prototype._size = function() { + return AST_ClassProperty.prototype._size.call(this) + 1; +}; +AST_Symbol.prototype._size = function() { + if (!(mangle_options && this.thedef && !this.thedef.unmangleable(mangle_options))) { + return this.name.length; + } else { + return 1; + } +}; +AST_SymbolClassProperty.prototype._size = function() { + return this.name.length; +}; +AST_SymbolRef.prototype._size = AST_SymbolDeclaration.prototype._size = function() { + if (this.name === "arguments") + return 9; + return AST_Symbol.prototype._size.call(this); +}; +AST_NewTarget.prototype._size = () => 10; +AST_SymbolImportForeign.prototype._size = function() { + return this.name.length; +}; +AST_SymbolExportForeign.prototype._size = function() { + return this.name.length; +}; +AST_This.prototype._size = () => 4; +AST_Super.prototype._size = () => 5; +AST_String.prototype._size = function() { + return this.value.length + 2; +}; +AST_Number.prototype._size = function() { + const { value } = this; + if (value === 0) + return 1; + if (value > 0 && Math.floor(value) === value) { + return Math.floor(Math.log10(value) + 1); + } + return value.toString().length; +}; +AST_BigInt.prototype._size = function() { + return this.value.length; +}; +AST_RegExp.prototype._size = function() { + return this.value.toString().length; +}; +AST_Null.prototype._size = () => 4; +AST_NaN.prototype._size = () => 3; +AST_Undefined.prototype._size = () => 6; +AST_Hole.prototype._size = () => 0; +AST_Infinity.prototype._size = () => 8; +AST_True.prototype._size = () => 4; +AST_False.prototype._size = () => 5; +AST_Await.prototype._size = () => 6; +AST_Yield.prototype._size = () => 6; + +// node_modules/terser/lib/compress/compressor-flags.js +var UNUSED = 1; +var TRUTHY = 2; +var FALSY = 4; +var UNDEFINED = 8; +var INLINED = 16; +var WRITE_ONLY = 32; +var SQUEEZED = 256; +var OPTIMIZED = 512; +var TOP = 1024; +var CLEAR_BETWEEN_PASSES = SQUEEZED | OPTIMIZED | TOP; +var has_flag = (node, flag) => node.flags & flag; +var set_flag = (node, flag) => { + node.flags |= flag; +}; +var clear_flag = (node, flag) => { + node.flags &= ~flag; +}; + +// node_modules/terser/lib/compress/common.js +function merge_sequence(array, node) { + if (node instanceof AST_Sequence) { + array.push(...node.expressions); + } else { + array.push(node); + } + return array; +} +function make_sequence(orig, expressions) { + if (expressions.length == 1) + return expressions[0]; + if (expressions.length == 0) + throw new Error("trying to create a sequence with length zero!"); + return make_node(AST_Sequence, orig, { + expressions: expressions.reduce(merge_sequence, []) + }); +} +function make_empty_function(self2) { + return make_node(AST_Function, self2, { + uses_arguments: false, + argnames: [], + body: [], + is_generator: false, + async: false, + variables: new Map, + uses_with: false, + uses_eval: false, + parent_scope: null, + enclosed: [], + cname: 0, + block_scope: undefined + }); +} +function make_node_from_constant(val, orig) { + switch (typeof val) { + case "string": + return make_node(AST_String, orig, { + value: val + }); + case "number": + if (isNaN(val)) + return make_node(AST_NaN, orig); + if (isFinite(val)) { + return 1 / val < 0 ? make_node(AST_UnaryPrefix, orig, { + operator: "-", + expression: make_node(AST_Number, orig, { value: -val }) + }) : make_node(AST_Number, orig, { value: val }); + } + return val < 0 ? make_node(AST_UnaryPrefix, orig, { + operator: "-", + expression: make_node(AST_Infinity, orig) + }) : make_node(AST_Infinity, orig); + case "bigint": + return make_node(AST_BigInt, orig, { value: val.toString() }); + case "boolean": + return make_node(val ? AST_True : AST_False, orig); + case "undefined": + return make_node(AST_Undefined, orig); + default: + if (val === null) { + return make_node(AST_Null, orig, { value: null }); + } + if (val instanceof RegExp) { + return make_node(AST_RegExp, orig, { + value: { + source: regexp_source_fix(val.source), + flags: val.flags + } + }); + } + throw new Error(string_template("Can't handle constant of type: {type}", { + type: typeof val + })); + } +} +function best_of_expression(ast1, ast2) { + return ast1.size() > ast2.size() ? ast2 : ast1; +} +function best_of_statement(ast1, ast2) { + return best_of_expression(make_node(AST_SimpleStatement, ast1, { + body: ast1 + }), make_node(AST_SimpleStatement, ast2, { + body: ast2 + })).body; +} +function best_of(compressor, ast1, ast2) { + if (first_in_statement(compressor)) { + return best_of_statement(ast1, ast2); + } else { + return best_of_expression(ast1, ast2); + } +} +function get_simple_key(key) { + if (key instanceof AST_Constant) { + return key.getValue(); + } + if (key instanceof AST_UnaryPrefix && key.operator == "void" && key.expression instanceof AST_Constant) { + return; + } + return key; +} +function read_property(obj, key) { + key = get_simple_key(key); + if (key instanceof AST_Node) + return; + var value; + if (obj instanceof AST_Array) { + var elements = obj.elements; + if (key == "length") + return make_node_from_constant(elements.length, obj); + if (typeof key == "number" && key in elements) + value = elements[key]; + } else if (obj instanceof AST_Object) { + key = "" + key; + var props = obj.properties; + for (var i = props.length;--i >= 0; ) { + var prop = props[i]; + if (!(prop instanceof AST_ObjectKeyVal)) + return; + if (!value && props[i].key === key) + value = props[i].value; + } + } + return value instanceof AST_SymbolRef && value.fixed_value() || value; +} +function has_break_or_continue(loop, parent) { + var found = false; + var tw = new TreeWalker(function(node) { + if (found || node instanceof AST_Scope) + return true; + if (node instanceof AST_LoopControl && tw.loopcontrol_target(node) === loop) { + return found = true; + } + }); + if (parent instanceof AST_LabeledStatement) + tw.push(parent); + tw.push(loop); + loop.body.walk(tw); + return found; +} +function maintain_this_binding(parent, orig, val) { + if (requires_sequence_to_maintain_binding(parent, orig, val)) { + const zero = make_node(AST_Number, orig, { value: 0 }); + return make_sequence(orig, [zero, val]); + } else { + return val; + } +} +function requires_sequence_to_maintain_binding(parent, orig, val) { + return parent instanceof AST_UnaryPrefix && parent.operator == "delete" || parent instanceof AST_Call && parent.expression === orig && (val instanceof AST_Chain || val instanceof AST_PropAccess || val instanceof AST_SymbolRef && val.name == "eval"); +} +function is_func_expr(node) { + return node instanceof AST_Arrow || node instanceof AST_Function; +} +function is_iife_call(node) { + if (node.TYPE != "Call") + return false; + return node.expression instanceof AST_Function || is_iife_call(node.expression); +} +function is_empty(thing) { + if (thing === null) + return true; + if (thing instanceof AST_EmptyStatement) + return true; + if (thing instanceof AST_BlockStatement) + return thing.body.length == 0; + return false; +} +var identifier_atom = makePredicate("Infinity NaN undefined"); +function is_identifier_atom(node) { + return node instanceof AST_Infinity || node instanceof AST_NaN || node instanceof AST_Undefined; +} +function is_ref_of(ref, type) { + if (!(ref instanceof AST_SymbolRef)) + return false; + var orig = ref.definition().orig; + for (var i = orig.length;--i >= 0; ) { + if (orig[i] instanceof type) + return true; + } +} +function can_be_evicted_from_block(node) { + return !(node instanceof AST_DefClass || node instanceof AST_Defun || node instanceof AST_Let || node instanceof AST_Const || node instanceof AST_Export || node instanceof AST_Import); +} +function as_statement_array(thing) { + if (thing === null) + return []; + if (thing instanceof AST_BlockStatement) + return thing.body; + if (thing instanceof AST_EmptyStatement) + return []; + if (thing instanceof AST_Statement) + return [thing]; + throw new Error("Can't convert thing to statement array"); +} +function is_reachable(scope_node, defs) { + const find_ref = (node) => { + if (node instanceof AST_SymbolRef && defs.includes(node.definition())) { + return walk_abort; + } + }; + return walk_parent(scope_node, (node, info) => { + if (node instanceof AST_Scope && node !== scope_node) { + var parent = info.parent(); + if (parent instanceof AST_Call && parent.expression === node && !(node.async || node.is_generator)) { + return; + } + if (walk(node, find_ref)) + return walk_abort; + return true; + } + }); +} +function is_recursive_ref(tw, def) { + var node; + for (var i = 0;node = tw.parent(i); i++) { + if (node instanceof AST_Lambda || node instanceof AST_Class) { + var name = node.name; + if (name && name.definition() === def) { + return true; + } + } + } + return false; +} +function retain_top_func(fn, compressor) { + return compressor.top_retain && fn instanceof AST_Defun && has_flag(fn, TOP) && fn.name && compressor.top_retain(fn.name.definition()); +} + +// node_modules/terser/lib/compress/native-objects.js +function make_nested_lookup(obj) { + const out = new Map; + for (var key of Object.keys(obj)) { + out.set(key, makePredicate(obj[key])); + } + const does_have = (global_name, fname) => { + const inner_map = out.get(global_name); + return inner_map != null && inner_map.has(fname); + }; + return does_have; +} +var pure_prop_access_globals = new Set([ + "Number", + "String", + "Array", + "Object", + "Function", + "Promise" +]); +var object_methods = [ + "constructor", + "toString", + "valueOf" +]; +var is_pure_native_method = make_nested_lookup({ + Array: [ + "at", + "flat", + "includes", + "indexOf", + "join", + "lastIndexOf", + "slice", + ...object_methods + ], + Boolean: object_methods, + Function: object_methods, + Number: [ + "toExponential", + "toFixed", + "toPrecision", + ...object_methods + ], + Object: object_methods, + RegExp: [ + "test", + ...object_methods + ], + String: [ + "at", + "charAt", + "charCodeAt", + "charPointAt", + "concat", + "endsWith", + "fromCharCode", + "fromCodePoint", + "includes", + "indexOf", + "italics", + "lastIndexOf", + "localeCompare", + "match", + "matchAll", + "normalize", + "padStart", + "padEnd", + "repeat", + "replace", + "replaceAll", + "search", + "slice", + "split", + "startsWith", + "substr", + "substring", + "repeat", + "toLocaleLowerCase", + "toLocaleUpperCase", + "toLowerCase", + "toUpperCase", + "trim", + "trimEnd", + "trimStart", + ...object_methods + ] +}); +var is_pure_native_fn = make_nested_lookup({ + Array: [ + "isArray" + ], + Math: [ + "abs", + "acos", + "asin", + "atan", + "ceil", + "cos", + "exp", + "floor", + "log", + "round", + "sin", + "sqrt", + "tan", + "atan2", + "pow", + "max", + "min" + ], + Number: [ + "isFinite", + "isNaN" + ], + Object: [ + "create", + "getOwnPropertyDescriptor", + "getOwnPropertyNames", + "getPrototypeOf", + "isExtensible", + "isFrozen", + "isSealed", + "hasOwn", + "keys" + ], + String: [ + "fromCharCode" + ] +}); +var is_pure_native_value = make_nested_lookup({ + Math: [ + "E", + "LN10", + "LN2", + "LOG2E", + "LOG10E", + "PI", + "SQRT1_2", + "SQRT2" + ], + Number: [ + "MAX_VALUE", + "MIN_VALUE", + "NaN", + "NEGATIVE_INFINITY", + "POSITIVE_INFINITY" + ] +}); + +// node_modules/terser/lib/compress/inference.js +var is_undeclared_ref = (node) => node instanceof AST_SymbolRef && node.definition().undeclared; +var bitwise_binop = makePredicate("<<< >> << & | ^ ~"); +var lazy_op = makePredicate("&& || ??"); +var unary_side_effects = makePredicate("delete ++ --"); +(function(def_is_boolean) { + const unary_bool = makePredicate("! delete"); + const binary_bool = makePredicate("in instanceof == != === !== < <= >= >"); + def_is_boolean(AST_Node, return_false); + def_is_boolean(AST_UnaryPrefix, function() { + return unary_bool.has(this.operator); + }); + def_is_boolean(AST_Binary, function() { + return binary_bool.has(this.operator) || lazy_op.has(this.operator) && this.left.is_boolean() && this.right.is_boolean(); + }); + def_is_boolean(AST_Conditional, function() { + return this.consequent.is_boolean() && this.alternative.is_boolean(); + }); + def_is_boolean(AST_Assign, function() { + return this.operator == "=" && this.right.is_boolean(); + }); + def_is_boolean(AST_Sequence, function() { + return this.tail_node().is_boolean(); + }); + def_is_boolean(AST_True, return_true); + def_is_boolean(AST_False, return_true); +})(function(node, func) { + node.DEFMETHOD("is_boolean", func); +}); +(function(def_is_number) { + def_is_number(AST_Node, return_false); + def_is_number(AST_Number, return_true); + const unary = makePredicate("+ - ~ ++ --"); + def_is_number(AST_Unary, function(compressor) { + return unary.has(this.operator) && this.expression.is_number(compressor); + }); + const numeric_ops = makePredicate("- * / % & | ^ << >> >>>"); + def_is_number(AST_Binary, function(compressor) { + if (this.operator === "+") { + return this.left.is_number(compressor) && this.right.is_number_or_bigint(compressor) || this.right.is_number(compressor) && this.left.is_number_or_bigint(compressor); + } else if (numeric_ops.has(this.operator)) { + return this.left.is_number(compressor) || this.right.is_number(compressor); + } else { + return false; + } + }); + def_is_number(AST_Assign, function(compressor) { + return (this.operator === "=" || numeric_ops.has(this.operator.slice(0, -1))) && this.right.is_number(compressor); + }); + def_is_number(AST_Sequence, function(compressor) { + return this.tail_node().is_number(compressor); + }); + def_is_number(AST_Conditional, function(compressor) { + return this.consequent.is_number(compressor) && this.alternative.is_number(compressor); + }); +})(function(node, func) { + node.DEFMETHOD("is_number", func); +}); +(function(def_is_bigint) { + def_is_bigint(AST_Node, return_false); + def_is_bigint(AST_BigInt, return_true); + const unary = makePredicate("+ - ~ ++ --"); + def_is_bigint(AST_Unary, function(compressor) { + return unary.has(this.operator) && this.expression.is_bigint(compressor); + }); + const numeric_ops = makePredicate("- * / % & | ^ << >>"); + def_is_bigint(AST_Binary, function(compressor) { + if (this.operator === "+") { + return this.left.is_bigint(compressor) && this.right.is_number_or_bigint(compressor) || this.right.is_bigint(compressor) && this.left.is_number_or_bigint(compressor); + } else if (numeric_ops.has(this.operator)) { + return this.left.is_bigint(compressor) || this.right.is_bigint(compressor); + } else { + return false; + } + }); + def_is_bigint(AST_Assign, function(compressor) { + return (numeric_ops.has(this.operator.slice(0, -1)) || this.operator == "=") && this.right.is_bigint(compressor); + }); + def_is_bigint(AST_Sequence, function(compressor) { + return this.tail_node().is_bigint(compressor); + }); + def_is_bigint(AST_Conditional, function(compressor) { + return this.consequent.is_bigint(compressor) && this.alternative.is_bigint(compressor); + }); +})(function(node, func) { + node.DEFMETHOD("is_bigint", func); +}); +(function(def_is_number_or_bigint) { + def_is_number_or_bigint(AST_Node, return_false); + def_is_number_or_bigint(AST_Number, return_true); + def_is_number_or_bigint(AST_BigInt, return_true); + const numeric_unary_ops = makePredicate("+ - ~ ++ --"); + def_is_number_or_bigint(AST_Unary, function(_compressor) { + return numeric_unary_ops.has(this.operator); + }); + const numeric_ops = makePredicate("- * / % & | ^ << >>"); + def_is_number_or_bigint(AST_Binary, function(compressor) { + return this.operator === "+" ? this.left.is_number_or_bigint(compressor) && this.right.is_number_or_bigint(compressor) : numeric_ops.has(this.operator); + }); + def_is_number_or_bigint(AST_Assign, function(compressor) { + return numeric_ops.has(this.operator.slice(0, -1)) || this.operator == "=" && this.right.is_number_or_bigint(compressor); + }); + def_is_number_or_bigint(AST_Sequence, function(compressor) { + return this.tail_node().is_number_or_bigint(compressor); + }); + def_is_number_or_bigint(AST_Conditional, function(compressor) { + return this.consequent.is_number_or_bigint(compressor) && this.alternative.is_number_or_bigint(compressor); + }); +})(function(node, func) { + node.DEFMETHOD("is_number_or_bigint", func); +}); +(function(def_is_32_bit_integer) { + def_is_32_bit_integer(AST_Node, return_false); + def_is_32_bit_integer(AST_Number, function(_compressor) { + return this.value === (this.value | 0); + }); + def_is_32_bit_integer(AST_UnaryPrefix, function(compressor) { + return this.operator == "~" ? this.expression.is_number(compressor) : this.operator === "+" ? this.expression.is_32_bit_integer(compressor) : false; + }); + def_is_32_bit_integer(AST_Binary, function(compressor) { + return bitwise_binop.has(this.operator) && (this.left.is_number(compressor) || this.right.is_number(compressor)); + }); +})(function(node, func) { + node.DEFMETHOD("is_32_bit_integer", func); +}); +(function(def_is_string) { + def_is_string(AST_Node, return_false); + def_is_string(AST_String, return_true); + def_is_string(AST_TemplateString, return_true); + def_is_string(AST_UnaryPrefix, function() { + return this.operator == "typeof"; + }); + def_is_string(AST_Binary, function(compressor) { + return this.operator == "+" && (this.left.is_string(compressor) || this.right.is_string(compressor)); + }); + def_is_string(AST_Assign, function(compressor) { + return (this.operator == "=" || this.operator == "+=") && this.right.is_string(compressor); + }); + def_is_string(AST_Sequence, function(compressor) { + return this.tail_node().is_string(compressor); + }); + def_is_string(AST_Conditional, function(compressor) { + return this.consequent.is_string(compressor) && this.alternative.is_string(compressor); + }); +})(function(node, func) { + node.DEFMETHOD("is_string", func); +}); +function is_undefined(node, compressor) { + return has_flag(node, UNDEFINED) || node instanceof AST_Undefined || node instanceof AST_UnaryPrefix && node.operator == "void" && !node.expression.has_side_effects(compressor); +} +function is_null_or_undefined(node, compressor) { + let fixed; + return node instanceof AST_Null || is_undefined(node, compressor) || node instanceof AST_SymbolRef && (fixed = node.definition().fixed) instanceof AST_Node && is_nullish(fixed, compressor); +} +function is_nullish_shortcircuited(node, compressor) { + if (node instanceof AST_PropAccess || node instanceof AST_Call) { + return node.optional && is_null_or_undefined(node.expression, compressor) || is_nullish_shortcircuited(node.expression, compressor); + } + if (node instanceof AST_Chain) + return is_nullish_shortcircuited(node.expression, compressor); + return false; +} +function is_nullish(node, compressor) { + if (is_null_or_undefined(node, compressor)) + return true; + return is_nullish_shortcircuited(node, compressor); +} +(function(def_has_side_effects) { + def_has_side_effects(AST_Node, return_true); + def_has_side_effects(AST_EmptyStatement, return_false); + def_has_side_effects(AST_Constant, return_false); + def_has_side_effects(AST_This, return_false); + function any(list, compressor) { + for (var i = list.length;--i >= 0; ) + if (list[i].has_side_effects(compressor)) + return true; + return false; + } + def_has_side_effects(AST_Block, function(compressor) { + return any(this.body, compressor); + }); + def_has_side_effects(AST_Call, function(compressor) { + if (!this.is_callee_pure(compressor) && (!this.expression.is_call_pure(compressor) || this.expression.has_side_effects(compressor))) { + return true; + } + return any(this.args, compressor); + }); + def_has_side_effects(AST_Switch, function(compressor) { + return this.expression.has_side_effects(compressor) || any(this.body, compressor); + }); + def_has_side_effects(AST_Case, function(compressor) { + return this.expression.has_side_effects(compressor) || any(this.body, compressor); + }); + def_has_side_effects(AST_Try, function(compressor) { + return this.body.has_side_effects(compressor) || this.bcatch && this.bcatch.has_side_effects(compressor) || this.bfinally && this.bfinally.has_side_effects(compressor); + }); + def_has_side_effects(AST_If, function(compressor) { + return this.condition.has_side_effects(compressor) || this.body && this.body.has_side_effects(compressor) || this.alternative && this.alternative.has_side_effects(compressor); + }); + def_has_side_effects(AST_ImportMeta, return_false); + def_has_side_effects(AST_LabeledStatement, function(compressor) { + return this.body.has_side_effects(compressor); + }); + def_has_side_effects(AST_SimpleStatement, function(compressor) { + return this.body.has_side_effects(compressor); + }); + def_has_side_effects(AST_Lambda, return_false); + def_has_side_effects(AST_Class, function(compressor) { + if (this.extends && this.extends.has_side_effects(compressor)) { + return true; + } + return any(this.properties, compressor); + }); + def_has_side_effects(AST_ClassStaticBlock, function(compressor) { + return any(this.body, compressor); + }); + def_has_side_effects(AST_Binary, function(compressor) { + return this.left.has_side_effects(compressor) || this.right.has_side_effects(compressor); + }); + def_has_side_effects(AST_Assign, return_true); + def_has_side_effects(AST_Conditional, function(compressor) { + return this.condition.has_side_effects(compressor) || this.consequent.has_side_effects(compressor) || this.alternative.has_side_effects(compressor); + }); + def_has_side_effects(AST_Unary, function(compressor) { + return unary_side_effects.has(this.operator) || this.expression.has_side_effects(compressor); + }); + def_has_side_effects(AST_SymbolRef, function(compressor) { + return !this.is_declared(compressor) && !pure_prop_access_globals.has(this.name); + }); + def_has_side_effects(AST_SymbolClassProperty, return_false); + def_has_side_effects(AST_SymbolDeclaration, return_false); + def_has_side_effects(AST_Object, function(compressor) { + return any(this.properties, compressor); + }); + def_has_side_effects(AST_ObjectKeyVal, function(compressor) { + return this.computed_key() && this.key.has_side_effects(compressor) || this.value && this.value.has_side_effects(compressor); + }); + def_has_side_effects([ + AST_ClassProperty, + AST_ClassPrivateProperty + ], function(compressor) { + return this.computed_key() && this.key.has_side_effects(compressor) || this.static && this.value && this.value.has_side_effects(compressor); + }); + def_has_side_effects([ + AST_PrivateMethod, + AST_PrivateGetter, + AST_PrivateSetter, + AST_ConciseMethod, + AST_ObjectGetter, + AST_ObjectSetter + ], function(compressor) { + return this.computed_key() && this.key.has_side_effects(compressor); + }); + def_has_side_effects(AST_Array, function(compressor) { + return any(this.elements, compressor); + }); + def_has_side_effects(AST_Dot, function(compressor) { + if (is_nullish(this, compressor)) { + return this.expression.has_side_effects(compressor); + } + if (!this.optional && this.expression.may_throw_on_access(compressor)) { + return true; + } + return this.expression.has_side_effects(compressor); + }); + def_has_side_effects(AST_Sub, function(compressor) { + if (is_nullish(this, compressor)) { + return this.expression.has_side_effects(compressor); + } + if (!this.optional && this.expression.may_throw_on_access(compressor)) { + return true; + } + var property = this.property.has_side_effects(compressor); + if (property && this.optional) + return true; + return property || this.expression.has_side_effects(compressor); + }); + def_has_side_effects(AST_Chain, function(compressor) { + return this.expression.has_side_effects(compressor); + }); + def_has_side_effects(AST_Sequence, function(compressor) { + return any(this.expressions, compressor); + }); + def_has_side_effects(AST_Definitions, function(compressor) { + return any(this.definitions, compressor); + }); + def_has_side_effects(AST_VarDef, function() { + return this.value != null; + }); + def_has_side_effects(AST_TemplateSegment, return_false); + def_has_side_effects(AST_TemplateString, function(compressor) { + return any(this.segments, compressor); + }); +})(function(node_or_nodes, func) { + for (const node of [].concat(node_or_nodes)) { + node.DEFMETHOD("has_side_effects", func); + } +}); +(function(def_may_throw) { + def_may_throw(AST_Node, return_true); + def_may_throw(AST_Constant, return_false); + def_may_throw(AST_EmptyStatement, return_false); + def_may_throw(AST_Lambda, return_false); + def_may_throw(AST_SymbolDeclaration, return_false); + def_may_throw(AST_This, return_false); + def_may_throw(AST_ImportMeta, return_false); + function any(list, compressor) { + for (var i = list.length;--i >= 0; ) + if (list[i].may_throw(compressor)) + return true; + return false; + } + def_may_throw(AST_Class, function(compressor) { + if (this.extends && this.extends.may_throw(compressor)) + return true; + return any(this.properties, compressor); + }); + def_may_throw(AST_ClassStaticBlock, function(compressor) { + return any(this.body, compressor); + }); + def_may_throw(AST_Array, function(compressor) { + return any(this.elements, compressor); + }); + def_may_throw(AST_Assign, function(compressor) { + if (this.right.may_throw(compressor)) + return true; + if (!compressor.has_directive("use strict") && this.operator == "=" && this.left instanceof AST_SymbolRef) { + return false; + } + return this.left.may_throw(compressor); + }); + def_may_throw(AST_Binary, function(compressor) { + return this.left.may_throw(compressor) || this.right.may_throw(compressor); + }); + def_may_throw(AST_Block, function(compressor) { + return any(this.body, compressor); + }); + def_may_throw(AST_Call, function(compressor) { + if (is_nullish(this, compressor)) + return false; + if (any(this.args, compressor)) + return true; + if (this.is_callee_pure(compressor)) + return false; + if (this.expression.may_throw(compressor)) + return true; + return !(this.expression instanceof AST_Lambda) || any(this.expression.body, compressor); + }); + def_may_throw(AST_Case, function(compressor) { + return this.expression.may_throw(compressor) || any(this.body, compressor); + }); + def_may_throw(AST_Conditional, function(compressor) { + return this.condition.may_throw(compressor) || this.consequent.may_throw(compressor) || this.alternative.may_throw(compressor); + }); + def_may_throw(AST_Definitions, function(compressor) { + return any(this.definitions, compressor); + }); + def_may_throw(AST_If, function(compressor) { + return this.condition.may_throw(compressor) || this.body && this.body.may_throw(compressor) || this.alternative && this.alternative.may_throw(compressor); + }); + def_may_throw(AST_LabeledStatement, function(compressor) { + return this.body.may_throw(compressor); + }); + def_may_throw(AST_Object, function(compressor) { + return any(this.properties, compressor); + }); + def_may_throw(AST_ObjectKeyVal, function(compressor) { + return this.computed_key() && this.key.may_throw(compressor) || this.value ? this.value.may_throw(compressor) : false; + }); + def_may_throw([ + AST_ClassProperty, + AST_ClassPrivateProperty + ], function(compressor) { + return this.computed_key() && this.key.may_throw(compressor) || this.static && this.value && this.value.may_throw(compressor); + }); + def_may_throw([ + AST_ConciseMethod, + AST_ObjectGetter, + AST_ObjectSetter + ], function(compressor) { + return this.computed_key() && this.key.may_throw(compressor); + }); + def_may_throw([ + AST_PrivateMethod, + AST_PrivateGetter, + AST_PrivateSetter + ], return_false); + def_may_throw(AST_Return, function(compressor) { + return this.value && this.value.may_throw(compressor); + }); + def_may_throw(AST_Sequence, function(compressor) { + return any(this.expressions, compressor); + }); + def_may_throw(AST_SimpleStatement, function(compressor) { + return this.body.may_throw(compressor); + }); + def_may_throw(AST_Dot, function(compressor) { + if (is_nullish(this, compressor)) + return false; + return !this.optional && this.expression.may_throw_on_access(compressor) || this.expression.may_throw(compressor); + }); + def_may_throw(AST_Sub, function(compressor) { + if (is_nullish(this, compressor)) + return false; + return !this.optional && this.expression.may_throw_on_access(compressor) || this.expression.may_throw(compressor) || this.property.may_throw(compressor); + }); + def_may_throw(AST_Chain, function(compressor) { + return this.expression.may_throw(compressor); + }); + def_may_throw(AST_Switch, function(compressor) { + return this.expression.may_throw(compressor) || any(this.body, compressor); + }); + def_may_throw(AST_SymbolRef, function(compressor) { + return !this.is_declared(compressor) && !pure_prop_access_globals.has(this.name); + }); + def_may_throw(AST_SymbolClassProperty, return_false); + def_may_throw(AST_Try, function(compressor) { + return this.bcatch ? this.bcatch.may_throw(compressor) : this.body.may_throw(compressor) || this.bfinally && this.bfinally.may_throw(compressor); + }); + def_may_throw(AST_Unary, function(compressor) { + if (this.operator == "typeof" && this.expression instanceof AST_SymbolRef) + return false; + return this.expression.may_throw(compressor); + }); + def_may_throw(AST_VarDef, function(compressor) { + if (!this.value) + return false; + return this.value.may_throw(compressor); + }); +})(function(node_or_nodes, func) { + for (const node of [].concat(node_or_nodes)) { + node.DEFMETHOD("may_throw", func); + } +}); +(function(def_is_constant_expression) { + function all_refs_local(scope) { + let result = true; + walk(this, (node) => { + if (node instanceof AST_SymbolRef) { + if (has_flag(this, INLINED)) { + result = false; + return walk_abort; + } + var def = node.definition(); + if (member(def, this.enclosed) && !this.variables.has(def.name)) { + if (scope) { + var scope_def = scope.find_variable(node); + if (def.undeclared ? !scope_def : scope_def === def) { + result = "f"; + return true; + } + } + result = false; + return walk_abort; + } + return true; + } + if (node instanceof AST_This && this instanceof AST_Arrow) { + result = false; + return walk_abort; + } + }); + return result; + } + def_is_constant_expression(AST_Node, return_false); + def_is_constant_expression(AST_Constant, return_true); + def_is_constant_expression(AST_Class, function(scope) { + if (this.extends && !this.extends.is_constant_expression(scope)) { + return false; + } + for (const prop of this.properties) { + if (prop.computed_key() && !prop.key.is_constant_expression(scope)) { + return false; + } + if (prop.static && prop.value && !prop.value.is_constant_expression(scope)) { + return false; + } + if (prop instanceof AST_ClassStaticBlock) { + return false; + } + } + return all_refs_local.call(this, scope); + }); + def_is_constant_expression(AST_Lambda, all_refs_local); + def_is_constant_expression(AST_Unary, function() { + return this.expression.is_constant_expression(); + }); + def_is_constant_expression(AST_Binary, function() { + return this.left.is_constant_expression() && this.right.is_constant_expression(); + }); + def_is_constant_expression(AST_Array, function() { + return this.elements.every((l) => l.is_constant_expression()); + }); + def_is_constant_expression(AST_Object, function() { + return this.properties.every((l) => l.is_constant_expression()); + }); + def_is_constant_expression(AST_ObjectProperty, function() { + return !!(!(this.key instanceof AST_Node) && this.value && this.value.is_constant_expression()); + }); +})(function(node, func) { + node.DEFMETHOD("is_constant_expression", func); +}); +(function(def_may_throw_on_access) { + AST_Node.DEFMETHOD("may_throw_on_access", function(compressor) { + return !compressor.option("pure_getters") || this._dot_throw(compressor); + }); + function is_strict(compressor) { + return /strict/.test(compressor.option("pure_getters")); + } + def_may_throw_on_access(AST_Node, is_strict); + def_may_throw_on_access(AST_Null, return_true); + def_may_throw_on_access(AST_Undefined, return_true); + def_may_throw_on_access(AST_Constant, return_false); + def_may_throw_on_access(AST_Array, return_false); + def_may_throw_on_access(AST_Object, function(compressor) { + if (!is_strict(compressor)) + return false; + for (var i = this.properties.length;--i >= 0; ) + if (this.properties[i]._dot_throw(compressor)) + return true; + return false; + }); + def_may_throw_on_access(AST_Class, return_false); + def_may_throw_on_access(AST_ObjectProperty, return_false); + def_may_throw_on_access(AST_ObjectGetter, return_true); + def_may_throw_on_access(AST_Expansion, function(compressor) { + return this.expression._dot_throw(compressor); + }); + def_may_throw_on_access(AST_Function, return_false); + def_may_throw_on_access(AST_Arrow, return_false); + def_may_throw_on_access(AST_UnaryPostfix, return_false); + def_may_throw_on_access(AST_UnaryPrefix, function() { + return this.operator == "void"; + }); + def_may_throw_on_access(AST_Binary, function(compressor) { + return (this.operator == "&&" || this.operator == "||" || this.operator == "??") && (this.left._dot_throw(compressor) || this.right._dot_throw(compressor)); + }); + def_may_throw_on_access(AST_Assign, function(compressor) { + if (this.logical) + return true; + return this.operator == "=" && this.right._dot_throw(compressor); + }); + def_may_throw_on_access(AST_Conditional, function(compressor) { + return this.consequent._dot_throw(compressor) || this.alternative._dot_throw(compressor); + }); + def_may_throw_on_access(AST_Dot, function(compressor) { + if (!is_strict(compressor)) + return false; + if (this.property == "prototype") { + return !(this.expression instanceof AST_Function || this.expression instanceof AST_Class); + } + return true; + }); + def_may_throw_on_access(AST_Chain, function(compressor) { + return this.expression._dot_throw(compressor); + }); + def_may_throw_on_access(AST_Sequence, function(compressor) { + return this.tail_node()._dot_throw(compressor); + }); + def_may_throw_on_access(AST_SymbolRef, function(compressor) { + if (this.name === "arguments" && this.scope instanceof AST_Lambda) + return false; + if (has_flag(this, UNDEFINED)) + return true; + if (!is_strict(compressor)) + return false; + if (is_undeclared_ref(this) && this.is_declared(compressor)) + return false; + if (this.is_immutable()) + return false; + var fixed = this.fixed_value(); + return !fixed || fixed._dot_throw(compressor); + }); +})(function(node, func) { + node.DEFMETHOD("_dot_throw", func); +}); +function is_lhs(node, parent) { + if (parent instanceof AST_Unary && unary_side_effects.has(parent.operator)) + return parent.expression; + if (parent instanceof AST_Assign && parent.left === node) + return node; + if (parent instanceof AST_ForIn && parent.init === node) + return node; +} +(function(def_negate) { + function basic_negation(exp) { + return make_node(AST_UnaryPrefix, exp, { + operator: "!", + expression: exp + }); + } + function best(orig, alt, first_in_statement2) { + var negated = basic_negation(orig); + if (first_in_statement2) { + var stat = make_node(AST_SimpleStatement, alt, { + body: alt + }); + return best_of_expression(negated, stat) === stat ? alt : negated; + } + return best_of_expression(negated, alt); + } + def_negate(AST_Node, function() { + return basic_negation(this); + }); + def_negate(AST_Statement, function() { + throw new Error("Cannot negate a statement"); + }); + def_negate(AST_Function, function() { + return basic_negation(this); + }); + def_negate(AST_Class, function() { + return basic_negation(this); + }); + def_negate(AST_Arrow, function() { + return basic_negation(this); + }); + def_negate(AST_UnaryPrefix, function() { + if (this.operator == "!") + return this.expression; + return basic_negation(this); + }); + def_negate(AST_Sequence, function(compressor) { + var expressions = this.expressions.slice(); + expressions.push(expressions.pop().negate(compressor)); + return make_sequence(this, expressions); + }); + def_negate(AST_Conditional, function(compressor, first_in_statement2) { + var self2 = this.clone(); + self2.consequent = self2.consequent.negate(compressor); + self2.alternative = self2.alternative.negate(compressor); + return best(this, self2, first_in_statement2); + }); + def_negate(AST_Binary, function(compressor, first_in_statement2) { + var self2 = this.clone(), op = this.operator; + if (compressor.option("unsafe_comps")) { + switch (op) { + case "<=": + self2.operator = ">"; + return self2; + case "<": + self2.operator = ">="; + return self2; + case ">=": + self2.operator = "<"; + return self2; + case ">": + self2.operator = "<="; + return self2; + } + } + switch (op) { + case "==": + self2.operator = "!="; + return self2; + case "!=": + self2.operator = "=="; + return self2; + case "===": + self2.operator = "!=="; + return self2; + case "!==": + self2.operator = "==="; + return self2; + case "&&": + self2.operator = "||"; + self2.left = self2.left.negate(compressor, first_in_statement2); + self2.right = self2.right.negate(compressor); + return best(this, self2, first_in_statement2); + case "||": + self2.operator = "&&"; + self2.left = self2.left.negate(compressor, first_in_statement2); + self2.right = self2.right.negate(compressor); + return best(this, self2, first_in_statement2); + } + return basic_negation(this); + }); +})(function(node, func) { + node.DEFMETHOD("negate", function(compressor, first_in_statement2) { + return func.call(this, compressor, first_in_statement2); + }); +}); +(function(def_bitwise_negate) { + function basic_bitwise_negation(exp) { + return make_node(AST_UnaryPrefix, exp, { + operator: "~", + expression: exp + }); + } + def_bitwise_negate(AST_Node, function(_compressor) { + return basic_bitwise_negation(this); + }); + def_bitwise_negate(AST_Number, function(_compressor) { + const neg = ~this.value; + if (neg.toString().length > this.value.toString().length) { + return basic_bitwise_negation(this); + } + return make_node(AST_Number, this, { value: neg }); + }); + def_bitwise_negate(AST_UnaryPrefix, function(compressor, in_32_bit_context) { + if (this.operator == "~" && (this.expression.is_32_bit_integer(compressor) || (in_32_bit_context != null ? in_32_bit_context : compressor.in_32_bit_context()))) { + return this.expression; + } else { + return basic_bitwise_negation(this); + } + }); +})(function(node, func) { + node.DEFMETHOD("bitwise_negate", func); +}); +var global_pure_fns = makePredicate("Boolean decodeURI decodeURIComponent Date encodeURI encodeURIComponent Error escape EvalError isFinite isNaN Number Object parseFloat parseInt RangeError ReferenceError String SyntaxError TypeError unescape URIError"); +AST_Call.DEFMETHOD("is_callee_pure", function(compressor) { + if (compressor.option("unsafe")) { + var expr = this.expression; + var first_arg = this.args && this.args[0] && this.args[0].evaluate(compressor); + if (expr.expression && expr.expression.name === "hasOwnProperty" && (first_arg == null || first_arg.thedef && first_arg.thedef.undeclared)) { + return false; + } + if (is_undeclared_ref(expr) && global_pure_fns.has(expr.name)) + return true; + if (expr instanceof AST_Dot && is_undeclared_ref(expr.expression) && is_pure_native_fn(expr.expression.name, expr.property)) { + return true; + } + } + if (this instanceof AST_New && compressor.option("pure_new")) { + return true; + } + if (compressor.option("side_effects") && has_annotation(this, _PURE)) { + return true; + } + return !compressor.pure_funcs(this); +}); +AST_Node.DEFMETHOD("is_call_pure", return_false); +AST_Dot.DEFMETHOD("is_call_pure", function(compressor) { + if (!compressor.option("unsafe")) + return; + const expr = this.expression; + let native_obj; + if (expr instanceof AST_Array) { + native_obj = "Array"; + } else if (expr.is_boolean()) { + native_obj = "Boolean"; + } else if (expr.is_number(compressor)) { + native_obj = "Number"; + } else if (expr instanceof AST_RegExp) { + native_obj = "RegExp"; + } else if (expr.is_string(compressor)) { + native_obj = "String"; + } else if (!this.may_throw_on_access(compressor)) { + native_obj = "Object"; + } + return native_obj != null && is_pure_native_method(native_obj, this.property); +}); +var aborts = (thing) => thing && thing.aborts(); +(function(def_aborts) { + def_aborts(AST_Statement, return_null); + def_aborts(AST_Jump, return_this); + function block_aborts() { + for (var i = 0;i < this.body.length; i++) { + if (aborts(this.body[i])) { + return this.body[i]; + } + } + return null; + } + def_aborts(AST_Import, return_null); + def_aborts(AST_BlockStatement, block_aborts); + def_aborts(AST_SwitchBranch, block_aborts); + def_aborts(AST_DefClass, function() { + for (const prop of this.properties) { + if (prop instanceof AST_ClassStaticBlock) { + if (prop.aborts()) + return prop; + } + } + return null; + }); + def_aborts(AST_ClassStaticBlock, block_aborts); + def_aborts(AST_If, function() { + return this.alternative && aborts(this.body) && aborts(this.alternative) && this; + }); +})(function(node, func) { + node.DEFMETHOD("aborts", func); +}); +AST_Node.DEFMETHOD("contains_this", function() { + return walk(this, (node) => { + if (node instanceof AST_This) + return walk_abort; + if (node !== this && node instanceof AST_Scope && !(node instanceof AST_Arrow)) { + return true; + } + }); +}); +function is_modified(compressor, tw, node, value, level, immutable) { + var parent = tw.parent(level); + var lhs = is_lhs(node, parent); + if (lhs) + return lhs; + if (!immutable && parent instanceof AST_Call && parent.expression === node && !(value instanceof AST_Arrow) && !(value instanceof AST_Class) && !parent.is_callee_pure(compressor) && (!(value instanceof AST_Function) || !(parent instanceof AST_New) && value.contains_this())) { + return true; + } + if (parent instanceof AST_Array) { + return is_modified(compressor, tw, parent, parent, level + 1); + } + if (parent instanceof AST_ObjectKeyVal && node === parent.value) { + var obj = tw.parent(level + 1); + return is_modified(compressor, tw, obj, obj, level + 2); + } + if (parent instanceof AST_PropAccess && parent.expression === node) { + var prop = read_property(value, parent.property); + return !immutable && is_modified(compressor, tw, parent, prop, level + 1); + } +} +function is_used_in_expression(tw) { + for (let p = -1, node, parent;node = tw.parent(p), parent = tw.parent(p + 1); p++) { + if (parent instanceof AST_Sequence) { + const nth_expression = parent.expressions.indexOf(node); + if (nth_expression !== parent.expressions.length - 1) { + const grandparent = tw.parent(p + 2); + if (parent.expressions.length > 2 || parent.expressions.length === 1 || !requires_sequence_to_maintain_binding(grandparent, parent, parent.expressions[1])) { + return false; + } + return true; + } else { + continue; + } + } + if (parent instanceof AST_Unary) { + const op = parent.operator; + if (op === "void") { + return false; + } + if (op === "typeof" || op === "+" || op === "-" || op === "!" || op === "~") { + continue; + } + } + if (parent instanceof AST_SimpleStatement || parent instanceof AST_LabeledStatement) { + return false; + } + if (parent instanceof AST_Scope) { + return false; + } + return true; + } + return true; +} + +// node_modules/terser/lib/compress/evaluate.js +function def_eval(node, func) { + node.DEFMETHOD("_eval", func); +} +var nullish = Symbol("This AST_Chain is nullish"); +AST_Node.DEFMETHOD("evaluate", function(compressor) { + if (!compressor.option("evaluate")) + return this; + var val = this._eval(compressor, 1); + if (!val || val instanceof RegExp) + return val; + if (typeof val == "function" || typeof val == "object" || val == nullish) + return this; + if (typeof val === "string") { + const unevaluated_size = this.size(compressor); + if (val.length + 2 > unevaluated_size) + return this; + } + return val; +}); +var unaryPrefix = makePredicate("! ~ - + void"); +AST_Node.DEFMETHOD("is_constant", function() { + if (this instanceof AST_Constant) { + return !(this instanceof AST_RegExp); + } else { + return this instanceof AST_UnaryPrefix && unaryPrefix.has(this.operator) && (this.expression instanceof AST_Constant || this.expression.is_constant()); + } +}); +def_eval(AST_Statement, function() { + throw new Error(string_template("Cannot evaluate a statement [{file}:{line},{col}]", this.start)); +}); +def_eval(AST_Lambda, return_this); +def_eval(AST_Class, return_this); +def_eval(AST_Node, return_this); +def_eval(AST_Constant, function() { + return this.getValue(); +}); +var supports_bigint = typeof BigInt === "function"; +def_eval(AST_BigInt, function() { + if (supports_bigint) { + return BigInt(this.value); + } else { + return this; + } +}); +def_eval(AST_RegExp, function(compressor) { + let evaluated = compressor.evaluated_regexps.get(this.value); + if (evaluated === undefined && regexp_is_safe(this.value.source)) { + try { + const { source, flags } = this.value; + evaluated = new RegExp(source, flags); + } catch (e) { + evaluated = null; + } + compressor.evaluated_regexps.set(this.value, evaluated); + } + return evaluated || this; +}); +def_eval(AST_TemplateString, function() { + if (this.segments.length !== 1) + return this; + return this.segments[0].value; +}); +def_eval(AST_Function, function(compressor) { + if (compressor.option("unsafe")) { + var fn = function() {}; + fn.node = this; + fn.toString = () => this.print_to_string(); + return fn; + } + return this; +}); +def_eval(AST_Array, function(compressor, depth) { + if (compressor.option("unsafe")) { + var elements = []; + for (var i = 0, len = this.elements.length;i < len; i++) { + var element = this.elements[i]; + var value = element._eval(compressor, depth); + if (element === value) + return this; + elements.push(value); + } + return elements; + } + return this; +}); +def_eval(AST_Object, function(compressor, depth) { + if (compressor.option("unsafe")) { + var val = {}; + for (var i = 0, len = this.properties.length;i < len; i++) { + var prop = this.properties[i]; + if (prop instanceof AST_Expansion) + return this; + var key = prop.key; + if (key instanceof AST_Symbol) { + key = key.name; + } else if (key instanceof AST_Node) { + key = key._eval(compressor, depth); + if (key === prop.key) + return this; + } + if (typeof Object.prototype[key] === "function") { + return this; + } + if (prop.value instanceof AST_Function) + continue; + val[key] = prop.value._eval(compressor, depth); + if (val[key] === prop.value) + return this; + } + return val; + } + return this; +}); +var non_converting_unary = makePredicate("! typeof void"); +def_eval(AST_UnaryPrefix, function(compressor, depth) { + var e = this.expression; + if (compressor.option("typeofs") && this.operator == "typeof") { + if (e instanceof AST_Lambda || e instanceof AST_SymbolRef && e.fixed_value() instanceof AST_Lambda) { + return "function"; + } + if ((e instanceof AST_Object || e instanceof AST_Array || e instanceof AST_SymbolRef && (e.fixed_value() instanceof AST_Object || e.fixed_value() instanceof AST_Array)) && !e.has_side_effects(compressor)) { + return "object"; + } + } + if (!non_converting_unary.has(this.operator)) + depth++; + e = e._eval(compressor, depth); + if (e === this.expression) + return this; + switch (this.operator) { + case "!": + return !e; + case "typeof": + if (e instanceof RegExp) + return this; + return typeof e; + case "void": + return; + case "~": + return ~e; + case "-": + return -e; + case "+": + return +e; + } + return this; +}); +var non_converting_binary = makePredicate("&& || ?? === !=="); +var identity_comparison = makePredicate("== != === !=="); +var has_identity = (value) => typeof value === "object" || typeof value === "function" || typeof value === "symbol"; +def_eval(AST_Binary, function(compressor, depth) { + if (!non_converting_binary.has(this.operator)) + depth++; + var left = this.left._eval(compressor, depth); + if (left === this.left) + return this; + var right = this.right._eval(compressor, depth); + if (right === this.right) + return this; + if (left != null && right != null && identity_comparison.has(this.operator) && has_identity(left) && has_identity(right) && typeof left === typeof right) { + return this; + } + if (typeof left === "bigint" !== (typeof right === "bigint") || typeof left === "bigint" && (this.operator === ">>>" || this.operator === "/" && Number(right) === 0)) { + return this; + } + var result; + switch (this.operator) { + case "&&": + result = left && right; + break; + case "||": + result = left || right; + break; + case "??": + result = left != null ? left : right; + break; + case "|": + result = left | right; + break; + case "&": + result = left & right; + break; + case "^": + result = left ^ right; + break; + case "+": + result = left + right; + break; + case "*": + result = left * right; + break; + case "**": + result = left ** right; + break; + case "/": + result = left / right; + break; + case "%": + result = left % right; + break; + case "-": + result = left - right; + break; + case "<<": + result = left << right; + break; + case ">>": + result = left >> right; + break; + case ">>>": + result = left >>> right; + break; + case "==": + result = left == right; + break; + case "===": + result = left === right; + break; + case "!=": + result = left != right; + break; + case "!==": + result = left !== right; + break; + case "<": + result = left < right; + break; + case "<=": + result = left <= right; + break; + case ">": + result = left > right; + break; + case ">=": + result = left >= right; + break; + default: + return this; + } + if (typeof result === "number" && isNaN(result) && compressor.find_parent(AST_With)) { + return this; + } + return result; +}); +def_eval(AST_Conditional, function(compressor, depth) { + var condition = this.condition._eval(compressor, depth); + if (condition === this.condition) + return this; + var node = condition ? this.consequent : this.alternative; + var value = node._eval(compressor, depth); + return value === node ? this : value; +}); +var reentrant_ref_eval = new Set; +def_eval(AST_SymbolRef, function(compressor, depth) { + if (reentrant_ref_eval.has(this)) + return this; + var fixed = this.fixed_value(); + if (!fixed) + return this; + reentrant_ref_eval.add(this); + const value = fixed._eval(compressor, depth); + reentrant_ref_eval.delete(this); + if (value === fixed) + return this; + if (value && typeof value == "object") { + var escaped = this.definition().escaped; + if (escaped && depth > escaped) + return this; + } + return value; +}); +var global_objs = { Array, Math, Number, Object, String }; +var regexp_flags = new Set([ + "dotAll", + "global", + "ignoreCase", + "multiline", + "sticky", + "unicode" +]); +def_eval(AST_PropAccess, function(compressor, depth) { + let obj = this.expression._eval(compressor, depth + 1); + if (obj === nullish || this.optional && obj == null) + return nullish; + if (this.property === "length") { + if (typeof obj === "string") { + return obj.length; + } + const is_spreadless_array = obj instanceof AST_Array && obj.elements.every((el) => !(el instanceof AST_Expansion)); + if (is_spreadless_array && obj.elements.every((el) => !el.has_side_effects(compressor))) { + return obj.elements.length; + } + } + if (compressor.option("unsafe")) { + var key = this.property; + if (key instanceof AST_Node) { + key = key._eval(compressor, depth); + if (key === this.property) + return this; + } + var exp = this.expression; + if (is_undeclared_ref(exp)) { + var aa; + var first_arg = exp.name === "hasOwnProperty" && key === "call" && (aa = compressor.parent() && compressor.parent().args) && (aa && aa[0] && aa[0].evaluate(compressor)); + first_arg = first_arg instanceof AST_Dot ? first_arg.expression : first_arg; + if (first_arg == null || first_arg.thedef && first_arg.thedef.undeclared) { + return this.clone(); + } + if (!is_pure_native_value(exp.name, key)) + return this; + obj = global_objs[exp.name]; + } else { + if (obj instanceof RegExp) { + if (key == "source") { + return regexp_source_fix(obj.source); + } else if (key == "flags" || regexp_flags.has(key)) { + return obj[key]; + } + } + if (!obj || obj === exp || !HOP(obj, key)) + return this; + if (typeof obj == "function") + switch (key) { + case "name": + return obj.node.name ? obj.node.name.name : ""; + case "length": + return obj.node.length_property(); + default: + return this; + } + } + return obj[key]; + } + return this; +}); +def_eval(AST_Chain, function(compressor, depth) { + const evaluated = this.expression._eval(compressor, depth); + return evaluated === nullish ? undefined : evaluated === this.expression ? this : evaluated; +}); +def_eval(AST_Call, function(compressor, depth) { + var exp = this.expression; + const callee = exp._eval(compressor, depth); + if (callee === nullish || this.optional && callee == null) + return nullish; + if (compressor.option("unsafe") && exp instanceof AST_PropAccess) { + var key = exp.property; + if (key instanceof AST_Node) { + key = key._eval(compressor, depth); + if (key === exp.property) + return this; + } + var val; + var e = exp.expression; + if (is_undeclared_ref(e)) { + var first_arg = e.name === "hasOwnProperty" && key === "call" && (this.args[0] && this.args[0].evaluate(compressor)); + first_arg = first_arg instanceof AST_Dot ? first_arg.expression : first_arg; + if (first_arg == null || first_arg.thedef && first_arg.thedef.undeclared) { + return this.clone(); + } + if (!is_pure_native_fn(e.name, key)) + return this; + val = global_objs[e.name]; + } else { + val = e._eval(compressor, depth + 1); + if (val === e || !val) + return this; + if (!is_pure_native_method(val.constructor.name, key)) + return this; + } + var args = []; + for (var i = 0, len = this.args.length;i < len; i++) { + var arg = this.args[i]; + var value = arg._eval(compressor, depth); + if (arg === value) + return this; + if (arg instanceof AST_Lambda) + return this; + args.push(value); + } + try { + return val[key].apply(val, args); + } catch (ex) {} + } + return this; +}); +def_eval(AST_New, return_this); + +// node_modules/terser/lib/compress/drop-side-effect-free.js +function def_drop_side_effect_free(node_or_nodes, func) { + for (const node of [].concat(node_or_nodes)) { + node.DEFMETHOD("drop_side_effect_free", func); + } +} +function trim(nodes, compressor, first_in_statement2) { + var len = nodes.length; + if (!len) + return null; + var ret = [], changed = false; + for (var i = 0;i < len; i++) { + var node = nodes[i].drop_side_effect_free(compressor, first_in_statement2); + changed |= node !== nodes[i]; + if (node) { + ret.push(node); + first_in_statement2 = false; + } + } + return changed ? ret.length ? ret : null : nodes; +} +def_drop_side_effect_free(AST_Node, return_this); +def_drop_side_effect_free(AST_Constant, return_null); +def_drop_side_effect_free(AST_This, return_null); +def_drop_side_effect_free(AST_Call, function(compressor, first_in_statement2) { + if (is_nullish_shortcircuited(this, compressor)) { + return this.expression.drop_side_effect_free(compressor, first_in_statement2); + } + if (!this.is_callee_pure(compressor)) { + if (this.expression.is_call_pure(compressor)) { + var exprs = this.args.slice(); + exprs.unshift(this.expression.expression); + exprs = trim(exprs, compressor, first_in_statement2); + return exprs && make_sequence(this, exprs); + } + if (is_func_expr(this.expression) && (!this.expression.name || !this.expression.name.definition().references.length)) { + var node = this.clone(); + node.expression.process_expression(false, compressor); + return node; + } + return this; + } + var args = trim(this.args, compressor, first_in_statement2); + return args && make_sequence(this, args); +}); +def_drop_side_effect_free(AST_Accessor, return_null); +def_drop_side_effect_free(AST_Function, return_null); +def_drop_side_effect_free(AST_Arrow, return_null); +def_drop_side_effect_free(AST_Class, function(compressor) { + const with_effects = []; + if (this.is_self_referential() && this.has_side_effects(compressor)) { + return this; + } + const trimmed_extends = this.extends && this.extends.drop_side_effect_free(compressor); + if (trimmed_extends) + with_effects.push(trimmed_extends); + for (const prop of this.properties) { + if (prop instanceof AST_ClassStaticBlock) { + if (prop.has_side_effects(compressor)) { + return this; + } + } else { + const trimmed_prop = prop.drop_side_effect_free(compressor); + if (trimmed_prop) + with_effects.push(trimmed_prop); + } + } + if (!with_effects.length) + return null; + const exprs = make_sequence(this, with_effects); + if (this instanceof AST_DefClass) { + return make_node(AST_SimpleStatement, this, { body: exprs }); + } else { + return exprs; + } +}); +def_drop_side_effect_free([ + AST_ClassProperty, + AST_ClassPrivateProperty +], function(compressor) { + const key = this.computed_key() && this.key.drop_side_effect_free(compressor); + const value = this.static && this.value && this.value.drop_side_effect_free(compressor); + if (key && value) + return make_sequence(this, [key, value]); + return key || value || null; +}); +def_drop_side_effect_free(AST_Binary, function(compressor, first_in_statement2) { + var right = this.right.drop_side_effect_free(compressor); + if (!right) + return this.left.drop_side_effect_free(compressor, first_in_statement2); + if (lazy_op.has(this.operator)) { + if (right === this.right) + return this; + var node = this.clone(); + node.right = right; + return node; + } else { + var left = this.left.drop_side_effect_free(compressor, first_in_statement2); + if (!left) + return this.right.drop_side_effect_free(compressor, first_in_statement2); + return make_sequence(this, [left, right]); + } +}); +def_drop_side_effect_free(AST_Assign, function(compressor) { + if (this.logical) + return this; + var left = this.left; + if (left.has_side_effects(compressor) || compressor.has_directive("use strict") && left instanceof AST_PropAccess && left.expression.is_constant()) { + return this; + } + set_flag(this, WRITE_ONLY); + while (left instanceof AST_PropAccess) { + left = left.expression; + } + if (left.is_constant_expression(compressor.find_parent(AST_Scope))) { + return this.right.drop_side_effect_free(compressor); + } + return this; +}); +def_drop_side_effect_free(AST_Conditional, function(compressor) { + var consequent = this.consequent.drop_side_effect_free(compressor); + var alternative = this.alternative.drop_side_effect_free(compressor); + if (consequent === this.consequent && alternative === this.alternative) + return this; + if (!consequent) + return alternative ? make_node(AST_Binary, this, { + operator: "||", + left: this.condition, + right: alternative + }) : this.condition.drop_side_effect_free(compressor); + if (!alternative) + return make_node(AST_Binary, this, { + operator: "&&", + left: this.condition, + right: consequent + }); + var node = this.clone(); + node.consequent = consequent; + node.alternative = alternative; + return node; +}); +def_drop_side_effect_free(AST_Unary, function(compressor, first_in_statement2) { + if (unary_side_effects.has(this.operator)) { + if (!this.expression.has_side_effects(compressor)) { + set_flag(this, WRITE_ONLY); + } else { + clear_flag(this, WRITE_ONLY); + } + return this; + } + if (this.operator == "typeof" && this.expression instanceof AST_SymbolRef) + return null; + var expression = this.expression.drop_side_effect_free(compressor, first_in_statement2); + if (first_in_statement2 && expression && is_iife_call(expression)) { + if (expression === this.expression && this.operator == "!") + return this; + return expression.negate(compressor, first_in_statement2); + } + return expression; +}); +def_drop_side_effect_free(AST_SymbolRef, function(compressor) { + const safe_access = this.is_declared(compressor) || pure_prop_access_globals.has(this.name); + return safe_access ? null : this; +}); +def_drop_side_effect_free(AST_Object, function(compressor, first_in_statement2) { + var values = trim(this.properties, compressor, first_in_statement2); + return values && make_sequence(this, values); +}); +def_drop_side_effect_free(AST_ObjectKeyVal, function(compressor, first_in_statement2) { + const computed_key = this.key instanceof AST_Node; + const key = computed_key && this.key.drop_side_effect_free(compressor, first_in_statement2); + const value = this.value.drop_side_effect_free(compressor, first_in_statement2); + if (key && value) { + return make_sequence(this, [key, value]); + } + return key || value; +}); +def_drop_side_effect_free([ + AST_ConciseMethod, + AST_ObjectGetter, + AST_ObjectSetter +], function() { + return this.computed_key() ? this.key : null; +}); +def_drop_side_effect_free([ + AST_PrivateMethod, + AST_PrivateGetter, + AST_PrivateSetter +], function() { + return null; +}); +def_drop_side_effect_free(AST_Array, function(compressor, first_in_statement2) { + var values = trim(this.elements, compressor, first_in_statement2); + return values && make_sequence(this, values); +}); +def_drop_side_effect_free(AST_Dot, function(compressor, first_in_statement2) { + if (is_nullish_shortcircuited(this, compressor)) { + return this.expression.drop_side_effect_free(compressor, first_in_statement2); + } + if (!this.optional && this.expression.may_throw_on_access(compressor)) { + return this; + } + return this.expression.drop_side_effect_free(compressor, first_in_statement2); +}); +def_drop_side_effect_free(AST_Sub, function(compressor, first_in_statement2) { + if (is_nullish_shortcircuited(this, compressor)) { + return this.expression.drop_side_effect_free(compressor, first_in_statement2); + } + if (!this.optional && this.expression.may_throw_on_access(compressor)) { + return this; + } + var property = this.property.drop_side_effect_free(compressor); + if (property && this.optional) + return this; + var expression = this.expression.drop_side_effect_free(compressor, first_in_statement2); + if (expression && property) + return make_sequence(this, [expression, property]); + return expression || property; +}); +def_drop_side_effect_free(AST_Chain, function(compressor, first_in_statement2) { + return this.expression.drop_side_effect_free(compressor, first_in_statement2); +}); +def_drop_side_effect_free(AST_Sequence, function(compressor) { + var last = this.tail_node(); + var expr = last.drop_side_effect_free(compressor); + if (expr === last) + return this; + var expressions = this.expressions.slice(0, -1); + if (expr) + expressions.push(expr); + if (!expressions.length) { + return make_node(AST_Number, this, { value: 0 }); + } + return make_sequence(this, expressions); +}); +def_drop_side_effect_free(AST_Expansion, function(compressor, first_in_statement2) { + return this.expression.drop_side_effect_free(compressor, first_in_statement2); +}); +def_drop_side_effect_free(AST_TemplateSegment, return_null); +def_drop_side_effect_free(AST_TemplateString, function(compressor) { + var values = trim(this.segments, compressor, first_in_statement); + return values && make_sequence(this, values); +}); + +// node_modules/terser/lib/compress/drop-unused.js +var r_keep_assign = /keep_assign/; +AST_Scope.DEFMETHOD("drop_unused", function(compressor) { + if (!compressor.option("unused")) + return; + if (compressor.has_directive("use asm")) + return; + if (!this.variables) + return; + var self2 = this; + if (self2.pinned()) + return; + var drop_funcs = !(self2 instanceof AST_Toplevel) || compressor.toplevel.funcs; + var drop_vars = !(self2 instanceof AST_Toplevel) || compressor.toplevel.vars; + const assign_as_unused = r_keep_assign.test(compressor.option("unused")) ? return_false : function(node) { + if (node instanceof AST_Assign && !node.logical && (has_flag(node, WRITE_ONLY) || node.operator == "=")) { + return node.left; + } + if (node instanceof AST_Unary && has_flag(node, WRITE_ONLY)) { + return node.expression; + } + }; + var in_use_ids = new Map; + var fixed_ids = new Map; + if (self2 instanceof AST_Toplevel && compressor.top_retain) { + self2.variables.forEach(function(def) { + if (compressor.top_retain(def)) { + in_use_ids.set(def.id, def); + } + }); + } + var var_defs_by_id = new Map; + var initializations = new Map; + var scope = this; + var tw = new TreeWalker(function(node, descend) { + if (node instanceof AST_Lambda && node.uses_arguments && !tw.has_directive("use strict")) { + node.argnames.forEach(function(argname) { + if (!(argname instanceof AST_SymbolDeclaration)) + return; + var def = argname.definition(); + in_use_ids.set(def.id, def); + }); + } + if (node === self2) + return; + if (node instanceof AST_Class && node.has_side_effects(compressor)) { + if (node.is_self_referential()) { + descend(); + } else { + node.visit_nondeferred_class_parts(tw); + } + } + if (node instanceof AST_Defun || node instanceof AST_DefClass) { + var node_def = node.name.definition(); + const in_export = tw.parent() instanceof AST_Export; + if (in_export || !drop_funcs && scope === self2) { + if (node_def.global) { + in_use_ids.set(node_def.id, node_def); + } + } + map_add(initializations, node_def.id, node); + return true; + } + const in_root_scope = scope === self2; + if (node instanceof AST_SymbolFunarg && in_root_scope) { + map_add(var_defs_by_id, node.definition().id, node); + } + if (node instanceof AST_Definitions && in_root_scope) { + const in_export = tw.parent() instanceof AST_Export; + node.definitions.forEach(function(def) { + if (def.name instanceof AST_SymbolVar) { + map_add(var_defs_by_id, def.name.definition().id, def); + } + if (in_export || !drop_vars) { + walk(def.name, (node2) => { + if (node2 instanceof AST_SymbolDeclaration) { + const def2 = node2.definition(); + if (def2.global) { + in_use_ids.set(def2.id, def2); + } + } + }); + } + if (def.name instanceof AST_Destructuring) { + def.walk(tw); + } + if (def.name instanceof AST_SymbolDeclaration && def.value) { + var node_def2 = def.name.definition(); + map_add(initializations, node_def2.id, def.value); + if (!node_def2.chained && def.name.fixed_value() === def.value) { + fixed_ids.set(node_def2.id, def); + } + if (def.value.has_side_effects(compressor)) { + def.value.walk(tw); + } + } + }); + return true; + } + return scan_ref_scoped(node, descend); + }); + self2.walk(tw); + tw = new TreeWalker(scan_ref_scoped); + in_use_ids.forEach(function(def) { + var init = initializations.get(def.id); + if (init) + init.forEach(function(init2) { + init2.walk(tw); + }); + }); + var tt = new TreeTransformer(function before(node, descend, in_list) { + var parent = tt.parent(); + if (drop_vars) { + const sym2 = assign_as_unused(node); + if (sym2 instanceof AST_SymbolRef) { + var def = sym2.definition(); + var in_use = in_use_ids.has(def.id); + if (node instanceof AST_Assign) { + if (!in_use || fixed_ids.has(def.id) && fixed_ids.get(def.id) !== node) { + const assignee = node.right.transform(tt); + if (!in_use && !assignee.has_side_effects(compressor) && !is_used_in_expression(tt)) { + return in_list ? MAP.skip : make_node(AST_Number, node, { value: 0 }); + } + return maintain_this_binding(parent, node, assignee); + } + } else if (!in_use) { + return in_list ? MAP.skip : make_node(AST_Number, node, { value: 0 }); + } + } + } + if (scope !== self2) + return; + var def; + if (node.name && (node instanceof AST_ClassExpression && !keep_name(compressor.option("keep_classnames"), (def = node.name.definition()).name) || node instanceof AST_Function && !keep_name(compressor.option("keep_fnames"), (def = node.name.definition()).name))) { + if (!in_use_ids.has(def.id) || def.orig.length > 1) + node.name = null; + } + if (node instanceof AST_Lambda && !(node instanceof AST_Accessor)) { + var trim2 = !compressor.option("keep_fargs") || parent instanceof AST_Call && parent.expression === node && !node.pinned() && (!node.name || node.name.unreferenced()); + for (var a = node.argnames, i = a.length;--i >= 0; ) { + var sym = a[i]; + if (sym instanceof AST_Expansion) { + sym = sym.expression; + } + if (sym instanceof AST_DefaultAssign) { + sym = sym.left; + } + if (!(sym instanceof AST_Destructuring) && !in_use_ids.has(sym.definition().id)) { + set_flag(sym, UNUSED); + if (trim2) { + a.pop(); + } + } else { + trim2 = false; + } + } + } + if (node instanceof AST_DefClass && node !== self2) { + const def2 = node.name.definition(); + descend(node, this); + const keep_class = def2.global && !drop_funcs || in_use_ids.has(def2.id); + if (!keep_class) { + const kept = node.drop_side_effect_free(compressor); + if (kept == null) { + def2.eliminated++; + return in_list ? MAP.skip : make_node(AST_EmptyStatement, node); + } + return kept; + } + return node; + } + if (node instanceof AST_Defun && node !== self2) { + const def2 = node.name.definition(); + const keep = def2.global && !drop_funcs || in_use_ids.has(def2.id); + if (!keep) { + def2.eliminated++; + return in_list ? MAP.skip : make_node(AST_EmptyStatement, node); + } + } + if (node instanceof AST_Definitions && !(parent instanceof AST_ForIn && parent.init === node)) { + var drop_block = !(parent instanceof AST_Toplevel) && !(node instanceof AST_Var); + var body = [], head = [], tail = []; + var side_effects = []; + node.definitions.forEach(function(def2) { + if (def2.value) + def2.value = def2.value.transform(tt); + var is_destructure = def2.name instanceof AST_Destructuring; + var sym2 = is_destructure ? new SymbolDef(null, { name: "" }) : def2.name.definition(); + if (drop_block && sym2.global) + return tail.push(def2); + if (!(drop_vars || drop_block) || is_destructure && (def2.name.names.length || def2.name.is_array || compressor.option("pure_getters") != true) || in_use_ids.has(sym2.id)) { + if (def2.value && fixed_ids.has(sym2.id) && fixed_ids.get(sym2.id) !== def2) { + def2.value = def2.value.drop_side_effect_free(compressor); + } + if (def2.name instanceof AST_SymbolVar) { + var var_defs = var_defs_by_id.get(sym2.id); + if (var_defs.length > 1 && (!def2.value || sym2.orig.indexOf(def2.name) > sym2.eliminated)) { + if (def2.value) { + var ref = make_node(AST_SymbolRef, def2.name, def2.name); + sym2.references.push(ref); + var assign = make_node(AST_Assign, def2, { + operator: "=", + logical: false, + left: ref, + right: def2.value + }); + if (fixed_ids.get(sym2.id) === def2) { + fixed_ids.set(sym2.id, assign); + } + side_effects.push(assign.transform(tt)); + } + remove(var_defs, def2); + sym2.eliminated++; + return; + } + } + if (def2.value) { + if (side_effects.length > 0) { + if (tail.length > 0) { + side_effects.push(def2.value); + def2.value = make_sequence(def2.value, side_effects); + } else { + body.push(make_node(AST_SimpleStatement, node, { + body: make_sequence(node, side_effects) + })); + } + side_effects = []; + } + tail.push(def2); + } else { + head.push(def2); + } + } else if (sym2.orig[0] instanceof AST_SymbolCatch) { + var value = def2.value && def2.value.drop_side_effect_free(compressor); + if (value) + side_effects.push(value); + def2.value = null; + head.push(def2); + } else { + var value = def2.value && def2.value.drop_side_effect_free(compressor); + if (value) { + side_effects.push(value); + } + sym2.eliminated++; + } + }); + if (head.length > 0 || tail.length > 0) { + node.definitions = head.concat(tail); + body.push(node); + } + if (side_effects.length > 0) { + body.push(make_node(AST_SimpleStatement, node, { + body: make_sequence(node, side_effects) + })); + } + switch (body.length) { + case 0: + return in_list ? MAP.skip : make_node(AST_EmptyStatement, node); + case 1: + return body[0]; + default: + return in_list ? MAP.splice(body) : make_node(AST_BlockStatement, node, { body }); + } + } + if (node instanceof AST_For) { + descend(node, this); + var block; + if (node.init instanceof AST_BlockStatement) { + block = node.init; + node.init = block.body.pop(); + block.body.push(node); + } + if (node.init instanceof AST_SimpleStatement) { + node.init = node.init.body; + } else if (is_empty(node.init)) { + node.init = null; + } + return !block ? node : in_list ? MAP.splice(block.body) : block; + } + if (node instanceof AST_LabeledStatement && node.body instanceof AST_For) { + descend(node, this); + if (node.body instanceof AST_BlockStatement) { + var block = node.body; + node.body = block.body.pop(); + block.body.push(node); + return in_list ? MAP.splice(block.body) : block; + } + return node; + } + if (node instanceof AST_BlockStatement) { + descend(node, this); + if (in_list && node.body.every(can_be_evicted_from_block)) { + return MAP.splice(node.body); + } + return node; + } + if (node instanceof AST_Scope && !(node instanceof AST_ClassStaticBlock)) { + const save_scope = scope; + scope = node; + descend(node, this); + scope = save_scope; + return node; + } + }, function after(node, in_list) { + if (node instanceof AST_Sequence) { + switch (node.expressions.length) { + case 0: + return in_list ? MAP.skip : make_node(AST_Number, node, { value: 0 }); + case 1: + return node.expressions[0]; + } + } + }); + self2.transform(tt); + function scan_ref_scoped(node, descend) { + var node_def; + const sym = assign_as_unused(node); + if (sym instanceof AST_SymbolRef && !is_ref_of(node.left, AST_SymbolBlockDeclaration) && self2.variables.get(sym.name) === (node_def = sym.definition())) { + if (node instanceof AST_Assign) { + node.right.walk(tw); + if (!node_def.chained && node.left.fixed_value() === node.right) { + fixed_ids.set(node_def.id, node); + } + } + return true; + } + if (node instanceof AST_SymbolRef) { + node_def = node.definition(); + if (!in_use_ids.has(node_def.id)) { + in_use_ids.set(node_def.id, node_def); + if (node_def.orig[0] instanceof AST_SymbolCatch) { + const redef = node_def.scope.is_block_scope() && node_def.scope.get_defun_scope().variables.get(node_def.name); + if (redef) + in_use_ids.set(redef.id, redef); + } + } + return true; + } + if (node instanceof AST_Class) { + descend(); + return true; + } + if (node instanceof AST_Scope && !(node instanceof AST_ClassStaticBlock)) { + var save_scope = scope; + scope = node; + descend(); + scope = save_scope; + return true; + } + } +}); + +// node_modules/terser/lib/compress/reduce-vars.js +function def_reduce_vars(node, func) { + node.DEFMETHOD("reduce_vars", func); +} +def_reduce_vars(AST_Node, noop); +function reset_def(compressor, def) { + def.assignments = 0; + def.chained = false; + def.direct_access = false; + def.escaped = 0; + def.recursive_refs = 0; + def.references = []; + def.single_use = undefined; + if (def.scope.pinned() || def.orig[0] instanceof AST_SymbolFunarg && def.scope.uses_arguments) { + def.fixed = false; + } else if (def.orig[0] instanceof AST_SymbolConst || !compressor.exposed(def)) { + def.fixed = def.init; + } else { + def.fixed = false; + } +} +function reset_variables(tw, compressor, node) { + node.variables.forEach(function(def) { + reset_def(compressor, def); + if (def.fixed === null) { + tw.defs_to_safe_ids.set(def.id, tw.safe_ids); + mark(tw, def, true); + } else if (def.fixed) { + tw.loop_ids.set(def.id, tw.in_loop); + mark(tw, def, true); + } + }); +} +function reset_block_variables(compressor, node) { + if (node.block_scope) + node.block_scope.variables.forEach((def) => { + reset_def(compressor, def); + }); +} +function push(tw) { + tw.safe_ids = Object.create(tw.safe_ids); +} +function pop(tw) { + tw.safe_ids = Object.getPrototypeOf(tw.safe_ids); +} +function mark(tw, def, safe) { + tw.safe_ids[def.id] = safe; +} +function safe_to_read(tw, def) { + if (def.single_use == "m") + return false; + if (tw.safe_ids[def.id]) { + if (def.fixed == null) { + var orig = def.orig[0]; + if (orig instanceof AST_SymbolFunarg || orig.name == "arguments") + return false; + def.fixed = make_node(AST_Undefined, orig); + } + return true; + } + return def.fixed instanceof AST_Defun; +} +function safe_to_assign(tw, def, scope, value) { + if (def.fixed === undefined) + return true; + let def_safe_ids; + if (def.fixed === null && (def_safe_ids = tw.defs_to_safe_ids.get(def.id))) { + def_safe_ids[def.id] = false; + tw.defs_to_safe_ids.delete(def.id); + return true; + } + if (!HOP(tw.safe_ids, def.id)) + return false; + if (!safe_to_read(tw, def)) + return false; + if (def.fixed === false) + return false; + if (def.fixed != null && (!value || def.references.length > def.assignments)) + return false; + if (def.fixed instanceof AST_Defun) { + return value instanceof AST_Node && def.fixed.parent_scope === scope; + } + return def.orig.every((sym) => { + return !(sym instanceof AST_SymbolConst || sym instanceof AST_SymbolDefun || sym instanceof AST_SymbolLambda); + }); +} +function ref_once(tw, compressor, def) { + return compressor.option("unused") && !def.scope.pinned() && def.references.length - def.recursive_refs == 1 && tw.loop_ids.get(def.id) === tw.in_loop; +} +function is_immutable(value) { + if (!value) + return false; + return value.is_constant() || value instanceof AST_Lambda || value instanceof AST_This; +} +function mark_escaped(tw, d, scope, node, value, level = 0, depth = 1) { + var parent = tw.parent(level); + if (value) { + if (value.is_constant()) + return; + if (value instanceof AST_ClassExpression) + return; + } + if (parent instanceof AST_Assign && (parent.operator === "=" || parent.logical) && node === parent.right || parent instanceof AST_Call && (node !== parent.expression || parent instanceof AST_New) || parent instanceof AST_Exit && node === parent.value && node.scope !== d.scope || parent instanceof AST_VarDef && node === parent.value || parent instanceof AST_Yield && node === parent.value && node.scope !== d.scope) { + if (depth > 1 && !(value && value.is_constant_expression(scope))) + depth = 1; + if (!d.escaped || d.escaped > depth) + d.escaped = depth; + return; + } else if (parent instanceof AST_Array || parent instanceof AST_Await || parent instanceof AST_Binary && lazy_op.has(parent.operator) || parent instanceof AST_Conditional && node !== parent.condition || parent instanceof AST_Expansion || parent instanceof AST_Sequence && node === parent.tail_node()) { + mark_escaped(tw, d, scope, parent, parent, level + 1, depth); + } else if (parent instanceof AST_ObjectKeyVal && node === parent.value) { + var obj = tw.parent(level + 1); + mark_escaped(tw, d, scope, obj, obj, level + 2, depth); + } else if (parent instanceof AST_PropAccess && node === parent.expression) { + value = read_property(value, parent.property); + mark_escaped(tw, d, scope, parent, value, level + 1, depth + 1); + if (value) + return; + } + if (level > 0) + return; + if (parent instanceof AST_Sequence && node !== parent.tail_node()) + return; + if (parent instanceof AST_SimpleStatement) + return; + d.direct_access = true; +} +var suppress = (node) => walk(node, (node2) => { + if (!(node2 instanceof AST_Symbol)) + return; + var d = node2.definition(); + if (!d) + return; + if (node2 instanceof AST_SymbolRef) + d.references.push(node2); + d.fixed = false; +}); +def_reduce_vars(AST_Accessor, function(tw, descend, compressor) { + push(tw); + reset_variables(tw, compressor, this); + descend(); + pop(tw); + return true; +}); +def_reduce_vars(AST_Assign, function(tw, descend, compressor) { + var node = this; + if (node.left instanceof AST_Destructuring) { + suppress(node.left); + return; + } + const finish_walk = () => { + if (node.logical) { + node.left.walk(tw); + push(tw); + node.right.walk(tw); + pop(tw); + return true; + } + }; + var sym = node.left; + if (!(sym instanceof AST_SymbolRef)) + return finish_walk(); + var def = sym.definition(); + var safe = safe_to_assign(tw, def, sym.scope, node.right); + def.assignments++; + if (!safe) + return finish_walk(); + var fixed = def.fixed; + if (!fixed && node.operator != "=" && !node.logical) + return finish_walk(); + var eq = node.operator == "="; + var value = eq ? node.right : node; + if (is_modified(compressor, tw, node, value, 0)) + return finish_walk(); + def.references.push(sym); + if (!node.logical) { + if (!eq) + def.chained = true; + def.fixed = eq ? function() { + return node.right; + } : function() { + return make_node(AST_Binary, node, { + operator: node.operator.slice(0, -1), + left: fixed instanceof AST_Node ? fixed : fixed(), + right: node.right + }); + }; + } + if (node.logical) { + mark(tw, def, false); + push(tw); + node.right.walk(tw); + pop(tw); + return true; + } + mark(tw, def, false); + node.right.walk(tw); + mark(tw, def, true); + mark_escaped(tw, def, sym.scope, node, value, 0, 1); + return true; +}); +def_reduce_vars(AST_Binary, function(tw) { + if (!lazy_op.has(this.operator)) + return; + this.left.walk(tw); + push(tw); + this.right.walk(tw); + pop(tw); + return true; +}); +def_reduce_vars(AST_Block, function(tw, descend, compressor) { + reset_block_variables(compressor, this); +}); +def_reduce_vars(AST_Case, function(tw) { + push(tw); + this.expression.walk(tw); + pop(tw); + push(tw); + walk_body(this, tw); + pop(tw); + return true; +}); +def_reduce_vars(AST_Class, function(tw, descend) { + clear_flag(this, INLINED); + push(tw); + descend(); + pop(tw); + return true; +}); +def_reduce_vars(AST_ClassStaticBlock, function(tw, descend, compressor) { + reset_block_variables(compressor, this); +}); +def_reduce_vars(AST_Conditional, function(tw) { + this.condition.walk(tw); + push(tw); + this.consequent.walk(tw); + pop(tw); + push(tw); + this.alternative.walk(tw); + pop(tw); + return true; +}); +def_reduce_vars(AST_Chain, function(tw, descend) { + const safe_ids = tw.safe_ids; + descend(); + tw.safe_ids = safe_ids; + return true; +}); +def_reduce_vars(AST_Call, function(tw) { + this.expression.walk(tw); + if (this.optional) { + push(tw); + } + for (const arg of this.args) + arg.walk(tw); + return true; +}); +def_reduce_vars(AST_PropAccess, function(tw) { + if (!this.optional) + return; + this.expression.walk(tw); + push(tw); + if (this.property instanceof AST_Node) + this.property.walk(tw); + return true; +}); +def_reduce_vars(AST_Default, function(tw, descend) { + push(tw); + descend(); + pop(tw); + return true; +}); +function mark_lambda(tw, descend, compressor) { + clear_flag(this, INLINED); + push(tw); + reset_variables(tw, compressor, this); + var iife; + if (!this.name && !this.uses_arguments && !this.pinned() && (iife = tw.parent()) instanceof AST_Call && iife.expression === this && !iife.args.some((arg) => arg instanceof AST_Expansion) && this.argnames.every((arg_name) => arg_name instanceof AST_Symbol)) { + this.argnames.forEach((arg, i) => { + if (!arg.definition) + return; + var d = arg.definition(); + if (d.orig.length > 1) + return; + if (d.fixed === undefined && (!this.uses_arguments || tw.has_directive("use strict"))) { + d.fixed = function() { + return iife.args[i] || make_node(AST_Undefined, iife); + }; + tw.loop_ids.set(d.id, tw.in_loop); + mark(tw, d, true); + } else { + d.fixed = false; + } + }); + } + descend(); + pop(tw); + handle_defined_after_hoist(this); + return true; +} +function handle_defined_after_hoist(parent) { + const defuns = []; + walk(parent, (node) => { + if (node === parent) + return; + if (node instanceof AST_Defun) { + defuns.push(node); + return true; + } + if (node instanceof AST_Scope || node instanceof AST_SimpleStatement) + return true; + }); + const defun_dependencies_map = new Map; + const dependencies_map = new Map; + const symbols_of_interest = new Set; + const defuns_of_interest = new Set; + for (const defun of defuns) { + const fname_def = defun.name.definition(); + const enclosing_defs = []; + for (const def of defun.enclosed) { + if (def.fixed === false || def === fname_def || def.scope.get_defun_scope() !== parent) { + continue; + } + symbols_of_interest.add(def.id); + if (def.assignments === 0 && def.orig.length === 1 && def.orig[0] instanceof AST_SymbolDefun) { + defuns_of_interest.add(def.id); + symbols_of_interest.add(def.id); + defuns_of_interest.add(fname_def.id); + symbols_of_interest.add(fname_def.id); + if (!defun_dependencies_map.has(fname_def.id)) { + defun_dependencies_map.set(fname_def.id, []); + } + defun_dependencies_map.get(fname_def.id).push(def.id); + continue; + } + enclosing_defs.push(def); + } + if (enclosing_defs.length) { + dependencies_map.set(fname_def.id, enclosing_defs); + defuns_of_interest.add(fname_def.id); + symbols_of_interest.add(fname_def.id); + } + } + if (!dependencies_map.size) { + return; + } + let symbol_index = 1; + const defun_first_read_map = new Map; + const symbol_last_write_map = new Map; + walk_parent(parent, (node, walk_info) => { + if (node instanceof AST_Symbol && node.thedef) { + const id = node.definition().id; + symbol_index++; + if (symbols_of_interest.has(id)) { + if (node instanceof AST_SymbolDeclaration || is_lhs(node, walk_info.parent())) { + symbol_last_write_map.set(id, symbol_index); + } + } + if (defuns_of_interest.has(id)) { + if (!defun_first_read_map.has(id) && !is_recursive_ref(walk_info, id)) { + defun_first_read_map.set(id, symbol_index); + } + } + } + }); + for (const [defun, defun_first_read] of defun_first_read_map) { + const queue = new Set(defun_dependencies_map.get(defun)); + for (const enclosed_defun of queue) { + let enclosed_defun_first_read = defun_first_read_map.get(enclosed_defun); + if (enclosed_defun_first_read != null && enclosed_defun_first_read < defun_first_read) { + continue; + } + defun_first_read_map.set(enclosed_defun, defun_first_read); + for (const enclosed_enclosed_defun of defun_dependencies_map.get(enclosed_defun) || []) { + queue.add(enclosed_enclosed_defun); + } + } + } + for (const [defun, defs] of dependencies_map) { + const defun_first_read = defun_first_read_map.get(defun); + if (defun_first_read === undefined) { + continue; + } + for (const def of defs) { + if (def.fixed === false) { + continue; + } + let def_last_write = symbol_last_write_map.get(def.id) || 0; + if (defun_first_read < def_last_write) { + def.fixed = false; + } + } + } +} +def_reduce_vars(AST_Lambda, mark_lambda); +def_reduce_vars(AST_Do, function(tw, descend, compressor) { + reset_block_variables(compressor, this); + const saved_loop = tw.in_loop; + tw.in_loop = this; + push(tw); + this.body.walk(tw); + if (has_break_or_continue(this)) { + pop(tw); + push(tw); + } + this.condition.walk(tw); + pop(tw); + tw.in_loop = saved_loop; + return true; +}); +def_reduce_vars(AST_For, function(tw, descend, compressor) { + reset_block_variables(compressor, this); + if (this.init) + this.init.walk(tw); + const saved_loop = tw.in_loop; + tw.in_loop = this; + push(tw); + if (this.condition) + this.condition.walk(tw); + this.body.walk(tw); + if (this.step) { + if (has_break_or_continue(this)) { + pop(tw); + push(tw); + } + this.step.walk(tw); + } + pop(tw); + tw.in_loop = saved_loop; + return true; +}); +def_reduce_vars(AST_ForIn, function(tw, descend, compressor) { + reset_block_variables(compressor, this); + suppress(this.init); + this.object.walk(tw); + const saved_loop = tw.in_loop; + tw.in_loop = this; + push(tw); + this.body.walk(tw); + pop(tw); + tw.in_loop = saved_loop; + return true; +}); +def_reduce_vars(AST_If, function(tw) { + this.condition.walk(tw); + push(tw); + this.body.walk(tw); + pop(tw); + if (this.alternative) { + push(tw); + this.alternative.walk(tw); + pop(tw); + } + return true; +}); +def_reduce_vars(AST_LabeledStatement, function(tw) { + push(tw); + this.body.walk(tw); + pop(tw); + return true; +}); +def_reduce_vars(AST_SymbolCatch, function() { + this.definition().fixed = false; +}); +def_reduce_vars(AST_SymbolRef, function(tw, descend, compressor) { + var d = this.definition(); + d.references.push(this); + if (d.references.length == 1 && !d.fixed && d.orig[0] instanceof AST_SymbolDefun) { + tw.loop_ids.set(d.id, tw.in_loop); + } + var fixed_value; + if (d.fixed === undefined || !safe_to_read(tw, d)) { + d.fixed = false; + } else if (d.fixed) { + fixed_value = this.fixed_value(); + if (fixed_value instanceof AST_Lambda && is_recursive_ref(tw, d)) { + d.recursive_refs++; + } else if (fixed_value && !compressor.exposed(d) && ref_once(tw, compressor, d)) { + d.single_use = fixed_value instanceof AST_Lambda && !fixed_value.pinned() || fixed_value instanceof AST_Class || d.scope === this.scope && fixed_value.is_constant_expression(); + } else { + d.single_use = false; + } + if (is_modified(compressor, tw, this, fixed_value, 0, is_immutable(fixed_value))) { + if (d.single_use) { + d.single_use = "m"; + } else { + d.fixed = false; + } + } + } + mark_escaped(tw, d, this.scope, this, fixed_value, 0, 1); +}); +def_reduce_vars(AST_Toplevel, function(tw, descend, compressor) { + this.globals.forEach(function(def) { + reset_def(compressor, def); + }); + reset_variables(tw, compressor, this); + descend(); + handle_defined_after_hoist(this); + return true; +}); +def_reduce_vars(AST_Try, function(tw, descend, compressor) { + reset_block_variables(compressor, this); + push(tw); + this.body.walk(tw); + pop(tw); + if (this.bcatch) { + push(tw); + this.bcatch.walk(tw); + pop(tw); + } + if (this.bfinally) + this.bfinally.walk(tw); + return true; +}); +def_reduce_vars(AST_Unary, function(tw) { + var node = this; + if (node.operator !== "++" && node.operator !== "--") + return; + var exp = node.expression; + if (!(exp instanceof AST_SymbolRef)) + return; + var def = exp.definition(); + var safe = safe_to_assign(tw, def, exp.scope, true); + def.assignments++; + if (!safe) + return; + var fixed = def.fixed; + if (!fixed) + return; + def.references.push(exp); + def.chained = true; + def.fixed = function() { + return make_node(AST_Binary, node, { + operator: node.operator.slice(0, -1), + left: make_node(AST_UnaryPrefix, node, { + operator: "+", + expression: fixed instanceof AST_Node ? fixed : fixed() + }), + right: make_node(AST_Number, node, { + value: 1 + }) + }); + }; + mark(tw, def, true); + return true; +}); +def_reduce_vars(AST_VarDef, function(tw, descend) { + var node = this; + if (node.name instanceof AST_Destructuring) { + suppress(node.name); + return; + } + var d = node.name.definition(); + if (node.value) { + if (safe_to_assign(tw, d, node.name.scope, node.value)) { + d.fixed = function() { + return node.value; + }; + tw.loop_ids.set(d.id, tw.in_loop); + mark(tw, d, false); + descend(); + mark(tw, d, true); + return true; + } else { + d.fixed = false; + } + } +}); +def_reduce_vars(AST_While, function(tw, descend, compressor) { + reset_block_variables(compressor, this); + const saved_loop = tw.in_loop; + tw.in_loop = this; + push(tw); + descend(); + pop(tw); + tw.in_loop = saved_loop; + return true; +}); + +// node_modules/terser/lib/compress/tighten-body.js +function loop_body(x) { + if (x instanceof AST_IterationStatement) { + return x.body instanceof AST_BlockStatement ? x.body : x; + } + return x; +} +function is_lhs_read_only(lhs) { + if (lhs instanceof AST_This) + return true; + if (lhs instanceof AST_SymbolRef) + return lhs.definition().orig[0] instanceof AST_SymbolLambda; + if (lhs instanceof AST_PropAccess) { + lhs = lhs.expression; + if (lhs instanceof AST_SymbolRef) { + if (lhs.is_immutable()) + return false; + lhs = lhs.fixed_value(); + } + if (!lhs) + return true; + if (lhs instanceof AST_RegExp) + return false; + if (lhs instanceof AST_Constant) + return true; + return is_lhs_read_only(lhs); + } + return false; +} +function remove_initializers(var_statement) { + var decls = []; + var_statement.definitions.forEach(function(def) { + if (def.name instanceof AST_SymbolDeclaration) { + def.value = null; + decls.push(def); + } else { + def.declarations_as_names().forEach((name) => { + decls.push(make_node(AST_VarDef, def, { + name, + value: null + })); + }); + } + }); + return decls.length ? make_node(AST_Var, var_statement, { definitions: decls }) : null; +} +function trim_unreachable_code(compressor, stat, target) { + walk(stat, (node) => { + if (node instanceof AST_Var) { + const no_initializers = remove_initializers(node); + if (no_initializers) + target.push(no_initializers); + return true; + } + if (node instanceof AST_Defun && (node === stat || !compressor.has_directive("use strict"))) { + target.push(node === stat ? node : make_node(AST_Var, node, { + definitions: [ + make_node(AST_VarDef, node, { + name: make_node(AST_SymbolVar, node.name, node.name), + value: null + }) + ] + })); + return true; + } + if (node instanceof AST_Export || node instanceof AST_Import) { + target.push(node); + return true; + } + if (node instanceof AST_Scope) { + return true; + } + }); +} +function tighten_body(statements, compressor) { + const nearest_scope = compressor.find_scope(); + const defun_scope = nearest_scope.get_defun_scope(); + const { in_loop, in_try } = find_loop_scope_try(); + var CHANGED, max_iter = 10; + do { + CHANGED = false; + eliminate_spurious_blocks(statements); + if (compressor.option("dead_code")) { + eliminate_dead_code(statements, compressor); + } + if (compressor.option("if_return")) { + handle_if_return(statements, compressor); + } + if (compressor.sequences_limit > 0) { + sequencesize(statements, compressor); + sequencesize_2(statements, compressor); + } + if (compressor.option("join_vars")) { + join_consecutive_vars(statements); + } + if (compressor.option("collapse_vars")) { + collapse(statements, compressor); + } + } while (CHANGED && max_iter-- > 0); + function find_loop_scope_try() { + var node = compressor.self(), level = 0, in_loop2 = false, in_try2 = false; + do { + if (node instanceof AST_IterationStatement) { + in_loop2 = true; + } else if (node instanceof AST_Scope) { + break; + } else if (node instanceof AST_TryBlock) { + in_try2 = true; + } + } while (node = compressor.parent(level++)); + return { in_loop: in_loop2, in_try: in_try2 }; + } + function collapse(statements2, compressor2) { + if (nearest_scope.pinned() || defun_scope.pinned()) + return statements2; + var args; + var candidates = []; + var stat_index = statements2.length; + var scanner = new TreeTransformer(function(node) { + if (abort) + return node; + if (!hit) { + if (node !== hit_stack[hit_index]) + return node; + hit_index++; + if (hit_index < hit_stack.length) + return handle_custom_scan_order(node); + hit = true; + stop_after = find_stop(node, 0); + if (stop_after === node) + abort = true; + return node; + } + var parent = scanner.parent(); + if (node instanceof AST_Assign && (node.logical || node.operator != "=" && lhs.equivalent_to(node.left)) || node instanceof AST_Await || node instanceof AST_Call && lhs instanceof AST_PropAccess && lhs.equivalent_to(node.expression) || (node instanceof AST_Call || node instanceof AST_PropAccess) && node.optional || node instanceof AST_Debugger || node instanceof AST_Destructuring || node instanceof AST_Expansion && node.expression instanceof AST_Symbol && (node.expression instanceof AST_This || node.expression.definition().references.length > 1) || node instanceof AST_IterationStatement && !(node instanceof AST_For) || node instanceof AST_LoopControl || node instanceof AST_Try || node instanceof AST_With || node instanceof AST_Yield || node instanceof AST_Export || node instanceof AST_Class || parent instanceof AST_For && node !== parent.init || !replace_all && (node instanceof AST_SymbolRef && !node.is_declared(compressor2) && !pure_prop_access_globals.has(node)) || node instanceof AST_SymbolRef && parent instanceof AST_Call && has_annotation(parent, _NOINLINE) || node instanceof AST_ObjectProperty && node.key instanceof AST_Node) { + abort = true; + return node; + } + if (!stop_if_hit && (!lhs_local || !replace_all) && (parent instanceof AST_Binary && lazy_op.has(parent.operator) && parent.left !== node || parent instanceof AST_Conditional && parent.condition !== node || parent instanceof AST_If && parent.condition !== node)) { + stop_if_hit = parent; + } + if (can_replace && !(node instanceof AST_SymbolDeclaration) && lhs.equivalent_to(node) && !shadows(scanner.find_scope() || nearest_scope, lvalues)) { + if (stop_if_hit) { + abort = true; + return node; + } + if (is_lhs(node, parent)) { + if (value_def) + replaced++; + return node; + } else { + replaced++; + if (value_def && candidate instanceof AST_VarDef) + return node; + } + CHANGED = abort = true; + if (candidate instanceof AST_UnaryPostfix) { + return make_node(AST_UnaryPrefix, candidate, candidate); + } + if (candidate instanceof AST_VarDef) { + var def2 = candidate.name.definition(); + var value = candidate.value; + if (def2.references.length - def2.replaced == 1 && !compressor2.exposed(def2)) { + def2.replaced++; + if (funarg && is_identifier_atom(value)) { + return value.transform(compressor2); + } else { + return maintain_this_binding(parent, node, value); + } + } + return make_node(AST_Assign, candidate, { + operator: "=", + logical: false, + left: make_node(AST_SymbolRef, candidate.name, candidate.name), + right: value + }); + } + clear_flag(candidate, WRITE_ONLY); + return candidate; + } + var sym; + if (node instanceof AST_Call || node instanceof AST_Exit && (side_effects || lhs instanceof AST_PropAccess || may_modify(lhs)) || node instanceof AST_PropAccess && (side_effects || node.expression.may_throw_on_access(compressor2)) || node instanceof AST_SymbolRef && (lvalues.has(node.name) && lvalues.get(node.name).modified || side_effects && may_modify(node)) || node instanceof AST_VarDef && node.value && (lvalues.has(node.name.name) || side_effects && may_modify(node.name)) || (sym = is_lhs(node.left, node)) && (sym instanceof AST_PropAccess || lvalues.has(sym.name)) || may_throw && (in_try ? node.has_side_effects(compressor2) : side_effects_external(node))) { + stop_after = node; + if (node instanceof AST_Scope) + abort = true; + } + return handle_custom_scan_order(node); + }, function(node) { + if (abort) + return; + if (stop_after === node) + abort = true; + if (stop_if_hit === node) + stop_if_hit = null; + }); + var multi_replacer = new TreeTransformer(function(node) { + if (abort) + return node; + if (!hit) { + if (node !== hit_stack[hit_index]) + return node; + hit_index++; + if (hit_index < hit_stack.length) + return; + hit = true; + return node; + } + if (node instanceof AST_SymbolRef && node.name == def.name) { + if (!--replaced) + abort = true; + if (is_lhs(node, multi_replacer.parent())) + return node; + def.replaced++; + value_def.replaced--; + return candidate.value; + } + if (node instanceof AST_Default || node instanceof AST_Scope) + return node; + }); + while (--stat_index >= 0) { + if (stat_index == 0 && compressor2.option("unused")) + extract_args(); + var hit_stack = []; + extract_candidates(statements2[stat_index]); + while (candidates.length > 0) { + hit_stack = candidates.pop(); + var hit_index = 0; + var candidate = hit_stack[hit_stack.length - 1]; + var value_def = null; + var stop_after = null; + var stop_if_hit = null; + var lhs = get_lhs(candidate); + if (!lhs || is_lhs_read_only(lhs) || lhs.has_side_effects(compressor2)) + continue; + var lvalues = get_lvalues(candidate); + var lhs_local = is_lhs_local(lhs); + if (lhs instanceof AST_SymbolRef) { + lvalues.set(lhs.name, { def: lhs.definition(), modified: false }); + } + var side_effects = value_has_side_effects(candidate); + var replace_all = replace_all_symbols(); + var may_throw = candidate.may_throw(compressor2); + var funarg = candidate.name instanceof AST_SymbolFunarg; + var hit = funarg; + var abort = false, replaced = 0, can_replace = !args || !hit; + if (!can_replace) { + for (let j = compressor2.self().argnames.lastIndexOf(candidate.name) + 1;!abort && j < args.length; j++) { + args[j].transform(scanner); + } + can_replace = true; + } + for (var i = stat_index;!abort && i < statements2.length; i++) { + statements2[i].transform(scanner); + } + if (value_def) { + var def = candidate.name.definition(); + if (abort && def.references.length - def.replaced > replaced) + replaced = false; + else { + abort = false; + hit_index = 0; + hit = funarg; + for (var i = stat_index;!abort && i < statements2.length; i++) { + statements2[i].transform(multi_replacer); + } + value_def.single_use = false; + } + } + if (replaced && !remove_candidate(candidate)) + statements2.splice(stat_index, 1); + } + } + function handle_custom_scan_order(node) { + if (node instanceof AST_Scope) + return node; + if (node instanceof AST_Switch) { + node.expression = node.expression.transform(scanner); + for (var i2 = 0, len = node.body.length;!abort && i2 < len; i2++) { + var branch = node.body[i2]; + if (branch instanceof AST_Case) { + if (!hit) { + if (branch !== hit_stack[hit_index]) + continue; + hit_index++; + } + branch.expression = branch.expression.transform(scanner); + if (!replace_all) + break; + } + } + abort = true; + return node; + } + } + function redefined_within_scope(def2, scope) { + if (def2.global) + return false; + let cur_scope = def2.scope; + while (cur_scope && cur_scope !== scope) { + if (cur_scope.variables.has(def2.name)) { + return true; + } + cur_scope = cur_scope.parent_scope; + } + return false; + } + function has_overlapping_symbol(fn, arg, fn_strict) { + var found = false, scan_this = !(fn instanceof AST_Arrow); + arg.walk(new TreeWalker(function(node, descend) { + if (found) + return true; + if (node instanceof AST_SymbolRef && (fn.variables.has(node.name) || redefined_within_scope(node.definition(), fn))) { + var s = node.definition().scope; + if (s !== defun_scope) + while (s = s.parent_scope) { + if (s === defun_scope) + return true; + } + return found = true; + } + if ((fn_strict || scan_this) && node instanceof AST_This) { + return found = true; + } + if (node instanceof AST_Scope && !(node instanceof AST_Arrow)) { + var prev = scan_this; + scan_this = false; + descend(); + scan_this = prev; + return true; + } + })); + return found; + } + function arg_is_injectable(arg) { + if (arg instanceof AST_Expansion) + return false; + const contains_await = walk(arg, (node) => { + if (node instanceof AST_Await) + return walk_abort; + }); + if (contains_await) + return false; + return true; + } + function extract_args() { + var iife, fn = compressor2.self(); + if (is_func_expr(fn) && !fn.name && !fn.uses_arguments && !fn.pinned() && (iife = compressor2.parent()) instanceof AST_Call && iife.expression === fn && iife.args.every(arg_is_injectable)) { + var fn_strict = compressor2.has_directive("use strict"); + if (fn_strict && !member(fn_strict, fn.body)) + fn_strict = false; + var len = fn.argnames.length; + args = iife.args.slice(len); + var names = new Set; + for (var i2 = len;--i2 >= 0; ) { + var sym = fn.argnames[i2]; + var arg = iife.args[i2]; + const def2 = sym.definition && sym.definition(); + const is_reassigned = def2 && def2.orig.length > 1; + if (is_reassigned) + continue; + args.unshift(make_node(AST_VarDef, sym, { + name: sym, + value: arg + })); + if (names.has(sym.name)) + continue; + names.add(sym.name); + if (sym instanceof AST_Expansion) { + var elements = iife.args.slice(i2); + if (elements.every((arg2) => !has_overlapping_symbol(fn, arg2, fn_strict))) { + candidates.unshift([make_node(AST_VarDef, sym, { + name: sym.expression, + value: make_node(AST_Array, iife, { + elements + }) + })]); + } + } else { + if (!arg) { + arg = make_node(AST_Undefined, sym).transform(compressor2); + } else if (arg instanceof AST_Lambda && arg.pinned() || has_overlapping_symbol(fn, arg, fn_strict)) { + arg = null; + } + if (arg) + candidates.unshift([make_node(AST_VarDef, sym, { + name: sym, + value: arg + })]); + } + } + } + } + function extract_candidates(expr) { + hit_stack.push(expr); + if (expr instanceof AST_Assign) { + if (!expr.left.has_side_effects(compressor2) && !(expr.right instanceof AST_Chain)) { + candidates.push(hit_stack.slice()); + } + extract_candidates(expr.right); + } else if (expr instanceof AST_Binary) { + extract_candidates(expr.left); + extract_candidates(expr.right); + } else if (expr instanceof AST_Call && !has_annotation(expr, _NOINLINE)) { + extract_candidates(expr.expression); + expr.args.forEach(extract_candidates); + } else if (expr instanceof AST_Case) { + extract_candidates(expr.expression); + } else if (expr instanceof AST_Conditional) { + extract_candidates(expr.condition); + extract_candidates(expr.consequent); + extract_candidates(expr.alternative); + } else if (expr instanceof AST_Definitions) { + var len = expr.definitions.length; + var i2 = len - 200; + if (i2 < 0) + i2 = 0; + for (;i2 < len; i2++) { + extract_candidates(expr.definitions[i2]); + } + } else if (expr instanceof AST_DWLoop) { + extract_candidates(expr.condition); + if (!(expr.body instanceof AST_Block)) { + extract_candidates(expr.body); + } + } else if (expr instanceof AST_Exit) { + if (expr.value) + extract_candidates(expr.value); + } else if (expr instanceof AST_For) { + if (expr.init) + extract_candidates(expr.init); + if (expr.condition) + extract_candidates(expr.condition); + if (expr.step) + extract_candidates(expr.step); + if (!(expr.body instanceof AST_Block)) { + extract_candidates(expr.body); + } + } else if (expr instanceof AST_ForIn) { + extract_candidates(expr.object); + if (!(expr.body instanceof AST_Block)) { + extract_candidates(expr.body); + } + } else if (expr instanceof AST_If) { + extract_candidates(expr.condition); + if (!(expr.body instanceof AST_Block)) { + extract_candidates(expr.body); + } + if (expr.alternative && !(expr.alternative instanceof AST_Block)) { + extract_candidates(expr.alternative); + } + } else if (expr instanceof AST_Sequence) { + expr.expressions.forEach(extract_candidates); + } else if (expr instanceof AST_SimpleStatement) { + extract_candidates(expr.body); + } else if (expr instanceof AST_Switch) { + extract_candidates(expr.expression); + expr.body.forEach(extract_candidates); + } else if (expr instanceof AST_Unary) { + if (expr.operator == "++" || expr.operator == "--") { + candidates.push(hit_stack.slice()); + } + } else if (expr instanceof AST_VarDef) { + if (expr.value && !(expr.value instanceof AST_Chain)) { + candidates.push(hit_stack.slice()); + extract_candidates(expr.value); + } + } + hit_stack.pop(); + } + function find_stop(node, level, write_only) { + var parent = scanner.parent(level); + if (parent instanceof AST_Assign) { + if (write_only && !parent.logical && !(parent.left instanceof AST_PropAccess || lvalues.has(parent.left.name))) { + return find_stop(parent, level + 1, write_only); + } + return node; + } + if (parent instanceof AST_Binary) { + if (write_only && (!lazy_op.has(parent.operator) || parent.left === node)) { + return find_stop(parent, level + 1, write_only); + } + return node; + } + if (parent instanceof AST_Call) + return node; + if (parent instanceof AST_Case) + return node; + if (parent instanceof AST_Conditional) { + if (write_only && parent.condition === node) { + return find_stop(parent, level + 1, write_only); + } + return node; + } + if (parent instanceof AST_Definitions) { + return find_stop(parent, level + 1, true); + } + if (parent instanceof AST_Exit) { + return write_only ? find_stop(parent, level + 1, write_only) : node; + } + if (parent instanceof AST_If) { + if (write_only && parent.condition === node) { + return find_stop(parent, level + 1, write_only); + } + return node; + } + if (parent instanceof AST_IterationStatement) + return node; + if (parent instanceof AST_Sequence) { + return find_stop(parent, level + 1, parent.tail_node() !== node); + } + if (parent instanceof AST_SimpleStatement) { + return find_stop(parent, level + 1, true); + } + if (parent instanceof AST_Switch) + return node; + if (parent instanceof AST_VarDef) + return node; + return null; + } + function mangleable_var(var_def) { + var value = var_def.value; + if (!(value instanceof AST_SymbolRef)) + return; + if (value.name == "arguments") + return; + var def2 = value.definition(); + if (def2.undeclared) + return; + return value_def = def2; + } + function get_lhs(expr) { + if (expr instanceof AST_Assign && expr.logical) { + return false; + } else if (expr instanceof AST_VarDef && expr.name instanceof AST_SymbolDeclaration) { + var def2 = expr.name.definition(); + if (!member(expr.name, def2.orig)) + return; + var referenced = def2.references.length - def2.replaced; + if (!referenced) + return; + var declared = def2.orig.length - def2.eliminated; + if (declared > 1 && !(expr.name instanceof AST_SymbolFunarg) || (referenced > 1 ? mangleable_var(expr) : !compressor2.exposed(def2))) { + return make_node(AST_SymbolRef, expr.name, expr.name); + } + } else { + const lhs2 = expr instanceof AST_Assign ? expr.left : expr.expression; + return !is_ref_of(lhs2, AST_SymbolConst) && !is_ref_of(lhs2, AST_SymbolLet) && lhs2; + } + } + function get_rvalue(expr) { + if (expr instanceof AST_Assign) { + return expr.right; + } else { + return expr.value; + } + } + function get_lvalues(expr) { + var lvalues2 = new Map; + if (expr instanceof AST_Unary) + return lvalues2; + var tw = new TreeWalker(function(node) { + var sym = node; + while (sym instanceof AST_PropAccess) + sym = sym.expression; + if (sym instanceof AST_SymbolRef) { + const prev = lvalues2.get(sym.name); + if (!prev || !prev.modified) { + lvalues2.set(sym.name, { + def: sym.definition(), + modified: is_modified(compressor2, tw, node, node, 0) + }); + } + } + }); + get_rvalue(expr).walk(tw); + return lvalues2; + } + function remove_candidate(expr) { + if (expr.name instanceof AST_SymbolFunarg) { + var iife = compressor2.parent(), argnames = compressor2.self().argnames; + var index = argnames.indexOf(expr.name); + if (index < 0) { + iife.args.length = Math.min(iife.args.length, argnames.length - 1); + } else { + var args2 = iife.args; + if (args2[index]) + args2[index] = make_node(AST_Number, args2[index], { + value: 0 + }); + } + return true; + } + var found = false; + return statements2[stat_index].transform(new TreeTransformer(function(node, descend, in_list) { + if (found) + return node; + if (node === expr || node.body === expr) { + found = true; + if (node instanceof AST_VarDef) { + node.value = node.name instanceof AST_SymbolConst ? make_node(AST_Undefined, node.value) : null; + return node; + } + return in_list ? MAP.skip : null; + } + }, function(node) { + if (node instanceof AST_Sequence) + switch (node.expressions.length) { + case 0: + return null; + case 1: + return node.expressions[0]; + } + })); + } + function is_lhs_local(lhs2) { + while (lhs2 instanceof AST_PropAccess) + lhs2 = lhs2.expression; + return lhs2 instanceof AST_SymbolRef && lhs2.definition().scope.get_defun_scope() === defun_scope && !(in_loop && (lvalues.has(lhs2.name) || candidate instanceof AST_Unary || candidate instanceof AST_Assign && !candidate.logical && candidate.operator != "=")); + } + function value_has_side_effects(expr) { + if (expr instanceof AST_Unary) + return unary_side_effects.has(expr.operator); + return get_rvalue(expr).has_side_effects(compressor2); + } + function replace_all_symbols() { + if (side_effects) + return false; + if (value_def) + return true; + if (lhs instanceof AST_SymbolRef) { + var def2 = lhs.definition(); + if (def2.references.length - def2.replaced == (candidate instanceof AST_VarDef ? 1 : 2)) { + return true; + } + } + return false; + } + function may_modify(sym) { + if (!sym.definition) + return true; + var def2 = sym.definition(); + if (def2.orig.length == 1 && def2.orig[0] instanceof AST_SymbolDefun) + return false; + if (def2.scope.get_defun_scope() !== defun_scope) + return true; + return def2.references.some((ref) => ref.scope.get_defun_scope() !== defun_scope); + } + function side_effects_external(node, lhs2) { + if (node instanceof AST_Assign) + return side_effects_external(node.left, true); + if (node instanceof AST_Unary) + return side_effects_external(node.expression, true); + if (node instanceof AST_VarDef) + return node.value && side_effects_external(node.value); + if (lhs2) { + if (node instanceof AST_Dot) + return side_effects_external(node.expression, true); + if (node instanceof AST_Sub) + return side_effects_external(node.expression, true); + if (node instanceof AST_SymbolRef) + return node.definition().scope.get_defun_scope() !== defun_scope; + } + return false; + } + function shadows(my_scope, lvalues2) { + for (const { def: def2 } of lvalues2.values()) { + const looked_up = my_scope.find_variable(def2.name); + if (looked_up) { + if (looked_up === def2) + continue; + return true; + } + } + return false; + } + } + function eliminate_spurious_blocks(statements2) { + var seen_dirs = []; + for (var i = 0;i < statements2.length; ) { + var stat = statements2[i]; + if (stat instanceof AST_BlockStatement && stat.body.every(can_be_evicted_from_block)) { + CHANGED = true; + eliminate_spurious_blocks(stat.body); + statements2.splice(i, 1, ...stat.body); + i += stat.body.length; + } else if (stat instanceof AST_EmptyStatement) { + CHANGED = true; + statements2.splice(i, 1); + } else if (stat instanceof AST_Directive) { + if (seen_dirs.indexOf(stat.value) < 0) { + i++; + seen_dirs.push(stat.value); + } else { + CHANGED = true; + statements2.splice(i, 1); + } + } else + i++; + } + } + function handle_if_return(statements2, compressor2) { + var self2 = compressor2.self(); + var multiple_if_returns = has_multiple_if_returns(statements2); + var in_lambda = self2 instanceof AST_Lambda; + const iteration_start = Math.min(statements2.length, 500); + for (var i = iteration_start;--i >= 0; ) { + var stat = statements2[i]; + var j = next_index(i); + var next = statements2[j]; + if (in_lambda && !next && stat instanceof AST_Return) { + if (!stat.value) { + CHANGED = true; + statements2.splice(i, 1); + continue; + } + if (stat.value instanceof AST_UnaryPrefix && stat.value.operator == "void") { + CHANGED = true; + statements2[i] = make_node(AST_SimpleStatement, stat, { + body: stat.value.expression + }); + continue; + } + } + if (stat instanceof AST_If) { + let ab, new_else; + ab = aborts(stat.body); + if (can_merge_flow(ab) && (new_else = as_statement_array_with_return(stat.body, ab))) { + if (ab.label) { + remove(ab.label.thedef.references, ab); + } + CHANGED = true; + stat = stat.clone(); + stat.condition = stat.condition.negate(compressor2); + stat.body = make_node(AST_BlockStatement, stat, { + body: as_statement_array(stat.alternative).concat(extract_defuns()) + }); + stat.alternative = make_node(AST_BlockStatement, stat, { + body: new_else + }); + statements2[i] = stat.transform(compressor2); + continue; + } + ab = aborts(stat.alternative); + if (can_merge_flow(ab) && (new_else = as_statement_array_with_return(stat.alternative, ab))) { + if (ab.label) { + remove(ab.label.thedef.references, ab); + } + CHANGED = true; + stat = stat.clone(); + stat.body = make_node(AST_BlockStatement, stat.body, { + body: as_statement_array(stat.body).concat(extract_defuns()) + }); + stat.alternative = make_node(AST_BlockStatement, stat.alternative, { + body: new_else + }); + statements2[i] = stat.transform(compressor2); + continue; + } + } + if (stat instanceof AST_If && stat.body instanceof AST_Return) { + var value = stat.body.value; + if (!value && !stat.alternative && (in_lambda && !next || next instanceof AST_Return && !next.value)) { + CHANGED = true; + statements2[i] = make_node(AST_SimpleStatement, stat.condition, { + body: stat.condition + }); + continue; + } + if (value && !stat.alternative && next instanceof AST_Return && next.value) { + CHANGED = true; + stat = stat.clone(); + stat.alternative = next; + statements2[i] = stat.transform(compressor2); + statements2.splice(j, 1); + continue; + } + if (value && !stat.alternative && (!next && in_lambda && multiple_if_returns || next instanceof AST_Return)) { + CHANGED = true; + stat = stat.clone(); + stat.alternative = next || make_node(AST_Return, stat, { + value: null + }); + statements2[i] = stat.transform(compressor2); + if (next) + statements2.splice(j, 1); + continue; + } + var prev = statements2[prev_index(i)]; + if (compressor2.option("sequences") && in_lambda && !stat.alternative && prev instanceof AST_If && prev.body instanceof AST_Return && next_index(j) == statements2.length && next instanceof AST_SimpleStatement) { + CHANGED = true; + stat = stat.clone(); + stat.alternative = make_node(AST_BlockStatement, next, { + body: [ + next, + make_node(AST_Return, next, { + value: null + }) + ] + }); + statements2[i] = stat.transform(compressor2); + statements2.splice(j, 1); + continue; + } + } + } + function has_multiple_if_returns(statements3) { + var n = 0; + for (var i2 = statements3.length;--i2 >= 0; ) { + var stat2 = statements3[i2]; + if (stat2 instanceof AST_If && stat2.body instanceof AST_Return) { + if (++n > 1) + return true; + } + } + return false; + } + function is_return_void(value2) { + return !value2 || value2 instanceof AST_UnaryPrefix && value2.operator == "void"; + } + function can_merge_flow(ab) { + if (!ab) + return false; + for (var j2 = i + 1, len = statements2.length;j2 < len; j2++) { + var stat2 = statements2[j2]; + if (stat2 instanceof AST_Const || stat2 instanceof AST_Let) + return false; + } + var lct = ab instanceof AST_LoopControl ? compressor2.loopcontrol_target(ab) : null; + return ab instanceof AST_Return && in_lambda && is_return_void(ab.value) || ab instanceof AST_Continue && self2 === loop_body(lct) || ab instanceof AST_Break && lct instanceof AST_BlockStatement && self2 === lct; + } + function extract_defuns() { + var tail = statements2.slice(i + 1); + statements2.length = i + 1; + return tail.filter(function(stat2) { + if (stat2 instanceof AST_Defun) { + statements2.push(stat2); + return false; + } + return true; + }); + } + function as_statement_array_with_return(node, ab) { + var body = as_statement_array(node); + if (ab !== body[body.length - 1]) { + return; + } + body = body.slice(0, -1); + if (!body.every((stat2) => can_be_evicted_from_block(stat2))) { + return; + } + if (ab.value) { + body.push(make_node(AST_SimpleStatement, ab.value, { + body: ab.value.expression + })); + } + return body; + } + function next_index(i2) { + for (var j2 = i2 + 1, len = statements2.length;j2 < len; j2++) { + var stat2 = statements2[j2]; + if (!(stat2 instanceof AST_Var && declarations_only(stat2))) { + break; + } + } + return j2; + } + function prev_index(i2) { + for (var j2 = i2;--j2 >= 0; ) { + var stat2 = statements2[j2]; + if (!(stat2 instanceof AST_Var && declarations_only(stat2))) { + break; + } + } + return j2; + } + } + function eliminate_dead_code(statements2, compressor2) { + var has_quit; + var self2 = compressor2.self(); + for (var i = 0, n = 0, len = statements2.length;i < len; i++) { + var stat = statements2[i]; + if (stat instanceof AST_LoopControl) { + var lct = compressor2.loopcontrol_target(stat); + if (stat instanceof AST_Break && !(lct instanceof AST_IterationStatement) && loop_body(lct) === self2 || stat instanceof AST_Continue && loop_body(lct) === self2) { + if (stat.label) { + remove(stat.label.thedef.references, stat); + } + } else { + statements2[n++] = stat; + } + } else { + statements2[n++] = stat; + } + if (aborts(stat)) { + has_quit = statements2.slice(i + 1); + break; + } + } + statements2.length = n; + CHANGED = n != len; + if (has_quit) + has_quit.forEach(function(stat2) { + trim_unreachable_code(compressor2, stat2, statements2); + }); + } + function declarations_only(node) { + return node.definitions.every((var_def) => !var_def.value); + } + function sequencesize(statements2, compressor2) { + if (statements2.length < 2) + return; + var seq = [], n = 0; + function push_seq() { + if (!seq.length) + return; + var body2 = make_sequence(seq[0], seq); + statements2[n++] = make_node(AST_SimpleStatement, body2, { body: body2 }); + seq = []; + } + for (var i = 0, len = statements2.length;i < len; i++) { + var stat = statements2[i]; + if (stat instanceof AST_SimpleStatement) { + if (seq.length >= compressor2.sequences_limit) + push_seq(); + var body = stat.body; + if (seq.length > 0) + body = body.drop_side_effect_free(compressor2); + if (body) + merge_sequence(seq, body); + } else if (stat instanceof AST_Definitions && declarations_only(stat) || stat instanceof AST_Defun) { + statements2[n++] = stat; + } else { + push_seq(); + statements2[n++] = stat; + } + } + push_seq(); + statements2.length = n; + if (n != len) + CHANGED = true; + } + function to_simple_statement(block, decls) { + if (!(block instanceof AST_BlockStatement)) + return block; + var stat = null; + for (var i = 0, len = block.body.length;i < len; i++) { + var line = block.body[i]; + if (line instanceof AST_Var && declarations_only(line)) { + decls.push(line); + } else if (stat || line instanceof AST_Const || line instanceof AST_Let) { + return false; + } else { + stat = line; + } + } + return stat; + } + function sequencesize_2(statements2, compressor2) { + function cons_seq(right) { + n--; + CHANGED = true; + var left = prev.body; + return make_sequence(left, [left, right]).transform(compressor2); + } + var n = 0, prev; + for (var i = 0;i < statements2.length; i++) { + var stat = statements2[i]; + if (prev) { + if (stat instanceof AST_Exit) { + stat.value = cons_seq(stat.value || make_node(AST_Undefined, stat).transform(compressor2)); + } else if (stat instanceof AST_For) { + if (!(stat.init instanceof AST_Definitions)) { + const abort = walk(prev.body, (node) => { + if (node instanceof AST_Scope) + return true; + if (node instanceof AST_Binary && node.operator === "in") { + return walk_abort; + } + }); + if (!abort) { + if (stat.init) + stat.init = cons_seq(stat.init); + else { + stat.init = prev.body; + n--; + CHANGED = true; + } + } + } + } else if (stat instanceof AST_ForIn) { + if (!(stat.init instanceof AST_Const) && !(stat.init instanceof AST_Let)) { + stat.object = cons_seq(stat.object); + } + } else if (stat instanceof AST_If) { + stat.condition = cons_seq(stat.condition); + } else if (stat instanceof AST_Switch) { + stat.expression = cons_seq(stat.expression); + } else if (stat instanceof AST_With) { + stat.expression = cons_seq(stat.expression); + } + } + if (compressor2.option("conditionals") && stat instanceof AST_If) { + var decls = []; + var body = to_simple_statement(stat.body, decls); + var alt = to_simple_statement(stat.alternative, decls); + if (body !== false && alt !== false && decls.length > 0) { + var len = decls.length; + decls.push(make_node(AST_If, stat, { + condition: stat.condition, + body: body || make_node(AST_EmptyStatement, stat.body), + alternative: alt + })); + decls.unshift(n, 1); + [].splice.apply(statements2, decls); + i += len; + n += len + 1; + prev = null; + CHANGED = true; + continue; + } + } + statements2[n++] = stat; + prev = stat instanceof AST_SimpleStatement ? stat : null; + } + statements2.length = n; + } + function join_object_assignments(defn, body) { + if (!(defn instanceof AST_Definitions)) + return; + var def = defn.definitions[defn.definitions.length - 1]; + if (!(def.value instanceof AST_Object)) + return; + var exprs; + if (body instanceof AST_Assign && !body.logical) { + exprs = [body]; + } else if (body instanceof AST_Sequence) { + exprs = body.expressions.slice(); + } + if (!exprs) + return; + var trimmed = false; + do { + var node = exprs[0]; + if (!(node instanceof AST_Assign)) + break; + if (node.operator != "=") + break; + if (!(node.left instanceof AST_PropAccess)) + break; + var sym = node.left.expression; + if (!(sym instanceof AST_SymbolRef)) + break; + if (def.name.name != sym.name) + break; + if (!node.right.is_constant_expression(nearest_scope)) + break; + var prop = node.left.property; + if (prop instanceof AST_Node) { + prop = prop.evaluate(compressor); + } + if (prop instanceof AST_Node) + break; + prop = "" + prop; + var diff = compressor.option("ecma") < 2015 && compressor.has_directive("use strict") ? function(node2) { + return node2.key != prop && (node2.key && node2.key.name != prop); + } : function(node2) { + return node2.key && node2.key.name != prop; + }; + if (!def.value.properties.every(diff)) + break; + var p = def.value.properties.filter(function(p2) { + return p2.key === prop; + })[0]; + if (!p) { + def.value.properties.push(make_node(AST_ObjectKeyVal, node, { + key: prop, + value: node.right + })); + } else { + p.value = new AST_Sequence({ + start: p.start, + expressions: [p.value.clone(), node.right.clone()], + end: p.end + }); + } + exprs.shift(); + trimmed = true; + } while (exprs.length); + return trimmed && exprs; + } + function join_consecutive_vars(statements2) { + var defs; + for (var i = 0, j = -1, len = statements2.length;i < len; i++) { + var stat = statements2[i]; + var prev = statements2[j]; + if (stat instanceof AST_Definitions) { + if (prev && prev.TYPE == stat.TYPE) { + prev.definitions = prev.definitions.concat(stat.definitions); + CHANGED = true; + } else if (defs && defs.TYPE == stat.TYPE && declarations_only(stat)) { + defs.definitions = defs.definitions.concat(stat.definitions); + CHANGED = true; + } else { + statements2[++j] = stat; + defs = stat; + } + } else if (stat instanceof AST_Exit) { + stat.value = extract_object_assignments(stat.value); + } else if (stat instanceof AST_For) { + var exprs = join_object_assignments(prev, stat.init); + if (exprs) { + CHANGED = true; + stat.init = exprs.length ? make_sequence(stat.init, exprs) : null; + statements2[++j] = stat; + } else if (prev instanceof AST_Var && (!stat.init || stat.init.TYPE == prev.TYPE)) { + if (stat.init) { + prev.definitions = prev.definitions.concat(stat.init.definitions); + } + stat.init = prev; + statements2[j] = stat; + CHANGED = true; + } else if (defs instanceof AST_Var && stat.init instanceof AST_Var && declarations_only(stat.init)) { + defs.definitions = defs.definitions.concat(stat.init.definitions); + stat.init = null; + statements2[++j] = stat; + CHANGED = true; + } else { + statements2[++j] = stat; + } + } else if (stat instanceof AST_ForIn) { + stat.object = extract_object_assignments(stat.object); + } else if (stat instanceof AST_If) { + stat.condition = extract_object_assignments(stat.condition); + } else if (stat instanceof AST_SimpleStatement) { + var exprs = join_object_assignments(prev, stat.body); + if (exprs) { + CHANGED = true; + if (!exprs.length) + continue; + stat.body = make_sequence(stat.body, exprs); + } + statements2[++j] = stat; + } else if (stat instanceof AST_Switch) { + stat.expression = extract_object_assignments(stat.expression); + } else if (stat instanceof AST_With) { + stat.expression = extract_object_assignments(stat.expression); + } else { + statements2[++j] = stat; + } + } + statements2.length = j + 1; + function extract_object_assignments(value) { + statements2[++j] = stat; + var exprs2 = join_object_assignments(prev, value); + if (exprs2) { + CHANGED = true; + if (exprs2.length) { + return make_sequence(value, exprs2); + } else if (value instanceof AST_Sequence) { + return value.tail_node().left; + } else { + return value.left; + } + } + return value; + } + } +} + +// node_modules/terser/lib/compress/inline.js +function within_array_or_object_literal(compressor) { + var node, level = 0; + while (node = compressor.parent(level++)) { + if (node instanceof AST_Statement) + return false; + if (node instanceof AST_Array || node instanceof AST_ObjectKeyVal || node instanceof AST_Object) { + return true; + } + } + return false; +} +function scope_encloses_variables_in_this_scope(scope, pulled_scope) { + for (const enclosed of pulled_scope.enclosed) { + if (pulled_scope.variables.has(enclosed.name)) { + continue; + } + const looked_up = scope.find_variable(enclosed.name); + if (looked_up) { + if (looked_up === enclosed) + continue; + return true; + } + } + return false; +} +function is_const_symbol_short_than_init_value(def, fixed_value) { + if (def.orig.length === 1 && fixed_value) { + const init_value_length = fixed_value.size(); + const identifer_length = def.name.length; + return init_value_length > identifer_length; + } + return true; +} +function inline_into_symbolref(self2, compressor) { + if (compressor.in_computed_key()) + return self2; + const parent = compressor.parent(); + const def = self2.definition(); + const nearest_scope = compressor.find_scope(); + let fixed = self2.fixed_value(); + if (compressor.top_retain && def.global && compressor.top_retain(def) && is_const_symbol_short_than_init_value(def, fixed)) { + def.fixed = false; + def.single_use = false; + return self2; + } + if (dont_inline_lambda_in_loop(compressor, fixed)) + return self2; + let single_use = def.single_use && !(parent instanceof AST_Call && parent.is_callee_pure(compressor) || has_annotation(parent, _NOINLINE)) && !(parent instanceof AST_Export && fixed instanceof AST_Lambda && fixed.name); + if (single_use && fixed instanceof AST_Node) { + single_use = !fixed.has_side_effects(compressor) && !fixed.may_throw(compressor); + } + if (fixed instanceof AST_Class && def.scope !== self2.scope) { + return self2; + } + if (single_use && (fixed instanceof AST_Lambda || fixed instanceof AST_Class)) { + if (retain_top_func(fixed, compressor)) { + single_use = false; + } else if (def.scope !== self2.scope && (def.escaped == 1 || has_flag(fixed, INLINED) || within_array_or_object_literal(compressor) || !compressor.option("reduce_funcs"))) { + single_use = false; + } else if (is_recursive_ref(compressor, def)) { + single_use = false; + } else if (def.scope !== self2.scope || def.orig[0] instanceof AST_SymbolFunarg) { + single_use = fixed.is_constant_expression(self2.scope); + if (single_use == "f") { + var scope = self2.scope; + do { + if (scope instanceof AST_Defun || is_func_expr(scope)) { + set_flag(scope, INLINED); + } + } while (scope = scope.parent_scope); + } + } + } + if (single_use && (fixed instanceof AST_Lambda || fixed instanceof AST_Class)) { + single_use = def.scope === self2.scope && !scope_encloses_variables_in_this_scope(nearest_scope, fixed) || parent instanceof AST_Call && parent.expression === self2 && !scope_encloses_variables_in_this_scope(nearest_scope, fixed) && !(fixed.name && fixed.name.definition().recursive_refs > 0); + } + if (single_use && fixed) { + if (fixed instanceof AST_DefClass) { + set_flag(fixed, SQUEEZED); + fixed = make_node(AST_ClassExpression, fixed, fixed); + } + if (fixed instanceof AST_Defun) { + set_flag(fixed, SQUEEZED); + fixed = make_node(AST_Function, fixed, fixed); + } + if (def.recursive_refs > 0 && fixed.name instanceof AST_SymbolDefun) { + const defun_def = fixed.name.definition(); + let lambda_def = fixed.variables.get(fixed.name.name); + let name = lambda_def && lambda_def.orig[0]; + if (!(name instanceof AST_SymbolLambda)) { + name = make_node(AST_SymbolLambda, fixed.name, fixed.name); + name.scope = fixed; + fixed.name = name; + lambda_def = fixed.def_function(name); + } + walk(fixed, (node) => { + if (node instanceof AST_SymbolRef && node.definition() === defun_def) { + node.thedef = lambda_def; + lambda_def.references.push(node); + } + }); + } + if ((fixed instanceof AST_Lambda || fixed instanceof AST_Class) && fixed.parent_scope !== nearest_scope) { + fixed = fixed.clone(true, compressor.get_toplevel()); + nearest_scope.add_child_scope(fixed); + } + return fixed.optimize(compressor); + } + if (fixed) { + let replace; + if (fixed instanceof AST_This) { + if (!(def.orig[0] instanceof AST_SymbolFunarg) && def.references.every((ref) => def.scope === ref.scope)) { + replace = fixed; + } + } else { + var ev = fixed.evaluate(compressor); + if (ev !== fixed && (compressor.option("unsafe_regexp") || !(ev instanceof RegExp))) { + replace = make_node_from_constant(ev, fixed); + } + } + if (replace) { + const name_length = self2.size(compressor); + const replace_size = replace.size(compressor); + let overhead = 0; + if (compressor.option("unused") && !compressor.exposed(def)) { + overhead = (name_length + 2 + fixed.size(compressor)) / (def.references.length - def.assignments); + } + if (replace_size <= name_length + overhead) { + return replace; + } + } + } + return self2; +} +function inline_into_call(self2, compressor) { + if (compressor.in_computed_key()) + return self2; + var exp = self2.expression; + var fn = exp; + var simple_args = self2.args.every((arg) => !(arg instanceof AST_Expansion)); + if (compressor.option("reduce_vars") && fn instanceof AST_SymbolRef && !has_annotation(self2, _NOINLINE)) { + const fixed = fn.fixed_value(); + if (retain_top_func(fixed, compressor) || !compressor.toplevel.funcs && exp.definition().global) { + return self2; + } + fn = fixed; + } + if (dont_inline_lambda_in_loop(compressor, fn) && !has_annotation(self2, _INLINE)) + return self2; + var is_func = fn instanceof AST_Lambda; + var stat = is_func && fn.body[0]; + var is_regular_func = is_func && !fn.is_generator && !fn.async; + var can_inline = is_regular_func && compressor.option("inline") && !self2.is_callee_pure(compressor); + if (can_inline && stat instanceof AST_Return) { + let returned = stat.value; + if (!returned || returned.is_constant_expression()) { + if (returned) { + returned = returned.clone(true); + } else { + returned = make_node(AST_Undefined, self2); + } + const args2 = self2.args.concat(returned); + return make_sequence(self2, args2).optimize(compressor); + } + if (fn.argnames.length === 1 && fn.argnames[0] instanceof AST_SymbolFunarg && self2.args.length < 2 && !(self2.args[0] instanceof AST_Expansion) && returned instanceof AST_SymbolRef && returned.name === fn.argnames[0].name) { + const replacement = (self2.args[0] || make_node(AST_Undefined)).optimize(compressor); + let parent; + if (replacement instanceof AST_PropAccess && (parent = compressor.parent()) instanceof AST_Call && parent.expression === self2) { + return make_sequence(self2, [ + make_node(AST_Number, self2, { value: 0 }), + replacement + ]); + } + return replacement; + } + } + if (can_inline) { + var scope, in_loop, level = -1; + let def; + let returned_value; + let nearest_scope; + if (simple_args && !fn.uses_arguments && !(compressor.parent() instanceof AST_Class) && !(fn.name && fn instanceof AST_Function) && (returned_value = can_flatten_body(stat)) && (exp === fn || has_annotation(self2, _INLINE) || compressor.option("unused") && (def = exp.definition()).references.length == 1 && !is_recursive_ref(compressor, def) && fn.is_constant_expression(exp.scope)) && !has_annotation(self2, _PURE | _NOINLINE) && !fn.contains_this() && can_inject_symbols() && (nearest_scope = compressor.find_scope()) && !scope_encloses_variables_in_this_scope(nearest_scope, fn) && !function in_default_assign() { + let i = 0; + let p; + while (p = compressor.parent(i++)) { + if (p instanceof AST_DefaultAssign) + return true; + if (p instanceof AST_Block) + break; + } + return false; + }() && !(scope instanceof AST_Class)) { + set_flag(fn, SQUEEZED); + nearest_scope.add_child_scope(fn); + return make_sequence(self2, flatten_fn(returned_value)).optimize(compressor); + } + } + if (can_inline && has_annotation(self2, _INLINE)) { + set_flag(fn, SQUEEZED); + fn = make_node(fn.CTOR === AST_Defun ? AST_Function : fn.CTOR, fn, fn); + fn = fn.clone(true); + fn.figure_out_scope({}, { + parent_scope: compressor.find_scope(), + toplevel: compressor.get_toplevel() + }); + return make_node(AST_Call, self2, { + expression: fn, + args: self2.args + }).optimize(compressor); + } + const can_drop_this_call = is_regular_func && compressor.option("side_effects") && fn.body.every(is_empty); + if (can_drop_this_call) { + var args = self2.args.concat(make_node(AST_Undefined, self2)); + return make_sequence(self2, args).optimize(compressor); + } + if (compressor.option("negate_iife") && compressor.parent() instanceof AST_SimpleStatement && is_iife_call(self2)) { + return self2.negate(compressor, true); + } + var ev = self2.evaluate(compressor); + if (ev !== self2) { + ev = make_node_from_constant(ev, self2).optimize(compressor); + return best_of(compressor, ev, self2); + } + return self2; + function return_value(stat2) { + if (!stat2) + return make_node(AST_Undefined, self2); + if (stat2 instanceof AST_Return) { + if (!stat2.value) + return make_node(AST_Undefined, self2); + return stat2.value.clone(true); + } + if (stat2 instanceof AST_SimpleStatement) { + return make_node(AST_UnaryPrefix, stat2, { + operator: "void", + expression: stat2.body.clone(true) + }); + } + } + function can_flatten_body(stat2) { + var body = fn.body; + var len = body.length; + if (compressor.option("inline") < 3) { + return len == 1 && return_value(stat2); + } + stat2 = null; + for (var i = 0;i < len; i++) { + var line = body[i]; + if (line instanceof AST_Var) { + if (stat2 && !line.definitions.every((var_def) => !var_def.value)) { + return false; + } + } else if (stat2) { + return false; + } else if (!(line instanceof AST_EmptyStatement)) { + stat2 = line; + } + } + return return_value(stat2); + } + function can_inject_args(block_scoped, safe_to_inject) { + for (var i = 0, len = fn.argnames.length;i < len; i++) { + var arg = fn.argnames[i]; + if (arg instanceof AST_DefaultAssign) { + if (has_flag(arg.left, UNUSED)) + continue; + return false; + } + if (arg instanceof AST_Destructuring) + return false; + if (arg instanceof AST_Expansion) { + if (has_flag(arg.expression, UNUSED)) + continue; + return false; + } + if (has_flag(arg, UNUSED)) + continue; + if (!safe_to_inject || block_scoped.has(arg.name) || identifier_atom.has(arg.name) || scope.conflicting_def(arg.name)) { + return false; + } + if (in_loop) + in_loop.push(arg.definition()); + } + return true; + } + function can_inject_vars(block_scoped, safe_to_inject) { + var len = fn.body.length; + for (var i = 0;i < len; i++) { + var stat2 = fn.body[i]; + if (!(stat2 instanceof AST_Var)) + continue; + if (!safe_to_inject) + return false; + for (var j = stat2.definitions.length;--j >= 0; ) { + var name = stat2.definitions[j].name; + if (name instanceof AST_Destructuring || block_scoped.has(name.name) || identifier_atom.has(name.name) || scope.conflicting_def(name.name)) { + return false; + } + if (in_loop) + in_loop.push(name.definition()); + } + } + return true; + } + function can_inject_symbols() { + var block_scoped = new Set; + do { + scope = compressor.parent(++level); + if (scope.is_block_scope() && scope.block_scope) { + scope.block_scope.variables.forEach(function(variable) { + block_scoped.add(variable.name); + }); + } + if (scope instanceof AST_Catch) { + if (scope.argname) { + block_scoped.add(scope.argname.name); + } + } else if (scope instanceof AST_IterationStatement) { + in_loop = []; + } else if (scope instanceof AST_SymbolRef) { + if (scope.fixed_value() instanceof AST_Scope) + return false; + } + } while (!(scope instanceof AST_Scope)); + var safe_to_inject = !(scope instanceof AST_Toplevel) || compressor.toplevel.vars; + var inline = compressor.option("inline"); + if (!can_inject_vars(block_scoped, inline >= 3 && safe_to_inject)) + return false; + if (!can_inject_args(block_scoped, inline >= 2 && safe_to_inject)) + return false; + return !in_loop || in_loop.length == 0 || !is_reachable(fn, in_loop); + } + function append_var(decls, expressions, name, value) { + var def = name.definition(); + const already_appended = scope.variables.has(name.name); + if (!already_appended) { + scope.variables.set(name.name, def); + scope.enclosed.push(def); + decls.push(make_node(AST_VarDef, name, { + name, + value: null + })); + } + var sym = make_node(AST_SymbolRef, name, name); + def.references.push(sym); + if (value) + expressions.push(make_node(AST_Assign, self2, { + operator: "=", + logical: false, + left: sym, + right: value.clone() + })); + } + function flatten_args(decls, expressions) { + var len = fn.argnames.length; + for (var i = self2.args.length;--i >= len; ) { + expressions.push(self2.args[i]); + } + for (i = len;--i >= 0; ) { + var name = fn.argnames[i]; + var value = self2.args[i]; + if (has_flag(name, UNUSED) || !name.name || scope.conflicting_def(name.name)) { + if (value) + expressions.push(value); + } else { + var symbol = make_node(AST_SymbolVar, name, name); + name.definition().orig.push(symbol); + if (!value && in_loop) + value = make_node(AST_Undefined, self2); + append_var(decls, expressions, symbol, value); + } + } + decls.reverse(); + expressions.reverse(); + } + function flatten_vars(decls, expressions) { + var pos = expressions.length; + for (var i = 0, lines = fn.body.length;i < lines; i++) { + var stat2 = fn.body[i]; + if (!(stat2 instanceof AST_Var)) + continue; + for (var j = 0, defs = stat2.definitions.length;j < defs; j++) { + var var_def = stat2.definitions[j]; + var name = var_def.name; + append_var(decls, expressions, name, var_def.value); + if (in_loop && fn.argnames.every((argname) => argname.name != name.name)) { + var def = fn.variables.get(name.name); + var sym = make_node(AST_SymbolRef, name, name); + def.references.push(sym); + expressions.splice(pos++, 0, make_node(AST_Assign, var_def, { + operator: "=", + logical: false, + left: sym, + right: make_node(AST_Undefined, name) + })); + } + } + } + } + function flatten_fn(returned_value) { + var decls = []; + var expressions = []; + flatten_args(decls, expressions); + flatten_vars(decls, expressions); + expressions.push(returned_value); + if (decls.length) { + const i = scope.body.indexOf(compressor.parent(level - 1)) + 1; + scope.body.splice(i, 0, make_node(AST_Var, fn, { + definitions: decls + })); + } + return expressions.map((exp2) => exp2.clone(true)); + } +} +function dont_inline_lambda_in_loop(compressor, maybe_lambda) { + return (maybe_lambda instanceof AST_Lambda || maybe_lambda instanceof AST_Class) && !!compressor.is_within_loop(); +} + +// node_modules/terser/lib/compress/global-defs.js +(function(def_find_defs) { + function to_node(value, orig) { + if (value instanceof AST_Node) { + if (!(value instanceof AST_Constant)) { + value = value.clone(true); + } + return make_node(value.CTOR, orig, value); + } + if (Array.isArray(value)) + return make_node(AST_Array, orig, { + elements: value.map(function(value2) { + return to_node(value2, orig); + }) + }); + if (value && typeof value == "object") { + var props = []; + for (var key in value) + if (HOP(value, key)) { + props.push(make_node(AST_ObjectKeyVal, orig, { + key, + value: to_node(value[key], orig) + })); + } + return make_node(AST_Object, orig, { + properties: props + }); + } + return make_node_from_constant(value, orig); + } + AST_Toplevel.DEFMETHOD("resolve_defines", function(compressor) { + if (!compressor.option("global_defs")) + return this; + this.figure_out_scope({ ie8: compressor.option("ie8") }); + return this.transform(new TreeTransformer(function(node) { + var def = node._find_defs(compressor, ""); + if (!def) + return; + var level = 0, child = node, parent; + while (parent = this.parent(level++)) { + if (!(parent instanceof AST_PropAccess)) + break; + if (parent.expression !== child) + break; + child = parent; + } + if (is_lhs(child, parent)) { + return; + } + return def; + })); + }); + def_find_defs(AST_Node, noop); + def_find_defs(AST_Chain, function(compressor, suffix) { + return this.expression._find_defs(compressor, suffix); + }); + def_find_defs(AST_Dot, function(compressor, suffix) { + return this.expression._find_defs(compressor, "." + this.property + suffix); + }); + def_find_defs(AST_SymbolDeclaration, function() { + if (!this.global()) + return; + }); + def_find_defs(AST_SymbolRef, function(compressor, suffix) { + if (!this.global()) + return; + var defines = compressor.option("global_defs"); + var name = this.name + suffix; + if (HOP(defines, name)) + return to_node(defines[name], this); + }); + def_find_defs(AST_ImportMeta, function(compressor, suffix) { + var defines = compressor.option("global_defs"); + var name = "import.meta" + suffix; + if (HOP(defines, name)) + return to_node(defines[name], this); + }); +})(function(node, func) { + node.DEFMETHOD("_find_defs", func); +}); + +// node_modules/terser/lib/compress/index.js +class Compressor extends TreeWalker { + constructor(options, { false_by_default = false, mangle_options: mangle_options2 = false }) { + super(); + if (options.defaults !== undefined && !options.defaults) + false_by_default = true; + this.options = defaults(options, { + arguments: false, + arrows: !false_by_default, + booleans: !false_by_default, + booleans_as_integers: false, + collapse_vars: !false_by_default, + comparisons: !false_by_default, + computed_props: !false_by_default, + conditionals: !false_by_default, + dead_code: !false_by_default, + defaults: true, + directives: !false_by_default, + drop_console: false, + drop_debugger: !false_by_default, + ecma: 5, + evaluate: !false_by_default, + expression: false, + global_defs: false, + hoist_funs: false, + hoist_props: !false_by_default, + hoist_vars: false, + ie8: false, + if_return: !false_by_default, + inline: !false_by_default, + join_vars: !false_by_default, + keep_classnames: false, + keep_fargs: true, + keep_fnames: false, + keep_infinity: false, + lhs_constants: !false_by_default, + loops: !false_by_default, + module: false, + negate_iife: !false_by_default, + passes: 1, + properties: !false_by_default, + pure_getters: !false_by_default && "strict", + pure_funcs: null, + pure_new: false, + reduce_funcs: !false_by_default, + reduce_vars: !false_by_default, + sequences: !false_by_default, + side_effects: !false_by_default, + switches: !false_by_default, + top_retain: null, + toplevel: !!(options && options["top_retain"]), + typeofs: !false_by_default, + unsafe: false, + unsafe_arrows: false, + unsafe_comps: false, + unsafe_Function: false, + unsafe_math: false, + unsafe_symbols: false, + unsafe_methods: false, + unsafe_proto: false, + unsafe_regexp: false, + unsafe_undefined: false, + unused: !false_by_default, + warnings: false + }, true); + var global_defs = this.options["global_defs"]; + if (typeof global_defs == "object") + for (var key in global_defs) { + if (key[0] === "@" && HOP(global_defs, key)) { + global_defs[key.slice(1)] = parse(global_defs[key], { + expression: true + }); + } + } + if (this.options["inline"] === true) + this.options["inline"] = 3; + var pure_funcs = this.options["pure_funcs"]; + if (typeof pure_funcs == "function") { + this.pure_funcs = pure_funcs; + } else { + this.pure_funcs = pure_funcs ? function(node) { + return !pure_funcs.includes(node.expression.print_to_string()); + } : return_true; + } + var top_retain = this.options["top_retain"]; + if (top_retain instanceof RegExp) { + this.top_retain = function(def) { + return top_retain.test(def.name); + }; + } else if (typeof top_retain == "function") { + this.top_retain = top_retain; + } else if (top_retain) { + if (typeof top_retain == "string") { + top_retain = top_retain.split(/,/); + } + this.top_retain = function(def) { + return top_retain.includes(def.name); + }; + } + if (this.options["module"]) { + this.directives["use strict"] = true; + this.options["toplevel"] = true; + } + var toplevel = this.options["toplevel"]; + this.toplevel = typeof toplevel == "string" ? { + funcs: /funcs/.test(toplevel), + vars: /vars/.test(toplevel) + } : { + funcs: toplevel, + vars: toplevel + }; + var sequences = this.options["sequences"]; + this.sequences_limit = sequences == 1 ? 800 : sequences | 0; + this.evaluated_regexps = new Map; + this._toplevel = undefined; + this._mangle_options = mangle_options2 ? format_mangler_options(mangle_options2) : mangle_options2; + } + mangle_options() { + var nth_identifier = this._mangle_options && this._mangle_options.nth_identifier || base54; + var module = this._mangle_options && this._mangle_options.module || this.option("module"); + return { ie8: this.option("ie8"), nth_identifier, module }; + } + option(key) { + return this.options[key]; + } + exposed(def) { + if (def.export) + return true; + if (def.global) { + for (var i = 0, len = def.orig.length;i < len; i++) + if (!this.toplevel[def.orig[i] instanceof AST_SymbolDefun ? "funcs" : "vars"]) + return true; + } + return false; + } + in_boolean_context() { + if (!this.option("booleans")) + return false; + var self2 = this.self(); + for (var i = 0, p;p = this.parent(i); i++) { + if (p instanceof AST_SimpleStatement || p instanceof AST_Conditional && p.condition === self2 || p instanceof AST_DWLoop && p.condition === self2 || p instanceof AST_For && p.condition === self2 || p instanceof AST_If && p.condition === self2 || p instanceof AST_UnaryPrefix && p.operator == "!" && p.expression === self2) { + return true; + } + if (p instanceof AST_Binary && (p.operator == "&&" || p.operator == "||" || p.operator == "??") || p instanceof AST_Conditional || p.tail_node() === self2) { + self2 = p; + } else { + return false; + } + } + } + in_32_bit_context(other_operand_must_be_number) { + if (!this.option("evaluate")) + return false; + var self2 = this.self(); + for (var i = 0, p;p = this.parent(i); i++) { + if (p instanceof AST_Binary && bitwise_binop.has(p.operator)) { + if (other_operand_must_be_number) { + return (self2 === p.left ? p.right : p.left).is_number(this); + } else { + return true; + } + } + if (p instanceof AST_UnaryPrefix) { + return p.operator === "~"; + } + if (p instanceof AST_Binary && (p.operator == "&&" || p.operator == "||" || p.operator == "??") || p instanceof AST_Conditional && p.condition !== self2 || p.tail_node() === self2) { + self2 = p; + } else { + return false; + } + } + } + in_computed_key() { + if (!this.option("evaluate")) + return false; + var self2 = this.self(); + for (var i = 0, p;p = this.parent(i); i++) { + if (p instanceof AST_ObjectProperty && p.key === self2) { + return true; + } + } + return false; + } + get_toplevel() { + return this._toplevel; + } + compress(toplevel) { + toplevel = toplevel.resolve_defines(this); + this._toplevel = toplevel; + if (this.option("expression")) { + this._toplevel.process_expression(true); + } + var passes = +this.options.passes || 1; + var min_count = 1 / 0; + var stopping = false; + var mangle = this.mangle_options(); + for (var pass = 0;pass < passes; pass++) { + this._toplevel.figure_out_scope(mangle); + if (pass === 0 && this.option("drop_console")) { + this._toplevel = this._toplevel.drop_console(this.option("drop_console")); + } + if (pass > 0 || this.option("reduce_vars")) { + this._toplevel.reset_opt_flags(this); + } + this._toplevel = this._toplevel.transform(this); + if (passes > 1) { + let count = 0; + walk(this._toplevel, () => { + count++; + }); + if (count < min_count) { + min_count = count; + stopping = false; + } else if (stopping) { + break; + } else { + stopping = true; + } + } + } + if (this.option("expression")) { + this._toplevel.process_expression(false); + } + toplevel = this._toplevel; + this._toplevel = undefined; + return toplevel; + } + before(node, descend) { + if (has_flag(node, SQUEEZED)) + return node; + var was_scope = false; + if (node instanceof AST_Scope) { + node = node.hoist_properties(this); + node = node.hoist_declarations(this); + was_scope = true; + } + descend(node, this); + descend(node, this); + var opt = node.optimize(this); + if (was_scope && opt instanceof AST_Scope) { + opt.drop_unused(this); + descend(opt, this); + } + if (opt === node) + set_flag(opt, SQUEEZED); + return opt; + } + is_lhs() { + const self2 = this.stack[this.stack.length - 1]; + const parent = this.stack[this.stack.length - 2]; + return is_lhs(self2, parent); + } +} +function def_optimize(node, optimizer) { + node.DEFMETHOD("optimize", function(compressor) { + var self2 = this; + if (has_flag(self2, OPTIMIZED)) + return self2; + if (compressor.has_directive("use asm")) + return self2; + var opt = optimizer(self2, compressor); + set_flag(opt, OPTIMIZED); + return opt; + }); +} +def_optimize(AST_Node, function(self2) { + return self2; +}); +AST_Toplevel.DEFMETHOD("drop_console", function(options) { + const isArray = Array.isArray(options); + const tt = new TreeTransformer(function(self2) { + if (self2.TYPE !== "Call") { + return; + } + var exp = self2.expression; + if (!(exp instanceof AST_PropAccess)) { + return; + } + var name = exp.expression; + var property = exp.property; + var depth = 2; + while (name.expression) { + property = name.property; + name = name.expression; + depth++; + } + if (isArray && !options.includes(property)) { + return; + } + if (is_undeclared_ref(name) && name.name == "console") { + if (depth === 3 && !["call", "apply"].includes(exp.property) && is_used_in_expression(tt)) { + exp.expression = make_empty_function(self2); + set_flag(exp.expression, SQUEEZED); + self2.args = []; + } else { + return make_node(AST_Undefined, self2); + } + } + }); + return this.transform(tt); +}); +AST_Node.DEFMETHOD("equivalent_to", function(node) { + return equivalent_to(this, node); +}); +AST_Scope.DEFMETHOD("process_expression", function(insert, compressor) { + var self2 = this; + var tt = new TreeTransformer(function(node) { + if (insert && node instanceof AST_SimpleStatement) { + return make_node(AST_Return, node, { + value: node.body + }); + } + if (!insert && node instanceof AST_Return) { + if (compressor) { + var value = node.value && node.value.drop_side_effect_free(compressor, true); + return value ? make_node(AST_SimpleStatement, node, { body: value }) : make_node(AST_EmptyStatement, node); + } + return make_node(AST_SimpleStatement, node, { + body: node.value || make_node(AST_UnaryPrefix, node, { + operator: "void", + expression: make_node(AST_Number, node, { + value: 0 + }) + }) + }); + } + if (node instanceof AST_Class || node instanceof AST_Lambda && node !== self2) { + return node; + } + if (node instanceof AST_Block) { + var index = node.body.length - 1; + if (index >= 0) { + node.body[index] = node.body[index].transform(tt); + } + } else if (node instanceof AST_If) { + node.body = node.body.transform(tt); + if (node.alternative) { + node.alternative = node.alternative.transform(tt); + } + } else if (node instanceof AST_With) { + node.body = node.body.transform(tt); + } + return node; + }); + self2.transform(tt); +}); +AST_Toplevel.DEFMETHOD("reset_opt_flags", function(compressor) { + const self2 = this; + const reduce_vars = compressor.option("reduce_vars"); + const preparation = new TreeWalker(function(node, descend) { + clear_flag(node, CLEAR_BETWEEN_PASSES); + if (reduce_vars) { + if (compressor.top_retain && node instanceof AST_Defun && preparation.parent() === self2) { + set_flag(node, TOP); + } + return node.reduce_vars(preparation, descend, compressor); + } + }); + preparation.safe_ids = Object.create(null); + preparation.in_loop = null; + preparation.loop_ids = new Map; + preparation.defs_to_safe_ids = new Map; + self2.walk(preparation); +}); +AST_Symbol.DEFMETHOD("fixed_value", function() { + var fixed = this.thedef.fixed; + if (!fixed || fixed instanceof AST_Node) + return fixed; + return fixed(); +}); +AST_SymbolRef.DEFMETHOD("is_immutable", function() { + var orig = this.definition().orig; + return orig.length == 1 && orig[0] instanceof AST_SymbolLambda; +}); +function find_variable(compressor, name) { + var scope, i = 0; + while (scope = compressor.parent(i++)) { + if (scope instanceof AST_Scope) + break; + if (scope instanceof AST_Catch && scope.argname) { + scope = scope.argname.definition().scope; + break; + } + } + return scope.find_variable(name); +} +var global_names = makePredicate("Array Boolean clearInterval clearTimeout console Date decodeURI decodeURIComponent encodeURI encodeURIComponent Error escape eval EvalError Function isFinite isNaN JSON Math Number parseFloat parseInt RangeError ReferenceError RegExp Object setInterval setTimeout String SyntaxError TypeError unescape URIError"); +AST_SymbolRef.DEFMETHOD("is_declared", function(compressor) { + return !this.definition().undeclared || compressor.option("unsafe") && global_names.has(this.name); +}); +var directives = new Set(["use asm", "use strict"]); +def_optimize(AST_Directive, function(self2, compressor) { + if (compressor.option("directives") && (!directives.has(self2.value) || compressor.has_directive(self2.value) !== self2)) { + return make_node(AST_EmptyStatement, self2); + } + return self2; +}); +def_optimize(AST_Debugger, function(self2, compressor) { + if (compressor.option("drop_debugger")) + return make_node(AST_EmptyStatement, self2); + return self2; +}); +def_optimize(AST_LabeledStatement, function(self2, compressor) { + if (self2.body instanceof AST_Break && compressor.loopcontrol_target(self2.body) === self2.body) { + return make_node(AST_EmptyStatement, self2); + } + return self2.label.references.length == 0 ? self2.body : self2; +}); +def_optimize(AST_Block, function(self2, compressor) { + tighten_body(self2.body, compressor); + return self2; +}); +function can_be_extracted_from_if_block(node) { + return !(node instanceof AST_Const || node instanceof AST_Let || node instanceof AST_Class); +} +def_optimize(AST_BlockStatement, function(self2, compressor) { + tighten_body(self2.body, compressor); + switch (self2.body.length) { + case 1: + if (!compressor.has_directive("use strict") && compressor.parent() instanceof AST_If && can_be_extracted_from_if_block(self2.body[0]) || can_be_evicted_from_block(self2.body[0])) { + return self2.body[0]; + } + break; + case 0: + return make_node(AST_EmptyStatement, self2); + } + return self2; +}); +function opt_AST_Lambda(self2, compressor) { + tighten_body(self2.body, compressor); + if (compressor.option("side_effects") && self2.body.length == 1 && self2.body[0] === compressor.has_directive("use strict")) { + self2.body.length = 0; + } + return self2; +} +def_optimize(AST_Lambda, opt_AST_Lambda); +AST_Scope.DEFMETHOD("hoist_declarations", function(compressor) { + var self2 = this; + if (compressor.has_directive("use asm")) + return self2; + var hoist_funs = compressor.option("hoist_funs"); + var hoist_vars = compressor.option("hoist_vars"); + if (hoist_funs || hoist_vars) { + var dirs = []; + var hoisted = []; + var vars = new Map, vars_found = 0, var_decl = 0; + walk(self2, (node) => { + if (node instanceof AST_Scope && node !== self2) + return true; + if (node instanceof AST_Var) { + ++var_decl; + return true; + } + }); + hoist_vars = hoist_vars && var_decl > 1; + var tt = new TreeTransformer(function before(node) { + if (node !== self2) { + if (node instanceof AST_Directive) { + dirs.push(node); + return make_node(AST_EmptyStatement, node); + } + if (hoist_funs && node instanceof AST_Defun && !(tt.parent() instanceof AST_Export) && tt.parent() === self2) { + hoisted.push(node); + return make_node(AST_EmptyStatement, node); + } + if (hoist_vars && node instanceof AST_Var && !node.definitions.some((def3) => def3.name instanceof AST_Destructuring)) { + node.definitions.forEach(function(def3) { + vars.set(def3.name.name, def3); + ++vars_found; + }); + var seq = node.to_assignments(compressor); + var p = tt.parent(); + if (p instanceof AST_ForIn && p.init === node) { + if (seq == null) { + var def2 = node.definitions[0].name; + return make_node(AST_SymbolRef, def2, def2); + } + return seq; + } + if (p instanceof AST_For && p.init === node) { + return seq; + } + if (!seq) + return make_node(AST_EmptyStatement, node); + return make_node(AST_SimpleStatement, node, { + body: seq + }); + } + if (node instanceof AST_Scope) + return node; + } + }); + self2 = self2.transform(tt); + if (vars_found > 0) { + var defs = []; + const is_lambda = self2 instanceof AST_Lambda; + const args_as_names = is_lambda ? self2.args_as_names() : null; + vars.forEach((def2, name) => { + if (is_lambda && args_as_names.some((x) => x.name === def2.name.name)) { + vars.delete(name); + } else { + def2 = def2.clone(); + def2.value = null; + defs.push(def2); + vars.set(name, def2); + } + }); + if (defs.length > 0) { + for (var i = 0;i < self2.body.length; ) { + if (self2.body[i] instanceof AST_SimpleStatement) { + var expr = self2.body[i].body, sym, assign; + if (expr instanceof AST_Assign && expr.operator == "=" && (sym = expr.left) instanceof AST_Symbol && vars.has(sym.name)) { + var def = vars.get(sym.name); + if (def.value) + break; + def.value = expr.right; + remove(defs, def); + defs.push(def); + self2.body.splice(i, 1); + continue; + } + if (expr instanceof AST_Sequence && (assign = expr.expressions[0]) instanceof AST_Assign && assign.operator == "=" && (sym = assign.left) instanceof AST_Symbol && vars.has(sym.name)) { + var def = vars.get(sym.name); + if (def.value) + break; + def.value = assign.right; + remove(defs, def); + defs.push(def); + self2.body[i].body = make_sequence(expr, expr.expressions.slice(1)); + continue; + } + } + if (self2.body[i] instanceof AST_EmptyStatement) { + self2.body.splice(i, 1); + continue; + } + if (self2.body[i] instanceof AST_BlockStatement) { + self2.body.splice(i, 1, ...self2.body[i].body); + continue; + } + break; + } + defs = make_node(AST_Var, self2, { + definitions: defs + }); + hoisted.push(defs); + } + } + self2.body = dirs.concat(hoisted, self2.body); + } + return self2; +}); +AST_Scope.DEFMETHOD("hoist_properties", function(compressor) { + var self2 = this; + if (!compressor.option("hoist_props") || compressor.has_directive("use asm")) + return self2; + var top_retain = self2 instanceof AST_Toplevel && compressor.top_retain || return_false; + var defs_by_id = new Map; + var hoister = new TreeTransformer(function(node, descend) { + if (node instanceof AST_VarDef) { + const sym = node.name; + let def; + let value; + if (sym.scope === self2 && (def = sym.definition()).escaped != 1 && !def.assignments && !def.direct_access && !def.single_use && !compressor.exposed(def) && !top_retain(def) && (value = sym.fixed_value()) === node.value && value instanceof AST_Object && !value.properties.some((prop) => prop instanceof AST_Expansion || prop.computed_key())) { + descend(node, this); + const defs = new Map; + const assignments = []; + value.properties.forEach(({ key, value: value2 }) => { + const scope = hoister.find_scope(); + const symbol = self2.create_symbol(sym.CTOR, { + source: sym, + scope, + conflict_scopes: new Set([ + scope, + ...sym.definition().references.map((ref) => ref.scope) + ]), + tentative_name: sym.name + "_" + key + }); + defs.set(String(key), symbol.definition()); + assignments.push(make_node(AST_VarDef, node, { + name: symbol, + value: value2 + })); + }); + defs_by_id.set(def.id, defs); + return MAP.splice(assignments); + } + } else if (node instanceof AST_PropAccess && node.expression instanceof AST_SymbolRef) { + const defs = defs_by_id.get(node.expression.definition().id); + if (defs) { + const def = defs.get(String(get_simple_key(node.property))); + const sym = make_node(AST_SymbolRef, node, { + name: def.name, + scope: node.expression.scope, + thedef: def + }); + sym.reference({}); + return sym; + } + } + }); + return self2.transform(hoister); +}); +def_optimize(AST_SimpleStatement, function(self2, compressor) { + if (compressor.option("side_effects")) { + var body = self2.body; + var node = body.drop_side_effect_free(compressor, true); + if (!node) { + return make_node(AST_EmptyStatement, self2); + } + if (node !== body) { + return make_node(AST_SimpleStatement, self2, { body: node }); + } + } + return self2; +}); +def_optimize(AST_While, function(self2, compressor) { + return compressor.option("loops") ? make_node(AST_For, self2, self2).optimize(compressor) : self2; +}); +def_optimize(AST_Do, function(self2, compressor) { + if (!compressor.option("loops")) + return self2; + var cond = self2.condition.tail_node().evaluate(compressor); + if (!(cond instanceof AST_Node)) { + if (cond) + return make_node(AST_For, self2, { + body: make_node(AST_BlockStatement, self2.body, { + body: [ + self2.body, + make_node(AST_SimpleStatement, self2.condition, { + body: self2.condition + }) + ] + }) + }).optimize(compressor); + if (!has_break_or_continue(self2, compressor.parent())) { + return make_node(AST_BlockStatement, self2.body, { + body: [ + self2.body, + make_node(AST_SimpleStatement, self2.condition, { + body: self2.condition + }) + ] + }).optimize(compressor); + } + } + return self2; +}); +function if_break_in_loop(self2, compressor) { + var first = self2.body instanceof AST_BlockStatement ? self2.body.body[0] : self2.body; + if (compressor.option("dead_code") && is_break(first)) { + var body = []; + if (self2.init instanceof AST_Statement) { + body.push(self2.init); + } else if (self2.init) { + body.push(make_node(AST_SimpleStatement, self2.init, { + body: self2.init + })); + } + if (self2.condition) { + body.push(make_node(AST_SimpleStatement, self2.condition, { + body: self2.condition + })); + } + trim_unreachable_code(compressor, self2.body, body); + return make_node(AST_BlockStatement, self2, { + body + }); + } + if (first instanceof AST_If) { + if (is_break(first.body)) { + if (self2.condition) { + self2.condition = make_node(AST_Binary, self2.condition, { + left: self2.condition, + operator: "&&", + right: first.condition.negate(compressor) + }); + } else { + self2.condition = first.condition.negate(compressor); + } + drop_it(first.alternative); + } else if (is_break(first.alternative)) { + if (self2.condition) { + self2.condition = make_node(AST_Binary, self2.condition, { + left: self2.condition, + operator: "&&", + right: first.condition + }); + } else { + self2.condition = first.condition; + } + drop_it(first.body); + } + } + return self2; + function is_break(node) { + return node instanceof AST_Break && compressor.loopcontrol_target(node) === compressor.self(); + } + function drop_it(rest) { + rest = as_statement_array(rest); + if (self2.body instanceof AST_BlockStatement) { + self2.body = self2.body.clone(); + self2.body.body = rest.concat(self2.body.body.slice(1)); + self2.body = self2.body.transform(compressor); + } else { + self2.body = make_node(AST_BlockStatement, self2.body, { + body: rest + }).transform(compressor); + } + self2 = if_break_in_loop(self2, compressor); + } +} +def_optimize(AST_For, function(self2, compressor) { + if (!compressor.option("loops")) + return self2; + if (compressor.option("side_effects") && self2.init) { + self2.init = self2.init.drop_side_effect_free(compressor); + } + if (self2.condition) { + var cond = self2.condition.evaluate(compressor); + if (!(cond instanceof AST_Node)) { + if (cond) + self2.condition = null; + else if (!compressor.option("dead_code")) { + var orig = self2.condition; + self2.condition = make_node_from_constant(cond, self2.condition); + self2.condition = best_of_expression(self2.condition.transform(compressor), orig); + } + } + if (compressor.option("dead_code")) { + if (cond instanceof AST_Node) + cond = self2.condition.tail_node().evaluate(compressor); + if (!cond) { + var body = []; + trim_unreachable_code(compressor, self2.body, body); + if (self2.init instanceof AST_Statement) { + body.push(self2.init); + } else if (self2.init) { + body.push(make_node(AST_SimpleStatement, self2.init, { + body: self2.init + })); + } + body.push(make_node(AST_SimpleStatement, self2.condition, { + body: self2.condition + })); + return make_node(AST_BlockStatement, self2, { body }).optimize(compressor); + } + } + } + return if_break_in_loop(self2, compressor); +}); +def_optimize(AST_If, function(self2, compressor) { + if (is_empty(self2.alternative)) + self2.alternative = null; + if (!compressor.option("conditionals")) + return self2; + var cond = self2.condition.evaluate(compressor); + if (!compressor.option("dead_code") && !(cond instanceof AST_Node)) { + var orig = self2.condition; + self2.condition = make_node_from_constant(cond, orig); + self2.condition = best_of_expression(self2.condition.transform(compressor), orig); + } + if (compressor.option("dead_code")) { + if (cond instanceof AST_Node) + cond = self2.condition.tail_node().evaluate(compressor); + if (!cond) { + var body = []; + trim_unreachable_code(compressor, self2.body, body); + body.push(make_node(AST_SimpleStatement, self2.condition, { + body: self2.condition + })); + if (self2.alternative) + body.push(self2.alternative); + return make_node(AST_BlockStatement, self2, { body }).optimize(compressor); + } else if (!(cond instanceof AST_Node)) { + var body = []; + body.push(make_node(AST_SimpleStatement, self2.condition, { + body: self2.condition + })); + body.push(self2.body); + if (self2.alternative) { + trim_unreachable_code(compressor, self2.alternative, body); + } + return make_node(AST_BlockStatement, self2, { body }).optimize(compressor); + } + } + var negated = self2.condition.negate(compressor); + var self_condition_length = self2.condition.size(); + var negated_length = negated.size(); + var negated_is_best = negated_length < self_condition_length; + if (self2.alternative && negated_is_best) { + negated_is_best = false; + self2.condition = negated; + var tmp = self2.body; + self2.body = self2.alternative || make_node(AST_EmptyStatement, self2); + self2.alternative = tmp; + } + if (is_empty(self2.body) && is_empty(self2.alternative)) { + return make_node(AST_SimpleStatement, self2.condition, { + body: self2.condition.clone() + }).optimize(compressor); + } + if (self2.body instanceof AST_SimpleStatement && self2.alternative instanceof AST_SimpleStatement) { + return make_node(AST_SimpleStatement, self2, { + body: make_node(AST_Conditional, self2, { + condition: self2.condition, + consequent: self2.body.body, + alternative: self2.alternative.body + }) + }).optimize(compressor); + } + if (is_empty(self2.alternative) && self2.body instanceof AST_SimpleStatement) { + if (self_condition_length === negated_length && !negated_is_best && self2.condition instanceof AST_Binary && self2.condition.operator == "||") { + negated_is_best = true; + } + if (negated_is_best) + return make_node(AST_SimpleStatement, self2, { + body: make_node(AST_Binary, self2, { + operator: "||", + left: negated, + right: self2.body.body + }) + }).optimize(compressor); + return make_node(AST_SimpleStatement, self2, { + body: make_node(AST_Binary, self2, { + operator: "&&", + left: self2.condition, + right: self2.body.body + }) + }).optimize(compressor); + } + if (self2.body instanceof AST_EmptyStatement && self2.alternative instanceof AST_SimpleStatement) { + return make_node(AST_SimpleStatement, self2, { + body: make_node(AST_Binary, self2, { + operator: "||", + left: self2.condition, + right: self2.alternative.body + }) + }).optimize(compressor); + } + if (self2.body instanceof AST_Exit && self2.alternative instanceof AST_Exit && self2.body.TYPE == self2.alternative.TYPE) { + return make_node(self2.body.CTOR, self2, { + value: make_node(AST_Conditional, self2, { + condition: self2.condition, + consequent: self2.body.value || make_node(AST_Undefined, self2.body), + alternative: self2.alternative.value || make_node(AST_Undefined, self2.alternative) + }).transform(compressor) + }).optimize(compressor); + } + if (self2.body instanceof AST_If && !self2.body.alternative && !self2.alternative) { + self2 = make_node(AST_If, self2, { + condition: make_node(AST_Binary, self2.condition, { + operator: "&&", + left: self2.condition, + right: self2.body.condition + }), + body: self2.body.body, + alternative: null + }); + } + if (aborts(self2.body)) { + if (self2.alternative) { + var alt = self2.alternative; + self2.alternative = null; + return make_node(AST_BlockStatement, self2, { + body: [self2, alt] + }).optimize(compressor); + } + } + if (aborts(self2.alternative)) { + var body = self2.body; + self2.body = self2.alternative; + self2.condition = negated_is_best ? negated : self2.condition.negate(compressor); + self2.alternative = null; + return make_node(AST_BlockStatement, self2, { + body: [self2, body] + }).optimize(compressor); + } + return self2; +}); +def_optimize(AST_Switch, function(self2, compressor) { + if (!compressor.option("switches")) + return self2; + var branch; + var value = self2.expression.evaluate(compressor); + if (!(value instanceof AST_Node)) { + var orig = self2.expression; + self2.expression = make_node_from_constant(value, orig); + self2.expression = best_of_expression(self2.expression.transform(compressor), orig); + } + if (!compressor.option("dead_code")) + return self2; + if (value instanceof AST_Node) { + value = self2.expression.tail_node().evaluate(compressor); + } + var decl = []; + var body = []; + var default_branch; + var exact_match; + for (var i = 0, len = self2.body.length;i < len && !exact_match; i++) { + branch = self2.body[i]; + if (branch instanceof AST_Default) { + if (!default_branch) { + default_branch = branch; + } else { + eliminate_branch(branch, body[body.length - 1]); + } + } else if (!(value instanceof AST_Node)) { + var exp = branch.expression.evaluate(compressor); + if (!(exp instanceof AST_Node) && exp !== value) { + eliminate_branch(branch, body[body.length - 1]); + continue; + } + if (exp instanceof AST_Node && !exp.has_side_effects(compressor)) { + exp = branch.expression.tail_node().evaluate(compressor); + } + if (exp === value) { + exact_match = branch; + if (default_branch) { + var default_index = body.indexOf(default_branch); + body.splice(default_index, 1); + eliminate_branch(default_branch, body[default_index - 1]); + default_branch = null; + } + } + } + body.push(branch); + } + while (i < len) + eliminate_branch(self2.body[i++], body[body.length - 1]); + self2.body = body; + let default_or_exact = default_branch || exact_match; + default_branch = null; + exact_match = null; + if (body.every((branch2, i2) => (branch2 === default_or_exact || branch2.expression instanceof AST_Constant) && (branch2.body.length === 0 || aborts(branch2) || body.length - 1 === i2))) { + for (let i2 = 0;i2 < body.length; i2++) { + const branch2 = body[i2]; + for (let j = i2 + 1;j < body.length; j++) { + const next = body[j]; + if (next.body.length === 0) + continue; + const last_branch = j === body.length - 1; + const equivalentBranch = branches_equivalent(next, branch2, false); + if (equivalentBranch || last_branch && branches_equivalent(next, branch2, true)) { + if (!equivalentBranch && last_branch) { + next.body.push(make_node(AST_Break)); + } + let x = j - 1; + let fallthroughDepth = 0; + while (x > i2) { + if (is_inert_body(body[x--])) { + fallthroughDepth++; + } else { + break; + } + } + const plucked = body.splice(j - fallthroughDepth, 1 + fallthroughDepth); + body.splice(i2 + 1, 0, ...plucked); + i2 += plucked.length; + } + } + } + } + for (let i2 = 0;i2 < body.length; i2++) { + let branch2 = body[i2]; + if (branch2.body.length === 0) + continue; + if (!aborts(branch2)) + continue; + for (let j = i2 + 1;j < body.length; i2++, j++) { + let next = body[j]; + if (next.body.length === 0) + continue; + if (branches_equivalent(next, branch2, false) || j === body.length - 1 && branches_equivalent(next, branch2, true)) { + branch2.body = []; + branch2 = next; + continue; + } + break; + } + } + { + let i2 = body.length - 1; + for (;i2 >= 0; i2--) { + let bbody = body[i2].body; + if (is_break(bbody[bbody.length - 1], compressor)) + bbody.pop(); + if (!is_inert_body(body[i2])) + break; + } + i2++; + if (!default_or_exact || body.indexOf(default_or_exact) >= i2) { + for (let j = body.length - 1;j >= i2; j--) { + let branch2 = body[j]; + if (branch2 === default_or_exact) { + default_or_exact = null; + body.pop(); + } else if (!branch2.expression.has_side_effects(compressor)) { + body.pop(); + } else { + break; + } + } + } + } + DEFAULT: + if (default_or_exact) { + let default_index2 = body.indexOf(default_or_exact); + let default_body_index = default_index2; + for (;default_body_index < body.length - 1; default_body_index++) { + if (!is_inert_body(body[default_body_index])) + break; + } + if (default_body_index < body.length - 1) { + break DEFAULT; + } + let side_effect_index = body.length - 1; + for (;side_effect_index >= 0; side_effect_index--) { + let branch2 = body[side_effect_index]; + if (branch2 === default_or_exact) + continue; + if (branch2.expression.has_side_effects(compressor)) + break; + } + if (default_body_index > side_effect_index) { + let prev_body_index = default_index2 - 1; + for (;prev_body_index >= 0; prev_body_index--) { + if (!is_inert_body(body[prev_body_index])) + break; + } + let before = Math.max(side_effect_index, prev_body_index) + 1; + let after = default_index2; + if (side_effect_index > default_index2) { + after = side_effect_index; + body[side_effect_index].body = body[default_body_index].body; + } else { + default_or_exact.body = body[default_body_index].body; + } + body.splice(after + 1, default_body_index - after); + body.splice(before, default_index2 - before); + } + } + DEFAULT: + if (default_or_exact) { + let i2 = body.findIndex((branch2) => !is_inert_body(branch2)); + let caseBody; + if (i2 === body.length - 1) { + let branch2 = body[i2]; + if (has_nested_break(self2)) + break DEFAULT; + caseBody = make_node(AST_BlockStatement, branch2, { + body: branch2.body + }); + branch2.body = []; + } else if (i2 !== -1) { + break DEFAULT; + } + let sideEffect = body.find((branch2) => branch2 !== default_or_exact && branch2.expression.has_side_effects(compressor)); + if (!sideEffect) { + return make_node(AST_BlockStatement, self2, { + body: decl.concat(statement(self2.expression), default_or_exact.expression ? statement(default_or_exact.expression) : [], caseBody || []) + }).optimize(compressor); + } + const default_index2 = body.indexOf(default_or_exact); + body.splice(default_index2, 1); + default_or_exact = null; + if (caseBody) { + return make_node(AST_BlockStatement, self2, { + body: decl.concat(self2, caseBody) + }).optimize(compressor); + } + } + if (body.length > 0) { + body[0].body = decl.concat(body[0].body); + } + if (body.length == 0) { + return make_node(AST_BlockStatement, self2, { + body: decl.concat(statement(self2.expression)) + }).optimize(compressor); + } + if (body.length == 1 && !has_nested_break(self2)) { + let branch2 = body[0]; + return make_node(AST_If, self2, { + condition: make_node(AST_Binary, self2, { + operator: "===", + left: self2.expression, + right: branch2.expression + }), + body: make_node(AST_BlockStatement, branch2, { + body: branch2.body + }), + alternative: null + }).optimize(compressor); + } + if (body.length === 2 && default_or_exact && !has_nested_break(self2)) { + let branch2 = body[0] === default_or_exact ? body[1] : body[0]; + let exact_exp = default_or_exact.expression && statement(default_or_exact.expression); + if (aborts(body[0])) { + let first = body[0]; + if (is_break(first.body[first.body.length - 1], compressor)) { + first.body.pop(); + } + return make_node(AST_If, self2, { + condition: make_node(AST_Binary, self2, { + operator: "===", + left: self2.expression, + right: branch2.expression + }), + body: make_node(AST_BlockStatement, branch2, { + body: branch2.body + }), + alternative: make_node(AST_BlockStatement, default_or_exact, { + body: [].concat(exact_exp || [], default_or_exact.body) + }) + }).optimize(compressor); + } + let operator = "==="; + let consequent = make_node(AST_BlockStatement, branch2, { + body: branch2.body + }); + let always = make_node(AST_BlockStatement, default_or_exact, { + body: [].concat(exact_exp || [], default_or_exact.body) + }); + if (body[0] === default_or_exact) { + operator = "!=="; + let tmp = always; + always = consequent; + consequent = tmp; + } + return make_node(AST_BlockStatement, self2, { + body: [ + make_node(AST_If, self2, { + condition: make_node(AST_Binary, self2, { + operator, + left: self2.expression, + right: branch2.expression + }), + body: consequent, + alternative: null + }), + always + ] + }).optimize(compressor); + } + return self2; + function eliminate_branch(branch2, prev) { + if (prev && !aborts(prev)) { + prev.body = prev.body.concat(branch2.body); + } else { + trim_unreachable_code(compressor, branch2, decl); + } + } + function branches_equivalent(branch2, prev, insertBreak) { + let bbody = branch2.body; + let pbody = prev.body; + if (insertBreak) { + bbody = bbody.concat(make_node(AST_Break)); + } + if (bbody.length !== pbody.length) + return false; + let bblock = make_node(AST_BlockStatement, branch2, { body: bbody }); + let pblock = make_node(AST_BlockStatement, prev, { body: pbody }); + return bblock.equivalent_to(pblock); + } + function statement(body2) { + return make_node(AST_SimpleStatement, body2, { body: body2 }); + } + function has_nested_break(root) { + let has_break = false; + let tw = new TreeWalker((node) => { + if (has_break) + return true; + if (node instanceof AST_Lambda) + return true; + if (node instanceof AST_SimpleStatement) + return true; + if (!is_break(node, tw)) + return; + let parent = tw.parent(); + if (parent instanceof AST_SwitchBranch && parent.body[parent.body.length - 1] === node) { + return; + } + has_break = true; + }); + root.walk(tw); + return has_break; + } + function is_break(node, stack) { + return node instanceof AST_Break && stack.loopcontrol_target(node) === self2; + } + function is_inert_body(branch2) { + return !aborts(branch2) && !make_node(AST_BlockStatement, branch2, { + body: branch2.body + }).has_side_effects(compressor); + } +}); +def_optimize(AST_Try, function(self2, compressor) { + if (self2.bcatch && self2.bfinally && self2.bfinally.body.every(is_empty)) + self2.bfinally = null; + if (compressor.option("dead_code") && self2.body.body.every(is_empty)) { + var body = []; + if (self2.bcatch) { + trim_unreachable_code(compressor, self2.bcatch, body); + } + if (self2.bfinally) + body.push(...self2.bfinally.body); + return make_node(AST_BlockStatement, self2, { + body + }).optimize(compressor); + } + return self2; +}); +AST_Definitions.DEFMETHOD("to_assignments", function(compressor) { + var reduce_vars = compressor.option("reduce_vars"); + var assignments = []; + for (const def of this.definitions) { + if (def.value) { + var name = make_node(AST_SymbolRef, def.name, def.name); + assignments.push(make_node(AST_Assign, def, { + operator: "=", + logical: false, + left: name, + right: def.value + })); + if (reduce_vars) + name.definition().fixed = false; + } + const thedef = def.name.definition(); + thedef.eliminated++; + thedef.replaced--; + } + if (assignments.length == 0) + return null; + return make_sequence(this, assignments); +}); +def_optimize(AST_Definitions, function(self2) { + if (self2.definitions.length == 0) { + return make_node(AST_EmptyStatement, self2); + } + return self2; +}); +def_optimize(AST_VarDef, function(self2, compressor) { + if (self2.name instanceof AST_SymbolLet && self2.value != null && is_undefined(self2.value, compressor)) { + self2.value = null; + } + return self2; +}); +def_optimize(AST_Import, function(self2) { + return self2; +}); +def_optimize(AST_Call, function(self2, compressor) { + var exp = self2.expression; + var fn = exp; + inline_array_like_spread(self2.args); + var simple_args = self2.args.every((arg2) => !(arg2 instanceof AST_Expansion)); + if (compressor.option("reduce_vars") && fn instanceof AST_SymbolRef) { + fn = fn.fixed_value(); + } + var is_func = fn instanceof AST_Lambda; + if (is_func && fn.pinned()) + return self2; + if (compressor.option("unused") && simple_args && is_func && !fn.uses_arguments) { + var pos = 0, last = 0; + for (var i = 0, len = self2.args.length;i < len; i++) { + if (fn.argnames[i] instanceof AST_Expansion) { + if (has_flag(fn.argnames[i].expression, UNUSED)) + while (i < len) { + var node = self2.args[i++].drop_side_effect_free(compressor); + if (node) { + self2.args[pos++] = node; + } + } + else + while (i < len) { + self2.args[pos++] = self2.args[i++]; + } + last = pos; + break; + } + var trim2 = i >= fn.argnames.length; + if (trim2 || has_flag(fn.argnames[i], UNUSED)) { + var node = self2.args[i].drop_side_effect_free(compressor); + if (node) { + self2.args[pos++] = node; + } else if (!trim2) { + self2.args[pos++] = make_node(AST_Number, self2.args[i], { + value: 0 + }); + continue; + } + } else { + self2.args[pos++] = self2.args[i]; + } + last = pos; + } + self2.args.length = last; + } + if (exp instanceof AST_Dot && exp.expression instanceof AST_SymbolRef && exp.expression.name === "console" && exp.expression.definition().undeclared && exp.property === "assert") { + const condition = self2.args[0]; + if (condition) { + const value2 = condition.evaluate(compressor); + if (value2 === 1 || value2 === true) { + return make_node(AST_Undefined, self2); + } + } + } + if (compressor.option("unsafe") && !exp.contains_optional()) { + if (exp instanceof AST_Dot && exp.start.value === "Array" && exp.property === "from" && self2.args.length === 1) { + const [argument] = self2.args; + if (argument instanceof AST_Array) { + return make_node(AST_Array, argument, { + elements: argument.elements + }).optimize(compressor); + } + } + if (is_undeclared_ref(exp)) + switch (exp.name) { + case "Array": + if (self2.args.length != 1) { + return make_node(AST_Array, self2, { + elements: self2.args + }).optimize(compressor); + } else if (self2.args[0] instanceof AST_Number && self2.args[0].value <= 11) { + const elements2 = []; + for (let i2 = 0;i2 < self2.args[0].value; i2++) + elements2.push(new AST_Hole); + return new AST_Array({ elements: elements2 }); + } + break; + case "Object": + if (self2.args.length == 0) { + return make_node(AST_Object, self2, { + properties: [] + }); + } + break; + case "String": + if (self2.args.length == 0) + return make_node(AST_String, self2, { + value: "" + }); + if (self2.args.length <= 1) + return make_node(AST_Binary, self2, { + left: self2.args[0], + operator: "+", + right: make_node(AST_String, self2, { value: "" }) + }).optimize(compressor); + break; + case "Number": + if (self2.args.length == 0) + return make_node(AST_Number, self2, { + value: 0 + }); + if (self2.args.length == 1 && compressor.option("unsafe_math")) { + return make_node(AST_UnaryPrefix, self2, { + expression: self2.args[0], + operator: "+" + }).optimize(compressor); + } + break; + case "Symbol": + if (self2.args.length == 1 && self2.args[0] instanceof AST_String && compressor.option("unsafe_symbols")) + self2.args.length = 0; + break; + case "Boolean": + if (self2.args.length == 0) + return make_node(AST_False, self2); + if (self2.args.length == 1) + return make_node(AST_UnaryPrefix, self2, { + expression: make_node(AST_UnaryPrefix, self2, { + expression: self2.args[0], + operator: "!" + }), + operator: "!" + }).optimize(compressor); + break; + case "RegExp": + var params = []; + if (self2.args.length >= 1 && self2.args.length <= 2 && self2.args.every((arg2) => { + var value2 = arg2.evaluate(compressor); + params.push(value2); + return arg2 !== value2; + }) && regexp_is_safe(params[0])) { + let [source, flags] = params; + source = regexp_source_fix(new RegExp(source).source); + const rx = make_node(AST_RegExp, self2, { + value: { source, flags } + }); + if (rx._eval(compressor) !== rx) { + return rx; + } + } + break; + } + else if (exp instanceof AST_Dot) + switch (exp.property) { + case "toString": + if (self2.args.length == 0 && !exp.expression.may_throw_on_access(compressor)) { + return make_node(AST_Binary, self2, { + left: make_node(AST_String, self2, { value: "" }), + operator: "+", + right: exp.expression + }).optimize(compressor); + } + break; + case "join": + if (exp.expression instanceof AST_Array) + EXIT: { + var separator; + if (self2.args.length > 0) { + separator = self2.args[0].evaluate(compressor); + if (separator === self2.args[0]) + break EXIT; + } + var elements = []; + var consts = []; + for (var i = 0, len = exp.expression.elements.length;i < len; i++) { + var el = exp.expression.elements[i]; + if (el instanceof AST_Expansion) + break EXIT; + var value = el.evaluate(compressor); + if (value !== el) { + consts.push(value); + } else { + if (consts.length > 0) { + elements.push(make_node(AST_String, self2, { + value: consts.join(separator) + })); + consts.length = 0; + } + elements.push(el); + } + } + if (consts.length > 0) { + elements.push(make_node(AST_String, self2, { + value: consts.join(separator) + })); + } + if (elements.length == 0) + return make_node(AST_String, self2, { value: "" }); + if (elements.length == 1) { + if (elements[0].is_string(compressor)) { + return elements[0]; + } + return make_node(AST_Binary, elements[0], { + operator: "+", + left: make_node(AST_String, self2, { value: "" }), + right: elements[0] + }); + } + if (separator == "") { + var first; + if (elements[0].is_string(compressor) || elements[1].is_string(compressor)) { + first = elements.shift(); + } else { + first = make_node(AST_String, self2, { value: "" }); + } + return elements.reduce(function(prev, el2) { + return make_node(AST_Binary, el2, { + operator: "+", + left: prev, + right: el2 + }); + }, first).optimize(compressor); + } + var node = self2.clone(); + node.expression = node.expression.clone(); + node.expression.expression = node.expression.expression.clone(); + node.expression.expression.elements = elements; + return best_of(compressor, self2, node); + } + break; + case "charAt": + if (exp.expression.is_string(compressor)) { + var arg = self2.args[0]; + var index = arg ? arg.evaluate(compressor) : 0; + if (index !== arg) { + return make_node(AST_Sub, exp, { + expression: exp.expression, + property: make_node_from_constant(index | 0, arg || exp) + }).optimize(compressor); + } + } + break; + case "apply": + if (self2.args.length == 2 && self2.args[1] instanceof AST_Array) { + var args = self2.args[1].elements.slice(); + args.unshift(self2.args[0]); + return make_node(AST_Call, self2, { + expression: make_node(AST_Dot, exp, { + expression: exp.expression, + optional: false, + property: "call" + }), + args + }).optimize(compressor); + } + break; + case "call": + var func = exp.expression; + if (func instanceof AST_SymbolRef) { + func = func.fixed_value(); + } + if (func instanceof AST_Lambda && !func.contains_this()) { + return (self2.args.length ? make_sequence(this, [ + self2.args[0], + make_node(AST_Call, self2, { + expression: exp.expression, + args: self2.args.slice(1) + }) + ]) : make_node(AST_Call, self2, { + expression: exp.expression, + args: [] + })).optimize(compressor); + } + break; + } + } + if (compressor.option("unsafe_Function") && is_undeclared_ref(exp) && exp.name == "Function") { + if (self2.args.length == 0) + return make_empty_function(self2).optimize(compressor); + if (self2.args.every((x) => x instanceof AST_String)) { + try { + var code = "n(function(" + self2.args.slice(0, -1).map(function(arg2) { + return arg2.value; + }).join(",") + "){" + self2.args[self2.args.length - 1].value + "})"; + var ast = parse(code); + var mangle = compressor.mangle_options(); + ast.figure_out_scope(mangle); + var comp = new Compressor(compressor.options, { + mangle_options: compressor._mangle_options + }); + ast = ast.transform(comp); + ast.figure_out_scope(mangle); + ast.compute_char_frequency(mangle); + ast.mangle_names(mangle); + var fun; + walk(ast, (node2) => { + if (is_func_expr(node2)) { + fun = node2; + return walk_abort; + } + }); + var code = OutputStream(); + AST_BlockStatement.prototype._codegen.call(fun, fun, code); + self2.args = [ + make_node(AST_String, self2, { + value: fun.argnames.map(function(arg2) { + return arg2.print_to_string(); + }).join(",") + }), + make_node(AST_String, self2.args[self2.args.length - 1], { + value: code.get().replace(/^{|}$/g, "") + }) + ]; + return self2; + } catch (ex) { + if (!(ex instanceof JS_Parse_Error)) { + throw ex; + } + } + } + } + return inline_into_call(self2, compressor); +}); +AST_Node.DEFMETHOD("contains_optional", function() { + if (this instanceof AST_PropAccess || this instanceof AST_Call || this instanceof AST_Chain) { + if (this.optional) { + return true; + } else { + return this.expression.contains_optional(); + } + } else { + return false; + } +}); +def_optimize(AST_New, function(self2, compressor) { + if (compressor.option("unsafe") && is_undeclared_ref(self2.expression) && ["Object", "RegExp", "Function", "Error", "Array"].includes(self2.expression.name)) + return make_node(AST_Call, self2, self2).transform(compressor); + return self2; +}); +def_optimize(AST_Sequence, function(self2, compressor) { + if (!compressor.option("side_effects")) + return self2; + var expressions = []; + filter_for_side_effects(); + var end = expressions.length - 1; + trim_right_for_undefined(); + if (end == 0) { + self2 = maintain_this_binding(compressor.parent(), compressor.self(), expressions[0]); + if (!(self2 instanceof AST_Sequence)) + self2 = self2.optimize(compressor); + return self2; + } + self2.expressions = expressions; + return self2; + function filter_for_side_effects() { + var first = first_in_statement(compressor); + var last = self2.expressions.length - 1; + self2.expressions.forEach(function(expr, index) { + if (index < last) + expr = expr.drop_side_effect_free(compressor, first); + if (expr) { + merge_sequence(expressions, expr); + first = false; + } + }); + } + function trim_right_for_undefined() { + while (end > 0 && is_undefined(expressions[end], compressor)) + end--; + if (end < expressions.length - 1) { + expressions[end] = make_node(AST_UnaryPrefix, self2, { + operator: "void", + expression: expressions[end] + }); + expressions.length = end + 1; + } + } +}); +AST_Unary.DEFMETHOD("lift_sequences", function(compressor) { + if (compressor.option("sequences")) { + if (this.expression instanceof AST_Sequence) { + var x = this.expression.expressions.slice(); + var e = this.clone(); + e.expression = x.pop(); + x.push(e); + return make_sequence(this, x).optimize(compressor); + } + } + return this; +}); +def_optimize(AST_UnaryPostfix, function(self2, compressor) { + return self2.lift_sequences(compressor); +}); +def_optimize(AST_UnaryPrefix, function(self2, compressor) { + var e = self2.expression; + if (self2.operator == "delete" && !(e instanceof AST_SymbolRef || e instanceof AST_PropAccess || e instanceof AST_Chain || is_identifier_atom(e))) { + return make_sequence(self2, [e, make_node(AST_True, self2)]).optimize(compressor); + } + var seq = self2.lift_sequences(compressor); + if (seq !== self2) { + return seq; + } + if (compressor.option("side_effects") && self2.operator == "void") { + e = e.drop_side_effect_free(compressor); + if (e) { + self2.expression = e; + return self2; + } else { + return make_node(AST_Undefined, self2).optimize(compressor); + } + } + if (compressor.in_boolean_context()) { + switch (self2.operator) { + case "!": + if (e instanceof AST_UnaryPrefix && e.operator == "!") { + return e.expression; + } + if (e instanceof AST_Binary) { + self2 = best_of(compressor, self2, e.negate(compressor, first_in_statement(compressor))); + } + break; + case "typeof": + return (e instanceof AST_SymbolRef ? make_node(AST_True, self2) : make_sequence(self2, [ + e, + make_node(AST_True, self2) + ])).optimize(compressor); + } + } + if (self2.operator == "-" && e instanceof AST_Infinity) { + e = e.transform(compressor); + } + if (e instanceof AST_Binary && (self2.operator == "+" || self2.operator == "-") && (e.operator == "*" || e.operator == "/" || e.operator == "%")) { + return make_node(AST_Binary, self2, { + operator: e.operator, + left: make_node(AST_UnaryPrefix, e.left, { + operator: self2.operator, + expression: e.left + }), + right: e.right + }); + } + if (compressor.option("evaluate")) { + if (self2.operator === "~" && self2.expression instanceof AST_UnaryPrefix && self2.expression.operator === "~" && (compressor.in_32_bit_context(false) || self2.expression.expression.is_32_bit_integer(compressor))) { + return self2.expression.expression; + } + if (self2.operator === "~" && e instanceof AST_Binary && e.operator === "^") { + if (e.left instanceof AST_UnaryPrefix && e.left.operator === "~") { + e.left = e.left.bitwise_negate(compressor, true); + } else { + e.right = e.right.bitwise_negate(compressor, true); + } + return e; + } + } + if (self2.operator != "-" || !(e instanceof AST_Number || e instanceof AST_Infinity || e instanceof AST_BigInt)) { + var ev = self2.evaluate(compressor); + if (ev !== self2) { + ev = make_node_from_constant(ev, self2).optimize(compressor); + return best_of(compressor, ev, self2); + } + } + return self2; +}); +AST_Binary.DEFMETHOD("lift_sequences", function(compressor) { + if (compressor.option("sequences")) { + if (this.left instanceof AST_Sequence) { + var x = this.left.expressions.slice(); + var e = this.clone(); + e.left = x.pop(); + x.push(e); + return make_sequence(this, x).optimize(compressor); + } + if (this.right instanceof AST_Sequence && !this.left.has_side_effects(compressor)) { + var assign = this.operator == "=" && this.left instanceof AST_SymbolRef; + var x = this.right.expressions; + var last = x.length - 1; + for (var i = 0;i < last; i++) { + if (!assign && x[i].has_side_effects(compressor)) + break; + } + if (i == last) { + x = x.slice(); + var e = this.clone(); + e.right = x.pop(); + x.push(e); + return make_sequence(this, x).optimize(compressor); + } else if (i > 0) { + var e = this.clone(); + e.right = make_sequence(this.right, x.slice(i)); + x = x.slice(0, i); + x.push(e); + return make_sequence(this, x).optimize(compressor); + } + } + } + return this; +}); +var commutativeOperators = makePredicate("== === != !== * & | ^"); +function is_object(node) { + return node instanceof AST_Array || node instanceof AST_Lambda || node instanceof AST_Object || node instanceof AST_Class; +} +def_optimize(AST_Binary, function(self2, compressor) { + function reversible() { + return self2.left.is_constant() || self2.right.is_constant() || !self2.left.has_side_effects(compressor) && !self2.right.has_side_effects(compressor); + } + function reverse(op) { + if (reversible()) { + if (op) + self2.operator = op; + var tmp = self2.left; + self2.left = self2.right; + self2.right = tmp; + } + } + if (compressor.option("lhs_constants") && commutativeOperators.has(self2.operator)) { + if (self2.right.is_constant() && !self2.left.is_constant()) { + if (!(self2.left instanceof AST_Binary && PRECEDENCE[self2.left.operator] >= PRECEDENCE[self2.operator])) { + reverse(); + } + } + } + self2 = self2.lift_sequences(compressor); + if (compressor.option("comparisons")) + switch (self2.operator) { + case "===": + case "!==": + var is_strict_comparison = true; + if (self2.left.is_string(compressor) && self2.right.is_string(compressor) || self2.left.is_number(compressor) && self2.right.is_number(compressor) || self2.left.is_bigint(compressor) && self2.right.is_bigint(compressor) || self2.left.is_boolean() && self2.right.is_boolean() || self2.left.equivalent_to(self2.right)) { + self2.operator = self2.operator.substr(0, 2); + } + case "==": + case "!=": + if (!is_strict_comparison && is_undefined(self2.left, compressor)) { + self2.left = make_node(AST_Null, self2.left); + } else if (!is_strict_comparison && is_undefined(self2.right, compressor)) { + self2.right = make_node(AST_Null, self2.right); + } else if (compressor.option("typeofs") && self2.left instanceof AST_String && self2.left.value == "undefined" && self2.right instanceof AST_UnaryPrefix && self2.right.operator == "typeof") { + var expr = self2.right.expression; + if (expr instanceof AST_SymbolRef ? expr.is_declared(compressor) : !(expr instanceof AST_PropAccess && compressor.option("ie8"))) { + self2.right = expr; + self2.left = make_node(AST_Undefined, self2.left).optimize(compressor); + if (self2.operator.length == 2) + self2.operator += "="; + } + } else if (compressor.option("typeofs") && self2.left instanceof AST_UnaryPrefix && self2.left.operator == "typeof" && self2.right instanceof AST_String && self2.right.value == "undefined") { + var expr = self2.left.expression; + if (expr instanceof AST_SymbolRef ? expr.is_declared(compressor) : !(expr instanceof AST_PropAccess && compressor.option("ie8"))) { + self2.left = expr; + self2.right = make_node(AST_Undefined, self2.right).optimize(compressor); + if (self2.operator.length == 2) + self2.operator += "="; + } + } else if (self2.left instanceof AST_SymbolRef && self2.right instanceof AST_SymbolRef && self2.left.definition() === self2.right.definition() && is_object(self2.left.fixed_value())) { + return make_node(self2.operator[0] == "=" ? AST_True : AST_False, self2); + } else if (self2.left.is_32_bit_integer(compressor) && self2.right.is_32_bit_integer(compressor)) { + const not = (node) => make_node(AST_UnaryPrefix, node, { + operator: "!", + expression: node + }); + const booleanify = (node, truthy) => { + if (truthy) { + return compressor.in_boolean_context() ? node : not(not(node)); + } else { + return not(node); + } + }; + if (self2.left instanceof AST_Number && self2.left.value === 0) { + return booleanify(self2.right, self2.operator[0] === "!"); + } + if (self2.right instanceof AST_Number && self2.right.value === 0) { + return booleanify(self2.left, self2.operator[0] === "!"); + } + let and_op, x, mask; + if ((and_op = self2.left instanceof AST_Binary ? self2.left : self2.right instanceof AST_Binary ? self2.right : null) && (mask = and_op === self2.left ? self2.right : self2.left) && and_op.operator === "&" && mask instanceof AST_Number && mask.is_32_bit_integer(compressor) && (x = and_op.left.equivalent_to(mask) ? and_op.right : and_op.right.equivalent_to(mask) ? and_op.left : null)) { + let optimized = booleanify(make_node(AST_Binary, self2, { + operator: "&", + left: mask, + right: make_node(AST_UnaryPrefix, self2, { + operator: "~", + expression: x + }) + }), self2.operator[0] === "!"); + return best_of(compressor, optimized, self2); + } + } + break; + case "&&": + case "||": + var lhs = self2.left; + if (lhs.operator == self2.operator) { + lhs = lhs.right; + } + if (lhs instanceof AST_Binary && lhs.operator == (self2.operator == "&&" ? "!==" : "===") && self2.right instanceof AST_Binary && lhs.operator == self2.right.operator && (is_undefined(lhs.left, compressor) && self2.right.left instanceof AST_Null || lhs.left instanceof AST_Null && is_undefined(self2.right.left, compressor)) && !lhs.right.has_side_effects(compressor) && lhs.right.equivalent_to(self2.right.right)) { + var combined = make_node(AST_Binary, self2, { + operator: lhs.operator.slice(0, -1), + left: make_node(AST_Null, self2), + right: lhs.right + }); + if (lhs !== self2.left) { + combined = make_node(AST_Binary, self2, { + operator: self2.operator, + left: self2.left.left, + right: combined + }); + } + return combined; + } + break; + } + if (self2.operator == "+" && compressor.in_boolean_context()) { + var ll = self2.left.evaluate(compressor); + var rr = self2.right.evaluate(compressor); + if (ll && typeof ll == "string") { + return make_sequence(self2, [ + self2.right, + make_node(AST_True, self2) + ]).optimize(compressor); + } + if (rr && typeof rr == "string") { + return make_sequence(self2, [ + self2.left, + make_node(AST_True, self2) + ]).optimize(compressor); + } + } + if (compressor.option("comparisons") && self2.is_boolean()) { + if (!(compressor.parent() instanceof AST_Binary) || compressor.parent() instanceof AST_Assign) { + var negated = make_node(AST_UnaryPrefix, self2, { + operator: "!", + expression: self2.negate(compressor, first_in_statement(compressor)) + }); + self2 = best_of(compressor, self2, negated); + } + if (compressor.option("unsafe_comps")) { + switch (self2.operator) { + case "<": + reverse(">"); + break; + case "<=": + reverse(">="); + break; + } + } + } + if (self2.operator == "+") { + if (self2.right instanceof AST_String && self2.right.getValue() == "" && self2.left.is_string(compressor)) { + return self2.left; + } + if (self2.left instanceof AST_String && self2.left.getValue() == "" && self2.right.is_string(compressor)) { + return self2.right; + } + if (self2.left instanceof AST_Binary && self2.left.operator == "+" && self2.left.left instanceof AST_String && self2.left.left.getValue() == "" && self2.right.is_string(compressor)) { + self2.left = self2.left.right; + return self2; + } + } + if (compressor.option("evaluate")) { + switch (self2.operator) { + case "&&": + var ll = has_flag(self2.left, TRUTHY) ? true : has_flag(self2.left, FALSY) ? false : self2.left.evaluate(compressor); + if (!ll) { + return maintain_this_binding(compressor.parent(), compressor.self(), self2.left).optimize(compressor); + } else if (!(ll instanceof AST_Node)) { + return make_sequence(self2, [self2.left, self2.right]).optimize(compressor); + } + var rr = self2.right.evaluate(compressor); + if (!rr) { + if (compressor.in_boolean_context()) { + return make_sequence(self2, [ + self2.left, + make_node(AST_False, self2) + ]).optimize(compressor); + } else { + set_flag(self2, FALSY); + } + } else if (!(rr instanceof AST_Node)) { + var parent = compressor.parent(); + if (parent.operator == "&&" && parent.left === compressor.self() || compressor.in_boolean_context()) { + return self2.left.optimize(compressor); + } + } + if (self2.left.operator == "||") { + var lr = self2.left.right.evaluate(compressor); + if (!lr) + return make_node(AST_Conditional, self2, { + condition: self2.left.left, + consequent: self2.right, + alternative: self2.left.right + }).optimize(compressor); + } + break; + case "||": + var ll = has_flag(self2.left, TRUTHY) ? true : has_flag(self2.left, FALSY) ? false : self2.left.evaluate(compressor); + if (!ll) { + return make_sequence(self2, [self2.left, self2.right]).optimize(compressor); + } else if (!(ll instanceof AST_Node)) { + return maintain_this_binding(compressor.parent(), compressor.self(), self2.left).optimize(compressor); + } + var rr = self2.right.evaluate(compressor); + if (!rr) { + var parent = compressor.parent(); + if (parent.operator == "||" && parent.left === compressor.self() || compressor.in_boolean_context()) { + return self2.left.optimize(compressor); + } + } else if (!(rr instanceof AST_Node)) { + if (compressor.in_boolean_context()) { + return make_sequence(self2, [ + self2.left, + make_node(AST_True, self2) + ]).optimize(compressor); + } else { + set_flag(self2, TRUTHY); + } + } + if (self2.left.operator == "&&") { + var lr = self2.left.right.evaluate(compressor); + if (lr && !(lr instanceof AST_Node)) + return make_node(AST_Conditional, self2, { + condition: self2.left.left, + consequent: self2.left.right, + alternative: self2.right + }).optimize(compressor); + } + break; + case "??": + if (is_nullish(self2.left, compressor)) { + return self2.right; + } + var ll = self2.left.evaluate(compressor); + if (!(ll instanceof AST_Node)) { + return ll == null ? self2.right : self2.left; + } + if (compressor.in_boolean_context()) { + const rr2 = self2.right.evaluate(compressor); + if (!(rr2 instanceof AST_Node) && !rr2) { + return self2.left; + } + } + } + var associative = true; + switch (self2.operator) { + case "+": + if (self2.right instanceof AST_Constant && self2.left instanceof AST_Binary && self2.left.operator == "+" && self2.left.is_string(compressor)) { + var binary = make_node(AST_Binary, self2, { + operator: "+", + left: self2.left.right, + right: self2.right + }); + var r = binary.optimize(compressor); + if (binary !== r) { + self2 = make_node(AST_Binary, self2, { + operator: "+", + left: self2.left.left, + right: r + }); + } + } + if (self2.left instanceof AST_Binary && self2.left.operator == "+" && self2.left.is_string(compressor) && self2.right instanceof AST_Binary && self2.right.operator == "+" && self2.right.is_string(compressor)) { + var binary = make_node(AST_Binary, self2, { + operator: "+", + left: self2.left.right, + right: self2.right.left + }); + var m = binary.optimize(compressor); + if (binary !== m) { + self2 = make_node(AST_Binary, self2, { + operator: "+", + left: make_node(AST_Binary, self2.left, { + operator: "+", + left: self2.left.left, + right: m + }), + right: self2.right.right + }); + } + } + if (self2.right instanceof AST_UnaryPrefix && self2.right.operator == "-" && self2.left.is_number_or_bigint(compressor)) { + self2 = make_node(AST_Binary, self2, { + operator: "-", + left: self2.left, + right: self2.right.expression + }); + break; + } + if (self2.left instanceof AST_UnaryPrefix && self2.left.operator == "-" && reversible() && self2.right.is_number_or_bigint(compressor)) { + self2 = make_node(AST_Binary, self2, { + operator: "-", + left: self2.right, + right: self2.left.expression + }); + break; + } + if (self2.left instanceof AST_TemplateString) { + var l = self2.left; + var r = self2.right.evaluate(compressor); + if (r != self2.right) { + l.segments[l.segments.length - 1].value += String(r); + return l; + } + } + if (self2.right instanceof AST_TemplateString) { + var r = self2.right; + var l = self2.left.evaluate(compressor); + if (l != self2.left) { + r.segments[0].value = String(l) + r.segments[0].value; + return r; + } + } + if (self2.left instanceof AST_TemplateString && self2.right instanceof AST_TemplateString) { + var l = self2.left; + var segments = l.segments; + var r = self2.right; + segments[segments.length - 1].value += r.segments[0].value; + for (var i = 1;i < r.segments.length; i++) { + segments.push(r.segments[i]); + } + return l; + } + case "*": + associative = compressor.option("unsafe_math"); + case "&": + case "|": + case "^": + if (self2.left.is_number_or_bigint(compressor) && self2.right.is_number_or_bigint(compressor) && reversible() && !(self2.left instanceof AST_Binary && self2.left.operator != self2.operator && PRECEDENCE[self2.left.operator] >= PRECEDENCE[self2.operator])) { + var reversed = make_node(AST_Binary, self2, { + operator: self2.operator, + left: self2.right, + right: self2.left + }); + if (self2.right instanceof AST_Constant && !(self2.left instanceof AST_Constant)) { + self2 = best_of(compressor, reversed, self2); + } else { + self2 = best_of(compressor, self2, reversed); + } + } + if (associative && self2.is_number_or_bigint(compressor)) { + if (self2.right instanceof AST_Binary && self2.right.operator == self2.operator) { + self2 = make_node(AST_Binary, self2, { + operator: self2.operator, + left: make_node(AST_Binary, self2.left, { + operator: self2.operator, + left: self2.left, + right: self2.right.left, + start: self2.left.start, + end: self2.right.left.end + }), + right: self2.right.right + }); + } + if (self2.right instanceof AST_Constant && self2.left instanceof AST_Binary && self2.left.operator == self2.operator) { + if (self2.left.left instanceof AST_Constant) { + self2 = make_node(AST_Binary, self2, { + operator: self2.operator, + left: make_node(AST_Binary, self2.left, { + operator: self2.operator, + left: self2.left.left, + right: self2.right, + start: self2.left.left.start, + end: self2.right.end + }), + right: self2.left.right + }); + } else if (self2.left.right instanceof AST_Constant) { + self2 = make_node(AST_Binary, self2, { + operator: self2.operator, + left: make_node(AST_Binary, self2.left, { + operator: self2.operator, + left: self2.left.right, + right: self2.right, + start: self2.left.right.start, + end: self2.right.end + }), + right: self2.left.left + }); + } + } + if (self2.left instanceof AST_Binary && self2.left.operator == self2.operator && self2.left.right instanceof AST_Constant && self2.right instanceof AST_Binary && self2.right.operator == self2.operator && self2.right.left instanceof AST_Constant) { + self2 = make_node(AST_Binary, self2, { + operator: self2.operator, + left: make_node(AST_Binary, self2.left, { + operator: self2.operator, + left: make_node(AST_Binary, self2.left.left, { + operator: self2.operator, + left: self2.left.right, + right: self2.right.left, + start: self2.left.right.start, + end: self2.right.left.end + }), + right: self2.left.left + }), + right: self2.right.right + }); + } + } + } + if (bitwise_binop.has(self2.operator)) { + let y, z, x_node, y_node, z_node = self2.left; + if (self2.operator === "&" && self2.right instanceof AST_Binary && self2.right.operator === "|" && typeof (z = self2.left.evaluate(compressor)) === "number") { + if (typeof (y = self2.right.right.evaluate(compressor)) === "number") { + x_node = self2.right.left; + y_node = self2.right.right; + } else if (typeof (y = self2.right.left.evaluate(compressor)) === "number") { + x_node = self2.right.right; + y_node = self2.right.left; + } + if (x_node && y_node) { + if ((y & z) === 0) { + self2 = make_node(AST_Binary, self2, { + operator: self2.operator, + left: z_node, + right: x_node + }); + } else { + const reordered_ops = make_node(AST_Binary, self2, { + operator: "|", + left: make_node(AST_Binary, self2, { + operator: "&", + left: x_node, + right: z_node + }), + right: make_node_from_constant(y & z, y_node) + }); + self2 = best_of(compressor, self2, reordered_ops); + } + } + } + if ((self2.operator === "|" || self2.operator === "&") && self2.left.equivalent_to(self2.right) && !self2.left.has_side_effects(compressor) && compressor.in_32_bit_context(true)) { + self2.left = make_node(AST_Number, self2, { value: 0 }); + self2.operator = "|"; + } + if (self2.operator === "^" && self2.left instanceof AST_UnaryPrefix && self2.left.operator === "~" && self2.right instanceof AST_UnaryPrefix && self2.right.operator === "~") { + self2 = make_node(AST_Binary, self2, { + operator: "^", + left: self2.left.expression, + right: self2.right.expression + }); + } + if ((self2.operator === "<<" || self2.operator === ">>") && self2.right instanceof AST_Number && self2.right.value === 0) { + self2.operator = "|"; + } + const zero_side = self2.right instanceof AST_Number && self2.right.value === 0 ? self2.right : self2.left instanceof AST_Number && self2.left.value === 0 ? self2.left : null; + const non_zero_side = zero_side && (zero_side === self2.right ? self2.left : self2.right); + if (zero_side && (self2.operator === "|" || self2.operator === "^") && (non_zero_side.is_32_bit_integer(compressor) || compressor.in_32_bit_context(true))) { + return non_zero_side; + } + if (zero_side && self2.operator === "&" && !non_zero_side.has_side_effects(compressor) && non_zero_side.is_32_bit_integer(compressor)) { + return zero_side; + } + const is_full_mask = (node) => node instanceof AST_Number && node.value === -1 || node instanceof AST_UnaryPrefix && node.operator === "-" && node.expression instanceof AST_Number && node.expression.value === 1; + const full_mask = is_full_mask(self2.right) ? self2.right : is_full_mask(self2.left) ? self2.left : null; + const other_side = full_mask === self2.right ? self2.left : self2.right; + if (full_mask && self2.operator === "&" && (other_side.is_32_bit_integer(compressor) || compressor.in_32_bit_context(true))) { + return other_side; + } + if (full_mask && self2.operator === "^" && (other_side.is_32_bit_integer(compressor) || compressor.in_32_bit_context(true))) { + return other_side.bitwise_negate(compressor); + } + } + } + if (self2.right instanceof AST_Binary && self2.right.operator == self2.operator && (lazy_op.has(self2.operator) || self2.operator == "+" && (self2.right.left.is_string(compressor) || self2.left.is_string(compressor) && self2.right.right.is_string(compressor)))) { + self2.left = make_node(AST_Binary, self2.left, { + operator: self2.operator, + left: self2.left.transform(compressor), + right: self2.right.left.transform(compressor) + }); + self2.right = self2.right.right.transform(compressor); + return self2.transform(compressor); + } + var ev = self2.evaluate(compressor); + if (ev !== self2) { + ev = make_node_from_constant(ev, self2).optimize(compressor); + return best_of(compressor, ev, self2); + } + return self2; +}); +def_optimize(AST_SymbolExport, function(self2) { + return self2; +}); +def_optimize(AST_SymbolRef, function(self2, compressor) { + if (!compressor.option("ie8") && is_undeclared_ref(self2) && !compressor.find_parent(AST_With)) { + switch (self2.name) { + case "undefined": + return make_node(AST_Undefined, self2).optimize(compressor); + case "NaN": + return make_node(AST_NaN, self2).optimize(compressor); + case "Infinity": + return make_node(AST_Infinity, self2).optimize(compressor); + } + } + if (compressor.option("reduce_vars") && !compressor.is_lhs()) { + return inline_into_symbolref(self2, compressor); + } else { + return self2; + } +}); +function is_atomic(lhs, self2) { + return lhs instanceof AST_SymbolRef || lhs.TYPE === self2.TYPE; +} +def_optimize(AST_Undefined, function(self2, compressor) { + if (compressor.option("unsafe_undefined")) { + var undef = find_variable(compressor, "undefined"); + if (undef) { + var ref = make_node(AST_SymbolRef, self2, { + name: "undefined", + scope: undef.scope, + thedef: undef + }); + set_flag(ref, UNDEFINED); + return ref; + } + } + var lhs = compressor.is_lhs(); + if (lhs && is_atomic(lhs, self2)) + return self2; + return make_node(AST_UnaryPrefix, self2, { + operator: "void", + expression: make_node(AST_Number, self2, { + value: 0 + }) + }); +}); +def_optimize(AST_Infinity, function(self2, compressor) { + var lhs = compressor.is_lhs(); + if (lhs && is_atomic(lhs, self2)) + return self2; + if (compressor.option("keep_infinity") && !(lhs && !is_atomic(lhs, self2)) && !find_variable(compressor, "Infinity")) { + return self2; + } + return make_node(AST_Binary, self2, { + operator: "/", + left: make_node(AST_Number, self2, { + value: 1 + }), + right: make_node(AST_Number, self2, { + value: 0 + }) + }); +}); +def_optimize(AST_NaN, function(self2, compressor) { + var lhs = compressor.is_lhs(); + if (lhs && !is_atomic(lhs, self2) || find_variable(compressor, "NaN")) { + return make_node(AST_Binary, self2, { + operator: "/", + left: make_node(AST_Number, self2, { + value: 0 + }), + right: make_node(AST_Number, self2, { + value: 0 + }) + }); + } + return self2; +}); +var ASSIGN_OPS = makePredicate("+ - / * % >> << >>> | ^ &"); +var ASSIGN_OPS_COMMUTATIVE = makePredicate("* | ^ &"); +def_optimize(AST_Assign, function(self2, compressor) { + if (self2.logical) { + return self2.lift_sequences(compressor); + } + var def; + if (self2.operator === "=" && self2.left instanceof AST_SymbolRef && self2.left.name !== "arguments" && !(def = self2.left.definition()).undeclared && self2.right.equivalent_to(self2.left)) { + return self2.right; + } + if (compressor.option("dead_code") && self2.left instanceof AST_SymbolRef && (def = self2.left.definition()).scope === compressor.find_parent(AST_Lambda)) { + var level = 0, node, parent = self2; + do { + node = parent; + parent = compressor.parent(level++); + if (parent instanceof AST_Exit) { + if (in_try(level, parent)) + break; + if (is_reachable(def.scope, [def])) + break; + if (self2.operator == "=") + return self2.right; + def.fixed = false; + return make_node(AST_Binary, self2, { + operator: self2.operator.slice(0, -1), + left: self2.left, + right: self2.right + }).optimize(compressor); + } + } while (parent instanceof AST_Binary && parent.right === node || parent instanceof AST_Sequence && parent.tail_node() === node); + } + self2 = self2.lift_sequences(compressor); + if (self2.operator == "=" && self2.left instanceof AST_SymbolRef && self2.right instanceof AST_Binary) { + if (self2.right.left instanceof AST_SymbolRef && self2.right.left.name == self2.left.name && ASSIGN_OPS.has(self2.right.operator)) { + self2.operator = self2.right.operator + "="; + self2.right = self2.right.right; + } else if (self2.right.right instanceof AST_SymbolRef && self2.right.right.name == self2.left.name && ASSIGN_OPS_COMMUTATIVE.has(self2.right.operator) && !self2.right.left.has_side_effects(compressor)) { + self2.operator = self2.right.operator + "="; + self2.right = self2.right.left; + } + } + return self2; + function in_try(level2, node2) { + function may_assignment_throw() { + const right = self2.right; + self2.right = make_node(AST_Null, right); + const may_throw = node2.may_throw(compressor); + self2.right = right; + return may_throw; + } + var stop_at = self2.left.definition().scope.get_defun_scope(); + var parent2; + while ((parent2 = compressor.parent(level2++)) !== stop_at) { + if (parent2 instanceof AST_Try) { + if (parent2.bfinally) + return true; + if (parent2.bcatch && may_assignment_throw()) + return true; + } + } + } +}); +def_optimize(AST_DefaultAssign, function(self2, compressor) { + if (!compressor.option("evaluate")) { + return self2; + } + var evaluateRight = self2.right.evaluate(compressor); + let lambda, iife; + if (evaluateRight === undefined) { + if ((lambda = compressor.parent()) instanceof AST_Lambda ? compressor.option("keep_fargs") === false || (iife = compressor.parent(1)).TYPE === "Call" && iife.expression === lambda : true) { + self2 = self2.left; + } + } else if (evaluateRight !== self2.right) { + evaluateRight = make_node_from_constant(evaluateRight, self2.right); + self2.right = best_of_expression(evaluateRight, self2.right); + } + return self2; +}); +function is_nullish_check(check, check_subject, compressor) { + if (check_subject.may_throw(compressor)) + return false; + let nullish_side; + if (check instanceof AST_Binary && check.operator === "==" && ((nullish_side = is_nullish(check.left, compressor) && check.left) || (nullish_side = is_nullish(check.right, compressor) && check.right)) && (nullish_side === check.left ? check.right : check.left).equivalent_to(check_subject)) { + return true; + } + if (check instanceof AST_Binary && check.operator === "||") { + let null_cmp; + let undefined_cmp; + const find_comparison = (cmp) => { + if (!(cmp instanceof AST_Binary && (cmp.operator === "===" || cmp.operator === "=="))) { + return false; + } + let found = 0; + let defined_side; + if (cmp.left instanceof AST_Null) { + found++; + null_cmp = cmp; + defined_side = cmp.right; + } + if (cmp.right instanceof AST_Null) { + found++; + null_cmp = cmp; + defined_side = cmp.left; + } + if (is_undefined(cmp.left, compressor)) { + found++; + undefined_cmp = cmp; + defined_side = cmp.right; + } + if (is_undefined(cmp.right, compressor)) { + found++; + undefined_cmp = cmp; + defined_side = cmp.left; + } + if (found !== 1) { + return false; + } + if (!defined_side.equivalent_to(check_subject)) { + return false; + } + return true; + }; + if (!find_comparison(check.left)) + return false; + if (!find_comparison(check.right)) + return false; + if (null_cmp && undefined_cmp && null_cmp !== undefined_cmp) { + return true; + } + } + return false; +} +def_optimize(AST_Conditional, function(self2, compressor) { + if (!compressor.option("conditionals")) + return self2; + if (self2.condition instanceof AST_Sequence) { + var expressions = self2.condition.expressions.slice(); + self2.condition = expressions.pop(); + expressions.push(self2); + return make_sequence(self2, expressions); + } + var cond = self2.condition.evaluate(compressor); + if (cond !== self2.condition) { + if (cond) { + return maintain_this_binding(compressor.parent(), compressor.self(), self2.consequent); + } else { + return maintain_this_binding(compressor.parent(), compressor.self(), self2.alternative); + } + } + var negated = cond.negate(compressor, first_in_statement(compressor)); + if (best_of(compressor, cond, negated) === negated) { + self2 = make_node(AST_Conditional, self2, { + condition: negated, + consequent: self2.alternative, + alternative: self2.consequent + }); + } + var condition = self2.condition; + var consequent = self2.consequent; + var alternative = self2.alternative; + if (condition instanceof AST_SymbolRef && consequent instanceof AST_SymbolRef && condition.definition() === consequent.definition()) { + return make_node(AST_Binary, self2, { + operator: "||", + left: condition, + right: alternative + }); + } + if (consequent instanceof AST_Assign && alternative instanceof AST_Assign && consequent.operator === alternative.operator && consequent.logical === alternative.logical && consequent.left.equivalent_to(alternative.left) && (!self2.condition.has_side_effects(compressor) || consequent.operator == "=" && !consequent.left.has_side_effects(compressor))) { + return make_node(AST_Assign, self2, { + operator: consequent.operator, + left: consequent.left, + logical: consequent.logical, + right: make_node(AST_Conditional, self2, { + condition: self2.condition, + consequent: consequent.right, + alternative: alternative.right + }) + }); + } + var arg_index; + if (consequent instanceof AST_Call && alternative.TYPE === consequent.TYPE && consequent.args.length > 0 && consequent.args.length == alternative.args.length && consequent.expression.equivalent_to(alternative.expression) && !self2.condition.has_side_effects(compressor) && !consequent.expression.has_side_effects(compressor) && typeof (arg_index = single_arg_diff()) == "number") { + var node = consequent.clone(); + node.args[arg_index] = make_node(AST_Conditional, self2, { + condition: self2.condition, + consequent: consequent.args[arg_index], + alternative: alternative.args[arg_index] + }); + return node; + } + if (alternative instanceof AST_Conditional && consequent.equivalent_to(alternative.consequent)) { + return make_node(AST_Conditional, self2, { + condition: make_node(AST_Binary, self2, { + operator: "||", + left: condition, + right: alternative.condition + }), + consequent, + alternative: alternative.alternative + }).optimize(compressor); + } + if (compressor.option("ecma") >= 2020 && is_nullish_check(condition, alternative, compressor)) { + return make_node(AST_Binary, self2, { + operator: "??", + left: alternative, + right: consequent + }).optimize(compressor); + } + if (alternative instanceof AST_Sequence && consequent.equivalent_to(alternative.expressions[alternative.expressions.length - 1])) { + return make_sequence(self2, [ + make_node(AST_Binary, self2, { + operator: "||", + left: condition, + right: make_sequence(self2, alternative.expressions.slice(0, -1)) + }), + consequent + ]).optimize(compressor); + } + if (alternative instanceof AST_Binary && alternative.operator == "&&" && consequent.equivalent_to(alternative.right)) { + return make_node(AST_Binary, self2, { + operator: "&&", + left: make_node(AST_Binary, self2, { + operator: "||", + left: condition, + right: alternative.left + }), + right: consequent + }).optimize(compressor); + } + if (consequent instanceof AST_Conditional && consequent.alternative.equivalent_to(alternative)) { + return make_node(AST_Conditional, self2, { + condition: make_node(AST_Binary, self2, { + left: self2.condition, + operator: "&&", + right: consequent.condition + }), + consequent: consequent.consequent, + alternative + }); + } + if (consequent.equivalent_to(alternative)) { + return make_sequence(self2, [ + self2.condition, + consequent + ]).optimize(compressor); + } + if (consequent instanceof AST_Binary && consequent.operator == "||" && consequent.right.equivalent_to(alternative)) { + return make_node(AST_Binary, self2, { + operator: "||", + left: make_node(AST_Binary, self2, { + operator: "&&", + left: self2.condition, + right: consequent.left + }), + right: alternative + }).optimize(compressor); + } + const in_bool = compressor.in_boolean_context(); + if (is_true(self2.consequent)) { + if (is_false(self2.alternative)) { + return booleanize(self2.condition); + } + return make_node(AST_Binary, self2, { + operator: "||", + left: booleanize(self2.condition), + right: self2.alternative + }); + } + if (is_false(self2.consequent)) { + if (is_true(self2.alternative)) { + return booleanize(self2.condition.negate(compressor)); + } + return make_node(AST_Binary, self2, { + operator: "&&", + left: booleanize(self2.condition.negate(compressor)), + right: self2.alternative + }); + } + if (is_true(self2.alternative)) { + return make_node(AST_Binary, self2, { + operator: "||", + left: booleanize(self2.condition.negate(compressor)), + right: self2.consequent + }); + } + if (is_false(self2.alternative)) { + return make_node(AST_Binary, self2, { + operator: "&&", + left: booleanize(self2.condition), + right: self2.consequent + }); + } + return self2; + function booleanize(node2) { + if (node2.is_boolean()) + return node2; + return make_node(AST_UnaryPrefix, node2, { + operator: "!", + expression: node2.negate(compressor) + }); + } + function is_true(node2) { + return node2 instanceof AST_True || in_bool && node2 instanceof AST_Constant && node2.getValue() || node2 instanceof AST_UnaryPrefix && node2.operator == "!" && node2.expression instanceof AST_Constant && !node2.expression.getValue(); + } + function is_false(node2) { + return node2 instanceof AST_False || in_bool && node2 instanceof AST_Constant && !node2.getValue() || node2 instanceof AST_UnaryPrefix && node2.operator == "!" && node2.expression instanceof AST_Constant && node2.expression.getValue(); + } + function single_arg_diff() { + var a = consequent.args; + var b = alternative.args; + for (var i = 0, len = a.length;i < len; i++) { + if (a[i] instanceof AST_Expansion) + return; + if (!a[i].equivalent_to(b[i])) { + if (b[i] instanceof AST_Expansion) + return; + for (var j = i + 1;j < len; j++) { + if (a[j] instanceof AST_Expansion) + return; + if (!a[j].equivalent_to(b[j])) + return; + } + return i; + } + } + } +}); +def_optimize(AST_Boolean, function(self2, compressor) { + if (compressor.in_boolean_context()) + return make_node(AST_Number, self2, { + value: +self2.value + }); + var p = compressor.parent(); + if (compressor.option("booleans_as_integers")) { + if (p instanceof AST_Binary && (p.operator == "===" || p.operator == "!==")) { + p.operator = p.operator.replace(/=$/, ""); + } + return make_node(AST_Number, self2, { + value: +self2.value + }); + } + if (compressor.option("booleans")) { + if (p instanceof AST_Binary && (p.operator == "==" || p.operator == "!=")) { + return make_node(AST_Number, self2, { + value: +self2.value + }); + } + return make_node(AST_UnaryPrefix, self2, { + operator: "!", + expression: make_node(AST_Number, self2, { + value: 1 - self2.value + }) + }); + } + return self2; +}); +function safe_to_flatten(value, compressor) { + if (value instanceof AST_SymbolRef) { + value = value.fixed_value(); + } + if (!value) + return false; + if (!(value instanceof AST_Lambda || value instanceof AST_Class)) + return true; + if (!(value instanceof AST_Lambda && value.contains_this())) + return true; + return compressor.parent() instanceof AST_New; +} +AST_PropAccess.DEFMETHOD("flatten_object", function(key, compressor) { + if (!compressor.option("properties")) + return; + if (key === "__proto__") + return; + if (this instanceof AST_DotHash) + return; + var arrows = compressor.option("unsafe_arrows") && compressor.option("ecma") >= 2015; + var expr = this.expression; + if (expr instanceof AST_Object) { + var props = expr.properties; + for (var i = props.length;--i >= 0; ) { + var prop = props[i]; + if ("" + (prop instanceof AST_ConciseMethod ? prop.key.name : prop.key) == key) { + const all_props_flattenable = props.every((p) => (p instanceof AST_ObjectKeyVal || arrows && p instanceof AST_ConciseMethod && !p.value.is_generator) && !p.computed_key()); + if (!all_props_flattenable) + return; + if (!safe_to_flatten(prop.value, compressor)) + return; + return make_node(AST_Sub, this, { + expression: make_node(AST_Array, expr, { + elements: props.map(function(prop2) { + var v = prop2.value; + if (v instanceof AST_Accessor) { + v = make_node(AST_Function, v, v); + } + var k = prop2.key; + if (k instanceof AST_Node && !(k instanceof AST_SymbolMethod)) { + return make_sequence(prop2, [k, v]); + } + return v; + }) + }), + property: make_node(AST_Number, this, { + value: i + }) + }); + } + } + } +}); +def_optimize(AST_Sub, function(self2, compressor) { + var expr = self2.expression; + var prop = self2.property; + if (compressor.option("properties")) { + var key = prop.evaluate(compressor); + if (key !== prop) { + if (typeof key == "string") { + if (key == "undefined") { + key = undefined; + } else { + var value = parseFloat(key); + if (value.toString() == key) { + key = value; + } + } + } + prop = self2.property = best_of_expression(prop, make_node_from_constant(key, prop).transform(compressor)); + var property = "" + key; + if (is_basic_identifier_string(property) && property.length <= prop.size() + 1) { + return make_node(AST_Dot, self2, { + expression: expr, + optional: self2.optional, + property, + quote: prop.quote + }).optimize(compressor); + } + } + } + var fn; + OPT_ARGUMENTS: + if (compressor.option("arguments") && expr instanceof AST_SymbolRef && expr.name == "arguments" && expr.definition().orig.length == 1 && (fn = expr.scope) instanceof AST_Lambda && fn.uses_arguments && !(fn instanceof AST_Arrow) && prop instanceof AST_Number) { + var index = prop.getValue(); + var params = new Set; + var argnames = fn.argnames; + for (var n = 0;n < argnames.length; n++) { + if (!(argnames[n] instanceof AST_SymbolFunarg)) { + break OPT_ARGUMENTS; + } + var param = argnames[n].name; + if (params.has(param)) { + break OPT_ARGUMENTS; + } + params.add(param); + } + var argname = fn.argnames[index]; + if (argname && compressor.has_directive("use strict")) { + var def = argname.definition(); + if (!compressor.option("reduce_vars") || def.assignments || def.orig.length > 1) { + argname = null; + } + } else if (!argname && !compressor.option("keep_fargs") && index < fn.argnames.length + 5) { + while (index >= fn.argnames.length) { + argname = fn.create_symbol(AST_SymbolFunarg, { + source: fn, + scope: fn, + tentative_name: "argument_" + fn.argnames.length + }); + fn.argnames.push(argname); + } + } + if (argname) { + var sym = make_node(AST_SymbolRef, self2, argname); + sym.reference({}); + clear_flag(argname, UNUSED); + return sym; + } + } + if (compressor.is_lhs()) + return self2; + if (key !== prop) { + var sub = self2.flatten_object(property, compressor); + if (sub) { + expr = self2.expression = sub.expression; + prop = self2.property = sub.property; + } + } + if (compressor.option("properties") && compressor.option("side_effects") && prop instanceof AST_Number && expr instanceof AST_Array) { + var index = prop.getValue(); + var elements = expr.elements; + var retValue = elements[index]; + FLATTEN: + if (safe_to_flatten(retValue, compressor)) { + var flatten = true; + var values = []; + for (var i = elements.length;--i > index; ) { + var value = elements[i].drop_side_effect_free(compressor); + if (value) { + values.unshift(value); + if (flatten && value.has_side_effects(compressor)) + flatten = false; + } + } + if (retValue instanceof AST_Expansion) + break FLATTEN; + retValue = retValue instanceof AST_Hole ? make_node(AST_Undefined, retValue) : retValue; + if (!flatten) + values.unshift(retValue); + while (--i >= 0) { + var value = elements[i]; + if (value instanceof AST_Expansion) + break FLATTEN; + value = value.drop_side_effect_free(compressor); + if (value) + values.unshift(value); + else + index--; + } + if (flatten) { + values.push(retValue); + return make_sequence(self2, values).optimize(compressor); + } else + return make_node(AST_Sub, self2, { + expression: make_node(AST_Array, expr, { + elements: values + }), + property: make_node(AST_Number, prop, { + value: index + }) + }); + } + } + var ev = self2.evaluate(compressor); + if (ev !== self2) { + ev = make_node_from_constant(ev, self2).optimize(compressor); + return best_of(compressor, ev, self2); + } + return self2; +}); +def_optimize(AST_Chain, function(self2, compressor) { + if (is_nullish(self2.expression, compressor)) { + let parent = compressor.parent(); + if (parent instanceof AST_UnaryPrefix && parent.operator === "delete") { + return make_node_from_constant(0, self2); + } + return make_node(AST_Undefined, self2); + } + if (self2.expression instanceof AST_PropAccess || self2.expression instanceof AST_Call) { + return self2; + } else { + return self2.expression; + } +}); +def_optimize(AST_Dot, function(self2, compressor) { + const parent = compressor.parent(); + if (compressor.is_lhs()) + return self2; + if (compressor.option("unsafe_proto") && self2.expression instanceof AST_Dot && self2.expression.property == "prototype") { + var exp = self2.expression.expression; + if (is_undeclared_ref(exp)) + switch (exp.name) { + case "Array": + self2.expression = make_node(AST_Array, self2.expression, { + elements: [] + }); + break; + case "Function": + self2.expression = make_empty_function(self2.expression); + break; + case "Number": + self2.expression = make_node(AST_Number, self2.expression, { + value: 0 + }); + break; + case "Object": + self2.expression = make_node(AST_Object, self2.expression, { + properties: [] + }); + break; + case "RegExp": + self2.expression = make_node(AST_RegExp, self2.expression, { + value: { source: "t", flags: "" } + }); + break; + case "String": + self2.expression = make_node(AST_String, self2.expression, { + value: "" + }); + break; + } + } + if (!(parent instanceof AST_Call) || !has_annotation(parent, _NOINLINE)) { + const sub = self2.flatten_object(self2.property, compressor); + if (sub) + return sub.optimize(compressor); + } + if (self2.expression instanceof AST_PropAccess && parent instanceof AST_PropAccess) { + return self2; + } + let ev = self2.evaluate(compressor); + if (ev !== self2) { + ev = make_node_from_constant(ev, self2).optimize(compressor); + return best_of(compressor, ev, self2); + } + return self2; +}); +function literals_in_boolean_context(self2, compressor) { + if (compressor.in_boolean_context()) { + return best_of(compressor, self2, make_sequence(self2, [ + self2, + make_node(AST_True, self2) + ]).optimize(compressor)); + } + return self2; +} +function inline_array_like_spread(elements) { + for (var i = 0;i < elements.length; i++) { + var el = elements[i]; + if (el instanceof AST_Expansion) { + var expr = el.expression; + if (expr instanceof AST_Array && !expr.elements.some((elm) => elm instanceof AST_Hole)) { + elements.splice(i, 1, ...expr.elements); + i--; + } + } + } +} +def_optimize(AST_Array, function(self2, compressor) { + var optimized = literals_in_boolean_context(self2, compressor); + if (optimized !== self2) { + return optimized; + } + inline_array_like_spread(self2.elements); + return self2; +}); +function inline_object_prop_spread(props) { + for (var i = 0;i < props.length; i++) { + var prop = props[i]; + if (prop instanceof AST_Expansion) { + const expr = prop.expression; + if (expr instanceof AST_Object && expr.properties.every((prop2) => prop2 instanceof AST_ObjectKeyVal)) { + props.splice(i, 1, ...expr.properties); + i--; + } else if ((expr instanceof AST_Constant || expr.is_constant()) && !(expr instanceof AST_String)) { + props.splice(i, 1); + i--; + } + } + } +} +def_optimize(AST_Object, function(self2, compressor) { + var optimized = literals_in_boolean_context(self2, compressor); + if (optimized !== self2) { + return optimized; + } + inline_object_prop_spread(self2.properties); + return self2; +}); +def_optimize(AST_RegExp, literals_in_boolean_context); +def_optimize(AST_Return, function(self2, compressor) { + if (self2.value && is_undefined(self2.value, compressor)) { + self2.value = null; + } + return self2; +}); +def_optimize(AST_Arrow, opt_AST_Lambda); +def_optimize(AST_Function, function(self2, compressor) { + self2 = opt_AST_Lambda(self2, compressor); + if (compressor.option("unsafe_arrows") && compressor.option("ecma") >= 2015 && !self2.name && !self2.is_generator && !self2.uses_arguments && !self2.pinned()) { + const uses_this = walk(self2, (node) => { + if (node instanceof AST_This) + return walk_abort; + }); + if (!uses_this) + return make_node(AST_Arrow, self2, self2).optimize(compressor); + } + return self2; +}); +def_optimize(AST_Class, function(self2) { + for (let i = 0;i < self2.properties.length; i++) { + const prop = self2.properties[i]; + if (prop instanceof AST_ClassStaticBlock && prop.body.length == 0) { + self2.properties.splice(i, 1); + i--; + } + } + return self2; +}); +def_optimize(AST_ClassStaticBlock, function(self2, compressor) { + tighten_body(self2.body, compressor); + return self2; +}); +def_optimize(AST_Yield, function(self2, compressor) { + if (self2.expression && !self2.is_star && is_undefined(self2.expression, compressor)) { + self2.expression = null; + } + return self2; +}); +def_optimize(AST_TemplateString, function(self2, compressor) { + if (!compressor.option("evaluate") || compressor.parent() instanceof AST_PrefixedTemplateString) { + return self2; + } + var segments = []; + for (var i = 0;i < self2.segments.length; i++) { + var segment = self2.segments[i]; + if (segment instanceof AST_Node) { + var result = segment.evaluate(compressor); + if (result !== segment && (result + "").length <= segment.size() + "${}".length) { + segments[segments.length - 1].value = segments[segments.length - 1].value + result + self2.segments[++i].value; + continue; + } + if (segment instanceof AST_TemplateString) { + var inners = segment.segments; + segments[segments.length - 1].value += inners[0].value; + for (var j = 1;j < inners.length; j++) { + segment = inners[j]; + segments.push(segment); + } + continue; + } + } + segments.push(segment); + } + self2.segments = segments; + if (segments.length == 1) { + return make_node(AST_String, self2, segments[0]); + } + if (segments.length === 3 && segments[1] instanceof AST_Node && (segments[1].is_string(compressor) || segments[1].is_number_or_bigint(compressor) || is_nullish(segments[1], compressor) || compressor.option("unsafe"))) { + if (segments[2].value === "") { + return make_node(AST_Binary, self2, { + operator: "+", + left: make_node(AST_String, self2, { + value: segments[0].value + }), + right: segments[1] + }); + } + if (segments[0].value === "") { + return make_node(AST_Binary, self2, { + operator: "+", + left: segments[1], + right: make_node(AST_String, self2, { + value: segments[2].value + }) + }); + } + } + return self2; +}); +def_optimize(AST_PrefixedTemplateString, function(self2) { + return self2; +}); +function lift_key(self2, compressor) { + if (!compressor.option("computed_props")) + return self2; + if (!(self2.key instanceof AST_Constant)) + return self2; + if (self2.key instanceof AST_String || self2.key instanceof AST_Number) { + const key = self2.key.value.toString(); + if (key === "__proto__") + return self2; + if (key == "constructor" && compressor.parent() instanceof AST_Class) + return self2; + if (self2 instanceof AST_ObjectKeyVal) { + self2.quote = self2.key.quote; + self2.key = key; + } else if (self2 instanceof AST_ClassProperty) { + self2.quote = self2.key.quote; + self2.key = make_node(AST_SymbolClassProperty, self2.key, { + name: key + }); + } else { + self2.quote = self2.key.quote; + self2.key = make_node(AST_SymbolMethod, self2.key, { + name: key + }); + } + } + return self2; +} +def_optimize(AST_ObjectProperty, lift_key); +def_optimize(AST_ConciseMethod, function(self2, compressor) { + lift_key(self2, compressor); + if (compressor.option("arrows") && compressor.parent() instanceof AST_Object && !self2.value.is_generator && !self2.value.uses_arguments && !self2.value.pinned() && self2.value.body.length == 1 && self2.value.body[0] instanceof AST_Return && self2.value.body[0].value && !self2.value.contains_this()) { + var arrow = make_node(AST_Arrow, self2.value, self2.value); + arrow.async = self2.value.async; + arrow.is_generator = self2.value.is_generator; + return make_node(AST_ObjectKeyVal, self2, { + key: self2.key instanceof AST_SymbolMethod ? self2.key.name : self2.key, + value: arrow, + quote: self2.quote + }); + } + return self2; +}); +def_optimize(AST_ObjectKeyVal, function(self2, compressor) { + lift_key(self2, compressor); + var unsafe_methods = compressor.option("unsafe_methods"); + if (unsafe_methods && compressor.option("ecma") >= 2015 && (!(unsafe_methods instanceof RegExp) || unsafe_methods.test(self2.key + ""))) { + var key = self2.key; + var value = self2.value; + var is_arrow_with_block = value instanceof AST_Arrow && Array.isArray(value.body) && !value.contains_this(); + if ((is_arrow_with_block || value instanceof AST_Function) && !value.name) { + return make_node(AST_ConciseMethod, self2, { + key: key instanceof AST_Node ? key : make_node(AST_SymbolMethod, self2, { + name: key + }), + value: make_node(AST_Accessor, value, value), + quote: self2.quote + }); + } + } + return self2; +}); +def_optimize(AST_Destructuring, function(self2, compressor) { + if (compressor.option("pure_getters") == true && compressor.option("unused") && !self2.is_array && Array.isArray(self2.names) && !is_destructuring_export_decl(compressor) && !(self2.names[self2.names.length - 1] instanceof AST_Expansion)) { + var keep = []; + for (var i = 0;i < self2.names.length; i++) { + var elem = self2.names[i]; + if (!(elem instanceof AST_ObjectKeyVal && typeof elem.key == "string" && elem.value instanceof AST_SymbolDeclaration && !should_retain(compressor, elem.value.definition()))) { + keep.push(elem); + } + } + if (keep.length != self2.names.length) { + self2.names = keep; + } + } + return self2; + function is_destructuring_export_decl(compressor2) { + var ancestors = [/^VarDef$/, /^(Const|Let|Var)$/, /^Export$/]; + for (var a = 0, p = 0, len = ancestors.length;a < len; p++) { + var parent = compressor2.parent(p); + if (!parent) + return false; + if (a === 0 && parent.TYPE == "Destructuring") + continue; + if (!ancestors[a].test(parent.TYPE)) { + return false; + } + a++; + } + return true; + } + function should_retain(compressor2, def) { + if (def.references.length) + return true; + if (!def.global) + return false; + if (compressor2.toplevel.vars) { + if (compressor2.top_retain) { + return compressor2.top_retain(def); + } + return false; + } + return true; + } +}); + +// node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.mjs +var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +var intToChar = new Uint8Array(64); +var charToInt = new Uint8Array(128); +for (let i = 0;i < chars.length; i++) { + const c = chars.charCodeAt(i); + intToChar[i] = c; + charToInt[c] = i; +} +var bufLength = 1024 * 16; + +// node_modules/@jridgewell/trace-mapping/dist/trace-mapping.mjs +var import_resolve_uri = __toESM(require_resolve_uri_umd(), 1); +// src/TestColliderMerge.ts +var game = new Game({ + name: "My game", + canvas: "can", + width: window.innerWidth, + height: window.innerHeight, + showFrameStats: "PERFORMANCE_HUD" +}); +var s1 = new Scene({ + name: "S1" +}); +var l1 = new Layer({ + name: "L1" +}); +s1.addChild(l1); +game.addChild(s1); +for (let x = 0;x < 50; x++) { + for (let y = 0;y < 50; y++) { + if ((x == 0 || y === 0) && Math.random() > 0.2) + continue; + const testObject = new GameObject({ + name: `Test Object (${x + 1}, ${y + 1})` + }); + const wh = Math.floor(Math.random() * 500) + 10; + const testTransform = new Transform({ + position: new Vector(30 + x * wh, 500 + y * wh), + rotation: 0, + scale: Vector.From(1) + }); + const n = Math.floor(Math.random() * 6) + 3; + const angleStep = Math.PI * 2 / n; + const radius = wh / 2; + const center = new Vector(0, 0); + const vertices = []; + for (let i = 0;i < n; i++) { + const angle = i * angleStep + Math.random() * (angleStep * 0.15); + const r = radius * (0.7 + Math.random() * 0.6); + vertices.push(new Vector(Math.cos(angle) * r, Math.sin(angle) * r)); + } + const polyCollider = new PolygonCollider({ + vertices + }); + const colorRender = new ColorRender({ + color: `red`, + width: wh, + height: wh + }); + testObject.addChildren(testTransform, polyCollider); + l1.addChild(testObject); + } +} +for (let x = 0;x < 10; x++) { + const testObject = new GameObject({ + name: `Test D- Object (${x + 1})` + }); + const testTransform = new Transform({ + position: new Vector(300 + x * 25, 100 + x * 25), + rotation: 0, + scale: Vector.From(1) + }); + const colorRender = new ColorRender({ + color: `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${Math.random() * 255})`, + width: 50, + height: 50 + }); + const boxCollider = new BoxCollider({ + width: 50, + height: 50, + tag: "testObjects" + }); + testObject.addChildren(testTransform, boxCollider, colorRender); + l1.addChild(testObject); +} +var input = new Input({ + key: () => {}, + keyup: () => {}, + mousemove: () => {}, + click: () => {} +}); +s1.addChild(input); + +class CollisionColorChanger extends Part { + collider; + renderer; + originalColor = "blue"; + onMount(part) { + super.onMount(part); + this.collider = this.siblingOf("Collider"); + console.log(this.collider); + this.renderer = this.sibling("ColorRender"); + console.log(this.renderer); + if (this.renderer) { + this.originalColor = this.renderer.color; + } + } + act(delta) { + super.act(delta); + if (this.collider && this.renderer) { + if (this.collider.colliding) { + this.renderer.color = "red"; + } else { + this.renderer.color = this.originalColor; + } + } + } +} +var player = new GameObject({ + name: "Player" +}); +var playerTransform = new Transform({ + position: new Vector(100, 100) +}); +var playerCollider = new BoxCollider({ + width: 50, + height: 50 +}); +var playerRenderer = new ColorRender({ + width: 50, + height: 50, + color: "blue" +}); +var playerMovement = new CharacterMovement({ + speed: 0.5, + input +}); +var camera = new Camera({ name: "Cam" }); +var follow = new Follow({ + target: playerTransform, + interpolationSpeed: 0.01 +}); +camera.addChild(follow); +var cameraTransform = new Transform({ position: new Vector(0, 0) }); +camera.addChild(cameraTransform); +s1.addChild(camera); +var colorChanger = new CollisionColorChanger; +player.addChildren(playerTransform, playerCollider, playerRenderer, playerMovement, colorChanger); +l1.addChild(player); +console.log(game); +game.start(s1); diff --git a/testDist/ishape_wasm_bg.wasm b/testDist/ishape_wasm_bg.wasm new file mode 100644 index 0000000..ec5262a Binary files /dev/null and b/testDist/ishape_wasm_bg.wasm differ diff --git a/vibeCoding/after.png b/vibeCoding/after.png new file mode 100644 index 0000000..3ad8eca Binary files /dev/null and b/vibeCoding/after.png differ diff --git a/vibeCoding/before.png b/vibeCoding/before.png new file mode 100644 index 0000000..d1e288a Binary files /dev/null and b/vibeCoding/before.png differ