Skip to content
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

Add ReactNative build back, fix base64 serialization #3251

Merged
merged 9 commits into from
Jun 30, 2020
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
6 changes: 6 additions & 0 deletions .changeset/shy-forks-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"firebase": minor
"@firebase/firestore": minor
---

Re-adding the ReactNative bundle, which allows Firestore to be used without `btoa`/`atob` Polyfills.
1 change: 1 addition & 0 deletions packages/firebase/src/index.rn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import '../database';
// TODO(b/158625454): Storage doesn't actually work by default in RN (it uses
// `atob`). We should provide a RN build that works out of the box.
import '../storage';
import '../firestore';

firebase.registerVersion(name, version, 'rn');

Expand Down
1 change: 1 addition & 0 deletions packages/firestore/externs.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"externs" : [
"node_modules/@types/node/base.d.ts",
"node_modules/@types/node/globals.d.ts",
"node_modules/typescript/lib/lib.es5.d.ts",
"node_modules/typescript/lib/lib.dom.d.ts",
"node_modules/typescript/lib/lib.es2015.promise.d.ts",
Expand Down
1 change: 1 addition & 0 deletions packages/firestore/memory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"description": "A memory-only build of the Cloud Firestore JS SDK.",
"main": "../dist/index.memory.node.cjs.js",
"main-esm2017": "../dist/index.memory.node.esm2017.js",
"react-native": "../dist/index.memory.rn.esm2017.js",
"browser": "../dist/index.memory.cjs.js",
"module": "../dist/index.memory.esm.js",
"esm2017": "../dist/index.memory.esm2017.js",
Expand Down
1 change: 1 addition & 0 deletions packages/firestore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
},
"main": "dist/index.node.cjs.js",
"main-esm2017": "dist/index.node.esm2017.js",
"react-native": "dist/index.rn.esm2017.js",
"browser": "dist/index.cjs.js",
"module": "dist/index.esm.js",
"esm2017": "dist/index.esm2017.js",
Expand Down
32 changes: 31 additions & 1 deletion packages/firestore/rollup.config.es2017.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,36 @@ const browserBuilds = [
}
];

const reactNativeBuildPlugins = [
alias(generateAliasConfig('rn')),
...browserBuildPlugins.slice(1)
];

const reactNativeBuilds = [
// Persistence build
{
input: 'index.rn.ts',
output: {
file: pkg['react-native'],
format: 'es',
sourcemap: true
},
plugins: reactNativeBuildPlugins,
external: resolveBrowserExterns
},
// Memory-only build
{
input: 'index.rn.memory.ts',
output: {
file: path.resolve('./memory', memoryPkg['react-native']),
format: 'es',
sourcemap: true
},
plugins: reactNativeBuildPlugins,
external: resolveBrowserExterns
}
];

// MARK: Node builds

const nodeBuildPlugins = [
Expand Down Expand Up @@ -149,4 +179,4 @@ const nodeBuilds = [
}
];

export default [...browserBuilds, ...nodeBuilds];
export default [...browserBuilds, ...reactNativeBuilds, ...nodeBuilds];
2 changes: 1 addition & 1 deletion packages/firestore/rollup.config.es5.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2018 Google Inc.
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
22 changes: 16 additions & 6 deletions packages/firestore/src/platform/rn/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,28 @@

import { base64 } from '@firebase/util';

// WebSafe uses a different URL-encoding safe alphabet that doesn't match
// the encoding used on the backend.
const WEB_SAFE = false;

/** Converts a Base64 encoded string to a binary string. */
export function decodeBase64(encoded: string): string {
// WebSafe uses a different URL-encoding safe alphabet that doesn't match
// the encoding used on the backend.
return base64.decodeString(encoded, /* webSafe =*/ false);
return String.fromCharCode.apply(
null,
// We use `decodeStringToByteArray()` instead of `decodeString()` since
// `decodeString()` returns Unicode strings, which doesn't match the values
// returned by `atob()`'s Latin1 representation.
base64.decodeStringToByteArray(encoded, WEB_SAFE)
);
}

/** Converts a binary string to a Base64 encoded string. */
export function encodeBase64(raw: string): string {
// WebSafe uses a different URL-encoding safe alphabet that doesn't match
// the encoding used on the backend.
return base64.encodeString(raw, /* webSafe =*/ false);
const bytes: number[] = [];
for (let i = 0; i < raw.length; i++) {
bytes[i] = raw.charCodeAt(i);
}
return base64.encodeByteArray(bytes, WEB_SAFE);
}

/** True if and only if the Base64 conversion functions are available. */
Expand Down
83 changes: 83 additions & 0 deletions packages/firestore/test/unit/util/base64.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { expect } from 'chai';

import * as rn from '../../../src/platform/rn/base64';

const BASE64_ENCODED = 'GRBoQgKB9LW1';
const BASE64_DECODED = '\u0019\u0010\u0068\u0042\u0002\u0081\u00f4\u00b5\u00b5';

describe('atob', () => {
// eslint-disable-next-line no-restricted-properties
(typeof atob !== 'undefined' ? it : it.skip)(
'decodes with native support',
() => {
const decoded = atob(BASE64_ENCODED);
expect(decoded).to.equal(BASE64_DECODED);
}
);

// eslint-disable-next-line no-restricted-properties
(typeof atob !== 'undefined' ? it : it.skip)(
'roundtrips with native support',
() => {
expect(atob(btoa(BASE64_ENCODED))).to.equal(BASE64_ENCODED);
}
);

it('decodes with polyfill', () => {
const decoded = rn.decodeBase64(BASE64_ENCODED);
expect(decoded).to.equal(BASE64_DECODED);
});

it('roundtrips with polyfill', () => {
expect(rn.encodeBase64(rn.decodeBase64(BASE64_ENCODED))).to.equal(
BASE64_ENCODED
);
});
});

describe('btoa', () => {
// eslint-disable-next-line no-restricted-properties
(typeof btoa !== 'undefined' ? it : it.skip)(
'encodes with native support',
() => {
const encoded = btoa(BASE64_DECODED);
expect(encoded).to.equal(BASE64_ENCODED);
}
);

// eslint-disable-next-line no-restricted-properties
(typeof btoa !== 'undefined' ? it : it.skip)(
'roundtrips with native support',
() => {
expect(atob(btoa(BASE64_DECODED))).to.equal(BASE64_DECODED);
}
);

it('encodes with polyfill', () => {
const encoded = rn.encodeBase64(BASE64_DECODED);
expect(encoded).to.equal(BASE64_ENCODED);
});

it('roundtrips with polyfill', () => {
expect(rn.decodeBase64(rn.encodeBase64(BASE64_DECODED))).to.equal(
BASE64_DECODED
);
});
});