diff --git a/.changeset/large-lemons-relax.md b/.changeset/large-lemons-relax.md new file mode 100644 index 00000000000..8df076bb896 --- /dev/null +++ b/.changeset/large-lemons-relax.md @@ -0,0 +1,6 @@ +--- +'@firebase/firestore': patch +'firebase': patch +--- + +Internal refactor of platform-specific logic to create TextEncoder and TextDecoder objects. diff --git a/packages/firestore/src/core/firestore_client.ts b/packages/firestore/src/core/firestore_client.ts index 4292ebeb752..ae96b64fa6e 100644 --- a/packages/firestore/src/core/firestore_client.ts +++ b/packages/firestore/src/core/firestore_client.ts @@ -36,7 +36,8 @@ import { DocumentKey } from '../model/document_key'; import { Mutation } from '../model/mutation'; import { ObjectValue } from '../model/object_value'; import { toByteStreamReader } from '../platform/byte_stream_reader'; -import { newSerializer, newTextEncoder } from '../platform/serializer'; +import { newSerializer } from '../platform/serializer'; +import { newTextEncoder } from '../platform/text_serializer'; import { Datastore, invokeRunAggregationQueryRpc } from '../remote/datastore'; import { canUseNetwork, diff --git a/packages/firestore/src/platform/browser/serializer.ts b/packages/firestore/src/platform/browser/serializer.ts index 722f40e605f..5e009d89f60 100644 --- a/packages/firestore/src/platform/browser/serializer.ts +++ b/packages/firestore/src/platform/browser/serializer.ts @@ -22,17 +22,3 @@ import { JsonProtoSerializer } from '../../remote/serializer'; export function newSerializer(databaseId: DatabaseId): JsonProtoSerializer { return new JsonProtoSerializer(databaseId, /* useProto3Json= */ true); } - -/** - * An instance of the Platform's 'TextEncoder' implementation. - */ -export function newTextEncoder(): TextEncoder { - return new TextEncoder(); -} - -/** - * An instance of the Platform's 'TextDecoder' implementation. - */ -export function newTextDecoder(): TextDecoder { - return new TextDecoder('utf-8'); -} diff --git a/packages/firestore/src/platform/browser/text_serializer.ts b/packages/firestore/src/platform/browser/text_serializer.ts new file mode 100644 index 00000000000..6a53021f7c0 --- /dev/null +++ b/packages/firestore/src/platform/browser/text_serializer.ts @@ -0,0 +1,30 @@ +/** + * @license + * Copyright 2023 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. + */ + +/** + * An instance of the Platform's 'TextEncoder' implementation. + */ +export function newTextEncoder(): TextEncoder { + return new TextEncoder(); +} + +/** + * An instance of the Platform's 'TextDecoder' implementation. + */ +export function newTextDecoder(): TextDecoder { + return new TextDecoder('utf-8'); +} diff --git a/packages/firestore/src/platform/browser_lite/text_serializer.ts b/packages/firestore/src/platform/browser_lite/text_serializer.ts new file mode 100644 index 00000000000..a9231f63914 --- /dev/null +++ b/packages/firestore/src/platform/browser_lite/text_serializer.ts @@ -0,0 +1,18 @@ +/** + * @license + * Copyright 2023 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. + */ + +export * from '../browser/text_serializer'; diff --git a/packages/firestore/src/platform/node/serializer.ts b/packages/firestore/src/platform/node/serializer.ts index 1f61010902b..e19dbc8b1db 100644 --- a/packages/firestore/src/platform/node/serializer.ts +++ b/packages/firestore/src/platform/node/serializer.ts @@ -16,25 +16,9 @@ */ /** Return the Platform-specific serializer monitor. */ -import { TextDecoder, TextEncoder } from 'util'; - import { DatabaseId } from '../../core/database_info'; import { JsonProtoSerializer } from '../../remote/serializer'; export function newSerializer(databaseId: DatabaseId): JsonProtoSerializer { return new JsonProtoSerializer(databaseId, /* useProto3Json= */ false); } - -/** - * An instance of the Platform's 'TextEncoder' implementation. - */ -export function newTextEncoder(): TextEncoder { - return new TextEncoder(); -} - -/** - * An instance of the Platform's 'TextDecoder' implementation. - */ -export function newTextDecoder(): TextDecoder { - return new TextDecoder('utf-8'); -} diff --git a/packages/firestore/src/platform/node/text_serializer.ts b/packages/firestore/src/platform/node/text_serializer.ts new file mode 100644 index 00000000000..cc7852e5f4b --- /dev/null +++ b/packages/firestore/src/platform/node/text_serializer.ts @@ -0,0 +1,32 @@ +/** + * @license + * Copyright 2023 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 { TextDecoder, TextEncoder } from 'util'; + +/** + * An instance of the Platform's 'TextEncoder' implementation. + */ +export function newTextEncoder(): TextEncoder { + return new TextEncoder(); +} + +/** + * An instance of the Platform's 'TextDecoder' implementation. + */ +export function newTextDecoder(): TextDecoder { + return new TextDecoder('utf-8'); +} diff --git a/packages/firestore/src/platform/node_lite/text_serializer.ts b/packages/firestore/src/platform/node_lite/text_serializer.ts new file mode 100644 index 00000000000..efc3610847d --- /dev/null +++ b/packages/firestore/src/platform/node_lite/text_serializer.ts @@ -0,0 +1,18 @@ +/** + * @license + * Copyright 2023 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. + */ + +export * from '../browser_lite/text_serializer'; diff --git a/packages/firestore/src/platform/rn/serializer.ts b/packages/firestore/src/platform/rn/serializer.ts index 2b168a0dffa..c5ab7bf2bb5 100644 --- a/packages/firestore/src/platform/rn/serializer.ts +++ b/packages/firestore/src/platform/rn/serializer.ts @@ -15,8 +15,4 @@ * limitations under the License. */ -export { - newSerializer, - newTextEncoder, - newTextDecoder -} from '../browser/serializer'; +export { newSerializer } from '../browser/serializer'; diff --git a/packages/firestore/src/platform/rn/text_serializer.ts b/packages/firestore/src/platform/rn/text_serializer.ts new file mode 100644 index 00000000000..8edf69e42f2 --- /dev/null +++ b/packages/firestore/src/platform/rn/text_serializer.ts @@ -0,0 +1,18 @@ +/** + * @license + * Copyright 2023 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. + */ + +export { newTextEncoder, newTextDecoder } from '../browser/text_serializer'; diff --git a/packages/firestore/src/platform/rn_lite/text_serializer.ts b/packages/firestore/src/platform/rn_lite/text_serializer.ts new file mode 100644 index 00000000000..efc3610847d --- /dev/null +++ b/packages/firestore/src/platform/rn_lite/text_serializer.ts @@ -0,0 +1,18 @@ +/** + * @license + * Copyright 2023 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. + */ + +export * from '../browser_lite/text_serializer'; diff --git a/packages/firestore/src/platform/serializer.ts b/packages/firestore/src/platform/serializer.ts index 89e8fe650d1..47b659f5664 100644 --- a/packages/firestore/src/platform/serializer.ts +++ b/packages/firestore/src/platform/serializer.ts @@ -15,15 +15,9 @@ * limitations under the License. */ -import { isNode, isReactNative } from '@firebase/util'; - import { DatabaseId } from '../core/database_info'; import { JsonProtoSerializer } from '../remote/serializer'; -import * as browser from './browser/serializer'; -import * as node from './node/serializer'; -import * as rn from './rn/serializer'; - // This file is only used under ts-node. // eslint-disable-next-line @typescript-eslint/no-require-imports const platform = require(`./${process.env.TEST_PLATFORM ?? 'node'}/serializer`); @@ -31,29 +25,3 @@ const platform = require(`./${process.env.TEST_PLATFORM ?? 'node'}/serializer`); export function newSerializer(databaseId: DatabaseId): JsonProtoSerializer { return platform.newSerializer(databaseId); } - -/** - * An instance of the Platform's 'TextEncoder' implementation. - */ -export function newTextEncoder(): TextEncoder { - if (isNode()) { - return node.newTextEncoder(); - } else if (isReactNative()) { - return rn.newTextEncoder(); - } else { - return browser.newTextEncoder(); - } -} - -/** - * An instance of the Platform's 'TextDecoder' implementation. - */ -export function newTextDecoder(): TextDecoder { - if (isNode()) { - return node.newTextDecoder() as TextDecoder; - } else if (isReactNative()) { - return rn.newTextDecoder(); - } else { - return browser.newTextDecoder(); - } -} diff --git a/packages/firestore/src/platform/text_serializer.ts b/packages/firestore/src/platform/text_serializer.ts new file mode 100644 index 00000000000..72ebba86c3c --- /dev/null +++ b/packages/firestore/src/platform/text_serializer.ts @@ -0,0 +1,48 @@ +/** + * @license + * Copyright 2023 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 { isNode, isReactNative } from '@firebase/util'; + +import * as browser from './browser/text_serializer'; +import * as node from './node/text_serializer'; +import * as rn from './rn/text_serializer'; + +/** + * An instance of the Platform's 'TextEncoder' implementation. + */ +export function newTextEncoder(): TextEncoder { + if (isNode()) { + return node.newTextEncoder(); + } else if (isReactNative()) { + return rn.newTextEncoder(); + } else { + return browser.newTextEncoder(); + } +} + +/** + * An instance of the Platform's 'TextDecoder' implementation. + */ +export function newTextDecoder(): TextDecoder { + if (isNode()) { + return node.newTextDecoder() as TextDecoder; + } else if (isReactNative()) { + return rn.newTextDecoder(); + } else { + return browser.newTextDecoder(); + } +} diff --git a/packages/firestore/src/util/bundle_reader_impl.ts b/packages/firestore/src/util/bundle_reader_impl.ts index 3f08218d2fc..05875fce1af 100644 --- a/packages/firestore/src/util/bundle_reader_impl.ts +++ b/packages/firestore/src/util/bundle_reader_impl.ts @@ -15,7 +15,7 @@ * limitations under the License. */ -import { newTextDecoder } from '../platform/serializer'; +import { newTextDecoder } from '../platform/text_serializer'; import { BundleMetadata } from '../protos/firestore_bundle_proto'; import { JsonProtoSerializer } from '../remote/serializer'; diff --git a/packages/firestore/test/unit/specs/spec_test_runner.ts b/packages/firestore/test/unit/specs/spec_test_runner.ts index 33bdcc49514..431711efcf2 100644 --- a/packages/firestore/test/unit/specs/spec_test_runner.ts +++ b/packages/firestore/test/unit/specs/spec_test_runner.ts @@ -90,7 +90,7 @@ import { Mutation } from '../../../src/model/mutation'; import { JsonObject } from '../../../src/model/object_value'; import { encodeBase64 } from '../../../src/platform/base64'; import { toByteStreamReader } from '../../../src/platform/byte_stream_reader'; -import { newTextEncoder } from '../../../src/platform/serializer'; +import { newTextEncoder } from '../../../src/platform/text_serializer'; import * as api from '../../../src/protos/firestore_proto_api'; import { ExistenceFilter } from '../../../src/remote/existence_filter'; import { diff --git a/packages/firestore/test/unit/util/bundle.test.ts b/packages/firestore/test/unit/util/bundle.test.ts index 9f225138491..b32ca0842b7 100644 --- a/packages/firestore/test/unit/util/bundle.test.ts +++ b/packages/firestore/test/unit/util/bundle.test.ts @@ -18,7 +18,7 @@ import { expect, use } from 'chai'; import chaiAsPromised from 'chai-as-promised'; import { toByteStreamReader } from '../../../src/platform/byte_stream_reader'; -import { newTextEncoder } from '../../../src/platform/serializer'; +import { newTextEncoder } from '../../../src/platform/text_serializer'; import { BundleReader, SizedBundleElement diff --git a/packages/firestore/test/unit/util/bundle_data.ts b/packages/firestore/test/unit/util/bundle_data.ts index 03bc8b35f4c..92cfb8c30cd 100644 --- a/packages/firestore/test/unit/util/bundle_data.ts +++ b/packages/firestore/test/unit/util/bundle_data.ts @@ -22,10 +22,8 @@ import { queryWithLimit } from '../../../src/core/query'; import { DocumentKey } from '../../../src/model/document_key'; -import { - newSerializer, - newTextEncoder -} from '../../../src/platform/serializer'; +import { newSerializer } from '../../../src/platform/serializer'; +import { newTextEncoder } from '../../../src/platform/text_serializer'; import { BundleElement, LimitType as BundleLimitType