feat: fix build scripts and server

This commit is contained in:
Aarnav Tale
2025-01-01 15:06:22 +05:30
parent f3c9d8b54c
commit a4bb3cce5f
7 changed files with 605 additions and 929 deletions
+7 -8
View File
@@ -11,11 +11,15 @@ import { join, resolve } from 'node:path';
import { env } from 'node:process';
import { log } from './utils.mjs';
import { getWss, registerWss } from './ws.mjs';
import {
createReadableStreamFromReadable,
writeReadableStreamToWritable,
} from './streams.mjs';
log('SRVX', 'INFO', `Running with Node.js ${process.versions.node}`);
try {
await access('./node_modules/@react-router', constants.F_OK | constants.R_OK);
await access('./node_modules/react-router', constants.F_OK | constants.R_OK);
log('SRVX', 'INFO', 'Found node_modules dependencies');
} catch (error) {
log('SRVX', 'ERROR', 'No node_modules found. Please run `pnpm install`');
@@ -23,11 +27,7 @@ try {
process.exit(1);
}
const {
createRequestHandler: remixRequestHandler,
createReadableStreamFromReadable,
writeReadableStreamToWritable,
} = await import('@react-router/node');
const { createRequestHandler } = await import('react-router');
const { default: mime } = await import('mime');
const port = env.PORT || 3000;
@@ -53,8 +53,7 @@ if (!global.BUILD) {
global.MODE = 'production';
}
console.log(remixRequestHandler);
const handler = remixRequestHandler(global.BUILD, global.MODE);
const handler = createRequestHandler(global.BUILD, global.MODE);
const http = createServer(async (req, res) => {
const url = new URL(`http://${req.headers.host}${req.url}`);
+123
View File
@@ -0,0 +1,123 @@
// https://github.com/remix-run/react-router/blob/main/packages/react-router-node/stream.ts#L4
export async function writeReadableStreamToWritable(stream, writable) {
let reader = stream.getReader();
let flushable = writable;
try {
while (true) {
let { done, value } = await reader.read();
if (done) {
writable.end();
break;
}
writable.write(value);
if (typeof flushable.flush === 'function') {
flushable.flush();
}
}
} catch (error) {
writable.destroy(error);
throw error;
}
}
// https://github.com/remix-run/react-router/blob/08e4f2fd399543cab776f4be8a29181093a3702c/packages/react-router-node/stream.ts#L66
export const createReadableStreamFromReadable = (source) => {
let pump = new StreamPump(source);
let stream = new ReadableStream(pump, pump);
return stream;
};
class StreamPump {
highWaterMark;
accumalatedSize;
stream;
controller;
constructor(stream) {
this.highWaterMark =
stream.readableHighWaterMark ||
new Stream.Readable().readableHighWaterMark;
this.accumalatedSize = 0;
this.stream = stream;
this.enqueue = this.enqueue.bind(this);
this.error = this.error.bind(this);
this.close = this.close.bind(this);
}
size(chunk) {
return chunk?.byteLength || 0;
}
start(controller) {
this.controller = controller;
this.stream.on('data', this.enqueue);
this.stream.once('error', this.error);
this.stream.once('end', this.close);
this.stream.once('close', this.close);
}
pull() {
this.resume();
}
cancel(reason) {
if (this.stream.destroy) {
this.stream.destroy(reason);
}
this.stream.off('data', this.enqueue);
this.stream.off('error', this.error);
this.stream.off('end', this.close);
this.stream.off('close', this.close);
}
enqueue(chunk) {
if (this.controller) {
try {
let bytes = chunk instanceof Uint8Array ? chunk : Buffer.from(chunk);
let available = (this.controller.desiredSize || 0) - bytes.byteLength;
this.controller.enqueue(bytes);
if (available <= 0) {
this.pause();
}
} catch (error) {
this.controller.error(
new Error(
'Could not create Buffer, chunk must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object',
),
);
this.cancel();
}
}
}
pause() {
if (this.stream.pause) {
this.stream.pause();
}
}
resume() {
if (this.stream.readable && this.stream.resume) {
this.stream.resume();
}
}
close() {
if (this.controller) {
this.controller.close();
delete this.controller;
}
}
error(error) {
if (this.controller) {
this.controller.error(error);
delete this.controller;
}
}
}
+33
View File
@@ -0,0 +1,33 @@
import { defineConfig } from 'vite';
const prefix = process.env.__INTERNAL_PREFIX || '/admin';
if (prefix.endsWith('/')) {
throw new Error('Prefix must not end with a slash');
}
export default defineConfig(() => {
return {
build: {
minify: false,
target: 'esnext',
rollupOptions: {
input: './server/prod.mjs',
output: {
entryFileNames: 'server.js',
dir: 'build/headplane',
banner: '#!/usr/bin/env node\n',
},
external: (id) => id.startsWith('node:') || id === 'ws',
},
},
define: {
PREFIX: JSON.stringify(prefix),
},
resolve: {
alias: {
stream: 'node:stream',
crypto: 'node:crypto',
},
},
};
});