fs-stream-websocket
Advanced tools
Comparing version 0.1.2 to 0.2.0
23
fs.js
@@ -63,3 +63,24 @@ var path = require('path'); | ||
var wsurl = cfg.protocol + '//' + path.join(cfg.host, filepath) | ||
// This __dirname __filename stuff is almost exclusively for jsbook, since | ||
// you want the "file" that is calling read/write to be able to use relative | ||
// paths without needing to configure options. There is basically no other | ||
// environment where __filename/__dirname would be defined globally. | ||
var gbl = null; | ||
if (typeof global !== 'undefined' && global.__filename && global.__dirname) { | ||
gbl = global; | ||
} | ||
if (typeof window !== 'undefined' && window.__filename && window.__dirname) { | ||
gbl = window; | ||
} | ||
if (gbl) { | ||
opts.__filename = gbl.__filename; | ||
opts.__dirname = gbl.__dirname; | ||
} | ||
opts.filepath = filepath; | ||
var wsurl = cfg.protocol + '//' + cfg.host + '/' + cfg.prefix + '/' | ||
+ '?' | ||
@@ -66,0 +87,0 @@ + querystring.stringify(opts); |
{ | ||
"name": "fs-stream-websocket", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "`fs.createReadStream` and `fs.createWriteStream` over a websocket, assumedly in a browser.", | ||
@@ -5,0 +5,0 @@ "main": "server.js", |
@@ -7,3 +7,3 @@ | ||
This module is incomplete, and is not secure. | ||
This module is incomplete, and has no security or sandbox for restricting file read/writing. | ||
@@ -23,3 +23,3 @@ [![Build Status](https://travis-ci.org/kirbysayshi/fs-stream-websocket.svg?branch=master)](https://travis-ci.org/kirbysayshi/fs-stream-websocket) | ||
host: window.location.host, | ||
path: '', // Added after host as a prefix | ||
prefix: '', // Added after host as a prefix | ||
protocol: 'ws:' | ||
@@ -26,0 +26,0 @@ }) |
@@ -22,4 +22,4 @@ var fs = require('fs'); | ||
var urlobj = url.parse(ws.upgradeReq.url); | ||
var filepath = path.join(cfg.root, urlobj.pathname); | ||
var options = querystring.parse(urlobj.query); | ||
var filepath = path.resolve(cfg.root, options.__dirname || '', options.filepath); | ||
@@ -26,0 +26,0 @@ dbg('recv start %s', filepath) |
38
test.js
@@ -6,2 +6,4 @@ var test = require('tape'); | ||
var fs = require('fs'); | ||
var querystring = require('querystring'); | ||
var url = require('url'); | ||
@@ -17,6 +19,38 @@ var client = require('./fs'); | ||
test('create[Read|Write]Stream transmits __filename __dirname if globally present', function(t) { | ||
t.plan(3); | ||
global.__filename = __filename; | ||
global.__dirname = __dirname; | ||
var echo = echoserver(function() { | ||
client | ||
.createReadStream('./package.json', { encoding: 'utf8' }) | ||
.pipe(concat(function(data) { | ||
// sink. | ||
})) | ||
}); | ||
echo.wss.on('connection', function(ws) { | ||
var urlobj = url.parse(ws.upgradeReq.url); | ||
var options = querystring.parse(urlobj.query); | ||
t.equal(options.__filename, __filename); | ||
t.equal(options.__dirname, __dirname); | ||
t.end(); | ||
}) | ||
t.test('cleanup', function(t) { | ||
delete global.__filename; | ||
delete global.__dirname; | ||
echo.http.close(t.end); | ||
}) | ||
}); | ||
test('createReadStream emits utf8', function(t) { | ||
var echo = echoserver(function() { | ||
client | ||
.createReadStream('./package.json', { encoding: 'utf8' }) | ||
.createReadStream('package.json', { encoding: 'utf8' }) | ||
.pipe(all) | ||
@@ -46,3 +80,3 @@ }); | ||
test('createWritableStream writes utf8', function(t) { | ||
test('createWriteStream writes utf8', function(t) { | ||
@@ -49,0 +83,0 @@ // package.json is is piped to the server as temp.json, |
9709
224