Skip to content

ayame113/mock-file

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mock-file

codecov Test

https://deno.land/x/mock_file

Some filesystem APIs cannot be used with deno deploy (eg Deno.readFileSync, Deno.writeFile).

This module makes a copy of the file in memory beforehand so that you can use the sync API and write API on deno deploy.

Warning It supports writing to files, but it does not actually cause writing to the file system. If you change the contents of the file, it only affects the memory.

Usage

Live samples are in ./example/main.ts and https://deploy-sqlite.deno.dev/.

With this module, SQLite works on deno deploy.

import {
  prepareLocalFile,
  prepareVirtualFile,
} from "https://deno.land/x/mock_file@$VERSION/mod.ts";

import { serve } from "https://deno.land/[email protected]/http/mod.ts";
import { DB } from "https://deno.land/x/[email protected]/mod.ts";

// Prepare the file in memory before opening it.
await prepareLocalFile("./db.sqlite");
prepareVirtualFile("./db.sqlite-journal");

// read db
const db = new DB("./db.sqlite", { mode: "read" });

// very simple server
serve(() => Response.json(db.query("SELECT * FROM people")));

The above code should be able to load faster as the sqlite file is delivered to the Edge location (like Cloudflare D1).

However, keep in mind that writes are only reflected in memory and are not persistent when you change the data.

API

prepareLocalFile(path: string|URL)

Reads the contents of the file into memory. The specified file will be available to synchronization APIs such as Deno.openSync() and Deno.readTextFileSync() after calling this function.

prepareVirtualFile(path: string|URL, content?: Uint8Array, info: Deno.FileInfo)

Similar to prepareLocalFile, but the file does not have to exist anywhere. You can use any Uint8Array as the content of the file.

Support status

  • Deno.FsFile#read(p: Uint8Array): Promise<number | null>
  • Deno.FsFile#readSync(p: Uint8Array): number | null
  • Deno.FsFile#write(p: Uint8Array): Promise<number>
  • Deno.FsFile#writeSync(p: Uint8Array): number
  • Deno.FsFile#seek(offset: number, whence: Deno.SeekMode): Promise<number>
  • Deno.FsFile#seekSync(offset: number, whence: Deno.SeekMode): number
  • Deno.FsFile#stat(): Promise<Deno.FileInfo>
  • Deno.FsFile#statSync(): Deno.FileInfo
  • Deno.FsFile#truncate(len?: number | undefined): Promise<void>
  • Deno.FsFile#truncateSync(len?: number | undefined): void
  • Deno.FsFile#close()
  • Deno.FsFile#readable
  • Deno.FsFile#writable
  • Deno.FsFile#rid
  • Deno.read(rid: number, buffer: Uint8Array): Promise<number | null>
  • Deno.readSync(rid: number, buffer: Uint8Array): number | null
  • Deno.write(rid: number, data: Uint8Array): Promise<number>
  • Deno.writeSync(rid: number, data: Uint8Array): number
  • Deno.seek(rid: number, offset: number, whence: Deno.SeekMode): Promise<number>
  • Deno.seekSync(rid: number, offset: number, whence: Deno.SeekMode): number
  • Deno.fstat(rid: number): Promise<Deno.FileInfo>
  • Deno.fstatSync(rid: number): Deno.FileInfo
  • Deno.ftruncate(rid: number, len?: number | undefined): Promise<void>
  • Deno.ftruncateSync(rid: number, len?: number | undefined): void
  • Deno.close(rid: number): void
  • Deno.open(path: string | URL, options?: Deno.OpenOptions | undefined): Promise<Deno.FsFile>
  • Deno.openSync(path: string | URL, options?: Deno.OpenOptions | undefined): Deno.FsFile
  • Deno.readFile(path: string | URL, options?: Deno.ReadFileOptions | undefined): Promise<Uint8Array>
  • Deno.readFileSync(path: string | URL): Uint8Array
  • Deno.readTextFile(path: string | URL, options?: Deno.ReadFileOptions | undefined): Promise<string>
  • Deno.readTextFileSync(path: string | URL): string
  • Deno.writeFile(path: string | URL, data: Uint8Array, options?: Deno.WriteFileOptions | undefined): Promise<void>
  • Deno.writeFileSync(path: string | URL, data: Uint8Array, options?: Deno.WriteFileOptions | undefined): void
  • Deno.writeTextFile(path: string | URL, data: string, options?: Deno.WriteFileOptions | undefined): Promise<void>
  • Deno.writeTextFileSync(path: string | URL, data: string, options?: Deno.WriteFileOptions | undefined): void
  • Deno.stat(path: string | URL): Promise<Deno.FileInfo>
  • Deno.statSync(path: string | URL): Deno.FileInfo
  • Deno.lstat(path: string | URL): Promise<Deno.FileInfo>
  • Deno.lstatSync(path: string | URL): Deno.FileInfo
  • Deno.fdatasyncSync(rid: number): void
  • Deno.fsync(rid: number): Promise<void>
  • Deno.fdatasyncSync(rid: number): void
  • Deno.fdatasync(rid: number): Promise<void>
  • Deno.flock(rid: number, exclusive?: boolean | undefined): Promise<void>
  • Deno.flockSync(rid: number, exclusive?: boolean | undefined): void
  • Deno.funlock(rid: number): Promise<void>
  • Deno.funlockSync(rid: number): void
  • Deno.truncate(name: string, len?: number | undefined): Promise<void>
  • Deno.truncateSync(name: string, len?: number | undefined): void
  • And more...(?)