Skip to content

Commit

Permalink
Migrate other Stripe infrastructure to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
anniel-stripe committed Sep 26, 2022
1 parent fffee63 commit 28d35cf
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 41 deletions.
6 changes: 4 additions & 2 deletions lib/Webhooks.js

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

2 changes: 1 addition & 1 deletion lib/crypto/SubtleCryptoProvider.js

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

7 changes: 5 additions & 2 deletions lib/multipart.js

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

3 changes: 2 additions & 1 deletion lib/net/FetchHttpClient.js

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

3 changes: 2 additions & 1 deletion lib/net/NodeHttpClient.js

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

8 changes: 5 additions & 3 deletions lib/utils.js

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

4 changes: 2 additions & 2 deletions src/Error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type StripeRawError = {
doc_url?: string;
decline_code?: string;
param?: string;
detail?: string;
detail?: any;
charge?: string;
payment_method_type?: string;

Expand All @@ -44,7 +44,7 @@ class StripeError extends Error {
readonly code?: string;
readonly doc_url?: string;
readonly param?: string;
readonly detail?: string;
readonly detail?: any;
readonly statusCode?: number;
readonly charge?: string;
readonly decline_code?: string;
Expand Down
File renamed without changes.
10 changes: 6 additions & 4 deletions src/Webhooks.js → src/Webhooks.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict';

const utils = require('./utils');
const {StripeError, StripeSignatureVerificationError} = require('./Error');
import utils = require('./utils');
import _Error = require('./Error');
const {StripeError, StripeSignatureVerificationError} = _Error;

const Webhook = {
DEFAULT_TOLERANCE: 300, // 5 minutes
signature: null,

constructEvent(payload, header, secret, tolerance, cryptoProvider) {
this.signature.verifyHeader(
Expand Down Expand Up @@ -242,7 +244,7 @@ function parseHeader(header, scheme) {
const kv = item.split('=');

if (kv[0] === 't') {
accum.timestamp = kv[1];
accum.timestamp = parseInt(kv[1], 10);
}

if (kv[0] === scheme) {
Expand Down Expand Up @@ -274,4 +276,4 @@ function getNodeCryptoProvider() {

Webhook.signature = signature;

module.exports = Webhook;
export = Webhook;
6 changes: 3 additions & 3 deletions src/crypto/CryptoProvider.js → src/crypto/CryptoProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CryptoProvider {
* - computeHMACSignature('', 'test_secret') => 'f7f9bd47fb987337b5796fdc1fdb9ba221d0d5396814bfcaf9521f43fd8927fd'
* - computeHMACSignature('\ud83d\ude00', 'test_secret') => '837da296d05c4fe31f61d5d7ead035099d9585a5bcde87de952012a78f0b0c43
*/
computeHMACSignature(payload, secret) {
computeHMACSignature(payload: string, secret: string): string {
throw new Error('computeHMACSignature not implemented.');
}

Expand All @@ -28,9 +28,9 @@ class CryptoProvider {
* - computeHMACSignature('', 'test_secret') => 'f7f9bd47fb987337b5796fdc1fdb9ba221d0d5396814bfcaf9521f43fd8927fd'
* - computeHMACSignature('\ud83d\ude00', 'test_secret') => '837da296d05c4fe31f61d5d7ead035099d9585a5bcde87de952012a78f0b0c43
*/
computeHMACSignatureAsync(payload, secret) {
computeHMACSignatureAsync(payload: string, secret: string): Promise<string> {
throw new Error('computeHMACSignatureAsync not implemented.');
}
}

module.exports = CryptoProvider;
export = CryptoProvider;
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
'use strict';

const crypto = require('crypto');
import crypto = require('crypto');

const CryptoProvider = require('./CryptoProvider');
import CryptoProvider = require('./CryptoProvider');

/**
* `CryptoProvider which uses the Node `crypto` package for its computations.
*/
class NodeCryptoProvider extends CryptoProvider {
/** @override */
computeHMACSignature(payload, secret) {
computeHMACSignature(payload: string, secret: string): string {
return crypto
.createHmac('sha256', secret)
.update(payload, 'utf8')
.digest('hex');
}

/** @override */
async computeHMACSignatureAsync(payload, secret) {
async computeHMACSignatureAsync(
payload: string,
secret: string
): Promise<string> {
const signature = await this.computeHMACSignature(payload, secret);
return signature;
}
}

module.exports = NodeCryptoProvider;
export = NodeCryptoProvider;
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
'use strict';

const CryptoProvider = require('./CryptoProvider');
import CryptoProvider = require('./CryptoProvider');

/**
* `CryptoProvider which uses the SubtleCrypto interface of the Web Crypto API.
*
* This only supports asynchronous operations.
*/
class SubtleCryptoProvider extends CryptoProvider {
subtleCrypto: any;

constructor(subtleCrypto) {
super();

Expand All @@ -18,15 +20,15 @@ class SubtleCryptoProvider extends CryptoProvider {
}

/** @override */
computeHMACSignature(payload, secret) {
computeHMACSignature(payload: string, secret: string): string {
throw new Error(
'SubtleCryptoProvider cannot be used in a synchronous context.'
);
}

/** @override */
async computeHMACSignatureAsync(payload, secret) {
const encoder = new TextEncoder('utf-8');
const encoder = new TextEncoder();

const key = await this.subtleCrypto.importKey(
'raw',
Expand Down Expand Up @@ -66,4 +68,4 @@ for (let i = 0; i < byteHexMapping.length; i++) {
byteHexMapping[i] = i.toString(16).padStart(2, '0');
}

module.exports = SubtleCryptoProvider;
export = SubtleCryptoProvider;
9 changes: 6 additions & 3 deletions src/multipart.js → src/multipart.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

const utils = require('./utils');
const {StripeError} = require('./Error');
import utils = require('./utils');
import _Error = require('./Error');
const {StripeError} = _Error;

class StreamProcessingError extends StripeError {}

Expand Down Expand Up @@ -93,4 +94,6 @@ const multipartRequestDataProcessor = (method, data, headers, callback) => {
return callback(null, buffer);
};

module.exports.multipartRequestDataProcessor = multipartRequestDataProcessor;
export = {
multipartRequestDataProcessor: multipartRequestDataProcessor,
};
10 changes: 8 additions & 2 deletions src/net/FetchHttpClient.js → src/net/FetchHttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {HttpClient, HttpClientResponse} = require('./HttpClient');
import _HttpClient = require('./HttpClient');
const {HttpClient, HttpClientResponse} = _HttpClient;

/**
* HTTP client which uses a `fetch` function to issue requests.
Expand All @@ -11,6 +12,9 @@ const {HttpClient, HttpClientResponse} = require('./HttpClient');
* node-fetch package (https://github.com/node-fetch/node-fetch).
*/
class FetchHttpClient extends HttpClient {
_fetchFn: any;
_res: any;

constructor(fetchFn) {
super();
this._fetchFn = fetchFn;
Expand Down Expand Up @@ -88,6 +92,8 @@ class FetchHttpClient extends HttpClient {
}

class FetchHttpClientResponse extends HttpClientResponse {
_res: any;

constructor(res) {
super(
res.status,
Expand Down Expand Up @@ -135,4 +141,4 @@ class FetchHttpClientResponse extends HttpClientResponse {
}
}

module.exports = {FetchHttpClient, FetchHttpClientResponse};
export = {FetchHttpClient, FetchHttpClientResponse};
14 changes: 12 additions & 2 deletions src/net/HttpClient.js → src/net/HttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

type TimeoutError = TypeError & {code?: string};

/**
* Encapsulates the logic for issuing a request to the Stripe API.
*
Expand All @@ -10,6 +12,9 @@
* returning their own response class when making requests.
*/
class HttpClient {
static CONNECTION_CLOSED_ERROR_CODES: string[];
static TIMEOUT_ERROR_CODE: string;

/** The client name used for diagnostics. */
getClientName() {
throw new Error('getClientName not implemented.');
Expand All @@ -30,7 +35,9 @@ class HttpClient {

/** Helper to make a consistent timeout error across implementations. */
static makeTimeoutError() {
const timeoutErr = new TypeError(HttpClient.TIMEOUT_ERROR_CODE);
const timeoutErr: TimeoutError = new TypeError(
HttpClient.TIMEOUT_ERROR_CODE
);
timeoutErr.code = HttpClient.TIMEOUT_ERROR_CODE;
return timeoutErr;
}
Expand All @@ -40,6 +47,9 @@ HttpClient.CONNECTION_CLOSED_ERROR_CODES = ['ECONNRESET', 'EPIPE'];
HttpClient.TIMEOUT_ERROR_CODE = 'ETIMEDOUT';

class HttpClientResponse {
_statusCode: number;
_headers: object;

constructor(statusCode, headers) {
this._statusCode = statusCode;
this._headers = headers;
Expand All @@ -66,4 +76,4 @@ class HttpClientResponse {
}
}

module.exports = {HttpClient, HttpClientResponse};
export = {HttpClient, HttpClientResponse};
9 changes: 7 additions & 2 deletions src/net/NodeHttpClient.js → src/net/NodeHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
const http = require('http');
const https = require('https');

const {HttpClient, HttpClientResponse} = require('./HttpClient');
import _HttpClient = require('./HttpClient');
const {HttpClient, HttpClientResponse} = _HttpClient;

const defaultHttpAgent = new http.Agent({keepAlive: true});
const defaultHttpsAgent = new https.Agent({keepAlive: true});
Expand All @@ -13,6 +14,8 @@ const defaultHttpsAgent = new https.Agent({keepAlive: true});
* requests.`
*/
class NodeHttpClient extends HttpClient {
_agent: any;

constructor(agent) {
super();
this._agent = agent;
Expand Down Expand Up @@ -86,6 +89,8 @@ class NodeHttpClient extends HttpClient {
}

class NodeHttpClientResponse extends HttpClientResponse {
_res: any;

constructor(res) {
super(res.statusCode, res.headers || {});
this._res = res;
Expand Down Expand Up @@ -122,4 +127,4 @@ class NodeHttpClientResponse extends HttpClientResponse {
}
}

module.exports = {NodeHttpClient, NodeHttpClientResponse};
export = {NodeHttpClient, NodeHttpClientResponse};
Loading

0 comments on commit 28d35cf

Please sign in to comment.