Showing the changes to packages/plugin-runtime/src (2 of 8 files). Show all
Unified · Split
| @@ -1,4 +1,12 @@ | |||
| 1 | export interface Contribution<Data = unknown> { | 1 | export type ContributionData = |
| 2 | | null | ||
| 3 | | boolean | ||
| 4 | | number | ||
| 5 | | string | ||
| 6 | | ReadonlyArray<ContributionData> | ||
| 7 | | { readonly [key: string]: ContributionData }; | ||
| 8 | |||
| 9 | export interface Contribution<Data extends ContributionData = ContributionData> { | ||
| 2 | readonly id: string; | 10 | readonly id: string; |
| 3 | readonly label: string; | 11 | readonly label: string; |
| 4 | readonly data?: Data; | 12 | readonly data?: Data; |
| @@ -12,6 +12,7 @@ import * as Scope from "effect/Scope"; · 11 unmodified lines | |||
| 12 | 12 | ||
| 13 | import type { | 13 | import type { |
| 14 | Contribution, | 14 | Contribution, |
| 15 | ContributionData, | ||
| 15 | PluginActivationContext, | 16 | PluginActivationContext, |
| 16 | PluginDefinition, | 17 | PluginDefinition, |
| 17 | PluginRuntimeContributionSnapshot, | 18 | PluginRuntimeContributionSnapshot, |
| @@ -245,19 +246,58 @@ const sameStringRecord = ( · 227 unmodified lines | |||
| 245 | return leftKeys.every((key) => left[key] === right[key]); | 246 | return leftKeys.every((key) => left[key] === right[key]); |
| 246 | }; | 247 | }; |
| 247 | 248 | ||
| 248 | const deepFreeze = <Value>(value: Value): Value => { | 249 | const cloneContributionData = ( |
| 249 | if (typeof value !== "object" || value === null || Object.isFrozen(value)) return value; | 250 | value: ContributionData, |
| 250 | for (const nested of Object.values(value)) deepFreeze(nested); | 251 | ancestors = new WeakSet<object>(), |
| 251 | return Object.freeze(value); | 252 | ): ContributionData => { |
| 253 | if (value === null || typeof value === "string" || typeof value === "boolean") return value; | ||
| 254 | if (typeof value === "number") { | ||
| 255 | if (!Number.isFinite(value)) throw new TypeError("Contribution data numbers must be finite"); | ||
| 256 | return value; | ||
| 257 | } | ||
| 258 | if (typeof value !== "object") { | ||
| 259 | throw new TypeError("Contribution data must contain only JSON-compatible values"); | ||
| 260 | } | ||
| 261 | if (ancestors.has(value)) throw new TypeError("Contribution data cannot contain cycles"); | ||
| 262 | ancestors.add(value); | ||
| 263 | try { | ||
| 264 | if (Array.isArray(value)) { | ||
| 265 | const clone: Array<ContributionData> = []; | ||
| 266 | for (let index = 0; index < value.length; index += 1) { | ||
| 267 | if (!Object.hasOwn(value, index)) { | ||
| 268 | throw new TypeError("Contribution data arrays cannot contain holes"); | ||
| 269 | } | ||
| 270 | clone.push(cloneContributionData(value[index]!, ancestors)); | ||
| 271 | } | ||
| 272 | return Object.freeze(clone); | ||
| 273 | } | ||
| 274 | |||
| 275 | const prototype = Object.getPrototypeOf(value); | ||
| 276 | if (prototype !== Object.prototype && prototype !== null) { | ||
| 277 | throw new TypeError("Contribution data objects must use a plain or null prototype"); | ||
| 278 | } | ||
| 279 | const clone = createNullPrototypeRecord<ContributionData>(); | ||
| 280 | for (const key of Reflect.ownKeys(value)) { | ||
| 281 | if (typeof key !== "string") { | ||
| 282 | throw new TypeError("Contribution data objects cannot use symbol keys"); | ||
| 283 | } | ||
| 284 | const descriptor = Object.getOwnPropertyDescriptor(value, key); | ||
| 285 | if (descriptor === undefined || descriptor.enumerable !== true || !("value" in descriptor)) { | ||
| 286 | throw new TypeError("Contribution data objects must use enumerable data properties"); | ||
| 287 | } | ||
| 288 | clone[key] = cloneContributionData(descriptor.value as ContributionData, ancestors); | ||
| 289 | } | ||
| 290 | return Object.freeze(clone); | ||
| 291 | } finally { | ||
| 292 | ancestors.delete(value); | ||
| 293 | } | ||
| 252 | }; | 294 | }; |
| 253 | 295 | ||
| 254 | const detachContribution = (contribution: Contribution): Contribution => | 296 | const detachContribution = (contribution: Contribution): Contribution => |
| 255 | Object.freeze({ | 297 | Object.freeze({ |
| 256 | id: contribution.id, | 298 | id: contribution.id, |
| 257 | label: contribution.label, | 299 | label: contribution.label, |
| 258 | ...(contribution.data === undefined | 300 | ...(contribution.data === undefined ? {} : { data: cloneContributionData(contribution.data) }), |
| 259 | ? {} | ||
| 260 | : { data: deepFreeze(structuredClone(contribution.data)) }), | ||
| 261 | }); | 301 | }); |
| 262 | 302 | ||
| 263 | const snapshotDefinitions = ( | 303 | const snapshotDefinitions = ( |