Skip to content

Commit

Permalink
Merge branch 'release/0.14.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
nwtgck committed Aug 26, 2019
2 parents 8d50d6a + e97f969 commit f5e62b9
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 17 deletions.
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ANCHORS:
node_steps: &node_steps
steps:
- checkout
- run: npm install
- run: npm ci
- run: npm run lint
- run: npm test
# Check whether "run build" is successful
Expand Down Expand Up @@ -41,7 +41,7 @@ jobs:
sleep 1
# Create a file to send
echo 'hello, world' > /tmp/hello.txt
# Send and wait for a reciever
# Send and wait for a receiver
curl -T /tmp/hello.txt localhost:8080/mypath &
# Get data as a file
curl localhost:8080/mypath > /tmp/download.txt
Expand All @@ -56,7 +56,7 @@ jobs:
- image: circleci/node:10
steps:
- checkout
- run: npm install
- run: npm ci
- run:
name: Authenticate with registry
command: echo -e "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/.npmrc
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jobs:
uses: actions/setup-node@v1
with:
version: 10.x
- name: npm install, build, and test
- name: npm ci, build, and test
run: |
npm install
npm ci
npm run build
npm test
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.14.0] - 2019-08-26
### Changed
* Reject Service Worker registration requests for receiver using Web browser
* Not add `application/octet-stream` if Content-Type is missing

## [0.13.2] - 2019-08-23
### Changed
* Update dependencies
Expand Down Expand Up @@ -224,7 +229,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.13.2...HEAD
[Unreleased]: https://github.com/nwtgck/piping-server/compare/v0.14.0...HEAD
[0.14.0]: https://github.com/nwtgck/piping-server/compare/v0.13.2...v0.14.0
[0.13.2]: https://github.com/nwtgck/piping-server/compare/v0.13.1...v0.13.2
[0.13.1]: https://github.com/nwtgck/piping-server/compare/v0.13.0...v0.13.1
[0.13.0]: https://github.com/nwtgck/piping-server/compare/v0.12.0...v0.13.0
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ WORKDIR /app

# Install requirements, build and remove devDependencies
# (from: https://stackoverflow.com/a/25571391/2885946)
RUN npm install && \
RUN npm ci && \
npm run build && \
npm prune --production

Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ install:
# Get the latest stable version of Node.js or io.js
- ps: Install-Product node $env:nodejs_version
# Install dependencies
- npm install
- npm ci

# Post-install test scripts.
test_script:
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "piping-server",
"version": "0.13.2",
"version": "0.14.0",
"description": "Streaming Data Transfer Server over HTTP/HTTPS",
"bin": {
"piping-server": "dist/src/index.js"
Expand Down
26 changes: 20 additions & 6 deletions src/piping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ export class Server {
case NAME_TO_RESERVED_PATH.robotsTxt:
res.writeHead(404);
res.end();
break;
default:
// Handle a receiver
this.handleReceiver(req, res, reqPath);
Expand All @@ -301,6 +302,7 @@ export class Server {
"Content-Length": 0
});
res.end();
break;
default:
res.end(`[ERROR] Unsupported method: ${req.method}.\n` as any);
break;
Expand Down Expand Up @@ -367,14 +369,16 @@ export class Server {
const contentLength: string | number | undefined = part === undefined ?
sender.req.headers["content-length"] : part.byteCount;
// Get Content-Type from part or HTTP header.
const contentType: string = (() => {
// If none, it is 'application/octet-stream'
const type = (part === undefined ?
sender.req.headers["content-type"] : part.headers["content-type"]) || "application/octet-stream";
const contentType: string | undefined = (() => {
const type: string | undefined = (part === undefined ?
sender.req.headers["content-type"] : part.headers["content-type"]);
if (type === undefined) {
return undefined;
}
const matched = type.match(/^\s*([^;]*)(\s*;?.*)$/);
// If invalid Content-Type
if (matched === null) {
return "application/octet-stream";
return undefined;
} else {
// Extract MIME type and parameters
const mimeType: string = matched[1];
Expand All @@ -391,7 +395,7 @@ export class Server {
// Write headers to a receiver
receiver.res.writeHead(200, {
...(contentLength === undefined ? {} : {"Content-Length": contentLength}),
"Content-Type": contentType,
...(contentType === undefined ? {} : {"Content-Type": contentType}),
...(contentDisposition === undefined ? {} : {"Content-Disposition": contentDisposition}),
"Access-Control-Allow-Origin": "*",
"Access-Control-Expose-Headers": "Content-Length, Content-Type",
Expand Down Expand Up @@ -527,6 +531,16 @@ export class Server {
* @param {string} reqPath
*/
private handleReceiver(req: HttpReq, res: HttpRes, reqPath: string): void {
// If the receiver requests Service Worker registration
// (from: https://speakerdeck.com/masatokinugawa/pwa-study-sw?slide=32)"
if (req.headers["service-worker"] === "script") {
// Reject Service Worker registration
res.writeHead(400, {
"Access-Control-Allow-Origin": "*"
});
res.end(`[ERROR] Service Worker registration is rejected.\n` as any);
return;
}
// Get the number of receivers
const nReceivers = Server.getNReceivers(req.url);
// If the number of receivers is invalid
Expand Down
14 changes: 13 additions & 1 deletion test/piping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ describe("piping.Server", () => {
assert.strictEqual(headers["content-length"], "0");
});

it("should reject Service Worker registration request", async () => {
const res = await thenRequest("GET", `${pipingUrl}/mysw.js`, {
headers: {
"Service-Worker": "script"
}
});

assert.strictEqual(res.statusCode, 400);
const headers = res.headers;
assert.strictEqual(headers["access-control-allow-origin"], "*");
});

it("should handle connection (receiver O, sender: O)", async () => {
// Get request promise
const reqPromise = thenRequest("GET", `${pipingUrl}/mydataid`);
Expand All @@ -167,7 +179,7 @@ describe("piping.Server", () => {
// Content-length should be returned
assert.strictEqual(data.headers["content-length"], "this is a content".length.toString());
assert.strictEqual(data.headers["content-length"], "this is a content".length.toString());
assert.strictEqual(data.headers["content-type"], "application/octet-stream");
assert.strictEqual(data.headers["content-type"], undefined);
assert.strictEqual(data.headers["x-content-type-options"], "nosniff");
});

Expand Down

0 comments on commit f5e62b9

Please sign in to comment.