diff --git a/scripts/generate-data.js b/scripts/generate-data.js index c50f089..51e62dc 100644 --- a/scripts/generate-data.js +++ b/scripts/generate-data.js @@ -2,7 +2,6 @@ const resources = require('../data/resources.js'); const generateData = require('../index.js'); -const utils = require('../scripts/utils.js'); // ----------------------------------------------------------------------------- @@ -28,7 +27,9 @@ const complicatedWorkThatTakesTime = (resource, callback) => { console.log('[%s] Worker %d \u2192 Unicode v%s', getTime(), cluster.worker.id, version); + console.groupCollapsed(); generateData(version); + console.groupEnd(); complicatedWorkThatTakesTime( resource.slice(1), @@ -40,10 +41,16 @@ const complicatedWorkThatTakesTime = (resource, callback) => { } }; -if (cluster.isMaster) { +if (cluster.isPrimary) { for (let index = 0; index < numCPUs; index++) { - cluster.fork(); + const worker = cluster.fork(); + worker.on('message', (error) => { + for (const id in cluster.workers) { + cluster.workers[id].kill(); + } + throw new Error(`Worker ${worker.id} encountered an error: ${error}`); + }) } cluster.on('online', (worker) => { @@ -76,4 +83,14 @@ if (cluster.isMaster) { }); }); + process.on('uncaughtException', (error) => { + console.error(error); + process.send(error.message); + }); + + process.on('unhandledRejection', (error) => { + console.error(error); + process.send(error.message || error); + }); + } diff --git a/scripts/utils.js b/scripts/utils.js index cdd42f0..21baff8 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -11,7 +11,7 @@ const gzipInline = function(data) { if (data instanceof Map) { return `new Map(${ gzipInline([...data]) })`; } - const json = jsesc(data, { 'json': true }); + const json = JSON.stringify(data); const gzipBuffer = zlib.gzipSync(json); const str = gzipBuffer.toString('base64'); return `JSON.parse(require('zlib').gunzipSync(Buffer.from('${ str }','base64')))`; @@ -84,7 +84,13 @@ const writeFiles = function(options) { return; } const dirMap = {}; - const bidiMirroringGlyphMap = []; + /** + * A list of flatten (x, y) pairs, + * where x is a codepoint, y := codepoint(z) - x, + * where z is the BidiMirroringGlyph of character(x) and codepoint(z) > x + * @type number[] + */ + const bidiMirroringGlyphFlatPairs = []; const auxMap = {}; Object.keys(map).forEach(function(item) { const codePoints = map[item]; @@ -112,10 +118,11 @@ const writeFiles = function(options) { ) ) { if (type == 'Bidi_Mirroring_Glyph') { - const shortName = item.codePointAt(0); + const toCodepoint = item.codePointAt(0); codePoints.toArray().forEach(function(codePoint) { - console.assert(!bidiMirroringGlyphMap[codePoint]); - bidiMirroringGlyphMap[codePoint] = shortName; + if (codePoint < toCodepoint) { + bidiMirroringGlyphFlatPairs.push(codePoint, toCodepoint - codePoint); + } }); } else { if (!auxMap[type]) { @@ -124,34 +131,7 @@ const writeFiles = function(options) { auxMap[type].push([item, codePoints]); } } - if (isNamesCanon) { - return; - } - - if (type == 'Bidi_Mirroring_Glyph') { - const dir = path.resolve( - __dirname, '..', - 'output', 'unicode-' + version, type - ); - if (!hasKey(dirMap, type)) { - dirMap[type] = []; - } - fs.mkdirSync(dir, { recursive: true }); - // `Bidi_Mirroring_Glyph/index.js` - // Note: `Bidi_Mirroring_Glyph` doesn’t have repeated strings; don’t gzip. - const flatPairs = bidiMirroringGlyphMap - .flatMap((a, b) => a < b ? [a, b - a] : []); - const output = [ - `const chr=String.fromCodePoint`, - `const pair=(t,u,v)=>[t?u+v:v,chr(t?u:u+v)]`, - `module.exports=new Map(${ - jsesc(flatPairs) - }.map((v,i,a)=>pair(i&1,a[i^1],v)))` - ].join(';'); - fs.writeFileSync( - path.resolve(dir, 'index.js'), - output - ); + if (isNamesCanon || type == 'Bidi_Mirroring_Glyph') { return; } @@ -211,6 +191,31 @@ const writeFiles = function(options) { `module.exports=${ symbolsExports }` ); }); + if (options.type == 'Bidi_Mirroring_Glyph') { + const type = options.type; + const dir = path.resolve( + __dirname, '..', + 'output', 'unicode-' + version, type + ); + if (!hasKey(dirMap, type)) { + dirMap[type] = []; + } + fs.mkdirSync(dir, { recursive: true }); + // `Bidi_Mirroring_Glyph/index.js` + // Note: `Bidi_Mirroring_Glyph` doesn’t have repeated strings; don’t gzip. + const output = [ + `const chr=String.fromCodePoint`, + `const pair=(t,u,v)=>[t?u+v:v,chr(t?u:u+v)]`, + `module.exports=new Map(${ + JSON.stringify(bidiMirroringGlyphFlatPairs) + }.map((v,i,a)=>pair(i&1,a[i^1],v)))` + ].join(';'); + fs.writeFileSync( + path.resolve(dir, 'index.js'), + output + ); + return; + } Object.keys(auxMap).forEach(function(type) { const dir = path.resolve( __dirname, '..',