mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-21 08:03:13 +09:00
Use fflate for zlib in the web interface
ModPack and map exchange strings need synchronous inflate and deflate. In the browser these came from the browserify-zlib polyfill, which brings pako, assert and util along. The browser build now imports fflate through a #zlib_sync subpath import, Node.js keeps node:zlib. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
909f6ab08b
commit
4d7c1020b0
8 files changed
+64
-3
No files matched your search
@@ -31,6 +31,13 @@
|
||||
},
|
||||
"import": "./src/link/WebSocket.browser.ts"
|
||||
},
|
||||
"#zlib_sync": {
|
||||
"node": {
|
||||
"types": "./src/zlib_sync.node.ts",
|
||||
"default": "./dist/node/src/zlib_sync.node.js"
|
||||
},
|
||||
"import": "./src/zlib_sync.browser.ts"
|
||||
},
|
||||
"#is_deep_strict_equal": {
|
||||
"node": {
|
||||
"types": "./src/is_deep_strict_equal.node.ts",
|
||||
@@ -56,6 +63,7 @@
|
||||
"ajv": "^8.20.0",
|
||||
"chalk": "catalog:",
|
||||
"fast-deep-equal": "^3.1.3",
|
||||
"fflate": "catalog:",
|
||||
"set-blocking": "catalog:",
|
||||
"triple-beam": "^1.4.1",
|
||||
"winston": "catalog:",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import zlib from "zlib";
|
||||
import * as zlib from "#zlib_sync";
|
||||
import { Type, Static } from "@sinclair/typebox";
|
||||
|
||||
import * as libSchema from "../schema.js";
|
||||
@@ -306,7 +306,7 @@ export default class ModPack {
|
||||
|
||||
// eslint-disable-next-line node/no-sync
|
||||
let buf = zlib.deflateSync(JSON.stringify(json));
|
||||
return buf.toString("base64");
|
||||
return Buffer.from(buf).toString("base64");
|
||||
}
|
||||
|
||||
static fromModPackString(modPackString: string) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import zlib from "zlib";
|
||||
import * as zlib from "#zlib_sync";
|
||||
|
||||
class MapReaderState {
|
||||
pos = 0;
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
/* eslint-disable node/no-sync */
|
||||
import { strToU8, unzlibSync, zlibSync } from "fflate";
|
||||
|
||||
export function inflateSync(data: Uint8Array): Uint8Array {
|
||||
try {
|
||||
return unzlibSync(data);
|
||||
} catch (err: any) {
|
||||
// Match the error code node:zlib gives for invalid data.
|
||||
throw Object.assign(new Error(err.message), { code: "Z_DATA_ERROR" });
|
||||
}
|
||||
}
|
||||
|
||||
export function deflateSync(data: Uint8Array | string): Uint8Array {
|
||||
return zlibSync(typeof data === "string" ? strToU8(data) : data);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import zlib from "zlib";
|
||||
|
||||
export function inflateSync(data: Uint8Array): Uint8Array {
|
||||
// eslint-disable-next-line node/no-sync
|
||||
return zlib.inflateSync(data);
|
||||
}
|
||||
|
||||
export function deflateSync(data: Uint8Array | string): Uint8Array {
|
||||
// eslint-disable-next-line node/no-sync
|
||||
return zlib.deflateSync(data);
|
||||
}
|
||||
@@ -19,6 +19,7 @@
|
||||
"./src/hash.ts",
|
||||
"./src/ini.ts",
|
||||
"./src/is_deep_strict_equal.browser.ts",
|
||||
"./src/zlib_sync.browser.ts",
|
||||
"./src/link/*.ts",
|
||||
"./src/lua_tools.ts",
|
||||
"./src/logging.ts",
|
||||
|
||||
@@ -23,6 +23,7 @@ catalog:
|
||||
antd: ^5.29.3
|
||||
chalk: ^6.0.1
|
||||
express: ^5.3.0
|
||||
fflate: ^0.8.3
|
||||
jsonwebtoken: ^9.0.3
|
||||
react: ^18.3.1
|
||||
react-dom: ^18.3.1
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/* eslint-disable node/no-sync */
|
||||
import assert from "node:assert/strict";
|
||||
import zlib from "node:zlib";
|
||||
|
||||
import * as browserZlib from "../../packages/lib/src/zlib_sync.browser.ts";
|
||||
|
||||
describe("lib/zlib_sync.browser", function() {
|
||||
const text = JSON.stringify({ name: "pack", mods: [{ name: "base", version: "2.0.0" }] });
|
||||
|
||||
it("should inflate data deflated by node:zlib", function() {
|
||||
const inflated = browserZlib.inflateSync(zlib.deflateSync(text));
|
||||
assert.equal(Buffer.from(inflated).toString(), text);
|
||||
});
|
||||
it("should deflate data node:zlib can inflate", function() {
|
||||
assert.equal(zlib.inflateSync(browserZlib.deflateSync(text)).toString(), text);
|
||||
const bytes = Buffer.from(text);
|
||||
assert.equal(zlib.inflateSync(browserZlib.deflateSync(bytes)).toString(), text);
|
||||
});
|
||||
it("should throw with a Z_ code on invalid data", function() {
|
||||
assert.throws(
|
||||
() => browserZlib.inflateSync(Buffer.from("not zlib data")),
|
||||
err => err.code === "Z_DATA_ERROR"
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user