All Projects → justjavac → node2deno

justjavac / node2deno

Licence: MIT license
Deno for Node.js developers

Projects that are alternatives of or similar to node2deno

deno cheerio
How to use cheerio in Deno
Stars: ✭ 23 (+21.05%)
Mutual labels:  deno
deno serverless aliyun
为阿里云 serverless 平台添加 Deno Runtime
Stars: ✭ 60 (+215.79%)
Mutual labels:  deno
justjavac
justjavac's modules
Stars: ✭ 15 (-21.05%)
Mutual labels:  deno
garn-validator
Create validations with ease
Stars: ✭ 42 (+121.05%)
Mutual labels:  deno
svelte
Svelte compiler ported to Deno
Stars: ✭ 71 (+273.68%)
Mutual labels:  deno
sinco
Browser Automation and Testing Tool for Deno, written in full TypeScript
Stars: ✭ 54 (+184.21%)
Mutual labels:  deno
dotland
deno.land website
Stars: ✭ 947 (+4884.21%)
Mutual labels:  deno
reno
A thin, testable routing library designed to sit on top of Deno's standard HTTP module
Stars: ✭ 127 (+568.42%)
Mutual labels:  deno
Deno-news-cli
A deno based CLI app to show quick news at your terminal
Stars: ✭ 18 (-5.26%)
Mutual labels:  deno
kyuko
Fast and easy http framework for Deno Deploy 🦕
Stars: ✭ 18 (-5.26%)
Mutual labels:  deno
shell
A very lightweight framework for building shell/CLI applications. Works in Node.js, Deno, and the browser.
Stars: ✭ 63 (+231.58%)
Mutual labels:  deno
compress
compress and uncompress for Deno
Stars: ✭ 29 (+52.63%)
Mutual labels:  deno
deno math
Deno module for high-precision calculations and scientific computing
Stars: ✭ 21 (+10.53%)
Mutual labels:  deno
kafkaSaur
Apache Kafka client for Deno
Stars: ✭ 42 (+121.05%)
Mutual labels:  deno
deno-mongo-api
Example for building REST APIS with deno and MongoDB
Stars: ✭ 17 (-10.53%)
Mutual labels:  deno
zeno.zsh
zsh fuzzy completion and utility plugin with Deno.
Stars: ✭ 119 (+526.32%)
Mutual labels:  deno
croner
Trigger functions and/or evaluate cron expressions in JavaScript. No dependencies. Most features. All environments.
Stars: ✭ 169 (+789.47%)
Mutual labels:  deno
cotton
SQL Database Toolkit for Deno
Stars: ✭ 129 (+578.95%)
Mutual labels:  deno
sqlite3
The fastest and correct module for SQLite3 in Deno.
Stars: ✭ 143 (+652.63%)
Mutual labels:  deno
AloeDB
Light, Embeddable, NoSQL database for Deno 🦕
Stars: ✭ 111 (+484.21%)
Mutual labels:  deno

From Node.js to Deno

Deno for Node.js developers.

Console

Class Console

// Node
const { Console } = console;
new Console({ stdout: process.stdout, stderr: process.stderror });

// Deno
const { Console } = console;
// undefined

// Chrome
const { Console } = console;
// undefined

console.error

// Node
console.error(new Error("Whoops, something bad happened"));
// Error: Whoops, something bad happened
//     at REPL1:1:15
//     at Script.runInThisContext (vm.js:133:18)
//     at REPLServer.defaultEval (repl.js:484:29)
//     at bound (domain.js:413:15)
//     at REPLServer.runBound [as eval] (domain.js:424:12)
//     at REPLServer.onLine (repl.js:817:10)
//     at REPLServer.emit (events.js:327:22)
//     at REPLServer.EventEmitter.emit (domain.js:467:12)
//     at REPLServer.Interface._onLine (readline.js:337:10)
//     at REPLServer.Interface._line (readline.js:666:8)

// Deno
console.error(new Error("Whoops, something bad happened"));
// Error: Whoops, something bad happened
//     at <anonymous>:2:15

// Chrome
console.error(new Error("Whoops, something bad happened"));
// Error: Whoops, something bad happened
//     at <anonymous>:1:15

console.trace

// Node
console.trace("Show me");
// Trace: Show me
//     at REPL14:1:9
//     at Script.runInThisContext (vm.js:133:18)
//     at REPLServer.defaultEval (repl.js:484:29)
//     at bound (domain.js:413:15)
//     at REPLServer.runBound [as eval] (domain.js:424:12)
//     at REPLServer.onLine (repl.js:817:10)
//     at REPLServer.emit (events.js:327:22)
//     at REPLServer.EventEmitter.emit (domain.js:467:12)
//     at REPLServer.Interface._onLine (readline.js:337:10)
//     at REPLServer.Interface._line (readline.js:666:8)

// Deno
console.trace("Show me");
// Trace: Show me
//     at <anonymous>:2:9

// Chrome
console.trace("Show me");
// Show me
// (anonymous) @ VM45:1

File system

Reading file

// Node
import fs from "fs/promises";
const data = await fs.readFile("./README.md", "utf8");

// Deno
// Requires `--allow-read` permission
const data = await Deno.readTextFile("./README.md");

Writing file

// Node
import fs from "fs/promises";
const content = "Some content!";
await fs.writeFile("./file.txt", content);

// Deno
// Requires `--allow-write` permission
const content = "Some content!";
await Deno.writeTextFile("./file.txt", content);

File descriptors

// Node
import fs from "fs/promises";
const file = await fs.open("./file.txt");

// Deno
// Requires `allow-read` and/or `allow-write` permissions depending on options.
const file = await Deno.open("./file.txt");

Copying file

// Node
import fs from "fs/promises";
await fs.copyFile('source.txt', 'target.txt');

// Deno
// Requires `allow-read` and/or `allow-write` permissions depending on options.
import { copy } from "https://deno.land/[email protected]/fs/mod.ts";
await copy('./source.txt', './target.txt');

Moveing file

// Node
import fs from "fs/promises";
await fs.rename('source.txt', 'target.txt');

// Deno
// Requires `allow-read` and/or `allow-write` permissions depending on options.
import { move } from "https://deno.land/[email protected]/fs/mod.ts";
await move('./source.txt', './target.txt');

Path

Last portion of a path

// Node
import path from "path";
path.basename("/foo/bar/baz/asdf/quux.html");
// -> 'quux.html'

// Deno
import * as path from "https://deno.land/std/path/mod.ts";
path.basename("/foo/bar/baz/asdf/quux.html");
// -> 'quux.html'

Or with file extensions:

// Node
import path from "path";
path.basename("/foo/bar/baz/asdf/quux.html", ".html");
// -> 'quux'

// Deno
import * as path from "https://deno.land/std/path/mod.ts";
path.basename("/foo/bar/baz/asdf/quux.html", ".html");
// -> 'quux'

Directory name of a path

// Node
import path from "path";
path.dirname("/foo/bar/baz/asdf/quux");
// -> '/foo/bar/baz/asdf'

// Deno
import * as path from "https://deno.land/std/path/mod.ts";
path.dirname("/foo/bar/baz/asdf/quux");
// -> '/foo/bar/baz/asdf'

Extension of the path

// Node
import path from "path";
path.extname("index.html");
// -> '.html'

// Deno
import * as path from "https://deno.land/std/path/mod.ts";
path.extname("index.html");
// -> '.html'

Join all path segments together

// Node
import path from "path";
path.join("/foo", "bar", "baz/asdf", "quux", "..");
// -> '/foo/bar/baz/asdf'

// Deno
import * as path from "https://deno.land/std/path/mod.ts";
path.join("/foo", "bar", "baz/asdf", "quux", "..");
// -> '/foo/bar/baz/asdf'

Resolves path segments into an absolute path.

// Node
import path from "path";
path.resolve('/foo/bar', './baz');
// -> '/foo/bar/baz'

// Deno
import * as path from "https://deno.land/std/path/mod.ts";
path.resolve('/foo/bar', './baz');
// -> '/foo/bar/baz'

Normalizes a path

// Node
import path from "path";
path.normalize("/foo/bar//baz/asdf/quux/..");
// -> '/foo/bar/baz/asdf'

// Deno
import * as path from "https://deno.land/std/path/mod.ts";
path.normalize("/foo/bar//baz/asdf/quux/..");
// -> '/foo/bar/baz/asdf'

Parse a path

// Node
import path from "path";
path.parse("/home/user/dir/file.txt");
// -> { root: "/", dir: "/home/user/dir", base: "file.txt", ext: ".txt", name: "file" }

// Deno
import * as path from "https://deno.land/std/path/mod.ts";
path.parse("/home/user/dir/file.txt");
// -> { root: "/", dir: "/home/user/dir", base: "file.txt", ext: ".txt", name: "file" }

Subprocess

TODO: needs improvement

Subprocess in a short story

// Node
// All functionalities associated to subprocess are under `child_process` module
const child_process = require("child_process");
// Spawn a subprocess
const p = child_process.spawn("ping", ["-h"]);
// OR buffered version `child_process.exec`
// OR synced version `child_process.spawnSync`
// redirect stdout
p.stdout.on("data", (data) => console.log(data));
// redirect stderr
p.stderr.on("data", (error) => console.log(error));
// Subprocess Close event
p.on("close", (code) => console.log(`child process exited with code ${code}`));

// Here's how we can spawn a *BRAND NEW* nodejs instance to run a js "module"
child_process.fork("hello.js");

// Final output:
//  <Buffer d1 a1 cf ee 20 2d 68 20 b2 bb d5 fd c8 b7 a1 a3 0d 0a 0d 0a d3 c3 b7 a8 3a 20 70 69 6e 67 20 5b 2d 74 5d 20 5b 2d 61 5d 20 5b 2d 6e 20 63 6f 75 6e 74 ... 1447 more bytes>
//  child process exited with code 1
//  hello from hello.js

// Deno
// Deno uses `Deno.run` to spawn a subprocess
// `allow-run` permission *REQUIRED*
const p = Deno.run({
  cmd: ["ping", "-h"],
  stdout: "piped",
  stderr: "piped",
});

// HERE, we read out the stdout output that we set to piped before
console.log(await p.output());
// and of course the stderr
console.log(await p.stderrOutput());

// In Deno, we don't have a fork like function,
// instead you can still use `Deno.run` to spawn
// a `BRAND NEW` Deno instance to run the script
const decoder = new TextDecoder();
const forkLike = Deno.run({
  cmd: ["deno", "run", "hello.js"],
  stdout: "piped",
});
console.log(decoder.decode(await forkLike.output())); // hello from hello.js
// OR in a IMO better way: `Web workers`
new Worker(
  new URL("./hello.js", import.meta.url).href,
  { type: "module" },
); // hello from hello.js

// Final output:
//   Uint8Array(1497) [
//    209, 161, 207, 238,  32,  45, 104,  32, 178, 187, 213, 253, 200, 183, 161,
//    163,  13,  10,  13,  10, 211, 195, 183, 168,  58,  32, 112, 105, 110, 103,
//     32,  91,  45, 116,  93,  32,  91,  45,  97,  93,  32,  91,  45, 110,  32,
//     99, 111, 117, 110, 116,  93,  32,  91,  45, 108,  32, 115, 105, 122, 101,
//     93,  32,  91,  45, 102,  93,  32,  91,  45, 105,  32,  84,  84,  76,  93,
//     32,  91,  45, 118,  32,  84,  79,  83,  93,  13,  10,  32,  32,  32,  32,
//     32,  32,  32,  32,  32,  32,  32,  32,  91,  45,
//    ... 1397 more items
//  ]
//  Uint8Array(0) []
//  hello from hello.js

//  hello from hello.js
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].