Coder Social home page Coder Social logo

denoland / node_deno_shims Goto Github PK

View Code? Open in Web Editor NEW
141.0 18.0 27.0 545 KB

npm packages providing shims for the Deno namespace and other globals. Useful for running Deno-first programs on Node.

License: MIT License

TypeScript 92.56% Shell 0.07% JavaScript 7.13% Dockerfile 0.24%

node_deno_shims's People

Contributors

bartlomieju avatar darvesh avatar denizdogan avatar dsherret avatar halvardssm avatar hyp3rflow avatar jonbcard avatar knorpelsenf avatar kt3k avatar leodog896 avatar mkrhere avatar sno2 avatar tiloio avatar trgwii avatar ultirequiem avatar uncenter avatar urielch avatar wojpawlik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node_deno_shims's Issues

Implement Deno.run

I have most of the implementation done for this, but it's not tested so I'm not sure if it's going to work yet.

Missing resolveDns

Hi I know that resolveDns is not checked in progress

My first deno project is block issue.

Any work in progress for this function ?

Or Should I try to open a PR ?

Add setup instructions

It would be useful to create a list of all the commands someone needs to run to clone and setup this project.

crypto shim missing `randomUUID`

Hey there!
First of all thanks for dnt, I'm so glad I don't have to wrangle all the different tools for nodejs anymore and can just use deno and be happy.

I'm using crypto.randomUUID in my tests and want to run those automatically after building with dnt, so I have added the crypto shim, but randomUUID is missing in the interface declaration here
Is there a reason for that? If not I'd like to add it.

Cheers,
Andreas

Split up non-deno namespace shims into separate packages

This library requires depending on undici to wrap fetch, blob, file, formdata, headers, request, response, but it would be better if these shims were moved to separate packages to eliminate this dependency when not necessary.

Do not require dev dependency on @types/node

Similar to #40, the types should be constructed in such a way so dependents don't need a dev dependency on @types/node.

Current diagnostics:

npm/node_modules/deno.ns/dist/deno/stable/functions.d.ts:1:24 - error TS2307: Cannot find module 'tty' or its corresponding type declarations.

1 export { isatty } from "tty";
                         ~~~~~
npm/node_modules/deno.ns/dist/deno/stable/functions/run.d.ts:2:23 - error TS2688: Cannot find type definition file for 'node'.

2 /// <reference types="node" />
                        ~~~~
npm/node_modules/deno.ns/dist/deno/stable/functions/run.d.ts:3:26 - error TS2307: Cannot find module 'child_process' or its corresponding type declarations.

3 import childProcess from "child_process";
                           ~~~~~~~~~~~~~~~
npm/node_modules/deno.ns/dist/deno/stable/functions/stat.d.ts:2:23 - error TS2688: Cannot find type definition file for 'node'.

2 /// <reference types="node" />
                        ~~~~
npm/node_modules/deno.ns/dist/deno/stable/functions/stat.d.ts:3:28 - error TS2307: Cannot find module 'fs' or its corresponding type declarations.

3 import type { Stats } from "fs";
                             ~~~~
npm/node_modules/deno.ns/dist/index.d.ts:1:22 - error TS2307: Cannot find module 'buffer' or its corresponding type declarations.

1 export { Blob } from "buffer";
                       ~~~~~~~~
npm/node_modules/deno.ns/dist/index.d.ts:2:37 - error TS2307: Cannot find module 'crypto' or its corresponding type declarations.

2 export { webcrypto as crypto } from "crypto";
                                      ~~~~~~~~
npm/node_modules/undici/types/agent.d.ts:1:21 - error TS2307: Cannot find module 'url' or its corresponding type declarations.

1 import { URL } from 'url'
                      ~~~~~
npm/node_modules/undici/types/api.d.ts:1:32 - error TS2307: Cannot find module 'url' or its corresponding type declarations.

1 import { URL, UrlObject } from 'url'
                                 ~~~~~
npm/node_modules/undici/types/api.d.ts:2:24 - error TS2307: Cannot find module 'stream' or its corresponding type declarations.

2 import { Duplex } from 'stream'
                         ~~~~~~~~
npm/node_modules/undici/types/balanced-pool.d.ts:4:21 - error TS2307: Cannot find module 'url' or its corresponding type declarations.

4 import { URL } from 'url'
                      ~~~~~
npm/node_modules/undici/types/client.d.ts:1:21 - error TS2307: Cannot find module 'url' or its corresponding type declarations.

1 import { URL } from 'url'
                      ~~~~~
npm/node_modules/undici/types/client.d.ts:2:28 - error TS2307: Cannot find module 'tls' or its corresponding type declarations.

2 import { TlsOptions } from 'tls'
                             ~~~~~
npm/node_modules/undici/types/connector.d.ts:1:21 - error TS2307: Cannot find module 'url' or its corresponding type declarations.

1 import { URL } from 'url'
                      ~~~~~
npm/node_modules/undici/types/connector.d.ts:2:39 - error TS2307: Cannot find module 'tls' or its corresponding type declarations.

2 import { TLSSocket, TlsOptions } from 'tls'
                                        ~~~~~
npm/node_modules/undici/types/connector.d.ts:3:24 - error TS2307: Cannot find module 'net' or its corresponding type declarations.

3 import { Socket } from 'net'
                         ~~~~~
npm/node_modules/undici/types/dispatcher.d.ts:1:21 - error TS2307: Cannot find module 'url' or its corresponding type declarations.

1 import { URL } from 'url'
                      ~~~~~
npm/node_modules/undici/types/dispatcher.d.ts:2:44 - error TS2307: Cannot find module 'stream' or its corresponding type declarations.

2 import { Duplex, Readable, Writable } from 'stream'
                                             ~~~~~~~~
npm/node_modules/undici/types/dispatcher.d.ts:3:30 - error TS2307: Cannot find module 'events' or its corresponding type declarations.

3 import { EventEmitter } from 'events'
                               ~~~~~~~~
npm/node_modules/undici/types/dispatcher.d.ts:4:37 - error TS2307: Cannot find module 'http' or its corresponding type declarations.

4 import { IncomingHttpHeaders } from 'http'
                                      ~~~~~~
npm/node_modules/undici/types/dispatcher.d.ts:5:22 - error TS2307: Cannot find module 'buffer' or its corresponding type declarations.

5 import { Blob } from 'buffer'
                       ~~~~~~~~
npm/node_modules/undici/types/dispatcher.d.ts:45:21 - error TS2580: Cannot find name 'Buffer'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.

45     body?: string | Buffer | Uint8Array | Readable | null;
                       ~~~~~~
npm/node_modules/undici/types/dispatcher.d.ts:142:20 - error TS2580: Cannot find name 'Buffer'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.

142     onData?(chunk: Buffer): boolean;
                       ~~~~~~
npm/node_modules/undici/types/fetch.d.ts:3:23 - error TS2688: Cannot find type definition file for 'node'.

3 /// <reference types="node" />
                        ~~~~
npm/node_modules/undici/types/fetch.d.ts:5:22 - error TS2307: Cannot find module 'buffer' or its corresponding type declarations.

5 import { Blob } from 'buffer'
                       ~~~~~~~~
npm/node_modules/undici/types/fetch.d.ts:6:38 - error TS2307: Cannot find module 'url' or its corresponding type declarations.

6 import { URL, URLSearchParams } from 'url'
                                       ~~~~~
npm/node_modules/undici/types/fetch.d.ts:29:5 - error TS2503: Cannot find namespace 'NodeJS'.

29   | NodeJS.ArrayBufferView
       ~~~~~~
npm/node_modules/undici/types/file.d.ts:2:23 - error TS2688: Cannot find type definition file for 'node'.

2 /// <reference types="node" />
                        ~~~~
npm/node_modules/undici/types/file.d.ts:4:35 - error TS2307: Cannot find module 'buffer' or its corresponding type declarations.

4 import { Blob, BlobOptions } from 'buffer'
                                    ~~~~~~~~
npm/node_modules/undici/types/file.d.ts:21:48 - error TS2503: Cannot find namespace 'NodeJS'.

21   constructor(fileBits: ReadonlyArray<string | NodeJS.ArrayBufferView | Blob>, fileName: string, options?: FileOptions)
                                                  ~~~~~~
npm/node_modules/undici/types/formdata.d.ts:2:23 - error TS2688: Cannot find type definition file for 'node'.

2 /// <reference types="node" />
                        ~~~~
npm/node_modules/undici/types/mock-interceptor.d.ts:1:37 - error TS2307: Cannot find module 'http' or its corresponding type declarations.

1 import { IncomingHttpHeaders } from 'http'
                                      ~~~~~~
npm/node_modules/undici/types/pool.d.ts:3:21 - error TS2307: Cannot find module 'url' or its corresponding type declarations.

3 import { URL } from 'url'
                      ~~~~~

Some shims are buggy

After quick glance I found a few shims that are buggy - they do something else in Node than they intend to do in Deno.

  • Deno.read()
  • Deno.readSync()
  • Deno.write()
  • Deno.writeSync()

Namely:

const { bytesRead } = await _read(rid, buffer, 0, buffer.length, null);

Node expects fd (file descriptor) as first argument, but rid is not that. Resource ids are internal Deno representation and they don't map to file descriptors.

That said, we are on track to deprecate these APIs (alongside Deno.seek() and Deno.seekSync() in 1.15 (denoland/deno#12107) and to be removed in 2.0; so that shouldn't be a big deal.

`Response` in lib.deno.d.ts should refer to global `Response` type if exists

I've found mixing the shim Response with types in lib.deno.d.ts causes type checking issues.

For example:

export function createStreaming(
  response: Promise<denoShim.Response>,
) {
  if (typeof WebAssembly.instantiateStreaming === "function") {
    return WebAssembly
      .instantiateStreaming(response, createImportObject());
  } else {
    // etc...
  }
}

Provide Node.js error constructors for Deno namespace errors

Currently Node.js SystemError doesn't work well with Deno.errors, for example, when a file is not found, Node.js produces a SystemError with a code of ENOENT which equates to a Deno.errors.NotFound, but err instanceof Deno.errors.NotFound works under Deno, but fails under Node.js.

We should consider providing a [Symbol.hasInstance] implementation for a special class of Deno.errors in the shim that would do further analysis on the Node.js error instances and see if it maps back to a Deno error, so instanceof operator can work.

[help] node.js doesn't support writeSync

Deno.stdout.writeSync
can i do this?

 writeSync(p) {
    process.stdout.write(p);
    return p.length
  },

This code is in :stdout

I have encountered issues while using dnt, or can I use padding only for stdout shims?

just like:

  shims: {
    deno: true,
    custom: [{
      module: "./shims/stdout.ts",
      globalNames: ["Deno.stdout"],
    }]
  },

Any advice is greatly appreciated😃

Automatically publish packages on CI on merge to main

The tags are now somewhat meaningless due to this being a mono repo.

It would be good to write a deno script that automatically publishes any unpublished npm packages on merge to main if the commit message has a version in it.

Support Node LTS

Running this example:

const { Deno } = require("deno.ns");

I get the following in Node v14.17.1:

> node test.js
node_modules\deno.ns\dist\deno\stable\classes\PermissionStatus.js:7
class PermissionStatus extends EventTarget {
                               ^

ReferenceError: EventTarget is not defined
    at Object.<anonymous> (node_modules\deno.ns\dist\deno\stable\classes\PermissionStatus.js:7:32)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:14)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:92:18)
    at Object.<anonymous> (node_modules\deno.ns\dist\deno\stable\classes\Permissions.js:5:31)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)

This is because EventTarget was not a global until node v15 I think (works in 16). Current LTS version of node is v14. We should try to support at least Node LTS.

Fix declaration file errors in 4.4.3

Trying out version 4.4.3 and getting the following errors:

> npx tsc
node_modules/deno.ns/dist/deno/stable/functions/sleepSync.d.ts:2:38 - error TS2724: 'Deno' has no exported member named 'sleepSync'. Did you mean 'seekSync'?

2 export declare const sleepSync: Deno.sleepSync;
                                       ~~~~~~~~~

node_modules/deno.ns/dist/deno/stable/functions/test.d.ts:2:29 - error TS2307: Cannot find module '@fromdeno/test' or its corresponding type declarations.

2 export { test, tests } from "@fromdeno/test";
                              ~~~~~~~~~~~~~~~~

node_modules/deno.ns/dist/deno/stable/variables/ppid.d.ts:2:33 - error TS2724: 'Deno' has no exported member named 'ppid'. Did you mean 'pid'?

2 export declare const ppid: Deno.ppid;
                                  ~~~~


Found 3 errors.

After adding @fromdeno/test:

> npx tsc
node_modules/deno.ns/dist/deno/stable/functions/sleepSync.d.ts:2:38 - error TS2724: 'Deno' has no exported member named 'sleepSync'. Did you mean 'seekSync'?

2 export declare const sleepSync: Deno.sleepSync;
                                       ~~~~~~~~~

node_modules/deno.ns/dist/deno/stable/variables/ppid.d.ts:2:33 - error TS2724: 'Deno' has no exported member named 'ppid'. Did you mean 'pid'?

2 export declare const ppid: Deno.ppid;
                                  ~~~~


Found 2 errors.

Does not handle `Deno.FsFile`

As of Deno 1.19, Deno.File is deprecated and Deno.FsFile is used. Code using the new Deno.FsFile will not compile properly. You will get errors like Property 'FsFile' does not exist on type 'typeof Deno'. Did you mean 'File'?.

Fix web crypto types

It's been in node.js since v15 though.

npm/src/etag.ts:49:52 - error TS2339: Property 'subtle' does not exist on type 'typeof webcrypto'.

49   const hash = base64.encode(await denoShim.crypto.subtle.digest("SHA-1", entity))
                                                      ~~~~~~

TestContext type not exported

In our tests we sometimes use the Deno.TestContext type to pass around a test context to functions that trigger test steps. Using this results in this error:

Namespace '".../npm/node_modules/@deno/shim-deno-test/dist/test"' has no exported member 'TestContext'.

Implement `Buffer`

As title.

It may not be worth implementing this though because Buffer is deprecated.

Add global type for `Error#cause`

To eliminate these type checking errors:

npm/src/deps/deno_land/std_0.114.0/io/buffer.ts:433:13 - error TS2339: Property 'cause' does not exist on type 'PartialReadError'.

433           e.cause = err.cause;

@deno/shim-prompts `prompt` doesn't end/exit on enter

If you run this in Deno with deno run index.js, pressing enter ends the prompt:

prompt("> ");

But if you run this code with node index.js (assuming you have installed @deno/shim-prompts locally):

import { prompt } from "@deno/shim-prompts";

prompt("> ");

You get something different, where pressing enter just continues onto the next line and I can't seem to exit the prompt without exiting the program.

`Deno.listen` shim: `server._handle` is null

import { Deno } from "@deno/shim-deno"

Deno.listen({ port: 8000 })
.../node_modules/@deno/shim-deno/dist/deno/stable/functions/listen.js:48
    const listener = new Listener_js_1.Listener(server._handle.fd, {
                                                               ^
TypeError: Cannot read properties of null (reading 'fd')
    at Object.listen (.../node_modules/@deno/shim-deno/dist/deno/stable/functions/listen.js:48:64)
    at .../test.ts:4:6
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
$ node -v
v18.14.0

https://github.com/denoland/node_shims/blob/main/packages/shim-deno/src/deno/stable/functions/listen.ts#L58-L59

TypeError: dntShim.Deno.seekSync is not a function

When trying to transpile with shims, I get the following error:

FAILURES

blocks events that post the same content too quickly
  TypeError: dntShim.Deno.seekSync is not a function
      at js_read (file:///home/alex/Projects/strfry-policies/npm/esm/deps/deno.land/x/[email protected]/build/vfs.js:42:26)
      at wasm://wasm/0027cea2:wasm-function[84]:0x16fc
      at wasm://wasm/0027cea2:wasm-function[379]:0x236f6
      at wasm://wasm/0027cea2:wasm-function[373]:0x22415
      at wasm://wasm/0027cea2:wasm-function[385]:0x23c12
      at wasm://wasm/0027cea2:wasm-function[20]:0xf0f
      at wasm://wasm/0027cea2:wasm-function[1407]:0x9311b
      at file:///home/alex/Projects/strfry-policies/npm/esm/deps/deno.land/x/[email protected]/src/db.js:86:135
      at setStr (file:///home/alex/Projects/strfry-policies/npm/esm/deps/deno.land/x/[email protected]/src/wasm.js:13:24)
      at new DB (file:///home/alex/Projects/strfry-policies/npm/esm/deps/deno.land/x/[email protected]/src/db.js:86:24)error: Uncaught (in promise) Error: npm run test failed with exit code 1
        throw new Error(
              ^
    at runCommand (https://deno.land/x/[email protected]/lib/utils.ts:57:15)
    at async build (https://deno.land/x/[email protected]/mod.ts:386:5)
    at async file:///home/alex/Projects/strfry-policies/scripts/npm.ts:5:1

`ensureDir` cannot catch `NotFound` error from `dntShim.lstat`

NotFound error from dntShim.Deno.lstat is node one, so it is not same as dntShim.Deno.errors.NotFound

export async function ensureDir(dir) {
    try {
        const fileInfo = await dntShim.Deno.lstat(dir);
        if (!fileInfo.isDirectory) {
            throw new Error(`Ensure path exists, expected 'dir', got '${getFileInfoType(fileInfo)}'`);
        }
    }
    catch (err) {
        if (err instanceof dntShim.Deno.errors.NotFound) { // <- Always false
            await dntShim.Deno.mkdir(dir, { recursive: true });
            return;
        }
        throw err;
    }
}

Deno.open createNew not working

When I run await Deno.open('./test-file', { createNew: true, write: true }); I will get the following error when I use the deno shims with the https://github.com/denoland/dnt build esm output.

node_modules/@deno/shim-deno/dist/deno/internal/fs_flags.js:81
    throw new errors.BadResource("EINVAL: Invalid fs flags.");
          ^

BadResource [Error]: EINVAL: Invalid fs flags.

I created an example to show the error: https://github.com/tiloio/deno-open-create-new-shim-bug

There is no way to have not an error if I set createNew to true:
https://github.com/denoland/node_deno_shims/blob/430ad2bfade33c532a9a078fc4e79a48831ce5fe/packages/shim-deno/src/deno/internal/fs_flags.ts#L35

Deno color logging support in nodejs

Reference: https://examples.deno.land/color-logging

After months of thinking, I came up with this code that achieves color logging:

let chalk = await Import("[email protected]");
let oc = console;
function styled(type, message, ...styles) {
  // Split the message by the placeholder %c and trim the spaces
  let messageArray = message.split("%c").map((e) => e.trim());
  // Remove the first empty string
  if (messageArray[0].trim() == "") messageArray.shift();
  // Initialize an array to store the chalk objects
  let chalkArray = [];
  // Loop through the styles array and create the chalk objects
  for (let style of styles) {
    // Split the style string by semicolons and trim the spaces
    let styleArray = style.split(";").map((s) => s.trim());
    // Initialize a chalk object
    let chalkObject = chalk;
    // Loop through the style array and apply the corresponding methods to the chalk object
    for (let s of styleArray) {
      // Split the style by colon and trim the spaces
      let [property, value] = s.split(":").map((s) => s.trim());
      // Convert the property and value to lower case
      property = property.toLowerCase();
      value = value.toLowerCase();
      // Switch on the property and apply the value as a method to the chalk object
      switch (property) {
        case "color":
          chalkObject = chalkObject[value];
          break;
        case "font-weight":
          if (value === "bold") {
            chalkObject = chalkObject.bold;
          }
          break;
        case "font-style":
          if (value === "italic") {
            chalkObject = chalkObject.italic;
          }
          break;
        case "text-decoration":
          if (value === "underline") {
            chalkObject = chalkObject.underline;
          }
          break;
        default:
          // Ignore other properties
          break;
      }
    }
    // Push the chalk object to the chalk array
    chalkArray.push(chalkObject);
  }
  // Initialize a string to store the final message
  let finalMessage = "";
  // Loop through the message array and the chalk array and concatenate the styled messages
  for (let i = 0; i < messageArray.length; i++) {
    // If there is a chalk object for the current index, apply it to the message
    if (chalkArray[i]) {
      finalMessage += " " + chalkArray[i](messageArray[i]);
    } else {
      // Otherwise, just append the message
      finalMessage += " " + messageArray[i];
    }
  }
  // Log the final message
  oc[type](finalMessage.trim());
}
//Polyfill global console object with below:
globalThis.console={
 log: (...args)=>styled("log", ...args)
// Add other functions here like warn, error, info
}
//use it normally
console.log("%cHello %cWorld", "color: red", "color: blue");

The Import function is my custom function, dont worry about it and we need a chalk alternative maybe or we can just use it?
This is how it looks:
Screenshot_20230306_230104_Termux

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.