-
Notifications
You must be signed in to change notification settings - Fork 100
/
index.js
194 lines (173 loc) · 5.59 KB
/
index.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const ERRNO_CODES = {
EPERM: 63,
ENOENT: 44
};
// This implements an emscripten-compatible filesystem that is means
// to be mounted to the one from `sql.js`. Example:
//
// let BFS = new BlockedFS(SQL.FS, idbBackend);
// SQL.FS.mount(BFS, {}, '/blocked');
//
// Now any files created under '/blocked' will be handled by this
// filesystem, which creates a special file that handles read/writes
// in the way that we want.
class BlockedFS$1 {
constructor(FS, backend) {
this.FS = FS;
this.backend = backend;
this.node_ops = {
getattr: node => {
let fileattr = FS.isFile(node.mode) ? node.contents.getattr() : null;
let attr = {};
attr.dev = 1;
attr.ino = node.id;
attr.mode = fileattr ? fileattr.mode : node.mode;
attr.nlink = 1;
attr.uid = 0;
attr.gid = 0;
attr.rdev = node.rdev;
attr.size = fileattr ? fileattr.size : FS.isDir(node.mode) ? 4096 : 0;
attr.atime = new Date(0);
attr.mtime = new Date(0);
attr.ctime = new Date(0);
attr.blksize = fileattr ? fileattr.blockSize : 4096;
attr.blocks = Math.ceil(attr.size / attr.blksize);
return attr;
},
setattr: (node, attr) => {
if (FS.isFile(node)) {
node.contents.setattr(attr);
} else {
if (attr.mode != null) {
node.mode = attr.mode;
}
if (attr.size != null) {
node.size = attr.size;
}
}
},
lookup: (parent, name) => {
throw new this.FS.ErrnoError(ERRNO_CODES.ENOENT);
},
mknod: (parent, name, mode, dev) => {
if (name.endsWith('.lock')) {
throw new Error('Locking via lockfiles is not supported');
}
return this.createNode(parent, name, mode, dev);
},
rename: (old_node, new_dir, new_name) => {
throw new Error('rename not implemented');
},
unlink: (parent, name) => {
let node = this.FS.lookupNode(parent, name);
node.contents.delete(name);
},
readdir: node => {
// We could list all the available databases here if `node` is
// the root directory. However Firefox does not implemented
// such a methods. Other browsers do, but since it's not
// supported on all browsers users will need to track it
// separate anyway right now
throw new Error('readdir not implemented');
},
symlink: (parent, newname, oldpath) => {
throw new Error('symlink not implemented');
},
readlink: node => {
throw new Error('symlink not implemented');
}
};
this.stream_ops = {
open: stream => {
if (this.FS.isFile(stream.node.mode)) {
stream.node.contents.open();
}
},
close: stream => {
if (this.FS.isFile(stream.node.mode)) {
stream.node.contents.close();
}
},
read: (stream, buffer, offset, length, position) => {
// console.log('read', offset, length, position)
return stream.node.contents.read(buffer, offset, length, position);
},
write: (stream, buffer, offset, length, position) => {
// console.log('write', offset, length, position);
return stream.node.contents.write(buffer, offset, length, position);
},
llseek: (stream, offset, whence) => {
// Copied from MEMFS
var position = offset;
if (whence === 1) {
position += stream.position;
} else if (whence === 2) {
if (FS.isFile(stream.node.mode)) {
position += stream.node.contents.getattr().size;
}
}
if (position < 0) {
throw new this.FS.ErrnoError(28);
}
return position;
},
allocate: (stream, offset, length) => {
stream.node.contents.setattr({ size: offset + length });
},
mmap: (stream, address, length, position, prot, flags) => {
throw new Error('mmap not implemented');
},
msync: (stream, buffer, offset, length, mmapFlags) => {
throw new Error('msync not implemented');
},
fsync: (stream, buffer, offset, length, mmapFlags) => {
stream.node.contents.fsync();
}
};
}
async init() {
await this.backend.init();
}
mount() {
return this.createNode(null, '/', 16384 /* dir */ | 511 /* 0777 */, 0);
}
lock(path, lockType) {
let { node } = this.FS.lookupPath(path);
return node.contents.lock(lockType);
}
unlock(path, lockType) {
let { node } = this.FS.lookupPath(path);
return node.contents.unlock(lockType);
}
createNode(parent, name, mode, dev) {
// Only files and directories supported
if (!(this.FS.isDir(mode) || this.FS.isFile(mode))) {
throw new this.FS.ErrnoError(ERRNO_CODES.EPERM);
}
var node = this.FS.createNode(parent, name, mode, dev);
if (this.FS.isDir(node.mode)) {
node.node_ops = {
mknod: this.node_ops.mknod,
lookup: this.node_ops.lookup,
unlink: this.node_ops.unlink,
setattr: this.node_ops.setattr
};
node.stream_ops = {};
node.contents = {};
} else if (this.FS.isFile(node.mode)) {
node.node_ops = this.node_ops;
node.stream_ops = this.stream_ops;
// Create file!
node.contents = this.backend.createFile(name);
}
// add the new node to the parent
if (parent) {
parent.contents[name] = node;
parent.timestamp = node.timestamp;
}
return node;
}
}
// Right now we don't support `export from` so we do this manually
const BlockedFS = BlockedFS$1;
export { BlockedFS };