Skip to content

Commit d4c9b69

Browse files
author
Guy Bedford
authored
fix: null return instead of an error for missing geo data
1 parent e48ed70 commit d4c9b69

29 files changed

+276
-191
lines changed

.github/workflows/main.yml

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ jobs:
174174
with:
175175
path: "/home/runner/.cargo/bin/wasm-tools"
176176
key: crate-cache-wasm-tools-${{ env.wasm-tools_version }}
177-
- name: Build
178-
run: npm run build:debug
177+
- name: Build with full debug info
178+
run: npm run build:debug:info
179179
- uses: actions/upload-artifact@v3
180180
with:
181181
if-no-files-found: error
@@ -204,18 +204,18 @@ jobs:
204204
path: "/home/runner/.cargo/bin/wasm-tools"
205205
key: crate-cache-wasm-tools-${{ env.wasm-tools_version }}
206206
- name: Build
207-
if: ${{ matrix.profile == 'release' }}
207+
if: matrix.profile == 'release'
208208
run: npm run build
209209
- name: Build
210-
if: ${{ matrix.profile == 'weval' }}
210+
if: matrix.profile == 'weval'
211211
run: npm run build:weval
212212
- uses: actions/upload-artifact@v3
213213
with:
214214
if-no-files-found: error
215215
name: fastly-${{ matrix.profile }}
216216
path: fastly${{ matrix.profile == 'debug' && '.debug.wasm' || (matrix.profile == 'weval' && '-weval.wasm' || '.wasm') }}
217217
- uses: actions/upload-artifact@v3
218-
if: ${{ matrix.profile == 'weval' }}
218+
if: matrix.profile == 'weval'
219219
with:
220220
name: fastly-${{ matrix.profile }}-ic-cache
221221
path: fastly-ics.wevalcache
@@ -297,14 +297,14 @@ jobs:
297297
with:
298298
path: "/home/runner/.cargo/bin/viceroy"
299299
key: crate-cache-viceroy-${{ env.viceroy_version }}
300-
300+
301301
- name: Restore wasm-tools from cache
302302
uses: actions/cache@v3
303303
id: wasm-tools
304304
with:
305305
path: "/home/runner/.cargo/bin/wasm-tools"
306306
key: crate-cache-wasm-tools-${{ env.wasm-tools_version }}
307-
307+
308308
- run: npm install
309309

310310
- name: Build WPT runtime
@@ -347,13 +347,13 @@ jobs:
347347
cli_version: ${{ env.fastly-cli_version }}
348348

349349
- name: Restore Viceroy from cache
350-
if: ${{ matrix.platform == 'viceroy' }}
350+
if: matrix.platform == 'viceroy'
351351
uses: actions/cache@v3
352352
id: viceroy
353353
with:
354354
path: "/home/runner/.cargo/bin/viceroy"
355355
key: crate-cache-viceroy-${{ env.viceroy_version }}
356-
356+
357357
- name: Restore wasm-tools from cache
358358
uses: actions/cache@v3
359359
id: wasm-tools
@@ -367,7 +367,7 @@ jobs:
367367
name: fastly-${{ matrix.profile }}
368368
- name: Download Engine (AOT weval cache)
369369
uses: actions/download-artifact@v3
370-
if: ${{ matrix.profile == 'weval'}}
370+
if: matrix.profile == 'weval'
371371
with:
372372
name: fastly-${{ matrix.profile }}-ic-cache
373373

@@ -411,13 +411,13 @@ jobs:
411411
cli_version: ${{ env.fastly-cli_version }}
412412

413413
- name: Restore Viceroy from cache
414-
if: ${{ matrix.platform == 'viceroy' }}
414+
if: matrix.platform == 'viceroy'
415415
uses: actions/cache@v3
416416
id: viceroy
417417
with:
418418
path: "/home/runner/.cargo/bin/viceroy"
419419
key: crate-cache-viceroy-${{ env.viceroy_version }}
420-
420+
421421
- name: Restore wasm-tools from cache
422422
uses: actions/cache@v3
423423
id: wasm-tools
@@ -430,6 +430,10 @@ jobs:
430430
with:
431431
name: fastly-debug
432432

433+
- name: Strip debug sections (compute only)
434+
if: matrix.platform == 'compute'
435+
run: wasm-tools strip fastly.debug.wasm -d ".debug_(info|loc|ranges|abbrev|line|str)" -o fastly.debug.wasm
436+
433437
- name: Npm install
434438
run: npm install && cd ./integration-tests/js-compute && npm install
435439

@@ -442,4 +446,3 @@ jobs:
442446
run: SUFFIX_STRING=debug node integration-tests/js-compute/test.js --tla --debug-build ${{ matrix.platform == 'viceroy' && '--local' || '' }}
443447
env:
444448
FASTLY_API_TOKEN: ${{ secrets.FASTLY_API_TOKEN }}
445-

documentation/docs/geolocation/getGeolocationForIpAddress.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ getGeolocationForIpAddress(address)
2525

2626
### Return value
2727

28-
Returns an `Object` which contains information about the given IP address with the following properties:
28+
Returns an `Object`, or `null` if no geolocation data was found.
29+
30+
The object contains information about the given IP address with the following properties:
2931

3032
- `as_name` _: string | null_
3133
- The name of the organization associated with `as_number`.

integration-tests/js-compute/fixtures/app/.eslintrc.cjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ module.exports = {
22
env: {
33
es2021: true,
44
},
5-
extends: "eslint:recommended",
5+
extends: 'eslint:recommended',
66
overrides: [],
77
parserOptions: {
8-
ecmaVersion: "latest",
9-
sourceType: "module",
8+
ecmaVersion: 'latest',
9+
sourceType: 'module',
1010
},
1111
rules: {},
1212
};

integration-tests/js-compute/fixtures/app/setup.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22

3-
import { $ as zx } from "zx";
4-
import { argv } from "node:process";
3+
import { $ as zx } from 'zx';
4+
import { argv } from 'node:process';
55

66
const serviceName = argv[2];
77

@@ -15,10 +15,10 @@ if (process.env.FASTLY_API_TOKEN === undefined) {
1515
).trim();
1616
} catch {
1717
console.error(
18-
"No environment variable named FASTLY_API_TOKEN has been set and no default fastly profile exists.",
18+
'No environment variable named FASTLY_API_TOKEN has been set and no default fastly profile exists.',
1919
);
2020
console.error(
21-
"In order to run the tests, either create a fastly profile using `fastly profile create` or export a fastly token under the name FASTLY_API_TOKEN",
21+
'In order to run the tests, either create a fastly profile using `fastly profile create` or export a fastly token under the name FASTLY_API_TOKEN',
2222
);
2323
process.exit(1);
2424
}
@@ -36,7 +36,7 @@ async function setupConfigStores() {
3636
}
3737
})();
3838

39-
let STORE_ID = stores.find(({ name }) => name === "aZ1 __ 2")?.id;
39+
let STORE_ID = stores.find(({ name }) => name === 'aZ1 __ 2')?.id;
4040
if (!STORE_ID) {
4141
process.env.STORE_ID = JSON.parse(
4242
await zx`fastly config-store create --quiet --name='aZ1 __ 2' --json --token $FASTLY_API_TOKEN`,
@@ -48,10 +48,10 @@ async function setupConfigStores() {
4848
try {
4949
await zx`fastly resource-link create --service-name ${serviceName} --version latest --resource-id $STORE_ID --token $FASTLY_API_TOKEN --autoclone`;
5050
} catch (e) {
51-
if (!e.message.includes("Duplicate record")) throw e;
51+
if (!e.message.includes('Duplicate record')) throw e;
5252
}
5353

54-
STORE_ID = stores.find(({ name }) => name === "testconfig")?.id;
54+
STORE_ID = stores.find(({ name }) => name === 'testconfig')?.id;
5555
if (!STORE_ID) {
5656
process.env.STORE_ID = JSON.parse(
5757
await zx`fastly config-store create --quiet --name='testconfig' --json --token $FASTLY_API_TOKEN`,
@@ -63,7 +63,7 @@ async function setupConfigStores() {
6363
try {
6464
await zx`fastly resource-link create --service-name ${serviceName} --version latest --resource-id $STORE_ID --token $FASTLY_API_TOKEN --autoclone`;
6565
} catch (e) {
66-
if (!e.message.includes("Duplicate record")) throw e;
66+
if (!e.message.includes('Duplicate record')) throw e;
6767
}
6868
}
6969

@@ -93,7 +93,7 @@ async function setupKVStore() {
9393
try {
9494
await zx`fastly resource-link create --service-name ${serviceName} --version latest --resource-id $STORE_ID --token $FASTLY_API_TOKEN --autoclone`;
9595
} catch (e) {
96-
if (!e.message.includes("Duplicate record")) throw e;
96+
if (!e.message.includes('Duplicate record')) throw e;
9797
}
9898
}
9999

@@ -108,7 +108,7 @@ async function setupSecretStore() {
108108
}
109109
})();
110110
const STORE_ID = stores?.find(
111-
({ name }) => name === "example-test-secret-store",
111+
({ name }) => name === 'example-test-secret-store',
112112
)?.id;
113113
if (!STORE_ID) {
114114
process.env.STORE_ID = JSON.parse(
@@ -119,12 +119,12 @@ async function setupSecretStore() {
119119
}
120120
await zx`echo -n 'This is also some secret data' | fastly secret-store-entry create --recreate-allow --name first --store-id=$STORE_ID --stdin --token $FASTLY_API_TOKEN`;
121121
let key =
122-
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
122+
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
123123
await zx`echo -n 'This is some secret data' | fastly secret-store-entry create --recreate-allow --name ${key} --store-id=$STORE_ID --stdin --token $FASTLY_API_TOKEN`;
124124
try {
125125
await zx`fastly resource-link create --service-name ${serviceName} --version latest --resource-id $STORE_ID --token $FASTLY_API_TOKEN --autoclone`;
126126
} catch (e) {
127-
if (!e.message.includes("Duplicate record")) throw e;
127+
if (!e.message.includes('Duplicate record')) throw e;
128128
}
129129
}
130130

integration-tests/js-compute/fixtures/app/src/assertions.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export async function sleep(milliseconds) {
99
// TODO: Implement ReadableStream getIterator() and [@@asyncIterator]() methods
1010
export async function streamToString(stream) {
1111
const decoder = new TextDecoder();
12-
let string = "";
12+
let string = '';
1313
let reader = stream.getReader();
1414
// eslint-disable-next-line no-constant-condition
1515
while (true) {
@@ -32,16 +32,16 @@ export function iteratableToStream(iterable) {
3232
});
3333
}
3434

35-
export function pass(message = "") {
35+
export function pass(message = '') {
3636
return new Response(message);
3737
}
3838

39-
export function fail(message = "") {
39+
export function fail(message = '') {
4040
throw new Response(message, { status: 500 });
4141
}
4242

4343
function prettyPrintSymbol(a) {
44-
if (typeof a === "symbol") {
44+
if (typeof a === 'symbol') {
4545
return String(a);
4646
}
4747
return a;
@@ -54,7 +54,7 @@ export function assert(actual, expected, code) {
5454
}
5555
}
5656

57-
export { assert as strictEqual }
57+
export { assert as strictEqual };
5858

5959
export function ok(truthy, code) {
6060
if (!truthy) {
@@ -134,7 +134,7 @@ export function assertDoesNotThrow(func) {
134134
}
135135
}
136136

137-
export { deepEqual as deepStrictEqual }
137+
export { deepEqual as deepStrictEqual };
138138

139139
export function deepEqual(a, b) {
140140
var aKeys;
@@ -146,15 +146,15 @@ export function deepEqual(a, b) {
146146

147147
typeA = typeof a;
148148
typeB = typeof b;
149-
if (a === null || typeA !== "object") {
150-
if (b === null || typeB !== "object") {
149+
if (a === null || typeA !== 'object') {
150+
if (b === null || typeB !== 'object') {
151151
return a === b;
152152
}
153153
return false;
154154
}
155155

156156
// Case: `a` is of type 'object'
157-
if (b === null || typeB !== "object") {
157+
if (b === null || typeB !== 'object') {
158158
return false;
159159
}
160160
if (Object.getPrototypeOf(a) !== Object.getPrototypeOf(b)) {

integration-tests/js-compute/fixtures/app/src/cache-override.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { CacheOverride } from 'fastly:cache-override';
2-
import {
3-
assert,
4-
assertThrows,
5-
assertDoesNotThrow,
6-
} from './assertions.js';
2+
import { assert, assertThrows, assertDoesNotThrow } from './assertions.js';
73
import { isRunningLocally, routes } from './routes.js';
84

95
// CacheOverride constructor

integration-tests/js-compute/fixtures/app/src/device.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ routes.set('/device/interface', () => {
247247
`typeof Reflect.getOwnPropertyDescriptor(Device, '${property}').get`,
248248
);
249249
}
250-
251250
});
252251

253252
// Device constructor
@@ -359,7 +358,6 @@ routes.set('/device/interface', () => {
359358
assert(device.isSmartTV, false, `device.isSmartTV`);
360359
assert(device.isTablet, false, `device.isTablet`);
361360
assert(device.isTouchscreen, true, `device.isTouchscreen`);
362-
363361
},
364362
);
365363
routes.set('/device/lookup/useragent-exists-some-fields-identified', () => {

integration-tests/js-compute/fixtures/app/src/fanout.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
assert,
3-
assertDoesNotThrow,
4-
assertThrows,
5-
} from './assertions.js';
1+
import { assert, assertDoesNotThrow, assertThrows } from './assertions.js';
62
import { routes } from './routes.js';
73
import { createFanoutHandoff } from 'fastly:fanout';
84

integration-tests/js-compute/fixtures/app/src/geoip.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-env serviceworker */
22
/* global fastly */
3-
import { assert, assertThrows } from './assertions.js';
3+
import { assert, assertThrows, strictEqual } from './assertions.js';
44
import { getGeolocationForIpAddress } from 'fastly:geolocation';
55
import { isRunningLocally, routes } from './routes.js';
66

@@ -127,9 +127,9 @@ routes.set(
127127
);
128128

129129
let geoFields = [
130-
'area_code',
131130
'as_name',
132131
'as_number',
132+
'area_code',
133133
'city',
134134
'conn_speed',
135135
'conn_type',
@@ -148,15 +148,33 @@ let geoFields = [
148148
'utc_offset',
149149
];
150150

151+
routes.set('/fastly/getgeolocationforipaddress/bad-ip', async () => {
152+
let geo = fastly.getGeolocationForIpAddress('0.0.0.0');
153+
if (isRunningLocally()) {
154+
strictEqual(
155+
geo,
156+
null,
157+
`fastly.getGeolocationForIpAddress('0.0.0.0') == null`,
158+
);
159+
} else {
160+
strictEqual(geo.as_name, '?');
161+
}
162+
assertThrows(() => {
163+
fastly.getGeolocationForIpAddress('999.999.999.999');
164+
}, Error);
165+
});
166+
151167
routes.set(
152168
'/fastly/getgeolocationforipaddress/parameter-ipv4-string',
153169
async () => {
170+
let geo = fastly.getGeolocationForIpAddress('151.101.1.1');
154171
if (isRunningLocally()) {
155-
let geo = fastly.getGeolocationForIpAddress('2.216.196.179');
172+
strictEqual(geo, null);
173+
} else {
156174
assert(
157175
Object.keys(geo),
158176
geoFields,
159-
`Object.keys(fastly.getGeolocationForIpAddress('2.216.196.179')) == geoFields`,
177+
`Object.keys(fastly.getGeolocationForIpAddress('151.101.1.1')) == geoFields`,
160178
);
161179
}
162180
},

0 commit comments

Comments
 (0)