Skip to content

Commit 39cf218

Browse files
committed
Replace string errorCodes with numeric errorIds to make the bundles smaller.
1 parent c7c099a commit 39cf218

File tree

4 files changed

+35
-16
lines changed

4 files changed

+35
-16
lines changed

packages/firestore/scripts/remove-asserts.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/firestore/scripts/remove-asserts.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,26 @@ class RemoveAsserts {
7979
const errorMessage = RemoveAsserts.trimErrorMessage(stringLiteral.getFullText());
8080
const errorCode = RemoveAsserts.errorCode(errorMessage);
8181

82+
let errorId: number = -1;
8283
try {
83-
RemoveAsserts.saveErrorCode(errorCode, errorMessage);
84+
errorId = RemoveAsserts.saveErrorCode(errorCode, errorMessage);
8485
}
8586
catch (e) {
8687
console.log('Failed to save error code ' + JSON.stringify(e));
8788
}
8889
const newArguments = [...node.arguments];
89-
newArguments[messageIndex] = ts.createLiteral(errorCode);
90+
newArguments[messageIndex] = ts.factory.createNumericLiteral(errorId);
9091

9192
// Replace the call with the full error message to a
9293
// build with an error code
93-
updatedNode = ts.createCall(
94+
updatedNode = ts.factory.createCallExpression(
9495
declaration.name!,
9596
/*typeArgs*/ undefined,
9697
newArguments
9798
);
9899
} else {
99100
const newArguments = [...node.arguments];
100-
newArguments[messageIndex] = ts.createLiteral('Unexpected error');
101+
newArguments[messageIndex] = ts.factory.createNumericLiteral(-1);
101102
// Remove the log message but keep the assertion
102103
updatedNode = ts.createCall(
103104
declaration.name!,
@@ -135,22 +136,41 @@ class RemoveAsserts {
135136
return paramHash;
136137
}
137138

138-
static saveErrorCode(errorCode: string, errorMessage: string): void {
139+
140+
141+
static saveErrorCode(errorCode: string, errorMessage: string): number {
139142
const errorCodes = RemoveAsserts.getErrorCodes();
140-
errorCodes[errorCode] = errorMessage;
143+
144+
const existingErrorCode: Error | undefined = errorCodes[errorCode];
145+
if (existingErrorCode)
146+
{return existingErrorCode.id;}
147+
148+
const id = Object.keys(errorCodes).length;
149+
errorCodes[errorCode] = {
150+
message: errorMessage,
151+
id
152+
};
153+
141154
RemoveAsserts.saveErrorCodes(errorCodes);
155+
156+
return id;
142157
}
143158

144-
static getErrorCodes(): Record<string, string> {
159+
static getErrorCodes(): Record<string, Error> {
145160
const path = join(module.path, ERROR_CODE_LOCATION);
146161
if (!existsSync(path)){
147162
return {};
148163
}
149164
return JSON.parse(readFileSync(path, 'utf-8'));
150165
}
151166

152-
static saveErrorCodes(errorCodes: Record<string, string>): void {
167+
static saveErrorCodes(errorCodes: Record<string, Error>): void {
153168
const path = join(module.path, ERROR_CODE_LOCATION);
154169
writeFileSync(path, JSON.stringify(errorCodes, undefined, 4), {encoding: "utf-8", });
155170
}
156171
}
172+
173+
interface Error {
174+
id: number,
175+
message: string
176+
};

packages/firestore/src/util/assert.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,13 @@ import { logError } from './log';
2828
* let futureVar = fail('not implemented yet');
2929
*/
3030
export function fail(
31-
failure: string = 'Unexpected state',
31+
failure: string | number,
3232
context: unknown = undefined
3333
): never {
3434
_fail(failure, context);
3535
}
3636

37-
function _fail(
38-
failure: string = 'Unexpected state',
39-
context: unknown = undefined
40-
): never {
37+
function _fail(failure: string | number, context: unknown = undefined): never {
4138
// Log the failure in addition to throw an exception, just in case the
4239
// exception is swallowed.
4340
let message = `FIRESTORE (${SDK_VERSION}) INTERNAL ASSERTION FAILED: ${failure}`;
@@ -65,7 +62,7 @@ function _fail(
6562
*/
6663
export function hardAssert(
6764
assertion: boolean,
68-
message?: string,
65+
message: string | number,
6966
context?: unknown
7067
): asserts assertion {
7168
if (!assertion) {

packages/firestore/test/unit/index/ordered_code_writer.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ function getBytes(val: unknown): { asc: Uint8Array; desc: Uint8Array } {
248248
ascWriter.writeUtf8Ascending(val);
249249
descWriter.writeUtf8Descending(val);
250250
} else {
251-
hardAssert(val instanceof Uint8Array);
251+
hardAssert(val instanceof Uint8Array, 'val is not instance of Uint8Array', {
252+
val
253+
});
252254
ascWriter.writeBytesAscending(ByteString.fromUint8Array(val));
253255
descWriter.writeBytesDescending(ByteString.fromUint8Array(val));
254256
}

0 commit comments

Comments
 (0)