Skip to content
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

http/endpoint: extract common pipe code #1113

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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
25 changes: 14 additions & 11 deletions lib/http/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,28 @@ const endpointBase = ({ preprocessor = noop, before = noop, resultWriter, errorW
// ENDPOINT FORMAT SPECIALIZATIONS

////////////////////////////////////////
Copy link
Member

@matthew-white matthew-white Oct 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line comment goes together with the line comment on the next line, so the two should be moved together. I think the two lines should come before defaultResultWriter()/defaultErrorWriter()/defaultEndpoint(), similar to how there are line comments a bit above odataJsonWriter()/odataXmlErrorWriter()/odataJsonEndpoint()/etc. (the // ODATA comment and the line above it).

// JSON/default

const streamErrorHandler = (response) => () => {
response.addTrailers({ Status: 'Error' }); // TODO: improve response content.
};
const defaultResultWriter = (result, request, response, next) => {
if (!response.hasHeader('Content-Type')) response.type('json');

const pipelineResult = (result, response, next) => {
if (result instanceof PartialPipe) {
result.streams.at(-1).on('error', streamErrorHandler(response));
result.with(response).pipeline((err) => next?.(err));
return true;
} else if (result.pipe != null) {
result.on('error', streamErrorHandler(response));
pipeline(result, response, (err) => err && next?.(err));
return true;
}
};
Comment on lines +207 to +209
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know undefined is falsy, but I think it'd be nice if the function returned false if it doesn't return true.


// JSON/default
const defaultResultWriter = (result, request, response, next) => {
if (!response.hasHeader('Content-Type')) response.type('json');

if (pipelineResult(result, response, next)) {
// handled
} else if (isJsonType(response.getHeader('Content-Type'))) {
response.send(jsonSerialize(result));
} else {
Expand Down Expand Up @@ -349,16 +357,11 @@ const odataPreprocessor = (format) => (_, context) => {
// respond with the appropriate OData version (section 8.1.5).
const odataBefore = (response) => { response.setHeader('OData-Version', '4.0'); };

// TODO refactor to share common code with defaultResultWriter?
const odataJsonWriter = (result, request, response, next) => {
if (!response.hasHeader('Content-Type')) response.type('json');

if (result instanceof PartialPipe) {
result.streams.at(-1).on('error', streamErrorHandler(response));
result.with(response).pipeline((err) => next?.(err));
} else if (result.pipe != null) {
result.on('error', streamErrorHandler(response));
pipeline(result, response, (err) => err && next?.(err));
if (pipelineResult(result, response, next)) {
// handled
} else {
// OData JSON endpoints craft JSON by hand
response.send(result);
Expand Down