This repository was archived by the owner on May 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnbin.ts
145 lines (126 loc) · 3.62 KB
/
nbin.ts
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
import * as fs from "fs";
import { Stat } from "nbin";
import * as path from "path";
import { readString } from "../common/buffer";
import { ReadableFilesystem } from "../common/filesystem";
import { readFooter } from "../common/footer";
import { fillFs } from "./fs";
const execPath = process.execPath;
const execPathStat = fs.statSync(execPath);
const nbinFd = fs.openSync(execPath, "r");
// Footer is located at the end of the file
const footer = readFooter(nbinFd, execPathStat.size);
// Contains the ID, mainFile and the filesystem
const headerBuffer = Buffer.allocUnsafe(footer.headerLength);
fs.readSync(nbinFd, headerBuffer, 0, footer.headerLength, footer.headerOffset);
// Reading the ID.
const id = readString(headerBuffer, 0);
// Reading the mainfile
const mainFile = readString(headerBuffer, id.offset);
/**
* Maximize read perf by storing before any overrides
*/
const originalRead = fs.read;
const originalReadSync = fs.readSync;
const fsBuffer = headerBuffer.slice(mainFile.offset);
const readableFs = ReadableFilesystem.fromBuffer(fsBuffer, {
readContents: (offset: number, length: number): Promise<Buffer> => {
const buffer = Buffer.allocUnsafe(length);
return new Promise<Buffer>((resolve, reject) => {
originalRead(nbinFd, buffer, 0, length, offset + footer.contentOffset, (err, _, buffer) => {
if (err) {
return reject(err);
}
resolve(buffer);
});
});
},
readContentsSync: (offset: number, length: number): Buffer => {
const buffer = Buffer.alloc(length);
originalReadSync(nbinFd, buffer, 0, length, offset + footer.contentOffset);
return buffer;
},
});
/**
* Parses an entry from a readable FS.
* Will split the inputted path and attempt to
* nest down the tree.
*/
const parse = (fullPath: string): {
readonly fs: ReadableFilesystem;
readonly name: string;
} | undefined => {
const parts = path.normalize(fullPath).split(path.sep).filter(i => i.length);
let fs = readableFs;
for (let i = 0; i < parts.length; i++) {
if (!fs) {
return;
}
const part = parts[i];
if (i === parts.length - 1) {
return {
fs,
name: part,
};
} else {
fs = fs.cd(part);
}
}
};
const createNotFound = (): Error => {
const e = new Error("File not found");
Object.defineProperty(e, "code", { value: "ENOENT" });
return e;
};
const exported: typeof import("nbin") = {
id: id.value,
mainFile: mainFile.value,
existsSync: (pathName: string): boolean => {
const stat = exported.statSync(pathName);
return stat.isFile || stat.isDirectory;
},
readdirSync: (pathName: string): ReadonlyArray<string> => {
const res = parse(pathName);
if (!res) {
throw createNotFound();
}
return res.fs.cd(res.name).ls();
},
readFile: async (pathName: string, encoding?: "utf8", offset?: number, length?: number): Promise<Buffer> | Promise<string> => {
const res = parse(pathName);
if (!res) {
throw createNotFound();
}
const b = await res.fs.read(res.name, offset, length);
if (encoding && encoding === "utf8") {
return b.toString();
}
return b;
},
readFileSync: (pathName: string, encoding?: "utf8", offset?: number, length?: number): Buffer | string => {
const res = parse(pathName);
if (!res) {
throw createNotFound();
}
const b = res.fs.readSync(res.name, offset, length);
if (encoding && encoding === "utf8") {
return b.toString();
}
return b;
},
statSync: (pathName: string): Stat => {
const res = parse(pathName);
if (!res) {
return {
isDirectory: false,
isFile: false,
size: 0,
};
}
return res.fs.stat(res.name);
},
shimNativeFs: (pathName: string): void => {
fillFs(pathName);
},
} as typeof import("nbin");
export = exported;