Skip to content

Commit f355335

Browse files
authored
Included experimental support for Deno (#6560)
Fixed issue where Deno would hang after attempting to terminate.
1 parent 474025c commit f355335

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-3
lines changed

.changeset/good-bears-obey.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@firebase/database": patch
3+
---
4+
5+
Included experimental support for Deno

packages/database/src/core/util/util.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -618,10 +618,21 @@ export const setTimeoutNonBlocking = function (
618618
time: number
619619
): number | object {
620620
const timeout: number | object = setTimeout(fn, time);
621-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
622-
if (typeof timeout === 'object' && (timeout as any)['unref']) {
621+
// Note: at the time of this comment, unrefTimer is under the unstable set of APIs. Run with --unstable to enable the API.
622+
if (
623+
typeof timeout === 'number' &&
624+
// @ts-ignore Is only defined in Deno environments.
625+
typeof Deno !== 'undefined' &&
626+
// @ts-ignore Deno and unrefTimer are only defined in Deno environments.
627+
Deno['unrefTimer']
628+
) {
629+
// @ts-ignore Deno and unrefTimer are only defined in Deno environments.
630+
Deno.unrefTimer(timeout);
631+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
632+
} else if (typeof timeout === 'object' && (timeout as any)['unref']) {
623633
// eslint-disable-next-line @typescript-eslint/no-explicit-any
624634
(timeout as any)['unref']();
625635
}
636+
626637
return timeout;
627638
};

packages/database/test/deno.test.ts

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @license
3+
* Copyright 2022 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { expect, use } from 'chai';
19+
import * as sinon from 'sinon';
20+
import sinonChai from 'sinon-chai';
21+
22+
import { setTimeoutNonBlocking } from '../src/core/util/util';
23+
use(sinonChai);
24+
describe('Deno tests', () => {
25+
let oldSetTimeout;
26+
beforeEach(() => {
27+
oldSetTimeout = globalThis.setTimeout;
28+
});
29+
afterEach(() => {
30+
globalThis.setTimeout = oldSetTimeout;
31+
});
32+
it('should call the deno unrefTimer() if in Deno', () => {
33+
// @ts-ignore override nodejs behavior
34+
global.Deno = {
35+
unrefTimer: sinon.spy()
36+
};
37+
// @ts-ignore override nodejs behavior
38+
global.setTimeout = () => 1;
39+
setTimeoutNonBlocking(() => {}, 0);
40+
expect(globalThis.Deno.unrefTimer).to.have.been.called;
41+
});
42+
it('should not call the deno unrefTimer() if not in Deno', () => {
43+
// @ts-ignore override nodejs behavior
44+
global.Deno2 = {
45+
unrefTimer: sinon.spy()
46+
};
47+
// @ts-ignore override node.js behavior
48+
global.setTimeout = () => 1;
49+
setTimeoutNonBlocking(() => {}, 0);
50+
expect(globalThis.Deno2.unrefTimer).to.not.have.been.called;
51+
});
52+
});

scripts/emulator-testing/emulators/database-emulator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import * as request from 'request';
1919

2020
import { Emulator } from './emulator';
2121

22-
import * as rulesJSON from '../../../config/database.rules.json';
22+
import rulesJSON from '../../../config/database.rules.json';
2323

2424
export class DatabaseEmulator extends Emulator {
2525
namespace: string;

0 commit comments

Comments
 (0)