feat(std/node): Added node timers builtin (#3634)

This commit is contained in:
Luca Casonato 2020-01-15 20:13:12 +01:00 committed by Ry Dahl
parent b310f32e39
commit 3eab20ce42
3 changed files with 75 additions and 2 deletions

View file

@ -1,11 +1,71 @@
# Deno Node compatibility
This module is meant to have a compatibility layer for the
[nodeJS standard library](https://nodejs.org/docs/latest-v12.x/api/).
[NodeJS standard library](https://nodejs.org/docs/latest-v12.x/api/).
**Warning**: Any function of this module should not be referred anywhere in the
deno standard library as it's a compatiblity module.
## Supported Builtins
- [ ] assert
- [ ] buffer
- [ ] child_process
- [ ] cluster
- [ ] console
- [ ] crypto
- [ ] dgram
- [ ] dns
- [ ] events
- [x] fs _partly_
- [ ] http
- [ ] http2
- [ ] https
- [x] module
- [ ] net
- [ ] os
- [x] path
- [ ] perf_hooks
- [x] process _partly_
- [ ] querystring
- [ ] readline
- [ ] repl
- [ ] stream
- [ ] string_decoder
- [ ] sys
- [x] timers
- [ ] tls
- [ ] tty
- [ ] url
- [x] util _partly_
- [ ] ~~v8~~ _can't implement_
- [ ] vm
- [ ] worker_threads
- [ ] zlib
* [x] node globals _partly_
### Deprecated
These builtins are deprecated in NodeJS v13 and will probably not be polyfilled:
- constants
- domain
- freelist
- punycode
### Experimental
These builtins are experimental in NodeJS v13 and will not be polyfilled until
they are stable:
- async_hooks
- inspector
- policies
- report
- trace_events
- wasi
## CommonJS Module Loading
`createRequire(...)` is provided to create a `require` function for loading CJS

View file

@ -24,6 +24,7 @@ import "./global.ts";
import * as nodeFS from "./fs.ts";
import * as nodeUtil from "./util.ts";
import * as nodePath from "./path.ts";
import * as nodeTimers from "./timers.ts";
import * as path from "../path/mod.ts";
import { assert } from "../testing/asserts.ts";
@ -580,6 +581,7 @@ function createNativeModule(id: string, exports: any): Module {
nativeModulePolyfill.set("fs", createNativeModule("fs", nodeFS));
nativeModulePolyfill.set("util", createNativeModule("util", nodeUtil));
nativeModulePolyfill.set("path", createNativeModule("path", nodePath));
nativeModulePolyfill.set("timers", createNativeModule("timers", nodeTimers));
function loadNativeModule(
_filename: string,
request: string

11
std/node/timers.ts Normal file
View file

@ -0,0 +1,11 @@
// TODO: implement the 'NodeJS.Timeout' and 'NodeJS.Immediate' versions of the timers.
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/1163ead296d84e7a3c80d71e7c81ecbd1a130e9a/types/node/v12/globals.d.ts#L1120-L1131
export const setTimeout = window.setTimeout;
export const clearTimeout = window.clearTimeout;
export const setInterval = window.setInterval;
export const clearInterval = window.clearInterval;
export const setImmediate = (
cb: (...args: unknown[]) => void,
...args: unknown[]
): number => window.setTimeout(cb, 0, ...args);
export const clearImmediate = window.clearTimeout;