Skip to content

Commit 5680cef

Browse files
Merge pull request #16 from fingerprintjs/refactor/plugin-system
refactor: change open client response flag name
2 parents 772cd2b + 6c85ea8 commit 5680cef

File tree

14 files changed

+91
-48
lines changed

14 files changed

+91
-48
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ This is a quick overview of the installation setup. For detailed step-by-step in
3636

3737
2. [Create an empty Compute Service](https://docs.fastly.com/en/guides/working-with-compute-services#creating-a-new-compute-service) in your Fastly account.
3838

39-
3. [Create a Config store](https://docs.fastly.com/en/guides/working-with-config-stores#creating-a-config-store) named `Fingerprint_Fastly_Compute_Proxy_Integration_ConfigStore_<SERVICE_ID>`, where the suffix is your proxy integration's [Compute Service ID](https://docs.fastly.com/en/guides/about-services). Add the following values:
39+
3. [Create a Config store](https://docs.fastly.com/en/guides/working-with-config-stores#creating-a-config-store) named `Fingerprint_Compute_Config_Store_<SERVICE_ID>`, where the suffix is your proxy integration's [Compute Service ID](https://docs.fastly.com/en/guides/about-services). Add the following values:
4040

4141
| Key | Example Value | Description |
4242
| ---------------------------- | ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
4343
| AGENT_SCRIPT_DOWNLOAD_PATH | z5kms2 | Random path segment for downloading the JavaScript agent. |
4444
| GET_RESULT_PATH | nocmjw | Random path segment for Fingerprint identification requests. |
45-
| OPEN_CLIENT_RESPONSE_ENABLED | false | Set to `true` if you have [Open client response](https://dev.fingerprint.com/docs/open-client-response) enabled for your Fingerprint application. Defaults to `false`. |
45+
| OPEN_CLIENT_RESPONSE_PLUGINS_ENABLED | false | Set to `true` if you have [Open client response](https://dev.fingerprint.com/docs/open-client-response) enabled for your Fingerprint application. Defaults to `false`. |
4646

47-
4. [Create a Secret store](https://docs.fastly.com/en/guides/working-with-secret-stores#creating-a-secret-store) named `Fingerprint_Fastly_Compute_Proxy_Integration_SecretStore_<SERVICE_ID>`, where the suffix is your proxy integration's [Compute Service ID](https://docs.fastly.com/en/guides/about-services). Add your proxy secret:
47+
4. [Create a Secret store](https://docs.fastly.com/en/guides/working-with-secret-stores#creating-a-secret-store) named `Fingerprint_Compute_Secret_Store_<SERVICE_ID>`, where the suffix is your proxy integration's [Compute Service ID](https://docs.fastly.com/en/guides/about-services). Add your proxy secret:
4848

4949
| Key | Example Value | Description |
5050
| ------------ | -------------------- | --------------------------------------------- |
@@ -75,8 +75,8 @@ See the [Fastly Compute proxy integration guide](https://dev.fingerprint.com/doc
7575

7676
By default, the service package provided in releases assumes the following names for the Config store and Secret Store:
7777

78-
* `Fingerprint_Fastly_Compute_Proxy_Integration_ConfigStore_<SERVICE_ID>`
79-
* `Fingerprint_Fastly_Compute_Proxy_Integration_SecretStore_<SERVICE_ID>`
78+
* `Fingerprint_Compute_Config_Store_<SERVICE_ID>`
79+
* `Fingerprint_Compute_Secret_Store_<SERVICE_ID>`
8080

8181
To use a custom name prefix for both stores, use the `STORE_NAME_PREFIX` environment variable to build a custom service package:
8282

@@ -86,8 +86,8 @@ STORE_NAME_PREFIX=CustomName pnpm run build
8686

8787
Your custom built package in `pkg/package.tar.gz` will use your custom prefix in store names like:
8888

89-
* `CustomName_ConfigStore_<SERVICE_ID>`
90-
* `CustomName_SecretStore_<SERVICE_ID>`
89+
* `CustomName_Config_Store_<SERVICE_ID>`
90+
* `CustomName_Secret_Store_<SERVICE_ID>`
9191

9292

9393
## Feedback and support

__mocks__/fastly:kv-store.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export class KVStore {
2+
async put(name, value) {
3+
return { name, value }
4+
}
5+
}

build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { build } from 'esbuild'
22

33
// Load environment variables from process.env
4-
const configStoreNamePrefix = process.env.STORE_NAME_PREFIX || 'Fingerprint_Fastly_Compute_Proxy_Integration'
4+
const configStoreNamePrefix = process.env.STORE_NAME_PREFIX || 'Fingerprint_Compute'
55

66
build({
77
entryPoints: ['./src/index.ts'],

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"@fingerprintjs/commit-lint-dx-team": "^0.1.0",
1313
"@fingerprintjs/conventional-changelog-dx-team": "^0.1.0",
1414
"@fingerprintjs/eslint-config-dx-team": "^0.1.0",
15-
"@fingerprintjs/fingerprintjs-pro-server-api": "^5.0.0",
1615
"@fingerprintjs/prettier-config-dx-team": "^0.2.0",
1716
"@fingerprintjs/tsconfig-dx-team": "^0.0.2",
1817
"@jest/globals": "^29.7.0",
@@ -62,5 +61,8 @@
6261
"commitizen": {
6362
"path": "./node_modules/cz-conventional-changelog"
6463
}
64+
},
65+
"peerDependencies": {
66+
"@fingerprintjs/fingerprintjs-pro-server-api": "^5.2.0"
6567
}
6668
}

plugins/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// import { saveFingerprintResultToKVStore } from './saveToKVStore'
1+
import { saveFingerprintResultToKVStore } from './saveToKVStore'
22
import { Plugin } from '../src/utils/registerPlugin'
33

44
export default [
5-
// {
6-
// name: 'Save Fingerprint Result to KV Store',
7-
// callback: saveFingerprintResultToKVStore,
8-
// type: 'processOpenClientResponse',
9-
// },
5+
{
6+
name: 'Save Fingerprint Result to KV Store',
7+
callback: saveFingerprintResultToKVStore,
8+
type: 'processOpenClientResponse',
9+
},
1010
] satisfies Plugin[]

plugins/saveToKVStore.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,23 @@
22

33
import { KVStore } from 'fastly:kv-store'
44
import { ProcessOpenClientResponseContext } from '../src/utils/registerPlugin'
5+
import { getConfigStore } from '../src/utils/getStore'
6+
import { env } from 'fastly:env'
57
export async function saveFingerprintResultToKVStore(context: ProcessOpenClientResponseContext) {
8+
const configStore = getConfigStore()
9+
const isPluginEnabled = configStore?.get('SAVE_TO_KV_STORE_PLUGIN_ENABLED') === 'true'
10+
11+
if (!isPluginEnabled) {
12+
console.log("Plugin 'saveFingerprintResultToKVStore' is not enabled")
13+
return
14+
}
15+
616
const requestId = context.event?.products.identification?.data?.requestId
717
if (!requestId) {
18+
console.log('[saveFingerprintResultToKVStore] Plugin Error: request ID is undefined in the event response.')
819
return
920
}
10-
const store = new KVStore('FingerprintResults')
21+
const serviceId = env('FASTLY_SERVICE_ID')
22+
const store = new KVStore(`Fingerprint_Results_${serviceId}`)
1123
await store.put(requestId, JSON.stringify(context.event))
1224
}

pnpm-lock.yaml

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/api/createService.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ async function linkStoreResource(
8282
resource_id: string,
8383
type: 'secret' | 'config' = 'config'
8484
) {
85-
const storeNameWithPrefix = `${STORE_NAME_PREFIX}_${type === 'config' ? 'ConfigStore' : 'SecretStore'}_${service_id}`
85+
const storeNameWithPrefix = `${STORE_NAME_PREFIX}_${type === 'config' ? 'Config_Store' : 'Secret_Store'}_${service_id}`
8686
return createClient('resource').createResource({
8787
service_id,
8888
version_id,
@@ -93,7 +93,7 @@ async function linkStoreResource(
9393

9494
async function createSecretStore(service_id: string) {
9595
console.log('Creating secret store')
96-
const secretStoreNameWithPrefix = `${STORE_NAME_PREFIX}_SecretStore_${service_id}`
96+
const secretStoreNameWithPrefix = `${STORE_NAME_PREFIX}_Secret_Store_${service_id}`
9797
const secretStoreClient = createClient('secretStore')
9898
const secretStoreItemClient = createClient('secretStoreItem')
9999
let secretStore
@@ -122,7 +122,7 @@ async function createSecretStore(service_id: string) {
122122

123123
async function createConfigStore(service_id: string) {
124124
console.log('Creating config store')
125-
const configStoreNameWithPrefix = `${STORE_NAME_PREFIX}_ConfigStore_${service_id}`
125+
const configStoreNameWithPrefix = `${STORE_NAME_PREFIX}_Config_Store_${service_id}`
126126
const configStoreClient = createClient('configStore')
127127
const configStoreItemClient = createClient('configStoreItem')
128128
let configStore
@@ -147,7 +147,7 @@ async function createConfigStore(service_id: string) {
147147
})
148148
await configStoreItemClient.createConfigStoreItem({
149149
config_store_id: configStore.id,
150-
item_key: 'OPEN_CLIENT_RESPONSE_ENABLED',
150+
item_key: 'OPEN_CLIENT_RESPONSE_PLUGINS_ENABLED',
151151
item_value: 'false',
152152
})
153153

src/env.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ export type IntegrationEnv = {
22
AGENT_SCRIPT_DOWNLOAD_PATH: string | null
33
GET_RESULT_PATH: string | null
44
PROXY_SECRET: string | null
5-
OPEN_CLIENT_RESPONSE_ENABLED: string | null
5+
OPEN_CLIENT_RESPONSE_PLUGINS_ENABLED: string | null
66
DECRYPTION_KEY: string | null
77
}
88

99
const Defaults: IntegrationEnv = {
1010
AGENT_SCRIPT_DOWNLOAD_PATH: 'agent',
1111
GET_RESULT_PATH: 'result',
1212
PROXY_SECRET: null,
13-
OPEN_CLIENT_RESPONSE_ENABLED: 'false',
13+
OPEN_CLIENT_RESPONSE_PLUGINS_ENABLED: 'false',
1414
DECRYPTION_KEY: null,
1515
}
1616

@@ -55,9 +55,9 @@ export const decryptionKeyVarName = 'DECRYPTION_KEY'
5555
const getDecryptionKeyVar = getVarOrDefault(decryptionKeyVarName, Defaults)
5656
export const isDecryptionKeySet = isVarSet(decryptionKeyVarName)
5757

58-
export const openClientResponseVarName = 'OPEN_CLIENT_RESPONSE_ENABLED'
58+
export const openClientResponseVarName = 'OPEN_CLIENT_RESPONSE_PLUGINS_ENABLED'
5959
export const isOpenClientResponseSet = (env: IntegrationEnv) =>
60-
env.OPEN_CLIENT_RESPONSE_ENABLED === 'true' || env.OPEN_CLIENT_RESPONSE_ENABLED === 'false'
60+
env.OPEN_CLIENT_RESPONSE_PLUGINS_ENABLED === 'true' || env.OPEN_CLIENT_RESPONSE_PLUGINS_ENABLED === 'false'
6161

6262
export const isOpenClientResponseEnabled = (env: IntegrationEnv) =>
6363
env[openClientResponseVarName]?.toLowerCase() === 'true'

src/handlers/handleIngressAPI.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,22 @@ async function makeIngressRequest(receivedRequest: Request, env: IntegrationEnv)
2929
const response = await fetch(request, { backend: getIngressBackendByRegion(url) })
3030

3131
if (!isOpenClientResponseEnabled(env)) {
32+
console.log(
33+
"Open client response plugings are disabled. Set OPEN_CLIENT_RESPONSE_PLUGINS_ENABLED to `true` in your proxy integration's Config store to enable them."
34+
)
3235
return response
3336
}
3437

35-
const responseBody = await response.text()
36-
37-
processOpenClientResponse(responseBody, response, env).catch((e) =>
38-
console.error('failed when processing open client response', e)
39-
)
38+
console.log('Plugin system for Open Client Response is enabled')
39+
if (response.status >= 200 && response.status < 300) {
40+
const responseBody = await response.text()
41+
processOpenClientResponse(responseBody, response, env).catch((e) =>
42+
console.error('Processing open client response failed: ', e)
43+
)
44+
return cloneFastlyResponse(responseBody, response)
45+
}
4046

41-
return cloneFastlyResponse(responseBody, response)
47+
return response
4248
}
4349

4450
function makeCacheEndpointRequest(receivedRequest: Request, routeMatches: RegExpMatchArray | undefined) {

0 commit comments

Comments
 (0)