95 lines
4.7 KiB
JavaScript
95 lines
4.7 KiB
JavaScript
;(function (global) {
|
|
'use strict'
|
|
|
|
const CURRENT = { workspace: 1, canvas: 1, backup: 2, groups: 1 }
|
|
|
|
function clone(value) {
|
|
if (value === undefined) return value
|
|
return JSON.parse(JSON.stringify(value))
|
|
}
|
|
|
|
// 防原型污染:导入文件(备份 / 画布 / 节点组)是唯一「外来结构化数据」入口。
|
|
// JSON.parse 会把 "__proto__" 解析成**自有键**,随后 Object.assign/深合并再触发 setter 污染原型。
|
|
// 迁移只作用于外来文件(workspace/groups 为本地自写、且是热路径,不在此处清洗)。
|
|
const isDangerousKey = (k) => k === '__proto__' || k === 'constructor' || k === 'prototype'
|
|
function stripDangerous(value) {
|
|
if (Array.isArray(value)) return value.map(stripDangerous)
|
|
if (value && typeof value === 'object') {
|
|
const out = {}
|
|
for (const k of Object.keys(value)) {
|
|
if (isDangerousKey(k)) continue
|
|
out[k] = stripDangerous(value[k])
|
|
}
|
|
return out
|
|
}
|
|
return value
|
|
}
|
|
|
|
function versionOf(value, key) {
|
|
const n = value && Number(value.schemaVersion)
|
|
return Number.isInteger(n) && n >= 0 ? n : 0
|
|
}
|
|
|
|
function migrateWorkspace(raw) {
|
|
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return { ok: false, code: 'invalid-schema', error: '工作台数据格式无效' }
|
|
const from = versionOf(raw, 'workspace')
|
|
if (from > CURRENT.workspace) return { ok: false, code: 'unsupported-schema', error: '工作台数据版本过新,请升级应用后再打开' }
|
|
const value = clone(raw)
|
|
if (!Array.isArray(value.workspaces)) value.workspaces = []
|
|
value.workspaces = value.workspaces.filter((w) => w && typeof w === 'object').map((w) => Object.assign({}, w, {
|
|
id: String(w.id || 'ws_default'),
|
|
name: String(w.name || '未命名工作台'),
|
|
nodes: Array.isArray(w.nodes) ? w.nodes : [],
|
|
edges: Array.isArray(w.edges) ? w.edges : [],
|
|
groups: Array.isArray(w.groups) ? w.groups : [],
|
|
}))
|
|
if (!value.workspaces.length) value.workspaces = [{ id: 'ws_default', name: '工作台 1', nodes: [], edges: [], groups: [] }]
|
|
if (!value.activeId || !value.workspaces.some((w) => w.id === value.activeId)) value.activeId = value.workspaces[0].id
|
|
if (!Number.isFinite(Number(value.rev)) || Number(value.rev) < 1) value.rev = 1
|
|
value.schemaVersion = CURRENT.workspace
|
|
return { ok: true, changed: from !== CURRENT.workspace || JSON.stringify(raw) !== JSON.stringify(value), from, value }
|
|
}
|
|
|
|
function migrateCanvas(raw) {
|
|
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return { ok: false, code: 'invalid-schema', error: '画布数据格式无效' }
|
|
const from = versionOf(raw, 'canvas')
|
|
if (from > CURRENT.canvas) return { ok: false, code: 'unsupported-schema', error: '画布数据版本过新,请升级应用后再导入' }
|
|
const value = stripDangerous(clone(raw))
|
|
value.nodes = Array.isArray(value.nodes) ? value.nodes : []
|
|
value.edges = Array.isArray(value.edges) ? value.edges : []
|
|
value.groups = Array.isArray(value.groups) ? value.groups : []
|
|
value.schemaVersion = CURRENT.canvas
|
|
return { ok: true, changed: from !== CURRENT.canvas, from, value }
|
|
}
|
|
|
|
function migrateGroups(raw) {
|
|
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return { ok: false, code: 'invalid-schema', error: '节点组数据格式无效' }
|
|
const from = versionOf(raw, 'groups')
|
|
if (from > CURRENT.groups) return { ok: false, code: 'unsupported-schema', error: '节点组数据版本过新,请升级应用后再打开' }
|
|
const value = clone(raw)
|
|
value.groups = Array.isArray(value.groups) ? value.groups : []
|
|
value.groupGroups = Array.isArray(value.groupGroups) ? value.groupGroups : []
|
|
value.schemaVersion = CURRENT.groups
|
|
return { ok: true, changed: from !== CURRENT.groups || JSON.stringify(raw) !== JSON.stringify(value), from, value }
|
|
}
|
|
|
|
function migrateBackup(raw) {
|
|
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return { ok: false, code: 'invalid-schema', error: '备份数据格式无效' }
|
|
const from = versionOf(raw, 'backup')
|
|
if (from > CURRENT.backup) return { ok: false, code: 'unsupported-schema', error: '备份版本过新,请升级应用后再恢复' }
|
|
const value = stripDangerous(clone(raw))
|
|
if (!value.items || typeof value.items !== 'object' || Array.isArray(value.items)) value.items = {}
|
|
value.schemaVersion = CURRENT.backup
|
|
value.version = Number(value.version) || 2
|
|
return { ok: true, changed: from !== CURRENT.backup, from, value }
|
|
}
|
|
|
|
global.IWBStorageMigrations = {
|
|
current: CURRENT,
|
|
migrateWorkspace,
|
|
migrateCanvas,
|
|
migrateGroups,
|
|
migrateBackup,
|
|
}
|
|
})(typeof window !== 'undefined' ? window : globalThis)
|