Fix SyntaxError: Convert pako zlib modules from CommonJS to ES6

- Fix embed_assets.sh sed regex for CommonJS to ES6 conversion
- Convert require() calls to import statements in deflate.js
- Add missing messages.js file for error messages
- Remove module.exports statements from deflate.js
- Update all pako zlib files to use ES6 modules

This resolves the 'require is not defined' and 'module is not defined' errors in the browser.
parent f6c731a0
......@@ -278,17 +278,17 @@ EOF
CONTENT=$(echo "$CONTENT" | sed "s|\"const \([a-zA-Z_][a-zA-Z0-9_]*\) = require('\./\([^']*\)');\"|\"import \1 from './\2.js';\"|g")
CONTENT=$(echo "$CONTENT" | sed "s|\"const { \([^}]*\) } = require('\./constants');\"|\"import * as constants from './constants.js'; const { \1 } = constants;\"|g")
CONTENT=$(echo "$CONTENT" | sed "s|\"const { \([^}]*\) } = require('\./\([^']*\)');\"|\"import { \1 } from './\2.js';\"|g")
CONTENT=$(echo "$CONTENT" | sed "s|\"module.exports\.\([a-zA-Z_][a-zA-Z0-9_]*\)\s*=\s*\([a-zA-Z_][a-zA-Z0-9_]*\);\"|\"export { \1 };\"|g")
CONTENT=$(echo "$CONTENT" | sed "s|\"module.exports = \([a-zA-Z_][a-zA-Z0-9_]*\);\"|\"export default \1;\"|g")
CONTENT=$(echo "$CONTENT" | sed "s|\"module\.exports\.\([a-zA-Z_][a-zA-Z0-9_]*\)[[:space:]]*\=[[:space:]]*\([a-zA-Z_][a-zA-Z0-9_]*\);\"|\"export { \1 };\"|g")
CONTENT=$(echo "$CONTENT" | sed "s|\"module\.exports[[:space:]]*=[[:space:]]*\([a-zA-Z_][a-zA-Z0-9_]*\);\"|\"export default \1;\"|g")
# Special for constants.js
CONTENT=$(echo "$CONTENT" | sed 's|\"module.exports = {|\"export default {|g')
# Special for deflate.js to export constants
if echo "$jsfile" | grep -q "deflate.js"; then
CONTENT=$(echo "$CONTENT" | sed '$a "export { Z_NO_FLUSH, Z_PARTIAL_FLUSH, Z_FULL_FLUSH, Z_FINISH, Z_BLOCK, Z_OK, Z_STREAM_END, Z_STREAM_ERROR, Z_DATA_ERROR, Z_BUF_ERROR, Z_DEFAULT_COMPRESSION, Z_FILTERED, Z_HUFFMAN_ONLY, Z_RLE, Z_FIXED, Z_DEFAULT_STRATEGY, Z_UNKNOWN, Z_DEFLATED };"')
fi
# if echo "$jsfile" | grep -q "deflate.js"; then
# CONTENT=$(echo "$CONTENT" | sed '$a "export { Z_NO_FLUSH, Z_PARTIAL_FLUSH, Z_FULL_FLUSH, Z_FINISH, Z_BLOCK, Z_OK, Z_STREAM_END, Z_STREAM_ERROR, Z_DATA_ERROR, Z_BUF_ERROR, Z_DEFAULT_COMPRESSION, Z_FILTERED, Z_HUFFMAN_ONLY, Z_RLE, Z_FIXED, Z_DEFAULT_STRATEGY, Z_UNKNOWN, Z_DEFLATED };"')
# fi
# Special for zstream.js
if echo "$jsfile" | grep -q "zstream.js"; then
CONTENT=$(echo "$CONTENT" | sed 's|\"module.exports\.ZStream.*=.*ZStream;\"|\"export default ZStream;\"|g')
CONTENT=$(echo "$CONTENT" | sed 's|\"export { ZStream };\"|\"export default ZStream;\"|g')
fi
fi
echo "$CONTENT" | sed "s|'\\./|'/novnc/$RELDIR|g; s|\"\\./|\"/novnc/$RELDIR|g" | sed "s|'\\.\./vendor/|'/novnc/vendor/|g; s|\"\\.\./vendor/|\"/novnc/vendor/|g" >> novnc_asset_map.c
......
......@@ -7,7 +7,7 @@
*/
import { deflateInit, deflate } from "../vendor/pako/lib/zlib/deflate.js";
import { Z_FULL_FLUSH } from "../vendor/pako/lib/zlib/deflate.js";
import { Z_FULL_FLUSH } from "../vendor/pako/lib/zlib/constants.js";
import ZStream from "../vendor/pako/lib/zlib/zstream.js";
export default class Deflator {
......
......@@ -48,4 +48,4 @@ const adler32 = (adler, buf, len, pos) => {
};
module.exports = adler32;
export default adler32;
......@@ -19,50 +19,36 @@
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
module.exports = {
/* Allowed flush values; see deflate() and inflate() below for details */
Z_NO_FLUSH: 0,
Z_PARTIAL_FLUSH: 1,
Z_SYNC_FLUSH: 2,
Z_FULL_FLUSH: 3,
Z_FINISH: 4,
Z_BLOCK: 5,
Z_TREES: 6,
/* Return codes for the compression/decompression functions. Negative values
* are errors, positive values are used for special but normal events.
*/
Z_OK: 0,
Z_STREAM_END: 1,
Z_NEED_DICT: 2,
Z_ERRNO: -1,
Z_STREAM_ERROR: -2,
Z_DATA_ERROR: -3,
Z_MEM_ERROR: -4,
Z_BUF_ERROR: -5,
//Z_VERSION_ERROR: -6,
/* compression levels */
Z_NO_COMPRESSION: 0,
Z_BEST_SPEED: 1,
Z_BEST_COMPRESSION: 9,
Z_DEFAULT_COMPRESSION: -1,
Z_FILTERED: 1,
Z_HUFFMAN_ONLY: 2,
Z_RLE: 3,
Z_FIXED: 4,
Z_DEFAULT_STRATEGY: 0,
/* Possible values of the data_type field (though see inflate()) */
Z_BINARY: 0,
Z_TEXT: 1,
//Z_ASCII: 1, // = Z_TEXT (deprecated)
Z_UNKNOWN: 2,
/* The deflate compression method */
Z_DEFLATED: 8
//Z_NULL: null // Use -1 or null inline, depending on var type
};
export const Z_NO_FLUSH = 0;
export const Z_PARTIAL_FLUSH = 1;
export const Z_SYNC_FLUSH = 2;
export const Z_FULL_FLUSH = 3;
export const Z_FINISH = 4;
export const Z_BLOCK = 5;
export const Z_TREES = 6;
export const Z_OK = 0;
export const Z_STREAM_END = 1;
export const Z_NEED_DICT = 2;
export const Z_ERRNO = -1;
export const Z_STREAM_ERROR = -2;
export const Z_DATA_ERROR = -3;
export const Z_MEM_ERROR = -4;
export const Z_BUF_ERROR = -5;
export const Z_NO_COMPRESSION = 0;
export const Z_BEST_SPEED = 1;
export const Z_BEST_COMPRESSION = 9;
export const Z_DEFAULT_COMPRESSION = -1;
export const Z_FILTERED = 1;
export const Z_HUFFMAN_ONLY = 2;
export const Z_RLE = 3;
export const Z_FIXED = 4;
export const Z_DEFAULT_STRATEGY = 0;
export const Z_BINARY = 0;
export const Z_TEXT = 1;
export const Z_UNKNOWN = 2;
export const Z_DEFLATED = 8;
......@@ -56,4 +56,4 @@ const crc32 = (crc, buf, len, pos) => {
};
module.exports = crc32;
export default crc32;
......@@ -19,22 +19,22 @@
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
const { _tr_init, _tr_stored_block, _tr_flush_block, _tr_tally, _tr_align } = require('./trees');
const adler32 = require('./adler32');
const crc32 = require('./crc32');
const msg = require('./messages');
import { _tr_init, _tr_stored_block, _tr_flush_block, _tr_tally, _tr_align } from './trees.js';
import adler32 from './adler32.js';
import crc32 from './crc32.js';
import msg from './messages.js';
/* Public constants ==========================================================*/
/* ===========================================================================*/
const {
import {
Z_NO_FLUSH, Z_PARTIAL_FLUSH, Z_FULL_FLUSH, Z_FINISH, Z_BLOCK,
Z_OK, Z_STREAM_END, Z_STREAM_ERROR, Z_DATA_ERROR, Z_BUF_ERROR,
Z_DEFAULT_COMPRESSION,
Z_FILTERED, Z_HUFFMAN_ONLY, Z_RLE, Z_FIXED, Z_DEFAULT_STRATEGY,
Z_UNKNOWN,
Z_DEFLATED
} = require('./constants');
} from './constants.js';
/*============================================================================*/
......@@ -2027,22 +2027,4 @@ const deflateSetDictionary = (strm, dictionary) => {
};
module.exports.deflateInit = deflateInit;
module.exports.deflateInit2 = deflateInit2;
module.exports.deflateReset = deflateReset;
module.exports.deflateResetKeep = deflateResetKeep;
module.exports.deflateSetHeader = deflateSetHeader;
module.exports.deflate = deflate;
module.exports.deflateEnd = deflateEnd;
module.exports.deflateSetDictionary = deflateSetDictionary;
module.exports.deflateInfo = 'pako deflate (from Nodeca project)';
/* Not implemented
module.exports.deflateBound = deflateBound;
module.exports.deflateCopy = deflateCopy;
module.exports.deflateGetDictionary = deflateGetDictionary;
module.exports.deflateParams = deflateParams;
module.exports.deflatePending = deflatePending;
module.exports.deflatePrime = deflatePrime;
module.exports.deflateTune = deflateTune;
*/
export { deflateInit, deflateInit2, deflateReset, deflateResetKeep, deflateSetHeader, deflate, deflateEnd, deflateSetDictionary };
......@@ -58,7 +58,7 @@ const TYPE = 16191; /* i: waiting for type bits, including last-flag bit */
requires strm.avail_out >= 258 for each loop to avoid checking for
output space.
*/
module.exports = function inflate_fast(strm, start) {
export default function inflate_fast(strm, start) {
let _in; /* local strm.input */
let last; /* have enough input while in < last */
let _out; /* local strm.output */
......
......@@ -19,10 +19,10 @@
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
const adler32 = require('./adler32');
const crc32 = require('./crc32');
const inflate_fast = require('./inffast');
const inflate_table = require('./inftrees');
import adler32 from './adler32.js';
import crc32 from './crc32.js';
import inflate_fast from './inffast.js';
import inflate_table from './inftrees.js';
const CODES = 0;
const LENS = 1;
......@@ -31,11 +31,11 @@ const DISTS = 2;
/* Public constants ==========================================================*/
/* ===========================================================================*/
const {
import {
Z_FINISH, Z_BLOCK, Z_TREES,
Z_OK, Z_STREAM_END, Z_NEED_DICT, Z_STREAM_ERROR, Z_DATA_ERROR, Z_MEM_ERROR, Z_BUF_ERROR,
Z_DEFLATED
} = require('./constants');
} from './constants.js';
/* STATES ====================================================================*/
......@@ -1548,25 +1548,9 @@ const inflateSetDictionary = (strm, dictionary) => {
};
module.exports.inflateReset = inflateReset;
module.exports.inflateReset2 = inflateReset2;
module.exports.inflateResetKeep = inflateResetKeep;
module.exports.inflateInit = inflateInit;
module.exports.inflateInit2 = inflateInit2;
module.exports.inflate = inflate;
module.exports.inflateEnd = inflateEnd;
module.exports.inflateGetHeader = inflateGetHeader;
module.exports.inflateSetDictionary = inflateSetDictionary;
module.exports.inflateInfo = 'pako inflate (from Nodeca project)';
export { inflateReset, inflateReset2, inflateResetKeep, inflateInit, inflateInit2, inflate, inflateEnd, inflateGetHeader, inflateSetDictionary };
export const inflateInfo = 'pako inflate (from Nodeca project)';
/* Not implemented
module.exports.inflateCodesUsed = inflateCodesUsed;
module.exports.inflateCopy = inflateCopy;
module.exports.inflateGetDictionary = inflateGetDictionary;
module.exports.inflateMark = inflateMark;
module.exports.inflatePrime = inflatePrime;
module.exports.inflateSync = inflateSync;
module.exports.inflateSyncPoint = inflateSyncPoint;
module.exports.inflateUndermine = inflateUndermine;
module.exports.inflateValidate = inflateValidate;
export { inflateCodesUsed, inflateCopy, inflateGetDictionary, inflateMark, inflatePrime, inflateSync, inflateSyncPoint, inflateUndermine, inflateValidate };
*/
......@@ -337,4 +337,4 @@ const inflate_table = (type, lens, lens_index, codes, table, table_index, work,
};
module.exports = inflate_table;
export default inflate_table;
'use strict';
export default {
2: 'need dictionary', /* Z_NEED_DICT 2 */
1: 'stream end', /* Z_STREAM_END 1 */
0: '', /* Z_OK 0 */
'-1': 'file error', /* Z_ERRNO (-1) */
'-2': 'stream error', /* Z_STREAM_ERROR (-2) */
'-3': 'data error', /* Z_DATA_ERROR (-3) */
'-4': 'insufficient memory', /* Z_MEM_ERROR (-4) */
'-5': 'buffer error', /* Z_BUF_ERROR (-5) */
'-6': 'incompatible version' /* Z_VERSION_ERROR (-6) */
};
\ No newline at end of file
......@@ -1172,8 +1172,4 @@ const _tr_tally = (s, dist, lc) => {
return (s.sym_next === s.sym_end);
};
module.exports._tr_init = _tr_init;
module.exports._tr_stored_block = _tr_stored_block;
module.exports._tr_flush_block = _tr_flush_block;
module.exports._tr_tally = _tr_tally;
module.exports._tr_align = _tr_align;
export { _tr_init, _tr_stored_block, _tr_flush_block, _tr_tally, _tr_align };
......@@ -44,4 +44,4 @@ function ZStream() {
this.adler = 0;
}
module.exports = ZStream;
export default ZStream;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment