Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
# branches: [ master ]
pull_request:
# branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [0.10, 12.x, 14.x, 16.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}

- run: cd test/webpack && npm run preinstall && npm install
- run: npm i && npm test
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,5 @@ $ # To view test coverage:
$ npm run coverage
$ open coverage/lcov-report/index.html
```

ci-test-210909
6 changes: 4 additions & 2 deletions encodings/dbcs-codec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"use strict";
var Buffer = require("safer-buffer").Buffer;
import pkg from 'safer-buffer'
const { Buffer } = pkg

// Multibyte codec. In this scheme, a character is represented by 1 or more bytes.
// Our codec supports UTF-16 surrogates, extensions for GB18030 and unicode sequences.
// To save memory and loading time, we read table files only when requested.

exports._dbcs = DBCSCodec;
const _dbcs = DBCSCodec;

var UNASSIGNED = -1,
GB18030_CODE = -2,
Expand Down Expand Up @@ -595,3 +596,4 @@ function findIdx(table, val) {
return l;
}

export default { _dbcs }
32 changes: 21 additions & 11 deletions encodings/dbcs-data.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
"use strict";

import fs from 'fs';
// Description of supported double byte encodings and aliases.
// Tables are not require()-d until they are needed to speed up library load.
// require()-s are direct to support Browserify.

module.exports = {

/**
*
* @param {String} url
* @returns {Array}
*/
function readJsonFile (url) {
return JSON.parse(fs.readFileSync(url, 'utf8'))
}

export default {

// == Japanese/ShiftJIS ====================================================
// All japanese encodings are based on JIS X set of standards:
// JIS X 0201 - Single-byte encoding of ASCII + ¥ + Kana chars at 0xA1-0xDF.
Expand Down Expand Up @@ -40,7 +50,7 @@ module.exports = {

'shiftjis': {
type: '_dbcs',
table: function() { return require('./tables/shiftjis.json') },
table: () => readJsonFile('./encodings/tables/shiftjis.json'),
encodeAdd: {'\u00a5': 0x5C, '\u203E': 0x7E},
encodeSkipVals: [{from: 0xED40, to: 0xF940}],
},
Expand All @@ -57,7 +67,7 @@ module.exports = {

'eucjp': {
type: '_dbcs',
table: function() { return require('./tables/eucjp.json') },
table: () => readJsonFile('./encodings/tables/eucjp.json'),
encodeAdd: {'\u00a5': 0x5C, '\u203E': 0x7E},
},

Expand All @@ -84,13 +94,13 @@ module.exports = {
'936': 'cp936',
'cp936': {
type: '_dbcs',
table: function() { return require('./tables/cp936.json') },
table: () => readJsonFile('./encodings/tables/cp936.json')
},

// GBK (~22000 chars) is an extension of CP936 that added user-mapped chars and some other.
'gbk': {
type: '_dbcs',
table: function() { return require('./tables/cp936.json').concat(require('./tables/gbk-added.json')) },
table: () => readJsonFile('./encodings/tables/cp936.json').concat(readJsonFile('./encodings/tables/gbk-added.json'))
},
'xgbk': 'gbk',
'isoir58': 'gbk',
Expand All @@ -102,8 +112,8 @@ module.exports = {
// http://www.khngai.com/chinese/charmap/tblgbk.php?page=0
'gb18030': {
type: '_dbcs',
table: function() { return require('./tables/cp936.json').concat(require('./tables/gbk-added.json')) },
gb18030: function() { return require('./tables/gb18030-ranges.json') },
table: () => readJsonFile('./encodings/tables/cp936.json').concat(readJsonFile('./encodings/tables/gbk-added.json')),
gb18030: () => readJsonFile('./encodings/tables/gb18030-ranges.json'),
encodeSkipVals: [0x80],
encodeAdd: {'€': 0xA2E3},
},
Expand All @@ -118,7 +128,7 @@ module.exports = {
'949': 'cp949',
'cp949': {
type: '_dbcs',
table: function() { return require('./tables/cp949.json') },
table: () => readJsonFile('./encodings/tables/cp949.json')
},

'cseuckr': 'cp949',
Expand Down Expand Up @@ -159,14 +169,14 @@ module.exports = {
'950': 'cp950',
'cp950': {
type: '_dbcs',
table: function() { return require('./tables/cp950.json') },
table: () => readJsonFile('./encodings/tables/cp950.json'),
},

// Big5 has many variations and is an extension of cp950. We use Encoding Standard's as a consensus.
'big5': 'big5hkscs',
'big5hkscs': {
type: '_dbcs',
table: function() { return require('./tables/cp950.json').concat(require('./tables/big5-added.json')) },
table: () => readJsonFile('./encodings/tables/cp950.json').concat(readJsonFile('./encodings/tables/big5-added.json')),
encodeSkipVals: [
// Although Encoding Standard says we should avoid encoding to HKSCS area (See Step 1 of
// https://encoding.spec.whatwg.org/#index-big5-pointer), we still do it to increase compatibility with ICU.
Expand Down
41 changes: 24 additions & 17 deletions encodings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,29 @@

// Update this array if you add/rename/remove files in this directory.
// We support Browserify by skipping automatic module discovery and requiring modules directly.
var modules = [
require("./internal"),
require("./utf32"),
require("./utf16"),
require("./utf7"),
require("./sbcs-codec"),
require("./sbcs-data"),
require("./sbcs-data-generated"),
require("./dbcs-codec"),
require("./dbcs-data"),
];

const encodingList = {}
await Promise.all([
import("./internal.js"),
import("./utf32.js"),
import("./utf16.js"),
import("./utf7.js"),
import("./sbcs-codec.js"),
import("./sbcs-data.js"),
import("./sbcs-data-generated.js"),
import("./dbcs-codec.js"),
import("./dbcs-data.js")
])
.then(res => {
// Put all encoding/alias/codec definitions to single object and export it.
for (var i = 0; i < modules.length; i++) {
var module = modules[i];
for (var enc in module)
if (Object.prototype.hasOwnProperty.call(module, enc))
exports[enc] = module[enc];
}
for (var i = 0; i < res.length; i++) {
var module = res[i].default;
for (var enc in module) {
if (Object.prototype.hasOwnProperty.call(module, enc)) {
encodingList[enc] = module[enc];
}
}
}
});

export default encodingList
8 changes: 4 additions & 4 deletions encodings/internal.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"use strict";
var Buffer = require("safer-buffer").Buffer;

import pkg from 'safer-buffer'
const { Buffer } = pkg
// Export Node.js internal encodings.

module.exports = {
export default {
// Encodings
utf8: { type: "_internal", bomAware: true},
cesu8: { type: "_internal", bomAware: true},
Expand Down Expand Up @@ -46,7 +46,7 @@ InternalCodec.prototype.decoder = InternalDecoder;
//------------------------------------------------------------------------------

// We use node.js internal decoder. Its signature is the same as ours.
var StringDecoder = require('string_decoder').StringDecoder;
import { StringDecoder } from "string_decoder"

if (!StringDecoder.prototype.end) // Node v0.8 doesn't have this method.
StringDecoder.prototype.end = function() {};
Expand Down
7 changes: 5 additions & 2 deletions encodings/sbcs-codec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"use strict";
var Buffer = require("safer-buffer").Buffer;
import pkg from 'safer-buffer'
const { Buffer } = pkg

// Single-byte codec. Needs a 'chars' string parameter that contains 256 or 128 chars that
// correspond to encoded bytes (if 128 - then lower half is ASCII).

exports._sbcs = SBCSCodec;
const _sbcs = SBCSCodec
function SBCSCodec(codecOptions, iconv) {
if (!codecOptions)
throw new Error("SBCS codec is called without the data.")
Expand Down Expand Up @@ -70,3 +71,5 @@ SBCSDecoder.prototype.write = function(buf) {

SBCSDecoder.prototype.end = function() {
}

export default { _sbcs }
2 changes: 1 addition & 1 deletion encodings/sbcs-data-generated.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

// Generated data for sbcs codec. Don't edit manually. Regenerate using generation/gen-sbcs.js script.
module.exports = {
export default {
"437": "cp437",
"737": "cp737",
"775": "cp775",
Expand Down
2 changes: 1 addition & 1 deletion encodings/sbcs-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// Manually added data to be used by sbcs codec in addition to generated one.

module.exports = {
export default {
// Not supported by iconv, not sure why.
"10029": "maccenteuro",
"maccenteuro": {
Expand Down
9 changes: 5 additions & 4 deletions encodings/utf16.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"use strict";
var Buffer = require("safer-buffer").Buffer;
import pkg from 'safer-buffer'
const { Buffer } = pkg

// Note: UTF16-LE (or UCS2) codec is Node.js native. See encodings/internal.js

// == UTF16-BE codec. ==========================================================

exports.utf16be = Utf16BECodec;
const utf16be = Utf16BECodec
function Utf16BECodec() {
}

Expand Down Expand Up @@ -73,7 +74,7 @@ Utf16BEDecoder.prototype.end = function() {

// Encoder uses UTF-16LE and prepends BOM (which can be overridden with addBOM: false).

exports.utf16 = Utf16Codec;
const utf16 = Utf16Codec
function Utf16Codec(codecOptions, iconv) {
this.iconv = iconv;
}
Expand Down Expand Up @@ -194,4 +195,4 @@ function detectEncoding(bufs, defaultEncoding) {
return defaultEncoding || 'utf-16le';
}


export default { utf16be, utf16 }
19 changes: 11 additions & 8 deletions encodings/utf32.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
'use strict';

var Buffer = require('safer-buffer').Buffer;
import pkg from 'safer-buffer'
const { Buffer } = pkg

// == UTF32-LE/BE codec. ==========================================================

exports._utf32 = Utf32Codec;
const _utf32 = Utf32Codec

function Utf32Codec(codecOptions, iconv) {
this.iconv = iconv;
this.bomAware = true;
this.isLE = codecOptions.isLE;
}

exports.utf32le = { type: '_utf32', isLE: true };
exports.utf32be = { type: '_utf32', isLE: false };
const utf32le = { type: '_utf32', isLE: true }
const utf32be = { type: '_utf32', isLE: false }

// Aliases
exports.ucs4le = 'utf32le';
exports.ucs4be = 'utf32be';
const ucs4le = 'utf32le'
const ucs4be = 'utf32be'

Utf32Codec.prototype.encoder = Utf32Encoder;
Utf32Codec.prototype.decoder = Utf32Decoder;
Expand Down Expand Up @@ -189,8 +190,8 @@ Utf32Decoder.prototype.end = function() {

// Encoder prepends BOM (which can be overridden with (addBOM: false}).

exports.utf32 = Utf32AutoCodec;
exports.ucs4 = 'utf32';
const utf32 = Utf32AutoCodec
const ucs4 = 'utf32'

function Utf32AutoCodec(options, iconv) {
this.iconv = iconv;
Expand Down Expand Up @@ -317,3 +318,5 @@ function detectEncoding(bufs, defaultEncoding) {
// Couldn't decide (likely all zeros or not enough data).
return defaultEncoding || 'utf-32le';
}

export default { _utf32, utf32le, utf32be, ucs4le, ucs4be, utf32, ucs4 }
11 changes: 6 additions & 5 deletions encodings/utf7.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"use strict";
var Buffer = require("safer-buffer").Buffer;
import pkg from 'safer-buffer'
const { Buffer } = pkg

// UTF-7 codec, according to https://tools.ietf.org/html/rfc2152
// See also below a UTF-7-IMAP codec, according to http://tools.ietf.org/html/rfc3501#section-5.1.3

exports.utf7 = Utf7Codec;
exports.unicode11utf7 = 'utf7'; // Alias UNICODE-1-1-UTF-7
const utf7 = Utf7Codec
const unicode11utf7 = 'utf7' // Alias UNICODE-1-1-UTF-7
function Utf7Codec(codecOptions, iconv) {
this.iconv = iconv;
};
Expand Down Expand Up @@ -129,7 +130,7 @@ Utf7Decoder.prototype.end = function() {
// * "-&" while in base64 is not allowed.


exports.utf7imap = Utf7IMAPCodec;
const utf7imap = Utf7IMAPCodec
function Utf7IMAPCodec(codecOptions, iconv) {
this.iconv = iconv;
};
Expand Down Expand Up @@ -287,4 +288,4 @@ Utf7IMAPDecoder.prototype.end = function() {
return res;
}


export default { utf7, unicode11utf7, utf7imap }
5 changes: 3 additions & 2 deletions lib/bom-handling.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var BOMChar = '\uFEFF';

exports.PrependBOM = PrependBOMWrapper
const PrependBOM = PrependBOMWrapper
function PrependBOMWrapper(encoder, options) {
this.encoder = encoder;
this.addBOM = true;
Expand All @@ -24,7 +24,7 @@ PrependBOMWrapper.prototype.end = function() {

//------------------------------------------------------------------------------

exports.StripBOM = StripBOMWrapper;
const StripBOM = StripBOMWrapper
function StripBOMWrapper(decoder, options) {
this.decoder = decoder;
this.pass = false;
Expand All @@ -50,3 +50,4 @@ StripBOMWrapper.prototype.end = function() {
return this.decoder.end();
}

export default { PrependBOM, StripBOM }
Loading