Skip to content

Commit c27fd83

Browse files
committed
Getting jsmime can be testing in browser & nodejs, they shareing the same
copy of test cases. To do this, we need to using the same text-encoding for both nodejs & browser. And in the future, we could easily running jsmime in worker, and add UTF7 support. Support for code coverage inspect.
1 parent 8b68118 commit c27fd83

16 files changed

Lines changed: 295 additions & 28 deletions

.babelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"plugins": ["transform-es2015-destructuring"],
3+
"ignore": ["./node_modules/**/*", "./jsmime.*"]
4+
}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
dist
22
node_modules
3+
/npm-debug.log
4+
/coverage
5+
/jsmime.*

.tern-project

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"ecmaVersion": 6,
3+
"libs": []
4+
}

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ This code depends on the following ES6 features and Web APIs:
2929
* btoa, atob (found on global Windows or WorkerScopes)
3030
* TextDecoder
3131

32+
Developments
33+
============
34+
```
35+
npm install -g mocha # For running mocha tests
36+
npm install -g http-server # Installing the http-server for firefox test
37+
http-server # At the jsmime root directory
38+
39+
npm install -g gulp # Installing gulp for mocha test with node
40+
41+
gulp test #Running all the tests
42+
npm run mocha -- test\test_header.js #Used for running specific test case
43+
44+
gulp bundle # Creating the dist\jsmime.js that could be able used in browser
45+
gulp coverage # Viewing the running result
46+
gulp watch # Watch the file changes & generating the dist\jsmime.js automatically
47+
```
48+
3249
Versions and API stability
3350
==========================
3451

encodings.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict'
2+
3+
4+
if (typeof TextDecoder === 'undefined' || typeof TextEncoder === 'undefined') {
5+
// TODO: GB18030 decode is invalid in text-encoding
6+
module.exports = require('text-encoding')
7+
} else {
8+
module.exports = {
9+
TextDecoder: TextDecoder,
10+
TextEncoder: TextEncoder
11+
}
12+
}

gulpfile.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
'use strict'
2+
3+
const watchify = require('watchify')
4+
const browserify = require('browserify')
5+
const gulp = require('gulp')
6+
const plumber = require('gulp-plumber')
7+
// const uglify = require('gulp-uglify')
8+
const source = require('vinyl-source-stream')
9+
const buffer = require('vinyl-buffer')
10+
const gutil = require('gulp-util')
11+
const sourcemaps = require('gulp-sourcemaps')
12+
const mocha = require('gulp-mocha') // Unit testing
13+
const rename = require('gulp-rename')
14+
const babelify = require('babelify')
15+
const istanbul = require('gulp-babel-istanbul')
16+
const clean = require('gulp-clean')
17+
18+
require('./test')
19+
20+
// Browsersync + Gulp.js
21+
// http://www.browsersync.cn/docs/gulp/
22+
23+
function compile (watch) {
24+
let bundler = browserify('./jsmimeMain.js', {
25+
debug: true,
26+
standalone: 'jsmime'
27+
})
28+
bundler = bundler.transform(babelify)
29+
bundler.on('log', gutil.log)
30+
31+
function rebundle () {
32+
return bundler.bundle()
33+
.on('error', function (err) { console.error(err); this.emit('end') })
34+
.pipe(plumber())
35+
.pipe(source('./jsmimeMain.js'))
36+
.pipe(rename('jsmime.js'))
37+
.pipe(buffer())
38+
// .pipe(uglify())
39+
.pipe(sourcemaps.init({ loadMaps: true }))
40+
.pipe(sourcemaps.write('./'))
41+
.pipe(gulp.dest('./dist'))
42+
.pipe(gulp.dest('./'))
43+
}
44+
45+
if (watch) {
46+
bundler = watchify(bundler)
47+
bundler.on('update', function () {
48+
console.log('-> bundling...')
49+
rebundle()
50+
})
51+
}
52+
53+
return rebundle()
54+
}
55+
56+
gulp.task('bundle', function () {
57+
return compile(false)
58+
})
59+
60+
gulp.task('watch-bundle', function () {
61+
return compile(true)
62+
})
63+
64+
function handleError (e) {
65+
console.error(e.toString())
66+
this.emit('end')
67+
}
68+
69+
gulp.task('cleanup', () => {
70+
return gulp.src(['./jsmime.*', './dist/*'], {read:false})
71+
.pipe(clean({force: true}))
72+
})
73+
74+
gulp.task('test', () => {
75+
// gulp-mocha needs filepaths so you can't have any plugins before it
76+
return gulp.src(['test/**/test_*.js'], {read: false})
77+
.pipe(
78+
mocha({
79+
ui: 'tdd',
80+
reporters : ['xunit-file', 'spec'],
81+
globals: ['define']
82+
}).on('error', handleError)
83+
)
84+
})
85+
86+
gulp.task('coverage-hook', () => {
87+
return gulp.src(['./*.js', '!jsmime.*'])
88+
// Covering files
89+
.pipe(istanbul())
90+
.pipe(istanbul.hookRequire())
91+
})
92+
93+
gulp.task('coverage', ['coverage-hook'], () => {
94+
// gulp-mocha needs filepaths so you can't have any plugins before it
95+
return gulp.src(['test/**/test_*.js'], {read: false})
96+
.pipe(
97+
mocha({
98+
ui: 'tdd',
99+
reporters : ['xunit-file', 'spec'],
100+
globals: ['define']
101+
}).on('error', handleError)
102+
)
103+
// Creating the reports after tests ran
104+
.pipe(
105+
istanbul.writeReports()
106+
)
107+
// Enforce a coverage of at least 90%
108+
.pipe(
109+
istanbul.enforceThresholds({ thresholds: { global: 10 } })
110+
)
111+
})

headeremitter.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
define(function(require) {
21
/**
32
* This module implements the code for emitting structured representations of
43
* MIME headers into their encoded forms. The code here is a companion to,
@@ -10,6 +9,7 @@ define(function(require) {
109
"use strict";
1110

1211
var mimeutils = require('./mimeutils');
12+
const { TextEncoder } = require('./encodings');
1313

1414
// Get the default structured encoders and add them to the map
1515
var structuredHeaders = require('./structuredHeaders');
@@ -768,12 +768,9 @@ function addStructuredEncoder(header, encoder) {
768768
preferredSpellings.set(lowerName, header);
769769
}
770770

771-
return Object.freeze({
771+
module.exports = Object.freeze({
772772
addStructuredEncoder: addStructuredEncoder,
773773
emitStructuredHeader: emitStructuredHeader,
774774
emitStructuredHeaders: emitStructuredHeaders,
775775
makeStreamingEmitter: makeStreamingEmitter
776776
});
777-
778-
});
779-

headerparser.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
define(function(require) {
21
/**
32
* This file implements the structured decoding of message header fields. It is
43
* part of the same system as found in mimemimeutils.js, and occasionally makes
@@ -8,6 +7,7 @@ define(function(require) {
87

98
"use strict";
109
var mimeutils = require('./mimeutils');
10+
const { TextDecoder } = require('./encodings');
1111

1212
/**
1313
* This is the API that we ultimately return.
@@ -1151,7 +1151,4 @@ headerparser.parseAddressingHeader = parseAddressingHeader;
11511151
headerparser.parseDateHeader = parseDateHeader;
11521152
headerparser.parseParameterHeader = parseParameterHeader;
11531153
headerparser.parseStructuredHeader = parseStructuredHeader;
1154-
return Object.freeze(headerparser);
1155-
1156-
});
1157-
1154+
module.exports = Object.freeze(headerparser);

jsmime.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

jsmimeDefine.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict'
2+
const path = require('path')
3+
define(function (require, exports, module) {
4+
const mainURI = path.join(module.uri, '../jsmimeMain')
5+
return require(mainURI)
6+
})

0 commit comments

Comments
 (0)