mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-31 13:00:49 +00:00
Revert "Make packed fs always available, and manually settable"
This commit is contained in:
1 parent
b852e0f8c3
commit
bb37f72a63
83 files changed
+8051
-288
No files matched your search
+97
-37
@@ -1,43 +1,103 @@
|
||||
// Copyright (c) 2024 Cesanta Software Limited
|
||||
//
|
||||
// Utility that generates packed filesystem C file compatible with the
|
||||
// Mongoose Network Library, https://github.com/cesanta/mongoose
|
||||
//
|
||||
// Usage:
|
||||
// node pack.js FILE[:DESTINATION[:gzip]] ...
|
||||
|
||||
const fs = require('fs');
|
||||
const zlib = require('zlib');
|
||||
const argv = process.argv.slice(2);
|
||||
|
||||
// Convert each command-line arguments into [ DATA_ARRAY, STRUCT_INITIALIZER ]
|
||||
const entries = argv.map(function(filename, i) {
|
||||
const parts = filename.split(':');
|
||||
const stat = fs.statSync(parts[0]);
|
||||
const data = fs.readFileSync(parts[0], null);
|
||||
let bytes = Array.from(data);
|
||||
let destination = (parts[1] || parts[0]).replace(/^\.+[\/\\]*/, '');
|
||||
if (parts[2] == 'gzip') {
|
||||
bytes = Array.from(zlib.gzipSync(data));
|
||||
destination += '.gz';
|
||||
}
|
||||
|
||||
// concat(0) appends trailing 0, in order to make any file an asciz string
|
||||
return [
|
||||
`static const unsigned char v${i}[] = {${bytes.concat(0).join(',')}};`,
|
||||
` {"/${destination}", v${i}, sizeof(v${i}) - 1, ${parseInt(stat.mtimeMs / 1000)}}`,
|
||||
];
|
||||
});
|
||||
|
||||
process.stdout.write(`// DO NOT EDIT. This file is generated using this command:
|
||||
// ${process.argv.join(' ')}
|
||||
// Copyright (c) 2013-2024 Cesanta Software Limited
|
||||
|
||||
const Pack = async function(files) {
|
||||
let out = `// DO NOT EDIT. This file is generated
|
||||
#include "mongoose.h"
|
||||
|
||||
${entries.map(x => x[0]).join('\n\n')}
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
const struct mg_mem_file mg_packed_files[] = {
|
||||
${entries.map(x => x[1]).join(',\n')},
|
||||
const char *mg_unlist(size_t no);
|
||||
const char *mg_unpack(const char *, size_t *, time_t *);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
`;
|
||||
//const str2bytes = str => new TextEncoder().encode(str);
|
||||
let no = 1;
|
||||
for (const f of files) {
|
||||
let byteArray = new TextEncoder().encode(f.data);
|
||||
if (f.zip) {
|
||||
const cs = new CompressionStream('gzip');
|
||||
const writer = cs.writable.getWriter();
|
||||
writer.write(byteArray);
|
||||
writer.close();
|
||||
const resp = new Response(cs.readable);
|
||||
const arr = await resp.arrayBuffer();
|
||||
byteArray = new Uint8Array(arr);
|
||||
}
|
||||
const bytes = Array.from(byteArray);
|
||||
|
||||
// console.log(f.path, bytes);
|
||||
out += `
|
||||
static const unsigned char v${no}[] = {`;
|
||||
// concat(0) appends trailing 0, in order to make any file an asciz string
|
||||
out += bytes.concat(0).join(',');
|
||||
out +=`};`;
|
||||
no++;
|
||||
}
|
||||
|
||||
out += `
|
||||
|
||||
static const struct packed_file {
|
||||
const char *name;
|
||||
const unsigned char *data;
|
||||
size_t size;
|
||||
time_t mtime;
|
||||
} packed_files[] = {`;
|
||||
|
||||
no = 1;
|
||||
for (const f of files) {
|
||||
out += `
|
||||
{"/${f.path}${f.zip ? '.gz' : ''}", v${no}, sizeof(v${no}), 0},`;
|
||||
no++;
|
||||
}
|
||||
out +=`
|
||||
{NULL, NULL, 0, 0}
|
||||
};
|
||||
`);
|
||||
`;
|
||||
|
||||
out += `
|
||||
static int scmp(const char *a, const char *b) {
|
||||
while (*a && (*a == *b)) a++, b++;
|
||||
return *(const unsigned char *) a - *(const unsigned char *) b;
|
||||
}
|
||||
|
||||
const char *mg_unlist(size_t no) {
|
||||
return packed_files[no].name;
|
||||
}
|
||||
|
||||
const char *mg_unpack(const char *name, size_t *size, time_t *mtime) {
|
||||
const struct packed_file *p;
|
||||
for (p = packed_files; p->name != NULL; p++) {
|
||||
if (scmp(p->name, name) != 0) continue;
|
||||
if (size != NULL) *size = p->size - 1;
|
||||
if (mtime != NULL) *mtime = p->mtime;
|
||||
return (const char *) p->data;
|
||||
}
|
||||
return NULL;
|
||||
};
|
||||
`;
|
||||
|
||||
return out;
|
||||
};
|
||||
|
||||
// Script entry point
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const [, , ...args] = process.argv;
|
||||
|
||||
const files = args.map(f => ({
|
||||
path: f,
|
||||
data: fs.readFileSync(f, 'utf8'),
|
||||
zip: !!f.match(/web_root\/.+/),
|
||||
}));
|
||||
|
||||
(async () => {
|
||||
const text = await Pack(files);
|
||||
process.stdout.write(text);
|
||||
})();
|
||||
|
||||
Reference in new issue
Block a user