Skip to content

Commit

Permalink
Make base PlatformFunction class
Browse files Browse the repository at this point in the history
  • Loading branch information
anniel-stripe committed Jan 31, 2023
1 parent 6834c56 commit c51ee5f
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 94 deletions.
6 changes: 3 additions & 3 deletions lib/platform/NodePlatformFunctions.js

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

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

38 changes: 38 additions & 0 deletions lib/platform/WebPlatformFunctions.js

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

5 changes: 2 additions & 3 deletions lib/stripe.common.js

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

10 changes: 3 additions & 7 deletions lib/stripe.worker.js

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

2 changes: 1 addition & 1 deletion src/Types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ type StripeObject = {
_requestSender: RequestSender;
_getPropsFromConfig: (config: Record<string, unknown>) => UserProvidedConfig;
_clientId?: string;
_platformFunctions: import('./platform/DefaultPlatformFunctions');
_platformFunctions: import('./platform/WebPlatformFunctions');
};
type RequestSender = {
_request(
Expand Down
4 changes: 2 additions & 2 deletions src/Webhooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import utils = require('./utils');
import _Error = require('./Error');
import DefaultPlatformFunctions = require('./platform/DefaultPlatformFunctions');
import WebPlatformFunctions = require('./platform/WebPlatformFunctions');
import CryptoProvider = require('./crypto/CryptoProvider');
const {StripeError, StripeSignatureVerificationError} = _Error;

Expand Down Expand Up @@ -60,7 +60,7 @@ type WebhookObject = {
) => Promise<WebhookEvent>;
generateTestHeaderString: (opts: WebhookTestHeaderOptions) => string;
_createCryptoProvider: () => CryptoProvider | null;
_platformFunctions: DefaultPlatformFunctions | null;
_platformFunctions: WebPlatformFunctions | null;
};

const Webhook: WebhookObject = {
Expand Down
7 changes: 3 additions & 4 deletions src/platform/NodePlatformFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import crypto = require('crypto');
import EventEmitter = require('events');

import DefaultPlatformFunctions = require('./DefaultPlatformFunctions');
import PlatformFunctions = require('./PlatformFunctions');

/**
* Specializes DefaultPlatformFunctions using APIs available in Node.js.
* Specializes WebPlatformFunctions using APIs available in Node.js.
*/
class NodePlatformFunctions extends DefaultPlatformFunctions {
class NodePlatformFunctions extends PlatformFunctions {
/** For mocking in tests */
_exec: any;
_UNAME_CACHE: Promise<string | null> | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import StripeEmitter = require('../StripeEmitter');
* Interface encapsulating various utility functions whose
* implementations depend on the platform / JS runtime.
*/
class DefaultPlatformFunctions {
class PlatformFunctions {
/**
* Gets uname with Node's built-in `exec` function, if available.
*/
getUname(): Promise<string | null> {
return Promise.resolve(null);
throw new Error('getUname not implemented.');
}

/**
Expand All @@ -32,19 +32,17 @@ class DefaultPlatformFunctions {
if (a.length !== b.length) {
return false;
}

const len = a.length;
let result = 0;

for (let i = 0; i < len; ++i) {
result |= a.charCodeAt(i) ^ b.charCodeAt(i);
}
return result === 0;
}

createEmitter(): StripeEmitter | EventEmitter {
throw new Error('createEmitter not implemented');
throw new Error('createEmitter not implemented.');
}
}

export = DefaultPlatformFunctions;
export = PlatformFunctions;
45 changes: 45 additions & 0 deletions src/platform/WebPlatformFunctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import EventEmitter = require('events');
import StripeEmitter = require('../StripeEmitter');
import PlatformFunctions = require('./PlatformFunctions');

/**
* Specializes WebPlatformFunctions using APIs available in Web workers.
*/
class WebPlatformFunctions extends PlatformFunctions {
/** @override */
getUname(): Promise<string | null> {
return Promise.resolve(null);
}

/** @override */
uuid4(): string {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}

/** @override */
secureCompare(a: string, b: string): boolean {
// return early here if buffer lengths are not equal
if (a.length !== b.length) {
return false;
}

const len = a.length;
let result = 0;

for (let i = 0; i < len; ++i) {
result |= a.charCodeAt(i) ^ b.charCodeAt(i);
}
return result === 0;
}

/** @override */
createEmitter(): StripeEmitter | EventEmitter {
return new StripeEmitter(new EventTarget());
}
}

export = WebPlatformFunctions;
5 changes: 2 additions & 3 deletions src/stripe.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ Stripe.HttpClientResponse = HttpClient.HttpClientResponse;
import CryptoProvider = require('./crypto/CryptoProvider');
Stripe.CryptoProvider = CryptoProvider;

import DefaultPlatformFunctions = require('./platform/DefaultPlatformFunctions');
Stripe._platformFunctions = new DefaultPlatformFunctions();
import PlatformFunctions = require('./platform/PlatformFunctions');
Stripe._platformFunctions = new PlatformFunctions();

function Stripe(
this: StripeObject,
Expand Down Expand Up @@ -202,7 +202,6 @@ Stripe.prototype = {
on: null!,
off: null!,
once: null!,
emit: null!,
VERSION: null!,
StripeResource: null!,
webhooks: null!,
Expand Down
12 changes: 4 additions & 8 deletions src/stripe.worker.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import Stripe = require('./stripe.common');
import StripeEmitter = require('./StripeEmitter');
import WebPlatformFunctions = require('./platform/WebPlatformFunctions');

Stripe._platformFunctions = new WebPlatformFunctions();
Stripe.webhooks._platformFunctions = Stripe._platformFunctions;

Stripe.createHttpClient = Stripe.createFetchHttpClient;
Stripe.webhooks._createCryptoProvider = Stripe.createSubtleCryptoProvider;

// StripeEmitter uses the Event Web API. `Event` is not available
// in the global scope until Node 15, so we set `createEmitter` here
// to avoid reference errors.
Stripe._platformFunctions.createEmitter = (): StripeEmitter => {
return new StripeEmitter(new EventTarget());
};

module.exports = Stripe;

// expose constructor as a named property to enable mocking with Sinon.JS
Expand Down
Loading

0 comments on commit c51ee5f

Please sign in to comment.