Skip to main content
The Deno 2 Release Candidate is here
Learn more
Module

x/deno/cli/js/lib.deno.ns.d.ts>Deno.seek

A modern runtime for JavaScript and TypeScript.
Go to Latest
function Deno.seek
import { Deno } from "https://deno.land/x/deno@v1.0.0/cli/js/lib.deno.ns.d.ts";
const { seek } = Deno;

Seek a resource ID (rid) to the given offset under mode given by whence. The call resolves to the new position within the resource (bytes from the start).

   const file = await Deno.open('hello.txt', {read: true, write: true, truncate: true, create: true});
   await Deno.write(file.rid, new TextEncoder().encode("Hello world"));
   // advance cursor 6 bytes
   const cursorPosition = await Deno.seek(file.rid, 6, Deno.SeekMode.Start);
   console.log(cursorPosition);  // 6
   const buf = new Uint8Array(100);
   await file.read(buf);
   console.log(new TextDecoder().decode(buf)); // "world"

The seek modes work as follows:

   // Given file.rid pointing to file with "Hello world", which is 11 bytes long:
   // Seek 6 bytes from the start of the file
   console.log(await Deno.seek(file.rid, 6, Deno.SeekMode.Start)); // "6"
   // Seek 2 more bytes from the current position
   console.log(await Deno.seek(file.rid, 2, Deno.SeekMode.Current)); // "8"
   // Seek backwards 2 bytes from the end of the file
   console.log(await Deno.seek(file.rid, -2, Deno.SeekMode.End)); // "9" (e.g. 11-2)

Parameters

rid: number
offset: number
whence: SeekMode

Returns

Promise<number>