Skip to content

Commit

Permalink
Included experimental support for Deno (#6560)
Browse files Browse the repository at this point in the history
Fixed issue where Deno would hang after attempting to terminate.
  • Loading branch information
maneesht authored Aug 25, 2022
1 parent 474025c commit f355335
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-bears-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/database": patch
---

Included experimental support for Deno
15 changes: 13 additions & 2 deletions packages/database/src/core/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,10 +618,21 @@ 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']) {
// 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.
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;
};
52 changes: 52 additions & 0 deletions packages/database/test/deno.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @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';

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;
});
});
2 changes: 1 addition & 1 deletion scripts/emulator-testing/emulators/database-emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f355335

Please sign in to comment.