@push-rpc/next 2.0.1 → 2.0.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/README.md +5 -1
- package/dist/server/http.js +5 -3
- package/dist/server/http.js.map +1 -1
- package/dist/utils/server.d.ts +5 -0
- package/dist/utils/server.js +48 -0
- package/dist/utils/server.js.map +1 -0
- package/package.json +1 -1
- package/src/server/http.ts +6 -3
- package/src/utils/server.ts +29 -0
package/README.md
CHANGED
|
@@ -28,4 +28,8 @@ triggers.
|
|
|
28
28
|
## Issues / TBDs
|
|
29
29
|
|
|
30
30
|
- [important] Importing index.js from the root of the package will import node's http package. Not good for clients.
|
|
31
|
-
- Browser sockets don't have 'ping' event. Need to find a different way to detect connection loss.
|
|
31
|
+
- Browser sockets don't have 'ping' event. Need to find a different way to detect connection loss.
|
|
32
|
+
|
|
33
|
+
## Features
|
|
34
|
+
|
|
35
|
+
- Supports compressed HTTP requests.
|
package/dist/server/http.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.serveHttpRequest = void 0;
|
|
4
4
|
const json_js_1 = require("../utils/json.js");
|
|
5
5
|
const logger_js_1 = require("../logger.js");
|
|
6
|
+
const server_1 = require("../utils/server");
|
|
6
7
|
async function serveHttpRequest(req, res, path, hooks, createConnectionContext) {
|
|
7
8
|
// if port is in options - response 404 on other URLs
|
|
8
9
|
// oherwise just handle request
|
|
@@ -71,15 +72,16 @@ async function serveHttpRequest(req, res, path, hooks, createConnectionContext)
|
|
|
71
72
|
}
|
|
72
73
|
exports.serveHttpRequest = serveHttpRequest;
|
|
73
74
|
function readBody(req) {
|
|
75
|
+
const decompressed = (0, server_1.decompressRequest)(req);
|
|
74
76
|
return new Promise((resolve, reject) => {
|
|
75
77
|
let body = "";
|
|
76
|
-
|
|
78
|
+
decompressed.on("data", (chunk) => {
|
|
77
79
|
body += chunk.toString();
|
|
78
80
|
});
|
|
79
|
-
|
|
81
|
+
decompressed.on("end", () => {
|
|
80
82
|
resolve(body);
|
|
81
83
|
});
|
|
82
|
-
|
|
84
|
+
decompressed.on("error", (error) => {
|
|
83
85
|
reject(error);
|
|
84
86
|
});
|
|
85
87
|
});
|
package/dist/server/http.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http.js","sourceRoot":"","sources":["../../src/server/http.ts"],"names":[],"mappings":";;;AAGA,8CAA6D;AAC7D,4CAAgC;
|
|
1
|
+
{"version":3,"file":"http.js","sourceRoot":"","sources":["../../src/server/http.ts"],"names":[],"mappings":";;;AAGA,8CAA6D;AAC7D,4CAAgC;AAChC,4CAAiD;AAE1C,KAAK,UAAU,gBAAgB,CACpC,GAAoB,EACpB,GAAmB,EACnB,IAAY,EACZ,KAAsB,EACtB,uBAAgF;IAEhF,qDAAqD;IACrD,+BAA+B;IAE/B,IAAI,GAAG,CAAC,GAAG,EAAE,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;QAC9B,IAAI,CAAC;YACH,MAAM,GAAG,GAAG,MAAM,uBAAuB,CAAC,GAAG,CAAC,CAAA;YAE9C,MAAM,QAAQ,GAAG,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;YAE/C,MAAM,MAAM,GAAG,GAAG,CAAC,OAAO,CAAC,cAAc,CAAC,EAAE,QAAQ,CAAC,kBAAkB,CAAC,IAAI,KAAK,CAAA;YACjF,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,GAAG,CAAC,CAAA;YACpC,IAAI,IAAI,GAAG,MAAM,IAAI,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAA,uBAAa,EAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;YAE9D,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;gBACzB,IAAI,GAAG,CAAC,IAAI,CAAC,CAAA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YAAY,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,KAAU,EAAE,EAAE;YACtC,MAAM,CAAC,KAAK,CAAC,CAAA;QACf,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.decompressRequest = void 0;
|
|
27
|
+
const zlib = __importStar(require("zlib"));
|
|
28
|
+
const stream_1 = require("stream");
|
|
29
|
+
function decompressRequest(request) {
|
|
30
|
+
const contentEncoding = (request.headers["content-encoding"] || "").toLowerCase();
|
|
31
|
+
if (!["gzip", "deflate", "br"].includes(contentEncoding)) {
|
|
32
|
+
return request;
|
|
33
|
+
}
|
|
34
|
+
const isBrotli = contentEncoding === "br";
|
|
35
|
+
const decompress = isBrotli ? zlib.createBrotliDecompress() : zlib.createUnzip();
|
|
36
|
+
const stream = new stream_1.PassThrough();
|
|
37
|
+
decompress.on("error", (error) => {
|
|
38
|
+
// Ignore empty request
|
|
39
|
+
if (error.code === "Z_BUF_ERROR") {
|
|
40
|
+
stream.end();
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
stream.emit("error", error);
|
|
44
|
+
});
|
|
45
|
+
return (0, stream_1.pipeline)(request, decompress, stream, () => { });
|
|
46
|
+
}
|
|
47
|
+
exports.decompressRequest = decompressRequest;
|
|
48
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../src/utils/server.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AACA,2CAA4B;AAC5B,mCAAmF;AAGnF,SAAgB,iBAAiB,CAAC,OAA6B;IAC7D,MAAM,eAAe,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,kBAAkB,CAAC,IAAI,EAAE,CAAC,CAAC,WAAW,EAAE,CAAA;IAEjF,IAAI,CAAC,CAAC,MAAM,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC,QAAQ,CAAC,eAAe,CAAC,EAAE,CAAC;QACzD,OAAO,OAAO,CAAA;IAChB,CAAC;IAED,MAAM,QAAQ,GAAG,eAAe,KAAK,IAAI,CAAA;IAEzC,MAAM,UAAU,GAAG,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,sBAAsB,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,EAAE,CAAA;IAChF,MAAM,MAAM,GAAG,IAAI,oBAAiB,EAAE,CAAA;IAEtC,UAAU,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,KAAU,EAAE,EAAE;QACpC,uBAAuB;QACvB,IAAI,KAAK,CAAC,IAAI,KAAK,aAAa,EAAE,CAAC;YACjC,MAAM,CAAC,GAAG,EAAE,CAAA;YACZ,OAAM;QACR,CAAC;QAED,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,KAAK,CAAC,CAAA;IAC7B,CAAC,CAAC,CAAA;IAEF,OAAO,IAAA,iBAAc,EAAC,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,EAAE,GAAE,CAAC,CAAC,CAAA;AAC9D,CAAC;AAvBD,8CAuBC"}
|
package/package.json
CHANGED
package/src/server/http.ts
CHANGED
|
@@ -3,6 +3,7 @@ import {IncomingMessage, ServerResponse} from "http"
|
|
|
3
3
|
import {RpcConnectionContext} from "../rpc.js"
|
|
4
4
|
import {safeParseJson, safeStringify} from "../utils/json.js"
|
|
5
5
|
import {log} from "../logger.js"
|
|
6
|
+
import {decompressRequest} from "../utils/server"
|
|
6
7
|
|
|
7
8
|
export async function serveHttpRequest(
|
|
8
9
|
req: IncomingMessage,
|
|
@@ -85,15 +86,17 @@ export async function serveHttpRequest(
|
|
|
85
86
|
}
|
|
86
87
|
|
|
87
88
|
function readBody(req: http.IncomingMessage) {
|
|
89
|
+
const decompressed = decompressRequest(req)
|
|
90
|
+
|
|
88
91
|
return new Promise<string>((resolve, reject) => {
|
|
89
92
|
let body = ""
|
|
90
|
-
|
|
93
|
+
decompressed.on("data", (chunk: Buffer) => {
|
|
91
94
|
body += chunk.toString()
|
|
92
95
|
})
|
|
93
|
-
|
|
96
|
+
decompressed.on("end", () => {
|
|
94
97
|
resolve(body)
|
|
95
98
|
})
|
|
96
|
-
|
|
99
|
+
decompressed.on("error", (error: any) => {
|
|
97
100
|
reject(error)
|
|
98
101
|
})
|
|
99
102
|
})
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import * as http from "http"
|
|
2
|
+
import * as zlib from "zlib"
|
|
3
|
+
import {PassThrough as PassThroughStream, pipeline as streamPipeline} from "stream"
|
|
4
|
+
import * as Stream from "stream"
|
|
5
|
+
|
|
6
|
+
export function decompressRequest(request: http.IncomingMessage): Stream {
|
|
7
|
+
const contentEncoding = (request.headers["content-encoding"] || "").toLowerCase()
|
|
8
|
+
|
|
9
|
+
if (!["gzip", "deflate", "br"].includes(contentEncoding)) {
|
|
10
|
+
return request
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const isBrotli = contentEncoding === "br"
|
|
14
|
+
|
|
15
|
+
const decompress = isBrotli ? zlib.createBrotliDecompress() : zlib.createUnzip()
|
|
16
|
+
const stream = new PassThroughStream()
|
|
17
|
+
|
|
18
|
+
decompress.on("error", (error: any) => {
|
|
19
|
+
// Ignore empty request
|
|
20
|
+
if (error.code === "Z_BUF_ERROR") {
|
|
21
|
+
stream.end()
|
|
22
|
+
return
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
stream.emit("error", error)
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
return streamPipeline(request, decompress, stream, () => {})
|
|
29
|
+
}
|