From 3eab20ce42458fced42dcd031958b07113322e0a Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Wed, 15 Jan 2020 20:13:12 +0100 Subject: [PATCH] feat(std/node): Added node timers builtin (#3634) --- std/node/README.md | 64 ++++++++++++++++++++++++++++++++++++++++++++-- std/node/module.ts | 2 ++ std/node/timers.ts | 11 ++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 std/node/timers.ts diff --git a/std/node/README.md b/std/node/README.md index 14c245809c..d0d638d059 100644 --- a/std/node/README.md +++ b/std/node/README.md @@ -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 +**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 diff --git a/std/node/module.ts b/std/node/module.ts index 7ed43349f5..599ae23258 100644 --- a/std/node/module.ts +++ b/std/node/module.ts @@ -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 diff --git a/std/node/timers.ts b/std/node/timers.ts new file mode 100644 index 0000000000..b942d5fbc5 --- /dev/null +++ b/std/node/timers.ts @@ -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;