kopia lustrzana https://github.com/viljoviitanen/js-untar
test running, but web worker isn't being called?
rodzic
d49886d085
commit
4d871a39f5
15
gulpfile.js
15
gulpfile.js
|
@ -6,7 +6,7 @@ var gulp = require("gulp"),
|
||||||
addSrc = require("gulp-add-src"),
|
addSrc = require("gulp-add-src"),
|
||||||
concat = require("gulp-concat"),
|
concat = require("gulp-concat"),
|
||||||
jshint = require("gulp-jshint"),
|
jshint = require("gulp-jshint"),
|
||||||
jasmine = require("gulp-jasmine");
|
KarmaServer = require('karma').Server;
|
||||||
|
|
||||||
gulp.task("default", function() {
|
gulp.task("default", function() {
|
||||||
return gulp.src("untar-worker.js")
|
return gulp.src("untar-worker.js")
|
||||||
|
@ -16,7 +16,7 @@ gulp.task("default", function() {
|
||||||
.pipe(jshint.reporter("fail"))
|
.pipe(jshint.reporter("fail"))
|
||||||
.pipe(uglify())
|
.pipe(uglify())
|
||||||
.pipe(insert.transform(function(contents, file) {
|
.pipe(insert.transform(function(contents, file) {
|
||||||
var str = ["\nworkerScriptUri = URL.createObjectURL(new Blob([\""];
|
var str = ["\nvar workerScriptUri = URL.createObjectURL(createBlob([\""];
|
||||||
str.push(contents.replace(/"/g, '\\"'));
|
str.push(contents.replace(/"/g, '\\"'));
|
||||||
str.push("\"]));");
|
str.push("\"]));");
|
||||||
|
|
||||||
|
@ -37,10 +37,9 @@ gulp.task("default", function() {
|
||||||
.pipe(gulp.dest("build/dist"));
|
.pipe(gulp.dest("build/dist"));
|
||||||
});
|
});
|
||||||
|
|
||||||
gulp.task("test", ["default"], function() {
|
gulp.task("test", ["default"], function(done) {
|
||||||
return gulp.src("spec/*.js")
|
new KarmaServer({
|
||||||
.pipe(jasmine({
|
configFile: __dirname + '/karma.conf.js',
|
||||||
includeStackTrace: true,
|
singleRun: true
|
||||||
verbose: true
|
}, done).start();
|
||||||
}));
|
|
||||||
});
|
});
|
|
@ -0,0 +1,69 @@
|
||||||
|
// Karma configuration
|
||||||
|
// Generated on Wed Aug 26 2015 11:44:53 GMT+0200 (Vesteuropa, sommertid)
|
||||||
|
|
||||||
|
module.exports = function(config) {
|
||||||
|
config.set({
|
||||||
|
|
||||||
|
// base path that will be used to resolve all patterns (eg. files, exclude)
|
||||||
|
basePath: '',
|
||||||
|
|
||||||
|
|
||||||
|
// frameworks to use
|
||||||
|
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
|
||||||
|
frameworks: ['jasmine', 'requirejs'],
|
||||||
|
|
||||||
|
|
||||||
|
// list of files / patterns to load in the browser
|
||||||
|
files: [
|
||||||
|
'https://www.promisejs.org/polyfills/promise-6.1.0.js',
|
||||||
|
'test-main.js',
|
||||||
|
{pattern: 'build/dev/**/*.js', included: false},
|
||||||
|
{pattern: 'spec/**/*.*', included: false}
|
||||||
|
],
|
||||||
|
|
||||||
|
|
||||||
|
// list of files to exclude
|
||||||
|
exclude: [
|
||||||
|
],
|
||||||
|
|
||||||
|
|
||||||
|
// preprocess matching files before serving them to the browser
|
||||||
|
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
|
||||||
|
preprocessors: {
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
// test results reporter to use
|
||||||
|
// possible values: 'dots', 'progress'
|
||||||
|
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
|
||||||
|
reporters: ['progress'],
|
||||||
|
|
||||||
|
|
||||||
|
// web server port
|
||||||
|
port: 9876,
|
||||||
|
|
||||||
|
|
||||||
|
// enable / disable colors in the output (reporters and logs)
|
||||||
|
colors: true,
|
||||||
|
|
||||||
|
|
||||||
|
// level of logging
|
||||||
|
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
|
||||||
|
logLevel: config.LOG_INFO,
|
||||||
|
|
||||||
|
|
||||||
|
// enable / disable watching file and executing tests whenever any file changes
|
||||||
|
autoWatch: false,
|
||||||
|
|
||||||
|
|
||||||
|
// start these browsers
|
||||||
|
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
|
||||||
|
browsers: ['PhantomJS'],
|
||||||
|
|
||||||
|
browserNoActivityTimeout: 60000,
|
||||||
|
|
||||||
|
// Continuous Integration mode
|
||||||
|
// if true, Karma captures browsers, runs the tests and exits
|
||||||
|
singleRun: false
|
||||||
|
})
|
||||||
|
}
|
11
package.json
11
package.json
|
@ -26,14 +26,21 @@
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/Qvazar/js-untar",
|
"homepage": "https://github.com/Qvazar/js-untar",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"es6-promise": "^3.0.2",
|
||||||
"gulp": "^3.9.0",
|
"gulp": "^3.9.0",
|
||||||
"gulp-add-src": "^0.2.0",
|
"gulp-add-src": "^0.2.0",
|
||||||
"gulp-concat": "^2.6.0",
|
"gulp-concat": "^2.6.0",
|
||||||
"gulp-insert": "^0.5.0",
|
"gulp-insert": "^0.5.0",
|
||||||
"gulp-jasmine": "^2.0.1",
|
|
||||||
"gulp-jshint": "^1.11.2",
|
"gulp-jshint": "^1.11.2",
|
||||||
"gulp-sourcemaps": "^1.5.2",
|
"gulp-sourcemaps": "^1.5.2",
|
||||||
"gulp-uglify": "^1.3.0",
|
"gulp-uglify": "^1.3.0",
|
||||||
"gulp-umd": "^0.2.0"
|
"gulp-umd": "^0.2.0",
|
||||||
|
"jasmine-core": "^2.3.4",
|
||||||
|
"karma": "^0.13.9",
|
||||||
|
"karma-jasmine": "^0.3.6",
|
||||||
|
"karma-phantomjs-launcher": "^0.2.1",
|
||||||
|
"karma-requirejs": "^0.2.2",
|
||||||
|
"phantomjs": "^1.9.18",
|
||||||
|
"requirejs": "^2.1.20"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
define(["build/dev/untar"], function(untar) {
|
||||||
|
describe("untar", function() {
|
||||||
|
it("should unpack 3 files and a directory with 3 files", function(done) {
|
||||||
|
untar("/base/spec/data/test.tar", {
|
||||||
|
onExtract: function(file) { done(); }
|
||||||
|
}).then(
|
||||||
|
function(files) {
|
||||||
|
expect(files.length).toBe(6);
|
||||||
|
done();
|
||||||
|
},
|
||||||
|
function(err) {
|
||||||
|
done.fail(JSON.stringify(err));
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}, 20000);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
var untar = require("../build/dev/untar.js");
|
|
||||||
|
|
||||||
describe("untar", function() {
|
|
||||||
it("should unpack 3 files and a directory with 3 files", function() {
|
|
||||||
untar("data/test.tar").then(function(files) {
|
|
||||||
expect(files.length).toBe(6);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
})
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
var allTestFiles = [];
|
||||||
|
var TEST_REGEXP = /(spec|test)\.js$/i;
|
||||||
|
|
||||||
|
// Get a list of all the test files to include
|
||||||
|
Object.keys(window.__karma__.files).forEach(function(file) {
|
||||||
|
if (TEST_REGEXP.test(file)) {
|
||||||
|
// Normalize paths to RequireJS module names.
|
||||||
|
// If you require sub-dependencies of test files to be loaded as-is (requiring file extension)
|
||||||
|
// then do not normalize the paths
|
||||||
|
var normalizedTestModule = file.replace(/^\/base\/|\.js$/g, '');
|
||||||
|
allTestFiles.push(normalizedTestModule);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
require.config({
|
||||||
|
// Karma serves files under /base, which is the basePath from your config file
|
||||||
|
baseUrl: '/base',
|
||||||
|
|
||||||
|
// dynamically load all test files
|
||||||
|
deps: allTestFiles,
|
||||||
|
|
||||||
|
// we have to kickoff jasmine, as it is asynchronous
|
||||||
|
callback: window.__karma__.start
|
||||||
|
});
|
|
@ -1,54 +1,43 @@
|
||||||
(function() {
|
/* globals postMessage: false, DataView: false, self: false */
|
||||||
|
/* jshint -W097 */
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
onmessage = function(source) {
|
self.onmessage = function(msg) {
|
||||||
if (typeof source === "string") {
|
postLog("info", "Received message.");
|
||||||
loadArrayBuffer(source).then(
|
try {
|
||||||
untarBuffer,
|
if (msg.data.type === "extract") {
|
||||||
function(err) { postMessage({ type: "error", data: err }); }
|
untarBuffer(msg.data.buffer);
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
untarBuffer(source);
|
throw new Error("Unknown message type.");
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
postError(err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function loadArrayBuffer(uri) {
|
function postError(err) {
|
||||||
return new Promise(function(resolve, reject) {
|
postMessage({ type: "error", data: err });
|
||||||
try {
|
|
||||||
var request = new XMLHttpRequest();
|
|
||||||
|
|
||||||
request.addEventListener("progress", function(e) {
|
|
||||||
postMessage({ type: "loading", data: e });
|
|
||||||
});
|
|
||||||
|
|
||||||
request.addEventListener("load", function(e) {
|
|
||||||
resolve(request.response);
|
|
||||||
});
|
|
||||||
|
|
||||||
request.addEventListener("error", reject);
|
|
||||||
request.addEventListener("abort", reject);
|
|
||||||
|
|
||||||
request.open("GET", uri);
|
|
||||||
request.responseType = "arraybuffer";
|
|
||||||
request.send();
|
|
||||||
} catch (err) {
|
|
||||||
reject(err);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
function postLog(level, msg) {
|
||||||
|
postMessage({ type: "log", data: { level: level, msg: msg }});
|
||||||
}
|
}
|
||||||
|
|
||||||
function untarBuffer(arrayBuffer) {
|
function untarBuffer(arrayBuffer) {
|
||||||
try {
|
try {
|
||||||
|
postLog("info", "buffer size: " + arrayBuffer.byteLength);
|
||||||
var tarFileStream = new TarFileStream(arrayBuffer);
|
var tarFileStream = new TarFileStream(arrayBuffer);
|
||||||
while (tarFileStream.hasNext()) {
|
while (tarFileStream.hasNext()) {
|
||||||
var file = tarFileStream.next();
|
var file = tarFileStream.next();
|
||||||
|
|
||||||
|
if (file.buffer) {
|
||||||
postMessage({ type: "extract", data: file }, [file.buffer]);
|
postMessage({ type: "extract", data: file }, [file.buffer]);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
postMessage({ type: "complete" });
|
postMessage({ type: "complete" });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
postMessage({ type: "error", data: err });
|
postError(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,6 +93,10 @@
|
||||||
} else {
|
} else {
|
||||||
this._position = newpos;
|
this._position = newpos;
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
size: function() {
|
||||||
|
return this._bufferView.byteLength;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -113,7 +106,7 @@
|
||||||
|
|
||||||
TarFileStream.prototype = {
|
TarFileStream.prototype = {
|
||||||
hasNext: function() {
|
hasNext: function() {
|
||||||
return this._stream.peekUint32() !== 0;
|
return this._stream.position() < this._stream.size() && this._stream.peekUint32() !== 0;
|
||||||
},
|
},
|
||||||
|
|
||||||
next: function() {
|
next: function() {
|
||||||
|
@ -166,4 +159,5 @@
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}());
|
|
||||||
|
postMessage({ type: "ready" });
|
111
untar.js
111
untar.js
|
@ -1,5 +1,73 @@
|
||||||
|
/* globals window: false, Blob: false, Promise: false, console: false, XMLHttpRequest: false, Worker: false */
|
||||||
|
/* jshint -W097 */
|
||||||
|
"use strict";
|
||||||
|
|
||||||
var workerScriptUri; // Included at compile time
|
var workerScriptUri; // Included at compile time
|
||||||
|
|
||||||
|
var URL = window.URL || window.webkitURL;
|
||||||
|
|
||||||
|
var createBlob = (function() {
|
||||||
|
if (typeof window.Blob === "function") {
|
||||||
|
return function(dataArray) { return new Blob(dataArray); };
|
||||||
|
} else {
|
||||||
|
var BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder;
|
||||||
|
|
||||||
|
return function(dataArray) {
|
||||||
|
var builder = new BlobBuilder();
|
||||||
|
|
||||||
|
for (var i = 0; i < dataArray.length; ++i) {
|
||||||
|
builder.append(dataArray[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return builder.getBlob();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}());
|
||||||
|
|
||||||
|
function createBlob(dataArray) {
|
||||||
|
if (typeof window.Blob === "function") {
|
||||||
|
return new Blob(dataArray);
|
||||||
|
} else {
|
||||||
|
var BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder;
|
||||||
|
var builder = new BlobBuilder();
|
||||||
|
|
||||||
|
for (var i = 0; i < dataArray.length; ++i) {
|
||||||
|
builder.append(dataArray[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return builder.getBlob();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadArrayBuffer(uri) {
|
||||||
|
console.info("loadArrayBuffer called");
|
||||||
|
|
||||||
|
return new Promise(function(resolve, reject) {
|
||||||
|
var request = new XMLHttpRequest();
|
||||||
|
|
||||||
|
/*
|
||||||
|
request.addEventListener("progress", function(e) {
|
||||||
|
postMessage({ type: "loading", data: e });
|
||||||
|
});
|
||||||
|
*/
|
||||||
|
|
||||||
|
request.addEventListener("load", function(e) {
|
||||||
|
if (request.status >= 200 && request.status < 400) {
|
||||||
|
resolve(request.response);
|
||||||
|
} else {
|
||||||
|
reject(new Error(request.status + " " + request.statusText));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
request.addEventListener("error", function(err) { reject(err); });
|
||||||
|
request.addEventListener("abort", function(err) { reject(err); });
|
||||||
|
|
||||||
|
request.open("GET", uri, true);
|
||||||
|
request.responseType = "arraybuffer";
|
||||||
|
request.send();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
source = ArrayBuffer or a url string. If an ArrayBuffer, it will be transfered to the web worker and will thus not be available in the window after.
|
source = ArrayBuffer or a url string. If an ArrayBuffer, it will be transfered to the web worker and will thus not be available in the window after.
|
||||||
options = {
|
options = {
|
||||||
|
@ -10,12 +78,13 @@ options = {
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
function untar(source, options) {
|
function untar(source, options) {
|
||||||
"use strict";
|
console.info("untar called");
|
||||||
|
|
||||||
if (typeof Promise !== "function") {
|
if (typeof Promise !== "function") {
|
||||||
throw new Error("Promise implementation not available in this environment.");
|
throw new Error("Promise implementation not available in this environment.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof Worker !== "function") {
|
if (!window.Worker) {
|
||||||
throw new Error("Worker implementation not available in this environment.");
|
throw new Error("Worker implementation not available in this environment.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,11 +98,40 @@ function untar(source, options) {
|
||||||
var onError = options.onError || noop;
|
var onError = options.onError || noop;
|
||||||
|
|
||||||
var worker = new Worker(workerScriptUri);
|
var worker = new Worker(workerScriptUri);
|
||||||
|
|
||||||
|
function initWorker() {
|
||||||
|
// Is source a string? Then assume it's a URL and download it.
|
||||||
|
if (typeof source === "string") {
|
||||||
|
loadArrayBuffer(source).then(
|
||||||
|
function(buffer) {
|
||||||
|
console.info("Loaded tar file, extracting.");
|
||||||
|
worker.postMessage({ type: "extract", buffer: buffer }, [buffer]);
|
||||||
|
},
|
||||||
|
function(err) {
|
||||||
|
onError(err);
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
console.info("Extracting tar file.");
|
||||||
|
worker.postMessage({ type: "extract", buffer: source }, [source]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var files = [];
|
var files = [];
|
||||||
var msgData;
|
var msgData;
|
||||||
|
|
||||||
worker.onmessage = function(message) {
|
worker.onmessage = function(message) {
|
||||||
|
message = message.data;
|
||||||
|
|
||||||
switch (message.type) {
|
switch (message.type) {
|
||||||
|
case "ready":
|
||||||
|
console.info("Worker is ready.");
|
||||||
|
initWorker();
|
||||||
|
break;
|
||||||
|
case "log":
|
||||||
|
console[message.data.level]("Worker: " + message.data.msg);
|
||||||
|
break;
|
||||||
case "loading":
|
case "loading":
|
||||||
onLoading(message.data);
|
onLoading(message.data);
|
||||||
break;
|
break;
|
||||||
|
@ -52,26 +150,22 @@ function untar(source, options) {
|
||||||
reject(msgData);
|
reject(msgData);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
msgData = new Error("Unknown message from worker.");
|
msgData = new Error("Unknown message from worker: " + message.type);
|
||||||
onError(msgData);
|
onError(msgData);
|
||||||
reject(msgData);
|
reject(msgData);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Don't transfer if source is a string. Only ArrayBuffer can be transfered.
|
|
||||||
worker.postMessage(source, (typeof source === "string" ? undefined : [source]));
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function TarFile(orig) {
|
function TarFile(orig) {
|
||||||
"use strict";
|
|
||||||
this._blobUrl = null;
|
this._blobUrl = null;
|
||||||
|
|
||||||
for (var p in orig) {
|
for (var p in orig) {
|
||||||
switch (p) {
|
switch (p) {
|
||||||
case "buffer":
|
case "buffer":
|
||||||
this.blob = new Blob([orig.buffer]);
|
this.blob = createBlob([orig.buffer]);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
this[p] = orig[p];
|
this[p] = orig[p];
|
||||||
|
@ -82,7 +176,6 @@ function TarFile(orig) {
|
||||||
|
|
||||||
TarFile.prototype = {
|
TarFile.prototype = {
|
||||||
getObjectUrl: function() {
|
getObjectUrl: function() {
|
||||||
"use strict";
|
|
||||||
if (!this._blobUrl) {
|
if (!this._blobUrl) {
|
||||||
this._blobUrl = URL.createObjectURL(this.blob);
|
this._blobUrl = URL.createObjectURL(this.blob);
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue