Skip to content

Commit

Permalink
do the thing again
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgomes committed Sep 5, 2024
1 parent 26fdb2a commit b365b94
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
15 changes: 14 additions & 1 deletion export/httpQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ interface HTTPQueryOptions {
rows: any,
opts: any,
) => void;

// JWT auth token to be passed as the Bearer token in the Authorization
// header
authToken?: string | (() => string);
}

interface HTTPTransactionOptions extends HTTPQueryOptions {
Expand Down Expand Up @@ -124,6 +128,7 @@ export function neon(
deferrable: neonOptDeferrable,
queryCallback,
resultCallback,
authToken,
}: HTTPTransactionOptions = {},
) {
// check the connection string
Expand Down Expand Up @@ -218,7 +223,9 @@ export function neon(

const url =
typeof fetchEndpoint === 'function'
? fetchEndpoint(hostname, port)
? fetchEndpoint(hostname, port, {
jwtAuth: Boolean(authToken),
})
: fetchEndpoint;

const bodyData = Array.isArray(parameterizedQuery)
Expand Down Expand Up @@ -272,6 +279,12 @@ export function neon(
'Neon-Array-Mode': 'true', // this saves data and post-processing even if we return objects, not arrays
};

if (typeof authToken === 'string') {
headers['Authorization'] = `Bearer ${authToken}`;
} else if (typeof authToken === 'function') {
headers['Authorization'] = `Bearer ${authToken()}`;
}

if (Array.isArray(parameterizedQuery)) {
// only send these headers for batch queries, where they matter
if (resolvedIsolationLevel !== undefined)
Expand Down
30 changes: 24 additions & 6 deletions shims/net/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,20 @@ export function isIP(input: string) {
return 0;
}

interface FetchEndpointOptions {
jwtAuth?: boolean;
}

export interface SocketDefaults {
// these options relate to the fetch transport and take effect *only* when set globally
poolQueryViaFetch: boolean;
fetchEndpoint: string | ((host: string, port: number | string) => string);
fetchEndpoint:
| string
| ((
host: string,
port: number | string,
options?: FetchEndpointOptions,
) => string);
fetchConnectionCache: boolean;
fetchFunction: any;
// these options relate to the WebSocket transport
Expand All @@ -77,15 +87,23 @@ type GlobalOnlyDefaults =
| 'fetchConnectionCache'
| 'fetchFunction';

const transformHost = (host: string): string => {
return host.replace(/^[^.]+\./, 'api.');
};

export class Socket extends EventEmitter {
static defaults: SocketDefaults = {
// these options relate to the fetch transport and take effect *only* when set globally
poolQueryViaFetch: false,
fetchEndpoint: (host) => 'https://' + transformHost(host) + '/sql',
fetchEndpoint: (host, _port, options) => {
let newHost;
if (options?.jwtAuth) {
// If the caller sends in a JWT, we need to use the Neon Authorize API
// endpoint instead (this goes to the Auth Broker instead of the Neon
// Proxy).
newHost = host.replace(/^[^.]+\./, 'apiauth.');
} else {
newHost = host.replace(/^[^.]+\./, 'api.');
}

return 'https://' + newHost + '/sql';
},
fetchConnectionCache: true,
fetchFunction: undefined,
// these options relate to the WebSocket transport
Expand Down

0 comments on commit b365b94

Please sign in to comment.