Skip to content

Commit

Permalink
ADD error on bailout (#4272)
Browse files Browse the repository at this point in the history
* ADD error on bailout

* REPLACE fast-deep-equal because of epoberezkin/fast-deep-equal#105

* REPLACE object-path

* UPDATE event-reduce
  • Loading branch information
pubkey authored Jan 7, 2023
1 parent 3356260 commit 79410d3
Show file tree
Hide file tree
Showing 15 changed files with 455 additions and 56 deletions.
18 changes: 15 additions & 3 deletions config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,20 @@ const TerserPlugin = require('terser-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;


//console.log(process.env.NODE_ENV);
//process.exit();
/**
* Throw on bailouts.
* If a dependency is causing a bailout,
* it must be replaced!
*/
const oldConsoleLog = console.log.bind(console);
console.log = function (m1, m2, m3) {
if (m1.includes('not an ECMAScript module')) {
oldConsoleLog(m1);
throw new Error('ERROR: A dependency of RxDB is causing an optimization bailout. This is not allowed.');
} else {
return oldConsoleLog(m1, m2, m3);
}
};


const plugins = [];
Expand All @@ -30,6 +42,6 @@ module.exports = {
* @link https://webpack.js.org/plugins/module-concatenation-plugin/#debugging-optimization-bailouts
*/
optimizationBailout: true,
warnings: false
warnings: true
}
};
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,6 @@
"@types/cors": "2.8.13",
"@types/express": "4.17.15",
"@types/lokijs": "1.5.7",
"@types/object-path": "0.11.1",
"@types/simple-peer": "9.11.5",
"@types/ws": "8.5.4",
"ajv": "8.12.0",
Expand All @@ -393,8 +392,7 @@
"crypto-js": "4.1.1",
"custom-idle-queue": "3.0.1",
"dexie": "4.0.0-alpha.4",
"event-reduce-js": "2.0.4",
"fast-deep-equal": "3.1.3",
"event-reduce-js": "2.1.0",
"firebase": "9.15.0",
"get-graphql-from-jsonschema": "8.1.0",
"graphql": "15.8.0",
Expand All @@ -406,7 +404,6 @@
"lokijs": "1.5.12",
"mingo": "6.2.5",
"modifyjs": "0.3.1",
"object-path": "0.11.8",
"oblivious-set": "1.1.1",
"pouchdb-selector-core": "7.3.1",
"reconnecting-websocket": "4.4.0",
Expand Down
13 changes: 7 additions & 6 deletions src/plugins/crdt/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { newRxError } from '../../rx-error';
import objectPath from 'object-path';
import type {
CRDTDocumentField,
CRDTEntry,
Expand All @@ -19,8 +18,10 @@ import {
clone,
deepEqual,
ensureNotFalsy,
getProperty,
now,
objectPathMonad,
setProperty,
toArray
} from '../../plugins/utils';
import modifyjs from 'modifyjs';
Expand Down Expand Up @@ -50,7 +51,7 @@ export async function updateCRDT<RxDocType>(
const storageToken = await this.collection.database.storageToken;

return this.incrementalModify((docData) => {
const crdtDocField: CRDTDocumentField<RxDocType> = clone(objectPath.get(docData as any, crdtOptions.field));
const crdtDocField: CRDTDocumentField<RxDocType> = clone(getProperty(docData as any, crdtOptions.field));
const operation: CRDTOperation<RxDocType> = {
body: toArray(entry),
creator: storageToken,
Expand All @@ -71,7 +72,7 @@ export async function updateCRDT<RxDocType>(
docData,
operation
);
objectPath.set(docData, crdtOptions.field, crdtDocField);
setProperty(docData, crdtOptions.field, crdtDocField);
return docData;
}, RX_CRDT_CONTEXT);
}
Expand Down Expand Up @@ -109,7 +110,7 @@ export async function insertCRDT<RxDocType>(
operations: [],
hash: ''
};
objectPath.set(insertData as any, crdtOptions.field, crdtDocField);
setProperty(insertData as any, crdtOptions.field, crdtDocField);

const lastAr: CRDTOperation<RxDocType>[] = [operation];
crdtDocField.operations.push(lastAr);
Expand Down Expand Up @@ -288,7 +289,7 @@ export function rebuildFromCRDT<RxDocType>(
let base: WithDeleted<RxDocType> = {
_deleted: false
} as any;
objectPath.set(base, ensureNotFalsy(schema.crdt).field, crdts);
setProperty(base, ensureNotFalsy(schema.crdt).field, crdts);
crdts.operations.forEach(operations => {
operations.forEach(op => {
base = runOperationOnDocument(
Expand Down Expand Up @@ -502,7 +503,7 @@ export const RxDBcrdtPlugin: RxPlugin = {
hash: ''
};
crdtOperations.hash = hashCRDTOperations(collection.database.hashFunction, crdtOperations);
objectPath.set(docData, crdtOptions.field, crdtOperations);
setProperty(docData, crdtOptions.field, crdtOperations);
return docData;
});
return bulkInsertBefore(useDocsData);
Expand Down
16 changes: 7 additions & 9 deletions src/plugins/dev-mode/check-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
* does additional checks over the schema-json
* to ensure nothing is broken or not supported
*/

import objectPath from 'object-path';
import {
newRxError
} from '../../rx-error';
Expand All @@ -16,7 +14,7 @@ import type {
TopLevelProperty
} from '../../types';
import {
flattenObject, isMaybeReadonlyArray,
flattenObject, getProperty, isMaybeReadonlyArray,
trimDots
} from '../../plugins/utils';
import { rxDocumentProperties } from './entity-properties';
Expand Down Expand Up @@ -458,8 +456,8 @@ export function checkSchema(jsonSchema: RxJsonSchema<any>) {
.filter(key => key !== '')
.filter((elem, pos, arr) => arr.indexOf(elem) === pos) // unique
.filter(key => { // check if this path defines an index
const value = objectPath.get(jsonSchema, key);
return !!value.index;
const value = getProperty(jsonSchema, key);
return value && !!value.index;
})
.forEach(key => { // replace inner properties
key = key.replace('properties.', ''); // first
Expand All @@ -483,7 +481,7 @@ export function checkSchema(jsonSchema: RxJsonSchema<any>) {
.filter((elem, pos, arr) => arr.indexOf(elem) === pos) // from now on working only with unique indexes
.map(indexPath => {
const realPath = getSchemaPropertyRealPath(indexPath); // real path in the collection schema
const schemaObj = objectPath.get(jsonSchema, realPath); // get the schema of the indexed property
const schemaObj = getProperty(jsonSchema, realPath); // get the schema of the indexed property
if (!schemaObj || typeof schemaObj !== 'object') {
throw newRxError('SC21', {
index: indexPath,
Expand Down Expand Up @@ -524,8 +522,8 @@ export function checkSchema(jsonSchema: RxJsonSchema<any>) {
.filter((elem, pos, arr) => arr.indexOf(elem) === pos) // unique
.filter(key => {
// check if this path defines an encrypted field
const value = objectPath.get(jsonSchema, key);
return !!value.encrypted;
const value = getProperty(jsonSchema, key);
return value && !!value.encrypted;
})
.forEach(key => { // replace inner properties
key = key.replace('properties.', ''); // first
Expand All @@ -543,7 +541,7 @@ export function checkSchema(jsonSchema: RxJsonSchema<any>) {
// real path in the collection schema
const realPath = getSchemaPropertyRealPath(propPath);
// get the schema of the indexed property
const schemaObj = objectPath.get(jsonSchema, realPath);
const schemaObj = getProperty(jsonSchema, realPath);
if (!schemaObj || typeof schemaObj !== 'object') {
throw newRxError('SC28', {
field: propPath,
Expand Down
13 changes: 7 additions & 6 deletions src/plugins/encryption/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
import AES from 'crypto-js/aes';
import * as cryptoEnc from 'crypto-js/enc-utf8';
import objectPath from 'object-path';
import { wrapRxStorageInstance } from '../../plugin-helpers';
import {
INTERNAL_STORE_SCHEMA_TITLE
Expand All @@ -26,7 +25,9 @@ import {
b64EncodeUnicode,
clone,
ensureNotFalsy,
flatClone
flatClone,
getProperty,
setProperty
} from '../../plugins/utils';

export const MINIMUM_PASSWORD_LENGTH: 8 = 8;
Expand Down Expand Up @@ -121,14 +122,14 @@ export function wrappedKeyEncryptionStorage<Internals, InstanceCreationOptions>(
docData = cloneWithoutAttachments(docData);
ensureNotFalsy(params.schema.encrypted)
.forEach(path => {
const value = objectPath.get(docData, path);
const value = getProperty(docData, path);
if (typeof value === 'undefined') {
return;
}

const stringValue = JSON.stringify(value);
const encrypted = encryptString(stringValue, password);
objectPath.set(docData, path, encrypted);
setProperty(docData, path, encrypted);
});

// handle attachments
Expand All @@ -153,13 +154,13 @@ export function wrappedKeyEncryptionStorage<Internals, InstanceCreationOptions>(
docData = cloneWithoutAttachments(docData);
ensureNotFalsy(params.schema.encrypted)
.forEach(path => {
const value = objectPath.get(docData, path);
const value = getProperty(docData, path);
if (typeof value === 'undefined') {
return;
}
const decrypted = decryptString(value, password);
const decryptedParsed = JSON.parse(decrypted);
objectPath.set(docData, path, decryptedParsed);
setProperty(docData, path, decryptedParsed);
});
return docData;
}
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/local-documents/rx-local-document.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import objectPath from 'object-path';
import { Observable } from 'rxjs';
import {
distinctUntilChanged,
Expand Down Expand Up @@ -34,6 +33,7 @@ import {
getDefaultRxDocumentMeta,
getFromMapOrThrow,
getFromObjectOrThrow,
getProperty,
RXJS_SHARE_REPLAY_DEFAULTS
} from '../../plugins/utils';
import { getLocalDocStateByParent, LOCAL_DOC_STATE_BY_PARENT_RESOLVED } from './local-documents-helper';
Expand Down Expand Up @@ -100,7 +100,7 @@ const RxLocalDocumentPrototype: any = {
});
}

let valueObj = objectPath.get(this._data, objPath);
let valueObj = getProperty(this._data, objPath);
valueObj = overwritable.deepFreezeWhenDevMode(valueObj);
return valueObj;
},
Expand All @@ -119,7 +119,7 @@ const RxLocalDocumentPrototype: any = {
}
return this.$
.pipe(
map(data => objectPath.get(data, objPath)),
map(data => getProperty(data, objPath)),
distinctUntilChanged()
);
},
Expand Down
10 changes: 4 additions & 6 deletions src/plugins/replication-graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
* this plugin adds the RxCollection.syncGraphQl()-function to rxdb
* you can use it to sync collections with a remote graphql endpoint.
*/
import objectPath from 'object-path';
import {
ensureNotFalsy,
fastUnsecureHash
fastUnsecureHash,
getProperty
} from '../../plugins/utils';

import {
Expand Down Expand Up @@ -127,10 +127,8 @@ export function replicateGraphQL<RxDocType, CheckpointType>(
if (result.errors) {
throw result.errors;
}

const dataPath = pull.dataPath || ['data', Object.keys(result.data)[0]];
let data: any = objectPath.get(result, dataPath);

let data: any = getProperty(result, dataPath);
if (pull.responseModifier) {
data = await pull.responseModifier(
data,
Expand Down Expand Up @@ -165,7 +163,7 @@ export function replicateGraphQL<RxDocType, CheckpointType>(
throw result.errors;
}
const dataPath = Object.keys(result.data)[0];
const data: any = objectPath.get(result.data, dataPath);
const data: any = getProperty(result.data, dataPath);
return data;
},
batchSize: push.batchSize,
Expand Down
7 changes: 3 additions & 4 deletions src/plugins/storage-lokijs/lokijs-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ import {
add as unloadAdd,
AddReturn
} from 'unload';
import { ensureNotFalsy, flatClone, promiseWait, randomCouchString } from '../utils';
import { ensureNotFalsy, flatClone, getProperty, promiseWait, randomCouchString } from '../utils';
import { LokiSaveQueue } from './loki-save-queue';
import type { DeterministicSortComparator } from 'event-reduce-js';
import { newRxError } from '../../rx-error';
import objectPath from 'object-path';
import {
LeaderElector,
OnMessageHandler
Expand Down Expand Up @@ -225,8 +224,8 @@ export function getLokiSortComparator<RxDocType>(
const fieldName: string = Object.keys(sortPart)[0];
const direction: MangoQuerySortDirection = Object.values(sortPart)[0];
const directionMultiplier = direction === 'asc' ? 1 : -1;
const valueA: any = objectPath.get(a as any, fieldName);
const valueB: any = objectPath.get(b as any, fieldName);
const valueA: any = getProperty(a as any, fieldName);
const valueB: any = getProperty(b as any, fieldName);
if (valueA === valueB) {
return false;
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export * from './utils-document';
export * from './utils-hash';
export * from './utils-promise';
export * from './utils-string';
export * from './utils-object-deep-equal';
export * from './utils-object-dot-prop';
export * from './utils-object';
export * from './utils-error';
export * from './utils-time';
Expand Down
46 changes: 46 additions & 0 deletions src/plugins/utils/utils-object-deep-equal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

/**
* Copied from the fast-deep-equal package
* because it does not support es modules and causes optimization bailouts.
* TODO use the npm package again when this is merged:
* @link https://github.com/epoberezkin/fast-deep-equal/pull/105
*/
export function deepEqual(a: any, b: any): boolean {
if (a === b) return true;

if (a && b && typeof a == 'object' && typeof b == 'object') {
if (a.constructor !== b.constructor) return false;

let length;
let i;
if (Array.isArray(a)) {
length = a.length;
if (length !== b.length) return false;
for (i = length; i-- !== 0;)
if (!deepEqual(a[i], b[i])) return false;
return true;
}


if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags;
if (a.valueOf !== Object.prototype.valueOf) return a.valueOf() === b.valueOf();
if (a.toString !== Object.prototype.toString) return a.toString() === b.toString();

const keys = Object.keys(a);
length = keys.length;
if (length !== Object.keys(b).length) return false;

for (i = length; i-- !== 0;)
if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;

for (i = length; i-- !== 0;) {
const key = keys[i];
if (!deepEqual(a[key], b[key])) return false;
}

return true;
}

// true if both NaN, false otherwise
return a !== a && b !== b;
}
Loading

0 comments on commit 79410d3

Please sign in to comment.