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

Update node-fetch dependency #141

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
74 changes: 31 additions & 43 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@
},
"dependencies": {
"@jlguenego/asn.1": "^0.0.4",
"@types/node-fetch": "^2.5.7",
"debug": "^4.3.1",
"http-errors": "^1.8.0",
"node-fetch": "3.0.0-beta.9",
"node-fetch": "^3.2.3",
"ntlm-parser": "^1.0.9"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/sso/client/AbstractHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RequestInit, Response } from 'node-fetch';
import type { RequestInit, Response } from 'node-fetch';
import { ClientCookie } from './ClientCookie';
import { ClientInfo } from './ClientInfo';

Expand Down
4 changes: 3 additions & 1 deletion src/sso/client/BasicHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import dbg from 'debug';
import fetch, { RequestInit, Response } from 'node-fetch';
import type { RequestInit, Response } from 'node-fetch';
import { loadNodeFetch } from '../loadNodeFetch';

import { AbstractHandler } from './AbstractHandler';
import { ClientCookie } from './ClientCookie';
Expand All @@ -25,6 +26,7 @@ export class BasicHandler extends AbstractHandler {
};
clientCookie.restituteCookies(requestInit);
debug('first requestInit.headers', requestInit.headers);
const { fetch } = await loadNodeFetch();
response = await fetch(resource, requestInit);
debug('first response.headers', response.headers);
clientCookie.saveCookies(response);
Expand Down
2 changes: 1 addition & 1 deletion src/sso/client/ClientCookie.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RequestInit, Response } from 'node-fetch';
import type { RequestInit, Response } from 'node-fetch';
import dbg from 'debug';

import { CookieList } from '../interfaces';
Expand Down
10 changes: 6 additions & 4 deletions src/sso/client/DigestHandler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import dbg from 'debug';
import fetch, { RequestInit, Response } from 'node-fetch';
import type { RequestInit, Response } from 'node-fetch';
import { URL } from 'url';
import { Props } from '../../../lib/api';
import { loadNodeFetch } from '../loadNodeFetch';

import { AbstractHandler } from './AbstractHandler';
import { ClientCookie } from './ClientCookie';
Expand Down Expand Up @@ -51,11 +52,11 @@ export class DigestHandler extends AbstractHandler {
}

debug('digestHeader: ', digestHeader);
const digestChallenge = (digestHeader.split(/, */).reduce((acc, prop) => {
const digestChallenge = digestHeader.split(/, */).reduce((acc, prop) => {
const [key, value] = prop.split('=');
acc[key] = value.replace(/^"?(.*?)"?$/, '$1');
return acc;
}, {} as Props) as unknown) as DigestChallenge;
}, {} as Props) as unknown as DigestChallenge;
debug('digestChallenge: ', digestChallenge);

const requestInit: RequestInit = { ...init };
Expand Down Expand Up @@ -104,11 +105,12 @@ export class DigestHandler extends AbstractHandler {
Authorization:
'Digest ' +
Object.keys(digestAnswer)
.map((k) => `${k}=${((digestAnswer as unknown) as Props)[k]}`)
.map((k) => `${k}=${(digestAnswer as unknown as Props)[k]}`)
.join(', '),
};
clientCookie.restituteCookies(requestInit);
debug('first requestInit.headers', requestInit.headers);
const { fetch } = await loadNodeFetch();
response = await fetch(resource, requestInit);
debug('first response.headers', response.headers);
clientCookie.saveCookies(response);
Expand Down
4 changes: 3 additions & 1 deletion src/sso/client/NegotiateHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dbg from 'debug';
import fetch, { RequestInit, Response } from 'node-fetch';
import type { RequestInit, Response } from 'node-fetch';
import { negotiateParse } from '../msgParser';

import {
Expand All @@ -12,6 +12,7 @@ import { ClientCookie } from './ClientCookie';
import { ClientInfo } from './ClientInfo';
import { AbstractHandler } from './AbstractHandler';
import { decode, encode, hexDump } from '../misc';
import { loadNodeFetch } from '../loadNodeFetch';

const debug = dbg('node-expose-sspi:client');

Expand Down Expand Up @@ -118,6 +119,7 @@ export class NegotiateHandler extends AbstractHandler {
};
clientCookie.restituteCookies(requestInit);
debug('requestInit.headers', requestInit.headers);
const { fetch } = await loadNodeFetch();
response = await fetch(resource, requestInit);
debug('response.status', response.status);
debug('response.headers', response.headers);
Expand Down
29 changes: 29 additions & 0 deletions src/sso/loadNodeFetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type * as NodeFetchModule from 'node-fetch';

type NodeFetchInternal = typeof NodeFetchModule;

export interface NodeFetch extends NodeFetchInternal {
fetch: NodeFetchInternal['default'];
}

// Prevent transpiling `await import(...)` into a simple require call.
// This would not work because this library compiles to commonjs but node-fetch is a esm library by now.
// Wrapping the import expression into a string hides it from the typescript compiler, so the
// native import expression will be used.
//
// Note that a future typescript module target would fix this as well ("Node12").
//
// See
// - https://github.com/microsoft/TypeScript/issues/43329#issuecomment-1008361973
// - https://nodejs.org/docs/latest-v12.x/api/esm.html#esm_import_expressions
const internalLoader = new Function(
'return import("node-fetch")'
) as () => Promise<NodeFetchInternal>;

export async function loadNodeFetch(): Promise<NodeFetch> {
const module = await internalLoader();
return {
...module,
fetch: module.default,
};
}