Skip to content

Commit

Permalink
Use EventTarget in worker / browser runtimes (#1668)
Browse files Browse the repository at this point in the history
* passes tests

* Remove _StripeEvent class

* Convert flows test to TS

* Add PlatformFunctions test

* Node 14 woes

* Fix Node version check

* fix tests

* Make base PlatformFunction class

* more reviewer comments

* More createEmitter tests

* wip

* Build and actually resolve

* Throw error in web workers for streaming files

* Remove checkForStream

* Add tests

* Update src/Webhooks.ts

Co-authored-by: pakrym-stripe <[email protected]>

* reviewer comments

* better types

* more cleanup

---------

Co-authored-by: pakrym-stripe <[email protected]>
  • Loading branch information
anniel-stripe and pakrym-stripe authored Feb 1, 2023
1 parent 4f5bd5f commit b5fc448
Show file tree
Hide file tree
Showing 23 changed files with 499 additions and 144 deletions.
44 changes: 44 additions & 0 deletions lib/StripeEmitter.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/StripeResource.js

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

41 changes: 9 additions & 32 deletions lib/multipart.js

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

40 changes: 37 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.

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

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

7 changes: 3 additions & 4 deletions lib/stripe.common.js

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

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

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

11 changes: 0 additions & 11 deletions lib/utils.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/Error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class StripeError extends Error {
readonly code?: string;
readonly doc_url?: string;
readonly param?: string;
readonly detail?: string;
readonly detail?: string | Error;
readonly statusCode?: number;
readonly charge?: string;
readonly decline_code?: string;
Expand Down
56 changes: 56 additions & 0 deletions src/StripeEmitter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @private
* (For internal use in stripe-node.)
* Wrapper around the Event Web API.
*/
class _StripeEvent extends Event {
data?: RequestEvent | ResponseEvent;
constructor(eventName: string, data: any) {
super(eventName);
this.data = data;
}
}

type Listener = (...args: any[]) => any;
type ListenerWrapper = (event: _StripeEvent) => void;

/** Minimal EventEmitter wrapper around EventTarget. */
class StripeEmitter {
eventTarget: EventTarget;
listenerMapping: Map<Listener, ListenerWrapper>;

constructor() {
this.eventTarget = new EventTarget();
this.listenerMapping = new Map();
}

on(eventName: string, listener: Listener): void {
const listenerWrapper: ListenerWrapper = (event: _StripeEvent): void => {
listener(event.data);
};
this.listenerMapping.set(listener, listenerWrapper);
return this.eventTarget.addEventListener(eventName, listenerWrapper);
}

removeListener(eventName: string, listener: Listener): void {
const listenerWrapper = this.listenerMapping.get(listener);
this.listenerMapping.delete(listener);
return this.eventTarget.removeEventListener(eventName, listenerWrapper!);
}

once(eventName: string, listener: Listener): void {
const listenerWrapper: ListenerWrapper = (event: _StripeEvent): void => {
listener(event.data);
};
this.listenerMapping.set(listener, listenerWrapper);
return this.eventTarget.addEventListener(eventName, listenerWrapper, {
once: true,
});
}

emit(eventName: string, data: RequestEvent | ResponseEvent): boolean {
return this.eventTarget.dispatchEvent(new _StripeEvent(eventName, data));
}
}

export = StripeEmitter;
2 changes: 1 addition & 1 deletion src/StripeResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ StripeResource.prototype = {
opts.auth,
{headers, settings, streaming: opts.streaming},
requestCallback,
this.requestDataProcessor
this.requestDataProcessor?.bind(this)
);
});
},
Expand Down
8 changes: 4 additions & 4 deletions src/Types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type RequestCallback = (
response?: any
) => RequestCallbackReturn;
type RequestCallbackReturn = any;
type RequestData = Record<string, unknown>;
type RequestData = Record<string, any>;
type RequestEvent = {
api_version?: string;
account?: string;
Expand Down 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/PlatformFunctions');
};
type RequestSender = {
_request(
Expand All @@ -170,7 +170,7 @@ type RequestSender = {
auth: string | null,
options: RequestOptions,
callback: RequestCallback,
requestDataProcessor: RequestDataProcessor | null
requestDataProcessor: RequestDataProcessor | undefined
): void;
};
type StripeRawError = {
Expand All @@ -183,7 +183,7 @@ type StripeRawError = {
doc_url?: string;
decline_code?: string;
param?: string;
detail?: string;
detail?: string | Error;
charge?: string;
payment_method_type?: string;
payment_intent?: any;
Expand Down
Loading

0 comments on commit b5fc448

Please sign in to comment.