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

Add browser extension support #504

Merged
merged 31 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0109cbb
Add connect and disconnect handlers
bgins Feb 23, 2023
738f27f
Improve connect and disconnect handlers
bgins Feb 26, 2023
21634db
Rename connection messages
bgins Feb 27, 2023
a975bc0
Add State type and rework state helper function
bgins Feb 27, 2023
3733fd8
Add filesystem event listeners
bgins Feb 27, 2023
3911b40
Return connection from connection handlers
bgins Feb 27, 2023
4099eea
Add session events and emitters
bgins Feb 28, 2023
9364953
Emit ready post message when Webnative is ready
bgins Mar 8, 2023
f964a16
Emit session on session create event
bgins Mar 9, 2023
4cf29b2
Add check to avoid adding listeners more than once
bgins Mar 9, 2023
9f742c9
Add timestamps to messages
bgins Mar 14, 2023
49fbf30
Clean up imports
bgins Mar 14, 2023
eb99b3b
Add capabilities to state
bgins Mar 14, 2023
8043fff
Move version into a webnative object
bgins Mar 15, 2023
5cb44cf
Only send capabilities on state when defined
bgins Mar 15, 2023
6f0f452
Remove console logs
bgins Mar 24, 2023
6a62270
Re-work event listener types and interface
bgins Mar 27, 2023
e9d2997
Revert "Re-work event listener types and interface"
bgins Mar 28, 2023
26641c3
Break up circular dependency
bgins Mar 28, 2023
c6f7f72
Rename webnative to odd
bgins Mar 28, 2023
55ef50e
Update changelog
bgins Mar 28, 2023
1290857
Move events onto program and rename them
bgins Mar 28, 2023
2672264
Improve comment
bgins Mar 28, 2023
58737d8
Remove commented out code
bgins Mar 28, 2023
43551d7
Update version to 0.37.0
bgins Mar 28, 2023
e4bace0
Rename filesystem window message types to fileSystem
bgins Mar 30, 2023
3d0d40c
Rename filesystem to fileSystem in State object
bgins Mar 30, 2023
d805166
Pass in components instead of shorthands
bgins Mar 30, 2023
40f2c93
Emit session create from session constructor
bgins Mar 30, 2023
f1c88be
Add event emitter merge
bgins Mar 31, 2023
bb40d7f
Fix overwritten event listeners on program
bgins Mar 31, 2023
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

### V0.37.0

- Adds browser extension support
- Moves events onto top-level program and renames them. For example, the `local-change` is now `fileSystem:local-change`.
- Adds session create and destroy events

### V0.36.3

Now parses DAG-JSON formatted CIDs.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webnative",
"version": "0.36.3",
"version": "0.37.0",
"description": "Webnative SDK",
"keywords": [
"WebCrypto",
Expand Down
2 changes: 1 addition & 1 deletion src/common/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const VERSION = "0.36.3"
export const VERSION = "0.37.0"
export const WASM_WNFS_VERSION = "0.1.7"
7 changes: 6 additions & 1 deletion src/components/auth/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ export type Implementation<C> = {
type: string

// `Session` producer
session: (components: C, authenticatedUsername: Maybe<string>, config: Configuration, eventEmitters: { fileSystem: Events.Emitter<Events.FileSystem> }) => Promise<Maybe<Session>>
session: (
components: C,
authenticatedUsername: Maybe<string>,
config: Configuration,
eventEmitters: { fileSystem: Events.Emitter<Events.FileSystem>; session: Events.Emitter<Events.Session<Session>> }
) => Promise<Maybe<Session>>

// Account creation
isUsernameAvailable: (username: string) => Promise<boolean>
Expand Down
9 changes: 7 additions & 2 deletions src/components/auth/implementation/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as Reference from "../../reference/implementation.js"
import * as Storage from "../../storage/implementation.js"

import * as Did from "../../../did/index.js"
import * as Events from "../../../events.js"
import * as SessionMod from "../../../session.js"
import * as Ucan from "../../../ucan/index.js"

Expand Down Expand Up @@ -108,16 +109,20 @@ export async function register(
export async function session(
components: Components,
authedUsername: Maybe<string>,
config: Configuration
config: Configuration,
eventEmitters: { session: Events.Emitter<Events.Session<Session>> }
): Promise<Maybe<Session>> {
if (authedUsername) {
return new Session({
const session = new Session({
crypto: components.crypto,
storage: components.storage,
eventEmitter: eventEmitters.session,
type: TYPE,
username: authedUsername
})

return session

} else {
return null

Expand Down
10 changes: 7 additions & 3 deletions src/components/auth/implementation/wnfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export async function session(
components: Components,
authedUsername: Maybe<string>,
config: Configuration,
eventEmitters: { fileSystem: Events.Emitter<Events.FileSystem> }
eventEmitters: { fileSystem: Events.Emitter<Events.FileSystem>; session: Events.Emitter<Events.Session<Session>> }
): Promise<Maybe<Session>> {
if (authedUsername) {
// Self-authorize a filesystem UCAN if needed
Expand Down Expand Up @@ -140,14 +140,18 @@ export async function session(
username: authedUsername,
})

// Fin
return new Session({

const session = new Session({
crypto: components.crypto,
fs: fs,
eventEmitter: eventEmitters.session,
storage: components.storage,
type: Base.TYPE,
username: authedUsername
})

// Fin
return session
}

return null
Expand Down
7 changes: 7 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ export type Configuration = {
* Debugging settings.
*/
debugging?: {
/**
* Should I emit window post messages with session and filesystem information?
*
* @default true
*/
emitWindowPostMessages?: boolean
bgins marked this conversation as resolved.
Show resolved Hide resolved

/**
* Should I add programs to the global context while in debugging mode?
*
Expand Down
36 changes: 32 additions & 4 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ export { EventEmitter, EventEmitter as Emitter }
* alternatively you can use `addListener` and `removeListener`.
*
* ```ts
* program.fileSystem.on("local-change", ({ path, root }) => {
* program.on("fileSystem:local-change", ({ path, root }) => {
* console.log("The file system has changed locally 🔔")
* console.log("Changed path:", path)
* console.log("New data root CID:", root)
* })
*
* program.fileSystem.off("publish")
* program.off("fileSystem:publish")
* ```
*/
export type ListenTo<EventMap> = Pick<
Expand All @@ -29,11 +29,20 @@ export type ListenTo<EventMap> = Pick<


export type FileSystem = {
"local-change": { root: CID; path: DistinctivePath<Partitioned<Partition>> }
"publish": { root: CID }
"fileSystem:local-change": { root: CID; path: DistinctivePath<Partitioned<Partition>> }
"fileSystem:publish": { root: CID }
}


export type Session<S> = {
"session:create": { session: S }
"session:destroy": { username: string }
}


export type All<S> = FileSystem & Session<S>


export function createEmitter<EventMap>(): EventEmitter<EventMap> {
return new EventEmitter()
}
Expand All @@ -46,4 +55,23 @@ export function listenTo<EventMap>(emitter: EventEmitter<EventMap>): ListenTo<Ev
on: emitter.on.bind(emitter),
off: emitter.off.bind(emitter),
}
}


export function merge<A, B>(a: EventEmitter<A>, b: EventEmitter<B>): EventEmitter<A & B> {
const merged = createEmitter<A & B>()
const aEmit = a.emit
const bEmit = b.emit

a.emit = <K extends keyof A>(eventName: K, event: (A & B)[ K ]) => {
aEmit.call(a, eventName, event)
merged.emit(eventName, event)
}

b.emit = <K extends keyof B>(eventName: K, event: (A & B)[ K ]) => {
bEmit.call(b, eventName, event)
merged.emit(eventName, event)
}

return merged
}
Loading