The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import type * as bigquery from "@distilled.cloud/gcp/bigquery_v2"; |
| 2 | |
| 3 | /** |
| 4 | * Codec between BigQuery REST row payloads and plain JavaScript rows. |
| 5 | * NOT exported from `index.ts`. |
| 6 | * |
| 7 | * Decoding (driven by the table / result schema, with |
| 8 | * `formatOptions.useInt64Timestamp` requested): `INTEGER` → `number` |
| 9 | * (`bigint` beyond the safe range), `FLOAT` → `number`, `BOOLEAN` → |
| 10 | * `boolean`, `TIMESTAMP` → `Date`, `BYTES` → `Uint8Array`, `JSON` → parsed |
| 11 | * value, `RECORD` → object, `REPEATED` → array, `NULL` → `null`. `NUMERIC`, |
| 12 | * `BIGNUMERIC`, `DATE`, `TIME`, `DATETIME`, `GEOGRAPHY`, and everything else |
| 13 | * stay strings. |
| 14 | */ |
| 15 | |
| 16 | const toBase64 = (bytes: Uint8Array): string => { |
| 17 | let binary = ""; |
| 18 | for (let i = 0; i < bytes.length; i++) { |
| 19 | binary += String.fromCharCode(bytes[i]!); |
| 20 | } |
| 21 | return btoa(binary); |
| 22 | }; |
| 23 | |
| 24 | const fromBase64 = (value: string): Uint8Array => { |
| 25 | const binary = atob(value); |
| 26 | const bytes = new Uint8Array(binary.length); |
| 27 | for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i); |
| 28 | return bytes; |
| 29 | }; |
| 30 | |
| 31 | const toInteger = (value: string): number | bigint => { |
| 32 | const big = BigInt(value); |
| 33 | return big >= BigInt(Number.MIN_SAFE_INTEGER) && big <= BigInt(Number.MAX_SAFE_INTEGER) |
| 34 | ? Number(big) |
| 35 | : big; |
| 36 | }; |
| 37 | |
| 38 | const isCell = (value: unknown): value is { v?: unknown } => |
| 39 | typeof value === "object" && value !== null && "v" in value; |
| 40 | |
| 41 | const decodeScalar = (field: bigquery.TableFieldSchema, value: unknown): unknown => { |
| 42 | if (value === null || value === undefined) return null; |
| 43 | switch ((field.type ?? "STRING").toUpperCase()) { |
| 44 | case "RECORD": |
| 45 | case "STRUCT": |
| 46 | return decodeRow(field.fields ?? [], (value as { f?: bigquery.TableCell[] }).f); |
| 47 | } |
| 48 | if (typeof value !== "string") return value; |
| 49 | switch ((field.type ?? "STRING").toUpperCase()) { |
| 50 | case "INTEGER": |
| 51 | case "INT64": |
| 52 | return toInteger(value); |
| 53 | case "FLOAT": |
| 54 | case "FLOAT64": |
| 55 | return Number(value); |
| 56 | case "BOOLEAN": |
| 57 | case "BOOL": |
| 58 | return value === "true"; |
| 59 | case "TIMESTAMP": |
| 60 | // Int64 microseconds since the epoch (useInt64Timestamp); fall back |
| 61 | // to the float-seconds wire format. |
| 62 | return /^-?\d+$/.test(value) |
| 63 | ? new Date(Number(BigInt(value) / BigInt(1000))) |
| 64 | : new Date(Number(value) * 1000); |
| 65 | case "BYTES": |
| 66 | return fromBase64(value); |
| 67 | case "JSON": |
| 68 | return JSON.parse(value); |
| 69 | default: |
| 70 | return value; |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | const decodeCell = (field: bigquery.TableFieldSchema, value: unknown) => |
| 75 | (field.mode ?? "").toUpperCase() === "REPEATED" |
| 76 | ? (Array.isArray(value) ? value : []).map((item) => |
| 77 | decodeScalar(field, isCell(item) ? item.v : item), |
| 78 | ) |
| 79 | : decodeScalar(field, value); |
| 80 | |
| 81 | export const decodeRow = ( |
| 82 | fields: ReadonlyArray<bigquery.TableFieldSchema>, |
| 83 | cells: ReadonlyArray<bigquery.TableCell> | undefined, |
| 84 | ): Record<string, unknown> => { |
| 85 | const row: Record<string, unknown> = {}; |
| 86 | fields.forEach((field, index) => { |
| 87 | row[field.name ?? `f${index}`] = decodeCell(field, cells?.[index]?.v); |
| 88 | }); |
| 89 | return row; |
| 90 | }; |
| 91 | |
| 92 | export const decodeRows = ( |
| 93 | schema: bigquery.TableSchema | undefined, |
| 94 | rows: ReadonlyArray<bigquery.TableRow> | undefined, |
| 95 | ): Record<string, unknown>[] => (rows ?? []).map((row) => decodeRow(schema?.fields ?? [], row.f)); |
| 96 | |
| 97 | /** Encode a plain JS value for `tabledata.insertAll` JSON rows. */ |
| 98 | export const encodeJson = (value: unknown): unknown => { |
| 99 | if (value === undefined || value === null) return null; |
| 100 | if (typeof value === "bigint") return value.toString(); |
| 101 | if (value instanceof Date) return value.toISOString(); |
| 102 | if (value instanceof Uint8Array) return toBase64(value); |
| 103 | if (Array.isArray(value)) return value.map(encodeJson); |
| 104 | if (typeof value === "object") { |
| 105 | const out: Record<string, unknown> = {}; |
| 106 | for (const [key, item] of Object.entries(value)) { |
| 107 | if (item !== undefined) out[key] = encodeJson(item); |
| 108 | } |
| 109 | return out; |
| 110 | } |
| 111 | return value; |
| 112 | }; |
| 113 | |
| 114 | /** Encode one row for `tabledata.insertAll`; `undefined` keys are dropped. */ |
| 115 | export const encodeRow = (row: Record<string, unknown>): Record<string, unknown> => { |
| 116 | const out: Record<string, unknown> = {}; |
| 117 | for (const [key, value] of Object.entries(row)) { |
| 118 | if (value !== undefined) out[key] = encodeJson(value); |
| 119 | } |
| 120 | return out; |
| 121 | }; |
| 122 | |
| 123 | /** Named query parameter for a scalar JS value. */ |
| 124 | export const queryParameter = ( |
| 125 | name: string, |
| 126 | value: string | number | boolean, |
| 127 | ): bigquery.QueryParameter => ({ |
| 128 | name, |
| 129 | parameterType: { |
| 130 | type: |
| 131 | typeof value === "boolean" |
| 132 | ? "BOOL" |
| 133 | : typeof value === "number" |
| 134 | ? Number.isSafeInteger(value) |
| 135 | ? "INT64" |
| 136 | : "FLOAT64" |
| 137 | : "STRING", |
| 138 | }, |
| 139 | parameterValue: { value: String(value) }, |
| 140 | }); |