Skip to main content

I/O

Interfaces for reading, writing, seeking, and managing resources. For handling of data streams, file I/O, and console interactions.

Eg Deno.stdin, Deno.inspect

Enums

E
Deno.SeekMode

A enum which defines the seek mode for IO related APIs that support seeking.

    Functions

    f
    Deno.consoleSize

    Gets the size of the console as columns/rows.

      f
      Deno.inspect

      Converts the input into a string that has the same format as printed by console.log().

        Variables

        v
        Deno.stderr

        A reference to stderr which can be used to write directly to stderr. It implements the Deno specific Writer, WriterSync, and Closer interfaces as well as provides a WritableStream interface.

        v
        Deno.stdin

        A reference to stdin which can be used to read directly from stdin.

        v
        Deno.stdout

        A reference to stdout which can be used to write directly to stdout. It implements the Deno specific Writer, WriterSync, and Closer interfaces as well as provides a WritableStream interface.


        enum Deno.SeekMode

        A enum which defines the seek mode for IO related APIs that support seeking.

        Members #

        #Current = 1
        #End = 2
        #Start = 0

        function Deno.consoleSize

        #consoleSize(): { columns: number; rows: number; }

        Gets the size of the console as columns/rows.

        const { columns, rows } = Deno.consoleSize();
        

        This returns the size of the console window as reported by the operating system. It's not a reflection of how many characters will fit within the console window, but can be used as part of that calculation.

        Throws if none of stdin, stdout, or stderr is connected to a terminal (e.g. all are piped or redirected). Use Deno.stdout.isTerminal to check before calling.

        Return Type #

        { columns: number; rows: number; }

        function Deno.inspect

        #inspect(
        value: unknown,
        options?: InspectOptions,
        ): string

        Converts the input into a string that has the same format as printed by console.log().

        const obj = {
          a: 10,
          b: "hello",
        };
        const objAsString = Deno.inspect(obj); // { a: 10, b: "hello" }
        console.log(obj);  // prints same value as objAsString, e.g. { a: 10, b: "hello" }
        

        A custom inspect functions can be registered on objects, via the symbol Symbol.for("Deno.customInspect"), to control and customize the output of inspect() or when using console logging:

        class A {
          x = 10;
          y = "hello";
          [Symbol.for("Deno.customInspect")]() {
            return `x=${this.x}, y=${this.y}`;
          }
        }
        
        const inStringFormat = Deno.inspect(new A()); // "x=10, y=hello"
        console.log(inStringFormat);  // prints "x=10, y=hello"
        

        A depth can be specified by using the depth option:

        Deno.inspect({a: {b: {c: {d: 'hello'}}}}, {depth: 2}); // { a: { b: [Object] } }
        

        Parameters #

        #value: unknown
        #options: InspectOptions
        optional

        Return Type #

        string

        interface Deno.InspectOptions

        Option which can be specified when performing Deno.inspect.

        Properties #

        #colors: boolean = false
        optional

        Stylize output with ANSI colors.

        #compact: boolean = true
        optional

        Try to fit more than one entry of a collection on the same line.

        #depth: number = 4
        optional

        Traversal depth for nested objects.

        #breakLength: number = 80
        optional

        The maximum length for an inspection to take up a single line.

        #escapeSequences: boolean = true
        optional

        Whether or not to escape sequences.

        #iterableLimit: number = 100
        optional

        The maximum number of iterable entries to print.

        #showProxy: boolean = false
        optional

        Show a Proxy's target and handler.

        #sorted: boolean = false
        optional

        Sort Object, Set and Map entries by key.

        #trailingComma: boolean = false
        optional

        Add a trailing comma for multiline collections.

        #getters: boolean = false
        optional

        Evaluate the result of calling getters.

        #showHidden: boolean = false
        optional

        Show an object's non-enumerable properties.

        #strAbbreviateSize: number
        optional

        The maximum length of a string before it is truncated with an ellipsis.


        interface Deno.SetRawOptions

        Properties #

        #cbreak: boolean

        The cbreak option can be used to indicate that characters that correspond to a signal should still be generated. When disabling raw mode, this option is ignored. This functionality currently only works on Linux and Mac OS.


        variable Deno.stderr

        A reference to stderr which can be used to write directly to stderr. It implements the Deno specific Writer, WriterSync, and Closer interfaces as well as provides a WritableStream interface.

        These are low level constructs, and the console interface is a more straight forward way to interact with stdout and stderr.

        Properties #

        #writable: WritableStream<Uint8Array<ArrayBufferLike>>
        readonly

        A writable stream interface to stderr.

        Methods #

        #write(p: Uint8Array): Promise<number>

        Write the contents of the array buffer (p) to stderr.

        Resolves to the number of bytes written.

        It is not guaranteed that the full buffer will be written in a single call.

        const encoder = new TextEncoder();
        const data = encoder.encode("Hello world");
        const bytesWritten = await Deno.stderr.write(data); // 11
        
        #writeSync(p: Uint8Array): number

        Synchronously write the contents of the array buffer (p) to stderr.

        Returns the number of bytes written.

        It is not guaranteed that the full buffer will be written in a single call.

        const encoder = new TextEncoder();
        const data = encoder.encode("Hello world");
        const bytesWritten = Deno.stderr.writeSync(data); // 11
        
        #close(): void

        Closes stderr, freeing the resource.

        Deno.stderr.close();
        
        #isTerminal(): boolean

        Checks if stderr is a TTY (terminal).

        // This example is system and context specific
        Deno.stderr.isTerminal(); // true
        

        variable Deno.stdin

        A reference to stdin which can be used to read directly from stdin.

        It implements the Deno specific Reader, ReaderSync, and Closer interfaces as well as provides a ReadableStream interface.

        Reading chunks from the readable stream

        const decoder = new TextDecoder();
        for await (const chunk of Deno.stdin.readable) {
          const text = decoder.decode(chunk);
          // do something with the text
        }
        

        Properties #

        #readable: ReadableStream<Uint8Array<ArrayBuffer>>
        readonly

        A readable stream interface to stdin.

        Methods #

        #read(p: Uint8Array): Promise<number | null>

        Read the incoming data from stdin into an array buffer (p).

        Resolves to either the number of bytes read during the operation or EOF (null) if there was nothing more to read.

        It is possible for a read to successfully return with 0 bytes. This does not indicate EOF.

        It is not guaranteed that the full buffer will be read in a single call.

        // If the text "hello world" is piped into the script:
        const buf = new Uint8Array(100);
        const numberOfBytesRead = await Deno.stdin.read(buf); // 11 bytes
        const text = new TextDecoder().decode(buf);  // "hello world"
        
        #readSync(p: Uint8Array): number | null

        Synchronously read from the incoming data from stdin into an array buffer (p).

        Returns either the number of bytes read during the operation or EOF (null) if there was nothing more to read.

        It is possible for a read to successfully return with 0 bytes. This does not indicate EOF.

        It is not guaranteed that the full buffer will be read in a single call.

        // If the text "hello world" is piped into the script:
        const buf = new Uint8Array(100);
        const numberOfBytesRead = Deno.stdin.readSync(buf); // 11 bytes
        const text = new TextDecoder().decode(buf);  // "hello world"
        
        #close(): void

        Closes stdin, freeing the resource.

        Deno.stdin.close();
        
        #setRaw(
        mode: boolean,
        options?: SetRawOptions,
        ): void

        Set TTY to be under raw mode or not. In raw mode, characters are read and returned as is, without being processed. All special processing of characters by the terminal is disabled, including echoing input characters. Reading from a TTY device in raw mode is faster than reading from a TTY device in canonical mode.

        Deno.stdin.setRaw(true, { cbreak: true });
        
        #isTerminal(): boolean

        Checks if stdin is a TTY (terminal).

        // This example is system and context specific
        Deno.stdin.isTerminal(); // true
        

        variable Deno.stdout

        A reference to stdout which can be used to write directly to stdout. It implements the Deno specific Writer, WriterSync, and Closer interfaces as well as provides a WritableStream interface.

        These are low level constructs, and the console interface is a more straight forward way to interact with stdout and stderr.

        Properties #

        #writable: WritableStream<Uint8Array<ArrayBufferLike>>
        readonly

        A writable stream interface to stdout.

        Methods #

        #write(p: Uint8Array): Promise<number>

        Write the contents of the array buffer (p) to stdout.

        Resolves to the number of bytes written.

        It is not guaranteed that the full buffer will be written in a single call.

        const encoder = new TextEncoder();
        const data = encoder.encode("Hello world");
        const bytesWritten = await Deno.stdout.write(data); // 11
        
        #writeSync(p: Uint8Array): number

        Synchronously write the contents of the array buffer (p) to stdout.

        Returns the number of bytes written.

        It is not guaranteed that the full buffer will be written in a single call.

        const encoder = new TextEncoder();
        const data = encoder.encode("Hello world");
        const bytesWritten = Deno.stdout.writeSync(data); // 11
        
        #close(): void

        Closes stdout, freeing the resource.

        Deno.stdout.close();
        
        #isTerminal(): boolean

        Checks if stdout is a TTY (terminal).

        // This example is system and context specific
        Deno.stdout.isTerminal(); // true
        

        Did you find what you needed?

        Privacy policy