-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.ts
273 lines (216 loc) · 11.1 KB
/
writer.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import { AttachedFile, ExternalFile, ExternalFileConfig, FileDictionaryEntry, FileHeader, CompressionType } from "./types";
import { CompressBrotli, CompressGzip } from "./asynclibz";
import fs from 'fs';
type WorkingFile = {
WriteFile: AttachedFile | ExternalFile;
FileEntry: FileDictionaryEntry;
}
export class MFSWriter {
fileCustomProperty: string = 'Modular File System';
containingFiles: AttachedFile[] = [];
externalFiles: ExternalFile[] = [];
customdataCompressionType: CompressionType = CompressionType.None;
constructor() { }
setCustomProperty(value: string) {
this.fileCustomProperty = value;
}
addFile(name: string, data: ArrayBuffer, customData: string, compressionType?: CompressionType) {
let compression: CompressionType = CompressionType.None;
if (compressionType != undefined) {
compression = compressionType;
}
this.containingFiles.push({ name, data, customData, compressionType: compression } as AttachedFile);
}
addFileFromPath(name: string, path: string, customData: string, compressionType?: CompressionType) {
let compression: CompressionType = CompressionType.None;
if (compressionType != undefined) {
compression = compressionType;
}
let data = fs.readFileSync(path).buffer;
this.containingFiles.push({ name, data, customData, compressionType: compression } as AttachedFile);
}
addExternalFile(name: string, fileinfo: ExternalFileConfig, customData: string, compressionType?: CompressionType) {
let compression: CompressionType = CompressionType.None;
if (compressionType != undefined) {
compression = compressionType;
}
this.externalFiles.push({ name, ...fileinfo, customData, compressionType: compression } as ExternalFile)
}
setCustomDataCompressionType(compressionType: CompressionType) {
this.customdataCompressionType = compressionType;
}
private stringArray(strings: string[]): ArrayBuffer {
const buffer = new ArrayBuffer(strings.join("").length + (strings.length * 4));
const view = new DataView(buffer);
let pointer = 0;
strings.forEach((str, i) => {
const strBuffer = new TextEncoder().encode(str);
view.setUint32(pointer, strBuffer.byteLength, true);
pointer += 4;
for (let i = 0; i < strBuffer.byteLength; i++) {
view.setUint8(pointer, strBuffer[i]);
pointer++;
}
})
return buffer;
}
private headerToBuffer(header: FileHeader): ArrayBuffer {
const buffer = new ArrayBuffer(34);
const view = new DataView(buffer);
view.setUint32(0, 777209421, true); // MFS.
view.setUint8(4, header.version);
view.setBigUint64(5, BigInt(header.fileDictionaryLength), true);
view.setBigUint64(13, BigInt(header.stringArrayCount), true);
view.setBigUint64(21, BigInt(header.stringArrayLength), true);
view.setUint32(29, header.customDataStringArrayIndex, true);
view.setUint8(33, header.compressionType);
return buffer;
}
private fileToBuffer(file: FileDictionaryEntry): ArrayBuffer {
const buffer = new ArrayBuffer(45);
const view = new DataView(buffer);
view.setUint32(0, file.nameStringArrayIndex, true);
view.setUint32(4, file.filenameStringArrayIndex, true);
view.setBigUint64(8, BigInt(file.offset), true);
view.setBigUint64(16, BigInt(file.length), true);
view.setUint8(24, file.compressionType);
view.setBigUint64(25, BigInt(file.uncompressedLength), true);
view.setBigUint64(33, BigInt(file.customDataOffset), true);
view.setUint32(41, file.customDataLength, true);
return buffer;
}
addStringToList(stringToAdd: string, stringList: string[]): number {
// check if the string is already in the list
let index = stringList.indexOf(stringToAdd);
if (index == -1) {
index = stringList.push(stringToAdd) - 1;
}
return index;
}
private async compressData(data: ArrayBuffer): Promise<ArrayBuffer> {
switch (this.customdataCompressionType) {
case CompressionType.Brotli:
return await CompressBrotli(data);
case CompressionType.Gzip:
return await CompressGzip(data);
default:
return data;
}
}
async export(): Promise<ArrayBuffer> {
const fileStrings: string[] = [];
const files: WorkingFile[] = [];
const header = {
magic: 'MFS.',
version: 1,
fileDictionaryLength: -1,
stringArrayCount: -1,
stringArrayLength: -1,
customDataStringArrayIndex: this.addStringToList(this.fileCustomProperty, fileStrings),
compressionType: this.customdataCompressionType.valueOf()
} as FileHeader
// containing files
for (const file of this.containingFiles) {
// if the custom dats isnt a arraybuffer we need to convert it
let customDataBuffer = new ArrayBuffer(0);
if (typeof file.customData != 'string') {
customDataBuffer = file.customData as ArrayBuffer;
} else {
customDataBuffer = new TextEncoder().encode(file.customData);
}
file.customData = await this.compressData(customDataBuffer)
const nameStringIndex = this.addStringToList(file.name, fileStrings);
const FileEntry = {
nameStringArrayIndex: nameStringIndex,
filenameStringArrayIndex: 0, // 0 means the file is in this file
offset: 0, // will be set later
length: file.data.byteLength,
compressionType: file.compressionType.valueOf(), // 0: none, 1: brotli, 2: gzip
uncompressedLength: 7308332045228794194n, // since these arent used we can put some text in for fun!
customDataOffset: 0, // will be set later
customDataLength: file.customData.byteLength
} as FileDictionaryEntry
switch (file.compressionType.valueOf()) {
case CompressionType.Brotli:
FileEntry.uncompressedLength = BigInt(file.data.byteLength);
let compressedData = await CompressBrotli(file.data);
FileEntry.length = compressedData.byteLength;
file.data = compressedData;
break;
case CompressionType.Gzip:
FileEntry.uncompressedLength = BigInt(file.data.byteLength);
let compressedDataGzip = await CompressGzip(file.data);
FileEntry.length = compressedDataGzip.byteLength;
file.data = compressedDataGzip;
break;
default:
break;
}
files.push({ WriteFile: file, FileEntry } as WorkingFile);
}
// external files
for (const file of this.externalFiles) {
// if the custom dats isnt a arraybuffer we need to convert it
let customDataBuffer = new ArrayBuffer(0);
if (typeof file.customData != 'string') {
customDataBuffer = file.customData as ArrayBuffer;
} else {
customDataBuffer = new TextEncoder().encode(file.customData);
}
file.customData = await this.compressData(customDataBuffer)
const nameStringIndex = this.addStringToList(file.name, fileStrings);
const filenameStringIndex = this.addStringToList(file.filename, fileStrings);
const FileEntry = {
nameStringArrayIndex: nameStringIndex,
filenameStringArrayIndex: filenameStringIndex + 1, // +1 means the file is in another file
offset: file.offset, // will be set later
length: file.length, // will be set later
compressionType: file.compressionType.valueOf(), // 0: none, 1: brotli, 2: gzip
// we dont know the uncompressed length of a external file because we didnt compress it...
uncompressedLength: 7308332045227682116n, // since these arent used we can put some text in for fun!
customDataOffset: 0, // will be set later
customDataLength: file.customData.byteLength
} as FileDictionaryEntry
files.push({ WriteFile: file, FileEntry } as WorkingFile);
}
header.fileDictionaryLength = files.length;
header.stringArrayCount = fileStrings.length
let uncStringBuffer = this.stringArray(fileStrings);
let stringBuffer = await this.compressData(uncStringBuffer);
header.stringArrayLength = stringBuffer.byteLength;
let headerBuffer = new Uint8Array(this.headerToBuffer(header));
const lengthOfHeader = headerBuffer.byteLength + stringBuffer.byteLength + (files.length * 45);
const lengthOfFiles = files.reduce((acc, file) => acc + (file.FileEntry.filenameStringArrayIndex > 0 ? 0 : file.FileEntry.length) + file.FileEntry.customDataLength, 0);
let fullBuffer = new Uint8Array(lengthOfHeader + lengthOfFiles);
fullBuffer.set(new Uint8Array(headerBuffer), 0);
fullBuffer.set(new Uint8Array(stringBuffer), headerBuffer.byteLength + (files.length * 45));
let pointer = lengthOfHeader;
for (let i = 0; i < files.length; i++) {
let thisFile: AttachedFile = files[i].WriteFile as unknown as AttachedFile;
// check if the file is to be stored in this file or is a external file
if (thisFile.data != undefined) {
let thisFileEntry: FileDictionaryEntry = files[i].FileEntry;
let fileBuffer = new Uint8Array(thisFile.data);
fullBuffer.set(fileBuffer, pointer);
thisFileEntry.offset = pointer
pointer += fileBuffer.byteLength;
if (typeof thisFile.customData == 'string') continue
let customDataBuffer = new Uint8Array(thisFile.customData);
//customDataBuffer = new Uint8Array(await this.compressData(customDataBuffer))
fullBuffer.set(customDataBuffer, pointer);
thisFileEntry.customDataOffset = pointer
pointer += customDataBuffer.byteLength;
} else {
// a external file only writes its custom data
let thisFileEntry: FileDictionaryEntry = files[i].FileEntry;
if (typeof thisFile.customData == 'string') continue
fullBuffer.set(new Uint8Array(new Uint8Array(thisFile.customData)), pointer);
thisFileEntry.customDataOffset = pointer
pointer += thisFile.customData.byteLength;
}
let fileBuffer = this.fileToBuffer(files[i].FileEntry);
fullBuffer.set(new Uint8Array(fileBuffer), headerBuffer.byteLength + (i * 45));
}
return fullBuffer.buffer
}
}