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

ES module refactor #513

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 3 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ module.exports = {
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended'
'plugin:@typescript-eslint/recommended',
'plugin:import/typescript'
],
parserOptions: {
ecmaVersion: 2018
ecmaVersion: 2020
},
rules: {
camelcase: 'off',
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
"name": "noderssbot",
"version": "0.10.1",
"description": "Another Telegram RSSBot in Node.js",
"main": "dist/source",
"exports": "./dist/source/index.js",
"engines": {
"node": ">=12.13"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"type": "module",
"scripts": {
"build": "del-cli dist && tsc && cpy source/template dist/source/template && cpy i18n dist/i18n",
"build": "del-cli dist && tsc && cpy source/template dist/source/template && cpy i18n dist/i18n && cpy package.json dist",
"start": "cross-env NODE_PRODUTION=true node dist/source/index.js",
"start-withsnapshot": "cross-env NODE_PRODUTION=true node --heapsnapshot-signal=SIGUSR2 dist/source/index.js",
"dev": "node $NODE_DEBUG_OPTIONS dist/source/index",
Expand Down
12 changes: 7 additions & 5 deletions source/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import * as path from 'path';
import { Config } from './types/config';
import { version } from '../package.json';
import env from 'env-var';
import { createRequire } from 'module';
import { fileURLToPath } from 'url';

const PKGROOT = path.join(
__dirname,
__dirname.includes('dist') ? '../..' : '..'
);
const cjsRequire = createRequire(import.meta.url);
const { version } = cjsRequire('../package.json');

const PKGROOT = fileURLToPath(new URL('../..', import.meta.url));
console.log(PKGROOT);
export const config: Config = {
token: env.get('RSSBOT_TOKEN').required().asString(),
proxy: {
Expand Down
8 changes: 4 additions & 4 deletions source/controlers/import-reply.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { getUserById } from '../proxies/users';
import i18n from '../i18n';
import { getUserById } from '../proxies/users.js';
import i18n from '../i18n.js';
import { MContext, Next } from '../types/ctx';
import { config } from '../config';
import { isSome } from '../types/option';
import { config } from '../config.js';
import { isSome } from '../types/option.js';

export default async (ctx: MContext, next?: Next): Promise<void> => {
const user = await getUserById(ctx.message.chat.id);
Expand Down
4 changes: 2 additions & 2 deletions source/controlers/language.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { setLangById } from '../proxies/users';
import i18n from '../i18n';
import { setLangById } from '../proxies/users.js';
import i18n from '../i18n.js';
import { MContext, Next } from '../types/ctx';

const chunk = (input: any[], size: number) => {
Expand Down
14 changes: 7 additions & 7 deletions source/controlers/rss.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as RSS from '../proxies/rss-feed';
import i18n from '../i18n';
import twoKeyReply from '../utils/two-key-reply';
import errors from '../utils/errors';
import * as RSS from '../proxies/rss-feed.js';
import i18n from '../i18n.js';
import twoKeyReply from '../utils/two-key-reply.js';
import errors from '../utils/errors.js';
import { MContext, Next } from '../types/ctx';
import { isNone } from '../types/option';
import { decodeUrl, encodeUrl } from '../utils/urlencode';
import sanitize from '../utils/sanitize';
import { isNone } from '../types/option.js';
import { decodeUrl, encodeUrl } from '../utils/urlencode.js';
import sanitize from '../utils/sanitize.js';

export async function sub(ctx: MContext, next: Next): Promise<void> {
const { feedUrl, chat, lang } = ctx.state;
Expand Down
6 changes: 3 additions & 3 deletions source/database/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import knex from 'knex';
import { config } from '../config';
import knexConfig from '../knexfile';
import logger from '../utils/logger';
import { config } from '../config.js';
import knexConfig from '../knexfile.js';
import logger from '../utils/logger.js';
export const db = knex({
...knexConfig,
debug: process.env.NODE_ENV === 'development'
Expand Down
3 changes: 3 additions & 0 deletions source/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import * as yaml from 'js-yaml';
import * as fs from 'fs';
import * as path from 'path';
import { I18n, I18nLang } from './types/i18n';
import { fileURLToPath } from 'url';

const __dirname = fileURLToPath(new URL('../i18n', import.meta.url));
const result: I18n = {};
const localeDir = path.join(__dirname, '../i18n'); // /dist/source/i18n -> /dist/[i18n]
console.log(__dirname, localeDir);
const baseStr = fs.readFileSync(path.join(localeDir, 'en.yaml'), {
encoding: 'utf8'
});
Expand Down
54 changes: 28 additions & 26 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import Telegraf from 'telegraf';
import { fork } from 'child_process';
import { join } from 'path';
import * as pkg from '../package.json';
import send from './utils/send';
import logger from './utils/logger';
import errors from './utils/errors';
import i18n from './i18n';
import { initDB } from './database';
import send from './utils/send.js';
import logger from './utils/logger.js';
import errors from './utils/errors.js';
import i18n from './i18n.js';
import { initDB } from './database/index.js';
import cleanStack from 'clean-stack';
import { replyKeyboard, changeLangCallback } from './controlers/language';
import { replyKeyboard, changeLangCallback } from './controlers/language.js';
import {
getUrlById,
rss,
Expand All @@ -17,33 +16,36 @@ import {
unsubAll,
viewAll,
getActiveFeedWithErrorCount
} from './controlers/rss';
import importReply from './controlers/import-reply';
import { config } from './config';
import agent from './utils/agent';
} from './controlers/rss.js';
import importReply from './controlers/import-reply.js';
import { config } from './config.js';
import agent from './utils/agent.js';
const { token, view_all, lang, item_num, db_path, not_send } = config;

import getUrl from './middlewares/get-url';
import getUrlByTitle from './middlewares/get-url-by-title';
import getFileLink from './middlewares/get-file-link';
import sendError from './middlewares/send-error';
import testUrl from './middlewares/test-url';
import isAdmin from './middlewares/is-admin';
import onlyPrivateChat from './middlewares/only-private-chat';
import subMultiUrl from './middlewares/sub-multi-url';
import exportToOpml from './middlewares/export-to-opml';
import importFromOpml from './middlewares/import-from-opml';
import userAllowList from './middlewares/user_allow_list';
import getUrl from './middlewares/get-url.js';
import getUrlByTitle from './middlewares/get-url-by-title.js';
import getFileLink from './middlewares/get-file-link.js';
import sendError from './middlewares/send-error.js';
import testUrl from './middlewares/test-url.js';
import isAdmin from './middlewares/is-admin.js';
import onlyPrivateChat from './middlewares/only-private-chat.js';
import subMultiUrl from './middlewares/sub-multi-url.js';
import exportToOpml from './middlewares/export-to-opml.js';
import importFromOpml from './middlewares/import-from-opml.js';
import userAllowList from './middlewares/user_allow_list.js';
import { MContext, Next } from './types/ctx';
import twoKeyReply from './utils/two-key-reply';
import twoKeyReply from './utils/two-key-reply.js';
import {
isChangeFeedUrl,
isErrorMaxTime,
isSuccess,
Message
} from './types/message';
import { migrateUser } from './proxies/users';
import { encodeUrl } from './utils/urlencode';
} from './types/message.js';
import { migrateUser } from './proxies/users.js';
import { encodeUrl } from './utils/urlencode.js';
import { createRequire } from 'module';
const cjsRequire = createRequire(import.meta.url);
const pkg = cjsRequire('../package.json');

const bot = new Telegraf(token, {
telegram: {
Expand Down
11 changes: 6 additions & 5 deletions source/knexfile.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* eslint @typescript-eslint/explicit-module-boundary-types: 0 */
import { config } from './config';
import { parse } from 'url';
import { config } from './config.js';
import { parse, fileURLToPath } from 'url';
import { Knex } from 'knex';
import { join } from 'path';
import logger from './utils/logger';
import logger from './utils/logger.js';

const parsed = parse(config.db_path);
const isWindows = process && process.platform && process.platform === 'win32';
Expand All @@ -21,9 +20,11 @@ const knexConfig: Knex.Config = {
client,
connection: config.db_path,
migrations: {
// @ts-expect-error esm
esm: true,
tableName: 'knex_migrations',
extension: 'ts',
directory: join(__dirname, 'migrations')
directory: fileURLToPath(new URL('migrations', import.meta.url))
},
pool: {
min: 1,
Expand Down
11 changes: 6 additions & 5 deletions source/middlewares/export-to-opml.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { getSubscribedFeedsByUserId } from '../proxies/rss-feed';
import errors from '../utils/errors';
import * as path from 'path';
import * as ejs from 'ejs';
import * as fs from 'fs';
import * as ejs from 'ejs';
import { htmlEscape } from 'escape-goat';

import { getSubscribedFeedsByUserId } from '../proxies/rss-feed.js';
import errors from '../utils/errors.js';
import { config } from '../config.js';
import { MContext, Next } from '../types/ctx';
import { Feed } from '../types/feed';
import { config } from '../config';
import { htmlEscape } from 'escape-goat';

function readFilePromise(path: string): Promise<string> {
return new Promise((resolve, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion source/middlewares/get-file-link.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import errors from '../utils/errors';
import errors from '../utils/errors.js';
import { MContext, Next } from '../types/ctx';

export default async (ctx: MContext, next: Next): Promise<void> => {
Expand Down
6 changes: 3 additions & 3 deletions source/middlewares/get-url-by-title.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { MContext, Next } from '../types/ctx';

import { getFeedsByTitle } from '../proxies/rss-feed';
import errors from '../utils/errors';
import { decodeUrl } from '../utils/urlencode';
import { getFeedsByTitle } from '../proxies/rss-feed.js';
import errors from '../utils/errors.js';
import { decodeUrl } from '../utils/urlencode.js';
export default async (ctx: MContext, next: Next): Promise<void> => {
const me = await ctx.telegram.getMe();
const myId = me.id;
Expand Down
8 changes: 4 additions & 4 deletions source/middlewares/get-url.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import errors from '../utils/errors';
import { getSubscribedFeedsByUserId } from '../proxies/rss-feed';
import i18n from '../i18n';
import errors from '../utils/errors.js';
import { getSubscribedFeedsByUserId } from '../proxies/rss-feed.js';
import i18n from '../i18n.js';
import { MContext, Next } from '../types/ctx';
import { decodeUrl } from '../utils/urlencode';
import { decodeUrl } from '../utils/urlencode.js';

export default async (ctx: MContext, next: Next): Promise<void> => {
const { lang } = ctx.state;
Expand Down
8 changes: 4 additions & 4 deletions source/middlewares/import-from-opml.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Outline } from '../types/outline';
import got from '../utils/got';
import got from '../utils/got.js';
import { transform } from 'camaro';
import errors from '../utils/errors';
import { sub } from '../proxies/rss-feed';
import i18n from '../i18n';
import errors from '../utils/errors.js';
import { sub } from '../proxies/rss-feed.js';
import i18n from '../i18n.js';
import { MContext, Next } from '../types/ctx';

const getOutlines = async function (data: string): Promise<Outline[]> {
Expand Down
8 changes: 4 additions & 4 deletions source/middlewares/is-admin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { config } from '../config';
import errors from '../utils/errors';
import { getUserById, newUser } from '../proxies/users';
import { config } from '../config.js';
import errors from '../utils/errors.js';
import { getUserById, newUser } from '../proxies/users.js';
import { MContext, Next } from '../types/ctx';
import { User } from 'telegraf/typings/telegram-types';
import { isNone, Option } from '../types/option';
import { isNone, Option } from '../types/option.js';
import { User as DBUser } from '../types/user';

/**
Expand Down
2 changes: 1 addition & 1 deletion source/middlewares/only-private-chat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import errors from '../utils/errors';
import errors from '../utils/errors.js';
import { MContext, Next } from '../types/ctx';
export default async (ctx: MContext, next: Next): Promise<void> => {
ctx.state.chat = await ctx.getChat();
Expand Down
6 changes: 3 additions & 3 deletions source/middlewares/send-error.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import i18n from '../i18n';
import logger from '../utils/logger';
import errors from '../utils/errors';
import i18n from '../i18n.js';
import logger from '../utils/logger.js';
import errors from '../utils/errors.js';
import { MContext, Next } from '../types/ctx';

export default async (ctx: MContext, next: Next): Promise<void> => {
Expand Down
14 changes: 7 additions & 7 deletions source/middlewares/sub-multi-url.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import errors from '../utils/errors';
import got from '../utils/got';
import { getFeedByUrl, sub } from '../proxies/rss-feed';
import i18n from '../i18n';
import errors from '../utils/errors.js';
import got from '../utils/got.js';
import { getFeedByUrl, sub } from '../proxies/rss-feed.js';
import i18n from '../i18n.js';
import { MContext, Next } from '../types/ctx';
import { isSome } from '../types/option';
import { isSome } from '../types/option.js';
import { Feed } from '../types/feed';
import { parseString } from '../parser/parse';
import { decodeUrl, encodeUrl } from '../utils/urlencode';
import { parseString } from '../parser/parse.js';
import { decodeUrl, encodeUrl } from '../utils/urlencode.js';

export default async (ctx: MContext, next: Next): Promise<void> => {
const urls = ctx.message.text.match(
Expand Down
16 changes: 8 additions & 8 deletions source/middlewares/test-url.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import got from '../utils/got';
import { findFeed, isFeedValid } from '../utils/feed';
import errors from '../utils/errors';
import i18n from '../i18n';
import { getFeedByUrl } from '../proxies/rss-feed';
import got from '../utils/got.js';
import { findFeed, isFeedValid } from '../utils/feed.js';
import errors from '../utils/errors.js';
import i18n from '../i18n.js';
import { getFeedByUrl } from '../proxies/rss-feed.js';
import { MContext, Next } from '../types/ctx';
import { isNone, isSome } from '../types/option';
import { parseString } from '../parser/parse';
import { decodeUrl, encodeUrl } from '../utils/urlencode';
import { isNone, isSome } from '../types/option.js';
import { parseString } from '../parser/parse.js';
import { decodeUrl, encodeUrl } from '../utils/urlencode.js';

export default async (ctx: MContext, next: Next): Promise<void> => {
const url = encodeUrl(ctx.state.feedUrl);
Expand Down
8 changes: 4 additions & 4 deletions source/middlewares/user_allow_list.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { config } from '../config';
import i18n from '../i18n';
import { config } from '../config.js';
import i18n from '../i18n.js';
import { MContext, Next } from '../types/ctx';
import { getUserById } from '../proxies/users';
import { isSome } from '../types/option';
import { getUserById } from '../proxies/users.js';
import { isSome } from '../types/option.js';

export default async (ctx: MContext, next: Next): Promise<void> => {
let id: number;
Expand Down
4 changes: 2 additions & 2 deletions source/parser/parse.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { detectFeed } from './detect-feed';
import { detectFeed } from './detect-feed.js';
import { transform } from 'camaro';
import { atomTpl, rdfTpl, rss2Tpl } from './templates';
import { atomTpl, rdfTpl, rss2Tpl } from './templates.js';
export type TRSS = {
version: string;
title: string;
Expand Down
8 changes: 4 additions & 4 deletions source/proxies/rss-feed.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { db } from '../database';
import errors from '../utils/errors';
import { db } from '../database/index.js';
import errors from '../utils/errors.js';
import { Feed } from '../types/feed';
import { Subscribe } from '../types/subscribe';
import { isSome, Option, Optional, Some } from '../types/option';
import { decodeUrl } from '../utils/urlencode';
import { isSome, Option, Optional, Some } from '../types/option.js';
import { decodeUrl } from '../utils/urlencode.js';

export async function sub(
userId: number,
Expand Down
4 changes: 2 additions & 2 deletions source/proxies/subscribes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import errors from '../utils/errors';
import errors from '../utils/errors.js';
import { Subscribe } from '../types/subscribe';
import { db } from '../database';
import { db } from '../database/index.js';

export async function getSubscribersByFeedId(
feedId: number
Expand Down
Loading