Skip to content

Commit

Permalink
Modified the index.js stream functions by using sendChunk method.
Browse files Browse the repository at this point in the history
  • Loading branch information
MustafaJadid2025 committed Feb 19, 2025
1 parent 5d56561 commit dd1bf7a
Showing 1 changed file with 76 additions and 74 deletions.
150 changes: 76 additions & 74 deletions firebase-functions/src/androidTest/backend/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,127 +12,131 @@
// See the License for the specific language governing permissions and
// limitations under the License.

const assert = require('assert');
const functions = require('firebase-functions');
const assert = require("assert");
const functionsV1 = require("firebase-functions");
const functionsV2 = require("firebase-functions/v2");

exports.dataTest = functions.https.onRequest((request, response) => {
/**
* Pauses the execution for a specified amount of time.
* @param {number} ms - The number of milliseconds to sleep.
* @return {Promise<void>} A promise that resolves after the specified time.
*/
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

exports.dataTest = functionsV1.https.onRequest((request, response) => {
assert.deepEqual(request.body, {
data: {
bool: true,
int: 2,
long: {
value: '3',
'@type': 'type.googleapis.com/google.protobuf.Int64Value',
"bool": true,
"int": 2,
"long": {
"value": "3",
"@type": "type.googleapis.com/google.protobuf.Int64Value",
},
string: 'four',
array: [5, 6],
'null': null,
}
"string": "four",
"array": [5, 6],
"null": null,
},
});
response.send({
data: {
message: 'stub response',
message: "stub response",
code: 42,
long: {
value: '420',
'@type': 'type.googleapis.com/google.protobuf.Int64Value',
"value": "420",
"@type": "type.googleapis.com/google.protobuf.Int64Value",
},
}
},
});
});

exports.scalarTest = functions.https.onRequest((request, response) => {
exports.scalarTest = functionsV1.https.onRequest((request, response) => {
assert.deepEqual(request.body, {data: 17});
response.send({data: 76});
});

exports.tokenTest = functions.https.onRequest((request, response) => {
assert.equal(request.get('Authorization'), 'Bearer token');
exports.tokenTest = functionsV1.https.onRequest((request, response) => {
assert.equal(request.get("Authorization"), "Bearer token");
assert.deepEqual(request.body, {data: {}});
response.send({data: {}});
});

exports.instanceIdTest = functions.https.onRequest((request, response) => {
assert.equal(request.get('Firebase-Instance-ID-Token'), 'iid');
exports.instanceIdTest = functionsV1.https.onRequest((request, response) => {
assert.equal(request.get("Firebase-Instance-ID-Token"), "iid");
assert.deepEqual(request.body, {data: {}});
response.send({data: {}});
});

exports.appCheckTest = functions.https.onRequest((request, response) => {
assert.equal(request.get('X-Firebase-AppCheck'), 'appCheck');
exports.appCheckTest = functionsV1.https.onRequest((request, response) => {
assert.equal(request.get("X-Firebase-AppCheck"), "appCheck");
assert.deepEqual(request.body, {data: {}});
response.send({data: {}});
});

exports.appCheckLimitedUseTest = functions.https.onRequest((request, response) => {
assert.equal(request.get('X-Firebase-AppCheck'), 'appCheck-limited-use');
assert.deepEqual(request.body, {data: {}});
response.send({data: {}});
});
exports.appCheckLimitedUseTest = functionsV1.https.onRequest(
(request, response) => {
assert.equal(request.get("X-Firebase-AppCheck"), "appCheck-limited-use");
assert.deepEqual(request.body, {data: {}});
response.send({data: {}});
});

exports.nullTest = functions.https.onRequest((request, response) => {
exports.nullTest = functionsV1.https.onRequest((request, response) => {
assert.deepEqual(request.body, {data: null});
response.send({data: null});
});

exports.missingResultTest = functions.https.onRequest((request, response) => {
exports.missingResultTest = functionsV1.https.onRequest((request, response) => {
assert.deepEqual(request.body, {data: null});
response.send({});
});

exports.unhandledErrorTest = functions.https.onRequest((request, response) => {
// Fail in a way that the client shouldn't see.
throw 'nope';
});
exports.unhandledErrorTest = functionsV1.https.onRequest(
(request, response) => {
// Fail in a way that the client shouldn't see.
throw new Error("nope");
},
);

exports.unknownErrorTest = functions.https.onRequest((request, response) => {
exports.unknownErrorTest = functionsV1.https.onRequest((request, response) => {
// Send an http error with a body with an explicit code.
response.status(400).send({
error: {
status: 'THIS_IS_NOT_VALID',
message: 'this should be ignored',
status: "THIS_IS_NOT_VALID",
message: "this should be ignored",
},
});
});

exports.explicitErrorTest = functions.https.onRequest((request, response) => {
exports.explicitErrorTest = functionsV1.https.onRequest((request, response) => {
// Send an http error with a body with an explicit code.
response.status(400).send({
error: {
status: 'OUT_OF_RANGE',
message: 'explicit nope',
status: "OUT_OF_RANGE",
message: "explicit nope",
details: {
start: 10,
end: 20,
long: {
value: '30',
'@type': 'type.googleapis.com/google.protobuf.Int64Value',
"value": "30",
"@type": "type.googleapis.com/google.protobuf.Int64Value",
},
},
},
});
});

exports.httpErrorTest = functions.https.onRequest((request, response) => {
exports.httpErrorTest = functionsV1.https.onRequest((request, response) => {
// Send an http error with no body.
response.status(400).send();
});

exports.timeoutTest = functions.https.onRequest((request, response) => {
exports.timeoutTest = functionsV1.https.onRequest((request, response) => {
// Wait for longer than 500ms.
setTimeout(() => response.send({data: true}), 500);
});

const data = ["hello", "world", "this", "is", "cool"];

/**
* Pauses the execution for a specified amount of time.
* @param {number} ms - The number of milliseconds to sleep.
* @return {Promise<void>} A promise that resolves after the specified time.
*/
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
const streamData = ["hello", "world", "this", "is", "cool"];

/**
* Generates chunks of text asynchronously, yielding one chunk at a time.
Expand All @@ -141,38 +145,36 @@ function sleep(ms) {
* @yields {string} A chunk of text from the data array.
*/
async function* generateText() {
for (const chunk of data) {
for (const chunk of streamData) {
yield chunk;
await sleep(1000);
await sleep(100);
}
}

exports.genStream = functions.https.onCall(async (request, response) => {
if (response && response.acceptsStreaming) {
exports.genStream = functionsV2.https.onCall(async (request, response) => {
if (request.acceptsStreaming) {
for await (const chunk of generateText()) {
console.log("got chunk", chunk);
response.write({chunk});
response.sendChunk({chunk});
}
}
return data.join(" ");
return streamData.join(" ");
});

exports.genStreamError = functions.https.onCall(async (request, response) => {
if (response && response.acceptsStreaming) {
for await (const chunk of generateText()) {
console.log("got chunk", chunk);
response.write({chunk});
}
throw new Error("BOOM");
}
});
exports.genStreamError = functionsV2.https.onCall(
async (request, response) => {
if (request.acceptsStreaming) {
for await (const chunk of generateText()) {
response.sendChunk({chunk});
}
}
throw new Error("BOOM");
});

exports.genStreamNoReturn = functions.https.onCall(
exports.genStreamNoReturn = functionsV2.https.onCall(
async (request, response) => {
if (response && response.acceptsStreaming) {
if (request.acceptsStreaming) {
for await (const chunk of generateText()) {
console.log("got chunk", chunk);
response.write({chunk});
response.sendChunk({chunk});
}
}
},
Expand Down

0 comments on commit dd1bf7a

Please sign in to comment.