jsfs

package
v0.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 20, 2024 License: BSD-3-Clause Imports: 21 Imported by: 0

README

jsfs

Package jsfs provides a Node.js style filesystem API in Go that can be used to allow os functions to work on wasm. It implements all standard Node.js fs functions except for the synchronous versions, as those are not needed by the Go standard library.

It is built on hackpadfs and based on hackpad, which are licensed under the Apache 2.0 License.

Documentation

Rendered for js/wasm

Overview

Package jsfs provides a Node.js style filesystem API in Go that can be used to allow os functions to work on wasm.

Index

Constants

This section is empty.

Variables

View Source
var (
	FuncTrue = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
		return true
	})
	FuncFalse = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
		return false
	})
)
View Source
var (
	Stdout hackpadfs.File = &BufferedLogger{Nm: "dev/stdout", PrintFn: func(args ...any) {
		js.Global().Get("console").Call("log", args...)
	}}
	Stderr hackpadfs.File = &BufferedLogger{Nm: "dev/stderr", PrintFn: func(args ...any) {
		js.Global().Get("console").Call("error", args...)
	}}
)
View Source
var ErrNotDir = WrapError(errors.New("not a directory"), "ENOTDIR")

Functions

func BlockCount

func BlockCount(size, blockSize int64) int64

func ErrBadFile

func ErrBadFile(identifier string) error

func ErrBadFileNumber

func ErrBadFileNumber(fd uint64) error

func FuncOf

func FuncOf[F Func](name string, fn F) js.Func

FuncOf turns the given function into a callback js.Func.

func GetErrType

func GetErrType(err error, debugMessage string) string

GetErrType returns the JS type of the given error.

func JSBoolFunc

func JSBoolFunc(b bool) js.Func

func JSError

func JSError(err error, message string, args ...js.Value) js.Value

JsError converts the given error value into a JS error value.

func JSMode

func JSMode(mode os.FileMode) uint32

func JSStat

func JSStat(info os.FileInfo) js.Value

func NewNullFile

func NewNullFile(name string) hackpadfs.File

func NormPath

func NormPath(p string) string

NormPath normalizes the given path by cleaning it and making it non-rooted, as all go fs paths must be non-rooted.

func SetFunc

func SetFunc[F Func](v js.Value, name string, fn F)

SetFunc sets the function with the given name on the given value to the given function.

func WrapError

func WrapError(err error, code string) error

Types

type BufferedLogger

type BufferedLogger struct {
	Nm        string
	PrintFn   func(args ...any)
	Mu        sync.Mutex
	Buf       bytes.Buffer
	TimerOnce sync.Once
}

func (*BufferedLogger) Close

func (b *BufferedLogger) Close() error

func (*BufferedLogger) Flush

func (b *BufferedLogger) Flush()

func (*BufferedLogger) Name

func (b *BufferedLogger) Name() string

func (*BufferedLogger) Print

func (b *BufferedLogger) Print(s string) int

func (*BufferedLogger) Read

func (b *BufferedLogger) Read(p []byte) (n int, err error)

func (*BufferedLogger) Stat

func (b *BufferedLogger) Stat() (os.FileInfo, error)

func (*BufferedLogger) Write

func (b *BufferedLogger) Write(p []byte) (n int, err error)

type FS

type FS struct {
	*mount.FS

	PreviousFID uint64
	Files       map[uint64]hackpadfs.File
	Mu          sync.Mutex
}

FS represents a filesystem that implements the Node.js fs API. It is backed by a mount.FS, and automatically provides /dev/stdin, /dev/stdout, /dev/stderr, and /tmp. It can be configured with a default unix-style home directory backed by a persistent IndexedDB storage mechanism using FS.ConfigUnix.

func Config

func Config(jfs js.Value) (*FS, error)

Config configures the given JavaScript object to be a filesystem that implements the Node.js fs API. It is the main entry point for code using jsfs. It returns the resulting FS, which should not typically be needed.

func NewFS

func NewFS() (*FS, error)

NewFS returns a new FS. Most code should use Config instead.

func (*FS) Chmod

func (f *FS) Chmod(args []js.Value) (any, error)

func (*FS) Chown

func (f *FS) Chown(args []js.Value) (any, error)

func (*FS) Close

func (f *FS) Close(args []js.Value) (any, error)

func (*FS) ConfigUnix

func (f *FS) ConfigUnix() error

ConfigUnix configures a standard unix-style home directory in the filesystem located at /home/me. It is backed by a persistent IndexedDB storage mechanism such that files will persist between browser sessions, and it is initialized to contain .data, Desktop, Documents, and Downloads directories.

func (*FS) Fchmod

func (f *FS) Fchmod(args []js.Value) (any, error)

func (*FS) Fchown

func (f *FS) Fchown(args []js.Value) (any, error)

func (*FS) Fstat

func (f *FS) Fstat(args []js.Value) (any, error)

func (*FS) Fsync

func (f *FS) Fsync(args []js.Value) (any, error)

func (*FS) Ftruncate

func (f *FS) Ftruncate(args []js.Value) (any, error)

func (*FS) GetFile

func (f *FS) GetFile(args []js.Value) (hackpadfs.File, error)

GetFile fetches the file specified by the file descriptor that is the first of the given arguments.

func (*FS) Lchown

func (f *FS) Lchown(args []js.Value) (any, error)
func (f *FS) Link(args []js.Value) (any, error)

func (*FS) Lstat

func (f *FS) Lstat(args []js.Value) (any, error)

func (*FS) Mkdir

func (f *FS) Mkdir(args []js.Value) (any, error)

func (*FS) MkdirAll

func (f *FS) MkdirAll(args []js.Value) (any, error)

func (*FS) NewFile

func (f *FS) NewFile(absPath string, flags int, mode os.FileMode) (hackpadfs.File, error)

func (*FS) Open

func (f *FS) Open(args []js.Value) (any, error)

func (*FS) OpenImpl

func (f *FS) OpenImpl(path string, flags int, mode hackpadfs.FileMode) (uint64, error)

func (*FS) Read

func (f *FS) Read(args []js.Value) (any, any, error)

func (*FS) ReadFile

func (f *FS) ReadFile(args []js.Value) (any, error)

func (*FS) Readdir

func (f *FS) Readdir(args []js.Value) (any, error)
func (f *FS) Readlink(args []js.Value) (any, error)

func (*FS) Rename

func (f *FS) Rename(args []js.Value) (any, error)

func (*FS) Rmdir

func (f *FS) Rmdir(args []js.Value) (any, error)

func (*FS) Stat

func (f *FS) Stat(args []js.Value) (any, error)
func (f *FS) Symlink(args []js.Value) (any, error)

func (*FS) Truncate

func (f *FS) Truncate(args []js.Value) (any, error)
func (f *FS) Unlink(args []js.Value) (any, error)

func (*FS) Utimes

func (f *FS) Utimes(args []js.Value) (any, error)

func (*FS) Write

func (f *FS) Write(args []js.Value) (any, any, error)

type Func

type Func interface {
	func(args []js.Value) (any, error) | func(args []js.Value) (any, any, error)
}

Func is the type of a jsfs function.

type NullFile

type NullFile struct {
	Nm string
}

func (NullFile) Close

func (f NullFile) Close() error

func (NullFile) Read

func (f NullFile) Read(p []byte) (n int, err error)

func (NullFile) ReadAt

func (f NullFile) ReadAt(p []byte, off int64) (n int, err error)

func (NullFile) Seek

func (f NullFile) Seek(offset int64, whence int) (int64, error)

func (NullFile) Stat

func (f NullFile) Stat() (os.FileInfo, error)

func (NullFile) Truncate

func (f NullFile) Truncate(size int64) error

func (NullFile) Write

func (f NullFile) Write(p []byte) (n int, err error)

func (NullFile) WriteAt

func (f NullFile) WriteAt(p []byte, off int64) (n int, err error)

type NullStat

type NullStat struct {
	Nm string
}

func (NullStat) IsDir

func (s NullStat) IsDir() bool

func (NullStat) ModTime

func (s NullStat) ModTime() time.Time

func (NullStat) Mode

func (s NullStat) Mode() os.FileMode

func (NullStat) Name

func (s NullStat) Name() string

func (NullStat) Size

func (s NullStat) Size() int64

func (NullStat) Sys

func (s NullStat) Sys() interface{}

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL