Skip to content

Commit

Permalink
Revision 0.8.2
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed May 16, 2024
1 parent 16b19c5 commit 11a7fab
Show file tree
Hide file tree
Showing 12 changed files with 578 additions and 31 deletions.
28 changes: 24 additions & 4 deletions example/index.mts
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
import { Network, Stream } from '@sinclair/smoke'
import { Network, FileSystem } from '@sinclair/smoke'

// ------------------------------------------------------------------
// Store Static Files
// ------------------------------------------------------------------

const Fs = await FileSystem.open('filesystem')

await Fs.writeText('/index.html', '<html>hello world</html>')

// ------------------------------------------------------------------
// Serve Static Files
// ------------------------------------------------------------------

const { Http } = new Network()

Http.listen({ port: 5000 }, () => new Response('hello webrtc'))
Http.listen({ port: 5000 }, (request) => {
const { pathname } = new URL(request.url)

return new Response(Fs.readable(pathname))
})

// ------------------------------------------------------------------
// Fetch
// ------------------------------------------------------------------

const text = await Http.fetch('http://localhost:5000').then((x) => x.text())
const html = await Http.fetch('http://localhost:5000/index.html').then((x) => x.text())

console.log(text)
console.log(html)
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sinclair/smoke",
"version": "0.8.1",
"version": "0.8.2",
"description": "Run Web Servers in Web Browsers over WebRTC",
"type": "module",
"main": "./index.mjs",
Expand Down
48 changes: 47 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Licence MIT
- [Pattern](#Media-Pattern)
- [FileSystem](#FileSystem)
- [Open](#FileSystem-Open)
- [Listen](#FileSystem-Listen)
- [Stat](#FileSystem-Stat)
- [Exists](#FileSystem-Exists)
- [MkDir](#FileSystem-Mkdir)
Expand All @@ -88,10 +89,12 @@ Licence MIT
- [Read](#FileSystem-Read)
- [Write](#FileSystem-Write)
- [Delete](#FileSystem-Delete)
- [Rename](#FileSystem-Rename)
- [Copy](#FileSystem-Copy)
- [Move](#FileSystem-Move)
- [Contribute](#Contribute)



<a name="Network"></a>
## Network

Expand Down Expand Up @@ -310,6 +313,16 @@ import { FileSystem } from '@sinclair/smoke'
const Fs = await FileSystem.open('<database-name>')
```

<a name="FileSystem-Listen"></a>
### Listen

Use the listen function to receive events for file and directory paths.

```typescript
Fs.listen('/dir', event => console.log(event))
```


<a name="FileSystem-Stat"></a>
### Stat

Expand Down Expand Up @@ -388,6 +401,39 @@ Use the delete function to delete a file or directory. Delete is recursive.
await Fs.delete('/path/file.txt')
```

<a name="FileSystem-Rename"></a>
### Rename

Use the rename function to rename a file or directory.

```typescript
await Fs.writeText('/path/fileA.txt', '...')

await Fs.rename('/path/fileA.txt', 'fileB.txt')
```

<a name="FileSystem-Copy"></a>
### Copy

Use the copy function to copy a file or directory into a target directory.

```typescript
await Fs.writeText('/path/fileA.txt', '...')

await Fs.copy('/path/fileA.txt', '/backup')
```

<a name="FileSystem-Move"></a>
### Move

Use the move function to move a file or directory into a target directory.

```typescript
await Fs.writeText('/path/fileA.txt', '...')

await Fs.move('/path/fileA.txt', '/backup')
```


## Contribute

Expand Down
4 changes: 2 additions & 2 deletions src/events/events.mts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ import { EventHandler } from './handler.mjs'
import { Event } from './event.mjs'

export class Events<T extends Record<string, any> = Record<string, any>> {
readonly #events: Map<string, Event<any>>
readonly #events: Map<string, Event>
constructor() {
this.#events = new Map<string, Event<any>>()
this.#events = new Map<string, Event>()
}
/** Subscribes to an event */
public on<K extends keyof T>(name: K, handler: EventHandler<T[K]>): EventListener {
Expand Down
11 changes: 7 additions & 4 deletions src/events/listener.mts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

export class EventListener {
readonly #callback: () => any
constructor(callback: () => any) {
this.#callback = callback
readonly #disposeCallback: () => any
constructor(disposeCallback: () => any) {
this.#disposeCallback = disposeCallback
}
[Symbol.dispose]() {
this.dispose()
}
/** Disposes of this event listener */
public dispose() {
this.#callback()
this.#disposeCallback()
}
}
97 changes: 97 additions & 0 deletions src/filesystem/events.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*--------------------------------------------------------------------------
@sinclair/smoke
The MIT License (MIT)
Copyright (c) 2024 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/

import * as Dispose from '../dispose/index.mjs'
import * as Events from '../events/index.mjs'

export interface FileSystemCreatedEvent {
type: 'created'
path: string
}
export interface FileSystemDeletedEvent {
type: 'deleted'
path: string
}
export interface FileSystemUpdatedEvent {
type: 'updated'
path: string
}
export type FileSystemEvent = FileSystemCreatedEvent | FileSystemDeletedEvent | FileSystemUpdatedEvent
export class FileSystemEvents implements Dispose.Dispose {
readonly #receiver: BroadcastChannel
readonly #sender: BroadcastChannel
readonly #events: Map<string, Events.Event>
constructor(database: string) {
const channel = `filesystem::${database}`
this.#events = new Map<string, Events.Event>()
this.#sender = new BroadcastChannel(channel)
this.#receiver = new BroadcastChannel(channel)
this.#receiver.addEventListener('messageerror', (event) => this.#onMessageError(event))
this.#receiver.addEventListener('message', (event) => this.#onMessage(event))
}
// ----------------------------------------------------------------
// Dispose
// ----------------------------------------------------------------
[Symbol.dispose]() {
this.dispose()
}
public dispose() {
for (const event of this.#events.values()) event.dispose()
this.#events.clear()
this.#receiver.close()
this.#sender.close()
}
// ----------------------------------------------------------------
// Send and Subscribe
// ----------------------------------------------------------------
public send(event: FileSystemEvent) {
this.#sender.postMessage(event)
}
public once(path: string, handler: (event: FileSystemEvent) => any): Events.EventListener {
if (!this.#events.has(path)) this.#events.set(path, new Events.Event())
const event = this.#events.get(path)!
return event.on(handler)
}
public on(path: string, handler: (event: FileSystemEvent) => any): Events.EventListener {
if (!this.#events.has(path)) this.#events.set(path, new Events.Event())
const event = this.#events.get(path)!
return event.on(handler)
}
// ----------------------------------------------------------------
// Broadcast: Events
// ----------------------------------------------------------------
#onMessage(message: MessageEvent<FileSystemEvent>) {
for (const [key, event] of this.#events) {
if (!message.data.path.startsWith(key)) continue
event.send(message.data)
}
}
#onMessageError(message: MessageEvent) {
console.error(message)
}
}
Loading

0 comments on commit 11a7fab

Please sign in to comment.