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

fix: Handle OPTIONS (#8) #10

Merged
merged 1 commit into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM node:16 as base
WORKDIR /usr/src/spring83
COPY package*.json ./
RUN npm install
COPY common.js .
COPY common/*.js common/
COPY *.tmpl.html .
COPY serve .
COPY public-boards.json .
Expand Down
43 changes: 43 additions & 0 deletions common/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const headerNames = {
difficulty: 'spring-difficulty',
signature: 'spring-signature',
version: 'spring-version'
};

module.exports = {
constants: Object.freeze({
maximumContentLength: 2217,
maximumNumberOfBoards: 10_000_000,
protocolVersion: '83',
contentType: 'text/html;charset=utf-8',
keyMatchRegex: /83e(0[1-9]|1[0-2])(\d\d)$/,
unmodifiedSinceTimeFudgeMs: 6000,
boardTTLDays: 22,
getKeySecurityPolicies: {
none: ['default-src', 'script-src', 'script-src-attr', 'script-src-elem',
'child-src', 'frame-src', 'prefetch-src', ' object-src'],
self: ['font-src'],
'unsafe-inline': ['style-src'],
'data:': ['img-src']
},
headerNames,
rootTemplateName: 'root.tmpl.html',
notFoundTmplName: '404.tmpl.html',
testKeyTmplName: 'testkey.tmpl.html',
defaultContentPath: '.content',
defaultFQDN: 'example.com',
strictVerification: true,
difficultyFactorExp: 4,
ttlCheckFreqMinutes: 11,
keypairFilenamePrefix: 'spring-83-keypair',
pubBoardsJsonFileName: 'public-boards.json',
pubBoardRefreshFreqMinutes: 3,
testPublicKey: 'ab589f4dde9fce4180fcf42c7b05185b0a02a5d682e353fa39177995083e0583',
corsOptions: {
origin: '*',
methods: ['GET', 'PUT'],
allowedHeaders: ['Content-Type', 'If-Modified-Since', 'Spring-Signature', 'Spring-Version'],
exposedHeaders: ['Content-Type', 'Last-Modified', ...Object.values(headerNames)]
}
})
};
6 changes: 6 additions & 0 deletions common/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = {
...require('./constants'),
...require('./utility')
};
36 changes: 1 addition & 35 deletions common.js → common/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,7 @@

const fs = require('fs');
const path = require('path');

const constants = Object.freeze({
maximumContentLength: 2217,
maximumNumberOfBoards: 10_000_000,
protocolVersion: '83',
contentType: 'text/html;charset=utf-8',
keyMatchRegex: /83e(0[1-9]|1[0-2])(\d\d)$/,
unmodifiedSinceTimeFudgeMs: 6000,
boardTTLDays: 22,
getKeySecurityPolicies: {
none: ['default-src', 'script-src', 'script-src-attr', 'script-src-elem',
'child-src', 'frame-src', 'prefetch-src', ' object-src'],
self: ['font-src'],
'unsafe-inline': ['style-src'],
'data:': ['img-src']
},
headerNames: {
difficulty: 'spring-difficulty',
signature: 'spring-signature',
version: 'spring-version'
},
rootTemplateName: 'root.tmpl.html',
notFoundTmplName: '404.tmpl.html',
testKeyTmplName: 'testkey.tmpl.html',
defaultContentPath: '.content',
defaultFQDN: 'example.com',
strictVerification: true,
difficultyFactorExp: 4,
ttlCheckFreqMinutes: 11,
keypairFilenamePrefix: 'spring-83-keypair',
pubBoardsJsonFileName: 'public-boards.json',
pubBoardRefreshFreqMinutes: 3,
testPublicKey: 'ab589f4dde9fce4180fcf42c7b05185b0a02a5d682e353fa39177995083e0583'
});
const { constants } = require('./constants');

function keyPairFilename (publicKey, root = __dirname) {
if (typeof publicKey !== 'string') {
Expand Down Expand Up @@ -136,7 +103,6 @@ function keyIsUnderDifficultyThreshold (pubKey, knownKeys) {
}

module.exports = {
constants,
pubKeyIsValid: (pubKeyData, strict = false) => pubKeyHexIsValid(Buffer.from(pubKeyData).toString('hex'), strict),
pubKeyHexIsValid,
getCurrentDifficultyFactor,
Expand Down
46 changes: 44 additions & 2 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
"description": "An implementation of the Spring 83 protocol 🌸",
"main": "serve",
"scripts": {
"lint": "semistandard --fix --verbose findkey putnew serve *.js test/*.js",
"lint": "semistandard --fix --verbose findkey putnew serve common/*.js test/*.js",
"test": "tape test/*.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"@fastify/cors": "^8.0.0",
"@noble/ed25519": "^1.6.0",
"cheerio": "^1.0.0-rc.11",
"fastify": "^4.0.1",
Expand Down
4 changes: 3 additions & 1 deletion serve
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const path = require('path');
const cheerio = require('cheerio');
const ed = require('@noble/ed25519');
const mustache = require('mustache');
const app = require('fastify')({ logger: true });
const {
constants,
findKnownKeys,
Expand All @@ -17,6 +16,9 @@ const {
boardExistsLocally
} = require('./common');

const app = require('fastify')({ logger: true });
app.register(require('@fastify/cors'), constants.corsOptions);

const expectPutHeaders = Object.freeze({
'content-type': constants.contentType,
[constants.headerNames.version]: constants.protocolVersion,
Expand Down