Cache every fetch request in a Cloudflare worker - cloudflare

I am bulding a cloudflare worker and I want to cache a fetch request for at least 24 hours. This mean if I make the same request twice within 24h the fetch() should not be called and used the cached response.
I've written this script, but the remote website (unixtimestamp.com) it's called every time.
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
});
async function handleRequest(request) {
const url = 'https://www.unixtimestamp.com/'
let response = await fetch(url, {
cf: {
cacheTtlByStatus: { "200-299": 60*60*24, 404: -1, "500-599": -1 }
}
})
return response
}
Documentation: https://developers.cloudflare.com/workers/examples/cache-using-fetch

I'm wondering if Page rules is more efficient in this case than workers in your case ? (moreover, workers count calls can be limited depending on your plan)
I have done once in page rules (and is easily configurable) with Cache TTL by status code option.

Related

How do I check network response in playwright?

I want to test the network https status response with playwright.
test.only('Rights.2: Users without the required author role do not have access.', async ({page}) => {
await login(page, 'xxx', "password.xxx");
const search = await searchForProcess(page, `${process.process1.title}`);
await login(page, 'PortalUser');
const open = await openProcess(page, `${process.process1.title}`);
});
So I tried with this
expect(response.status()).toEqual(403);
But it won't work, because response is not defined. Thats curious, because Playwright does document the "response.status()" as a function.
Can somebody help?
You could try page.waitForResponse(). Official docs:
https://playwright.dev/docs/api/class-page#page-wait-for-response
Here is an example where we click a button and we want to monitor that certain API calls actually happen and that they have certain response statuses:
// Start waiting for all required API calls as we select company.
// We can accept only 200 but we could accept 403 like with /api/Environment/
await Promise.all([
page.waitForResponse(resp => resp.url().includes('/api/Environment/') && (resp.status() === 200 || resp.status() === 403)),
page.waitForResponse(resp => resp.url().includes('/api/Security/') && (resp.status() === 200)),
// We already started waiting before we perform the click that triggers the API calls. So now we just perform the click
page.locator('div[role="gridcell"]:has-text("text")').click()
]);
If you wish to validate other reponse data, see: https://playwright.dev/docs/api/class-response
If you want to monitor all network traffic, this official docs would be more suitable: https://playwright.dev/docs/network#network-events

Axios onUploadProgress jumps at 100% even before the request is done

I have a controller that calls multiple external api from Gmail API concurrently, and then saves the retrieved data into the database, all of this in ONE post request.
To start, I pooled the external api requests, map the results and save it to db.
In my Controller:
$resp = Http::pool(fn (Pool $pool) [
$pool->as('pool-1')->withToken($user->token)->get('https://....call-1');
$pool->as('pool-2')->withToken($user->token)->get('https://....call-2');
$pool->as('pool-3')->withToken($user->token)->get('https://....call-3');
]);
collect($resp)->map(function ($req) {
Email::firstOrCreate(...);
})
In my vue component:
const config = {
onUploadProgress: progressEvent => {
console.log(`sent: ${Math.floor((progressEvent.loaded * 100) / progressEvent.total)}`)
}
}
axios.post(url, param, config).then(response => {}).catch(err => {})
Now, when I check in console.
It should be logging:
sent:0
sent:1
sent:2
...
sent:100
But instead, it automatically logs sent:100 even though the post request is still pending.
Is this a bug in chrome? or axios?, or perhaps it has something to do with external api calls?
Or if it isn't, can someone point out where I went wrong?

calculate packet send/receive time

i am using vue.js for webrtc client. My system has multi servers so i need to pick one and connect. I am getting server list from my webservice. Everything is ok till here, i want user connect to best server so decided to make custom ping (its called rtt, i think).
to sum up my aim is send a udp packet to server(sendTime in long), then server receives this packet and now server sends my a packet(receiveTime in long) => this servers ping is receiveTime - sendTime
So i will do this for every server and choose best server.Is it posible to send and receive udp packet or anyone got better idea?
In my opinion the simplest way would be to make a get request to every url, then do a promise race on the requests, which one returns the fastest is the server you would use.
const urls = ['https://www.google.com/', 'https://www.facebook.com/', 'https://www.twitter.com/', 'https://www.instagram.com/'];
const serverPromises = [];
urls.forEach((url) => {
serverPromises.push(
new Promise((resolve, reject) => {
fetch(url, {
mode: 'no-cors'
}).then((response) => {
if ([400, 500, 404].includes(response.status)) {
return reject(null);
}
return resolve(url);
})
})
);
});
Promise.race(serverPromises).then((url) => {
if (url) {
console.log('URL to use: ', url);
}
});
Check it out, see if it suits your needs!
To me this seems to be the easiest, and quickest way, without any calculations, plus you get the result as soon as the first request resolves, this way you don't have to wait for every request to resolve.

Response Time - NodeJS + Express

I have a little problem with my application.
My architecture:
Angular 6 (front)
NodeJS + Express + MongoDB (back)
There is a part, in my NodeJS application, that communicates with a REST API.
When an user clicks on button on the Angular WebSite, I send a request to Express.
In my Express application, I send another request to the API to retrieve information.
But this process takes a lot of time. My request timeout
What is the best solution to keep my process working after the Express reply has been sent?
Should I do otherwise?
Assuming the problem is with timeout, you can increase the default timeout:
You may set the timeout either globally for entire server. Read more here:
var server = app.listen(app.get('port'), function() {
//Server is running
});
server.timeout = 1000; //1000 is one second.
or just for specific route. Read more here:
app.post('/xxx', function (req, res) {
req.setTimeout(500000);
});
You can also change the timeout for the other API request you are making. Read more here
//Assuming you are using request module.
var options = {
url: 'http://url',
timeout: 120000
}
request(options, function(err, resp, body) {});

Express.io can not emit events when routed from http request

Here i use those codes:
// Initial web request.
app.get('/hello', function(req, res) {
// Forward to an io route.
req.io.route('hello')
})
app.io.route('hello', function(req) {
//Here use emit
req.io.emit("world","world");
})
it report an error as follow:
TypeError: Object #<Object> has no method 'emit'
at Object.hello (/Users/wensonsmith/ProjectX/Server/app.js:44:12)
at Manager.io.route (/Users/wensonsmith/ProjectX/Server/node_modules/express.io/lib/index.coffee:65:29)
at Object.request.io.route (/Users/wensonsmith/ProjectX/Server/node_modules/express.io/lib/index.coffee:143:29)
at /Users/wensonsmith/ProjectX/Server/app.js:39:12
at callbacks (/Users/wensonsmith/ProjectX/Server/node_modules/express.io/node_modules/express/lib/router/index.js:160:37)
at param (/Users/wensonsmith/ProjectX/Server/node_modules/express.io/node_modules/express/lib/router/index.js:134:11)
at pass (/Users/wensonsmith/ProjectX/Server/node_modules/express.io/node_modules/express/lib/router/index.js:141:5)
at Router._dispatch (/Users/wensonsmith/ProjectX/Server/node_modules/express.io/node_modules/express/lib/router/index.js:169:5)
at Object.router (/Users/wensonsmith/ProjectX/Server/node_modules/express.io/node_modules/express/lib/router/index.js:32:10)
at next (/Users/wensonsmith/ProjectX/Server/node_modules/express.io/node_modules/connect/lib/proto.js:190:15)
req.io.respond is OK .
Broadcast is also have some problem.It can broadcast ,but it doesn't stop after broadcast.
it run for a long while ,then return nothing ,and no error messages.
My code is
// Initial web request.
app.get('/hello', function(req, res) {
// Forward to an io route.
req.io.route('hello')
})
// Forward io route to another io route.
app.io.route('hello', function(req) {
req.io.broadcast("world","world");
})
It doesn't look like the code you're posting is the actual code, judging by the stack trace.
But apart from that: as far as I understand express.io, when you're forwarding an HTTP request to an io route, the io route should always send back a response with respond; otherwise, the HTTP request will stall.
So try this:
app.get('/hello', function(req, res) {
req.io.route('hello');
});
app.io.route('hello', function(req) {
// broadcast first...
req.io.broadcast("world","world");
// ...then send back an empty response
req.io.respond();
});
Just to make sure: req.io.broadcast will not send the message back to the client that initiated the request. If you want that, use app.io.broadcast instead (see docs).
Its only 50% answered ;) ::
.respond takes your arguments "directly" to emit them,
e.g.:
req.io.respond({hello: 'world'})