Skip to content
Closed
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
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

## Unreleased

## 3.1.0

**Housekeeping:**

- Slim down the default request context to `headers`, `ip`, and `library`. The
client id is already carried by `headers` (the `x-castle-client-id` header /
`__cid` cookie) and resolved by Castle server-side, so the SDK no longer
derives it separately.
- Remove the internal client-id extraction service and the now-unused cookie
plumbing (`HeadersGetCookieService`) in `ContextGetDefaultService` /
`ContextPrepareService`.

## 3.0.0

**BREAKING CHANGES:**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "http://json.schemastore.org/package",
"name": "@castleio/sdk",
"description": "Castle SDK for Node",
"version": "3.0.0",
"version": "3.1.0",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
Expand Down
1 change: 0 additions & 1 deletion src/client-id/client-id.module.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/client-id/services/client-id-extract.service.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/client-id/services/index.ts

This file was deleted.

40 changes: 8 additions & 32 deletions src/context/services/context-get-default.service.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,19 @@
import { isEmpty, pickByTruthy } from '../../utils/object';
import { Configuration } from '../../configuration';
import { ClientIdExtractService } from '../../client-id/client-id.module';
import { HeadersExtractService } from '../../headers/headers.module';
import { IPsExtractService } from '../../ips/ips.module';
import { version } from '../../../package.json';
import type { IncomingHttpHeaders } from 'http2';

const requestContextData = (
request: { headers: IncomingHttpHeaders },
cookies: string | undefined,
configuration: Configuration
): { [key: string]: any } => {
if (isEmpty(request)) {
return {};
}

const cookiesForClientId =
cookies || (request.headers as { [key: string]: any })?.cookies;
return {
client_id:
ClientIdExtractService.call(request.headers, cookiesForClientId) || false,
active: true,
headers: HeadersExtractService.call(request.headers, configuration),
ip: IPsExtractService.call(request.headers, configuration),
};
};

export const ContextGetDefaultService = {
call: (
request: { headers: IncomingHttpHeaders },
cookies: string | undefined,
configuration: Configuration
): { [key: string]: any } => {
return {
...pickByTruthy(requestContextData(request, cookies, configuration)),
library: {
name: 'castle-node',
version,
},
};
},
): { [key: string]: any } => ({
headers: HeadersExtractService.call(request.headers, configuration),
ip: IPsExtractService.call(request.headers, configuration),
library: {
name: 'castle-node',
version,
},
}),
};
1 change: 0 additions & 1 deletion src/context/services/context-prepare.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const ContextPrepareService = {
) => {
const defaultContext = ContextGetDefaultService.call(
request,
options?.cookies,
configuration
);
return deepMerge(defaultContext, options?.context);
Expand Down
20 changes: 0 additions & 20 deletions src/headers/services/headers-get-cookie.service.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/headers/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './headers-extract.service';
export * from './headers-get-cookie.service';
53 changes: 0 additions & 53 deletions test/client-id/services/client-id-extract.service.test.ts

This file was deleted.

8 changes: 1 addition & 7 deletions test/context/services/context-get-default.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ describe('ContextGetDefaultService', () => {
name: 'castle-node',
version,
},
client_id: 'client_id',
ip: '1.2.3.4',
};

Expand All @@ -29,17 +28,12 @@ describe('ContextGetDefaultService', () => {
headers: {
'x-forwarded-for': '1.2.3.4',
'x-castle-client-id': 'client_id',
cookies: 'client_id',
},
body: {},
} as ExpressRequest;

it('generates default context', () => {
const received = ContextGetDefaultService.call(
mockRequest,
undefined,
config
);
const received = ContextGetDefaultService.call(mockRequest, config);
expect(received).toMatchObject(expected);
});
});
Expand Down
1 change: 0 additions & 1 deletion test/context/services/context-prepare.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ describe('ContextPrepareService', () => {
});

const options = {
cookies: '__cid=abcd;',
context: {
client_id: 'client_id',
active: true,
Expand Down
70 changes: 0 additions & 70 deletions test/headers/services/headers-get-cookie.service.test.ts

This file was deleted.

1 change: 0 additions & 1 deletion test/payload/services/payload-prepare-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe('PayloadPrepareService', () => {
version,
},
client_id: '123',
active: true,
},
user_id: '123',
};
Expand Down
Loading