@jsenv/server 12.3.2 → 12.4.2

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": "@jsenv/server",
3
- "version": "12.3.2",
3
+ "version": "12.4.2",
4
4
  "description": "Write your Node.js server using pure functions",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -16,7 +16,8 @@
16
16
  "node": ">=16.13.0"
17
17
  },
18
18
  "publishConfig": {
19
- "access": "public"
19
+ "access": "public",
20
+ "registry": "https://registry.npmjs.org"
20
21
  },
21
22
  "type": "module",
22
23
  "exports": {
@@ -44,27 +45,27 @@
44
45
  "dependencies": {
45
46
  "@jsenv/abort": "4.1.2",
46
47
  "@jsenv/logger": "4.0.1",
47
- "@jsenv/url-meta": "6.0.3",
48
- "node-fetch": "2.6.2",
48
+ "@jsenv/url-meta": "6.2.0",
49
+ "node-fetch": "2.6.7",
49
50
  "path-to-regexp": "6.2.0"
50
51
  },
51
52
  "devDependencies": {
52
- "@jsenv/assert": "2.4.1",
53
- "@jsenv/core": "25.1.0",
53
+ "@jsenv/assert": "2.5.2",
54
+ "@jsenv/core": "25.4.9",
54
55
  "@jsenv/eslint-config": "16.0.9",
55
- "@jsenv/filesystem": "2.6.1",
56
- "@jsenv/github-release-package": "1.2.3",
57
- "@jsenv/https-local": "1.0.3",
58
- "@jsenv/importmap-eslint-resolver": "5.2.2",
59
- "@jsenv/importmap-node-module": "5.1.0",
60
- "@jsenv/package-publish": "1.6.2",
61
- "@jsenv/performance-impact": "2.2.1",
56
+ "@jsenv/filesystem": "2.7.1",
57
+ "@jsenv/github-release-package": "1.3.4",
58
+ "@jsenv/https-local": "1.0.7",
59
+ "@jsenv/importmap-eslint-resolver": "5.2.5",
60
+ "@jsenv/importmap-node-module": "5.1.3",
61
+ "@jsenv/package-publish": "1.7.2",
62
+ "@jsenv/performance-impact": "2.2.7",
62
63
  "eslint": "8.7.0",
63
64
  "eslint-plugin-import": "2.25.4",
64
65
  "eventsource": "1.1.0",
65
- "fastify": "3.25.3",
66
+ "fastify": "3.27.0",
66
67
  "fastify-static": "4.5.0",
67
- "playwright": "1.17.2",
68
+ "playwright": "1.18.1",
68
69
  "prettier": "2.5.1"
69
70
  }
70
- }
71
+ }
@@ -1,12 +1,17 @@
1
1
  // https://github.com/jamestalmage/stream-to-observable/blob/master/index.js
2
2
  import { Readable } from "node:stream"
3
-
4
3
  import { createObservable } from "./observable.js"
5
4
 
6
5
  const readableStreamLifetimeInSeconds = 120
7
6
 
8
7
  export const nodeStreamToObservable = (nodeStream) => {
9
8
  const observable = createObservable(({ next, error, complete }) => {
9
+ if (nodeStream.isPaused()) {
10
+ nodeStream.resume()
11
+ } else if (nodeStream.complete) {
12
+ complete()
13
+ return null
14
+ }
10
15
  const cleanup = () => {
11
16
  nodeStream.removeListener("data", next)
12
17
  nodeStream.removeListener("error", error)
@@ -14,13 +19,17 @@ export const nodeStreamToObservable = (nodeStream) => {
14
19
  nodeStream.removeListener("close", cleanup)
15
20
  nodeStream.destroy()
16
21
  }
17
-
18
22
  // should we do nodeStream.resume() in case the stream was paused ?
19
23
  nodeStream.once("error", error)
20
- nodeStream.on("data", next)
21
- nodeStream.once("close", cleanup)
22
- nodeStream.once("end", complete)
23
-
24
+ nodeStream.on("data", (data) => {
25
+ next(data)
26
+ })
27
+ nodeStream.once("close", () => {
28
+ cleanup()
29
+ })
30
+ nodeStream.once("end", () => {
31
+ complete()
32
+ })
24
33
  return cleanup
25
34
  })
26
35
 
@@ -1,38 +1,28 @@
1
- import { urlToMeta, normalizeStructuredMetaMap } from "@jsenv/url-meta"
1
+ import { applyAliases } from "@jsenv/url-meta"
2
2
 
3
3
  export const pluginRessourceAliases = (ressourceAliases) => {
4
- const ressourceAliasMap = {}
4
+ const aliases = {}
5
5
  Object.keys(ressourceAliases).forEach((key) => {
6
- ressourceAliasMap[ressourceAliases[key]] = key
6
+ aliases[asFileUrl(key)] = asFileUrl(ressourceAliases[key])
7
7
  })
8
- const structuredMetaMap = normalizeStructuredMetaMap(
9
- { alias: ressourceAliasMap },
10
- "file:///",
11
- )
12
-
13
- const getRessourceAlias = (pathname) => {
14
- const { alias } = urlToMeta({
15
- url: `file://${pathname}`,
16
- structuredMetaMap,
17
- })
18
- return alias
19
- }
20
-
21
8
  return {
22
9
  ressource_aliases: {
23
10
  onRequest: (request, { redirectRequest }) => {
24
- const ressourceAlias = getRessourceAlias(request.pathname)
25
- if (!ressourceAlias) {
11
+ const ressourceBeforeAlias = request.ressource
12
+ const urlAfterAliasing = applyAliases({
13
+ url: asFileUrl(request.pathname),
14
+ aliases,
15
+ })
16
+ const ressourceAfterAlias = urlAfterAliasing.slice("file://".length)
17
+ if (ressourceBeforeAlias === ressourceAfterAlias) {
26
18
  return
27
19
  }
28
-
29
- const urlObjectAfterAlias = replaceUrlRessource(
30
- request.url,
31
- ressourceAlias,
20
+ const ressource = replaceRessource(
21
+ ressourceBeforeAlias,
22
+ ressourceAfterAlias,
32
23
  )
33
- const ressource = `${urlObjectAfterAlias.pathname}${urlObjectAfterAlias.search}`
34
24
  redirectRequest({
35
- ressourceBeforeAlias: request.ressource,
25
+ ressourceBeforeAlias,
36
26
  ressource,
37
27
  })
38
28
  },
@@ -40,16 +30,15 @@ export const pluginRessourceAliases = (ressourceAliases) => {
40
30
  }
41
31
  }
42
32
 
43
- const replaceUrlRessource = (url, ressource) => {
44
- const urlObject = new URL(url)
33
+ const asFileUrl = (specifier) => new URL(specifier, "file:///").href
45
34
 
46
- const searchSeparatorIndex = ressource.indexOf("?")
47
- if (searchSeparatorIndex === -1) {
48
- urlObject.pathname = ressource
49
- return urlObject
35
+ const replaceRessource = (ressourceBeforeAlias, newValue) => {
36
+ const urlObject = new URL(ressourceBeforeAlias, "file://")
37
+ const searchSeparatorIndex = newValue.indexOf("?")
38
+ if (searchSeparatorIndex > -1) {
39
+ return newValue // let new value override search params
50
40
  }
51
-
52
- const pathname = ressource.slice(0, searchSeparatorIndex)
53
- urlObject.pathname = pathname
54
- return urlObject
41
+ urlObject.pathname = newValue
42
+ const ressource = `${urlObject.pathname}${urlObject.search}`
43
+ return ressource
55
44
  }
@@ -230,6 +230,10 @@ export const startServer = async ({
230
230
  })
231
231
 
232
232
  const requestCallback = async (nodeRequest, nodeResponse) => {
233
+ // pause the stream to let a chance to "requestToResponse"
234
+ // to call "requestRequestBody". Without this the request body readable stream
235
+ // might be closed when we'll try to attach "data" and "end" listeners to it
236
+ nodeRequest.pause()
233
237
  if (!nagle) {
234
238
  nodeRequest.connection.setNoDelay(true)
235
239
  }
@@ -309,7 +313,6 @@ export const startServer = async ({
309
313
 
310
314
  visitRequestNodeToLog(rootRequestNode, 0)
311
315
  }
312
-
313
316
  nodeRequest.on("error", (error) => {
314
317
  if (error.message === "aborted") {
315
318
  addRequestLog(rootRequestNode, {
@@ -683,6 +686,7 @@ export const startServer = async ({
683
686
  const responseProperties = await handleRequest(request, {
684
687
  requestNode: rootRequestNode,
685
688
  })
689
+ nodeRequest.resume()
686
690
  if (receiveRequestOperation.signal.aborted) {
687
691
  return
688
692
  }