-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
67 lines (56 loc) · 2.23 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const secrets = require('./secrets.json');
const deploy = require('.');
const fetch = require('node-fetch');
void async function () {
// https://dash.cloudflare.com/${accountId}
const accountId = secrets.accountId;
if (!accountId) {
throw new Error('Account ID must be supplied in the accountId key of secrets.json.');
}
const workerName = secrets.workerName;
if (!workerName) {
throw new Error('Worker name must be supplied in the workerName key of secrets.json.');
}
// https://dash.cloudflare.com/${accountId}/profile/api-tokens
const apiToken = secrets.apiToken;
if (!apiToken) {
throw new Error('API token must be supplied in the apiToken key of secrets.json.');
}
const binding = secrets.bindings[0];
if (!binding) {
throw new Error('Binding must be supplied in the bindings key of secrets.json.');
}
// https://${workerName}.${userName}.workers.dev
const userName = secrets.userName;
if (!userName) {
throw new Error('User name must be supplied in the userName key of secrets.json.');
}
const nonce = Math.random();
const script = `
addEventListener('fetch', event => event.respondWith(handleRequest(event.request)));
async function handleRequest(request) {
await ${binding.name}.put('nonce', ${nonce});
const nonce = await ${binding.name}.get('nonce');
return new Response('hello world ' + nonce, { status: 200 });
}
`;
await deploy(accountId, workerName, apiToken, script, ...secrets.bindings);
const attempts = 10;
console.log('Nonce', nonce);
for (let attempt = 1; attempt <= attempts; attempt++) {
const response = await fetch(`https://${workerName}.${userName}.workers.dev`);
const text = await response.text();
if (text !== `hello world ${nonce}`) {
console.log('Attempt', attempt, 'mismatch:', text);
if (attempt === 10) {
throw new Error(`The worker did not have the expected response. Expected 'hello world ${nonce}'. Got '${text}'.`);
}
}
else {
console.log('Attempt', attempt, 'success');
break;
}
// Wait for a bit to give the worker a chance to finish deployment and try again
await new Promise(resolve => setTimeout(resolve, 1000));
}
}()