Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
… into bun-sh
  • Loading branch information
ThatOneCalculator committed Oct 7, 2023
2 parents b88bcec + 1f9dae3 commit 23d1e28
Show file tree
Hide file tree
Showing 196 changed files with 2,674 additions and 2,646 deletions.
15 changes: 15 additions & 0 deletions example/browser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
At first, set environment vairables

```
$ export MASTODON_URL=wss://mastodon.social
$ export MASTODON_ACCESS_TOKEN=foobar
```
And execute

```
$ yarn workspace megalodon build
$ yarn workspace browser build
$ yarn workspace browser start
```

Let's open `http://127.0.0.1:8000`.
55 changes: 50 additions & 5 deletions example/browser/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,55 @@
import generator, { Entity, Response } from 'megalodon'
import generator, { Entity } from 'megalodon'

const BASE_URL: string = 'https://mastodon.social'
const BASE_URL: string = process.env.MASTODON_URL!
const ACCESS_TOKEN: string = process.env.MASTODON_ACCESS_TOKEN!
console.log(BASE_URL)
console.log('start')

const client = generator('mastodon', BASE_URL)
const client = generator('mastodon', BASE_URL, ACCESS_TOKEN)

client.getInstance().then((res: Response<Entity.Instance>) => {
console.log(res)
const stream = client.localSocket()
stream.on('connect', () => {
console.log('connect')
})

stream.on('pong', () => {
console.log('pong')
})

stream.on('update', (status: Entity.Status) => {
console.log(status)
})

stream.on('notification', (notification: Entity.Notification) => {
console.log(notification)
})

stream.on('delete', (id: number) => {
console.log(id)
})

stream.on('error', (err: Error) => {
console.error(err)
})

stream.on('status_update', (status: Entity.Status) => {
console.log('updated: ', status.url)
})

stream.on('heartbeat', () => {
console.log('thump.')
})

stream.on('close', () => {
console.log('close')
})

stream.on('parser-error', (err: Error) => {
console.error(err)
})

setTimeout(() => {
stream.removeAllListeners()
stream.stop()
console.log('closed')
}, 10000)
4 changes: 3 additions & 1 deletion example/browser/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ module.exports = {
plugins: [
new webpack.DefinePlugin({
'process.browser': true,
'process.env.NODE_DEBUG': false
'process.env.NODE_DEBUG': false,
'process.env.MASTODON_URL': `"${process.env.MASTODON_URL}"`,
'process.env.MASTODON_ACCESS_TOKEN': `"${process.env.MASTODON_ACCESS_TOKEN}"`
}),
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
Expand Down
12 changes: 3 additions & 9 deletions example/typescript/src/mastodon/streaming.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import generator, { Entity, WebSocketInterface } from 'megalodon'

declare var process: {
env: {
MASTODON_ACCESS_TOKEN: string
}
}
const BASE_URL: string = process.env.MASTODON_STREAMING_URL!

const BASE_URL: string = 'wss://streaming.fedibird.com'

const access_token: string = process.env.MASTODON_ACCESS_TOKEN
const access_token: string = process.env.MASTODON_ACCESS_TOKEN!

const client = generator('mastodon', BASE_URL, access_token)

const stream: WebSocketInterface = client.userSocket()
const stream: WebSocketInterface = client.localSocket()
stream.on('connect', () => {
console.log('connect')
})
Expand Down
5 changes: 3 additions & 2 deletions megalodon/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "megalodon",
"version": "8.1.1",
"version": "8.1.3",
"description": "Mastodon API client for node.js and browser",
"main": "./lib/src/index.js",
"typings": "./lib/src/index.d.ts",
Expand Down Expand Up @@ -66,7 +66,8 @@
"socks-proxy-agent": "^8.0.2",
"typescript": "5.2.2",
"uuid": "^9.0.1",
"ws": "8.14.2"
"ws": "8.14.2",
"isomorphic-ws": "^5.0.0"
},
"devDependencies": {
"@types/core-js": "^2.5.6",
Expand Down
8 changes: 8 additions & 0 deletions megalodon/src/default.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
export const NO_REDIRECT = 'urn:ietf:wg:oauth:2.0:oob'
export const DEFAULT_SCOPE = ['read', 'write', 'follow']
export const DEFAULT_UA = 'megalodon'

export function isBrowser() {
if (typeof window !== 'undefined') {
return true
} else {
return false
}
}
67 changes: 33 additions & 34 deletions megalodon/src/entities/account.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
/// <reference path="emoji.ts" />
/// <reference path="source.ts" />
/// <reference path="field.ts" />
/// <reference path="role.ts" />
namespace Entity {
export type Account = {
id: string
username: string
acct: string
display_name: string
locked: boolean
discoverable?: boolean
group: boolean | null
noindex: boolean | null
suspended: boolean | null
limited: boolean | null
created_at: string
followers_count: number
following_count: number
statuses_count: number
note: string
url: string
avatar: string
avatar_static: string
header: string
header_static: string
emojis: Array<Emoji>
moved: Account | null
fields: Array<Field>
bot: boolean | null
source?: Source
role?: Role
mute_expires_at?: string
}
import { Emoji } from './emoji'
import { Field } from './field'
import { Source } from './source'
import { Role } from './role'

export type Account = {
id: string
username: string
acct: string
display_name: string
locked: boolean
discoverable?: boolean
group: boolean | null
noindex: boolean | null
suspended: boolean | null
limited: boolean | null
created_at: string
followers_count: number
following_count: number
statuses_count: number
note: string
url: string
avatar: string
avatar_static: string
header: string
header_static: string
emojis: Array<Emoji>
moved: Account | null
fields: Array<Field>
bot: boolean | null
source?: Source
role?: Role
mute_expires_at?: string
}
12 changes: 5 additions & 7 deletions megalodon/src/entities/activity.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
namespace Entity {
export type Activity = {
week: string
statuses: string
logins: string
registrations: string
}
export type Activity = {
week: string
statuses: string
logins: string
registrations: string
}
69 changes: 34 additions & 35 deletions megalodon/src/entities/announcement.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
/// <reference path="emoji.ts" />
import { Emoji } from './emoji'
import { StatusTag } from './status'

namespace Entity {
export type Announcement = {
id: string
content: string
starts_at: string | null
ends_at: string | null
published: boolean
all_day: boolean
published_at: string
updated_at: string | null
read: boolean | null
mentions: Array<AnnouncementAccount>
statuses: Array<AnnouncementStatus>
tags: Array<StatusTag>
emojis: Array<Emoji>
reactions: Array<AnnouncementReaction>
}
export type Announcement = {
id: string
content: string
starts_at: string | null
ends_at: string | null
published: boolean
all_day: boolean
published_at: string
updated_at: string | null
read: boolean | null
mentions: Array<AnnouncementAccount>
statuses: Array<AnnouncementStatus>
tags: Array<StatusTag>
emojis: Array<Emoji>
reactions: Array<AnnouncementReaction>
}

export type AnnouncementAccount = {
id: string
username: string
url: string
acct: string
}
export type AnnouncementAccount = {
id: string
username: string
url: string
acct: string
}

export type AnnouncementStatus = {
id: string
url: string
}
export type AnnouncementStatus = {
id: string
url: string
}

export type AnnouncementReaction = {
name: string
count: number
me: boolean | null
url: string | null
static_url: string | null
}
export type AnnouncementReaction = {
name: string
count: number
me: boolean | null
url: string | null
static_url: string | null
}
10 changes: 4 additions & 6 deletions megalodon/src/entities/application.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace Entity {
export type Application = {
name: string
website?: string | null
vapid_key?: string | null
}
export type Application = {
name: string
website?: string | null
vapid_key?: string | null
}
25 changes: 12 additions & 13 deletions megalodon/src/entities/async_attachment.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/// <reference path="attachment.ts" />
namespace Entity {
export type AsyncAttachment = {
id: string
type: 'unknown' | 'image' | 'gifv' | 'video' | 'audio'
url: string | null
remote_url: string | null
preview_url: string
text_url: string | null
meta: Meta | null
description: string | null
blurhash: string | null
}
import { Meta } from './attachment'

export type AsyncAttachment = {
id: string
type: 'unknown' | 'image' | 'gifv' | 'video' | 'audio'
url: string | null
remote_url: string | null
preview_url: string
text_url: string | null
meta: Meta | null
description: string | null
blurhash: string | null
}
Loading

0 comments on commit 23d1e28

Please sign in to comment.