-
Notifications
You must be signed in to change notification settings - Fork 45
/
MemoryVFS.js
176 lines (152 loc) · 4.46 KB
/
MemoryVFS.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
// Copyright 2024 Roy T. Hashimoto. All Rights Reserved.
import { FacadeVFS } from '../FacadeVFS.js';
import * as VFS from '../VFS.js';
// Sample in-memory filesystem.
export class MemoryVFS extends FacadeVFS {
// Map of existing files, keyed by filename.
mapNameToFile = new Map();
// Map of open files, keyed by id (sqlite3_file pointer).
mapIdToFile = new Map();
static async create(name, module) {
const vfs = new MemoryVFS(name, module);
await vfs.isReady();
return vfs;
}
constructor(name, module) {
super(name, module);
}
close() {
for (const fileId of this.mapIdToFile.keys()) {
this.jClose(fileId);
}
}
/**
* @param {string?} filename
* @param {number} fileId
* @param {number} flags
* @param {DataView} pOutFlags
* @returns {number|Promise<number>}
*/
jOpen(filename, fileId, flags, pOutFlags) {
const url = new URL(filename || Math.random().toString(36).slice(2), 'file://');
const pathname = url.pathname;
let file = this.mapNameToFile.get(pathname);
if (!file) {
if (flags & VFS.SQLITE_OPEN_CREATE) {
// Create a new file object.
file = {
pathname,
flags,
size: 0,
data: new ArrayBuffer(0)
};
this.mapNameToFile.set(pathname, file);
} else {
return VFS.SQLITE_CANTOPEN;
}
}
// Put the file in the opened files map.
this.mapIdToFile.set(fileId, file);
pOutFlags.setInt32(0, flags, true);
return VFS.SQLITE_OK;
}
/**
* @param {number} fileId
* @returns {number|Promise<number>}
*/
jClose(fileId) {
const file = this.mapIdToFile.get(fileId);
this.mapIdToFile.delete(fileId);
if (file.flags & VFS.SQLITE_OPEN_DELETEONCLOSE) {
this.mapNameToFile.delete(file.pathname);
}
return VFS.SQLITE_OK;
}
/**
* @param {number} fileId
* @param {Uint8Array} pData
* @param {number} iOffset
* @returns {number|Promise<number>}
*/
jRead(fileId, pData, iOffset) {
const file = this.mapIdToFile.get(fileId);
// Clip the requested read to the file boundary.
const bgn = Math.min(iOffset, file.size);
const end = Math.min(iOffset + pData.byteLength, file.size);
const nBytes = end - bgn;
if (nBytes) {
pData.set(new Uint8Array(file.data, bgn, nBytes));
}
if (nBytes < pData.byteLength) {
// Zero unused area of read buffer.
pData.fill(0, nBytes);
return VFS.SQLITE_IOERR_SHORT_READ;
}
return VFS.SQLITE_OK;
}
/**
* @param {number} fileId
* @param {Uint8Array} pData
* @param {number} iOffset
* @returns {number|Promise<number>}
*/
jWrite(fileId, pData, iOffset) {
const file = this.mapIdToFile.get(fileId);
if (iOffset + pData.byteLength > file.data.byteLength) {
// Resize the ArrayBuffer to hold more data.
const newSize = Math.max(iOffset + pData.byteLength, 2 * file.data.byteLength);
const data = new ArrayBuffer(newSize);
new Uint8Array(data).set(new Uint8Array(file.data, 0, file.size));
file.data = data;
}
// Copy data.
new Uint8Array(file.data, iOffset, pData.byteLength).set(pData);
file.size = Math.max(file.size, iOffset + pData.byteLength);
return VFS.SQLITE_OK;
}
/**
* @param {number} fileId
* @param {number} iSize
* @returns {number|Promise<number>}
*/
jTruncate(fileId, iSize) {
const file = this.mapIdToFile.get(fileId);
// For simplicity we don't make the ArrayBuffer smaller.
file.size = Math.min(file.size, iSize);
return VFS.SQLITE_OK;
}
/**
* @param {number} fileId
* @param {DataView} pSize64
* @returns {number|Promise<number>}
*/
jFileSize(fileId, pSize64) {
const file = this.mapIdToFile.get(fileId);
pSize64.setBigInt64(0, BigInt(file.size), true);
return VFS.SQLITE_OK;
}
/**
* @param {string} name
* @param {number} syncDir
* @returns {number|Promise<number>}
*/
jDelete(name, syncDir) {
const url = new URL(name, 'file://');
const pathname = url.pathname;
this.mapNameToFile.delete(pathname);
return VFS.SQLITE_OK;
}
/**
* @param {string} name
* @param {number} flags
* @param {DataView} pResOut
* @returns {number|Promise<number>}
*/
jAccess(name, flags, pResOut) {
const url = new URL(name, 'file://');
const pathname = url.pathname;
const file = this.mapNameToFile.get(pathname);
pResOut.setInt32(0, file ? 1 : 0, true);
return VFS.SQLITE_OK;
}
}