Giter Site home page Giter Site logo

mock-file's Introduction

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...(?)

mock-file's People

Contributors

ayame113 avatar filmaj avatar github-actions[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

filmaj sntran

mock-file's Issues

originalFunc is not a function

I'm using [email protected] with [email protected] on Deno Deploy with the fresh framework. I get this error:

An error occured during route handling or page rendering. TypeError: originalFunc is not a function
    at Object.openSync (https://deno.land/x/[email protected]/src/polyfill.ts:120:20)
    at js_open (https://deno.land/x/[email protected]/build/vfs.js:29:24)
    at denoOpen (wasm://wasm/002a4c4a:1:3005)
    at sqlite3PagerSharedLock (wasm://wasm/002a4c4a:1:30327)
    at sqlite3BtreeBeginTrans (wasm://wasm/002a4c4a:1:24816)
    at sqlite3InitOne (wasm://wasm/002a4c4a:1:157318)
    at sqlite3Init (wasm://wasm/002a4c4a:1:156554)
    at sqlite3ReadSchema (wasm://wasm/002a4c4a:1:100815)
    at sqlite3LocateTable (wasm://wasm/002a4c4a:1:98255)
    at sqlite3LocateTableItem (wasm://wasm/002a4c4a:1:245062)

Running it locally works fine.

No support for readDir?

Great module, but i'm not seeing files when I read dir. I tried some stuff like

prepareVirtualFile(path, undefined, { isDirectory: true )

Set up continuous deployment to deno.land

Thank you for maintaining and updating this project! It is extremely useful to many in the deno community, I am sure. ❤️

I noticed that there is a 1.1.0 tag on this repo. I am excited to use it! However, the latest released version on deno.land is 1.0.1.

May I suggest setting up continuous delivery in this project? For example, whenever a tag is pushed to the repo, it is easy to set up a webhook that automatically publishes code from that tag to deno.land. We at Slack use this setup to publish some of our deno libraries like slackapi/deno-slack-runtime to deno.land/x/deno_slack_runtime.

If this interests you, deno.land has instructions on how to set it up: https://deno.land/add_module

In case it useful for you and as an example, for our deno-slack-runtime repository, the webhook in the GitHub repo's settings are as follows:

  • The payload URL is https://api.deno.land/webhook/gh/deno_slack_runtime?subdir=src%2F
    • Note the ?subdir=src%2F query parameter, this only applies if the code in the repo is located in a subdirectory called src/. I don't think mock-file would need that, since the main entry-point for this project is in the mod.ts located in the root of the project.
    • The URL for this project would need to change to match the deno.land module name. So I believe the URL for this project would be https://api.deno.land/webhook/gh/mock_file
  • Only "Branch of tag creation" events are selected in the webhook events.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.