-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
write.go
63 lines (52 loc) · 1.43 KB
/
write.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright (c) 2023, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Based on https://github.com/hack-pad/hackpad
// Licensed under the Apache 2.0 License
//go:build js
package jsfs
import (
"fmt"
"io"
"syscall/js"
"github.com/hack-pad/hackpadfs"
"github.com/hack-pad/hackpadfs/indexeddb/idbblob"
"github.com/hack-pad/hackpadfs/keyvalue/blob"
)
func (f *FS) Write(args []js.Value) (any, any, error) {
fl, err := f.GetFile(args)
if err != nil {
return 0, nil, err
}
flw, ok := fl.(io.Writer)
if !ok {
return 0, nil, hackpadfs.ErrNotImplemented
}
var n int
buffer := args[1]
iblob, err := idbblob.New(buffer)
if err != nil {
return n, nil, err
}
offset := args[2].Int()
length := args[3].Int()
position := args[4]
// 'offset' in Node.js's read is the offset in the buffer to start writing at,
// and 'position' is where to begin reading from in the file.
if !position.IsUndefined() && !position.IsNull() {
_, err := hackpadfs.SeekFile(fl, int64(position.Int()), io.SeekStart)
if err != nil {
return n, nil, err
}
}
dataToCopy, err := blob.View(iblob, int64(offset), int64(offset+length))
if err != nil {
// TODO: is this the right path?
return 0, nil, &hackpadfs.PathError{Op: "write", Path: fmt.Sprint(fl), Err: err}
}
n, err = blob.Write(flw, dataToCopy)
if err == io.EOF {
err = nil
}
return n, buffer, err
}