feat: align JS exports to PascalCase and fix encoding dependency (by AI)
This commit is contained in:
parent
72a1916cee
commit
ba95b78adb
@ -1,5 +1,8 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## v1.5.2 (2026-06-08)
|
||||||
|
- **JS 对齐**: 将所有注册到 `jsmod` 的导出方法名统一为 PascalCase(如 `Read`, `Write`, `GetFileInfo`),消除 JS 环境下的调用摩擦。
|
||||||
|
|
||||||
## [1.3.3] - 2026-05-30
|
## [1.3.3] - 2026-05-30
|
||||||
- **新增**: 注册到 `jsmod`,将所有文件操作方法(Read/Write/Archive 等)注册为高危方法(unsafeList),确保安全沙箱隔离。
|
- **新增**: 注册到 `jsmod`,将所有文件操作方法(Read/Write/Archive 等)注册为高危方法(unsafeList),确保安全沙箱隔离。
|
||||||
|
|
||||||
|
|||||||
40
js_export.go
40
js_export.go
@ -9,42 +9,42 @@ import (
|
|||||||
func init() {
|
func init() {
|
||||||
jsmod.Register("file", map[string]any{
|
jsmod.Register("file", map[string]any{
|
||||||
// 读操作 (映射到私有包装器)
|
// 读操作 (映射到私有包装器)
|
||||||
"exists": func(ctx context.Context, path string) bool {
|
"Exists": func(ctx context.Context, path string) bool {
|
||||||
p, err := VerifyPathForSafeMode(ctx, path)
|
p, err := VerifyPathForSafeMode(ctx, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return Exists(p)
|
return Exists(p)
|
||||||
},
|
},
|
||||||
"read": func(ctx context.Context, path string) (string, error) {
|
"Read": func(ctx context.Context, path string) (string, error) {
|
||||||
p, err := VerifyPathForSafeMode(ctx, path)
|
p, err := VerifyPathForSafeMode(ctx, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return Read(p)
|
return Read(p)
|
||||||
},
|
},
|
||||||
"readBytes": func(ctx context.Context, path string) ([]byte, error) {
|
"ReadBytes": func(ctx context.Context, path string) ([]byte, error) {
|
||||||
p, err := VerifyPathForSafeMode(ctx, path)
|
p, err := VerifyPathForSafeMode(ctx, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return ReadBytes(p)
|
return ReadBytes(p)
|
||||||
},
|
},
|
||||||
"readLines": func(ctx context.Context, path string) ([]string, error) {
|
"ReadLines": func(ctx context.Context, path string) ([]string, error) {
|
||||||
p, err := VerifyPathForSafeMode(ctx, path)
|
p, err := VerifyPathForSafeMode(ctx, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return ReadLines(p)
|
return ReadLines(p)
|
||||||
},
|
},
|
||||||
"readDir": func(ctx context.Context, path string) ([]FileInfo, error) {
|
"ReadDir": func(ctx context.Context, path string) ([]FileInfo, error) {
|
||||||
p, err := VerifyPathForSafeMode(ctx, path)
|
p, err := VerifyPathForSafeMode(ctx, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return ReadDir(p)
|
return ReadDir(p)
|
||||||
},
|
},
|
||||||
"getFileInfo": func(ctx context.Context, path string) *FileInfo {
|
"GetFileInfo": func(ctx context.Context, path string) *FileInfo {
|
||||||
p, err := VerifyPathForSafeMode(ctx, path)
|
p, err := VerifyPathForSafeMode(ctx, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
@ -53,35 +53,35 @@ func init() {
|
|||||||
},
|
},
|
||||||
|
|
||||||
// 写操作
|
// 写操作
|
||||||
"write": func(ctx context.Context, path string, content string) error {
|
"Write": func(ctx context.Context, path string, content string) error {
|
||||||
p, err := VerifyPathForSafeMode(ctx, path)
|
p, err := VerifyPathForSafeMode(ctx, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return Write(p, content)
|
return Write(p, content)
|
||||||
},
|
},
|
||||||
"writeBytes": func(ctx context.Context, path string, content []byte) error {
|
"WriteBytes": func(ctx context.Context, path string, content []byte) error {
|
||||||
p, err := VerifyPathForSafeMode(ctx, path)
|
p, err := VerifyPathForSafeMode(ctx, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return WriteBytes(p, content)
|
return WriteBytes(p, content)
|
||||||
},
|
},
|
||||||
"remove": func(ctx context.Context, path string) error {
|
"Remove": func(ctx context.Context, path string) error {
|
||||||
p, err := VerifyPathForSafeMode(ctx, path)
|
p, err := VerifyPathForSafeMode(ctx, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return Remove(p)
|
return Remove(p)
|
||||||
},
|
},
|
||||||
"mkdir": func(ctx context.Context, path string) error {
|
"Mkdir": func(ctx context.Context, path string) error {
|
||||||
p, err := VerifyPathForSafeMode(ctx, path)
|
p, err := VerifyPathForSafeMode(ctx, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return Mkdir(p)
|
return Mkdir(p)
|
||||||
},
|
},
|
||||||
"copy": func(ctx context.Context, from, to string) error {
|
"Copy": func(ctx context.Context, from, to string) error {
|
||||||
pFrom, err := VerifyPathForSafeMode(ctx, from)
|
pFrom, err := VerifyPathForSafeMode(ctx, from)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -92,7 +92,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
return Copy(pFrom, pTo)
|
return Copy(pFrom, pTo)
|
||||||
},
|
},
|
||||||
"move": func(ctx context.Context, from, to string) error {
|
"Move": func(ctx context.Context, from, to string) error {
|
||||||
pFrom, err := VerifyPathForSafeMode(ctx, from)
|
pFrom, err := VerifyPathForSafeMode(ctx, from)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -103,7 +103,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
return Move(pFrom, pTo)
|
return Move(pFrom, pTo)
|
||||||
},
|
},
|
||||||
"replace": func(ctx context.Context, path, old, new string) error {
|
"Replace": func(ctx context.Context, path, old, new string) error {
|
||||||
p, err := VerifyPathForSafeMode(ctx, path)
|
p, err := VerifyPathForSafeMode(ctx, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -112,21 +112,21 @@ func init() {
|
|||||||
},
|
},
|
||||||
|
|
||||||
// 序列化
|
// 序列化
|
||||||
"unmarshalFile": func(ctx context.Context, path string, to any) error {
|
"UnmarshalFile": func(ctx context.Context, path string, to any) error {
|
||||||
p, err := VerifyPathForSafeMode(ctx, path)
|
p, err := VerifyPathForSafeMode(ctx, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return UnmarshalFile(p, to)
|
return UnmarshalFile(p, to)
|
||||||
},
|
},
|
||||||
"marshalFile": func(ctx context.Context, path string, data any) error {
|
"MarshalFile": func(ctx context.Context, path string, data any) error {
|
||||||
p, err := VerifyPathForSafeMode(ctx, path)
|
p, err := VerifyPathForSafeMode(ctx, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return MarshalFile(p, data)
|
return MarshalFile(p, data)
|
||||||
},
|
},
|
||||||
"marshalFilePretty": func(ctx context.Context, path string, data any) error {
|
"MarshalFilePretty": func(ctx context.Context, path string, data any) error {
|
||||||
p, err := VerifyPathForSafeMode(ctx, path)
|
p, err := VerifyPathForSafeMode(ctx, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -135,7 +135,7 @@ func init() {
|
|||||||
},
|
},
|
||||||
|
|
||||||
// 归档
|
// 归档
|
||||||
"archive": func(ctx context.Context, src, dest string) error {
|
"Archive": func(ctx context.Context, src, dest string) error {
|
||||||
pSrc, err := VerifyPathForSafeMode(ctx, src)
|
pSrc, err := VerifyPathForSafeMode(ctx, src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -146,7 +146,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
return Archive(pSrc, pDest)
|
return Archive(pSrc, pDest)
|
||||||
},
|
},
|
||||||
"extract": func(ctx context.Context, src, dest string, strip bool) error {
|
"Extract": func(ctx context.Context, src, dest string, strip bool) error {
|
||||||
pSrc, err := VerifyPathForSafeMode(ctx, src)
|
pSrc, err := VerifyPathForSafeMode(ctx, src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -159,7 +159,7 @@ func init() {
|
|||||||
},
|
},
|
||||||
|
|
||||||
// 压缩工具 (无路径,不校验)
|
// 压缩工具 (无路径,不校验)
|
||||||
"compress": Compress,
|
"Compress": Compress,
|
||||||
"decompress": Decompress,
|
"Decompress": Decompress,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -186,7 +186,7 @@ func LoadFileToB64(filename string) *MemFileB64 {
|
|||||||
ModTime: info.ModTime(),
|
ModTime: info.ModTime(),
|
||||||
IsDir: false,
|
IsDir: false,
|
||||||
Size: info.Size(),
|
Size: info.Size(),
|
||||||
DataB64: encoding.Base64(data),
|
DataB64: []byte(encoding.Base64(data)),
|
||||||
Compressed: compressed,
|
Compressed: compressed,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user