Skip to content

Fix a regression that leads to larger bundle size #2006

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 3 commits into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 0 additions & 1 deletion config/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"moduleResolution": "node",
"sourceMap": true,
"target": "es5",
"downlevelIteration": true,
"typeRoots": [
"../node_modules/@types"
]
Expand Down
3 changes: 2 additions & 1 deletion packages/database/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"extends": "../../config/tsconfig.base.json",
"compilerOptions": {
"outDir": "dist",
"strict": false
"strict": false,
"downlevelIteration": true
},
"exclude": [
"dist/**/*"
Expand Down
61 changes: 40 additions & 21 deletions packages/util/src/crypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,56 +85,77 @@ const byteArrayToString = function(bytes: number[]): string {
return out.join('');
};

interface Base64 {
byteToCharMap_: { [key: number]: string } | null;
charToByteMap_: { [key: string]: number } | null;
byteToCharMapWebSafe_: { [key: number]: string } | null;
charToByteMapWebSafe_: { [key: string]: number } | null;
ENCODED_VALS_BASE: string;
readonly ENCODED_VALS: string;
readonly ENCODED_VALS_WEBSAFE: string;
HAS_NATIVE_SUPPORT: boolean;
encodeByteArray(input: number[] | Uint8Array, webSafe?: boolean): string;
encodeString(input: string, webSafe?: boolean): string;
decodeString(input: string, webSafe: boolean): string;
decodeStringToByteArray(input: string, webSafe: boolean): number[];
init_(): void;
}

// We define it as an object literal instead of a class because a class compiled down to es5 can't
// be treeshaked. https://github.com/rollup/rollup/issues/1691
// Static lookup maps, lazily populated by init_()
class Base64 {
export const base64: Base64 = {
/**
* Maps bytes to characters.
*/
byteToCharMap_: { [key: number]: string } | null = null;
byteToCharMap_: null,

/**
* Maps characters to bytes.
*/
charToByteMap_: { [key: string]: number } | null = null;
charToByteMap_: null,

/**
* Maps bytes to websafe characters.
* @private
*/
byteToCharMapWebSafe_: { [key: number]: string } | null = null;
byteToCharMapWebSafe_: null,

/**
* Maps websafe characters to bytes.
* @private
*/
charToByteMapWebSafe_: { [key: string]: number } | null = null;
charToByteMapWebSafe_: null,

/**
* Our default alphabet shared between
* Our default alphabet, shared between
* ENCODED_VALS and ENCODED_VALS_WEBSAFE
*/
ENCODED_VALS_BASE: string =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'abcdefghijklmnopqrstuvwxyz' + '0123456789';
ENCODED_VALS_BASE:
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'abcdefghijklmnopqrstuvwxyz' + '0123456789',

/**
* Our default alphabet. Value 64 (=) is special; it means "nothing."
*/
get ENCODED_VALS(): string {
get ENCODED_VALS() {
return this.ENCODED_VALS_BASE + '+/=';
}
},

/**
* Our websafe alphabet.
*/
get ENCODED_VALS_WEBSAFE(): string {
get ENCODED_VALS_WEBSAFE() {
return this.ENCODED_VALS_BASE + '-_.';
}
},

/**
* Whether this browser supports the atob and btoa functions. This extension
* started at Mozilla but is now implemented by many browsers. We use the
* ASSUME_* variables to avoid pulling in the full useragent detection library
* but still allowing the standard per-browser compilations.
*
*/
HAS_NATIVE_SUPPORT: boolean = typeof atob === 'function';
HAS_NATIVE_SUPPORT: typeof atob === 'function',

/**
* Base64-encode an array of bytes.
Expand Down Expand Up @@ -187,7 +208,7 @@ class Base64 {
}

return output.join('');
}
},

/**
* Base64-encode a string.
Expand All @@ -204,7 +225,7 @@ class Base64 {
return btoa(input);
}
return this.encodeByteArray(stringToByteArray(input), webSafe);
}
},

/**
* Base64-decode a string.
Expand All @@ -221,7 +242,7 @@ class Base64 {
return atob(input);
}
return byteArrayToString(this.decodeStringToByteArray(input, webSafe));
}
},

/**
* Base64-decode a string.
Expand Down Expand Up @@ -281,14 +302,14 @@ class Base64 {
}

return output;
}
},

/**
* Lazy static initialization function. Called before
* accessing any of the static map variables.
* @private
*/
init_(): void {
init_() {
if (!this.byteToCharMap_) {
this.byteToCharMap_ = {};
this.charToByteMap_ = {};
Expand All @@ -310,7 +331,7 @@ class Base64 {
}
}
}
}
};

/**
* URL-safe base64 encoding
Expand All @@ -337,5 +358,3 @@ export const base64Decode = function(str: string): string | null {
}
return null;
};

export const base64 = new Base64();