Skip to content

Commit 2254f09

Browse files
committed
feat: use client registration token for ndt7 tests
Update ndt7-js to 0.1.3 which supports the `clientRegistrationToken` option. Before running ndt7 tests, fetch a short-lived JWT token from the speed-backend service which enables priority access to the Locate API for registered integrations. This is the first step toward using the client registration system for M-Lab's speed test website along with the new backend that we have written for this purpose: https://github.com/m-lab/speed-proxy. We implement an open failure model where failure to get the token causes ndt7 to avoid using the client-integration code. Also, update `yarn.lock`. Also, distinguish between staging and production.
1 parent 9f7363f commit 2254f09

File tree

5 files changed

+689
-763
lines changed

5 files changed

+689
-763
lines changed

.github/workflows/firebase-hosting-merge.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ jobs:
1212
steps:
1313
- uses: actions/checkout@v4
1414
- run: npm install -g yarn && yarn install --frozen-lockfile && yarn build
15+
- name: Substitute M-Lab project placeholder
16+
run: sed -i 's/MLAB_PROJECT_PLACEHOLDER/mlab-oti/g' app/measure/measure.js
1517
- uses: FirebaseExtended/action-hosting-deploy@v0
1618
with:
1719
repoToken: '${{ secrets.GITHUB_TOKEN }}'

.github/workflows/firebase-hosting-pull-request.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ jobs:
1010
steps:
1111
- uses: actions/checkout@v4
1212
- run: npm install -g yarn && yarn install --frozen-lockfile && yarn build
13+
- name: Substitute M-Lab project placeholder
14+
run: sed -i 's/MLAB_PROJECT_PLACEHOLDER/mlab-sandbox/g' app/measure/measure.js
1315
- uses: FirebaseExtended/action-hosting-deploy@v0
1416
with:
1517
repoToken: '${{ secrets.GITHUB_TOKEN }}'

app/measure/measure.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,44 @@ angular.module('Measure.Measure', ['ngRoute'])
6767
testRunning = false;
6868
}
6969

70+
// Determine the M-Lab project based on a placeholder that is substituted
71+
// during deployment. If the placeholder is not substituted (e.g., local
72+
// development), default to sandbox for safe testing.
73+
function mlabProject() {
74+
const placeholder = 'MLAB_PROJECT_PLACEHOLDER';
75+
return placeholder.includes('PLACEHOLDER') ? 'mlab-sandbox' : placeholder;
76+
}
77+
78+
// Build the locate service priority URL for the given project. Production uses
79+
// locate.measurementlab.net while sandbox uses locate.mlab-sandbox.measurementlab.net.
80+
function locatePriorityURLForProject(project) {
81+
const host = project === 'mlab-oti'
82+
? 'locate.measurementlab.net'
83+
: `locate.${project}.measurementlab.net`;
84+
return `https://${host}/v2/priority/nearest/ndt/ndt7`;
85+
}
86+
7087
async function runNdt7(sid) {
88+
// Fetch a short-lived token from the speed-backend service to enable
89+
// priority access to the Locate API for registered integrations.
90+
// If token fetch fails, gracefully degrade to running without a token.
91+
const project = mlabProject();
92+
const tokenURL = `https://speed-backend.${project}.measurementlab.net/v0/token`;
93+
const locatePriorityURL = locatePriorityURLForProject(project);
94+
95+
let token = null;
96+
try {
97+
const tokenResp = await fetch(tokenURL);
98+
const tokenData = await tokenResp.json();
99+
token = tokenData.token;
100+
} catch (err) {
101+
console.warn('Failed to fetch token, running without priority access:', err);
102+
}
103+
71104
return ndt7.test(
72105
{
106+
clientRegistrationToken: token,
107+
loadbalancer: token ? locatePriorityURL : null,
73108
userAcceptedDataPolicy: true,
74109
uploadworkerfile: "/libraries/ndt7-upload-worker.min.js",
75110
downloadworkerfile: "/libraries/ndt7-download-worker.min.js",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"@bower_components/jquery": "jquery/jquery-dist#3.0.0",
3636
"@bower_components/skel": "n33/skel#~3.0.1",
3737
"@m-lab/msak": "0.3.1",
38-
"@m-lab/ndt7": "0.0.6",
38+
"@m-lab/ndt7": "0.1.3",
3939
"@m-lab/packet-test": "0.0.16",
4040
"ng-device-detector": "^5.1.4",
4141
"re-tree": "^0.1.7",

0 commit comments

Comments
 (0)