Skip to content

Commit

Permalink
refactor: Add "common" library for shared code (#12)
Browse files Browse the repository at this point in the history
The `UpdateResponse` interface was previously duplicated between worker
and client. In future patches, additional pieces of similar code can be placed
into common, such as environment variable parsing.
  • Loading branch information
meyfa authored Dec 1, 2024
1 parent 45a04ed commit 46be1da
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 13 deletions.
4 changes: 4 additions & 0 deletions client/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ FROM node:20.18.1-alpine AS build
WORKDIR /app

COPY ./package*.json ./
COPY ./common/package*.json ./common/
COPY ./client/package*.json ./client/
RUN npm ci

COPY ./tsconfig.json ./
COPY ./common ./common
COPY ./client ./client
RUN npm run build

Expand All @@ -17,9 +19,11 @@ WORKDIR /app
RUN apk add --no-cache tini

COPY ./package*.json ./
COPY ./common/package*.json ./common/
COPY ./client/package*.json ./client/
RUN npm ci --omit=dev

COPY --from=build /app/common/dist ./common/dist
COPY --from=build /app/client/dist ./client/dist

USER node
Expand Down
7 changes: 2 additions & 5 deletions client/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import type { UpdateResponse } from '@meyfa/ddns-common'

export interface UpdateOptions {
url: URL
secret: string
signal?: AbortSignal
}

export interface UpdateResponse {
ip: string
modified: boolean
}

export async function update(options: UpdateOptions): Promise<UpdateResponse> {
const res = await fetch(options.url, {
method: 'PUT',
Expand Down
11 changes: 11 additions & 0 deletions common/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"private": true,
"name": "@meyfa/ddns-common",
"type": "module",
"main": "dist/main.js",
"types": "dist/main.d.ts",
"scripts": {
"build": "tsc",
"lint": "tsc --noEmit"
}
}
4 changes: 4 additions & 0 deletions common/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface UpdateResponse {
ip: string
modified: boolean
}
12 changes: 12 additions & 0 deletions common/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"sourceMap": true,
"declaration": true,
"noEmit": false
},
"include": [
"src"
]
}
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"private": true,
"name": "@meyfa/ddns",
"scripts": {
"lint": "npm run lint --workspaces",
"build": "npm run build --workspace=client",
"deploy": "npm run deploy --workspace=worker"
"lint": "npm run build --workspace=common && npm run lint --workspaces",
"build": "npm run build --workspace=common && npm run build --workspace=client",
"deploy": "npm run build --workspace=common && npm run deploy --workspace=worker"
},
"workspaces": [
"common",
"client",
"worker"
],
Expand Down
7 changes: 2 additions & 5 deletions worker/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import type { UpdateResponse } from '@meyfa/ddns-common'

interface Env {
DDNS_SECRET?: string
CLOUDFLARE_API_TOKEN?: string
CLOUDFLARE_ZONE_ID?: string
CLOUDFLARE_RECORD_NAME?: string
}

interface UpdateResponse {
ip: string
modified: boolean
}

const API = new URL('https://api.cloudflare.com/client/v4/')
const RATE_LIMIT_MS = 30_000

Expand Down

0 comments on commit 46be1da

Please sign in to comment.