The JS runtimes
Published in

The JS runtimes

Get file information in Deno

Introduction

Exists

import {exists, existsSync} from "https://deno.land/std/fs/mod.ts";
async function exists(filePath: string): Promise<boolean>
function existsSync(filePath: string): boolean
import {exists} from "https://deno.land/std/fs/mod.ts";await exists('./abcd'); //false as abcd doesn't exists
await exists('./app.ts'); //true as app.ts exists
await exists('/var/tmp'); //true as /var/tmp exists

stat/lstat

function stat(path: string | URL): Promise<FileInfo>
function statSync(path: string | URL): FileInfo;
await Deno.stat('./abcd');
//NotFound: No such file or directory (os error 2)
await Deno.stat('./readings100M.txt');
//{
isFile: true,
isDirectory: false,
isSymlink: false,
size: 104857600,
mtime: 2021-06-07T03:06:28.242Z,
atime: 2021-06-17T05:26:29.864Z,
birthtime: 2021-04-07T01:26:28.170Z
}
await Deno.stat('/var/tmp');
//{
isFile: false,
isDirectory: true,
isSymlink: false,
size: 416,
mtime: 2021-06-17T06:19:25.516Z,
atime: 2021-06-17T22:08:10.736Z,
birthtime: 2020-01-01T08:00:00.000Z
}
await Deno.stat('/var');
//{
isFile: false,
isDirectory: true,
isSymlink: false,
size: 992,
mtime: 2021-05-22T22:24:13.049Z,
atime: 2021-05-23T18:51:40.871Z,
birthtime: 2020-01-01T08:00:00.000Z
}
await Deno.lstat('/var');
//{
isFile: false,
isDirectory: false,
isSymlink: true,
size: 11,
mtime: 2020-01-01T08:00:00.000Z,
atime: 2020-01-01T08:00:00.000Z,
birthtime: 2020-01-01T08:00:00.000Z
}

Example file server

import {exists} from "https://deno.land/std/fs/mod.ts";
import {readableStreamFromReader as toStream} from "https://deno.land/std/io/mod.ts";
const port=5000, contentDir='/var/tmp';
const listener = Deno.listen({port});
for await(const conn of listener)
handleNewConnection(conn);

async function handleNewConnection(conn: Deno.Conn) {
for await(const req of Deno.serveHttp(conn))
await handleRequest(req.request, req.respondWith);
}
async function handleRequest(req:Request, resp:any) {
const fileName=new URL(req.url).searchParams.get('filename');
if(!fileName)
return resp(new Response(undefined, {status: 400}));
const filePath=`${contentDir}/${fileName}`;
if(!await exists(filePath))
return resp(new Response(undefined, {status: 404}));
const fileSize=(await Deno.stat(filePath)).size;
const r=await Deno.open(filePath);
resp(new Response(toStream(r), {
headers: new Headers({'content-length': fileSize.toString()})
}));
await r.close();
}
curl http://localhost:5000?filename=dummy.jpg -o /dev/null
< HTTP/1.1 404 Not Found
curl http://localhost:5000?filename=helloFile.txt
< HTTP/1.1 200 OK
< content-length: 13
< date: Fri, 18 Jun 2021 18:51:35 GMT
<
Hello world
curl http://localhost:5000?filename=readings5G.txt -s -o /dev/null
< HTTP/1.1 200 OK
< content-length: 5000000000
< date: Fri, 18 Jun 2021 18:52:41 GMT
<

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store