-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
file-api-driver-memory.js
160 lines (135 loc) · 3.78 KB
/
file-api-driver-memory.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
const { time } = require('lib/time-utils.js');
const fs = require('fs-extra');
const { basicDelta } = require('lib/file-api');
class FileApiDriverMemory {
constructor() {
this.items_ = [];
this.deletedItems_ = [];
}
encodeContent_(content) {
if (content instanceof Buffer) {
return content.toString('base64');
} else {
return Buffer.from(content).toString('base64');
}
}
decodeContent_(content) {
return Buffer.from(content, 'base64').toString('utf-8');
}
itemIndexByPath(path) {
for (let i = 0; i < this.items_.length; i++) {
if (this.items_[i].path == path) return i;
}
return -1;
}
itemByPath(path) {
let index = this.itemIndexByPath(path);
return index < 0 ? null : this.items_[index];
}
newItem(path, isDir = false) {
let now = time.unixMs();
return {
path: path,
isDir: isDir,
updated_time: now, // In milliseconds!!
// created_time: now, // In milliseconds!!
content: '',
};
}
stat(path) {
let item = this.itemByPath(path);
return Promise.resolve(item ? Object.assign({}, item) : null);
}
async setTimestamp(path, timestampMs) {
let item = this.itemByPath(path);
if (!item) return Promise.reject(new Error(`File not found: ${path}`));
item.updated_time = timestampMs;
}
async list(path) {
let output = [];
for (let i = 0; i < this.items_.length; i++) {
let item = this.items_[i];
if (item.path == path) continue;
if (item.path.indexOf(`${path}/`) === 0) {
let s = item.path.substr(path.length + 1);
if (s.split('/').length === 1) {
let it = Object.assign({}, item);
it.path = it.path.substr(path.length + 1);
output.push(it);
}
}
}
return Promise.resolve({
items: output,
hasMore: false,
context: null,
});
}
async get(path, options) {
let item = this.itemByPath(path);
if (!item) return Promise.resolve(null);
if (item.isDir) return Promise.reject(new Error(`${path} is a directory, not a file`));
let output = null;
if (options.target === 'file') {
await fs.writeFile(options.path, Buffer.from(item.content, 'base64'));
} else {
const content = this.decodeContent_(item.content);
output = Promise.resolve(content);
}
return output;
}
async mkdir(path) {
let index = this.itemIndexByPath(path);
if (index >= 0) return;
this.items_.push(this.newItem(path, true));
}
async put(path, content, options = null) {
if (!options) options = {};
if (options.source === 'file') content = await fs.readFile(options.path);
let index = this.itemIndexByPath(path);
if (index < 0) {
let item = this.newItem(path, false);
item.content = this.encodeContent_(content);
this.items_.push(item);
} else {
this.items_[index].content = this.encodeContent_(content);
this.items_[index].updated_time = time.unix();
}
}
async delete(path) {
let index = this.itemIndexByPath(path);
if (index >= 0) {
let item = Object.assign({}, this.items_[index]);
item.isDeleted = true;
item.updated_time = time.unixMs();
this.deletedItems_.push(item);
this.items_.splice(index, 1);
}
}
async move(oldPath, newPath) {
let sourceItem = this.itemByPath(oldPath);
if (!sourceItem) return Promise.reject(new Error(`Path not found: ${oldPath}`));
this.delete(newPath); // Overwrite if newPath already exists
sourceItem.path = newPath;
}
async format() {
this.items_ = [];
}
async delta(path, options = null) {
const getStatFn = async path => {
let output = this.items_.slice();
for (let i = 0; i < output.length; i++) {
const item = Object.assign({}, output[i]);
item.path = item.path.substr(path.length + 1);
output[i] = item;
}
return output;
};
const output = await basicDelta(path, getStatFn, options);
return output;
}
async clearRoot() {
this.items_ = [];
}
}
module.exports = { FileApiDriverMemory };