d25d482dc2
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
98 lines
2.9 KiB
TypeScript
98 lines
2.9 KiB
TypeScript
export const LARGE_VALUE_REF_MARKER = '__simLargeValueRef'
|
|
|
|
export const LARGE_VALUE_THRESHOLD_BYTES = 8 * 1024 * 1024
|
|
export const LARGE_VALUE_REF_VERSION = 1
|
|
|
|
export const LARGE_VALUE_KINDS = ['array', 'object', 'string', 'json'] as const
|
|
|
|
export type LargeValueKind = (typeof LARGE_VALUE_KINDS)[number]
|
|
|
|
export interface LargeValueRef {
|
|
[LARGE_VALUE_REF_MARKER]: true
|
|
version: typeof LARGE_VALUE_REF_VERSION
|
|
id: string
|
|
kind: LargeValueKind
|
|
size: number
|
|
key?: string
|
|
executionId?: string
|
|
preview?: unknown
|
|
}
|
|
|
|
const LARGE_VALUE_ID_PATTERN = /^lv_[A-Za-z0-9_-]{12}$/
|
|
|
|
export function isLargeValueStorageKey(key: string, id: string, executionId?: string): boolean {
|
|
if (!key.startsWith('execution/')) return false
|
|
if (!key.endsWith(`/large-value-${id}.json`)) return false
|
|
if (executionId && !key.includes(`/${executionId}/`)) return false
|
|
return true
|
|
}
|
|
|
|
export function isLargeValueRef(value: unknown): value is LargeValueRef {
|
|
if (!value || typeof value !== 'object') return false
|
|
|
|
const candidate = value as Record<string, unknown>
|
|
const id = candidate.id
|
|
const key = candidate.key
|
|
const executionId = candidate.executionId
|
|
|
|
return (
|
|
candidate[LARGE_VALUE_REF_MARKER] === true &&
|
|
candidate.version === LARGE_VALUE_REF_VERSION &&
|
|
typeof id === 'string' &&
|
|
LARGE_VALUE_ID_PATTERN.test(id) &&
|
|
typeof candidate.kind === 'string' &&
|
|
(LARGE_VALUE_KINDS as readonly string[]).includes(candidate.kind) &&
|
|
typeof candidate.size === 'number' &&
|
|
Number.isFinite(candidate.size) &&
|
|
candidate.size > 0 &&
|
|
(executionId === undefined || typeof executionId === 'string') &&
|
|
(key === undefined ||
|
|
(typeof key === 'string' &&
|
|
isLargeValueStorageKey(key, id, executionId as string | undefined)))
|
|
)
|
|
}
|
|
|
|
export function containsLargeValueRef(
|
|
value: unknown,
|
|
seen = new WeakSet<object>()
|
|
): LargeValueRef | null {
|
|
if (!value || typeof value !== 'object') return null
|
|
if (isLargeValueRef(value)) return value
|
|
if (seen.has(value)) return null
|
|
|
|
seen.add(value)
|
|
|
|
if (Array.isArray(value)) {
|
|
for (const item of value) {
|
|
const ref = containsLargeValueRef(item, seen)
|
|
if (ref) return ref
|
|
}
|
|
return null
|
|
}
|
|
|
|
for (const entryValue of Object.values(value)) {
|
|
const ref = containsLargeValueRef(entryValue, seen)
|
|
if (ref) return ref
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
export function getLargeValueMaterializationError(ref: LargeValueRef): Error {
|
|
return new Error(
|
|
`This execution value is too large to inline (${formatLargeValueSize(ref.size)}). Select a nested field or reduce the amount of data passed between blocks.`
|
|
)
|
|
}
|
|
|
|
export function formatLargeValueSize(bytes: number): string {
|
|
const megabytes = bytes / (1024 * 1024)
|
|
return `${megabytes.toFixed(1)} MB`
|
|
}
|
|
|
|
export function assertNoLargeValueRefs(value: unknown): void {
|
|
const ref = containsLargeValueRef(value)
|
|
if (ref) {
|
|
throw getLargeValueMaterializationError(ref)
|
|
}
|
|
}
|