Skip to content

Commit

Permalink
Nits.
Browse files Browse the repository at this point in the history
  • Loading branch information
taeold committed Nov 22, 2024
1 parent cfbcd46 commit 1801c53
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 12 deletions.
1 change: 1 addition & 0 deletions config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Uncomment this if you'd like others to create their own Firebase project.
12 changes: 0 additions & 12 deletions packages/functions/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,6 @@ async function streamAtURL(
// Encode any special types, such as dates, in the input data.
data = encode(data);
const body = { data };

// Add a header for the authToken.
const headers: { [key: string]: string } = {};
const context = await functionsInstance.contextProvider.getContext();
Expand All @@ -398,7 +397,6 @@ async function streamAtURL(
if (context.appCheckToken !== null) {
headers['X-Firebase-AppCheck'] = context.appCheckToken;
}

headers['Content-Type'] = 'application/json';
headers['Accept'] = 'text/event-stream';

Expand Down Expand Up @@ -448,30 +446,26 @@ async function streamAtURL(
}
};
}

let resultResolver: (value: unknown) => void;
let resultRejecter: (reason: unknown) => void;
const resultPromise = new Promise<unknown>((resolve, reject) => {
resultResolver = resolve;
resultRejecter = reject;
});

options?.signal?.addEventListener('abort', () => {
const error = new FunctionsError(
'cancelled',
'Request was cancelled.'
);
resultRejecter(error);
});

const reader = response.body!.getReader();
const rstream = createResponseStream(
reader,
resultResolver!,
resultRejecter!,
options?.signal
);

return {
stream: {
[Symbol.asyncIterator]() {
Expand Down Expand Up @@ -500,12 +494,10 @@ function createResponseStream(
): ReadableStream<unknown> {
const processLine = (line: string, controller: ReadableStreamDefaultController): void => {
const match = line.match(responseLineRE);
//
// ignore all other lines (newline, comments, etc.)
if (!match) {
return;
}

const data = match[1];
try {
const jsonData = JSON.parse(data);
Expand Down Expand Up @@ -545,7 +537,6 @@ function createResponseStream(
resultRejecter(error);
return Promise.resolve();
}

try {
const { value, done } = await reader.read();
if (done) {
Expand All @@ -555,19 +546,16 @@ function createResponseStream(
controller.close();
return;
}

if (signal?.aborted) {
const error = new FunctionsError('cancelled', 'Request was cancelled');
controller.error(error);
resultRejecter(error);
await reader.cancel();
return;
}

currentText += decoder.decode(value, { stream: true });
const lines = currentText.split("\n");
currentText = lines.pop() || '';

for (const line of lines) {
if (line.trim()) {
processLine(line.trim(), controller);
Expand Down

0 comments on commit 1801c53

Please sign in to comment.