Skip to content

Commit fa244ba

Browse files
committed
[hygiene] Obfuscate protected methods
1 parent cd9cf9c commit fa244ba

7 files changed

Lines changed: 111 additions & 36 deletions

File tree

coverage.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"tests":7665,"assertions":32897,"lines":{"total":2596,"covered":2596,"skipped":0,"pct":100},"statements":{"total":2820,"covered":2820,"skipped":0,"pct":100},"functions":{"total":1146,"covered":1146,"skipped":0,"pct":100},"branches":{"total":974,"covered":974,"skipped":0,"pct":100},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}
1+
{"tests":4555,"assertions":24950,"lines":{"total":2630,"covered":2617,"skipped":0,"pct":99.5},"statements":{"total":2859,"covered":2839,"skipped":0,"pct":99.3},"functions":{"total":1164,"covered":1144,"skipped":0,"pct":98.28},"branches":{"total":1012,"covered":1000,"skipped":0,"pct":98.81},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}

src/checkpoints/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {objFreeze} from '../common/obj.ts';
4343
import {ifNotUndefined, isUndefined, size} from '../common/other.ts';
4444
import {IdSet2} from '../common/set.ts';
4545
import {EMPTY_STRING} from '../common/strings.ts';
46+
import {ProtectedStore} from '../index.ts';
4647

4748
type CellsDelta = IdMap3<ChangedCell>;
4849
type ValuesDelta = IdMap<ChangedValue>;
@@ -76,7 +77,7 @@ export const createCheckpoints = getCreateFunction(
7677
collForEach(cellsDelta, (table, tableId) =>
7778
collForEach(table, (row, rowId) =>
7879
collForEach(row, (oldNew, cellId) =>
79-
(store as any).setOrDelCell(
80+
(store as ProtectedStore)._[5](
8081
tableId,
8182
rowId,
8283
cellId,
@@ -87,7 +88,7 @@ export const createCheckpoints = getCreateFunction(
8788
),
8889
);
8990
collForEach(valuesDelta, (oldNew, valueId) =>
90-
(store as any).setOrDelValue(
91+
(store as ProtectedStore)._[6](
9192
valueId,
9293
oldNew[oldOrNew] as ValueOrUndefined,
9394
true,

src/mergeable-store/index.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,17 @@ import {
7878
strEndsWith,
7979
strStartsWith,
8080
} from '../common/strings.ts';
81-
import {createStore} from '../store/index.ts';
81+
import {ProtectedStore, createStore} from '../store/index.ts';
82+
83+
export type ProtectedMergeableStore = ProtectedStore & {__: ProtectedMethods};
84+
85+
type ProtectedMethods = [
86+
hadMutated: () => 0 | 1,
87+
getEncodedMergeableContent: () => MergeableContent,
88+
getEncodedTransactionMergeableChanges: (
89+
withHashes: boolean,
90+
) => MergeableChanges<typeof withHashes>,
91+
];
8292

8393
const LISTENER_ARGS: IdObj<number> = {
8494
HasTable: 1,
@@ -687,13 +697,14 @@ export const createMergeableStore = ((
687697
applyMergeableChanges,
688698
merge,
689699

690-
// only used internally by other modules
691-
hadMutated,
692-
getEncodedMergeableContent,
693-
getEncodedTransactionMergeableChanges,
700+
__: [
701+
hadMutated,
702+
getEncodedMergeableContent,
703+
getEncodedTransactionMergeableChanges,
704+
] as ProtectedMethods,
694705
};
695706

696-
(store as any).setInternalListeners(
707+
(store as ProtectedStore)._[3](
697708
preStartTransaction,
698709
preFinishTransaction,
699710
postFinishTransaction,

src/middleware/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {getCreateFunction} from '../common/definable.ts';
3636
import {IdMap, mapEnsure, mapGet, mapNew} from '../common/map.ts';
3737
import {objFreeze} from '../common/obj.ts';
3838
import {ifNotUndefined, isUndefined} from '../common/other.ts';
39+
import {ProtectedStore} from '../store/index.ts';
3940

4041
const reduceCallbacks = (
4142
callbacks: ((...args: any[]) => any)[],
@@ -190,7 +191,7 @@ export const createMiddleware = getCreateFunction(
190191
destroy,
191192
} as Middleware);
192193

193-
(store as any).setMiddleware(
194+
(store as ProtectedStore)._[4](
194195
willSetContent,
195196
willSetTables,
196197
willSetTable,

src/persisters/common/create.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import {mapEnsure, mapGet, mapNew, mapSet} from '../../common/map.ts';
2121
import {objFreeze, objIsEmpty} from '../../common/obj.ts';
2222
import {errorNew, isArray, isUndefined, tryCatch} from '../../common/other.ts';
2323
import {IdSet2} from '../../common/set.ts';
24+
import {ProtectedMergeableStore} from '../../mergeable-store/index.ts';
25+
import {ProtectedStore} from '../../store/index.ts';
2426

2527
const enum StatusValues {
2628
Idle = 0,
@@ -75,18 +77,20 @@ const getStoreFunctions = (
7577
persist != PersistsValues.StoreOnly && store.isMergeable()
7678
? [
7779
1,
78-
(store as any).getEncodedMergeableContent,
80+
(store as ProtectedMergeableStore).__[1],
7981
() =>
80-
(store as any).getEncodedTransactionMergeableChanges(!isSynchronizer),
82+
(store as ProtectedMergeableStore).__[2](
83+
!isSynchronizer,
84+
) as MergeableChanges<typeof isSynchronizer extends 1 ? false : true>,
8185
([[changedTables], [changedValues]]: MergeableChanges) =>
8286
!objIsEmpty(changedTables) || !objIsEmpty(changedValues),
8387
(store as MergeableStore).setDefaultContent,
8488
]
8589
: persist != PersistsValues.MergeableStoreOnly
8690
? [
8791
0,
88-
(store as any).getEncodedContent,
89-
(store as any).getEncodedTransactionChanges,
92+
(store as ProtectedStore)._[7],
93+
(store as ProtectedStore)._[8],
9094
([changedTables, changedValues]: Changes) =>
9195
!objIsEmpty(changedTables) || !objIsEmpty(changedValues),
9296
store.setContent,
@@ -191,7 +195,7 @@ export const createCustomPersister = <
191195
};
192196

193197
const saveAfterMutated = async (): Promise<void> => {
194-
if (isAutoSaving() && (store as any).hadMutated?.()) {
198+
if (isAutoSaving() && (store as ProtectedMergeableStore).__?.[0]?.()) {
195199
await save();
196200
}
197201
};

src/queries/index.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ import {
4949
collSize3,
5050
} from '../common/coll.ts';
5151
import {getCreateFunction, getDefinableFunctions} from '../common/definable.ts';
52-
import {AddListener, CallListeners} from '../common/listeners.ts';
5352
import {
5453
IdMap,
5554
IdMap2,
@@ -92,6 +91,7 @@ import {
9291
SORTED_ROW_IDS,
9392
TABLE,
9493
} from '../common/strings.ts';
94+
import {ProtectedStore} from '../index.ts';
9595

9696
type Build = (builders: {
9797
select: Select;
@@ -102,11 +102,6 @@ type Build = (builders: {
102102
param: Param;
103103
}) => void;
104104

105-
type StoreWithPrivateMethods = Store & {
106-
createStore: () => Store;
107-
addListener: AddListener;
108-
callListeners: CallListeners;
109-
};
110105
type SelectClause = (getTableCell: GetTableCell, rowId: Id) => CellOrUndefined;
111106
type JoinClause = [
112107
realTableId: Id,
@@ -127,18 +122,17 @@ type Aggregators = [
127122
];
128123

129124
export const createQueries = getCreateFunction((store: Store): Queries => {
130-
const createStore = (store as StoreWithPrivateMethods).createStore;
125+
const createStore = (store as ProtectedStore)._[0];
131126
const preStore = createStore();
132127
const resultStore = createStore();
133128
const preStoreListenerIds: Map<Id, Map<Store, IdSet>> = mapNew();
134129
const paramValuesListeners: IdSet2 = mapNew();
135130
const paramValueListeners: IdSet3 = mapNew();
136131

137132
const {
138-
addListener,
139-
callListeners,
133+
_: [, addListener, callListeners],
140134
delListener: delListenerImpl,
141-
} = resultStore as StoreWithPrivateMethods;
135+
} = resultStore as ProtectedStore;
142136
const [
143137
getStore,
144138
getQueryIds,
@@ -516,7 +510,7 @@ export const createQueries = getCreateFunction((store: Store): Queries => {
516510
selectJoinWhereStore.transaction(() =>
517511
arrayEvery(wheres, (where) => where(getTableCell))
518512
? mapForEach(selects, (asCellId, tableCellGetter) =>
519-
(selectJoinWhereStore as any).setOrDelCell(
513+
(selectJoinWhereStore as ProtectedStore)._[5](
520514
queryId,
521515
rootRowId,
522516
asCellId,

src/store/index.ts

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ import {
6868
import {defaultSorter} from '../common/index.ts';
6969
import {jsonParse, jsonStringWithMap} from '../common/json.ts';
7070
import {
71+
AddListener,
72+
CallListeners,
7173
ExtraArgsGetter,
7274
IdSetNode,
7375
PathGetters,
@@ -147,6 +149,67 @@ import {
147149
id,
148150
} from '../common/strings.ts';
149151

152+
export type ProtectedStore = Store & {_: ProtectedMethods};
153+
154+
type ProtectedMethods = [
155+
createStore: () => Store,
156+
addListener: AddListener,
157+
callListeners: CallListeners,
158+
setInternalListeners: (
159+
preStartTransaction: () => void,
160+
preFinishTransaction: () => void,
161+
postFinishTransaction: () => void,
162+
cellChanged: (
163+
tableId: Id,
164+
rowId: Id,
165+
cellId: Id,
166+
newCell: CellOrUndefined,
167+
mutating: 0 | 1,
168+
) => void,
169+
valueChanged: (
170+
valueId: Id,
171+
newValue: ValueOrUndefined,
172+
mutating: 0 | 1,
173+
) => void,
174+
) => void,
175+
setMiddleware: (
176+
willSetContent: (content: Content) => Content | undefined,
177+
willSetTables: (tables: Tables) => Tables | undefined,
178+
willSetTable: (tableId: Id, table: Table) => Table | undefined,
179+
willSetRow: (tableId: Id, rowId: Id, row: Row) => Row | undefined,
180+
willSetCell: (
181+
tableId: Id,
182+
rowId: Id,
183+
cellId: Id,
184+
cell: Cell,
185+
) => CellOrUndefined,
186+
willSetValues: (values: Values) => Values | undefined,
187+
willSetValue: (valueId: Id, value: Value) => ValueOrUndefined,
188+
willDelTables: () => boolean,
189+
willDelTable: (tableId: Id) => boolean,
190+
willDelRow: (tableId: Id, rowId: Id) => boolean,
191+
willDelCell: (tableId: Id, rowId: Id, cellId: Id) => boolean,
192+
willDelValues: () => boolean,
193+
willDelValue: (valueId: Id) => boolean,
194+
willApplyChanges: (changes: Changes) => Changes | undefined,
195+
didSetRow: (tableId: Id, rowId: Id, oldRow: Row, newRow: Row) => Row,
196+
) => void,
197+
setOrDelCell: (
198+
tableId: Id,
199+
rowId: Id,
200+
cellId: Id,
201+
cell: CellOrUndefined,
202+
skipMiddleware?: boolean,
203+
) => Store,
204+
setOrDelValue: (
205+
valueId: Id,
206+
value: ValueOrUndefined,
207+
skipMiddleware?: boolean,
208+
) => Store,
209+
getEncodedContent: () => Content,
210+
getEncodedTransactionChanges: () => Changes,
211+
];
212+
150213
type TablesSchemaMap = IdMap2<CellSchema>;
151214
type ValuesSchemaMap = IdMap<ValueSchema>;
152215
type RowMap = IdMap<Cell>;
@@ -2152,16 +2215,17 @@ export const createStore: typeof createStoreDecl = (): Store => {
21522215

21532216
isMergeable: () => false,
21542217

2155-
// only used internally by other modules
2156-
createStore,
2157-
addListener,
2158-
callListeners,
2159-
setInternalListeners,
2160-
setMiddleware,
2161-
setOrDelCell,
2162-
setOrDelValue,
2163-
getEncodedContent,
2164-
getEncodedTransactionChanges,
2218+
_: [
2219+
createStore,
2220+
addListener,
2221+
callListeners,
2222+
setInternalListeners,
2223+
setMiddleware,
2224+
setOrDelCell,
2225+
setOrDelValue,
2226+
getEncodedContent,
2227+
getEncodedTransactionChanges,
2228+
] as ProtectedMethods,
21652229
};
21662230

21672231
// and now for some gentle meta-programming

0 commit comments

Comments
 (0)