Skip to content

Commit

Permalink
feat: re-organize package
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed May 21, 2023
1 parent 49d73ea commit ea60904
Show file tree
Hide file tree
Showing 44 changed files with 448 additions and 265 deletions.
1 change: 1 addition & 0 deletions config.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './build/config'
1 change: 1 addition & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./build/config')
1 change: 1 addition & 0 deletions constant.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './build/constant'
1 change: 1 addition & 0 deletions constant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./build/constant')
1 change: 1 addition & 0 deletions generator.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './build/generator'
1 change: 1 addition & 0 deletions generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./build/generator')
17 changes: 16 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,28 @@
"files": [
"bin",
"build",
"hygen-template"
"hygen-template",
"index.js",
"index.d.ts",
"internal.js",
"internal.d.ts",
"generator.js",
"generator.d.ts",
"provider.js",
"provider.d.ts",
"constant.js",
"constant.d.ts",
"utils.js",
"utils.d.ts",
"config.js",
"config.d.ts"
],
"bugs": {
"url": "https://github.com/geekdada/surgio/issues"
},
"homepage": "https://surgio.royli.dev",
"scripts": {
"dev": "pnpm build --watch",
"test": "run-s build test:*",
"test:types": "tsc --noEmit",
"test:lint": "eslint -c .eslintrc.js --ext .js,.ts .",
Expand Down
1 change: 1 addition & 0 deletions provider.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './build/provider'
1 change: 1 addition & 0 deletions provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./build/provider')
5 changes: 2 additions & 3 deletions src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import fs from 'fs-extra'
import path from 'path'

import BaseCommand from '../base-command'
import { Artifact } from '../generator/artifact'
import { getEngine } from '../generator/template'
import { Artifact, getEngine } from '../generator'
import redis from '../redis'
import { ArtifactConfig } from '../types'
import { setConfig } from '../config'
import { checkAndFix } from '../utils/linter'
import { loadRemoteSnippetList } from '../utils/remote-snippet'
import { loadRemoteSnippetList } from '../utils'

class GenerateCommand extends BaseCommand<typeof GenerateCommand> {
static description = '生成规则'
Expand Down
2 changes: 1 addition & 1 deletion src/commands/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import ShadowsocksSubscribeProvider from '../provider/ShadowsocksSubscribeProvid
import V2rayNSubscribeProvider from '../provider/V2rayNSubscribeProvider'
import redis from '../redis'
import { getProvider } from '../provider'
import { formatSubscriptionUserInfo } from '../utils/subscription'
import { formatSubscriptionUserInfo } from '../utils'

const logger = createLogger({
service: 'surgio:SubscriptionsCommand',
Expand Down
5 changes: 4 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import fs from 'fs-extra'
import _ from 'lodash'
import path from 'path'
import { URL } from 'url'

import {
INTERNET_TEST_URL,
PROXY_TEST_INTERVAL,
PROXY_TEST_URL,
} from './constant'

import redis from './redis'
import { CommandConfig, CommandConfigBeforeNormalize } from './types'
import { SurgioConfigValidator } from './validators'
Expand Down Expand Up @@ -121,6 +121,9 @@ export const normalizeConfig = (
cache: {
type: 'default',
},
gateway: {
passRequestUserAgent: false,
},
}
const config: CommandConfig = _.defaultsDeep(userConfig, defaultConfig)

Expand Down
2 changes: 1 addition & 1 deletion src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { Environment } from 'nunjucks'

import { Artifact } from './generator/artifact'
import { Artifact } from './generator'
import { ArtifactConfig, CommandConfig, RemoteSnippet } from './types'

export async function generate(
Expand Down
35 changes: 13 additions & 22 deletions src/generator/artifact.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { logger } from '@surgio/logger'
import assert from 'assert'
import Bluebird from 'bluebird'
import fs from 'fs-extra'
import _ from 'lodash'
Expand All @@ -8,13 +7,15 @@ import path from 'path'
import { EventEmitter } from 'events'

import { getProvider, PossibleProviderType } from '../provider'
import { GetNodeListParams } from '../provider/types'
import {
ArtifactConfig,
CommandConfig,
NodeTypeEnum,
PossibleNodeConfigType,
ProviderConfig,
RemoteSnippet,
ThenArg,
} from '../types'
import {
getClashNodeNames,
Expand Down Expand Up @@ -67,16 +68,12 @@ import {
import { prependFlag, removeFlag } from '../utils/flag'
import { loadLocalSnippet } from './template'

type ThenArg<T> = T extends PromiseLike<infer U> ? U : T

export interface ArtifactOptions {
readonly remoteSnippetList?: ReadonlyArray<RemoteSnippet>
readonly templateEngine?: Environment
}

export interface ExtendableRenderContext {
readonly urlParams?: Record<string, string>
}
export type ExtendableRenderContext = Record<string, string>

export class Artifact extends EventEmitter {
public initProgress = 0
Expand All @@ -101,14 +98,6 @@ export class Artifact extends EventEmitter {
) {
super()

const { name: artifactName, template, templateString } = artifact

assert(artifactName, '必须指定 artifact 的 name 属性')
assert(artifact.provider, '必须指定 artifact 的 provider 属性')
if (!templateString) {
assert(template, '必须指定 artifact 的 template 属性')
}

const mainProviderName = artifact.provider
const combineProviders = artifact.combineProviders || []

Expand Down Expand Up @@ -137,7 +126,7 @@ export class Artifact extends EventEmitter {
{},
globalCustomParams,
customParams,
extendRenderContext?.urlParams,
extendRenderContext,
)

return {
Expand Down Expand Up @@ -198,12 +187,14 @@ export class Artifact extends EventEmitter {
youtubePremiumFilter,
customFilters,
customParams: mergedCustomParams,
}
} as const
}

public async init({
requestUserAgent,
}: { requestUserAgent?: string } = {}): Promise<this> {
public async init(
params: {
getNodeListParams?: GetNodeListParams
} = {},
): Promise<this> {
if (this.isReady) {
throw new Error('Artifact 已经初始化完成')
}
Expand All @@ -213,7 +204,7 @@ export class Artifact extends EventEmitter {
await Bluebird.map(
this.providerNameList,
async (providerName) => {
await this.providerMapper(providerName, { requestUserAgent })
await this.providerMapper(providerName, params.getNodeListParams)
},
{
concurrency: getNetworkConcurrency(),
Expand Down Expand Up @@ -270,7 +261,7 @@ export class Artifact extends EventEmitter {

private async providerMapper(
providerName: string,
{ requestUserAgent }: { requestUserAgent?: string } = {},
getNodeListParams: GetNodeListParams = {},
): Promise<void> {
const config = this.surgioConfig
const mainProviderName = this.artifact.provider
Expand Down Expand Up @@ -302,7 +293,7 @@ export class Artifact extends EventEmitter {
}

try {
nodeConfigList = await provider.getNodeList({ requestUserAgent })
nodeConfigList = await provider.getNodeList(getNodeListParams)
} catch (err) /* istanbul ignore next */ {
throw new Error(
`获取 Provider ${providerName} 节点时出现错误\n文件地址: ${filePath}`,
Expand Down
2 changes: 2 additions & 0 deletions src/generator/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './artifact'
export * from './template'
3 changes: 1 addition & 2 deletions src/generator/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import { JsonObject } from 'type-fest'
import YAML from 'yaml'

import { RemoteSnippet } from '../types'
import { decodeStringList, toBase64 } from '../utils'
import { decodeStringList, toBase64, addProxyToSurgeRuleSet } from '../utils'
import {
MELLOW_UNSUPPORTED_RULE,
QUANTUMULT_X_SUPPORTED_RULE,
CLASH_SUPPORTED_RULE,
LOON_SUPPORTED_RULE,
SURFBOARD_SUPPORTED_RULE,
} from '../constant'
import { addProxyToSurgeRuleSet } from '../utils/remote-snippet'

export function getEngine(templateDir: string): nunjucks.Environment {
const engine = nunjucks.configure(templateDir, {
Expand Down
7 changes: 0 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { PackageJson } from 'type-fest'

import {
isAWS,
isAWSLambda,
Expand All @@ -16,11 +14,8 @@ import * as filter from './utils/filter'
import { CATEGORIES } from './constant'

export type { CommandConfigBeforeNormalize as SurgioConfig } from './types'
export * as caches from './utils/cache'
export * from './utils/configurables'

const pkg = require('../package.json') as PackageJson

export const utils = {
...filter,
isHeroku,
Expand All @@ -38,5 +33,3 @@ export const utils = {
export const categories = {
...CATEGORIES,
}

export { pkg }
17 changes: 3 additions & 14 deletions src/internal.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import { loadConfig, setConfig, getConfig } from './config'
import { loadRemoteSnippetList } from './utils/remote-snippet'
import { PackageJson } from 'type-fest'

export * as redis from './redis'
export * from './utils/cache'
export * from './types'

const utils = {
loadRemoteSnippetList,
} as const

const config = {
loadConfig,
setConfig,
getConfig,
} as const

export { utils, config }
export const packageJson = require('../package.json') as PackageJson
39 changes: 25 additions & 14 deletions src/provider/BlackSSLProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { ConfigCache } from '../utils/cache'
import httpClient from '../utils/http-client'
import Provider from './Provider'
import { GetNodeListFunction, GetSubscriptionUserInfoFunction } from './types'

export default class BlackSSLProvider extends Provider {
public readonly username: string
Expand All @@ -36,25 +37,35 @@ export default class BlackSSLProvider extends Provider {
this.supportGetSubscriptionUserInfo = true
}

public async getSubscriptionUserInfo(): Promise<
SubscriptionUserinfo | undefined
> {
const { subscriptionUserinfo } = await this.getBlackSSLConfig(
this.username,
this.password,
)

if (subscriptionUserinfo) {
return subscriptionUserinfo
public getSubscriptionUserInfo: GetSubscriptionUserInfoFunction =
async () => {
const { subscriptionUserinfo } = await this.getBlackSSLConfig(
this.username,
this.password,
)

if (subscriptionUserinfo) {
return subscriptionUserinfo
}
return undefined
}
return void 0
}

public async getNodeList(): Promise<ReadonlyArray<HttpsNodeConfig>> {
public getNodeList: GetNodeListFunction = async (): Promise<
Array<HttpsNodeConfig>
> => {
const { nodeList } = await this.getBlackSSLConfig(
this.username,
this.password,
)

if (this.config.hooks?.afterFetchNodeList) {
const newList = await this.config.hooks.afterFetchNodeList(nodeList, {})

if (newList) {
return newList
}
}

return nodeList
}

Expand All @@ -63,7 +74,7 @@ export default class BlackSSLProvider extends Provider {
username: string,
password: string,
): Promise<{
readonly nodeList: ReadonlyArray<HttpsNodeConfig>
readonly nodeList: Array<HttpsNodeConfig>
readonly subscriptionUserinfo?: SubscriptionUserinfo
}> {
assert(username, '未指定 BlackSSL username.')
Expand Down
Loading

0 comments on commit ea60904

Please sign in to comment.