Skip to content

feat(rc): Add custom signals support #8602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
4cfd43b
Add support to set/update custom signals
ashish-kothari Oct 4, 2024
2bd7dc0
Add explanatory comments
ashish-kothari Oct 4, 2024
33444c5
Minor fixes in the custom signals storage API
ashish-kothari Oct 15, 2024
ea317aa
Add `setCustomSignals()` to RemoteConfigCompat
ashish-kothari Oct 24, 2024
ad74ca5
Add `setCustomSignals()` under firebase/compat
ashish-kothari Oct 24, 2024
77f1cb6
Address lint errors + add/fix few tests
ashish-kothari Oct 25, 2024
ed9a8b1
Add changeset
ashish-kothari Oct 25, 2024
26ce7e1
Run yarn format
ashish-kothari Oct 25, 2024
f3a0f16
Run `yarn docgen:all`
ashish-kothari Oct 25, 2024
44015b1
Add & update unit tests
ashish-kothari Oct 25, 2024
3f7a55c
Run `yarn format`
ashish-kothari Oct 25, 2024
95df4e4
Refactor `setCustomSignals` storage API
ashish-kothari Oct 25, 2024
ff99a7c
Add unit test + improve public API documentation
ashish-kothari Oct 30, 2024
805db83
Update changeset
ashish-kothari Oct 30, 2024
fd7c6c2
Add limit checks on custom signals keys and values
ashish-kothari Nov 6, 2024
832258b
Run yarn format
ashish-kothari Nov 6, 2024
07075dd
Multiple bug fixes
ashish-kothari Nov 20, 2024
9e1b157
Run `yarn docgen:all`
ashish-kothari Nov 21, 2024
bb5871b
Merge main into ashish-kothari/rc-custom-targeting
ashish-kothari Nov 24, 2024
b482664
Add debug logging in fetch
ashish-kothari Dec 4, 2024
cddc326
Run `yarn:format`
ashish-kothari Dec 4, 2024
3d6c298
Revert "Merge main into ashish-kothari/rc-custom-targeting"
ashish-kothari Dec 9, 2024
d306b5d
Resolve merge conflict
ashish-kothari Jan 7, 2025
e33869b
Merge branch 'main' into ashish-kothari/rc-custom-targeting
ashish-kothari Jan 7, 2025
98e6cfe
Update changeset description
ashish-kothari Jan 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add limit checks on custom signals keys and values
  • Loading branch information
ashish-kothari committed Nov 6, 2024
commit fd7c6c274be708c19e08fd4ea75bfdb05aa6c01c
21 changes: 19 additions & 2 deletions packages/remote-config/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import {
Value
} from './public_types';
import { RemoteConfigAbortSignal } from './client/remote_config_fetch_client';
import { RC_COMPONENT_NAME } from './constants';
import { ErrorCode, hasErrorCode } from './errors';
import { RC_COMPONENT_NAME, RC_CUSTOM_SIGNAL_KEY_MAX_LENGTH, RC_CUSTOM_SIGNAL_VALUE_MAX_LENGTH } from './constants';
import { ERROR_FACTORY, ErrorCode, hasErrorCode } from './errors';
import { RemoteConfig as RemoteConfigImpl } from './remote_config';
import { Value as ValueImpl } from './value';
import { LogLevel as FirebaseLogLevel } from '@firebase/logger';
Expand Down Expand Up @@ -276,5 +276,22 @@ export async function setCustomSignals(
customSignals: CustomSignals
): Promise<void> {
const rc = getModularInstance(remoteConfig) as RemoteConfigImpl;
// eslint-disable-next-line guard-for-in
for (const key in customSignals) {
if (key.length > RC_CUSTOM_SIGNAL_KEY_MAX_LENGTH) {
throw ERROR_FACTORY.create(ErrorCode.CUSTOM_SIGNAL_KEY_LENGTH, {
key,
maxLength: RC_CUSTOM_SIGNAL_KEY_MAX_LENGTH
});
}
const value = customSignals[key];
if (typeof value === 'string' && value.length > RC_CUSTOM_SIGNAL_VALUE_MAX_LENGTH) {
throw ERROR_FACTORY.create(ErrorCode.CUSTOM_SIGNAL_VALUE_LENGTH, {
key,
maxLength: RC_CUSTOM_SIGNAL_VALUE_MAX_LENGTH
});
}
}

return rc._storageCache.setCustomSignals(customSignals);
}
3 changes: 3 additions & 0 deletions packages/remote-config/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@
*/

export const RC_COMPONENT_NAME = 'remote-config';
export const RC_CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS = 100;
export const RC_CUSTOM_SIGNAL_KEY_MAX_LENGTH = 250;
export const RC_CUSTOM_SIGNAL_VALUE_MAX_LENGTH = 500;
16 changes: 14 additions & 2 deletions packages/remote-config/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ export const enum ErrorCode {
FETCH_THROTTLE = 'fetch-throttle',
FETCH_PARSE = 'fetch-client-parse',
FETCH_STATUS = 'fetch-status',
INDEXED_DB_UNAVAILABLE = 'indexed-db-unavailable'
INDEXED_DB_UNAVAILABLE = 'indexed-db-unavailable',
CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS = 'custom-signal-max-allowed-signals',
CUSTOM_SIGNAL_KEY_LENGTH = 'custom-signal-key-length',
CUSTOM_SIGNAL_VALUE_LENGTH = 'custom-signal-value-length'
}

const ERROR_DESCRIPTION_MAP: { readonly [key in ErrorCode]: string } = {
Expand Down Expand Up @@ -67,7 +70,13 @@ const ERROR_DESCRIPTION_MAP: { readonly [key in ErrorCode]: string } = {
[ErrorCode.FETCH_STATUS]:
'Fetch server returned an HTTP error status. HTTP status: {$httpStatus}.',
[ErrorCode.INDEXED_DB_UNAVAILABLE]:
'Indexed DB is not supported by current browser'
'Indexed DB is not supported by current browser',
[ErrorCode.CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS]:
'Setting more than {$maxSignals} custom signals is not supported.',
[ErrorCode.CUSTOM_SIGNAL_KEY_LENGTH]:
'Custom signal key {$key} is too long, max allowed length is {$maxLength}.',
[ErrorCode.CUSTOM_SIGNAL_VALUE_LENGTH]:
'Value supplied for custom signal {$key} is too long, max allowed length is {$maxLength}.'
};

// Note this is effectively a type system binding a code to params. This approach overlaps with the
Expand All @@ -86,6 +95,9 @@ interface ErrorParams {
[ErrorCode.FETCH_THROTTLE]: { throttleEndTimeMillis: number };
[ErrorCode.FETCH_PARSE]: { originalErrorMessage: string };
[ErrorCode.FETCH_STATUS]: { httpStatus: number };
[ErrorCode.CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS]: { maxSignals: number };
[ErrorCode.CUSTOM_SIGNAL_KEY_LENGTH]: { key: string, maxLength: number };
[ErrorCode.CUSTOM_SIGNAL_VALUE_LENGTH]: { key: string, maxLength: number };
}

export const ERROR_FACTORY = new ErrorFactory<ErrorCode, ErrorParams>(
Expand Down
9 changes: 9 additions & 0 deletions packages/remote-config/src/storage/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
FirebaseRemoteConfigObject
} from '../client/remote_config_fetch_client';
import { ERROR_FACTORY, ErrorCode } from '../errors';
import { RC_CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS } from '../constants';
import { FirebaseError } from '@firebase/util';

/**
Expand Down Expand Up @@ -201,6 +202,14 @@ export class Storage {
const signalsToUpdate = Object.fromEntries(
Object.entries(combinedSignals).filter(([_, v]) => v !== null)
);

// Throw an error if the number of custom signals to be stored exceeds the limit
if (Object.keys(signalsToUpdate).length > RC_CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS) {
throw ERROR_FACTORY.create(ErrorCode.CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS, {
maxSignals: RC_CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS
});
}

return this.setWithTransaction<CustomSignals>(
'custom_signals',
signalsToUpdate,
Expand Down
18 changes: 18 additions & 0 deletions packages/remote-config/test/remote_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ describe('RemoteConfig', () => {
key: 'value'
});
});

it('throws an error when supplied with a custom signal key greater than 250 characters', async () => {
const longKey = 'a'.repeat(251);
const customSignals = { [longKey]: 'value' };

await expect(setCustomSignals(rc, customSignals)).to.eventually.be.rejectedWith(
`Remote Config: Custom signal key ${longKey} is too long, max allowed length is 250.`
);
});

it('throws an error when supplied with a custom signal value greater than 500 characters', async () => {
const longValue = 'a'.repeat(501);
const customSignals = { 'key': longValue };

await expect(setCustomSignals(rc, customSignals)).to.eventually.be.rejectedWith(
'Remote Config: Value supplied for custom signal key is too long, max allowed length is 500.'
);
});
});

// Adapts getUserLanguage tests from packages/auth/test/utils_test.js for TypeScript.
Expand Down
12 changes: 12 additions & 0 deletions packages/remote-config/test/storage/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,16 @@ describe('Storage', () => {

expect(storedCustomSignals).to.deep.eq(updatedSignals);
});

it('throws an error when supplied with excess custom signals', async () => {
const customSignals: { [key: string]: string } = {};
for (let i = 0; i < 101; i++) {
customSignals[`key${i}`] = `value${i}`;
}

await expect(storage.setCustomSignals(customSignals)).to.eventually.be.rejectedWith(
'Remote Config: Setting more than 100 custom signals is not supported.'
);

});
});
Loading