由于 Deno 于上周三发布,我尝试使用它并重做小示例聊天应用程序,我尝试了以下操作:
import { Application, Router, send } from 'https://deno.land/x/oak/mod.ts';
import { listenAndServe } from 'https://deno.land/std/http/server.ts'
const app = new Application();
const router = new Router();
router
.get('/ws', handleSocket);
app.use(router.routes());
app.use(router.allowedMethods());
await app.listen({ port: HTTP_PORT });
Run Code Online (Sandbox Code Playgroud)
应用程序.ts
import { WebSocket, acceptWebSocket, isWebSocketCloseEvent, acceptable } from 'https://deno.land/std/ws/mod.ts'
import { v4 } from 'https://deno.land/std/uuid/mod.ts'
const users = new Map<string, WebSocket>()
export const handleSocket = async (ctx: any) => {
if (acceptable(ctx.request.serverRequest)) {
const { conn, r: bufReader, w: bufWriter, headers } …Run Code Online (Sandbox Code Playgroud) 我Deno使用 Powershell 安装,尽管如此,我在我的Visual Studio Code如何验证?
The term 'deno' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.
Run Code Online (Sandbox Code Playgroud)
如何为程序退出添加回调?
在nodejs中的实现:
process.on('exit', function (){
console.log('Goodbye!');
});
Run Code Online (Sandbox Code Playgroud) 刚从 Deno 开始,我试图弄清楚如何计算二进制文件校验和。在我看来,问题不在于标准库的哈希模块提供的方法,而在于文件流方法和/或提供 hash.update 方法的块的类型。我一直在尝试一些与文件打开和块类型相关的替代方案,但没有成功。一个简单的例子如下:
import {createHash} from "https://deno.land/std@0.80.0/hash/mod.ts";
const file= new File(["my_big_folder.tar.gz"], "./my_big_folder.tar.gz");
const iterator = file.stream() .getIterator();
const hash = createHash("md5");
for await( let chunk of iterator){
hash.update(chunk);
}
console.log(hash.toString()); //b35edd0be7acc21cae8490a17c545928
Run Code Online (Sandbox Code Playgroud)
这段代码编译并运行没有错误,遗憾的是结果与我运行node提供的crypto模块的函数和linux coreutils提供的md5sum的结果不同。有什么建议吗?
节点代码:
const crypto = require('crypto');
const fs = require('fs');
const hash = crypto.createHash('md5');
const file = './my_big_folder.tar.gz';
const stream = fs.ReadStream(file);
stream.on('data', data=> { hash.update(data); });
stream.on('end', ()=> {
console.log(hash.digest('hex')); //c18f5eac67656328f7c4ec5d0ef5b96f
});
Run Code Online (Sandbox Code Playgroud)
bash 中的结果相同:
$ md5sum ./my_big_folder.tar.gz
$ c18f5eac67656328f7c4ec5d0ef5b96f ./my_big_folder.tar.gz
Run Code Online (Sandbox Code Playgroud)
在 Windows 10 上可以使用:
import …Run Code Online (Sandbox Code Playgroud) 我正在 Deno 中将 Docker 构建作为子进程运行,并且希望将标准输出流式传输到父标准输出 (Deno.stdout),以便立即输出。
我怎样才能实现这个目标?
目前我有以下内容,但在子进程完成之前它不会输出任何内容。
const p = Deno.run({
cmd: ['docker', 'build', '.'],
stdout: 'piped'
});
const stdout = await p.output();
await Deno.stdout.write(stdout);
Run Code Online (Sandbox Code Playgroud) 在工作线程之间共享线性任务以提高性能的最佳方式是什么?
以下面的基本 Deno Web 服务器为例:
主线程
// Create an array of four worker threads
const workers = new Array<Worker>(4).fill(
new Worker(new URL("./worker.ts", import.meta.url).href, {
type: "module",
})
);
for await (const req of server) {
// Pass this request to worker a worker thread
}
Run Code Online (Sandbox Code Playgroud)
工人.ts
self.onmessage = async (req) => {
//Peform some linear task on the request and make a response
};
Run Code Online (Sandbox Code Playgroud)
分配任务的最佳方式是否与此类似?
function* generator(): Generator<number> {
let i = 0;
while (true) {
i == 3 ? …Run Code Online (Sandbox Code Playgroud) 我已经为Deno完成了第一个包,然后将其发布到eno.land/x/。
我想知道是否有办法在上传包时忽略一些文件和目录,例如:[“.github”,“.vim”,“test_deps.ts”]。
非常像.npmignore。
如何在 Deno 中获取客户端的IP 地址?
我已经使用标准http库创建了一个测试服务器,但我无法找到提取客户端 IP 的方法。我需要它作为防止多次提交的安全功能。
在 NodeJS/Express 中,对象
ip的一个属性request具有相同的作用。req.ip给出了我在 Express 中想要的东西,但它在 Deno 中的等价物是什么?
我的代码是:
import { serve } from "https://deno.land/std@0.125.0/http/server.ts";
serve(
(req) => {
console.log(/* the client IP */);
return new Response("hello");
},
{ port: 8080 }
);
Run Code Online (Sandbox Code Playgroud)
是否有其他解决方法可以防止同一设备的多次访问?
谢谢
我使用 Deno 编译一些 TypeScript,然后将其作为网页的一部分提供,以便它在浏览器端运行。我正在尝试在客户端使用 canvas 元素,为此我需要类似CanvasRenderingContext2Dor 的类型CanvasGradient,它们在lib.dom.d.ts中定义,但它们不可用:Deno 编译给出类似 的错误TS2304 [ERROR]: Cannot find name 'CanvasRenderingContext2D'.。(另一方面,类型Path2D(在同一文件中定义)不会引起问题。)
注意:我知道当代码在浏览器中运行时,这些类型将存在于运行时,但我希望 Deno 在编译时了解它们。
我尝试以某种方式包含 .d.ts 文件。我尝试过的事情:
"libs": ["deno.window", "esnext"]在编译器选项中指定等(在 deno.json 中)。/// <reference types="https://raw.githubusercontent.com/microsoft/TypeScript/main/lib/lib.dom.d.ts" />
Run Code Online (Sandbox Code Playgroud)
// @deno-types="https://raw.githubusercontent.com/microsoft/TypeScript/main/lib/lib.dom.d.ts"
Run Code Online (Sandbox Code Playgroud)
其中一些尝试根本不起作用,有些甚至明显没有被解析。看起来我不明白 Deno 如何加载类型定义,例如它Path2D从哪里加载类型声明。如何解决这个问题?
deno ×10
typescript ×2
api ×1
checksum ×1
dom ×1
http ×1
ip ×1
javascript ×1
md5sum ×1
networking ×1
node.js ×1
sha1sum ×1
stream ×1
urlparse ×1
websocket ×1