Skip to content

Commit

Permalink
Merge branch 'develop' into feat/fail-dropped-jupiter-transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
NickKelly1 authored Nov 14, 2024
2 parents c0bbe9a + b6e5f6d commit 216798b
Show file tree
Hide file tree
Showing 94 changed files with 1,737 additions and 1,617 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v22.10.0
v22.11.0
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:22.10-bookworm
FROM node:22.11-bookworm
RUN apt-get update
RUN apt-get install build-essential zip -y
ENV HOME /home
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
"devDependencies": {
"@commitlint/cli": "^19.5.0",
"@commitlint/config-conventional": "^19.5.0",
"@swc/core": "^1.7.39",
"concurrently": "^9.0.1",
"@swc/core": "^1.9.2",
"concurrently": "^9.1.0",
"husky": "^9.1.6",
"node-notifier": "^10.0.1",
"nodemon": "^3.1.7",
Expand Down
4 changes: 2 additions & 2 deletions packages/extension-bridge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ button.addEventListener("click", async () => {
const res = await sendMessage(
"get-selection",
{ ignoreCasing: true },
"content-script"
"content-script",
);
console.log(res); // > "The brown fox is alive and well"
});
Expand All @@ -57,7 +57,7 @@ onMessage("get-selection", async (message) => {
const { selection } = await sendMessage(
"get-preferences",
{ sync: false },
"background"
"background",
);
return calculateSelection(data.ignoreCasing, selection);
});
Expand Down
16 changes: 8 additions & 8 deletions packages/extension-bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,23 @@
"webextension-polyfill": "^0.12.0"
},
"devDependencies": {
"@types/node": "^22.7.8",
"@types/node": "^22.9.0",
"@types/webextension-polyfill": "^0.12.1",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"bumpp": "^9.7.1",
"eslint": "^8.57.1",
"bumpp": "^9.8.1",
"eslint": "^9.14.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.10.0",
"eslint-config-prettier": "^9.1.0",
"eslint-import-resolver-alias": "^1.1.2",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-module-resolver": "^1.5.0",
"prettier": "^2.8.8",
"prettier": "^3.3.3",
"ts-node": "^10.9.2",
"tsconfig-paths": "^4.2.0",
"tsup": "^8.3.0",
"tsup": "^8.3.5",
"type-fest": "^4.26.1",
"typescript": "^4.9.5",
"typescript-eslint": "0.0.1-alpha.0"
"typescript": "^5.6.3",
"typescript-eslint": "8.14.0"
}
}
4 changes: 2 additions & 2 deletions packages/extension-bridge/src/apis/onMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import { onMessageListeners } from "../internal";

export function onMessage<
Data extends JsonValue,
K extends DataTypeKey | string
K extends DataTypeKey | string,
>(
messageID: K,
callback: OnMessageCallback<GetDataType<K, Data>, GetReturnType<K, any>>
callback: OnMessageCallback<GetDataType<K, Data>, GetReturnType<K, any>>,
): void {
onMessageListeners.set(messageID, callback);
}
8 changes: 4 additions & 4 deletions packages/extension-bridge/src/apis/sendMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@ import { parseEndpoint } from "../utils";
*/
export async function sendMessage<
ReturnType extends JsonValue,
K extends DataTypeKey | string
K extends DataTypeKey | string,
>(
messageID: K,
data: GetDataType<K, JsonValue>,
destination: Destination = "background"
destination: Destination = "background",
) {
const endpoint =
typeof destination === "string" ? parseEndpoint(destination) : destination;
const errFn = "Bridge#sendMessage ->";

if (!endpoint.context)
throw new TypeError(
`${errFn} Destination must be any one of known destinations`
`${errFn} Destination must be any one of known destinations`,
);
if (context === "background") {
const { context: dest, tabId: destTabId } = endpoint;
if (dest !== "background" && !destTabId)
throw new TypeError(
`${errFn} When sending messages from background page, use @tabId syntax to target specific tab`
`${errFn} When sending messages from background page, use @tabId syntax to target specific tab`,
);
}

Expand Down
8 changes: 4 additions & 4 deletions packages/extension-bridge/src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ onMessage<{ channel: string; streamId: string }, string>(
};

readyup();
})
}),
);

async function openStream(
channel: string,
destination: RuntimeContext | Endpoint | string
destination: RuntimeContext | Endpoint | string,
): Promise<Stream> {
if (openStreams.has(channel))
throw new Error("webext-bridge: A Stream is already open at this channel");
Expand All @@ -58,11 +58,11 @@ async function openStream(

function onOpenStreamChannel(
channel: string,
callback: (stream: Stream) => void
callback: (stream: Stream) => void,
): void {
if (onOpenStreamCallbacks.has(channel))
throw new Error(
"webext-bridge: This channel has already been claimed. Stream allows only one-on-one communication"
"webext-bridge: This channel has already been claimed. Stream allows only one-on-one communication",
);

onOpenStreamCallbacks.set(channel, callback);
Expand Down
32 changes: 16 additions & 16 deletions packages/extension-bridge/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import { hasAPI, parseEndpoint, getBackgroundPageType } from "./utils";
export const context: RuntimeContext = hasAPI("devtools")
? "devtools"
: hasAPI("tabs")
? getBackgroundPageType()
: hasAPI("extension")
? "content-script"
: typeof document !== "undefined"
? "window"
: null;
? getBackgroundPageType()
: hasAPI("extension")
? "content-script"
: typeof document !== "undefined"
? "window"
: null;

const runtimeId: string = uuid();
export const openTransactions = new Map<
Expand Down Expand Up @@ -51,7 +51,7 @@ export const allowWindowMessaging = (nsps: string): void => {
namespace = nsps;
};
const handleInboundMessage = async (
message: IInternalMessage
message: IInternalMessage,
): Promise<void> => {
const { transactionId, messageID, messageType } = message;

Expand All @@ -63,7 +63,7 @@ const handleInboundMessage = async (
const dehydratedErr = err as Record<string, string>;
const errCtr = self[dehydratedErr.name] as any;
const hydratedErr = new (typeof errCtr === "function" ? errCtr : Error)(
dehydratedErr.message
dehydratedErr.message,
);
Object.keys(dehydratedErr).forEach((prop) => {
hydratedErr[prop] = dehydratedErr[prop];
Expand Down Expand Up @@ -93,7 +93,7 @@ const handleInboundMessage = async (
} else {
noHandlerFoundError = true;
throw new Error(
`[webext-bridge] No handler registered in '${context}' to accept messages with id '${messageID}'`
`[webext-bridge] No handler registered in '${context}' to accept messages with id '${messageID}'`,
);
}
} catch (error) {
Expand Down Expand Up @@ -128,7 +128,7 @@ const handleInboundMessage = async (
const initIntercoms = () => {
if (context === null)
throw new Error(
"Unable to detect runtime context i.e webext-bridge can't figure out what to do"
"Unable to detect runtime context i.e webext-bridge can't figure out what to do",
);

if (context === "window" || context === "content-script")
Expand Down Expand Up @@ -245,7 +245,7 @@ const initIntercoms = () => {

initIntercoms();
export const routeMessage = (
message: IInternalMessage
message: IInternalMessage,
): void | Promise<void> => {
const { origin, destination } = message;
if (message.hops.includes(runtimeId)) return;
Expand Down Expand Up @@ -275,7 +275,7 @@ export const routeMessage = (
routeMessageThroughWindow(window, message);
} else if (
["devtools", "content-script", "popup", "options", "new-window"].includes(
context
context,
)
) {
if (destination.context === "background") message.destination = null;
Expand Down Expand Up @@ -320,7 +320,7 @@ export const routeMessage = (

// eslint-disable-next-line @typescript-eslint/no-empty-function
const assertInternalMessage: (x: unknown) => asserts x = (
_msg: any
_msg: any,
): asserts _msg is IInternalMessage => {
// todo
};
Expand Down Expand Up @@ -373,7 +373,7 @@ const routeMessageThroughWindow = (win: Window, msg: IInternalMessage) => {
context,
payload: msg,
},
"*"
"*",
);
};
win.postMessage(
Expand All @@ -383,7 +383,7 @@ const routeMessageThroughWindow = (win: Window, msg: IInternalMessage) => {
context,
},
"*",
[channel.port2]
[channel.port2],
);
};

Expand All @@ -393,7 +393,7 @@ function ensureNamespaceSet() {
'webext-bridge uses window.postMessage to talk with other "window"(s), for message routing and stuff,' +
"which is global/conflicting operation in case there are other scripts using webext-bridge. " +
"Call Bridge#setNamespace(nsps) to isolate your app. Example: setNamespace('com.facebook.react-devtools'). " +
"Make sure to use same namespace across all your scripts whereever window.postMessage is likely to be used`"
"Make sure to use same namespace across all your scripts whereever window.postMessage is likely to be used`",
);
}
}
Expand Down
12 changes: 6 additions & 6 deletions packages/extension-bridge/src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Stream {
public send(msg?: JsonValue): void {
if (this.isClosed)
throw new Error(
"Attempting to send a message over closed stream. Use stream.onClose(<callback>) to keep an eye on stream status"
"Attempting to send a message over closed stream. Use stream.onClose(<callback>) to keep an eye on stream status",
);

sendMessage(
Expand All @@ -84,7 +84,7 @@ class Stream {
streamTransfer: msg,
action: "transfer",
},
this.internalInfo.endpoint
this.internalInfo.endpoint,
);
}

Expand All @@ -106,7 +106,7 @@ class Stream {
streamTransfer: null,
action: "close",
},
this.internalInfo.endpoint
this.internalInfo.endpoint,
);
}

Expand All @@ -115,7 +115,7 @@ class Stream {
* @param callback
*/
public onMessage<T extends JsonValue>(
callback: (msg?: T) => void
callback: (msg?: T) => void,
): HybridUnsubscriber {
return this.getDisposable("message", callback);
}
Expand All @@ -125,7 +125,7 @@ class Stream {
* @param callback
*/
public onClose<T extends JsonValue>(
callback: (msg?: T) => void
callback: (msg?: T) => void,
): HybridUnsubscriber {
return this.getDisposable("closed", callback);
}
Expand All @@ -140,7 +140,7 @@ class Stream {

private getDisposable(
event: string,
callback: () => void
callback: () => void,
): HybridUnsubscriber {
const off = this.emitter.on(event, callback);

Expand Down
8 changes: 4 additions & 4 deletions packages/extension-bridge/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface IBridgeMessage<T extends JsonValue> {
}

export type OnMessageCallback<T extends JsonValue, R = void | JsonValue> = (
message: IBridgeMessage<T>
message: IBridgeMessage<T>,
) => R | Promise<R>;

export interface IInternalMessage {
Expand Down Expand Up @@ -61,7 +61,7 @@ declare const ProtocolWithReturnSymbol: unique symbol;

export interface ProtocolWithReturn<
Data extends JsonValue,
Return extends JsonValue
Return extends JsonValue,
> {
data: Data;
return: Return;
Expand All @@ -84,7 +84,7 @@ export type DataTypeKey = keyof ProtocolMap;

export type GetDataType<
K extends DataTypeKey | string,
Fallback extends JsonValue
Fallback extends JsonValue,
> = K extends DataTypeKey
? ProtocolMap[K] extends ProtocolWithReturn<infer Data, any>
? Data
Expand All @@ -93,7 +93,7 @@ export type GetDataType<

export type GetReturnType<
K extends DataTypeKey | string,
Fallback extends JsonValue
Fallback extends JsonValue,
> = K extends DataTypeKey
? ProtocolMap[K] extends ProtocolWithReturn<any, infer Return>
? Return
Expand Down
4 changes: 2 additions & 2 deletions packages/extension-bridge/src/window/apis/onMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import { onMessageListeners } from "../internal";

export function onMessage<
Data extends JsonValue,
K extends DataTypeKey | string
K extends DataTypeKey | string,
>(
messageID: K,
callback: OnMessageCallback<GetDataType<K, Data>, GetReturnType<K, any>>
callback: OnMessageCallback<GetDataType<K, Data>, GetReturnType<K, any>>,
): void {
onMessageListeners.set(messageID, callback);
}
8 changes: 4 additions & 4 deletions packages/extension-bridge/src/window/apis/sendMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@ import { parseEndpoint } from "../utils";
*/
export async function sendMessage<
ReturnType extends JsonValue,
K extends DataTypeKey | string
K extends DataTypeKey | string,
>(
messageID: K,
data: GetDataType<K, JsonValue>,
destination: Destination = "background"
destination: Destination = "background",
) {
const endpoint =
typeof destination === "string" ? parseEndpoint(destination) : destination;
const errFn = "Bridge#sendMessage ->";

if (!endpoint.context)
throw new TypeError(
`${errFn} Destination must be any one of known destinations`
`${errFn} Destination must be any one of known destinations`,
);

if (context === "background") {
const { context: dest, tabId: destTabId } = endpoint;
if (dest !== "background" && !destTabId)
throw new TypeError(
`${errFn} When sending messages from background page, use @tabId syntax to target specific tab`
`${errFn} When sending messages from background page, use @tabId syntax to target specific tab`,
);
}

Expand Down
Loading

0 comments on commit 216798b

Please sign in to comment.