From 8152cf81cb6f11f96339450e2b0b061fed875063 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Fri, 4 Oct 2024 21:23:59 +0200 Subject: [PATCH] [#667] sanity checks for Yjs caveats. In dev_mode, objects inserted into Yjs can't be manipulated. --- package-lock.json | 9 +++++---- package.json | 4 ++-- src/structs/ContentAny.js | 6 ++++++ tests/y-array.tests.js | 25 +++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index c0757f88..69feaaa8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "13.6.19", "license": "MIT", "dependencies": { - "lib0": "^0.2.86" + "lib0": "^0.2.98" }, "devDependencies": { "@rollup/plugin-commonjs": "^24.0.1", @@ -2785,13 +2785,14 @@ } }, "node_modules/lib0": { - "version": "0.2.88", - "resolved": "https://registry.npmjs.org/lib0/-/lib0-0.2.88.tgz", - "integrity": "sha512-KyroiEvCeZcZEMx5Ys+b4u4eEBbA1ch7XUaBhYpwa/nPMrzTjUhI4RfcytmQfYoTBPcdyx+FX6WFNIoNuJzJfQ==", + "version": "0.2.98", + "resolved": "https://registry.npmjs.org/lib0/-/lib0-0.2.98.tgz", + "integrity": "sha512-XteTiNO0qEXqqweWx+b21p/fBnNHUA1NwAtJNJek1oPrewEZs2uiT4gWivHKr9GqCjDPAhchz0UQO8NwU3bBNA==", "dependencies": { "isomorphic.js": "^0.2.4" }, "bin": { + "0ecdsa-generate-keypair": "bin/0ecdsa-generate-keypair.js", "0gentesthtml": "bin/gentesthtml.js", "0serve": "bin/0serve.js" }, diff --git a/package.json b/package.json index 3452ac16..4afa4fab 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ }, "scripts": { "clean": "rm -rf dist docs", - "test": "npm run dist && node ./dist/tests.cjs --repetition-time 50", + "test": "npm run dist && NODE_ENV=development node ./dist/tests.cjs --repetition-time 50", "test-extensive": "npm run lint && npm run dist && node ./dist/tests.cjs --production --repetition-time 10000", "dist": "npm run clean && rollup -c && tsc", "watch": "rollup -wc", @@ -76,7 +76,7 @@ }, "homepage": "https://docs.yjs.dev", "dependencies": { - "lib0": "^0.2.86" + "lib0": "^0.2.98" }, "devDependencies": { "@rollup/plugin-commonjs": "^24.0.1", diff --git a/src/structs/ContentAny.js b/src/structs/ContentAny.js index 613144d8..3ab2dc5f 100644 --- a/src/structs/ContentAny.js +++ b/src/structs/ContentAny.js @@ -2,6 +2,11 @@ import { UpdateEncoderV1, UpdateEncoderV2, UpdateDecoderV1, UpdateDecoderV2, Transaction, Item, StructStore // eslint-disable-line } from '../internals.js' +import * as env from 'lib0/environment' +import * as object from 'lib0/object' + +const isDevMode = env.getVariable('node_env') === 'development' + export class ContentAny { /** * @param {Array} arr @@ -11,6 +16,7 @@ export class ContentAny { * @type {Array} */ this.arr = arr + isDevMode && object.deepFreeze(arr) } /** diff --git a/tests/y-array.tests.js b/tests/y-array.tests.js index 9875e85e..1f593485 100644 --- a/tests/y-array.tests.js +++ b/tests/y-array.tests.js @@ -4,6 +4,9 @@ import * as Y from '../src/index.js' import * as t from 'lib0/testing' import * as prng from 'lib0/prng' import * as math from 'lib0/math' +import * as env from 'lib0/environment' + +const isDevMode = env.getVariable('node_env') === 'development' /** * @param {t.TestCase} tc @@ -17,6 +20,28 @@ export const testBasicUpdate = tc => { t.compare(doc2.getArray('array').toArray(), ['hi']) } +/** + * @param {t.TestCase} tc + */ +export const testFailsObjectManipulationInDevMode = tc => { + if (isDevMode) { + t.info('running in dev mode') + const doc = new Y.Doc() + const a = [1, 2, 3] + const b = { o: 1 } + doc.getArray('test').insert(0, [a]) + doc.getMap('map').set('k', b) + t.fails(() => { + a[0] = 42 + }) + t.fails(() => { + b.o = 42 + }) + } else { + t.info('not in dev mode') + } +} + /** * @param {t.TestCase} tc */