Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - fastly support #35

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public/dist
.turbo
wrangler.toml
**/coverage/**
.wrangler
bin/

**/fastly/**/pkg/

# ignore all .vscode folders unless they're in the root directory
.vscode
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@
"prettier": "^2.5.1",
"turbo": "latest"
},
"packageManager": "[email protected]"
"packageManager": "[email protected]",
"resolutions": {
"@segment/edge-sdk": "workspace:*"
}
}
3 changes: 3 additions & 0 deletions packages/edge-sdk-integration-tests/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ["@segment/edge-sdk"],
};
5 changes: 5 additions & 0 deletions packages/edge-sdk-integration-tests/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
22 changes: 22 additions & 0 deletions packages/edge-sdk-integration-tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@segment/edge-sdk-integration-tests",
"version": "0.0.0",
"private": true,
"license": "MIT",
"type": "module",
"scripts": {
"test": "jest"
},
"dependencies": {
"@segment/edge-sdk": "*"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20231002.0",
"@fastly/js-compute": "^3.7.3",
"@segment/eslint-config-edge-sdk": "*",
"@types/jest": "^29.5.9",
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"wrangler": "^3.17.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { join as joinPath } from "path";
import { unstable_dev } from "wrangler";
import { Service } from '../service';

export async function startService(): Promise<Service> {
const worker = await unstable_dev(joinPath(__dirname, "workers", "index.ts"), {
experimental: { disableExperimentalWarning: true, },
bundle: true,
nodeCompat: false,
config: joinPath(__dirname, "workers", "wrangler.test.toml"),
});

return {
fetch: worker.fetch.bind(worker) as any as Service['fetch'],
stop: worker.stop.bind(worker),
edgeSdkSettings: {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// <reference types="@cloudflare/workers-types" />
import { Segment } from '@segment/edge-sdk'

const EdgeSettingsHeader = 'x-segment-edge-settings';
const EdgeFeaturesHeader = "x-segment-edge-features";

export default {
async fetch(request: Request, _env: {}, _ctx: ExecutionContext) {
console.log(request.url)
const settingsHeader = request.headers.get(EdgeSettingsHeader);
const featuresHeader = request.headers.get(EdgeFeaturesHeader);
const settings = settingsHeader ? JSON.parse(settingsHeader) : undefined;
const features = featuresHeader ? JSON.parse(featuresHeader) : undefined;
const segment = new Segment(settings, features)
const response = await segment.handleEvent(request);
return response;
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name = "test-worker"
compatibility_date = "2022-11-30"

[dev]
# Mock Origin server
host = "localhost:3001"
14 changes: 14 additions & 0 deletions packages/edge-sdk-integration-tests/src/fastly/service/fastly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language = "javascript"
manifest_version = 3
name = "test-fastly"

[local_server]
[local_server.backends.origin]
url = "http://localhost:3001"
[local_server.backends.segment-cdn]
url = "http://localhost:3002"
[local_server.backends.tracking-api]
url = "http://localhost:3003"

[scripts]
build = "npx js-compute-runtime ./src/fastly/service/index.js ./src/fastly/service/bin/main.wasm"
22 changes: 22 additions & 0 deletions packages/edge-sdk-integration-tests/src/fastly/service/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// <reference types="@fastly/js-compute" />
import { Segment } from '@segment/edge-sdk';

const EdgeSettingsHeader = 'x-segment-edge-settings';
const EdgeFeaturesHeader = "x-segment-edge-features";

addEventListener('fetch', (event) => event.respondWith(handleRequest(event)));

/**
* @param {FetchEvent} event
* @returns {Promise<Response>}
*/
async function handleRequest(event) {
const { request } = event;
const settingsHeader = request.headers.get(EdgeSettingsHeader);
const featuresHeader = request.headers.get(EdgeFeaturesHeader);
const settings = settingsHeader ? JSON.parse(settingsHeader) : undefined;
const features = featuresHeader ? JSON.parse(featuresHeader) : undefined;
const segment = new Segment(settings, features)
const response = await segment.handleEvent(request);
return response;
}
60 changes: 60 additions & 0 deletions packages/edge-sdk-integration-tests/src/fastly/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { spawn } from 'node:child_process';
import { join as joinPath } from "node:path";
import { Service } from "../service";

export async function startService(): Promise<Service> {
const abortController = new AbortController();
const host = '127.0.0.1:3010';
const fastly = spawn('fastly', ['compute', 'serve', '--addr', host, '--dir', joinPath(__dirname, 'service')], {
signal: abortController.signal
});

let serverRunningResolver: () => void;
const serverRunning = new Promise<void>((resolve) => {
serverRunningResolver = resolve;
});

fastly.stderr.on('data', (data) => {
// console.error(`stderr: ${data}`);
});
fastly.stdout.on('data', (data) => {
if (data.toString().includes('Listening on http://127.0.0.1:3010')) {
serverRunningResolver();
}
console.log(`stdout: ${data}`);
});

await serverRunning;

return {
fetch: (...args) => {
if (typeof args[0] === 'string') {
const url = new URL(args[0]);
url.host = host;
url.protocol = 'http';
args[0] = url.toString();
} else {
const request = args[0];
const url = new URL(request.url);
url.host = host;
url.protocol = 'http';
args[0] = new Request(url.toString(), request);
}
return fetch(...args);
},
stop: () => {
return new Promise((resolve, reject) => {
fastly.on('close', resolve);
fastly.kill();
});
},
edgeSdkSettings: {
fastly: {
segmentCdnBackend: 'segment-cdn',
segmentTrackingAPIBackend: 'tracking-api',
websiteOriginBackend: 'origin'
}
}
}
}

Loading
Loading