| 32 | const allChildren: unknown[] = []; |
| 33 | |
| 34 | function _clone(parent: any) { |
| 35 | // cloning null always returns null |
| 36 | if (parent === null) { |
| 37 | return null; |
| 38 | } |
| 39 | |
| 40 | if (typeof parent !== 'object') { |
| 41 | return parent; |
| 42 | } |
| 43 | |
| 44 | if (respectCustomCloneMethod && 'clone' in parent && typeof parent.clone === 'function') { |
| 45 | // an async `clone()` signals a live stateful resource (e.g. a PGlite instance |
| 46 | // in `driverOptions`, whose `clone()` boots a second WASM database) — this sync |
| 47 | // function cannot await it, so keep the instance by reference instead |
| 48 | if (parent.clone.constructor.name === 'AsyncFunction') { |
| 49 | return parent; |
| 50 | } |
| 51 | |
| 52 | return parent.clone(); |
| 53 | } |
| 54 | |
| 55 | let child: unknown; |
| 56 | let proto; |
| 57 | |
| 58 | if (parent instanceof Map) { |
| 59 | child = new Map(); |
| 60 | } else if (parent instanceof Set) { |
| 61 | child = new Set(); |
| 62 | } else if (parent instanceof Promise) { |
| 63 | child = new Promise((resolve, reject) => { |
| 64 | parent.then(resolve.bind(null, _clone), reject.bind(null, _clone)); |
| 65 | }); |
| 66 | } else if (Array.isArray(parent)) { |
| 67 | child = []; |
| 68 | } else if (parent instanceof RegExp) { |
| 69 | let flags = ''; |
| 70 | |
| 71 | if (parent.global) { |
| 72 | flags += 'g'; |
| 73 | } |
| 74 | |
| 75 | if (parent.ignoreCase) { |
| 76 | flags += 'i'; |
| 77 | } |
| 78 | |
| 79 | if (parent.multiline) { |
| 80 | flags += 'm'; |
| 81 | } |
| 82 | |
| 83 | child = new RegExp(parent.source, flags); |
| 84 | |
| 85 | if (parent.lastIndex) { |
| 86 | (child as RegExp).lastIndex = parent.lastIndex; |
| 87 | } |
| 88 | } else if (parent instanceof Date) { |
| 89 | child = new Date(parent.getTime()); |
| 90 | } else if (Buffer.isBuffer(parent)) { |
| 91 | child = Buffer.allocUnsafe(parent.length); |