Skip to content

Commit

Permalink
Merge pull request #1811 from dherault/arccode-sponsoring
Browse files Browse the repository at this point in the history
Arccode sponsoring
  • Loading branch information
dherault authored Aug 22, 2024
2 parents 50a1d22 + 128c653 commit 8289bbe
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 101 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# Serverless Offline

⚠️
We are looking for maintainers! This package is entirely community-driven. Please send an email to dherault to start helping now.
⚠️

<p>
<a href="https://www.serverless.com">
<img src="http://public.serverless.com/badges/v3.svg">
Expand Down
160 changes: 80 additions & 80 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
]
},
"dependencies": {
"@aws-sdk/client-lambda": "^3.632.0",
"@aws-sdk/client-lambda": "^3.636.0",
"@hapi/boom": "^10.0.1",
"@hapi/h2o2": "^10.0.4",
"@hapi/hapi": "^21.3.10",
Expand All @@ -88,7 +88,7 @@
"fs-extra": "^11.2.0",
"is-wsl": "^3.1.0",
"java-invoke-local": "0.0.6",
"jose": "^5.6.3",
"jose": "^5.7.0",
"js-string-escape": "^1.0.1",
"jsonpath-plus": "^9.0.0",
"jsonschema": "^1.4.1",
Expand All @@ -115,7 +115,7 @@
"mocha": "^10.7.3",
"nyc": "^17.0.0",
"prettier": "^3.3.3",
"serverless": "^4.1.22"
"serverless": "^4.2.1"
},
"peerDependencies": {
"serverless": "^4.0.0"
Expand Down
9 changes: 7 additions & 2 deletions src/ServerlessOffline.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import process, { exit } from "node:process"
import { log, setLogUtils } from "./utils/log.js"
import logSponsor from "./utils/logSponsor.js"
import {
commandOptions,
CUSTOM_OPTION,
Expand Down Expand Up @@ -64,6 +65,12 @@ export default class ServerlessOffline {
async start() {
this.#mergeOptions()

if (this.#cliOptions.noSponsor) {
log.notice()
} else {
logSponsor()
}

const {
albEvents,
httpEvents,
Expand Down Expand Up @@ -278,13 +285,11 @@ export default class ServerlessOffline {
origin: this.#options.corsAllowOrigin,
}

log.notice()
log.notice(
`Starting Offline at stage ${
this.#options.stage || provider.stage
} ${gray(`(${this.#options.region || provider.region})`)}`,
)
log.notice()
log.debug("options:", this.#options)
}

Expand Down
4 changes: 4 additions & 0 deletions src/config/commandOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ export default {
type: "boolean",
usage: "Don't prepend http routes with the stage.",
},
noSponsor: {
type: "boolean",
usage: "Remove sponsor message from the output.",
},
noTimeout: {
shortcut: "t",
type: "boolean",
Expand Down
8 changes: 3 additions & 5 deletions src/lambda/HttpServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default class HttpServer {
.listFunctionNames()
.map(
(functionName) =>
` * ${funcNamePairs[functionName]}: ${functionName}`,
` * ${funcNamePairs[functionName]}: ${functionName}`,
),
].join("\n"),
)
Expand All @@ -73,9 +73,7 @@ export default class HttpServer {
.listFunctionNames()
.map(
(functionName) =>
` * ${
invRoute.method
} ${basePath}${invRoute.path.replace(
` * ${invRoute.method} ${basePath}${invRoute.path.replace(
"{functionName}",
functionName,
)}`,
Expand All @@ -90,7 +88,7 @@ export default class HttpServer {
.listFunctionNames()
.map(
(functionName) =>
` * ${
` * ${
invAsyncRoute.method
} ${basePath}${invAsyncRoute.path.replace(
"{functionName}",
Expand Down
13 changes: 6 additions & 7 deletions src/utils/logRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import {
yellow,
} from "../config/colors.js"

const { max } = Math

const post = "POST"
const colorMethodMapping = new Map([
["DELETE", red],
["GET", dodgerblue],
Expand All @@ -28,13 +27,13 @@ function logRoute(method, server, path, maxLength, dimPath = false) {
const methodColor = colorMethodMapping.get(method) ?? peachpuff
const methodFormatted = method.padEnd(maxLength, " ")

return `${methodColor(methodFormatted)} ${yellow.dim("|")} ${gray.dim(
server,
)}${dimPath ? gray.dim(path) : lime(path)}`
return `${methodColor(methodFormatted)} ${yellow.dim("|")} ${gray.dim(server)}${dimPath ? gray.dim(path) : lime(path)}`
}

function getMaxHttpMethodNameLength(routeInfo) {
return max(...routeInfo.map(({ method }) => method.length))
return Math.max(
...routeInfo.map(({ method }) => Math.max(method.length, post.length)),
)
}

export default function logRoutes(routeInfo) {
Expand All @@ -55,7 +54,7 @@ export default function logRoutes(routeInfo) {
// eslint-disable-next-line prefer-template
logRoute(method, server, path, maxLength) +
"\n" +
logRoute("POST", server, invokePath, maxLength, true),
logRoute(post, server, invokePath, maxLength, true),
)
.join("\n"),
boxenOptions,
Expand Down
69 changes: 69 additions & 0 deletions src/utils/logSponsor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* eslint-disable no-use-before-define */
/* eslint-disable no-console */
import process from "node:process"

import boxen from "boxen"
import { gray, dodgerblue } from "../config/colors.js"

const boxenOptions = {
borderColor: "blue",
margin: 1,
padding: 1,
}

// Promotion starts on August 22, 2024
const startAt = new Date("2024-08-22T00:00:00.000Z")
// By October 22, 2024, the promotion will be displayed to 100% of users
const endAt = new Date("2024-10-22T00:00:00.000Z")
const nDays = diffDays(startAt, endAt)

function logSponsor() {
if (!shouldDisplaySponsor()) {
console.log()

return
}

console.log(
boxen(
`Sponsored by ${dodgerblue("Arccode, the RPG for developers")}\nhttps://arccode.dev?ref=so\n${gray.dim(
"Disable with --noSponsor",
)}`,
boxenOptions,
),
)
}

// Display the message progressively over time to 100% of users
function shouldDisplaySponsor() {
const ratio = diffDays(startAt, new Date()) / nDays

if (ratio >= 1) return true

try {
const nonce = Number(
encodeStringToNumber(process.cwd()).toString().padStart(2, "0").slice(-2),
)

return nonce <= ratio * 100
} catch {
//
}

return false
}

function encodeStringToNumber(string) {
let sum = 0

for (let i = 0; i < string.length; i += 1) {
sum += Number(string.codePointAt(i).toString(10))
}

return sum
}

function diffDays(a, b) {
return Math.round((b - a) / (1000 * 60 * 60 * 24))
}
export default logSponsor

0 comments on commit 8289bbe

Please sign in to comment.