Skip to content

Commit

Permalink
Add back createCryptoProvider and createHttpClient, remove non-null a…
Browse files Browse the repository at this point in the history
…ssertion, more specific return type on createStripe
  • Loading branch information
anniel-stripe committed Feb 7, 2023
1 parent ef4162c commit 81c80d5
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 22 deletions.
10 changes: 5 additions & 5 deletions lib/Webhooks.js

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

9 changes: 9 additions & 0 deletions lib/platform/NodePlatformFunctions.js

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

13 changes: 13 additions & 0 deletions lib/platform/PlatformFunctions.js

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

10 changes: 9 additions & 1 deletion lib/platform/WebPlatformFunctions.js

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

17 changes: 7 additions & 10 deletions src/Webhooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import CryptoProvider = require('./crypto/CryptoProvider');
import _Error = require('./Error');
import PlatformFunctions = require('./platform/PlatformFunctions');
import CryptoProvider = require('./crypto/CryptoProvider');
const {StripeError, StripeSignatureVerificationError} = _Error;

type WebhookHeader = string | Uint8Array;
Expand Down Expand Up @@ -131,7 +131,7 @@ function createWebhooks(platformFunctions: PlatformFunctions): WebhookObject {
Math.floor(opts.timestamp) || Math.floor(Date.now() / 1000);
opts.scheme = opts.scheme || signature.EXPECTED_SCHEME;

opts.cryptoProvider = opts.cryptoProvider || getNodeCryptoProvider();
opts.cryptoProvider = opts.cryptoProvider || getCryptoProvider();

opts.signature =
opts.signature ||
Expand Down Expand Up @@ -169,7 +169,7 @@ function createWebhooks(platformFunctions: PlatformFunctions): WebhookObject {
this.EXPECTED_SCHEME
);

cryptoProvider = cryptoProvider || getNodeCryptoProvider();
cryptoProvider = cryptoProvider || getCryptoProvider();
const expectedSignature = cryptoProvider.computeHMACSignature(
makeHMACContent(payload, details),
secret
Expand Down Expand Up @@ -203,7 +203,7 @@ function createWebhooks(platformFunctions: PlatformFunctions): WebhookObject {
this.EXPECTED_SCHEME
);

cryptoProvider = cryptoProvider || getNodeCryptoProvider();
cryptoProvider = cryptoProvider || getCryptoProvider();

const expectedSignature = await cryptoProvider.computeHMACSignatureAsync(
makeHMACContent(payload, details),
Expand Down Expand Up @@ -290,10 +290,7 @@ function createWebhooks(platformFunctions: PlatformFunctions): WebhookObject {
tolerance: number
): boolean {
const signatureFound = !!details.signatures.filter(
platformFunctions!.secureCompare.bind(
platformFunctions,
expectedSignature
)
platformFunctions.secureCompare.bind(platformFunctions, expectedSignature)
).length;

if (!signatureFound) {
Expand Down Expand Up @@ -353,9 +350,9 @@ function createWebhooks(platformFunctions: PlatformFunctions): WebhookObject {
* Lazily instantiate a CryptoProvider instance. This is a stateless object
* so a singleton can be used here.
*/
function getNodeCryptoProvider(): StripeCryptoProvider {
function getCryptoProvider(): StripeCryptoProvider {
if (!webhooksCryptoProviderInstance) {
webhooksCryptoProviderInstance = platformFunctions.createNodeCryptoProvider();
webhooksCryptoProviderInstance = platformFunctions.createCryptoProvider();
}
return webhooksCryptoProviderInstance!;
}
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/SubtleCryptoProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import CryptoProvider = require('./CryptoProvider');
class SubtleCryptoProvider extends CryptoProvider {
subtleCrypto: SubtleCrypto;

constructor(subtleCrypto: SubtleCrypto) {
constructor(subtleCrypto?: SubtleCrypto) {
super();

// If no subtle crypto is interface, default to the global namespace. This
Expand Down
11 changes: 11 additions & 0 deletions src/platform/NodePlatformFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,21 @@ class NodePlatformFunctions extends PlatformFunctions {
return new NodeHttpClient(agent);
}

/** @override */
createHttpClient(agent?: http.Agent): typeof HttpClient {
// @ts-ignore
return new NodeHttpClient(agent);
}

/** @override */
createNodeCryptoProvider(): StripeCryptoProvider {
return new NodeCryptoProvider();
}

/** @override */
createCryptoProvider(): StripeCryptoProvider {
return this.createNodeCryptoProvider();
}
}

export = NodePlatformFunctions;
25 changes: 22 additions & 3 deletions src/platform/PlatformFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ import SubtleCryptoProvider = require('../crypto/SubtleCryptoProvider');
* implementations depend on the platform / JS runtime.
*/
class PlatformFunctions {
_fetchFn: any | null;
_agent: http.Agent | null;

constructor() {
this._fetchFn = null;
this._agent = null;
}

/**
* Gets uname with Node's built-in `exec` function, if available.
*/
Expand Down Expand Up @@ -67,7 +75,7 @@ class PlatformFunctions {
* Creates an HTTP client which uses the Node `http` and `https` packages
* to issue requests.
*/
createNodeHttpClient(agent: http.Agent): typeof HttpClient {
createNodeHttpClient(agent?: http.Agent | null): typeof HttpClient {
throw new Error('createNodeHttpClient not implemented.');
}

Expand All @@ -78,11 +86,18 @@ class PlatformFunctions {
* A fetch function can optionally be passed in as a parameter. If none is
* passed, will default to the default `fetch` function in the global scope.
*/
createFetchHttpClient(fetchFn: typeof fetch): typeof HttpClient {
createFetchHttpClient(fetchFn?: typeof fetch | null): typeof HttpClient {
// @ts-ignore
return new FetchHttpClient(fetchFn);
}

/**
* Creates an HTTP client using runtime-specific APIs.
*/
createHttpClient(agent?: http.Agent | null): typeof HttpClient {
throw new Error('createHttpClient not implemented.');
}

/**
* Creates a CryptoProvider which uses the Node `crypto` package for its computations.
*/
Expand All @@ -94,10 +109,14 @@ class PlatformFunctions {
* Creates a CryptoProvider which uses the SubtleCrypto interface of the Web Crypto API.
*/
createSubtleCryptoProvider(
subtleCrypto: typeof crypto.subtle
subtleCrypto?: typeof crypto.subtle
): StripeCryptoProvider {
return new SubtleCryptoProvider(subtleCrypto);
}

createCryptoProvider(): StripeCryptoProvider {
throw new Error('createCryptoProvider not implemented.');
}
}

export = PlatformFunctions;
12 changes: 11 additions & 1 deletion src/platform/WebPlatformFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,28 @@ class WebPlatformFunctions extends PlatformFunctions {
}

/** @override */
createNodeHttpClient(agent: http.Agent): typeof HttpClient {
createNodeHttpClient(): typeof HttpClient {
throw new Error(
'Stripe: `createNodeHttpClient()` is not available in non-Node environments. Please use `createFetchHttpClient()` instead.'
);
}

/** @override */
createHttpClient(): typeof HttpClient {
return super.createFetchHttpClient();
}

/** @override */
createNodeCryptoProvider(): StripeCryptoProvider {
throw new Error(
'Stripe: `createNodeCryptoProvider()` is not available in non-Node environments. Please use `createSubtleCryptoProvider()` instead.'
);
}

/** @override */
createCryptoProvider(): StripeCryptoProvider {
return this.createSubtleCryptoProvider();
}
}

export = WebPlatformFunctions;
2 changes: 1 addition & 1 deletion src/stripe.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import CryptoProvider = require('./crypto/CryptoProvider');
import PlatformFunctions = require('./platform/PlatformFunctions');
import createWebhooks = require('./Webhooks');

function createStripe(platformFunctions: PlatformFunctions): any {
function createStripe(platformFunctions: PlatformFunctions): typeof Stripe {
Stripe.PACKAGE_VERSION = require('../package.json').version;
Stripe.USER_AGENT = {
bindings_version: Stripe.PACKAGE_VERSION,
Expand Down

0 comments on commit 81c80d5

Please sign in to comment.