From a75bd9fd8dbb60ce0533b933d6ef681e69963baf Mon Sep 17 00:00:00 2001 From: Maneesh Tewani Date: Wed, 24 Aug 2022 16:36:43 -0700 Subject: [PATCH 1/4] Included experimental support with Deno --- packages/database/src/core/util/util.ts | 14 ++++++-- packages/database/test/deno.test.ts | 35 +++++++++++++++++++ .../emulators/database-emulator.ts | 2 +- 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 packages/database/test/deno.test.ts diff --git a/packages/database/src/core/util/util.ts b/packages/database/src/core/util/util.ts index 4ce187b755c..bef16b4bd44 100644 --- a/packages/database/src/core/util/util.ts +++ b/packages/database/src/core/util/util.ts @@ -618,10 +618,20 @@ export const setTimeoutNonBlocking = function ( time: number ): number | object { const timeout: number | object = setTimeout(fn, time); - // eslint-disable-next-line @typescript-eslint/no-explicit-any - if (typeof timeout === 'object' && (timeout as any)['unref']) { + // @ts-ignore Deno and unrefTimer are only defined in Deno environments. + // Note: at the time of this comment, unrefTimer is under the unstable set of APIs. Run with --unstable to enable the API. + if ( + typeof timeout === 'number' && + typeof Deno !== 'undefined' && + Deno['unrefTimer'] + ) { + // @ts-ignore Deno and unrefTimer are only defined in Deno environments. + Deno.unrefTimer(timeout); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + } else if (typeof timeout === 'object' && (timeout as any)['unref']) { // eslint-disable-next-line @typescript-eslint/no-explicit-any (timeout as any)['unref'](); } + return timeout; }; diff --git a/packages/database/test/deno.test.ts b/packages/database/test/deno.test.ts new file mode 100644 index 00000000000..0d1723573bf --- /dev/null +++ b/packages/database/test/deno.test.ts @@ -0,0 +1,35 @@ +import { expect, use } from 'chai'; +import * as sinon from 'sinon'; +import sinonChai from 'sinon-chai'; + +import { setTimeoutNonBlocking } from '../src/core/util/util'; +use(sinonChai); +describe('Deno tests', () => { + let oldSetTimeout; + beforeEach(() => { + oldSetTimeout = globalThis.setTimeout; + }); + afterEach(() => { + globalThis.setTimeout = oldSetTimeout; + }); + it('should call the deno unrefTimer() if in Deno', () => { + // @ts-ignore override nodejs behavior + global.Deno = { + unrefTimer: sinon.spy() + }; + // @ts-ignore override nodejs behavior + global.setTimeout = () => 1; + setTimeoutNonBlocking(() => {}, 0); + expect(globalThis.Deno.unrefTimer).to.have.been.called; + }); + it('should not call the deno unrefTimer() if not in Deno', () => { + // @ts-ignore override nodejs behavior + global.Deno2 = { + unrefTimer: sinon.spy() + }; + // @ts-ignore override node.js behavior + global.setTimeout = () => 1; + setTimeoutNonBlocking(() => {}, 0); + expect(globalThis.Deno2.unrefTimer).to.not.have.been.called; + }); +}); \ No newline at end of file diff --git a/scripts/emulator-testing/emulators/database-emulator.ts b/scripts/emulator-testing/emulators/database-emulator.ts index 7c9576c4b4c..86079e379e6 100644 --- a/scripts/emulator-testing/emulators/database-emulator.ts +++ b/scripts/emulator-testing/emulators/database-emulator.ts @@ -19,7 +19,7 @@ import * as request from 'request'; import { Emulator } from './emulator'; -import * as rulesJSON from '../../../config/database.rules.json'; +import rulesJSON from '../../../config/database.rules.json'; export class DatabaseEmulator extends Emulator { namespace: string; From dece935d7e204f0a54083298a0f610088d01bd97 Mon Sep 17 00:00:00 2001 From: Maneesh Tewani Date: Wed, 24 Aug 2022 16:40:51 -0700 Subject: [PATCH 2/4] Create good-bears-obey.md --- .changeset/good-bears-obey.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/good-bears-obey.md diff --git a/.changeset/good-bears-obey.md b/.changeset/good-bears-obey.md new file mode 100644 index 00000000000..e3295293d58 --- /dev/null +++ b/.changeset/good-bears-obey.md @@ -0,0 +1,5 @@ +--- +"@firebase/database": patch +--- + +Included experimental support for Deno From 3d58e06189f1483ec9d6dbe34efd958d13bb380e Mon Sep 17 00:00:00 2001 From: Maneesh Tewani Date: Wed, 24 Aug 2022 16:45:33 -0700 Subject: [PATCH 3/4] Included formatting --- packages/database/test/deno.test.ts | 73 ++++++++++++++++++----------- 1 file changed, 45 insertions(+), 28 deletions(-) diff --git a/packages/database/test/deno.test.ts b/packages/database/test/deno.test.ts index 0d1723573bf..5a88f74feff 100644 --- a/packages/database/test/deno.test.ts +++ b/packages/database/test/deno.test.ts @@ -1,3 +1,20 @@ +/** + * @license + * Copyright 2022 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, use } from 'chai'; import * as sinon from 'sinon'; import sinonChai from 'sinon-chai'; @@ -5,31 +22,31 @@ import sinonChai from 'sinon-chai'; import { setTimeoutNonBlocking } from '../src/core/util/util'; use(sinonChai); describe('Deno tests', () => { - let oldSetTimeout; - beforeEach(() => { - oldSetTimeout = globalThis.setTimeout; - }); - afterEach(() => { - globalThis.setTimeout = oldSetTimeout; - }); - it('should call the deno unrefTimer() if in Deno', () => { - // @ts-ignore override nodejs behavior - global.Deno = { - unrefTimer: sinon.spy() - }; - // @ts-ignore override nodejs behavior - global.setTimeout = () => 1; - setTimeoutNonBlocking(() => {}, 0); - expect(globalThis.Deno.unrefTimer).to.have.been.called; - }); - it('should not call the deno unrefTimer() if not in Deno', () => { - // @ts-ignore override nodejs behavior - global.Deno2 = { - unrefTimer: sinon.spy() - }; - // @ts-ignore override node.js behavior - global.setTimeout = () => 1; - setTimeoutNonBlocking(() => {}, 0); - expect(globalThis.Deno2.unrefTimer).to.not.have.been.called; - }); -}); \ No newline at end of file + let oldSetTimeout; + beforeEach(() => { + oldSetTimeout = globalThis.setTimeout; + }); + afterEach(() => { + globalThis.setTimeout = oldSetTimeout; + }); + it('should call the deno unrefTimer() if in Deno', () => { + // @ts-ignore override nodejs behavior + global.Deno = { + unrefTimer: sinon.spy() + }; + // @ts-ignore override nodejs behavior + global.setTimeout = () => 1; + setTimeoutNonBlocking(() => {}, 0); + expect(globalThis.Deno.unrefTimer).to.have.been.called; + }); + it('should not call the deno unrefTimer() if not in Deno', () => { + // @ts-ignore override nodejs behavior + global.Deno2 = { + unrefTimer: sinon.spy() + }; + // @ts-ignore override node.js behavior + global.setTimeout = () => 1; + setTimeoutNonBlocking(() => {}, 0); + expect(globalThis.Deno2.unrefTimer).to.not.have.been.called; + }); +}); From b7196004cb7c337bf90a74c753cfabd237d7c605 Mon Sep 17 00:00:00 2001 From: Maneesh Tewani Date: Wed, 24 Aug 2022 16:52:20 -0700 Subject: [PATCH 4/4] Fixed TS errors --- packages/database/src/core/util/util.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/database/src/core/util/util.ts b/packages/database/src/core/util/util.ts index bef16b4bd44..09c37bafe24 100644 --- a/packages/database/src/core/util/util.ts +++ b/packages/database/src/core/util/util.ts @@ -618,11 +618,12 @@ export const setTimeoutNonBlocking = function ( time: number ): number | object { const timeout: number | object = setTimeout(fn, time); - // @ts-ignore Deno and unrefTimer are only defined in Deno environments. // Note: at the time of this comment, unrefTimer is under the unstable set of APIs. Run with --unstable to enable the API. if ( typeof timeout === 'number' && + // @ts-ignore Is only defined in Deno environments. typeof Deno !== 'undefined' && + // @ts-ignore Deno and unrefTimer are only defined in Deno environments. Deno['unrefTimer'] ) { // @ts-ignore Deno and unrefTimer are only defined in Deno environments.