Skip to content

Support stream #1395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@
"serialize-error": "^8.1.0",
"targz": "^1.0.1",
"try-require": "^1.2.1",
"turbo-stream": "^2.4.1",
"undici": "^6.19.8",
"universalify": "^2.0.0",
"yargs": "^17.7.2"
}
}
}
4 changes: 3 additions & 1 deletion src/registry/domain/require-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import tryRequire from 'try-require';

import strings from '../../resources';

const isCoreDependency = (x: string) => coreModules.includes(x);
const allCoreModules = [...coreModules, ...coreModules.map((m) => `node:${m}`)];

const isCoreDependency = (x: string) => allCoreModules.includes(x);
const requireCoreDependency = (x: string) =>
(isCoreDependency(x) && tryRequire(x)) || undefined;

Expand Down
26 changes: 24 additions & 2 deletions src/registry/routes/component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { serializeError } from 'serialize-error';

import { Readable } from 'node:stream';
import type { Request, RequestHandler, Response } from 'express';
import { encode } from 'turbo-stream';
import strings from '../../resources';
import type { Config } from '../../types';
import type { Repository } from '../domain/repository';
import GetComponentHelper from './helpers/get-component';
import GetComponentHelper, { stream } from './helpers/get-component';

export default function component(
conf: Config,
Expand Down Expand Up @@ -48,7 +50,27 @@ export default function component(
res.set(result.headers);
}

res.status(result.status).json(result.response);
const streamEnabled =
!!result.response.data?.component?.props?.[stream];
if (streamEnabled) {
delete result.response.data.component.props[stream];
const webStream = encode({ ...result.response });

const nodeStream = Readable.from(webStream);

res.status(result.status);
nodeStream.on('error', (err) => {
res.status(500).end(String(err));
});

res.setHeader('Content-Type', 'x-text/stream');

nodeStream.pipe(res).on('finish', () => {
res.end();
});
} else {
res.status(result.status).json(result.response);
}
} catch (e) {
res.status(500).json({
code: 'RENDER_ERROR',
Expand Down
7 changes: 6 additions & 1 deletion src/registry/routes/helpers/get-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface GetComponentResult {
};
}

export const stream = Symbol('stream');
const noop = () => {};
const noopConsole = Object.fromEntries(
Object.keys(console).map((key) => [key, noop])
Expand Down Expand Up @@ -520,7 +521,8 @@ export default function getComponent(conf: Config, repository: Repository) {
responseHeaders[header.toLowerCase()] = value;
}
},
templates: repository.getTemplatesInfo()
templates: repository.getTemplatesInfo(),
streamSymbol: stream
};

const setCallbackTimeout = () => {
Expand Down Expand Up @@ -573,6 +575,9 @@ export default function getComponent(conf: Config, repository: Repository) {
Buffer,
AbortController: globalThis?.AbortController,
AbortSignal: globalThis?.AbortSignal,
Promise,
Date,
Symbol,
eval: undefined,
fetch: globalThis?.fetch
};
Expand Down