-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
681 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,3 +29,5 @@ core | |
# Other | ||
.scraper-* | ||
*.log* | ||
|
||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { Queue } from 'bullmq' | ||
export * from './events' | ||
|
||
export const bullmq = new Queue< | ||
{ message: string }, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
import type * as jose from 'jose' | ||
import type { JWTVerifyGetKey } from 'jose' | ||
|
||
export type KeyPair = jose.GenerateKeyPairResult< | ||
jose.KeyLike & jose.JWK | ||
> | ||
|
||
|
||
export interface IJoseVerify { | ||
key: KeyPair | ||
jwks: JWTVerifyGetKey | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import consola from 'consola' | ||
|
||
import { sign, verify } from '../jose/sign' | ||
|
||
import type { IJoseVerify } from '../jose/types' | ||
|
||
export * from './types' | ||
|
||
const logger = consola.withTag('ws/browser') | ||
export class WebSocketBrowserProxy extends WebSocket { | ||
jose?: IJoseVerify | ||
public constructor( | ||
address: string | URL, | ||
protocols?: string | string[], | ||
jose?: IJoseVerify, | ||
) { | ||
super(address, protocols) | ||
this.jose = jose | ||
|
||
return wrapSocket(this) | ||
} | ||
} | ||
|
||
export function wrapSocket(ws: WebSocketBrowserProxy) { | ||
return new Proxy(ws, { | ||
get: (target, prop) => { | ||
logger.debug('Getting', prop, target) | ||
switch (prop) { | ||
case 'addEventListener': | ||
return customOn.bind(target) | ||
case 'send': | ||
return customSend.bind(target) | ||
} | ||
|
||
return Reflect.get(target, prop) | ||
}, | ||
}) | ||
} | ||
|
||
type BufferLike = string | ArrayBufferView | ArrayBufferLike | Blob | ||
|
||
async function customOn( | ||
this: WebSocketBrowserProxy, | ||
event: string, | ||
listener: (...args: any[]) => void, | ||
) { | ||
this.addEventListener(event, customListener) | ||
|
||
async function customListener(this: WebSocketBrowserProxy, ...args: any[]) { | ||
if (event === 'message') { | ||
const [event] = args as [MessageEvent<string>] | ||
const data = event.data | ||
|
||
if (!this.jose) { | ||
logger.debug('Receiving: jose not initialized', data) | ||
|
||
return listener.call(this, event) | ||
} | ||
try { | ||
const { payload, ..._jws } = await verify(data.toString(), this.jose.jwks) | ||
|
||
const newEvent = createMessageEvent(event, payload) | ||
|
||
logger.debug('Receiving payload"', { payload, event: newEvent }) | ||
|
||
return listener.call(this, newEvent) | ||
} | ||
catch (error) { | ||
const newEvent = createMessageEvent(event, {}) | ||
return listener.call(this, newEvent) | ||
} | ||
} | ||
|
||
logger.debug('Receiving', event, args) | ||
|
||
return listener.call(this, ...args) | ||
} | ||
} | ||
|
||
function createMessageEvent(event: MessageEvent, payload: unknown) { | ||
return new MessageEvent('message', { | ||
data: JSON.stringify(payload), | ||
origin: event.origin, | ||
source: event.source, | ||
lastEventId: event.lastEventId, | ||
ports: [...event.ports], | ||
}) | ||
} | ||
|
||
async function customSend(this: WebSocketBrowserProxy, data: BufferLike) { | ||
if (!this.jose) { | ||
logger.debug('Sending: jose not initialized', data) | ||
this.send(data) | ||
return | ||
} | ||
|
||
logger.debug('Signing payload: ', { payload: data, jose: this.jose }) | ||
|
||
const jws = await sign(this.jose.key, { | ||
payload: JSON.parse(data.toString()), | ||
}) | ||
|
||
logger.debug('Sending', jws) | ||
|
||
this.send(jws) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './ws' | ||
export * from './wss' | ||
export * from './types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { IJoseVerify } from '../jose/types' | ||
|
||
declare module 'ws' { | ||
export interface WebSocketServer { | ||
jose?: IJoseVerify | ||
} | ||
export interface WebSocket { | ||
jose?: IJoseVerify | ||
} | ||
} | ||
|
||
declare global { | ||
export interface WebSocket { | ||
jose?: IJoseVerify | ||
} | ||
} |
Oops, something went wrong.