Skip to content

Commit

Permalink
feat(#60): read & serve archived assets
Browse files Browse the repository at this point in the history
  • Loading branch information
jannis-baum committed Jul 4, 2024
1 parent ee71e27 commit 6d62541
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
"markdown-it-inject-linenumbers": "^0.3.0",
"markdown-it-task-lists": "^2.1.1",
"markdown-it-texmath": "^1.0.0",
"node-stream-zip": "^1.15.0",
"tmp": "^0.2.3",
"uuid": "^9.0.0",
"ws": "^8.13.0"
},
"devDependencies": {
"@types/express": "^4.17.17",
"@types/markdown-it": "^12.2.3",
"@types/node": "^20.4.2",
"@types/tmp": "^0.2.6",
"@types/uuid": "^9.0.2",
"@types/ws": "^8.5.5",
"@typescript-eslint/eslint-plugin": "^6.2.0",
Expand Down
24 changes: 13 additions & 11 deletions src/routes/static.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import StreamZip from 'node-stream-zip';
import tmp from 'tmp';
import { Router } from 'express';
import { readFile } from 'fs/promises';
import { dirname as pdirname, join as pjoin } from 'path';
import { writeFileSync } from 'fs';

/* eslint-disable @typescript-eslint/no-var-requires */
const { isSea, getAsset } = require('node:sea');
/* eslint-enable @typescript-eslint/no-var-requires */

tmp.setGracefulCleanup();

export const router = Router();

class StaticProvider {
Expand All @@ -16,22 +21,19 @@ class StaticProvider {
private constructor() {
if (isSea()) {
const assets = getAsset('static.zip');
this._content = (path) => this._contentProd(assets, path);
const buffer = Buffer.from(assets);

const archiveFile = tmp.fileSync({ postfix: '.zip' }).name;
writeFileSync(archiveFile, buffer);
const zip = new StreamZip.async({ file: archiveFile });

this._content = (path) => zip.entryData(`static${path}`);
} else {
const staticDir = pjoin(pdirname(pdirname(__dirname)), 'static');
this._content = (path) => this._contentDev(staticDir, path);
this._content = (path) => readFile(pjoin(staticDir, path));
}
}

private async _contentDev(staticDir: string, path: string): Promise<Buffer> {
return readFile(pjoin(staticDir, path));
}

private async _contentProd(assets: Blob, path: string): Promise<Buffer> {
console.log(assets);
throw new Error('Not implemented' + path);
}

static async content(path: string): Promise<Buffer> {
return StaticProvider.instance._content(path);
}
Expand Down
15 changes: 15 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@
"@types/mime" "*"
"@types/node" "*"

"@types/tmp@^0.2.6":
version "0.2.6"
resolved "https://registry.yarnpkg.com/@types/tmp/-/tmp-0.2.6.tgz#d785ee90c52d7cc020e249c948c36f7b32d1e217"
integrity sha512-chhaNf2oKHlRkDGt+tiKE2Z5aJ6qalm7Z9rlLdBwmOiAAf09YQvvoLXjWK4HWPF1xU/fqvMgfNfpVoBscA/tKA==

"@types/uuid@^9.0.2":
version "9.0.2"
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-9.0.2.tgz#ede1d1b1e451548d44919dc226253e32a6952c4b"
Expand Down Expand Up @@ -1894,6 +1899,11 @@ node-releases@^2.0.14:
resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-2.0.14.tgz#2ffb053bceb8b2be8495ece1ab6ce600c4461b0b"
integrity sha512-y10wOWt8yZpqXmOgRo77WaHEmhYQYGNA6y421PKsKYWEK8aW+cqAphborZDhqfyKrbZEN92CN1X2KbafY2s7Yw==

node-stream-zip@^1.15.0:
version "1.15.0"
resolved "https://registry.yarnpkg.com/node-stream-zip/-/node-stream-zip-1.15.0.tgz#158adb88ed8004c6c49a396b50a6a5de3bca33ea"
integrity sha512-LN4fydt9TqhZhThkZIVQnF9cwjU3qmUH9h78Mx/K7d3VvfRqqwthLwJEUOEL0QPZ0XQmNN7be5Ggit5+4dq3Bw==

nodemon@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/nodemon/-/nodemon-3.0.1.tgz#affe822a2c5f21354466b2fc8ae83277d27dadc7"
Expand Down Expand Up @@ -2467,6 +2477,11 @@ titleize@^3.0.0:
resolved "https://registry.yarnpkg.com/titleize/-/titleize-3.0.0.tgz#71c12eb7fdd2558aa8a44b0be83b8a76694acd53"
integrity sha512-KxVu8EYHDPBdUYdKZdKtU2aj2XfEx9AfjXxE/Aj0vT06w2icA09Vus1rh6eSu1y01akYg6BjIK/hxyLJINoMLQ==

tmp@^0.2.3:
version "0.2.3"
resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.2.3.tgz#eb783cc22bc1e8bebd0671476d46ea4eb32a79ae"
integrity sha512-nZD7m9iCPC5g0pYmcaxogYKggSfLsdxl8of3Q/oIbqCqLLIO9IAF0GWjX1z9NZRHPiXv8Wex4yDCaZsgEw0Y8w==

to-regex-range@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/to-regex-range/-/to-regex-range-5.0.1.tgz#1648c44aae7c8d988a326018ed72f5b4dd0392e4"
Expand Down

0 comments on commit 6d62541

Please sign in to comment.