Skip to content

Commit 903f1ef

Browse files
committed
Add tab-delimited printer/parser and abstract backend.
1 parent c21564e commit 903f1ef

File tree

5 files changed

+1863
-67
lines changed

5 files changed

+1863
-67
lines changed

azure-pipelines.yml

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# Node.js
2-
# Build a general Node.js application with npm.
3-
# Add steps that analyze code, save build artifacts, deploy, and more:
4-
# https://docs.microsoft.com/vsts/pipelines/languages/javascript
51
jobs:
62
- job: Linux
73

@@ -51,20 +47,11 @@ jobs:
5147
condition: and(succeededOrFailed(), ne(variables['system.pullrequest.isfork'], true))
5248
inputs:
5349
artifactName: 'npm-tarball.tgz'
54-
PathtoPublish: '$(System.DefaultWorkingDirectory)/isomorphic-git-0.0.0-development.tgz'
55-
56-
- task: PublishBuildArtifacts@1
57-
displayName: 'Save bundle.umd.min.js'
58-
condition: and(succeededOrFailed(), ne(variables['system.pullrequest.isfork'], true))
59-
inputs:
60-
artifactName: 'bundle.umd.min.js'
61-
PathtoPublish: '$(System.DefaultWorkingDirectory)/dist/bundle.umd.min.js'
50+
PathtoPublish: '$(System.DefaultWorkingDirectory)/isomorphic-git-idb-fs-0.0.0-development.tgz'
6251

6352
- script: npm run semantic-release
6453
displayName: 'Publish to npm'
6554
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
6655
env:
6756
GH_TOKEN: $(GITHUB_TOKEN)
6857
NPM_TOKEN: $(Npm.Token)
69-
TWITTER_ACCESS_TOKEN_SECRET: $(TWITTER_ACCESS_TOKEN_SECRET)
70-
TWITTER_CONSUMER_SECRET: $(TWITTER_CONSUMER_SECRET)

src/CacheFS.js

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,80 @@
1-
const printTree = require("print-tree");
2-
31
const path = require("./path.js");
42
const { ENOENT, EEXIST, ENOTEMPTY } = require("./errors.js");
53

64
const STAT = 0;
75

86
module.exports = class CacheFS {
97
constructor() {
8+
const root = this._makeRoot();
9+
this._root = new Map([["/", root]]);
10+
}
11+
_makeRoot() {
1012
let root = new Map();
1113
let stat = { mode: 0o777, type: "dir", size: 0, mtimeMs: Date.now() };
1214
root.set(STAT, stat);
13-
this._root = new Map([["/", root]]);
15+
return root
1416
}
15-
_print() {
16-
const root = [...this._root.entries()][0];
17-
return printTree(
18-
root,
19-
node => {
20-
let stat = node[1].get(STAT);
17+
print(root) {
18+
root = root || this._root.get("/")
19+
let str = "";
20+
const printTree = (root, indent) => {
21+
for (let [file, node] of root) {
22+
if (file === 0) continue;
23+
let stat = node.get(STAT);
2124
let mode = stat.mode.toString(8);
2225
if (stat.type === "file") {
23-
return `${node[0]} [mode=${mode} size=${stat.size} mtime=${stat.mtimeMs}]`;
26+
str += `\n${"\t".repeat(indent)}${file}\t${mode}\t${stat.size}\t${
27+
stat.mtimeMs
28+
}`;
2429
} else {
25-
return `${node[0]} [mode=${mode}]`;
30+
str += `\n${"\t".repeat(indent)}${file}\t${mode}`;
31+
printTree(node, indent + 1);
2632
}
27-
},
28-
node => {
29-
if (node[1] === null) {
30-
// it's a file
31-
return [];
32-
} else {
33-
// it's a dir
34-
return [...node[1].entries()].filter(([key]) => typeof key === "string");
33+
}
34+
};
35+
printTree(root, 0);
36+
return str.trimStart();
37+
}
38+
parse(print) {
39+
function mk(stat) {
40+
if (stat.length === 1) {
41+
let [mode] = stat
42+
mode = parseInt(mode, 8);
43+
return new Map([
44+
[STAT, { mode, type: "dir", size: 0, mtimeMs: Date.now() }]
45+
]);
46+
} else {
47+
let [mode, size, mtimeMs] = stat;
48+
mode = parseInt(mode, 8);
49+
size = parseInt(size);
50+
mtimeMs = parseInt(mtimeMs);
51+
return new Map([[STAT, { mode, type: "file", size, mtimeMs }]]);
52+
}
53+
}
54+
55+
let lines = print.trim().split("\n");
56+
let _root = this._makeRoot();
57+
let stack = [
58+
{ indent: -1, node: _root },
59+
{ indent: 0, node: null }
60+
];
61+
for (let line of lines) {
62+
// let [, prefix, filename, stat]
63+
let prefix = line.match(/^\t*/)[0];
64+
let indent = prefix.length;
65+
line = line.slice(indent);
66+
let [filename, ...stat] = line.split("\t");
67+
let node = mk(stat);
68+
if (indent <= stack[stack.length - 1].indent) {
69+
while (indent <= stack[stack.length - 1].indent) {
70+
stack.pop();
3571
}
3672
}
37-
);
73+
stack.push({ indent, node });
74+
let cd = stack[stack.length - 2].node;
75+
cd.set(filename, node);
76+
}
77+
return _root;
3878
}
3979
_lookup(filepath) {
4080
let dir = this._root;
@@ -44,27 +84,6 @@ module.exports = class CacheFS {
4484
}
4585
return dir;
4686
}
47-
// _mkdirp(filepath) {
48-
// let dir = this._root;
49-
// let traversing = true;
50-
// let tmp;
51-
// for (let part of path.split(filepath)) {
52-
// if (traversing) {
53-
// tmp = dir.get(part);
54-
// if (tmp) {
55-
// dir = tmp;
56-
// } else {
57-
// traversing = false;
58-
// }
59-
// }
60-
// if (!traversing) {
61-
// tmp = new Map();
62-
// dir.set(part, tmp);
63-
// dir = tmp;
64-
// }
65-
// }
66-
// return dir;
67-
// }
6887
mkdir(filepath, { mode }) {
6988
if (filepath === "/") throw new EEXIST();
7089
let dir = this._lookup(path.dirname(filepath));

src/IdbBackend.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const idb = require("idb-keyval");
2+
3+
module.exports = class IdbBackend {
4+
constructor(name) {
5+
this._database = name;
6+
this._store = new idb.Store(this._database, this._database + "_files");
7+
}
8+
saveSuperblock(superblock) {
9+
return idb.set("!root", superblock, this._store);
10+
}
11+
loadSuperblock() {
12+
return idb.get("!root", this._store);
13+
}
14+
readFile(filepath) {
15+
return idb.get(filepath, this._store)
16+
}
17+
writeFile(filepath, data) {
18+
return idb.set(filepath, data, this._store)
19+
}
20+
unlink(filepath) {
21+
return idb.del(filepath, this._store)
22+
}
23+
wipe() {
24+
return idb.clear(this._store)
25+
}
26+
}

0 commit comments

Comments
 (0)