Skip to content

Commit

Permalink
Merge branch 'release/0.9.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
nwtgck committed Mar 8, 2019
2 parents 9f01bdd + bb901a2 commit 61af41d
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 80 deletions.
13 changes: 10 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,21 @@ jobs:
- run:
name: Working test with Docker image
command: |
set -x
# Run a server
docker run -d -p 8080:8080 piping-server
# Wait for server running
sleep 1
# Create a file to send
echo 'hello, world' > /tmp/hello.txt
# Send and wait for a reciever
curl --data-binary 'hello, world' localhost:8080/mypath &
# Get data
curl localhost:8080/mypath
curl -T /tmp/hello.txt localhost:8080/mypath &
# Get data as a file
curl localhost:8080/mypath > /tmp/download.txt
# Print downloaded file
cat /tmp/download.txt
# Test the equality
diff /tmp/hello.txt /tmp/download.txt
# (from: https://circleci.com/blog/publishing-npm-packages-using-circleci-2-0/)
npm_publish:
Expand Down
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)

## [Unreleased]

## [0.9.0] - 2019-03-08
### Changed
* Make logs for sender consistent
* Update dependencies

## [0.8.10] - 2019-02-24
### Changed
* Not to allow user to send "/robots.txt" and return 404 when user access to "/rebots.txt"
Expand Down Expand Up @@ -138,7 +143,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
* Docker automated build on Docker Hub
* Support HTTPS

[Unreleased]: https://github.com/nwtgck/piping-server/compare/v0.8.10...HEAD
[Unreleased]: https://github.com/nwtgck/piping-server/compare/v0.9.0...HEAD
[0.9.0]: https://github.com/nwtgck/piping-seraver/compare/v0.8.10...v0.9.0
[0.8.10]: https://github.com/nwtgck/piping-seraver/compare/v0.8.9...v0.8.10
[0.8.9]: https://github.com/nwtgck/piping-seraver/compare/v0.8.8...v0.8.9
[0.8.8]: https://github.com/nwtgck/piping-seraver/compare/v0.8.7...v0.8.8
Expand Down
64 changes: 22 additions & 42 deletions package-lock.json

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

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "piping-server",
"version": "0.8.10",
"version": "0.9.0",
"description": "Streaming Data Transfer Server over HTTP/HTTPS",
"bin": {
"piping-server": "dist/src/index.js"
Expand Down Expand Up @@ -38,7 +38,6 @@
"espower-typescript": "^9.0.0",
"get-port": "^4.0.0",
"mocha": "^6.0.0",
"pkginfo": "^0.4.1",
"power-assert": "^1.4.4",
"request": "^2.88.0",
"then-request": "^6.0.0",
Expand Down
37 changes: 17 additions & 20 deletions src/piping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type ReqRes = {

type Pipe = {
readonly sender: ReqRes;
readonly receivers: ReqRes[];
readonly receivers: ReadonlyArray<ReqRes>;
};

type ReqResAndUnsubscribe = {
Expand Down Expand Up @@ -272,7 +272,7 @@ export class Server {
}
break;
default:
res.end(`Error: Unsupported method: ${req.method}\n`);
res.end(`[ERROR] Unsupported method: ${req.method}.\n`);
break;
}
};
Expand All @@ -293,6 +293,9 @@ export class Server {

const {sender, receivers} = pipe;

// Emit message to sender
sender.res.write(`[INFO] Start sending with ${pipe.receivers.length} receiver(s)!\n`);

const isMultipart: boolean = (sender.req.headers["content-type"] || "").includes("multipart/form-data");

const part: multiparty.Part | undefined =
Expand Down Expand Up @@ -324,16 +327,10 @@ export class Server {
}
};

// Common headers for receivers
const commonHeaders: http.OutgoingHttpHeaders = {
"Access-Control-Allow-Origin": "*"
};

const headers: http.OutgoingHttpHeaders =
// If not multi-part sending
part === undefined ?
{
...commonHeaders,
// Add Content-Length if it exists
...(
sender.req.headers["content-length"] === undefined ?
Expand All @@ -351,7 +348,6 @@ export class Server {
)
} :
{
...commonHeaders,
// Add Content-Length if it exists
...(
part.byteCount === undefined ?
Expand All @@ -369,7 +365,12 @@ export class Server {
};

// Write headers to a receiver
receiver.res.writeHead(200, headers);
receiver.res.writeHead(200, {
...{
"Access-Control-Allow-Origin": "*"
},
...headers
});

const passThrough = new stream.PassThrough();
senderData.pipe(passThrough);
Expand Down Expand Up @@ -399,13 +400,13 @@ export class Server {
});

senderData.on("end", () => {
sender.res.end("[INFO] Sending Successful!\n");
sender.res.end("[INFO] Sending successful!\n");
// Delete from established
delete this.pathToEstablished[path];
});

senderData.on("error", (error) => {
sender.res.end("[ERROR] Sending Failed.\n");
sender.res.end("[ERROR] Sending failed.\n");
// Delete from established
delete this.pathToEstablished[path];
});
Expand Down Expand Up @@ -454,14 +455,12 @@ export class Server {
getPipeIfEstablished(unestablishedPipe);

if (pipe !== undefined) {
// Emit message to sender
res.write("Start sending!\n");
// Start data transfer
this.runPipe(reqPath, pipe);
}
} else {
res.writeHead(400);
res.end(`Error: The number of receivers should be ${unestablishedPipe.nReceivers} but ${nReceivers}.\n`);
res.end(`[ERROR] The number of receivers should be ${unestablishedPipe.nReceivers} but ${nReceivers}.\n`);
}
} else {
res.writeHead(400);
Expand Down Expand Up @@ -497,7 +496,7 @@ export class Server {
res.end(`[ERROR] n should > 0, but n = ${nReceivers}.\n`);
} else if (reqPath in this.pathToEstablished) {
res.writeHead(400);
res.end(`Error: Connection on '${reqPath}' has been established already.\n`);
res.end(`[ERROR] Connection on '${reqPath}' has been established already.\n`);
} else {
// If the path connection is connecting
if (reqPath in this.pathToUnestablishedPipe) {
Expand All @@ -522,18 +521,16 @@ export class Server {
getPipeIfEstablished(unestablishedPipe);

if (pipe !== undefined) {
// Emit message to sender
pipe.sender.res.write(`[INFO] Start sending with ${pipe.receivers.length} receiver(s)!\n`);
// Start data transfer
this.runPipe(reqPath, pipe);
}
} else {
res.writeHead(400);
res.end("Error: The number of receivers has reached limits.\n");
res.end("[ERROR] The number of receivers has reached limits.\n");
}
} else {
res.writeHead(400);
res.end(`Error: The number of receivers should be ${unestablishedPipe.nReceivers} but ${nReceivers}.\n`);
res.end(`[ERROR] The number of receivers should be ${unestablishedPipe.nReceivers} but ${nReceivers}.\n`);
}
} else {
// Create a receiver
Expand Down
11 changes: 5 additions & 6 deletions test/piping.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import * as getPort from "get-port";
import * as http from "http";
import * as pkginfo from "pkginfo";
import * as assert from "power-assert";
import * as request from "request";
import thenRequest from "then-request";
import * as piping from "../src/piping";

// Set module.exports.version
pkginfo(module, "version");
import {VERSION} from "../src/version";

/**
* Listen on the specify port
Expand All @@ -26,7 +23,7 @@ function listenPromise(server: http.Server, port: number): Promise<void> {
*/
function closePromise(server: http.Server): Promise<void> {
return new Promise<void>((resolve) => {
server.close(resolve);
server.close(() => resolve());
});
}

Expand Down Expand Up @@ -74,7 +71,7 @@ describe("piping.Server", () => {

// Body should be index page
// (from: https://stackoverflow.com/a/22339262/2885946)
assert.equal(res.getBody("UTF-8"), module.exports.version + "\n");
assert.equal(res.getBody("UTF-8"), VERSION + "\n");
});

it("should return help page", async () => {
Expand Down Expand Up @@ -175,6 +172,8 @@ describe("piping.Server", () => {
// Get request promise
const reqPromise = thenRequest("GET", `${pipingUrl}/mydataid`);

await sleep(10);

// Send data
const postRes = await thenRequest("POST", `${pipingUrl}/mydataid`, {
body: "this is a content"
Expand Down
6 changes: 0 additions & 6 deletions types/pkginfo.d.ts

This file was deleted.

0 comments on commit 61af41d

Please sign in to comment.