@srfnstack/spliffy 0.9.2 → 0.9.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@srfnstack/spliffy",
3
- "version": "0.9.2",
3
+ "version": "0.9.5",
4
4
  "author": "snowbldr",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/narcolepticsnowman/spliffy",
package/src/errors.mjs ADDED
@@ -0,0 +1,254 @@
1
+ export class HttpError extends Error {
2
+ constructor (statusCode, statusMessage, body) {
3
+ super()
4
+ this.statusCode = statusCode
5
+ this.statusMessage = statusMessage
6
+ this.body = body
7
+ }
8
+ }
9
+
10
+ export class BadRequestError extends HttpError {
11
+ constructor (body) {
12
+ super(400, 'Bad Request', body)
13
+ }
14
+ }
15
+
16
+ export class UnauthorizedError extends HttpError {
17
+ constructor (body) {
18
+ super(401, 'Unauthorized', body)
19
+ }
20
+ }
21
+
22
+ export class PaymentRequiredError extends HttpError {
23
+ constructor (body) {
24
+ super(402, 'Payment Required', body)
25
+ }
26
+ }
27
+
28
+ export class ForbiddenError extends HttpError {
29
+ constructor (body) {
30
+ super(403, 'Forbidden', body)
31
+ }
32
+ }
33
+
34
+ export class NotFoundError extends HttpError {
35
+ constructor (body) {
36
+ super(404, 'Not Found', body)
37
+ }
38
+ }
39
+
40
+ export class MethodNotAllowedError extends HttpError {
41
+ constructor (body) {
42
+ super(405, 'Method Not Allowed', body)
43
+ }
44
+ }
45
+
46
+ export class NotAcceptableError extends HttpError {
47
+ constructor (body) {
48
+ super(406, 'Not Acceptable', body)
49
+ }
50
+ }
51
+
52
+ export class ProxyAuthenticationRequiredError extends HttpError {
53
+ constructor (body) {
54
+ super(407, 'Proxy Authentication Required', body)
55
+ }
56
+ }
57
+
58
+ export class RequestTimeoutError extends HttpError {
59
+ constructor (body) {
60
+ super(408, 'Request Timeout', body)
61
+ }
62
+ }
63
+
64
+ export class ConflictError extends HttpError {
65
+ constructor (body) {
66
+ super(409, 'Conflict', body)
67
+ }
68
+ }
69
+
70
+ export class GoneError extends HttpError {
71
+ constructor (body) {
72
+ super(410, 'Gone', body)
73
+ }
74
+ }
75
+
76
+ export class LengthRequiredError extends HttpError {
77
+ constructor (body) {
78
+ super(411, 'Length Required', body)
79
+ }
80
+ }
81
+
82
+ export class PreconditionFailedError extends HttpError {
83
+ constructor (body) {
84
+ super(412, 'Precondition Failed', body)
85
+ }
86
+ }
87
+
88
+ export class PayloadTooLargeError extends HttpError {
89
+ constructor (body) {
90
+ super(413, 'Payload Too Large', body)
91
+ }
92
+ }
93
+
94
+ export class URITooLongError extends HttpError {
95
+ constructor (body) {
96
+ super(414, 'URI Too Long', body)
97
+ }
98
+ }
99
+
100
+ export class UnsupportedMediaTypeError extends HttpError {
101
+ constructor (body) {
102
+ super(415, 'Unsupported Media Type', body)
103
+ }
104
+ }
105
+
106
+ export class RangeNotSatisfiableError extends HttpError {
107
+ constructor (body) {
108
+ super(416, 'Bad Request', body)
109
+ }
110
+ }
111
+
112
+ export class ExpectationFailedError extends HttpError {
113
+ constructor (body) {
114
+ super(417, 'Expectation Failed', body)
115
+ }
116
+ }
117
+
118
+ export class ImATeapotError extends HttpError {
119
+ constructor (body) {
120
+ super(418, 'I can\'t brew coffee', body)
121
+ }
122
+ }
123
+
124
+ export class EnhanceYourCalmError extends HttpError {
125
+ constructor (body) {
126
+ super(420, 'Enhance Your Calm', body)
127
+ }
128
+ }
129
+
130
+ export class MisdirectedRequestError extends HttpError {
131
+ constructor (body) {
132
+ super(421, 'Misdirected Request', body)
133
+ }
134
+ }
135
+
136
+ export class UnprocessableEntityError extends HttpError {
137
+ constructor (body) {
138
+ super(422, 'Unprocessable Entity', body)
139
+ }
140
+ }
141
+
142
+ export class LockedError extends HttpError {
143
+ constructor (body) {
144
+ super(423, 'Locked', body)
145
+ }
146
+ }
147
+
148
+ export class FailedDependencyError extends HttpError {
149
+ constructor (body) {
150
+ super(424, 'Failed Dependency', body)
151
+ }
152
+ }
153
+
154
+ export class TooEarlyError extends HttpError {
155
+ constructor (body) {
156
+ super(425, 'Too Early', body)
157
+ }
158
+ }
159
+
160
+ export class UpgradeRequiredError extends HttpError {
161
+ constructor (body) {
162
+ super(426, 'Upgrade Required', body)
163
+ }
164
+ }
165
+
166
+ export class PreconditionRequiredError extends HttpError {
167
+ constructor (body) {
168
+ super(428, 'Precondition Required', body)
169
+ }
170
+ }
171
+
172
+ export class TooManyRequestsError extends HttpError {
173
+ constructor (body) {
174
+ super(429, 'Too Many Requests', body)
175
+ }
176
+ }
177
+
178
+ export class RequestHeaderFieldsTooLargeError extends HttpError {
179
+ constructor (body) {
180
+ super(431, 'Request Header Fields Too Large', body)
181
+ }
182
+ }
183
+
184
+ export class UnavailableForLegalReasonsError extends HttpError {
185
+ constructor (body) {
186
+ super(451, 'Unavailable For Legal Reasons', body)
187
+ }
188
+ }
189
+
190
+ export class InternalServerError extends HttpError {
191
+ constructor (body) {
192
+ super(500, 'Internal Server Error', body)
193
+ }
194
+ }
195
+
196
+ export class NotImplementedError extends HttpError {
197
+ constructor (body) {
198
+ super(501, 'Not Implemented', body)
199
+ }
200
+ }
201
+
202
+ export class BadGatewayError extends HttpError {
203
+ constructor (body) {
204
+ super(502, 'Bad Gateway', body)
205
+ }
206
+ }
207
+
208
+ export class ServiceUnavailableError extends HttpError {
209
+ constructor (body) {
210
+ super(503, 'Service Unavailable', body)
211
+ }
212
+ }
213
+
214
+ export class GatewayTimeoutError extends HttpError {
215
+ constructor (body) {
216
+ super(504, 'Gateway Timeout', body)
217
+ }
218
+ }
219
+
220
+ export class HTTPVersionNotSupportedError extends HttpError {
221
+ constructor (body) {
222
+ super(505, 'HTTP Version Not Supported', body)
223
+ }
224
+ }
225
+
226
+ export class VariantAlsoNegotiatesError extends HttpError {
227
+ constructor (body) {
228
+ super(506, 'Variant Also Negotiates', body)
229
+ }
230
+ }
231
+
232
+ export class InsufficientStorageError extends HttpError {
233
+ constructor (body) {
234
+ super(507, 'Insufficient Storage', body)
235
+ }
236
+ }
237
+
238
+ export class LoopDetectedError extends HttpError {
239
+ constructor (body) {
240
+ super(508, 'Loop Detected', body)
241
+ }
242
+ }
243
+
244
+ export class NotExtendedError extends HttpError {
245
+ constructor (body) {
246
+ super(510, 'Not Extended', body)
247
+ }
248
+ }
249
+
250
+ export class NetworkAuthenticationRequiredError extends HttpError {
251
+ constructor (body) {
252
+ super(511, 'Network Authentication Required', body)
253
+ }
254
+ }
package/src/handler.mjs CHANGED
@@ -34,7 +34,6 @@ const executeHandler = async (url, res, req, bodyPromise, handler, middleware, e
34
34
  finalizeResponse(req, res, handled, handler.statusCodeOverride)
35
35
  } catch (e) {
36
36
  const refId = uuid()
37
- log.error('handler failed', e, refId)
38
37
  await executeMiddleware(middleware, req, res, errorTransformer, refId, e)
39
38
  endError(res, e, refId, errorTransformer)
40
39
  }
@@ -174,7 +173,6 @@ const handleRequest = async (req, res, handler, middleware, errorTransformer) =>
174
173
  }
175
174
  } catch (e) {
176
175
  const refId = uuid()
177
- log.error('Handling request failed', e, refId)
178
176
  await executeMiddleware(middleware, req, res, errorTransformer, refId, e)
179
177
  if (!res.writableEnded) { endError(res, e, refId, errorTransformer) }
180
178
  }
@@ -7,10 +7,18 @@ import { getContentTypeByExtension } from './content.mjs'
7
7
  const stripLeadingSlash = p => p.startsWith('/') ? p.substr(1) : p
8
8
 
9
9
  /**
10
- This method will add all of the configured node_module files to the given routes.
10
+ This helper will add all the configured node_module files to the given routes.
11
11
  The configured node moduleRoutes must be explicit files, no pattern matching is supported.
12
12
  Generating the list of files using pattern matching yourself is highly discouraged.
13
13
  It is much safer to explicitly list every file you wish to be served so you don't inadvertently serve additional files.
14
+
15
+ The default method is to read and serve directly from the node_modules directory without copying.
16
+ if method is set to "copy", files are copied from node_modules to their final location within the routes dir folder.
17
+
18
+ The primary benefit of using this in copy mode is that the files will be automatically updated when the package version
19
+ is updated, and it improves IDE integration by making the file really available after first run.
20
+
21
+ This could be destructive if not configured correctly, hence the default of read only
14
22
  */
15
23
  export function getNodeModuleRoutes (config) {
16
24
  const nodeModuleRoutes = config.nodeModuleRoutes
@@ -20,7 +28,7 @@ export function getNodeModuleRoutes (config) {
20
28
  if (!fs.existsSync(nodeModulesDir)) {
21
29
  throw new Error(`Unable to find node_modules dir at ${nodeModulesDir}`)
22
30
  }
23
- const prefix = stripLeadingSlash(nodeModuleRoutes.routePrefix || 'lib')
31
+ const prefix = stripLeadingSlash(nodeModuleRoutes.routePrefix || 'lib/ext')
24
32
  if (!Array.isArray(nodeModuleRoutes.files)) {
25
33
  nodeModuleRoutes.files = [nodeModuleRoutes.files]
26
34
  }
@@ -37,21 +45,27 @@ export function getNodeModuleRoutes (config) {
37
45
  }
38
46
 
39
47
  if (fs.existsSync(filePath)) {
40
- const parts = urlPath.split('/')
41
- const lastPart = parts.pop()
42
- const mw = {}
43
- mergeMiddleware(config.middleware, mw)
44
- mergeMiddleware(nodeModuleRoutes.middleware || {}, mw)
45
- routes.push({
46
- pathParameters: [],
47
- urlPath,
48
- filePath,
49
- handlers: createStaticHandler(
50
- filePath, getContentTypeByExtension(lastPart, config.staticContentTypes),
51
- config.cacheStatic, config.staticCacheControl
52
- ),
53
- middleware: mw
54
- })
48
+ if (nodeModuleRoutes.method === 'copy') {
49
+ const dest = path.join(config.routeDir, urlPath)
50
+ fs.mkdirSync(path.dirname(dest), { recursive: true })
51
+ fs.copyFileSync(filePath, dest)
52
+ } else {
53
+ const parts = urlPath.split('/')
54
+ const lastPart = parts.pop()
55
+ const mw = {}
56
+ mergeMiddleware(config.middleware, mw)
57
+ mergeMiddleware(nodeModuleRoutes.middleware || {}, mw)
58
+ routes.push({
59
+ pathParameters: [],
60
+ urlPath,
61
+ filePath,
62
+ handlers: createStaticHandler(
63
+ filePath, getContentTypeByExtension(lastPart, config.staticContentTypes),
64
+ config.cacheStatic, config.staticCacheControl
65
+ ),
66
+ middleware: mw
67
+ })
68
+ }
55
69
  } else {
56
70
  console.warn(`The specified node_modules file: ${file} does not exist and will not be served.`)
57
71
  }
package/src/server.mjs CHANGED
@@ -68,7 +68,7 @@ const getHttpsApp = (key, cert) => {
68
68
  export async function startServer (config) {
69
69
  if (!state.initialized) {
70
70
  state.initialized = true
71
- const routes = [...(await findRoutes(config)), ...getNodeModuleRoutes(config)]
71
+ const routes = [...getNodeModuleRoutes(config), ...(await findRoutes(config))]
72
72
  let app, port
73
73
  if (config.httpsKeyFile) {
74
74
  app = getHttpsApp(config.secure)