feat: prepare as pesde & npm package

* Add README with basic documentation
* Add exports.ts with required TS and Luau exports
* Add build script to extract Luau exports from exports.ts
* Setup pesde package manifest
* Apply formatting, setup stylua
This commit is contained in:
Erica Marigold 2024-12-27 11:14:28 +00:00
parent cb885f7b45
commit 659cde4017
Signed by: DevComp
GPG key ID: 429EF1C337871656
17 changed files with 354 additions and 88 deletions

4
.gitignore vendored
View file

@ -1,4 +1,8 @@
# ts
/node_modules
/out
/include
*.tsbuildinfo
# pesde
*_packages/

View file

@ -3,4 +3,4 @@
"tabWidth": 4,
"trailingComma": "all",
"useTabs": true
}
}

View file

@ -1,7 +1,3 @@
{
"recommendations": [
"esbenp.prettier-vscode",
"dbaeumer.vscode-eslint",
"DavidAnson.vscode-markdownlint"
]
"recommendations": ["esbenp.prettier-vscode", "dbaeumer.vscode-eslint", "DavidAnson.vscode-markdownlint"]
}

View file

@ -1,4 +1,4 @@
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
}
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"
}

View file

@ -1,3 +1,38 @@
# pako-roblox-ts
Fork of [pako](https://github.com/nodeca/pako) for Roblox-TS.
## Installation
Supported as a pesde (Luau) package and npm (roblox-ts) package:
```sh
# pesde
pesde add 0x5eal/pako
# roblox-ts
npm add @rbxts/pako
```
## Usage
### Luau
```luau
local serde = require("@lune/serde")
local pako = require("./luau_packages/pako")
local test = { my = "super", puper = { 456, 567 }, awesome = "pako" }
local compressed = pako.deflate(serde.encode("json", test))
local restored = serde.decode(pako.inflate(compressed, { to = "string" }))
```
### roblox-ts
```ts
import pako from "@rbxts/pako";
const test = { my: "super", puper: [456, 567], awesome: "pako" };
const compressed = pako.deflate(JSON.stringify(test));
const restored = JSON.parse(pako.inflate(compressed, { to: "string" }));
```

24
build.ts Normal file
View file

@ -0,0 +1,24 @@
// extracts the Luau exports section from a given file path
async function extractLuauExports(file: string): Promise<string | null> {
const input = await Bun.file(file).text();
const regex = /\/\/ @@@@@@@@@ LUAU_START @@@@@@@@@([\s\S]*?)\/\/ @@@@@@@@@ LUAU_END @@@@@@@@@/;
const match = input.match(regex);
if (match) {
// remove leading comment specifier from each line from each line
return match[1]
.split("\n")
.map((line) => line.replace(/\/\/\s?/, ""))
.join("\n")
.trim();
}
return null;
}
const EXPORTS_TS_FILE = "./src/exports.ts";
const EXPORTS_LUAU_FILE = "./out/init.luau";
console.log("extract and export Luau type thunk");
await extractLuauExports(EXPORTS_TS_FILE).then((exports) => Bun.write(EXPORTS_LUAU_FILE, exports!));
export {}; // treat as esmodule

BIN
bun.lockb Normal file → Executable file

Binary file not shown.

View file

@ -1,48 +1,54 @@
{
"name": "@rbxts/pako",
"version": "1.0.0",
"description": "fork of pako for Roblox-TS",
"main": "out/init.lua",
"scripts": {
"build": "rbxtsc",
"watch": "rbxtsc -w",
"prepublishOnly": "bun run build"
},
"keywords": [],
"contributors": [
"Andrei Tuputcyn (https://github.com/andr83)",
"Vitaly Puzrin (https://github.com/puzrin)",
"Friedel Ziegelmayer (https://github.com/dignifiedquire)",
"Kirill Efimov (https://github.com/Kirill89)",
"Jean-loup Gailly",
"Mark Adler",
"daimond113 (https://www.daimond113.com) <contact@daimond113.com>",
"Erica Marigold <hi@devcomp.xyz>"
],
"author": "",
"license": "(MIT AND Zlib)",
"types": "out/index.d.ts",
"files": [
"out",
"!**/*.tsbuildinfo"
],
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@rbxts/compiler-types": "3.0.0-types.0",
"@rbxts/types": "^1.0.813",
"@typescript-eslint/eslint-plugin": "^8.18.2",
"@typescript-eslint/parser": "^8.18.2",
"eslint": "^9.17.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-roblox-ts": "^0.0.36",
"prettier": "^3.4.2",
"roblox-ts": "^3.0.0",
"typescript": "^5.7.2"
},
"patchedDependencies": {
"@rbxts/types@1.0.813": "patches/@rbxts%2Ftypes@1.0.813.patch"
}
}
"name": "@rbxts/pako",
"version": "0.1.0",
"description": "fork of pako for Roblox-TS",
"main": "out/init.lua",
"scripts": {
"build": "rbxtsc --verbose && bun run build.ts",
"fmt": "prettier -w . && stylua .",
"watch": "rbxtsc -w",
"prepublishOnly": "bun run build"
},
"keywords": [],
"contributors": [
"Andrei Tuputcyn (https://github.com/andr83)",
"Vitaly Puzrin (https://github.com/puzrin)",
"Friedel Ziegelmayer (https://github.com/dignifiedquire)",
"Kirill Efimov (https://github.com/Kirill89)",
"Jean-loup Gailly",
"Mark Adler",
"daimond113 (https://www.daimond113.com) <contact@daimond113.com>",
"Erica Marigold <hi@devcomp.xyz>"
],
"author": "",
"license": "(MIT AND Zlib)",
"types": "out/index.d.ts",
"files": [
"out",
"!**/*.tsbuildinfo"
],
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@johnnymorganz/stylua-bin": "^2.0.2",
"@rbxts/compiler-types": "3.0.0-types.0",
"@rbxts/types": "^1.0.813",
"@types/bun": "^1.1.14",
"@typescript-eslint/eslint-plugin": "^8.18.2",
"@typescript-eslint/parser": "^8.18.2",
"eslint": "^9.17.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-roblox-ts": "^0.0.36",
"prettier": "^3.4.2",
"roblox-ts": "^3.0.0",
"typescript": "^5.7.2"
},
"patchedDependencies": {
"@rbxts/types@1.0.813": "patches/@rbxts%2Ftypes@1.0.813.patch"
},
"trustedDependencies": [
"@johnnymorganz/stylua-bin"
]
}

3
pesde.lock Normal file
View file

@ -0,0 +1,3 @@
name = "0x5eal/pako"
version = "0.1.0"
target = "luau"

30
pesde.toml Normal file
View file

@ -0,0 +1,30 @@
name = "0x5eal/pako"
version = "0.1.0"
description = "ZLib port for JavaScript, compiled to Luau"
repository = "https://github.com/0x5eal/rbxts-pako"
license = "MIT"
authors = [
"Andrei Tuputcyn (https://github.com/andr83)",
"Vitaly Puzrin (https://github.com/puzrin)",
"Friedel Ziegelmayer (https://github.com/dignifiedquire)",
"Kirill Efimov (https://github.com/Kirill89)",
"Jean-loup Gailly",
"Mark Adler",
"daimond113 (https://www.daimond113.com) <contact@daimond113.com>",
"Erica Marigold <hi@devcomp.xyz>"
]
includes = [
"pesde.toml",
"README.md",
"out/**/*.luau",
"!out/*.tsbuildinfo",
"!out/**/*.d.ts"
]
[target]
environment = "luau"
lib = "out/init.luau"
[indices]
default = "https://github.com/pesde-pkg/index"

View file

@ -1,20 +1,20 @@
"use strict";
import type { DeflateState } from "./zlib/deflate";
import type Messages from "./zlib/messages"
import type * as TypedArrays from "./utils/typedArrays";
import type Messages from "./zlib/messages";
import type * as TypedArrays from "./utils/typedArrays";
const { ZStream } = require("./zlib/zstream") as typeof import("./zlib/zstream");
const zlibDeflate = require("./zlib/deflate") as typeof import("./zlib/deflate");
const messages = require("./zlib/messages") as typeof Messages;
const { assign, flattenChunks } = require("./utils/common") as typeof import("./utils/common");
const { Uint8Array } = require("./utils/typedArrays") as typeof TypedArrays;
const { Uint8Array } = require("./utils/typedArrays") as typeof TypedArrays;
/* Public constants ==========================================================*/
/* ===========================================================================*/
export const {
const {
Z_NO_FLUSH,
Z_SYNC_FLUSH,
Z_FULL_FLUSH,
@ -113,7 +113,7 @@ export const {
* ```
**/
type Options = {
export type Options = {
level: number;
method: number;
chunkSize: number;
@ -238,7 +238,7 @@ export class Deflate {
strm.next_in = 0;
strm.avail_in = strm.input.length;
for (; ;) {
for (;;) {
if (strm.avail_out === 0) {
strm.output = new Uint8Array(chunkSize);
strm.next_out = 0;

176
src/exports.ts Normal file
View file

@ -0,0 +1,176 @@
//
// Luau exports
//
// @@@@@@@@@ LUAU_START @@@@@@@@@
// export type InflateOptions = {
// chunkSize: number?,
// windowBits: number?,
// to: string?,
// raw: boolean?,
// dictionary: (string | buffer)?
// }
//
// export type InflateRawOptions = {
// chunkSize: number?,
// windowBits: number?,
// to: string?,
// dictionary: (string | buffer)?
// }
//
// export type DeflateOptions = {
// level: number?,
// method: number?,
// chunkSize: number?,
// windowBits: number?,
// memLevel: number?,
// strategy: number?,
// raw: boolean?,
// gzip: boolean?,
// header: ({
// done: boolean?,
// time: number?,
// xflags: number?,
// os: number?,
// extra_len: number?,
// comment: string?,
// extra: buffer?,
// name: string?,
// hcrc: number?,
// text: number?,
// })?,
// dictionary: (buffer | string)?,
// }
//
// export type DeflateRawOptions = {
// level: number?,
// method: number?,
// chunkSize: number?,
// windowBits: number?,
// memLevel: number?,
// strategy: number?,
// gzip: boolean?,
// header: ({
// done: boolean?,
// time: number?,
// xflags: number?,
// os: number?,
// extra_len: number?,
// comment: string?,
// extra: buffer?,
// name: string?,
// hcrc: number?,
// text: number?,
// })?,
// dictionary: (buffer | string)?,
// }
//
// return require("./exports") :: {
// -- Inflate exports --
// Inflate: any, -- TODO: type Inflate class
// inflate: (input: string | buffer, options: InflateOptions?) -> (string | buffer)?,
// inflateRaw: (input: string | buffer, options: InflateRawOptions?) -> (string | buffer)?,
//
// -- Deflate exports --
// Deflate: any, -- TODO: type Deflate class
// deflate: (input: string | buffer, options: DeflateOptions?) -> buffer?,
// deflateRaw: (input: string | buffer, options: DeflateRawOptions?) -> buffer?,
// }
// @@@@@@@@@ LUAU_END @@@@@@@@@
const { Uint8Array } = require("./utils/typedArrays") as typeof import("./utils/typedArrays");
//
// Type exports
//
import type { Options as InflateOptionsInner } from "./inflate";
import type { Options as DeflateOptionsInner } from "./deflate";
export type InflateOptions = Omit<InflateOptionsInner, "dictionary"> & {
dictionary?: string | buffer;
};
export type DeflateOptions = Omit<DeflateOptionsInner, "dictionary"> & {
dictionary?: string | buffer;
};
//
// Inflate exports
//
const {
Inflate,
inflate: inflateImpl,
inflateRaw: inflateRawImpl,
} = require("./inflate") as typeof import("./inflate");
export function inflate(input: string | buffer, options?: Partial<InflateOptions>): string | buffer | undefined {
const inputArray = Uint8Array.from(typeIs(input, "string") ? buffer.fromstring(input) : input);
const modifiedOptions = options as Partial<InflateOptionsInner>;
if (typeIs(options?.dictionary, "buffer")) {
modifiedOptions.dictionary = Uint8Array.from(options.dictionary);
}
return inflateImpl(inputArray, modifiedOptions);
}
export function inflateRaw(
input: string | buffer,
options?: Exclude<Partial<InflateOptions>, "raw">,
): string | buffer | undefined {
const inputArray = Uint8Array.from(typeIs(input, "string") ? buffer.fromstring(input) : input);
const modifiedOptions = options as Exclude<Partial<InflateOptionsInner>, "raw">;
if (typeIs(options?.dictionary, "buffer")) {
modifiedOptions.dictionary = Uint8Array.from(options.dictionary);
}
return inflateRawImpl(inputArray, modifiedOptions);
}
export { Inflate };
//
// Deflate exports
//
const {
Deflate,
deflate: deflateImpl,
deflateRaw: deflateRawImpl,
} = require("./deflate") as typeof import("./deflate");
export function deflate(input: string | buffer, options?: Partial<DeflateOptions>): buffer | undefined {
const inputArray = Uint8Array.from(typeIs(input, "string") ? buffer.fromstring(input) : input);
const modifiedOptions = options as Partial<DeflateOptionsInner>;
if (typeIs(modifiedOptions?.dictionary, "buffer")) {
modifiedOptions.dictionary = Uint8Array.from(modifiedOptions.dictionary);
}
if (typeIs(modifiedOptions?.header?.extra, "buffer")) {
modifiedOptions.header.extra = Uint8Array.from(modifiedOptions.header.extra);
}
return deflateImpl(inputArray, modifiedOptions);
}
export function deflateRaw(
input: string | buffer,
options?: Exclude<Partial<DeflateOptions>, "raw">,
): buffer | undefined {
const inputArray = Uint8Array.from(typeIs(input, "string") ? buffer.fromstring(input) : input);
const modifiedOptions = options as Exclude<Partial<DeflateOptionsInner>, "raw">;
if (typeIs(modifiedOptions?.dictionary, "buffer")) {
modifiedOptions.dictionary = Uint8Array.from(modifiedOptions.dictionary);
}
if (typeIs(modifiedOptions?.header?.extra, "buffer")) {
modifiedOptions.header.extra = Uint8Array.from(modifiedOptions.header.extra);
}
return deflateRawImpl(inputArray, modifiedOptions);
}
export { Deflate };
//
// Constants exports
//
export const constants = require("./zlib/constants") as typeof import("./zlib/constants");

View file

@ -1,7 +1,7 @@
"use strict";
import type { InflateState } from "./zlib/inflate";
import type Messages from "./zlib/messages";
import type Messages from "./zlib/messages";
import type * as TypedArrays from "./utils/typedArrays";
const zlibInflate = require("./zlib/inflate") as typeof import("./zlib/inflate");
@ -14,16 +14,8 @@ const { assign, flattenChunks } = require("./utils/common") as typeof import("./
/* Public constants ==========================================================*/
/* ===========================================================================*/
export const {
Z_NO_FLUSH,
Z_FINISH,
Z_OK,
Z_STREAM_END,
Z_NEED_DICT,
Z_STREAM_ERROR,
Z_DATA_ERROR,
Z_MEM_ERROR,
} = require("./zlib/constants") as typeof import("./zlib/constants");
const { Z_NO_FLUSH, Z_FINISH, Z_OK, Z_STREAM_END, Z_NEED_DICT, Z_STREAM_ERROR, Z_DATA_ERROR, Z_MEM_ERROR } =
require("./zlib/constants") as typeof import("./zlib/constants");
/* ===========================================================================*/
@ -104,7 +96,7 @@ export const {
* ```
**/
type Options = {
export type Options = {
chunkSize: number;
windowBits: number;
to: string;

View file

@ -21,20 +21,18 @@ Copyright:
(C) 1995-2013 Jean-loup Gailly and Mark Adler
(C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
From zlib's README
=============================================================================
# From zlib's README
Acknowledgments:
The deflate format used by zlib was defined by Phil Katz. The deflate and
zlib specifications were written by L. Peter Deutsch. Thanks to all the
people who reported problems and suggested various improvements in zlib; they
are too numerous to cite here.
The deflate format used by zlib was defined by Phil Katz. The deflate and
zlib specifications were written by L. Peter Deutsch. Thanks to all the
people who reported problems and suggested various improvements in zlib; they
are too numerous to cite here.
Copyright notice:
(C) 1995-2013 Jean-loup Gailly and Mark Adler
(C) 1995-2013 Jean-loup Gailly and Mark Adler
Copyright (c) <''year''> <''copyright holders''>
@ -54,6 +52,5 @@ freely, subject to the following restrictions:
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jean-loup Gailly Mark Adler
jloup@gzip.org madler@alumni.caltech.edu
Jean-loup Gailly Mark Adler
jloup@gzip.org madler@alumni.caltech.edu

View file

@ -24,7 +24,8 @@ import type { TreeDesc } from "./trees";
import type Messages from "./messages";
import type * as TypedArrays from "../utils/typedArrays";
const { _tr_init, _tr_stored_block, _tr_flush_block, _tr_tally, _tr_align } = require("./trees") as typeof import("./trees");
const { _tr_init, _tr_stored_block, _tr_flush_block, _tr_tally, _tr_align } =
require("./trees") as typeof import("./trees");
const { adler32 } = require("./adler32") as typeof import("./adler32");
const { crc32 } = require("./crc32") as typeof import("./crc32");
const {

View file

@ -1,6 +1,6 @@
"use strict";
import type * as TypedArrays from "../utils/typedArrays";
import type * as TypedArrays from "../utils/typedArrays";
const { Z_UNKNOWN } = require("./constants") as typeof import("./constants");

View file

@ -23,5 +23,7 @@
"incremental": true,
"tsBuildInfoFile": "out/tsconfig.tsbuildinfo",
"declaration": true
}
},
"include": ["src/**/*.ts"]
}