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

Use EventTarget in worker / browser runtimes #1668

Merged
merged 19 commits into from
Feb 1, 2023
Merged
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
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;
anniel-stripe marked this conversation as resolved.
Show resolved Hide resolved
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 {
anniel-stripe marked this conversation as resolved.
Show resolved Hide resolved
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