Skip to content

Commit

Permalink
chore: enable lint and prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
aleortega committed Nov 24, 2023
1 parent 16d37a7 commit 9f3b997
Show file tree
Hide file tree
Showing 16 changed files with 967 additions and 118 deletions.
5 changes: 5 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dist/
*.js
*.d.ts
*.json
src/index.ts
18 changes: 18 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": "@dcl/eslint-config/sdk",
"parserOptions": {
"project": [
"tsconfig.json",
"test/tsconfig.json"
]
},
"rules": {
"prettier/prettier": ["error", {
"printWidth": 120,
"semi": false,
"singleQuote": true,
"trailingComma": "none",
"tabWidth": 2
}]
}
}
7 changes: 5 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
"[typescript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
}
}
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
"scripts": {
"build": "tsc -p tsconfig.json",
"start": "node --trace-warnings --abort-on-uncaught-exception --unhandled-rejections=strict dist/index.js",
"test": "jest --forceExit --detectOpenHandles --coverage --verbose"
"test": "jest --forceExit --detectOpenHandles --coverage --verbose",
"lint:check": "eslint '**/*.{js,ts}'"
},
"devDependencies": {
"@dcl/eslint-config": "^1.1.12",
"@types/node": "^14.17.33",
"@well-known-components/test-helpers": "^1.3.0",
"typescript": "^4.6.2"
"typescript": "^4.9.5"
},
"prettier": {
"printWidth": 120,
"semi": false
"semi": false,
"singleQuote": true,
"trailingComma": "none",
"tabWidth": 2
},
"dependencies": {
"@dcl/catalyst-storage": "^2.0.3",
Expand Down
40 changes: 22 additions & 18 deletions src/adapters/deployer/index.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,59 @@
import { downloadEntityAndContentFiles } from "@dcl/snapshots-fetcher"
import { IDeployerComponent } from "@dcl/snapshots-fetcher/dist/types"
import { SNS } from "aws-sdk"
import { AppComponents } from "../../types"
import { DeploymentToSqs } from "@dcl/schemas/dist/misc/deployments-to-sqs";
import { downloadEntityAndContentFiles } from '@dcl/snapshots-fetcher'
import { IDeployerComponent } from '@dcl/snapshots-fetcher/dist/types'
import { SNS } from 'aws-sdk'
import { AppComponents } from '../../types'
import { DeploymentToSqs } from '@dcl/schemas/dist/misc/deployments-to-sqs'

export function createDeployerComponent(
components: Pick<AppComponents, "logs" | "storage" | "downloadQueue" | "fetch" | "metrics" | "sns">
components: Pick<AppComponents, 'logs' | 'storage' | 'downloadQueue' | 'fetch' | 'metrics' | 'sns'>
): IDeployerComponent {
const logger = components.logs.getLogger("downloader")
const logger = components.logs.getLogger('downloader')

const sns = new SNS()

return {
async deployEntity(entity, servers) {
const markAsDeployed = entity.markAsDeployed ? entity.markAsDeployed : async () => { }
if (entity.entityType == "scene" || entity.entityType == "wearable" || entity.entityType == "emote") {
const markAsDeployed = entity.markAsDeployed ? entity.markAsDeployed : async () => {}
if (entity.entityType === 'scene' || entity.entityType === 'wearable' || entity.entityType === 'emote') {
const exists = await components.storage.exist(entity.entityId)

if (!exists) {
await components.downloadQueue.onSizeLessThan(1000)

components.downloadQueue.scheduleJob(async () => {
logger.info("Downloading entity", { entityId: entity.entityId, entityType: entity.entityType, servers: servers.join(',') })
void components.downloadQueue.scheduleJob(async () => {
logger.info('Downloading entity', {
entityId: entity.entityId,
entityType: entity.entityType,
servers: servers.join(',')
})

await downloadEntityAndContentFiles(
{ ...components, fetcher: components.fetch },
entity.entityId,
servers,
new Map(),
"content",
'content',
10,
1000
)

logger.info("Entity stored", { entityId: entity.entityId, entityType: entity.entityType })
logger.info('Entity stored', { entityId: entity.entityId, entityType: entity.entityType })

// send sns
if (components.sns.arn) {
const deploymentToSqs: DeploymentToSqs = {
entity,
contentServerUrls: servers,
contentServerUrls: servers
}
const receipt = await sns
.publish({
TopicArn: components.sns.arn,
Message: JSON.stringify(deploymentToSqs),
Message: JSON.stringify(deploymentToSqs)
})
.promise()
logger.info("Notification sent", {
logger.info('Notification sent', {
MessageId: receipt.MessageId as any,
SequenceNumber: receipt.SequenceNumber as any,
SequenceNumber: receipt.SequenceNumber as any
})
}
await markAsDeployed()
Expand All @@ -61,6 +65,6 @@ export function createDeployerComponent(
await markAsDeployed()
}
},
async onIdle() { },
async onIdle() {}
}
}
6 changes: 3 additions & 3 deletions src/adapters/fetch/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { IFetchComponent } from "@well-known-components/http-server"
import * as nodeFetch from "node-fetch"
import { IFetchComponent } from '@well-known-components/http-server'
import * as nodeFetch from 'node-fetch'

export async function createFetchComponent() {
const fetch: IFetchComponent = {
async fetch(url: nodeFetch.RequestInfo, init?: nodeFetch.RequestInit): Promise<nodeFetch.Response> {
return nodeFetch.default(url, init)
},
}
}

return fetch
Expand Down
46 changes: 25 additions & 21 deletions src/components.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { createDotEnvConfigComponent } from "@well-known-components/env-config-provider"
import { createServerComponent, createStatusCheckComponent } from "@well-known-components/http-server"
import { createLogComponent } from "@well-known-components/logger"
import { createFetchComponent } from "./adapters/fetch"
import { createMetricsComponent, instrumentHttpServerWithMetrics } from "@well-known-components/metrics"
import { AppComponents, GlobalContext, SnsComponent } from "./types"
import { metricDeclarations } from "./metrics"
import { createJobQueue } from "@dcl/snapshots-fetcher/dist/job-queue-port"
import { createSynchronizer, } from "@dcl/snapshots-fetcher"
import { ISnapshotStorageComponent, IProcessedSnapshotStorageComponent } from "@dcl/snapshots-fetcher/dist/types"
import { createDeployerComponent } from "./adapters/deployer"
import { createAwsS3BasedFileSystemContentStorage, createFolderBasedFileSystemContentStorage, createFsComponent } from "@dcl/catalyst-storage"
import { Readable } from "stream"
import { createDotEnvConfigComponent } from '@well-known-components/env-config-provider'
import { createServerComponent, createStatusCheckComponent } from '@well-known-components/http-server'
import { createLogComponent } from '@well-known-components/logger'
import { createFetchComponent } from './adapters/fetch'
import { createMetricsComponent, instrumentHttpServerWithMetrics } from '@well-known-components/metrics'
import { AppComponents, GlobalContext, SnsComponent } from './types'
import { metricDeclarations } from './metrics'
import { createJobQueue } from '@dcl/snapshots-fetcher/dist/job-queue-port'
import { createSynchronizer } from '@dcl/snapshots-fetcher'
import { ISnapshotStorageComponent, IProcessedSnapshotStorageComponent } from '@dcl/snapshots-fetcher/dist/types'
import { createDeployerComponent } from './adapters/deployer'
import {
createAwsS3BasedFileSystemContentStorage,
createFolderBasedFileSystemContentStorage,
createFsComponent
} from '@dcl/catalyst-storage'
import { Readable } from 'stream'

// Initialize all the components of the app
export async function initComponents(): Promise<AppComponents> {
const config = await createDotEnvConfigComponent({ path: [".env.default", ".env"] })
const config = await createDotEnvConfigComponent({ path: ['.env.default', '.env'] })

const metrics = await createMetricsComponent(metricDeclarations, { config })
const logs = await createLogComponent({ metrics })
Expand All @@ -26,10 +30,10 @@ export async function initComponents(): Promise<AppComponents> {

const fs = createFsComponent()

const downloadsFolder = "content"
const downloadsFolder = 'content'

const bucket = await config.getString("BUCKET")
const snsArn = await config.getString("SNS_ARN")
const bucket = await config.getString('BUCKET')
const snsArn = await config.getString('SNS_ARN')

const storage = bucket
? await createAwsS3BasedFileSystemContentStorage({ fs, config }, bucket)
Expand All @@ -38,11 +42,11 @@ export async function initComponents(): Promise<AppComponents> {
const downloadQueue = createJobQueue({
autoStart: true,
concurrency: 5,
timeout: 100000,
timeout: 100000
})

const sns: SnsComponent = {
arn: snsArn,
arn: snsArn
}

const deployer = createDeployerComponent({ storage, downloadQueue, fetch, logs, metrics, sns })
Expand Down Expand Up @@ -70,7 +74,7 @@ export async function initComponents(): Promise<AppComponents> {
}
}
return ret
},
}
}

const synchronizer = await createSynchronizer(
Expand Down Expand Up @@ -122,6 +126,6 @@ export async function initComponents(): Promise<AppComponents> {
downloadQueue,
synchronizer,
deployer,
sns,
sns
}
}
12 changes: 6 additions & 6 deletions src/controllers/handlers/ping-handler.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { HandlerContextWithPath } from "../../types"
import { HandlerContextWithPath } from '../../types'

// handlers arguments only type what they need, to make unit testing easier
export async function pingHandler(context: Pick<HandlerContextWithPath<"metrics", "/ping">, "url" | "components">) {
export async function pingHandler(context: Pick<HandlerContextWithPath<'metrics', '/ping'>, 'url' | 'components'>) {
const {
url,
components: { metrics },
components: { metrics }
} = context

metrics.increment("test_ping_counter", {
pathname: url.pathname,
metrics.increment('test_ping_counter', {
pathname: url.pathname
})

return {
body: url.pathname,
body: url.pathname
}
}
10 changes: 5 additions & 5 deletions src/controllers/routes.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Router } from "@well-known-components/http-server"
import { GlobalContext } from "../types"
import { pingHandler } from "./handlers/ping-handler"
import { Router } from '@well-known-components/http-server'
import { GlobalContext } from '../types'
import { pingHandler } from './handlers/ping-handler'

// We return the entire router because it will be easier to test than a whole server
export async function setupRouter(globalContext: GlobalContext): Promise<Router<GlobalContext>> {
export async function setupRouter(_: GlobalContext): Promise<Router<GlobalContext>> {
const router = new Router<GlobalContext>()

router.get("/ping", pingHandler)
router.get('/ping', pingHandler)

return router
}
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Lifecycle } from "@well-known-components/interfaces"
import { initComponents } from "./components"
import { main } from "./service"
import { Lifecycle } from '@well-known-components/interfaces'
import { initComponents } from './components'
import { main } from './service'

// This file is the program entry point, it only calls the Lifecycle function
Lifecycle.run({ main, initComponents })
14 changes: 7 additions & 7 deletions src/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { metricsDefinitions } from "@dcl/snapshots-fetcher"
import { IMetricsComponent } from "@well-known-components/interfaces"
import { metricDeclarations as logMetricDeclarations } from "@well-known-components/logger"
import { validateMetricsDeclaration, getDefaultHttpMetrics } from "@well-known-components/metrics"
import { metricsDefinitions } from '@dcl/snapshots-fetcher'
import { IMetricsComponent } from '@well-known-components/interfaces'
import { metricDeclarations as logMetricDeclarations } from '@well-known-components/logger'
import { validateMetricsDeclaration, getDefaultHttpMetrics } from '@well-known-components/metrics'

export const metricDeclarations = {
...getDefaultHttpMetrics(),
...metricsDefinitions,
...logMetricDeclarations,
test_ping_counter: {
help: "Count calls to ping",
help: 'Count calls to ping',
type: IMetricsComponent.CounterType,
labelNames: ["pathname"],
},
labelNames: ['pathname']
}
}

// type assertions
Expand Down
26 changes: 14 additions & 12 deletions src/service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Lifecycle } from "@well-known-components/interfaces"
import { setupRouter } from "./controllers/routes"
import { AppComponents, GlobalContext, TestComponents } from "./types"
import { Lifecycle } from '@well-known-components/interfaces'
import { setupRouter } from './controllers/routes'
import { AppComponents, GlobalContext, TestComponents } from './types'

// this function wires the business logic (adapters & controllers) with the components (ports)
export async function main(program: Lifecycle.EntryPointParameters<AppComponents | TestComponents>) {
const { components, startComponents } = program
const globalContext: GlobalContext = {
components,
components
}

// wire the HTTP router (make it automatic? TBD)
Expand All @@ -21,12 +21,14 @@ export async function main(program: Lifecycle.EntryPointParameters<AppComponents
// start ports: db, listeners, synchronizations, etc
await startComponents()

await components.synchronizer.syncWithServers(new Set([
"https://peer.decentraland.org/content",
"https://peer-ec1.decentraland.org/content",
"https://peer-ec2.decentraland.org/content",
"https://peer-wc1.decentraland.org/content",
"https://peer-eu1.decentraland.org/content",
"https://peer-ap1.decentraland.org/content",
]))
await components.synchronizer.syncWithServers(
new Set([
'https://peer.decentraland.org/content',
'https://peer-ec1.decentraland.org/content',
'https://peer-ec2.decentraland.org/content',
'https://peer-wc1.decentraland.org/content',
'https://peer-eu1.decentraland.org/content',
'https://peer-ap1.decentraland.org/content'
])
)
}
14 changes: 7 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { IJobQueue } from "@dcl/snapshots-fetcher/dist/job-queue-port"
import { IDeployerComponent, SynchronizerComponent } from "@dcl/snapshots-fetcher/dist/types"
import type { IFetchComponent } from "@well-known-components/http-server"
import { IJobQueue } from '@dcl/snapshots-fetcher/dist/job-queue-port'
import { IDeployerComponent, SynchronizerComponent } from '@dcl/snapshots-fetcher/dist/types'
import type { IFetchComponent } from '@well-known-components/http-server'
import type {
IConfigComponent,
ILoggerComponent,
IHttpServerComponent,
IBaseComponent,
IMetricsComponent,
} from "@well-known-components/interfaces"
import { IContentStorageComponent, IFileSystemComponent } from "@dcl/catalyst-storage"
import { metricDeclarations } from "./metrics"
IMetricsComponent
} from '@well-known-components/interfaces'
import { IContentStorageComponent, IFileSystemComponent } from '@dcl/catalyst-storage'
import { metricDeclarations } from './metrics'

export type GlobalContext = {
components: BaseComponents
Expand Down
Loading

0 comments on commit 9f3b997

Please sign in to comment.