denoland / node_deno_shims Goto Github PK
View Code? Open in Web Editor NEWnpm packages providing shims for the Deno namespace and other globals. Useful for running Deno-first programs on Node.
License: MIT License
npm packages providing shims for the Deno namespace and other globals. Useful for running Deno-first programs on Node.
License: MIT License
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.
Make the readFileWithAbortSignal
test pass. Need to use https://www.npmjs.com/package/domexception
It would be useful to create a list of all the commands someone needs to run to clone and setup this project.
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
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.
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'
~~~~~
Perhaps using something like: urlpattern-polyfill; like fetch
in old Node, URLPattern
is currently available in Deno, but not in Node yet.
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.
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...
}
}
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.
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😃
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.
This line when reading stdin seems racy:
I think it's possible that between the process.stdin.read(p.length)
and process.stdin.read()
calls that stdin could receive more bytes than are in the buffer, which would cause a RangeError
.
https://github.com/fromdeno/deno.ns/blob/62b7ca9301118fc3954a3ac557a4178672ab1b02/src/global.ts#L16
have a look at this:
https://github.com/node-fetch/fetch-blob/blob/e00f849f6da9794c680ede8386454a5b82cd3be6/streams.cjs
By just depending on fetch-blob@v3 you will solve this issue...
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.
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.
I've started work on this.
Once stabilized.
It would be useful to publish this library via a tag.
Just a bump, would be great to have
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'?
.
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))
~~~~~~
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'.
Tracking issue for shimming the following two functions (possibly in its own package?)
As title.
It may not be worth implementing this though because Buffer
is deprecated.
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;
we're having some issues with dnt
published node modules on aarc64 (mac and linux) and we’re wondering if this maybe is something to do with it?
Thanks for your time.
As title.
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.
https://deno.com/blog/v1.17#updates-to-deno-test
This would require updating https://github.com/denoland/deno.ns/blob/main/src/deno/stable/functions/test.ts and making sure everything gets put into a single test definition.
recommend formdata-polyfill
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
It has also been in node.js since v15.
npm/src/util.ts:60:21 - error TS2339: Property 'getRandomValues' does not exist on type 'typeof webcrypto'.
60 denoShim.crypto.getRandomValues(new Uint8Array(256)),
~~~~~~~~~~~~~~~
https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
Closes #56
Can you please remove Object.preventExtensions for the Deno
object? I can't add additional polyilfils that seem to be missing during my local development.
Deno.emit
and Deno.formatDiagnostics
should throw a runtime error though. It's not worth implementing these unstable APIs and it will be going away from Deno's namespace soon.
This line (import from 'process') makes stdin unavailable
https://github.com/denoland/node_deno_shims/blob/ee4710f96270ba0ed4ceb6f939078404a50fd294/packages/shim-deno/src/deno/stable/functions/kill.ts#L4
related with nodejs/help#2663, nodejs/node#35997
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
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;
}
}
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
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:
I think it could import ReadableStream
and WritableStream
from node:stream/web
, but would probably have to lazily import from that in order to not cause an experimental warning every time the shim is used.
I'm trying to build a cli with Deno and cross publish to npm using dnt
but the cli works only if deno is installed and the above LOC should be the culprit. It tryes to find deno and throws if there's no deno installed.
Can i do something about this?
The contributing instructions in the readme should show the command for cloning with submodules.
This is what the CI does:
- uses: actions/checkout@v3
with:
submodules: true
- run: (cd packages/shim-deno/third_party/deno; git submodule update --init --depth=1 test_util/std)
The shimmed version of Deno.remove
throws an error when it is passed a directory for deletion:
SystemError [ERR_FS_EISDIR]: Path is a directory: rm returned EISDIR (is a directory) my/dir/at/some/path
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.