refs #571 Use dayjs instead of momentjs

This commit is contained in:
AkiraFukushima 2021-02-04 00:39:51 +09:00
parent c8c3578f35
commit af2ca3f35a
8 changed files with 27 additions and 39 deletions

View file

@ -2032,7 +2032,6 @@
"@types/oauth": "^0.9.0", "@types/oauth": "^0.9.0",
"@types/ws": "^7.2.0", "@types/ws": "^7.2.0",
"https-proxy-agent": "^5.0.0", "https-proxy-agent": "^5.0.0",
"moment": "^2.24.0",
"oauth": "^0.9.15", "oauth": "^0.9.15",
"socks-proxy-agent": "^5.0.0", "socks-proxy-agent": "^5.0.0",
"ws": "^7.2.1" "ws": "^7.2.1"
@ -5624,11 +5623,6 @@
} }
} }
}, },
"moment": {
"version": "2.24.0",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.24.0.tgz",
"integrity": "sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg=="
},
"ms": { "ms": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",

View file

@ -75,7 +75,6 @@
"@types/oauth": "^0.9.0", "@types/oauth": "^0.9.0",
"@types/ws": "^7.2.0", "@types/ws": "^7.2.0",
"https-proxy-agent": "^5.0.0", "https-proxy-agent": "^5.0.0",
"moment": "^2.24.0",
"oauth": "^0.9.15", "oauth": "^0.9.15",
"socks-proxy-agent": "^5.0.0", "socks-proxy-agent": "^5.0.0",
"uuid": "^8.0.0", "uuid": "^8.0.0",
@ -3717,11 +3716,6 @@
} }
} }
}, },
"moment": {
"version": "2.24.0",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.24.0.tgz",
"integrity": "sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg=="
},
"ms": { "ms": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",

View file

@ -56,8 +56,8 @@
"@types/oauth": "^0.9.0", "@types/oauth": "^0.9.0",
"@types/ws": "^7.2.0", "@types/ws": "^7.2.0",
"axios": "^0.21.0", "axios": "^0.21.0",
"dayjs": "^1.10.4",
"https-proxy-agent": "^5.0.0", "https-proxy-agent": "^5.0.0",
"moment": "^2.24.0",
"oauth": "^0.9.15", "oauth": "^0.9.15",
"socks-proxy-agent": "^5.0.0", "socks-proxy-agent": "^5.0.0",
"typescript": "3.9.7", "typescript": "3.9.7",

View file

@ -1,5 +1,5 @@
import WS from 'ws' import WS from 'ws'
import moment, { Moment } from 'moment' import dayjs, { Dayjs } from 'dayjs'
import { EventEmitter } from 'events' import { EventEmitter } from 'events'
import proxyAgent, { ProxyConfig } from '../proxy_config' import proxyAgent, { ProxyConfig } from '../proxy_config'
import { WebSocketInterface } from '../megalodon' import { WebSocketInterface } from '../megalodon'
@ -23,7 +23,7 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
private _reconnectCurrentAttempts: number private _reconnectCurrentAttempts: number
private _connectionClosed: boolean private _connectionClosed: boolean
private _client: WS | null private _client: WS | null
private _pongReceivedTimestamp: Moment private _pongReceivedTimestamp: Dayjs
private _heartbeatInterval: number = 60000 private _heartbeatInterval: number = 60000
private _pongWaiting: boolean = false private _pongWaiting: boolean = false
@ -57,7 +57,7 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
this._reconnectCurrentAttempts = 0 this._reconnectCurrentAttempts = 0
this._connectionClosed = false this._connectionClosed = false
this._client = null this._client = null
this._pongReceivedTimestamp = moment() this._pongReceivedTimestamp = dayjs()
} }
/** /**
@ -209,7 +209,7 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
client.on('pong', () => { client.on('pong', () => {
this._pongWaiting = false this._pongWaiting = false
this.emit('pong', {}) this.emit('pong', {})
this._pongReceivedTimestamp = moment() this._pongReceivedTimestamp = dayjs()
// It is required to anonymous function since get this scope in checkAlive. // It is required to anonymous function since get this scope in checkAlive.
setTimeout(() => this._checkAlive(this._pongReceivedTimestamp), this._heartbeatInterval) setTimeout(() => this._checkAlive(this._pongReceivedTimestamp), this._heartbeatInterval)
}) })
@ -255,8 +255,8 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
/** /**
* Call ping and wait to pong. * Call ping and wait to pong.
*/ */
private _checkAlive(timestamp: Moment) { private _checkAlive(timestamp: Dayjs) {
const now: Moment = moment() const now: Dayjs = dayjs()
// Block multiple calling, if multiple pong event occur. // Block multiple calling, if multiple pong event occur.
// It the duration is less than interval, through ping. // It the duration is less than interval, through ping.
if (now.diff(timestamp) > this._heartbeatInterval - 1000 && !this._connectionClosed) { if (now.diff(timestamp) > this._heartbeatInterval - 1000 && !this._connectionClosed) {

View file

@ -1,5 +1,5 @@
import axios, { AxiosResponse, CancelTokenSource, AxiosRequestConfig } from 'axios' import axios, { AxiosResponse, CancelTokenSource, AxiosRequestConfig } from 'axios'
import moment from 'moment' import dayjs from 'dayjs'
import { DEFAULT_UA } from '../default' import { DEFAULT_UA } from '../default'
import proxyAgent, { ProxyConfig } from '../proxy_config' import proxyAgent, { ProxyConfig } from '../proxy_config'
import Response from '../response' import Response from '../response'
@ -184,8 +184,8 @@ namespace MisskeyAPI {
} }
export const poll = (p: Entity.Poll): MegalodonEntity.Poll => { export const poll = (p: Entity.Poll): MegalodonEntity.Poll => {
const now = moment() const now = dayjs()
const expire = moment(p.expiresAt) const expire = dayjs(p.expiresAt)
const count = p.choices.reduce((sum, choice) => sum + choice.votes, 0) const count = p.choices.reduce((sum, choice) => sum + choice.votes, 0)
return { return {
id: '', id: '',

View file

@ -1,5 +1,5 @@
import WS from 'ws' import WS from 'ws'
import moment, { Moment } from 'moment' import dayjs, { Dayjs } from 'dayjs'
import { v4 as uuid } from 'uuid' import { v4 as uuid } from 'uuid'
import { EventEmitter } from 'events' import { EventEmitter } from 'events'
import { WebSocketInterface } from '../megalodon' import { WebSocketInterface } from '../megalodon'
@ -25,7 +25,7 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
private _connectionClosed: boolean private _connectionClosed: boolean
private _client: WS | null = null private _client: WS | null = null
private _channelID: string private _channelID: string
private _pongReceivedTimestamp: Moment private _pongReceivedTimestamp: Dayjs
private _heartbeatInterval: number = 60000 private _heartbeatInterval: number = 60000
private _pongWaiting: boolean = false private _pongWaiting: boolean = false
@ -58,7 +58,7 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
this._reconnectCurrentAttempts = 0 this._reconnectCurrentAttempts = 0
this._connectionClosed = false this._connectionClosed = false
this._channelID = uuid() this._channelID = uuid()
this._pongReceivedTimestamp = moment() this._pongReceivedTimestamp = dayjs()
} }
/** /**
@ -253,7 +253,7 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
client.on('pong', () => { client.on('pong', () => {
this._pongWaiting = false this._pongWaiting = false
this.emit('pong', {}) this.emit('pong', {})
this._pongReceivedTimestamp = moment() this._pongReceivedTimestamp = dayjs()
// It is required to anonymous function since get this scope in checkAlive. // It is required to anonymous function since get this scope in checkAlive.
setTimeout(() => this._checkAlive(this._pongReceivedTimestamp), this._heartbeatInterval) setTimeout(() => this._checkAlive(this._pongReceivedTimestamp), this._heartbeatInterval)
}) })
@ -294,8 +294,8 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
/** /**
* Call ping and wait to pong. * Call ping and wait to pong.
*/ */
private _checkAlive(timestamp: Moment) { private _checkAlive(timestamp: Dayjs) {
const now: Moment = moment() const now: Dayjs = dayjs()
// Block multiple calling, if multiple pong event occur. // Block multiple calling, if multiple pong event occur.
// It the duration is less than interval, through ping. // It the duration is less than interval, through ping.
if (now.diff(timestamp) > this._heartbeatInterval - 1000 && !this._connectionClosed) { if (now.diff(timestamp) > this._heartbeatInterval - 1000 && !this._connectionClosed) {

View file

@ -1,5 +1,5 @@
import WS from 'ws' import WS from 'ws'
import moment, { Moment } from 'moment' import dayjs, { Dayjs } from 'dayjs'
import { EventEmitter } from 'events' import { EventEmitter } from 'events'
import proxyAgent, { ProxyConfig } from '../proxy_config' import proxyAgent, { ProxyConfig } from '../proxy_config'
@ -24,7 +24,7 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
private _reconnectCurrentAttempts: number private _reconnectCurrentAttempts: number
private _connectionClosed: boolean private _connectionClosed: boolean
private _client: WS | null private _client: WS | null
private _pongReceivedTimestamp: Moment private _pongReceivedTimestamp: Dayjs
private _heartbeatInterval: number = 60000 private _heartbeatInterval: number = 60000
private _pongWaiting: boolean = false private _pongWaiting: boolean = false
@ -58,7 +58,7 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
this._reconnectCurrentAttempts = 0 this._reconnectCurrentAttempts = 0
this._connectionClosed = false this._connectionClosed = false
this._client = null this._client = null
this._pongReceivedTimestamp = moment() this._pongReceivedTimestamp = dayjs()
} }
/** /**
@ -210,7 +210,7 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
client.on('pong', () => { client.on('pong', () => {
this._pongWaiting = false this._pongWaiting = false
this.emit('pong', {}) this.emit('pong', {})
this._pongReceivedTimestamp = moment() this._pongReceivedTimestamp = dayjs()
// It is required to anonymous function since get this scope in checkAlive. // It is required to anonymous function since get this scope in checkAlive.
setTimeout(() => this._checkAlive(this._pongReceivedTimestamp), this._heartbeatInterval) setTimeout(() => this._checkAlive(this._pongReceivedTimestamp), this._heartbeatInterval)
}) })
@ -256,8 +256,8 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
/** /**
* Call ping and wait to pong. * Call ping and wait to pong.
*/ */
private _checkAlive(timestamp: Moment) { private _checkAlive(timestamp: Dayjs) {
const now: Moment = moment() const now: Dayjs = dayjs()
// Block multiple calling, if multiple pong event occur. // Block multiple calling, if multiple pong event occur.
// It the duration is less than interval, through ping. // It the duration is less than interval, through ping.
if (now.diff(timestamp) > this._heartbeatInterval - 1000 && !this._connectionClosed) { if (now.diff(timestamp) > this._heartbeatInterval - 1000 && !this._connectionClosed) {

View file

@ -1399,6 +1399,11 @@ data-urls@^2.0.0:
whatwg-mimetype "^2.3.0" whatwg-mimetype "^2.3.0"
whatwg-url "^8.0.0" whatwg-url "^8.0.0"
dayjs@^1.10.4:
version "1.10.4"
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.10.4.tgz#8e544a9b8683f61783f570980a8a80eaf54ab1e2"
integrity sha512-RI/Hh4kqRc1UKLOAf/T5zdMMX5DQIlDxwUe3wSyMMnEbGunnpENCdbUgM+dW7kXidZqCttBrmw7BhN4TMddkCw==
debug@4, debug@^4.0.1, debug@^4.1.0, debug@^4.1.1: debug@4, debug@^4.0.1, debug@^4.1.0, debug@^4.1.1:
version "4.1.1" version "4.1.1"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791" resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791"
@ -3365,11 +3370,6 @@ mkdirp@^0.5.1:
dependencies: dependencies:
minimist "^1.2.5" minimist "^1.2.5"
moment@^2.24.0:
version "2.29.1"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.1.tgz#b2be769fa31940be9eeea6469c075e35006fa3d3"
integrity sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==
ms@2.0.0: ms@2.0.0:
version "2.0.0" version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"